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

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