nlr.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. // If MICROPY_NLR_SETJMP is not enabled then auto-detect the machine arch
  34. #if !MICROPY_NLR_SETJMP
  35. // A lot of nlr-related things need different treatment on Windows
  36. #if defined(_WIN32) || defined(__CYGWIN__)
  37. #define MICROPY_NLR_OS_WINDOWS 1
  38. #else
  39. #define MICROPY_NLR_OS_WINDOWS 0
  40. #endif
  41. #if defined(__i386__)
  42. #define MICROPY_NLR_X86 (1)
  43. #define MICROPY_NLR_NUM_REGS (6)
  44. #elif defined(__x86_64__)
  45. #define MICROPY_NLR_X64 (1)
  46. #if MICROPY_NLR_OS_WINDOWS
  47. #define MICROPY_NLR_NUM_REGS (10)
  48. #else
  49. #define MICROPY_NLR_NUM_REGS (8)
  50. #endif
  51. #elif defined(__thumb2__) || defined(__thumb__) || defined(__arm__)
  52. #define MICROPY_NLR_THUMB (1)
  53. #define MICROPY_NLR_NUM_REGS (10)
  54. #elif defined(__xtensa__)
  55. #define MICROPY_NLR_XTENSA (1)
  56. #define MICROPY_NLR_NUM_REGS (10)
  57. #else
  58. #define MICROPY_NLR_SETJMP (1)
  59. //#warning "No native NLR support for this arch, using setjmp implementation"
  60. #endif
  61. #endif
  62. #if MICROPY_NLR_SETJMP
  63. #include <setjmp.h>
  64. #endif
  65. typedef struct _nlr_buf_t nlr_buf_t;
  66. struct _nlr_buf_t {
  67. // the entries here must all be machine word size
  68. nlr_buf_t *prev;
  69. void *ret_val; // always a concrete object (an exception instance)
  70. #if MICROPY_NLR_SETJMP
  71. jmp_buf jmpbuf;
  72. #else
  73. void *regs[MICROPY_NLR_NUM_REGS];
  74. #endif
  75. #if MICROPY_ENABLE_PYSTACK
  76. void *pystack;
  77. #endif
  78. };
  79. // Helper macros to save/restore the pystack state
  80. #if MICROPY_ENABLE_PYSTACK
  81. #define MP_NLR_SAVE_PYSTACK(nlr_buf) (nlr_buf)->pystack = MP_STATE_THREAD(pystack_cur)
  82. #define MP_NLR_RESTORE_PYSTACK(nlr_buf) MP_STATE_THREAD(pystack_cur) = (nlr_buf)->pystack
  83. #else
  84. #define MP_NLR_SAVE_PYSTACK(nlr_buf) (void)nlr_buf
  85. #define MP_NLR_RESTORE_PYSTACK(nlr_buf) (void)nlr_buf
  86. #endif
  87. // Helper macro to use at the start of a specific nlr_jump implementation
  88. #define MP_NLR_JUMP_HEAD(val, top) \
  89. nlr_buf_t **_top_ptr = &MP_STATE_THREAD(nlr_top); \
  90. nlr_buf_t *top = *_top_ptr; \
  91. if (top == NULL) { \
  92. nlr_jump_fail(val); \
  93. } \
  94. top->ret_val = val; \
  95. MP_NLR_RESTORE_PYSTACK(top); \
  96. *_top_ptr = top->prev; \
  97. #if MICROPY_NLR_SETJMP
  98. // nlr_push() must be defined as a macro, because "The stack context will be
  99. // invalidated if the function which called setjmp() returns."
  100. // For this case it is safe to call nlr_push_tail() first.
  101. #define nlr_push(buf) (nlr_push_tail(buf), setjmp((buf)->jmpbuf))
  102. #else
  103. unsigned int nlr_push(nlr_buf_t *);
  104. #endif
  105. unsigned int nlr_push_tail(nlr_buf_t *top);
  106. void nlr_pop(void);
  107. NORETURN void nlr_jump(void *val);
  108. // This must be implemented by a port. It's called by nlr_jump
  109. // if no nlr buf has been pushed. It must not return, but rather
  110. // should bail out with a fatal error.
  111. NORETURN void nlr_jump_fail(void *val);
  112. // use nlr_raise instead of nlr_jump so that debugging is easier
  113. #ifndef MICROPY_DEBUG_NLR
  114. #define nlr_raise(val) nlr_jump(MP_OBJ_TO_PTR(val))
  115. #else
  116. #include "mpstate.h"
  117. #define nlr_raise(val) \
  118. do { \
  119. /*printf("nlr_raise: nlr_top=%p\n", MP_STATE_THREAD(nlr_top)); \
  120. fflush(stdout);*/ \
  121. void *_val = MP_OBJ_TO_PTR(val); \
  122. assert(_val != NULL); \
  123. assert(mp_obj_is_exception_instance(val)); \
  124. nlr_jump(_val); \
  125. } while (0)
  126. #if !MICROPY_NLR_SETJMP
  127. #define nlr_push(val) \
  128. assert(MP_STATE_THREAD(nlr_top) != val),nlr_push(val)
  129. /*
  130. #define nlr_push(val) \
  131. 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)
  132. */
  133. #endif
  134. #endif
  135. #endif // MICROPY_INCLUDED_PY_NLR_H