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 4151 - (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 3948 structure U = Util
28 : jhr 3828
29 : jhr 3835 (* associate Tree IL globals variables with Low IL variables using properties *)
30 :     local
31 :     val {setFn, peekFn, ...} =
32 :     GV.newProp (fn x => raise Fail(concat["getGlobalVar(", GV.uniqueName x, ")"]))
33 :     in
34 :     fun mkGlobalVar x = (case peekFn x
35 :     of NONE => let
36 :     val x' = TGV.new {
37 :     name = GV.name x,
38 : jhr 3948 ty = U.trType (GV.ty x),
39 : jhr 3835 input = GV.isInput x,
40 :     output = false, (* FIXME: change once we support output globals *)
41 : jhr 3843 varying = GV.isVarying x,
42 :     apiTy = if GV.isInput x
43 : jhr 3948 then SOME(U.toAPIType (GV.ty x))
44 : jhr 3843 else NONE
45 : jhr 3835 }
46 :     in
47 :     setFn (x, x');
48 :     x'
49 :     end
50 :     | SOME x' => x'
51 :     (* end case *))
52 :     end
53 :    
54 :     (* associate Tree IL state variables with Low IL variables using properties *)
55 :     local
56 :     fun mkStateVar x = TSV.new {
57 :     name = SV.name x,
58 : jhr 3948 ty = U.trType (SV.ty x),
59 : jhr 3843 varying = SV.isVarying x,
60 :     apiTy = if (SV.isOutput x)
61 : jhr 3948 then SOME(U.toAPIType (SV.ty x))
62 : jhr 3843 else NONE
63 : jhr 3835 }
64 :     in
65 :     val {getFn = getStateVar, ...} = SV.newProp mkStateVar
66 :     end
67 :    
68 : jhr 3853 (* for variables that are in an equivalence class (see UnifyVars), we use a single
69 :     * TreeIR variable (or vector of variables) to represent them.
70 :     *)
71 :     datatype eq_var_rep = NOEQ | VAR of TV.t | VEC of TV.t list
72 :     local
73 :     val {peekFn : IR.var -> eq_var_rep option, setFn, ...} =
74 :     V.newProp (fn x => raise Fail(V.toString x))
75 : jhr 3856
76 : jhr 3955 fun repOf (env, x) = (case peekFn x
77 : jhr 3856 of SOME b => b
78 :     | NONE => let
79 :     val rep = (case V.ty x
80 : jhr 3948 of Ty.TensorTy[d] => VEC(U.newVectorVars(Env.layoutVec env d))
81 : jhr 4092 | ty => if AssignTypes.isMemoryVar x
82 :     then VAR(U.newMemVar x)
83 :     else VAR(U.newLocalVar x)
84 : jhr 3856 (* end case *))
85 :     in
86 :     setFn (x, rep);
87 :     rep
88 :     end
89 :     (* end case *))
90 : jhr 3853 in
91 : jhr 3856
92 : jhr 3853 fun eqClassRepOf (env, x) = (case UnifyVars.eqClassOf x
93 : jhr 3955 of SOME x' => repOf (env, x')
94 : jhr 3853 | NONE => NOEQ
95 :     (* end case *))
96 : jhr 3856
97 :     fun useVar env = let
98 :     val useV = Env.useVar env
99 :     in
100 :     fn x => (case UnifyVars.eqClassOf x
101 : jhr 3955 of SOME x' => (case repOf (env, x')
102 : jhr 3856 of VAR x => Env.TREE(T.E_Var x)
103 :     | VEC xs => let
104 :     val Ty.TensorTy[d] = V.ty x
105 :     in
106 :     Env.VEC(Env.layoutVec env d, List.map T.E_Var xs)
107 :     end
108 :     | NOEQ => raise Fail "impossible"
109 :     (* end case *))
110 :     | NONE => useV x
111 :     (* end case *))
112 :     end
113 : jhr 3853 end
114 :    
115 : jhr 3848 fun mkBlock stms = T.Block{locals = ref [], body = stms}
116 : jhr 3834 fun mkIf (x, stms, []) = T.S_IfThen(x, mkBlock stms)
117 :     | mkIf (x, stms1, stms2) = T.S_IfThenElse(x, mkBlock stms1, mkBlock stms2)
118 : jhr 3857 fun mkAssign (x, e) = T.S_Assign(false, x, e)
119 :     fun mkDefn (x, e) = T.S_Assign(true, x, e)
120 : jhr 3889 val zero = T.E_Lit(Literal.Real(RealLit.zero false))
121 : jhr 3828
122 : jhr 4033 (* turn an expression of type TensorTy to one of TensorTyRef *)
123 :     fun mkRef e = (case TreeTypeOf.exp e
124 :     of TTy.TensorTy(shp as _::_) => T.E_Op(TOp.TensorRef shp, [e])
125 :     | _ => e
126 :     (* end case *))
127 :    
128 : jhr 4067 (* turn an expression of type TensorRefTy to one of TensorRef *)
129 :     fun mkDeref e = (case TreeTypeOf.exp e
130 :     of TTy.TensorRefTy(shp as _::_) => T.E_Op(TOp.TensorCopy shp, [e])
131 :     | _ => e
132 :     (* end case *))
133 :    
134 : jhr 3834 fun cvtScalarTy Ty.BoolTy = TTy.BoolTy
135 :     | cvtScalarTy Ty.IntTy = TTy.IntTy
136 : jhr 3835 | cvtScalarTy (Ty.TensorTy[]) = TTy.realTy
137 : jhr 3834 | cvtScalarTy ty = raise Fail(concat["cvtScalarTy(", Ty.toString ty, ")"])
138 :    
139 : jhr 3948 (* define a new local variable and bind x to it in the environment. *)
140 : jhr 3858 fun newLocal (env, x) = let
141 : jhr 4092 val x' = if AssignTypes.isMemoryVar x then U.newMemVar x else U.newLocalVar x
142 : jhr 3858 in
143 : jhr 3948 Env.bindSimple (env, x, T.E_Var x');
144 : jhr 3858 x'
145 :     end
146 :    
147 : jhr 4091 (* define a new local variable and bind x to it in the environment. *)
148 :     fun newMemLocal (env, x) = let
149 :     val x' = U.newMemVar x
150 :     in
151 :     Env.bindSimple (env, x, T.E_Var x');
152 :     x'
153 :     end
154 :    
155 : jhr 3852 (* get a variable's binding as a single argument expression. This means that
156 : jhr 3941 * if x is bound to a vector of expressions, then we need to pack it. Since
157 :     * Pack gets translated to an array, we need to create a new temp.
158 : jhr 3852 *)
159 : jhr 3948 fun singleArg env (x, stms) = (case useVar env x
160 :     of Env.RHS(ty, e) => let
161 :     val tmp = U.newTempVar ("t", ty)
162 :     in
163 :     (T.E_Var tmp, T.S_Assign(true, tmp, e) :: stms)
164 :     end
165 :     | Env.TREE e => (e, stms)
166 : jhr 3941 | Env.VEC(layout, es) => let
167 : jhr 3948 val tmp = U.newTempVar("_arg", TTy.TensorTy[#wid layout])
168 : jhr 3941 in
169 : jhr 3948 (T.E_Var tmp, T.S_Assign(true, tmp, T.E_Pack(layout, es)) :: stms)
170 : jhr 3941 end
171 : jhr 3850 (* end case *))
172 :    
173 : jhr 3948 fun singleArgs (env, args) = let
174 :     val singleArg = singleArg env
175 :     fun doArg (arg, (es, stms)) = let
176 :     val (e', stms') = singleArg (arg, stms)
177 : jhr 3941 in
178 : jhr 3948 (e'::es, stms')
179 : jhr 3941 end
180 :     in
181 : jhr 3948 List.foldr doArg ([], []) args
182 : jhr 3941 end
183 :    
184 : jhr 3948 fun simpleArg env = let
185 :     val useVar = useVar env
186 :     in
187 :     fn (x, stms) => (case useVar x
188 :     of Env.RHS(ty, e) => let
189 :     val tmp = U.newTempVar ("t", ty)
190 :     in
191 :     (T.E_Var tmp, T.S_Assign(true, tmp, e)::stms)
192 :     end
193 :     | Env.TREE e => (e, stms)
194 :     | _ => raise Fail("expected simple binding for " ^ IR.Var.toString x)
195 :     (* end case *))
196 :     end
197 : jhr 3852
198 : jhr 3948 (* translation for simple operations (e.g., scalar operations) *)
199 :     fun simpleArgs (env, args) = let
200 :     val simpleArg = simpleArg env
201 :     fun doArg (arg, (es, stms)) = let
202 :     val (e', stms') = simpleArg (arg, stms)
203 :     in
204 :     (e'::es, stms')
205 :     end
206 :     in
207 :     List.foldr doArg ([], []) args
208 :     end
209 :    
210 :     fun vectorArg (env, x) = let
211 :     fun expToArg (e, stms) = (case V.ty x
212 : jhr 3843 of Ty.TensorTy[d] => let
213 :     val layout = Env.layoutVec env d
214 : jhr 4033 val e = mkRef e
215 : jhr 3843 val es = List.tabulate (
216 :     List.length(#pieces layout),
217 :     fn i => T.E_VLoad(layout, e, i))
218 :     in
219 : jhr 3948 (layout, es, stms)
220 : jhr 3843 end
221 : jhr 3855 | ty => raise Fail(concat[
222 :     "expected ", V.toString x, " : TensorTy[_], but found " ^ Ty.toString ty
223 :     ])
224 : jhr 3843 (* end case *))
225 : jhr 3948 in
226 :     case useVar env x
227 :     of Env.RHS(ty, e) => let
228 :     val tmp = U.newTempVar ("t", ty)
229 :     in
230 :     expToArg (T.E_Var tmp, [T.S_Assign(true, tmp, e)])
231 :     end
232 :     | Env.TREE e => expToArg(e, [])
233 :     | Env.VEC(layout, es) => (layout, es, [])
234 :     (* end case *)
235 :     end
236 : jhr 3834
237 :     (* convert a list of LowIR variables, each of which are mapped
238 :     * to lists of of vector expressions, to a list of list of expressions
239 :     *)
240 :     fun vectorArgs (env, []) = raise Fail "unexpected empty argument list"
241 :     | vectorArgs (env, x::xs) = let
242 : jhr 3948 val (layout, exps, stms) = vectorArg (env, x)
243 :     fun doArg (x, (argLists, stms)) = let
244 :     val (_, exps, stms') = vectorArg (env, x)
245 : jhr 3834 in
246 : jhr 3948 (ListPair.mapEq (op ::) (exps, argLists), stms'@stms)
247 : jhr 3834 end
248 : jhr 3948 val (argLists, stms) = List.foldl doArg ([exps], stms) xs
249 : jhr 3834 in
250 : jhr 3948 (layout, List.map List.rev argLists, List.rev stms)
251 : jhr 3834 end
252 :    
253 : jhr 3948 (* translation for simple vector operations *)
254 :     fun trVecOp (env, rator, args) = let
255 :     val useVar = useVar env
256 :     fun doArg (x, (es, stms)) = (case useVar x
257 :     of Env.RHS(ty, e) => let
258 :     val tmp = U.newTempVar ("t", ty)
259 :     in
260 :     (T.E_Var tmp :: es, T.S_Assign(true, tmp, e)::stms)
261 :     end
262 :     | Env.TREE e => (e::es, stms)
263 :     | _ => raise Fail("expected simple binding for " ^ IR.Var.toString x)
264 :     (* end case *))
265 :     val (args, stms) = List.foldr doArg ([], []) args
266 :     in
267 :     (Env.TREE(T.E_Op(rator, args)), stms)
268 :     end
269 :    
270 : jhr 3852 fun trOp (env, srcRator, args) = let
271 : jhr 4039 fun bindTREE rator = let
272 : jhr 3948 val (args, stms) = simpleArgs (env, args)
273 :     in
274 :     (Env.TREE(T.E_Op(rator, args)), stms)
275 :     end
276 : jhr 4065 fun bindTREE' rator = let
277 :     val (args, stms) = singleArgs (env, args)
278 :     in
279 :     (Env.TREE(T.E_Op(rator, args)), stms)
280 :     end
281 : jhr 3948 fun bindRHS (ty, rator) = let
282 :     val (args, stms) = simpleArgs (env, args)
283 :     in
284 :     (Env.RHS(ty, T.E_Op(rator, args)), stms)
285 :     end
286 : jhr 3835 fun bindVOp rator = let
287 : jhr 3948 val (layout, argss, stms) = vectorArgs (env, args)
288 : jhr 4077 fun mkArgs (w, [p], [args]) = [T.E_Op(rator(w, p), args)]
289 : jhr 3880 | mkArgs (w, p::ps, args::argss) =
290 : jhr 4077 T.E_Op(rator(p, p), args) :: mkArgs (w-p, ps, argss)
291 :     | mkArgs _ = raise Fail "bindVOp: arity mismatch"
292 : jhr 3880 val exps = mkArgs (#wid layout, #pieces layout, argss)
293 : jhr 3829 in
294 : jhr 3948 (Env.VEC(layout, exps), stms)
295 : jhr 3829 end
296 : jhr 3880 fun mkArgs f ({wid, pieces, ...} : TTy.vec_layout, args) = let
297 :     fun mk (w, [p], [x]) = [T.E_Op(f(w, p, x))]
298 :     | mk (w, p::ps, x::xs) = T.E_Op(f(p, p, x)) :: mk (w-p, ps, xs)
299 :     | mk _ = raise Fail "mkArgs: arity mismatch"
300 :     in
301 :     mk (wid, pieces, args)
302 :     end
303 : jhr 3829 in
304 : jhr 3834 case srcRator
305 : jhr 4039 of Op.IAdd => bindTREE TOp.IAdd
306 :     | Op.ISub => bindTREE TOp.ISub
307 :     | Op.IMul => bindTREE TOp.IMul
308 :     | Op.IDiv => bindTREE TOp.IDiv
309 :     | Op.IMod => bindTREE TOp.IMod
310 :     | Op.INeg => bindTREE TOp.INeg
311 : jhr 3843 (* QUESTION: should we just use VAdd 1, etc ?*)
312 : jhr 4039 | Op.RAdd => bindTREE TOp.RAdd
313 :     | Op.RSub => bindTREE TOp.RSub
314 :     | Op.RMul => bindTREE TOp.RMul
315 :     | Op.RDiv => bindTREE TOp.RDiv
316 :     | Op.RNeg => bindTREE TOp.RNeg
317 :     | Op.LT ty => bindTREE (TOp.LT (cvtScalarTy ty))
318 :     | Op.LTE ty => bindTREE (TOp.LTE (cvtScalarTy ty))
319 :     | Op.EQ ty => bindTREE (TOp.EQ (cvtScalarTy ty))
320 :     | Op.NEQ ty => bindTREE (TOp.NEQ (cvtScalarTy ty))
321 :     | Op.GT ty => bindTREE (TOp.GT (cvtScalarTy ty))
322 :     | Op.GTE ty => bindTREE (TOp.GTE (cvtScalarTy ty))
323 :     | Op.Not => bindTREE TOp.Not
324 :     | Op.Abs ty => bindTREE (TOp.Abs (cvtScalarTy ty))
325 :     | Op.Max ty => bindTREE (TOp.Max (cvtScalarTy ty))
326 :     | Op.Min ty => bindTREE (TOp.Min (cvtScalarTy ty))
327 :     | Op.RClamp => bindTREE TOp.RClamp
328 :     | Op.RLerp => bindTREE TOp.RLerp
329 : jhr 3834 | Op.VAdd _ => bindVOp TOp.VAdd
330 :     | Op.VSub _ => bindVOp TOp.VSub
331 : jhr 3855 | Op.VScale _ => let
332 :     val [s, v] = args
333 : jhr 3948 val (s, stms) = simpleArg env (s, [])
334 :     val (layout, vs, stms') = vectorArg (env, v)
335 : jhr 3880 val exps = mkArgs (fn (w, p, x) => (TOp.VScale(w, p), [s, x])) (layout, vs)
336 : jhr 3855 in
337 : jhr 3948 (Env.VEC(layout, exps), stms@stms')
338 : jhr 3855 end
339 : jhr 3834 | Op.VMul _ => bindVOp TOp.VMul
340 :     | Op.VNeg _ => bindVOp TOp.VNeg
341 : jhr 3856 | Op.VSum _ => let
342 :     val [v] = args
343 : jhr 3948 val (layout, pieces, stms) = vectorArg (env, v)
344 :     val e::es = mkArgs (fn (w, p, x) => (TOp.VSum(w, p), [x])) (layout, pieces)
345 : jhr 3856 in
346 : jhr 3948 (Env.TREE(List.foldr (fn (e, es) => T.E_Op(TOp.RAdd, [e, es])) e es), stms)
347 : jhr 3856 end
348 : jhr 4056 | Op.VDot _ => let
349 :     val (layout, argss, stms) = vectorArgs (env, args)
350 : jhr 4077 fun mkArgs (w, [p], [args]) = [T.E_Op(TOp.VDot(w, p), args)]
351 : jhr 4056 | mkArgs (w, p::ps, args::argss) =
352 : jhr 4077 T.E_Op(TOp.VDot(p, p), args) :: mkArgs (w-p, ps, argss)
353 :     | mkArgs _ = raise Fail "VDot: arity mismatch"
354 : jhr 4056 val e::es = mkArgs (#wid layout, #pieces layout, argss)
355 :     in
356 :     (Env.TREE(List.foldr (fn (e, es) => T.E_Op(TOp.RAdd, [e, es])) e es), stms)
357 :     end
358 : jhr 3844 | Op.VIndex(_, i) => let
359 : jhr 3856 val [v] = args
360 : jhr 3948 val ({wid, pieces, ...}, es, stms) = vectorArg (env, v)
361 : jhr 3880 fun select (_, wid, [w], [e]) = Env.TREE(T.E_Op(TOp.VIndex(wid, w, i), [e]))
362 :     | select (i, wid, w::ws, e::es) =
363 : jhr 3844 if (i < w)
364 : jhr 3880 then Env.TREE(T.E_Op(TOp.VIndex(w, w, i), [e]))
365 :     else select (i-w, wid-w, ws, es)
366 : jhr 3844 | select _ = raise Fail("bogus " ^ Op.toString srcRator)
367 :     in
368 : jhr 3948 (select (i, wid, pieces, es), stms)
369 : jhr 3844 end
370 : jhr 3855 | Op.VClamp n => let
371 : jhr 4128 val [lo, hi, v] = args
372 :     val (lo, stms) = simpleArg env (lo, [])
373 : jhr 3948 val (hi, stms) = simpleArg env (hi, stms)
374 : jhr 4128 val (layout, vs, stms') = vectorArg (env, v)
375 : jhr 3880 val exps = mkArgs
376 : jhr 4128 (fn (w, p, x) => (TOp.VClamp(w, p), [lo, hi, x]))
377 : jhr 3880 (layout, vs)
378 : jhr 3855 in
379 : jhr 4128 (Env.VEC(layout, exps), List.revAppend(stms, List.rev stms))
380 : jhr 3855 end
381 : jhr 3834 | Op.VMapClamp n => bindVOp TOp.VMapClamp
382 : jhr 4077 | Op.VLerp n => let
383 :     val [u, v, t] = args
384 :     val (layout, us, stms1) = vectorArg (env, u)
385 :     val (_, vs, stms2) = vectorArg (env, v)
386 :     val (t, stms) = simpleArg env (t, stms2 @ stms1)
387 :     val exps = let
388 :     fun mkArgs (w, [p], [u], [v]) = [T.E_Op(TOp.VLerp(w, p), [u, v, t])]
389 :     | mkArgs (w, p::ps, u::ur, v::vr) =
390 :     T.E_Op(TOp.VLerp(p, p), [u, v, t]) :: mkArgs (w-p, ps, ur, vr)
391 :     | mkArgs _ = raise Fail "VLerp: arity mismatch"
392 :     in
393 :     mkArgs (#wid layout, #pieces layout, us, vs)
394 :     end
395 :     in
396 :     (Env.VEC(layout, exps), stms)
397 :     end
398 : jhr 4036 | Op.TensorIndex(ty, idxs) => let
399 :     val ([arg], stms) = simpleArgs (env, args)
400 :     val ty = TreeTypeOf.exp arg
401 : jhr 3958 in
402 : jhr 4036 (Env.TREE(T.E_Op(TOp.TensorIndex(ty, idxs), [arg])), stms)
403 : jhr 3958 end
404 : jhr 4036 | Op.ProjectLast(_, idxs) => let
405 :     val ([arg], stms) = simpleArgs (env, args)
406 :     val ty = TreeTypeOf.exp arg
407 : jhr 3958 in
408 : jhr 4036 (Env.TREE(T.E_Op(TOp.ProjectLast(ty, idxs), [arg])), stms)
409 : jhr 3958 end
410 : jhr 4039 | Op.Select(ty, i) => bindTREE (TOp.Select(U.trType ty, i))
411 :     | Op.Subscript ty => bindTREE (TOp.Subscript(U.trType ty))
412 :     | Op.MkDynamic(ty, n) => bindTREE (TOp.MkDynamic(U.trType ty, n))
413 : jhr 4065 | Op.Append ty => bindTREE' (TOp.Append(U.trType ty))
414 :     | Op.Prepend ty => bindTREE' (TOp.Prepend(U.trType ty))
415 : jhr 4039 | Op.Concat ty => bindTREE (TOp.Concat(U.trType ty))
416 :     | Op.Range => bindTREE TOp.Range
417 :     | Op.Length ty => bindTREE (TOp.Length(U.trType ty))
418 : jhr 4065 | Op.SphereQuery(ty1, ty2) => bindTREE' (TOp.SphereQuery(U.trType ty1, U.trType ty2))
419 : jhr 4039 | Op.Sqrt => bindTREE TOp.Sqrt
420 :     | Op.Cos => bindTREE TOp.Cos
421 :     | Op.ArcCos => bindTREE TOp.ArcCos
422 :     | Op.Sin => bindTREE TOp.Sin
423 :     | Op.ArcSin => bindTREE TOp.ArcSin
424 :     | Op.Tan => bindTREE TOp.Tan
425 :     | Op.ArcTan => bindTREE TOp.ArcTan
426 :     | Op.Exp => bindTREE TOp.Exp
427 :     | Op.Ceiling 1 => bindTREE TOp.RCeiling
428 : jhr 3880 | Op.Ceiling d => bindVOp TOp.VCeiling
429 : jhr 4039 | Op.Floor 1 => bindTREE TOp.RFloor
430 : jhr 3880 | Op.Floor d => bindVOp TOp.VFloor
431 : jhr 4039 | Op.Round 1 => bindTREE TOp.RRound
432 : jhr 3880 | Op.Round d => bindVOp TOp.VRound
433 : jhr 4039 | Op.Trunc 1 => bindTREE TOp.RTrunc
434 : jhr 3880 | Op.Trunc d => bindVOp TOp.VTrunc
435 : jhr 4039 | Op.IntToReal => bindTREE TOp.IntToReal
436 :     | Op.RealToInt 1 => bindTREE TOp.RealToInt
437 : jhr 3880 | Op.RealToInt d => let
438 : jhr 3966 val [v] = args
439 :     val (layout, args, stms) = vectorArg (env, v)
440 : jhr 3880 in
441 :     case #pieces layout
442 : jhr 3948 of [w] => (Env.TREE(T.E_Op(TOp.VToInt(d, w), args)), stms)
443 : jhr 3880 | _ => raise Fail(concat["FIXME: RealToInt<", Int.toString d, ">"])
444 :     (* end case *)
445 :     end
446 : jhr 3955 (* FIXME: reduction operators
447 : jhr 3834 | Op.R_All ty => ??
448 :     | Op.R_Exists ty => ??
449 :     | Op.R_Max ty => ??
450 :     | Op.R_Min ty => ??
451 :     | Op.R_Sum ty => ??
452 :     | Op.R_Product ty => ??
453 :     | Op.R_Mean ty => ??
454 :     | Op.R_Variance ty => ??
455 : jhr 3844 *)
456 : jhr 4039 | Op.Transform info => bindTREE (TOp.Transform info)
457 :     | Op.Translate info => bindTREE (TOp.Translate info)
458 :     | Op.ControlIndex(info, ctl, d) => bindTREE (TOp.ControlIndex(info, ctl, d))
459 :     | Op.LoadVoxel info => bindTREE (TOp.LoadVoxel info)
460 : jhr 4151 (*
461 : jhr 4039 | Op.Inside(info, s) => bindTREE (TOp.Inside(info, s))
462 : jhr 4151 *)
463 :     | Op.Inside(info, s) => (case ImageInfo.dim info
464 :     of 1 => bindTREE (TOp.Inside(VectorLayout.realLayout, info, s))
465 :     | d => let
466 :     val [x, img] = args
467 :     val (layout, args, stms) = vectorArg (env, x)
468 :     val (img, stms) = simpleArg env (img, stms)
469 :     in
470 :     (Env.TREE(T.E_Op(TOp.Inside(layout, info, s), args@[img])), stms)
471 :     end
472 :     (* end case *))
473 : jhr 4039 | Op.ImageDim(info, d) => bindTREE(TOp.ImageDim(info, d))
474 :     | Op.MathFn f => bindTREE (TOp.MathFn f)
475 : jhr 3844 | rator => raise Fail("bogus operator " ^ Op.toString srcRator)
476 : jhr 3829 (* end case *)
477 :     end
478 :    
479 : jhr 3955 (* FIXME: we need to defer the copy when possible *)
480 :     (* if required, add a TensorCopy operation to the rhs of an assignment *)
481 :     fun mkAssignRHS (TTy.TensorTy _, rhs) = (case TreeTypeOf.exp rhs
482 :     of TTy.TensorRefTy shp => T.E_Op(TOp.TensorCopy shp, [rhs])
483 :     | _ => rhs
484 :     (* end case *))
485 :     | mkAssignRHS (_, rhs) = rhs
486 :    
487 :     fun mkDefn' (x, rhs) = mkDefn (x, mkAssignRHS(TV.ty x, rhs))
488 :     fun mkAssign' (x, rhs) = mkAssign (x, mkAssignRHS(TV.ty x, rhs))
489 :    
490 : jhr 3852 (* cases:
491 :     x in EqClass
492 :     issue assignment; lhs is binding of representative (could be multiple vars)
493 :     useCount(x) > 1 and rhs is not simple
494 :     rhs is simple
495 :     rhs is vector
496 :     *)
497 : jhr 4091 fun trAssign (env, lhs, rhs : IR.rhs) = let
498 : jhr 3955 fun getLHS () = (case UnifyVars.eqClassOf lhs of SOME x => x | _ => lhs)
499 : jhr 3889 (* binding for the lhs variable, where the rhs is a simple expression. We check to
500 :     * see if it is part of an merged equivalence class, in which case we need to generate
501 :     * assigment(s)
502 : jhr 3836 *)
503 : jhr 3853 fun bindSimple rhs = (case eqClassRepOf(env, lhs)
504 : jhr 3948 of NOEQ => (Env.bindSimple (env, lhs, rhs); [])
505 : jhr 3955 | VAR x' => [mkAssign' (x', rhs)]
506 : jhr 3889 | VEC xs' => (case V.ty lhs
507 :     of Ty.TensorTy[d] => let
508 :     val layout = Env.layoutVec env d
509 : jhr 4033 val rhs = mkRef rhs
510 : jhr 3889 in
511 :     List.mapi
512 :     (fn (i, x') => mkAssign(x', T.E_VLoad(layout, rhs, i)))
513 :     xs'
514 :     end
515 :     | _ => raise Fail "inconsistent"
516 :     (* end case *))
517 : jhr 3836 (* end case *))
518 : jhr 3948 fun assignOp (rator, args) = let
519 :     val (args, stms) = simpleArgs (env, args)
520 :     in
521 :     case eqClassRepOf(env, lhs)
522 :     of NOEQ => (
523 : jhr 4091 Env.bindVar (env, lhs, Env.RHS(U.trTempType(V.ty lhs), T.E_Op(rator, args)));
524 : jhr 3948 stms)
525 : jhr 3955 | VAR x' => stms @ [mkAssign' (x', T.E_Op(rator, args))]
526 : jhr 3948 | VEC _ => raise Fail ("unexpected VEC for lhs " ^ V.toString lhs)
527 :     (* end case *)
528 :     end
529 : jhr 4091 (* bind the lhs to a tensor cons expression (including Op.Zero) *)
530 :     fun bindCons (args, Ty.TensorTy[d], stms) = let
531 :     val layout = Env.layoutVec env d
532 :     fun mkVecs (args, w::ws) = let
533 :     (* take arguments from args to build a vector value of width w; pad as
534 :     * necessary.
535 :     *)
536 :     fun take (0, args, es) = T.E_Vec(w, w, List.rev es) :: mkVecs (args, ws)
537 :     | take (i, [], es) = if #padded layout andalso null ws
538 :     then [T.E_Vec(w-i, w, List.rev es)]
539 :     else raise Fail "too few arguments for CONS"
540 :     | take (i, arg::args, es) = take (i-1, args, arg :: es)
541 :     in
542 :     take (w, args, [])
543 :     end
544 :     | mkVecs ([], []) = []
545 :     | mkVecs (_, []) = raise Fail "too many arguments for CONS"
546 :     val es = mkVecs (args, #pieces layout)
547 :     in
548 : jhr 4092 case eqClassRepOf(env, lhs)
549 :     of NOEQ => if (V.useCount lhs > 1)
550 :     then (Env.bindVar(env, lhs, Env.VEC(layout, es)); stms)
551 :     else let
552 :     val vs = U.newVectorVars layout
553 :     in
554 :     Env.bindVar (env, lhs, Env.VEC(layout, List.map T.E_Var vs));
555 :     ListPair.foldl (fn (v, e, stms) => mkDefn(v, e)::stms) stms (vs, es)
556 :     end
557 :     | VEC xs =>
558 : jhr 4091 ListPair.foldl (fn (x, e, stms) => mkAssign(x, e)::stms) stms (xs, es)
559 :     | _ => raise Fail "inconsistent"
560 :     (* end case *)
561 :     end
562 :     | bindCons (args, ty as Ty.TensorTy _, stms) = let
563 :     val ty = U.trType ty
564 :     val cons = T.E_Cons(args, ty)
565 :     in
566 :     case eqClassRepOf(env, lhs)
567 : jhr 4092 of NOEQ => if (V.useCount lhs > 1)
568 :     then mkDefn (newMemLocal (env, lhs), cons) :: stms
569 :     else (
570 :     Env.bindVar (env, lhs, Env.RHS(ty, cons));
571 :     stms)
572 : jhr 4091 | VAR x => mkAssign (x, cons) :: stms
573 :     | VEC xs => raise Fail "inconsistent"
574 :     (* end case *)
575 :     end
576 : jhr 3834 in
577 :     case rhs
578 : jhr 3844 of IR.GLOBAL x => bindSimple (T.E_Global(mkGlobalVar x))
579 : jhr 3852 | IR.STATE(NONE, fld) =>
580 :     bindSimple (T.E_State(NONE, getStateVar fld))
581 : jhr 3948 | IR.STATE(SOME x, fld) => let
582 :     val (arg, stms) = simpleArg env (x, [])
583 :     val stms' = bindSimple (T.E_State(SOME arg, getStateVar fld))
584 :     in
585 :     stms' @ stms
586 :     end
587 : jhr 3854 | IR.VAR x => raise Fail "FIXME: VAR"
588 : jhr 3850 | IR.LIT lit => bindSimple (T.E_Lit lit)
589 : jhr 4025 (* FIXME: use the general IR.OP case for eigen-things, since the target info says that they
590 :     * are not inline.
591 :     *)
592 : jhr 3852 | IR.OP(Op.EigenVecs2x2, args) => assignOp (TOp.EigenVecs2x2, args)
593 :     | IR.OP(Op.EigenVecs3x3, args) => assignOp (TOp.EigenVecs3x3, args)
594 :     | IR.OP(Op.EigenVals2x2, args) => assignOp (TOp.EigenVals2x2, args)
595 :     | IR.OP(Op.EigenVals3x3, args) => assignOp (TOp.EigenVals3x3, args)
596 : jhr 3886 | IR.OP(Op.Zero(ty as Ty.TensorTy dd), []) => let
597 : jhr 3887 val z = T.E_Lit(Literal.Real(RealLit.zero false))
598 : jhr 3886 val sz = List.foldl Int.* 1 dd
599 : jhr 3948 in
600 : jhr 4091 bindCons (List.tabulate(sz, fn _ => z), ty, [])
601 : jhr 3948 end
602 : jhr 3886 | IR.OP(Op.LoadSeq(ty, file), []) => let
603 : jhr 3955 val lhs = newLocal (env, getLHS ())
604 : jhr 3886 in
605 : jhr 3948 [T.S_LoadNrrd(lhs, U.toAPIType ty, file)]
606 : jhr 3886 end
607 :     | IR.OP(Op.LoadImage(ty, file), []) => let
608 : jhr 3955 val lhs = newLocal (env, getLHS ())
609 : jhr 3886 in
610 : jhr 3948 [T.S_LoadNrrd(lhs, U.toAPIType ty, file)]
611 : jhr 3886 end
612 : jhr 3852 | IR.OP(rator, args) => let
613 : jhr 3948 val (rhs, stms) = trOp (env, rator, args)
614 : jhr 4025 val emitBind = (V.useCount lhs > 1) orelse not(Env.isInlineOp env rator)
615 : jhr 3852 in
616 : jhr 4025 case (rhs, eqClassRepOf(env, lhs), emitBind)
617 : jhr 3948 of (_, NOEQ, false) => (Env.bindVar (env, lhs, rhs); stms)
618 : jhr 4026 (* FIXME: if the rhs has TensorRef type, then we should make the lhs TensorRef too! *)
619 : jhr 3955 | (Env.TREE e, NOEQ, true) => mkDefn'(newLocal(env, lhs), e) :: stms
620 :     | (Env.TREE e, VAR x', _) => mkAssign'(x', e) :: stms
621 : jhr 3854 | (Env.VEC(layout, es), NOEQ, true) => let
622 : jhr 3948 val vs = U.newVectorVars layout
623 : jhr 3854 in
624 : jhr 3948 Env.bindVar (env, lhs, Env.VEC(layout, List.map T.E_Var vs));
625 :     ListPair.foldl (fn (v, e, stms) => mkDefn(v, e)::stms) stms (vs, es)
626 : jhr 3854 end
627 : jhr 3948 | (Env.VEC(layout, es), VEC xs, _) =>
628 :     ListPair.foldl (fn (x, e, stms) => mkAssign(x, e)::stms) stms (xs, es)
629 : jhr 3854 | _ => raise Fail "inconsistent"
630 :     (* end case *)
631 : jhr 3852 end
632 : jhr 3941 | IR.CONS(args, ty) => let
633 : jhr 4091 val (es, stms) = simpleArgs (env, args)
634 : jhr 3941 in
635 : jhr 4091 bindCons (es, ty, stms)
636 : jhr 3941 end
637 :     | IR.SEQ(args, ty) => let
638 :     val (es, stms) = singleArgs (env, args)
639 : jhr 3948 val ty = U.trType ty
640 : jhr 4067 (* if we are dealing with a sequence of tensors, then we need to copy references *)
641 :     val es = (case ty
642 :     of TTy.SeqTy(TTy.TensorTy _, _) => List.map mkDeref es
643 :     | _ => es
644 :     (* end case *))
645 : jhr 4091 val seq = T.E_Seq(es, ty)
646 : jhr 3941 in
647 : jhr 4091 case eqClassRepOf(env, lhs)
648 : jhr 4092 of NOEQ => if (V.useCount lhs > 1)
649 :     then mkDefn (newMemLocal (env, lhs), seq) :: stms
650 :     else (
651 :     Env.bindVar (env, lhs, Env.RHS(ty, seq));
652 :     stms)
653 : jhr 4091 | VAR x => mkAssign (x, seq) :: stms
654 :     | VEC xs => raise Fail "inconsistent"
655 :     (* end case *)
656 : jhr 3941 end
657 : jhr 3852 | rhs => raise Fail(concat["unexpected ", IR.RHS.toString rhs, " in LowIR code"])
658 : jhr 3834 (* end case *)
659 :     end
660 : jhr 3856 handle ex => (
661 :     print(concat["trAssign: ", V.toString lhs, " = ", IR.RHS.toString rhs, "\n"]);
662 :     raise ex)
663 : jhr 3834
664 :     (* In order to reconstruct the block-structure from the CFG, we keep a stack of open ifs.
665 :     * the items on this stack distinguish between when we are processing the then and else
666 :     * branches of the if.
667 :     *)
668 :     datatype open_if
669 :     (* working on the "then" branch. The fields are statments that preceed the if, the condition,
670 :     * and the else-branch node.
671 :     *)
672 :     = THEN_BR of T.stm list * T.exp * IR.node
673 :     (* working on the "else" branch. The fields are statments that preceed the if, the condition,
674 :     * the "then" branch statements, and the node kind that terminated the "then" branch (will be
675 :     * a JOIN or EXIT(DIE, STABILIZE, or UNREACHABLE)).
676 :     *)
677 :     | ELSE_BR of T.stm list * T.exp * T.stm list * IR.node_kind
678 :    
679 : jhr 3852 fun trCFGWithEnv (env, cfg) = let
680 : jhr 3856 fun useScalar x = (case useVar env x
681 : jhr 3948 of Env.RHS(_, e) => e
682 :     | Env.TREE e => e
683 : jhr 3835 | _ => raise Fail("expected scalar binding for " ^ V.toString x)
684 :     (* end case *))
685 : jhr 4092 (* analyze the CFG *)
686 : jhr 3834 val _ = UnifyVars.analyze cfg
687 : jhr 4092 val _ = AssignTypes.analyze cfg
688 : jhr 3851 (* join (stk, stms, k): handle a control-flow join, where env is the
689 : jhr 3834 * current environment, stk is the stack of open ifs (the top of stk specifies
690 :     * which branch we are in), stms are the TreeIL statements preceding the join
691 :     * on the current path, and k is the kind of the join node (either JOIN or EXIT).
692 :     *)
693 : jhr 3851 fun join ([], _, IR.JOIN _) = raise Fail "JOIN with no open if"
694 : jhr 3852 | join ([], stms, _) = mkBlock (List.rev stms)
695 : jhr 3851 | join (THEN_BR(stms1, cond, elseBr)::stk, thenBlk, k) = let
696 :     val thenBlk = Env.flushPending (env, thenBlk)
697 : jhr 3834 in
698 : jhr 3851 doNode (elseBr, ELSE_BR(stms1, cond, thenBlk, k)::stk, [])
699 : jhr 3834 end
700 : jhr 3851 | join (ELSE_BR(stms, cond, thenBlk, k1)::stk, elseBlk, k2) = let
701 :     val elseBlk = Env.flushPending (env, elseBlk)
702 : jhr 3834 in
703 :     case (k1, k2)
704 : jhr 3848 of (IR.JOIN{succ, ...}, IR.JOIN _) => let
705 : jhr 3834 val stm = mkIf(cond, List.rev thenBlk, List.rev elseBlk)
706 :     in
707 : jhr 3851 doNode (!succ, stk, stm::stms)
708 : jhr 3834 end
709 : jhr 3848 | (IR.JOIN{succ, ...}, _) => let
710 : jhr 3834 val stm = mkIf(cond, List.rev thenBlk, List.rev elseBlk)
711 :     in
712 : jhr 3851 doNode (!succ, stk, stm::stms)
713 : jhr 3834 end
714 : jhr 3848 | (_, IR.JOIN{succ, ...}) => let
715 : jhr 3834 val stm = mkIf(cond, List.rev thenBlk, List.rev elseBlk)
716 :     in
717 : jhr 3851 doNode (!succ, stk, stm::stms)
718 : jhr 3834 end
719 :     | (_, _) => let
720 :     val stm = mkIf(cond, List.rev thenBlk, List.rev elseBlk)
721 :     in
722 : jhr 3852 mkBlock (List.rev(stm::stms))
723 : jhr 3834 end
724 :     (* end case *)
725 :     end
726 : jhr 3851 and doNode (nd : IR.node, ifStk : open_if list, stms) = (case IR.Node.kind nd
727 : jhr 3834 of IR.NULL => raise Fail "unexpected NULL"
728 : jhr 3851 | IR.ENTRY{succ} => doNode (!succ, ifStk, stms)
729 :     | k as IR.JOIN _ => join (ifStk, stms, k)
730 : jhr 3834 | IR.COND{cond, trueBranch, falseBranch, ...} => let
731 : jhr 3835 val cond = useScalar (!cond)
732 : jhr 3851 val stms = Env.flushPending (env, stms)
733 : jhr 3834 in
734 : jhr 3851 doNode (!trueBranch, THEN_BR(stms, cond, !falseBranch)::ifStk, [])
735 : jhr 3834 end
736 : jhr 3835 | IR.FOREACH{var, src, bodyEntry, succ, ...} => let
737 : jhr 3948 val var' = U.newIterVar var
738 :     val _ = Env.bindSimple (env, var, T.E_Var var')
739 : jhr 3924 (* note that the flatten phase on LowIR should guarantee that each
740 :     * FOREACH node that takes a Range argument has its own instance of
741 :     * the Range expression.
742 : jhr 3948 * Note also that we resolve the src scalar before processing the body
743 :     * of the loop so that any nested flushPendings do not cause Range
744 :     * bindings to be flushed.
745 : jhr 3924 *)
746 : jhr 3948 val mkStm = (case useScalar(!src)
747 :     of T.E_Op(TOp.Range, [lo, hi]) =>
748 :     (fn body => T.S_For(var', lo, hi, body))
749 :     | e =>
750 :     (fn body => T.S_Foreach(var', e, body))
751 : jhr 3924 (* end case *))
752 :     val stms = Env.flushPending (env, stms)
753 : jhr 3948 val stm = mkStm (doNode (!bodyEntry, [], []))
754 : jhr 3834 in
755 : jhr 3851 doNode (!succ, ifStk, stm::stms)
756 : jhr 3834 end
757 : jhr 3848 | IR.NEXT _ => mkBlock (List.rev stms)
758 : jhr 3834 | IR.COM {text, succ, ...} =>
759 : jhr 3851 doNode (!succ, ifStk, T.S_Comment text :: stms)
760 : jhr 3834 | IR.ASSIGN{stm=(lhs, rhs), succ, ...} => let
761 : jhr 3852 val stms' = trAssign (env, lhs, rhs)
762 : jhr 3834 in
763 : jhr 3851 doNode (!succ, ifStk, stms' @ stms)
764 : jhr 3834 end
765 : jhr 3866 | IR.MASSIGN{stm=([], Op.Print tys, xs), succ, ...} => let
766 : jhr 3941 val (es, stms') = singleArgs (env, xs)
767 : jhr 4033 (* translate TensorTy to TensorRefTy in the type list *)
768 :     fun trType (Ty.TensorTy(shp as _::_)) = TTy.TensorRefTy shp
769 :     | trType ty = U.trType ty
770 :     val tys = List.map trType tys
771 :     val stm = T.S_Print(tys, List.map mkRef es)
772 : jhr 3848 in
773 : jhr 3941 doNode (!succ, ifStk, stm :: List.revAppend (stms', stms))
774 : jhr 3848 end
775 : jhr 3866 | IR.MASSIGN{stm=(ys, rator, xs), succ, ...} => raise Fail(concat[
776 :     "unexepected operator ", Op.toString rator, " for MASSIGN"
777 :     ])
778 : jhr 3834 | IR.GASSIGN{lhs, rhs, succ, ...} => let
779 : jhr 3948 val gv = mkGlobalVar lhs
780 : jhr 3955 fun mkGAssign (gv, e) = T.S_GAssign(gv, mkAssignRHS(TGV.ty gv, e))
781 : jhr 3948 val stm = (case useVar env rhs
782 : jhr 3955 of Env.RHS(_, e) => mkGAssign(gv, e)
783 :     | Env.TREE e => mkGAssign(gv, e)
784 : jhr 3948 | Env.VEC(layout, es) => let
785 :     val tmp = U.newTempVar("_arg", TTy.TensorTy[#wid layout])
786 :     in
787 :     T.S_GAssign(gv, T.E_Pack(layout, es))
788 :     end
789 :     (* end case *))
790 : jhr 3834 in
791 : jhr 3948 doNode (!succ, ifStk, stm :: stms)
792 : jhr 3834 end
793 : jhr 3851 | IR.NEW{strand, args, succ, ...} => let
794 : jhr 3941 val (es, stms') = singleArgs (env, args)
795 :     val stm = T.S_New(strand, es)
796 : jhr 3851 in
797 : jhr 3941 doNode (!succ, ifStk, stm :: List.revAppend (stms', stms))
798 : jhr 3851 end
799 : jhr 3834 | IR.SAVE{lhs, rhs, succ, ...} => let
800 : jhr 3948 val sv = getStateVar lhs
801 : jhr 3955 fun mkSAssign (sv, e) = T.S_Save(sv, mkAssignRHS(TSV.ty sv, e))
802 : jhr 3948 val stm = (case useVar env rhs
803 : jhr 3955 of Env.RHS(_, e) => mkSAssign(sv, e)
804 :     | Env.TREE e => mkSAssign(sv, e)
805 : jhr 3948 | Env.VEC(layout, es) => let
806 :     val tmp = U.newTempVar("_arg", TTy.TensorTy[#wid layout])
807 :     in
808 :     T.S_Save(sv, T.E_Pack(layout, es))
809 :     end
810 :     (* end case *))
811 : jhr 3834 in
812 : jhr 3948 doNode (!succ, ifStk, stm :: stms)
813 : jhr 3834 end
814 :     | k as IR.EXIT{kind, succ, ...} => (case (!succ, kind)
815 : jhr 3852 of (NONE, ExitKind.RETURN) => mkBlock (List.rev(T.S_Exit :: stms))
816 :     | (NONE, ExitKind.ACTIVE) => mkBlock (List.rev(T.S_Active :: stms))
817 : jhr 3834 | (NONE, ExitKind.STABILIZE) => let
818 :     val stms = T.S_Stabilize :: stms
819 :     in
820 : jhr 3851 join (ifStk, stms, k)
821 : jhr 3834 end
822 : jhr 3851 | (NONE, ExitKind.DIE) => join (ifStk, T.S_Die :: stms, k)
823 :     | (NONE, ExitKind.UNREACHABLE) => join (ifStk, stms, k)
824 :     | (SOME nd, ExitKind.ACTIVE) => doNode (nd, ifStk, T.S_Active :: stms)
825 :     | (SOME nd, ExitKind.STABILIZE) => doNode (nd, ifStk, T.S_Stabilize :: stms)
826 :     | (SOME nd, ExitKind.DIE) => doNode (nd, ifStk, T.S_Die :: stms)
827 :     | (SOME nd, ExitKind.UNREACHABLE) => doNode (nd, ifStk, stms)
828 : jhr 3834 | _ => raise Fail("unexpected continuation edge from "^IR.Node.toString nd)
829 :     (* end case *))
830 :     (* end case *))
831 :     in
832 : jhr 3857 doNode (IR.CFG.entry cfg, [], [])
833 : jhr 3834 end
834 :    
835 : jhr 3857 fun trCFG info cfg = ScopeVars.assignScopes ([], trCFGWithEnv (Env.new info, cfg))
836 : jhr 3835
837 : jhr 3953 (* Build a strand method from a TreeIR block. We need to check for language features
838 :     * that require the world pointer (e.g., printing) and for references to global variables.
839 :     *)
840 : jhr 3952 fun mkMethod body = let
841 :     fun chkBlock (T.Block{body, ...}, uG, nW) =
842 :     chkStms (body, uG, nW)
843 :     and chkStms ([], uG, nW) = (uG, nW)
844 :     | chkStms (stm::stms, uG, nW) = let
845 :     fun next (true, true) = (true, true)
846 :     | next (uG, nW) = chkStms (stms, uG, nW)
847 :     in
848 :     case stm
849 :     of T.S_Assign(_, _, e) => next (chkExp (e, uG, nW))
850 :     | T.S_MAssign(_, e) => next (chkExp (e, uG, nW))
851 :     | T.S_GAssign(_, e) => next (chkExp (e, true, nW))
852 :     | T.S_IfThen(e, blk) => let
853 :     val (uG, nW) = chkExp (e, uG, nW)
854 :     in
855 : jhr 3974 next (chkBlock (blk, uG, nW))
856 : jhr 3952 end
857 :     | T.S_IfThenElse(e, blk1, blk2) => let
858 :     val (uG, nW) = chkExp (e, uG, nW)
859 :     val (uG, nW) = chkBlock (blk1, uG, nW)
860 :     in
861 : jhr 3974 next (chkBlock (blk2, uG, nW))
862 : jhr 3952 end
863 :     | T.S_For(_, e1, e2, blk) => let
864 :     val (uG, nW) = chkExp (e1, uG, nW)
865 :     val (uG, nW) = chkExp (e2, uG, nW)
866 :     in
867 : jhr 3974 next (chkBlock (blk, uG, nW))
868 : jhr 3952 end
869 :     | T.S_Foreach(_, e, blk) => let
870 :     val (uG, nW) = chkExp (e, uG, nW)
871 :     in
872 : jhr 3974 next (chkBlock (blk, uG, nW))
873 : jhr 3952 end
874 :     | T.S_LoadNrrd _ => raise Fail "unexpected LoadNrrd"
875 :     | T.S_Input _ => raise Fail "unexpected Input"
876 :     | T.S_InputNrrd _ => raise Fail "unexpected InputNrrd"
877 :     | T.S_New(_, es) => next (chkExps (es, uG, true))
878 :     | T.S_Save(_, e) => next (chkExp (e, uG, nW))
879 :     | T.S_Print(_, es) => next (chkExps (es, uG, true))
880 :     | _ => next (uG, nW)
881 :     (* end case *)
882 :     end
883 :     and chkExps ([], uG, nW) = (uG, nW)
884 :     | chkExps (_, true, true) = (true, true)
885 :     | chkExps (e::es, uG, nW) = let
886 :     val (uG, nW) = chkExp (e, uG, nW)
887 :     in
888 :     chkExps (es, uG, nW)
889 :     end
890 : jhr 3974 and chkExp (T.E_Global gv, _, nW) = (true, nW)
891 :     | chkExp (T.E_State(SOME e, _), uG, nW) = chkExp (e, uG, nW)
892 : jhr 3952 | chkExp (T.E_Op(TOp.SphereQuery _, es), uG, nW) = chkExps (es, uG, true)
893 :     | chkExp (T.E_Op(_, es), uG, nW) = chkExps (es, uG, nW)
894 :     | chkExp (T.E_Vec(_, _, es), uG, nW) = chkExps (es, uG, nW)
895 :     | chkExp (T.E_Cons(es, _), uG, nW) = chkExps (es, uG, nW)
896 :     | chkExp (T.E_Seq(es, _), uG, nW) = chkExps (es, uG, nW)
897 :     | chkExp (T.E_Pack(_, es), uG, nW) = chkExps (es, uG, nW)
898 :     | chkExp (T.E_VLoad(_, e, _), uG, nW) = chkExp (e, uG, nW)
899 :     | chkExp (_, uG, nW) = (uG, nW)
900 :     val (usesGlobals, needsWorld) = chkBlock (body, false, false)
901 :     in
902 :     T.Method{usesGlobals = usesGlobals, needsWorld = needsWorld, body = body}
903 :     end
904 :    
905 : jhr 3851 fun trStrand info strand = let
906 : jhr 3952 val trMethod = mkMethod o trCFG info
907 : jhr 3835 val IR.Strand{name, params, state, stateInit, initM, updateM, stabilizeM} = strand
908 : jhr 3948 val params' = List.map U.newParamVar params
909 : jhr 3835 val state' = List.map getStateVar state
910 : jhr 3857 val stateInit' = let
911 :     val env = Env.new info
912 :     in
913 :     ListPair.appEq
914 : jhr 3948 (fn (x, x') => Env.bindSimple (env, x, T.E_Var x'))
915 : jhr 3857 (params, params');
916 : jhr 3952 mkMethod (ScopeVars.assignScopes (params', trCFGWithEnv (env, stateInit)))
917 : jhr 3857 end
918 : jhr 3835 in
919 :     T.Strand{
920 :     name = name,
921 :     params = params',
922 :     state = state',
923 : jhr 3857 stateInit = stateInit',
924 : jhr 3952 initM = Option.map trMethod initM,
925 :     updateM = trMethod updateM,
926 :     stabilizeM = Option.map trMethod stabilizeM
927 : jhr 3835 }
928 :     end
929 :    
930 : jhr 3851 fun translate (prog, info) = let
931 : jhr 3879 (* first step is to flatten any nested CONS nodes *)
932 :     val prog = Flatten.transform prog
933 : jhr 3834 val LowIR.Program{
934 : jhr 3995 props, consts, inputs, constInit, globals,
935 :     globInit, strand, create, init, update
936 : jhr 3834 } = prog
937 : jhr 3852 val trCFG = trCFG info
938 : jhr 3834 in
939 :     TreeIR.Program{
940 :     props = props,
941 : jhr 3861 target = info,
942 : jhr 3852 consts = List.map mkGlobalVar consts,
943 :     inputs = List.map (Inputs.map mkGlobalVar) inputs,
944 :     constInit = trCFG constInit,
945 :     globals = List.map mkGlobalVar globals,
946 : jhr 3995 globInit = trCFG globInit,
947 : jhr 3851 strand = trStrand info strand,
948 : jhr 4045 create = Create.map trCFG create,
949 : jhr 3995 init = Option.map trCFG init,
950 : jhr 3852 update = Option.map trCFG update
951 : jhr 3834 }
952 :     end
953 :    
954 : jhr 3828 end

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