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 842 - (download) (annotate)
Mon Apr 18 17:58:57 2011 UTC (9 years, 9 months ago) by jhr
File size: 3745 byte(s)
  Working on refactoring
(* codegen-fn.sml
 *
 * COPYRIGHT (c) 2011 The Diderot Project (http://diderot-language.cs.uchicago.edu)
 * All rights reserved.
 *
 * Generic support for translating LowIL code to the target representation.
 *
 * FIXME:
 *	We need to run the TreeIL through a 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 LowToTree = LowToTreeFn(T)

    fun trMethod (strand, stateVars, env) = let
	  val env = T.Env.scopeMethod (env, stateVars)
	  fun tr (IL.Method{name, body}) = let
(*
	  fun saveState (env, args, stm) =
		ListPair.foldrEq
		  (fn (x, e, stms) => T.Stmt.assignState(x, trExp(env, e))::stms)
		    [stm]
		      (stateVars, args)
*)
		val body = T.Tr.block (env, body)
		in
		  T.Strand.method (strand, Atom.toString name, body)
		end
	  in
	    tr
	  end

    fun trStrand (prog, env) (IL.Strand{name, params, state, stateInit, methods}) = let
	  val strand = T.Strand.define(prog, name)
	  val env = T.Env.scopeStrand (env, strand)
	(* the output state variable *)
	  val outputVar = ref NONE
	(* map the state variables to target state variables and extend the environment *)
	  val env = let
		fun cvtSVar ((isOut, x), env) = let
		      val x' = T.Var.state(strand, x)
		      in
			case (isOut, !outputVar)
			 of (true, NONE) => outputVar := SOME(IL.Var.ty x, x')
			  | (false, _) => ()
			  | _ => raise Fail("multiple outputs in " ^ Atom.toString name)
			(* end case *);
			T.Env.bind(env, x, x')
		      end
		in
		  List.foldr cvtSVar env state
		end
	(* define the parameters and add them to the environment *)
	  val params' = List.map T.Var.param params
	  val env =
		ListPair.foldlEq (fn (x, y, env) => T.Env.bind(env, x, y)) env  (params, params')
	  in
	    T.Strand.init (strand, params', T.Tr.block (env, stateInit));
	    case !outputVar
	     of NONE => raise Fail("no output specified for strand " ^ Atom.toString name)
	      | SOME(ty, x) => T.Strand.output (strand, ty, x)
	    (* end case *);
	    List.app (trMethod (strand, List.map #2 state, env)) methods
	  end

    fun trInitially (env0, prog, {isArray, iterPrefix, iters, createPrefix, strand, args}) = let
	  val env0 = T.Env.scopeInitially env0
	  fun trIter ((param, lo, hi), (env, iters)) = let
		val param' = T.Var.param param
		val env = T.Env.bind (env, param, param')
		val iter = (param', T.Tr.exp (env0, lo), T.Tr.exp (env0, hi))
		in
		  (env, iter::iters)
		end
	  val (env, iters) = List.foldr trIter (env0, []) iters
	  in
	    T.Program.initially {
		prog = prog,
		isArray = isArray,
		iterPrefix = T.Tr.block (env0, iterPrefix),
		iters = iters,
		createPrefix = T.Tr.block (env, createPrefix),
		strand = strand,
		args = List.map (fn e => T.Tr.exp(env, e)) args
	      }
	  end

    fun generate (fileStem, srcProg) = let
	  val treeProg as TreeIL.Program{globals, globalInit, strands, initially} =
		LowToTree.translate srcProg
val _ = (
TextIO.output(Log.logFile(), "********** After translation to TreeIL **********\n");
TreeILPP.program (Log.logFile(), treeProg))
	  val prog = T.Program.new ()
	  val env = T.Env.new prog
	(* define the globals and initialize the environment *)
	  val env = let
		fun gvar (x, env) = T.Env.bind(env, x, T.Var.global(prog, x))
		in
		  List.foldl gvar (T.Env.scopeGlobal env) globals
		end
	  in
	  (* global initialization *)
	    T.Program.init (prog, T.Tr.block (T.Env.scopeGlobal env, globalInit));
	  (* translate strands *)
	    List.app (trStrand (prog, env)) strands;
	  (* generate the initially function *)
	    trInitially (env, prog, initially);
	  (* output the program *)
	    T.Program.generate (fileStem, prog)
	  end

  end

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