GCC Code Coverage Report


Directory: ../src/
File: /home/joels/Current/lispbm/src/lbm_flat_value.c
Date: 2025-10-27 19:12:55
Exec Total Coverage
Lines: 509 605 84.1%
Functions: 35 36 97.2%
Branches: 237 389 60.9%

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 40655 bool lbm_start_flatten(lbm_flat_value_t *v, size_t buffer_size) {
37 40655 bool res = false;
38 40655 uint8_t *data = lbm_malloc_reserve(buffer_size);
39
1/2
✓ Branch 0 taken 40655 times.
✗ Branch 1 not taken.
40655 if (data) {
40 40655 v->buf = data;
41 40655 v->buf_size = buffer_size;
42 40655 v->buf_pos = 0;
43 40655 res = true;
44 }
45 40655 return res;
46 }
47
48 6088 bool lbm_finish_flatten(lbm_flat_value_t *v) {
49 lbm_uint size_words;
50
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6088 times.
6088 if (v->buf_pos % sizeof(lbm_uint) == 0) {
51 size_words = v->buf_pos / sizeof(lbm_uint);
52 } else {
53 6088 size_words = (v->buf_pos / sizeof(lbm_uint)) + 1;
54 }
55
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 6060 times.
6088 if (v->buf_size <= size_words * sizeof(lbm_uint)) return true;
56 6060 v->buf_size = size_words * sizeof(lbm_uint);
57 6060 return (lbm_memory_shrink((lbm_uint*)v->buf, size_words) >= 0);
58 }
59
60 421549 static bool write_byte(lbm_flat_value_t *v, uint8_t b) {
61 421549 bool res = false;
62
1/2
✓ Branch 0 taken 421549 times.
✗ Branch 1 not taken.
421549 if (v->buf_size >= v->buf_pos + 1) {
63 421549 v->buf[v->buf_pos++] = b;
64 421549 res = true;
65 }
66 421549 return res;
67 }
68
69 181861 static bool write_bytes(lbm_flat_value_t *v, uint8_t *data,lbm_uint num_bytes) {
70 181861 bool res = false;
71
1/2
✓ Branch 0 taken 181861 times.
✗ Branch 1 not taken.
181861 if (v->buf_size >= v->buf_pos + num_bytes) {
72 181861 memcpy(v->buf + v->buf_pos, data, num_bytes);
73 181861 v->buf_pos += num_bytes;
74 181861 res = true;
75 }
76 181861 return res;
77 }
78
79 237056 static bool write_word(lbm_flat_value_t *v, uint32_t w) {
80 237056 bool res = false;
81
1/2
✓ Branch 0 taken 237056 times.
✗ Branch 1 not taken.
237056 if (v->buf_size >= v->buf_pos + 4) {
82 237056 v->buf[v->buf_pos++] = (uint8_t)(w >> 24);
83 237056 v->buf[v->buf_pos++] = (uint8_t)(w >> 16);
84 237056 v->buf[v->buf_pos++] = (uint8_t)(w >> 8);
85 237056 v->buf[v->buf_pos++] = (uint8_t)w;
86 237056 res = true;
87 }
88 237056 return res;
89 }
90
91 84 static bool write_dword(lbm_flat_value_t *v, uint64_t w) {
92 84 bool res = false;
93
1/2
✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
84 if (v->buf_size >= v->buf_pos + 8) {
94 84 v->buf[v->buf_pos++] = (uint8_t)(w >> 56);
95 84 v->buf[v->buf_pos++] = (uint8_t)(w >> 48);
96 84 v->buf[v->buf_pos++] = (uint8_t)(w >> 40);
97 84 v->buf[v->buf_pos++] = (uint8_t)(w >> 32);
98 84 v->buf[v->buf_pos++] = (uint8_t)(w >> 24);
99 84 v->buf[v->buf_pos++] = (uint8_t)(w >> 16);
100 84 v->buf[v->buf_pos++] = (uint8_t)(w >> 8);
101 84 v->buf[v->buf_pos++] = (uint8_t)w;
102 84 res = true;
103 }
104 84 return res;
105 }
106
107 319882 bool f_cons(lbm_flat_value_t *v) {
108 319882 bool res = false;
109
1/2
✓ Branch 0 taken 319882 times.
✗ Branch 1 not taken.
319882 if (v->buf_size >= v->buf_pos + 1) {
110 319882 v->buf[v->buf_pos++] = S_CONS;
111 319882 res = true;
112 }
113 319882 return res;
114 }
115
116 112 bool f_lisp_array(lbm_flat_value_t *v, uint32_t size) {
117 // arrays are smaller than 2^32 elements long
118 112 bool res = true;
119
2/4
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 112 times.
✗ Branch 3 not taken.
112 res = res && write_byte(v, S_LBM_LISP_ARRAY);
120
2/4
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 112 times.
✗ Branch 3 not taken.
112 res = res && write_word(v, size); // number of elements.
121 112 return res;
122 }
123
124 4842 bool f_sym(lbm_flat_value_t *v, lbm_uint sym_id) {
125 4842 bool res = true;
126
2/4
✓ Branch 0 taken 4842 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4842 times.
✗ Branch 3 not taken.
4842 res = res && write_byte(v,S_SYM_VALUE);
127 #ifndef LBM64
128
2/4
✓ Branch 0 taken 4842 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4842 times.
✗ Branch 3 not taken.
4842 res = res && write_word(v,sym_id);
129 #else
130 res = res && write_dword(v,sym_id);
131 #endif
132 4842 return res;
133 }
134
135 63113 bool f_sym_string(lbm_flat_value_t *v, char *str) {
136 63113 bool res = false;
137
1/2
✓ Branch 0 taken 63113 times.
✗ Branch 1 not taken.
63113 if (str) {
138 63113 lbm_uint sym_bytes = strlen(str) + 1;
139
2/4
✓ Branch 0 taken 63113 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 63113 times.
✗ Branch 3 not taken.
126226 if (write_byte(v, S_SYM_STRING) &&
140 63113 write_bytes(v, (uint8_t*)str, sym_bytes)) {
141 63113 res = true;
142 }
143 }
144 63113 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 63073 int f_sym_string_bytes(lbm_value sym) {
150 63073 int res = FLATTEN_VALUE_ERROR_FATAL;
151
1/2
✓ Branch 0 taken 63073 times.
✗ Branch 1 not taken.
63073 if (lbm_is_symbol(sym)) {
152 63073 lbm_uint s = lbm_dec_sym(sym);
153 63073 char *sym_str = (char*)lbm_get_name_by_symbol(s);
154
1/2
✓ Branch 0 taken 63073 times.
✗ Branch 1 not taken.
63073 if (sym_str) {
155 63073 lbm_uint sym_bytes = strlen(sym_str) + 1;
156 63073 res = (int)sym_bytes;
157 }
158 }
159 63073 return res;
160 }
161
162 45804 bool f_i(lbm_flat_value_t *v, lbm_int i) {
163 45804 bool res = true;
164 #ifndef LBM64
165
2/4
✓ Branch 0 taken 45804 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 45804 times.
✗ Branch 3 not taken.
45804 res = res && write_byte(v,S_I28_VALUE);
166
2/4
✓ Branch 0 taken 45804 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 45804 times.
✗ Branch 3 not taken.
45804 res = res && write_word(v,(uint32_t)i);
167 #else
168 res = res && write_byte(v,S_I56_VALUE);
169 res = res && write_dword(v, (uint64_t)i);
170 #endif
171 45804 return res;
172 }
173
174 28 bool f_u(lbm_flat_value_t *v, lbm_uint u) {
175 28 bool res = true;
176 #ifndef LBM64
177
2/4
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
28 res = res && write_byte(v,S_U28_VALUE);
178
2/4
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
28 res = res && write_word(v,(uint32_t)u);
179 #else
180 res = res && write_byte(v,S_U56_VALUE);
181 res = res && write_dword(v,(uint64_t)u);
182 #endif
183 28 return res;
184 }
185
186 60648 bool f_b(lbm_flat_value_t *v, uint8_t b) {
187 60648 bool res = true;
188
2/4
✓ Branch 0 taken 60648 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 60648 times.
✗ Branch 3 not taken.
60648 res = res && write_byte(v,S_BYTE_VALUE);
189
2/4
✓ Branch 0 taken 60648 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 60648 times.
✗ Branch 3 not taken.
60648 res = res && write_byte(v,b);
190 60648 return res;
191 }
192
193 29274 bool f_i32(lbm_flat_value_t *v, int32_t w) {
194 29274 bool res = true;
195
2/4
✓ Branch 0 taken 29274 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29274 times.
✗ Branch 3 not taken.
29274 res = res && write_byte(v, S_I32_VALUE);
196
2/4
✓ Branch 0 taken 29274 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29274 times.
✗ Branch 3 not taken.
29274 res = res && write_word(v, (uint32_t)w);
197 29274 return res;
198 }
199
200 31122 bool f_u32(lbm_flat_value_t *v, uint32_t w) {
201 31122 bool res = true;
202
2/4
✓ Branch 0 taken 31122 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 31122 times.
✗ Branch 3 not taken.
31122 res = res && write_byte(v, S_U32_VALUE);
203
2/4
✓ Branch 0 taken 31122 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 31122 times.
✗ Branch 3 not taken.
31122 res = res && write_word(v, w);
204 31122 return res;
205 }
206
207 7126 bool f_float(lbm_flat_value_t *v, float f) {
208 7126 bool res = true;
209
2/4
✓ Branch 0 taken 7126 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7126 times.
✗ Branch 3 not taken.
7126 res = res && write_byte(v, S_FLOAT_VALUE);
210 uint32_t u;
211 7126 memcpy(&u, &f, sizeof(uint32_t));
212
2/4
✓ Branch 0 taken 7126 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7126 times.
✗ Branch 3 not taken.
7126 res = res && write_word(v, (uint32_t)u);
213 7126 return res;
214 }
215
216 28 bool f_double(lbm_flat_value_t *v, double d) {
217 28 bool res = true;
218
2/4
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
28 res = res && write_byte(v, S_DOUBLE_VALUE);
219 uint64_t u;
220 28 memcpy(&u, &d, sizeof(uint64_t));
221
2/4
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
28 res = res && write_dword(v, u);
222 28 return res;
223 }
224
225 28 bool f_i64(lbm_flat_value_t *v, int64_t w) {
226 28 bool res = true;
227
2/4
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
28 res = res && write_byte(v, S_I64_VALUE);
228
2/4
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
28 res = res && write_dword(v, (uint64_t)w);
229 28 return res;
230 }
231
232 28 bool f_u64(lbm_flat_value_t *v, uint64_t w) {
233 28 bool res = true;
234
2/4
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
28 res = res && write_byte(v, S_U64_VALUE);
235
2/4
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
28 res = res && write_dword(v, w);
236 28 return res;
237 }
238
239 // num_bytes is specifically an uint32_t
240 118748 bool f_lbm_array(lbm_flat_value_t *v, uint32_t num_bytes, uint8_t *data) {
241 118748 bool res = write_byte(v, S_LBM_ARRAY);
242
2/4
✓ Branch 0 taken 118748 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 118748 times.
✗ Branch 3 not taken.
118748 res = res && write_word(v, num_bytes);
243
2/4
✓ Branch 0 taken 118748 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 118748 times.
✗ Branch 3 not taken.
118748 res = res && write_bytes(v, data, num_bytes);
244 118748 return res;
245 }
246
247 static int flatten_maximum_depth = FLATTEN_VALUE_MAXIMUM_DEPTH;
248
249 28 void lbm_set_max_flatten_depth(int depth) {
250 28 flatten_maximum_depth = depth;
251 28 }
252
253 int lbm_get_max_flatten_depth(void) {
254 return flatten_maximum_depth;
255 }
256
257 28 static void flatten_error(jmp_buf jb, int val) {
258 28 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 663103 static int flatten_value_size_internal(jmp_buf jb, lbm_value v, int depth, bool image) {
285
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 663075 times.
663103 if (depth > flatten_maximum_depth) {
286 28 flatten_error(jb, FLATTEN_VALUE_ERROR_MAXIMUM_DEPTH);
287 }
288
289 663075 lbm_uint t = lbm_type_of(v);
290
3/4
✓ Branch 0 taken 493498 times.
✓ Branch 1 taken 169577 times.
✓ Branch 2 taken 493498 times.
✗ Branch 3 not taken.
663075 if (t >= LBM_POINTER_TYPE_FIRST && t < LBM_POINTER_TYPE_LAST) {
291 // Clear constant bit, it is irrelevant to flattening
292 493498 t = t & ~(LBM_PTR_TO_CONSTANT_BIT);
293 }
294
295
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 663075 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
663075 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 314110 times.
✓ Branch 1 taken 112 times.
✓ Branch 2 taken 60664 times.
✓ Branch 3 taken 45840 times.
✓ Branch 4 taken 60468 times.
✓ Branch 5 taken 84 times.
✓ Branch 6 taken 63073 times.
✓ Branch 7 taken 118724 times.
✗ Branch 8 not taken.
663075 switch (t) {
301 314110 case LBM_TYPE_CONS: {
302 314110 int res = 0;
303 314110 int s1 = flatten_value_size_internal(jb,lbm_car(v), depth + 1, image);
304
1/2
✓ Branch 0 taken 314026 times.
✗ Branch 1 not taken.
314026 if (s1 > 0) {
305 314026 int s2 = flatten_value_size_internal(jb,lbm_cdr(v), depth + 1, image);
306
1/2
✓ Branch 0 taken 314026 times.
✗ Branch 1 not taken.
314026 if (s2 > 0) {
307 314026 res = (1 + s1 + s2);
308 }
309 }
310 314026 return res;
311 }
312 112 case LBM_TYPE_LISPARRAY: {
313 112 int sum = 4 + 1; // sizeof(uint32_t) + 1;
314 112 lbm_array_header_t *header = (lbm_array_header_t*)lbm_car(v);
315
1/2
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
112 if (header) {
316 112 lbm_value *arrdata = (lbm_value*)header->data;
317 112 lbm_uint size = header->size / sizeof(lbm_value);
318
2/2
✓ Branch 0 taken 364 times.
✓ Branch 1 taken 112 times.
476 for (lbm_uint i = 0; i < size; i ++ ) {
319 364 sum += flatten_value_size_internal(jb, arrdata[i], depth + 1, image);
320 }
321 } else {
322 flatten_error(jb, FLATTEN_VALUE_ERROR_ARRAY);
323 }
324 112 return sum;
325 }
326 60664 case LBM_TYPE_BYTE:
327 60664 return 1 + 1;
328 45840 case LBM_TYPE_U: /* fall through */
329 case LBM_TYPE_I:
330 #ifndef LBM64
331 45840 return 1 + 4;
332 #else
333 return 1 + 8;
334 #endif
335 60468 case LBM_TYPE_U32: /* fall through */
336 case LBM_TYPE_I32:
337 case LBM_TYPE_FLOAT:
338 60468 return 1 + 4;
339 84 case LBM_TYPE_U64: /* fall through */
340 case LBM_TYPE_I64:
341 case LBM_TYPE_DOUBLE:
342 84 return 1 + 8;
343 63073 case LBM_TYPE_SYMBOL: {
344
1/2
✓ Branch 0 taken 63073 times.
✗ Branch 1 not taken.
63073 if (!image) {
345 63073 int s = f_sym_string_bytes(v);
346
1/2
✓ Branch 0 taken 63073 times.
✗ Branch 1 not taken.
63073 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 118724 case LBM_TYPE_ARRAY: {
353 // Platform dependent size.
354 // TODO: Something needs to be done to these inconsistencies.
355 118724 lbm_int s = lbm_heap_array_get_size(v);
356
1/2
✓ Branch 0 taken 118724 times.
✗ Branch 1 not taken.
118724 if (s > 0)
357 118724 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 34603 int flatten_value_size(lbm_value v, bool image) {
366 jmp_buf jb;
367 34603 int r = setjmp(jb);
368
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 34603 times.
34631 if (r != 0) {
369 28 return r;
370 }
371 34603 return flatten_value_size_internal(jb, v, 0, image);
372 }
373
374 662823 int flatten_value_c(lbm_flat_value_t *fv, lbm_value v) {
375
376 662823 lbm_uint t = lbm_type_of(v);
377
3/4
✓ Branch 0 taken 493286 times.
✓ Branch 1 taken 169537 times.
✓ Branch 2 taken 493286 times.
✗ Branch 3 not taken.
662823 if (t >= LBM_POINTER_TYPE_FIRST && t < LBM_POINTER_TYPE_LAST) {
378 // Clear constant bit, it is irrelevant to flattening
379 493286 t = t & ~(LBM_PTR_TO_CONSTANT_BIT);
380 }
381
382
13/14
✓ Branch 0 taken 313946 times.
✓ Branch 1 taken 112 times.
✓ Branch 2 taken 60648 times.
✓ Branch 3 taken 28 times.
✓ Branch 4 taken 45804 times.
✓ Branch 5 taken 31122 times.
✓ Branch 6 taken 29274 times.
✓ Branch 7 taken 28 times.
✓ Branch 8 taken 28 times.
✓ Branch 9 taken 56 times.
✓ Branch 10 taken 28 times.
✓ Branch 11 taken 63057 times.
✓ Branch 12 taken 118692 times.
✗ Branch 13 not taken.
662823 switch (t) {
383 313946 case LBM_TYPE_CONS: {
384 313946 bool res = true;
385
2/4
✓ Branch 0 taken 313946 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 313946 times.
✗ Branch 3 not taken.
313946 res = res && f_cons(fv);
386
1/2
✓ Branch 0 taken 313946 times.
✗ Branch 1 not taken.
313946 if (res) {
387 313946 int fv_r = flatten_value_c(fv, lbm_car(v));
388
1/2
✓ Branch 0 taken 313946 times.
✗ Branch 1 not taken.
313946 if (fv_r == FLATTEN_VALUE_OK) {
389 313946 fv_r = flatten_value_c(fv, lbm_cdr(v));
390 }
391 313946 return fv_r;
392 }
393 }break;
394 112 case LBM_TYPE_LISPARRAY: {
395 112 lbm_array_header_t *header = (lbm_array_header_t*)lbm_car(v);
396
1/2
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
112 if (header) {
397 112 lbm_value *arrdata = (lbm_value*)header->data;
398 // always exact multiple of sizeof(lbm_value)
399 112 uint32_t size = (uint32_t)(header->size / sizeof(lbm_value));
400
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 112 times.
112 if (!f_lisp_array(fv, size)) return FLATTEN_VALUE_ERROR_NOT_ENOUGH_MEMORY;
401 112 int fv_r = FLATTEN_VALUE_OK;
402
2/2
✓ Branch 0 taken 364 times.
✓ Branch 1 taken 112 times.
476 for (lbm_uint i = 0; i < size; i ++ ) {
403 364 fv_r = flatten_value_c(fv, arrdata[i]);
404
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 364 times.
364 if (fv_r != FLATTEN_VALUE_OK) {
405 break;
406 }
407 }
408 112 return fv_r;
409 } else {
410 return FLATTEN_VALUE_ERROR_ARRAY;
411 }
412 } break;
413 60648 case LBM_TYPE_BYTE:
414
1/2
✓ Branch 0 taken 60648 times.
✗ Branch 1 not taken.
60648 if (f_b(fv, (uint8_t)lbm_dec_as_char(v))) {
415 60648 return FLATTEN_VALUE_OK;
416 }
417 break;
418 28 case LBM_TYPE_U:
419
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 if (f_u(fv, lbm_dec_u(v))) {
420 28 return FLATTEN_VALUE_OK;
421 }
422 break;
423 45804 case LBM_TYPE_I:
424
1/2
✓ Branch 0 taken 45804 times.
✗ Branch 1 not taken.
45804 if (f_i(fv, lbm_dec_i(v))) {
425 45804 return FLATTEN_VALUE_OK;
426 }
427 break;
428 31122 case LBM_TYPE_U32:
429
1/2
✓ Branch 0 taken 31122 times.
✗ Branch 1 not taken.
31122 if (f_u32(fv, lbm_dec_as_u32(v))) {
430 31122 return FLATTEN_VALUE_OK;
431 }
432 break;
433 29274 case LBM_TYPE_I32:
434
1/2
✓ Branch 0 taken 29274 times.
✗ Branch 1 not taken.
29274 if (f_i32(fv, lbm_dec_as_i32(v))) {
435 29274 return FLATTEN_VALUE_OK;
436 }
437 break;
438 28 case LBM_TYPE_U64:
439
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 if (f_u64(fv, lbm_dec_as_u64(v))) {
440 28 return FLATTEN_VALUE_OK;
441 }
442 break;
443 28 case LBM_TYPE_I64:
444
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 if (f_i64(fv, lbm_dec_as_i64(v))) {
445 28 return FLATTEN_VALUE_OK;
446 }
447 break;
448 56 case LBM_TYPE_FLOAT:
449
1/2
✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
56 if (f_float(fv, lbm_dec_as_float(v))) {
450 56 return FLATTEN_VALUE_OK;
451 }
452 break;
453 28 case LBM_TYPE_DOUBLE:
454
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 if (f_double(fv, lbm_dec_as_double(v))) {
455 28 return FLATTEN_VALUE_OK;
456 }
457 break;
458 63057 case LBM_TYPE_SYMBOL: {
459 63057 char *sym_str = (char*)lbm_get_name_by_symbol(lbm_dec_sym(v));
460
1/2
✓ Branch 0 taken 63057 times.
✗ Branch 1 not taken.
63057 if (f_sym_string(fv, sym_str)) {
461 63057 return FLATTEN_VALUE_OK;
462 }
463 } break;
464 118692 case LBM_TYPE_ARRAY: {
465 118692 lbm_int s = lbm_heap_array_get_size(v);
466 118692 const uint8_t *d = lbm_heap_array_get_data_ro(v);
467
2/4
✓ Branch 0 taken 118692 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 118692 times.
✗ Branch 3 not taken.
118692 if (s > 0 && d != NULL) {
468
1/2
✓ Branch 0 taken 118692 times.
✗ Branch 1 not taken.
118692 if (f_lbm_array(fv, (uint32_t)s, (uint8_t*)d)) {
469 118692 return FLATTEN_VALUE_OK;
470 }
471 } else {
472 return FLATTEN_VALUE_ERROR_ARRAY;
473 }
474 }break;
475 default:
476 return FLATTEN_VALUE_ERROR_CANNOT_BE_FLATTENED;
477 }
478 return FLATTEN_VALUE_ERROR_BUFFER_TOO_SMALL;
479 }
480
481 28 lbm_value handle_flatten_error(int err_val) {
482
1/5
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
28 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 28 case FLATTEN_VALUE_ERROR_CIRCULAR: /* fall through */
489 case FLATTEN_VALUE_ERROR_MAXIMUM_DEPTH:
490 28 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 34605 lbm_value flatten_value(lbm_value v) {
499
500 34605 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 2 times.
✓ Branch 1 taken 34603 times.
34605 if (array_cell == ENC_SYM_MERROR) {
503 2 return array_cell;
504 }
505
506 lbm_flat_value_t fv;
507
508 34603 lbm_array_header_t *array = NULL;
509 34603 int required_mem = flatten_value_size(v, false);
510
2/2
✓ Branch 0 taken 34575 times.
✓ Branch 1 taken 28 times.
34603 if (required_mem > 0) {
511 34575 array = (lbm_array_header_t *)lbm_malloc(sizeof(lbm_array_header_t));
512
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 34567 times.
34575 if (array == NULL) {
513 8 lbm_set_car_and_cdr(array_cell, ENC_SYM_NIL, ENC_SYM_NIL);
514 8 return ENC_SYM_MERROR;
515 }
516
517 34567 bool r = lbm_start_flatten(&fv, (lbm_uint)required_mem);
518
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34567 times.
34567 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 0 taken 34567 times.
✗ Branch 1 not taken.
34567 if (flatten_value_c(&fv, v) == FLATTEN_VALUE_OK) {
525 // it would be wasteful to run finish_flatten here.
526 34567 r = true;
527 } else {
528 r = false;
529 }
530
531
1/2
✓ Branch 0 taken 34567 times.
✗ Branch 1 not taken.
34567 if (r) {
532 // lift flat_value
533 34567 array->data = (lbm_uint*)fv.buf;
534 34567 array->size = fv.buf_size;
535 34567 lbm_set_car(array_cell, (lbm_uint)array);
536 34567 array_cell = lbm_set_ptr_type(array_cell, LBM_TYPE_ARRAY);
537 34567 return array_cell;
538 }
539 }
540 28 lbm_set_car_and_cdr(array_cell, ENC_SYM_NIL, ENC_SYM_NIL);
541 28 return handle_flatten_error(required_mem);
542 }
543
544 // ------------------------------------------------------------
545 // Unflattening
546 61304 static bool extract_byte(lbm_flat_value_t *v, uint8_t *r) {
547
1/2
✓ Branch 0 taken 61304 times.
✗ Branch 1 not taken.
61304 if (v->buf_size >= v->buf_pos + 1) {
548 61304 *r = v->buf[v->buf_pos++];
549 61304 return true;
550 }
551 return false;
552 }
553
554 243754 static bool extract_word(lbm_flat_value_t *v, uint32_t *r) {
555 243754 bool res = false;
556
1/2
✓ Branch 0 taken 243754 times.
✗ Branch 1 not taken.
243754 if (v->buf_size >= v->buf_pos + 4) {
557 243754 uint32_t tmp = 0;
558 243754 tmp |= (lbm_value)v->buf[v->buf_pos++];
559 243754 tmp = tmp << 8 | (uint32_t)v->buf[v->buf_pos++];
560 243754 tmp = tmp << 8 | (uint32_t)v->buf[v->buf_pos++];
561 243754 tmp = tmp << 8 | (uint32_t)v->buf[v->buf_pos++];
562 243754 *r = tmp;
563 243754 res = true;
564 }
565 243754 return res;
566 }
567
568 102 static bool extract_dword(lbm_flat_value_t *v, uint64_t *r) {
569 102 bool res = false;
570
1/2
✓ Branch 0 taken 102 times.
✗ Branch 1 not taken.
102 if (v->buf_size >= v->buf_pos + 8) {
571 102 uint64_t tmp = 0;
572 102 tmp |= (lbm_value)v->buf[v->buf_pos++];
573 102 tmp = tmp << 8 | (uint64_t)v->buf[v->buf_pos++];
574 102 tmp = tmp << 8 | (uint64_t)v->buf[v->buf_pos++];
575 102 tmp = tmp << 8 | (uint64_t)v->buf[v->buf_pos++];
576 102 tmp = tmp << 8 | (uint64_t)v->buf[v->buf_pos++];
577 102 tmp = tmp << 8 | (uint64_t)v->buf[v->buf_pos++];
578 102 tmp = tmp << 8 | (uint64_t)v->buf[v->buf_pos++];
579 102 tmp = tmp << 8 | (uint64_t)v->buf[v->buf_pos++];
580 102 *r = tmp;
581 102 res = true;;
582 }
583 102 return res;
584 }
585
586 368035 static int lbm_unflatten_value_atom(lbm_flat_value_t *v, lbm_value *res) {
587
588 368035 uint8_t curr = v->buf[v->buf_pos++];
589
590
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 368035 times.
368035 if (v->buf_size <= v->buf_pos) return UNFLATTEN_MALFORMED;
591
592
13/17
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 7801 times.
✓ Branch 3 taken 61304 times.
✓ Branch 4 taken 48795 times.
✓ Branch 5 taken 32 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 6283 times.
✓ Branch 9 taken 39 times.
✓ Branch 10 taken 29389 times.
✓ Branch 11 taken 31543 times.
✓ Branch 12 taken 33 times.
✓ Branch 13 taken 30 times.
✓ Branch 14 taken 119665 times.
✓ Branch 15 taken 63120 times.
✗ Branch 16 not taken.
368035 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 times.
✗ Branch 1 not taken.
1 if (b) {
605 1 *res = tmp;
606 1 return UNFLATTEN_OK;
607 }
608 return UNFLATTEN_MALFORMED;
609 }
610 7801 case S_SYM_VALUE: {
611 lbm_uint tmp;
612 bool b;
613 #ifndef LBM64
614 7801 b = extract_word(v, &tmp);
615 #else
616 b = extract_dword(v, &tmp);
617 #endif
618
1/2
✓ Branch 0 taken 7801 times.
✗ Branch 1 not taken.
7801 if (b) {
619 7801 *res = lbm_enc_sym(tmp);
620 7801 return UNFLATTEN_OK;
621 }
622 return UNFLATTEN_MALFORMED;
623 }
624 61304 case S_BYTE_VALUE: {
625 uint8_t tmp;
626 61304 bool b = extract_byte(v, &tmp);
627
1/2
✓ Branch 0 taken 61304 times.
✗ Branch 1 not taken.
61304 if (b) {
628 61304 *res = lbm_enc_char((uint8_t)tmp);
629 61304 return UNFLATTEN_OK;
630 }
631 return UNFLATTEN_MALFORMED;
632 }
633 48795 case S_I28_VALUE: {
634 uint32_t tmp;
635 bool b;
636 48795 b = extract_word(v, &tmp);
637
1/2
✓ Branch 0 taken 48795 times.
✗ Branch 1 not taken.
48795 if (b) {
638 48795 *res = lbm_enc_i((int32_t)tmp);
639 48795 return UNFLATTEN_OK;
640 }
641 return UNFLATTEN_MALFORMED;
642 }
643 32 case S_U28_VALUE: {
644 uint32_t tmp;
645 bool b;
646 32 b = extract_word(v, &tmp);
647
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 if (b) {
648 32 *res = lbm_enc_u((uint32_t)tmp);
649 32 return UNFLATTEN_OK;
650 }
651 return UNFLATTEN_MALFORMED;
652 }
653 case S_I56_VALUE: {
654 uint64_t tmp;
655 bool b;
656 b = extract_dword(v, &tmp);
657 if (b) {
658 #ifndef LBM64
659 *res = lbm_enc_i64((int64_t)tmp);
660 #else
661 *res = lbm_enc_i((int64_t)tmp);
662 #endif
663 return UNFLATTEN_OK;
664 }
665 return UNFLATTEN_MALFORMED;
666 }
667 case S_U56_VALUE: {
668 uint64_t tmp;
669 bool b;
670 b = extract_dword(v, &tmp);
671 if (b) {
672 #ifndef LBM64
673 *res = lbm_enc_u64(tmp);
674 #else
675 *res = lbm_enc_u(tmp);
676 #endif
677 return UNFLATTEN_OK;
678 }
679 return UNFLATTEN_MALFORMED;
680 }
681 6283 case S_FLOAT_VALUE: {
682 uint32_t tmp;
683 bool b;
684 6283 b = extract_word(v, &tmp);
685
1/2
✓ Branch 0 taken 6283 times.
✗ Branch 1 not taken.
6283 if (b) {
686 lbm_float f;
687 6283 memcpy(&f, &tmp, sizeof(lbm_float));
688 6283 lbm_value im = lbm_enc_float(f);
689
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6283 times.
6283 if (lbm_is_symbol_merror(im)) {
690 return UNFLATTEN_GC_RETRY;
691 }
692 6283 *res = im;
693 6283 return UNFLATTEN_OK;
694 }
695 return UNFLATTEN_MALFORMED;
696 }
697 39 case S_DOUBLE_VALUE: {
698 uint64_t tmp;
699 bool b;
700 39 b = extract_dword(v, &tmp);
701
1/2
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
39 if (b) {
702 double f;
703 39 memcpy(&f, &tmp, sizeof(uint64_t));
704 39 lbm_value im = lbm_enc_double(f);
705
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
39 if (lbm_is_symbol_merror(im)) {
706 return UNFLATTEN_GC_RETRY;
707 }
708 39 *res = im;
709 39 return UNFLATTEN_OK;
710 }
711 return UNFLATTEN_MALFORMED;
712 }
713 29389 case S_I32_VALUE: {
714 uint32_t tmp;
715
1/2
✓ Branch 0 taken 29389 times.
✗ Branch 1 not taken.
29389 if (extract_word(v, &tmp)) {
716 29389 lbm_value im = lbm_enc_i32((int32_t)tmp);
717
2/2
✓ Branch 0 taken 74 times.
✓ Branch 1 taken 29315 times.
29389 if (lbm_is_symbol_merror(im)) {
718 74 return UNFLATTEN_GC_RETRY;
719 }
720 29315 *res = im;
721 29315 return UNFLATTEN_OK;
722 }
723 return UNFLATTEN_MALFORMED;
724 }
725 31543 case S_U32_VALUE: {
726 uint32_t tmp;
727
1/2
✓ Branch 0 taken 31543 times.
✗ Branch 1 not taken.
31543 if (extract_word(v, &tmp)) {
728 31543 lbm_value im = lbm_enc_u32(tmp);
729
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 31519 times.
31543 if (lbm_is_symbol_merror(im)) {
730 24 return UNFLATTEN_GC_RETRY;
731 }
732 31519 *res = im;
733 31519 return UNFLATTEN_OK;
734 }
735 return UNFLATTEN_MALFORMED;
736 }
737 33 case S_I64_VALUE: {
738 33 uint64_t tmp = 0;
739
1/2
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
33 if (extract_dword(v, &tmp)) {
740 33 lbm_value im = lbm_enc_i64((int64_t)tmp);
741
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if (lbm_is_symbol_merror(im)) {
742 return UNFLATTEN_GC_RETRY;
743 }
744 33 *res = im;
745 33 return UNFLATTEN_OK;
746 }
747 return UNFLATTEN_MALFORMED;
748 }
749 30 case S_U64_VALUE: {
750 30 uint64_t tmp = 0;
751
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 if (extract_dword(v, &tmp)) {
752 30 lbm_value im = lbm_enc_u64(tmp);
753
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (lbm_is_symbol_merror(im)) {
754 return UNFLATTEN_GC_RETRY;
755 }
756 30 *res = im;
757 30 return UNFLATTEN_OK;
758 }
759 return UNFLATTEN_MALFORMED;
760 }
761 119665 case S_LBM_ARRAY: {
762 uint32_t num_elt;
763 // TODO: Feels slightly wrong with <= here.
764
3/4
✓ Branch 0 taken 119665 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 119661 times.
✓ Branch 3 taken 4 times.
119665 if (extract_word(v, &num_elt) && v->buf_pos + num_elt <= v->buf_size) {
765
2/2
✓ Branch 0 taken 119524 times.
✓ Branch 1 taken 137 times.
119661 if (lbm_heap_allocate_array(res, num_elt)) {
766 119524 lbm_array_header_t *arr = (lbm_array_header_t*)lbm_car(*res);
767 119524 lbm_uint num_bytes = num_elt;
768 119524 memcpy(arr->data, v->buf + v->buf_pos, num_bytes);
769 119524 v->buf_pos += num_bytes;
770 } else {
771 137 return UNFLATTEN_GC_RETRY;
772 }
773 119524 return UNFLATTEN_OK;
774 }
775 4 return UNFLATTEN_MALFORMED;
776 }
777 63120 case S_SYM_STRING: {
778 lbm_uint sym_id;
779 63120 lbm_uint max_bytes = v->buf_size - v->buf_pos;
780 63120 lbm_uint num_bytes = max_bytes;
781 63120 bool found_null = false;
782
1/2
✓ Branch 0 taken 252368 times.
✗ Branch 1 not taken.
252368 for (lbm_uint i = 0; i < max_bytes; i ++) {
783
2/2
✓ Branch 0 taken 63120 times.
✓ Branch 1 taken 189248 times.
252368 if (v->buf[v->buf_pos + i] == 0) {
784 63120 num_bytes = i + 1;
785 63120 found_null = true;
786 63120 break;
787 }
788 }
789
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 63120 times.
63120 if (!found_null) return UNFLATTEN_MALFORMED;
790
1/2
✓ Branch 0 taken 63120 times.
✗ Branch 1 not taken.
63120 if (lbm_add_symbol((char *)(v->buf + v->buf_pos), &sym_id)) {
791 63120 v->buf_pos += num_bytes;
792 63120 *res = lbm_enc_sym(sym_id);
793 63120 return UNFLATTEN_OK;
794 }
795 return UNFLATTEN_GC_RETRY;
796 }
797 default:
798 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 38716 static int lbm_unflatten_value_nostack(sharing_table *st, lbm_uint *target_map, lbm_flat_value_t *v, lbm_value *res) {
866 38716 bool done = false;
867 lbm_value val0;
868 38716 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 697878 times.
✗ Branch 1 not taken.
697878 while (!done) {
873 697878 int32_t set_ix = -1;
874
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 697835 times.
697878 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 697878 bool is_leaf = true;
903 697878 lbm_value unflattened = ENC_SYM_NIL;
904
905
2/2
✓ Branch 0 taken 329641 times.
✓ Branch 1 taken 368237 times.
697878 if (v->buf[v->buf_pos] == S_CONS) {
906 329641 lbm_value tmp = curr;
907 329641 curr = lbm_cons(tmp, ENC_SYM_PLACEHOLDER);
908
2/2
✓ Branch 0 taken 206 times.
✓ Branch 1 taken 329435 times.
329641 if (lbm_is_symbol_merror(curr)) return UNFLATTEN_GC_RETRY;
909
910
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 329397 times.
329435 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 329435 v->buf_pos ++;
918 329435 is_leaf = false;
919
2/2
✓ Branch 0 taken 134 times.
✓ Branch 1 taken 368103 times.
368237 } else if (v->buf[v->buf_pos] == S_LBM_LISP_ARRAY) {
920 uint32_t size;
921 134 v->buf_pos ++;
922 134 bool b = extract_word(v, &size);
923
1/2
✓ Branch 0 taken 134 times.
✗ Branch 1 not taken.
134 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 132 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 130 times.
134 if (size > 0 && v->buf_pos + (size * 2) > v->buf_size) return UNFLATTEN_MALFORMED;
928 lbm_value array;
929 132 lbm_heap_allocate_lisp_array(&array, size);
930 132 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 130 times.
132 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 130 is_leaf = false;
936 130 lbm_value *arrdata = (lbm_value*)header->data;
937
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 130 times.
130 if (lbm_is_symbol_merror(array)) return UNFLATTEN_GC_RETRY;
938 130 header->index = 0;
939 130 arrdata[size-1] = curr; // backptr
940 130 curr = array;
941
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 125 times.
130 if (set_ix >= 0) target_map[set_ix] = curr;
942 }
943 } else {
944 return UNFLATTEN_MALFORMED;
945 }
946
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 368103 times.
368103 } else if (v->buf[v->buf_pos] == 0) {
947 return UNFLATTEN_MALFORMED;
948
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 368035 times.
368103 } 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 368035 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 368035 times.
368035 if (set_ix >= 0) {
991 target_map[set_ix] = unflattened;
992 }
993
2/2
✓ Branch 0 taken 239 times.
✓ Branch 1 taken 367796 times.
368035 if (e_val != UNFLATTEN_OK) {
994 239 return e_val;
995 }
996 }
997
998
2/2
✓ Branch 0 taken 367866 times.
✓ Branch 1 taken 329565 times.
697431 if (is_leaf) {
999 367866 val0 = unflattened;
1000
4/4
✓ Branch 0 taken 657139 times.
✓ Branch 1 taken 38269 times.
✓ Branch 2 taken 327939 times.
✓ Branch 3 taken 329200 times.
1352547 while (lbm_dec_ptr(curr) != LBM_PTR_NULL &&
1001 657139 lbm_cdr(curr) != ENC_SYM_PLACEHOLDER) { // has done left
1002
2/2
✓ Branch 0 taken 527 times.
✓ Branch 1 taken 327412 times.
327939 if ( lbm_type_of(curr) == LBM_TYPE_LISPARRAY) {
1003 527 lbm_array_header_extended_t *header = (lbm_array_header_extended_t*)lbm_car(curr);
1004 527 lbm_value *arrdata = (lbm_value*)header->data;
1005 527 uint32_t arrlen = (uint32_t)(header->size / sizeof(lbm_value));
1006
2/2
✓ Branch 0 taken 130 times.
✓ Branch 1 taken 397 times.
527 if (header->index == arrlen - 1) {
1007 130 lbm_value prev = arrdata[arrlen-1];
1008 130 header->index = 0;
1009 130 arrdata[arrlen-1] = val0;
1010 130 val0 = curr;
1011 130 curr = prev;
1012 } else {
1013 397 arrdata[header->index++] = val0;
1014 397 break;
1015 }
1016 } else {
1017 327412 lbm_value prev = lbm_car(curr);
1018 327412 lbm_value r0 = lbm_cdr(curr);
1019 327412 lbm_set_cdr(curr, val0);
1020 327412 lbm_set_car(curr, r0);
1021 327412 val0 = curr;
1022 327412 curr = prev;
1023 }
1024 }
1025
2/2
✓ Branch 0 taken 38269 times.
✓ Branch 1 taken 329597 times.
367866 if (lbm_dec_ptr(curr) == LBM_PTR_NULL) {
1026 38269 *res = val0; // done
1027 38269 break;
1028
2/2
✓ Branch 0 taken 329200 times.
✓ Branch 1 taken 397 times.
329597 } else if (lbm_type_of(curr) == LBM_TYPE_LISPARRAY) {
1029 // Do nothing in this case. It has been arranged..
1030
1/2
✓ Branch 0 taken 329200 times.
✗ Branch 1 not taken.
329200 } else if (lbm_cdr(curr) == ENC_SYM_PLACEHOLDER) {
1031 329200 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 38269 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 38174 bool lbm_unflatten_value(lbm_flat_value_t *v, lbm_value *res) {
1053 38174 bool b = false;
1054 #ifdef LBM_ALWAYS_GC
1055 lbm_perform_gc();
1056 #endif
1057 38174 int r = lbm_unflatten_value_nostack(NULL,NULL, v,res);
1058
2/2
✓ Branch 0 taken 441 times.
✓ Branch 1 taken 37733 times.
38174 if (r == UNFLATTEN_GC_RETRY) {
1059 441 lbm_perform_gc();
1060 441 v->buf_pos = 0;
1061 441 r = lbm_unflatten_value_nostack(NULL,NULL,v,res);
1062 }
1063
2/3
✓ Branch 0 taken 38168 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
38174 switch(r) {
1064 38168 case UNFLATTEN_OK:
1065 38168 b = true;
1066 38168 break;
1067 case UNFLATTEN_GC_RETRY:
1068 *res = ENC_SYM_MERROR;
1069 break;
1070 6 default:
1071 6 *res = ENC_SYM_EERROR;
1072 6 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 38174 return b;
1079 }
1080
1081 101 bool lbm_unflatten_value_sharing(sharing_table *st, lbm_uint *target_map, lbm_flat_value_t *v, lbm_value *res) {
1082 101 bool b = false;
1083 #ifdef LBM_ALWAYS_GC
1084 lbm_perform_gc();
1085 #endif
1086 101 int r = lbm_unflatten_value_nostack(st,target_map, v,res);
1087
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 101 times.
101 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 101 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
101 switch(r) {
1093 101 case UNFLATTEN_OK:
1094 101 b = true;
1095 101 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 101 return b;
1108 }
1109