GCC Code Coverage Report


Directory: ../src/
File: /home/joels/Current/lispbm/src/lbm_flat_value.c
Date: 2025-10-28 15:15:18
Exec Total Coverage
Lines: 545 617 88.3%
Functions: 36 36 100.0%
Branches: 295 409 72.1%

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 125895 bool lbm_start_flatten(lbm_flat_value_t *v, size_t buffer_size) {
37 125895 bool res = false;
38 125895 uint8_t *data = lbm_malloc_reserve(buffer_size);
39
2/2
✓ Branch 0 taken 125894 times.
✓ Branch 1 taken 1 times.
125895 if (data) {
40 125894 v->buf = data;
41 125894 v->buf_size = buffer_size;
42 125894 v->buf_pos = 0;
43 125894 res = true;
44 }
45 125895 return res;
46 }
47
48 22144 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 22138 times.
22144 if (v->buf_pos % sizeof(lbm_uint) == 0) {
51 6 size_words = v->buf_pos / sizeof(lbm_uint);
52 } else {
53 22138 size_words = (v->buf_pos / sizeof(lbm_uint)) + 1;
54 }
55
2/2
✓ Branch 0 taken 150 times.
✓ Branch 1 taken 21994 times.
22144 if (v->buf_size <= size_words * sizeof(lbm_uint)) return true;
56 21994 v->buf_size = size_words * sizeof(lbm_uint);
57 21994 return (lbm_memory_shrink((lbm_uint*)v->buf, size_words) >= 0);
58 }
59
60 1268866 static bool write_byte(lbm_flat_value_t *v, uint8_t b) {
61 1268866 bool res = false;
62
2/2
✓ Branch 0 taken 1268857 times.
✓ Branch 1 taken 9 times.
1268866 if (v->buf_size >= v->buf_pos + 1) {
63 1268857 v->buf[v->buf_pos++] = b;
64 1268857 res = true;
65 }
66 1268866 return res;
67 }
68
69 545906 static bool write_bytes(lbm_flat_value_t *v, uint8_t *data,lbm_uint num_bytes) {
70 545906 bool res = false;
71
2/2
✓ Branch 0 taken 545900 times.
✓ Branch 1 taken 6 times.
545906 if (v->buf_size >= v->buf_pos + num_bytes) {
72 545900 memcpy(v->buf + v->buf_pos, data, num_bytes);
73 545900 v->buf_pos += num_bytes;
74 545900 res = true;
75 }
76 545906 return res;
77 }
78
79 662880 static bool write_word(lbm_flat_value_t *v, uint32_t w) {
80 662880 bool res = false;
81
2/2
✓ Branch 0 taken 662848 times.
✓ Branch 1 taken 32 times.
662880 if (v->buf_size >= v->buf_pos + 4) {
82 662848 v->buf[v->buf_pos++] = (uint8_t)(w >> 24);
83 662848 v->buf[v->buf_pos++] = (uint8_t)(w >> 16);
84 662848 v->buf[v->buf_pos++] = (uint8_t)(w >> 8);
85 662848 v->buf[v->buf_pos++] = (uint8_t)w;
86 662848 res = true;
87 }
88 662880 return res;
89 }
90
91 52424 static bool write_dword(lbm_flat_value_t *v, uint64_t w) {
92 52424 bool res = false;
93
2/2
✓ Branch 0 taken 52405 times.
✓ Branch 1 taken 19 times.
52424 if (v->buf_size >= v->buf_pos + 8) {
94 52405 v->buf[v->buf_pos++] = (uint8_t)(w >> 56);
95 52405 v->buf[v->buf_pos++] = (uint8_t)(w >> 48);
96 52405 v->buf[v->buf_pos++] = (uint8_t)(w >> 40);
97 52405 v->buf[v->buf_pos++] = (uint8_t)(w >> 32);
98 52405 v->buf[v->buf_pos++] = (uint8_t)(w >> 24);
99 52405 v->buf[v->buf_pos++] = (uint8_t)(w >> 16);
100 52405 v->buf[v->buf_pos++] = (uint8_t)(w >> 8);
101 52405 v->buf[v->buf_pos++] = (uint8_t)w;
102 52405 res = true;
103 }
104 52424 return res;
105 }
106
107 959864 bool f_cons(lbm_flat_value_t *v) {
108 959864 bool res = false;
109
2/2
✓ Branch 0 taken 959862 times.
✓ Branch 1 taken 2 times.
959864 if (v->buf_size >= v->buf_pos + 1) {
110 959862 v->buf[v->buf_pos++] = S_CONS;
111 959862 res = true;
112 }
113 959864 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 16815 bool f_sym(lbm_flat_value_t *v, lbm_uint sym_id) {
125 16815 bool res = true;
126
2/4
✓ Branch 0 taken 16815 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16815 times.
✗ Branch 3 not taken.
16815 res = res && write_byte(v,S_SYM_VALUE);
127 #ifndef LBM64
128
2/4
✓ Branch 0 taken 10320 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10320 times.
✗ Branch 3 not taken.
10320 res = res && write_word(v,sym_id);
129 #else
130
2/4
✓ Branch 0 taken 6495 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6495 times.
✗ Branch 3 not taken.
6495 res = res && write_dword(v,sym_id);
131 #endif
132 16815 return res;
133 }
134
135 189460 bool f_sym_string(lbm_flat_value_t *v, char *str) {
136 189460 bool res = false;
137
1/2
✓ Branch 0 taken 189460 times.
✗ Branch 1 not taken.
189460 if (str) {
138 189460 lbm_uint sym_bytes = strlen(str) + 1;
139
4/4
✓ Branch 0 taken 189458 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 189455 times.
✓ Branch 3 taken 3 times.
378918 if (write_byte(v, S_SYM_STRING) &&
140 189458 write_bytes(v, (uint8_t*)str, sym_bytes)) {
141 189455 res = true;
142 }
143 }
144 189460 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 189336 int f_sym_string_bytes(lbm_value sym) {
150 189336 int res = FLATTEN_VALUE_ERROR_FATAL;
151
1/2
✓ Branch 0 taken 189336 times.
✗ Branch 1 not taken.
189336 if (lbm_is_symbol(sym)) {
152 189336 lbm_uint s = lbm_dec_sym(sym);
153 189336 char *sym_str = (char*)lbm_get_name_by_symbol(s);
154
1/2
✓ Branch 0 taken 189336 times.
✗ Branch 1 not taken.
189336 if (sym_str) {
155 189336 lbm_uint sym_bytes = strlen(sym_str) + 1;
156 189336 res = (int)sym_bytes;
157 }
158 }
159 189336 return res;
160 }
161
162 137144 bool f_i(lbm_flat_value_t *v, lbm_int i) {
163 137144 bool res = true;
164 #ifndef LBM64
165
3/4
✓ Branch 0 taken 91518 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 91515 times.
✓ Branch 3 taken 3 times.
91518 res = res && write_byte(v,S_I28_VALUE);
166
4/4
✓ Branch 0 taken 91515 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 91504 times.
✓ Branch 3 taken 11 times.
91518 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 137144 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 182049 bool f_b(lbm_flat_value_t *v, uint8_t b) {
187 182049 bool res = true;
188
2/4
✓ Branch 0 taken 182049 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 182049 times.
✗ Branch 3 not taken.
182049 res = res && write_byte(v,S_BYTE_VALUE);
189
3/4
✓ Branch 0 taken 182049 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 182046 times.
✓ Branch 3 taken 3 times.
182049 res = res && write_byte(v,b);
190 182049 return res;
191 }
192
193 87876 bool f_i32(lbm_flat_value_t *v, int32_t w) {
194 87876 bool res = true;
195
2/4
✓ Branch 0 taken 87876 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 87876 times.
✗ Branch 3 not taken.
87876 res = res && write_byte(v, S_I32_VALUE);
196
3/4
✓ Branch 0 taken 87876 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 87872 times.
✓ Branch 3 taken 4 times.
87876 res = res && write_word(v, (uint32_t)w);
197 87876 return res;
198 }
199
200 93420 bool f_u32(lbm_flat_value_t *v, uint32_t w) {
201 93420 bool res = true;
202
2/4
✓ Branch 0 taken 93420 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 93420 times.
✗ Branch 3 not taken.
93420 res = res && write_byte(v, S_U32_VALUE);
203
3/4
✓ Branch 0 taken 93420 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 93416 times.
✓ Branch 3 taken 4 times.
93420 res = res && write_word(v, w);
204 93420 return res;
205 }
206
207 22904 bool f_float(lbm_flat_value_t *v, float f) {
208 22904 bool res = true;
209
2/4
✓ Branch 0 taken 22904 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 22904 times.
✗ Branch 3 not taken.
22904 res = res && write_byte(v, S_FLOAT_VALUE);
210 uint32_t u;
211 22904 memcpy(&u, &f, sizeof(uint32_t));
212
3/4
✓ Branch 0 taken 22904 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 22896 times.
✓ Branch 3 taken 8 times.
22904 res = res && write_word(v, (uint32_t)u);
213 22904 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 356449 bool f_lbm_array(lbm_flat_value_t *v, uint32_t num_bytes, uint8_t *data) {
241 356449 bool res = write_byte(v, S_LBM_ARRAY);
242
3/4
✓ Branch 0 taken 356449 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 356448 times.
✓ Branch 3 taken 1 times.
356449 res = res && write_word(v, num_bytes);
243
4/4
✓ Branch 0 taken 356448 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 356445 times.
✓ Branch 3 taken 3 times.
356449 res = res && write_bytes(v, data, num_bytes);
244 356449 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 1991710 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 1991623 times.
1991710 if (depth > flatten_maximum_depth) {
286 87 flatten_error(jb, FLATTEN_VALUE_ERROR_MAXIMUM_DEPTH);
287 }
288
289 1991623 lbm_uint t = lbm_type_of(v);
290
3/4
✓ Branch 0 taken 1422496 times.
✓ Branch 1 taken 569127 times.
✓ Branch 2 taken 1422496 times.
✗ Branch 3 not taken.
1991623 if (t >= LBM_POINTER_TYPE_FIRST && t < LBM_POINTER_TYPE_LAST) {
291 // Clear constant bit, it is irrelevant to flattening
292 1422496 t = t & ~(LBM_PTR_TO_CONSTANT_BIT);
293 }
294
295
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 1991623 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
1991623 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 944499 times.
✓ Branch 1 taken 336 times.
✓ Branch 2 taken 182091 times.
✓ Branch 3 taken 137232 times.
✓ Branch 4 taken 181506 times.
✓ Branch 5 taken 257 times.
✓ Branch 6 taken 189336 times.
✓ Branch 7 taken 356366 times.
✗ Branch 8 not taken.
1991623 switch (t) {
301 944499 case LBM_TYPE_CONS: {
302 944499 int res = 0;
303 944499 int s1 = flatten_value_size_internal(jb,lbm_car(v), depth + 1, image);
304
1/2
✓ Branch 0 taken 942229 times.
✗ Branch 1 not taken.
942229 if (s1 > 0) {
305 942229 int s2 = flatten_value_size_internal(jb,lbm_cdr(v), depth + 1, image);
306
1/2
✓ Branch 0 taken 942229 times.
✗ Branch 1 not taken.
942229 if (s2 > 0) {
307 942229 res = (1 + s1 + s2);
308 }
309 }
310 942229 return res;
311 }
312 336 case LBM_TYPE_LISPARRAY: {
313 336 int sum = 4 + 1; // sizeof(uint32_t) + 1;
314 336 lbm_array_header_t *header = (lbm_array_header_t*)lbm_car(v);
315
1/2
✓ Branch 0 taken 336 times.
✗ Branch 1 not taken.
336 if (header) {
316 336 lbm_value *arrdata = (lbm_value*)header->data;
317 336 lbm_uint size = header->size / sizeof(lbm_value);
318
2/2
✓ Branch 0 taken 1092 times.
✓ Branch 1 taken 336 times.
1428 for (lbm_uint i = 0; i < size; i ++ ) {
319 1092 sum += flatten_value_size_internal(jb, arrdata[i], depth + 1, image);
320 }
321 } else {
322 flatten_error(jb, FLATTEN_VALUE_ERROR_ARRAY);
323 }
324 336 return sum;
325 }
326 182091 case LBM_TYPE_BYTE:
327 182091 return 1 + 1;
328 137232 case LBM_TYPE_U: /* fall through */
329 case LBM_TYPE_I:
330 #ifndef LBM64
331 91570 return 1 + 4;
332 #else
333 45662 return 1 + 8;
334 #endif
335 181506 case LBM_TYPE_U32: /* fall through */
336 case LBM_TYPE_I32:
337 case LBM_TYPE_FLOAT:
338 181506 return 1 + 4;
339 257 case LBM_TYPE_U64: /* fall through */
340 case LBM_TYPE_I64:
341 case LBM_TYPE_DOUBLE:
342 257 return 1 + 8;
343 189336 case LBM_TYPE_SYMBOL: {
344
1/2
✓ Branch 0 taken 189336 times.
✗ Branch 1 not taken.
189336 if (!image) {
345 189336 int s = f_sym_string_bytes(v);
346
1/2
✓ Branch 0 taken 189336 times.
✗ Branch 1 not taken.
189336 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 356366 case LBM_TYPE_ARRAY: {
353 // Platform dependent size.
354 // TODO: Something needs to be done to these inconsistencies.
355 356366 lbm_int s = lbm_heap_array_get_size(v);
356
1/2
✓ Branch 0 taken 356366 times.
✗ Branch 1 not taken.
356366 if (s > 0)
357 356366 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 103890 int flatten_value_size(lbm_value v, bool image) {
366 jmp_buf jb;
367 103890 int r = setjmp(jb);
368
2/2
✓ Branch 0 taken 87 times.
✓ Branch 1 taken 103890 times.
103977 if (r != 0) {
369 87 return r;
370 }
371 103890 return flatten_value_size_internal(jb, v, 0, image);
372 }
373
374 1989003 int flatten_value_c(lbm_flat_value_t *fv, lbm_value v) {
375
376 1989003 lbm_uint t = lbm_type_of(v);
377
3/4
✓ Branch 0 taken 1419978 times.
✓ Branch 1 taken 569025 times.
✓ Branch 2 taken 1419978 times.
✗ Branch 3 not taken.
1989003 if (t >= LBM_POINTER_TYPE_FIRST && t < LBM_POINTER_TYPE_LAST) {
378 // Clear constant bit, it is irrelevant to flattening
379 1419978 t = t & ~(LBM_PTR_TO_CONSTANT_BIT);
380 }
381
382
13/14
✓ Branch 0 taken 942056 times.
✓ Branch 1 taken 336 times.
✓ Branch 2 taken 182049 times.
✓ Branch 3 taken 88 times.
✓ Branch 4 taken 137144 times.
✓ Branch 5 taken 93420 times.
✓ Branch 6 taken 87876 times.
✓ Branch 7 taken 89 times.
✓ Branch 8 taken 95 times.
✓ Branch 9 taken 185 times.
✓ Branch 10 taken 92 times.
✓ Branch 11 taken 189292 times.
✓ Branch 12 taken 356281 times.
✗ Branch 13 not taken.
1989003 switch (t) {
383 942056 case LBM_TYPE_CONS: {
384 942056 bool res = true;
385
3/4
✓ Branch 0 taken 942056 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 942054 times.
✓ Branch 3 taken 2 times.
942056 res = res && f_cons(fv);
386
2/2
✓ Branch 0 taken 942054 times.
✓ Branch 1 taken 2 times.
942056 if (res) {
387 942054 int fv_r = flatten_value_c(fv, lbm_car(v));
388
2/2
✓ Branch 0 taken 942027 times.
✓ Branch 1 taken 27 times.
942054 if (fv_r == FLATTEN_VALUE_OK) {
389 942027 fv_r = flatten_value_c(fv, lbm_cdr(v));
390 }
391 942054 return fv_r;
392 }
393 2 }break;
394 336 case LBM_TYPE_LISPARRAY: {
395 336 lbm_array_header_t *header = (lbm_array_header_t*)lbm_car(v);
396
1/2
✓ Branch 0 taken 336 times.
✗ Branch 1 not taken.
336 if (header) {
397 336 lbm_value *arrdata = (lbm_value*)header->data;
398 // always exact multiple of sizeof(lbm_value)
399 336 uint32_t size = (uint32_t)(header->size / sizeof(lbm_value));
400
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 336 times.
336 if (!f_lisp_array(fv, size)) return FLATTEN_VALUE_ERROR_NOT_ENOUGH_MEMORY;
401 336 int fv_r = FLATTEN_VALUE_OK;
402
2/2
✓ Branch 0 taken 1092 times.
✓ Branch 1 taken 336 times.
1428 for (lbm_uint i = 0; i < size; i ++ ) {
403 1092 fv_r = flatten_value_c(fv, arrdata[i]);
404
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1092 times.
1092 if (fv_r != FLATTEN_VALUE_OK) {
405 break;
406 }
407 }
408 336 return fv_r;
409 } else {
410 return FLATTEN_VALUE_ERROR_ARRAY;
411 }
412 } break;
413 182049 case LBM_TYPE_BYTE:
414
2/2
✓ Branch 0 taken 182046 times.
✓ Branch 1 taken 3 times.
182049 if (f_b(fv, (uint8_t)lbm_dec_as_char(v))) {
415 182046 return FLATTEN_VALUE_OK;
416 }
417 3 break;
418 88 case LBM_TYPE_U:
419
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 4 times.
88 if (f_u(fv, lbm_dec_u(v))) {
420 84 return FLATTEN_VALUE_OK;
421 }
422 4 break;
423 137144 case LBM_TYPE_I:
424
2/2
✓ Branch 0 taken 137130 times.
✓ Branch 1 taken 14 times.
137144 if (f_i(fv, lbm_dec_i(v))) {
425 137130 return FLATTEN_VALUE_OK;
426 }
427 14 break;
428 93420 case LBM_TYPE_U32:
429
2/2
✓ Branch 0 taken 93416 times.
✓ Branch 1 taken 4 times.
93420 if (f_u32(fv, lbm_dec_as_u32(v))) {
430 93416 return FLATTEN_VALUE_OK;
431 }
432 4 break;
433 87876 case LBM_TYPE_I32:
434
2/2
✓ Branch 0 taken 87872 times.
✓ Branch 1 taken 4 times.
87876 if (f_i32(fv, lbm_dec_as_i32(v))) {
435 87872 return FLATTEN_VALUE_OK;
436 }
437 4 break;
438 89 case LBM_TYPE_U64:
439
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 5 times.
89 if (f_u64(fv, lbm_dec_as_u64(v))) {
440 84 return FLATTEN_VALUE_OK;
441 }
442 5 break;
443 95 case LBM_TYPE_I64:
444
2/2
✓ Branch 0 taken 87 times.
✓ Branch 1 taken 8 times.
95 if (f_i64(fv, lbm_dec_as_i64(v))) {
445 87 return FLATTEN_VALUE_OK;
446 }
447 8 break;
448 185 case LBM_TYPE_FLOAT:
449
2/2
✓ Branch 0 taken 177 times.
✓ Branch 1 taken 8 times.
185 if (f_float(fv, lbm_dec_as_float(v))) {
450 177 return FLATTEN_VALUE_OK;
451 }
452 8 break;
453 92 case LBM_TYPE_DOUBLE:
454
2/2
✓ Branch 0 taken 85 times.
✓ Branch 1 taken 7 times.
92 if (f_double(fv, lbm_dec_as_double(v))) {
455 85 return FLATTEN_VALUE_OK;
456 }
457 7 break;
458 189292 case LBM_TYPE_SYMBOL: {
459 189292 char *sym_str = (char*)lbm_get_name_by_symbol(lbm_dec_sym(v));
460
2/2
✓ Branch 0 taken 189287 times.
✓ Branch 1 taken 5 times.
189292 if (f_sym_string(fv, sym_str)) {
461 189287 return FLATTEN_VALUE_OK;
462 }
463 5 } break;
464 356281 case LBM_TYPE_ARRAY: {
465 356281 lbm_int s = lbm_heap_array_get_size(v);
466 356281 const uint8_t *d = lbm_heap_array_get_data_ro(v);
467
2/4
✓ Branch 0 taken 356281 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 356281 times.
✗ Branch 3 not taken.
356281 if (s > 0 && d != NULL) {
468
2/2
✓ Branch 0 taken 356277 times.
✓ Branch 1 taken 4 times.
356281 if (f_lbm_array(fv, (uint32_t)s, (uint8_t*)d)) {
469 356277 return FLATTEN_VALUE_OK;
470 }
471 } else {
472 return FLATTEN_VALUE_ERROR_ARRAY;
473 }
474 4 }break;
475 default:
476 return FLATTEN_VALUE_ERROR_CANNOT_BE_FLATTENED;
477 }
478 68 return FLATTEN_VALUE_ERROR_BUFFER_TOO_SMALL;
479 }
480
481 84 lbm_value handle_flatten_error(int err_val) {
482
1/5
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 84 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
84 switch (err_val) {
483 case FLATTEN_VALUE_ERROR_CANNOT_BE_FLATTENED:
484 return ENC_SYM_EERROR;
485 case FLATTEN_VALUE_ERROR_BUFFER_TOO_SMALL: /* fall through */
486 case FLATTEN_VALUE_ERROR_FATAL:
487 return ENC_SYM_FATAL_ERROR;
488 84 case FLATTEN_VALUE_ERROR_CIRCULAR: /* fall through */
489 case FLATTEN_VALUE_ERROR_MAXIMUM_DEPTH:
490 84 return ENC_SYM_EERROR;
491 case FLATTEN_VALUE_ERROR_ARRAY: /* fall through */
492 case FLATTEN_VALUE_ERROR_NOT_ENOUGH_MEMORY:
493 return ENC_SYM_MERROR;
494 }
495 return ENC_SYM_NIL;
496 }
497
498 103874 lbm_value flatten_value(lbm_value v) {
499
500 103874 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 18 times.
✓ Branch 1 taken 103856 times.
103874 if (array_cell == ENC_SYM_MERROR) {
503 18 return array_cell;
504 }
505
506 lbm_flat_value_t fv;
507
508 103856 lbm_array_header_t *array = NULL;
509 103856 int required_mem = flatten_value_size(v, false);
510
2/2
✓ Branch 0 taken 103772 times.
✓ Branch 1 taken 84 times.
103856 if (required_mem > 0) {
511 103772 array = (lbm_array_header_t *)lbm_malloc(sizeof(lbm_array_header_t));
512
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 103750 times.
103772 if (array == NULL) {
513 22 lbm_set_car_and_cdr(array_cell, ENC_SYM_NIL, ENC_SYM_NIL);
514 22 return ENC_SYM_MERROR;
515 }
516
517 103750 bool r = lbm_start_flatten(&fv, (lbm_uint)required_mem);
518
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 103750 times.
103750 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 103750 times.
✗ Branch 1 not taken.
103750 if (flatten_value_c(&fv, v) == FLATTEN_VALUE_OK) {
525 // it would be wasteful to run finish_flatten here.
526 103750 r = true;
527 } else {
528 r = false;
529 }
530
531
1/2
✓ Branch 0 taken 103750 times.
✗ Branch 1 not taken.
103750 if (r) {
532 // lift flat_value
533 103750 array->data = (lbm_uint*)fv.buf;
534 103750 array->size = fv.buf_size;
535 103750 lbm_set_car(array_cell, (lbm_uint)array);
536 103750 array_cell = lbm_set_ptr_type(array_cell, LBM_TYPE_ARRAY);
537 103750 return array_cell;
538 }
539 }
540 84 lbm_set_car_and_cdr(array_cell, ENC_SYM_NIL, ENC_SYM_NIL);
541 84 return handle_flatten_error(required_mem);
542 }
543
544 // ------------------------------------------------------------
545 // Unflattening
546 184047 static bool extract_byte(lbm_flat_value_t *v, uint8_t *r) {
547
1/2
✓ Branch 0 taken 184047 times.
✗ Branch 1 not taken.
184047 if (v->buf_size >= v->buf_pos + 1) {
548 184047 *r = v->buf[v->buf_pos++];
549 184047 return true;
550 }
551 return false;
552 }
553
554 666928 static bool extract_word(lbm_flat_value_t *v, uint32_t *r) {
555 666928 bool res = false;
556
1/2
✓ Branch 0 taken 666928 times.
✗ Branch 1 not taken.
666928 if (v->buf_size >= v->buf_pos + 4) {
557 666928 uint32_t tmp = 0;
558 666928 tmp |= (lbm_value)v->buf[v->buf_pos++];
559 666928 tmp = tmp << 8 | (uint32_t)v->buf[v->buf_pos++];
560 666928 tmp = tmp << 8 | (uint32_t)v->buf[v->buf_pos++];
561 666928 tmp = tmp << 8 | (uint32_t)v->buf[v->buf_pos++];
562 666928 *r = tmp;
563 666928 res = true;
564 }
565 666928 return res;
566 }
567
568 49094 static bool extract_dword(lbm_flat_value_t *v, uint64_t *r) {
569 49094 bool res = false;
570
1/2
✓ Branch 0 taken 49094 times.
✗ Branch 1 not taken.
49094 if (v->buf_size >= v->buf_pos + 8) {
571 49094 uint64_t tmp = 0;
572 49094 tmp |= (lbm_value)v->buf[v->buf_pos++];
573 49094 tmp = tmp << 8 | (uint64_t)v->buf[v->buf_pos++];
574 49094 tmp = tmp << 8 | (uint64_t)v->buf[v->buf_pos++];
575 49094 tmp = tmp << 8 | (uint64_t)v->buf[v->buf_pos++];
576 49094 tmp = tmp << 8 | (uint64_t)v->buf[v->buf_pos++];
577 49094 tmp = tmp << 8 | (uint64_t)v->buf[v->buf_pos++];
578 49094 tmp = tmp << 8 | (uint64_t)v->buf[v->buf_pos++];
579 49094 tmp = tmp << 8 | (uint64_t)v->buf[v->buf_pos++];
580 49094 *r = tmp;
581 49094 res = true;;
582 }
583 49094 return res;
584 }
585
586 1089138 static int lbm_unflatten_value_atom(lbm_flat_value_t *v, lbm_value *res) {
587
588 1089138 uint8_t curr = v->buf[v->buf_pos++];
589
590
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1089137 times.
1089138 if (v->buf_size <= v->buf_pos) return UNFLATTEN_MALFORMED;
591
592
16/17
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 13905 times.
✓ Branch 3 taken 184047 times.
✓ Branch 4 taken 94509 times.
✓ Branch 5 taken 60 times.
✓ Branch 6 taken 45744 times.
✓ Branch 7 taken 28 times.
✓ Branch 8 taken 18772 times.
✓ Branch 9 taken 95 times.
✓ Branch 10 taken 88241 times.
✓ Branch 11 taken 94852 times.
✓ Branch 12 taken 89 times.
✓ Branch 13 taken 86 times.
✓ Branch 14 taken 359170 times.
✓ Branch 15 taken 189537 times.
✓ Branch 16 taken 1 times.
1089137 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 13905 case S_SYM_VALUE: {
611 lbm_uint tmp;
612 bool b;
613 #ifndef LBM64
614 10853 b = extract_word(v, &tmp);
615 #else
616 3052 b = extract_dword(v, &tmp);
617 #endif
618
1/2
✓ Branch 0 taken 13905 times.
✗ Branch 1 not taken.
13905 if (b) {
619 13905 *res = lbm_enc_sym(tmp);
620 13905 return UNFLATTEN_OK;
621 }
622 return UNFLATTEN_MALFORMED;
623 }
624 184047 case S_BYTE_VALUE: {
625 uint8_t tmp;
626 184047 bool b = extract_byte(v, &tmp);
627
1/2
✓ Branch 0 taken 184047 times.
✗ Branch 1 not taken.
184047 if (b) {
628 184047 *res = lbm_enc_char((uint8_t)tmp);
629 184047 return UNFLATTEN_OK;
630 }
631 return UNFLATTEN_MALFORMED;
632 }
633 94509 case S_I28_VALUE: {
634 uint32_t tmp;
635 bool b;
636 94509 b = extract_word(v, &tmp);
637
1/2
✓ Branch 0 taken 94509 times.
✗ Branch 1 not taken.
94509 if (b) {
638 94509 *res = lbm_enc_i((int32_t)tmp);
639 94509 return UNFLATTEN_OK;
640 }
641 return UNFLATTEN_MALFORMED;
642 }
643 60 case S_U28_VALUE: {
644 uint32_t tmp;
645 bool b;
646 60 b = extract_word(v, &tmp);
647
1/2
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
60 if (b) {
648 60 *res = lbm_enc_u((uint32_t)tmp);
649 60 return UNFLATTEN_OK;
650 }
651 return UNFLATTEN_MALFORMED;
652 }
653 45744 case S_I56_VALUE: {
654 uint64_t tmp;
655 bool b;
656 45744 b = extract_dword(v, &tmp);
657
1/2
✓ Branch 0 taken 45744 times.
✗ Branch 1 not taken.
45744 if (b) {
658 #ifndef LBM64
659 *res = lbm_enc_i64((int64_t)tmp);
660 #else
661 45744 *res = lbm_enc_i((int64_t)tmp);
662 #endif
663 45744 return UNFLATTEN_OK;
664 }
665 return UNFLATTEN_MALFORMED;
666 }
667 28 case S_U56_VALUE: {
668 uint64_t tmp;
669 bool b;
670 28 b = extract_dword(v, &tmp);
671
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 if (b) {
672 #ifndef LBM64
673 *res = lbm_enc_u64(tmp);
674 #else
675 28 *res = lbm_enc_u(tmp);
676 #endif
677 28 return UNFLATTEN_OK;
678 }
679 return UNFLATTEN_MALFORMED;
680 }
681 18772 case S_FLOAT_VALUE: {
682 uint32_t tmp;
683 bool b;
684 18772 b = extract_word(v, &tmp);
685
1/2
✓ Branch 0 taken 18772 times.
✗ Branch 1 not taken.
18772 if (b) {
686 lbm_float f;
687 18772 memcpy(&f, &tmp, sizeof(lbm_float));
688 18772 lbm_value im = lbm_enc_float(f);
689
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18772 times.
18772 if (lbm_is_symbol_merror(im)) {
690 return UNFLATTEN_GC_RETRY;
691 }
692 18772 *res = im;
693 18772 return UNFLATTEN_OK;
694 }
695 return UNFLATTEN_MALFORMED;
696 }
697 95 case S_DOUBLE_VALUE: {
698 uint64_t tmp;
699 bool b;
700 95 b = extract_dword(v, &tmp);
701
1/2
✓ Branch 0 taken 95 times.
✗ Branch 1 not taken.
95 if (b) {
702 double f;
703 95 memcpy(&f, &tmp, sizeof(uint64_t));
704 95 lbm_value im = lbm_enc_double(f);
705
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 95 times.
95 if (lbm_is_symbol_merror(im)) {
706 return UNFLATTEN_GC_RETRY;
707 }
708 95 *res = im;
709 95 return UNFLATTEN_OK;
710 }
711 return UNFLATTEN_MALFORMED;
712 }
713 88241 case S_I32_VALUE: {
714 uint32_t tmp;
715
1/2
✓ Branch 0 taken 88241 times.
✗ Branch 1 not taken.
88241 if (extract_word(v, &tmp)) {
716 88241 lbm_value im = lbm_enc_i32((int32_t)tmp);
717
2/2
✓ Branch 0 taken 148 times.
✓ Branch 1 taken 88093 times.
88241 if (lbm_is_symbol_merror(im)) {
718 148 return UNFLATTEN_GC_RETRY;
719 }
720 88093 *res = im;
721 88093 return UNFLATTEN_OK;
722 }
723 return UNFLATTEN_MALFORMED;
724 }
725 94852 case S_U32_VALUE: {
726 uint32_t tmp;
727
1/2
✓ Branch 0 taken 94852 times.
✗ Branch 1 not taken.
94852 if (extract_word(v, &tmp)) {
728 94852 lbm_value im = lbm_enc_u32(tmp);
729
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 94804 times.
94852 if (lbm_is_symbol_merror(im)) {
730 48 return UNFLATTEN_GC_RETRY;
731 }
732 94804 *res = im;
733 94804 return UNFLATTEN_OK;
734 }
735 return UNFLATTEN_MALFORMED;
736 }
737 89 case S_I64_VALUE: {
738 89 uint64_t tmp = 0;
739
1/2
✓ Branch 0 taken 89 times.
✗ Branch 1 not taken.
89 if (extract_dword(v, &tmp)) {
740 89 lbm_value im = lbm_enc_i64((int64_t)tmp);
741
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 89 times.
89 if (lbm_is_symbol_merror(im)) {
742 return UNFLATTEN_GC_RETRY;
743 }
744 89 *res = im;
745 89 return UNFLATTEN_OK;
746 }
747 return UNFLATTEN_MALFORMED;
748 }
749 86 case S_U64_VALUE: {
750 86 uint64_t tmp = 0;
751
1/2
✓ Branch 0 taken 86 times.
✗ Branch 1 not taken.
86 if (extract_dword(v, &tmp)) {
752 86 lbm_value im = lbm_enc_u64(tmp);
753
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 86 times.
86 if (lbm_is_symbol_merror(im)) {
754 return UNFLATTEN_GC_RETRY;
755 }
756 86 *res = im;
757 86 return UNFLATTEN_OK;
758 }
759 return UNFLATTEN_MALFORMED;
760 }
761 359170 case S_LBM_ARRAY: {
762 uint32_t num_elt;
763 // TODO: Feels slightly wrong with <= here.
764
3/4
✓ Branch 0 taken 359170 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 359165 times.
✓ Branch 3 taken 5 times.
359170 if (extract_word(v, &num_elt) && v->buf_pos + num_elt <= v->buf_size) {
765
2/2
✓ Branch 0 taken 358559 times.
✓ Branch 1 taken 606 times.
359165 if (lbm_heap_allocate_array(res, num_elt)) {
766 358559 lbm_array_header_t *arr = (lbm_array_header_t*)lbm_car(*res);
767 358559 lbm_uint num_bytes = num_elt;
768 358559 memcpy(arr->data, v->buf + v->buf_pos, num_bytes);
769 358559 v->buf_pos += num_bytes;
770 } else {
771 606 return UNFLATTEN_GC_RETRY;
772 }
773 358559 return UNFLATTEN_OK;
774 }
775 5 return UNFLATTEN_MALFORMED;
776 }
777 189537 case S_SYM_STRING: {
778 lbm_uint sym_id;
779 189537 lbm_uint max_bytes = v->buf_size - v->buf_pos;
780 189537 lbm_uint num_bytes = max_bytes;
781 189537 bool found_null = false;
782
2/2
✓ Branch 0 taken 758811 times.
✓ Branch 1 taken 1 times.
758812 for (lbm_uint i = 0; i < max_bytes; i ++) {
783
2/2
✓ Branch 0 taken 189536 times.
✓ Branch 1 taken 569275 times.
758811 if (v->buf[v->buf_pos + i] == 0) {
784 189536 num_bytes = i + 1;
785 189536 found_null = true;
786 189536 break;
787 }
788 }
789
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 189536 times.
189537 if (!found_null) return UNFLATTEN_MALFORMED;
790
1/2
✓ Branch 0 taken 189536 times.
✗ Branch 1 not taken.
189536 if (lbm_add_symbol((char *)(v->buf + v->buf_pos), &sym_id)) {
791 189536 v->buf_pos += num_bytes;
792 189536 *res = lbm_enc_sym(sym_id);
793 189536 return UNFLATTEN_OK;
794 }
795 return UNFLATTEN_GC_RETRY;
796 }
797 1 default:
798 1 return UNFLATTEN_MALFORMED;
799 }
800 }
801
802 // ////////////////////////////////////////////////////////////
803 // Pointer-reversal-esque "stackless" deserialization of
804 // flattened (serialized) trees.
805 //
806 // Initially:
807 // curr = LBM_NULL; v->buf = { ... }
808 //
809 // FORWARDS PHASE:
810 // Cons case:
811 // Reading conses from the buffer builds a backpointing list.
812 // Placeholder element acts as a 1 bit "visited" field.
813 //
814 // curr = p; v->buf = {S_CONS, ... }
815 // =>
816 // curr = [p, placeholder]; v->buf = { ... }
817 //
818 // Lisp array case:
819 // An Array tag in the buffer leads to the creation of an array
820 // with a backptr in the last element position. Placeholder element
821 // is not needed as LBM-Arrays have a built in index field (used by GC)
822 // that can keep a count of how far along the array we have progressed.
823 //
824 // curr = p; v->buf = {S_LBM_LISP_ARRAY, ... }
825 // =>
826 // curr = [| nil ... p |]; v->buf = { ... }
827 //
828 // Atom case:
829 // Reading an atom triggers a backwards traversal along the backpointer
830 // structure.
831 //
832 // curr = X; v->buf = {any_atom, ... } example integer, string.
833 // =>
834 // val = unflatten_atom(v->buf); v->buf = { ... }
835 //
836 // BACKWARDS PHASE: Start the backwards traversal:
837 //
838 // Case on X
839 // LBM_NULL;
840 // => Done! result = val
841 //
842 // [p, placeholder];
843 // =>
844 // [p, val] Base case. Finishes back traversal.
845 // Go back to FORWARDS PHASE.
846 //
847 //
848 // [p, val0];
849 // =>
850 // tmp = [val0, val]; val = tmp; curr = p; continue backwards with value pointing to recently constructed final subresult.
851 //
852 //
853 // [| a b nil ... p |]
854 // =>
855 // [| a b val ... p |] Base case. Finishes back traversal.
856 // Array internal index field keeps track of write position.
857 // Go back to FORWARDS PHASE.
858 //
859 //
860 // [| a0 a1 ... an p |]
861 // =>
862 // tmp = [| a0 a1 ... an val |]; val = tmp; curr = p; continue backwards
863 //
864
865 115752 static int lbm_unflatten_value_nostack(sharing_table *st, lbm_uint *target_map, lbm_flat_value_t *v, lbm_value *res) {
866 115752 bool done = false;
867 lbm_value val0;
868 115752 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 2064335 times.
✗ Branch 1 not taken.
2064335 while (!done) {
873 2064335 int32_t set_ix = -1;
874
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 2064292 times.
2064335 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 2064335 bool is_leaf = true;
903 2064335 lbm_value unflattened = ENC_SYM_NIL;
904
905
2/2
✓ Branch 0 taken 974769 times.
✓ Branch 1 taken 1089566 times.
2064335 if (v->buf[v->buf_pos] == S_CONS) {
906 974769 lbm_value tmp = curr;
907 974769 curr = lbm_cons(tmp, ENC_SYM_PLACEHOLDER);
908
2/2
✓ Branch 0 taken 704 times.
✓ Branch 1 taken 974065 times.
974769 if (lbm_is_symbol_merror(curr)) return UNFLATTEN_GC_RETRY;
909
910
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 974027 times.
974065 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 974065 v->buf_pos ++;
918 974065 is_leaf = false;
919
2/2
✓ Branch 0 taken 359 times.
✓ Branch 1 taken 1089207 times.
1089566 } else if (v->buf[v->buf_pos] == S_LBM_LISP_ARRAY) {
920 uint32_t size;
921 359 v->buf_pos ++;
922 359 bool b = extract_word(v, &size);
923
1/2
✓ Branch 0 taken 359 times.
✗ Branch 1 not taken.
359 if (b) {
924 // Abort if buffer cannot possibly hold that size array.
925 // a flattened byte occupies 2 bytes in fv. so smallest possible
926 // array is array of bytes.
927
4/4
✓ Branch 0 taken 357 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 354 times.
359 if (size > 0 && v->buf_pos + (size * 2) > v->buf_size) return UNFLATTEN_MALFORMED;
928 lbm_value array;
929 356 lbm_heap_allocate_lisp_array(&array, size);
930 356 lbm_array_header_extended_t *header = (lbm_array_header_extended_t*)lbm_car(array);
931
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 354 times.
356 if (size == 0) {
932 2 unflattened = array;
933
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (set_ix >= 0) target_map[set_ix] = array;
934 } else {
935 354 is_leaf = false;
936 354 lbm_value *arrdata = (lbm_value*)header->data;
937
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 354 times.
354 if (lbm_is_symbol_merror(array)) return UNFLATTEN_GC_RETRY;
938 354 header->index = 0;
939 354 arrdata[size-1] = curr; // backptr
940 354 curr = array;
941
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 349 times.
354 if (set_ix >= 0) target_map[set_ix] = curr;
942 }
943 } else {
944 return UNFLATTEN_MALFORMED;
945 }
946
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1089206 times.
1089207 } else if (v->buf[v->buf_pos] == 0) {
947 1 return UNFLATTEN_MALFORMED;
948
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 1089138 times.
1089206 } 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 1089138 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 1089138 times.
1089138 if (set_ix >= 0) {
991 target_map[set_ix] = unflattened;
992 }
993
2/2
✓ Branch 0 taken 810 times.
✓ Branch 1 taken 1088328 times.
1089138 if (e_val != UNFLATTEN_OK) {
994 810 return e_val;
995 }
996 }
997
998
2/2
✓ Branch 0 taken 1088398 times.
✓ Branch 1 taken 974419 times.
2062817 if (is_leaf) {
999 1088398 val0 = unflattened;
1000
4/4
✓ Branch 0 taken 1941727 times.
✓ Branch 1 taken 114234 times.
✓ Branch 2 taken 968464 times.
✓ Branch 3 taken 973263 times.
3997688 while (lbm_dec_ptr(curr) != LBM_PTR_NULL &&
1001 1941727 lbm_cdr(curr) != ENC_SYM_PLACEHOLDER) { // has done left
1002
2/2
✓ Branch 0 taken 1255 times.
✓ Branch 1 taken 967209 times.
968464 if ( lbm_type_of(curr) == LBM_TYPE_LISPARRAY) {
1003 1255 lbm_array_header_extended_t *header = (lbm_array_header_extended_t*)lbm_car(curr);
1004 1255 lbm_value *arrdata = (lbm_value*)header->data;
1005 1255 uint32_t arrlen = (uint32_t)(header->size / sizeof(lbm_value));
1006
2/2
✓ Branch 0 taken 354 times.
✓ Branch 1 taken 901 times.
1255 if (header->index == arrlen - 1) {
1007 354 lbm_value prev = arrdata[arrlen-1];
1008 354 header->index = 0;
1009 354 arrdata[arrlen-1] = val0;
1010 354 val0 = curr;
1011 354 curr = prev;
1012 } else {
1013 901 arrdata[header->index++] = val0;
1014 901 break;
1015 }
1016 } else {
1017 967209 lbm_value prev = lbm_car(curr);
1018 967209 lbm_value r0 = lbm_cdr(curr);
1019 967209 lbm_set_cdr(curr, val0);
1020 967209 lbm_set_car(curr, r0);
1021 967209 val0 = curr;
1022 967209 curr = prev;
1023 }
1024 }
1025
2/2
✓ Branch 0 taken 114234 times.
✓ Branch 1 taken 974164 times.
1088398 if (lbm_dec_ptr(curr) == LBM_PTR_NULL) {
1026 114234 *res = val0; // done
1027 114234 break;
1028
2/2
✓ Branch 0 taken 973263 times.
✓ Branch 1 taken 901 times.
974164 } 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 973263 times.
✗ Branch 1 not taken.
973263 } else if (lbm_cdr(curr) == ENC_SYM_PLACEHOLDER) {
1031 973263 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 114234 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 114145 bool lbm_unflatten_value(lbm_flat_value_t *v, lbm_value *res) {
1053 114145 bool b = false;
1054 #ifdef LBM_ALWAYS_GC
1055 lbm_perform_gc();
1056 #endif
1057 114145 int r = lbm_unflatten_value_nostack(NULL,NULL, v,res);
1058
2/2
✓ Branch 0 taken 1506 times.
✓ Branch 1 taken 112639 times.
114145 if (r == UNFLATTEN_GC_RETRY) {
1059 1506 lbm_perform_gc();
1060 1506 v->buf_pos = 0;
1061 1506 r = lbm_unflatten_value_nostack(NULL,NULL,v,res);
1062 }
1063
2/3
✓ Branch 0 taken 114133 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
114145 switch(r) {
1064 114133 case UNFLATTEN_OK:
1065 114133 b = true;
1066 114133 break;
1067 case UNFLATTEN_GC_RETRY:
1068 *res = ENC_SYM_MERROR;
1069 break;
1070 12 default:
1071 12 *res = ENC_SYM_EERROR;
1072 12 break;
1073 }
1074 // Do not free the flat value buffer here.
1075 // there are 2 cases:
1076 // 1: unflatten was called from lisp code -> GC removes the buffer.
1077 // 2: unflatten called from event processing -> event processor frees buffer.
1078 114145 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