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 1722 - (view) (download)

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

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