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

SCM Repository

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

View of /branches/charisee/src/compiler/mid-to-low/mid-to-low.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 459 - (download) (annotate)
Wed Oct 27 12:54:03 2010 UTC (8 years, 11 months ago) by jhr
Original Path: trunk/src/compiler/mid-to-low/mid-to-low.sml
File size: 5078 byte(s)
  Working on Mid to Low 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 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)
		in
		  VTbl.insert env (x, x');
		  x'
		end
	  (* end case *))
    fun renameList (env, xs) = List.map (fn x => rename(env, x)) xs

  (* 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 expandKernel (result, d, h, k) = let
	  val {isCont, segs} = Kernel.curve (h, k)
	  in
	  end

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

    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 => assign (DstOp.Select ty) * int
	      | 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.Transform info => assign (DstOp.Transform 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))
	      | _ => raise Fail("unexpected " ^ SrcOp.toString rator)
	    (* 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