1 ; This is a forth interpreter for x86_64 (elf64)
2 format elf64 executable
7 ;;; ============================================================
9 segment readable writable executable
11 WORD return_stack,'RS',dovariable
13 rb 1048576 ; 1 Mb return stack
14 RS_TOP: ; The initial rbp
16 WORD data_stack,'DS',dovariable
18 rb 1048576 ; 1 Mb data stack
19 DS_TOP: ; The initial rsp
21 WORD inline_code,'[ASM]',fasm
23 ;; This transitions execution into inline assembler in the
24 ;; calling word defintion. Note that it stops advancing rsi;
25 ;; code should use FORTH macro to reenter forth execution, or
26 ;; exit to the calling definition via "jmp exit".
29 WORD p_exit, 'EXIT',fasm
30 ;; ( -- ) ( R: addr -- )
31 ;; Returns execution to the calling definition as per the
37 ;;; Execution semantics for FORTH defition word
38 ;;; At entry, rsi points into the calling definition, at the cell
39 ;;; following the cell indicating this word, rax points to the CFA of
43 lea rsi, [rax+8] ; rsi = the DFA of the rax word
46 ;;; Execution semantics for DOES>
47 ;;; The cell at [cfa-8] holds an adjustment offset.
50 lea rsi, [rax+8] ; rsi = the DFA of the rax word
51 add rsi,[rax-8] ; adjust rsi to the DOES> part
54 ;; Execution semantics for a variable ( -- addr )
55 ;; rax points to CFA field
61 ;; Execution semantics for a constant ( -- v )
62 ;; rax points to CFA field
67 ;; Execution semantics for a string constant ( -- addr n )
68 ;; rax points to CFA field
78 include 'wordlists.asm'
79 include 'syscalls.asm'
85 WORD p_program_version,'PROGRAM_VERSION',dostring
86 STRING 'RRQ Forth version 0.1 - 2021-05-13',10
88 WORD p_stdin,'STDIN',dovalue
89 ;; Initialised to hold a STREAM for fd 0
92 WORD p_quit,'QUIT',fasm
93 ;; QUIT is the program entry point ********************
97 ;; Initialize STREAM STDIN
101 pop qword [p_stdin_DFA]
104 push qword 1 ; ( fd ) =stdout
105 push qword [p_stdin_DFA]
107 dq p_read_word ; ( fd s n )
111 push qword 1 ; stdout
112 DOFORTH p_program_version ; version string => ( s n )
113 DOFORTH sys_write ; printout
114 pop rax ; ignore errors
119 ;; TERMINATE0 terminates the program with code 0
121 WORD terminate, 'TERMINATE',fasm
128 ;; FORTH is the last word of VOCABULARY FORTH
129 WORD forth,'FORTH',dovalue