Re: recursion

Liste des GroupesRevenir à cl forth 
Sujet : Re: recursion
De : anton (at) *nospam* mips.complang.tuwien.ac.at (Anton Ertl)
Groupes : comp.lang.forth
Date : 22. Jul 2024, 15:44:41
Autres entêtes
Organisation : Institut fuer Computersprachen, Technische Universitaet Wien
Message-ID : <2024Jul22.164441@mips.complang.tuwien.ac.at>
References : 1 2 3 4 5
User-Agent : xrn 10.11
Stephen Pelc <stephen@vfxforth.com> writes:
On 17 Jul 2024 at 08:47:45 BST, "dxf" <dxforth@gmail.com> wrote:
>
DEFER may not be as fast as a directly patched definition but neither
has that prevented a generation from using it.
>
At least on x64 and CISC CPUs, calling a deferred word is just
 CALL [] foo
rather than
 CALL foo
>
The difference on x64 is one byte and a a few (hardware and cache
dependent) cycles.

It's a bit more in Gforth (see below), but indeed, the difference is
small.

IMHO Forward referencing and resolving words are likely just to be
wrappers for syntactic sugar around DEFER and IS.

As I demonstrated in <2024Jul15.152917@mips.complang.tuwien.ac.at>,
Gforth's FORWARD uses direct calls in compiled words.  Maybe the
example was unclear.  Here is a simpler one:

forward x1
: y1 x1 ;
: x1 ;
y1

defer x2
: y2 x2 ;
: z2 ;
' z2 is x2
z2

cr see-code y1
cr see-code y2

Here's what the SEE-CODE calls produce:

cr see-code y1
$7F411D19D810 call    1->1
$7F411D19D818 x1
   0x00007f411ce3f2c0:  mov    0x8(%rbx),%rax
   0x00007f411ce3f2c4:  sub    $0x8,%r14
   0x00007f411ce3f2c8:  add    $0x10,%rbx
   0x00007f411ce3f2cc:  mov    %rbx,(%r14)
   0x00007f411ce3f2cf:  mov    %rax,%rbx
   0x00007f411ce3f2d2:  mov    (%rbx),%rax
   0x00007f411ce3f2d5:  jmp    *%rax
$7F411D19D820 ;s    1->1
   0x00007f411ce3f2d7:  mov    (%r14),%rbx
   0x00007f411ce3f2da:  add    $0x8,%r14
   0x00007f411ce3f2de:  mov    (%rbx),%rax
   0x00007f411ce3f2e1:  jmp    *%rax
 ok
cr see-code y2
$7F411D19D8B0 lit-perform    1->1
$7F411D19D8B8 x2
   0x00007f411ce3f2f0:  mov    0x8(%rbx),%rax
   0x00007f411ce3f2f4:  add    $0x8,%rbx
   0x00007f411ce3f2f8:  mov    (%rax),%rdx
   0x00007f411ce3f2fb:  mov    -0x10(%rdx),%rax
   0x00007f411ce3f2ff:  jmp    *%rax
$7F411D19D8C0 ;s    1->1
   0x00007f411ce3f301:  mov    (%r14),%rbx
   0x00007f411ce3f304:  add    $0x8,%r14
   0x00007f411ce3f308:  mov    (%rbx),%rax
   0x00007f411ce3f30b:  jmp    *%rax

So the LIT-PERFORM in Y2 looks shorter, but it calls
the docol of Z2:

docol: 19 discode
   0x00005647dfff8a08 <gforth_engine+104>:      add    $0x8,%rbx
   0x00005647dfff8a0c <gforth_engine+108>:      sub    $0x8,%r14
   0x00005647dfff8a10 <gforth_engine+112>:      mov    %rbx,(%r14)
   0x00005647dfff8a13 <gforth_engine+115>:      mov    %rdx,%rbx
   0x00005647dfff8a16 <gforth_engine+118>:      mov    (%rbx),%rax
   0x00005647dfff8a19 <gforth_engine+121>:      jmp    *%rax

before it executes the code of Z2.  By contrast, the CALL in Y1
directly continues with the code of X1.  So overall, the
LIT-PERFORM+DOCOL cost 5+6=11 instructions, while the CALL costs 7
instructions.

And before we forget, here we see clearly that the FORWARD is resolved
to a direct call (CALL), not the code generated for a deferred word.

- anton
--
M. Anton Ertl  http://www.complang.tuwien.ac.at/anton/home.html
comp.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

Date Sujet#  Auteur
14 Jul 24 * Re: exercise in double number arithmetic35Marc Olschok
14 Jul 24 +- Re: exercise in double number arithmetic1Marc Olschok
15 Jul 24 +* Re: exercise in double number arithmetic12Krishna Myneni
15 Jul 24 i+* Re: exercise in double number arithmetic8minforth
15 Jul 24 ii`* Re: exercise in double number arithmetic7minforth
15 Jul 24 ii `* Re: exercise in double number arithmetic6Ahmed
15 Jul 24 ii  `* Re: exercise in double number arithmetic5minforth
15 Jul 24 ii   +- Re: exercise in double number arithmetic1minforth
15 Jul 24 ii   +- Re: exercise in double number arithmetic1Ahmed
15 Jul 24 ii   `* Re: exercise in double number arithmetic2albert
15 Jul 24 ii    `- Re: exercise in double number arithmetic1minforth
15 Jul 24 i+- Re: exercise in double number arithmetic1Anton Ertl
15 Jul 24 i+- Re: exercise in double number arithmetic1Gerry Jackson
31 Jul 24 i`- Re: exercise in double number arithmetic1Marc Olschok
15 Jul 24 `* Re: exercise in double number arithmetic21minforth
15 Jul 24  `* recursion (was: exercise in double number arithmetic)20Anton Ertl
15 Jul 24   +* Re: recursion10Marc Olschok
15 Jul 24   i`* Re: recursion9Ruvim
15 Jul 24   i +* Re: recursion5Gerry Jackson
15 Jul 24   i i+* Re: recursion2Gerry Jackson
16 Jul 24   i ii`- Re: recursion1Gerry Jackson
16 Jul 24   i i`* Re: recursion2Ruvim
16 Jul 24   i i `- Re: recursion1dxf
16 Jul 24   i +- Re: recursion1mhx
16 Jul 24   i `* Re: recursion2Ruvim
16 Jul 24   i  `- Re: recursion1Ruvim
16 Jul 24   `* Re: recursion9sjack
16 Jul 24    +- Re: recursion1sjack
16 Jul 24    +* Re: recursion4minforth
16 Jul 24    i`* Re: recursion3sjack
16 Jul 24    i `* Re: recursion2minforth
16 Jul 24    i  `- Re: recursion1mhx
17 Jul 24    `* Re: recursion3dxf
22 Jul 24     `* Re: recursion2Stephen Pelc
22 Jul 24      `- Re: recursion1Anton Ertl

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal