blob: 461b459e213d9f22061154d24858d470b27d9bb9 [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
Damien George5bf8e852017-12-28 16:18:39 +110029#if MICROPY_NLR_X86
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
stijnb184b6a2017-12-26 10:52:09 +010036#if MICROPY_NLR_OS_WINDOWS
Paul Sokolovsky096e9672017-12-26 18:39:51 +020037unsigned 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
Damien Georgec97607d2018-05-15 11:17:28 +100042#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
Damien Georgef0dddb62017-03-01 23:20:13 +110058unsigned int nlr_push(nlr_buf_t *nlr) {
59 (void)nlr;
60
61 __asm volatile (
Damien Georgec97607d2018-05-15 11:17:28 +100062 #if UNDO_PRELUDE
Damien Georgef0dddb62017-03-01 23:20:13 +110063 "pop %ebp \n" // undo function's prelude
Paul Sokolovskyfd49ff92017-03-07 16:40:00 +010064 #endif
Damien Georgef0dddb62017-03-01 23:20:13 +110065 "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
Damien Georgec97607d2018-05-15 11:17:28 +100076 #if !USE_NAKED
Damien Georgef0dddb62017-03-01 23:20:13 +110077 return 0; // needed to silence compiler warning
Damien Georgec97607d2018-05-15 11:17:28 +100078 #endif
Damien Georgef0dddb62017-03-01 23:20:13 +110079}
80
Paul Sokolovsky096e9672017-12-26 18:39:51 +020081NORETURN void nlr_jump(void *val) {
Damien Georgeb25f9212017-12-28 16:46:30 +110082 MP_NLR_JUMP_HEAD(val, top)
Damien Georgef0dddb62017-03-01 23:20:13 +110083
84 __asm volatile (
Paul Sokolovsky096e9672017-12-26 18:39:51 +020085 "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
Damien Georgef0dddb62017-03-01 23:20:13 +110095 "ret \n" // return
Paul Sokolovsky096e9672017-12-26 18:39:51 +020096 : // output operands
97 : "r"(top) // input operands
98 : // clobbered registers
Damien Georgef0dddb62017-03-01 23:20:13 +110099 );
100
Damien George11ecdf22019-08-19 15:51:40 +1000101 MP_UNREACHABLE
Damien Georgef0dddb62017-03-01 23:20:13 +1100102}
103
Damien George5bf8e852017-12-28 16:18:39 +1100104#endif // MICROPY_NLR_X86