nlrthumb.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. __asm volatile (
  36. "str r4, [r0, #12] \n" // store r4 into nlr_buf
  37. "str r5, [r0, #16] \n" // store r5 into nlr_buf
  38. "str r6, [r0, #20] \n" // store r6 into nlr_buf
  39. "str r7, [r0, #24] \n" // store r7 into nlr_buf
  40. #if !defined(__thumb2__)
  41. "mov r1, r8 \n"
  42. "str r1, [r0, #28] \n" // store r8 into nlr_buf
  43. "mov r1, r9 \n"
  44. "str r1, [r0, #32] \n" // store r9 into nlr_buf
  45. "mov r1, r10 \n"
  46. "str r1, [r0, #36] \n" // store r10 into nlr_buf
  47. "mov r1, r11 \n"
  48. "str r1, [r0, #40] \n" // store r11 into nlr_buf
  49. "mov r1, r13 \n"
  50. "str r1, [r0, #44] \n" // store r13=sp into nlr_buf
  51. "mov r1, lr \n"
  52. "str r1, [r0, #8] \n" // store lr into nlr_buf
  53. #else
  54. "str r8, [r0, #28] \n" // store r8 into nlr_buf
  55. "str r9, [r0, #32] \n" // store r9 into nlr_buf
  56. "str r10, [r0, #36] \n" // store r10 into nlr_buf
  57. "str r11, [r0, #40] \n" // store r11 into nlr_buf
  58. "str r13, [r0, #44] \n" // store r13=sp into nlr_buf
  59. #if MICROPY_NLR_NUM_REGS == 16
  60. "vstr d8, [r0, #48] \n" // store s16-s17 into nlr_buf
  61. "vstr d9, [r0, #56] \n" // store s18-s19 into nlr_buf
  62. "vstr d10, [r0, #64] \n" // store s20-s21 into nlr_buf
  63. #endif
  64. "str lr, [r0, #8] \n" // store lr into nlr_buf
  65. #endif
  66. #if !defined(__thumb2__)
  67. "ldr r1, nlr_push_tail_var \n"
  68. "bx r1 \n" // do the rest in C
  69. ".align 2 \n"
  70. "nlr_push_tail_var: .word nlr_push_tail \n"
  71. #else
  72. #if defined(__APPLE__) || defined(__MACH__)
  73. "b _nlr_push_tail \n" // do the rest in C
  74. #else
  75. "b nlr_push_tail \n" // do the rest in C
  76. #endif
  77. #endif
  78. );
  79. #if !defined(__clang__) && defined(__GNUC__) && (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 8))
  80. // Older versions of gcc give an error when naked functions don't return a value
  81. // Additionally exclude Clang as it also defines __GNUC__ but doesn't need this statement
  82. return 0;
  83. #endif
  84. }
  85. NORETURN void nlr_jump(void *val) {
  86. MP_NLR_JUMP_HEAD(val, top)
  87. __asm volatile (
  88. "mov r0, %0 \n" // r0 points to nlr_buf
  89. "ldr r4, [r0, #12] \n" // load r4 from nlr_buf
  90. "ldr r5, [r0, #16] \n" // load r5 from nlr_buf
  91. "ldr r6, [r0, #20] \n" // load r6 from nlr_buf
  92. "ldr r7, [r0, #24] \n" // load r7 from nlr_buf
  93. #if !defined(__thumb2__)
  94. "ldr r1, [r0, #28] \n" // load r8 from nlr_buf
  95. "mov r8, r1 \n"
  96. "ldr r1, [r0, #32] \n" // load r9 from nlr_buf
  97. "mov r9, r1 \n"
  98. "ldr r1, [r0, #36] \n" // load r10 from nlr_buf
  99. "mov r10, r1 \n"
  100. "ldr r1, [r0, #40] \n" // load r11 from nlr_buf
  101. "mov r11, r1 \n"
  102. "ldr r1, [r0, #44] \n" // load r13=sp from nlr_buf
  103. "mov r13, r1 \n"
  104. "ldr r1, [r0, #8] \n" // load lr from nlr_buf
  105. "mov lr, r1 \n"
  106. #else
  107. "ldr r8, [r0, #28] \n" // load r8 from nlr_buf
  108. "ldr r9, [r0, #32] \n" // load r9 from nlr_buf
  109. "ldr r10, [r0, #36] \n" // load r10 from nlr_buf
  110. "ldr r11, [r0, #40] \n" // load r11 from nlr_buf
  111. "ldr r13, [r0, #44] \n" // load r13=sp from nlr_buf
  112. #if MICROPY_NLR_NUM_REGS == 16
  113. "vldr d8, [r0, #48] \n" // load s16-s17 from nlr_buf
  114. "vldr d9, [r0, #56] \n" // load s18-s19 from nlr_buf
  115. "vldr d10, [r0, #64] \n" // load s20-s21 from nlr_buf
  116. #endif
  117. "ldr lr, [r0, #8] \n" // load lr from nlr_buf
  118. #endif
  119. "movs r0, #1 \n" // return 1, non-local return
  120. "bx lr \n" // return
  121. : // output operands
  122. : "r" (top) // input operands
  123. : "memory" // clobbered registers
  124. );
  125. MP_UNREACHABLE
  126. }
  127. #endif // MICROPY_NLR_THUMB