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 548 - (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 :     fun ppVarDecl out x = prln (out, [Ty.toString(Var.ty x), " ", Var.name x, ";\n"])
44 :    
45 :     fun ppStrand out (IL.Strand{name, params, state, stateInit, methods}) = let
46 :     val out' = incIndent out
47 :     fun ppParams [] = ()
48 :     | ppParams [x] = prl(out, [Ty.toString (Var.ty x), " ", Var.name x])
49 :     | ppParams (x::r) = (
50 :     prl(out, [Ty.toString (Var.ty x), " ", Var.name x, ","]);
51 : jhr 539 ppParams r)
52 : jhr 535 in
53 : jhr 541 prln (out, ["strand ", Atom.toString name, " ("]);
54 : jhr 535 ppParams params; pr(out, ") {\n");
55 :     List.app (ppVarDecl out') state;
56 : jhr 539 prln (out', ["init () "]); ppBlock(out', stateInit); pr (out', "\n");
57 : jhr 535 List.app (ppMethod out') methods;
58 :     prln (out, ["}\n"])
59 :     end
60 :    
61 :     and ppMethod out (IL.Method{name, body}) = (
62 : jhr 539 prln (out, [Atom.toString name, " () "]);
63 : jhr 535 ppBlock (out, body);
64 :     pr (out, "\n"))
65 :    
66 :     and ppBlock (out, IL.Block{locals, body}) = let
67 :     val out' = incIndent out
68 :     in
69 :     pr (out, "{\n");
70 :     List.app (ppVarDecl out') locals;
71 :     List.app (fn stm => ppStm(out', stm)) body;
72 :     indent out; pr (out, "}")
73 :     end
74 :    
75 :     and ppStm (out, stm) = (case stm
76 :     of IL.S_Comment text => let
77 :     val out = decIndent out
78 :     in
79 :     List.app (fn s => prln(out, ["// ", s, "\n"])) text
80 :     end
81 :     | IL.S_Assign(x, e) => prln(out, [Var.name x, " = ", expToString e, ";\n"])
82 : jhr 543 | IL.S_Cons(x, args) => let
83 :     fun mkStrings [] = []
84 :     | mkStrings [e] = [expToString e]
85 :     | mkStrings (e::es) = (expToString e ^ ",") :: mkStrings es
86 :     val args = mkStrings args
87 :     in
88 :     if (List.foldl (fn (s, n) => n+size s) 0 args > 60)
89 :     then let
90 :     val out' = incIndent out
91 :     val out'' = incIndent out'
92 :     fun prArgs (_, []) = (
93 :     pr (out', "\n");
94 :     prln (out', ["};\n"]))
95 :     | prArgs (n, arg::args) =
96 :     if (n = 0)
97 :     then (
98 :     prln (out'', [arg]);
99 :     prArgs (n + size arg, args))
100 :     else if (n + size arg > 60)
101 :     then (
102 :     pr(out'', "\n");
103 :     prArgs (0, arg::args))
104 :     else (
105 :     pr(out'', " ");
106 :     pr(out'', arg);
107 :     prArgs (n + size arg + 1, args))
108 :     in
109 :     prln (out, [Var.name x, " = {\n"]);
110 :     prArgs (0, args)
111 :     end
112 :     else (
113 :     prln (out, Var.name x :: " = {" :: args);
114 :     pr (out, "};\n"))
115 : jhr 548 end
116 :     | IL.S_LoadVoxels(x, dim, exp) =>
117 :     prln (out, [
118 :     Var.name x, " = loadVoxels<",
119 :     Int.toString dim, "> (", expToString exp, ");\n"
120 :     ])
121 : jhr 535 | IL.S_LoadImage(x, dim, exp) =>
122 :     prln (out, [
123 :     Var.name x, " = load<", Int.toString dim, "> (", expToString exp, ");\n"
124 :     ])
125 :     | IL.S_Input(x, name, NONE) =>
126 :     prln (out, [
127 :     Var.name x, " = input<", Ty.toString(Var.ty x), "> (\"",
128 :     String.toString name, "\");\n"
129 :     ])
130 :     | IL.S_Input(x, name, SOME dflt) =>
131 :     prln (out, [
132 :     Var.name x, " = input<", Ty.toString(Var.ty x), "> (\"",
133 :     String.toString name, "\",", expToString dflt, ");\n"
134 :     ])
135 :     | IL.S_IfThen(cond, blk) => (
136 :     prln (out, ["if (", expToString cond, ") "]);
137 :     ppBlock (out, blk);
138 :     pr (out, "\n"))
139 :     | IL.S_IfThenElse(cond, blk1, blk2) => (
140 :     prln (out, ["if (", expToString cond, ") "]);
141 :     ppBlock (out, blk1);
142 :     pr (out, " else ");
143 :     ppBlock (out, blk2);
144 :     pr (out, "\n"))
145 :     | IL.S_Die => prln (out, ["die;\n"])
146 :     | IL.S_Stabilize => prln (out, ["stabilize;\n"])
147 :     (* end case *))
148 :    
149 :     fun block (outS, blk) = (ppBlock ((outS, 0), blk); pr ((outS, 0), "\n"))
150 :    
151 :     fun program (outS, IL.Program{globals, globalInit, strands}) = let
152 :     val out = (outS, 0)
153 :     val out' = incIndent out
154 :     in
155 :     prln(out, ["//***** GLOBALS *****\n"]);
156 :     List.app (ppVarDecl out') globals;
157 :     prln(out, ["//***** GLOBAL INIT *****\n"]);
158 :     indent out'; ppBlock (out', globalInit); pr (out, "\n");
159 :     prln(out, ["//***** STRANDS *****\n"]);
160 :     List.app (ppStrand out) strands
161 :     end
162 :    
163 :     end

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