Tail recursion optimization

Sam Kendall sam at delftcc.UUCP
Thu Apr 17 08:14:19 AEST 1986


I haven't seen it mentioned yet that if you optimize away a stack frame
which is referred to by a jmp_buf (the setjmp data type), a longjmp
to that stack frame will fail.  Therefore a compiler that wants to
optimize tail recursion must know about setjmp in order to suppress
tail recursion optimization in functions that call setjmp.  This is
compatible with the ANSI draft standard.

A couple of points to answer (square brackets mine):

In article <2122 at watmath.UUCP>, atbowler at watmath.UUCP writes:
> There is a problem if you want to support a "nargs" function with [tail
> recursion].
> If an implementation tries stores the nargs value in some fixed position
> in the call sequence then it cannot optimize tail recursion into
> a branch to this or some other function.

If you really want nargs, just optimize tail recursion into a change to
the nargs value, followed by a branch.  What's the problem?

> [I]t is annoying to have to do things like
> "node1(a), node2(a, b), node3(a,b,c)".

You can use varargs here.  You also need an argument list terminator,
so do something like

	#define NIL	((NODE *) NULL)
	/* assuming arguments to node are of type NODE * */
	node(a, NIL)
	node(a, b, c, NIL)

----
Sam Kendall			{ ihnp4 | seismo!cmcl2 }!delftcc!sam
Delft Consulting Corp.		ARPA: delftcc!sam at NYU.ARPA



More information about the Comp.lang.c mailing list