Sujet : Re: Stack vs stackless operation
De : anton (at) *nospam* mips.complang.tuwien.ac.at (Anton Ertl)
Groupes : comp.lang.forthDate : 01. Mar 2025, 18:22:45
Autres entêtes
Organisation : Institut fuer Computersprachen, Technische Universitaet Wien
Message-ID : <2025Mar1.182245@mips.complang.tuwien.ac.at>
References : 1 2 3 4 5 6 7 8 9 10
User-Agent : xrn 10.11
Hans Bezemer <
the.beez.speaks@gmail.com> writes:
On 01-03-2025 12:47, Anton Ertl wrote:
11 !@
3 atomic!@
66 +!
>
We've done without it all this time.
>
Sure, you can replace it with DUP @ >R ! R>. Having a word for that
relieves the programmer of producing such a sequence (possibly with a
bug) and the reader of having to analyse what's going on here.
>
I found the sequence exactly twice in my code
Yes, you can replace !@ with that sequence, but not every case where
one fetches one value from and address and stores another value to
that address is expressed by this sequence. E.g., another equivalent
sequence is: DUP >R @ SWAP R> !; and another: DUP @ -ROT !. And you
can also use that word profitably in cases where some other
functionality is mixed in with the code without !@. E.g., in none of
the variants of :=:/etc. without !@ in this thread one of the two
sequences occured; in several of them the ! of the other address was
inserted before the ! of the address that was fetched the second time.
E.g.,
: exchange2 ( addr1 addr2 -- )
dup >r @ over @ r> ! swap ! ;
Yet
: exchange ( addr1 addr2 -- )
over @ swap !@ swap ! ;
is shorter, easier to follow, and (in gforth-fast) faster.
As mentioned, Bernd Paysan used !@ 11 times in the Gforth image in
code where atomicity is not needed. Up to yesterday we only had the
atomic version and I have avoided using !@ because I was worried that
it would be slow, so there may be some additional opportunity in the
Gforth image for using it.
However, if it is that rare there is no point in adding it. Creating too
many superfluous abstractions may even get counter productive in the
sense that predefined abstractions are ignored and reinvented.
In that case they are obviously not superfluous. Yes, reinvention
happens; it shows that the word is needed. Then at some point
somebody notices the duplication, decides on a canonical version and
goes through the code and replaces all uses of the duplicated words
with the canonical version.
There is a valid reason to avoid rarely used words that can be
replaced by a sequence: human memory load. I don't think that !@ is
such a case, though.
- 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/