[comp.lang.forth] Unstable references

wsbusup@eutws1.win.tue.nl (Jan Stout) (09/28/90)

I'll define an unstable reference as a reference to a storage-area which
contents can possibly be changed/moved by the sytem.
If i.e. the following code was executed,

SP@ CELL- CONSTANT UNSTABLE
5 UNSTABLE !   UNSTABLE @ .

the result would prob. not be a 5 appearing. The reason for this is that
the stackspace that is referenced is used by the system.

In the '83 Standard a word created by a highlevel defining word that uses
DOES>, returns the address of it's body (pfa) upon execution.  
A defining word like VARIABLE can be defined as:

: VARIABLE   CREATE  2 ALLOT  DOES> ;

The unstability problem now arises when something like the following occurs:

VARIABLE BANANAS
VARIABLE BASKET
5 BANANAS !
BANANAS BASKET !
   ...
BASKET @ @ .

In a system where a heap is used to contain the bodies of forth words, 
[See `Object Oriented Forth' '87 by Dick Pountain p. 133] a 5 may well not
appear! The reason for this is that in such a system a reference to
BANANAS' pfa is unstable. In this case an easy solution would be to change
the last three lines to

' BANANAS BASKET !
   ...
BASKET @ >BODY @ .

assuming that references to addresses stored in a definitions body (cfa's) are
stable, which of course they must be in ANY system.

If we consider the following case however

: VOCABULARY   CREATE  2 ALLOT  DOES>  CONTEXT ! ;
VOCABULARY FRUIT
   ...
' FRUIT >BODY  CONTEXT @  =

the result will prob. NOT be a -1, even if we ensure that no other vocabularies
place their pfa in CONTEXT during the ...
In this case the solution  would be 

: VOCABULARY   CREATE  2ALLOT  DOES>  BODY>  CONTEXT ! ;

implying each body must have access to the location of it's head.

A more efficient approach, consistent with the philosophy of a single address
to define a word, is that of moving from DOES> to DOES, where a word defined
with the DOES clause put's it's cfa on stack, NOT it's pfa.
Note that this approach is not a plea for implementors to pick up `Heapforth',
but just a try to make the Standard more portable by not ignoring the
possibility of a `Heapforth'.
The Standard could specify something along the lines of:

A Standard Program may not use unstable references in storageareas other than
the stack (implying local use only).

The : VARIABLE   CREATE  2 ALLOT  DOES  >BODY ; would be allowable but the use
of a pfa thus obtained should be restricted to @ing or !ing upon that address,
(not !ing the pfa itself!).