nlr.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 <setjmp.h>
  32. #include <assert.h>
  33. #include "py/mpconfig.h"
  34. typedef struct _nlr_buf_t nlr_buf_t;
  35. struct _nlr_buf_t {
  36. // the entries here must all be machine word size
  37. nlr_buf_t *prev;
  38. void *ret_val; // always a concrete object (an exception instance)
  39. #if !defined(MICROPY_NLR_SETJMP) || !MICROPY_NLR_SETJMP
  40. #if defined(__i386__)
  41. void *regs[6];
  42. #elif defined(__x86_64__)
  43. #if defined(__CYGWIN__)
  44. void *regs[12];
  45. #else
  46. void *regs[8];
  47. #endif
  48. #elif defined(__thumb2__) || defined(__thumb__) || defined(__arm__)
  49. void *regs[10];
  50. #elif defined(__xtensa__)
  51. void *regs[10];
  52. #else
  53. #define MICROPY_NLR_SETJMP (1)
  54. //#warning "No native NLR support for this arch, using setjmp implementation"
  55. #endif
  56. #endif
  57. #if MICROPY_NLR_SETJMP
  58. jmp_buf jmpbuf;
  59. #endif
  60. };
  61. #if MICROPY_NLR_SETJMP
  62. #include "py/mpstate.h"
  63. NORETURN void nlr_setjmp_jump(void *val);
  64. // nlr_push() must be defined as a macro, because "The stack context will be
  65. // invalidated if the function which called setjmp() returns."
  66. #define nlr_push(buf) ((buf)->prev = MP_STATE_THREAD(nlr_top), MP_STATE_THREAD(nlr_top) = (buf), setjmp((buf)->jmpbuf))
  67. #define nlr_pop() { MP_STATE_THREAD(nlr_top) = MP_STATE_THREAD(nlr_top)->prev; }
  68. #define nlr_jump(val) nlr_setjmp_jump(val)
  69. #else
  70. unsigned int nlr_push(nlr_buf_t *);
  71. void nlr_pop(void);
  72. NORETURN void nlr_jump(void *val);
  73. #endif
  74. // This must be implemented by a port. It's called by nlr_jump
  75. // if no nlr buf has been pushed. It must not return, but rather
  76. // should bail out with a fatal error.
  77. NORETURN void nlr_jump_fail(void *val);
  78. // use nlr_raise instead of nlr_jump so that debugging is easier
  79. #ifndef MICROPY_DEBUG_NLR
  80. #define nlr_raise(val) nlr_jump(MP_OBJ_TO_PTR(val))
  81. #else
  82. #include "mpstate.h"
  83. #define nlr_raise(val) \
  84. do { \
  85. /*printf("nlr_raise: nlr_top=%p\n", MP_STATE_THREAD(nlr_top)); \
  86. fflush(stdout);*/ \
  87. void *_val = MP_OBJ_TO_PTR(val); \
  88. assert(_val != NULL); \
  89. assert(mp_obj_is_exception_instance(val)); \
  90. nlr_jump(_val); \
  91. } while (0)
  92. #if !MICROPY_NLR_SETJMP
  93. #define nlr_push(val) \
  94. assert(MP_STATE_THREAD(nlr_top) != val),nlr_push(val)
  95. /*
  96. #define nlr_push(val) \
  97. 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)
  98. #endif
  99. */
  100. #endif
  101. #endif
  102. #endif // MICROPY_INCLUDED_PY_NLR_H