Sujet : Re: Stack vs stackless operation
De : anton (at) *nospam* mips.complang.tuwien.ac.at (Anton Ertl)
Groupes : comp.lang.forthDate : 24. Feb 2025, 22:50:21
Autres entêtes
Organisation : Institut fuer Computersprachen, Technische Universitaet Wien
Message-ID : <2025Feb24.225021@mips.complang.tuwien.ac.at>
References : 1
User-Agent : xrn 10.11
zbigniew2011@gmail.com (LIT) writes:
I wonder: wouldn't it be useful to have stackless basic
arith operations? I mean instead of fetching the values
first and putting them on the stack, then doing something,
and in the end storing the result somewhere wouldn't it
be practical to use directly the variables?
I don't remember ever doing that, so no, it would not be practical.
Forth has had values for quite a while, so you could avoid the need to
write @ and !; you would instead write something like:
a b + to c
For global values the code is typically not better than when using
variables, though.
But after I came up with this idea I realized someone
surely invented that before - it looks so obvious — yet
I didn't see it anywhere.
The VAX architecture has instructions with three memory operands,
including ADD3. That feature makes it pretty hard to implement
efficiently.
Did anyone of you see something
like this in any code?
No.
If so — actually why somehow
(probably?) such solution has not become widespread?
Probably because the case where the two operands of a + are in memory,
and the result is needed in memory is not that frequent.
Looks good to me; math can be done completely in ML
avoiding "Forth machine" engagement, therefore saving many
cycles.
Not sure what you mean with "Forth machine engagement"; with good
Forth compilers these days, a typical stack-to-stack addition is
faster than the best machine code for a memory-to-memory addition.
E.g. VFX64 turns
: dec-u#b ( u1 -- u2 )
dup #-3689348814741910323 um* nip 3 rshift tuck 10 * - '0' + hold ; ok
into
( 0050A300 48BACDCCCCCCCCCCCCCC ) MOV RDX, # CCCCCCCC:CCCCCCCD
( 0050A30A 488BC2 ) MOV RAX, RDX
( 0050A30D 48F7E3 ) MUL RBX
( 0050A310 48C1EA03 ) SHR RDX, # 03
( 0050A314 486BCA0A ) IMUL RCX, RDX, # 0A
( 0050A318 482BD9 ) SUB RBX, RCX
( 0050A31B 4883C330 ) ADD RBX, # 30
( 0050A31F 488D6DF8 ) LEA RBP, [RBP+-08]
( 0050A323 48895500 ) MOV [RBP], RDX
( 0050A327 E87CA7F1FF ) CALL 00424AA8 HOLD
( 0050A32C C3 ) RET/NEXT
( 45 bytes, 11 instructions )
I don't think that it would be faster or shorter to use
memory-to-memory operations here. That's also why the VAX died: RISCs
just outperformed it.
- anton
-- M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.htmlcomp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html New standard: https://forth-standard.org/EuroForth 2023 proceedings: http://www.euroforth.org/ef23/papers/EuroForth 2024 proceedings:
http://www.euroforth.org/ef24/papers/