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

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