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

SCM Repository

[diderot] Annotation of /trunk/src/compiler/IL/ssa-pp-fn.sml
ViewVC logotype

Annotation of /trunk/src/compiler/IL/ssa-pp-fn.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 350 - (view) (download)

1 : jhr 198 (* ssa-pp-fn.sml
2 :     *
3 :     * COPYRIGHT (c) 2010 The Diderot Project (http://diderot.cs.uchicago.edu)
4 :     * All rights reserved.
5 :     *
6 :     * Pretty printing for SSA representations
7 :     *)
8 :    
9 :     functor SSAPPFn (IL : SSA) : sig
10 :    
11 : jhr 350 val assignToString : IL.assign -> string
12 :    
13 : jhr 340 val output : TextIO.outstream * string * IL.program -> unit
14 : jhr 256
15 : jhr 198 end = struct
16 :    
17 : jhr 256 structure Op = IL.Op
18 :     structure Var = IL.Var
19 :    
20 :     fun indent (outS, i) = TextIO.output(outS, StringCvt.padLeft #" " i "")
21 :     fun incIndent (outS, i) = (outS, i+2)
22 :     fun pr ((outS, _), s) = TextIO.output(outS, s)
23 :     fun prl (out, l) = pr(out, concat l)
24 :    
25 : jhr 350 fun assignToString (y, rhs) = let
26 :     val rhs = (case rhs
27 :     of IL.VAR x => [Var.toString x, ";"]
28 :     | IL.LIT lit => [Literal.toString lit, ";"]
29 :     | IL.OP(rator, []) => [Op.toString rator, ";"]
30 :     | IL.OP(rator, args) => [
31 :     Op.toString rator, "(",
32 :     String.concatWith "," (List.map Var.toString args), ");"
33 :     ]
34 :     | IL.CONS xs => ["[", String.concatWith "," (List.map Var.toString xs), "];"]
35 :     (* end case *))
36 :     in
37 :     String.concat(Var.toString y :: " = " :: rhs)
38 :     end
39 : jhr 256
40 :     fun labelOf (IL.STM{id, ...}) = "L"^Stamp.toString id
41 :    
42 :     fun ppStmt (out, stm as IL.STM{id, kind, next, ...}) = let
43 :     val out1 = incIndent out
44 :     fun prPhi (y, xs) = (
45 :     indent out1;
46 :     prl (out1, [
47 :     Var.toString y, " = phi(",
48 :     String.concatWith "," (List.map Var.toString xs), ")\n"
49 :     ]))
50 :     in
51 :     indent out;
52 :     prl (out, [
53 :     IL.Stmt.toString stm, "(", IL.Node.toString(IL.Stmt.entry stm), "): # preds = [",
54 :     String.concatWith "," (List.map IL.Node.toString (IL.Node.preds(IL.Stmt.entry stm))),
55 :     "]\n"
56 :     ]);
57 :     case kind
58 :     of IL.S_SIMPLE(IL.ND{kind, ...}) => (case kind
59 :     of IL.NULL => raise Fail "unexpected S_SIMPLE with NULL node"
60 :     | IL.ENTRY _ => (indent out1; pr(out1, "entry;\n"))
61 :     | IL.JOIN{phis, ...} => List.app prPhi (!phis)
62 :     | IL.COND _ => raise Fail "unexpected S_SIMPLE with COND node"
63 :     | IL.BLOCK{body=ref [], ...} => (indent out1; pr (out, "empty;\n"))
64 :     | IL.BLOCK{body, ...} => (
65 :     List.app
66 : jhr 350 (fn assign => (indent out1; prl(out1, [assignToString assign, "\n"])))
67 :     (!body))
68 : jhr 256 | IL.NEW{actor, args, ...} => (
69 :     indent out1;
70 :     prl (out1, [
71 :     "new ", Atom.toString actor, "(",
72 :     String.concatWith "," (List.map Var.toString args), ");\n"
73 :     ]))
74 :     | IL.DIE _ => (indent out1; pr(out1, "die;\n"))
75 :     | IL.STABILIZE _ => (indent out1; pr(out1, "stabilize;\n"))
76 :     | IL.EXIT _ => (indent out1; pr(out1, "exit;\n"))
77 :     (* end case *))
78 :     | IL.S_IF{cond=IL.ND{kind=IL.COND{cond, ...}, ...}, thenBranch, elseBranch} => (
79 :     indent out1; prl(out1, ["if ", Var.toString cond, " then\n"]);
80 :     ppStmt (incIndent out1, thenBranch);
81 :     indent out1; pr(out1, "else\n");
82 :     ppStmt (incIndent out1, elseBranch);
83 :     indent out1; pr(out1, "endif;\n"))
84 :     | IL.S_LOOP _ => raise Fail "LOOP"
85 :     (* end case *);
86 :     ppNext (out, next)
87 :     end
88 :    
89 :     and ppNext (out, NONE) = ()
90 :     | ppNext (out, SOME stm) = ppStmt (out, stm)
91 :    
92 :     fun ppMethod (out, IL.Method{name, stateIn, stateOut, body}) = let
93 :     val out1 = incIndent out
94 :     fun prVars xs = List.app (fn x => prl(out, [" ", Var.toString x])) xs
95 :     in
96 :     indent out; prl(out, ["method ", Atom.toString name, "\n"]);
97 :     indent out1; pr(out1, "state in: "); prVars stateIn; pr(out1, "\n");
98 :     ppStmt (incIndent out1, body);
99 :     indent out1; pr(out1, "state out:"); prVars stateOut; pr(out1, "\n");
100 :     indent out; prl(out, ["end ", Atom.toString name, "\n"])
101 :     end
102 :    
103 :     and ppActor (out, IL.Actor{name, params, state, stateInit, methods}) = let
104 :     val out1 = incIndent out
105 :     fun prVars xs = List.app (fn x => prl(out, [" ", Var.toString x])) xs
106 :     in
107 :     indent out;
108 :     prl(out, [
109 :     "actor ", Atom.toString name, " (",
110 :     String.concatWith "," (List.map Var.toString params), ")\n"
111 :     ]);
112 :     indent out1; pr(out1, "state: "); prVars state; pr(out1, "\n");
113 :     ppStmt (incIndent out1, stateInit);
114 :     List.app (fn m => ppMethod(out1, m)) methods;
115 :     indent out; prl(out, ["end ", Atom.toString name, "\n"])
116 :     end
117 :    
118 : jhr 340 fun output (outS, msg, IL.Program{globals, globalInit, actors}) = let
119 : jhr 256 val out = (outS, 0)
120 :     val out1 = incIndent out
121 :     in
122 : jhr 340 pr (out, concat["##### ", msg, " ####\n"]);
123 : jhr 256 pr (out, "## globals\n");
124 :     List.app (fn x => (indent out1; prl(out1, ["global ", Var.toString x, "\n"]))) globals;
125 :     pr (out, "## global initialization\n");
126 :     ppStmt (out1, globalInit);
127 :     pr (out, "## actors\n");
128 :     List.app (fn actor => ppActor(out1, actor)) actors;
129 : jhr 340 pr (out, "#### end program ####\n")
130 : jhr 256 end
131 :    
132 : jhr 198 end

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