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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 493 - (download) (annotate)
Thu Jan 27 16:40:50 2011 UTC (8 years, 7 months ago) by jhr
File size: 3773 byte(s)
  Working on porting to new IL
(* translate-fn.sml
 *
 * COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu)
 * All rights reserved.
 *
 * This functor supports the common parts of translating between different
 * instances of the SSA-based ILs (e.g., from HighIL to MidIL).
 *)

signature TRANSLATE_PARAMS =
  sig

    structure SrcIL : SSA
    structure DstIL : SSA

    type var_env = DstIL.var SrcIL.Var.Tbl.hash_table

    val rename : (var_env * SrcIL.var) -> DstIL.var
    val expand : (var_env * SrcIL.assign) -> DstIL.cfg

  end

functor TranslateFn (Params : TRANSLATE_PARAMS) : sig

    structure SrcIL : SSA
    structure DstIL : SSA

    type var_env = DstIL.var SrcIL.Var.Tbl.hash_table

    val translate : var_env * SrcIL.cfg -> DstIL.cfg

  end = struct

    structure SrcIL = Params.SrcIL
    structure SrcNd = SrcIL.Node
    structure DstIL = Params.DstIL
    structure DstNd = DstIL.Node
    structure DstCFG = DstIL.CFG

    type var_env = Params.var_env

    datatype env = E of {
	ndMap : DstIL.node Stamp.Tbl.hash_table,
	vMap : var_env
      }

    fun rename (E{vMap, ...}) x = Params.rename(vMap, x)

    fun expand (E{vMap, ...}, assign) = Params.expand (vMap, assign)

    fun insertNd (E{ndMap, ...}, id, nd) = Stamp.Tbl.insert ndMap (id, nd)

    fun findNd (E{ndMap, ...}) = Stamp.Tbl.find ndMap

    fun renameNd (E{ndMap, ...}) (nd as SrcIL.ND{id, ...}) = (
	  case Stamp.Tbl.find ndMap id
	   of SOME nd' => nd'
	    | NONE => raise Fail("unable to find " ^ SrcNd.toString nd)
	  (* end case *))

    fun translateCFG (env, SrcIL.CFG{entry, exit}) = let
	  val findNd = findNd env
	  fun trans (SrcIL.ND{id, kind, ...}) = let
		fun newNd nd = (insertNd (env, id, nd); nd)
		in
		  case findNd id
		   of SOME nd => nd
		    | NONE => (case kind
			 of SrcIL.NULL => raise Fail "unexpected NULL node"
			  | SrcIL.ENTRY{succ} => let
			      val nd = newNd (DstNd.mkENTRY())
			      in
				DstNd.addEdge (nd, trans (!succ));
				nd
			      end
			  | SrcIL.JOIN{phis, succ, ...} => let
			      fun cvtPhi (x, xs) =
				    (rename env x, List.map (rename env) xs)
			      val nd = newNd (DstNd.mkJOIN(List.map cvtPhi (!phis)))
			      in
				DstNd.addEdge (nd, trans (!succ));
				nd
			      end
			  | SrcIL.COND{cond, trueBranch, falseBranch, ...} => let
			      val nd = newNd (DstNd.mkCOND{
				      cond = rename env cond,
				      trueBranch = DstNd.dummy,
				      falseBranch = DstNd.dummy
				    })
			      val trueB = trans (!trueBranch)
			      val _ = (DstNd.setTrueBranch (nd, trueB); DstNd.setPred(trueB, nd))
			      val falseB = trans (!falseBranch)
			      val _ = (DstNd.setTrueBranch (nd, falseB); DstNd.setPred(falseB, nd))
			      in
				nd
			      end
			  | SrcIL.COM{text, succ, ...} => let
			      val nd = newNd (DstNd.mkCOM text)
			      in
				DstNd.addEdge (nd, trans (!succ));
				nd
			      end
			  | SrcIL.ASSIGN{stm, succ, ...} => let
			      val DstIL.CFG{entry, exit} = expand (env, stm)
			      in
				DstNd.addEdge (!exit, trans (!succ));
				!entry
			      end
			  | SrcIL.NEW{actor, args, succ, ...} => let
			      val nd = newNd (DstNd.mkNEW{
				      actor = actor,
				      args = List.map (rename env) args
				    })
			      in
				DstNd.addEdge (nd, trans (!succ));
				nd
			      end
			  | SrcIL.DIE _ => newNd (DstNd.mkDIE())
			  | SrcIL.STABILIZE _ => newNd (DstNd.mkSTABILIZE())
			  | SrcIL.EXIT _ => newNd (DstNd.mkEXIT())
		       (* end case *))
		  (* end case *)
		end
	  val entry = trans (!entry)
	  in
	    DstIL.CFG{entry = ref entry, exit = ref (renameNd env (!exit))}
	  end

    fun translate (vMap, cfg) = let
	  val env = E{
		  ndMap = Stamp.Tbl.mkTable (256, Fail "ndMap"),
		  vMap = vMap
		}
	  in
	    translateCFG (env, cfg)
	  end

  end

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