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

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