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 2668 - (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 2668 | toS (IL.E_LoadVec(A,tyNew, tyOrig, arg, offset), l) =
53 : jhr 2632 "LdVec<" :: itos tyNew :: ":" :: itos tyOrig :: ">("
54 :     :: argsToS (",", "(", [arg, offset], ")", l)
55 : cchiw 2668 | toS (IL.E_mkVec(A,tyNew, tyOrig, indexAt,pieces, args), l) =let
56 :     val m = argsToS (",\n\t","(", [args], ")", l)
57 : jhr 2632 in
58 :     "mkVec<" ::itos tyNew :: ":" :: itos tyOrig ::
59 :     ">\n\t" :: m
60 :     end
61 : cchiw 2668 in
62 : jhr 2356 String.concat (toS (e, []))
63 :     end
64 : jhr 1115
65 :     fun argsToString (prefix, es) = String.concat[
66 : jhr 2356 prefix, "(", String.concatWith "," (List.map expToString es), ")"
67 :     ]
68 : jhr 1115
69 :     fun ppVarDecl out x = prln (out, [Ty.toString(Var.ty x), " ", Var.name x, ";\n"])
70 :    
71 :     fun ppStrand out (IL.Strand{name, params, state, stateInit, methods}) = let
72 : jhr 2356 val out' = incIndent out
73 :     fun ppParams [] = ()
74 :     | ppParams [x] = prl(out, [Ty.toString (Var.ty x), " ", Var.name x])
75 :     | ppParams (x::r) = (
76 :     prl(out, [Ty.toString (Var.ty x), " ", Var.name x, ","]);
77 :     ppParams r)
78 :     fun ppSVarDecl (IL.SV{varying, output, name, ty, ...}) = let
79 :     val v = if varying then "varying " else ""
80 :     val out = if output then "output " else ""
81 :     in
82 :     prln (out', [v, out, Ty.toString ty, " ", name, ";\n"])
83 :     end
84 :     in
85 :     prln (out, ["strand ", Atom.toString name, " ("]);
86 :     ppParams params; pr(out, ") {\n");
87 :     List.app ppSVarDecl state;
88 :     prln (out', ["init () "]); ppBlock(out', stateInit); pr (out', "\n");
89 :     List.app (ppMethod out') methods;
90 :     prln (out, ["}\n"])
91 :     end
92 : jhr 1115
93 :     and ppMethod out (IL.Method{name, body}) = (
94 : jhr 2356 prln (out, [StrandUtil.nameToString name, " () "]);
95 :     ppBlock (out, body);
96 :     pr (out, "\n"))
97 : jhr 1115
98 :     and ppBlock (out, IL.Block{locals, body}) = let
99 : jhr 2356 val out' = incIndent out
100 :     in
101 :     pr (out, "{\n");
102 :     List.app (ppVarDecl out') locals;
103 :     List.app (fn stm => ppStm(out', stm)) body;
104 :     indent out; pr (out, "}")
105 :     end
106 : jhr 1115
107 :     and ppStm (out, stm) = (case stm
108 : jhr 2356 of IL.S_Comment text => let
109 :     val out = decIndent out
110 :     in
111 :     List.app (fn s => prln(out, ["// ", s, "\n"])) text
112 :     end
113 :     | IL.S_Assign([], e) => prln(out, [expToString e, ";\n"])
114 :     | IL.S_Assign([x], e) => prln(out, [Var.name x, " = ", expToString e, ";\n"])
115 :     | IL.S_Assign(x::xs, e) => (
116 : jhr 1640 prln(out, ["(", Var.name x]);
117 :     List.app (fn x => prl(out, [",", Var.name x])) xs;
118 :     prl (out, [") = ", expToString e, ";\n"]))
119 : jhr 2356 | IL.S_IfThen(cond, blk) => (
120 :     prln (out, ["if (", expToString cond, ") "]);
121 :     ppBlock (out, blk);
122 :     pr (out, "\n"))
123 :     | IL.S_IfThenElse(cond, blk1, blk2) => (
124 :     prln (out, ["if (", expToString cond, ") "]);
125 :     ppBlock (out, blk1);
126 :     pr (out, " else ");
127 :     ppBlock (out, blk2);
128 :     pr (out, "\n"))
129 : jhr 1115 (*
130 : jhr 2356 | IL.S_For(x, e1, e2, blk) => (
131 :     prln (out, [
132 :     "for (", Ty.toString(Var.ty x), " ", Var.name x, " = ",
133 :     expToString e1, " ..", expToString e2, ") "
134 :     ]);
135 :     ppBlock (out, blk);
136 :     pr (out, "\n"))
137 : jhr 1115 *)
138 : jhr 2356 | IL.S_New(strand, args) =>
139 :     prln (out, [argsToString("new "^Atom.toString strand, args), ";\n"])
140 : jhr 1640 | IL.S_Save([x], rhs) =>
141 :     prln (out, [IL.stateVarToString x, " = ", expToString rhs, ";\n"])
142 : jhr 2356 | IL.S_Save(x::xs, e) => (
143 : jhr 1640 prln(out, ["(", IL.stateVarToString x]);
144 :     List.app (fn x => prl(out, [",", IL.stateVarToString x])) xs;
145 :     prl (out, [") = ", expToString e, ";\n"]))
146 : jhr 2356 | IL.S_LoadImage(x, dim, exp) =>
147 :     prln (out, [
148 :     Var.name x, " = load<", Int.toString dim, "> (", expToString exp, ");\n"
149 :     ])
150 :     | IL.S_Input(x, name, desc, NONE) =>
151 :     prln (out, [
152 :     Var.name x, " = input<", Ty.toString(Var.ty x), "> (\"",
153 :     String.toString name, "\",\"", String.toString desc, "\");\n"
154 :     ])
155 :     | IL.S_Input(x, name, desc, SOME dflt) =>
156 :     prln (out, [
157 :     Var.name x, " = input<", Ty.toString(Var.ty x), "> (\"",
158 :     String.toString name, "\",\"", String.toString desc, "\",",
159 :     expToString dflt, ");\n"
160 :     ])
161 :     | IL.S_Exit es => prln (out, [argsToString("exit", es), ";\n"])
162 :     (* return functions for methods *)
163 :     | IL.S_Active => prln (out, ["active;\n"])
164 :     | IL.S_Stabilize => prln (out, ["stabilize;\n"])
165 :     | IL.S_Die => prln (out, ["die;\n"])
166 : cchiw 2668
167 : jhr 2356 (* end case *))
168 : jhr 1115
169 :     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