version 1.7, 1994/09/12 19:00:30
|
version 1.27, 1996/09/24 19:15:02
|
Line 1
|
Line 1
|
|
\ A powerful locals implementation |
|
|
|
\ Copyright (C) 1995 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 |
\ 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 61
|
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. |
|
|
include search-order.fs |
require search-order.fs |
include float.fs |
require float.fs |
|
|
: compile-@local ( n -- ) |
: compile-@local ( n -- ) \ gforth compile-fetch-local |
case |
case |
0 of postpone @local0 endof |
0 of postpone @local0 endof |
1 cells of postpone @local1 endof |
1 cells of postpone @local1 endof |
Line 73 include float.fs
|
Line 97 include float.fs
|
( otherwise ) dup postpone @local# , |
( otherwise ) dup postpone @local# , |
endcase ; |
endcase ; |
|
|
: compile-f@local ( n -- ) |
: compile-f@local ( n -- ) \ gforth compile-f-fetch-local |
case |
case |
0 of postpone f@local0 endof |
0 of postpone f@local0 endof |
1 floats of postpone f@local1 endof |
1 floats of postpone f@local1 endof |
( 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 118 variable locals-dp \ so here's the speci
|
Line 157 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 134 variable locals-dp \ so here's the speci
|
Line 222 variable locals-dp \ so here's the speci
|
postpone lp@ postpone c! ; |
postpone lp@ postpone c! ; |
|
|
: create-local ( " name" -- a-addr ) |
: 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 |
create |
immediate |
immediate restrict |
here 0 , ( place for the offset ) ; |
here 0 , ( place for the offset ) ; |
|
|
: lp-offset ( n1 -- n2 ) |
: lp-offset ( n1 -- n2 ) |
Line 152 variable locals-dp \ so here's the speci
|
Line 241 variable locals-dp \ so here's the speci
|
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 |
@ lp-offset compile-@local ; |
@ 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 ) |
@ lp-offset compile-f@local ; |
@ 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, ; |
Line 229 previous
|
Line 318 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, ' new-locals-reveal A, |
|
|
|
slowvoc @ |
|
slowvoc on |
vocabulary new-locals |
vocabulary new-locals |
|
slowvoc ! |
new-locals-map ' new-locals >body cell+ A! \ !! use special access words |
new-locals-map ' new-locals >body cell+ 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 ) |
: { ( -- addr wid 0 ) \ gforth open-brace |
dp old-dpp ! |
dp old-dpp ! |
locals-dp dpp ! |
locals-dp dpp ! |
also new-locals |
also new-locals |
Line 247 variable old-dpp
|
Line 340 variable old-dpp
|
|
|
locals-types definitions |
locals-types definitions |
|
|
: } ( addr wid 0 a-addr1 xt1 ... -- ) |
: } ( addr wid 0 a-addr1 xt1 ... -- ) \ gforth close-brace |
\ ends locals definitions |
\ ends locals definitions |
] old-dpp @ dpp ! |
] old-dpp @ dpp ! |
begin |
begin |
Line 261 locals-types definitions
|
Line 354 locals-types definitions
|
previous previous |
previous previous |
locals-list TO locals-wordlist ; |
locals-list TO locals-wordlist ; |
|
|
: -- ( addr wid 0 ... -- ) |
: -- ( addr wid 0 ... -- ) \ gforth dash-dash |
} |
} |
[char] } word drop ; |
[char] } parse 2drop ; |
|
|
forth definitions |
forth definitions |
|
|
Line 355 forth definitions
|
Line 448 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: migrated to kernel.fs |
|
|
\ 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 372 forth definitions
|
Line 465 forth definitions
|
|
|
\ explicit scoping |
\ explicit scoping |
|
|
: scope ( -- scope ) |
: scope ( compilation -- scope ; run-time -- ) \ gforth |
cs-push-part scopestart ; immediate |
cs-push-part scopestart ; immediate |
|
|
: endscope ( scope -- ) |
: endscope ( compilation scope -- ; run-time -- ) \ gforth |
scope? |
scope? |
drop |
drop |
locals-list @ common-list |
locals-list @ common-list |
Line 402 forth definitions
|
Line 495 forth definitions
|
lastcfa ! last ! |
lastcfa ! last ! |
DEFERS ;-hook ; |
DEFERS ;-hook ; |
|
|
|
: (then-like) ( orig -- addr ) |
|
swap -rot dead-orig = |
|
if |
|
drop |
|
else |
|
dead-code @ |
|
if |
|
set-locals-size-list dead-code off |
|
else \ both live |
|
dup list-size adjust-locals-size |
|
locals-list @ common-list dup list-size adjust-locals-size |
|
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 448 forth definitions
|
Line 603 forth definitions
|
|
|
\ 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) |
\ as long as you use it in a definition |
\ as long as you use it in a definition |
Line 463 forth definitions
|
Line 618 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 [ ' bits >code-address ] Literal = |
dup >code-address [ ' spaces >code-address ] Literal = |
\ !! this definition will not work on some implementations for `bits' |
\ !! this definition will not work on some implementations for `bits' |
if \ if >code-address delivers the same value for all does>-def'd words |
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 |
>does-code 1 or \ bit 0 marks special treatment for does codes |
Line 474 forth definitions
|
Line 629 forth definitions
|
: definer! ( definer xt -- ) |
: definer! ( definer xt -- ) |
\ gives the word represented by xt the behaviour associated with definer |
\ gives the word represented by xt the behaviour associated with definer |
over 1 and if |
over 1 and if |
does-code! |
swap [ 1 invert ] literal and does-code! |
else |
else |
code-address! |
code-address! |
then ; |
then ; |
|
|
\ !! untested |
:noname |
: TO ( c|w|d|r "name" -- ) |
' dup >definer [ ' locals-wordlist >definer ] literal = |
\ !! state smart |
if |
0 0 0. 0.0e0 { c: clocal w: wlocal d: dlocal f: flocal } |
>body ! |
' dup >definer |
else |
state @ |
-&32 throw |
if |
endif ; |
case |
:noname |
[ ' locals-wordlist >definer ] literal \ value |
0 0 0. 0.0e0 { c: clocal w: wlocal d: dlocal f: flocal } |
OF >body POSTPONE Aliteral POSTPONE ! ENDOF |
' dup >definer |
[ ' clocal >definer ] literal |
case |
OF POSTPONE laddr# >body @ lp-offset, POSTPONE c! ENDOF |
[ ' locals-wordlist >definer ] literal \ value |
[ ' wlocal >definer ] literal |
OF >body POSTPONE Aliteral POSTPONE ! ENDOF |
OF POSTPONE laddr# >body @ lp-offset, POSTPONE ! ENDOF |
[ comp' clocal drop >definer ] literal |
[ ' dlocal >definer ] literal |
OF POSTPONE laddr# >body @ lp-offset, POSTPONE c! ENDOF |
OF POSTPONE laddr# >body @ lp-offset, POSTPONE d! ENDOF |
[ comp' wlocal drop >definer ] literal |
[ ' flocal >definer ] literal |
OF POSTPONE laddr# >body @ lp-offset, POSTPONE ! ENDOF |
OF POSTPONE laddr# >body @ lp-offset, POSTPONE f! ENDOF |
[ comp' dlocal drop >definer ] literal |
abort" can only store TO value or local value" |
OF POSTPONE laddr# >body @ lp-offset, POSTPONE 2! ENDOF |
endcase |
[ comp' flocal drop >definer ] literal |
else |
OF POSTPONE laddr# >body @ lp-offset, POSTPONE f! ENDOF |
[ ' locals-wordlist >definer ] literal = |
-&32 throw |
if |
endcase ; |
>body ! |
interpret/compile: TO ( c|w|d|r "name" -- ) \ core-ext,local |
else |
|
abort" can only store TO value" |
|
endif |
|
endif ; immediate |
|
|
|
: locals| |
: locals| |
BEGIN name 2dup s" |" compare 0= WHILE |
\ don't use 'locals|'! use '{'! A portable and free '{' |
(local) REPEAT drop 0 (local) ; immediate restrict |
\ implementation is compat/anslocals.fs |
|
BEGIN |
|
name 2dup s" |" compare 0<> |
|
WHILE |
|
(local) |
|
REPEAT |
|
drop 0 (local) ; immediate restrict |