GCC Code Coverage Report


Directory: ../src/
File: eval_cps.c
Date: 2026-02-08 10:07:39
Coverage Exec Excl Total
Lines: 94.0% 3171 0 3375
Functions: 96.9% 216 0 223
Branches: 77.6% 1107 0 1426

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