nlrx64.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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_X64
  28. #undef nlr_push
  29. // x86-64 callee-save registers are:
  30. // rbx, rbp, rsp, r12, r13, r14, r15
  31. __attribute__((used)) unsigned int nlr_push_tail(nlr_buf_t *nlr);
  32. #if !MICROPY_NLR_OS_WINDOWS
  33. #if defined(__clang__) || (defined(__GNUC__) && __GNUC__ >= 8)
  34. #define USE_NAKED 1
  35. #else
  36. // On older gcc the equivalent here is to force omit-frame-pointer
  37. __attribute__((optimize("omit-frame-pointer")))
  38. #endif
  39. #endif
  40. #if !defined(USE_NAKED)
  41. #define USE_NAKED 0
  42. #endif
  43. #if USE_NAKED
  44. // nlr_push prelude should never push frame pointer register ebp onto the stack
  45. __attribute__((naked))
  46. #endif
  47. unsigned int nlr_push(nlr_buf_t *nlr) {
  48. #if !USE_NAKED
  49. (void)nlr;
  50. #endif
  51. #if MICROPY_NLR_OS_WINDOWS
  52. __asm volatile (
  53. "movq (%rsp), %rax \n" // load return %rip
  54. "movq %rax, 16(%rcx) \n" // store %rip into nlr_buf
  55. "movq %rbp, 24(%rcx) \n" // store %rbp into nlr_buf
  56. "movq %rsp, 32(%rcx) \n" // store %rsp into nlr_buf
  57. "movq %rbx, 40(%rcx) \n" // store %rbx into nlr_buf
  58. "movq %r12, 48(%rcx) \n" // store %r12 into nlr_buf
  59. "movq %r13, 56(%rcx) \n" // store %r13 into nlr_buf
  60. "movq %r14, 64(%rcx) \n" // store %r14 into nlr_buf
  61. "movq %r15, 72(%rcx) \n" // store %r15 into nlr_buf
  62. "movq %rdi, 80(%rcx) \n" // store %rdr into nlr_buf
  63. "movq %rsi, 88(%rcx) \n" // store %rsi into nlr_buf
  64. "jmp nlr_push_tail \n" // do the rest in C
  65. );
  66. #else
  67. __asm volatile (
  68. "movq (%rsp), %rax \n" // load return %rip
  69. "movq %rax, 16(%rdi) \n" // store %rip into nlr_buf
  70. "movq %rbp, 24(%rdi) \n" // store %rbp into nlr_buf
  71. "movq %rsp, 32(%rdi) \n" // store %rsp into nlr_buf
  72. "movq %rbx, 40(%rdi) \n" // store %rbx into nlr_buf
  73. "movq %r12, 48(%rdi) \n" // store %r12 into nlr_buf
  74. "movq %r13, 56(%rdi) \n" // store %r13 into nlr_buf
  75. "movq %r14, 64(%rdi) \n" // store %r14 into nlr_buf
  76. "movq %r15, 72(%rdi) \n" // store %r15 into nlr_buf
  77. #if defined(__APPLE__) && defined(__MACH__)
  78. "jmp _nlr_push_tail \n" // do the rest in C
  79. #else
  80. "jmp nlr_push_tail \n" // do the rest in C
  81. #endif
  82. );
  83. #endif
  84. #if !USE_NAKED
  85. return 0; // needed to silence compiler warning
  86. #endif
  87. }
  88. NORETURN void nlr_jump(void *val) {
  89. MP_NLR_JUMP_HEAD(val, top)
  90. __asm volatile (
  91. "movq %0, %%rcx \n" // %rcx points to nlr_buf
  92. #if MICROPY_NLR_OS_WINDOWS
  93. "movq 88(%%rcx), %%rsi \n" // load saved %rsi
  94. "movq 80(%%rcx), %%rdi \n" // load saved %rdi
  95. #endif
  96. "movq 72(%%rcx), %%r15 \n" // load saved %r15
  97. "movq 64(%%rcx), %%r14 \n" // load saved %r14
  98. "movq 56(%%rcx), %%r13 \n" // load saved %r13
  99. "movq 48(%%rcx), %%r12 \n" // load saved %r12
  100. "movq 40(%%rcx), %%rbx \n" // load saved %rbx
  101. "movq 32(%%rcx), %%rsp \n" // load saved %rsp
  102. "movq 24(%%rcx), %%rbp \n" // load saved %rbp
  103. "movq 16(%%rcx), %%rax \n" // load saved %rip
  104. "movq %%rax, (%%rsp) \n" // store saved %rip to stack
  105. "xorq %%rax, %%rax \n" // clear return register
  106. "inc %%al \n" // increase to make 1, non-local return
  107. "ret \n" // return
  108. : // output operands
  109. : "r" (top) // input operands
  110. : "memory" // clobbered registers
  111. );
  112. MP_UNREACHABLE
  113. }
  114. #endif // MICROPY_NLR_X64