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

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