nlrthumb.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013-2017 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. #include "py/mpstate.h"
  27. #if MICROPY_NLR_THUMB
  28. #undef nlr_push
  29. // We only need the functions here if we are on arm/thumb, and we are not
  30. // using setjmp/longjmp.
  31. //
  32. // For reference, arm/thumb callee save regs are:
  33. // r4-r11, r13=sp
  34. __attribute__((naked)) unsigned int nlr_push(nlr_buf_t *nlr) {
  35. // If you get a linker error here, indicating that a relocation doesn't
  36. // fit, try the following (in that order):
  37. //
  38. // 1. Ensure that nlr.o nlrthumb.o are linked closely together, i.e.
  39. // there aren't too many other files between them in the linker list
  40. // (PY_CORE_O_BASENAME in py/py.mk)
  41. // 2. Set -DMICROPY_NLR_THUMB_USE_LONG_JUMP=1 during the build
  42. //
  43. __asm volatile (
  44. "str r4, [r0, #12] \n" // store r4 into nlr_buf
  45. "str r5, [r0, #16] \n" // store r5 into nlr_buf
  46. "str r6, [r0, #20] \n" // store r6 into nlr_buf
  47. "str r7, [r0, #24] \n" // store r7 into nlr_buf
  48. #if !defined(__thumb2__)
  49. "mov r1, r8 \n"
  50. "str r1, [r0, #28] \n" // store r8 into nlr_buf
  51. "mov r1, r9 \n"
  52. "str r1, [r0, #32] \n" // store r9 into nlr_buf
  53. "mov r1, r10 \n"
  54. "str r1, [r0, #36] \n" // store r10 into nlr_buf
  55. "mov r1, r11 \n"
  56. "str r1, [r0, #40] \n" // store r11 into nlr_buf
  57. "mov r1, r13 \n"
  58. "str r1, [r0, #44] \n" // store r13=sp into nlr_buf
  59. "mov r1, lr \n"
  60. "str r1, [r0, #8] \n" // store lr into nlr_buf
  61. #else
  62. "str r8, [r0, #28] \n" // store r8 into nlr_buf
  63. "str r9, [r0, #32] \n" // store r9 into nlr_buf
  64. "str r10, [r0, #36] \n" // store r10 into nlr_buf
  65. "str r11, [r0, #40] \n" // store r11 into nlr_buf
  66. "str r13, [r0, #44] \n" // store r13=sp into nlr_buf
  67. #if MICROPY_NLR_NUM_REGS == 16
  68. "vstr d8, [r0, #48] \n" // store s16-s17 into nlr_buf
  69. "vstr d9, [r0, #56] \n" // store s18-s19 into nlr_buf
  70. "vstr d10, [r0, #64] \n" // store s20-s21 into nlr_buf
  71. #endif
  72. "str lr, [r0, #8] \n" // store lr into nlr_buf
  73. #endif
  74. #if MICROPY_NLR_THUMB_USE_LONG_JUMP
  75. "ldr r1, nlr_push_tail_var \n"
  76. "bx r1 \n" // do the rest in C
  77. ".align 2 \n"
  78. "nlr_push_tail_var: .word nlr_push_tail \n"
  79. #else
  80. #if defined(__APPLE__) || defined(__MACH__)
  81. "b _nlr_push_tail \n" // do the rest in C
  82. #else
  83. "b nlr_push_tail \n" // do the rest in C
  84. #endif
  85. #endif
  86. );
  87. #if !defined(__clang__) && defined(__GNUC__) && (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 8))
  88. // Older versions of gcc give an error when naked functions don't return a value
  89. // Additionally exclude Clang as it also defines __GNUC__ but doesn't need this statement
  90. return 0;
  91. #endif
  92. }
  93. NORETURN void nlr_jump(void *val) {
  94. MP_NLR_JUMP_HEAD(val, top)
  95. __asm volatile (
  96. "mov r0, %0 \n" // r0 points to nlr_buf
  97. "ldr r4, [r0, #12] \n" // load r4 from nlr_buf
  98. "ldr r5, [r0, #16] \n" // load r5 from nlr_buf
  99. "ldr r6, [r0, #20] \n" // load r6 from nlr_buf
  100. "ldr r7, [r0, #24] \n" // load r7 from nlr_buf
  101. #if !defined(__thumb2__)
  102. "ldr r1, [r0, #28] \n" // load r8 from nlr_buf
  103. "mov r8, r1 \n"
  104. "ldr r1, [r0, #32] \n" // load r9 from nlr_buf
  105. "mov r9, r1 \n"
  106. "ldr r1, [r0, #36] \n" // load r10 from nlr_buf
  107. "mov r10, r1 \n"
  108. "ldr r1, [r0, #40] \n" // load r11 from nlr_buf
  109. "mov r11, r1 \n"
  110. "ldr r1, [r0, #44] \n" // load r13=sp from nlr_buf
  111. "mov r13, r1 \n"
  112. "ldr r1, [r0, #8] \n" // load lr from nlr_buf
  113. "mov lr, r1 \n"
  114. #else
  115. "ldr r8, [r0, #28] \n" // load r8 from nlr_buf
  116. "ldr r9, [r0, #32] \n" // load r9 from nlr_buf
  117. "ldr r10, [r0, #36] \n" // load r10 from nlr_buf
  118. "ldr r11, [r0, #40] \n" // load r11 from nlr_buf
  119. "ldr r13, [r0, #44] \n" // load r13=sp from nlr_buf
  120. #if MICROPY_NLR_NUM_REGS == 16
  121. "vldr d8, [r0, #48] \n" // load s16-s17 from nlr_buf
  122. "vldr d9, [r0, #56] \n" // load s18-s19 from nlr_buf
  123. "vldr d10, [r0, #64] \n" // load s20-s21 from nlr_buf
  124. #endif
  125. "ldr lr, [r0, #8] \n" // load lr from nlr_buf
  126. #endif
  127. "movs r0, #1 \n" // return 1, non-local return
  128. "bx lr \n" // return
  129. : // output operands
  130. : "r" (top) // input operands
  131. : "memory" // clobbered registers
  132. );
  133. MP_UNREACHABLE
  134. }
  135. #endif // MICROPY_NLR_THUMB