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

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