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

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