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

SCM Repository

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

Annotation of /branches/charisee/src/compiler/tree-il/tree-il-pp.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3544 - (view) (download)

1 : jhr 1115 (* tree-il-pp.sml
2 :     *
3 : jhr 3349 * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
4 :     *
5 :     * COPYRIGHT (c) 2015 The University of Chicago
6 : jhr 1115 * All rights reserved.
7 :     *
8 :     * Printing for the TreeIL
9 :     *)
10 :    
11 :     structure TreeILPP : sig
12 :    
13 :     val statement : TextIO.outstream * TreeIL.stm -> unit
14 :    
15 :     val block : TextIO.outstream * TreeIL.block -> unit
16 :    
17 :     val program : TextIO.outstream * TreeIL.program -> unit
18 :    
19 :     end = struct
20 :    
21 :     structure IL = TreeIL
22 :     structure Op = IL.Op
23 :     structure Var = IL.Var
24 :     structure Ty = IL.Ty
25 :    
26 :     fun indent (outS, i) = TextIO.output(outS, StringCvt.padLeft #" " i "")
27 :     fun incIndent (outS, i) = (outS, i+2)
28 :     fun decIndent (outS, i) = (outS, Int.max(0, i-2))
29 :     fun pr ((outS, _), s) = TextIO.output(outS, s)
30 :     fun prl (out, l) = pr(out, concat l)
31 :     fun prln (out, l) = (indent out; prl(out, l))
32 :    
33 : jhr 2632 fun itos m = Int.toString m
34 : cchiw 2795 fun preBool(pre, true)=pre
35 :     | preBool(_,false)=""
36 :    
37 : jhr 3060 fun descToString NONE = ""
38 :     | descToString (SOME desc) = String.toString desc
39 :    
40 : jhr 1115 fun expToString e = let
41 : jhr 2632 fun argsToS (shift, lp, args, rp, l) = let
42 : jhr 2356 fun argToS ([], l) = l
43 : cchiw 2627 | argToS ([e], l) = toS (e, l)
44 : jhr 3060 | argToS (e::es, l) = toS(e, shift :: argToS(es, l))
45 : jhr 2356 in
46 :     lp :: argToS(args, rp :: l)
47 :     end
48 : jhr 3060 and toS (IL.E_State x, l) = "self." :: IL.stateVarToString x :: l
49 : jhr 1640 | toS (IL.E_Var x, l) = (case (Var.kind x)
50 : jhr 3060 of IL.VK_Local => Var.name x :: l
51 :     | _ => "::" :: Var.name x :: l
52 : jhr 2356 (* end case *))
53 :     | toS (IL.E_Lit lit, l) = Literal.toString lit :: l
54 : cchiw 2627 | toS (IL.E_Op(rator, args), l) = Op.toString rator :: argsToS (",","(", args, ")", l)
55 :     | toS (IL.E_Apply(f, args), l) = MathFuns.toString f :: argsToS (",","(", args, ")", l)
56 : cchiw 2795 | toS (IL.E_Cons(nSize, oSize,args), l) =
57 : jhr 3060 "ConsExp<" :: itos nSize :: ">" :: itos oSize:: argsToS (",","{", args, "}", l)
58 : cchiw 2795 | toS (IL.E_Mux(A,isFill,oSize, pieces, args),l) =
59 : jhr 3060 "Mux<" ::preBool("_Aligned_", A) ::preBool("_filled_", isFill) :: ":" :: itos oSize ::"[>\t ]"::argsToS (",","(", args, ")", l)
60 : cchiw 2691 | toS (IL.E_LoadArr(A,nSize,oSize, arg, offset), l) =
61 : jhr 3060 "LdArr<" :: itos nSize :: ":" :: itos oSize :: ">("
62 :     :: argsToS (",", "(", [arg, offset], ")", l)
63 : cchiw 2789 | toS (IL.E_Holder _ ,l)= "Holder"::l
64 : jhr 3060 in
65 : jhr 2356 String.concat (toS (e, []))
66 :     end
67 : jhr 1115
68 :     fun argsToString (prefix, es) = String.concat[
69 : jhr 2356 prefix, "(", String.concatWith "," (List.map expToString es), ")"
70 :     ]
71 : jhr 1115
72 :     fun ppVarDecl out x = prln (out, [Ty.toString(Var.ty x), " ", Var.name x, ";\n"])
73 :    
74 :     fun ppStrand out (IL.Strand{name, params, state, stateInit, methods}) = let
75 : jhr 2356 val out' = incIndent out
76 :     fun ppParams [] = ()
77 :     | ppParams [x] = prl(out, [Ty.toString (Var.ty x), " ", Var.name x])
78 :     | ppParams (x::r) = (
79 :     prl(out, [Ty.toString (Var.ty x), " ", Var.name x, ","]);
80 :     ppParams r)
81 :     fun ppSVarDecl (IL.SV{varying, output, name, ty, ...}) = let
82 :     val v = if varying then "varying " else ""
83 :     val out = if output then "output " else ""
84 :     in
85 :     prln (out', [v, out, Ty.toString ty, " ", name, ";\n"])
86 :     end
87 :     in
88 :     prln (out, ["strand ", Atom.toString name, " ("]);
89 :     ppParams params; pr(out, ") {\n");
90 :     List.app ppSVarDecl state;
91 :     prln (out', ["init () "]); ppBlock(out', stateInit); pr (out', "\n");
92 :     List.app (ppMethod out') methods;
93 :     prln (out, ["}\n"])
94 :     end
95 : jhr 1115
96 :     and ppMethod out (IL.Method{name, body}) = (
97 : jhr 2356 prln (out, [StrandUtil.nameToString name, " () "]);
98 :     ppBlock (out, body);
99 :     pr (out, "\n"))
100 : jhr 1115
101 :     and ppBlock (out, IL.Block{locals, body}) = let
102 : jhr 2356 val out' = incIndent out
103 :     in
104 :     pr (out, "{\n");
105 :     List.app (ppVarDecl out') locals;
106 :     List.app (fn stm => ppStm(out', stm)) body;
107 :     indent out; pr (out, "}")
108 :     end
109 : jhr 1115
110 :     and ppStm (out, stm) = (case stm
111 : jhr 2356 of IL.S_Comment text => let
112 :     val out = decIndent out
113 :     in
114 :     List.app (fn s => prln(out, ["// ", s, "\n"])) text
115 :     end
116 :     | IL.S_Assign([], e) => prln(out, [expToString e, ";\n"])
117 : jhr 3060 | IL.S_Assign([x], e) => prln(out, [Var.name x, " = ", expToString e, ";\n"])
118 : jhr 2356 | IL.S_Assign(x::xs, e) => (
119 : jhr 1640 prln(out, ["(", Var.name x]);
120 :     List.app (fn x => prl(out, [",", Var.name x])) xs;
121 :     prl (out, [") = ", expToString e, ";\n"]))
122 : jhr 2356 | IL.S_IfThen(cond, blk) => (
123 :     prln (out, ["if (", expToString cond, ") "]);
124 :     ppBlock (out, blk);
125 :     pr (out, "\n"))
126 :     | IL.S_IfThenElse(cond, blk1, blk2) => (
127 :     prln (out, ["if (", expToString cond, ") "]);
128 :     ppBlock (out, blk1);
129 :     pr (out, " else ");
130 :     ppBlock (out, blk2);
131 :     pr (out, "\n"))
132 : jhr 1115 (*
133 : jhr 2356 | IL.S_For(x, e1, e2, blk) => (
134 :     prln (out, [
135 :     "for (", Ty.toString(Var.ty x), " ", Var.name x, " = ",
136 :     expToString e1, " ..", expToString e2, ") "
137 :     ]);
138 :     ppBlock (out, blk);
139 :     pr (out, "\n"))
140 : jhr 1115 *)
141 : jhr 2356 | IL.S_New(strand, args) =>
142 :     prln (out, [argsToString("new "^Atom.toString strand, args), ";\n"])
143 : cchiw 2827 | IL.S_Copy(x, rhs,offset,n) =>
144 :     prln (out, ["Copy Array",itos n,"offset",itos offset,"(",(*expToString x,*)" from ", expToString rhs, ");\n"])
145 : jhr 1640 | IL.S_Save([x], rhs) =>
146 : jhr 3060 prln (out, [IL.stateVarToString x, " = ", expToString rhs, ";\n"])
147 : jhr 2356 | IL.S_Save(x::xs, e) => (
148 : jhr 1640 prln(out, ["(", IL.stateVarToString x]);
149 :     List.app (fn x => prl(out, [",", IL.stateVarToString x])) xs;
150 :     prl (out, [") = ", expToString e, ";\n"]))
151 : jhr 3060 | IL.S_LoadNrrd(x, ty, nrrd) =>
152 : jhr 2356 prln (out, [
153 : jhr 3060 Var.name x, " = load<", Ty.toString ty, "> (\"", String.toString nrrd, "\");\n"
154 : jhr 2356 ])
155 :     | IL.S_Input(x, name, desc, NONE) =>
156 :     prln (out, [
157 :     Var.name x, " = input<", Ty.toString(Var.ty x), "> (\"",
158 : jhr 3060 String.toString name, "\",\"", descToString desc, "\");\n"
159 : jhr 2356 ])
160 :     | IL.S_Input(x, name, desc, SOME dflt) =>
161 :     prln (out, [
162 :     Var.name x, " = input<", Ty.toString(Var.ty x), "> (\"",
163 : jhr 3060 String.toString name, "\",\"", descToString desc, "\",",
164 : jhr 2356 expToString dflt, ");\n"
165 :     ])
166 : jhr 3060 | IL.S_InputNrrd(x, name, desc, NONE) =>
167 :     prln (out, [
168 :     Var.name x, " = input-nrrd<", Ty.toString(Var.ty x), "> (\"",
169 :     String.toString name, "\",\"", descToString desc, "\");\n"
170 :     ])
171 :     | IL.S_InputNrrd(x, name, desc, SOME dflt) =>
172 :     prln (out, [
173 :     Var.name x, " = input-nrrd<", Ty.toString(Var.ty x), "> (\"",
174 :     String.toString name, "\",\"", descToString desc, "\",\"",
175 :     String.toString dflt, "\");\n"
176 :     ])
177 : jhr 2356 | IL.S_Exit es => prln (out, [argsToString("exit", es), ";\n"])
178 :     (* return functions for methods *)
179 :     | IL.S_Active => prln (out, ["active;\n"])
180 :     | IL.S_Stabilize => prln (out, ["stabilize;\n"])
181 :     | IL.S_Die => prln (out, ["die;\n"])
182 : cchiw 2827 | IL.S_StoreVec(x,offset,A,isFill, oSize, ty,pieces, args) =>
183 : jhr 3060 prln (out, [
184 :     "StoreVectoArr1<", expToString x, " = _offset_", itos offset,
185 : cchiw 3544 "|", itos oSize, "_Ty:",Ty.toString ty, "@ [", "] >\n"
186 : jhr 3060 ])
187 :     | IL.S_Cons(x,oSize, args) => prln(out,["ConsStmt", Var.name x,"<" ,itos oSize,">\n"])
188 :     (* end case *))
189 : cchiw 2668
190 : jhr 1115 fun statement (outS, stm) = ppStm((outS, 0), stm)
191 :    
192 :     fun block (outS, blk) = (ppBlock ((outS, 0), blk); pr ((outS, 0), "\n"))
193 :    
194 : jhr 1151 fun ppInitially (out, {isArray, iterPrefix, iters, createPrefix, strand, args}) = let
195 : jhr 2356 fun ppBlock (out, IL.Block{locals, body}, inside) = let
196 :     val out' = incIndent out
197 :     in
198 :     pr (out, "{\n");
199 :     List.app (ppVarDecl out') locals;
200 :     List.app (fn stm => ppStm(out', stm)) body;
201 :     inside out';
202 :     indent out; pr (out, "}")
203 :     end
204 :     fun ppCreate out = let
205 :     fun ppNew out = prln(out, [argsToString("new "^Atom.toString strand, args), ";\n"])
206 :     in
207 :     indent out; ppBlock (out, createPrefix, ppNew); pr(out, "\n")
208 :     end
209 :     fun ppIters out = let
210 :     fun ppIter (out, []) = ppCreate out
211 :     | ppIter (out, (i, lo, hi)::iters) = (
212 :     prln(out, ["for ", Var.name i, " = ", expToString lo, " .. ", expToString hi, "\n"]);
213 :     ppIter (incIndent out, iters))
214 :     in
215 :     ppIter (out, iters)
216 :     end
217 :     in
218 :     indent out; pr(out, if isArray then "ARRAY " else "COLLECTION ");
219 :     ppBlock (out, iterPrefix, ppIters);
220 :     pr (out, "\n")
221 :     end
222 : jhr 1151
223 : jhr 3060 fun program (outS, IL.Program{props, types, operations, globals, inputInit, globalInit, strands, initially}) = let
224 : jhr 2356 val out = (outS, 0)
225 :     val out' = incIndent out
226 :     in
227 :     pr(out, "//***** PROPERTIES *****\n");
228 :     case props
229 :     of [] => prln(out', ["none\n"])
230 :     | _ => prln(out', [String.concatWith " " (List.map StrandUtil.propToString props), "\n"])
231 :     (* end case *);
232 :     prln(out, ["//***** GLOBALS *****\n"]);
233 :     List.app (ppVarDecl out') globals;
234 :     prln(out, ["//***** INPUT INIT *****\n"]);
235 :     indent out'; ppBlock (out', inputInit); pr (out, "\n");
236 :     prln(out, ["//***** GLOBAL INIT *****\n"]);
237 :     indent out'; ppBlock (out', globalInit); pr (out, "\n");
238 :     prln(out, ["//***** STRANDS *****\n"]);
239 :     List.app (ppStrand out) strands;
240 :     prln(out, ["//***** INITIALLY *****\n"]);
241 :     ppInitially (out', initially)
242 :     end
243 : jhr 1115
244 :     end

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