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

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