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

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