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

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