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

SCM Repository

[diderot] Annotation of /branches/vis12/src/compiler/translate/translate.sml
ViewVC logotype

Annotation of /branches/vis12/src/compiler/translate/translate.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2026 - (view) (download)

1 : jhr 137 (* translate.sml
2 :     *
3 : jhr 435 * COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu)
4 : jhr 137 * All rights reserved.
5 :     *
6 : jhr 1116 * Translate Simple-AST code into the IL representation. This translation is based on the
7 :     * algorithm described in
8 :     *
9 :     * Single-pass generation of static single assignment form for structured languages
10 :     * ACM TOPLAS, Nov. 1994
11 :     * by Brandis and MossenBock.
12 : jhr 137 *)
13 :    
14 :     structure Translate : sig
15 :    
16 : jhr 176 val translate : Simple.program -> HighIL.program
17 : jhr 137
18 :     end = struct
19 :    
20 : jhr 176 structure S = Simple
21 : jhr 394 structure Ty = Types
22 : jhr 137 structure VMap = Var.Map
23 : jhr 189 structure VSet = Var.Set
24 : jhr 168 structure IL = HighIL
25 : jhr 1640 structure Op = HighOps
26 : jhr 394 structure DstTy = HighILTypes
27 : jhr 1116 structure Census = HighILCensus
28 : jhr 137
29 : jhr 1640 val cvtTy = TranslateTy.tr
30 :    
31 : jhr 1116 (* maps from SimpleAST variables to the current corresponding SSA variable *)
32 : jhr 511 type env = IL.var VMap.map
33 :    
34 : jhr 1640 (* +DEBUG *)
35 :     fun prEnv (prefix, env) = let
36 :     val wid = ref 0
37 :     fun pr s = (print s; wid := !wid + size s)
38 :     fun nl () = if (!wid > 0) then (print "\n"; wid := 0) else ()
39 :     fun prElem (src, dst) = let
40 :     val s = String.concat [
41 :     " ", Var.uniqueNameOf src, "->", IL.Var.toString dst
42 :     ]
43 :     in
44 :     pr s;
45 :     if (!wid >= 100) then (nl(); pr " ") else ()
46 :     end
47 :     in
48 :     pr prefix; pr " ENV: {"; nl(); pr " ";
49 :     VMap.appi prElem env;
50 :     nl(); pr "}"; nl()
51 :     end
52 :     (* -DEBUG *)
53 :    
54 : jhr 197 fun lookup env x = (case VMap.find (env, x)
55 : jhr 176 of SOME x' => x'
56 :     | NONE => raise Fail(concat[
57 : jhr 197 "no binding for ", Var.uniqueNameOf x, " in environment"
58 : jhr 176 ])
59 :     (* end case *))
60 :    
61 : jhr 189 (* create a new instance of a variable *)
62 : jhr 394 fun newVar x = IL.Var.new (Var.nameOf x, cvtTy(Var.monoTypeOf x))
63 : jhr 189
64 : jhr 1116 (* generate fresh SSA variables and add them to the environment *)
65 :     fun freshVars (env, xs) = let
66 :     fun cvtVar (x, (env, xs)) = let
67 :     val x' = newVar x
68 :     in
69 :     (VMap.insert(env, x, x'), x'::xs)
70 :     end
71 :     val (env, xs) = List.foldl cvtVar (env, []) xs
72 :     in
73 :     (env, List.rev xs)
74 :     end
75 :    
76 :     (* a pending-join node tracks the phi nodes needed to join the assignments
77 :     * that flow into the join node.
78 :     *)
79 :     datatype join = JOIN of {
80 : jhr 1232 env : env, (* the environment that was current at the conditional *)
81 :     (* associated with this node. *)
82 : jhr 1116 arity : int ref, (* actual number of predecessors *)
83 :     nd : IL.node, (* the CFG node for this pending join *)
84 :     phiMap : IL.phi VMap.map ref, (* a mapping from Simple AST variables that are assigned *)
85 :     (* to their phi nodes. *)
86 :     predKill : bool array (* killed predecessor edges (because of DIE or STABILIZE *)
87 :     }
88 :    
89 :     (* a stack of pending joins. The first component specifies the path index of the current
90 :     * path to the join.
91 :     *)
92 :     type pending_joins = (int * join) list
93 :    
94 :     (* create a new pending-join node *)
95 : jhr 1232 fun newJoin (env, arity) = JOIN{
96 :     env = env, arity = ref arity, nd = IL.Node.mkJOIN [], phiMap = ref VMap.empty,
97 : jhr 1116 predKill = Array.array(arity, false)
98 :     }
99 :    
100 :     (* record that a path to the top join in the stack has been killed because f DIE or STABILIZE *)
101 :     fun killPath ((i, JOIN{arity, predKill, ...}) :: _) = (
102 :     arity := !arity - 1;
103 :     Array.update (predKill, i, true))
104 :     | killPath _ = ()
105 :    
106 :     (* record an assignment to the IL variable dstVar (corresponding to the Simple AST variable
107 :     * srcVar) in the current pending-join node. The predIndex specifies which path into the
108 :     * JOIN node this assignment occurs on.
109 :     *)
110 : jhr 1232 fun recordAssign ([], _, _) = ()
111 :     | recordAssign ((predIndex, JOIN{env, phiMap, predKill, nd, ...})::_, srcVar, dstVar) = let
112 : jhr 1116 val arity = Array.length predKill (* the original arity before any killPath calls *)
113 :     val m = !phiMap
114 :     in
115 :     case VMap.find (env, srcVar)
116 :     of NONE => () (* local temporary *)
117 :     | SOME dstVar' => (case VMap.find (m, srcVar)
118 :     of NONE => let
119 :     val lhs = newVar srcVar
120 :     val rhs = List.tabulate (arity, fn i => if (i = predIndex) then dstVar else dstVar')
121 :     in
122 : jhr 1232 (*
123 :     print(concat["recordAssign: ", Var.uniqueNameOf srcVar, " --> ", IL.Var.toString lhs,
124 :     " @ ", IL.Node.toString nd, "\n"]);
125 :     *)
126 : jhr 1116 phiMap := VMap.insert (m, srcVar, (lhs, rhs))
127 :     end
128 :     | SOME(lhs, rhs) => let
129 :     fun update (i, l as x::r) = if (i = predIndex)
130 :     then dstVar::r
131 :     else x::update(i+1, r)
132 :     | update _ = raise Fail "invalid predecessor index"
133 :     in
134 :     phiMap := VMap.insert (m, srcVar, (lhs, update(0, rhs)))
135 :     end
136 :     (* end case *))
137 :     (* end case *)
138 :     end
139 :    
140 :     (* complete a pending join operation by filling in the phi nodes from the phi map and
141 :     * updating the environment.
142 :     *)
143 : jhr 1232 fun commitJoin (joinStk, JOIN{env, arity, nd, phiMap, predKill}) = (case !arity
144 : jhr 1116 of 0 => (env, NONE)
145 : jhr 1763 | 1 => let
146 :     (* there is only one path to the join, so we do not need phi nodes, but
147 :     * we still need to propogate assignments to the next join on the stack.
148 :     *)
149 :     val IL.ND{kind=IL.JOIN{phis, ...}, ...} = nd
150 :     val ix = let (* find pred of this join *)
151 :     fun find i = if Array.sub(predKill, i) then find(i+1) else i
152 :     in
153 :     find 0
154 :     end
155 :     fun doVar (srcVar, (_, xs), env) = let
156 :     val dstVar = List.nth(xs, ix)
157 :     in
158 :     (*
159 :     print(concat["doVar (", Var.uniqueNameOf srcVar, ", ", IL.phiToString phi, ", _) @ ", IL.Node.toString nd, "\n"]);
160 :     *)
161 :     recordAssign (joinStk, srcVar, dstVar);
162 :     VMap.insert (env, srcVar, dstVar)
163 :     end
164 :     val env = VMap.foldli doVar env (!phiMap)
165 :     in
166 :     (env, SOME nd)
167 :     end
168 : jhr 1116 | n => if (n = Array.length predKill)
169 :     then let
170 :     val IL.ND{kind=IL.JOIN{phis, ...}, ...} = nd
171 :     fun doVar (srcVar, phi as (dstVar, _), (env, phis)) = (
172 : jhr 1232 (*
173 :     print(concat["doVar (", Var.uniqueNameOf srcVar, ", ", IL.phiToString phi, ", _) @ ", IL.Node.toString nd, "\n"]);
174 :     *)
175 :     recordAssign (joinStk, srcVar, dstVar);
176 : jhr 1116 (VMap.insert (env, srcVar, dstVar), phi::phis))
177 :     val (env, phis') = VMap.foldli doVar (env, []) (!phiMap)
178 :     in
179 :     phis := phis';
180 :     (env, SOME nd)
181 :     end
182 :     else raise Fail "FIXME: prune killed paths."
183 :     (* end case *))
184 :    
185 : jhr 168 (* expression translation *)
186 : jhr 1116 fun cvtExp (env : env, lhs, exp) = (case exp
187 : jhr 1640 of S.E_Var x => [IL.ASSGN(lhs, IL.VAR(lookup env x))]
188 :     | S.E_Lit lit => [IL.ASSGN(lhs, IL.LIT lit)]
189 : jhr 176 | S.E_Tuple xs => raise Fail "E_Tuple not implemeted"
190 : jhr 188 | S.E_Apply(f, tyArgs, args, ty) => let
191 :     val args' = List.map (lookup env) args
192 :     in
193 :     TranslateBasis.translate (lhs, f, tyArgs, args')
194 :     end
195 : jhr 1640 | S.E_Cons args => [IL.ASSGN(lhs, IL.CONS(IL.Var.ty lhs, List.map (lookup env) args))]
196 : jhr 1688 | S.E_Seq args => [IL.ASSGN(lhs, IL.CONS(IL.Var.ty lhs, List.map (lookup env) args))]
197 : jhr 400 | S.E_Slice(x, indices, ty) => let
198 :     val x = lookup env x
199 :     val mask = List.map isSome indices
200 :     fun cvt NONE = NONE
201 :     | cvt (SOME x) = SOME(lookup env x)
202 :     val indices = List.mapPartial cvt indices
203 :     in
204 :     if List.all (fn b => b) mask
205 : jhr 1640 then [IL.ASSGN(lhs, IL.OP(HighOps.TensorSub(IL.Var.ty x), x::indices))]
206 :     else [IL.ASSGN(lhs, IL.OP(HighOps.Slice(IL.Var.ty lhs, mask), x::indices))]
207 : jhr 400 end
208 : jhr 2016 (* DEPRECATED
209 : jhr 1301 | S.E_Input(_, name, desc, NONE) =>
210 : jhr 1640 [IL.ASSGN(lhs, IL.OP(HighOps.Input(IL.Var.ty lhs, name, desc), []))]
211 : jhr 1301 | S.E_Input(_, name, desc, SOME dflt) =>
212 : jhr 1640 [IL.ASSGN(lhs, IL.OP(HighOps.InputWithDefault(IL.Var.ty lhs, name, desc), [lookup env dflt]))]
213 : jhr 2016 *)
214 : jhr 1687 | S.E_Coerce{srcTy, dstTy, x} => (case (srcTy, dstTy)
215 :     of (Ty.T_Int, Ty.T_Tensor _) =>
216 :     [IL.ASSGN(lhs, IL.OP(HighOps.IntToReal, [lookup env x]))]
217 :     | (Ty.T_Sequence(ty, Ty.DimConst n), Ty.T_DynSequence _) =>
218 :     [IL.ASSGN(lhs, IL.OP(HighOps.MkDynamic(cvtTy ty, n), [lookup env x]))]
219 : jhr 1943 | (Ty.T_Field _, Ty.T_Field _) =>
220 : jhr 1687 (* change in continuity is a no-op *)
221 :     [IL.ASSGN(lhs, IL.VAR(lookup env x))]
222 :     | _ => raise Fail "unsupported type coercion"
223 :     (* end case *))
224 : jhr 2026 | S.E_LoadSeq(ty, nrrd) => raise Fail "FIXME"
225 :     | S.E_LoadImage(ty, nrrd, info) => raise Fail "FIXME"
226 : jhr 176 (* end case *))
227 : jhr 168
228 : jhr 1640 (* add nodes to save the strand state, followed by an exit node *)
229 :     fun saveStrandState (env, (srcState, dstState), exit) = let
230 :     val stateOut = List.map (lookup env) srcState
231 :     fun save (x, x', cfg) = IL.CFG.appendNode (cfg, IL.Node.mkSAVE(x, x'))
232 :     in
233 :     IL.CFG.appendNode (
234 :     ListPair.foldlEq save IL.CFG.empty (dstState, stateOut),
235 :     exit)
236 :     end
237 : jhr 2026 (*DEBUG*)handle ex => raise ex
238 : jhr 1640
239 : jhr 1116 fun cvtBlock (state, env : env, joinStk, S.Block stms) = let
240 :     fun cvt (env : env, cfg, []) = (cfg, env)
241 :     | cvt (env, cfg, stm::stms) = (case stm
242 :     of S.S_Var x => let
243 :     val x' = newVar x
244 :     in
245 :     cvt (VMap.insert (env, x, x'), cfg, stms)
246 :     end
247 :     | S.S_Assign(lhs, rhs) => let
248 :     val lhs' = newVar lhs
249 :     val assigns = cvtExp (env, lhs', rhs)
250 :     in
251 : jhr 1232 (*
252 :     print "doAssign\n";
253 :     *)
254 :     recordAssign (joinStk, lhs, lhs');
255 : jhr 1116 cvt (
256 :     VMap.insert(env, lhs, lhs'),
257 :     IL.CFG.concat(cfg, IL.CFG.mkBlock assigns),
258 :     stms)
259 :     end
260 :     | S.S_IfThenElse(x, b0, b1) => let
261 : jhr 192 val x' = lookup env x
262 : jhr 1232 val join = newJoin (env, 2)
263 : jhr 1116 val (cfg0, _) = cvtBlock (state, env, (0, join)::joinStk, b0)
264 :     val (cfg1, _) = cvtBlock (state, env, (1, join)::joinStk, b1)
265 :     val cond = IL.Node.mkCOND {
266 :     cond = x',
267 :     trueBranch = IL.Node.dummy,
268 :     falseBranch = IL.Node.dummy
269 :     }
270 : jhr 190 in
271 : jhr 1116 IL.Node.addEdge (IL.CFG.exit cfg, cond);
272 : jhr 1232 case commitJoin (joinStk, join)
273 : jhr 1116 of (env, SOME joinNd) => (
274 :     if IL.CFG.isEmpty cfg0
275 :     then (
276 :     IL.Node.setTrueBranch (cond, joinNd);
277 :     IL.Node.setPred (joinNd, cond))
278 :     else (
279 :     IL.Node.setTrueBranch (cond, IL.CFG.entry cfg0);
280 :     IL.Node.setPred (IL.CFG.entry cfg0, cond);
281 :     IL.Node.addEdge (IL.CFG.exit cfg0, joinNd));
282 :     if IL.CFG.isEmpty cfg1
283 :     then (
284 :     IL.Node.setFalseBranch (cond, joinNd);
285 :     IL.Node.setPred (joinNd, cond))
286 :     else (
287 :     IL.Node.setFalseBranch (cond, IL.CFG.entry cfg1);
288 :     IL.Node.setPred (IL.CFG.entry cfg1, cond);
289 :     IL.Node.addEdge (IL.CFG.exit cfg1, joinNd));
290 :     cvt (
291 :     env,
292 : jhr 1339 IL.CFG.concat (
293 :     cfg,
294 :     IL.CFG{entry = cond, exit = joinNd}),
295 : jhr 1116 stms))
296 :     (* the join node has only zero predecessors, so
297 :     * it was killed.
298 :     *)
299 :     | (env, NONE) => raise Fail "unimplemented" (* FIXME *)
300 : jhr 192 (* end case *)
301 : jhr 190 end
302 : jhr 1116 | S.S_New(strandId, args) => let
303 :     val nd = IL.Node.mkNEW{
304 :     strand = strandId,
305 :     args = List.map (lookup env) args
306 :     }
307 : jhr 192 in
308 : jhr 1116 cvt (env, IL.CFG.appendNode (cfg, nd), stms)
309 : jhr 194 end
310 : jhr 1116 | S.S_Die => (
311 :     killPath joinStk;
312 :     (IL.CFG.appendNode (cfg, IL.Node.mkDIE ()), env))
313 : jhr 1640 | S.S_Stabilize => (
314 :     killPath joinStk;
315 :     (IL.CFG.concat (cfg, saveStrandState (env, state, IL.Node.mkSTABILIZE())), env))
316 :     | S.S_Print args => let
317 :     val args = List.map (lookup env) args
318 :     val nd = IL.Node.mkMASSIGN([], Op.Print(List.map IL.Var.ty args), args)
319 :     in
320 :     cvt (env, IL.CFG.appendNode (cfg, nd), stms)
321 :     end
322 : jhr 192 (* end case *))
323 :     in
324 : jhr 1116 cvt (env, IL.CFG.empty, stms)
325 : jhr 192 end
326 : jhr 1339 (*DEBUG*)handle ex => raise ex
327 : jhr 168
328 : jhr 1232 fun cvtTopLevelBlock (env, blk, mkExit) = let
329 : jhr 1640 val (cfg, env) = cvtBlock (([], []), env, [], blk)
330 :     val cfg = IL.CFG.prependNode (IL.Node.mkENTRY(), cfg)
331 :     val cfg = IL.CFG.concat (cfg, mkExit env)
332 : jhr 256 in
333 : jhr 1640 (cfg, env)
334 : jhr 256 end
335 : jhr 1339 (*DEBUG*)handle ex => raise ex
336 : jhr 256
337 : jhr 1116 (* FIXME: the following function could be refactored with cvtTopLevelBlock to share code *)
338 : jhr 1232 fun cvtFragmentBlock (env0, blk) = let
339 : jhr 1640 val (cfg, env) = cvtBlock (([], []), env0, [], blk)
340 : jhr 1116 val entry = IL.Node.mkENTRY ()
341 : jhr 1232 (* the live variables out are those that were not live coming in *)
342 :     val liveOut = VMap.foldli
343 :     (fn (x, x', xs) => if VMap.inDomain(env0, x) then xs else x'::xs)
344 :     [] env
345 :     val exit = IL.Node.mkFRAGMENT liveOut
346 : jhr 1116 in
347 :     if IL.CFG.isEmpty cfg
348 :     then IL.Node.addEdge (entry, exit)
349 :     else (
350 :     IL.Node.addEdge (entry, IL.CFG.entry cfg);
351 : jhr 1232 IL.Node.addEdge (IL.CFG.exit cfg, exit));
352 : jhr 1116 (IL.CFG{entry = entry, exit = exit}, env)
353 :     end
354 : jhr 1339 (*DEBUG*)handle ex => raise ex
355 : jhr 1116
356 : jhr 1640 fun cvtMethod (env, name, state, svars, blk) = let
357 :     (* load the state into fresh variables *)
358 :     val (env, loadCFG) = let
359 :     (* allocate shadow variables for the state variables *)
360 :     val (env, stateIn) = freshVars (env, state)
361 :     fun load (x, x') = IL.ASSGN(x, IL.STATE x')
362 :     in
363 :     (env, IL.CFG.mkBlock (ListPair.map load (stateIn, svars)))
364 :     end
365 : jhr 1116 (* convert the body of the method *)
366 : jhr 1640 val (cfg, env) = cvtBlock ((state, svars), env, [], blk)
367 : jhr 1116 (* add the entry/exit nodes *)
368 :     val entry = IL.Node.mkENTRY ()
369 : jhr 1640 val loadCFG = IL.CFG.prependNode (entry, loadCFG)
370 : jhr 1444 val exit = (case name
371 : jhr 1640 of StrandUtil.Update => IL.Node.mkACTIVE ()
372 :     | StrandUtil.Stabilize => IL.Node.mkRETURN []
373 : jhr 1444 (* end case *))
374 : jhr 1640 val body = IL.CFG.concat (loadCFG, cfg)
375 :     (*DEBUG**val _ = prEnv (StrandUtil.nameToString name, env);*)
376 :     (* FIXME: the following code doesn't work properly *)
377 :     val body = if IL.Node.hasSucc(IL.CFG.exit body)
378 :     then IL.CFG.concat (body, saveStrandState (env, (state, svars), exit))
379 :     else IL.CFG{entry = IL.CFG.entry body, exit = exit}
380 : jhr 1116 in
381 :     IL.Method{
382 :     name = name,
383 : jhr 1640 body = body
384 : jhr 1116 }
385 :     end
386 : jhr 1640 (*DEBUG*)handle ex => (print(concat["error in cvtMethod(", StrandUtil.nameToString name, ", ...)\n"]); raise ex)
387 : jhr 1116
388 :     (* convert the initially code *)
389 :     fun cvtInitially (env, S.Initially{isArray, rangeInit, create, iters}) = let
390 :     val S.C_Create{argInit, name, args} = create
391 :     fun cvtIter ({param, lo, hi}, (env, iters)) = let
392 :     val param' = newVar param
393 :     val env = VMap.insert (env, param, param')
394 :     val iter = (param', lookup env lo, lookup env hi)
395 : jhr 256 in
396 : jhr 1116 (env, iter::iters)
397 : jhr 256 end
398 : jhr 1116 val (cfg, env) = cvtFragmentBlock (env, rangeInit)
399 :     val (env, iters) = List.foldl cvtIter (env, []) iters
400 :     val (argInitCFG, env) = cvtFragmentBlock (env, argInit)
401 : jhr 256 in
402 : jhr 1116 IL.Initially{
403 :     isArray = isArray,
404 :     rangeInit = cfg,
405 :     iters = List.rev iters,
406 :     create = (argInitCFG, name, List.map (lookup env) args)
407 :     }
408 : jhr 256 end
409 :    
410 : jhr 1640 (* check strands for properties *)
411 :     fun checkProps strands = let
412 :     val hasDie = ref false
413 :     val hasNew = ref false
414 :     fun chkStm e = (case e
415 :     of S.S_IfThenElse(_, b1, b2) => (chkBlk b1; chkBlk b2)
416 :     | S.S_New _ => (hasNew := true)
417 :     | S.S_Die => (hasDie := true)
418 :     | _ => ()
419 :     (* end case *))
420 :     and chkBlk (S.Block body) = List.app chkStm body
421 :     fun chkStrand (S.Strand{stateInit, methods, ...}) = let
422 :     fun chkMeth (S.Method(_, body)) = chkBlk body
423 :     in
424 :     chkBlk stateInit;
425 :     List.app chkMeth methods
426 :     end
427 :     fun condCons (x, v, l) = if !x then v::l else l
428 :     in
429 :     List.app chkStrand strands;
430 :     condCons (hasDie, StrandUtil.StrandsMayDie,
431 :     condCons (hasNew, StrandUtil.NewStrands, []))
432 :     end
433 :    
434 : jhr 2018 fun cvtInputs inputs = let
435 :     fun cvt ((x, inp), (env, stms)) = let
436 :     val x' = newVar x
437 :     val stm = IL.ASSGN(x', IL.OP(Op.Input(Inputs.map cvtTy inp), []))
438 :     in
439 :     (VMap.insert(env, x, x'), stm::stms)
440 :     end
441 :     val (env, stms) = List.foldr cvt (VMap.empty, []) inputs
442 :     in
443 :     (IL.CFG.mkBlock stms, env)
444 :     end
445 :    
446 : jhr 2016 fun translate (S.Program{inputs, globals, globalInit, init, strands}) = let
447 : jhr 1640 val (globalInit, env) = let
448 :     fun mkExit env = let
449 :     val nd = IL.Node.mkRETURN(VMap.listItems env)
450 :     in
451 :     IL.CFG{entry = nd, exit = nd}
452 :     end
453 : jhr 2018 val (inputBlk, env) = cvtInputs inputs
454 :     val (globBlk, env) = cvtBlock (([], []), env, [], globalInit)
455 :     val cfg = IL.CFG.prependNode (IL.Node.mkENTRY(), inputBlk)
456 :     val cfg = IL.CFG.concat(cfg, globBlk)
457 :     val cfg = IL.CFG.concat (cfg, mkExit env)
458 : jhr 1640 in
459 : jhr 2018 (cfg, env)
460 : jhr 1640 end
461 : jhr 2018 (* construct a reduced environment that just defines the globals (including inputs). *)
462 : jhr 1116 val env = let
463 : jhr 200 val lookup = lookup env
464 : jhr 1116 fun cvtVar (x, env) = VMap.insert(env, x, lookup x)
465 : jhr 2018 val env = List.foldl (fn ((x, _), env) => cvtVar(x, env)) VMap.empty inputs
466 :     val env = List.foldl cvtVar env globals
467 : jhr 200 in
468 : jhr 1116 env
469 : jhr 200 end
470 : jhr 1116 val init = cvtInitially (env, init)
471 : jhr 511 fun cvtStrand (S.Strand{name, params, state, stateInit, methods}) = let
472 : jhr 1232 (* extend the global environment with the strand's parameters *)
473 : jhr 200 val (env, params) = let
474 :     fun cvtParam (x, (env, xs)) = let
475 :     val x' = newVar x
476 :     in
477 :     (VMap.insert(env, x, x'), x'::xs)
478 :     end
479 :     val (env, params) = List.foldl cvtParam (env, []) params
480 :     in
481 :     (env, List.rev params)
482 :     end
483 : jhr 1640 (* create the state variables *)
484 :     val svars = let
485 :     fun newSVar x = IL.StateVar.new (
486 :     Var.kindOf x = S.StrandOutputVar,
487 :     Var.nameOf x, cvtTy(Var.monoTypeOf x))
488 :     in
489 :     List.map newSVar state
490 :     end
491 : jhr 1232 (* convert the state initialization code *)
492 :     val (stateInit, env) = let
493 : jhr 1640 fun mkExit env = saveStrandState (env, (state, svars), IL.Node.mkSINIT())
494 : jhr 1232 in
495 :     cvtTopLevelBlock (env, stateInit, mkExit)
496 :     end
497 : jhr 1640 fun cvtMeth (S.Method(name, blk)) = cvtMethod (env, name, state, svars, blk)
498 : jhr 200 in
499 : jhr 511 IL.Strand{
500 : jhr 200 name = name,
501 :     params = params,
502 : jhr 1640 state = svars,
503 : jhr 200 stateInit = stateInit,
504 : jhr 1116 methods = List.map cvtMeth methods
505 : jhr 200 }
506 :     end
507 : jhr 511 val prog = IL.Program{
508 : jhr 1640 props = checkProps strands,
509 : jhr 1116 globalInit = globalInit,
510 :     initially = init,
511 :     strands = List.map cvtStrand strands
512 :     }
513 : jhr 511 in
514 :     Census.init prog;
515 :     prog
516 : jhr 200 end
517 : jhr 176
518 : jhr 137 end

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