nlr.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013-2023 Damien P. George
  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. */
  26. #ifndef MICROPY_INCLUDED_PY_NLR_H
  27. #define MICROPY_INCLUDED_PY_NLR_H
  28. // non-local return
  29. // exception handling, basically a stack of setjmp/longjmp buffers
  30. #include <limits.h>
  31. #include <assert.h>
  32. #include <stdbool.h>
  33. #include "py/mpconfig.h"
  34. #define MICROPY_NLR_NUM_REGS_X86 (6)
  35. #define MICROPY_NLR_NUM_REGS_X64 (8)
  36. #define MICROPY_NLR_NUM_REGS_X64_WIN (10)
  37. #define MICROPY_NLR_NUM_REGS_ARM_THUMB (10)
  38. #define MICROPY_NLR_NUM_REGS_ARM_THUMB_FP (10 + 6)
  39. #define MICROPY_NLR_NUM_REGS_AARCH64 (13)
  40. #define MICROPY_NLR_NUM_REGS_MIPS (13)
  41. #define MICROPY_NLR_NUM_REGS_XTENSA (10)
  42. #define MICROPY_NLR_NUM_REGS_XTENSAWIN (17)
  43. // *FORMAT-OFF*
  44. // If MICROPY_NLR_SETJMP is not enabled then auto-detect the machine arch
  45. #if !MICROPY_NLR_SETJMP
  46. // A lot of nlr-related things need different treatment on Windows
  47. #if defined(_WIN32) || defined(__CYGWIN__)
  48. #define MICROPY_NLR_OS_WINDOWS 1
  49. #else
  50. #define MICROPY_NLR_OS_WINDOWS 0
  51. #endif
  52. #if defined(__i386__)
  53. #define MICROPY_NLR_X86 (1)
  54. #define MICROPY_NLR_NUM_REGS (MICROPY_NLR_NUM_REGS_X86)
  55. #elif defined(__x86_64__)
  56. #define MICROPY_NLR_X64 (1)
  57. #if MICROPY_NLR_OS_WINDOWS
  58. #define MICROPY_NLR_NUM_REGS (MICROPY_NLR_NUM_REGS_X64_WIN)
  59. #else
  60. #define MICROPY_NLR_NUM_REGS (MICROPY_NLR_NUM_REGS_X64)
  61. #endif
  62. #elif defined(__thumb2__) || defined(__thumb__) || defined(__arm__)
  63. #define MICROPY_NLR_THUMB (1)
  64. #if defined(__SOFTFP__)
  65. #define MICROPY_NLR_NUM_REGS (MICROPY_NLR_NUM_REGS_ARM_THUMB)
  66. #else
  67. // With hardware FP registers s16-s31 are callee save so in principle
  68. // should be saved and restored by the NLR code. gcc only uses s16-s21
  69. // so only save/restore those as an optimisation.
  70. #define MICROPY_NLR_NUM_REGS (MICROPY_NLR_NUM_REGS_ARM_THUMB_FP)
  71. #endif
  72. #elif defined(__aarch64__)
  73. #define MICROPY_NLR_AARCH64 (1)
  74. #define MICROPY_NLR_NUM_REGS (MICROPY_NLR_NUM_REGS_AARCH64)
  75. #elif defined(__xtensa__)
  76. #define MICROPY_NLR_XTENSA (1)
  77. #define MICROPY_NLR_NUM_REGS (MICROPY_NLR_NUM_REGS_XTENSA)
  78. #elif defined(__powerpc__)
  79. #define MICROPY_NLR_POWERPC (1)
  80. // this could be less but using 128 for safety
  81. #define MICROPY_NLR_NUM_REGS (128)
  82. #elif defined(__mips__)
  83. #define MICROPY_NLR_MIPS (1)
  84. #define MICROPY_NLR_NUM_REGS (MICROPY_NLR_NUM_REGS_MIPS)
  85. #else
  86. #define MICROPY_NLR_SETJMP (1)
  87. //#warning "No native NLR support for this arch, using setjmp implementation"
  88. #endif
  89. #endif
  90. // *FORMAT-ON*
  91. #if MICROPY_NLR_SETJMP
  92. #include <setjmp.h>
  93. #endif
  94. typedef struct _nlr_buf_t nlr_buf_t;
  95. struct _nlr_buf_t {
  96. // The entries in this struct must all be machine word size.
  97. // Pointer to the previous nlr_buf_t in the chain.
  98. // Or NULL if it's the top-level one.
  99. nlr_buf_t *prev;
  100. // The exception that is being raised:
  101. // - NULL means the jump is because of a VM abort (only if MICROPY_ENABLE_VM_ABORT enabled)
  102. // - otherwise it's always a concrete object (an exception instance)
  103. void *ret_val;
  104. #if MICROPY_NLR_SETJMP
  105. jmp_buf jmpbuf;
  106. #else
  107. void *regs[MICROPY_NLR_NUM_REGS];
  108. #endif
  109. #if MICROPY_ENABLE_PYSTACK
  110. void *pystack;
  111. #endif
  112. };
  113. typedef void (*nlr_jump_callback_fun_t)(void *ctx);
  114. typedef struct _nlr_jump_callback_node_t nlr_jump_callback_node_t;
  115. struct _nlr_jump_callback_node_t {
  116. nlr_jump_callback_node_t *prev;
  117. nlr_jump_callback_fun_t fun;
  118. };
  119. // Helper macros to save/restore the pystack state
  120. #if MICROPY_ENABLE_PYSTACK
  121. #define MP_NLR_SAVE_PYSTACK(nlr_buf) (nlr_buf)->pystack = MP_STATE_THREAD(pystack_cur)
  122. #define MP_NLR_RESTORE_PYSTACK(nlr_buf) MP_STATE_THREAD(pystack_cur) = (nlr_buf)->pystack
  123. #else
  124. #define MP_NLR_SAVE_PYSTACK(nlr_buf) (void)nlr_buf
  125. #define MP_NLR_RESTORE_PYSTACK(nlr_buf) (void)nlr_buf
  126. #endif
  127. // Helper macro to use at the start of a specific nlr_jump implementation
  128. #define MP_NLR_JUMP_HEAD(val, top) \
  129. nlr_buf_t **_top_ptr = &MP_STATE_THREAD(nlr_top); \
  130. nlr_buf_t *top = *_top_ptr; \
  131. if (top == NULL) { \
  132. nlr_jump_fail(val); \
  133. } \
  134. top->ret_val = val; \
  135. nlr_call_jump_callbacks(top); \
  136. MP_NLR_RESTORE_PYSTACK(top); \
  137. *_top_ptr = top->prev; \
  138. #if MICROPY_NLR_SETJMP
  139. // nlr_push() must be defined as a macro, because "The stack context will be
  140. // invalidated if the function which called setjmp() returns."
  141. // For this case it is safe to call nlr_push_tail() first.
  142. #define nlr_push(buf) (nlr_push_tail(buf), setjmp((buf)->jmpbuf))
  143. #else
  144. unsigned int nlr_push(nlr_buf_t *);
  145. #endif
  146. unsigned int nlr_push_tail(nlr_buf_t *top);
  147. void nlr_pop(void);
  148. NORETURN void nlr_jump(void *val);
  149. #if MICROPY_ENABLE_VM_ABORT
  150. #define nlr_set_abort(buf) MP_STATE_VM(nlr_abort) = buf
  151. #define nlr_get_abort() MP_STATE_VM(nlr_abort)
  152. NORETURN void nlr_jump_abort(void);
  153. #endif
  154. // This must be implemented by a port. It's called by nlr_jump
  155. // if no nlr buf has been pushed. It must not return, but rather
  156. // should bail out with a fatal error.
  157. NORETURN void nlr_jump_fail(void *val);
  158. // use nlr_raise instead of nlr_jump so that debugging is easier
  159. #ifndef MICROPY_DEBUG_NLR
  160. #define nlr_raise(val) nlr_jump(MP_OBJ_TO_PTR(val))
  161. #else
  162. #define nlr_raise(val) \
  163. do { \
  164. void *_val = MP_OBJ_TO_PTR(val); \
  165. assert(_val != NULL); \
  166. assert(mp_obj_is_exception_instance(val)); \
  167. nlr_jump(_val); \
  168. } while (0)
  169. #if !MICROPY_NLR_SETJMP
  170. #define nlr_push(val) \
  171. assert(MP_STATE_THREAD(nlr_top) != val), nlr_push(val)
  172. #endif
  173. #endif
  174. // Push a callback on to the linked-list of NLR jump callbacks. The `node` pointer must
  175. // be on the C stack. The `fun` callback will be executed if an NLR jump is taken which
  176. // unwinds the C stack through this `node`.
  177. void nlr_push_jump_callback(nlr_jump_callback_node_t *node, nlr_jump_callback_fun_t fun);
  178. // Pop a callback from the linked-list of NLR jump callbacks. The corresponding function
  179. // will be called if `run_callback` is true.
  180. void nlr_pop_jump_callback(bool run_callback);
  181. // Pop and call all NLR jump callbacks that were registered after `nlr` buffer was pushed.
  182. void nlr_call_jump_callbacks(nlr_buf_t *nlr);
  183. #endif // MICROPY_INCLUDED_PY_NLR_H