--- gforth/glocals.fs 1994/10/24 19:15:58 1.8 +++ gforth/glocals.fs 1999/02/03 00:10:21 1.39 @@ -1,3 +1,27 @@ +\ A powerful locals implementation + +\ Copyright (C) 1995,1996,1997,1998 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 2 +\ 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, write to the Free Software +\ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + + +\ 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 \ IMO (anton) they are the worst part of the standard. There they are very \ restricted and have an ugly interface. @@ -61,10 +85,10 @@ \ Currently locals may only be \ defined at the outer level and TO is not supported. -include search-order.fs -include float.fs +require search.fs +require float.fs -: compile-@local ( n -- ) +: compile-@local ( n -- ) \ gforth compile-fetch-local case 0 of postpone @local0 endof 1 cells of postpone @local1 endof @@ -73,13 +97,28 @@ include float.fs ( otherwise ) dup postpone @local# , endcase ; -: compile-f@local ( n -- ) +: 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) \ of the local variables of a group (in braces) the leftmost is on top, \ i.e. by going onto the locals stack the order is reversed. @@ -90,7 +129,7 @@ include float.fs slowvoc @ slowvoc on \ we want a linked list for the vocabulary locals vocabulary locals \ this contains the local variables -' locals >body ' locals-list >body ! +' locals >body wordlist-id ' locals-list >body ! slowvoc ! create locals-buffer 1000 allot \ !! limited and unsafe @@ -118,6 +157,55 @@ variable locals-dp \ so here's the speci swap ! postpone >l ; +\ locals list operations + +: common-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 ; + +: sub-list? ( list1 list2 -- f ) \ gforth-internal +\ true iff list1 is a sublist of list2 + begin + 2dup u< + while + @ + repeat + = ; + +: 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 + ." compiler was overly optimistic about locals at a BEGIN" cr + \ !! print assumption and reality + then ; + : compile-pushlocal-f ( a-addr -- ) ( run-time: f -- ) locals-size @ alignlp-f float+ dup locals-size ! swap ! @@ -134,9 +222,10 @@ variable locals-dp \ so here's the speci postpone lp@ postpone c! ; : create-local ( " name" -- a-addr ) - \ defines the local "name"; the offset of the local shall be stored in a-addr + \ defines the local "name"; the offset of the local shall be + \ stored in a-addr create - immediate + immediate restrict here 0 , ( place for the offset ) ; : lp-offset ( n1 -- n2 ) @@ -152,59 +241,70 @@ variable locals-dp \ so here's the speci vocabulary locals-types \ this contains all the type specifyers, -- and } locals-types definitions -: W: - create-local ( "name" -- a-addr xt ) +: W: ( "name" -- a-addr xt ) \ gforth w-colon + create-local \ xt produces the appropriate locals pushing code when executed ['] compile-pushlocal-w does> ( Compilation: -- ) ( Run-time: -- w ) \ compiles a local variable access @ lp-offset compile-@local ; -: W^ - create-local ( "name" -- a-addr xt ) +: W^ ( "name" -- a-addr xt ) \ gforth w-caret + create-local ['] compile-pushlocal-w does> ( Compilation: -- ) ( Run-time: -- w ) postpone laddr# @ lp-offset, ; -: F: - create-local ( "name" -- a-addr xt ) +: F: ( "name" -- a-addr xt ) \ gforth f-colon + create-local ['] compile-pushlocal-f does> ( Compilation: -- ) ( Run-time: -- w ) @ lp-offset compile-f@local ; -: F^ - create-local ( "name" -- a-addr xt ) +: F^ ( "name" -- a-addr xt ) \ gforth f-caret + create-local ['] compile-pushlocal-f does> ( Compilation: -- ) ( Run-time: -- w ) postpone laddr# @ lp-offset, ; -: D: - create-local ( "name" -- a-addr xt ) +: D: ( "name" -- a-addr xt ) \ gforth d-colon + create-local ['] compile-pushlocal-d does> ( Compilation: -- ) ( Run-time: -- w ) postpone laddr# @ lp-offset, postpone 2@ ; -: D^ - create-local ( "name" -- a-addr xt ) +: D^ ( "name" -- a-addr xt ) \ gforth d-caret + create-local ['] compile-pushlocal-d does> ( Compilation: -- ) ( Run-time: -- w ) postpone laddr# @ lp-offset, ; -: C: - create-local ( "name" -- a-addr xt ) +: C: ( "name" -- a-addr xt ) \ gforth c-colon + create-local ['] compile-pushlocal-c does> ( Compilation: -- ) ( Run-time: -- w ) postpone laddr# @ lp-offset, postpone c@ ; -: C^ - create-local ( "name" -- a-addr xt ) +: C^ ( "name" -- a-addr xt ) \ gforth c-caret + create-local ['] compile-pushlocal-c does> ( Compilation: -- ) ( Run-time: -- w ) postpone laddr# @ lp-offset, ; \ you may want to make comments in a locals definitions group: -' \ alias \ immediate -' ( alias ( immediate +' \ alias \ ( -- ) \ core-ext,block-ext backslash +\G Line comment: if @code{BLK} contains 0, parse and discard the remainder +\G of the parse area. Otherwise, parse and discard all subsequent characters in the +\G parse area corresponding to the current line. +immediate + +' ( alias ( ( compilation 'ccc' -- ; run-time -- ) \ core,file paren +\G Comment: parse and discard all subsequent characters in the parse +\G area until ")" is encountered. During interactive input, an end-of-line +\G also acts as a comment terminator. For file input, it does not; if the +\G end-of-file is encountered whilst parsing for the ")" delimiter, Gforth +\G will generate a warning. +immediate forth definitions @@ -229,25 +329,33 @@ previous : 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, +' new-locals-reveal A, +' drop A, \ rehash method +' drop A, +slowvoc @ +slowvoc on vocabulary new-locals -new-locals-map ' new-locals >body cell+ A! \ !! use special access words +slowvoc ! +new-locals-map ' new-locals >body wordlist-map A! \ !! use special access words variable old-dpp \ and now, finally, the user interface words -: { ( -- addr wid 0 ) +: { ( -- lastxt wid 0 ) \ gforth open-brace dp old-dpp ! locals-dp dpp ! + lastxt get-current also new-locals - also get-current locals definitions locals-types + also locals definitions locals-types 0 TO locals-wordlist 0 postpone [ ; immediate locals-types definitions -: } ( addr wid 0 a-addr1 xt1 ... -- ) +: } ( lastxt wid 0 a-addr1 xt1 ... -- ) \ gforth close-brace \ ends locals definitions ] old-dpp @ dpp ! begin @@ -257,13 +365,13 @@ locals-types definitions repeat drop locals-size @ alignlp-f locals-size ! \ the strictest alignment - set-current 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 @@ -355,32 +463,21 @@ forth definitions \ If this assumption is too optimistic, the compiler will warn the user. -\ Implementation: migrated to kernal.fs - -\ THEN (another control flow from before joins the current one): -\ The new locals-list is the intersection of the current locals-list and -\ the orig-local-list. The new locals-size is the (alignment-adjusted) -\ size of the new locals-list. The following code is generated: -\ lp+!# (current-locals-size - orig-locals-size) -\ : -\ lp+!# (orig-locals-size - new-locals-size) - -\ Of course "lp+!# 0" is not generated. Still this is admittedly a bit -\ inefficient, e.g. if there is a locals declaration between IF and -\ ELSE. However, if ELSE generates an appropriate "lp+!#" before the -\ branch, there will be none after the target . +\ Implementation: \ explicit scoping -: scope ( -- scope ) - cs-push-part scopestart ; immediate +: scope ( compilation -- scope ; run-time -- ) \ gforth + cs-push-part scopestart ; immediate -: endscope ( scope -- ) - scope? - drop - locals-list @ common-list - dup list-size adjust-locals-size - locals-list ! ; immediate +: adjust-locals-list ( wid -- ) + locals-list @ common-list + dup list-size adjust-locals-size + locals-list ! ; + +: endscope ( compilation scope -- ; run-time -- ) \ gforth + scope? + drop adjust-locals-list ; immediate \ adapt the hooks @@ -402,9 +499,84 @@ forth definitions lastcfa ! last ! DEFERS ;-hook ; +\ THEN (another control flow from before joins the current one): +\ The new locals-list is the intersection of the current locals-list and +\ the orig-local-list. The new locals-size is the (alignment-adjusted) +\ size of the new locals-list. The following code is generated: +\ lp+!# (current-locals-size - orig-locals-size) +\ : +\ lp+!# (orig-locals-size - new-locals-size) + +\ Of course "lp+!# 0" is not generated. Still this is admittedly a bit +\ inefficient, e.g. if there is a locals declaration between IF and +\ ELSE. However, if ELSE generates an appropriate "lp+!#" before the +\ branch, there will be none after the target . + +: (then-like) ( orig -- ) + dead-orig = + if + >resolve drop + else + dead-code @ + if + >resolve set-locals-size-list dead-code off + else \ both live + over list-size adjust-locals-size + >resolve + adjust-locals-list + then + then ; + +: (begin-like) ( -- ) + dead-code @ if + \ set up an assumption of the locals visible here. if the + \ users want something to be visible, they have to declare + \ that using ASSUME-LIVE + backedge-locals @ set-locals-size-list + then + dead-code off ; + +\ AGAIN (the current control flow joins another, earlier one): +\ If the dest-locals-list is not a subset of the current locals-list, +\ issue a warning (see below). The following code is generated: +\ lp+!# (current-local-size - dest-locals-size) +\ branch + +: (again-like) ( dest -- addr ) + over list-size adjust-locals-size + swap check-begin POSTPONE unreachable ; + +\ 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 +\ ones. The following code is generated: +\ ?branch-lp+!# (current-local-size - dest-locals-size) + +: (until-like) ( list addr xt1 xt2 -- ) + \ 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 compile, drop + then ( list ) + check-begin ; + +: (exit-like) ( -- ) + 0 adjust-locals-size ; + ' 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 \ of the current word. This is a bit too conservative, but very simple. @@ -415,10 +587,6 @@ forth definitions \ things above are not control flow joins. Everything should be taken \ 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: \ 1) Do not complain (After all, this is Forth;-) @@ -448,7 +616,7 @@ forth definitions \ 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 ;-) \ In exchange, there are no restrictions whatsoever on using (local) \ as long as you use it in a definition @@ -463,10 +631,9 @@ forth definitions \ this gives a unique identifier for the way the xt was defined \ words defined with different does>-codes have different definers \ the definer can be used for comparison and in definer! - dup >code-address [ ' bits >code-address ] Literal = - \ !! this definition will not work on some implementations for `bits' - if \ if >code-address delivers the same value for all does>-def'd words - >does-code 1 or \ bit 0 marks special treatment for does codes + dup >does-code + ?dup-if + nip 1 or else >code-address then ; @@ -474,44 +641,45 @@ forth definitions : definer! ( definer xt -- ) \ gives the word represented by xt the behaviour associated with definer over 1 and if - does-code! + swap [ 1 invert ] literal and does-code! else code-address! then ; -\ !! untested -: TO ( c|w|d|r "name" -- ) -\ !! state smart - 0 0 0. 0.0e0 { c: clocal w: wlocal d: dlocal f: flocal } - ' dup >definer - state @ - if - case - [ ' locals-wordlist >definer ] literal \ value - OF >body POSTPONE Aliteral POSTPONE ! ENDOF - [ ' clocal >definer ] literal - OF POSTPONE laddr# >body @ lp-offset, POSTPONE c! ENDOF - [ ' wlocal >definer ] literal - OF POSTPONE laddr# >body @ lp-offset, POSTPONE ! ENDOF - [ ' dlocal >definer ] literal - OF POSTPONE laddr# >body @ lp-offset, POSTPONE d! ENDOF - [ ' flocal >definer ] literal - OF POSTPONE laddr# >body @ lp-offset, POSTPONE f! ENDOF - abort" can only store TO value or local value" - endcase - else - [ ' locals-wordlist >definer ] literal = - if - >body ! - else - abort" can only store TO value" - endif - endif ; immediate +:noname + ' dup >definer [ ' locals-wordlist ] literal >definer = + if + >body ! + else + -&32 throw + endif ; +:noname + 0 0 0. 0.0e0 { c: clocal w: wlocal d: dlocal f: flocal } + comp' drop dup >definer + case + [ ' locals-wordlist ] literal >definer \ value + OF >body POSTPONE Aliteral POSTPONE ! 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' clocal drop >definer ] literal + OF POSTPONE laddr# >body @ lp-offset, POSTPONE c! ENDOF + [ comp' wlocal drop >definer ] literal + OF POSTPONE laddr# >body @ lp-offset, POSTPONE ! ENDOF + [ comp' dlocal drop >definer ] literal + OF POSTPONE laddr# >body @ lp-offset, POSTPONE 2! ENDOF + [ comp' flocal drop >definer ] literal + OF POSTPONE laddr# >body @ lp-offset, POSTPONE f! ENDOF + -&32 throw + endcase ; +interpret/compile: TO ( c|w|d|r "name" -- ) \ core-ext,local : locals| + \ don't use 'locals|'! use '{'! A portable and free '{' + \ implementation is compat/anslocals.fs BEGIN name 2dup s" |" compare 0<> WHILE (local) REPEAT - drop 0 (local) ; immediate restrict + drop 0 (local) ; immediate restrict