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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3473 - (view) (download)

1 : jhr 3470 (* ssa-pp-fn.sml
2 :     *
3 :     * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
4 :     *
5 :     * COPYRIGHT (c) 2015 The University of Chicago
6 :     * All rights reserved.
7 :     *
8 :     * Pretty printing for SSA representations
9 :     *)
10 :    
11 :     functor SSAPPFn (IL : SSA) : sig
12 :    
13 :     val assignToString : IL.assign -> string
14 :    
15 :     val output : TextIO.outstream * string * IL.program -> unit
16 :    
17 :     end = struct
18 :    
19 :     structure Op = IL.Op
20 :     structure Var = IL.Var
21 :     structure GVar = IL.GlobalVar
22 :     structure Ty = IL.Ty
23 :    
24 :     local
25 :     val {getFn, setFn} = IL.Node.newFlag()
26 :     in
27 :     val isMarked = getFn
28 :     fun mark nd = setFn(nd, true)
29 :     fun clear nd = setFn(nd, false)
30 :     end
31 :    
32 :     (* if true, the output is in compact form *)
33 :     val compact = ref true
34 :    
35 :     fun indent (outS, i) = TextIO.output(outS, StringCvt.padLeft #" " i "")
36 :     fun incIndent (outS, i) = (outS, i+2)
37 :     fun pr ((outS, _), s) = TextIO.output(outS, s)
38 :     fun prl (out, l) = pr(out, concat l)
39 :     fun prln (out, l) = (indent out; prl(out, l))
40 :    
41 :     fun typedVar x = String.concat [
42 :     Ty.toString(Var.ty x), " ", Var.toString x, "#", Int.toString(Var.useCount x)
43 :     ]
44 :    
45 :     fun assignToString (y, rhs) = let
46 :     val rhs = (case rhs
47 :     of IL.GLOBAL x => [GVar.toString x, ";"]
48 :     | IL.STATE x => [IL.StateVar.toString x, ";"]
49 :     | IL.VAR x => [Var.toString x, ";"]
50 :     | IL.LIT lit => [Literal.toString lit, ";"]
51 :     | IL.OP(rator, []) => [Op.toString rator, ";"]
52 :     | IL.OP(rator, args) => [
53 :     Op.toString rator, "(",
54 :     String.concatWith "," (List.map Var.toString args), ");"
55 :     ]
56 :     | IL.CONS(xs, ty) => [
57 :     "<", Ty.toString ty, ">[",
58 :     String.concatWith "," (List.map Var.toString xs), "];"
59 :     ]
60 :     | IL.SEQ(xs, ty) => [
61 :     "<", Ty.toString ty, ">{",
62 :     String.concatWith "," (List.map Var.toString xs), "};"
63 :     ]
64 :     | IL.EINAPP(ein, args) => [
65 :     (* FIXME *)
66 :     "EIN", "(",
67 :     String.concatWith "," (List.map Var.toString args), ");"
68 :     ]
69 :     (* end case *))
70 :     in
71 :     String.concat(typedVar y :: " = " :: rhs)
72 :     end
73 :    
74 :     fun massignToString ([], rator, xs) = String.concat [
75 :     Op.toString rator, "(", String.concatWith "," (List.map Var.toString xs), ");"
76 :     ]
77 :     | massignToString (ys, rator, xs) = String.concat [
78 :     "(", String.concatWith "," (List.map typedVar ys), ") = ",
79 :     Op.toString rator, "(", String.concatWith "," (List.map Var.toString xs), ");"
80 :     ]
81 :    
82 :     fun labelOf (IL.ND{id, ...}) = "L"^Stamp.toString id
83 :    
84 :     fun ppCFG (out, cfg as IL.CFG{entry, exit}) = let
85 :     fun goto (out, nd) = (case IL.Node.kind nd
86 :     of IL.JOIN _ => (
87 :     prln(incIndent out, ["goto ", IL.Node.toString nd, "\n"]);
88 :     ppNd (out, false, nd))
89 :     | _ => ppNd (out, true, nd)
90 :     (* end case *))
91 :     and ppNd (out, noLabel, nd) = let
92 :     val out1 = incIndent out
93 : jhr 3473 fun prPhi out (y, xs) =
94 :     prln (out, [
95 : jhr 3470 typedVar y, " = phi(",
96 :     String.concatWith "," (List.mapPartial (Option.map Var.toString) xs), ")\n"
97 :     ])
98 :     in
99 :     if isMarked nd
100 :     then ()
101 :     else (
102 :     mark nd;
103 :     if noLabel andalso (! compact)
104 :     then ()
105 :     else (case IL.Node.kind nd
106 :     of IL.JOIN{preds, mask, ...} => let
107 :     val preds = ListPair.mapEq
108 :     (fn (false, nd) => IL.Node.toString nd
109 :     | (true, nd) => "*" ^ IL.Node.toString nd)
110 :     (!mask, !preds)
111 :     (* +DEBUG*)
112 :     handle ex => (
113 :     print(concat["**** Broken CFG at ", IL.Node.toString nd, "\n"]);
114 :     List.map IL.Node.toString (!preds))
115 :     (* -DEBUG*)
116 :     in
117 :     prln (out, [
118 :     IL.Node.toString nd, ": preds = [",
119 :     String.concatWith "," preds, "]\n"
120 :     ])
121 :     end
122 :     | _ => prln (out, [
123 :     IL.Node.toString nd, ": preds = [",
124 :     String.concatWith "," (List.map IL.Node.toString (IL.Node.preds nd)),
125 :     "]\n"
126 :     ])
127 :     (* end case *));
128 :     case IL.Node.kind nd
129 :     of IL.NULL => ()
130 :     | IL.ENTRY{succ} => goto (out, !succ)
131 :     | IL.JOIN{phis, succ, ...} => (
132 : jhr 3473 List.app (prPhi out1) (!phis);
133 : jhr 3470 goto (out, !succ))
134 :     | IL.COND{cond, trueBranch, falseBranch, ...} => (
135 :     prln (out1, [
136 :     "if ", Var.toString cond,
137 :     " then goto ", IL.Node.toString(!trueBranch),
138 :     " else goto ", IL.Node.toString(!falseBranch), "\n"
139 :     ]);
140 :     ppNd (out, false, !trueBranch);
141 :     ppNd (out, false, !falseBranch))
142 : jhr 3473 | IL.FOREACH{phis, var, src, body, succ, ...} => (
143 :     prln (out1, [
144 :     "foreach (", Var.toString var, " in ", Var.toString src, ")",
145 :     " on exit goto ", IL.Node.toString(!succ), "\n"
146 :     ]);
147 :     List.app (prPhi (incIndent out1)) (!phis);
148 :     goto (out, !succ))
149 : jhr 3470 | IL.COM{text, succ, ...} => (
150 :     List.app (fn s => prln (out1, ["//", s, "\n"])) text;
151 :     goto (out, !succ))
152 :     | IL.ASSIGN{stm, succ, ...} => (
153 :     prln (out1, [assignToString stm, "\n"]);
154 :     goto (out, !succ))
155 :     | IL.MASSIGN{stm, succ, ...} => (
156 :     prln (out1, [massignToString stm, "\n"]);
157 :     goto (out, !succ))
158 :     | IL.GASSIGN{lhs, rhs, succ, ...} => (
159 :     prln (out1, [
160 :     GVar.toString lhs, " = ", Var.toString rhs, ";\n"
161 :     ]);
162 :     goto (out, !succ))
163 :     | IL.NEW{strand, args, succ, ...} => (
164 :     prln (out1, [
165 :     "new ", Atom.toString strand, "(",
166 :     String.concatWith "," (List.map Var.toString args), ");\n"
167 :     ]);
168 :     goto (out, !succ))
169 :     | IL.SAVE{lhs, rhs, succ, ...} => (
170 :     prln (out1, [
171 :     IL.StateVar.toString lhs, " = ", Var.toString rhs, ";\n"
172 :     ]);
173 :     goto (out, !succ))
174 :     | IL.EXIT{kind, live, succ, ...} => let
175 :     val live = (case live
176 :     of [] => "()"
177 :     | [x] => String.concat["(", Var.toString x, ")"]
178 :     | xs => String.concat[
179 :     "(",
180 :     String.concatWith "," (List.map Var.toString xs),
181 :     ")"
182 :     ]
183 :     (* end case *))
184 :     in
185 :     case kind
186 :     of ExitKind.FRAGMENT =>
187 :     prln (out1, ["live vars = ", live, "\n"])
188 :     | ExitKind.SINIT =>
189 :     prln (out1, ["strand_init ", live, "\n"])
190 :     | ExitKind.RETURN =>
191 :     prln (out1, ["return ", live, "\n"])
192 :     | ExitKind.ACTIVE =>
193 :     prln (out1, ["active ", live, "\n"])
194 :     | ExitKind.STABILIZE =>
195 :     prln (out1, ["stabilize ", live, "\n"])
196 :     | ExitKind.DIE =>
197 :     prln (out1, ["die ", live, "\n"])
198 :     | ExitKind.UNREACHABLE =>
199 :     prln (out1, ["unreachable\n"])
200 :     (* end case *);
201 :     case !succ
202 :     of SOME nd => goto (out, nd)
203 :     | _ => ()
204 :     (* end case *)
205 :     end
206 :     (* end case *))
207 :     end
208 :     in
209 :     ppNd (out, false, entry);
210 :     (* clear marks *)
211 :     IL.CFG.apply clear cfg
212 :     end
213 :    
214 :     fun ppInitially (out, IL.Initially{isArray, rangeInit, iters, create}) = let
215 :     val (initArg, strand, args) = create
216 :     fun ppIters (out, []) = (
217 :     ppCFG (out, initArg);
218 :     prln (out, [
219 :     "new ", Atom.toString strand, "(",
220 :     String.concatWith "," (List.map Var.toString args), ");\n"
221 :     ]))
222 :     | ppIters (out, (param, lo, hi)::iters) = (
223 :     indent out; prl(out, [
224 :     "for ", typedVar param, " = ", Var.toString lo, " .. ", Var.toString hi, "\n"
225 :     ]);
226 :     ppIters (incIndent out, iters))
227 :     in
228 :     indent out; prl(out, if isArray then ["ARRAY\n"] else ["COLLECTION\n"]);
229 :     ppCFG (incIndent out, rangeInit);
230 :     ppIters (incIndent out, iters)
231 :     end
232 :    
233 :     fun ppMethod (out, IL.Method{name, body}) = let
234 :     val out1 = incIndent out
235 :     fun prVars xs = List.app (fn x => prl(out, [" ", typedVar x, ";"])) xs
236 :     in
237 :     indent out; prl(out, ["method ", StrandUtil.nameToString name, "\n"]);
238 :     ppCFG (incIndent out1, body);
239 :     indent out; prl(out, ["end ", StrandUtil.nameToString name, "\n"])
240 :     end
241 :    
242 :     and ppStrand (out, IL.Strand{name, params, state, stateInit, methods}) = let
243 :     val out1 = incIndent out
244 :     fun stateVar x = let
245 :     val l = [Ty.toString(IL.StateVar.ty x), " ", IL.StateVar.toString x, ";"]
246 :     val l = if IL.StateVar.isOutput x then "output " :: l else l
247 :     in
248 :     prl (out1, l)
249 :     end
250 :     in
251 :     indent out;
252 :     prl(out, [
253 :     "strand ", Atom.toString name, " (",
254 :     String.concatWith ", " (List.map typedVar params), ")\n"
255 :     ]);
256 :     indent out1; pr(out1, "state: "); List.app stateVar state; pr(out1, "\n");
257 :     ppCFG (incIndent out1, stateInit);
258 :     List.app (fn m => ppMethod(out1, m)) methods;
259 :     indent out; prl(out, ["end ", Atom.toString name, "\n"])
260 :     end
261 :    
262 :     fun ppGlobal outS gv = let
263 :     val prefix = if GVar.isInput gv then "input " else "global "
264 :     in
265 :     indent outS;
266 :     prl(outS, [
267 :     prefix, Ty.toString(GVar.ty gv), " ", GVar.uniqueName gv, "\n"
268 :     ])
269 :     end
270 :    
271 :     fun output (outS, msg, IL.Program{props, globals, inputInit, globalInit, initially, strands}) = let
272 :     val out = (outS, 0)
273 :     val out1 = incIndent out
274 :     in
275 :     pr (out, concat["##### ", msg, " ####\n"]);
276 :     pr (out, "## properties\n");
277 :     prln (out1, [
278 :     String.concatWithMap " " Properties.toString props, "\n"
279 :     ]);
280 :     pr (out, "## globals\n");
281 :     List.app (ppGlobal out1) globals;
282 :     pr (out, "## input initialization\n");
283 :     ppCFG (out1, inputInit);
284 :     pr (out, "## global initialization\n");
285 :     ppCFG (out1, globalInit);
286 :     pr (out, "## initially\n");
287 :     ppInitially (out1, initially);
288 :     pr (out, "## strands\n");
289 :     List.app (fn strand => ppStrand(out1, strand)) strands;
290 :     pr (out, "#### end program ####\n")
291 :     end
292 :    
293 :     end

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