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

SCM Repository

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

Annotation of /branches/vis12/src/compiler/simplify/simplify.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1687 - (view) (download)

1 : jhr 171 (* simplify.sml
2 :     *
3 : jhr 435 * COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu)
4 : jhr 171 * All rights reserved.
5 :     *
6 :     * Simplify the AST representation.
7 :     *)
8 :    
9 :     structure Simplify : sig
10 :    
11 : jhr 1140 val transform : Error.err_stream * AST.program -> Simple.program
12 : jhr 171
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 : jhr 227 fun simplifyProgram (AST.Program dcls) = let
28 : jhr 171 val globals = ref []
29 :     val globalInit = ref []
30 : jhr 1116 val initially = ref NONE
31 : jhr 511 val strands = ref []
32 : jhr 1116 fun setInitially init = (case !initially
33 :     of NONE => initially := SOME init
34 :     (* FIXME: the check for multiple initially decls should happen in type checking *)
35 :     | SOME _ => raise Fail "multiple initially declarations"
36 :     (* end case *))
37 : jhr 171 fun simplifyDecl dcl = (case dcl
38 : jhr 1301 of AST.D_Input(x, desc, NONE) => let
39 :     val e' = S.E_Input(Var.monoTypeOf x, Var.nameOf x, desc, NONE)
40 : jhr 185 in
41 :     globals := x :: !globals;
42 : jhr 221 globalInit := S.S_Assign(x, e') :: !globalInit
43 : jhr 185 end
44 : jhr 1301 | AST.D_Input(x, desc, SOME e) => let
45 : jhr 179 val (stms, x') = simplifyExpToVar (e, [])
46 : jhr 1301 val e' = S.E_Input(Var.monoTypeOf x, Var.nameOf x, desc, SOME x')
47 : jhr 171 in
48 :     globals := x :: !globals;
49 : jhr 221 globalInit := S.S_Assign(x, e') :: (stms @ !globalInit)
50 : jhr 171 end
51 :     | AST.D_Var(AST.VD_Decl(x, e)) => let
52 :     val (stms, e') = simplifyExp (e, [])
53 :     in
54 :     globals := x :: !globals;
55 :     globalInit := S.S_Assign(x, e') :: (stms @ !globalInit)
56 :     end
57 : jhr 511 | AST.D_Strand info => strands := simplifyStrand info :: !strands
58 : jhr 1116 | AST.D_InitialArray(creat, iters) =>
59 :     setInitially (simplifyInit(true, creat, iters))
60 :     | AST.D_InitialCollection(creat, iters) =>
61 :     setInitially (simplifyInit(false, creat, iters))
62 : jhr 171 (* 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 : jhr 1116 init = (case !initially
69 :     (* FIXME: the check for the initially block should really happen in typechecking *)
70 :     of NONE => raise Fail "missing initially declaration"
71 :     | SOME blk => blk
72 :     (* end case *)),
73 : jhr 511 strands = List.rev(!strands)
74 : jhr 171 }
75 :     end
76 :    
77 : jhr 1116 and simplifyInit (isArray, AST.C_Create(strand, exps), iters) = let
78 :     val (stms, xs) = simplifyExpsToVars (exps, [])
79 :     val creat = S.C_Create{
80 :     argInit = mkBlock stms,
81 :     name = strand,
82 :     args = xs
83 :     }
84 :     fun simplifyIter (AST.I_Range(x, e1, e2), (iters, stms)) = let
85 :     val (stms, lo) = simplifyExpToVar (e1, stms)
86 :     val (stms, hi) = simplifyExpToVar (e2, stms)
87 :     in
88 :     ({param=x, lo=lo, hi=hi}::iters, stms)
89 :     end
90 :     val (iters, stms) = List.foldl simplifyIter ([], []) iters
91 :     in
92 :     S.Initially{
93 :     isArray = isArray,
94 :     rangeInit = mkBlock stms,
95 :     iters = List.rev iters,
96 :     create = creat
97 :     }
98 :     end
99 :    
100 : jhr 511 and simplifyStrand {name, params, state, methods} = let
101 : jhr 171 fun simplifyState ([], xs, stms) = (List.rev xs, mkBlock stms)
102 :     | simplifyState (AST.VD_Decl(x, e) :: r, xs, stms) = let
103 :     val (stms, e') = simplifyExp (e, stms)
104 :     in
105 :     simplifyState (r, x::xs, S.S_Assign(x, e') :: stms)
106 :     end
107 :     val (xs, stm) = simplifyState (state, [], [])
108 :     in
109 : jhr 511 S.Strand{
110 : jhr 173 name = name,
111 : jhr 171 params = params,
112 :     state = xs, stateInit = stm,
113 :     methods = List.map simplifyMethod methods
114 :     }
115 :     end
116 :    
117 :     and simplifyMethod (AST.M_Method(name, body)) =
118 : jhr 197 S.Method(name, simplifyBlock body)
119 : jhr 171
120 : jhr 1116 (* simplify a statement into a single statement (i.e., a block if it expands
121 :     * into more than one new statement).
122 : jhr 171 *)
123 :     and simplifyBlock stm = mkBlock (simplifyStmt (stm, []))
124 :    
125 :     and simplifyStmt (stm, stms) = (case stm
126 :     of AST.S_Block body => let
127 :     fun simplify ([], stms) = stms
128 :     | simplify (stm::r, stms) = simplify (r, simplifyStmt (stm, stms))
129 :     in
130 :     simplify (body, stms)
131 :     end
132 :     | AST.S_Decl(AST.VD_Decl(x, e)) => let
133 :     val (stms, e') = simplifyExp (e, stms)
134 :     in
135 :     S.S_Assign(x, e') :: stms
136 :     end
137 :     | AST.S_IfThenElse(e, s1, s2) => let
138 :     val (stms, x) = simplifyExpToVar (e, stms)
139 :     val s1 = simplifyBlock s1
140 :     val s2 = simplifyBlock s2
141 :     in
142 :     S.S_IfThenElse(x, s1, s2) :: stms
143 :     end
144 :     | AST.S_Assign(x, e) => let
145 :     val (stms, e') = simplifyExp (e, stms)
146 :     in
147 :     S.S_Assign(x, e') :: stms
148 :     end
149 :     | AST.S_New(name, args) => let
150 :     val (stms, xs) = simplifyExpsToVars (args, stms)
151 :     in
152 :     S.S_New(name, xs) :: stms
153 :     end
154 :     | AST.S_Die => S.S_Die :: stms
155 :     | AST.S_Stabilize => S.S_Stabilize :: stms
156 : jhr 1640 | AST.S_Print args => let
157 :     val (stms, xs) = simplifyExpsToVars (args, stms)
158 :     in
159 :     S.S_Print xs :: stms
160 :     end
161 : jhr 171 (* end case *))
162 :    
163 :     and simplifyExp (exp, stms) = (
164 :     case exp
165 : jhr 197 of AST.E_Var x => (case Var.kindOf x
166 :     of Var.BasisVar => let
167 :     val ty = Var.monoTypeOf x
168 :     val x' = newTemp ty
169 :     val stm = S.S_Assign(x', S.E_Apply(x, [], [], ty))
170 :     in
171 :     (stm::stms, S.E_Var x')
172 :     end
173 :     | _ => (stms, S.E_Var x)
174 :     (* end case *))
175 : jhr 171 | AST.E_Lit lit => (stms, S.E_Lit lit)
176 :     | AST.E_Tuple es => raise Fail "E_Tuple not yet implemented"
177 :     | AST.E_Apply(f, tyArgs, args, ty) => let
178 :     val (stms, xs) = simplifyExpsToVars (args, stms)
179 :     in
180 :     (stms, S.E_Apply(f, tyArgs, xs, ty))
181 :     end
182 :     | AST.E_Cons es => let
183 :     val (stms, xs) = simplifyExpsToVars (es, stms)
184 :     in
185 :     (stms, S.E_Cons xs)
186 :     end
187 : jhr 399 | AST.E_Slice(e, indices, ty) => let (* tensor slicing *)
188 : jhr 395 val (stms, x) = simplifyExpToVar (e, stms)
189 : jhr 399 fun f ([], ys, stms) = (stms, List.rev ys)
190 :     | f (NONE::es, ys, stms) = f (es, NONE::ys, stms)
191 :     | f (SOME e::es, ys, stms) = let
192 :     val (stms, y) = simplifyExpToVar (e, stms)
193 :     in
194 :     f (es, SOME y::ys, stms)
195 :     end
196 :     val (stms, indices) = f (indices, [], stms)
197 : jhr 395 in
198 : jhr 399 (stms, S.E_Slice(x, indices, ty))
199 : jhr 395 end
200 : jhr 416 | AST.E_Cond(e1, e2, e3, ty) => let
201 : jhr 171 (* a conditional expression gets turned into an if-then-else statememt *)
202 : jhr 416 val result = newTemp ty
203 : jhr 1116 val (stms, x) = simplifyExpToVar (e1, S.S_Var result :: stms)
204 : jhr 171 fun simplifyBranch e = let
205 :     val (stms, e) = simplifyExp (e, [])
206 :     in
207 :     mkBlock (S.S_Assign(result, e)::stms)
208 :     end
209 : jhr 415 val s1 = simplifyBranch e2
210 :     val s2 = simplifyBranch e3
211 : jhr 171 in
212 :     (S.S_IfThenElse(x, s1, s2) :: stms, S.E_Var result)
213 :     end
214 : jhr 1687 | AST.E_Coerce{srcTy, dstTy, e} => let
215 :     val (stms, x) = simplifyExpToVar (e, stms)
216 :     val result = newTemp dstTy
217 :     in
218 :     (S.S_Assign(result, S.E_Coerce{srcTy=srcTy, dstTy=dstTy, x=x})::stms, S.E_Var result)
219 :     end
220 : jhr 171 (* end case *))
221 :    
222 :     and simplifyExpToVar (exp, stms) = let
223 :     val (stms, e) = simplifyExp (exp, stms)
224 :     in
225 :     case e
226 :     of S.E_Var x => (stms, x)
227 :     | _ => let
228 :     val x = newTemp (S.typeOf e)
229 :     in
230 :     (S.S_Assign(x, e)::stms, x)
231 :     end
232 :     (* end case *)
233 :     end
234 :    
235 :     and simplifyExpsToVars (exps, stms) = let
236 :     fun f ([], xs, stms) = (stms, List.rev xs)
237 :     | f (e::es, xs, stms) = let
238 :     val (stms, x) = simplifyExpToVar (e, stms)
239 :     in
240 :     f (es, x::xs, stms)
241 :     end
242 :     in
243 :     f (exps, [], stms)
244 :     end
245 :    
246 : jhr 1140 fun transform (errStrm, ast) = let
247 : jhr 227 val simple = simplifyProgram ast
248 : jhr 238 val _ = SimplePP.output (Log.logFile(), simple) (* DEBUG *)
249 : jhr 227 val simple = Lift.transform simple
250 : jhr 1140 handle Eval.Error msg => (Error.error(errStrm, msg); simple)
251 : jhr 227 in
252 :     simple
253 :     end
254 :    
255 : jhr 171 end

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