blob: 3a27460eb64d13656fd01306a2cf0c2d5d5fde2e [file] [log] [blame]
Damien Georgef0dddb62017-03-01 23:20:13 +11001/*
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
27#include "py/mpstate.h"
Damien Georgef0dddb62017-03-01 23:20:13 +110028
Paul Sokolovsky096e9672017-12-26 18:39:51 +020029#if !MICROPY_NLR_SETJMP && defined(__i386__)
Damien Georgef0dddb62017-03-01 23:20:13 +110030
31#undef nlr_push
32
33// For reference, x86 callee save regs are:
34// ebx, esi, edi, ebp, esp, eip
35
Paul Sokolovsky096e9672017-12-26 18:39:51 +020036#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
43unsigned int nlr_push_tail(nlr_buf_t *nlr) asm("nlr_push_tail");
44#else
45__attribute__((used)) unsigned int nlr_push_tail(nlr_buf_t *nlr);
46#endif
47
Damien Georgef0dddb62017-03-01 23:20:13 +110048unsigned int nlr_push(nlr_buf_t *nlr) {
49 (void)nlr;
50
51 __asm volatile (
Paul Sokolovskyfd49ff92017-03-07 16:40:00 +010052 // Check for Zephyr, which uses a different calling convention
53 // by default.
Paul Sokolovskyfd49ff92017-03-07 16:40:00 +010054 // TODE: Better support for various x86 calling conventions
55 // (unfortunately, __attribute__((naked)) is not supported on x86).
ASM52620c62017-09-08 15:01:14 +030056 #if !(defined(__ZEPHYR__) || defined(__ANDROID__))
Damien Georgef0dddb62017-03-01 23:20:13 +110057 "pop %ebp \n" // undo function's prelude
Paul Sokolovskyfd49ff92017-03-07 16:40:00 +010058 #endif
Damien Georgef0dddb62017-03-01 23:20:13 +110059 "mov 4(%esp), %edx \n" // load nlr_buf
60 "mov (%esp), %eax \n" // load return %eip
61 "mov %eax, 8(%edx) \n" // store %eip into nlr_buf
62 "mov %ebp, 12(%edx) \n" // store %ebp into nlr_buf
63 "mov %esp, 16(%edx) \n" // store %esp into nlr_buf
64 "mov %ebx, 20(%edx) \n" // store %ebx into nlr_buf
65 "mov %edi, 24(%edx) \n" // store %edi into nlr_buf
66 "mov %esi, 28(%edx) \n" // store %esi into nlr_buf
67 "jmp nlr_push_tail \n" // do the rest in C
68 );
69
70 return 0; // needed to silence compiler warning
71}
72
Paul Sokolovsky096e9672017-12-26 18:39:51 +020073__attribute__((used)) unsigned int nlr_push_tail(nlr_buf_t *nlr) {
74 nlr_buf_t **top = &MP_STATE_THREAD(nlr_top);
75 nlr->prev = *top;
76 MP_NLR_SAVE_PYSTACK(nlr);
77 *top = nlr;
78 return 0; // normal return
79}
80
81void nlr_pop(void) {
82 nlr_buf_t **top = &MP_STATE_THREAD(nlr_top);
83 *top = (*top)->prev;
84}
85
86NORETURN void nlr_jump(void *val) {
87 nlr_buf_t **top_ptr = &MP_STATE_THREAD(nlr_top);
88 nlr_buf_t *top = *top_ptr;
89 if (top == NULL) {
90 nlr_jump_fail(val);
91 }
92
93 top->ret_val = val;
94 MP_NLR_RESTORE_PYSTACK(top);
95 *top_ptr = top->prev;
Damien Georgef0dddb62017-03-01 23:20:13 +110096
97 __asm volatile (
Paul Sokolovsky096e9672017-12-26 18:39:51 +020098 "mov %0, %%edx \n" // %edx points to nlr_buf
99 "mov 28(%%edx), %%esi \n" // load saved %esi
100 "mov 24(%%edx), %%edi \n" // load saved %edi
101 "mov 20(%%edx), %%ebx \n" // load saved %ebx
102 "mov 16(%%edx), %%esp \n" // load saved %esp
103 "mov 12(%%edx), %%ebp \n" // load saved %ebp
104 "mov 8(%%edx), %%eax \n" // load saved %eip
105 "mov %%eax, (%%esp) \n" // store saved %eip to stack
106 "xor %%eax, %%eax \n" // clear return register
107 "inc %%al \n" // increase to make 1, non-local return
Damien Georgef0dddb62017-03-01 23:20:13 +1100108 "ret \n" // return
Paul Sokolovsky096e9672017-12-26 18:39:51 +0200109 : // output operands
110 : "r"(top) // input operands
111 : // clobbered registers
Damien Georgef0dddb62017-03-01 23:20:13 +1100112 );
113
114 for (;;); // needed to silence compiler warning
115}
116
Paul Sokolovsky096e9672017-12-26 18:39:51 +0200117#endif // !MICROPY_NLR_SETJMP && defined(__i386__)