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

SCM Repository

[diderot] View of /trunk/src/compiler/tree-il/tree-il-pp.sml
ViewVC logotype

View of /trunk/src/compiler/tree-il/tree-il-pp.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1232 - (download) (annotate)
Mon May 16 23:37:52 2011 UTC (8 years, 5 months ago) by jhr
File size: 6346 byte(s)
  Porting many changes from the pure-cfg branch, including value numbering
  and support for parallel execution on SMP systems.
(* tree-il-pp.sml
 *
 * COPYRIGHT (c) 2011 The Diderot Project (http://diderot-language.cs.uchicago.edu)
 * All rights reserved.
 *
 * Printing for the TreeIL
 *)

structure TreeILPP : sig

    val statement : TextIO.outstream * TreeIL.stm -> unit

    val block : TextIO.outstream * TreeIL.block -> unit

    val program : TextIO.outstream * TreeIL.program -> unit

  end = struct

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

    fun indent (outS, i) = TextIO.output(outS, StringCvt.padLeft #" " i "")
    fun incIndent (outS, i) = (outS, i+2)
    fun decIndent (outS, i) = (outS, Int.max(0, 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 expToString e = let
	  fun argsToS (lp, args, rp, l) = let
		fun argToS ([], l) = l
		  | argToS ([e], l) = toS (e, l) 
		  | argToS (e::es, l) = toS(e, "," :: argToS(es, l))
		in
		  lp :: argToS(args, rp :: l)
		end
	  and toS (IL.E_Var x, l) = Var.name x :: l
	    | toS (IL.E_Lit lit, l) = Literal.toString lit :: l
	    | toS (IL.E_Op(rator, args), l) = Op.toString rator :: argsToS ("(", args, ")", l)
	    | toS (IL.E_Apply(f, args), l) = ILBasis.toString f :: argsToS ("(", args, ")", l)
	    | toS (IL.E_Cons(ty, args), l) =
		"<" :: Ty.toString ty :: ">" :: argsToS ("{", args, "}", l)
	  in
	    String.concat (toS (e, []))
	  end

    fun argsToString (prefix, es) = String.concat[
	    prefix, "(", String.concatWith "," (List.map expToString es), ")"
	  ]

    fun ppVarDecl out x = prln (out, [Ty.toString(Var.ty x), " ", Var.name x, ";\n"])

    fun ppStrand out (IL.Strand{name, params, state, stateInit, methods}) = let
	  val out' = incIndent out
	  fun ppParams [] = ()
	    | ppParams [x] = prl(out, [Ty.toString (Var.ty x), " ", Var.name x])
	    | ppParams (x::r) = (
		prl(out, [Ty.toString (Var.ty x), " ", Var.name x, ","]);
		ppParams r)
	  fun ppSVarDecl (IL.SV{varying, output, var}) = let
		val v = if varying then "varying " else ""
		val out = if output then "output " else ""
		in
		  prln (out', [v, out, Ty.toString(Var.ty var), " ", Var.name var, ";\n"])
		end
	  in
	    prln (out, ["strand ", Atom.toString name, " ("]);
	    ppParams params; pr(out, ") {\n");
	    List.app ppSVarDecl state;
	    prln (out', ["init () "]); ppBlock(out', stateInit); pr (out', "\n");
	    List.app (ppMethod out') methods;
	    prln (out, ["}\n"])
	  end

    and ppMethod out (IL.Method{name, body}) = (
	  prln (out, [Atom.toString name, " () "]);
	  ppBlock (out, body);
	  pr (out, "\n"))

    and ppBlock (out, IL.Block{locals, body}) = let
	  val out' = incIndent out
	  in
	    pr (out, "{\n");
	    List.app (ppVarDecl out') locals;
	    List.app (fn stm => ppStm(out', stm)) body;
	    indent out; pr (out, "}")
	  end

    and ppStm (out, stm) = (case stm
	   of IL.S_Comment text => let
		val out = decIndent out
		in
		  List.app (fn s => prln(out, ["// ", s, "\n"])) text
		end
	    | IL.S_Assign(x, e) => prln(out, [Var.name x, " = ", expToString e, ";\n"])
	    | IL.S_IfThen(cond, blk) => (
		prln (out, ["if (", expToString cond, ") "]);
		ppBlock (out, blk);
		pr (out, "\n"))
	    | IL.S_IfThenElse(cond, blk1, blk2) => (
		prln (out, ["if (", expToString cond, ") "]);
		ppBlock (out, blk1);
		pr (out, " else ");
		ppBlock (out, blk2);
		pr (out, "\n"))
(*
	    | IL.S_For(x, e1, e2, blk) => (
		prln (out, [
		    "for (", Ty.toString(Var.ty x), " ", Var.name x, " = ",
		    expToString e1, " ..", expToString e2, ") "
		  ]);
		ppBlock (out, blk);
		pr (out, "\n"))
*)
	    | IL.S_New(strand, args) =>
		prln (out, [argsToString("new "^Atom.toString strand, args), ";\n"])
	    | IL.S_LoadImage(x, dim, exp) =>
		prln (out, [
		    Var.name x, " = load<", Int.toString dim, "> (", expToString exp, ");\n"
		  ])
	    | IL.S_Input(x, name, NONE) =>
		prln (out, [
		    Var.name x, " = input<", Ty.toString(Var.ty x), "> (\"",
		    String.toString name, "\");\n"
		  ])
	    | IL.S_Input(x, name, SOME dflt) =>
		prln (out, [
		    Var.name x, " = input<", Ty.toString(Var.ty x), "> (\"",
		    String.toString name, "\",", expToString dflt, ");\n"
		  ])
	    | IL.S_Exit es => prln (out, [argsToString("exit", es), ";\n"])
	  (* return functions for methods *)
	    | IL.S_Active es => prln (out, [argsToString("active", es), ";\n"])
	    | IL.S_Stabilize es => prln (out, [argsToString("stabilize", es), ";\n"])
	    | IL.S_Die => prln (out, ["die;\n"])
	  (* end case *))

    fun statement (outS, stm) = ppStm((outS, 0), stm)

    fun block (outS, blk) = (ppBlock ((outS, 0), blk); pr ((outS, 0), "\n"))

    fun ppInitially (out, {isArray, iterPrefix, iters, createPrefix, strand, args}) = let
	  fun ppBlock (out, IL.Block{locals, body}, inside) = let
		val out' = incIndent out
		in
		  pr (out, "{\n");
		  List.app (ppVarDecl out') locals;
		  List.app (fn stm => ppStm(out', stm)) body;
		  inside out';
		  indent out; pr (out, "}")
		end
	  fun ppCreate out = let
		fun ppNew out = prln(out, [argsToString("new "^Atom.toString strand, args), ";\n"])
		in
		  indent out; ppBlock (out, createPrefix, ppNew);  pr(out, "\n")
		end
	  fun ppIters out = let
		fun ppIter (out, []) = ppCreate out
		  | ppIter (out, (i, lo, hi)::iters) = (
		      prln(out, ["for ", Var.name i, " = ", expToString lo, " .. ", expToString hi, "\n"]);
		      ppIter (incIndent out, iters))
		in
		  ppIter (out, iters)
		end
	  in
	    indent out; pr(out, if isArray then "ARRAY " else "COLLECTION ");
	    ppBlock (out, iterPrefix, ppIters);
	    pr (out, "\n")
	  end

    fun program (outS, IL.Program{props, globals, globalInit, strands, initially}) = let
	  val out = (outS, 0)
	  val out' = incIndent out
	  in
	    pr(out, "//***** PROPERTIES *****\n");
	    case props
	     of [] => prln(out', ["none\n"])
	      | _ => prln(out', [String.concatWith " " (List.map IL.propToString props), "\n"])
	    (* end case *);
	    prln(out, ["//***** GLOBALS *****\n"]);
	    List.app (ppVarDecl out') globals;
	    prln(out, ["//***** GLOBAL INIT *****\n"]);
	    indent out'; ppBlock (out', globalInit); pr (out, "\n");
	    prln(out, ["//***** STRANDS *****\n"]);
	    List.app (ppStrand out) strands;
	    prln(out, ["//***** INITIALLY *****\n"]);
	    ppInitially (out', initially)
	  end

  end

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