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

SCM Repository

[diderot] Annotation of /branches/vis15/src/compiler/tree-ir/tree-pp.sml
ViewVC logotype

Annotation of /branches/vis15/src/compiler/tree-ir/tree-pp.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3952 - (view) (download)

1 : jhr 3753 (* tree-pp.sml
2 : jhr 3691 *
3 :     * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
4 :     *
5 :     * COPYRIGHT (c) 2016 The University of Chicago
6 :     * All rights reserved.
7 :     *
8 :     * Printing for the TreeIR
9 :     *)
10 :    
11 : jhr 3753 structure TreePP : sig
12 : jhr 3691
13 : jhr 3948 val expToString : TreeIR.exp -> string
14 :    
15 : jhr 3691 val statement : TextIO.outstream * TreeIR.stm -> unit
16 :    
17 :     val block : TextIO.outstream * TreeIR.block -> unit
18 :    
19 :     val program : TextIO.outstream * TreeIR.program -> unit
20 :    
21 : jhr 3753 val output : TextIO.outstream * string * TreeIR.program -> unit
22 :    
23 : jhr 3691 end = struct
24 :    
25 :     structure IR = TreeIR
26 : jhr 3833 structure Ty = TreeTypes
27 :     structure Op = TreeOps
28 :     structure Var = TreeVar
29 :     structure GVar = TreeGlobalVar
30 :     structure SVar = TreeStateVar
31 : jhr 3691
32 :     fun indent (outS, i) = TextIO.output(outS, StringCvt.padLeft #" " i "")
33 :     fun incIndent (outS, i) = (outS, i+2)
34 :     fun decIndent (outS, i) = (outS, Int.max(0, i-2))
35 :     fun pr ((outS, _), s) = TextIO.output(outS, s)
36 :     fun prl (out, l) = pr(out, concat l)
37 :     fun prln (out, l) = (indent out; prl(out, l))
38 :    
39 :     fun descToString NONE = ""
40 :     | descToString (SOME desc) = String.toString desc
41 :    
42 :     fun expToString e = let
43 :     fun argsToS (lp, args, rp, l) = let
44 :     fun argToS ([], l) = l
45 : jhr 3833 | argToS ([e], l) = toS (e, l)
46 : jhr 3691 | argToS (e::es, l) = toS(e, "," :: argToS(es, l))
47 :     in
48 :     lp :: argToS(args, rp :: l)
49 :     end
50 : jhr 3857 and toS (IR.E_Global x, l) = GVar.toString x :: l
51 : jhr 3848 | toS (IR.E_State(NONE, x), l) = "self." :: SVar.toString x :: l
52 : jhr 3852 | toS (IR.E_State(SOME e, fld), l) = toS (e, "." :: SVar.toString fld :: l)
53 : jhr 3691 | toS (IR.E_Var x, l) = Var.name x :: l
54 :     | toS (IR.E_Lit lit, l) = Literal.toString lit :: l
55 :     | toS (IR.E_Op(rator, args), l) = Op.toString rator :: argsToS ("(", args, ")", l)
56 : jhr 3889 | toS (IR.E_Vec(w, pw, args), l) = let
57 :     val args = argsToS ("(", args, ")", l)
58 :     in
59 :     if (w = pw)
60 :     then "VEC" :: Int.toString w :: args
61 :     else "VEC" :: Int.toString w :: "{" :: Int.toString pw :: "}" :: args
62 :     end
63 :     | toS (IR.E_Cons(args, ty), l) =
64 : jhr 3691 "<" :: Ty.toString ty :: ">" :: argsToS ("[", args, "]", l)
65 : jhr 3753 | toS (IR.E_Seq(args, ty), l) =
66 : jhr 3691 "<" :: Ty.toString ty :: ">" :: argsToS ("{", args, "}", l)
67 : jhr 3850 | toS (IR.E_Pack(_, es), l) = "PACK" :: argsToS ("(", es, ")", l)
68 : jhr 3844 | toS (IR.E_VLoad(layout, e, i), l) =
69 :     "LOAD_" :: Int.toString i :: "(" :: toS(e, ")" :: l)
70 : jhr 3691 in
71 :     String.concat (toS (e, []))
72 :     end
73 :    
74 :     fun argsToString (prefix, es) = String.concat[
75 :     prefix, "(", String.concatWith "," (List.map expToString es), ")"
76 :     ]
77 :    
78 :     fun ppGlobalDecl out x = prln (out, [
79 : jhr 3833 if GVar.isInput x then "input " else "",
80 :     Ty.toString(GVar.ty x), " ", GVar.name x, ";\n"
81 : jhr 3691 ])
82 :    
83 :     fun ppVarDecl out x = prln (out, [Ty.toString(Var.ty x), " ", Var.name x, ";\n"])
84 :    
85 :     fun ppStrand out strand = let
86 :     val IR.Strand{name, params, state, stateInit, initM, updateM, stabilizeM} = strand
87 :     val out' = incIndent out
88 :     fun ppParams [] = ()
89 :     | ppParams [x] = prl(out, [Ty.toString (Var.ty x), " ", Var.name x])
90 :     | ppParams (x::r) = (
91 :     prl(out, [Ty.toString (Var.ty x), " ", Var.name x, ","]);
92 :     ppParams r)
93 : jhr 3833 fun ppSVarDecl sv = let
94 :     val v = if SVar.isVarying sv then "varying " else ""
95 :     val out = if SVar.isOutput sv then "output " else ""
96 : jhr 3691 in
97 : jhr 3833 prln (out', [v, out, Ty.toString(SVar.ty sv), " ", SVar.name sv, ";\n"])
98 : jhr 3691 end
99 :     in
100 :     prln (out, ["strand ", Atom.toString name, " ("]);
101 :     ppParams params; pr(out, ") {\n");
102 :     List.app ppSVarDecl state;
103 : jhr 3952 ppMethod (out', "init", SOME stateInit);
104 : jhr 3691 ppMethod (out', "initially", initM);
105 :     ppMethod (out', "update", SOME updateM);
106 :     ppMethod (out', "stabilize", stabilizeM);
107 :     prln (out, ["}\n"])
108 :     end
109 :    
110 : jhr 3952 and ppMethod (out, name, SOME(IR.Method{usesGlobals, needsWorld, body})) = let
111 :     val params = (case (usesGlobals, needsWorld)
112 :     of (false, false) => " () "
113 :     | (false, true) => " (world) "
114 :     | (true, false) => " (globals) "
115 :     | (true, true) => " (globals, world) "
116 :     (* end case *))
117 :     in
118 :     prln (out, [name, params]);
119 :     ppBlock (out, body);
120 :     pr (out, "\n")
121 :     end
122 : jhr 3691 | ppMethod (_, _, NONE) = ()
123 :    
124 :     and ppBlock (out, IR.Block{locals, body}) = let
125 :     val out' = incIndent out
126 :     in
127 :     pr (out, "{\n");
128 : jhr 3848 List.app (ppVarDecl out') (!locals);
129 : jhr 3691 List.app (fn stm => ppStm(out', stm)) body;
130 :     indent out; pr (out, "}")
131 :     end
132 :    
133 :     and ppStm (out, stm) = (case stm
134 :     of IR.S_Comment text => let
135 :     val out = decIndent out
136 :     in
137 :     List.app (fn s => prln(out, ["// ", s, "\n"])) text
138 :     end
139 : jhr 3857 | IR.S_Assign(true, x, e) =>
140 :     prln(out, [Ty.toString(Var.ty x), " ", Var.name x, " = ", expToString e, ";\n"])
141 :     | IR.S_Assign(false, x, e) => prln(out, [Var.name x, " = ", expToString e, ";\n"])
142 : jhr 3845 | IR.S_MAssign([], e) => prln(out, [expToString e, ";\n"])
143 :     | IR.S_MAssign([x], e) => prln(out, [Var.name x, " = ", expToString e, ";\n"])
144 :     | IR.S_MAssign(x::xs, e) => (
145 :     prln(out, ["(", Var.name x]);
146 :     List.app (fn x => prl(out, [",", Var.name x])) xs;
147 :     prl (out, [") = ", expToString e, ";\n"]))
148 : jhr 3833 | IR.S_GAssign(x, e) => prln(out, [GVar.name x, " = ", expToString e, ";\n"])
149 : jhr 3691 | IR.S_IfThen(cond, blk) => (
150 :     prln (out, ["if (", expToString cond, ") "]);
151 :     ppBlock (out, blk);
152 :     pr (out, "\n"))
153 :     | IR.S_IfThenElse(cond, blk1, blk2) => (
154 :     prln (out, ["if (", expToString cond, ") "]);
155 :     ppBlock (out, blk1);
156 :     pr (out, " else ");
157 :     ppBlock (out, blk2);
158 :     pr (out, "\n"))
159 : jhr 3924 | IR.S_For(x, e1, e2, blk) => (
160 :     prln (out, [
161 :     "for ", Var.name x, " = ", expToString e1, " to ", expToString e2, " "
162 :     ]);
163 :     ppBlock (out, blk);
164 :     pr (out, "\n"))
165 : jhr 3691 | IR.S_Foreach(x, e, blk) => (
166 :     prln (out, [
167 :     "foreach ", Ty.toString(Var.ty x), " ", Var.name x, " in ", expToString e, " "
168 :     ]);
169 :     ppBlock (out, blk);
170 :     pr (out, "\n"))
171 : jhr 3894 | IR.S_LoadNrrd(x, ty, nrrd) =>
172 : jhr 3691 prln (out, [
173 : jhr 3894 Var.name x, " = load<", APITypes.toString ty, "> (\"",
174 : jhr 3754 String.toString nrrd, "\");\n"
175 : jhr 3691 ])
176 :     | IR.S_Input(x, name, desc, NONE) =>
177 :     prln (out, [
178 : jhr 3833 GVar.name x, " = input<", Ty.toString(GVar.ty x), "> (\"",
179 : jhr 3691 String.toString name, "\",\"", descToString desc, "\");\n"
180 :     ])
181 :     | IR.S_Input(x, name, desc, SOME dflt) =>
182 :     prln (out, [
183 : jhr 3833 GVar.name x, " = input<", Ty.toString(GVar.ty x), "> (\"",
184 : jhr 3691 String.toString name, "\",\"", descToString desc, "\",",
185 :     expToString dflt, ");\n"
186 :     ])
187 :     | IR.S_InputNrrd(x, name, desc, NONE) =>
188 :     prln (out, [
189 : jhr 3833 GVar.name x, " = input-nrrd<", Ty.toString(GVar.ty x), "> (\"",
190 : jhr 3691 String.toString name, "\",\"", descToString desc, "\");\n"
191 :     ])
192 :     | IR.S_InputNrrd(x, name, desc, SOME dflt) =>
193 :     prln (out, [
194 : jhr 3833 GVar.name x, " = input-nrrd<", Ty.toString(GVar.ty x), "> (\"",
195 : jhr 3691 String.toString name, "\",\"", descToString desc, "\",\"",
196 :     String.toString dflt, "\");\n"
197 :     ])
198 : jhr 3749 | IR.S_New(strand, args) =>
199 :     prln (out, [argsToString("new "^Atom.toString strand, args), ";\n"])
200 : jhr 3767 | IR.S_Save(x, rhs) =>
201 : jhr 3927 prln (out, ["self.", SVar.toString x, " = ", expToString rhs, ";\n"])
202 : jhr 3835 | IR.S_Exit => prln (out, ["exit;\n"])
203 : jhr 3768 | IR.S_Print(_, es) => prln (out, [argsToString("print", es), ";\n"])
204 : jhr 3691 (* return functions for methods *)
205 :     | IR.S_Active => prln (out, ["active;\n"])
206 :     | IR.S_Stabilize => prln (out, ["stabilize;\n"])
207 :     | IR.S_Die => prln (out, ["die;\n"])
208 :     (* end case *))
209 :    
210 :     fun statement (outS, stm) = ppStm((outS, 0), stm)
211 :    
212 :     fun block (outS, blk) = (ppBlock ((outS, 0), blk); pr ((outS, 0), "\n"))
213 :    
214 :     fun program (outS, prog) = let
215 :     val IR.Program{
216 : jhr 3861 props, target, consts, inputs, constInit,
217 : jhr 3924 globals, globalInit, strand,
218 :     create=IR.Create{dim, code}, update
219 : jhr 3691 } = prog
220 :     val out = (outS, 0)
221 :     val out' = incIndent out
222 :     in
223 :     pr(out, "//***** PROPERTIES *****\n");
224 :     case props
225 :     of [] => prln(out', ["none\n"])
226 :     | _ => prln(out', [String.concatWith " " (List.map Properties.toString props), "\n"])
227 :     (* end case *);
228 :     prln(out, ["//***** CONSTS *****\n"]);
229 :     List.app (ppGlobalDecl out') consts;
230 :     prln(out, ["//***** INPUTS *****\n"]);
231 :     (* FIXME
232 :     List.app (ppGlobalDecl out') inputs;
233 :     *)
234 :     prln(out, ["//***** CONST INIT *****\n"]);
235 :     indent out'; ppBlock (out', constInit); pr (out, "\n");
236 :     prln(out, ["//***** GLOBALS *****\n"]);
237 :     List.app (ppGlobalDecl out') globals;
238 :     prln(out, ["//***** GLOBAL INIT *****\n"]);
239 :     indent out'; ppBlock (out', globalInit); pr (out, "\n");
240 :     prln(out, ["//***** STRAND *****\n"]);
241 :     ppStrand out strand;
242 : jhr 3924 case dim
243 :     of SOME d => prln(out, ["//***** CREATE GRID(", Int.toString d, ") *****\n"])
244 :     | NONE => prln(out, ["//***** CREATE COLLECTION *****\n"])
245 :     (* end case *);
246 :     indent out'; ppBlock (out', code); pr (out, "\n");
247 : jhr 3691 case update
248 :     of SOME blk => (
249 :     prln(out, ["//***** GLOBAL UPDATE *****\n"]);
250 :     indent out'; ppBlock (out', blk); pr (out, "\n"))
251 :     | NONE => ()
252 :     (* end case *)
253 :     end
254 :    
255 : jhr 3753 fun output (outS, msg, prog) = (
256 :     pr ((outS, 0), concat["##### TreeIR: ", msg, " ####\n"]);
257 :     program (outS, prog);
258 :     pr ((outS, 0), "#### end program ####\n"))
259 :    
260 : jhr 3691 end

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