Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] Annotation of /trunk/src/compiler/IL/ssa-pp-fn.sml
ViewVC logotype

Annotation of /trunk/src/compiler/IL/ssa-pp-fn.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3349 - (view) (download)

1 : jhr 198 (* ssa-pp-fn.sml
2 :     *
3 : jhr 3349 * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
4 :     *
5 :     * COPYRIGHT (c) 2015 The University of Chicago
6 : jhr 198 * All rights reserved.
7 :     *
8 :     * Pretty printing for SSA representations
9 :     *)
10 :    
11 :     functor SSAPPFn (IL : SSA) : sig
12 :    
13 : jhr 350 val assignToString : IL.assign -> string
14 :    
15 : jhr 340 val output : TextIO.outstream * string * IL.program -> unit
16 : jhr 256
17 : jhr 198 end = struct
18 :    
19 : jhr 256 structure Op = IL.Op
20 :     structure Var = IL.Var
21 : jhr 398 structure Ty = IL.Ty
22 : jhr 256
23 : jhr 1116 local
24 :     val {getFn, setFn} = IL.Node.newFlag()
25 :     in
26 :     val isMarked = getFn
27 :     fun mark nd = setFn(nd, true)
28 :     fun clear nd = setFn(nd, false)
29 :     end
30 :    
31 :     (* if true, the output is in compact form *)
32 :     val compact = ref true
33 :    
34 : jhr 256 fun indent (outS, i) = TextIO.output(outS, StringCvt.padLeft #" " i "")
35 :     fun incIndent (outS, i) = (outS, i+2)
36 :     fun pr ((outS, _), s) = TextIO.output(outS, s)
37 :     fun prl (out, l) = pr(out, concat l)
38 : jhr 1116 fun prln (out, l) = (indent out; prl(out, l))
39 : jhr 256
40 : jhr 511 fun typedVar x = String.concat [
41 : jhr 2356 Ty.toString(Var.ty x), " ", Var.toString x, "#", Int.toString(Var.useCount x)
42 :     ]
43 : jhr 398
44 : jhr 350 fun assignToString (y, rhs) = let
45 : jhr 2356 val rhs = (case rhs
46 :     of IL.STATE x => [IL.StateVar.toString x, ";"]
47 : jhr 1640 | IL.VAR x => [Var.toString x, ";"]
48 : jhr 2356 | IL.LIT lit => [Literal.toString lit, ";"]
49 :     | IL.OP(rator, []) => [Op.toString rator, ";"]
50 :     | IL.OP(rator, args) => [
51 :     Op.toString rator, "(",
52 :     String.concatWith "," (List.map Var.toString args), ");"
53 :     ]
54 :     | IL.APPLY(f, args) => [
55 :     MathFuns.toString f, "(",
56 :     String.concatWith "," (List.map Var.toString args), ");"
57 :     ]
58 :     | IL.CONS(ty, xs) => [
59 :     "<", Ty.toString ty, ">[",
60 :     String.concatWith "," (List.map Var.toString xs), "];"
61 :     ]
62 :     (* end case *))
63 :     in
64 :     String.concat(typedVar y :: " = " :: rhs)
65 :     end
66 : jhr 256
67 : jhr 1640 fun massignToString ([], rator, xs) = String.concat [
68 :     Op.toString rator, "(", String.concatWith "," (List.map Var.toString xs), ");"
69 :     ]
70 :     | massignToString (ys, rator, xs) = String.concat [
71 :     "(", String.concatWith "," (List.map typedVar ys), ") = ",
72 :     Op.toString rator, "(", String.concatWith "," (List.map Var.toString xs), ");"
73 :     ]
74 :    
75 : jhr 1116 fun labelOf (IL.ND{id, ...}) = "L"^Stamp.toString id
76 : jhr 256
77 : jhr 1116 fun ppCFG (out, cfg as IL.CFG{entry, exit}) = let
78 : jhr 2356 fun goto (out, nd) = if (List.length(IL.Node.preds nd) > 1)
79 :     then (
80 :     prln(incIndent out, ["goto ", IL.Node.toString nd, "\n"]);
81 :     ppNd (out, false, nd))
82 :     else ppNd (out, true, nd)
83 :     and ppNd (out, noLabel, nd) = let
84 :     val out1 = incIndent out
85 :     fun prPhi (y, xs) =
86 :     prln (out1, [
87 :     typedVar y, " = phi(",
88 :     String.concatWith "," (List.map Var.toString xs), ")\n"
89 :     ])
90 :     in
91 :     if isMarked nd
92 :     then ()
93 :     else (
94 :     mark nd;
95 :     if noLabel andalso (! compact)
96 :     then ()
97 :     else prln (out, [
98 :     IL.Node.toString nd, ": preds = [",
99 :     String.concatWith "," (List.map IL.Node.toString (IL.Node.preds nd)),
100 :     "]\n"
101 :     ]);
102 :     case IL.Node.kind nd
103 :     of IL.NULL => ()
104 :     | IL.ENTRY{succ} => goto (out, !succ)
105 :     | IL.JOIN{phis, succ, ...} => (
106 :     List.app prPhi (!phis);
107 :     goto (out, !succ))
108 :     | IL.COND{cond, trueBranch, falseBranch, ...} => (
109 :     prln (out1, [
110 :     "if ", Var.toString cond,
111 :     " then goto ", IL.Node.toString(!trueBranch),
112 :     " else goto ", IL.Node.toString(!falseBranch), "\n"
113 :     ]);
114 :     ppNd (out, false, !trueBranch);
115 :     ppNd (out, false, !falseBranch))
116 :     | IL.COM{text, succ, ...} => (
117 :     List.app (fn s => prln (out1, ["//", s, "\n"])) text;
118 :     goto (out, !succ))
119 :     | IL.ASSIGN{stm, succ, ...} => (
120 :     prln (out1, [assignToString stm, "\n"]);
121 :     goto (out, !succ))
122 :     | IL.MASSIGN{stm, succ, ...} => (
123 :     prln (out1, [massignToString stm, "\n"]);
124 :     goto (out, !succ))
125 :     | IL.NEW{strand, args, succ, ...} => (
126 :     prln (out1, [
127 :     "new ", Atom.toString strand, "(",
128 :     String.concatWith "," (List.map Var.toString args), ");\n"
129 :     ]);
130 :     goto (out, !succ))
131 :     | IL.SAVE{lhs, rhs, succ, ...} => (
132 :     prln (out1, [
133 :     IL.StateVar.toString lhs, " = ", Var.toString rhs, ";\n"
134 :     ]);
135 :     goto (out, !succ))
136 :     | IL.EXIT{kind, live, ...} => let
137 :     val live = (case live
138 :     of [] => "()"
139 :     | [x] => String.concat["(", Var.toString x, ")"]
140 :     | xs => String.concat[
141 :     "(",
142 :     String.concatWith "," (List.map Var.toString xs),
143 :     ")"
144 :     ]
145 :     (* end case *))
146 :     in
147 :     case kind
148 :     of ExitKind.FRAGMENT =>
149 :     prln (out1, ["live vars = ", live, "\n"])
150 :     | ExitKind.SINIT =>
151 :     prln (out1, ["strand_init ", live, "\n"])
152 :     | ExitKind.RETURN =>
153 :     prln (out1, ["return ", live, "\n"])
154 :     | ExitKind.ACTIVE =>
155 :     prln (out1, ["active ", live, "\n"])
156 :     | ExitKind.STABILIZE =>
157 :     prln (out1, ["stabilize ", live, "\n"])
158 :     | ExitKind.DIE =>
159 :     prln (out1, ["die ", live, "\n"])
160 :     (* end case *)
161 :     end
162 :     (* end case *))
163 :     end
164 :     in
165 :     ppNd (out, false, entry);
166 :     (* clear marks *)
167 :     IL.CFG.apply clear cfg
168 :     end
169 : jhr 1116
170 :     fun ppInitially (out, IL.Initially{isArray, rangeInit, iters, create}) = let
171 : jhr 2356 val (initArg, strand, args) = create
172 :     fun ppIters (out, []) = (
173 :     ppCFG (out, initArg);
174 :     prln (out, [
175 :     "new ", Atom.toString strand, "(",
176 :     String.concatWith "," (List.map Var.toString args), ");\n"
177 :     ]))
178 :     | ppIters (out, (param, lo, hi)::iters) = (
179 :     indent out; prl(out, [
180 :     "for ", typedVar param, " = ", Var.toString lo, " .. ", Var.toString hi, "\n"
181 :     ]);
182 :     ppIters (incIndent out, iters))
183 :     in
184 :     indent out; prl(out, if isArray then ["ARRAY\n"] else ["COLLECTION\n"]);
185 :     ppCFG (incIndent out, rangeInit);
186 :     ppIters (incIndent out, iters)
187 :     end
188 : jhr 256
189 : jhr 1640 fun ppMethod (out, IL.Method{name, body}) = let
190 : jhr 2356 val out1 = incIndent out
191 :     fun prVars xs = List.app (fn x => prl(out, [" ", typedVar x, ";"])) xs
192 :     in
193 :     indent out; prl(out, ["method ", StrandUtil.nameToString name, "\n"]);
194 :     ppCFG (incIndent out1, body);
195 :     indent out; prl(out, ["end ", StrandUtil.nameToString name, "\n"])
196 :     end
197 : jhr 256
198 : jhr 511 and ppStrand (out, IL.Strand{name, params, state, stateInit, methods}) = let
199 : jhr 2356 val out1 = incIndent out
200 : jhr 1640 fun stateVar x = let
201 :     val l = [Ty.toString(IL.StateVar.ty x), " ", IL.StateVar.toString x, ";"]
202 :     val l = if IL.StateVar.isOutput x then "output " :: l else l
203 :     in
204 :     prl (out1, l)
205 :     end
206 : jhr 2356 in
207 :     indent out;
208 :     prl(out, [
209 :     "strand ", Atom.toString name, " (",
210 :     String.concatWith ", " (List.map typedVar params), ")\n"
211 :     ]);
212 :     indent out1; pr(out1, "state: "); List.app stateVar state; pr(out1, "\n");
213 :     ppCFG (incIndent out1, stateInit);
214 :     List.app (fn m => ppMethod(out1, m)) methods;
215 :     indent out; prl(out, ["end ", Atom.toString name, "\n"])
216 :     end
217 : jhr 256
218 : jhr 1640 fun output (outS, msg, IL.Program{props, globalInit, initially, strands}) = let
219 : jhr 2356 val out = (outS, 0)
220 :     val out1 = incIndent out
221 :     in
222 :     pr (out, concat["##### ", msg, " ####\n"]);
223 : jhr 2636 pr (out, "## properties\n");
224 :     prln (out1, [StrandUtil.propsToString props, "\n"]);
225 : jhr 2356 pr (out, "## globals\n");
226 :     List.app
227 :     (fn x => (indent out1; prl(out1, ["global ", typedVar x, "\n"])))
228 :     (IL.CFG.liveAtExit globalInit);
229 :     pr (out, "## global initialization\n");
230 :     ppCFG (out1, globalInit);
231 :     pr (out, "## initially\n");
232 :     ppInitially (out1, initially);
233 :     pr (out, "## strands\n");
234 :     List.app (fn strand => ppStrand(out1, strand)) strands;
235 :     pr (out, "#### end program ####\n")
236 :     end
237 : jhr 256
238 : jhr 198 end

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