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

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