another example
[rrq/rrqforth.git] / temp.asm
1 ;;; Managing a memory area for temporary objects
2 ;;;
3 ;;; A temporary object space is allocated from the current usage
4 ;;; level, or cycling back to be from the lowest space address when
5 ;;; the requested size exceeds the space edge.
6 ;;;
7
8         WORD p_objectspace,'TEMPSPACE',dovariable
9         ;; Holds size and address of the object space.
10         dq 104857600 ; [0] Total object space size (request size)
11         dq 0 ; [8] Object space base address
12         dq 0 ; [16] Currently used.
13
14         WORD p_temp,'TEMP',fasm
15         ;; ( size -- addr )
16         ;; Allocate an object of given size
17         pushr rsi
18         cmp qword [p_objectspace_DFA+8],0
19         jg p_objecthole_initialized
20         ;; initialize object space
21         push qword [p_objectspace_DFA]
22         DOFORTH p_malloc
23         pop qword [p_objectspace_DFA+8]
24 p_objecthole_initialized:
25         mov rax,qword [rsp]
26         add rax,qword [p_objectspace_DFA+16]
27         cmp rax,qword [p_objectspace_DFA]
28         jl p_objecthole_from_tail
29         mov qword [p_objectspace_DFA+16],0
30         mov rax,qword [rsp]
31 p_objecthole_from_tail:
32         ;; rax = new usage count
33         mov rbx,qword [p_objectspace_DFA+16]
34         mov qword [p_objectspace_DFA+16],rax
35         add rbx,qword [p_objectspace_DFA+8]
36         mov qword [rsp],rbx
37         popr rsi
38         next
39
40         WORD p_str2temp,'STR>TEMP',fasm
41         ;; ( char* n -- char* n )
42         ;; Capture a given [n:char*] string as a new temp object with
43         ;; leading size cell.
44         pushr rsi
45         mov rax,qword [rsp]
46         add rax,8
47         push rax
48         DOFORTH p_temp          ; ( -- char* n  addr )
49         pop rax
50         pop rbx
51         mov qword[rax],rbx
52         add rax,8
53         pop rcx
54         push rax
55         push rbx
56         push rcx
57         push rax
58         push rbx                ; ( -- addr n char* addr n )
59         DOFORTH p_strncpy
60         popr rsi
61         next