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 4317 - (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 : jhr 4317 * - types are simplified by removing meta variables (which will have been resolved)
11 : jhr 3468 *
12 : jhr 4317 * - expressions are simplified to involve a single operation on variables
13 : jhr 3468 *
14 : jhr 4317 * - global reductions are converted to MapReduce statements
15 : jhr 3468 *
16 : jhr 4317 * - other comprehensions and reductions are converted to foreach loops
17 : jhr 3468 *
18 : jhr 4317 * - unreachable code is pruned
19 : jhr 3468 *
20 : jhr 4317 * - 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 4317 | 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 : jhr 4317 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 4317 | 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 : jhr 4317 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 :     | cvtTy (STy.T_Image info) =
68 :     APITypes.ImageTy(II.dim info, II.voxelShape info)
69 :     | cvtTy ty = raise Fail "bogus API type"
70 :     in
71 :     cvtTy (SimpleVar.typeOf x)
72 :     end
73 : jhr 3811
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 : jhr 4317 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 : jhr 4163 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 : jhr 4317 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 : jhr 4310 (* FIXME: we should also define a "boolean negate" operation on AST expressions so that we can
143 : jhr 4254 * handle both cases!
144 :     *)
145 :     | AST.S_IfThenElse(AST.E_Orelse(e1, e2), s1 as AST.S_Block[], s2) =>
146 : jhr 4317 simplifyStmt (errStrm, AST.S_IfThenElse(e1, s1, AST.S_IfThenElse(e2, s1, s2)), stms)
147 : jhr 4254 | AST.S_IfThenElse(AST.E_Andalso(e1, e2), s1, s2 as AST.S_Block[]) =>
148 : jhr 4317 simplifyStmt (errStrm, AST.S_IfThenElse(e1, AST.S_IfThenElse(e2, s1, s2), s2), stms)
149 : jhr 3437 | AST.S_IfThenElse(e, s1, s2) => let
150 : jhr 3456 val (stms, x) = simplifyExpToVar (errStrm, e, stms)
151 : jhr 4113 val s1 = simplifyBlock (errStrm, s1)
152 :     val s2 = simplifyBlock (errStrm, s2)
153 : jhr 3437 in
154 : jhr 3456 S.S_IfThenElse(x, s1, s2) :: stms
155 : jhr 3437 end
156 : jhr 4317 | AST.S_Foreach((x, e), body) => let
157 :     val (stms, xs') = simplifyExpToVar (errStrm, e, stms)
158 :     val body' = simplifyBlock (errStrm, body)
159 :     in
160 :     S.S_Foreach(cvtVar x, xs', body') :: stms
161 :     end
162 : jhr 3452 | AST.S_Assign((x, _), e) => let
163 : jhr 3456 val (stms, e') = simplifyExp (errStrm, e, stms)
164 : jhr 4193 val x' = cvtLHS (x, e')
165 : jhr 3437 in
166 : jhr 4193 S.S_Assign(x', e') :: stms
167 : jhr 3437 end
168 :     | AST.S_New(name, args) => let
169 : jhr 3456 val (stms, xs) = simplifyExpsToVars (errStrm, args, stms)
170 : jhr 3437 in
171 : jhr 3456 S.S_New(name, xs) :: stms
172 : jhr 3437 end
173 : jhr 3456 | AST.S_Continue => S.S_Continue :: stms
174 :     | AST.S_Die => S.S_Die :: stms
175 :     | AST.S_Stabilize => S.S_Stabilize :: stms
176 : jhr 3437 | AST.S_Return e => let
177 : jhr 3456 val (stms, x) = simplifyExpToVar (errStrm, e, stms)
178 : jhr 3437 in
179 : jhr 3456 S.S_Return x :: stms
180 : jhr 3437 end
181 :     | AST.S_Print args => let
182 : jhr 3456 val (stms, xs) = simplifyExpsToVars (errStrm, args, stms)
183 : jhr 3437 in
184 : jhr 3456 S.S_Print xs :: stms
185 : jhr 3437 end
186 :     (* end case *))
187 :    
188 : jhr 3456 and simplifyExp (errStrm, exp, stms) = let
189 : jhr 4317 fun doBorderCtl (f, args) = let
190 :     val (ctl, arg) = if Var.same(BasisVars.image_border, f)
191 :     then (BorderCtl.Default(hd args), hd(tl args))
192 :     else if Var.same(BasisVars.image_clamp, f)
193 :     then (BorderCtl.Clamp, hd args)
194 :     else if Var.same(BasisVars.image_mirror, f)
195 :     then (BorderCtl.Mirror, hd args)
196 :     else if Var.same(BasisVars.image_wrap, f)
197 :     then (BorderCtl.Wrap, hd args)
198 :     else raise Fail "impossible"
199 :     in
200 :     S.E_BorderCtl(ctl, arg)
201 :     end
202 :     fun doPrimApply (f, tyArgs, args, ty) = let
203 :     val (stms, xs) = simplifyExpsToVars (errStrm, args, stms)
204 :     in
205 :     if Basis.isBorderCtl f
206 :     then (stms, doBorderCtl (f, xs))
207 :     else (case Var.kindOf f
208 :     of Var.BasisVar => let
209 :     fun cvtTyArg (Types.TYPE tv) = S.TY(cvtTy(TU.resolve tv))
210 :     | cvtTyArg (Types.DIFF dv) = S.DIFF(TU.monoDiff(TU.resolveDiff dv))
211 :     | cvtTyArg (Types.SHAPE sv) = S.SHAPE(TU.monoShape(TU.resolveShape sv))
212 :     | cvtTyArg (Types.DIM dv) = S.DIM(TU.monoDim(TU.resolveDim dv))
213 :     val tyArgs = List.map cvtTyArg tyArgs
214 :     in
215 :     (stms, S.E_Prim(f, tyArgs, xs, cvtTy ty))
216 :     end
217 :     | _ => raise Fail "bogus prim application"
218 :     (* end case *))
219 :     end
220 :     in
221 :     case exp
222 :     of AST.E_Var(x, _) => (case Var.kindOf x
223 :     of Var.BasisVar => let
224 :     val ty = cvtTy(Var.monoTypeOf x)
225 :     val x' = newTemp ty
226 :     val stm = S.S_Var(x', SOME(S.E_Prim(x, [], [], ty)))
227 :     in
228 :     (stm::stms, S.E_Var x')
229 :     end
230 :     | _ => (stms, S.E_Var(cvtVar x))
231 :     (* end case *))
232 :     | AST.E_Lit lit => (stms, S.E_Lit lit)
233 :     | AST.E_Kernel h => (stms, S.E_Kernel h)
234 :     | AST.E_Select(e, (fld, _)) => let
235 :     val (stms, x) = simplifyExpToVar (errStrm, e, stms)
236 :     in
237 :     (stms, S.E_Select(x, cvtVar fld))
238 :     end
239 :     | AST.E_Prim(rator, tyArgs, args as [e], ty) => (case e
240 :     of AST.E_Lit(Literal.Int n) => if Var.same(BasisVars.neg_i, rator)
241 :     then (stms, S.E_Lit(Literal.Int(~n))) (* constant-fold negation of integer literals *)
242 :     else doPrimApply (rator, tyArgs, args, ty)
243 :     | AST.E_Lit(Literal.Real f) =>
244 :     if Var.same(BasisVars.neg_t, rator)
245 :     then (stms, S.E_Lit(Literal.Real(RealLit.negate f))) (* constant-fold negation of real literals *)
246 :     else doPrimApply (rator, tyArgs, args, ty)
247 :     | AST.E_Comprehension(e', (x, e''), seqTy) => if Basis.isReductionOp rator
248 :     then let
249 :     val {rator, init, mvs} = Util.reductionInfo rator
250 :     val (stms, xs) = simplifyExpToVar (errStrm, e'', stms)
251 :     val (bodyStms, bodyResult) = simplifyExpToVar (errStrm, e, [])
252 :     val acc = SimpleVar.new ("accum", Var.LocalVar, cvtTy ty)
253 :     val seqTy' as STy.T_Sequence(elemTy, NONE) = cvtTy seqTy
254 :     val initStm = S.S_Var(acc, SOME(S.E_Lit init))
255 :     val updateStm = S.S_Assign(acc,
256 :     S.E_Prim(rator, mvs, [acc, bodyResult], seqTy'))
257 :     val foreachStm = S.S_Foreach(cvtVar x, xs, mkBlock(updateStm :: bodyStms))
258 :     in
259 :     (foreachStm :: initStm :: stms, S.E_Var acc)
260 :     end
261 :     else doPrimApply (rator, tyArgs, args, ty)
262 :     | AST.E_ParallelMap(e', x, xs, _) =>
263 :     if Basis.isReductionOp rator
264 :     then let
265 :     (* parallel map-reduce *)
266 :     val result = SimpleVar.new ("res", Var.LocalVar, cvtTy ty)
267 :     val (bodyStms, bodyResult) = simplifyExpToVar (errStrm, e', [])
268 :     val (func, args) = Util.makeFunction(
269 :     Var.nameOf rator, mkBlock(S.S_Return bodyResult :: bodyStms),
270 :     SimpleVar.typeOf bodyResult)
271 :     val mapReduceStm = S.S_MapReduce{
272 :     results = [result],
273 :     reductions = [rator],
274 :     body = func,
275 :     args = args,
276 :     source = xs
277 :     }
278 :     in
279 :     (mapReduceStm :: stms, S.E_Var result)
280 :     end
281 :     else raise Fail "unsupported operation on parallel map"
282 :     | _ => doPrimApply (rator, tyArgs, args, ty)
283 :     (* end case *))
284 :     | AST.E_Prim(f, tyArgs, args, ty) => doPrimApply (f, tyArgs, args, ty)
285 :     | AST.E_Apply((f, _), args, ty) => let
286 :     val (stms, xs) = simplifyExpsToVars (errStrm, args, stms)
287 :     in
288 :     case Var.kindOf f
289 :     of Var.FunVar => (stms, S.E_Apply(SimpleFunc.use(cvtFunc f), xs))
290 :     | _ => raise Fail "bogus application"
291 :     (* end case *)
292 :     end
293 :     | AST.E_Comprehension(e, (x, e'), seqTy) => let
294 :     (* convert a comprehension to a foreach loop over the sequence defined by e' *)
295 :     val (stms, xs) = simplifyExpToVar (errStrm, e', stms)
296 :     val (bodyStms, bodyResult) = simplifyExpToVar (errStrm, e, [])
297 :     val seqTy' as STy.T_Sequence(elemTy, NONE) = cvtTy seqTy
298 :     val acc = SimpleVar.new ("accum", Var.LocalVar, seqTy')
299 :     val initStm = S.S_Var(acc, SOME(S.E_Seq([], seqTy')))
300 :     val updateStm = S.S_Assign(acc,
301 :     S.E_Prim(BasisVars.at_dT, [S.TY elemTy], [acc, bodyResult], seqTy'))
302 :     val foreachStm = S.S_Foreach(cvtVar x, xs, mkBlock(updateStm :: bodyStms))
303 :     in
304 :     (foreachStm :: initStm :: stms, S.E_Var acc)
305 :     end
306 :     | AST.E_ParallelMap(e, x, xs, ty) => raise Fail "FIXME: ParallelMap"
307 :     | AST.E_Tensor(es, ty) => let
308 :     val (stms, xs) = simplifyExpsToVars (errStrm, es, stms)
309 :     in
310 :     (stms, S.E_Tensor(xs, cvtTy ty))
311 :     end
312 :     | AST.E_Seq(es, ty) => let
313 :     val (stms, xs) = simplifyExpsToVars (errStrm, es, stms)
314 :     in
315 :     (stms, S.E_Seq(xs, cvtTy ty))
316 :     end
317 :     | AST.E_Slice(e, indices, ty) => let (* tensor slicing *)
318 :     val (stms, x) = simplifyExpToVar (errStrm, e, stms)
319 :     fun f NONE = NONE
320 :     | f (SOME(AST.E_Lit(Literal.Int i))) = SOME(Int.fromLarge i)
321 :     | f _ = raise Fail "expected integer literal in slice"
322 :     val indices = List.map f indices
323 :     in
324 :     (stms, S.E_Slice(x, indices, cvtTy ty))
325 :     end
326 :     | AST.E_Cond(e1, e2, e3, ty) => let
327 :     (* a conditional expression gets turned into an if-then-else statememt *)
328 :     val result = newTemp(cvtTy ty)
329 :     val (stms, x) = simplifyExpToVar (errStrm, e1, S.S_Var(result, NONE) :: stms)
330 :     fun simplifyBranch e = let
331 :     val (stms, e) = simplifyExp (errStrm, e, [])
332 :     in
333 :     mkBlock (S.S_Assign(result, e)::stms)
334 :     end
335 :     val s1 = simplifyBranch e2
336 :     val s2 = simplifyBranch e3
337 :     in
338 :     (S.S_IfThenElse(x, s1, s2) :: stms, S.E_Var result)
339 :     end
340 :     | AST.E_Orelse(e1, e2) => simplifyExp (
341 :     errStrm,
342 :     AST.E_Cond(e1, AST.E_Lit(Literal.Bool true), e2, Ty.T_Bool),
343 :     stms)
344 :     | AST.E_Andalso(e1, e2) => simplifyExp (
345 :     errStrm,
346 :     AST.E_Cond(e1, e2, AST.E_Lit(Literal.Bool false), Ty.T_Bool),
347 :     stms)
348 :     | AST.E_LoadNrrd(_, nrrd, ty) => (case cvtTy ty
349 :     of ty as STy.T_Sequence(_, NONE) => (stms, S.E_LoadSeq(ty, nrrd))
350 :     | ty as STy.T_Image info => let
351 :     val dim = II.dim info
352 :     val shape = II.voxelShape info
353 :     in
354 :     case NrrdInfo.getInfo (errStrm, nrrd)
355 :     of SOME nrrdInfo => (case II.fromNrrd(nrrdInfo, dim, shape)
356 :     of NONE => (
357 :     Error.error (errStrm, [
358 :     "nrrd file \"", nrrd, "\" does not have expected type"
359 :     ]);
360 :     (stms, S.E_LoadImage(ty, nrrd, II.mkInfo(dim, shape))))
361 :     | SOME imgInfo =>
362 :     (stms, S.E_LoadImage(STy.T_Image imgInfo, nrrd, imgInfo))
363 :     (* end case *))
364 :     | NONE => (
365 :     Error.warning (errStrm, [
366 :     "nrrd file \"", nrrd, "\" does not exist"
367 :     ]);
368 :     (stms, S.E_LoadImage(ty, nrrd, II.mkInfo(dim, shape))))
369 :     (* end case *)
370 :     end
371 :     | _ => raise Fail "bogus type for E_LoadNrrd"
372 :     (* end case *))
373 :     | AST.E_Coerce{dstTy, e=AST.E_Lit(Literal.Int n), ...} => (case cvtTy dstTy
374 :     of SimpleTypes.T_Tensor[] => (stms, S.E_Lit(Literal.Real(RealLit.fromInt n)))
375 :     | _ => raise Fail "impossible: bad coercion"
376 :     (* end case *))
377 :     | AST.E_Coerce{srcTy, dstTy, e} => let
378 :     val (stms, x) = simplifyExpToVar (errStrm, e, stms)
379 :     val dstTy = cvtTy dstTy
380 :     val result = newTemp dstTy
381 :     val rhs = S.E_Coerce{srcTy = cvtTy srcTy, dstTy = dstTy, x = x}
382 :     in
383 :     (S.S_Var(result, SOME rhs)::stms, S.E_Var result)
384 :     end
385 :     (* end case *)
386 :     end
387 : jhr 3437
388 : jhr 3456 and simplifyExpToVar (errStrm, exp, stms) = let
389 :     val (stms, e) = simplifyExp (errStrm, exp, stms)
390 : jhr 3437 in
391 :     case e
392 :     of S.E_Var x => (stms, x)
393 :     | _ => let
394 :     val x = newTemp (S.typeOf e)
395 :     in
396 : jhr 3465 (S.S_Var(x, SOME e)::stms, x)
397 : jhr 3437 end
398 :     (* end case *)
399 :     end
400 :    
401 : jhr 3456 and simplifyExpsToVars (errStrm, exps, stms) = let
402 : jhr 3437 fun f ([], xs, stms) = (stms, List.rev xs)
403 :     | f (e::es, xs, stms) = let
404 : jhr 3456 val (stms, x) = simplifyExpToVar (errStrm, e, stms)
405 : jhr 3437 in
406 :     f (es, x::xs, stms)
407 :     end
408 :     in
409 :     f (exps, [], stms)
410 :     end
411 :    
412 : jhr 4113 (* simplify a block and then prune unreachable and dead code *)
413 :     fun simplifyAndPruneBlock errStrm blk =
414 : jhr 4317 DeadCode.eliminate (simplifyBlock (errStrm, blk))
415 : jhr 4113
416 : jhr 3995 fun simplifyStrand (errStrm, strand) = let
417 : jhr 4317 val AST.Strand{name, params, state, stateInit, initM, updateM, stabilizeM} = strand
418 : jhr 3456 val params' = cvtVars params
419 :     fun simplifyState ([], xs, stms) = (List.rev xs, mkBlock stms)
420 :     | simplifyState ((x, optE) :: r, xs, stms) = let
421 :     val x' = cvtVar x
422 : jhr 4317 in
423 :     case optE
424 :     of NONE => simplifyState (r, x'::xs, stms)
425 :     | SOME e => let
426 :     val (stms, e') = simplifyExp (errStrm, e, stms)
427 :     in
428 :     simplifyState (r, x'::xs, S.S_Assign(x', e') :: stms)
429 :     end
430 :     (* end case *)
431 :     end
432 : jhr 3456 val (xs, stm) = simplifyState (state, [], [])
433 : jhr 3437 in
434 : jhr 3452 S.Strand{
435 :     name = name,
436 :     params = params',
437 : jhr 3456 state = xs,
438 : jhr 4317 stateInit = stm,
439 :     initM = Option.map (simplifyAndPruneBlock errStrm) initM,
440 :     updateM = simplifyAndPruneBlock errStrm updateM,
441 :     stabilizeM = Option.map (simplifyAndPruneBlock errStrm) stabilizeM
442 : jhr 3452 }
443 : jhr 3437 end
444 :    
445 : jhr 3452 fun transform (errStrm, prog) = let
446 : jhr 4317 val AST.Program{
447 :     props, const_dcls, input_dcls, globals, globInit, strand, create, init, update
448 :     } = prog
449 :     val consts' = ref[]
450 :     val constInit = ref[]
451 :     val inputs' = ref[]
452 :     val globals' = ref[]
453 :     val globalInit = ref[]
454 :     val funcs = ref[]
455 :     fun simplifyConstDcl (x, SOME e) = let
456 :     val (stms, e') = simplifyExp (errStrm, e, [])
457 :     val x' = cvtVar x
458 :     in
459 :     consts' := x' :: !consts';
460 :     constInit := S.S_Assign(x', e') :: (stms @ !constInit)
461 :     end
462 :     fun simplifyInputDcl ((x, NONE), desc) = let
463 :     val x' = cvtVar x
464 :     val init = (case SimpleVar.typeOf x'
465 :     of STy.T_Image info => S.Image info
466 :     | _ => S.NoDefault
467 :     (* end case *))
468 :     val inp = S.INP{
469 :     var = x',
470 :     name = Var.nameOf x,
471 :     ty = apiTypeOf x',
472 :     desc = desc,
473 :     init = init
474 :     }
475 :     in
476 :     inputs' := inp :: !inputs'
477 :     end
478 :     | simplifyInputDcl ((x, SOME(AST.E_LoadNrrd(tvs, nrrd, ty))), desc) = let
479 :     val (x', init) = (case Var.monoTypeOf x
480 :     of Ty.T_Sequence(_, NONE) => (cvtVar x, S.LoadSeq nrrd)
481 :     | Ty.T_Image{dim, shape} => let
482 :     val dim = TU.monoDim dim
483 :     val shape = TU.monoShape shape
484 :     in
485 :     case NrrdInfo.getInfo (errStrm, nrrd)
486 :     of SOME nrrdInfo => (case II.fromNrrd(nrrdInfo, dim, shape)
487 :     of NONE => (
488 :     Error.error (errStrm, [
489 :     "proxy nrrd file \"", nrrd,
490 :     "\" does not have expected type"
491 :     ]);
492 :     (cvtVar x, S.Image(II.mkInfo(dim, shape))))
493 :     | SOME info =>
494 :     (newVarWithType(x, STy.T_Image info), S.Proxy(nrrd, info))
495 :     (* end case *))
496 :     | NONE => (
497 :     Error.warning (errStrm, [
498 :     "proxy nrrd file \"", nrrd, "\" does not exist"
499 :     ]);
500 :     (cvtVar x, S.Image(II.mkInfo(dim, shape))))
501 :     (* end case *)
502 :     end
503 :     | _ => raise Fail "impossible"
504 :     (* end case *))
505 :     val inp = S.INP{
506 :     var = x',
507 :     name = Var.nameOf x,
508 :     ty = apiTypeOf x',
509 :     desc = desc,
510 :     init = init
511 :     }
512 :     in
513 :     inputs' := inp :: !inputs'
514 :     end
515 :     | simplifyInputDcl ((x, SOME e), desc) = let
516 :     val x' = cvtVar x
517 :     val (stms, e') = simplifyExp (errStrm, e, [])
518 :     val inp = S.INP{
519 :     var = x',
520 :     name = Var.nameOf x,
521 :     ty = apiTypeOf x',
522 :     desc = desc,
523 :     init = S.ConstExpr
524 :     }
525 :     in
526 :     inputs' := inp :: !inputs';
527 :     constInit := S.S_Assign(x', e') :: (stms @ !constInit)
528 :     end
529 :     fun simplifyGlobalDcl (AST.D_Var(x, NONE)) = globals' := cvtVar x :: !globals'
530 :     | simplifyGlobalDcl (AST.D_Var(x, SOME e)) = let
531 :     val (stms, e') = simplifyExp (errStrm, e, [])
532 :     val x' = cvtLHS (x, e')
533 :     in
534 :     globals' := x' :: !globals';
535 :     globalInit := S.S_Assign(x', e') :: (stms @ !globalInit)
536 :     end
537 :     | simplifyGlobalDcl (AST.D_Func(f, params, body)) = let
538 :     val f' = cvtFunc f
539 :     val params' = cvtVars params
540 :     val body' = simplifyAndPruneBlock errStrm body
541 :     in
542 :     funcs := S.Func{f=f', params=params', body=body'} :: !funcs
543 :     end
544 :     val () = (
545 :     List.app simplifyConstDcl const_dcls;
546 :     List.app simplifyInputDcl input_dcls;
547 :     List.app simplifyGlobalDcl globals)
548 :     (* make the global-initialization block *)
549 :     val globInit = (case globInit
550 :     of SOME stm => mkBlock (simplifyStmt (errStrm, stm, !globalInit))
551 :     | NONE => mkBlock (!globalInit)
552 :     (* end case *))
553 :     (* if the globInit block is non-empty, record the fact in the property list *)
554 :     val props = (case globInit
555 :     of S.Block{code=[], ...} => props
556 :     | _ => Properties.GlobalInit :: props
557 :     (* end case *))
558 : jhr 3452 in
559 :     S.Program{
560 :     props = props,
561 : jhr 4317 consts = List.rev(!consts'),
562 : jhr 3452 inputs = List.rev(!inputs'),
563 : jhr 4317 constInit = mkBlock (!constInit),
564 : jhr 3452 globals = List.rev(!globals'),
565 : jhr 3995 globInit = globInit,
566 : jhr 3452 funcs = List.rev(!funcs),
567 : jhr 3456 strand = simplifyStrand (errStrm, strand),
568 : jhr 4113 create = Create.map (simplifyAndPruneBlock errStrm) create,
569 : jhr 4317 init = Option.map (simplifyAndPruneBlock errStrm) init,
570 :     update = Option.map (simplifyAndPruneBlock errStrm) update
571 : jhr 3452 }
572 :     end
573 :    
574 : jhr 3437 end

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