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

SCM Repository

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

View of /branches/vis15/src/compiler/cfg-ir/ssa-pp-fn.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3512 - (download) (annotate)
Fri Dec 18 18:52:06 2015 UTC (3 years, 9 months ago) by jhr
File size: 11174 byte(s)
merged in printing for Ein
(* ssa-pp-fn.sml
 *
 * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
 *
 * COPYRIGHT (c) 2015 The University of Chicago
 * All rights reserved.
 *
 * Pretty printing for SSA representations
 *)

functor SSAPPFn (IR : SSA) : sig

    val assignToString : IR.assign -> string

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

  end = struct

    structure Op = IR.Op
    structure Var = IR.Var
    structure GVar = IR.GlobalVar
    structure Ty = IR.Ty

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

  (* if true, the output is in compact form *)
    val compact = ref true

    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, "#", Int.toString(Var.useCount x)
          ]

    fun assignToString (y, rhs) = let
          val rhs = (case rhs
                 of IR.GLOBAL x => [GVar.toString x, ";"]
                  | IR.STATE x => [IR.StateVar.toString x, ";"]
                  | IR.VAR x => [Var.toString x, ";"]
                  | IR.LIT lit => [Literal.toString lit, ";"]
                  | IR.OP(rator, []) => [Op.toString rator, ";"]
                  | IR.OP(rator, args) => [
                        Op.toString rator, "(",
                        String.concatWithMap "," Var.toString args, ");"
                      ]
                  | IR.CONS(xs, ty) => [
                        "<", Ty.toString ty, ">[",
                        String.concatWithMap "," Var.toString xs, "];"
                      ]
                  | IR.SEQ(xs, ty) => [
                        "<", Ty.toString ty, ">{",
                        String.concatWithMap "," Var.toString xs, "};"
                      ]
                  | IR.EINAPP(ein, args) => [
                        EinPP.toString ein, " (",
                        String.concatWithMap "," Var.toString args, ");"
                      ]
                (* end case *))
          in
            String.concat(typedVar y :: " = " :: rhs)
          end

    fun massignToString ([], rator, xs) = String.concat [
            Op.toString rator, "(", String.concatWithMap "," Var.toString xs, ");"
          ]
      | massignToString (ys, rator, xs) = String.concat [
            "(", String.concatWith "," (List.map typedVar ys), ") = ",
            Op.toString rator, "(", String.concatWithMap "," Var.toString xs, ");"
          ]

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

    fun ppCFG (out, cfg as IR.CFG{entry, exit}) = let
          fun goto (out, nd) = (case IR.Node.kind nd
                 of IR.JOIN _ => (
                      prln(incIndent out, ["goto ", IR.Node.toString nd, "\n"]);
                      ppNd (out, false, nd))
		  | IR.FOREACH _ =>
		      if isMarked nd
                        then prln(incIndent out, ["goto ", IR.Node.toString nd, "\n"])
			else ppNd (out, false, nd)
                  | _ => ppNd (out, true, nd)
                (* end case *))
          and ppNd (out, noLabel, nd) = let
                val out1 = incIndent out
                fun prPhi out (y, xs) =
                      prln (out, [
                          typedVar y, " = phi(",
                          String.concatWith "," (List.mapPartial (Option.map Var.toString) xs), ")\n"
                        ])
                in
                  if isMarked nd
                    then ()
                    else (
                      mark nd;
                      if noLabel andalso (! compact)
                        then ()
                        else (case IR.Node.kind nd
                           of IR.JOIN{preds, mask, ...} => let
                                val preds = ListPair.mapEq
                                      (fn (false, nd) => IR.Node.toString nd
                                        | (true, nd) => "*" ^ IR.Node.toString nd)
                                      (!mask, !preds)
(* +DEBUG*)
handle ex => (
print(concat["**** Broken CFG at ", IR.Node.toString nd, "\n"]);
List.map IR.Node.toString (!preds))
(* -DEBUG*)
                                in
                                  prln (out, [
                                      IR.Node.toString nd, ":  preds = [",
                                      String.concatWith "," preds, "]\n"
                                    ])
                                end
                            | _ => prln (out, [
                                  IR.Node.toString nd, ":  preds = [",
                                  String.concatWith "," (List.map IR.Node.toString (IR.Node.preds nd)),
                                  "]\n"
                                ])
                          (* end case *));
                      case IR.Node.kind nd
                       of IR.NULL => ()
                        | IR.ENTRY{succ} => goto (out, !succ)
                        | IR.JOIN{phis, succ, ...} => (
                            List.app (prPhi out1) (!phis);
                            goto (out, !succ))
                        | IR.COND{cond, trueBranch, falseBranch, ...} => (
                            prln (out1, [
                                "if ", Var.toString cond,
                                " then goto ", IR.Node.toString(!trueBranch),
                                " else goto ", IR.Node.toString(!falseBranch), "\n"
                              ]);
                            ppNd (out, false, !trueBranch);
                            ppNd (out, false, !falseBranch))
			| IR.FOREACH{phis, var, src, bodyEntry, succ, ...} => (
			    prln (out1, [
				"foreach (", Var.toString var, " in ", Var.toString src, ")",
				" on exit goto ", IR.Node.toString(!succ), "\n"
			      ]);
			    List.app (prPhi (incIndent out1)) (!phis);
                            goto (out, !bodyEntry);
			    ppNd (out, false, !succ))
                        | IR.COM{text, succ, ...} => (
                            List.app (fn s => prln (out1, ["//", s, "\n"])) text;
                            goto (out, !succ))
                        | IR.ASSIGN{stm, succ, ...} => (
                            prln (out1, [assignToString stm, "\n"]);
                            goto (out, !succ))
                        | IR.MASSIGN{stm, succ, ...} => (
                            prln (out1, [massignToString stm, "\n"]);
                            goto (out, !succ))
                        | IR.GASSIGN{lhs, rhs, succ, ...} => (
                            prln (out1, [
                                GVar.toString lhs, " = ", Var.toString rhs, ";\n"
                              ]);
                            goto (out, !succ))
                        | IR.NEW{strand, args, succ, ...} => (
                            prln (out1, [
                                "new ", Atom.toString strand, "(",
                                String.concatWith "," (List.map Var.toString args), ");\n"
                              ]);
                            goto (out, !succ))
                        | IR.SAVE{lhs, rhs, succ, ...} => (
                            prln (out1, [
                                IR.StateVar.toString lhs, " = ", Var.toString rhs, ";\n"
                              ]);
                            goto (out, !succ))
                        | IR.EXIT{kind, succ, ...} => (
			    case kind
			     of ExitKind.RETURN => prln (out1, ["return\n"])
			      | ExitKind.ACTIVE => prln (out1, ["active\n"])
			      | ExitKind.STABILIZE => prln (out1, ["stabilize\n"])
			      | ExitKind.DIE => prln (out1, ["die\n"])
			      | ExitKind.UNREACHABLE => prln (out1, ["unreachable\n"])
			    (* end case *);
			    case !succ
			     of SOME nd => goto (out, nd)
			      | _ => ()
			    (* end case *))
                      (* end case *))
                end
          in
            ppNd (out, false, entry);
          (* clear marks *)
            IR.CFG.apply clear cfg
          end

    fun ppCreate (out, IR.Create{dim, code}) = (
	  indent out;
	  case dim
	   of NONE => pr (out, "COLLECTION\n")
	    | SOME dim => prl (out, ["GRID(", Int.toString dim, ")\n"])
	  (* end case *);
	  ppCFG (incIndent out, code))

    fun ppMethod (out, name) body = let
          val out1 = incIndent out
          in
            indent out; prl(out, ["method ", name, "\n"]);
            ppCFG (incIndent out1, body);
            indent out; prl(out, ["end ", name, "\n"])
          end

    and ppStrand (out, IR.Strand{name, params, state, stateInit, initM, updateM, stabilizeM}) = let
          val out1 = incIndent out
          fun stateVar x = let
                val l = [Ty.toString(IR.StateVar.ty x), " ", IR.StateVar.toString x, ";"]
                val l = if IR.StateVar.isOutput x then "output " :: l else l
                in
                  prl (out1, l)
                end
          in
            indent out;
            prl(out, [
                "strand ", Atom.toString name, " (",
                String.concatWithMap ", " typedVar params, ")\n"
              ]);
            indent out1; pr(out1, "state: "); List.app stateVar state; pr(out1, "\n");
            ppCFG (incIndent out1, stateInit);
	    Option.app (ppMethod (out1, "initially")) initM;
	    ppMethod (out1, "update") updateM;
	    Option.app (ppMethod (out1, "stabilize")) stabilizeM;
            indent out; prl(out, ["end ", Atom.toString name, "\n"])
          end

    fun ppGlobal prefix outS gv = (
	  indent outS;
	  prl(outS, [
	      prefix, " ", Ty.toString(GVar.ty gv), " ", GVar.uniqueName gv, "\n"
	    ]))

    fun ppInput outS inp = prl (outS, ["input ", Inputs.toString inp, "\n"])

    fun output (outS, msg, prog) = let
	  val IR.Program{
		  props, consts, inputs, globals,
		  constInit, globalInit, strand, create, update
		} = prog
          val out = (outS, 0)
          val out1 = incIndent out
          in
            pr (out, concat["##### ", IR.irName, ": ", msg, " ####\n"]);
            pr (out, "## properties\n");
            prln (out1, [
		String.concatWithMap " " Properties.toString props, "\n"
	      ]);
            pr (out, "## globals\n");
            List.app (ppGlobal "const" out1) consts;
            List.app (ppInput out1) inputs;
            List.app (ppGlobal "global" out1) globals;
            pr (out, "## input initialization\n");
            ppCFG (out1, constInit);
            pr (out, "## global initialization\n");
            ppCFG (out1, globalInit);
            pr (out, "## strand\n");
            ppStrand (out1, strand);
            pr (out, "## initial strand creation\n");
            ppCreate (out1, create);
	    case update
	     of SOME cfg => (
		  pr (out, "## global update\n");
		  ppCFG (out1, cfg))
	      | NONE => ()
	    (* end case *);
            pr (out, "#### end program ####\n")
          end

  end

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