Sujet : Re: Code generation for DOES> in Gforth
De : anton (at) *nospam* mips.complang.tuwien.ac.at (Anton Ertl)
Groupes : comp.lang.forthDate : 03. Oct 2024, 11:59:26
Autres entêtes
Organisation : Institut fuer Computersprachen, Technische Universitaet Wien
Message-ID : <2024Oct3.125926@mips.complang.tuwien.ac.at>
References : 1
User-Agent : xrn 10.11
anton@mips.complang.tuwien.ac.at (Anton Ertl) writes:
I recently noticed that Gforth still used the following COMPILE,
implementation for words defined with CREATE...SET-DOES> (and
consequently also for words defined with CREATE...DOES>):
>
: does, ( xt -- ) does-check ['] does-xt peephole-compile, , ;
>
Ignore DOES-CHECK (it has to do with stack-depth checking, still
incomplete). The rest means that it compiles the primitive DOES-XT
with the xt of the COMPILE,d word as immediate argument. DOES-XT
pushes the body of the word and then EXECUTEs the xt that SET-DOES>
has registered for this word. In most cases this is a colon
definition (always if DOES> is used), so the next thing that happens
is DOCOL, and then the code for the colon definition is run.
>
I have now replaced this with
>
: does, ( xt -- ) does-check dup >body lit, >extra @ compile, ;
>
What this does is to compile the body as a literal, and then it
COMPILE,s the xt that DOES-XT would EXECUTE. In the common case of a
colon definition this compiles a call to the colon definition. This
saves the overhead of accessing the doesfield and of dispatching on
its contents at run-time; all that is now done during compilation.
Another benefit: Gforth used to implement special COMPILE,
implementations for 2VALUE and FVALUE. Here's the old implementation
of FVALUE:
: opt-fval ( xt -- ) >body postpone Literal postpone f@ ;
create dummy-fvalue
' f@ set-does>
' fvalue-to set-to
' opt-fval set-optimizer
: fvalue ( r "name" -- ) \ floating-ext f-value
\g Define @i{name} @code{( -- r1 )} where @i{r1} initially is
\g @i{r}; this value can be changed with @code{to @i{name}} or
\g @code{->@i{name}}.
['] dummy-fvalue create-from reveal f, ;
The new DOES, generates exactly the same code for FVALUEs as OPT-FVAL
does, so we no longer need OPT-FVAL and the use of SET-OPTIMIZER here.
Likewise for 2VALUE. This simplification reduces the image size by
927 bytes and the native-code size by 176 bytes.
The code for compiling an FVALUE looks as follows (before and after
the change):
5e fvalue x ok
: bla x ; ok
see-code bla
$7F1341F2D5A8 lit 1->2
$7F1341F2D5B0 x
7F1341A4EA63: mov r15,$08[rbx]
$7F1341F2D5B8 f@ 2->1
7F1341A4EA67: movsd [r12],xmm15
7F1341A4EA6D: movsd xmm15,[r15]
7F1341A4EA72: sub r12,$08
$7F1341F2D5C0 ;s 1->1
7F1341A4EA76: mov rbx,[r14]
7F1341A4EA79: add r14,$08
7F1341A4EA7D: mov rax,[rbx]
7F1341A4EA80: jmp eax
- 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 2024: https://euro.theforth.net