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

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