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

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