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 3889 - (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 : jhr 3762 val compact = ref true
34 :     (* FIXME: use a control to set this flag from the command-line *)
35 : jhr 3470
36 :     fun indent (outS, i) = TextIO.output(outS, StringCvt.padLeft #" " i "")
37 :     fun incIndent (outS, i) = (outS, i+2)
38 :     fun pr ((outS, _), s) = TextIO.output(outS, s)
39 :     fun prl (out, l) = pr(out, concat l)
40 :     fun prln (out, l) = (indent out; prl(out, l))
41 :    
42 :     fun typedVar x = String.concat [
43 :     Ty.toString(Var.ty x), " ", Var.toString x, "#", Int.toString(Var.useCount x)
44 :     ]
45 :    
46 :     fun assignToString (y, rhs) = let
47 :     val rhs = (case rhs
48 : jhr 3475 of IR.GLOBAL x => [GVar.toString x, ";"]
49 : jhr 3846 | IR.STATE(NONE, x) => ["self.", IR.StateVar.toString x, ";"]
50 :     | IR.STATE(SOME x, y) => [IR.Var.toString x, ".", IR.StateVar.toString y, ";"]
51 : jhr 3475 | IR.VAR x => [Var.toString x, ";"]
52 :     | IR.LIT lit => [Literal.toString lit, ";"]
53 :     | IR.OP(rator, []) => [Op.toString rator, ";"]
54 :     | IR.OP(rator, args) => [
55 : jhr 3470 Op.toString rator, "(",
56 : jhr 3512 String.concatWithMap "," Var.toString args, ");"
57 : jhr 3470 ]
58 : jhr 3475 | IR.CONS(xs, ty) => [
59 : jhr 3470 "<", Ty.toString ty, ">[",
60 : jhr 3512 String.concatWithMap "," Var.toString xs, "];"
61 : jhr 3470 ]
62 : jhr 3475 | IR.SEQ(xs, ty) => [
63 : jhr 3470 "<", Ty.toString ty, ">{",
64 : jhr 3512 String.concatWithMap "," Var.toString xs, "};"
65 : jhr 3470 ]
66 : jhr 3475 | IR.EINAPP(ein, args) => [
67 : jhr 3512 EinPP.toString ein, " (",
68 :     String.concatWithMap "," Var.toString args, ");"
69 : jhr 3470 ]
70 :     (* end case *))
71 :     in
72 :     String.concat(typedVar y :: " = " :: rhs)
73 :     end
74 :    
75 :     fun massignToString ([], rator, xs) = String.concat [
76 : jhr 3512 Op.toString rator, "(", String.concatWithMap "," Var.toString xs, ");"
77 : jhr 3470 ]
78 :     | massignToString (ys, rator, xs) = String.concat [
79 :     "(", String.concatWith "," (List.map typedVar ys), ") = ",
80 : jhr 3512 Op.toString rator, "(", String.concatWithMap "," Var.toString xs, ");"
81 : jhr 3470 ]
82 :    
83 : jhr 3475 fun labelOf (IR.ND{id, ...}) = "L"^Stamp.toString id
84 : jhr 3470
85 : jhr 3475 fun ppCFG (out, cfg as IR.CFG{entry, exit}) = let
86 :     fun goto (out, nd) = (case IR.Node.kind nd
87 :     of IR.JOIN _ => (
88 :     prln(incIndent out, ["goto ", IR.Node.toString nd, "\n"]);
89 : jhr 3470 ppNd (out, false, nd))
90 :     | _ => ppNd (out, true, nd)
91 :     (* end case *))
92 :     and ppNd (out, noLabel, nd) = let
93 :     val out1 = incIndent out
94 : jhr 3473 fun prPhi out (y, xs) =
95 :     prln (out, [
96 : jhr 3470 typedVar y, " = phi(",
97 :     String.concatWith "," (List.mapPartial (Option.map Var.toString) xs), ")\n"
98 :     ])
99 :     in
100 :     if isMarked nd
101 :     then ()
102 :     else (
103 :     mark nd;
104 :     if noLabel andalso (! compact)
105 :     then ()
106 : jhr 3475 else (case IR.Node.kind nd
107 :     of IR.JOIN{preds, mask, ...} => let
108 : jhr 3470 val preds = ListPair.mapEq
109 : jhr 3475 (fn (false, nd) => IR.Node.toString nd
110 :     | (true, nd) => "*" ^ IR.Node.toString nd)
111 : jhr 3470 (!mask, !preds)
112 :     (* +DEBUG*)
113 :     handle ex => (
114 : jhr 3475 print(concat["**** Broken CFG at ", IR.Node.toString nd, "\n"]);
115 :     List.map IR.Node.toString (!preds))
116 : jhr 3470 (* -DEBUG*)
117 :     in
118 :     prln (out, [
119 : jhr 3475 IR.Node.toString nd, ": preds = [",
120 : jhr 3470 String.concatWith "," preds, "]\n"
121 :     ])
122 :     end
123 :     | _ => prln (out, [
124 : jhr 3475 IR.Node.toString nd, ": preds = [",
125 :     String.concatWith "," (List.map IR.Node.toString (IR.Node.preds nd)),
126 : jhr 3470 "]\n"
127 :     ])
128 :     (* end case *));
129 : jhr 3475 case IR.Node.kind nd
130 :     of IR.NULL => ()
131 :     | IR.ENTRY{succ} => goto (out, !succ)
132 :     | IR.JOIN{phis, succ, ...} => (
133 : jhr 3473 List.app (prPhi out1) (!phis);
134 : jhr 3470 goto (out, !succ))
135 : jhr 3475 | IR.COND{cond, trueBranch, falseBranch, ...} => (
136 : jhr 3470 prln (out1, [
137 : jhr 3536 "if ", Var.toString (!cond),
138 : jhr 3475 " then goto ", IR.Node.toString(!trueBranch),
139 :     " else goto ", IR.Node.toString(!falseBranch), "\n"
140 : jhr 3470 ]);
141 :     ppNd (out, false, !trueBranch);
142 :     ppNd (out, false, !falseBranch))
143 : jhr 3506 | IR.FOREACH{phis, var, src, bodyEntry, succ, ...} => (
144 : jhr 3473 prln (out1, [
145 : jhr 3536 "foreach (", Var.toString var, " in ", Var.toString (!src), ")",
146 : jhr 3475 " on exit goto ", IR.Node.toString(!succ), "\n"
147 : jhr 3473 ]);
148 :     List.app (prPhi (incIndent out1)) (!phis);
149 : jhr 3506 goto (out, !bodyEntry);
150 : jhr 3509 ppNd (out, false, !succ))
151 : jhr 3754 | IR.NEXT{succ, ...} =>
152 :     prln (out1, ["goto ", IR.Node.toString(!succ), "\n"])
153 : jhr 3475 | IR.COM{text, succ, ...} => (
154 : jhr 3470 List.app (fn s => prln (out1, ["//", s, "\n"])) text;
155 :     goto (out, !succ))
156 : jhr 3475 | IR.ASSIGN{stm, succ, ...} => (
157 : jhr 3470 prln (out1, [assignToString stm, "\n"]);
158 :     goto (out, !succ))
159 : jhr 3475 | IR.MASSIGN{stm, succ, ...} => (
160 : jhr 3470 prln (out1, [massignToString stm, "\n"]);
161 :     goto (out, !succ))
162 : jhr 3475 | IR.GASSIGN{lhs, rhs, succ, ...} => (
163 : jhr 3470 prln (out1, [
164 :     GVar.toString lhs, " = ", Var.toString rhs, ";\n"
165 :     ]);
166 :     goto (out, !succ))
167 : jhr 3475 | IR.NEW{strand, args, succ, ...} => (
168 : jhr 3470 prln (out1, [
169 :     "new ", Atom.toString strand, "(",
170 :     String.concatWith "," (List.map Var.toString args), ");\n"
171 :     ]);
172 :     goto (out, !succ))
173 : jhr 3475 | IR.SAVE{lhs, rhs, succ, ...} => (
174 : jhr 3470 prln (out1, [
175 : jhr 3889 "self.", IR.StateVar.toString lhs, " = ", Var.toString rhs, ";\n"
176 : jhr 3470 ]);
177 :     goto (out, !succ))
178 : jhr 3506 | IR.EXIT{kind, succ, ...} => (
179 :     case kind
180 :     of ExitKind.RETURN => prln (out1, ["return\n"])
181 :     | ExitKind.ACTIVE => prln (out1, ["active\n"])
182 :     | ExitKind.STABILIZE => prln (out1, ["stabilize\n"])
183 :     | ExitKind.DIE => prln (out1, ["die\n"])
184 :     | ExitKind.UNREACHABLE => prln (out1, ["unreachable\n"])
185 :     (* end case *);
186 :     case !succ
187 :     of SOME nd => goto (out, nd)
188 :     | _ => ()
189 :     (* end case *))
190 : jhr 3470 (* end case *))
191 :     end
192 :     in
193 :     ppNd (out, false, entry);
194 :     (* clear marks *)
195 : jhr 3475 IR.CFG.apply clear cfg
196 : jhr 3470 end
197 :    
198 : jhr 3485 fun ppCreate (out, IR.Create{dim, code}) = (
199 :     indent out;
200 :     case dim
201 :     of NONE => pr (out, "COLLECTION\n")
202 :     | SOME dim => prl (out, ["GRID(", Int.toString dim, ")\n"])
203 :     (* end case *);
204 :     ppCFG (incIndent out, code))
205 : jhr 3470
206 : jhr 3485 fun ppMethod (out, name) body = let
207 : jhr 3470 val out1 = incIndent out
208 :     in
209 : jhr 3485 indent out; prl(out, ["method ", name, "\n"]);
210 : jhr 3470 ppCFG (incIndent out1, body);
211 : jhr 3485 indent out; prl(out, ["end ", name, "\n"])
212 : jhr 3470 end
213 :    
214 : jhr 3485 and ppStrand (out, IR.Strand{name, params, state, stateInit, initM, updateM, stabilizeM}) = let
215 : jhr 3470 val out1 = incIndent out
216 :     fun stateVar x = let
217 : jhr 3475 val l = [Ty.toString(IR.StateVar.ty x), " ", IR.StateVar.toString x, ";"]
218 :     val l = if IR.StateVar.isOutput x then "output " :: l else l
219 : jhr 3470 in
220 :     prl (out1, l)
221 :     end
222 :     in
223 :     indent out;
224 :     prl(out, [
225 :     "strand ", Atom.toString name, " (",
226 : jhr 3485 String.concatWithMap ", " typedVar params, ")\n"
227 : jhr 3470 ]);
228 :     indent out1; pr(out1, "state: "); List.app stateVar state; pr(out1, "\n");
229 :     ppCFG (incIndent out1, stateInit);
230 : jhr 3485 Option.app (ppMethod (out1, "initially")) initM;
231 :     ppMethod (out1, "update") updateM;
232 :     Option.app (ppMethod (out1, "stabilize")) stabilizeM;
233 : jhr 3470 indent out; prl(out, ["end ", Atom.toString name, "\n"])
234 :     end
235 :    
236 : jhr 3527 fun ppGlobal outS gv = (
237 : jhr 3485 indent outS;
238 :     prl(outS, [
239 : jhr 3527 GlobalVarKind.toString(IR.GlobalVar.kind gv), " ", Ty.toString(GVar.ty gv),
240 :     " ", GVar.uniqueName gv, "\n"
241 : jhr 3485 ]))
242 : jhr 3470
243 : jhr 3506 fun ppInput outS inp = prl (outS, ["input ", Inputs.toString inp, "\n"])
244 :    
245 : jhr 3485 fun output (outS, msg, prog) = let
246 :     val IR.Program{
247 :     props, consts, inputs, globals,
248 :     constInit, globalInit, strand, create, update
249 :     } = prog
250 : jhr 3470 val out = (outS, 0)
251 :     val out1 = incIndent out
252 :     in
253 : jhr 3508 pr (out, concat["##### ", IR.irName, ": ", msg, " ####\n"]);
254 : jhr 3470 pr (out, "## properties\n");
255 :     prln (out1, [
256 :     String.concatWithMap " " Properties.toString props, "\n"
257 :     ]);
258 :     pr (out, "## globals\n");
259 : jhr 3527 List.app (ppGlobal out1) consts;
260 : jhr 3506 List.app (ppInput out1) inputs;
261 : jhr 3527 List.app (ppGlobal out1) globals;
262 : jhr 3470 pr (out, "## input initialization\n");
263 : jhr 3485 ppCFG (out1, constInit);
264 : jhr 3470 pr (out, "## global initialization\n");
265 :     ppCFG (out1, globalInit);
266 : jhr 3485 pr (out, "## strand\n");
267 :     ppStrand (out1, strand);
268 :     pr (out, "## initial strand creation\n");
269 :     ppCreate (out1, create);
270 :     case update
271 :     of SOME cfg => (
272 :     pr (out, "## global update\n");
273 :     ppCFG (out1, cfg))
274 :     | NONE => ()
275 :     (* end case *);
276 : jhr 3470 pr (out, "#### end program ####\n")
277 :     end
278 :    
279 :     end

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