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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

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