GCC Code Coverage Report


Directory: ../src/
File: lbm_image.c
Date: 2026-07-02 10:10:49
Coverage Exec Excl Total
Lines: 88.7% 541 0 610
Functions: 96.1% 49 0 51
Branches: 56.0% 266 0 475

Line Branch Exec Source
1 /*
2 Copyright 2025 Joel Svensson svenssonjoel@yahoo.se
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include <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 true
193 #define UPWARDS false
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 72 uint32_t *lbm_image_get_image(void) {
204 72 return image_address;
205 }
206
207 72 uint32_t lbm_image_get_size(void) {
208 72 return image_size;
209 }
210
211 8210 int32_t lbm_image_get_write_index(void) {
212 8210 return write_index;
213 }
214
215 382 bool lbm_image_has_extensions(void) {
216 382 return image_has_extensions;
217 }
218
219 363906 uint32_t read_u32(int32_t index) {
220 363906 return *((uint32_t*)(image_address + index));
221 }
222
223 ✗ uint64_t read_u64(int32_t index) {
224 // image_addres is an u32 ptr. so addr + i is a step of i * 4 bytes
225 ✗ return *((uint64_t*)(image_address + index));
226 }
227
228 12770775 bool write_u32(uint32_t w, int32_t *i, bool direction) {
229 12770775 bool r = image_write(w, *i, false);
230
2/2
✓ Branch 0 taken 11865158 times.
✓ Branch 1 taken 905617 times.
12770775 (*i) += direction ? -1 : 1;
231 12770775 return r;
232 }
233
234 2209562 bool write_u64(uint64_t dw, int32_t *i, bool direction) {
235 2209562 uint32_t *words = (uint32_t*)&dw;
236
237 // downwards ... hw lw
238 // ix ix-1
239 // upwards hw lw ...
240 // ix+1 ix
241
242 // true = downwards
243
244 2209562 bool r = true;
245
1/2
✓ Branch 0 taken 2209562 times.
✗ Branch 1 not taken.
2209562 if (direction) {
246
2/4
✓ Branch 0 taken 2209562 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 2209562 times.
✗ Branch 4 not taken.
2209562 r = r && write_u32(words[1], i, direction);
247
2/4
✓ Branch 0 taken 2209562 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 2209562 times.
✗ Branch 4 not taken.
2209562 r = r && write_u32(words[0], i, direction);
248 } else {
249 ✗ r = r && write_u32(words[0], i, direction);
250 ✗ r = r && write_u32(words[1], i, direction);
251 }
252 2209562 return r;
253 }
254
255 // fv_write function write values as big endian.
256
257 uint32_t fv_buf_ix = 0;
258 uint8_t fv_buf[4] = {0};
259 50668 bool fv_write_u8(uint8_t b) {
260 50668 bool r = true;
261
2/2
✓ Branch 0 taken 12519 times.
✓ Branch 1 taken 38149 times.
50668 if (fv_buf_ix >= 4) {
262 12519 r = write_u32(((uint32_t*)fv_buf)[0], &write_index, UPWARDS);
263 12519 memset(fv_buf,0,4);
264 12519 fv_buf_ix = 0;
265 }
266 50668 fv_buf[fv_buf_ix] = b;
267 50668 fv_buf_ix++;
268 50668 return r;
269 }
270
271 313 bool fv_write_flush(void) {
272
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 313 times.
313 if (fv_buf_ix == 0) return true;
273 else {
274 313 bool r = write_u32(((uint32_t*)fv_buf)[0], &write_index, UPWARDS);;
275 313 fv_buf_ix = 0;
276 313 memset(fv_buf,0,4);
277 313 return r;
278 }
279 }
280
281 8270 bool fv_write_u32(uint32_t w) {
282 8270 uint8_t * bytes = (uint8_t*)&w;
283 return
284
1/2
✓ Branch 1 taken 8270 times.
✗ Branch 2 not taken.
16540 fv_write_u8(bytes[3]) &&
285
1/2
✓ Branch 1 taken 8270 times.
✗ Branch 2 not taken.
16540 fv_write_u8(bytes[2]) &&
286
2/4
✓ Branch 0 taken 8270 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 8270 times.
✗ Branch 4 not taken.
24810 fv_write_u8(bytes[1]) &&
287 8270 fv_write_u8(bytes[0]);
288 }
289
290 9 bool fv_write_u64(uint64_t dw) {
291 9 uint8_t * bytes = (uint8_t*)&dw;
292 return
293
1/2
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
18 fv_write_u8(bytes[7]) &&
294
1/2
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
18 fv_write_u8(bytes[6]) &&
295
1/2
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
18 fv_write_u8(bytes[5]) &&
296
1/2
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
18 fv_write_u8(bytes[4]) &&
297
1/2
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
18 fv_write_u8(bytes[3]) &&
298
1/2
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
18 fv_write_u8(bytes[2]) &&
299
2/4
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 9 times.
✗ Branch 4 not taken.
27 fv_write_u8(bytes[1]) &&
300 9 fv_write_u8(bytes[0]);
301 }
302
303
304 6721478 bool write_lbm_uint(lbm_uint ptr_val, int32_t *i, bool direction) {
305 #ifdef LBM64
306 2209562 return write_u64(ptr_val, i, direction);
307 #else
308 4511916 return write_u32(ptr_val, i, direction);
309 #endif
310 }
311
312 529 bool write_lbm_value(lbm_value v, int32_t *i, bool direction) {
313 #ifdef LBM64
314 ✗ return write_u64(v, i, direction);
315 #else
316 529 return write_u32(v, i, direction);
317 #endif
318 }
319
320 // ////////////////////////////////////////////////////////////
321 // Flatten a value into image
322
323 // TODO: Consants things that are stored in the image
324 // does not need to be flattened. Could refer to these by
325 // reference. Some new kinds of flat values needs to be added
326 // for this referencing to work.
327
328 // TODO: Symbols in a flat_value in an image can be stored as
329 // its numerical representation rather than its string rep.
330
331 7782 static bool i_f_cons(void ) {
332 7782 return fv_write_u8(S_CONS);
333 }
334
335 20 static bool i_f_lisp_array(uint32_t size) {
336 // arrays are smaller than 2^32 elements long
337 20 bool r = fv_write_u8(S_LBM_LISP_ARRAY);
338
2/4
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 20 times.
✗ Branch 4 not taken.
20 r = r && fv_write_u32(size);
339 20 return r;
340 }
341
342 4776 static bool i_f_sym(lbm_value sym) {
343 4776 lbm_uint sym_id = lbm_dec_sym(sym);
344 4776 bool r = fv_write_u8(S_SYM_VALUE);
345 #ifndef LBM64
346
2/4
✓ Branch 0 taken 4776 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 4776 times.
✗ Branch 4 not taken.
4776 r = r && fv_write_u32(sym_id);
347 #else
348 ✗ r = r && fv_write_u64(sym_id);
349 #endif
350 4776 return r;
351 }
352
353 3139 static bool i_f_i(lbm_int i) {
354 3139 bool res = true;
355 #ifndef LBM64
356
2/4
✓ Branch 0 taken 3139 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 3139 times.
✗ Branch 4 not taken.
3139 res = res && fv_write_u8(S_I28_VALUE);
357
2/4
✓ Branch 0 taken 3139 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 3139 times.
✗ Branch 4 not taken.
3139 res = res && fv_write_u32((uint32_t)i);
358 #else
359 ✗ res = res && fv_write_u8(S_I56_VALUE);
360 ✗ res = res && fv_write_u64((uint64_t)i);
361 #endif
362 3139 return res;
363 }
364
365 4 static bool i_f_u(lbm_uint u) {
366 4 bool res = true;
367 #ifndef LBM64
368
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
4 res = res && fv_write_u8(S_U28_VALUE);
369
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
4 res = res && fv_write_u32((uint32_t)u);
370 #else
371 ✗ res = res && fv_write_u8(S_U56_VALUE);
372 ✗ res = res && fv_write_u64((uint64_t)u);
373 #endif
374 4 return res;
375 }
376
377 22 static bool i_f_b(uint8_t b) {
378 22 bool res = true;
379
2/4
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 22 times.
✗ Branch 4 not taken.
22 res = res && fv_write_u8(S_BYTE_VALUE);
380
2/4
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 22 times.
✗ Branch 4 not taken.
22 res = res && fv_write_u8(b);
381 22 return res;
382 }
383
384 5 static bool i_f_i32(int32_t w) {
385 5 bool res = true;
386
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 5 times.
✗ Branch 4 not taken.
5 res = res && fv_write_u8(S_I32_VALUE);
387
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 5 times.
✗ Branch 4 not taken.
5 res = res && fv_write_u32((uint32_t)w);
388 5 return res;
389 }
390
391 2 static bool i_f_u32(uint32_t w) {
392 2 bool res = true;
393
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
2 res = res && fv_write_u8(S_U32_VALUE);
394
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
2 res = res && fv_write_u32(w);
395 2 return res;
396 }
397
398 39 static bool i_f_float(float f) {
399 39 bool res = true;
400
2/4
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 39 times.
✗ Branch 4 not taken.
39 res = res && fv_write_u8(S_FLOAT_VALUE);
401 uint32_t u;
402 39 memcpy(&u, &f, sizeof(uint32_t));
403
2/4
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 39 times.
✗ Branch 4 not taken.
39 res = res && fv_write_u32((uint32_t)u);
404 39 return res;
405 }
406
407 2 static bool i_f_double(double d) {
408 2 bool res = true;
409
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
2 res = res && fv_write_u8(S_DOUBLE_VALUE);
410 uint64_t u;
411 2 memcpy(&u, &d, sizeof(uint64_t));
412
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
2 res = res && fv_write_u64(u);
413 2 return res;
414 }
415
416 5 static bool i_f_i64(int64_t w) {
417 5 bool res = true;
418
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 5 times.
✗ Branch 4 not taken.
5 res = res && fv_write_u8(S_I64_VALUE);
419
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 5 times.
✗ Branch 4 not taken.
5 res = res && fv_write_u64((uint64_t)w);
420 5 return res;
421 }
422
423 2 static bool i_f_u64(uint64_t w) {
424 2 bool res = true;
425
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
2 res = res && fv_write_u8(S_U64_VALUE);
426
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
2 res = res && fv_write_u64(w);
427 2 return res;
428 }
429
430 // num_bytes is specifically an uint32_t
431 173 static bool i_f_lbm_array(uint32_t num_bytes, uint8_t *data) {
432 173 bool res = fv_write_u8(S_LBM_ARRAY);
433
2/4
✓ Branch 0 taken 173 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 173 times.
✗ Branch 4 not taken.
173 res = res && fv_write_u32(num_bytes);
434
1/2
✓ Branch 0 taken 173 times.
✗ Branch 1 not taken.
173 if (res) {
435
2/2
✓ Branch 0 taken 1411 times.
✓ Branch 1 taken 173 times.
1584 for (uint32_t i = 0; i < num_bytes; i ++ ) {
436
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1411 times.
1411 if (!fv_write_u8(data[i])) return false;
437 }
438 }
439 173 return res;
440 }
441
442
443
444 // ////////////////////////////////////////////////////////////
445 //
446
447 620 char *lbm_image_get_version(void) {
448
2/2
✓ Branch 0 taken 238 times.
✓ Branch 1 taken 382 times.
620 if (image_version) {
449 238 return image_version;
450 } else {
451 382 int32_t pos = (int32_t)image_size-2; // fixed position version string.
452 382 uint32_t val = read_u32(pos); pos --;
453
1/2
✓ Branch 0 taken 382 times.
✗ Branch 1 not taken.
382 if (val == VERSION_ENTRY) {
454 382 int32_t size = (int32_t)read_u32(pos);
455 382 image_version = (char*)(image_address + (pos - size));
456 382 return image_version;
457 }
458 }
459 ✗ return NULL;
460 }
461
462 // ////////////////////////////////////////////////////////////
463 // Constant heaps as part of an image.
464
465 lbm_const_heap_t image_const_heap;
466 lbm_uint image_const_heap_start_ix = 0;
467
468 // If an image is "present"
469 // but the const_heap_index does not point to a free location
470 // in flash, something is wrong!
471 ✗ lbm_uint lbm_image_const_heap_index(void) {
472 ✗ return image_const_heap.next;
473 }
474
475 16535 static bool image_const_heap_write(lbm_uint w, lbm_uint ix) {
476 #ifdef LBM64
477 4872 int32_t i = (int32_t)(image_const_heap_start_ix + (ix * 2));
478 4872 uint32_t *words = (uint32_t*)&w;
479 4872 bool r = image_write(words[0], i, false);
480
2/4
✓ Branch 0 taken 4872 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 4872 times.
✗ Branch 4 not taken.
4872 r = r && image_write(words[1], i + 1, false);
481 4872 return r;
482 #else
483 11663 int32_t i = (int32_t)(image_const_heap_start_ix + ix);
484 11663 return write_u32(w, &i, false);
485 #endif
486 }
487
488 // ////////////////////////////////////////////////////////////
489 // Image manipulation
490
491 455002 lbm_uint *lbm_image_add_symbol(char *name, lbm_uint id, lbm_uint symlist) {
492 // 64 bit | 32 bit
493 // image[i] = SYMBOL_ENTRY | image[i] = SYMBOL_ENTRY
494 // image[i-1] = symlist_ptr_high_word | image[i-1] = symlist_ptr
495 // image[i-2] = symlist_ptr_low_word | image[i-2] = id
496 // image[i-3] = id_high_word | image[i-3] = name_ptr
497 // image[i-4] = id_low_word
498 // image[i-5] = name_ptr_high_word
499 // image[i-6] = name_ptr_low_word
500 455002 bool r = write_u32(SYMBOL_ENTRY, &write_index,DOWNWARDS);
501
2/4
✓ Branch 0 taken 455002 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 455002 times.
✗ Branch 4 not taken.
455002 r = r && write_lbm_uint(symlist, &write_index, DOWNWARDS);
502
2/4
✓ Branch 0 taken 455002 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 455002 times.
✗ Branch 4 not taken.
455002 r = r && write_lbm_uint(id, &write_index, DOWNWARDS);
503
2/4
✓ Branch 0 taken 455002 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 455002 times.
✗ Branch 4 not taken.
455002 r = r && write_lbm_uint((lbm_uint)name, &write_index, DOWNWARDS);
504 455002 lbm_uint entry_ptr = (lbm_uint)(image_address + write_index + 1);
505
1/2
✓ Branch 0 taken 455002 times.
✗ Branch 1 not taken.
455002 if (r)
506 455002 return (lbm_uint*)entry_ptr;
507 ✗ return NULL;
508 }
509
510 // Writes size in words followed by the string
511 // todo sensible error detection.
512 521493 bool write_string_upwards(const char *str, size_t len) {
513 521493 uint32_t bytes = (uint32_t)len + 1;
514
2/2
✓ Branch 0 taken 68226 times.
✓ Branch 1 taken 453267 times.
521493 uint32_t words = (bytes % 4 == 0) ? bytes / 4 : (bytes / 4) + 1;
515 521493 write_u32(words, &write_index, DOWNWARDS);
516 521493 uint32_t w = 0;
517 521493 char *buf = (char*)&w;
518 521493 uint32_t i = 0;
519 521493 int32_t ix = write_index - (int32_t)(words -1);
520 521493 int wi = 0;
521
2/2
✓ Branch 0 taken 2435302 times.
✓ Branch 1 taken 521493 times.
2956795 while (i < bytes) {
522
2/2
✓ Branch 0 taken 521493 times.
✓ Branch 1 taken 1913809 times.
2435302 if (wi == 0 ) {
523 521493 w = 0;
524 }
525
2/2
✓ Branch 0 taken 291403 times.
✓ Branch 1 taken 2143899 times.
2435302 if (wi == 4) wi = 0;
526 2435302 buf[wi] = str[i];
527
2/2
✓ Branch 0 taken 359629 times.
✓ Branch 1 taken 2075673 times.
2435302 if (wi == 3) {
528 359629 write_u32(w, &ix, UPWARDS);
529 359629 w = 0;
530 }
531 2435302 i ++;
532 2435302 wi ++;
533 }
534
1/2
✓ Branch 0 taken 521493 times.
✗ Branch 1 not taken.
521493 if (wi != 0) {
535 521493 write_u32(w, &ix, UPWARDS);
536 }
537 521493 write_index -= (int32_t)words;
538 521493 return true;
539 }
540
541 // return the address the string
542 454987 char *lbm_image_add_symbol_name(const char *name, size_t len) {
543
1/2
✓ Branch 0 taken 454987 times.
✗ Branch 1 not taken.
454987 if (len > 0) {
544 454987 bool r = write_u32(SYMBOL_NAME_ENTRY, &write_index, DOWNWARDS);
545
2/4
✓ Branch 0 taken 454987 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 454987 times.
✗ Branch 4 not taken.
454987 r = r && write_string_upwards(name, len);
546
1/2
✓ Branch 0 taken 454987 times.
✗ Branch 1 not taken.
454987 if (r)
547 454987 return (char*)(image_address + write_index + 1);
548 }
549 ✗ return NULL;
550 }
551
552 // The symbol id is written to the link address upon image-boot
553 1339118 lbm_uint *lbm_image_add_and_link_symbol(char *name, lbm_uint id, lbm_uint symlist, lbm_uint *link) {
554 // 64 bit | 32 bit
555 // image[i] = SYMBOL_ENTRY | image[i] = SYMBOL_ENTRY
556 // image[i-1] = link_ptr_high | image[i-1] link_ptr
557 // image[i-2] = link_ptr_low | image[i-2] = symlist_ptr
558 // image[i-3] = symlist_ptr_high_word | image[i-3] = id
559 // image[i-4] = symlist_ptr_low_word | image[i-4] = name_ptr
560 // image[i-5] = id_high_word
561 // image[i-6] = id_low_word
562 // image[i-7] = name_ptr_high_word
563 // image[i-8] = name_ptr_low_word
564 1339118 bool r = write_u32(SYMBOL_LINK_ENTRY, &write_index,DOWNWARDS);
565
2/4
✓ Branch 0 taken 1339118 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 1339118 times.
✗ Branch 4 not taken.
1339118 r = r && write_lbm_uint((lbm_uint)link, &write_index, DOWNWARDS);
566
2/4
✓ Branch 0 taken 1339118 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 1339118 times.
✗ Branch 4 not taken.
1339118 r = r && write_lbm_uint(symlist, &write_index, DOWNWARDS);
567
2/4
✓ Branch 0 taken 1339118 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 1339118 times.
✗ Branch 4 not taken.
1339118 r = r && write_lbm_uint(id, &write_index, DOWNWARDS);
568
2/4
✓ Branch 0 taken 1339118 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 1339118 times.
✗ Branch 4 not taken.
1339118 r = r && write_lbm_uint((lbm_uint)name, &write_index, DOWNWARDS);
569 1339118 lbm_uint entry_ptr = (lbm_uint)(image_address + write_index + 1);
570
1/2
✓ Branch 0 taken 1339118 times.
✗ Branch 1 not taken.
1339118 if (r)
571 1339118 return (lbm_uint*)entry_ptr;
572 ✗ return NULL;
573 }
574
575 // ////////////////////////////////////////////////////////////
576 // Construction site
577
578 // Sharing is detected and annotated by:
579 // 1. Generate an array of addresses of shared structures. (sharing table)
580 // Sharing table will contain a boolean field where "having been flattened"
581 // status is tracked.
582 // 2. Flatten values and for each ptr-cell, check if it's address is in the
583 // sharing table. If the cell is in the sharing table and the boolean
584 // flag is not set: Set the flag and flatten the value with a shared tag
585 // is set: Do not flatten value, create a REF tag.
586
587 // Note: the boolean field may not be needed, it may be possible to recreate that
588 // information on the fly during flattening using the GC bit.
589 // But since all complete traversals require a GC this change just moves the
590 // bookkeeping cost forward (first to the size phase).
591
592 // The sharing table could be a temporary list on the LBM heap
593 // if only it wasn't for GC. GC cannot be run while doing pointer
594 // reversal traversals.
595 // Another option would be to allocate an area in LBM mem and fill that
596 // that with temporary sharing data. Only problem is that we do not know how much
597 // temporary data is needed until after doing at least one traversal where we
598 // also need to accumulate shared addresses in order to not count them doubly.
599 // ** allocating lbm_memory_longest_free amount of temp data at flattening
600 // could be a good solution. But even then the final number of shared
601 // nodes could be written to the image so that the unflattener does not need
602 // guess and allocate in chunks.
603 // - Very little lbm mem could be available here.
604
605 // Sharing is restored by:
606 // 1. Allocate a new column for the sharing table in LBM mem, the size is now known.
607 // 2. Unflatten flat values:
608 // if a value has a shared tag, fill in the address it is unflattened to into the
609 // new sharing table column.
610 // if a value has the ref tag, look it up in sharing table and read out the new address.
611 //
612 // The process is order dependent and shared tag for address 'a' must the unflattened
613 // before a ref tag for the same address 'a'.
614
615 // Tricky: The traversals for size computation and flattening has to be
616 // made aware of sharing in some way. The traversal always must fully traverse
617 // a value in order to not just partially mark it (it will then be destroyed by GC).
618 // Options:
619 // 1: run GC after a complete traversal of all values in env for size and flattening.
620 // This means that all the sizes must be stored temporarily...
621 // 2: Give a sharing table to the traversal and have the traversal switch to gc_mark_phace
622 // at each shared node to bookkeep it and keep it safe from upcomming gc.
623
624 //
625 // NOTE about sharing detection: It should be possible to traverse the
626 // heap structures using the same explicit stack algorithm as
627 // lbm_gc_mark_phase uses. Because if a structure is too large
628 // to be GC'd it cannot be used anyway, so no point in
629 // serialising/deserialising it.
630 //
631 // There are probably pros/cons to both approaches (ptr-rev vs
632 // explicit stack).
633 // ptr-rev -> correct
634 // explicit stack -> fast, simple but sensitive to programming style and limits
635 // programmer.
636 //
637 // For full benefit of pointer reversal the -DLBM_USE_GC_PTR_REV
638 // build flag is needed.
639
640
641 #ifdef LBM64
642 #define SHARING_TABLE_ENTRY_SIZE (2 + 1 + 1)
643 #else
644 #define SHARING_TABLE_ENTRY_SIZE (1 + 1 + 1)
645 #endif
646
647 #define SHARING_TABLE_TRUE 0xDEADBEEFu
648 #define SHARING_TABLE_FALSE 0xDEADBEEFu
649
650 18856 int32_t index_sharing_table(sharing_table *st, int32_t i) {
651
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18856 times.
18856 if (i < 0) return i; // maybe check if more than num?
652 18856 return st->start - 2 - (i * SHARING_TABLE_ENTRY_SIZE);
653 }
654
655 // Search sharing table, O(N) where N shared nodes
656 32259 int32_t sharing_table_contains(sharing_table *st, lbm_uint addr) {
657 32259 int32_t num = st->num;
658 32259 uint32_t st_tag = read_u32(st->start);
659
1/2
✓ Branch 0 taken 32259 times.
✗ Branch 1 not taken.
32259 if (st_tag == SHARING_TABLE) {
660 // sharing table tag exists but not the num field.
661
2/2
✓ Branch 0 taken 18548 times.
✓ Branch 1 taken 31901 times.
50449 for (int32_t i = 0; i < num; i ++ ) {
662 lbm_uint a;
663 18548 int32_t ix = index_sharing_table(st, i);
664 #ifdef LBM64
665 ✗ a = read_u64(ix);
666 #else
667 18548 a = read_u32(ix);
668 #endif
669
2/2
✓ Branch 0 taken 358 times.
✓ Branch 1 taken 18190 times.
18548 if (addr == a) {
670 358 return i;
671 }
672 }
673 }
674 31901 return -1;
675 }
676
677 #define SHARING_TABLE_SIZED_FIELD 0
678 #define SHARING_TABLE_FLATTENED_FIELD 1
679
680 86 bool sharing_table_set_field(sharing_table *st, int32_t ix, int32_t field, uint32_t value) {
681 int32_t wix;
682 #ifdef LBM64
683 ✗ wix = index_sharing_table(st, ix) - 2 - field;
684 #else
685 86 wix = index_sharing_table(st, ix) - 1 - field;
686 #endif
687 86 return write_u32(value, &wix, DOWNWARDS); // Dir irrelevant
688 }
689
690 222 uint32_t sharing_table_get_field(sharing_table *st, int32_t ix, int32_t field) {
691 int32_t wix;
692 #ifdef LBM64
693 ✗ wix = index_sharing_table(st, ix) - 2 - field;
694 #else
695 222 wix = index_sharing_table(st, ix) - 1 - field;
696 #endif
697 222 return read_u32(wix);
698 }
699
700 16040 static int detect_shared(lbm_value v, bool shared, void *acc) {
701 16040 sharing_table *st = (sharing_table*)acc;
702
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 15972 times.
16040 if (shared) {
703
1/2
✓ Branch 1 taken 68 times.
✗ Branch 2 not taken.
68 if (lbm_is_ptr(v)) {
704 68 lbm_uint addr = v;
705 68 int32_t ix = sharing_table_contains(st,addr);
706
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 25 times.
68 if (ix < 0) {
707 // Create place in table for a shared address and skip
708 // enough words for boolean fields.
709 #ifdef LBM64
710 ✗ write_u64(addr, &write_index, DOWNWARDS);
711 #else
712 43 write_u32(addr, &write_index, DOWNWARDS);
713 #endif
714 43 write_index -= 2; // skip 2 words for "sized" and "flattened" booleans.
715 43 st->num++;
716 }
717 }
718 }
719 16040 return TRAV_FUN_SUBTREE_PROCEED;
720 }
721
722 72 sharing_table lbm_image_sharing(void) {
723 72 lbm_value *env = lbm_get_global_env();
724
725 sharing_table st;
726 72 st.start = write_index;
727 72 st.num = 0;
728
729 72 write_u32(SHARING_TABLE, &write_index, DOWNWARDS);
730 72 write_index -= 1; // skip a word where size is to be written out of order.
731 // index is now correct for starting to write sharing table rows.
732
733
1/2
✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
72 if (env) {
734
2/2
✓ Branch 0 taken 2304 times.
✓ Branch 1 taken 72 times.
2376 for (int i = 0; i < GLOBAL_ENV_ROOTS; i ++) {
735 2304 lbm_value curr = env[i];
736
2/2
✓ Branch 1 taken 421 times.
✓ Branch 2 taken 2304 times.
2725 while(lbm_is_cons(curr)) {
737 // lbm_value name_field = lbm_caar(curr);
738 421 lbm_value val_field = lbm_cdr(lbm_car(curr));
739
2/2
✓ Branch 1 taken 313 times.
✓ Branch 2 taken 108 times.
421 if (!lbm_is_constant(val_field)) {
740 313 lbm_ptr_rev_trav(detect_shared, val_field, &st);
741 }
742 421 curr = lbm_cdr(curr);
743 }
744 }
745 // clean out all mark-bits
746 72 lbm_perform_gc();
747 }
748 // Write the number of shared nodes, 0 or more, to table entry.
749 72 int32_t wix = st.start - 1;
750 72 write_u32((uint32_t)st.num,&wix, DOWNWARDS);
751
752 72 return st;
753 }
754
755 // ////////////////////////////////////////////////////////////
756 //
757
758 typedef struct {
759 int32_t s;
760 sharing_table *st;
761 } size_accumulator;
762
763 16040 static int size_acc(lbm_value v, bool shared, void *acc) {
764 (void) shared;
765 16040 size_accumulator *sa = (size_accumulator*)acc;
766
767 16040 int32_t ix = sharing_table_contains(sa->st, v);
768
769
2/2
✓ Branch 0 taken 111 times.
✓ Branch 1 taken 15929 times.
16040 if (ix >= 0) {
770
2/2
✓ Branch 1 taken 68 times.
✓ Branch 2 taken 43 times.
111 if (SHARING_TABLE_TRUE == sharing_table_get_field(sa->st, ix, SHARING_TABLE_SIZED_FIELD)) {
771 // shared node has been sized already and should return size of a ref node
772 // sizeof S_REF and addr
773 #ifdef LBM64
774 ✗ sa->s += 9;
775 #else
776 68 sa->s += 5;
777 #endif
778 68 return TRAV_FUN_SUBTREE_DONE;
779 } else {
780 // setting the sized field to not include the size in future occurrances.
781 43 sharing_table_set_field(sa->st, ix, SHARING_TABLE_SIZED_FIELD, SHARING_TABLE_TRUE);
782 #ifdef LBM64
783 ✗ sa->s += 9;
784 #else
785 43 sa->s += 5;
786 #endif
787 }
788 }
789
790 15972 lbm_uint t = lbm_type_of(v);
791
792
3/4
✓ Branch 0 taken 8031 times.
✓ Branch 1 taken 7941 times.
✓ Branch 2 taken 8031 times.
✗ Branch 3 not taken.
15972 if (t >= LBM_POINTER_TYPE_FIRST && t < LBM_POINTER_TYPE_LAST) {
793 8031 t = t & ~(LBM_PTR_TO_CONSTANT_BIT);
794 }
795
796
4/4
✓ Branch 1 taken 8031 times.
✓ Branch 2 taken 7941 times.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 8030 times.
15972 if (lbm_is_ptr(v) && (v & LBM_PTR_TO_CONSTANT_BIT)) {
797 1 sa->s += (int32_t)sizeof(lbm_uint) + 1;
798 1 return TRAV_FUN_SUBTREE_DONE;
799 }
800
801
13/14
✓ Branch 0 taken 7782 times.
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 22 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 3139 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 4776 times.
✓ Branch 12 taken 173 times.
✗ Branch 13 not taken.
15971 switch (t) {
802 7782 case LBM_TYPE_CONS:
803 7782 sa->s += 1;
804 7782 break;
805 20 case LBM_TYPE_LISPARRAY:
806 20 sa->s += 4 + 1;
807 20 break;
808 22 case LBM_TYPE_BYTE:
809 22 sa->s += 2;
810 22 break;
811 4 case LBM_TYPE_U:
812 4 sa->s += (int32_t)sizeof(lbm_uint) + 1;
813 4 break;
814 3139 case LBM_TYPE_I:
815 3139 sa->s += (int32_t)sizeof(lbm_uint) + 1;
816 3139 break;
817 2 case LBM_TYPE_U32:
818 2 sa->s += 4 + 1;
819 2 break;
820 5 case LBM_TYPE_I32:
821 5 sa->s += 4 + 1;
822 5 break;
823 2 case LBM_TYPE_U64:
824 2 sa->s += 8 + 1;
825 2 break;
826 5 case LBM_TYPE_I64:
827 5 sa->s += 8 + 1;
828 5 break;
829 39 case LBM_TYPE_FLOAT:
830 39 sa->s += 4 + 1;
831 39 break;
832 2 case LBM_TYPE_DOUBLE:
833 2 sa->s += 8 + 1;
834 2 break;
835 4776 case LBM_TYPE_SYMBOL:
836 4776 sa->s += (int32_t)sizeof(lbm_uint) + 1;
837 4776 break;
838 173 case LBM_TYPE_ARRAY: {
839 173 lbm_int arr_size = lbm_heap_array_get_size(v);
840 173 const uint8_t *d = lbm_heap_array_get_data_ro(v);
841
2/4
✓ Branch 0 taken 173 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 173 times.
✗ Branch 3 not taken.
173 if (arr_size > 0 && d != NULL) {
842 173 sa->s += (int32_t)(1 + 4 + arr_size);
843 }
844 173 }break;
845 }
846 15971 return TRAV_FUN_SUBTREE_CONTINUE;
847 }
848
849 typedef struct {
850 bool res;
851 sharing_table *st;
852 int arg;
853 } flatten_node_meta_data;
854
855 16040 static int flatten_node(lbm_value v, bool shared, void *arg) {
856 (void) shared;
857 16040 flatten_node_meta_data *md = (flatten_node_meta_data*)arg;
858 16040 bool *acc = &md->res;
859
860 16040 int32_t ix = sharing_table_contains(md->st, v);
861
862
2/2
✓ Branch 0 taken 111 times.
✓ Branch 1 taken 15929 times.
16040 if (ix >= 0) {
863
2/2
✓ Branch 1 taken 68 times.
✓ Branch 2 taken 43 times.
111 if (SHARING_TABLE_TRUE == sharing_table_get_field(md->st, ix, SHARING_TABLE_FLATTENED_FIELD)) {
864 // Shared node already flattened.
865 #if DEBUG
866 printf("FLATTEN: Writing S_REF for target_map[%d] (addr %x)\n", ix, (unsigned int)v);
867 #endif
868 68 fv_write_u8(S_REF);
869 #ifdef LBM64
870 ✗ fv_write_u64((lbm_uint)v);
871 #else
872 68 fv_write_u32((lbm_uint)v);
873 #endif
874 68 return TRAV_FUN_SUBTREE_DONE;
875 } else {
876 // Shared node not yet flattened.
877 #if DEBUG
878 printf("FLATTEN: Writing S_SHARED for target_map[%d] (addr %x)\n", ix, (unsigned int)v);
879 #endif
880 43 sharing_table_set_field(md->st, ix, SHARING_TABLE_FLATTENED_FIELD, SHARING_TABLE_TRUE);
881 43 fv_write_u8(S_SHARED);
882 #ifdef LBM64
883 ✗ fv_write_u64((lbm_uint)v);
884 #else
885 43 fv_write_u32((lbm_uint)v);
886 #endif
887 // Continue flattening along this subtree.
888 }
889 }
890
891 15972 lbm_uint t = lbm_type_of(v);
892
893
3/4
✓ Branch 0 taken 8031 times.
✓ Branch 1 taken 7941 times.
✓ Branch 2 taken 8031 times.
✗ Branch 3 not taken.
15972 if (t >= LBM_POINTER_TYPE_FIRST && t < LBM_POINTER_TYPE_LAST) {
894 8031 t = t & ~(LBM_PTR_TO_CONSTANT_BIT);
895 }
896
897
4/4
✓ Branch 1 taken 8031 times.
✓ Branch 2 taken 7941 times.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 8030 times.
15972 if (lbm_is_ptr(v) && (v & LBM_PTR_TO_CONSTANT_BIT)) {
898
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 3 taken 1 time.
✗ Branch 4 not taken.
1 *acc = *acc && fv_write_u8(S_CONSTANT_REF);
899 #ifdef LBM64
900 ✗ *acc = *acc && fv_write_u64((lbm_uint)v);
901 #else
902
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 3 taken 1 time.
✗ Branch 4 not taken.
1 *acc = *acc && fv_write_u32((lbm_uint)v);
903 #endif
904 1 return TRAV_FUN_SUBTREE_DONE;
905 }
906
907
13/14
✓ Branch 0 taken 7782 times.
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 22 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 3139 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 4776 times.
✓ Branch 12 taken 173 times.
✗ Branch 13 not taken.
15971 switch (t) {
908 7782 case LBM_TYPE_CONS:
909
2/4
✓ Branch 0 taken 7782 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 7782 times.
✗ Branch 4 not taken.
7782 *acc = *acc && i_f_cons();
910 7782 break;
911 20 case LBM_TYPE_LISPARRAY: {
912 20 lbm_array_header_t *header = (lbm_array_header_t*)lbm_car(v);
913
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 if (header) {
914 20 uint32_t size = (uint32_t)(header->size / sizeof(lbm_value));
915
2/4
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 20 times.
✗ Branch 4 not taken.
20 *acc = *acc && i_f_lisp_array(size);
916 } else {
917 // hmm
918 }
919 20 } break;
920 22 case LBM_TYPE_BYTE:
921
2/4
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
✓ Branch 4 taken 22 times.
✗ Branch 5 not taken.
22 *acc = *acc && i_f_b((uint8_t)lbm_dec_as_char(v));
922 22 break;
923 4 case LBM_TYPE_U:
924
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
4 *acc = *acc && i_f_u(lbm_dec_u(v));
925 4 break;
926 3139 case LBM_TYPE_I:
927
2/4
✓ Branch 0 taken 3139 times.
✗ Branch 1 not taken.
✓ Branch 4 taken 3139 times.
✗ Branch 5 not taken.
3139 *acc = *acc && i_f_i(lbm_dec_i(v));
928 3139 break;
929 2 case LBM_TYPE_U32:
930
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
2 *acc = *acc && i_f_u32(lbm_dec_as_u32(v));
931 2 break;
932 5 case LBM_TYPE_I32:
933
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
5 *acc = *acc && i_f_i32(lbm_dec_as_i32(v));
934 5 break;
935 2 case LBM_TYPE_U64:
936
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
2 *acc = *acc && i_f_u64(lbm_dec_as_u64(v));
937 2 break;
938 5 case LBM_TYPE_I64:
939
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
5 *acc = *acc && i_f_i64(lbm_dec_as_i64(v));
940 5 break;
941 39 case LBM_TYPE_FLOAT:
942
2/4
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
✓ Branch 4 taken 39 times.
✗ Branch 5 not taken.
39 *acc = *acc && i_f_float(lbm_dec_as_float(v));
943 39 break;
944 2 case LBM_TYPE_DOUBLE:
945
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
2 *acc = *acc && i_f_double(lbm_dec_as_double(v));
946 2 break;
947 4776 case LBM_TYPE_SYMBOL:
948
2/4
✓ Branch 0 taken 4776 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 4776 times.
✗ Branch 4 not taken.
4776 *acc = *acc && i_f_sym(v);
949 4776 break;
950 173 case LBM_TYPE_ARRAY: {
951 173 lbm_int s = lbm_heap_array_get_size(v);
952 173 const uint8_t *d = lbm_heap_array_get_data_ro(v);
953
2/4
✓ Branch 0 taken 173 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 173 times.
✗ Branch 3 not taken.
173 if (s > 0 && d != NULL) {
954
2/4
✓ Branch 0 taken 173 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 173 times.
✗ Branch 4 not taken.
173 *acc = *acc && i_f_lbm_array((uint32_t)s, (uint8_t*)d);
955 }
956 173 }break;
957 ✗ default:
958 ✗ break;
959 }
960 15971 return TRAV_FUN_SUBTREE_CONTINUE;
961 }
962
963 // Performing GC after using the ptr_rev_trav to restore the
964 // GC-bit in the value traversed.
965 313 static int32_t image_flatten_size(sharing_table *st, lbm_value v) {
966 size_accumulator sa;
967 313 sa.s = 0;
968 313 sa.st = st;
969 313 lbm_ptr_rev_trav(size_acc, v, &sa);
970 313 lbm_perform_gc();
971 313 return sa.s; // Should always be "ok" now.
972 }
973
974 313 static bool image_flatten_value(sharing_table *st, lbm_value v) {
975 flatten_node_meta_data md;
976 313 md.res = true;
977 313 md.st = st;
978 313 md.arg = 0;
979 313 lbm_ptr_rev_trav(flatten_node, v, &md);
980 313 lbm_perform_gc();
981 313 return md.res; // ok = enough space in image for flat val.
982 }
983
984 // ////////////////////////////////////////////////////////////
985 // print sharing table
986 #if DEBUG
987 void print_sharing_table(sharing_table *st) {
988 int32_t pos = st->start;
989 int32_t num = st->num;
990 char buf[256];
991
992 for (int i = 0; i < num; i ++) {
993 int32_t ix = index_sharing_table(st, i);
994 lbm_uint a = read_u32(ix); // address
995
996 lbm_print_value(buf, 256, a);
997 printf("%d\t%x\t%s\n",i, a, buf);
998 }
999
1000
1001 }
1002 #endif
1003
1004 // ////////////////////////////////////////////////////////////
1005 //
1006 72 bool lbm_image_save_global_env(void) {
1007
1008 72 sharing_table st = lbm_image_sharing();
1009 72 lbm_value *env = lbm_get_global_env();
1010
1/2
✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
72 if (env) {
1011
2/2
✓ Branch 0 taken 2304 times.
✓ Branch 1 taken 72 times.
2376 for (int i = 0; i < GLOBAL_ENV_ROOTS; i ++) {
1012 2304 lbm_value curr = env[i];
1013
2/2
✓ Branch 1 taken 421 times.
✓ Branch 2 taken 2304 times.
2725 while(lbm_is_cons(curr)) {
1014 421 lbm_value name_field = lbm_caar(curr);
1015 421 lbm_value val_field = lbm_cdr(lbm_car(curr));
1016
1017
2/2
✓ Branch 1 taken 108 times.
✓ Branch 2 taken 313 times.
421 if (lbm_is_constant(val_field)) {
1018 108 write_u32(BINDING_CONST, &write_index, DOWNWARDS);
1019 108 write_lbm_value(name_field, &write_index, DOWNWARDS);
1020 108 write_lbm_value(val_field, &write_index, DOWNWARDS);
1021 } else {
1022 313 int fv_size = image_flatten_size(&st, val_field);
1023
1/2
✓ Branch 0 taken 313 times.
✗ Branch 1 not taken.
313 if (fv_size > 0) {
1024
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 285 times.
313 fv_size = (fv_size % 4 == 0) ? (fv_size / 4) : (fv_size / 4) + 1; // num 32bit words
1025
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 313 times.
313 if ((write_index - fv_size) <= (int32_t)image_const_heap.next) {
1026 ✗ return false;
1027 }
1028 313 write_u32(BINDING_FLAT, &write_index, DOWNWARDS);
1029 313 write_u32((uint32_t)fv_size , &write_index, DOWNWARDS);
1030 313 write_lbm_value(name_field, &write_index, DOWNWARDS);
1031 313 write_index = write_index - fv_size; // subtract fv_size
1032 #if DEBUG
1033 int32_t data_start = write_index; // Save the start position
1034 #endif
1035
1/2
✓ Branch 1 taken 313 times.
✗ Branch 2 not taken.
313 if (image_flatten_value(&st, val_field)) { // adds fv_size back
1036 313 fv_write_flush();
1037 #if DEBUG
1038 printf("Flattenining address: %x\n", val_field);
1039 for (int i = 0; i < fv_size; i ++) {
1040 uint32_t v = read_u32(data_start + i);
1041 uint8_t *p = &v;
1042 for (int j = 0; j < 4; j ++) {
1043 printf("%x ", p[j]);
1044 }
1045 printf(" ");
1046 }
1047 printf("\n");
1048 #endif
1049
1050 // TODO: What error handling makes sense?
1051 }
1052 313 write_index = write_index - fv_size - 1; // subtract fv_size
1053 } else {
1054 ✗ return false;
1055 }
1056 }
1057 421 curr = lbm_cdr(curr);
1058 }
1059 }
1060 #if DEBUG
1061 printf("Sharing table:\n");
1062 print_sharing_table(&st);
1063 #endif
1064 72 return true;
1065 }
1066 ✗ return false;
1067 }
1068
1069 // The extension table is created at system startup.
1070 // Extensions can also be added dynamically.
1071 // Dynamically added extensions have names starting with "ext-"
1072 // and their names are placed in RAM by the reader.
1073 //
1074 // Symbol_id -> index in extension table mapping
1075 // is created as extensions are added.
1076 // dynamic extensions are added after "built-in" extensions
1077 // and have higher indices.
1078
1079 72 bool lbm_image_save_extensions(void) {
1080 72 bool r = true;
1081 72 lbm_uint num = lbm_get_num_extensions();
1082
1/2
✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
72 if (num > 0) {
1083
2/4
✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 72 times.
✗ Branch 4 not taken.
72 r = r && write_u32(EXTENSION_TABLE, &write_index, DOWNWARDS);
1084
2/4
✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 72 times.
✗ Branch 4 not taken.
72 r = r && write_u32((uint32_t)num , &write_index, DOWNWARDS);
1085
2/2
✓ Branch 0 taken 14400 times.
✓ Branch 1 taken 72 times.
14472 for (lbm_uint i = 0; i < num; i ++) {
1086
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14400 times.
14400 if (!r) return r;
1087
1088 14400 const char *name_ptr = extension_table[i].name;
1089 lbm_uint addr;
1090 // when PIC, name pointers may move around
1091 // between restarts. It is also the case that
1092 // the FPTRs will move around as well.
1093 // This makes dynamic extensions useless on Linux.
1094 // Static extensions are fine as they will be re-added after image-boot
1095 // and faulty FPTRs will be replaced.
1096 //#ifdef __PIC__
1097 //r = store_symbol_name_flash(name_ptr, &addr);
1098 //if (!r) return r;
1099 //name_ptr = (const char *)addr;
1100 //#else
1101
1102 // I dont think this code makes sense anymore given that
1103 // all symbols are always in flash. So if you have a symbol name
1104 // lbm_memory_ptr_inside(name) will never be true.
1105
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14400 times.
14400 if (lbm_memory_ptr_inside((lbm_uint *)name_ptr)) {
1106 ✗ addr = (lbm_uint)lbm_image_add_symbol_name(name_ptr, strlen(name_ptr));
1107 ✗ if (!addr) return false;
1108 ✗ name_ptr = (const char *)addr;
1109 }
1110 //#endif
1111 #ifdef LBM64
1112 ✗ r = r && write_u64((uint64_t)name_ptr, &write_index, DOWNWARDS);
1113 ✗ r = r && write_u64((uint64_t)extension_table[i].fptr, &write_index, DOWNWARDS);
1114 #else
1115
2/4
✓ Branch 0 taken 14400 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 14400 times.
✗ Branch 4 not taken.
14400 r = r && write_u32((uint32_t)name_ptr, &write_index, DOWNWARDS);
1116
2/4
✓ Branch 0 taken 14400 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 14400 times.
✗ Branch 4 not taken.
14400 r = r && write_u32((uint32_t)extension_table[i].fptr, &write_index, DOWNWARDS);
1117 #endif
1118 } }
1119 72 return true;
1120 }
1121
1122 static uint32_t last_const_heap_ix = 0;
1123
1124 72 bool lbm_image_save_constant_heap_ix(void) {
1125 72 bool r = true; // saved or no need to save it.
1126
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 59 times.
72 if (image_const_heap.next != last_const_heap_ix) {
1127 13 last_const_heap_ix = (uint32_t)image_const_heap.next;
1128 13 r = write_u32(CONSTANT_HEAP_IX, &write_index, DOWNWARDS);
1129
2/4
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 13 times.
✗ Branch 4 not taken.
13 r = r && write_u32((uint32_t)image_const_heap.next, &write_index, DOWNWARDS);
1130 }
1131 72 return r;
1132 }
1133
1134 382 bool lbm_image_exists(void) {
1135 382 uint32_t val = read_u32((int32_t)image_size - 1);
1136 382 return val == IMAGE_INITIALIZED;
1137 }
1138
1139 66578 void lbm_image_init(uint32_t* image_mem_address,
1140 uint32_t image_size_words,
1141 lbm_image_write_fun image_write_fun) {
1142 66578 image_write = image_write_fun;
1143 66578 image_address = image_mem_address;
1144 66578 image_size = image_size_words;
1145 66578 write_index = (int32_t)image_size_words -1;
1146 66578 image_has_extensions = false;
1147 66578 image_version = NULL;
1148 66578 last_const_heap_ix = 0;
1149 66578 }
1150
1151 66506 void lbm_image_create(char *version_str) {
1152 66506 write_u32(IMAGE_INITIALIZED, &write_index, DOWNWARDS);
1153
1/2
✓ Branch 0 taken 66506 times.
✗ Branch 1 not taken.
66506 if (version_str) {
1154 66506 write_u32(VERSION_ENTRY, &write_index, DOWNWARDS);
1155 66506 write_string_upwards(version_str, strlen(version_str));
1156 }
1157 66506 }
1158
1159
1160 66578 bool lbm_image_boot(void) {
1161 //process image
1162 66578 int32_t pos = (int32_t)image_size-1;
1163 66578 last_const_heap_ix = 0;
1164
1165 sharing_table st;
1166 66578 lbm_uint *target_map = NULL; // Target addresses for shared/refs from the flat values.
1167
1168
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 206524 times.
✓ Branch 2 taken 206524 times.
✗ Branch 3 not taken.
206524 while (pos >= 0 && pos > (int32_t)last_const_heap_ix) {
1169 206524 uint32_t val = read_u32(pos);
1170 206524 pos --;
1171
11/11
✓ Branch 0 taken 66578 times.
✓ Branch 1 taken 66578 times.
✓ Branch 2 taken 1198 times.
✓ Branch 3 taken 13 times.
✓ Branch 4 taken 108 times.
✓ Branch 5 taken 313 times.
✓ Branch 6 taken 1198 times.
✓ Branch 7 taken 3816 times.
✓ Branch 8 taken 72 times.
✓ Branch 9 taken 72 times.
✓ Branch 10 taken 66578 times.
206524 switch(val) {
1172 66578 case IMAGE_INITIALIZED: {
1173 66578 image_const_heap_start_ix = 0; // const heap starts at 0
1174 66578 lbm_const_heap_init(image_const_heap_write,
1175 &image_const_heap,
1176 (lbm_uint*)(image_address));
1177 // initialized is a one word field
1178 66578 } break;
1179 66578 case VERSION_ENTRY: {
1180 66578 uint32_t size = read_u32(pos); pos --;
1181 66578 image_version = (char*)(image_address + (pos - (int32_t)size + 1));
1182 66578 pos -= (int32_t)size;
1183 66578 } break;
1184 1198 case SYMBOL_NAME_ENTRY: {
1185 // Just jump accross the string.
1186 1198 uint32_t size = read_u32(pos); pos --;
1187 1198 pos -= (int32_t)size;
1188 1198 } break;
1189 13 case CONSTANT_HEAP_IX: {
1190 13 uint32_t next = read_u32(pos);
1191 13 pos --;
1192 13 last_const_heap_ix = next;
1193 13 image_const_heap.next = next;
1194 13 } break;
1195 108 case BINDING_CONST: {
1196 // on 64 bit | on 32 bit
1197 // pos -> key_high | pos -> key
1198 // pos - 1 -> key_low | pos - 1 -> val
1199 // pos - 2 -> val_high
1200 // pos - 3 -> val_low
1201 #ifdef LBM64
1202 ✗ lbm_uint bind_key = read_u64(pos-1);
1203 ✗ lbm_uint bind_val = read_u64(pos-3);
1204 ✗ pos -= 4;
1205 #else
1206 108 lbm_uint bind_key = read_u32(pos);
1207 108 lbm_uint bind_val = read_u32(pos-1);
1208 108 pos -= 2;
1209 #endif
1210 108 lbm_uint ix_key = lbm_dec_sym(bind_key) & GLOBAL_ENV_MASK;
1211 108 lbm_value *global_env = lbm_get_global_env();
1212 108 lbm_uint orig_env = global_env[ix_key];
1213 108 lbm_value new_env = lbm_env_set(orig_env,bind_key,bind_val);
1214
1215
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 108 times.
108 if (lbm_is_symbol(new_env)) {
1216 ✗ return false;
1217 }
1218 108 global_env[ix_key] = new_env;
1219 108 } break;
1220 313 case BINDING_FLAT: {
1221 // on 64 bit | on 32 bit
1222 // pos -> size | pos -> size
1223 // pos - 1 -> key_high | pos - 1 -> key
1224 // pos - 2 -> key_low
1225 //
1226 313 int32_t s = (int32_t)read_u32(pos);
1227 // size in 32 or 64 bit words.
1228 #ifdef LBM64
1229 ✗ lbm_uint bind_key = read_u64(pos-2);
1230 ✗ pos -= 3;
1231 #else
1232 313 lbm_uint bind_key = read_u32(pos-1);
1233 313 pos -= 2;
1234 #endif
1235
1236 313 pos -= s;
1237 lbm_flat_value_t fv;
1238 313 fv.buf = (uint8_t*)(image_address + pos);
1239 313 fv.buf_size = (uint32_t)s * sizeof(lbm_uint); // GEQ to actual buf
1240 313 fv.buf_pos = 0;
1241 lbm_value unflattened;
1242
2/2
✓ Branch 0 taken 103 times.
✓ Branch 1 taken 210 times.
313 if (target_map) {
1243
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 103 times.
103 if (!lbm_unflatten_value_sharing(&st, target_map, &fv, &unflattened)) {
1244 ✗ return false;
1245 }
1246 // When a value is unflattened it may contain shared subvalues
1247 // and references to shared values. A reference may point to either
1248 // values that are shared within the value that is currently unflattened
1249 // or to a value that has previously been unflattened.
1250 //
1251 // There is an ordering property that must be maintained that the node
1252 // with the S_SHARED tag is always processed before any corresponding S_REF tags.
1253 // This means that if a ref node is unflattened the target to point it to will
1254 // already exist.
1255 //
1256 // If GC needs to happen while unflattening a value, there is no danger of messing
1257 // up the addresses to point references to because:
1258 // 1. The S_SHARED node is local to the same value and will be recreated after GC
1259 // and the ref value in target map will be overwritten. Any local refs will be also
1260 // be recreated. Any refs to the S_Shared outside of this value, will be in values
1261 // processed in the future.
1262 // 2. S_SHARED nodes that have been created as part of prvious value are untouched
1263 // by running GC as they have already been unflattened and should be reachable
1264 // on the environment. Their mapping in the target map is still valid.
1265
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 103 times.
103 if (lbm_is_symbol_merror(unflattened)) {
1266 //memset(target_map, 0, st.num * sizeof(lbm_uint));
1267 ✗ lbm_perform_gc();
1268 ✗ lbm_unflatten_value_sharing(&st, target_map, &fv, &unflattened);
1269 }
1270 } else {
1271 210 lbm_unflatten_value(&fv, &unflattened);
1272
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 210 times.
210 if (lbm_is_symbol_merror(unflattened)) {
1273 ✗ lbm_perform_gc();
1274 ✗ lbm_unflatten_value(&fv, &unflattened);
1275 }
1276 }
1277 313 lbm_uint ix_key = lbm_dec_sym(bind_key) & GLOBAL_ENV_MASK;
1278 313 lbm_value *global_env = lbm_get_global_env();
1279 313 lbm_uint orig_env = global_env[ix_key];
1280 313 lbm_value new_env = lbm_env_set(orig_env,bind_key,unflattened);
1281
1282
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 313 times.
313 if (lbm_is_symbol(new_env)) {
1283 ✗ return false;
1284 }
1285 313 global_env[ix_key] = new_env;
1286 313 pos --;
1287 313 } break;
1288 1198 case SYMBOL_ENTRY: {
1289 // on 64 bit | on 32 bit
1290 // pos -> symlist_addr_high_word | pos -> symlist_ptr
1291 // pos - 1 -> symlist_addr_low_word | pos - 1 -> id
1292 // pos - 2 -> id_high_word | pos - 2 -> name_ptr
1293 // pos - 3 -> id_low_word |
1294 // pos - 4 -> name_ptr_high_word |
1295 // pos - 5 -> name_ptr_low_word |
1296 #ifdef LBM64
1297 ✗ int32_t entry_pos = pos - 5;
1298 ✗ lbm_uint *p = (lbm_uint*)(image_address + entry_pos);
1299 ✗ uint32_t sym_id = (uint32_t)(p[1]);
1300 ✗ lbm_uint next_id = lbm_symrepr_get_next_id();
1301 ✗ if (sym_id >= RUNTIME_SYMBOLS_START && sym_id >= next_id ) {
1302 ✗ lbm_symrepr_set_next_id(sym_id + 1);
1303 }
1304 ✗ lbm_symrepr_set_symlist((lbm_uint*)(image_address + entry_pos));
1305 ✗ pos -= 6;
1306 #else
1307 1198 int32_t entry_pos = pos - 2;
1308 1198 lbm_uint *p = (lbm_uint*)(image_address + entry_pos);
1309 1198 uint32_t sym_id = (uint32_t)(p[1]);
1310 1198 lbm_uint next_id = lbm_symrepr_get_next_id();
1311
2/4
✓ Branch 0 taken 1198 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1198 times.
✗ Branch 3 not taken.
1198 if (sym_id >= RUNTIME_SYMBOLS_START && sym_id >= next_id ) {
1312 1198 lbm_symrepr_set_next_id(sym_id + 1);
1313 }
1314 1198 lbm_symrepr_set_symlist((lbm_uint*)(image_address + entry_pos));
1315 1198 pos -= 3;
1316 #endif
1317 1198 } break;
1318 3816 case SYMBOL_LINK_ENTRY: {
1319 // on 64 bits | on 32 bit
1320 // pos -> link_ptr_high | pos -> link_ptr
1321 // pos - 1 -> link_ptr_low | pos - 1 -> symlist_ptr
1322 // pos - 2 -> symlist_addr_high_word | pos - 2 -> id
1323 // pos - 3 -> symlist_addr_low_word | pos - 3 -> name_ptr;
1324 // pos - 4 -> id_high_word
1325 // pos - 5 -> id_low_word
1326 // pos - 6 -> name_ptr_high_word
1327 // pos - 7 -> name_ptr_low_word
1328 //int32_t entry_pos = pos - (int32_t)(3 * (sizeof(lbm_uint) / 4));
1329 lbm_uint link_ptr;
1330 lbm_uint sym_id;
1331 #ifdef LBM64
1332 ✗ link_ptr = read_u64(pos-1);
1333 ✗ sym_id = read_u64(pos-5);
1334 ✗ *((lbm_uint*)link_ptr) = sym_id;
1335 ✗ lbm_uint next_id = lbm_symrepr_get_next_id();
1336 ✗ if (sym_id >= RUNTIME_SYMBOLS_START && sym_id >= next_id ) {
1337 ✗ lbm_symrepr_set_next_id(sym_id + 1);
1338 }
1339 ✗ lbm_symrepr_set_symlist((lbm_uint*)(image_address + (pos - 7)));
1340 ✗ pos -= 8;
1341 #else
1342 3816 link_ptr = read_u32(pos);
1343 3816 sym_id = read_u32(pos-2);
1344 3816 *((lbm_uint*)link_ptr) = sym_id;
1345 3816 lbm_uint next_id = lbm_symrepr_get_next_id();
1346
2/4
✓ Branch 0 taken 3816 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3816 times.
✗ Branch 3 not taken.
3816 if (sym_id >= RUNTIME_SYMBOLS_START && sym_id >= next_id ) {
1347 3816 lbm_symrepr_set_next_id(sym_id + 1);
1348 }
1349 3816 lbm_symrepr_set_symlist((lbm_uint*)(image_address + (pos - 3)));
1350 3816 pos -= 4;
1351 #endif
1352 3816 } break;
1353 72 case EXTENSION_TABLE: {
1354 // on 64 bit | on 32 bit
1355 // pos -> name_ptr_high | pos -> name_ptr
1356 // pos - 1 -> name_ptr_low | pos - 1 -> fptr
1357 // pos - 2 -> fptr_high
1358 // pos - 3 -> fptr_low
1359 72 int32_t num = (int32_t)read_u32(pos); pos --;
1360
1361 72 int32_t i = 0;
1362
2/2
✓ Branch 0 taken 14400 times.
✓ Branch 1 taken 72 times.
14472 for (i = 0; i < num; i ++) {
1363 lbm_uint name;
1364 lbm_uint fptr;
1365 #ifdef LBM64
1366 ✗ name = read_u64(pos-1);
1367 ✗ fptr = read_u64(pos-3);
1368 ✗ pos -= 4;
1369 #else
1370 14400 name = read_u32(pos);
1371 14400 fptr = read_u32(pos-1);
1372 14400 pos -= 2;
1373 #endif
1374 14400 extension_table[i].name = (char*)name;
1375 14400 extension_table[i].fptr = (extension_fptr)fptr;
1376 }
1377 72 lbm_extensions_set_next((lbm_uint)i);
1378 72 image_has_extensions = true;
1379 72 } break;
1380 72 case SHARING_TABLE: {
1381 72 st.start = pos +1;
1382 72 uint32_t num = read_u32(pos); pos --;
1383 72 st.num = (int32_t)num;
1384
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 52 times.
72 if (num > 0) {
1385 20 target_map = lbm_malloc(num * sizeof(lbm_uint));
1386
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (!target_map ) {
1387 ✗ return false;
1388 }
1389 20 memset(target_map, 0, num * sizeof(lbm_uint));
1390 }
1391 #ifdef LBM64
1392 ✗ pos -= (int32_t)(num + (num * 3));
1393 #else
1394 72 pos -= (int32_t)num * 3;
1395 #endif
1396 72 } break;
1397 66578 default:
1398 66578 write_index = pos+1;
1399 66578 goto done_loading_image;
1400 break;
1401 }
1402 }
1403 ✗ done_loading_image:
1404
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 66558 times.
66578 if (target_map) lbm_free(target_map);
1405 66578 return true;
1406 }
1407