bug fixes
[rrq/rrqforth.git] / compile.asm
1 ;;; Words for adding words
2
3         WORD p_here,'HERE',dovariable
4         ;; The heap
5         dq heap_start
6         
7         WORD p_create,'CREATE',fasm
8         ;; CREATE ( chars* n -- tfa )
9         ;; Add the pstring as a new word in the current wordlist.
10         pushr rsi
11         mov rdx,qword [p_wordlist_DFA] ; Current word list
12         mov rbx,qword [p_here_DFA] 
13         mov rax,qword [rdx]     ; set up tfa linking to previous word
14         mov qword [rbx],rax     ; 
15         mov qword [rbx+16],0    ; flags field
16         ;; copy pname
17         pop rcx                 ; n
18         mov qword [rbx+24],rcx  ; PFA (length)
19         pop rsi                 ; chars* (source)
20         lea rdi,[rbx+32]        ; (dest)
21         ;; clear DF
22 p_create_COPY:
23         movsb
24         dec rcx
25         jg p_create_COPY
26         mov byte [rdi],0        ; extra NUL
27         inc rdi
28         mov qword [rdi],rbx     ; pTFA
29         add rdi,8
30         mov qword [rdi],0       ; OFF
31         add rdi,8
32         mov qword [rbx+8],rdi   ; pCFA
33         mov qword [rdi],dovariable ; CFA
34         add rdi,8
35         mov qword [p_here_DFA],rdi ; allocate the space
36         mov qword [rdx],rbx     ; Install new word (rdx still wordlist ptr)
37         push rbx
38         popr rsi
39         next
40
41         WORD p_allot,'ALLOT',fasm
42         ;; ( n -- )
43         ;; Allocate n bytes on the heap
44         pop rax
45         add qword [p_here_DFA],rax
46         next
47         
48         WORD p_quote,"'",fasm
49         ;; ( "word" -- cfa )
50         ;; Find the following word and push its cfa, or 0
51         pushr rsi
52         DOFORTH p_stdin, p_read_word, p_find
53         cmp qword[rsp],0
54         jne p_quote_tfa
55         add rsp,16
56         mov qword[rsp],0
57         jmp p_quote_end
58 p_quote_tfa:
59         mov rax,qword [rsp]
60         tfa2cfa rax
61         mov qword [rsp],rax
62 p_quote_end:
63         popr rsi
64         next
65
66         WORD p_bracketed_quote,"[']",doforth,IMMEDIATE
67         ;; Compilation ( "word" -- cfa )
68         ;; Find the following word and push its cfa, or 0
69         dq p_literal
70         dq p_literal
71         dq p_comma
72         dq p_quote
73         dq p_comma
74         dq p_exit
75
76         WORD p_comma,',',fasm
77         ;; ( v -- )
78         ;; Put cell value onto the heap and advance "HERE"
79         mov rax,qword [p_here_DFA]
80         pop rbx
81         mov qword [rax],rbx
82         add rax,8
83         mov qword [p_here_DFA],rax
84         next
85         
86         WORD p_Ccomma,'C,',fasm
87         ;; ( c -- )
88         ;; Put byte value onto the heap and advance "HERE"
89         mov rax,qword [p_here_DFA]
90         pop rbx
91         mov byte [rax],bl
92         inc rax
93         mov qword [p_here_DFA],rax
94         next
95
96         WORD p_does,"DOES>",fasm,IMMEDIATE
97         ;; ( -- )
98         ;; Change the "DOES offset" of most recent word and assign it
99         ;; the "dodoes" execution semantics that follows.
100         mov rax,qword [rsp]
101         mov rbx,rax
102         tfa2does rax            ; *rax is the DOES offset field
103         tfa2dfa rbx
104         mov rcx,qword [p_here_DFA]
105         sub rcx,rbx
106         mov qword [rax],rcx ; save offset from DFA to HERE
107         mov qword [rax+8],dodoes
108         next
109
110         WORD p_literal,'LIT',fasm
111         ;; ( -- v )
112         ;; Push the value of successor cell onto stack, and skip it.
113         ;; not for interactive use!!
114         push qword [rsi]
115         add rsi,8
116         next
117
118         WORD p_literal_string,'S"',fasm ;; " (fool emacs)
119         ;; ( -- char* n )
120         ;; Save string on heap to make available at interpretation
121         ;; not for interactive use!!
122         mov rax,qword [rsi]
123         add rsi,8
124         push rsi
125         push rax
126         add rsi,rax
127         next
128
129         WORD p_state,'STATE',dovariable
130         ;; Interpretation state (0=interpreting, 1=compiling)
131         dq 0
132
133         WORD p_left_bracket,'[',fasm,IMMEDIATE
134         ;; ( -- )
135         ;; Change state to interpreting state.
136         mov qword[p_state_DFA],0
137         next
138
139         WORD p_right_bracket,']',fasm
140         ;; ( -- )
141         ;; Change state to compilation state.
142         mov qword[p_state_DFA],1
143         next
144
145         WORD p_base,'BASE',dovariable
146         dq 10
147
148         WORD p_decimal,'DECIMAL',fasm
149         ;; ( -- )
150         ;; Set BASE to 10
151         mov qword [p_base_DFA],10
152         next
153
154         WORD p_hex,'HEX',fasm
155         ;; ( -- )
156         ;; Set BASE to 16
157         mov qword [p_base_DFA],16
158         next
159
160         WORD p_number,'NUMBER',fasm
161         ;; ( chars* n -- [ 0 ]/[ v 1 ] )
162         pushr rsi
163         pop rcx                 ; ( -- chars* )
164         pop rsi                 ; ( -- )
165         xor rdi,rdi             ; value
166         mov rbx,1               ; sign (byte 0=0 means negative)
167         cmp qword [p_base_DFA],10
168         jne p_number_LOOP
169         cmp byte [rsi],'-'
170         jne p_number_LOOP
171         mov rbx,0
172         inc rsi
173         dec rcx
174         jle p_number_BAD
175 p_number_LOOP:
176         dec rcx
177         jl p_number_DONE
178         xor rax,rax             ; clearing
179         lodsb
180         cmp al,'0'
181         jl p_number_BAD
182         cmp al,'9'
183         jg p_number_ALPHA
184         sub al,'0'
185 p_number_CONSUME:
186         mov r8,rax
187         mov rax,rdi
188         mul qword [p_base_DFA]  ; uses rdx:rax
189         add rax,r8
190         mov rdi,rax
191         jmp p_number_LOOP
192 p_number_ALPHA:
193         cmp al,'A'
194         jl p_number_BAD
195         cmp al,'Z'
196         jg p_number_alpha
197         sub al,'A'-10
198         jmp p_number_CONSUME
199 p_number_alpha:
200         cmp al,'a'
201         jl p_number_BAD
202         cmp al,'z'
203         jg p_number_BAD
204         sub al,'a'-10
205         jmp p_number_CONSUME
206 p_number_BAD:
207         push qword 0
208         popr rsi
209         next
210 p_number_DONE:
211         cmp rbx,0
212         jne p_numper_POSITIVE
213         neg rdi
214 p_numper_POSITIVE:
215         push rdi
216         push qword 1
217         popr rsi
218         next
219
220         WORD p_this_word,'THIS-WORD',dovariable
221         dq 0,0                  ; ( n chars* )
222
223         WORD p_evaluate_stream,'EVALUATE-STREAM'
224         ;; ( stream* -- *?* flag )
225         ;; Execute the words from the given stream
226         ;; returns 1 if stream ends and 0 if an unknown word is found
227         dq p_gtR                ; Keep the stream on the return stack.
228 p_evaluate_stream_PROMPT:
229         dq p_depth
230         dq p_dot
231         dq p_literal_string
232         STRING '> '
233         dq p_tell
234         dq p_Rget
235         dq p_clear_stream
236 p_evaluate_stream_LOOP:
237         dq p_Rget
238         dq p_read_word
239         dq p_dup
240         BRANCH 0,p_evaluate_stream_END ; branch if 0 on TOP
241         dq p_2dup
242         dq p_this_word
243         dq p_2put
244         dq p_find
245         dq p_dup
246         BRANCH 0,p_evaluate_stream_NOTWORD ; branch if 0 on TOP
247         dq p_state
248         dq p_get
249         BRANCH 0,p_evaluate_stream_INTERPRET
250         dq p_dup
251         dq p_tfa2flags_get
252         dq p_literal, 1 ; the immediate bit
253         dq p_and
254         BRANCH 0,p_evaluate_stream_COMPILE
255 p_evaluate_stream_INTERPRET:
256         dq p_tfa2cfa
257         dq p_execute
258         BRANCH ,p_evaluate_stream_AFTER
259 p_evaluate_stream_COMPILE:
260         dq p_comma
261         BRANCH ,p_evaluate_stream_AFTER
262 p_evaluate_stream_NOTWORD:
263         dq p_drop
264         dq p_number
265         BRANCH 0,p_evaluate_stream_BAD ; branch if 0
266         dq p_state
267         dq p_get
268         BRANCH 0,p_evaluate_stream_AFTER ; branch if 0
269         dq p_literal, p_literal
270         dq p_comma, p_comma
271 p_evaluate_stream_AFTER:
272         dq p_Rget
273         dq p_stream_nchars
274         BRANCH 0,p_evaluate_stream_PROMPT
275         BRANCH ,p_evaluate_stream_LOOP
276 p_evaluate_stream_END:
277         dq p_2drop
278         dq p_literal, 1
279 p_evaluate_stream_BAD:
280         dq p_Rgt
281         dq p_drop
282         dq p_exit
283
284         WORD p_colon,':'
285         ;; ( -- )
286         ;; Read next word as a new word into current wordlist, set it
287         ;; to be a doforth word, and set compiling mode.
288         dq p_literal, doforth
289         dq p_stdin
290         dq p_read_word
291         dq p_create
292         dq p_tfa2cfa
293         dq p_put
294         dq p_right_bracket
295         dq p_exit
296
297         WORD p_semicolon,';',,IMMEDIATE
298         ;; ( -- )
299         ;; Lay out p_exit, and set interpreting mode
300         dq p_left_bracket
301         dq p_literal, p_exit
302         dq p_comma
303         dq p_left_bracket
304         dq p_exit
305
306         WORD p_immediate,'IMMEDIATE',fasm,IMMEDIATE
307         ;; ( -- )
308         ;; Set "immediate flag" of the word being defined
309         mov rax,qword [p_wordlist_DFA]
310         mov rax,qword [rax]     ; tfa of most recent word
311         mov qword [rax+16],1    ; set the flags field to 1
312         next