Numworks Epsilon  1.4.1
Graphing Calculator Operating System
nlrthumb.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-2016 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 (!defined(MICROPY_NLR_SETJMP) || !MICROPY_NLR_SETJMP) && (defined(__thumb2__) || defined(__thumb__) || defined(__arm__))
30 
31 #undef nlr_push
32 
33 // We only need the functions here if we are on arm/thumb, and we are not
34 // using setjmp/longjmp.
35 //
36 // For reference, arm/thumb callee save regs are:
37 // r4-r11, r13=sp
38 
39 __attribute__((naked)) unsigned int nlr_push(nlr_buf_t *nlr) {
40 
41  __asm volatile (
42  "str r4, [r0, #12] \n" // store r4 into nlr_buf
43  "str r5, [r0, #16] \n" // store r5 into nlr_buf
44  "str r6, [r0, #20] \n" // store r6 into nlr_buf
45  "str r7, [r0, #24] \n" // store r7 into nlr_buf
46 
47 #if defined(__ARM_ARCH_6M__)
48  "mov r1, r8 \n"
49  "str r1, [r0, #28] \n" // store r8 into nlr_buf
50  "mov r1, r9 \n"
51  "str r1, [r0, #32] \n" // store r9 into nlr_buf
52  "mov r1, r10 \n"
53  "str r1, [r0, #36] \n" // store r10 into nlr_buf
54  "mov r1, r11 \n"
55  "str r1, [r0, #40] \n" // store r11 into nlr_buf
56  "mov r1, r13 \n"
57  "str r1, [r0, #44] \n" // store r13=sp into nlr_buf
58  "mov r1, lr \n"
59  "str r1, [r0, #8] \n" // store lr into nlr_buf
60 #else
61  "str r8, [r0, #28] \n" // store r8 into nlr_buf
62  "str r9, [r0, #32] \n" // store r9 into nlr_buf
63  "str r10, [r0, #36] \n" // store r10 into nlr_buf
64  "str r11, [r0, #40] \n" // store r11 into nlr_buf
65  "str r13, [r0, #44] \n" // store r13=sp into nlr_buf
66  "str lr, [r0, #8] \n" // store lr into nlr_buf
67 #endif
68 
69 #if defined(__ARM_ARCH_6M__)
70  "ldr r1, nlr_push_tail_var \n"
71  "bx r1 \n" // do the rest in C
72  ".align 2 \n"
73  "nlr_push_tail_var: .word nlr_push_tail \n"
74 #else
75  "b nlr_push_tail \n" // do the rest in C
76 #endif
77  );
78 
79  return 0; // needed to silence compiler warning
80 }
81 
82 __attribute__((used)) unsigned int nlr_push_tail(nlr_buf_t *nlr) {
83  nlr_buf_t **top = &MP_STATE_THREAD(nlr_top);
84  nlr->prev = *top;
85  *top = nlr;
86  return 0; // normal return
87 }
88 
89 void nlr_pop(void) {
90  nlr_buf_t **top = &MP_STATE_THREAD(nlr_top);
91  *top = (*top)->prev;
92 }
93 
94 NORETURN __attribute__((naked)) void nlr_jump(void *val) {
95  nlr_buf_t **top_ptr = &MP_STATE_THREAD(nlr_top);
96  nlr_buf_t *top = *top_ptr;
97  if (top == NULL) {
98  nlr_jump_fail(val);
99  }
100 
101  top->ret_val = val;
102  *top_ptr = top->prev;
103 
104  __asm volatile (
105  "mov r0, %0 \n" // r0 points to nlr_buf
106  "ldr r4, [r0, #12] \n" // load r4 from nlr_buf
107  "ldr r5, [r0, #16] \n" // load r5 from nlr_buf
108  "ldr r6, [r0, #20] \n" // load r6 from nlr_buf
109  "ldr r7, [r0, #24] \n" // load r7 from nlr_buf
110 
111 #if defined(__ARM_ARCH_6M__)
112  "ldr r1, [r0, #28] \n" // load r8 from nlr_buf
113  "mov r8, r1 \n"
114  "ldr r1, [r0, #32] \n" // load r9 from nlr_buf
115  "mov r9, r1 \n"
116  "ldr r1, [r0, #36] \n" // load r10 from nlr_buf
117  "mov r10, r1 \n"
118  "ldr r1, [r0, #40] \n" // load r11 from nlr_buf
119  "mov r11, r1 \n"
120  "ldr r1, [r0, #44] \n" // load r13=sp from nlr_buf
121  "mov r13, r1 \n"
122  "ldr r1, [r0, #8] \n" // load lr from nlr_buf
123  "mov lr, r1 \n"
124 #else
125  "ldr r8, [r0, #28] \n" // load r8 from nlr_buf
126  "ldr r9, [r0, #32] \n" // load r9 from nlr_buf
127  "ldr r10, [r0, #36] \n" // load r10 from nlr_buf
128  "ldr r11, [r0, #40] \n" // load r11 from nlr_buf
129  "ldr r13, [r0, #44] \n" // load r13=sp from nlr_buf
130  "ldr lr, [r0, #8] \n" // load lr from nlr_buf
131 #endif
132  "movs r0, #1 \n" // return 1, non-local return
133  "bx lr \n" // return
134  : // output operands
135  : "r"(top) // input operands
136  : // clobbered registers
137  );
138 
139  for (;;); // needed to silence compiler warning
140 }
141 
142 #endif // (!defined(MICROPY_NLR_SETJMP) || !MICROPY_NLR_SETJMP) && (defined(__thumb2__) || defined(__thumb__) || defined(__arm__))
#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