SCM Repository
View of /branches/pure-cfg/src/compiler/translate/translate-basis.sml
Parent Directory
|
Revision Log
Revision 896 -
(download)
(annotate)
Wed Apr 20 17:21:05 2011 UTC (11 years, 1 month ago) by jhr
File size: 7605 byte(s)
Wed Apr 20 17:21:05 2011 UTC (11 years, 1 month ago) by jhr
File size: 7605 byte(s)
Fixed field negation
(* translate-basis.sml * * COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu) * All rights reserved. * * Translation for basis operations in Simple AST to HighIL code *) structure TranslateBasis : sig (* translate(lhs, f, mvs, args) translates the application of f (specialized * to the instantiated meta variables mvs) to a list of SSA assignments in * reverse order. *) val translate : (HighIL.var * Var.var * Types.meta_var list * HighIL.var list) -> HighIL.assign list end = struct structure BV = BasisVars structure IL = HighIL structure DstTy = HighILTypes structure Op = HighOps structure Ty = Types structure TU = TypeUtil structure MV = MetaVar structure VTbl = Var.Tbl fun pruneDim d = (case TU.pruneDim d of (Ty.DimConst n) => n | d => raise Fail("unresolved dimension " ^ TU.dimToString d) (* end case *)) fun pruneShape sv = (case TU.pruneShape(MV.toShape sv) of Ty.Shape dd => DstTy.tensorTy(List.map pruneDim dd) | shp => raise Fail("unresolved shape " ^ TU.shapeToString shp) (* end case *)) fun dimVarToTensor dv = DstTy.tensorTy[pruneDim(MV.toDim dv)] fun dimVarToMatrix dv = let val d = pruneDim(MV.toDim dv) in DstTy.tensorTy[d, d] (* square matrix type *) end fun shapeVarToTensor sv = pruneShape sv fun assign (y, rator, xs) = [(y, IL.OP(rator, xs))] fun basisFn name (y, [], xs) = [(y, IL.APPLY(name, xs))] fun simpleOp rator (y, [], xs) = assign (y, rator, xs) fun tensorOp rator (y, [sv], xs) = assign (y, rator(shapeVarToTensor sv), xs) fun vectorOp rator (y, [dv], xs) = assign (y, rator(dimVarToTensor dv), xs) fun kernel h (y, [], []) = assign(y, Op.Kernel(h, 0), []) (* build a table that maps Basis variables to their translation functions *) val tbl : ((IL.var * Ty.meta_var list * IL.var list) -> IL.assign list) VTbl.hash_table = let val tbl = VTbl.mkTable (128, Fail "Translate table") in List.app (VTbl.insert tbl) [ (BV.lt_ii, simpleOp(Op.LT DstTy.IntTy)), (BV.lt_rr, simpleOp(Op.LT(DstTy.realTy))), (BV.lte_ii, simpleOp(Op.LTE DstTy.IntTy)), (BV.lte_rr, simpleOp(Op.LTE(DstTy.realTy))), (BV.gte_ii, simpleOp(Op.GTE DstTy.IntTy)), (BV.gte_rr, simpleOp(Op.GTE(DstTy.realTy))), (BV.gt_ii, simpleOp(Op.GT DstTy.IntTy)), (BV.gt_rr, simpleOp(Op.GT(DstTy.realTy))), (BV.equ_bb, simpleOp(Op.EQ DstTy.BoolTy)), (BV.equ_ii, simpleOp(Op.EQ DstTy.IntTy)), (BV.equ_ss, simpleOp(Op.EQ DstTy.StringTy)), (BV.equ_rr, simpleOp(Op.EQ(DstTy.realTy))), (BV.neq_bb, simpleOp(Op.NEQ DstTy.BoolTy)), (BV.neq_ii, simpleOp(Op.NEQ DstTy.IntTy)), (BV.neq_ss, simpleOp(Op.NEQ DstTy.StringTy)), (BV.neq_rr, simpleOp(Op.NEQ(DstTy.realTy))), (BV.add_ii, simpleOp(Op.Add DstTy.IntTy)), (BV.add_tt, tensorOp Op.Add), (BV.sub_ii, simpleOp(Op.Sub DstTy.IntTy)), (BV.sub_tt, tensorOp Op.Sub), (BV.mul_ii, simpleOp(Op.Mul DstTy.IntTy)), (BV.mul_rr, simpleOp(Op.Mul(DstTy.realTy))), (BV.mul_rt, tensorOp Op.Scale), (BV.mul_tr, fn (y, sv, [t, r]) => tensorOp Op.Scale (y, sv, [r, t])), (BV.div_ii, simpleOp(Op.Div DstTy.IntTy)), (BV.div_rr, simpleOp(Op.Div DstTy.realTy)), (BV.div_tr, fn (y, [sv], [x, s]) => let val one = IL.Var.new("one", DstTy.realTy) val s' = IL.Var.new("s", DstTy.realTy) in [ (one, IL.LIT(Literal.Float(FloatLit.one))), (s', IL.OP(Op.Div DstTy.realTy, [one, s])), (y, IL.OP(Op.Scale(shapeVarToTensor sv), [s', x])) ] end), (BV.exp_ri, simpleOp(Op.Power)), (BV.exp_rr, basisFn ILBasis.pow), (BV.neg_i, simpleOp(Op.Neg DstTy.IntTy)), (BV.neg_t, tensorOp Op.Neg), (BV.neg_f, fn (y, _, xs) => assign(y, Op.NegField, xs)), (BV.op_at, fn (y, [_, dv, sv], xs) => assign(y, Op.Probe(dimVarToTensor dv, shapeVarToTensor sv), xs)), (BV.op_convolve, fn (y, [_, dv, _], xs) => assign(y, Op.Field(pruneDim(MV.toDim dv)), xs)), (BV.op_D, fn (y, _, xs) => assign(y, Op.DiffField, xs)), (BV.op_norm, fn (y, [sv], xs) => (case shapeVarToTensor sv of DstTy.TensorTy[] => assign(y, Op.Abs DstTy.realTy, xs) | ty => assign(y, Op.Norm ty, xs) (* end case *))), (BV.op_not, simpleOp Op.Not), (BV.fn_atan2, basisFn ILBasis.atan2), (BV.fn_CL, fn (y, _, xs) => assign(y, Op.CL, xs)), (BV.fn_convolve, fn (y, [_, dv, _], [h, img]) => assign(y, Op.Field(pruneDim(MV.toDim dv)), [img, h])), (BV.fn_cos, basisFn ILBasis.cos), (BV.op_cross, simpleOp Op.Cross), (BV.fn_cross, simpleOp Op.Cross), (BV.op_outer, fn (y, [dv1, dv2], xs) => let val d1 = pruneDim(MV.toDim dv1) val d2 = pruneDim(MV.toDim dv2) in assign (y, Op.Outer(DstTy.tensorTy[d1, d2]), xs) end), (BV.fn_outer, fn (y, [dv1, dv2], xs) => let val d1 = pruneDim(MV.toDim dv1) val d2 = pruneDim(MV.toDim dv2) in assign (y, Op.Outer(DstTy.tensorTy[d1, d2]), xs) end), (BV.op_inner, fn (y, [sh1, sh2, _], xs) => let val ty1 as DstTy.TensorTy dd1 = pruneShape sh1 val ty2 as DstTy.TensorTy dd2 = pruneShape sh2 val rator = (case (dd1, dd2) of ([d], [d']) => Op.Dot ty1 | ([d1], [d1', d2]) => Op.MulVecMat ty2 | ([d1, d2], [d2']) => Op.MulMatVec ty1 | ([d1, d2], [d2', d3]) => Op.MulMatMat(ty1, ty2) | _ => raise Fail "unsupported inner-product type" (* end case *)) in assign (y, rator, xs) end), (BV.fn_dot, vectorOp Op.Dot), (BV.fn_evals, fn _ => raise Fail "evals not implemented yet"), (* FIXME *) (BV.fn_evecs, fn _ => raise Fail "evecs not implemented yet"), (* FIXME *) (BV.fn_inside, fn (y, [_, dv, _], xs) => assign(y, Op.Inside(pruneDim(MV.toDim dv)), xs)), (BV.lerp3, tensorOp Op.Lerp), (BV.lerp5, fn (y, [sv], [a, b, x0, x, x1]) => let val t1 = IL.Var.new("t1", DstTy.realTy) val t2 = IL.Var.new("t2", DstTy.realTy) val t3 = IL.Var.new("t3", DstTy.realTy) in [ (t1, IL.OP(Op.Sub DstTy.realTy, [x, x0])), (t2, IL.OP(Op.Sub DstTy.realTy, [x1, x0])), (t3, IL.OP(Op.Div DstTy.realTy, [t1, t2])), (y, IL.OP(Op.Lerp(shapeVarToTensor sv), [a, b, t3])) ] end), (BV.fn_max, simpleOp Op.Max), (BV.fn_min, simpleOp Op.Min), (BV.fn_modulate, vectorOp Op.Mul), (BV.fn_normalize, vectorOp Op.Normalize), (BV.fn_outer, fn _ => raise Fail "outer not implemented yet"), (* FIXME *) (BV.fn_pow, basisFn ILBasis.pow), (BV.fn_principleEvec, vectorOp Op.PrincipleEvec), (BV.fn_sin, basisFn ILBasis.sin), (BV.fn_sqrt, basisFn ILBasis.sqrt), (BV.fn_tan, basisFn ILBasis.tan), (BV.fn_trace, fn (y, [dv], xs) => assign(y, Op.Trace(dimVarToMatrix dv), xs)), (BV.kn_bspln3, kernel Kernel.bspln3), (BV.kn_bspln5, kernel Kernel.bspln5), (BV.kn_ctmr, kernel Kernel.ctmr), (BV.kn_tent, kernel Kernel.tent), (BV.kn_c1tent, kernel Kernel.tent), (BV.i2r, simpleOp Op.IntToReal), (BV.identity, fn (y, [dv], []) => assign(y, Op.Identity(pruneDim(MV.toDim dv)), [])), (BV.zero, fn (y, [sv], []) => assign(y, Op.Zero(shapeVarToTensor sv), [])) ]; tbl end fun translate (y, f, mvs, xs) = (case VTbl.find tbl f of SOME transFn => transFn(y, mvs, xs) | NONE => raise Fail("TranslateBasis.translate: unknown basis function " ^ Var.uniqueNameOf f) (* end case *)) handle ex => (print(concat["translate (", IL.Var.toString y, ", ", Var.uniqueNameOf f, ", ...)\n"]); raise ex) end
root@smlnj-gforge.cs.uchicago.edu | ViewVC Help |
Powered by ViewVC 1.0.0 |