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 4092 - (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 :     val [v, lo, hi] = args
372 : jhr 3948 val (layout, vs, stms) = vectorArg (env, v)
373 :     val (lo, stms) = simpleArg env (lo, stms)
374 :     val (hi, stms) = simpleArg env (hi, stms)
375 : jhr 3880 val exps = mkArgs
376 :     (fn (w, p, x) => (TOp.VClamp(w, p), [x, lo, hi]))
377 :     (layout, vs)
378 : jhr 3855 in
379 : jhr 3948 (Env.VEC(layout, exps), 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 :     | Op.Inside(info, s) => bindTREE (TOp.Inside(info, s))
461 :     | Op.ImageDim(info, d) => bindTREE(TOp.ImageDim(info, d))
462 :     | Op.MathFn f => bindTREE (TOp.MathFn f)
463 : jhr 3844 | rator => raise Fail("bogus operator " ^ Op.toString srcRator)
464 : jhr 3829 (* end case *)
465 :     end
466 :    
467 : jhr 3955 (* FIXME: we need to defer the copy when possible *)
468 :     (* if required, add a TensorCopy operation to the rhs of an assignment *)
469 :     fun mkAssignRHS (TTy.TensorTy _, rhs) = (case TreeTypeOf.exp rhs
470 :     of TTy.TensorRefTy shp => T.E_Op(TOp.TensorCopy shp, [rhs])
471 :     | _ => rhs
472 :     (* end case *))
473 :     | mkAssignRHS (_, rhs) = rhs
474 :    
475 :     fun mkDefn' (x, rhs) = mkDefn (x, mkAssignRHS(TV.ty x, rhs))
476 :     fun mkAssign' (x, rhs) = mkAssign (x, mkAssignRHS(TV.ty x, rhs))
477 :    
478 : jhr 3852 (* cases:
479 :     x in EqClass
480 :     issue assignment; lhs is binding of representative (could be multiple vars)
481 :     useCount(x) > 1 and rhs is not simple
482 :     rhs is simple
483 :     rhs is vector
484 :     *)
485 : jhr 4091 fun trAssign (env, lhs, rhs : IR.rhs) = let
486 : jhr 3955 fun getLHS () = (case UnifyVars.eqClassOf lhs of SOME x => x | _ => lhs)
487 : jhr 3889 (* binding for the lhs variable, where the rhs is a simple expression. We check to
488 :     * see if it is part of an merged equivalence class, in which case we need to generate
489 :     * assigment(s)
490 : jhr 3836 *)
491 : jhr 3853 fun bindSimple rhs = (case eqClassRepOf(env, lhs)
492 : jhr 3948 of NOEQ => (Env.bindSimple (env, lhs, rhs); [])
493 : jhr 3955 | VAR x' => [mkAssign' (x', rhs)]
494 : jhr 3889 | VEC xs' => (case V.ty lhs
495 :     of Ty.TensorTy[d] => let
496 :     val layout = Env.layoutVec env d
497 : jhr 4033 val rhs = mkRef rhs
498 : jhr 3889 in
499 :     List.mapi
500 :     (fn (i, x') => mkAssign(x', T.E_VLoad(layout, rhs, i)))
501 :     xs'
502 :     end
503 :     | _ => raise Fail "inconsistent"
504 :     (* end case *))
505 : jhr 3836 (* end case *))
506 : jhr 3948 fun assignOp (rator, args) = let
507 :     val (args, stms) = simpleArgs (env, args)
508 :     in
509 :     case eqClassRepOf(env, lhs)
510 :     of NOEQ => (
511 : jhr 4091 Env.bindVar (env, lhs, Env.RHS(U.trTempType(V.ty lhs), T.E_Op(rator, args)));
512 : jhr 3948 stms)
513 : jhr 3955 | VAR x' => stms @ [mkAssign' (x', T.E_Op(rator, args))]
514 : jhr 3948 | VEC _ => raise Fail ("unexpected VEC for lhs " ^ V.toString lhs)
515 :     (* end case *)
516 :     end
517 : jhr 4091 (* bind the lhs to a tensor cons expression (including Op.Zero) *)
518 :     fun bindCons (args, Ty.TensorTy[d], stms) = let
519 :     val layout = Env.layoutVec env d
520 :     fun mkVecs (args, w::ws) = let
521 :     (* take arguments from args to build a vector value of width w; pad as
522 :     * necessary.
523 :     *)
524 :     fun take (0, args, es) = T.E_Vec(w, w, List.rev es) :: mkVecs (args, ws)
525 :     | take (i, [], es) = if #padded layout andalso null ws
526 :     then [T.E_Vec(w-i, w, List.rev es)]
527 :     else raise Fail "too few arguments for CONS"
528 :     | take (i, arg::args, es) = take (i-1, args, arg :: es)
529 :     in
530 :     take (w, args, [])
531 :     end
532 :     | mkVecs ([], []) = []
533 :     | mkVecs (_, []) = raise Fail "too many arguments for CONS"
534 :     val es = mkVecs (args, #pieces layout)
535 :     in
536 : jhr 4092 case eqClassRepOf(env, lhs)
537 :     of NOEQ => if (V.useCount lhs > 1)
538 :     then (Env.bindVar(env, lhs, Env.VEC(layout, es)); stms)
539 :     else let
540 :     val vs = U.newVectorVars layout
541 :     in
542 :     Env.bindVar (env, lhs, Env.VEC(layout, List.map T.E_Var vs));
543 :     ListPair.foldl (fn (v, e, stms) => mkDefn(v, e)::stms) stms (vs, es)
544 :     end
545 :     | VEC xs =>
546 : jhr 4091 ListPair.foldl (fn (x, e, stms) => mkAssign(x, e)::stms) stms (xs, es)
547 :     | _ => raise Fail "inconsistent"
548 :     (* end case *)
549 :     end
550 :     | bindCons (args, ty as Ty.TensorTy _, stms) = let
551 :     val ty = U.trType ty
552 :     val cons = T.E_Cons(args, ty)
553 :     in
554 :     case eqClassRepOf(env, lhs)
555 : jhr 4092 of NOEQ => if (V.useCount lhs > 1)
556 :     then mkDefn (newMemLocal (env, lhs), cons) :: stms
557 :     else (
558 :     Env.bindVar (env, lhs, Env.RHS(ty, cons));
559 :     stms)
560 : jhr 4091 | VAR x => mkAssign (x, cons) :: stms
561 :     | VEC xs => raise Fail "inconsistent"
562 :     (* end case *)
563 :     end
564 : jhr 3834 in
565 :     case rhs
566 : jhr 3844 of IR.GLOBAL x => bindSimple (T.E_Global(mkGlobalVar x))
567 : jhr 3852 | IR.STATE(NONE, fld) =>
568 :     bindSimple (T.E_State(NONE, getStateVar fld))
569 : jhr 3948 | IR.STATE(SOME x, fld) => let
570 :     val (arg, stms) = simpleArg env (x, [])
571 :     val stms' = bindSimple (T.E_State(SOME arg, getStateVar fld))
572 :     in
573 :     stms' @ stms
574 :     end
575 : jhr 3854 | IR.VAR x => raise Fail "FIXME: VAR"
576 : jhr 3850 | IR.LIT lit => bindSimple (T.E_Lit lit)
577 : jhr 4025 (* FIXME: use the general IR.OP case for eigen-things, since the target info says that they
578 :     * are not inline.
579 :     *)
580 : jhr 3852 | IR.OP(Op.EigenVecs2x2, args) => assignOp (TOp.EigenVecs2x2, args)
581 :     | IR.OP(Op.EigenVecs3x3, args) => assignOp (TOp.EigenVecs3x3, args)
582 :     | IR.OP(Op.EigenVals2x2, args) => assignOp (TOp.EigenVals2x2, args)
583 :     | IR.OP(Op.EigenVals3x3, args) => assignOp (TOp.EigenVals3x3, args)
584 : jhr 3886 | IR.OP(Op.Zero(ty as Ty.TensorTy dd), []) => let
585 : jhr 3887 val z = T.E_Lit(Literal.Real(RealLit.zero false))
586 : jhr 3886 val sz = List.foldl Int.* 1 dd
587 : jhr 3948 in
588 : jhr 4091 bindCons (List.tabulate(sz, fn _ => z), ty, [])
589 : jhr 3948 end
590 : jhr 3886 | IR.OP(Op.LoadSeq(ty, file), []) => let
591 : jhr 3955 val lhs = newLocal (env, getLHS ())
592 : jhr 3886 in
593 : jhr 3948 [T.S_LoadNrrd(lhs, U.toAPIType ty, file)]
594 : jhr 3886 end
595 :     | IR.OP(Op.LoadImage(ty, file), []) => let
596 : jhr 3955 val lhs = newLocal (env, getLHS ())
597 : jhr 3886 in
598 : jhr 3948 [T.S_LoadNrrd(lhs, U.toAPIType ty, file)]
599 : jhr 3886 end
600 : jhr 3852 | IR.OP(rator, args) => let
601 : jhr 3948 val (rhs, stms) = trOp (env, rator, args)
602 : jhr 4025 val emitBind = (V.useCount lhs > 1) orelse not(Env.isInlineOp env rator)
603 : jhr 3852 in
604 : jhr 4025 case (rhs, eqClassRepOf(env, lhs), emitBind)
605 : jhr 3948 of (_, NOEQ, false) => (Env.bindVar (env, lhs, rhs); stms)
606 : jhr 4026 (* FIXME: if the rhs has TensorRef type, then we should make the lhs TensorRef too! *)
607 : jhr 3955 | (Env.TREE e, NOEQ, true) => mkDefn'(newLocal(env, lhs), e) :: stms
608 :     | (Env.TREE e, VAR x', _) => mkAssign'(x', e) :: stms
609 : jhr 3854 | (Env.VEC(layout, es), NOEQ, true) => let
610 : jhr 3948 val vs = U.newVectorVars layout
611 : jhr 3854 in
612 : jhr 3948 Env.bindVar (env, lhs, Env.VEC(layout, List.map T.E_Var vs));
613 :     ListPair.foldl (fn (v, e, stms) => mkDefn(v, e)::stms) stms (vs, es)
614 : jhr 3854 end
615 : jhr 3948 | (Env.VEC(layout, es), VEC xs, _) =>
616 :     ListPair.foldl (fn (x, e, stms) => mkAssign(x, e)::stms) stms (xs, es)
617 : jhr 3854 | _ => raise Fail "inconsistent"
618 :     (* end case *)
619 : jhr 3852 end
620 : jhr 3941 | IR.CONS(args, ty) => let
621 : jhr 4091 val (es, stms) = simpleArgs (env, args)
622 : jhr 3941 in
623 : jhr 4091 bindCons (es, ty, stms)
624 : jhr 3941 end
625 :     | IR.SEQ(args, ty) => let
626 :     val (es, stms) = singleArgs (env, args)
627 : jhr 3948 val ty = U.trType ty
628 : jhr 4067 (* if we are dealing with a sequence of tensors, then we need to copy references *)
629 :     val es = (case ty
630 :     of TTy.SeqTy(TTy.TensorTy _, _) => List.map mkDeref es
631 :     | _ => es
632 :     (* end case *))
633 : jhr 4091 val seq = T.E_Seq(es, ty)
634 : jhr 3941 in
635 : jhr 4091 case eqClassRepOf(env, lhs)
636 : jhr 4092 of NOEQ => if (V.useCount lhs > 1)
637 :     then mkDefn (newMemLocal (env, lhs), seq) :: stms
638 :     else (
639 :     Env.bindVar (env, lhs, Env.RHS(ty, seq));
640 :     stms)
641 : jhr 4091 | VAR x => mkAssign (x, seq) :: stms
642 :     | VEC xs => raise Fail "inconsistent"
643 :     (* end case *)
644 : jhr 3941 end
645 : jhr 3852 | rhs => raise Fail(concat["unexpected ", IR.RHS.toString rhs, " in LowIR code"])
646 : jhr 3834 (* end case *)
647 :     end
648 : jhr 3856 handle ex => (
649 :     print(concat["trAssign: ", V.toString lhs, " = ", IR.RHS.toString rhs, "\n"]);
650 :     raise ex)
651 : jhr 3834
652 :     (* In order to reconstruct the block-structure from the CFG, we keep a stack of open ifs.
653 :     * the items on this stack distinguish between when we are processing the then and else
654 :     * branches of the if.
655 :     *)
656 :     datatype open_if
657 :     (* working on the "then" branch. The fields are statments that preceed the if, the condition,
658 :     * and the else-branch node.
659 :     *)
660 :     = THEN_BR of T.stm list * T.exp * IR.node
661 :     (* working on the "else" branch. The fields are statments that preceed the if, the condition,
662 :     * the "then" branch statements, and the node kind that terminated the "then" branch (will be
663 :     * a JOIN or EXIT(DIE, STABILIZE, or UNREACHABLE)).
664 :     *)
665 :     | ELSE_BR of T.stm list * T.exp * T.stm list * IR.node_kind
666 :    
667 : jhr 3852 fun trCFGWithEnv (env, cfg) = let
668 : jhr 3856 fun useScalar x = (case useVar env x
669 : jhr 3948 of Env.RHS(_, e) => e
670 :     | Env.TREE e => e
671 : jhr 3835 | _ => raise Fail("expected scalar binding for " ^ V.toString x)
672 :     (* end case *))
673 : jhr 4092 (* analyze the CFG *)
674 : jhr 3834 val _ = UnifyVars.analyze cfg
675 : jhr 4092 val _ = AssignTypes.analyze cfg
676 : jhr 3851 (* join (stk, stms, k): handle a control-flow join, where env is the
677 : jhr 3834 * current environment, stk is the stack of open ifs (the top of stk specifies
678 :     * which branch we are in), stms are the TreeIL statements preceding the join
679 :     * on the current path, and k is the kind of the join node (either JOIN or EXIT).
680 :     *)
681 : jhr 3851 fun join ([], _, IR.JOIN _) = raise Fail "JOIN with no open if"
682 : jhr 3852 | join ([], stms, _) = mkBlock (List.rev stms)
683 : jhr 3851 | join (THEN_BR(stms1, cond, elseBr)::stk, thenBlk, k) = let
684 :     val thenBlk = Env.flushPending (env, thenBlk)
685 : jhr 3834 in
686 : jhr 3851 doNode (elseBr, ELSE_BR(stms1, cond, thenBlk, k)::stk, [])
687 : jhr 3834 end
688 : jhr 3851 | join (ELSE_BR(stms, cond, thenBlk, k1)::stk, elseBlk, k2) = let
689 :     val elseBlk = Env.flushPending (env, elseBlk)
690 : jhr 3834 in
691 :     case (k1, k2)
692 : jhr 3848 of (IR.JOIN{succ, ...}, IR.JOIN _) => let
693 : jhr 3834 val stm = mkIf(cond, List.rev thenBlk, List.rev elseBlk)
694 :     in
695 : jhr 3851 doNode (!succ, stk, stm::stms)
696 : jhr 3834 end
697 : jhr 3848 | (IR.JOIN{succ, ...}, _) => let
698 : jhr 3834 val stm = mkIf(cond, List.rev thenBlk, List.rev elseBlk)
699 :     in
700 : jhr 3851 doNode (!succ, stk, stm::stms)
701 : jhr 3834 end
702 : jhr 3848 | (_, IR.JOIN{succ, ...}) => let
703 : jhr 3834 val stm = mkIf(cond, List.rev thenBlk, List.rev elseBlk)
704 :     in
705 : jhr 3851 doNode (!succ, stk, stm::stms)
706 : jhr 3834 end
707 :     | (_, _) => let
708 :     val stm = mkIf(cond, List.rev thenBlk, List.rev elseBlk)
709 :     in
710 : jhr 3852 mkBlock (List.rev(stm::stms))
711 : jhr 3834 end
712 :     (* end case *)
713 :     end
714 : jhr 3851 and doNode (nd : IR.node, ifStk : open_if list, stms) = (case IR.Node.kind nd
715 : jhr 3834 of IR.NULL => raise Fail "unexpected NULL"
716 : jhr 3851 | IR.ENTRY{succ} => doNode (!succ, ifStk, stms)
717 :     | k as IR.JOIN _ => join (ifStk, stms, k)
718 : jhr 3834 | IR.COND{cond, trueBranch, falseBranch, ...} => let
719 : jhr 3835 val cond = useScalar (!cond)
720 : jhr 3851 val stms = Env.flushPending (env, stms)
721 : jhr 3834 in
722 : jhr 3851 doNode (!trueBranch, THEN_BR(stms, cond, !falseBranch)::ifStk, [])
723 : jhr 3834 end
724 : jhr 3835 | IR.FOREACH{var, src, bodyEntry, succ, ...} => let
725 : jhr 3948 val var' = U.newIterVar var
726 :     val _ = Env.bindSimple (env, var, T.E_Var var')
727 : jhr 3924 (* note that the flatten phase on LowIR should guarantee that each
728 :     * FOREACH node that takes a Range argument has its own instance of
729 :     * the Range expression.
730 : jhr 3948 * Note also that we resolve the src scalar before processing the body
731 :     * of the loop so that any nested flushPendings do not cause Range
732 :     * bindings to be flushed.
733 : jhr 3924 *)
734 : jhr 3948 val mkStm = (case useScalar(!src)
735 :     of T.E_Op(TOp.Range, [lo, hi]) =>
736 :     (fn body => T.S_For(var', lo, hi, body))
737 :     | e =>
738 :     (fn body => T.S_Foreach(var', e, body))
739 : jhr 3924 (* end case *))
740 :     val stms = Env.flushPending (env, stms)
741 : jhr 3948 val stm = mkStm (doNode (!bodyEntry, [], []))
742 : jhr 3834 in
743 : jhr 3851 doNode (!succ, ifStk, stm::stms)
744 : jhr 3834 end
745 : jhr 3848 | IR.NEXT _ => mkBlock (List.rev stms)
746 : jhr 3834 | IR.COM {text, succ, ...} =>
747 : jhr 3851 doNode (!succ, ifStk, T.S_Comment text :: stms)
748 : jhr 3834 | IR.ASSIGN{stm=(lhs, rhs), succ, ...} => let
749 : jhr 3852 val stms' = trAssign (env, lhs, rhs)
750 : jhr 3834 in
751 : jhr 3851 doNode (!succ, ifStk, stms' @ stms)
752 : jhr 3834 end
753 : jhr 3866 | IR.MASSIGN{stm=([], Op.Print tys, xs), succ, ...} => let
754 : jhr 3941 val (es, stms') = singleArgs (env, xs)
755 : jhr 4033 (* translate TensorTy to TensorRefTy in the type list *)
756 :     fun trType (Ty.TensorTy(shp as _::_)) = TTy.TensorRefTy shp
757 :     | trType ty = U.trType ty
758 :     val tys = List.map trType tys
759 :     val stm = T.S_Print(tys, List.map mkRef es)
760 : jhr 3848 in
761 : jhr 3941 doNode (!succ, ifStk, stm :: List.revAppend (stms', stms))
762 : jhr 3848 end
763 : jhr 3866 | IR.MASSIGN{stm=(ys, rator, xs), succ, ...} => raise Fail(concat[
764 :     "unexepected operator ", Op.toString rator, " for MASSIGN"
765 :     ])
766 : jhr 3834 | IR.GASSIGN{lhs, rhs, succ, ...} => let
767 : jhr 3948 val gv = mkGlobalVar lhs
768 : jhr 3955 fun mkGAssign (gv, e) = T.S_GAssign(gv, mkAssignRHS(TGV.ty gv, e))
769 : jhr 3948 val stm = (case useVar env rhs
770 : jhr 3955 of Env.RHS(_, e) => mkGAssign(gv, e)
771 :     | Env.TREE e => mkGAssign(gv, e)
772 : jhr 3948 | Env.VEC(layout, es) => let
773 :     val tmp = U.newTempVar("_arg", TTy.TensorTy[#wid layout])
774 :     in
775 :     T.S_GAssign(gv, T.E_Pack(layout, es))
776 :     end
777 :     (* end case *))
778 : jhr 3834 in
779 : jhr 3948 doNode (!succ, ifStk, stm :: stms)
780 : jhr 3834 end
781 : jhr 3851 | IR.NEW{strand, args, succ, ...} => let
782 : jhr 3941 val (es, stms') = singleArgs (env, args)
783 :     val stm = T.S_New(strand, es)
784 : jhr 3851 in
785 : jhr 3941 doNode (!succ, ifStk, stm :: List.revAppend (stms', stms))
786 : jhr 3851 end
787 : jhr 3834 | IR.SAVE{lhs, rhs, succ, ...} => let
788 : jhr 3948 val sv = getStateVar lhs
789 : jhr 3955 fun mkSAssign (sv, e) = T.S_Save(sv, mkAssignRHS(TSV.ty sv, e))
790 : jhr 3948 val stm = (case useVar env rhs
791 : jhr 3955 of Env.RHS(_, e) => mkSAssign(sv, e)
792 :     | Env.TREE e => mkSAssign(sv, e)
793 : jhr 3948 | Env.VEC(layout, es) => let
794 :     val tmp = U.newTempVar("_arg", TTy.TensorTy[#wid layout])
795 :     in
796 :     T.S_Save(sv, T.E_Pack(layout, es))
797 :     end
798 :     (* end case *))
799 : jhr 3834 in
800 : jhr 3948 doNode (!succ, ifStk, stm :: stms)
801 : jhr 3834 end
802 :     | k as IR.EXIT{kind, succ, ...} => (case (!succ, kind)
803 : jhr 3852 of (NONE, ExitKind.RETURN) => mkBlock (List.rev(T.S_Exit :: stms))
804 :     | (NONE, ExitKind.ACTIVE) => mkBlock (List.rev(T.S_Active :: stms))
805 : jhr 3834 | (NONE, ExitKind.STABILIZE) => let
806 :     val stms = T.S_Stabilize :: stms
807 :     in
808 : jhr 3851 join (ifStk, stms, k)
809 : jhr 3834 end
810 : jhr 3851 | (NONE, ExitKind.DIE) => join (ifStk, T.S_Die :: stms, k)
811 :     | (NONE, ExitKind.UNREACHABLE) => join (ifStk, stms, k)
812 :     | (SOME nd, ExitKind.ACTIVE) => doNode (nd, ifStk, T.S_Active :: stms)
813 :     | (SOME nd, ExitKind.STABILIZE) => doNode (nd, ifStk, T.S_Stabilize :: stms)
814 :     | (SOME nd, ExitKind.DIE) => doNode (nd, ifStk, T.S_Die :: stms)
815 :     | (SOME nd, ExitKind.UNREACHABLE) => doNode (nd, ifStk, stms)
816 : jhr 3834 | _ => raise Fail("unexpected continuation edge from "^IR.Node.toString nd)
817 :     (* end case *))
818 :     (* end case *))
819 :     in
820 : jhr 3857 doNode (IR.CFG.entry cfg, [], [])
821 : jhr 3834 end
822 :    
823 : jhr 3857 fun trCFG info cfg = ScopeVars.assignScopes ([], trCFGWithEnv (Env.new info, cfg))
824 : jhr 3835
825 : jhr 3953 (* Build a strand method from a TreeIR block. We need to check for language features
826 :     * that require the world pointer (e.g., printing) and for references to global variables.
827 :     *)
828 : jhr 3952 fun mkMethod body = let
829 :     fun chkBlock (T.Block{body, ...}, uG, nW) =
830 :     chkStms (body, uG, nW)
831 :     and chkStms ([], uG, nW) = (uG, nW)
832 :     | chkStms (stm::stms, uG, nW) = let
833 :     fun next (true, true) = (true, true)
834 :     | next (uG, nW) = chkStms (stms, uG, nW)
835 :     in
836 :     case stm
837 :     of T.S_Assign(_, _, e) => next (chkExp (e, uG, nW))
838 :     | T.S_MAssign(_, e) => next (chkExp (e, uG, nW))
839 :     | T.S_GAssign(_, e) => next (chkExp (e, true, nW))
840 :     | T.S_IfThen(e, blk) => let
841 :     val (uG, nW) = chkExp (e, uG, nW)
842 :     in
843 : jhr 3974 next (chkBlock (blk, uG, nW))
844 : jhr 3952 end
845 :     | T.S_IfThenElse(e, blk1, blk2) => let
846 :     val (uG, nW) = chkExp (e, uG, nW)
847 :     val (uG, nW) = chkBlock (blk1, uG, nW)
848 :     in
849 : jhr 3974 next (chkBlock (blk2, uG, nW))
850 : jhr 3952 end
851 :     | T.S_For(_, e1, e2, blk) => let
852 :     val (uG, nW) = chkExp (e1, uG, nW)
853 :     val (uG, nW) = chkExp (e2, uG, nW)
854 :     in
855 : jhr 3974 next (chkBlock (blk, uG, nW))
856 : jhr 3952 end
857 :     | T.S_Foreach(_, e, blk) => let
858 :     val (uG, nW) = chkExp (e, uG, nW)
859 :     in
860 : jhr 3974 next (chkBlock (blk, uG, nW))
861 : jhr 3952 end
862 :     | T.S_LoadNrrd _ => raise Fail "unexpected LoadNrrd"
863 :     | T.S_Input _ => raise Fail "unexpected Input"
864 :     | T.S_InputNrrd _ => raise Fail "unexpected InputNrrd"
865 :     | T.S_New(_, es) => next (chkExps (es, uG, true))
866 :     | T.S_Save(_, e) => next (chkExp (e, uG, nW))
867 :     | T.S_Print(_, es) => next (chkExps (es, uG, true))
868 :     | _ => next (uG, nW)
869 :     (* end case *)
870 :     end
871 :     and chkExps ([], uG, nW) = (uG, nW)
872 :     | chkExps (_, true, true) = (true, true)
873 :     | chkExps (e::es, uG, nW) = let
874 :     val (uG, nW) = chkExp (e, uG, nW)
875 :     in
876 :     chkExps (es, uG, nW)
877 :     end
878 : jhr 3974 and chkExp (T.E_Global gv, _, nW) = (true, nW)
879 :     | chkExp (T.E_State(SOME e, _), uG, nW) = chkExp (e, uG, nW)
880 : jhr 3952 | chkExp (T.E_Op(TOp.SphereQuery _, es), uG, nW) = chkExps (es, uG, true)
881 :     | chkExp (T.E_Op(_, es), uG, nW) = chkExps (es, uG, nW)
882 :     | chkExp (T.E_Vec(_, _, es), uG, nW) = chkExps (es, uG, nW)
883 :     | chkExp (T.E_Cons(es, _), uG, nW) = chkExps (es, uG, nW)
884 :     | chkExp (T.E_Seq(es, _), uG, nW) = chkExps (es, uG, nW)
885 :     | chkExp (T.E_Pack(_, es), uG, nW) = chkExps (es, uG, nW)
886 :     | chkExp (T.E_VLoad(_, e, _), uG, nW) = chkExp (e, uG, nW)
887 :     | chkExp (_, uG, nW) = (uG, nW)
888 :     val (usesGlobals, needsWorld) = chkBlock (body, false, false)
889 :     in
890 :     T.Method{usesGlobals = usesGlobals, needsWorld = needsWorld, body = body}
891 :     end
892 :    
893 : jhr 3851 fun trStrand info strand = let
894 : jhr 3952 val trMethod = mkMethod o trCFG info
895 : jhr 3835 val IR.Strand{name, params, state, stateInit, initM, updateM, stabilizeM} = strand
896 : jhr 3948 val params' = List.map U.newParamVar params
897 : jhr 3835 val state' = List.map getStateVar state
898 : jhr 3857 val stateInit' = let
899 :     val env = Env.new info
900 :     in
901 :     ListPair.appEq
902 : jhr 3948 (fn (x, x') => Env.bindSimple (env, x, T.E_Var x'))
903 : jhr 3857 (params, params');
904 : jhr 3952 mkMethod (ScopeVars.assignScopes (params', trCFGWithEnv (env, stateInit)))
905 : jhr 3857 end
906 : jhr 3835 in
907 :     T.Strand{
908 :     name = name,
909 :     params = params',
910 :     state = state',
911 : jhr 3857 stateInit = stateInit',
912 : jhr 3952 initM = Option.map trMethod initM,
913 :     updateM = trMethod updateM,
914 :     stabilizeM = Option.map trMethod stabilizeM
915 : jhr 3835 }
916 :     end
917 :    
918 : jhr 3851 fun translate (prog, info) = let
919 : jhr 3879 (* first step is to flatten any nested CONS nodes *)
920 :     val prog = Flatten.transform prog
921 : jhr 3834 val LowIR.Program{
922 : jhr 3995 props, consts, inputs, constInit, globals,
923 :     globInit, strand, create, init, update
924 : jhr 3834 } = prog
925 : jhr 3852 val trCFG = trCFG info
926 : jhr 3834 in
927 :     TreeIR.Program{
928 :     props = props,
929 : jhr 3861 target = info,
930 : jhr 3852 consts = List.map mkGlobalVar consts,
931 :     inputs = List.map (Inputs.map mkGlobalVar) inputs,
932 :     constInit = trCFG constInit,
933 :     globals = List.map mkGlobalVar globals,
934 : jhr 3995 globInit = trCFG globInit,
935 : jhr 3851 strand = trStrand info strand,
936 : jhr 4045 create = Create.map trCFG create,
937 : jhr 3995 init = Option.map trCFG init,
938 : jhr 3852 update = Option.map trCFG update
939 : jhr 3834 }
940 :     end
941 :    
942 : jhr 3828 end

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