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

SCM Repository

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

Annotation of /branches/pure-cfg/src/compiler/codegen/tree-il-pp.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 563 - (view) (download)

1 : jhr 535 (* 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 block : TextIO.outstream * TreeIL.block -> unit
12 :    
13 :     val program : TextIO.outstream * TreeIL.program -> unit
14 :    
15 :     end = struct
16 :    
17 :     structure IL = TreeIL
18 :     structure Op = IL.Op
19 :     structure Var = IL.Var
20 :     structure Ty = IL.Ty
21 :    
22 :     fun indent (outS, i) = TextIO.output(outS, StringCvt.padLeft #" " i "")
23 :     fun incIndent (outS, i) = (outS, i+2)
24 :     fun decIndent (outS, i) = (outS, Int.max(0, i-2))
25 :     fun pr ((outS, _), s) = TextIO.output(outS, s)
26 :     fun prl (out, l) = pr(out, concat l)
27 :     fun prln (out, l) = (indent out; prl(out, l))
28 :    
29 :     fun expToString e = let
30 :     fun toS (IL.E_Var x, l) = Var.name x :: l
31 :     | toS (IL.E_Lit lit, l) = Literal.toString lit :: l
32 :     | toS (IL.E_Op(rator, args), l) = let
33 :     fun argToS ([], l) = l
34 :     | argToS ([e], l) = toS (e, l)
35 :     | argToS (e::es, l) = toS(e, "," :: argToS(es, l))
36 :     in
37 :     Op.toString rator :: "(" :: argToS(args, ")" :: l)
38 :     end
39 :     in
40 :     String.concat (toS (e, []))
41 :     end
42 :    
43 : jhr 563 fun argsToString (prefix, es) = String.concat[
44 :     prefix, "(", String.concatWith "," (List.map expToString es), ")"
45 :     ]
46 :    
47 : jhr 535 fun ppVarDecl out x = prln (out, [Ty.toString(Var.ty x), " ", Var.name x, ";\n"])
48 :    
49 :     fun ppStrand out (IL.Strand{name, params, state, stateInit, methods}) = let
50 :     val out' = incIndent out
51 :     fun ppParams [] = ()
52 :     | ppParams [x] = prl(out, [Ty.toString (Var.ty x), " ", Var.name x])
53 :     | ppParams (x::r) = (
54 :     prl(out, [Ty.toString (Var.ty x), " ", Var.name x, ","]);
55 : jhr 539 ppParams r)
56 : jhr 535 in
57 : jhr 541 prln (out, ["strand ", Atom.toString name, " ("]);
58 : jhr 535 ppParams params; pr(out, ") {\n");
59 :     List.app (ppVarDecl out') state;
60 : jhr 539 prln (out', ["init () "]); ppBlock(out', stateInit); pr (out', "\n");
61 : jhr 535 List.app (ppMethod out') methods;
62 :     prln (out, ["}\n"])
63 :     end
64 :    
65 :     and ppMethod out (IL.Method{name, body}) = (
66 : jhr 539 prln (out, [Atom.toString name, " () "]);
67 : jhr 535 ppBlock (out, body);
68 :     pr (out, "\n"))
69 :    
70 :     and ppBlock (out, IL.Block{locals, body}) = let
71 :     val out' = incIndent out
72 :     in
73 :     pr (out, "{\n");
74 :     List.app (ppVarDecl out') locals;
75 :     List.app (fn stm => ppStm(out', stm)) body;
76 :     indent out; pr (out, "}")
77 :     end
78 :    
79 :     and ppStm (out, stm) = (case stm
80 :     of IL.S_Comment text => let
81 :     val out = decIndent out
82 :     in
83 :     List.app (fn s => prln(out, ["// ", s, "\n"])) text
84 :     end
85 :     | IL.S_Assign(x, e) => prln(out, [Var.name x, " = ", expToString e, ";\n"])
86 : jhr 543 | IL.S_Cons(x, args) => let
87 :     fun mkStrings [] = []
88 :     | mkStrings [e] = [expToString e]
89 :     | mkStrings (e::es) = (expToString e ^ ",") :: mkStrings es
90 :     val args = mkStrings args
91 :     in
92 :     if (List.foldl (fn (s, n) => n+size s) 0 args > 60)
93 :     then let
94 :     val out' = incIndent out
95 :     val out'' = incIndent out'
96 :     fun prArgs (_, []) = (
97 :     pr (out', "\n");
98 :     prln (out', ["};\n"]))
99 :     | prArgs (n, arg::args) =
100 :     if (n = 0)
101 :     then (
102 :     prln (out'', [arg]);
103 :     prArgs (n + size arg, args))
104 :     else if (n + size arg > 60)
105 :     then (
106 :     pr(out'', "\n");
107 :     prArgs (0, arg::args))
108 :     else (
109 :     pr(out'', " ");
110 :     pr(out'', arg);
111 :     prArgs (n + size arg + 1, args))
112 :     in
113 :     prln (out, [Var.name x, " = {\n"]);
114 :     prArgs (0, args)
115 :     end
116 :     else (
117 :     prln (out, Var.name x :: " = {" :: args);
118 :     pr (out, "};\n"))
119 : jhr 548 end
120 :     | IL.S_LoadVoxels(x, dim, exp) =>
121 :     prln (out, [
122 :     Var.name x, " = loadVoxels<",
123 :     Int.toString dim, "> (", expToString exp, ");\n"
124 :     ])
125 : jhr 535 | IL.S_LoadImage(x, dim, exp) =>
126 :     prln (out, [
127 :     Var.name x, " = load<", Int.toString dim, "> (", expToString exp, ");\n"
128 :     ])
129 :     | IL.S_Input(x, name, NONE) =>
130 :     prln (out, [
131 :     Var.name x, " = input<", Ty.toString(Var.ty x), "> (\"",
132 :     String.toString name, "\");\n"
133 :     ])
134 :     | IL.S_Input(x, name, SOME dflt) =>
135 :     prln (out, [
136 :     Var.name x, " = input<", Ty.toString(Var.ty x), "> (\"",
137 :     String.toString name, "\",", expToString dflt, ");\n"
138 :     ])
139 :     | IL.S_IfThen(cond, blk) => (
140 :     prln (out, ["if (", expToString cond, ") "]);
141 :     ppBlock (out, blk);
142 :     pr (out, "\n"))
143 :     | IL.S_IfThenElse(cond, blk1, blk2) => (
144 :     prln (out, ["if (", expToString cond, ") "]);
145 :     ppBlock (out, blk1);
146 :     pr (out, " else ");
147 :     ppBlock (out, blk2);
148 :     pr (out, "\n"))
149 : jhr 563 | IL.S_Exit es => prln (out, [argsToString("exit", es), ";"])
150 :     (* return functions for methods *)
151 :     | IL.S_Active es => prln (out, [argsToString("active", es), ";"])
152 :     | IL.S_Stabilize es => prln (out, [argsToString("stabilize", es), ";"])
153 : jhr 535 | IL.S_Die => prln (out, ["die;\n"])
154 :     (* end case *))
155 :    
156 :     fun block (outS, blk) = (ppBlock ((outS, 0), blk); pr ((outS, 0), "\n"))
157 :    
158 :     fun program (outS, IL.Program{globals, globalInit, strands}) = let
159 :     val out = (outS, 0)
160 :     val out' = incIndent out
161 :     in
162 :     prln(out, ["//***** GLOBALS *****\n"]);
163 :     List.app (ppVarDecl out') globals;
164 :     prln(out, ["//***** GLOBAL INIT *****\n"]);
165 :     indent out'; ppBlock (out', globalInit); pr (out, "\n");
166 :     prln(out, ["//***** STRANDS *****\n"]);
167 :     List.app (ppStrand out) strands
168 :     end
169 :    
170 :     end

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