py/nlr: Fix nlr functions for 64bit ports built with gcc on Windows

The number of registers used should be 10, not 12, to match the assembly
code in nlrx64.c. With this change the 64bit mingw builds don't need to
use the setjmp implementation, and this fixes miscellaneous crashes and
assertion failures as reported in #1751 for instance.

To avoid mistakes in the future where something gcc-related for Windows
only gets fixed for one particular compiler/environment combination,
make use of a MICROPY_NLR_OS_WINDOWS macro.

To make sure everything nlr-related is now ok when built with gcc this
has been verified with:
- unix port built with gcc on Cygwin (i686-pc-cygwin-gcc and
  x86_64-pc-cygwin-gcc, version 6.4.0)
- windows port built with mingw-w64's gcc from Cygwin
 (i686-w64-mingw32-gcc and x86_64-w64-mingw32-gcc, version 6.4.0)
 and MSYS2 (like the ones on Cygwin but version 7.2.0)
This commit is contained in:
stijn 2017-12-26 10:52:09 +01:00 committed by Damien George
parent 8041de59fe
commit b184b6ae53
5 changed files with 12 additions and 22 deletions

View File

@ -50,10 +50,6 @@ CFLAGS_MOD += -DMICROPY_USE_READLINE=2
LDFLAGS_MOD += -lreadline LDFLAGS_MOD += -lreadline
endif endif
ifeq ($(CROSS_COMPILE),x86_64-w64-mingw32-)
CFLAGS_MOD += -DMICROPY_NLR_SETJMP=1
endif
LIB += -lws2_32 LIB += -lws2_32
# List of sources for qstr extraction # List of sources for qstr extraction

View File

@ -28,7 +28,7 @@
#if !MICROPY_NLR_SETJMP #if !MICROPY_NLR_SETJMP
// When not using setjmp, nlr_push_tail is called from inline asm so needs special c // When not using setjmp, nlr_push_tail is called from inline asm so needs special c
#if MICROPY_NLR_X86 && (defined(_WIN32) || defined(__CYGWIN__)) #if MICROPY_NLR_X86 && MICROPY_NLR_OS_WINDOWS
// On these 32-bit platforms make sure nlr_push_tail doesn't have a leading undersco // On these 32-bit platforms make sure nlr_push_tail doesn't have a leading undersco
unsigned int nlr_push_tail(nlr_buf_t *nlr) asm("nlr_push_tail"); unsigned int nlr_push_tail(nlr_buf_t *nlr) asm("nlr_push_tail");
#else #else

View File

@ -36,13 +36,19 @@
// If MICROPY_NLR_SETJMP is not enabled then auto-detect the machine arch // If MICROPY_NLR_SETJMP is not enabled then auto-detect the machine arch
#if !MICROPY_NLR_SETJMP #if !MICROPY_NLR_SETJMP
// A lot of nlr-related things need different treatment on Windows
#if defined(_WIN32) || defined(__CYGWIN__)
#define MICROPY_NLR_OS_WINDOWS 1
#else
#define MICROPY_NLR_OS_WINDOWS 0
#endif
#if defined(__i386__) #if defined(__i386__)
#define MICROPY_NLR_X86 (1) #define MICROPY_NLR_X86 (1)
#define MICROPY_NLR_NUM_REGS (6) #define MICROPY_NLR_NUM_REGS (6)
#elif defined(__x86_64__) #elif defined(__x86_64__)
#define MICROPY_NLR_X64 (1) #define MICROPY_NLR_X64 (1)
#if defined(__CYGWIN__) #if MICROPY_NLR_OS_WINDOWS
#define MICROPY_NLR_NUM_REGS (12) #define MICROPY_NLR_NUM_REGS (10)
#else #else
#define MICROPY_NLR_NUM_REGS (8) #define MICROPY_NLR_NUM_REGS (8)
#endif #endif

View File

@ -33,18 +33,12 @@
// x86-64 callee-save registers are: // x86-64 callee-save registers are:
// rbx, rbp, rsp, r12, r13, r14, r15 // rbx, rbp, rsp, r12, r13, r14, r15
#if defined(_WIN32) || defined(__CYGWIN__)
#define NLR_OS_WINDOWS 1
#else
#define NLR_OS_WINDOWS 0
#endif
__attribute__((used)) unsigned int nlr_push_tail(nlr_buf_t *nlr); __attribute__((used)) unsigned int nlr_push_tail(nlr_buf_t *nlr);
unsigned int nlr_push(nlr_buf_t *nlr) { unsigned int nlr_push(nlr_buf_t *nlr) {
(void)nlr; (void)nlr;
#if NLR_OS_WINDOWS #if MICROPY_NLR_OS_WINDOWS
__asm volatile ( __asm volatile (
"movq (%rsp), %rax \n" // load return %rip "movq (%rsp), %rax \n" // load return %rip
@ -93,7 +87,7 @@ NORETURN void nlr_jump(void *val) {
__asm volatile ( __asm volatile (
"movq %0, %%rcx \n" // %rcx points to nlr_buf "movq %0, %%rcx \n" // %rcx points to nlr_buf
#if NLR_OS_WINDOWS #if MICROPY_NLR_OS_WINDOWS
"movq 88(%%rcx), %%rsi \n" // load saved %rsi "movq 88(%%rcx), %%rsi \n" // load saved %rsi
"movq 80(%%rcx), %%rdi \n" // load saved %rdr "movq 80(%%rcx), %%rdi \n" // load saved %rdr
#endif #endif

View File

@ -33,13 +33,7 @@
// For reference, x86 callee save regs are: // For reference, x86 callee save regs are:
// ebx, esi, edi, ebp, esp, eip // ebx, esi, edi, ebp, esp, eip
#if defined(_WIN32) || defined(__CYGWIN__) #if MICROPY_NLR_OS_WINDOWS
#define NLR_OS_WINDOWS 1
#else
#define NLR_OS_WINDOWS 0
#endif
#if NLR_OS_WINDOWS
unsigned int nlr_push_tail(nlr_buf_t *nlr) asm("nlr_push_tail"); unsigned int nlr_push_tail(nlr_buf_t *nlr) asm("nlr_push_tail");
#else #else
__attribute__((used)) unsigned int nlr_push_tail(nlr_buf_t *nlr); __attribute__((used)) unsigned int nlr_push_tail(nlr_buf_t *nlr);