Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] Annotation of /branches/vis15/src/compiler/translate/translate-basis.sml
ViewVC logotype

Annotation of /branches/vis15/src/compiler/translate/translate-basis.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3498 - (view) (download)

1 : jhr 3471 (* translate-basis.sml
2 :     *
3 :     * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
4 :     *
5 :     * COPYRIGHT (c) 2015 The University of Chicago
6 :     * All rights reserved.
7 :     *
8 : jhr 3478 * Translation for basis operations in Simple AST to HighIR code
9 : jhr 3471 *)
10 :    
11 :     structure TranslateBasis : sig
12 :    
13 :     (* translate(lhs, f, mvs, args) translates the application of f (specialized
14 :     * to the instantiated meta variables mvs) to a list of SSA assignments in
15 :     * reverse order.
16 :     *)
17 : jhr 3478 val translate : (HighIR.var * Var.t * SimpleTypes.meta_arg list * HighIR.var list)
18 :     -> HighIR.assignment list
19 : jhr 3471
20 :     end = struct
21 :    
22 :     structure BV = BasisVars
23 : jhr 3478 structure IR = HighIR
24 :     structure DstTy = HighTypes
25 : jhr 3471 structure Op = HighOps
26 :     structure Ty = SimpleTypes
27 :     structure VTbl = Var.Tbl
28 : jhr 3478 structure Mk = MkOperators
29 : jhr 3471
30 :     fun trType (Ty.TY ty) = TranslateTy.tr ty
31 :     | trType _ = raise Fail "expected type"
32 :     fun dimVarToInt (Ty.DIM d) = d
33 :     | dimVarToInt _ = raise Fail "expected dim"
34 :     fun dimVarToTensor dv = DstTy.tensorTy[dimVarToInt dv]
35 :     fun dimVarToMatrix dv = let
36 :     val d = dimVarToInt dv
37 :     in
38 :     DstTy.tensorTy[d, d] (* square matrix type *)
39 :     end
40 :     fun shapeVarToTensor (Ty.SHAPE shp) = DstTy.tensorTy shp
41 :     | shapeVarToTensor _ = raise Fail "expected shape"
42 :    
43 : jhr 3478 fun assign (y, rator, xs) = [IR.ASSGN(y, IR.OP(rator, xs))]
44 : jhr 3471
45 :     fun simpleOp rator (y, [], xs) = assign (y, rator, xs)
46 :    
47 :     fun tensorOp rator (y, [sv], xs) = assign (y, rator(shapeVarToTensor sv), xs)
48 :    
49 :     fun vectorOp rator (y, [dv], xs) = assign (y, rator(dimVarToTensor dv), xs)
50 :    
51 :     fun kernel h (y, [], []) = assign(y, Op.Kernel(h, 0), [])
52 :    
53 :     (* utility functions for synthesizing eigenvector/eigenvalue code *)
54 :     fun eigenVec (rator, dim) = let
55 : jhr 3478 val ty = DstTy.SeqTy(DstTy.realTy, SOME dim)
56 : jhr 3471 in
57 :     fn (y, _, [m]) => let
58 : jhr 3478 val v = IR.Var.new("evals", ty)
59 : jhr 3471 in
60 : jhr 3478 [IR.MASSGN([v, y], rator, [m])]
61 : jhr 3471 end
62 :     end
63 :     fun eigenVal (rator, dim) = let
64 : jhr 3478 val ty = DstTy.SeqTy(DstTy.vecTy dim, SOME dim)
65 : jhr 3471 in
66 :     fn (y, _, [m]) => let
67 : jhr 3478 val v = IR.Var.new("evecs", ty)
68 : jhr 3471 in
69 : jhr 3478 [IR.MASSGN([y, v], rator, [m])]
70 : jhr 3471 end
71 :     end
72 :    
73 : jhr 3478 fun assignEin (y, rator, xs) = IR.ASSGN(y, IR.EINAPP(rator, xs))
74 :    
75 :     fun simpleEOp rator (y, _, xs) = [assignEin(y, rator, xs)]
76 :    
77 : jhr 3498 fun dist (y, d, xs) = let
78 :     val t0 = IR.Var.new("t0", DstTy.TensorTy[d])
79 :     in [
80 :     assignEin(t0, Mk.subTT[d], xs),
81 :     assignEin(y, Mk.normT[d], [t0])]
82 :     end
83 : cchiw 3494
84 : jhr 3471 (* build a table that maps Basis variables to their translation functions *)
85 : jhr 3478 val tbl : ((IR.var * Ty.meta_arg list * IR.var list) -> IR.assignment list) VTbl.hash_table = let
86 : jhr 3471 val tbl = VTbl.mkTable (128, Fail "Translate table")
87 : jhr 3478 fun insert (id, def) = (case VTbl.find tbl id
88 : jhr 3498 of NONE => VTbl.insert tbl (id, def)
89 :     | SOME _ => raise Fail("duplicate definition of " ^ Var.nameOf id)
90 :     (* end case *))
91 : jhr 3471 in
92 :     List.app insert [
93 :     (BV.at_Td, fn (y, [tv], args) => assign(y, Op.Prepend(trType tv), args)),
94 :     (BV.at_dT, fn (y, [tv], args) => assign(y, Op.Append(trType tv), args)),
95 :     (BV.at_dd, fn (y, [tv], args) => assign(y, Op.Concat(trType tv), args)),
96 :     (BV.lt_ii, simpleOp(Op.LT DstTy.IntTy)),
97 :     (BV.lt_rr, simpleOp(Op.LT DstTy.realTy)),
98 :     (BV.lte_ii, simpleOp(Op.LTE DstTy.IntTy)),
99 :     (BV.lte_rr, simpleOp(Op.LTE DstTy.realTy)),
100 :     (BV.gte_ii, simpleOp(Op.GTE DstTy.IntTy)),
101 :     (BV.gte_rr, simpleOp(Op.GTE(DstTy.realTy))),
102 :     (BV.gt_ii, simpleOp(Op.GT DstTy.IntTy)),
103 :     (BV.gt_rr, simpleOp(Op.GT(DstTy.realTy))),
104 :     (BV.equ_bb, simpleOp(Op.EQ DstTy.BoolTy)),
105 :     (BV.equ_ii, simpleOp(Op.EQ DstTy.IntTy)),
106 :     (BV.equ_ss, simpleOp(Op.EQ DstTy.StringTy)),
107 :     (BV.equ_rr, simpleOp(Op.EQ(DstTy.realTy))),
108 :     (BV.neq_bb, simpleOp(Op.NEQ DstTy.BoolTy)),
109 :     (BV.neq_ii, simpleOp(Op.NEQ DstTy.IntTy)),
110 :     (BV.neq_ss, simpleOp(Op.NEQ DstTy.StringTy)),
111 :     (BV.neq_rr, simpleOp(Op.NEQ(DstTy.realTy))),
112 : jhr 3476 (BV.add_ii, simpleOp Op.IAdd),
113 :     (BV.sub_ii, simpleOp Op.ISub),
114 :     (BV.mul_ii, simpleOp Op.IMul),
115 :     (BV.div_ii, simpleOp Op.IDiv),
116 : jhr 3478 (BV.op_mod, simpleOp Op.IMod),
117 :     (BV.neg_i, simpleOp Op.INeg),
118 :     (BV.add_tt, fn (y, [shp], xs) => let
119 :     val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor shp
120 :     val rator = Mk.addTT(dd1)
121 :     in
122 :     [assignEin(y, rator, xs)]
123 :     end),
124 :     (BV.add_ff, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], xs) =>
125 :     [assignEin(y, Mk.addFF(d, dd), xs)]),
126 :     (BV.add_ft, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], [f, s]) =>
127 :     [assignEin(y, Mk.addTF(d, dd), [s, f])]),
128 :     (BV.add_tf, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], xs) =>
129 :     [assignEin(y, Mk.addTF(d, dd), xs)]),
130 :     (BV.sub_tt, fn (y, [shp], xs) => let
131 :     val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor shp
132 :     val rator = Mk.subTT dd1
133 :     in
134 :     [assignEin(y, rator, xs)]
135 :     end),
136 :     (BV.sub_ff, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], xs) =>
137 :     [assignEin(y, Mk.subFF(d, dd), xs)]),
138 :     (BV.sub_ft, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], [x1, x2]) =>
139 :     [assignEin(y, Mk.subFT(d, dd), [x2, x1])]),
140 :     (BV.sub_tf, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], xs) =>
141 :     [assignEin(y, Mk.subTF(d, dd), xs)]),
142 :     (BV.mul_rr, fn (y, _, args) => [assignEin(y, Mk.mulRR, args)]),
143 :     (BV.mul_rt, fn (y, [shp], xs) => let
144 :     val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor shp
145 :     val rator = Mk.mulRT(dd1)
146 :     in
147 :     [assignEin(y, rator, xs)]
148 :     end),
149 :     (BV.mul_tr, fn (y, [shp], [t, r]) => let
150 :     val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor shp
151 :     val rator = Mk.mulRT dd1
152 :     in
153 :     [assignEin(y, rator, [r, t])]
154 :     end),
155 :     (BV.mul_rf, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], xs) =>
156 :     [assignEin(y, Mk.mulRF(d, dd), xs)]),
157 :     (BV.mul_fr, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], [f, s]) =>
158 :     [assignEin(y, Mk.mulRF(d, dd), [s, f])]),
159 :     (BV.mul_ss, fn (y, [_, Ty.DIM d], xs) =>
160 :     [assignEin(y, Mk.mulSS d, xs)]),
161 :     (BV.mul_sf, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], xs) =>
162 :     [assignEin(y, Mk.mulSF(d, dd), xs)]),
163 :     (BV.mul_fs, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], [s, f]) =>
164 :     [assignEin(y, Mk.mulSF(d, dd), [f, s])]),
165 :     (BV.mul_st, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], xs) =>
166 :     [assignEin(y, Mk.mulST(d, dd), List.rev xs)]),
167 :     (BV.mul_ts, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], xs) =>
168 :     [assignEin(y, Mk.mulST(d, dd), xs)]),
169 :     (BV.div_rr, fn (y, _, args) => [assignEin(y, Mk.divRR, args)]),
170 :     (BV.div_tr, fn (y, [shp], xs) => let
171 :     val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor shp
172 :     val rator = Mk.divTR dd1
173 :     in
174 :     [assignEin(y, rator, xs)]
175 :     end),
176 :     (BV.div_fr, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], xs) =>
177 :     [assignEin(y, Mk.divFR(d, dd), xs)]),
178 :     (BV.div_fs, fn (y, [_, _, Ty.DIM d, Ty.SHAPE dd], xs) =>
179 :     [assignEin(y, Mk.divFS(d, dd), xs)]),
180 :     (BV.div_ss, fn (y, [_, Ty.DIM d], xs) =>
181 :     [assignEin(y, Mk.divSS d, xs)]),
182 : jhr 3482 (BV.pow_ri, simpleOp Op.Power),
183 : jhr 3483 (* FIXME: BV.pos_rr *)
184 : jhr 3498 (BV.pow_si, fn (y, [_, Ty.DIM d1], [f, n]) => let
185 : jhr 3483 fun getN x = (case IR.Var.binding x
186 :     of IR.VB_RHS(IR.LIT(Literal.Int n)) => IntInf.toInt n
187 :     | vb => raise Fail (String.concat[
188 :     (* FIXME: should catch this earlier, where we can generate an error message! *)
189 :     "Not a constant integer, ", IR.Var.toString x,
190 :     " found ", IR.vbToString vb, "\n"
191 :     ])
192 :     (* end case *))
193 :     in
194 :     [assignEin(y, Mk.powF(d1, getN n), [f])]
195 :     end),
196 : jhr 3478 (BV.curl2D, simpleEOp Mk.curl2d),
197 :     (BV.curl3D, simpleEOp Mk.curl3d),
198 : jhr 3471 (BV.convolve_vk, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], xs) =>
199 : jhr 3478 [assignEin(y, Mk.conv(d, dd), xs)]),
200 :     (BV.convolve_kv, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], xs) =>
201 :     [assignEin(y, Mk.conv(d, dd), xs)]),
202 :     (BV.neg_t, fn (y, [shp], xs) => let
203 :     val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor shp
204 :     val rator = Mk.negTT dd1
205 :     in
206 :     [assignEin(y, rator, xs)]
207 :     end),
208 :     (BV.neg_f, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], xs) =>
209 :     [assignEin(y, Mk.negFF(d, dd), xs)]),
210 :     (BV.op_probe, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], xs) =>
211 :     [assignEin(y, (Mk.probe(dd, d)), xs)]),
212 :     (BV.op_D, fn (y, [_, Ty.DIM d], xs) =>
213 :     if (d = 2) orelse (d = 3)
214 :     then [assignEin(y, Mk.grad [d], xs)]
215 :     else raise Fail "unsupported gradient"),
216 :     (BV.op_Dotimes, fn (y, [_, Ty.DIM d1, Ty.SHAPE dd, Ty.DIM d2], xs) =>
217 :     [assignEin(y, Mk.dotimes(d1, dd@[d2]), xs)]),
218 :     (BV.op_Ddot, fn (y, [_, Ty.DIM d1, Ty.SHAPE dd, Ty.DIM d2], xs) =>
219 :     [assignEin(y, Mk.divergence(d1, dd), xs)] ),
220 : jhr 3498 (BV.op_norm_t, fn (y, [sv], xs) => let
221 :     val DstTy.TensorTy dd1 = shapeVarToTensor sv
222 :     in
223 :     case (dd1,xs)
224 :     of ([], [arg0]) => [IR.ASSGN(y, IR.VAR arg0)]
225 :     | _ => [assignEin(y, Mk.normT dd1, xs)]
226 :     (* end case *)
227 :     end),
228 :     (BV.op_norm_f, fn (y, [ _,Ty.DIM d1, Ty.SHAPE dd1], xs) => (case (dd1, xs)
229 :     of ([], [arg0]) => [IR.ASSGN(y, IR.VAR arg0)]
230 :     | _ => [assignEin(y, Mk.normF(d1, dd1), xs)]
231 :     (* end case *))),
232 : jhr 3478 (BV.op_not, simpleOp Op.Not),
233 :     (BV.op_cross3_tt, simpleEOp Mk.cross3TT),
234 :     (BV.op_cross2_tt, simpleEOp Mk.cross2TT),
235 :     (BV.op_cross2_ff, simpleEOp Mk.cross2FF),
236 :     (BV.op_cross3_ff, simpleEOp Mk.cross3FF),
237 : jhr 3498 (* FIXME: need to update BasisVars *)
238 :     (BV.op_outer_tt, fn (y, [Ty.DIM d1, Ty.DIM d2], xs) =>
239 :     [assignEin(y, Mk.outerTT([d1], [d2]), xs)]),
240 :     (BV.op_outer_mt, fn (y, [Ty.DIM d1, Ty.DIM d2,Ty.DIM d3], xs) =>
241 :     [assignEin(y, Mk.outerTT([d1, d2], [d3]), xs)]),
242 :     (BV.op_outer_tm, fn (y, [Ty.DIM d1, Ty.DIM d2, Ty.DIM d3], xs) =>
243 :     [assignEin(y, Mk.outerTT([d1], [d2, d3]), xs)]),
244 :     (BV.op_outer_ff, fn (y, [_, Ty.DIM d1, Ty.DIM i, Ty.DIM j], xs)=>
245 :     [assignEin(y, Mk.outerFF(d1, [i], [j]), xs)]),
246 : jhr 3478 (BV.op_inner_tt, fn (y, [sh1, sh2, _], xs) => let
247 :     val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor sh1
248 :     val ty2 as DstTy.TensorTy dd2 = shapeVarToTensor sh2
249 :     in
250 :     [assignEin(y, (Mk.innerTT(dd1, dd2)), xs)]
251 :     end),
252 :     (BV.op_inner_tf, fn (y, [_, Ty.DIM d, sh1, Ty.SHAPE dd2, _], xs) =>let
253 :     val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor sh1
254 : cchiw 3496 in
255 : jhr 3478 [assignEin(y, Mk.innerTF(dd1, d, dd2), xs)]
256 :     end),
257 :     (BV.op_inner_ft, fn (y, [_, Ty.DIM d, Ty.SHAPE dd1, sh2, _], xs) =>let
258 :     val ty1 as DstTy.TensorTy dd2 = shapeVarToTensor sh2
259 :     in
260 :     [assignEin(y, Mk.innerFT(dd1, d, dd2), xs)]
261 :     end),
262 :     (BV.op_inner_ff, fn (y, [_, _, Ty.DIM d, Ty.SHAPE dd1, Ty.SHAPE dd2, _], xs) =>
263 :     [assignEin(y, Mk.innerFF(dd1, d, dd2), xs)]),
264 :     (BV.op_colon_tt, fn (y, [sh1, sh2, _], xs) => let
265 :     val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor sh1
266 :     val ty2 as DstTy.TensorTy dd2 = shapeVarToTensor sh2
267 :     in
268 :     [assignEin(y, Mk.colonTT(dd1, dd2), xs)]
269 :     end),
270 :     (BV.op_colon_ft, fn (y, [_, Ty.SHAPE dd1, Ty.DIM d, Ty.SHAPE dd2, _], xs) =>
271 :     [assignEin(y, Mk.colonFT(d, dd1, dd2), xs)]),
272 :     (BV.op_colon_tf, fn (y, [_, Ty.SHAPE dd1, Ty.DIM d, Ty.SHAPE dd2, _], xs) =>
273 :     [assignEin(y, Mk.colonTF(d, dd1, dd2), xs)]),
274 :     (BV.op_colon_ff, fn (y, [_, Ty.SHAPE dd1, Ty.DIM d, Ty.SHAPE dd2, _], xs) =>
275 :     [assignEin(y, Mk.colonFF(d, dd1, dd2), xs)]),
276 :     (* modulate is vector * vector pointwise multiplication *)
277 :     (BV.fn_modulate, fn (y, [Ty.DIM dd1], xs) =>
278 :     [assignEin(y, (Mk.modulate dd1), xs)]),
279 : jhr 3498 (BV.fn_normalize_t, fn (y, [shp], xs) => let
280 :     val DstTy.TensorTy dd1 = shapeVarToTensor shp
281 :     in
282 :     case (dd1, xs)
283 :     (* FIXME: this assignment is incorrect (there should be an absolute-value), but I don't
284 :     * think that this case should ever arise. Same for fn_normalize_f.
285 :     *)
286 :     of ([], [x]) => [IR.ASSGN(y, IR.VAR x)]
287 :     | (_, [_]) => [assignEin(y, Mk.normalizeTT dd1, xs)]
288 :     (* end case *)
289 :     end),
290 :     (BV.fn_normalize_f, fn (y, [ _,Ty.DIM d1, Ty.SHAPE dd1], xs) => (case (dd1, xs)
291 :     of ([], [arg0]) => [IR.ASSGN(y, IR.VAR arg0)]
292 :     | (_, [arg0]) => [assignEin(y, Mk.normalizeFF(d1, dd1), xs@xs)]
293 :     (* end case *))),
294 : jhr 3478 (BV.fn_trace_t, fn (y, [Ty.DIM d], xs) =>
295 :     [assignEin(y, (Mk.traceT d), xs)]),
296 :     (BV.fn_trace_f, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], xs) =>
297 :     [assignEin(y, Mk.traceF(d, dd), xs)]),
298 :     (BV.fn_transpose_t, fn (y, [Ty.DIM d1, Ty.DIM d2], xs) =>
299 :     [assignEin(y, (Mk.transposeT [d1, d2]), xs)]),
300 :     (BV.fn_transpose_f, fn (y, [_, Ty.DIM d1, Ty.DIM d2, Ty.DIM d3], xs) =>
301 :     [assignEin(y, (Mk.transposeF (d1, d2, d3)), xs)]),
302 :     (BV.fn_det2_t, simpleEOp (Mk.det2T)),
303 :     (BV.fn_det3_t, simpleEOp (Mk.det3T)),
304 :     (BV.fn_det2_f, simpleEOp (Mk.det2F)),
305 :     (BV.fn_det3_f, simpleEOp (Mk.det3F)),
306 : jhr 3498 (BV.fn_sqrt_r, fn (y, _, xs) =>
307 :     [assignEin(y, Mk.sqrtR, xs)]),
308 : jhr 3483 (BV.fn_sqrt_s, fn (y, [_, Ty.DIM d1], xs) =>
309 : jhr 3478 [assignEin(y, Mk.sqrtF d1, xs)]),
310 : cchiw 3494 (BV.fn_cos_r, fn (y, _, xs) =>
311 : jhr 3498 [assignEin(y, Mk.cosR, xs)]),
312 : jhr 3483 (BV.fn_cos_s, fn (y, [_, Ty.DIM d1], xs) =>
313 : jhr 3478 [assignEin(y, Mk.cosF d1, xs)]),
314 : cchiw 3494 (BV.fn_acos_r, fn (y, _, xs) =>
315 :     [assignEin(y, Mk.acosR, xs)]),
316 : jhr 3483 (BV.fn_acos_s, fn (y, [_, Ty.DIM d1], xs) =>
317 : jhr 3478 [assignEin(y, Mk.acosF d1, xs)]),
318 : cchiw 3494 (BV.fn_sin_r, fn (y, _, xs) =>
319 : jhr 3498 [assignEin(y, Mk.sinR, xs)]),
320 : jhr 3483 (BV.fn_sin_s, fn (y, [_, Ty.DIM d1], xs) =>
321 : jhr 3478 [assignEin(y, Mk.sinF d1, xs)]),
322 : jhr 3498 (BV.fn_asin_r, fn (y, _, xs) =>
323 :     [assignEin(y, Mk.asinR, xs)]),
324 : jhr 3483 (BV.fn_asin_s, fn (y, [_, Ty.DIM d1], xs) =>
325 : jhr 3478 [assignEin(y, Mk.asinF d1, xs)]),
326 : cchiw 3494 (BV.fn_tan_r, fn (y, _, xs) =>
327 : jhr 3498 [assignEin(y, Mk.tanR, xs)]),
328 : jhr 3483 (BV.fn_tan_s, fn (y, [_, Ty.DIM d1], xs) =>
329 : jhr 3478 [assignEin(y, Mk.tanF d1, xs)]),
330 : jhr 3498 (BV.fn_atan_r, fn (y, _, xs) =>
331 :     [assignEin(y, Mk.atanR, xs)]),
332 : jhr 3483 (BV.fn_atan_s, fn (y, [_, Ty.DIM d1], xs) =>
333 : jhr 3478 [assignEin(y, Mk.atanF d1, xs)]),
334 : jhr 3483 (BV.fn_exp_r, fn (y, _, xs) =>
335 :     [assignEin(y, Mk.expT, xs)]),
336 :     (BV.fn_exp_s, fn (y, [_, Ty.DIM d1], xs) =>
337 : jhr 3478 [assignEin(y, Mk.expF d1, xs)]),
338 :     (BV.fn_principleEvec, vectorOp Op.PrincipleEvec),
339 :     (BV.fn_inside, fn (y, [_, Ty.DIM d, _], xs) => assign(y, Op.Inside d, xs)),
340 : jhr 3471 (BV.clamp_rrr, simpleOp (Op.Clamp DstTy.realTy)),
341 :     (BV.clamp_vvv, vectorOp Op.Clamp),
342 :     (BV.lerp3, tensorOp Op.Lerp),
343 :     (BV.lerp5, fn (y, [sv], [a, b, x0, x, x1]) => let
344 : jhr 3478 val t1 = IR.Var.new("t1", DstTy.realTy)
345 :     val t2 = IR.Var.new("t2", DstTy.realTy)
346 :     val t3 = IR.Var.new("t3", DstTy.realTy)
347 : jhr 3471 in [
348 : jhr 3478 assignEin(t1, Mk.subRR, [x, x0]),
349 :     assignEin(t2, Mk.subRR, [x1, x0]),
350 :     assignEin(t3, Mk.divRR, [t1, t2]),
351 :     IR.ASSGN(y, IR.OP(Op.Lerp(shapeVarToTensor sv), [a, b, t3]))
352 : jhr 3471 ] end),
353 :     (BV.evals2x2, eigenVal (Op.Eigen2x2, 2)),
354 :     (BV.evals3x3, eigenVal (Op.Eigen3x3, 3)),
355 :     (BV.evecs2x2, eigenVec (Op.Eigen2x2, 2)),
356 :     (BV.evecs3x3, eigenVec (Op.Eigen3x3, 3)),
357 :     (BV.fn_length, fn (y, [tv], [s]) => assign(y, Op.Length(trType tv), [s])),
358 : jhr 3498 (BV.fn_abs_i, simpleOp (Op.Abs DstTy.IntTy)),
359 :     (BV.fn_abs_r, simpleOp (Op.Abs DstTy.realTy)),
360 : jhr 3483 (BV.fn_max_i, simpleOp (Op.Max DstTy.IntTy)),
361 :     (BV.fn_max_r, simpleOp (Op.Max DstTy.realTy)),
362 :     (BV.fn_min_i, simpleOp (Op.Min DstTy.IntTy)),
363 :     (BV.fn_min_r, simpleOp (Op.Min DstTy.realTy)),
364 : jhr 3471 (BV.kn_bspln3, kernel Kernel.bspln3),
365 :     (BV.kn_bspln5, kernel Kernel.bspln5),
366 :     (BV.kn_ctmr, kernel Kernel.ctmr),
367 :     (BV.kn_c4hexic, kernel Kernel.c4hexic),
368 :     (BV.kn_tent, kernel Kernel.tent),
369 :     (BV.i2r, simpleOp Op.IntToReal),
370 : jhr 3478 (BV.identity, fn (y, [Ty.DIM d], xs) =>
371 :     [assignEin(y, Mk.identity d, xs)]),
372 : jhr 3492 (* FIXME: eventually zero should be implemented by Ein
373 : jhr 3478 (BV.zero, fn (y, [Ty.SHAPE dd], []) =>
374 :     [assignEin(y, Mk.zeros dd, [])]),
375 : jhr 3492 *)
376 :     (BV.zero, fn (y, [sv], []) =>
377 :     assign(y, Op.Zero(shapeVarToTensor sv), [])),
378 : jhr 3471 (BV.nan, fn (y, [Ty.SHAPE dd], []) => let
379 : jhr 3478 val nan = IR.LIT(Literal.Real(RealLit.nan))
380 :     fun mk (y, [], stms) = IR.ASSGN(y, nan) :: stms
381 : jhr 3471 | mk (y, d::dd, stms) = let
382 :     val ty = shapeVarToTensor(Ty.SHAPE dd)
383 : jhr 3478 val zs = List.tabulate(d, fn _ => IR.Var.new("_nan", ty))
384 : jhr 3471 in
385 : jhr 3478 IR.ASSGN(y, IR.CONS(zs, IR.Var.ty y)) ::
386 : jhr 3471 List.foldl (fn (z, stms) => mk(z, dd, stms)) stms zs
387 :     end
388 :     in
389 :     List.rev (mk (y, dd, []))
390 :     end),
391 :     (BV.subscript, fn (y, [tv, Ty.DIM d], args) =>
392 :     assign(y,
393 : jhr 3478 Op.SeqSub(DstTy.SeqTy(trType tv, SOME d)),
394 : jhr 3471 args)),
395 :     (BV.dynSubscript, fn (y, [tv], args) =>
396 : jhr 3478 assign(y, Op.SeqSub(DstTy.SeqTy(trType tv, NONE)), args)),
397 : jhr 3471 (* image operations *)
398 :     (BV.fn_size, fn (y, [Ty.DIM d, _], [img]) => let
399 : jhr 3478 val DstTy.ImageTy info = IR.Var.ty img
400 : jhr 3471 (* we extract each dimension separately and then build the sequence value *)
401 : jhr 3478 val dims = List.tabulate(d, fn i => IR.Var.new("i"^Int.toString i, DstTy.IntTy))
402 : jhr 3471 fun mkStms ([], _, stms) = stms (* in reverse order! *)
403 :     | mkStms (d::dr, i, stms) = mkStms (dr, i+1,
404 : jhr 3478 IR.ASSGN(d, IR.OP(Op.ImageDim(info, i), [img])) :: stms)
405 : jhr 3471 in
406 :     List.revAppend (mkStms (dims, 0, []), [
407 : jhr 3478 IR.ASSGN(y, IR.SEQ(dims, DstTy.SeqTy(DstTy.intTy, SOME d)))
408 : jhr 3471 ])
409 :     end),
410 :     (BV.image_border, fn (y, _, args as [img, _]) => let
411 : jhr 3478 val DstTy.ImageTy info = IR.Var.ty img
412 : jhr 3471 in
413 :     assign(y, Op.BorderCtlDefault info, args)
414 :     end),
415 :     (BV.image_clamp, fn (y, _, args as [img]) => let
416 : jhr 3478 val DstTy.ImageTy info = IR.Var.ty img
417 : jhr 3471 in
418 :     assign(y, Op.BorderCtlClamp info, args)
419 :     end),
420 :     (BV.image_mirror, fn (y, _, args as [img]) => let
421 : jhr 3478 val DstTy.ImageTy info = IR.Var.ty img
422 : jhr 3471 in
423 :     assign(y, Op.BorderCtlMirror info, args)
424 :     end),
425 :     (BV.image_wrap, fn (y, _, args as [img]) => let
426 : jhr 3478 val DstTy.ImageTy info = IR.Var.ty img
427 : jhr 3471 in
428 :     assign(y, Op.BorderCtlWrap info, args)
429 : jhr 3484 end),
430 : jhr 3498 (BV.dist2_t, fn (y, _, xs) => dist(y, 2, xs)),
431 :     (BV.dist3_t, fn (y, _, xs) => dist(y, 3, xs)),
432 : jhr 3484 (BV.red_all, fn (y, _, args as [e, v, k]) =>
433 : jhr 3498 assign(y, Op.R_All(IR.Var.ty v), args)),
434 : jhr 3484 (BV.red_exists, fn (y, _, args as [e, v, k]) =>
435 : jhr 3498 assign(y, Op.R_Exists(IR.Var.ty v), args)),
436 : jhr 3484 (BV.red_max, fn (y, _, args as [e, v, k]) =>
437 : jhr 3498 assign(y, Op.R_Max(IR.Var.ty v), args)),
438 : jhr 3484 (BV.red_mean, fn (y, _, args as [e, v, k]) =>
439 : jhr 3498 assign(y, Op.R_Mean(IR.Var.ty v), args)),
440 : jhr 3484 (BV.red_min, fn (y, _, args as [e, v, k]) =>
441 : jhr 3498 assign(y, Op.R_Min(IR.Var.ty v), args)),
442 : jhr 3484 (BV.red_product, fn (y, _, args as [e, v, k]) =>
443 : jhr 3498 assign(y, Op.R_Product(IR.Var.ty v),args)),
444 : jhr 3484 (BV.red_sum, fn (y, _, args as [e, v, k]) =>
445 : jhr 3498 assign(y, Op.R_Sum(IR.Var.ty v), args)),
446 : jhr 3484 (BV.red_variance, fn (y, _, args as [e, v, k]) =>
447 : jhr 3498 assign(y, Op.R_Variance(IR.Var.ty v), args)),
448 : jhr 3484 (BV.fn_sphere_im, fn (y, [tv], args as [p, s]) =>
449 : jhr 3498 assign(y, Op.SphereQuery(IR.Var.ty p, trType tv), args)),
450 : jhr 3484 (BV.fn_sphere1_r, fn (y, [tv], args as [p, s]) =>
451 : jhr 3498 assign(y, Op.SphereQuery(IR.Var.ty p, trType tv), args)),
452 : jhr 3484 (BV.fn_sphere2_t, fn (y, [tv], args as [p, s]) =>
453 : jhr 3498 assign(y, Op.SphereQuery(IR.Var.ty p, trType tv), args)),
454 : jhr 3484 (BV.fn_sphere3_t, fn (y, [tv], args as [p, s]) =>
455 : jhr 3498 assign(y, Op.SphereQuery(IR.Var.ty p, trType tv), args))
456 : jhr 3471 ];
457 :     tbl
458 :     end
459 :    
460 :     fun translate (y, f, mvs, xs) = (case VTbl.find tbl f
461 :     of SOME transFn => transFn(y, mvs, xs)
462 :     | NONE => raise Fail("TranslateBasis.translate: unknown basis function " ^ Var.uniqueNameOf f)
463 :     (* end case *))
464 : jhr 3478 handle ex => (print(concat["translate (", IR.Var.toString y, ", ",
465 : jhr 3471 Var.uniqueNameOf f, ", ...)\n"]); raise ex)
466 :    
467 :     end

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