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 5286 - (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 : jhr 4204 val outputCFG : TextIO.outstream * IR.cfg -> unit
18 :    
19 : jhr 3470 end = struct
20 :    
21 : jhr 3475 structure Op = IR.Op
22 :     structure Var = IR.Var
23 :     structure GVar = IR.GlobalVar
24 :     structure Ty = IR.Ty
25 : jhr 3470
26 :     local
27 : jhr 3475 val {getFn, setFn} = IR.Node.newFlag()
28 : jhr 3470 in
29 :     val isMarked = getFn
30 :     fun mark nd = setFn(nd, true)
31 :     fun clear nd = setFn(nd, false)
32 :     end
33 :    
34 :     (* if true, the output is in compact form *)
35 : jhr 4101 val compact = Ctl.compactCFG
36 : jhr 3470
37 :     fun indent (outS, i) = TextIO.output(outS, StringCvt.padLeft #" " i "")
38 :     fun incIndent (outS, i) = (outS, i+2)
39 :     fun pr ((outS, _), s) = TextIO.output(outS, s)
40 :     fun prl (out, l) = pr(out, concat l)
41 :     fun prln (out, l) = (indent out; prl(out, l))
42 :    
43 :     fun typedVar x = String.concat [
44 :     Ty.toString(Var.ty x), " ", Var.toString x, "#", Int.toString(Var.useCount x)
45 :     ]
46 :    
47 : jhr 4362 fun rhsToStringList rhs = (case rhs
48 : jhr 4378 of IR.GLOBAL x => [GVar.toString x, ";"]
49 :     | IR.STATE(NONE, x) => ["self.", IR.StateVar.toString x, ";"]
50 :     | IR.STATE(SOME x, y) => [IR.Var.toString x, ".", IR.StateVar.toString y, ";"]
51 :     | 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 :     Op.toString rator, "(",
56 :     String.concatWithMap "," Var.toString args, ");"
57 :     ]
58 :     | IR.CONS(xs, ty) => [
59 :     "<", Ty.toString ty, ">[",
60 :     String.concatWithMap "," Var.toString xs, "];"
61 :     ]
62 :     | IR.SEQ(xs, ty) => [
63 :     "<", Ty.toString ty, ">{",
64 :     String.concatWithMap "," Var.toString xs, "};"
65 :     ]
66 :     | IR.EINAPP(ein, args) => [
67 :     EinPP.toString ein, " (",
68 :     String.concatWithMap "," Var.toString args, ");"
69 :     ]
70 :     | IR.APPLY(f, args) => [
71 :     IR.Func.toString f, " (",
72 :     String.concatWithMap "," Var.toString args, ");"
73 :     ]
74 :     | IR.MAPREDUCE[(r, f, args)] => [
75 :     Reductions.toString r, "(MAP ", IR.Func.toString f, " (",
76 :     String.concatWithMap "," Var.toString args, "));"
77 :     ]
78 :     | IR.MAPREDUCE mrs => let
79 :     fun toS (r, f, args) = String.concat [
80 :     Reductions.toString r, "(MAP ", IR.Func.toString f, " (",
81 :     String.concatWithMap "," Var.toString args, "))"
82 :     ]
83 :     in
84 :     ["{ ", String.concatWithMap " | " toS mrs, " };"]
85 :     end
86 :     (* end case *))
87 : jhr 3470
88 : jhr 4362 fun assignToString (y, rhs) = String.concat(typedVar y :: " = " :: rhsToStringList rhs)
89 : jhr 3470
90 : jhr 4362 fun massignToString ([], rhs) = String.concat(rhsToStringList rhs)
91 :     | massignToString (ys, rhs) = String.concat(
92 :     "(" :: String.concatWithMap "," typedVar ys :: ") = " :: rhsToStringList rhs)
93 :    
94 : jhr 3475 fun labelOf (IR.ND{id, ...}) = "L"^Stamp.toString id
95 : jhr 3470
96 : jhr 3475 fun ppCFG (out, cfg as IR.CFG{entry, exit}) = let
97 :     fun goto (out, nd) = (case IR.Node.kind nd
98 :     of IR.JOIN _ => (
99 :     prln(incIndent out, ["goto ", IR.Node.toString nd, "\n"]);
100 : jhr 3470 ppNd (out, false, nd))
101 :     | _ => ppNd (out, true, nd)
102 :     (* end case *))
103 :     and ppNd (out, noLabel, nd) = let
104 :     val out1 = incIndent out
105 : jhr 3473 fun prPhi out (y, xs) =
106 :     prln (out, [
107 : jhr 3470 typedVar y, " = phi(",
108 :     String.concatWith "," (List.mapPartial (Option.map Var.toString) xs), ")\n"
109 :     ])
110 : jhr 4339 fun prJoin (nd, preds, mask) = let
111 :     val preds = ListPair.mapEq
112 :     (fn (false, nd) => IR.Node.toString nd
113 :     | (true, nd) => "*" ^ IR.Node.toString nd)
114 :     (mask, preds)
115 : jhr 4333 (* +DEBUG*)
116 :     handle ex => (
117 :     print(concat["**** Broken CFG at ", IR.Node.toString nd, "\n"]);
118 :     List.map IR.Node.toString preds)
119 :     (* -DEBUG*)
120 : jhr 4339 in
121 :     prln (out, [
122 :     IR.Node.toString nd, ": preds = [",
123 :     String.concatWith "," preds, "]\n"
124 :     ])
125 :     end
126 :     val kind = IR.Node.kind nd
127 :     val noLabel = (case kind of IR.FOREACH _ => false | _ => noLabel)
128 : jhr 3470 in
129 :     if isMarked nd
130 :     then ()
131 :     else (
132 :     mark nd;
133 :     if noLabel andalso (! compact)
134 :     then ()
135 : jhr 4333 else (case kind
136 :     of IR.JOIN{preds, mask, ...} => prJoin (nd, !preds, !mask)
137 : jhr 4339 | IR.FOREACH{pred, bodyExit, mask, ...} =>
138 :     prJoin (nd, [!pred, !bodyExit], !mask)
139 :     | _ => prln (out, [
140 : jhr 3475 IR.Node.toString nd, ": preds = [",
141 : jhr 4339 String.concatWithMap "," IR.Node.toString (IR.Node.preds nd),
142 : jhr 3470 "]\n"
143 :     ])
144 :     (* end case *));
145 : jhr 4333 case kind
146 : jhr 3475 of IR.NULL => ()
147 :     | IR.ENTRY{succ} => goto (out, !succ)
148 :     | IR.JOIN{phis, succ, ...} => (
149 : jhr 3473 List.app (prPhi out1) (!phis);
150 : jhr 3470 goto (out, !succ))
151 : jhr 3475 | IR.COND{cond, trueBranch, falseBranch, ...} => (
152 : jhr 3470 prln (out1, [
153 : jhr 3536 "if ", Var.toString (!cond),
154 : jhr 3475 " then goto ", IR.Node.toString(!trueBranch),
155 :     " else goto ", IR.Node.toString(!falseBranch), "\n"
156 : jhr 3470 ]);
157 :     ppNd (out, false, !trueBranch);
158 :     ppNd (out, false, !falseBranch))
159 : jhr 4317 | IR.FOREACH{phis, var, src, bodyEntry, succ, ...} => (
160 :     prln (out1, [
161 :     "foreach (", Var.toString var, " in ", Var.toString (!src), ")",
162 :     " on exit goto ", IR.Node.toString(!succ), "\n"
163 :     ]);
164 :     List.app (prPhi (incIndent out1)) (!phis);
165 : jhr 3506 goto (out, !bodyEntry);
166 : jhr 4317 ppNd (out, false, !succ))
167 :     | IR.NEXT{succ, ...} =>
168 :     prln (out1, ["goto ", IR.Node.toString(!succ), "\n"])
169 : jhr 3475 | IR.COM{text, succ, ...} => (
170 : jhr 3470 List.app (fn s => prln (out1, ["//", s, "\n"])) text;
171 :     goto (out, !succ))
172 : jhr 3475 | IR.ASSIGN{stm, succ, ...} => (
173 : jhr 3470 prln (out1, [assignToString stm, "\n"]);
174 :     goto (out, !succ))
175 : jhr 3475 | IR.MASSIGN{stm, succ, ...} => (
176 : jhr 3470 prln (out1, [massignToString stm, "\n"]);
177 :     goto (out, !succ))
178 : jhr 3475 | IR.GASSIGN{lhs, rhs, succ, ...} => (
179 : jhr 3470 prln (out1, [
180 :     GVar.toString lhs, " = ", Var.toString rhs, ";\n"
181 :     ]);
182 :     goto (out, !succ))
183 : jhr 3475 | IR.NEW{strand, args, succ, ...} => (
184 : jhr 3470 prln (out1, [
185 :     "new ", Atom.toString strand, "(",
186 : jhr 4339 String.concatWithMap "," Var.toString args, ");\n"
187 : jhr 3470 ]);
188 :     goto (out, !succ))
189 : jhr 3475 | IR.SAVE{lhs, rhs, succ, ...} => (
190 : jhr 3470 prln (out1, [
191 : jhr 3889 "self.", IR.StateVar.toString lhs, " = ", Var.toString rhs, ";\n"
192 : jhr 3470 ]);
193 :     goto (out, !succ))
194 : jhr 3506 | IR.EXIT{kind, succ, ...} => (
195 : jhr 4317 case kind
196 :     of ExitKind.RETURN NONE => prln (out1, ["return\n"])
197 :     | ExitKind.RETURN(SOME x) =>
198 :     prln (out1, ["return ", Var.toString x, "\n"])
199 :     | ExitKind.ACTIVE => prln (out1, ["active\n"])
200 :     | ExitKind.STABILIZE => prln (out1, ["stabilize\n"])
201 :     | ExitKind.DIE => prln (out1, ["die\n"])
202 : jhr 5286 | ExitKind.NEXTSTEP => prln (out1, ["continue\n"])
203 : jhr 4317 | ExitKind.UNREACHABLE => prln (out1, ["unreachable\n"])
204 :     (* end case *);
205 :     case !succ
206 :     of SOME nd => goto (out, nd)
207 :     | _ => ()
208 :     (* end case *))
209 : jhr 3470 (* end case *))
210 :     end
211 :     in
212 :     ppNd (out, false, entry);
213 :     (* clear marks *)
214 : jhr 3475 IR.CFG.apply clear cfg
215 : jhr 3470 end
216 :    
217 : jhr 4166 fun ppFunc out (IR.Func{name, params, body}) = (
218 : jhr 4317 prl (out, [
219 :     " function ", Ty.toString(#1(IR.Func.ty name)), " ", IR.Func.toString name,
220 :     " (", String.concatWithMap ", " typedVar params, ") {\n"
221 :     ]);
222 :     ppCFG (incIndent out, body);
223 :     prl (out, ["}\n"]))
224 : jhr 3470
225 : jhr 3485 fun ppMethod (out, name) body = let
226 : jhr 3470 val out1 = incIndent out
227 :     in
228 : jhr 4097 prln(out, ["method ", name, "\n"]);
229 : jhr 3470 ppCFG (incIndent out1, body);
230 : jhr 4097 prln(out, ["end ", name, "\n"])
231 : jhr 3470 end
232 :    
233 : jhr 4369 and ppStrand (out, strand) = let
234 :     val IR.Strand{
235 : jhr 4491 name, params, spatialDim, state, stateInit, startM, updateM, stabilizeM
236 : jhr 4369 } = strand
237 : jhr 3470 val out1 = incIndent out
238 : jhr 4097 val out2 = incIndent out1
239 : jhr 3470 fun stateVar x = let
240 : jhr 4097 val l = [Ty.toString(IR.StateVar.ty x), " ", IR.StateVar.toString x, ";\n"]
241 : jhr 3475 val l = if IR.StateVar.isOutput x then "output " :: l else l
242 : jhr 3470 in
243 : jhr 4097 prln (out2, l)
244 : jhr 3470 end
245 :     in
246 :     indent out;
247 :     prl(out, [
248 :     "strand ", Atom.toString name, " (",
249 : jhr 4369 String.concatWithMap ", " typedVar params, ")"
250 : jhr 3470 ]);
251 : jhr 4378 case spatialDim
252 : jhr 4369 of SOME d => prl(out, [" in ", Int.toString d, "D\n"])
253 : jhr 4378 | NONE => pr(out, "\n")
254 :     (* end case *);
255 : jhr 4097 indent out1; pr(out1, "state:\n"); List.app stateVar state;
256 :     prln(out1, ["state init\n"]);
257 :     ppCFG (out2, stateInit);
258 :     prln(out1, ["end state init\n"]);
259 : jhr 4491 Option.app (ppMethod (out1, "start")) startM;
260 : jhr 4317 ppMethod (out1, "update") updateM;
261 :     Option.app (ppMethod (out1, "stabilize")) stabilizeM;
262 : jhr 4097 prln(out, ["end ", Atom.toString name, "\n"])
263 : jhr 3470 end
264 :    
265 : jhr 4166 fun ppCreate (out, cr) = (
266 : jhr 4317 indent out;
267 : jhr 4491 case Create.arrayDim cr
268 : jhr 4317 of NONE => pr (out, "COLLECTION\n")
269 :     | SOME dim => prl (out, ["GRID(", Int.toString dim, ")\n"])
270 :     (* end case *);
271 :     ppCFG (incIndent out, Create.createCode cr))
272 : jhr 4166
273 : jhr 4052 fun ppGlobal outS gv = let
274 : jhr 4317 val ln = [
275 :     GlobalVarKind.toString(IR.GlobalVar.kind gv), " ", Ty.toString(GVar.ty gv),
276 :     " ", GVar.uniqueName gv, "\n"
277 :     ]
278 :     val ln = if GVar.isVarying gv then "varying " :: ln else ln
279 :     in
280 :     indent outS; prl(outS, ln)
281 :     end
282 : jhr 3470
283 : jhr 4014 fun ppInput outS (Inputs.INP{var, name, init, ...}) = prl (outS, [
284 : jhr 4317 " input ", GVar.uniqueName var, " (", name, ") = ", Inputs.initToString init, "\n"
285 :     ])
286 : jhr 3506
287 : jhr 3485 fun output (outS, msg, prog) = let
288 : jhr 4317 val IR.Program{
289 :     props, consts, inputs, globals, funcs,
290 : jhr 4491 constInit, globInit, strand, create, start, update
291 : jhr 4317 } = prog
292 : jhr 3470 val out = (outS, 0)
293 :     val out1 = incIndent out
294 :     in
295 : jhr 3508 pr (out, concat["##### ", IR.irName, ": ", msg, " ####\n"]);
296 : jhr 3470 pr (out, "## properties\n");
297 :     prln (out1, [
298 : jhr 4317 String.concatWithMap " " Properties.toString props, "\n"
299 :     ]);
300 : jhr 3470 pr (out, "## globals\n");
301 : jhr 3527 List.app (ppGlobal out1) consts;
302 : jhr 3506 List.app (ppInput out1) inputs;
303 : jhr 3527 List.app (ppGlobal out1) globals;
304 : jhr 4317 pr (out, "## functions\n");
305 :     List.app (ppFunc out1) funcs;
306 : jhr 3470 pr (out, "## input initialization\n");
307 : jhr 3485 ppCFG (out1, constInit);
308 : jhr 3995 pr (out, "## global-variable initialization\n");
309 :     ppCFG (out1, globInit);
310 : jhr 3485 pr (out, "## strand\n");
311 :     ppStrand (out1, strand);
312 :     pr (out, "## initial strand creation\n");
313 :     ppCreate (out1, create);
314 : jhr 4491 case start
315 : jhr 4317 of SOME cfg => (
316 : jhr 4491 pr (out, "## global start\n");
317 : jhr 4317 ppCFG (out1, cfg))
318 :     | NONE => ()
319 :     (* end case *);
320 :     case update
321 :     of SOME cfg => (
322 :     pr (out, "## global update\n");
323 :     ppCFG (out1, cfg))
324 :     | NONE => ()
325 :     (* end case *);
326 : jhr 3470 pr (out, "#### end program ####\n")
327 :     end
328 :    
329 : jhr 4204 fun outputCFG (outS, cfg) = ppCFG ((outS, 0), cfg)
330 :    
331 : jhr 3470 end

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