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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

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