commit 54158ea7afd54a152fccc46810b0e904678cbf09
parent 3c0dbda9fefcd754c490582eb7a627727c0dd73b
Author: Virgil Dupras <hsoft@hardcoded.net>
Date: Sat, 18 Mar 2023 16:49:44 -0400
halcc: HAL-ify switch lookup
Still a bit slower than pure native lookup, but a lot more acceptable than the
previous forth version.
Diffstat:
1 file changed, 14 insertions(+), 13 deletions(-)
diff --git a/fs/comp/c/vm/forth.fs b/fs/comp/c/vm/forth.fs
@@ -9,7 +9,6 @@
: PS- CELLSZ neg to+ psoff ;
: halsz! ( operand sz -- operand )
case 1 of = 8b) endof 2 of = 16b) endof endcase ;
-: compilesz [compile] ['] compile findmod compile execute, ; immediate
struct+[ VMOp
: _compile ( arg loc -- ) \ compile "straight" operands, errors on * ops.
@@ -38,8 +37,8 @@ struct+[ VMOp
: :*op dup :locptr? if dup :compile dup :>reg then VMOp :*op ;
: :*n ( n self -- ) tuck :compile litn compile * :>reg ;
: :/n ( n self -- ) tuck :compile litn compile / :>reg ;
- : :+n ( n self -- ) tuck :compile litn compile + :>reg ;
- : :&n ( n self -- ) tuck :compile litn compile and :>reg ;
+ : :+n ( n self -- ) tuck :compile W+n, :>reg ;
+ : :&n ( n self -- ) tuck :compile andn, :>reg ;
: :>res dup :compile :>reg ;
]struct
@@ -155,14 +154,16 @@ LOGOPCNT wordtbl _tblunsigned
compile drop PS- vmop :compile vmop :init
]vmjmp vmop :>reg ;
-: _lookup ( nref lookup -- )
- [ -4 [rcnt] ! ] \ V1=return addr
- @+ ?dup if rot >r dup >r for ( a ) \ V2=nref V3=totcnt
- @+ V2 = if ( a )
- \ match! we jump to a blind spot by dropping the return address of this
- \ call and replacing it with our target.
- V3 1- CELLSZ * + @ ( tgtaddr ) to V1 break then
- next ( a ) drop then 2rdrop
- else ( nref lookup ) 2drop then ;
+code _lookup ( nref lookup -- )
+ W>A, A) @,
+ -8 rs+, RSP) 4 +) !, 0 LIT>W, RSP) !, begin \ RS+0=i RS+4=totcnt
+ RSP) @, RSP) 4 +) cmp, 0 NZ) branchC, \ not found
+ 8 rs+, nip, drop, exit, then
+ 1 RSP) [+n], CELLSZ A+n, A) @,
+ PSP) cmp, NZ) branchC, drop \ Z=match
+ \ we have a match, add totcnt*CELLSZ to A, dereference. that's our target.
+ RSP) 4 +) @, 2 <<n, RSP) !, W<>A, RSP) +, W) @, \ W=target
+ 12 rs+, W>A, nip, drop, branchA,
+
: vmswitch, ( 'lookup -- )
- _compileFinal litn compile @ compile _lookup ;
+ _compileFinal litn W) @, compile _lookup ;