GCC Code Coverage Report


Directory: ../src/
File: /home/joels/Current/lispbm/src/extensions/runtime_extensions.c
Date: 2026-09-05 08:38:23
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 90 lbm_value ext_eval_set_quota(lbm_value *args, lbm_uint argn) {
51
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 87 times.
90 LBM_CHECK_ARGN_NUMBER(1);
52 87 uint32_t q = lbm_dec_as_u32(args[0]);
53 #ifdef LBM_USE_TIME_QUOTA
54 3 lbm_set_eval_time_quota(q);
55 #else
56 84 lbm_set_eval_step_quota(q);
57 #endif
58 87 return ENC_SYM_TRUE;
59 }
60
61 108 lbm_value ext_hide_trapped_error(lbm_value *args, lbm_uint argn) {
62 (void)args;
63 (void)argn;
64 108 lbm_set_hide_trapped_error(true);
65 108 return ENC_SYM_TRUE;
66 }
67
68 3 lbm_value ext_show_trapped_error(lbm_value *args, lbm_uint argn) {
69 (void)args;
70 (void)argn;
71 3 lbm_set_hide_trapped_error(false);
72 3 return ENC_SYM_TRUE;
73 }
74
75 #ifdef FULL_RTS_LIB
76 3195 lbm_value ext_memory_num_free(lbm_value *args, lbm_uint argn) {
77 (void)args;
78 (void)argn;
79 3195 lbm_uint n = lbm_memory_num_free();
80 3195 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 955 lbm_value ext_memory_word_size(lbm_value *args, lbm_uint argn) {
98 (void)args;
99 (void)argn;
100 955 return lbm_enc_i((lbm_int)sizeof(lbm_uint));
101 }
102
103 87 lbm_value ext_lbm_version(lbm_value *args, lbm_uint argn) {
104 (void) args;
105 (void) argn;
106 87 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 87 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 783 lbm_value ext_lbm_heap_state(lbm_value *args, lbm_uint argn) {
124
125 783 lbm_value res = ENC_SYM_TERROR;
126
127 lbm_heap_state_t hs;
128 783 lbm_get_heap_state(&hs);
129
130
4/4
✓ Branch 0 taken 774 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 768 times.
✓ Branch 3 taken 6 times.
1557 if (argn == 1 &&
131 774 lbm_is_symbol(args[0])) {
132 768 lbm_uint s = lbm_dec_sym(args[0]);
133
2/2
✓ Branch 0 taken 87 times.
✓ Branch 1 taken 681 times.
768 if (s == sym_heap_size) {
134 87 res = lbm_enc_u(hs.heap_size);
135
2/2
✓ Branch 0 taken 87 times.
✓ Branch 1 taken 594 times.
681 } else if (s == sym_heap_bytes) {
136 87 res = lbm_enc_u(hs.heap_bytes);
137
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 510 times.
594 } 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 3 times.
✓ Branch 1 taken 507 times.
510 } else if (s == sym_num_alloc_arrays) {
140 3 res = lbm_enc_u(hs.num_alloc_arrays);
141
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 423 times.
507 } 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 339 times.
423 } 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 255 times.
339 } 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 171 times.
255 } 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 87 times.
171 } 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 3 times.
87 } else if (s == sym_num_last_free) {
152 84 res = lbm_enc_u(hs.gc_last_free);
153 } else {
154 3 res = ENC_SYM_NIL;
155 }
156 }
157 783 return res;
158 }
159
160 2022 lbm_value ext_env_get(lbm_value *args, lbm_uint argn) {
161
4/4
✓ Branch 0 taken 2019 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 2016 times.
✓ Branch 3 taken 3 times.
2022 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 6 return ENC_SYM_TERROR;
166 }
167
168 3 lbm_value ext_local_env_get(lbm_value *args, lbm_uint argn) {
169 (void) args;
170 (void) argn;
171 3 eval_context_t *ctx = lbm_get_current_context();
172 3 return ctx->curr_env;
173 }
174
175 174 lbm_value ext_env_set(lbm_value *args, lbm_uint argn) {
176
4/4
✓ Branch 0 taken 171 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 168 times.
✓ Branch 3 taken 3 times.
174 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 6 return ENC_SYM_NIL;
183 }
184
185 24 lbm_value ext_env_drop(lbm_value *args, lbm_uint argn) {
186 24 lbm_value r = ENC_SYM_TERROR;
187
4/4
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 3 times.
24 if (argn == 2 && lbm_is_symbol(args[0])) {
188 12 r = lbm_env_drop_binding(args[1], args[0]);
189 }
190 24 return r;
191 }
192
193 6 lbm_value ext_global_env_size(lbm_value *args, lbm_uint argn) {
194 (void) args;
195 (void) argn; // ignores any and all arguments.
196 6 return lbm_enc_u(lbm_get_global_env_size());
197 }
198
199 516 lbm_value ext_set_gc_stack_size(lbm_value *args, lbm_uint argn) {
200
2/2
✓ Branch 0 taken 513 times.
✓ Branch 1 taken 3 times.
516 if (argn == 1) {
201
2/2
✓ Branch 0 taken 510 times.
✓ Branch 1 taken 3 times.
513 if (lbm_is_number(args[0])) {
202 510 uint32_t n = lbm_dec_as_u32(args[0]);
203 510 lbm_uint *new_stack = lbm_malloc(n * sizeof(lbm_uint));
204
2/2
✓ Branch 0 taken 504 times.
✓ Branch 1 taken 6 times.
510 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 6 return ENC_SYM_MERROR;
212 }
213 }
214 6 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 9 void find_cid(eval_context_t *ctx, void *arg1, void *arg2) {
266 9 lbm_cid id = (lbm_cid)arg1;
267
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6 times.
9 if (ctx->id == id) {
268 3 *(eval_context_t**)arg2 = ctx;
269 }
270 9 }
271
272
273 12 lbm_value ext_mailbox_get(lbm_uint *args, lbm_uint argn) {
274 12 lbm_value res = ENC_SYM_TERROR;
275 12 eval_context_t *ctx = NULL;
276
277
4/4
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 3 times.
12 if (argn == 1 && lbm_is_number(args[0])) {
278 6 res = ENC_SYM_NIL;
279 6 lbm_cid cid = lbm_dec_as_i32(args[0]);
280 6 lbm_all_ctxs_iterator(find_cid, (void*)cid, (void*)&ctx);
281
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 if (ctx) {
282 3 uint32_t num_mail = ctx->num_mail;
283 3 lbm_value ls = (lbm_heap_allocate_list(num_mail));
284 3 res = ls;
285
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (lbm_is_ptr(ls)) {
286 3 lbm_value curr = ls;
287 3 int i = 0;
288
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 3 times.
12 while (lbm_is_ptr(curr)) {
289 9 lbm_set_car(curr, ctx->mailbox[i++]);
290 9 curr = lbm_cdr(curr);
291 }
292 }
293 }
294 }
295 12 return res;
296 }
297 #endif
298
299
300 66906 void lbm_runtime_extensions_init(void) {
301
302 #ifdef FULL_RTS_LIB
303 66906 lbm_add_symbol_const("get-heap-size", &sym_heap_size);
304 66906 lbm_add_symbol_const("get-heap-bytes", &sym_heap_bytes);
305 66906 lbm_add_symbol_const("get-num-alloc-cells", &sym_num_alloc_cells);
306 66906 lbm_add_symbol_const("get-num-alloc-arrays", &sym_num_alloc_arrays);
307 66906 lbm_add_symbol_const("get-gc-num", &sym_num_gc);
308 66906 lbm_add_symbol_const("get-gc-num-marked", &sym_num_gc_marked);
309 66906 lbm_add_symbol_const("get-gc-num-recovered-cells", &sym_num_gc_recovered_cells);
310 66906 lbm_add_symbol_const("get-gc-num-recovered-arrays", &sym_num_gc_recovered_arrays);
311 66906 lbm_add_symbol_const("get-gc-num-least-free", &sym_num_least_free);
312 66906 lbm_add_symbol_const("get-gc-num-last-free", &sym_num_last_free);
313
314 66906 lbm_add_symbol_const("little-endian", &little_endian);
315 66906 lbm_add_symbol_const("big-endian", &big_endian);
316 #endif
317
318 #if defined(LBM_USE_EXT_MAILBOX_GET) || defined(FULL_RTS_LIB)
319 66906 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 66906 lbm_add_extension("is-always-gc",ext_is_always_gc);
327 66906 lbm_add_extension("set-eval-quota", ext_eval_set_quota);
328 66906 lbm_add_extension("hide-trapped-error", ext_hide_trapped_error);
329 66906 lbm_add_extension("show-trapped-error", ext_show_trapped_error);
330 66906 lbm_add_extension("mem-num-free", ext_memory_num_free);
331 66906 lbm_add_extension("mem-longest-free", ext_memory_longest_free);
332 66906 lbm_add_extension("mem-size", ext_memory_size);
333 66906 lbm_add_extension("word-size", ext_memory_word_size);
334 66906 lbm_add_extension("lbm-version", ext_lbm_version);
335 66906 lbm_add_extension("lbm-endian", ext_lbm_endianness);
336 66906 lbm_add_extension("lbm-heap-state", ext_lbm_heap_state);
337 66906 lbm_add_extension("env-get", ext_env_get);
338 66906 lbm_add_extension("env-set", ext_env_set);
339 66906 lbm_add_extension("env-drop", ext_env_drop);
340 66906 lbm_add_extension("local-env-get", ext_local_env_get);
341 66906 lbm_add_extension("global-env-size", ext_global_env_size);
342 66906 lbm_add_extension("set-gc-stack-size", ext_set_gc_stack_size);
343 66906 lbm_add_extension("is-64bit", ext_is_64bit);
344 66906 lbm_add_extension("symtab-size", ext_symbol_table_size);
345 66906 lbm_add_extension("symtab-size-flash", ext_symbol_table_size_flash);
346 66906 lbm_add_extension("symtab-size-names", ext_symbol_table_size_names);
347 66906 lbm_add_extension("symtab-size-names-flash", ext_symbol_table_size_names_flash);
348 #endif
349 66906 }
350