GCC Code Coverage Report


Directory: ../src/
File: eval_cps.c
Date: 2026-10-16 14:49:47
Coverage Exec Excl Total
Lines: 94.2% 3188 0 3384
Functions: 97.3% 217 0 223
Branches: 77.4% 1113 0 1438

Line Branch Exec Source
1 /*
2 Copyright 2018, 2020 - 2026 Joel Svensson svenssonjoel@yahoo.se
3 2025 - 2026 Rasmus Söderhielm rasmus.soderhielm@gmail.com
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 /*
20 eval_cps uses setjmp/longjmp for error handling.
21
22 setjmp/longjmp behavior is undefined:
23 * If the function which called setjmp() returns before longjmp() is
24 called, the behavior is undefined. Some kind of subtle or unsubtle
25 chaos is sure to result.
26
27 * If, in a multithreaded program, a longjmp() call employs an env buffer
28 that was initialized by a call to setjmp() in a different thread, the
29 behavior is undefined.
30
31 As I understand it MISRA C guidelines prohibit (quite completely) any use
32 of setjmp/longjmp for safety-critical applications.
33
34 The LispBM eval_cps evaluator is careful to not fall into either of the
35 undefined behavior situations by:
36 1. setjmp is called in the lbm_run_eval() function which is responsible
37 for running all evaluation.
38 2. longjmp is called as part of the ERROR_CTX/ERROR_AT_CTX macros which
39 are executed by the evaluator in error cases.
40 3. the jump buffers are static (error_jmp_buf).
41 4. The error_ctx/error_at_ctx functions are static.
42 5. The ERROR_CTX/ERROR_AT_CTX/READ_ERROR_CTX macros are only called in
43 static functions.
44 TODO Check static functions that call ERROR_CTX are not called by any
45 non-static function, The only call-chain that leads to a longjmp must
46 originate in lbm_run_eval().
47 => (if the TODO is dealt with) it is impossible to trigger the undefined
48 conditions.
49
50 Possible alternative to setjmp/longjmp is to cram all of the stuff
51 that eval_cps does into a single function and use GOTO to jump between the
52 different continuation and evaluation and error cases. This would be even
53 worse from a "structured programming" viewpoint, but it would also possibly
54 be a bit a faster.
55
56 The setjmp/longjmp error handling applied here is well contained and has
57 been rigorously tested over time. There is a bit more to do in terms of
58 documenting the error handling choices and it would also be nice to statically
59 check/verify that we are ruling out the undefined use cases.
60 */
61
62 /* A short NOTE on Dependability and Fault-Tolerance
63
64 LispBM does not aim to be resilient to bit flips in memory caused by cosmic rays
65 or to externally corrupted or broken memory.
66 The goal is to ensure that LispBM itself does not cause memory corruption.
67 */
68
69 #include <lbm_memory.h>
70 #include <lbm_types.h>
71 #include "symrepr.h"
72 #include "heap.h"
73 #include "env.h"
74 #include "eval_cps.h"
75 #include "stack.h"
76 #include "fundamental.h"
77 #include "extensions.h"
78 #include "tokpar.h"
79 #include "lbm_channel.h"
80 #include "print.h"
81 #include "platform_mutex.h"
82 #include "platform_timestamp.h"
83 #include "lbm_flat_value.h"
84
85 #include <setjmp.h>
86 #include <stdarg.h>
87 #include <stdnoreturn.h>
88
89 #if __STDC_VERSION__ < 201112L
90 // Lower than C11
91 #undef noreturn
92 #define noreturn __attribute__ ((__noreturn__))
93 #endif
94
95 // The cold attribute is a hint for the compiler to do the following:
96 // - Optimize function for size.
97 // - Possibly move cold functions to a separate area of memory.
98 // - Act as a branch predition hint for branches leading to "cold" calls.
99 #ifdef __clang__
100 #define sizeopt __attribute__((cold, noinline))
101 #else
102 #define sizeopt __attribute__((cold, noinline, optimize("Os")))
103 #endif
104
105 static jmp_buf error_jmp_buf;
106
107 #define S_TO_US(X) (lbm_uint)((X) * 1000000)
108
109 #define DEC_CONTINUATION(x) (((x) & ~LBM_CONTINUATION_INTERNAL) >> LBM_ADDRESS_SHIFT)
110 #define IS_CONTINUATION(x) (((x) & LBM_CONTINUATION_INTERNAL) == LBM_CONTINUATION_INTERNAL)
111 #define CONTINUATION(x) (((x) << LBM_ADDRESS_SHIFT) | LBM_CONTINUATION_INTERNAL)
112
113 #define DONE CONTINUATION(0)
114 #define SET_GLOBAL_ENV CONTINUATION(1)
115 #define BIND_TO_KEY_REST CONTINUATION(2)
116 #define IF CONTINUATION(3)
117 #define PROGN_REST CONTINUATION(4)
118 #define APPLICATION_ARGS CONTINUATION(5)
119 #define AND CONTINUATION(6)
120 #define OR CONTINUATION(7)
121 #define WAIT CONTINUATION(8)
122 #define MATCH CONTINUATION(9)
123 #define APPLICATION_START CONTINUATION(10)
124 #define EVAL_R CONTINUATION(11)
125 #define RESUME CONTINUATION(12)
126 #define CLOSURE_ARGS CONTINUATION(13)
127 #define EXIT_ATOMIC CONTINUATION(14)
128 #define READ_NEXT_TOKEN CONTINUATION(15)
129 #define READ_APPEND_CONTINUE CONTINUATION(16)
130 #define READ_EVAL_CONTINUE CONTINUATION(17)
131 #define READ_EXPECT_CLOSEPAR CONTINUATION(18)
132 #define READ_DOT_TERMINATE CONTINUATION(19)
133 #define READ_DONE CONTINUATION(20)
134 #define READ_START_BYTEARRAY CONTINUATION(21)
135 #define READ_APPEND_BYTEARRAY CONTINUATION(22)
136 #define MAP CONTINUATION(23)
137 #define MATCH_GUARD CONTINUATION(24)
138 #define TERMINATE CONTINUATION(25)
139 #define PROGN_VAR CONTINUATION(26)
140 #define SETQ CONTINUATION(27)
141 #define MOVE_TO_FLASH CONTINUATION(28)
142 #define MOVE_VAL_TO_FLASH_DISPATCH CONTINUATION(29)
143 #define MOVE_LIST_TO_FLASH CONTINUATION(30)
144 #define CLOSE_LIST_IN_FLASH CONTINUATION(31)
145 #define QQ_EXPAND_START CONTINUATION(32)
146 #define QQ_EXPAND CONTINUATION(33)
147 #define QQ_APPEND CONTINUATION(34)
148 #define QQ_EXPAND_LIST CONTINUATION(35)
149 #define QQ_LIST CONTINUATION(36)
150 #define KILL CONTINUATION(37)
151 #define LOOP CONTINUATION(38)
152 #define LOOP_CONDITION CONTINUATION(39)
153 #define MERGE_REST CONTINUATION(40)
154 #define MERGE_LAYER CONTINUATION(41)
155 #define CLOSURE_ARGS_REST CONTINUATION(42)
156 #define MOVE_ARRAY_ELTS_TO_FLASH CONTINUATION(43)
157 #define POP_READER_FLAGS CONTINUATION(44)
158 #define EXCEPTION_HANDLER CONTINUATION(45)
159 #define RECV_TO CONTINUATION(46)
160 #define WRAP_RESULT CONTINUATION(47)
161 #define RECV_TO_RETRY CONTINUATION(48)
162 #define READ_START_ARRAY CONTINUATION(49)
163 #define READ_APPEND_ARRAY CONTINUATION(50)
164 #define LOOP_ENV_PREP CONTINUATION(51)
165 #define READ_NEXT_TOKEN_GRAB_ROW CONTINUATION(52)
166 #define NUM_CONTINUATIONS 53
167
168 #define FM_NEED_GC -1
169 #define FM_NO_MATCH -2
170 #define FM_PATTERN_ERROR -3
171
172 typedef enum {
173 BL_OK = 0,
174 BL_NO_MEMORY,
175 BL_INCORRECT_KEY
176 } binding_location_status;
177
178 #define FB_OK 0
179 #define FB_TYPE_ERROR -1
180
181 #ifdef LBM_USE_ERROR_LINENO
182 #define ERROR_AT_CTX(err_val, at) error_at_ctx(err_val, at, __LINE__)
183 #define ERROR_CTX(err_val) error_ctx(err_val, __LINE__)
184 #define READ_ERROR_CTX(row, col) read_error_ctx(row, col, __LINE__)
185 #else
186 #define ERROR_AT_CTX(err_val, at) error_at_ctx(err_val, at)
187 #define ERROR_CTX(err_val) error_ctx(err_val)
188 #define READ_ERROR_CTX(row, col) read_error_ctx(row, col)
189 #endif
190
191 // ////////////////////////////////////////////////////////////
192 // Local variables used in sort and merge
193 lbm_value symbol_x = ENC_SYM_NIL;
194 lbm_value symbol_y = ENC_SYM_NIL;
195 #ifdef CLEAN_UP_CLOSURES
196 static lbm_value clean_cl_env_symbol = ENC_SYM_NIL;
197 #endif
198
199 // ////////////////////////////////////////////////////////////
200 // Error strings
201 const char* lbm_error_str_parse_eof = "End of parse stream.";
202 const char* lbm_error_str_parse_dot = "Incorrect usage of '.'.";
203 const char* lbm_error_str_parse_close = "Expected closing parenthesis.";
204 const char* lbm_error_str_num_args = "Incorrect number of arguments.";
205 const char* lbm_error_str_forbidden_in_atomic = "Operation is forbidden in an atomic block.";
206 const char* lbm_error_str_no_number = "Argument(s) must be a number.";
207 const char* lbm_error_str_not_a_boolean = "Argument must be t or nil (true or false).";
208 const char* lbm_error_str_incorrect_arg = "Incorrect argument.";
209 const char* lbm_error_str_var_outside_progn = "Usage of var outside of progn.";
210 const char* lbm_error_str_flash_not_possible = "Value cannot be written to flash.";
211 const char* lbm_error_str_flash_error = "Error writing to flash.";
212 const char* lbm_error_str_flash_full = "Flash memory is full.";
213 const char* lbm_error_str_variable_not_bound = "Variable not bound.";
214 const char* lbm_error_str_read_no_mem = "Out of memory while reading.";
215 const char* lbm_error_str_qq_expand = "Quasiquotation expansion error.";
216 const char* lbm_error_str_not_applicable = "Value is not applicable.";
217 const char* lbm_error_str_built_in = "Cannot redefine built-in.";
218
219 static lbm_value lbm_error_suspect;
220 static bool lbm_error_has_suspect = false;
221
222
223 // ////////////////////////////////////////////////////////////
224 // Prototypes for locally used functions (static)
225 static uint32_t lbm_mailbox_free_space_for_cid(lbm_cid cid);
226 static void apply_apply(lbm_value *args, lbm_uint nargs, eval_context_t *ctx);
227 static int gc(void);
228 #ifdef LBM_USE_ERROR_LINENO
229 static noreturn void error_ctx(lbm_value, int line_no);
230 static noreturn void error_at_ctx(lbm_value err_val, lbm_value at, int line_no);
231 #else
232 static noreturn void error_ctx(lbm_value);
233 static noreturn void error_at_ctx(lbm_value err_val, lbm_value at);
234 #endif
235 static void mailbox_add_mail(eval_context_t *ctx, lbm_value mail);
236
237 // TODO: Optimize, In a large number of cases
238 // where WITH_GC is used, it is not really required to check is_symbol_merror.
239 // Just checking is_symbol should be enough.
240 // Given the number of calls to WITH_GC this could save some code
241 // space and potentially also be a slight speedup.
242 // TODO: profile.
243 #ifdef LBM_ALWAYS_GC
244 #define WITH_GC(y, x) \
245 gc(); \
246 (y) = (x); \
247 if (lbm_is_symbol_merror((y))) { \
248 ERROR_CTX(ENC_SYM_MERROR); \
249 }
250
251 #define WITH_GC_RMBR_1(y, x, r) \
252 lbm_gc_mark_phase(r); \
253 gc(); \
254 (y) = (x); \
255 if (lbm_is_symbol_merror((y))) { \
256 ERROR_CTX(ENC_SYM_MERROR); \
257 }
258
259 #else
260
261 #define WITH_GC(y, x) \
262 (y) = (x); \
263 if (lbm_is_symbol_merror((y))) { \
264 gc(); \
265 (y) = (x); \
266 if (lbm_is_symbol_merror((y))) { \
267 ERROR_CTX(ENC_SYM_MERROR); \
268 } \
269 /* continue executing statements below */ \
270 }
271 #define WITH_GC_RMBR_1(y, x, r) \
272 (y) = (x); \
273 if (lbm_is_symbol_merror((y))) { \
274 lbm_gc_mark_phase(r); \
275 gc(); \
276 (y) = (x); \
277 if (lbm_is_symbol_merror((y))) { \
278 ERROR_CTX(ENC_SYM_MERROR); \
279 } \
280 /* continue executing statements below */ \
281 }
282 #endif
283
284 #define DROP(c) \
285 do { \
286 if (lbm_is_ptr(c)) { \
287 lbm_cons_t *_cell = lbm_ref_cell(c); \
288 c = _cell->cdr; \
289 } \
290 } while (0)
291
292 #define EXTRACT(c, a) \
293 do { \
294 if (lbm_is_ptr(c)) { \
295 lbm_cons_t *_cell = lbm_ref_cell(c); \
296 a = _cell->car; \
297 c = _cell->cdr; \
298 } else { \
299 a = ENC_SYM_NIL; \
300 } \
301 } while (0)
302
303 #define EXTRACT_NO_ADVANCE(c, a) \
304 do { \
305 if (lbm_is_ptr(c)) { \
306 lbm_cons_t *_cell = lbm_ref_cell(c); \
307 a = _cell->car; \
308 } else { \
309 a = ENC_SYM_NIL; \
310 } \
311 } while (0)
312
313 // ////////////////////////////////////////////////////////////
314 // Context queues
315 typedef struct {
316 eval_context_t *first;
317 eval_context_t *last;
318 } eval_context_queue_t;
319
320 static eval_context_queue_t blocked = {NULL, NULL};
321 static eval_context_queue_t queue = {NULL, NULL};
322
323 lbm_mutex_t qmutex;
324 bool qmutex_initialized = false;
325
326 static void enqueue_ctx(eval_context_queue_t *q, eval_context_t *ctx);
327
328 // The currently executing context.
329 eval_context_t *ctx_running = NULL;
330 volatile bool lbm_system_sleeping = false;
331
332 static volatile bool gc_requested = false;
333 11518 void lbm_request_gc(void) {
334 11518 gc_requested = true;
335 11518 }
336
337 /*
338 On ChibiOs the CH_CFG_ST_FREQUENCY setting in chconf.h sets the
339 resolution of the timer used for sleep operations. If this is set
340 to 10KHz the resolution is 100us.
341
342 The CH_CFG_ST_TIMEDELTA specifies the minimum number of ticks that
343 can be safely specified in a timeout directive (wonder if that
344 means sleep-period). The timedelta is set to 2.
345
346 If I have understood these correctly it means that the minimum
347 sleep duration possible is 2 * 100us = 200us.
348 */
349
350 #define EVAL_CPS_DEFAULT_STACK_SIZE 256
351 // The intended unit on the QUOTA is us (microseconds) and this should
352 // be ensured by the platform support libraries for a given platform.
353 #define EVAL_TIME_QUOTA 400
354 #define EVAL_CPS_MIN_SLEEP 200
355 #define EVAL_STEPS_QUOTA 10
356
357 #ifdef LBM_USE_TIME_QUOTA
358 static volatile uint32_t eval_time_refill = EVAL_TIME_QUOTA;
359 static uint32_t eval_time_quota = EVAL_TIME_QUOTA;
360 static uint32_t eval_current_quota = 0;
361 3 void lbm_set_eval_time_quota(uint32_t quota) {
362 3 eval_time_refill = quota;
363 3 }
364 #else
365 static volatile uint32_t eval_steps_refill = EVAL_STEPS_QUOTA;
366 static uint32_t eval_steps_quota = EVAL_STEPS_QUOTA;
367 36 void lbm_set_eval_step_quota(uint32_t quota) {
368 36 eval_steps_refill = quota;
369 36 }
370 #endif
371
372 static volatile uint32_t eval_cps_run_state = EVAL_CPS_STATE_DEAD;
373 static volatile uint32_t eval_cps_next_state = EVAL_CPS_STATE_NONE;
374 static volatile uint32_t eval_cps_next_state_arg = 0;
375 static volatile bool eval_cps_state_changed = false;
376
377 8136 sizeopt static void usleep_nonsense(uint32_t us) {
378 (void) us;
379 8136 }
380
381 ✗ sizeopt static bool dynamic_load_nonsense(const char *sym, const char **code) {
382 (void) sym;
383 (void) code;
384 ✗ return false;
385 }
386
387 ✗ sizeopt static int printf_nonsense(const char *fmt, ...) {
388 (void) fmt;
389 ✗ return 0;
390 }
391
392 ✗ sizeopt static void ctx_done_nonsense(eval_context_t *ctx) {
393 (void) ctx;
394 ✗ }
395
396 static void (*usleep_callback)(uint32_t) = usleep_nonsense;
397 static void (*ctx_done_callback)(eval_context_t *) = ctx_done_nonsense;
398 int (*lbm_printf_callback)(const char *, ...) = printf_nonsense;
399 static bool (*dynamic_load_callback)(const char *, const char **) = dynamic_load_nonsense;
400
401 29347 sizeopt void lbm_set_usleep_callback(void (*fptr)(uint32_t)) {
402
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 29346 times.
29347 if (fptr == NULL) usleep_callback = usleep_nonsense;
403 29346 else usleep_callback = fptr;
404 29347 }
405
406 29347 sizeopt void lbm_set_ctx_done_callback(void (*fptr)(eval_context_t *)) {
407
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 29346 times.
29347 if (fptr == NULL) ctx_done_callback = ctx_done_nonsense;
408 29346 else ctx_done_callback = fptr;
409 29347 }
410
411 29347 sizeopt void lbm_set_printf_callback(int (*fptr)(const char*, ...)){
412
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 29346 times.
29347 if (fptr == NULL) lbm_printf_callback = printf_nonsense;
413 29346 else lbm_printf_callback = fptr;
414 29347 }
415
416 29347 sizeopt void lbm_set_dynamic_load_callback(bool (*fptr)(const char *, const char **)) {
417
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 29346 times.
29347 if (fptr == NULL) dynamic_load_callback = dynamic_load_nonsense;
418 29346 else dynamic_load_callback = fptr;
419 29347 }
420
421 static volatile lbm_event_t *lbm_events = NULL;
422 static unsigned int lbm_events_head = 0;
423 static unsigned int lbm_events_tail = 0;
424 static unsigned int lbm_events_max = 0;
425 static bool lbm_events_full = false;
426 static lbm_mutex_t lbm_events_mutex;
427 static bool lbm_events_mutex_initialized = false;
428 static volatile lbm_cid lbm_event_handler_pid = -1;
429
430 4316 static unsigned int lbm_event_queue_item_count(void) {
431 4316 unsigned int res = lbm_events_max;
432
2/2
✓ Branch 0 taken 4311 times.
✓ Branch 1 taken 5 times.
4316 if (!lbm_events_full) {
433
1/2
✓ Branch 0 taken 4311 times.
✗ Branch 1 not taken.
4311 if (lbm_events_head >= lbm_events_tail) {
434 4311 res = lbm_events_head - lbm_events_tail;
435 } else {
436 ✗ res = lbm_events_max - lbm_events_tail + lbm_events_head;
437 }
438 }
439 4316 return res;
440 }
441
442 1 lbm_cid lbm_get_event_handler_pid(void) {
443 1 return lbm_event_handler_pid;
444 }
445
446 295 void lbm_set_event_handler_pid(lbm_cid pid) {
447 295 lbm_event_handler_pid = pid;
448 295 }
449
450 8 bool lbm_event_handler_exists(void) {
451 8 return(lbm_event_handler_pid > 0);
452 }
453
454 4429 static bool event_internal(lbm_event_type_t event_type, lbm_uint parameter, lbm_uint buf_ptr, lbm_uint buf_len) {
455 4429 bool r = false;
456
1/2
✓ Branch 0 taken 4429 times.
✗ Branch 1 not taken.
4429 if (lbm_events) {
457 4429 lbm_mutex_lock(&lbm_events_mutex);
458
2/2
✓ Branch 0 taken 4424 times.
✓ Branch 1 taken 5 times.
4429 if (!lbm_events_full) {
459 lbm_event_t event;
460 4424 event.type = event_type;
461 4424 event.parameter = parameter;
462 4424 event.buf_ptr = buf_ptr;
463 4424 event.buf_len = buf_len;
464 4424 lbm_events[lbm_events_head] = event;
465 4424 lbm_events_head = (lbm_events_head + 1) % lbm_events_max;
466 4424 lbm_events_full = lbm_events_head == lbm_events_tail;
467 4424 r = true;
468 }
469 4429 lbm_mutex_unlock(&lbm_events_mutex);
470 }
471 4429 return r;
472 }
473
474 5 bool lbm_event_define(lbm_value key, lbm_flat_value_t *fv) {
475 5 return event_internal(LBM_EVENT_DEFINE, key, (lbm_uint)fv->buf, fv->buf_size);
476 }
477
478 36 bool lbm_event_unboxed(lbm_value unboxed) {
479 36 lbm_uint t = lbm_type_of(unboxed);
480
4/4
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 15 times.
✓ Branch 3 taken 18 times.
36 if (t == LBM_TYPE_SYMBOL ||
481
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 7 times.
15 t == LBM_TYPE_I ||
482
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1 time.
8 t == LBM_TYPE_U ||
483 t == LBM_TYPE_CHAR) {
484
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 3 times.
35 if (lbm_event_handler_pid > 0) {
485
1/2
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
32 if (lbm_mailbox_free_space_for_cid(lbm_event_handler_pid) > lbm_event_queue_item_count()) {
486 32 return event_internal(LBM_EVENT_FOR_HANDLER, 0, (lbm_uint)unboxed, 0);
487 }
488 }
489 }
490 4 return false;
491 }
492
493 10335 bool lbm_event(lbm_flat_value_t *fv) {
494
2/2
✓ Branch 0 taken 4284 times.
✓ Branch 1 taken 6051 times.
10335 if (lbm_event_handler_pid > 0) {
495
1/2
✓ Branch 2 taken 4284 times.
✗ Branch 3 not taken.
4284 if (lbm_mailbox_free_space_for_cid(lbm_event_handler_pid) > lbm_event_queue_item_count()) {
496 4284 return event_internal(LBM_EVENT_FOR_HANDLER, 0, (lbm_uint)fv->buf, fv->buf_size);
497 }
498 }
499 6051 return false;
500 }
501
502 265369332 static bool lbm_event_pop(lbm_event_t *event) {
503 265369332 lbm_mutex_lock(&lbm_events_mutex);
504 265369331 bool r = false;
505
4/4
✓ Branch 0 taken 265364908 times.
✓ Branch 1 taken 4423 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 265364907 times.
265369331 if (lbm_events_head != lbm_events_tail || lbm_events_full) {
506 4424 *event = lbm_events[lbm_events_tail];
507 4424 lbm_events_tail = (lbm_events_tail + 1) % lbm_events_max;
508 4424 lbm_events_full = false;
509 4424 r = true;
510 }
511 265369331 lbm_mutex_unlock(&lbm_events_mutex);
512 265369331 return r;
513 }
514
515 6 bool lbm_event_queue_is_empty(void) {
516 6 lbm_mutex_lock(&lbm_events_mutex);
517 6 bool empty = false;
518
4/4
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 1 time.
6 if (lbm_events_head == lbm_events_tail && !lbm_events_full) {
519 4 empty = true;
520 }
521 6 lbm_mutex_unlock(&lbm_events_mutex);
522 6 return empty;
523 }
524
525 static bool eval_running = false;
526 static volatile bool blocking_extension = false;
527 static lbm_mutex_t blocking_extension_mutex;
528 static bool blocking_extension_mutex_initialized = false;
529 static lbm_uint blocking_extension_timeout_us = 0;
530 static bool blocking_extension_timeout = false;
531
532 static bool is_atomic = false;
533
534 // MODES
535 static volatile bool lbm_verbose = false;
536 static volatile bool lbm_hide_trapped_error = false;
537
538 9 void lbm_toggle_verbose(void) {
539 9 lbm_verbose = !lbm_verbose;
540 9 }
541
542 28440 void lbm_set_verbose(bool verbose) {
543 28440 lbm_verbose = verbose;
544 28440 }
545
546 111 void lbm_set_hide_trapped_error(bool hide) {
547 111 lbm_hide_trapped_error = hide;
548 111 }
549
550 3392 lbm_cid lbm_get_current_cid(void) {
551 3392 lbm_cid cid = -1;
552
1/2
✓ Branch 0 taken 3392 times.
✗ Branch 1 not taken.
3392 if (ctx_running)
553 3392 cid = ctx_running->id;
554 3392 return cid;
555 }
556
557 4611 eval_context_t *lbm_get_current_context(void) {
558 4611 return ctx_running;
559 }
560
561 #ifdef LBM_USE_TIME_QUOTA
562 ✗ void lbm_surrender_quota(void) {
563 // dummy;
564 ✗ }
565 #else
566 10 void lbm_surrender_quota(void) {
567 10 eval_steps_quota = 0;
568 10 }
569 #endif
570
571 /****************************************************/
572 /* Utilities used locally in this file */
573
574 502402 static inline lbm_array_header_t *assume_array(lbm_value a){
575 502402 return (lbm_array_header_t*)lbm_ref_cell(a)->car;
576 }
577
578 7445846 static lbm_value cons_with_gc(lbm_value head, lbm_value tail, lbm_value remember) {
579 lbm_value res;
580 #ifdef LBM_ALWAYS_GC
581 lbm_value always_gc_roots[3] = {head, tail, remember};
582 lbm_gc_mark_roots(always_gc_roots,3);
583 gc();
584 #endif
585
2/2
✓ Branch 1 taken 5435 times.
✓ Branch 2 taken 7440411 times.
7445846 if (!lbm_heap_num_free()) {
586 5435 lbm_value roots[3] = {head, tail, remember};
587 5435 lbm_gc_mark_roots(roots,3);
588 5435 gc();
589
2/2
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 5429 times.
5435 if (!lbm_heap_num_free()) {
590 6 ERROR_CTX(ENC_SYM_MERROR);
591 }
592 }
593
594 7445840 res = lbm_heap_state.freelist;
595 7445840 lbm_uint heap_ix = lbm_dec_ptr(res);
596 7445840 lbm_heap_state.freelist = lbm_heap_state.heap[heap_ix].cdr;
597 7445840 lbm_heap_state.num_free--;
598 7445840 lbm_heap_state.heap[heap_ix].car = head;
599 7445840 lbm_heap_state.heap[heap_ix].cdr = tail;
600 7445840 res = lbm_set_ptr_type(res, LBM_TYPE_CONS);
601 7445840 return res;
602 }
603
604 // ////////////////////////////////////////////////////////////
605 // Stack and CPS
606
607 // The CPS system is dependent on a diciplined use of a continuation
608 // stack! Continuations are pushed and popped from this stack and
609 // between the point in time when a continuation is pushed and when it is popped
610 // significant stack "traffic" may have occurred.
611 //
612 // In the evaluator, functions called things line "eval_X" usually sets up
613 // a stack-frame for a future exection of a "cont_X" continuation.
614 // Some kind of proof is needed that Things that pushed in a continuation setup-phase
615 // are all popped (no more, no less) in a continuation execution phase.
616 // continuations "cont_X" also may set up more continuations and in that
617 // case they may modify the stack in place or reuse in part or full the
618 // stack-frame associated with "cont_X"
619 //
620 // TODO: for each "eval_X" and "cont_X" pair write an argument or some kind
621 // of test that illustrates correct usage of the stack discipline.
622 //
623 // For now the argument of stack discipline being honored is that all tests
624 // pass and no tests end up in stack underflow.
625 //
626 // Stack overflow is easy to end up in, just write a non-tail-recursive but recursive funtions
627 // and run it for a suitably large input.
628 //
629 // * Potentially tricky situation is "trap" used on a call to a non-tail-recursive function that
630 // ends out exhausting stack.
631 // # Trap rewinds the stack to the point of trap so it should be perfectly safe to trap even in this situation.
632
633 // get_stack_ptr and pop_stack_ptr does in no tests reach
634 // the error condition. The check n <= sp is not really needed.
635 1460648491 static inline lbm_uint *get_stack_ptr(eval_context_t *ctx, unsigned int n) {
636 1460648491 lbm_uint index = ctx->K.sp - n;
637 1460648491 return &ctx->K.data[index];
638 }
639
640 // pop_stack_ptr is safe when no GC is performed and
641 // the values of the stack will be dropped.
642 38063933 static inline lbm_uint *pop_stack_ptr(eval_context_t *ctx, unsigned int n) {
643 38063933 ctx->K.sp -= n;
644 38063933 return &ctx->K.data[ctx->K.sp];
645 }
646
647 // stack_drop is assuming eval_cps uses a rigoursly tested and principled
648 // approach to stack management. If the stack is being abused by the runtime
649 // system this is usually very quickly discovered using the test suite as
650 // the behavior of the RTS becomes completely wacko.
651 701701247 static inline void stack_drop(eval_context_t *ctx, unsigned int n) {
652 701701247 ctx->K.sp -= n;
653 701701247 }
654
655 1588626452 static inline lbm_uint *stack_reserve(eval_context_t *ctx, unsigned int n) {
656
2/2
✓ Branch 0 taken 1588626440 times.
✓ Branch 1 taken 12 times.
1588626452 if (ctx->K.sp + n < ctx->K.size) {
657 1588626440 lbm_uint *ptr = &ctx->K.data[ctx->K.sp];
658 1588626440 ctx->K.sp += n;
659 1588626440 return ptr;
660 }
661 12 ERROR_CTX(ENC_SYM_STACK_ERROR);
662 }
663
664 10877 static void handle_flash_status(lbm_flash_status s) {
665
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10877 times.
10877 if ( s == LBM_FLASH_FULL) {
666 ✗ lbm_set_error_reason((char*)lbm_error_str_flash_full);
667 ✗ ERROR_CTX(ENC_SYM_EERROR);
668 }
669
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 10875 times.
10877 if (s == LBM_FLASH_WRITE_ERROR) {
670 2 lbm_set_error_reason((char*)lbm_error_str_flash_error);
671 2 ERROR_CTX(ENC_SYM_FATAL_ERROR);
672 }
673 10875 }
674
675 128 static void lift_array_flash(lbm_value flash_cell, bool bytearray, char *data, lbm_uint num_elt) {
676
677 lbm_array_header_t flash_array_header;
678 128 flash_array_header.size = num_elt;
679 128 flash_array_header.data = (lbm_uint*)data;
680 128 lbm_uint flash_array_header_ptr = 0;
681 128 handle_flash_status(lbm_write_const_raw((lbm_uint*)&flash_array_header,
682 sizeof(lbm_array_header_t) / sizeof(lbm_uint),
683 &flash_array_header_ptr));
684 128 handle_flash_status(write_const_car(flash_cell, flash_array_header_ptr));
685
2/2
✓ Branch 0 taken 92 times.
✓ Branch 1 taken 36 times.
128 lbm_uint t = bytearray ? ENC_SYM_ARRAY_TYPE : ENC_SYM_LISPARRAY_TYPE;
686 128 handle_flash_status(write_const_cdr(flash_cell, t));
687 128 }
688
689 // ////////////////////////////////////////////////////////////
690 // get_car and lbm_car
691
692 // lbm_car is a lower level operation that extracts a car field from a cons cell
693 // without any consideration of any additional type-tags associated with the cell.
694 // get_car is for list cons-cells only.
695
696 61251355 static inline void get_car_and_cdr(lbm_value a, lbm_value *a_car, lbm_value *a_cdr) {
697
2/2
✓ Branch 1 taken 61251298 times.
✓ Branch 2 taken 57 times.
61251355 if (lbm_is_cons(a)) {
698 61251298 lbm_cons_t *cell = lbm_ref_cell(a);
699 61251298 *a_car = cell->car;
700 61251298 *a_cdr = cell->cdr;
701
2/2
✓ Branch 1 taken 33 times.
✓ Branch 2 taken 24 times.
57 } else if (lbm_is_symbol_nil(a)) {
702 33 *a_car = *a_cdr = ENC_SYM_NIL;
703 } else {
704 24 ERROR_CTX(ENC_SYM_TERROR);
705 }
706 61251331 }
707
708 /* car cdr caar cadr replacements that are evaluator safe. */
709 22009378 static inline lbm_value get_car(lbm_value a) {
710
2/2
✓ Branch 1 taken 22009366 times.
✓ Branch 2 taken 12 times.
22009378 if (lbm_is_cons(a)) {
711 22009366 lbm_cons_t *cell = lbm_ref_cell(a);
712 22009366 return cell->car;
713
2/2
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 3 times.
12 } else if (lbm_is_symbol_nil(a)) {
714 9 return a;
715 }
716 3 ERROR_CTX(ENC_SYM_TERROR);
717 }
718
719 89539725 static inline lbm_value get_cdr(lbm_value a) {
720
2/2
✓ Branch 1 taken 89539683 times.
✓ Branch 2 taken 42 times.
89539725 if (lbm_is_cons(a)) {
721 89539683 lbm_cons_t *cell = lbm_ref_cell(a);
722 89539683 return cell->cdr;
723
1/2
✓ Branch 1 taken 42 times.
✗ Branch 2 not taken.
42 } else if (lbm_is_symbol_nil(a)) {
724 42 return a;
725 }
726 ✗ ERROR_CTX(ENC_SYM_TERROR);
727 }
728
729 44912620 static inline lbm_value get_cadr(lbm_value a) {
730
2/2
✓ Branch 1 taken 44912611 times.
✓ Branch 2 taken 9 times.
44912620 if (lbm_is_cons(a)) {
731 44912611 lbm_cons_t *cell = lbm_ref_cell(a);
732 44912611 lbm_value tmp = cell->cdr;
733
2/2
✓ Branch 1 taken 44779246 times.
✓ Branch 2 taken 133365 times.
44912611 if (lbm_is_cons(tmp)) {
734 44779246 return lbm_ref_cell(tmp)->car;
735
1/2
✓ Branch 1 taken 133365 times.
✗ Branch 2 not taken.
133365 } else if (lbm_is_symbol_nil(tmp)) {
736 133365 return tmp;
737 }
738
1/2
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
9 } else if (lbm_is_symbol_nil(a)) {
739 9 return a;
740 }
741 ✗ ERROR_CTX(ENC_SYM_TERROR);
742 }
743
744 // Allocate a binding and attach it to a list (if so desired)
745 134844527 static lbm_value allocate_binding(lbm_value key, lbm_value val, lbm_value the_cdr) {
746 #ifdef LBM_ALWAYS_GC
747 lbm_gc_mark_phase(key);
748 lbm_gc_mark_phase(val);
749 lbm_gc_mark_phase(the_cdr);
750 gc();
751 if (lbm_heap_num_free() < 2) {
752 ERROR_CTX(ENC_SYM_MERROR);
753 }
754 #else
755
2/2
✓ Branch 1 taken 230792 times.
✓ Branch 2 taken 134613735 times.
134844527 if (lbm_heap_num_free() < 2) {
756 230792 lbm_gc_mark_phase(key);
757 230792 lbm_gc_mark_phase(val);
758 230792 lbm_gc_mark_phase(the_cdr);
759 230792 gc();
760
2/2
✓ Branch 1 taken 36 times.
✓ Branch 2 taken 230756 times.
230792 if (lbm_heap_num_free() < 2) {
761 36 ERROR_CTX(ENC_SYM_MERROR);
762 }
763 }
764 #endif
765 // If num_free is calculated correctly, freelist is definitely a cons-cell.
766 134844491 lbm_cons_t* heap = lbm_heap_state.heap;
767 134844491 lbm_value binding_cell = lbm_heap_state.freelist;
768 134844491 lbm_uint binding_cell_ix = lbm_dec_ptr(binding_cell);
769 134844491 lbm_value list_cell = heap[binding_cell_ix].cdr;
770 134844491 lbm_uint list_cell_ix = lbm_dec_ptr(list_cell);
771 134844491 lbm_heap_state.freelist = heap[list_cell_ix].cdr;
772 134844491 lbm_heap_state.num_free -= 2;
773 134844491 heap[binding_cell_ix].car = key;
774 134844491 heap[binding_cell_ix].cdr = val;
775 134844491 heap[list_cell_ix].car = binding_cell;
776 134844491 heap[list_cell_ix].cdr = the_cdr;
777 134844491 return list_cell;
778 }
779
780 #define CLO_PARAMS 0
781 #define CLO_BODY 1
782 #define CLO_ENV 2
783 #define LOOP_BINDS 0
784 #define LOOP_COND 1
785 #define LOOP_BODY 2
786
787 39 static void atomic_error(void) {
788 39 lbm_set_error_reason((char*)lbm_error_str_forbidden_in_atomic);
789 39 ERROR_CTX(ENC_SYM_EERROR);
790 }
791
792 // block_current_ctx blocks a context until it is
793 // woken up externally or a timeout period of time passes.
794 // Blocking while in an atomic block would have bad consequences.
795 7898 static void block_current_ctx(uint32_t state, lbm_uint sleep_us, bool do_cont) {
796
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7898 times.
7898 if (is_atomic) atomic_error();
797 7898 ctx_running->timestamp = lbm_timestamp();
798 7898 ctx_running->sleep_us = sleep_us;
799 7898 ctx_running->state = state;
800 7898 ctx_running->app_cont = do_cont;
801 7898 enqueue_ctx(&blocked, ctx_running);
802 7898 ctx_running = NULL;
803 7898 }
804
805 // reblock an essentially already blocked context.
806 // Same as block but sets no new timestamp or sleep_us.
807 ✗ static void reblock_current_ctx(uint32_t state, bool do_cont) {
808 ✗ if (is_atomic) atomic_error();
809 ✗ ctx_running->state = state;
810 ✗ ctx_running->app_cont = do_cont;
811 ✗ enqueue_ctx(&blocked, ctx_running);
812 ✗ ctx_running = NULL;
813 ✗ }
814
815
816 92 lbm_flash_status lbm_write_const_array_padded(uint8_t *data, lbm_uint n, lbm_uint *res) {
817 92 lbm_uint full_words = n / sizeof(lbm_uint);
818 92 lbm_uint n_mod = n % sizeof(lbm_uint);
819
820
2/2
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 26 times.
92 if (n_mod == 0) { // perfect fit.
821 66 return lbm_write_const_raw((lbm_uint*)data, full_words, res);
822 } else {
823 26 lbm_uint last_word = 0;
824 26 memcpy(&last_word, &data[full_words * sizeof(lbm_uint)], n_mod);
825
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 16 times.
26 if (full_words >= 1) {
826 10 lbm_flash_status s = lbm_write_const_raw((lbm_uint*)data, full_words, res);
827
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 if ( s == LBM_FLASH_WRITE_OK) {
828 lbm_uint dummy;
829 10 s = lbm_write_const_raw(&last_word, 1, &dummy);
830 }
831 10 return s;
832 } else {
833 16 return lbm_write_const_raw(&last_word, 1, res);
834 }
835 }
836 }
837
838 /****************************************************/
839 /* Error message creation */
840
841 #define ERROR_MESSAGE_BUFFER_SIZE_BYTES 256
842
843 348876 sizeopt void print_env_ll(lbm_value e, char *buf, unsigned int size) {
844 lbm_value curr_l = e;
845
2/2
✓ Branch 1 taken 59376 times.
✓ Branch 2 taken 348876 times.
408252 while (lbm_type_of(curr_l) == LBM_TYPE_CONS) {
846 59376 lbm_print_value(buf, (size/2) - 1, lbm_caar(curr_l));
847 59376 lbm_print_value(buf + (size/2),size/2, lbm_cdr(lbm_car(curr_l)));
848 59376 lbm_printf_callback("\t%s = %s\n", buf, buf+(size/2));
849 59376 curr_l = lbm_cdr(curr_l);
850 }
851 348876 }
852
853 10572 sizeopt void print_environments(char *buf, unsigned int size) {
854
855 10572 lbm_printf_callback("\tCurrent local environment:\n");
856 10572 print_env_ll(ctx_running->curr_env, buf, size);
857 10572 lbm_printf_callback("\n\n\tCurrent global environment:\n");
858 10572 lbm_value *glob_env = lbm_get_global_env();
859
2/2
✓ Branch 0 taken 338304 times.
✓ Branch 1 taken 10572 times.
348876 for (int i = 0; i < GLOBAL_ENV_ROOTS; i ++) {
860 338304 print_env_ll(glob_env[i], buf, size);
861 }
862 10572 }
863
864 37870 sizeopt void print_error_value(char *buf, uint32_t bufsize, char *pre, lbm_value v, bool lookup) {
865
866 37870 lbm_print_value(buf, bufsize, v);
867 37870 lbm_printf_callback("%s %s\n",pre, buf);
868
2/2
✓ Branch 0 taken 25688 times.
✓ Branch 1 taken 12182 times.
37870 if (lookup) {
869
2/2
✓ Branch 1 taken 15384 times.
✓ Branch 2 taken 10304 times.
25688 if (lbm_is_symbol(v)) {
870
2/2
✓ Branch 1 taken 1731 times.
✓ Branch 2 taken 13653 times.
15384 if (lbm_dec_sym(v) >= RUNTIME_SYMBOLS_START) {
871 1731 lbm_value res = ENC_SYM_NIL;
872
4/4
✓ Branch 1 taken 1602 times.
✓ Branch 2 taken 129 times.
✓ Branch 3 taken 972 times.
✓ Branch 4 taken 630 times.
3333 if (lbm_env_lookup_b(&res, v, ctx_running->curr_env) ||
873 1602 lbm_global_env_lookup(&res, v)) {
874 1101 lbm_print_value(buf, bufsize, res);
875 1101 lbm_printf_callback(" bound to: %s\n", buf);
876 } else {
877 630 lbm_printf_callback(" UNDEFINED\n");
878 }
879 }
880 }
881 }
882 37870 }
883
884 12182 sizeopt static void print_error_message(lbm_value error,
885 bool has_at,
886 lbm_value at,
887 unsigned int row,
888 unsigned int col,
889 lbm_int row0,
890 lbm_int row1,
891 lbm_int cid,
892 char *name,
893 bool trapped) {
894 /* try to allocate a lbm_print_value buffer on the lbm_memory */
895 12182 char *buf = lbm_malloc_reserve(ERROR_MESSAGE_BUFFER_SIZE_BYTES);
896
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12182 times.
12182 if (!buf) {
897 ✗ lbm_printf_callback("Error: Not enough memory to show a human readable error message\n");
898 ✗ return;
899 }
900
2/2
✓ Branch 0 taken 11900 times.
✓ Branch 1 taken 282 times.
12182 if (trapped) {
901 11900 print_error_value(buf, ERROR_MESSAGE_BUFFER_SIZE_BYTES," Error (trapped):", error, false);
902 } else {
903 282 print_error_value(buf, ERROR_MESSAGE_BUFFER_SIZE_BYTES," Error:", error, false);
904 }
905
2/2
✓ Branch 1 taken 171 times.
✓ Branch 2 taken 12011 times.
12182 if (lbm_is_symbol_merror(error)) {
906 171 lbm_printf_callback("\n Heap cells free: %d\n", lbm_heap_state.num_free);
907 171 lbm_printf_callback(" Mem longest free: %d\n\n", lbm_memory_longest_free());
908 }
909
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12182 times.
12182 if (name) {
910 ✗ lbm_printf_callback( " CTX: %d \"%s\"\n", cid, name);
911 } else {
912 12182 lbm_printf_callback( " CTX: %d\n", cid);
913 }
914 12182 print_error_value(buf, ERROR_MESSAGE_BUFFER_SIZE_BYTES," Current:", ctx_running->curr_exp, true);
915 // An error can have both a set suspect that can be more detailed than the "at"
916 // show both if present!
917
2/2
✓ Branch 0 taken 1887 times.
✓ Branch 1 taken 10295 times.
12182 if (lbm_error_has_suspect) {
918 1887 print_error_value(buf, ERROR_MESSAGE_BUFFER_SIZE_BYTES," At:", lbm_error_suspect, true);
919 1887 lbm_error_has_suspect = false;
920 }
921 // TODO: Should perhaps be called has_in and be meant to capture a bit
922 // of the surrounding of where the error happened.
923
2/2
✓ Branch 0 taken 11619 times.
✓ Branch 1 taken 563 times.
12182 if (has_at) {
924 11619 print_error_value(buf, ERROR_MESSAGE_BUFFER_SIZE_BYTES," In:", at, true);
925 }
926
927 12182 lbm_printf_callback("\n");
928
929
4/4
✓ Branch 1 taken 12146 times.
✓ Branch 2 taken 36 times.
✓ Branch 3 taken 141 times.
✓ Branch 4 taken 12005 times.
12182 if (lbm_is_symbol(error) &&
930 error == ENC_SYM_RERROR) {
931 141 lbm_printf_callback(" Line: %u\n", row);
932 141 lbm_printf_callback(" Column: %u\n", col);
933
2/2
✓ Branch 0 taken 6628 times.
✓ Branch 1 taken 5413 times.
12041 } else if (row0 >= 0) {
934
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 6619 times.
6628 if (row1 < 0) lbm_printf_callback(" Starting at row: %d\n", row0);
935 6619 else lbm_printf_callback(" Between row %d and %d\n", row0, row1);
936 }
937
938 12182 lbm_printf_callback("\n");
939
940
2/2
✓ Branch 0 taken 3006 times.
✓ Branch 1 taken 9176 times.
12182 if (ctx_running->error_reason) {
941 3006 lbm_printf_callback(" Reason: %s\n\n", ctx_running->error_reason);
942 }
943
2/2
✓ Branch 0 taken 10572 times.
✓ Branch 1 taken 1610 times.
12182 if (lbm_verbose) {
944 10572 lbm_print_value(buf, ERROR_MESSAGE_BUFFER_SIZE_BYTES, ctx_running->r);
945 10572 lbm_printf_callback(" Current intermediate result: %s\n\n", buf);
946
947 10572 print_environments(buf, ERROR_MESSAGE_BUFFER_SIZE_BYTES);
948
949 10572 lbm_printf_callback("\n Mailbox:\n");
950
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10572 times.
10572 for (unsigned int i = 0; i < ctx_running->num_mail; i ++) {
951 ✗ lbm_print_value(buf, ERROR_MESSAGE_BUFFER_SIZE_BYTES, ctx_running->mailbox[i]);
952 ✗ lbm_printf_callback(" %s\n", buf);
953 }
954 10572 lbm_printf_callback("\n Stack:\n");
955
2/2
✓ Branch 0 taken 185808 times.
✓ Branch 1 taken 10572 times.
196380 for (unsigned int i = 0; i < ctx_running->K.sp; i ++) {
956 185808 lbm_print_value(buf, ERROR_MESSAGE_BUFFER_SIZE_BYTES, ctx_running->K.data[i]);
957 185808 lbm_printf_callback(" %s\n", buf);
958 }
959 }
960 12182 lbm_free(buf);
961 }
962
963 /****************************************************/
964 /* Tokenizing and parsing */
965
966 411269 static bool create_string_channel(char *str, lbm_value *res, lbm_value dep) {
967
968 411269 lbm_char_channel_t *chan = NULL;
969 411269 lbm_string_channel_state_t *st = NULL;
970
971 411269 st = (lbm_string_channel_state_t*)lbm_malloc(sizeof(lbm_string_channel_state_t));
972
2/2
✓ Branch 0 taken 1452 times.
✓ Branch 1 taken 409817 times.
411269 if (st == NULL) {
973 1452 return false;
974 }
975 409817 chan = (lbm_char_channel_t*)lbm_malloc(sizeof(lbm_char_channel_t));
976
2/2
✓ Branch 0 taken 723 times.
✓ Branch 1 taken 409094 times.
409817 if (chan == NULL) {
977 723 lbm_free(st);
978 723 return false;
979 }
980
981 409094 lbm_create_string_char_channel(st, chan, str);
982 409094 lbm_value cell = lbm_heap_allocate_cell(LBM_TYPE_CHANNEL, (lbm_uint) chan, ENC_SYM_CHANNEL_TYPE);
983
2/2
✓ Branch 0 taken 4288 times.
✓ Branch 1 taken 404806 times.
409094 if (cell == ENC_SYM_MERROR) {
984 4288 lbm_free(st);
985 4288 lbm_free(chan);
986 4288 return false;
987 }
988
989 404806 lbm_char_channel_set_dependency(chan, dep);
990
991 404806 *res = cell;
992 404806 return true;
993 }
994
995 /****************************************************/
996 /* Queue functions */
997
998 1605194 static void queue_iterator_nm(eval_context_queue_t *q, ctx_fun f, void *arg1, void *arg2) {
999 eval_context_t *curr;
1000 1605194 curr = q->first;
1001
1002
2/2
✓ Branch 0 taken 21230 times.
✓ Branch 1 taken 1605194 times.
1626424 while (curr != NULL) {
1003 21230 f(curr, arg1, arg2);
1004 21230 curr = curr->next;
1005 }
1006 1605194 }
1007
1008 6 void lbm_all_ctxs_iterator(ctx_fun f, void *arg1, void *arg2) {
1009 6 lbm_mutex_lock(&qmutex);
1010 6 queue_iterator_nm(&blocked, f, arg1, arg2);
1011 6 queue_iterator_nm(&queue, f, arg1, arg2);
1012
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (ctx_running) f(ctx_running, arg1, arg2);
1013 6 lbm_mutex_unlock(&qmutex);
1014 6 }
1015
1016 102 void lbm_running_iterator(ctx_fun f, void *arg1, void *arg2){
1017 102 lbm_mutex_lock(&qmutex);
1018 102 queue_iterator_nm(&queue, f, arg1, arg2);
1019 102 lbm_mutex_unlock(&qmutex);
1020 102 }
1021
1022 102 void lbm_blocked_iterator(ctx_fun f, void *arg1, void *arg2){
1023 102 lbm_mutex_lock(&qmutex);
1024 102 queue_iterator_nm(&blocked, f, arg1, arg2);
1025 102 lbm_mutex_unlock(&qmutex);
1026 102 }
1027
1028 262006216 static void enqueue_ctx_nm(eval_context_queue_t *q, eval_context_t *ctx) {
1029
2/2
✓ Branch 0 taken 255761306 times.
✓ Branch 1 taken 6244910 times.
262006216 if (q->last == NULL) {
1030 255761306 ctx->prev = NULL;
1031 255761306 ctx->next = NULL;
1032 255761306 q->first = ctx;
1033 255761306 q->last = ctx;
1034 } else {
1035 6244910 ctx->prev = q->last;
1036 6244910 ctx->next = NULL;
1037 6244910 q->last->next = ctx;
1038 6244910 q->last = ctx;
1039 }
1040 262006216 }
1041
1042 85216 static void enqueue_ctx(eval_context_queue_t *q, eval_context_t *ctx) {
1043 85216 lbm_mutex_lock(&qmutex);
1044 85216 enqueue_ctx_nm(q,ctx);
1045 85216 lbm_mutex_unlock(&qmutex);
1046 85216 }
1047
1048 // //////////////////////////////////////////////////
1049 // If CID was a type that is impossible to create
1050 // other than by "spawning a thread", we could
1051 // make direct use of the fact that it is an index into
1052 // lbm_memory that identifies a context.
1053 // We would still need to ensure by inspection
1054 // that the value stored is a context.
1055 //
1056 // Currently any access to a context via its CID is
1057 // done by searching all queues for the context and then modifying it.
1058 // This is a SAFE way of accessing the context as it enables
1059 // detection of state CID.
1060 //
1061 // Alternative: A CID should be a heap cell (context_address . symbol)
1062 // where no program can generate symbol and the type of (context_address . symbol)
1063 // is LBM_TYPE_CID.
1064 // This would cost one heap cell per context but, it would
1065 // give O(1) lookup of context structure which
1066 // is useful in for example message delivery.
1067
1068 23355 static eval_context_t *lookup_ctx_nm(eval_context_queue_t *q, lbm_cid cid) {
1069 eval_context_t *curr;
1070 23355 curr = q->first;
1071
2/2
✓ Branch 0 taken 13455 times.
✓ Branch 1 taken 9900 times.
23355 while (curr != NULL) {
1072
1/2
✓ Branch 0 taken 13455 times.
✗ Branch 1 not taken.
13455 if (curr->id == cid) {
1073 13455 return curr;
1074 }
1075 ✗ curr = curr->next;
1076 }
1077 9900 return NULL;
1078 }
1079
1080 12 static eval_context_t *lookup_ctx_name_nm(eval_context_queue_t *q, char *name, lbm_uint name_len) {
1081 eval_context_t *curr;
1082 12 curr = q->first;
1083
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 9 times.
12 while (curr != NULL) {
1084
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 if (curr->name && strncmp(curr->name, name, name_len) == 0) {
1085 3 return curr;
1086 }
1087 ✗ curr = curr->next;
1088 }
1089 9 return NULL;
1090 }
1091
1092
1093 // Unlinks a context from a queue (doubly linked list).
1094 // Unlinking must be preceded by a lookup, both within the same
1095 // lock of a mutex.
1096 7748 static void unlink_ctx_nm(eval_context_queue_t *q, eval_context_t *ctx) {
1097
1098
1/2
✓ Branch 0 taken 7748 times.
✗ Branch 1 not taken.
7748 if (ctx->prev == NULL) { // ctx is first in queue
1099 7748 q->first = ctx->next;
1100 } else {
1101 ✗ ctx->prev->next = ctx->next;
1102 }
1103
1104
2/2
✓ Branch 0 taken 7745 times.
✓ Branch 1 taken 3 times.
7748 if (ctx->next == NULL) { // ctx is last in queue
1105 7745 q->last = ctx->prev;
1106 } else {
1107 3 ctx->next->prev = ctx->prev;
1108 }
1109 7748 ctx->next = NULL;
1110 7748 ctx->prev = NULL;
1111 7748 }
1112
1113 /* End execution of the running context. */
1114 30533 static void finish_ctx(void) {
1115
1116 // Reset atomic status if this finish_ctx is part of an
1117 // untrapper error or a self-kill within an atomic block.
1118 30533 is_atomic = false;
1119
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30533 times.
30533 if (!ctx_running) {
1120 ✗ return;
1121 }
1122
2/2
✓ Branch 0 taken 288 times.
✓ Branch 1 taken 30245 times.
30533 if (ctx_running->id == lbm_event_handler_pid) {
1123 288 lbm_event_handler_pid = -1;
1124 }
1125 /* Drop the continuation stack immediately to free up lbm_memory */
1126 30533 lbm_stack_free(&ctx_running->K);
1127 30533 ctx_done_callback(ctx_running);
1128
1129 30533 lbm_free(ctx_running->name); //free name if in LBM_MEM
1130
1131 30533 lbm_memory_free((lbm_uint*)ctx_running->error_reason); //free error_reason if in LBM_MEM
1132
1133 30533 lbm_memory_free((lbm_uint*)ctx_running->mailbox);
1134 30533 lbm_memory_free((lbm_uint*)ctx_running);
1135 30533 ctx_running = NULL;
1136 }
1137
1138 120 static void context_exists(eval_context_t *ctx, void *cid, void *b) {
1139
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 96 times.
120 if (ctx->id == *(lbm_cid*)cid) {
1140 24 *(bool*)b = true;
1141 }
1142 120 }
1143
1144 1896 sizeopt void lbm_set_error_suspect(lbm_value suspect) {
1145 1896 lbm_error_suspect = suspect;
1146 1896 lbm_error_has_suspect = true;
1147 1896 }
1148
1149 2185 sizeopt void lbm_set_error_reason(const char *error_str) {
1150
2/2
✓ Branch 0 taken 2178 times.
✓ Branch 1 taken 7 times.
2185 if (ctx_running != NULL) {
1151 2178 ctx_running->error_reason = error_str;
1152 }
1153 2185 }
1154
1155 // Not possible to CONS_WITH_GC in error_ctx_base (potential loop)
1156 #ifdef LBM_USE_ERROR_LINENO
1157 1960 sizeopt static noreturn void error_ctx_base(lbm_value err_val, bool has_at, lbm_value at, unsigned int row, unsigned int column, int line_no) {
1158 #else
1159 10573 sizeopt static noreturn void error_ctx_base(lbm_value err_val, bool has_at, lbm_value at, unsigned int row, unsigned int column) {
1160 #endif
1161
4/4
✓ Branch 0 taken 12182 times.
✓ Branch 1 taken 351 times.
✓ Branch 2 taken 282 times.
✓ Branch 3 taken 11900 times.
12533 bool print_trapped = !lbm_hide_trapped_error && (ctx_running->flags & EVAL_CPS_CONTEXT_FLAG_TRAP_UNROLL_RETURN);
1162 // An error always exists atomic status.
1163 12533 is_atomic = false;
1164
1165
2/2
✓ Branch 0 taken 351 times.
✓ Branch 1 taken 12182 times.
12533 if (!(lbm_hide_trapped_error &&
1166
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 351 times.
351 (ctx_running->flags & EVAL_CPS_CONTEXT_FLAG_TRAP_UNROLL_RETURN))) {
1167 12182 print_error_message(err_val,
1168 has_at,
1169 at,
1170 row,
1171 column,
1172 ctx_running->row0,
1173 ctx_running->row1,
1174 ctx_running->id,
1175 12182 ctx_running->name,
1176 print_trapped
1177 );
1178 }
1179 #ifdef LBM_USE_ERROR_LINENO
1180
2/2
✓ Branch 0 taken 1609 times.
✓ Branch 1 taken 351 times.
1960 if (!lbm_hide_trapped_error) {
1181 1609 lbm_printf_callback("eval_cps.c line number: %d\n", line_no);
1182 }
1183 #endif
1184
2/2
✓ Branch 0 taken 257 times.
✓ Branch 1 taken 12276 times.
12533 if (ctx_running->flags & EVAL_CPS_CONTEXT_FLAG_TRAP) {
1185
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 257 times.
257 if (lbm_heap_num_free() < 3) {
1186 ✗ gc();
1187 }
1188
1189
1/2
✓ Branch 1 taken 257 times.
✗ Branch 2 not taken.
257 if (lbm_heap_num_free() >= 3) {
1190 257 lbm_value msg = lbm_cons(err_val, ENC_SYM_NIL);
1191 257 msg = lbm_cons(lbm_enc_i(ctx_running->id), msg);
1192 257 msg = lbm_cons(ENC_SYM_EXIT_ERROR, msg);
1193
1/2
✓ Branch 1 taken 257 times.
✗ Branch 2 not taken.
257 if (!lbm_is_symbol_merror(msg)) {
1194 257 lbm_find_receiver_and_send(ctx_running->parent, msg);
1195 }
1196 }
1197 // context dies.
1198
3/4
✓ Branch 0 taken 12251 times.
✓ Branch 1 taken 25 times.
✓ Branch 2 taken 12251 times.
✗ Branch 3 not taken.
12276 } else if ((ctx_running->flags & EVAL_CPS_CONTEXT_FLAG_TRAP_UNROLL_RETURN) &&
1199 (err_val != ENC_SYM_FATAL_ERROR)) {
1200
1/2
✓ Branch 0 taken 49265 times.
✗ Branch 1 not taken.
49265 while (ctx_running->K.sp > 0) {
1201 49265 lbm_uint v = ctx_running->K.data[--ctx_running->K.sp];
1202
2/2
✓ Branch 0 taken 37014 times.
✓ Branch 1 taken 12251 times.
49265 if (v == EXCEPTION_HANDLER) { // context continues executing.
1203 12251 lbm_value *sptr = get_stack_ptr(ctx_running, 2);
1204 12251 lbm_set_car(sptr[0], ENC_SYM_EXIT_ERROR);
1205 12251 stack_reserve(ctx_running, 1)[0] = EXCEPTION_HANDLER;
1206 12251 ctx_running->app_cont = true;
1207 12251 ctx_running->r = err_val;
1208 12251 longjmp(error_jmp_buf, 1);
1209 }
1210 }
1211 err_val = ENC_SYM_FATAL_ERROR;
1212 }
1213 282 ctx_running->r = err_val;
1214 282 finish_ctx();
1215 282 longjmp(error_jmp_buf, 1);
1216 }
1217
1218 #ifdef LBM_USE_ERROR_LINENO
1219 1766 sizeopt static noreturn void error_at_ctx(lbm_value err_val, lbm_value at, int line_no) {
1220 1766 error_ctx_base(err_val, true, at, 0, 0, line_no);
1221 }
1222
1223 89 sizeopt static noreturn void error_ctx(lbm_value err_val, int line_no) {
1224 89 error_ctx_base(err_val, false, 0, 0, 0, line_no);
1225 }
1226
1227 105 sizeopt static noreturn void read_error_ctx(unsigned int row, unsigned int column, int line_no) {
1228 105 error_ctx_base(ENC_SYM_RERROR, false, 0, row, column, line_no);
1229 }
1230 #else
1231 10165 sizeopt static noreturn void error_at_ctx(lbm_value err_val, lbm_value at) {
1232 10165 error_ctx_base(err_val, true, at, 0, 0);
1233 }
1234
1235 336 sizeopt static noreturn void error_ctx(lbm_value err_val) {
1236 336 error_ctx_base(err_val, false, 0, 0, 0);
1237 }
1238
1239 72 sizeopt static noreturn void read_error_ctx(unsigned int row, unsigned int column) {
1240 72 error_ctx_base(ENC_SYM_RERROR, false, 0, row, column);
1241 }
1242 #endif
1243
1244 // successfully finish a context
1245 30251 static void ok_ctx(void) {
1246
2/2
✓ Branch 0 taken 199 times.
✓ Branch 1 taken 30052 times.
30251 if (ctx_running->flags & EVAL_CPS_CONTEXT_FLAG_TRAP) {
1247 lbm_value msg;
1248
1/4
✗ Branch 3 not taken.
✓ Branch 4 taken 199 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
199 WITH_GC(msg, lbm_heap_allocate_list_init(3,
1249 ENC_SYM_EXIT_OK,
1250 lbm_enc_i(ctx_running->id),
1251 ctx_running->r));
1252 199 lbm_find_receiver_and_send(ctx_running->parent, msg);
1253 }
1254 30251 finish_ctx();
1255 30251 }
1256
1257 267732851 static eval_context_t *dequeue_ctx_nm(eval_context_queue_t *q) {
1258
2/2
✓ Branch 0 taken 5781001 times.
✓ Branch 1 taken 261951850 times.
267732851 if (q->last == NULL) {
1259 5781001 return NULL;
1260 }
1261 // q->first should only be NULL if q->last is.
1262 261951850 eval_context_t *res = q->first;
1263
1264
2/2
✓ Branch 0 taken 255713891 times.
✓ Branch 1 taken 6237959 times.
261951850 if (q->first == q->last) { // One thing in queue
1265 255713891 q->first = NULL;
1266 255713891 q->last = NULL;
1267 } else {
1268 6237959 q->first = q->first->next;
1269 6237959 q->first->prev = NULL;
1270 }
1271 261951850 res->prev = NULL;
1272 261951850 res->next = NULL;
1273 261951850 return res;
1274 }
1275
1276 267732850 static void wake_up_ctxs_nm(void) {
1277 lbm_uint t_now;
1278 267732850 t_now = lbm_timestamp();
1279 267732850 eval_context_queue_t *q = &blocked;
1280 267732850 eval_context_t *curr = q->first;
1281
1282
2/2
✓ Branch 0 taken 4761126 times.
✓ Branch 1 taken 267732850 times.
272493976 while (curr != NULL) {
1283 lbm_uint t_diff;
1284 4761126 eval_context_t *next = curr->next;
1285
2/2
✓ Branch 0 taken 4257355 times.
✓ Branch 1 taken 503771 times.
4761126 if (LBM_IS_STATE_WAKE_UP_WAKABLE(curr->state)) {
1286
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4257355 times.
4257355 if ( curr->timestamp > t_now) {
1287 /* There was an overflow on the counter */
1288 #ifndef LBM64
1289 ✗ t_diff = (0xFFFFFFFF - curr->timestamp) + t_now;
1290 #else
1291 ✗ t_diff = (0xFFFFFFFFFFFFFFFF - curr->timestamp) + t_now;
1292 #endif
1293 } else {
1294 4257355 t_diff = t_now - curr->timestamp;
1295 }
1296
1297
2/2
✓ Branch 0 taken 46320 times.
✓ Branch 1 taken 4211035 times.
4257355 if (t_diff >= curr->sleep_us) {
1298 46320 eval_context_t *wake_ctx = curr;
1299
2/2
✓ Branch 0 taken 42669 times.
✓ Branch 1 taken 3651 times.
46320 if (curr == q->last) {
1300
2/2
✓ Branch 0 taken 3296 times.
✓ Branch 1 taken 39373 times.
42669 if (curr->prev) {
1301 3296 q->last = curr->prev;
1302 3296 q->last->next = NULL;
1303 } else {
1304 39373 q->first = NULL;
1305 39373 q->last = NULL;
1306 }
1307
2/2
✓ Branch 0 taken 3648 times.
✓ Branch 1 taken 3 times.
3651 } else if (curr->prev == NULL) {
1308 3648 q->first = curr->next;
1309 3648 q->first->prev = NULL;
1310 } else {
1311 3 curr->prev->next = curr->next;
1312
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (curr->next) {
1313 3 curr->next->prev = curr->prev;
1314 }
1315 }
1316 46320 wake_ctx->next = NULL;
1317 46320 wake_ctx->prev = NULL;
1318
2/2
✓ Branch 0 taken 180 times.
✓ Branch 1 taken 46140 times.
46320 if (LBM_IS_STATE_TIMEOUT(curr->state)) {
1319 180 mailbox_add_mail(wake_ctx, ENC_SYM_TIMEOUT);
1320 180 wake_ctx->r = ENC_SYM_TIMEOUT;
1321 }
1322 46320 wake_ctx->state = LBM_THREAD_STATE_READY;
1323 46320 enqueue_ctx_nm(&queue, wake_ctx);
1324 }
1325 }
1326 4761126 curr = next;
1327 }
1328 267732850 }
1329
1330 46393 static void yield_ctx(lbm_uint sleep_us) {
1331
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 46357 times.
46393 if (is_atomic) atomic_error();
1332 46357 ctx_running->timestamp = lbm_timestamp();
1333 46357 ctx_running->sleep_us = sleep_us;
1334 46357 ctx_running->state = LBM_THREAD_STATE_SLEEPING;
1335 46357 ctx_running->r = ENC_SYM_TRUE;
1336 46357 ctx_running->app_cont = true;
1337 46357 enqueue_ctx(&blocked,ctx_running);
1338 46357 ctx_running = NULL;
1339 46357 }
1340
1341 30997 static lbm_cid lbm_create_ctx_parent(lbm_value program, lbm_value env, lbm_uint stack_size, lbm_cid parent, uint32_t context_flags, char *name) {
1342
1343
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 30997 times.
30997 if (!lbm_is_cons(program)) return -1;
1344
1345 30997 eval_context_t *ctx = NULL;
1346 #ifdef LBM_ALWAYS_GC
1347 {
1348 lbm_uint roots[2] = {program, env};
1349 lbm_gc_mark_roots(roots, 2);
1350 gc();
1351 }
1352 #endif
1353 30997 ctx = (eval_context_t*)lbm_malloc(sizeof(eval_context_t));
1354
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30997 times.
30997 if (ctx == NULL) {
1355 ✗ lbm_uint roots[2] = {program, env};
1356 ✗ lbm_gc_mark_roots(roots, 2);
1357 ✗ gc();
1358 ✗ ctx = (eval_context_t*)lbm_malloc(sizeof(eval_context_t));
1359 }
1360
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30997 times.
30997 if (ctx == NULL) return -1;
1361 #ifdef LBM_ALWAYS_GC
1362 {
1363 lbm_uint roots[2] = {program, env};
1364 lbm_gc_mark_roots(roots, 2);
1365 gc();
1366 }
1367 #endif
1368
2/2
✓ Branch 1 taken 36 times.
✓ Branch 2 taken 30961 times.
30997 if (!lbm_stack_allocate(&ctx->K, stack_size)) {
1369 36 lbm_uint roots[2] = {program, env};
1370 36 lbm_gc_mark_roots(roots, 2);
1371 36 gc();
1372
1/2
✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
36 if (!lbm_stack_allocate(&ctx->K, stack_size)) {
1373 36 lbm_memory_free((lbm_uint*)ctx);
1374 36 return -1;
1375 }
1376 }
1377
1378 30961 lbm_value *mailbox = NULL;
1379 #ifdef LBM_ALWAYS_GC
1380 {
1381 lbm_uint roots[2] = {program, env};
1382 lbm_gc_mark_roots(roots, 2);
1383 gc();
1384 }
1385 #endif
1386 30961 mailbox = (lbm_value*)lbm_memory_allocate(EVAL_CPS_DEFAULT_MAILBOX_SIZE);
1387
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30961 times.
30961 if (mailbox == NULL) {
1388 ✗ lbm_value roots[2] = {program, env};
1389 ✗ lbm_gc_mark_roots(roots,2);
1390 ✗ gc();
1391 ✗ mailbox = (lbm_value *)lbm_memory_allocate(EVAL_CPS_DEFAULT_MAILBOX_SIZE);
1392 }
1393
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30961 times.
30961 if (mailbox == NULL) {
1394 ✗ lbm_stack_free(&ctx->K);
1395 ✗ lbm_memory_free((lbm_uint*)ctx);
1396 ✗ return -1;
1397 }
1398
1399 // TODO: Limit names to 19 chars + 1 char for 0? (or something similar).
1400
2/2
✓ Branch 0 taken 270 times.
✓ Branch 1 taken 30691 times.
30961 if (name) {
1401 270 lbm_uint name_len = strlen(name) + 1;
1402 #ifdef LBM_ALWAYS_GC
1403 {
1404 lbm_uint roots[2] = {program, env};
1405 lbm_gc_mark_roots(roots, 2);
1406 gc();
1407 }
1408 #endif
1409 270 ctx->name = lbm_malloc(name_len);
1410
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 270 times.
270 if (ctx->name == NULL) {
1411 ✗ lbm_value roots[2] = {program, env};
1412 ✗ lbm_gc_mark_roots(roots, 2);
1413 ✗ gc();
1414 ✗ ctx->name = lbm_malloc(name_len);
1415 }
1416
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 270 times.
270 if (ctx->name == NULL) {
1417 ✗ lbm_stack_free(&ctx->K);
1418 ✗ lbm_memory_free((lbm_uint*)mailbox);
1419 ✗ lbm_memory_free((lbm_uint*)ctx);
1420 ✗ return -1;
1421 }
1422 270 memcpy(ctx->name, name, name_len);
1423 } else {
1424 30691 ctx->name = NULL;
1425 }
1426
1427 30961 lbm_int cid = lbm_memory_address_to_ix((lbm_uint*)ctx);
1428
1429 30961 ctx->program = lbm_cdr(program);
1430 30961 ctx->curr_exp = lbm_car(program);
1431 30961 ctx->curr_env = env;
1432 30961 ctx->r = ENC_SYM_NIL;
1433 30961 ctx->error_reason = NULL;
1434 30961 ctx->mailbox = mailbox;
1435 30961 ctx->mailbox_size = EVAL_CPS_DEFAULT_MAILBOX_SIZE;
1436 30961 ctx->flags = context_flags;
1437 30961 ctx->num_mail = 0;
1438 30961 ctx->app_cont = false;
1439 30961 ctx->timestamp = 0;
1440 30961 ctx->sleep_us = 0;
1441 30961 ctx->state = LBM_THREAD_STATE_READY;
1442 30961 ctx->prev = NULL;
1443 30961 ctx->next = NULL;
1444
1445 30961 ctx->row0 = -1;
1446 30961 ctx->row1 = -1;
1447 30961 ctx->reader_stream = ENC_SYM_NIL;
1448
1449 30961 ctx->id = cid;
1450 30961 ctx->parent = parent;
1451
1452
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 30961 times.
30961 if (!lbm_push(&ctx->K, DONE)) {
1453 ✗ lbm_memory_free((lbm_uint*)ctx->mailbox);
1454 ✗ lbm_stack_free(&ctx->K);
1455 ✗ lbm_memory_free((lbm_uint*)ctx);
1456 ✗ return -1;
1457 }
1458
1459 30961 enqueue_ctx(&queue,ctx);
1460
1461 30961 return ctx->id;
1462 }
1463
1464 29267 lbm_cid lbm_create_ctx(lbm_value program, lbm_value env, lbm_uint stack_size, char *name) {
1465 // Creates a parentless context.
1466 29267 return lbm_create_ctx_parent(program,
1467 env,
1468 stack_size,
1469 -1,
1470 EVAL_CPS_CONTEXT_FLAG_NOTHING,
1471 name);
1472 }
1473
1474 180 bool lbm_mailbox_change_size(eval_context_t *ctx, lbm_uint new_size) {
1475
1476 180 lbm_value *mailbox = NULL;
1477 #ifdef LBM_ALWAYS_GC
1478 gc();
1479 #endif
1480 180 mailbox = (lbm_value*)lbm_memory_allocate(new_size);
1481
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 144 times.
180 if (mailbox == NULL) {
1482 36 gc();
1483 36 mailbox = (lbm_value *)lbm_memory_allocate(new_size);
1484 }
1485
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 144 times.
180 if (mailbox == NULL) {
1486 36 return false;
1487 }
1488
1489
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 144 times.
144 for (lbm_uint i = 0; i < ctx->num_mail; i ++ ) {
1490 ✗ mailbox[i] = ctx->mailbox[i];
1491 }
1492 144 lbm_memory_free(ctx->mailbox);
1493 144 ctx->mailbox = mailbox;
1494 144 ctx->mailbox_size = (uint32_t)new_size;
1495 144 return true;
1496 }
1497
1498 11611 static void mailbox_remove_mail(eval_context_t *ctx, lbm_uint ix) {
1499
1500
2/2
✓ Branch 0 taken 20510 times.
✓ Branch 1 taken 11611 times.
32121 for (lbm_uint i = ix; i < ctx->num_mail-1; i ++) {
1501 20510 ctx->mailbox[i] = ctx->mailbox[i+1];
1502 }
1503 11611 ctx->num_mail --;
1504 11611 }
1505
1506 12883 static void mailbox_add_mail(eval_context_t *ctx, lbm_value mail) {
1507
1508
2/2
✓ Branch 0 taken 756 times.
✓ Branch 1 taken 12127 times.
12883 if (ctx->num_mail >= ctx->mailbox_size) {
1509 756 mailbox_remove_mail(ctx, 0);
1510 }
1511
1512 12883 ctx->mailbox[ctx->num_mail] = mail;
1513 12883 ctx->num_mail ++;
1514 12883 }
1515
1516 /**************************************************************
1517 * Advance execution to the next expression in the program.
1518 * Assumes programs are not malformed. Apply_eval_program
1519 * ensures programs are lists ending in nil. The reader
1520 * ensures this likewise.
1521 *************************************************************/
1522 86006 static void advance_ctx(eval_context_t *ctx) {
1523
2/2
✓ Branch 0 taken 55905 times.
✓ Branch 1 taken 30101 times.
86006 if (ctx->program) { // fast not-nil check, assume cons if not nil.
1524 55905 stack_reserve(ctx, 1)[0] = DONE;
1525 55905 lbm_cons_t *cell = lbm_ref_cell(ctx->program);
1526 55905 ctx->curr_exp = cell->car;
1527 55905 ctx->program = cell->cdr;
1528 55905 ctx->curr_env = ENC_SYM_NIL;
1529 } else {
1530
1/2
✓ Branch 0 taken 30101 times.
✗ Branch 1 not taken.
30101 if (ctx_running == ctx) { // This should always be the case because of odd historical reasons.
1531 30101 ok_ctx();
1532 }
1533 }
1534 86006 }
1535
1536 108 bool lbm_unblock_ctx(lbm_cid cid, lbm_flat_value_t *fv) {
1537 108 return event_internal(LBM_EVENT_UNBLOCK_CTX, (lbm_uint)cid, (lbm_uint)fv->buf, fv->buf_size);
1538 }
1539
1540 36 bool lbm_unblock_ctx_r(lbm_cid cid) {
1541 36 lbm_mutex_lock(&blocking_extension_mutex);
1542 36 bool r = false;
1543 36 eval_context_t *found = NULL;
1544 36 lbm_mutex_lock(&qmutex);
1545 36 found = lookup_ctx_nm(&blocked, cid);
1546
2/4
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 36 times.
✗ Branch 3 not taken.
36 if (found && (LBM_IS_STATE_UNBLOCKABLE(found->state))) {
1547 36 unlink_ctx_nm(&blocked,found);
1548 36 found->state = LBM_THREAD_STATE_READY;
1549 36 enqueue_ctx_nm(&queue,found);
1550 36 r = true;
1551 }
1552 36 lbm_mutex_unlock(&qmutex);
1553 36 lbm_mutex_unlock(&blocking_extension_mutex);
1554 36 return r;
1555 }
1556
1557 // unblock unboxed is also safe for rmbr:ed things.
1558 // TODO: What happens if we unblock and the value is "merror"
1559 3 bool lbm_unblock_ctx_unboxed(lbm_cid cid, lbm_value unboxed) {
1560 3 lbm_mutex_lock(&blocking_extension_mutex);
1561 3 bool r = false;
1562 3 eval_context_t *found = NULL;
1563 3 lbm_mutex_lock(&qmutex);
1564 3 found = lookup_ctx_nm(&blocked, cid);
1565
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 if (found && (LBM_IS_STATE_UNBLOCKABLE(found->state))) {
1566 3 unlink_ctx_nm(&blocked,found);
1567 3 found->r = unboxed;
1568
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (lbm_is_error(unboxed)) {
1569 ✗ get_stack_ptr(found, 1)[0] = TERMINATE; // replace TOS
1570 ✗ found->app_cont = true;
1571 }
1572 3 found->state = LBM_THREAD_STATE_READY;
1573 3 enqueue_ctx_nm(&queue,found);
1574 3 r = true;
1575 }
1576 3 lbm_mutex_unlock(&qmutex);
1577 3 lbm_mutex_unlock(&blocking_extension_mutex);
1578 3 return r;
1579 }
1580
1581 147 static bool lbm_block_ctx_base(bool timeout, float t_s) {
1582 147 lbm_mutex_lock(&blocking_extension_mutex);
1583 147 blocking_extension = true;
1584
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 147 times.
147 if (timeout) {
1585 ✗ blocking_extension_timeout_us = S_TO_US(t_s);
1586 ✗ blocking_extension_timeout = true;
1587 } else {
1588 147 blocking_extension_timeout = false;
1589 }
1590 147 return true;
1591 }
1592
1593 ✗ void lbm_block_ctx_from_extension_timeout(float s) {
1594 ✗ lbm_block_ctx_base(true, s);
1595 ✗ }
1596
1597 147 void lbm_block_ctx_from_extension(void) {
1598 147 lbm_block_ctx_base(false, 0);
1599 147 }
1600
1601 // todo: May need to pop rmbrs from stack, if present.
1602 // Suspect that the letting the discard cont run is really not a problem.
1603 // Either way will be quite confusing what happens to allocated things when undoing block.
1604 ✗ void lbm_undo_block_ctx_from_extension(void) {
1605 ✗ blocking_extension = false;
1606 ✗ blocking_extension_timeout_us = 0;
1607 ✗ blocking_extension_timeout = false;
1608 ✗ lbm_mutex_unlock(&blocking_extension_mutex);
1609 ✗ }
1610
1611 // TODO: very similar iteration patterns.
1612 // Try to break out common part from free_space and from find_and_send
1613 /** mailbox_free_space_for_cid is used to get the available
1614 * space in a given context's mailbox.
1615 */
1616 4316 static uint32_t lbm_mailbox_free_space_for_cid(lbm_cid cid) {
1617 4316 eval_context_t *found = NULL;
1618 4316 uint32_t res = 0;
1619
1620 4316 lbm_mutex_lock(&qmutex);
1621
1622 4316 found = lookup_ctx_nm(&blocked, cid);
1623
2/2
✓ Branch 0 taken 706 times.
✓ Branch 1 taken 3610 times.
4316 if (!found) {
1624 706 found = lookup_ctx_nm(&queue, cid);
1625 }
1626
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 4316 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
4316 if (!found && ctx_running && ctx_running->id == cid) {
1627 ✗ found = ctx_running;
1628 }
1629
1630
1/2
✓ Branch 0 taken 4316 times.
✗ Branch 1 not taken.
4316 if (found) {
1631 4316 res = found->mailbox_size - found->num_mail;
1632 }
1633
1634 4316 lbm_mutex_unlock(&qmutex);
1635
1636 4316 return res;
1637 }
1638
1639 typedef struct {
1640 bool by_name;
1641 lbm_uint name_len;
1642 union {
1643 char *name;
1644 lbm_cid cid;
1645 } u;
1646 } find_receiver_t;
1647
1648 12709 bool find_receiver_and_send(find_receiver_t c, lbm_value msg) {
1649 12709 lbm_mutex_lock(&qmutex);
1650 12709 eval_context_t *found = NULL;
1651 12709 int res = true;
1652
1653
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 12703 times.
12709 if (c.by_name) {
1654 6 found = lookup_ctx_name_nm(&blocked, c.u.name, c.name_len);
1655 } else {
1656 12703 found = lookup_ctx_nm(&blocked, c.u.cid);
1657 }
1658
2/2
✓ Branch 0 taken 7478 times.
✓ Branch 1 taken 5231 times.
12709 if (found) {
1659
2/2
✓ Branch 0 taken 7457 times.
✓ Branch 1 taken 21 times.
7478 if (LBM_IS_STATE_RECV(found->state)) { // only if unblock receivers here.
1660 7457 unlink_ctx_nm(&blocked,found);
1661 7457 found->state = LBM_THREAD_STATE_READY;
1662 7457 enqueue_ctx_nm(&queue,found);
1663 }
1664 7478 mailbox_add_mail(found, msg);
1665 7478 goto find_receiver_end;
1666 }
1667
1668
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 5225 times.
5231 if (c.by_name) {
1669 6 found = lookup_ctx_name_nm(&queue, c.u.name, c.name_len);
1670 } else {
1671 5225 found = lookup_ctx_nm(&queue, c.u.cid);
1672 }
1673
2/2
✓ Branch 0 taken 1373 times.
✓ Branch 1 taken 3858 times.
5231 if (found) {
1674 1373 mailbox_add_mail(found, msg);
1675 1373 goto find_receiver_end;
1676 }
1677
1678 /* check the current context */
1679
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3855 times.
3858 if (c.by_name) {
1680
2/6
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
3 if (ctx_running && ctx_running->name && strncmp(ctx_running->name, c.u.name, c.name_len) == 0) {
1681 ✗ mailbox_add_mail(ctx_running, msg);
1682 ✗ goto find_receiver_end;
1683 }
1684 } else {
1685
3/4
✓ Branch 0 taken 3855 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3852 times.
✓ Branch 3 taken 3 times.
3855 if (ctx_running && ctx_running->id == c.u.cid) {
1686 3852 mailbox_add_mail(ctx_running, msg);
1687 3852 goto find_receiver_end;
1688 }
1689 }
1690 6 res = false;
1691 12709 find_receiver_end:
1692 12709 lbm_mutex_unlock(&qmutex);
1693 12709 return res;
1694 }
1695 /** find_receiver_and_send is used for message passing where
1696 * the semantics is that the oldest message is dropped if the
1697 * receiver mailbox is full.
1698 */
1699 12703 bool lbm_find_receiver_and_send(lbm_cid cid, lbm_value msg) {
1700 find_receiver_t c;
1701 12703 c.u.cid = cid;
1702 12703 c.by_name = false;
1703 12703 return find_receiver_and_send(c, msg);
1704 }
1705 // a match binder looks like (? x) or (? _) for example.
1706 // It is a list of two elements where the first is a ? and the second is a symbol.
1707 251551 static inline lbm_value get_match_binder_variable(lbm_value exp) {
1708 251551 lbm_value var = ENC_SYM_NIL; // 0 false
1709
2/2
✓ Branch 1 taken 153874 times.
✓ Branch 2 taken 97677 times.
251551 if (lbm_is_cons(exp)) {
1710 153874 lbm_cons_t *e_cell = lbm_ref_cell(exp);
1711 153874 lbm_value bt = e_cell->car;
1712
3/4
✓ Branch 0 taken 43054 times.
✓ Branch 1 taken 110820 times.
✓ Branch 3 taken 43054 times.
✗ Branch 4 not taken.
153874 if (bt == ENC_SYM_MATCH_ANY && lbm_is_cons(e_cell->cdr)) {
1713 43054 var = lbm_ref_cell(e_cell->cdr)->car;
1714 }
1715 }
1716 251551 return var;
1717 }
1718
1719 /* Pattern matching is currently implemented as a recursive
1720 function and make use of stack relative to the size of
1721 expressions that are being matched. */
1722 203133 static bool match(lbm_value p, lbm_value e, lbm_value *env, int rlevel) {
1723
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 203133 times.
203133 if (rlevel >= LBM_MAX_C_RECURSION) return false;
1724 bool r;
1725 203133 match_quickpath:
1726 251551 r = false;
1727 251551 lbm_value var = get_match_binder_variable(p);
1728
2/2
✓ Branch 0 taken 43054 times.
✓ Branch 1 taken 208497 times.
251551 if (var) {
1729 #ifdef LBM_ALWAYS_GC
1730 lbm_gc_mark_phase(*env);
1731 gc();
1732 #endif
1733 43054 lbm_value ls = lbm_heap_allocate_list_init(2, var, ENC_SYM_NIL);
1734
2/2
✓ Branch 1 taken 177 times.
✓ Branch 2 taken 42877 times.
43054 if (!lbm_is_ptr(ls)) {
1735 177 lbm_gc_mark_phase(*env);
1736 177 gc();
1737 177 ls = lbm_heap_allocate_list_init(2, var, ENC_SYM_NIL);
1738
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 177 times.
177 if (!lbm_is_ptr(ls)) {
1739 ✗ ERROR_CTX(ls);
1740 }
1741 }
1742 43054 lbm_value c1 = ls;
1743 43054 lbm_value c2 = lbm_cdr(ls);
1744 43054 lbm_set_cdr(c1, e);
1745 43054 lbm_set_car_and_cdr(c2, c1, *env);
1746 43054 *env = c2;
1747 43054 r = true;
1748
2/2
✓ Branch 1 taken 95517 times.
✓ Branch 2 taken 112980 times.
208497 } else if (lbm_is_symbol(p)) {
1749
2/2
✓ Branch 0 taken 1656 times.
✓ Branch 1 taken 93861 times.
95517 if (p == ENC_SYM_DONTCARE) r = true;
1750 93861 else r = (p == e);
1751
4/4
✓ Branch 1 taken 110820 times.
✓ Branch 2 taken 2160 times.
✓ Branch 4 taken 109308 times.
✓ Branch 5 taken 1512 times.
112980 } else if (lbm_is_cons(p) && lbm_is_cons(e) ) {
1752 109308 lbm_cons_t *p_cell = lbm_ref_cell(p);
1753 109308 lbm_cons_t *e_cell = lbm_ref_cell(e);
1754 109308 lbm_value headp = p_cell->car;
1755 109308 lbm_value tailp = p_cell->cdr;
1756 109308 lbm_value heade = e_cell->car;
1757 109308 lbm_value taile = e_cell->cdr;
1758
2/2
✓ Branch 1 taken 48418 times.
✓ Branch 2 taken 60890 times.
109308 if (match(headp, heade, env, rlevel+1)) {
1759 48418 p = tailp;
1760 48418 e = taile;
1761 48418 goto match_quickpath;
1762 }
1763 60890 r = false;
1764 } else {
1765 // TODO: Think about this.
1766 // may be better to pass rlevel here.
1767 3672 r = struct_eq(p, e, 0);
1768 }
1769 203133 return r;
1770 }
1771
1772 // Find match is not very picky about syntax.
1773 // A completely malformed recv form is most likely to
1774 // just return no_match.
1775 10963 static int find_match(lbm_value plist, lbm_value *earr, lbm_uint num, lbm_value *e, lbm_value *env) {
1776 // A pattern list is a list of pattern, expression lists.
1777 // ( (p1 e1) (p2 e2) ... (pn en))
1778 10963 lbm_value curr_p = plist;
1779 10963 int n = 0;
1780
2/2
✓ Branch 0 taken 11611 times.
✓ Branch 1 taken 108 times.
11719 for (int i = 0; i < (int)num; i ++ ) {
1781 11611 lbm_value curr_e = earr[i];
1782
2/2
✓ Branch 1 taken 24895 times.
✓ Branch 2 taken 756 times.
25651 while (lbm_is_cons(curr_p)) {
1783
1784 24895 lbm_value curr = lbm_ref_cell(curr_p)->car;
1785 lbm_value p0, p1, p2;
1786
1/2
✓ Branch 1 taken 24895 times.
✗ Branch 2 not taken.
24895 EXTRACT(curr, p0);
1787
1/2
✓ Branch 1 taken 24895 times.
✗ Branch 2 not taken.
24895 EXTRACT(curr, p1);
1788
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 24895 times.
24895 EXTRACT_NO_ADVANCE(curr, p2);
1789
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 24895 times.
24895 if (!lbm_is_symbol_nil(p2)) { // A rare syntax check. maybe drop?
1790 ✗ lbm_set_error_reason("Incorrect pattern format for recv");
1791 ✗ ERROR_AT_CTX(ENC_SYM_EERROR,curr);
1792 }
1793
2/2
✓ Branch 1 taken 10855 times.
✓ Branch 2 taken 14040 times.
24895 if (match(p0, curr_e, env, 0)) {
1794 10855 *e = p1;
1795 10855 return n;
1796 }
1797 14040 curr_p = lbm_ref_cell(curr_p)->cdr;
1798 }
1799 756 curr_p = plist; /* search all patterns against next exp */
1800 756 n ++;
1801 }
1802 108 return FM_NO_MATCH;
1803 }
1804
1805 /****************************************************/
1806 /* Garbage collection */
1807
1808 823556 static void mark_context(eval_context_t *ctx, void *arg1, void *arg2) {
1809 (void) arg1;
1810 (void) arg2;
1811 823556 lbm_value roots[4] = {ctx->curr_exp, ctx->program, ctx->r, ctx->reader_stream};
1812 823556 lbm_gc_mark_env(ctx->curr_env);
1813 823556 lbm_gc_mark_roots(roots, 4);
1814 823556 lbm_gc_mark_roots(ctx->mailbox, ctx->num_mail);
1815 823556 lbm_gc_mark_continuation_stack(ctx->K.data, ctx->K.sp);
1816 823556 }
1817
1818 802489 static int gc(void) {
1819
2/2
✓ Branch 0 taken 802449 times.
✓ Branch 1 taken 40 times.
802489 if (ctx_running) {
1820 802449 ctx_running->state = ctx_running->state | LBM_THREAD_STATE_GC_BIT;
1821 }
1822
1823 802489 gc_requested = false;
1824 802489 lbm_gc_state_inc();
1825
1826 // The freelist should generally be NIL when GC runs.
1827 802489 lbm_nil_freelist();
1828 802489 lbm_value *env = lbm_get_global_env();
1829
2/2
✓ Branch 0 taken 25679648 times.
✓ Branch 1 taken 802489 times.
26482137 for (int i = 0; i < GLOBAL_ENV_ROOTS; i ++) {
1830 25679648 lbm_gc_mark_env(env[i]);
1831 }
1832
1833 802489 lbm_mutex_lock(&qmutex); // Lock the queues.
1834 // Any concurrent messing with the queues
1835 // while doing GC cannot possibly be good.
1836 802489 queue_iterator_nm(&queue, mark_context, NULL, NULL);
1837 802489 queue_iterator_nm(&blocked, mark_context, NULL, NULL);
1838
1839
2/2
✓ Branch 0 taken 802449 times.
✓ Branch 1 taken 40 times.
802489 if (ctx_running) {
1840 802449 mark_context(ctx_running, NULL, NULL);
1841 }
1842 802489 lbm_mutex_unlock(&qmutex);
1843
1844 802489 int r = lbm_gc_sweep_phase();
1845 802489 lbm_heap_new_freelist_length();
1846 802489 lbm_memory_update_min_free();
1847
1848
2/2
✓ Branch 0 taken 802449 times.
✓ Branch 1 taken 40 times.
802489 if (ctx_running) {
1849 802449 ctx_running->state = ctx_running->state & ~LBM_THREAD_STATE_GC_BIT;
1850 }
1851 802489 return r;
1852 }
1853
1854 18983 int lbm_perform_gc(void) {
1855 18983 return gc();
1856 }
1857
1858 /****************************************************/
1859 /* Evaluation functions */
1860
1861
1862 /* eval_symbol
1863 * There are multiple possible ways a symbol could be "bound".
1864 * 1. there is a mapping symbol -> value in the global env.
1865 * 2. there is a mapping symbol -> value in the local env (ctx->curr_env).
1866 * 3. there is a dynamically loadable value associated with the symbol.
1867 * 4. The symbol is special or associated with an extension,
1868 * in which case it evaluates to itself.
1869 *
1870 * Syntax: any-symbol
1871 *
1872 * Expects: T(ctx->curr_exp) == symbol
1873 *
1874 * cases: 1,2,4
1875 * Produces:
1876 * ctx->r = value bound to symbol
1877 * ctx->app_cont = true
1878 *
1879 * cases: 3
1880 * Produces:
1881 * sp = sp+3
1882 * s[sp-3] = ctx->curr_exp (the symbol to evaluate)
1883 * s[sp-2] = ctx->curr_env
1884 * s[sp-1] = RESUME
1885 * ctx->curr_exp <- read_and_evaluate(dynamic value)
1886 * ctx->curr_env <- NIL
1887 */
1888 595584982 static void eval_symbol(eval_context_t *ctx) {
1889 595584982 lbm_uint s = lbm_dec_sym(ctx->curr_exp);
1890
2/2
✓ Branch 0 taken 289540053 times.
✓ Branch 1 taken 306044929 times.
595584982 if (s >= RUNTIME_SYMBOLS_START) {
1891 289540053 lbm_value res = ENC_SYM_NIL;
1892
4/4
✓ Branch 1 taken 52247560 times.
✓ Branch 2 taken 237292493 times.
✓ Branch 3 taken 52239818 times.
✓ Branch 4 taken 7742 times.
341787613 if (lbm_env_lookup_b(&res, ctx->curr_exp, ctx->curr_env) ||
1893 52247560 lbm_global_env_lookup(&res, ctx->curr_exp)) {
1894 289532311 ctx->r = res;
1895 289532311 ctx->app_cont = true;
1896 289532311 return;
1897 }
1898 // Dynamic load attempt
1899 // Only symbols of kind RUNTIME can be dynamically loaded.
1900 7742 const char *sym_str = lbm_get_name_by_symbol(s);
1901 7742 const char *code_str = NULL;
1902
2/2
✓ Branch 1 taken 180 times.
✓ Branch 2 taken 7562 times.
7742 if (!dynamic_load_callback(sym_str, &code_str)) {
1903 180 ERROR_AT_CTX(ENC_SYM_NOT_FOUND, ctx->curr_exp);
1904 }
1905 7562 lbm_value *sptr = stack_reserve(ctx, 3);
1906 7562 sptr[0] = ctx->curr_exp;
1907 7562 sptr[1] = ctx->curr_env;
1908 7562 sptr[2] = RESUME;
1909
1910 7562 lbm_value chan = ENC_SYM_NIL;
1911 #ifdef LBM_ALWAYS_GC
1912 gc();
1913 #endif
1914
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7562 times.
7562 if (!create_string_channel((char *)code_str, &chan, ENC_SYM_NIL)) {
1915 ✗ gc();
1916 ✗ if (!create_string_channel((char *)code_str, &chan, ENC_SYM_NIL)) {
1917 ✗ ERROR_CTX(ENC_SYM_MERROR);
1918 }
1919 }
1920
1921 // Here, chan has either been assigned or execution has terminated.
1922
1923 lbm_value loader;
1924
1/4
✗ Branch 2 not taken.
✓ Branch 3 taken 7562 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
7562 WITH_GC_RMBR_1(loader, lbm_heap_allocate_list_init(2,
1925 ENC_SYM_READ,
1926 chan), chan);
1927 lbm_value evaluator;
1928
1/4
✗ Branch 2 not taken.
✓ Branch 3 taken 7562 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
7562 WITH_GC_RMBR_1(evaluator, lbm_heap_allocate_list_init(2,
1929 ENC_SYM_EVAL,
1930 loader), loader);
1931 7562 ctx->curr_exp = evaluator;
1932 7562 ctx->curr_env = ENC_SYM_NIL; // dynamics should be evaluable in empty local env
1933 } else {
1934 //special symbols and extensions can be handled the same way.
1935 306044929 ctx->r = ctx->curr_exp;
1936 306044929 ctx->app_cont = true;
1937 }
1938 }
1939
1940 // (quote e) => e
1941 6451477 static void eval_quote(eval_context_t *ctx) {
1942 6451477 ctx->r = get_cadr(ctx->curr_exp);
1943 6451477 ctx->app_cont = true;
1944 6451477 }
1945
1946 // a => a
1947 329287396 static void eval_selfevaluating(eval_context_t *ctx) {
1948 329287396 ctx->r = ctx->curr_exp;
1949 329287396 ctx->app_cont = true;
1950 329287396 }
1951
1952 // (progn e1 ... en)
1953 25033142 static void eval_progn(eval_context_t *ctx) {
1954 25033142 lbm_value exps = get_cdr(ctx->curr_exp);
1955
1956
2/2
✓ Branch 1 taken 25033106 times.
✓ Branch 2 taken 36 times.
25033142 if (lbm_is_cons(exps)) {
1957 25033106 lbm_cons_t *cell = lbm_ref_cell(exps); // already checked that it's cons.
1958 25033106 ctx->curr_exp = cell->car;
1959
2/2
✓ Branch 1 taken 21420678 times.
✓ Branch 2 taken 3612428 times.
25033106 if (lbm_is_cons(cell->cdr)) { // malformed progn not ending in nil is tolerated
1960 21420678 lbm_uint *sptr = stack_reserve(ctx, 4);
1961 21420678 sptr[0] = ctx->curr_env; // env to restore between expressions in progn
1962 21420678 sptr[1] = lbm_enc_u(0); // Has env been copied (needed for progn local bindings)
1963 21420678 sptr[2] = cell->cdr; // Requirement: sptr[2] is a cons.
1964 21420678 sptr[3] = PROGN_REST;
1965 }
1966
1/2
✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
36 } else if (lbm_is_symbol_nil(exps)) { // Empty progn is nil
1967 36 ctx->r = ENC_SYM_NIL;
1968 36 ctx->app_cont = true;
1969 } else {
1970 ✗ ERROR_CTX(ENC_SYM_EERROR);
1971 }
1972 25033142 }
1973
1974 // (atomic e1 ... en)
1975 327 static void eval_atomic(eval_context_t *ctx) {
1976
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 327 times.
327 if (is_atomic) atomic_error();
1977 327 stack_reserve(ctx, 1)[0] = EXIT_ATOMIC;
1978 327 is_atomic = true;
1979 327 eval_progn(ctx);
1980 327 }
1981
1982 // (call-cc (lambda (k) .... ))
1983 396 static void eval_callcc(eval_context_t *ctx) {
1984 lbm_value cont_array;
1985 396 lbm_uint *sptr0 = stack_reserve(ctx, 1);
1986
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 396 times.
396 sptr0[0] = is_atomic ? ENC_SYM_TRUE : ENC_SYM_NIL;
1987 #ifdef LBM_ALWAYS_GC
1988 gc();
1989 #endif
1990
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 396 times.
396 if (!lbm_heap_allocate_lisp_array(&cont_array, ctx->K.sp)) {
1991 ✗ gc();
1992 ✗ lbm_heap_allocate_lisp_array(&cont_array, ctx->K.sp);
1993 }
1994
1/2
✓ Branch 1 taken 396 times.
✗ Branch 2 not taken.
396 if (lbm_is_ptr(cont_array)) {
1995 396 lbm_array_header_t *arr = assume_array(cont_array);
1996 // sp is small and sp*sizeof(lbm_uint) will now overflow.
1997 396 memcpy(arr->data, ctx->K.data, ctx->K.sp * sizeof(lbm_uint));
1998 // The stored stack contains the is_atomic flag.
1999 // This flag is overwritten in the following execution path.
2000
2001 396 lbm_value acont = cons_with_gc(ENC_SYM_CONT, cont_array, ENC_SYM_NIL);
2002 396 lbm_value arg_list = cons_with_gc(acont, ENC_SYM_NIL, ENC_SYM_NIL);
2003 // Go directly into application evaluation without passing go
2004 396 lbm_uint *sptr = stack_reserve(ctx, 2);
2005 396 sptr0[0] = ctx->curr_env; // overwrites is_atomic in this execution path
2006 396 sptr[0] = arg_list;
2007 396 sptr[1] = APPLICATION_START;
2008 396 ctx->curr_exp = get_cadr(ctx->curr_exp);
2009 } else {
2010 // failed to create continuation array.
2011 ✗ ERROR_CTX(ENC_SYM_MERROR);
2012 }
2013 396 }
2014
2015 // (call-cc-unsafe (lambda (k) ... ))
2016 // cc-unsafe: continuation should not be bound to any global directly or indirectly.
2017 // invoking the continuation must check that target SP holds a continuation that
2018 // can be applied using app_cont, otherwise error. The continuation need not be correct
2019 // in case user globally bound the continuation, but it may rule out disastrous failure.
2020 4495 static void eval_call_cc_unsafe(eval_context_t *ctx) {
2021 4495 lbm_uint sp = ctx->K.sp;
2022 // The stored stack contains the is_atomic flag.
2023 // This flag is overwritten in the following execution path.
2024 lbm_value acont;
2025
2/8
✗ Branch 0 not taken.
✓ Branch 1 taken 4495 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 4495 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
4495 WITH_GC(acont, lbm_heap_allocate_list_init(3,
2026 ENC_SYM_CONT_SP,
2027 lbm_enc_i((int32_t)sp),
2028 is_atomic ? ENC_SYM_TRUE : ENC_SYM_NIL, ENC_SYM_NIL));
2029 4495 lbm_value arg_list = cons_with_gc(acont, ENC_SYM_NIL, ENC_SYM_NIL);
2030 // Go directly into application evaluation without passing go
2031 4495 lbm_uint *sptr = stack_reserve(ctx, 3);
2032 4495 sptr[0] = ctx->curr_env;
2033 4495 sptr[1] = arg_list;
2034 4495 sptr[2] = APPLICATION_START;
2035 4495 ctx->curr_exp = get_cadr(ctx->curr_exp);
2036 4495 }
2037
2038 // (define sym exp)
2039 9195794 static void eval_define(eval_context_t *ctx) {
2040 lbm_value pKEY, pVAL;
2041 9195794 lbm_value curr = ctx->curr_exp;
2042
1/2
✓ Branch 1 taken 9195794 times.
✗ Branch 2 not taken.
9195794 DROP(curr);
2043
2/2
✓ Branch 1 taken 9195788 times.
✓ Branch 2 taken 6 times.
9195794 EXTRACT(curr, pKEY);
2044
2/2
✓ Branch 1 taken 9195785 times.
✓ Branch 2 taken 9 times.
9195794 EXTRACT(curr, pVAL);
2045 9195794 lbm_value rest = curr;
2046 9195794 lbm_uint *sptr = stack_reserve(ctx, 2);
2047
3/4
✓ Branch 1 taken 9195791 times.
✓ Branch 2 taken 3 times.
✓ Branch 4 taken 9195791 times.
✗ Branch 5 not taken.
9195794 if (lbm_is_symbol(pKEY) && lbm_is_symbol_nil(rest)) {
2048 9195791 lbm_uint sym_val = lbm_dec_sym(pKEY);
2049 9195791 sptr[0] = pKEY;
2050
2/2
✓ Branch 0 taken 9195782 times.
✓ Branch 1 taken 9 times.
9195791 if (sym_val >= RUNTIME_SYMBOLS_START) {
2051 9195782 sptr[1] = SET_GLOBAL_ENV;
2052
2/2
✓ Branch 0 taken 248 times.
✓ Branch 1 taken 9195534 times.
9195782 if (ctx->flags & EVAL_CPS_CONTEXT_FLAG_CONST) {
2053 248 stack_reserve(ctx, 1)[0] = MOVE_VAL_TO_FLASH_DISPATCH;
2054 }
2055 9195782 ctx->curr_exp = pVAL;
2056 9195782 return;
2057 } else {
2058 9 lbm_set_error_reason((char*)lbm_error_str_built_in);
2059 }
2060 }
2061 12 ERROR_AT_CTX(ENC_SYM_EERROR, ctx->curr_exp);
2062 }
2063
2064 /* Eval lambda is cheating, a lot! It does this
2065 for performance reasons. The cheats are that
2066 1. When closure is created, a reference to the local env
2067 in which the lambda was evaluated is added to the closure.
2068 Ideally it should have created a list of free variables in the function
2069 and then looked up the values of these creating a new environment.
2070 2. The global env is considered global constant. As there is no copying
2071 of environment bindings into the closure, undefine may break closures.
2072
2073 some obscure programs such as test_setq_local_closure.lisp does not
2074 work properly due to this cheating.
2075 */
2076 // (lambda param-list body-exp) -> (closure param-list body-exp env)
2077 1282743 static void eval_lambda(eval_context_t *ctx) {
2078 #ifdef LBM_ALWAYS_GC
2079 gc();
2080 #endif
2081
1/2
✓ Branch 0 taken 1286361 times.
✗ Branch 1 not taken.
1286361 for (int retry = 0; retry < 2; retry ++) {
2082
2/2
✓ Branch 1 taken 1282743 times.
✓ Branch 2 taken 3618 times.
1286361 if (lbm_heap_num_free() >= 4) {
2083 1282743 lbm_value clo = lbm_heap_state.freelist;
2084 1282743 lbm_value lam = get_cdr(ctx->curr_exp);
2085 1282743 lbm_uint ix = lbm_dec_ptr(clo);
2086 1282743 lbm_cons_t *heap = lbm_heap_state.heap;
2087 1282743 heap[ix].car = ENC_SYM_CLOSURE;
2088 1282743 ix = lbm_dec_ptr(heap[ix].cdr);
2089 1282743 get_car_and_cdr(lam, &heap[ix].car, &lam); // params
2090 1282743 ix = lbm_dec_ptr(heap[ix].cdr);
2091 1282743 get_car_and_cdr(lam, &heap[ix].car, &lam); // body
2092 1282728 ix = lbm_dec_ptr(heap[ix].cdr);
2093 1282728 heap[ix].car = ctx->curr_env;
2094 1282728 lbm_heap_state.freelist = heap[ix].cdr;
2095 1282728 heap[ix].cdr = ENC_SYM_NIL;
2096 1282728 lbm_heap_state.num_free-=4;
2097 1282728 ctx->r = clo;
2098 #ifdef CLEAN_UP_CLOSURES
2099 lbm_uint sym_id = 0;
2100 if (clean_cl_env_symbol) {
2101 lbm_value tail = cons_with_gc(ctx->r, ENC_SYM_NIL, ENC_SYM_NIL);
2102 lbm_value app = cons_with_gc(clean_cl_env_symbol, tail, tail);
2103 ctx->curr_exp = app;
2104 } else if (lbm_get_symbol_by_name("clean-cl-env", &sym_id)) {
2105 clean_cl_env_symbol = lbm_enc_sym(sym_id);
2106 lbm_value tail = cons_with_gc(ctx->r, ENC_SYM_NIL, ENC_SYM_NIL);
2107 lbm_value app = cons_with_gc(clean_cl_env_symbol, tail, tail);
2108 ctx->curr_exp = app;
2109 } else {
2110 ctx->app_cont = true;
2111 }
2112 #else
2113 1282728 ctx->app_cont = true;
2114 #endif
2115 1282728 return;
2116 } else {
2117 3618 gc();
2118 }
2119 }
2120 ✗ ERROR_CTX(ENC_SYM_MERROR);
2121 }
2122
2123 // (if cond-expr then-expr else-expr)
2124 38012234 static void eval_if(eval_context_t *ctx) {
2125 38012234 lbm_value cdr = get_cdr(ctx->curr_exp);
2126 38012234 lbm_value *sptr = stack_reserve(ctx, 3);
2127 38012234 get_car_and_cdr(cdr, &ctx->curr_exp, &sptr[0]);
2128 38012231 sptr[1] = ctx->curr_env;
2129 38012231 sptr[2] = IF;
2130 38012231 }
2131
2132 // (cond (cond-expr-1 expr-1)
2133 // ...
2134 // (cond-expr-N expr-N))
2135 8370 static void eval_cond(eval_context_t *ctx) {
2136 8370 lbm_value curr = ctx->curr_exp;
2137 lbm_value c1;
2138
1/2
✓ Branch 1 taken 8370 times.
✗ Branch 2 not taken.
8370 DROP(curr);
2139
2/2
✓ Branch 1 taken 8325 times.
✓ Branch 2 taken 45 times.
8370 EXTRACT(curr, c1);
2140 8370 lbm_value rest_conds = curr;
2141
2142 // end recursion at (cond )
2143
2/2
✓ Branch 1 taken 48 times.
✓ Branch 2 taken 8322 times.
8370 if (lbm_is_symbol_nil(c1)) {
2144 48 ctx->r = ENC_SYM_NIL;
2145 48 ctx->app_cont = true;
2146 } else {
2147 // Cond is one of the few places where a bit of syntax checking takes place at runtime..
2148 // Maybe dont bother?
2149 8322 lbm_uint len = lbm_list_length(c1);
2150
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8322 times.
8322 if (len != 2) {
2151 ✗ lbm_set_error_reason("Incorrect syntax in cond");
2152 ✗ ERROR_CTX(ENC_SYM_EERROR);
2153 }
2154 8322 lbm_value cond = c1;
2155 lbm_value ce0, ce1;
2156
1/2
✓ Branch 1 taken 8322 times.
✗ Branch 2 not taken.
8322 EXTRACT(cond, ce0);
2157
1/2
✓ Branch 1 taken 8322 times.
✗ Branch 2 not taken.
8322 EXTRACT_NO_ADVANCE(cond, ce1);
2158 lbm_value rest;
2159
3/4
✓ Branch 3 taken 18 times.
✓ Branch 4 taken 8304 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 18 times.
8322 WITH_GC(rest, lbm_heap_allocate_list_init(2,
2160 ce1, // Then branch
2161 cons_with_gc(ENC_SYM_COND, rest_conds , ENC_SYM_NIL)));
2162 8322 lbm_value *sptr = stack_reserve(ctx, 3);
2163 8322 sptr[0] = rest;
2164 8322 sptr[1] = ctx->curr_env;
2165 8322 sptr[2] = IF;
2166 8322 ctx->curr_exp = ce0; //condition;
2167 }
2168 8370 }
2169
2170 15016 static void eval_app_cont(eval_context_t *ctx) {
2171 15016 stack_drop(ctx, 1);
2172 15016 ctx->app_cont = true;
2173 15016 }
2174
2175 // Create a named location in an environment to later receive a value.
2176 // Protects env from GC, other data is the obligation of the caller.
2177 52714504 static void create_binding_location(lbm_value key, lbm_value *env) {
2178
2/2
✓ Branch 1 taken 34713157 times.
✓ Branch 2 taken 18001347 times.
52714504 if (lbm_is_symbol(key)) { // default case
2179
4/4
✓ Branch 0 taken 31113049 times.
✓ Branch 1 taken 3600108 times.
✓ Branch 2 taken 3600144 times.
✓ Branch 3 taken 27512905 times.
34713157 if (key == ENC_SYM_NIL || key == ENC_SYM_DONTCARE) return;
2180 #ifdef LBM_ALWAYS_GC
2181 lbm_gc_mark_phase(*env);
2182 gc();
2183 #endif
2184 27512905 lbm_value ls = lbm_heap_allocate_list_init(2,
2185 key,
2186 ENC_SYM_NIL);
2187
2/2
✓ Branch 1 taken 43419 times.
✓ Branch 2 taken 27469486 times.
27512905 if (!lbm_is_ptr(ls)) {
2188 43419 lbm_gc_mark_phase(*env);
2189 43419 gc();
2190 43419 ls = lbm_heap_allocate_list_init(2,
2191 key,
2192 ENC_SYM_NIL);
2193
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 43419 times.
43419 if (!lbm_is_ptr(ls)) ERROR_CTX(ENC_SYM_MERROR);
2194 }
2195 27512905 lbm_value binding = ls;
2196 27512905 lbm_cons_t *ls_ref = lbm_ref_cell(ls);
2197 27512905 lbm_value new_env = ls_ref->cdr;
2198 27512905 ls_ref->cdr = ENC_SYM_PLACEHOLDER; // known cons
2199 //lbm_set_cdr(binding, ENC_SYM_PLACEHOLDER);
2200 27512905 lbm_cons_t *new_env_ref = lbm_ref_cell(new_env); //known cons
2201 27512905 new_env_ref->car = binding;
2202 27512905 new_env_ref->cdr = *env;
2203 //lbm_set_car_and_cdr(new_env,binding, *env);
2204 27512905 *env = new_env;
2205
2/2
✓ Branch 1 taken 18001335 times.
✓ Branch 2 taken 12 times.
18001347 } else if (lbm_is_cons(key)) { // deconstruct case
2206 18001335 create_binding_location(lbm_ref_cell(key)->car, env);
2207 18001335 create_binding_location(lbm_ref_cell(key)->cdr, env);
2208 } else {
2209 12 ERROR_CTX(ENC_SYM_EERROR);
2210 }
2211 }
2212
2213 15662492 static void let_bind_values_eval(lbm_value binds, lbm_value exp, lbm_value env, eval_context_t *ctx) {
2214
2/2
✓ Branch 1 taken 15662486 times.
✓ Branch 2 taken 6 times.
15662492 if (lbm_is_cons(binds)) {
2215 // Preallocate binding locations.
2216 15662486 lbm_value curr = binds;
2217
2/2
✓ Branch 1 taken 15872910 times.
✓ Branch 2 taken 15662477 times.
31535387 while (lbm_is_cons(curr)) {
2218 15872910 lbm_value new_env_tmp = env;
2219 15872910 lbm_cons_t *cell = lbm_ref_cell(curr); // already checked that cons.
2220 15872910 lbm_value car_curr = cell->car;
2221 15872910 lbm_value cdr_curr = cell->cdr;
2222 15872910 lbm_value key = get_car(car_curr);
2223 15872910 create_binding_location(key, &new_env_tmp);
2224 15872901 env = new_env_tmp;
2225 15872901 curr = cdr_curr;
2226 }
2227
2228 15662477 lbm_cons_t *cell = lbm_ref_cell(binds); // already checked that cons.
2229 15662477 lbm_value car_binds = cell->car;
2230 15662477 lbm_value cdr_binds = cell->cdr;
2231 lbm_value kv0, kv1;
2232 15662477 lbm_value kv = car_binds;
2233
1/2
✓ Branch 1 taken 15662477 times.
✗ Branch 2 not taken.
15662477 EXTRACT(kv, kv0);
2234
1/2
✓ Branch 1 taken 15662477 times.
✗ Branch 2 not taken.
15662477 EXTRACT_NO_ADVANCE(kv, kv1);
2235
2236 15662477 lbm_uint *sptr = stack_reserve(ctx, 5);
2237 15662477 sptr[0] = exp;
2238 15662477 sptr[1] = cdr_binds;
2239 15662477 sptr[2] = env;
2240 15662477 sptr[3] = kv0;
2241 15662477 sptr[4] = BIND_TO_KEY_REST;
2242 15662477 ctx->curr_exp = kv1;
2243 15662477 ctx->curr_env = env;
2244 } else {
2245 6 ctx->curr_exp = exp;
2246 }
2247 15662483 }
2248
2249 // (var x (...)) - local binding inside of an progn
2250 // var has to take, place root-level nesting within progn.
2251 // (progn ... (var a 10) ...) OK!
2252 // (progn ... (something (var a 10)) ... ) NOT OK!
2253 /* progn stack
2254 sp-4 : env
2255 sp-3 : 0
2256 sp-2 : rest
2257 sp-1 : PROGN_REST
2258 */
2259 838930 static void eval_var(eval_context_t *ctx) {
2260
1/2
✓ Branch 0 taken 838930 times.
✗ Branch 1 not taken.
838930 if (ctx->K.sp >= 4) { // Possibly in progn
2261 838930 lbm_value sv = ctx->K.data[ctx->K.sp - 1];
2262
3/4
✓ Branch 0 taken 838930 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 838924 times.
✓ Branch 3 taken 6 times.
838930 if (IS_CONTINUATION(sv) && (sv == PROGN_REST)) {
2263 838924 lbm_uint sp = ctx->K.sp;
2264 838924 lbm_uint is_copied = lbm_dec_u(ctx->K.data[sp-3]);
2265
2/2
✓ Branch 0 taken 820432 times.
✓ Branch 1 taken 18492 times.
838924 if (is_copied == 0) {
2266 lbm_value env;
2267
3/4
✓ Branch 2 taken 822 times.
✓ Branch 3 taken 819610 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 822 times.
820432 WITH_GC(env, lbm_env_copy_spine(ctx->K.data[sp-4]));
2268 820432 ctx->K.data[sp-3] = lbm_enc_u(1);
2269 820432 ctx->K.data[sp-4] = env;
2270 }
2271 838924 lbm_value new_env = ctx->K.data[sp-4];
2272 838924 lbm_value args = get_cdr(ctx->curr_exp);
2273 838924 lbm_value key = get_car(args);
2274 838924 create_binding_location(key, &new_env);
2275
2276 838921 ctx->K.data[sp-4] = new_env;
2277
2278 838921 lbm_value v_exp = get_cadr(args);
2279 838921 lbm_value *sptr = stack_reserve(ctx, 3);
2280 838921 sptr[0] = new_env;
2281 838921 sptr[1] = key;
2282 838921 sptr[2] = PROGN_VAR;
2283 // Activating the new environment before the evaluation of the value to be bound.
2284 // This would normally shadow the existing value, but create_binding_location sets
2285 // the binding to be $placeholder, which is ignored when looking up the value.
2286 // The way closures work, the var-variable needs to be in scope during val
2287 // evaluation for a recursive closure to be possible.
2288 838921 ctx->curr_env = new_env;
2289 838921 ctx->curr_exp = v_exp;
2290 838921 return;
2291 }
2292 }
2293 6 lbm_set_error_reason((char*)lbm_error_str_var_outside_progn);
2294 6 ERROR_CTX(ENC_SYM_EERROR);
2295 }
2296
2297 // (setq x (...)) - same as (set 'x (...)) or (setvar 'x (...))
2298 // does not error when given incorrect number of arguments.
2299 3942932 static void eval_setq(eval_context_t *ctx) {
2300 lbm_value p1, p2;
2301 3942932 lbm_value curr = ctx->curr_exp;
2302
1/2
✓ Branch 1 taken 3942932 times.
✗ Branch 2 not taken.
3942932 DROP(curr);
2303
1/2
✓ Branch 1 taken 3942932 times.
✗ Branch 2 not taken.
3942932 EXTRACT(curr, p1);
2304
1/2
✓ Branch 1 taken 3942932 times.
✗ Branch 2 not taken.
3942932 EXTRACT_NO_ADVANCE(curr, p2);
2305 3942932 lbm_value *sptr = stack_reserve(ctx, 3);
2306 3942932 sptr[0] = ctx->curr_env;
2307 3942932 sptr[1] = p1;
2308 3942932 sptr[2] = SETQ;
2309 3942932 ctx->curr_exp = p2;
2310 3942932 }
2311
2312 468 static void eval_move_to_flash(eval_context_t *ctx) {
2313 468 lbm_value args = get_cdr(ctx->curr_exp);
2314 468 lbm_value *sptr = stack_reserve(ctx,2);
2315 468 sptr[0] = args;
2316 468 sptr[1] = MOVE_TO_FLASH;
2317 468 ctx->app_cont = true;
2318 468 }
2319
2320 // (loop list-of-local-bindings
2321 // condition-exp
2322 // body-exp)
2323 372 static void eval_loop(eval_context_t *ctx) {
2324 372 lbm_value env = ctx->curr_env;
2325 lbm_value p0, p1, p2;
2326 372 lbm_value curr = ctx->curr_exp;
2327
1/2
✓ Branch 1 taken 372 times.
✗ Branch 2 not taken.
372 DROP(curr);
2328
1/2
✓ Branch 1 taken 372 times.
✗ Branch 2 not taken.
372 EXTRACT(curr,p0); // LOOP BINDS
2329
1/2
✓ Branch 1 taken 372 times.
✗ Branch 2 not taken.
372 EXTRACT(curr,p1); // LOOP COND
2330
1/2
✓ Branch 1 taken 372 times.
✗ Branch 2 not taken.
372 EXTRACT_NO_ADVANCE(curr,p2); // LOOP BODY
2331 372 lbm_value *sptr = stack_reserve(ctx, 4);
2332 372 sptr[0] = p2;
2333 372 sptr[1] = p1;
2334 372 sptr[2] = ENC_SYM_NIL;
2335 372 sptr[3] = LOOP_ENV_PREP;
2336 372 let_bind_values_eval(p0, ENC_SYM_NIL, env, ctx);
2337 372 }
2338
2339 /* (trap expression)
2340 *
2341 * suggested use:
2342 * (match (trap expression)
2343 * ((exit-error (? err)) (error-handler err))
2344 * ((exit-ok (? v)) (value-handler v)))
2345 */
2346 12631 static void eval_trap(eval_context_t *ctx) {
2347
2348 12631 lbm_value expr = get_cadr(ctx->curr_exp);
2349 lbm_value retval;
2350
1/4
✗ Branch 2 not taken.
✓ Branch 3 taken 12631 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
12631 WITH_GC(retval, lbm_heap_allocate_list(2));
2351 12631 lbm_ref_cell(retval)->car = ENC_SYM_EXIT_OK;
2352 // lbm_set_car(retval, ENC_SYM_EXIT_OK); // Assume things will go well.
2353 12631 lbm_uint *sptr = stack_reserve(ctx,3);
2354 12631 sptr[0] = retval;
2355 12631 sptr[1] = ctx->flags;
2356 12631 sptr[2] = EXCEPTION_HANDLER;
2357 12631 ctx->flags |= EVAL_CPS_CONTEXT_FLAG_TRAP_UNROLL_RETURN;
2358 12631 ctx->curr_exp = expr;
2359 12631 }
2360
2361 // (let list-of-binding s
2362 // body-exp)
2363 15662120 static void eval_let(eval_context_t *ctx) {
2364 15662120 lbm_value env = ctx->curr_env;
2365 lbm_value p1, p2;
2366 15662120 lbm_value curr = ctx->curr_exp;
2367
1/2
✓ Branch 1 taken 15662120 times.
✗ Branch 2 not taken.
15662120 DROP(curr);
2368
2/2
✓ Branch 1 taken 15662117 times.
✓ Branch 2 taken 3 times.
15662120 EXTRACT(curr, p1);
2369
2/2
✓ Branch 1 taken 15662114 times.
✓ Branch 2 taken 6 times.
15662120 EXTRACT_NO_ADVANCE(curr, p2);
2370 15662120 let_bind_values_eval(p1, p2, env, ctx);
2371 15662111 }
2372
2373 // (and exp0 ... expN)
2374 20605489 static void eval_and(eval_context_t *ctx) {
2375 20605489 lbm_value rest = get_cdr(ctx->curr_exp);
2376
2/2
✓ Branch 1 taken 36 times.
✓ Branch 2 taken 20605453 times.
20605489 if (lbm_is_symbol_nil(rest)) {
2377 36 ctx->app_cont = true;
2378 36 ctx->r = ENC_SYM_TRUE;
2379 } else {
2380 20605453 lbm_value *sptr = stack_reserve(ctx, 3);
2381 20605453 get_car_and_cdr(rest, &ctx->curr_exp, &sptr[1]);
2382 20605450 sptr[0] = ctx->curr_env;
2383 20605450 sptr[2] = AND;
2384 }
2385 20605486 }
2386
2387 // (or exp0 ... expN)
2388 9653 static void eval_or(eval_context_t *ctx) {
2389 9653 lbm_value rest = get_cdr(ctx->curr_exp);
2390
2/2
✓ Branch 1 taken 36 times.
✓ Branch 2 taken 9617 times.
9653 if (lbm_is_symbol_nil(rest)) {
2391 36 ctx->app_cont = true;
2392 36 ctx->r = ENC_SYM_NIL;
2393 } else {
2394 9617 lbm_value *sptr = stack_reserve(ctx, 3);
2395 9617 get_car_and_cdr(rest, &ctx->curr_exp, &sptr[1]);
2396 9614 sptr[0] = ctx->curr_env;
2397 9614 sptr[2] = OR;
2398 }
2399 9650 }
2400
2401 // Pattern matching
2402 // format:
2403 // (match e (pattern body)
2404 // (pattern body)
2405 // ... )
2406 //
2407 // There can be an optional pattern guard:
2408 // (match e (pattern guard body)
2409 // ... )
2410 // a guard is a boolean expression.
2411 // Guards make match, pattern matching more complicated
2412 // than the recv pattern matching and requires staged execution
2413 // via the continuation system rather than a while loop over a list.
2414 18236 static void eval_match(eval_context_t *ctx) {
2415
2416 18236 lbm_value rest = get_cdr(ctx->curr_exp);
2417
2/2
✓ Branch 1 taken 18230 times.
✓ Branch 2 taken 6 times.
18236 if (lbm_is_cons(rest)) {
2418 18230 lbm_cons_t *cell = lbm_ref_cell(rest);
2419 18230 lbm_value cdr_rest = cell->cdr;
2420 18230 ctx->curr_exp = cell->car;
2421 18230 lbm_value *sptr = stack_reserve(ctx, 3);
2422 18230 sptr[0] = cdr_rest;
2423 18230 sptr[1] = ctx->curr_env;
2424 18230 sptr[2] = MATCH;
2425 } else {
2426 // someone wrote the program (match)
2427 6 ERROR_CTX(ENC_SYM_EERROR);
2428 }
2429 18230 }
2430
2431 // Receive-timeout
2432 // (recv-to timeout (pattern expr)
2433 // (pattern expr))
2434 324 static void eval_receive_timeout(eval_context_t *ctx) {
2435
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 324 times.
324 if (is_atomic) atomic_error();
2436 324 lbm_value timeout_val = get_cadr(ctx->curr_exp);
2437 324 lbm_value pats = get_cdr(get_cdr(ctx->curr_exp));
2438
2/2
✓ Branch 1 taken 72 times.
✓ Branch 2 taken 252 times.
324 if (lbm_is_symbol_nil(pats)) {
2439 72 lbm_set_error_reason((char*)lbm_error_str_num_args);
2440 72 ERROR_AT_CTX(ENC_SYM_EERROR, ctx->curr_exp);
2441 } else {
2442 252 lbm_value *sptr = stack_reserve(ctx, 2);
2443 252 sptr[0] = pats;
2444 252 sptr[1] = RECV_TO;
2445 252 ctx->curr_exp = timeout_val;
2446 }
2447 252 }
2448
2449 // Receive
2450 // (recv (pattern expr)
2451 // (pattern expr))
2452 18321 static void eval_receive(eval_context_t *ctx) {
2453
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 18318 times.
18321 if (is_atomic) atomic_error();
2454 18318 lbm_value pats = get_cdr(ctx->curr_exp);
2455
2/2
✓ Branch 0 taken 18282 times.
✓ Branch 1 taken 36 times.
18318 if (pats) { // non-nil check
2456
2/2
✓ Branch 0 taken 7571 times.
✓ Branch 1 taken 10711 times.
18282 if (ctx->num_mail == 0) {
2457 7571 block_current_ctx(LBM_THREAD_STATE_RECV_BL,0,false);
2458 } else {
2459 10711 lbm_value *msgs = ctx->mailbox;
2460 10711 lbm_uint num = ctx->num_mail;
2461
2462 lbm_value e;
2463 10711 lbm_value new_env = ctx->curr_env;
2464 10711 int n = find_match(pats, msgs, num, &e, &new_env);
2465
1/2
✓ Branch 0 taken 10711 times.
✗ Branch 1 not taken.
10711 if (n >= 0 ) { /* Match */
2466 10711 mailbox_remove_mail(ctx, (lbm_uint)n);
2467 10711 ctx->curr_env = new_env;
2468 10711 ctx->curr_exp = e;
2469 } else { /* No match go back to sleep */
2470 ✗ ctx->r = ENC_SYM_NO_MATCH;
2471 ✗ block_current_ctx(LBM_THREAD_STATE_RECV_BL, 0,false);
2472 }
2473 }
2474 } else {
2475 36 lbm_set_error_reason((char*)lbm_error_str_num_args);
2476 36 ERROR_AT_CTX(ENC_SYM_EERROR,ctx->curr_exp);
2477 }
2478 18282 }
2479
2480 /*********************************************************/
2481 /* Continuation functions */
2482
2483 // cont_set_global_env:
2484 //
2485 // s[sp-1] = Key-symbol
2486 //
2487 // ctx->r = Value
2488 9196365 static void cont_set_global_env(eval_context_t *ctx){
2489
2490 9196365 lbm_value val = ctx->r;
2491
2492 9196365 lbm_value key = ctx->K.data[--ctx->K.sp];
2493 9196365 lbm_uint dec_key = lbm_dec_sym(key);
2494 9196365 lbm_uint ix_key = dec_key & GLOBAL_ENV_MASK;
2495 9196365 lbm_value *global_env = lbm_get_global_env();
2496 9196365 lbm_uint orig_env = global_env[ix_key];
2497 lbm_value new_env;
2498 // A key is a symbol and should not need to be remembered.
2499
3/4
✓ Branch 2 taken 4002 times.
✓ Branch 3 taken 9192363 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 4002 times.
9196365 WITH_GC(new_env, lbm_env_set(orig_env,key,val));
2500
2501 9196365 global_env[ix_key] = new_env;
2502 9196365 ctx->r = val;
2503
2504 9196365 ctx->app_cont = true;
2505 9196365 }
2506
2507 // cont_resume:
2508 //
2509 // s[sp-2] = Expression
2510 // s[sp-1] = Environment
2511 //
2512 // ctx->r = Irrelevant.
2513 7556 static void cont_resume(eval_context_t *ctx) {
2514 7556 ctx->curr_env = ctx->K.data[--ctx->K.sp];
2515 7556 ctx->curr_exp = ctx->K.data[--ctx->K.sp];
2516 7556 }
2517
2518 // cont_progn_rest:
2519 //
2520 // s[sp-3] = Environment to evaluate each expression in.
2521 // s[sp-2] = Flag indicating if env has been copied.
2522 // s[sp-1] = list of expressions to evaluate.
2523 //
2524 // ctx->r = Result of last evaluated expression.
2525 25073553 static void cont_progn_rest(eval_context_t *ctx) {
2526 25073553 lbm_value *sptr = get_stack_ptr(ctx, 3);
2527
2528 25073553 lbm_value env = sptr[0];
2529 // eval_progn and cont_progn_rest both ensure that sptr[2] is a list
2530 // whenever cont_progn_rest is called.
2531
2532 25073553 lbm_cons_t *rest_cell = lbm_ref_cell(sptr[2]);
2533 25073553 lbm_value rest_cdr = rest_cell->cdr;
2534 25073553 ctx->curr_exp = rest_cell->car;;
2535 25073553 ctx->curr_env = env;
2536
2/2
✓ Branch 1 taken 3653352 times.
✓ Branch 2 taken 21420201 times.
25073553 if (lbm_is_cons(rest_cdr)) {
2537 3653352 sptr[2] = rest_cdr; // Requirement: rest_cdr is a cons
2538 3653352 stack_reserve(ctx, 1)[0] = PROGN_REST;
2539 } else {
2540 // Nothing is pushed to stack for final element in progn. (tail-call req)
2541 21420201 stack_drop(ctx, 3);
2542 }
2543 25073553 }
2544
2545 // cont_wait
2546 //
2547 // s[sp-1] = cid
2548 102 static void cont_wait(eval_context_t *ctx) {
2549
2550 102 lbm_value cid_val = ctx->K.data[--ctx->K.sp];
2551 102 lbm_cid cid = (lbm_cid)lbm_dec_i(cid_val);
2552
2553 102 bool exists = false;
2554
2555 102 lbm_blocked_iterator(context_exists, &cid, &exists);
2556 102 lbm_running_iterator(context_exists, &cid, &exists);
2557
2558
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 102 times.
102 if (ctx_running->id == cid) {
2559 ✗ exists = true;
2560 }
2561
2562
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 78 times.
102 if (exists) {
2563 24 lbm_value *sptr = stack_reserve(ctx, 2);
2564 24 sptr[0] = lbm_enc_i(cid);
2565 24 sptr[1] = WAIT;
2566 24 ctx->r = ENC_SYM_TRUE;
2567 24 ctx->app_cont = true;
2568 24 yield_ctx(50000);
2569 } else {
2570 78 ctx->r = ENC_SYM_TRUE;
2571 78 ctx->app_cont = true;
2572 }
2573 102 }
2574
2575 /***************************************************/
2576 /* Application helper functions. */
2577
2578
2579 /**
2580 * @brief Setup application of cont object (created by call-cc)
2581 *
2582 * The "function" form, e.g. `(SYM_CONT . cont-array)`, is expected to be stored
2583 * in `ctx->r`.
2584 *
2585 * @param args List of the arguments to apply with.
2586 * @return lbm_value The resulting argument value which should either be
2587 * evaluated or passed on directly depending on how you use this.
2588 */
2589 360 static lbm_value setup_cont(eval_context_t *ctx, lbm_value args) {
2590 /* Continuation created using call-cc.
2591 * ((SYM_CONT . cont-array) arg0 )
2592 */
2593 360 lbm_value c = get_cdr(ctx->r); /* should be the continuation array*/
2594
2595
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 360 times.
360 if (!lbm_is_lisp_array_r(c)) {
2596 ✗ ERROR_CTX(ENC_SYM_FATAL_ERROR);
2597 }
2598
2599 lbm_value arg;
2600 360 lbm_uint arg_count = lbm_list_length(args);
2601
2/3
✓ Branch 0 taken 108 times.
✓ Branch 1 taken 252 times.
✗ Branch 2 not taken.
360 switch (arg_count) {
2602 108 case 0:
2603 108 arg = ENC_SYM_NIL;
2604 108 break;
2605 252 case 1:
2606 252 arg = get_car(args);
2607 252 break;
2608 ✗ default:
2609 ✗ lbm_set_error_reason(lbm_error_str_num_args);
2610 ✗ ERROR_CTX(ENC_SYM_EERROR);
2611 }
2612
2613 360 lbm_stack_clear(&ctx->K);
2614
2615 360 lbm_array_header_t *arr = assume_array(c);
2616 360 ctx->K.sp = arr->size / sizeof(lbm_uint);
2617 360 memcpy(ctx->K.data, arr->data, arr->size);
2618
2619 360 lbm_value atomic = ctx->K.data[--ctx->K.sp];
2620 360 is_atomic = atomic ? 1 : 0;
2621
2622 360 return arg;
2623 }
2624
2625 /**
2626 * @brief Setup application of cont sp object (created by call-cc-unsafe)
2627 *
2628 * The "function" form, e.g. `(SYM_CONT_SP . stack_ptr)` is expected to be
2629 * stored in `ctx->r`.
2630 *
2631 * @param args List of the arguments to apply with.
2632 * @return lbm_value The resulting argument value which should either be
2633 * evaluated or passed on directly depending on how you use this.
2634 */
2635 126 static lbm_value setup_cont_sp(eval_context_t *ctx, lbm_value args) {
2636 // continuation created using call-cc-unsafe
2637 // ((SYM_CONT_SP . stack_ptr) arg0 )
2638 126 lbm_value c = get_cadr(ctx->r); /* should be the stack_ptr*/
2639 126 lbm_value atomic = get_cadr(get_cdr(ctx->r));
2640
2641
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 126 times.
126 if (!lbm_is_number(c)) {
2642 ✗ ERROR_CTX(ENC_SYM_FATAL_ERROR);
2643 }
2644
2645 126 lbm_uint sp = (lbm_uint)lbm_dec_i(c);
2646
2647 lbm_value arg;
2648 126 lbm_uint arg_count = lbm_list_length(args);
2649
2/3
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 90 times.
✗ Branch 2 not taken.
126 switch (arg_count) {
2650 36 case 0:
2651 36 arg = ENC_SYM_NIL;
2652 36 break;
2653 90 case 1:
2654 90 arg = get_car(args);
2655 90 break;
2656 ✗ default:
2657 ✗ lbm_set_error_reason(lbm_error_str_num_args);
2658 ✗ ERROR_CTX(ENC_SYM_EERROR);
2659 }
2660
2661
3/6
✓ Branch 0 taken 126 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 126 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 126 times.
✗ Branch 5 not taken.
126 if (sp > 0 && sp <= ctx->K.sp && IS_CONTINUATION(ctx->K.data[sp-1])) {
2662 126 is_atomic = atomic ? 1 : 0; // works fine with nil/true
2663 126 ctx->K.sp = sp;
2664 126 return arg;
2665 } else {
2666 ✗ ERROR_CTX(ENC_SYM_FATAL_ERROR);
2667 }
2668 }
2669
2670 /**
2671 * @brief Setup application of macro
2672 *
2673 * The macro form, e.g. `(macro (...) ...)`, is expected to be stored in
2674 * `ctx->r`.
2675 *
2676 * @param args List of the arguments to apply the macro with.
2677 * @param curr_env The environment to re-evaluate the result of the macro
2678 * experssion in.
2679 */
2680 static inline __attribute__ ((always_inline)) void setup_macro(eval_context_t *ctx, lbm_value args, lbm_value curr_env) {
2681 /*
2682 * Perform macro expansion.
2683 * Macro expansion is really just evaluation in an
2684 * environment augmented with the unevaluated expressions passed
2685 * as arguments.
2686 */
2687
2688 30755 lbm_uint *sptr = stack_reserve(ctx, 2);
2689 // For EVAL_R, placed here already to protect from GC
2690 30899 sptr[0] = curr_env;
2691 // Placed here only to protect from GC, will be overriden.
2692 30899 sptr[1] = args;
2693
2694 30899 lbm_value curr_param = get_cadr(ctx->r);
2695 30899 lbm_value curr_arg = args;
2696 30899 lbm_value expand_env = curr_env;
2697
6/8
✓ Branch 1 taken 216 times.
✓ Branch 2 taken 144 times.
✓ Branch 3 taken 216 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 77358 times.
✓ Branch 7 taken 30755 times.
✓ Branch 8 taken 77358 times.
✗ Branch 9 not taken.
186047 while (lbm_is_cons(curr_param) &&
2698 77574 lbm_is_cons(curr_arg)) {
2699 77574 lbm_cons_t *param_cell = lbm_ref_cell(curr_param); // already checked that cons.
2700 77574 lbm_cons_t *arg_cell = lbm_ref_cell(curr_arg);
2701 77574 lbm_value car_curr_param = param_cell->car;
2702 77574 lbm_value cdr_curr_param = param_cell->cdr;
2703 77574 lbm_value car_curr_arg = arg_cell->car;
2704 77574 lbm_value cdr_curr_arg = arg_cell->cdr;
2705
2706 77574 lbm_value entry = cons_with_gc(car_curr_param, car_curr_arg, expand_env);
2707 77574 lbm_value aug_env = cons_with_gc(entry, expand_env,ENC_SYM_NIL);
2708 77574 expand_env = aug_env;
2709
2710 77574 curr_param = cdr_curr_param;
2711 77574 curr_arg = cdr_curr_arg;
2712 }
2713 #ifdef LBM_USE_MACRO_REST_ARGS
2714
2/4
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 4 taken 6027 times.
✓ Branch 5 taken 16130 times.
22157 if (lbm_is_cons(curr_arg)) {
2715 6027 expand_env = allocate_binding(ENC_SYM_REST_ARGS, curr_arg, expand_env);
2716 }
2717 #endif
2718
2719 /* Two rounds of evaluation is performed.
2720 * First to instantiate the arguments into the macro body.
2721 * Second to evaluate the resulting program.
2722 */
2723
2724 30899 sptr[1] = EVAL_R;
2725 30899 lbm_value exp = get_cadr(get_cdr(ctx->r));
2726 30899 ctx->curr_exp = exp;
2727 30899 ctx->curr_env = expand_env;
2728 30899 }
2729
2730 3943318 static lbm_value perform_setvar(lbm_value key, lbm_value val, lbm_value env) {
2731
2732 3943318 lbm_uint s = lbm_dec_sym(key);
2733
2/2
✓ Branch 0 taken 3943282 times.
✓ Branch 1 taken 36 times.
3943318 if (s >= RUNTIME_SYMBOLS_START) {
2734 3943282 lbm_value new_env = lbm_env_modify_binding(env, key, val);
2735
3/4
✓ Branch 1 taken 2737219 times.
✓ Branch 2 taken 1206063 times.
✓ Branch 3 taken 2737219 times.
✗ Branch 4 not taken.
3943282 if (lbm_is_symbol(new_env) && new_env == ENC_SYM_NOT_FOUND) {
2736 2737219 lbm_uint ix_key = lbm_dec_sym(key) & GLOBAL_ENV_MASK;
2737 2737219 lbm_value *glob_env = lbm_get_global_env();
2738 2737219 new_env = lbm_env_modify_binding(glob_env[ix_key], key, val);
2739
2/2
✓ Branch 0 taken 2737180 times.
✓ Branch 1 taken 39 times.
2737219 if (new_env != ENC_SYM_NOT_FOUND) {
2740 2737180 glob_env[ix_key] = new_env;
2741 }
2742 }
2743
3/4
✓ Branch 1 taken 39 times.
✓ Branch 2 taken 3943243 times.
✓ Branch 3 taken 39 times.
✗ Branch 4 not taken.
3943282 if (lbm_is_symbol(new_env) && new_env == ENC_SYM_NOT_FOUND) {
2744 39 lbm_set_error_reason((char*)lbm_error_str_variable_not_bound);
2745 39 ERROR_AT_CTX(ENC_SYM_NOT_FOUND, key);
2746 }
2747 3943243 return val;
2748 }
2749 36 ERROR_AT_CTX(ENC_SYM_EERROR, ENC_SYM_SETVAR);
2750 return ENC_SYM_NIL; // unreachable
2751 }
2752
2753 548 static void apply_setvar(lbm_value *args, lbm_uint nargs, eval_context_t *ctx) {
2754
4/4
✓ Branch 0 taken 476 times.
✓ Branch 1 taken 72 times.
✓ Branch 3 taken 404 times.
✓ Branch 4 taken 72 times.
548 if (nargs == 2 && lbm_is_symbol(args[0])) {
2755 lbm_value res;
2756
1/4
✗ Branch 2 not taken.
✓ Branch 3 taken 404 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
404 WITH_GC(res, perform_setvar(args[0], args[1], ctx->curr_env));
2757 404 ctx->r = args[1];
2758 404 stack_drop(ctx, (unsigned int)nargs+1);
2759 404 ctx->app_cont = true;
2760 } else {
2761
2/2
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 72 times.
144 if (nargs == 2) lbm_set_error_reason((char*)lbm_error_str_incorrect_arg);
2762 72 else lbm_set_error_reason((char*)lbm_error_str_num_args);
2763 144 ERROR_AT_CTX(ENC_SYM_EERROR, ENC_SYM_SETVAR);
2764 }
2765 404 }
2766
2767
2768 #define READING_EXPRESSION ((0 << LBM_VAL_SHIFT) | LBM_TYPE_U)
2769 #define READING_PROGRAM ((1 << LBM_VAL_SHIFT) | LBM_TYPE_U)
2770 #define READING_PROGRAM_INCREMENTALLY ((2 << LBM_VAL_SHIFT) | LBM_TYPE_U)
2771
2772 // apply_read_base
2773 // called from apply functions, so the stack holds N arguments.
2774 // Only 1 argument is allowed and that argument must be either a string or a stream.
2775 434249 static void apply_read_base(lbm_value *args, lbm_uint nargs, eval_context_t *ctx, bool program, bool incremental) {
2776
2/2
✓ Branch 0 taken 434213 times.
✓ Branch 1 taken 36 times.
434249 if (nargs == 1) {
2777 434213 lbm_value chan = ENC_SYM_NIL;
2778
2/2
✓ Branch 1 taken 397388 times.
✓ Branch 2 taken 36825 times.
434213 if (lbm_type_of_functional(args[0]) == LBM_TYPE_ARRAY) {
2779 397388 char *str = lbm_dec_str(args[0]);
2780
2/2
✓ Branch 0 taken 397244 times.
✓ Branch 1 taken 144 times.
397388 if (str) {
2781 #ifdef LBM_ALWAYS_GC
2782 gc();
2783 #endif
2784
2/2
✓ Branch 1 taken 6463 times.
✓ Branch 2 taken 390781 times.
397244 if (!create_string_channel(str, &chan, args[0])) {
2785 6463 gc();
2786
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6463 times.
6463 if (!create_string_channel(str, &chan, args[0])) {
2787 ✗ ERROR_CTX(ENC_SYM_MERROR);
2788 }
2789 }
2790 } else {
2791 144 ERROR_CTX(ENC_SYM_EERROR);
2792 }
2793
1/2
✓ Branch 1 taken 36825 times.
✗ Branch 2 not taken.
36825 } else if (lbm_type_of(args[0]) == LBM_TYPE_CHANNEL) {
2794 36825 chan = args[0];
2795 // Streaming transfers can freeze the evaluator if the stream is cut while
2796 // the reader is reading inside of an atomic block.
2797 // It is generally not advisable to read in an atomic block but now it is also
2798 // enforced in the case where it can cause problems.
2799
3/4
✓ Branch 2 taken 14220 times.
✓ Branch 3 taken 22605 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 14220 times.
36825 if (lbm_channel_may_block(lbm_dec_channel(chan)) && is_atomic) {
2800 ✗ lbm_set_error_reason((char*)lbm_error_str_forbidden_in_atomic);
2801 ✗ ERROR_CTX(ENC_SYM_EERROR);
2802 }
2803 } else {
2804 ✗ ERROR_CTX(ENC_SYM_EERROR);
2805 }
2806 // Readers can nest if they are incremental. Nesting can occur
2807 // if the context evaluates code that starts reads additional code.
2808 // Evaluation happens directly after the "read_eval_continue" continuation
2809 // and such a reader will complete its task entirely before we are going back
2810 // to reading in the enclosing reader.
2811 // A program that uses read-eval-program possibly nests incremental readers
2812 // within incremental readers (as deeply as the evaluation stack allows).
2813 // But, inner readers finishes completely before returning back to enclosing readers.
2814
2815 // cont_read_eval_continue pushes the flags and the stream and
2816 // adds a continuation that pops these values after the evaluation steps have taken place.
2817
2818 434069 ctx->reader_stream = chan;
2819 434069 lbm_value *sptr = get_stack_ptr(ctx, 2); // Overwrite the args.
2820
2821 // if reading a single expression, incremental
2822 // or not have no meaning.
2823 434069 sptr[0] = READING_EXPRESSION;
2824
2/2
✓ Branch 0 taken 50395 times.
✓ Branch 1 taken 383674 times.
434069 if (program) {
2825
2/2
✓ Branch 0 taken 35660 times.
✓ Branch 1 taken 14735 times.
50395 if (incremental) {
2826 35660 sptr[0] = READING_PROGRAM_INCREMENTALLY;
2827 } else {
2828 14735 sptr[0] = READING_PROGRAM;
2829 }
2830 }
2831 434069 sptr[1] = READ_DONE;
2832
2833 // Each reader starts in a fresh situation
2834 434069 ctx->flags &= ~EVAL_CPS_CONTEXT_READER_FLAGS_MASK;
2835 434069 ctx->r = ENC_SYM_NIL; // set r to a known state.
2836
2837
2/2
✓ Branch 0 taken 50395 times.
✓ Branch 1 taken 383674 times.
434069 if (program) {
2838
2/2
✓ Branch 0 taken 35660 times.
✓ Branch 1 taken 14735 times.
50395 if (incremental) {
2839 35660 ctx->flags |= EVAL_CPS_CONTEXT_FLAG_INCREMENTAL_READ;
2840 35660 lbm_value *rptr1 = stack_reserve(ctx,2);
2841 // Evaluating the program in the curr_env can lead to
2842 // values on curr_env to be captured in closures created in
2843 // the program.
2844 35660 rptr1[0] = ENC_SYM_NIL; // ctx->curr_env;
2845 35660 rptr1[1] = READ_EVAL_CONTINUE;
2846 } else {
2847 14735 ctx->flags &= ~EVAL_CPS_CONTEXT_FLAG_INCREMENTAL_READ;
2848 14735 lbm_value *rptr1 = stack_reserve(ctx,3);
2849 14735 rptr1[0] = ENC_SYM_NIL;
2850 14735 rptr1[1] = ENC_SYM_NIL;
2851 14735 rptr1[2] = READ_APPEND_CONTINUE;
2852 }
2853 }
2854
2855 // S | reading expression | reading program | reading program incrementally
2856 // ---------------------------------------------------------------------------------
2857 // -5| | READING_PROGRAM |
2858 // -4| | READ_DONE | READING_PROGRAM_INCREMENTALLY
2859 // -3| | NIL | READ_DONE
2860 // -2| READING_EXPRESSION | NIL | curr_env
2861 // -1| READ_DONE | READ_APPEND_CONTINUE | READ_EVAL_CONTINUE
2862
2863 434069 stack_reserve(ctx,1)[0] = READ_NEXT_TOKEN_GRAB_ROW;
2864 434069 ctx->app_cont = true;
2865 } else {
2866 36 lbm_set_error_reason((char*)lbm_error_str_num_args);
2867 36 ERROR_CTX(ENC_SYM_EERROR);
2868 }
2869 434069 }
2870
2871 14843 static void apply_read_program(lbm_value *args, lbm_uint nargs, eval_context_t *ctx) {
2872 14843 apply_read_base(args,nargs,ctx,true,false);
2873 14735 }
2874
2875 35660 static void apply_read_eval_program(lbm_value *args, lbm_uint nargs, eval_context_t *ctx) {
2876 35660 apply_read_base(args,nargs,ctx,true,true);
2877 35660 }
2878
2879 383746 static void apply_read(lbm_value *args, lbm_uint nargs, eval_context_t *ctx) {
2880 383746 apply_read_base(args,nargs,ctx,false,false);
2881 383674 }
2882
2883 1748 static void apply_spawn_base(lbm_value *args, lbm_uint nargs, eval_context_t *ctx, uint32_t context_flags) {
2884
2885 1748 lbm_uint stack_size = EVAL_CPS_DEFAULT_STACK_SIZE;
2886 1748 lbm_uint closure_pos = 0;
2887 1748 char *name = NULL;
2888 // allowed arguments:
2889 // (spawn opt-name opt-stack-size closure arg1 ... argN)
2890
2891
3/4
✓ Branch 0 taken 1748 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1143 times.
✓ Branch 3 taken 605 times.
3496 if (nargs >= 1 &&
2892 1748 lbm_is_closure(args[0])) {
2893 1143 closure_pos = 0;
2894
3/4
✓ Branch 0 taken 605 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 325 times.
✓ Branch 3 taken 280 times.
1210 } else if (nargs >= 2 &&
2895
1/2
✓ Branch 1 taken 325 times.
✗ Branch 2 not taken.
930 lbm_is_number(args[0]) &&
2896 325 lbm_is_closure(args[1])) {
2897 325 stack_size = lbm_dec_as_u32(args[0]);
2898 325 closure_pos = 1;
2899
3/4
✓ Branch 0 taken 280 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 268 times.
✓ Branch 3 taken 12 times.
560 } else if (nargs >= 2 &&
2900
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 264 times.
548 lbm_is_array_r(args[0]) &&
2901 268 lbm_is_closure(args[1])) {
2902 4 name = lbm_dec_str(args[0]);
2903 4 closure_pos = 1;
2904
4/4
✓ Branch 0 taken 270 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 264 times.
✓ Branch 3 taken 6 times.
546 } else if (nargs >= 3 &&
2905
2/2
✓ Branch 1 taken 258 times.
✓ Branch 2 taken 6 times.
534 lbm_is_array_r(args[0]) &&
2906
1/2
✓ Branch 1 taken 258 times.
✗ Branch 2 not taken.
522 lbm_is_number(args[1]) &&
2907 258 lbm_is_closure(args[2])) {
2908 258 stack_size = lbm_dec_as_u32(args[1]);
2909 258 closure_pos = 2;
2910 258 name = lbm_dec_str(args[0]);
2911 } else {
2912
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 9 times.
18 if (context_flags & EVAL_CPS_CONTEXT_FLAG_TRAP)
2913 9 ERROR_AT_CTX(ENC_SYM_TERROR,ENC_SYM_SPAWN_TRAP);
2914 else
2915 9 ERROR_AT_CTX(ENC_SYM_TERROR,ENC_SYM_SPAWN);
2916 }
2917
2918 lbm_value cl0, cl1, cl2;
2919 1730 lbm_value cl = get_cdr(args[closure_pos]);
2920
1/2
✓ Branch 1 taken 1730 times.
✗ Branch 2 not taken.
1730 EXTRACT(cl, cl0); // CLO_PARAMS
2921
1/2
✓ Branch 1 taken 1730 times.
✗ Branch 2 not taken.
1730 EXTRACT(cl, cl1); // CLO_BODY
2922
1/2
✓ Branch 1 taken 1730 times.
✗ Branch 2 not taken.
1730 EXTRACT_NO_ADVANCE(cl, cl2); // CLO_ENV
2923 1730 lbm_value curr_param = cl0;
2924 1730 lbm_value clo_env = cl2;
2925 1730 lbm_uint i = closure_pos + 1;
2926
3/4
✓ Branch 1 taken 1117 times.
✓ Branch 2 taken 1730 times.
✓ Branch 3 taken 1117 times.
✗ Branch 4 not taken.
2847 while (lbm_is_cons(curr_param) && i <= nargs) {
2927 1117 lbm_value entry = cons_with_gc(lbm_ref_cell(curr_param)->car, args[i], clo_env);
2928 1117 lbm_value aug_env = cons_with_gc(entry, clo_env,ENC_SYM_NIL);
2929 1117 clo_env = aug_env;
2930 1117 curr_param = lbm_ref_cell(curr_param)->cdr;
2931 1117 i ++;
2932 }
2933
2934 1730 stack_drop(ctx, (unsigned int)nargs+1);
2935
2936 1730 lbm_value program = cons_with_gc(cl1, ENC_SYM_NIL, clo_env);
2937
2938 1730 lbm_cid cid = lbm_create_ctx_parent(program,
2939 clo_env,
2940 stack_size,
2941 lbm_get_current_cid(),
2942 context_flags,
2943 name);
2944 // setting these values is a good idea
2945 // even if creating a context failed.
2946 // The info in r may be useful for user.
2947 1730 ctx->r = lbm_enc_i(cid);
2948 1730 ctx->app_cont = true;
2949
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 1694 times.
1730 if (cid == -1) ERROR_CTX(ENC_SYM_MERROR); // Kill parent and signal out of memory.
2950 1694 }
2951
2952 1283 static void apply_spawn(lbm_value *args, lbm_uint nargs, eval_context_t *ctx) {
2953 1283 apply_spawn_base(args,nargs,ctx, EVAL_CPS_CONTEXT_FLAG_NOTHING);
2954 1238 }
2955
2956 465 static void apply_spawn_trap(lbm_value *args, lbm_uint nargs, eval_context_t *ctx) {
2957 465 apply_spawn_base(args,nargs,ctx, EVAL_CPS_CONTEXT_FLAG_TRAP);
2958 456 }
2959
2960 36683 static void apply_yield(lbm_value *args, lbm_uint nargs, eval_context_t *ctx) {
2961
4/4
✓ Branch 0 taken 36677 times.
✓ Branch 1 taken 6 times.
✓ Branch 3 taken 36674 times.
✓ Branch 4 taken 3 times.
73357 if (nargs == 1 && lbm_is_number(args[0])) {
2962 36674 lbm_uint ts = lbm_dec_as_u32(args[0]);
2963 36674 stack_drop(ctx, (unsigned int)nargs+1);
2964 36674 yield_ctx(ts);
2965 } else {
2966 9 lbm_set_error_reason((char*)lbm_error_str_no_number);
2967 9 ERROR_AT_CTX(ENC_SYM_TERROR, ENC_SYM_YIELD);
2968 }
2969 36674 }
2970
2971 6882 static void apply_sleep(lbm_value *args, lbm_uint nargs, eval_context_t *ctx) {
2972
4/4
✓ Branch 0 taken 6876 times.
✓ Branch 1 taken 6 times.
✓ Branch 3 taken 6873 times.
✓ Branch 4 taken 3 times.
13719 if (nargs == 1 && lbm_is_number(args[0])) {
2973 6873 lbm_uint ts = (lbm_uint)(1000000.0f * lbm_dec_as_float(args[0]));
2974 6873 stack_drop(ctx, (unsigned int)nargs+1);
2975 6873 yield_ctx(ts);
2976 } else {
2977 9 lbm_set_error_reason((char*)lbm_error_str_no_number);
2978 9 ERROR_AT_CTX(ENC_SYM_TERROR, ENC_SYM_SLEEP);
2979 }
2980 6837 }
2981
2982 84 static void apply_wait(lbm_value *args, lbm_uint nargs, eval_context_t *ctx) {
2983
4/4
✓ Branch 0 taken 81 times.
✓ Branch 1 taken 3 times.
✓ Branch 3 taken 78 times.
✓ Branch 4 taken 3 times.
162 if (nargs == 1 && lbm_type_of(args[0]) == LBM_TYPE_I) {
2984 78 lbm_cid cid = (lbm_cid)lbm_dec_i(args[0]);
2985 78 lbm_value *sptr = get_stack_ptr(ctx, 2);
2986 78 sptr[0] = lbm_enc_i(cid);
2987 78 sptr[1] = WAIT;
2988 78 ctx->r = ENC_SYM_TRUE;
2989 78 ctx->app_cont = true;
2990 78 yield_ctx(50000);
2991 } else {
2992 6 ERROR_AT_CTX(ENC_SYM_TERROR, ENC_SYM_WAIT);
2993 }
2994 78 }
2995
2996 /* (eval expr)
2997 (eval env expr) */
2998 491261 static void apply_eval(lbm_value *args, lbm_uint nargs, eval_context_t *ctx) {
2999
2/2
✓ Branch 0 taken 491255 times.
✓ Branch 1 taken 6 times.
491261 if ( nargs == 1) {
3000 491255 ctx->curr_exp = args[0];
3001
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 } else if (nargs == 2) {
3002 3 ctx->curr_exp = args[1];
3003 3 ctx->curr_env = args[0];
3004 } else {
3005 3 lbm_set_error_reason((char*)lbm_error_str_num_args);
3006 3 ERROR_AT_CTX(ENC_SYM_EERROR, ENC_SYM_EVAL);
3007 }
3008 491258 stack_drop(ctx, (unsigned int)nargs+1);
3009 491258 }
3010
3011 15037 static void apply_eval_program(lbm_value *args, lbm_uint nargs, eval_context_t *ctx) {
3012
2/2
✓ Branch 0 taken 15028 times.
✓ Branch 1 taken 9 times.
15037 if (nargs == 1) {
3013 // here ctx->r = args[0];
3014 15028 lbm_value prg = args[0]; // No check that this is a program.
3015 lbm_value app_cont;
3016 lbm_value app_cont_prg;
3017 lbm_value new_prg;
3018 lbm_value prg_copy;
3019
3020 15028 int len = -1;
3021
3/4
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 15022 times.
✓ Branch 7 taken 6 times.
✗ Branch 8 not taken.
15028 WITH_GC(prg_copy, lbm_list_copy(&len, prg, ENC_SYM_NIL));
3022 15022 stack_drop(ctx, (unsigned int)nargs+1);
3023 // There is always a continuation (DONE).
3024 // If ctx->program is nil, the stack should contain DONE.
3025 // after adding an intermediate done for prg, stack becomes DONE, DONE.
3026 15022 app_cont = cons_with_gc(ENC_SYM_APP_CONT, ENC_SYM_NIL, prg_copy);
3027 15022 app_cont_prg = cons_with_gc(app_cont, ENC_SYM_NIL, prg_copy);
3028 15022 new_prg = lbm_list_append(app_cont_prg, ctx->program);
3029 15022 new_prg = lbm_list_append(prg_copy, new_prg);
3030 // new_prg is guaranteed to be a cons cell or nil
3031 // even if the eval-program application is syntactically broken.
3032 15022 stack_reserve(ctx, 1)[0] = DONE;
3033 15022 ctx->program = get_cdr(new_prg);
3034 15022 ctx->curr_exp = get_car(new_prg);
3035 } else {
3036 9 lbm_set_error_reason((char*)lbm_error_str_num_args);
3037 9 ERROR_AT_CTX(ENC_SYM_EERROR, ENC_SYM_EVAL_PROGRAM);
3038 }
3039 15022 }
3040
3041 7950 static void apply_send(lbm_value *args, lbm_uint nargs, eval_context_t *ctx) {
3042
2/2
✓ Branch 0 taken 7947 times.
✓ Branch 1 taken 3 times.
7950 if (nargs == 2) {
3043 7947 lbm_value msg = args[1];
3044
2/2
✓ Branch 1 taken 7935 times.
✓ Branch 2 taken 12 times.
7947 if (lbm_type_of(args[0]) == LBM_TYPE_I) {
3045 7935 lbm_cid cid = (lbm_cid)lbm_dec_i(args[0]);
3046 7935 bool r = lbm_find_receiver_and_send(cid, msg);
3047 /* return the status */
3048 7935 stack_drop(ctx, (unsigned int)nargs+1);
3049
2/2
✓ Branch 0 taken 7932 times.
✓ Branch 1 taken 3 times.
7935 ctx->r = r ? ENC_SYM_TRUE : ENC_SYM_NIL;
3050 7935 ctx->app_cont = true;
3051
2/2
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 3 times.
12 } else if (lbm_type_of_functional(args[0]) == LBM_TYPE_ARRAY) {
3052 char *name;
3053 size_t name_len;
3054
3/4
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 6 times.
✗ Branch 4 not taken.
15 if (lbm_value_is_printable_string(args[0], &name) &&
3055 6 lbm_dec_str_size(args[0], &name, &name_len)) {
3056 find_receiver_t c;
3057 6 c.u.name = name;
3058 6 c.name_len = (lbm_uint)name_len;
3059 6 c.by_name = true;
3060 6 bool r = find_receiver_and_send(c, msg);
3061 6 stack_drop(ctx, (unsigned int)nargs + 1);
3062
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 ctx->r = r ? ENC_SYM_TRUE : ENC_SYM_NIL;
3063 6 ctx->app_cont = true;
3064 } else {
3065 3 ERROR_AT_CTX(ENC_SYM_TERROR, ENC_SYM_SEND);
3066 }
3067 } else {
3068 3 ERROR_AT_CTX(ENC_SYM_TERROR, ENC_SYM_SEND);
3069 }
3070 } else {
3071 3 lbm_set_error_reason((char*)lbm_error_str_num_args);
3072 3 ERROR_AT_CTX(ENC_SYM_EERROR, ENC_SYM_SEND);
3073 }
3074 7941 }
3075
3076 6 static void apply_ok(lbm_value *args, lbm_uint nargs, eval_context_t *ctx) {
3077 6 lbm_value ok_val = ENC_SYM_TRUE;
3078
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 if (nargs >= 1) {
3079 3 ok_val = args[0];
3080 }
3081 6 ctx->r = ok_val;
3082 6 ok_ctx();
3083 6 }
3084
3085 48 static void apply_error(lbm_value *args, lbm_uint nargs, eval_context_t *ctx) {
3086 (void) ctx;
3087 48 lbm_value err_val = ENC_SYM_EERROR;
3088
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 3 times.
48 if (nargs >= 1) {
3089 45 err_val = args[0];
3090 }
3091 48 ERROR_AT_CTX(err_val, ENC_SYM_EXIT_ERROR);
3092 }
3093
3094 // ////////////////////////////////////////////////////////////
3095 // Map takes a function f and a list ls as arguments.
3096 // The function f is applied to each element of ls.
3097 //
3098 // Normally when applying a function to an argument this happens:
3099 // 1. the function is evaluated
3100 // 2. the argument is evaluated
3101 // 3. the result of evaluating the function is applied to the result of evaluating
3102 // the argument.
3103 //
3104 // When doing (map f arg-list) I assume one means to apply f to each element of arg-list
3105 // exactly as those elements are. That is, no evaluation of the argument.
3106 // The implementation of map below makes sure that the elements of the arg-list are not
3107 // evaluated by wrapping them each in a `quote`.
3108 //
3109 // Map creates a structure in memory that looks like this (f (quote dummy . nil) . nil).
3110 // Then, for each element from arg-list (example a1 ... aN) the object
3111 // (f (quote aM . nil) . nil) is created by substituting dummy for an element of the list.
3112 // after this substitution the evaluator is fired up to evaluate the entire (f (quote aM . nil) . nil)
3113 // structure resulting in an element for the result list.
3114 //
3115 // Here comes the fun part, if you (map quote arg-list), then the object
3116 // (quote (quote aM . nil) . nil) is created and evaluated. Now note that quote just gives back
3117 // exactly what you give to it when evaluated.
3118 // So (quote (quote aM . nil) . nil) gives you as result (quote aM . nil) and now also note that
3119 // this is a list, and a list is really just an address on the heap!
3120 // This leads to the very fun behavior that:
3121 //
3122 // # (map quote '(1 2 3 4))
3123 // > ((quote 4) (quote 4) (quote 4) (quote 4))
3124 //
3125 // A potential fix is to instead of creating the object (f (quote aM . nil) . nil)
3126 // we create the object (f var) for some unique var and then extend the environment
3127 // for each round of evaluation with a binding var => aM.
3128
3129 // (map f arg-list)
3130 1233 static void apply_map(lbm_value *args, lbm_uint nargs, eval_context_t *ctx) {
3131
4/4
✓ Branch 0 taken 1230 times.
✓ Branch 1 taken 3 times.
✓ Branch 3 taken 1080 times.
✓ Branch 4 taken 150 times.
1233 if (nargs == 2 && lbm_is_cons(args[1])) {
3132 1080 lbm_value *sptr = get_stack_ptr(ctx, 3);
3133
3134 1080 lbm_value f = args[0];
3135 1080 lbm_cons_t *args1_cell = lbm_ref_cell(args[1]);
3136 1080 lbm_value h = args1_cell->car;
3137 1080 lbm_value t = args1_cell->cdr;
3138
3139 lbm_value appli_1;
3140 lbm_value appli;
3141
1/4
✗ Branch 2 not taken.
✓ Branch 3 taken 1080 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
1080 WITH_GC(appli_1, lbm_heap_allocate_list(2));
3142
1/4
✗ Branch 2 not taken.
✓ Branch 3 taken 1080 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
1080 WITH_GC_RMBR_1(appli, lbm_heap_allocate_list(2), appli_1);
3143
3144 // appli_1 is a list of length 2 here.
3145 1080 lbm_value appli_0 = lbm_ref_cell(appli_1)->cdr;
3146
3147 // appli is a list of length 2 here, so a cons
3148 1080 lbm_cons_t *cell = lbm_ref_cell(appli_0);
3149 1080 cell->car = h;
3150 1080 cell->cdr = ENC_SYM_NIL;
3151
3152 1080 cell = lbm_ref_cell(appli_1);
3153 1080 cell->car = ENC_SYM_QUOTE;
3154
3155 1080 lbm_cons_t *appli_cell = lbm_ref_cell(appli);
3156 1080 cell = lbm_ref_cell(appli_cell->cdr);
3157 1080 cell->car = appli_1;
3158 1080 cell->cdr = ENC_SYM_NIL;
3159
3160 1080 appli_cell->car = f;
3161
3162
3163 1080 lbm_value elt = cons_with_gc(ctx->r, ENC_SYM_NIL, appli);
3164 1080 sptr[0] = t; // reuse stack space
3165 1080 sptr[1] = ctx->curr_env;
3166 1080 sptr[2] = elt;
3167 1080 lbm_value *rptr = stack_reserve(ctx,4);
3168 1080 rptr[0] = elt;
3169 1080 rptr[1] = appli;
3170 1080 rptr[2] = appli_0;
3171 1080 rptr[3] = MAP;
3172 1080 ctx->curr_exp = appli;
3173
4/4
✓ Branch 0 taken 150 times.
✓ Branch 1 taken 3 times.
✓ Branch 3 taken 144 times.
✓ Branch 4 taken 6 times.
153 } else if (nargs == 2 && lbm_is_symbol_nil(args[1])) {
3174 144 stack_drop(ctx, 3);
3175 144 ctx->r = ENC_SYM_NIL;
3176 144 ctx->app_cont = true;
3177 } else {
3178 9 lbm_set_error_reason((char*)lbm_error_str_num_args);
3179 9 ERROR_AT_CTX(ENC_SYM_EERROR, ENC_SYM_MAP);
3180 }
3181 1224 }
3182
3183 207 static void apply_reverse(lbm_value *args, lbm_uint nargs, eval_context_t *ctx) {
3184
4/4
✓ Branch 0 taken 204 times.
✓ Branch 1 taken 3 times.
✓ Branch 3 taken 195 times.
✓ Branch 4 taken 9 times.
207 if (nargs == 1 && lbm_is_list(args[0])) {
3185 195 lbm_value curr = args[0];
3186
3187 195 lbm_value new_list = ENC_SYM_NIL;
3188
2/2
✓ Branch 1 taken 4146 times.
✓ Branch 2 taken 195 times.
4341 while (lbm_is_cons(curr)) {
3189 4146 lbm_cons_t *curr_cell = lbm_ref_cell(curr); // known cons.
3190 4146 lbm_value tmp = cons_with_gc(curr_cell->car, new_list, ENC_SYM_NIL);
3191 4146 new_list = tmp;
3192 4146 curr = curr_cell->cdr;
3193 }
3194 195 stack_drop(ctx, 2);
3195 195 ctx->r = new_list;
3196 195 ctx->app_cont = true;
3197 } else {
3198 12 lbm_set_error_reason("Reverse requires a list argument");
3199 12 ERROR_AT_CTX(ENC_SYM_EERROR, ENC_SYM_REVERSE);
3200 }
3201 195 }
3202
3203 44550 static void apply_flatten(lbm_value *args, lbm_uint nargs, eval_context_t *ctx) {
3204
2/2
✓ Branch 0 taken 44514 times.
✓ Branch 1 taken 36 times.
44550 if (nargs == 1) {
3205 #ifdef LBM_ALWAYS_GC
3206 gc();
3207 #endif
3208 44514 lbm_value v = flatten_value(args[0]);
3209
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 44497 times.
44514 if ( v == ENC_SYM_MERROR) {
3210 17 gc();
3211 17 v = flatten_value(args[0]);
3212 }
3213
3214
2/2
✓ Branch 1 taken 36 times.
✓ Branch 2 taken 44478 times.
44514 if (lbm_is_symbol(v)) {
3215 36 ERROR_AT_CTX(v, ENC_SYM_FLATTEN);
3216 } else {
3217 44478 stack_drop(ctx, 2);
3218 44478 ctx->r = v;
3219 44478 ctx->app_cont = true;
3220 }
3221 44478 return;
3222 }
3223 36 ERROR_AT_CTX(ENC_SYM_TERROR, ENC_SYM_FLATTEN);
3224 }
3225
3226 44468 static void apply_unflatten(lbm_value *args, lbm_uint nargs, eval_context_t *ctx) {
3227 lbm_array_header_t *array;
3228
4/4
✓ Branch 0 taken 44462 times.
✓ Branch 1 taken 6 times.
✓ Branch 3 taken 44456 times.
✓ Branch 4 taken 6 times.
44468 if(nargs == 1 && (array = lbm_dec_array_r(args[0]))) {
3229 lbm_flat_value_t fv;
3230 44456 fv.buf = (uint8_t*)array->data;
3231 44456 fv.buf_size = array->size;
3232 44456 fv.buf_pos = 0;
3233
3234 lbm_value res;
3235
3236 44456 ctx->r = ENC_SYM_NIL;
3237
2/2
✓ Branch 1 taken 44438 times.
✓ Branch 2 taken 18 times.
44456 if (lbm_unflatten_value(&fv, &res)) {
3238 44438 ctx->r = res;
3239 }
3240 44456 stack_drop(ctx, 2);
3241 44456 ctx->app_cont = true;
3242 44456 return;
3243 }
3244 12 ERROR_AT_CTX(ENC_SYM_TERROR, ENC_SYM_UNFLATTEN);
3245 }
3246
3247 153 static void apply_kill(lbm_value *args, lbm_uint nargs, eval_context_t *ctx) {
3248
3/4
✓ Branch 0 taken 147 times.
✓ Branch 1 taken 6 times.
✓ Branch 3 taken 147 times.
✗ Branch 4 not taken.
153 if (nargs == 2 && lbm_is_number(args[0])) {
3249 147 lbm_cid cid = lbm_dec_as_i32(args[0]);
3250
3251
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 147 times.
147 if (ctx->id == cid) {
3252 ✗ ctx->r = args[1];
3253 ✗ finish_ctx();
3254 ✗ return;
3255 }
3256 147 lbm_mutex_lock(&qmutex);
3257 147 eval_context_t *found = NULL;
3258
2/2
✓ Branch 1 taken 36 times.
✓ Branch 2 taken 111 times.
147 if ((found = lookup_ctx_nm(&blocked, cid))) {
3259 36 unlink_ctx_nm(&blocked, found);
3260
2/2
✓ Branch 1 taken 108 times.
✓ Branch 2 taken 3 times.
111 } else if ((found = lookup_ctx_nm(&queue, cid))) {
3261 108 unlink_ctx_nm(&queue, found);
3262 }
3263
3264
2/2
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 3 times.
147 if (found) {
3265 144 found->K.data[found->K.sp - 1] = KILL;
3266 144 found->r = args[1];
3267 144 found->app_cont = true;
3268 144 found->state = LBM_THREAD_STATE_READY;
3269 144 enqueue_ctx_nm(&queue,found);
3270 144 ctx->r = ENC_SYM_TRUE;
3271 } else {
3272 3 ctx->r = ENC_SYM_NIL;
3273 }
3274 147 stack_drop(ctx, 3);
3275 147 ctx->app_cont = true;
3276 147 lbm_mutex_unlock(&qmutex);
3277 147 return;
3278 }
3279 6 ERROR_AT_CTX(ENC_SYM_TERROR, ENC_SYM_KILL);
3280 }
3281
3282 363702 static lbm_value cmp_to_clo(lbm_value cmp) {
3283 lbm_value closure;
3284
3/4
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 363690 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 12 times.
363702 WITH_GC(closure, lbm_heap_allocate_list(4));
3285 363702 lbm_set_car(closure, ENC_SYM_CLOSURE);
3286 363702 lbm_value cl1 = lbm_cdr(closure);
3287 lbm_value par;
3288
1/4
✗ Branch 2 not taken.
✓ Branch 3 taken 363702 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
363702 WITH_GC_RMBR_1(par, lbm_heap_allocate_list_init(2, symbol_x, symbol_y), closure);
3289 363702 lbm_set_car(cl1, par);
3290 363702 lbm_value cl2 = lbm_cdr(cl1);
3291 lbm_value body;
3292
3/4
✓ Branch 2 taken 7488 times.
✓ Branch 3 taken 356214 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 7488 times.
363702 WITH_GC_RMBR_1(body, lbm_heap_allocate_list_init(3, cmp, symbol_x, symbol_y), closure);
3293 363702 lbm_set_car(cl2, body);
3294 363702 lbm_value cl3 = lbm_cdr(cl2);
3295 363702 lbm_set_car(cl3, ENC_SYM_NIL);
3296 363702 return closure;
3297 }
3298
3299 // (merge comparator list1 list2)
3300 618 static void apply_merge(lbm_value *args, lbm_uint nargs, eval_context_t *ctx) {
3301
6/6
✓ Branch 0 taken 606 times.
✓ Branch 1 taken 12 times.
✓ Branch 3 taken 588 times.
✓ Branch 4 taken 18 times.
✓ Branch 6 taken 573 times.
✓ Branch 7 taken 15 times.
618 if (nargs == 3 && lbm_is_list(args[1]) && lbm_is_list(args[2])) {
3302
3303
2/2
✓ Branch 1 taken 63 times.
✓ Branch 2 taken 510 times.
573 if (!lbm_is_closure(args[0])) {
3304 63 args[0] = cmp_to_clo(args[0]);
3305 }
3306
3307 // Copy input lists for functional behaviour at top-level
3308 // merge itself is in-place in the copied lists.
3309 lbm_value a;
3310 lbm_value b;
3311 573 int len_a = -1;
3312 573 int len_b = -1;
3313
1/4
✗ Branch 2 not taken.
✓ Branch 3 taken 573 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
573 WITH_GC(a, lbm_list_copy(&len_a, args[1], ENC_SYM_NIL));
3314
1/4
✗ Branch 2 not taken.
✓ Branch 3 taken 573 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
573 WITH_GC_RMBR_1(b, lbm_list_copy(&len_b, args[2], ENC_SYM_NIL), a);
3315
3316
2/2
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 495 times.
573 if (len_a == 0) {
3317 78 ctx->r = b;
3318 78 stack_drop(ctx, 4);
3319 78 ctx->app_cont = true;
3320 573 return;
3321 }
3322
2/2
✓ Branch 0 taken 75 times.
✓ Branch 1 taken 420 times.
495 if (len_b == 0) {
3323 75 ctx->r = a;
3324 75 stack_drop(ctx, 4);
3325 75 ctx->app_cont = true;
3326 75 return;
3327 }
3328
3329 420 args[1] = a; // keep safe by replacing the original on stack.
3330 420 args[2] = b;
3331
3332 420 lbm_value cl = lbm_cdr(args[0]);
3333 lbm_value cl0, cl1, cl2;
3334
1/2
✓ Branch 1 taken 420 times.
✗ Branch 2 not taken.
420 EXTRACT(cl, cl0); // CLO_PARAMS
3335
1/2
✓ Branch 1 taken 420 times.
✗ Branch 2 not taken.
420 EXTRACT(cl, cl1); // CLO_BODY
3336
1/2
✓ Branch 1 taken 420 times.
✗ Branch 2 not taken.
420 EXTRACT_NO_ADVANCE(cl, cl2); // CLO_ENV
3337
3338 420 lbm_value cmp_env = cl2;
3339 420 lbm_uint len = lbm_list_length(cl0);
3340
1/2
✓ Branch 0 taken 420 times.
✗ Branch 1 not taken.
420 if (len == 2) {
3341 420 lbm_value a_1 = a;
3342 420 lbm_value b_1 = b;
3343 420 lbm_value a_rest = lbm_cdr(a);
3344 420 lbm_value b_rest = lbm_cdr(b);
3345 420 lbm_value par1 = get_car(cl0);
3346 420 lbm_value par2 = get_cadr(cl0);
3347 lbm_value new_env0;
3348 lbm_value new_env;
3349
1/4
✗ Branch 3 not taken.
✓ Branch 4 taken 420 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
420 WITH_GC(new_env0, lbm_env_set(cmp_env, par1, lbm_car(a_1)));
3350
1/4
✗ Branch 3 not taken.
✓ Branch 4 taken 420 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
420 WITH_GC_RMBR_1(new_env, lbm_env_set(new_env0, par2, lbm_car(b_1)),new_env0);
3351 420 cmp_env = new_env;
3352 420 lbm_set_cdr(a_1, b_1);
3353 420 lbm_set_cdr(b_1, ENC_SYM_NIL);
3354 420 lbm_value cmp = cl1;
3355
3356 420 stack_drop(ctx, 4); // TODO: Optimize drop 4 alloc 10 into alloc 6
3357 420 lbm_uint *sptr = stack_reserve(ctx, 10);
3358 420 sptr[0] = ENC_SYM_NIL; // head of merged list
3359 420 sptr[1] = ENC_SYM_NIL; // last of merged list
3360 420 sptr[2] = a_1;
3361 420 sptr[3] = a_rest;
3362 420 sptr[4] = b_rest;
3363 420 sptr[5] = cmp;
3364 420 sptr[6] = cmp_env;
3365 420 sptr[7] = par1;
3366 420 sptr[8] = par2;
3367 420 sptr[9] = MERGE_REST;
3368 420 ctx->curr_exp = cl1;
3369 420 ctx->curr_env = cmp_env;
3370 420 return;
3371 }
3372 }
3373 45 ERROR_AT_CTX(ENC_SYM_TERROR, ENC_SYM_MERGE);
3374 }
3375
3376 // (sort comparator list)
3377 364104 static void apply_sort(lbm_value *args, lbm_uint nargs, eval_context_t *ctx) {
3378
4/4
✓ Branch 0 taken 364095 times.
✓ Branch 1 taken 9 times.
✓ Branch 3 taken 364077 times.
✓ Branch 4 taken 18 times.
364104 if (nargs == 2 && lbm_is_list(args[1])) {
3379
3380
2/2
✓ Branch 1 taken 363639 times.
✓ Branch 2 taken 438 times.
364077 if (!lbm_is_closure(args[0])) {
3381 363639 args[0] = cmp_to_clo(args[0]);
3382 }
3383
3384 364077 int len = -1;
3385 lbm_value list_copy;
3386
3/4
✓ Branch 2 taken 402 times.
✓ Branch 3 taken 363675 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 402 times.
364077 WITH_GC(list_copy, lbm_list_copy(&len, args[1], ENC_SYM_NIL));
3387
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 364035 times.
364077 if (len <= 1) {
3388 42 stack_drop(ctx, 3);
3389 42 ctx->r = list_copy;
3390 42 ctx->app_cont = true;
3391 364077 return;
3392 }
3393
3394 364035 args[1] = list_copy; // Keep safe, original replaced on stack.
3395
3396 // Take the headmost 2, 1-element sublists.
3397 364035 lbm_value a = list_copy;
3398 364035 lbm_value b = lbm_cdr(a);
3399 364035 lbm_value rest = lbm_cdr(b);
3400 // Do not terminate b. keep rest of list safe from GC in the following
3401 // closure extraction.
3402 //lbm_set_cdr(a, b); // This is void
3403
3404 364035 lbm_value cl = lbm_cdr(args[0]);
3405 lbm_value cl0, cl1, cl2;
3406
1/2
✓ Branch 1 taken 364035 times.
✗ Branch 2 not taken.
364035 EXTRACT(cl, cl0); // CLO_PARAMS
3407
1/2
✓ Branch 1 taken 364035 times.
✗ Branch 2 not taken.
364035 EXTRACT(cl, cl1); // CLO_BODY
3408
1/2
✓ Branch 1 taken 364035 times.
✗ Branch 2 not taken.
364035 EXTRACT_NO_ADVANCE(cl, cl2); // CLO_ENV
3409 364035 lbm_value cmp_env = cl2;
3410
3411 364035 lbm_uint cl_len = lbm_list_length(cl0);
3412
1/2
✓ Branch 0 taken 364035 times.
✗ Branch 1 not taken.
364035 if (cl_len == 2) {
3413 364035 lbm_value par1 = get_car(cl0);
3414 364035 lbm_value par2 = get_cadr(cl0);
3415 lbm_value new_env0;
3416 lbm_value new_env;
3417
3/4
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 364029 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 6 times.
364035 WITH_GC(new_env0, lbm_env_set(cmp_env, par1, lbm_car(a)));
3418
3/4
✓ Branch 3 taken 36 times.
✓ Branch 4 taken 363999 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 36 times.
364035 WITH_GC_RMBR_1(new_env, lbm_env_set(new_env0, par2, lbm_car(b)), new_env0);
3419 364035 cmp_env = new_env;
3420
3421 364035 lbm_value cmp = cl1;
3422
3423 // Terminate the comparator argument list.
3424 364035 lbm_set_cdr(b, ENC_SYM_NIL);
3425
3426 364035 stack_drop(ctx, 3); //TODO: optimize drop 3, alloc 20 into alloc 17
3427 364035 lbm_uint *sptr = stack_reserve(ctx, 20);
3428 364035 sptr[0] = cmp;
3429 364035 sptr[1] = cmp_env;
3430 364035 sptr[2] = par1;
3431 364035 sptr[3] = par2;
3432 364035 sptr[4] = ENC_SYM_NIL; // head of merged accumulation of sublists
3433 364035 sptr[5] = ENC_SYM_NIL; // last of merged accumulation of sublists
3434 364035 sptr[6] = rest;
3435 364035 sptr[7] = lbm_enc_i(1);
3436 364035 sptr[8] = lbm_enc_i(len); //TODO: 28 bit i vs 32 bit i
3437 364035 sptr[9] = MERGE_LAYER;
3438 364035 sptr[10] = ENC_SYM_NIL; // head of merged sublist
3439 364035 sptr[11] = ENC_SYM_NIL; // last of merged sublist
3440 364035 sptr[12] = a;
3441 364035 sptr[13] = ENC_SYM_NIL; // no a_rest, 1 element lists in layer 1.
3442 364035 sptr[14] = ENC_SYM_NIL; // no b_rest, 1 element lists in layer 1.
3443 364035 sptr[15] = cmp;
3444 364035 sptr[16] = cmp_env;
3445 364035 sptr[17] = par1;
3446 364035 sptr[18] = par2;
3447 364035 sptr[19] = MERGE_REST;
3448 364035 ctx->curr_exp = cmp;
3449 364035 ctx->curr_env = cmp_env;
3450 364035 return;
3451 }
3452 }
3453 27 ERROR_AT_CTX(ENC_SYM_TERROR, ENC_SYM_SORT);
3454 }
3455
3456 804840 static void apply_rest_args(lbm_value *args, lbm_uint nargs, eval_context_t *ctx) {
3457 804840 lbm_value res = ENC_SYM_NIL; //TODO: lbm_env_lookup does not set res in all cases.
3458
2/2
✓ Branch 1 taken 798657 times.
✓ Branch 2 taken 6183 times.
804840 if (lbm_env_lookup_b(&res, ENC_SYM_REST_ARGS, ctx->curr_env)) {
3459
3/4
✓ Branch 0 taken 72192 times.
✓ Branch 1 taken 726465 times.
✓ Branch 3 taken 72192 times.
✗ Branch 4 not taken.
798657 if (nargs == 1 && lbm_is_number(args[0])) {
3460 72192 int32_t ix = lbm_dec_as_i32(args[0]);
3461 72192 res = lbm_index_list(res, ix);
3462 }
3463 798657 ctx->r = res;
3464 } else {
3465 6183 ctx->r = ENC_SYM_NIL;
3466 }
3467 804840 stack_drop(ctx, (unsigned int)nargs+1);
3468 804840 ctx->app_cont = true;
3469 804840 }
3470
3471 /* (rotate list-expr dist/dir-expr) */
3472 120 static void apply_rotate(lbm_value *args, lbm_uint nargs, eval_context_t *ctx) {
3473
6/6
✓ Branch 0 taken 114 times.
✓ Branch 1 taken 6 times.
✓ Branch 3 taken 111 times.
✓ Branch 4 taken 3 times.
✓ Branch 6 taken 108 times.
✓ Branch 7 taken 3 times.
120 if (nargs == 2 && lbm_is_list(args[0]) && lbm_is_number(args[1])) {
3474 108 int len = -1;
3475 lbm_value ls;
3476
1/4
✗ Branch 2 not taken.
✓ Branch 3 taken 108 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
108 WITH_GC(ls, lbm_list_copy(&len, args[0], ENC_SYM_NIL));
3477 108 int dist = lbm_dec_as_i32(args[1]);
3478
3/4
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 36 times.
✓ Branch 2 taken 72 times.
✗ Branch 3 not taken.
108 if (len > 0 && dist != 0) {
3479 72 int d = dist;
3480
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 36 times.
72 if (dist > 0) {
3481 36 ls = lbm_list_destructive_reverse(ls);
3482 } else {
3483 36 d = -dist;
3484 }
3485
3486 72 lbm_value start = ls;
3487 72 lbm_value end = ENC_SYM_NIL;
3488 72 lbm_value curr = start;
3489
2/2
✓ Branch 1 taken 324 times.
✓ Branch 2 taken 72 times.
396 while (lbm_is_cons(curr)) {
3490 324 end = curr;
3491 324 curr = lbm_ref_cell(curr)->cdr;
3492 }
3493
3494
2/2
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 72 times.
216 for (int i = 0; i < d; i ++) {
3495 144 lbm_value a = start;
3496 144 start = lbm_cdr(start);
3497 144 lbm_set_cdr(a, ENC_SYM_NIL);
3498 144 lbm_set_cdr(end, a);
3499 144 end = a;
3500 }
3501 72 ls = start;
3502
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 36 times.
72 if (dist > 0) {
3503 36 ls = lbm_list_destructive_reverse(ls);
3504 }
3505 }
3506 108 stack_drop(ctx, (unsigned int)nargs+1);
3507 108 ctx->app_cont = true;
3508 108 ctx->r = ls;
3509 108 return;
3510 }
3511 12 ERROR_CTX(ENC_SYM_EERROR);
3512 }
3513
3514 /***************************************************/
3515 /* Application lookup table */
3516
3517 typedef void (*apply_fun)(lbm_value *, lbm_uint, eval_context_t *);
3518 static const apply_fun fun_table[] =
3519 {
3520 apply_setvar,
3521 apply_read,
3522 apply_read_program,
3523 apply_read_eval_program,
3524 apply_spawn,
3525 apply_spawn_trap,
3526 apply_yield,
3527 apply_wait,
3528 apply_eval,
3529 apply_eval_program,
3530 apply_send,
3531 apply_ok,
3532 apply_error,
3533 apply_map,
3534 apply_reverse,
3535 apply_flatten,
3536 apply_unflatten,
3537 apply_kill,
3538 apply_sleep,
3539 apply_merge,
3540 apply_sort,
3541 apply_rest_args,
3542 apply_rotate,
3543 apply_apply,
3544 };
3545
3546
3547 /***************************************************/
3548 /* fptr call with retry on GC */
3549 295200347 static void call_fptr(lbm_value (*fptr)(lbm_value *, lbm_uint), lbm_value *args, lbm_uint arg_count, eval_context_t *ctx) {
3550 lbm_value res;
3551 #ifdef LBM_ALWAYS_GC
3552 gc();
3553 #endif
3554 295200347 res = fptr(&args[1], arg_count);
3555
2/2
✓ Branch 1 taken 490587 times.
✓ Branch 2 taken 294709760 times.
295200347 if (lbm_is_error(res)) {
3556
2/2
✓ Branch 1 taken 479607 times.
✓ Branch 2 taken 10980 times.
490587 if (lbm_is_symbol_merror(res)) {
3557 479607 gc();
3558 479607 res = fptr(&args[1], arg_count);
3559 }
3560
2/2
✓ Branch 1 taken 11067 times.
✓ Branch 2 taken 479520 times.
490587 if (lbm_is_error(res)) {
3561 11067 ERROR_AT_CTX(res, args[0]);
3562 }
3563 }
3564 295189280 stack_drop(ctx, (unsigned int)arg_count+1);
3565 295189280 ctx->app_cont = true;
3566 295189280 ctx->r = res;
3567 295189280 }
3568
3569 /***************************************************/
3570 /* Application of function that takes arguments */
3571 /* passed over the stack. */
3572
3573 304768818 static void application(eval_context_t *ctx, lbm_value *fun_args, lbm_uint arg_count) {
3574 /* If arriving here, we know that the fun is a symbol.
3575 * and can be a built in operation or an extension.
3576 */
3577 304768818 lbm_value fun = fun_args[0];
3578
3579 304768818 lbm_uint fun_val = lbm_dec_sym(fun);
3580 304768818 lbm_uint fun_kind = SYMBOL_KIND(fun_val);
3581
3582
4/4
✓ Branch 0 taken 751146 times.
✓ Branch 1 taken 294449201 times.
✓ Branch 2 taken 9568468 times.
✓ Branch 3 taken 3 times.
304768818 switch (fun_kind) {
3583 751146 case SYMBOL_KIND_EXTENSION:
3584 751146 call_fptr(extension_table[SYMBOL_IX(fun_val)].fptr, fun_args, arg_count, ctx);
3585
2/2
✓ Branch 0 taken 147 times.
✓ Branch 1 taken 746775 times.
746922 if (blocking_extension) {
3586 147 blocking_extension = false;
3587
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 147 times.
147 if (is_atomic) {
3588 // Check atomic_error explicitly so that the mutex
3589 // can be released if there is an error.
3590 ✗ lbm_mutex_unlock(&blocking_extension_mutex);
3591 ✗ atomic_error();
3592 }
3593
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 147 times.
147 if (blocking_extension_timeout) {
3594 ✗ blocking_extension_timeout = false;
3595 ✗ block_current_ctx(LBM_THREAD_STATE_TIMEOUT, blocking_extension_timeout_us,true);
3596 } else {
3597 147 block_current_ctx(LBM_THREAD_STATE_BLOCKED, 0,true);
3598 }
3599 147 lbm_mutex_unlock(&blocking_extension_mutex);
3600 }
3601 746922 break;
3602 294449201 case SYMBOL_KIND_FUNDAMENTAL:
3603 294449201 call_fptr(fundamental_table[SYMBOL_IX(fun_val)], fun_args, arg_count, ctx);
3604 294442357 break;
3605 9568468 case SYMBOL_KIND_APPFUN:
3606 9568468 fun_table[SYMBOL_IX(fun_val)](&fun_args[1], arg_count, ctx);
3607 9567739 break;
3608 3 default:
3609 // Symbols that are "special" but not in the way caught above
3610 // ends up here.
3611 3 lbm_set_error_reason("Symbol does not represent a function");
3612 3 ERROR_AT_CTX(ENC_SYM_EERROR,fun_args[0]);
3613 break;
3614 }
3615 304757018 }
3616
3617 // cont_cloure_application_args
3618 //
3619 // s[sp-5] = environment to evaluate the args in.
3620 // s[sp-4] = body
3621 // s[sp-3] = closure environment
3622 // s[sp-2] = parameter list (Guaranteed cons cell)
3623 // s[sp-1] = args list (Possibly NIL)
3624 //
3625 // ctx->r = evaluated argument.
3626 72881663 static void cont_closure_application_args(eval_context_t *ctx) {
3627 72881663 lbm_uint* sptr = get_stack_ptr(ctx, 5);
3628
3629 // Not naming these (arg_env, exp, clo_env) may seem like
3630 // poor programming practice, but has a measureable effect on performance on STM32F4.
3631 // Possible reasons:
3632 // - Minute difference in register pressure.
3633 // - Difficulty optimizing across function calls with unknown behavior (unless the compiler
3634 // would analyze it) in relation to the context stack.
3635
3636 //lbm_value arg_env = (lbm_value)sptr[0];
3637 //lbm_value exp = (lbm_value)sptr[1];
3638 //lbm_value clo_env = (lbm_value)sptr[2];
3639 72881663 lbm_value params = (lbm_value)sptr[3];
3640 72881663 lbm_value args = (lbm_value)sptr[4];
3641
3642 72881663 lbm_cons_t *params_cell = lbm_ref_cell(params);
3643
3644 72881663 lbm_value binder = allocate_binding(params_cell->car, ctx->r, sptr[2]);
3645
3646
2/2
✓ Branch 1 taken 35339365 times.
✓ Branch 2 taken 37542262 times.
72881627 if (lbm_is_cons(args)) { // There are args
3647 35339365 lbm_cons_t *args_cell = lbm_ref_cell(args);
3648
3649 // params_cell->cdr is programmer entered syntax, we need to check if a cons
3650
2/2
✓ Branch 1 taken 35302999 times.
✓ Branch 2 taken 36366 times.
35339365 if (lbm_is_cons(params_cell->cdr)) {
3651 35302999 sptr[2] = binder;
3652 35302999 sptr[3] = params_cell->cdr; // Guaranteed cons cell here
3653 35302999 sptr[4] = args_cell->cdr; // Possibly NIL
3654 35302999 stack_reserve(ctx,1)[0] = CLOSURE_ARGS;
3655 } else { // Args but no params => REST_ARGS
3656 36366 lbm_value rest_binder = allocate_binding(ENC_SYM_REST_ARGS, ENC_SYM_NIL, binder);
3657 36366 sptr[2] = rest_binder;
3658 36366 sptr[3] = args_cell->cdr;
3659 36366 sptr[4] = get_car(rest_binder); // last element of rest_args so far
3660 36366 stack_reserve(ctx,1)[0] = CLOSURE_ARGS_REST;
3661 }
3662 35339365 ctx->curr_exp = args_cell->car;
3663 35339365 ctx->curr_env = sptr[0]; //arg_env;
3664 } else { // No args
3665
2/2
✓ Branch 1 taken 37542256 times.
✓ Branch 2 taken 6 times.
37542262 if (lbm_is_symbol_nil(params_cell->cdr)) { // No Parameters
3666 37542256 stack_drop(ctx, 5);
3667 37542256 ctx->curr_env = binder;
3668 37542256 ctx->curr_exp = sptr[1]; //exp;
3669 } else { // Not enough arguments
3670 6 lbm_set_error_reason((char*)lbm_error_str_num_args);
3671 6 ERROR_CTX(ENC_SYM_EERROR);
3672 }
3673 }
3674 72881621 }
3675
3676 // cont_closure_args_rest
3677 //
3678 // s[sp-5] = environment to evaluate args in
3679 // s[sp-4] = body
3680 // s[sp-3] = closure environment
3681 // s[sp-2] = argument list
3682 // s[sp-1] = last cell in rest-args list so far.
3683 3853710 static void cont_closure_args_rest(eval_context_t *ctx) {
3684 3853710 lbm_uint* sptr = get_stack_ptr(ctx, 5);
3685 3853710 lbm_value arg_env = (lbm_value)sptr[0];
3686 3853710 lbm_value exp = (lbm_value)sptr[1];
3687 3853710 lbm_value clo_env = (lbm_value)sptr[2];
3688 3853710 lbm_value args = (lbm_value)sptr[3];
3689 3853710 lbm_value last = (lbm_value)sptr[4];
3690 3853710 lbm_cons_t* heap = lbm_heap_state.heap;
3691 #ifdef LBM_ALWAYS_GC
3692 gc();
3693 #endif
3694 3853710 lbm_value binding = lbm_heap_state.freelist;
3695
2/2
✓ Branch 0 taken 990 times.
✓ Branch 1 taken 3852720 times.
3853710 if (binding == ENC_SYM_NIL) {
3696 990 gc();
3697 990 binding = lbm_heap_state.freelist;
3698
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 990 times.
990 if (binding == ENC_SYM_NIL) ERROR_CTX(ENC_SYM_MERROR);
3699 }
3700 3853710 lbm_uint binding_ix = lbm_dec_ptr(binding);
3701 3853710 lbm_heap_state.freelist = heap[binding_ix].cdr;
3702 3853710 lbm_heap_state.num_free -= 1;
3703 3853710 heap[binding_ix].car = ctx->r;
3704 3853710 heap[binding_ix].cdr = ENC_SYM_NIL;
3705
3706 3853710 lbm_set_cdr(last, binding);
3707 3853710 sptr[4] = binding;
3708
3709
2/2
✓ Branch 0 taken 396546 times.
✓ Branch 1 taken 3457164 times.
3853710 if (args == ENC_SYM_NIL) {
3710 396546 stack_drop(ctx, 5);
3711 396546 ctx->curr_env = clo_env;
3712 396546 ctx->curr_exp = exp;
3713 } else {
3714 3457164 stack_reserve(ctx,1)[0] = CLOSURE_ARGS_REST;
3715 3457164 sptr[3] = get_cdr(args);
3716 3457164 ctx->curr_exp = get_car(args);
3717 3457164 ctx->curr_env = arg_env;
3718 }
3719 3853710 }
3720
3721
3722 // cont_application_args
3723 // Functions that take arguments passed on the stack, fundamental and apply_f.
3724 //
3725 // s[sp-3] = environment to evaluate arguments in.
3726 // s[sp-2] = argument list (user input syntax)
3727 // s[sp-1] = count
3728 //
3729 // ctx->r = function
3730 959880653 static void cont_application_args(eval_context_t *ctx) {
3731 959880653 lbm_uint *sptr = get_stack_ptr(ctx, 3);
3732
3733 959880653 lbm_value env = sptr[0];
3734 959880653 lbm_value rest = sptr[1];
3735 959880653 lbm_value count = sptr[2];
3736
3737 959880653 ctx->curr_env = env;
3738 959880653 sptr[0] = ctx->r; // Function 1st then Arguments
3739
2/2
✓ Branch 1 taken 655944234 times.
✓ Branch 2 taken 303936419 times.
959880653 if (lbm_is_cons(rest)) { // rest is user input syntax, expensive check needed
3740 655944234 lbm_cons_t *cell = lbm_ref_cell(rest);
3741 655944234 sptr[1] = env;
3742 655944234 sptr[2] = cell->cdr;
3743 655944234 lbm_value *rptr = stack_reserve(ctx,2);
3744 655944231 rptr[0] = count + (1 << LBM_VAL_SHIFT); // arithmetic on encoded value
3745 655944231 rptr[1] = APPLICATION_ARGS;
3746 655944231 ctx->curr_exp = cell->car;
3747 } else { // tollerant for incorrect list termination.
3748 // No more arguments
3749 303936419 stack_drop(ctx, 2);
3750 303936419 lbm_uint nargs = lbm_dec_u(count);
3751 303936419 lbm_value *args = get_stack_ptr(ctx, (uint32_t)(nargs + 1));
3752 303936419 application(ctx,args, nargs);
3753 }
3754 959868851 }
3755
3756 // cont_and
3757 //
3758 // s[sp-2] = environment to evaluate args in.
3759 // s[sp-1] = rest of argument list (user input syntax)
3760 70042836 static void cont_and(eval_context_t *ctx) {
3761 70042836 lbm_value rest = ctx->K.data[--ctx->K.sp];
3762 70042836 lbm_value env = ctx->K.data[--ctx->K.sp];
3763
2/2
✓ Branch 1 taken 362173 times.
✓ Branch 2 taken 69680663 times.
70042836 if (lbm_is_symbol_nil(ctx->r)) {
3764 362173 ctx->app_cont = true;
3765
2/2
✓ Branch 1 taken 49437389 times.
✓ Branch 2 taken 20243274 times.
69680663 } else if (lbm_is_cons(rest)) {
3766 49437389 lbm_cons_t *r_cell = lbm_ref_cell(rest);
3767 49437389 lbm_value *sptr = stack_reserve(ctx, 3);
3768 49437389 sptr[0] = env;
3769 49437389 sptr[1] = r_cell->cdr;
3770 49437389 sptr[2] = AND;
3771 49437389 ctx->curr_env = env;
3772 49437389 ctx->curr_exp = r_cell->car;
3773 } else {
3774 20243274 ctx->app_cont = true;
3775 }
3776 70042836 }
3777
3778 // cont_or
3779 //
3780 // s[sp-2] = environment to evaluate args in.
3781 // s[sp-1] = rest of argument list (user input syntax)
3782 21395 static void cont_or(eval_context_t *ctx) {
3783 21395 lbm_value rest = ctx->K.data[--ctx->K.sp];
3784 21395 lbm_value env = ctx->K.data[--ctx->K.sp];
3785
2/2
✓ Branch 1 taken 1200 times.
✓ Branch 2 taken 20195 times.
21395 if (!lbm_is_symbol_nil(ctx->r)) {
3786 1200 ctx->app_cont = true;
3787
2/2
✓ Branch 1 taken 11781 times.
✓ Branch 2 taken 8414 times.
20195 } else if (lbm_is_cons(rest)) {
3788 11781 lbm_value *sptr = stack_reserve(ctx, 3);
3789 11781 lbm_cons_t *r_cell = lbm_ref_cell(rest);
3790 11781 sptr[0] = env;
3791 11781 sptr[1] = r_cell->cdr;
3792 11781 sptr[2] = OR;
3793 11781 ctx->curr_exp = r_cell->car;
3794 11781 ctx->curr_env = env;
3795 } else {
3796 // if we end up here we have traversed all arguments
3797 // and seen no non-nil. (see top case).
3798 8414 ctx->app_cont = true;
3799 }
3800 21395 }
3801
3802 52714328 static void fill_binding_location(lbm_value key, lbm_value value, lbm_value env) {
3803
2/2
✓ Branch 1 taken 34712993 times.
✓ Branch 2 taken 18001335 times.
52714328 if (lbm_type_of(key) == LBM_TYPE_SYMBOL) {
3804 // NILs dual role makes it hard to detect the difference
3805 // between the end of a structural key or an attempt to use NIL as the key
3806 // or as part of big key.
3807 // NIL has been given the same role as dont care.
3808
2/2
✓ Branch 1 taken 27512738 times.
✓ Branch 2 taken 7200255 times.
34712993 if (lbm_dec_sym(key) >= RUNTIME_SYMBOLS_START) {
3809 27512738 lbm_env_modify_binding(env,key,value);
3810 } else {
3811
4/4
✓ Branch 0 taken 3600111 times.
✓ Branch 1 taken 3600144 times.
✓ Branch 2 taken 3600108 times.
✓ Branch 3 taken 3 times.
7200255 if (key == ENC_SYM_DONTCARE || key == ENC_SYM_NIL) return;
3812 3 lbm_set_error_reason((char*)lbm_error_str_built_in);
3813 3 ERROR_AT_CTX(ENC_SYM_EERROR, key);
3814 }
3815
3/4
✓ Branch 1 taken 18001335 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 18001332 times.
✓ Branch 4 taken 3 times.
36002670 } else if (lbm_is_cons(key) &&
3816 18001335 lbm_is_cons(value)) {
3817 18001332 fill_binding_location(lbm_ref_cell(key)->car, lbm_ref_cell(value)->car, env);
3818 18001332 fill_binding_location(lbm_ref_cell(key)->cdr, lbm_ref_cell(value)->cdr, env);
3819 } else {
3820 3 lbm_set_error_reason("Incorrect type of key in binding");
3821 3 ERROR_AT_CTX(ENC_SYM_TERROR, key);
3822 }
3823 }
3824
3825 // cont_bind_to_key_rest
3826 //
3827 // s[sp-4] = expression to evaluate in final env
3828 // s[sp-3] = rest of list of bindings
3829 // s[sp-2] = env to evaluate values in (Modified along the way)
3830 // s[sp-1] = key
3831 //
3832 // ctx->r = evaluated value to bind to key
3833 15872791 static void cont_bind_to_key_rest(eval_context_t *ctx) {
3834
3835 15872791 lbm_value *sptr = get_stack_ptr(ctx, 4);
3836
3837 15872791 lbm_value rest = sptr[1];
3838 15872791 lbm_value env = sptr[2];
3839 15872791 lbm_value key = sptr[3];
3840
3841 15872791 fill_binding_location(key, ctx->r, env);
3842
3843
2/2
✓ Branch 1 taken 210352 times.
✓ Branch 2 taken 15662433 times.
15872785 if (lbm_is_cons(rest)) {
3844 210352 lbm_value car_rest = lbm_ref_cell(rest)->car;
3845 210352 lbm_value cr = car_rest;
3846 lbm_value kv0, kv1;
3847
1/2
✓ Branch 1 taken 210352 times.
✗ Branch 2 not taken.
210352 EXTRACT(cr, kv0);
3848
1/2
✓ Branch 1 taken 210352 times.
✗ Branch 2 not taken.
210352 EXTRACT_NO_ADVANCE(cr, kv1);
3849
3850 210352 sptr[1] = lbm_ref_cell(rest)->cdr;
3851 210352 sptr[3] = kv0;
3852 210352 stack_reserve(ctx,1)[0] = BIND_TO_KEY_REST;
3853 210352 ctx->curr_exp = kv1;
3854 210352 ctx->curr_env = env;
3855 } else {
3856 // Otherwise evaluate the expression in the populated env
3857 15662433 ctx->curr_exp = sptr[0];
3858 15662433 ctx->curr_env = env;
3859 15662433 stack_drop(ctx, 4);
3860 }
3861 15872785 }
3862
3863 // cont_if
3864 //
3865 // s[sp-2] = then/else list (user input syntax)
3866 // s[sp-1] = environment
3867 //
3868 // ctx->r = evaluated condition
3869 38020547 static void cont_if(eval_context_t *ctx) {
3870
3871 38020547 lbm_value arg = ctx->r;
3872
3873 38020547 lbm_value *sptr = pop_stack_ptr(ctx, 2);
3874
3875 38020547 ctx->curr_env = sptr[1];
3876
2/2
✓ Branch 1 taken 37092966 times.
✓ Branch 2 taken 927581 times.
38020547 if (lbm_is_symbol_nil(arg)) {
3877 37092966 ctx->curr_exp = get_cadr(sptr[0]); // else branch
3878 } else {
3879 927581 ctx->curr_exp = get_car(sptr[0]); // then branch
3880 }
3881 38020544 }
3882
3883 // cont_match
3884 //
3885 // s[sp-1] = patterns (user input syntax)
3886 // s[sp-1] = orig_env
3887 //
3888 // ctx->r = expression to match against patterns
3889 68936 static void cont_match(eval_context_t *ctx) {
3890 68936 lbm_value e = ctx->r;
3891
3892 68936 lbm_uint *sptr = get_stack_ptr(ctx, 2);
3893 68936 lbm_value patterns = (lbm_value)sptr[0];
3894 68936 lbm_value orig_env = (lbm_value)sptr[1]; // restore enclosing environment.
3895 68936 lbm_value new_env = orig_env;
3896
3897
2/2
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 68930 times.
68936 if (lbm_is_symbol_nil(patterns)) {
3898 // no more patterns
3899 6 stack_drop(ctx, 2);
3900 6 ctx->r = ENC_SYM_NO_MATCH;
3901 6 ctx->app_cont = true;
3902
1/2
✓ Branch 1 taken 68930 times.
✗ Branch 2 not taken.
68930 } else if (lbm_is_cons(patterns)) {
3903 68930 lbm_value match_case = lbm_ref_cell(patterns)->car;
3904 68930 lbm_value pattern = get_car(match_case);
3905 68930 lbm_value n1 = get_cadr(match_case);
3906 68930 lbm_value n2 = get_cdr(get_cdr(match_case));
3907 lbm_value body;
3908 68930 bool check_guard = false;
3909
2/2
✓ Branch 1 taken 19682 times.
✓ Branch 2 taken 49248 times.
68930 if (lbm_is_symbol_nil(n2)) { // TODO: Not a very robust check.
3910 19682 body = n1;
3911 } else {
3912 49248 body = get_car(n2);
3913 49248 check_guard = true;
3914 }
3915 68930 bool is_match = match(pattern, e, &new_env, 0);
3916
2/2
✓ Branch 0 taken 18836 times.
✓ Branch 1 taken 50094 times.
68930 if (is_match) {
3917
2/2
✓ Branch 0 taken 7656 times.
✓ Branch 1 taken 11180 times.
18836 if (check_guard) {
3918 7656 lbm_value *rptr = stack_reserve(ctx,5);
3919 7656 sptr[0] = lbm_ref_cell(patterns)->cdr;
3920 7656 sptr[1] = ctx->curr_env;
3921 7656 rptr[0] = MATCH;
3922 7656 rptr[1] = new_env;
3923 7656 rptr[2] = body;
3924 7656 rptr[3] = e;
3925 7656 rptr[4] = MATCH_GUARD;
3926 7656 ctx->curr_env = new_env;
3927 7656 ctx->curr_exp = n1; // The guard
3928 } else {
3929 11180 stack_drop(ctx, 2);
3930 11180 ctx->curr_env = new_env;
3931 11180 ctx->curr_exp = body;
3932 }
3933 } else {
3934 // set up for checking of next pattern
3935 50094 sptr[0] = get_cdr(patterns);
3936 50094 sptr[1] = orig_env;
3937 50094 stack_reserve(ctx,1)[0] = MATCH;
3938 // leave r unaltered
3939 50094 ctx->app_cont = true;
3940 }
3941 } else {
3942 ✗ ERROR_AT_CTX(ENC_SYM_TERROR, ENC_SYM_MATCH);
3943 }
3944 68936 }
3945
3946 288 static void cont_exit_atomic(eval_context_t *ctx) {
3947 288 is_atomic = false; // atomic blocks cannot nest!
3948 288 ctx->app_cont = true;
3949 288 }
3950
3951 // cont_map:
3952 //
3953 // sptr[0]: s[sp-6] = Rest of the input list.
3954 // sptr[1]: s[sp-5] = Environment to restore for the eval of each application.
3955 // sptr[2]: s[sp-4] = Result list.
3956 // sptr[3]: s[sp-3] = Cell that goes into result list after being populated with application result.
3957 // sptr[4]: s[sp-2] = Ref to application.
3958 // sptr[5]: s[sp-1] = Ref to application argument.
3959 //
3960 // ctx->r = eval result of previous application.
3961 3459 static void cont_map(eval_context_t *ctx) {
3962 3459 lbm_value *sptr = get_stack_ptr(ctx, 6);
3963 3459 lbm_value ls = sptr[0];
3964 3459 lbm_value env = sptr[1];
3965 3459 lbm_value t = sptr[3]; // known cons!
3966 3459 lbm_ref_cell(t)->car = ctx->r;
3967 //lbm_set_car(t, ctx->r); // update car field tailmost position.
3968
2/2
✓ Branch 1 taken 2379 times.
✓ Branch 2 taken 1080 times.
3459 if (lbm_is_cons(ls)) {
3969 2379 lbm_cons_t *cell = lbm_ref_cell(ls); // already checked that cons.
3970 2379 lbm_value next = cell->car;
3971 2379 lbm_value rest = cell->cdr;
3972 2379 sptr[0] = rest;
3973 2379 stack_reserve(ctx,1)[0] = MAP;
3974 2379 lbm_ref_cell(sptr[5])->car = next; // update known cons
3975 //lbm_set_car(sptr[5], next); // new arguments
3976
3977 2379 lbm_value elt = cons_with_gc(ENC_SYM_NIL, ENC_SYM_NIL, ENC_SYM_NIL);
3978 2379 lbm_ref_cell(t)->cdr = elt;
3979 //lbm_set_cdr(t, elt);
3980 2379 sptr[3] = elt; // (r1 ... rN . (nil . nil))
3981 2379 ctx->curr_exp = sptr[4];
3982 2379 ctx->curr_env = env;
3983 } else {
3984 1080 ctx->r = sptr[2]; //head of result list
3985 1080 ctx->curr_env = env;
3986 1080 stack_drop(ctx, 6);
3987 1080 ctx->app_cont = true;
3988 }
3989 3459 }
3990
3991 7656 static void cont_match_guard(eval_context_t *ctx) {
3992
2/2
✓ Branch 1 taken 612 times.
✓ Branch 2 taken 7044 times.
7656 if (lbm_is_symbol_nil(ctx->r)) {
3993 612 lbm_value e = ctx->K.data[--ctx->K.sp];
3994 612 stack_drop(ctx, 2);
3995 612 ctx->r = e;
3996 612 ctx->app_cont = true;
3997 } else {
3998 7044 stack_drop(ctx, 1);
3999 7044 lbm_value body = ctx->K.data[--ctx->K.sp];
4000 7044 lbm_value env = ctx->K.data[--ctx->K.sp];
4001 7044 stack_drop(ctx, 3);
4002 7044 ctx->curr_env = env;
4003 7044 ctx->curr_exp = body;
4004 }
4005 7656 }
4006
4007 36 static void cont_terminate(eval_context_t *ctx) {
4008 36 ERROR_CTX(ctx->r);
4009 }
4010
4011 1190550 static void cont_loop(eval_context_t *ctx) {
4012 1190550 lbm_value *sptr = get_stack_ptr(ctx, 3);
4013 1190550 stack_reserve(ctx,1)[0] = LOOP_CONDITION;
4014 1190550 ctx->curr_env = sptr[2];
4015 1190550 ctx->curr_exp = sptr[1];
4016 1190550 }
4017
4018 1190922 static void cont_loop_condition(eval_context_t *ctx) {
4019
2/2
✓ Branch 1 taken 372 times.
✓ Branch 2 taken 1190550 times.
1190922 if (lbm_is_symbol_nil(ctx->r)) {
4020 372 stack_drop(ctx, 3);
4021 372 ctx->app_cont = true; // A loop returns nil? Makes sense to me... but in general?
4022 372 return;
4023 }
4024 1190550 lbm_value *sptr = get_stack_ptr(ctx, 3);
4025 1190550 stack_reserve(ctx,1)[0] = LOOP;
4026 1190550 ctx->curr_env = sptr[2];
4027 1190550 ctx->curr_exp = sptr[0];
4028 }
4029
4030 372 static void cont_loop_env_prep(eval_context_t *ctx) {
4031 372 lbm_value *sptr = get_stack_ptr(ctx, 3);
4032 372 sptr[2] = ctx->curr_env;
4033 372 stack_reserve(ctx,1)[0] = LOOP_CONDITION;
4034 372 ctx->curr_exp = sptr[1];
4035 372 }
4036
4037 11303757 static void cont_merge_rest(eval_context_t *ctx) {
4038 11303757 lbm_uint *sptr = get_stack_ptr(ctx, 9);
4039
4040 // If comparator returns true (result is in ctx->r):
4041 // "a" should be moved to the last element position in merged list.
4042 // A new element from "a_rest" should be moved into comparator argument 1 pos.
4043 // else
4044 // "b" should be moved to last element position in merged list.
4045 // A new element from "b_rest" should be moved into comparator argument 2 pos.
4046 //
4047 // If a_rest or b_rest is NIL:
4048 // we are done, the remaining elements of
4049 // non_nil list should be appended to merged list.
4050 // else
4051 // Set up for a new comparator evaluation and recurse.
4052 11303757 lbm_value a = sptr[2];
4053 11303757 lbm_value b = lbm_cdr(a);
4054 11303757 lbm_set_cdr(a, ENC_SYM_NIL); // terminate 1 element list
4055
4056
2/2
✓ Branch 0 taken 6560091 times.
✓ Branch 1 taken 4743666 times.
11303757 if (ctx->r == ENC_SYM_NIL) { // Comparison false
4057
4058
2/2
✓ Branch 0 taken 2550357 times.
✓ Branch 1 taken 4009734 times.
6560091 if (sptr[0] == ENC_SYM_NIL) {
4059 2550357 sptr[0] = b;
4060 2550357 sptr[1] = b;
4061 } else {
4062 4009734 lbm_set_cdr(sptr[1], b);
4063 4009734 sptr[1] = b;
4064 }
4065
2/2
✓ Branch 0 taken 3277923 times.
✓ Branch 1 taken 3282168 times.
6560091 if (sptr[4] == ENC_SYM_NIL) {
4066 3277923 lbm_set_cdr(a, sptr[3]);
4067 3277923 lbm_set_cdr(sptr[1], a);
4068 3277923 ctx->r = sptr[0];
4069 3277923 stack_drop(ctx, 9);
4070 3277923 ctx->app_cont = true;
4071 3277923 return;
4072 } else {
4073 3282168 b = sptr[4];
4074 3282168 sptr[4] = lbm_cdr(sptr[4]);
4075 3282168 lbm_set_cdr(b, ENC_SYM_NIL);
4076 }
4077 } else {
4078
2/2
✓ Branch 0 taken 1459137 times.
✓ Branch 1 taken 3284529 times.
4743666 if (sptr[0] == ENC_SYM_NIL) {
4079 1459137 sptr[0] = a;
4080 1459137 sptr[1] = a;
4081 } else {
4082 3284529 lbm_set_cdr(sptr[1], a);
4083 3284529 sptr[1] = a;
4084 }
4085
4086
2/2
✓ Branch 0 taken 731571 times.
✓ Branch 1 taken 4012095 times.
4743666 if (sptr[3] == ENC_SYM_NIL) {
4087 731571 lbm_set_cdr(b, sptr[4]);
4088 731571 lbm_set_cdr(sptr[1], b);
4089 731571 ctx->r = sptr[0];
4090 731571 stack_drop(ctx, 9);
4091 731571 ctx->app_cont = true;
4092 731571 return;
4093 } else {
4094 4012095 a = sptr[3];
4095 4012095 sptr[3] = lbm_cdr(sptr[3]);
4096 4012095 lbm_set_cdr(a, ENC_SYM_NIL);
4097 }
4098 }
4099 7294263 lbm_set_cdr(a, b);
4100 7294263 sptr[2] = a;
4101
4102 7294263 lbm_value par1 = sptr[7];
4103 7294263 lbm_value par2 = sptr[8];
4104 7294263 lbm_value cmp_body = sptr[5];
4105 7294263 lbm_value cmp_env = sptr[6];
4106 // Environment should be preallocated already at this point
4107 // and the operations below should never need GC.
4108 7294263 lbm_value new_env0 = lbm_env_set(cmp_env, par1, lbm_car(a));
4109 7294263 lbm_value new_env = lbm_env_set(new_env0, par2, lbm_car(b));
4110
2/4
✓ Branch 1 taken 7294263 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 7294263 times.
7294263 if (lbm_is_symbol(new_env0) || lbm_is_symbol(new_env)) {
4111 ✗ ERROR_CTX(ENC_SYM_FATAL_ERROR);
4112 }
4113 7294263 cmp_env = new_env;
4114
4115 7294263 stack_reserve(ctx,1)[0] = MERGE_REST;
4116 7294263 ctx->curr_exp = cmp_body;
4117 7294263 ctx->curr_env = cmp_env;
4118 }
4119
4120 // merge_layer stack contents
4121 // s[sp-9] = cmp
4122 // s[sp-8] = cmp_env
4123 // s[sp-7] = par1
4124 // s[sp-6] = par2
4125 // s[sp-5] = acc - first cell
4126 // s[sp-4] = acc - last cell
4127 // s[sp-3] = rest;
4128 // s[sp-2] = layer
4129 // s[sp-1] = length or original list
4130 //
4131 // ctx->r merged sublist
4132 4373217 static void cont_merge_layer(eval_context_t *ctx) {
4133 4373217 lbm_uint *sptr = get_stack_ptr(ctx, 9);
4134 4373217 lbm_int layer = lbm_dec_i(sptr[7]);
4135 4373217 lbm_int len = lbm_dec_i(sptr[8]);
4136
4137 4373217 lbm_value r_curr = ctx->r;
4138
1/2
✓ Branch 1 taken 17512641 times.
✗ Branch 2 not taken.
17512641 while (lbm_is_cons(r_curr)) {
4139 17512641 lbm_value next = lbm_ref_cell(r_curr)->cdr;
4140
2/2
✓ Branch 0 taken 4373217 times.
✓ Branch 1 taken 13139424 times.
17512641 if (next == ENC_SYM_NIL) {
4141 4373217 break;
4142 }
4143 13139424 r_curr = next;
4144 }
4145
4146
2/2
✓ Branch 0 taken 1455951 times.
✓ Branch 1 taken 2917266 times.
4373217 if (sptr[4] == ENC_SYM_NIL) {
4147 1455951 sptr[4] = ctx->r;
4148 1455951 sptr[5] = r_curr;
4149 } else {
4150 2917266 lbm_set_cdr(sptr[5], ctx->r); // accumulate merged sublists.
4151 2917266 sptr[5] = r_curr;
4152 }
4153
4154 4373217 lbm_value layer_rest = sptr[6];
4155 // switch layer or done ?
4156
2/2
✓ Branch 0 taken 1455951 times.
✓ Branch 1 taken 2917266 times.
4373217 if (layer_rest == ENC_SYM_NIL) {
4157
2/2
✓ Branch 0 taken 364023 times.
✓ Branch 1 taken 1091928 times.
1455951 if (layer * 2 >= len) {
4158 364023 ctx->r = sptr[4];
4159 364023 ctx->app_cont = true;
4160 364023 stack_drop(ctx, 9);
4161 364023 return;
4162 } else {
4163 // Setup for merges of the next layer
4164 1091928 layer = layer * 2;
4165 1091928 sptr[7] = lbm_enc_i(layer);
4166 1091928 layer_rest = sptr[4]; // continue on the accumulation of all sublists.
4167 1091928 sptr[5] = ENC_SYM_NIL;
4168 1091928 sptr[4] = ENC_SYM_NIL;
4169 }
4170 }
4171 // merge another sublist based on current layer.
4172 4009194 lbm_value a_list = layer_rest;
4173 // build sublist a
4174 4009194 lbm_value curr = layer_rest;
4175
2/2
✓ Branch 0 taken 5841165 times.
✓ Branch 1 taken 4009047 times.
9850212 for (int i = 0; i < layer-1; i ++) {
4176
2/2
✓ Branch 1 taken 5841018 times.
✓ Branch 2 taken 147 times.
5841165 if (lbm_is_cons(curr)) {
4177 5841018 curr = lbm_ref_cell(curr)->cdr;
4178 } else {
4179 147 break;
4180 }
4181 }
4182 4009194 layer_rest = lbm_cdr(curr);
4183 4009194 lbm_set_cdr(curr, ENC_SYM_NIL); //terminate sublist.
4184
4185 4009194 lbm_value b_list = layer_rest;
4186 // build sublist b
4187 4009194 curr = layer_rest;
4188
2/2
✓ Branch 0 taken 4381554 times.
✓ Branch 1 taken 3281241 times.
7662795 for (int i = 0; i < layer-1; i ++) {
4189
2/2
✓ Branch 1 taken 3653601 times.
✓ Branch 2 taken 727953 times.
4381554 if (lbm_is_cons(curr)) {
4190 3653601 curr = lbm_ref_cell(curr)->cdr;
4191 } else {
4192 727953 break;
4193 }
4194 }
4195 4009194 layer_rest = lbm_cdr(curr);
4196 4009194 lbm_set_cdr(curr, ENC_SYM_NIL); //terminate sublist.
4197
4198 4009194 sptr[6] = layer_rest;
4199
4200
2/2
✓ Branch 0 taken 364134 times.
✓ Branch 1 taken 3645060 times.
4009194 if (b_list == ENC_SYM_NIL) {
4201 364134 stack_reserve(ctx,1)[0] = MERGE_LAYER;
4202 364134 ctx->r = a_list;
4203 364134 ctx->app_cont = true;
4204 364134 return;
4205 }
4206 // Set up for a merge of sublists.
4207
4208 3645060 lbm_value a_rest = lbm_cdr(a_list);
4209 3645060 lbm_value b_rest = lbm_cdr(b_list);
4210 3645060 lbm_value a = a_list;
4211 3645060 lbm_value b = b_list;
4212 3645060 lbm_set_cdr(a, b);
4213 // Terminating the b list would be incorrect here
4214 // if there was any chance that the environment update below
4215 // performs GC.
4216 3645060 lbm_set_cdr(b, ENC_SYM_NIL);
4217
4218 3645060 lbm_value cmp_body = sptr[0];
4219 3645060 lbm_value cmp_env = sptr[1];
4220 3645060 lbm_value par1 = sptr[2];
4221 3645060 lbm_value par2 = sptr[3];
4222 // Environment should be preallocated already at this point
4223 // and the operations below should never need GC.
4224 3645060 lbm_value new_env0 = lbm_env_set(cmp_env, par1, lbm_car(a));
4225 3645060 lbm_value new_env = lbm_env_set(cmp_env, par2, lbm_car(b));
4226
2/4
✓ Branch 1 taken 3645060 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 3645060 times.
3645060 if (lbm_is_symbol(new_env0) || lbm_is_symbol(new_env)) {
4227 ✗ ERROR_CTX(ENC_SYM_FATAL_ERROR);
4228 }
4229 3645060 cmp_env = new_env;
4230
4231 3645060 lbm_uint *merge_cont = stack_reserve(ctx, 11);
4232 3645060 merge_cont[0] = MERGE_LAYER;
4233 3645060 merge_cont[1] = ENC_SYM_NIL;
4234 3645060 merge_cont[2] = ENC_SYM_NIL;
4235 3645060 merge_cont[3] = a;
4236 3645060 merge_cont[4] = a_rest;
4237 3645060 merge_cont[5] = b_rest;
4238 3645060 merge_cont[6] = cmp_body;
4239 3645060 merge_cont[7] = cmp_env;
4240 3645060 merge_cont[8] = par1;
4241 3645060 merge_cont[9] = par2;
4242 3645060 merge_cont[10] = MERGE_REST;
4243 3645060 ctx->curr_exp = cmp_body;
4244 3645060 ctx->curr_env = cmp_env;
4245 3645060 return;
4246 }
4247
4248 /****************************************************/
4249 /* READER */
4250
4251 484153 static void error_on_invalid_read_result(lbm_char_channel_t *str, eval_context_t *ctx) {
4252
2/2
✓ Branch 1 taken 44829 times.
✓ Branch 2 taken 439324 times.
484153 if (lbm_is_symbol(ctx->r)) {
4253 44829 lbm_uint sym_val = lbm_dec_sym(ctx->r);
4254
4/4
✓ Branch 0 taken 15021 times.
✓ Branch 1 taken 29808 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 15018 times.
44829 if (sym_val >= TOKENIZER_SYMBOLS_START &&
4255 sym_val <= TOKENIZER_SYMBOLS_END) {
4256 3 READ_ERROR_CTX(lbm_channel_row(str), lbm_channel_column(str));
4257 }
4258 }
4259 484150 }
4260
4261 50364 static void read_finish(lbm_char_channel_t *str, eval_context_t *ctx) {
4262
4263 /* Tokenizer reached "end of file"
4264 The parser could be in a state where it needs
4265 more tokens to correctly finish an expression.
4266
4267 Four cases
4268 1. The program / expression is malformed and the context should die.
4269 2. We are finished reading a program and should close off the
4270 internal representation with a closing parenthesis. Then
4271 apply continuation.
4272 3. We are finished reading an expression and should
4273 apply the continuation
4274 4. We are finished read-and-evaluating
4275
4276 In case 2, we should find the READ_DONE at sp - 4.
4277 In case 3, we should find the READ_DONE at sp - 1.
4278 In case 4, we should find the READ_DONE at sp - 3.
4279
4280 case 3 should not end up here, but rather end up in
4281 cont_read_done.
4282 */
4283
4284 50364 error_on_invalid_read_result(str, ctx);
4285
4286
3/4
✓ Branch 0 taken 50361 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 35569 times.
✓ Branch 3 taken 14792 times.
50361 if (ctx->K.sp > 4 && (ctx->K.data[ctx->K.sp - 3] == READ_DONE) &&
4287
2/2
✓ Branch 0 taken 35557 times.
✓ Branch 1 taken 12 times.
35569 (ctx->K.data[ctx->K.sp - 4] == READING_PROGRAM_INCREMENTALLY)) {
4288 /* read and evaluate is done */
4289 35557 --ctx->K.sp; // Pop READ_EVAL_CONTINUE
4290 35557 lbm_value env = ctx->K.data[--ctx->K.sp];
4291 35557 ctx->curr_env = env;
4292 35557 ctx->app_cont = true; // Program evaluated and result is in ctx->r.
4293
3/4
✓ Branch 0 taken 14804 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14750 times.
✓ Branch 3 taken 54 times.
14804 } else if (ctx->K.sp > 5 && (ctx->K.data[ctx->K.sp - 4] == READ_DONE) &&
4294
2/2
✓ Branch 0 taken 14732 times.
✓ Branch 1 taken 18 times.
14750 (ctx->K.data[ctx->K.sp - 5] == READING_PROGRAM)) {
4295 /* successfully finished reading a program (CASE 2) */
4296 14732 ctx->r = ENC_SYM_CLOSEPAR;
4297 14732 ctx->app_cont = true; // Perform the final READ_APPEND_CONTINUE
4298 } else {
4299
2/4
✓ Branch 1 taken 72 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 72 times.
72 if (lbm_channel_row(str) == 1 && lbm_channel_column(str) == 1) {
4300 // (read "") evaluates to nil.
4301 ✗ ctx->r = ENC_SYM_NIL;
4302 ✗ ctx->app_cont = true;
4303 } else {
4304 72 lbm_channel_reader_close(str);
4305 72 lbm_set_error_reason((char*)lbm_error_str_parse_eof);
4306 72 READ_ERROR_CTX(lbm_channel_row(str), lbm_channel_column(str));
4307 }
4308 }
4309 50289 }
4310
4311 /* cont_read_next_token
4312 uses ctx->reader_stream
4313 ctx->r : Input value ignored.
4314 */
4315 9730296 static void cont_read_next_token(eval_context_t *ctx) {
4316
4317 9730296 lbm_char_channel_t *chan = lbm_dec_channel(ctx->reader_stream);
4318
4319
2/4
✓ Branch 0 taken 9730296 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 9730296 times.
9730296 if (chan == NULL || chan->state == NULL) {
4320 ✗ ERROR_CTX(ENC_SYM_FATAL_ERROR);
4321 }
4322
4323
4/4
✓ Branch 1 taken 9114353 times.
✓ Branch 2 taken 615943 times.
✓ Branch 4 taken 21611 times.
✓ Branch 5 taken 9092742 times.
9730296 if (!lbm_channel_more(chan) && lbm_channel_is_empty(chan)) {
4324 21611 read_finish(chan, ctx);
4325 21536 return;
4326 }
4327 /* Eat whitespace and comments */
4328
2/2
✓ Branch 1 taken 1884 times.
✓ Branch 2 taken 9706801 times.
9708685 if (!tok_clean_whitespace(chan)) {
4329 1884 stack_reserve(ctx,1)[0] = READ_NEXT_TOKEN;
4330 1884 yield_ctx(EVAL_CPS_MIN_SLEEP);
4331 1884 return;
4332 }
4333 /* After eating whitespace we may be at end of file/stream */
4334
4/4
✓ Branch 1 taken 9093089 times.
✓ Branch 2 taken 613712 times.
✓ Branch 4 taken 28753 times.
✓ Branch 5 taken 9064336 times.
9706801 if (!lbm_channel_more(chan) && lbm_channel_is_empty(chan)) {
4335 28753 read_finish(chan, ctx);
4336 28753 return;
4337 }
4338
4339 /* Attempt to extract tokens from the character stream */
4340 9678048 int n = 0;
4341 9678048 lbm_value res = ENC_SYM_NIL;
4342 9678048 unsigned int string_len = 0;
4343
4344 /*
4345 * SYNTAX
4346 */
4347 uint32_t tok_match;
4348 9678048 n = tok_syntax(chan, &tok_match);
4349
2/2
✓ Branch 0 taken 3023558 times.
✓ Branch 1 taken 6654490 times.
9678048 if (n > 0) {
4350
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3023558 times.
3023558 if (!lbm_channel_drop(chan, (unsigned int)n)) {
4351 ✗ ERROR_CTX(ENC_SYM_FATAL_ERROR);
4352 }
4353 3023558 lbm_value compound_read_start = READ_START_BYTEARRAY;
4354 3023558 lbm_value compound_value_closer = ENC_SYM_CLOSEBRACK;
4355 3023558 lbm_value compound_value_opener = ENC_SYM_OPENBRACK;
4356
16/17
✓ Branch 0 taken 1454385 times.
✓ Branch 1 taken 1454325 times.
✓ Branch 2 taken 726 times.
✓ Branch 3 taken 4757 times.
✓ Branch 4 taken 717 times.
✓ Branch 5 taken 4748 times.
✓ Branch 6 taken 8426 times.
✓ Branch 7 taken 1407 times.
✓ Branch 8 taken 48305 times.
✓ Branch 9 taken 6869 times.
✓ Branch 10 taken 241 times.
✓ Branch 11 taken 18231 times.
✓ Branch 12 taken 10817 times.
✓ Branch 13 taken 4721 times.
✓ Branch 14 taken 4721 times.
✓ Branch 15 taken 162 times.
✗ Branch 16 not taken.
3023558 switch(tok_match) {
4357 1454385 case TOKOPENPAR: {
4358 1454385 lbm_value *rptr = stack_reserve(ctx,3);
4359 1454385 rptr[0] = ENC_SYM_NIL;
4360 1454385 rptr[1] = ENC_SYM_NIL;
4361 1454385 rptr[2] = READ_APPEND_CONTINUE;
4362 1454385 ctx->r = ENC_SYM_OPENPAR; // This return value is used in case the stream is ended here.
4363 // The stream having no more data after an OPENPAR is an error
4364 // and read_finish will make use of this info to issue an error.
4365 1454385 } break;
4366 1454325 case TOKCLOSEPAR: {
4367 1454325 ctx->r = ENC_SYM_CLOSEPAR;
4368 1454325 ctx->app_cont = true;
4369 1454325 } return;
4370 726 case TOKOPENARRAY:
4371 // Switch to array reader
4372 726 compound_read_start = READ_START_ARRAY;
4373 726 compound_value_opener = ENC_SYM_OPENARRAY; /* fall through */
4374 5483 case TOKOPENBRACK: {
4375 5483 lbm_value *rptr = stack_reserve(ctx, 1);
4376 5483 rptr[0] = compound_read_start;
4377 5483 ctx->r = compound_value_opener;
4378 5483 } break;
4379 717 case TOKCLOSEARRAY:
4380 717 compound_value_closer = ENC_SYM_CLOSEARRAY; /* fall through */
4381 5465 case TOKCLOSEBRACK:
4382 5465 ctx->r = compound_value_closer;
4383 5465 ctx->app_cont = true;
4384 5465 return;
4385 8426 case TOKDOT:
4386 8426 ctx->r = ENC_SYM_DOT;
4387 8426 ctx->app_cont = true;
4388 8426 return;
4389 1407 case TOKDONTCARE:
4390 1407 ctx->r = ENC_SYM_DONTCARE;
4391 1407 ctx->app_cont = true;
4392 1407 return;
4393 48305 case TOKQUOTE: {
4394 48305 lbm_value *rptr = stack_reserve(ctx, 2);
4395 48305 rptr[0] = ENC_SYM_QUOTE;
4396 48305 rptr[1] = WRAP_RESULT;
4397 48305 }break;
4398 6869 case TOKBACKQUOTE:
4399 6869 stack_reserve(ctx, 1)[0] = QQ_EXPAND_START;
4400 6869 break;
4401 241 case TOKCOMMAAT: {
4402 241 lbm_value *rptr = stack_reserve(ctx, 2);
4403 241 rptr[0] = ENC_SYM_COMMAAT;
4404 241 rptr[1] = WRAP_RESULT;
4405 241 } break;
4406 18231 case TOKCOMMA: {
4407 18231 lbm_value *rptr = stack_reserve(ctx, 2);
4408 18231 rptr[0] = ENC_SYM_COMMA;
4409 18231 rptr[1] = WRAP_RESULT;
4410 18231 } break;
4411 10817 case TOKMATCHANY:
4412 10817 ctx->r = ENC_SYM_MATCH_ANY;
4413 10817 ctx->app_cont = true;
4414 10817 return;
4415 4721 case TOKOPENCURL: {
4416 4721 lbm_value *rptr = stack_reserve(ctx,3);
4417 4721 rptr[0] = ENC_SYM_NIL;
4418 4721 rptr[1] = ENC_SYM_NIL;
4419 4721 rptr[2] = READ_APPEND_CONTINUE;
4420 4721 ctx->r = ENC_SYM_PROGN;
4421 4721 ctx->app_cont = true;
4422 4721 } return;
4423 4721 case TOKCLOSECURL:
4424 4721 ctx->r = ENC_SYM_CLOSEPAR;
4425 4721 ctx->app_cont = true;
4426 4721 return;
4427 162 case TOKCONSTSTART: /* fall through */
4428 case TOKCONSTEND: {
4429
2/2
✓ Branch 0 taken 82 times.
✓ Branch 1 taken 80 times.
162 if (tok_match == TOKCONSTSTART) ctx->flags |= EVAL_CPS_CONTEXT_FLAG_CONST;
4430
2/2
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 82 times.
162 if (tok_match == TOKCONSTEND) ctx->flags &= ~EVAL_CPS_CONTEXT_FLAG_CONST;
4431 162 stack_reserve(ctx,1)[0] = READ_NEXT_TOKEN;
4432 162 ctx->app_cont = true;
4433 162 } return;
4434 ✗ default:
4435 ✗ READ_ERROR_CTX(lbm_channel_row(chan), lbm_channel_column(chan));
4436 }
4437 // read next token
4438 1533514 stack_reserve(ctx, 1)[0] = READ_NEXT_TOKEN;
4439 1533514 ctx->app_cont = true;
4440 1533514 return;
4441
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6654490 times.
6654490 } else if (n < 0) goto retry_token;
4442
4443 /*
4444 * STRING
4445 */
4446 6654490 n = tok_string(chan, &string_len);
4447
2/2
✓ Branch 0 taken 17272 times.
✓ Branch 1 taken 6637218 times.
6654490 if (n >= 2) {
4448 17272 lbm_channel_drop(chan, (unsigned int)n);
4449 #ifdef LBM_ALWAYS_GC
4450 gc();
4451 #endif
4452
2/2
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 17263 times.
17272 if (!lbm_heap_allocate_array(&res, (unsigned int)(string_len+1))) {
4453 9 gc();
4454 9 lbm_heap_allocate_array(&res, (unsigned int)(string_len+1));
4455 }
4456
1/2
✓ Branch 1 taken 17272 times.
✗ Branch 2 not taken.
17272 if (lbm_is_ptr(res)) {
4457 17272 lbm_array_header_t *arr = assume_array(res);
4458 17272 char *data = (char*)arr->data;
4459 17272 memset(data,0, string_len + 1);
4460 17272 memcpy(data, tokpar_sym_str, string_len);
4461 17272 ctx->r = res;
4462 17272 ctx->app_cont = true;
4463 17272 return;
4464 } else {
4465 ✗ ERROR_CTX(ENC_SYM_MERROR);
4466 }
4467
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 6637162 times.
6637218 } else if (n < 0) goto retry_token;
4468
4469 /*
4470 * FLOAT
4471 */
4472 token_float f_val;
4473 6637162 n = tok_double(chan, &f_val);
4474
2/2
✓ Branch 0 taken 16487 times.
✓ Branch 1 taken 6620675 times.
6637162 if (n > 0) {
4475 16487 lbm_channel_drop(chan, (unsigned int) n);
4476
2/3
✓ Branch 0 taken 12552 times.
✓ Branch 1 taken 3935 times.
✗ Branch 2 not taken.
16487 switch(f_val.type) {
4477 12552 case TOKTYPEF32:
4478
1/4
✗ Branch 2 not taken.
✓ Branch 3 taken 12552 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
12552 WITH_GC(res, lbm_enc_float((float)f_val.value));
4479 12552 break;
4480 3935 case TOKTYPEF64:
4481 3935 res = lbm_enc_double(f_val.value);
4482 3935 break;
4483 ✗ default:
4484 ✗ READ_ERROR_CTX(lbm_channel_row(chan), lbm_channel_column(chan));
4485 }
4486 16487 ctx->r = res;
4487 16487 ctx->app_cont = true;
4488 16487 return;
4489
2/2
✓ Branch 0 taken 185 times.
✓ Branch 1 taken 6620490 times.
6620675 } else if (n < 0) goto retry_token;
4490
4491 /*
4492 * INTEGER
4493 */
4494 token_int int_result;
4495 6620490 n = tok_integer(chan, &int_result);
4496
2/2
✓ Branch 0 taken 4480523 times.
✓ Branch 1 taken 2139967 times.
6620490 if (n > 0) {
4497 4480523 lbm_channel_drop(chan, (unsigned int)n);
4498
7/8
✓ Branch 0 taken 3510 times.
✓ Branch 1 taken 4451957 times.
✓ Branch 2 taken 4522 times.
✓ Branch 3 taken 4752 times.
✓ Branch 4 taken 5931 times.
✓ Branch 5 taken 5142 times.
✓ Branch 6 taken 4709 times.
✗ Branch 7 not taken.
4480523 switch(int_result.type) {
4499 3510 case TOKTYPEBYTE:
4500
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3510 times.
3510 res = lbm_enc_char((uint8_t)(int_result.negative ? -int_result.value : int_result.value));
4501 3510 break;
4502 4451957 case TOKTYPEI:
4503
2/2
✓ Branch 0 taken 2276 times.
✓ Branch 1 taken 4449681 times.
4451957 res = lbm_enc_i((lbm_int)(int_result.negative ? -int_result.value : int_result.value));
4504 4451957 break;
4505 4522 case TOKTYPEU:
4506
2/2
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 4450 times.
4522 res = lbm_enc_u((lbm_uint)(int_result.negative ? -int_result.value : int_result.value));
4507 4522 break;
4508 4752 case TOKTYPEI32:
4509
3/8
✓ Branch 0 taken 82 times.
✓ Branch 1 taken 4670 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 4752 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
4752 WITH_GC(res, lbm_enc_i32((int32_t)(int_result.negative ? -int_result.value : int_result.value)));
4510 4752 break;
4511 5931 case TOKTYPEU32:
4512
3/8
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 5859 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5931 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
5931 WITH_GC(res,lbm_enc_u32((uint32_t)(int_result.negative ? -int_result.value : int_result.value)));
4513 5931 break;
4514 5142 case TOKTYPEI64:
4515
3/8
✓ Branch 0 taken 193 times.
✓ Branch 1 taken 4949 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5142 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
5142 WITH_GC(res,lbm_enc_i64((int64_t)(int_result.negative ? -int_result.value : int_result.value)));
4516 5142 break;
4517 4709 case TOKTYPEU64:
4518
3/8
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 4637 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 4709 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
4709 WITH_GC(res,lbm_enc_u64((uint64_t)(int_result.negative ? -int_result.value : int_result.value)));
4519 4709 break;
4520 ✗ default:
4521 ✗ READ_ERROR_CTX(lbm_channel_row(chan), lbm_channel_column(chan));
4522 }
4523 4480523 ctx->r = res;
4524 4480523 ctx->app_cont = true;
4525 4480523 return;
4526
2/2
✓ Branch 0 taken 85 times.
✓ Branch 1 taken 2139882 times.
2139967 } else if (n < 0) goto retry_token;
4527
4528 /*
4529 * SYMBOL
4530 */
4531 2139882 n = tok_symbol(chan);
4532
2/2
✓ Branch 0 taken 2138364 times.
✓ Branch 1 taken 1518 times.
2139882 if (n > 0) {
4533 2138364 lbm_channel_drop(chan, (unsigned int) n);
4534 lbm_uint symbol_id;
4535
2/2
✓ Branch 1 taken 145302 times.
✓ Branch 2 taken 1993062 times.
2138364 if (!lbm_get_symbol_by_name(tokpar_sym_str, &symbol_id)) {
4536 145302 int r = 0;
4537
4538 // Checked for every symbol read.
4539
2/2
✓ Branch 0 taken 37513 times.
✓ Branch 1 taken 107789 times.
145302 if (n > 4 &&
4540
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 37495 times.
37513 (memcmp(tokpar_sym_str, "ext-", 4) == 0)) {
4541
4542 // "ext-" symbols shouldnt have a regular symbol-id.
4543 // These symbols take a slot in the extension table
4544 // and follow a specialised lookup procedure.
4545 lbm_uint id;
4546
1/2
✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
18 if (!lbm_lookup_extension_id(tokpar_sym_str, &id)) {
4547 18 lbm_uint ext_name_len = (lbm_uint)n + 1;
4548 #ifdef LBM_ALWAYS_GC
4549 gc();
4550 #endif
4551 18 char *ext_name = lbm_malloc(ext_name_len);
4552
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (!ext_name) {
4553 ✗ gc();
4554 ✗ ext_name = lbm_malloc(ext_name_len);
4555 }
4556
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
18 if (ext_name) {
4557 18 memcpy(ext_name, tokpar_sym_str, ext_name_len);
4558 18 lbm_add_extension(ext_name, lbm_extensions_default);
4559 } else {
4560 ✗ ERROR_CTX(ENC_SYM_MERROR);
4561 }
4562 }
4563 // TODO: Change API so that an extra O(N) not needed?
4564
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
18 if (!lbm_lookup_extension_id(tokpar_sym_str, &id)) {
4565 ✗ ERROR_CTX(ENC_SYM_FATAL_ERROR);
4566 }
4567 18 symbol_id = id;
4568 18 r = 1;
4569 } else {
4570 145284 r = lbm_add_symbol_base(tokpar_sym_str, &symbol_id);
4571 }
4572
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 145302 times.
145302 if (!r) {
4573 ✗ READ_ERROR_CTX(lbm_channel_row(chan), lbm_channel_column(chan));
4574 }
4575 }
4576 2138364 ctx->r = lbm_enc_sym(symbol_id);
4577 2138364 ctx->app_cont = true;
4578 2138364 return;
4579
2/2
✓ Branch 0 taken 582 times.
✓ Branch 1 taken 936 times.
1518 } else if (n == TOKENIZER_NEED_MORE) {
4580 582 goto retry_token;
4581
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 933 times.
936 } else if (n <= TOKENIZER_STRING_ERROR) {
4582 3 READ_ERROR_CTX(lbm_channel_row(chan), lbm_channel_column(chan));
4583 }
4584
4585 /*
4586 * CHAR
4587 */
4588 char c_val;
4589 933 n = tok_char(chan, &c_val);
4590
2/2
✓ Branch 0 taken 891 times.
✓ Branch 1 taken 42 times.
933 if(n > 0) {
4591 891 lbm_channel_drop(chan,(unsigned int) n);
4592 891 ctx->r = lbm_enc_char((uint8_t)c_val);
4593 891 ctx->app_cont = true;
4594 891 return;
4595
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 6 times.
42 }else if (n < 0) goto retry_token;
4596
4597 6 READ_ERROR_CTX(lbm_channel_row(chan), lbm_channel_column(chan));
4598
4599 944 retry_token:
4600
2/2
✓ Branch 0 taken 860 times.
✓ Branch 1 taken 84 times.
944 if (n == TOKENIZER_NEED_MORE) {
4601 860 stack_reserve(ctx,1)[0] = READ_NEXT_TOKEN;
4602 860 yield_ctx(EVAL_CPS_MIN_SLEEP);
4603 860 return;
4604 }
4605 84 READ_ERROR_CTX(lbm_channel_row(chan), lbm_channel_column(chan));
4606 }
4607
4608 4757 static void cont_read_start_bytearray(eval_context_t *ctx) {
4609 4757 lbm_char_channel_t *str = lbm_dec_channel(ctx->reader_stream);
4610
2/4
✓ Branch 0 taken 4757 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4757 times.
4757 if (str == NULL || str->state == NULL) {
4611 ✗ ERROR_CTX(ENC_SYM_FATAL_ERROR);
4612 return; // INFER does not understand that error_ctx longjmps out
4613 // of this function here.
4614 }
4615
2/2
✓ Branch 0 taken 90 times.
✓ Branch 1 taken 4667 times.
4757 if (ctx->r == ENC_SYM_CLOSEBRACK) {
4616 lbm_value array;
4617
4618
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 90 times.
90 if (!lbm_heap_allocate_array(&array, 0)) {
4619 ✗ gc();
4620 ✗ if (!lbm_heap_allocate_array(&array, 0)) {
4621 ✗ lbm_set_error_reason((char*)lbm_error_str_read_no_mem);
4622 ✗ lbm_channel_reader_close(str);
4623 ✗ ERROR_CTX(ENC_SYM_FATAL_ERROR); // Terminates ctx
4624 }
4625 }
4626 90 ctx->r = array;
4627 90 ctx->app_cont = true;
4628
1/2
✓ Branch 1 taken 4667 times.
✗ Branch 2 not taken.
4667 } else if (lbm_is_number(ctx->r)) {
4629 #ifdef LBM_ALWAYS_GC
4630 gc();
4631 #endif
4632 4667 lbm_uint num_free = lbm_memory_longest_free();
4633 4667 lbm_uint initial_size = (lbm_uint)((float)num_free * 0.9);
4634
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4667 times.
4667 if (initial_size == 0) {
4635 ✗ gc();
4636 ✗ num_free = lbm_memory_longest_free();
4637 ✗ initial_size = (lbm_uint)((float)num_free * 0.9);
4638 ✗ if (initial_size == 0) {
4639 ✗ lbm_channel_reader_close(str);
4640 ✗ ERROR_CTX(ENC_SYM_MERROR);
4641 }
4642 }
4643 lbm_value array;
4644 // lbm_memory should not be large enough
4645 // for this to overflow a 32bit number.
4646 4667 initial_size = sizeof(lbm_uint) * initial_size;
4647
4648 // Keep in mind that this allocation can fail for both
4649 // lbm_memory and heap reasons.
4650
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4667 times.
4667 if (!lbm_heap_allocate_array(&array, initial_size)) {
4651 ✗ gc();
4652 ✗ if (!lbm_heap_allocate_array(&array, initial_size)) {
4653 ✗ lbm_set_error_reason((char*)lbm_error_str_read_no_mem);
4654 ✗ lbm_channel_reader_close(str);
4655 ✗ ERROR_CTX(ENC_SYM_FATAL_ERROR);
4656 // NOTE: If array is not created evaluation ends here.
4657 // Static analysis seems unaware.
4658 }
4659 }
4660
4661
4662 4667 lbm_value *rptr = stack_reserve(ctx, 4);
4663 4667 rptr[0] = array;
4664 4667 rptr[1] = lbm_enc_u(initial_size);
4665 4667 rptr[2] = lbm_enc_u(0);
4666 4667 rptr[3] = READ_APPEND_BYTEARRAY;
4667 4667 ctx->app_cont = true;
4668 } else {
4669 ✗ lbm_channel_reader_close(str);
4670 ✗ READ_ERROR_CTX(lbm_channel_row(str), lbm_channel_column(str));
4671 }
4672 }
4673
4674 480984 static void cont_read_append_bytearray(eval_context_t *ctx) {
4675 480984 lbm_uint *sptr = get_stack_ptr(ctx, 3);
4676
4677 480984 lbm_value array = sptr[0];
4678 480984 lbm_value size = lbm_dec_u(sptr[1]);
4679 480984 lbm_value ix = lbm_dec_u(sptr[2]);
4680
4681 // This is an MERROR because if this comparison
4682 // is true, there is a literal byte array in the code
4683 // and it did not fit into the array we are reading into,
4684 // which is 90% of free memory.
4685
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 480984 times.
480984 if (ix >= size) {
4686 ✗ ERROR_CTX(ENC_SYM_MERROR);
4687 }
4688
4689 // if sptr[0] is not an array something is very very wrong.
4690 // Not robust against a garbage on stack. But how would garbage get onto stack?
4691 480984 lbm_array_header_t *arr = assume_array(array);
4692
2/2
✓ Branch 1 taken 476326 times.
✓ Branch 2 taken 4658 times.
480984 if (lbm_is_number(ctx->r)) {
4693 476326 ((uint8_t*)arr->data)[ix] = (uint8_t)lbm_dec_as_u32(ctx->r);
4694
4695 476326 sptr[2] = lbm_enc_u(ix + 1);
4696 476326 lbm_value *rptr = stack_reserve(ctx, 2);
4697 476326 rptr[0] = READ_APPEND_BYTEARRAY;
4698 476326 rptr[1] = READ_NEXT_TOKEN;
4699 476326 ctx->app_cont = true;
4700
3/4
✓ Branch 1 taken 4658 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4655 times.
✓ Branch 4 taken 3 times.
4658 } else if (lbm_is_symbol(ctx->r) && ctx->r == ENC_SYM_CLOSEBRACK) {
4701 4655 lbm_uint array_size = ix / sizeof(lbm_uint);
4702
4703
2/2
✓ Branch 0 taken 3769 times.
✓ Branch 1 taken 886 times.
4655 if (ix % sizeof(lbm_uint) != 0) {
4704 3769 array_size = array_size + 1;
4705 }
4706 4655 lbm_memory_shrink((lbm_uint*)arr->data, array_size);
4707 4655 arr->size = ix;
4708 4655 stack_drop(ctx, 3);
4709 4655 ctx->r = array;
4710 4655 ctx->app_cont = true;
4711 } else {
4712 3 ERROR_CTX(ENC_SYM_TERROR);
4713 }
4714 480981 }
4715
4716 // Lisp array syntax reading ////////////////////////////////////////
4717
4718 726 static void cont_read_start_array(eval_context_t *ctx) {
4719 726 lbm_char_channel_t *str = lbm_dec_channel(ctx->reader_stream);
4720
2/4
✓ Branch 0 taken 726 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 726 times.
726 if (str == NULL || str->state == NULL) {
4721 ✗ ERROR_CTX(ENC_SYM_FATAL_ERROR);
4722 return; // INFER does not understand that error_ctx longjmps out
4723 // of this function here.
4724 }
4725
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 686 times.
726 if (ctx->r == ENC_SYM_CLOSEARRAY) {
4726 lbm_value array;
4727
4728
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 40 times.
40 if (!lbm_heap_allocate_lisp_array(&array, 0)) {
4729 ✗ gc();
4730 ✗ if (!lbm_heap_allocate_lisp_array(&array, 0)) {
4731 ✗ lbm_set_error_reason((char*)lbm_error_str_read_no_mem);
4732 ✗ lbm_channel_reader_close(str);
4733 ✗ ERROR_CTX(ENC_SYM_FATAL_ERROR); // Terminates ctx
4734 }
4735 }
4736 40 ctx->r = array;
4737 40 ctx->app_cont = true;
4738 } else {
4739 #ifdef LBM_ALWAYS_GC
4740 gc();
4741 #endif
4742 686 lbm_uint num = ((lbm_uint)((float)lbm_memory_longest_free() * 0.9) / sizeof(lbm_uint)) ;
4743 686 lbm_uint initial_size = (lbm_uint)num;
4744
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 686 times.
686 if (initial_size == 0) {
4745 ✗ gc();
4746 ✗ num = ((lbm_uint)((float)lbm_memory_longest_free() * 0.9) / sizeof(lbm_uint)) ;
4747 ✗ initial_size = (lbm_uint)num;
4748 ✗ if (initial_size == 0) {
4749 ✗ lbm_channel_reader_close(str);
4750 ✗ ERROR_CTX(ENC_SYM_MERROR);
4751 }
4752 }
4753 lbm_value array;
4754 // lbm_memory should not be large enough
4755 // for this to overflow a 32bit number.
4756 686 initial_size = sizeof(lbm_uint) * initial_size;
4757
4758
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 686 times.
686 if (!lbm_heap_allocate_lisp_array(&array, initial_size)) {
4759 ✗ gc();
4760 ✗ if (!lbm_heap_allocate_lisp_array(&array, initial_size)) {
4761 ✗ lbm_set_error_reason((char*)lbm_error_str_read_no_mem);
4762 ✗ lbm_channel_reader_close(str);
4763 ✗ ERROR_CTX(ENC_SYM_FATAL_ERROR);
4764 }
4765 }
4766
4767 686 lbm_value *rptr = stack_reserve(ctx, 4);
4768 686 rptr[0] = array;
4769 686 rptr[1] = lbm_enc_u(initial_size);
4770 686 rptr[2] = lbm_enc_u(0);
4771 686 rptr[3] = READ_APPEND_ARRAY;
4772 686 ctx->app_cont = true;
4773 }
4774 }
4775
4776 3174 static void cont_read_append_array(eval_context_t *ctx) {
4777 3174 lbm_uint *sptr = get_stack_ptr(ctx, 3);
4778
4779 3174 lbm_value array = sptr[0];
4780 3174 lbm_value size = lbm_dec_as_u32(sptr[1]);
4781 3174 lbm_value ix = lbm_dec_as_u32(sptr[2]);
4782
4783 // This is a MERROR because if this comparison is true
4784 // it means that a literal lisp aray is requireing more that
4785 // 90% of currently free memory. The programmer should
4786 // be informed that her program uses too much memory.
4787
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3174 times.
3174 if (ix >= (size / sizeof(lbm_uint))) {
4788 ✗ ERROR_CTX(ENC_SYM_MERROR);
4789 }
4790
4791 // if sptr[0] is not an array something is very very wrong.
4792 // Not robust against a garbage on stack. But how would garbage get onto stack?
4793 3174 lbm_array_header_t *arr = assume_array(array);
4794
4/4
✓ Branch 1 taken 2088 times.
✓ Branch 2 taken 1086 times.
✓ Branch 3 taken 677 times.
✓ Branch 4 taken 1411 times.
3174 if (lbm_is_symbol(ctx->r) && ctx->r == ENC_SYM_CLOSEARRAY) {
4795 677 lbm_uint array_size = ix;
4796
4797
2/2
✓ Branch 0 taken 491 times.
✓ Branch 1 taken 186 times.
677 if (ix % sizeof(lbm_uint) != 0) {
4798 491 array_size = array_size + 1;
4799 }
4800 677 lbm_memory_shrink((lbm_uint*)arr->data, array_size);
4801 // Ix is small and ix * sizeof(lbm_uint) will not overflow.
4802 677 arr->size = ix * sizeof(lbm_uint);
4803 677 stack_drop(ctx, 3);
4804 677 ctx->r = array;
4805 677 ctx->app_cont = true;
4806 } else {
4807 2497 ((lbm_uint*)arr->data)[ix] = ctx->r;
4808
4809 2497 sptr[2] = lbm_enc_u(ix + 1);
4810 2497 lbm_value *rptr = stack_reserve(ctx, 2);
4811 2497 rptr[0] = READ_APPEND_ARRAY;
4812 2497 rptr[1] = READ_NEXT_TOKEN;
4813 2497 ctx->app_cont = true;
4814 }
4815 3174 }
4816
4817 // read_grab_row leads into an implicit read_next_token.
4818 621359 static void cont_read_next_token_grab_row(eval_context_t *ctx) {
4819 621359 lbm_char_channel_t *chan = lbm_dec_channel(ctx->reader_stream);
4820
2/4
✓ Branch 0 taken 621359 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 621359 times.
621359 if (chan == NULL || chan->state == NULL) {
4821 ✗ ERROR_CTX(ENC_SYM_FATAL_ERROR);
4822 }
4823 621359 ctx->row0 = (int32_t)lbm_channel_row(chan);
4824 621359 ctx->row1 = -1; // a new start, end is unknown
4825 621359 cont_read_next_token(ctx);
4826 621284 }
4827
4828 // Lisp list syntax reading ////////////////////////////////////////
4829
4830 8550656 static void cont_read_append_continue(eval_context_t *ctx) {
4831 8550656 lbm_value *sptr = get_stack_ptr(ctx, 2);
4832
4833 8550656 lbm_value first_cell = sptr[0];
4834 8550656 lbm_value last_cell = sptr[1];
4835
4836 8550656 lbm_char_channel_t *str = lbm_dec_channel(ctx->reader_stream);
4837
2/4
✓ Branch 0 taken 8550656 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8550656 times.
8550656 if (str == NULL || str->state == NULL) {
4838 ✗ ERROR_CTX(ENC_SYM_FATAL_ERROR);
4839 }
4840
4841
2/2
✓ Branch 1 taken 3561471 times.
✓ Branch 2 taken 4989185 times.
8550656 if (lbm_is_symbol(ctx->r)) {
4842
3/3
✓ Branch 0 taken 1465355 times.
✓ Branch 1 taken 8426 times.
✓ Branch 2 taken 2087690 times.
3561471 switch(ctx->r) {
4843 1465355 case ENC_SYM_CLOSEPAR:
4844 // last_cell is either nil or a rw cons cell.
4845
2/2
✓ Branch 1 taken 4321 times.
✓ Branch 2 taken 1461034 times.
1465355 if (lbm_is_symbol_nil(last_cell)) {
4846 4321 ctx->r = last_cell;
4847 } else {
4848 // Here we know, by the principled use of the stack, that last_cell is a rw cons cell.
4849 1461034 lbm_ref_cell(last_cell)->cdr = ENC_SYM_NIL;
4850 // Could even rewrite as: lbm_heap_state.heap[lbm_dec_ptr(last_cell)].cdr = ENC_SYM_NIL;
4851 1461034 ctx->r = first_cell;
4852 }
4853 1465355 stack_drop(ctx, 2);
4854 // Skip reading another token and apply the continuation.
4855 1465355 ctx->app_cont = true;
4856 1465355 return;
4857 8426 case ENC_SYM_DOT: {
4858 8426 lbm_value *rptr = stack_reserve(ctx, 2);
4859 8426 rptr[0] = READ_DOT_TERMINATE;
4860 8426 rptr[1] = READ_NEXT_TOKEN;
4861 8426 ctx->app_cont = true;
4862 8426 } return;
4863 }
4864 }
4865 7076875 lbm_value new_cell = cons_with_gc(ctx->r, ENC_SYM_NIL, ENC_SYM_NIL);
4866 // Does not return if merror. So we cannot get a read-error here
4867 // unless we write the a version of cons_with_gc here.
4868 //if (lbm_is_symbol_merror(new_cell)) {
4869 // lbm_channel_reader_close(str);
4870 // read_error_ctx(lbm_channel_row(str), lbm_channel_column(str));
4871 // return;
4872 //}
4873
2/2
✓ Branch 1 taken 1469514 times.
✓ Branch 2 taken 5607355 times.
7076869 if (lbm_is_symbol_nil(last_cell)) {
4874 1469514 first_cell = last_cell = new_cell;
4875 } else {
4876 5607355 lbm_ref_cell(last_cell)->cdr = new_cell;
4877 5607355 last_cell = new_cell;
4878 }
4879
4880 7076869 sptr[0] = first_cell;
4881 7076869 sptr[1] = last_cell;
4882 7076869 lbm_value *rptr = stack_reserve(ctx, 2);
4883 7076869 rptr[0] = READ_APPEND_CONTINUE;
4884 7076869 rptr[1] = READ_NEXT_TOKEN;
4885 7076869 ctx->app_cont = true;
4886 }
4887
4888 187360 static void cont_read_eval_continue(eval_context_t *ctx) {
4889 lbm_value env;
4890 187360 lbm_value *sptr = get_stack_ptr(ctx, 1);
4891 187360 env = sptr[0];
4892 187360 lbm_char_channel_t *str = lbm_dec_channel(ctx->reader_stream);
4893
2/4
✓ Branch 0 taken 187360 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 187360 times.
✗ Branch 3 not taken.
187360 if (str && str->state) {
4894 187360 ctx->row1 = (lbm_int)str->row(str);
4895
2/2
✓ Branch 1 taken 13299 times.
✓ Branch 2 taken 174061 times.
187360 if (lbm_type_of(ctx->r) == LBM_TYPE_SYMBOL) {
4896
1/3
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 13299 times.
13299 switch(ctx->r) {
4897 ✗ case ENC_SYM_CLOSEPAR:
4898 ✗ stack_drop(ctx, 1);
4899 ✗ ctx->app_cont = true;
4900 ✗ return;
4901 ✗ case ENC_SYM_DOT:
4902 // A dot here is a syntax error.
4903 ✗ lbm_set_error_reason((char*)lbm_error_str_parse_dot);
4904 ✗ READ_ERROR_CTX(lbm_channel_row(str),lbm_channel_column(str));
4905 return;
4906 }
4907 }
4908 187360 lbm_value *rptr = stack_reserve(ctx, 5);
4909 187360 rptr[0] = READ_EVAL_CONTINUE;
4910 187360 rptr[1] = READ_NEXT_TOKEN_GRAB_ROW;
4911 187360 rptr[2] = ctx->reader_stream;
4912 187360 rptr[3] = lbm_enc_u(ctx->flags);
4913 187360 rptr[4] = POP_READER_FLAGS;
4914
4915 // Evaluate result of reading (in r) and then continue with
4916 // reading reading the rest of the program.
4917 187360 ctx->curr_env = env;
4918 187360 ctx->curr_exp = ctx->r;
4919 } else {
4920 ✗ ERROR_CTX(ENC_SYM_FATAL_ERROR);
4921 }
4922 }
4923
4924 8420 static void cont_read_expect_closepar(eval_context_t *ctx) {
4925 8420 lbm_value res = ctx->K.data[--ctx->K.sp];
4926
4927 8420 lbm_char_channel_t *str = lbm_dec_channel(ctx->reader_stream);
4928
2/4
✓ Branch 0 taken 8420 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8420 times.
8420 if (str == NULL || str->state == NULL) { // TODO: De Morgan these conditions.
4929 ✗ ERROR_CTX(ENC_SYM_FATAL_ERROR);
4930 } else {
4931
2/2
✓ Branch 1 taken 8417 times.
✓ Branch 2 taken 3 times.
8420 if (lbm_type_of(ctx->r) == LBM_TYPE_SYMBOL &&
4932
1/2
✓ Branch 0 taken 8417 times.
✗ Branch 1 not taken.
8417 ctx->r == ENC_SYM_CLOSEPAR) {
4933 8417 ctx->r = res;
4934 8417 ctx->app_cont = true;
4935 } else {
4936 3 lbm_channel_reader_close(str);
4937 3 lbm_set_error_reason((char*)lbm_error_str_parse_close);
4938 3 READ_ERROR_CTX(lbm_channel_row(str), lbm_channel_column(str));
4939 }
4940 }
4941 8417 }
4942
4943 8426 static void cont_read_dot_terminate(eval_context_t *ctx) {
4944 8426 lbm_value *sptr = get_stack_ptr(ctx, 2);
4945
4946 8426 lbm_value last_cell = sptr[1];
4947
4948 8426 lbm_char_channel_t *str = lbm_dec_channel(ctx->reader_stream);
4949
2/4
✓ Branch 0 taken 8426 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8426 times.
8426 if (str == NULL || str->state == NULL) {
4950 ✗ ERROR_CTX(ENC_SYM_FATAL_ERROR);
4951
2/2
✓ Branch 1 taken 2234 times.
✓ Branch 2 taken 6192 times.
8426 } else if (lbm_type_of(ctx->r) == LBM_TYPE_SYMBOL &&
4952
2/2
✓ Branch 0 taken 2231 times.
✓ Branch 1 taken 3 times.
2234 (ctx->r == ENC_SYM_CLOSEPAR ||
4953
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2231 times.
2231 ctx->r == ENC_SYM_DOT)) {
4954 3 lbm_channel_reader_close(str);
4955 3 lbm_set_error_reason((char*)lbm_error_str_parse_dot);
4956 3 READ_ERROR_CTX(lbm_channel_row(str), lbm_channel_column(str));
4957
2/2
✓ Branch 1 taken 8420 times.
✓ Branch 2 taken 3 times.
8423 } else if (lbm_is_cons(last_cell)) {
4958 8420 lbm_ref_cell(last_cell)->cdr = ctx->r;
4959 //lbm_set_cdr(last_cell, ctx->r);
4960 8420 ctx->r = sptr[0]; // first cell
4961 8420 lbm_value *rptr = stack_reserve(ctx, 1);
4962 8420 sptr[0] = ctx->r;
4963 8420 sptr[1] = READ_EXPECT_CLOSEPAR;
4964 8420 rptr[0] = READ_NEXT_TOKEN;
4965 8420 ctx->app_cont = true;
4966 } else {
4967 3 lbm_channel_reader_close(str);
4968 3 lbm_set_error_reason((char*)lbm_error_str_parse_dot);
4969 3 READ_ERROR_CTX(lbm_channel_row(str), lbm_channel_column(str));
4970 }
4971 8420 }
4972
4973 433789 static void cont_read_done(eval_context_t *ctx) {
4974 433789 --ctx->K.sp; // Ignore reader mode.
4975
4976 433789 lbm_char_channel_t *str = lbm_dec_channel(ctx->reader_stream);
4977
2/4
✓ Branch 0 taken 433789 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 433789 times.
433789 if (str == NULL || str->state == NULL) {
4978 ✗ ERROR_CTX(ENC_SYM_FATAL_ERROR);
4979 }
4980
4981 433789 lbm_channel_reader_close(str);
4982
4983 // Reading row/col from closed channel is OK!
4984 433789 error_on_invalid_read_result(str, ctx);
4985
4986 433789 ctx->row0 = -1;
4987 433789 ctx->row1 = -1;
4988 433789 ctx->reader_stream = ENC_SYM_NIL;
4989 433789 ctx->app_cont = true;
4990 433789 }
4991
4992 66366 static void cont_wrap_result(eval_context_t *ctx) {
4993 lbm_value cell;
4994 66366 lbm_value wrapper = ctx->K.data[--ctx->K.sp];
4995
3/4
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 66363 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 3 times.
66366 WITH_GC(cell, lbm_heap_allocate_list_init(2,
4996 wrapper,
4997 ctx->r));
4998 66366 ctx->r = cell;
4999 66366 ctx->app_cont = true;
5000 66366 }
5001
5002 // cont_application_start
5003 //
5004 // sptr[0] = env
5005 // sptr[1] = args
5006 //
5007 // ctx->r = function
5008 354441584 static void cont_application_start(eval_context_t *ctx) {
5009
2/2
✓ Branch 1 taken 303936905 times.
✓ Branch 2 taken 50504679 times.
354441584 if (lbm_is_symbol(ctx->r)) {
5010 303936905 stack_reserve(ctx,1)[0] = lbm_enc_u(0);
5011 303936904 cont_application_args(ctx);
5012
2/2
✓ Branch 1 taken 50504668 times.
✓ Branch 2 taken 12 times.
50504679 } else if (lbm_is_cons(ctx->r)) {
5013 50504668 lbm_uint *sptr = get_stack_ptr(ctx, 2);
5014 50504668 lbm_value args = (lbm_value)sptr[1];
5015
4/5
✓ Branch 1 taken 50473643 times.
✓ Branch 2 taken 252 times.
✓ Branch 3 taken 18 times.
✓ Branch 4 taken 30755 times.
✗ Branch 5 not taken.
50504668 switch (lbm_ref_cell(ctx->r)->car) { // Already checked that is_cons
5016 50473643 case ENC_SYM_CLOSURE: {
5017 50473643 lbm_value cl = lbm_ref_cell(ctx->r)->cdr; // Already checked that is_cons
5018 lbm_value cl0, cl1, cl2;
5019
1/2
✓ Branch 1 taken 50473643 times.
✗ Branch 2 not taken.
50473643 EXTRACT(cl, cl0); // CLO_PARAMS
5020
1/2
✓ Branch 1 taken 50473643 times.
✗ Branch 2 not taken.
50473643 EXTRACT(cl, cl1); // CLO_BODY
5021
1/2
✓ Branch 1 taken 50473643 times.
✗ Branch 2 not taken.
50473643 EXTRACT_NO_ADVANCE(cl, cl2); // CLO_ENV
5022 50473643 lbm_value arg_env = (lbm_value)sptr[0];
5023
5024
2/2
✓ Branch 1 taken 37939000 times.
✓ Branch 2 taken 12534643 times.
50473643 if (lbm_is_cons(args)) { // There are args
5025 37939000 lbm_cons_t *args_cell = lbm_ref_cell(args);
5026 37939000 sptr[1] = cl1; // WARNING overwrites args on stack. args are unprotected.
5027
2/2
✓ Branch 1 taken 37578820 times.
✓ Branch 2 taken 360180 times.
37939000 if (lbm_is_cons(cl0)) { // There are params
5028 37578820 lbm_value *reserved = stack_reserve(ctx, 4);
5029 37578820 reserved[0] = cl2;
5030 37578820 reserved[1] = cl0; // Guaranteed cons cell here
5031 37578820 reserved[2] = args_cell->cdr; // Possibly NIL
5032 37578820 reserved[3] = CLOSURE_ARGS;
5033 37578820 ctx->curr_exp = args_cell->car;
5034 37578820 ctx->curr_env = arg_env;
5035 } else { // args but no params => REST_ARGS
5036 360180 lbm_value *reserved0 = stack_reserve(ctx, 2);
5037 360180 reserved0[0] = ENC_SYM_NIL;;
5038 360180 reserved0[1] = args_cell->cdr;
5039 360180 ctx->curr_exp = args_cell->car; // protect car(args) from allocate binding
5040 360180 ctx->curr_env = arg_env;
5041 360180 lbm_value rest_binder = allocate_binding(ENC_SYM_REST_ARGS, ENC_SYM_NIL, cl2);
5042 360180 lbm_value *reserved1 = stack_reserve(ctx, 2);
5043 360180 reserved0[0] = rest_binder;
5044 360180 reserved1[0] = get_car(rest_binder);
5045 360180 reserved1[1] = CLOSURE_ARGS_REST;
5046 }
5047 } else { // No args
5048
2/2
✓ Branch 1 taken 12534637 times.
✓ Branch 2 taken 6 times.
12534643 if (lbm_is_symbol_nil(cl0)) { // No parameters
5049 12534637 stack_drop(ctx, 2);
5050 12534637 ctx->curr_exp = cl1;
5051 12534637 ctx->curr_env = cl2;
5052 } else { // Not enough arguments
5053 6 lbm_set_error_reason((char*)lbm_error_str_num_args);
5054 6 ERROR_AT_CTX(ENC_SYM_EERROR, ctx->r);
5055 }
5056 }
5057 50473637 } break;
5058 252 case ENC_SYM_CONT:{
5059 252 ctx->curr_exp = setup_cont(ctx, args);
5060 252 } break;
5061 18 case ENC_SYM_CONT_SP: {
5062 18 ctx->curr_exp = setup_cont_sp(ctx, args);
5063 18 return;
5064 } break;
5065 30755 case ENC_SYM_MACRO:{
5066 30755 lbm_value env = (lbm_value)sptr[0];
5067 30755 pop_stack_ptr(ctx, 2);
5068 setup_macro(ctx, args, env);
5069 30755 } break;
5070 ✗ default:
5071 ✗ ERROR_CTX(ENC_SYM_EERROR);
5072 }
5073 } else {
5074 12 lbm_set_error_reason(lbm_error_str_not_applicable);
5075 12 ERROR_AT_CTX(ENC_SYM_EERROR, ctx->r);
5076 }
5077 }
5078
5079 30899 static void cont_eval_r(eval_context_t* ctx) {
5080 30899 lbm_value env = ctx->K.data[--ctx->K.sp];
5081 30899 ctx->curr_exp = ctx->r;
5082 30899 ctx->curr_env = env;
5083 30899 }
5084
5085 838873 static void cont_progn_var(eval_context_t* ctx) {
5086
5087 838873 lbm_value key = ctx->K.data[--ctx->K.sp];
5088 838873 lbm_value env = ctx->K.data[--ctx->K.sp];
5089 838873 fill_binding_location(key, ctx->r, env);
5090
5091 838873 ctx->curr_env = env; // evaluating value may build upon local env.
5092 838873 ctx->app_cont = true;
5093 838873 }
5094
5095 3942914 static void cont_setq(eval_context_t *ctx) {
5096 3942914 lbm_value sym = ctx->K.data[--ctx->K.sp];
5097 3942914 lbm_value env = ctx->K.data[--ctx->K.sp];
5098 lbm_value res;
5099
1/4
✗ Branch 2 not taken.
✓ Branch 3 taken 3942839 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
3942914 WITH_GC(res, perform_setvar(sym, ctx->r, env));
5100 3942839 ctx->r = res;
5101 3942839 ctx->app_cont = true;
5102 3942839 }
5103
5104 3557 lbm_flash_status request_flash_storage_cell(lbm_value val, lbm_value *res) {
5105
5106 lbm_value flash_cell;
5107 3557 lbm_flash_status s = lbm_allocate_const_cell(&flash_cell);
5108
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3557 times.
3557 if (s != LBM_FLASH_WRITE_OK)
5109 ✗ return s;
5110 3557 lbm_value new_val = val;
5111 3557 new_val &= ~LBM_PTR_VAL_MASK; // clear the value part of the ptr
5112 3557 new_val |= (flash_cell & LBM_PTR_VAL_MASK);
5113 3557 new_val |= LBM_PTR_TO_CONSTANT_BIT;
5114 3557 *res = new_val;
5115 3557 return s;
5116 }
5117
5118 1080 static void cont_move_to_flash(eval_context_t *ctx) {
5119
5120 1080 lbm_value args = ctx->K.data[--ctx->K.sp];
5121
5122
2/2
✓ Branch 1 taken 468 times.
✓ Branch 2 taken 612 times.
1080 if (lbm_is_symbol_nil(args)) {
5123 // Done looping over arguments. return true.
5124 468 ctx->r = ENC_SYM_TRUE;
5125 468 ctx->app_cont = true;
5126 468 return;
5127 }
5128
5129 lbm_value first_arg, rest;
5130 612 get_car_and_cdr(args, &first_arg, &rest);
5131
5132 lbm_value val;
5133
2/4
✓ Branch 1 taken 612 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 612 times.
✗ Branch 5 not taken.
612 if (lbm_is_symbol(first_arg) && lbm_global_env_lookup(&val, first_arg)) {
5134 // Prepare to copy the rest of the arguments when done with first.
5135 612 lbm_value *rptr = stack_reserve(ctx, 2);
5136 612 rptr[0] = rest;
5137 612 rptr[1] = MOVE_TO_FLASH;
5138
2/2
✓ Branch 1 taken 552 times.
✓ Branch 2 taken 60 times.
612 if (lbm_is_ptr(val) &&
5139
1/2
✓ Branch 0 taken 552 times.
✗ Branch 1 not taken.
552 (!(val & LBM_PTR_TO_CONSTANT_BIT))) {
5140 552 lbm_value * rptr1 = stack_reserve(ctx, 3);
5141 552 rptr1[0] = first_arg;
5142 552 rptr1[1] = SET_GLOBAL_ENV;
5143 552 rptr1[2] = MOVE_VAL_TO_FLASH_DISPATCH;
5144 552 ctx->r = val;
5145 }
5146 612 ctx->app_cont = true;
5147 612 return;
5148 }
5149 ✗ ERROR_CTX(ENC_SYM_EERROR);
5150 }
5151
5152 5164 static void cont_move_val_to_flash_dispatch(eval_context_t *ctx) {
5153
5154 5164 lbm_value val = ctx->r;
5155
5156
2/2
✓ Branch 1 taken 1204 times.
✓ Branch 2 taken 3960 times.
5164 if (lbm_is_cons(val)) { // non-constant cons-cell
5157 1204 lbm_value *rptr = stack_reserve(ctx, 5);
5158 1204 rptr[0] = ENC_SYM_NIL; // fst cell of list
5159 1204 rptr[1] = ENC_SYM_NIL; // last cell of list
5160 1204 rptr[2] = get_cdr(val);
5161 1204 rptr[3] = MOVE_LIST_TO_FLASH;
5162 1204 rptr[4] = MOVE_VAL_TO_FLASH_DISPATCH;
5163 1204 ctx->r = lbm_ref_cell(val)->car; // already checked is_cons
5164 1204 ctx->app_cont = true;
5165 1204 return;
5166 }
5167
5168
3/4
✓ Branch 1 taken 432 times.
✓ Branch 2 taken 3528 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 432 times.
3960 if (lbm_is_ptr(val) && (val & LBM_PTR_TO_CONSTANT_BIT)) { // constant pointer cons or not.
5169 //ctx->r unchanged
5170 ✗ ctx->app_cont = true;
5171 ✗ return;
5172 }
5173
5174
2/2
✓ Branch 1 taken 432 times.
✓ Branch 2 taken 3528 times.
3960 if (lbm_is_ptr(val)) { // something that is not a cons but still a ptr type.
5175 432 lbm_cons_t *ref = lbm_ref_cell(val);
5176
1/2
✓ Branch 1 taken 432 times.
✗ Branch 2 not taken.
432 if (lbm_type_of(ref->cdr) == LBM_TYPE_SYMBOL) {
5177
4/6
✓ Branch 0 taken 243 times.
✓ Branch 1 taken 61 times.
✓ Branch 2 taken 36 times.
✓ Branch 3 taken 92 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
432 switch (ref->cdr) {
5178 243 case ENC_SYM_RAW_I_TYPE: /* fall through */
5179 case ENC_SYM_RAW_U_TYPE:
5180 case ENC_SYM_RAW_F_TYPE: {
5181 243 lbm_value flash_cell = ENC_SYM_NIL;
5182 243 handle_flash_status(request_flash_storage_cell(val, &flash_cell));
5183 243 handle_flash_status(write_const_car(flash_cell, ref->car));
5184 242 handle_flash_status(write_const_cdr(flash_cell, ref->cdr));
5185 242 ctx->r = flash_cell;
5186 242 } break;
5187 61 case ENC_SYM_IND_I_TYPE: /* fall through */
5188 case ENC_SYM_IND_U_TYPE:
5189 case ENC_SYM_IND_F_TYPE: {
5190 #ifndef LBM64
5191 /* 64 bit values are in lbm mem on 32bit platforms. */
5192 61 lbm_uint *lbm_mem_ptr = (lbm_uint*)ref->car;
5193 lbm_uint flash_ptr;
5194
5195 61 handle_flash_status(lbm_write_const_raw(lbm_mem_ptr, 2, &flash_ptr));
5196 61 lbm_value flash_cell = ENC_SYM_NIL;
5197 61 handle_flash_status(request_flash_storage_cell(val, &flash_cell));
5198 61 handle_flash_status(write_const_car(flash_cell, flash_ptr));
5199 61 handle_flash_status(write_const_cdr(flash_cell, ref->cdr));
5200 61 ctx->r = flash_cell;
5201 #else
5202 // There are no indirect types in LBM64
5203 ✗ ERROR_CTX(ENC_SYM_FATAL_ERROR);
5204 #endif
5205 61 } break;
5206 36 case ENC_SYM_LISPARRAY_TYPE: {
5207 36 lbm_array_header_t *arr = (lbm_array_header_t*)ref->car;
5208 36 lbm_uint size = arr->size / sizeof(lbm_uint);
5209 36 lbm_uint flash_addr = 0;
5210 36 lbm_value *arrdata = (lbm_value *)arr->data;
5211 36 lbm_value flash_cell = ENC_SYM_NIL;
5212 36 handle_flash_status(request_flash_storage_cell(val, &flash_cell));
5213 36 handle_flash_status(lbm_allocate_const_raw(size, &flash_addr));
5214 36 lift_array_flash(flash_cell,
5215 false,
5216 (char *)flash_addr,
5217 arr->size);
5218 // Move array contents to flash recursively
5219 36 lbm_value *rptr = stack_reserve(ctx, 5);
5220 36 rptr[0] = flash_cell;
5221 36 rptr[1] = lbm_enc_u(0);
5222 36 rptr[2] = val;
5223 36 rptr[3] = MOVE_ARRAY_ELTS_TO_FLASH;
5224 36 rptr[4] = MOVE_VAL_TO_FLASH_DISPATCH;
5225 36 ctx->r = arrdata[0];
5226 36 ctx->app_cont = true;
5227 36 return;
5228 }
5229 92 case ENC_SYM_ARRAY_TYPE: {
5230 92 lbm_array_header_t *arr = (lbm_array_header_t*)ref->car;
5231 // arbitrary address: flash_arr.
5232 92 lbm_uint flash_arr = 0;
5233 92 handle_flash_status(lbm_write_const_array_padded((uint8_t*)arr->data, arr->size, &flash_arr));
5234 92 lbm_value flash_cell = ENC_SYM_NIL;
5235 92 handle_flash_status(request_flash_storage_cell(val, &flash_cell));
5236 92 lift_array_flash(flash_cell,
5237 true,
5238 (char *)flash_arr,
5239 arr->size);
5240 92 ctx->r = flash_cell;
5241 92 } break;
5242 ✗ case ENC_SYM_CHANNEL_TYPE: /* fall through */
5243 case ENC_SYM_CUSTOM_TYPE:
5244 ✗ lbm_set_error_reason((char *)lbm_error_str_flash_not_possible);
5245 ✗ ERROR_CTX(ENC_SYM_EERROR);
5246 }
5247 } else {
5248 ✗ ERROR_CTX(ENC_SYM_FATAL_ERROR);
5249 }
5250 395 ctx->app_cont = true;
5251 395 return;
5252 }
5253
5254 // if no condition matches, nothing happens (id).
5255 3528 ctx->r = val;
5256 3528 ctx->app_cont = true;
5257 }
5258
5259 3053 static void cont_move_list_to_flash(eval_context_t *ctx) {
5260
5261 // ctx->r holds the value that should go in car
5262
5263 3053 lbm_value *sptr = get_stack_ptr(ctx, 3);
5264
5265 3053 lbm_value fst = sptr[0];
5266 3053 lbm_value lst = sptr[1];
5267 3053 lbm_value val = sptr[2];
5268
5269
5270 3053 lbm_value new_lst = ENC_SYM_NIL;
5271 // Allocate element ptr storage after storing the element to flash.
5272 3053 handle_flash_status(request_flash_storage_cell(lbm_enc_cons_ptr(LBM_PTR_NULL), &new_lst));
5273
5274
2/2
✓ Branch 1 taken 1204 times.
✓ Branch 2 taken 1849 times.
3053 if (lbm_is_symbol_nil(fst)) {
5275 1204 lst = new_lst;
5276 1204 fst = new_lst;
5277 1204 handle_flash_status(write_const_car(lst, ctx->r));
5278 } else {
5279 1849 handle_flash_status(write_const_cdr(lst, new_lst)); // low before high
5280 1849 handle_flash_status(write_const_car(new_lst, ctx->r));
5281 1849 lst = new_lst;
5282 }
5283
5284
2/2
✓ Branch 1 taken 1850 times.
✓ Branch 2 taken 1202 times.
3052 if (lbm_is_cons(val)) {
5285 1850 sptr[0] = fst;
5286 1850 sptr[1] = lst;//rest_cell;
5287 1850 sptr[2] = lbm_ref_cell(val)->cdr;
5288 1850 lbm_value *rptr = stack_reserve(ctx, 2);
5289 1850 rptr[0] = MOVE_LIST_TO_FLASH;
5290 1850 rptr[1] = MOVE_VAL_TO_FLASH_DISPATCH;
5291 1850 ctx->r = lbm_ref_cell(val)->car; // already checked is_cons
5292 } else {
5293 1202 sptr[0] = fst;
5294 1202 sptr[1] = lst;
5295 1202 sptr[2] = CLOSE_LIST_IN_FLASH;
5296 1202 stack_reserve(ctx, 1)[0] = MOVE_VAL_TO_FLASH_DISPATCH;
5297 1202 ctx->r = val;
5298 }
5299 3052 ctx->app_cont = true;
5300 3052 }
5301
5302 1202 static void cont_close_list_in_flash(eval_context_t *ctx) {
5303 1202 lbm_value lst = ctx->K.data[--ctx->K.sp];
5304 1202 lbm_value fst = ctx->K.data[--ctx->K.sp];
5305 1202 lbm_value val = ctx->r;
5306 1202 handle_flash_status(write_const_cdr(lst, val));
5307 1202 ctx->r = fst;
5308 1202 ctx->app_cont = true;
5309 1202 }
5310
5311 108 static void cont_move_array_elts_to_flash(eval_context_t *ctx) {
5312 108 lbm_value *sptr = get_stack_ptr(ctx, 3);
5313 // sptr[2] = source array in RAM
5314 // sptr[1] = current index
5315 // sptr[0] = target array in flash
5316 108 lbm_array_header_t *src_arr = assume_array(sptr[2]);
5317 108 lbm_uint size = src_arr->size / sizeof(lbm_uint);
5318 108 lbm_value *srcdata = (lbm_value *)src_arr->data;
5319
5320 108 lbm_array_header_t *tgt_arr = assume_array(sptr[0]);
5321 108 lbm_uint *tgtdata = (lbm_value *)tgt_arr->data;
5322 108 lbm_uint ix = lbm_dec_as_u32(sptr[1]);
5323 108 handle_flash_status(lbm_const_write(&tgtdata[ix], ctx->r));
5324
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 72 times.
108 if (ix >= size-1) {
5325 36 ctx->r = sptr[0];
5326 36 stack_drop(ctx, 3);
5327 36 ctx->app_cont = true;
5328 36 return;
5329 }
5330 72 sptr[1] = lbm_enc_u(ix + 1);
5331 72 lbm_value *rptr = stack_reserve(ctx, 2);
5332 72 rptr[0] = MOVE_ARRAY_ELTS_TO_FLASH;
5333 72 rptr[1] = MOVE_VAL_TO_FLASH_DISPATCH;
5334 72 ctx->r = srcdata[ix+1];
5335 72 ctx->app_cont = true;
5336 72 return;
5337 }
5338
5339 6671 static void cont_qq_expand_start(eval_context_t *ctx) {
5340 6671 lbm_value *rptr = stack_reserve(ctx, 2);
5341 6671 rptr[0] = ctx->r;
5342 6671 rptr[1] = QQ_EXPAND;
5343 6671 ctx->r = ENC_SYM_NIL;
5344 6671 ctx->app_cont = true;
5345 6671 }
5346
5347 13994 static lbm_value quote_it(lbm_value qquoted) {
5348
3/4
✓ Branch 1 taken 13379 times.
✓ Branch 2 taken 615 times.
✓ Branch 3 taken 13379 times.
✗ Branch 4 not taken.
27373 if (lbm_is_symbol(qquoted) &&
5349 26758 lbm_is_special(qquoted)) return qquoted;
5350
5351 615 lbm_value val = cons_with_gc(qquoted, ENC_SYM_NIL, ENC_SYM_NIL);
5352 615 return cons_with_gc(ENC_SYM_QUOTE, val, ENC_SYM_NIL);
5353 }
5354
5355 50743 static bool is_append(lbm_value a) {
5356
1/2
✓ Branch 1 taken 50503 times.
✗ Branch 2 not taken.
101246 return (lbm_is_cons(a) &&
5357
2/2
✓ Branch 0 taken 50503 times.
✓ Branch 1 taken 240 times.
101246 lbm_is_symbol(lbm_ref_cell(a)->car) &&
5358
2/2
✓ Branch 1 taken 24891 times.
✓ Branch 2 taken 25612 times.
50503 (lbm_ref_cell(a)->car == ENC_SYM_APPEND));
5359 }
5360
5361 85827 static lbm_value append(lbm_value front, lbm_value back) {
5362
2/2
✓ Branch 1 taken 46133 times.
✓ Branch 2 taken 39694 times.
85827 if (lbm_is_symbol_nil(front)) return back;
5363
2/2
✓ Branch 1 taken 13379 times.
✓ Branch 2 taken 26315 times.
39694 if (lbm_is_symbol_nil(back)) return front;
5364
5365
4/4
✓ Branch 1 taken 14090 times.
✓ Branch 2 taken 12225 times.
✓ Branch 3 taken 615 times.
✓ Branch 4 taken 13475 times.
40405 if (lbm_is_quoted_list(front) &&
5366 14090 lbm_is_quoted_list(back)) {
5367 615 lbm_value f = get_cadr(front);
5368 615 lbm_value b = get_cadr(back);
5369 615 return quote_it(lbm_list_append(f, b));
5370 }
5371
5372
4/4
✓ Branch 1 taken 12774 times.
✓ Branch 2 taken 12926 times.
✓ Branch 3 taken 663 times.
✓ Branch 4 taken 12111 times.
38474 if (is_append(back) &&
5373
2/2
✓ Branch 2 taken 657 times.
✓ Branch 3 taken 6 times.
13437 lbm_is_quoted_list(get_cadr(back)) &&
5374 663 lbm_is_quoted_list(front)) {
5375 657 lbm_value ql = get_cadr(back);
5376 657 lbm_value f = get_cadr(front);
5377 657 lbm_value b = get_cadr(ql);
5378
5379 657 lbm_value v = lbm_list_append(f, b);
5380 657 lbm_set_car(get_cdr(ql), v);
5381 657 return back;
5382 }
5383
5384
2/2
✓ Branch 1 taken 12117 times.
✓ Branch 2 taken 12926 times.
25043 if (is_append(back)) {
5385 12117 back = get_cdr(back);
5386 12117 lbm_value new = cons_with_gc(front, back, ENC_SYM_NIL);
5387 12117 return cons_with_gc(ENC_SYM_APPEND, new, ENC_SYM_NIL);
5388 }
5389
5390 lbm_value t0, t1;
5391
5392 12926 t0 = cons_with_gc(back, ENC_SYM_NIL, front);
5393 12926 t1 = cons_with_gc(front, t0, ENC_SYM_NIL);
5394 12926 return cons_with_gc(ENC_SYM_APPEND, t1, ENC_SYM_NIL);
5395 }
5396
5397 // ////////////////////////////////////////////////////////////
5398 // Quasiquotation expansion that takes place at read time
5399 // and is based on the paper by Bawden "Quasiquotation in lisp".
5400 // Bawden, Alan. "Quasiquotation in Lisp." PEPM. 1999.
5401 //
5402 // cont_qq_expand and cont_qq_expand_list corresponds (mostly) to
5403 // qq-expand and qq-expand-list in the paper.
5404 // One difference is that the case where a backquote is nested
5405 // inside of a backqoute is handled via the recursion through the
5406 // reader.
5407
5408 /* Bawden's qq-expand implementation
5409 (define (qq-expand x)
5410 (cond ((tag-comma? x)
5411 (tag-data x))
5412 ((tag-comma-atsign? x)
5413 (error "Illegal"))
5414 ((tag-backquote? x)
5415 (qq-expand
5416 (qq-expand (tag-data x))))
5417 ((pair? x)
5418 `(append
5419 ,(qq-expand-list (car x))
5420 ,(qq-expand (cdr x))))
5421 (else `',x)))
5422 */
5423 46365 static void cont_qq_expand(eval_context_t *ctx) {
5424 46365 lbm_value qquoted = ctx->K.data[--ctx->K.sp];
5425
5426
2/2
✓ Branch 1 taken 32986 times.
✓ Branch 2 taken 13379 times.
46365 switch(lbm_type_of(qquoted)) {
5427 32986 case LBM_TYPE_CONS: {
5428 lbm_value car_val;
5429 lbm_value cdr_val;
5430 32986 get_car_and_cdr(qquoted, &car_val, &cdr_val);
5431
5432
2/2
✓ Branch 1 taken 6963 times.
✓ Branch 2 taken 26023 times.
32986 if (lbm_type_of(car_val) == LBM_TYPE_SYMBOL &&
5433
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 6927 times.
6963 car_val == ENC_SYM_COMMA) {
5434 36 ctx->r = append(ctx->r, get_car(cdr_val));
5435 36 ctx->app_cont = true;
5436
2/2
✓ Branch 1 taken 6927 times.
✓ Branch 2 taken 26023 times.
32950 } else if (lbm_type_of(car_val) == LBM_TYPE_SYMBOL &&
5437
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6924 times.
6927 car_val == ENC_SYM_COMMAAT) {
5438 3 lbm_set_error_reason((char*)lbm_error_str_qq_expand);
5439 3 ERROR_AT_CTX(ENC_SYM_RERROR, qquoted);
5440 } else {
5441 32947 lbm_value *rptr = stack_reserve(ctx, 6);
5442 32947 rptr[0] = ctx->r;
5443 32947 rptr[1] = QQ_APPEND;
5444 32947 rptr[2] = cdr_val;
5445 32947 rptr[3] = QQ_EXPAND;
5446 32947 rptr[4] = car_val;
5447 32947 rptr[5] = QQ_EXPAND_LIST;
5448 32947 ctx->app_cont = true;
5449 32947 ctx->r = ENC_SYM_NIL;
5450 }
5451
5452 32983 } break;
5453 13379 default: {
5454 13379 lbm_value res = quote_it(qquoted);
5455 13379 ctx->r = append(ctx->r, res);
5456 13379 ctx->app_cont = true;
5457 }
5458 }
5459 46362 }
5460
5461 39694 static void cont_qq_append(eval_context_t *ctx) {
5462 39694 lbm_value head = ctx->K.data[--ctx->K.sp];
5463 39694 ctx->r = append(head, ctx->r);
5464 39694 ctx->app_cont = true;
5465 39694 }
5466
5467 /* Bawden's qq-expand-list implementation
5468 (define (qq-expand-list x)
5469 (cond ((tag-comma? x)
5470 `(list ,(tag-data x)))
5471 ((tag-comma-atsign? x)
5472 (tag-data x))
5473 ((tag-backquote? x)
5474 (qq-expand-list
5475 (qq-expand (tag-data x))))
5476 ((pair? x)
5477 `(list
5478 (append
5479 ,(qq-expand-list (car x))
5480 ,(qq-expand (cdr x)))))
5481 (else `'(,x))))
5482 */
5483
5484 39694 static void cont_qq_expand_list(eval_context_t* ctx) {
5485 39694 lbm_value l = ctx->K.data[--ctx->K.sp];
5486
5487 39694 ctx->app_cont = true;
5488
2/2
✓ Branch 1 taken 24967 times.
✓ Branch 2 taken 14727 times.
39694 switch(lbm_type_of(l)) {
5489 24967 case LBM_TYPE_CONS: {
5490 lbm_value car_val;
5491 lbm_value cdr_val;
5492 24967 get_car_and_cdr(l, &car_val, &cdr_val);
5493
5494
2/2
✓ Branch 1 taken 24961 times.
✓ Branch 2 taken 6 times.
24967 if (lbm_type_of(car_val) == LBM_TYPE_SYMBOL &&
5495
2/2
✓ Branch 0 taken 17991 times.
✓ Branch 1 taken 6970 times.
24961 car_val == ENC_SYM_COMMA) {
5496 17991 lbm_value tl = cons_with_gc(get_car(cdr_val), ENC_SYM_NIL, ENC_SYM_NIL);
5497 17991 lbm_value tmp = cons_with_gc(ENC_SYM_LIST, tl, ENC_SYM_NIL);
5498 17991 ctx->r = append(ctx->r, tmp);
5499 18220 return;
5500
2/2
✓ Branch 1 taken 6970 times.
✓ Branch 2 taken 6 times.
6976 } else if (lbm_type_of(car_val) == LBM_TYPE_SYMBOL &&
5501
2/2
✓ Branch 0 taken 229 times.
✓ Branch 1 taken 6741 times.
6970 car_val == ENC_SYM_COMMAAT) {
5502 229 lbm_value cadr_val = get_car(cdr_val);
5503 229 ctx->r = cadr_val;
5504 229 return;
5505 } else {
5506 6747 lbm_value *rptr = stack_reserve(ctx, 7);
5507 6747 rptr[0] = QQ_LIST;
5508 6747 rptr[1] = ctx->r;
5509 6747 rptr[2] = QQ_APPEND;
5510 6747 rptr[3] = cdr_val;
5511 6747 rptr[4] = QQ_EXPAND;
5512 6747 rptr[5] = car_val;
5513 6747 rptr[6] = QQ_EXPAND_LIST;
5514 6747 ctx->r = ENC_SYM_NIL;
5515 }
5516
5517 6747 } break;
5518 14727 default: {
5519 14727 lbm_value a_list = cons_with_gc(l, ENC_SYM_NIL, ENC_SYM_NIL);
5520 14727 lbm_value tl = cons_with_gc(a_list, ENC_SYM_NIL, ENC_SYM_NIL);
5521 14727 lbm_value tmp = cons_with_gc(ENC_SYM_QUOTE, tl, ENC_SYM_NIL);
5522 14727 ctx->r = append(ctx->r, tmp);
5523 }
5524 }
5525 }
5526
5527 6747 static void cont_qq_list(eval_context_t *ctx) {
5528 6747 lbm_value val = ctx->r;
5529 6747 lbm_value apnd_app = cons_with_gc(val, ENC_SYM_NIL, ENC_SYM_NIL);
5530 6747 lbm_value tmp = cons_with_gc(ENC_SYM_LIST, apnd_app, ENC_SYM_NIL);
5531 6747 ctx->r = tmp;
5532 6747 ctx->app_cont = true;
5533 6747 }
5534
5535 144 static void cont_kill(eval_context_t *ctx) {
5536 (void) ctx;
5537 144 ok_ctx();
5538 144 }
5539
5540 // cont_pop_reader_flags
5541 // s[sp-1] flags
5542 // Preserves r
5543 187290 static void cont_pop_reader_flags(eval_context_t *ctx) {
5544 187290 lbm_value flags = ctx->K.data[--ctx->K.sp];
5545 187290 lbm_value stream = ctx->K.data[--ctx->K.sp];
5546 187290 ctx->reader_stream = stream;
5547 187290 ctx->flags = ctx->flags & ~EVAL_CPS_CONTEXT_READER_FLAGS_MASK;
5548 187290 ctx->flags |= (lbm_dec_as_u32(flags) & EVAL_CPS_CONTEXT_READER_FLAGS_MASK);
5549 187290 ctx->app_cont = true;
5550 187290 }
5551
5552 // cont_exception_handler
5553 //
5554 // s[sp-2] retval - a list of 2 elements created by eval_trap
5555 // s[sp-1] flags - context flags stored by eval_trap
5556 //
5557 12631 static void cont_exception_handler(eval_context_t *ctx) {
5558 12631 lbm_value *sptr = pop_stack_ptr(ctx, 2);
5559 12631 lbm_value retval = sptr[0];
5560 12631 lbm_value flags = sptr[1];
5561 12631 lbm_set_car(get_cdr(retval), ctx->r);
5562 12631 ctx->flags = (uint32_t)flags;
5563 12631 ctx->r = retval;
5564 12631 ctx->app_cont = true;
5565 12631 }
5566
5567 // cont_recv_to:
5568 //
5569 // s[sp-1] = patterns
5570 //
5571 // ctx->r = timeout value
5572 252 static void cont_recv_to(eval_context_t *ctx) {
5573
1/2
✓ Branch 1 taken 252 times.
✗ Branch 2 not taken.
252 if (lbm_is_number(ctx->r)) {
5574 252 lbm_value *sptr = get_stack_ptr(ctx, 1); // patterns at sptr[0]
5575 252 float timeout_time = lbm_dec_as_float(ctx->r);
5576
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 216 times.
252 if (timeout_time < 0.0) timeout_time = 0.0; // clamp.
5577
2/2
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 180 times.
252 if (ctx->num_mail > 0) {
5578 lbm_value e;
5579 72 lbm_value new_env = ctx->curr_env;
5580 72 int n = find_match(sptr[0], ctx->mailbox, ctx->num_mail, &e, &new_env);
5581
1/2
✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
72 if (n >= 0) { // match
5582 72 mailbox_remove_mail(ctx, (lbm_uint)n);
5583 72 ctx->curr_env = new_env;
5584 72 ctx->curr_exp = e;
5585 72 stack_drop(ctx, 1);
5586 72 return;
5587 }
5588 }
5589 // If no mail or no match, go to sleep
5590 180 lbm_uint *rptr = stack_reserve(ctx,2);
5591 180 rptr[0] = ctx->r;
5592 180 rptr[1] = RECV_TO_RETRY;
5593 180 block_current_ctx(LBM_THREAD_STATE_RECV_TO,S_TO_US(timeout_time),true);
5594 } else {
5595 ✗ ERROR_CTX(ENC_SYM_TERROR);
5596 }
5597 }
5598
5599 // cont_recv_to_retry
5600 //
5601 // s[sp-2] = patterns
5602 // s[sp-1] = timeout value
5603 //
5604 // ctx->r = nonsense | timeout symbol
5605 180 static void cont_recv_to_retry(eval_context_t *ctx) {
5606 180 lbm_value *sptr = get_stack_ptr(ctx, 2); //sptr[0] = patterns, sptr[1] = timeout
5607
5608
1/2
✓ Branch 0 taken 180 times.
✗ Branch 1 not taken.
180 if (ctx->num_mail > 0) {
5609 lbm_value e;
5610 180 lbm_value new_env = ctx->curr_env;
5611 180 int n = find_match(sptr[0], ctx->mailbox, ctx->num_mail, &e, &new_env);
5612
2/2
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 108 times.
180 if (n >= 0) { // match
5613 72 mailbox_remove_mail(ctx, (lbm_uint)n);
5614 72 ctx->curr_env = new_env;
5615 72 ctx->curr_exp = e;
5616 72 stack_drop(ctx, 2);
5617 72 return;
5618 }
5619 }
5620
5621 // No message matched but the timeout was reached.
5622 // This is like having a recv-to with no case that matches
5623 // the timeout symbol.
5624
1/2
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
108 if (ctx->r == ENC_SYM_TIMEOUT) {
5625 108 stack_drop(ctx, 2);
5626 108 ctx->app_cont = true;
5627 108 return;
5628 }
5629
5630 ✗ stack_reserve(ctx,1)[0] = RECV_TO_RETRY;
5631 ✗ reblock_current_ctx(LBM_THREAD_STATE_RECV_TO,true);
5632 }
5633
5634
5635 /*********************************************************/
5636 /* Continuations table */
5637 typedef void (*cont_fun)(eval_context_t *);
5638
5639 static const cont_fun continuations[NUM_CONTINUATIONS] =
5640 { advance_ctx, // CONT_DONE
5641 cont_set_global_env,
5642 cont_bind_to_key_rest,
5643 cont_if,
5644 cont_progn_rest,
5645 cont_application_args,
5646 cont_and,
5647 cont_or,
5648 cont_wait,
5649 cont_match,
5650 cont_application_start,
5651 cont_eval_r,
5652 cont_resume,
5653 cont_closure_application_args,
5654 cont_exit_atomic,
5655 cont_read_next_token,
5656 cont_read_append_continue,
5657 cont_read_eval_continue,
5658 cont_read_expect_closepar,
5659 cont_read_dot_terminate,
5660 cont_read_done,
5661 cont_read_start_bytearray,
5662 cont_read_append_bytearray,
5663 cont_map,
5664 cont_match_guard,
5665 cont_terminate,
5666 cont_progn_var,
5667 cont_setq,
5668 cont_move_to_flash,
5669 cont_move_val_to_flash_dispatch,
5670 cont_move_list_to_flash,
5671 cont_close_list_in_flash,
5672 cont_qq_expand_start,
5673 cont_qq_expand,
5674 cont_qq_append,
5675 cont_qq_expand_list,
5676 cont_qq_list,
5677 cont_kill,
5678 cont_loop,
5679 cont_loop_condition,
5680 cont_merge_rest,
5681 cont_merge_layer,
5682 cont_closure_args_rest,
5683 cont_move_array_elts_to_flash,
5684 cont_pop_reader_flags,
5685 cont_exception_handler,
5686 cont_recv_to,
5687 cont_wrap_result,
5688 cont_recv_to_retry,
5689 cont_read_start_array,
5690 cont_read_append_array,
5691 cont_loop_env_prep,
5692 cont_read_next_token_grab_row,
5693 };
5694
5695 /*********************************************************/
5696 /* Evaluators lookup table (special forms) */
5697 typedef void (*evaluator_fun)(eval_context_t *);
5698
5699 static const evaluator_fun evaluators[] =
5700 {
5701 eval_quote,
5702 eval_define,
5703 eval_progn,
5704 eval_lambda,
5705 eval_if,
5706 eval_let,
5707 eval_and,
5708 eval_or,
5709 eval_match,
5710 eval_receive,
5711 eval_receive_timeout,
5712 eval_callcc,
5713 eval_atomic,
5714 eval_selfevaluating, // macro
5715 eval_selfevaluating, // cont
5716 eval_selfevaluating, // closure
5717 eval_cond,
5718 eval_app_cont,
5719 eval_var,
5720 eval_setq,
5721 eval_move_to_flash,
5722 eval_loop,
5723 eval_trap,
5724 eval_call_cc_unsafe,
5725 eval_selfevaluating, // cont_sp
5726 };
5727
5728
5729 /*********************************************************/
5730 /* Evaluator step function */
5731
5732 2688640513 static void evaluation_step(void){
5733 2688640513 eval_context_t *ctx = ctx_running;
5734
5735
2/2
✓ Branch 0 taken 1288218916 times.
✓ Branch 1 taken 1400421597 times.
2688640513 if (ctx->app_cont) {
5736 1288218916 lbm_value k = ctx->K.data[--ctx->K.sp];
5737 1288218916 ctx->app_cont = false;
5738
5739 // If app_cont is true, then top of stack must be a valid continuation!
5740 // If top of stack is not a valid continuation CRASH!
5741
5742 1288218916 lbm_uint decoded_k = DEC_CONTINUATION(k);
5743 1288218916 continuations[decoded_k](ctx);
5744 1288206742 return;
5745 }
5746
5747
2/2
✓ Branch 1 taken 595584956 times.
✓ Branch 2 taken 804836641 times.
1400421597 if (lbm_is_symbol(ctx->curr_exp)) {
5748 595584956 eval_symbol(ctx);
5749 595584776 return;
5750 }
5751
2/2
✓ Branch 1 taken 475564478 times.
✓ Branch 2 taken 329272163 times.
804836641 if (lbm_is_cons(ctx->curr_exp)) {
5752 475564478 lbm_cons_t *cell = lbm_ref_cell(ctx->curr_exp);
5753 475564478 lbm_value h = cell->car;
5754
4/4
✓ Branch 1 taken 475554875 times.
✓ Branch 2 taken 9603 times.
✓ Branch 3 taken 121127635 times.
✓ Branch 4 taken 354427240 times.
475564478 if (lbm_is_symbol(h) && ((h & ENC_SPECIAL_FORMS_MASK) == ENC_SPECIAL_FORMS_BIT)) {
5755 121127635 lbm_uint eval_index = lbm_dec_sym(h) & SPECIAL_FORMS_INDEX_MASK;
5756 121127635 evaluators[eval_index](ctx);
5757 121127464 return;
5758 }
5759 /*
5760 * At this point head can be anything. It should evaluate
5761 * into a form that can be applied (closure, symbol, ...) though.
5762 */
5763 354436843 lbm_value *reserved = stack_reserve(ctx, 3);
5764 354436834 reserved[0] = ctx->curr_env; // INFER: stack_reserve aborts context if error.
5765 354436834 reserved[1] = cell->cdr;
5766 354436834 reserved[2] = APPLICATION_START;
5767 354436834 ctx->curr_exp = h; // evaluate the function
5768 354436834 return;
5769 }
5770
5771 329272163 eval_selfevaluating(ctx);
5772 329272164 return;
5773 }
5774
5775 // Placed down here since it depends on a lot of things.
5776 // (apply fun arg-list)
5777 7313679 static void apply_apply(lbm_value *args, lbm_uint nargs, eval_context_t *ctx) {
5778
4/4
✓ Branch 0 taken 7313673 times.
✓ Branch 1 taken 6 times.
✓ Branch 3 taken 7313670 times.
✓ Branch 4 taken 3 times.
7313679 if (nargs == 2 && lbm_is_list(args[1])) {
5779 7313670 lbm_value fun = args[0];
5780 7313670 lbm_value arg_list = args[1];
5781
5782 7313670 stack_drop(ctx, (unsigned int)nargs+1);
5783
5784
2/2
✓ Branch 1 taken 833082 times.
✓ Branch 2 taken 6480588 times.
7313670 if (lbm_is_symbol(fun)) {
5785
2/2
✓ Branch 0 taken 684 times.
✓ Branch 1 taken 832398 times.
833082 if ((fun & ENC_SPECIAL_FORMS_MASK) == ENC_SPECIAL_FORMS_BIT) {
5786 // Since the special form evaluators are responsible for conditionally
5787 // evaluating their arguments there is no easy way to prevent them
5788 // evaluating their arguments. Therefore we compromise and allow them to do
5789 // so, even if this isn't always how you would expect apply to work.
5790 // For instance, `(apply and '(a))` would try evaluating the symbol `a`,
5791 // instead of just returning the symbol `a` outright.
5792
5793 // Evaluator functions expect the current expression to equal the special
5794 // form, i.e. including the function symbol.
5795 684 lbm_value fun_and_args = cons_with_gc(fun, arg_list, ENC_SYM_NIL);
5796 684 ctx->curr_exp = fun_and_args;
5797 684 lbm_uint eval_index = lbm_dec_sym(fun) & SPECIAL_FORMS_INDEX_MASK;
5798 684 evaluators[eval_index](ctx);
5799 684 return;
5800 } else { // lbm_is_symbol(fun)
5801 832398 stack_reserve(ctx, 1)[0] = fun;
5802 832398 unsigned int arg_count = 0;
5803
2/2
✓ Branch 1 taken 7724447 times.
✓ Branch 2 taken 832398 times.
8556845 for (lbm_value current = arg_list; lbm_is_cons(current);) {
5804 7724447 lbm_cons_t *cell = lbm_ref_cell(current);
5805 7724447 stack_reserve(ctx, 1)[0] = cell->car;
5806 7724447 arg_count++;
5807 7724447 current = cell->cdr;
5808 }
5809 832398 lbm_value *fun_and_args = get_stack_ptr(ctx, arg_count + 1);
5810 832398 application(ctx, fun_and_args, arg_count);
5811 832398 return;
5812 }
5813
2/2
✓ Branch 1 taken 6480582 times.
✓ Branch 2 taken 6 times.
6480588 } else if (lbm_is_cons(fun)) {
5814 6480582 lbm_cons_t *fun_cell = lbm_ref_cell(fun);
5815
5/5
✓ Branch 0 taken 6480219 times.
✓ Branch 1 taken 108 times.
✓ Branch 2 taken 108 times.
✓ Branch 3 taken 144 times.
✓ Branch 4 taken 3 times.
6480582 switch (fun_cell->car) {
5816 6480219 case ENC_SYM_CLOSURE: {
5817 6480219 lbm_value closure = fun_cell->cdr;
5818 lbm_value cl0, cl1, cl2;
5819
1/2
✓ Branch 1 taken 6480219 times.
✗ Branch 2 not taken.
6480219 EXTRACT(closure, cl0); // CLO_PARAMS
5820
1/2
✓ Branch 1 taken 6480219 times.
✗ Branch 2 not taken.
6480219 EXTRACT(closure, cl1); // CLO_BODY
5821
2/2
✓ Branch 1 taken 6480183 times.
✓ Branch 2 taken 36 times.
6480219 EXTRACT_NO_ADVANCE(closure, cl2); // CLO_ENV
5822
5823 // Only placed here to protect from GC. Will be overriden later.
5824 // ctx->r = arg_list; // Should already be placed there.
5825 6480219 ctx->curr_exp = fun;
5826
5827 6480219 lbm_value env = cl2;
5828
5829 6480219 lbm_value current_params = cl0;
5830 6480219 lbm_value current_args = arg_list;
5831
4/4
✓ Branch 1 taken 61200186 times.
✓ Branch 2 taken 6480216 times.
✓ Branch 4 taken 61200183 times.
✓ Branch 5 taken 3 times.
67680402 while (lbm_is_cons(current_params) && lbm_is_cons(current_args)) {
5832 61200183 lbm_cons_t *p_cell = lbm_ref_cell(current_params);
5833 61200183 lbm_cons_t *a_cell = lbm_ref_cell(current_args);
5834 61200183 env = allocate_binding(p_cell->car, a_cell->car, env);
5835 61200183 current_params = p_cell->cdr;
5836 61200183 current_args = a_cell->cdr;
5837 }
5838
2/2
✓ Branch 1 taken 360108 times.
✓ Branch 2 taken 6120111 times.
6480219 if (lbm_is_cons(current_args)) {
5839 360108 env = allocate_binding(ENC_SYM_REST_ARGS, current_args, env);
5840
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 6120108 times.
6120111 } else if (lbm_is_cons(current_params)) {
5841 3 lbm_set_error_reason(lbm_error_str_num_args);
5842 3 ERROR_AT_CTX(ENC_SYM_EERROR, fun);
5843 }
5844
5845 6480216 ctx->curr_env = env;
5846 6480216 ctx->curr_exp = cl1;
5847 6480216 return;
5848 }
5849 108 case ENC_SYM_CONT:
5850 108 ctx->r = fun;
5851 108 ctx->r = setup_cont(ctx, arg_list);
5852 108 ctx->app_cont = true;
5853 108 return;
5854 108 case ENC_SYM_CONT_SP:
5855 108 ctx->r = fun;
5856 108 ctx->r = setup_cont_sp(ctx, arg_list);
5857 108 ctx->app_cont = true;
5858 108 return;
5859 144 case ENC_SYM_MACRO:
5860 144 ctx->r = fun;
5861 144 setup_macro(ctx, arg_list, ctx->curr_env);
5862 144 return;
5863 3 default:
5864 3 lbm_set_error_reason(lbm_error_str_not_applicable);
5865 3 ERROR_AT_CTX(ENC_SYM_EERROR, fun);
5866 }
5867 } else {
5868 6 lbm_set_error_reason(lbm_error_str_not_applicable);
5869 6 ERROR_AT_CTX(ENC_SYM_EERROR, fun);
5870 }
5871 } else {
5872 9 lbm_set_error_reason(lbm_error_str_incorrect_arg);
5873 9 ERROR_AT_CTX(ENC_SYM_EERROR, ENC_SYM_APPLY);
5874 }
5875 }
5876
5877 // Reset has a built in pause.
5878 // so after reset, continue.
5879 8 void lbm_reset_eval(void) {
5880 8 eval_cps_next_state_arg = 0;
5881 8 eval_cps_next_state = EVAL_CPS_STATE_RESET;
5882
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (eval_cps_next_state != eval_cps_run_state) eval_cps_state_changed = true;
5883 8 }
5884
5885 28439 void lbm_pause_eval(void ) {
5886 28439 eval_cps_next_state_arg = 0;
5887 28439 eval_cps_next_state = EVAL_CPS_STATE_PAUSED;
5888
1/2
✓ Branch 0 taken 28439 times.
✗ Branch 1 not taken.
28439 if (eval_cps_next_state != eval_cps_run_state) eval_cps_state_changed = true;
5889 28439 }
5890
5891 29242 void lbm_pause_eval_with_gc(uint32_t num_free) {
5892 29242 eval_cps_next_state_arg = num_free;
5893 29242 eval_cps_next_state = EVAL_CPS_STATE_PAUSED;
5894
1/2
✓ Branch 0 taken 29242 times.
✗ Branch 1 not taken.
29242 if (eval_cps_next_state != eval_cps_run_state) eval_cps_state_changed = true;
5895 29242 }
5896
5897 29257 void lbm_continue_eval(void) {
5898 29257 eval_cps_next_state = EVAL_CPS_STATE_RUNNING;
5899
1/2
✓ Branch 0 taken 29257 times.
✗ Branch 1 not taken.
29257 if (eval_cps_next_state != eval_cps_run_state) eval_cps_state_changed = true;
5900 29257 }
5901
5902 898 void lbm_kill_eval(void) {
5903 898 eval_cps_next_state = EVAL_CPS_STATE_KILL;
5904
1/2
✓ Branch 0 taken 898 times.
✗ Branch 1 not taken.
898 if (eval_cps_next_state != eval_cps_run_state) eval_cps_state_changed = true;
5905 898 }
5906
5907 186435 uint32_t lbm_get_eval_state(void) {
5908 186435 return eval_cps_run_state;
5909 }
5910
5911 // Only unblocks threads that are unblockable.
5912 // A sleeping thread is not unblockable.
5913 108 static void handle_event_unblock_ctx(lbm_cid cid, lbm_value v) {
5914 108 eval_context_t *found = NULL;
5915 108 lbm_mutex_lock(&qmutex);
5916
5917 108 found = lookup_ctx_nm(&blocked, cid);
5918
2/4
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 108 times.
✗ Branch 3 not taken.
108 if (found && LBM_IS_STATE_UNBLOCKABLE(found->state)){
5919 108 unlink_ctx_nm(&blocked,found);
5920
2/2
✓ Branch 1 taken 36 times.
✓ Branch 2 taken 72 times.
108 if (lbm_is_error(v)) {
5921 36 get_stack_ptr(found, 1)[0] = TERMINATE; // replace TOS
5922 36 found->app_cont = true;
5923 }
5924 108 found->r = v;
5925 108 found->state = LBM_THREAD_STATE_READY;
5926 108 enqueue_ctx_nm(&queue,found);
5927 }
5928 108 lbm_mutex_unlock(&qmutex);
5929 108 }
5930
5931 5 static void handle_event_define(lbm_value key, lbm_value val) {
5932 5 lbm_uint dec_key = lbm_dec_sym(key);
5933 5 lbm_uint ix_key = dec_key & GLOBAL_ENV_MASK;
5934 5 lbm_value *global_env = lbm_get_global_env();
5935 5 lbm_uint orig_env = global_env[ix_key];
5936 lbm_value new_env;
5937 // A key is a symbol and should not need to be remembered.
5938
1/4
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
5 WITH_GC(new_env, lbm_env_set(orig_env,key,val));
5939
5940 5 global_env[ix_key] = new_env;
5941 5 }
5942
5943 4424 static lbm_value get_event_value(lbm_event_t *e) {
5944 lbm_value v;
5945
2/2
✓ Branch 0 taken 4397 times.
✓ Branch 1 taken 27 times.
4424 if (e->buf_len > 0) {
5946 lbm_flat_value_t fv;
5947 4397 fv.buf = (uint8_t*)e->buf_ptr;
5948 4397 fv.buf_size = e->buf_len;
5949 4397 fv.buf_pos = 0;
5950 4397 lbm_unflatten_value(&fv, &v);
5951 // Free the flat value buffer. GC is unaware of its existence.
5952 4397 lbm_free(fv.buf);
5953 } else {
5954 27 v = (lbm_value)e->buf_ptr;
5955 }
5956 4424 return v;
5957 }
5958
5959 // In a scenario where C is enqueuing events and other LBM threads
5960 // are sendind mail to event handler concurrently, old events will
5961 // be dropped as the backpressure mechanism wont detect this scenario.
5962 // TODO: Low prio pondering on robust solutions.
5963 267732623 static void process_events(void) {
5964
5965
2/2
✓ Branch 0 taken 2367969 times.
✓ Branch 1 taken 265364654 times.
267732623 if (!lbm_events) {
5966 2367969 return;
5967 }
5968
5969 lbm_event_t e;
5970
2/2
✓ Branch 1 taken 4424 times.
✓ Branch 2 taken 265364654 times.
265369078 while (lbm_event_pop(&e)) {
5971 4424 lbm_value event_val = get_event_value(&e);
5972
3/4
✓ Branch 0 taken 108 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 4311 times.
✗ Branch 3 not taken.
4424 switch(e.type) {
5973 108 case LBM_EVENT_UNBLOCK_CTX:
5974 108 handle_event_unblock_ctx((lbm_cid)e.parameter, event_val);
5975 108 break;
5976 5 case LBM_EVENT_DEFINE:
5977 5 handle_event_define((lbm_value)e.parameter, event_val);
5978 5 break;
5979 4311 case LBM_EVENT_FOR_HANDLER:
5980
1/2
✓ Branch 0 taken 4311 times.
✗ Branch 1 not taken.
4311 if (lbm_event_handler_pid >= 0) {
5981 //If multiple events for handler, this is wasteful!
5982 // TODO: Find the event_handler once and send all mails.
5983 // However, do it with as little new code as possible.
5984 4311 lbm_find_receiver_and_send(lbm_event_handler_pid, event_val);
5985 }
5986 4311 break;
5987 }
5988 }
5989 }
5990
5991 29346 void lbm_add_eval_symbols(void) {
5992 29346 lbm_uint x = 0;
5993 29346 lbm_uint y = 0;
5994 29346 lbm_add_symbol("x", &x);
5995 29346 lbm_add_symbol("y", &y);
5996 29346 symbol_x = lbm_enc_sym(x);
5997 29346 symbol_y = lbm_enc_sym(y);
5998 29346 }
5999
6000
6001
6002 #ifdef LBM_SINGLE_THREADED
6003 bool lbm_eval_step(int n) {
6004 if (setjmp(error_jmp_buf) > 0) { return false; }
6005
6006 bool busy = false;
6007
6008 if (ctx_running) {
6009 busy = true;
6010 while (n > 0 && ctx_running) {
6011 evaluation_step();
6012 n--;
6013 }
6014 if (ctx_running) {
6015 enqueue_ctx_nm(&queue, ctx_running);
6016 ctx_running = NULL;
6017 }
6018 } else {
6019 if (gc_requested) gc();
6020 process_events();
6021 lbm_mutex_lock(&qmutex);
6022 wake_up_ctxs_nm();
6023 ctx_running = dequeue_ctx_nm(&queue);
6024 lbm_mutex_unlock(&qmutex);
6025 }
6026 return busy;
6027 }
6028
6029 bool lbm_eval_init(void) {
6030 blocked.first = NULL;
6031 blocked.last = NULL;
6032 queue.first = NULL;
6033 queue.last = NULL;
6034 ctx_running = NULL;
6035
6036 if (!lbm_init_env()) return false;
6037
6038 return true;
6039 }
6040
6041 #else
6042
6043 /* eval_cps_run can be paused
6044 I think it would be better use a mailbox for
6045 communication between other threads and the run_eval
6046 but for now a set of variables will be used. */
6047 29346 void lbm_run_eval(void){
6048 29346 setjmp(error_jmp_buf);
6049
6050
2/2
✓ Branch 0 taken 141216 times.
✓ Branch 1 taken 27656 times.
168872 while (eval_running) {
6051
4/4
✓ Branch 0 taken 80132 times.
✓ Branch 1 taken 61084 times.
✓ Branch 2 taken 39161 times.
✓ Branch 3 taken 40971 times.
141216 if (eval_cps_state_changed || eval_cps_run_state == EVAL_CPS_STATE_PAUSED) {
6052 100245 eval_cps_state_changed = false;
6053
4/4
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 70082 times.
✓ Branch 2 taken 898 times.
✓ Branch 3 taken 29257 times.
100245 switch (eval_cps_next_state) {
6054 8 case EVAL_CPS_STATE_RESET:
6055
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (eval_cps_run_state != EVAL_CPS_STATE_RESET) {
6056 8 is_atomic = false;
6057 8 blocked.first = NULL;
6058 8 blocked.last = NULL;
6059 8 queue.first = NULL;
6060 8 queue.last = NULL;
6061 8 ctx_running = NULL;
6062 #ifdef LBM_USE_TIME_QUOTA
6063 ✗ eval_time_quota = 0; // maybe timestamp here ?
6064 #else
6065 8 eval_steps_quota = eval_steps_refill;
6066 #endif
6067 8 eval_cps_run_state = EVAL_CPS_STATE_RESET;
6068
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (blocking_extension) {
6069 ✗ blocking_extension = false;
6070 ✗ lbm_mutex_unlock(&blocking_extension_mutex);
6071 }
6072 }
6073 8 usleep_callback(EVAL_CPS_MIN_SLEEP);
6074 8 continue;
6075 70082 case EVAL_CPS_STATE_PAUSED:
6076
2/2
✓ Branch 0 taken 57680 times.
✓ Branch 1 taken 12402 times.
70082 if (eval_cps_run_state != EVAL_CPS_STATE_PAUSED) {
6077
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 57680 times.
57680 if (lbm_heap_num_free() < eval_cps_next_state_arg) {
6078 ✗ gc();
6079 }
6080 57680 eval_cps_next_state_arg = 0;
6081 57680 eval_cps_run_state = EVAL_CPS_STATE_PAUSED;
6082 }
6083 70082 usleep_callback(EVAL_CPS_MIN_SLEEP);
6084 68421 continue;
6085 898 case EVAL_CPS_STATE_KILL:
6086 898 eval_cps_run_state = EVAL_CPS_STATE_DEAD;
6087 898 eval_running = false;
6088 898 continue;
6089 29257 default: // running state
6090 29257 eval_cps_run_state = eval_cps_next_state;
6091 29257 break;
6092 }
6093 }
6094 69600596 while (true) {
6095 #ifdef LBM_USE_TIME_QUOTA
6096 // Is "negative" (closer to max value) when the quota timestamp is "ahead
6097 // of" the current timestamp. It handles the quota being very large while
6098 // the current timestamp has overflowed back to being small, giving a
6099 // "positive" (closer to min value) result, meaning the context will be
6100 // switched.
6101 69603358 uint32_t unsigned_difference = lbm_timestamp() - eval_current_quota;
6102 69603358 bool is_negative = unsigned_difference & (1u << 31);
6103
4/4
✓ Branch 0 taken 69581445 times.
✓ Branch 1 taken 21913 times.
✓ Branch 2 taken 69560730 times.
✓ Branch 3 taken 20715 times.
69603358 if (is_negative && ctx_running) {
6104 69560730 evaluation_step();
6105 } else {
6106
2/2
✓ Branch 0 taken 802 times.
✓ Branch 1 taken 41826 times.
42628 if (eval_cps_state_changed) break;
6107
1/2
✓ Branch 0 taken 41826 times.
✗ Branch 1 not taken.
41826 if (!is_atomic) {
6108
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 41823 times.
41826 if (gc_requested) {
6109 3 gc();
6110 }
6111 41826 process_events();
6112 41826 lbm_mutex_lock(&qmutex);
6113
2/2
✓ Branch 0 taken 21906 times.
✓ Branch 1 taken 19920 times.
41826 if (ctx_running) {
6114 21906 enqueue_ctx_nm(&queue, ctx_running);
6115 21906 ctx_running = NULL;
6116 }
6117 41826 wake_up_ctxs_nm();
6118 41826 ctx_running = dequeue_ctx_nm(&queue);
6119 41826 lbm_mutex_unlock(&qmutex);
6120
2/2
✓ Branch 0 taken 18437 times.
✓ Branch 1 taken 23389 times.
41826 if (!ctx_running) {
6121 18437 lbm_system_sleeping = true;
6122 //Fixed sleep interval to poll events regularly.
6123 18437 usleep_callback(EVAL_CPS_MIN_SLEEP);
6124 18437 lbm_system_sleeping = false;
6125 }
6126 }
6127 // Assign a new quota last.
6128 // This means that the time it takes in finding a context to dequeue
6129 // and the other work above is not included in the woken up contexts quota.
6130 //
6131 // Earlier the new quota was assigned at the top of this branch,
6132 // If that happened such that timestamp + eval_time_refil ends
6133 // up to be just shy of an overflow, going through all the rest of
6134 // logic could potentially overflow the timestamp and create a situation
6135 // where the scheduled task has a humongous quota!
6136 41826 eval_current_quota = lbm_timestamp() + eval_time_refill;
6137 }
6138 #else
6139
4/4
✓ Branch 0 taken 2624938370 times.
✓ Branch 1 taken 261889965 times.
✓ Branch 2 taken 2619079773 times.
✓ Branch 3 taken 5858597 times.
2886828335 if (eval_steps_quota && ctx_running) {
6140 2619079773 eval_steps_quota--;
6141 2619079773 evaluation_step();
6142 } else {
6143
2/2
✓ Branch 0 taken 56864 times.
✓ Branch 1 taken 267691698 times.
267748562 if (eval_cps_state_changed) break;
6144 267691698 eval_steps_quota = eval_steps_refill;
6145
2/2
✓ Branch 0 taken 267690796 times.
✓ Branch 1 taken 902 times.
267691698 if (!is_atomic) {
6146
2/2
✓ Branch 0 taken 109 times.
✓ Branch 1 taken 267690687 times.
267690796 if (gc_requested) {
6147 109 gc();
6148 }
6149 267690796 process_events();
6150 267690796 lbm_mutex_lock(&qmutex);
6151
2/2
✓ Branch 0 taken 261844999 times.
✓ Branch 1 taken 5845797 times.
267690796 if (ctx_running) {
6152 261844999 enqueue_ctx_nm(&queue, ctx_running);
6153 261844999 ctx_running = NULL;
6154 }
6155 267690796 wake_up_ctxs_nm();
6156 267690796 ctx_running = dequeue_ctx_nm(&queue);
6157 267690796 lbm_mutex_unlock(&qmutex);
6158
2/2
✓ Branch 0 taken 5762361 times.
✓ Branch 1 taken 261928435 times.
267690796 if (!ctx_running) {
6159 5762361 lbm_system_sleeping = true;
6160 //Fixed sleep interval to poll events regularly.
6161 5762361 usleep_callback(EVAL_CPS_MIN_SLEEP);
6162 5762333 lbm_system_sleeping = false;
6163 }
6164 }
6165 }
6166 #endif
6167 }
6168 }
6169 27656 }
6170
6171 29346 bool lbm_eval_init(void) {
6172
2/2
✓ Branch 0 taken 29253 times.
✓ Branch 1 taken 93 times.
29346 if (!qmutex_initialized) {
6173 29253 lbm_mutex_init(&qmutex);
6174 29253 qmutex_initialized = true;
6175 }
6176
2/2
✓ Branch 0 taken 29253 times.
✓ Branch 1 taken 93 times.
29346 if (!lbm_events_mutex_initialized) {
6177 29253 lbm_mutex_init(&lbm_events_mutex);
6178 29253 lbm_events_mutex_initialized = true;
6179 }
6180
2/2
✓ Branch 0 taken 29253 times.
✓ Branch 1 taken 93 times.
29346 if (!blocking_extension_mutex_initialized) {
6181 29253 lbm_mutex_init(&blocking_extension_mutex);
6182 29253 blocking_extension_mutex_initialized = true;
6183 }
6184
6185 29346 lbm_mutex_lock(&qmutex);
6186 29346 lbm_mutex_lock(&lbm_events_mutex);
6187
6188 29346 is_atomic = false;
6189 29346 blocked.first = NULL;
6190 29346 blocked.last = NULL;
6191 29346 queue.first = NULL;
6192 29346 queue.last = NULL;
6193 29346 ctx_running = NULL;
6194
6195 29346 eval_cps_run_state = EVAL_CPS_STATE_RUNNING;
6196
6197 29346 lbm_mutex_unlock(&lbm_events_mutex);
6198 29346 lbm_mutex_unlock(&qmutex);
6199
6200
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 29346 times.
29346 if (!lbm_init_env()) return false;
6201 29346 eval_running = true;
6202 29346 return true;
6203 }
6204
6205 #endif
6206
6207 29251 bool lbm_eval_init_events(unsigned int num_events) {
6208
6209 29251 lbm_mutex_lock(&lbm_events_mutex);
6210 29251 lbm_events = (lbm_event_t*)lbm_malloc(num_events * sizeof(lbm_event_t));
6211 29251 bool r = false;
6212
1/2
✓ Branch 0 taken 29251 times.
✗ Branch 1 not taken.
29251 if (lbm_events) {
6213 29251 lbm_events_max = num_events;
6214 29251 lbm_events_head = 0;
6215 29251 lbm_events_tail = 0;
6216 29251 lbm_events_full = false;
6217 29251 lbm_event_handler_pid = -1;
6218 29251 r = true;
6219 }
6220 29251 lbm_mutex_unlock(&lbm_events_mutex);
6221 29251 return r;
6222 }
6223