Skip to content

Commit b006b98

Browse files
committed
Fix passing of large function arguments on riscv64
- Correct stack adjustment in gfunc_call() - Add a regression test (130_large_argument)
1 parent e7262ac commit b006b98

File tree

3 files changed

+57
-6
lines changed

3 files changed

+57
-6
lines changed

riscv64-gen.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -627,10 +627,14 @@ ST_FUNC void gfunc_call(int nb_args)
627627
if ((vtop->r & VT_VALMASK) == VT_CMP)
628628
gv(RC_INT);
629629

630+
630631
if (stack_add) {
631-
if (stack_add >= 0x1000) {
632-
o(0x37 | (5 << 7) | (-stack_add & 0xfffff000)); //lui t0, upper(v)
633-
EI(0x13, 0, 5, 5, -stack_add << 20 >> 20); // addi t0, t0, lo(v)
632+
if (stack_add >= 0x800) {
633+
unsigned int bit11 = (((unsigned int)-stack_add) >> 11) & 1;
634+
o(0x37 | (5 << 7) |
635+
((-stack_add + (bit11 << 12)) & 0xfffff000)); //lui t0, upper(v)
636+
EI(0x13, 0, 5, 5, ((-stack_add & 0xfff) - bit11 * (1 << 12)));
637+
// addi t0, t0, lo(v)
634638
ER(0x33, 0, 2, 2, 5, 0); // add sp, sp, t0
635639
}
636640
else
@@ -757,9 +761,12 @@ ST_FUNC void gfunc_call(int nb_args)
757761
gcall_or_jmp(1);
758762
vtop -= nb_args + 1;
759763
if (stack_add) {
760-
if (stack_add >= 0x1000) {
761-
o(0x37 | (5 << 7) | (stack_add & 0xfffff000)); //lui t0, upper(v)
762-
EI(0x13, 0, 5, 5, stack_add << 20 >> 20); // addi t0, t0, lo(v)
764+
if (stack_add >= 0x800) {
765+
unsigned int bit11 = ((unsigned int)stack_add >> 11) & 1;
766+
o(0x37 | (5 << 7) |
767+
((stack_add + (bit11 << 12)) & 0xfffff000)); //lui t0, upper(v)
768+
EI(0x13, 0, 5, 5, (stack_add & 0xfff) - bit11 * (1 << 12));
769+
// addi t0, t0, lo(v)
763770
ER(0x33, 0, 2, 2, 5, 0); // add sp, sp, t0
764771
}
765772
else

tests/tests2/130_large_argument.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include<stdio.h>
2+
3+
struct large1 {
4+
int a[768];
5+
};
6+
7+
struct large2 {
8+
int a[1920];
9+
};
10+
11+
void pass_large_struct1(struct large1 in)
12+
{
13+
printf("%d %d\n", in.a[200], in.a[767]);
14+
return;
15+
}
16+
17+
void pass_large_struct2(struct large2 in)
18+
{
19+
printf("%d %d %d\n", in.a[200], in.a[1023], in.a[1919]);
20+
return;
21+
}
22+
23+
void pass_many_args(int a, int b, int c, int d, int e, int f, int g, int h, int i,
24+
int j, int k, int l, int m)
25+
{
26+
printf("%d %d %d %d %d %d %d %d %d %d %d %d %d\n", a, b, c, d, e, f, g, h, i,
27+
j, k, l, m);
28+
return;
29+
}
30+
31+
struct large1 l1 = { .a = { [200] = 1, [767] = 2 } };
32+
struct large2 l2 = { .a = { [200] = 3, [1023] = 4, [1919] = 5} };
33+
34+
int main(void)
35+
{
36+
pass_large_struct1(l1);
37+
pass_large_struct2(l2);
38+
pass_many_args(13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1);
39+
40+
return 0;
41+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
1 2
2+
3 4 5
3+
13 12 11 10 9 8 7 6 5 4 3 2 1

0 commit comments

Comments
 (0)