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 ;;; Execution semantics for FORTH defition word
30 ;;; At entry, rsi points into the calling definition, at the cell
31 ;;; following the cell indicating this word, rax points to the CFA of
35 lea rsi, [rax+8] ; rsi = the DFA of the rax word
38 WORD p_exit, 'EXIT',fasm
39 ;; ( -- ) ( R: addr -- )
40 ;; Returns execution to the calling definition as per the
46 ;; Execution semantics for a variable ( -- addr )
47 ;; rax points to doer field
52 ;; Execution semantics for a constant ( -- v )
53 ;; rax points to doer field
58 ;; Execution semantics for a string constant ( -- addr n )
59 ;; rax points to doer field
67 include 'wordlists.asm'
68 include 'syscalls.asm'
74 WORD p_program_version,'PROGRAM_VERSION',dostring
76 program_version_string:
77 db 'RRQ Forth version 0.1 - 2021-05-13',10
78 length = $ - program_version_string
80 WORD p_stdin,'STDIN',dovalue
81 ;; Initialised to hold a STREAM for fd 0
84 WORD p_quit,'QUIT',fasm
85 ;; QUIT is the program entry point ********************
89 ;; Initialize STREAM STDIN
93 pop qword [p_stdin_DFA]
96 push qword 1 ; ( fd ) =stdout
97 push qword [p_stdin_DFA]
99 dq p_read_word ; ( fd s n )
103 push qword 1 ; stdout
104 push qword program_version_string ; address of string
105 push qword length ; length of string (cheating)
106 DOFORTH sys_write ; printout
107 pop rax ; ignore errors
112 ;; TERMINATE0 terminates the program with code 0
114 WORD terminate, 'TERMINATE',fasm
121 ;; FORTH is the last word of VOCABULARY FORTH
122 WORD forth,'FORTH',dovalue