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

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