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