GCC Code Coverage Report


Directory: ../src/
File: lbm_image.c
Date: 2026-09-06 17:54:43
Coverage Exec Excl Total
Lines: 88.2% 551 0 625
Functions: 96.1% 49 0 51
Branches: 56.8% 280 0 493

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 73778 int32_t lbm_image_get_write_index(void) {
212 73778 return write_index;
213 }
214
215 786 bool lbm_image_has_extensions(void) {
216 786 return image_has_extensions;
217 }
218
219 420143 static uint32_t read_u32(int32_t index) {
220 420143 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 12832680 static bool write_u32(uint32_t w, int32_t *i, bool direction) {
235 12832680 bool r = image_write(w, *i, false);
236
2/2
✓ Branch 0 taken 839921 times.
✓ Branch 1 taken 11992759 times.
12832680 (*i) += direction ? 1 : -1;
237 12832680 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 3 taken 2212390 times.
✗ Branch 4 not taken.
2212390 r = r && write_u32(words[1], i, direction);
257
2/4
✓ Branch 0 taken 2212390 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 2212390 times.
✗ Branch 4 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 12778 times.
✓ Branch 1 taken 38962 times.
51740 if (fv_buf_ix >= 4) {
270 12778 r = write_u32(((uint32_t*)fv_buf)[0], &write_index, UPWARDS);
271 12778 memset(fv_buf,0,4);
272 12778 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 1 taken 8442 times.
✗ Branch 2 not taken.
16884 fv_write_u8(bytes[3]) &&
293
1/2
✓ Branch 1 taken 8442 times.
✗ Branch 2 not taken.
16884 fv_write_u8(bytes[2]) &&
294
2/4
✓ Branch 0 taken 8442 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 8442 times.
✗ Branch 4 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 1 taken 9 times.
✗ Branch 2 not taken.
18 fv_write_u8(bytes[7]) &&
302
1/2
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
18 fv_write_u8(bytes[6]) &&
303
1/2
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
18 fv_write_u8(bytes[5]) &&
304
1/2
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
18 fv_write_u8(bytes[4]) &&
305
1/2
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
18 fv_write_u8(bytes[3]) &&
306
1/2
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
18 fv_write_u8(bytes[2]) &&
307
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]) &&
308 9 fv_write_u8(bytes[0]);
309 }
310
311
312 6805577 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 4593187 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 3 taken 20 times.
✗ Branch 4 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 3 taken 4910 times.
✗ Branch 4 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 3 taken 3171 times.
✗ Branch 4 not taken.
3171 res = res && fv_write_u8(S_I28_VALUE);
365
2/4
✓ Branch 0 taken 3171 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 3171 times.
✗ Branch 4 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 3 taken 4 times.
✗ Branch 4 not taken.
4 res = res && fv_write_u8(S_U28_VALUE);
377
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);
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 3 taken 22 times.
✗ Branch 4 not taken.
22 res = res && fv_write_u8(S_BYTE_VALUE);
388
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);
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 3 taken 5 times.
✗ Branch 4 not taken.
5 res = res && fv_write_u8(S_I32_VALUE);
395
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);
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 3 taken 2 times.
✗ Branch 4 not taken.
2 res = res && fv_write_u8(S_U32_VALUE);
402
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);
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 3 taken 39 times.
✗ Branch 4 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 3 taken 39 times.
✗ Branch 4 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 3 taken 2 times.
✗ Branch 4 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 3 taken 2 times.
✗ Branch 4 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 3 taken 5 times.
✗ Branch 4 not taken.
5 res = res && fv_write_u8(S_I64_VALUE);
427
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);
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 3 taken 2 times.
✗ Branch 4 not taken.
2 res = res && fv_write_u8(S_U64_VALUE);
434
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);
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 3 taken 179 times.
✗ Branch 4 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 1 not taken.
✓ Branch 2 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 1058 char *lbm_image_get_version(void) {
456
2/2
✓ Branch 0 taken 272 times.
✓ Branch 1 taken 786 times.
1058 if (image_version) {
457 272 return image_version;
458 } else {
459 786 int32_t pos = (int32_t)image_size-2; // fixed position version string.
460 786 uint32_t val = read_u32(pos); pos --;
461
1/2
✓ Branch 0 taken 786 times.
✗ Branch 1 not taken.
786 if (val == VERSION_ENTRY) {
462 786 int32_t size = (int32_t)read_u32(pos);
463 786 image_version = (char*)(image_address + (pos - size));
464 786 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 3 taken 4872 times.
✗ Branch 4 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 460955 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 460955 bool r = write_u32(SYMBOL_ENTRY, &write_index,DOWNWARDS);
509
2/4
✓ Branch 0 taken 460955 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 460955 times.
✗ Branch 4 not taken.
460955 r = r && write_lbm_uint(symlist, &write_index, DOWNWARDS);
510
2/4
✓ Branch 0 taken 460955 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 460955 times.
✗ Branch 4 not taken.
460955 r = r && write_lbm_uint(id, &write_index, DOWNWARDS);
511
2/4
✓ Branch 0 taken 460955 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 460955 times.
✗ Branch 4 not taken.
460955 r = r && write_lbm_uint((lbm_uint)name, &write_index, DOWNWARDS);
512 460955 lbm_uint entry_ptr = (lbm_uint)(image_address + write_index + 1);
513
1/2
✓ Branch 0 taken 460955 times.
✗ Branch 1 not taken.
460955 if (r)
514 460955 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 527765 static bool write_string_upwards(const char *str, size_t len) {
521 527765 uint32_t bytes = (uint32_t)len + 1;
522
2/2
✓ Branch 0 taken 69402 times.
✓ Branch 1 taken 458363 times.
527765 uint32_t words = (bytes % 4 == 0) ? bytes / 4 : (bytes / 4) + 1;
523 527765 write_u32(words, &write_index, DOWNWARDS);
524 527765 uint32_t w = 0;
525 527765 char *buf = (char*)&w;
526 527765 uint32_t i = 0;
527 527765 int32_t ix = write_index - (int32_t)(words -1);
528 527765 int wi = 0;
529
2/2
✓ Branch 0 taken 2480797 times.
✓ Branch 1 taken 527765 times.
3008562 while (i < bytes) {
530
2/2
✓ Branch 0 taken 527765 times.
✓ Branch 1 taken 1953032 times.
2480797 if (wi == 0 ) {
531 527765 w = 0;
532 }
533
2/2
✓ Branch 0 taken 299057 times.
✓ Branch 1 taken 2181740 times.
2480797 if (wi == 4) wi = 0;
534 2480797 buf[wi] = str[i];
535
2/2
✓ Branch 0 taken 368459 times.
✓ Branch 1 taken 2112338 times.
2480797 if (wi == 3) {
536
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 368459 times.
368459 if (!write_u32(w, &ix, UPWARDS)) return false;
537 //write_u32(w, &ix, UPWARDS);
538 368459 w = 0;
539 }
540 2480797 i ++;
541 2480797 wi ++;
542 }
543
3/4
✓ Branch 0 taken 527765 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 458363 times.
✓ Branch 3 taken 69402 times.
527765 if (wi != 0 && wi != 4) {
544
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 458363 times.
458363 if (!write_u32(w, &ix, UPWARDS)) return false;
545 //write_u32(w, &ix, UPWARDS);
546 }
547 527765 write_index -= (int32_t)words;
548 527765 return true;
549 }
550
551 // return the address of the string
552 460941 char *lbm_image_add_symbol_name(const char *name, size_t len) {
553
2/2
✓ Branch 0 taken 460940 times.
✓ Branch 1 taken 1 time.
460941 if (len > 0) {
554 460940 uint32_t bytes = (uint32_t)len + 1;
555
2/2
✓ Branch 0 taken 69402 times.
✓ Branch 1 taken 391538 times.
460940 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 460940 times.
460940 if ((write_index - (int32_t)(words + 2)) <= (int32_t)image_const_heap.next) {
558 ✗ return NULL;
559 }
560 460940 bool r = write_u32(SYMBOL_NAME_ENTRY, &write_index, DOWNWARDS);
561
2/4
✓ Branch 0 taken 460940 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 460940 times.
✗ Branch 4 not taken.
460940 r = r && write_string_upwards(name, len);
562
1/2
✓ Branch 0 taken 460940 times.
✗ Branch 1 not taken.
460940 if (r)
563 460940 return (char*)(image_address + write_index + 1);
564 }
565 1 return NULL;
566 }
567
568 // The symbol id is written to the link address upon image-boot
569 1355678 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 1355678 bool r = write_u32(SYMBOL_LINK_ENTRY, &write_index,DOWNWARDS);
581
2/4
✓ Branch 0 taken 1355678 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 1355678 times.
✗ Branch 4 not taken.
1355678 r = r && write_lbm_uint((lbm_uint)link, &write_index, DOWNWARDS);
582
2/4
✓ Branch 0 taken 1355678 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 1355678 times.
✗ Branch 4 not taken.
1355678 r = r && write_lbm_uint(symlist, &write_index, DOWNWARDS);
583
2/4
✓ Branch 0 taken 1355678 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 1355678 times.
✗ Branch 4 not taken.
1355678 r = r && write_lbm_uint(id, &write_index, DOWNWARDS);
584
2/4
✓ Branch 0 taken 1355678 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 1355678 times.
✗ Branch 4 not taken.
1355678 r = r && write_lbm_uint((lbm_uint)name, &write_index, DOWNWARDS);
585 1355678 lbm_uint entry_ptr = (lbm_uint)(image_address + write_index + 1);
586
1/2
✓ Branch 0 taken 1355678 times.
✗ Branch 1 not taken.
1355678 if (r)
587 1355678 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 18868 int32_t index_sharing_table(sharing_table *st, int32_t i) {
666
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18868 times.
18868 if (i < 0) return i; // maybe check if more than num?
667 18868 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 18560 times.
✓ Branch 1 taken 32573 times.
51133 for (int32_t i = 0; i < num; i ++ ) {
677 lbm_uint a;
678 18560 int32_t ix = index_sharing_table(st, i);
679 #ifdef LBM64
680 ✗ a = read_u64(ix);
681 #else
682 18560 a = read_u32(ix);
683 #endif
684
2/2
✓ Branch 0 taken 358 times.
✓ Branch 1 taken 18202 times.
18560 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 1 taken 68 times.
✗ Branch 2 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 1 taken 431 times.
✓ Branch 2 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 1 taken 321 times.
✓ Branch 2 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 bool ok;
777 } size_accumulator;
778
779 16376 static int size_acc(lbm_value v, bool shared, void *acc) {
780 (void) shared;
781 16376 size_accumulator *sa = (size_accumulator*)acc;
782
783 16376 int32_t ix = sharing_table_contains(sa->st, v);
784
785
2/2
✓ Branch 0 taken 111 times.
✓ Branch 1 taken 16265 times.
16376 if (ix >= 0) {
786
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)) {
787 // shared node has been sized already and should return size of a ref node
788 // sizeof S_REF and addr
789 #ifdef LBM64
790 ✗ sa->s += 9;
791 #else
792 68 sa->s += 5;
793 #endif
794 68 return TRAV_FUN_SUBTREE_DONE;
795 } else {
796 // setting the sized field to not include the size in future occurrances.
797 43 sharing_table_set_field(sa->st, ix, SHARING_TABLE_SIZED_FIELD, SHARING_TABLE_TRUE);
798 #ifdef LBM64
799 ✗ sa->s += 9;
800 #else
801 43 sa->s += 5;
802 #endif
803 }
804 }
805
806 16308 lbm_uint t = lbm_type_of(v);
807
808
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) {
809 8201 t = t & ~(LBM_PTR_TO_CONSTANT_BIT);
810 }
811
812
4/4
✓ Branch 1 taken 8201 times.
✓ Branch 2 taken 8107 times.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 8200 times.
16308 if (lbm_is_ptr(v) && (v & LBM_PTR_TO_CONSTANT_BIT)) {
813 1 sa->s += (int32_t)sizeof(lbm_uint) + 1;
814 1 return TRAV_FUN_SUBTREE_DONE;
815 }
816
817
13/15
✓ Branch 0 taken 7946 times.
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 22 times.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 3171 times.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 5 times.
✓ Branch 8 taken 2 times.
✓ Branch 9 taken 5 times.
✓ Branch 10 taken 39 times.
✓ Branch 11 taken 2 times.
✓ Branch 12 taken 4910 times.
✓ Branch 13 taken 179 times.
✗ Branch 14 not taken.
16307 switch (t) {
818 7946 case LBM_TYPE_CONS:
819 7946 sa->s += 1;
820 7946 break;
821 20 case LBM_TYPE_LISPARRAY:
822 20 sa->s += 4 + 1;
823 20 break;
824 ✗ case LBM_TYPE_CHANNEL:
825 ✗ sa->ok = false;
826 ✗ return TRAV_FUN_SUBTREE_DONE;
827 22 case LBM_TYPE_BYTE:
828 22 sa->s += 2;
829 22 break;
830 4 case LBM_TYPE_U:
831 4 sa->s += (int32_t)sizeof(lbm_uint) + 1;
832 4 break;
833 3171 case LBM_TYPE_I:
834 3171 sa->s += (int32_t)sizeof(lbm_uint) + 1;
835 3171 break;
836 2 case LBM_TYPE_U32:
837 2 sa->s += 4 + 1;
838 2 break;
839 5 case LBM_TYPE_I32:
840 5 sa->s += 4 + 1;
841 5 break;
842 2 case LBM_TYPE_U64:
843 2 sa->s += 8 + 1;
844 2 break;
845 5 case LBM_TYPE_I64:
846 5 sa->s += 8 + 1;
847 5 break;
848 39 case LBM_TYPE_FLOAT:
849 39 sa->s += 4 + 1;
850 39 break;
851 2 case LBM_TYPE_DOUBLE:
852 2 sa->s += 8 + 1;
853 2 break;
854 4910 case LBM_TYPE_SYMBOL:
855 4910 sa->s += (int32_t)sizeof(lbm_uint) + 1;
856 4910 break;
857 179 case LBM_TYPE_ARRAY: {
858 179 lbm_int arr_size = lbm_heap_array_get_size(v);
859 179 const uint8_t *d = lbm_heap_array_get_data_ro(v);
860
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) {
861 179 sa->s += (int32_t)(1 + 4 + arr_size);
862 }
863 179 }break;
864 }
865 16307 return TRAV_FUN_SUBTREE_CONTINUE;
866 }
867
868 typedef struct {
869 bool res;
870 sharing_table *st;
871 int arg;
872 } flatten_node_meta_data;
873
874 16376 static int flatten_node(lbm_value v, bool shared, void *arg) {
875 (void) shared;
876 16376 flatten_node_meta_data *md = (flatten_node_meta_data*)arg;
877 16376 bool *acc = &md->res;
878
879 16376 int32_t ix = sharing_table_contains(md->st, v);
880
881
2/2
✓ Branch 0 taken 111 times.
✓ Branch 1 taken 16265 times.
16376 if (ix >= 0) {
882
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)) {
883 // Shared node already flattened.
884 #if DEBUG
885 printf("FLATTEN: Writing S_REF for target_map[%d] (addr %x)\n", ix, (unsigned int)v);
886 #endif
887 68 fv_write_u8(S_REF);
888 #ifdef LBM64
889 ✗ fv_write_u64((lbm_uint)v);
890 #else
891 68 fv_write_u32((lbm_uint)v);
892 #endif
893 68 return TRAV_FUN_SUBTREE_DONE;
894 } else {
895 // Shared node not yet flattened.
896 #if DEBUG
897 printf("FLATTEN: Writing S_SHARED for target_map[%d] (addr %x)\n", ix, (unsigned int)v);
898 #endif
899 43 sharing_table_set_field(md->st, ix, SHARING_TABLE_FLATTENED_FIELD, SHARING_TABLE_TRUE);
900 43 fv_write_u8(S_SHARED);
901 #ifdef LBM64
902 ✗ fv_write_u64((lbm_uint)v);
903 #else
904 43 fv_write_u32((lbm_uint)v);
905 #endif
906 // Continue flattening along this subtree.
907 }
908 }
909
910 16308 lbm_uint t = lbm_type_of(v);
911
912
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) {
913 8201 t = t & ~(LBM_PTR_TO_CONSTANT_BIT);
914 }
915
916
4/4
✓ Branch 1 taken 8201 times.
✓ Branch 2 taken 8107 times.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 8200 times.
16308 if (lbm_is_ptr(v) && (v & LBM_PTR_TO_CONSTANT_BIT)) {
917
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);
918 #ifdef LBM64
919 ✗ *acc = *acc && fv_write_u64((lbm_uint)v);
920 #else
921
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);
922 #endif
923 1 return TRAV_FUN_SUBTREE_DONE;
924 }
925
926
13/15
✓ Branch 0 taken 7946 times.
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 22 times.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 3171 times.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 5 times.
✓ Branch 8 taken 2 times.
✓ Branch 9 taken 5 times.
✓ Branch 10 taken 39 times.
✓ Branch 11 taken 2 times.
✓ Branch 12 taken 4910 times.
✓ Branch 13 taken 179 times.
✗ Branch 14 not taken.
16307 switch (t) {
927 7946 case LBM_TYPE_CONS:
928
2/4
✓ Branch 0 taken 7946 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 7946 times.
✗ Branch 4 not taken.
7946 *acc = *acc && i_f_cons();
929 7946 break;
930 20 case LBM_TYPE_LISPARRAY: {
931 20 lbm_array_header_t *header = (lbm_array_header_t*)lbm_car(v);
932
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 if (header) {
933 20 uint32_t size = (uint32_t)(header->size / sizeof(lbm_value));
934
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);
935 } else {
936 // hmm
937 }
938 20 } break;
939 ✗ case LBM_TYPE_CHANNEL:
940 ✗ *acc = false;
941 ✗ return TRAV_FUN_SUBTREE_DONE;
942 22 case LBM_TYPE_BYTE:
943
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));
944 22 break;
945 4 case LBM_TYPE_U:
946
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));
947 4 break;
948 3171 case LBM_TYPE_I:
949
2/4
✓ Branch 0 taken 3171 times.
✗ Branch 1 not taken.
✓ Branch 4 taken 3171 times.
✗ Branch 5 not taken.
3171 *acc = *acc && i_f_i(lbm_dec_i(v));
950 3171 break;
951 2 case LBM_TYPE_U32:
952
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));
953 2 break;
954 5 case LBM_TYPE_I32:
955
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));
956 5 break;
957 2 case LBM_TYPE_U64:
958
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));
959 2 break;
960 5 case LBM_TYPE_I64:
961
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));
962 5 break;
963 39 case LBM_TYPE_FLOAT:
964
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));
965 39 break;
966 2 case LBM_TYPE_DOUBLE:
967
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));
968 2 break;
969 4910 case LBM_TYPE_SYMBOL:
970
2/4
✓ Branch 0 taken 4910 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 4910 times.
✗ Branch 4 not taken.
4910 *acc = *acc && i_f_sym(v);
971 4910 break;
972 179 case LBM_TYPE_ARRAY: {
973 179 lbm_int s = lbm_heap_array_get_size(v);
974 179 const uint8_t *d = lbm_heap_array_get_data_ro(v);
975
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) {
976
2/4
✓ Branch 0 taken 179 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 179 times.
✗ Branch 4 not taken.
179 *acc = *acc && i_f_lbm_array((uint32_t)s, (uint8_t*)d);
977 }
978 179 }break;
979 ✗ default:
980 ✗ break;
981 }
982 16307 return TRAV_FUN_SUBTREE_CONTINUE;
983 }
984
985 // Performing GC after using the ptr_rev_trav to restore the
986 // GC-bit in the value traversed.
987 321 static int32_t image_flatten_size(sharing_table *st, lbm_value v) {
988 size_accumulator sa;
989 321 sa.s = 0;
990 321 sa.st = st;
991 321 sa.ok = true;
992 321 lbm_ptr_rev_trav(size_acc, v, &sa);
993 321 lbm_perform_gc();
994
1/2
✓ Branch 0 taken 321 times.
✗ Branch 1 not taken.
321 if (sa.ok) return sa.s;
995 ✗ else return -1;
996 }
997
998 321 static bool image_flatten_value(sharing_table *st, lbm_value v) {
999 flatten_node_meta_data md;
1000 321 md.res = true;
1001 321 md.st = st;
1002 321 md.arg = 0;
1003 321 lbm_ptr_rev_trav(flatten_node, v, &md);
1004 321 lbm_perform_gc();
1005 321 return md.res; // ok = enough space in image for flat val.
1006 }
1007
1008 // ////////////////////////////////////////////////////////////
1009 // print sharing table
1010 #if DEBUG
1011 void print_sharing_table(sharing_table *st) {
1012 int32_t pos = st->start;
1013 int32_t num = st->num;
1014 char buf[256];
1015
1016 for (int i = 0; i < num; i ++) {
1017 int32_t ix = index_sharing_table(st, i);
1018 lbm_uint a = read_u32(ix); // address
1019
1020 lbm_print_value(buf, 256, a);
1021 printf("%d\t%x\t%s\n",i, a, buf);
1022 }
1023
1024
1025 }
1026 #endif
1027
1028 // ////////////////////////////////////////////////////////////
1029 //
1030 75 bool lbm_image_save_global_env(void) {
1031
1032 75 sharing_table st = lbm_image_sharing();
1033 75 lbm_value *env = lbm_get_global_env();
1034
1/2
✓ Branch 0 taken 75 times.
✗ Branch 1 not taken.
75 if (env) {
1035
2/2
✓ Branch 0 taken 2400 times.
✓ Branch 1 taken 75 times.
2475 for (int i = 0; i < GLOBAL_ENV_ROOTS; i ++) {
1036 2400 lbm_value curr = env[i];
1037
2/2
✓ Branch 1 taken 431 times.
✓ Branch 2 taken 2400 times.
2831 while(lbm_is_cons(curr)) {
1038 431 lbm_value name_field = lbm_caar(curr);
1039 431 lbm_value val_field = lbm_cdr(lbm_car(curr));
1040
1041
2/2
✓ Branch 1 taken 110 times.
✓ Branch 2 taken 321 times.
431 if (lbm_is_constant(val_field)) {
1042 110 write_u32(BINDING_CONST, &write_index, DOWNWARDS);
1043 110 write_lbm_value(name_field, &write_index, DOWNWARDS);
1044 110 write_lbm_value(val_field, &write_index, DOWNWARDS);
1045 } else {
1046 321 int fv_size = image_flatten_size(&st, val_field);
1047
1/2
✓ Branch 0 taken 321 times.
✗ Branch 1 not taken.
321 if (fv_size > 0) {
1048
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 288 times.
321 fv_size = (fv_size % 4 == 0) ? (fv_size / 4) : (fv_size / 4) + 1; // num 32bit words
1049
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 321 times.
321 if ((write_index - fv_size) <= (int32_t)image_const_heap.next) {
1050 ✗ return false;
1051 }
1052 321 write_u32(BINDING_FLAT, &write_index, DOWNWARDS);
1053 321 write_u32((uint32_t)fv_size , &write_index, DOWNWARDS);
1054 321 write_lbm_value(name_field, &write_index, DOWNWARDS);
1055 321 write_index = write_index - fv_size; // subtract fv_size
1056 #if DEBUG
1057 int32_t data_start = write_index; // Save the start position
1058 #endif
1059
1/2
✓ Branch 1 taken 321 times.
✗ Branch 2 not taken.
321 if (image_flatten_value(&st, val_field)) { // adds fv_size back
1060 321 fv_write_flush();
1061 #if DEBUG
1062 printf("Flattenining address: %x\n", val_field);
1063 for (int i = 0; i < fv_size; i ++) {
1064 uint32_t v = read_u32(data_start + i);
1065 uint8_t *p = &v;
1066 for (int j = 0; j < 4; j ++) {
1067 printf("%x ", p[j]);
1068 }
1069 printf(" ");
1070 }
1071 printf("\n");
1072 #endif
1073
1074 // TODO: What error handling makes sense?
1075 } else {
1076 ✗ return false;
1077 }
1078 321 write_index = write_index - fv_size - 1; // subtract fv_size
1079 } else {
1080 ✗ return false;
1081 }
1082 }
1083 431 curr = lbm_cdr(curr);
1084 }
1085 }
1086 #if DEBUG
1087 printf("Sharing table:\n");
1088 print_sharing_table(&st);
1089 #endif
1090 75 return true;
1091 }
1092 ✗ return false;
1093 }
1094
1095 // The extension table is created at system startup.
1096 // Extensions can also be added dynamically.
1097 // Dynamically added extensions have names starting with "ext-"
1098 // and their names are placed in RAM by the reader.
1099 //
1100 // Symbol_id -> index in extension table mapping
1101 // is created as extensions are added.
1102 // dynamic extensions are added after "built-in" extensions
1103 // and have higher indices.
1104
1105 75 bool lbm_image_save_extensions(void) {
1106 75 bool r = true;
1107 75 lbm_uint num = lbm_get_num_extensions();
1108
1/2
✓ Branch 0 taken 75 times.
✗ Branch 1 not taken.
75 if (num > 0) {
1109 // Dynamically registered extensions can have name fields
1110 // pointing to LBM_MEMORY (RAM). Move these strings to flash
1111 // Before copying the entire extension table to flash.
1112
2/2
✓ Branch 0 taken 17027 times.
✓ Branch 1 taken 75 times.
17102 for (lbm_uint i = 0; i < num; i ++) {
1113 17027 const char *name_ptr = extension_table[i].name;
1114
3/4
✓ Branch 0 taken 17027 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 17026 times.
17027 if (name_ptr && lbm_memory_ptr_inside((lbm_uint *)name_ptr)) {
1115 1 char *addr = lbm_image_add_symbol_name(name_ptr, strlen(name_ptr));
1116
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 if (!addr) return false;
1117 1 extension_table[i].name = addr;
1118 }
1119 }
1120
1121
2/4
✓ Branch 0 taken 75 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 75 times.
✗ Branch 4 not taken.
75 r = r && write_u32(EXTENSION_TABLE, &write_index, DOWNWARDS);
1122
2/4
✓ Branch 0 taken 75 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 75 times.
✗ Branch 4 not taken.
75 r = r && write_u32((uint32_t)num , &write_index, DOWNWARDS);
1123
2/2
✓ Branch 0 taken 17027 times.
✓ Branch 1 taken 75 times.
17102 for (lbm_uint i = 0; i < num; i ++) {
1124
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17027 times.
17027 if (!r) return r;
1125
1126 17027 const char *name_ptr = extension_table[i].name;
1127 // Static extensions have names that are C string literals
1128 // living in the firmware binary itself. Those are assumed
1129 // stable for as long as that firmware build is running.
1130 #ifdef LBM64
1131 ✗ r = r && write_u64((uint64_t)name_ptr, &write_index, DOWNWARDS);
1132 ✗ r = r && write_u64((uint64_t)extension_table[i].fptr, &write_index, DOWNWARDS);
1133 #else
1134
2/4
✓ Branch 0 taken 17027 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 17027 times.
✗ Branch 4 not taken.
17027 r = r && write_u32((uint32_t)name_ptr, &write_index, DOWNWARDS);
1135
2/4
✓ Branch 0 taken 17027 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 17027 times.
✗ Branch 4 not taken.
17027 r = r && write_u32((uint32_t)extension_table[i].fptr, &write_index, DOWNWARDS);
1136 #endif
1137 } }
1138 75 return r;
1139 }
1140
1141 static uint32_t last_const_heap_ix = 0;
1142
1143 75 bool lbm_image_save_constant_heap_ix(void) {
1144 75 bool r = true; // saved or no need to save it.
1145
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 61 times.
75 if (image_const_heap.next != last_const_heap_ix) {
1146 14 last_const_heap_ix = (uint32_t)image_const_heap.next;
1147 14 r = write_u32(CONSTANT_HEAP_IX, &write_index, DOWNWARDS);
1148
2/4
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 14 times.
✗ Branch 4 not taken.
14 r = r && write_u32((uint32_t)image_const_heap.next, &write_index, DOWNWARDS);
1149 }
1150 75 return r;
1151 }
1152
1153 786 bool lbm_image_exists(void) {
1154 786 uint32_t val = read_u32((int32_t)image_size - 1);
1155 786 return val == IMAGE_INITIALIZED;
1156 }
1157
1158 67082 void lbm_image_init(uint32_t* image_mem_address,
1159 uint32_t image_size_words,
1160 lbm_image_write_fun image_write_fun) {
1161 67082 image_write = image_write_fun;
1162 67082 image_address = image_mem_address;
1163 67082 image_size = image_size_words;
1164 67082 write_index = (int32_t)image_size_words -1;
1165 67082 image_has_extensions = false;
1166 67082 image_version = NULL;
1167 67082 last_const_heap_ix = 0;
1168 67082 }
1169
1170 66825 void lbm_image_create(char *version_str) {
1171 66825 write_u32(IMAGE_INITIALIZED, &write_index, DOWNWARDS);
1172
1/2
✓ Branch 0 taken 66825 times.
✗ Branch 1 not taken.
66825 if (version_str) {
1173 66825 write_u32(VERSION_ENTRY, &write_index, DOWNWARDS);
1174 66825 write_string_upwards(version_str, strlen(version_str));
1175 }
1176 66825 }
1177
1178
1179 67082 bool lbm_image_boot(void) {
1180 //process image
1181 67082 int32_t pos = (int32_t)image_size-1;
1182 67082 last_const_heap_ix = 0;
1183
1184 sharing_table st;
1185 67082 lbm_uint *target_map = NULL; // Target addresses for shared/refs from the flat values.
1186
1187
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 228057 times.
✓ Branch 2 taken 228057 times.
✗ Branch 3 not taken.
228057 while (pos >= 0 && pos > (int32_t)last_const_heap_ix) {
1188 228057 uint32_t val = read_u32(pos);
1189 228057 pos --;
1190
11/11
✓ Branch 0 taken 67082 times.
✓ Branch 1 taken 67082 times.
✓ Branch 2 taken 5527 times.
✓ Branch 3 taken 14 times.
✓ Branch 4 taken 110 times.
✓ Branch 5 taken 321 times.
✓ Branch 6 taken 5526 times.
✓ Branch 7 taken 15163 times.
✓ Branch 8 taken 75 times.
✓ Branch 9 taken 75 times.
✓ Branch 10 taken 67082 times.
228057 switch(val) {
1191 67082 case IMAGE_INITIALIZED: {
1192 67082 image_const_heap_start_ix = 0; // const heap starts at 0
1193 67082 lbm_const_heap_init(image_const_heap_write,
1194 &image_const_heap,
1195 (lbm_uint*)(image_address));
1196 // initialized is a one word field
1197 67082 } break;
1198 67082 case VERSION_ENTRY: {
1199 67082 uint32_t size = read_u32(pos); pos --;
1200 67082 image_version = (char*)(image_address + (pos - (int32_t)size + 1));
1201 67082 pos -= (int32_t)size;
1202 67082 } break;
1203 5527 case SYMBOL_NAME_ENTRY: {
1204 // Just jump accross the string.
1205 5527 uint32_t size = read_u32(pos); pos --;
1206 5527 pos -= (int32_t)size;
1207 5527 } break;
1208 14 case CONSTANT_HEAP_IX: {
1209 14 uint32_t next = read_u32(pos);
1210 14 pos --;
1211 14 last_const_heap_ix = next;
1212 14 image_const_heap.next = next;
1213 14 } break;
1214 110 case BINDING_CONST: {
1215 // on 64 bit | on 32 bit
1216 // pos -> key_high | pos -> key
1217 // pos - 1 -> key_low | pos - 1 -> val
1218 // pos - 2 -> val_high
1219 // pos - 3 -> val_low
1220 #ifdef LBM64
1221 ✗ lbm_uint bind_key = read_u64(pos-1);
1222 ✗ lbm_uint bind_val = read_u64(pos-3);
1223 ✗ pos -= 4;
1224 #else
1225 110 lbm_uint bind_key = read_u32(pos);
1226 110 lbm_uint bind_val = read_u32(pos-1);
1227 110 pos -= 2;
1228 #endif
1229 110 lbm_uint ix_key = lbm_dec_sym(bind_key) & GLOBAL_ENV_MASK;
1230 110 lbm_value *global_env = lbm_get_global_env();
1231 110 lbm_uint orig_env = global_env[ix_key];
1232 110 lbm_value new_env = lbm_env_set(orig_env,bind_key,bind_val);
1233
1234
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 110 times.
110 if (lbm_is_symbol(new_env)) {
1235 ✗ return false;
1236 }
1237 110 global_env[ix_key] = new_env;
1238 110 } break;
1239 321 case BINDING_FLAT: {
1240 // on 64 bit | on 32 bit
1241 // pos -> size | pos -> size
1242 // pos - 1 -> key_high | pos - 1 -> key
1243 // pos - 2 -> key_low
1244 //
1245 321 int32_t s = (int32_t)read_u32(pos);
1246 // size in 32 or 64 bit words.
1247 #ifdef LBM64
1248 ✗ lbm_uint bind_key = read_u64(pos-2);
1249 ✗ pos -= 3;
1250 #else
1251 321 lbm_uint bind_key = read_u32(pos-1);
1252 321 pos -= 2;
1253 #endif
1254
1255 321 pos -= s;
1256 lbm_flat_value_t fv;
1257 321 fv.buf = (uint8_t*)(image_address + pos);
1258 321 fv.buf_size = (uint32_t)s * sizeof(lbm_uint); // GEQ to actual buf
1259 321 fv.buf_pos = 0;
1260 lbm_value unflattened;
1261
2/2
✓ Branch 0 taken 103 times.
✓ Branch 1 taken 218 times.
321 if (target_map) {
1262
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 103 times.
103 if (!lbm_unflatten_value_sharing(&st, target_map, &fv, &unflattened)) {
1263 ✗ return false;
1264 }
1265 // When a value is unflattened it may contain shared subvalues
1266 // and references to shared values. A reference may point to either
1267 // values that are shared within the value that is currently unflattened
1268 // or to a value that has previously been unflattened.
1269 //
1270 // There is an ordering property that must be maintained that the node
1271 // with the S_SHARED tag is always processed before any corresponding S_REF tags.
1272 // This means that if a ref node is unflattened the target to point it to will
1273 // already exist.
1274 //
1275 // If GC needs to happen while unflattening a value, there is no danger of messing
1276 // up the addresses to point references to because:
1277 // 1. The S_SHARED node is local to the same value and will be recreated after GC
1278 // and the ref value in target map will be overwritten. Any local refs will be also
1279 // be recreated. Any refs to the S_Shared outside of this value, will be in values
1280 // processed in the future.
1281 // 2. S_SHARED nodes that have been created as part of prvious value are untouched
1282 // by running GC as they have already been unflattened and should be reachable
1283 // on the environment. Their mapping in the target map is still valid.
1284
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 103 times.
103 if (lbm_is_symbol_merror(unflattened)) {
1285 //memset(target_map, 0, st.num * sizeof(lbm_uint));
1286 ✗ lbm_perform_gc();
1287 ✗ lbm_unflatten_value_sharing(&st, target_map, &fv, &unflattened);
1288 }
1289 } else {
1290 218 lbm_unflatten_value(&fv, &unflattened);
1291
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 218 times.
218 if (lbm_is_symbol_merror(unflattened)) {
1292 ✗ lbm_perform_gc();
1293 ✗ lbm_unflatten_value(&fv, &unflattened);
1294 }
1295 }
1296 321 lbm_uint ix_key = lbm_dec_sym(bind_key) & GLOBAL_ENV_MASK;
1297 321 lbm_value *global_env = lbm_get_global_env();
1298 321 lbm_uint orig_env = global_env[ix_key];
1299 321 lbm_value new_env = lbm_env_set(orig_env,bind_key,unflattened);
1300
1301
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 321 times.
321 if (lbm_is_symbol(new_env)) {
1302 ✗ return false;
1303 }
1304 321 global_env[ix_key] = new_env;
1305 321 pos --;
1306 321 } break;
1307 5526 case SYMBOL_ENTRY: {
1308 // on 64 bit | on 32 bit
1309 // pos -> symlist_addr_high_word | pos -> symlist_ptr
1310 // pos - 1 -> symlist_addr_low_word | pos - 1 -> id
1311 // pos - 2 -> id_high_word | pos - 2 -> name_ptr
1312 // pos - 3 -> id_low_word |
1313 // pos - 4 -> name_ptr_high_word |
1314 // pos - 5 -> name_ptr_low_word |
1315 #ifdef LBM64
1316 ✗ int32_t entry_pos = pos - 5;
1317 ✗ lbm_uint *p = (lbm_uint*)(image_address + entry_pos);
1318 ✗ uint32_t sym_id = (uint32_t)(p[1]);
1319 ✗ lbm_uint next_id = lbm_symrepr_get_next_id();
1320 ✗ if (sym_id >= RUNTIME_SYMBOLS_START && sym_id >= next_id ) {
1321 ✗ lbm_symrepr_set_next_id(sym_id + 1);
1322 }
1323 ✗ lbm_symrepr_set_symlist((lbm_uint*)(image_address + entry_pos));
1324 ✗ pos -= 6;
1325 #else
1326 5526 int32_t entry_pos = pos - 2;
1327 5526 lbm_uint *p = (lbm_uint*)(image_address + entry_pos);
1328 5526 uint32_t sym_id = (uint32_t)(p[1]);
1329 5526 lbm_uint next_id = lbm_symrepr_get_next_id();
1330
2/4
✓ Branch 0 taken 5526 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5526 times.
✗ Branch 3 not taken.
5526 if (sym_id >= RUNTIME_SYMBOLS_START && sym_id >= next_id ) {
1331 5526 lbm_symrepr_set_next_id(sym_id + 1);
1332 }
1333 5526 lbm_symrepr_set_symlist((lbm_uint*)(image_address + entry_pos));
1334 5526 pos -= 3;
1335 #endif
1336 5526 } break;
1337 15163 case SYMBOL_LINK_ENTRY: {
1338 // on 64 bits | on 32 bit
1339 // pos -> link_ptr_high | pos -> link_ptr
1340 // pos - 1 -> link_ptr_low | pos - 1 -> symlist_ptr
1341 // pos - 2 -> symlist_addr_high_word | pos - 2 -> id
1342 // pos - 3 -> symlist_addr_low_word | pos - 3 -> name_ptr;
1343 // pos - 4 -> id_high_word
1344 // pos - 5 -> id_low_word
1345 // pos - 6 -> name_ptr_high_word
1346 // pos - 7 -> name_ptr_low_word
1347 //int32_t entry_pos = pos - (int32_t)(3 * (sizeof(lbm_uint) / 4));
1348 lbm_uint link_ptr;
1349 lbm_uint sym_id;
1350 #ifdef LBM64
1351 ✗ link_ptr = read_u64(pos-1);
1352 ✗ sym_id = read_u64(pos-5);
1353 ✗ *((lbm_uint*)link_ptr) = sym_id;
1354 ✗ lbm_uint next_id = lbm_symrepr_get_next_id();
1355 ✗ if (sym_id >= RUNTIME_SYMBOLS_START && sym_id >= next_id ) {
1356 ✗ lbm_symrepr_set_next_id(sym_id + 1);
1357 }
1358 ✗ lbm_symrepr_set_symlist((lbm_uint*)(image_address + (pos - 7)));
1359 ✗ pos -= 8;
1360 #else
1361 15163 link_ptr = read_u32(pos);
1362 15163 sym_id = read_u32(pos-2);
1363 15163 *((lbm_uint*)link_ptr) = sym_id;
1364 15163 lbm_uint next_id = lbm_symrepr_get_next_id();
1365
2/4
✓ Branch 0 taken 15163 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15163 times.
✗ Branch 3 not taken.
15163 if (sym_id >= RUNTIME_SYMBOLS_START && sym_id >= next_id ) {
1366 15163 lbm_symrepr_set_next_id(sym_id + 1);
1367 }
1368 15163 lbm_symrepr_set_symlist((lbm_uint*)(image_address + (pos - 3)));
1369 15163 pos -= 4;
1370 #endif
1371 15163 } break;
1372 75 case EXTENSION_TABLE: {
1373 // on 64 bit | on 32 bit
1374 // pos -> name_ptr_high | pos -> name_ptr
1375 // pos - 1 -> name_ptr_low | pos - 1 -> fptr
1376 // pos - 2 -> fptr_high
1377 // pos - 3 -> fptr_low
1378 75 int32_t num = (int32_t)read_u32(pos); pos --;
1379
1380 75 int32_t i = 0;
1381
2/2
✓ Branch 0 taken 17027 times.
✓ Branch 1 taken 75 times.
17102 for (i = 0; i < num; i ++) {
1382 lbm_uint name;
1383 lbm_uint fptr;
1384 #ifdef LBM64
1385 ✗ name = read_u64(pos-1);
1386 ✗ fptr = read_u64(pos-3);
1387 ✗ pos -= 4;
1388 #else
1389 17027 name = read_u32(pos);
1390 17027 fptr = read_u32(pos-1);
1391 17027 pos -= 2;
1392 #endif
1393 17027 extension_table[i].name = (char*)name;
1394 17027 extension_table[i].fptr = (extension_fptr)fptr;
1395 }
1396 75 lbm_extensions_set_next((lbm_uint)i);
1397 75 image_has_extensions = true;
1398 75 } break;
1399 75 case SHARING_TABLE: {
1400 75 st.start = pos +1;
1401 75 uint32_t num = read_u32(pos); pos --;
1402 75 st.num = (int32_t)num;
1403
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 55 times.
75 if (num > 0) {
1404 20 target_map = lbm_malloc(num * sizeof(lbm_uint));
1405
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (!target_map ) {
1406 ✗ return false;
1407 }
1408 20 memset(target_map, 0, num * sizeof(lbm_uint));
1409 }
1410 #ifdef LBM64
1411 ✗ pos -= (int32_t)(num + (num * 3));
1412 #else
1413 75 pos -= (int32_t)num * 3;
1414 #endif
1415 75 } break;
1416 67082 default:
1417 67082 write_index = pos+1;
1418 67082 goto done_loading_image;
1419 break;
1420 }
1421 }
1422 ✗ done_loading_image:
1423
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 67062 times.
67082 if (target_map) lbm_free(target_map);
1424 67082 return true;
1425 }
1426