corrected BREAK example
[rrq/rrqforth.git] / stdio.asm
1 ;;; ========================================
2 ;;; Dynamic memory management. Allocated with MALLOC and released with
3 ;;; MUNMAP (see below)
4
5         WORD p_malloc,'MALLOC',fasm
6         ;; ( size -- addr )
7         ;; Allocates memory (using mmap)
8         pushr rsi               ; pretend it's a FORTH word since it
9                                 ; ends via sys_mmap_asm
10         pop rax
11         push qword 0            ; address of mapping (suggestion)
12         push rax                ; length of mapping
13         push qword 3            ; protection mode PROT_READ | PROT_WRITE
14         push qword 8226         ; flags PRIVATE | ANONYMOUS | LOCKED
15         push qword -1           ; fd -1
16         push qword 0            ; offset
17         jmp sys_mmap_asm        ; exit via sys_mmap
18
19         WORD p_realloc,'REALLOC',fasm
20         ;; ( addr old new -- )
21         ;; Try remapping a given MMAP region of old size to a new size
22         ;; mremap(void *old_address, size_t old_size,
23         ;;        size_t new_size, int flags, ... /* void *new_address */);
24         pushr rsi
25         push 1                  ; MREMAP_MAYMOVE
26         jmp sys_mmap_asm        ; exit via sys_mmap
27
28 ;;; ========================================
29 ;;; Input stream handling.
30 ;;;
31 ;;; An input stream for a file descriptor has a stream buffer that is
32 ;;; gradually filled on needs basis. The stream buffer includes a
33 ;;; header portion with:
34 ;;; * size of buffer (excluding the 32 byte head)
35 ;;; * source file descriptor (or -1 for pure in-core data)
36 ;;; * current fill
37 ;;; * current read position
38 ;;;
39 ;;; An input stream for a memory block as a "detached" stream head
40 ;;; with:
41 ;;; * block address
42 ;;; * -1
43 ;;; * size of block
44 ;;; * current read position
45
46         WORD p_stream,'STREAM',fasm
47         ;; ( fd size -- addr ) or ( block -1 -- addr )
48         ;; Allocates a stream buffer of the given size and initializes
49         ;; it to be filled from the given input file descriptor.
50         pushr rsi
51         mov rax,[rsp]
52         cmp rax,-1
53         je p_stream_MEM
54         push rax
55         DOFORTH p_malloc        ; ( fd size addr )
56         push qword [rsp]
57         push qword [rsp+16]     ; ( fd size addr addr size )
58         DOFORTH p_erase         ; ( fd size addr )
59         pop rax                 ; addr ( fd size )
60         pop rbx                 ; size ( fd )
61         sub rbx,32              ; reduce by header size
62         mov [rax],rbx
63         pop rbx
64         mov [rax+8],rbx
65         push rax
66         jmp exit
67
68 p_stream_MEM:
69         push 32                 ; size of detached header (wastefull?)
70         DOFORTH p_malloc        ; ( block addr )
71         pop rax                 ; header address
72         pop rbx                 ; block address
73         mov rcx,[rbx]           ; block content size (excludes size field)
74         add rbx,8               ; block content address
75         mov qword [rax],rbx     ; save block content address
76         mov qword [rax+8],-1    ; -1 = memblock flag
77         mov qword [rax+16],rcx  ; save block content size
78         mov qword [rax+24],0    ; current position
79         push rax
80         jmp exit
81
82         WORD p_clear_stream,'CLEAR-STREAM',fasm
83         ;; ( stream -- )
84         ;; Clear buffer of input stream
85         pop rax
86         mov rbx,qword [rax+16]  ; copy fill
87         mov qword [rax+24],rbx  ; into current
88         next
89
90         WORD p_stream_nchars,'STREAM-NCHARS',fasm
91         ;; ( stream -- n )
92         ;; Scan over whitespace in the stream buffer (without actually
93         ;; consuming) and tell how much then remains.
94         pushr rsi
95         mov rcx,qword [rsp]
96         mov rbx,qword [rcx+16] ; fill
97         sub rbx,qword [rcx+24] ; current
98         cmp qword [rcx+8],-1
99         je p_stream_nchars_memblock
100         mov rsi,rcx
101         add rsi,32
102         jmp p_stream_nchars_skipblanks
103 p_stream_nchars_memblock:
104         mov rsi,qword [rcx]
105 p_stream_nchars_skipblanks:
106         add rsi,qword [rcx+24] ; 
107         cmp rbx,0
108         je p_stream_nchars_done
109 p_stream_nchars_loop:
110         lodsb
111         cmp al,32
112         jg p_stream_nchars_done
113         dec rbx
114         jg p_stream_nchars_loop
115 p_stream_nchars_done:
116         mov qword [rsp],rbx
117         popr rsi
118         next
119
120 ;;; ========================================
121 ;;; Stream reading
122 ;;; READ-STREAM-CHAR ( stream -- ch )
123         
124         WORD p_read_stream_char,'READ-STREAM-CHAR',fasm
125         ;; ( stream -- ch )
126         pushr rsi
127         mov rax,qword [rsp]
128         mov rbx,qword [rax+16]  ; fill
129
130 p_read_stream_char.READ:
131         mov rcx,qword [rax+24]  ; current
132         cmp rbx,rcx
133         jg p_read_stream_char.CHAR
134
135         ;; pull in more from the source, if any
136         cmp qword [rax+8],-1    ; fd == -1 for "no source"
137         je p_read_stream_char.EOF
138         
139         push qword [rax+8]      ; fd
140         lea rbx,[rax+32]
141         push rbx                ; buffer
142         push qword [rax]        ; size
143         mov qword[rax+16],0
144         mov qword[rax+24],0
145         DOFORTH sys_read
146         pop rbx
147         mov rax,qword [rsp]
148         cmp rbx,0
149         jle p_read_stream_char.EOF
150         mov qword[rax+16],rbx
151         jmp p_read_stream_char.READ
152
153 p_read_stream_char.EOF:
154         mov qword [rsp],-1
155         popr rsi
156         next
157
158 p_read_stream_char.CHAR:
159         inc qword [rax+24]
160         add rcx,32
161         mov qword [rsp],0
162         mov bl,[rax+rcx]
163         mov byte [rsp],bl
164         popr rsi
165         next
166
167 ;;; ========================================
168 ;;; Input handling
169
170         WORD p_pad,'PAD',dovariable
171         ;; A buffer for holding a word
172         rb 1024
173
174         WORD p_read_word,'READ-WORD',fasm
175         ;; ( stream -- char* length )
176         ;; Read next word from the given stream into the PAD
177         pushr rsi
178         pop rax
179         push qword p_pad_DFA
180         push qword 0
181         push rax
182
183 p_read_word_skipblanks: 
184         FORTH
185         dq p_dup
186         dq p_read_stream_char
187         ENDFORTH
188         pop rbx
189         cmp bl,0
190         jl p_read_word_nomore
191         cmp bl,' '
192         jle p_read_word_skipblanks
193
194 p_read_word_readword:
195         ;; ( buffer length stream )
196         mov rax,qword [rsp+16]
197         mov rcx,qword [rsp+8]
198         mov [rax+rcx],bl
199         inc qword [rsp+8]
200         FORTH
201         dq p_dup
202         dq p_read_stream_char
203         ENDFORTH
204         pop rbx
205         cmp bl,0
206         jl p_read_word_nomore
207         cmp bl,' '
208         jg p_read_word_readword
209
210 p_read_word_nomore:
211         pop rax
212         popr rsi
213         next
214
215         WORD p_double_quote,'"',fasm ;; " (fool emacs)
216         ;; ( -- char* n )
217         ;; Scan to double quote in stream buffer, putting the string on PAD
218         pushr rsi
219         push p_pad_DFA
220         push 0
221 p_double_quote_loop:    
222         DOFORTH p_stdin, p_read_stream_char
223         pop rax
224         cmp rax,0
225         jl p_double_quote_endstream
226         cmp rax,'"'             ; " (fool emacs)
227         je  p_double_quote_endquote
228         lea rdi,[p_pad_DFA]
229         add rdi,qword [rsp]
230         stosb
231         inc qword [rsp]
232         jmp p_double_quote_loop
233 p_double_quote_endquote:
234 p_double_quote_endstream:
235         popr rsi
236         next
237
238         WORD p_tell,'TELL',fasm
239         ;; ( chars* n -- )
240         ;; Write n bytes from chars* to stdout
241         pushr rsi
242         pop rbx
243         pop rax
244         push 1
245         push rax
246         push rbx
247         DOFORTH sys_write
248         pop rax
249         popr rsi
250         next
251
252         WORD p_emit,'EMIT',fasm
253         ;; ( c -- )
254         ;; Write byte to stdout
255         pushr rsi
256         mov rax,rsp
257         push 1
258         push rax
259         push 1
260         DOFORTH sys_write
261         pop rax                 ; ignore return value
262         pop rax                 ; drop input data
263         popr rsi
264         next
265
266         WORD p_nl,'NL',dovalue
267         ;; ( -- c )
268         ;; Pushes a newline character on the stack
269         dq 10
270
271         WORD p_sp,'SP',dovalue
272         ;; ( -- c )
273         ;; Pushes a space character on the stack
274         dq 32
275
276         WORD p_digits,'DIGITS',dovariable
277         db '0123456789abcdef'
278
279         WORD p_dot,'.',fasm
280         ;; ( v -- )
281         ;; Print TOP value as unsigned BASE integer
282         pushr rsi
283         mov rax,qword [rsp]
284         cmp rax,0
285         jge p_dot_positive
286         cmp qword [p_base_DFA],10
287         jne p_dot_positive
288         push '-'
289         DOFORTH p_emit
290         mov rax,qword [rsp]
291         neg rax
292 p_dot_positive:
293         xor rdx,rdx
294         div qword [p_base_DFA]  ; rdx:rax / BASE => q=rax, r=rdx
295         mov qword [rsp],rdx 
296         cmp rax,0
297         je p_dot_remainder
298         push rax
299         DOFORTH p_dot
300 p_dot_remainder:
301         pop rdx
302         xor rax,rax
303         mov al,[p_digits_DFA+rdx]
304         push rax
305         DOFORTH p_emit
306         popr rsi
307         next