--- gforth/glocals.fs 1996/07/16 20:57:09 1.23 +++ gforth/glocals.fs 2012/02/09 17:27:37 1.67 @@ -1,12 +1,12 @@ \ A powerful locals implementation -\ Copyright (C) 1995 Free Software Foundation, Inc. +\ 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 2 +\ 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, @@ -15,8 +15,7 @@ \ 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. +\ along with this program. If not, see http://www.gnu.org/licenses/. \ More documentation can be found in the manual and in @@ -80,13 +79,9 @@ \ aligned correctly, but our locals stack must be float-aligned between \ words. -\ Other things about the internals are pretty unclear now. - -\ Currently locals may only be -\ defined at the outer level and TO is not supported. - -require search-order.fs +require search.fs require float.fs +require extend.fs \ for case : compile-@local ( n -- ) \ gforth compile-fetch-local case @@ -104,6 +99,21 @@ require float.fs ( 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. @@ -114,14 +124,25 @@ require 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 - \ here the names of the local variables are stored - \ we would have problems storing them at the normal dp +variable locals-mem-list \ linked list of all locals name memory in +0 locals-mem-list ! \ the current (outer-level) definition -variable locals-dp \ so here's the special dp for locals. +: 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 ) \ cell-align size and generate the corresponding code for aligning lp @@ -142,6 +163,89 @@ variable locals-dp \ so here's the speci swap ! 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 -- ) locals-size @ alignlp-f float+ dup locals-size ! swap ! @@ -157,12 +261,45 @@ variable locals-dp \ so here's the speci locals-size @ swap ! postpone lp@ postpone c! ; +7 cells 32 + constant locals-name-size \ 32-char name + fields + wiggle room + +: create-local1 ( "name" -- a-addr ) + create + immediate restrict + 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 - create - immediate restrict - here 0 , ( place for the offset ) ; + 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 @@ -228,26 +365,46 @@ locals-types definitions postpone laddr# @ lp-offset, ; \ you may want to make comments in a locals definitions group: -' \ alias \ immediate -' ( alias ( immediate +' \ alias \ ( compilation 'ccc' -- ; run-time -- ) \ core-ext,block-ext backslash +\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' -- ; 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 +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. \ we exploit a feature of our dictionary: every wordlist \ has it's own methods for finding words etc. \ So we create a vocabulary new-locals, that creates a 'w:' local named x \ when it is asked if it contains x. -also locals-types - : new-locals-find ( caddr u w -- nfa ) \ this is the find method of the new-locals vocabulary \ make a new local with name caddr u; w is ignored \ the returned nfa denotes a word that produces what W: produces \ !! do the whole thing without nextname drop nextname - ['] W: >name ; + ['] W: >head-noprim ; previous @@ -255,27 +412,32 @@ previous true abort" this should not happen: new-locals-reveal" ; create new-locals-map ( -- wordlist-map ) -' new-locals-find A, ' new-locals-reveal A, - -vocabulary new-locals -new-locals-map ' new-locals >body cell+ A! \ !! use special access words - -variable old-dpp +' new-locals-find A, +' new-locals-reveal A, +' drop A, \ rehash method +' drop A, + +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 -: { ( -- addr wid 0 ) \ gforth open-brace - dp old-dpp ! - locals-dp dpp ! - also new-locals - also get-current locals definitions locals-types +: { ( -- latestxt wid 0 ) \ gforth open-brace + latestxt get-current + get-order new-locals-wl swap 1+ set-order + also locals definitions locals-types 0 TO locals-wordlist 0 postpone [ ; immediate locals-types definitions -: } ( addr wid 0 a-addr1 xt1 ... -- ) \ gforth close-brace +: } ( latestxt wid 0 a-addr1 xt1 ... -- ) \ gforth close-brace \ ends locals definitions - ] old-dpp @ dpp ! + ] begin dup while @@ -283,9 +445,9 @@ 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 ... -- ) \ gforth dash-dash } @@ -381,46 +543,39 @@ 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 ( compilation -- scope ; run-time -- ) \ gforth - cs-push-part scopestart ; immediate + cs-push-part scopestart ; 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 - locals-list @ common-list - dup list-size adjust-locals-size - locals-list ! ; immediate + scope? + drop adjust-locals-list ; immediate \ adapt the hooks : locals-:-hook ( sys -- sys addr xt n ) \ addr is the nfa of the defined word, xt its xt DEFERS :-hook - last @ lastcfa @ + latest latestxt clear-leave-stack 0 locals-size ! - locals-buffer locals-dp ! 0 locals-list ! dead-code off defstart ; +:noname ( -- ) + locals-mem-list @ free-list + 0 locals-mem-list ! ; +is free-old-local-names + : locals-;-hook ( sys addr xt sys -- sys ) def? 0 TO locals-wordlist @@ -428,9 +583,85 @@ 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. @@ -441,10 +672,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;-) @@ -485,20 +712,21 @@ forth definitions 2drop endif ; -: >definer ( xt -- definer ) - \ 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 [ ' spaces >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 +: >definer ( xt -- definer ) \ gforth + \G @var{Definer} is a unique identifier for the way the @var{xt} + \G was defined. Words defined with different @code{does>}-codes + \G have different definers. The definer can be used for + \G comparison and in @code{definer!}. + dup >does-code + ?dup-if + nip 1 or else >code-address then ; -: definer! ( definer xt -- ) - \ gives the word represented by xt the behaviour associated with definer +: definer! ( definer xt -- ) \ gforth + \G The word represented by @var{xt} changes its behaviour to the + \G behaviour associated with @var{definer}. over 1 and if swap [ 1 invert ] literal and does-code! else @@ -506,35 +734,37 @@ forth definitions then ; :noname - ' dup >definer [ ' locals-wordlist >definer ] literal = + ' 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 } - ' dup >definer + comp' drop dup >definer case - [ ' locals-wordlist >definer ] literal \ value + [ ' locals-wordlist ] literal >definer \ value OF >body POSTPONE Aliteral POSTPONE ! ENDOF - [ ' clocal >definer ] literal + \ !! 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 - [ ' wlocal >definer ] literal + [ comp' some-wlocal drop ] literal >definer OF POSTPONE laddr# >body @ lp-offset, POSTPONE ! ENDOF - [ ' dlocal >definer ] literal + [ comp' some-dlocal drop ] literal >definer OF POSTPONE laddr# >body @ lp-offset, POSTPONE 2! ENDOF - [ ' flocal >definer ] literal + [ comp' some-flocal drop ] literal >definer OF POSTPONE laddr# >body @ lp-offset, POSTPONE f! ENDOF -&32 throw endcase ; -special: TO ( c|w|d|r "name" -- ) \ core-ext,local +interpret/compile: TO ( c|w|d|r "name" -- ) \ core-ext,local -: locals| +: locals| ( ... "name ..." -- ) \ local-ext locals-bar \ don't use 'locals|'! use '{'! A portable and free '{' \ implementation is compat/anslocals.fs BEGIN - name 2dup s" |" compare 0<> + name 2dup s" |" str= 0= WHILE (local) REPEAT