Closed jkotlinski closed 2 years ago
My understanding is this:
Tail call elimination is a compile optimization that replaces the final jsr/rts
of a word with a jmp
to save code size and improve performance.
Some words are not eligible for tail call elimination when they occur as the last word in a word's definition.
From what I can tell, the following are the only words without tail call elimination:
]
, :
, unloop
, >r
, r>
, r@
The rationale for some of these is that they do not end with rts
or jsr
, hence there is no opportunity to jump to them knowing that they will return properly.
Some of them I don't know the reason.
>r
, r>
, r@
and unloop
directly manipulate the return stack. They assume that the top of return stack contains a return adress. As such, they need to be called using jsr
, as a jmp
will not push a return address to the return stack.
immediate
words are not subject to tail-call elimination. The reason is that immediate words have custom compile-time behavior. As such, it cannot be assumed that tail-call elimination applies.
]
is not subject to tail-call elimination, since it does not compile a jsr
. Consider the following word definition:
: nop [ ] ;
Replacing the last jsr
with jmp
would fail, since no jsr
has been compiled.
:
is not subject to tail-call elimination, since it does not compile a jsr
. Consider the following word definition:
: nop ;
Replacing the last jsr
with a jmp
would fail, since no jsr
has been compiled.
I think those are all the facts, that need to be served...
Great! I'll put that in TeX format.
Maybe the particular explanations why certain words are exempt from TCE belong more in source code than manual. I will do that, it should not be much work.
...it might be fine to mention some of these words in manual, but everything seems like a bit much maybe.
Hmm. The manual has a few more pressing shortcomings that make any further elaboration on tail call elimination a bit unnecessary.
Thanks for the source docs! They were enlightening, for sure.
If further documentation is a bit unnecessary, perhaps this issue can be now closed?
What is it and when is it guaranteed to happen?