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

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