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

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