GCC Code Coverage Report


Directory: ../src/
File: lbm_flat_value.c
Date: 2026-02-08 10:07:39
Coverage Exec Excl Total
Lines: 88.3% 545 0 617
Functions: 100.0% 36 0 36
Branches: 72.1% 295 0 409

Line Branch Exec Source
1 /*
2 Copyright 2023, 2024, 2025 Joel Svensson svenssonjoel@yahoo.se
3 2023 Benjamin Vedder
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include <lbm_flat_value.h>
20 #include <eval_cps.h>
21 #include <stack.h>
22
23 #include <setjmp.h>
24
25 #ifndef DEBUG
26 #define DEBUG 0
27 #endif
28
29 // ------------------------------------------------------------
30 // Access to GC from eval_cps
31 int lbm_perform_gc(void);
32
33
34 // ------------------------------------------------------------
35 // Flatteners
36 126361 bool lbm_start_flatten(lbm_flat_value_t *v, size_t buffer_size) {
37 126361 bool res = false;
38 126361 uint8_t *data = lbm_malloc_reserve(buffer_size);
39
2/2
✓ Branch 0 taken 126360 times.
✓ Branch 1 taken 1 time.
126361 if (data) {
40 126360 v->buf = data;
41 126360 v->buf_size = buffer_size;
42 126360 v->buf_pos = 0;
43 126360 res = true;
44 }
45 126361 return res;
46 }
47
48 22633 bool lbm_finish_flatten(lbm_flat_value_t *v) {
49 lbm_uint size_words;
50
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 22627 times.
22633 if (v->buf_pos % sizeof(lbm_uint) == 0) {
51 6 size_words = v->buf_pos / sizeof(lbm_uint);
52 } else {
53 22627 size_words = (v->buf_pos / sizeof(lbm_uint)) + 1;
54 }
55
2/2
✓ Branch 0 taken 150 times.
✓ Branch 1 taken 22483 times.
22633 if (v->buf_size <= size_words * sizeof(lbm_uint)) return true;
56 22483 v->buf_size = size_words * sizeof(lbm_uint);
57 22483 return (lbm_memory_shrink((lbm_uint*)v->buf, size_words) >= 0);
58 }
59
60 1269056 static bool write_byte(lbm_flat_value_t *v, uint8_t b) {
61 1269056 bool res = false;
62
2/2
✓ Branch 0 taken 1269047 times.
✓ Branch 1 taken 9 times.
1269056 if (v->buf_size >= v->buf_pos + 1) {
63 1269047 v->buf[v->buf_pos++] = b;
64 1269047 res = true;
65 }
66 1269056 return res;
67 }
68
69 545768 static bool write_bytes(lbm_flat_value_t *v, uint8_t *data,lbm_uint num_bytes) {
70 545768 bool res = false;
71
2/2
✓ Branch 0 taken 545762 times.
✓ Branch 1 taken 6 times.
545768 if (v->buf_size >= v->buf_pos + num_bytes) {
72 545762 memcpy(v->buf + v->buf_pos, data, num_bytes);
73 545762 v->buf_pos += num_bytes;
74 545762 res = true;
75 }
76 545768 return res;
77 }
78
79 662910 static bool write_word(lbm_flat_value_t *v, uint32_t w) {
80 662910 bool res = false;
81
2/2
✓ Branch 0 taken 662878 times.
✓ Branch 1 taken 32 times.
662910 if (v->buf_size >= v->buf_pos + 4) {
82 662878 v->buf[v->buf_pos++] = (uint8_t)(w >> 24);
83 662878 v->buf[v->buf_pos++] = (uint8_t)(w >> 16);
84 662878 v->buf[v->buf_pos++] = (uint8_t)(w >> 8);
85 662878 v->buf[v->buf_pos++] = (uint8_t)w;
86 662878 res = true;
87 }
88 662910 return res;
89 }
90
91 52722 static bool write_dword(lbm_flat_value_t *v, uint64_t w) {
92 52722 bool res = false;
93
2/2
✓ Branch 0 taken 52703 times.
✓ Branch 1 taken 19 times.
52722 if (v->buf_size >= v->buf_pos + 8) {
94 52703 v->buf[v->buf_pos++] = (uint8_t)(w >> 56);
95 52703 v->buf[v->buf_pos++] = (uint8_t)(w >> 48);
96 52703 v->buf[v->buf_pos++] = (uint8_t)(w >> 40);
97 52703 v->buf[v->buf_pos++] = (uint8_t)(w >> 32);
98 52703 v->buf[v->buf_pos++] = (uint8_t)(w >> 24);
99 52703 v->buf[v->buf_pos++] = (uint8_t)(w >> 16);
100 52703 v->buf[v->buf_pos++] = (uint8_t)(w >> 8);
101 52703 v->buf[v->buf_pos++] = (uint8_t)w;
102 52703 res = true;
103 }
104 52722 return res;
105 }
106
107 959634 bool f_cons(lbm_flat_value_t *v) {
108 959634 bool res = false;
109
2/2
✓ Branch 0 taken 959632 times.
✓ Branch 1 taken 2 times.
959634 if (v->buf_size >= v->buf_pos + 1) {
110 959632 v->buf[v->buf_pos++] = S_CONS;
111 959632 res = true;
112 }
113 959634 return res;
114 }
115
116 336 bool f_lisp_array(lbm_flat_value_t *v, uint32_t size) {
117 // arrays are smaller than 2^32 elements long
118 336 bool res = true;
119
2/4
✓ Branch 0 taken 336 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 336 times.
✗ Branch 4 not taken.
336 res = res && write_byte(v, S_LBM_LISP_ARRAY);
120
2/4
✓ Branch 0 taken 336 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 336 times.
✗ Branch 4 not taken.
336 res = res && write_word(v, size); // number of elements.
121 336 return res;
122 }
123
124 17248 bool f_sym(lbm_flat_value_t *v, lbm_uint sym_id) {
125 17248 bool res = true;
126
2/4
✓ Branch 0 taken 17248 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 17248 times.
✗ Branch 4 not taken.
17248 res = res && write_byte(v,S_SYM_VALUE);
127 #ifndef LBM64
128
2/4
✓ Branch 0 taken 10482 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 10482 times.
✗ Branch 4 not taken.
10482 res = res && write_word(v,sym_id);
129 #else
130
2/4
✓ Branch 0 taken 6766 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 6766 times.
✗ Branch 4 not taken.
6766 res = res && write_dword(v,sym_id);
131 #endif
132 17248 return res;
133 }
134
135 189414 bool f_sym_string(lbm_flat_value_t *v, char *str) {
136 189414 bool res = false;
137
1/2
✓ Branch 0 taken 189414 times.
✗ Branch 1 not taken.
189414 if (str) {
138 189414 lbm_uint sym_bytes = strlen(str) + 1;
139
4/4
✓ Branch 1 taken 189412 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 189409 times.
✓ Branch 4 taken 3 times.
378826 if (write_byte(v, S_SYM_STRING) &&
140 189412 write_bytes(v, (uint8_t*)str, sym_bytes)) {
141 189409 res = true;
142 }
143 }
144 189414 return res;
145 }
146
147 // Potentially a difference between 32/64 bit version.
148 // strlen returns size_t which is different on 32/64 bit platforms.
149 189248 int f_sym_string_bytes(lbm_value sym) {
150 189248 int res = FLATTEN_VALUE_ERROR_FATAL;
151
1/2
✓ Branch 1 taken 189248 times.
✗ Branch 2 not taken.
189248 if (lbm_is_symbol(sym)) {
152 189248 lbm_uint s = lbm_dec_sym(sym);
153 189248 char *sym_str = (char*)lbm_get_name_by_symbol(s);
154
1/2
✓ Branch 0 taken 189248 times.
✗ Branch 1 not taken.
189248 if (sym_str) {
155 189248 lbm_uint sym_bytes = strlen(sym_str) + 1;
156 189248 res = (int)sym_bytes;
157 }
158 }
159 189248 return res;
160 }
161
162 137121 bool f_i(lbm_flat_value_t *v, lbm_int i) {
163 137121 bool res = true;
164 #ifndef LBM64
165
3/4
✓ Branch 0 taken 91468 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 91465 times.
✓ Branch 4 taken 3 times.
91468 res = res && write_byte(v,S_I28_VALUE);
166
4/4
✓ Branch 0 taken 91465 times.
✓ Branch 1 taken 3 times.
✓ Branch 3 taken 91454 times.
✓ Branch 4 taken 11 times.
91468 res = res && write_word(v,(uint32_t)i);
167 #else
168
2/4
✓ Branch 0 taken 45653 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 45653 times.
✗ Branch 4 not taken.
45653 res = res && write_byte(v,S_I56_VALUE);
169
2/4
✓ Branch 0 taken 45653 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 45653 times.
✗ Branch 4 not taken.
45653 res = res && write_dword(v, (uint64_t)i);
170 #endif
171 137121 return res;
172 }
173
174 88 bool f_u(lbm_flat_value_t *v, lbm_uint u) {
175 88 bool res = true;
176 #ifndef LBM64
177
2/4
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 60 times.
✗ Branch 4 not taken.
60 res = res && write_byte(v,S_U28_VALUE);
178
3/4
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 56 times.
✓ Branch 4 taken 4 times.
60 res = res && write_word(v,(uint32_t)u);
179 #else
180
2/4
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 28 times.
✗ Branch 4 not taken.
28 res = res && write_byte(v,S_U56_VALUE);
181
2/4
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 28 times.
✗ Branch 4 not taken.
28 res = res && write_dword(v,(uint64_t)u);
182 #endif
183 88 return res;
184 }
185
186 182003 bool f_b(lbm_flat_value_t *v, uint8_t b) {
187 182003 bool res = true;
188
2/4
✓ Branch 0 taken 182003 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 182003 times.
✗ Branch 4 not taken.
182003 res = res && write_byte(v,S_BYTE_VALUE);
189
3/4
✓ Branch 0 taken 182003 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 182000 times.
✓ Branch 4 taken 3 times.
182003 res = res && write_byte(v,b);
190 182003 return res;
191 }
192
193 87853 bool f_i32(lbm_flat_value_t *v, int32_t w) {
194 87853 bool res = true;
195
2/4
✓ Branch 0 taken 87853 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 87853 times.
✗ Branch 4 not taken.
87853 res = res && write_byte(v, S_I32_VALUE);
196
3/4
✓ Branch 0 taken 87853 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 87849 times.
✓ Branch 4 taken 4 times.
87853 res = res && write_word(v, (uint32_t)w);
197 87853 return res;
198 }
199
200 93397 bool f_u32(lbm_flat_value_t *v, uint32_t w) {
201 93397 bool res = true;
202
2/4
✓ Branch 0 taken 93397 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 93397 times.
✗ Branch 4 not taken.
93397 res = res && write_byte(v, S_U32_VALUE);
203
3/4
✓ Branch 0 taken 93397 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 93393 times.
✓ Branch 4 taken 4 times.
93397 res = res && write_word(v, w);
204 93397 return res;
205 }
206
207 22960 bool f_float(lbm_flat_value_t *v, float f) {
208 22960 bool res = true;
209
2/4
✓ Branch 0 taken 22960 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 22960 times.
✗ Branch 4 not taken.
22960 res = res && write_byte(v, S_FLOAT_VALUE);
210 uint32_t u;
211 22960 memcpy(&u, &f, sizeof(uint32_t));
212
3/4
✓ Branch 0 taken 22960 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 22952 times.
✓ Branch 4 taken 8 times.
22960 res = res && write_word(v, (uint32_t)u);
213 22960 return res;
214 }
215
216 92 bool f_double(lbm_flat_value_t *v, double d) {
217 92 bool res = true;
218
2/4
✓ Branch 0 taken 92 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 92 times.
✗ Branch 4 not taken.
92 res = res && write_byte(v, S_DOUBLE_VALUE);
219 uint64_t u;
220 92 memcpy(&u, &d, sizeof(uint64_t));
221
3/4
✓ Branch 0 taken 92 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 85 times.
✓ Branch 4 taken 7 times.
92 res = res && write_dword(v, u);
222 92 return res;
223 }
224
225 95 bool f_i64(lbm_flat_value_t *v, int64_t w) {
226 95 bool res = true;
227
3/4
✓ Branch 0 taken 95 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 94 times.
✓ Branch 4 taken 1 time.
95 res = res && write_byte(v, S_I64_VALUE);
228
4/4
✓ Branch 0 taken 94 times.
✓ Branch 1 taken 1 time.
✓ Branch 3 taken 87 times.
✓ Branch 4 taken 7 times.
95 res = res && write_dword(v, (uint64_t)w);
229 95 return res;
230 }
231
232 89 bool f_u64(lbm_flat_value_t *v, uint64_t w) {
233 89 bool res = true;
234
2/4
✓ Branch 0 taken 89 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 89 times.
✗ Branch 4 not taken.
89 res = res && write_byte(v, S_U64_VALUE);
235
3/4
✓ Branch 0 taken 89 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 84 times.
✓ Branch 4 taken 5 times.
89 res = res && write_dword(v, w);
236 89 return res;
237 }
238
239 // num_bytes is specifically an uint32_t
240 356357 bool f_lbm_array(lbm_flat_value_t *v, uint32_t num_bytes, uint8_t *data) {
241 356357 bool res = write_byte(v, S_LBM_ARRAY);
242
3/4
✓ Branch 0 taken 356357 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 356356 times.
✓ Branch 4 taken 1 time.
356357 res = res && write_word(v, num_bytes);
243
4/4
✓ Branch 0 taken 356356 times.
✓ Branch 1 taken 1 time.
✓ Branch 3 taken 356353 times.
✓ Branch 4 taken 3 times.
356357 res = res && write_bytes(v, data, num_bytes);
244 356357 return res;
245 }
246
247 static int flatten_maximum_depth = FLATTEN_VALUE_MAXIMUM_DEPTH;
248
249 92 void lbm_set_max_flatten_depth(int depth) {
250 92 flatten_maximum_depth = depth;
251 92 }
252
253 8 int lbm_get_max_flatten_depth(void) {
254 8 return flatten_maximum_depth;
255 }
256
257 87 static void flatten_error(jmp_buf jb, int val) {
258 87 longjmp(jb, val);
259 }
260
261 /* Use of setjmp/longjmp in flatten_value_size
262
263 setjmp/longjmp behavior is undefined:
264 * If the function which called setjmp() returns before longjmp() is
265 called, the behavior is undefined. Some kind of subtle or unsubtle
266 chaos is sure to result.
267
268 * If, in a multithreaded program, a longjmp() call employs an env buffer
269 that was initialized by a call to setjmp() in a different thread, the
270 behavior is undefined.
271
272 The use of setjmp/longjmp in in flatten_value_size_internal/flatten_value_size
273 is avoiding undefined behviour by:
274 1. flatten_value_size_internal is static.
275 2. flatten_error is static.
276 3. flatten_error is ONLY allowed to be called from flatten_value_size_internal.
277 4. The jmpbuf is created in flatten_value_size, which is exposed API.
278 5. Only flatten_value_size calls flatten_value_size_internal.
279
280 Changes to the flat value code MUST NOT change 1 - 5 properties mentioned
281 above.
282 */
283
284 1990786 static int flatten_value_size_internal(jmp_buf jb, lbm_value v, int depth, bool image) {
285
2/2
✓ Branch 0 taken 87 times.
✓ Branch 1 taken 1990699 times.
1990786 if (depth > flatten_maximum_depth) {
286 87 flatten_error(jb, FLATTEN_VALUE_ERROR_MAXIMUM_DEPTH);
287 }
288
289 1990699 lbm_uint t = lbm_type_of(v);
290
3/4
✓ Branch 0 taken 1421752 times.
✓ Branch 1 taken 568947 times.
✓ Branch 2 taken 1421752 times.
✗ Branch 3 not taken.
1990699 if (t >= LBM_POINTER_TYPE_FIRST && t < LBM_POINTER_TYPE_LAST) {
291 // Clear constant bit, it is irrelevant to flattening
292 1421752 t = t & ~(LBM_PTR_TO_CONSTANT_BIT);
293 }
294
295
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 1990699 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
1990699 if (image && lbm_is_ptr(v) && (v & LBM_PTR_TO_CONSTANT_BIT)) {
296 // If flattening to image, constants can be stored by reference.
297 ✗ return (sizeof(lbm_uint) + 1); // one byte tag, one word ptr
298 }
299
300
8/9
✓ Branch 0 taken 944059 times.
✓ Branch 1 taken 336 times.
✓ Branch 2 taken 182003 times.
✓ Branch 3 taken 137188 times.
✓ Branch 4 taken 181418 times.
✓ Branch 5 taken 257 times.
✓ Branch 6 taken 189248 times.
✓ Branch 7 taken 356190 times.
✗ Branch 8 not taken.
1990699 switch (t) {
301 944059 case LBM_TYPE_CONS: {
302 944059 int res = 0;
303 944059 int s1 = flatten_value_size_internal(jb,lbm_car(v), depth + 1, image);
304
1/2
✓ Branch 0 taken 941789 times.
✗ Branch 1 not taken.
941789 if (s1 > 0) {
305 941789 int s2 = flatten_value_size_internal(jb,lbm_cdr(v), depth + 1, image);
306
1/2
✓ Branch 0 taken 941789 times.
✗ Branch 1 not taken.
941789 if (s2 > 0) {
307 941789 res = (1 + s1 + s2);
308 }
309 }
310 941789 return res;
311 }
312 336 case LBM_TYPE_LISPARRAY: {
313 336 int sum = 4 + 1; // sizeof(uint32_t) + 1;
314 336 lbm_array_header_t *header = (lbm_array_header_t*)lbm_car(v);
315
1/2
✓ Branch 0 taken 336 times.
✗ Branch 1 not taken.
336 if (header) {
316 336 lbm_value *arrdata = (lbm_value*)header->data;
317 336 lbm_uint size = header->size / sizeof(lbm_value);
318
2/2
✓ Branch 0 taken 1092 times.
✓ Branch 1 taken 336 times.
1428 for (lbm_uint i = 0; i < size; i ++ ) {
319 1092 sum += flatten_value_size_internal(jb, arrdata[i], depth + 1, image);
320 }
321 } else {
322 ✗ flatten_error(jb, FLATTEN_VALUE_ERROR_ARRAY);
323 }
324 336 return sum;
325 }
326 182003 case LBM_TYPE_BYTE:
327 182003 return 1 + 1;
328 137188 case LBM_TYPE_U: /* fall through */
329 case LBM_TYPE_I:
330 #ifndef LBM64
331 91506 return 1 + 4;
332 #else
333 45682 return 1 + 8;
334 #endif
335 181418 case LBM_TYPE_U32: /* fall through */
336 case LBM_TYPE_I32:
337 case LBM_TYPE_FLOAT:
338 181418 return 1 + 4;
339 257 case LBM_TYPE_U64: /* fall through */
340 case LBM_TYPE_I64:
341 case LBM_TYPE_DOUBLE:
342 257 return 1 + 8;
343 189248 case LBM_TYPE_SYMBOL: {
344
1/2
✓ Branch 0 taken 189248 times.
✗ Branch 1 not taken.
189248 if (!image) {
345 189248 int s = f_sym_string_bytes(v);
346
1/2
✓ Branch 0 taken 189248 times.
✗ Branch 1 not taken.
189248 if (s > 0) return 1 + s;
347 ✗ flatten_error(jb, (int)s);
348 } else {
349 ✗ return 1 + sizeof(lbm_uint);
350 }
351 ✗ } return 0; // already terminated with error
352 356190 case LBM_TYPE_ARRAY: {
353 // Platform dependent size.
354 // TODO: Something needs to be done to these inconsistencies.
355 356190 lbm_int s = lbm_heap_array_get_size(v);
356
1/2
✓ Branch 0 taken 356190 times.
✗ Branch 1 not taken.
356190 if (s > 0)
357 356190 return 1 + 4 + (int)s;
358 ✗ flatten_error(jb, (int)s);
359 ✗ } return 0; // already terminated with error
360 ✗ default:
361 ✗ return FLATTEN_VALUE_ERROR_CANNOT_BE_FLATTENED;
362 }
363 }
364
365 103846 int flatten_value_size(lbm_value v, bool image) {
366 jmp_buf jb;
367 103846 int r = setjmp(jb);
368
2/2
✓ Branch 0 taken 87 times.
✓ Branch 1 taken 103846 times.
103933 if (r != 0) {
369 87 return r;
370 }
371 103846 return flatten_value_size_internal(jb, v, 0, image);
372 }
373
374 1988520 int flatten_value_c(lbm_flat_value_t *fv, lbm_value v) {
375
376 1988520 lbm_uint t = lbm_type_of(v);
377
3/4
✓ Branch 0 taken 1419556 times.
✓ Branch 1 taken 568964 times.
✓ Branch 2 taken 1419556 times.
✗ Branch 3 not taken.
1988520 if (t >= LBM_POINTER_TYPE_FIRST && t < LBM_POINTER_TYPE_LAST) {
378 // Clear constant bit, it is irrelevant to flattening
379 1419556 t = t & ~(LBM_PTR_TO_CONSTANT_BIT);
380 }
381
382
13/14
✓ Branch 0 taken 941826 times.
✓ Branch 1 taken 336 times.
✓ Branch 2 taken 182003 times.
✓ Branch 3 taken 88 times.
✓ Branch 4 taken 137121 times.
✓ Branch 5 taken 93397 times.
✓ Branch 6 taken 87853 times.
✓ Branch 7 taken 89 times.
✓ Branch 8 taken 95 times.
✓ Branch 9 taken 185 times.
✓ Branch 10 taken 92 times.
✓ Branch 11 taken 189246 times.
✓ Branch 12 taken 356189 times.
✗ Branch 13 not taken.
1988520 switch (t) {
383 941826 case LBM_TYPE_CONS: {
384 941826 bool res = true;
385
3/4
✓ Branch 0 taken 941826 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 941824 times.
✓ Branch 4 taken 2 times.
941826 res = res && f_cons(fv);
386
2/2
✓ Branch 0 taken 941824 times.
✓ Branch 1 taken 2 times.
941826 if (res) {
387 941824 int fv_r = flatten_value_c(fv, lbm_car(v));
388
2/2
✓ Branch 0 taken 941797 times.
✓ Branch 1 taken 27 times.
941824 if (fv_r == FLATTEN_VALUE_OK) {
389 941797 fv_r = flatten_value_c(fv, lbm_cdr(v));
390 }
391 941824 return fv_r;
392 }
393 2 }break;
394 336 case LBM_TYPE_LISPARRAY: {
395 336 lbm_array_header_t *header = (lbm_array_header_t*)lbm_car(v);
396
1/2
✓ Branch 0 taken 336 times.
✗ Branch 1 not taken.
336 if (header) {
397 336 lbm_value *arrdata = (lbm_value*)header->data;
398 // always exact multiple of sizeof(lbm_value)
399 336 uint32_t size = (uint32_t)(header->size / sizeof(lbm_value));
400
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 336 times.
336 if (!f_lisp_array(fv, size)) return FLATTEN_VALUE_ERROR_NOT_ENOUGH_MEMORY;
401 336 int fv_r = FLATTEN_VALUE_OK;
402
2/2
✓ Branch 0 taken 1092 times.
✓ Branch 1 taken 336 times.
1428 for (lbm_uint i = 0; i < size; i ++ ) {
403 1092 fv_r = flatten_value_c(fv, arrdata[i]);
404
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1092 times.
1092 if (fv_r != FLATTEN_VALUE_OK) {
405 ✗ break;
406 }
407 }
408 336 return fv_r;
409 } else {
410 ✗ return FLATTEN_VALUE_ERROR_ARRAY;
411 }
412 } break;
413 182003 case LBM_TYPE_BYTE:
414
2/2
✓ Branch 2 taken 182000 times.
✓ Branch 3 taken 3 times.
182003 if (f_b(fv, (uint8_t)lbm_dec_as_char(v))) {
415 182000 return FLATTEN_VALUE_OK;
416 }
417 3 break;
418 88 case LBM_TYPE_U:
419
2/2
✓ Branch 2 taken 84 times.
✓ Branch 3 taken 4 times.
88 if (f_u(fv, lbm_dec_u(v))) {
420 84 return FLATTEN_VALUE_OK;
421 }
422 4 break;
423 137121 case LBM_TYPE_I:
424
2/2
✓ Branch 2 taken 137107 times.
✓ Branch 3 taken 14 times.
137121 if (f_i(fv, lbm_dec_i(v))) {
425 137107 return FLATTEN_VALUE_OK;
426 }
427 14 break;
428 93397 case LBM_TYPE_U32:
429
2/2
✓ Branch 2 taken 93393 times.
✓ Branch 3 taken 4 times.
93397 if (f_u32(fv, lbm_dec_as_u32(v))) {
430 93393 return FLATTEN_VALUE_OK;
431 }
432 4 break;
433 87853 case LBM_TYPE_I32:
434
2/2
✓ Branch 2 taken 87849 times.
✓ Branch 3 taken 4 times.
87853 if (f_i32(fv, lbm_dec_as_i32(v))) {
435 87849 return FLATTEN_VALUE_OK;
436 }
437 4 break;
438 89 case LBM_TYPE_U64:
439
2/2
✓ Branch 2 taken 84 times.
✓ Branch 3 taken 5 times.
89 if (f_u64(fv, lbm_dec_as_u64(v))) {
440 84 return FLATTEN_VALUE_OK;
441 }
442 5 break;
443 95 case LBM_TYPE_I64:
444
2/2
✓ Branch 2 taken 87 times.
✓ Branch 3 taken 8 times.
95 if (f_i64(fv, lbm_dec_as_i64(v))) {
445 87 return FLATTEN_VALUE_OK;
446 }
447 8 break;
448 185 case LBM_TYPE_FLOAT:
449
2/2
✓ Branch 2 taken 177 times.
✓ Branch 3 taken 8 times.
185 if (f_float(fv, lbm_dec_as_float(v))) {
450 177 return FLATTEN_VALUE_OK;
451 }
452 8 break;
453 92 case LBM_TYPE_DOUBLE:
454
2/2
✓ Branch 2 taken 85 times.
✓ Branch 3 taken 7 times.
92 if (f_double(fv, lbm_dec_as_double(v))) {
455 85 return FLATTEN_VALUE_OK;
456 }
457 7 break;
458 189246 case LBM_TYPE_SYMBOL: {
459 189246 char *sym_str = (char*)lbm_get_name_by_symbol(lbm_dec_sym(v));
460
2/2
✓ Branch 1 taken 189241 times.
✓ Branch 2 taken 5 times.
189246 if (f_sym_string(fv, sym_str)) {
461 189241 return FLATTEN_VALUE_OK;
462 }
463 5 } break;
464 356189 case LBM_TYPE_ARRAY: {
465 356189 lbm_int s = lbm_heap_array_get_size(v);
466 356189 const uint8_t *d = lbm_heap_array_get_data_ro(v);
467
2/4
✓ Branch 0 taken 356189 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 356189 times.
✗ Branch 3 not taken.
356189 if (s > 0 && d != NULL) {
468
2/2
✓ Branch 1 taken 356185 times.
✓ Branch 2 taken 4 times.
356189 if (f_lbm_array(fv, (uint32_t)s, (uint8_t*)d)) {
469 356185 return FLATTEN_VALUE_OK;
470 }
471 } else {
472 ✗ return FLATTEN_VALUE_ERROR_ARRAY;
473 }
474 4 }break;
475 ✗ default:
476 ✗ return FLATTEN_VALUE_ERROR_CANNOT_BE_FLATTENED;
477 }
478 68 return FLATTEN_VALUE_ERROR_BUFFER_TOO_SMALL;
479 }
480
481 84 lbm_value handle_flatten_error(int err_val) {
482
1/5
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 84 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
84 switch (err_val) {
483 ✗ case FLATTEN_VALUE_ERROR_CANNOT_BE_FLATTENED:
484 ✗ return ENC_SYM_EERROR;
485 ✗ case FLATTEN_VALUE_ERROR_BUFFER_TOO_SMALL: /* fall through */
486 case FLATTEN_VALUE_ERROR_FATAL:
487 ✗ return ENC_SYM_FATAL_ERROR;
488 84 case FLATTEN_VALUE_ERROR_CIRCULAR: /* fall through */
489 case FLATTEN_VALUE_ERROR_MAXIMUM_DEPTH:
490 84 return ENC_SYM_EERROR;
491 ✗ case FLATTEN_VALUE_ERROR_ARRAY: /* fall through */
492 case FLATTEN_VALUE_ERROR_NOT_ENOUGH_MEMORY:
493 ✗ return ENC_SYM_MERROR;
494 }
495 ✗ return ENC_SYM_NIL;
496 }
497
498 103832 lbm_value flatten_value(lbm_value v) {
499
500 103832 lbm_value array_cell = lbm_heap_allocate_cell(LBM_TYPE_CONS, ENC_SYM_NIL, ENC_SYM_ARRAY_TYPE);
501
502
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 103812 times.
103832 if (array_cell == ENC_SYM_MERROR) {
503 20 return array_cell;
504 }
505
506 lbm_flat_value_t fv;
507
508 103812 lbm_array_header_t *array = NULL;
509 103812 int required_mem = flatten_value_size(v, false);
510
2/2
✓ Branch 0 taken 103728 times.
✓ Branch 1 taken 84 times.
103812 if (required_mem > 0) {
511 103728 array = (lbm_array_header_t *)lbm_malloc(sizeof(lbm_array_header_t));
512
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 103727 times.
103728 if (array == NULL) {
513 1 lbm_set_car_and_cdr(array_cell, ENC_SYM_NIL, ENC_SYM_NIL);
514 1 return ENC_SYM_MERROR;
515 }
516
517 103727 bool r = lbm_start_flatten(&fv, (lbm_uint)required_mem);
518
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 103727 times.
103727 if (!r) {
519 ✗ lbm_free(array);
520 ✗ lbm_set_car_and_cdr(array_cell, ENC_SYM_NIL, ENC_SYM_NIL);
521 ✗ return ENC_SYM_MERROR;
522 }
523
524
1/2
✓ Branch 1 taken 103727 times.
✗ Branch 2 not taken.
103727 if (flatten_value_c(&fv, v) == FLATTEN_VALUE_OK) {
525 // it would be wasteful to run finish_flatten here.
526 103727 r = true;
527 } else {
528 ✗ r = false;
529 }
530
531
1/2
✓ Branch 0 taken 103727 times.
✗ Branch 1 not taken.
103727 if (r) {
532 // lift flat_value
533 103727 array->data = (lbm_uint*)fv.buf;
534 103727 array->size = fv.buf_size;
535 103727 lbm_set_car(array_cell, (lbm_uint)array);
536 103727 array_cell = lbm_set_ptr_type(array_cell, LBM_TYPE_ARRAY);
537 103727 return array_cell;
538 }
539 }
540 84 lbm_set_car_and_cdr(array_cell, ENC_SYM_NIL, ENC_SYM_NIL);
541 84 return handle_flatten_error(required_mem);
542 }
543
544 // ------------------------------------------------------------
545 // Unflattening
546 184047 static bool extract_byte(lbm_flat_value_t *v, uint8_t *r) {
547
1/2
✓ Branch 0 taken 184047 times.
✗ Branch 1 not taken.
184047 if (v->buf_size >= v->buf_pos + 1) {
548 184047 *r = v->buf[v->buf_pos++];
549 184047 return true;
550 }
551 ✗ return false;
552 }
553
554 667282 static bool extract_word(lbm_flat_value_t *v, uint32_t *r) {
555 667282 bool res = false;
556
1/2
✓ Branch 0 taken 667282 times.
✗ Branch 1 not taken.
667282 if (v->buf_size >= v->buf_pos + 4) {
557 667282 uint32_t tmp = 0;
558 667282 tmp |= (lbm_value)v->buf[v->buf_pos++];
559 667282 tmp = tmp << 8 | (uint32_t)v->buf[v->buf_pos++];
560 667282 tmp = tmp << 8 | (uint32_t)v->buf[v->buf_pos++];
561 667282 tmp = tmp << 8 | (uint32_t)v->buf[v->buf_pos++];
562 667282 *r = tmp;
563 667282 res = true;
564 }
565 667282 return res;
566 }
567
568 49133 static bool extract_dword(lbm_flat_value_t *v, uint64_t *r) {
569 49133 bool res = false;
570
1/2
✓ Branch 0 taken 49133 times.
✗ Branch 1 not taken.
49133 if (v->buf_size >= v->buf_pos + 8) {
571 49133 uint64_t tmp = 0;
572 49133 tmp |= (lbm_value)v->buf[v->buf_pos++];
573 49133 tmp = tmp << 8 | (uint64_t)v->buf[v->buf_pos++];
574 49133 tmp = tmp << 8 | (uint64_t)v->buf[v->buf_pos++];
575 49133 tmp = tmp << 8 | (uint64_t)v->buf[v->buf_pos++];
576 49133 tmp = tmp << 8 | (uint64_t)v->buf[v->buf_pos++];
577 49133 tmp = tmp << 8 | (uint64_t)v->buf[v->buf_pos++];
578 49133 tmp = tmp << 8 | (uint64_t)v->buf[v->buf_pos++];
579 49133 tmp = tmp << 8 | (uint64_t)v->buf[v->buf_pos++];
580 49133 *r = tmp;
581 49133 res = true;;
582 }
583 49133 return res;
584 }
585
586 1089506 static int lbm_unflatten_value_atom(lbm_flat_value_t *v, lbm_value *res) {
587
588 1089506 uint8_t curr = v->buf[v->buf_pos++];
589
590
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1089505 times.
1089506 if (v->buf_size <= v->buf_pos) return UNFLATTEN_MALFORMED;
591
592
16/17
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 13970 times.
✓ Branch 3 taken 184047 times.
✓ Branch 4 taken 94481 times.
✓ Branch 5 taken 60 times.
✓ Branch 6 taken 45783 times.
✓ Branch 7 taken 28 times.
✓ Branch 8 taken 18772 times.
✓ Branch 9 taken 95 times.
✓ Branch 10 taken 88248 times.
✓ Branch 11 taken 95004 times.
✓ Branch 12 taken 89 times.
✓ Branch 13 taken 86 times.
✓ Branch 14 taken 359328 times.
✓ Branch 15 taken 189512 times.
✓ Branch 16 taken 1 time.
1089505 switch(curr) {
593 ✗ case S_CONS: {
594 ✗ return UNFLATTEN_MALFORMED;
595 }
596 1 case S_CONSTANT_REF: {
597 lbm_uint tmp;
598 bool b;
599 #ifndef LBM64
600 1 b = extract_word(v, &tmp);
601 #else
602 ✗ b = extract_dword(v, &tmp);
603 #endif
604
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 if (b) {
605 1 *res = tmp;
606 1 return UNFLATTEN_OK;
607 }
608 ✗ return UNFLATTEN_MALFORMED;
609 }
610 13970 case S_SYM_VALUE: {
611 lbm_uint tmp;
612 bool b;
613 #ifndef LBM64
614 10918 b = extract_word(v, &tmp);
615 #else
616 3052 b = extract_dword(v, &tmp);
617 #endif
618
1/2
✓ Branch 0 taken 13970 times.
✗ Branch 1 not taken.
13970 if (b) {
619 13970 *res = lbm_enc_sym(tmp);
620 13970 return UNFLATTEN_OK;
621 }
622 ✗ return UNFLATTEN_MALFORMED;
623 }
624 184047 case S_BYTE_VALUE: {
625 uint8_t tmp;
626 184047 bool b = extract_byte(v, &tmp);
627
1/2
✓ Branch 0 taken 184047 times.
✗ Branch 1 not taken.
184047 if (b) {
628 184047 *res = lbm_enc_char((uint8_t)tmp);
629 184047 return UNFLATTEN_OK;
630 }
631 ✗ return UNFLATTEN_MALFORMED;
632 }
633 94481 case S_I28_VALUE: {
634 uint32_t tmp;
635 bool b;
636 94481 b = extract_word(v, &tmp);
637
1/2
✓ Branch 0 taken 94481 times.
✗ Branch 1 not taken.
94481 if (b) {
638 94481 *res = lbm_enc_i((int32_t)tmp);
639 94481 return UNFLATTEN_OK;
640 }
641 ✗ return UNFLATTEN_MALFORMED;
642 }
643 60 case S_U28_VALUE: {
644 uint32_t tmp;
645 bool b;
646 60 b = extract_word(v, &tmp);
647
1/2
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
60 if (b) {
648 60 *res = lbm_enc_u((uint32_t)tmp);
649 60 return UNFLATTEN_OK;
650 }
651 ✗ return UNFLATTEN_MALFORMED;
652 }
653 45783 case S_I56_VALUE: {
654 uint64_t tmp;
655 bool b;
656 45783 b = extract_dword(v, &tmp);
657
1/2
✓ Branch 0 taken 45783 times.
✗ Branch 1 not taken.
45783 if (b) {
658 #ifndef LBM64
659 ✗ *res = lbm_enc_i64((int64_t)tmp);
660 #else
661 45783 *res = lbm_enc_i((int64_t)tmp);
662 #endif
663 45783 return UNFLATTEN_OK;
664 }
665 ✗ return UNFLATTEN_MALFORMED;
666 }
667 28 case S_U56_VALUE: {
668 uint64_t tmp;
669 bool b;
670 28 b = extract_dword(v, &tmp);
671
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 if (b) {
672 #ifndef LBM64
673 ✗ *res = lbm_enc_u64(tmp);
674 #else
675 28 *res = lbm_enc_u(tmp);
676 #endif
677 28 return UNFLATTEN_OK;
678 }
679 ✗ return UNFLATTEN_MALFORMED;
680 }
681 18772 case S_FLOAT_VALUE: {
682 uint32_t tmp;
683 bool b;
684 18772 b = extract_word(v, &tmp);
685
1/2
✓ Branch 0 taken 18772 times.
✗ Branch 1 not taken.
18772 if (b) {
686 lbm_float f;
687 18772 memcpy(&f, &tmp, sizeof(lbm_float));
688 18772 lbm_value im = lbm_enc_float(f);
689
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 18772 times.
18772 if (lbm_is_symbol_merror(im)) {
690 ✗ return UNFLATTEN_GC_RETRY;
691 }
692 18772 *res = im;
693 18772 return UNFLATTEN_OK;
694 }
695 ✗ return UNFLATTEN_MALFORMED;
696 }
697 95 case S_DOUBLE_VALUE: {
698 uint64_t tmp;
699 bool b;
700 95 b = extract_dword(v, &tmp);
701
1/2
✓ Branch 0 taken 95 times.
✗ Branch 1 not taken.
95 if (b) {
702 double f;
703 95 memcpy(&f, &tmp, sizeof(uint64_t));
704 95 lbm_value im = lbm_enc_double(f);
705
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 95 times.
95 if (lbm_is_symbol_merror(im)) {
706 ✗ return UNFLATTEN_GC_RETRY;
707 }
708 95 *res = im;
709 95 return UNFLATTEN_OK;
710 }
711 ✗ return UNFLATTEN_MALFORMED;
712 }
713 88248 case S_I32_VALUE: {
714 uint32_t tmp;
715
1/2
✓ Branch 1 taken 88248 times.
✗ Branch 2 not taken.
88248 if (extract_word(v, &tmp)) {
716 88248 lbm_value im = lbm_enc_i32((int32_t)tmp);
717
2/2
✓ Branch 1 taken 146 times.
✓ Branch 2 taken 88102 times.
88248 if (lbm_is_symbol_merror(im)) {
718 146 return UNFLATTEN_GC_RETRY;
719 }
720 88102 *res = im;
721 88102 return UNFLATTEN_OK;
722 }
723 ✗ return UNFLATTEN_MALFORMED;
724 }
725 95004 case S_U32_VALUE: {
726 uint32_t tmp;
727
1/2
✓ Branch 1 taken 95004 times.
✗ Branch 2 not taken.
95004 if (extract_word(v, &tmp)) {
728 95004 lbm_value im = lbm_enc_u32(tmp);
729
2/2
✓ Branch 1 taken 48 times.
✓ Branch 2 taken 94956 times.
95004 if (lbm_is_symbol_merror(im)) {
730 48 return UNFLATTEN_GC_RETRY;
731 }
732 94956 *res = im;
733 94956 return UNFLATTEN_OK;
734 }
735 ✗ return UNFLATTEN_MALFORMED;
736 }
737 89 case S_I64_VALUE: {
738 89 uint64_t tmp = 0;
739
1/2
✓ Branch 1 taken 89 times.
✗ Branch 2 not taken.
89 if (extract_dword(v, &tmp)) {
740 89 lbm_value im = lbm_enc_i64((int64_t)tmp);
741
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 89 times.
89 if (lbm_is_symbol_merror(im)) {
742 ✗ return UNFLATTEN_GC_RETRY;
743 }
744 89 *res = im;
745 89 return UNFLATTEN_OK;
746 }
747 ✗ return UNFLATTEN_MALFORMED;
748 }
749 86 case S_U64_VALUE: {
750 86 uint64_t tmp = 0;
751
1/2
✓ Branch 1 taken 86 times.
✗ Branch 2 not taken.
86 if (extract_dword(v, &tmp)) {
752 86 lbm_value im = lbm_enc_u64(tmp);
753
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 86 times.
86 if (lbm_is_symbol_merror(im)) {
754 ✗ return UNFLATTEN_GC_RETRY;
755 }
756 86 *res = im;
757 86 return UNFLATTEN_OK;
758 }
759 ✗ return UNFLATTEN_MALFORMED;
760 }
761 359328 case S_LBM_ARRAY: {
762 uint32_t num_elt;
763 // TODO: Feels slightly wrong with <= here.
764
3/4
✓ Branch 1 taken 359328 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 359323 times.
✓ Branch 4 taken 5 times.
359328 if (extract_word(v, &num_elt) && v->buf_pos + num_elt <= v->buf_size) {
765
2/2
✓ Branch 1 taken 358552 times.
✓ Branch 2 taken 771 times.
359323 if (lbm_heap_allocate_array(res, num_elt)) {
766 358552 lbm_array_header_t *arr = (lbm_array_header_t*)lbm_car(*res);
767 358552 lbm_uint num_bytes = num_elt;
768 358552 memcpy(arr->data, v->buf + v->buf_pos, num_bytes);
769 358552 v->buf_pos += num_bytes;
770 } else {
771 771 return UNFLATTEN_GC_RETRY;
772 }
773 358552 return UNFLATTEN_OK;
774 }
775 5 return UNFLATTEN_MALFORMED;
776 }
777 189512 case S_SYM_STRING: {
778 lbm_uint sym_id;
779 189512 lbm_uint max_bytes = v->buf_size - v->buf_pos;
780 189512 lbm_uint num_bytes = max_bytes;
781 189512 bool found_null = false;
782
2/2
✓ Branch 0 taken 758711 times.
✓ Branch 1 taken 1 time.
758712 for (lbm_uint i = 0; i < max_bytes; i ++) {
783
2/2
✓ Branch 0 taken 189511 times.
✓ Branch 1 taken 569200 times.
758711 if (v->buf[v->buf_pos + i] == 0) {
784 189511 num_bytes = i + 1;
785 189511 found_null = true;
786 189511 break;
787 }
788 }
789
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 189511 times.
189512 if (!found_null) return UNFLATTEN_MALFORMED;
790
1/2
✓ Branch 1 taken 189511 times.
✗ Branch 2 not taken.
189511 if (lbm_add_symbol((char *)(v->buf + v->buf_pos), &sym_id)) {
791 189511 v->buf_pos += num_bytes;
792 189511 *res = lbm_enc_sym(sym_id);
793 189511 return UNFLATTEN_OK;
794 }
795 ✗ return UNFLATTEN_GC_RETRY;
796 }
797 1 default:
798 1 return UNFLATTEN_MALFORMED;
799 }
800 }
801
802 // ////////////////////////////////////////////////////////////
803 // Pointer-reversal-esque "stackless" deserialization of
804 // flattened (serialized) trees.
805 //
806 // Initially:
807 // curr = LBM_NULL; v->buf = { ... }
808 //
809 // FORWARDS PHASE:
810 // Cons case:
811 // Reading conses from the buffer builds a backpointing list.
812 // Placeholder element acts as a 1 bit "visited" field.
813 //
814 // curr = p; v->buf = {S_CONS, ... }
815 // =>
816 // curr = [p, placeholder]; v->buf = { ... }
817 //
818 // Lisp array case:
819 // An Array tag in the buffer leads to the creation of an array
820 // with a backptr in the last element position. Placeholder element
821 // is not needed as LBM-Arrays have a built in index field (used by GC)
822 // that can keep a count of how far along the array we have progressed.
823 //
824 // curr = p; v->buf = {S_LBM_LISP_ARRAY, ... }
825 // =>
826 // curr = [| nil ... p |]; v->buf = { ... }
827 //
828 // Atom case:
829 // Reading an atom triggers a backwards traversal along the backpointer
830 // structure.
831 //
832 // curr = X; v->buf = {any_atom, ... } example integer, string.
833 // =>
834 // val = unflatten_atom(v->buf); v->buf = { ... }
835 //
836 // BACKWARDS PHASE: Start the backwards traversal:
837 //
838 // Case on X
839 // LBM_NULL;
840 // => Done! result = val
841 //
842 // [p, placeholder];
843 // =>
844 // [p, val] Base case. Finishes back traversal.
845 // Go back to FORWARDS PHASE.
846 //
847 //
848 // [p, val0];
849 // =>
850 // tmp = [val0, val]; val = tmp; curr = p; continue backwards with value pointing to recently constructed final subresult.
851 //
852 //
853 // [| a b nil ... p |]
854 // =>
855 // [| a b val ... p |] Base case. Finishes back traversal.
856 // Array internal index field keeps track of write position.
857 // Go back to FORWARDS PHASE.
858 //
859 //
860 // [| a0 a1 ... an p |]
861 // =>
862 // tmp = [| a0 a1 ... an val |]; val = tmp; curr = p; continue backwards
863 //
864
865 115907 static int lbm_unflatten_value_nostack(sharing_table *st, lbm_uint *target_map, lbm_flat_value_t *v, lbm_value *res) {
866 115907 bool done = false;
867 lbm_value val0;
868 115907 lbm_value curr = lbm_enc_cons_ptr(LBM_PTR_NULL);
869 #if DEBUG
870 char buf[256];
871 #endif
872
1/2
✓ Branch 0 taken 2065103 times.
✗ Branch 1 not taken.
2065103 while (!done) {
873 2065103 int32_t set_ix = -1;
874
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 2065060 times.
2065103 if (v->buf[v->buf_pos] == S_SHARED) {
875 43 v->buf_pos++;
876
2/4
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 43 times.
✗ Branch 3 not taken.
86 if (st && target_map) {
877 43 bool b = false;
878 lbm_uint tmp;
879 #ifndef LBM64
880 43 b = extract_word(v, &tmp);
881 #else
882 ✗ b = extract_dword(v, &tmp);
883 #endif
884
1/2
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
43 if (b) {
885 43 int32_t ix = sharing_table_contains(st, tmp);
886 #if DEBUG
887 printf("UNFLATTEN S_SHARED: addr %x -> sharing_table index %d\n", (unsigned int)tmp, ix);
888 #endif
889
1/2
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
43 if (ix >= 0) {
890 43 set_ix = ix;
891 } else {
892 ✗ return UNFLATTEN_SHARING_TABLE_ERROR;
893 }
894 } else {
895 ✗ return UNFLATTEN_MALFORMED;
896 }
897 } else {
898 ✗ return UNFLATTEN_SHARING_TABLE_REQUIRED;
899 }
900 }
901
902 2065103 bool is_leaf = true;
903 2065103 lbm_value unflattened = ENC_SYM_NIL;
904
905
2/2
✓ Branch 0 taken 975169 times.
✓ Branch 1 taken 1089934 times.
2065103 if (v->buf[v->buf_pos] == S_CONS) {
906 975169 lbm_value tmp = curr;
907 975169 curr = lbm_cons(tmp, ENC_SYM_PLACEHOLDER);
908
2/2
✓ Branch 1 taken 716 times.
✓ Branch 2 taken 974453 times.
975169 if (lbm_is_symbol_merror(curr)) return UNFLATTEN_GC_RETRY;
909
910
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 974415 times.
974453 if (set_ix >= 0) {
911 #if DEBUG
912 lbm_print_value(buf,256,curr);
913 printf("target_map[%d] = %s\n", set_ix, buf);
914 #endif
915 38 target_map[set_ix] = curr;
916 }
917 974453 v->buf_pos ++;
918 974453 is_leaf = false;
919
2/2
✓ Branch 0 taken 359 times.
✓ Branch 1 taken 1089575 times.
1089934 } else if (v->buf[v->buf_pos] == S_LBM_LISP_ARRAY) {
920 uint32_t size;
921 359 v->buf_pos ++;
922 359 bool b = extract_word(v, &size);
923
1/2
✓ Branch 0 taken 359 times.
✗ Branch 1 not taken.
359 if (b) {
924 // Abort if buffer cannot possibly hold that size array.
925 // a flattened byte occupies 2 bytes in fv. so smallest possible
926 // array is array of bytes.
927
4/4
✓ Branch 0 taken 357 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 354 times.
359 if (size > 0 && v->buf_pos + (size * 2) > v->buf_size) return UNFLATTEN_MALFORMED;
928 lbm_value array;
929 356 lbm_heap_allocate_lisp_array(&array, size);
930 356 lbm_array_header_extended_t *header = (lbm_array_header_extended_t*)lbm_car(array);
931
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 354 times.
356 if (size == 0) {
932 2 unflattened = array;
933
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (set_ix >= 0) target_map[set_ix] = array;
934 } else {
935 354 is_leaf = false;
936 354 lbm_value *arrdata = (lbm_value*)header->data;
937
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 354 times.
354 if (lbm_is_symbol_merror(array)) return UNFLATTEN_GC_RETRY;
938 354 header->index = 0;
939 354 arrdata[size-1] = curr; // backptr
940 354 curr = array;
941
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 349 times.
354 if (set_ix >= 0) target_map[set_ix] = curr;
942 }
943 } else {
944 ✗ return UNFLATTEN_MALFORMED;
945 }
946
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1089574 times.
1089575 } else if (v->buf[v->buf_pos] == 0) {
947 1 return UNFLATTEN_MALFORMED;
948
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 1089506 times.
1089574 } else if (v->buf[v->buf_pos] == S_REF) {
949 68 v->buf_pos++;
950
2/4
✓ Branch 0 taken 68 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 68 times.
✗ Branch 3 not taken.
136 if (st && target_map) {
951 68 bool b = false;
952 lbm_uint tmp;
953 #ifndef LBM64
954 68 b = extract_word(v, &tmp);
955 #else
956 ✗ b = extract_dword(v, &tmp);
957 #endif
958
1/2
✓ Branch 0 taken 68 times.
✗ Branch 1 not taken.
68 if (b) {
959 // Shared should have been hit before S_REF. So just look up index and copy from
960 // the target_map.
961 68 int32_t ix = sharing_table_contains(st, tmp);
962 #if DEBUG
963 printf("UNFLATTEN S_REF: addr %x -> sharing_table index %d\n", (unsigned int)tmp, ix);
964 #endif
965
1/2
✓ Branch 0 taken 68 times.
✗ Branch 1 not taken.
68 if (ix >= 0) {
966 //curr = target_map[ix];
967 68 unflattened = target_map[ix];
968 #if DEBUG
969 lbm_print_value(buf,256, unflattened);
970 printf("read %s from target_map[%d]\n", buf, ix);
971 lbm_print_value(buf,256, curr);
972 printf("curr is currently: %s\n", buf);
973 printf("this is a %s\n", is_leaf ? "leaf" : "internal node");
974 #endif
975 } else {
976 ✗ return UNFLATTEN_SHARING_TABLE_ERROR;
977 }
978 } else {
979 ✗ return UNFLATTEN_MALFORMED;
980 }
981 } else {
982 ✗ return UNFLATTEN_SHARING_TABLE_REQUIRED;
983 }
984 } else {
985 1089506 int e_val = lbm_unflatten_value_atom(v, &unflattened);
986 #if DEBUG
987 lbm_print_value(buf,256, unflattened);
988 printf("atom: %s\n", buf);
989 #endif
990
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1089506 times.
1089506 if (set_ix >= 0) {
991 ✗ target_map[set_ix] = unflattened;
992 }
993
2/2
✓ Branch 0 taken 973 times.
✓ Branch 1 taken 1088533 times.
1089506 if (e_val != UNFLATTEN_OK) {
994 973 return e_val;
995 }
996 }
997
998
2/2
✓ Branch 0 taken 1088603 times.
✓ Branch 1 taken 974807 times.
2063410 if (is_leaf) {
999 1088603 val0 = unflattened;
1000
4/4
✓ Branch 1 taken 1941777 times.
✓ Branch 2 taken 114214 times.
✓ Branch 3 taken 968289 times.
✓ Branch 4 taken 973488 times.
3997768 while (lbm_dec_ptr(curr) != LBM_PTR_NULL &&
1001 1941777 lbm_cdr(curr) != ENC_SYM_PLACEHOLDER) { // has done left
1002
2/2
✓ Branch 1 taken 1255 times.
✓ Branch 2 taken 967034 times.
968289 if ( lbm_type_of(curr) == LBM_TYPE_LISPARRAY) {
1003 1255 lbm_array_header_extended_t *header = (lbm_array_header_extended_t*)lbm_car(curr);
1004 1255 lbm_value *arrdata = (lbm_value*)header->data;
1005 1255 uint32_t arrlen = (uint32_t)(header->size / sizeof(lbm_value));
1006
2/2
✓ Branch 0 taken 354 times.
✓ Branch 1 taken 901 times.
1255 if (header->index == arrlen - 1) {
1007 354 lbm_value prev = arrdata[arrlen-1];
1008 354 header->index = 0;
1009 354 arrdata[arrlen-1] = val0;
1010 354 val0 = curr;
1011 354 curr = prev;
1012 } else {
1013 901 arrdata[header->index++] = val0;
1014 901 break;
1015 }
1016 } else {
1017 967034 lbm_value prev = lbm_car(curr);
1018 967034 lbm_value r0 = lbm_cdr(curr);
1019 967034 lbm_set_cdr(curr, val0);
1020 967034 lbm_set_car(curr, r0);
1021 967034 val0 = curr;
1022 967034 curr = prev;
1023 }
1024 }
1025
2/2
✓ Branch 1 taken 114214 times.
✓ Branch 2 taken 974389 times.
1088603 if (lbm_dec_ptr(curr) == LBM_PTR_NULL) {
1026 114214 *res = val0; // done
1027 114214 break;
1028
2/2
✓ Branch 1 taken 973488 times.
✓ Branch 2 taken 901 times.
974389 } else if (lbm_type_of(curr) == LBM_TYPE_LISPARRAY) {
1029 // Do nothing in this case. It has been arranged..
1030
1/2
✓ Branch 1 taken 973488 times.
✗ Branch 2 not taken.
973488 } else if (lbm_cdr(curr) == ENC_SYM_PLACEHOLDER) {
1031 973488 lbm_set_cdr(curr, val0);
1032 #if DEBUG
1033 lbm_print_value(buf,256, curr);
1034 printf("curr: %s\n", buf);
1035 #endif
1036 } else {
1037 ✗ return UNFLATTEN_MALFORMED;
1038 }
1039 }
1040 }
1041 114214 return UNFLATTEN_OK;
1042 }
1043
1044 /* lbm_unflatten_value_nostack, does not backtrack
1045 upon error to swap pointer to the correct direction
1046 and to remove the LBM_PTR_NULL tag.
1047
1048 unflatten_value_nostack does not alter the GC_FLAG or the GC_MARK,
1049 So really only reverse pointers and the NIL tag could be
1050 potential problems.
1051 */
1052 114123 bool lbm_unflatten_value(lbm_flat_value_t *v, lbm_value *res) {
1053 114123 bool b = false;
1054 #ifdef LBM_ALWAYS_GC
1055 lbm_perform_gc();
1056 #endif
1057 114123 int r = lbm_unflatten_value_nostack(NULL,NULL, v,res);
1058
2/2
✓ Branch 0 taken 1681 times.
✓ Branch 1 taken 112442 times.
114123 if (r == UNFLATTEN_GC_RETRY) {
1059 1681 lbm_perform_gc();
1060 1681 v->buf_pos = 0;
1061 1681 r = lbm_unflatten_value_nostack(NULL,NULL,v,res);
1062 }
1063
2/3
✓ Branch 0 taken 114111 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
114123 switch(r) {
1064 114111 case UNFLATTEN_OK:
1065 114111 b = true;
1066 114111 break;
1067 ✗ case UNFLATTEN_GC_RETRY:
1068 ✗ *res = ENC_SYM_MERROR;
1069 ✗ break;
1070 12 default:
1071 12 *res = ENC_SYM_EERROR;
1072 12 break;
1073 }
1074 // Do not free the flat value buffer here.
1075 // there are 2 cases:
1076 // 1: unflatten was called from lisp code -> GC removes the buffer.
1077 // 2: unflatten called from event processing -> event processor frees buffer.
1078 114123 return b;
1079 }
1080
1081 103 bool lbm_unflatten_value_sharing(sharing_table *st, lbm_uint *target_map, lbm_flat_value_t *v, lbm_value *res) {
1082 103 bool b = false;
1083 #ifdef LBM_ALWAYS_GC
1084 lbm_perform_gc();
1085 #endif
1086 103 int r = lbm_unflatten_value_nostack(st,target_map, v,res);
1087
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 103 times.
103 if (r == UNFLATTEN_GC_RETRY) {
1088 ✗ lbm_perform_gc();
1089 ✗ v->buf_pos = 0;
1090 ✗ r = lbm_unflatten_value_nostack(st,target_map,v,res);
1091 }
1092
1/3
✓ Branch 0 taken 103 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
103 switch(r) {
1093 103 case UNFLATTEN_OK:
1094 103 b = true;
1095 103 break;
1096 ✗ case UNFLATTEN_GC_RETRY:
1097 ✗ *res = ENC_SYM_MERROR;
1098 ✗ break;
1099 ✗ default:
1100 ✗ *res = ENC_SYM_EERROR;
1101 ✗ break;
1102 }
1103 // Do not free the flat value buffer here.
1104 // there are 2 cases:
1105 // 1: unflatten was called from lisp code -> GC removes the buffer.
1106 // 2: unflatten called from event processing -> event processor frees buffer.
1107 103 return b;
1108 }
1109