In article <
[email protected]>,
Anton Ertl <
[email protected]> wrote:
Brad Eckert <[email protected]> writes:
I notice that the GreenArrays chips have an opcode called "exec" that swaps the P and R registers.
So in more traditional terms I guess this is:
: exec r> r> swap >r >r ;
This is an inexpensive code word in 80i86
CODE_HEADER({CO},{CO})
XCHG HIP,[RPO]
_NEXT
HIP is mapped onto SI, RPO is mapped onto BP
High lever Interpreter Pointer, Return stack POinter.
(In this implementation in ciforth).
The name CO (coroutine) is way better that EXEC.
Are there other tricks that "exec" can do besides slightly improving
"execute"?
A number of people have used return-address-manipulating words for >implementing control-flow in some form. E.g., I remember Bernd
Paysan's LIST>, which is used like this:
: foo ... ( list ) list> code ;
where CODE is executed once for every element of the list, and that
element is pushed before every time. IIRC he also had a word that
changed the standard output device to stderr, and when the colon
definition ended, the standard output device was changed back. These
days we instead pass an xt (e.g., of a quotation) to a word that
executes the xt: once per stack element in case of the LIST>
replacement, or that changes the user output device, then executes the
xt, then changes it back afterwards.
So while return-address manipulation has a certain cuteness factor, it
can be replaced with standard code, and I guess that's why the
suggestions to standardize it never led to a Forth 200x proposal (I
remember something that was close to what one would expect in such a >proposal, but I think that was before Forth 200x started).
Many never appreciated that it is in fact a coroutine call.
So abstracts away any return address manipulation.
\ : CO R> R> SWAP >R >R ; if missing.
\ For WID, yield a next DEA (namefield) for each coroutine call,
\ then zero.
[ ">LFA @" turns a name token into the next name token. ]
: FOR-WORDS BEGIN DUP CO >LFA @ DUP 0= UNTIL ;
\ Print all words in the context dictionary.
: WORDS CONTEXT @ FOR-WORDS BEGIN DUP WHILE ID. CO REPEAT DROP ;
It is bang for the buck, adding CO is merely one code word with
one instruction.
You could have passed `ID. to a looping construct
but then you have to store the xt temporarily. And what if you
want to put a carriage return after each ID. ?
This is the description in the glossary of ciforth: -----------------------------------------------------------
CO
STACKEFFECT:
DESCRIPTION:
Return to the caller, suspending interpretation of the current
definition, such that when the caller exits, this definition is
resumed.
The return stack must not be engaged, such as between >R and R> , or
DO and LOOP .
GLOSSARY INDEX
SEE ALSO: CONTROL EXIT -----------------------------------------------------------
Note how the description is concise, compelling and clear.
- anton
Groetjes Albert
--
"in our communism country Viet Nam, people are forced to be
alive and in the western country like US, people are free to
die from Covid 19 lol" duc ha
albert@spe&ar&c.xs4all.nl &=n
http://home.hccnet.nl/a.w.m.van.der.horst
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)