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

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