GCC Code Coverage Report


Directory: ../src/
File: /home/joels/Current/lispbm/src/symrepr.c
Date: 2026-09-05 08:38:23
Exec Total Coverage
Lines: 129 129 100.0%
Functions: 20 20 100.0%
Branches: 47 51 92.2%

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 20774 void lbm_symrepr_set_symlist(lbm_uint *ls) {
270 20774 symlist = ls;
271 20774 }
272
273
274 20777 lbm_uint lbm_symrepr_get_next_id(void) {
275 20777 return next_symbol_id;
276 }
277
278 20775 void lbm_symrepr_set_next_id(lbm_uint id) {
279 20775 next_symbol_id = id;
280 20775 }
281
282 67087 bool lbm_symrepr_init(void) {
283 67087 symlist = NULL;
284 67087 next_symbol_id = RUNTIME_SYMBOLS_START;
285 67087 symbol_table_size_list = 0;
286 67087 symbol_table_size_list_flash = 0;
287 67087 symbol_table_size_strings = 0;
288 67087 symbol_table_size_strings_flash = 0;
289 67087 return true;
290 }
291
292 1 void lbm_symrepr_name_iterator(symrepr_name_iterator_fun f) {
293
294 1 lbm_uint *curr = symlist;
295
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 1 times.
11 while (curr) {
296 10 f((const char *)curr[NAME]);
297 10 curr = (lbm_uint *)curr[NEXT];
298 }
299 1 }
300
301 435250 const char *lookup_symrepr_name_memory(lbm_uint id) {
302 435250 const char *res = NULL;
303 435250 lbm_uint *curr = symlist;
304
2/2
✓ Branch 0 taken 18116068 times.
✓ Branch 1 taken 435250 times.
18551318 while (curr) {
305
2/2
✓ Branch 0 taken 435249 times.
✓ Branch 1 taken 17680819 times.
18116068 if (id == curr[ID]) {
306 435249 res = (const char *)curr[NAME];
307 }
308 18116068 curr = (lbm_uint*)curr[NEXT];
309 }
310 435250 return res;
311 }
312
313 // Lookup symbol name given a symbol id
314 1231022 const char *lbm_get_name_by_symbol(lbm_uint id) {
315 1231022 lbm_uint sym_kind = SYMBOL_KIND(id);
316 1231022 const char *res = NULL;
317
3/3
✓ Branch 0 taken 772078 times.
✓ Branch 1 taken 23694 times.
✓ Branch 2 taken 435250 times.
1231022 switch (sym_kind) {
318 772078 case SYMBOL_KIND_SPECIAL: /* fall through */
319 case SYMBOL_KIND_FUNDAMENTAL:
320 case SYMBOL_KIND_APPFUN:
321
2/2
✓ Branch 0 taken 15369200 times.
✓ Branch 1 taken 28 times.
15369228 for (unsigned int i = 0; i < NUM_SPECIAL_SYMBOLS; i ++) {
322
2/2
✓ Branch 0 taken 772050 times.
✓ Branch 1 taken 14597150 times.
15369200 if (id == special_symbols[i].id) {
323 772050 res = (special_symbols[i].name);
324 // With aliases there can be more than one hit here.
325 // exit after first hit.
326 772050 break;
327 }
328 }
329 772078 break;
330 23694 case SYMBOL_KIND_EXTENSION: {
331 23694 lbm_uint ext_id = id - EXTENSION_SYMBOLS_START;
332
1/2
✓ Branch 0 taken 23694 times.
✗ Branch 1 not taken.
23694 if (ext_id < lbm_get_max_extensions()) {
333 23694 res = extension_table[ext_id].name;
334 }
335 23694 } break;
336 435250 default:
337 435250 res = lookup_symrepr_name_memory(id);
338 }
339 1231022 return res;
340 }
341
342 5 lbm_uint *lbm_get_symbol_list_entry_by_name(const char *name) {
343 5 lbm_uint *curr = symlist;
344
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 3 times.
32 while (curr) {
345 29 char *str = (char*)curr[NAME];
346
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 27 times.
29 if (str_eq(name, str)) {
347 2 break;
348 }
349 27 curr = (lbm_uint*)curr[NEXT];
350 }
351 5 return curr;
352 }
353
354 // Lookup symbol id given symbol name
355 12861691 int lbm_get_symbol_by_name(const char *name, lbm_uint* id) {
356 12861691 int res = 0;
357 lbm_uint *curr;
358
359 // loop through special symbols
360
2/2
✓ Branch 0 taken 2249580860 times.
✓ Branch 1 taken 11427513 times.
2261008373 for (unsigned int i = 0; i < NUM_SPECIAL_SYMBOLS; i ++) {
361
2/2
✓ Branch 0 taken 1434178 times.
✓ Branch 1 taken 2248146682 times.
2249580860 if (str_eq(name, (char *)special_symbols[i].name)) {
362 1434178 *id = special_symbols[i].id;
363 1434178 res = 1; goto get_symbol_by_name_done;
364 }
365 }
366
367 // loop through extensions
368
2/2
✓ Branch 0 taken 5117126328 times.
✓ Branch 1 taken 11285191 times.
5128411519 for (unsigned int i = 0; i < lbm_get_max_extensions(); i ++) {
369
4/4
✓ Branch 0 taken 809735610 times.
✓ Branch 1 taken 4307390718 times.
✓ Branch 2 taken 142322 times.
✓ Branch 3 taken 809593288 times.
5117126328 if (extension_table[i].name && str_eq(name, extension_table[i].name)) {
370 142322 *id = EXTENSION_SYMBOLS_START + i;
371 142322 res = 1; goto get_symbol_by_name_done;
372 }
373 }
374
375 11285191 curr = symlist;
376
2/2
✓ Branch 0 taken 148346284 times.
✓ Branch 1 taken 9063572 times.
157409856 while (curr) {
377 148346284 char *str = (char*)curr[NAME];
378
2/2
✓ Branch 0 taken 2221619 times.
✓ Branch 1 taken 146124665 times.
148346284 if (str_eq(name, str)) {
379 2221619 *id = curr[ID];
380 2221619 res = 1; goto get_symbol_by_name_done;
381 }
382 146124665 curr = (lbm_uint*)curr[NEXT];
383 }
384 9063572 get_symbol_by_name_done:
385 12861691 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 460992 int lbm_add_symbol_base(const char *name, lbm_uint *id) {
430 460992 int res = 0;
431 460992 size_t len = strlen(name);
432 460992 char *name_ptr = lbm_image_add_symbol_name(name, len);
433
2/2
✓ Branch 0 taken 460991 times.
✓ Branch 1 taken 1 times.
460992 if (name_ptr) {
434 460991 lbm_uint *new_symlist = lbm_image_add_symbol(name_ptr, next_symbol_id, (lbm_uint)symlist);
435 460991 symbol_table_size_strings_flash += len; // approximation
436
1/2
✓ Branch 0 taken 460991 times.
✗ Branch 1 not taken.
460991 if (new_symlist) {
437 460991 symlist = new_symlist;
438 460991 *id = next_symbol_id ++;
439 460991 res = 1;
440 }
441 }
442 460992 return res;
443 }
444
445 328458 int lbm_add_symbol(const char *name, lbm_uint* id) {
446 328458 int res = 0;
447
2/2
✓ Branch 0 taken 192192 times.
✓ Branch 1 taken 136266 times.
328458 if (lbm_get_symbol_by_name(name, id)) {
448 192192 res = 1;
449 } else {
450 136266 res = lbm_add_symbol_base(name, id);
451 }
452 328458 return res;
453 }
454
455 // on Linux, win, etc a const string may not be at
456 // the same address between runs.
457 1355812 int lbm_add_symbol_const_base(const char *name, lbm_uint* id, bool link) {
458 1355812 lbm_uint symbol_name_storage = (lbm_uint)name;
459 lbm_uint *new_symlist;
460 1355812 int res = 0;
461
2/2
✓ Branch 0 taken 1355796 times.
✓ Branch 1 taken 16 times.
1355812 if (link) {
462 1355796 new_symlist = lbm_image_add_and_link_symbol((char*)symbol_name_storage, next_symbol_id, (lbm_uint)symlist, id);
463 } else {
464 16 new_symlist = lbm_image_add_symbol((char*)symbol_name_storage, next_symbol_id, (lbm_uint)symlist);
465 }
466
1/2
✓ Branch 0 taken 1355812 times.
✗ Branch 1 not taken.
1355812 if (new_symlist) {
467 1355812 symlist = new_symlist;
468 1355812 *id = next_symbol_id ++;
469 1355812 res = 1;
470 }
471 1355812 return res;
472 }
473
474 1572977 int lbm_add_symbol_const(const char *name, lbm_uint* id) {
475 1572977 int res = 0;
476
2/2
✓ Branch 0 taken 217182 times.
✓ Branch 1 taken 1355795 times.
1572977 if (lbm_get_symbol_by_name(name, id)) {
477 217182 res = 1;
478 } else {
479 1355795 res = lbm_add_symbol_const_base(name, id, true);
480 }
481 1572977 return res;
482 }
483
484 225 int lbm_str_to_symbol(const char *name, lbm_uint *sym_id) {
485 225 int res = lbm_get_symbol_by_name(name, sym_id);
486
2/2
✓ Branch 0 taken 117 times.
✓ Branch 1 taken 108 times.
225 if (!res)
487 117 res = lbm_add_symbol(name, sym_id);
488 225 return res;
489 }
490
491 86 lbm_uint lbm_get_symbol_table_size(void) {
492 86 return (symbol_table_size_list +symbol_table_size_strings);
493 }
494
495 86 lbm_uint lbm_get_symbol_table_size_flash(void) {
496 86 return (symbol_table_size_list_flash +
497 86 symbol_table_size_strings_flash) * sizeof(lbm_uint);
498 }
499
500 86 lbm_uint lbm_get_symbol_table_size_names(void) {
501 86 return symbol_table_size_strings; // Bytes already
502 }
503
504 86 lbm_uint lbm_get_symbol_table_size_names_flash(void) {
505 86 return symbol_table_size_strings_flash * sizeof(lbm_uint);
506 }
507
508 1 bool lbm_symbol_in_flash(char *str) {
509 1 return !lbm_memory_ptr_inside((lbm_uint*)str);
510 }
511
512 2 bool lbm_symbol_list_entry_in_flash(char *str) {
513 2 lbm_uint *entry = lbm_get_symbol_list_entry_by_name(str);
514
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
2 return (entry == NULL || !lbm_memory_ptr_inside(entry));
515 }
516