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

SCM Repository

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

Annotation of /branches/vis15/src/compiler/low-to-tree/low-to-tree.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3852 - (view) (download)

1 : jhr 3828 (* low-to-tree.sml
2 :     *
3 :     * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
4 :     *
5 :     * COPYRIGHT (c) 2016 The University of Chicago
6 :     * All rights reserved.
7 :     *)
8 :    
9 :     structure LowToTree : sig
10 :    
11 : jhr 3851 val translate : LowIR.program * Env.target_info -> TreeIR.program
12 : jhr 3828
13 :     end = struct
14 :    
15 : jhr 3834 structure IR = LowIR
16 :     structure V = LowIR.Var
17 :     structure Ty = LowTypes
18 :     structure Op = LowOps
19 : jhr 3835 structure GV = IR.GlobalVar
20 :     structure SV = IR.StateVar
21 : jhr 3834 structure T = TreeIR
22 :     structure TTy = TreeTypes
23 :     structure TOp = TreeOps
24 :     structure TV = TreeVar
25 :     structure TGV = TreeGlobalVar
26 :     structure TSV = TreeStateVar
27 : jhr 3828
28 : jhr 3835 (* associate Tree IL globals variables with Low IL variables using properties *)
29 :     local
30 :     val {setFn, peekFn, ...} =
31 :     GV.newProp (fn x => raise Fail(concat["getGlobalVar(", GV.uniqueName x, ")"]))
32 :     in
33 :     fun mkGlobalVar x = (case peekFn x
34 :     of NONE => let
35 :     val x' = TGV.new {
36 :     name = GV.name x,
37 : jhr 3843 ty = Util.trType (GV.ty x),
38 : jhr 3835 input = GV.isInput x,
39 :     output = false, (* FIXME: change once we support output globals *)
40 : jhr 3843 varying = GV.isVarying x,
41 :     apiTy = if GV.isInput x
42 :     then SOME(Util.toAPIType (GV.ty x))
43 :     else NONE
44 : jhr 3835 }
45 :     in
46 :     setFn (x, x');
47 :     x'
48 :     end
49 :     | SOME x' => x'
50 :     (* end case *))
51 :     end
52 :    
53 :     (* associate Tree IL state variables with Low IL variables using properties *)
54 :     local
55 :     fun mkStateVar x = TSV.new {
56 :     name = SV.name x,
57 : jhr 3843 ty = Util.trType (SV.ty x),
58 :     varying = SV.isVarying x,
59 :     apiTy = if (SV.isOutput x)
60 :     then SOME(Util.toAPIType (SV.ty x))
61 :     else NONE
62 : jhr 3835 }
63 :     in
64 :     val {getFn = getStateVar, ...} = SV.newProp mkStateVar
65 :     end
66 :    
67 : jhr 3848 fun mkBlock stms = T.Block{locals = ref [], body = stms}
68 : jhr 3834 fun mkIf (x, stms, []) = T.S_IfThen(x, mkBlock stms)
69 :     | mkIf (x, stms1, stms2) = T.S_IfThenElse(x, mkBlock stms1, mkBlock stms2)
70 : jhr 3828
71 : jhr 3834 fun cvtScalarTy Ty.BoolTy = TTy.BoolTy
72 :     | cvtScalarTy Ty.IntTy = TTy.IntTy
73 : jhr 3835 | cvtScalarTy (Ty.TensorTy[]) = TTy.realTy
74 : jhr 3834 | cvtScalarTy ty = raise Fail(concat["cvtScalarTy(", Ty.toString ty, ")"])
75 :    
76 : jhr 3852 (* get a variable's binding as a single argument expression. This means that
77 :     * if x is bound to a vector of expressions, then we need to pack it.
78 :     *)
79 : jhr 3850 fun singleArg env x = (case Env.useVar env x
80 :     of Env.TREE e => e
81 :     | Env.VEC(layout, es) => T.E_Pack(layout, es)
82 :     (* end case *))
83 :    
84 : jhr 3852 fun scalarArg env x = (case Env.useVar env x
85 :     of Env.TREE e => e
86 :     | _ => raise Fail("expected scalar binding for " ^ IR.Var.toString x)
87 :     (* end case *))
88 :    
89 : jhr 3834 fun vectorArg (env, x) = (case Env.useVar env x
90 : jhr 3843 of Env.TREE e => (case IR.Var.ty x
91 :     of Ty.TensorTy[d] => let
92 :     val layout = Env.layoutVec env d
93 :     (* QUESTION: can "e" be a complicated expression or are we guaranteed that it will just
94 :     * be a memory reference?
95 :     *)
96 :     val es = List.tabulate (
97 :     List.length(#pieces layout),
98 :     fn i => T.E_VLoad(layout, e, i))
99 :     in
100 :     (layout, es)
101 :     end
102 :     | ty => raise Fail("expected TensorTy[_], but found " ^ Ty.toString ty)
103 :     (* end case *))
104 : jhr 3834 | Env.VEC(layout, es) => (layout, es)
105 :     (* end case *))
106 :    
107 :     (* convert a list of LowIR variables, each of which are mapped
108 :     * to lists of of vector expressions, to a list of list of expressions
109 :     *)
110 :     fun vectorArgs (env, []) = raise Fail "unexpected empty argument list"
111 :     | vectorArgs (env, x::xs) = let
112 :     val (layout, exps) = vectorArg (env, x)
113 :     fun doArg (x, argLists) = let
114 :     val (_, exps) = vectorArg (env, x)
115 :     in
116 :     ListPair.mapEq (op ::) (exps, argLists)
117 :     end
118 : jhr 3843 val argLists = List.foldl doArg [exps] xs
119 : jhr 3834 in
120 :     (layout, List.map List.rev argLists)
121 :     end
122 :    
123 : jhr 3852 fun trOp (env, srcRator, args) = let
124 : jhr 3851 (* FIXME: if the use count of lhs is > 1, then we should bind to a local variable *)
125 : jhr 3852 fun bindOp rator = Env.TREE(T.E_Op(rator, List.map (scalarArg env) args))
126 : jhr 3835 fun bindVOp rator = let
127 : jhr 3834 val (layout, argss) = vectorArgs (env, args)
128 : jhr 3829 val exps = ListPair.map
129 : jhr 3835 (fn (w, args) => T.E_Op(rator w, args))
130 : jhr 3834 (#pieces layout, argss)
131 : jhr 3829 in
132 : jhr 3852 Env.VEC(layout, exps)
133 : jhr 3829 end
134 :     in
135 : jhr 3834 case srcRator
136 :     of Op.IAdd => bindOp TOp.IAdd
137 :     | Op.ISub => bindOp TOp.ISub
138 :     | Op.IMul => bindOp TOp.IMul
139 :     | Op.IDiv => bindOp TOp.IDiv
140 :     | Op.IMod => bindOp TOp.IMod
141 :     | Op.INeg => bindOp TOp.INeg
142 : jhr 3843 (* QUESTION: should we just use VAdd 1, etc ?*)
143 : jhr 3834 | Op.RAdd => bindOp TOp.RAdd
144 :     | Op.RSub => bindOp TOp.RSub
145 :     | Op.RMul => bindOp TOp.RMul
146 :     | Op.RDiv => bindOp TOp.RDiv
147 :     | Op.RNeg => bindOp TOp.RNeg
148 :     | Op.LT ty => bindOp (TOp.LT (cvtScalarTy ty))
149 :     | Op.LTE ty => bindOp (TOp.LTE (cvtScalarTy ty))
150 :     | Op.EQ ty => bindOp (TOp.EQ (cvtScalarTy ty))
151 :     | Op.NEQ ty => bindOp (TOp.NEQ (cvtScalarTy ty))
152 :     | Op.GT ty => bindOp (TOp.GT (cvtScalarTy ty))
153 :     | Op.GTE ty => bindOp (TOp.GTE (cvtScalarTy ty))
154 :     | Op.Not => bindOp TOp.Not
155 :     | Op.Abs ty => bindOp (TOp.Abs (cvtScalarTy ty))
156 :     | Op.Max ty => bindOp (TOp.Max (cvtScalarTy ty))
157 :     | Op.Min ty => bindOp (TOp.Min (cvtScalarTy ty))
158 :     | Op.RClamp => bindOp TOp.RClamp
159 :     | Op.RLerp => bindOp TOp.RLerp
160 :     | Op.VAdd _ => bindVOp TOp.VAdd
161 :     | Op.VSub _ => bindVOp TOp.VSub
162 :     | Op.VScale _ => bindVOp TOp.VScale
163 :     | Op.VMul _ => bindVOp TOp.VMul
164 :     | Op.VNeg _ => bindVOp TOp.VNeg
165 : jhr 3852 | Op.VSum _ => raise Fail "FIXME: VSum"
166 : jhr 3844 | Op.VIndex(_, i) => let
167 :     (* FIXME: more efficient to lookup the variable and avoid expanding TREE args *)
168 :     val ({pieces, ...}, es) = vectorArg (env, hd args)
169 :     fun select (i, w::ws, e::es) =
170 :     if (i < w)
171 : jhr 3851 (* FIXME: what if lhs is used more than once? *)
172 : jhr 3852 then Env.TREE(T.E_Op(TOp.VIndex(w, i), [e]))
173 : jhr 3844 else select (i-w, ws, es)
174 :     | select _ = raise Fail("bogus " ^ Op.toString srcRator)
175 :     in
176 : jhr 3852 select (i, pieces, es)
177 : jhr 3844 end
178 : jhr 3834 | Op.VClamp n => bindVOp TOp.VClamp
179 :     | Op.VMapClamp n => bindVOp TOp.VMapClamp
180 :     | Op.VLerp n => bindVOp TOp.VLerp
181 :     | Op.TensorIndex(ty, idxs) => ??
182 :     | Op.ProjectLast(ty, idxs) => ??
183 :     | Op.Zero ty => ??
184 :     | Op.Select(Ty.TupleTy tys, i) => ??
185 :     | Op.Subscript(Ty.SeqTy(ty, NONE)) => ??
186 :     | Op.Subscript(Ty.SeqTy(ty, SOME _)) => ??
187 :     | Op.MkDynamic(ty, n) => ??
188 :     | Op.Append ty => ??
189 :     | Op.Prepend ty => ??
190 :     | Op.Concat ty => ??
191 :     | Op.Range => ??
192 :     | Op.Length ty => ??
193 : jhr 3852 | Op.SphereQuery(ty1, ty2) => raise Fail "FIXME: SphereQuery"
194 : jhr 3834 | Op.Sqrt => bindOp TOp.Sqrt
195 :     | Op.Cos => bindOp TOp.Cos
196 :     | Op.ArcCos => bindOp TOp.ArcCos
197 :     | Op.Sin => bindOp TOp.Sin
198 :     | Op.ArcSin => bindOp TOp.ArcSin
199 :     | Op.Tan => bindOp TOp.Tan
200 :     | Op.ArcTan => bindOp TOp.ArcTan
201 :     | Op.Ceiling 1 => bindOp (TOp.Ceiling 1)
202 : jhr 3851 | Op.Ceiling d => bindVOp TOp.Ceiling
203 : jhr 3834 | Op.Floor 1 => bindOp (TOp.Floor 1)
204 : jhr 3851 | Op.Floor d => bindVOp TOp.Floor
205 :     | Op.Round 1 => bindOp (TOp.Round 1)
206 :     | Op.Round d => bindVOp TOp.Round
207 : jhr 3834 | Op.Trunc 1 => bindOp (TOp.Trunc 1)
208 : jhr 3851 | Op.Trunc d => bindVOp TOp.Trunc
209 : jhr 3835 | Op.IntToReal => bindOp TOp.IntToReal
210 : jhr 3851 | Op.RealToInt 1 => bindOp (TOp.RealToInt 1)
211 : jhr 3834 | Op.RealToInt d => ??
212 : jhr 3844 (* FIXME
213 : jhr 3834 | Op.R_All ty => ??
214 :     | Op.R_Exists ty => ??
215 :     | Op.R_Max ty => ??
216 :     | Op.R_Min ty => ??
217 :     | Op.R_Sum ty => ??
218 :     | Op.R_Product ty => ??
219 :     | Op.R_Mean ty => ??
220 :     | Op.R_Variance ty => ??
221 : jhr 3844 *)
222 : jhr 3851 | Op.Transform info => bindOp (TOp.Transform info)
223 :     | Op.Translate info => bindOp (TOp.Translate info)
224 :     | Op.ControlIndex(info, ctl, d) => bindOp (TOp.ControlIndex(info, ctl, d))
225 :     | Op.LoadVoxel info => bindOp (TOp.LoadVoxel info)
226 : jhr 3852 | Op.Inside(info, s) => bindOp (TOp.Inside(info, s))
227 : jhr 3835 | Op.ImageDim(info, d) => bindOp(TOp.ImageDim(info, d))
228 : jhr 3834 | Op.LoadSeq(ty, file) => ??
229 :     | Op.LoadImage(ty, file) => ??
230 :     | Op.MathFn f => bindOp (TOp.MathFn f)
231 : jhr 3844 | rator => raise Fail("bogus operator " ^ Op.toString srcRator)
232 : jhr 3829 (* end case *)
233 :     end
234 :    
235 : jhr 3852 (* cases:
236 :     x in EqClass
237 :     issue assignment; lhs is binding of representative (could be multiple vars)
238 :     useCount(x) > 1 and rhs is not simple
239 :     rhs is simple
240 :     rhs is vector
241 :     *)
242 : jhr 3834 fun trAssign (env, lhs, rhs) = let
243 : jhr 3836 (* simple binding for lhs variable; we check to see if it is part of an merged
244 :     * equivalence class, in which case we need to generate the assigment.
245 :     *)
246 :     fun bindSimple rhs = (case UnifyVars.eqClassOf lhs
247 : jhr 3852 of SOME x => [T.S_Assign(x', rhs)]
248 :     | NONE => (Env.bindSimple (env, lhs, Env.TREE rhs); [])
249 : jhr 3836 (* end case *))
250 : jhr 3852 fun assignOp (rator, args) = let
251 :     (* FIXME: what if lhs is EqClass var? *)
252 :     val t = Util.newLocalVar lhs
253 :     val stm = T.S_Assign(t, T.E_Op(rator, List.map (scalarArg env) args))
254 :     in
255 :     Env.bindSimple (env, lhs, Env.TREE(T.E_Var t));
256 :     [stm]
257 :     end
258 : jhr 3834 in
259 :     case rhs
260 : jhr 3844 of IR.GLOBAL x => bindSimple (T.E_Global(mkGlobalVar x))
261 : jhr 3852 | IR.STATE(NONE, fld) =>
262 :     bindSimple (T.E_State(NONE, getStateVar fld))
263 :     | IR.STATE(SOME x, fld) =>
264 :     bindSimple (T.E_State(SOME(scalarArg env x), getStateVar fld))
265 : jhr 3836 | IR.VAR x => (case Env.useVar env x
266 :     of Env.TREE e => ??
267 :     | Env.VEC(layout, es) => ??
268 :     (* end case *))
269 : jhr 3850 | IR.LIT lit => bindSimple (T.E_Lit lit)
270 : jhr 3852 | IR.OP(Op.EigenVecs2x2, args) => assignOp (TOp.EigenVecs2x2, args)
271 :     | IR.OP(Op.EigenVecs3x3, args) => assignOp (TOp.EigenVecs3x3, args)
272 :     | IR.OP(Op.EigenVals2x2, args) => assignOp (TOp.EigenVals2x2, args)
273 :     | IR.OP(Op.EigenVals3x3, args) => assignOp (TOp.EigenVals3x3, args)
274 :     | IR.OP(rator, args) => let
275 :     val rhs = trOp (env, rator, args)
276 :     in
277 :     if Env.isInlineOp env rator
278 :     then (case (rhs, UnifyVars.eqClassOf lhs, useCount lhs > 1)
279 :     of (_, NONE, false) => (Env.bindSimple (env, lhs, rhs); [])
280 :     | (Env.TREE e, NONE, true) => let
281 :     val t = Util.newLocalVar lhs
282 :     in
283 :     Env.bindSimple (env, lhs, Env.TREE(T.E_Var t));
284 :     [T.S_Assign(t, e)]
285 :     end
286 :     | (Env.TREE e, SOME x, _) => [T.S_Assign(x', e)]
287 :     | (Env.VEC(layout, es), NONE, true) => let
288 :     val vs = Util.newVectorVars layout
289 :     in
290 :     Env.bindSimple (env, lhs, Env.VEC(layout, List.map T.E_Var vs));
291 :     ListPair.mapEq T.S_Assign (vs, es)
292 :     end
293 :     | (Env.VEC(layout, es), SOME x, _) => ??
294 :     (* end case *))
295 :     else ??
296 :     end
297 :     | IR.CONS(args, Ty.TensorTy[d]) => let
298 :     val {padded, pieces, ...} = Env.layoutVec env d
299 :     in
300 :     ??
301 :     end
302 :     | IR.CONS(args, Ty.TensorTy shape) => ??
303 : jhr 3834 | IR.SEQ(args, ty) => ??
304 : jhr 3852 | rhs => raise Fail(concat["unexpected ", IR.RHS.toString rhs, " in LowIR code"])
305 : jhr 3834 (* end case *)
306 :     end
307 :    
308 :     (* In order to reconstruct the block-structure from the CFG, we keep a stack of open ifs.
309 :     * the items on this stack distinguish between when we are processing the then and else
310 :     * branches of the if.
311 :     *)
312 :     datatype open_if
313 :     (* working on the "then" branch. The fields are statments that preceed the if, the condition,
314 :     * and the else-branch node.
315 :     *)
316 :     = THEN_BR of T.stm list * T.exp * IR.node
317 :     (* working on the "else" branch. The fields are statments that preceed the if, the condition,
318 :     * the "then" branch statements, and the node kind that terminated the "then" branch (will be
319 :     * a JOIN or EXIT(DIE, STABILIZE, or UNREACHABLE)).
320 :     *)
321 :     | ELSE_BR of T.stm list * T.exp * T.stm list * IR.node_kind
322 :    
323 : jhr 3852 fun trCFGWithEnv (env, cfg) = let
324 : jhr 3835 fun useScalar x = (case Env.useVar env x
325 :     of Env.TREE e => e
326 :     | _ => raise Fail("expected scalar binding for " ^ V.toString x)
327 :     (* end case *))
328 : jhr 3834 val _ = UnifyVars.analyze cfg
329 : jhr 3851 (* join (stk, stms, k): handle a control-flow join, where env is the
330 : jhr 3834 * current environment, stk is the stack of open ifs (the top of stk specifies
331 :     * which branch we are in), stms are the TreeIL statements preceding the join
332 :     * on the current path, and k is the kind of the join node (either JOIN or EXIT).
333 :     *)
334 : jhr 3851 fun join ([], _, IR.JOIN _) = raise Fail "JOIN with no open if"
335 : jhr 3852 | join ([], stms, _) = mkBlock (List.rev stms)
336 : jhr 3851 | join (THEN_BR(stms1, cond, elseBr)::stk, thenBlk, k) = let
337 :     val thenBlk = Env.flushPending (env, thenBlk)
338 : jhr 3834 in
339 : jhr 3851 doNode (elseBr, ELSE_BR(stms1, cond, thenBlk, k)::stk, [])
340 : jhr 3834 end
341 : jhr 3851 | join (ELSE_BR(stms, cond, thenBlk, k1)::stk, elseBlk, k2) = let
342 :     val elseBlk = Env.flushPending (env, elseBlk)
343 : jhr 3834 in
344 :     case (k1, k2)
345 : jhr 3848 of (IR.JOIN{succ, ...}, IR.JOIN _) => let
346 : jhr 3834 val stm = mkIf(cond, List.rev thenBlk, List.rev elseBlk)
347 :     in
348 : jhr 3851 doNode (!succ, stk, stm::stms)
349 : jhr 3834 end
350 : jhr 3848 | (IR.JOIN{succ, ...}, _) => let
351 : jhr 3834 val stm = mkIf(cond, List.rev thenBlk, List.rev elseBlk)
352 :     in
353 : jhr 3851 doNode (!succ, stk, stm::stms)
354 : jhr 3834 end
355 : jhr 3848 | (_, IR.JOIN{succ, ...}) => let
356 : jhr 3834 val stm = mkIf(cond, List.rev thenBlk, List.rev elseBlk)
357 :     in
358 : jhr 3851 doNode (!succ, stk, stm::stms)
359 : jhr 3834 end
360 :     | (_, _) => let
361 :     val stm = mkIf(cond, List.rev thenBlk, List.rev elseBlk)
362 :     in
363 : jhr 3852 mkBlock (List.rev(stm::stms))
364 : jhr 3834 end
365 :     (* end case *)
366 :     end
367 : jhr 3851 and doNode (nd : IR.node, ifStk : open_if list, stms) = (case IR.Node.kind nd
368 : jhr 3834 of IR.NULL => raise Fail "unexpected NULL"
369 : jhr 3851 | IR.ENTRY{succ} => doNode (!succ, ifStk, stms)
370 :     | k as IR.JOIN _ => join (ifStk, stms, k)
371 : jhr 3834 | IR.COND{cond, trueBranch, falseBranch, ...} => let
372 : jhr 3835 val cond = useScalar (!cond)
373 : jhr 3851 val stms = Env.flushPending (env, stms)
374 : jhr 3834 in
375 : jhr 3851 doNode (!trueBranch, THEN_BR(stms, cond, !falseBranch)::ifStk, [])
376 : jhr 3834 end
377 : jhr 3835 | IR.FOREACH{var, src, bodyEntry, succ, ...} => let
378 :     val src = useScalar (!src)
379 : jhr 3844 val var = Util.newIterVar var
380 : jhr 3851 val stms = Env.flushPending (env, stms)
381 :     val body = doNode (!bodyEntry, [], [])
382 : jhr 3834 val stm = T.S_Foreach(var, src, body)
383 :     in
384 : jhr 3851 doNode (!succ, ifStk, stm::stms)
385 : jhr 3834 end
386 : jhr 3848 | IR.NEXT _ => mkBlock (List.rev stms)
387 : jhr 3834 | IR.COM {text, succ, ...} =>
388 : jhr 3851 doNode (!succ, ifStk, T.S_Comment text :: stms)
389 : jhr 3834 | IR.ASSIGN{stm=(lhs, rhs), succ, ...} => let
390 : jhr 3852 val stms' = trAssign (env, lhs, rhs)
391 : jhr 3834 in
392 : jhr 3851 doNode (!succ, ifStk, stms' @ stms)
393 : jhr 3834 end
394 :     | IR.MASSIGN{stm=(ys, rator, xs), succ, ...} => let
395 : jhr 3852 fun doLHSVar (y, ys) = let
396 : jhr 3848 val t = Util.newLocalVar y
397 :     in
398 : jhr 3852 Env.bindSimple (env, y, Env.TREE(T.E_Var t));
399 :     t::ys
400 : jhr 3848 end
401 : jhr 3852 val ys = List.foldr doLHSVar [] ys
402 : jhr 3848 val rator = (case rator
403 :     of Op.Print tys => TOp.Print(List.map Util.trType tys)
404 :     | _ => raise Fail(concat[
405 :     "unexepected operator ", Op.toString rator, " for MASSIGN"
406 :     ])
407 :     (* end case *))
408 : jhr 3850 val stm = T.S_MAssign(ys, T.E_Op(rator, List.map (singleArg env) xs))
409 : jhr 3848 in
410 : jhr 3851 doNode (!succ, ifStk, stm :: stms)
411 : jhr 3848 end
412 : jhr 3834 | IR.GASSIGN{lhs, rhs, succ, ...} => let
413 : jhr 3852 val stm = T.S_GAssign(mkGlobalVar lhs, singleArg env rhs)
414 : jhr 3834 in
415 : jhr 3851 doNode (!succ, ifStk, stm::stms)
416 : jhr 3834 end
417 : jhr 3851 | IR.NEW{strand, args, succ, ...} => let
418 :     val stm = T.S_New(strand, List.map (singleArg env) args)
419 :     in
420 :     doNode (!succ, ifStk, stm::stms)
421 :     end
422 : jhr 3834 | IR.SAVE{lhs, rhs, succ, ...} => let
423 : jhr 3850 val stm = T.S_Save(getStateVar lhs, singleArg env rhs)
424 : jhr 3834 in
425 : jhr 3851 doNode (!succ, ifStk, stm::stms)
426 : jhr 3834 end
427 :     | k as IR.EXIT{kind, succ, ...} => (case (!succ, kind)
428 : jhr 3852 of (NONE, ExitKind.RETURN) => mkBlock (List.rev(T.S_Exit :: stms))
429 :     | (NONE, ExitKind.ACTIVE) => mkBlock (List.rev(T.S_Active :: stms))
430 : jhr 3834 | (NONE, ExitKind.STABILIZE) => let
431 :     val stms = T.S_Stabilize :: stms
432 :     in
433 : jhr 3851 join (ifStk, stms, k)
434 : jhr 3834 end
435 : jhr 3851 | (NONE, ExitKind.DIE) => join (ifStk, T.S_Die :: stms, k)
436 :     | (NONE, ExitKind.UNREACHABLE) => join (ifStk, stms, k)
437 :     | (SOME nd, ExitKind.ACTIVE) => doNode (nd, ifStk, T.S_Active :: stms)
438 :     | (SOME nd, ExitKind.STABILIZE) => doNode (nd, ifStk, T.S_Stabilize :: stms)
439 :     | (SOME nd, ExitKind.DIE) => doNode (nd, ifStk, T.S_Die :: stms)
440 :     | (SOME nd, ExitKind.UNREACHABLE) => doNode (nd, ifStk, stms)
441 : jhr 3834 | _ => raise Fail("unexpected continuation edge from "^IR.Node.toString nd)
442 :     (* end case *))
443 :     (* end case *))
444 :     in
445 : jhr 3851 ScopeVars.assignScopes (doNode (IR.CFG.entry cfg, [], []))
446 : jhr 3834 end
447 :    
448 : jhr 3852 fun trCFG info cfg = trCFGWithEnv (Env.new info, cfg)
449 : jhr 3835
450 : jhr 3851 fun trStrand info strand = let
451 :     val trCFG = trCFG info
452 : jhr 3835 val IR.Strand{name, params, state, stateInit, initM, updateM, stabilizeM} = strand
453 : jhr 3844 val params' = List.map Util.newParamVar params
454 : jhr 3852 val env = Env.new info
455 :     val () = ListPair.appEq
456 :     (fn (x, x') => Env.bindSimple (env, x, Env.TREE(T.E_Var x'))) (params, params')
457 : jhr 3835 val state' = List.map getStateVar state
458 :     in
459 :     T.Strand{
460 :     name = name,
461 :     params = params',
462 :     state = state',
463 : jhr 3852 stateInit = trCFGWithEnv (env, stateInit),
464 :     initM = Option.map trCFG initM,
465 :     updateM = trCFG updateM,
466 :     stabilizeM = Option.map trCFG stabilizeM
467 : jhr 3835 }
468 :     end
469 :    
470 : jhr 3851 fun translate (prog, info) = let
471 : jhr 3834 val LowIR.Program{
472 :     props, consts, inputs, constInit, globals, globalInit, strand, create, update
473 :     } = prog
474 : jhr 3852 val trCFG = trCFG info
475 : jhr 3834 in
476 :     TreeIR.Program{
477 :     props = props,
478 : jhr 3852 consts = List.map mkGlobalVar consts,
479 :     inputs = List.map (Inputs.map mkGlobalVar) inputs,
480 :     constInit = trCFG constInit,
481 :     globals = List.map mkGlobalVar globals,
482 :     globalInit = trCFG globalInit,
483 : jhr 3851 strand = trStrand info strand,
484 : jhr 3852 create = let
485 :     val IR.Create{dim, code} = create
486 :     in
487 :     T.Create{dim = dim, code = trCFG code}
488 :     end,
489 :     update = Option.map trCFG update
490 : jhr 3834 }
491 :     end
492 :    
493 : jhr 3828 end

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