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 3879 - (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 3862 fun take (0, args, es) = T.E_Vec(w, List.rev es) :: mkVecs (args, ws)
375 : jhr 3855 | take (i, [], es) = if #padded layout andalso null ws
376 : jhr 3862 then [T.E_Vec(w, List.rev es)]
377 : jhr 3855 else raise Fail "too few arguments for CONS"
378 : jhr 3854 | take (i, arg::args, es) = take (i-1, args, scalarArg env arg :: es)
379 :     in
380 :     take (w, args, [])
381 :     end
382 :     | mkVecs ([], []) = []
383 :     | mkVecs (_, []) = raise Fail "too many arguments for CONS"
384 :     val es = mkVecs (args, #pieces layout)
385 : jhr 3852 in
386 : jhr 3854 case (eqClassRepOf(env, lhs), V.useCount lhs > 1)
387 :     of (NOEQ, false) => (Env.bindVar(env, lhs, Env.VEC(layout, es)); [])
388 :     | (NOEQ, true) => let
389 :     val vs = Util.newVectorVars layout
390 :     in
391 :     Env.bindSimple (env, lhs, Env.VEC(layout, List.map T.E_Var vs));
392 : jhr 3857 ListPair.mapEq mkDefn (vs, es)
393 : jhr 3854 end
394 : jhr 3857 | (VEC xs, _) => ListPair.mapEq mkAssign (xs, es)
395 : jhr 3854 | _ => raise Fail "inconsistent"
396 :     (* end case *)
397 : jhr 3852 end
398 : jhr 3858 | IR.CONS(args, ty) => [
399 :     mkDefn (
400 :     newLocal(env, lhs),
401 :     T.E_Cons(List.map (singleArg env) args, Util.trType ty))
402 :     ]
403 :     | IR.SEQ(args, ty) => [
404 :     mkDefn (
405 :     newLocal(env, lhs),
406 :     T.E_Seq(List.map (singleArg env) args, Util.trType ty))
407 :     ]
408 : jhr 3852 | rhs => raise Fail(concat["unexpected ", IR.RHS.toString rhs, " in LowIR code"])
409 : jhr 3834 (* end case *)
410 :     end
411 : jhr 3856 handle ex => (
412 :     print(concat["trAssign: ", V.toString lhs, " = ", IR.RHS.toString rhs, "\n"]);
413 :     raise ex)
414 : jhr 3834
415 :     (* In order to reconstruct the block-structure from the CFG, we keep a stack of open ifs.
416 :     * the items on this stack distinguish between when we are processing the then and else
417 :     * branches of the if.
418 :     *)
419 :     datatype open_if
420 :     (* working on the "then" branch. The fields are statments that preceed the if, the condition,
421 :     * and the else-branch node.
422 :     *)
423 :     = THEN_BR of T.stm list * T.exp * IR.node
424 :     (* working on the "else" branch. The fields are statments that preceed the if, the condition,
425 :     * the "then" branch statements, and the node kind that terminated the "then" branch (will be
426 :     * a JOIN or EXIT(DIE, STABILIZE, or UNREACHABLE)).
427 :     *)
428 :     | ELSE_BR of T.stm list * T.exp * T.stm list * IR.node_kind
429 :    
430 : jhr 3852 fun trCFGWithEnv (env, cfg) = let
431 : jhr 3856 fun useScalar x = (case useVar env x
432 : jhr 3835 of Env.TREE e => e
433 :     | _ => raise Fail("expected scalar binding for " ^ V.toString x)
434 :     (* end case *))
435 : jhr 3834 val _ = UnifyVars.analyze cfg
436 : jhr 3851 (* join (stk, stms, k): handle a control-flow join, where env is the
437 : jhr 3834 * current environment, stk is the stack of open ifs (the top of stk specifies
438 :     * which branch we are in), stms are the TreeIL statements preceding the join
439 :     * on the current path, and k is the kind of the join node (either JOIN or EXIT).
440 :     *)
441 : jhr 3851 fun join ([], _, IR.JOIN _) = raise Fail "JOIN with no open if"
442 : jhr 3852 | join ([], stms, _) = mkBlock (List.rev stms)
443 : jhr 3851 | join (THEN_BR(stms1, cond, elseBr)::stk, thenBlk, k) = let
444 :     val thenBlk = Env.flushPending (env, thenBlk)
445 : jhr 3834 in
446 : jhr 3851 doNode (elseBr, ELSE_BR(stms1, cond, thenBlk, k)::stk, [])
447 : jhr 3834 end
448 : jhr 3851 | join (ELSE_BR(stms, cond, thenBlk, k1)::stk, elseBlk, k2) = let
449 :     val elseBlk = Env.flushPending (env, elseBlk)
450 : jhr 3834 in
451 :     case (k1, k2)
452 : jhr 3848 of (IR.JOIN{succ, ...}, IR.JOIN _) => 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 : jhr 3848 | (_, IR.JOIN{succ, ...}) => let
463 : jhr 3834 val stm = mkIf(cond, List.rev thenBlk, List.rev elseBlk)
464 :     in
465 : jhr 3851 doNode (!succ, stk, stm::stms)
466 : jhr 3834 end
467 :     | (_, _) => let
468 :     val stm = mkIf(cond, List.rev thenBlk, List.rev elseBlk)
469 :     in
470 : jhr 3852 mkBlock (List.rev(stm::stms))
471 : jhr 3834 end
472 :     (* end case *)
473 :     end
474 : jhr 3851 and doNode (nd : IR.node, ifStk : open_if list, stms) = (case IR.Node.kind nd
475 : jhr 3834 of IR.NULL => raise Fail "unexpected NULL"
476 : jhr 3851 | IR.ENTRY{succ} => doNode (!succ, ifStk, stms)
477 :     | k as IR.JOIN _ => join (ifStk, stms, k)
478 : jhr 3834 | IR.COND{cond, trueBranch, falseBranch, ...} => let
479 : jhr 3835 val cond = useScalar (!cond)
480 : jhr 3851 val stms = Env.flushPending (env, stms)
481 : jhr 3834 in
482 : jhr 3851 doNode (!trueBranch, THEN_BR(stms, cond, !falseBranch)::ifStk, [])
483 : jhr 3834 end
484 : jhr 3835 | IR.FOREACH{var, src, bodyEntry, succ, ...} => let
485 :     val src = useScalar (!src)
486 : jhr 3856 val var' = Util.newIterVar var
487 : jhr 3851 val stms = Env.flushPending (env, stms)
488 : jhr 3856 val _ = Env.bindSimple (env, var, Env.TREE(T.E_Var var'))
489 : jhr 3851 val body = doNode (!bodyEntry, [], [])
490 : jhr 3856 val stm = T.S_Foreach(var', src, body)
491 : jhr 3834 in
492 : jhr 3851 doNode (!succ, ifStk, stm::stms)
493 : jhr 3834 end
494 : jhr 3848 | IR.NEXT _ => mkBlock (List.rev stms)
495 : jhr 3834 | IR.COM {text, succ, ...} =>
496 : jhr 3851 doNode (!succ, ifStk, T.S_Comment text :: stms)
497 : jhr 3834 | IR.ASSIGN{stm=(lhs, rhs), succ, ...} => let
498 : jhr 3852 val stms' = trAssign (env, lhs, rhs)
499 : jhr 3834 in
500 : jhr 3851 doNode (!succ, ifStk, stms' @ stms)
501 : jhr 3834 end
502 : jhr 3866 | IR.MASSIGN{stm=([], Op.Print tys, xs), succ, ...} => let
503 :     val stm = T.S_Print(List.map Util.trType tys, List.map (singleArg env) xs)
504 : jhr 3848 in
505 : jhr 3851 doNode (!succ, ifStk, stm :: stms)
506 : jhr 3848 end
507 : jhr 3866 | IR.MASSIGN{stm=(ys, rator, xs), succ, ...} => raise Fail(concat[
508 :     "unexepected operator ", Op.toString rator, " for MASSIGN"
509 :     ])
510 : jhr 3834 | IR.GASSIGN{lhs, rhs, succ, ...} => let
511 : jhr 3852 val stm = T.S_GAssign(mkGlobalVar lhs, singleArg env rhs)
512 : jhr 3834 in
513 : jhr 3851 doNode (!succ, ifStk, stm::stms)
514 : jhr 3834 end
515 : jhr 3851 | IR.NEW{strand, args, succ, ...} => let
516 :     val stm = T.S_New(strand, List.map (singleArg env) args)
517 :     in
518 :     doNode (!succ, ifStk, stm::stms)
519 :     end
520 : jhr 3834 | IR.SAVE{lhs, rhs, succ, ...} => let
521 : jhr 3850 val stm = T.S_Save(getStateVar lhs, singleArg env rhs)
522 : jhr 3834 in
523 : jhr 3851 doNode (!succ, ifStk, stm::stms)
524 : jhr 3834 end
525 :     | k as IR.EXIT{kind, succ, ...} => (case (!succ, kind)
526 : jhr 3852 of (NONE, ExitKind.RETURN) => mkBlock (List.rev(T.S_Exit :: stms))
527 :     | (NONE, ExitKind.ACTIVE) => mkBlock (List.rev(T.S_Active :: stms))
528 : jhr 3834 | (NONE, ExitKind.STABILIZE) => let
529 :     val stms = T.S_Stabilize :: stms
530 :     in
531 : jhr 3851 join (ifStk, stms, k)
532 : jhr 3834 end
533 : jhr 3851 | (NONE, ExitKind.DIE) => join (ifStk, T.S_Die :: stms, k)
534 :     | (NONE, ExitKind.UNREACHABLE) => join (ifStk, stms, k)
535 :     | (SOME nd, ExitKind.ACTIVE) => doNode (nd, ifStk, T.S_Active :: stms)
536 :     | (SOME nd, ExitKind.STABILIZE) => doNode (nd, ifStk, T.S_Stabilize :: stms)
537 :     | (SOME nd, ExitKind.DIE) => doNode (nd, ifStk, T.S_Die :: stms)
538 :     | (SOME nd, ExitKind.UNREACHABLE) => doNode (nd, ifStk, stms)
539 : jhr 3834 | _ => raise Fail("unexpected continuation edge from "^IR.Node.toString nd)
540 :     (* end case *))
541 :     (* end case *))
542 :     in
543 : jhr 3857 doNode (IR.CFG.entry cfg, [], [])
544 : jhr 3834 end
545 :    
546 : jhr 3857 fun trCFG info cfg = ScopeVars.assignScopes ([], trCFGWithEnv (Env.new info, cfg))
547 : jhr 3835
548 : jhr 3851 fun trStrand info strand = let
549 :     val trCFG = trCFG info
550 : jhr 3835 val IR.Strand{name, params, state, stateInit, initM, updateM, stabilizeM} = strand
551 : jhr 3844 val params' = List.map Util.newParamVar params
552 : jhr 3835 val state' = List.map getStateVar state
553 : jhr 3857 val stateInit' = let
554 :     val env = Env.new info
555 :     in
556 :     ListPair.appEq
557 :     (fn (x, x') => Env.bindSimple (env, x, Env.TREE(T.E_Var x')))
558 :     (params, params');
559 :     ScopeVars.assignScopes (params', trCFGWithEnv (env, stateInit))
560 :     end
561 : jhr 3835 in
562 :     T.Strand{
563 :     name = name,
564 :     params = params',
565 :     state = state',
566 : jhr 3857 stateInit = stateInit',
567 : jhr 3852 initM = Option.map trCFG initM,
568 :     updateM = trCFG updateM,
569 :     stabilizeM = Option.map trCFG stabilizeM
570 : jhr 3835 }
571 :     end
572 :    
573 : jhr 3851 fun translate (prog, info) = let
574 : jhr 3879 (* first step is to flatten any nested CONS nodes *)
575 :     val prog = Flatten.transform prog
576 : jhr 3834 val LowIR.Program{
577 :     props, consts, inputs, constInit, globals, globalInit, strand, create, update
578 :     } = prog
579 : jhr 3852 val trCFG = trCFG info
580 : jhr 3834 in
581 :     TreeIR.Program{
582 :     props = props,
583 : jhr 3861 target = info,
584 : jhr 3852 consts = List.map mkGlobalVar consts,
585 :     inputs = List.map (Inputs.map mkGlobalVar) inputs,
586 :     constInit = trCFG constInit,
587 :     globals = List.map mkGlobalVar globals,
588 :     globalInit = trCFG globalInit,
589 : jhr 3851 strand = trStrand info strand,
590 : jhr 3852 create = let
591 :     val IR.Create{dim, code} = create
592 :     in
593 :     T.Create{dim = dim, code = trCFG code}
594 :     end,
595 :     update = Option.map trCFG update
596 : jhr 3834 }
597 :     end
598 :    
599 : jhr 3828 end

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