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

SCM Repository

[diderot] View of /trunk/src/compiler/translate/translate-basis.sml
ViewVC logotype

View of /trunk/src/compiler/translate/translate-basis.sml

Parent Directory Parent Directory | Revision Log Revision Log

Revision 263 - (download) (annotate)
Tue Aug 10 17:36:19 2010 UTC (11 years, 10 months ago) by jhr
File size: 4416 byte(s)
  Bug fix
(* translate-basis.sml
 * COPYRIGHT (c) 2010 The Diderot Project (http://diderot.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 Op = HighOps
    structure Ty = Types
    structure TU = TypeUtil
    structure MV = MetaVar
    structure VTbl = Var.Tbl

    fun assign (y, rator, xs) = [(y, IL.OP(rator, xs))]

    fun simpleOp rator (y, [], xs) = assign (y, rator, xs)

    fun pruneDim d = (case TU.pruneDim d
	   of (Ty.DimConst n) => n
	    | d => raise Fail("unresolved dimension " ^ TU.dimToString d)
	  (* end case *))

    fun tensorOp rator (y, [sv], xs) = (case TU.pruneShape(MV.toShape sv)
	   of Ty.Shape dd =>
		assign (y, rator(Op.TensorTy(List.map pruneDim dd)), xs)
	    | shp => raise Fail("unresolved shape " ^ TU.shapeToString shp)
	  (* end case *))

    fun vectorOp rator (y, [dv], xs) =
	  assign (y, rator(Op.TensorTy[pruneDim(MV.toDim dv)]), xs)

    fun kernel h (y, [], []) = assign(y, Op.Kernel h, [])

  (* 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")
	    List.app (VTbl.insert tbl) [
		(BV.add_ii,		simpleOp(Op.Add Op.IntTy)),
		(BV.add_tt,		tensorOp Op.Add),
		(BV.sub_ii,		simpleOp(Op.Sub Op.IntTy)),
		(BV.sub_tt,		tensorOp Op.Sub),
		(BV.mul_ii,		simpleOp(Op.Mul Op.IntTy)),
		(BV.mul_rr,		simpleOp(Op.Mul(Op.TensorTy[]))),
		(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 Op.IntTy)),
		(BV.div_rr,		simpleOp(Op.Div(Op.TensorTy[]))),
		(BV.div_tr,		tensorOp Op.InvScale),
		(BV.lt_ii,		simpleOp(Op.LT Op.IntTy)),
		(BV.lt_rr,		simpleOp(Op.LT(Op.TensorTy[]))),
		(BV.lte_ii,		simpleOp(Op.LTE Op.IntTy)),
		(BV.lte_rr,		simpleOp(Op.LTE(Op.TensorTy[]))),
		(BV.gte_ii,		simpleOp(Op.GTE Op.IntTy)),
		(BV.gte_rr,		simpleOp(Op.GTE(Op.TensorTy[]))),
		(BV.gt_ii,		simpleOp(Op.GT Op.IntTy)),
		(BV.gt_rr,		simpleOp(Op.GT(Op.TensorTy[]))),
		(BV.equ_bb,		simpleOp(Op.EQ Op.BoolTy)),
		(BV.equ_ii,		simpleOp(Op.EQ Op.IntTy)),
		(BV.equ_ss,		simpleOp(Op.EQ Op.StringTy)),
		(BV.equ_rr,		simpleOp(Op.EQ(Op.TensorTy[]))),
		(BV.neq_bb,		simpleOp(Op.NEQ Op.BoolTy)),
		(BV.neq_ii,		simpleOp(Op.NEQ Op.IntTy)),
		(BV.neq_ss,		simpleOp(Op.NEQ Op.StringTy)),
		(BV.neq_rr,		simpleOp(Op.NEQ(Op.TensorTy[]))),
		(BV.neg_i,		simpleOp(Op.Neg Op.IntTy)),
		(BV.neg_t,		tensorOp Op.Neg),
		(BV.neg_f,		fn (y, _, xs) => assign(y, Op.NegField, xs)),
		(BV.op_at,		fn (y, _, xs) => assign(y, Op.Probe, xs)),
		(BV.op_convolve,	fn (y, _, xs) => assign(y, Op.Convolve, xs)),
		(BV.op_D,		fn (y, _, xs) => assign(y, Op.DiffField, xs)),
		(BV.op_norm,		tensorOp Op.Norm),
		(BV.op_not,		simpleOp Op.Not),
		(BV.op_subscript,	fn (y, [SK, NK], xs) => ??), (*FIXME*)
		(BV.fn_CL,		fn (y, _, xs) => assign(y, Op.CL, xs)),
		(BV.fn_convolve,	fn (y, _, [h, img]) => assign(y, Op.Convolve, [img, h])),
		(BV.fn_cos,		simpleOp Op.Cos),
		(BV.fn_dot,		vectorOp Op.Dot),
		(BV.fn_inside,		fn (y, _, xs) => assign(y, Op.Inside, xs)),
		(BV.fn_load,		fn (y, [NK, SK], xs) => ??), (*FIXME*)
		(BV.fn_max,		simpleOp Op.Max),
		(BV.fn_min,		simpleOp Op.Min),
		(BV.fn_modulate,	vectorOp Op.Mul),
		(BV.fn_pow,		simpleOp Op.Pow),
		(BV.fn_principleEvec,	vectorOp Op.PrincipleEvec),
		(BV.fn_sin,		simpleOp Op.Sin),
		(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.i2r,		simpleOp Op.IntToReal)(*,
		(BV.input,		fn (y, [TK], xs) => ??), (*FIXME*)
		(BV.optInput,		fn (y, [TK], xs) => ??) (*FIXME*)

    fun translate (y, f, mvs, xs) = (case VTbl.find tbl f
	   of SOME transFn => transFn(y, mvs, xs)
	    | NONE => raise Fail("TranslateBasis.translate: unknown function " ^ Var.uniqueNameOf f)
	  (* end case *))


ViewVC Help
Powered by ViewVC 1.0.0