1 /*
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"
28 
29 #if MICROPY_NLR_X86
30 
31 #undef nlr_push
32 
33 // For reference, x86 callee save regs are:
34 //  ebx, esi, edi, ebp, esp, eip
35 
36 #if MICROPY_NLR_OS_WINDOWS
37 unsigned int nlr_push_tail(nlr_buf_t *nlr) asm ("nlr_push_tail");
38 #else
39 __attribute__((used)) unsigned int nlr_push_tail(nlr_buf_t *nlr);
40 #endif
41 
42 #if !defined(__clang__) && defined(__GNUC__) && __GNUC__ >= 8
43 // Since gcc 8.0 the naked attribute is supported
44 #define USE_NAKED (1)
45 #define UNDO_PRELUDE (0)
46 #elif defined(__ZEPHYR__) || defined(__ANDROID__)
47 // Zephyr and Android use a different calling convention by default
48 #define USE_NAKED (0)
49 #define UNDO_PRELUDE (0)
50 #else
51 #define USE_NAKED (0)
52 #define UNDO_PRELUDE (1)
53 #endif
54 
55 #if USE_NAKED
56 __attribute__((naked))
57 #endif
nlr_push(nlr_buf_t * nlr)58 unsigned int nlr_push(nlr_buf_t *nlr) {
59     (void)nlr;
60 
61     __asm volatile (
62         #if UNDO_PRELUDE
63         "pop    %ebp                \n" // undo function's prelude
64         #endif
65         "mov    4(%esp), %edx       \n" // load nlr_buf
66         "mov    (%esp), %eax        \n" // load return %eip
67         "mov    %eax, 8(%edx)       \n" // store %eip into nlr_buf
68         "mov    %ebp, 12(%edx)      \n" // store %ebp into nlr_buf
69         "mov    %esp, 16(%edx)      \n" // store %esp into nlr_buf
70         "mov    %ebx, 20(%edx)      \n" // store %ebx into nlr_buf
71         "mov    %edi, 24(%edx)      \n" // store %edi into nlr_buf
72         "mov    %esi, 28(%edx)      \n" // store %esi into nlr_buf
73         "jmp    nlr_push_tail       \n" // do the rest in C
74         );
75 
76     #if !USE_NAKED
77     return 0; // needed to silence compiler warning
78     #endif
79 }
80 
nlr_jump(void * val)81 NORETURN void nlr_jump(void *val) {
82     MP_NLR_JUMP_HEAD(val, top)
83 
84     __asm volatile (
85         "mov    %0, %%edx           \n" // %edx points to nlr_buf
86         "mov    28(%%edx), %%esi    \n" // load saved %esi
87         "mov    24(%%edx), %%edi    \n" // load saved %edi
88         "mov    20(%%edx), %%ebx    \n" // load saved %ebx
89         "mov    16(%%edx), %%esp    \n" // load saved %esp
90         "mov    12(%%edx), %%ebp    \n" // load saved %ebp
91         "mov    8(%%edx), %%eax     \n" // load saved %eip
92         "mov    %%eax, (%%esp)      \n" // store saved %eip to stack
93         "xor    %%eax, %%eax        \n" // clear return register
94         "inc    %%al                \n" // increase to make 1, non-local return
95         "ret                        \n" // return
96         :                           // output operands
97         : "r" (top)                 // input operands
98         :                           // clobbered registers
99         );
100 
101     MP_UNREACHABLE
102 }
103 
104 #endif // MICROPY_NLR_X86
105