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

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