1 ;;; ========================================
2 ;;; Dynamic memory management. Allocated with MALLOC and released with
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
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
17 jmp sys_mmap_asm ; exit via sys_mmap
19 ;;; ========================================
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
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
34 jmp sys_mmap_asm ; exit via sys_mmap
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
43 ;;; * current read position
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 )
64 WORD p_read_stream_char,'READ-STREAM-CHAR',fasm
68 mov rbx,[rax+16] ; fill
69 p_read_stream_char.READ:
70 mov rcx,[rax+24] ; current
72 jg p_read_stream_char.CHAR
73 push qword [rax+8] ; fd
76 push qword [rax] ; size
83 jle p_read_stream_char.EOF
85 jmp p_read_stream_char.READ
86 p_read_stream_char.EOF:
90 p_read_stream_char.CHAR:
99 WORD p_line_buffer,'LINE-BUFFER',dovariable
100 ;; A buffer for holding a text line
103 WORD p_read_word,'READ-WORD',fasm
104 ;; ( stream -- addr length )
105 ;; Read a text line from the stream into the line buffer
108 push qword p_line_buffer_DFA
112 p_read_word_skipblanks:
115 dq p_read_stream_char
119 jl p_read_word_nomore
121 jle p_read_word_skipblanks
123 p_read_word_readword:
124 ;; ( buffer length stream )
125 mov rax,qword [rsp+16]
126 mov rcx,qword [rsp+8]
131 dq p_read_stream_char
135 jl p_read_word_nomore
137 jg p_read_word_readword