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

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