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

SCM Repository

[diderot] Annotation of /branches/vis12/src/compiler/tree-il/low-to-tree-fn.sml
ViewVC logotype

Annotation of /branches/vis12/src/compiler/tree-il/low-to-tree-fn.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2820 - (view) (download)

1 : jhr 1115 (* low-to-tree-fn.sml
2 :     *
3 :     * COPYRIGHT (c) 2011 The Diderot Project (http://diderot-language.cs.uchicago.edu)
4 :     * All rights reserved.
5 :     *
6 :     * This module translates the LowIL representation of a program (i.e., a pure CFG) to
7 :     * a block-structured AST with nested expressions.
8 :     *
9 :     * NOTE: this translation is pretty dumb about variable coalescing (i.e., it doesn't do any).
10 :     *)
11 :    
12 :     functor LowToTreeFn (Target : sig
13 :    
14 : jhr 1640 val supportsPrinting : unit -> bool (* does the target support the Print op? *)
15 :    
16 : jhr 1115 (* tests for whether various expression forms can appear inline *)
17 : jhr 2279 val inlineCons : int -> bool (* can n'th-order tensor construction appear inline *)
18 :     val inlineMatrixExp : bool (* can matrix-valued expressions appear inline? *)
19 : jhr 1115
20 :     end) : sig
21 :    
22 :     val translate : LowIL.program -> TreeIL.program
23 :    
24 :     end = struct
25 :    
26 :     structure IL = LowIL
27 :     structure Ty = LowILTypes
28 :     structure V = LowIL.Var
29 : jhr 2796 structure GV = LowIL.GlobalVar
30 : jhr 1640 structure StV = LowIL.StateVar
31 : jhr 1115 structure Op = LowOps
32 :     structure Nd = LowIL.Node
33 :     structure CFG = LowIL.CFG
34 :     structure T = TreeIL
35 :     structure VA = VarAnalysis
36 : jhr 2012 structure InP = Inputs
37 : jhr 1115
38 : jhr 2796 (* associate Tree IL globals variables with Low IL variables using properties *)
39 : jhr 1115 local
40 : jhr 2796 fun mkGlobalVar x = T.GV{
41 :     name = GV.name x,
42 :     ty = GV.ty x,
43 :     input = GV.isInput x
44 :     }
45 : jhr 1115 in
46 : jhr 2818 val {setFn = setGlobalVar, getFn = getGlobalVar : IL.global_var -> T.global_var, ...} =
47 : jhr 2820 GV.newProp (fn x => raise Fail(concat["getGlobalVar(", GV.uniqueName x, ")"]))
48 : jhr 1115 end
49 :    
50 : jhr 1640 (* associate Tree IL state variables with Low IL variables using properties *)
51 :     local
52 :     fun mkStateVar x = T.SV{
53 :     name = StV.name x,
54 :     ty = StV.ty x,
55 :     varying = VA.isVarying x,
56 :     output = StV.isOutput x
57 :     }
58 :     in
59 :     val {getFn = getStateVar, ...} = StV.newProp mkStateVar
60 :     end
61 :    
62 : jhr 2796 (* create new tree IL variables *)
63 :     local
64 :     val newVar = T.Var.new
65 :     val cnt = ref 0
66 :     fun genName prefix = let
67 :     val n = !cnt
68 :     in
69 :     cnt := n+1;
70 :     String.concat[prefix, "_", Int.toString n]
71 :     end
72 :     in
73 :     fun newParam x = newVar (genName("p_" ^ V.name x), V.ty x)
74 :     fun newLocal x = newVar (genName("l_" ^ V.name x), V.ty x)
75 :     fun newIter x = newVar (genName("i_" ^ V.name x), V.ty x)
76 :     end
77 :    
78 : jhr 1115 fun mkBlock stms = T.Block{locals=[], body=stms}
79 :     fun mkIf (x, stms, []) = T.S_IfThen(x, mkBlock stms)
80 :     | mkIf (x, stms1, stms2) = T.S_IfThenElse(x, mkBlock stms1, mkBlock stms2)
81 :    
82 :     (* an environment that tracks bindings of variables to target expressions and the list
83 :     * of locals that have been defined.
84 :     *)
85 :     local
86 :     structure VT = V.Tbl
87 :     fun decCount (IL.V{useCnt, ...}) = let
88 : jhr 2279 val n = !useCnt - 1
89 :     in
90 :     useCnt := n; (n <= 0)
91 :     end
92 : jhr 1115 datatype target_binding
93 : jhr 2818 = TREE of T.exp (* variable bound to target expression tree *)
94 : jhr 2279 | DEF of T.exp (* either a target variable or constant for a defined variable *)
95 : jhr 2820 | INP of T.global_var -> T.stm
96 :     (* Input statement constructor *)
97 : jhr 1115 datatype env = E of {
98 : jhr 2279 tbl : target_binding VT.hash_table,
99 :     locals : T.var list
100 :     }
101 : jhr 1115 in
102 :     (* DEBUG *)
103 :     fun bindToString binding = (case binding
104 : jhr 2818 of TREE e => "TREE"
105 : jhr 2279 | DEF(T.E_Var y) => "DEF " ^ T.Var.name y
106 :     | DEF e => "DEF"
107 : jhr 2820 | INP _ => "INP"
108 : jhr 1115 (* end case *))
109 :     fun dumpEnv (E{tbl, ...}) = let
110 :     fun prEntry (x, binding) =
111 : jhr 2279 print(concat[" ", IL.Var.toString x, " --> ", bindToString binding, "\n"])
112 : jhr 1115 in
113 : jhr 2279 print "*** dump environment\n";
114 :     VT.appi prEntry tbl;
115 :     print "***\n"
116 : jhr 1115 end
117 :     (* DEBUG *)
118 :    
119 :     fun newEnv () = E{tbl = VT.mkTable (512, Fail "tbl"), locals=[]}
120 :    
121 :     (* use a variable. If it is a pending expression, we remove it from the table *)
122 :     fun useVar (env as E{tbl, ...}) x = (case VT.find tbl x
123 : jhr 2818 of SOME(TREE e) => (
124 : jhr 1115 (*print(concat["useVar ", V.toString x, " ==> TREE\n"]);*)
125 : jhr 2279 ignore(VT.remove tbl x);
126 :     e)
127 :     | SOME(DEF e) => (
128 : jhr 1115 (*print(concat["useVar ", V.toString x, " ==> ", bindToString(DEF e), "; use count = ", Int.toString(V.useCount x), "\n"]);*)
129 : jhr 2279 (* if this is the last use of x, then remove it from the table *)
130 :     if (decCount x) then ignore(VT.remove tbl x) else ();
131 :     e)
132 : jhr 2818 | _ => (
133 : jhr 1115 dumpEnv env;
134 :     raise Fail(concat ["useVar(", V.toString x, ")"])
135 :     )
136 : jhr 2279 (* end case *))
137 : jhr 1115
138 : jhr 2818 (* check if a variable is bound to an input-statement constructor *)
139 :     fun useAsInput (env as E{tbl, ...}, x) = (case VT.find tbl x
140 : jhr 2820 of SOME(INP mk) => SOME mk
141 :     | _ => NONE
142 :     (* end case *))
143 : jhr 2818
144 : jhr 1115 (* record a local variable *)
145 :     fun addLocal (E{tbl, locals}, x) = E{tbl=tbl, locals=x::locals}
146 :    
147 :     (* insert a pending expression into the table. Note that x should only be used once! *)
148 :     fun insert (env as E{tbl, ...}, x, exp) = (
149 : jhr 2279 VT.insert tbl (x, TREE exp);
150 :     env)
151 : jhr 1115
152 :     fun rename (env as E{tbl, ...}, x, x') = (
153 : jhr 2279 VT.insert tbl (x, DEF(T.E_Var x'));
154 :     env)
155 : jhr 1115
156 : jhr 2818 fun bind (env, lhs, rhs) = if (V.useCount lhs = 1)
157 : jhr 2279 then (insert(env, lhs, rhs), [])
158 :     else let
159 :     val t = newLocal lhs
160 :     in
161 :     (rename(addLocal(env, t), lhs, t), [T.S_Assign([t], rhs)])
162 :     end
163 : jhr 1115
164 : jhr 2818 fun recordInput (env as E{tbl, ...}, lhs, mkFn) = if (V.useCount lhs > 1)
165 : jhr 2820 then raise Fail(concat[
166 :     "recordInput: useCount(", V.toString lhs, ") = ", Int.toString(V.useCount lhs)
167 :     ])
168 :     else (VT.insert tbl (lhs, INP mkFn); env)
169 : jhr 1115
170 :     (* set the definition of a variable, where the RHS is either a literal constant or a variable *)
171 :     fun bindSimple (env as E{tbl, ...}, lhs, rhs) = (
172 : jhr 2818 VT.insert tbl (lhs, DEF rhs); (env, []))
173 : jhr 1115
174 :     (* at the end of a block, we need to assign any pending expressions to locals. The
175 :     * blkStms list and the resulting statement list are in reverse order.
176 :     *)
177 :     fun flushPending (E{tbl, locals}, blkStms) = let
178 : jhr 2279 fun doVar (x, TREE e, (locals, stms)) = let
179 :     val t = newLocal x
180 :     in
181 :     VT.insert tbl (x, DEF(T.E_Var t));
182 :     (t::locals, T.S_Assign([t], e)::stms)
183 :     end
184 :     | doVar (_, _, acc) = acc
185 :     val (locals, stms) = VT.foldi doVar (locals, blkStms) tbl
186 :     in
187 :     (E{tbl=tbl, locals=locals}, stms)
188 :     end
189 : jhr 1115
190 :     fun doPhi ((lhs, rhs), (env, predBlks : T.stm list list)) = let
191 : jhr 2279 (* t will be the variable in the continuation of the JOIN *)
192 :     val t = newLocal lhs
193 :     val predBlks = ListPair.map
194 :     (fn (x, stms) => T.S_Assign([t], useVar env x)::stms)
195 :     (rhs, predBlks)
196 :     in
197 :     (rename (addLocal(env, t), lhs, t), predBlks)
198 :     end
199 : jhr 1115
200 :     fun endScope (E{locals, ...}, stms) = T.Block{
201 : jhr 2279 locals = List.rev locals,
202 :     body = stms
203 :     }
204 : jhr 1115
205 :     end
206 :    
207 :     (* Certain IL operators cannot be compiled to inline expressions. Return
208 :     * false for those and true for all others.
209 :     *)
210 :     fun isInlineOp rator = let
211 : jhr 2279 fun chkTensorTy (Ty.TensorTy[]) = true
212 :     | chkTensorTy (Ty.TensorTy[_]) = true
213 :     | chkTensorTy (Ty.TensorTy[_, _]) = Target.inlineMatrixExp
214 :     | chkTensorTy _ = false
215 :     in
216 :     case rator
217 :     of Op.LoadVoxels(_, 1) => true
218 :     | Op.LoadVoxels _ => false
219 :     | Op.Add ty => chkTensorTy ty
220 :     | Op.Sub ty => chkTensorTy ty
221 :     | Op.Neg ty => chkTensorTy ty
222 :     | Op.Scale ty => chkTensorTy ty
223 :     | Op.MulMatMat _ => Target.inlineMatrixExp
224 :     | Op.MulVecTen3 _ => false
225 :     | Op.MulTen3Vec _ => false
226 :     | Op.ColonMul(Ty.TensorTy dd1, Ty.TensorTy dd2) =>
227 :     (* if the result is a vector or scalar, then it is inline *)
228 :     (List.length dd1 + List.length dd2 > 5)
229 : jhr 1640 | Op.EigenVecs2x2 => false
230 :     | Op.EigenVecs3x3 => false
231 :     | Op.EigenVals2x2 => false
232 :     | Op.EigenVals3x3 => false
233 : jhr 2279 | Op.Identity _ => Target.inlineMatrixExp
234 :     | Op.Zero _ => Target.inlineMatrixExp
235 : jhr 2345 | Op.Transpose _ => false
236 : jhr 2279 | Op.TensorToWorldSpace(_, ty) => chkTensorTy ty
237 :     | _ => true
238 :     (* end case *)
239 :     end
240 : jhr 1115
241 : jhr 2059 (* is a CONS inline? *)
242 :     fun isInlineCons ty = (case ty
243 : jhr 2279 of Ty.SeqTy(Ty.IntTy, _) => true
244 :     | Ty.TensorTy dd => Target.inlineCons(List.length dd)
245 :     | Ty.SeqTy _ => false
246 :     | Ty.DynSeqTy ty => false
247 :     | _ => raise Fail(concat["invalid CONS<", Ty.toString ty, ">"])
248 :     (* end case *))
249 : jhr 2059
250 : jhr 2343 (* translate a LowIL assignment to a list of zero or more target statements in reverse
251 :     * order.
252 :     *)
253 : jhr 1115 fun doAssign (env, (lhs, rhs)) = let
254 : jhr 2818 fun doLHS () = let
255 : jhr 2820 val t = newLocal lhs
256 :     in
257 :     (rename (addLocal(env, t), lhs, t), t)
258 :     end
259 : jhr 2279 (* for expressions that are going to be compiled to a call statement *)
260 :     fun assignExp (env, exp) = let
261 :     (* operations that return matrices may not be supported inline *)
262 :     val (env, t) = doLHS()
263 :     in
264 :     (env, [T.S_Assign([t], exp)])
265 :     end
266 : jhr 2345 (* force an argument to be stored in something that will be mapped to an l-value *)
267 :     fun bindVar (env, x) = (case useVar env x
268 : jhr 2796 of x' as T.E_Global _ => (env, x', [])
269 : jhr 2805 | x' as T.E_State _ => (env, x', [])
270 : jhr 2345 | x' as T.E_Var _ => (env, x', [])
271 :     | e => let
272 :     val x' = newLocal x
273 :     in
274 :     (addLocal(env, x'), T.E_Var x', [T.S_Assign([x'], e)])
275 :     end
276 :     (* end case *))
277 : jhr 2279 in
278 :     case rhs
279 : jhr 2796 of IL.GLOBAL x => bindSimple (env, lhs, T.E_Global(getGlobalVar x))
280 : jhr 2805 | IL.STATE x => bindSimple (env, lhs, T.E_State(getStateVar x))
281 : jhr 1640 | IL.VAR x => bindSimple (env, lhs, useVar env x)
282 : jhr 2279 | IL.LIT lit => bindSimple (env, lhs, T.E_Lit lit)
283 : jhr 2345 | IL.OP(Op.Prepend ty, [item, seq]) => let
284 :     val (env, t) = doLHS()
285 :     val (env, item', stms) = bindVar (env, item)
286 : jhr 2343 val exp = T.E_Op(Op.Prepend ty, [item', useVar env seq])
287 : jhr 2345 in
288 :     (env, T.S_Assign([t], exp) :: stms)
289 :     end
290 :     | IL.OP(Op.Append ty, [seq, item]) => let
291 :     val (env, t) = doLHS()
292 :     val (env, item', stms) = bindVar (env, item)
293 : jhr 2343 val exp = T.E_Op(Op.Append ty, [useVar env seq, item'])
294 : jhr 2345 in
295 :     (env, T.S_Assign([t], exp) :: stms)
296 :     end
297 : jhr 2279 | IL.OP(Op.LoadSeq(ty, nrrd), []) => let
298 :     val (env, t) = doLHS()
299 :     in
300 :     (env, [T.S_LoadNrrd(t, ty, nrrd)])
301 :     end
302 : jhr 2796 | IL.OP(Op.LoadImage(ty, nrrd), []) => let
303 : jhr 2279 val (env, t) = doLHS()
304 :     in
305 :     (env, [T.S_LoadNrrd(t, ty, nrrd)])
306 :     end
307 :     | IL.OP(Op.Input(InP.INP{ty=Ty.DynSeqTy _, name, desc, init}), []) => let
308 : jhr 2820 fun mkStm gv = (case init
309 :     of NONE => T.S_InputNrrd(gv, name, desc, NONE)
310 :     | SOME(InP.DynSeq nrrd) => T.S_InputNrrd(gv, name, desc, SOME nrrd)
311 :     | _ => raise Fail "bogus initialization for dynamic sequence"
312 :     (* end case *))
313 :     in
314 : jhr 2818 (recordInput (env, lhs, mkStm), [])
315 : jhr 2279 end
316 :     | IL.OP(Op.Input(InP.INP{ty=Ty.ImageTy _, name, desc, init}), []) => let
317 : jhr 2820 fun mkStm gv = (case init
318 :     of SOME(InP.Proxy(nrrd, _)) => T.S_InputNrrd(gv, name, desc, SOME nrrd)
319 :     | SOME(InP.Image _) => T.S_InputNrrd(gv, name, desc, NONE)
320 :     | _ => raise Fail "bogus initialization for image"
321 :     (* end case *))
322 : jhr 2279 in
323 : jhr 2818 (recordInput (env, lhs, mkStm), [])
324 : jhr 2279 end
325 :     | IL.OP(Op.Input(InP.INP{ty, name, desc, init=NONE}), []) => let
326 : jhr 2818 fun mkStm gv = T.S_Input(gv, name, desc, NONE)
327 : jhr 2279 in
328 : jhr 2818 (recordInput (env, lhs, mkStm), [])
329 : jhr 2279 end
330 : jhr 2813 | IL.OP(Op.InputWithDefault(InP.INP{ty, name, desc, init=NONE}), [dflt]) => let
331 : jhr 2820 val dflt = useVar env dflt
332 : jhr 2818 fun mkStm gv = T.S_Input(gv, name, desc, SOME dflt)
333 : jhr 2813 in
334 : jhr 2818 (recordInput (env, lhs, mkStm), [])
335 : jhr 2813 end
336 : jhr 2279 | IL.OP(rator, args) => let
337 :     val exp = T.E_Op(rator, List.map (useVar env) args)
338 :     in
339 :     if isInlineOp rator
340 :     then bind (env, lhs, exp)
341 :     else assignExp (env, exp)
342 :     end
343 :     | IL.APPLY(f, args) =>
344 :     bind (env, lhs, T.E_Apply(f, List.map (useVar env) args))
345 :     | IL.CONS(ty, args) => let
346 :     val exp = T.E_Cons(ty, List.map (useVar env) args)
347 :     in
348 :     if isInlineCons ty
349 :     then bind (env, lhs, exp)
350 :     else assignExp (env, exp)
351 :     end
352 :     (* end case *)
353 :     end
354 : jhr 1115
355 :     (* In order to reconstruct the block-structure from the CFG, we keep a stack of open ifs.
356 :     * the items on this stack distinguish between when we are processing the then and else
357 :     * branches of the if.
358 :     *)
359 :     datatype open_if
360 :     (* working on the "then" branch. The fields are statments that preceed the if, the condition,
361 :     * and the else-branch node.
362 :     *)
363 :     = THEN_BR of T.stm list * T.exp * IL.node
364 :     (* working on the "else" branch. The fields are statments that preceed the if, the condition,
365 :     * the "then" branch statements, and the node that terminated the "then" branch (will be
366 :     * a JOIN, DIE, or STABILIZE).
367 :     *)
368 :     | ELSE_BR of T.stm list * T.exp * T.stm list * IL.node_kind
369 :    
370 :     fun trCFG (env, prefix, finish, cfg) = let
371 : jhr 2279 fun join (env, [], _, IL.JOIN _) = raise Fail "JOIN with no open if"
372 :     | join (env, [], stms, _) = endScope (env, prefix @ List.rev stms)
373 :     | join (env, THEN_BR(stms1, cond, elseBr)::stk, thenBlk, k) = let
374 :     val (env, thenBlk) = flushPending (env, thenBlk)
375 :     in
376 :     doNode (env, ELSE_BR(stms1, cond, thenBlk, k)::stk, [], elseBr)
377 :     end
378 :     | join (env, ELSE_BR(stms, cond, thenBlk, k1)::stk, elseBlk, k2) = let
379 :     val (env, elseBlk) = flushPending (env, elseBlk)
380 :     in
381 :     case (k1, k2)
382 :     of (IL.JOIN{phis, succ, ...}, IL.JOIN _) => let
383 :     val (env, [thenBlk, elseBlk]) =
384 :     List.foldl doPhi (env, [thenBlk, elseBlk]) (!phis)
385 :     val stm = mkIf(cond, List.rev thenBlk, List.rev elseBlk)
386 :     in
387 :     doNode (env, stk, stm::stms, !succ)
388 :     end
389 :     | (IL.JOIN{phis, succ, ...}, _) => let
390 :     val (env, [thenBlk]) = List.foldl doPhi (env, [thenBlk]) (!phis)
391 :     val stm = mkIf(cond, List.rev thenBlk, List.rev elseBlk)
392 :     in
393 :     doNode (env, stk, stm::stms, !succ)
394 :     end
395 :     | (_, IL.JOIN{phis, succ, ...}) => let
396 :     val (env, [elseBlk]) = List.foldl doPhi (env, [elseBlk]) (!phis)
397 :     val stm = mkIf(cond, List.rev thenBlk, List.rev elseBlk)
398 :     in
399 :     doNode (env, stk, stm::stms, !succ)
400 :     end
401 :     | (_, _) => raise Fail "no path to exit unimplemented" (* FIXME *)
402 :     (* end case *)
403 :     end
404 :     and doNode (env, ifStk : open_if list, stms, nd) = (
405 :     case Nd.kind nd
406 :     of IL.NULL => raise Fail "unexpected NULL"
407 :     | IL.ENTRY{succ} => doNode (env, ifStk, stms, !succ)
408 :     | k as IL.JOIN{phis, succ, ...} => join (env, ifStk, stms, k)
409 :     | IL.COND{cond, trueBranch, falseBranch, ...} => let
410 :     val cond = useVar env cond
411 :     val (env, stms) = flushPending (env, stms)
412 :     in
413 :     doNode (env, THEN_BR(stms, cond, !falseBranch)::ifStk, [], !trueBranch)
414 :     end
415 :     | IL.COM {text, succ, ...} =>
416 :     doNode (env, ifStk, T.S_Comment text :: stms, !succ)
417 :     | IL.ASSIGN{stm, succ, ...} => let
418 :     val (env, stms') = doAssign (env, stm)
419 :     in
420 :     doNode (env, ifStk, stms' @ stms, !succ)
421 :     end
422 : jhr 1640 | IL.MASSIGN{stm=(ys, rator, xs), succ, ...} => let
423 :     fun doit () = let
424 : jhr 2818 fun doLHSVar (y, (env, ys)) = let
425 : jhr 2820 val t = newLocal y
426 :     in
427 :     (rename (addLocal(env, t), y, t), t::ys)
428 :     end
429 : jhr 1640 val (env, ys) = List.foldr doLHSVar (env, []) ys
430 :     val exp = T.E_Op(rator, List.map (useVar env) xs)
431 :     val stm = T.S_Assign(ys, exp)
432 :     in
433 :     doNode (env, ifStk, stm :: stms, !succ)
434 :     end
435 :     in
436 :     case rator
437 :     of Op.Print _ => if Target.supportsPrinting()
438 :     then doit ()
439 :     else doNode (env, ifStk, stms, !succ)
440 :     | _ => doit()
441 :     (* end case *)
442 :     end
443 : jhr 2805 | IL.GASSIGN{lhs, rhs, succ, ...} => let
444 : jhr 2818 val stm = (case useAsInput(env, rhs)
445 : jhr 2820 of NONE => T.S_GAssign(getGlobalVar lhs, useVar env rhs)
446 :     | SOME mkStm => mkStm(getGlobalVar lhs)
447 :     (* end case *))
448 : jhr 2805 in
449 :     doNode (env, ifStk, stm::stms, !succ)
450 : jhr 2796 end
451 : jhr 2279 | IL.NEW{strand, args, succ, ...} => raise Fail "NEW unimplemented"
452 : jhr 1640 | IL.SAVE{lhs, rhs, succ, ...} => let
453 :     val stm = T.S_Save([getStateVar lhs], useVar env rhs)
454 :     in
455 :     doNode (env, ifStk, stm::stms, !succ)
456 :     end
457 : jhr 2279 | k as IL.EXIT{kind, live, ...} => (case kind
458 :     of ExitKind.FRAGMENT =>
459 :     endScope (env, prefix @ List.revAppend(stms, finish env))
460 :     | ExitKind.SINIT => let
461 : jhr 1232 (* FIXME: we should probably call flushPending here! *)
462 : jhr 2279 val suffix = finish env @ [T.S_Exit[]]
463 :     in
464 :     endScope (env, prefix @ List.revAppend(stms, suffix))
465 :     end
466 :     | ExitKind.RETURN => let
467 : jhr 1115 (* FIXME: we should probably call flushPending here! *)
468 : jhr 2279 val suffix = finish env @ [T.S_Exit(List.map (useVar env) live)]
469 :     in
470 :     endScope (env, prefix @ List.revAppend(stms, suffix))
471 :     end
472 :     | ExitKind.ACTIVE => let
473 : jhr 1115 (* FIXME: we should probably call flushPending here! *)
474 : jhr 2279 val suffix = finish env @ [T.S_Active]
475 :     in
476 :     endScope (env, prefix @ List.revAppend(stms, suffix))
477 :     end
478 :     | ExitKind.STABILIZE => let
479 : jhr 1115 (* FIXME: we should probably call flushPending here! *)
480 : jhr 2279 val stms = T.S_Stabilize :: stms
481 :     in
482 : jhr 1115 (* FIXME: we should probably call flushPending here! *)
483 : jhr 2279 join (env, ifStk, stms, k)
484 :     end
485 :     | ExitKind.DIE => join (env, ifStk, T.S_Die :: stms, k)
486 :     (* end case *))
487 :     (* end case *))
488 :     in
489 :     doNode (env, [], [], CFG.entry cfg)
490 :     end
491 : jhr 1115
492 :     fun trInitially (env, IL.Initially{isArray, rangeInit, iters, create=(createInit, strand, args)}) =
493 : jhr 2279 let
494 :     val iterPrefix = trCFG (env, [], fn _ => [], rangeInit)
495 :     fun cvtIter ((param, lo, hi), (env, iters)) = let
496 :     val param' = newIter param
497 :     val env = rename (env, param, param')
498 :     in
499 :     (env, (param', useVar env lo, useVar env hi)::iters)
500 :     end
501 :     val (env, iters) = List.foldr cvtIter (env, []) iters
502 :     val createPrefix = trCFG (env, [], fn _ => [], createInit)
503 :     in {
504 :     isArray = isArray,
505 :     iterPrefix = iterPrefix,
506 :     iters = iters,
507 :     createPrefix = createPrefix,
508 :     strand = strand,
509 :     args = List.map (useVar env) args
510 :     } end
511 : jhr 1115
512 : jhr 1640 fun trMethod env (IL.Method{name, body}) = T.Method{
513 :     name = name,
514 :     body = trCFG (env, [], fn _ => [], body)
515 :     }
516 : jhr 1115
517 : jhr 1640 fun trStrand globalEnv (IL.Strand{name, params, state, stateInit, methods}) = let
518 : jhr 2279 val params' = List.map newParam params
519 :     val env = ListPair.foldlEq (fn (x, x', env) => rename(env, x, x')) globalEnv (params, params')
520 :     in
521 :     T.Strand{
522 :     name = name,
523 :     params = params',
524 :     state = List.map getStateVar state,
525 :     stateInit = trCFG (env, [], fn _ => [], stateInit),
526 :     methods = List.map (trMethod env) methods
527 :     }
528 :     end
529 : jhr 1115
530 :     fun translate prog = let
531 : jhr 2279 (* first we do a variable analysis pass on the Low IL *)
532 : jhr 2799 val prog as IL.Program{props, globals, inputInit, globalInit, initially, strands} = VA.optimize prog
533 : jhr 1115 (* FIXME: here we should do a contraction pass to eliminate unused variables that VA may have created *)
534 : jhr 2279 val _ = (* DEBUG *)
535 :     LowPP.output (Log.logFile(), "LowIL after variable analysis", prog)
536 :     val env = newEnv()
537 : jhr 2818 val globals = let
538 :     (* produce a list of TreeIL globals for the globals, such that each global
539 :     * has a unique name.
540 :     *)
541 : jhr 2820 fun genGlobal (gv, (cnt, globs)) = let
542 :     val name = "gv_" ^ GV.name gv
543 :     fun uniqueName (name, cnt) =
544 :     if List.exists (fn y => T.GlobalVar.name y = name) globs
545 :     then uniqueName(name ^ Int.toString cnt, cnt+1)
546 :     else (name, cnt)
547 :     val (name, cnt) = uniqueName ("gv_" ^ GV.name gv, cnt)
548 :     val gv' = T.GV{name = name, ty = GV.ty gv, input = GV.isInput gv}
549 :     in
550 :     setGlobalVar (gv, gv');
551 :     (cnt, gv'::globs)
552 :     end
553 :     val (_, globs) = List.foldl genGlobal (0, []) globals
554 :     in
555 :     List.rev globs
556 :     end
557 : jhr 2279 val strands = List.map (trStrand env) strands
558 :     in
559 :     T.Program{
560 :     props = props,
561 :     globals = globals,
562 :     inputInit = trCFG (env, [], fn _ => [], inputInit),
563 :     globalInit = trCFG (env, [], fn _ => [], globalInit),
564 :     strands = strands,
565 :     initially = trInitially (env, initially)
566 :     }
567 :     end
568 : jhr 1115
569 :     end

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