Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | Copyright 2023 Joel Svensson svenssonjoel@yahoo.se | ||
3 | |||
4 | This program is free software: you can redistribute it and/or modify | ||
5 | it under the terms of the GNU General Public License as published by | ||
6 | the Free Software Foundation, either version 3 of the License, or | ||
7 | (at your option) any later version. | ||
8 | |||
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. | ||
13 | |||
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/>. | ||
16 | */ | ||
17 | |||
18 | #include <lbm_memory.h> | ||
19 | #include <heap.h> | ||
20 | #include <eval_cps.h> | ||
21 | #include <extensions.h> | ||
22 | #include <lbm_utils.h> | ||
23 | #include <lbm_version.h> | ||
24 | #include <env.h> | ||
25 | |||
26 | static lbm_uint sym_heap_size; | ||
27 | static lbm_uint sym_heap_bytes; | ||
28 | static lbm_uint sym_num_alloc_cells; | ||
29 | static lbm_uint sym_num_alloc_arrays; | ||
30 | static lbm_uint sym_num_gc; | ||
31 | static lbm_uint sym_num_gc_marked; | ||
32 | static lbm_uint sym_num_gc_recovered_cells; | ||
33 | static lbm_uint sym_num_gc_recovered_arrays; | ||
34 | static lbm_uint sym_num_least_free; | ||
35 | static lbm_uint sym_num_last_free; | ||
36 | |||
37 | ✗ | lbm_value ext_eval_set_quota(lbm_value *args, lbm_uint argn) { | |
38 | ✗ | LBM_CHECK_ARGN_NUMBER(1); | |
39 | ✗ | uint32_t q = lbm_dec_as_u32(args[0]); | |
40 | ✗ | lbm_set_eval_step_quota(q); | |
41 | ✗ | return ENC_SYM_TRUE; | |
42 | } | ||
43 | |||
44 | 1064 | lbm_value ext_memory_num_free(lbm_value *args, lbm_uint argn) { | |
45 | (void)args; | ||
46 | (void)argn; | ||
47 | 1064 | lbm_uint n = lbm_memory_num_free(); | |
48 | 1064 | return lbm_enc_i((lbm_int)n); | |
49 | } | ||
50 | |||
51 | 112 | lbm_value ext_memory_longest_free(lbm_value *args, lbm_uint argn) { | |
52 | (void)args; | ||
53 | (void)argn; | ||
54 | 112 | lbm_uint n = lbm_memory_longest_free(); | |
55 | 112 | return lbm_enc_i((lbm_int)n); | |
56 | } | ||
57 | |||
58 | ✗ | lbm_value ext_memory_size(lbm_value *args, lbm_uint argn) { | |
59 | (void)args; | ||
60 | (void)argn; | ||
61 | ✗ | lbm_uint n = lbm_memory_num_words(); | |
62 | ✗ | return lbm_enc_i((lbm_int)n); | |
63 | } | ||
64 | |||
65 | 84 | lbm_value ext_memory_word_size(lbm_value *args, lbm_uint argn) { | |
66 | (void)args; | ||
67 | (void)argn; | ||
68 | 84 | return lbm_enc_i((lbm_int)sizeof(lbm_uint)); | |
69 | } | ||
70 | |||
71 | ✗ | lbm_value ext_lbm_version(lbm_value *args, lbm_uint argn) { | |
72 | (void) args; | ||
73 | (void) argn; | ||
74 | ✗ | lbm_value version = lbm_heap_allocate_list_init(3, | |
75 | lbm_enc_i(LBM_MAJOR_VERSION), | ||
76 | lbm_enc_i(LBM_MINOR_VERSION), | ||
77 | lbm_enc_i(LBM_PATCH_VERSION)); | ||
78 | ✗ | return version; | |
79 | } | ||
80 | |||
81 | ✗ | lbm_value ext_lbm_heap_state(lbm_value *args, lbm_uint argn) { | |
82 | |||
83 | ✗ | lbm_value res = ENC_SYM_TERROR; | |
84 | |||
85 | lbm_heap_state_t hs; | ||
86 | ✗ | lbm_get_heap_state(&hs); | |
87 | |||
88 | ✗ | if (argn == 1 && | |
89 | ✗ | lbm_is_symbol(args[0])) { | |
90 | ✗ | lbm_uint s = lbm_dec_sym(args[0]); | |
91 | ✗ | if (s == sym_heap_size) { | |
92 | ✗ | res = lbm_enc_u(hs.heap_size); | |
93 | ✗ | } else if (s == sym_heap_bytes) { | |
94 | ✗ | res = lbm_enc_u(hs.heap_bytes); | |
95 | ✗ | } else if (s == sym_num_alloc_cells) { | |
96 | ✗ | res = lbm_enc_u(hs.num_alloc); | |
97 | ✗ | } else if (s == sym_num_alloc_arrays) { | |
98 | ✗ | res = lbm_enc_u(hs.num_alloc_arrays); | |
99 | ✗ | } else if (s == sym_num_gc) { | |
100 | ✗ | res = lbm_enc_u(hs.gc_num); | |
101 | ✗ | } else if (s == sym_num_gc_marked) { | |
102 | ✗ | res = lbm_enc_u(hs.gc_marked); | |
103 | ✗ | } else if (s == sym_num_gc_recovered_cells) { | |
104 | ✗ | res = lbm_enc_u(hs.gc_recovered); | |
105 | ✗ | } else if (s == sym_num_gc_recovered_arrays) { | |
106 | ✗ | res = lbm_enc_u(hs.gc_recovered_arrays); | |
107 | ✗ | } else if (s == sym_num_least_free) { | |
108 | ✗ | res = lbm_enc_u(hs.gc_least_free); | |
109 | ✗ | } else if (s == sym_num_last_free) { | |
110 | ✗ | res = lbm_enc_u(hs.gc_last_free); | |
111 | } else { | ||
112 | ✗ | res = ENC_SYM_NIL; | |
113 | } | ||
114 | } | ||
115 | ✗ | return res; | |
116 | } | ||
117 | |||
118 | ✗ | lbm_value ext_env_get(lbm_value *args, lbm_uint argn) { | |
119 | (void)args; | ||
120 | (void)argn; | ||
121 | ✗ | if (argn == 1 && lbm_is_number(args[0])) { | |
122 | ✗ | lbm_uint ix = lbm_dec_as_u32(args[0]) & GLOBAL_ENV_MASK; | |
123 | ✗ | return lbm_get_global_env()[ix]; | |
124 | } | ||
125 | ✗ | return ENC_SYM_TERROR; | |
126 | } | ||
127 | |||
128 | ✗ | lbm_value ext_env_set(lbm_value *args, lbm_uint argn) { | |
129 | ✗ | if (argn == 2 && lbm_is_number(args[0])) { | |
130 | ✗ | lbm_uint ix = lbm_dec_as_u32(args[0]) & GLOBAL_ENV_MASK; | |
131 | ✗ | lbm_value *glob_env = lbm_get_global_env(); | |
132 | ✗ | glob_env[ix] = args[1]; | |
133 | ✗ | return ENC_SYM_TRUE; | |
134 | } | ||
135 | ✗ | return ENC_SYM_NIL; | |
136 | } | ||
137 | |||
138 | ✗ | lbm_value ext_set_gc_stack_size(lbm_value *args, lbm_uint argn) { | |
139 | ✗ | if (argn == 1) { | |
140 | ✗ | if (lbm_is_number(args[0])) { | |
141 | ✗ | uint32_t n = lbm_dec_as_u32(args[0]); | |
142 | ✗ | lbm_uint *new_stack = lbm_malloc(n * sizeof(lbm_uint)); | |
143 | ✗ | if (new_stack) { | |
144 | ✗ | lbm_free(lbm_heap_state.gc_stack.data); | |
145 | ✗ | lbm_heap_state.gc_stack.data = new_stack; | |
146 | ✗ | lbm_heap_state.gc_stack.size = n; | |
147 | ✗ | lbm_heap_state.gc_stack.max_sp = 0; | |
148 | ✗ | lbm_heap_state.gc_stack.sp = 0; // should already be 0 | |
149 | ✗ | return ENC_SYM_TRUE; | |
150 | } | ||
151 | ✗ | return ENC_SYM_MERROR; | |
152 | } | ||
153 | } | ||
154 | ✗ | return ENC_SYM_TERROR; | |
155 | } | ||
156 | |||
157 | 280 | lbm_value ext_is_64bit(lbm_value *args, lbm_uint argn) { | |
158 | (void) args; | ||
159 | (void) argn; | ||
160 | #ifndef LBM64 | ||
161 | 280 | return ENC_SYM_NIL; | |
162 | #else | ||
163 | return ENC_SYM_TRUE; | ||
164 | #endif | ||
165 | } | ||
166 | |||
167 | 17444 | bool lbm_runtime_extensions_init(bool minimal) { | |
168 | |||
169 |
1/2✓ Branch 0 taken 17444 times.
✗ Branch 1 not taken.
|
17444 | if (!minimal) { |
170 | 17444 | lbm_add_symbol_const("get-heap-size", &sym_heap_size); | |
171 | 17444 | lbm_add_symbol_const("get-heap-bytes", &sym_heap_bytes); | |
172 | 17444 | lbm_add_symbol_const("get-num-alloc-cells", &sym_num_alloc_cells); | |
173 | 17444 | lbm_add_symbol_const("get-num-alloc-arrays", &sym_num_alloc_arrays); | |
174 | 17444 | lbm_add_symbol_const("get-gc-num", &sym_num_gc); | |
175 | 17444 | lbm_add_symbol_const("get-gc-num-marked", &sym_num_gc_marked); | |
176 | 17444 | lbm_add_symbol_const("get-gc-num-recovered-cells", &sym_num_gc_recovered_cells); | |
177 | 17444 | lbm_add_symbol_const("get-gc-num-recovered-arrays", &sym_num_gc_recovered_arrays); | |
178 | 17444 | lbm_add_symbol_const("get-gc-num-least-free", &sym_num_least_free); | |
179 | 17444 | lbm_add_symbol_const("get-gc-num-last-free", &sym_num_last_free); | |
180 | } | ||
181 | |||
182 | 17444 | bool res = true; | |
183 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 17444 times.
|
17444 | if (minimal) { |
184 | ✗ | res = res && lbm_add_extension("set-eval-quota", ext_eval_set_quota); | |
185 | } else { | ||
186 |
2/4✓ Branch 0 taken 17444 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 17444 times.
✗ Branch 4 not taken.
|
17444 | res = res && lbm_add_extension("set-eval-quota", ext_eval_set_quota); |
187 |
2/4✓ Branch 0 taken 17444 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 17444 times.
✗ Branch 4 not taken.
|
17444 | res = res && lbm_add_extension("mem-num-free", ext_memory_num_free); |
188 |
2/4✓ Branch 0 taken 17444 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 17444 times.
✗ Branch 4 not taken.
|
17444 | res = res && lbm_add_extension("mem-longest-free", ext_memory_longest_free); |
189 |
2/4✓ Branch 0 taken 17444 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 17444 times.
✗ Branch 4 not taken.
|
17444 | res = res && lbm_add_extension("mem-size", ext_memory_size); |
190 |
2/4✓ Branch 0 taken 17444 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 17444 times.
✗ Branch 4 not taken.
|
17444 | res = res && lbm_add_extension("word-size", ext_memory_word_size); |
191 |
2/4✓ Branch 0 taken 17444 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 17444 times.
✗ Branch 4 not taken.
|
17444 | res = res && lbm_add_extension("lbm-version", ext_lbm_version); |
192 |
2/4✓ Branch 0 taken 17444 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 17444 times.
✗ Branch 4 not taken.
|
17444 | res = res && lbm_add_extension("lbm-heap-state", ext_lbm_heap_state); |
193 |
2/4✓ Branch 0 taken 17444 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 17444 times.
✗ Branch 4 not taken.
|
17444 | res = res && lbm_add_extension("env-get", ext_env_get); |
194 |
2/4✓ Branch 0 taken 17444 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 17444 times.
✗ Branch 4 not taken.
|
17444 | res = res && lbm_add_extension("env-set", ext_env_set); |
195 |
2/4✓ Branch 0 taken 17444 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 17444 times.
✗ Branch 4 not taken.
|
17444 | res = res && lbm_add_extension("set-gc-stack-size", ext_set_gc_stack_size); |
196 |
2/4✓ Branch 0 taken 17444 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 17444 times.
✗ Branch 4 not taken.
|
17444 | res = res && lbm_add_extension("is-64bit", ext_is_64bit); |
197 | } | ||
198 | 17444 | return res; | |
199 | } | ||
200 |