From c7bd2072c9a131bff7f03f36fefd65f7b6f60f02 Mon Sep 17 00:00:00 2001 From: Ralph Ronnquist Date: Wed, 19 May 2021 00:55:35 +1000 Subject: [PATCH] started compiler words --- compile.asm | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++ rrqforth.asm | 21 ++++++++++----- 2 files changed, 91 insertions(+), 6 deletions(-) create mode 100644 compile.asm diff --git a/compile.asm b/compile.asm new file mode 100644 index 0000000..1e76aee --- /dev/null +++ b/compile.asm @@ -0,0 +1,76 @@ +;;; Words for adding words + + WORD p_here,'HERE',dovariable + ;; The heap + dq heap_start + + ;; CREATE ( "[ \t\0]*([^ \t\0]+)" -- tfa ) + ;; Skip leading whitespace and scan following non-whitespace + ;; as being a "word" to add into the current vocabulary. Add + ;; that word header, which consisits of: + WORD p_create,'CREATE',fasm + pushr rsi + push p_stdin_DFA ; ( -- stream ) + DOFORTH p_read_word ; ( -- chars* n ) read next word + mov rax,qword [p_wordlist_DFA] ; Current word list + mov rax,[rax] ; last word of current wordlist + mov rbx,qword [p_here_DFA] + mov [rbx],rax ; TFA of new word + mov qword [rbx+16],0 ; flags field + ;; copy pname + pop rcx ; n + mov qword [rbx+24],rcx ; PFA (length) + pop rsi ; chars* (source) + lea rdi,[rbx+32] ; (dest) + ;; clear DF +p_create_COPY: + movsb + dec rcx + jge p_create_COPY + mov byte [rdi],0 ; extra NUL + inc rdi + mov qword [rdi],rbx ; pTFA + add rdi,8 + mov qword [rdi],rbx ; OFF + add rdi,8 + mov qword [rbx+8],rdi ; pCFA + add rdi,8 + mov qword [rdi],dovalue ;CFA + add rdi,8 + mov qword [rax],rbx ; Install new word + mov qword [p_here_DFA],rdi ; allocate the space + push rbx + popr rsi + next + + WORD p_allot,'ALLOT',fasm + ;; ( n -- ) + ;; Allocate n bytes on the heap + pop rax + add rax,qword [p_here_DFA] + mov qword [p_here_DFA],rax + next + + ;; DOES> ( -- ) + ;; Change DOES offset of latest compilation word to current + ;; compilation address. + ;; LATEST @ TFA>DOES HERE @ OVER - SWAP ! + WORD p_does,'DOES>',fasm + mov rax,qword [p_wordlist_DFA] + mov rax,[rax] ; last word of current wordlist + add rax,8 + ;add rax,byte [rax] + add rax,1 + mov rbx,[p_here_DFA] + mov qword [rax],rbx + mov qword [rax+8],dodoes + next + + WORD p_literal,'LIT',IMMEDIATE + dq p_create + dq p_does + dq p_get + dq p_exit + + WORD p_execute,'EXECUTE' + diff --git a/rrqforth.asm b/rrqforth.asm index ba41cbe..16dc7d6 100644 --- a/rrqforth.asm +++ b/rrqforth.asm @@ -119,6 +119,7 @@ include 'memory.asm' include 'stack.asm' include 'math.asm' include 'stdio.asm' +include 'compile.asm' WORD p_program_version,'PROGRAM_VERSION',dostring STRING 'RRQ Forth version 0.1 - 2021-05-13',10 @@ -142,12 +143,17 @@ main: pop qword [p_stdin_DFA] ;; Initial blurb - push qword 1 ; stdout - DOFORTH p_program_version ; version string => ( s n ) - DOFORTH sys_write ; printout - pop rax ; ignore errors - - DOFORTH p_words + FORTH + dq p_program_version + dq p_tell + dq p_stdin + dq p_read_word + dq p_tell + dq p_nl + dq p_emit + ENDFORTH + + ;; DOFORTH p_words push 0 DOFORTH sys_exit @@ -160,3 +166,6 @@ include 'syscalls.asm' last_word: heap_start: + rb 1048576 ; +1 Mb heap + rb 1048576 ; +1 Mb heap + rb 1048576 ; +1 Mb heap -- 2.39.2