adamc@1272
|
1 (* Copyright (c) 2008-2010, Adam Chlipala
|
adamc@443
|
2 * All rights reserved.
|
adamc@443
|
3 *
|
adamc@443
|
4 * Redistribution and use in source and binary forms, with or without
|
adamc@443
|
5 * modification, are permitted provided that the following conditions are met:
|
adamc@443
|
6 *
|
adamc@443
|
7 * - Redistributions of source code must retain the above copyright notice,
|
adamc@443
|
8 * this list of conditions and the following disclaimer.
|
adamc@443
|
9 * - Redistributions in binary form must reproduce the above copyright notice,
|
adamc@443
|
10 * this list of conditions and the following disclaimer in the documentation
|
adamc@443
|
11 * and/or other materials provided with the distribution.
|
adamc@443
|
12 * - The names of contributors may not be used to endorse or promote products
|
adamc@443
|
13 * derived from this software without specific prior written permission.
|
adamc@443
|
14 *
|
adamc@443
|
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
adamc@443
|
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
adamc@443
|
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
adamc@443
|
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
adamc@443
|
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
adamc@443
|
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
adamc@443
|
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
adamc@443
|
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
adamc@443
|
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
adamc@443
|
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
adamc@443
|
25 * POSSIBILITY OF SUCH DAMAGE.
|
adamc@443
|
26 *)
|
adamc@443
|
27
|
adamc@443
|
28 structure ESpecialize :> ESPECIALIZE = struct
|
adamc@443
|
29
|
adamc@443
|
30 open Core
|
adamc@443
|
31
|
adamc@443
|
32 structure E = CoreEnv
|
adamc@443
|
33 structure U = CoreUtil
|
adamc@443
|
34
|
adamc@479
|
35 type skey = exp
|
adamc@453
|
36
|
adamc@453
|
37 structure K = struct
|
adam@1314
|
38 type ord_key = con list * exp list
|
adam@1314
|
39 fun compare ((cs1, es1), (cs2, es2)) = Order.join (Order.joinL U.Con.compare (cs1, cs2),
|
adam@1314
|
40 fn () => Order.joinL U.Exp.compare (es1, es2))
|
adamc@443
|
41 end
|
adamc@443
|
42
|
adamc@453
|
43 structure KM = BinaryMapFn(K)
|
adamc@443
|
44 structure IM = IntBinaryMap
|
adamc@482
|
45 structure IS = IntBinarySet
|
adamc@443
|
46
|
adamc@626
|
47 val freeVars = U.Exp.foldB {kind = fn (_, _, xs) => xs,
|
adamc@488
|
48 con = fn (_, _, xs) => xs,
|
adamc@488
|
49 exp = fn (bound, e, xs) =>
|
adamc@488
|
50 case e of
|
adamc@488
|
51 ERel x =>
|
adamc@488
|
52 if x >= bound then
|
adamc@488
|
53 IS.add (xs, x - bound)
|
adamc@488
|
54 else
|
adamc@488
|
55 xs
|
adamc@488
|
56 | _ => xs,
|
adamc@488
|
57 bind = fn (bound, b) =>
|
adamc@488
|
58 case b of
|
adamc@488
|
59 U.Exp.RelE _ => bound + 1
|
adamc@488
|
60 | _ => bound}
|
adamc@488
|
61 0 IS.empty
|
adamc@479
|
62
|
adamc@1120
|
63 fun isPolyT (t, _) =
|
adamc@1120
|
64 case t of
|
adamc@1120
|
65 TFun (_, ran) => isPolyT ran
|
adamc@1120
|
66 | TCFun _ => true
|
adamc@1120
|
67 | TKFun _ => true
|
adamc@1120
|
68 | _ => false
|
adamc@1120
|
69
|
adamc@1120
|
70 fun isPoly (d, _) =
|
adamc@1120
|
71 case d of
|
adamc@1120
|
72 DVal (_, _, t, _, _) => isPolyT t
|
adamc@1120
|
73 | DValRec vis => List.exists (isPolyT o #3) vis
|
adamc@1120
|
74 | _ => false
|
adamc@522
|
75
|
adamc@488
|
76 fun positionOf (v : int, ls) =
|
adamc@488
|
77 let
|
adamc@488
|
78 fun pof (pos, ls) =
|
adamc@488
|
79 case ls of
|
adamc@488
|
80 [] => raise Fail "Defunc.positionOf"
|
adamc@488
|
81 | v' :: ls' =>
|
adamc@488
|
82 if v = v' then
|
adamc@488
|
83 pos
|
adamc@488
|
84 else
|
adamc@488
|
85 pof (pos + 1, ls')
|
adamc@488
|
86 in
|
adamc@488
|
87 pof (0, ls)
|
adamc@488
|
88 end
|
adamc@488
|
89
|
adamc@1079
|
90 fun squish fvs =
|
adamc@626
|
91 U.Exp.mapB {kind = fn _ => fn k => k,
|
adamc@488
|
92 con = fn _ => fn c => c,
|
adamc@488
|
93 exp = fn bound => fn e =>
|
adamc@479
|
94 case e of
|
adamc@488
|
95 ERel x =>
|
adamc@488
|
96 if x >= bound then
|
adamc@1079
|
97 ERel (positionOf (x - bound, fvs) + bound)
|
adamc@488
|
98 else
|
adamc@488
|
99 e
|
adamc@488
|
100 | _ => e,
|
adamc@488
|
101 bind = fn (bound, b) =>
|
adamc@488
|
102 case b of
|
adamc@488
|
103 U.Exp.RelE _ => bound + 1
|
adamc@488
|
104 | _ => bound}
|
adamc@488
|
105 0
|
adamc@453
|
106
|
adamc@443
|
107 type func = {
|
adamc@443
|
108 name : string,
|
adamc@453
|
109 args : int KM.map,
|
adamc@443
|
110 body : exp,
|
adamc@443
|
111 typ : con,
|
adamc@443
|
112 tag : string
|
adamc@443
|
113 }
|
adamc@443
|
114
|
adamc@443
|
115 type state = {
|
adamc@443
|
116 maxName : int,
|
adamc@443
|
117 funcs : func IM.map,
|
adamc@1079
|
118 decls : (string * int * con * exp * string) list,
|
adamc@1080
|
119 specialized : IS.set
|
adamc@443
|
120 }
|
adamc@443
|
121
|
adamc@488
|
122 fun default (_, x, st) = (x, st)
|
adamc@443
|
123
|
adam@1289
|
124 val functionInside = U.Con.exists {kind = fn _ => false,
|
adam@1289
|
125 con = fn TFun _ => true
|
adam@1289
|
126 | CFfi ("Basis", "transaction") => true
|
adam@1289
|
127 | CFfi ("Basis", "eq") => true
|
adam@1289
|
128 | CFfi ("Basis", "num") => true
|
adam@1289
|
129 | CFfi ("Basis", "ord") => true
|
adam@1289
|
130 | CFfi ("Basis", "show") => true
|
adam@1289
|
131 | CFfi ("Basis", "read") => true
|
adam@1289
|
132 | CFfi ("Basis", "sql_injectable_prim") => true
|
adam@1289
|
133 | CFfi ("Basis", "sql_injectable") => true
|
adam@1289
|
134 | _ => false}
|
adam@1289
|
135
|
adamc@1080
|
136 fun specialize' (funcs, specialized) file =
|
adamc@443
|
137 let
|
adamc@488
|
138 fun bind (env, b) =
|
adamc@488
|
139 case b of
|
adamc@521
|
140 U.Decl.RelE xt => xt :: env
|
adamc@521
|
141 | _ => env
|
adamc@488
|
142
|
adamc@1080
|
143 fun exp (env, e as (_, loc), st : state) =
|
adamc@482
|
144 let
|
adamc@721
|
145 (*val () = Print.prefaces "exp" [("e", CorePrint.p_exp CoreEnv.empty
|
adamc@721
|
146 (e, ErrorMsg.dummySpan))]*)
|
adamc@721
|
147
|
adamc@1080
|
148 fun getApp (e, _) =
|
adamc@482
|
149 case e of
|
adamc@488
|
150 ENamed f => SOME (f, [])
|
adamc@482
|
151 | EApp (e1, e2) =>
|
adamc@1080
|
152 (case getApp e1 of
|
adamc@482
|
153 NONE => NONE
|
adamc@488
|
154 | SOME (f, xs) => SOME (f, xs @ [e2]))
|
adamc@482
|
155 | _ => NONE
|
adamc@1080
|
156
|
adamc@1080
|
157 val getApp = fn e => case getApp e of
|
adamc@1080
|
158 v as SOME (_, _ :: _) => v
|
adamc@1080
|
159 | _ => NONE
|
adamc@1080
|
160
|
adamc@1080
|
161 fun default () =
|
adamc@1080
|
162 case #1 e of
|
adamc@1080
|
163 EPrim _ => (e, st)
|
adamc@1080
|
164 | ERel _ => (e, st)
|
adamc@1080
|
165 | ENamed _ => (e, st)
|
adamc@1080
|
166 | ECon (_, _, _, NONE) => (e, st)
|
adamc@1080
|
167 | ECon (dk, pc, cs, SOME e) =>
|
adamc@1080
|
168 let
|
adamc@1080
|
169 val (e, st) = exp (env, e, st)
|
adamc@1080
|
170 in
|
adamc@1080
|
171 ((ECon (dk, pc, cs, SOME e), loc), st)
|
adamc@1080
|
172 end
|
adamc@1080
|
173 | EFfi _ => (e, st)
|
adamc@1080
|
174 | EFfiApp (m, x, es) =>
|
adamc@1080
|
175 let
|
adam@1663
|
176 val (es, st) = ListUtil.foldlMap (fn ((e, t), st) =>
|
adam@1663
|
177 let
|
adam@1663
|
178 val (e, st) = exp (env, e, st)
|
adam@1663
|
179 in
|
adam@1663
|
180 ((e, t), st)
|
adam@1663
|
181 end) st es
|
adamc@1080
|
182 in
|
adamc@1080
|
183 ((EFfiApp (m, x, es), loc), st)
|
adamc@1080
|
184 end
|
adamc@1080
|
185 | EApp (e1, e2) =>
|
adamc@1080
|
186 let
|
adamc@1080
|
187 val (e1, st) = exp (env, e1, st)
|
adamc@1080
|
188 val (e2, st) = exp (env, e2, st)
|
adamc@1080
|
189 in
|
adamc@1080
|
190 ((EApp (e1, e2), loc), st)
|
adamc@1080
|
191 end
|
adamc@1080
|
192 | EAbs (x, d, r, e) =>
|
adamc@1080
|
193 let
|
adamc@1080
|
194 val (e, st) = exp ((x, d) :: env, e, st)
|
adamc@1080
|
195 in
|
adamc@1080
|
196 ((EAbs (x, d, r, e), loc), st)
|
adamc@1080
|
197 end
|
adamc@1080
|
198 | ECApp (e, c) =>
|
adamc@1080
|
199 let
|
adamc@1080
|
200 val (e, st) = exp (env, e, st)
|
adamc@1080
|
201 in
|
adamc@1080
|
202 ((ECApp (e, c), loc), st)
|
adamc@1080
|
203 end
|
adamc@1185
|
204 | ECAbs _ => (e, st)
|
adamc@1120
|
205 | EKAbs _ => (e, st)
|
adamc@1080
|
206 | EKApp (e, k) =>
|
adamc@1080
|
207 let
|
adamc@1080
|
208 val (e, st) = exp (env, e, st)
|
adamc@1080
|
209 in
|
adamc@1080
|
210 ((EKApp (e, k), loc), st)
|
adamc@1080
|
211 end
|
adamc@1080
|
212 | ERecord fs =>
|
adamc@1080
|
213 let
|
adamc@1080
|
214 val (fs, st) = ListUtil.foldlMap (fn ((c1, e, c2), st) =>
|
adamc@1080
|
215 let
|
adamc@1080
|
216 val (e, st) = exp (env, e, st)
|
adamc@1080
|
217 in
|
adamc@1080
|
218 ((c1, e, c2), st)
|
adamc@1080
|
219 end) st fs
|
adamc@1080
|
220 in
|
adamc@1080
|
221 ((ERecord fs, loc), st)
|
adamc@1080
|
222 end
|
adamc@1080
|
223 | EField (e, c, cs) =>
|
adamc@1080
|
224 let
|
adamc@1080
|
225 val (e, st) = exp (env, e, st)
|
adamc@1080
|
226 in
|
adamc@1080
|
227 ((EField (e, c, cs), loc), st)
|
adamc@1080
|
228 end
|
adamc@1080
|
229 | EConcat (e1, c1, e2, c2) =>
|
adamc@1080
|
230 let
|
adamc@1080
|
231 val (e1, st) = exp (env, e1, st)
|
adamc@1080
|
232 val (e2, st) = exp (env, e2, st)
|
adamc@1080
|
233 in
|
adamc@1080
|
234 ((EConcat (e1, c1, e2, c2), loc), st)
|
adamc@1080
|
235 end
|
adamc@1080
|
236 | ECut (e, c, cs) =>
|
adamc@1080
|
237 let
|
adamc@1080
|
238 val (e, st) = exp (env, e, st)
|
adamc@1080
|
239 in
|
adamc@1080
|
240 ((ECut (e, c, cs), loc), st)
|
adamc@1080
|
241 end
|
adamc@1080
|
242 | ECutMulti (e, c, cs) =>
|
adamc@1080
|
243 let
|
adamc@1080
|
244 val (e, st) = exp (env, e, st)
|
adamc@1080
|
245 in
|
adamc@1080
|
246 ((ECutMulti (e, c, cs), loc), st)
|
adamc@1080
|
247 end
|
adamc@1080
|
248
|
adamc@1080
|
249 | ECase (e, pes, cs) =>
|
adamc@1080
|
250 let
|
adamc@1080
|
251 val (e, st) = exp (env, e, st)
|
adamc@1080
|
252 val (pes, st) = ListUtil.foldlMap (fn ((p, e), st) =>
|
adamc@1080
|
253 let
|
adamc@1080
|
254 val (e, st) = exp (E.patBindsL p @ env, e, st)
|
adamc@1080
|
255 in
|
adamc@1080
|
256 ((p, e), st)
|
adamc@1080
|
257 end) st pes
|
adamc@1080
|
258 in
|
adamc@1080
|
259 ((ECase (e, pes, cs), loc), st)
|
adamc@1080
|
260 end
|
adamc@1080
|
261
|
adamc@1080
|
262 | EWrite e =>
|
adamc@1080
|
263 let
|
adamc@1080
|
264 val (e, st) = exp (env, e, st)
|
adamc@1080
|
265 in
|
adamc@1080
|
266 ((EWrite e, loc), st)
|
adamc@1080
|
267 end
|
adamc@1080
|
268 | EClosure (n, es) =>
|
adamc@1080
|
269 let
|
adamc@1080
|
270 val (es, st) = ListUtil.foldlMap (fn (e, st) => exp (env, e, st)) st es
|
adamc@1080
|
271 in
|
adamc@1080
|
272 ((EClosure (n, es), loc), st)
|
adamc@1080
|
273 end
|
adamc@1080
|
274 | ELet (x, t, e1, e2) =>
|
adamc@1080
|
275 let
|
adamc@1080
|
276 val (e1, st) = exp (env, e1, st)
|
adamc@1080
|
277 val (e2, st) = exp ((x, t) :: env, e2, st)
|
adamc@1080
|
278 in
|
adamc@1080
|
279 ((ELet (x, t, e1, e2), loc), st)
|
adamc@1080
|
280 end
|
adamc@1080
|
281 | EServerCall (n, es, t) =>
|
adamc@1080
|
282 let
|
adamc@1080
|
283 val (es, st) = ListUtil.foldlMap (fn (e, st) => exp (env, e, st)) st es
|
adamc@1080
|
284 in
|
adamc@1080
|
285 ((EServerCall (n, es, t), loc), st)
|
adamc@1080
|
286 end
|
adamc@482
|
287 in
|
adamc@482
|
288 case getApp e of
|
adamc@1080
|
289 NONE => default ()
|
adamc@488
|
290 | SOME (f, xs) =>
|
adamc@485
|
291 case IM.find (#funcs st, f) of
|
adamc@1272
|
292 NONE => ((*print ("No find: " ^ Int.toString f ^ "\n");*) default ())
|
adamc@485
|
293 | SOME {name, args, body, typ, tag} =>
|
adamc@488
|
294 let
|
adamc@1080
|
295 val (xs, st) = ListUtil.foldlMap (fn (e, st) => exp (env, e, st)) st xs
|
adamc@1080
|
296
|
adamc@721
|
297 (*val () = Print.prefaces "Consider" [("e", CorePrint.p_exp CoreEnv.empty
|
adamc@721
|
298 (e, ErrorMsg.dummySpan))]*)
|
adamc@721
|
299
|
adamc@488
|
300 val loc = ErrorMsg.dummySpan
|
adamc@488
|
301
|
adamc@1080
|
302 fun findSplit av (xs, typ, fxs, fvs, fin) =
|
adamc@488
|
303 case (#1 typ, xs) of
|
adamc@488
|
304 (TFun (dom, ran), e :: xs') =>
|
adamc@1078
|
305 let
|
adamc@1079
|
306 val av = case #1 e of
|
adamc@1079
|
307 ERel _ => av
|
adamc@1079
|
308 | _ => false
|
adamc@1078
|
309 in
|
adamc@1079
|
310 if functionInside dom orelse (av andalso case #1 e of
|
adamc@1079
|
311 ERel _ => true
|
adamc@1079
|
312 | _ => false) then
|
adamc@1079
|
313 findSplit av (xs',
|
adamc@1079
|
314 ran,
|
adamc@1079
|
315 e :: fxs,
|
adamc@1080
|
316 IS.union (fvs, freeVars e),
|
adamc@1080
|
317 fin orelse functionInside dom)
|
adamc@1078
|
318 else
|
adamc@1080
|
319 (rev fxs, xs, fvs, fin)
|
adamc@1078
|
320 end
|
adamc@1080
|
321 | _ => (rev fxs, xs, fvs, fin)
|
adamc@488
|
322
|
adamc@1080
|
323 val (fxs, xs, fvs, fin) = findSplit true (xs, typ, [], IS.empty, false)
|
adamc@1079
|
324
|
adam@1382
|
325 fun valueish (all as (e, _)) =
|
adam@1355
|
326 case e of
|
adam@1355
|
327 EPrim _ => true
|
adam@1355
|
328 | ERel _ => true
|
adam@1355
|
329 | ENamed _ => true
|
adam@1355
|
330 | ECon (_, _, _, NONE) => true
|
adam@1355
|
331 | ECon (_, _, _, SOME e) => valueish e
|
adam@1355
|
332 | EFfi (_, _) => true
|
adam@1355
|
333 | EAbs _ => true
|
adam@1355
|
334 | ECAbs _ => true
|
adam@1355
|
335 | EKAbs _ => true
|
adam@1355
|
336 | ECApp (e, _) => valueish e
|
adam@1355
|
337 | EKApp (e, _) => valueish e
|
adam@1383
|
338 | EApp (e1, e2) => valueish e1 andalso valueish e2
|
adam@1355
|
339 | ERecord xes => List.all (valueish o #2) xes
|
adam@1383
|
340 | EField (e, _, _) => valueish e
|
adam@1355
|
341 | _ => false
|
adam@1355
|
342
|
adam@1314
|
343 val vts = map (fn n => #2 (List.nth (env, n))) (IS.listItems fvs)
|
adamc@1079
|
344 val fxs' = map (squish (IS.listItems fvs)) fxs
|
adam@1362
|
345
|
adam@1362
|
346 val p_bool = Print.PD.string o Bool.toString
|
adam@1667
|
347
|
adam@1667
|
348 fun bumpCount n =
|
adam@1667
|
349 if IS.member (#specialized st, f) then
|
adam@1667
|
350 n
|
adam@1667
|
351 else
|
adam@1667
|
352 5 + 2 *n
|
adamc@488
|
353 in
|
adam@1355
|
354 (*Print.prefaces "Func" [("name", Print.PD.string name),
|
adam@1355
|
355 ("e", CorePrint.p_exp CoreEnv.empty e),
|
adam@1355
|
356 ("fxs'", Print.p_list (CorePrint.p_exp CoreEnv.empty) fxs')];*)
|
adamc@1080
|
357 if not fin
|
adamc@1080
|
358 orelse List.all (fn (ERel _, _) => true
|
adamc@1080
|
359 | _ => false) fxs'
|
adam@1355
|
360 orelse List.exists (not o valueish) fxs'
|
adam@1667
|
361 orelse IS.numItems fvs >= bumpCount (length fxs) then
|
adamc@1083
|
362 ((*Print.prefaces "No" [("name", Print.PD.string name),
|
adamc@1120
|
363 ("f", Print.PD.string (Int.toString f)),
|
adamc@1083
|
364 ("fxs'",
|
adam@1362
|
365 Print.p_list (CorePrint.p_exp CoreEnv.empty) fxs'),
|
adam@1362
|
366 ("b1", p_bool (not fin)),
|
adam@1362
|
367 ("b2", p_bool (List.all (fn (ERel _, _) => true
|
adam@1362
|
368 | _ => false) fxs')),
|
adam@1382
|
369 ("b3", p_bool (List.exists (not o valueish) fxs')),
|
adam@1382
|
370 ("b4", p_bool (IS.numItems fvs >= length fxs
|
adam@1362
|
371 andalso IS.exists (fn n => functionInside (#2 (List.nth (env, n)))) fvs))];*)
|
adamc@1083
|
372 default ())
|
adamc@488
|
373 else
|
adam@1667
|
374 case KM.find (args, (vts, fxs')) of
|
adam@1667
|
375 SOME f' =>
|
adamc@485
|
376 let
|
adamc@488
|
377 val e = (ENamed f', loc)
|
adamc@488
|
378 val e = IS.foldr (fn (arg, e) => (EApp (e, (ERel arg, loc)), loc))
|
adamc@488
|
379 e fvs
|
adamc@1079
|
380 val e = foldl (fn (arg, e) => (EApp (e, arg), loc))
|
adamc@488
|
381 e xs
|
adamc@488
|
382 in
|
adamc@488
|
383 (*Print.prefaces "Brand new (reuse)"
|
adamc@721
|
384 [("e'", CorePrint.p_exp CoreEnv.empty e)];*)
|
adamc@1080
|
385 (e, st)
|
adamc@488
|
386 end
|
adam@1667
|
387 | NONE =>
|
adamc@488
|
388 let
|
adamc@800
|
389 (*val () = Print.prefaces "New one"
|
adam@1667
|
390 [("name", Print.PD.string name),
|
adam@1667
|
391 ("f", Print.PD.string (Int.toString f)),
|
adam@1667
|
392 ("|fvs|", Print.PD.string (Int.toString (IS.numItems fvs))),
|
adam@1667
|
393 ("|fxs|", Print.PD.string (Int.toString (length fxs))),
|
adam@1667
|
394 ("spec", Print.PD.string (Bool.toString (IS.member (#specialized st, f))))]*)
|
adamc@800
|
395
|
adamc@818
|
396 (*val () = Print.prefaces ("Yes(" ^ name ^ ")")
|
adamc@818
|
397 [("fxs'",
|
adamc@818
|
398 Print.p_list (CorePrint.p_exp CoreEnv.empty) fxs')]*)
|
adamc@818
|
399
|
adamc@1079
|
400 fun subBody (body, typ, fxs') =
|
adamc@1079
|
401 case (#1 body, #1 typ, fxs') of
|
adamc@488
|
402 (_, _, []) => SOME (body, typ)
|
adamc@1079
|
403 | (EAbs (_, _, _, body'), TFun (_, typ'), x :: fxs'') =>
|
adamc@488
|
404 let
|
adamc@1079
|
405 val body'' = E.subExpInExp (0, x) body'
|
adamc@488
|
406 in
|
adamc@488
|
407 subBody (body'',
|
adamc@488
|
408 typ',
|
adamc@1079
|
409 fxs'')
|
adamc@488
|
410 end
|
adamc@488
|
411 | _ => NONE
|
adamc@488
|
412 in
|
adamc@1079
|
413 case subBody (body, typ, fxs') of
|
adamc@1080
|
414 NONE => default ()
|
adamc@488
|
415 | SOME (body', typ') =>
|
adamc@488
|
416 let
|
adamc@488
|
417 val f' = #maxName st
|
adam@1314
|
418 val args = KM.insert (args, (vts, fxs'), f')
|
adamc@488
|
419 val funcs = IM.insert (#funcs st, f, {name = name,
|
adamc@488
|
420 args = args,
|
adamc@488
|
421 body = body,
|
adamc@488
|
422 typ = typ,
|
adamc@488
|
423 tag = tag})
|
adamc@1079
|
424
|
adamc@488
|
425 val st = {
|
adamc@488
|
426 maxName = f' + 1,
|
adamc@488
|
427 funcs = funcs,
|
adamc@1079
|
428 decls = #decls st,
|
adamc@1080
|
429 specialized = IS.add (#specialized st, f')
|
adamc@488
|
430 }
|
adamc@487
|
431
|
adamc@488
|
432 (*val () = Print.prefaces "specExp"
|
adamc@488
|
433 [("f", CorePrint.p_exp env (ENamed f, loc)),
|
adamc@488
|
434 ("f'", CorePrint.p_exp env (ENamed f', loc)),
|
adamc@488
|
435 ("xs", Print.p_list (CorePrint.p_exp env) xs),
|
adamc@488
|
436 ("fxs'", Print.p_list
|
adamc@488
|
437 (CorePrint.p_exp E.empty) fxs'),
|
adamc@488
|
438 ("e", CorePrint.p_exp env (e, loc))]*)
|
adamc@488
|
439 val (body', typ') = IS.foldl (fn (n, (body', typ')) =>
|
adamc@488
|
440 let
|
adamc@521
|
441 val (x, xt) = List.nth (env, n)
|
adamc@488
|
442 in
|
adamc@488
|
443 ((EAbs (x, xt, typ', body'),
|
adamc@488
|
444 loc),
|
adamc@488
|
445 (TFun (xt, typ'), loc))
|
adamc@488
|
446 end)
|
adamc@488
|
447 (body', typ') fvs
|
adamc@1272
|
448 (*val () = print ("NEW: " ^ name ^ "__" ^ Int.toString f' ^ "\n");*)
|
adamc@1272
|
449 val body' = ReduceLocal.reduceExp body'
|
adamc@1080
|
450 (*val () = Print.preface ("PRE", CorePrint.p_exp CoreEnv.empty body')*)
|
adamc@1080
|
451 val (body', st) = exp (env, body', st)
|
adamc@482
|
452
|
adamc@488
|
453 val e' = (ENamed f', loc)
|
adamc@488
|
454 val e' = IS.foldr (fn (arg, e) => (EApp (e, (ERel arg, loc)), loc))
|
adamc@488
|
455 e' fvs
|
adamc@1079
|
456 val e' = foldl (fn (arg, e) => (EApp (e, arg), loc))
|
adamc@488
|
457 e' xs
|
adam@1362
|
458
|
adamc@488
|
459 (*val () = Print.prefaces "Brand new"
|
adamc@721
|
460 [("e'", CorePrint.p_exp CoreEnv.empty e'),
|
adamc@1080
|
461 ("e", CorePrint.p_exp CoreEnv.empty e),
|
adamc@721
|
462 ("body'", CorePrint.p_exp CoreEnv.empty body')]*)
|
adamc@488
|
463 in
|
adamc@1080
|
464 (e',
|
adamc@488
|
465 {maxName = #maxName st,
|
adamc@488
|
466 funcs = #funcs st,
|
adamc@1079
|
467 decls = (name, f', typ', body', tag) :: #decls st,
|
adamc@1079
|
468 specialized = #specialized st})
|
adamc@488
|
469 end
|
adamc@485
|
470 end
|
adamc@488
|
471 end
|
adamc@485
|
472 end
|
adamc@482
|
473
|
adamc@521
|
474 fun doDecl (d, (st : state, changed)) =
|
adamc@488
|
475 let
|
adamc@521
|
476 (*val befor = Time.now ()*)
|
adamc@482
|
477
|
adamc@453
|
478 val funcs = #funcs st
|
adamc@453
|
479 val funcs =
|
adamc@453
|
480 case #1 d of
|
adamc@453
|
481 DValRec vis =>
|
adamc@453
|
482 foldl (fn ((x, n, c, e, tag), funcs) =>
|
adamc@453
|
483 IM.insert (funcs, n, {name = x,
|
adamc@453
|
484 args = KM.empty,
|
adamc@453
|
485 body = e,
|
adamc@453
|
486 typ = c,
|
adamc@453
|
487 tag = tag}))
|
adamc@453
|
488 funcs vis
|
adamc@453
|
489 | _ => funcs
|
adamc@453
|
490
|
adamc@453
|
491 val st = {maxName = #maxName st,
|
adamc@453
|
492 funcs = funcs,
|
adamc@1079
|
493 decls = [],
|
adamc@1079
|
494 specialized = #specialized st}
|
adamc@453
|
495
|
adamc@482
|
496 (*val () = Print.prefaces "decl" [("d", CorePrint.p_decl CoreEnv.empty d)]*)
|
adamc@521
|
497
|
adamc@522
|
498 val (d', st) =
|
adamc@522
|
499 if isPoly d then
|
adamc@522
|
500 (d, st)
|
adamc@522
|
501 else
|
adamc@1080
|
502 case #1 d of
|
adamc@1080
|
503 DVal (x, n, t, e, s) =>
|
adamc@1080
|
504 let
|
adam@1362
|
505 (*val () = Print.preface ("Visiting", Print.box [Print.PD.string (x ^ "__" ^ Int.toString n),
|
adam@1362
|
506 Print.space,
|
adam@1362
|
507 Print.PD.string ":",
|
adam@1362
|
508 Print.space,
|
adam@1362
|
509 CorePrint.p_con CoreEnv.empty t])*)
|
adam@1362
|
510
|
adamc@1080
|
511 val (e, st) = exp ([], e, st)
|
adamc@1080
|
512 in
|
adamc@1080
|
513 ((DVal (x, n, t, e, s), #2 d), st)
|
adamc@1080
|
514 end
|
adamc@1080
|
515 | DValRec vis =>
|
adamc@1080
|
516 let
|
adamc@1120
|
517 (*val () = Print.preface ("Visiting", Print.p_list (fn vi =>
|
adam@1362
|
518 Print.box [Print.PD.string (#1 vi ^ "__"
|
adam@1362
|
519 ^ Int.toString
|
adam@1362
|
520 (#2 vi)),
|
adam@1362
|
521 Print.space,
|
adam@1362
|
522 Print.PD.string ":",
|
adam@1362
|
523 Print.space,
|
adam@1362
|
524 CorePrint.p_con CoreEnv.empty (#3 vi)])
|
adamc@1120
|
525 vis)*)
|
adamc@1120
|
526
|
adamc@1080
|
527 val (vis, st) = ListUtil.foldlMap (fn ((x, n, t, e, s), st) =>
|
adamc@1080
|
528 let
|
adamc@1080
|
529 val (e, st) = exp ([], e, st)
|
adamc@1080
|
530 in
|
adamc@1080
|
531 ((x, n, t, e, s), st)
|
adamc@1080
|
532 end) st vis
|
adamc@1080
|
533 in
|
adamc@1080
|
534 ((DValRec vis, #2 d), st)
|
adamc@1080
|
535 end
|
adamc@1080
|
536 | DTable (s, n, t, s1, e1, t1, e2, t2) =>
|
adamc@1080
|
537 let
|
adamc@1080
|
538 val (e1, st) = exp ([], e1, st)
|
adamc@1080
|
539 val (e2, st) = exp ([], e2, st)
|
adamc@1080
|
540 in
|
adamc@1080
|
541 ((DTable (s, n, t, s1, e1, t2, e2, t2), #2 d), st)
|
adamc@1080
|
542 end
|
adamc@1080
|
543 | DView (x, n, s, e, t) =>
|
adamc@1080
|
544 let
|
adamc@1080
|
545 val (e, st) = exp ([], e, st)
|
adamc@1080
|
546 in
|
adamc@1080
|
547 ((DView (x, n, s, e, t), #2 d), st)
|
adamc@1080
|
548 end
|
adamc@1080
|
549 | DTask (e1, e2) =>
|
adamc@1080
|
550 let
|
adamc@1080
|
551 val (e1, st) = exp ([], e1, st)
|
adamc@1080
|
552 val (e2, st) = exp ([], e2, st)
|
adamc@1080
|
553 in
|
adamc@1080
|
554 ((DTask (e1, e2), #2 d), st)
|
adamc@1080
|
555 end
|
adamc@1080
|
556 | _ => (d, st)
|
adamc@1080
|
557
|
adamc@482
|
558 (*val () = print "/decl\n"*)
|
adamc@443
|
559
|
adamc@443
|
560 val funcs = #funcs st
|
adamc@443
|
561 val funcs =
|
adamc@443
|
562 case #1 d of
|
adamc@443
|
563 DVal (x, n, c, e as (EAbs _, _), tag) =>
|
adamc@443
|
564 IM.insert (funcs, n, {name = x,
|
adamc@453
|
565 args = KM.empty,
|
adamc@443
|
566 body = e,
|
adamc@443
|
567 typ = c,
|
adamc@443
|
568 tag = tag})
|
adamc@469
|
569 | DVal (_, n, _, (ENamed n', _), _) =>
|
adamc@469
|
570 (case IM.find (funcs, n') of
|
adamc@469
|
571 NONE => funcs
|
adamc@469
|
572 | SOME v => IM.insert (funcs, n, v))
|
adamc@443
|
573 | _ => funcs
|
adamc@443
|
574
|
adamc@453
|
575 val (changed, ds) =
|
adamc@443
|
576 case #decls st of
|
adamc@453
|
577 [] => (changed, [d'])
|
adamc@453
|
578 | vis =>
|
adamc@453
|
579 (true, case d' of
|
adamc@453
|
580 (DValRec vis', _) => [(DValRec (vis @ vis'), ErrorMsg.dummySpan)]
|
adamc@453
|
581 | _ => [(DValRec vis, ErrorMsg.dummySpan), d'])
|
adamc@443
|
582 in
|
adamc@802
|
583 (*Print.prefaces "doDecl" [("d", CorePrint.p_decl E.empty d),
|
adamc@802
|
584 ("d'", CorePrint.p_decl E.empty d')];*)
|
adamc@521
|
585 (ds, ({maxName = #maxName st,
|
adamc@453
|
586 funcs = funcs,
|
adamc@1079
|
587 decls = [],
|
adamc@1079
|
588 specialized = #specialized st}, changed))
|
adamc@443
|
589 end
|
adamc@443
|
590
|
adamc@1120
|
591 (*val () = Print.preface ("RESET", CorePrint.p_file CoreEnv.empty file)*)
|
adamc@1079
|
592 val (ds, (st, changed)) = ListUtil.foldlMapConcat doDecl
|
adamc@521
|
593 ({maxName = U.File.maxName file + 1,
|
adamc@1080
|
594 funcs = funcs,
|
adamc@1079
|
595 decls = [],
|
adamc@1079
|
596 specialized = specialized},
|
adamc@488
|
597 false)
|
adamc@488
|
598 file
|
adamc@443
|
599 in
|
adamc@1120
|
600 (*print ("Changed = " ^ Bool.toString changed ^ "\n");*)
|
adamc@1080
|
601 (changed, ds, #funcs st, #specialized st)
|
adamc@443
|
602 end
|
adamc@443
|
603
|
adamc@1080
|
604 fun specializeL (funcs, specialized) file =
|
adamc@453
|
605 let
|
adamc@721
|
606 val file = ReduceLocal.reduce file
|
adamc@520
|
607 (*val file = ReduceLocal.reduce file*)
|
adamc@1080
|
608 val (changed, file, funcs, specialized) = specialize' (funcs, specialized) file
|
adamc@520
|
609 (*val file = ReduceLocal.reduce file
|
adamc@520
|
610 val file = CoreUntangle.untangle file
|
adamc@488
|
611 val file = Shake.shake file*)
|
adamc@453
|
612 in
|
adamc@488
|
613 (*print "Round over\n";*)
|
adamc@453
|
614 if changed then
|
adamc@520
|
615 let
|
adamc@721
|
616 (*val file = ReduceLocal.reduce file*)
|
adamc@802
|
617 (*val () = Print.prefaces "Pre-untangle" [("file", CorePrint.p_file CoreEnv.empty file)]*)
|
adamc@520
|
618 val file = CoreUntangle.untangle file
|
adamc@802
|
619 (*val () = Print.prefaces "Post-untangle" [("file", CorePrint.p_file CoreEnv.empty file)]*)
|
adamc@520
|
620 val file = Shake.shake file
|
adamc@520
|
621 in
|
adamc@520
|
622 (*print "Again!\n";*)
|
adamc@1080
|
623 (*Print.prefaces "Again" [("file", CorePrint.p_file CoreEnv.empty file)];*)
|
adamc@1080
|
624 specializeL (funcs, specialized) file
|
adamc@520
|
625 end
|
adamc@453
|
626 else
|
adamc@453
|
627 file
|
adamc@453
|
628 end
|
adamc@453
|
629
|
adamc@1080
|
630 val specialize = specializeL (IM.empty, IS.empty)
|
adamc@1079
|
631
|
adamc@443
|
632 end
|