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

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