diff --git a/py/nlr.h b/py/nlr.h
index 1235f1460948de3d8e61e4a8fc288e8c92604cf5..bd9fcc8847c233e7ca3f3067409a383342515ddc 100644
--- a/py/nlr.h
+++ b/py/nlr.h
@@ -30,29 +30,28 @@
 // exception handling, basically a stack of setjmp/longjmp buffers
 
 #include <limits.h>
-#include <setjmp.h>
 #include <assert.h>
 
 #include "py/mpconfig.h"
 
-typedef struct _nlr_buf_t nlr_buf_t;
-struct _nlr_buf_t {
-    // the entries here must all be machine word size
-    nlr_buf_t *prev;
-    void *ret_val; // always a concrete object (an exception instance)
-#if !defined(MICROPY_NLR_SETJMP) || !MICROPY_NLR_SETJMP
+// If MICROPY_NLR_SETJMP is not enabled then auto-detect the machine arch
+#if !MICROPY_NLR_SETJMP
 #if defined(__i386__)
-    void *regs[6];
+    #define MICROPY_NLR_X86 (1)
+    #define MICROPY_NLR_NUM_REGS (6)
 #elif defined(__x86_64__)
-  #if defined(__CYGWIN__)
-    void *regs[12];
-  #else
-    void *regs[8];
-  #endif
+    #define MICROPY_NLR_X64 (1)
+    #if defined(__CYGWIN__)
+        #define MICROPY_NLR_NUM_REGS (12)
+    #else
+        #define MICROPY_NLR_NUM_REGS (8)
+    #endif
 #elif defined(__thumb2__) || defined(__thumb__) || defined(__arm__)
-    void *regs[10];
+    #define MICROPY_NLR_THUMB (1)
+    #define MICROPY_NLR_NUM_REGS (10)
 #elif defined(__xtensa__)
-    void *regs[10];
+    #define MICROPY_NLR_XTENSA (1)
+    #define MICROPY_NLR_NUM_REGS (10)
 #else
     #define MICROPY_NLR_SETJMP (1)
     //#warning "No native NLR support for this arch, using setjmp implementation"
@@ -60,9 +59,21 @@ struct _nlr_buf_t {
 #endif
 
 #if MICROPY_NLR_SETJMP
-    jmp_buf jmpbuf;
+#include <setjmp.h>
 #endif
 
+typedef struct _nlr_buf_t nlr_buf_t;
+struct _nlr_buf_t {
+    // the entries here must all be machine word size
+    nlr_buf_t *prev;
+    void *ret_val; // always a concrete object (an exception instance)
+
+    #if MICROPY_NLR_SETJMP
+    jmp_buf jmpbuf;
+    #else
+    void *regs[MICROPY_NLR_NUM_REGS];
+    #endif
+
     #if MICROPY_ENABLE_PYSTACK
     void *pystack;
     #endif
@@ -123,7 +134,6 @@ NORETURN void nlr_jump_fail(void *val);
 /*
 #define nlr_push(val) \
     printf("nlr_push: before: nlr_top=%p, val=%p\n", MP_STATE_THREAD(nlr_top), val),assert(MP_STATE_THREAD(nlr_top) != val),nlr_push(val)
-#endif
 */
 #endif
 
diff --git a/py/nlrthumb.c b/py/nlrthumb.c
index 18d31eb70673fc9fcf4f343a49758ba84d9220aa..cc081e3ffbef7af79db9fcb869a54a07ca922fb6 100644
--- a/py/nlrthumb.c
+++ b/py/nlrthumb.c
@@ -26,7 +26,7 @@
 
 #include "py/mpstate.h"
 
-#if (!defined(MICROPY_NLR_SETJMP) || !MICROPY_NLR_SETJMP) && (defined(__thumb2__) || defined(__thumb__) || defined(__arm__))
+#if MICROPY_NLR_THUMB
 
 #undef nlr_push
 
@@ -148,4 +148,4 @@ NORETURN void nlr_jump(void *val) {
     #endif
 }
 
-#endif // (!defined(MICROPY_NLR_SETJMP) || !MICROPY_NLR_SETJMP) && (defined(__thumb2__) || defined(__thumb__) || defined(__arm__))
+#endif // MICROPY_NLR_THUMB
diff --git a/py/nlrx64.c b/py/nlrx64.c
index ddcd76166514a3405cc13757efd62d934236068e..927b215912b0d60778a8f4d018464c44241ae8fa 100644
--- a/py/nlrx64.c
+++ b/py/nlrx64.c
@@ -26,7 +26,7 @@
 
 #include "py/mpstate.h"
 
-#if !MICROPY_NLR_SETJMP && defined(__x86_64__)
+#if MICROPY_NLR_X64
 
 #undef nlr_push
 
@@ -138,4 +138,4 @@ NORETURN void nlr_jump(void *val) {
     for (;;); // needed to silence compiler warning
 }
 
-#endif // !MICROPY_NLR_SETJMP && defined(__x86_64__)
+#endif // MICROPY_NLR_X64
diff --git a/py/nlrx86.c b/py/nlrx86.c
index 3a27460eb64d13656fd01306a2cf0c2d5d5fde2e..0e03eef6feb2b5da1bb18113b5e1c47d67dc7e77 100644
--- a/py/nlrx86.c
+++ b/py/nlrx86.c
@@ -26,7 +26,7 @@
 
 #include "py/mpstate.h"
 
-#if !MICROPY_NLR_SETJMP && defined(__i386__)
+#if MICROPY_NLR_X86
 
 #undef nlr_push
 
@@ -114,4 +114,4 @@ NORETURN void nlr_jump(void *val) {
     for (;;); // needed to silence compiler warning
 }
 
-#endif // !MICROPY_NLR_SETJMP && defined(__i386__)
+#endif // MICROPY_NLR_X86
diff --git a/py/nlrxtensa.c b/py/nlrxtensa.c
index 5a969fc87cbeed7a5ff2252859f0d69ab1e6b639..73f14385d00edf11afe09671b4fb07b17a519ea9 100644
--- a/py/nlrxtensa.c
+++ b/py/nlrxtensa.c
@@ -26,7 +26,7 @@
 
 #include "py/mpstate.h"
 
-#if !MICROPY_NLR_SETJMP && defined(__xtensa__)
+#if MICROPY_NLR_XTENSA
 
 #undef nlr_push
 
@@ -101,4 +101,4 @@ NORETURN void nlr_jump(void *val) {
     for (;;); // needed to silence compiler warning
 }
 
-#endif // !MICROPY_NLR_SETJMP && defined(__xtensa__)
+#endif // MICROPY_NLR_XTENSA