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

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