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 202 - (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 :     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 :     fun fcFun (m,[],acc) = acc
529 :     | fcFun (m,(f,body,args,fk as {inline,cconv,known,isrec},actuals)::fs,
530 :     acc) =
531 : monnier 201 let val fi = C.get f
532 :     in if C.dead fi then fcFun(m, fs, acc)
533 :     else if C.iusenb fi = C.usenb fi then
534 :     (* we need to be careful that undertake not be called
535 :     * recursively *)
536 :     (C.use NONE fi; undertake m f; fcFun(m, fs, acc))
537 :     else
538 :     let (* val _ = say ("\nEntering "^(C.LVarString f)) *)
539 :     val saved_ic = inline_count()
540 :     (* make up the bindings for args inside the body *)
541 : monnier 202 val actuals = if isSome isrec orelse
542 :     C.escaping fi orelse
543 :     null(!actuals)
544 :     then map (fn _ => []) args
545 :     else OU.transpose(!actuals)
546 :     val nm = ListPair.foldl merge_actuals m (args, actuals)
547 : monnier 201 (* contract the body and create the resulting fundec *)
548 :     val nbody = fcexp (S.add(f, ifs)) nm body #2
549 :     (* if inlining took place, the body might be completely
550 :     * changed (read: bigger), so we have to reset the
551 :     * `inline' bit *)
552 :     val nfk = {isrec=isrec, cconv=cconv,
553 :     known=known orelse not(C.escaping fi),
554 :     inline=if inline_count() = saved_ic
555 :     then inline
556 :     else F.IH_SAFE}
557 :     (* update the binding in the map. This step is
558 :     * not just a mere optimization but is necessary
559 :     * because if we don't do it and the function
560 :     * gets inlined afterwards, the counts will reflect the
561 :     * new contracted code while we'll be working on the
562 :     * the old uncontracted code *)
563 : monnier 202 val nm = if null fs then nm else
564 :     addbind(m, f, Fun(f, nbody, args, nfk, ref []))
565 : monnier 201 in fcFun(nm, fs, (nfk, f, args, nbody)::acc)
566 :     (* before say ("\nExiting "^(C.LVarString f)) *)
567 :     end
568 :     end
569 :    
570 :     (* check for eta redex *)
571 : monnier 202 fun fcEta (fdec as (f,F.APP(F.VAR g,vs),args,_,_),(m,fs,hs)) =
572 : monnier 201 if List.length args = List.length vs andalso
573 :     OU.ListPair_all (fn (v,(lv,t)) =>
574 :     case v of F.VAR v => v = lv andalso lv <> g
575 :     | _ => false)
576 :     (vs, args)
577 :     then
578 :     let val svg = lookup m g
579 :     val g = case sval2val svg
580 :     of F.VAR g => g
581 :     | v => bugval("not a variable", v)
582 :     (* NOTE: we don't want to turn a known function into an
583 :     * escaping one. It's dangerous for optimisations based
584 :     * on known functions (elimination of dead args, f.ex)
585 :     * and could generate cases where call>use in collect *)
586 :     in if (C.escaping(C.get f)) andalso not(C.escaping(C.get g))
587 :     (* the default case could ensure the inline *)
588 :     then (m, fdec::fs, hs)
589 :     else let
590 :     (* if an earlier function h has been eta-reduced
591 :     * to f, we have to be careful to update its
592 :     * binding to not refer to f any more since f
593 :     * will disappear *)
594 :     val nm = foldl (fn (h,m) =>
595 :     if sval2val(lookup m h) = F.VAR f
596 :     then addbind(m, h, svg) else m)
597 :     m hs
598 :     in
599 :     (* I could almost reuse `substitute' but the
600 :     * unuse in substitute assumes the val is escaping *)
601 :     click_eta();
602 :     C.transfer(f, g);
603 :     unusecall m g;
604 :     (addbind(m, f, svg), fs, f::hs)
605 :     end
606 : monnier 189 end
607 : monnier 201 else (m, fdec::fs, hs)
608 :     | fcEta (fdec,(m,fs,hs)) = (m,fdec::fs,hs)
609 :    
610 :     (* add wrapper for various purposes *)
611 :     fun wrap (f as ({inline=F.IH_ALWAYS,...},_,_,_):F.fundec,fs) = f::fs
612 :     | wrap (f as (fk as {isrec,...},g,args,body):F.fundec,fs) =
613 :     let val gi = C.get g
614 :     fun dropargs filter =
615 :     let val (nfk,nfk') = OU.fk_wrap(fk, O.map #1 isrec)
616 :     val args' = filter args
617 :     val ng = cplv g
618 :     val nargs = map (fn (v,t) => (cplv v, t)) args
619 :     val nargs' = map #1 (filter nargs)
620 :     val appargs = (map F.VAR nargs')
621 :     val nf = (nfk, g, nargs, F.APP(F.VAR ng, appargs))
622 :     val nf' = (nfk', ng, args', body)
623 :    
624 :     val ngi = C.new (SOME(map #1 args')) ng
625 :     in
626 :     C.ireset gi;
627 :     app (ignore o (C.new NONE) o #1) nargs;
628 :     C.use (SOME appargs) ngi;
629 :     app (C.use NONE o C.get) nargs';
630 :     nf'::nf::fs
631 : monnier 121 end
632 : monnier 201 in
633 :     (* Don't introduce wrappers for escaping-only functions.
634 :     * This is debatable since although wrappers are useless
635 :     * on escaping-only functions, some of the escaping uses
636 :     * might turn into calls in the course of fcontract, so
637 :     * by not introducing wrappers here, we avoid useless work
638 :     * but we also postpone useful work to later invocations. *)
639 :     if C.dead gi then fs else
640 :     let val used = map (used o #1) args
641 :     in if C.called gi then
642 :     (* if some args are not used, let's drop them *)
643 :     if not (List.all (fn x => x) used) then
644 :     (click_dropargs();
645 :     dropargs (fn xs => OU.filter(used, xs)))
646 : monnier 190
647 : monnier 201 (* eta-split: add a wrapper for escaping uses *)
648 :     else if C.escaping gi then
649 :     (* like dropargs but keeping all args *)
650 :     (click_etasplit(); dropargs (fn x => x))
651 :    
652 :     else f::fs
653 :     else f::fs
654 :     end
655 :     end
656 :    
657 :     (* add various wrappers *)
658 :     val fs = foldl wrap [] fs
659 :    
660 :     (* register the new bindings (uncontracted for now) *)
661 : monnier 202 val (nm,fs) = foldl (fn (fdec as (fk,f,args,body),(m,fs)) =>
662 :     let val nf = (f, body, args, fk, ref [])
663 :     in (addbind(m, f, Fun nf), nf::fs) end)
664 :     (m,[]) fs
665 : monnier 201 (* check for eta redexes *)
666 :     val (nm,fs,_) = foldl fcEta (nm,[],[]) fs
667 :    
668 :     (* move the inlinable functions to the end of the list *)
669 :     val (f1s,f2s) =
670 : monnier 202 List.partition (fn (_,_,_,{inline=F.IH_ALWAYS,...},_) => true
671 : monnier 201 | _ => false) fs
672 :     val fs = f2s @ f1s
673 :    
674 :     (* contract the main body *)
675 :     val nle = loop nm le cont
676 :     (* contract the functions *)
677 :     val fs = fcFun(nm, fs, [])
678 :     (* junk newly unused funs *)
679 :     val fs = List.filter (used o #2) fs
680 :     in
681 :     case fs
682 :     of [] => nle
683 :     | [f1 as ({isrec=NONE,...},_,_,_),f2] =>
684 :     (* gross hack: `wrap' might have added a second
685 :     * non-recursive function. we need to split them into
686 : monnier 202 * 2 FIXes. This is _very_ ad-hoc. We know that the wrapper
687 :     * ("wrap") is inlinable while the main function ("fun") isn't,
688 :     * so `wrap' is in f1s and `fun' is in f2s. Hence `wrap' is after
689 :     * `fun' when passed to fcFun which inverts the order, so
690 :     * f1 is `wrap' and f2 is `fun'. *)
691 : monnier 201 F.FIX([f2], F.FIX([f1], nle))
692 :     | _ => F.FIX(fs, nle)
693 :     end
694 : monnier 163
695 : monnier 201 fun fcApp (f,vs) =
696 : monnier 202 let val svs = map (val2sval m) vs
697 : monnier 201 val svf = val2sval m f
698 :     (* F.APP inlining (if any) *)
699 : monnier 202
700 : monnier 201 in case svf
701 : monnier 202 of Fun(g,body,args,{inline,...},actuals) =>
702 :     let val gi = C.get g
703 :     fun noinline () =
704 :     (actuals := svs :: (!actuals);
705 :     cont(m,F.APP(sval2val svf, map sval2val svs)))
706 :     fun simpleinline () =
707 :     (* simple inlining: we should copy the body and then
708 :     * kill the function, but instead we just move the body
709 :     * and kill only the function name.
710 :     * This inlining strategy looks inoffensive enough,
711 :     * but still requires some care: see comments at the
712 :     * begining of this file and in cfun *)
713 :     (click_simpleinline();
714 :     ignore(C.unuse true gi);
715 :     loop m (F.LET(map #1 args, F.RET vs, body)) cont)
716 :     fun copyinline () =
717 :     (* aggressive inlining. We allow pretty much
718 :     * any inlinling, but we detect and reject inlining
719 :     * recursively which would else lead to infinite loop *)
720 :     (* unrolling is not as straightforward as it seems:
721 :     * if you inline the function you're currently
722 :     * fcontracting, you're asking for trouble: there is a
723 :     * hidden assumption in the counting that the old code
724 :     * will be replaced by the new code (and is hence dead).
725 :     * If the function to be unrolled has the only call to
726 :     * function f, then f might get simpleinlined before
727 :     * unrolling, which means that unrolling will introduce
728 :     * a second occurence of the `only call' but at that point
729 :     * f has already been killed. *)
730 :     let val nle = (F.LET(map #1 args, F.RET vs, body))
731 :     val nle = C.copylexp M.empty nle
732 :     in
733 :     click_copyinline();
734 :     (app (unuseval m) vs);
735 :     unusecall m g;
736 :     fcexp (S.add(g, ifs)) m nle cont
737 :     end
738 :    
739 :     in if C.usenb gi = 1 andalso not(S.member ifs g) then simpleinline()
740 :     else case inline of
741 :     F.IH_SAFE => noinline()
742 :     | F.IH_UNROLL => noinline()
743 :     | F.IH_ALWAYS =>
744 :     if S.member ifs g then noinline() else copyinline()
745 :     | F.IH_MAYBE(min,ws) =>
746 :     if S.member ifs g then noinline() else let
747 :     fun value w _ (Val _ | Con _ | Record _) = w
748 :     | value w v (Fun (f,_,args,_,_)) =
749 :     if C.usenb(C.get v) = 1 then w * 2 else w
750 :     | value w _ _ = 0
751 :     val s = (OU.foldl3 (fn (sv,w,(v,t),s) => value w v sv + s)
752 :     0 (svs,ws,args))
753 :     handle OU.Unbalanced => 0
754 :     in if s > min then copyinline() else noinline()
755 :     end
756 :     end
757 :     | sv => cont(m,F.APP(sval2val svf, map sval2val svs))
758 : monnier 201 end
759 : monnier 184
760 : monnier 201 fun fcTfn ((f,args,body),le) =
761 :     let val fi = C.get f
762 :     in if C.dead fi then (click_deadlexp(); loop m le cont) else
763 :     let val nbody = fcexp ifs m body #2
764 :     val nm = addbind(m, f, TFun(f, nbody, args))
765 : monnier 184 val nle = loop nm le cont
766 : monnier 121 in
767 : monnier 201 if C.dead fi then nle else F.TFN((f, args, nbody), nle)
768 : monnier 121 end
769 : monnier 201 end
770 :    
771 :     fun fcSwitch (v,ac,arms,def) =
772 :     let fun fcsCon (lvc,svc,dc1:F.dcon,tycs1) =
773 :     let fun killle le = C.unuselexp (undertake m) le
774 :     fun kill lv le =
775 :     C.unuselexp (undertake (addbind(m,lv,Var(lv,NONE)))) le
776 :     fun killarm (F.DATAcon(_,_,lv),le) = kill lv le
777 :     | killarm _ = buglexp("bad arm in switch(con)", le)
778 :    
779 :     fun carm ((F.DATAcon(dc2,tycs2,lv),le)::tl) =
780 :     (* sometimes lty1 <> lty2 :-( so this doesn't work:
781 :     * FU.dcon_eq(dc1, dc2) andalso tycs_eq(tycs1,tycs2) *)
782 :     if #2 dc1 = #2 (cdcon dc2) then
783 :     (map killarm tl; (* kill the rest *)
784 :     O.map killle def; (* and the default case *)
785 :     loop (substitute(m, lv, svc, F.VAR lvc))
786 :     le cont)
787 :     else
788 :     (* kill this arm and continue with the rest *)
789 :     (kill lv le; carm tl)
790 :     | carm [] = loop m (O.valOf def) cont
791 :     | carm _ = buglexp("unexpected arm in switch(con,...)", le)
792 :     in click_switch(); carm arms
793 : monnier 186 end
794 : monnier 121
795 : monnier 201 fun fcsVal v =
796 :     let fun kill le = C.unuselexp (undertake m) le
797 :     fun carm ((con,le)::tl) =
798 :     if eqConV(con, v) then
799 :     (map (kill o #2) tl;
800 :     O.map kill def;
801 :     loop m le cont)
802 :     else (kill le; carm tl)
803 :     | carm [] = loop m (O.valOf def) cont
804 :     in click_switch(); carm arms
805 :     end
806 :    
807 :     fun fcsDefault (sv,lvc) =
808 :     case (arms,def)
809 :     of ([(F.DATAcon(dc,tycs,lv),le)],NONE) =>
810 :     (* this is a mere DECON, so we can push the let binding
811 :     * (hidden in cont) inside and maybe even drop the DECON *)
812 :     let val ndc = cdcon dc
813 :     val slv = Decon(lv, sv, ndc, tycs)
814 :     val nm = addbind(m, lv, slv)
815 :     (* see below *)
816 :     (* val nm = addbind(nm, lvc, Con(lvc, slv, ndc, tycs)) *)
817 :     val nle = loop nm le cont
818 :     val nv = sval2val sv
819 :     in
820 :     if used lv then
821 :     F.SWITCH(nv,ac,[(F.DATAcon(ndc,tycs,lv),nle)],NONE)
822 :     else (unuseval m nv; nle)
823 :     end
824 :     | (([(_,le)],NONE) | ([],SOME le)) =>
825 :     (* This should never happen, but we can optimize it away *)
826 :     (unuseval m (sval2val sv); loop m le cont)
827 :     | _ =>
828 :     let fun carm (F.DATAcon(dc,tycs,lv),le) =
829 :     let val ndc = cdcon dc
830 :     val slv = Decon(lv, sv, ndc, tycs)
831 :     val nm = addbind(m, lv, slv)
832 :     (* we can rebind lv to a more precise value
833 :     * !!BEWARE!! This rebinding is misleading:
834 :     * - it gives the impression that `lvc' is built
835 :     * from`lv' although the reverse is true:
836 :     * if `lvc' is undertaken, `lv's count should
837 :     * *not* be updated!
838 :     * Luckily, `lvc' will not become dead while
839 :     * rebound to Con(lv) because it's used by the
840 :     * SWITCH. All in all, it works fine, but it's
841 :     * not as straightforward as it seems.
842 :     * - it seems to be a good idea, but it can hide
843 :     * other opt-opportunities since it hides the
844 :     * previous binding. *)
845 :     (* val nm = addbind(nm, lvc, Con(lvc,slv,ndc,tycs)) *)
846 :     in (F.DATAcon(ndc, tycs, lv), loop nm le #2)
847 :     end
848 :     | carm (con,le) = (con, loop m le #2)
849 :     val narms = map carm arms
850 :     val ndef = Option.map (fn le => loop m le #2) def
851 :     in cont(m, F.SWITCH(sval2val sv, ac, narms, ndef))
852 :     end
853 : monnier 121
854 : monnier 201 in case val2sval m v
855 :     of sv as Con x => fcsCon x
856 :     | sv as Val v => fcsVal v
857 :     | sv as (Var{1=lvc,...} | Select{1=lvc,...} | Decon{1=lvc, ...}
858 :     | (* will probably never happen *) Record{1=lvc,...}) =>
859 :     fcsDefault(sv, lvc)
860 :     | sv as (Fun _ | TFun _) =>
861 :     bugval("unexpected switch arg", sval2val sv)
862 :     end
863 : monnier 159
864 : monnier 201 fun fcCon (dc1,tycs1,v,lv,le) =
865 :     let val lvi = C.get lv
866 :     in if C.dead lvi then (click_deadval(); loop m le cont) else
867 :     let val ndc = cdcon dc1
868 :     fun ccon sv =
869 :     let val nm = addbind(m, lv, Con(lv, sv, ndc, tycs1))
870 :     val nle = loop nm le cont
871 :     in if C.dead lvi then nle
872 :     else F.CON(ndc, tycs1, sval2val sv, lv, nle)
873 :     end
874 :     in case val2sval m v
875 :     of sv as (Decon (lvd,sv',dc2,tycs2)) =>
876 :     if FU.dcon_eq(dc1, dc2) andalso tycs_eq(tycs1,tycs2) then
877 :     (click_con();
878 :     loop (substitute(m, lv, sv', F.VAR lvd)) le cont)
879 :     else ccon sv
880 :     | sv => ccon sv
881 : monnier 189 end
882 : monnier 201 end
883 : monnier 121
884 : monnier 201 fun fcRecord (rk,vs,lv,le) =
885 :     (* g: check whether the record already exists *)
886 :     let val lvi = C.get lv
887 :     in if C.dead lvi then (click_deadval(); loop m le cont) else
888 :     let fun g (Select(_,sv,0)::ss) =
889 :     let fun g' (n,Select(_,sv',i)::ss) =
890 :     if n = i andalso (sval2val sv) = (sval2val sv')
891 :     then g'(n+1,ss) else NONE
892 :     | g' (n,[]) =
893 :     (case sval2lty sv
894 :     of SOME lty =>
895 :     let val ltd = case rk
896 :     of F.RK_STRUCT => LT.ltd_str
897 :     | F.RK_TUPLE _ => LT.ltd_tuple
898 :     | _ => buglexp("bogus rk",le)
899 :     in if length(ltd lty) = n
900 :     then SOME sv else NONE
901 :     end
902 :     | _ => (click_lacktype(); NONE)) (* sad *)
903 :     | g' _ = NONE
904 :     in g'(1,ss)
905 :     end
906 :     | g _ = NONE
907 :     val svs = map (val2sval m) vs
908 :     in case g svs
909 :     of SOME sv => (click_record();
910 :     loop (substitute(m, lv, sv, F.INT 0)) le cont
911 :     before app (unuseval m) vs)
912 :     | _ =>
913 :     let val nm = addbind(m, lv, Record(lv, svs))
914 :     val nle = loop nm le cont
915 :     in if C.dead lvi then nle
916 :     else F.RECORD(rk, map sval2val svs, lv, nle)
917 :     end
918 : monnier 189 end
919 : monnier 201 end
920 : monnier 121
921 : monnier 201 fun fcSelect (v,i,lv,le) =
922 :     let val lvi = C.get lv
923 :     in if C.dead lvi then (click_deadval(); loop m le cont) else
924 :     (case val2sval m v
925 :     of Record (lvr,svs) =>
926 :     let val sv = List.nth(svs, i)
927 :     in click_select();
928 :     loop (substitute(m, lv, sv, F.VAR lvr)) le cont
929 :     end
930 :     | sv =>
931 :     let val nm = addbind (m, lv, Select(lv, sv, i))
932 :     val nle = loop nm le cont
933 :     in if C.dead lvi then nle
934 :     else F.SELECT(sval2val sv, i, lv, nle)
935 :     end)
936 :     end
937 : monnier 121
938 : monnier 201 fun fcBranch (po,vs,le1,le2) =
939 :     let val nvs = map substval vs
940 :     val npo = cpo po
941 :     val nle1 = loop m le1 #2
942 :     val nle2 = loop m le2 #2
943 :     in cont(m, F.BRANCH(npo, nvs, nle1, nle2))
944 :     end
945 : monnier 121
946 : monnier 201 fun fcPrimop (po,vs,lv,le) =
947 :     let val lvi = C.get lv
948 : monnier 202 val pure = PO.purePrimop (#2 po)
949 : monnier 201 in if pure andalso C.dead lvi then (click_deadval();loop m le cont) else
950 : monnier 199 let val nvs = map substval vs
951 : monnier 121 val npo = cpo po
952 : monnier 201 val nm = addbind(m, lv, Var(lv,NONE))
953 :     val nle = loop nm le cont
954 :     in
955 :     if pure andalso C.dead lvi then nle
956 :     else F.PRIMOP(npo, nvs, lv, nle)
957 : monnier 121 end
958 : monnier 201 end
959 : monnier 121
960 : monnier 201 in case le
961 :     of F.RET vs => cont(m, F.RET(map substval vs))
962 :     | F.LET x => fcLet x
963 :     | F.FIX x => fcFix x
964 :     | F.APP x => fcApp x
965 :     | F.TFN x => fcTfn x
966 :     | F.TAPP (f,tycs) => cont(m, F.TAPP(substval f, tycs))
967 :     | F.SWITCH x => fcSwitch x
968 :     | F.CON x => fcCon x
969 :     | F.RECORD x => fcRecord x
970 :     | F.SELECT x => fcSelect x
971 :     | F.RAISE (v,ltys) => cont(m, F.RAISE(substval v, ltys))
972 :     | F.HANDLE (le,v) => cont(m, F.HANDLE(loop m le #2, substval v))
973 :     | F.BRANCH x => fcBranch x
974 :     | F.PRIMOP x => fcPrimop x
975 : monnier 121 end
976 :    
977 : monnier 185 in
978 :     (* C.collect fdec; *)
979 : monnier 201 case fcexp S.empty
980 : monnier 185 M.empty (F.FIX([fdec], F.RET[F.VAR f])) #2
981 :     of F.FIX([fdec], F.RET[F.VAR f]) => fdec
982 :     | fdec => bug "invalid return fundec"
983 :     end
984 : monnier 121
985 :     end
986 :     end

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