nlrx86.c 4.1 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-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_SETJMP && defined(__i386__)
  28. #undef nlr_push
  29. // For reference, x86 callee save regs are:
  30. // ebx, esi, edi, ebp, esp, eip
  31. #if defined(_WIN32) || defined(__CYGWIN__)
  32. #define NLR_OS_WINDOWS 1
  33. #else
  34. #define NLR_OS_WINDOWS 0
  35. #endif
  36. #if NLR_OS_WINDOWS
  37. unsigned int nlr_push_tail(nlr_buf_t *nlr) asm("nlr_push_tail");
  38. #else
  39. __attribute__((used)) unsigned int nlr_push_tail(nlr_buf_t *nlr);
  40. #endif
  41. unsigned int nlr_push(nlr_buf_t *nlr) {
  42. (void)nlr;
  43. __asm volatile (
  44. // Check for Zephyr, which uses a different calling convention
  45. // by default.
  46. // TODE: Better support for various x86 calling conventions
  47. // (unfortunately, __attribute__((naked)) is not supported on x86).
  48. #if !(defined(__ZEPHYR__) || defined(__ANDROID__))
  49. "pop %ebp \n" // undo function's prelude
  50. #endif
  51. "mov 4(%esp), %edx \n" // load nlr_buf
  52. "mov (%esp), %eax \n" // load return %eip
  53. "mov %eax, 8(%edx) \n" // store %eip into nlr_buf
  54. "mov %ebp, 12(%edx) \n" // store %ebp into nlr_buf
  55. "mov %esp, 16(%edx) \n" // store %esp into nlr_buf
  56. "mov %ebx, 20(%edx) \n" // store %ebx into nlr_buf
  57. "mov %edi, 24(%edx) \n" // store %edi into nlr_buf
  58. "mov %esi, 28(%edx) \n" // store %esi into nlr_buf
  59. "jmp nlr_push_tail \n" // do the rest in C
  60. );
  61. return 0; // needed to silence compiler warning
  62. }
  63. __attribute__((used)) unsigned int nlr_push_tail(nlr_buf_t *nlr) {
  64. nlr_buf_t **top = &MP_STATE_THREAD(nlr_top);
  65. nlr->prev = *top;
  66. *top = nlr;
  67. return 0; // normal return
  68. }
  69. void nlr_pop(void) {
  70. nlr_buf_t **top = &MP_STATE_THREAD(nlr_top);
  71. *top = (*top)->prev;
  72. }
  73. NORETURN void nlr_jump(void *val) {
  74. nlr_buf_t **top_ptr = &MP_STATE_THREAD(nlr_top);
  75. nlr_buf_t *top = *top_ptr;
  76. if (top == NULL) {
  77. nlr_jump_fail(val);
  78. }
  79. top->ret_val = val;
  80. *top_ptr = top->prev;
  81. __asm volatile (
  82. "mov %0, %%edx \n" // %edx points to nlr_buf
  83. "mov 28(%%edx), %%esi \n" // load saved %esi
  84. "mov 24(%%edx), %%edi \n" // load saved %edi
  85. "mov 20(%%edx), %%ebx \n" // load saved %ebx
  86. "mov 16(%%edx), %%esp \n" // load saved %esp
  87. "mov 12(%%edx), %%ebp \n" // load saved %ebp
  88. "mov 8(%%edx), %%eax \n" // load saved %eip
  89. "mov %%eax, (%%esp) \n" // store saved %eip to stack
  90. "xor %%eax, %%eax \n" // clear return register
  91. "inc %%al \n" // increase to make 1, non-local return
  92. "ret \n" // return
  93. : // output operands
  94. : "r"(top) // input operands
  95. : // clobbered registers
  96. );
  97. for (;;); // needed to silence compiler warning
  98. }
  99. #endif // !MICROPY_NLR_SETJMP && defined(__i386__)