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