Scope rules for CPS and other questions...

Daniel Wang danwang@cs.princeton.edu
01 Dec 1999 22:21:33 -0500


"Stephen Weeks" <sweeks@intertrust.com> writes:


> I don't think either of these is the right interpretation.  The right
> interpretation is to think of Cps as a handy notation for SSA, where
> local functions just define labels on basic blocks that one can jump

Ahhh.. okay got it.. SSA is of course functional programming. :) So it's got
the semantics I expected.....

> The magic is in backend/chunkify.fun, which takes the Cps program and
> partitions all of the Funcs and Jumps into disjoint sets, each of
> which corresponds to a C procedure.  There is an internal control (not 
> exposed as a command line switch) called Control.chunk which specifies 
> how aggressive the chukifier should be.

Ahh okay, this should be fine..

> > I just want to
> > know if I'll be able to compile big programs if I CPS convert everything in
> > a resonable time.
> 
> I don't understand what it means to CPS convert the Cps language.  Can 
> you tell me how the result wouldn't be isomorphic to Cps?

Ahhh, I've been missreading the IR okay, so it is CPS. It's just that the
return continuation isn't an explicit parameter but something that's
implicitly maintained. 

I'll need an explicit encoding of the closure for the return continuation
for the work I'm doing, so the garbage collector can get access to variables
that are on the current stack frame.

So a source level program such as the one below is 

let
 fun f(x:int) = x
 val x = f(1)
 val y = f(2)
in (x,y)
end

represented in the current CPS with something like the following 

let
 val ret : (int -> Ans) ref = ref initial 
 fun f(x:int) = (!ret) x 
 fun k1(x) = let 
   fun k2(y) = (x,y)
   in ret := k2;
      call f(2) 
   end
 in ret := k1; 
    call f(1)
 end

I need it translated into a version with a explicit continuation parameter
and explicit continuation values.

let
 datatype cont = K1 | K2 of int
 fun f(k:cont,x:int) = apply (k,x)
 and apply (K1,x) = f(K2 x,2)
     apply (K2 x,y) = halt(x,y)
in f(K1,1)
end