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 2663 - (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 : jhr 2632 | toS (IL.E_LoadVec(tyNew, tyOrig, arg, offset), l) =
53 :     "LdVec<" :: itos tyNew :: ":" :: itos tyOrig :: ">("
54 :     :: argsToS (",", "(", [arg, offset], ")", l)
55 :     | toS (IL.E_mkVec(tyNew, tyOrig, pieces, args), l) =let
56 :     val m = argsToS (",\n\t","(", args, ")", l)
57 :     in
58 :     "mkVec<" ::itos tyNew :: ":" :: itos tyOrig ::
59 :     ">\n\t" :: m
60 :     end
61 : cchiw 2663 | toS (IL.E_LoadVecAligned(tyNew, tyOrig, arg, offset), l) =
62 :     "LdVecAligned<" :: itos tyNew :: ":" :: itos tyOrig :: ">("
63 : cchiw 2637 :: argsToS (",", "(", [arg, offset], ")", l)
64 : cchiw 2663 | toS (IL.E_mkVecAligned(tyNew, tyOrig, pieces, args), l) =let
65 :     val m = argsToS (",\n\t","(", args, ")", l)
66 :     in
67 :     "mkVec<" ::itos tyNew :: ":" :: itos tyOrig ::
68 :     ">\n\t" :: m
69 :     end
70 : jhr 2356 in
71 :     String.concat (toS (e, []))
72 :     end
73 : jhr 1115
74 :     fun argsToString (prefix, es) = String.concat[
75 : jhr 2356 prefix, "(", String.concatWith "," (List.map expToString es), ")"
76 :     ]
77 : jhr 1115
78 :     fun ppVarDecl out x = prln (out, [Ty.toString(Var.ty x), " ", Var.name x, ";\n"])
79 :    
80 :     fun ppStrand out (IL.Strand{name, params, state, stateInit, methods}) = let
81 : jhr 2356 val out' = incIndent out
82 :     fun ppParams [] = ()
83 :     | ppParams [x] = prl(out, [Ty.toString (Var.ty x), " ", Var.name x])
84 :     | ppParams (x::r) = (
85 :     prl(out, [Ty.toString (Var.ty x), " ", Var.name x, ","]);
86 :     ppParams r)
87 :     fun ppSVarDecl (IL.SV{varying, output, name, ty, ...}) = let
88 :     val v = if varying then "varying " else ""
89 :     val out = if output then "output " else ""
90 :     in
91 :     prln (out', [v, out, Ty.toString ty, " ", name, ";\n"])
92 :     end
93 :     in
94 :     prln (out, ["strand ", Atom.toString name, " ("]);
95 :     ppParams params; pr(out, ") {\n");
96 :     List.app ppSVarDecl state;
97 :     prln (out', ["init () "]); ppBlock(out', stateInit); pr (out', "\n");
98 :     List.app (ppMethod out') methods;
99 :     prln (out, ["}\n"])
100 :     end
101 : jhr 1115
102 :     and ppMethod out (IL.Method{name, body}) = (
103 : jhr 2356 prln (out, [StrandUtil.nameToString name, " () "]);
104 :     ppBlock (out, body);
105 :     pr (out, "\n"))
106 : jhr 1115
107 :     and ppBlock (out, IL.Block{locals, body}) = let
108 : jhr 2356 val out' = incIndent out
109 :     in
110 :     pr (out, "{\n");
111 :     List.app (ppVarDecl out') locals;
112 :     List.app (fn stm => ppStm(out', stm)) body;
113 :     indent out; pr (out, "}")
114 :     end
115 : jhr 1115
116 :     and ppStm (out, stm) = (case stm
117 : jhr 2356 of IL.S_Comment text => let
118 :     val out = decIndent out
119 :     in
120 :     List.app (fn s => prln(out, ["// ", s, "\n"])) text
121 :     end
122 :     | IL.S_Assign([], e) => prln(out, [expToString e, ";\n"])
123 :     | IL.S_Assign([x], e) => prln(out, [Var.name x, " = ", expToString e, ";\n"])
124 :     | IL.S_Assign(x::xs, e) => (
125 : jhr 1640 prln(out, ["(", Var.name x]);
126 :     List.app (fn x => prl(out, [",", Var.name x])) xs;
127 :     prl (out, [") = ", expToString e, ";\n"]))
128 : jhr 2356 | IL.S_IfThen(cond, blk) => (
129 :     prln (out, ["if (", expToString cond, ") "]);
130 :     ppBlock (out, blk);
131 :     pr (out, "\n"))
132 :     | IL.S_IfThenElse(cond, blk1, blk2) => (
133 :     prln (out, ["if (", expToString cond, ") "]);
134 :     ppBlock (out, blk1);
135 :     pr (out, " else ");
136 :     ppBlock (out, blk2);
137 :     pr (out, "\n"))
138 : jhr 1115 (*
139 : jhr 2356 | IL.S_For(x, e1, e2, blk) => (
140 :     prln (out, [
141 :     "for (", Ty.toString(Var.ty x), " ", Var.name x, " = ",
142 :     expToString e1, " ..", expToString e2, ") "
143 :     ]);
144 :     ppBlock (out, blk);
145 :     pr (out, "\n"))
146 : jhr 1115 *)
147 : jhr 2356 | IL.S_New(strand, args) =>
148 :     prln (out, [argsToString("new "^Atom.toString strand, args), ";\n"])
149 : jhr 1640 | IL.S_Save([x], rhs) =>
150 :     prln (out, [IL.stateVarToString x, " = ", expToString rhs, ";\n"])
151 : jhr 2356 | IL.S_Save(x::xs, e) => (
152 : jhr 1640 prln(out, ["(", IL.stateVarToString x]);
153 :     List.app (fn x => prl(out, [",", IL.stateVarToString x])) xs;
154 :     prl (out, [") = ", expToString e, ";\n"]))
155 : jhr 2356 | IL.S_LoadImage(x, dim, exp) =>
156 :     prln (out, [
157 :     Var.name x, " = load<", Int.toString dim, "> (", expToString exp, ");\n"
158 :     ])
159 :     | IL.S_Input(x, name, desc, NONE) =>
160 :     prln (out, [
161 :     Var.name x, " = input<", Ty.toString(Var.ty x), "> (\"",
162 :     String.toString name, "\",\"", String.toString desc, "\");\n"
163 :     ])
164 :     | IL.S_Input(x, name, desc, SOME dflt) =>
165 :     prln (out, [
166 :     Var.name x, " = input<", Ty.toString(Var.ty x), "> (\"",
167 :     String.toString name, "\",\"", String.toString desc, "\",",
168 :     expToString dflt, ");\n"
169 :     ])
170 :     | IL.S_Exit es => prln (out, [argsToString("exit", es), ";\n"])
171 :     (* return functions for methods *)
172 :     | IL.S_Active => prln (out, ["active;\n"])
173 :     | IL.S_Stabilize => prln (out, ["stabilize;\n"])
174 :     | IL.S_Die => prln (out, ["die;\n"])
175 :     (* end case *))
176 : jhr 1115
177 :     fun statement (outS, stm) = ppStm((outS, 0), stm)
178 :    
179 :     fun block (outS, blk) = (ppBlock ((outS, 0), blk); pr ((outS, 0), "\n"))
180 :    
181 : jhr 1151 fun ppInitially (out, {isArray, iterPrefix, iters, createPrefix, strand, args}) = let
182 : jhr 2356 fun ppBlock (out, IL.Block{locals, body}, inside) = let
183 :     val out' = incIndent out
184 :     in
185 :     pr (out, "{\n");
186 :     List.app (ppVarDecl out') locals;
187 :     List.app (fn stm => ppStm(out', stm)) body;
188 :     inside out';
189 :     indent out; pr (out, "}")
190 :     end
191 :     fun ppCreate out = let
192 :     fun ppNew out = prln(out, [argsToString("new "^Atom.toString strand, args), ";\n"])
193 :     in
194 :     indent out; ppBlock (out, createPrefix, ppNew); pr(out, "\n")
195 :     end
196 :     fun ppIters out = let
197 :     fun ppIter (out, []) = ppCreate out
198 :     | ppIter (out, (i, lo, hi)::iters) = (
199 :     prln(out, ["for ", Var.name i, " = ", expToString lo, " .. ", expToString hi, "\n"]);
200 :     ppIter (incIndent out, iters))
201 :     in
202 :     ppIter (out, iters)
203 :     end
204 :     in
205 :     indent out; pr(out, if isArray then "ARRAY " else "COLLECTION ");
206 :     ppBlock (out, iterPrefix, ppIters);
207 :     pr (out, "\n")
208 :     end
209 : jhr 1151
210 : cchiw 2637 fun program (outS, IL.Program{props,types,oprations, globals, inputInit, globalInit, strands, initially}) = let
211 : jhr 2356 val out = (outS, 0)
212 :     val out' = incIndent out
213 :     in
214 :     pr(out, "//***** PROPERTIES *****\n");
215 :     case props
216 :     of [] => prln(out', ["none\n"])
217 :     | _ => prln(out', [String.concatWith " " (List.map StrandUtil.propToString props), "\n"])
218 :     (* end case *);
219 :     prln(out, ["//***** GLOBALS *****\n"]);
220 :     List.app (ppVarDecl out') globals;
221 :     prln(out, ["//***** INPUT INIT *****\n"]);
222 :     indent out'; ppBlock (out', inputInit); pr (out, "\n");
223 :     prln(out, ["//***** GLOBAL INIT *****\n"]);
224 :     indent out'; ppBlock (out', globalInit); pr (out, "\n");
225 :     prln(out, ["//***** STRANDS *****\n"]);
226 :     List.app (ppStrand out) strands;
227 :     prln(out, ["//***** INITIALLY *****\n"]);
228 :     ppInitially (out', initially)
229 :     end
230 : jhr 1115
231 :     end

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