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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4083 - (view) (download)

1 : jhr 3437 (* simplify.sml
2 :     *
3 :     * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
4 :     *
5 :     * COPYRIGHT (c) 2015 The University of Chicago
6 :     * All rights reserved.
7 :     *
8 : jhr 3468 * Simplify the AST representation. This phase involves the following transformations:
9 :     *
10 :     * - types are simplified by removing meta variables (which will have been resolved)
11 :     *
12 :     * - expressions are simplified to involve a single operation on variables
13 :     *
14 :     * - global reductions are converted to MapReduce statements
15 :     *
16 :     * - other comprehensions and reductions are converted to foreach loops
17 :     *
18 :     * - unreachable code is pruned
19 :     *
20 :     * - negation of literal integers and reals are constant folded
21 : jhr 3437 *)
22 :    
23 :     structure Simplify : sig
24 :    
25 :     val transform : Error.err_stream * AST.program -> Simple.program
26 :    
27 :     end = struct
28 :    
29 :     structure TU = TypeUtil
30 :     structure S = Simple
31 : jhr 3445 structure STy = SimpleTypes
32 :     structure Ty = Types
33 : jhr 3437 structure VMap = Var.Map
34 :    
35 : jhr 3445 (* convert a Types.ty to a SimpleTypes.ty *)
36 :     fun cvtTy ty = (case ty
37 :     of Ty.T_Var(Ty.TV{bind, ...}) => (case !bind
38 :     of NONE => raise Fail "unresolved type variable"
39 :     | SOME ty => cvtTy ty
40 :     (* end case *))
41 :     | Ty.T_Bool => STy.T_Bool
42 :     | Ty.T_Int => STy.T_Int
43 :     | Ty.T_String => STy.T_String
44 :     | Ty.T_Sequence(ty, NONE) => STy.T_Sequence(cvtTy ty, NONE)
45 : jhr 3452 | Ty.T_Sequence(ty, SOME dim) => STy.T_Sequence(cvtTy ty, SOME(TU.monoDim dim))
46 : jhr 3846 | Ty.T_Strand id => STy.T_Strand id
47 : jhr 3445 | Ty.T_Kernel n => STy.T_Kernel(TU.monoDiff n)
48 :     | Ty.T_Tensor shape => STy.T_Tensor(TU.monoShape shape)
49 :     | Ty.T_Image{dim, shape} => STy.T_Image{
50 :     dim = TU.monoDim dim,
51 :     shape = TU.monoShape shape
52 :     }
53 :     | Ty.T_Field{diff, dim, shape} => STy.T_Field{
54 :     diff = TU.monoDiff diff,
55 :     dim = TU.monoDim dim,
56 :     shape = TU.monoShape shape
57 :     }
58 : jhr 3452 | Ty.T_Fun(tys1, ty2) => STy.T_Fun(List.map cvtTy tys1, cvtTy ty2)
59 : jhr 3456 | Ty.T_Error => raise Fail "unexpected T_Error in Simplify"
60 : jhr 3445 (* end case *))
61 : jhr 3437
62 : jhr 3811 fun apiTypeOf x = let
63 :     fun cvtTy STy.T_Bool = APITypes.BoolTy
64 :     | cvtTy STy.T_Int = APITypes.IntTy
65 :     | cvtTy STy.T_String = APITypes.StringTy
66 :     | cvtTy (STy.T_Sequence(ty, len)) = APITypes.SeqTy(cvtTy ty, len)
67 :     | cvtTy (STy.T_Tensor shape) = APITypes.TensorTy shape
68 :     | cvtTy (STy.T_Image{dim, shape}) = APITypes.ImageTy(dim, shape)
69 :     | cvtTy ty = raise Fail "bogus API type"
70 :     in
71 :     cvtTy (SimpleVar.typeOf x)
72 :     end
73 :    
74 : jhr 3437 fun newTemp ty = SimpleVar.new ("_t", SimpleVar.LocalVar, ty)
75 :    
76 : jhr 3456 (* a property to map AST variables to SimpleAST variables *)
77 :     local
78 :     fun cvt x = SimpleVar.new (Var.nameOf x, Var.kindOf x, cvtTy(Var.monoTypeOf x))
79 :     in
80 :     val {getFn = cvtVar, ...} = Var.newProp cvt
81 :     end
82 : jhr 3452
83 : jhr 3456 fun cvtVars xs = List.map cvtVar xs
84 : jhr 3452
85 : jhr 3437 (* make a block out of a list of statements that are in reverse order *)
86 : jhr 3501 fun mkBlock stms = S.Block{props = PropList.newHolder(), code = List.rev stms}
87 : jhr 3437
88 :     datatype 'a ctl_flow_info
89 :     = EXIT (* stm sequence always exits; no pruning so far *)
90 :     | PRUNE of 'a (* stm sequence always exits at last stm in argument, which
91 : jhr 3445 * is either a block or stm list *)
92 : jhr 3437 | CONT (* stm sequence falls through *)
93 :     | EDIT of 'a (* pruned code that has non-exiting paths *)
94 :    
95 : jhr 3501 fun pruneUnreachableCode blk = let
96 : jhr 3437 fun isExit S.S_Die = true
97 :     | isExit S.S_Stabilize = true
98 :     | isExit (S.S_Return _) = true
99 :     | isExit _ = false
100 :     fun pruneStms [] = CONT
101 :     | pruneStms [S.S_IfThenElse(x, blk1, blk2)] = (
102 :     case pruneIf(x, blk1, blk2)
103 :     of EXIT => EXIT
104 :     | PRUNE stm => PRUNE[stm]
105 :     | CONT => CONT
106 :     | EDIT stm => EDIT[stm]
107 :     (* end case *))
108 :     | pruneStms [stm] = if isExit stm then EXIT else CONT
109 :     | pruneStms ((stm as S.S_IfThenElse(x, blk1, blk2))::stms) = (
110 :     case pruneIf(x, blk1, blk2)
111 :     of EXIT => PRUNE[stm]
112 :     | PRUNE stm => PRUNE[stm]
113 :     | CONT => (case pruneStms stms
114 :     of PRUNE stms => PRUNE(stm::stms)
115 :     | EDIT stms => EDIT(stm::stms)
116 :     | EXIT => EXIT (* different instances of ctl_flow_info *)
117 :     | CONT => CONT
118 :     (* end case *))
119 :     | EDIT stm => (case pruneStms stms
120 :     of PRUNE stms => PRUNE(stm::stms)
121 :     | EDIT stms => EDIT(stm::stms)
122 :     | _ => EDIT(stm::stms)
123 :     (* end case *))
124 :     (* end case *))
125 :     | pruneStms (stm::stms) = if isExit stm
126 :     then PRUNE[stm]
127 :     else (case pruneStms stms
128 :     of PRUNE stms => PRUNE(stm::stms)
129 :     | EDIT stms => EDIT(stm::stms)
130 :     | info => info
131 :     (* end case *))
132 :     and pruneIf (x, blk1, blk2) = (case (pruneBlk blk1, pruneBlk blk2)
133 :     of (EXIT, EXIT ) => EXIT
134 :     | (CONT, CONT ) => CONT
135 :     | (CONT, EXIT ) => CONT
136 :     | (EXIT, CONT ) => CONT
137 :     | (CONT, EDIT blk2 ) => EDIT(S.S_IfThenElse(x, blk1, blk2))
138 :     | (EDIT blk1, CONT ) => EDIT(S.S_IfThenElse(x, blk1, blk2))
139 :     | (CONT, PRUNE blk2) => EDIT(S.S_IfThenElse(x, blk1, blk2))
140 :     | (PRUNE blk1, CONT ) => EDIT(S.S_IfThenElse(x, blk1, blk2))
141 :     | (EXIT, EDIT blk2 ) => EDIT(S.S_IfThenElse(x, blk1, blk2))
142 :     | (EDIT blk1, EXIT ) => EDIT(S.S_IfThenElse(x, blk1, blk2))
143 :     | (EDIT blk1, EDIT blk2 ) => EDIT(S.S_IfThenElse(x, blk1, blk2))
144 :     | (EDIT blk1, PRUNE blk2) => EDIT(S.S_IfThenElse(x, blk1, blk2))
145 :     | (PRUNE blk1, EDIT blk2 ) => EDIT(S.S_IfThenElse(x, blk1, blk2))
146 :     | (EXIT, PRUNE blk2) => PRUNE(S.S_IfThenElse(x, blk1, blk2))
147 :     | (PRUNE blk1, EXIT ) => PRUNE(S.S_IfThenElse(x, blk1, blk2))
148 :     | (PRUNE blk1, PRUNE blk2) => PRUNE(S.S_IfThenElse(x, blk1, blk2))
149 :     (* end case *))
150 : jhr 3501 and pruneBlk (S.Block{props, code}) = (case pruneStms code
151 :     of PRUNE stms => PRUNE(S.Block{props=props, code=stms})
152 :     | EDIT stms => EDIT(S.Block{props=props, code=stms})
153 : jhr 3437 | EXIT => EXIT (* different instances of ctl_flow_info *)
154 :     | CONT => CONT
155 :     (* end case *))
156 :     in
157 :     case pruneBlk blk
158 :     of PRUNE blk => blk
159 :     | EDIT blk => blk
160 :     | _=> blk
161 :     (* end case *)
162 :     end
163 :    
164 :     (* simplify a statement into a single statement (i.e., a block if it expands
165 :     * into more than one new statement).
166 :     *)
167 : jhr 3456 fun simplifyBlock errStrm stm = mkBlock (simplifyStmt (errStrm, stm, []))
168 : jhr 3437
169 :     (* simplify the statement stm where stms is a reverse-order list of preceeding simplified
170 :     * statements. This function returns a reverse-order list of simplified statements.
171 :     * Note that error reporting is done in the typechecker, but it does not prune unreachable
172 :     * code.
173 :     *)
174 : jhr 3456 and simplifyStmt (errStrm, stm, stms) : S.stmt list = (case stm
175 : jhr 3437 of AST.S_Block body => let
176 : jhr 3456 fun simplify ([], stms) = stms
177 :     | simplify (stm::r, stms) = simplify (r, simplifyStmt (errStrm, stm, stms))
178 : jhr 3437 in
179 : jhr 3456 simplify (body, stms)
180 : jhr 3437 end
181 : jhr 3452 | AST.S_Decl(x, NONE) => let
182 : jhr 3456 val x' = cvtVar x
183 : jhr 3452 in
184 : jhr 3465 S.S_Var(x', NONE) :: stms
185 : jhr 3452 end
186 :     | AST.S_Decl(x, SOME e) => let
187 : jhr 3456 val (stms, e') = simplifyExp (errStrm, e, stms)
188 :     val x' = cvtVar x
189 : jhr 3437 in
190 : jhr 3465 S.S_Var(x', SOME e') :: stms
191 : jhr 3437 end
192 :     | AST.S_IfThenElse(e, s1, s2) => let
193 : jhr 3456 val (stms, x) = simplifyExpToVar (errStrm, e, stms)
194 :     val s1 = simplifyBlock errStrm s1
195 :     val s2 = simplifyBlock errStrm s2
196 : jhr 3437 in
197 : jhr 3456 S.S_IfThenElse(x, s1, s2) :: stms
198 : jhr 3437 end
199 : jhr 3456 | AST.S_Foreach((x, e), body) => let
200 :     val (stms, xs') = simplifyExpToVar (errStrm, e, stms)
201 :     val body' = simplifyBlock errStrm body
202 :     in
203 :     S.S_Foreach(cvtVar x, xs', body') :: stms
204 :     end
205 : jhr 3452 | AST.S_Assign((x, _), e) => let
206 : jhr 3456 val (stms, e') = simplifyExp (errStrm, e, stms)
207 : jhr 3437 in
208 : jhr 3456 S.S_Assign(cvtVar x, e') :: stms
209 : jhr 3437 end
210 :     | AST.S_New(name, args) => let
211 : jhr 3456 val (stms, xs) = simplifyExpsToVars (errStrm, args, stms)
212 : jhr 3437 in
213 : jhr 3456 S.S_New(name, xs) :: stms
214 : jhr 3437 end
215 : jhr 3456 | AST.S_Continue => S.S_Continue :: stms
216 :     | AST.S_Die => S.S_Die :: stms
217 :     | AST.S_Stabilize => S.S_Stabilize :: stms
218 : jhr 3437 | AST.S_Return e => let
219 : jhr 3456 val (stms, x) = simplifyExpToVar (errStrm, e, stms)
220 : jhr 3437 in
221 : jhr 3456 S.S_Return x :: stms
222 : jhr 3437 end
223 :     | AST.S_Print args => let
224 : jhr 3456 val (stms, xs) = simplifyExpsToVars (errStrm, args, stms)
225 : jhr 3437 in
226 : jhr 3456 S.S_Print xs :: stms
227 : jhr 3437 end
228 :     (* end case *))
229 :    
230 : jhr 3456 and simplifyExp (errStrm, exp, stms) = let
231 : jhr 3452 fun doPrimApply (f, tyArgs, args, ty) = let
232 : jhr 3456 val (stms, xs) = simplifyExpsToVars (errStrm, args, stms)
233 : jhr 3437 in
234 :     case Var.kindOf f
235 : jhr 3452 of Var.BasisVar => let
236 : jhr 3437 fun cvtTyArg (Types.TYPE tv) = S.TY(cvtTy(TU.resolve tv))
237 :     | cvtTyArg (Types.DIFF dv) = S.DIFF(TU.monoDiff(TU.resolveDiff dv))
238 :     | cvtTyArg (Types.SHAPE sv) = S.SHAPE(TU.monoShape(TU.resolveShape sv))
239 :     | cvtTyArg (Types.DIM dv) = S.DIM(TU.monoDim(TU.resolveDim dv))
240 :     val tyArgs = List.map cvtTyArg tyArgs
241 :     in
242 :     (stms, S.E_Prim(f, tyArgs, xs, cvtTy ty))
243 :     end
244 : jhr 3452 | _ => raise Fail "bogus prim application"
245 : jhr 3437 (* end case *)
246 :     end
247 :     in
248 :     case exp
249 : jhr 3452 of AST.E_Var(x, _) => (case Var.kindOf x
250 : jhr 3437 of Var.BasisVar => let
251 :     val ty = cvtTy(Var.monoTypeOf x)
252 :     val x' = newTemp ty
253 : jhr 3465 val stm = S.S_Var(x', SOME(S.E_Prim(x, [], [], ty)))
254 : jhr 3437 in
255 :     (stm::stms, S.E_Var x')
256 :     end
257 : jhr 3456 | _ => (stms, S.E_Var(cvtVar x))
258 : jhr 3437 (* end case *))
259 :     | AST.E_Lit lit => (stms, S.E_Lit lit)
260 : jhr 4043 | AST.E_Kernel h => (stms, S.E_Kernel h)
261 : jhr 3456 | AST.E_Select(e, (fld, _)) => let
262 :     val (stms, x) = simplifyExpToVar (errStrm, e, stms)
263 :     in
264 :     (stms, S.E_Select(x, cvtVar fld))
265 :     end
266 : jhr 3464 | AST.E_Prim(rator, tyArgs, args as [e], ty) => (case e
267 :     of AST.E_Lit(Literal.Int n) => if Var.same(BasisVars.neg_i, rator)
268 :     then (stms, S.E_Lit(Literal.Int(~n))) (* constant-fold negation of integer literals *)
269 :     else doPrimApply (rator, tyArgs, args, ty)
270 :     | AST.E_Lit(Literal.Real f) =>
271 :     if Var.same(BasisVars.neg_t, rator)
272 :     then (stms, S.E_Lit(Literal.Real(RealLit.negate f))) (* constant-fold negation of real literals *)
273 :     else doPrimApply (rator, tyArgs, args, ty)
274 :     | AST.E_Comprehension(e', (x, e''), seqTy) => if Basis.isReductionOp rator
275 :     then let
276 :     val {rator, init, mvs} = Util.reductionInfo rator
277 :     val (stms, xs) = simplifyExpToVar (errStrm, e'', stms)
278 :     val (bodyStms, bodyResult) = simplifyExpToVar (errStrm, e, [])
279 :     val acc = SimpleVar.new ("accum", Var.LocalVar, cvtTy ty)
280 :     val seqTy' as STy.T_Sequence(elemTy, NONE) = cvtTy seqTy
281 : jhr 3465 val initStm = S.S_Var(acc, SOME(S.E_Lit init))
282 : jhr 3464 val updateStm = S.S_Assign(acc,
283 :     S.E_Prim(rator, mvs, [acc, bodyResult], seqTy'))
284 :     val foreachStm = S.S_Foreach(cvtVar x, xs, mkBlock(updateStm :: bodyStms))
285 :     in
286 :     (foreachStm :: initStm :: stms, S.E_Var acc)
287 :     end
288 :     else doPrimApply (rator, tyArgs, args, ty)
289 :     | AST.E_ParallelMap(e', x, xs, _) =>
290 :     if Basis.isReductionOp rator
291 : jhr 3465 then let
292 :     (* parallel map-reduce *)
293 :     val result = SimpleVar.new ("res", Var.LocalVar, cvtTy ty)
294 :     val (bodyStms, bodyResult) = simplifyExpToVar (errStrm, e', [])
295 :     val (func, args) = Util.makeFunction(
296 :     Var.nameOf rator, mkBlock(S.S_Return bodyResult :: bodyStms),
297 :     SimpleVar.typeOf bodyResult)
298 :     val mapReduceStm = S.S_MapReduce{
299 :     results = [result],
300 :     reductions = [rator],
301 :     body = func,
302 :     args = args,
303 :     source = xs
304 :     }
305 :     in
306 :     (mapReduceStm :: stms, S.E_Var result)
307 :     end
308 : jhr 3464 else raise Fail "unsupported operation on parallel map"
309 :     | _ => doPrimApply (rator, tyArgs, args, ty)
310 :     (* end case *))
311 : jhr 3452 | AST.E_Prim(f, tyArgs, args, ty) => doPrimApply (f, tyArgs, args, ty)
312 :     | AST.E_Apply((f, _), args, ty) => let
313 : jhr 3456 val (stms, xs) = simplifyExpsToVars (errStrm, args, stms)
314 : jhr 3452 in
315 :     case Var.kindOf f
316 : jhr 3456 of Var.FunVar => (stms, S.E_Apply(cvtVar f, xs, cvtTy ty))
317 : jhr 3452 | _ => raise Fail "bogus application"
318 :     (* end case *)
319 :     end
320 : jhr 3464 | AST.E_Comprehension(e, (x, e'), seqTy) => let
321 :     (* convert a comprehension to a foreach loop over the sequence defined by e' *)
322 :     val (stms, xs) = simplifyExpToVar (errStrm, e', stms)
323 :     val (bodyStms, bodyResult) = simplifyExpToVar (errStrm, e, [])
324 :     val seqTy' as STy.T_Sequence(elemTy, NONE) = cvtTy seqTy
325 :     val acc = SimpleVar.new ("accum", Var.LocalVar, seqTy')
326 : jhr 3465 val initStm = S.S_Var(acc, SOME(S.E_Seq([], seqTy')))
327 : jhr 3464 val updateStm = S.S_Assign(acc,
328 :     S.E_Prim(BasisVars.at_dT, [S.TY elemTy], [acc, bodyResult], seqTy'))
329 :     val foreachStm = S.S_Foreach(cvtVar x, xs, mkBlock(updateStm :: bodyStms))
330 :     in
331 :     (foreachStm :: initStm :: stms, S.E_Var acc)
332 :     end
333 : jhr 3467 | AST.E_ParallelMap(e, x, xs, ty) => raise Fail "FIXME: ParallelMap"
334 : jhr 3452 | AST.E_Tensor(es, ty) => let
335 : jhr 3456 val (stms, xs) = simplifyExpsToVars (errStrm, es, stms)
336 : jhr 3437 in
337 : jhr 3452 (stms, S.E_Tensor(xs, cvtTy ty))
338 : jhr 3437 end
339 :     | AST.E_Seq(es, ty) => let
340 : jhr 3456 val (stms, xs) = simplifyExpsToVars (errStrm, es, stms)
341 : jhr 3437 in
342 :     (stms, S.E_Seq(xs, cvtTy ty))
343 :     end
344 :     | AST.E_Slice(e, indices, ty) => let (* tensor slicing *)
345 : jhr 3456 val (stms, x) = simplifyExpToVar (errStrm, e, stms)
346 : jhr 3797 fun f NONE = NONE
347 :     | f (SOME(AST.E_Lit(Literal.Int i))) = SOME(Int.fromLarge i)
348 :     | f _ = raise Fail "expected integer literal in slice"
349 :     val indices = List.map f indices
350 : jhr 3437 in
351 :     (stms, S.E_Slice(x, indices, cvtTy ty))
352 :     end
353 :     | AST.E_Cond(e1, e2, e3, ty) => let
354 :     (* a conditional expression gets turned into an if-then-else statememt *)
355 :     val result = newTemp(cvtTy ty)
356 : jhr 3465 val (stms, x) = simplifyExpToVar (errStrm, e1, S.S_Var(result, NONE) :: stms)
357 : jhr 3437 fun simplifyBranch e = let
358 : jhr 3456 val (stms, e) = simplifyExp (errStrm, e, [])
359 : jhr 3437 in
360 :     mkBlock (S.S_Assign(result, e)::stms)
361 :     end
362 :     val s1 = simplifyBranch e2
363 :     val s2 = simplifyBranch e3
364 :     in
365 :     (S.S_IfThenElse(x, s1, s2) :: stms, S.E_Var result)
366 :     end
367 :     | AST.E_LoadNrrd(_, nrrd, ty) => (case cvtTy ty
368 : jhr 3452 of ty as SimpleTypes.T_Sequence(_, NONE) => (stms, S.E_LoadSeq(ty, nrrd))
369 : jhr 3437 | ty as SimpleTypes.T_Image{dim, shape} => (
370 : jhr 4083 case NrrdInfo.getInfo (errStrm, nrrd)
371 :     of SOME info => (case ImageInfo.fromNrrd(info, dim, shape)
372 :     of NONE => (
373 :     Error.error (errStrm, [
374 :     "nrrd file \"", nrrd, "\" does not have expected type"
375 :     ]);
376 :     (stms, S.E_LoadImage(ty, nrrd, ImageInfo.mkInfo(dim, shape))))
377 :     | SOME info => (stms, S.E_LoadImage(ty, nrrd, info))
378 :     (* end case *))
379 :     | NONE => (
380 :     Error.warning (errStrm, [
381 :     "nrrd file \"", nrrd, "\" does not exist"
382 :     ]);
383 :     (stms, S.E_LoadImage(ty, nrrd, ImageInfo.mkInfo(dim, shape))))
384 : jhr 3437 (* end case *))
385 :     | _ => raise Fail "bogus type for E_LoadNrrd"
386 :     (* end case *))
387 : jhr 3976 | AST.E_Coerce{dstTy, e=AST.E_Lit(Literal.Int n), ...} => (case cvtTy dstTy
388 :     of SimpleTypes.T_Tensor[] => (stms, S.E_Lit(Literal.Real(RealLit.fromInt n)))
389 :     | _ => raise Fail "impossible: bad coercion"
390 :     (* end case *))
391 : jhr 3437 | AST.E_Coerce{srcTy, dstTy, e} => let
392 : jhr 3456 val (stms, x) = simplifyExpToVar (errStrm, e, stms)
393 : jhr 3437 val dstTy = cvtTy dstTy
394 :     val result = newTemp dstTy
395 :     val rhs = S.E_Coerce{srcTy = cvtTy srcTy, dstTy = dstTy, x = x}
396 :     in
397 : jhr 3465 (S.S_Var(result, SOME rhs)::stms, S.E_Var result)
398 : jhr 3437 end
399 :     (* end case *)
400 :     end
401 :    
402 : jhr 3456 and simplifyExpToVar (errStrm, exp, stms) = let
403 :     val (stms, e) = simplifyExp (errStrm, exp, stms)
404 : jhr 3437 in
405 :     case e
406 :     of S.E_Var x => (stms, x)
407 :     | _ => let
408 :     val x = newTemp (S.typeOf e)
409 :     in
410 : jhr 3465 (S.S_Var(x, SOME e)::stms, x)
411 : jhr 3437 end
412 :     (* end case *)
413 :     end
414 :    
415 : jhr 3456 and simplifyExpsToVars (errStrm, exps, stms) = let
416 : jhr 3437 fun f ([], xs, stms) = (stms, List.rev xs)
417 :     | f (e::es, xs, stms) = let
418 : jhr 3456 val (stms, x) = simplifyExpToVar (errStrm, e, stms)
419 : jhr 3437 in
420 :     f (es, x::xs, stms)
421 :     end
422 :     in
423 :     f (exps, [], stms)
424 :     end
425 :    
426 : jhr 3995 fun simplifyStrand (errStrm, strand) = let
427 :     val AST.Strand{name, params, state, stateInit, initM, updateM, stabilizeM} = strand
428 : jhr 3456 val params' = cvtVars params
429 :     fun simplifyState ([], xs, stms) = (List.rev xs, mkBlock stms)
430 :     | simplifyState ((x, optE) :: r, xs, stms) = let
431 :     val x' = cvtVar x
432 : jhr 3452 in
433 :     case optE
434 : jhr 3456 of NONE => simplifyState (r, x'::xs, stms)
435 : jhr 3452 | SOME e => let
436 : jhr 3456 val (stms, e') = simplifyExp (errStrm, e, stms)
437 : jhr 3452 in
438 : jhr 3469 simplifyState (r, x'::xs, S.S_Assign(x', e') :: stms)
439 : jhr 3452 end
440 :     (* end case *)
441 :     end
442 : jhr 3456 val (xs, stm) = simplifyState (state, [], [])
443 : jhr 3437 in
444 : jhr 3452 S.Strand{
445 :     name = name,
446 :     params = params',
447 : jhr 3456 state = xs,
448 :     stateInit = stm,
449 :     initM = Option.map (simplifyBlock errStrm) initM,
450 :     updateM = simplifyBlock errStrm updateM,
451 :     stabilizeM = Option.map (simplifyBlock errStrm) stabilizeM
452 : jhr 3452 }
453 : jhr 3437 end
454 :    
455 : jhr 3452 fun transform (errStrm, prog) = let
456 :     val AST.Program{
457 : jhr 3995 props, const_dcls, input_dcls, globals, globInit, strand, create, init, update
458 : jhr 3452 } = prog
459 : jhr 3456 val consts' = ref[]
460 :     val constInit = ref[]
461 : jhr 3452 val inputs' = ref[]
462 :     val globals' = ref[]
463 :     val globalInit = ref[]
464 :     val funcs = ref[]
465 : jhr 3456 fun simplifyConstDcl (x, SOME e) = let
466 :     val (stms, e') = simplifyExp (errStrm, e, [])
467 :     val x' = cvtVar x
468 : jhr 3452 in
469 : jhr 3456 consts' := x' :: !consts';
470 :     constInit := S.S_Assign(x', e') :: (stms @ !constInit)
471 :     end
472 :     fun simplifyInputDcl ((x, NONE), desc) = let
473 :     val x' = cvtVar x
474 :     val init = (case SimpleVar.typeOf x'
475 : jhr 3811 of STy.T_Image{dim, shape} => let
476 : jhr 3456 val info = ImageInfo.mkInfo(dim, shape)
477 :     in
478 :     S.Image info
479 :     end
480 :     | _ => S.NoDefault
481 :     (* end case *))
482 :     val inp = S.INP{
483 :     var = x',
484 : jhr 3504 name = Var.nameOf x,
485 : jhr 3811 ty = apiTypeOf x',
486 : jhr 3456 desc = desc,
487 :     init = init
488 :     }
489 :     in
490 :     inputs' := inp :: !inputs'
491 :     end
492 :     | simplifyInputDcl ((x, SOME(AST.E_LoadNrrd(tvs, nrrd, ty))), desc) = let
493 :     val x' = cvtVar x
494 :     val init = (case SimpleVar.typeOf x'
495 :     of SimpleTypes.T_Sequence(_, NONE) => S.LoadSeq nrrd
496 : jhr 4083 | SimpleTypes.T_Image{dim, shape} => (
497 :     case NrrdInfo.getInfo (errStrm, nrrd)
498 :     of SOME info => (case ImageInfo.fromNrrd(info, dim, shape)
499 :     of NONE => (
500 :     Error.error (errStrm, [
501 :     "proxy nrrd file \"", nrrd,
502 :     "\" does not have expected type"
503 :     ]);
504 :     S.Image(ImageInfo.mkInfo(dim, shape)))
505 :     | SOME info => S.Proxy(nrrd, info)
506 :     (* end case *))
507 :     | NONE => (
508 :     Error.warning (errStrm, [
509 :     "proxy nrrd file \"", nrrd, "\" does not exist"
510 :     ]);
511 :     S.Image(ImageInfo.mkInfo(dim, shape)))
512 :     (* end case *))
513 : jhr 3456 | _ => raise Fail "impossible"
514 :     (* end case *))
515 :     val inp = S.INP{
516 :     var = x',
517 : jhr 3504 name = Var.nameOf x,
518 : jhr 3811 ty = apiTypeOf x',
519 : jhr 3456 desc = desc,
520 :     init = init
521 :     }
522 :     in
523 :     inputs' := inp :: !inputs'
524 :     end
525 :     | simplifyInputDcl ((x, SOME e), desc) = let
526 :     val x' = cvtVar x
527 :     val (stms, e') = simplifyExp (errStrm, e, [])
528 :     val inp = S.INP{
529 :     var = x',
530 : jhr 3504 name = Var.nameOf x,
531 : jhr 3811 ty = apiTypeOf x',
532 : jhr 3456 desc = desc,
533 :     init = S.ConstExpr
534 :     }
535 :     in
536 :     inputs' := inp :: !inputs';
537 :     constInit := S.S_Assign(x', e') :: (stms @ !constInit)
538 :     end
539 :     fun simplifyGlobalDcl (AST.D_Var(x, optE)) = let
540 :     val x' = cvtVar x
541 :     in
542 : jhr 3452 case optE
543 : jhr 3456 of NONE => globals' := x' :: !globals'
544 : jhr 3452 | SOME e => let
545 : jhr 3456 val (stms, e') = simplifyExp (errStrm, e, [])
546 : jhr 3452 in
547 :     globals' := x' :: !globals';
548 : jhr 3456 globalInit := S.S_Assign(x', e') :: (stms @ !globalInit)
549 : jhr 3452 end
550 :     (* end case *)
551 :     end
552 : jhr 3456 | simplifyGlobalDcl (AST.D_Func(f, params, body)) = let
553 :     val f' = cvtVar f
554 :     val params' = cvtVars params
555 :     val body' = pruneUnreachableCode (simplifyBlock errStrm body)
556 : jhr 3452 in
557 : jhr 3456 funcs := S.Func{f=f', params=params', body=body'} :: !funcs
558 : jhr 3452 end
559 : jhr 3995 val () = (
560 :     List.app simplifyConstDcl const_dcls;
561 :     List.app simplifyInputDcl input_dcls;
562 :     List.app simplifyGlobalDcl globals)
563 :     (* make the global-initialization block *)
564 :     val globInit = (case globInit
565 :     of SOME stm => mkBlock (simplifyStmt (errStrm, stm, !globalInit))
566 :     | NONE => mkBlock (!globalInit)
567 :     (* end case *))
568 : jhr 3452 in
569 :     S.Program{
570 :     props = props,
571 : jhr 3456 consts = List.rev(!consts'),
572 : jhr 3452 inputs = List.rev(!inputs'),
573 : jhr 3456 constInit = mkBlock (!constInit),
574 : jhr 3452 globals = List.rev(!globals'),
575 : jhr 3995 globInit = globInit,
576 : jhr 3452 funcs = List.rev(!funcs),
577 : jhr 3456 strand = simplifyStrand (errStrm, strand),
578 : jhr 4045 create = Create.map (simplifyBlock errStrm) create,
579 : jhr 3995 init = Option.map (simplifyBlock errStrm) init,
580 : jhr 3456 update = Option.map (simplifyBlock errStrm) update
581 : jhr 3452 }
582 :     end
583 :    
584 : jhr 3437 end

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