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

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