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

SCM Repository

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

View of /branches/charisee/src/compiler/translate/translate-basis.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 406 - (download) (annotate)
Fri Oct 15 12:12:10 2010 UTC (8 years, 11 months ago) by jhr
Original Path: trunk/src/compiler/translate/translate-basis.sml
File size: 4354 byte(s)
  Simplify basis by removing cruft that is not being used.
(* 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 DstTy = HighILTypes
    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(DstTy.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(DstTy.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")
	  in
	    List.app (VTbl.insert tbl) [
		(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.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 DstTy.IntTy)),
		(BV.div_rr,		simpleOp(Op.Div(DstTy.TensorTy[]))),
		(BV.div_tr,		tensorOp Op.InvScale),
		(BV.lt_ii,		simpleOp(Op.LT DstTy.IntTy)),
		(BV.lt_rr,		simpleOp(Op.LT(DstTy.TensorTy[]))),
		(BV.lte_ii,		simpleOp(Op.LTE DstTy.IntTy)),
		(BV.lte_rr,		simpleOp(Op.LTE(DstTy.TensorTy[]))),
		(BV.gte_ii,		simpleOp(Op.GTE DstTy.IntTy)),
		(BV.gte_rr,		simpleOp(Op.GTE(DstTy.TensorTy[]))),
		(BV.gt_ii,		simpleOp(Op.GT DstTy.IntTy)),
		(BV.gt_rr,		simpleOp(Op.GT(DstTy.TensorTy[]))),
		(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.TensorTy[]))),
		(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.TensorTy[]))),
		(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, _, 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.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)
	      ];
	    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 function " ^ Var.uniqueNameOf f)
	  (* end case *))

  end

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