nlrx86.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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_X86
  28. #undef nlr_push
  29. // For reference, x86 callee save regs are:
  30. // ebx, esi, edi, ebp, esp, eip
  31. #if MICROPY_NLR_OS_WINDOWS
  32. unsigned int nlr_push_tail(nlr_buf_t *nlr) asm ("nlr_push_tail");
  33. #else
  34. __attribute__((used)) unsigned int nlr_push_tail(nlr_buf_t *nlr);
  35. #endif
  36. #if !defined(__clang__) && defined(__GNUC__) && __GNUC__ >= 8
  37. // Since gcc 8.0 the naked attribute is supported
  38. #define USE_NAKED (1)
  39. #define UNDO_PRELUDE (0)
  40. #elif defined(__ZEPHYR__) || defined(__ANDROID__)
  41. // Zephyr and Android use a different calling convention by default
  42. #define USE_NAKED (0)
  43. #define UNDO_PRELUDE (0)
  44. #else
  45. #define USE_NAKED (0)
  46. #define UNDO_PRELUDE (1)
  47. #endif
  48. #if USE_NAKED
  49. __attribute__((naked))
  50. #endif
  51. unsigned int nlr_push(nlr_buf_t *nlr) {
  52. (void)nlr;
  53. __asm volatile (
  54. #if UNDO_PRELUDE
  55. "pop %ebp \n" // undo function's prelude
  56. #endif
  57. "mov 4(%esp), %edx \n" // load nlr_buf
  58. "mov (%esp), %eax \n" // load return %eip
  59. "mov %eax, 8(%edx) \n" // store %eip into nlr_buf
  60. "mov %ebp, 12(%edx) \n" // store %ebp into nlr_buf
  61. "mov %esp, 16(%edx) \n" // store %esp into nlr_buf
  62. "mov %ebx, 20(%edx) \n" // store %ebx into nlr_buf
  63. "mov %edi, 24(%edx) \n" // store %edi into nlr_buf
  64. "mov %esi, 28(%edx) \n" // store %esi into nlr_buf
  65. "jmp nlr_push_tail \n" // do the rest in C
  66. );
  67. #if !USE_NAKED
  68. return 0; // needed to silence compiler warning
  69. #endif
  70. }
  71. NORETURN void nlr_jump(void *val) {
  72. MP_NLR_JUMP_HEAD(val, top)
  73. __asm volatile (
  74. "mov %0, %%edx \n" // %edx points to nlr_buf
  75. "mov 28(%%edx), %%esi \n" // load saved %esi
  76. "mov 24(%%edx), %%edi \n" // load saved %edi
  77. "mov 20(%%edx), %%ebx \n" // load saved %ebx
  78. "mov 16(%%edx), %%esp \n" // load saved %esp
  79. "mov 12(%%edx), %%ebp \n" // load saved %ebp
  80. "mov 8(%%edx), %%eax \n" // load saved %eip
  81. "mov %%eax, (%%esp) \n" // store saved %eip to stack
  82. "xor %%eax, %%eax \n" // clear return register
  83. "inc %%al \n" // increase to make 1, non-local return
  84. "ret \n" // return
  85. : // output operands
  86. : "r" (top) // input operands
  87. : "memory" // clobbered registers
  88. );
  89. MP_UNREACHABLE
  90. }
  91. #endif // MICROPY_NLR_X86