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

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