Commit Diff


commit - 58a4e61d0f6350f28132a9e5e8a09351f9c0c3e0
commit + 9fbaab72c1953893040111708affa2b75702cf87
blob - 307867f6704ca91602381a4850ba4437f4c470ae
blob + 187d8c4683bbfe89f50fe21c75f516a4d549fb5b
--- rvforth.S
+++ rvforth.S
@@ -165,57 +165,14 @@ _MEMEQ:
 	j _MEMEQ
 
 1:	// equal
-	li a0, 1
+	li a0, -1
 	jr ra
 
 2:	// not equal
 	mv a0, zero
 	jr ra
 .size _MEMEQ, . - _MEMEQ	
-
-// a0 - name
-// a1 - namelen
-_FIND:
-	addi sp, sp, -32
-	sd ra, 0(sp)
-	sd s0, 8(sp)
-	sd s1, 16(sp)
-	sd s2, 24(sp)
-
-	mv s0, a0	// name
-	mv s1, a1	// namelen
-	la s2, var_LATEST
-	j 2f
-	
-1:
-	lbu a2, 8(s2)
-	andi t0, a2, F_HIDDEN
-	bne t0, zero, 2f
-	andi a2, a2, F_LENMASK
-	bne a2, s1, 2f
-
-	mv a0, s0
-	addi a1, s2, 9
-	jal _MEMEQ
-	beq a0, zero, 2f
 
-	j 3f
-
-2:
-	ld s2, 0(s2)
-	bne s2, zero, 1b
-
-3:
-	mv a0, s2
-
-	ld s2, 24(sp)
-	ld s1, 16(sp)
-	ld s0, 8(sp)
-	ld ra, 0(sp)
-	addi sp, sp, 32
-	jr ra
-.size _FIND, . - _FIND
-
 // return:
 //   a0 - word
 //   a1 - wordlen
@@ -453,14 +410,55 @@ defcode "WORD", 4, WORD, KEY
 	PUSH a0
 	NEXT
 
-// ( name namelen -- *word )
-defcode "FIND", 4, FIND, WORD
+defcode "MEMEQ", 5, MEMEQ, WORD
 	POP a0
 	POP a1
-	jal _FIND
+	POP a2
+	jal _MEMEQ
 	PUSH a0
 	NEXT
 
+// ( name namelen -- &word )
+defword "FIND", 4, FIND, MEMEQ
+	.quad TOR		// ( namelen -- name )
+	.quad TOR		// ( -- namelen name )
+	.quad LATEST		// ( &LATEST -- namelen name )
+	.quad FETCH		// ( LATEST -- namelen name )
+
+1:	// ( &word -- namelen name )
+	.quad DUP		// ( &word &word -- namelen name )
+	.quad LIT, 8		// ( 8 &word &word -- namelen name )
+	.quad ADD		// ( &flags &word -- namelen name )
+	.quad FETCHBYTE		// ( flags &word -- namelen name )
+	.quad LIT, F_HIDDEN	// ( F_HIDDEN flags &word -- namelen name )
+	.quad AND		// ( hidden &word -- namelen name )
+	GOTONZ 2f		// ( &word -- namelen name )
+
+	.quad DUP		// ( &word &word -- namelen name )
+	.quad LIT, 9		// ( 9 &word &word -- namelen name )
+	.quad ADD		// ( &wname &word -- namelen name )
+
+	.quad RSPFETCH		// ( &namelen wname &word -- namelen name )
+	.quad FETCH		// ( namelen wname &word -- namelen name )
+	.quad SWAP		// ( &wname namelen &word -- namelen name )
+	.quad RSPFETCH		// ( RSP wname namelen &word -- namelen name )
+	.quad LIT, 8		// ( 8 RSP wname namelen &word -- namelen name )
+	.quad ADD		// ( &name wname namelen &word -- namelen name )
+	.quad FETCH		// ( name wname namelen &word -- namelen name )
+	.quad MEMEQ		// ( eql &word -- namelen name )
+	GOTOZ 2f		// ( &word -- namelen name )
+	
+3:
+	.quad RDROP		// ( &word -- name )
+	.quad RDROP		// ( &word )
+	.quad EXIT		// noreturn
+
+2:	// hidden or not equal	// ( &word -- namelen name )
+	.quad FETCH		// ( &next -- namelen name )
+	.quad DUP		// ( &next &next -- namelen name )
+	GOTONZ 1b		// ( &next -- namelen name )
+	GOTO 3b			// ( 0 -- namelen name )
+
 defcode "BRANCH", 6, BRANCH, FIND
 	ld t0, 0(PC)
 	add PC, PC, t0