GCC Code Coverage Report


Directory: ../src/
File: lbm_flat_value.c
Date: 2026-10-16 14:49:47
Coverage Exec Excl Total
Lines: 88.1% 550 0 624
Functions: 100.0% 36 0 36
Branches: 71.8% 301 0 419

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