On 6/12/05, Piers Cawley <[EMAIL PROTECTED]> wrote:
> Chip Salzenberg <[EMAIL PROTECTED]> writes:
> 
> > On Sun, Jun 12, 2005 at 03:15:22PM +0100, Piers Cawley wrote:
> >> But if you fallow the calling conventions that looks like:
> >>
> >>    sub foo {
> >>      $a = 1.
> >>      $c = 10;
> >>      print $c
> >>      
> >> save_dollar_a_and_only_dollar_a_because_im_going_to_use_it_after_this_function_call
> >>      foo()
> >>     _implicit_label_for_return_continuation:
> >>      restore_dollar_a
> >>     _ooh_i_dont_have_to_save_anything
> >>      $b = bar()
> >>     _nor_do_i_have_to_restore_anything
> >>     print $b
> >>    }
> >
> > You have greatly misunderstood.  We're talking about how &foo manages
> > its callee-saves registers.  The registers involved, the ones that I'm
> > calling $a and $b, are P16-P31.
> >
> >> Of course, if you're going to actually use GOTO to get to some label
> >> that you should only get to via a continuation ...
> >
> > For purposes of allocating the callee-saves registers, a continuation
> > may as well _be_ a goto.
> 
> No it's not. A continuation should carry all the information required to
> restore the registers to the correct state when it is taken. A goto
> doesn't. For the purposes of allocating the registers in foo you can allocate
> $a to P16, and $b to p16, because when the call to bar takes the continuation
> back to bar, the 'restore' phase should grab $a from the continuation and bung
> it back on P16. The continuation doesn't even need to know where to restore $a
> to, because the 'caller restores' code should take care of that.
> 
> > Don't feel bad, though.  I thought the same thing the first time *I*
> > heard about this problem.
> 
> I think you should have held that thought.
> 

I also do not see the wisdom of reducing continuations to just a GOTO.

Continuations should have all the abilities of a function, with the
additional abilities of returning its state, in the form of its
activation record, and being reinvoked with said activation record. 
The activation record stores all info about the state of the function
(saved registers, calling function, PC, etc).  Maybe think of it as a
snapshot of the function's state.

So, in the example, bar() reinvokes the continuation, but this all
takes place in a new call to foo(), using the returned activation
record.  The registers and PC return to the continuation's previous
state.  So, just like a recursive function, you are running the same
function in a different context, and the registers do not overlap.

Callee-save registers must be saved after the function gains control,
and restored before it passes off control.  So anywhere a function
returns a continuation, it must restore the callee-save registers
before and save them after.

It might also be helpful to take a look at other systems that also
implement continuations:
 -Stackless Python (http://www.stackless.com/spcpaper.htm)
 -Standard ML (http://www.smlnj.org/doc/features.html)
 -Formalizing Implementation Strategies
(http://citeseer.ist.psu.edu/danvy00formalizing.html)
 -Others (http://c2.com/cgi/wiki?ContinuationImplementation)

-Curtis Rawls

Reply via email to