Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] Annotation of /branches/vis12/src/compiler/simplify/simplify.sml
ViewVC logotype

Annotation of /branches/vis12/src/compiler/simplify/simplify.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1992 - (view) (download)

1 : jhr 171 (* simplify.sml
2 :     *
3 : jhr 435 * COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu)
4 : jhr 171 * All rights reserved.
5 :     *
6 :     * Simplify the AST representation.
7 :     *)
8 :    
9 :     structure Simplify : sig
10 :    
11 : jhr 1140 val transform : Error.err_stream * AST.program -> Simple.program
12 : jhr 171
13 :     end = struct
14 :    
15 :     structure Ty = Types
16 :     structure S = Simple
17 :    
18 :     local
19 :     val tempName = Atom.atom "_t"
20 :     in
21 :     fun newTemp ty = Var.new (tempName, AST.LocalVar, ty)
22 :     end
23 :    
24 :     (* make a block out of a list of statements that are in reverse order *)
25 : jhr 197 fun mkBlock stms = S.Block(List.rev stms)
26 : jhr 171
27 : jhr 227 fun simplifyProgram (AST.Program dcls) = let
28 : jhr 171 val globals = ref []
29 :     val globalInit = ref []
30 : jhr 1116 val initially = ref NONE
31 : jhr 511 val strands = ref []
32 : jhr 1116 fun setInitially init = (case !initially
33 :     of NONE => initially := SOME init
34 :     (* FIXME: the check for multiple initially decls should happen in type checking *)
35 :     | SOME _ => raise Fail "multiple initially declarations"
36 :     (* end case *))
37 : jhr 171 fun simplifyDecl dcl = (case dcl
38 : jhr 1301 of AST.D_Input(x, desc, NONE) => let
39 :     val e' = S.E_Input(Var.monoTypeOf x, Var.nameOf x, desc, NONE)
40 : jhr 185 in
41 :     globals := x :: !globals;
42 : jhr 221 globalInit := S.S_Assign(x, e') :: !globalInit
43 : jhr 185 end
44 : jhr 1992 | AST.D_Input(x, desc, SOME(AST.E_LoadNrrd(tvs, nrrd, ty))) => let
45 :     (* load the nrrd prox here *)
46 :     val info = NrrdInfo.getInfo filename
47 :     val stm = (case TypeUtil.pruneHead ty
48 :     of Ty.T_DynSequence _ => (* FIXME *) raise Fail "unimplemented"
49 :     | Ty.T_Image _ => (* FIXME *) raise Fail "unimplemented"
50 :     | _ => raise Fail "impossible"
51 :     (* end case *))
52 :     in
53 :     globals := x :: !globals;
54 :     globalInit := stm :: !globalInit
55 :     end
56 : jhr 1301 | AST.D_Input(x, desc, SOME e) => let
57 : jhr 179 val (stms, x') = simplifyExpToVar (e, [])
58 : jhr 1301 val e' = S.E_Input(Var.monoTypeOf x, Var.nameOf x, desc, SOME x')
59 : jhr 171 in
60 :     globals := x :: !globals;
61 : jhr 221 globalInit := S.S_Assign(x, e') :: (stms @ !globalInit)
62 : jhr 171 end
63 :     | AST.D_Var(AST.VD_Decl(x, e)) => let
64 :     val (stms, e') = simplifyExp (e, [])
65 :     in
66 :     globals := x :: !globals;
67 :     globalInit := S.S_Assign(x, e') :: (stms @ !globalInit)
68 :     end
69 : jhr 511 | AST.D_Strand info => strands := simplifyStrand info :: !strands
70 : jhr 1116 | AST.D_InitialArray(creat, iters) =>
71 :     setInitially (simplifyInit(true, creat, iters))
72 :     | AST.D_InitialCollection(creat, iters) =>
73 :     setInitially (simplifyInit(false, creat, iters))
74 : jhr 171 (* end case *))
75 :     in
76 :     List.app simplifyDecl dcls;
77 : jhr 175 S.Program{
78 : jhr 171 globals = List.rev(!globals),
79 :     globalInit = mkBlock (!globalInit),
80 : jhr 1116 init = (case !initially
81 :     (* FIXME: the check for the initially block should really happen in typechecking *)
82 :     of NONE => raise Fail "missing initially declaration"
83 :     | SOME blk => blk
84 :     (* end case *)),
85 : jhr 511 strands = List.rev(!strands)
86 : jhr 171 }
87 :     end
88 :    
89 : jhr 1116 and simplifyInit (isArray, AST.C_Create(strand, exps), iters) = let
90 :     val (stms, xs) = simplifyExpsToVars (exps, [])
91 :     val creat = S.C_Create{
92 :     argInit = mkBlock stms,
93 :     name = strand,
94 :     args = xs
95 :     }
96 :     fun simplifyIter (AST.I_Range(x, e1, e2), (iters, stms)) = let
97 :     val (stms, lo) = simplifyExpToVar (e1, stms)
98 :     val (stms, hi) = simplifyExpToVar (e2, stms)
99 :     in
100 :     ({param=x, lo=lo, hi=hi}::iters, stms)
101 :     end
102 :     val (iters, stms) = List.foldl simplifyIter ([], []) iters
103 :     in
104 :     S.Initially{
105 :     isArray = isArray,
106 :     rangeInit = mkBlock stms,
107 :     iters = List.rev iters,
108 :     create = creat
109 :     }
110 :     end
111 :    
112 : jhr 511 and simplifyStrand {name, params, state, methods} = let
113 : jhr 171 fun simplifyState ([], xs, stms) = (List.rev xs, mkBlock stms)
114 :     | simplifyState (AST.VD_Decl(x, e) :: r, xs, stms) = let
115 :     val (stms, e') = simplifyExp (e, stms)
116 :     in
117 :     simplifyState (r, x::xs, S.S_Assign(x, e') :: stms)
118 :     end
119 :     val (xs, stm) = simplifyState (state, [], [])
120 :     in
121 : jhr 511 S.Strand{
122 : jhr 173 name = name,
123 : jhr 171 params = params,
124 :     state = xs, stateInit = stm,
125 :     methods = List.map simplifyMethod methods
126 :     }
127 :     end
128 :    
129 :     and simplifyMethod (AST.M_Method(name, body)) =
130 : jhr 197 S.Method(name, simplifyBlock body)
131 : jhr 171
132 : jhr 1116 (* simplify a statement into a single statement (i.e., a block if it expands
133 :     * into more than one new statement).
134 : jhr 171 *)
135 :     and simplifyBlock stm = mkBlock (simplifyStmt (stm, []))
136 :    
137 :     and simplifyStmt (stm, stms) = (case stm
138 :     of AST.S_Block body => let
139 :     fun simplify ([], stms) = stms
140 :     | simplify (stm::r, stms) = simplify (r, simplifyStmt (stm, stms))
141 :     in
142 :     simplify (body, stms)
143 :     end
144 :     | AST.S_Decl(AST.VD_Decl(x, e)) => let
145 :     val (stms, e') = simplifyExp (e, stms)
146 :     in
147 :     S.S_Assign(x, e') :: stms
148 :     end
149 :     | AST.S_IfThenElse(e, s1, s2) => let
150 :     val (stms, x) = simplifyExpToVar (e, stms)
151 :     val s1 = simplifyBlock s1
152 :     val s2 = simplifyBlock s2
153 :     in
154 :     S.S_IfThenElse(x, s1, s2) :: stms
155 :     end
156 :     | AST.S_Assign(x, e) => let
157 :     val (stms, e') = simplifyExp (e, stms)
158 :     in
159 :     S.S_Assign(x, e') :: stms
160 :     end
161 :     | AST.S_New(name, args) => let
162 :     val (stms, xs) = simplifyExpsToVars (args, stms)
163 :     in
164 :     S.S_New(name, xs) :: stms
165 :     end
166 :     | AST.S_Die => S.S_Die :: stms
167 :     | AST.S_Stabilize => S.S_Stabilize :: stms
168 : jhr 1640 | AST.S_Print args => let
169 :     val (stms, xs) = simplifyExpsToVars (args, stms)
170 :     in
171 :     S.S_Print xs :: stms
172 :     end
173 : jhr 171 (* end case *))
174 :    
175 :     and simplifyExp (exp, stms) = (
176 :     case exp
177 : jhr 197 of AST.E_Var x => (case Var.kindOf x
178 :     of Var.BasisVar => let
179 :     val ty = Var.monoTypeOf x
180 :     val x' = newTemp ty
181 :     val stm = S.S_Assign(x', S.E_Apply(x, [], [], ty))
182 :     in
183 :     (stm::stms, S.E_Var x')
184 :     end
185 :     | _ => (stms, S.E_Var x)
186 :     (* end case *))
187 : jhr 171 | AST.E_Lit lit => (stms, S.E_Lit lit)
188 :     | AST.E_Tuple es => raise Fail "E_Tuple not yet implemented"
189 :     | AST.E_Apply(f, tyArgs, args, ty) => let
190 :     val (stms, xs) = simplifyExpsToVars (args, stms)
191 :     in
192 :     (stms, S.E_Apply(f, tyArgs, xs, ty))
193 :     end
194 :     | AST.E_Cons es => let
195 :     val (stms, xs) = simplifyExpsToVars (es, stms)
196 :     in
197 :     (stms, S.E_Cons xs)
198 :     end
199 : jhr 1688 | AST.E_Seq es => let
200 :     val (stms, xs) = simplifyExpsToVars (es, stms)
201 :     in
202 :     (stms, S.E_Seq xs)
203 :     end
204 : jhr 399 | AST.E_Slice(e, indices, ty) => let (* tensor slicing *)
205 : jhr 395 val (stms, x) = simplifyExpToVar (e, stms)
206 : jhr 399 fun f ([], ys, stms) = (stms, List.rev ys)
207 :     | f (NONE::es, ys, stms) = f (es, NONE::ys, stms)
208 :     | f (SOME e::es, ys, stms) = let
209 :     val (stms, y) = simplifyExpToVar (e, stms)
210 :     in
211 :     f (es, SOME y::ys, stms)
212 :     end
213 :     val (stms, indices) = f (indices, [], stms)
214 : jhr 395 in
215 : jhr 399 (stms, S.E_Slice(x, indices, ty))
216 : jhr 395 end
217 : jhr 416 | AST.E_Cond(e1, e2, e3, ty) => let
218 : jhr 171 (* a conditional expression gets turned into an if-then-else statememt *)
219 : jhr 416 val result = newTemp ty
220 : jhr 1116 val (stms, x) = simplifyExpToVar (e1, S.S_Var result :: stms)
221 : jhr 171 fun simplifyBranch e = let
222 :     val (stms, e) = simplifyExp (e, [])
223 :     in
224 :     mkBlock (S.S_Assign(result, e)::stms)
225 :     end
226 : jhr 415 val s1 = simplifyBranch e2
227 :     val s2 = simplifyBranch e3
228 : jhr 171 in
229 :     (S.S_IfThenElse(x, s1, s2) :: stms, S.E_Var result)
230 :     end
231 : jhr 1687 | AST.E_Coerce{srcTy, dstTy, e} => let
232 :     val (stms, x) = simplifyExpToVar (e, stms)
233 :     val result = newTemp dstTy
234 :     in
235 :     (S.S_Assign(result, S.E_Coerce{srcTy=srcTy, dstTy=dstTy, x=x})::stms, S.E_Var result)
236 :     end
237 : jhr 171 (* end case *))
238 :    
239 :     and simplifyExpToVar (exp, stms) = let
240 :     val (stms, e) = simplifyExp (exp, stms)
241 :     in
242 :     case e
243 :     of S.E_Var x => (stms, x)
244 :     | _ => let
245 :     val x = newTemp (S.typeOf e)
246 :     in
247 :     (S.S_Assign(x, e)::stms, x)
248 :     end
249 :     (* end case *)
250 :     end
251 :    
252 :     and simplifyExpsToVars (exps, stms) = let
253 :     fun f ([], xs, stms) = (stms, List.rev xs)
254 :     | f (e::es, xs, stms) = let
255 :     val (stms, x) = simplifyExpToVar (e, stms)
256 :     in
257 :     f (es, x::xs, stms)
258 :     end
259 :     in
260 :     f (exps, [], stms)
261 :     end
262 :    
263 : jhr 1140 fun transform (errStrm, ast) = let
264 : jhr 227 val simple = simplifyProgram ast
265 : jhr 238 val _ = SimplePP.output (Log.logFile(), simple) (* DEBUG *)
266 : jhr 227 val simple = Lift.transform simple
267 : jhr 1140 handle Eval.Error msg => (Error.error(errStrm, msg); simple)
268 : jhr 227 in
269 :     simple
270 :     end
271 :    
272 : jhr 171 end

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