Skip to content

Commit e2e71a4

Browse files
agattidpgeorge
authored andcommitted
shared/runtime/gchelper: Add LoongArch64 GC helper.
This commit provides optimised function implementations for the GC helper function to collect register values. Both a generic C version and a pure assembler version are provided by these changes, collecting the subset of callee-save registers mentioned in the LoongArch64 ABI document (S0-S9). As for the other platforms, it is preferable to use the assembler version whenever possible, although both were found to be working. Signed-off-by: Alessandro Gatti <[email protected]>
1 parent 9161386 commit e2e71a4

3 files changed

Lines changed: 78 additions & 0 deletions

File tree

shared/runtime/gchelper.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ typedef uintptr_t gc_helper_regs_t[10];
4343
typedef uintptr_t gc_helper_regs_t[11]; // x19-x29
4444
#elif defined(__riscv) && (__riscv_xlen <= 64)
4545
typedef uintptr_t gc_helper_regs_t[12]; // S0-S11
46+
#elif defined(__loongarch__) && defined(__loongarch64)
47+
typedef uintptr_t gc_helper_regs_t[10]; // S0-S9
4648
#endif
4749

4850
#endif

shared/runtime/gchelper_generic.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,32 @@ static void gc_helper_get_regs(gc_helper_regs_t arr) {
189189
arr[11] = s11;
190190
}
191191

192+
#elif defined(__loongarch__) && defined(__loongarch64)
193+
194+
// Fallback implementation for LOONG64, prefer gchelper_loong64.s.
195+
static void gc_helper_get_regs(gc_helper_regs_t arr) {
196+
register uintptr_t s0 asm ("r23");
197+
register uintptr_t s1 asm ("r24");
198+
register uintptr_t s2 asm ("r25");
199+
register uintptr_t s3 asm ("r26");
200+
register uintptr_t s4 asm ("r27");
201+
register uintptr_t s5 asm ("r28");
202+
register uintptr_t s6 asm ("r29");
203+
register uintptr_t s7 asm ("r30");
204+
register uintptr_t s8 asm ("r31");
205+
register uintptr_t s9 asm ("r22");
206+
arr[0] = s0;
207+
arr[1] = s1;
208+
arr[2] = s2;
209+
arr[3] = s3;
210+
arr[4] = s4;
211+
arr[5] = s5;
212+
arr[6] = s6;
213+
arr[7] = s7;
214+
arr[8] = s8;
215+
arr[9] = s9;
216+
}
217+
192218
#else
193219

194220
#error "Architecture not supported for gc_helper_get_regs. Set MICROPY_GCREGS_SETJMP to use the fallback implementation."

shared/runtime/gchelper_loong64.s

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2026 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+
.global gc_helper_get_regs_and_sp
28+
.type gc_helper_get_regs_and_sp, @function
29+
30+
gc_helper_get_regs_and_sp:
31+
32+
/* Store registers into the given array. */
33+
34+
st.d $r23, $r4, 0 /* Save S0. */
35+
st.d $r24, $r4, 8 /* Save S1. */
36+
st.d $r25, $r4, 16 /* Save S2. */
37+
st.d $r26, $r4, 24 /* Save S3. */
38+
st.d $r27, $r4, 32 /* Save S4. */
39+
st.d $r28, $r4, 40 /* Save S5. */
40+
st.d $r29, $r4, 48 /* Save S6. */
41+
st.d $r30, $r4, 56 /* Save S7. */
42+
st.d $r31, $r4, 64 /* Save S8. */
43+
st.d $r22, $r4, 72 /* Save S9. */
44+
45+
/* Return the stack pointer. */
46+
47+
add.d $r4, $r0, $r3
48+
jirl $r0, $r1, 0
49+
50+
.size gc_helper_get_regs_and_sp, .-gc_helper_get_regs_and_sp

0 commit comments

Comments
 (0)