• Uses for "exec" (swap P and R)

    From Brad Eckert@21:1/5 to All on Wed Sep 28 11:17:12 2022
    I notice that the GreenArrays chips have an opcode called "exec" that swaps the P and R registers. I suppose this is for "execute", which would be a "push exec" macro. One could just as easily call a word "execute" whose definition is

    : execute push ;

    Are there other tricks that "exec" can do besides slightly improving "execute"?

    --
    Brad

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Anton Ertl@21:1/5 to Brad Eckert on Wed Sep 28 18:47:17 2022
    Brad Eckert <hwfwguy@gmail.com> 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 ;

    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).

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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From none) (albert@21:1/5 to Anton Ertl on Thu Sep 29 10:49:02 2022
    In article <2022Sep28.204717@mips.complang.tuwien.ac.at>,
    Anton Ertl <anton@mips.complang.tuwien.ac.at> wrote:
    Brad Eckert <hwfwguy@gmail.com> 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)
  • From none) (albert@21:1/5 to hwfwguy@gmail.com on Thu Sep 29 11:42:39 2022
    In article <8d85f5cc-62b5-436c-b594-5ba0a6d68fc6n@googlegroups.com>,
    Brad Eckert <hwfwguy@gmail.com> wrote:
    I notice that the GreenArrays chips have an opcode called "exec" that
    swaps the P and R registers. I suppose this is for "execute", which
    would be a "push exec" macro. One could just as easily call a word
    "execute" whose definition is

    : execute push ;

    Are there other tricks that "exec" can do besides slightly improving "execute"?

    Once you realize that it implements the well known concept of
    coroutines a flurry of uses arises; it is a respectable technique
    rather than a trick.
    A better name is CO.
    "
    Return to the caller, suspending interpretation of the current
    definition, such that when the caller exits, this definition is
    resumed.
    "
    A few examples:

    VARIABLE (BASE)
    \ Switch to hex for the duration of the definition.
    : HEX: BASE @ (BASE) ! HEX CO (BASE) @ BASE ! ;
    \ Some care is needed if you want to use the return stack.
    : HEX: R> BASE @ >R >R HEX CO R> BASE ! ;
    : DEC: R> BASE @ >R >R DECIMAL CO R> BASE ! ;

    \ For WID, yield a next DEA (namefield) for each coroutine call,
    \ then zero.
    : 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 comes indispensable once you have added decorators to your
    Forth. (An idea I took from Python).
    It is best shown by example:

    OK : add + ;
    OK : before "before the stack is " TYPE .S ;
    OK
    'before 'add decorated \ `add is decorated by `before .
    OK
    1 2 add
    before the stack is
    S[ 1 2 ] OK

    : before&after "before the stack is " TYPE .S CO
    " after the stack is " TYPE .S ;
    OK
    'add undecorated
    OK
    'before&after 'add decorated
    OK
    1000 100 add
    before the stack is
    S[ 3 1000 100 ] after the stack is
    S[ 3 1100 ] OK

    --
    Brad

    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)