On Sat, Jun 08, 2002 at 03:35:39PM -0400, Melvin Smith wrote:
> At 08:30 PM 6/8/2002 +0200, Jerome Vouillon wrote:
> >Instead of using some space on the stack, co-routines can store all
> >their local variables into their closure.  Then, there is no need to
> >swap in any context.
> 
> We have to store the closure's variables somewhere, if not on a stack, 
> where?

There are some pretty good slides on compiling functions at:
  http://pauillac.inria.fr/~xleroy/talks/compilation-agay.pdf

>From a semantics point of view, a closure is a pair of a code pointer
and an environment.  The environment maps the variables defined outside
the function (lexical scope) to their value.

A closure is usually implemented as a flat record: the first member of
the record is the code pointer, the remaining members contain the
values of the external variables the function may need.

When the body of a function is executed, some local variables may be
needed.  They are usually stored in registers and on the stack.

The code pointer and environment remain unchanged from one function
invocation to another.  This is not the case with co-routines, where
the code pointer is updated at each invocation and local variables
need to preserved across co-routine invocation.  So, my idea was to
put the local variables in some additional entries of the closure
instead of the stack.

Of course, this does not work with real coroutines (i.e.
cooperative threads).  But you can implement Python-style
coroutines (http://www.python.org/peps/pep-0255.html) this
way.

-- Jerome

Reply via email to