Skip to content

Commit 2d5cece

Browse files
Jongydpgeorge
authored andcommitted
py/nlr: Implement NLR for AArch64.
1 parent 7d73b9f commit 2d5cece

3 files changed

Lines changed: 79 additions & 0 deletions

File tree

py/nlr.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#define MICROPY_NLR_NUM_REGS_X64_WIN (10)
4040
#define MICROPY_NLR_NUM_REGS_ARM_THUMB (10)
4141
#define MICROPY_NLR_NUM_REGS_ARM_THUMB_FP (10 + 6)
42+
#define MICROPY_NLR_NUM_REGS_AARCH64 (13)
4243
#define MICROPY_NLR_NUM_REGS_XTENSA (10)
4344
#define MICROPY_NLR_NUM_REGS_XTENSAWIN (17)
4445

@@ -72,6 +73,9 @@
7273
// so only save/restore those as an optimisation.
7374
#define MICROPY_NLR_NUM_REGS (MICROPY_NLR_NUM_REGS_ARM_THUMB_FP)
7475
#endif
76+
#elif defined(__aarch64__)
77+
#define MICROPY_NLR_AARCH64 (1)
78+
#define MICROPY_NLR_NUM_REGS (MICROPY_NLR_NUM_REGS_AARCH64)
7579
#elif defined(__xtensa__)
7680
#define MICROPY_NLR_XTENSA (1)
7781
#define MICROPY_NLR_NUM_REGS (MICROPY_NLR_NUM_REGS_XTENSA)

py/nlraarch64.c

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2021 Yonatan Goldschmidt
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" // needed for NLR defs
28+
29+
#if MICROPY_NLR_AARCH64
30+
31+
// AArch64 callee-saved registers are x19-x29.
32+
// https://en.wikipedia.org/wiki/Calling_convention#ARM_(A64)
33+
34+
// Implemented purely as inline assembly; inside a function, we have to deal with undoing the prologue, restoring
35+
// SP and LR. This way, we don't.
36+
__asm(
37+
"nlr_push: \n"
38+
".global nlr_push \n"
39+
"mov x9, sp \n"
40+
"stp lr, x9, [x0, #16]\n" // 16 == offsetof(nlr_buf_t, regs)
41+
"stp x19, x20, [x0, #32]\n"
42+
"stp x21, x22, [x0, #48]\n"
43+
"stp x23, x24, [x0, #64]\n"
44+
"stp x25, x26, [x0, #80]\n"
45+
"stp x27, x28, [x0, #96]\n"
46+
"str x29, [x0, #112]\n"
47+
"b nlr_push_tail \n" // do the rest in C
48+
);
49+
50+
NORETURN void nlr_jump(void *val) {
51+
MP_NLR_JUMP_HEAD(val, top)
52+
53+
MP_STATIC_ASSERT(offsetof(nlr_buf_t, regs) == 16); // asm assumes it
54+
55+
__asm volatile (
56+
"ldr x29, [%0, #112]\n"
57+
"ldp x27, x28, [%0, #96]\n"
58+
"ldp x25, x26, [%0, #80]\n"
59+
"ldp x23, x24, [%0, #64]\n"
60+
"ldp x21, x22, [%0, #48]\n"
61+
"ldp x19, x20, [%0, #32]\n"
62+
"ldp lr, x9, [%0, #16]\n" // 16 == offsetof(nlr_buf_t, regs)
63+
"mov sp, x9 \n"
64+
"mov x0, #1 \n" // non-local return
65+
"ret \n"
66+
:
67+
: "r" (top)
68+
:
69+
);
70+
71+
MP_UNREACHABLE
72+
}
73+
74+
#endif // MICROPY_NLR_AARCH64

py/py.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ PY_CORE_O_BASENAME = $(addprefix py/,\
5757
nlrx86.o \
5858
nlrx64.o \
5959
nlrthumb.o \
60+
nlraarch64.o \
6061
nlrpowerpc.o \
6162
nlrxtensa.o \
6263
nlrsetjmp.o \

0 commit comments

Comments
 (0)