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

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