GCC Code Coverage Report


Directory: ../src/
File: /home/joels/Current/lispbm/src/lbm_flat_value.c
Date: 2026-07-11 23:13:58
Exec Total Coverage
Lines: 546 623 87.6%
Functions: 36 36 100.0%
Branches: 298 417 71.5%

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 120928 bool lbm_start_flatten(lbm_flat_value_t *v, size_t buffer_size) {
37 120928 bool res = false;
38 120928 uint8_t *data = lbm_malloc_reserve(buffer_size);
39
2/2
✓ Branch 0 taken 120927 times.
✓ Branch 1 taken 1 times.
120928 if (data) {
40 120927 v->buf = data;
41 120927 v->buf_size = buffer_size;
42 120927 v->buf_pos = 0;
43 120927 res = true;
44 }
45 120928 return res;
46 }
47
48 17225 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 17219 times.
17225 if (v->buf_pos % sizeof(lbm_uint) == 0) {
51 6 size_words = v->buf_pos / sizeof(lbm_uint);
52 } else {
53 17219 size_words = (v->buf_pos / sizeof(lbm_uint)) + 1;
54 }
55
2/2
✓ Branch 0 taken 150 times.
✓ Branch 1 taken 17075 times.
17225 if (v->buf_size <= size_words * sizeof(lbm_uint)) return true;
56 17075 v->buf_size = size_words * sizeof(lbm_uint);
57 17075 return (lbm_memory_shrink((lbm_uint*)v->buf, size_words) >= 0);
58 }
59
60 1263655 static bool write_byte(lbm_flat_value_t *v, uint8_t b) {
61 1263655 bool res = false;
62
2/2
✓ Branch 0 taken 1263646 times.
✓ Branch 1 taken 9 times.
1263655 if (v->buf_size >= v->buf_pos + 1) {
63 1263646 v->buf[v->buf_pos++] = b;
64 1263646 res = true;
65 }
66 1263655 return res;
67 }
68
69 545608 static bool write_bytes(lbm_flat_value_t *v, uint8_t *data,lbm_uint num_bytes) {
70 545608 bool res = false;
71
2/2
✓ Branch 0 taken 545602 times.
✓ Branch 1 taken 6 times.
545608 if (v->buf_size >= v->buf_pos + num_bytes) {
72 545602 memcpy(v->buf + v->buf_pos, data, num_bytes);
73 545602 v->buf_pos += num_bytes;
74 545602 res = true;
75 }
76 545608 return res;
77 }
78
79 659854 static bool write_word(lbm_flat_value_t *v, uint32_t w) {
80 659854 bool res = false;
81
2/2
✓ Branch 0 taken 659822 times.
✓ Branch 1 taken 32 times.
659854 if (v->buf_size >= v->buf_pos + 4) {
82 659822 v->buf[v->buf_pos++] = (uint8_t)(w >> 24);
83 659822 v->buf[v->buf_pos++] = (uint8_t)(w >> 16);
84 659822 v->buf[v->buf_pos++] = (uint8_t)(w >> 8);
85 659822 v->buf[v->buf_pos++] = (uint8_t)w;
86 659822 res = true;
87 }
88 659854 return res;
89 }
90
91 50537 static bool write_dword(lbm_flat_value_t *v, uint64_t w) {
92 50537 bool res = false;
93
2/2
✓ Branch 0 taken 50518 times.
✓ Branch 1 taken 19 times.
50537 if (v->buf_size >= v->buf_pos + 8) {
94 50518 v->buf[v->buf_pos++] = (uint8_t)(w >> 56);
95 50518 v->buf[v->buf_pos++] = (uint8_t)(w >> 48);
96 50518 v->buf[v->buf_pos++] = (uint8_t)(w >> 40);
97 50518 v->buf[v->buf_pos++] = (uint8_t)(w >> 32);
98 50518 v->buf[v->buf_pos++] = (uint8_t)(w >> 24);
99 50518 v->buf[v->buf_pos++] = (uint8_t)(w >> 16);
100 50518 v->buf[v->buf_pos++] = (uint8_t)(w >> 8);
101 50518 v->buf[v->buf_pos++] = (uint8_t)w;
102 50518 res = true;
103 }
104 50537 return res;
105 }
106
107 959720 bool f_cons(lbm_flat_value_t *v) {
108 959720 bool res = false;
109
2/2
✓ Branch 0 taken 959718 times.
✓ Branch 1 taken 2 times.
959720 if (v->buf_size >= v->buf_pos + 1) {
110 959718 v->buf[v->buf_pos++] = S_CONS;
111 959718 res = true;
112 }
113 959720 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 2 taken 336 times.
✗ Branch 3 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 2 taken 336 times.
✗ Branch 3 not taken.
336 res = res && write_word(v, size); // number of elements.
121 336 return res;
122 }
123
124 13600 bool f_sym(lbm_flat_value_t *v, lbm_uint sym_id) {
125 13600 bool res = true;
126
2/4
✓ Branch 0 taken 13600 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13600 times.
✗ Branch 3 not taken.
13600 res = res && write_byte(v,S_SYM_VALUE);
127 #ifndef LBM64
128
2/4
✓ Branch 0 taken 8992 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8992 times.
✗ Branch 3 not taken.
8992 res = res && write_word(v,sym_id);
129 #else
130
2/4
✓ Branch 0 taken 4608 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4608 times.
✗ Branch 3 not taken.
4608 res = res && write_dword(v,sym_id);
131 #endif
132 13600 return res;
133 }
134
135 189362 bool f_sym_string(lbm_flat_value_t *v, char *str) {
136 189362 bool res = false;
137
1/2
✓ Branch 0 taken 189362 times.
✗ Branch 1 not taken.
189362 if (str) {
138 189362 lbm_uint sym_bytes = strlen(str) + 1;
139
4/4
✓ Branch 0 taken 189360 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 189357 times.
✓ Branch 3 taken 3 times.
378722 if (write_byte(v, S_SYM_STRING) &&
140 189360 write_bytes(v, (uint8_t*)str, sym_bytes)) {
141 189357 res = true;
142 }
143 }
144 189362 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 189194 int f_sym_string_bytes(lbm_value sym) {
150 189194 int res = FLATTEN_VALUE_ERROR_FATAL;
151
1/2
✓ Branch 0 taken 189194 times.
✗ Branch 1 not taken.
189194 if (lbm_is_symbol(sym)) {
152 189194 lbm_uint s = lbm_dec_sym(sym);
153 189194 char *sym_str = (char*)lbm_get_name_by_symbol(s);
154
1/2
✓ Branch 0 taken 189194 times.
✗ Branch 1 not taken.
189194 if (sym_str) {
155 189194 lbm_uint sym_bytes = strlen(sym_str) + 1;
156 189194 res = (int)sym_bytes;
157 }
158 }
159 189194 return res;
160 }
161
162 137450 bool f_i(lbm_flat_value_t *v, lbm_int i) {
163 137450 bool res = true;
164 #ifndef LBM64
165
3/4
✓ Branch 0 taken 91824 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 91821 times.
✓ Branch 3 taken 3 times.
91824 res = res && write_byte(v,S_I28_VALUE);
166
4/4
✓ Branch 0 taken 91821 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 91810 times.
✓ Branch 3 taken 11 times.
91824 res = res && write_word(v,(uint32_t)i);
167 #else
168
2/4
✓ Branch 0 taken 45626 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 45626 times.
✗ Branch 3 not taken.
45626 res = res && write_byte(v,S_I56_VALUE);
169
2/4
✓ Branch 0 taken 45626 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 45626 times.
✗ Branch 3 not taken.
45626 res = res && write_dword(v, (uint64_t)i);
170 #endif
171 137450 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 2 taken 60 times.
✗ Branch 3 not taken.
60 res = res && write_byte(v,S_U28_VALUE);
178
3/4
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 56 times.
✓ Branch 3 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 2 taken 28 times.
✗ Branch 3 not taken.
28 res = res && write_byte(v,S_U56_VALUE);
181
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)u);
182 #endif
183 88 return res;
184 }
185
186 181949 bool f_b(lbm_flat_value_t *v, uint8_t b) {
187 181949 bool res = true;
188
2/4
✓ Branch 0 taken 181949 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 181949 times.
✗ Branch 3 not taken.
181949 res = res && write_byte(v,S_BYTE_VALUE);
189
3/4
✓ Branch 0 taken 181949 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 181946 times.
✓ Branch 3 taken 3 times.
181949 res = res && write_byte(v,b);
190 181949 return res;
191 }
192
193 87826 bool f_i32(lbm_flat_value_t *v, int32_t w) {
194 87826 bool res = true;
195
2/4
✓ Branch 0 taken 87826 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 87826 times.
✗ Branch 3 not taken.
87826 res = res && write_byte(v, S_I32_VALUE);
196
3/4
✓ Branch 0 taken 87826 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 87822 times.
✓ Branch 3 taken 4 times.
87826 res = res && write_word(v, (uint32_t)w);
197 87826 return res;
198 }
199
200 93370 bool f_u32(lbm_flat_value_t *v, uint32_t w) {
201 93370 bool res = true;
202
2/4
✓ Branch 0 taken 93370 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 93370 times.
✗ Branch 3 not taken.
93370 res = res && write_byte(v, S_U32_VALUE);
203
3/4
✓ Branch 0 taken 93370 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 93366 times.
✓ Branch 3 taken 4 times.
93370 res = res && write_word(v, w);
204 93370 return res;
205 }
206
207 21200 bool f_float(lbm_flat_value_t *v, float f) {
208 21200 bool res = true;
209
2/4
✓ Branch 0 taken 21200 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 21200 times.
✗ Branch 3 not taken.
21200 res = res && write_byte(v, S_FLOAT_VALUE);
210 uint32_t u;
211 21200 memcpy(&u, &f, sizeof(uint32_t));
212
3/4
✓ Branch 0 taken 21200 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 21192 times.
✓ Branch 3 taken 8 times.
21200 res = res && write_word(v, (uint32_t)u);
213 21200 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 2 taken 92 times.
✗ Branch 3 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 2 taken 85 times.
✓ Branch 3 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 2 taken 94 times.
✓ Branch 3 taken 1 times.
95 res = res && write_byte(v, S_I64_VALUE);
228
4/4
✓ Branch 0 taken 94 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 87 times.
✓ Branch 3 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 2 taken 89 times.
✗ Branch 3 not taken.
89 res = res && write_byte(v, S_U64_VALUE);
235
3/4
✓ Branch 0 taken 89 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 84 times.
✓ Branch 3 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 356249 bool f_lbm_array(lbm_flat_value_t *v, uint32_t num_bytes, uint8_t *data) {
241 356249 bool res = write_byte(v, S_LBM_ARRAY);
242
3/4
✓ Branch 0 taken 356249 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 356248 times.
✓ Branch 3 taken 1 times.
356249 res = res && write_word(v, num_bytes);
243
4/4
✓ Branch 0 taken 356248 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 356245 times.
✓ Branch 3 taken 3 times.
356249 res = res && write_bytes(v, data, num_bytes);
244 356249 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 1990912 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 1990825 times.
1990912 if (depth > flatten_maximum_depth) {
286 87 flatten_error(jb, FLATTEN_VALUE_ERROR_MAXIMUM_DEPTH);
287 }
288
289 1990825 lbm_uint t = lbm_type_of(v);
290
3/4
✓ Branch 0 taken 1421716 times.
✓ Branch 1 taken 569109 times.
✓ Branch 2 taken 1421716 times.
✗ Branch 3 not taken.
1990825 if (t >= LBM_POINTER_TYPE_FIRST && t < LBM_POINTER_TYPE_LAST) {
291 // Clear constant bit, it is irrelevant to flattening
292 1421716 t = t & ~(LBM_PTR_TO_CONSTANT_BIT);
293 }
294
295
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 1990825 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
1990825 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 944135 times.
✓ Branch 1 taken 336 times.
✓ Branch 2 taken 181947 times.
✓ Branch 3 taken 137516 times.
✓ Branch 4 taken 181362 times.
✓ Branch 5 taken 257 times.
✓ Branch 6 taken 189194 times.
✓ Branch 7 taken 356078 times.
✗ Branch 8 not taken.
1990825 switch (t) {
301 944135 case LBM_TYPE_CONS: {
302 944135 int res = flatten_value_size_internal(jb,lbm_car(v), depth + 1, image);
303
1/2
✓ Branch 0 taken 941865 times.
✗ Branch 1 not taken.
941865 if (res > 0) {
304 941865 int s2 = flatten_value_size_internal(jb,lbm_cdr(v), depth + 1, image);
305
1/2
✓ Branch 0 taken 941865 times.
✗ Branch 1 not taken.
941865 if (s2 > 0) {
306 941865 res = (1 + res + s2);
307 } else {
308 ✗ res = s2;
309 }
310 }
311 941865 return res;
312 }
313 336 case LBM_TYPE_LISPARRAY: {
314 336 int sum = 4 + 1; // sizeof(uint32_t) + 1;
315 336 lbm_array_header_t *header = (lbm_array_header_t*)lbm_car(v);
316
1/2
✓ Branch 0 taken 336 times.
✗ Branch 1 not taken.
336 if (header) {
317 336 lbm_value *arrdata = (lbm_value*)header->data;
318 336 lbm_uint size = header->size / sizeof(lbm_value);
319
2/2
✓ Branch 0 taken 1092 times.
✓ Branch 1 taken 336 times.
1428 for (lbm_uint i = 0; i < size; i ++ ) {
320 1092 int r = flatten_value_size_internal(jb, arrdata[i], depth + 1, image);
321
1/2
✓ Branch 0 taken 1092 times.
✗ Branch 1 not taken.
1092 if (r > 0) {
322 1092 sum += r;
323 } else {
324 ✗ return r;
325 }
326 }
327 } else {
328 ✗ flatten_error(jb, FLATTEN_VALUE_ERROR_ARRAY);
329 }
330 336 return sum;
331 }
332 181947 case LBM_TYPE_BYTE:
333 181947 return 1 + 1;
334 137516 case LBM_TYPE_U: /* fall through */
335 case LBM_TYPE_I:
336 #ifndef LBM64
337 91862 return 1 + 4;
338 #else
339 45654 return 1 + 8;
340 #endif
341 181362 case LBM_TYPE_U32: /* fall through */
342 case LBM_TYPE_I32:
343 case LBM_TYPE_FLOAT:
344 181362 return 1 + 4;
345 257 case LBM_TYPE_U64: /* fall through */
346 case LBM_TYPE_I64:
347 case LBM_TYPE_DOUBLE:
348 257 return 1 + 8;
349 189194 case LBM_TYPE_SYMBOL: {
350
1/2
✓ Branch 0 taken 189194 times.
✗ Branch 1 not taken.
189194 if (!image) {
351 189194 int s = f_sym_string_bytes(v);
352
1/2
✓ Branch 0 taken 189194 times.
✗ Branch 1 not taken.
189194 if (s > 0) return 1 + s;
353 ✗ flatten_error(jb, (int)s);
354 } else {
355 ✗ return 1 + sizeof(lbm_uint);
356 }
357 ✗ } return 0; // already terminated with error
358 356078 case LBM_TYPE_ARRAY: {
359 // Platform dependent size.
360 // TODO: Something needs to be done to these inconsistencies.
361 356078 lbm_int s = lbm_heap_array_get_size(v);
362
1/2
✓ Branch 0 taken 356078 times.
✗ Branch 1 not taken.
356078 if (s > 0)
363 356078 return 1 + 4 + (int)s;
364 ✗ flatten_error(jb, (int)s);
365 ✗ } return 0; // already terminated with error
366 ✗ default:
367 ✗ return FLATTEN_VALUE_ERROR_CANNOT_BE_FLATTENED;
368 }
369 }
370
371 103820 int flatten_value_size(lbm_value v, bool image) {
372 jmp_buf jb;
373 103820 int r = setjmp(jb);
374
2/2
✓ Branch 0 taken 87 times.
✓ Branch 1 taken 103820 times.
103907 if (r != 0) {
375 87 return r;
376 }
377 103820 return flatten_value_size_internal(jb, v, 0, image);
378 }
379
380 1988667 int flatten_value_c(lbm_flat_value_t *fv, lbm_value v) {
381
382 1988667 lbm_uint t = lbm_type_of(v);
383
3/4
✓ Branch 0 taken 1419534 times.
✓ Branch 1 taken 569133 times.
✓ Branch 2 taken 1419534 times.
✗ Branch 3 not taken.
1988667 if (t >= LBM_POINTER_TYPE_FIRST && t < LBM_POINTER_TYPE_LAST) {
384 // Clear constant bit, it is irrelevant to flattening
385 1419534 t = t & ~(LBM_PTR_TO_CONSTANT_BIT);
386 }
387
388
13/14
✓ Branch 0 taken 941912 times.
✓ Branch 1 taken 336 times.
✓ Branch 2 taken 181949 times.
✓ Branch 3 taken 88 times.
✓ Branch 4 taken 137450 times.
✓ Branch 5 taken 93370 times.
✓ Branch 6 taken 87826 times.
✓ Branch 7 taken 89 times.
✓ Branch 8 taken 95 times.
✓ Branch 9 taken 185 times.
✓ Branch 10 taken 92 times.
✓ Branch 11 taken 189194 times.
✓ Branch 12 taken 356081 times.
✗ Branch 13 not taken.
1988667 switch (t) {
389 941912 case LBM_TYPE_CONS: {
390 941912 bool res = true;
391
3/4
✓ Branch 0 taken 941912 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 941910 times.
✓ Branch 3 taken 2 times.
941912 res = res && f_cons(fv);
392
2/2
✓ Branch 0 taken 941910 times.
✓ Branch 1 taken 2 times.
941912 if (res) {
393 941910 int fv_r = flatten_value_c(fv, lbm_car(v));
394
2/2
✓ Branch 0 taken 941883 times.
✓ Branch 1 taken 27 times.
941910 if (fv_r == FLATTEN_VALUE_OK) {
395 941883 fv_r = flatten_value_c(fv, lbm_cdr(v));
396 }
397 941910 return fv_r;
398 }
399 2 }break;
400 336 case LBM_TYPE_LISPARRAY: {
401 336 lbm_array_header_t *header = (lbm_array_header_t*)lbm_car(v);
402
1/2
✓ Branch 0 taken 336 times.
✗ Branch 1 not taken.
336 if (header) {
403 336 lbm_value *arrdata = (lbm_value*)header->data;
404 // always exact multiple of sizeof(lbm_value)
405 336 uint32_t size = (uint32_t)(header->size / sizeof(lbm_value));
406
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 336 times.
336 if (!f_lisp_array(fv, size)) return FLATTEN_VALUE_ERROR_NOT_ENOUGH_MEMORY;
407 336 int fv_r = FLATTEN_VALUE_OK;
408
2/2
✓ Branch 0 taken 1092 times.
✓ Branch 1 taken 336 times.
1428 for (lbm_uint i = 0; i < size; i ++ ) {
409 1092 fv_r = flatten_value_c(fv, arrdata[i]);
410
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1092 times.
1092 if (fv_r != FLATTEN_VALUE_OK) {
411 ✗ break;
412 }
413 }
414 336 return fv_r;
415 } else {
416 ✗ return FLATTEN_VALUE_ERROR_ARRAY;
417 }
418 } break;
419 181949 case LBM_TYPE_BYTE:
420
2/2
✓ Branch 0 taken 181946 times.
✓ Branch 1 taken 3 times.
181949 if (f_b(fv, (uint8_t)lbm_dec_as_char(v))) {
421 181946 return FLATTEN_VALUE_OK;
422 }
423 3 break;
424 88 case LBM_TYPE_U:
425
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 4 times.
88 if (f_u(fv, lbm_dec_u(v))) {
426 84 return FLATTEN_VALUE_OK;
427 }
428 4 break;
429 137450 case LBM_TYPE_I:
430
2/2
✓ Branch 0 taken 137436 times.
✓ Branch 1 taken 14 times.
137450 if (f_i(fv, lbm_dec_i(v))) {
431 137436 return FLATTEN_VALUE_OK;
432 }
433 14 break;
434 93370 case LBM_TYPE_U32:
435
2/2
✓ Branch 0 taken 93366 times.
✓ Branch 1 taken 4 times.
93370 if (f_u32(fv, lbm_dec_as_u32(v))) {
436 93366 return FLATTEN_VALUE_OK;
437 }
438 4 break;
439 87826 case LBM_TYPE_I32:
440
2/2
✓ Branch 0 taken 87822 times.
✓ Branch 1 taken 4 times.
87826 if (f_i32(fv, lbm_dec_as_i32(v))) {
441 87822 return FLATTEN_VALUE_OK;
442 }
443 4 break;
444 89 case LBM_TYPE_U64:
445
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 5 times.
89 if (f_u64(fv, lbm_dec_as_u64(v))) {
446 84 return FLATTEN_VALUE_OK;
447 }
448 5 break;
449 95 case LBM_TYPE_I64:
450
2/2
✓ Branch 0 taken 87 times.
✓ Branch 1 taken 8 times.
95 if (f_i64(fv, lbm_dec_as_i64(v))) {
451 87 return FLATTEN_VALUE_OK;
452 }
453 8 break;
454 185 case LBM_TYPE_FLOAT:
455
2/2
✓ Branch 0 taken 177 times.
✓ Branch 1 taken 8 times.
185 if (f_float(fv, lbm_dec_as_float(v))) {
456 177 return FLATTEN_VALUE_OK;
457 }
458 8 break;
459 92 case LBM_TYPE_DOUBLE:
460
2/2
✓ Branch 0 taken 85 times.
✓ Branch 1 taken 7 times.
92 if (f_double(fv, lbm_dec_as_double(v))) {
461 85 return FLATTEN_VALUE_OK;
462 }
463 7 break;
464 189194 case LBM_TYPE_SYMBOL: {
465 189194 char *sym_str = (char*)lbm_get_name_by_symbol(lbm_dec_sym(v));
466
2/2
✓ Branch 0 taken 189189 times.
✓ Branch 1 taken 5 times.
189194 if (f_sym_string(fv, sym_str)) {
467 189189 return FLATTEN_VALUE_OK;
468 }
469 5 } break;
470 356081 case LBM_TYPE_ARRAY: {
471 356081 lbm_int s = lbm_heap_array_get_size(v);
472 356081 const uint8_t *d = lbm_heap_array_get_data_ro(v);
473
2/4
✓ Branch 0 taken 356081 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 356081 times.
✗ Branch 3 not taken.
356081 if (s > 0 && d != NULL) {
474
2/2
✓ Branch 0 taken 356077 times.
✓ Branch 1 taken 4 times.
356081 if (f_lbm_array(fv, (uint32_t)s, (uint8_t*)d)) {
475 356077 return FLATTEN_VALUE_OK;
476 }
477 } else {
478 ✗ return FLATTEN_VALUE_ERROR_ARRAY;
479 }
480 4 }break;
481 ✗ default:
482 ✗ return FLATTEN_VALUE_ERROR_CANNOT_BE_FLATTENED;
483 }
484 68 return FLATTEN_VALUE_ERROR_BUFFER_TOO_SMALL;
485 }
486
487 84 lbm_value handle_flatten_error(int err_val) {
488
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) {
489 ✗ case FLATTEN_VALUE_ERROR_CANNOT_BE_FLATTENED:
490 ✗ return ENC_SYM_EERROR;
491 ✗ case FLATTEN_VALUE_ERROR_BUFFER_TOO_SMALL: /* fall through */
492 case FLATTEN_VALUE_ERROR_FATAL:
493 ✗ return ENC_SYM_FATAL_ERROR;
494 84 case FLATTEN_VALUE_ERROR_CIRCULAR: /* fall through */
495 case FLATTEN_VALUE_ERROR_MAXIMUM_DEPTH:
496 84 return ENC_SYM_EERROR;
497 ✗ case FLATTEN_VALUE_ERROR_ARRAY: /* fall through */
498 case FLATTEN_VALUE_ERROR_NOT_ENOUGH_MEMORY:
499 ✗ return ENC_SYM_MERROR;
500 }
501 ✗ return ENC_SYM_NIL;
502 }
503
504 103808 lbm_value flatten_value(lbm_value v) {
505
506 103808 lbm_value array_cell = lbm_heap_allocate_cell(LBM_TYPE_CONS, ENC_SYM_NIL, ENC_SYM_ARRAY_TYPE);
507
508
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 103786 times.
103808 if (array_cell == ENC_SYM_MERROR) {
509 22 return array_cell;
510 }
511
512 lbm_flat_value_t fv;
513
514 103786 lbm_array_header_t *array = NULL;
515 103786 int required_mem = flatten_value_size(v, false);
516
2/2
✓ Branch 0 taken 103702 times.
✓ Branch 1 taken 84 times.
103786 if (required_mem > 0) {
517 103702 array = (lbm_array_header_t *)lbm_malloc(sizeof(lbm_array_header_t));
518
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 103702 times.
103702 if (array == NULL) {
519 ✗ lbm_set_car_and_cdr(array_cell, ENC_SYM_NIL, ENC_SYM_NIL);
520 ✗ return ENC_SYM_MERROR;
521 }
522
523 103702 bool r = lbm_start_flatten(&fv, (lbm_uint)required_mem);
524
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 103702 times.
103702 if (!r) {
525 ✗ lbm_free(array);
526 ✗ lbm_set_car_and_cdr(array_cell, ENC_SYM_NIL, ENC_SYM_NIL);
527 ✗ return ENC_SYM_MERROR;
528 }
529
530
1/2
✓ Branch 0 taken 103702 times.
✗ Branch 1 not taken.
103702 if (flatten_value_c(&fv, v) == FLATTEN_VALUE_OK) {
531 // it would be wasteful to run finish_flatten here.
532 103702 r = true;
533 } else {
534 ✗ r = false;
535 }
536
537
1/2
✓ Branch 0 taken 103702 times.
✗ Branch 1 not taken.
103702 if (r) {
538 // lift flat_value
539 103702 array->data = (lbm_uint*)fv.buf;
540 103702 array->size = fv.buf_size;
541 103702 lbm_set_car(array_cell, (lbm_uint)array);
542 103702 array_cell = lbm_set_ptr_type(array_cell, LBM_TYPE_ARRAY);
543 103702 return array_cell;
544 }
545 }
546 84 lbm_set_car_and_cdr(array_cell, ENC_SYM_NIL, ENC_SYM_NIL);
547 84 return handle_flatten_error(required_mem);
548 }
549
550 // ////////////////////////////////////////////////////////////
551 // Unflattening
552 183983 static bool extract_byte(lbm_flat_value_t *v, uint8_t *r) {
553
1/2
✓ Branch 0 taken 183983 times.
✗ Branch 1 not taken.
183983 if (v->buf_size >= v->buf_pos + 1) {
554 183983 *r = v->buf[v->buf_pos++];
555 183983 return true;
556 }
557 ✗ return false;
558 }
559
560 667285 static bool extract_word(lbm_flat_value_t *v, uint32_t *r) {
561 667285 bool res = false;
562
1/2
✓ Branch 0 taken 667285 times.
✗ Branch 1 not taken.
667285 if (v->buf_size >= v->buf_pos + 4) {
563 667285 uint32_t tmp = 0;
564 667285 tmp |= (lbm_value)v->buf[v->buf_pos++];
565 667285 tmp = tmp << 8 | (uint32_t)v->buf[v->buf_pos++];
566 667285 tmp = tmp << 8 | (uint32_t)v->buf[v->buf_pos++];
567 667285 tmp = tmp << 8 | (uint32_t)v->buf[v->buf_pos++];
568 667285 *r = tmp;
569 667285 res = true;
570 }
571 667285 return res;
572 }
573
574 49129 static bool extract_dword(lbm_flat_value_t *v, uint64_t *r) {
575 49129 bool res = false;
576
1/2
✓ Branch 0 taken 49129 times.
✗ Branch 1 not taken.
49129 if (v->buf_size >= v->buf_pos + 8) {
577 49129 uint64_t tmp = 0;
578 49129 tmp |= (lbm_value)v->buf[v->buf_pos++];
579 49129 tmp = tmp << 8 | (uint64_t)v->buf[v->buf_pos++];
580 49129 tmp = tmp << 8 | (uint64_t)v->buf[v->buf_pos++];
581 49129 tmp = tmp << 8 | (uint64_t)v->buf[v->buf_pos++];
582 49129 tmp = tmp << 8 | (uint64_t)v->buf[v->buf_pos++];
583 49129 tmp = tmp << 8 | (uint64_t)v->buf[v->buf_pos++];
584 49129 tmp = tmp << 8 | (uint64_t)v->buf[v->buf_pos++];
585 49129 tmp = tmp << 8 | (uint64_t)v->buf[v->buf_pos++];
586 49129 *r = tmp;
587 49129 res = true;;
588 }
589 49129 return res;
590 }
591
592 1089388 static int lbm_unflatten_value_atom(lbm_flat_value_t *v, lbm_value *res) {
593
594
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1089388 times.
1089388 if (v->buf_pos >= v->buf_size) return UNFLATTEN_MALFORMED;
595 1089388 uint8_t curr = v->buf[v->buf_pos++];
596
597
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1089387 times.
1089388 if (v->buf_size <= v->buf_pos) return UNFLATTEN_MALFORMED;
598
599
16/17
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 14124 times.
✓ Branch 3 taken 183983 times.
✓ Branch 4 taken 94513 times.
✓ Branch 5 taken 60 times.
✓ Branch 6 taken 45752 times.
✓ Branch 7 taken 28 times.
✓ Branch 8 taken 18806 times.
✓ Branch 9 taken 86 times.
✓ Branch 10 taken 88217 times.
✓ Branch 11 taken 94966 times.
✓ Branch 12 taken 89 times.
✓ Branch 13 taken 86 times.
✓ Branch 14 taken 359212 times.
✓ Branch 15 taken 189463 times.
✓ Branch 16 taken 1 times.
1089387 switch(curr) {
600 ✗ case S_CONS: {
601 ✗ return UNFLATTEN_MALFORMED;
602 }
603 1 case S_CONSTANT_REF: {
604 lbm_uint tmp;
605 bool b;
606 #ifndef LBM64
607 1 b = extract_word(v, &tmp);
608 #else
609 ✗ b = extract_dword(v, &tmp);
610 #endif
611
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (b) {
612 1 *res = tmp;
613 1 return UNFLATTEN_OK;
614 }
615 ✗ return UNFLATTEN_MALFORMED;
616 }
617 14124 case S_SYM_VALUE: {
618 lbm_uint tmp;
619 bool b;
620 #ifndef LBM64
621 11036 b = extract_word(v, &tmp);
622 #else
623 3088 b = extract_dword(v, &tmp);
624 #endif
625
1/2
✓ Branch 0 taken 14124 times.
✗ Branch 1 not taken.
14124 if (b) {
626 14124 *res = lbm_enc_sym(tmp);
627 14124 return UNFLATTEN_OK;
628 }
629 ✗ return UNFLATTEN_MALFORMED;
630 }
631 183983 case S_BYTE_VALUE: {
632 uint8_t tmp;
633 183983 bool b = extract_byte(v, &tmp);
634
1/2
✓ Branch 0 taken 183983 times.
✗ Branch 1 not taken.
183983 if (b) {
635 183983 *res = lbm_enc_char((uint8_t)tmp);
636 183983 return UNFLATTEN_OK;
637 }
638 ✗ return UNFLATTEN_MALFORMED;
639 }
640 94513 case S_I28_VALUE: {
641 uint32_t tmp;
642 bool b;
643 94513 b = extract_word(v, &tmp);
644
1/2
✓ Branch 0 taken 94513 times.
✗ Branch 1 not taken.
94513 if (b) {
645 94513 *res = lbm_enc_i((int32_t)tmp);
646 94513 return UNFLATTEN_OK;
647 }
648 ✗ return UNFLATTEN_MALFORMED;
649 }
650 60 case S_U28_VALUE: {
651 uint32_t tmp;
652 bool b;
653 60 b = extract_word(v, &tmp);
654
1/2
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
60 if (b) {
655 60 *res = lbm_enc_u((uint32_t)tmp);
656 60 return UNFLATTEN_OK;
657 }
658 ✗ return UNFLATTEN_MALFORMED;
659 }
660 45752 case S_I56_VALUE: {
661 uint64_t tmp;
662 bool b;
663 45752 b = extract_dword(v, &tmp);
664
1/2
✓ Branch 0 taken 45752 times.
✗ Branch 1 not taken.
45752 if (b) {
665 #ifndef LBM64
666 ✗ *res = lbm_enc_i64((int64_t)tmp);
667 #else
668 45752 *res = lbm_enc_i((int64_t)tmp);
669 #endif
670 45752 return UNFLATTEN_OK;
671 }
672 ✗ return UNFLATTEN_MALFORMED;
673 }
674 28 case S_U56_VALUE: {
675 uint64_t tmp;
676 bool b;
677 28 b = extract_dword(v, &tmp);
678
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 if (b) {
679 #ifndef LBM64
680 ✗ *res = lbm_enc_u64(tmp);
681 #else
682 28 *res = lbm_enc_u(tmp);
683 #endif
684 28 return UNFLATTEN_OK;
685 }
686 ✗ return UNFLATTEN_MALFORMED;
687 }
688 18806 case S_FLOAT_VALUE: {
689 uint32_t tmp;
690 bool b;
691 18806 b = extract_word(v, &tmp);
692
1/2
✓ Branch 0 taken 18806 times.
✗ Branch 1 not taken.
18806 if (b) {
693 lbm_float f;
694 18806 memcpy(&f, &tmp, sizeof(lbm_float));
695 18806 lbm_value im = lbm_enc_float(f);
696
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18806 times.
18806 if (lbm_is_symbol_merror(im)) {
697 ✗ return UNFLATTEN_GC_RETRY;
698 }
699 18806 *res = im;
700 18806 return UNFLATTEN_OK;
701 }
702 ✗ return UNFLATTEN_MALFORMED;
703 }
704 86 case S_DOUBLE_VALUE: {
705 uint64_t tmp;
706 bool b;
707 86 b = extract_dword(v, &tmp);
708
1/2
✓ Branch 0 taken 86 times.
✗ Branch 1 not taken.
86 if (b) {
709 double f;
710 86 memcpy(&f, &tmp, sizeof(uint64_t));
711 86 lbm_value im = lbm_enc_double(f);
712
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 86 times.
86 if (lbm_is_symbol_merror(im)) {
713 ✗ return UNFLATTEN_GC_RETRY;
714 }
715 86 *res = im;
716 86 return UNFLATTEN_OK;
717 }
718 ✗ return UNFLATTEN_MALFORMED;
719 }
720 88217 case S_I32_VALUE: {
721 uint32_t tmp;
722
1/2
✓ Branch 0 taken 88217 times.
✗ Branch 1 not taken.
88217 if (extract_word(v, &tmp)) {
723 88217 lbm_value im = lbm_enc_i32((int32_t)tmp);
724
2/2
✓ Branch 0 taken 148 times.
✓ Branch 1 taken 88069 times.
88217 if (lbm_is_symbol_merror(im)) {
725 148 return UNFLATTEN_GC_RETRY;
726 }
727 88069 *res = im;
728 88069 return UNFLATTEN_OK;
729 }
730 ✗ return UNFLATTEN_MALFORMED;
731 }
732 94966 case S_U32_VALUE: {
733 uint32_t tmp;
734
1/2
✓ Branch 0 taken 94966 times.
✗ Branch 1 not taken.
94966 if (extract_word(v, &tmp)) {
735 94966 lbm_value im = lbm_enc_u32(tmp);
736
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 94918 times.
94966 if (lbm_is_symbol_merror(im)) {
737 48 return UNFLATTEN_GC_RETRY;
738 }
739 94918 *res = im;
740 94918 return UNFLATTEN_OK;
741 }
742 ✗ return UNFLATTEN_MALFORMED;
743 }
744 89 case S_I64_VALUE: {
745 89 uint64_t tmp = 0;
746
1/2
✓ Branch 0 taken 89 times.
✗ Branch 1 not taken.
89 if (extract_dword(v, &tmp)) {
747 89 lbm_value im = lbm_enc_i64((int64_t)tmp);
748
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 89 times.
89 if (lbm_is_symbol_merror(im)) {
749 ✗ return UNFLATTEN_GC_RETRY;
750 }
751 89 *res = im;
752 89 return UNFLATTEN_OK;
753 }
754 ✗ return UNFLATTEN_MALFORMED;
755 }
756 86 case S_U64_VALUE: {
757 86 uint64_t tmp = 0;
758
1/2
✓ Branch 0 taken 86 times.
✗ Branch 1 not taken.
86 if (extract_dword(v, &tmp)) {
759 86 lbm_value im = lbm_enc_u64(tmp);
760
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 86 times.
86 if (lbm_is_symbol_merror(im)) {
761 ✗ return UNFLATTEN_GC_RETRY;
762 }
763 86 *res = im;
764 86 return UNFLATTEN_OK;
765 }
766 ✗ return UNFLATTEN_MALFORMED;
767 }
768 359212 case S_LBM_ARRAY: {
769 uint32_t num_elt;
770 // TODO: Feels slightly wrong with <= here.
771
3/4
✓ Branch 0 taken 359212 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 359199 times.
✓ Branch 3 taken 13 times.
359212 if (extract_word(v, &num_elt) && v->buf_pos + num_elt <= v->buf_size) {
772
2/2
✓ Branch 0 taken 358433 times.
✓ Branch 1 taken 766 times.
359199 if (lbm_heap_allocate_array(res, num_elt)) {
773 358433 lbm_array_header_t *arr = (lbm_array_header_t*)lbm_car(*res);
774 358433 lbm_uint num_bytes = num_elt;
775 358433 memcpy(arr->data, v->buf + v->buf_pos, num_bytes);
776 358433 v->buf_pos += num_bytes;
777 } else {
778 766 return UNFLATTEN_GC_RETRY;
779 }
780 358433 return UNFLATTEN_OK;
781 }
782 13 return UNFLATTEN_MALFORMED;
783 }
784 189463 case S_SYM_STRING: {
785 lbm_uint sym_id;
786 189463 lbm_uint max_bytes = v->buf_size - v->buf_pos;
787 189463 lbm_uint num_bytes = max_bytes;
788 189463 bool found_null = false;
789
2/2
✓ Branch 0 taken 758515 times.
✓ Branch 1 taken 1 times.
758516 for (lbm_uint i = 0; i < max_bytes; i ++) {
790
2/2
✓ Branch 0 taken 189462 times.
✓ Branch 1 taken 569053 times.
758515 if (v->buf[v->buf_pos + i] == 0) {
791 189462 num_bytes = i + 1;
792 189462 found_null = true;
793 189462 break;
794 }
795 }
796
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 189462 times.
189463 if (!found_null) return UNFLATTEN_MALFORMED;
797
1/2
✓ Branch 0 taken 189462 times.
✗ Branch 1 not taken.
189462 if (lbm_add_symbol((char *)(v->buf + v->buf_pos), &sym_id)) {
798 189462 v->buf_pos += num_bytes;
799 189462 *res = lbm_enc_sym(sym_id);
800 189462 return UNFLATTEN_OK;
801 }
802 ✗ return UNFLATTEN_GC_RETRY;
803 }
804 1 default:
805 1 return UNFLATTEN_MALFORMED;
806 }
807 }
808
809 // ////////////////////////////////////////////////////////////
810 // Pointer-reversal-esque "stackless" deserialization of
811 // flattened (serialized) trees.
812 //
813 // Initially:
814 // curr = LBM_NULL; v->buf = { ... }
815 //
816 // FORWARDS PHASE:
817 // Cons case:
818 // Reading conses from the buffer builds a backpointing list.
819 // Placeholder element acts as a 1 bit "visited" field.
820 //
821 // curr = p; v->buf = {S_CONS, ... }
822 // =>
823 // curr = [p, placeholder]; v->buf = { ... }
824 //
825 // Lisp array case:
826 // An Array tag in the buffer leads to the creation of an array
827 // with a backptr in the last element position. Placeholder element
828 // is not needed as LBM-Arrays have a built in index field (used by GC)
829 // that can keep a count of how far along the array we have progressed.
830 //
831 // curr = p; v->buf = {S_LBM_LISP_ARRAY, ... }
832 // =>
833 // curr = [| nil ... p |]; v->buf = { ... }
834 //
835 // Atom case:
836 // Reading an atom triggers a backwards traversal along the backpointer
837 // structure.
838 //
839 // curr = X; v->buf = {any_atom, ... } example integer, string.
840 // =>
841 // val = unflatten_atom(v->buf); v->buf = { ... }
842 //
843 // BACKWARDS PHASE: Start the backwards traversal:
844 //
845 // Case on X
846 // LBM_NULL;
847 // => Done! result = val
848 //
849 // [p, placeholder];
850 // =>
851 // [p, val] Base case. Finishes back traversal.
852 // Go back to FORWARDS PHASE.
853 //
854 //
855 // [p, val0];
856 // =>
857 // tmp = [val0, val]; val = tmp; curr = p; continue backwards with value pointing to recently constructed final subresult.
858 //
859 //
860 // [| a b nil ... p |]
861 // =>
862 // [| a b val ... p |] Base case. Finishes back traversal.
863 // Array internal index field keeps track of write position.
864 // Go back to FORWARDS PHASE.
865 //
866 //
867 // [| a0 a1 ... an p |]
868 // =>
869 // tmp = [| a0 a1 ... an val |]; val = tmp; curr = p; continue backwards
870 //
871
872 115976 static int lbm_unflatten_value_nostack(sharing_table *st, lbm_uint *target_map, lbm_flat_value_t *v, lbm_value *res) {
873 115976 bool done = false;
874 lbm_value val0;
875 115976 lbm_value curr = lbm_enc_cons_ptr(LBM_PTR_NULL);
876 #if DEBUG
877 char buf[256];
878 #endif
879
1/2
✓ Branch 0 taken 2064787 times.
✗ Branch 1 not taken.
2064787 while (!done) {
880 2064787 int32_t set_ix = -1;
881
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2064786 times.
2066480 if (v->buf_pos >= v->buf_size) return UNFLATTEN_MALFORMED;
882
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 2064743 times.
2064786 if (v->buf[v->buf_pos] == S_SHARED) {
883 43 v->buf_pos++;
884
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) {
885 43 bool b = false;
886 lbm_uint tmp;
887 #ifndef LBM64
888 43 b = extract_word(v, &tmp);
889 #else
890 ✗ b = extract_dword(v, &tmp);
891 #endif
892
1/2
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
43 if (b) {
893 43 int32_t ix = sharing_table_contains(st, tmp);
894 #if DEBUG
895 printf("UNFLATTEN S_SHARED: addr %x -> sharing_table index %d\n", (unsigned int)tmp, ix);
896 #endif
897
1/2
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
43 if (ix >= 0) {
898 43 set_ix = ix;
899 } else {
900 ✗ return UNFLATTEN_SHARING_TABLE_ERROR;
901 }
902 } else {
903 ✗ return UNFLATTEN_MALFORMED;
904 }
905 } else {
906 ✗ return UNFLATTEN_SHARING_TABLE_REQUIRED;
907 }
908 }
909
910
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2064786 times.
2064786 if (v->buf_pos >= v->buf_size) return UNFLATTEN_MALFORMED;
911
912 2064786 bool is_leaf = true;
913 2064786 lbm_value unflattened = ENC_SYM_NIL;
914
915
2/2
✓ Branch 0 taken 974967 times.
✓ Branch 1 taken 1089819 times.
2064786 if (v->buf[v->buf_pos] == S_CONS) {
916 974967 lbm_value tmp = curr;
917 974967 curr = lbm_cons(tmp, ENC_SYM_PLACEHOLDER);
918
2/2
✓ Branch 0 taken 708 times.
✓ Branch 1 taken 974259 times.
974967 if (lbm_is_symbol_merror(curr)) return UNFLATTEN_GC_RETRY;
919
920
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 974221 times.
974259 if (set_ix >= 0) {
921 #if DEBUG
922 lbm_print_value(buf,256,curr);
923 printf("target_map[%d] = %s\n", set_ix, buf);
924 #endif
925 38 target_map[set_ix] = curr;
926 }
927 974259 v->buf_pos ++;
928 974259 is_leaf = false;
929
2/2
✓ Branch 0 taken 363 times.
✓ Branch 1 taken 1089456 times.
1089819 } else if (v->buf[v->buf_pos] == S_LBM_LISP_ARRAY) {
930 uint32_t size;
931 363 v->buf_pos ++;
932 363 bool b = extract_word(v, &size);
933
1/2
✓ Branch 0 taken 363 times.
✗ Branch 1 not taken.
363 if (b) {
934 // Abort if buffer cannot possibly hold that size array.
935 // a flattened byte occupies 2 bytes in fv. so smallest possible
936 // array is array of bytes.
937
4/4
✓ Branch 0 taken 361 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 354 times.
363 if (size > 0 && v->buf_pos + (size * 2) > v->buf_size) return UNFLATTEN_MALFORMED;
938 lbm_value array;
939 356 lbm_heap_allocate_lisp_array(&array, size);
940 356 lbm_array_header_extended_t *header = (lbm_array_header_extended_t*)lbm_car(array);
941
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 354 times.
356 if (size == 0) {
942 2 unflattened = array;
943
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (set_ix >= 0) target_map[set_ix] = array;
944 } else {
945 354 is_leaf = false;
946 354 lbm_value *arrdata = (lbm_value*)header->data;
947
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 354 times.
354 if (lbm_is_symbol_merror(array)) return UNFLATTEN_GC_RETRY;
948 354 header->index = 0;
949 354 arrdata[size-1] = curr; // backptr
950 354 curr = array;
951
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 349 times.
354 if (set_ix >= 0) target_map[set_ix] = curr;
952 }
953 } else {
954 ✗ return UNFLATTEN_MALFORMED;
955 }
956
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1089456 times.
1089456 } else if (v->buf[v->buf_pos] == 0) {
957 ✗ return UNFLATTEN_MALFORMED;
958
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 1089388 times.
1089456 } else if (v->buf[v->buf_pos] == S_REF) {
959 68 v->buf_pos++;
960
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) {
961 68 bool b = false;
962 lbm_uint tmp;
963 #ifndef LBM64
964 68 b = extract_word(v, &tmp);
965 #else
966 ✗ b = extract_dword(v, &tmp);
967 #endif
968
1/2
✓ Branch 0 taken 68 times.
✗ Branch 1 not taken.
68 if (b) {
969 // Shared should have been hit before S_REF. So just look up index and copy from
970 // the target_map.
971 68 int32_t ix = sharing_table_contains(st, tmp);
972 #if DEBUG
973 printf("UNFLATTEN S_REF: addr %x -> sharing_table index %d\n", (unsigned int)tmp, ix);
974 #endif
975
1/2
✓ Branch 0 taken 68 times.
✗ Branch 1 not taken.
68 if (ix >= 0) {
976 //curr = target_map[ix];
977 68 unflattened = target_map[ix];
978 #if DEBUG
979 lbm_print_value(buf,256, unflattened);
980 printf("read %s from target_map[%d]\n", buf, ix);
981 lbm_print_value(buf,256, curr);
982 printf("curr is currently: %s\n", buf);
983 printf("this is a %s\n", is_leaf ? "leaf" : "internal node");
984 #endif
985 } else {
986 ✗ return UNFLATTEN_SHARING_TABLE_ERROR;
987 }
988 } else {
989 ✗ return UNFLATTEN_MALFORMED;
990 }
991 } else {
992 ✗ return UNFLATTEN_SHARING_TABLE_REQUIRED;
993 }
994 } else {
995 1089388 int e_val = lbm_unflatten_value_atom(v, &unflattened);
996 #if DEBUG
997 lbm_print_value(buf,256, unflattened);
998 printf("atom: %s\n", buf);
999 #endif
1000
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1089388 times.
1089388 if (set_ix >= 0) {
1001 ✗ target_map[set_ix] = unflattened;
1002 }
1003
2/2
✓ Branch 0 taken 978 times.
✓ Branch 1 taken 1088410 times.
1089388 if (e_val != UNFLATTEN_OK) {
1004 978 return e_val;
1005 }
1006 }
1007
1008
2/2
✓ Branch 0 taken 1088480 times.
✓ Branch 1 taken 974613 times.
2063093 if (is_leaf) {
1009 1088480 val0 = unflattened;
1010
4/4
✓ Branch 0 taken 1941443 times.
✓ Branch 1 taken 114282 times.
✓ Branch 2 taken 968146 times.
✓ Branch 3 taken 973297 times.
3997168 while (lbm_dec_ptr(curr) != LBM_PTR_NULL &&
1011 1941443 lbm_cdr(curr) != ENC_SYM_PLACEHOLDER) { // has done left
1012
2/2
✓ Branch 0 taken 1255 times.
✓ Branch 1 taken 966891 times.
968146 if ( lbm_type_of(curr) == LBM_TYPE_LISPARRAY) {
1013 1255 lbm_array_header_extended_t *header = (lbm_array_header_extended_t*)lbm_car(curr);
1014 1255 lbm_value *arrdata = (lbm_value*)header->data;
1015 1255 uint32_t arrlen = (uint32_t)(header->size / sizeof(lbm_value));
1016
2/2
✓ Branch 0 taken 354 times.
✓ Branch 1 taken 901 times.
1255 if (header->index == arrlen - 1) {
1017 354 lbm_value prev = arrdata[arrlen-1];
1018 354 header->index = 0;
1019 354 arrdata[arrlen-1] = val0;
1020 354 val0 = curr;
1021 354 curr = prev;
1022 } else {
1023 901 arrdata[header->index++] = val0;
1024 901 break;
1025 }
1026 } else {
1027 966891 lbm_value prev = lbm_car(curr);
1028 966891 lbm_value r0 = lbm_cdr(curr);
1029 966891 lbm_set_cdr(curr, val0);
1030 966891 lbm_set_car(curr, r0);
1031 966891 val0 = curr;
1032 966891 curr = prev;
1033 }
1034 }
1035
2/2
✓ Branch 0 taken 114282 times.
✓ Branch 1 taken 974198 times.
1088480 if (lbm_dec_ptr(curr) == LBM_PTR_NULL) {
1036 114282 *res = val0; // done
1037 114282 break;
1038
2/2
✓ Branch 0 taken 973297 times.
✓ Branch 1 taken 901 times.
974198 } else if (lbm_type_of(curr) == LBM_TYPE_LISPARRAY) {
1039 // Do nothing in this case. It has been arranged..
1040
1/2
✓ Branch 0 taken 973297 times.
✗ Branch 1 not taken.
973297 } else if (lbm_cdr(curr) == ENC_SYM_PLACEHOLDER) {
1041 973297 lbm_set_cdr(curr, val0);
1042 #if DEBUG
1043 lbm_print_value(buf,256, curr);
1044 printf("curr: %s\n", buf);
1045 #endif
1046 } else {
1047 ✗ return UNFLATTEN_MALFORMED;
1048 }
1049 }
1050 }
1051 114282 return UNFLATTEN_OK;
1052 }
1053
1054 /* lbm_unflatten_value_nostack, does not backtrack
1055 upon error to swap pointer to the correct direction
1056 and to remove the LBM_PTR_NULL tag.
1057
1058 unflatten_value_nostack does not alter the GC_FLAG or the GC_MARK,
1059 So really only reverse pointers and the NIL tag could be
1060 potential problems.
1061 */
1062 114203 bool lbm_unflatten_value(lbm_flat_value_t *v, lbm_value *res) {
1063 114203 bool b = false;
1064 #ifdef LBM_ALWAYS_GC
1065 lbm_perform_gc();
1066 #endif
1067 114203 int r = lbm_unflatten_value_nostack(NULL,NULL, v,res);
1068
2/2
✓ Branch 0 taken 1670 times.
✓ Branch 1 taken 112533 times.
114203 if (r == UNFLATTEN_GC_RETRY) {
1069 1670 lbm_perform_gc();
1070 1670 v->buf_pos = 0;
1071 1670 r = lbm_unflatten_value_nostack(NULL,NULL,v,res);
1072 }
1073
2/3
✓ Branch 0 taken 114179 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
114203 switch(r) {
1074 114179 case UNFLATTEN_OK:
1075 114179 b = true;
1076 114179 break;
1077 ✗ case UNFLATTEN_GC_RETRY:
1078 ✗ *res = ENC_SYM_MERROR;
1079 ✗ break;
1080 24 default:
1081 24 *res = ENC_SYM_EERROR;
1082 24 break;
1083 }
1084 // Do not free the flat value buffer here.
1085 // there are 2 cases:
1086 // 1: unflatten was called from lisp code -> GC removes the buffer.
1087 // 2: unflatten called from event processing -> event processor frees buffer.
1088 114203 return b;
1089 }
1090
1091 103 bool lbm_unflatten_value_sharing(sharing_table *st, lbm_uint *target_map, lbm_flat_value_t *v, lbm_value *res) {
1092 103 bool b = false;
1093 #ifdef LBM_ALWAYS_GC
1094 lbm_perform_gc();
1095 #endif
1096 103 int r = lbm_unflatten_value_nostack(st,target_map, v,res);
1097
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 103 times.
103 if (r == UNFLATTEN_GC_RETRY) {
1098 ✗ lbm_perform_gc();
1099 ✗ v->buf_pos = 0;
1100 ✗ r = lbm_unflatten_value_nostack(st,target_map,v,res);
1101 }
1102
1/3
✓ Branch 0 taken 103 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
103 switch(r) {
1103 103 case UNFLATTEN_OK:
1104 103 b = true;
1105 103 break;
1106 ✗ case UNFLATTEN_GC_RETRY:
1107 ✗ *res = ENC_SYM_MERROR;
1108 ✗ break;
1109 ✗ default:
1110 ✗ *res = ENC_SYM_EERROR;
1111 ✗ break;
1112 }
1113 // Do not free the flat value buffer here.
1114 // there are 2 cases:
1115 // 1: unflatten was called from lisp code -> GC removes the buffer.
1116 // 2: unflatten called from event processing -> event processor frees buffer.
1117 103 return b;
1118 }
1119