Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 1 | /* |
Alexander Steffen | 55f3324 | 2017-06-30 09:22:17 +0200 | [diff] [blame] | 2 | * This file is part of the MicroPython project, http://micropython.org/ |
Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
Damien George | 2757acf | 2023-05-09 11:03:04 +1000 | [diff] [blame] | 6 | * Copyright (c) 2013-2023 Damien P. George |
Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | * of this software and associated documentation files (the "Software"), to deal |
| 10 | * in the Software without restriction, including without limitation the rights |
| 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | * copies of the Software, and to permit persons to whom the Software is |
| 13 | * furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | * THE SOFTWARE. |
| 25 | */ |
Alexander Steffen | 299bc62 | 2017-06-29 23:14:58 +0200 | [diff] [blame] | 26 | #ifndef MICROPY_INCLUDED_PY_NLR_H |
| 27 | #define MICROPY_INCLUDED_PY_NLR_H |
Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 28 | |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 29 | // non-local return |
| 30 | // exception handling, basically a stack of setjmp/longjmp buffers |
| 31 | |
| 32 | #include <limits.h> |
Emmanuel Blot | bf3366a | 2014-06-19 18:47:38 +0200 | [diff] [blame] | 33 | #include <assert.h> |
Damien George | 2757acf | 2023-05-09 11:03:04 +1000 | [diff] [blame] | 34 | #include <stdbool.h> |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 35 | |
Damien George | 51dfcb4 | 2015-01-01 20:27:54 +0000 | [diff] [blame] | 36 | #include "py/mpconfig.h" |
| 37 | |
Damien George | a48cdb5 | 2019-09-18 13:39:01 +1000 | [diff] [blame] | 38 | #define MICROPY_NLR_NUM_REGS_X86 (6) |
| 39 | #define MICROPY_NLR_NUM_REGS_X64 (8) |
| 40 | #define MICROPY_NLR_NUM_REGS_X64_WIN (10) |
| 41 | #define MICROPY_NLR_NUM_REGS_ARM_THUMB (10) |
| 42 | #define MICROPY_NLR_NUM_REGS_ARM_THUMB_FP (10 + 6) |
Yonatan Goldschmidt | 2d5cece | 2021-02-18 01:28:42 +0200 | [diff] [blame] | 43 | #define MICROPY_NLR_NUM_REGS_AARCH64 (13) |
Jan Willeke | 40a3aa7 | 2022-01-07 21:57:20 +0100 | [diff] [blame] | 44 | #define MICROPY_NLR_NUM_REGS_MIPS (13) |
Damien George | a48cdb5 | 2019-09-18 13:39:01 +1000 | [diff] [blame] | 45 | #define MICROPY_NLR_NUM_REGS_XTENSA (10) |
Damien George | 9adedce | 2019-09-13 13:15:12 +1000 | [diff] [blame] | 46 | #define MICROPY_NLR_NUM_REGS_XTENSAWIN (17) |
Damien George | a48cdb5 | 2019-09-18 13:39:01 +1000 | [diff] [blame] | 47 | |
Damien George | 3f39d18 | 2020-02-26 11:58:42 +1100 | [diff] [blame] | 48 | // *FORMAT-OFF* |
| 49 | |
Damien George | 5bf8e85 | 2017-12-28 16:18:39 +1100 | [diff] [blame] | 50 | // If MICROPY_NLR_SETJMP is not enabled then auto-detect the machine arch |
| 51 | #if !MICROPY_NLR_SETJMP |
stijn | b184b6a | 2017-12-26 10:52:09 +0100 | [diff] [blame] | 52 | // A lot of nlr-related things need different treatment on Windows |
| 53 | #if defined(_WIN32) || defined(__CYGWIN__) |
| 54 | #define MICROPY_NLR_OS_WINDOWS 1 |
| 55 | #else |
| 56 | #define MICROPY_NLR_OS_WINDOWS 0 |
| 57 | #endif |
Paul Sokolovsky | 82a165d | 2014-02-27 18:01:43 +0200 | [diff] [blame] | 58 | #if defined(__i386__) |
Damien George | 5bf8e85 | 2017-12-28 16:18:39 +1100 | [diff] [blame] | 59 | #define MICROPY_NLR_X86 (1) |
Damien George | a48cdb5 | 2019-09-18 13:39:01 +1000 | [diff] [blame] | 60 | #define MICROPY_NLR_NUM_REGS (MICROPY_NLR_NUM_REGS_X86) |
Paul Sokolovsky | 82a165d | 2014-02-27 18:01:43 +0200 | [diff] [blame] | 61 | #elif defined(__x86_64__) |
Damien George | 5bf8e85 | 2017-12-28 16:18:39 +1100 | [diff] [blame] | 62 | #define MICROPY_NLR_X64 (1) |
stijn | b184b6a | 2017-12-26 10:52:09 +0100 | [diff] [blame] | 63 | #if MICROPY_NLR_OS_WINDOWS |
Damien George | a48cdb5 | 2019-09-18 13:39:01 +1000 | [diff] [blame] | 64 | #define MICROPY_NLR_NUM_REGS (MICROPY_NLR_NUM_REGS_X64_WIN) |
Damien George | 5bf8e85 | 2017-12-28 16:18:39 +1100 | [diff] [blame] | 65 | #else |
Damien George | a48cdb5 | 2019-09-18 13:39:01 +1000 | [diff] [blame] | 66 | #define MICROPY_NLR_NUM_REGS (MICROPY_NLR_NUM_REGS_X64) |
Damien George | 5bf8e85 | 2017-12-28 16:18:39 +1100 | [diff] [blame] | 67 | #endif |
Paul Sokolovsky | a96cc82 | 2014-06-22 01:14:28 +0300 | [diff] [blame] | 68 | #elif defined(__thumb2__) || defined(__thumb__) || defined(__arm__) |
Damien George | 5bf8e85 | 2017-12-28 16:18:39 +1100 | [diff] [blame] | 69 | #define MICROPY_NLR_THUMB (1) |
Damien George | 34c04d2 | 2019-06-17 23:19:34 +1000 | [diff] [blame] | 70 | #if defined(__SOFTFP__) |
Damien George | a48cdb5 | 2019-09-18 13:39:01 +1000 | [diff] [blame] | 71 | #define MICROPY_NLR_NUM_REGS (MICROPY_NLR_NUM_REGS_ARM_THUMB) |
Damien George | 34c04d2 | 2019-06-17 23:19:34 +1000 | [diff] [blame] | 72 | #else |
| 73 | // With hardware FP registers s16-s31 are callee save so in principle |
| 74 | // should be saved and restored by the NLR code. gcc only uses s16-s21 |
| 75 | // so only save/restore those as an optimisation. |
Damien George | a48cdb5 | 2019-09-18 13:39:01 +1000 | [diff] [blame] | 76 | #define MICROPY_NLR_NUM_REGS (MICROPY_NLR_NUM_REGS_ARM_THUMB_FP) |
Damien George | 34c04d2 | 2019-06-17 23:19:34 +1000 | [diff] [blame] | 77 | #endif |
Yonatan Goldschmidt | 2d5cece | 2021-02-18 01:28:42 +0200 | [diff] [blame] | 78 | #elif defined(__aarch64__) |
| 79 | #define MICROPY_NLR_AARCH64 (1) |
| 80 | #define MICROPY_NLR_NUM_REGS (MICROPY_NLR_NUM_REGS_AARCH64) |
Damien George | 2399aa0 | 2014-11-27 20:29:33 +0000 | [diff] [blame] | 81 | #elif defined(__xtensa__) |
Damien George | 5bf8e85 | 2017-12-28 16:18:39 +1100 | [diff] [blame] | 82 | #define MICROPY_NLR_XTENSA (1) |
Damien George | a48cdb5 | 2019-09-18 13:39:01 +1000 | [diff] [blame] | 83 | #define MICROPY_NLR_NUM_REGS (MICROPY_NLR_NUM_REGS_XTENSA) |
Michael Neuling | 079cc94 | 2019-08-22 10:21:48 +1000 | [diff] [blame] | 84 | #elif defined(__powerpc__) |
| 85 | #define MICROPY_NLR_POWERPC (1) |
| 86 | // this could be less but using 128 for safety |
| 87 | #define MICROPY_NLR_NUM_REGS (128) |
Jan Willeke | 40a3aa7 | 2022-01-07 21:57:20 +0100 | [diff] [blame] | 88 | #elif defined(__mips__) |
| 89 | #define MICROPY_NLR_MIPS (1) |
| 90 | #define MICROPY_NLR_NUM_REGS (MICROPY_NLR_NUM_REGS_MIPS) |
Paul Sokolovsky | 82a165d | 2014-02-27 18:01:43 +0200 | [diff] [blame] | 91 | #else |
Paul Sokolovsky | 3a83b80 | 2014-04-17 00:16:45 +0300 | [diff] [blame] | 92 | #define MICROPY_NLR_SETJMP (1) |
Paul Sokolovsky | 851c856 | 2014-04-30 04:14:31 +0300 | [diff] [blame] | 93 | //#warning "No native NLR support for this arch, using setjmp implementation" |
Paul Sokolovsky | 3a83b80 | 2014-04-17 00:16:45 +0300 | [diff] [blame] | 94 | #endif |
| 95 | #endif |
| 96 | |
Damien George | 3f39d18 | 2020-02-26 11:58:42 +1100 | [diff] [blame] | 97 | // *FORMAT-ON* |
| 98 | |
Paul Sokolovsky | 3a83b80 | 2014-04-17 00:16:45 +0300 | [diff] [blame] | 99 | #if MICROPY_NLR_SETJMP |
Damien George | 5bf8e85 | 2017-12-28 16:18:39 +1100 | [diff] [blame] | 100 | #include <setjmp.h> |
Paul Sokolovsky | 096e967 | 2017-12-26 18:39:51 +0200 | [diff] [blame] | 101 | #endif |
Damien George | 6a3a742 | 2017-12-18 18:57:15 +1100 | [diff] [blame] | 102 | |
Damien George | 5bf8e85 | 2017-12-28 16:18:39 +1100 | [diff] [blame] | 103 | typedef struct _nlr_buf_t nlr_buf_t; |
| 104 | struct _nlr_buf_t { |
Damien George | d54208a | 2022-12-16 17:31:21 +1100 | [diff] [blame] | 105 | // The entries in this struct must all be machine word size. |
| 106 | |
| 107 | // Pointer to the previous nlr_buf_t in the chain. |
| 108 | // Or NULL if it's the top-level one. |
Damien George | 5bf8e85 | 2017-12-28 16:18:39 +1100 | [diff] [blame] | 109 | nlr_buf_t *prev; |
Damien George | d54208a | 2022-12-16 17:31:21 +1100 | [diff] [blame] | 110 | |
| 111 | // The exception that is being raised: |
| 112 | // - NULL means the jump is because of a VM abort (only if MICROPY_ENABLE_VM_ABORT enabled) |
| 113 | // - otherwise it's always a concrete object (an exception instance) |
| 114 | void *ret_val; |
Damien George | 5bf8e85 | 2017-12-28 16:18:39 +1100 | [diff] [blame] | 115 | |
| 116 | #if MICROPY_NLR_SETJMP |
| 117 | jmp_buf jmpbuf; |
| 118 | #else |
| 119 | void *regs[MICROPY_NLR_NUM_REGS]; |
| 120 | #endif |
| 121 | |
Damien George | 02d830c | 2017-11-26 23:28:40 +1100 | [diff] [blame] | 122 | #if MICROPY_ENABLE_PYSTACK |
| 123 | void *pystack; |
| 124 | #endif |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 125 | }; |
| 126 | |
Damien George | 2757acf | 2023-05-09 11:03:04 +1000 | [diff] [blame] | 127 | typedef void (*nlr_jump_callback_fun_t)(void *ctx); |
| 128 | |
| 129 | typedef struct _nlr_jump_callback_node_t nlr_jump_callback_node_t; |
| 130 | |
| 131 | struct _nlr_jump_callback_node_t { |
| 132 | nlr_jump_callback_node_t *prev; |
| 133 | nlr_jump_callback_fun_t fun; |
| 134 | }; |
| 135 | |
Paul Sokolovsky | 096e967 | 2017-12-26 18:39:51 +0200 | [diff] [blame] | 136 | // Helper macros to save/restore the pystack state |
| 137 | #if MICROPY_ENABLE_PYSTACK |
| 138 | #define MP_NLR_SAVE_PYSTACK(nlr_buf) (nlr_buf)->pystack = MP_STATE_THREAD(pystack_cur) |
| 139 | #define MP_NLR_RESTORE_PYSTACK(nlr_buf) MP_STATE_THREAD(pystack_cur) = (nlr_buf)->pystack |
Paul Sokolovsky | 3a83b80 | 2014-04-17 00:16:45 +0300 | [diff] [blame] | 140 | #else |
Paul Sokolovsky | 096e967 | 2017-12-26 18:39:51 +0200 | [diff] [blame] | 141 | #define MP_NLR_SAVE_PYSTACK(nlr_buf) (void)nlr_buf |
| 142 | #define MP_NLR_RESTORE_PYSTACK(nlr_buf) (void)nlr_buf |
Damien George | 6a3a742 | 2017-12-18 18:57:15 +1100 | [diff] [blame] | 143 | #endif |
| 144 | |
Damien George | b25f921 | 2017-12-28 16:46:30 +1100 | [diff] [blame] | 145 | // Helper macro to use at the start of a specific nlr_jump implementation |
| 146 | #define MP_NLR_JUMP_HEAD(val, top) \ |
| 147 | nlr_buf_t **_top_ptr = &MP_STATE_THREAD(nlr_top); \ |
| 148 | nlr_buf_t *top = *_top_ptr; \ |
| 149 | if (top == NULL) { \ |
| 150 | nlr_jump_fail(val); \ |
| 151 | } \ |
| 152 | top->ret_val = val; \ |
Damien George | 2757acf | 2023-05-09 11:03:04 +1000 | [diff] [blame] | 153 | nlr_call_jump_callbacks(top); \ |
Damien George | b25f921 | 2017-12-28 16:46:30 +1100 | [diff] [blame] | 154 | MP_NLR_RESTORE_PYSTACK(top); \ |
| 155 | *_top_ptr = top->prev; \ |
Paul Sokolovsky | 096e967 | 2017-12-26 18:39:51 +0200 | [diff] [blame] | 156 | |
Damien George | b25f921 | 2017-12-28 16:46:30 +1100 | [diff] [blame] | 157 | #if MICROPY_NLR_SETJMP |
Paul Sokolovsky | 096e967 | 2017-12-26 18:39:51 +0200 | [diff] [blame] | 158 | // nlr_push() must be defined as a macro, because "The stack context will be |
| 159 | // invalidated if the function which called setjmp() returns." |
Damien George | b25f921 | 2017-12-28 16:46:30 +1100 | [diff] [blame] | 160 | // For this case it is safe to call nlr_push_tail() first. |
| 161 | #define nlr_push(buf) (nlr_push_tail(buf), setjmp((buf)->jmpbuf)) |
Paul Sokolovsky | 096e967 | 2017-12-26 18:39:51 +0200 | [diff] [blame] | 162 | #else |
| 163 | unsigned int nlr_push(nlr_buf_t *); |
Damien George | b25f921 | 2017-12-28 16:46:30 +1100 | [diff] [blame] | 164 | #endif |
| 165 | |
| 166 | unsigned int nlr_push_tail(nlr_buf_t *top); |
Damien | 8b3a7c2 | 2013-10-23 20:20:17 +0100 | [diff] [blame] | 167 | void nlr_pop(void); |
Paul Sokolovsky | e908591 | 2014-04-30 05:35:18 +0300 | [diff] [blame] | 168 | NORETURN void nlr_jump(void *val); |
Damien George | ea13f40 | 2014-04-05 18:32:08 +0100 | [diff] [blame] | 169 | |
Damien George | d54208a | 2022-12-16 17:31:21 +1100 | [diff] [blame] | 170 | #if MICROPY_ENABLE_VM_ABORT |
| 171 | #define nlr_set_abort(buf) MP_STATE_VM(nlr_abort) = buf |
| 172 | #define nlr_get_abort() MP_STATE_VM(nlr_abort) |
| 173 | NORETURN void nlr_jump_abort(void); |
| 174 | #endif |
| 175 | |
Damien George | 26cf55a | 2014-04-08 14:08:14 +0000 | [diff] [blame] | 176 | // This must be implemented by a port. It's called by nlr_jump |
| 177 | // if no nlr buf has been pushed. It must not return, but rather |
| 178 | // should bail out with a fatal error. |
Damien George | be3d7f9 | 2017-02-16 17:23:06 +1100 | [diff] [blame] | 179 | NORETURN void nlr_jump_fail(void *val); |
Damien George | 26cf55a | 2014-04-08 14:08:14 +0000 | [diff] [blame] | 180 | |
Damien George | ea13f40 | 2014-04-05 18:32:08 +0100 | [diff] [blame] | 181 | // use nlr_raise instead of nlr_jump so that debugging is easier |
stijn | 2f0ce2a | 2017-04-26 13:17:55 +0200 | [diff] [blame] | 182 | #ifndef MICROPY_DEBUG_NLR |
Damien George | 999cedb | 2015-11-27 17:01:44 +0000 | [diff] [blame] | 183 | #define nlr_raise(val) nlr_jump(MP_OBJ_TO_PTR(val)) |
Damien George | ea13f40 | 2014-04-05 18:32:08 +0100 | [diff] [blame] | 184 | #else |
Damien George | f36ae5e | 2023-06-02 17:52:40 +1000 | [diff] [blame] | 185 | |
Damien George | ea13f40 | 2014-04-05 18:32:08 +0100 | [diff] [blame] | 186 | #define nlr_raise(val) \ |
| 187 | do { \ |
Damien George | 999cedb | 2015-11-27 17:01:44 +0000 | [diff] [blame] | 188 | void *_val = MP_OBJ_TO_PTR(val); \ |
Damien George | ea13f40 | 2014-04-05 18:32:08 +0100 | [diff] [blame] | 189 | assert(_val != NULL); \ |
Damien George | 999cedb | 2015-11-27 17:01:44 +0000 | [diff] [blame] | 190 | assert(mp_obj_is_exception_instance(val)); \ |
Damien George | ea13f40 | 2014-04-05 18:32:08 +0100 | [diff] [blame] | 191 | nlr_jump(_val); \ |
| 192 | } while (0) |
Paul Sokolovsky | e89cc13 | 2015-02-15 20:23:52 +0300 | [diff] [blame] | 193 | |
stijn | 803264b | 2015-03-03 11:15:06 +0100 | [diff] [blame] | 194 | #if !MICROPY_NLR_SETJMP |
Paul Sokolovsky | e89cc13 | 2015-02-15 20:23:52 +0300 | [diff] [blame] | 195 | #define nlr_push(val) \ |
Damien George | 5b700b0 | 2022-05-05 13:28:32 +1000 | [diff] [blame] | 196 | assert(MP_STATE_THREAD(nlr_top) != val), nlr_push(val) |
stijn | 803264b | 2015-03-03 11:15:06 +0100 | [diff] [blame] | 197 | #endif |
Paul Sokolovsky | 3077fbf | 2015-02-15 20:28:18 +0300 | [diff] [blame] | 198 | |
Damien George | ea13f40 | 2014-04-05 18:32:08 +0100 | [diff] [blame] | 199 | #endif |
Damien George | 51dfcb4 | 2015-01-01 20:27:54 +0000 | [diff] [blame] | 200 | |
Damien George | 2757acf | 2023-05-09 11:03:04 +1000 | [diff] [blame] | 201 | // Push a callback on to the linked-list of NLR jump callbacks. The `node` pointer must |
| 202 | // be on the C stack. The `fun` callback will be executed if an NLR jump is taken which |
| 203 | // unwinds the C stack through this `node`. |
| 204 | void nlr_push_jump_callback(nlr_jump_callback_node_t *node, nlr_jump_callback_fun_t fun); |
| 205 | |
| 206 | // Pop a callback from the linked-list of NLR jump callbacks. The corresponding function |
| 207 | // will be called if `run_callback` is true. |
| 208 | void nlr_pop_jump_callback(bool run_callback); |
| 209 | |
| 210 | // Pop and call all NLR jump callbacks that were registered after `nlr` buffer was pushed. |
| 211 | void nlr_call_jump_callbacks(nlr_buf_t *nlr); |
| 212 | |
Alexander Steffen | 299bc62 | 2017-06-29 23:14:58 +0200 | [diff] [blame] | 213 | #endif // MICROPY_INCLUDED_PY_NLR_H |