GCC Code Coverage Report


Directory: ../src/
File: eval_cps.c
Date: 2026-09-06 17:54:43
Coverage Exec Excl Total
Lines: 93.4% 3172 0 3397
Functions: 96.0% 217 0 226
Branches: 76.6% 1104 0 1442

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