Misc ideas & comments

Kragen kragen@pobox.com
Mon, 30 Mar 1998 15:04:46 -0500 (EST)


On 30 Mar 1998, Harvey J. Stein wrote:
> tc@gauss.muc.de (Matthias Hoelzl tc) writes:
> 
> > 1) As far as I can see there is no simple way to generate tail
> > calls in the front end.  Gcc does some tail call elimination in the
> > back end, but not enough to be satisfying for a Scheme compiler (as far
> > as I can figure out it only optimizes self-tail-calls).
> 
> It's not too hard if one's willing to use a different calling
> paradigm.  I think that in C the caller sets up the stack, calls the
> routine, & cleans up the stack.  If instead the called rtn were to
> clean up then it'd be easy to generate full tail-recursive code.  

This would result in much smaller and easier-to-read output code, which
would probably be faster as well.

It has two potential problems:

1) variadic functions will be a little more dangerous -- they'll have to
call va_arg exactly the right number of times.  To illustrate:

printf("%s"); normally causes a crash.
printf("%s", "Cholo\n"); normally works OK, and would continue to.
printf("%s", "Cholo\n", 1); normally works OK, but would cause a crash
if the called printf were to clean up the stack.

2) you won't be able to call any standard library functions without
recompiling them.  MSVC++ has this situation, and has about half a
dozen extra, nonstandard declaration specifiers you can use to indicate
that the declared function has different calling conventions: __far,
__pascal, __fortran, __syscall, __fastcall, and __cdecl are some of
them.  In fact, it even has one for callee-stack-cleanup: __stdcall.

MSVC++ __stdcall doesn't allow variadic functions.

The use of these declspecs could be limited to .h files specific to
your hacked-up compiler.

> You might also want to add a size into the stack to make it a little safer.

That is an *excellent* idea.  It would solve (1) and probably further
reduce the size of your emitted code, too.

Kragen