SCM Repository
Annotation of /sml/trunk/src/compiler/FLINT/opt/fcontract.sml
Parent Directory
|
Revision Log
Revision 217 - (view) (download)
1 : | monnier | 121 | (* copyright 1998 YALE FLINT PROJECT *) |
2 : | monnier | 159 | (* monnier@cs.yale.edu *) |
3 : | monnier | 121 | |
4 : | signature FCONTRACT = | ||
5 : | sig | ||
6 : | |||
7 : | (* needs Collect to be setup properly *) | ||
8 : | monnier | 213 | val contract : FLINT.prog -> FLINT.prog |
9 : | monnier | 121 | |
10 : | end | ||
11 : | |||
12 : | (* All kinds of beta-reductions. In order to do as much work per pass as | ||
13 : | * possible, the usage counts of each variable (maintained by the Collect | ||
14 : | * module) is kept as much uptodate as possible. For instance as soon as a | ||
15 : | * variable becomes dead, all the variables that were referenced have their | ||
16 : | * usage counts decremented correspondingly. This means that we have to | ||
17 : | * be careful to make sure that a dead variable will indeed not appear | ||
18 : | * in the output lexp since it might else reference other dead variables *) | ||
19 : | |||
20 : | monnier | 159 | (* things that fcontract does: |
21 : | * - several things not mentioned | ||
22 : | * - elimination of Con(Decon x) | ||
23 : | * - update counts when selecting a SWITCH alternative | ||
24 : | monnier | 162 | * - contracting RECORD(R.1,R.2) => R (only if the type is easily available) |
25 : | monnier | 184 | * - dropping of dead arguments |
26 : | monnier | 159 | *) |
27 : | |||
28 : | monnier | 121 | (* things that lcontract.sml does that fcontract doesn't do (yet): |
29 : | monnier | 159 | * - inline across DeBruijn depths (will be solved by named-tvar) |
30 : | monnier | 121 | * - elimination of let [dead-vs] = pure in body |
31 : | *) | ||
32 : | |||
33 : | (* things that cpsopt/eta.sml did that fcontract doesn't do: | ||
34 : | monnier | 159 | * - let f vs = select(v,i,g,g vs) |
35 : | monnier | 121 | *) |
36 : | |||
37 : | (* things that cpsopt/contract.sml did that fcontract doesn't do: | ||
38 : | monnier | 159 | * - IF-idiom (I still don't know what it is) |
39 : | monnier | 121 | * - unifying branches |
40 : | * - Handler operations | ||
41 : | * - primops expressions | ||
42 : | * - branch expressions | ||
43 : | *) | ||
44 : | |||
45 : | (* things that could also be added: | ||
46 : | monnier | 184 | * - elimination of dead vars in let |
47 : | monnier | 191 | * - elimination of constant arguments |
48 : | monnier | 121 | *) |
49 : | |||
50 : | (* things that would require some type info: | ||
51 : | * - dropping foo in LET vs = RAISE v IN foo | ||
52 : | *) | ||
53 : | |||
54 : | (* eta-reduction is tricky: | ||
55 : | * - recognition of eta-redexes and introduction of the corresponding | ||
56 : | * substitution in the table has to be done at the very beginning of | ||
57 : | * the processing of the FIX | ||
58 : | * - eta-reduction can turn a known function into an escaping function | ||
59 : | * - fun f (g,v2,v3) = g(g,v2,v3) looks tremendously like an eta-redex | ||
60 : | *) | ||
61 : | |||
62 : | (* order of contraction is important: | ||
63 : | * - the body of a FIX is contracted before the functions because the | ||
64 : | * functions might end up being inlined in the body in which case they | ||
65 : | * could be contracted twice. | ||
66 : | *) | ||
67 : | |||
68 : | (* When creating substitution f->g (as happens with eta redexes or with | ||
69 : | * code like `LET [f] = RET[g]'), we need to make sure that the usage cout | ||
70 : | * of f gets properly transfered to g. One way to do that is to make the | ||
71 : | * transfer incremental: each time we apply the substitution, we decrement | ||
72 : | * f's count and increment g's count. But this can be tricky since the | ||
73 : | * elimination of the eta-redex (or the trivial binding) eliminates one of the | ||
74 : | monnier | 159 | * references to g and if this is the only one, we might trigger the killing |
75 : | monnier | 121 | * of g even though its count would be later incremented. Similarly, inlining |
76 : | * of g would be dangerous as long as some references to f exist. | ||
77 : | * So instead we do the transfer once and for all when we see the eta-redex, | ||
78 : | * which frees us from those two problems but forces us to make sure that | ||
79 : | * every existing reference to f will be substituted with g. | ||
80 : | * Also, the transfer of counts from f to g is not quite straightforward | ||
81 : | * since some of the references to f might be from inside g and without doing | ||
82 : | * the transfer incrementally, we can't easily know which of the usage counts | ||
83 : | * of f should be transfered to the internal counts of g and which to the | ||
84 : | * external counts. | ||
85 : | *) | ||
86 : | |||
87 : | monnier | 159 | (* Preventing infinite inlining: |
88 : | * - inlining a function in its own body amounts to unrolling which has | ||
89 : | * to be controlled (you only want to unroll some number of times). | ||
90 : | * It's currently simply not allowed. | ||
91 : | * - inlining a recursive function outside of tis body amounts to `peeling' | ||
92 : | * one iteration. Here also, since the inlined body will have yet another | ||
93 : | * call, the inlining risks non-termination. It's hence also | ||
94 : | * not allowed. | ||
95 : | * - inlining a mutually recursive function is just a more general form | ||
96 : | * of the problem above although it can be safe and desirable in some cases. | ||
97 : | * To be safe, you simply need that one of the functions forming the | ||
98 : | * mutual-recursion loop cannot be inlined (to break the loop). This cannot | ||
99 : | * be trivially checked. So we (foolishly?) trust the `inline' bit in | ||
100 : | * those cases. This is mostly used to inline wrappers inside the | ||
101 : | * function they wrap. | ||
102 : | * - even if one only allows inlining of funtions showing no sign of | ||
103 : | * recursion, we can be bitten by a program creating its own Y combinator: | ||
104 : | * datatype dt = F of dt -> int -> int | ||
105 : | * let fun f (F g) x = g (F g) x in f (F f) end | ||
106 : | * To solve this problem, `cexp' has an `ifs' parameter containing the set | ||
107 : | * of funtions that we are inlining in order to detect (and break) cycles. | ||
108 : | * - funnily enough, if we allow inlining recursive functions the cycle | ||
109 : | * detection will ensure that the unrolling (or peeling) will only be done | ||
110 : | * once. In the future, maybe. | ||
111 : | *) | ||
112 : | |||
113 : | monnier | 184 | (* Dropping useless arguments. |
114 : | * Arguments whose value is constant (i.e. the function is known and each | ||
115 : | * call site provides the same value for that argument (or the argument | ||
116 : | * itself in the case of recursive calls) can be safely removed and replaced | ||
117 : | * inside the body by a simple let binding. The only problem is that the | ||
118 : | * constant argument might be out of scope at the function definition site. | ||
119 : | * It is obviously always possible to move the function to bring the argument | ||
120 : | * in scope, but since we don't do any code motion here, we're stuck. | ||
121 : | * If it wasn't for this little problem, we could do the cst-arg removal in | ||
122 : | * collect (we don't gain anything from doing it here). | ||
123 : | * The removal of dead arguments (args not used in the body) on the other | ||
124 : | * hand can quite well be done in collect, the only problem being that it | ||
125 : | * is convenient to do it after the cst-arg removal so that we can rely | ||
126 : | * on deadarg to do the actual removal of the cst-arg. | ||
127 : | *) | ||
128 : | |||
129 : | monnier | 121 | (* Simple inlining (inlining called-once functions, which doesn't require |
130 : | * alpha-renaming) seems inoffensive enough but is not always desirable. | ||
131 : | monnier | 159 | * The typical example is wrapper functions introduced by eta-expand: they |
132 : | * usually (until inlined) contain the only call to the main function, | ||
133 : | monnier | 121 | * but inlining the main function in the wrapper defeats the purpose of the |
134 : | * wrapper. | ||
135 : | * cpsopt dealt with this problem by adding a `NO_INLINE_INTO' hint to the | ||
136 : | monnier | 159 | * wrapper function. In this file, the idea is the following: |
137 : | * If you have a function declaration like `let f x = body in exp', first | ||
138 : | * contract `exp' and only contract `body' afterwards. This ensures that | ||
139 : | * the eta-wrapper gets a chance to be inlined before it is (potentially) | ||
140 : | * eta-reduced away. Interesting details: | ||
141 : | monnier | 121 | * - all functions (even the ones that would have a `NO_INLINE_INTO') are |
142 : | * contracted, because the "aggressive usage count maintenance" makes any | ||
143 : | * alternative painful (the collect phase has already assumed that dead code | ||
144 : | * will be eliminated, which means that fcontract should at the very least | ||
145 : | monnier | 159 | * do the dead-code elimination, so you can only avoid fcontracting a |
146 : | * a function if you can be sure that the body doesn't contain any dead-code, | ||
147 : | * which is generally not known). | ||
148 : | monnier | 190 | * - once a function is fcontracted, its inlinable status is re-examined. |
149 : | * More specifically, if no inlining occured during its fcontraction, then | ||
150 : | * we assume that the code has just become smaller and should hence | ||
151 : | * still be considered inlinable. On another hand, if inlining took place, | ||
152 : | * then we have to reset the inline-bit because the new body might | ||
153 : | * be completely different (i.e. much bigger) and inlining it might be | ||
154 : | * undesirable. | ||
155 : | monnier | 159 | * This means that in the case of |
156 : | * let fwrap x = body1 and f y = body2 in exp | ||
157 : | monnier | 190 | * if fwrap is fcontracted before f and something gets inlined into it, |
158 : | * then fwrap cannot be inlined in f. | ||
159 : | monnier | 159 | * To minimize the impact of this problem, we make sure that we fcontract |
160 : | * inlinable functions only after fcontracting other mutually recursive | ||
161 : | monnier | 190 | * functions. One way to solve the problem more thoroughly would be |
162 : | * to keep the uncontracted fwrap around until f has been contracted. | ||
163 : | * Such a trick hasn't seemed necessary yet. | ||
164 : | monnier | 121 | * - at the very end of the optimization phase, cpsopt had a special pass |
165 : | * that ignored the `NO_INLINE_INTO' hint (since at this stage, inlining | ||
166 : | * into it doesn't have any undesirable side effects any more). The present | ||
167 : | * code doesn't need such a thing. On another hand, the cpsopt approach | ||
168 : | * had the advantage of keeping the `inline' bit from one contract phase to | ||
169 : | monnier | 159 | * the next. If this ends up being important, one could add a global |
170 : | monnier | 121 | * "noinline" flag that could be set to true whenever fcontracting an |
171 : | monnier | 159 | * inlinable function (this would ensure that fcontracting such an inlinable |
172 : | * function can only reduce its size, which would allow keeping the `inline' | ||
173 : | * bit set after fcontracting). | ||
174 : | monnier | 121 | *) |
175 : | |||
176 : | structure FContract :> FCONTRACT = | ||
177 : | struct | ||
178 : | local | ||
179 : | structure F = FLINT | ||
180 : | structure M = IntmapF | ||
181 : | monnier | 159 | structure S = IntSetF |
182 : | monnier | 121 | structure C = Collect |
183 : | monnier | 184 | structure O = Option |
184 : | monnier | 121 | structure DI = DebIndex |
185 : | structure PP = PPFlint | ||
186 : | monnier | 159 | structure FU = FlintUtil |
187 : | structure LT = LtyExtern | ||
188 : | monnier | 200 | structure LK = LtyKernel |
189 : | monnier | 163 | structure OU = OptUtils |
190 : | monnier | 202 | structure PO = PrimOp |
191 : | monnier | 159 | structure CTRL = Control.FLINT |
192 : | monnier | 121 | in |
193 : | |||
194 : | val say = Control.Print.say | ||
195 : | fun bug msg = ErrorMsg.impossible ("FContract: "^msg) | ||
196 : | fun buglexp (msg,le) = (say "\n"; PP.printLexp le; bug msg) | ||
197 : | fun bugval (msg,v) = (say "\n"; PP.printSval v; bug msg) | ||
198 : | |||
199 : | (* fun sayexn e = app say (map (fn s => s^" <- ") (SMLofNJ.exnHistory e)) *) | ||
200 : | |||
201 : | monnier | 159 | val cplv = LambdaVar.dupLvar |
202 : | monnier | 200 | val mklv = LambdaVar.mkLvar |
203 : | monnier | 121 | |
204 : | datatype sval | ||
205 : | = Val of F.value (* F.value should never be F.VAR lv *) | ||
206 : | monnier | 202 | | Fun of F.lvar * F.lexp * (F.lvar * F.lty) list * F.fkind * sval list list ref |
207 : | monnier | 197 | | TFun of F.lvar * F.lexp * (F.tvar * F.tkind) list |
208 : | monnier | 189 | | Record of F.lvar * sval list |
209 : | | Con of F.lvar * sval * F.dcon * F.tyc list | ||
210 : | | Decon of F.lvar * sval * F.dcon * F.tyc list | ||
211 : | | Select of F.lvar * sval * int | ||
212 : | monnier | 121 | | Var of F.lvar * F.lty option (* cop out case *) |
213 : | |||
214 : | monnier | 159 | fun sval2lty (Var(_,x)) = x |
215 : | | sval2lty (Decon(_,_,(_,_,lty),tycs)) = | ||
216 : | SOME(hd(#2 (LT.ltd_arrow (hd(LT.lt_inst(lty, tycs)))))) | ||
217 : | monnier | 199 | | sval2lty (Select(_,sv,i)) = |
218 : | (case sval2lty sv of SOME lty => SOME(LT.lt_select(lty, i)) | _ => NONE) | ||
219 : | monnier | 159 | | sval2lty _ = NONE |
220 : | monnier | 121 | |
221 : | monnier | 159 | fun tycs_eq ([],[]) = true |
222 : | | tycs_eq (tyc1::tycs1,tyc2::tycs2) = | ||
223 : | LT.tc_eqv(tyc1,tyc2) andalso tycs_eq(tycs1,tycs2) | ||
224 : | | tycs_eq _ = false | ||
225 : | monnier | 121 | |
226 : | monnier | 200 | (* calls `code' to append a lexp to each leaf of `le'. |
227 : | * Typically used to transform `let lvs = le in code' so that | ||
228 : | * `code' is now copied at the end of each branch of `le'. | ||
229 : | * `lvs' is a list of lvars that should be used if the result of `le' | ||
230 : | * needs to be bound before calling `code'. *) | ||
231 : | fun append lvs code le = | ||
232 : | let fun l (F.RET vs) = code vs | ||
233 : | | l (le as (F.APP _ | F.TAPP _ | F.RAISE _ | F.HANDLE _)) = | ||
234 : | let val lvs = map (fn lv => let val nlv = cplv lv | ||
235 : | in C.new NONE nlv; nlv end) | ||
236 : | lvs | ||
237 : | in F.LET(lvs, le, code(map F.VAR lvs)) | ||
238 : | end | ||
239 : | | l (F.LET (lvs,body,le)) = F.LET(lvs,body, l le) | ||
240 : | | l (F.FIX (fdecs,le)) = F.FIX(fdecs, l le) | ||
241 : | | l (F.TFN (tfdec,le)) = F.TFN(tfdec, l le) | ||
242 : | | l (F.SWITCH (v,ac,arms,def)) = | ||
243 : | let fun larm (con,le) = (con, l le) | ||
244 : | in F.SWITCH(v, ac, map larm arms, O.map l def) | ||
245 : | end | ||
246 : | | l (F.CON (dc,tycs,v,lv,le)) = F.CON(dc, tycs, v, lv, l le) | ||
247 : | | l (F.RECORD (rk,vs,lv,le)) = F.RECORD(rk, vs, lv, l le) | ||
248 : | | l (F.SELECT (v,i,lv,le)) = F.SELECT(v, i, lv, l le) | ||
249 : | | l (F.BRANCH (po,vs,le1,le2)) = F.BRANCH(po, vs, l le1, l le2) | ||
250 : | | l (F.PRIMOP (po,vs,lv,le)) = F.PRIMOP(po, vs, lv, l le) | ||
251 : | in l le | ||
252 : | end | ||
253 : | |||
254 : | monnier | 201 | (* `extract' extracts the code of a switch arm into a function |
255 : | * and replaces it with a call to that function *) | ||
256 : | fun extract (con,le) = | ||
257 : | let val f = mklv() | ||
258 : | val fk = {isrec=NONE,known=true,inline=F.IH_SAFE, | ||
259 : | cconv=F.CC_FUN(LK.FF_FIXED)} | ||
260 : | in case con of | ||
261 : | F.DATAcon(dc as (_,_,lty),tycs,lv) => | ||
262 : | let val nlv = cplv lv | ||
263 : | val _ = C.new (SOME[lv]) f | ||
264 : | val _ = C.use NONE (C.new NONE nlv) | ||
265 : | val (lty,_) = LT.ltd_parrow(hd(LT.lt_inst(lty, tycs))) | ||
266 : | in ((F.DATAcon(dc, tycs, nlv), | ||
267 : | F.APP(F.VAR f, [F.VAR nlv])), | ||
268 : | (fk, f, [(lv, lty)], le)) | ||
269 : | end | ||
270 : | | con => | ||
271 : | let val _ = C.new (SOME[]) f | ||
272 : | in ((con, F.APP(F.VAR f, [])), | ||
273 : | (fk, f, [], le)) | ||
274 : | end | ||
275 : | end | ||
276 : | |||
277 : | monnier | 202 | fun inScope m lv = (M.lookup m lv; true) handle M.IntmapF => false |
278 : | |||
279 : | monnier | 213 | fun click s c = (if !CTRL.misc = 1 then say s else (); |
280 : | c := !c + 1 (* Stats.addCounter c 1 *) ) | ||
281 : | monnier | 185 | |
282 : | monnier | 213 | fun contract (fdec as (_,f,_,_)) = let |
283 : | monnier | 185 | |
284 : | monnier | 213 | val c_dummy = ref 0 (* Stats.newCounter[] *) |
285 : | val c_miss = ref 0 (* Stats.newCounter[] *) | ||
286 : | monnier | 189 | |
287 : | monnier | 213 | val counter = c_dummy |
288 : | monnier | 189 | |
289 : | fun click_deadval () = (click "d" counter) | ||
290 : | fun click_deadlexp () = (click "D" counter) | ||
291 : | fun click_select () = (click "s" counter) | ||
292 : | fun click_record () = (click "r" counter) | ||
293 : | fun click_con () = (click "c" counter) | ||
294 : | fun click_switch () = (click "s" counter) | ||
295 : | fun click_eta () = (click "e" counter) | ||
296 : | fun click_etasplit () = (click "E" counter) | ||
297 : | fun click_branch () = (click "b" counter) | ||
298 : | fun click_dropargs () = (click "a" counter) | ||
299 : | |||
300 : | fun click_lacktype () = (click "t" c_miss) | ||
301 : | |||
302 : | (* this counters is actually *used* by fcontract. | ||
303 : | * It's not used just for statistics. *) | ||
304 : | monnier | 213 | val c_inline = ref 0 (* Stats.newCounter[counter] *) |
305 : | monnier | 189 | fun click_simpleinline () = (click "i" c_inline) |
306 : | fun click_copyinline () = (click "I" c_inline) | ||
307 : | fun click_unroll () = (click "u" c_inline) | ||
308 : | monnier | 213 | fun inline_count () = (* Stats.getCounter *) !c_inline |
309 : | monnier | 189 | |
310 : | monnier | 186 | fun used lv = (C.usenb(C.get lv) > 0) |
311 : | monnier | 199 | (* handle x => |
312 : | monnier | 186 | (say("while in FContract.used "^(C.LVarString lv)^"\n"); |
313 : | monnier | 199 | raise x) *) |
314 : | monnier | 121 | |
315 : | fun eqConV (F.INTcon i1, F.INT i2) = i1 = i2 | ||
316 : | | eqConV (F.INT32con i1, F.INT32 i2) = i1 = i2 | ||
317 : | | eqConV (F.WORDcon i1, F.WORD i2) = i1 = i2 | ||
318 : | | eqConV (F.WORD32con i1, F.WORD32 i2) = i1 = i2 | ||
319 : | | eqConV (F.REALcon r1, F.REAL r2) = r1 = r2 | ||
320 : | | eqConV (F.STRINGcon s1, F.STRING s2) = s1 = s2 | ||
321 : | | eqConV (con,v) = bugval("unexpected comparison with val", v) | ||
322 : | |||
323 : | fun lookup m lv = (M.lookup m lv) | ||
324 : | monnier | 202 | handle e as M.IntmapF => |
325 : | monnier | 121 | (say "\nlooking up unbound "; |
326 : | say (!PP.LVarString lv); | ||
327 : | monnier | 202 | raise e) |
328 : | monnier | 121 | |
329 : | fun sval2val sv = | ||
330 : | case sv | ||
331 : | monnier | 159 | of (Fun{1=lv,...} | TFun{1=lv,...} | Record{1=lv,...} | Decon{1=lv,...} |
332 : | monnier | 121 | | Con{1=lv,...} | Select{1=lv,...} | Var{1=lv,...}) => F.VAR lv |
333 : | | Val v => v | ||
334 : | |||
335 : | monnier | 163 | fun val2sval m (F.VAR ov) = |
336 : | monnier | 199 | ((lookup m ov) (* handle x => |
337 : | (say("val2sval "^(C.LVarString ov)^"\n"); raise x) *) ) | ||
338 : | monnier | 121 | | val2sval m v = Val v |
339 : | |||
340 : | fun bugsv (msg,sv) = bugval(msg, sval2val sv) | ||
341 : | |||
342 : | fun subst m ov = sval2val (lookup m ov) | ||
343 : | monnier | 199 | fun substval m = sval2val o (val2sval m) |
344 : | fun substvar m lv = | ||
345 : | case substval m (F.VAR lv) | ||
346 : | monnier | 121 | of F.VAR lv => lv |
347 : | | v => bugval ("unexpected val", v) | ||
348 : | |||
349 : | (* called when a variable becomes dead. | ||
350 : | * it simply adjusts the use-counts *) | ||
351 : | fun undertake m lv = | ||
352 : | let val undertake = undertake m | ||
353 : | in case lookup m lv | ||
354 : | monnier | 186 | of Var {1=nlv,...} => () |
355 : | monnier | 121 | | Val v => () |
356 : | monnier | 202 | | Fun (lv,le,args,_,_) => |
357 : | monnier | 187 | C.unuselexp undertake |
358 : | (F.LET(map #1 args, | ||
359 : | F.RET (map (fn _ => F.INT 0) args), | ||
360 : | le)) | ||
361 : | | TFun{1=lv,2=le,...} => | ||
362 : | C.unuselexp undertake le | ||
363 : | monnier | 189 | | (Select {2=sv,...} | Con {2=sv,...}) => unusesval m sv |
364 : | | Record {2=svs,...} => app (unusesval m) svs | ||
365 : | monnier | 159 | (* decon's are implicit so we can't get rid of them *) |
366 : | | Decon _ => () | ||
367 : | monnier | 121 | end |
368 : | handle M.IntmapF => | ||
369 : | monnier | 186 | (say("Unable to undertake "^(C.LVarString lv)^"\n")) |
370 : | monnier | 121 | | x => |
371 : | monnier | 186 | (say("while undertaking "^(C.LVarString lv)^"\n"); raise x) |
372 : | monnier | 121 | |
373 : | monnier | 189 | and unusesval m sv = unuseval m (sval2val sv) |
374 : | monnier | 187 | and unuseval m (F.VAR lv) = |
375 : | if (C.unuse false (C.get lv)) then undertake m lv else () | ||
376 : | | unuseval f _ = () | ||
377 : | fun unusecall m lv = | ||
378 : | if (C.unuse true (C.get lv)) then undertake m lv else () | ||
379 : | |||
380 : | |||
381 : | monnier | 121 | fun addbind (m,lv,sv) = M.add(m, lv, sv) |
382 : | |||
383 : | monnier | 164 | (* substitute a value sv for a variable lv and unuse value v. *) |
384 : | monnier | 121 | fun substitute (m, lv1, sv, v) = |
385 : | (case sval2val sv of F.VAR lv2 => C.transfer(lv1,lv2) | v2 => (); | ||
386 : | monnier | 187 | unuseval m v; |
387 : | monnier | 199 | addbind(m, lv1, sv)) (* handle x => |
388 : | monnier | 186 | (say ("while substituting "^ |
389 : | monnier | 164 | (C.LVarString lv1)^ |
390 : | " -> "); | ||
391 : | monnier | 121 | PP.printSval (sval2val sv); |
392 : | monnier | 199 | raise x) *) |
393 : | monnier | 121 | |
394 : | (* common code for primops *) | ||
395 : | monnier | 199 | fun cpo m (SOME{default,table},po,lty,tycs) = |
396 : | (SOME{default=substvar m default, | ||
397 : | table=map (fn (tycs,lv) => (tycs, substvar m lv)) table}, | ||
398 : | monnier | 121 | po,lty,tycs) |
399 : | monnier | 199 | | cpo _ po = po |
400 : | monnier | 121 | |
401 : | monnier | 199 | fun cdcon m (s,Access.EXN(Access.LVAR lv),lty) = |
402 : | (s, Access.EXN(Access.LVAR(substvar m lv)), lty) | ||
403 : | | cdcon _ dc = dc | ||
404 : | monnier | 121 | |
405 : | monnier | 201 | (* ifs (inlined functions): records which functions we're currently inlining |
406 : | monnier | 199 | * in order to detect loops |
407 : | * m: is a map lvars to their defining expressions (svals) *) | ||
408 : | monnier | 201 | fun fcexp ifs m le cont = let |
409 : | val loop = fcexp ifs | ||
410 : | monnier | 199 | val substval = substval m |
411 : | val cdcon = cdcon m | ||
412 : | val cpo = cpo m | ||
413 : | monnier | 163 | |
414 : | monnier | 201 | fun fcLet (lvs,le,body) = |
415 : | loop m le | ||
416 : | (fn (nm,nle) => | ||
417 : | let fun cbody () = | ||
418 : | let val nm = (foldl (fn (lv,m) => | ||
419 : | addbind(m, lv, Var(lv, NONE))) | ||
420 : | nm lvs) | ||
421 : | in case loop nm body cont | ||
422 : | of F.RET vs => if vs = (map F.VAR lvs) then nle | ||
423 : | else F.LET(lvs, nle, F.RET vs) | ||
424 : | | nbody => F.LET(lvs, nle, nbody) | ||
425 : | end | ||
426 : | in case nle | ||
427 : | of F.RET vs => | ||
428 : | let fun simplesubst (lv,v,m) = | ||
429 : | let val sv = val2sval m v | ||
430 : | in substitute(m, lv, sv, sval2val sv) | ||
431 : | end | ||
432 : | val nm = (ListPair.foldl simplesubst nm (lvs, vs)) | ||
433 : | in loop nm body cont | ||
434 : | end | ||
435 : | | F.TAPP _ => | ||
436 : | if List.all (C.dead o C.get) lvs | ||
437 : | then loop nm body cont | ||
438 : | else cbody() | ||
439 : | | (F.BRANCH _ | F.SWITCH _) => | ||
440 : | (* this is a hack originally meant to cleanup the BRANCH | ||
441 : | * mess introduced in flintnm (where each branch returns | ||
442 : | * just true or false which is generally only used as | ||
443 : | * input to a SWITCH). | ||
444 : | * The present code does more than clean up this case. | ||
445 : | * It has one serious shortcoming: it ends up making | ||
446 : | * three fcontract passes through the same code (plus | ||
447 : | * one cheap traversal). *) | ||
448 : | let fun cassoc (lv,F.SWITCH(F.VAR v,ac,arms,NONE),wrap) = | ||
449 : | if lv <> v orelse C.usenb(C.get lv) > 1 | ||
450 : | then cbody() else | ||
451 : | let val (narms,fdecs) = | ||
452 : | ListPair.unzip (map extract arms) | ||
453 : | fun addswitch [v] = | ||
454 : | C.copylexp | ||
455 : | IntmapF.empty | ||
456 : | (F.SWITCH(v,ac,narms,NONE)) | ||
457 : | | addswitch _ = bug "prob in addswitch" | ||
458 : | (* replace each leaf `ret' with a copy | ||
459 : | * of the switch *) | ||
460 : | val nle = append [lv] addswitch nle | ||
461 : | (* decorate with the functions extracted | ||
462 : | * from the switch arms *) | ||
463 : | val nle = | ||
464 : | foldl (fn (f,le) => F.FIX([f],le)) | ||
465 : | (wrap nle) fdecs | ||
466 : | (* Ugly hack: force one more traversal *) | ||
467 : | val nle = loop nm nle #2 | ||
468 : | in click_branch(); | ||
469 : | loop nm nle cont | ||
470 : | end | ||
471 : | | cassoc _ = cbody() | ||
472 : | in case (lvs,body) | ||
473 : | of ([lv],le as F.SWITCH _) => | ||
474 : | cassoc(lv, le, fn x => x) | ||
475 : | | ([lv],F.LET(lvs,le as F.SWITCH _,rest)) => | ||
476 : | cassoc(lv, le, fn le => F.LET(lvs,le,rest)) | ||
477 : | | _ => cbody() | ||
478 : | end | ||
479 : | | _ => cbody() | ||
480 : | end) | ||
481 : | monnier | 200 | |
482 : | monnier | 201 | fun fcFix (fs,le) = |
483 : | monnier | 202 | let (* merge actual arguments to extract the constant subpart *) |
484 : | fun merge_actuals ((lv,lty),[],m) = addbind(m, lv, Var(lv, SOME lty)) | ||
485 : | | merge_actuals ((lv,lty),a::bs,m) = addbind(m, lv, Var(lv, SOME lty)) | ||
486 : | (* FIXME: there's a bug here, but it's not caught by chkflint | ||
487 : | let fun f (b::bs) = | ||
488 : | if sval2val a = sval2val b then f bs | ||
489 : | else addbind(m, lv, Var(lv, SOME lty)) | ||
490 : | | f [] = | ||
491 : | (click "C" c_cstarg; | ||
492 : | case sval2val a | ||
493 : | of v as F.VAR lv' => | ||
494 : | (* FIXME: this inScope check is wrong for non-recursive | ||
495 : | * functions. But it only matters if the function is | ||
496 : | * passed itself as a parameter which cannot happen | ||
497 : | * with the current type system I believe. *) | ||
498 : | if inScope m lv' then | ||
499 : | let val sv = | ||
500 : | case a of Var (v,NONE) => Var(v, SOME lty) | ||
501 : | | _ => a | ||
502 : | in substitute(m, lv, sv, v) | ||
503 : | end | ||
504 : | else (click "O" c_outofscope; | ||
505 : | |||
506 : | addbind(m, lv, Var(lv, SOME lty))) | ||
507 : | | v => substitute(m, lv, a, v)) | ||
508 : | in f bs | ||
509 : | end *) | ||
510 : | (* The actual function contraction *) | ||
511 : | monnier | 203 | fun fcFun ((f,body,args,fk as {inline,cconv,known,isrec},actuals), |
512 : | (m,fs)) = | ||
513 : | monnier | 201 | let val fi = C.get f |
514 : | monnier | 203 | in if C.dead fi then (m,fs) |
515 : | monnier | 201 | else if C.iusenb fi = C.usenb fi then |
516 : | (* we need to be careful that undertake not be called | ||
517 : | * recursively *) | ||
518 : | monnier | 203 | (C.use NONE fi; undertake m f; (m,fs)) |
519 : | monnier | 201 | else |
520 : | monnier | 213 | let (* val _ = say ("\nEntering "^(C.LVarString f)) *) |
521 : | monnier | 201 | val saved_ic = inline_count() |
522 : | (* make up the bindings for args inside the body *) | ||
523 : | monnier | 202 | val actuals = if isSome isrec orelse |
524 : | C.escaping fi orelse | ||
525 : | null(!actuals) | ||
526 : | then map (fn _ => []) args | ||
527 : | else OU.transpose(!actuals) | ||
528 : | val nm = ListPair.foldl merge_actuals m (args, actuals) | ||
529 : | monnier | 201 | (* contract the body and create the resulting fundec *) |
530 : | val nbody = fcexp (S.add(f, ifs)) nm body #2 | ||
531 : | (* if inlining took place, the body might be completely | ||
532 : | * changed (read: bigger), so we have to reset the | ||
533 : | * `inline' bit *) | ||
534 : | val nfk = {isrec=isrec, cconv=cconv, | ||
535 : | known=known orelse not(C.escaping fi), | ||
536 : | inline=if inline_count() = saved_ic | ||
537 : | then inline | ||
538 : | else F.IH_SAFE} | ||
539 : | (* update the binding in the map. This step is | ||
540 : | * not just a mere optimization but is necessary | ||
541 : | * because if we don't do it and the function | ||
542 : | * gets inlined afterwards, the counts will reflect the | ||
543 : | * new contracted code while we'll be working on the | ||
544 : | * the old uncontracted code *) | ||
545 : | monnier | 203 | val nm = addbind(m, f, Fun(f, nbody, args, nfk, ref [])) |
546 : | in (nm, (nfk, f, args, nbody)::fs) | ||
547 : | monnier | 213 | (* before say ("\nExiting "^(C.LVarString f)) *) |
548 : | monnier | 201 | end |
549 : | end | ||
550 : | |||
551 : | (* check for eta redex *) | ||
552 : | monnier | 202 | fun fcEta (fdec as (f,F.APP(F.VAR g,vs),args,_,_),(m,fs,hs)) = |
553 : | monnier | 201 | if List.length args = List.length vs andalso |
554 : | OU.ListPair_all (fn (v,(lv,t)) => | ||
555 : | case v of F.VAR v => v = lv andalso lv <> g | ||
556 : | | _ => false) | ||
557 : | (vs, args) | ||
558 : | then | ||
559 : | let val svg = lookup m g | ||
560 : | val g = case sval2val svg | ||
561 : | of F.VAR g => g | ||
562 : | | v => bugval("not a variable", v) | ||
563 : | (* NOTE: we don't want to turn a known function into an | ||
564 : | * escaping one. It's dangerous for optimisations based | ||
565 : | * on known functions (elimination of dead args, f.ex) | ||
566 : | monnier | 217 | * and could generate cases where call>use in collect. |
567 : | * Of course, if g is not a locally defined function (it's | ||
568 : | * bound by a LET or as an argument), then knownness is | ||
569 : | * irrelevant. *) | ||
570 : | monnier | 215 | in if f = g orelse |
571 : | monnier | 217 | ((C.escaping(C.get f)) andalso |
572 : | not(C.escaping(C.get g)) andalso | ||
573 : | (case svg of Fun _ => true | _ => false)) | ||
574 : | monnier | 201 | (* the default case could ensure the inline *) |
575 : | then (m, fdec::fs, hs) | ||
576 : | else let | ||
577 : | (* if an earlier function h has been eta-reduced | ||
578 : | * to f, we have to be careful to update its | ||
579 : | * binding to not refer to f any more since f | ||
580 : | * will disappear *) | ||
581 : | val nm = foldl (fn (h,m) => | ||
582 : | if sval2val(lookup m h) = F.VAR f | ||
583 : | then addbind(m, h, svg) else m) | ||
584 : | m hs | ||
585 : | in | ||
586 : | (* I could almost reuse `substitute' but the | ||
587 : | * unuse in substitute assumes the val is escaping *) | ||
588 : | click_eta(); | ||
589 : | C.transfer(f, g); | ||
590 : | unusecall m g; | ||
591 : | (addbind(m, f, svg), fs, f::hs) | ||
592 : | end | ||
593 : | monnier | 189 | end |
594 : | monnier | 201 | else (m, fdec::fs, hs) |
595 : | | fcEta (fdec,(m,fs,hs)) = (m,fdec::fs,hs) | ||
596 : | |||
597 : | (* add wrapper for various purposes *) | ||
598 : | monnier | 217 | fun wrap (f as (fk as {isrec,inline,...},g,args,body):F.fundec,fs) = |
599 : | monnier | 201 | let val gi = C.get g |
600 : | fun dropargs filter = | ||
601 : | let val (nfk,nfk') = OU.fk_wrap(fk, O.map #1 isrec) | ||
602 : | val args' = filter args | ||
603 : | val ng = cplv g | ||
604 : | val nargs = map (fn (v,t) => (cplv v, t)) args | ||
605 : | val nargs' = map #1 (filter nargs) | ||
606 : | val appargs = (map F.VAR nargs') | ||
607 : | val nf = (nfk, g, nargs, F.APP(F.VAR ng, appargs)) | ||
608 : | val nf' = (nfk', ng, args', body) | ||
609 : | |||
610 : | val ngi = C.new (SOME(map #1 args')) ng | ||
611 : | in | ||
612 : | C.ireset gi; | ||
613 : | app (ignore o (C.new NONE) o #1) nargs; | ||
614 : | C.use (SOME appargs) ngi; | ||
615 : | app (C.use NONE o C.get) nargs'; | ||
616 : | nf'::nf::fs | ||
617 : | monnier | 121 | end |
618 : | monnier | 201 | in |
619 : | (* Don't introduce wrappers for escaping-only functions. | ||
620 : | * This is debatable since although wrappers are useless | ||
621 : | * on escaping-only functions, some of the escaping uses | ||
622 : | * might turn into calls in the course of fcontract, so | ||
623 : | * by not introducing wrappers here, we avoid useless work | ||
624 : | * but we also postpone useful work to later invocations. *) | ||
625 : | monnier | 217 | if C.dead gi then fs |
626 : | else if inline=F.IH_ALWAYS then f::fs else | ||
627 : | monnier | 201 | let val used = map (used o #1) args |
628 : | in if C.called gi then | ||
629 : | (* if some args are not used, let's drop them *) | ||
630 : | if not (List.all (fn x => x) used) then | ||
631 : | (click_dropargs(); | ||
632 : | monnier | 203 | dropargs (fn xs => OU.filter used xs)) |
633 : | monnier | 190 | |
634 : | monnier | 201 | (* eta-split: add a wrapper for escaping uses *) |
635 : | else if C.escaping gi then | ||
636 : | (* like dropargs but keeping all args *) | ||
637 : | (click_etasplit(); dropargs (fn x => x)) | ||
638 : | |||
639 : | else f::fs | ||
640 : | else f::fs | ||
641 : | end | ||
642 : | end | ||
643 : | |||
644 : | (* add various wrappers *) | ||
645 : | val fs = foldl wrap [] fs | ||
646 : | |||
647 : | (* register the new bindings (uncontracted for now) *) | ||
648 : | monnier | 202 | val (nm,fs) = foldl (fn (fdec as (fk,f,args,body),(m,fs)) => |
649 : | let val nf = (f, body, args, fk, ref []) | ||
650 : | in (addbind(m, f, Fun nf), nf::fs) end) | ||
651 : | (m,[]) fs | ||
652 : | monnier | 201 | (* check for eta redexes *) |
653 : | val (nm,fs,_) = foldl fcEta (nm,[],[]) fs | ||
654 : | |||
655 : | monnier | 204 | val (wrappers,funs) = |
656 : | monnier | 202 | List.partition (fn (_,_,_,{inline=F.IH_ALWAYS,...},_) => true |
657 : | monnier | 201 | | _ => false) fs |
658 : | monnier | 204 | val (maybes,funs) = |
659 : | monnier | 203 | List.partition (fn (_,_,_,{inline=F.IH_MAYBE _,...},_) => true |
660 : | | _ => false) funs | ||
661 : | monnier | 213 | |
662 : | monnier | 204 | (* First contract the big inlinable functions. This might make them |
663 : | * non-inlinable and we'd rather know that before we inline them. | ||
664 : | * Then we inline the body (so that we won't go through the inline-once | ||
665 : | * functions twice), then the normal functions and finally the wrappers | ||
666 : | * (which need to come last to make sure that they get inlined if | ||
667 : | * at all possible) *) | ||
668 : | val fs = [] | ||
669 : | val (nm,fs) = foldl fcFun (nm,fs) maybes | ||
670 : | monnier | 201 | val nle = loop nm le cont |
671 : | monnier | 203 | val (nm,fs) = foldl fcFun (nm,fs) funs |
672 : | val (nm,fs) = foldl fcFun (nm,fs) wrappers | ||
673 : | monnier | 201 | (* junk newly unused funs *) |
674 : | val fs = List.filter (used o #2) fs | ||
675 : | in | ||
676 : | case fs | ||
677 : | of [] => nle | ||
678 : | | [f1 as ({isrec=NONE,...},_,_,_),f2] => | ||
679 : | (* gross hack: `wrap' might have added a second | ||
680 : | * non-recursive function. we need to split them into | ||
681 : | monnier | 203 | * 2 FIXes. This is _very_ ad-hoc. *) |
682 : | monnier | 204 | F.FIX([f2], F.FIX([f1], nle)) |
683 : | monnier | 201 | | _ => F.FIX(fs, nle) |
684 : | end | ||
685 : | monnier | 163 | |
686 : | monnier | 201 | fun fcApp (f,vs) = |
687 : | monnier | 202 | let val svs = map (val2sval m) vs |
688 : | monnier | 201 | val svf = val2sval m f |
689 : | (* F.APP inlining (if any) *) | ||
690 : | monnier | 202 | |
691 : | monnier | 201 | in case svf |
692 : | monnier | 202 | of Fun(g,body,args,{inline,...},actuals) => |
693 : | let val gi = C.get g | ||
694 : | fun noinline () = | ||
695 : | (actuals := svs :: (!actuals); | ||
696 : | cont(m,F.APP(sval2val svf, map sval2val svs))) | ||
697 : | fun simpleinline () = | ||
698 : | (* simple inlining: we should copy the body and then | ||
699 : | * kill the function, but instead we just move the body | ||
700 : | * and kill only the function name. | ||
701 : | * This inlining strategy looks inoffensive enough, | ||
702 : | * but still requires some care: see comments at the | ||
703 : | * begining of this file and in cfun *) | ||
704 : | monnier | 213 | (click_simpleinline(); |
705 : | monnier | 202 | ignore(C.unuse true gi); |
706 : | loop m (F.LET(map #1 args, F.RET vs, body)) cont) | ||
707 : | fun copyinline () = | ||
708 : | (* aggressive inlining. We allow pretty much | ||
709 : | * any inlinling, but we detect and reject inlining | ||
710 : | * recursively which would else lead to infinite loop *) | ||
711 : | (* unrolling is not as straightforward as it seems: | ||
712 : | * if you inline the function you're currently | ||
713 : | * fcontracting, you're asking for trouble: there is a | ||
714 : | * hidden assumption in the counting that the old code | ||
715 : | * will be replaced by the new code (and is hence dead). | ||
716 : | * If the function to be unrolled has the only call to | ||
717 : | * function f, then f might get simpleinlined before | ||
718 : | * unrolling, which means that unrolling will introduce | ||
719 : | * a second occurence of the `only call' but at that point | ||
720 : | * f has already been killed. *) | ||
721 : | let val nle = (F.LET(map #1 args, F.RET vs, body)) | ||
722 : | val nle = C.copylexp M.empty nle | ||
723 : | in | ||
724 : | click_copyinline(); | ||
725 : | (app (unuseval m) vs); | ||
726 : | unusecall m g; | ||
727 : | fcexp (S.add(g, ifs)) m nle cont | ||
728 : | end | ||
729 : | |||
730 : | in if C.usenb gi = 1 andalso not(S.member ifs g) then simpleinline() | ||
731 : | else case inline of | ||
732 : | F.IH_SAFE => noinline() | ||
733 : | | F.IH_UNROLL => noinline() | ||
734 : | | F.IH_ALWAYS => | ||
735 : | if S.member ifs g then noinline() else copyinline() | ||
736 : | | F.IH_MAYBE(min,ws) => | ||
737 : | if S.member ifs g then noinline() else let | ||
738 : | fun value w _ (Val _ | Con _ | Record _) = w | ||
739 : | | value w v (Fun (f,_,args,_,_)) = | ||
740 : | if C.usenb(C.get v) = 1 then w * 2 else w | ||
741 : | | value w _ _ = 0 | ||
742 : | val s = (OU.foldl3 (fn (sv,w,(v,t),s) => value w v sv + s) | ||
743 : | 0 (svs,ws,args)) | ||
744 : | handle OU.Unbalanced => 0 | ||
745 : | in if s > min then copyinline() else noinline() | ||
746 : | end | ||
747 : | end | ||
748 : | | sv => cont(m,F.APP(sval2val svf, map sval2val svs)) | ||
749 : | monnier | 201 | end |
750 : | monnier | 184 | |
751 : | monnier | 201 | fun fcTfn ((f,args,body),le) = |
752 : | let val fi = C.get f | ||
753 : | in if C.dead fi then (click_deadlexp(); loop m le cont) else | ||
754 : | let val nbody = fcexp ifs m body #2 | ||
755 : | val nm = addbind(m, f, TFun(f, nbody, args)) | ||
756 : | monnier | 184 | val nle = loop nm le cont |
757 : | monnier | 121 | in |
758 : | monnier | 201 | if C.dead fi then nle else F.TFN((f, args, nbody), nle) |
759 : | monnier | 121 | end |
760 : | monnier | 201 | end |
761 : | |||
762 : | fun fcSwitch (v,ac,arms,def) = | ||
763 : | let fun fcsCon (lvc,svc,dc1:F.dcon,tycs1) = | ||
764 : | let fun killle le = C.unuselexp (undertake m) le | ||
765 : | fun kill lv le = | ||
766 : | C.unuselexp (undertake (addbind(m,lv,Var(lv,NONE)))) le | ||
767 : | fun killarm (F.DATAcon(_,_,lv),le) = kill lv le | ||
768 : | | killarm _ = buglexp("bad arm in switch(con)", le) | ||
769 : | |||
770 : | fun carm ((F.DATAcon(dc2,tycs2,lv),le)::tl) = | ||
771 : | (* sometimes lty1 <> lty2 :-( so this doesn't work: | ||
772 : | * FU.dcon_eq(dc1, dc2) andalso tycs_eq(tycs1,tycs2) *) | ||
773 : | if #2 dc1 = #2 (cdcon dc2) then | ||
774 : | (map killarm tl; (* kill the rest *) | ||
775 : | O.map killle def; (* and the default case *) | ||
776 : | loop (substitute(m, lv, svc, F.VAR lvc)) | ||
777 : | le cont) | ||
778 : | else | ||
779 : | (* kill this arm and continue with the rest *) | ||
780 : | (kill lv le; carm tl) | ||
781 : | | carm [] = loop m (O.valOf def) cont | ||
782 : | | carm _ = buglexp("unexpected arm in switch(con,...)", le) | ||
783 : | in click_switch(); carm arms | ||
784 : | monnier | 186 | end |
785 : | monnier | 121 | |
786 : | monnier | 201 | fun fcsVal v = |
787 : | let fun kill le = C.unuselexp (undertake m) le | ||
788 : | fun carm ((con,le)::tl) = | ||
789 : | if eqConV(con, v) then | ||
790 : | (map (kill o #2) tl; | ||
791 : | O.map kill def; | ||
792 : | loop m le cont) | ||
793 : | else (kill le; carm tl) | ||
794 : | | carm [] = loop m (O.valOf def) cont | ||
795 : | in click_switch(); carm arms | ||
796 : | end | ||
797 : | |||
798 : | fun fcsDefault (sv,lvc) = | ||
799 : | case (arms,def) | ||
800 : | of ([(F.DATAcon(dc,tycs,lv),le)],NONE) => | ||
801 : | (* this is a mere DECON, so we can push the let binding | ||
802 : | * (hidden in cont) inside and maybe even drop the DECON *) | ||
803 : | let val ndc = cdcon dc | ||
804 : | val slv = Decon(lv, sv, ndc, tycs) | ||
805 : | val nm = addbind(m, lv, slv) | ||
806 : | (* see below *) | ||
807 : | (* val nm = addbind(nm, lvc, Con(lvc, slv, ndc, tycs)) *) | ||
808 : | val nle = loop nm le cont | ||
809 : | val nv = sval2val sv | ||
810 : | in | ||
811 : | if used lv then | ||
812 : | F.SWITCH(nv,ac,[(F.DATAcon(ndc,tycs,lv),nle)],NONE) | ||
813 : | else (unuseval m nv; nle) | ||
814 : | end | ||
815 : | | (([(_,le)],NONE) | ([],SOME le)) => | ||
816 : | (* This should never happen, but we can optimize it away *) | ||
817 : | (unuseval m (sval2val sv); loop m le cont) | ||
818 : | | _ => | ||
819 : | let fun carm (F.DATAcon(dc,tycs,lv),le) = | ||
820 : | let val ndc = cdcon dc | ||
821 : | val slv = Decon(lv, sv, ndc, tycs) | ||
822 : | val nm = addbind(m, lv, slv) | ||
823 : | (* we can rebind lv to a more precise value | ||
824 : | * !!BEWARE!! This rebinding is misleading: | ||
825 : | * - it gives the impression that `lvc' is built | ||
826 : | * from`lv' although the reverse is true: | ||
827 : | * if `lvc' is undertaken, `lv's count should | ||
828 : | * *not* be updated! | ||
829 : | * Luckily, `lvc' will not become dead while | ||
830 : | * rebound to Con(lv) because it's used by the | ||
831 : | * SWITCH. All in all, it works fine, but it's | ||
832 : | * not as straightforward as it seems. | ||
833 : | * - it seems to be a good idea, but it can hide | ||
834 : | * other opt-opportunities since it hides the | ||
835 : | * previous binding. *) | ||
836 : | (* val nm = addbind(nm, lvc, Con(lvc,slv,ndc,tycs)) *) | ||
837 : | in (F.DATAcon(ndc, tycs, lv), loop nm le #2) | ||
838 : | end | ||
839 : | | carm (con,le) = (con, loop m le #2) | ||
840 : | val narms = map carm arms | ||
841 : | val ndef = Option.map (fn le => loop m le #2) def | ||
842 : | in cont(m, F.SWITCH(sval2val sv, ac, narms, ndef)) | ||
843 : | end | ||
844 : | monnier | 121 | |
845 : | monnier | 201 | in case val2sval m v |
846 : | of sv as Con x => fcsCon x | ||
847 : | | sv as Val v => fcsVal v | ||
848 : | | sv as (Var{1=lvc,...} | Select{1=lvc,...} | Decon{1=lvc, ...} | ||
849 : | | (* will probably never happen *) Record{1=lvc,...}) => | ||
850 : | fcsDefault(sv, lvc) | ||
851 : | | sv as (Fun _ | TFun _) => | ||
852 : | bugval("unexpected switch arg", sval2val sv) | ||
853 : | end | ||
854 : | monnier | 159 | |
855 : | monnier | 201 | fun fcCon (dc1,tycs1,v,lv,le) = |
856 : | let val lvi = C.get lv | ||
857 : | in if C.dead lvi then (click_deadval(); loop m le cont) else | ||
858 : | let val ndc = cdcon dc1 | ||
859 : | fun ccon sv = | ||
860 : | let val nm = addbind(m, lv, Con(lv, sv, ndc, tycs1)) | ||
861 : | val nle = loop nm le cont | ||
862 : | in if C.dead lvi then nle | ||
863 : | else F.CON(ndc, tycs1, sval2val sv, lv, nle) | ||
864 : | end | ||
865 : | in case val2sval m v | ||
866 : | of sv as (Decon (lvd,sv',dc2,tycs2)) => | ||
867 : | if FU.dcon_eq(dc1, dc2) andalso tycs_eq(tycs1,tycs2) then | ||
868 : | (click_con(); | ||
869 : | loop (substitute(m, lv, sv', F.VAR lvd)) le cont) | ||
870 : | else ccon sv | ||
871 : | | sv => ccon sv | ||
872 : | monnier | 189 | end |
873 : | monnier | 201 | end |
874 : | monnier | 121 | |
875 : | monnier | 201 | fun fcRecord (rk,vs,lv,le) = |
876 : | (* g: check whether the record already exists *) | ||
877 : | let val lvi = C.get lv | ||
878 : | in if C.dead lvi then (click_deadval(); loop m le cont) else | ||
879 : | let fun g (Select(_,sv,0)::ss) = | ||
880 : | let fun g' (n,Select(_,sv',i)::ss) = | ||
881 : | if n = i andalso (sval2val sv) = (sval2val sv') | ||
882 : | then g'(n+1,ss) else NONE | ||
883 : | monnier | 204 | | g' (n,[]) = |
884 : | monnier | 201 | (case sval2lty sv |
885 : | of SOME lty => | ||
886 : | monnier | 204 | let val ltd = |
887 : | case (rk, LT.ltp_tyc lty) | ||
888 : | of (F.RK_STRUCT,false) => LT.ltd_str | ||
889 : | | (F.RK_TUPLE _,true) => LT.ltd_tuple | ||
890 : | (* we might select out of a struct | ||
891 : | * into a tuple or vice-versa *) | ||
892 : | | _ => (fn _ => []) | ||
893 : | monnier | 201 | in if length(ltd lty) = n |
894 : | then SOME sv else NONE | ||
895 : | end | ||
896 : | | _ => (click_lacktype(); NONE)) (* sad *) | ||
897 : | | g' _ = NONE | ||
898 : | in g'(1,ss) | ||
899 : | end | ||
900 : | | g _ = NONE | ||
901 : | val svs = map (val2sval m) vs | ||
902 : | in case g svs | ||
903 : | of SOME sv => (click_record(); | ||
904 : | loop (substitute(m, lv, sv, F.INT 0)) le cont | ||
905 : | before app (unuseval m) vs) | ||
906 : | | _ => | ||
907 : | let val nm = addbind(m, lv, Record(lv, svs)) | ||
908 : | val nle = loop nm le cont | ||
909 : | in if C.dead lvi then nle | ||
910 : | else F.RECORD(rk, map sval2val svs, lv, nle) | ||
911 : | end | ||
912 : | monnier | 189 | end |
913 : | monnier | 201 | end |
914 : | monnier | 121 | |
915 : | monnier | 201 | fun fcSelect (v,i,lv,le) = |
916 : | let val lvi = C.get lv | ||
917 : | in if C.dead lvi then (click_deadval(); loop m le cont) else | ||
918 : | (case val2sval m v | ||
919 : | of Record (lvr,svs) => | ||
920 : | let val sv = List.nth(svs, i) | ||
921 : | in click_select(); | ||
922 : | loop (substitute(m, lv, sv, F.VAR lvr)) le cont | ||
923 : | end | ||
924 : | | sv => | ||
925 : | let val nm = addbind (m, lv, Select(lv, sv, i)) | ||
926 : | val nle = loop nm le cont | ||
927 : | in if C.dead lvi then nle | ||
928 : | else F.SELECT(sval2val sv, i, lv, nle) | ||
929 : | end) | ||
930 : | end | ||
931 : | monnier | 121 | |
932 : | monnier | 201 | fun fcBranch (po,vs,le1,le2) = |
933 : | let val nvs = map substval vs | ||
934 : | val npo = cpo po | ||
935 : | val nle1 = loop m le1 #2 | ||
936 : | val nle2 = loop m le2 #2 | ||
937 : | in cont(m, F.BRANCH(npo, nvs, nle1, nle2)) | ||
938 : | end | ||
939 : | monnier | 121 | |
940 : | monnier | 201 | fun fcPrimop (po,vs,lv,le) = |
941 : | let val lvi = C.get lv | ||
942 : | monnier | 204 | val pure = not(PO.effect(#2 po)) |
943 : | monnier | 201 | in if pure andalso C.dead lvi then (click_deadval();loop m le cont) else |
944 : | monnier | 199 | let val nvs = map substval vs |
945 : | monnier | 121 | val npo = cpo po |
946 : | monnier | 201 | val nm = addbind(m, lv, Var(lv,NONE)) |
947 : | val nle = loop nm le cont | ||
948 : | in | ||
949 : | if pure andalso C.dead lvi then nle | ||
950 : | else F.PRIMOP(npo, nvs, lv, nle) | ||
951 : | monnier | 121 | end |
952 : | monnier | 201 | end |
953 : | monnier | 121 | |
954 : | monnier | 201 | in case le |
955 : | of F.RET vs => cont(m, F.RET(map substval vs)) | ||
956 : | | F.LET x => fcLet x | ||
957 : | | F.FIX x => fcFix x | ||
958 : | | F.APP x => fcApp x | ||
959 : | | F.TFN x => fcTfn x | ||
960 : | | F.TAPP (f,tycs) => cont(m, F.TAPP(substval f, tycs)) | ||
961 : | | F.SWITCH x => fcSwitch x | ||
962 : | | F.CON x => fcCon x | ||
963 : | | F.RECORD x => fcRecord x | ||
964 : | | F.SELECT x => fcSelect x | ||
965 : | | F.RAISE (v,ltys) => cont(m, F.RAISE(substval v, ltys)) | ||
966 : | | F.HANDLE (le,v) => cont(m, F.HANDLE(loop m le #2, substval v)) | ||
967 : | | F.BRANCH x => fcBranch x | ||
968 : | | F.PRIMOP x => fcPrimop x | ||
969 : | monnier | 121 | end |
970 : | |||
971 : | monnier | 185 | in |
972 : | (* C.collect fdec; *) | ||
973 : | monnier | 201 | case fcexp S.empty |
974 : | monnier | 185 | M.empty (F.FIX([fdec], F.RET[F.VAR f])) #2 |
975 : | of F.FIX([fdec], F.RET[F.VAR f]) => fdec | ||
976 : | | fdec => bug "invalid return fundec" | ||
977 : | end | ||
978 : | monnier | 121 | |
979 : | end | ||
980 : | end |
root@smlnj-gforge.cs.uchicago.edu | ViewVC Help |
Powered by ViewVC 1.0.0 |