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

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