GCC Code Coverage Report


Directory: ../src/
File: /home/joels/Current/lispbm/src/eval_cps.c
Date: 2026-09-05 08:38:23
Exec Total Coverage
Lines: 3191 3398 93.9%
Functions: 218 226 96.5%
Branches: 1123 1442 77.9%

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