1 |
(* low-to-tree.sml |
(* low-to-tree-fn.sml |
2 |
* |
* |
3 |
* COPYRIGHT (c) 2011 The Diderot Project (http://diderot-language.cs.uchicago.edu) |
* COPYRIGHT (c) 2011 The Diderot Project (http://diderot-language.cs.uchicago.edu) |
4 |
* All rights reserved. |
* All rights reserved. |
9 |
* NOTE: this translation is pretty dumb about variable coalescing (i.e., it doesn't do any). |
* NOTE: this translation is pretty dumb about variable coalescing (i.e., it doesn't do any). |
10 |
*) |
*) |
11 |
|
|
12 |
structure LowToTree : sig |
functor LowToTreeFn (Target : TARGET) : sig |
13 |
|
|
14 |
val translate : LowIL.program -> TreeIL.program |
val translate : LowIL.program -> TreeIL.program |
15 |
|
|
158 |
|
|
159 |
end |
end |
160 |
|
|
161 |
|
(* Certain IL operators cannot be compiled to inline expressions. Return |
162 |
|
* false for those and true for all others. |
163 |
|
*) |
164 |
|
fun isInlineOp rator = (case rator |
165 |
|
of Op.LoadVoxels(_, 1) => true |
166 |
|
| Op.LoadVoxels _ => false |
167 |
|
| Op.Add(Ty.TensorTy(_::_::_)) => Target.inlineMatrixExp |
168 |
|
| Op.Sub(Ty.TensorTy(_::_::_)) => Target.inlineMatrixExp |
169 |
|
| Op.Scale(Ty.TensorTy(_::_::_)) => Target.inlineMatrixExp |
170 |
|
| Op.MulMatMat _ => Target.inlineMatrixExp |
171 |
|
| Op.Identity _ => Target.inlineMatrixExp |
172 |
|
| Op.Zero _ => Target.inlineMatrixExp |
173 |
|
| _ => true |
174 |
|
(* end case *)) |
175 |
|
|
176 |
(* translate a LowIL assignment to a list of zero or more target statements *) |
(* translate a LowIL assignment to a list of zero or more target statements *) |
177 |
fun doAssign (env, (lhs, rhs)) = let |
fun doAssign (env, (lhs, rhs)) = let |
178 |
fun doLHS () = (case peekGlobal(env, lhs) |
fun doLHS () = (case peekGlobal(env, lhs) |
183 |
(rename (addLocal(env, t), lhs, t), t) |
(rename (addLocal(env, t), lhs, t), t) |
184 |
end |
end |
185 |
(* end case *)) |
(* end case *)) |
186 |
|
(* for expressions that are going to be compiled to a call statement *) |
187 |
|
fun assignExp (env, exp) = let |
188 |
|
(* operations that return matrices may not be supported inline *) |
189 |
|
val (env, t) = doLHS() |
190 |
|
in |
191 |
|
(env, [T.S_Assign(t, exp)]) |
192 |
|
end |
193 |
in |
in |
194 |
case rhs |
case rhs |
195 |
of IL.VAR x => setDef (env, lhs, useVar env x) |
of IL.VAR x => setDef (env, lhs, useVar env x) |
209 |
in |
in |
210 |
(env, [T.S_Input(t, name, SOME(useVar env a))]) |
(env, [T.S_Input(t, name, SOME(useVar env a))]) |
211 |
end |
end |
212 |
| IL.OP(rator as Op.LoadVoxels(_, 1), [a]) => |
| IL.OP(rator, args) => let |
213 |
bind (env, lhs, T.E_Op(rator, [useVar env a])) |
val exp = T.E_Op(rator, List.map (useVar env) args) |
|
| IL.OP(Op.LoadVoxels(info, n), [a]) => let |
|
|
(* loading multiple values from memory may not be supported inline *) |
|
|
val (env, t) = doLHS() |
|
214 |
in |
in |
215 |
(env, [T.S_LoadVoxels(t, n, useVar env a)]) |
if isInlineOp rator |
216 |
|
then bind (env, lhs, exp) |
217 |
|
else assignExp (env, exp) |
218 |
end |
end |
|
| IL.OP(rator as Op.MulMatMat _, args) => let |
|
|
(* operations that return matrices may not be supported inline *) |
|
|
val (env, t) = doLHS() |
|
|
in |
|
|
(env, [T.S_Assign(t, T.E_Op(rator, List.map (useVar env) args))]) |
|
|
end |
|
|
| IL.OP(rator, args) => |
|
|
bind (env, lhs, T.E_Op(rator, List.map (useVar env) args)) |
|
219 |
| IL.APPLY(f, args) => |
| IL.APPLY(f, args) => |
220 |
bind (env, lhs, T.E_Apply(f, List.map (useVar env) args)) |
bind (env, lhs, T.E_Apply(f, List.map (useVar env) args)) |
221 |
| IL.CONS(ty, args) => let |
| IL.CONS(ty, args) => let |
222 |
(* we give cons expressions names, since not all targets support them inline *) |
val inline = (case ty |
223 |
val (env, t) = doLHS() |
of Ty.IVecTy _ => true |
224 |
val rhs = List.map (useVar env) args |
| Ty.TensorTy dd => Target.inlineCons(List.length dd) |
225 |
|
(* end case *)) |
226 |
|
val exp = T.E_Cons(ty, List.map (useVar env) args) |
227 |
in |
in |
228 |
(env, [T.S_Cons(t, rhs)]) |
if inline |
229 |
|
then bind (env, lhs, exp) |
230 |
|
else assignExp (env, exp) |
231 |
end |
end |
232 |
(* end case *) |
(* end case *) |
233 |
end |
end |