commit 4717adf3449d385b855ef0e7796a12a377c0475f
parent 18e558b98f930799710bf78e2e474968c5bdd560
Author: Virgil Dupras <hsoft@hardcoded.net>
Date: Fri, 3 Feb 2023 07:36:03 -0500
Remove rfree
I don't like using it, it represent an unnecessary risk of something going wrong
with a word for a very small gain in terseness.
Diffstat:
9 files changed, 30 insertions(+), 38 deletions(-)
diff --git a/fs/doc/dict.txt b/fs/doc/dict.txt
@@ -113,12 +113,11 @@ rot a b c -- b c a
rot> a b c -- c a b
rdrop -- *I* Compile a RS shrink of 4 bytes.
+2rdrop -- *I* Compile a RS shrink of 8 bytes.
r@ -- *I* Compile a push of current RS top to PS.
r> -- *I* Equivalent to r@ rdrop
>r -- *I* Compiles a RS grow of 4 bytes followed by a pop
of PS into that new RS space.
-rfree -- *I* Shrink RS by the current [rcnt] level and reset
- [rcnt] to 0.
r+, n -- Compile a RS grow (n is negative) or shrink (n is
positive) operation by n bytes.
r', off -- Compile the yield of RSP with "off" offset applied to
diff --git a/fs/doc/usage.txt b/fs/doc/usage.txt
@@ -256,42 +256,37 @@ V1=RSTART-4 and V4=RSTART-16.
When using local variables, you are responsible for pushing and popping to/from
RS. All those variables give "to" semantics to an "RS slot". Example:
-: foo ( a b c -- ) >r >r >r V1 . spc> V2 . spc> V3 . rfree ;
+ : foo ( a b c -- ) >r >r >r V1 . spc> V2 . spc> V3 . 2rdrop rdrop ;
-1 2 3 foo \ prints "3 2 1"
+ 1 2 3 foo \ prints "3 2 1"
-: inc ( a -- a+1 ) >r 1 to+ V1 V1 rdrop ;
-42 inc . \ prints 43
+ : inc ( a -- a+1 ) >r 1 to+ V1 V1 rdrop ;
+ 42 inc . \ prints 43
-\ this works too
-: inc5 ( a -- a+5 ) >r 5 >r begin 1 to+ V1 next V1 rdrop ;
-42 inc5 . \ prints 47
-
-### rfree
-
-What's this "rfree" used above? It's an automatic RS adjuster. It looks at the
-"R counter" and emits an RS adjustment equivalent to its current level, and then
-sets this level to 0. In the example above, it's equivalent to "rdrop rdrop
-rdrop".
-
-Be aware that the "R counter" is not always accurate! If you have conditional
-modifications to RS levels, "rfree" is going to be broken (and local variables
-too!). See section below.
+ \ this works too
+ : inc5 ( a -- a+5 ) >r 5 >r begin 1 to+ V1 next V1 rdrop ;
+ 42 inc5 . \ prints 47
### Manual [rcnt] adjustments
-The "R counter" that determines local variable slots is oblivious to conditional
-code or loops. It's not common to have code that conditionally maintain separate
-RS levels (they always need to stay balanced, of course), but it can happen. For
-example, in early "exit" paths, we often have to include a few "rdrop" before
-the "exit" call. If you have a fancy word that returns a variable number of
-arguments, you might end up with >r and r> in loops. All of this messes up the
-"R counter". You can manually adjust it through the [rcnt] variable. For
-example, if you want your next ">r" to push to
-V1, you would precede it with:
+The "R counter" that determines local variable slots at compile time is
+oblivious to conditional code or loops. It's not common to have code that
+conditionally maintain separate RS levels (they always need to stay balanced, of
+course), but it can happen. For example, in early "exit" paths, we often have to
+include a few "rdrop" before the "exit" call. If you have a fancy word that
+returns a variable number of arguments, you might end up with >r and r> in
+loops. All of this messes up the "R counter". You can manually adjust it through
+the [rcnt] variable. For example, if you want your next ">r" to push to V1, you
+would precede it with:
[ 0 [rcnt] ! ]
+Another use for manual [rcnt] adjustments is to have local variables to "below"
+the word line. For example, if for some reason you'd want "V1" to be bound to
+the word's return address, you can assign -4 to [rcnt]:
+
+ : foo [ -4 [rcnt] ! ] $1234 to V1 ; \ returns to address $1234
+
## Binary width modulation
In a 32-bit system, it is frequent to want to access memory in 3 widths: 32-bit,
diff --git a/fs/drv/pc/ahci.fs b/fs/drv/pc/ahci.fs
@@ -166,7 +166,7 @@ extends Drive struct[ AHCIDrive
V1 $20 < while
0 V1 V2 pcisel if
pci.class 1 = pci.subclass 6 = and if pci0.bar5 ['] ahci rebind then then
- 1 to+ V2 V2 8 = if 0 to V2 1 to+ V1 then repeat rfree ;
+ 1 to+ V2 V2 8 = if 0 to V2 1 to+ V1 then repeat 2rdrop ;
\ Do we have a AHCI controller?
: ahci? ahci :self bool ;
diff --git a/fs/fs/fatlo.fs b/fs/fs/fatlo.fs
@@ -255,7 +255,7 @@ struct+[ FAT
DirEntry SZ + dup V1 :)buf = if
drop V1 :nextsector? not if
rootdirentry( ( parses as "lastentry" ) else V1 :buf( then then
- repeat ( a ) drop rfree ;
+ repeat ( a ) drop 2rdrop ;
: :child ( dirid name self -- id-or-0 ) >r
fnbuf! r@ :getdirentry r@ :readdir r@ :findindir
diff --git a/fs/gr/plane.fs b/fs/gr/plane.fs
@@ -61,13 +61,13 @@ extends Rect struct[ Plane
dup :topleft rot height for ( x y )
2dup V2 :xyoff V2 buffer + V1 _addr V3 move
1 V1 :ty+ 1+ next ( x y+1 )
- 2drop rfree ;
+ 2drop 2rdrop rdrop ;
: :copy> ( rect dst self -- ) >r >r ( rect ) \ V1=self V2=dst
dup width V1 encoding _colorbytes * >r ( rect ) \ V3=u
dup :topleft rot height for ( x y )
2dup V2 :xyoff V2 buffer + V1 _addr swap V3 move
1 V1 :ty+ 1+ next ( x y+1 )
- 2drop rfree ;
+ 2drop 2rdrop rdrop ;
]struct
diff --git a/fs/gr/rect.fs b/fs/gr/rect.fs
@@ -24,7 +24,7 @@ struct[ Rect
:bottomright V1 :bottomright ( x1 y1 x2 y2 )
rot min V3 - dup 0< if drop 0 then >r ( x1 x2 ) \ V4=height
min V2 - dup 0< if drop 0 then ( width )
- V2 swap V3 swap V4 :tmpnew rfree ;
+ V2 swap V3 swap V4 :tmpnew 2rdrop 2rdrop ;
: :copy ( other self -- ) SZ move ;
: :move ( x y self -- ) tuck to y to x ;
: :resize ( width height self -- ) tuck to height to width ;
diff --git a/fs/tests/kernel.fs b/fs/tests/kernel.fs
@@ -101,7 +101,7 @@ create expected 38 , 39 , 40 , 41 , 42 ,
here foo expected 20 []= #
\ Local variables
-: foo 54 >r 42 >r 1 to+ V1 2 to+ V2 V2 V1 rfree ;
+: foo 54 >r 42 >r 1 to+ V1 2 to+ V2 V2 V1 2rdrop rdrop ;
foo 55 #eq 44 #eq
: inc5 >r 5 for 1 to+ V1 next V1 rdrop ;
42 inc5 47 #eq
diff --git a/fs/xcomp/bootlo.fs b/fs/xcomp/bootlo.fs
@@ -65,8 +65,6 @@ code 2drop 8 p+, exit,
: or? or bool ;
: upcase ( c -- c ) dup 'a' - 26 < if $df and then ;
-: rfree 0 [rcnt] @! neg r+, ; immediate
-
\ Local variables + beginning of compiling words
: create code compile (cell) ;
: const code litn exit, ;
diff --git a/fs/xcomp/i386/pc/build.fs b/fs/xcomp/i386/pc/build.fs
@@ -59,7 +59,7 @@ create _buf SECSZ allot0
V2 bootfs 0 Path :new Path :find# Path :copyfile
V3 bootfs 0 Path :new Path :find#
S" /init.fs" V1 0 Path :new Path :find# Path :appendfile
- rfree ;
+ 2rdrop rdrop ;
: buildPC ( drv clustercnt -- fat ) over >r \ V1=drv
." creating FAT and copying files" nl>