Diff for /gforth/glocals.fs between versions 1.2 and 1.69

version 1.2, 1994/06/01 10:05:17 version 1.69, 2012/06/27 20:49:34
Line 1 Line 1
   \ A powerful locals implementation
   
   \ Copyright (C) 1995,1996,1997,1998,2000,2003,2004,2005,2007,2011 Free Software Foundation, Inc.
   
   \ This file is part of Gforth.
   
   \ Gforth is free software; you can redistribute it and/or
   \ modify it under the terms of the GNU General Public License
   \ as published by the Free Software Foundation, either version 3
   \ of the License, or (at your option) any later version.
   
   \ This program is distributed in the hope that it will be useful,
   \ but WITHOUT ANY WARRANTY; without even the implied warranty of
   \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   \ GNU General Public License for more details.
   
   \ You should have received a copy of the GNU General Public License
   \ along with this program. If not, see http://www.gnu.org/licenses/.
   
   
   \ More documentation can be found in the manual and in
   \ http://www.complang.tuwien.ac.at/papers/ertl94l.ps.gz
   
 \ Local variables are quite important for writing readable programs, but  \ Local variables are quite important for writing readable programs, but
 \ IMO (anton) they are the worst part of the standard. There they are very  \ IMO (anton) they are the worst part of the standard. There they are very
 \ restricted and have an ugly interface.  \ restricted and have an ugly interface.
Line 56 Line 79
 \ aligned correctly, but our locals stack must be float-aligned between  \ aligned correctly, but our locals stack must be float-aligned between
 \ words.  \ words.
   
 \ Other things about the internals are pretty unclear now.  require search.fs
   require float.fs
 \ Currently locals may only be  require extend.fs \ for case
 \ defined at the outer level and TO is not supported.  
   : compile-@local ( n -- ) \ gforth compile-fetch-local
 include float.fs   case
 include search-order.fs      0       of postpone @local0 endof
       1 cells of postpone @local1 endof
       2 cells of postpone @local2 endof
       3 cells of postpone @local3 endof
      ( otherwise ) dup postpone @local# ,
    endcase ;
   
   : compile-f@local ( n -- ) \ gforth compile-f-fetch-local
    case
       0        of postpone f@local0 endof
       1 floats of postpone f@local1 endof
      ( otherwise ) dup postpone f@local# ,
    endcase ;
   
   \ locals stuff needed for control structures
   
   : compile-lp+! ( n -- ) \ gforth        compile-l-p-plus-store
       dup negate locals-size +!
       0 over = if
       else -1 cells  over = if postpone lp-
       else  1 floats over = if postpone lp+
       else  2 floats over = if postpone lp+2
       else postpone lp+!# dup ,
       then then then then drop ;
   
   : adjust-locals-size ( n -- ) \ gforth
       \ sets locals-size to n and generates an appropriate lp+!
       locals-size @ swap - compile-lp+! ;
   
 \ the locals stack grows downwards (see primitives)  \ the locals stack grows downwards (see primitives)
 \ of the local variables of a group (in braces) the leftmost is on top,  \ of the local variables of a group (in braces) the leftmost is on top,
Line 71  include search-order.fs Line 121  include search-order.fs
 \ lp must have the strictest alignment (usually float) across calls;  \ lp must have the strictest alignment (usually float) across calls;
 \ for simplicity we align it strictly for every group.  \ for simplicity we align it strictly for every group.
   
   slowvoc @
   slowvoc on \ we want a linked list for the vocabulary locals
 vocabulary locals \ this contains the local variables  vocabulary locals \ this contains the local variables
 ' locals >body Constant locals-list \ acts like a variable that contains  ' locals >body wordlist-id ' locals-list >body !
                                     \ a linear list of locals names  slowvoc !
 : locals-list! ( list -- )  locals-list ! locals-list rehash ;  
   
 create locals-buffer 1000 allot \ !! limited and unsafe  
     \ here the names of the local variables are stored  
     \ we would have problems storing them at the normal dp  
   
 variable locals-dp \ so here's the special dp for locals.  variable locals-mem-list \ linked list of all locals name memory in
   0 locals-mem-list !      \ the current (outer-level) definition
   
   : free-list ( addr -- )
       \ free all members of a linked list (link field is first)
       begin
           dup while
               dup @ swap free throw
       repeat
       drop ;
   
   : prepend-list ( addr1 addr2 -- )
       \ addr1 is the address of a list element, addr2 is the address of
       \ the cell containing the address of the first list element
       2dup @ swap ! \ store link to next element
       ! ; \ store pointer to new first element
   
 : alignlp-w ( n1 -- n2 )  : alignlp-w ( n1 -- n2 )
     \ cell-align size and generate the corresponding code for aligning lp      \ cell-align size and generate the corresponding code for aligning lp
     dup aligned tuck - compile-lp+!# ;      aligned dup adjust-locals-size ;
   
 : alignlp-f ( n1 -- n2 )  : alignlp-f ( n1 -- n2 )
     dup faligned tuck - compile-lp+!# ;      faligned dup adjust-locals-size ;
   
 \ a local declaration group (the braces stuff) is compiled by calling  \ a local declaration group (the braces stuff) is compiled by calling
 \ the appropriate compile-pushlocal for the locals, starting with the  \ the appropriate compile-pushlocal for the locals, starting with the
Line 101  variable locals-dp \ so here's the speci Line 163  variable locals-dp \ so here's the speci
     swap !      swap !
     postpone >l ;      postpone >l ;
   
   \ locals list operations
   
   : list-length ( list -- u )
       0 swap begin ( u1 list1 )
           dup while
               @ swap 1+ swap
       repeat
       drop ;
   
   : /list ( list1 u -- list2 )
       \ list2 is list1 with the first u elements removed
       0 ?do
           @
       loop ;
   
   : common-list ( list1 list2 -- list3 )
       \ list3 is the largest common tail of both lists.
       over list-length over list-length - dup 0< if
           negate >r swap r>
       then ( long short u )
       rot swap /list begin ( list3 list4 )
           2dup u<> while
               @ swap @
       repeat
       drop ;
   
   : sub-list? ( list1 list2 -- f )
       \ true iff list1 is a sublist of list2
       over list-length over list-length swap - 0 max /list = ;
   
   \ : ocommon-list ( list1 list2 -- list3 ) \ gforth-internal
   \ \ list1 and list2 are lists, where the heads are at higher addresses than
   \ \ the tail. list3 is the largest sublist of both lists.
   \  begin
   \    2dup u<>
   \  while
   \    2dup u>
   \    if
   \      swap
   \    then
   \    @
   \  repeat
   \  drop ;
   
   \ : osub-list? ( list1 list2 -- f ) \ gforth-internal
   \ \ true iff list1 is a sublist of list2
   \  begin
   \    2dup u<
   \  while
   \    @
   \  repeat
   \  = ;
   
   \ defer common-list
   \ defer sub-list?
   
   \ ' ocommon-list is common-list
   \ ' osub-list?   is sub-list?
   
   : list-size ( list -- u ) \ gforth-internal
       \ size of the locals frame represented by list
       0 ( list n )
       begin
           over 0<>
       while
           over
           ((name>)) >body @ max
           swap @ swap ( get next )
       repeat
       faligned nip ;
   
   : set-locals-size-list ( list -- )
       dup locals-list !
       list-size locals-size ! ;
   
   : check-begin ( list -- )
   \ warn if list is not a sublist of locals-list
    locals-list @ sub-list? 0= if
      \ !! print current position
        >stderr ." compiler was overly optimistic about locals at a BEGIN" cr
      \ !! print assumption and reality
    then ;
   
 : compile-pushlocal-f ( a-addr -- ) ( run-time: f -- )  : compile-pushlocal-f ( a-addr -- ) ( run-time: f -- )
     locals-size @ alignlp-f float+ dup locals-size !      locals-size @ alignlp-f float+ dup locals-size !
     swap !      swap !
Line 112  variable locals-dp \ so here's the speci Line 257  variable locals-dp \ so here's the speci
     postpone swap postpone >l postpone >l ;      postpone swap postpone >l postpone >l ;
   
 : compile-pushlocal-c ( a-addr -- ) ( run-time: w -- )  : compile-pushlocal-c ( a-addr -- ) ( run-time: w -- )
     -1 chars compile-lp+!#      -1 chars compile-lp+!
     locals-size @ swap !      locals-size @ swap !
     postpone lp@ postpone c! ;      postpone lp@ postpone c! ;
   
 : create-local ( " name" -- a-addr )  7 cells 32 + constant locals-name-size \ 32-char name + fields + wiggle room
         \ defines the local "name"; the offset of the local shall be stored in a-addr  
   : create-local1 ( "name" -- a-addr )
     create      create
         immediate      immediate restrict
         here 0 , ( place for the offset ) ;      here 0 , ( place for the offset ) ;
   
   variable dict-execute-dp \ the special dp for DICT-EXECUTE
   
   0 value dict-execute-ude \ USABLE-DICTIONARY-END during DICT-EXECUTE
   
   : dict-execute1 ( ... addr1 addr2 xt -- ... )
       \ execute xt with HERE set to addr1 and USABLE-DICTIONARY-END set to addr2
       dict-execute-dp @ dp 2>r
       dict-execute-ude ['] usable-dictionary-end defer@ 2>r
       swap to dict-execute-ude
       ['] dict-execute-ude is usable-dictionary-end
       swap to dict-execute-dp
       dict-execute-dp dpp !
       catch
       2r> is usable-dictionary-end to dict-execute-ude
       2r> dpp ! dict-execute-dp !
       throw ;
   
   defer dict-execute ( ... addr1 addr2 xt -- ... )
   
   :noname ( ... addr1 addr2 xt -- ... )
       \ first have a dummy routine, for SOME-CLOCAL etc. below
       nip nip execute ;
   is dict-execute
   
   : create-local ( " name" -- a-addr )
       \ defines the local "name"; the offset of the local shall be
       \ stored in a-addr
       locals-name-size allocate throw
       dup locals-mem-list prepend-list
       locals-name-size cell /string over + ['] create-local1 dict-execute ;
   
   variable locals-dp \ so here's the special dp for locals.
   
   : lp-offset ( n1 -- n2 )
   \ converts the offset from the frame start to an offset from lp and
   \ i.e., the address of the local is lp+locals_size-offset
     locals-size @ swap - ;
   
 : lp-offset, ( n -- )  : lp-offset, ( n -- )
 \ converts the offset from the frame start to an offset from lp and  \ converts the offset from the frame start to an offset from lp and
 \ adds it as inline argument to a preceding locals primitive  \ adds it as inline argument to a preceding locals primitive
 \ i.e., the address of the local is lp+locals_size-offset    lp-offset , ;
   locals-size @ swap - , ;  
   
 vocabulary locals-types \ this contains all the type specifyers, -- and }  vocabulary locals-types \ this contains all the type specifyers, -- and }
 locals-types definitions  locals-types definitions
   
 : W:  : W: ( "name" -- a-addr xt ) \ gforth w-colon
     create-local ( "name" -- a-addr xt )      create-local
         \ xt produces the appropriate locals pushing code when executed          \ xt produces the appropriate locals pushing code when executed
         ['] compile-pushlocal-w          ['] compile-pushlocal-w
     does> ( Compilation: -- ) ( Run-time: -- w )      does> ( Compilation: -- ) ( Run-time: -- w )
         \ compiles a local variable access          \ compiles a local variable access
         postpone @local# @ lp-offset, ;          @ lp-offset compile-@local ;
   
 : W^  : W^ ( "name" -- a-addr xt ) \ gforth w-caret
     create-local ( "name" -- a-addr xt )      create-local
         ['] compile-pushlocal-w          ['] compile-pushlocal-w
     does> ( Compilation: -- ) ( Run-time: -- w )      does> ( Compilation: -- ) ( Run-time: -- w )
         postpone laddr# @ lp-offset, ;          postpone laddr# @ lp-offset, ;
   
 : F:  : F: ( "name" -- a-addr xt ) \ gforth f-colon
     create-local ( "name" -- a-addr xt )      create-local
         ['] compile-pushlocal-f          ['] compile-pushlocal-f
     does> ( Compilation: -- ) ( Run-time: -- w )      does> ( Compilation: -- ) ( Run-time: -- w )
         postpone f@local# @ lp-offset, ;          @ lp-offset compile-f@local ;
   
 : F^  : F^ ( "name" -- a-addr xt ) \ gforth f-caret
     create-local ( "name" -- a-addr xt )      create-local
         ['] compile-pushlocal-f          ['] compile-pushlocal-f
     does> ( Compilation: -- ) ( Run-time: -- w )      does> ( Compilation: -- ) ( Run-time: -- w )
         postpone laddr# @ lp-offset, ;          postpone laddr# @ lp-offset, ;
   
 : D:  : D: ( "name" -- a-addr xt ) \ gforth d-colon
     create-local ( "name" -- a-addr xt )      create-local
         ['] compile-pushlocal-d          ['] compile-pushlocal-d
     does> ( Compilation: -- ) ( Run-time: -- w )      does> ( Compilation: -- ) ( Run-time: -- w )
         postpone laddr# @ lp-offset, postpone 2@ ;          postpone laddr# @ lp-offset, postpone 2@ ;
   
 : D^  : D^ ( "name" -- a-addr xt ) \ gforth d-caret
     create-local ( "name" -- a-addr xt )      create-local
         ['] compile-pushlocal-d          ['] compile-pushlocal-d
     does> ( Compilation: -- ) ( Run-time: -- w )      does> ( Compilation: -- ) ( Run-time: -- w )
         postpone laddr# @ lp-offset, ;          postpone laddr# @ lp-offset, ;
   
 : C:  : C: ( "name" -- a-addr xt ) \ gforth c-colon
     create-local ( "name" -- a-addr xt )      create-local
         ['] compile-pushlocal-c          ['] compile-pushlocal-c
     does> ( Compilation: -- ) ( Run-time: -- w )      does> ( Compilation: -- ) ( Run-time: -- w )
         postpone laddr# @ lp-offset, postpone c@ ;          postpone laddr# @ lp-offset, postpone c@ ;
   
 : C^  : C^ ( "name" -- a-addr xt ) \ gforth c-caret
     create-local ( "name" -- a-addr xt )      create-local
         ['] compile-pushlocal-c          ['] compile-pushlocal-c
     does> ( Compilation: -- ) ( Run-time: -- w )      does> ( Compilation: -- ) ( Run-time: -- w )
         postpone laddr# @ lp-offset, ;          postpone laddr# @ lp-offset, ;
   
 \ you may want to make comments in a locals definitions group:  \ you may want to make comments in a locals definitions group:
 ' \ alias \ immediate  ' \ alias \ ( compilation 'ccc<newline>' -- ; run-time -- ) \ core-ext,block-ext backslash
 ' ( alias ( immediate  \G Comment till the end of the line if @code{BLK} contains 0 (i.e.,
   \G while not loading a block), parse and discard the remainder of the
   \G parse area. Otherwise, parse and discard all subsequent characters
   \G in the parse area corresponding to the current line.
   immediate
   
   ' ( alias ( ( compilation 'ccc<close-paren>' -- ; run-time -- ) \ core,file     paren
   \G Comment, usually till the next @code{)}: parse and discard all
   \G subsequent characters in the parse area until ")" is
   \G encountered. During interactive input, an end-of-line also acts as
   \G a comment terminator. For file input, it does not; if the
   \G end-of-file is encountered whilst parsing for the ")" delimiter,
   \G Gforth will generate a warning.
   immediate
   
 forth definitions  forth definitions
   also locals-types
       
   \ these "locals" are used for comparison in TO
   c: some-clocal 2drop
   d: some-dlocal 2drop
   f: some-flocal 2drop
   w: some-wlocal 2drop
   
   ' dict-execute1 is dict-execute \ now the real thing
       
 \ the following gymnastics are for declaring locals without type specifier.  \ the following gymnastics are for declaring locals without type specifier.
 \ we exploit a feature of our dictionary: every wordlist  \ we exploit a feature of our dictionary: every wordlist
 \ has it's own methods for finding words etc.  \ has it's own methods for finding words etc.
 \ So we create a vocabulary new-locals, that creates a 'w:' local named x  \ So we create a vocabulary new-locals, that creates a 'w:' local named x
 \ when it is asked if it contains x.  \ when it is asked if it contains x.
   
 0. 2constant last-local \ !! actually a 2value  
   
 also locals-types  
   
 : new-locals-find ( caddr u w -- nfa )  : new-locals-find ( caddr u w -- nfa )
 \ this is the find method of the new-locals vocabulary  \ this is the find method of the new-locals vocabulary
 \ make a new local with name caddr u; w is ignored  \ make a new local with name caddr u; w is ignored
 \ the returned nfa denotes a word that produces what W: produces  \ the returned nfa denotes a word that produces what W: produces
 \ !! do the whole thing without nextname  \ !! do the whole thing without nextname
     drop nextname W: \ we don't want the thing that W: produces,      drop nextname
     ['] last-local >body 2!   \ but the nfa of a word that produces that value: last-local      ['] W: >head-noprim ;
     [ ' last-local >name ] Aliteral ;  
   
 previous  previous
   
 : new-locals-reveal ( -- )  : new-locals-reveal ( -- )
   true abort" this should not happen: new-locals-reveal" ;    true abort" this should not happen: new-locals-reveal" ;
   
 create new-locals-map ' new-locals-find A, ' new-locals-reveal A,  create new-locals-map ( -- wordlist-map )
   ' new-locals-find A,
 vocabulary new-locals  ' new-locals-reveal A,
 new-locals-map ' new-locals >body cell+ A! \ !! use special access words  ' drop A, \ rehash method
   ' drop A,
 variable old-dpp  
   new-locals-map mappedwordlist Constant new-locals-wl
   
   \ slowvoc @
   \ slowvoc on
   \ vocabulary new-locals
   \ slowvoc !
   \ new-locals-map ' new-locals >body wordlist-map A! \ !! use special access words
   
 \ and now, finally, the user interface words  \ and now, finally, the user interface words
 : { ( -- addr wid 0 )  : { ( -- latestxt wid 0 ) \ gforth open-brace
     dp old-dpp !      latestxt get-current
     locals-dp dpp !      get-order new-locals-wl swap 1+ set-order
     also new-locals      also locals definitions locals-types
     also get-current locals definitions  locals-types  
     0 TO locals-wordlist      0 TO locals-wordlist
     0 postpone [ ; immediate      0 postpone [ ; immediate
   
 locals-types definitions  locals-types definitions
   
 : } ( addr wid 0 a-addr1 xt1 ... -- )  : } ( latestxt wid 0 a-addr1 xt1 ... -- ) \ gforth close-brace
     \ ends locals definitions      \ ends locals definitions
     ] old-dpp @ dpp !      ]
     begin      begin
         dup          dup
     while      while
Line 239  locals-types definitions Line 445  locals-types definitions
     repeat      repeat
     drop      drop
     locals-size @ alignlp-f locals-size ! \ the strictest alignment      locals-size @ alignlp-f locals-size ! \ the strictest alignment
     set-current  
     previous previous      previous previous
     locals-list TO locals-wordlist ;      set-current lastcfa !
       locals-list 0 wordlist-id - TO locals-wordlist ;
   
 : -- ( addr wid 0 ... -- )  : -- ( addr wid 0 ... -- ) \ gforth dash-dash
     }      }
     [char] } word drop ;      [char] } parse 2drop ;
   
 forth definitions  forth definitions
   
Line 339  forth definitions Line 545  forth definitions
   
 \ Implementation:  \ Implementation:
   
 \ orig, dest and do-sys have the following structure:  \ explicit scoping
 \ address (of the branch or the instruction to be branched to) (TOS)  
 \ locals-list (valid at address) (second)  
 \ locals-size (at address; this could be computed from locals-list, but so what) (third)  
   
 3 constant cs-item-size  
   
 : CS-PICK ( ... u -- ... destu )  
  1+ cs-item-size * 1- >r  
  r@ pick  r@ pick  r@ pick  
  rdrop ;  
   
 : CS-ROLL ( destu/origu .. dest0/orig0 u -- .. dest0/orig0 destu/origu )  
  1+ cs-item-size * 1- >r  
  r@ roll r@ roll r@ roll  
  rdrop ;   
   
 : CS-PUSH ( -- dest/orig )  
  locals-size @  
  locals-list @  
  here ;  
   
 : BUT       sys? 1 cs-roll ;                      immediate restrict  : scope ( compilation  -- scope ; run-time  -- ) \ gforth
 : YET       sys? 0 cs-pick ;                       immediate restrict      cs-push-part scopestart ; immediate
   
 : common-list ( list1 list2 -- list3 )  : adjust-locals-list ( wid -- )
 \ list1 and list2 are lists, where the heads are at higher addresses than      locals-list @ common-list
 \ the tail. list3 is the largest sublist of both lists.      dup list-size adjust-locals-size
  begin      locals-list ! ;
    2dup u<>  
  while  
    2dup u>  
    if  
      swap  
    endif  
    @  
  repeat  
  drop ;  
   
 : sub-list? ( list1 list2 -- f )  : endscope ( compilation scope -- ; run-time  -- ) \ gforth
 \ true iff list1 is a sublist of list2      scope?
  begin      drop  adjust-locals-list ; immediate
    2dup u<  
  while  
    @  
  repeat  
  = ;  
   
 : list-size ( list -- u )  
 \ size of the locals frame represented by list  
  0 ( list n )  
  begin  
    over 0<>  
  while  
    over  
    cell+ name> >body @ max  
    swap @ swap ( get next )  
  repeat  
  faligned nip ;  
   
 : x>mark ( -- orig )  
  cs-push 0 , ;  
   
 variable dead-code \ true if normal code at "here" would be dead  
   
 : unreachable ( -- )  
 \ declares the current point of execution as unreachable and  
 \ prepares the assumptions for a possible upcoming BEGIN  
  dead-code on  
  dup 0<> if  
    2 pick 2 pick  
  else  
    0 0  
  endif  
  locals-list!  
  locals-size ! ;  
   
 : check-begin ( list -- )  \ adapt the hooks
 \ warn if list is not a sublist of locals-list  
  locals-list @ sub-list? 0= if  
    \ !! print current position  
    ." compiler was overly optimistic about locals at a BEGIN" cr  
    \ !! print assumption and reality  
  endif ;  
   
 : xahead ( -- orig )  : locals-:-hook ( sys -- sys addr xt n )
  POSTPONE branch x>mark unreachable ; immediate      \ addr is the nfa of the defined word, xt its xt
       DEFERS :-hook
       latest latestxt
       clear-leave-stack
       0 locals-size !
       0 locals-list !
       dead-code off
       defstart ;
   
   [IFDEF] free-old-local-names
   :noname ( -- )
       locals-mem-list @ free-list
       0 locals-mem-list ! ;
   is free-old-local-names
   [THEN]
   
 : xif ( -- orig )  : locals-;-hook ( sys addr xt sys -- sys )
  POSTPONE ?branch x>mark ; immediate      def?
       0 TO locals-wordlist
       0 adjust-locals-size ( not every def ends with an exit )
       lastcfa ! last !
       DEFERS ;-hook ;
   
 \ THEN (another control flow from before joins the current one):  \ THEN (another control flow from before joins the current one):
 \ The new locals-list is the intersection of the current locals-list and  \ The new locals-list is the intersection of the current locals-list and
Line 442  variable dead-code \ true if normal code Line 597  variable dead-code \ true if normal code
 \ inefficient, e.g. if there is a locals declaration between IF and  \ inefficient, e.g. if there is a locals declaration between IF and
 \ ELSE. However, if ELSE generates an appropriate "lp+!#" before the  \ ELSE. However, if ELSE generates an appropriate "lp+!#" before the
 \ branch, there will be none after the target <then>.  \ branch, there will be none after the target <then>.
 : xthen ( orig -- )  
  sys? dup @ ?struc  : (then-like) ( orig -- )
  dead-code @      dead-orig =
  if      if
    >resolve          >resolve drop
    locals-list!      else
    locals-size !          dead-code @
  else          if
    locals-size @ 3 roll - compile-lp+!#              >resolve set-locals-size-list dead-code off
    >resolve          else \ both live
    locals-list @ common-list locals-list!              over list-size adjust-locals-size
    locals-size @  locals-list @ list-size - compile-lp+!#              >resolve
  endif              adjust-locals-list
  dead-code off ; immediate          then
       then ;
 : scope ( -- dest )  
  cs-push ; immediate  : (begin-like) ( -- )
       dead-code @ if
 : endscope ( dest -- )          \ set up an assumption of the locals visible here.  if the
  drop          \ users want something to be visible, they have to declare
  locals-list @ common-list locals-list!          \ that using ASSUME-LIVE
  locals-size @  locals-list @ list-size - compile-lp+!#          backedge-locals @ set-locals-size-list
  drop ; immediate      then
       dead-code off ;
 : xexit ( -- )  
     locals-size @ compile-lp+!# POSTPONE exit unreachable ; immediate  
   
 : x?exit ( -- )  
     POSTPONE xif POSTPONE xexit POSTPONE xthen ; immediate  
   
 : xelse ( orig1 -- orig2 )  
  sys?  
  POSTPONE xahead  
  1 cs-roll  
  POSTPONE xthen ; immediate  
   
 : xbegin ( -- dest )  
  cs-push dead-code off ; immediate  
   
 : xwhile ( dest -- orig dest )  
  sys?  
  POSTPONE xif  
  1 cs-roll ; immediate  
   
 \ AGAIN (the current control flow joins another, earlier one):  \ AGAIN (the current control flow joins another, earlier one):
 \ If the dest-locals-list is not a subset of the current locals-list,  \ If the dest-locals-list is not a subset of the current locals-list,
 \ issue a warning (see below). The following code is generated:  \ issue a warning (see below). The following code is generated:
 \ lp+!# (current-local-size - dest-locals-size)  \ lp+!# (current-local-size - dest-locals-size)
 \ branch <begin>  \ branch <begin>
 : xagain ( dest -- )  
  sys?  : (again-like) ( dest -- addr )
  locals-size @ 3 roll - compile-lp+!#      over list-size adjust-locals-size
  POSTPONE branch      swap check-begin  POSTPONE unreachable ;
  <resolve  
  check-begin  
  unreachable ; immediate  
   
 \ UNTIL (the current control flow may join an earlier one or continue):  \ UNTIL (the current control flow may join an earlier one or continue):
 \ Similar to AGAIN. The new locals-list and locals-size are the current  \ Similar to AGAIN. The new locals-list and locals-size are the current
 \ ones. The following code is generated:  \ ones. The following code is generated:
 \ lp+!# (current-local-size - dest-locals-size)  \ ?branch-lp+!# <begin> (current-local-size - dest-locals-size)
 \ ?branch <begin>  
 \ lp+!# (dest-local-size - current-locals-size)  
 \ (Another inefficiency. Maybe we should introduce a ?branch-lp+!#  
 \ primitive. This would also solve the interrupt problem)  
 : until-like ( dest xt -- )  
  >r  
  sys?  
  locals-size @ dup 4 roll - compile-lp+!# ( list dest-addr old-locals-size )  
  r> compile,  
  >r <resolve  
  check-begin  
  locals-size @ r> - compile-lp+!# ;  
   
 : xuntil ( dest -- )  
  ['] ?branch until-like ; immediate  
   
 : xrepeat ( orig dest -- )  
  3 pick 0= ?struc  
  postpone xagain  
  postpone xthen ; immediate  
   
 \ counted loops  
   
 \ leave poses a little problem here  
 \ we have to store more than just the address of the branch, so the  
 \ traditional linked list approach is no longer viable.  
 \ This is solved by storing the information about the leavings in a  
 \ special stack. The leavings of different DO-LOOPs are separated  
 \ by a 0 entry  
   
 \ !! remove the fixed size limit. 'Tis easy.  
 20 constant leave-stack-size  
 create leave-stack leave-stack-size cs-item-size * cells allot  
 variable leave-sp  leave-stack leave-sp !  
   
 : clear-leave-stack ( -- )  
  leave-stack leave-sp ! ;  
   
 \ : leave-empty? ( -- f )  
 \  leave-sp @ leave-stack = ;  
   
 : >leave ( orig -- )  
 \ push on leave-stack  
  leave-sp @  
  dup [ leave-stack leave-stack-size cs-item-size * cells + ] Aliteral >=  
  if  
    abort" leave-stack full"  
  endif  
  tuck ! cell+  
  tuck ! cell+  
  tuck ! cell+  
  leave-sp ! ;  
   
 : leave> ( -- orig )  
 \ pop from leave-stack  
  leave-sp @  
  dup leave-stack <= if  
    abort" leave-stack empty"  
  endif  
  cell - dup @ swap  
  cell - dup @ swap  
  cell - dup @ swap  
  leave-sp ! ;  
   
 : done ( -- )  
 \ !! the original done had ( addr -- )  
  begin  
    leave>  
    dup  
  while  
    POSTPONE xthen  
  repeat  
  2drop drop ; immediate  
   
 : xleave ( -- )  
  POSTPONE xahead  
  >leave ; immediate  
   
 : x?leave ( -- )  
  POSTPONE 0= POSTPONE xif  
  >leave ; immediate  
   
 : xdo ( -- do-sys )  
  POSTPONE (do)  
  POSTPONE xbegin  
  0 0 0 >leave ; immediate  
   
 : x?do ( -- do-sys )  
  0 0 0 >leave  
  POSTPONE (?do)  
  x>mark >leave  
  POSTPONE xbegin ; immediate  
   
 : xfor ( -- do-sys )  
  POSTPONE (for)  
  POSTPONE xbegin  
  0 0 0 >leave ; immediate  
   
 \ LOOP etc. are just like UNTIL  
 \ the generated code for ?DO ... LOOP with locals is inefficient, this   
 \ could be changed by introducing (loop)-lp+!# etc.  
   
 : loop-like ( do-sys xt -- )  
  until-like  POSTPONE done  POSTPONE unloop ;  
   
 : xloop ( do-sys -- )  
  ['] (loop) loop-like ; immediate  
   
 : x+loop ( do-sys -- )  : (until-like) ( list addr xt1 xt2 -- )
  ['] (+loop) loop-like ; immediate      \ list and addr are a fragment of a cs-item
       \ xt1 is the conditional branch without lp adjustment, xt2 is with
       >r >r
       locals-size @ 2 pick list-size - dup if ( list dest-addr adjustment )
           r> drop r> compile,
           swap <resolve ( list adjustment ) ,
       else ( list dest-addr adjustment )
           drop
           r> compile, <resolve
           r> drop
       then ( list )
       check-begin ;
   
 : xs+loop ( do-sys -- )  : (exit-like) ( -- )
  ['] (s+loop) loop-like ; immediate      0 adjust-locals-size ;
   
 : locals-:-hook ( sys -- sys addr xt )  
     DEFERS :-hook  
     last @ lastcfa @  
     clear-leave-stack  
     0 locals-size !  
     locals-buffer locals-dp !  
     0 locals-list! ; ( clear locals vocabulary )  
   
 : locals-;-hook ( sys addr xt -- sys )  
     0 TO locals-wordlist  
     locals-size @ compile-lp+!#  
     lastcfa ! last !  
     DEFERS ;-hook ;  
   
 ' locals-:-hook IS :-hook  ' locals-:-hook IS :-hook
 ' locals-;-hook IS ;-hook  ' locals-;-hook IS ;-hook
   
   
   ' (then-like)  IS then-like
   ' (begin-like) IS begin-like
   ' (again-like) IS again-like
   ' (until-like) IS until-like
   ' (exit-like)  IS exit-like
   
 \ The words in the locals dictionary space are not deleted until the end  \ The words in the locals dictionary space are not deleted until the end
 \ of the current word. This is a bit too conservative, but very simple.  \ of the current word. This is a bit too conservative, but very simple.
   
Line 644  variable leave-sp  leave-stack leave-sp Line 674  variable leave-sp  leave-stack leave-sp
 \ things above are not control flow joins. Everything should be taken  \ things above are not control flow joins. Everything should be taken
 \ over from the live flow. No lp+!# is generated.  \ over from the live flow. No lp+!# is generated.
   
 \ !! The lp gymnastics for UNTIL are also a real problem: locals cannot be  
 \ used in signal handlers (or anything else that may be called while  
 \ locals live beyond the lp) without changing the locals stack.  
   
 \ About warning against uses of dead locals. There are several options:  \ About warning against uses of dead locals. There are several options:
   
 \ 1) Do not complain (After all, this is Forth;-)  \ 1) Do not complain (After all, this is Forth;-)
Line 677  variable leave-sp  leave-stack leave-sp Line 703  variable leave-sp  leave-stack leave-sp
   
 \ And here's finally the ANS standard stuff  \ And here's finally the ANS standard stuff
   
 : (local) ( addr u -- )  : (local) ( addr u -- ) \ local paren-local-paren
 \ a little space-inefficient, but well deserved ;-)      \ a little space-inefficient, but well deserved ;-)
 \ In exchange, there are no restrictions whatsoever on using (local)      \ In exchange, there are no restrictions whatsoever on using (local)
  dup      \ as long as you use it in a definition
  if      dup
    nextname POSTPONE { [ also locals-types ] W: } [ previous ]      if
  else          nextname POSTPONE { [ also locals-types ] W: } [ previous ]
    2drop      else
  endif ;          2drop
       endif ;
 \ \ !! untested  
 \ : TO ( c|w|d|r "name" -- )  : >definer ( xt -- definer ) \ gforth
 \ \ !! state smart      \G @var{Definer} is a unique identifier for the way the @var{xt}
 \  0 0 0. 0.0e0 { c: clocal w: wlocal d: dlocal f: flocal }      \G was defined.  Words defined with different @code{does>}-codes
 \  ' dup >definer      \G have different definers.  The definer can be used for
 \  state @       \G comparison and in @code{definer!}.
 \  if      dup >does-code
 \    case      ?dup-if
 \      [ ' locals-wordlist >definer ] literal \ value          nip 1 or
 \      OF >body POSTPONE Aliteral POSTPONE ! ENDOF      else
 \      [ ' clocal >definer ] literal          >code-address
 \      OF POSTPONE laddr# >body @ lp-offset, POSTPONE c! ENDOF      then ;
 \      [ ' wlocal >definer ] literal  
 \      OF POSTPONE laddr# >body @ lp-offset, POSTPONE ! ENDOF  : definer! ( definer xt -- ) \ gforth
 \      [ ' dlocal >definer ] literal      \G The word represented by @var{xt} changes its behaviour to the
 \      OF POSTPONE laddr# >body @ lp-offset, POSTPONE d! ENDOF      \G behaviour associated with @var{definer}.
 \      [ ' flocal >definer ] literal      over 1 and if
 \      OF POSTPONE laddr# >body @ lp-offset, POSTPONE f! ENDOF          swap [ 1 invert ] literal and does-code!
 \      abort" can only store TO value or local value"      else
 \    endcase          code-address!
 \  else      then ;
 \    [ ' locals-wordlist >definer ] literal =  
 \    if  : (int-to) ( xt -- ) dup >definer
 \      >body !      case
 \    else          [ ' locals-wordlist ] literal >definer \ value
 \      abort" can only store TO value"          of  >body ! endof
 \    endif          [ ' parse-name ] literal >definer \ defer
 \  endif ;             of  defer! endof
           -&32 throw
 \ : locals|      endcase ;
 \ !! should lie around somewhere  
   : (comp-to) ( xt -- ) dup >definer
       case
           [ ' locals-wordlist ] literal >definer \ value
           OF >body POSTPONE Aliteral POSTPONE ! ENDOF
           [ ' parse-name ] literal >definer \ defer
           OF POSTPONE Aliteral POSTPONE defer! ENDOF
           \ !! dependent on c: etc. being does>-defining words
           \ this works, because >definer uses >does-code in this case,
           \ which produces a relocatable address
           [ comp' some-clocal drop ] literal >definer
           OF POSTPONE laddr# >body @ lp-offset, POSTPONE c! ENDOF
           [ comp' some-wlocal drop ] literal >definer
           OF POSTPONE laddr# >body @ lp-offset, POSTPONE ! ENDOF
           [ comp' some-dlocal drop ] literal >definer
           OF POSTPONE laddr# >body @ lp-offset, POSTPONE 2! ENDOF
           [ comp' some-flocal drop ] literal >definer
           OF POSTPONE laddr# >body @ lp-offset, POSTPONE f! ENDOF
           -&32 throw
       endcase ;
   
   :noname
       ' (int-to) ;
   :noname
       comp' drop (comp-to) ;
   interpret/compile: TO ( c|w|d|r "name" -- ) \ core-ext,local
   
   : locals| ( ... "name ..." -- ) \ local-ext locals-bar
       \ don't use 'locals|'! use '{'! A portable and free '{'
       \ implementation is compat/anslocals.fs
       BEGIN
           name 2dup s" |" str= 0=
       WHILE
           (local)
       REPEAT
       drop 0 (local) ; immediate restrict

Removed from v.1.2  
changed lines
  Added in v.1.69


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>