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 3485 - (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 : jhr 3475 functor SSAPPFn (IR : SSA) : sig
12 : jhr 3470
13 : jhr 3475 val assignToString : IR.assign -> string
14 : jhr 3470
15 : jhr 3475 val output : TextIO.outstream * string * IR.program -> unit
16 : jhr 3470
17 :     end = struct
18 :    
19 : jhr 3475 structure Op = IR.Op
20 :     structure Var = IR.Var
21 :     structure GVar = IR.GlobalVar
22 :     structure Ty = IR.Ty
23 : jhr 3470
24 :     local
25 : jhr 3475 val {getFn, setFn} = IR.Node.newFlag()
26 : jhr 3470 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 : jhr 3475 of IR.GLOBAL x => [GVar.toString x, ";"]
48 :     | IR.STATE x => [IR.StateVar.toString x, ";"]
49 :     | IR.VAR x => [Var.toString x, ";"]
50 :     | IR.LIT lit => [Literal.toString lit, ";"]
51 :     | IR.OP(rator, []) => [Op.toString rator, ";"]
52 :     | IR.OP(rator, args) => [
53 : jhr 3470 Op.toString rator, "(",
54 :     String.concatWith "," (List.map Var.toString args), ");"
55 :     ]
56 : jhr 3475 | IR.CONS(xs, ty) => [
57 : jhr 3470 "<", Ty.toString ty, ">[",
58 :     String.concatWith "," (List.map Var.toString xs), "];"
59 :     ]
60 : jhr 3475 | IR.SEQ(xs, ty) => [
61 : jhr 3470 "<", Ty.toString ty, ">{",
62 :     String.concatWith "," (List.map Var.toString xs), "};"
63 :     ]
64 : jhr 3475 | IR.EINAPP(ein, args) => [
65 : jhr 3470 (* 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 : jhr 3475 fun labelOf (IR.ND{id, ...}) = "L"^Stamp.toString id
83 : jhr 3470
84 : jhr 3475 fun ppCFG (out, cfg as IR.CFG{entry, exit}) = let
85 :     fun goto (out, nd) = (case IR.Node.kind nd
86 :     of IR.JOIN _ => (
87 :     prln(incIndent out, ["goto ", IR.Node.toString nd, "\n"]);
88 : jhr 3470 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 : jhr 3475 else (case IR.Node.kind nd
106 :     of IR.JOIN{preds, mask, ...} => let
107 : jhr 3470 val preds = ListPair.mapEq
108 : jhr 3475 (fn (false, nd) => IR.Node.toString nd
109 :     | (true, nd) => "*" ^ IR.Node.toString nd)
110 : jhr 3470 (!mask, !preds)
111 :     (* +DEBUG*)
112 :     handle ex => (
113 : jhr 3475 print(concat["**** Broken CFG at ", IR.Node.toString nd, "\n"]);
114 :     List.map IR.Node.toString (!preds))
115 : jhr 3470 (* -DEBUG*)
116 :     in
117 :     prln (out, [
118 : jhr 3475 IR.Node.toString nd, ": preds = [",
119 : jhr 3470 String.concatWith "," preds, "]\n"
120 :     ])
121 :     end
122 :     | _ => prln (out, [
123 : jhr 3475 IR.Node.toString nd, ": preds = [",
124 :     String.concatWith "," (List.map IR.Node.toString (IR.Node.preds nd)),
125 : jhr 3470 "]\n"
126 :     ])
127 :     (* end case *));
128 : jhr 3475 case IR.Node.kind nd
129 :     of IR.NULL => ()
130 :     | IR.ENTRY{succ} => goto (out, !succ)
131 :     | IR.JOIN{phis, succ, ...} => (
132 : jhr 3473 List.app (prPhi out1) (!phis);
133 : jhr 3470 goto (out, !succ))
134 : jhr 3475 | IR.COND{cond, trueBranch, falseBranch, ...} => (
135 : jhr 3470 prln (out1, [
136 :     "if ", Var.toString cond,
137 : jhr 3475 " then goto ", IR.Node.toString(!trueBranch),
138 :     " else goto ", IR.Node.toString(!falseBranch), "\n"
139 : jhr 3470 ]);
140 :     ppNd (out, false, !trueBranch);
141 :     ppNd (out, false, !falseBranch))
142 : jhr 3475 | IR.FOREACH{phis, var, src, body, succ, ...} => (
143 : jhr 3473 prln (out1, [
144 :     "foreach (", Var.toString var, " in ", Var.toString src, ")",
145 : jhr 3475 " on exit goto ", IR.Node.toString(!succ), "\n"
146 : jhr 3473 ]);
147 :     List.app (prPhi (incIndent out1)) (!phis);
148 :     goto (out, !succ))
149 : jhr 3475 | IR.COM{text, succ, ...} => (
150 : jhr 3470 List.app (fn s => prln (out1, ["//", s, "\n"])) text;
151 :     goto (out, !succ))
152 : jhr 3475 | IR.ASSIGN{stm, succ, ...} => (
153 : jhr 3470 prln (out1, [assignToString stm, "\n"]);
154 :     goto (out, !succ))
155 : jhr 3475 | IR.MASSIGN{stm, succ, ...} => (
156 : jhr 3470 prln (out1, [massignToString stm, "\n"]);
157 :     goto (out, !succ))
158 : jhr 3475 | IR.GASSIGN{lhs, rhs, succ, ...} => (
159 : jhr 3470 prln (out1, [
160 :     GVar.toString lhs, " = ", Var.toString rhs, ";\n"
161 :     ]);
162 :     goto (out, !succ))
163 : jhr 3475 | IR.NEW{strand, args, succ, ...} => (
164 : jhr 3470 prln (out1, [
165 :     "new ", Atom.toString strand, "(",
166 :     String.concatWith "," (List.map Var.toString args), ");\n"
167 :     ]);
168 :     goto (out, !succ))
169 : jhr 3475 | IR.SAVE{lhs, rhs, succ, ...} => (
170 : jhr 3470 prln (out1, [
171 : jhr 3475 IR.StateVar.toString lhs, " = ", Var.toString rhs, ";\n"
172 : jhr 3470 ]);
173 :     goto (out, !succ))
174 : jhr 3475 | IR.EXIT{kind, live, succ, ...} => let
175 : jhr 3470 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.RETURN =>
189 :     prln (out1, ["return ", live, "\n"])
190 :     | ExitKind.ACTIVE =>
191 :     prln (out1, ["active ", live, "\n"])
192 :     | ExitKind.STABILIZE =>
193 :     prln (out1, ["stabilize ", live, "\n"])
194 :     | ExitKind.DIE =>
195 :     prln (out1, ["die ", live, "\n"])
196 :     | ExitKind.UNREACHABLE =>
197 :     prln (out1, ["unreachable\n"])
198 :     (* end case *);
199 :     case !succ
200 :     of SOME nd => goto (out, nd)
201 :     | _ => ()
202 :     (* end case *)
203 :     end
204 :     (* end case *))
205 :     end
206 :     in
207 :     ppNd (out, false, entry);
208 :     (* clear marks *)
209 : jhr 3475 IR.CFG.apply clear cfg
210 : jhr 3470 end
211 :    
212 : jhr 3485 fun ppCreate (out, IR.Create{dim, code}) = (
213 :     indent out;
214 :     case dim
215 :     of NONE => pr (out, "COLLECTION\n")
216 :     | SOME dim => prl (out, ["GRID(", Int.toString dim, ")\n"])
217 :     (* end case *);
218 :     ppCFG (incIndent out, code))
219 : jhr 3470
220 : jhr 3485 fun ppMethod (out, name) body = let
221 : jhr 3470 val out1 = incIndent out
222 :     in
223 : jhr 3485 indent out; prl(out, ["method ", name, "\n"]);
224 : jhr 3470 ppCFG (incIndent out1, body);
225 : jhr 3485 indent out; prl(out, ["end ", name, "\n"])
226 : jhr 3470 end
227 :    
228 : jhr 3485 and ppStrand (out, IR.Strand{name, params, state, stateInit, initM, updateM, stabilizeM}) = let
229 : jhr 3470 val out1 = incIndent out
230 :     fun stateVar x = let
231 : jhr 3475 val l = [Ty.toString(IR.StateVar.ty x), " ", IR.StateVar.toString x, ";"]
232 :     val l = if IR.StateVar.isOutput x then "output " :: l else l
233 : jhr 3470 in
234 :     prl (out1, l)
235 :     end
236 :     in
237 :     indent out;
238 :     prl(out, [
239 :     "strand ", Atom.toString name, " (",
240 : jhr 3485 String.concatWithMap ", " typedVar params, ")\n"
241 : jhr 3470 ]);
242 :     indent out1; pr(out1, "state: "); List.app stateVar state; pr(out1, "\n");
243 :     ppCFG (incIndent out1, stateInit);
244 : jhr 3485 Option.app (ppMethod (out1, "initially")) initM;
245 :     ppMethod (out1, "update") updateM;
246 :     Option.app (ppMethod (out1, "stabilize")) stabilizeM;
247 : jhr 3470 indent out; prl(out, ["end ", Atom.toString name, "\n"])
248 :     end
249 :    
250 : jhr 3485 fun ppGlobal prefix outS gv = (
251 :     indent outS;
252 :     prl(outS, [
253 :     prefix, " ", Ty.toString(GVar.ty gv), " ", GVar.uniqueName gv, "\n"
254 :     ]))
255 : jhr 3470
256 : jhr 3485 fun output (outS, msg, prog) = let
257 :     val IR.Program{
258 :     props, consts, inputs, globals,
259 :     constInit, globalInit, strand, create, update
260 :     } = prog
261 : jhr 3470 val out = (outS, 0)
262 :     val out1 = incIndent out
263 :     in
264 :     pr (out, concat["##### ", msg, " ####\n"]);
265 :     pr (out, "## properties\n");
266 :     prln (out1, [
267 :     String.concatWithMap " " Properties.toString props, "\n"
268 :     ]);
269 :     pr (out, "## globals\n");
270 : jhr 3485 List.app (ppGlobal "const" out1) consts;
271 :     List.app (ppGlobal "input" out1) inputs;
272 :     List.app (ppGlobal "global" out1) globals;
273 : jhr 3470 pr (out, "## input initialization\n");
274 : jhr 3485 ppCFG (out1, constInit);
275 : jhr 3470 pr (out, "## global initialization\n");
276 :     ppCFG (out1, globalInit);
277 : jhr 3485 pr (out, "## strand\n");
278 :     ppStrand (out1, strand);
279 :     pr (out, "## initial strand creation\n");
280 :     ppCreate (out1, create);
281 :     case update
282 :     of SOME cfg => (
283 :     pr (out, "## global update\n");
284 :     ppCFG (out1, cfg))
285 :     | NONE => ()
286 :     (* end case *);
287 : jhr 3470 pr (out, "#### end program ####\n")
288 :     end
289 :    
290 :     end

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