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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3476 - (view) (download)

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

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