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

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