[comp.lang.forth] BLK - read-only?

wmb@MITCH.ENG.SUN.COM (Mitch Bradley) (07/01/90)

Ray Duncan, replying to Robert Berkey:

RD> it's BLK that is
RD> read-only.  Your cycle counts are meaningless because I was not
RD> referring to the action of loading BLK itself but the stuff that must
RD> depend on it in systems where BLK is changeable ---
RD>
RD>    BLK @ ?DUP IF BLOCK ... ELSE TIB #TIB @ ...

I got the impression that Robert was suggesting something slightly
different.  If the system "remembers" the value that was in BLK the
last time that WORD was called, then the system can quickly detect when
BLK has been changed, using something like the assembly code Robert
showed.

Here's how it might work in high level:

VARIABLE INPUT-BUFFER		\ Address of current input stream buffer
VARIABLE INPUT-BUFFER-SIZE	\ Size of that buffer
VARIABLE SAVED-BLK		\ Block to which INPUT-BUFFER corresponds

: SOURCE  ( -- adr len )

   SAVED-BLK @  BLK @  =  IF	\ See if BLK has changed "behind our back"

      \ Input source hasn't moved; use remembered location

      INPUT-BUFFER @  INPUT-BUFFER-SIZE @                  ( adr len )

   ELSE
      \ BLK has changed; remembered location is invalid

      BLK @ SAVED-BLK !		\ Update SAVED-BLK to its new value

      \ Recalculate input location
      BLK @  ?DUP  IF  BLOCK 1024  ELSE  TIB #TIB @  THEN  ( adr len )

      \ Remember new location of input stream
      2DUP INPUT-BUFFER-SIZE !  INPUT-BUFFER !             ( adr len )

   THEN
;

I suspect that LMI's "2EQU SOURCE" is rather similar in spirit to
"INPUT-BUFFER @  INPUT-BUFFER-SIZE @" as shown above.

The above may be interpreted as a one-entry "cache" of the input stream
location, with the SAVED-BLK variable as the "cache tag" validating it.

The initial "cache hit test" "SAVED-BLK @  BLK @  =  IF" can be implemented
easily and efficiently in assembly language as Robert suggested.

For this to work right, the block buffer corresponding to SAVED-BLK
needs to be "locked" into memory so that it isn't replaced by another
call to BLOCK .  This could be accomodated easily in the algorithm
that selects the block buffer to replace (just arrange for it to choose
some buffer that doesn't correspond to SAVED-BLK).

(OTOH, I personally haven't used BLOCKs since 1983, and good riddance!)

Mitch