8000 py/nlrrv32: Add RISC-V RV32I NLR implementation. · davidgiven/micropython@e6ae699 · GitHub
[go: up one dir, main page]

Skip to content

Commit e6ae699

Browse files
agattidpgeorge
authored andcommitted
py/nlrrv32: Add RISC-V RV32I NLR implementation.
Add custom NLR support for 32 bits RISC-V RV32I targets. Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
1 parent 596f92b commit e6ae699

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

py/nlr.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#define MICROPY_NLR_NUM_REGS_MIPS (13)
4545
#define MICROPY_NLR_NUM_REGS_XTENSA (10)
4646
#define MICROPY_NLR_NUM_REGS_XTENSAWIN (17)
47+
#define MICROPY_NLR_NUM_REGS_RV32I (14)
4748

4849
// *FORMAT-OFF*
4950

@@ -88,6 +89,9 @@
8889
#elif defined(__mips__)
8990
#define MICROPY_NLR_MIPS (1)
9091
#define MICROPY_NLR_NUM_REGS (MICROPY_NLR_NUM_REGS_MIPS)
92+
#elif defined(__riscv) && defined(__riscv_xlen) && (__riscv_xlen == 32)
93+
#define MICROPY_NLR_RV32I (1)
94+
#define MICROPY_NLR_NUM_REGS (MICROPY_NLR_NUM_REGS_RV32I)
9195
#else
9296
#define MICROPY_NLR_SETJMP (1)
9397
//#warning "No native NLR support for this arch, using setjmp implementation"

py/nlrrv32.c

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2024 Alessandro Gatti
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_RV32I
30+
31+
#undef nlr_push
32+
33+
__attribute__((naked)) unsigned int nlr_push(nlr_buf_t *nlr) {
34+
__asm volatile (
35+
"sw x1, 8(x10) \n" // Store RA.
36+
"sw x8, 12(x10) \n" // Store S0.
37+
"sw x9, 16(x10) \n" // Store S1.
38+
"sw x18, 20(x10) \n" // Store S2.
39+
"sw x19, 24(x10) \n" // Store S3.
40+
"sw x20, 28(x10) \n" // Store S4.
41+
"sw x21, 32(x10) \n" // Store S5.
42+
"sw x22, 36(x10) \n" // Store S6.
43+
"sw x23, 40(x10) \n" // Store S7.
44+
"sw x24, 44(x10) \n" // Store S8.
45+
"sw x25, 48(x10) \n" // Store S9.
46+
"sw x26, 52(x10) \n" // Store S10.
47+
"sw x27, 56(x10) \n" // Store S11.
48+
"sw x2, 60(x10) \n" // Store SP.
49+
"jal x0, nlr_push_tail \n" // Jump to the C part.
50+
);
51+
}
52+
53+
NORETURN void nlr_jump(void *val) {
54+
MP_NLR_JUMP_HEAD(val, top)
55+
__asm volatile (
56+
"add x10, x0, %0 \n" // Load nlr_buf address.
57+
"lw x1, 8(x10) \n" // Retrieve RA.
58+
"lw x8, 12(x10) \n" // Retrieve S0.
59+
"lw x9, 16(x10) \n" // Retrieve S1.
60+
"lw x18, 20(x10) \n" // Retrieve S2.
61+
"lw x19, 24(x10) \n" // Retrieve S3.
62+
"lw x20, 28(x10) \n" // Retrieve S4.
63+
"lw x21, 32(x10) \n" // Retrieve S5.
64+
"lw x22, 36(x10) \n" // Retrieve S6.
65+
"lw x23, 40(x10) \n" // Retrieve S7.
66+
"lw x24, 44(x10) \n" // Retrieve S8.
67+
"lw x25, 48(x10) \n" // Retrieve S9.
68+
"lw x26, 52(x10) \n" // Retrieve S10.
69+
"lw x27, 56(x10) \n" // Retrieve S11.
70+
"lw x2, 60(x10) \n" // Retrieve SP.
71+
"addi x10, x0, 1 \n" // Return 1 for a non-local return.
72+
"jalr x0, x1, 0 \n" // Return.
73+
: // Outputs.
74+
: "r" (top) // Inputs.
75+
: "memory" // Clobbered.
76+
);
77+
78+
MP_UNREACHABLE
79+
}
80+
81+
#endif // MICROPY_NLR_RV32I

py/py.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ set(MICROPY_SOURCE_PY
5555
${MICROPY_PY_DIR}/nlr.c
5656
${MICROPY_PY_DIR}/nlrmips.c
5757
${MICROPY_PY_DIR}/nlrpowerpc.c
58+
${MICROPY_PY_DIR}/nlrrv32.c
5859
${MICROPY_PY_DIR}/nlrsetjmp.c
5960
${MICROPY_PY_DIR}/nlrthumb.c
6061
${MICROPY_PY_DIR}/nlrx64.c

py/py.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ PY_CORE_O_BASENAME = $(addprefix py/,\
8383
nlrmips.o \
8484
nlrpowerpc.o \
8585
nlrxtensa.o \
86+
nlrrv32.o \
8687
nlrsetjmp.o \
8788
malloc.o \
8889
gc.o \

0 commit comments

Comments
 (0)
0