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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2635 - (view) (download)

1 : jhr 198 (* ssa-pp-fn.sml
2 :     *
3 : jhr 435 * COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu)
4 : jhr 198 * 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 : jhr 1116 local
22 :     val {getFn, setFn} = IL.Node.newFlag()
23 :     in
24 :     val isMarked = getFn
25 :     fun mark nd = setFn(nd, true)
26 :     fun clear nd = setFn(nd, false)
27 :     end
28 :    
29 :     (* if true, the output is in compact form *)
30 :     val compact = ref true
31 :    
32 : jhr 256 fun indent (outS, i) = TextIO.output(outS, StringCvt.padLeft #" " i "")
33 :     fun incIndent (outS, i) = (outS, i+2)
34 :     fun pr ((outS, _), s) = TextIO.output(outS, s)
35 :     fun prl (out, l) = pr(out, concat l)
36 : jhr 1116 fun prln (out, l) = (indent out; prl(out, l))
37 : jhr 256
38 : jhr 511 fun typedVar x = String.concat [
39 : jhr 2332 Ty.toString(Var.ty x), " ", Var.toString x, "#", Int.toString(Var.useCount x)
40 :     ]
41 : jhr 398
42 : jhr 350 fun assignToString (y, rhs) = let
43 : jhr 2332 val rhs = (case rhs
44 :     of IL.STATE x => [IL.StateVar.toString x, ";"]
45 : jhr 1640 | IL.VAR x => [Var.toString x, ";"]
46 : jhr 2332 | IL.LIT lit => [Literal.toString lit, ";"]
47 :     | IL.OP(rator, []) => [Op.toString rator, ";"]
48 :     | IL.OP(rator, args) => [
49 :     Op.toString rator, "(",
50 :     String.concatWith "," (List.map Var.toString args), ");"
51 :     ]
52 :     | IL.APPLY(f, args) => [
53 :     MathFuns.toString f, "(",
54 :     String.concatWith "," (List.map Var.toString args), ");"
55 :     ]
56 :     | IL.CONS(ty, xs) => [
57 :     "<", Ty.toString ty, ">[",
58 :     String.concatWith "," (List.map Var.toString xs), "];"
59 :     ]
60 :     (* end case *))
61 :     in
62 :     String.concat(typedVar y :: " = " :: rhs)
63 :     end
64 : jhr 256
65 : jhr 1640 fun massignToString ([], rator, xs) = String.concat [
66 :     Op.toString rator, "(", String.concatWith "," (List.map Var.toString xs), ");"
67 :     ]
68 :     | massignToString (ys, rator, xs) = String.concat [
69 :     "(", String.concatWith "," (List.map typedVar ys), ") = ",
70 :     Op.toString rator, "(", String.concatWith "," (List.map Var.toString xs), ");"
71 :     ]
72 :    
73 : jhr 1116 fun labelOf (IL.ND{id, ...}) = "L"^Stamp.toString id
74 : jhr 256
75 : jhr 1116 fun ppCFG (out, cfg as IL.CFG{entry, exit}) = let
76 : jhr 2332 fun goto (out, nd) = if (List.length(IL.Node.preds nd) > 1)
77 :     then (
78 :     prln(incIndent out, ["goto ", IL.Node.toString nd, "\n"]);
79 :     ppNd (out, false, nd))
80 :     else ppNd (out, true, nd)
81 :     and ppNd (out, noLabel, nd) = let
82 :     val out1 = incIndent out
83 :     fun prPhi (y, xs) =
84 :     prln (out1, [
85 :     typedVar y, " = phi(",
86 :     String.concatWith "," (List.map Var.toString xs), ")\n"
87 :     ])
88 :     in
89 :     if isMarked nd
90 :     then ()
91 :     else (
92 :     mark nd;
93 :     if noLabel andalso (! compact)
94 :     then ()
95 :     else prln (out, [
96 :     IL.Node.toString nd, ": preds = [",
97 :     String.concatWith "," (List.map IL.Node.toString (IL.Node.preds nd)),
98 :     "]\n"
99 :     ]);
100 :     case IL.Node.kind nd
101 :     of IL.NULL => ()
102 :     | IL.ENTRY{succ} => goto (out, !succ)
103 :     | IL.JOIN{phis, succ, ...} => (
104 :     List.app prPhi (!phis);
105 :     goto (out, !succ))
106 :     | IL.COND{cond, trueBranch, falseBranch, ...} => (
107 :     prln (out1, [
108 :     "if ", Var.toString cond,
109 :     " then goto ", IL.Node.toString(!trueBranch),
110 :     " else goto ", IL.Node.toString(!falseBranch), "\n"
111 :     ]);
112 :     ppNd (out, false, !trueBranch);
113 :     ppNd (out, false, !falseBranch))
114 :     | IL.COM{text, succ, ...} => (
115 :     List.app (fn s => prln (out1, ["//", s, "\n"])) text;
116 :     goto (out, !succ))
117 :     | IL.ASSIGN{stm, succ, ...} => (
118 :     prln (out1, [assignToString stm, "\n"]);
119 :     goto (out, !succ))
120 :     | IL.MASSIGN{stm, succ, ...} => (
121 :     prln (out1, [massignToString stm, "\n"]);
122 :     goto (out, !succ))
123 :     | IL.NEW{strand, args, succ, ...} => (
124 :     prln (out1, [
125 :     "new ", Atom.toString strand, "(",
126 :     String.concatWith "," (List.map Var.toString args), ");\n"
127 :     ]);
128 :     goto (out, !succ))
129 :     | IL.SAVE{lhs, rhs, succ, ...} => (
130 :     prln (out1, [
131 :     IL.StateVar.toString lhs, " = ", Var.toString rhs, ";\n"
132 :     ]);
133 :     goto (out, !succ))
134 :     | IL.EXIT{kind, live, ...} => let
135 :     val live = (case live
136 :     of [] => "()"
137 :     | [x] => String.concat["(", Var.toString x, ")"]
138 :     | xs => String.concat[
139 :     "(",
140 :     String.concatWith "," (List.map Var.toString xs),
141 :     ")"
142 :     ]
143 :     (* end case *))
144 :     in
145 :     case kind
146 :     of ExitKind.FRAGMENT =>
147 :     prln (out1, ["live vars = ", live, "\n"])
148 :     | ExitKind.SINIT =>
149 :     prln (out1, ["strand_init ", live, "\n"])
150 :     | ExitKind.RETURN =>
151 :     prln (out1, ["return ", live, "\n"])
152 :     | ExitKind.ACTIVE =>
153 :     prln (out1, ["active ", live, "\n"])
154 :     | ExitKind.STABILIZE =>
155 :     prln (out1, ["stabilize ", live, "\n"])
156 :     | ExitKind.DIE =>
157 :     prln (out1, ["die ", live, "\n"])
158 :     (* end case *)
159 :     end
160 :     (* end case *))
161 :     end
162 :     in
163 :     ppNd (out, false, entry);
164 :     (* clear marks *)
165 :     IL.CFG.apply clear cfg
166 :     end
167 : jhr 1116
168 :     fun ppInitially (out, IL.Initially{isArray, rangeInit, iters, create}) = let
169 : jhr 2332 val (initArg, strand, args) = create
170 :     fun ppIters (out, []) = (
171 :     ppCFG (out, initArg);
172 :     prln (out, [
173 :     "new ", Atom.toString strand, "(",
174 :     String.concatWith "," (List.map Var.toString args), ");\n"
175 :     ]))
176 :     | ppIters (out, (param, lo, hi)::iters) = (
177 :     indent out; prl(out, [
178 :     "for ", typedVar param, " = ", Var.toString lo, " .. ", Var.toString hi, "\n"
179 :     ]);
180 :     ppIters (incIndent out, iters))
181 :     in
182 :     indent out; prl(out, if isArray then ["ARRAY\n"] else ["COLLECTION\n"]);
183 :     ppCFG (incIndent out, rangeInit);
184 :     ppIters (incIndent out, iters)
185 :     end
186 : jhr 256
187 : jhr 1640 fun ppMethod (out, IL.Method{name, body}) = let
188 : jhr 2332 val out1 = incIndent out
189 :     fun prVars xs = List.app (fn x => prl(out, [" ", typedVar x, ";"])) xs
190 :     in
191 :     indent out; prl(out, ["method ", StrandUtil.nameToString name, "\n"]);
192 :     ppCFG (incIndent out1, body);
193 :     indent out; prl(out, ["end ", StrandUtil.nameToString name, "\n"])
194 :     end
195 : jhr 256
196 : jhr 511 and ppStrand (out, IL.Strand{name, params, state, stateInit, methods}) = let
197 : jhr 2332 val out1 = incIndent out
198 : jhr 1640 fun stateVar x = let
199 :     val l = [Ty.toString(IL.StateVar.ty x), " ", IL.StateVar.toString x, ";"]
200 :     val l = if IL.StateVar.isOutput x then "output " :: l else l
201 :     in
202 :     prl (out1, l)
203 :     end
204 : jhr 2332 in
205 :     indent out;
206 :     prl(out, [
207 :     "strand ", Atom.toString name, " (",
208 :     String.concatWith ", " (List.map typedVar params), ")\n"
209 :     ]);
210 :     indent out1; pr(out1, "state: "); List.app stateVar state; pr(out1, "\n");
211 :     ppCFG (incIndent out1, stateInit);
212 :     List.app (fn m => ppMethod(out1, m)) methods;
213 :     indent out; prl(out, ["end ", Atom.toString name, "\n"])
214 :     end
215 : jhr 256
216 : jhr 1640 fun output (outS, msg, IL.Program{props, globalInit, initially, strands}) = let
217 : jhr 2332 val out = (outS, 0)
218 :     val out1 = incIndent out
219 :     in
220 :     pr (out, concat["##### ", msg, " ####\n"]);
221 : jhr 2635 pr (out, "## properties\n");
222 :     prln (out1, [StrandUtil.propsToString props, "\n"]);
223 : jhr 2332 pr (out, "## globals\n");
224 :     List.app
225 :     (fn x => (indent out1; prl(out1, ["global ", typedVar x, "\n"])))
226 :     (IL.CFG.liveAtExit globalInit);
227 :     pr (out, "## global initialization\n");
228 :     ppCFG (out1, globalInit);
229 :     pr (out, "## initially\n");
230 :     ppInitially (out1, initially);
231 :     pr (out, "## strands\n");
232 :     List.app (fn strand => ppStrand(out1, strand)) strands;
233 :     pr (out, "#### end program ####\n")
234 :     end
235 : jhr 256
236 : jhr 198 end

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