be5a9088e76a186fe9e6e9c22e09e84ae1f301c1
[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'
42         ;; ( n -- )
43         ;; Allocate n bytes on the heap
44         dq p_here, p_put_plus, p_exit
45         
46         WORD p_quote,"'"
47         ;; ( "word" -- cfa )
48         ;; Find the following word and push its cfa, or 0
49         dq p_input, p_get, p_read_word, p_find
50         BRANCH 0,p_quote_end
51         dq p_tfa2cfa
52 p_quote_end:
53         dq p_exit
54
55         WORD p_bracketed_quote,"[']",doforth,IMMEDIATE
56         ;; Compilation ( "word" -- cfa )
57         ;; Compile down " LIT value "
58         dq p_literal, p_literal, p_comma,p_quote, p_comma, p_exit
59
60         WORD p_comma,','
61         ;; ( v -- )
62         ;; Put cell value onto the heap and advance "HERE"
63         dq p_here, p_put, p_literal, 8, p_here, p_put_plus, p_exit
64         
65         WORD p_Ccomma,'C,'
66         ;; ( c -- )
67         ;; Put byte value onto the heap and advance "HERE"
68         dq p_here, p_Cput, p_literal, 1, p_here, p_put_plus, p_exit
69
70         WORD p_does,"DOES>",fasm,IMMEDIATE
71         ;; ( -- )
72         ;; Change the "DOES offset" of most recent word and assign it
73         ;; the "dodoes" execution semantics that follows.
74         mov rax,qword [rsp]
75         mov rbx,rax
76         tfa2does rax            ; *rax is the DOES offset field
77         tfa2dfa rbx
78         mov rcx,qword [p_here_DFA]
79         sub rcx,rbx
80         mov qword [rax],rcx ; save offset from DFA to HERE
81         mov qword [rax+8],dodoes
82         next
83
84         WORD p_literal,'LIT',fasm
85         ;; ( -- v )
86         ;; Push the value of successor cell onto stack, and skip it.
87         ;; not for interactive use!!
88         push qword [rsi]
89         add rsi,8
90         next
91
92         WORD p_literal_string,'S"',fasm,IMMEDIATE ;; " (fool emacs)
93         ;; ( -- char* n )
94         ;; Save string on heap to make available at interpretation
95         ;; not for interactive use!!
96         cmp qword [p_state_DFA],0
97         je p_literal_string_executing
98         pushr rsi
99         mov rdi,qword [p_here_DFA]
100         mov qword [rdi],p_literal_string
101         add rdi,8
102         mov qword [p_here_DFA],rdi
103         DOFORTH p_double_quote
104         pop rcx
105         pop rsi
106         mov rdi,qword [p_here_DFA]
107         mov qword [rdi],rcx
108         add rdi,8
109 p_literal_string_copy:
110         dec rcx
111         jl p_literal_string_copied
112         movsb
113         jmp p_literal_string_copy
114 p_literal_string_copied:
115         mov qword [p_here_DFA],rdi
116         popr rsi
117         next
118         
119 p_literal_string_executing:
120         mov rax,qword [rsi]
121         add rsi,8
122         push rsi
123         push rax
124         add rsi,rax
125         next
126
127         WORD p_state,'STATE',dovariable
128         ;; Interpretation state (0=interpreting, 1=compiling)
129         dq 0
130
131         WORD p_left_bracket,'[',fasm,IMMEDIATE
132         ;; ( -- )
133         ;; Change state to interpreting state.
134         mov qword[p_state_DFA],0
135         next
136
137         WORD p_right_bracket,']',fasm
138         ;; ( -- )
139         ;; Change state to compilation state.
140         mov qword[p_state_DFA],1
141         next
142
143         WORD p_base,'BASE',dovariable
144         dq 10
145
146         WORD p_decimal,'DECIMAL',fasm
147         ;; ( -- )
148         ;; Set BASE to 10
149         mov qword [p_base_DFA],10
150         next
151
152         WORD p_hex,'HEX',fasm
153         ;; ( -- )
154         ;; Set BASE to 16
155         mov qword [p_base_DFA],16
156         next
157
158         WORD p_number,'NUMBER',fasm
159         ;; ( chars* n -- [ 0 ]/[ v 1 ] )
160         pushr rsi
161         pop rcx                 ; ( -- chars* )
162         pop rsi                 ; ( -- )
163         xor rdi,rdi             ; value
164         mov rbx,1               ; sign (byte 0=0 means negative)
165         cmp qword [p_base_DFA],10
166         jne p_number_LOOP
167         cmp byte [rsi],'-'
168         jne p_number_LOOP
169         mov rbx,0
170         inc rsi
171         dec rcx
172         jle p_number_BAD
173 p_number_LOOP:
174         dec rcx
175         jl p_number_DONE
176         xor rax,rax             ; clearing
177         lodsb
178         cmp al,'0'
179         jl p_number_BAD
180         cmp al,'9'
181         jg p_number_ALPHA
182         sub al,'0'
183 p_number_CONSUME:
184         mov r8,rax
185         mov rax,rdi
186         mul qword [p_base_DFA]  ; uses rdx:rax
187         add rax,r8
188         mov rdi,rax
189         jmp p_number_LOOP
190 p_number_ALPHA:
191         cmp al,'A'
192         jl p_number_BAD
193         cmp al,'Z'
194         jg p_number_alpha
195         sub al,'A'-10
196         jmp p_number_CONSUME
197 p_number_alpha:
198         cmp al,'a'
199         jl p_number_BAD
200         cmp al,'z'
201         jg p_number_BAD
202         sub al,'a'-10
203         jmp p_number_CONSUME
204 p_number_BAD:
205         push qword 0
206         popr rsi
207         next
208 p_number_DONE:
209         cmp rbx,0
210         jne p_numper_POSITIVE
211         neg rdi
212 p_numper_POSITIVE:
213         push rdi
214         push qword 1
215         popr rsi
216         next
217
218         WORD p_input,'INPUT',dovariable
219         ;; The current input stream for evaluate-stream
220         dq 0
221
222         WORD p_this_word,'THIS-WORD',dovariable
223         dq 0,0                  ; ( n chars* )
224
225         WORD p_evaluate_stream,'EVALUATE-STREAM'
226         ;; ( stream* -- *?* flag )
227         ;; Execute the words from the given stream
228         ;; returns 1 if stream ends and 0 if an unknown word is found
229         dq p_input, p_get, p_gtR ; save old stream on R-stack
230         dq p_input, p_put
231 p_evaluate_stream_PROMPT:
232         dq p_verboseQ, p_get
233         BRANCH 0,p_evaluate_stream_LOOP
234         dq p_depth, p_dot
235         dq p_literal_string
236         STRING '> '
237         dq p_tell
238         dq p_input, p_get
239         dq p_clear_stream
240 p_evaluate_stream_LOOP:
241         dq p_input, p_get
242         dq p_read_word
243         dq p_dup
244         BRANCH 0,p_evaluate_stream_END ; branch if 0 on TOP
245         dq p_2dup, p_this_word, p_2put
246         dq p_find
247         dq p_dup
248         BRANCH 0,p_evaluate_stream_NOTWORD ; branch if 0 on TOP
249         dq p_state
250         dq p_get
251         BRANCH 0,p_evaluate_stream_INTERPRET
252         dq p_dup
253         dq p_tfa2flags_get
254         dq p_literal, 1 ; the immediate bit
255         dq p_and
256         BRANCH 0,p_evaluate_stream_COMPILE
257 p_evaluate_stream_INTERPRET:
258         dq p_tfa2cfa
259         dq p_execute
260         BRANCH ,p_evaluate_stream_AFTER
261 p_evaluate_stream_COMPILE:
262         dq p_tfa2cfa
263         dq p_comma
264         BRANCH ,p_evaluate_stream_AFTER
265 p_evaluate_stream_NOTWORD:
266         dq p_drop
267         dq p_number
268         dq p_dup
269         BRANCH 0,p_evaluate_stream_BAD ; branch if 0
270         dq p_drop
271         dq p_state, p_get
272         BRANCH 0,p_evaluate_stream_AFTER ; branch if 0
273         dq p_literal, p_literal
274         dq p_comma, p_comma
275 p_evaluate_stream_AFTER:
276         dq p_input, p_get
277         dq p_stream_nchars
278         BRANCH 0,p_evaluate_stream_PROMPT
279         BRANCH ,p_evaluate_stream_LOOP
280 p_evaluate_stream_END:
281         dq p_2drop
282         dq p_literal, 1
283 p_evaluate_stream_BAD:
284         dq p_Rgt, p_input, p_put ; restore previous stream
285         dq p_exit
286
287         WORD p_colon,':'
288         ;; ( -- )
289         ;; Read next word as a new word into current wordlist, set it
290         ;; to be a doforth word, and set compiling mode.
291         dq p_literal, doforth
292         dq p_input, p_get
293         dq p_read_word
294         dq p_create
295         dq p_tfa2cfa
296         dq p_put
297         dq p_right_bracket
298         dq p_exit
299
300         WORD p_semicolon,';',,IMMEDIATE
301         ;; ( -- )
302         ;; Lay out p_exit, and set interpreting mode
303         dq p_literal, p_exit, p_comma, p_left_bracket, p_exit
304
305         WORD p_immediate,'IMMEDIATE',fasm,IMMEDIATE
306         ;; ( -- )
307         ;; Set "immediate flag" of the word being defined
308         mov rax,qword [p_wordlist_DFA]
309         mov rax,qword [rax]     ; tfa of most recent word
310         mov qword [rax+16],1    ; set the flags field to 1
311         next
312
313         WORD p_load_buffer_size,'LOAD-BUFFER-SIZE',dovariable
314         ;; ( -- a )
315         ;; The buffer size (in bytes) used by LOAD-FILE
316         dq 15000
317         
318         WORD p_open_file,'OPEN-FILE',fasm
319         ;; ( chaz* n -- fd )
320         ;; Open the nominated file
321         pushr rsi
322         add rsp,8 ; drop n ... assuming NUL-ended string
323         push qword 0
324         push qword 0
325         DOFORTH sys_open
326         popr rsi
327         next
328
329         WORD p_load_file,'LOAD-FILE'
330         ;; ( chaz* n -- )
331         dq p_open_file
332         dq p_dup, p_0less
333         BRANCH 1,p_load_file_badfile
334         dq p_load_buffer_size, p_get
335         dq p_stream, p_dup, p_gtR
336         dq p_evaluate_stream
337         dq p_Rgt, p_unstream
338         BRANCH ,p_load_file_exit
339 p_load_file_badfile:
340         dq p_literal_string
341         STRING '** open file error: '
342         dq p_tell, p_dot, p_nl, p_emit
343         dq p_literal,1
344 p_load_file_exit:
345         dq p_exit