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

SCM Repository

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

Annotation of /branches/pure-cfg/src/compiler/IL/ssa-pp-fn.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 736 - (view) (download)

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

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