Changes between Version 2 and Version 3 of Internals/StackFrames

01/31/08 08:44:30 (9 years ago)

notes on tail calling a function taking stack args


  • Internals/StackFrames

    v2 v3  
    6767Returning a single value is a matter of leave/ret, regardless of how the frame was constructed. 
     69== Tail calling == 
     70When tail calling a function that gets some args on the stack, 
     71we copy the stack args to the location just below the current function's saved frame pointer, 
     72push the current funtion's return address, and set the frame pointer to 
     73the saved frame pointer.  We then jump to the new function (which can 
     74then build a stack frame as described above). 
     76Stack layout in caller, before sliding args: 
     78|| || return address || 
     79|| 0(%ebp) || saved ebp || 
     80|| || local junk on the stack || 
     81|| || reserved-frame-marker || 
     82|| || reserved-frame-marker || 
     83|| || arg1 || 
     84|| || arg2 || 
     85|| 0(%esp) || arg3|| 
     87After sliding: 
     88|| || return address || 
     89|| || saved ebp || 
     90|| || arg1 || 
     91|| || arg2 || 
     92|| || arg3|| 
     93|| 0(%esp) || return address || 
     95At this point, %ebp contains the saved %ebp. 
     97The return address and saved ebp above the stack args are 
     98in the place where the reserved-frame-marker would be 
     99expected if we weren't recycling the stack frame for a tail call.  These locations will 
     100be overwritten when the called function builds a stack 
     101frame (via the save-lisp-context-variable-args vinsn). 
     103When there aren't any args being passed on the stack (that is, 
     104all the args fit in registers), we don't have to do any copying; 
     105we just unlink the frame pointer with LEAVE, and jump to the new function.