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 1996 - (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 : jhr 1996 val rhs = (case Var.monoTypeOf x
40 :     of ty as Ty.T_DynSequence _ => S.E_InputNrrd(ty, Var.nameOf x, desc, NONE)
41 :     | ty as Ty.T_Image _ => S.E_InputNrrd(ty, Var.nameOf x, desc, NONE)
42 :     | ty => S.E_Input(ty, Var.nameOf x, desc, NONE)
43 :     (* end case *))
44 : jhr 185 in
45 :     globals := x :: !globals;
46 : jhr 1996 globalInit := S.S_Assign(x, rhs) :: !globalInit
47 : jhr 185 end
48 : jhr 1992 | AST.D_Input(x, desc, SOME(AST.E_LoadNrrd(tvs, nrrd, ty))) => let
49 : jhr 1996 (* load the nrrd proxy here *)
50 : jhr 1993 val info = NrrdInfo.getInfo nrrd
51 : jhr 1996 (* FIXME: what do we do with the proxy info? *)
52 :     val rhs = (case TypeUtil.pruneHead ty
53 :     of ty as Ty.T_DynSequence _ =>
54 :     S.E_InputNrrd(ty, Var.nameOf x, desc, SOME nrrd)
55 :     | Ty.T_Image _ =>
56 :     S.E_InputNrrd(ty, Var.nameOf x, desc, SOME nrrd)
57 : jhr 1992 | _ => raise Fail "impossible"
58 :     (* end case *))
59 : jhr 1996 val stm = S.S_Assign(x, rhs)
60 : jhr 1992 in
61 :     globals := x :: !globals;
62 :     globalInit := stm :: !globalInit
63 :     end
64 : jhr 1301 | AST.D_Input(x, desc, SOME e) => let
65 : jhr 179 val (stms, x') = simplifyExpToVar (e, [])
66 : jhr 1301 val e' = S.E_Input(Var.monoTypeOf x, Var.nameOf x, desc, SOME x')
67 : jhr 171 in
68 :     globals := x :: !globals;
69 : jhr 221 globalInit := S.S_Assign(x, e') :: (stms @ !globalInit)
70 : jhr 171 end
71 :     | AST.D_Var(AST.VD_Decl(x, e)) => let
72 :     val (stms, e') = simplifyExp (e, [])
73 :     in
74 :     globals := x :: !globals;
75 :     globalInit := S.S_Assign(x, e') :: (stms @ !globalInit)
76 :     end
77 : jhr 511 | AST.D_Strand info => strands := simplifyStrand info :: !strands
78 : jhr 1116 | AST.D_InitialArray(creat, iters) =>
79 :     setInitially (simplifyInit(true, creat, iters))
80 :     | AST.D_InitialCollection(creat, iters) =>
81 :     setInitially (simplifyInit(false, creat, iters))
82 : jhr 171 (* end case *))
83 :     in
84 :     List.app simplifyDecl dcls;
85 : jhr 175 S.Program{
86 : jhr 171 globals = List.rev(!globals),
87 :     globalInit = mkBlock (!globalInit),
88 : jhr 1116 init = (case !initially
89 :     (* FIXME: the check for the initially block should really happen in typechecking *)
90 :     of NONE => raise Fail "missing initially declaration"
91 :     | SOME blk => blk
92 :     (* end case *)),
93 : jhr 511 strands = List.rev(!strands)
94 : jhr 171 }
95 :     end
96 :    
97 : jhr 1116 and simplifyInit (isArray, AST.C_Create(strand, exps), iters) = let
98 :     val (stms, xs) = simplifyExpsToVars (exps, [])
99 :     val creat = S.C_Create{
100 :     argInit = mkBlock stms,
101 :     name = strand,
102 :     args = xs
103 :     }
104 :     fun simplifyIter (AST.I_Range(x, e1, e2), (iters, stms)) = let
105 :     val (stms, lo) = simplifyExpToVar (e1, stms)
106 :     val (stms, hi) = simplifyExpToVar (e2, stms)
107 :     in
108 :     ({param=x, lo=lo, hi=hi}::iters, stms)
109 :     end
110 :     val (iters, stms) = List.foldl simplifyIter ([], []) iters
111 :     in
112 :     S.Initially{
113 :     isArray = isArray,
114 :     rangeInit = mkBlock stms,
115 :     iters = List.rev iters,
116 :     create = creat
117 :     }
118 :     end
119 :    
120 : jhr 511 and simplifyStrand {name, params, state, methods} = let
121 : jhr 171 fun simplifyState ([], xs, stms) = (List.rev xs, mkBlock stms)
122 :     | simplifyState (AST.VD_Decl(x, e) :: r, xs, stms) = let
123 :     val (stms, e') = simplifyExp (e, stms)
124 :     in
125 :     simplifyState (r, x::xs, S.S_Assign(x, e') :: stms)
126 :     end
127 :     val (xs, stm) = simplifyState (state, [], [])
128 :     in
129 : jhr 511 S.Strand{
130 : jhr 173 name = name,
131 : jhr 171 params = params,
132 :     state = xs, stateInit = stm,
133 :     methods = List.map simplifyMethod methods
134 :     }
135 :     end
136 :    
137 :     and simplifyMethod (AST.M_Method(name, body)) =
138 : jhr 197 S.Method(name, simplifyBlock body)
139 : jhr 171
140 : jhr 1116 (* simplify a statement into a single statement (i.e., a block if it expands
141 :     * into more than one new statement).
142 : jhr 171 *)
143 :     and simplifyBlock stm = mkBlock (simplifyStmt (stm, []))
144 :    
145 :     and simplifyStmt (stm, stms) = (case stm
146 :     of AST.S_Block body => let
147 :     fun simplify ([], stms) = stms
148 :     | simplify (stm::r, stms) = simplify (r, simplifyStmt (stm, stms))
149 :     in
150 :     simplify (body, stms)
151 :     end
152 :     | AST.S_Decl(AST.VD_Decl(x, e)) => let
153 :     val (stms, e') = simplifyExp (e, stms)
154 :     in
155 :     S.S_Assign(x, e') :: stms
156 :     end
157 :     | AST.S_IfThenElse(e, s1, s2) => let
158 :     val (stms, x) = simplifyExpToVar (e, stms)
159 :     val s1 = simplifyBlock s1
160 :     val s2 = simplifyBlock s2
161 :     in
162 :     S.S_IfThenElse(x, s1, s2) :: stms
163 :     end
164 :     | AST.S_Assign(x, e) => let
165 :     val (stms, e') = simplifyExp (e, stms)
166 :     in
167 :     S.S_Assign(x, e') :: stms
168 :     end
169 :     | AST.S_New(name, args) => let
170 :     val (stms, xs) = simplifyExpsToVars (args, stms)
171 :     in
172 :     S.S_New(name, xs) :: stms
173 :     end
174 :     | AST.S_Die => S.S_Die :: stms
175 :     | AST.S_Stabilize => S.S_Stabilize :: stms
176 : jhr 1640 | AST.S_Print args => let
177 :     val (stms, xs) = simplifyExpsToVars (args, stms)
178 :     in
179 :     S.S_Print xs :: stms
180 :     end
181 : jhr 171 (* end case *))
182 :    
183 :     and simplifyExp (exp, stms) = (
184 :     case exp
185 : jhr 197 of AST.E_Var x => (case Var.kindOf x
186 :     of Var.BasisVar => let
187 :     val ty = Var.monoTypeOf x
188 :     val x' = newTemp ty
189 :     val stm = S.S_Assign(x', S.E_Apply(x, [], [], ty))
190 :     in
191 :     (stm::stms, S.E_Var x')
192 :     end
193 :     | _ => (stms, S.E_Var x)
194 :     (* end case *))
195 : jhr 171 | AST.E_Lit lit => (stms, S.E_Lit lit)
196 :     | AST.E_Tuple es => raise Fail "E_Tuple not yet implemented"
197 :     | AST.E_Apply(f, tyArgs, args, ty) => let
198 :     val (stms, xs) = simplifyExpsToVars (args, stms)
199 :     in
200 :     (stms, S.E_Apply(f, tyArgs, xs, ty))
201 :     end
202 :     | AST.E_Cons es => let
203 :     val (stms, xs) = simplifyExpsToVars (es, stms)
204 :     in
205 :     (stms, S.E_Cons xs)
206 :     end
207 : jhr 1688 | AST.E_Seq es => let
208 :     val (stms, xs) = simplifyExpsToVars (es, stms)
209 :     in
210 :     (stms, S.E_Seq xs)
211 :     end
212 : jhr 399 | AST.E_Slice(e, indices, ty) => let (* tensor slicing *)
213 : jhr 395 val (stms, x) = simplifyExpToVar (e, stms)
214 : jhr 399 fun f ([], ys, stms) = (stms, List.rev ys)
215 :     | f (NONE::es, ys, stms) = f (es, NONE::ys, stms)
216 :     | f (SOME e::es, ys, stms) = let
217 :     val (stms, y) = simplifyExpToVar (e, stms)
218 :     in
219 :     f (es, SOME y::ys, stms)
220 :     end
221 :     val (stms, indices) = f (indices, [], stms)
222 : jhr 395 in
223 : jhr 399 (stms, S.E_Slice(x, indices, ty))
224 : jhr 395 end
225 : jhr 416 | AST.E_Cond(e1, e2, e3, ty) => let
226 : jhr 171 (* a conditional expression gets turned into an if-then-else statememt *)
227 : jhr 416 val result = newTemp ty
228 : jhr 1116 val (stms, x) = simplifyExpToVar (e1, S.S_Var result :: stms)
229 : jhr 171 fun simplifyBranch e = let
230 :     val (stms, e) = simplifyExp (e, [])
231 :     in
232 :     mkBlock (S.S_Assign(result, e)::stms)
233 :     end
234 : jhr 415 val s1 = simplifyBranch e2
235 :     val s2 = simplifyBranch e3
236 : jhr 171 in
237 :     (S.S_IfThenElse(x, s1, s2) :: stms, S.E_Var result)
238 :     end
239 : jhr 1996 | AST.E_LoadNrrd _ => raise Fail "unexpected E_LoadNrrd" (* should be handled by simplifyDecl *)
240 : jhr 1687 | AST.E_Coerce{srcTy, dstTy, e} => let
241 :     val (stms, x) = simplifyExpToVar (e, stms)
242 :     val result = newTemp dstTy
243 :     in
244 :     (S.S_Assign(result, S.E_Coerce{srcTy=srcTy, dstTy=dstTy, x=x})::stms, S.E_Var result)
245 :     end
246 : jhr 171 (* end case *))
247 :    
248 :     and simplifyExpToVar (exp, stms) = let
249 :     val (stms, e) = simplifyExp (exp, stms)
250 :     in
251 :     case e
252 :     of S.E_Var x => (stms, x)
253 :     | _ => let
254 :     val x = newTemp (S.typeOf e)
255 :     in
256 :     (S.S_Assign(x, e)::stms, x)
257 :     end
258 :     (* end case *)
259 :     end
260 :    
261 :     and simplifyExpsToVars (exps, stms) = let
262 :     fun f ([], xs, stms) = (stms, List.rev xs)
263 :     | f (e::es, xs, stms) = let
264 :     val (stms, x) = simplifyExpToVar (e, stms)
265 :     in
266 :     f (es, x::xs, stms)
267 :     end
268 :     in
269 :     f (exps, [], stms)
270 :     end
271 :    
272 : jhr 1140 fun transform (errStrm, ast) = let
273 : jhr 227 val simple = simplifyProgram ast
274 : jhr 238 val _ = SimplePP.output (Log.logFile(), simple) (* DEBUG *)
275 : jhr 1993 (*
276 : jhr 227 val simple = Lift.transform simple
277 : jhr 1140 handle Eval.Error msg => (Error.error(errStrm, msg); simple)
278 : jhr 1993 *)
279 : jhr 227 in
280 :     simple
281 :     end
282 :    
283 : jhr 171 end

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