making the stack explicit
Stephen Weeks
sweeks@intertrust.com
Thu, 2 Dec 1999 10:27:30 -0800 (PST)
Dan, maybe you already figured this out, but I just understood this
morning how to make the stack explicit and express the result without
changing Cps at all. More precisely, one can write a fairly simple
pass that takes a Cps program and turns it into an equivalent Cps
program with all tail calls (i.e. cont = NONE in all Call Transfers).
The idea is to:
1. Call Live.live (backend/live.fun) to obtain the live variables of
every Jump.
2. Build a new datatype called stack with one variant for every
Jump that appears as SOME j in the cont of a Call Transfer. The
variant constructor will have as argument types the types of the
live variables for j and stack.
3. Make each local jump j a toplevel function, taking as an extra
argument the live variables of j and a stack.
Once you've done this, you can even run another simplification pass,
cps/contify.fun, which takes toplevel functions and turns them into
local functions if possible. This optimization will be particularly
helpful after running the above algorithm because lots of jumps really
are for expressing local control flow and have nothing to do with
creating new continuations.
In fact, now that I look at it, contify is slightly too weak of an
optimization for what is needed. Right now, contify will only turn a
toplevel function f into a local function if there is only one call site
to f from outside of f. In fact, contify could use a slightly weaker
condition that there is only one other function g that calls f, where
all calls to f are in tail position. This would allow f to safely be
moved inside g as a local function.
Anyways, I may be rambling a bit, but my main point is that maybe you
can get away without creating a new IL at all.