
limited test-less loop-less call-full repetition
Quote:
>The idea is very simple, for some types of inner interpreters, and
>not too interesting for others.
Actually, as long as the compiler does not reorder or combine code, it
can be extended to work for any Forth implementation.
Quote:
> You create an array of XTs of a
>single kind, stick a NEXT or suitable returning word at the end of
>it, and then with a calculated BRANCH enter this structure at a
>certain point. Repetition without tests or loops, but lots of
>subroutine calls. Just to fill in this idea, I'll define a couple
>of the words involved, and offer trivial use.
> : XTARY create 0 do dup , loop drop ['] next , ;
The generalization would look like this:
: XTARY ( xt u "name" -- )
>r :
0 ?do
loop
POSTPONE ;
r> drop
;
Of course, computing the jump target address is system-dependent.
Quote:
>Does anyone know of a practical use for such a construct?
Well, this kind of unrolling, but without jumping into the middle, is
used in http://www.complang.tuwien.ac.at/forth/bench.zip (file
forth/mm-rtcg.fs), if you consider a benchmark a practical
application, and also in
http://www.complang.tuwien.ac.at/forth/garbage-collection.zip. Here's
the code:
\ I want to avoid having a DO LOOP in mark at run-time (for time and
\ space reasons), so I unroll it at compile-time using gen-mark. "see
\ mark" to see what is actualy generated.
: gen-mark ( -- ; generated code: addr1 -- addr2 )
\ generates the recursive calls for mark
\ addr2=addr1+grain
grain 0 ?do
1 cells +loop ;
: mark ( x -- )
\ if x is a pointer to a gc chunk, mark it and its descendents
dup mark? if
begin ( x1 )
[ gen-mark ]
endif
drop ;
Quote:
>Has anyone seen one before?
Not sure if that's what you have in mind, but in C there is a
technique called Duff's device.
- anton
--
M. Anton Ertl Some things have to be seen to be believed
http://www.complang.tuwien.ac.at/anton/home.html