Skip to content
This repository was archived by the owner on Aug 2, 2020. It is now read-only.

Commit a85755a

Browse files
committed
py/nlrxtensa: Convert from assembler to C file with inline asm.
nlr_jump is a little bit inefficient because it now saves a register to the stack.
1 parent be3d7f9 commit a85755a

File tree

2 files changed

+103
-119
lines changed

2 files changed

+103
-119
lines changed

py/nlrxtensa.S

Lines changed: 0 additions & 119 deletions
This file was deleted.

py/nlrxtensa.c

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2014-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(__xtensa__)
31+
32+
#undef nlr_push
33+
34+
// Xtensa calling conventions:
35+
// a0 = return address
36+
// a1 = stack pointer
37+
// a2 = first arg, return value
38+
// a3-a7 = rest of args
39+
40+
unsigned int nlr_push(nlr_buf_t *nlr) {
41+
42+
__asm volatile (
43+
"s32i.n a0, a2, 8 \n" // save regs...
44+
"s32i.n a1, a2, 12 \n"
45+
"s32i.n a8, a2, 16 \n"
46+
"s32i.n a9, a2, 20 \n"
47+
"s32i.n a10, a2, 24 \n"
48+
"s32i.n a11, a2, 28 \n"
49+
"s32i.n a12, a2, 32 \n"
50+
"s32i.n a13, a2, 36 \n"
51+
"s32i.n a14, a2, 40 \n"
52+
"s32i.n a15, a2, 44 \n"
53+
"j nlr_push_tail \n" // do the rest in C
54+
);
55+
56+
return 0; // needed to silence compiler warning
57+
}
58+
59+
__attribute__((used)) unsigned int nlr_push_tail(nlr_buf_t *nlr) {
60+
nlr_buf_t **top = &MP_STATE_THREAD(nlr_top);
61+
nlr->prev = *top;
62+
*top = nlr;
63+
return 0; // normal return
64+
}
65+
66+
void nlr_pop(void) {
67+
nlr_buf_t **top = &MP_STATE_THREAD(nlr_top);
68+
*top = (*top)->prev;
69+
}
70+
71+
NORETURN void nlr_jump(void *val) {
72+
nlr_buf_t **top_ptr = &MP_STATE_THREAD(nlr_top);
73+
nlr_buf_t *top = *top_ptr;
74+
if (top == NULL) {
75+
nlr_jump_fail(val);
76+
}
77+
78+
top->ret_val = val;
79+
*top_ptr = top->prev;
80+
81+
__asm volatile (
82+
"mov.n a2, %0 \n" // a2 points to nlr_buf
83+
"l32i.n a0, a2, 8 \n" // restore regs...
84+
"l32i.n a1, a2, 12 \n"
85+
"l32i.n a8, a2, 16 \n"
86+
"l32i.n a9, a2, 20 \n"
87+
"l32i.n a10, a2, 24 \n"
88+
"l32i.n a11, a2, 28 \n"
89+
"l32i.n a12, a2, 32 \n"
90+
"l32i.n a13, a2, 36 \n"
91+
"l32i.n a14, a2, 40 \n"
92+
"l32i.n a15, a2, 44 \n"
93+
"movi.n a2, 1 \n" // return 1, non-local return
94+
"ret.n \n" // return
95+
: // output operands
96+
: "r"(top) // input operands
97+
: // clobbered registers
98+
);
99+
100+
for (;;); // needed to silence compiler warning
101+
}
102+
103+
#endif // !MICROPY_NLR_SETJMP && defined(__xtensa__)

0 commit comments

Comments
 (0)