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 541 - (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 :     | IL.S_Cons(x, args) => raise Fail "Cons"
83 :     | IL.S_LoadImage(x, dim, exp) =>
84 :     prln (out, [
85 :     Var.name x, " = load<", Int.toString dim, "> (", expToString exp, ");\n"
86 :     ])
87 :     | IL.S_Input(x, name, NONE) =>
88 :     prln (out, [
89 :     Var.name x, " = input<", Ty.toString(Var.ty x), "> (\"",
90 :     String.toString name, "\");\n"
91 :     ])
92 :     | IL.S_Input(x, name, SOME dflt) =>
93 :     prln (out, [
94 :     Var.name x, " = input<", Ty.toString(Var.ty x), "> (\"",
95 :     String.toString name, "\",", expToString dflt, ");\n"
96 :     ])
97 :     | IL.S_IfThen(cond, blk) => (
98 :     prln (out, ["if (", expToString cond, ") "]);
99 :     ppBlock (out, blk);
100 :     pr (out, "\n"))
101 :     | IL.S_IfThenElse(cond, blk1, blk2) => (
102 :     prln (out, ["if (", expToString cond, ") "]);
103 :     ppBlock (out, blk1);
104 :     pr (out, " else ");
105 :     ppBlock (out, blk2);
106 :     pr (out, "\n"))
107 :     | IL.S_Die => prln (out, ["die;\n"])
108 :     | IL.S_Stabilize => prln (out, ["stabilize;\n"])
109 :     (* end case *))
110 :    
111 :     fun block (outS, blk) = (ppBlock ((outS, 0), blk); pr ((outS, 0), "\n"))
112 :    
113 :     fun program (outS, IL.Program{globals, globalInit, strands}) = let
114 :     val out = (outS, 0)
115 :     val out' = incIndent out
116 :     in
117 :     prln(out, ["//***** GLOBALS *****\n"]);
118 :     List.app (ppVarDecl out') globals;
119 :     prln(out, ["//***** GLOBAL INIT *****\n"]);
120 :     indent out'; ppBlock (out', globalInit); pr (out, "\n");
121 :     prln(out, ["//***** STRANDS *****\n"]);
122 :     List.app (ppStrand out) strands
123 :     end
124 :    
125 :     end

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