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

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