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

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