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 3762 - (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 :     | IR.STATE x => [IR.StateVar.toString x, ";"]
50 :     | IR.VAR x => [Var.toString x, ";"]
51 :     | IR.LIT lit => [Literal.toString lit, ";"]
52 :     | IR.OP(rator, []) => [Op.toString rator, ";"]
53 :     | IR.OP(rator, args) => [
54 : jhr 3470 Op.toString rator, "(",
55 : jhr 3512 String.concatWithMap "," Var.toString args, ");"
56 : jhr 3470 ]
57 : jhr 3475 | IR.CONS(xs, ty) => [
58 : jhr 3470 "<", Ty.toString ty, ">[",
59 : jhr 3512 String.concatWithMap "," Var.toString xs, "];"
60 : jhr 3470 ]
61 : jhr 3475 | IR.SEQ(xs, ty) => [
62 : jhr 3470 "<", Ty.toString ty, ">{",
63 : jhr 3512 String.concatWithMap "," Var.toString xs, "};"
64 : jhr 3470 ]
65 : jhr 3475 | IR.EINAPP(ein, args) => [
66 : jhr 3512 EinPP.toString ein, " (",
67 :     String.concatWithMap "," Var.toString args, ");"
68 : jhr 3470 ]
69 :     (* end case *))
70 :     in
71 :     String.concat(typedVar y :: " = " :: rhs)
72 :     end
73 :    
74 :     fun massignToString ([], rator, xs) = String.concat [
75 : jhr 3512 Op.toString rator, "(", String.concatWithMap "," Var.toString xs, ");"
76 : jhr 3470 ]
77 :     | massignToString (ys, rator, xs) = String.concat [
78 :     "(", String.concatWith "," (List.map typedVar ys), ") = ",
79 : jhr 3512 Op.toString rator, "(", String.concatWithMap "," Var.toString xs, ");"
80 : jhr 3470 ]
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 : jhr 3536 "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 3506 | IR.FOREACH{phis, var, src, bodyEntry, succ, ...} => (
143 : jhr 3473 prln (out1, [
144 : jhr 3536 "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 : jhr 3506 goto (out, !bodyEntry);
149 : jhr 3509 ppNd (out, false, !succ))
150 : jhr 3754 | IR.NEXT{succ, ...} =>
151 :     prln (out1, ["goto ", IR.Node.toString(!succ), "\n"])
152 : jhr 3475 | IR.COM{text, succ, ...} => (
153 : jhr 3470 List.app (fn s => prln (out1, ["//", s, "\n"])) text;
154 :     goto (out, !succ))
155 : jhr 3475 | IR.ASSIGN{stm, succ, ...} => (
156 : jhr 3470 prln (out1, [assignToString stm, "\n"]);
157 :     goto (out, !succ))
158 : jhr 3475 | IR.MASSIGN{stm, succ, ...} => (
159 : jhr 3470 prln (out1, [massignToString stm, "\n"]);
160 :     goto (out, !succ))
161 : jhr 3475 | IR.GASSIGN{lhs, rhs, succ, ...} => (
162 : jhr 3470 prln (out1, [
163 :     GVar.toString lhs, " = ", Var.toString rhs, ";\n"
164 :     ]);
165 :     goto (out, !succ))
166 : jhr 3475 | IR.NEW{strand, args, succ, ...} => (
167 : jhr 3470 prln (out1, [
168 :     "new ", Atom.toString strand, "(",
169 :     String.concatWith "," (List.map Var.toString args), ");\n"
170 :     ]);
171 :     goto (out, !succ))
172 : jhr 3475 | IR.SAVE{lhs, rhs, succ, ...} => (
173 : jhr 3470 prln (out1, [
174 : jhr 3475 IR.StateVar.toString lhs, " = ", Var.toString rhs, ";\n"
175 : jhr 3470 ]);
176 :     goto (out, !succ))
177 : jhr 3506 | IR.EXIT{kind, succ, ...} => (
178 :     case kind
179 :     of ExitKind.RETURN => prln (out1, ["return\n"])
180 :     | ExitKind.ACTIVE => prln (out1, ["active\n"])
181 :     | ExitKind.STABILIZE => prln (out1, ["stabilize\n"])
182 :     | ExitKind.DIE => prln (out1, ["die\n"])
183 :     | ExitKind.UNREACHABLE => prln (out1, ["unreachable\n"])
184 :     (* end case *);
185 :     case !succ
186 :     of SOME nd => goto (out, nd)
187 :     | _ => ()
188 :     (* end case *))
189 : jhr 3470 (* end case *))
190 :     end
191 :     in
192 :     ppNd (out, false, entry);
193 :     (* clear marks *)
194 : jhr 3475 IR.CFG.apply clear cfg
195 : jhr 3470 end
196 :    
197 : jhr 3485 fun ppCreate (out, IR.Create{dim, code}) = (
198 :     indent out;
199 :     case dim
200 :     of NONE => pr (out, "COLLECTION\n")
201 :     | SOME dim => prl (out, ["GRID(", Int.toString dim, ")\n"])
202 :     (* end case *);
203 :     ppCFG (incIndent out, code))
204 : jhr 3470
205 : jhr 3485 fun ppMethod (out, name) body = let
206 : jhr 3470 val out1 = incIndent out
207 :     in
208 : jhr 3485 indent out; prl(out, ["method ", name, "\n"]);
209 : jhr 3470 ppCFG (incIndent out1, body);
210 : jhr 3485 indent out; prl(out, ["end ", name, "\n"])
211 : jhr 3470 end
212 :    
213 : jhr 3485 and ppStrand (out, IR.Strand{name, params, state, stateInit, initM, updateM, stabilizeM}) = let
214 : jhr 3470 val out1 = incIndent out
215 :     fun stateVar x = let
216 : jhr 3475 val l = [Ty.toString(IR.StateVar.ty x), " ", IR.StateVar.toString x, ";"]
217 :     val l = if IR.StateVar.isOutput x then "output " :: l else l
218 : jhr 3470 in
219 :     prl (out1, l)
220 :     end
221 :     in
222 :     indent out;
223 :     prl(out, [
224 :     "strand ", Atom.toString name, " (",
225 : jhr 3485 String.concatWithMap ", " typedVar params, ")\n"
226 : jhr 3470 ]);
227 :     indent out1; pr(out1, "state: "); List.app stateVar state; pr(out1, "\n");
228 :     ppCFG (incIndent out1, stateInit);
229 : jhr 3485 Option.app (ppMethod (out1, "initially")) initM;
230 :     ppMethod (out1, "update") updateM;
231 :     Option.app (ppMethod (out1, "stabilize")) stabilizeM;
232 : jhr 3470 indent out; prl(out, ["end ", Atom.toString name, "\n"])
233 :     end
234 :    
235 : jhr 3527 fun ppGlobal outS gv = (
236 : jhr 3485 indent outS;
237 :     prl(outS, [
238 : jhr 3527 GlobalVarKind.toString(IR.GlobalVar.kind gv), " ", Ty.toString(GVar.ty gv),
239 :     " ", GVar.uniqueName gv, "\n"
240 : jhr 3485 ]))
241 : jhr 3470
242 : jhr 3506 fun ppInput outS inp = prl (outS, ["input ", Inputs.toString inp, "\n"])
243 :    
244 : jhr 3485 fun output (outS, msg, prog) = let
245 :     val IR.Program{
246 :     props, consts, inputs, globals,
247 :     constInit, globalInit, strand, create, update
248 :     } = prog
249 : jhr 3470 val out = (outS, 0)
250 :     val out1 = incIndent out
251 :     in
252 : jhr 3508 pr (out, concat["##### ", IR.irName, ": ", msg, " ####\n"]);
253 : jhr 3470 pr (out, "## properties\n");
254 :     prln (out1, [
255 :     String.concatWithMap " " Properties.toString props, "\n"
256 :     ]);
257 :     pr (out, "## globals\n");
258 : jhr 3527 List.app (ppGlobal out1) consts;
259 : jhr 3506 List.app (ppInput out1) inputs;
260 : jhr 3527 List.app (ppGlobal out1) globals;
261 : jhr 3470 pr (out, "## input initialization\n");
262 : jhr 3485 ppCFG (out1, constInit);
263 : jhr 3470 pr (out, "## global initialization\n");
264 :     ppCFG (out1, globalInit);
265 : jhr 3485 pr (out, "## strand\n");
266 :     ppStrand (out1, strand);
267 :     pr (out, "## initial strand creation\n");
268 :     ppCreate (out1, create);
269 :     case update
270 :     of SOME cfg => (
271 :     pr (out, "## global update\n");
272 :     ppCFG (out1, cfg))
273 :     | NONE => ()
274 :     (* end case *);
275 : jhr 3470 pr (out, "#### end program ####\n")
276 :     end
277 :    
278 :     end

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