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

SCM Repository

[diderot] View of /branches/pure-cfg/src/compiler/codegen/codegen-fn.sml
ViewVC logotype

View of /branches/pure-cfg/src/compiler/codegen/codegen-fn.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 532 - (download) (annotate)
Mon Feb 14 20:26:41 2011 UTC (8 years, 7 months ago) by jhr
File size: 5307 byte(s)
  Working on code generation
(* codegen-fn.sml
 *
 * COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu)
 * All rights reserved.
 *
 * Generic support for translating LowIL code to the target representation.  We
 * assume that the LowIL has first been run through the splitting pass to match
 * the target's vector widths.
 *)

functor CodeGenFn (T : TARGET) : sig

    val generate : string * LowIL.program -> unit

  end = struct

    structure IL = TreeIL
    structure Ty = IL.Ty
    structure Op = IL.Op
    structure V = IL.Var

  (* convert LowIL types to T types *)
    fun cvtTy ty = (case ty
	   of Ty.BoolTy => T.boolTy
	    | Ty.StringTy => raise Fail "FIXME: StringTy"
	    | Ty.IVecTy 1 => T.intTy
	    | Ty.IVecTy n => T.ivecTy n		(* FIXME: what about vector splits? *)
	    | Ty.VecTy 1 => T.realTy
	    | Ty.VecTy n => T.vecTy n		(* FIXME: what about vector splits? *)
	    | Ty.AddrTy => raise Fail "FIXME: AddrTy"
	    | Ty.ImageTy =>  raise Fail "FIXME: ImageTy"
	  (* end case *))

    fun lookup (env, x) = (case V.Map.find (env, x)
	   of SOME x' => x'
	    | NONE => raise Fail "lookup"
	  (* end case *))

    fun trExp (env, e) = (case e
	   of IL.E_Var x => (case V.kind x
		 of IL.VK_Global => T.Expr.global(lookup(env, x))
		  | IL.VK_State strand => raise Fail "FIXME: state var"
		  | IL.VK_Local => T.Expr.var(lookup(env, x))
		(* end case *))
	    | IL.E_Lit(Literal.Int n) => T.Expr.intLit n
	    | IL.E_Lit(Literal.Bool b) => T.Expr.boolLit b
	    | IL.E_Lit(Literal.Float f) => T.Expr.floatLit f
	    | IL.E_Lit(Literal.String s) => T.Expr.stringLit s
	    | IL.E_Op(rator, args) => (case (rator, trExps(env, args))
		 of (Op.Add ty, [a, b])		=> T.Expr.add(a, b)
		  | (Op.Sub ty, [a, b])		=> T.Expr.sub(a, b)
		  | (Op.Mul ty, [a, b])		=> T.Expr.mul(a, b)
		  | (Op.Div ty, [a, b])		=> T.Expr.divide(a, b)
		  | (Op.Neg ty, [a])		=> T.Expr.neg a
		  | (Op.LT ty, [a, b])		=> T.Expr.lt(a, b)
		  | (Op.LTE ty, [a, b])		=> T.Expr.lte(a, b)
		  | (Op.EQ ty, [a, b])		=> T.Expr.equ(a, b)
		  | (Op.NEQ ty, [a, b])		=> T.Expr.neq(a, b)
		  | (Op.GT ty, [a, b])		=> T.Expr.gt(a, b)
		  | (Op.GTE ty, [a, b])		=> T.Expr.gte(a, b)
		  | (Op.Not, [a])		=> T.Expr.not a
		  | (Op.Max, [a, b])		=> T.Expr.max(a, b)
		  | (Op.Min, [a, b])		=> T.Expr.min(a, b)
		  | (Op.Sin, [a])		=> T.Expr.sin a
		  | (Op.Cos, [a])		=> T.Expr.cos a
		  | (Op.Pow, [a, b])		=> T.Expr.pow(a, b)
		  | (Op.Dot d, [a, b])		=> T.Expr.dot(a, b)
		  | (Op.Cross, [a, b])		=> T.Expr.cross(a, b)
		  | (Op.Select(ty, i), [a])	=> T.Expr.select(i, a)
		  | (Op.Norm d, [a])		=> T.Expr.length a
		  | (Op.Scale d, [a, b])	=> T.Expr.mul(a, b)
		  | (Op.InvScale d, [a, b])	=> T.Expr.divide(a, b)
		  | (Op.CL, _)			=> raise Fail "CL unimplemented"
		  | (Op.PrincipleEvec ty, _)	=> raise Fail "PrincipleEvec unimplemented"
(*
		  | (Op.Subscript ty,
*)
		  | (Op.Floor d, [a])		=> T.Expr.floor a
		  | (Op.IntToReal, [a])		=> T.Expr.toReal a
		  | (Op.TruncToInt d, [a])	=> T.Expr.truncToInt a
		  | (Op.RoundToInt d, [a])	=> T.Expr.roundToInt a
		  | (Op.CeilToInt d, [a])	=> T.Expr.ceilToInt a
		  | (Op.FloorToInt d, [a])	=> T.Expr.floorToInt a
		  | (Op.ImageAddress, [a])	=> T.Expr.imageAddr a
		  | (Op.LoadVoxels(rTy, n), [a]) => T.Expr.intLit 0 (* FIXME *)
		  | (Op.PosToImgSpace d, [v, x]) => T.Expr.intLit 0 (* FIXME *)
		  | (Op.GradToWorldSpace d, [v, x]) => T.Expr.intLit 0 (* FIXME *)
		  | (Op.LoadImage info, [a]) => T.Expr.intLit 0 (* FIXME *)
		  | (Op.Inside d, [v, x]) => T.Expr.intLit 0 (* FIXME *)
		  | (Op.Input(ty, name), []) => T.Expr.intLit 0 (* FIXME *)
		  | (Op.InputWithDefault(ty, name), [a]) => T.Expr.intLit 0 (* FIXME *)
		  | _ => raise Fail(concat[
			"incorrect number of arguments for ", Op.toString rator
		      ])
		(* end case *))
	    | IL.E_Cons args => T.Expr.intLit 0 (* FIXME *)
	  (* end case *))

    and trExps (env, exps) = List.map (fn exp => trExp(env, exp)) exps

    fun trStmt (env, stm) = (case stm
	   of IL.S_Comment text => T.Stmt.comment text
(* FIXME: special case for when x is a strand-state variable *)
	    | IL.S_Assign(x, exp) => (case V.kind x
		 of IL.VK_Global => T.Stmt.assignState(lookup(env, x), trExp(env, exp))
		  | IL.VK_State strand => raise Fail "FIXME: state var"
		  | IL.VK_Local => T.Stmt.assign(lookup(env, x), trExp(env, exp))
		(* end case *))
	    | IL.S_IfThen(cond, thenBlk) =>
		T.Stmt.ifthen(trExp(env, cond), trBlock(env, thenBlk))
	    | IL.S_IfThenElse(cond, thenBlk, elseBlk) =>
		T.Stmt.ifthenelse(trExp(env, cond),
		  trBlock(env, thenBlk),
		  trBlock(env, elseBlk))
	    | IL.S_Die => T.Stmt.die()
	    | IL.S_Stabilize => T.Stmt.stabilize()
	  (* end case *))

    and trBlock (env, IL.Block{locals, body}) = (
(* what about the locals?? *)
	  T.Stmt.block(List.map (fn stm => trStmt(env, stm)) body))

    fun generate (fileStem, srcProg) = let
	  val TreeIL.Program{globals, globalInit, strands} = LowToTree.translate srcProg
	  val prog = T.newProgram ()
	(* define the globals and initialize the environment *)
	  val env = let
		fun gvar (x, env) =
		      V.Map.insert(env, x, T.Var.global(prog, cvtTy(V.ty x), V.name x))
		in
		  List.foldl gvar V.Map.empty globals
		end
	  val globalInit = trBlock (env, globalInit)
	  in
	  (* output the program *)
	    T.generate (fileStem, prog)
	  end

  end

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