summaryrefslogtreecommitdiff
path: root/py/nlrx86.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2017-12-18 18:57:15 +1100
committerDamien George <damien.p.george@gmail.com>2017-12-20 15:42:06 +1100
commit6a3a742a6c9caaa2be0fd0aac7a5df4ac816081c (patch)
treecb91633e1592d474a5bc51db97f6442616d1cf63 /py/nlrx86.c
parentd8d633f15658911369d7b1b912777fa74efd3ea6 (diff)
py/nlr: Factor out common NLR code to generic functions.
Each NLR implementation (Thumb, x86, x64, xtensa, setjmp) duplicates a lot of the NLR code, specifically that dealing with pushing and popping the NLR pointer to maintain the linked-list of NLR buffers. This patch factors all of that code out of the specific implementations into generic functions in nlr.c. This eliminates duplicated code. The factoring also allows to make the machine-specific NLR code pure assembler code, thus allowing nlrthumb.c to use naked function attributes in the correct way (naked functions can only have basic inline assembler code in them). There is a small overhead introduced (typically 1 machine instruction) because now the generic nlr_jump() must call nlr_jump_tail() rather than them being one combined function.
Diffstat (limited to 'py/nlrx86.c')
-rw-r--r--py/nlrx86.c63
1 files changed, 13 insertions, 50 deletions
diff --git a/py/nlrx86.c b/py/nlrx86.c
index 3a27460eb..cc37f72af 100644
--- a/py/nlrx86.c
+++ b/py/nlrx86.c
@@ -26,25 +26,13 @@
#include "py/mpstate.h"
-#if !MICROPY_NLR_SETJMP && defined(__i386__)
+#if MICROPY_NLR_X86
#undef nlr_push
// For reference, x86 callee save regs are:
// ebx, esi, edi, ebp, esp, eip
-#if defined(_WIN32) || defined(__CYGWIN__)
-#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");
-#else
-__attribute__((used)) unsigned int nlr_push_tail(nlr_buf_t *nlr);
-#endif
-
unsigned int nlr_push(nlr_buf_t *nlr) {
(void)nlr;
@@ -70,48 +58,23 @@ unsigned int nlr_push(nlr_buf_t *nlr) {
return 0; // needed to silence compiler warning
}
-__attribute__((used)) unsigned int nlr_push_tail(nlr_buf_t *nlr) {
- nlr_buf_t **top = &MP_STATE_THREAD(nlr_top);
- nlr->prev = *top;
- MP_NLR_SAVE_PYSTACK(nlr);
- *top = nlr;
- return 0; // normal return
-}
-
-void nlr_pop(void) {
- nlr_buf_t **top = &MP_STATE_THREAD(nlr_top);
- *top = (*top)->prev;
-}
-
-NORETURN void nlr_jump(void *val) {
- nlr_buf_t **top_ptr = &MP_STATE_THREAD(nlr_top);
- nlr_buf_t *top = *top_ptr;
- if (top == NULL) {
- nlr_jump_fail(val);
- }
-
- top->ret_val = val;
- MP_NLR_RESTORE_PYSTACK(top);
- *top_ptr = top->prev;
+NORETURN void nlr_jump_tail(nlr_buf_t *top) {
+ (void)top;
__asm volatile (
- "mov %0, %%edx \n" // %edx points to nlr_buf
- "mov 28(%%edx), %%esi \n" // load saved %esi
- "mov 24(%%edx), %%edi \n" // load saved %edi
- "mov 20(%%edx), %%ebx \n" // load saved %ebx
- "mov 16(%%edx), %%esp \n" // load saved %esp
- "mov 12(%%edx), %%ebp \n" // load saved %ebp
- "mov 8(%%edx), %%eax \n" // load saved %eip
- "mov %%eax, (%%esp) \n" // store saved %eip to stack
- "xor %%eax, %%eax \n" // clear return register
- "inc %%al \n" // increase to make 1, non-local return
+ "mov 28(%edx), %esi \n" // load saved %esi
+ "mov 24(%edx), %edi \n" // load saved %edi
+ "mov 20(%edx), %ebx \n" // load saved %ebx
+ "mov 16(%edx), %esp \n" // load saved %esp
+ "mov 12(%edx), %ebp \n" // load saved %ebp
+ "mov 8(%edx), %eax \n" // load saved %eip
+ "mov %eax, (%esp) \n" // store saved %eip to stack
+ "xor %eax, %eax \n" // clear return register
+ "inc %al \n" // increase to make 1, non-local return
"ret \n" // return
- : // output operands
- : "r"(top) // input operands
- : // clobbered registers
);
for (;;); // needed to silence compiler warning
}
-#endif // !MICROPY_NLR_SETJMP && defined(__i386__)
+#endif // MICROPY_NLR_X86