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

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