version 1.20, 1996/05/03 13:05:05
|
version 1.47, 2001/09/16 10:20:39
|
Line 1
|
Line 1
|
\ A powerful locals implementation |
\ A powerful locals implementation |
|
|
\ Copyright (C) 1995 Free Software Foundation, Inc. |
\ Copyright (C) 1995,1996,1997,1998,2000 Free Software Foundation, Inc. |
|
|
\ This file is part of Gforth. |
\ This file is part of Gforth. |
|
|
Line 16
|
Line 16
|
|
|
\ You should have received a copy of the GNU General Public License |
\ You should have received a copy of the GNU General Public License |
\ along with this program; if not, write to the Free Software |
\ along with this program; if not, write to the Free Software |
\ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
\ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. |
|
|
|
|
\ More documentation can be found in the manual and in |
\ More documentation can be found in the manual and in |
Line 85
|
Line 85
|
\ Currently locals may only be |
\ Currently locals may only be |
\ defined at the outer level and TO is not supported. |
\ defined at the outer level and TO is not supported. |
|
|
require search-order.fs |
require search.fs |
require float.fs |
require float.fs |
|
require extend.fs \ for case |
|
|
: compile-@local ( n -- ) \ gforth compile-fetch-local |
: compile-@local ( n -- ) \ gforth compile-fetch-local |
case |
case |
Line 104 require float.fs
|
Line 105 require float.fs
|
( otherwise ) dup postpone f@local# , |
( otherwise ) dup postpone f@local# , |
endcase ; |
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, |
\ i.e. by going onto the locals stack the order is reversed. |
\ i.e. by going onto the locals stack the order is reversed. |
Line 114 require float.fs
|
Line 130 require float.fs
|
slowvoc @ |
slowvoc @ |
slowvoc on \ we want a linked list for the vocabulary locals |
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 ' locals-list >body ! |
' locals >body wordlist-id ' locals-list >body ! |
slowvoc ! |
slowvoc ! |
|
|
create locals-buffer 1000 allot \ !! limited and unsafe |
create locals-buffer 1000 allot \ !! limited and unsafe |
Line 142 variable locals-dp \ so here's the speci
|
Line 158 variable locals-dp \ so here's the speci
|
swap ! |
swap ! |
postpone >l ; |
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 -- ) |
: 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 228 locals-types definitions
|
Line 293 locals-types definitions
|
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 |
|
|
Line 247 also locals-types
|
Line 325 also locals-types
|
\ 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 |
drop nextname |
['] W: >name ; |
['] W: >head-noprim ; |
|
|
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, |
|
|
|
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 |
|
|
variable old-dpp |
variable old-dpp |
|
|
\ and now, finally, the user interface words |
\ and now, finally, the user interface words |
: { ( -- addr wid 0 ) \ gforth open-brace |
: { ( -- lastxt wid 0 ) \ gforth open-brace |
dp old-dpp ! |
dp old-dpp ! |
locals-dp dpp ! |
locals-dp dpp ! |
also new-locals |
lastxt get-current |
also get-current locals definitions locals-types |
get-order new-locals-wl swap 1+ set-order |
|
also 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 ... -- ) \ gforth close-brace |
: } ( lastxt wid 0 a-addr1 xt1 ... -- ) \ gforth close-brace |
\ ends locals definitions |
\ ends locals definitions |
] old-dpp @ dpp ! |
] old-dpp @ dpp ! |
begin |
begin |
Line 282 locals-types definitions
|
Line 370 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 ... -- ) \ gforth dash-dash |
: -- ( addr wid 0 ... -- ) \ gforth dash-dash |
} |
} |
Line 380 forth definitions
|
Line 468 forth definitions
|
|
|
\ If this assumption is too optimistic, the compiler will warn the user. |
\ If this assumption is too optimistic, the compiler will warn the user. |
|
|
\ Implementation: migrated to kernal.fs |
\ Implementation: |
|
|
\ 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) |
|
\ <then>: |
|
\ 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>. |
|
|
|
\ explicit scoping |
\ explicit scoping |
|
|
: scope ( compilation -- scope ; run-time -- ) \ gforth |
: 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 |
: endscope ( compilation scope -- ; run-time -- ) \ gforth |
scope? |
scope? |
drop |
drop adjust-locals-list ; immediate |
locals-list @ common-list |
|
dup list-size adjust-locals-size |
|
locals-list ! ; immediate |
|
|
|
\ adapt the hooks |
\ adapt the hooks |
|
|
Line 427 forth definitions
|
Line 504 forth definitions
|
lastcfa ! last ! |
lastcfa ! last ! |
DEFERS ;-hook ; |
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) |
|
\ <then>: |
|
\ 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>. |
|
|
|
: (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 <begin> |
|
|
|
: (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+!# <begin> (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 <resolve ( list adjustment ) , |
|
else ( list dest-addr adjustment ) |
|
drop |
|
r> compile, <resolve |
|
r> drop |
|
then ( list ) |
|
check-begin ; |
|
|
|
: (exit-like) ( -- ) |
|
0 adjust-locals-size ; |
|
|
' 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 440 forth definitions
|
Line 592 forth definitions
|
\ 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 488 forth definitions
|
Line 636 forth definitions
|
\ this gives a unique identifier for the way the xt was defined |
\ this gives a unique identifier for the way the xt was defined |
\ words defined with different does>-codes have different definers |
\ words defined with different does>-codes have different definers |
\ the definer can be used for comparison and in definer! |
\ the definer can be used for comparison and in definer! |
dup >code-address [ ' spaces >code-address ] Literal = |
dup >does-code |
\ !! this definition will not work on some implementations for `bits' |
?dup-if |
if \ if >code-address delivers the same value for all does>-def'd words |
nip 1 or |
>does-code 1 or \ bit 0 marks special treatment for does codes |
|
else |
else |
>code-address |
>code-address |
then ; |
then ; |
Line 504 forth definitions
|
Line 651 forth definitions
|
code-address! |
code-address! |
then ; |
then ; |
|
|
: TO ( c|w|d|r "name" -- ) \ core-ext,local |
:noname |
0 0 0. 0.0e0 { c: clocal w: wlocal d: dlocal f: flocal } |
' dup >definer [ ' locals-wordlist ] literal >definer = |
' dup >definer |
if |
state @ |
>body ! |
if |
else |
case |
-&32 throw |
[ ' locals-wordlist >definer ] literal \ value |
endif ; |
OF >body POSTPONE Aliteral POSTPONE ! ENDOF |
:noname |
[ ' clocal >definer ] literal |
0 0 0. 0.0e0 { c: clocal w: wlocal d: dlocal f: flocal } |
OF POSTPONE laddr# >body @ lp-offset, POSTPONE c! ENDOF |
comp' drop dup >definer |
[ ' wlocal >definer ] literal |
case |
OF POSTPONE laddr# >body @ lp-offset, POSTPONE ! ENDOF |
[ ' locals-wordlist ] literal >definer \ value |
[ ' dlocal >definer ] literal |
OF >body POSTPONE Aliteral POSTPONE ! ENDOF |
OF POSTPONE laddr# >body @ lp-offset, POSTPONE 2! ENDOF |
\ !! dependent on c: etc. being does>-defining words |
[ ' flocal >definer ] literal |
\ this works, because >definer uses >does-code in this case, |
OF POSTPONE laddr# >body @ lp-offset, POSTPONE f! ENDOF |
\ which produces a relocatable address |
-&32 throw |
[ comp' clocal drop >definer ] literal |
endcase |
OF POSTPONE laddr# >body @ lp-offset, POSTPONE c! ENDOF |
else |
[ comp' wlocal drop >definer ] literal |
[ ' locals-wordlist >definer ] literal = |
OF POSTPONE laddr# >body @ lp-offset, POSTPONE ! ENDOF |
if |
[ comp' dlocal drop >definer ] literal |
>body ! |
OF POSTPONE laddr# >body @ lp-offset, POSTPONE 2! ENDOF |
else |
[ comp' flocal drop >definer ] literal |
-&32 throw |
OF POSTPONE laddr# >body @ lp-offset, POSTPONE f! ENDOF |
endif |
-&32 throw |
endif ; immediate |
endcase ; |
|
interpret/compile: TO ( c|w|d|r "name" -- ) \ core-ext,local |
|
|
: locals| |
: locals| |
\ don't use 'locals|'! use '{'! A portable and free '{' |
\ don't use 'locals|'! use '{'! A portable and free '{' |
\ implementation is anslocals.fs |
\ implementation is compat/anslocals.fs |
BEGIN |
BEGIN |
name 2dup s" |" compare 0<> |
name 2dup s" |" compare 0<> |
WHILE |
WHILE |