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

SCM Repository

[diderot] Annotation of /trunk/src/compiler/simplify/simplify.sml
ViewVC logotype

Annotation of /trunk/src/compiler/simplify/simplify.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 185 - (view) (download)

1 : jhr 171 (* simplify.sml
2 :     *
3 :     * COPYRIGHT (c) 2010 The Diderot Project (http://diderot.cs.uchicago.edu)
4 :     * All rights reserved.
5 :     *
6 :     * Simplify the AST representation.
7 :     *)
8 :    
9 :     structure Simplify : sig
10 :    
11 :     val transform : AST.program -> Simple.program
12 :    
13 :     end = struct
14 :    
15 :     structure Ty = Types
16 :     structure S = Simple
17 :    
18 :     local
19 :     val tempName = Atom.atom "_t"
20 :     in
21 :     fun newTemp ty = Var.new (tempName, AST.LocalVar, ty)
22 :     end
23 :    
24 :     (* make a block out of a list of statements that are in reverse order *)
25 :     fun mkBlock stms = let
26 :     fun flatten [] = []
27 :     | flatten (S.S_Block stms :: r) = stms @ flatten r
28 :     | flatten (stm :: r) = stm :: flatten r
29 :     in
30 :     S.S_Block(flatten (List.rev stms))
31 :     end
32 :    
33 :     fun transform (AST.Program dcls) = let
34 :     val globals = ref []
35 :     val globalInit = ref []
36 :     val actors = ref []
37 :     fun simplifyDecl dcl = (case dcl
38 : jhr 185 of AST.D_Input(x, NONE) => let
39 :     val t = newTemp Ty.T_String
40 :     val stm = S.S_Assign(t, S.E_Lit(Literal.String(Var.nameOf x)))
41 :     val ty = Var.monoTypeOf x
42 :     val e' = S.E_Apply(BasisVars.input,
43 :     [Ty.TYPE(MetaVar.newFromType ty)], [t], ty)
44 :     in
45 :     globals := x :: !globals;
46 :     globalInit := S.S_Assign(x, e') :: stm :: !globalInit
47 :     end
48 : jhr 171 | AST.D_Input(x, SOME e) => let
49 : jhr 179 val (stms, x') = simplifyExpToVar (e, [])
50 :     val t = newTemp Ty.T_String
51 :     val stm = S.S_Assign(t, S.E_Lit(Literal.String(Var.nameOf x)))
52 :     val ty = Var.monoTypeOf x
53 : jhr 185 val e' = S.E_Apply(BasisVars.optInput,
54 : jhr 179 [Ty.TYPE(MetaVar.newFromType ty)], [t, x'], ty)
55 : jhr 171 in
56 :     globals := x :: !globals;
57 : jhr 185 globalInit := S.S_Assign(x, e') :: stm :: (stms @ !globalInit)
58 : jhr 171 end
59 :     | AST.D_Var(AST.VD_Decl(x, e)) => let
60 :     val (stms, e') = simplifyExp (e, [])
61 :     in
62 :     globals := x :: !globals;
63 :     globalInit := S.S_Assign(x, e') :: (stms @ !globalInit)
64 :     end
65 :     | AST.D_Actor info => actors := simplifyActor info :: !actors
66 :     | AST.D_InitialArray(e, iters) => () (* FIXME *)
67 :     | AST.D_InitialCollection(e, iters) => () (* FIXME *)
68 :     (* end case *))
69 :     in
70 :     List.app simplifyDecl dcls;
71 : jhr 175 S.Program{
72 : jhr 171 globals = List.rev(!globals),
73 :     globalInit = mkBlock (!globalInit),
74 :     actors = List.rev(!actors)
75 :     }
76 :     end
77 :    
78 :     and simplifyActor {name, params, state, methods} = let
79 :     fun simplifyState ([], xs, stms) = (List.rev xs, mkBlock stms)
80 :     | simplifyState (AST.VD_Decl(x, e) :: r, xs, stms) = let
81 :     val (stms, e') = simplifyExp (e, stms)
82 :     in
83 :     simplifyState (r, x::xs, S.S_Assign(x, e') :: stms)
84 :     end
85 :     val (xs, stm) = simplifyState (state, [], [])
86 :     in
87 :     S.Actor{
88 : jhr 173 name = name,
89 : jhr 171 params = params,
90 :     state = xs, stateInit = stm,
91 :     methods = List.map simplifyMethod methods
92 :     }
93 :     end
94 :    
95 :     and simplifyMethod (AST.M_Method(name, body)) =
96 :     S.M_Method(name, simplifyBlock body)
97 :    
98 :     (* simplify a statement into a single statement (i.e., a block if it expands into more
99 :     * than one new statement.
100 :     *)
101 :     and simplifyBlock stm = mkBlock (simplifyStmt (stm, []))
102 :    
103 :     and simplifyStmt (stm, stms) = (case stm
104 :     of AST.S_Block body => let
105 :     fun simplify ([], stms) = stms
106 :     | simplify (stm::r, stms) = simplify (r, simplifyStmt (stm, stms))
107 :     in
108 :     simplify (body, stms)
109 :     end
110 :     | AST.S_Decl(AST.VD_Decl(x, e)) => let
111 :     val (stms, e') = simplifyExp (e, stms)
112 :     in
113 :     S.S_Assign(x, e') :: stms
114 :     end
115 :     | AST.S_IfThenElse(e, s1, s2) => let
116 :     val (stms, x) = simplifyExpToVar (e, stms)
117 :     val s1 = simplifyBlock s1
118 :     val s2 = simplifyBlock s2
119 :     in
120 :     S.S_IfThenElse(x, s1, s2) :: stms
121 :     end
122 :     | AST.S_Assign(x, e) => let
123 :     val (stms, e') = simplifyExp (e, stms)
124 :     in
125 :     S.S_Assign(x, e') :: stms
126 :     end
127 :     | AST.S_New(name, args) => let
128 :     val (stms, xs) = simplifyExpsToVars (args, stms)
129 :     in
130 :     S.S_New(name, xs) :: stms
131 :     end
132 :     | AST.S_Die => S.S_Die :: stms
133 :     | AST.S_Stabilize => S.S_Stabilize :: stms
134 :     (* end case *))
135 :    
136 :     and simplifyExp (exp, stms) = (
137 :     case exp
138 :     of AST.E_Var x => (stms, S.E_Var x)
139 :     | AST.E_Lit lit => (stms, S.E_Lit lit)
140 :     | AST.E_Tuple es => raise Fail "E_Tuple not yet implemented"
141 :     | AST.E_Apply(f, tyArgs, args, ty) => let
142 :     val (stms, xs) = simplifyExpsToVars (args, stms)
143 :     in
144 :     (stms, S.E_Apply(f, tyArgs, xs, ty))
145 :     end
146 :     | AST.E_Cons es => let
147 :     val (stms, xs) = simplifyExpsToVars (es, stms)
148 :     in
149 :     (stms, S.E_Cons xs)
150 :     end
151 :     | AST.E_Cond(e1, e2, e3) => let
152 :     (* a conditional expression gets turned into an if-then-else statememt *)
153 :     val result = newTemp Ty.T_Bool
154 :     val (stms, x) = simplifyExpToVar (e1, stms)
155 :     fun simplifyBranch e = let
156 :     val (stms, e) = simplifyExp (e, [])
157 :     in
158 :     mkBlock (S.S_Assign(result, e)::stms)
159 :     end
160 :     val s1 = simplifyBranch e1
161 :     val s2 = simplifyBranch e2
162 :     in
163 :     (S.S_IfThenElse(x, s1, s2) :: stms, S.E_Var result)
164 :     end
165 :     (* end case *))
166 :    
167 :     and simplifyExpToVar (exp, stms) = let
168 :     val (stms, e) = simplifyExp (exp, stms)
169 :     in
170 :     case e
171 :     of S.E_Var x => (stms, x)
172 :     | _ => let
173 :     val x = newTemp (S.typeOf e)
174 :     in
175 :     (S.S_Assign(x, e)::stms, x)
176 :     end
177 :     (* end case *)
178 :     end
179 :    
180 :     and simplifyExpsToVars (exps, stms) = let
181 :     fun f ([], xs, stms) = (stms, List.rev xs)
182 :     | f (e::es, xs, stms) = let
183 :     val (stms, x) = simplifyExpToVar (e, stms)
184 :     in
185 :     f (es, x::xs, stms)
186 :     end
187 :     in
188 :     f (exps, [], stms)
189 :     end
190 :    
191 :     end

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