What are the reasons VFX Forth has given up on tail call optimization?
On 25 Jul 2022 at 11:30:07 CEST, "Michał Kasprzak" <siarc...@gmail.com> wrote:
What are the reasons VFX Forth has given up on tail call optimization?We have not given up on it, it's just that once you have a code generator of a
certain
quality, tail call elimination does not buy you much. Many people wo want to write a
code generator initially get excited about binary inlining and tail call elimination. Later
they realise that there's little overall gain.
Even later, one has to make decisions about support for common Forth practice
which is forbidden by the standard. A particular example is using
DROPto exit from an outer word. It's a design choice for the Forth system. VFX is
particularly
conservative about return stack handling; at the time of its design, return stack
manipulation to produce branches was quite common. It is/was also widely used
for inline literals and strings.
It is probably true that return stack action can be be fully and correctly detected, but
I have not yet persuaded myself that it can be done 100%, and do I want to introduce
a significant block of code that produces little benefit?
A large chunk of our business is in embedded systems. In plenty of these CPUs,
e.g. Cortex-M, the item on the call stack is not a pure return address. In some devices
the call return item is more than one cell on the stack.
Stephen
--
Stephen Pelc, ste...@vfxforth.com
MicroProcessor Engineering, Ltd. - More Real, Less Time
133 Hill Lane, Southampton SO15 5AF, England
tel: +44 (0)23 8063 1441, +44 (0)78 0390 3612, +34 649 662 974 http://www.mpeforth.com - free VFX Forth downloads
Thanks, Stephen, for the open explanations. I appreciate them and you for the work you have given to the community and for your carefulness and conservatism
when working with delicate words. I smiled at the thought that beginners always start with inline and tail optimizations ;) because of course you are right.
Hi Brian, are you the famous Brian Fox who made the GNU Bash shell?
Your word -; might look like this considering that 64-bit VFX Forth uses the codes $C3 for ret, $E8 for call, and $E9 for jmp and that the latter two
instructions have a 32-bit signed displacement to add to the address of the next instruction to get the target address.
: -; [compile] ; here 1- c@ $C3 = here 6 - c@ $E8 = and if $E9 here 6 - c! else ." Can't -;" then ; immediate
You use -; replacing ; wherever you try to force tail optimization at your own request.
-; will complete the definition exactly as ; does and it will check if it can optimize tail call which might not be possible for example when using locals
(even then, despite the warning message, the word is correctly defined). >Alas, our tests in the content of -; can be successful in undesirable cases. For example there might be THEN before ; as in the definition of our -; word.
Luckily with this side effect everything works fine in the end, but assume that we are using the word -; at our own request and that we know what we are
doing.
Disassembling the last example also suggests that trying to save one ret byte in the dictionary by using -1 ALLOT is not a good idea.
Thanks, Stephen, for the open explanations. I appreciate them and you for the work you have given to the community and for your carefulness and conservatism when working with delicate words. I smiled at the thought that beginners always start withinline and tail optimizations ;) because of course you are right.
Hi Brian, are you the famous Brian Fox who made the GNU Bash shell?the target address.
Your word -; might look like this considering that 64-bit VFX Forth uses the codes $C3 for ret, $E8 for call, and $E9 for jmp and that the latter two instructions have a 32-bit signed displacement to add to the address of the next instruction to get
: -; [compile] ; here 1- c@ $C3 = here 6 - c@ $E8 = and if $E9 here 6 - c! else ." Can't -;" then ; immediateword -; at our own request and that we know what we are doing.
You use -; replacing ; wherever you try to force tail optimization at your own request.
-; will complete the definition exactly as ; does and it will check if it can optimize tail call which might not be possible for example when using locals (even then, despite the warning message, the word is correctly defined).
Alas, our tests in the content of -; can be successful in undesirable cases. For example there might be THEN before ; as in the definition of our -; word. Luckily with this side effect everything works fine in the end, but assume that we are using the
Disassembling the last example also suggests that trying to save one ret byte in the dictionary by using -1 ALLOT is not a good idea.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 505 |
Nodes: | 16 (2 / 14) |
Uptime: | 85:07:34 |
Calls: | 9,934 |
Files: | 13,813 |
Messages: | 6,348,180 |