From ab545d1d4c68b46af77ed241cfba6ada83bbb459 Mon Sep 17 00:00:00 2001
From: Ralph Ronnquist <ralph.ronnquist@gmail.com>
Date: Tue, 18 May 2021 22:21:26 +1000
Subject: [PATCH] Fixups for new word layout, and restructure for using
 wordlists.

---
 rrqforth.asm  | 75 +++++++++++++++++++++++++++++++++-------------
 syscalls.asm  | 29 +++++++++---------
 wordlists.asm | 82 ++++++++++++++++++++++++++++++++++++++++-----------
 3 files changed, 133 insertions(+), 53 deletions(-)

diff --git a/rrqforth.asm b/rrqforth.asm
index 0f7e8fb..ba41cbe 100644
--- a/rrqforth.asm
+++ b/rrqforth.asm
@@ -8,6 +8,42 @@ include 'machine.asm'
 
 	segment readable writable executable
 
+;;; This is the very first word
+	
+	;; FORTH is the last word of WORDLIST FORTH
+	WORD p_forth,'FORTH',dovalue
+	;; ( -- )
+	;; Change to use this wordlist
+	dq last_forth_word
+	dq inline_code
+	mov rax,qword [p_forth_DFA]
+	mov qword [p_wordlist],rax
+	popr rsi
+	next
+
+	WORD p_syscall,'SYSCALL',dodoes,,,8
+	;; ( -- )
+	;; Change to use this wordlist
+	dq last_syscall_word
+	dq inline_code
+	mov rax,qword [p_syscall_DFA]
+	mov qword [p_wordlist],rax
+	popr rsi
+	next
+
+last_wordlists_word:
+	WORD p_wordlists,'WORDLISTS',dodoes,,,8
+	;; ( -- )
+	;; Change to use this wordlist
+	dq p_wordlists_TFA
+	dq inline_code
+	mov rax,qword [p_wordlists_DFA]
+	mov qword [p_wordlist],rax
+	popr rsi
+	next
+	
+include 'wordlists.asm'
+
 	WORD return_stack,'RS',dovariable
 	;; The return stack
 	rb 1048576		; 1 Mb return stack
@@ -34,6 +70,14 @@ exit:
 	popr rsi
 	next
 
+	;; TERMINATE0 terminates the program with code 0
+	;; ( -- )
+	WORD terminate, 'TERMINATE0',fasm
+	pop rdx
+terminate_special:
+	mov eax,60
+	syscall
+
 ;;; Execution semantics for FORTH defition word
 ;;; At entry, rsi points into the calling definition, at the cell
 ;;; following the cell indicating this word, rax points to the CFA of
@@ -67,16 +111,10 @@ dovalue:
 	;; Execution semantics for a string constant ( -- addr n )
 	;; rax points to CFA field
 dostring:
-	add rax,8
-	xor rbx,rbx
-	mov bl,[rax]
-	inc rax
-	push rax
-	push rbx
+	cfa2dfa rax
+	pushpname rax
 	next
 
-include 'wordlists.asm'
-include 'syscalls.asm'
 include 'memory.asm'
 include 'stack.asm'
 include 'math.asm'
@@ -88,7 +126,10 @@ include 'stdio.asm'
 	WORD p_stdin,'STDIN',dovalue
 	;; Initialised to hold a STREAM for fd 0
 	dq 0
-	
+
+;;; The main entry point.
+;;; This word is also the last word before syscalls
+last_forth_word:
 	WORD p_quit,'QUIT',fasm
 	;; QUIT is the program entry point ********************
 main:
@@ -111,19 +152,11 @@ main:
 	push 0
 	DOFORTH sys_exit
 
-	;; TERMINATE0 terminates the program with code 0
-	;; ( v -- )
-	WORD terminate, 'TERMINATE',fasm
-	pop rdx
-terminate_special:
-	mov eax,60
-	syscall
+	previous_word = last_wordlists_word
 
-last_word:
-	;; FORTH is the last word of VOCABULARY FORTH
-	WORD forth,'FORTH',dovalue
-	dq forth_TFA
-	dq 0
+include 'syscalls.asm'
 
 	
+last_word:
+	
 heap_start:
diff --git a/syscalls.asm b/syscalls.asm
index ede8849..aeb3041 100644
--- a/syscalls.asm
+++ b/syscalls.asm
@@ -356,17 +356,18 @@ end if
 	SYSCALL 319,sys_memfd_create,'SYS_MEMFD_CREATE',2
 	SYSCALL 320,sys_kexec_file_load,'SYS_KEXEC_FILE_LOAD',5
 	SYSCALL 321,sys_bpf,'SYS_BPF',3
-	SYSCALL 322,stub_execveat,'STUB_EXECVEAT',5
-	SYSCALL 323,userfaultfd,'USERFAULTFD',1
-	SYSCALL 324,membarrier,'MEMBARRIER',2
-	SYSCALL 325,mlock2,'MLOCK2',3
-	SYSCALL 326,copy_file_range,'COPY_FILE_RANGE',6
-	SYSCALL 327,preadv2,'PREADV2',6
-	SYSCALL 328,pwritev2,'PWRITEV2',6
-	SYSCALL 329,pkey_mprotect,'PKEY_MPROTECT',0
-	SYSCALL 330,pkey_alloc,'PKEY_ALLOC',0
-	SYSCALL 331,pkey_free,'PKEY_FREE',0
-	SYSCALL 332,statx,'STATX',0
-	SYSCALL 333,io_pgetevents,'IO_PGETEVENTS',0
-	SYSCALL 334,rseq,'RSEQ',0
-	SYSCALL 335,pkey_mprotect1,'PKEY_MPROTECT1',0
+	SYSCALL 322,sys_stub_execveat,'SYS_STUB_EXECVEAT',5
+	SYSCALL 323,sys_userfaultfd,'SYS_USERFAULTFD',1
+	SYSCALL 324,sys_membarrier,'SYS_MEMBARRIER',2
+	SYSCALL 325,sys_mlock2,'SYS_MLOCK2',3
+	SYSCALL 326,sys_copy_file_range,'SYS_COPY_FILE_RANGE',6
+	SYSCALL 327,sys_preadv2,'SYS_PREADV2',6
+	SYSCALL 328,sys_pwritev2,'SYS_PWRITEV2',6
+	SYSCALL 329,sys_pkey_mprotect,'SYS_PKEY_MPROTECT',0
+	SYSCALL 330,sys_pkey_alloc,'SYS_PKEY_ALLOC',0
+	SYSCALL 331,sys_pkey_free,'SYS_PKEY_FREE',0
+	SYSCALL 332,sys_statx,'SYS_STATX',0
+	SYSCALL 333,sys_io_pgetevents,'SYS_IO_PGETEVENTS',0
+	SYSCALL 334,sys_rseq,'SYS_RSEQ',0
+last_syscall_word:
+	SYSCALL 335,sys_pkey_mprotect1,'SYS_PKEY_MPROTECT1',0
diff --git a/wordlists.asm b/wordlists.asm
index ae3062b..4862dd1 100644
--- a/wordlists.asm
+++ b/wordlists.asm
@@ -1,34 +1,74 @@
 ;;; This file contains words dealing with word lists (vocabularies)
+;;;
+;;; CURRENT-WORDLIST (variable) points out "the current wordlist"
+;;; SYSCALLS is a wordl list
+;;; FORTH is a word list
+;;;
+;;; !! When a word list word is created, it gets added to the tail end
+;;; of the current word list as way of making all word list words be
+;;; present in all word lists. This is different to all other kinds of
+;;; words, which instead are added to the head end of the current word
+;;; list.
+;;;
+;;; !! A word list word is created, it is initialised to the head end
+;;; of the current word list. It will this be an extension to that
+;;; current word list.
+;;;
+;;; EMPTY-WORDLIST is a word list word for an empty word list. It thus
+;;; only contains word list words.
+;;; 
+;;; WORDLIST ( "name" -- ) = start
 
-	WORD p_wordlist,'WORDLIST',dovariable
-	;; VARIABLE WORDLIST is the currently active wordlist.
-	dq last_word
+	WORD p_tfa2cfa,'TFA>CFA',fasm
+	;; ( tfa -- cfa )
+	;; Advance a word tfa pointer to the cfa field
+	mov rax,qword[rsp]
+	tfa2cfa rax
+	mov qword [rsp],rax
+	next
+	
+	WORD p_tfa2dfa,'TFA>DFA',fasm
+	;; ( tfa -- dfa )
+	;; Advance a word tfa pointer to the dfa field
+	mov rax,qword[rsp]
+	tfa2dfa rax
+	mov qword [rsp],rax
+	next
+
+	WORD p_dfa2tfa,'DFA>TFA',fasm
+	;; ( dfa -- tfa )
+	;; Advance a word tfa pointer to the dfa field
+	mov rax,qword[rsp]
+	mov rax,qword [rax-24]	; tfa
+	mov qword [rsp],rax
+	next
+
+	WORD p_wordlist,'CURRENT-WORDLIST',dovariable
+	;; CURRENT-WORDLIST points to cfa of the currently active wordlist.
+	dq p_forth_DFA
 
 	WORD p_words,'WORDS',fasm
 	;; ( -- )
 	;; Dump all words
 	pushr rsi
-	push qword [p_wordlist_DFA]
+	mov rax,qword [p_wordlist_DFA] ; current wordlist word
+	mov rax,[rax]		       ; list start
+	sub rsp,8		       ; use stack to hold ptr
 p_words_LOOP:
-	mov rax,qword [rsp]
+	mov qword [rsp],rax
 	cmp rax,0
 	je p_words_END
 	;; tfa>pfa
-	add rax,9
-	;; pfa@ => ( chars* length)
-	xor rbx,rbx
-	mov bl,[rax]
-	inc rax
+	tfa2pfa rax
 	push 1
-	push rax
-	push rbx
+	;; pfa@ => ( chars* length)
+	pushpname rax
 	DOFORTH sys_write
 	pop rax			; ignore errors
-	push 10
+	push qword 10
 	DOFORTH p_emit
 	mov rax,qword [rsp]
 	mov rax,qword [rax]
-	mov qword [rsp],rax
 	jmp p_words_LOOP
 
 p_words_END:
@@ -61,24 +101,29 @@ p_strncmp_end:
 	WORD p_find,'FIND'
 	;; ( chars length -- [ chars 0 | cfa 1 )
 	;; Search the current wordlists for the given pname
+	pushr rsi
 	mov rcx,[p_wordlist_DFA]
+	mov rcx,qword [rcx]
+	mov rbx,[rsp]
+	mov rax,[rsp+8]
 p_find_loop:
 	cmp rcx,0
 	je p_find_done
-	mov rbx,[rsp]
-	mov rax,[rsp+8]
-	cmp bl,byte [rcx+10]
+	cmp rbx,qword [rcx+16]	; compare lengths
 	jne p_find_nextword
 	push rcx
 	;; check word
 	push rax
-	push rcx+11
+	tfa2pname rcx
+	push rcx
 	push rbx
 	DOFORTH p_strncmp
 	pop rax			; return value
 	pop rcx
 	cmp rax,0
 	je p_find_done
+	mov rbx,[rsp]
+	mov rax,[rsp+8]
 p_find_nextword:
 	mov rcx,[rcx]
 	jmp p_find_loop
@@ -87,4 +132,5 @@ p_find_found:
 	mov rcx,1
 p_find_done:
 	push rcx
+	popr rsi
 	next
-- 
2.39.5