Skip to content

Commit b184b6a

Browse files
stinosdpgeorge
authored andcommitted
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 micropython#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)
1 parent 8041de5 commit b184b6a

File tree

5 files changed

+12
-22
lines changed

5 files changed

+12
-22
lines changed

ports/windows/Makefile

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,6 @@ CFLAGS_MOD += -DMICROPY_USE_READLINE=2
5050
LDFLAGS_MOD += -lreadline
5151
endif
5252

53-
ifeq ($(CROSS_COMPILE),x86_64-w64-mingw32-)
54-
CFLAGS_MOD += -DMICROPY_NLR_SETJMP=1
55-
endif
56-
5753
LIB += -lws2_32
5854

5955
# List of sources for qstr extraction

py/nlr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

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

py/nlr.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,19 @@
3636

3737
// If MICROPY_NLR_SETJMP is not enabled then auto-detect the machine arch
3838
#if !MICROPY_NLR_SETJMP
39+
// A lot of nlr-related things need different treatment on Windows
40+
#if defined(_WIN32) || defined(__CYGWIN__)
41+
#define MICROPY_NLR_OS_WINDOWS 1
42+
#else
43+
#define MICROPY_NLR_OS_WINDOWS 0
44+
#endif
3945
#if defined(__i386__)
4046
#define MICROPY_NLR_X86 (1)
4147
#define MICROPY_NLR_NUM_REGS (6)
4248
#elif defined(__x86_64__)
4349
#define MICROPY_NLR_X64 (1)
44-
#if defined(__CYGWIN__)
45-
#define MICROPY_NLR_NUM_REGS (12)
50+
#if MICROPY_NLR_OS_WINDOWS
51+
#define MICROPY_NLR_NUM_REGS (10)
4652
#else
4753
#define MICROPY_NLR_NUM_REGS (8)
4854
#endif

py/nlrx64.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,12 @@
3333
// x86-64 callee-save registers are:
3434
// rbx, rbp, rsp, r12, r13, r14, r15
3535

36-
#if defined(_WIN32) || defined(__CYGWIN__)
37-
#define NLR_OS_WINDOWS 1
38-
#else
39-
#define NLR_OS_WINDOWS 0
40-
#endif
41-
4236
__attribute__((used)) unsigned int nlr_push_tail(nlr_buf_t *nlr);
4337

4438
unsigned int nlr_push(nlr_buf_t *nlr) {
4539
(void)nlr;
4640

47-
#if NLR_OS_WINDOWS
41+
#if MICROPY_NLR_OS_WINDOWS
4842

4943
__asm volatile (
5044
"movq (%rsp), %rax \n" // load return %rip
@@ -93,7 +87,7 @@ NORETURN void nlr_jump(void *val) {
9387

9488
__asm volatile (
9589
"movq %0, %%rcx \n" // %rcx points to nlr_buf
96-
#if NLR_OS_WINDOWS
90+
#if MICROPY_NLR_OS_WINDOWS
9791
"movq 88(%%rcx), %%rsi \n" // load saved %rsi
9892
"movq 80(%%rcx), %%rdi \n" // load saved %rdr
9993
#endif

py/nlrx86.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,7 @@
3333
// For reference, x86 callee save regs are:
3434
// ebx, esi, edi, ebp, esp, eip
3535

36-
#if defined(_WIN32) || defined(__CYGWIN__)
37-
#define NLR_OS_WINDOWS 1
38-
#else
39-
#define NLR_OS_WINDOWS 0
40-
#endif
41-
42-
#if NLR_OS_WINDOWS
36+
#if MICROPY_NLR_OS_WINDOWS
4337
unsigned int nlr_push_tail(nlr_buf_t *nlr) asm("nlr_push_tail");
4438
#else
4539
__attribute__((used)) unsigned int nlr_push_tail(nlr_buf_t *nlr);

0 commit comments

Comments
 (0)