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