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

SCM Repository

[diderot] View of /branches/vis15/src/compiler/tree-ir/tree-pp.sml
ViewVC logotype

View of /branches/vis15/src/compiler/tree-ir/tree-pp.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3835 - (download) (annotate)
Sun May 8 13:10:17 2016 UTC (3 years, 2 months ago) by jhr
File size: 9293 byte(s)
  working on merge
(* tree-pp.sml
 *
 * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
 *
 * COPYRIGHT (c) 2016 The University of Chicago
 * All rights reserved.
 *
 * Printing for the TreeIR
 *)

structure TreePP : sig

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

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

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

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

  end = struct

    structure IR = TreeIR
    structure Ty = TreeTypes
    structure Op = TreeOps
    structure Var = TreeVar
    structure GVar = TreeGlobalVar
    structure SVar = TreeStateVar

    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 descToString NONE = ""
      | descToString (SOME desc) = String.toString desc

    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 (IR.E_Global x, l) = "::" :: GVar.toString x :: l
            | toS (IR.E_State x, l) = SVar.toString x :: l
            | toS (IR.E_Var x, l) = Var.name x :: l
            | toS (IR.E_Lit lit, l) = Literal.toString lit :: l
            | toS (IR.E_Op(rator, args), l) = Op.toString rator :: argsToS ("(", args, ")", l)
            | toS (IR.E_Cons(args, ty), l) =
                "<" :: Ty.toString ty :: ">" :: argsToS ("[", args, "]", l)
            | toS (IR.E_Seq(args, ty), l) =
                "<" :: Ty.toString ty :: ">" :: argsToS ("{", args, "}", l)
	    | toS (IR.E_Pack es, l) = "PACK" :: argsToS ("(", es, ")", l)
	    | toS (IR.E_Select(e, i), l) = "SELECT_" :: Int.toString i :: "(" :: toS(e, ")" :: l)
          in
            String.concat (toS (e, []))
          end

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

    fun ppGlobalDecl out x = prln (out, [
            if GVar.isInput x then "input " else "",
            Ty.toString(GVar.ty x), " ", GVar.name x, ";\n"
          ])

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

    fun ppStrand out strand = let
	  val IR.Strand{name, params, state, stateInit, initM, updateM, stabilizeM} = strand
          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 sv = let
                val v = if SVar.isVarying sv then "varying " else ""
                val out = if SVar.isOutput sv then "output " else ""
                in
                  prln (out', [v, out, Ty.toString(SVar.ty sv), " ", SVar.name sv, ";\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");
            ppMethod (out', "initially", initM);
            ppMethod (out', "update", SOME updateM);
            ppMethod (out', "stabilize", stabilizeM);
            prln (out, ["}\n"])
          end

    and ppMethod (out, name, SOME body) = (
          prln (out, [name, " () "]);
          ppBlock (out, body);
          pr (out, "\n"))
      | ppMethod (_, _, NONE) = ()

    and ppBlock (out, IR.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 IR.S_Comment text => let
                val out = decIndent out
                in
                  List.app (fn s => prln(out, ["// ", s, "\n"])) text
                end
	    | IR.S_Unpack([], e) => raise Fail "empty lhs for Unpack"
	    | IR.S_Unpack(x::xs, e) => (
                prln(out, ["(", Var.name x]);
                List.app (fn x => prl(out, [",", Var.name x])) xs;
                prl (out, [") = UNPACK(", expToString e, ");\n"]))
            | IR.S_Assign(x, e) => prln(out, [Var.name x, " = ", expToString e, ";\n"])
            | IR.S_GAssign(x, e) => prln(out, [GVar.name x, " = ", expToString e, ";\n"])
            | IR.S_IfThen(cond, blk) => (
                prln (out, ["if (", expToString cond, ") "]);
                ppBlock (out, blk);
                pr (out, "\n"))
            | IR.S_IfThenElse(cond, blk1, blk2) => (
                prln (out, ["if (", expToString cond, ") "]);
                ppBlock (out, blk1);
                pr (out, " else ");
                ppBlock (out, blk2);
                pr (out, "\n"))
            | IR.S_Foreach(x, e, blk) => (
                prln (out, [
                    "foreach ", Ty.toString(Var.ty x), " ", Var.name x, " in ", expToString e, " "
                  ]);
                ppBlock (out, blk);
                pr (out, "\n"))
            | IR.S_LoadNrrd(x, nrrd) =>
                prln (out, [
                    Var.name x, " = load<", Ty.toString(Var.ty x), "> (\"",
		    String.toString nrrd, "\");\n"
                  ])
            | IR.S_Input(x, name, desc, NONE) =>
                prln (out, [
                    GVar.name x, " = input<", Ty.toString(GVar.ty x), "> (\"",
                    String.toString name, "\",\"", descToString desc, "\");\n"
                  ])
            | IR.S_Input(x, name, desc, SOME dflt) =>
                prln (out, [
                    GVar.name x, " = input<", Ty.toString(GVar.ty x), "> (\"",
                    String.toString name, "\",\"", descToString desc, "\",",
                    expToString dflt, ");\n"
                  ])
            | IR.S_InputNrrd(x, name, desc, NONE) =>
                prln (out, [
                    GVar.name x, " = input-nrrd<", Ty.toString(GVar.ty x), "> (\"",
                    String.toString name, "\",\"", descToString desc, "\");\n"
                  ])
            | IR.S_InputNrrd(x, name, desc, SOME dflt) =>
                prln (out, [
                    GVar.name x, " = input-nrrd<", Ty.toString(GVar.ty x), "> (\"",
                    String.toString name, "\",\"", descToString desc, "\",\"",
                    String.toString dflt, "\");\n"
                  ])
            | IR.S_New(strand, args) =>
                prln (out, [argsToString("new "^Atom.toString strand, args), ";\n"])
            | IR.S_Save(x, rhs) =>
                prln (out, [SVar.toString x, " = ", expToString rhs, ";\n"])
            | IR.S_Exit => prln (out, ["exit;\n"])
	    | IR.S_Print(_, es) => prln (out, [argsToString("print", es), ";\n"])
          (* return functions for methods *)
            | IR.S_Active => prln (out, ["active;\n"])
            | IR.S_Stabilize => prln (out, ["stabilize;\n"])
            | IR.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 program (outS, prog) = let
	  val IR.Program{
		props, consts, inputs, constInit, globals, globalInit, strand, create, update
	      } = prog
          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 Properties.toString props), "\n"])
            (* end case *);
            prln(out, ["//***** CONSTS *****\n"]);
            List.app (ppGlobalDecl out') consts;
            prln(out, ["//***** INPUTS *****\n"]);
(* FIXME
            List.app (ppGlobalDecl out') inputs;
*)
            prln(out, ["//***** CONST INIT *****\n"]);
            indent out'; ppBlock (out', constInit); pr (out, "\n");
            prln(out, ["//***** GLOBALS *****\n"]);
            List.app (ppGlobalDecl out') globals;
            prln(out, ["//***** GLOBAL INIT *****\n"]);
            indent out'; ppBlock (out', globalInit); pr (out, "\n");
            prln(out, ["//***** STRAND *****\n"]);
            ppStrand out strand;
            prln(out, ["//***** CREATE *****\n"]);
(* FIXME
            indent out'; ppBlock (out', create); pr (out, "\n");
*)
	    case update
	     of SOME blk => (
		  prln(out, ["//***** GLOBAL UPDATE *****\n"]);
		  indent out'; ppBlock (out', blk); pr (out, "\n"))
	      | NONE => ()
	    (* end case *)
          end

    fun output (outS, msg, prog) = (
	  pr ((outS, 0), concat["##### TreeIR: ", msg, " ####\n"]);
	  program (outS, prog);
	  pr ((outS, 0), "#### end program ####\n"))

  end

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