GCC Code Coverage Report


Directory: ../src/
File: extensions/runtime_extensions.c
Date: 2026-10-16 14:49:47
Coverage Exec Excl Total
Lines: 98.8% 162 0 164
Functions: 96.0% 24 0 25
Branches: 98.2% 55 0 56

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 42 lbm_value ext_eval_set_quota(lbm_value *args, lbm_uint argn) {
51
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 39 times.
42 LBM_CHECK_ARGN_NUMBER(1);
52 39 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 36 lbm_set_eval_step_quota(q);
57 #endif
58 39 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 1371 lbm_value ext_memory_num_free(lbm_value *args, lbm_uint argn) {
77 (void)args;
78 (void)argn;
79 1371 lbm_uint n = lbm_memory_num_free();
80 1371 return lbm_enc_i((lbm_int)n);
81 }
82
83 144 lbm_value ext_memory_longest_free(lbm_value *args, lbm_uint argn) {
84 (void)args;
85 (void)argn;
86 144 lbm_uint n = lbm_memory_longest_free();
87 144 return lbm_enc_i((lbm_int)n);
88 }
89
90 36 lbm_value ext_memory_size(lbm_value *args, lbm_uint argn) {
91 (void)args;
92 (void)argn;
93 36 lbm_uint n = lbm_memory_num_words();
94 36 return lbm_enc_i((lbm_int)n);
95 }
96
97 411 lbm_value ext_memory_word_size(lbm_value *args, lbm_uint argn) {
98 (void)args;
99 (void)argn;
100 411 return lbm_enc_i((lbm_int)sizeof(lbm_uint));
101 }
102
103 39 lbm_value ext_lbm_version(lbm_value *args, lbm_uint argn) {
104 (void) args;
105 (void) argn;
106 39 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 39 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 351 lbm_value ext_lbm_heap_state(lbm_value *args, lbm_uint argn) {
124
125 351 lbm_value res = ENC_SYM_TERROR;
126
127 lbm_heap_state_t hs;
128 351 lbm_get_heap_state(&hs);
129
130
4/4
✓ Branch 0 taken 342 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 336 times.
✓ Branch 3 taken 6 times.
693 if (argn == 1 &&
131 342 lbm_is_symbol(args[0])) {
132 336 lbm_uint s = lbm_dec_sym(args[0]);
133
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 297 times.
336 if (s == sym_heap_size) {
134 39 res = lbm_enc_u(hs.heap_size);
135
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 258 times.
297 } else if (s == sym_heap_bytes) {
136 39 res = lbm_enc_u(hs.heap_bytes);
137
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 222 times.
258 } else if (s == sym_num_alloc_cells) {
138 36 res = lbm_enc_u(hs.heap_size - hs.num_free);
139
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 219 times.
222 } else if (s == sym_num_alloc_arrays) {
140 3 res = lbm_enc_u(hs.num_alloc_arrays);
141
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 183 times.
219 } else if (s == sym_num_gc) {
142 36 res = lbm_enc_u(hs.gc_num);
143
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 147 times.
183 } else if (s == sym_num_gc_marked) {
144 36 res = lbm_enc_u(hs.gc_marked);
145
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 111 times.
147 } else if (s == sym_num_gc_recovered_cells) {
146 36 res = lbm_enc_u(hs.gc_recovered);
147
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 75 times.
111 } else if (s == sym_num_gc_recovered_arrays) {
148 36 res = lbm_enc_u(hs.gc_recovered_arrays);
149
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 39 times.
75 } else if (s == sym_num_least_free) {
150 36 res = lbm_enc_u(hs.gc_least_free);
151
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 3 times.
39 } else if (s == sym_num_last_free) {
152 36 res = lbm_enc_u(hs.gc_last_free);
153 } else {
154 3 res = ENC_SYM_NIL;
155 }
156 }
157 351 return res;
158 }
159
160 906 lbm_value ext_env_get(lbm_value *args, lbm_uint argn) {
161
4/4
✓ Branch 0 taken 903 times.
✓ Branch 1 taken 3 times.
✓ Branch 3 taken 900 times.
✓ Branch 4 taken 3 times.
906 if (argn == 1 && lbm_is_number(args[0])) {
162 900 lbm_uint ix = lbm_dec_as_u32(args[0]) & GLOBAL_ENV_MASK;
163 900 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 78 lbm_value ext_env_set(lbm_value *args, lbm_uint argn) {
176
4/4
✓ Branch 0 taken 75 times.
✓ Branch 1 taken 3 times.
✓ Branch 3 taken 72 times.
✓ Branch 4 taken 3 times.
78 if (argn == 2 && lbm_is_number(args[0])) {
177 72 lbm_uint ix = lbm_dec_as_u32(args[0]) & GLOBAL_ENV_MASK;
178 72 lbm_value *glob_env = lbm_get_global_env();
179 72 glob_env[ix] = args[1];
180 72 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 3 taken 12 times.
✓ Branch 4 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 228 lbm_value ext_set_gc_stack_size(lbm_value *args, lbm_uint argn) {
200
2/2
✓ Branch 0 taken 225 times.
✓ Branch 1 taken 3 times.
228 if (argn == 1) {
201
2/2
✓ Branch 1 taken 222 times.
✓ Branch 2 taken 3 times.
225 if (lbm_is_number(args[0])) {
202 222 uint32_t n = lbm_dec_as_u32(args[0]);
203 222 lbm_uint *new_stack = lbm_malloc(n * sizeof(lbm_uint));
204
2/2
✓ Branch 0 taken 216 times.
✓ Branch 1 taken 6 times.
222 if (new_stack) {
205 216 lbm_free(lbm_heap_state.gc_stack.data);
206 216 lbm_heap_state.gc_stack.data = new_stack;
207 216 lbm_heap_state.gc_stack.size = n;
208 216 lbm_heap_state.gc_stack.sp = 0; // should already be 0
209 216 return ENC_SYM_TRUE;
210 }
211 6 return ENC_SYM_MERROR;
212 }
213 }
214 6 return ENC_SYM_TERROR;
215 }
216
217 360 lbm_value ext_is_64bit(lbm_value *args, lbm_uint argn) {
218 (void) args;
219 (void) argn;
220 #ifndef LBM64
221 240 return ENC_SYM_NIL;
222 #else
223 120 return ENC_SYM_TRUE;
224 #endif
225 }
226
227 36 lbm_value ext_symbol_table_size(lbm_uint *args, lbm_uint argn) {
228 (void) args;
229 (void) argn;
230 36 return lbm_enc_u(lbm_get_symbol_table_size());
231 }
232
233 36 lbm_value ext_symbol_table_size_flash(lbm_uint *args, lbm_uint argn) {
234 (void) args;
235 (void) argn;
236 36 return lbm_enc_u(lbm_get_symbol_table_size_flash());
237 }
238
239 36 lbm_value ext_symbol_table_size_names(lbm_uint *args, lbm_uint argn) {
240 (void) args;
241 (void) argn;
242 36 return lbm_enc_u(lbm_get_symbol_table_size_names());
243 }
244
245 36 lbm_value ext_symbol_table_size_names_flash(lbm_uint *args, lbm_uint argn) {
246 (void) args;
247 (void) argn;
248 36 return lbm_enc_u(lbm_get_symbol_table_size_names_flash());
249 }
250
251 174 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 174 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 3 taken 6 times.
✓ Branch 4 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 1 taken 3 times.
✗ Branch 2 not taken.
3 if (lbm_is_ptr(ls)) {
286 3 lbm_value curr = ls;
287 3 int i = 0;
288
2/2
✓ Branch 1 taken 9 times.
✓ Branch 2 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 29167 void lbm_runtime_extensions_init(void) {
301
302 #ifdef FULL_RTS_LIB
303 29167 lbm_add_symbol_const("get-heap-size", &sym_heap_size);
304 29167 lbm_add_symbol_const("get-heap-bytes", &sym_heap_bytes);
305 29167 lbm_add_symbol_const("get-num-alloc-cells", &sym_num_alloc_cells);
306 29167 lbm_add_symbol_const("get-num-alloc-arrays", &sym_num_alloc_arrays);
307 29167 lbm_add_symbol_const("get-gc-num", &sym_num_gc);
308 29167 lbm_add_symbol_const("get-gc-num-marked", &sym_num_gc_marked);
309 29167 lbm_add_symbol_const("get-gc-num-recovered-cells", &sym_num_gc_recovered_cells);
310 29167 lbm_add_symbol_const("get-gc-num-recovered-arrays", &sym_num_gc_recovered_arrays);
311 29167 lbm_add_symbol_const("get-gc-num-least-free", &sym_num_least_free);
312 29167 lbm_add_symbol_const("get-gc-num-last-free", &sym_num_last_free);
313
314 29167 lbm_add_symbol_const("little-endian", &little_endian);
315 29167 lbm_add_symbol_const("big-endian", &big_endian);
316 #endif
317
318 #if defined(LBM_USE_EXT_MAILBOX_GET) || defined(FULL_RTS_LIB)
319 29167 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 29167 lbm_add_extension("is-always-gc",ext_is_always_gc);
327 29167 lbm_add_extension("set-eval-quota", ext_eval_set_quota);
328 29167 lbm_add_extension("hide-trapped-error", ext_hide_trapped_error);
329 29167 lbm_add_extension("show-trapped-error", ext_show_trapped_error);
330 29167 lbm_add_extension("mem-num-free", ext_memory_num_free);
331 29167 lbm_add_extension("mem-longest-free", ext_memory_longest_free);
332 29167 lbm_add_extension("mem-size", ext_memory_size);
333 29167 lbm_add_extension("word-size", ext_memory_word_size);
334 29167 lbm_add_extension("lbm-version", ext_lbm_version);
335 29167 lbm_add_extension("lbm-endian", ext_lbm_endianness);
336 29167 lbm_add_extension("lbm-heap-state", ext_lbm_heap_state);
337 29167 lbm_add_extension("env-get", ext_env_get);
338 29167 lbm_add_extension("env-set", ext_env_set);
339 29167 lbm_add_extension("env-drop", ext_env_drop);
340 29167 lbm_add_extension("local-env-get", ext_local_env_get);
341 29167 lbm_add_extension("global-env-size", ext_global_env_size);
342 29167 lbm_add_extension("set-gc-stack-size", ext_set_gc_stack_size);
343 29167 lbm_add_extension("is-64bit", ext_is_64bit);
344 29167 lbm_add_extension("symtab-size", ext_symbol_table_size);
345 29167 lbm_add_extension("symtab-size-flash", ext_symbol_table_size_flash);
346 29167 lbm_add_extension("symtab-size-names", ext_symbol_table_size_names);
347 29167 lbm_add_extension("symtab-size-names-flash", ext_symbol_table_size_names_flash);
348 #endif
349 29167 }
350