GCC Code Coverage Report


Directory: ../src/
File: /home/joels/Current/lispbm/src/symrepr.c
Date: 2026-07-11 23:13:58
Exec Total Coverage
Lines: 110 129 85.3%
Functions: 16 20 80.0%
Branches: 37 51 72.5%

Line Branch Exec Source
1 /*
2 Copyright 2018, 2021, 2022, 2024, 2025 Joel Svensson svenssonjoel@yahoo.se
3 2025 Rasmus Söderhielm rasmus.soderhielm@gmail.com
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include <stdint.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include <inttypes.h>
24
25
26 #include <lbm_memory.h>
27 #include <heap.h>
28 #include "symrepr.h"
29 #include "extensions.h"
30 #include "lbm_utils.h"
31 #include "lbm_image.h"
32
33 #define NUM_SPECIAL_SYMBOLS (sizeof(special_symbols) / sizeof(special_sym))
34 #define NAME 0
35 #define ID 1
36 #define NEXT 2
37
38 typedef struct {
39 const char *name;
40 const lbm_uint id;
41 } special_sym;
42
43 special_sym const special_symbols[] = {
44 {"nil" , SYM_NIL},
45 {"quote" , SYM_QUOTE},
46 {"t" , SYM_TRUE},
47 {"if" , SYM_IF},
48 {"cond" , SYM_COND},
49 {"lambda" , SYM_LAMBDA},
50 {"closure" , SYM_CLOSURE},
51 {"let" , SYM_LET},
52 {"define" , SYM_DEFINE},
53 {"progn" , SYM_PROGN},
54 {"read" , SYM_READ},
55 {"read-program" , SYM_READ_PROGRAM},
56 {"read-eval-program", SYM_READ_AND_EVAL_PROGRAM},
57 {"match" , SYM_MATCH},
58 {"_" , SYM_DONTCARE},
59 {"send" , SYM_SEND},
60 {"recv" , SYM_RECEIVE},
61 {"recv-to" , SYM_RECEIVE_TIMEOUT},
62 {"macro" , SYM_MACRO},
63 {"call-cc" , SYM_CALLCC},
64 {"continuation" , SYM_CONT},
65 {"var" , SYM_PROGN_VAR},
66 {"timeout" , SYM_TIMEOUT},
67
68 {"set" , SYM_SETVAR},
69 {"setq" , SYM_SETQ},
70 {"move-to-flash", SYM_MOVE_TO_FLASH},
71 {"exit-ok" , SYM_EXIT_OK},
72 {"exit-error" , SYM_EXIT_ERROR},
73 {"map" , SYM_MAP},
74 {"reverse" , SYM_REVERSE},
75 {"flatten" , SYM_FLATTEN},
76 {"unflatten" , SYM_UNFLATTEN},
77 {"kill" , SYM_KILL},
78 {"sleep" , SYM_SLEEP},
79 {"merge" , SYM_MERGE},
80 {"sort" , SYM_SORT},
81 {"gc" , SYM_PERFORM_GC},
82 {"loop" , SYM_LOOP},
83 {"trap" , SYM_TRAP},
84 {"rest-args" , SYM_REST_ARGS},
85 {"rotate" , SYM_ROTATE},
86 {"call-cc-unsafe", SYM_CALL_CC_UNSAFE},
87 {"apply" , SYM_APPLY},
88
89 // pattern matching
90 {"?" , SYM_MATCH_ANY},
91
92 // Error symbols with parsable names
93 {"no_match" , SYM_NO_MATCH},
94 {"read_error" , SYM_RERROR},
95 {"type_error" , SYM_TERROR},
96 {"eval_error" , SYM_EERROR},
97 {"out_of_memory" , SYM_MERROR},
98 {"fatal_error" , SYM_FATAL_ERROR},
99 {"out_of_stack" , SYM_STACK_ERROR},
100 {"division_by_zero" , SYM_DIVZERO},
101 {"variable_not_bound" , SYM_NOT_FOUND},
102 {"flash_full" , SYM_ERROR_FLASH_HEAP_FULL},
103
104 // Special symbols with unparsable names
105 {"$barray" , SYM_ARRAY_TYPE},
106 {"$raw_i" , SYM_RAW_I_TYPE},
107 {"$raw_u" , SYM_RAW_U_TYPE},
108 {"$raw_f" , SYM_RAW_F_TYPE},
109 {"$ind_i" , SYM_IND_I_TYPE},
110 {"$ind_u" , SYM_IND_U_TYPE},
111 {"$ind_f" , SYM_IND_F_TYPE},
112 {"$channel" , SYM_CHANNEL_TYPE},
113 {"$recovered" , SYM_RECOVERED},
114 {"$placeholder" , SYM_PLACEHOLDER},
115 {"$custom" , SYM_CUSTOM_TYPE},
116 {"$array" , SYM_LISPARRAY_TYPE},
117 {"$nonsense" , SYM_NONSENSE},
118 {"$dm-array" , SYM_DEFRAG_ARRAY_TYPE},
119 {"$dm" , SYM_DEFRAG_MEM_TYPE},
120
121 // tokenizer symbols with unparsable names
122 {"[openpar]" , SYM_OPENPAR},
123 {"[closepar]" , SYM_CLOSEPAR},
124 {"[backquote]" , SYM_BACKQUOTE},
125 {"[comma]" , SYM_COMMA},
126 {"[commaat]" , SYM_COMMAAT},
127 {"[dot]" , SYM_DOT},
128 {"[done]" , SYM_TOKENIZER_DONE},
129 {"[quote_it]" , SYM_QUOTE_IT},
130 {"[colon]" , SYM_COLON},
131 {"[wait]" , SYM_TOKENIZER_WAIT},
132 {"[openbrack]" , SYM_OPENBRACK},
133 {"[closebrack]" , SYM_CLOSEBRACK},
134 {"[rerror]" , SYM_TOKENIZER_RERROR},
135 {"[appcont]" , SYM_APP_CONT},
136 {"[openarr]" , SYM_OPENARRAY},
137 {"[closearr]" , SYM_CLOSEARRAY},
138
139 // special symbols with parseable names
140 {"type-list" , SYM_TYPE_LIST},
141 {"type-i" , SYM_TYPE_I},
142 {"type-u" , SYM_TYPE_U},
143 {"type-float" , SYM_TYPE_FLOAT},
144 {"type-i32" , SYM_TYPE_I32},
145 {"type-u32" , SYM_TYPE_U32},
146 {"type-double" , SYM_TYPE_DOUBLE},
147 {"type-i64" , SYM_TYPE_I64},
148 {"type-u64" , SYM_TYPE_U64},
149 {"type-array" , SYM_TYPE_ARRAY},
150 {"type-symbol" , SYM_TYPE_SYMBOL},
151 {"type-char" , SYM_TYPE_CHAR},
152 {"type-byte" , SYM_TYPE_BYTE},
153 {"type-channel" , SYM_TYPE_CHANNEL},
154 {"type-lisparray" , SYM_TYPE_LISPARRAY},
155 {"type-dm" , SYM_TYPE_DEFRAG_MEM},
156 {"type-custom" , SYM_TYPE_CUSTOM},
157
158 // Fundamental operations
159 {"+" , SYM_ADD},
160 {"-" , SYM_SUB},
161 {"*" , SYM_MUL},
162 {"/" , SYM_DIV},
163 {"//" , SYM_INT_DIV},
164 {"mod" , SYM_MOD},
165 {"=" , SYM_NUMEQ},
166 {"!=" , SYM_NUM_NOT_EQ},
167 {"<" , SYM_LT},
168 {">" , SYM_GT},
169 {"<=" , SYM_LEQ},
170 {">=" , SYM_GEQ},
171 {"eval" , SYM_EVAL},
172 {"eval-program" , SYM_EVAL_PROGRAM},
173 {"and" , SYM_AND},
174 {"or" , SYM_OR},
175 {"not" , SYM_NOT},
176 {"yield" , SYM_YIELD},
177 {"wait" , SYM_WAIT},
178 {"spawn" , SYM_SPAWN},
179 {"atomic" , SYM_ATOMIC},
180 {"self" , SYM_SELF},
181 {"spawn-trap" , SYM_SPAWN_TRAP},
182 {"set-mailbox-size" , SYM_SET_MAILBOX_SIZE},
183 {"eq" , SYM_EQ},
184 {"not-eq" , SYM_NOT_EQ},
185 {"car" , SYM_CAR},
186 {"cdr" , SYM_CDR},
187 {"cons" , SYM_CONS},
188 {"list" , SYM_LIST},
189 {"append" , SYM_APPEND},
190 {"undefine" , SYM_UNDEFINE},
191 {"bufcreate" , SYM_BYTEARRAY_CREATE},
192 {"type-of" , SYM_TYPE_OF},
193 {"sym2str" , SYM_SYMBOL_TO_STRING},
194 {"str2sym" , SYM_STRING_TO_SYMBOL},
195 {"sym2u" , SYM_SYMBOL_TO_UINT},
196 {"u2sym" , SYM_UINT_TO_SYMBOL},
197 {"setcar" , SYM_SET_CAR},
198 {"setcdr" , SYM_SET_CDR},
199 {"setix" , SYM_SET_IX},
200 {"length" , SYM_LIST_LENGTH},
201 {"range" , SYM_RANGE},
202 {"member" , SYM_MEMBER},
203
204 {"assoc" , SYM_ASSOC}, // lookup an association
205 {"cossa" , SYM_COSSA}, // lookup an association "backwards"
206 {"acons" , SYM_ACONS}, // Add to alist
207 {"setassoc" , SYM_SET_ASSOC}, // Change association
208
209 {"shl" , SYM_SHL},
210 {"shr" , SYM_SHR},
211 {"bitwise-and" , SYM_BITWISE_AND},
212 {"bitwise-or" , SYM_BITWISE_OR},
213 {"bitwise-xor" , SYM_BITWISE_XOR},
214 {"bitwise-not" , SYM_BITWISE_NOT},
215
216 {"custom-destruct", SYM_CUSTOM_DESTRUCT},
217
218 {"to-i" , SYM_TO_I},
219 {"to-i32" , SYM_TO_I32},
220 {"to-u" , SYM_TO_U},
221 {"to-u32" , SYM_TO_U32},
222 {"to-float" , SYM_TO_FLOAT},
223 {"to-i64" , SYM_TO_I64},
224 {"to-u64" , SYM_TO_U64},
225 {"to-double" , SYM_TO_DOUBLE},
226 {"to-byte" , SYM_TO_BYTE},
227
228 {"event-register-handler", SYM_REG_EVENT_HANDLER},
229 {"take" , SYM_TAKE},
230 {"drop" , SYM_DROP},
231 {"mkarray" , SYM_MKARRAY},
232
233 {"dm-create" , SYM_DM_CREATE},
234 {"dm-alloc" , SYM_DM_ALLOC},
235
236 {"list?" , SYM_IS_LIST},
237 {"number?" , SYM_IS_NUMBER},
238 {"string?" , SYM_IS_STRING},
239 {"constant?" , SYM_IS_CONSTANT},
240
241 // fast access in list
242 {"ix" , SYM_IX},
243
244 {"identity" , SYM_IDENTITY},
245 {"array" , SYM_ARRAY},
246
247 // aliases
248 {"first" , SYM_CAR},
249 {"rest" , SYM_CDR},
250 {"fn" , SYM_LAMBDA},
251 {"def" , SYM_DEFINE},
252 {"true" , SYM_TRUE},
253 {"false" , SYM_NIL},
254 {"setvar" , SYM_SETVAR},
255 {"type-f32" , SYM_TYPE_FLOAT},
256 {"type-f64" , SYM_TYPE_DOUBLE},
257 {"array-create" , SYM_BYTEARRAY_CREATE},
258
259 };
260
261 static lbm_uint *symlist = NULL;
262 static lbm_uint next_symbol_id = RUNTIME_SYMBOLS_START;
263 static lbm_uint symbol_table_size_list = 0;
264 static lbm_uint symbol_table_size_list_flash = 0;
265 static lbm_uint symbol_table_size_strings = 0;
266 static lbm_uint symbol_table_size_strings_flash = 0;
267
268 // When rebooting an image...
269 18943 void lbm_symrepr_set_symlist(lbm_uint *ls) {
270 18943 symlist = ls;
271 18943 }
272
273
274 18943 lbm_uint lbm_symrepr_get_next_id(void) {
275 18943 return next_symbol_id;
276 }
277
278 18943 void lbm_symrepr_set_next_id(lbm_uint id) {
279 18943 next_symbol_id = id;
280 18943 }
281
282 67033 bool lbm_symrepr_init(void) {
283 67033 symlist = NULL;
284 67033 next_symbol_id = RUNTIME_SYMBOLS_START;
285 67033 symbol_table_size_list = 0;
286 67033 symbol_table_size_list_flash = 0;
287 67033 symbol_table_size_strings = 0;
288 67033 symbol_table_size_strings_flash = 0;
289 67033 return true;
290 }
291
292 ✗ void lbm_symrepr_name_iterator(symrepr_name_iterator_fun f) {
293
294 ✗ lbm_uint *curr = symlist;
295 ✗ while (curr) {
296 ✗ f((const char *)curr[NAME]);
297 ✗ curr = (lbm_uint *)curr[NEXT];
298 }
299 ✗ }
300
301 435133 const char *lookup_symrepr_name_memory(lbm_uint id) {
302 435133 const char *res = NULL;
303 435133 lbm_uint *curr = symlist;
304
2/2
✓ Branch 0 taken 17923107 times.
✓ Branch 1 taken 435133 times.
18358240 while (curr) {
305
2/2
✓ Branch 0 taken 435133 times.
✓ Branch 1 taken 17487974 times.
17923107 if (id == curr[ID]) {
306 435133 res = (const char *)curr[NAME];
307 }
308 17923107 curr = (lbm_uint*)curr[NEXT];
309 }
310 435133 return res;
311 }
312
313 // Lookup symbol name given a symbol id
314 1230720 const char *lbm_get_name_by_symbol(lbm_uint id) {
315 1230720 lbm_uint sym_kind = SYMBOL_KIND(id);
316 1230720 const char *res = NULL;
317
3/3
✓ Branch 0 taken 771920 times.
✓ Branch 1 taken 23667 times.
✓ Branch 2 taken 435133 times.
1230720 switch (sym_kind) {
318 771920 case SYMBOL_KIND_SPECIAL: /* fall through */
319 case SYMBOL_KIND_FUNDAMENTAL:
320 case SYMBOL_KIND_APPFUN:
321
2/2
✓ Branch 0 taken 15367000 times.
✓ Branch 1 taken 28 times.
15367028 for (unsigned int i = 0; i < NUM_SPECIAL_SYMBOLS; i ++) {
322
2/2
✓ Branch 0 taken 771892 times.
✓ Branch 1 taken 14595108 times.
15367000 if (id == special_symbols[i].id) {
323 771892 res = (special_symbols[i].name);
324 // With aliases there can be more than one hit here.
325 // exit after first hit.
326 771892 break;
327 }
328 }
329 771920 break;
330 23667 case SYMBOL_KIND_EXTENSION: {
331 23667 lbm_uint ext_id = id - EXTENSION_SYMBOLS_START;
332
1/2
✓ Branch 0 taken 23667 times.
✗ Branch 1 not taken.
23667 if (ext_id < lbm_get_max_extensions()) {
333 23667 res = extension_table[ext_id].name;
334 }
335 23667 } break;
336 435133 default:
337 435133 res = lookup_symrepr_name_memory(id);
338 }
339 1230720 return res;
340 }
341
342 ✗ lbm_uint *lbm_get_symbol_list_entry_by_name(const char *name) {
343 ✗ lbm_uint *curr = symlist;
344 ✗ while (curr) {
345 ✗ char *str = (char*)curr[NAME];
346 ✗ if (str_eq(name, str)) {
347 ✗ break;
348 }
349 ✗ curr = (lbm_uint*)curr[NEXT];
350 }
351 ✗ return curr;
352 }
353
354 // Lookup symbol id given symbol name
355 12824382 int lbm_get_symbol_by_name(const char *name, lbm_uint* id) {
356 12824382 int res = 0;
357 lbm_uint *curr;
358
359 // loop through special symbols
360
2/2
✓ Branch 0 taken 2242719295 times.
✓ Branch 1 taken 11391545 times.
2254110840 for (unsigned int i = 0; i < NUM_SPECIAL_SYMBOLS; i ++) {
361
2/2
✓ Branch 0 taken 1432837 times.
✓ Branch 1 taken 2241286458 times.
2242719295 if (str_eq(name, (char *)special_symbols[i].name)) {
362 1432837 *id = special_symbols[i].id;
363 1432837 res = 1; goto get_symbol_by_name_done;
364 }
365 }
366
367 // loop through extensions
368
2/2
✓ Branch 0 taken 4975970168 times.
✓ Branch 1 taken 11250575 times.
4987220743 for (unsigned int i = 0; i < lbm_get_max_extensions(); i ++) {
369
4/4
✓ Branch 0 taken 792268917 times.
✓ Branch 1 taken 4183701251 times.
✓ Branch 2 taken 140970 times.
✓ Branch 3 taken 792127947 times.
4975970168 if (extension_table[i].name && str_eq(name, extension_table[i].name)) {
370 140970 *id = EXTENSION_SYMBOLS_START + i;
371 140970 res = 1; goto get_symbol_by_name_done;
372 }
373 }
374
375 11250575 curr = symlist;
376
2/2
✓ Branch 0 taken 145662843 times.
✓ Branch 1 taken 9034074 times.
154696917 while (curr) {
377 145662843 char *str = (char*)curr[NAME];
378
2/2
✓ Branch 0 taken 2216501 times.
✓ Branch 1 taken 143446342 times.
145662843 if (str_eq(name, str)) {
379 2216501 *id = curr[ID];
380 2216501 res = 1; goto get_symbol_by_name_done;
381 }
382 143446342 curr = (lbm_uint*)curr[NEXT];
383 }
384 9034074 get_symbol_by_name_done:
385 12824382 return res;
386 }
387
388 extern lbm_flash_status lbm_write_const_array_padded(uint8_t *data, lbm_uint n, lbm_uint *res);
389
390 /* bool store_symbol_name_flash(const char *name, lbm_uint *res) { */
391 /* bool ret = false; */
392 /* size_t n = strlen(name) + 1; */
393 /* if (n > 1) { */
394
395 /* lbm_uint alloc_size; */
396 /* if (n % sizeof(lbm_uint) == 0) { */
397 /* alloc_size = n/(sizeof(lbm_uint)); */
398 /* } else { */
399 /* alloc_size = (n/(sizeof(lbm_uint))) + 1; */
400 /* } */
401
402 /* lbm_uint symbol_addr = 0; */
403 /* lbm_flash_status s = lbm_write_const_array_padded((uint8_t*)name, n, &symbol_addr); */
404 /* if (s == LBM_FLASH_WRITE_OK && symbol_addr) { */
405 /* symbol_table_size_strings_flash += alloc_size; */
406 /* *res = symbol_addr; */
407 /* ret = true; */
408 /* } */
409 /* } */
410 /* return ret; */
411 /* } */
412
413 // Symbol table
414 // non-const name copied into symbol-table-entry:
415 // Entry
416 // |
417 // [name-ptr | symbol-id | next-ptr | name n-bytes]
418 // | /
419 // ------------points here -----
420 //
421 // const name referenced by symbol-table-entry:
422 // Entry
423 // |
424 // [name-ptr | symbol-id | next-ptr]
425 // |
426 // [name n-bytes]
427 //
428
429 459994 int lbm_add_symbol_base(const char *name, lbm_uint *id) {
430 459994 int res = 0;
431 459994 size_t len = strlen(name);
432 459994 char *name_ptr = lbm_image_add_symbol_name(name, len);
433
1/2
✓ Branch 0 taken 459994 times.
✗ Branch 1 not taken.
459994 if (name_ptr) {
434 459994 lbm_uint *new_symlist = lbm_image_add_symbol(name_ptr, next_symbol_id, (lbm_uint)symlist);
435 459994 symbol_table_size_strings_flash += len; // approximation
436
1/2
✓ Branch 0 taken 459994 times.
✗ Branch 1 not taken.
459994 if (new_symlist) {
437 459994 symlist = new_symlist;
438 459994 *id = next_symbol_id ++;
439 459994 res = 1;
440 }
441 }
442 459994 return res;
443 }
444
445 327979 int lbm_add_symbol(const char *name, lbm_uint* id) {
446 327979 int res = 0;
447
2/2
✓ Branch 0 taken 192041 times.
✓ Branch 1 taken 135938 times.
327979 if (lbm_get_symbol_by_name(name, id)) {
448 192041 res = 1;
449 } else {
450 135938 res = lbm_add_symbol_base(name, id);
451 }
452 327979 return res;
453 }
454
455 // on Linux, win, etc a const string may not be at
456 // the same address between runs.
457 1350830 int lbm_add_symbol_const_base(const char *name, lbm_uint* id, bool link) {
458 1350830 lbm_uint symbol_name_storage = (lbm_uint)name;
459 lbm_uint *new_symlist;
460 1350830 int res = 0;
461
2/2
✓ Branch 0 taken 1350815 times.
✓ Branch 1 taken 15 times.
1350830 if (link) {
462 1350815 new_symlist = lbm_image_add_and_link_symbol((char*)symbol_name_storage, next_symbol_id, (lbm_uint)symlist, id);
463 } else {
464 15 new_symlist = lbm_image_add_symbol((char*)symbol_name_storage, next_symbol_id, (lbm_uint)symlist);
465 }
466
1/2
✓ Branch 0 taken 1350830 times.
✗ Branch 1 not taken.
1350830 if (new_symlist) {
467 1350830 symlist = new_symlist;
468 1350830 *id = next_symbol_id ++;
469 1350830 res = 1;
470 }
471 1350830 return res;
472 }
473
474 1564666 int lbm_add_symbol_const(const char *name, lbm_uint* id) {
475 1564666 int res = 0;
476
2/2
✓ Branch 0 taken 213851 times.
✓ Branch 1 taken 1350815 times.
1564666 if (lbm_get_symbol_by_name(name, id)) {
477 213851 res = 1;
478 } else {
479 1350815 res = lbm_add_symbol_const_base(name, id, true);
480 }
481 1564666 return res;
482 }
483
484 222 int lbm_str_to_symbol(const char *name, lbm_uint *sym_id) {
485 222 int res = lbm_get_symbol_by_name(name, sym_id);
486
2/2
✓ Branch 0 taken 116 times.
✓ Branch 1 taken 106 times.
222 if (!res)
487 116 res = lbm_add_symbol(name, sym_id);
488 222 return res;
489 }
490
491 84 lbm_uint lbm_get_symbol_table_size(void) {
492 84 return (symbol_table_size_list +symbol_table_size_strings);
493 }
494
495 84 lbm_uint lbm_get_symbol_table_size_flash(void) {
496 84 return (symbol_table_size_list_flash +
497 84 symbol_table_size_strings_flash) * sizeof(lbm_uint);
498 }
499
500 84 lbm_uint lbm_get_symbol_table_size_names(void) {
501 84 return symbol_table_size_strings; // Bytes already
502 }
503
504 84 lbm_uint lbm_get_symbol_table_size_names_flash(void) {
505 84 return symbol_table_size_strings_flash * sizeof(lbm_uint);
506 }
507
508 ✗ bool lbm_symbol_in_flash(char *str) {
509 ✗ return !lbm_memory_ptr_inside((lbm_uint*)str);
510 }
511
512 ✗ bool lbm_symbol_list_entry_in_flash(char *str) {
513 ✗ lbm_uint *entry = lbm_get_symbol_list_entry_by_name(str);
514 ✗ return (entry == NULL || !lbm_memory_ptr_inside(entry));
515 }
516