Fixups for new word layout, and restructure for using wordlists.
[rrq/rrqforth.git] / rrqforth.asm
1 ; This is a forth interpreter for x86_64 (elf64)
2         format elf64 executable
3         entry main
4
5 include 'machine.asm'
6
7 ;;; ============================================================
8
9         segment readable writable executable
10
11 ;;; This is the very first word
12         
13         ;; FORTH is the last word of WORDLIST FORTH
14         WORD p_forth,'FORTH',dovalue
15         ;; ( -- )
16         ;; Change to use this wordlist
17         dq last_forth_word
18         dq inline_code
19         mov rax,qword [p_forth_DFA]
20         mov qword [p_wordlist],rax
21         popr rsi
22         next
23
24         WORD p_syscall,'SYSCALL',dodoes,,,8
25         ;; ( -- )
26         ;; Change to use this wordlist
27         dq last_syscall_word
28         dq inline_code
29         mov rax,qword [p_syscall_DFA]
30         mov qword [p_wordlist],rax
31         popr rsi
32         next
33
34 last_wordlists_word:
35         WORD p_wordlists,'WORDLISTS',dodoes,,,8
36         ;; ( -- )
37         ;; Change to use this wordlist
38         dq p_wordlists_TFA
39         dq inline_code
40         mov rax,qword [p_wordlists_DFA]
41         mov qword [p_wordlist],rax
42         popr rsi
43         next
44         
45 include 'wordlists.asm'
46
47         WORD return_stack,'RS',dovariable
48         ;; The return stack
49         rb 1048576              ; 1 Mb return stack
50 RS_TOP:                         ; The initial rbp
51         
52         WORD data_stack,'DS',dovariable
53         ;; The data stack
54         rb 1048576              ; 1 Mb data stack
55 DS_TOP:                         ; The initial rsp
56
57         WORD inline_code,'[ASM]',fasm
58         ;; ( -- )
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".
63         jmp qword rsi
64
65         WORD p_exit, 'EXIT',fasm
66         ;; ( -- ) ( R: addr -- )
67         ;; Returns execution to the calling definition as per the
68         ;; return stack.
69 exit:
70         popr rsi
71         next
72
73         ;; TERMINATE0 terminates the program with code 0
74         ;; ( -- )
75         WORD terminate, 'TERMINATE0',fasm
76         pop rdx
77 terminate_special:
78         mov eax,60
79         syscall
80
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
84 ;;; this word.
85 doforth:
86         pushr rsi
87         lea rsi, [rax+8]        ; rsi = the DFA of the rax word
88         next
89
90 ;;; Execution semantics for DOES>
91 ;;; The cell at [cfa-8] holds an adjustment offset.
92 dodoes:
93         pushr rsi
94         lea rsi, [rax+8]        ; rsi = the DFA of the rax word
95         add rsi,[rax-8]         ; adjust rsi to the DOES> part
96         next
97
98         ;; Execution semantics for a variable ( -- addr )
99         ;; rax points to CFA field
100 dovariable:
101         add rax,8
102         push rax
103         next
104
105         ;; Execution semantics for a constant ( -- v )
106         ;; rax points to CFA field
107 dovalue:
108         push qword [rax+8]
109         next
110
111         ;; Execution semantics for a string constant ( -- addr n )
112         ;; rax points to CFA field
113 dostring:
114         cfa2dfa rax
115         pushpname rax
116         next
117
118 include 'memory.asm'
119 include 'stack.asm'
120 include 'math.asm'
121 include 'stdio.asm'
122
123         WORD p_program_version,'PROGRAM_VERSION',dostring
124         STRING 'RRQ Forth version 0.1 - 2021-05-13',10
125
126         WORD p_stdin,'STDIN',dovalue
127         ;; Initialised to hold a STREAM for fd 0
128         dq 0
129
130 ;;; The main entry point.
131 ;;; This word is also the last word before syscalls
132 last_forth_word:
133         WORD p_quit,'QUIT',fasm
134         ;; QUIT is the program entry point ********************
135 main:
136         mov rsp,DS_TOP
137         mov rbp,RS_TOP
138         ;; Initialize STREAM STDIN
139         push 0
140         push 10000
141         DOFORTH p_stream
142         pop qword [p_stdin_DFA]
143
144         ;; Initial blurb
145         push qword 1                    ; stdout
146         DOFORTH p_program_version       ; version string => ( s n )
147         DOFORTH sys_write               ; printout
148         pop rax                         ; ignore errors
149
150         DOFORTH p_words
151         
152         push 0
153         DOFORTH sys_exit
154
155         previous_word = last_wordlists_word
156
157 include 'syscalls.asm'
158
159         
160 last_word:
161         
162 heap_start: