The future of lambda delegates

Mikola Lysenko mclysenk at mtu.edu
Thu Aug 17 09:47:20 PDT 2006


"BCS" <BCS at pathlink.com> wrote in message 
news:ec2581$74o$2 at digitaldaemon.com...
> 1) What happens when a heap based function calls a stack based function? 
> Will the code try to extend the heap frame? How would this be prevented? 
> Use another register for the heap-frame-pointer? (stack-frame, heap-frame, 
> stack, how many register are left?) Have a stub stack-frame to put it in? 
> (makes fn calls more costly.)
>

This would not present any sort of problem. Here is what a stack based 
prolog looks like:

push EBP;                    //Save old frame pointer on the stack
mov EBP, ESP;            //Set the new frame pointer & save stack pointer
sub ESP, frame_size;    //Allocate the new frame from the stack

And when we exit, we use the following epilog:

mov ESP, EBP;            //Restore old stack pointer
pop EBP;                     //Restore old frame pointer
ret;                               //Resume execution from the previous 
frame

In this case, it does not matter what EBP was before we entered the 
function.  It could even be an invalid memory location for all we care.  So, 
calling a stack based function from a heap based function does not create 
any sort of problem.



> 2) heap frames only tackles the case where the "lost" scope is the 
> function it's self. What about where the scope is a if block?
>
> void fn()
> {
> int i;
> auto dg = {return i;};
>
> if(i);
> {
> int j;
> dg = {return i+j;};
> }
>
> if(i)
> {
> int k = 3;
> k = dg(); // j overlaps k
> }
> }

Typically all the variables used in a given function would get allocated in 
the same frame.  In this case, the frame would look something like:

EBP[4] = return address
EBP[0] = previous frame pointer
EBP[-4] = i
EBP[-8] = j
EBP[-12] = k

If this is the situation, then i, j and k are all allocated at once when the 
function first gets called.  They are only initialized once they enter 
scope.

When considering the semantics of heap allocated frames, I would try to 
think of it like the class example given in the original post.  In that 
case, all of the variables and arguments are copied into a container class, 
which also has methods for every nested function.

Hopefully this makes things clearer. 





More information about the Digitalmars-d mailing list