commit 73cf5d43d5d4dd6833bb89e9205b4391c19ee566
parent 545d769623e91ad9316fdae0d9da00ba41dd6867
Author: Christian Ermann <christianermann@gmail.com>
Date: Wed, 30 Oct 2024 13:48:17 -0700
Redefine 'find' as a high-level word
Diffstat:
| M | forth.s | | | 75 | ++++++++++++++++++++++++++++++++++++++++++++------------------------------- |
1 file changed, 44 insertions(+), 31 deletions(-)
diff --git a/forth.s b/forth.s
@@ -290,26 +290,25 @@ defcode "parse-word", 10, 0, 0xB218226F, parse_word, fib
push x # word len
next
-defcode "find", 4, 0, 0xBDF0855A, find, parse_word
- pop x # length
- pop w # address
- jal hash_impl
- la s3, _latest
- load_cell s3, 0(s3)
-_check_word_hidden:
- lb t0, flag_offset(s3)
- # beq t0, ?, _next_word
-_check_word_hash:
- load_cell t0, hash_offset(s3)
- bne t0, w, _next_word
- j _found_or_out_of_words
-_next_word:
- load_cell s3, 0(s3)
- bnez s3, _check_word_hidden
-_found_or_out_of_words:
- mv w, s3
- push w
- next
+defcode "word>hash", 9, 0, 0x50E0A245, word_to_hash, parse_word
+ pop w # word address
+ load_cell w, hash_offset(w)
+ push w # word hash
+ next
+
+defword "find", 4, 0, 0xBDF0855A, find, word_to_hash
+ .int hash
+ .int latest, fetch
+_find_loop:
+ .int dup, zero_equal, q_branch, _find_check_hash
+ .int swap, drop, exit
+_find_check_hash:
+ .int two_dup
+ .int word_to_hash, equal, q_branch, _find_next_word
+ .int swap, drop, exit
+_find_next_word:
+ .int fetch
+ .int branch, _find_loop
defcode ">cfa", 4, 0, 0x8CAC3233, to_cfa, find
pop w
@@ -367,16 +366,7 @@ _branch_done:
addi ip, ip, cell
next
-defcode "0=", 2, 0, 0x14ED5DD6, zero_equal, q_branch
- pop w
- mv x, zero
- bnez w, _zero_done
- addi x, x, -1
-_zero_done:
- push x
- next
-
-defword "quit", 4, 0, 0x47878736, quit, zero_equal
+defword "quit", 4, 0, 0x47878736, quit, q_branch
_quit_top:
.int refill, drop # TODO: What should happen when 'refill' fails?
.int interpret
@@ -520,10 +510,33 @@ defcode "aligned", 7, 0, 0xC73174DF, aligned, plus
next
# -----------------------------------------------------------------------------
+# comparison
+# -----------------------------------------------------------------------------
+
+defcode "0=", 2, 0, 0x14ED5DD6, zero_equal, aligned
+ pop w
+ mv x, zero
+ bnez w, _zero_done
+ addi x, x, -1
+_zero_done:
+ push x
+ next
+
+defcode "=", 1, 0, 0x380CAD68, equal, zero_equal
+ pop w
+ pop x
+ bne w, x, _equal_not
+ push_imm -1
+ next
+_equal_not:
+ push zero
+ next
+
+# -----------------------------------------------------------------------------
# compiler
# -----------------------------------------------------------------------------
-defword "create", 6, 0, 0x26BB595D, create, aligned
+defword "create", 6, 0, 0x26BB595D, create, equal
.int latest, fetch, comma # link
.int hash, comma # hash
.int lit, 0, comma # meta