Numworks Epsilon  1.4.1
Graphing Calculator Operating System
nlrx86.c
Go to the documentation of this file.
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_SETJMP && defined(__i386__)
30 
31 #undef nlr_push
32 
33 // For reference, x86 callee save regs are:
34 // ebx, esi, edi, ebp, esp, eip
35 
36 #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
43 unsigned 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 
48 unsigned int nlr_push(nlr_buf_t *nlr) {
49  (void)nlr;
50 
51  __asm volatile (
52  // Check for Zephyr, which uses a different calling convention
53  // by default.
54  // TODE: Better support for various x86 calling conventions
55  // (unfortunately, __attribute__((naked)) is not supported on x86).
56  #if !(defined(__ZEPHYR__) || defined(__ANDROID__))
57  "pop %ebp \n" // undo function's prelude
58  #endif
59  "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 
73 __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  *top = nlr;
77  return 0; // normal return
78 }
79 
80 void nlr_pop(void) {
81  nlr_buf_t **top = &MP_STATE_THREAD(nlr_top);
82  *top = (*top)->prev;
83 }
84 
85 NORETURN void nlr_jump(void *val) {
86  nlr_buf_t **top_ptr = &MP_STATE_THREAD(nlr_top);
87  nlr_buf_t *top = *top_ptr;
88  if (top == NULL) {
89  nlr_jump_fail(val);
90  }
91 
92  top->ret_val = val;
93  *top_ptr = top->prev;
94 
95  __asm volatile (
96  "mov %0, %%edx \n" // %edx points to nlr_buf
97  "mov 28(%%edx), %%esi \n" // load saved %esi
98  "mov 24(%%edx), %%edi \n" // load saved %edi
99  "mov 20(%%edx), %%ebx \n" // load saved %ebx
100  "mov 16(%%edx), %%esp \n" // load saved %esp
101  "mov 12(%%edx), %%ebp \n" // load saved %ebp
102  "mov 8(%%edx), %%eax \n" // load saved %eip
103  "mov %%eax, (%%esp) \n" // store saved %eip to stack
104  "xor %%eax, %%eax \n" // clear return register
105  "inc %%al \n" // increase to make 1, non-local return
106  "ret \n" // return
107  : // output operands
108  : "r"(top) // input operands
109  : // clobbered registers
110  );
111 
112  for (;;); // needed to silence compiler warning
113 }
114 
115 #endif // !MICROPY_NLR_SETJMP && defined(__i386__)
#define nlr_push(buf)
Definition: nlr.h:73
#define MP_STATE_THREAD(x)
Definition: mpstate.h:248
#define NULL
Definition: stddef.h:4
nlr_buf_t * prev
Definition: nlr.h:41
#define NORETURN
Definition: mpconfig.h:1268
#define nlr_pop()
Definition: nlr.h:74
void * ret_val
Definition: nlr.h:42
void nlr_jump_fail(void *val)
Definition: port.cpp:142
Definition: nlr.h:39
ISR InitialisationVector [INITIALISATION_VECTOR_SIZE] __attribute__((section(".isr_vector_table")))
#define nlr_jump(val)
Definition: nlr.h:75