1 ;; Native x86 GNU/Linux Forth System, Direct Threaded Code
3 ;; Copyright (C) 2020 Ketmar Dark // Invisible Vector
5 ;; This program is free software: you can redistribute it and/or modify
6 ;; it under the terms of the GNU General Public License as published by
7 ;; the Free Software Foundation, version 3 of the License ONLY.
9 ;; This program is distributed in the hope that it will be useful,
10 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ;; GNU General Public License for more details.
14 ;; You should have received a copy of the GNU General Public License
15 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
18 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
19 ;; very simple pool allocator for temporary data
20 ;; allocates bytes from the end of the memory
21 urword_var
"(TEMP-POOL-TOP)",temp_pool_top
,0
23 urword_const
"(TEMP-POOL-BOT-OFS)",temp_pool_bot_ofs
,1024
26 urword_forth
"TEMP-POOL-RESET",temp_pool_reset
28 ;; resets temporary pool (i.e. deallocates everything)
29 UF dpend @ temp_pool_bot_ofs
- temp_pool_top
!
32 urword_forth
"TEMP-POOL-MARK",temp_pool_mark
34 ;; returns temp pool "mark", which can be used in "TEMP-POOL-RELEASE"
38 urword_forth
"TEMP-POOL-HERE",temp_pool_here
40 ;; returns temp pool "here", i.e. current top
41 ;; can be used to avoid keeping it elsewhere
45 urword_forth
"TEMP-POOL-RELEASE",temp_pool_release
47 ;; deallocates temp pool bytes
48 ;; aborts on invalid mark values
49 ; trying to allocate something instead of freeing it?
50 UF dup temp_pool_top @ uless errid_invalid_temp_pool_release qerror
51 ; trying to go beyound the end of the pool?
52 UF dup dpend @ temp_pool_bot_ofs
- ugreat
53 UF errid_invalid_temp_pool_release qerror
54 ; set this as new pool top
58 urword_forth
"TEMP-POOL-ALLOC",temp_pool_alloc
60 ;; deallocates temp pool bytes
61 ;; aborts on invalid mark values
62 ; sanity check for size
63 UF dup
0 less errid_invalid_temp_pool_allocation qerror
64 UF dup unused great errid_invalid_temp_pool_allocation qerror
65 ; check if we'll have less than 64KB of room between the temp pool and HERE
66 UF dup here
+ 65536 + temp_pool_top @ ugreat errid_out_of_temp_pool qerror
68 UF temp_pool_top subpoke
69 ; return the address of the allocated memory
75 urword_code
"(NO-CALL-TEMP-POOL-CODESUBS)",par_no_call_temp_pool_subs
79 ;; other registers are preserved (except flags)
81 ld
eax,[fvar_temp_pool_top_data
]
86 ;; all registers are preserved (except flags)
87 ;; TODO: sanity checks
89 ld
[fvar_temp_pool_top_data
],eax
94 ;; all other registers (including EAX) are preserved (except flags)
95 ;; TODO: sanity checks
97 ld
edi,[fvar_temp_pool_top_data
]
99 jr c
,urpool_alloc_error
100 cmp edi,[fvar_temp_pool_top_data
]
101 jr nc
,urpool_alloc_error
102 ld
eax,[fvar_dp_temp_data
]
104 cmovz
eax,[fvar_dp_data
]
107 jr c
,urpool_alloc_error
109 ld
[fvar_temp_pool_top_data
],edi
113 ld TOS
,ERR_OUT_OF_TEMP_POOL
116 ; it will never return