Rocksolid Light

Welcome to Rocksolid Light

mail  files  register  newsreader  groups  login

Message-ID:  

You can't evaluate a man by logic alone. -- McCoy, "I, Mudd", stardate 4513.3


devel / comp.lang.forth / Re: Forth systems where do/?do pushes that loop start address

SubjectAuthor
* Forth systems where do/?do pushes that loop start addressAnton Ertl
+* Re: Forth systems where do/?do pushes that loop start addressKrishna Myneni
|+- Re: Forth systems where do/?do pushes that loop start addressKrishna Myneni
|+- Re: Forth systems where do/?do pushes that loop start addressAnton Ertl
|`- Re: Forth systems where do/?do pushes that loop start addressAnton Ertl
+* Re: Forth systems where do/?do pushes that loop start addressmhx
|`- Re: Forth systems where do/?do pushes that loop start addressAnton Ertl
+* Re: Forth systems where do/?do pushes that loop start addressStephen Pelc
|`* Re: Forth systems where do/?do pushes that loop start addressdxf
| `- Re: Forth systems where do/?do pushes that loop start addressStephen Pelc
`* Re: Forth systems where do/?do pushes that loop start addressRuvim
 +* Re: Forth systems where do/?do pushes that loop start addressminforth
 |`- Re: Forth systems where do/?do pushes that loop start addressRuvim
 `* Re: Forth systems where do/?do pushes that loop start addressdxf
  +* Re: Forth systems where do/?do pushes that loop start addressRuvim
  |`- Re: Forth systems where do/?do pushes that loop start addressdxf
  `* Summary: Forth systems where do/?do pushes that loop start addressAnton Ertl
   `* Re: Summary: Forth systems where do/?do pushes that loop start addressdxf
    `- Re: Summary: Forth systems where do/?do pushes that loop start addressAnton Ertl

1
Forth systems where do/?do pushes that loop start address

<2024Mar4.182409@mips.complang.tuwien.ac.at>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=26250&group=comp.lang.forth#26250

  copy link   Newsgroups: comp.lang.forth
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: anton@mips.complang.tuwien.ac.at (Anton Ertl)
Newsgroups: comp.lang.forth
Subject: Forth systems where do/?do pushes that loop start address
Date: Mon, 04 Mar 2024 17:24:09 GMT
Organization: Institut fuer Computersprachen, Technische Universitaet Wien
Lines: 19
Message-ID: <2024Mar4.182409@mips.complang.tuwien.ac.at>
Injection-Info: dont-email.me; posting-host="5f7b967894908ea9ab01696180a3a5c9";
logging-data="3448296"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/HptfcniFIEyKZUGV89+Ev"
Cancel-Lock: sha1:CHQDmZRLWOQfcv0FiqyFn3k7UWQ=
X-newsreader: xrn 10.11
 by: Anton Ertl - Mon, 4 Mar 2024 17:24 UTC

Many years ago I have read here about Forth systems where DO and ?DO
push three items on the return stack: the two values from the data
stack (initial index and limit) like many other Forth systems, but in
addition they also push the address that LOOP/+LOOP later jumps to.

I used to consider this to be inefficient, but it turns out that in an
efficient interpreter-based Forth system like, say gforth-fast from
2022 it would actually be more efficient than compiling that address
with the (LOOP)/(+LOOP) and loading it from there.

My question is: Which Forth systems have a DO/?DO that pushes the
address that LOOP/+LOOP then jumps to?

- 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 2023: https://euro.theforth.net/2023

Re: Forth systems where do/?do pushes that loop start address

<us68iq$3jsgk$1@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=26256&group=comp.lang.forth#26256

  copy link   Newsgroups: comp.lang.forth
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: krishna.myneni@ccreweb.org (Krishna Myneni)
Newsgroups: comp.lang.forth
Subject: Re: Forth systems where do/?do pushes that loop start address
Date: Mon, 4 Mar 2024 22:54:50 -0600
Organization: A noiseless patient Spider
Lines: 38
Message-ID: <us68iq$3jsgk$1@dont-email.me>
References: <2024Mar4.182409@mips.complang.tuwien.ac.at>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 5 Mar 2024 04:54:50 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="bb34fdb24d10ff12e7deded905420378";
logging-data="3797524"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/CXc/8CBFeewLcsmzbtiJV"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:CvAUZ8+hGWJbzbfjRSuVa0xXz5E=
Content-Language: en-US
In-Reply-To: <2024Mar4.182409@mips.complang.tuwien.ac.at>
 by: Krishna Myneni - Tue, 5 Mar 2024 04:54 UTC

On 3/4/24 11:24, Anton Ertl wrote:
> Many years ago I have read here about Forth systems where DO and ?DO
> push three items on the return stack: the two values from the data
> stack (initial index and limit) like many other Forth systems, but in
> addition they also push the address that LOOP/+LOOP later jumps to.
>
> I used to consider this to be inefficient, but it turns out that in an
> efficient interpreter-based Forth system like, say gforth-fast from
> 2022 it would actually be more efficient than compiling that address
> with the (LOOP)/(+LOOP) and loading it from there.
>
> My question is: Which Forth systems have a DO/?DO that pushes the
> address that LOOP/+LOOP then jumps to?
>
> - anton

Yes, kForth uses this method. DO pushes three items onto the return
stack, the two loop parameters, and the virtual instruction pointer.

\ From ForthVM.cpp

int CPP_do ()
{ // stack: ( -- | generate opcodes for beginning of loop structure )

pCurrentOps->push_back(OP_PUSH);
pCurrentOps->push_back(OP_PUSH);
pCurrentOps->push_back(OP_PUSHIP);

dostack.push(pCurrentOps->size());
return 0;
}

--
Krishna

Re: Forth systems where do/?do pushes that loop start address

<us68n5$3jsgk$2@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=26257&group=comp.lang.forth#26257

  copy link   Newsgroups: comp.lang.forth
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: krishna.myneni@ccreweb.org (Krishna Myneni)
Newsgroups: comp.lang.forth
Subject: Re: Forth systems where do/?do pushes that loop start address
Date: Mon, 4 Mar 2024 22:57:09 -0600
Organization: A noiseless patient Spider
Lines: 42
Message-ID: <us68n5$3jsgk$2@dont-email.me>
References: <2024Mar4.182409@mips.complang.tuwien.ac.at>
<us68iq$3jsgk$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 5 Mar 2024 04:57:09 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="bb34fdb24d10ff12e7deded905420378";
logging-data="3797524"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19h4b/qoTeypPqkJ5325wPd"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:Hap67JQQ2jfLj2yYlFuLnm7/zo8=
In-Reply-To: <us68iq$3jsgk$1@dont-email.me>
Content-Language: en-US
 by: Krishna Myneni - Tue, 5 Mar 2024 04:57 UTC

On 3/4/24 22:54, Krishna Myneni wrote:
> On 3/4/24 11:24, Anton Ertl wrote:
>> Many years ago I have read here about Forth systems where DO and ?DO
>> push three items on the return stack: the two values from the data
>> stack (initial index and limit) like many other Forth systems, but in
>> addition they also push the address that LOOP/+LOOP later jumps to.
>>
>> I used to consider this to be inefficient, but it turns out that in an
>> efficient interpreter-based Forth system like, say gforth-fast from
>> 2022 it would actually be more efficient than compiling that address
>> with the (LOOP)/(+LOOP) and loading it from there.
>>
>> My question is: Which Forth systems have a DO/?DO that pushes the
>> address that LOOP/+LOOP then jumps to?
>>
>> - anton
>
> Yes, kForth uses this method. DO pushes three items onto the return
> stack, the two loop parameters, and the virtual instruction pointer.
>
> \ From ForthVM.cpp
>
> int CPP_do ()
> {
>   // stack: ( -- | generate opcodes for beginning of loop structure )
>
>   pCurrentOps->push_back(OP_PUSH);
>   pCurrentOps->push_back(OP_PUSH);
>   pCurrentOps->push_back(OP_PUSHIP);
>
>   dostack.push(pCurrentOps->size());
>   return 0;
> }
>

To be clear, DO compiles three VM instructions to push the items onto
the return stack.

--
Krishna

Re: Forth systems where do/?do pushes that loop start address

<abd5b4fdc9e825e8e2be5785e597d237@www.novabbs.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=26262&group=comp.lang.forth#26262

  copy link   Newsgroups: comp.lang.forth
Path: i2pn2.org!.POSTED!not-for-mail
From: mhx@iae.nl (mhx)
Newsgroups: comp.lang.forth
Subject: Re: Forth systems where do/?do pushes that loop sta
rt address
Date: Tue, 5 Mar 2024 09:32:34 +0000
Organization: novaBBS
Message-ID: <abd5b4fdc9e825e8e2be5785e597d237@www.novabbs.com>
References: <2024Mar4.182409@mips.complang.tuwien.ac.at>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Info: i2pn2.org;
logging-data="895175"; mail-complaints-to="usenet@i2pn2.org";
posting-account="t+lO0yBNO1zGxasPvGSZV1BRu71QKx+JE37DnW+83jQ";
User-Agent: Rocksolid Light
X-Spam-Checker-Version: SpamAssassin 4.0.0
X-Rslight-Posting-User: 59549e76d0c3560fb37b97f0b9407a8c14054f24
X-Rslight-Site: $2y$10$Xle2UVFIwqTiqGIaMRQPiOw.tD6x5.tlNmqlHuis49sSvwxwG74Uq
 by: mhx - Tue, 5 Mar 2024 09:32 UTC

Anton Ertl wrote:
[..]
> My question is: Which Forth systems have a DO/?DO that pushes the
> address that LOOP/+LOOP then jumps to?

It is not 100% clear what you mean.
In iForth I do something special with both DO and LOOP , where the
LOOP action is probably closest to your question.

FORTH> : test 22 10 2 do 1+ loop . ; ok
FORTH> see test
Flags: ANSI
$01340A00 : test
$01340A0A push #22 b#
$01340A0C mov rcx, #10 d#
$01340A13 mov rbx, 2 d#
$01340A1A call (DO) offset NEAR
$01340A24 lea rax, [rax 0 +] qword
$01340A28 lea rbx, [rbx 1 +] qword
$01340A2C add [rbp 0 +] qword, 1 b#
$01340A31 add [rbp 8 +] qword, 1 b#
$01340A36 jno $01340A28 offset NEAR
$01340A3C add rbp, #24 b#
$01340A40 push rbx
$01340A41 jmp .+10 ( $0124A102 ) offset NEAR

Or, without SYMBOLIC disguising the (DO) machine code:

FORTH> false TO symbolic ok
FORTH> $01340A1A idis
$01340A1A call $012413F8 offset NEAR
$01340A1F jmp $01340A3C offset NEAR
$01340A24 lea rax, [rax 0 +] qword
$01340A28 lea rbx, [rbx 1 +] qword
$01340A2C add [rbp 0 +] qword, 1 b#
$01340A31 add [rbp 8 +] qword, 1 b#
$01340A36 jno $01340A28 offset NEAR
$01340A3C add rbp, #24 b#
$01340A40 push rbx
$01340A41 jmp $0124A102 offset NEAR

-marcel

Re: Forth systems where do/?do pushes that loop start address

<us6v27$3nvkk$1@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=26263&group=comp.lang.forth#26263

  copy link   Newsgroups: comp.lang.forth
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: stephen@vfxforth.com (Stephen Pelc)
Newsgroups: comp.lang.forth
Subject: Re: Forth systems where do/?do pushes that loop start address
Date: Tue, 5 Mar 2024 11:18:31 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 17
Message-ID: <us6v27$3nvkk$1@dont-email.me>
References: <2024Mar4.182409@mips.complang.tuwien.ac.at>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=fixed
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 5 Mar 2024 11:18:31 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="4d7c3b01435ce4c3fc1821af160f8921";
logging-data="3931796"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/+BaJeJjvZF3wW6FBtp+sM"
User-Agent: Usenapp for MacOS
Cancel-Lock: sha1:Tc4XMEd1SRUas1ovy46Sge2NN8E=
X-Usenapp: v1.27.2/l - Full License
 by: Stephen Pelc - Tue, 5 Mar 2024 11:18 UTC

On 4 Mar 2024 at 18:24:09 CET, "Anton Ertl" <Anton Ertl> wrote:
>
> My question is: Which Forth systems have a DO/?DO that pushes the
> address that LOOP/+LOOP then jumps to?
>
> - anton

VFX since the beginning.

Stephen

--
Stephen Pelc, stephen@vfxforth.com
MicroProcessor Engineering, Ltd. - More Real, Less Time
133 Hill Lane, Southampton SO15 5AF, England
tel: +44 (0)78 0390 3612, +34 649 662 974
http://www.mpeforth.com - free VFX Forth downloads

Re: Forth systems where do/?do pushes that loop start address

<2024Mar5.121836@mips.complang.tuwien.ac.at>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=26264&group=comp.lang.forth#26264

  copy link   Newsgroups: comp.lang.forth
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: anton@mips.complang.tuwien.ac.at (Anton Ertl)
Newsgroups: comp.lang.forth
Subject: Re: Forth systems where do/?do pushes that loop start address
Date: Tue, 05 Mar 2024 11:18:36 GMT
Organization: Institut fuer Computersprachen, Technische Universitaet Wien
Lines: 41
Message-ID: <2024Mar5.121836@mips.complang.tuwien.ac.at>
References: <2024Mar4.182409@mips.complang.tuwien.ac.at> <abd5b4fdc9e825e8e2be5785e597d237@www.novabbs.com>
Injection-Info: dont-email.me; posting-host="e3a6c9aa69f794531e98f8bdcaa53fa3";
logging-data="3939142"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18tuEvBxFpqasinGohj+I2N"
Cancel-Lock: sha1:bbq0muHeOOJ4rYfyI8LkHqroyQs=
X-newsreader: xrn 10.11
 by: Anton Ertl - Tue, 5 Mar 2024 11:18 UTC

mhx@iae.nl (mhx) writes:
>Anton Ertl wrote:
>[..]
>> My question is: Which Forth systems have a DO/?DO that pushes the
>> address that LOOP/+LOOP then jumps to?
>
>It is not 100% clear what you mean.
>In iForth I do something special with both DO and LOOP , where the
>LOOP action is probably closest to your question.
>
>FORTH> : test 22 10 2 do 1+ loop . ; ok
>FORTH> see test
>Flags: ANSI
>$01340A00 : test
>$01340A0A push #22 b#
>$01340A0C mov rcx, #10 d#
>$01340A13 mov rbx, 2 d#
>$01340A1A call (DO) offset NEAR
>$01340A24 lea rax, [rax 0 +] qword
>$01340A28 lea rbx, [rbx 1 +] qword
>$01340A2C add [rbp 0 +] qword, 1 b#
>$01340A31 add [rbp 8 +] qword, 1 b#
>$01340A36 jno $01340A28 offset NEAR
>$01340A3C add rbp, #24 b#
>$01340A40 push rbx
>$01340A41 jmp .+10 ( $0124A102 ) offset NEAR

Native-code systems generally use direct (conditional) jumps to the
loop start, like iforth does here with the jno.

What I meant is that some (interpreter-based) systems keep the loop
start address ($01340A28 in this example) on the return stack, and
LOOP/+LOOP takes it from there and then performs a (VM-level) jump
there (unless the loop is exited).

- 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 2023: https://euro.theforth.net/2023

Re: Forth systems where do/?do pushes that loop start address

<2024Mar5.123811@mips.complang.tuwien.ac.at>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=26265&group=comp.lang.forth#26265

  copy link   Newsgroups: comp.lang.forth
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: anton@mips.complang.tuwien.ac.at (Anton Ertl)
Newsgroups: comp.lang.forth
Subject: Re: Forth systems where do/?do pushes that loop start address
Date: Tue, 05 Mar 2024 11:38:11 GMT
Organization: Institut fuer Computersprachen, Technische Universitaet Wien
Lines: 16
Message-ID: <2024Mar5.123811@mips.complang.tuwien.ac.at>
References: <2024Mar4.182409@mips.complang.tuwien.ac.at> <us68iq$3jsgk$1@dont-email.me>
Injection-Info: dont-email.me; posting-host="e3a6c9aa69f794531e98f8bdcaa53fa3";
logging-data="3946814"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+Ad7LnAqXgxn5Sds6o8ncL"
Cancel-Lock: sha1:Inxj2JNxXaqjImvZg++3PrqLe2k=
X-newsreader: xrn 10.11
 by: Anton Ertl - Tue, 5 Mar 2024 11:38 UTC

Krishna Myneni <krishna.myneni@ccreweb.org> writes:
>Yes, kForth uses this method. DO pushes three items onto the return
>stack, the two loop parameters, and the virtual instruction pointer.

Thanks. You can find the performance benefit from that in gforth-fast
in the right bar of each benchmark
<http://www.complang.tuwien.ac.at/anton/tmp/select-uarch.eps>. It
provides pretty good speedups for siev, bubble, and matrix, and small
speedups in sha512.

- 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 2023: https://euro.theforth.net/2023

Re: Forth systems where do/?do pushes that loop start address

<65e7adf0$1@news.ausics.net>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=26277&group=comp.lang.forth#26277

  copy link   Newsgroups: comp.lang.forth
Date: Wed, 6 Mar 2024 10:42:41 +1100
MIME-Version: 1.0
User-Agent: Mozilla Thunderbird
Subject: Re: Forth systems where do/?do pushes that loop start address
Newsgroups: comp.lang.forth
References: <2024Mar4.182409@mips.complang.tuwien.ac.at>
<us6v27$3nvkk$1@dont-email.me>
Content-Language: en-GB
From: dxforth@gmail.com (dxf)
In-Reply-To: <us6v27$3nvkk$1@dont-email.me>
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: news.ausics.net
Message-ID: <65e7adf0$1@news.ausics.net>
Organization: Ausics - https://newsgroups.ausics.net
Lines: 31
X-Complaints: abuse@ausics.net
Path: i2pn2.org!i2pn.org!news.bbs.nz!news.ausics.net!not-for-mail
 by: dxf - Tue, 5 Mar 2024 23:42 UTC

On 5/03/2024 10:18 pm, Stephen Pelc wrote:
> On 4 Mar 2024 at 18:24:09 CET, "Anton Ertl" <Anton Ertl> wrote:
>>
>> My question is: Which Forth systems have a DO/?DO that pushes the
>> address that LOOP/+LOOP then jumps to?
>>
>> - anton
>
> VFX since the beginning.

AFAICS the loop jump addr is hard-coded (JNO) as that was generally
seen as most efficient:

: test 10 0 do loop ; ok
see test
TEST
( 005945D0 488D6DF0 ) LEA RBP, [RBP+-10]
( 005945D4 48C745000A000000 ) MOV QWord [RBP], # 0000000A
( 005945DC 48895D08 ) MOV [RBP+08], RBX
( 005945E0 BB00000000 ) MOV EBX, # 00000000
( 005945E5 E86615E9FFFF45590000000 CALL 00425B50 (DO) 00000000005945FF
( 005945F2 49FFC6 ) INC R14
( 005945F5 49FFC7 ) INC R15
( 005945F8 71F8 ) JNO 005945F2
( 005945FA 415E ) POP R14
( 005945FC 415F ) POP R15
( 005945FE 58 ) POP RAX
( 005945FF C3 ) RET/NEXT
( 48 bytes, 12 instructions )

Re: Forth systems where do/?do pushes that loop start address

<us9r2g$eh8t$1@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=26285&group=comp.lang.forth#26285

  copy link   Newsgroups: comp.lang.forth
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ruvim.pinka@gmail.com (Ruvim)
Newsgroups: comp.lang.forth
Subject: Re: Forth systems where do/?do pushes that loop start address
Date: Wed, 6 Mar 2024 17:28:45 +0400
Organization: A noiseless patient Spider
Lines: 53
Message-ID: <us9r2g$eh8t$1@dont-email.me>
References: <2024Mar4.182409@mips.complang.tuwien.ac.at>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 6 Mar 2024 13:28:49 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="f94553e154d0facccf1d9a5092eeb5a9";
logging-data="476445"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/YIwVLJTBvMes5uH/TYWwi"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:i1+i0kA7QR18OrmRCslsP5IDiJM=
In-Reply-To: <2024Mar4.182409@mips.complang.tuwien.ac.at>
Content-Language: en-US
 by: Ruvim - Wed, 6 Mar 2024 13:28 UTC

On 2024-03-04 21:24, Anton Ertl wrote:
> Many years ago I have read here about Forth systems where DO and ?DO
> push three items on the return stack: the two values from the data
> stack (initial index and limit) like many other Forth systems, but in
> addition they also push the address that LOOP/+LOOP later jumps to.
>
> I used to consider this to be inefficient, but it turns out that in an
> efficient interpreter-based Forth system like, say gforth-fast from
> 2022 it would actually be more efficient than compiling that address
> with the (LOOP)/(+LOOP) and loading it from there.
>
> My question is: Which Forth systems have a DO/?DO that pushes the
> address that LOOP/+LOOP then jumps to?

In SP-Forth v3 and v4 (they generate native code), "DO" pushes three
items on the return stack, and among them the address that "LEAVE" then
jumps to. The address (actually, an offset) for "LOOP" is inlined.

Code in SP-Forth/4

: foo 7 1 do i . loop ; see foo

579793 8945FC MOV FC [EBP] , EAX
579796 C745F807000000 MOV F8 [EBP] , # 7
57979D B801000000 MOV EAX , # 1
5797A2 8BD8 MOV EBX , EAX
5797A4 81C000000080 ADD EAX , # 80000000
5797AA 2B45F8 SUB EAX , F8 [EBP]
5797AD 8BD0 MOV EDX , EAX
5797AF 8B45FC MOV EAX , FC [EBP]
5797B2 68D7975700 PUSH , # 5797D7
5797B7 52 PUSH EDX
5797B8 53 PUSH EBX
5797B9 90 XCHG EAX, EAX
5797BA 90 XCHG EAX, EAX
5797BB 90 XCHG EAX, EAX
5797BC 8945FC MOV FC [EBP] , EAX
5797BF 8B0424 MOV EAX , [ESP]
5797C2 8D6DFC LEA EBP , FC [EBP]
5797C5 E81ECDFDFF CALL 5564E8 ( . )
5797CA FF0424 INC [ESP]
5797CD FF442404 INC 4 [ESP]
5797D1 71E9 JNO 5797BC
5797D3 8D64240C LEA ESP , C [ESP]
5797D7 C3 RET NEAR
END-CODE

--
Ruvim

Re: Forth systems where do/?do pushes that loop start address

<7a026af384a608f38351e22c7a8621b1@www.novabbs.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=26286&group=comp.lang.forth#26286

  copy link   Newsgroups: comp.lang.forth
Path: i2pn2.org!.POSTED!not-for-mail
From: minforth@gmx.net (minforth)
Newsgroups: comp.lang.forth
Subject: Re: Forth systems where do/?do pushes that loop sta
rt address
Date: Wed, 6 Mar 2024 14:15:25 +0000
Organization: novaBBS
Message-ID: <7a026af384a608f38351e22c7a8621b1@www.novabbs.com>
References: <2024Mar4.182409@mips.complang.tuwien.ac.at> <us9r2g$eh8t$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Info: i2pn2.org;
logging-data="1057567"; mail-complaints-to="usenet@i2pn2.org";
posting-account="t+lO0yBNO1zGxasPvGSZV1BRu71QKx+JE37DnW+83jQ";
User-Agent: Rocksolid Light
X-Rslight-Site: $2y$10$z3HLlh7hGwfxebVdJuWRNuRVTfQrY5mFIDVQi0GyrKUbE6QnBVtZy
X-Spam-Checker-Version: SpamAssassin 4.0.0
X-Rslight-Posting-User: d2a19558f194e2f1f8393b8d9be9ef51734a4da3
 by: minforth - Wed, 6 Mar 2024 14:15 UTC

Ruvim wrote:

> In SP-Forth v3 and v4 (they generate native code), "DO" pushes three
> items on the return stack, and among them the address that "LEAVE" then
> jumps to.

That would make implementing BREAK and CONTINUE rather easy...

Re: Forth systems where do/?do pushes that loop start address

<usa1tf$fpqj$1@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=26287&group=comp.lang.forth#26287

  copy link   Newsgroups: comp.lang.forth
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ruvim.pinka@gmail.com (Ruvim)
Newsgroups: comp.lang.forth
Subject: Re: Forth systems where do/?do pushes that loop start address
Date: Wed, 6 Mar 2024 19:25:31 +0400
Organization: A noiseless patient Spider
Lines: 15
Message-ID: <usa1tf$fpqj$1@dont-email.me>
References: <2024Mar4.182409@mips.complang.tuwien.ac.at>
<us9r2g$eh8t$1@dont-email.me>
<7a026af384a608f38351e22c7a8621b1@www.novabbs.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 6 Mar 2024 15:25:35 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="f94553e154d0facccf1d9a5092eeb5a9";
logging-data="517971"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18O8ek4gdqRgW/PPv5QRTij"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:Mm8QI4ZWnDChzIijsfv9SkHapi8=
Content-Language: en-US
In-Reply-To: <7a026af384a608f38351e22c7a8621b1@www.novabbs.com>
 by: Ruvim - Wed, 6 Mar 2024 15:25 UTC

On 2024-03-06 18:15, minforth wrote:
> Ruvim wrote:
>
>> In SP-Forth v3 and v4 (they generate native code), "DO" pushes three
>> items on the return stack, and among them the address that "LEAVE"
>> then jumps to.
>
> That would make implementing BREAK and CONTINUE rather easy...

Yes, and it does make "LEAVE" implementing easy. But there are no other
advantages.

--
Ruvim

Re: Forth systems where do/?do pushes that loop start address

<usahhu$jeq8$1@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=26288&group=comp.lang.forth#26288

  copy link   Newsgroups: comp.lang.forth
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: stephen@vfxforth.com (Stephen Pelc)
Newsgroups: comp.lang.forth
Subject: Re: Forth systems where do/?do pushes that loop start address
Date: Wed, 6 Mar 2024 19:52:30 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 47
Message-ID: <usahhu$jeq8$1@dont-email.me>
References: <2024Mar4.182409@mips.complang.tuwien.ac.at> <us6v27$3nvkk$1@dont-email.me> <65e7adf0$1@news.ausics.net>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=fixed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 6 Mar 2024 19:52:30 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="f3fd320e0db4ae60437c6b69aff404cf";
logging-data="637768"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/8ciFxi1FuXNXcDX6FBCPU"
User-Agent: Usenapp for MacOS
Cancel-Lock: sha1:IsQe8EeLbN82w8TsJcSy+VPhJyg=
X-Usenapp: v1.27.2/l - Full License
 by: Stephen Pelc - Wed, 6 Mar 2024 19:52 UTC

On 6 Mar 2024 at 00:42:41 CET, "dxf" <dxforth@gmail.com> wrote:

> On 5/03/2024 10:18 pm, Stephen Pelc wrote:
>> On 4 Mar 2024 at 18:24:09 CET, "Anton Ertl" <Anton Ertl> wrote:
>>>
>>> My question is: Which Forth systems have a DO/?DO that pushes the
>>> address that LOOP/+LOOP then jumps to?
>>>
>>> - anton
>>
>> VFX since the beginning.
>
> AFAICS the loop jump addr is hard-coded (JNO) as that was generally
> seen as most efficient:
>
> : test 10 0 do loop ; ok
> see test
> TEST
> ( 005945D0 488D6DF0 ) LEA RBP, [RBP+-10]
> ( 005945D4 48C745000A000000 ) MOV QWord [RBP], # 0000000A
> ( 005945DC 48895D08 ) MOV [RBP+08], RBX
> ( 005945E0 BB00000000 ) MOV EBX, # 00000000
> ( 005945E5 E86615E9FFFF45590000000 CALL 00425B50 (DO) 00000000005945FF
> ( 005945F2 49FFC6 ) INC R14
> ( 005945F5 49FFC7 ) INC R15
> ( 005945F8 71F8 ) JNO 005945F2
> ( 005945FA 415E ) POP R14
> ( 005945FC 415F ) POP R15
> ( 005945FE 58 ) POP RAX
> ( 005945FF C3 ) RET/NEXT
> ( 48 bytes, 12 instructions )

The three items pushed are
loop exit address
limit data of previous loop
index data of previous loop

The slightly odd list of items allows us to keep the index/limit data in
registers.

Stephen
--
Stephen Pelc, stephen@vfxforth.com
MicroProcessor Engineering, Ltd. - More Real, Less Time
133 Hill Lane, Southampton SO15 5AF, England
tel: +44 (0)78 0390 3612, +34 649 662 974
http://www.mpeforth.com - free VFX Forth downloads

Re: Forth systems where do/?do pushes that loop start address

<65e922ca$1@news.ausics.net>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=26290&group=comp.lang.forth#26290

  copy link   Newsgroups: comp.lang.forth
Date: Thu, 7 Mar 2024 13:13:32 +1100
MIME-Version: 1.0
User-Agent: Mozilla Thunderbird
Subject: Re: Forth systems where do/?do pushes that loop start address
Content-Language: en-GB
Newsgroups: comp.lang.forth
References: <2024Mar4.182409@mips.complang.tuwien.ac.at>
<us9r2g$eh8t$1@dont-email.me>
From: dxforth@gmail.com (dxf)
In-Reply-To: <us9r2g$eh8t$1@dont-email.me>
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: news.ausics.net
Message-ID: <65e922ca$1@news.ausics.net>
Organization: Ausics - https://newsgroups.ausics.net
Lines: 8
X-Complaints: abuse@ausics.net
Path: i2pn2.org!i2pn.org!news.bbs.nz!news.ausics.net!not-for-mail
 by: dxf - Thu, 7 Mar 2024 02:13 UTC

On 7/03/2024 12:28 am, Ruvim wrote:
> ...
> In SP-Forth v3 and v4 (they generate native code), "DO" pushes three items on the return stack, and among them the address that "LEAVE" then jumps to. ...
That is the classic implementation as suggested by Bob Berkey - inventor of
the Forth-83 DO LOOP. IIUC Anton is asking about systems that push the loop
address - not the exit address.

Re: Forth systems where do/?do pushes that loop start address

<usc14u$vj20$1@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=26291&group=comp.lang.forth#26291

  copy link   Newsgroups: comp.lang.forth
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ruvim.pinka@gmail.com (Ruvim)
Newsgroups: comp.lang.forth
Subject: Re: Forth systems where do/?do pushes that loop start address
Date: Thu, 7 Mar 2024 13:24:46 +0400
Organization: A noiseless patient Spider
Lines: 26
Message-ID: <usc14u$vj20$1@dont-email.me>
References: <2024Mar4.182409@mips.complang.tuwien.ac.at>
<us9r2g$eh8t$1@dont-email.me> <65e922ca$1@news.ausics.net>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 7 Mar 2024 09:24:46 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="7c31c56a8902121f849736208126ed69";
logging-data="1035328"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/E/mwBh5iSpP4Vo09rmo+3"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:/iC8DSQtwaXIsXNMGMFjDo4cXLM=
In-Reply-To: <65e922ca$1@news.ausics.net>
Content-Language: en-US
 by: Ruvim - Thu, 7 Mar 2024 09:24 UTC

On 2024-03-07 06:13, dxf wrote:
> On 7/03/2024 12:28 am, Ruvim wrote:
>> ...
>> In SP-Forth v3 and v4 (they generate native code), "DO" pushes three items on the return stack, and among them the address that "LEAVE" then jumps to. ...
> That is the classic implementation as suggested by Bob Berkey - inventor of
> the Forth-83 DO LOOP.

Thank you, I did not know that!

I see this fact was mentioned in Forth Dimension Volume 04 Number 3,
1982 (FD-V4N3, p24) [1]

> IIUC Anton is asking about systems that push the loop
> address - not the exit address.

Yes, I noticed, just wanted to give another option of three items on the
return stack.

[1]
https://archive.org/details/Forth_Dimension_Volume_04_Number_3/page/n24/mode/1up?view=theater

--
Ruvim

Re: Forth systems where do/?do pushes that loop start address

<65eabeb2$1@news.ausics.net>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=26300&group=comp.lang.forth#26300

  copy link   Newsgroups: comp.lang.forth
Date: Fri, 8 Mar 2024 18:30:58 +1100
MIME-Version: 1.0
User-Agent: Mozilla Thunderbird
Subject: Re: Forth systems where do/?do pushes that loop start address
Content-Language: en-GB
Newsgroups: comp.lang.forth
References: <2024Mar4.182409@mips.complang.tuwien.ac.at>
<us9r2g$eh8t$1@dont-email.me> <65e922ca$1@news.ausics.net>
<usc14u$vj20$1@dont-email.me>
From: dxforth@gmail.com (dxf)
In-Reply-To: <usc14u$vj20$1@dont-email.me>
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: news.ausics.net
Message-ID: <65eabeb2$1@news.ausics.net>
Organization: Ausics - https://newsgroups.ausics.net
Lines: 23
X-Complaints: abuse@ausics.net
Path: i2pn2.org!i2pn.org!news.bbs.nz!news.ausics.net!not-for-mail
 by: dxf - Fri, 8 Mar 2024 07:30 UTC

On 7/03/2024 8:24 pm, Ruvim wrote:
> On 2024-03-07 06:13, dxf wrote:
>> On 7/03/2024 12:28 am, Ruvim wrote:
>>> ...
>>> In SP-Forth v3 and v4 (they generate native code), "DO" pushes three items on the return stack, and among them the address that "LEAVE" then jumps to. ...
>> That is the classic implementation as suggested by Bob Berkey - inventor of
>> the Forth-83 DO LOOP.
>
> Thank you, I did not know that!
>
> I see this fact was mentioned in Forth Dimension Volume 04 Number 3, 1982 (FD-V4N3, p24) [1]
>
>
>> IIUC Anton is asking about systems that push the loop
>> address - not the exit address.
>
> Yes, I noticed, just wanted to give another option of three items on the return stack.

An alternative two item solution was presented in FD V5N4 p22. It would suit smaller
systems.

Summary: Forth systems where do/?do pushes that loop start address

<2024Mar9.190957@mips.complang.tuwien.ac.at>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=26318&group=comp.lang.forth#26318

  copy link   Newsgroups: comp.lang.forth
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: anton@mips.complang.tuwien.ac.at (Anton Ertl)
Newsgroups: comp.lang.forth
Subject: Summary: Forth systems where do/?do pushes that loop start address
Date: Sat, 09 Mar 2024 18:09:57 GMT
Organization: Institut fuer Computersprachen, Technische Universitaet Wien
Lines: 27
Message-ID: <2024Mar9.190957@mips.complang.tuwien.ac.at>
References: <2024Mar4.182409@mips.complang.tuwien.ac.at> <us9r2g$eh8t$1@dont-email.me> <65e922ca$1@news.ausics.net>
Injection-Info: dont-email.me; posting-host="24628311cf9514b3cbba0385260d6135";
logging-data="2590933"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19mJ46fFLBKEeXXMg6I/LfI"
Cancel-Lock: sha1:LLyksyL10AmXWU6hoJHZET68n2E=
X-newsreader: xrn 10.11
 by: Anton Ertl - Sat, 9 Mar 2024 18:09 UTC

dxf <dxforth@gmail.com> writes:
>On 7/03/2024 12:28 am, Ruvim wrote:
>> ...
>> In SP-Forth v3 and v4 (they generate native code), "DO" pushes three items on the return stack, and among them the address that "LEAVE" then jumps to. ...
>
>That is the classic implementation as suggested by Bob Berkey - inventor of
>the Forth-83 DO LOOP. IIUC Anton is asking about systems that push the loop
>address - not the exit address.

Correct.

So, to summarize the answers:

kForth keeps the loop-back address on the return stack in addition to
index and limit.

A number of systems (at least ciForth, VFX, SP-Forth) have followed
Bob Berkey's suggestion of keeping the loop-exit address on the return
stack for LEAVE. That is not what I was asking about, but it's also
interesting.

- 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 2023: https://euro.theforth.net/2023

Re: Forth systems where do/?do pushes that loop start address

<2024Mar9.192623@mips.complang.tuwien.ac.at>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=26319&group=comp.lang.forth#26319

  copy link   Newsgroups: comp.lang.forth
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: anton@mips.complang.tuwien.ac.at (Anton Ertl)
Newsgroups: comp.lang.forth
Subject: Re: Forth systems where do/?do pushes that loop start address
Date: Sat, 09 Mar 2024 18:26:23 GMT
Organization: Institut fuer Computersprachen, Technische Universitaet Wien
Lines: 28
Message-ID: <2024Mar9.192623@mips.complang.tuwien.ac.at>
References: <2024Mar4.182409@mips.complang.tuwien.ac.at> <us68iq$3jsgk$1@dont-email.me>
Injection-Info: dont-email.me; posting-host="24628311cf9514b3cbba0385260d6135";
logging-data="2590933"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+HNPr2R7r2wquHLEjw6F7C"
Cancel-Lock: sha1:Or6s2gGT/X27r7DRBpYE3h83dIk=
X-newsreader: xrn 10.11
 by: Anton Ertl - Sat, 9 Mar 2024 18:26 UTC

Krishna Myneni <krishna.myneni@ccreweb.org> writes:
>Yes, kForth uses this method. DO pushes three items onto the return
>stack, the two loop parameters, and the virtual instruction pointer.
>
>\ From ForthVM.cpp
>
>int CPP_do ()
>{
> // stack: ( -- | generate opcodes for beginning of loop structure )
>
> pCurrentOps->push_back(OP_PUSH);
> pCurrentOps->push_back(OP_PUSH);
> pCurrentOps->push_back(OP_PUSHIP);
>
> dostack.push(pCurrentOps->size());
> return 0;
>}

Thanks. Why do you do it this way? Do you want to break dependence
chains on the virtual instruction pointer (the reason for the speedup
in my results)?

- 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 2023: https://euro.theforth.net/2023

Re: Summary: Forth systems where do/?do pushes that loop start address

<65ecf431@news.ausics.net>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=26327&group=comp.lang.forth#26327

  copy link   Newsgroups: comp.lang.forth
Date: Sun, 10 Mar 2024 10:43:48 +1100
MIME-Version: 1.0
User-Agent: Mozilla Thunderbird
Subject: Re: Summary: Forth systems where do/?do pushes that loop start
address
Newsgroups: comp.lang.forth
References: <2024Mar4.182409@mips.complang.tuwien.ac.at>
<us9r2g$eh8t$1@dont-email.me> <65e922ca$1@news.ausics.net>
<2024Mar9.190957@mips.complang.tuwien.ac.at>
Content-Language: en-GB
From: dxforth@gmail.com (dxf)
In-Reply-To: <2024Mar9.190957@mips.complang.tuwien.ac.at>
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: news.ausics.net
Message-ID: <65ecf431@news.ausics.net>
Organization: Ausics - https://newsgroups.ausics.net
Lines: 27
X-Complaints: abuse@ausics.net
Path: i2pn2.org!i2pn.org!news.bbs.nz!news.ausics.net!not-for-mail
 by: dxf - Sat, 9 Mar 2024 23:43 UTC

On 10/03/2024 5:09 am, Anton Ertl wrote:
> dxf <dxforth@gmail.com> writes:
>> On 7/03/2024 12:28 am, Ruvim wrote:
>>> ...
>>> In SP-Forth v3 and v4 (they generate native code), "DO" pushes three items on the return stack, and among them the address that "LEAVE" then jumps to. ...
>>
>> That is the classic implementation as suggested by Bob Berkey - inventor of
>> the Forth-83 DO LOOP. IIUC Anton is asking about systems that push the loop
>> address - not the exit address.
>
> Correct.
>
> So, to summarize the answers:
>
> kForth keeps the loop-back address on the return stack in addition to
> index and limit.
>
> A number of systems (at least ciForth, VFX, SP-Forth) have followed
> Bob Berkey's suggestion of keeping the loop-exit address on the return
> stack for LEAVE. That is not what I was asking about, but it's also
> interesting.

The lack of affirmative responses is unsurprising since a native hard-coded
loop is considered 'as good as it gets'. It's difficult to imagine under
what circumstances a loop address on the stack is faster, but it suggests
one is starting from an inefficient or compromised base.

Re: Summary: Forth systems where do/?do pushes that loop start address

<2024Mar12.124115@mips.complang.tuwien.ac.at>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=26375&group=comp.lang.forth#26375

  copy link   Newsgroups: comp.lang.forth
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: anton@mips.complang.tuwien.ac.at (Anton Ertl)
Newsgroups: comp.lang.forth
Subject: Re: Summary: Forth systems where do/?do pushes that loop start address
Date: Tue, 12 Mar 2024 11:41:15 GMT
Organization: Institut fuer Computersprachen, Technische Universitaet Wien
Lines: 81
Message-ID: <2024Mar12.124115@mips.complang.tuwien.ac.at>
References: <2024Mar4.182409@mips.complang.tuwien.ac.at> <us9r2g$eh8t$1@dont-email.me> <65e922ca$1@news.ausics.net> <2024Mar9.190957@mips.complang.tuwien.ac.at> <65ecf431@news.ausics.net>
Injection-Info: dont-email.me; posting-host="a3d96a3e5e8c9d1d65febe5a8b08fbcd";
logging-data="316284"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18v8eyo1hO7Yaq0LAnic0ut"
Cancel-Lock: sha1:ZPVLxljfIApNeYQQ3bzHMk5ktak=
X-newsreader: xrn 10.11
 by: Anton Ertl - Tue, 12 Mar 2024 11:41 UTC

dxf <dxforth@gmail.com> writes:
>On 10/03/2024 5:09 am, Anton Ertl wrote:
>It's difficult to imagine under
>what circumstances a loop address on the stack is faster, but it suggests
>one is starting from an inefficient or compromised base.

The starting point is gforth-fast from June 2023. Here's an example.
The inner loop of the siev benchmark is:

0 i c! dup +loop

The following shows the threaded code intermixed with the native code:

loop-back address in ...
.... threaded code ... return stack
lit 1->2 lit 1->2
#0 #0
mov r15,[r14] mov r15,[r14]
add r14,$10 add r14,$10
i 2->3 i 2->3
mov r9,[rbx] mov r9,[rbx]
add r14,$08 add r14,$08
c! 3->1 c! 3->1
mov [r9],r15lb mov [r9],r15lb
add r14,$08 add r14,$08
dup 1->2 dup 1->2
mov r15,r8 mov r15,r8
add r14,$08 add r14,$08
(+loop) 2->1 (+loop)-rstack 2->1
<PRIMES+$108>
mov rax,[rbx] mov rdx,[rbx]
mov rsi,[r14] mov rsi,$10[rbx]
lea r10,$08[r14] mov rax,rdx
mov rdx,rax sub rax,$08[rbx]
sub rdx,$08[rbx] add rdx,r15
add rax,r15 lea rcx,[r15][rax]
lea rcx,[r15][rdx] xor rcx,rax
xor rcx,rdx xor rax,r15
xor rdx,r15 test rcx,rax
test rcx,rdx js $7F22DC4C075F
js $7F860CE101F1 mov r14,rsi
mov [rbx],rax mov [rbx],rdx
mov rcx,[rsi] add r14,$08
lea r14,$08[rsi] mov rcx,-$08[r14]
jmp ecx jmp ecx

On Zen3 (Ryzen 5800X) and Tiger Lake (Core i5-1135G7) the return stack
variant is faster by a factor >2; we also see speedups on other
processors, but they are smaller. Where do these speedups come from?

If you look at the updates to r14, which contains the virtual-machine
instruction pointer updates, they are as follows:

loop-back address in ...
.... threaded code ... return stack
add r14,$10 add r14,$10
add r14,$08 add r14,$08
add r14,$08 add r14,$08
add r14,$08 add r14,$08
mov rsi,[r14] mov rsi,$10[rbx]
lea r14,$08[rsi] mov r14,rsi
add r14,$08

The crucial difference is that in the left column there is an unbroken
dependence chain from the r14 at the end of the previous iteration to
the r14 at the end of the present iteration; this dependence chain has
a latency of 9 cycles per iteration on Zen3, meaning that, with enough
iterations, the loop takes at least 9 cycles.

In the right column r14 at the end of one iteration does not depend on
r14 at the end of the previous iteration, because the dependence chain
starts from the instruction "mov rsi,$10[rbx]". This means that the
loop can be executed faster and on Zen3 and on Tiger Lake, that
speedup happens to be more than a factor of 2.

- 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 2023: https://euro.theforth.net/2023

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor