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/ssa-pp-fn.sml
ViewVC logotype

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 505 - (download) (annotate)
Thu Feb 3 17:03:20 2011 UTC (10 years, 7 months ago) by jhr
File size: 4560 byte(s)
  Add better IL pretty printing
(* ssa-pp-fn.sml
 *
 * COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu)
 * All rights reserved.
 *
 * Pretty printing for SSA representations
 *)

functor SSAPPFn (IL : SSA) : sig

    val assignToString : IL.assign -> string

    val output : TextIO.outstream * string * IL.program -> unit

  end = struct

    structure Op = IL.Op
    structure Var = IL.Var
    structure Ty = IL.Ty

    local
      val {getFn, setFn} = IL.Node.newFlag()
    in
    val isMarked = getFn
    fun mark nd = setFn(nd, true)
    fun clear nd = setFn(nd, false)
    end

    fun indent (outS, i) = TextIO.output(outS, StringCvt.padLeft #" " i "")
    fun incIndent (outS, i) = (outS, i+2)
    fun pr ((outS, _), s) = TextIO.output(outS, s)
    fun prl (out, l) = pr(out, concat l)
    fun prln (out, l) = (indent out; prl(out, l))

    fun typedVar x = String.concat [Ty.toString(Var.ty x), " ", Var.toString x]

    fun assignToString (y, rhs) = let
	  val rhs = (case rhs
		 of IL.VAR x => [Var.toString x, ";"]
		  | IL.LIT lit => [Literal.toString lit, ";"]
		  | IL.OP(rator, []) => [Op.toString rator, ";"]
		  | IL.OP(rator, args) => [
			Op.toString rator, "(",
			String.concatWith "," (List.map Var.toString args), ");"
		      ]
		  | IL.CONS xs => ["[", String.concatWith "," (List.map Var.toString xs), "];"]
		(* end case *))
	  in
	    String.concat(typedVar y :: " = " :: rhs)
	  end

    fun labelOf (IL.ND{id, ...}) = "L"^Stamp.toString id

    fun ppCFG (out, IL.CFG{entry, exit}) = let
	  fun ppNd (out, nd) = let
		val out1 = incIndent out
		fun prPhi (y, xs) =
		      prln (out1, [
			  typedVar y, " = phi(",
			  String.concatWith "," (List.map Var.toString xs), ")\n"
			])
		in
		  if isMarked nd
		    then ()
		    else (
		      prln (out, [
			  IL.Node.toString nd, ":  preds = [",
			  String.concatWith "," (List.map IL.Node.toString (IL.Node.preds nd)),
			  "]\n"
			]);
		      case IL.Node.kind nd
		       of IL.NULL => ()
			| IL.ENTRY{succ} => ppNd (out, !succ)
			| IL.JOIN{phis, succ, ...} => (
			    List.app prPhi (!phis);
			    ppNd (out, !succ))
			| IL.COND{cond, trueBranch, falseBranch, ...} => (
			    prln (out1, [
				"if ", Var.toString cond,
				" then goto ", IL.Node.toString(!trueBranch),
				" else goto ", IL.Node.toString(!falseBranch), "\n"
			      ]);
			    ppNd (out, !trueBranch);
			    ppNd (out, !falseBranch))
			| IL.COM{text, succ, ...} => (
			    List.app (fn s => prln (out1, ["//", s, "\n"])) text;
			    ppNd (out, !succ))
			| IL.ASSIGN{stm, succ, ...} => (
			    prln (out1, [assignToString stm, "\n"]);
			    ppNd (out, !succ))
			| IL.NEW{strand, args, succ, ...} => (
			    prln (out1, [
				"new ", Atom.toString strand, "(",
				String.concatWith "," (List.map Var.toString args), ");\n"
			      ]);
			    ppNd (out, !succ))
			| IL.DIE _ => ()
			| IL.STABILIZE _ => ()
			| IL.EXIT _ => ()
		      (* end case *))
		end
	  in
	    ppNd (out, entry)
	  end

    fun ppMethod (out, IL.Method{name, stateIn, stateOut, body}) = let
	  val out1 = incIndent out
	  fun prVars xs = List.app (fn x => prl(out, [" ", typedVar x, ";"])) xs
	  in
	    indent out; prl(out, ["method ", Atom.toString name, "\n"]);
	    indent out1; pr(out1, "state in: "); prVars stateIn; pr(out1, "\n");
	    ppCFG (incIndent out1, body);
	    indent out1; pr(out1, "state out:"); prVars stateOut; pr(out1, "\n");
	    indent out; prl(out, ["end ", Atom.toString name, "\n"])
	  end

    and ppStrand (out, IL.Strand{name, params, state, stateInit, methods}) = let
	  val out1 = incIndent out
	  fun prVars xs = List.app (fn x => prl(out, [" ", typedVar x, ";"])) xs
	  in
	    indent out;
	    prl(out, [
		"strand ", Atom.toString name, " (",
		String.concatWith ", " (List.map typedVar params), ")\n"
	      ]);
	    indent out1; pr(out1, "state: "); prVars state; pr(out1, "\n");
	    ppCFG (incIndent out1, stateInit);
	    List.app (fn m => ppMethod(out1, m)) methods;
	    indent out; prl(out, ["end ", Atom.toString name, "\n"])
	  end

    fun output (outS, msg, IL.Program{globals, globalInit, strands}) = let
	  val out = (outS, 0)
	  val out1 = incIndent out
	  in
	    pr (out, concat["##### ", msg, " ####\n"]);
	    pr (out, "## globals\n");
	    List.app
	      (fn x => (indent out1; prl(out1, ["global ", typedVar x, "\n"]))) 
		globals;
	    pr (out, "## global initialization\n");
	    ppCFG (out1, globalInit);
	    pr (out, "## strands\n");
	    List.app (fn strand => ppStrand(out1, strand)) strands;
	    pr (out, "#### end program ####\n")
	  end

  end

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