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

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