GCC Code Coverage Report


Directory: ../src/
File: /home/joels/Current/lispbm/src/extensions/runtime_extensions.c
Date: 2025-12-14 15:34:39
Exec Total Coverage
Lines: 162 164 98.8%
Functions: 24 25 96.0%
Branches: 55 56 98.2%

Line Branch Exec Source
1 /*
2 Copyright 2023, 2024, 2025 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 #ifdef LBM_OPT_RUNTIME_EXTENSIONS_SIZE
27 #pragma GCC optimize ("-Os")
28 #endif
29 #ifdef LBM_OPT_RUNTIME_EXTENSIONS_SIZE_AGGRESSIVE
30 #pragma GCC optimize ("-Oz")
31 #endif
32
33
34 #ifdef FULL_RTS_LIB
35 static lbm_uint sym_heap_size;
36 static lbm_uint sym_heap_bytes;
37 static lbm_uint sym_num_alloc_cells;
38 static lbm_uint sym_num_alloc_arrays;
39 static lbm_uint sym_num_gc;
40 static lbm_uint sym_num_gc_marked;
41 static lbm_uint sym_num_gc_recovered_cells;
42 static lbm_uint sym_num_gc_recovered_arrays;
43 static lbm_uint sym_num_least_free;
44 static lbm_uint sym_num_last_free;
45
46 static lbm_uint little_endian = 0;
47 static lbm_uint big_endian = 0;
48 #endif
49
50 86 lbm_value ext_eval_set_quota(lbm_value *args, lbm_uint argn) {
51
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 85 times.
86 LBM_CHECK_ARGN_NUMBER(1);
52 85 uint32_t q = lbm_dec_as_u32(args[0]);
53 #ifdef LBM_USE_TIME_QUOTA
54 1 lbm_set_eval_time_quota(q);
55 #else
56 84 lbm_set_eval_step_quota(q);
57 #endif
58 85 return ENC_SYM_TRUE;
59 }
60
61 34 lbm_value ext_hide_trapped_error(lbm_value *args, lbm_uint argn) {
62 (void)args;
63 (void)argn;
64 34 lbm_set_hide_trapped_error(true);
65 34 return ENC_SYM_TRUE;
66 }
67
68 1 lbm_value ext_show_trapped_error(lbm_value *args, lbm_uint argn) {
69 (void)args;
70 (void)argn;
71 1 lbm_set_hide_trapped_error(false);
72 1 return ENC_SYM_TRUE;
73 }
74
75 #ifdef FULL_RTS_LIB
76 3193 lbm_value ext_memory_num_free(lbm_value *args, lbm_uint argn) {
77 (void)args;
78 (void)argn;
79 3193 lbm_uint n = lbm_memory_num_free();
80 3193 return lbm_enc_i((lbm_int)n);
81 }
82
83 336 lbm_value ext_memory_longest_free(lbm_value *args, lbm_uint argn) {
84 (void)args;
85 (void)argn;
86 336 lbm_uint n = lbm_memory_longest_free();
87 336 return lbm_enc_i((lbm_int)n);
88 }
89
90 84 lbm_value ext_memory_size(lbm_value *args, lbm_uint argn) {
91 (void)args;
92 (void)argn;
93 84 lbm_uint n = lbm_memory_num_words();
94 84 return lbm_enc_i((lbm_int)n);
95 }
96
97 952 lbm_value ext_memory_word_size(lbm_value *args, lbm_uint argn) {
98 (void)args;
99 (void)argn;
100 952 return lbm_enc_i((lbm_int)sizeof(lbm_uint));
101 }
102
103 85 lbm_value ext_lbm_version(lbm_value *args, lbm_uint argn) {
104 (void) args;
105 (void) argn;
106 85 lbm_value version = lbm_heap_allocate_list_init(3,
107 lbm_enc_i(LBM_MAJOR_VERSION),
108 lbm_enc_i(LBM_MINOR_VERSION),
109 lbm_enc_i(LBM_PATCH_VERSION));
110 85 return version;
111 }
112
113 ✗ lbm_value ext_lbm_endianness(lbm_value *args, lbm_uint argn) {
114 (void) args;
115 (void) argn;
116 #if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
117 ✗ return lbm_enc_sym(little_endian);
118 #else
119 return lbm_enc_sym(big_endian);
120 #endif
121 }
122
123 765 lbm_value ext_lbm_heap_state(lbm_value *args, lbm_uint argn) {
124
125 765 lbm_value res = ENC_SYM_TERROR;
126
127 lbm_heap_state_t hs;
128 765 lbm_get_heap_state(&hs);
129
130
4/4
✓ Branch 0 taken 762 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 760 times.
✓ Branch 3 taken 2 times.
1527 if (argn == 1 &&
131 762 lbm_is_symbol(args[0])) {
132 760 lbm_uint s = lbm_dec_sym(args[0]);
133
2/2
✓ Branch 0 taken 85 times.
✓ Branch 1 taken 675 times.
760 if (s == sym_heap_size) {
134 85 res = lbm_enc_u(hs.heap_size);
135
2/2
✓ Branch 0 taken 85 times.
✓ Branch 1 taken 590 times.
675 } else if (s == sym_heap_bytes) {
136 85 res = lbm_enc_u(hs.heap_bytes);
137
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 506 times.
590 } else if (s == sym_num_alloc_cells) {
138 84 res = lbm_enc_u(hs.heap_size - hs.num_free);
139
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 505 times.
506 } else if (s == sym_num_alloc_arrays) {
140 1 res = lbm_enc_u(hs.num_alloc_arrays);
141
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 421 times.
505 } else if (s == sym_num_gc) {
142 84 res = lbm_enc_u(hs.gc_num);
143
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 337 times.
421 } else if (s == sym_num_gc_marked) {
144 84 res = lbm_enc_u(hs.gc_marked);
145
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 253 times.
337 } else if (s == sym_num_gc_recovered_cells) {
146 84 res = lbm_enc_u(hs.gc_recovered);
147
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 169 times.
253 } else if (s == sym_num_gc_recovered_arrays) {
148 84 res = lbm_enc_u(hs.gc_recovered_arrays);
149
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 85 times.
169 } else if (s == sym_num_least_free) {
150 84 res = lbm_enc_u(hs.gc_least_free);
151
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 1 times.
85 } else if (s == sym_num_last_free) {
152 84 res = lbm_enc_u(hs.gc_last_free);
153 } else {
154 1 res = ENC_SYM_NIL;
155 }
156 }
157 765 return res;
158 }
159
160 2018 lbm_value ext_env_get(lbm_value *args, lbm_uint argn) {
161
4/4
✓ Branch 0 taken 2017 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2016 times.
✓ Branch 3 taken 1 times.
2018 if (argn == 1 && lbm_is_number(args[0])) {
162 2016 lbm_uint ix = lbm_dec_as_u32(args[0]) & GLOBAL_ENV_MASK;
163 2016 return lbm_get_global_env()[ix];
164 }
165 2 return ENC_SYM_TERROR;
166 }
167
168 1 lbm_value ext_local_env_get(lbm_value *args, lbm_uint argn) {
169 (void) args;
170 (void) argn;
171 1 eval_context_t *ctx = lbm_get_current_context();
172 1 return ctx->curr_env;
173 }
174
175 170 lbm_value ext_env_set(lbm_value *args, lbm_uint argn) {
176
4/4
✓ Branch 0 taken 169 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 168 times.
✓ Branch 3 taken 1 times.
170 if (argn == 2 && lbm_is_number(args[0])) {
177 168 lbm_uint ix = lbm_dec_as_u32(args[0]) & GLOBAL_ENV_MASK;
178 168 lbm_value *glob_env = lbm_get_global_env();
179 168 glob_env[ix] = args[1];
180 168 return ENC_SYM_TRUE;
181 }
182 2 return ENC_SYM_NIL;
183 }
184
185 8 lbm_value ext_env_drop(lbm_value *args, lbm_uint argn) {
186 8 lbm_value r = ENC_SYM_TERROR;
187
4/4
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 1 times.
8 if (argn == 2 && lbm_is_symbol(args[0])) {
188 4 r = lbm_env_drop_binding(args[1], args[0]);
189 }
190 8 return r;
191 }
192
193 2 lbm_value ext_global_env_size(lbm_value *args, lbm_uint argn) {
194 (void) args;
195 (void) argn; // ignores any and all arguments.
196 2 return lbm_enc_u(lbm_get_global_env_size());
197 }
198
199 508 lbm_value ext_set_gc_stack_size(lbm_value *args, lbm_uint argn) {
200
2/2
✓ Branch 0 taken 507 times.
✓ Branch 1 taken 1 times.
508 if (argn == 1) {
201
2/2
✓ Branch 0 taken 506 times.
✓ Branch 1 taken 1 times.
507 if (lbm_is_number(args[0])) {
202 506 uint32_t n = lbm_dec_as_u32(args[0]);
203 506 lbm_uint *new_stack = lbm_malloc(n * sizeof(lbm_uint));
204
2/2
✓ Branch 0 taken 504 times.
✓ Branch 1 taken 2 times.
506 if (new_stack) {
205 504 lbm_free(lbm_heap_state.gc_stack.data);
206 504 lbm_heap_state.gc_stack.data = new_stack;
207 504 lbm_heap_state.gc_stack.size = n;
208 504 lbm_heap_state.gc_stack.sp = 0; // should already be 0
209 504 return ENC_SYM_TRUE;
210 }
211 2 return ENC_SYM_MERROR;
212 }
213 }
214 2 return ENC_SYM_TERROR;
215 }
216
217 840 lbm_value ext_is_64bit(lbm_value *args, lbm_uint argn) {
218 (void) args;
219 (void) argn;
220 #ifndef LBM64
221 560 return ENC_SYM_NIL;
222 #else
223 280 return ENC_SYM_TRUE;
224 #endif
225 }
226
227 84 lbm_value ext_symbol_table_size(lbm_uint *args, lbm_uint argn) {
228 (void) args;
229 (void) argn;
230 84 return lbm_enc_u(lbm_get_symbol_table_size());
231 }
232
233 84 lbm_value ext_symbol_table_size_flash(lbm_uint *args, lbm_uint argn) {
234 (void) args;
235 (void) argn;
236 84 return lbm_enc_u(lbm_get_symbol_table_size_flash());
237 }
238
239 84 lbm_value ext_symbol_table_size_names(lbm_uint *args, lbm_uint argn) {
240 (void) args;
241 (void) argn;
242 84 return lbm_enc_u(lbm_get_symbol_table_size_names());
243 }
244
245 84 lbm_value ext_symbol_table_size_names_flash(lbm_uint *args, lbm_uint argn) {
246 (void) args;
247 (void) argn;
248 84 return lbm_enc_u(lbm_get_symbol_table_size_names_flash());
249 }
250
251 414 lbm_value ext_is_always_gc(lbm_uint *args, lbm_uint argn) {
252 (void) args;
253 (void) argn;
254 #ifdef LBM_ALWAYS_GC
255 return ENC_SYM_TRUE;
256 #else
257 414 return ENC_SYM_NIL;
258 #endif
259 }
260
261 #endif
262
263 #if defined(LBM_USE_EXT_MAILBOX_GET) || defined(FULL_RTS_LIB)
264
265 3 void find_cid(eval_context_t *ctx, void *arg1, void *arg2) {
266 3 lbm_cid id = (lbm_cid)arg1;
267
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if (ctx->id == id) {
268 1 *(eval_context_t**)arg2 = ctx;
269 }
270 3 }
271
272
273 4 lbm_value ext_mailbox_get(lbm_uint *args, lbm_uint argn) {
274 4 lbm_value res = ENC_SYM_TERROR;
275 4 eval_context_t *ctx = NULL;
276
277
4/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 1 times.
4 if (argn == 1 && lbm_is_number(args[0])) {
278 2 res = ENC_SYM_NIL;
279 2 lbm_cid cid = lbm_dec_as_i32(args[0]);
280 2 lbm_all_ctxs_iterator(find_cid, (void*)cid, (void*)&ctx);
281
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (ctx) {
282 1 uint32_t num_mail = ctx->num_mail;
283 1 lbm_value ls = (lbm_heap_allocate_list(num_mail));
284 1 res = ls;
285
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (lbm_is_ptr(ls)) {
286 1 lbm_value curr = ls;
287 1 int i = 0;
288
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 while (lbm_is_ptr(curr)) {
289 3 lbm_set_car(curr, ctx->mailbox[i++]);
290 3 curr = lbm_cdr(curr);
291 }
292 }
293 }
294 }
295 4 return res;
296 }
297 #endif
298
299
300 66393 void lbm_runtime_extensions_init(void) {
301
302 #ifdef FULL_RTS_LIB
303 66393 lbm_add_symbol_const("get-heap-size", &sym_heap_size);
304 66393 lbm_add_symbol_const("get-heap-bytes", &sym_heap_bytes);
305 66393 lbm_add_symbol_const("get-num-alloc-cells", &sym_num_alloc_cells);
306 66393 lbm_add_symbol_const("get-num-alloc-arrays", &sym_num_alloc_arrays);
307 66393 lbm_add_symbol_const("get-gc-num", &sym_num_gc);
308 66393 lbm_add_symbol_const("get-gc-num-marked", &sym_num_gc_marked);
309 66393 lbm_add_symbol_const("get-gc-num-recovered-cells", &sym_num_gc_recovered_cells);
310 66393 lbm_add_symbol_const("get-gc-num-recovered-arrays", &sym_num_gc_recovered_arrays);
311 66393 lbm_add_symbol_const("get-gc-num-least-free", &sym_num_least_free);
312 66393 lbm_add_symbol_const("get-gc-num-last-free", &sym_num_last_free);
313
314 66393 lbm_add_symbol_const("little-endian", &little_endian);
315 66393 lbm_add_symbol_const("big-endian", &big_endian);
316 #endif
317
318 #if defined(LBM_USE_EXT_MAILBOX_GET) || defined(FULL_RTS_LIB)
319 66393 lbm_add_extension("mailbox-get", ext_mailbox_get);
320 #endif
321 #ifndef FULL_RTS_LIB
322 lbm_add_extension("set-eval-quota", ext_eval_set_quota);
323 lbm_add_extension("hide-trapped-error", ext_hide_trapped_error);
324 lbm_add_extension("show-trapped-error", ext_show_trapped_error);
325 #else
326 66393 lbm_add_extension("is-always-gc",ext_is_always_gc);
327 66393 lbm_add_extension("set-eval-quota", ext_eval_set_quota);
328 66393 lbm_add_extension("hide-trapped-error", ext_hide_trapped_error);
329 66393 lbm_add_extension("show-trapped-error", ext_show_trapped_error);
330 66393 lbm_add_extension("mem-num-free", ext_memory_num_free);
331 66393 lbm_add_extension("mem-longest-free", ext_memory_longest_free);
332 66393 lbm_add_extension("mem-size", ext_memory_size);
333 66393 lbm_add_extension("word-size", ext_memory_word_size);
334 66393 lbm_add_extension("lbm-version", ext_lbm_version);
335 66393 lbm_add_extension("lbm-endian", ext_lbm_endianness);
336 66393 lbm_add_extension("lbm-heap-state", ext_lbm_heap_state);
337 66393 lbm_add_extension("env-get", ext_env_get);
338 66393 lbm_add_extension("env-set", ext_env_set);
339 66393 lbm_add_extension("env-drop", ext_env_drop);
340 66393 lbm_add_extension("local-env-get", ext_local_env_get);
341 66393 lbm_add_extension("global-env-size", ext_global_env_size);
342 66393 lbm_add_extension("set-gc-stack-size", ext_set_gc_stack_size);
343 66393 lbm_add_extension("is-64bit", ext_is_64bit);
344 66393 lbm_add_extension("symtab-size", ext_symbol_table_size);
345 66393 lbm_add_extension("symtab-size-flash", ext_symbol_table_size_flash);
346 66393 lbm_add_extension("symtab-size-names", ext_symbol_table_size_names);
347 66393 lbm_add_extension("symtab-size-names-flash", ext_symbol_table_size_names_flash);
348 #endif
349 66393 }
350