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

SCM Repository

[diderot] View of /trunk/src/compiler/mid-to-low/mid-to-low.sml
ViewVC logotype

View of /trunk/src/compiler/mid-to-low/mid-to-low.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 463 - (download) (annotate)
Wed Oct 27 22:48:59 2010 UTC (9 years ago) by jhr
File size: 7196 byte(s)
  Working on MidIL to LowIL translation
(* mid-to-low.sml
 *
 * COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu)
 * All rights reserved.
 *
 * Translation from MidIL to LowIL representations.
 *)

structure MidToLow : sig

    val translate : MidIL.program -> LowIL.program

  end = struct


    structure SrcIL = MidIL
    structure SrcOp = MidOps
    structure VTbl = SrcIL.Var.Tbl
    structure DstIL = LowIL
    structure DstTy = LowTypes
    structure DstOp = LowOps

    type var_env = DstIL.var VTbl.hash_table

    fun rename (env : var_env, x) = (case VTbl.find env x
	   of SOME x' => x'
	    | NONE => let
		val x' = DstIL.Var.new (SrcIL.Var.name x, SrcIL.Var.ty x)
		in
		  VTbl.insert env (x, x');
		  x'
		end
	  (* end case *))
    fun renameList (env, xs) = List.map (fn x => rename(env, x)) xs

  (* convert a rational to a FloatLit.float value.  We do this by long division
   * with a cutoff when we get to 12 digits.
   *)
    fun ratToFloat r = (case Rational.explode r
	   of {sign=0, ...} => FloatList.zero
	    | {sign, num, denom=1} => FloatLit.fromInt(sign * IntInf.toInt num)
	    | {sign, num, denom} => let
	      (* normalize so that num <= denom *)
		val (denom, exp) = let
		      fun lp (n, denom) = if (denom < num)
			    then lp(n+1, denom*10)
			    else (n, denom)
		      in
			lp (0, denom)
		      end
	      (* normalize so that num <= denom < 10*num *)
(* FIXME *)
	      (* divide num/denom, computing the resulting digits *)
		fun divLp (n, a) = let
		      val (q, r) = IntInf.divMod(a, denom)
		      in
			if (r = 0) then (q, [])
			else if (n < 12) then let
			  val (d, dd) = divLp(n+1, 10*r)
			  in
			    if (d < 10) then (q, d::dd) else (q+1, 0::dd)
			  end
			else if (IntInf.div(10*r, denom) < 5)
			  then (q, [])
			  else (q+1, []) (* round up *)
		      end
		val digits = divLp (0, a)
		in
(* FIXME *)
		end

  (* variable names for kernel coefficients *)
  (* expand the EvalKernel operations into vector operations.  The parameters are
   *	result	-- the lhs variable to store the result
   *	d	-- the vector width of the operation, which should be equal to twice the
   *		   support of the kernel
   *	h	-- the kernel
   *	k	-- the derivative of the kernel to evaluate
   *)
    fun expandEvalKernel (result, d, h, k, [x]) = let
	  val {isCont, segs} = Kernel.curve (h, k)
	  val deg = List.length segs - i
	(* convert to a vector of vectors to give fast access *)
	  val segs = Vector.fromList (List.map Vector.fromList segs)
	(* get the kernel coefficient value for the d'th term of the i'th
	 * segment.
	 *)
	  fun coefficient d i = ratToFloat (Vector.sub (Vector.sub(segs, i), d))
	  val ty = DstTy.VecTy d
	  val coeffs = List.tabulate (deg+1,
		fn i => DstIL.Var.new(str(chr(ord #"a" + (deg - i)))), ty)
	(* define coefficient vectors *)
	  val coeffVecs = ???
	(* build the evaluation of the polynomials in reverse order *)
	  fun eval [coeff] = (coeff, coeffVecs)
	    | eval (coeff::r) = let
		val (t1, stms) = eval r
		val t2 = DstIL.Var.new ("_t", ty)
		val t3 = DstIL.Var.new ("_s", ty)
		val stms =
		      (t3, DstIL.OP(DstOp.Add ty, [coeff, t2])) ::
		      (t2, DstIL.OP(DstOp.Mul ty, [x, t1])) ::
		      stms
		in
		  (t3, stms)
		end
	  in
	    List.rev (eval coeffs)
	  end

  (* compute the load address for a given set of voxels indices *) 
    fun expandVoxelAddress (result, info) = raise Fail "unimplemented"

    fun expandOp (env, y, rator, args) = let
	  fun assign rator' =
		[(y, DstIL.OP(rator', renameList(env, args)))]
	  in
	    case rator
	     of SrcOp.Add ty => assign (DstOp.Add ty)
	      | SrcOp.Sub ty => assign (DstOp.Sub ty)
	      | SrcOp.Mul ty => assign (DstOp.Mul ty)
	      | SrcOp.Div ty => assign (DstOp.Div ty)
	      | SrcOp.Neg ty => assign (DstOp.Neg ty)
	      | SrcOp.LT ty => assign (DstOp.LT ty)
	      | SrcOp.LTE ty => assign (DstOp.LTE ty)
	      | SrcOp.EQ ty => assign (DstOp.EQ ty)
	      | SrcOp.NEQ ty => assign (DstOp.NEQ ty)
	      | SrcOp.GT ty => assign (DstOp.GT ty)
	      | SrcOp.GTE ty => assign (DstOp.GTE ty)
	      | SrcOp.Not => assign (DstOp.Not)
	      | SrcOp.Max => assign (DstOp.Max)
	      | SrcOp.Min => assign (DstOp.Min)
	      | SrcOp.Sin => assign (DstOp.Sin)
	      | SrcOp.Cos => assign (DstOp.Cos)
	      | SrcOp.Pow => assign (DstOp.Pow)
	      | SrcOp.Dot d => assign (DstOp.Dot d)
	      | SrcOp.Cross => assign (DstOp.Cross)
	      | SrcOp.Select(ty, i)=> assign (DstOp.Select(ty, i))
	      | SrcOp.Norm d => assign (DstOp.Norm d)
	      | SrcOp.Scale d => assign (DstOp.Scale d)
	      | SrcOp.InvScale d => assign (DstOp.InvScale d)
	      | SrcOp.CL => assign (DstOp.CL)
	      | SrcOp.PrincipleEvec ty => assign (DstOp.PrincipleEvec ty)
	      | SrcOp.Subscript ty => assign (DstOp.Subscript ty)
	      | SrcOp.Floor d => assign (DstOp.Floor d)
	      | SrcOp.IntToReal => assign (DstOp.IntToReal)
	      | SrcOp.TruncToInt d => assign (DstOp.TruncToInt d)
	      | SrcOp.RoundToInt d => assign (DstOp.RoundToInt d)
	      | SrcOp.CeilToInt d => assign (DstOp.CeilToInt d)
	      | SrcOp.FloorToInt d => assign (DstOp.FloorToInt d)
	      | SrcOp.VoxelAddress info => expandVoxelAddress (y, info)
	      | SrcOp.LoadVoxels(rty, d) => assign (DstOp.LoadVoxels(rty, d))
	      | SrcOp.PosToImgSpace info => assign (DstOp.PosToImgSpace info)
	      | SrcOp.GradToWorldSpace info => assign (DstOp.GradToWorldSpace info)
	      | SrcOp.EvalKernel(d, h, k) => expandEvalKernel(y, d, h, k)
	      | SrcOp.LoadImage info => assign (DstOp.LoadImage info)
	      | SrcOp.Inside info => assign (DstOp.Inside info)
	      | SrcOp.Input(ty, name) => assign (DstOp.Input(ty, name))
	      | SrcOp.InputWithDefault(ty, name) => assign (DstOp.InputWithDefault(ty, name))
	    (* end case *)
	  end

  (* expand a SrcIL assignment to a list of DstIL assignments *)
    fun expand (env, (y, rhs)) = let
	  val y' = rename (env, y)
	  fun assign rhs = [(y', rhs)]
	  in
	    case rhs
	     of SrcIL.VAR x => assign (DstIL.VAR(rename(env, x)))
	      | SrcIL.LIT lit => assign (DstIL.LIT lit)
	      | SrcIL.OP(rator, args) => expandOp (env, y', rator, args)
	      | SrcIL.CONS args => assign (DstIL.CONS(renameList(env, args)))
	    (* end case *)
	  end

    structure Trans =  TranslateFn (
      struct
	structure SrcIL = SrcIL
	structure DstIL = DstIL

	type var_env = var_env

	val rename = rename
	val expand = expand
      end)

    fun translate (SrcIL.Program{globals, globalInit, actors}) = let
	  val env = VTbl.mkTable (256, Fail "env")
	  fun transMethod (SrcIL.Method{name, stateIn, stateOut, body}) =
		DstIL.Method{
		    name = name,
		    stateIn = renameList (env, stateIn),
		    stateOut = renameList (env, stateOut),
		    body = Trans.translate (env, body)
		  }
	  fun transActor (SrcIL.Actor{name, params, state, stateInit, methods}) =
		DstIL.Actor{
		    name = name,
		    params = renameList (env, params),
		    state = renameList (env, state),
		    stateInit = Trans.translate (env, stateInit),
		    methods = List.map transMethod methods
		  }
	  in
	    DstIL.Program{
		globals = renameList (env, globals),
		globalInit = Trans.translate (env, globalInit),
		actors = List.map transActor actors
	      }
	  end

  end

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