commit 5cc5dd2753a35430439dade6fbdcac1fa94ebee7
parent a5349884cd7abd3e89b22dfacc4e43e068ea68c4
Author: Virgil Dupras <hsoft@hardcoded.net>
Date: Thu, 1 Sep 2022 16:04:14 -0400
Add "rfree"
See doc/usage
Diffstat:
5 files changed, 16 insertions(+), 4 deletions(-)
diff --git a/fs/doc/usage.txt b/fs/doc/usage.txt
@@ -106,7 +106,7 @@ V1=RSTART-4 and V4=RSTART-16.
When using local variables, you are responsible with pushing and popping to/from
RS. All those variables give you are "to" semantics to a "RS slot". Example:
-: foo ( a b c -- ) >r >r >r V1 . spc> V2 . spc> V3 . rdrop rdrop rdrop ;
+: foo ( a b c -- ) >r >r >r V1 . spc> V2 . spc> V3 . rfree ;
1 2 3 foo \ prints "3 2 1"
@@ -117,6 +117,16 @@ RS. All those variables give you are "to" semantics to a "RS slot". Example:
: 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. See section below.
+
## Manual [rcnt] adjustments
The "R counter" that determines local variable slots is oblivious to conditional
diff --git a/fs/fs/fatlo.fs b/fs/fs/fatlo.fs
@@ -256,7 +256,7 @@ extends _FAT struct[ FATLO
DirEntry SZ + dup V1 _FAT :)buf = if
drop V1 _FAT :nextsector? not if
rootdirentry( ( parses as "lastentry" ) else V1 _FAT :buf( then then
- repeat ( a ) drop rdrop rdrop ;
+ repeat ( a ) drop rfree ;
: :child ( dirid name self -- id-or-0 ) >r
fnbuf! r@ _FAT :getdirentry r@ _FAT :readdir r@ _FAT :findindir
diff --git a/fs/tests/kernel.fs b/fs/tests/kernel.fs
@@ -83,7 +83,7 @@ ll llcnt 3 #eq
42 ' bar emeta findemeta ( ll ) #eq
\ Local variables
-: foo 54 >r 42 >r 1 to+ V1 2 to+ V2 V2 V1 rdrop rdrop ;
+: foo 54 >r 42 >r 1 to+ V1 2 to+ V2 V2 V1 rfree ;
foo 55 #eq 44 #eq
: inc5 >r 5 >r begin 1 to+ V1 next V1 rdrop ;
42 inc5 47 #eq
diff --git a/fs/xcomp/bootlo.fs b/fs/xcomp/bootlo.fs
@@ -62,6 +62,8 @@ code : ] code ] ;
: ^ -1 xor ;
: 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/pc/build.fs b/fs/xcomp/pc/build.fs
@@ -41,7 +41,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
- rdrop rdrop rdrop ;
+ rfree ;
: buildPC ( drv clustercnt -- fat ) over >r \ V1=drv
." creating FAT and copying files" nl>