Cross-function tail calls?

Kaz Kylheku <[email protected]> Sat, 19 Jul 2025 14:31:48 -0700
Newsgroups gmane.lisp.clisp.general
Message-ID <[email protected]>
Hi all,

I seem to remember that CLISP only has tail call optimization for self-calls.

Recently I did work on TCO for TXR Lisp. In a first milestone, I made self TCO work,
where a backwards JMP instruction is at the core.

In the current release, I also made cross-call TCO work. I wonder whether a similar
technique could work for CLISP.

I came up with a trick: I added a prefix instruction called TAIL to the VM. The prefix
instruction is inserted before one of several types of fuction calling instructions
that were identified by the compiler as being in a tail position.

The VM can then perform a modified function call (if the target function is a VM
function). whereby it allocates the callee's frame in the same space as the
caller.

If the caller needs more space, I alloca() the difference. Some trickery is used to
propagate the arguments. They have to be prepared while the old stack frame is
still valid, and then be available to the new frame after the old one is overwritten.

It's possible to ignore the TAIL instruction as a no-op and everything will work,
minus the tail semantics.



_______________________________________________
clisp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/clisp-list