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 3512 - (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 : jhr 3512 String.concatWithMap "," Var.toString args, ");"
55 : jhr 3470 ]
56 : jhr 3475 | IR.CONS(xs, ty) => [
57 : jhr 3470 "<", Ty.toString ty, ">[",
58 : jhr 3512 String.concatWithMap "," Var.toString xs, "];"
59 : jhr 3470 ]
60 : jhr 3475 | IR.SEQ(xs, ty) => [
61 : jhr 3470 "<", Ty.toString ty, ">{",
62 : jhr 3512 String.concatWithMap "," Var.toString xs, "};"
63 : jhr 3470 ]
64 : jhr 3475 | IR.EINAPP(ein, args) => [
65 : jhr 3512 EinPP.toString ein, " (",
66 :     String.concatWithMap "," Var.toString args, ");"
67 : jhr 3470 ]
68 :     (* end case *))
69 :     in
70 :     String.concat(typedVar y :: " = " :: rhs)
71 :     end
72 :    
73 :     fun massignToString ([], rator, xs) = String.concat [
74 : jhr 3512 Op.toString rator, "(", String.concatWithMap "," Var.toString xs, ");"
75 : jhr 3470 ]
76 :     | massignToString (ys, rator, xs) = String.concat [
77 :     "(", String.concatWith "," (List.map typedVar ys), ") = ",
78 : jhr 3512 Op.toString rator, "(", String.concatWithMap "," Var.toString xs, ");"
79 : jhr 3470 ]
80 :    
81 : jhr 3475 fun labelOf (IR.ND{id, ...}) = "L"^Stamp.toString id
82 : jhr 3470
83 : jhr 3475 fun ppCFG (out, cfg as IR.CFG{entry, exit}) = let
84 :     fun goto (out, nd) = (case IR.Node.kind nd
85 :     of IR.JOIN _ => (
86 :     prln(incIndent out, ["goto ", IR.Node.toString nd, "\n"]);
87 : jhr 3470 ppNd (out, false, nd))
88 : jhr 3509 | IR.FOREACH _ =>
89 :     if isMarked nd
90 :     then prln(incIndent out, ["goto ", IR.Node.toString nd, "\n"])
91 :     else ppNd (out, false, nd)
92 : jhr 3470 | _ => ppNd (out, true, nd)
93 :     (* end case *))
94 :     and ppNd (out, noLabel, nd) = let
95 :     val out1 = incIndent out
96 : jhr 3473 fun prPhi out (y, xs) =
97 :     prln (out, [
98 : jhr 3470 typedVar y, " = phi(",
99 :     String.concatWith "," (List.mapPartial (Option.map Var.toString) xs), ")\n"
100 :     ])
101 :     in
102 :     if isMarked nd
103 :     then ()
104 :     else (
105 :     mark nd;
106 :     if noLabel andalso (! compact)
107 :     then ()
108 : jhr 3475 else (case IR.Node.kind nd
109 :     of IR.JOIN{preds, mask, ...} => let
110 : jhr 3470 val preds = ListPair.mapEq
111 : jhr 3475 (fn (false, nd) => IR.Node.toString nd
112 :     | (true, nd) => "*" ^ IR.Node.toString nd)
113 : jhr 3470 (!mask, !preds)
114 :     (* +DEBUG*)
115 :     handle ex => (
116 : jhr 3475 print(concat["**** Broken CFG at ", IR.Node.toString nd, "\n"]);
117 :     List.map IR.Node.toString (!preds))
118 : jhr 3470 (* -DEBUG*)
119 :     in
120 :     prln (out, [
121 : jhr 3475 IR.Node.toString nd, ": preds = [",
122 : jhr 3470 String.concatWith "," preds, "]\n"
123 :     ])
124 :     end
125 :     | _ => prln (out, [
126 : jhr 3475 IR.Node.toString nd, ": preds = [",
127 :     String.concatWith "," (List.map IR.Node.toString (IR.Node.preds nd)),
128 : jhr 3470 "]\n"
129 :     ])
130 :     (* end case *));
131 : jhr 3475 case IR.Node.kind nd
132 :     of IR.NULL => ()
133 :     | IR.ENTRY{succ} => goto (out, !succ)
134 :     | IR.JOIN{phis, succ, ...} => (
135 : jhr 3473 List.app (prPhi out1) (!phis);
136 : jhr 3470 goto (out, !succ))
137 : jhr 3475 | IR.COND{cond, trueBranch, falseBranch, ...} => (
138 : jhr 3470 prln (out1, [
139 :     "if ", Var.toString cond,
140 : jhr 3475 " then goto ", IR.Node.toString(!trueBranch),
141 :     " else goto ", IR.Node.toString(!falseBranch), "\n"
142 : jhr 3470 ]);
143 :     ppNd (out, false, !trueBranch);
144 :     ppNd (out, false, !falseBranch))
145 : jhr 3506 | IR.FOREACH{phis, var, src, bodyEntry, succ, ...} => (
146 : jhr 3473 prln (out1, [
147 :     "foreach (", Var.toString var, " in ", Var.toString src, ")",
148 : jhr 3475 " on exit goto ", IR.Node.toString(!succ), "\n"
149 : jhr 3473 ]);
150 :     List.app (prPhi (incIndent out1)) (!phis);
151 : jhr 3506 goto (out, !bodyEntry);
152 : jhr 3509 ppNd (out, false, !succ))
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 3475 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 3485 fun ppGlobal prefix outS gv = (
237 :     indent outS;
238 :     prl(outS, [
239 :     prefix, " ", Ty.toString(GVar.ty gv), " ", GVar.uniqueName gv, "\n"
240 :     ]))
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 3485 List.app (ppGlobal "const" out1) consts;
259 : jhr 3506 List.app (ppInput out1) inputs;
260 : jhr 3485 List.app (ppGlobal "global" 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