1 ; This is a forth interpreter for x86_64 (elf64)
2 format elf64 executable
7 ;;; ============================================================
9 segment readable writable executable
11 ;;; This is the very first word
13 ;; FORTH is the last word of WORDLIST FORTH
14 WORD p_forth,'FORTH',dovalue
16 ;; Change to use this wordlist
19 mov rax,qword [p_forth_DFA]
20 mov qword [p_wordlist],rax
24 WORD p_syscall,'SYSCALL',dodoes,,,8
26 ;; Change to use this wordlist
29 mov rax,qword [p_syscall_DFA]
30 mov qword [p_wordlist],rax
35 WORD p_wordlists,'WORDLISTS',dodoes,,,8
37 ;; Change to use this wordlist
40 mov rax,qword [p_wordlists_DFA]
41 mov qword [p_wordlist],rax
45 include 'wordlists.asm'
47 WORD return_stack,'RS',dovariable
49 rb 1048576 ; 1 Mb return stack
50 RS_TOP: ; The initial rbp
52 WORD data_stack,'DS',dovariable
54 rb 1048576 ; 1 Mb data stack
55 DS_TOP: ; The initial rsp
57 WORD inline_code,'[ASM]',fasm
59 ;; This transitions execution into inline assembler in the
60 ;; calling word defintion. Note that it stops advancing rsi;
61 ;; code should use FORTH macro to reenter forth execution, or
62 ;; exit to the calling definition via "jmp exit".
65 WORD p_exit, 'EXIT',fasm
66 ;; ( -- ) ( R: addr -- )
67 ;; Returns execution to the calling definition as per the
73 ;; TERMINATE0 terminates the program with code 0
75 WORD terminate, 'TERMINATE0',fasm
81 ;;; Execution semantics for FORTH defition word
82 ;;; At entry, rsi points into the calling definition, at the cell
83 ;;; following the cell indicating this word, rax points to the CFA of
87 lea rsi, [rax+8] ; rsi = the DFA of the rax word
90 ;;; Execution semantics for DOES>
91 ;;; The cell at [cfa-8] holds an adjustment offset.
94 lea rsi, [rax+8] ; rsi = the DFA of the rax word
95 add rsi,[rax-8] ; adjust rsi to the DOES> part
98 ;; Execution semantics for a variable ( -- addr )
99 ;; rax points to CFA field
105 ;; Execution semantics for a constant ( -- v )
106 ;; rax points to CFA field
111 ;; Execution semantics for a string constant ( -- addr n )
112 ;; rax points to CFA field
123 WORD p_program_version,'PROGRAM_VERSION',dostring
124 STRING 'RRQ Forth version 0.1 - 2021-05-13',10
126 WORD p_stdin,'STDIN',dovalue
127 ;; Initialised to hold a STREAM for fd 0
130 ;;; The main entry point.
131 ;;; This word is also the last word before syscalls
133 WORD p_quit,'QUIT',fasm
134 ;; QUIT is the program entry point ********************
138 ;; Initialize STREAM STDIN
142 pop qword [p_stdin_DFA]
145 push qword 1 ; stdout
146 DOFORTH p_program_version ; version string => ( s n )
147 DOFORTH sys_write ; printout
148 pop rax ; ignore errors
155 previous_word = last_wordlists_word
157 include 'syscalls.asm'