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

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