Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] Annotation of /branches/vis12/src/compiler/c-target/c-target.sml
ViewVC logotype

Annotation of /branches/vis12/src/compiler/c-target/c-target.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1774 - (view) (download)

1 : jhr 1115 (* c-target.sml
2 :     *
3 :     * COPYRIGHT (c) 2011 The Diderot Project (http://diderot-language.cs.uchicago.edu)
4 :     * All rights reserved.
5 :     *)
6 :    
7 :     structure CTarget : TARGET =
8 :     struct
9 :    
10 :     structure IL = TreeIL
11 :     structure V = IL.Var
12 :     structure Ty = IL.Ty
13 :     structure CL = CLang
14 : jhr 1376 structure N = CNames
15 : jhr 1115
16 : jhr 1716 type target_desc = TargetUtil.target_desc
17 :    
18 : jhr 1640 (* variable translation *)
19 :     structure TrVar =
20 :     struct
21 :     type env = CL.typed_var TreeIL.Var.Map.map
22 :     fun lookup (env, x) = (case V.Map.find (env, x)
23 :     of SOME(CL.V(_, x')) => x'
24 :     | NONE => raise Fail(concat["lookup(_, ", V.name x, ")"])
25 :     (* end case *))
26 :     (* translate a variable that occurs in an l-value context (i.e., as the target of an assignment) *)
27 : jhr 1706 fun lvalueVar (env, x) = CL.mkVar(lookup(env, x))
28 : jhr 1640 (* translate a variable that occurs in an r-value context *)
29 : jhr 1706 fun rvalueVar (env, x) = CL.mkVar(lookup(env, x))
30 : jhr 1640 (* translate a strand state variable that occurs in an l-value context *)
31 :     fun lvalueStateVar x = CL.mkIndirect(CL.mkVar "selfOut", IL.StateVar.name x)
32 :     (* translate a strand state variable that occurs in an r-value context *)
33 :     fun rvalueStateVar x = CL.mkIndirect(CL.mkVar "selfIn", IL.StateVar.name x)
34 :     end
35 :    
36 :     structure ToC = TreeToCFn (TrVar)
37 :    
38 :     type var = CL.typed_var
39 : jhr 1115 type exp = CL.exp
40 :     type stm = CL.stm
41 :    
42 :     datatype strand = Strand of {
43 : jhr 1375 name : string,
44 :     tyName : string,
45 : jhr 1640 state : var list,
46 :     output : (Ty.ty * CL.var), (* the strand's output variable (only one for now) *)
47 : jhr 1375 code : CL.decl list ref
48 : jhr 1115 }
49 :    
50 :     datatype program = Prog of {
51 : jhr 1716 tgt : target_desc, (* info about target *)
52 : jhr 1375 globals : CL.decl list ref,
53 :     topDecls : CL.decl list ref,
54 :     strands : strand AtomTable.hash_table,
55 : jhr 1713 nAxes : int option ref, (* number of axes in initial grid (NONE means collection) *)
56 : jhr 1375 initially : CL.decl ref
57 : jhr 1115 }
58 :    
59 :     datatype env = ENV of {
60 : jhr 1375 info : env_info,
61 :     vMap : var V.Map.map,
62 :     scope : scope
63 : jhr 1115 }
64 :    
65 :     and env_info = INFO of {
66 : jhr 1375 prog : program
67 : jhr 1115 }
68 :    
69 :     and scope
70 :     = NoScope
71 :     | GlobalScope
72 :     | InitiallyScope
73 : jhr 1640 | StrandScope (* strand initialization *)
74 :     | MethodScope of StrandUtil.method_name (* method body; vars are state variables *)
75 : jhr 1115
76 :     (* the supprted widths of vectors of reals on the target. For the GNU vector extensions,
77 :     * the supported sizes are powers of two, but float2 is broken.
78 :     * NOTE: we should also consider the AVX vector hardware, which has 256-bit registers.
79 :     *)
80 : jhr 1376 fun vectorWidths () = if !N.doublePrecision
81 : jhr 1375 then [2, 4, 8]
82 :     else [4, 8]
83 : jhr 1115
84 : jhr 1640 (* we support printing in the sequential C target *)
85 :     val supportsPrinting = true
86 :    
87 : jhr 1115 (* tests for whether various expression forms can appear inline *)
88 : jhr 1375 fun inlineCons n = (n < 2) (* vectors are inline, but not matrices *)
89 :     val inlineMatrixExp = false (* can matrix-valued expressions appear inline? *)
90 : jhr 1115
91 :     (* TreeIL to target translations *)
92 :     structure Tr =
93 :     struct
94 : jhr 1375 fun fragment (ENV{info, vMap, scope}, blk) = let
95 :     val (vMap, stms) = ToC.trFragment (vMap, blk)
96 :     in
97 :     (ENV{info=info, vMap=vMap, scope=scope}, stms)
98 :     end
99 : jhr 1640 (* NOTE: we may be able to simplify the interface to ToC.trBlock! *)
100 :     fun block (ENV{vMap, ...}, blk) = ToC.trBlock (vMap, blk)
101 : jhr 1375 fun exp (ENV{vMap, ...}, e) = ToC.trExp(vMap, e)
102 : jhr 1115 end
103 :    
104 :     (* variables *)
105 :     structure Var =
106 :     struct
107 : jhr 1640 fun name (CL.V(_, name)) = name
108 : jhr 1375 fun global (Prog{globals, ...}, name, ty) = let
109 :     val ty' = ToC.trType ty
110 :     in
111 :     globals := CL.D_Var([], ty', name, NONE) :: !globals;
112 : jhr 1640 CL.V(ty', name)
113 : jhr 1375 end
114 : jhr 1640 fun param x = CL.V(ToC.trType(V.ty x), V.name x)
115 : jhr 1115 end
116 :    
117 :     (* environments *)
118 :     structure Env =
119 :     struct
120 :     (* create a new environment *)
121 : jhr 1375 fun new prog = ENV{
122 :     info=INFO{prog = prog},
123 :     vMap = V.Map.empty,
124 :     scope = NoScope
125 :     }
126 : jhr 1115 (* define the current translation context *)
127 : jhr 1375 fun setScope scope (ENV{info, vMap, ...}) = ENV{info=info, vMap=vMap, scope=scope}
128 :     val scopeGlobal = setScope GlobalScope
129 :     val scopeInitially = setScope InitiallyScope
130 : jhr 1640 fun scopeStrand env = setScope StrandScope env
131 :     fun scopeMethod (env, name) = setScope (MethodScope name) env
132 : jhr 1115 (* bind a TreeIL varaiable to a target variable *)
133 : jhr 1375 fun bind (ENV{info, vMap, scope}, x, x') = ENV{
134 :     info = info,
135 :     vMap = V.Map.insert(vMap, x, x'),
136 :     scope = scope
137 :     }
138 : jhr 1115 end
139 :    
140 : jhr 1727 (* strands *)
141 :     structure Strand =
142 :     struct
143 :     fun define (Prog{strands, ...}, strandId, state) = let
144 :     val name = Atom.toString strandId
145 :     (* the output state variable *)
146 :     val outputVar = (case List.filter IL.StateVar.isOutput state
147 :     of [] => raise Fail("no output specified for strand " ^ name)
148 :     | [x] => (IL.StateVar.ty x, IL.StateVar.name x)
149 :     | _ => raise Fail("multiple outputs in " ^ name)
150 :     (* end case *))
151 :     (* the state variables *)
152 :     val state = let
153 :     fun cvt x = CL.V(ToC.trType(IL.StateVar.ty x), IL.StateVar.name x)
154 :     in
155 :     List.map cvt state
156 :     end
157 :     val strand = Strand{
158 :     name = name,
159 :     tyName = N.strandTy name,
160 :     state = state,
161 :     output = outputVar,
162 :     code = ref []
163 :     }
164 :     in
165 :     AtomTable.insert strands (strandId, strand);
166 :     strand
167 :     end
168 :    
169 :     (* return the strand with the given name *)
170 :     fun lookup (Prog{strands, ...}, strandId) = AtomTable.lookup strands strandId
171 :    
172 :     (* register the strand-state initialization code. The variables are the strand
173 :     * parameters.
174 :     *)
175 :     fun init (Strand{name, tyName, code, ...}, params, init) = let
176 :     val fName = N.strandInit name
177 :     val params =
178 :     CL.PARAM([], CL.T_Ptr(CL.T_Named tyName), "selfOut") ::
179 :     List.map (fn (CL.V(ty, x)) => CL.PARAM([], ty, x)) params
180 :     val initFn = CL.D_Func([], CL.voidTy, fName, params, init)
181 :     in
182 :     code := initFn :: !code
183 :     end
184 :    
185 :     (* register a strand method *)
186 :     fun method (Strand{name, tyName, code, ...}, methName, body) = let
187 :     val fName = concat[name, "_", StrandUtil.nameToString methName]
188 :     val params = [
189 :     CL.PARAM([], CL.T_Ptr(CL.T_Named tyName), "selfIn"),
190 :     CL.PARAM([], CL.T_Ptr(CL.T_Named tyName), "selfOut")
191 :     ]
192 :     val resTy = (case methName
193 :     of StrandUtil.Update => CL.T_Named "StrandStatus_t"
194 :     | StrandUtil.Stabilize => CL.voidTy
195 :     (* end case *))
196 :     val methFn = CL.D_Func(["static"], resTy, fName, params, body)
197 :     in
198 :     code := methFn :: !code
199 :     end
200 :    
201 :     end
202 :    
203 : jhr 1115 (* programs *)
204 :     structure Program =
205 :     struct
206 : jhr 1774 fun new (tgt : target_desc) = (
207 :     N.initTargetSpec {double= #double tgt, long=false};
208 :     Prog{
209 :     tgt = tgt,
210 :     globals = ref [],
211 :     topDecls = ref [],
212 :     strands = AtomTable.mkTable (16, Fail "strand table"),
213 :     nAxes = ref(SOME ~1),
214 :     initially = ref(CL.D_Comment["missing initially"])
215 :     })
216 : jhr 1727 (* FIXME: the inputFn was used for the standalone executable; we need to do something
217 :     * different for libraries.
218 :     *)
219 :     (* register the code that is used to set defaults for input variables *)
220 :     fun inputs (Prog{tgt, topDecls, ...}, stm) = let
221 :     val worldTy = CL.T_Ptr(CL.T_Named(N.worldTy tgt))
222 : jhr 1375 val inputsFn = CL.D_Func(
223 : jhr 1727 ["static"], CL.voidTy, N.initDefaults,
224 :     [CL.PARAM([], worldTy, "wrld")],
225 : jhr 1375 stm)
226 :     in
227 :     topDecls := inputsFn :: !topDecls
228 :     end
229 : jhr 1115 (* register the global initialization part of a program *)
230 : jhr 1727 fun init (Prog{tgt, topDecls, ...}, init) = let
231 :     val worldTy = CL.T_Ptr(CL.T_Named(N.worldTy tgt))
232 :     val initFn = CL.D_Func(
233 :     ["static"], CL.voidTy, N.initGlobals,
234 :     [CL.PARAM([], worldTy, "wrld")],
235 :     init)
236 : jhr 1375 in
237 : jhr 1718 topDecls := initFn :: !topDecls
238 : jhr 1375 end
239 : jhr 1115 (* create and register the initially function for a program *)
240 : jhr 1375 fun initially {
241 : jhr 1718 prog = Prog{tgt, strands, nAxes, initially, ...},
242 : jhr 1375 isArray : bool,
243 :     iterPrefix : stm list,
244 :     iters : (var * exp * exp) list,
245 :     createPrefix : stm list,
246 :     strand : Atom.atom,
247 :     args : exp list
248 :     } = let
249 :     val name = Atom.toString strand
250 :     val nDims = List.length iters
251 : jhr 1718 val worldTy = CL.T_Ptr(CL.T_Named(N.worldTy tgt))
252 : jhr 1375 fun mapi f xs = let
253 :     fun mapf (_, []) = []
254 :     | mapf (i, x::xs) = f(i, x) :: mapf(i+1, xs)
255 :     in
256 :     mapf (0, xs)
257 :     end
258 :     val baseInit = mapi (fn (i, (_, e, _)) => (i, CL.I_Exp e)) iters
259 :     val sizeInit = mapi
260 : jhr 1640 (fn (i, (CL.V(ty, _), lo, hi)) =>
261 : jhr 1375 (i, CL.I_Exp(CL.mkBinOp(CL.mkBinOp(hi, CL.#-, lo), CL.#+, CL.E_Int(1, ty))))
262 :     ) iters
263 :     (* code to allocate the world and initial strands *)
264 :     val allocCode = [
265 :     CL.mkComment["allocate initial block of strands"],
266 : jhr 1718 CL.mkDecl(CL.T_Array(CL.uint32, SOME nDims), "base", SOME(CL.I_Array baseInit)),
267 : jhr 1375 CL.mkDecl(CL.T_Array(CL.uint32, SOME nDims), "size", SOME(CL.I_Array sizeInit)),
268 : jhr 1718 CL.mkIfThen(CL.mkApply(N.allocInitially, [
269 :     CL.mkVar "wrld",
270 :     CL.E_Bool isArray,
271 :     CL.E_Int(IntInf.fromInt nDims, CL.int32),
272 :     CL.E_Var "base",
273 :     CL.E_Var "size"
274 :     ]),
275 :     (* then *)
276 :     CL.mkBlock [
277 :     (* FIXME: anything else? *)
278 :     CL.mkReturn(SOME(CL.mkVar "true"))
279 :     ])
280 :     (* endif *)
281 : jhr 1375 ]
282 :     (* create the loop nest for the initially iterations *)
283 :     val indexVar = "ix"
284 : jhr 1376 val strandTy = CL.T_Ptr(CL.T_Named(N.strandTy name))
285 : jhr 1759 fun statePtr inout = CL.mkSubscript(CL.mkIndirect(CL.mkVar "wrld", inout), CL.mkVar indexVar)
286 : jhr 1375 fun mkLoopNest [] = CL.mkBlock(createPrefix @ [
287 : jhr 1759 CL.mkCall(N.strandInit name, statePtr "inState" :: args),
288 :     CL.mkCall("memcpy", [
289 :     statePtr "outState", statePtr "inState",
290 :     CL.mkSizeof(CL.T_Named(N.strandTy name))
291 :     ]),
292 : jhr 1718 CL.S_Exp(CL.mkPostOp(CL.mkVar indexVar, CL.^++))
293 : jhr 1375 ])
294 : jhr 1640 | mkLoopNest ((CL.V(ty, param), lo, hi)::iters) = let
295 : jhr 1375 val body = mkLoopNest iters
296 :     in
297 :     CL.mkFor(
298 :     [(ty, param, lo)],
299 :     CL.mkBinOp(CL.E_Var param, CL.#<=, hi),
300 :     [CL.mkPostOp(CL.E_Var param, CL.^++)],
301 :     body)
302 :     end
303 :     val iterCode = [
304 :     CL.mkComment["initially"],
305 :     CL.mkDecl(CL.uint32, indexVar, SOME(CL.I_Exp(CL.E_Int(0, CL.uint32)))),
306 :     mkLoopNest iters
307 :     ]
308 :     val body = CL.mkBlock(
309 : jhr 1727 CL.mkCall (N.initGlobals, [CL.mkVar "wrld"]) ::
310 : jhr 1301 iterPrefix @
311 :     allocCode @
312 :     iterCode @
313 : jhr 1718 [CL.mkReturn(SOME(CL.mkVar "false"))])
314 :     val initFn = CL.D_Func([], CL.boolTy, N.initially tgt, [CL.PARAM([], worldTy, "wrld")], body)
315 : jhr 1375 in
316 : jhr 1713 nAxes := (if isArray then SOME nDims else NONE);
317 : jhr 1375 initially := initFn
318 :     end
319 : jhr 1115
320 :     (***** OUTPUT *****)
321 : jhr 1716
322 : jhr 1773 (* create the target-specific substitution list *)
323 :     fun mkSubs (tgt : target_desc, Strand{name, tyName, ...}) = [
324 :     ("CFILE", OS.Path.joinBaseExt{base= #outBase tgt, ext= SOME "c"}),
325 :     ("HDRFILE", OS.Path.joinBaseExt{base= #outBase tgt, ext= SOME "h"}),
326 :     ("PREFIX", #namespace tgt),
327 :     ("SRCFILE", #srcFile tgt),
328 :     ("STRAND", name),
329 :     ("STRANDTY", tyName)
330 :     ]
331 :    
332 :     fun condCons (true, x, xs) = x::xs
333 :     | condCons (false, _, xs) = xs
334 :    
335 :     fun compile (tgt : target_desc, basename) = let
336 :     (* generate the C compiler flags *)
337 :     val cflags = ["-I" ^ Paths.diderotInclude, "-I" ^ Paths.teemInclude]
338 :     val cflags = condCons (#parallel tgt, #pthread Paths.cflags, cflags)
339 :     val cflags = if #debug tgt
340 :     then #debug Paths.cflags :: cflags
341 :     else #ndebug Paths.cflags :: cflags
342 :     val cflags = #base Paths.cflags :: cflags
343 :     in
344 :     RunCC.compile (basename, cflags)
345 :     end
346 :    
347 :     fun ldFlags (tgt : target_desc) = if #exec tgt
348 :     then let
349 :     val extraLibs = condCons (#parallel tgt, #pthread Paths.extraLibs, [])
350 :     val extraLibs = Paths.teemLinkFlags @ #base Paths.extraLibs :: extraLibs
351 :     val rtLib = TargetUtil.runtimeName tgt
352 :     in
353 :     rtLib :: extraLibs
354 :     end
355 :     else [TargetUtil.runtimeName tgt]
356 :    
357 : jhr 1716 fun genStrand (tgt : target_desc, Strand{name, tyName, state, output, code}) = let
358 : jhr 1375 (* the type declaration for the strand's state struct *)
359 :     val selfTyDef = CL.D_StructDef(
360 : jhr 1716 SOME(concat[#namespace tgt, "struct_", name]),
361 : jhr 1640 List.rev (List.map (fn CL.V(ty, x) => (ty, x)) state),
362 : jhr 1716 NONE)
363 : jhr 1640 (* the type and access expression for the strand's output variable *)
364 :     val (outTy, outState) = (#1 output, CL.mkIndirect(CL.mkVar "self", #2 output))
365 : jhr 1375 (* the strand's descriptor object *)
366 :     val descI = let
367 :     fun fnPtr (ty, f) = CL.I_Exp(CL.mkCast(CL.T_Named ty, CL.mkVar f))
368 : jhr 1640 val nrrdTy = NrrdTypes.toNrrdType outTy
369 :     val nrrdSize = NrrdTypes.toNrrdSize outTy
370 :     in
371 :     CL.I_Struct[
372 :     ("name", CL.I_Exp(CL.mkStr name)),
373 :     ("stateSzb", CL.I_Exp(CL.mkSizeof(CL.T_Named(N.strandTy name)))),
374 :     ("update", fnPtr("update_method_t", name ^ "_Update")),
375 : jhr 1716 ("stabilize", fnPtr("stabilize_method_t", name ^ "_Stabilize"))
376 : jhr 1640 ]
377 :     end
378 : jhr 1376 val desc = CL.D_Var([], CL.T_Named N.strandDescTy, N.strandDesc name, SOME descI)
379 : jhr 1375 in
380 : jhr 1716 selfTyDef :: List.rev (desc :: !code)
381 : jhr 1375 end
382 : jhr 1115
383 : jhr 1716 (* FIXME: this function will also be used for other targets, so we should pull it into a
384 :     * utility module in c-util.
385 :     *)
386 :     (* generate the struct declaration for the world representation *)
387 : jhr 1773 fun genWorldStruct (isTypedef, tgt : target_desc, Strand{tyName, ...}) = let
388 :     val body = [
389 :     (* WORLD_STRUCT_PREFIX componenets (see Diderot/world.h) *)
390 :     (CL.T_Ptr(CL.T_Named "const char"), "name"),
391 :     (CL.charPtr, "err"),
392 :     (CL.boolTy, "verboseFlg"),
393 :     (CL.boolTy, "isArray"),
394 :     (CL.uint32, "nStrandTys"),
395 :     (CL.T_Ptr(CL.T_Named "Strand_t"), "strandDesc"),
396 :     (CL.uint32, "nAxes"),
397 :     (CL.T_Ptr CL.uint32, "base"),
398 :     (CL.T_Ptr CL.uint32, "size"),
399 :     (CL.uint32, "numStrands"),
400 :     (* target-specific world components *)
401 :     (CL.T_Ptr CL.uint8, "status"),
402 :     (CL.T_Ptr(CL.T_Ptr(CL.T_Named tyName)), "inState"),
403 :     (CL.T_Ptr(CL.T_Ptr(CL.T_Named tyName)), "outState")
404 :     ]
405 :     in
406 :     if isTypedef
407 :     then CL.D_StructDef(NONE, body, SOME(#namespace tgt ^ "World_t"))
408 :     else CL.D_StructDef(SOME(#namespace tgt ^ "struct_world"), body, NONE)
409 :     end
410 : jhr 1716
411 : jhr 1115 (* generate the table of strand descriptors *)
412 : jhr 1716 fun ppStrandTable (ppStrm, strands) = let
413 : jhr 1375 val nStrands = length strands
414 : jhr 1376 fun genInit (Strand{name, ...}) = CL.I_Exp(CL.mkUnOp(CL.%&, CL.E_Var(N.strandDesc name)))
415 : jhr 1375 fun genInits (_, []) = []
416 :     | genInits (i, s::ss) = (i, genInit s) :: genInits(i+1, ss)
417 :     fun ppDecl dcl = PrintAsC.output(ppStrm, dcl)
418 :     in
419 : jhr 1718 ppDecl (CL.D_Var(["static const"], CL.int32, "NumStrands",
420 : jhr 1375 SOME(CL.I_Exp(CL.E_Int(IntInf.fromInt nStrands, CL.int32)))));
421 :     ppDecl (CL.D_Var([],
422 : jhr 1376 CL.T_Array(CL.T_Ptr(CL.T_Named N.strandDescTy), SOME nStrands),
423 :     N.strands,
424 : jhr 1375 SOME(CL.I_Array(genInits (0, strands)))))
425 :     end
426 : jhr 1115
427 : jhr 1727 fun outputLibSrc (baseName, Prog{
428 :     tgt, globals, topDecls, strands, nAxes, initially, ...
429 :     }) = let
430 : jhr 1716 val [strand as Strand{name, tyName, state, output, ...}] = AtomTable.listItems strands
431 : jhr 1735 val outputs = GenOutput.gen (tgt, !nAxes) [output]
432 : jhr 1717 val substitutions = mkSubs (tgt, strand)
433 : jhr 1713 (* output to C file *)
434 :     val fileName = OS.Path.joinBaseExt{base=baseName, ext=SOME "c"}
435 :     val outS = TextIO.openOut fileName
436 :     val ppStrm = PrintAsC.new outS
437 :     fun ppDecl dcl = PrintAsC.output(ppStrm, dcl)
438 :     in
439 : jhr 1716 (* FIXME: use a fragment for this part of the file *)
440 : jhr 1717 ppDecl (CL.verbatim [CHeadFrag.text] substitutions);
441 : jhr 1773 ppDecl (genWorldStruct(false, tgt, strand));
442 : jhr 1715 List.app ppDecl (List.rev (!globals));
443 : jhr 1727 List.app ppDecl (List.rev (!topDecls));
444 : jhr 1716 List.app ppDecl (genStrand(tgt, strand));
445 : jhr 1714 List.app ppDecl outputs;
446 : jhr 1718 ppStrandTable (ppStrm, [strand]);
447 : jhr 1717 ppDecl (CL.verbatim [CBodyFrag.text] substitutions);
448 : jhr 1718 ppDecl (CL.verbatim [InitFrag.text] substitutions);
449 : jhr 1722 (* FIXME: the XXX_Init function needs to include code to initialize the globals with defaults *)
450 : jhr 1718 ppDecl (CL.verbatim [AllocFrag.text] substitutions);
451 :     ppDecl (!initially);
452 : jhr 1717 ppDecl (CL.verbatim [RunFrag.text] substitutions);
453 : jhr 1718 ppDecl (CL.verbatim [ShutdownFrag.text] substitutions);
454 : jhr 1714 PrintAsC.close ppStrm;
455 :     TextIO.closeOut outS
456 : jhr 1713 end
457 :    
458 : jhr 1706 fun generateLib (prog as Prog{tgt, strands, ...}) = let
459 :     val {outDir, outBase, exec, double, parallel, debug, ...} = tgt
460 :     val basename = OS.Path.joinDirFile{dir=outDir, file=outBase}
461 :     val [Strand{state, output, ...}] = AtomTable.listItems strands
462 :     in
463 : jhr 1713 (* generate the library .h file *)
464 : jhr 1706 GenLibraryInterface.gen {
465 :     tgt = tgt,
466 :     inputs = [], (* FIXME *)
467 :     outputs = [output]
468 : jhr 1713 };
469 :     (* *)
470 : jhr 1716 outputLibSrc (basename, prog);
471 : jhr 1714 (* compile and link *)
472 :     compile (tgt, basename);
473 :     RunCC.linkLib (basename, ldFlags tgt)
474 : jhr 1706 end
475 :    
476 : jhr 1773 fun genExecSrc (baseName, prog) = let
477 : jhr 1774 val Prog{tgt, globals, topDecls, strands, nAxes, initially, ...} = prog
478 : jhr 1773 val [strand as Strand{name, tyName, state, output, ...}] = AtomTable.listItems strands
479 : jhr 1774 val outputs = GenOutput.gen (tgt, !nAxes) [output]
480 : jhr 1773 val substitutions =
481 :     ("DIDEROT_FLOAT_PRECISION", TargetUtil.floatPrecisionDef tgt) ::
482 :     ("DIDEROT_INT_PRECISION", TargetUtil.intPrecisionDef tgt) ::
483 :     ("DIDEROT_TARGET", TargetUtil.targetDef tgt) ::
484 :     mkSubs (tgt, strand)
485 :     val fileName = OS.Path.joinBaseExt{base=baseName, ext=SOME "c"}
486 :     val outS = TextIO.openOut fileName
487 :     val ppStrm = PrintAsC.new outS
488 :     fun ppDecl dcl = PrintAsC.output(ppStrm, dcl)
489 :     val mainFrag = if (#parallel tgt)
490 :     then ParMainFrag.text
491 :     else SeqMainFrag.text
492 :     in
493 :     ppDecl (CL.verbatim [ExecHdr.text] substitutions);
494 :     ppDecl (genWorldStruct(true, tgt, strand));
495 :     List.app ppDecl (List.rev (!globals));
496 :     List.app ppDecl (List.rev (!topDecls));
497 : jhr 1774 ppDecl (GenInputs.genRegisterInputs (tgt, [(*FIXME*)]));
498 : jhr 1773 List.app ppDecl (genStrand(tgt, strand));
499 : jhr 1774 List.app ppDecl outputs;
500 : jhr 1773 ppStrandTable (ppStrm, [strand]);
501 :     ppDecl (CL.verbatim [InitFrag.text] substitutions);
502 :     (* FIXME: the XXX_Init function needs to include code to initialize the globals with defaults *)
503 :     ppDecl (CL.verbatim [AllocFrag.text] substitutions);
504 :     ppDecl (!initially);
505 :     ppDecl (CL.verbatim [RunFrag.text] substitutions);
506 :     ppDecl (CL.verbatim [ShutdownFrag.text] substitutions);
507 :     ppDecl (CL.verbatim [mainFrag] substitutions);
508 :     PrintAsC.close ppStrm;
509 :     TextIO.closeOut outS
510 :     end
511 :    
512 :     (* output the code to a file. The string is the basename of the file, the extension
513 :     * is provided by the target.
514 :     *)
515 :     fun generateExec (prog as Prog{tgt, ...}) = let
516 :     val {outDir, outBase, exec, double, parallel, debug, ...} = tgt
517 :     val basename = OS.Path.joinDirFile{dir=outDir, file=outBase}
518 :     in
519 :     genExecSrc (basename, prog);
520 :     compile (tgt, basename);
521 :     RunCC.linkExec (basename, ldFlags tgt)
522 :     end
523 :    
524 : jhr 1706 fun generate (prog as Prog{tgt, ...}) = if #exec tgt
525 :     then generateExec prog
526 :     else generateLib prog
527 :    
528 : jhr 1115 end
529 :    
530 :     end
531 :    
532 :     structure CBackEnd = CodeGenFn(CTarget)

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0