pack up forth ptrs for more compact source
[rrq/rrqforth.git] / machine.asm
1 ;;; This file define/describes the "machine"
2 ;;;
3 ;;; Abstract Machine:
4 ;;; https://en.wikibooks.org/wiki/X86_Assembly/X86_Architecture
5 ;;; 
6 ;;; General Purpose Registers ( * marks those used in syscalls )
7 ;;;  *rax = ( -, eax = ( -, ax = ( ah, al) )) "accumulator"
8 ;;;   rbx = ( -, ebx = ( -, bx = ( bh, bl) )) "base"
9 ;;;  *rcx = ( -, ecx = ( -, cx = ( ch, cl) )) "counter"
10 ;;;  *rdx = ( -, edx = ( -, dx = ( dh, dl) )) "data"
11 ;;;   rsp = ( -, esp = ( -, sp = ( -, spl) )) "stack pointer"
12 ;;;   rbp = ( -, ebp = ( -, bp = ( -, bpl) )) "stack base pointer"
13 ;;;  *rsi = ( -, esi = ( -, si = ( -, sil) )) "source"
14 ;;;  *rdi = ( -, edi = ( -, di = ( -, dil) )) "destination"
15 ;;;  *r8
16 ;;;  *r9
17 ;;;   r10
18 ;;;  *r11
19 ;;;   r12
20 ;;;   r13
21 ;;;   r14
22 ;;;   r15
23 ;;; clobbers rdi rsi rdx rcx r8 r9 r11
24 ;;; rax = syscall id
25 ;;;
26 ;;; Segment Registers
27 ;;;   SS "Stack Segment"
28 ;;;   CS "Code Segment"
29 ;;;   DS "Data Segment"
30 ;;;   ES "Extra Segment"
31 ;;;   FS "more Extra Segment"
32 ;;;   GS "more more Extra Segment"
33 ;;;
34 ;;; EFLAGS Register
35 ;;;   0,0,0,0,0,0,0,0,0,0,ID,VIP,VIF,AC,VM,RF,
36 ;;;   0,NT,[IOPL,IOPL],OF,DF,IF,TF,SF,ZF,0,AF,0,PF,1,CF
37 ;;; 
38 ;;; Instruction pointer
39 ;;;   EIP
40 ;;; 
41 ;;; Syscall allocations
42 ;;; clobbers rdi rsi rdx rcx r8 r9 r11
43 ;;; rax = syscall id
44 ;;;
45
46 ;;; ######################################################################
47
48 ;;; ============================================================
49 ;;; FORTH machine model
50 ;;; rsp = data stack pointer
51 ;;; rbp = return stack pointer
52 ;;; rsi = instruction pointer
53
54 ;;; ========================================
55 ;;; The pushr macro pushes x onto the return stack
56 ;;; The popr macro pops x from the return stack
57 macro pushr x {
58         sub rbp, 8
59         mov [rbp], x
60 }
61
62 macro popr x {
63         mov x, [rbp]
64         add rbp, 8
65 }
66
67 ;;; ========================================
68 ;;; The next macro "moves" execution to the next FORTH instruction,
69 ;;; using rsi as instruction pointer. It points to the doer field of a
70 ;;; word, which points to the assembly code that implements the
71 ;;; execution effect of the word. That doer code is entered with rsi
72 ;;; referring to the subsequent address in the colling word, and rax
73 ;;; referring to the doer field of the called word.
74
75 macro next {
76         lodsq                   ; mov rax, [rsi] + add rsi,8
77         jmp qword [rax]         ; goto code of that FORTH word (64 bit jump)
78 }
79
80 ;;; ========================================
81 ;;; The FORTH macro transitions to inline FORTH execution.
82 macro FORTH {
83         local forthcode
84         mov rsi,forthcode
85         next
86         ;; align 8
87 forthcode:
88 }
89
90 ;;; ========================================
91 ;;; The ENDFORTH macro transitions back to inline assembler after FORTH
92
93 macro ENDFORTH {
94         dq inline_code
95 }
96
97 ;;; ========================================
98 ;;; The DOFORTH lays out a single FORTH call
99
100 macro DOFORTH [label] {
101 common
102         FORTH
103 forward
104         dq label
105 common
106         ENDFORTH
107 }
108
109 ;;; ========================================
110 ;;; Macro WORD starts a FORTH word definition in this code.
111 ;;; The layout of a word is as follows:
112 ;;; TFA: [8 bytes] pointer to previous word in the word list
113 ;;;      [8 bytes] pointer to the word's CFA
114 ;;;      [8 bytes] a flags field
115 ;;;      [8 bytes] the length of the word's pname
116 ;;;      [varying] the word's pname
117 ;;;      [1 byte]  NUL -- making an asciiz of the pname
118 ;;;      ;;[? bytes] 0-7 bytes for address alignment to [disabled]
119 ;;;      [8 bytes] pointer to the word's TFA
120 ;;; OFF: [8 bytes] the DOES offset for the word
121 ;;; CFA: [8 bytes] pointer to the word's "doer" code
122 ;;; DFA: [? bytes] the word's data field
123
124 IMMEDIATE = 1           ; optional flag (symbol)
125
126 macro WORD label, name, doer, flags, previous, offset {
127         local pname
128         ;; align 8
129 label#_TFA:
130         ;; TFA
131         if previous eq
132             dq previous_word
133         else
134             dq previous
135         end if
136         previous_word = label#_TFA
137         ;; PFA
138 label#_pCFA:
139         dq label#_CFA           ; link to CFA of word
140         dq flags + 0
141 label#_PFA:
142         dq pname - $ - 8
143         db name
144 pname:  db 0                    ; extra NUL byte
145         ;; align 8
146 label#_pTFA:
147         dq label#_TFA           ; link to TFA of word
148 label#_OFF:
149         dq offset + 0           ; The DOES offset. Defaults to 0.
150         ;; also CFA = pointer to "doer"
151 label#_CFA:
152 label:
153         if doer eq
154             dq doforth
155         else
156             if doer in <fasm>
157                 dq dofasm ; label#_DFA
158             else
159                 dq doer
160             end if
161         end if
162         ;; DFA
163 label#_DFA:
164 }
165
166 macro tfa2cfa reg {
167         mov reg,qword [reg+8]
168 }
169 macro tfa2does reg {
170         tfa2cfa reg
171         sub reg,8
172 }
173 macro tfa2dfa reg {
174         tfa2cfa reg
175         add reg,8
176 }
177 macro tfa2flags reg {
178         add reg,16
179 }
180 macro tfa2pfa reg {
181         add reg,24
182 }
183 macro tfa2pname reg {
184         add reg,32
185 }
186 macro cfa2tfa reg {
187         sub reg,16
188         mov reg,qword [reg]
189 }
190 macro cfa2dfa reg {
191         add reg,8
192 }
193 macro dfa2cfa reg {
194         sub reg,8
195 }
196 macro dfa2tfa reg {
197         sub reg,24
198         mov reg,qword [reg]
199 }
200 ;;; Code snippet to push a pname string with address and 64-bit length field.
201 ;;; The register is advanced to point at the text part.
202 macro pushpname reg {
203         add reg,8
204         push reg
205         push qword [reg-8]
206 }
207 ;;; ========================================
208 ;;; The BLOCK macro lays out the length for a subsequent block to the
209 ;;; given label.
210 macro BLOCK endlabel {
211         local datastart
212         dq endlabel - datastart
213 datastart:
214         }
215
216 ;;; ========================================
217 ;;; The STRING macro lays out length cell and data for several string
218 ;;; components.
219 macro STRING [data] {
220 common
221         local datastart, dataend
222         dq dataend - datastart
223 datastart:
224 forward
225         db data
226 common
227 dataend:
228         }
229
230 ;;; ========================================
231 ;;; The BRANCH macro lays out FORTH words BRANCH and 0BRANCH with offset
232 macro BRANCH zero,label {
233         if zero in <0>
234             dq p_zero_branch
235         else
236             dq p_branch
237         end if
238         dq label - $ - 8
239 }
240
241 ;;; ========================================
242 ;;; The STREAM macro starts an in-core FORTH STREAM area. See WORD
243 ;;; STREAM for details.
244         macro STREAM endlabel {
245         local datastart
246         dq $+32
247         dq -1
248         dq endlabel - datastart
249         dq 0
250 datastart:
251         }