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