blob: 9490c4f42cb8c769eee3b89526051e1ef0e80bfa [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
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 +020073NORETURN void nlr_jump(void *val) {
Damien Georgeb25f9212017-12-28 16:46:30 +110074 MP_NLR_JUMP_HEAD(val, top)
Damien Georgef0dddb62017-03-01 23:20:13 +110075
76 __asm volatile (
Paul Sokolovsky096e9672017-12-26 18:39:51 +020077 "mov %0, %%edx \n" // %edx points to nlr_buf
78 "mov 28(%%edx), %%esi \n" // load saved %esi
79 "mov 24(%%edx), %%edi \n" // load saved %edi
80 "mov 20(%%edx), %%ebx \n" // load saved %ebx
81 "mov 16(%%edx), %%esp \n" // load saved %esp
82 "mov 12(%%edx), %%ebp \n" // load saved %ebp
83 "mov 8(%%edx), %%eax \n" // load saved %eip
84 "mov %%eax, (%%esp) \n" // store saved %eip to stack
85 "xor %%eax, %%eax \n" // clear return register
86 "inc %%al \n" // increase to make 1, non-local return
Damien Georgef0dddb62017-03-01 23:20:13 +110087 "ret \n" // return
Paul Sokolovsky096e9672017-12-26 18:39:51 +020088 : // output operands
89 : "r"(top) // input operands
90 : // clobbered registers
Damien Georgef0dddb62017-03-01 23:20:13 +110091 );
92
93 for (;;); // needed to silence compiler warning
94}
95
Damien George5bf8e852017-12-28 16:18:39 +110096#endif // MICROPY_NLR_X86