GCC Code Coverage Report


Directory: ../src/
File: /home/joels/Current/lispbm/src/lbm_image.c
Date: 2026-07-11 23:13:58
Exec Total Coverage
Lines: 549 617 89.0%
Functions: 49 51 96.1%
Branches: 278 489 56.9%

Line Branch Exec Source
1 /*
2 Copyright 2025, 2026 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 <extensions.h>
19 #include <lbm_image.h>
20 #include <heap.h>
21 #include <env.h>
22 #include <lbm_flat_value.h>
23 #include <eval_cps.h>
24 #include <extensions.h>
25
26 #ifndef DEBUG
27 #define DEBUG 0
28 #endif
29
30 // Assumptions about the image memory:
31 // * It is part of the address space.
32 // * Image is always available at the same address (across reboots)
33 // * It is a write-once memory.
34 // * Can be cleared in its entirety.
35 // * Can check on a byte-level is "is-writable" (has a known initial state when cleared)
36
37 // Details
38 // * @const_start @const_end is tricky.
39 // * Constant heap is needed because of small amount of RAM.
40 // * Arbitrary pointers will be tricky.
41
42 // Want to be able to build an image incrementally.
43 // Can @const_start @const_end be used in conjuction with a
44 // more exlicit image manipulation subsystem.
45
46 //TBD:
47 // * does an image contain running threads ? (I would prefer if no)
48 // instead there is a startup-entry that represents some code that will
49 // be executed after setting up an image.
50 // This startup-entry can spawn threads and initialize resources.
51 // * Will we have a heap-image or will all bindings move into the const heap.
52
53 // FEB 26:
54 // -- There will be no "heap-image" or "memory-image"
55 // Just flattened and stored bindings from the environment (which is as good but likely smaller).
56 // -- There will be an image of the const-heap. in fact the const heap lives in the image always.
57 // A bit problematic with building on image incrementally as it is in flash and the contents cannot be changed.
58 // Contents can be added though! (keep track of const-heap write-ptr is an issue)
59 // -- Maybe we should implement image for a read-write memory and then make the flash situation a special case?
60 // -- Maybe size fields should always be in bytes.
61 // -- TODO: a flatten function that flattens a value directly to flash and also does not flatten things that are
62 // already in flash, but rather then just refers to them.
63
64 // FEB 27:
65 // -- Symbol numbering will be an issue.
66 // * Store the symboltable in the image and restore it on boot.
67 // * Names already in flash can be refered to.
68 // * Names in ram can be copied.
69 // * Entire subtable may already be in flash - leave in place and refer to it.
70 // -- loading an image and then adding to it can be tricky to make possible.
71 // * const-heap write pointer needs to be stored. (but if it is stored then it cannot be changed)
72 // Could allow multiple "const-heap-write-pointer" fields in the image and use the one that appears last...
73 // * Symboltable could be created incrementally in a similar way. Append later symbol_table data fields
74 // to the previously loaded.
75
76 // FEB 28:
77 // -- Symbol numbering problem. The structure of the symboltable may
78 // need to change. It is currently impossible to append a symbol list stored
79 // in flash to the global symbol table. A richer structure is needed.
80 // -- The symbols in the SYMTAB can all be in flash (all in the image) and
81 // all name strings can be written to flash as well.
82 // * May be easiest if names go to the flash heap (as it is now)
83 // and table entries are a tagged field in the image (1 extra byte per symbol...)
84 // * Means the image must be initialized (to a degree) before symbols are created.
85
86 // MARCH 1:
87 // -- Symbols are added to and restored from the image.
88 // -- lbm_add_symbol_const, still creates a lbm_memory list structure.
89 // Const symbols should also be stored into the image and add_symbol_const
90 // should check and reuse stored symbol id.
91 // Check order of initialization to see how easy this is to fix.
92
93 // Offline image tools
94 // - Image compaction: remove overwrite fields and compact the image.
95 // - Change of base address: relabel all memory accesses.
96 // - ...
97
98
99 // MARCH 5
100 // -- Can constants (anything on const heap) contain references into non-constant heap?
101 // - I think not, but should verify this.
102 // - eval_cps move_to_flash performs a deep copy into flash.
103
104 // Endianess woes...
105 // - little endian least-significant byte at least address
106 // - big endian most-significant byte at least address
107 // - all platforms we target currently are little-endian
108 //
109 // 0x11223344
110 // | | | '--- [44] addr
111 // | | '----- [33] addr + 1
112 // | '------- [22] addr + 2
113 // '--------- [11] addr + 3
114 //
115 // Images are going to be mainly little endian. (what endianess does flatvalues use? I think BE)
116
117 // constant heap should be 4byte aligned so that there are 2 unused low end bits
118 // in all cell-pointers into constant heap.
119
120 // March 8
121 // -- flattening lead to duplication of shared nodes.
122 // if a = '(1 2 3) and b = (cons 4 a) and c = (cons 5 a)
123 // then the result of flattening a b c each contains a full copy of a.
124 // -- flattening a value that in turn points to a constant value, duplicates
125 // the constant value.
126
127 // TODO: Put more info into the IMAGE_INITIALIZED FIELD
128 // - 32/64 bit etc
129
130 // Sharing recovery:
131 //
132 // Construct a mapping of cons-cell address to key and flat value offset
133 // | Address | KEY | Offset |
134 // | addr0 | k0 | offs0 |
135 //
136 // Flat value needs a new reference values. Potentially one new for each kind of cons-cell (cons, byte-array etc)
137 // so that correct pointer can be created.
138 // [ REF_X | addr0 ]
139 //
140
141 // unflatten will create the column "new address"
142 // When unflattening k0 at offset offs0, fill in the new address field with the cons-cell address created.
143 // | Address | KEY | Offset | new address |
144 // | addr0 | k1 | offs0 | newaddr0 |
145
146 // Unflattening needs to check:
147 // - for each cell type thing, if it is at an offset that is shared.
148 // If it is, we need to fill in the "new address" field.
149 // - A shared node will only be created once! but there may be many REF_X pointing to it.
150 // - When unflattening a REF_X field, search through the mapping for a match on the "address" field.
151 // - Ordering is needed so that unflattening of a ref to X happens only after X has been assigned a
152 // a new address.
153 // Cost: search through the mapping for each unflattened cons-cell (including array etc).
154 // - Not all keys will exist in the mapping. Can check once and then not perform per lookup check.
155
156 // Flattening:
157 // - phase 0: find sharing and create collect the set of shared addresses into a table:
158 // | addr | k | empty |
159 // - phase 1: flatten, while flattening a cell see if it is at an address that exists in the table.
160 // if the address is in the table,
161 // see if offset = empty => set offset to current pos in flat value and flatten value as usual.
162 // see if offset = offs => create a REFX: to addr
163
164 // The key field is needed so that one knows when unflattening that a node is shared and the
165 // new address should be added to the table at key , offset.
166 // Possibly flattening a shared node could be a special field in the flat value.
167 // the flat value could hold [ Shared node | orig_address | flat_val ].
168 // Then no key field is needed in the sharing mapping table.
169
170 #ifdef LBM64
171 #define IMAGE_INITIALIZED (uint32_t)0xBEEF4001 // [ 0xBEEF4001 ]
172 #else
173 #define IMAGE_INITIALIZED (uint32_t)0xBEEF2001 // [ 0xBEEF2001 ]
174 #endif
175 // Address downwards ->
176 #define CONSTANT_HEAP_IX (uint32_t)0x02 // [ 0x02 | uint32]
177 #define BINDING_CONST (uint32_t)0x03 // [ 0x03 | key | lbm_uint ]
178 #define BINDING_FLAT (uint32_t)0x04 // [ 0x04 | size | key | flatval ]
179 #define SYMBOL_ENTRY (uint32_t)0x06 // [ 0x06 | NEXT_PTR | ID | NAME PTR ] // symbol_entry with highest address is root.
180 #define SYMBOL_LINK_ENTRY (uint32_t)0x07 // [ 0x07 | C_LINK_PTR | NEXT_PTR | ID | NAME PTR ]
181 #define EXTENSION_TABLE (uint32_t)0x08 // [ 0x08 | NUM | EXT ...]
182 #define VERSION_ENTRY (uint32_t)0x09 // [ 0x09 | size | string ]
183 #define SHARING_TABLE (uint32_t)0x10 // [ 0x10 | n | n-entries]
184 #define SYMBOL_NAME_ENTRY (uint32_t)0x11 // [ 0x11 | size | string ]
185 // Size is in number of 32bit words, even on 64 bit images.
186
187 // To be able to work on an image incrementally (even though it is not recommended)
188 // many fields are allowed to be duplicated and the later ones have priority
189 // over earlier ones.
190
191
192 #define DOWNWARDS false
193 #define UPWARDS true
194
195 static lbm_image_write_fun image_write = NULL;
196
197 static uint32_t *image_address = NULL;
198 static int32_t write_index = 0;
199 static uint32_t image_size = 0;
200 static bool image_has_extensions = false;
201 static char* image_version = NULL;
202
203 75 uint32_t *lbm_image_get_image(void) {
204 75 return image_address;
205 }
206
207 75 uint32_t lbm_image_get_size(void) {
208 75 return image_size;
209 }
210
211 8338 int32_t lbm_image_get_write_index(void) {
212 8338 return write_index;
213 }
214
215 753 bool lbm_image_has_extensions(void) {
216 753 return image_has_extensions;
217 }
218
219 411298 static uint32_t read_u32(int32_t index) {
220 411298 return *((uint32_t*)(image_address + index));
221 }
222
223 #ifdef LBM64
224 ✗ static uint64_t read_u64(int32_t index) {
225 // image_addres is an u32 ptr. so addr + i is a step of i * 4 bytes
226 ✗ return *((uint64_t*)(image_address + index));
227 }
228 #endif
229
230 // Write_u32 is only for writing within the bootable
231 // area of the image. it is sometimes convenient to be able to
232 // write upwards or downwards in this area selectively.
233 // Direction influences if the index incs or decs.
234 12797165 static bool write_u32(uint32_t w, int32_t *i, bool direction) {
235 12797165 bool r = image_write(w, *i, false);
236
2/2
✓ Branch 0 taken 837833 times.
✓ Branch 1 taken 11959332 times.
12797165 (*i) += direction ? 1 : -1;
237 12797165 return r;
238 }
239
240 #ifdef LBM64
241 2212390 static bool write_u64(uint64_t dw, int32_t *i, bool direction) {
242 2212390 uint32_t *words = (uint32_t*)&dw;
243
244 // downwards ... hw lw
245 // ix ix-1
246 // upwards hw lw ...
247 // ix+1 ix
248
249 // direction: DOWNWARDS (false) writes hw then lw; UPWARDS (true) writes lw then hw.
250
251 2212390 bool r = true;
252
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2212390 times.
2212390 if (direction) {
253 ✗ r = r && write_u32(words[0], i, direction);
254 ✗ r = r && write_u32(words[1], i, direction);
255 } else {
256
2/4
✓ Branch 0 taken 2212390 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2212390 times.
✗ Branch 3 not taken.
2212390 r = r && write_u32(words[1], i, direction);
257
2/4
✓ Branch 0 taken 2212390 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2212390 times.
✗ Branch 3 not taken.
2212390 r = r && write_u32(words[0], i, direction);
258 }
259 2212390 return r;
260 }
261 #endif
262
263 // fv_write function write values as big endian.
264
265 uint32_t fv_buf_ix = 0;
266 uint8_t fv_buf[4] = {0};
267 51740 static bool fv_write_u8(uint8_t b) {
268 51740 bool r = true;
269
2/2
✓ Branch 0 taken 12783 times.
✓ Branch 1 taken 38957 times.
51740 if (fv_buf_ix >= 4) {
270 12783 r = write_u32(((uint32_t*)fv_buf)[0], &write_index, UPWARDS);
271 12783 memset(fv_buf,0,4);
272 12783 fv_buf_ix = 0;
273 }
274 51740 fv_buf[fv_buf_ix] = b;
275 51740 fv_buf_ix++;
276 51740 return r;
277 }
278
279 321 static bool fv_write_flush(void) {
280
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 321 times.
321 if (fv_buf_ix == 0) return true;
281 else {
282 321 bool r = write_u32(((uint32_t*)fv_buf)[0], &write_index, UPWARDS);;
283 321 fv_buf_ix = 0;
284 321 memset(fv_buf,0,4);
285 321 return r;
286 }
287 }
288
289 8442 static bool fv_write_u32(uint32_t w) {
290 8442 uint8_t * bytes = (uint8_t*)&w;
291 return
292
1/2
✓ Branch 0 taken 8442 times.
✗ Branch 1 not taken.
16884 fv_write_u8(bytes[3]) &&
293
1/2
✓ Branch 0 taken 8442 times.
✗ Branch 1 not taken.
16884 fv_write_u8(bytes[2]) &&
294
2/4
✓ Branch 0 taken 8442 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8442 times.
✗ Branch 3 not taken.
25326 fv_write_u8(bytes[1]) &&
295 8442 fv_write_u8(bytes[0]);
296 }
297
298 9 static bool fv_write_u64(uint64_t dw) {
299 9 uint8_t * bytes = (uint8_t*)&dw;
300 return
301
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
18 fv_write_u8(bytes[7]) &&
302
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
18 fv_write_u8(bytes[6]) &&
303
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
18 fv_write_u8(bytes[5]) &&
304
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
18 fv_write_u8(bytes[4]) &&
305
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
18 fv_write_u8(bytes[3]) &&
306
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
18 fv_write_u8(bytes[2]) &&
307
2/4
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
27 fv_write_u8(bytes[1]) &&
308 9 fv_write_u8(bytes[0]);
309 }
310
311
312 6783287 static bool write_lbm_uint(lbm_uint ptr_val, int32_t *i, bool direction) {
313 #ifdef LBM64
314 2212390 return write_u64(ptr_val, i, direction);
315 #else
316 4570897 return write_u32(ptr_val, i, direction);
317 #endif
318 }
319
320 541 static bool write_lbm_value(lbm_value v, int32_t *i, bool direction) {
321 #ifdef LBM64
322 ✗ return write_u64(v, i, direction);
323 #else
324 541 return write_u32(v, i, direction);
325 #endif
326 }
327
328 // ////////////////////////////////////////////////////////////
329 // Flatten a value into image
330
331 // TODO: Consants things that are stored in the image
332 // does not need to be flattened. Could refer to these by
333 // reference. Some new kinds of flat values needs to be added
334 // for this referencing to work.
335
336 // TODO: Symbols in a flat_value in an image can be stored as
337 // its numerical representation rather than its string rep.
338
339 7946 static bool i_f_cons(void ) {
340 7946 return fv_write_u8(S_CONS);
341 }
342
343 20 static bool i_f_lisp_array(uint32_t size) {
344 // arrays are smaller than 2^32 elements long
345 20 bool r = fv_write_u8(S_LBM_LISP_ARRAY);
346
2/4
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
20 r = r && fv_write_u32(size);
347 20 return r;
348 }
349
350 4910 static bool i_f_sym(lbm_value sym) {
351 4910 lbm_uint sym_id = lbm_dec_sym(sym);
352 4910 bool r = fv_write_u8(S_SYM_VALUE);
353 #ifndef LBM64
354
2/4
✓ Branch 0 taken 4910 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4910 times.
✗ Branch 3 not taken.
4910 r = r && fv_write_u32(sym_id);
355 #else
356 ✗ r = r && fv_write_u64(sym_id);
357 #endif
358 4910 return r;
359 }
360
361 3171 static bool i_f_i(lbm_int i) {
362 3171 bool res = true;
363 #ifndef LBM64
364
2/4
✓ Branch 0 taken 3171 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3171 times.
✗ Branch 3 not taken.
3171 res = res && fv_write_u8(S_I28_VALUE);
365
2/4
✓ Branch 0 taken 3171 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3171 times.
✗ Branch 3 not taken.
3171 res = res && fv_write_u32((uint32_t)i);
366 #else
367 ✗ res = res && fv_write_u8(S_I56_VALUE);
368 ✗ res = res && fv_write_u64((uint64_t)i);
369 #endif
370 3171 return res;
371 }
372
373 4 static bool i_f_u(lbm_uint u) {
374 4 bool res = true;
375 #ifndef LBM64
376
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 res = res && fv_write_u8(S_U28_VALUE);
377
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 res = res && fv_write_u32((uint32_t)u);
378 #else
379 ✗ res = res && fv_write_u8(S_U56_VALUE);
380 ✗ res = res && fv_write_u64((uint64_t)u);
381 #endif
382 4 return res;
383 }
384
385 22 static bool i_f_b(uint8_t b) {
386 22 bool res = true;
387
2/4
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
22 res = res && fv_write_u8(S_BYTE_VALUE);
388
2/4
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
22 res = res && fv_write_u8(b);
389 22 return res;
390 }
391
392 5 static bool i_f_i32(int32_t w) {
393 5 bool res = true;
394
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 res = res && fv_write_u8(S_I32_VALUE);
395
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 res = res && fv_write_u32((uint32_t)w);
396 5 return res;
397 }
398
399 2 static bool i_f_u32(uint32_t w) {
400 2 bool res = true;
401
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 res = res && fv_write_u8(S_U32_VALUE);
402
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 res = res && fv_write_u32(w);
403 2 return res;
404 }
405
406 39 static bool i_f_float(float f) {
407 39 bool res = true;
408
2/4
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 39 times.
✗ Branch 3 not taken.
39 res = res && fv_write_u8(S_FLOAT_VALUE);
409 uint32_t u;
410 39 memcpy(&u, &f, sizeof(uint32_t));
411
2/4
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 39 times.
✗ Branch 3 not taken.
39 res = res && fv_write_u32((uint32_t)u);
412 39 return res;
413 }
414
415 2 static bool i_f_double(double d) {
416 2 bool res = true;
417
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 res = res && fv_write_u8(S_DOUBLE_VALUE);
418 uint64_t u;
419 2 memcpy(&u, &d, sizeof(uint64_t));
420
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 res = res && fv_write_u64(u);
421 2 return res;
422 }
423
424 5 static bool i_f_i64(int64_t w) {
425 5 bool res = true;
426
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 res = res && fv_write_u8(S_I64_VALUE);
427
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 res = res && fv_write_u64((uint64_t)w);
428 5 return res;
429 }
430
431 2 static bool i_f_u64(uint64_t w) {
432 2 bool res = true;
433
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 res = res && fv_write_u8(S_U64_VALUE);
434
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 res = res && fv_write_u64(w);
435 2 return res;
436 }
437
438 // num_bytes is specifically an uint32_t
439 179 static bool i_f_lbm_array(uint32_t num_bytes, uint8_t *data) {
440 179 bool res = fv_write_u8(S_LBM_ARRAY);
441
2/4
✓ Branch 0 taken 179 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 179 times.
✗ Branch 3 not taken.
179 res = res && fv_write_u32(num_bytes);
442
1/2
✓ Branch 0 taken 179 times.
✗ Branch 1 not taken.
179 if (res) {
443
2/2
✓ Branch 0 taken 1459 times.
✓ Branch 1 taken 179 times.
1638 for (uint32_t i = 0; i < num_bytes; i ++ ) {
444
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1459 times.
1459 if (!fv_write_u8(data[i])) return false;
445 }
446 }
447 179 return res;
448 }
449
450
451
452 // ////////////////////////////////////////////////////////////
453 //
454
455 998 char *lbm_image_get_version(void) {
456
2/2
✓ Branch 0 taken 245 times.
✓ Branch 1 taken 753 times.
998 if (image_version) {
457 245 return image_version;
458 } else {
459 753 int32_t pos = (int32_t)image_size-2; // fixed position version string.
460 753 uint32_t val = read_u32(pos); pos --;
461
1/2
✓ Branch 0 taken 753 times.
✗ Branch 1 not taken.
753 if (val == VERSION_ENTRY) {
462 753 int32_t size = (int32_t)read_u32(pos);
463 753 image_version = (char*)(image_address + (pos - size));
464 753 return image_version;
465 }
466 }
467 ✗ return NULL;
468 }
469
470 // ////////////////////////////////////////////////////////////
471 // Constant heaps as part of an image.
472
473 lbm_const_heap_t image_const_heap;
474 lbm_uint image_const_heap_start_ix = 0;
475
476 // If an image is "present"
477 // but the const_heap_index does not point to a free location
478 // in flash, something is wrong!
479 ✗ lbm_uint lbm_image_const_heap_index(void) {
480 ✗ return image_const_heap.next;
481 }
482
483 16779 static bool image_const_heap_write(lbm_uint w, lbm_uint ix) {
484 #ifdef LBM64
485 4872 int32_t i = (int32_t)(image_const_heap_start_ix + (ix * 2));
486 4872 uint32_t *words = (uint32_t*)&w;
487 4872 bool r = image_write(words[0], i, true);
488
2/4
✓ Branch 0 taken 4872 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4872 times.
✗ Branch 3 not taken.
4872 r = r && image_write(words[1], i + 1, true);
489 4872 return r;
490 #else
491 11907 int32_t i = (int32_t)(image_const_heap_start_ix + ix);
492 11907 return image_write(w,i,true);
493 #endif
494 }
495
496 // ////////////////////////////////////////////////////////////
497 // Image manipulation
498
499 460009 lbm_uint *lbm_image_add_symbol(char *name, lbm_uint id, lbm_uint symlist) {
500 // 64 bit | 32 bit
501 // image[i] = SYMBOL_ENTRY | image[i] = SYMBOL_ENTRY
502 // image[i-1] = symlist_ptr_high_word | image[i-1] = symlist_ptr
503 // image[i-2] = symlist_ptr_low_word | image[i-2] = id
504 // image[i-3] = id_high_word | image[i-3] = name_ptr
505 // image[i-4] = id_low_word
506 // image[i-5] = name_ptr_high_word
507 // image[i-6] = name_ptr_low_word
508 460009 bool r = write_u32(SYMBOL_ENTRY, &write_index,DOWNWARDS);
509
2/4
✓ Branch 0 taken 460009 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 460009 times.
✗ Branch 3 not taken.
460009 r = r && write_lbm_uint(symlist, &write_index, DOWNWARDS);
510
2/4
✓ Branch 0 taken 460009 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 460009 times.
✗ Branch 3 not taken.
460009 r = r && write_lbm_uint(id, &write_index, DOWNWARDS);
511
2/4
✓ Branch 0 taken 460009 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 460009 times.
✗ Branch 3 not taken.
460009 r = r && write_lbm_uint((lbm_uint)name, &write_index, DOWNWARDS);
512 460009 lbm_uint entry_ptr = (lbm_uint)(image_address + write_index + 1);
513
1/2
✓ Branch 0 taken 460009 times.
✗ Branch 1 not taken.
460009 if (r)
514 460009 return (lbm_uint*)entry_ptr;
515 ✗ return NULL;
516 }
517
518 // Writes size in words followed by the string
519 // todo sensible error detection.
520 526774 static bool write_string_upwards(const char *str, size_t len) {
521 526774 uint32_t bytes = (uint32_t)len + 1;
522
2/2
✓ Branch 0 taken 69247 times.
✓ Branch 1 taken 457527 times.
526774 uint32_t words = (bytes % 4 == 0) ? bytes / 4 : (bytes / 4) + 1;
523 526774 write_u32(words, &write_index, DOWNWARDS);
524 526774 uint32_t w = 0;
525 526774 char *buf = (char*)&w;
526 526774 uint32_t i = 0;
527 526774 int32_t ix = write_index - (int32_t)(words -1);
528 526774 int wi = 0;
529
2/2
✓ Branch 0 taken 2474088 times.
✓ Branch 1 taken 526774 times.
3000862 while (i < bytes) {
530
2/2
✓ Branch 0 taken 526774 times.
✓ Branch 1 taken 1947314 times.
2474088 if (wi == 0 ) {
531 526774 w = 0;
532 }
533
2/2
✓ Branch 0 taken 297955 times.
✓ Branch 1 taken 2176133 times.
2474088 if (wi == 4) wi = 0;
534 2474088 buf[wi] = str[i];
535
2/2
✓ Branch 0 taken 367202 times.
✓ Branch 1 taken 2106886 times.
2474088 if (wi == 3) {
536
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 367202 times.
367202 if (!write_u32(w, &ix, UPWARDS)) return false;
537 //write_u32(w, &ix, UPWARDS);
538 367202 w = 0;
539 }
540 2474088 i ++;
541 2474088 wi ++;
542 }
543
3/4
✓ Branch 0 taken 526774 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 457527 times.
✓ Branch 3 taken 69247 times.
526774 if (wi != 0 && wi != 4) {
544
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 457527 times.
457527 if (!write_u32(w, &ix, UPWARDS)) return false;
545 //write_u32(w, &ix, UPWARDS);
546 }
547 526774 write_index -= (int32_t)words;
548 526774 return true;
549 }
550
551 // return the address of the string
552 459995 char *lbm_image_add_symbol_name(const char *name, size_t len) {
553
1/2
✓ Branch 0 taken 459995 times.
✗ Branch 1 not taken.
459995 if (len > 0) {
554 459995 uint32_t bytes = (uint32_t)len + 1;
555
2/2
✓ Branch 0 taken 69247 times.
✓ Branch 1 taken 390748 times.
459995 uint32_t words = (bytes % 4 == 0) ? bytes / 4 : (bytes / 4) + 1;
556 // 1 word SYMBOL_NAME_ENTRY tag + 1 word size + the string data itself.
557
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 459995 times.
459995 if ((write_index - (int32_t)(words + 2)) <= (int32_t)image_const_heap.next) {
558 ✗ return NULL;
559 }
560 459995 bool r = write_u32(SYMBOL_NAME_ENTRY, &write_index, DOWNWARDS);
561
2/4
✓ Branch 0 taken 459995 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 459995 times.
✗ Branch 3 not taken.
459995 r = r && write_string_upwards(name, len);
562
1/2
✓ Branch 0 taken 459995 times.
✗ Branch 1 not taken.
459995 if (r)
563 459995 return (char*)(image_address + write_index + 1);
564 }
565 ✗ return NULL;
566 }
567
568 // The symbol id is written to the link address upon image-boot
569 1350815 lbm_uint *lbm_image_add_and_link_symbol(char *name, lbm_uint id, lbm_uint symlist, lbm_uint *link) {
570 // 64 bit | 32 bit
571 // image[i] = SYMBOL_ENTRY | image[i] = SYMBOL_ENTRY
572 // image[i-1] = link_ptr_high | image[i-1] link_ptr
573 // image[i-2] = link_ptr_low | image[i-2] = symlist_ptr
574 // image[i-3] = symlist_ptr_high_word | image[i-3] = id
575 // image[i-4] = symlist_ptr_low_word | image[i-4] = name_ptr
576 // image[i-5] = id_high_word
577 // image[i-6] = id_low_word
578 // image[i-7] = name_ptr_high_word
579 // image[i-8] = name_ptr_low_word
580 1350815 bool r = write_u32(SYMBOL_LINK_ENTRY, &write_index,DOWNWARDS);
581
2/4
✓ Branch 0 taken 1350815 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1350815 times.
✗ Branch 3 not taken.
1350815 r = r && write_lbm_uint((lbm_uint)link, &write_index, DOWNWARDS);
582
2/4
✓ Branch 0 taken 1350815 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1350815 times.
✗ Branch 3 not taken.
1350815 r = r && write_lbm_uint(symlist, &write_index, DOWNWARDS);
583
2/4
✓ Branch 0 taken 1350815 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1350815 times.
✗ Branch 3 not taken.
1350815 r = r && write_lbm_uint(id, &write_index, DOWNWARDS);
584
2/4
✓ Branch 0 taken 1350815 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1350815 times.
✗ Branch 3 not taken.
1350815 r = r && write_lbm_uint((lbm_uint)name, &write_index, DOWNWARDS);
585 1350815 lbm_uint entry_ptr = (lbm_uint)(image_address + write_index + 1);
586
1/2
✓ Branch 0 taken 1350815 times.
✗ Branch 1 not taken.
1350815 if (r)
587 1350815 return (lbm_uint*)entry_ptr;
588 ✗ return NULL;
589 }
590
591 // ////////////////////////////////////////////////////////////
592 // Construction site
593
594 // Sharing is detected and annotated by:
595 // 1. Generate an array of addresses of shared structures. (sharing table)
596 // Sharing table will contain a boolean field where "having been flattened"
597 // status is tracked.
598 // 2. Flatten values and for each ptr-cell, check if it's address is in the
599 // sharing table. If the cell is in the sharing table and the boolean
600 // flag is not set: Set the flag and flatten the value with a shared tag
601 // is set: Do not flatten value, create a REF tag.
602
603 // Note: the boolean field may not be needed, it may be possible to recreate that
604 // information on the fly during flattening using the GC bit.
605 // But since all complete traversals require a GC this change just moves the
606 // bookkeeping cost forward (first to the size phase).
607
608 // The sharing table could be a temporary list on the LBM heap
609 // if only it wasn't for GC. GC cannot be run while doing pointer
610 // reversal traversals.
611 // Another option would be to allocate an area in LBM mem and fill that
612 // that with temporary sharing data. Only problem is that we do not know how much
613 // temporary data is needed until after doing at least one traversal where we
614 // also need to accumulate shared addresses in order to not count them doubly.
615 // ** allocating lbm_memory_longest_free amount of temp data at flattening
616 // could be a good solution. But even then the final number of shared
617 // nodes could be written to the image so that the unflattener does not need
618 // guess and allocate in chunks.
619 // - Very little lbm mem could be available here.
620
621 // Sharing is restored by:
622 // 1. Allocate a new column for the sharing table in LBM mem, the size is now known.
623 // 2. Unflatten flat values:
624 // if a value has a shared tag, fill in the address it is unflattened to into the
625 // new sharing table column.
626 // if a value has the ref tag, look it up in sharing table and read out the new address.
627 //
628 // The process is order dependent and shared tag for address 'a' must the unflattened
629 // before a ref tag for the same address 'a'.
630
631 // Tricky: The traversals for size computation and flattening has to be
632 // made aware of sharing in some way. The traversal always must fully traverse
633 // a value in order to not just partially mark it (it will then be destroyed by GC).
634 // Options:
635 // 1: run GC after a complete traversal of all values in env for size and flattening.
636 // This means that all the sizes must be stored temporarily...
637 // 2: Give a sharing table to the traversal and have the traversal switch to gc_mark_phace
638 // at each shared node to bookkeep it and keep it safe from upcomming gc.
639
640 //
641 // NOTE about sharing detection: It should be possible to traverse the
642 // heap structures using the same explicit stack algorithm as
643 // lbm_gc_mark_phase uses. Because if a structure is too large
644 // to be GC'd it cannot be used anyway, so no point in
645 // serialising/deserialising it.
646 //
647 // There are probably pros/cons to both approaches (ptr-rev vs
648 // explicit stack).
649 // ptr-rev -> correct
650 // explicit stack -> fast, simple but sensitive to programming style and limits
651 // programmer.
652 //
653 // For full benefit of pointer reversal the -DLBM_USE_GC_PTR_REV
654 // build flag is needed.
655
656
657 #ifdef LBM64
658 #define SHARING_TABLE_ENTRY_SIZE (2 + 1 + 1)
659 #else
660 #define SHARING_TABLE_ENTRY_SIZE (1 + 1 + 1)
661 #endif
662
663 #define SHARING_TABLE_TRUE 0xDEADBEEFu
664
665 18856 int32_t index_sharing_table(sharing_table *st, int32_t i) {
666
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18856 times.
18856 if (i < 0) return i; // maybe check if more than num?
667 18856 return st->start - 2 - (i * SHARING_TABLE_ENTRY_SIZE);
668 }
669
670 // Search sharing table, O(N) where N shared nodes
671 32931 int32_t sharing_table_contains(sharing_table *st, lbm_uint addr) {
672 32931 int32_t num = st->num;
673 32931 uint32_t st_tag = read_u32(st->start);
674
1/2
✓ Branch 0 taken 32931 times.
✗ Branch 1 not taken.
32931 if (st_tag == SHARING_TABLE) {
675 // sharing table tag exists but not the num field.
676
2/2
✓ Branch 0 taken 18548 times.
✓ Branch 1 taken 32573 times.
51121 for (int32_t i = 0; i < num; i ++ ) {
677 lbm_uint a;
678 18548 int32_t ix = index_sharing_table(st, i);
679 #ifdef LBM64
680 ✗ a = read_u64(ix);
681 #else
682 18548 a = read_u32(ix);
683 #endif
684
2/2
✓ Branch 0 taken 358 times.
✓ Branch 1 taken 18190 times.
18548 if (addr == a) {
685 358 return i;
686 }
687 }
688 }
689 32573 return -1;
690 }
691
692 #define SHARING_TABLE_SIZED_FIELD 0
693 #define SHARING_TABLE_FLATTENED_FIELD 1
694
695 86 bool sharing_table_set_field(sharing_table *st, int32_t ix, int32_t field, uint32_t value) {
696 int32_t wix;
697 #ifdef LBM64
698 ✗ wix = index_sharing_table(st, ix) - 2 - field;
699 #else
700 86 wix = index_sharing_table(st, ix) - 1 - field;
701 #endif
702 86 return write_u32(value, &wix, DOWNWARDS); // Dir irrelevant
703 }
704
705 222 uint32_t sharing_table_get_field(sharing_table *st, int32_t ix, int32_t field) {
706 int32_t wix;
707 #ifdef LBM64
708 ✗ wix = index_sharing_table(st, ix) - 2 - field;
709 #else
710 222 wix = index_sharing_table(st, ix) - 1 - field;
711 #endif
712 222 return read_u32(wix);
713 }
714
715 16376 static int detect_shared(lbm_value v, bool shared, void *acc) {
716 16376 sharing_table *st = (sharing_table*)acc;
717
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 16308 times.
16376 if (shared) {
718
1/2
✓ Branch 0 taken 68 times.
✗ Branch 1 not taken.
68 if (lbm_is_ptr(v)) {
719 68 lbm_uint addr = v;
720 68 int32_t ix = sharing_table_contains(st,addr);
721
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 25 times.
68 if (ix < 0) {
722 // Create place in table for a shared address and skip
723 // enough words for boolean fields.
724 #ifdef LBM64
725 ✗ write_u64(addr, &write_index, DOWNWARDS);
726 #else
727 43 write_u32(addr, &write_index, DOWNWARDS);
728 #endif
729 43 write_index -= 2; // skip 2 words for "sized" and "flattened" booleans.
730 43 st->num++;
731 }
732 }
733 }
734 16376 return TRAV_FUN_SUBTREE_PROCEED;
735 }
736
737 75 sharing_table lbm_image_sharing(void) {
738 75 lbm_value *env = lbm_get_global_env();
739
740 sharing_table st;
741 75 st.start = write_index;
742 75 st.num = 0;
743
744 75 write_u32(SHARING_TABLE, &write_index, DOWNWARDS);
745 75 write_index -= 1; // skip a word where size is to be written out of order.
746 // index is now correct for starting to write sharing table rows.
747
748
1/2
✓ Branch 0 taken 75 times.
✗ Branch 1 not taken.
75 if (env) {
749
2/2
✓ Branch 0 taken 2400 times.
✓ Branch 1 taken 75 times.
2475 for (int i = 0; i < GLOBAL_ENV_ROOTS; i ++) {
750 2400 lbm_value curr = env[i];
751
2/2
✓ Branch 0 taken 431 times.
✓ Branch 1 taken 2400 times.
2831 while(lbm_is_cons(curr)) {
752 // lbm_value name_field = lbm_caar(curr);
753 431 lbm_value val_field = lbm_cdr(lbm_car(curr));
754
2/2
✓ Branch 0 taken 321 times.
✓ Branch 1 taken 110 times.
431 if (!lbm_is_constant(val_field)) {
755 321 lbm_ptr_rev_trav(detect_shared, val_field, &st);
756 }
757 431 curr = lbm_cdr(curr);
758 }
759 }
760 // clean out all mark-bits
761 75 lbm_perform_gc();
762 }
763 // Write the number of shared nodes, 0 or more, to table entry.
764 75 int32_t wix = st.start - 1;
765 75 write_u32((uint32_t)st.num,&wix, DOWNWARDS);
766
767 75 return st;
768 }
769
770 // ////////////////////////////////////////////////////////////
771 //
772
773 typedef struct {
774 int32_t s;
775 sharing_table *st;
776 } size_accumulator;
777
778 16376 static int size_acc(lbm_value v, bool shared, void *acc) {
779 (void) shared;
780 16376 size_accumulator *sa = (size_accumulator*)acc;
781
782 16376 int32_t ix = sharing_table_contains(sa->st, v);
783
784
2/2
✓ Branch 0 taken 111 times.
✓ Branch 1 taken 16265 times.
16376 if (ix >= 0) {
785
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 43 times.
111 if (SHARING_TABLE_TRUE == sharing_table_get_field(sa->st, ix, SHARING_TABLE_SIZED_FIELD)) {
786 // shared node has been sized already and should return size of a ref node
787 // sizeof S_REF and addr
788 #ifdef LBM64
789 ✗ sa->s += 9;
790 #else
791 68 sa->s += 5;
792 #endif
793 68 return TRAV_FUN_SUBTREE_DONE;
794 } else {
795 // setting the sized field to not include the size in future occurrances.
796 43 sharing_table_set_field(sa->st, ix, SHARING_TABLE_SIZED_FIELD, SHARING_TABLE_TRUE);
797 #ifdef LBM64
798 ✗ sa->s += 9;
799 #else
800 43 sa->s += 5;
801 #endif
802 }
803 }
804
805 16308 lbm_uint t = lbm_type_of(v);
806
807
3/4
✓ Branch 0 taken 8201 times.
✓ Branch 1 taken 8107 times.
✓ Branch 2 taken 8201 times.
✗ Branch 3 not taken.
16308 if (t >= LBM_POINTER_TYPE_FIRST && t < LBM_POINTER_TYPE_LAST) {
808 8201 t = t & ~(LBM_PTR_TO_CONSTANT_BIT);
809 }
810
811
4/4
✓ Branch 0 taken 8201 times.
✓ Branch 1 taken 8107 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 8200 times.
16308 if (lbm_is_ptr(v) && (v & LBM_PTR_TO_CONSTANT_BIT)) {
812 1 sa->s += (int32_t)sizeof(lbm_uint) + 1;
813 1 return TRAV_FUN_SUBTREE_DONE;
814 }
815
816
13/14
✓ Branch 0 taken 7946 times.
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 22 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 3171 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 5 times.
✓ Branch 7 taken 2 times.
✓ Branch 8 taken 5 times.
✓ Branch 9 taken 39 times.
✓ Branch 10 taken 2 times.
✓ Branch 11 taken 4910 times.
✓ Branch 12 taken 179 times.
✗ Branch 13 not taken.
16307 switch (t) {
817 7946 case LBM_TYPE_CONS:
818 7946 sa->s += 1;
819 7946 break;
820 20 case LBM_TYPE_LISPARRAY:
821 20 sa->s += 4 + 1;
822 20 break;
823 22 case LBM_TYPE_BYTE:
824 22 sa->s += 2;
825 22 break;
826 4 case LBM_TYPE_U:
827 4 sa->s += (int32_t)sizeof(lbm_uint) + 1;
828 4 break;
829 3171 case LBM_TYPE_I:
830 3171 sa->s += (int32_t)sizeof(lbm_uint) + 1;
831 3171 break;
832 2 case LBM_TYPE_U32:
833 2 sa->s += 4 + 1;
834 2 break;
835 5 case LBM_TYPE_I32:
836 5 sa->s += 4 + 1;
837 5 break;
838 2 case LBM_TYPE_U64:
839 2 sa->s += 8 + 1;
840 2 break;
841 5 case LBM_TYPE_I64:
842 5 sa->s += 8 + 1;
843 5 break;
844 39 case LBM_TYPE_FLOAT:
845 39 sa->s += 4 + 1;
846 39 break;
847 2 case LBM_TYPE_DOUBLE:
848 2 sa->s += 8 + 1;
849 2 break;
850 4910 case LBM_TYPE_SYMBOL:
851 4910 sa->s += (int32_t)sizeof(lbm_uint) + 1;
852 4910 break;
853 179 case LBM_TYPE_ARRAY: {
854 179 lbm_int arr_size = lbm_heap_array_get_size(v);
855 179 const uint8_t *d = lbm_heap_array_get_data_ro(v);
856
2/4
✓ Branch 0 taken 179 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 179 times.
✗ Branch 3 not taken.
179 if (arr_size > 0 && d != NULL) {
857 179 sa->s += (int32_t)(1 + 4 + arr_size);
858 }
859 179 }break;
860 }
861 16307 return TRAV_FUN_SUBTREE_CONTINUE;
862 }
863
864 typedef struct {
865 bool res;
866 sharing_table *st;
867 int arg;
868 } flatten_node_meta_data;
869
870 16376 static int flatten_node(lbm_value v, bool shared, void *arg) {
871 (void) shared;
872 16376 flatten_node_meta_data *md = (flatten_node_meta_data*)arg;
873 16376 bool *acc = &md->res;
874
875 16376 int32_t ix = sharing_table_contains(md->st, v);
876
877
2/2
✓ Branch 0 taken 111 times.
✓ Branch 1 taken 16265 times.
16376 if (ix >= 0) {
878
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 43 times.
111 if (SHARING_TABLE_TRUE == sharing_table_get_field(md->st, ix, SHARING_TABLE_FLATTENED_FIELD)) {
879 // Shared node already flattened.
880 #if DEBUG
881 printf("FLATTEN: Writing S_REF for target_map[%d] (addr %x)\n", ix, (unsigned int)v);
882 #endif
883 68 fv_write_u8(S_REF);
884 #ifdef LBM64
885 ✗ fv_write_u64((lbm_uint)v);
886 #else
887 68 fv_write_u32((lbm_uint)v);
888 #endif
889 68 return TRAV_FUN_SUBTREE_DONE;
890 } else {
891 // Shared node not yet flattened.
892 #if DEBUG
893 printf("FLATTEN: Writing S_SHARED for target_map[%d] (addr %x)\n", ix, (unsigned int)v);
894 #endif
895 43 sharing_table_set_field(md->st, ix, SHARING_TABLE_FLATTENED_FIELD, SHARING_TABLE_TRUE);
896 43 fv_write_u8(S_SHARED);
897 #ifdef LBM64
898 ✗ fv_write_u64((lbm_uint)v);
899 #else
900 43 fv_write_u32((lbm_uint)v);
901 #endif
902 // Continue flattening along this subtree.
903 }
904 }
905
906 16308 lbm_uint t = lbm_type_of(v);
907
908
3/4
✓ Branch 0 taken 8201 times.
✓ Branch 1 taken 8107 times.
✓ Branch 2 taken 8201 times.
✗ Branch 3 not taken.
16308 if (t >= LBM_POINTER_TYPE_FIRST && t < LBM_POINTER_TYPE_LAST) {
909 8201 t = t & ~(LBM_PTR_TO_CONSTANT_BIT);
910 }
911
912
4/4
✓ Branch 0 taken 8201 times.
✓ Branch 1 taken 8107 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 8200 times.
16308 if (lbm_is_ptr(v) && (v & LBM_PTR_TO_CONSTANT_BIT)) {
913
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 *acc = *acc && fv_write_u8(S_CONSTANT_REF);
914 #ifdef LBM64
915 ✗ *acc = *acc && fv_write_u64((lbm_uint)v);
916 #else
917
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 *acc = *acc && fv_write_u32((lbm_uint)v);
918 #endif
919 1 return TRAV_FUN_SUBTREE_DONE;
920 }
921
922
13/14
✓ Branch 0 taken 7946 times.
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 22 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 3171 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 5 times.
✓ Branch 7 taken 2 times.
✓ Branch 8 taken 5 times.
✓ Branch 9 taken 39 times.
✓ Branch 10 taken 2 times.
✓ Branch 11 taken 4910 times.
✓ Branch 12 taken 179 times.
✗ Branch 13 not taken.
16307 switch (t) {
923 7946 case LBM_TYPE_CONS:
924
2/4
✓ Branch 0 taken 7946 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7946 times.
✗ Branch 3 not taken.
7946 *acc = *acc && i_f_cons();
925 7946 break;
926 20 case LBM_TYPE_LISPARRAY: {
927 20 lbm_array_header_t *header = (lbm_array_header_t*)lbm_car(v);
928
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 if (header) {
929 20 uint32_t size = (uint32_t)(header->size / sizeof(lbm_value));
930
2/4
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
20 *acc = *acc && i_f_lisp_array(size);
931 } else {
932 // hmm
933 }
934 20 } break;
935 22 case LBM_TYPE_BYTE:
936
2/4
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
22 *acc = *acc && i_f_b((uint8_t)lbm_dec_as_char(v));
937 22 break;
938 4 case LBM_TYPE_U:
939
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 *acc = *acc && i_f_u(lbm_dec_u(v));
940 4 break;
941 3171 case LBM_TYPE_I:
942
2/4
✓ Branch 0 taken 3171 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3171 times.
✗ Branch 3 not taken.
3171 *acc = *acc && i_f_i(lbm_dec_i(v));
943 3171 break;
944 2 case LBM_TYPE_U32:
945
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 *acc = *acc && i_f_u32(lbm_dec_as_u32(v));
946 2 break;
947 5 case LBM_TYPE_I32:
948
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 *acc = *acc && i_f_i32(lbm_dec_as_i32(v));
949 5 break;
950 2 case LBM_TYPE_U64:
951
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 *acc = *acc && i_f_u64(lbm_dec_as_u64(v));
952 2 break;
953 5 case LBM_TYPE_I64:
954
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 *acc = *acc && i_f_i64(lbm_dec_as_i64(v));
955 5 break;
956 39 case LBM_TYPE_FLOAT:
957
2/4
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 39 times.
✗ Branch 3 not taken.
39 *acc = *acc && i_f_float(lbm_dec_as_float(v));
958 39 break;
959 2 case LBM_TYPE_DOUBLE:
960
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 *acc = *acc && i_f_double(lbm_dec_as_double(v));
961 2 break;
962 4910 case LBM_TYPE_SYMBOL:
963
2/4
✓ Branch 0 taken 4910 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4910 times.
✗ Branch 3 not taken.
4910 *acc = *acc && i_f_sym(v);
964 4910 break;
965 179 case LBM_TYPE_ARRAY: {
966 179 lbm_int s = lbm_heap_array_get_size(v);
967 179 const uint8_t *d = lbm_heap_array_get_data_ro(v);
968
2/4
✓ Branch 0 taken 179 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 179 times.
✗ Branch 3 not taken.
179 if (s > 0 && d != NULL) {
969
2/4
✓ Branch 0 taken 179 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 179 times.
✗ Branch 3 not taken.
179 *acc = *acc && i_f_lbm_array((uint32_t)s, (uint8_t*)d);
970 }
971 179 }break;
972 ✗ default:
973 ✗ break;
974 }
975 16307 return TRAV_FUN_SUBTREE_CONTINUE;
976 }
977
978 // Performing GC after using the ptr_rev_trav to restore the
979 // GC-bit in the value traversed.
980 321 static int32_t image_flatten_size(sharing_table *st, lbm_value v) {
981 size_accumulator sa;
982 321 sa.s = 0;
983 321 sa.st = st;
984 321 lbm_ptr_rev_trav(size_acc, v, &sa);
985 321 lbm_perform_gc();
986 321 return sa.s; // Should always be "ok" now.
987 }
988
989 321 static bool image_flatten_value(sharing_table *st, lbm_value v) {
990 flatten_node_meta_data md;
991 321 md.res = true;
992 321 md.st = st;
993 321 md.arg = 0;
994 321 lbm_ptr_rev_trav(flatten_node, v, &md);
995 321 lbm_perform_gc();
996 321 return md.res; // ok = enough space in image for flat val.
997 }
998
999 // ////////////////////////////////////////////////////////////
1000 // print sharing table
1001 #if DEBUG
1002 void print_sharing_table(sharing_table *st) {
1003 int32_t pos = st->start;
1004 int32_t num = st->num;
1005 char buf[256];
1006
1007 for (int i = 0; i < num; i ++) {
1008 int32_t ix = index_sharing_table(st, i);
1009 lbm_uint a = read_u32(ix); // address
1010
1011 lbm_print_value(buf, 256, a);
1012 printf("%d\t%x\t%s\n",i, a, buf);
1013 }
1014
1015
1016 }
1017 #endif
1018
1019 // ////////////////////////////////////////////////////////////
1020 //
1021 75 bool lbm_image_save_global_env(void) {
1022
1023 75 sharing_table st = lbm_image_sharing();
1024 75 lbm_value *env = lbm_get_global_env();
1025
1/2
✓ Branch 0 taken 75 times.
✗ Branch 1 not taken.
75 if (env) {
1026
2/2
✓ Branch 0 taken 2400 times.
✓ Branch 1 taken 75 times.
2475 for (int i = 0; i < GLOBAL_ENV_ROOTS; i ++) {
1027 2400 lbm_value curr = env[i];
1028
2/2
✓ Branch 0 taken 431 times.
✓ Branch 1 taken 2400 times.
2831 while(lbm_is_cons(curr)) {
1029 431 lbm_value name_field = lbm_caar(curr);
1030 431 lbm_value val_field = lbm_cdr(lbm_car(curr));
1031
1032
2/2
✓ Branch 0 taken 110 times.
✓ Branch 1 taken 321 times.
431 if (lbm_is_constant(val_field)) {
1033 110 write_u32(BINDING_CONST, &write_index, DOWNWARDS);
1034 110 write_lbm_value(name_field, &write_index, DOWNWARDS);
1035 110 write_lbm_value(val_field, &write_index, DOWNWARDS);
1036 } else {
1037 321 int fv_size = image_flatten_size(&st, val_field);
1038
1/2
✓ Branch 0 taken 321 times.
✗ Branch 1 not taken.
321 if (fv_size > 0) {
1039
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 293 times.
321 fv_size = (fv_size % 4 == 0) ? (fv_size / 4) : (fv_size / 4) + 1; // num 32bit words
1040
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 321 times.
321 if ((write_index - fv_size) <= (int32_t)image_const_heap.next) {
1041 ✗ return false;
1042 }
1043 321 write_u32(BINDING_FLAT, &write_index, DOWNWARDS);
1044 321 write_u32((uint32_t)fv_size , &write_index, DOWNWARDS);
1045 321 write_lbm_value(name_field, &write_index, DOWNWARDS);
1046 321 write_index = write_index - fv_size; // subtract fv_size
1047 #if DEBUG
1048 int32_t data_start = write_index; // Save the start position
1049 #endif
1050
1/2
✓ Branch 0 taken 321 times.
✗ Branch 1 not taken.
321 if (image_flatten_value(&st, val_field)) { // adds fv_size back
1051 321 fv_write_flush();
1052 #if DEBUG
1053 printf("Flattenining address: %x\n", val_field);
1054 for (int i = 0; i < fv_size; i ++) {
1055 uint32_t v = read_u32(data_start + i);
1056 uint8_t *p = &v;
1057 for (int j = 0; j < 4; j ++) {
1058 printf("%x ", p[j]);
1059 }
1060 printf(" ");
1061 }
1062 printf("\n");
1063 #endif
1064
1065 // TODO: What error handling makes sense?
1066 } else {
1067 ✗ return false;
1068 }
1069 321 write_index = write_index - fv_size - 1; // subtract fv_size
1070 } else {
1071 ✗ return false;
1072 }
1073 }
1074 431 curr = lbm_cdr(curr);
1075 }
1076 }
1077 #if DEBUG
1078 printf("Sharing table:\n");
1079 print_sharing_table(&st);
1080 #endif
1081 75 return true;
1082 }
1083 ✗ return false;
1084 }
1085
1086 // The extension table is created at system startup.
1087 // Extensions can also be added dynamically.
1088 // Dynamically added extensions have names starting with "ext-"
1089 // and their names are placed in RAM by the reader.
1090 //
1091 // Symbol_id -> index in extension table mapping
1092 // is created as extensions are added.
1093 // dynamic extensions are added after "built-in" extensions
1094 // and have higher indices.
1095
1096 75 bool lbm_image_save_extensions(void) {
1097 75 bool r = true;
1098 75 lbm_uint num = lbm_get_num_extensions();
1099
1/2
✓ Branch 0 taken 75 times.
✗ Branch 1 not taken.
75 if (num > 0) {
1100 // Dynamically registered extensions can have name fields
1101 // pointing to LBM_MEMORY (RAM). Move these strings to flash
1102 // Before copying the entire extension table to flash.
1103
2/2
✓ Branch 0 taken 15377 times.
✓ Branch 1 taken 75 times.
15452 for (lbm_uint i = 0; i < num; i ++) {
1104 15377 const char *name_ptr = extension_table[i].name;
1105
3/4
✓ Branch 0 taken 15377 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 15376 times.
15377 if (name_ptr && lbm_memory_ptr_inside((lbm_uint *)name_ptr)) {
1106 1 char *addr = lbm_image_add_symbol_name(name_ptr, strlen(name_ptr));
1107
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!addr) return false;
1108 1 extension_table[i].name = addr;
1109 }
1110 }
1111
1112
2/4
✓ Branch 0 taken 75 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 75 times.
✗ Branch 3 not taken.
75 r = r && write_u32(EXTENSION_TABLE, &write_index, DOWNWARDS);
1113
2/4
✓ Branch 0 taken 75 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 75 times.
✗ Branch 3 not taken.
75 r = r && write_u32((uint32_t)num , &write_index, DOWNWARDS);
1114
2/2
✓ Branch 0 taken 15377 times.
✓ Branch 1 taken 75 times.
15452 for (lbm_uint i = 0; i < num; i ++) {
1115
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15377 times.
15377 if (!r) return r;
1116
1117 15377 const char *name_ptr = extension_table[i].name;
1118 // Static extensions have names that are C string literals
1119 // living in the firmware binary itself. Those are assumed
1120 // stable for as long as that firmware build is running.
1121 #ifdef LBM64
1122 ✗ r = r && write_u64((uint64_t)name_ptr, &write_index, DOWNWARDS);
1123 ✗ r = r && write_u64((uint64_t)extension_table[i].fptr, &write_index, DOWNWARDS);
1124 #else
1125
2/4
✓ Branch 0 taken 15377 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15377 times.
✗ Branch 3 not taken.
15377 r = r && write_u32((uint32_t)name_ptr, &write_index, DOWNWARDS);
1126
2/4
✓ Branch 0 taken 15377 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15377 times.
✗ Branch 3 not taken.
15377 r = r && write_u32((uint32_t)extension_table[i].fptr, &write_index, DOWNWARDS);
1127 #endif
1128 } }
1129 75 return r;
1130 }
1131
1132 static uint32_t last_const_heap_ix = 0;
1133
1134 75 bool lbm_image_save_constant_heap_ix(void) {
1135 75 bool r = true; // saved or no need to save it.
1136
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 61 times.
75 if (image_const_heap.next != last_const_heap_ix) {
1137 14 last_const_heap_ix = (uint32_t)image_const_heap.next;
1138 14 r = write_u32(CONSTANT_HEAP_IX, &write_index, DOWNWARDS);
1139
2/4
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
14 r = r && write_u32((uint32_t)image_const_heap.next, &write_index, DOWNWARDS);
1140 }
1141 75 return r;
1142 }
1143
1144 753 bool lbm_image_exists(void) {
1145 753 uint32_t val = read_u32((int32_t)image_size - 1);
1146 753 return val == IMAGE_INITIALIZED;
1147 }
1148
1149 67033 void lbm_image_init(uint32_t* image_mem_address,
1150 uint32_t image_size_words,
1151 lbm_image_write_fun image_write_fun) {
1152 67033 image_write = image_write_fun;
1153 67033 image_address = image_mem_address;
1154 67033 image_size = image_size_words;
1155 67033 write_index = (int32_t)image_size_words -1;
1156 67033 image_has_extensions = false;
1157 67033 image_version = NULL;
1158 67033 last_const_heap_ix = 0;
1159 67033 }
1160
1161 66779 void lbm_image_create(char *version_str) {
1162 66779 write_u32(IMAGE_INITIALIZED, &write_index, DOWNWARDS);
1163
1/2
✓ Branch 0 taken 66779 times.
✗ Branch 1 not taken.
66779 if (version_str) {
1164 66779 write_u32(VERSION_ENTRY, &write_index, DOWNWARDS);
1165 66779 write_string_upwards(version_str, strlen(version_str));
1166 }
1167 66779 }
1168
1169
1170 67033 bool lbm_image_boot(void) {
1171 //process image
1172 67033 int32_t pos = (int32_t)image_size-1;
1173 67033 last_const_heap_ix = 0;
1174
1175 sharing_table st;
1176 67033 lbm_uint *target_map = NULL; // Target addresses for shared/refs from the flat values.
1177
1178
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 226119 times.
✓ Branch 2 taken 226119 times.
✗ Branch 3 not taken.
226119 while (pos >= 0 && pos > (int32_t)last_const_heap_ix) {
1179 226119 uint32_t val = read_u32(pos);
1180 226119 pos --;
1181
11/11
✓ Branch 0 taken 67033 times.
✓ Branch 1 taken 67033 times.
✓ Branch 2 taken 5482 times.
✓ Branch 3 taken 14 times.
✓ Branch 4 taken 110 times.
✓ Branch 5 taken 321 times.
✓ Branch 6 taken 5481 times.
✓ Branch 7 taken 13462 times.
✓ Branch 8 taken 75 times.
✓ Branch 9 taken 75 times.
✓ Branch 10 taken 67033 times.
226119 switch(val) {
1182 67033 case IMAGE_INITIALIZED: {
1183 67033 image_const_heap_start_ix = 0; // const heap starts at 0
1184 67033 lbm_const_heap_init(image_const_heap_write,
1185 &image_const_heap,
1186 (lbm_uint*)(image_address));
1187 // initialized is a one word field
1188 67033 } break;
1189 67033 case VERSION_ENTRY: {
1190 67033 uint32_t size = read_u32(pos); pos --;
1191 67033 image_version = (char*)(image_address + (pos - (int32_t)size + 1));
1192 67033 pos -= (int32_t)size;
1193 67033 } break;
1194 5482 case SYMBOL_NAME_ENTRY: {
1195 // Just jump accross the string.
1196 5482 uint32_t size = read_u32(pos); pos --;
1197 5482 pos -= (int32_t)size;
1198 5482 } break;
1199 14 case CONSTANT_HEAP_IX: {
1200 14 uint32_t next = read_u32(pos);
1201 14 pos --;
1202 14 last_const_heap_ix = next;
1203 14 image_const_heap.next = next;
1204 14 } break;
1205 110 case BINDING_CONST: {
1206 // on 64 bit | on 32 bit
1207 // pos -> key_high | pos -> key
1208 // pos - 1 -> key_low | pos - 1 -> val
1209 // pos - 2 -> val_high
1210 // pos - 3 -> val_low
1211 #ifdef LBM64
1212 ✗ lbm_uint bind_key = read_u64(pos-1);
1213 ✗ lbm_uint bind_val = read_u64(pos-3);
1214 ✗ pos -= 4;
1215 #else
1216 110 lbm_uint bind_key = read_u32(pos);
1217 110 lbm_uint bind_val = read_u32(pos-1);
1218 110 pos -= 2;
1219 #endif
1220 110 lbm_uint ix_key = lbm_dec_sym(bind_key) & GLOBAL_ENV_MASK;
1221 110 lbm_value *global_env = lbm_get_global_env();
1222 110 lbm_uint orig_env = global_env[ix_key];
1223 110 lbm_value new_env = lbm_env_set(orig_env,bind_key,bind_val);
1224
1225
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 110 times.
110 if (lbm_is_symbol(new_env)) {
1226 ✗ return false;
1227 }
1228 110 global_env[ix_key] = new_env;
1229 110 } break;
1230 321 case BINDING_FLAT: {
1231 // on 64 bit | on 32 bit
1232 // pos -> size | pos -> size
1233 // pos - 1 -> key_high | pos - 1 -> key
1234 // pos - 2 -> key_low
1235 //
1236 321 int32_t s = (int32_t)read_u32(pos);
1237 // size in 32 or 64 bit words.
1238 #ifdef LBM64
1239 ✗ lbm_uint bind_key = read_u64(pos-2);
1240 ✗ pos -= 3;
1241 #else
1242 321 lbm_uint bind_key = read_u32(pos-1);
1243 321 pos -= 2;
1244 #endif
1245
1246 321 pos -= s;
1247 lbm_flat_value_t fv;
1248 321 fv.buf = (uint8_t*)(image_address + pos);
1249 321 fv.buf_size = (uint32_t)s * sizeof(lbm_uint); // GEQ to actual buf
1250 321 fv.buf_pos = 0;
1251 lbm_value unflattened;
1252
2/2
✓ Branch 0 taken 103 times.
✓ Branch 1 taken 218 times.
321 if (target_map) {
1253
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 103 times.
103 if (!lbm_unflatten_value_sharing(&st, target_map, &fv, &unflattened)) {
1254 ✗ return false;
1255 }
1256 // When a value is unflattened it may contain shared subvalues
1257 // and references to shared values. A reference may point to either
1258 // values that are shared within the value that is currently unflattened
1259 // or to a value that has previously been unflattened.
1260 //
1261 // There is an ordering property that must be maintained that the node
1262 // with the S_SHARED tag is always processed before any corresponding S_REF tags.
1263 // This means that if a ref node is unflattened the target to point it to will
1264 // already exist.
1265 //
1266 // If GC needs to happen while unflattening a value, there is no danger of messing
1267 // up the addresses to point references to because:
1268 // 1. The S_SHARED node is local to the same value and will be recreated after GC
1269 // and the ref value in target map will be overwritten. Any local refs will be also
1270 // be recreated. Any refs to the S_Shared outside of this value, will be in values
1271 // processed in the future.
1272 // 2. S_SHARED nodes that have been created as part of prvious value are untouched
1273 // by running GC as they have already been unflattened and should be reachable
1274 // on the environment. Their mapping in the target map is still valid.
1275
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 103 times.
103 if (lbm_is_symbol_merror(unflattened)) {
1276 //memset(target_map, 0, st.num * sizeof(lbm_uint));
1277 ✗ lbm_perform_gc();
1278 ✗ lbm_unflatten_value_sharing(&st, target_map, &fv, &unflattened);
1279 }
1280 } else {
1281 218 lbm_unflatten_value(&fv, &unflattened);
1282
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 218 times.
218 if (lbm_is_symbol_merror(unflattened)) {
1283 ✗ lbm_perform_gc();
1284 ✗ lbm_unflatten_value(&fv, &unflattened);
1285 }
1286 }
1287 321 lbm_uint ix_key = lbm_dec_sym(bind_key) & GLOBAL_ENV_MASK;
1288 321 lbm_value *global_env = lbm_get_global_env();
1289 321 lbm_uint orig_env = global_env[ix_key];
1290 321 lbm_value new_env = lbm_env_set(orig_env,bind_key,unflattened);
1291
1292
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 321 times.
321 if (lbm_is_symbol(new_env)) {
1293 ✗ return false;
1294 }
1295 321 global_env[ix_key] = new_env;
1296 321 pos --;
1297 321 } break;
1298 5481 case SYMBOL_ENTRY: {
1299 // on 64 bit | on 32 bit
1300 // pos -> symlist_addr_high_word | pos -> symlist_ptr
1301 // pos - 1 -> symlist_addr_low_word | pos - 1 -> id
1302 // pos - 2 -> id_high_word | pos - 2 -> name_ptr
1303 // pos - 3 -> id_low_word |
1304 // pos - 4 -> name_ptr_high_word |
1305 // pos - 5 -> name_ptr_low_word |
1306 #ifdef LBM64
1307 ✗ int32_t entry_pos = pos - 5;
1308 ✗ lbm_uint *p = (lbm_uint*)(image_address + entry_pos);
1309 ✗ uint32_t sym_id = (uint32_t)(p[1]);
1310 ✗ lbm_uint next_id = lbm_symrepr_get_next_id();
1311 ✗ if (sym_id >= RUNTIME_SYMBOLS_START && sym_id >= next_id ) {
1312 ✗ lbm_symrepr_set_next_id(sym_id + 1);
1313 }
1314 ✗ lbm_symrepr_set_symlist((lbm_uint*)(image_address + entry_pos));
1315 ✗ pos -= 6;
1316 #else
1317 5481 int32_t entry_pos = pos - 2;
1318 5481 lbm_uint *p = (lbm_uint*)(image_address + entry_pos);
1319 5481 uint32_t sym_id = (uint32_t)(p[1]);
1320 5481 lbm_uint next_id = lbm_symrepr_get_next_id();
1321
2/4
✓ Branch 0 taken 5481 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5481 times.
✗ Branch 3 not taken.
5481 if (sym_id >= RUNTIME_SYMBOLS_START && sym_id >= next_id ) {
1322 5481 lbm_symrepr_set_next_id(sym_id + 1);
1323 }
1324 5481 lbm_symrepr_set_symlist((lbm_uint*)(image_address + entry_pos));
1325 5481 pos -= 3;
1326 #endif
1327 5481 } break;
1328 13462 case SYMBOL_LINK_ENTRY: {
1329 // on 64 bits | on 32 bit
1330 // pos -> link_ptr_high | pos -> link_ptr
1331 // pos - 1 -> link_ptr_low | pos - 1 -> symlist_ptr
1332 // pos - 2 -> symlist_addr_high_word | pos - 2 -> id
1333 // pos - 3 -> symlist_addr_low_word | pos - 3 -> name_ptr;
1334 // pos - 4 -> id_high_word
1335 // pos - 5 -> id_low_word
1336 // pos - 6 -> name_ptr_high_word
1337 // pos - 7 -> name_ptr_low_word
1338 //int32_t entry_pos = pos - (int32_t)(3 * (sizeof(lbm_uint) / 4));
1339 lbm_uint link_ptr;
1340 lbm_uint sym_id;
1341 #ifdef LBM64
1342 ✗ link_ptr = read_u64(pos-1);
1343 ✗ sym_id = read_u64(pos-5);
1344 ✗ *((lbm_uint*)link_ptr) = sym_id;
1345 ✗ lbm_uint next_id = lbm_symrepr_get_next_id();
1346 ✗ if (sym_id >= RUNTIME_SYMBOLS_START && sym_id >= next_id ) {
1347 ✗ lbm_symrepr_set_next_id(sym_id + 1);
1348 }
1349 ✗ lbm_symrepr_set_symlist((lbm_uint*)(image_address + (pos - 7)));
1350 ✗ pos -= 8;
1351 #else
1352 13462 link_ptr = read_u32(pos);
1353 13462 sym_id = read_u32(pos-2);
1354 13462 *((lbm_uint*)link_ptr) = sym_id;
1355 13462 lbm_uint next_id = lbm_symrepr_get_next_id();
1356
2/4
✓ Branch 0 taken 13462 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13462 times.
✗ Branch 3 not taken.
13462 if (sym_id >= RUNTIME_SYMBOLS_START && sym_id >= next_id ) {
1357 13462 lbm_symrepr_set_next_id(sym_id + 1);
1358 }
1359 13462 lbm_symrepr_set_symlist((lbm_uint*)(image_address + (pos - 3)));
1360 13462 pos -= 4;
1361 #endif
1362 13462 } break;
1363 75 case EXTENSION_TABLE: {
1364 // on 64 bit | on 32 bit
1365 // pos -> name_ptr_high | pos -> name_ptr
1366 // pos - 1 -> name_ptr_low | pos - 1 -> fptr
1367 // pos - 2 -> fptr_high
1368 // pos - 3 -> fptr_low
1369 75 int32_t num = (int32_t)read_u32(pos); pos --;
1370
1371 75 int32_t i = 0;
1372
2/2
✓ Branch 0 taken 15377 times.
✓ Branch 1 taken 75 times.
15452 for (i = 0; i < num; i ++) {
1373 lbm_uint name;
1374 lbm_uint fptr;
1375 #ifdef LBM64
1376 ✗ name = read_u64(pos-1);
1377 ✗ fptr = read_u64(pos-3);
1378 ✗ pos -= 4;
1379 #else
1380 15377 name = read_u32(pos);
1381 15377 fptr = read_u32(pos-1);
1382 15377 pos -= 2;
1383 #endif
1384 15377 extension_table[i].name = (char*)name;
1385 15377 extension_table[i].fptr = (extension_fptr)fptr;
1386 }
1387 75 lbm_extensions_set_next((lbm_uint)i);
1388 75 image_has_extensions = true;
1389 75 } break;
1390 75 case SHARING_TABLE: {
1391 75 st.start = pos +1;
1392 75 uint32_t num = read_u32(pos); pos --;
1393 75 st.num = (int32_t)num;
1394
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 55 times.
75 if (num > 0) {
1395 20 target_map = lbm_malloc(num * sizeof(lbm_uint));
1396
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (!target_map ) {
1397 ✗ return false;
1398 }
1399 20 memset(target_map, 0, num * sizeof(lbm_uint));
1400 }
1401 #ifdef LBM64
1402 ✗ pos -= (int32_t)(num + (num * 3));
1403 #else
1404 75 pos -= (int32_t)num * 3;
1405 #endif
1406 75 } break;
1407 67033 default:
1408 67033 write_index = pos+1;
1409 67033 goto done_loading_image;
1410 break;
1411 }
1412 }
1413 ✗ done_loading_image:
1414
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 67013 times.
67033 if (target_map) lbm_free(target_map);
1415 67033 return true;
1416 }
1417