[comp.lang.scheme] preserving proper tail-recursion while compiling to C

acha@CS.CMU.EDU (Anurag Acharya) (02/25/91)

In article <ACHA.91Feb25003534@DRAVIDO.SOAR.CS.CMU.EDU> acha@CS.CMU.EDU (Anurag Acharya) writes:

>There are two compilation strategies that would preserve proper tail-recursion

I forgot to mention that both these strategies generate portable C code. If
portability is not a big concern and the C compiler being used is able to 
handle the "asm" keyword, then it is possible to modify the C calling 
convention to handle tail-recursion. 

0. convert the source language program into CPS
1. compile individual source language functions to distinct C functions.
2. The last statement in these functions is an asm macro (or a call to an 
   assembly-coded procedure) that takes the target function and its arguments
   and modifies the stack appropriately. after the dust clears the target
   function is in place and the stack looks as if we were still using the 
   C calling convention.

This approach (call it the third approach) is machine and compiler dependent. 
It is likely to be a bit faster than the second approach since it manipulates 
the stack in place and doesn't need to go thru a return & a call to implement a
source level function call. On the other hand, it is likely to be slower than 
the second approach since the second approach is able to place its parameters 
in registers most of the time, whereas it has to use the stack all the time.

anurag