blob: 4b178f46b31c6077127f8ceddfa57bc939a1a289 [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"
28#include "py/nlr.h"
29
30#if !MICROPY_NLR_SETJMP && defined(__i386__)
31
32#undef nlr_push
33
34// For reference, x86 callee save regs are:
35// ebx, esi, edi, ebp, esp, eip
36
37#define NLR_OS_WINDOWS (defined(_WIN32) || defined(__CYGWIN__))
38
39#if NLR_OS_WINDOWS
40unsigned int nlr_push_tail(nlr_buf_t *nlr) asm("nlr_push_tail");
41#else
42__attribute__((used)) unsigned int nlr_push_tail(nlr_buf_t *nlr);
43#endif
44
45unsigned int nlr_push(nlr_buf_t *nlr) {
46 (void)nlr;
47
48 __asm volatile (
49 "pop %ebp \n" // undo function's prelude
50 "mov 4(%esp), %edx \n" // load nlr_buf
51 "mov (%esp), %eax \n" // load return %eip
52 "mov %eax, 8(%edx) \n" // store %eip into nlr_buf
53 "mov %ebp, 12(%edx) \n" // store %ebp into nlr_buf
54 "mov %esp, 16(%edx) \n" // store %esp into nlr_buf
55 "mov %ebx, 20(%edx) \n" // store %ebx into nlr_buf
56 "mov %edi, 24(%edx) \n" // store %edi into nlr_buf
57 "mov %esi, 28(%edx) \n" // store %esi into nlr_buf
58 "jmp nlr_push_tail \n" // do the rest in C
59 );
60
61 return 0; // needed to silence compiler warning
62}
63
64__attribute__((used)) unsigned int nlr_push_tail(nlr_buf_t *nlr) {
65 nlr_buf_t **top = &MP_STATE_THREAD(nlr_top);
66 nlr->prev = *top;
67 *top = nlr;
68 return 0; // normal return
69}
70
71void nlr_pop(void) {
72 nlr_buf_t **top = &MP_STATE_THREAD(nlr_top);
73 *top = (*top)->prev;
74}
75
76NORETURN void nlr_jump(void *val) {
77 nlr_buf_t **top_ptr = &MP_STATE_THREAD(nlr_top);
78 nlr_buf_t *top = *top_ptr;
79 if (top == NULL) {
80 nlr_jump_fail(val);
81 }
82
83 top->ret_val = val;
84 *top_ptr = top->prev;
85
86 __asm volatile (
87 "mov %0, %%edx \n" // %edx points to nlr_buf
88 "mov 28(%%edx), %%esi \n" // load saved %esi
89 "mov 24(%%edx), %%edi \n" // load saved %edi
90 "mov 20(%%edx), %%ebx \n" // load saved %ebx
91 "mov 16(%%edx), %%esp \n" // load saved %esp
92 "mov 12(%%edx), %%ebp \n" // load saved %ebp
93 "mov 8(%%edx), %%eax \n" // load saved %eip
94 "mov %%eax, (%%esp) \n" // store saved %eip to stack
95 "xor %%eax, %%eax \n" // clear return register
96 "inc %%al \n" // increase to make 1, non-local return
97 "ret \n" // return
98 : // output operands
99 : "r"(top) // input operands
100 : // clobbered registers
101 );
102
103 for (;;); // needed to silence compiler warning
104}
105
106#endif // !MICROPY_NLR_SETJMP && defined(__i386__)