renaming
[rrq/rrqforth.git] / stdio.asm
1 ;;; ========================================
2 ;;; Dynamic memory management. Allocated with MALLOC and released with
3 ;;; MUNMAP (see below)
4
5         ;; ( size -- addr )
6         ;; Allocates memory (using brk)
7         WORD p_malloc,'MALLOC',fasm
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 ;;; ========================================
20 ;;; Mapping files
21
22         ;; ( fd -- address )
23         ;; Request memory mapping of a file
24         WORD p_mmap,'MMAP',fasm
25         pushr rsi               ; pretend it's a FORTH word since it
26                                 ; ends via sys_mmap_asm
27         pop rax
28         push qword 0            ; address of mapping (suggestion)
29         push qword 10240        ; length of mapping
30         push qword 1            ; protection mode PROT_READ
31         push qword 2            ; flags MAP_PRIVATE
32         push rax                ; fd
33         push qword 0            ; offset
34         jmp sys_mmap_asm        ; exit via sys_mmap
35
36 ;;; ========================================
37 ;;; Input stream handling. An input stream has a stream buffer that is
38 ;;; gradually filled on needs basis. The stream buffer includes a
39 ;;; header portion with:
40 ;;; * size of buffer (excluding the 32 byte head)
41 ;;; * source file descriptor
42 ;;; * current fill
43 ;;; * current read position
44
45         WORD p_stream,'STREAM',
46         ;; ( fd size -- addr )
47         ;; Allocates a stream buffer of the given size and initializes
48         ;; it to be filled from the given input file descriptor.
49         dq p_dup                ; ( fd size size )
50         dq p_malloc             ; ( fd size addr )
51         dq p_2dup               ; ( fd size addr size addr )
52         dq p_swap               ; ( fd size addr addr size )
53         dq p_erase              ; ( fd size addr )
54         ENDFORTH
55         pop rax                 ; ( fd size )
56         pop rbx                 ; ( fd )
57         sub rbx,32
58         mov [rax],rbx
59         pop rbx
60         mov [rax+8],rbx
61         push rax
62         jmp exit
63
64         WORD p_read_stream_char,'READ-STREAM-CHAR',fasm
65         ;; ( stream -- ch )
66         pushr rsi
67         mov rax,qword [rsp]
68         mov rbx,[rax+16]        ; fill
69 p_read_stream_char.READ:
70         mov rcx,[rax+24]        ; current
71         cmp rbx,rcx
72         jg p_read_stream_char.CHAR
73         push qword [rax+8]      ; fd
74         lea rbx,[rax+32]
75         push rbx                ; buffer
76         push qword [rax]        ; size
77         mov qword[rax+16],0
78         mov qword[rax+24],0
79         DOFORTH sys_read
80         pop rbx
81         mov rax,qword [rsp]
82         cmp rbx,0
83         jle p_read_stream_char.EOF
84         mov qword[rax+16],rbx
85         jmp p_read_stream_char.READ
86 p_read_stream_char.EOF:
87         mov qword [rsp],-1
88         popr rsi
89         next
90 p_read_stream_char.CHAR:
91         inc qword [rax+24]
92         add rcx,32
93         mov qword [rsp],0
94         mov bl,[rax+rcx]
95         mov byte [rsp],bl
96         popr rsi
97         next
98
99         WORD p_line_buffer,'LINE-BUFFER',dovariable
100         ;; A buffer for holding a text line
101         rb 1024
102
103         WORD p_read_word,'READ-WORD',fasm
104         ;; ( stream -- addr length )
105         ;; Read a text line from the stream into the line buffer
106         pushr rsi
107         pop rax
108         push qword p_line_buffer_DFA
109         push qword 0
110         push rax
111 p_read_word_skipblanks: 
112         FORTH
113         dq p_dup
114         dq p_read_stream_char
115         ENDFORTH
116         pop rbx
117         cmp bl,0
118         jl p_read_word_nomore
119         cmp bl,' '
120         jle p_read_word_skipblanks
121 p_read_word_readword:
122         FORTH
123         dq p_dup
124         dq p_read_stream_char
125         ENDFORTH
126         pop rbx
127         cmp bl,0
128         jl p_read_word_nomore
129         cmp bl,' '
130         jle p_read_word_nomore
131         ;; ( buffer length stream )
132         mov rax,qword [rsp+16]
133         mov rcx,qword [rsp+8]
134         mov [rax+rcx],bl
135         inc qword [rsp+8]
136         jmp p_read_word_readword
137 p_read_word_nomore:
138         pop rax
139         popr rsi
140         next