GCC Code Coverage Report


Directory: ../src/
File: lbm_c_interop.c
Date: 2026-07-02 10:10:49
Coverage Exec Excl Total
Lines: 99.3% 145 0 146
Functions: 100.0% 20 0 20
Branches: 70.2% 59 0 84

Line Branch Exec Source
1 /*
2 Copyright 2022, 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_c_interop.h"
19
20 /* Utility */
21
22 66512 static bool lift_char_channel(lbm_char_channel_t *chan , lbm_value *res) {
23 66512 lbm_value cell = lbm_heap_allocate_cell(LBM_TYPE_CHANNEL, (lbm_uint) chan, ENC_SYM_CHANNEL_TYPE);
24 66512 bool rval = false;
25
1/2
✓ Branch 0 taken 66512 times.
✗ Branch 1 not taken.
66512 if (cell != ENC_SYM_MERROR) {
26 66512 *res = cell;
27 66512 rval = true;
28 }
29 66512 return rval;
30 }
31
32
33
34 /****************************************************/
35 /* Interface for loading and running programs and */
36 /* expressions */
37
38 66508 lbm_cid eval_cps_load_and_eval(lbm_char_channel_t *tokenizer, bool program, bool incremental, char *name) {
39
40 lbm_value stream;
41 66508 lbm_cid cid = -1;
42
1/2
✓ Branch 1 taken 66508 times.
✗ Branch 2 not taken.
66508 if (lift_char_channel(tokenizer, &stream)) {
43 66508 lbm_value read_mode = ENC_SYM_READ;
44
2/2
✓ Branch 0 taken 66498 times.
✓ Branch 1 taken 10 times.
66508 if (program) {
45
2/2
✓ Branch 0 taken 33438 times.
✓ Branch 1 taken 33060 times.
66498 if (incremental) {
46 33438 read_mode = ENC_SYM_READ_AND_EVAL_PROGRAM;
47 } else {
48 33060 read_mode = ENC_SYM_READ_PROGRAM;
49 }
50 }
51 /*
52 read-eval-program finishes with the result of the final expression in
53 the program. This should not be passed to eval-program as it is most likely
54 not a program. Even if it is a program, its not one we want to evaluate.
55 */
56
57 /* LISP ZONE */
58 66508 lbm_value launcher = lbm_cons(stream, ENC_SYM_NIL);
59 66508 launcher = lbm_cons(read_mode, launcher);
60 lbm_value evaluator;
61 lbm_value start_prg;
62
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 66498 times.
66508 if (read_mode == ENC_SYM_READ) {
63 10 evaluator = lbm_cons(launcher, ENC_SYM_NIL);
64 10 evaluator = lbm_cons(ENC_SYM_EVAL, evaluator);
65 10 start_prg = lbm_cons(evaluator, ENC_SYM_NIL);
66
2/2
✓ Branch 0 taken 33060 times.
✓ Branch 1 taken 33438 times.
66498 } else if (read_mode == ENC_SYM_READ_PROGRAM) {
67 33060 evaluator = lbm_cons(launcher, ENC_SYM_NIL);
68 33060 evaluator = lbm_cons(ENC_SYM_EVAL_PROGRAM, evaluator);
69 33060 start_prg = lbm_cons(evaluator, ENC_SYM_NIL);
70 } else { // ENC_SYM_READ_AND_EVAL_PROGRAM
71 33438 evaluator = launcher; // dummy so check below passes
72 33438 start_prg = lbm_cons(launcher, ENC_SYM_NIL);
73 }
74
75 /* LISP ZONE ENDS */
76
77
2/4
✓ Branch 1 taken 66508 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 66508 times.
✗ Branch 4 not taken.
133016 if (lbm_type_of(launcher) == LBM_TYPE_CONS &&
78
1/2
✓ Branch 1 taken 66508 times.
✗ Branch 2 not taken.
133016 lbm_type_of(evaluator) == LBM_TYPE_CONS &&
79 66508 lbm_type_of(start_prg) == LBM_TYPE_CONS ) {
80 66508 cid = lbm_create_ctx(start_prg, ENC_SYM_NIL, 256, name);
81 }
82 }
83 66508 return cid;
84 }
85
86 4 lbm_cid eval_cps_load_and_define(lbm_char_channel_t *tokenizer, char *symbol, bool program) {
87 lbm_value stream;
88 4 lbm_cid cid = -1;
89
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 if (lift_char_channel(tokenizer, &stream)) {
90 lbm_uint sym_id;
91
2/4
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
8 if (lbm_get_symbol_by_name(symbol, &sym_id) ||
92 4 lbm_add_symbol_base(symbol, &sym_id)) {
93 /* LISP ZONE */
94 4 lbm_value launcher = lbm_cons(stream, lbm_enc_sym(SYM_NIL));
95
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 launcher = lbm_cons(lbm_enc_sym(program ? SYM_READ_PROGRAM : SYM_READ), launcher);
96 4 lbm_value binding = lbm_cons(launcher, lbm_enc_sym(SYM_NIL));
97 4 binding = lbm_cons(lbm_enc_sym(sym_id), binding);
98 4 lbm_value definer = lbm_cons(lbm_enc_sym(SYM_DEFINE), binding);
99 4 definer = lbm_cons(definer, lbm_enc_sym(SYM_NIL));
100 /* LISP ZONE ENDS */
101
102
2/4
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
8 if (lbm_type_of(launcher) == LBM_TYPE_CONS &&
103
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
8 lbm_type_of(binding) == LBM_TYPE_CONS &&
104 4 lbm_type_of(definer) == LBM_TYPE_CONS ) {
105 4 cid = lbm_create_ctx(definer, lbm_enc_sym(SYM_NIL), 256, NULL);
106 }
107 }
108 }
109 4 return cid;
110 }
111
112 5 lbm_cid lbm_eval_defined(char *symbol, bool program) {
113
114 lbm_uint sym_id;
115 lbm_value binding;
116 5 lbm_cid cid = -1;
117
3/4
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
8 if (lbm_get_symbol_by_name(symbol, &sym_id) &&
118 3 lbm_global_env_lookup(&binding, lbm_enc_sym(sym_id))) {
119
120 /* LISP ZONE */
121 3 lbm_value launcher = lbm_cons(lbm_enc_sym(sym_id), lbm_enc_sym(SYM_NIL));
122 3 lbm_value evaluator = launcher;
123
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 2 times.
3 evaluator = lbm_cons(lbm_enc_sym(program ? SYM_EVAL_PROGRAM : SYM_EVAL), evaluator);
124 3 lbm_value start_prg = lbm_cons(evaluator, lbm_enc_sym(SYM_NIL));
125 /* LISP ZONE ENDS */
126
127
2/4
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
6 if (lbm_type_of(launcher) == LBM_TYPE_CONS &&
128
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
6 lbm_type_of(evaluator) == LBM_TYPE_CONS &&
129 3 lbm_type_of(start_prg) == LBM_TYPE_CONS ) {
130 3 cid = lbm_create_ctx(start_prg, lbm_enc_sym(SYM_NIL), 256, NULL);
131 }
132 }
133 5 return cid;
134 }
135
136
137
138 10 lbm_cid lbm_load_and_eval_expression(lbm_char_channel_t *tokenizer) {
139 10 return eval_cps_load_and_eval(tokenizer, false,false, NULL);
140 }
141
142 2 lbm_cid lbm_load_and_define_expression(lbm_char_channel_t *tokenizer, char *symbol) {
143 2 return eval_cps_load_and_define(tokenizer, symbol, false);
144 }
145
146 33060 lbm_cid lbm_load_and_eval_program(lbm_char_channel_t *tokenizer, char *name) {
147 33060 return eval_cps_load_and_eval(tokenizer, true, false, name);
148 }
149
150 33438 lbm_cid lbm_load_and_eval_program_incremental(lbm_char_channel_t *tokenizer, char *name) {
151 33438 return eval_cps_load_and_eval(tokenizer, true, true, name);
152 }
153
154 2 lbm_cid lbm_load_and_define_program(lbm_char_channel_t *tokenizer, char *symbol) {
155 2 return eval_cps_load_and_define(tokenizer, symbol, true);
156 }
157
158 3 lbm_cid lbm_eval_defined_expression(char *symbol) {
159 3 return lbm_eval_defined(symbol, false);
160 }
161
162 2 lbm_cid lbm_eval_defined_program(char *symbol) {
163 2 return lbm_eval_defined(symbol, true);
164 }
165
166 2 int lbm_send_message(lbm_cid cid, lbm_value msg) {
167 2 int res = 0;
168
169 #ifndef LBM_SINGLE_THREADED
170
2/2
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 1 time.
2 if (lbm_get_eval_state() == EVAL_CPS_STATE_PAUSED) {
171 #endif
172
1/2
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
1 if (lbm_find_receiver_and_send(cid, msg)) {
173 1 res = 1;
174 }
175 #ifndef LBM_SINGLE_THREADED
176 }
177 #endif
178 2 return res;
179 }
180
181 17 int lbm_define(char *symbol, lbm_value value) {
182 17 int res = 0;
183
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 1 time.
17 if (symbol) {
184 lbm_uint sym_id;
185 #ifndef LBM_SINGLE_THREADED
186
2/2
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 1 time.
16 if (lbm_get_eval_state() == EVAL_CPS_STATE_PAUSED) {
187 #endif
188
3/4
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 14 times.
✗ Branch 4 not taken.
29 if (lbm_get_symbol_by_name(symbol, &sym_id) ||
189 14 lbm_add_symbol_const_base(symbol, &sym_id, false)) {
190 15 lbm_uint ix_key = sym_id & GLOBAL_ENV_MASK;
191 15 lbm_value *glob_env = lbm_get_global_env();
192 15 lbm_value new_env_entry = lbm_env_set(glob_env[ix_key], lbm_enc_sym(sym_id), value);
193
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
15 if (lbm_is_symbol(new_env_entry)) {
194 ✗ res = 0;
195 } else {
196 15 glob_env[ix_key] = new_env_entry;
197 15 res = 1;
198 }
199 }
200 #ifndef LBM_SINGLE_THREADED
201 }
202 #endif
203 }
204 17 return res;
205 }
206
207 6 int lbm_undefine(char *symbol) {
208 lbm_uint sym_id;
209 6 int res = 0;
210
4/4
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 time.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 2 times.
6 if (symbol && lbm_get_symbol_by_name(symbol, &sym_id)) {
211
212 3 lbm_value *glob_env = lbm_get_global_env();
213 3 lbm_uint ix_key = sym_id & GLOBAL_ENV_MASK;
214 3 lbm_value new_env = lbm_env_drop_binding(glob_env[ix_key], lbm_enc_sym(sym_id));
215
216
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 time.
3 if (new_env != ENC_SYM_NOT_FOUND) {
217 2 glob_env[ix_key] = new_env;
218 2 res = 1;
219 }
220 }
221 6 return res;
222 }
223
224 3 int lbm_share_array(lbm_value *value, char *data, lbm_uint num_elt) {
225 3 return lbm_lift_array(value, data, num_elt);
226 }
227
228 168 static bool share_array_const(lbm_value flash_cell, char *data, lbm_uint num_elt) {
229 lbm_array_header_t flash_array_header;
230 168 flash_array_header.size = num_elt;
231 168 flash_array_header.data = (lbm_uint*)data;
232 lbm_uint flash_array_header_ptr;
233 168 lbm_flash_status s = lbm_write_const_raw((lbm_uint*)&flash_array_header,
234 sizeof(lbm_array_header_t) / sizeof(lbm_uint),
235 &flash_array_header_ptr);
236
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 168 times.
168 if (s != LBM_FLASH_WRITE_OK) return false;
237 168 s = write_const_car(flash_cell, flash_array_header_ptr);
238
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 168 times.
168 if (s != LBM_FLASH_WRITE_OK) return false;
239 168 s = write_const_cdr(flash_cell, ENC_SYM_ARRAY_TYPE);
240
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 168 times.
168 if (s != LBM_FLASH_WRITE_OK) return false;
241 168 return true;
242 }
243
244 168 int lbm_share_array_const(lbm_value *res, char *flash_ptr, lbm_uint num_elt) {
245 168 lbm_value arr = LBM_PTR_BIT | LBM_TYPE_ARRAY;
246 168 lbm_value flash_arr = 0;
247 168 int r = 0;
248
1/2
✓ Branch 1 taken 168 times.
✗ Branch 2 not taken.
168 if (request_flash_storage_cell(arr, &flash_arr) == LBM_FLASH_WRITE_OK) {
249
1/2
✓ Branch 1 taken 168 times.
✗ Branch 2 not taken.
168 if (share_array_const(flash_arr, flash_ptr, num_elt)) {
250 168 *res = flash_arr;
251 168 r = 1;
252 }
253 }
254 168 return r;
255 }
256
257 // TODO: There is no NULL check in lbm_heap_allocate_array.
258 // Users must provide a valid pointer.
259 415210 int lbm_create_array(lbm_value *value, lbm_uint num_elt) {
260 415210 return lbm_heap_allocate_array(value, num_elt);
261 }
262
263
264 1 void lbm_clear_env(void) {
265
266 1 lbm_value *env = lbm_get_global_env();
267
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 1 time.
33 for (int i = 0; i < GLOBAL_ENV_ROOTS; i ++) {
268 32 env[i] = ENC_SYM_NIL;
269 }
270 1 lbm_perform_gc();
271 1 }
272
273 // Evaluator should be paused when running this.
274 // Running gc will reclaim the fv storage.
275 3 bool lbm_flatten_env(int index, lbm_uint** data, lbm_uint *size) {
276
4/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 1 time.
3 if (index < 0 || index >= GLOBAL_ENV_ROOTS) return false;
277 1 lbm_value *env = lbm_get_global_env();
278
279 1 lbm_value fv = flatten_value(env[index]);
280
281
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
1 if (lbm_is_symbol(fv)) return false;
282
283 1 lbm_array_header_t *array = lbm_dec_array_r(fv);
284 1 bool rval = false;
285
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 if (array) {
286 1 *size = array->size;
287 1 *data = array->data;
288 1 rval = true;
289 }
290 1 return rval;
291 }
292