nlr.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013, 2014 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 "py/mpconfig.h"
  33. #define MICROPY_NLR_NUM_REGS_X86 (6)
  34. #define MICROPY_NLR_NUM_REGS_X64 (8)
  35. #define MICROPY_NLR_NUM_REGS_X64_WIN (10)
  36. #define MICROPY_NLR_NUM_REGS_ARM_THUMB (10)
  37. #define MICROPY_NLR_NUM_REGS_ARM_THUMB_FP (10 + 6)
  38. #define MICROPY_NLR_NUM_REGS_XTENSA (10)
  39. #define MICROPY_NLR_NUM_REGS_XTENSAWIN (17)
  40. // If MICROPY_NLR_SETJMP is not enabled then auto-detect the machine arch
  41. #if !MICROPY_NLR_SETJMP
  42. // A lot of nlr-related things need different treatment on Windows
  43. #if defined(_WIN32) || defined(__CYGWIN__)
  44. #define MICROPY_NLR_OS_WINDOWS 1
  45. #else
  46. #define MICROPY_NLR_OS_WINDOWS 0
  47. #endif
  48. #if defined(__i386__)
  49. #define MICROPY_NLR_X86 (1)
  50. #define MICROPY_NLR_NUM_REGS (MICROPY_NLR_NUM_REGS_X86)
  51. #elif defined(__x86_64__)
  52. #define MICROPY_NLR_X64 (1)
  53. #if MICROPY_NLR_OS_WINDOWS
  54. #define MICROPY_NLR_NUM_REGS (MICROPY_NLR_NUM_REGS_X64_WIN)
  55. #else
  56. #define MICROPY_NLR_NUM_REGS (MICROPY_NLR_NUM_REGS_X64)
  57. #endif
  58. #elif defined(__thumb2__) || defined(__thumb__) || defined(__arm__)
  59. #define MICROPY_NLR_THUMB (1)
  60. #if defined(__SOFTFP__)
  61. #define MICROPY_NLR_NUM_REGS (MICROPY_NLR_NUM_REGS_ARM_THUMB)
  62. #else
  63. // With hardware FP registers s16-s31 are callee save so in principle
  64. // should be saved and restored by the NLR code. gcc only uses s16-s21
  65. // so only save/restore those as an optimisation.
  66. #define MICROPY_NLR_NUM_REGS (MICROPY_NLR_NUM_REGS_ARM_THUMB_FP)
  67. #endif
  68. #elif defined(__xtensa__)
  69. #define MICROPY_NLR_XTENSA (1)
  70. #define MICROPY_NLR_NUM_REGS (MICROPY_NLR_NUM_REGS_XTENSA)
  71. #elif defined(__powerpc__)
  72. #define MICROPY_NLR_POWERPC (1)
  73. // this could be less but using 128 for safety
  74. #define MICROPY_NLR_NUM_REGS (128)
  75. #else
  76. #define MICROPY_NLR_SETJMP (1)
  77. //#warning "No native NLR support for this arch, using setjmp implementation"
  78. #endif
  79. #endif
  80. #if MICROPY_NLR_SETJMP
  81. #include <setjmp.h>
  82. #endif
  83. typedef struct _nlr_buf_t nlr_buf_t;
  84. struct _nlr_buf_t {
  85. // the entries here must all be machine word size
  86. nlr_buf_t *prev;
  87. void *ret_val; // always a concrete object (an exception instance)
  88. #if MICROPY_NLR_SETJMP
  89. jmp_buf jmpbuf;
  90. #else
  91. void *regs[MICROPY_NLR_NUM_REGS];
  92. #endif
  93. #if MICROPY_ENABLE_PYSTACK
  94. void *pystack;
  95. #endif
  96. };
  97. // Helper macros to save/restore the pystack state
  98. #if MICROPY_ENABLE_PYSTACK
  99. #define MP_NLR_SAVE_PYSTACK(nlr_buf) (nlr_buf)->pystack = MP_STATE_THREAD(pystack_cur)
  100. #define MP_NLR_RESTORE_PYSTACK(nlr_buf) MP_STATE_THREAD(pystack_cur) = (nlr_buf)->pystack
  101. #else
  102. #define MP_NLR_SAVE_PYSTACK(nlr_buf) (void)nlr_buf
  103. #define MP_NLR_RESTORE_PYSTACK(nlr_buf) (void)nlr_buf
  104. #endif
  105. // Helper macro to use at the start of a specific nlr_jump implementation
  106. #define MP_NLR_JUMP_HEAD(val, top) \
  107. nlr_buf_t **_top_ptr = &MP_STATE_THREAD(nlr_top); \
  108. nlr_buf_t *top = *_top_ptr; \
  109. if (top == NULL) { \
  110. nlr_jump_fail(val); \
  111. } \
  112. top->ret_val = val; \
  113. MP_NLR_RESTORE_PYSTACK(top); \
  114. *_top_ptr = top->prev; \
  115. #if MICROPY_NLR_SETJMP
  116. // nlr_push() must be defined as a macro, because "The stack context will be
  117. // invalidated if the function which called setjmp() returns."
  118. // For this case it is safe to call nlr_push_tail() first.
  119. #define nlr_push(buf) (nlr_push_tail(buf), setjmp((buf)->jmpbuf))
  120. #else
  121. unsigned int nlr_push(nlr_buf_t *);
  122. #endif
  123. unsigned int nlr_push_tail(nlr_buf_t *top);
  124. void nlr_pop(void);
  125. NORETURN void nlr_jump(void *val);
  126. // This must be implemented by a port. It's called by nlr_jump
  127. // if no nlr buf has been pushed. It must not return, but rather
  128. // should bail out with a fatal error.
  129. NORETURN void nlr_jump_fail(void *val);
  130. // use nlr_raise instead of nlr_jump so that debugging is easier
  131. #ifndef MICROPY_DEBUG_NLR
  132. #define nlr_raise(val) nlr_jump(MP_OBJ_TO_PTR(val))
  133. #else
  134. #include "mpstate.h"
  135. #define nlr_raise(val) \
  136. do { \
  137. /*printf("nlr_raise: nlr_top=%p\n", MP_STATE_THREAD(nlr_top)); \
  138. fflush(stdout);*/ \
  139. void *_val = MP_OBJ_TO_PTR(val); \
  140. assert(_val != NULL); \
  141. assert(mp_obj_is_exception_instance(val)); \
  142. nlr_jump(_val); \
  143. } while (0)
  144. #if !MICROPY_NLR_SETJMP
  145. #define nlr_push(val) \
  146. assert(MP_STATE_THREAD(nlr_top) != val),nlr_push(val)
  147. /*
  148. #define nlr_push(val) \
  149. printf("nlr_push: before: nlr_top=%p, val=%p\n", MP_STATE_THREAD(nlr_top), val),assert(MP_STATE_THREAD(nlr_top) != val),nlr_push(val)
  150. */
  151. #endif
  152. #endif
  153. #endif // MICROPY_INCLUDED_PY_NLR_H