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 1727 - (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 1716 fun new (tgt : target_desc) = let
207 :     (*
208 : jhr 1705 val includes = if #exec tgt
209 :     then ["#include \"Diderot/diderot.h\"\n"]
210 :     else [
211 :     "#include \"",
212 :     OS.Path.joinBaseExt{base= #outBase tgt, ext= SOME "h"},
213 :     "\"\n"
214 :     ]
215 : jhr 1716 *)
216 : jhr 1705 in
217 :     N.initTargetSpec {double= #double tgt, long=false};
218 :     Prog{
219 :     tgt = tgt,
220 : jhr 1717 globals = ref [],
221 : jhr 1716 (*
222 : jhr 1705 globals = ref [ (* NOTE: in reverse order! *)
223 :     CL.D_Var(["static"], CL.charPtr, "ProgramName",
224 :     SOME(CL.I_Exp(CL.mkStr(#srcFile tgt)))),
225 :     CL.D_Verbatim([
226 : jhr 1706 concat["#define " ^ TargetUtil.floatPrecisionDef tgt, "\n"],
227 :     concat["#define " ^ TargetUtil.intPrecisionDef tgt, "\n"],
228 :     concat["#define " ^ TargetUtil.targetDef tgt, "\n"]
229 : jhr 1705 ] @ includes)
230 :     ],
231 : jhr 1716 *)
232 : jhr 1705 topDecls = ref [],
233 :     strands = AtomTable.mkTable (16, Fail "strand table"),
234 : jhr 1713 nAxes = ref(SOME ~1),
235 : jhr 1705 initially = ref(CL.D_Comment["missing initially"])
236 :     }
237 :     end
238 : jhr 1727 (* FIXME: the inputFn was used for the standalone executable; we need to do something
239 :     * different for libraries.
240 :     *)
241 :     (* register the code that is used to set defaults for input variables *)
242 :     fun inputs (Prog{tgt, topDecls, ...}, stm) = let
243 :     val worldTy = CL.T_Ptr(CL.T_Named(N.worldTy tgt))
244 : jhr 1375 val inputsFn = CL.D_Func(
245 : jhr 1727 ["static"], CL.voidTy, N.initDefaults,
246 :     [CL.PARAM([], worldTy, "wrld")],
247 : jhr 1375 stm)
248 :     in
249 :     topDecls := inputsFn :: !topDecls
250 :     end
251 : jhr 1115 (* register the global initialization part of a program *)
252 : jhr 1727 fun init (Prog{tgt, topDecls, ...}, init) = let
253 :     val worldTy = CL.T_Ptr(CL.T_Named(N.worldTy tgt))
254 :     val initFn = CL.D_Func(
255 :     ["static"], CL.voidTy, N.initGlobals,
256 :     [CL.PARAM([], worldTy, "wrld")],
257 :     init)
258 : jhr 1375 in
259 : jhr 1718 topDecls := initFn :: !topDecls
260 : jhr 1375 end
261 : jhr 1115 (* create and register the initially function for a program *)
262 : jhr 1375 fun initially {
263 : jhr 1718 prog = Prog{tgt, strands, nAxes, initially, ...},
264 : jhr 1375 isArray : bool,
265 :     iterPrefix : stm list,
266 :     iters : (var * exp * exp) list,
267 :     createPrefix : stm list,
268 :     strand : Atom.atom,
269 :     args : exp list
270 :     } = let
271 :     val name = Atom.toString strand
272 :     val nDims = List.length iters
273 : jhr 1718 val worldTy = CL.T_Ptr(CL.T_Named(N.worldTy tgt))
274 : jhr 1375 fun mapi f xs = let
275 :     fun mapf (_, []) = []
276 :     | mapf (i, x::xs) = f(i, x) :: mapf(i+1, xs)
277 :     in
278 :     mapf (0, xs)
279 :     end
280 :     val baseInit = mapi (fn (i, (_, e, _)) => (i, CL.I_Exp e)) iters
281 :     val sizeInit = mapi
282 : jhr 1640 (fn (i, (CL.V(ty, _), lo, hi)) =>
283 : jhr 1375 (i, CL.I_Exp(CL.mkBinOp(CL.mkBinOp(hi, CL.#-, lo), CL.#+, CL.E_Int(1, ty))))
284 :     ) iters
285 :     (* code to allocate the world and initial strands *)
286 :     val allocCode = [
287 :     CL.mkComment["allocate initial block of strands"],
288 : jhr 1718 CL.mkDecl(CL.T_Array(CL.uint32, SOME nDims), "base", SOME(CL.I_Array baseInit)),
289 : jhr 1375 CL.mkDecl(CL.T_Array(CL.uint32, SOME nDims), "size", SOME(CL.I_Array sizeInit)),
290 : jhr 1718 CL.mkIfThen(CL.mkApply(N.allocInitially, [
291 :     CL.mkVar "wrld",
292 :     CL.E_Bool isArray,
293 :     CL.E_Int(IntInf.fromInt nDims, CL.int32),
294 :     CL.E_Var "base",
295 :     CL.E_Var "size"
296 :     ]),
297 :     (* then *)
298 :     CL.mkBlock [
299 :     (* FIXME: anything else? *)
300 :     CL.mkReturn(SOME(CL.mkVar "true"))
301 :     ])
302 :     (* endif *)
303 : jhr 1375 ]
304 :     (* create the loop nest for the initially iterations *)
305 :     val indexVar = "ix"
306 : jhr 1376 val strandTy = CL.T_Ptr(CL.T_Named(N.strandTy name))
307 : jhr 1375 fun mkLoopNest [] = CL.mkBlock(createPrefix @ [
308 : jhr 1718 CL.mkCall(N.strandInit name,
309 :     CL.mkSubscript(CL.mkIndirect(CL.mkVar "wrld", "inState"), CL.mkVar indexVar) :: args),
310 :     CL.S_Exp(CL.mkPostOp(CL.mkVar indexVar, CL.^++))
311 : jhr 1375 ])
312 : jhr 1640 | mkLoopNest ((CL.V(ty, param), lo, hi)::iters) = let
313 : jhr 1375 val body = mkLoopNest iters
314 :     in
315 :     CL.mkFor(
316 :     [(ty, param, lo)],
317 :     CL.mkBinOp(CL.E_Var param, CL.#<=, hi),
318 :     [CL.mkPostOp(CL.E_Var param, CL.^++)],
319 :     body)
320 :     end
321 :     val iterCode = [
322 :     CL.mkComment["initially"],
323 :     CL.mkDecl(CL.uint32, indexVar, SOME(CL.I_Exp(CL.E_Int(0, CL.uint32)))),
324 :     mkLoopNest iters
325 :     ]
326 :     val body = CL.mkBlock(
327 : jhr 1727 CL.mkCall (N.initGlobals, [CL.mkVar "wrld"]) ::
328 : jhr 1301 iterPrefix @
329 :     allocCode @
330 :     iterCode @
331 : jhr 1718 [CL.mkReturn(SOME(CL.mkVar "false"))])
332 :     val initFn = CL.D_Func([], CL.boolTy, N.initially tgt, [CL.PARAM([], worldTy, "wrld")], body)
333 : jhr 1375 in
334 : jhr 1713 nAxes := (if isArray then SOME nDims else NONE);
335 : jhr 1375 initially := initFn
336 :     end
337 : jhr 1115
338 :     (***** OUTPUT *****)
339 : jhr 1716
340 :     fun genStrand (tgt : target_desc, Strand{name, tyName, state, output, code}) = let
341 : jhr 1375 (* the type declaration for the strand's state struct *)
342 :     val selfTyDef = CL.D_StructDef(
343 : jhr 1716 SOME(concat[#namespace tgt, "struct_", name]),
344 : jhr 1640 List.rev (List.map (fn CL.V(ty, x) => (ty, x)) state),
345 : jhr 1716 NONE)
346 : jhr 1640 (* the type and access expression for the strand's output variable *)
347 :     val (outTy, outState) = (#1 output, CL.mkIndirect(CL.mkVar "self", #2 output))
348 : jhr 1375 (* the strand's descriptor object *)
349 :     val descI = let
350 :     fun fnPtr (ty, f) = CL.I_Exp(CL.mkCast(CL.T_Named ty, CL.mkVar f))
351 : jhr 1640 val nrrdTy = NrrdTypes.toNrrdType outTy
352 :     val nrrdSize = NrrdTypes.toNrrdSize outTy
353 :     in
354 :     CL.I_Struct[
355 :     ("name", CL.I_Exp(CL.mkStr name)),
356 :     ("stateSzb", CL.I_Exp(CL.mkSizeof(CL.T_Named(N.strandTy name)))),
357 :     ("update", fnPtr("update_method_t", name ^ "_Update")),
358 : jhr 1716 ("stabilize", fnPtr("stabilize_method_t", name ^ "_Stabilize"))
359 : jhr 1640 ]
360 :     end
361 : jhr 1376 val desc = CL.D_Var([], CL.T_Named N.strandDescTy, N.strandDesc name, SOME descI)
362 : jhr 1375 in
363 : jhr 1716 selfTyDef :: List.rev (desc :: !code)
364 : jhr 1375 end
365 : jhr 1115
366 : jhr 1716 (* FIXME: this function will also be used for other targets, so we should pull it into a
367 :     * utility module in c-util.
368 :     *)
369 :     (* generate the struct declaration for the world representation *)
370 :     fun genWorldStruct (tgt : target_desc, Strand{tyName, ...}) = CL.D_StructDef(
371 :     SOME(#namespace tgt ^ "struct_world"),
372 :     [
373 : jhr 1718 (* WORLD_STRUCT_PREFIX componenets (see Diderot/world.h) *)
374 : jhr 1716 (CL.T_Ptr(CL.T_Named "const char"), "name"),
375 :     (CL.charPtr, "err"),
376 : jhr 1717 (CL.boolTy, "verboseFlg"),
377 : jhr 1716 (CL.boolTy, "isArray"),
378 : jhr 1717 (CL.uint32, "nStrandTys"),
379 : jhr 1716 (CL.T_Ptr(CL.T_Named "Strand_t"), "strandDesc"),
380 :     (CL.uint32, "nAxes"),
381 :     (CL.T_Ptr CL.uint32, "base"),
382 :     (CL.T_Ptr CL.uint32, "size"),
383 :     (CL.uint32, "numStrands"),
384 : jhr 1718 (* target-specific world components *)
385 : jhr 1716 (CL.T_Ptr CL.uint8, "status"),
386 :     (CL.T_Ptr(CL.T_Ptr(CL.T_Named tyName)), "inState"),
387 :     (CL.T_Ptr(CL.T_Ptr(CL.T_Named tyName)), "outState")
388 :     ],
389 :     NONE)
390 :    
391 : jhr 1115 (* generate the table of strand descriptors *)
392 : jhr 1716 fun ppStrandTable (ppStrm, strands) = let
393 : jhr 1375 val nStrands = length strands
394 : jhr 1376 fun genInit (Strand{name, ...}) = CL.I_Exp(CL.mkUnOp(CL.%&, CL.E_Var(N.strandDesc name)))
395 : jhr 1375 fun genInits (_, []) = []
396 :     | genInits (i, s::ss) = (i, genInit s) :: genInits(i+1, ss)
397 :     fun ppDecl dcl = PrintAsC.output(ppStrm, dcl)
398 :     in
399 : jhr 1718 ppDecl (CL.D_Var(["static const"], CL.int32, "NumStrands",
400 : jhr 1375 SOME(CL.I_Exp(CL.E_Int(IntInf.fromInt nStrands, CL.int32)))));
401 :     ppDecl (CL.D_Var([],
402 : jhr 1376 CL.T_Array(CL.T_Ptr(CL.T_Named N.strandDescTy), SOME nStrands),
403 :     N.strands,
404 : jhr 1375 SOME(CL.I_Array(genInits (0, strands)))))
405 :     end
406 : jhr 1115
407 : jhr 1713 fun genExecSrc (baseName, prog) = let
408 : jhr 1716 val Prog{tgt, globals, topDecls, strands, initially, ...} = prog
409 : jhr 1375 val fileName = OS.Path.joinBaseExt{base=baseName, ext=SOME "c"}
410 :     val outS = TextIO.openOut fileName
411 :     val ppStrm = PrintAsC.new outS
412 :     fun ppDecl dcl = PrintAsC.output(ppStrm, dcl)
413 :     val strands = AtomTable.listItems strands
414 :     in
415 :     List.app ppDecl (List.rev (!globals));
416 :     List.app ppDecl (List.rev (!topDecls));
417 : jhr 1716 List.app (fn strand => List.app ppDecl (genStrand(tgt, strand))) strands;
418 :     ppStrandTable (ppStrm, strands);
419 : jhr 1375 ppDecl (!initially);
420 :     PrintAsC.close ppStrm;
421 :     TextIO.closeOut outS
422 :     end
423 : jhr 1115
424 : jhr 1714 fun condCons (true, x, xs) = x::xs
425 :     | condCons (false, _, xs) = xs
426 :    
427 : jhr 1716 fun compile (tgt : target_desc, basename) = let
428 : jhr 1375 (* generate the C compiler flags *)
429 :     val cflags = ["-I" ^ Paths.diderotInclude, "-I" ^ Paths.teemInclude]
430 : jhr 1714 val cflags = condCons (#parallel tgt, #pthread Paths.cflags, cflags)
431 :     val cflags = if #debug tgt
432 : jhr 1375 then #debug Paths.cflags :: cflags
433 :     else #ndebug Paths.cflags :: cflags
434 :     val cflags = #base Paths.cflags :: cflags
435 : jhr 1714 in
436 :     RunCC.compile (basename, cflags)
437 :     end
438 :    
439 : jhr 1717 fun ldFlags (tgt : target_desc) = if #exec tgt
440 :     then let
441 :     val extraLibs = condCons (#parallel tgt, #pthread Paths.extraLibs, [])
442 :     val extraLibs = Paths.teemLinkFlags @ #base Paths.extraLibs :: extraLibs
443 :     val rtLib = TargetUtil.runtimeName tgt
444 :     in
445 :     rtLib :: extraLibs
446 :     end
447 :     else [TargetUtil.runtimeName tgt]
448 : jhr 1714
449 :     (* output the code to a file. The string is the basename of the file, the extension
450 :     * is provided by the target.
451 :     *)
452 :     fun generateExec (prog as Prog{tgt, ...}) = let
453 :     val {outDir, outBase, exec, double, parallel, debug, ...} = tgt
454 : jhr 1705 val basename = OS.Path.joinDirFile{dir=outDir, file=outBase}
455 : jhr 1375 in
456 : jhr 1713 genExecSrc (basename, prog);
457 : jhr 1714 compile (tgt, basename);
458 :     RunCC.linkExec (basename, ldFlags tgt)
459 : jhr 1375 end
460 : jhr 1115
461 : jhr 1717 fun mkSubs (tgt : target_desc, Strand{name, tyName, ...}) = [
462 :     ("CFILE", OS.Path.joinBaseExt{base= #outBase tgt, ext= SOME "c"}),
463 :     ("HDRFILE", OS.Path.joinBaseExt{base= #outBase tgt, ext= SOME "h"}),
464 :     ("PREFIX", #namespace tgt),
465 :     ("SRCFILE", #srcFile tgt),
466 :     ("STRAND", name),
467 :     ("STRANDTY", tyName)
468 :     ]
469 :    
470 : jhr 1727 fun outputLibSrc (baseName, Prog{
471 :     tgt, globals, topDecls, strands, nAxes, initially, ...
472 :     }) = let
473 : jhr 1716 val [strand as Strand{name, tyName, state, output, ...}] = AtomTable.listItems strands
474 : jhr 1713 val outputs = List.map (GenOutput.gen (tgt, !nAxes)) [output]
475 : jhr 1717 val substitutions = mkSubs (tgt, strand)
476 : jhr 1713 (* output to C file *)
477 :     val fileName = OS.Path.joinBaseExt{base=baseName, ext=SOME "c"}
478 :     val outS = TextIO.openOut fileName
479 :     val ppStrm = PrintAsC.new outS
480 :     fun ppDecl dcl = PrintAsC.output(ppStrm, dcl)
481 :     in
482 : jhr 1716 (* FIXME: use a fragment for this part of the file *)
483 : jhr 1717 ppDecl (CL.verbatim [CHeadFrag.text] substitutions);
484 : jhr 1716 ppDecl (genWorldStruct(tgt, strand));
485 : jhr 1715 List.app ppDecl (List.rev (!globals));
486 : jhr 1727 List.app ppDecl (List.rev (!topDecls));
487 : jhr 1716 List.app ppDecl (genStrand(tgt, strand));
488 : jhr 1714 List.app ppDecl outputs;
489 : jhr 1718 ppStrandTable (ppStrm, [strand]);
490 : jhr 1717 ppDecl (CL.verbatim [CBodyFrag.text] substitutions);
491 : jhr 1718 ppDecl (CL.verbatim [InitFrag.text] substitutions);
492 : jhr 1722 (* FIXME: the XXX_Init function needs to include code to initialize the globals with defaults *)
493 : jhr 1718 ppDecl (CL.verbatim [AllocFrag.text] substitutions);
494 :     ppDecl (!initially);
495 : jhr 1717 ppDecl (CL.verbatim [RunFrag.text] substitutions);
496 : jhr 1718 ppDecl (CL.verbatim [ShutdownFrag.text] substitutions);
497 : jhr 1714 PrintAsC.close ppStrm;
498 :     TextIO.closeOut outS
499 : jhr 1713 end
500 :    
501 : jhr 1706 fun generateLib (prog as Prog{tgt, strands, ...}) = let
502 :     val {outDir, outBase, exec, double, parallel, debug, ...} = tgt
503 :     val basename = OS.Path.joinDirFile{dir=outDir, file=outBase}
504 :     val [Strand{state, output, ...}] = AtomTable.listItems strands
505 :     in
506 : jhr 1713 (* generate the library .h file *)
507 : jhr 1706 GenLibraryInterface.gen {
508 :     tgt = tgt,
509 :     inputs = [], (* FIXME *)
510 :     outputs = [output]
511 : jhr 1713 };
512 :     (* *)
513 : jhr 1716 outputLibSrc (basename, prog);
514 : jhr 1714 (* compile and link *)
515 :     compile (tgt, basename);
516 :     RunCC.linkLib (basename, ldFlags tgt)
517 : jhr 1706 end
518 :    
519 :     fun generate (prog as Prog{tgt, ...}) = if #exec tgt
520 :     then generateExec prog
521 :     else generateLib prog
522 :    
523 : jhr 1115 end
524 :    
525 :     end
526 :    
527 :     structure CBackEnd = CodeGenFn(CTarget)

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