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 1773 - (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 1759 fun statePtr inout = CL.mkSubscript(CL.mkIndirect(CL.mkVar "wrld", inout), CL.mkVar indexVar)
308 : jhr 1375 fun mkLoopNest [] = CL.mkBlock(createPrefix @ [
309 : jhr 1759 CL.mkCall(N.strandInit name, statePtr "inState" :: args),
310 :     CL.mkCall("memcpy", [
311 :     statePtr "outState", statePtr "inState",
312 :     CL.mkSizeof(CL.T_Named(N.strandTy name))
313 :     ]),
314 : jhr 1718 CL.S_Exp(CL.mkPostOp(CL.mkVar indexVar, CL.^++))
315 : jhr 1375 ])
316 : jhr 1640 | mkLoopNest ((CL.V(ty, param), lo, hi)::iters) = let
317 : jhr 1375 val body = mkLoopNest iters
318 :     in
319 :     CL.mkFor(
320 :     [(ty, param, lo)],
321 :     CL.mkBinOp(CL.E_Var param, CL.#<=, hi),
322 :     [CL.mkPostOp(CL.E_Var param, CL.^++)],
323 :     body)
324 :     end
325 :     val iterCode = [
326 :     CL.mkComment["initially"],
327 :     CL.mkDecl(CL.uint32, indexVar, SOME(CL.I_Exp(CL.E_Int(0, CL.uint32)))),
328 :     mkLoopNest iters
329 :     ]
330 :     val body = CL.mkBlock(
331 : jhr 1727 CL.mkCall (N.initGlobals, [CL.mkVar "wrld"]) ::
332 : jhr 1301 iterPrefix @
333 :     allocCode @
334 :     iterCode @
335 : jhr 1718 [CL.mkReturn(SOME(CL.mkVar "false"))])
336 :     val initFn = CL.D_Func([], CL.boolTy, N.initially tgt, [CL.PARAM([], worldTy, "wrld")], body)
337 : jhr 1375 in
338 : jhr 1713 nAxes := (if isArray then SOME nDims else NONE);
339 : jhr 1375 initially := initFn
340 :     end
341 : jhr 1115
342 :     (***** OUTPUT *****)
343 : jhr 1716
344 : jhr 1773 (* create the target-specific substitution list *)
345 :     fun mkSubs (tgt : target_desc, Strand{name, tyName, ...}) = [
346 :     ("CFILE", OS.Path.joinBaseExt{base= #outBase tgt, ext= SOME "c"}),
347 :     ("HDRFILE", OS.Path.joinBaseExt{base= #outBase tgt, ext= SOME "h"}),
348 :     ("PREFIX", #namespace tgt),
349 :     ("SRCFILE", #srcFile tgt),
350 :     ("STRAND", name),
351 :     ("STRANDTY", tyName)
352 :     ]
353 :    
354 :     fun condCons (true, x, xs) = x::xs
355 :     | condCons (false, _, xs) = xs
356 :    
357 :     fun compile (tgt : target_desc, basename) = let
358 :     (* generate the C compiler flags *)
359 :     val cflags = ["-I" ^ Paths.diderotInclude, "-I" ^ Paths.teemInclude]
360 :     val cflags = condCons (#parallel tgt, #pthread Paths.cflags, cflags)
361 :     val cflags = if #debug tgt
362 :     then #debug Paths.cflags :: cflags
363 :     else #ndebug Paths.cflags :: cflags
364 :     val cflags = #base Paths.cflags :: cflags
365 :     in
366 :     RunCC.compile (basename, cflags)
367 :     end
368 :    
369 :     fun ldFlags (tgt : target_desc) = if #exec tgt
370 :     then let
371 :     val extraLibs = condCons (#parallel tgt, #pthread Paths.extraLibs, [])
372 :     val extraLibs = Paths.teemLinkFlags @ #base Paths.extraLibs :: extraLibs
373 :     val rtLib = TargetUtil.runtimeName tgt
374 :     in
375 :     rtLib :: extraLibs
376 :     end
377 :     else [TargetUtil.runtimeName tgt]
378 :    
379 : jhr 1716 fun genStrand (tgt : target_desc, Strand{name, tyName, state, output, code}) = let
380 : jhr 1375 (* the type declaration for the strand's state struct *)
381 :     val selfTyDef = CL.D_StructDef(
382 : jhr 1716 SOME(concat[#namespace tgt, "struct_", name]),
383 : jhr 1640 List.rev (List.map (fn CL.V(ty, x) => (ty, x)) state),
384 : jhr 1716 NONE)
385 : jhr 1640 (* the type and access expression for the strand's output variable *)
386 :     val (outTy, outState) = (#1 output, CL.mkIndirect(CL.mkVar "self", #2 output))
387 : jhr 1375 (* the strand's descriptor object *)
388 :     val descI = let
389 :     fun fnPtr (ty, f) = CL.I_Exp(CL.mkCast(CL.T_Named ty, CL.mkVar f))
390 : jhr 1640 val nrrdTy = NrrdTypes.toNrrdType outTy
391 :     val nrrdSize = NrrdTypes.toNrrdSize outTy
392 :     in
393 :     CL.I_Struct[
394 :     ("name", CL.I_Exp(CL.mkStr name)),
395 :     ("stateSzb", CL.I_Exp(CL.mkSizeof(CL.T_Named(N.strandTy name)))),
396 :     ("update", fnPtr("update_method_t", name ^ "_Update")),
397 : jhr 1716 ("stabilize", fnPtr("stabilize_method_t", name ^ "_Stabilize"))
398 : jhr 1640 ]
399 :     end
400 : jhr 1376 val desc = CL.D_Var([], CL.T_Named N.strandDescTy, N.strandDesc name, SOME descI)
401 : jhr 1375 in
402 : jhr 1716 selfTyDef :: List.rev (desc :: !code)
403 : jhr 1375 end
404 : jhr 1115
405 : jhr 1716 (* FIXME: this function will also be used for other targets, so we should pull it into a
406 :     * utility module in c-util.
407 :     *)
408 :     (* generate the struct declaration for the world representation *)
409 : jhr 1773 fun genWorldStruct (isTypedef, tgt : target_desc, Strand{tyName, ...}) = let
410 :     val body = [
411 :     (* WORLD_STRUCT_PREFIX componenets (see Diderot/world.h) *)
412 :     (CL.T_Ptr(CL.T_Named "const char"), "name"),
413 :     (CL.charPtr, "err"),
414 :     (CL.boolTy, "verboseFlg"),
415 :     (CL.boolTy, "isArray"),
416 :     (CL.uint32, "nStrandTys"),
417 :     (CL.T_Ptr(CL.T_Named "Strand_t"), "strandDesc"),
418 :     (CL.uint32, "nAxes"),
419 :     (CL.T_Ptr CL.uint32, "base"),
420 :     (CL.T_Ptr CL.uint32, "size"),
421 :     (CL.uint32, "numStrands"),
422 :     (* target-specific world components *)
423 :     (CL.T_Ptr CL.uint8, "status"),
424 :     (CL.T_Ptr(CL.T_Ptr(CL.T_Named tyName)), "inState"),
425 :     (CL.T_Ptr(CL.T_Ptr(CL.T_Named tyName)), "outState")
426 :     ]
427 :     in
428 :     if isTypedef
429 :     then CL.D_StructDef(NONE, body, SOME(#namespace tgt ^ "World_t"))
430 :     else CL.D_StructDef(SOME(#namespace tgt ^ "struct_world"), body, NONE)
431 :     end
432 : jhr 1716
433 : jhr 1115 (* generate the table of strand descriptors *)
434 : jhr 1716 fun ppStrandTable (ppStrm, strands) = let
435 : jhr 1375 val nStrands = length strands
436 : jhr 1376 fun genInit (Strand{name, ...}) = CL.I_Exp(CL.mkUnOp(CL.%&, CL.E_Var(N.strandDesc name)))
437 : jhr 1375 fun genInits (_, []) = []
438 :     | genInits (i, s::ss) = (i, genInit s) :: genInits(i+1, ss)
439 :     fun ppDecl dcl = PrintAsC.output(ppStrm, dcl)
440 :     in
441 : jhr 1718 ppDecl (CL.D_Var(["static const"], CL.int32, "NumStrands",
442 : jhr 1375 SOME(CL.I_Exp(CL.E_Int(IntInf.fromInt nStrands, CL.int32)))));
443 :     ppDecl (CL.D_Var([],
444 : jhr 1376 CL.T_Array(CL.T_Ptr(CL.T_Named N.strandDescTy), SOME nStrands),
445 :     N.strands,
446 : jhr 1375 SOME(CL.I_Array(genInits (0, strands)))))
447 :     end
448 : jhr 1115
449 : jhr 1727 fun outputLibSrc (baseName, Prog{
450 :     tgt, globals, topDecls, strands, nAxes, initially, ...
451 :     }) = let
452 : jhr 1716 val [strand as Strand{name, tyName, state, output, ...}] = AtomTable.listItems strands
453 : jhr 1735 val outputs = GenOutput.gen (tgt, !nAxes) [output]
454 : jhr 1717 val substitutions = mkSubs (tgt, strand)
455 : jhr 1713 (* output to C file *)
456 :     val fileName = OS.Path.joinBaseExt{base=baseName, ext=SOME "c"}
457 :     val outS = TextIO.openOut fileName
458 :     val ppStrm = PrintAsC.new outS
459 :     fun ppDecl dcl = PrintAsC.output(ppStrm, dcl)
460 :     in
461 : jhr 1716 (* FIXME: use a fragment for this part of the file *)
462 : jhr 1717 ppDecl (CL.verbatim [CHeadFrag.text] substitutions);
463 : jhr 1773 ppDecl (genWorldStruct(false, tgt, strand));
464 : jhr 1715 List.app ppDecl (List.rev (!globals));
465 : jhr 1727 List.app ppDecl (List.rev (!topDecls));
466 : jhr 1716 List.app ppDecl (genStrand(tgt, strand));
467 : jhr 1714 List.app ppDecl outputs;
468 : jhr 1718 ppStrandTable (ppStrm, [strand]);
469 : jhr 1717 ppDecl (CL.verbatim [CBodyFrag.text] substitutions);
470 : jhr 1718 ppDecl (CL.verbatim [InitFrag.text] substitutions);
471 : jhr 1722 (* FIXME: the XXX_Init function needs to include code to initialize the globals with defaults *)
472 : jhr 1718 ppDecl (CL.verbatim [AllocFrag.text] substitutions);
473 :     ppDecl (!initially);
474 : jhr 1717 ppDecl (CL.verbatim [RunFrag.text] substitutions);
475 : jhr 1718 ppDecl (CL.verbatim [ShutdownFrag.text] substitutions);
476 : jhr 1714 PrintAsC.close ppStrm;
477 :     TextIO.closeOut outS
478 : jhr 1713 end
479 :    
480 : jhr 1706 fun generateLib (prog as Prog{tgt, strands, ...}) = let
481 :     val {outDir, outBase, exec, double, parallel, debug, ...} = tgt
482 :     val basename = OS.Path.joinDirFile{dir=outDir, file=outBase}
483 :     val [Strand{state, output, ...}] = AtomTable.listItems strands
484 :     in
485 : jhr 1713 (* generate the library .h file *)
486 : jhr 1706 GenLibraryInterface.gen {
487 :     tgt = tgt,
488 :     inputs = [], (* FIXME *)
489 :     outputs = [output]
490 : jhr 1713 };
491 :     (* *)
492 : jhr 1716 outputLibSrc (basename, prog);
493 : jhr 1714 (* compile and link *)
494 :     compile (tgt, basename);
495 :     RunCC.linkLib (basename, ldFlags tgt)
496 : jhr 1706 end
497 :    
498 : jhr 1773 fun genExecSrc (baseName, prog) = let
499 :     val Prog{tgt, globals, topDecls, strands, initially, ...} = prog
500 :     val [strand as Strand{name, tyName, state, output, ...}] = AtomTable.listItems strands
501 :     val substitutions =
502 :     ("DIDEROT_FLOAT_PRECISION", TargetUtil.floatPrecisionDef tgt) ::
503 :     ("DIDEROT_INT_PRECISION", TargetUtil.intPrecisionDef tgt) ::
504 :     ("DIDEROT_TARGET", TargetUtil.targetDef tgt) ::
505 :     mkSubs (tgt, strand)
506 :     val fileName = OS.Path.joinBaseExt{base=baseName, ext=SOME "c"}
507 :     val outS = TextIO.openOut fileName
508 :     val ppStrm = PrintAsC.new outS
509 :     fun ppDecl dcl = PrintAsC.output(ppStrm, dcl)
510 :     val mainFrag = if (#parallel tgt)
511 :     then ParMainFrag.text
512 :     else SeqMainFrag.text
513 :     in
514 :     ppDecl (CL.verbatim [ExecHdr.text] substitutions);
515 :     ppDecl (genWorldStruct(true, tgt, strand));
516 :     List.app ppDecl (List.rev (!globals));
517 :     List.app ppDecl (List.rev (!topDecls));
518 :     List.app ppDecl (genStrand(tgt, strand));
519 :     (* outputs *)
520 :     ppStrandTable (ppStrm, [strand]);
521 :     ppDecl (CL.verbatim [InitFrag.text] substitutions);
522 :     (* FIXME: the XXX_Init function needs to include code to initialize the globals with defaults *)
523 :     ppDecl (CL.verbatim [AllocFrag.text] substitutions);
524 :     ppDecl (!initially);
525 :     ppDecl (CL.verbatim [RunFrag.text] substitutions);
526 :     ppDecl (CL.verbatim [ShutdownFrag.text] substitutions);
527 :     ppDecl (CL.verbatim [mainFrag] substitutions);
528 :     PrintAsC.close ppStrm;
529 :     TextIO.closeOut outS
530 :     end
531 :    
532 :     (* output the code to a file. The string is the basename of the file, the extension
533 :     * is provided by the target.
534 :     *)
535 :     fun generateExec (prog as Prog{tgt, ...}) = let
536 :     val {outDir, outBase, exec, double, parallel, debug, ...} = tgt
537 :     val basename = OS.Path.joinDirFile{dir=outDir, file=outBase}
538 :     in
539 :     genExecSrc (basename, prog);
540 :     compile (tgt, basename);
541 :     RunCC.linkExec (basename, ldFlags tgt)
542 :     end
543 :    
544 : jhr 1706 fun generate (prog as Prog{tgt, ...}) = if #exec tgt
545 :     then generateExec prog
546 :     else generateLib prog
547 :    
548 : jhr 1115 end
549 :    
550 :     end
551 :    
552 :     structure CBackEnd = CodeGenFn(CTarget)

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