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 2690 - (view) (download)

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

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