blob: eab5759f2144aacbbd23b44b9887d1d388f4dd80 [file] [log] [blame]
Damien George1df41682016-05-30 19:22:28 +01001/*
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"
Damien George1df41682016-05-30 19:22:28 +010028
29#if (!defined(MICROPY_NLR_SETJMP) || !MICROPY_NLR_SETJMP) && (defined(__thumb2__) || defined(__thumb__) || defined(__arm__))
30
Dave Hylands26b7d8a2016-07-13 09:36:55 -070031#undef nlr_push
32
Damien George1df41682016-05-30 19:22:28 +010033// 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
Damien Georgedd376a22017-09-01 15:25:29 +100069#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
Damien George1df41682016-05-30 19:22:28 +010075 "b nlr_push_tail \n" // do the rest in C
Damien Georgedd376a22017-09-01 15:25:29 +100076#endif
Damien George1df41682016-05-30 19:22:28 +010077 );
78
79 return 0; // needed to silence compiler warning
80}
81
Daniel Tralamazza96266622016-07-08 15:46:01 +020082__attribute__((used)) unsigned int nlr_push_tail(nlr_buf_t *nlr) {
Damien George1df41682016-05-30 19:22:28 +010083 nlr_buf_t **top = &MP_STATE_THREAD(nlr_top);
84 nlr->prev = *top;
Damien George02d830c2017-11-26 23:28:40 +110085 MP_NLR_SAVE_PYSTACK(nlr);
Damien George1df41682016-05-30 19:22:28 +010086 *top = nlr;
87 return 0; // normal return
88}
89
90void nlr_pop(void) {
91 nlr_buf_t **top = &MP_STATE_THREAD(nlr_top);
92 *top = (*top)->prev;
93}
94
95NORETURN __attribute__((naked)) void nlr_jump(void *val) {
96 nlr_buf_t **top_ptr = &MP_STATE_THREAD(nlr_top);
97 nlr_buf_t *top = *top_ptr;
98 if (top == NULL) {
99 nlr_jump_fail(val);
100 }
101
102 top->ret_val = val;
Damien George02d830c2017-11-26 23:28:40 +1100103 MP_NLR_RESTORE_PYSTACK(top);
Damien George1df41682016-05-30 19:22:28 +0100104 *top_ptr = top->prev;
105
106 __asm volatile (
107 "mov r0, %0 \n" // r0 points to nlr_buf
108 "ldr r4, [r0, #12] \n" // load r4 from nlr_buf
109 "ldr r5, [r0, #16] \n" // load r5 from nlr_buf
110 "ldr r6, [r0, #20] \n" // load r6 from nlr_buf
111 "ldr r7, [r0, #24] \n" // load r7 from nlr_buf
112
113#if defined(__ARM_ARCH_6M__)
114 "ldr r1, [r0, #28] \n" // load r8 from nlr_buf
115 "mov r8, r1 \n"
116 "ldr r1, [r0, #32] \n" // load r9 from nlr_buf
117 "mov r9, r1 \n"
118 "ldr r1, [r0, #36] \n" // load r10 from nlr_buf
119 "mov r10, r1 \n"
120 "ldr r1, [r0, #40] \n" // load r11 from nlr_buf
121 "mov r11, r1 \n"
122 "ldr r1, [r0, #44] \n" // load r13=sp from nlr_buf
123 "mov r13, r1 \n"
124 "ldr r1, [r0, #8] \n" // load lr from nlr_buf
125 "mov lr, r1 \n"
126#else
127 "ldr r8, [r0, #28] \n" // load r8 from nlr_buf
128 "ldr r9, [r0, #32] \n" // load r9 from nlr_buf
129 "ldr r10, [r0, #36] \n" // load r10 from nlr_buf
130 "ldr r11, [r0, #40] \n" // load r11 from nlr_buf
131 "ldr r13, [r0, #44] \n" // load r13=sp from nlr_buf
132 "ldr lr, [r0, #8] \n" // load lr from nlr_buf
133#endif
134 "movs r0, #1 \n" // return 1, non-local return
135 "bx lr \n" // return
136 : // output operands
137 : "r"(top) // input operands
138 : // clobbered registers
139 );
140
141 for (;;); // needed to silence compiler warning
142}
143
144#endif // (!defined(MICROPY_NLR_SETJMP) || !MICROPY_NLR_SETJMP) && (defined(__thumb2__) || defined(__thumb__) || defined(__arm__))