Secret of Immediate Words? 
Author Message
 Secret of Immediate Words?

Hey Forthers,

What is the secret of immediate words.  I understand them conceptually, but
how do they actually work?  I was knocking this around in my head today, and
I came away stumped.  The example I was thinking about was IF.

An immediate word is executed at compile time, right?  If this is the case,
how does IF do a look ahead to determine the branch?  Is a word compiled via
a multiple pass?

Thanks for your time,

John



Sun, 10 Apr 2005 06:20:03 GMT  
 Secret of Immediate Words?

Quote:

> Hey Forthers,

> What is the secret of immediate words.  I understand them conceptually, but
> how do they actually work?  I was knocking this around in my head today, and
> I came away stumped.  The example I was thinking about was IF.

> An immediate word is executed at compile time, right?  If this is the case,
> how does IF do a look ahead to determine the branch?  Is a word compiled via
> a multiple pass?

In brief, IF compiles a conditional branch plus a place-marker for
the destination, leaving the addr of the place-marker on the stack at
compile time.  ELSE and THEN expect an address of a place-marker, which
they fill with an appropriate destination.  No look-ahead required.

There are many variation of detail, but that's the general principle.

Cheers,
Elizabeth

--
==================================================
Elizabeth D. Rather   (US & Canada)   800-55-FORTH
FORTH Inc.                         +1 310-491-3356
5155 W. Rosecrans Ave. #1018  Fax: +1 310-978-9454
Hawthorne, CA 90250
http://www.forth.com

"Forth-based products and Services for real-time
applications since 1973."
==================================================



Sun, 10 Apr 2005 06:38:22 GMT  
 Secret of Immediate Words?

Quote:

> Hey Forthers,

> What is the secret of immediate words.  I understand them conceptually, but
> how do they actually work?  I was knocking this around in my head today, and
> I came away stumped.  The example I was thinking about was IF.

> An immediate word is executed at compile time, right?  If this is the case,
> how does IF do a look ahead to determine the branch?  Is a word compiled via
> a multiple pass?

> Thanks for your time,

> John

IF lays down code in the growing definition that effects a conditional
branch to an as yet dummy address, and puts the location of that address
onto the stack. Later, THEN , another immediate word, uses information
about the current location (whatever it happens to be) and the number on
stack to convert the dummy address to a real one. If ELSE happened
between, it is ELSE's dummy that is corrected, and ELSE has already
corrected IF's. THEN doesn't need to know; it works only with HERE and
the top of the stack. One pass does it all.

Jerry
--
Engineering is the art of making what you want from things you can get.



Sun, 10 Apr 2005 07:56:59 GMT  
 Secret of Immediate Words?
Quote:


> > Hey Forthers,

> > What is the secret of immediate words.  I understand them conceptually, but
> > how do they actually work?  I was knocking this around in my head today, and
> > I came away stumped.  The example I was thinking about was IF.

> > An immediate word is executed at compile time, right?  If this is the case,
> > how does IF do a look ahead to determine the branch?  Is a word compiled via
> > a multiple pass?

> In brief, IF compiles a conditional branch plus a place-marker for
> the destination, leaving the addr of the place-marker on the stack at
> compile time.  ELSE and THEN expect an address of a place-marker, which
> they fill with an appropriate destination.  No look-ahead required.

> There are many variation of detail, but that's the general principle.

> Cheers,
> Elizabeth

I am again in awe at how much more simply you put things than I.

Jerry
--
Engineering is the art of making what you want from things you can get.



Sun, 10 Apr 2005 07:59:27 GMT  
 Secret of Immediate Words?
Thanks, that helps.  So immediate words use the stack just like any other
word.  They just do it at a different time.



Quote:

> > Hey Forthers,

> > What is the secret of immediate words.  I understand them conceptually,
but
> > how do they actually work?  I was knocking this around in my head today,
and
> > I came away stumped.  The example I was thinking about was IF.

> > An immediate word is executed at compile time, right?  If this is the
case,
> > how does IF do a look ahead to determine the branch?  Is a word compiled
via
> > a multiple pass?

> In brief, IF compiles a conditional branch plus a place-marker for
> the destination, leaving the addr of the place-marker on the stack at
> compile time.  ELSE and THEN expect an address of a place-marker, which
> they fill with an appropriate destination.  No look-ahead required.

> There are many variation of detail, but that's the general principle.

> Cheers,
> Elizabeth

> --
> ==================================================
> Elizabeth D. Rather   (US & Canada)   800-55-FORTH
> FORTH Inc.                         +1 310-491-3356
> 5155 W. Rosecrans Ave. #1018  Fax: +1 310-978-9454
> Hawthorne, CA 90250
> http://www.forth.com

> "Forth-based products and Services for real-time
> applications since 1973."
> ==================================================



Sun, 10 Apr 2005 08:20:52 GMT  
 Secret of Immediate Words?

Quote:
>>>>> "John" == news verizon net <news.verizon.net> writes:

John> Thanks, that helps.  So immediate words use the stack just like
John> any other word.  They just do it at a different time.

Yup. Conceptually, they use a "compilation stack", which in most
implementations happen to be the regular data stack used at
compilation time.

  Sam
--



Sun, 10 Apr 2005 17:35:06 GMT  
 Secret of Immediate Words?
That's a possibly misleading way to look at it.

Immediate words are words that execute at compile time, rather
than being included in the definition being compiled.

Similar to defining words, it has instructions it executes when it
is encountered, which lay down instructions that will be executed when
the word which was being compiled is executed.

When the word is encountered, it encodes the equivalent "Drop top of stack and
jump if zero" into the definition, then effectively saves the current address
being compiled (HERE) as puts in a placeholder for the destination
address.   The  THEN or ELSE immediate word will pop the address of
that jump instruction and will put the then-current compilation address (HERE)
into the jump instruction. (ELSE will also, prior to that, put a "jump" instruction
and placeholder save the current address for the subsequent THEN).  An
interesting thing to notice here is that THEN doesn't actually generate any
new code, it just fixes the jump address of the IF or ELSE.

When the word that was defined is executed, when it gets to the code
the IF put in, it will pop the stack and if it was zero, jump to the address
where the ELSE or THEN was executed, otherwise it just continues.

The first explanation that "did it" for me was in the TIL book.

Rufus

P.S. It can sure be hard to break through to find the simplicity and
elegance of FORTH, so I know where you're coming from.


Quote:
> Thanks, that helps.  So immediate words use the stack just like any other
> word.  They just do it at a different time.




> > > Hey Forthers,

> > > What is the secret of immediate words.  I understand them conceptually,
> but
> > > how do they actually work?  I was knocking this around in my head today,
> and
> > > I came away stumped.  The example I was thinking about was IF.

> > > An immediate word is executed at compile time, right?  If this is the
> case,
> > > how does IF do a look ahead to determine the branch?  Is a word compiled
> via
> > > a multiple pass?

> > In brief, IF compiles a conditional branch plus a place-marker for
> > the destination, leaving the addr of the place-marker on the stack at
> > compile time.  ELSE and THEN expect an address of a place-marker, which
> > they fill with an appropriate destination.  No look-ahead required.

> > There are many variation of detail, but that's the general principle.

> > Cheers,
> > Elizabeth

> > --
> > ==================================================
> > Elizabeth D. Rather   (US & Canada)   800-55-FORTH
> > FORTH Inc.                         +1 310-491-3356
> > 5155 W. Rosecrans Ave. #1018  Fax: +1 310-978-9454
> > Hawthorne, CA 90250
> > http://www.forth.com

> > "Forth-based products and Services for real-time
> > applications since 1973."
> > ==================================================



Sun, 10 Apr 2005 23:18:34 GMT  
 Secret of Immediate Words?
Quote:

> Thanks, that helps.  So immediate words use the stack just like any other
> word.  They just do it at a different time.

[snip]

They use the stack it the stack is appropriate to what they do. Here are
some words that add compatibility with ANS to an older Forth:


: ASCII   CHAR [COMPILE] LITERAL ; IMMEDIATE \ Compile only
: [CHAR]  [COMPILE] ASCII ; IMMEDIATE \ ANS compatibility

CHAR is an ordinary word.

WORD scans the input stream for a space ( BL ) delimited entity and
leaves it in a temporary buffer as a counted string, putting the address

right after the count -- the first one in the string. The effect is to
put the ASCII code for the first character in a space-delimited sequence
on the stack and to discard subsequent ones.

LITERAL is an immediate word that takes an item from the stack and puts
it into the dictionary, along with code to put it back at run time. Used
sensibly, it adds to a growing definition.

[COMPILE] is an override that causes a following immediate word to be
compiled instead of executed.

ASCII is from older Forths. It puts the ASCII code for a character into
a definition. It lets me write ' ASCII - ' instead of ' 45 ' in a
definition. It's not that I have no ASCII table; it clarifies the intent
of the code. A proper definition if ASCII works in compile and in
interactive mode; mine doesn't. The ANS name for my ASCII is [CHAR] .

Jerry

P.S. A proper ASCII could be built this way (assuming that interactive
input always comes from the keyboard):

: ASCII  ( -- symbol code if not compiling)

             ELSE KEY
             THEN ; IMMEDIATE

An immediate word executes even in compile mode, but it can execute any
time, so this ASCII is OK. For example, LITERAL does its work any time:

SwiftForth 2.2.2 09Mar2001
here . 4632424  ok
22 literal  ok
here . 4632435  ok

The "22", and the code to push it onto the stack are now orphans in the
dictionary. "Orphans" because there is no way for them to run. Like
greedy DNA, they take up room without other effect.

P.P.S In ANS Forth, [COMPILE] is replaced by POSTPONE.
--
Engineering is the art of making what you want from things you can get.



Mon, 11 Apr 2005 00:21:54 GMT  
 Secret of Immediate Words?
On Wed, 23 Oct 2002 00:20:52 GMT

Quote:

> Thanks, that helps.  So immediate words use the stack just like any other
> word.  They just do it at a different time.

                                ^^^^^^^^^^^^

immediate words, which weren't restricted, explicitely, can at any time be
executed.

an immediate words differs from others only by the fact that it is immediately
executing, regardless of the rsp. 4th system being in compiling or, in
interpreting (interactive) state. 'immediacy' by itself has no other effect.

any word can be declared immediate.

an immediate word which (in a system dependent manner) stores forth code into
the dictionary is a (mini-)compiler.

ans-4th 'POSTPONE' can suppress the 'immediacy' of the next word in the
input stream such that it can be compiled as other words, while in compiling
state. e.g. in a 'colon definition'.

other standards provide '[COMPILE]' for that purpose.

regards,
        hp

--
   >>> pse, reply to : clf -at- lxhp -dot- in-berlin -dot- de <<<
Linux,Assembly,Forth: http://www.lxhp.in-berlin.de/index-lx.shtml en/de



Mon, 11 Apr 2005 00:40:13 GMT  
 Secret of Immediate Words?

Quote:

> Hey Forthers,

> What is the secret of immediate words.  I understand them conceptually, but
> how do they actually work?  I was knocking this around in my head today, and
> I came away stumped.  The example I was thinking about was IF.

> An immediate word is executed at compile time, right?  If this is the case,
> how does IF do a look ahead to determine the branch?  Is a word compiled via
> a multiple pass?

> Thanks for your time,

> John

Here is an example of an IMMEDIATE word that is so familiar we rarely think
of it in that context. But it shows clearly why immediacy is vital to the
Forth compilation mechanism. We begin a new definition with a colon, ":" .
Colon _is_ the compiler (or anyway, one of them!) in Forth. Colon is EXECUTEd,
and it performs the following actions:

        1. Create a new dictionary header whose name is the first piece
           of text following ":" , as in the definition

                : *+    *  +   ;

           In the above example the new name is "*+" .

        2. Switch the system from interpret- to compile mode. Now each
           subsequent piece of text (e.g. "*" and "+" above) is read,
           looked up in the dictionary, and (if found) compiled rather
           than EXECUTEd (because the system is in compile mode). If
           the text is not found in the dictionary, the system tries to
           convert it to a number in the current base. If this is pos-
           sible, the number is entered into the code as a LITERAL.
           (And, BTW, LITERAL is an IMMEDIATE word!) If the text is
           neither an existing word nor a number, the system ABORTs
           with an error message.

        3. So now what? Barring an error, does the system keep on
           compiling everything you enter after the colon and the
           name? Help! How can you halt the Sorcerer's Apprentice?

           The answer, of course, is the semicolon ";" that termin-
           ates a definition. It is IMMEDIATE, so instead of being
           compiled, it is EXECUTEd. The task of ";" is to install
           appropriate code for popping the return stack and trans-
           ferring control to the next word in sequence (or whatever
           the particular Forth's word-terminating procedure is).
           Then it switches the system back to interpret mode.

So you see, being able to mark a word IMMEDIATE (usually IMMEDIATE
sets a bit or flag in the definition that has just finished) is vital to
being able to exit the compiler.

Since an IMMEDIATE word simply has a bit/flag set, you can see how it works:
when the system is interpreting, it ignores this flag. But if it is compiling,
the system checks this flag--if it is set, the word's "execution token" is
passed to the execution mechanism which EXECUTEs it just as though the system
were in interpretation mode; whereas if the flag is not set, the execution
token is simply compiled into the new definition under construction.

Finally, this behavior can be modified (an IMMEDIATE word can be compiled,
e.g.) by words like POSTPONE and [COMPILE]. See the widely available dpANS6
for details.

You will also find a description of compilation, with diagrams, in the latest
version of "A Beginner's Guide to Forth (HTML)" at

        http://www.*-*-*.com/ .{*filter*}ia.edu/classes/551.jvn.fall01/

--
Julian V. Noble
Professor of Physics

   "Science knows only one commandment: contribute to science."
   -- Bertolt Brecht, "Galileo".



Mon, 11 Apr 2005 03:35:33 GMT  
 Secret of Immediate Words?

Quote:

> Thanks, that helps.  So immediate words use the stack just like any other
> word.  They just do it at a different time.

At the risk of confusing you even further, someone once pointed out that you
can implement all the necessary looping and branching features with two
constructs. These are:-

- making every compiled word default to behaving as an infinite loop;

- a special conditional return to break out of any of these at any point in
it.

These don't actually need to know addresses at compile time, since there is
enough for them to work with on the return stack at run time.

The main thing is, you no longer need immediate execution of anything within
compilation, so - and getting even further away from mainstream Forth - you
can drop the : and ; behaviour and just have a way of compiling strings. So
you can eliminate immediate keywords and compilation mode, and not just by
faking their behaviour with CREATE and directly storing the right addresses
in memory. I don't think it's worth it, but it shows what you can do if you
try. PML.

--
GST+NPT=JOBS

I.e., a Goods and Services Tax (or almost any other broad based production
tax), with a Negative Payroll Tax, promotes employment.

See http://users.netlink.com.au/~peterl/publicns.html#AFRLET2 and the other
items on that page for some reasons why.



Mon, 11 Apr 2005 18:15:23 GMT  
 Secret of Immediate Words?

Quote:
> Similar to defining words, it has instructions it executes when it
> is encountered, which lay down instructions that will be executed when
> the word which was being compiled is executed.

So it is a pre interpreter for Forth words.  Then immediate words would have
to be marked a certain way for the compiler to distinguish them.  A
different dictionary?

Quote:
> When the word is encountered, it encodes the equivalent "Drop top of stack
and
> jump if zero" into the definition, then effectively saves the current
address
> being compiled (HERE) as puts in a placeholder for the destination
> address.   The  THEN or ELSE immediate word will pop the address of
> that jump instruction and will put the then-current compilation address
(HERE)
> into the jump instruction. (ELSE will also, prior to that, put a "jump"
instruction
> and placeholder save the current address for the subsequent THEN).  An
> interesting thing to notice here is that THEN doesn't actually generate
any
> new code, it just fixes the jump address of the IF or ELSE.

The main reason I asked this question, is that I was working on a utility
that had to be very flexible.  While it takes XML files as input, I needed a
fast way to execute the processing that the files generated.  I designed a
simple little threaded interpreter that worked great until I discovered that
I needed control flow.

What I did is built in two additional words into my interpreters instruction
set called `branch' and `branchif'.  When the compiler encountered `if', it
loaded the instruction for `branchif' instead and pushed the next memory
location onto the data stack.  When a `then' was encountered it loaded a
'branch' instruction, poped the address that `branch' would use to jump past
the if block, and then poped the postition directly after the `branch' onto
the stack for `eslse' to load the address for if to jump past the then
block.  I think that an example would explain this better.

0 if 2 2 + then 3 3 + else 5 +

would be compiled into

0 branchif 9 2 2 + branch 12 3 3 + 5 +

I made brachif and branch advance the instruction pointer when the where
encountered so I didn't need any variable assignment.

Quote:
> When the word that was defined is executed, when it gets to the code
> the IF put in, it will pop the stack and if it was zero, jump to the
address
> where the ELSE or THEN was executed, otherwise it just continues.

I was pretty close...except for the fact that I hard coded the immediate
words into the interpreter.

Quote:
> The first explanation that "did it" for me was in the TIL book.

I'll have to look for a copy.

Quote:
> Rufus

> P.S. It can sure be hard to break through to find the simplicity and
> elegance of FORTH, so I know where you're coming from.

Thanks!


Tue, 12 Apr 2005 11:05:10 GMT  
 
 [ 25 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Constants: immediate words?

2. can CREATEd words be IMMEDIATE?

3. test words immediate-mode in Sparc monitor ?

4. Poll: compiling an immediate word

5. Poll: compiling an immediate word

6. How to replace one or two words with one word with one line of awk code

7. unsigned word to signed word in RB

8. Word 97 and word 2000 - calling from clarion - HELP

9. Word by word cross reference

10. More MF.Fun WORD-RIGHT and WORD-LEFT

11. Class(y) 2.4b, with documentation for MS-Word 97, MS-Word 6.0 and WordPerfect 7.0

12. My final word on word size

 

 
Powered by phpBB® Forum Software