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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

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