Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files

SCM Repository

[smlnj] Annotation of /sml/trunk/src/compiler/FLINT/opt/fcontract.sml
ViewVC logotype

Annotation of /sml/trunk/src/compiler/FLINT/opt/fcontract.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 204 - (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 190 val contract : FLINT.prog * Stats.counter -> 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 189 fun click s c = (if !CTRL.misc = 1 then say s else (); Stats.addCounter c 1)
280 : monnier 185
281 : monnier 189 (* val c_inline = Stats.newCounter[] *)
282 :     (* val c_deadval = Stats.newCounter[] *)
283 :     (* val c_deadlexp = Stats.newCounter[] *)
284 :     (* val c_select = Stats.newCounter[] *)
285 :     (* val c_record = Stats.newCounter[] *)
286 :     (* val c_lacktype = Stats.newCounter[] *)
287 :     (* val c_con = Stats.newCounter[] *)
288 :     (* val c_switch = Stats.newCounter[] *)
289 :     (* val c_eta = Stats.newCounter[] *)
290 :     (* val c_etasplit = Stats.newCounter[] *)
291 :     (* val c_branch = Stats.newCounter[] *)
292 :     (* val c_dropargs = Stats.newCounter[] *)
293 : monnier 202 val c_cstarg = Stats.newCounter[]
294 :     val c_outofscope = Stats.newCounter[]
295 : monnier 203 (* (* val _ = Stats.registerStat(Stats.newStat("FC-cstarg", [c_cstarg])) *) *)
296 :     (* (* val _ = Stats.registerStat(Stats.newStat("FC-outofscope", [c_outofscope])) *) *)
297 : monnier 185
298 : monnier 189 fun contract (fdec as (_,f,_,_), counter) = let
299 :    
300 :     val c_dummy = Stats.newCounter[]
301 :     val c_miss = Stats.newCounter[]
302 :    
303 :     fun click_deadval () = (click "d" counter)
304 :     fun click_deadlexp () = (click "D" counter)
305 :     fun click_select () = (click "s" counter)
306 :     fun click_record () = (click "r" counter)
307 :     fun click_con () = (click "c" counter)
308 :     fun click_switch () = (click "s" counter)
309 :     fun click_eta () = (click "e" counter)
310 :     fun click_etasplit () = (click "E" counter)
311 :     fun click_branch () = (click "b" counter)
312 :     fun click_dropargs () = (click "a" counter)
313 :    
314 :     fun click_lacktype () = (click "t" c_miss)
315 :    
316 :     (* this counters is actually *used* by fcontract.
317 :     * It's not used just for statistics. *)
318 :     val c_inline = Stats.newCounter[counter]
319 :     (* val c_inline1 = Stats.newCounter[c_inline] *)
320 :     (* val c_inline2 = Stats.newCounter[c_inline] *)
321 :     (* val c_unroll = Stats.newCounter[c_inline] *)
322 :     fun click_simpleinline () = (click "i" c_inline)
323 :     fun click_copyinline () = (click "I" c_inline)
324 :     fun click_unroll () = (click "u" c_inline)
325 :     fun inline_count () = Stats.getCounter c_inline
326 :    
327 : monnier 186 fun used lv = (C.usenb(C.get lv) > 0)
328 : monnier 199 (* handle x =>
329 : monnier 186 (say("while in FContract.used "^(C.LVarString lv)^"\n");
330 : monnier 199 raise x) *)
331 : monnier 121
332 :     fun eqConV (F.INTcon i1, F.INT i2) = i1 = i2
333 :     | eqConV (F.INT32con i1, F.INT32 i2) = i1 = i2
334 :     | eqConV (F.WORDcon i1, F.WORD i2) = i1 = i2
335 :     | eqConV (F.WORD32con i1, F.WORD32 i2) = i1 = i2
336 :     | eqConV (F.REALcon r1, F.REAL r2) = r1 = r2
337 :     | eqConV (F.STRINGcon s1, F.STRING s2) = s1 = s2
338 :     | eqConV (con,v) = bugval("unexpected comparison with val", v)
339 :    
340 :     fun lookup m lv = (M.lookup m lv)
341 : monnier 202 handle e as M.IntmapF =>
342 : monnier 121 (say "\nlooking up unbound ";
343 :     say (!PP.LVarString lv);
344 : monnier 202 raise e)
345 : monnier 121
346 :     fun sval2val sv =
347 :     case sv
348 : monnier 159 of (Fun{1=lv,...} | TFun{1=lv,...} | Record{1=lv,...} | Decon{1=lv,...}
349 : monnier 121 | Con{1=lv,...} | Select{1=lv,...} | Var{1=lv,...}) => F.VAR lv
350 :     | Val v => v
351 :    
352 : monnier 163 fun val2sval m (F.VAR ov) =
353 : monnier 199 ((lookup m ov) (* handle x =>
354 :     (say("val2sval "^(C.LVarString ov)^"\n"); raise x) *) )
355 : monnier 121 | val2sval m v = Val v
356 :    
357 :     fun bugsv (msg,sv) = bugval(msg, sval2val sv)
358 :    
359 :     fun subst m ov = sval2val (lookup m ov)
360 : monnier 199 fun substval m = sval2val o (val2sval m)
361 :     fun substvar m lv =
362 :     case substval m (F.VAR lv)
363 : monnier 121 of F.VAR lv => lv
364 :     | v => bugval ("unexpected val", v)
365 :    
366 :     (* called when a variable becomes dead.
367 :     * it simply adjusts the use-counts *)
368 :     fun undertake m lv =
369 :     let val undertake = undertake m
370 :     in case lookup m lv
371 : monnier 186 of Var {1=nlv,...} => ()
372 : monnier 121 | Val v => ()
373 : monnier 202 | Fun (lv,le,args,_,_) =>
374 : monnier 187 C.unuselexp undertake
375 :     (F.LET(map #1 args,
376 :     F.RET (map (fn _ => F.INT 0) args),
377 :     le))
378 :     | TFun{1=lv,2=le,...} =>
379 :     C.unuselexp undertake le
380 : monnier 189 | (Select {2=sv,...} | Con {2=sv,...}) => unusesval m sv
381 :     | Record {2=svs,...} => app (unusesval m) svs
382 : monnier 159 (* decon's are implicit so we can't get rid of them *)
383 :     | Decon _ => ()
384 : monnier 121 end
385 :     handle M.IntmapF =>
386 : monnier 186 (say("Unable to undertake "^(C.LVarString lv)^"\n"))
387 : monnier 121 | x =>
388 : monnier 186 (say("while undertaking "^(C.LVarString lv)^"\n"); raise x)
389 : monnier 121
390 : monnier 189 and unusesval m sv = unuseval m (sval2val sv)
391 : monnier 187 and unuseval m (F.VAR lv) =
392 :     if (C.unuse false (C.get lv)) then undertake m lv else ()
393 :     | unuseval f _ = ()
394 :     fun unusecall m lv =
395 :     if (C.unuse true (C.get lv)) then undertake m lv else ()
396 :    
397 :    
398 : monnier 121 fun addbind (m,lv,sv) = M.add(m, lv, sv)
399 :    
400 : monnier 164 (* substitute a value sv for a variable lv and unuse value v. *)
401 : monnier 121 fun substitute (m, lv1, sv, v) =
402 :     (case sval2val sv of F.VAR lv2 => C.transfer(lv1,lv2) | v2 => ();
403 : monnier 187 unuseval m v;
404 : monnier 199 addbind(m, lv1, sv)) (* handle x =>
405 : monnier 186 (say ("while substituting "^
406 : monnier 164 (C.LVarString lv1)^
407 :     " -> ");
408 : monnier 121 PP.printSval (sval2val sv);
409 : monnier 199 raise x) *)
410 : monnier 121
411 :     (* common code for primops *)
412 : monnier 199 fun cpo m (SOME{default,table},po,lty,tycs) =
413 :     (SOME{default=substvar m default,
414 :     table=map (fn (tycs,lv) => (tycs, substvar m lv)) table},
415 : monnier 121 po,lty,tycs)
416 : monnier 199 | cpo _ po = po
417 : monnier 121
418 : monnier 199 fun cdcon m (s,Access.EXN(Access.LVAR lv),lty) =
419 :     (s, Access.EXN(Access.LVAR(substvar m lv)), lty)
420 :     | cdcon _ dc = dc
421 : monnier 121
422 : monnier 201 (* ifs (inlined functions): records which functions we're currently inlining
423 : monnier 199 * in order to detect loops
424 :     * m: is a map lvars to their defining expressions (svals) *)
425 : monnier 201 fun fcexp ifs m le cont = let
426 :     val loop = fcexp ifs
427 : monnier 199 val substval = substval m
428 :     val cdcon = cdcon m
429 :     val cpo = cpo m
430 : monnier 163
431 : monnier 201 fun fcLet (lvs,le,body) =
432 :     loop m le
433 :     (fn (nm,nle) =>
434 :     let fun cbody () =
435 :     let val nm = (foldl (fn (lv,m) =>
436 :     addbind(m, lv, Var(lv, NONE)))
437 :     nm lvs)
438 :     in case loop nm body cont
439 :     of F.RET vs => if vs = (map F.VAR lvs) then nle
440 :     else F.LET(lvs, nle, F.RET vs)
441 :     | nbody => F.LET(lvs, nle, nbody)
442 :     end
443 :     in case nle
444 :     of F.RET vs =>
445 :     let fun simplesubst (lv,v,m) =
446 :     let val sv = val2sval m v
447 :     in substitute(m, lv, sv, sval2val sv)
448 :     end
449 :     val nm = (ListPair.foldl simplesubst nm (lvs, vs))
450 :     in loop nm body cont
451 :     end
452 :     | F.TAPP _ =>
453 :     if List.all (C.dead o C.get) lvs
454 :     then loop nm body cont
455 :     else cbody()
456 :     | (F.BRANCH _ | F.SWITCH _) =>
457 :     (* this is a hack originally meant to cleanup the BRANCH
458 :     * mess introduced in flintnm (where each branch returns
459 :     * just true or false which is generally only used as
460 :     * input to a SWITCH).
461 :     * The present code does more than clean up this case.
462 :     * It has one serious shortcoming: it ends up making
463 :     * three fcontract passes through the same code (plus
464 :     * one cheap traversal). *)
465 :     let fun cassoc (lv,F.SWITCH(F.VAR v,ac,arms,NONE),wrap) =
466 :     if lv <> v orelse C.usenb(C.get lv) > 1
467 :     then cbody() else
468 :     let val (narms,fdecs) =
469 :     ListPair.unzip (map extract arms)
470 :     fun addswitch [v] =
471 :     C.copylexp
472 :     IntmapF.empty
473 :     (F.SWITCH(v,ac,narms,NONE))
474 :     | addswitch _ = bug "prob in addswitch"
475 :     (* replace each leaf `ret' with a copy
476 :     * of the switch *)
477 :     val nle = append [lv] addswitch nle
478 :     (* decorate with the functions extracted
479 :     * from the switch arms *)
480 :     val nle =
481 :     foldl (fn (f,le) => F.FIX([f],le))
482 :     (wrap nle) fdecs
483 :     (* Ugly hack: force one more traversal *)
484 :     val nle = loop nm nle #2
485 :     in click_branch();
486 :     loop nm nle cont
487 :     end
488 :     | cassoc _ = cbody()
489 :     in case (lvs,body)
490 :     of ([lv],le as F.SWITCH _) =>
491 :     cassoc(lv, le, fn x => x)
492 :     | ([lv],F.LET(lvs,le as F.SWITCH _,rest)) =>
493 :     cassoc(lv, le, fn le => F.LET(lvs,le,rest))
494 :     | _ => cbody()
495 :     end
496 :     | _ => cbody()
497 :     end)
498 : monnier 200
499 : monnier 201 fun fcFix (fs,le) =
500 : monnier 202 let (* merge actual arguments to extract the constant subpart *)
501 :     fun merge_actuals ((lv,lty),[],m) = addbind(m, lv, Var(lv, SOME lty))
502 :     | merge_actuals ((lv,lty),a::bs,m) = addbind(m, lv, Var(lv, SOME lty))
503 :     (* FIXME: there's a bug here, but it's not caught by chkflint
504 :     let fun f (b::bs) =
505 :     if sval2val a = sval2val b then f bs
506 :     else addbind(m, lv, Var(lv, SOME lty))
507 :     | f [] =
508 :     (click "C" c_cstarg;
509 :     case sval2val a
510 :     of v as F.VAR lv' =>
511 :     (* FIXME: this inScope check is wrong for non-recursive
512 :     * functions. But it only matters if the function is
513 :     * passed itself as a parameter which cannot happen
514 :     * with the current type system I believe. *)
515 :     if inScope m lv' then
516 :     let val sv =
517 :     case a of Var (v,NONE) => Var(v, SOME lty)
518 :     | _ => a
519 :     in substitute(m, lv, sv, v)
520 :     end
521 :     else (click "O" c_outofscope;
522 :    
523 :     addbind(m, lv, Var(lv, SOME lty)))
524 :     | v => substitute(m, lv, a, v))
525 :     in f bs
526 :     end *)
527 :     (* The actual function contraction *)
528 : monnier 203 fun fcFun ((f,body,args,fk as {inline,cconv,known,isrec},actuals),
529 :     (m,fs)) =
530 : monnier 201 let val fi = C.get f
531 : monnier 203 in if C.dead fi then (m,fs)
532 : monnier 201 else if C.iusenb fi = C.usenb fi then
533 :     (* we need to be careful that undertake not be called
534 :     * recursively *)
535 : monnier 203 (C.use NONE fi; undertake m f; (m,fs))
536 : monnier 201 else
537 : monnier 204 let (* val _ = say ("Entering "^(C.LVarString f)^"\n") *)
538 : monnier 201 val saved_ic = inline_count()
539 :     (* make up the bindings for args inside the body *)
540 : monnier 202 val actuals = if isSome isrec orelse
541 :     C.escaping fi orelse
542 :     null(!actuals)
543 :     then map (fn _ => []) args
544 :     else OU.transpose(!actuals)
545 :     val nm = ListPair.foldl merge_actuals m (args, actuals)
546 : monnier 201 (* contract the body and create the resulting fundec *)
547 :     val nbody = fcexp (S.add(f, ifs)) nm body #2
548 :     (* if inlining took place, the body might be completely
549 :     * changed (read: bigger), so we have to reset the
550 :     * `inline' bit *)
551 :     val nfk = {isrec=isrec, cconv=cconv,
552 :     known=known orelse not(C.escaping fi),
553 :     inline=if inline_count() = saved_ic
554 :     then inline
555 :     else F.IH_SAFE}
556 :     (* update the binding in the map. This step is
557 :     * not just a mere optimization but is necessary
558 :     * because if we don't do it and the function
559 :     * gets inlined afterwards, the counts will reflect the
560 :     * new contracted code while we'll be working on the
561 :     * the old uncontracted code *)
562 : monnier 203 val nm = addbind(m, f, Fun(f, nbody, args, nfk, ref []))
563 :     in (nm, (nfk, f, args, nbody)::fs)
564 : monnier 204 (* before say ("Exiting "^(C.LVarString f)^"\n") *)
565 : monnier 201 end
566 :     end
567 :    
568 :     (* check for eta redex *)
569 : monnier 202 fun fcEta (fdec as (f,F.APP(F.VAR g,vs),args,_,_),(m,fs,hs)) =
570 : monnier 201 if List.length args = List.length vs andalso
571 :     OU.ListPair_all (fn (v,(lv,t)) =>
572 :     case v of F.VAR v => v = lv andalso lv <> g
573 :     | _ => false)
574 :     (vs, args)
575 :     then
576 :     let val svg = lookup m g
577 :     val g = case sval2val svg
578 :     of F.VAR g => g
579 :     | v => bugval("not a variable", v)
580 :     (* NOTE: we don't want to turn a known function into an
581 :     * escaping one. It's dangerous for optimisations based
582 :     * on known functions (elimination of dead args, f.ex)
583 :     * and could generate cases where call>use in collect *)
584 :     in if (C.escaping(C.get f)) andalso not(C.escaping(C.get g))
585 :     (* the default case could ensure the inline *)
586 :     then (m, fdec::fs, hs)
587 :     else let
588 :     (* if an earlier function h has been eta-reduced
589 :     * to f, we have to be careful to update its
590 :     * binding to not refer to f any more since f
591 :     * will disappear *)
592 :     val nm = foldl (fn (h,m) =>
593 :     if sval2val(lookup m h) = F.VAR f
594 :     then addbind(m, h, svg) else m)
595 :     m hs
596 :     in
597 :     (* I could almost reuse `substitute' but the
598 :     * unuse in substitute assumes the val is escaping *)
599 :     click_eta();
600 :     C.transfer(f, g);
601 :     unusecall m g;
602 :     (addbind(m, f, svg), fs, f::hs)
603 :     end
604 : monnier 189 end
605 : monnier 201 else (m, fdec::fs, hs)
606 :     | fcEta (fdec,(m,fs,hs)) = (m,fdec::fs,hs)
607 :    
608 :     (* add wrapper for various purposes *)
609 :     fun wrap (f as ({inline=F.IH_ALWAYS,...},_,_,_):F.fundec,fs) = f::fs
610 :     | wrap (f as (fk as {isrec,...},g,args,body):F.fundec,fs) =
611 :     let val gi = C.get g
612 :     fun dropargs filter =
613 :     let val (nfk,nfk') = OU.fk_wrap(fk, O.map #1 isrec)
614 :     val args' = filter args
615 :     val ng = cplv g
616 :     val nargs = map (fn (v,t) => (cplv v, t)) args
617 :     val nargs' = map #1 (filter nargs)
618 :     val appargs = (map F.VAR nargs')
619 :     val nf = (nfk, g, nargs, F.APP(F.VAR ng, appargs))
620 :     val nf' = (nfk', ng, args', body)
621 :    
622 :     val ngi = C.new (SOME(map #1 args')) ng
623 :     in
624 :     C.ireset gi;
625 :     app (ignore o (C.new NONE) o #1) nargs;
626 :     C.use (SOME appargs) ngi;
627 :     app (C.use NONE o C.get) nargs';
628 :     nf'::nf::fs
629 : monnier 121 end
630 : monnier 201 in
631 :     (* Don't introduce wrappers for escaping-only functions.
632 :     * This is debatable since although wrappers are useless
633 :     * on escaping-only functions, some of the escaping uses
634 :     * might turn into calls in the course of fcontract, so
635 :     * by not introducing wrappers here, we avoid useless work
636 :     * but we also postpone useful work to later invocations. *)
637 :     if C.dead gi then fs else
638 :     let val used = map (used o #1) args
639 :     in if C.called gi then
640 :     (* if some args are not used, let's drop them *)
641 :     if not (List.all (fn x => x) used) then
642 :     (click_dropargs();
643 : monnier 203 dropargs (fn xs => OU.filter used xs))
644 : monnier 190
645 : monnier 201 (* eta-split: add a wrapper for escaping uses *)
646 :     else if C.escaping gi then
647 :     (* like dropargs but keeping all args *)
648 :     (click_etasplit(); dropargs (fn x => x))
649 :    
650 :     else f::fs
651 :     else f::fs
652 :     end
653 :     end
654 :    
655 :     (* add various wrappers *)
656 :     val fs = foldl wrap [] fs
657 :    
658 :     (* register the new bindings (uncontracted for now) *)
659 : monnier 202 val (nm,fs) = foldl (fn (fdec as (fk,f,args,body),(m,fs)) =>
660 :     let val nf = (f, body, args, fk, ref [])
661 :     in (addbind(m, f, Fun nf), nf::fs) end)
662 :     (m,[]) fs
663 : monnier 201 (* check for eta redexes *)
664 :     val (nm,fs,_) = foldl fcEta (nm,[],[]) fs
665 :    
666 : monnier 204 val (wrappers,funs) =
667 : monnier 202 List.partition (fn (_,_,_,{inline=F.IH_ALWAYS,...},_) => true
668 : monnier 201 | _ => false) fs
669 : monnier 204 val (maybes,funs) =
670 : monnier 203 List.partition (fn (_,_,_,{inline=F.IH_MAYBE _,...},_) => true
671 :     | _ => false) funs
672 : monnier 201
673 : monnier 204 (* First contract the big inlinable functions. This might make them
674 :     * non-inlinable and we'd rather know that before we inline them.
675 :     * Then we inline the body (so that we won't go through the inline-once
676 :     * functions twice), then the normal functions and finally the wrappers
677 :     * (which need to come last to make sure that they get inlined if
678 :     * at all possible) *)
679 :     val fs = []
680 :     val (nm,fs) = foldl fcFun (nm,fs) maybes
681 : monnier 201 val nle = loop nm le cont
682 : monnier 203 val (nm,fs) = foldl fcFun (nm,fs) funs
683 :     val (nm,fs) = foldl fcFun (nm,fs) wrappers
684 : monnier 201 (* junk newly unused funs *)
685 :     val fs = List.filter (used o #2) fs
686 :     in
687 :     case fs
688 :     of [] => nle
689 :     | [f1 as ({isrec=NONE,...},_,_,_),f2] =>
690 :     (* gross hack: `wrap' might have added a second
691 :     * non-recursive function. we need to split them into
692 : monnier 203 * 2 FIXes. This is _very_ ad-hoc. *)
693 : monnier 204 F.FIX([f2], F.FIX([f1], nle))
694 : monnier 201 | _ => F.FIX(fs, nle)
695 :     end
696 : monnier 163
697 : monnier 201 fun fcApp (f,vs) =
698 : monnier 202 let val svs = map (val2sval m) vs
699 : monnier 201 val svf = val2sval m f
700 :     (* F.APP inlining (if any) *)
701 : monnier 202
702 : monnier 201 in case svf
703 : monnier 202 of Fun(g,body,args,{inline,...},actuals) =>
704 :     let val gi = C.get g
705 :     fun noinline () =
706 :     (actuals := svs :: (!actuals);
707 :     cont(m,F.APP(sval2val svf, map sval2val svs)))
708 :     fun simpleinline () =
709 :     (* simple inlining: we should copy the body and then
710 :     * kill the function, but instead we just move the body
711 :     * and kill only the function name.
712 :     * This inlining strategy looks inoffensive enough,
713 :     * but still requires some care: see comments at the
714 :     * begining of this file and in cfun *)
715 : monnier 204 ((* say("SimpleInline of "^(C.LVarString g)^"\n"); *)
716 :     click_simpleinline();
717 : monnier 202 ignore(C.unuse true gi);
718 :     loop m (F.LET(map #1 args, F.RET vs, body)) cont)
719 :     fun copyinline () =
720 :     (* aggressive inlining. We allow pretty much
721 :     * any inlinling, but we detect and reject inlining
722 :     * recursively which would else lead to infinite loop *)
723 :     (* unrolling is not as straightforward as it seems:
724 :     * if you inline the function you're currently
725 :     * fcontracting, you're asking for trouble: there is a
726 :     * hidden assumption in the counting that the old code
727 :     * will be replaced by the new code (and is hence dead).
728 :     * If the function to be unrolled has the only call to
729 :     * function f, then f might get simpleinlined before
730 :     * unrolling, which means that unrolling will introduce
731 :     * a second occurence of the `only call' but at that point
732 :     * f has already been killed. *)
733 :     let val nle = (F.LET(map #1 args, F.RET vs, body))
734 :     val nle = C.copylexp M.empty nle
735 :     in
736 : monnier 204 (* say("CopyInline of "^(C.LVarString g)^"\n"); *)
737 : monnier 202 click_copyinline();
738 :     (app (unuseval m) vs);
739 :     unusecall m g;
740 :     fcexp (S.add(g, ifs)) m nle cont
741 :     end
742 :    
743 :     in if C.usenb gi = 1 andalso not(S.member ifs g) then simpleinline()
744 :     else case inline of
745 :     F.IH_SAFE => noinline()
746 :     | F.IH_UNROLL => noinline()
747 :     | F.IH_ALWAYS =>
748 :     if S.member ifs g then noinline() else copyinline()
749 :     | F.IH_MAYBE(min,ws) =>
750 :     if S.member ifs g then noinline() else let
751 :     fun value w _ (Val _ | Con _ | Record _) = w
752 :     | value w v (Fun (f,_,args,_,_)) =
753 :     if C.usenb(C.get v) = 1 then w * 2 else w
754 :     | value w _ _ = 0
755 :     val s = (OU.foldl3 (fn (sv,w,(v,t),s) => value w v sv + s)
756 :     0 (svs,ws,args))
757 :     handle OU.Unbalanced => 0
758 :     in if s > min then copyinline() else noinline()
759 :     end
760 :     end
761 :     | sv => cont(m,F.APP(sval2val svf, map sval2val svs))
762 : monnier 201 end
763 : monnier 184
764 : monnier 201 fun fcTfn ((f,args,body),le) =
765 :     let val fi = C.get f
766 :     in if C.dead fi then (click_deadlexp(); loop m le cont) else
767 :     let val nbody = fcexp ifs m body #2
768 :     val nm = addbind(m, f, TFun(f, nbody, args))
769 : monnier 184 val nle = loop nm le cont
770 : monnier 121 in
771 : monnier 201 if C.dead fi then nle else F.TFN((f, args, nbody), nle)
772 : monnier 121 end
773 : monnier 201 end
774 :    
775 :     fun fcSwitch (v,ac,arms,def) =
776 :     let fun fcsCon (lvc,svc,dc1:F.dcon,tycs1) =
777 :     let fun killle le = C.unuselexp (undertake m) le
778 :     fun kill lv le =
779 :     C.unuselexp (undertake (addbind(m,lv,Var(lv,NONE)))) le
780 :     fun killarm (F.DATAcon(_,_,lv),le) = kill lv le
781 :     | killarm _ = buglexp("bad arm in switch(con)", le)
782 :    
783 :     fun carm ((F.DATAcon(dc2,tycs2,lv),le)::tl) =
784 :     (* sometimes lty1 <> lty2 :-( so this doesn't work:
785 :     * FU.dcon_eq(dc1, dc2) andalso tycs_eq(tycs1,tycs2) *)
786 :     if #2 dc1 = #2 (cdcon dc2) then
787 :     (map killarm tl; (* kill the rest *)
788 :     O.map killle def; (* and the default case *)
789 :     loop (substitute(m, lv, svc, F.VAR lvc))
790 :     le cont)
791 :     else
792 :     (* kill this arm and continue with the rest *)
793 :     (kill lv le; carm tl)
794 :     | carm [] = loop m (O.valOf def) cont
795 :     | carm _ = buglexp("unexpected arm in switch(con,...)", le)
796 :     in click_switch(); carm arms
797 : monnier 186 end
798 : monnier 121
799 : monnier 201 fun fcsVal v =
800 :     let fun kill le = C.unuselexp (undertake m) le
801 :     fun carm ((con,le)::tl) =
802 :     if eqConV(con, v) then
803 :     (map (kill o #2) tl;
804 :     O.map kill def;
805 :     loop m le cont)
806 :     else (kill le; carm tl)
807 :     | carm [] = loop m (O.valOf def) cont
808 :     in click_switch(); carm arms
809 :     end
810 :    
811 :     fun fcsDefault (sv,lvc) =
812 :     case (arms,def)
813 :     of ([(F.DATAcon(dc,tycs,lv),le)],NONE) =>
814 :     (* this is a mere DECON, so we can push the let binding
815 :     * (hidden in cont) inside and maybe even drop the DECON *)
816 :     let val ndc = cdcon dc
817 :     val slv = Decon(lv, sv, ndc, tycs)
818 :     val nm = addbind(m, lv, slv)
819 :     (* see below *)
820 :     (* val nm = addbind(nm, lvc, Con(lvc, slv, ndc, tycs)) *)
821 :     val nle = loop nm le cont
822 :     val nv = sval2val sv
823 :     in
824 :     if used lv then
825 :     F.SWITCH(nv,ac,[(F.DATAcon(ndc,tycs,lv),nle)],NONE)
826 :     else (unuseval m nv; nle)
827 :     end
828 :     | (([(_,le)],NONE) | ([],SOME le)) =>
829 :     (* This should never happen, but we can optimize it away *)
830 :     (unuseval m (sval2val sv); loop m le cont)
831 :     | _ =>
832 :     let fun carm (F.DATAcon(dc,tycs,lv),le) =
833 :     let val ndc = cdcon dc
834 :     val slv = Decon(lv, sv, ndc, tycs)
835 :     val nm = addbind(m, lv, slv)
836 :     (* we can rebind lv to a more precise value
837 :     * !!BEWARE!! This rebinding is misleading:
838 :     * - it gives the impression that `lvc' is built
839 :     * from`lv' although the reverse is true:
840 :     * if `lvc' is undertaken, `lv's count should
841 :     * *not* be updated!
842 :     * Luckily, `lvc' will not become dead while
843 :     * rebound to Con(lv) because it's used by the
844 :     * SWITCH. All in all, it works fine, but it's
845 :     * not as straightforward as it seems.
846 :     * - it seems to be a good idea, but it can hide
847 :     * other opt-opportunities since it hides the
848 :     * previous binding. *)
849 :     (* val nm = addbind(nm, lvc, Con(lvc,slv,ndc,tycs)) *)
850 :     in (F.DATAcon(ndc, tycs, lv), loop nm le #2)
851 :     end
852 :     | carm (con,le) = (con, loop m le #2)
853 :     val narms = map carm arms
854 :     val ndef = Option.map (fn le => loop m le #2) def
855 :     in cont(m, F.SWITCH(sval2val sv, ac, narms, ndef))
856 :     end
857 : monnier 121
858 : monnier 201 in case val2sval m v
859 :     of sv as Con x => fcsCon x
860 :     | sv as Val v => fcsVal v
861 :     | sv as (Var{1=lvc,...} | Select{1=lvc,...} | Decon{1=lvc, ...}
862 :     | (* will probably never happen *) Record{1=lvc,...}) =>
863 :     fcsDefault(sv, lvc)
864 :     | sv as (Fun _ | TFun _) =>
865 :     bugval("unexpected switch arg", sval2val sv)
866 :     end
867 : monnier 159
868 : monnier 201 fun fcCon (dc1,tycs1,v,lv,le) =
869 :     let val lvi = C.get lv
870 :     in if C.dead lvi then (click_deadval(); loop m le cont) else
871 :     let val ndc = cdcon dc1
872 :     fun ccon sv =
873 :     let val nm = addbind(m, lv, Con(lv, sv, ndc, tycs1))
874 :     val nle = loop nm le cont
875 :     in if C.dead lvi then nle
876 :     else F.CON(ndc, tycs1, sval2val sv, lv, nle)
877 :     end
878 :     in case val2sval m v
879 :     of sv as (Decon (lvd,sv',dc2,tycs2)) =>
880 :     if FU.dcon_eq(dc1, dc2) andalso tycs_eq(tycs1,tycs2) then
881 :     (click_con();
882 :     loop (substitute(m, lv, sv', F.VAR lvd)) le cont)
883 :     else ccon sv
884 :     | sv => ccon sv
885 : monnier 189 end
886 : monnier 201 end
887 : monnier 121
888 : monnier 201 fun fcRecord (rk,vs,lv,le) =
889 :     (* g: check whether the record already exists *)
890 :     let val lvi = C.get lv
891 :     in if C.dead lvi then (click_deadval(); loop m le cont) else
892 :     let fun g (Select(_,sv,0)::ss) =
893 :     let fun g' (n,Select(_,sv',i)::ss) =
894 :     if n = i andalso (sval2val sv) = (sval2val sv')
895 :     then g'(n+1,ss) else NONE
896 : monnier 204 | g' (n,[]) =
897 : monnier 201 (case sval2lty sv
898 :     of SOME lty =>
899 : monnier 204 let val ltd =
900 :     case (rk, LT.ltp_tyc lty)
901 :     of (F.RK_STRUCT,false) => LT.ltd_str
902 :     | (F.RK_TUPLE _,true) => LT.ltd_tuple
903 :     (* we might select out of a struct
904 :     * into a tuple or vice-versa *)
905 :     | _ => (fn _ => [])
906 : monnier 201 in if length(ltd lty) = n
907 :     then SOME sv else NONE
908 :     end
909 :     | _ => (click_lacktype(); NONE)) (* sad *)
910 :     | g' _ = NONE
911 :     in g'(1,ss)
912 :     end
913 :     | g _ = NONE
914 :     val svs = map (val2sval m) vs
915 :     in case g svs
916 :     of SOME sv => (click_record();
917 :     loop (substitute(m, lv, sv, F.INT 0)) le cont
918 :     before app (unuseval m) vs)
919 :     | _ =>
920 :     let val nm = addbind(m, lv, Record(lv, svs))
921 :     val nle = loop nm le cont
922 :     in if C.dead lvi then nle
923 :     else F.RECORD(rk, map sval2val svs, lv, nle)
924 :     end
925 : monnier 189 end
926 : monnier 201 end
927 : monnier 121
928 : monnier 201 fun fcSelect (v,i,lv,le) =
929 :     let val lvi = C.get lv
930 :     in if C.dead lvi then (click_deadval(); loop m le cont) else
931 :     (case val2sval m v
932 :     of Record (lvr,svs) =>
933 :     let val sv = List.nth(svs, i)
934 :     in click_select();
935 :     loop (substitute(m, lv, sv, F.VAR lvr)) le cont
936 :     end
937 :     | sv =>
938 :     let val nm = addbind (m, lv, Select(lv, sv, i))
939 :     val nle = loop nm le cont
940 :     in if C.dead lvi then nle
941 :     else F.SELECT(sval2val sv, i, lv, nle)
942 :     end)
943 :     end
944 : monnier 121
945 : monnier 201 fun fcBranch (po,vs,le1,le2) =
946 :     let val nvs = map substval vs
947 :     val npo = cpo po
948 :     val nle1 = loop m le1 #2
949 :     val nle2 = loop m le2 #2
950 :     in cont(m, F.BRANCH(npo, nvs, nle1, nle2))
951 :     end
952 : monnier 121
953 : monnier 201 fun fcPrimop (po,vs,lv,le) =
954 :     let val lvi = C.get lv
955 : monnier 204 val pure = not(PO.effect(#2 po))
956 : monnier 201 in if pure andalso C.dead lvi then (click_deadval();loop m le cont) else
957 : monnier 199 let val nvs = map substval vs
958 : monnier 121 val npo = cpo po
959 : monnier 201 val nm = addbind(m, lv, Var(lv,NONE))
960 :     val nle = loop nm le cont
961 :     in
962 :     if pure andalso C.dead lvi then nle
963 :     else F.PRIMOP(npo, nvs, lv, nle)
964 : monnier 121 end
965 : monnier 201 end
966 : monnier 121
967 : monnier 201 in case le
968 :     of F.RET vs => cont(m, F.RET(map substval vs))
969 :     | F.LET x => fcLet x
970 :     | F.FIX x => fcFix x
971 :     | F.APP x => fcApp x
972 :     | F.TFN x => fcTfn x
973 :     | F.TAPP (f,tycs) => cont(m, F.TAPP(substval f, tycs))
974 :     | F.SWITCH x => fcSwitch x
975 :     | F.CON x => fcCon x
976 :     | F.RECORD x => fcRecord x
977 :     | F.SELECT x => fcSelect x
978 :     | F.RAISE (v,ltys) => cont(m, F.RAISE(substval v, ltys))
979 :     | F.HANDLE (le,v) => cont(m, F.HANDLE(loop m le #2, substval v))
980 :     | F.BRANCH x => fcBranch x
981 :     | F.PRIMOP x => fcPrimop x
982 : monnier 121 end
983 :    
984 : monnier 185 in
985 :     (* C.collect fdec; *)
986 : monnier 201 case fcexp S.empty
987 : monnier 185 M.empty (F.FIX([fdec], F.RET[F.VAR f])) #2
988 :     of F.FIX([fdec], F.RET[F.VAR f]) => fdec
989 :     | fdec => bug "invalid return fundec"
990 :     end
991 : monnier 121
992 :     end
993 :     end

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