Skip to content

Commit 3963387

Browse files
committed
Add tests for RISC-V C ABI
1 parent e590164 commit 3963387

File tree

5 files changed

+831
-1
lines changed

5 files changed

+831
-1
lines changed

Diff for: src/test/auxiliary/rust_test_helpers.c

+29
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,18 @@ struct floats {
168168
double c;
169169
};
170170

171+
struct char_char_double {
172+
uint8_t a;
173+
uint8_t b;
174+
double c;
175+
};
176+
177+
struct char_char_float {
178+
uint8_t a;
179+
uint8_t b;
180+
float c;
181+
};
182+
171183
struct quad
172184
rust_dbg_abi_1(struct quad q) {
173185
struct quad qq = { q.c + 1,
@@ -185,6 +197,23 @@ rust_dbg_abi_2(struct floats f) {
185197
return ff;
186198
}
187199

200+
struct char_char_double
201+
rust_dbg_abi_3(struct char_char_double a) {
202+
struct char_char_double ccd = { a.a + 1,
203+
a.b - 1,
204+
a.c + 1.0 };
205+
return ccd;
206+
}
207+
208+
struct char_char_float
209+
rust_dbg_abi_4(struct char_char_float a) {
210+
struct char_char_float ccd = { a.a + 1,
211+
a.b - 1,
212+
a.c + 1.0 };
213+
return ccd;
214+
}
215+
216+
188217
int
189218
rust_dbg_static_mut = 3;
190219

+181
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
// ignore-tidy-linelength
2+
// compile-flags: -C no-prepopulate-passes
3+
// only-riscv64
4+
// only-linux
5+
#![crate_type = "lib"]
6+
#![allow(improper_ctypes)]
7+
8+
// CHECK: define void @f_void()
9+
#[no_mangle]
10+
pub extern "C" fn f_void() {}
11+
12+
// CHECK: define zeroext i1 @f_scalar_0(i1 zeroext %a)
13+
#[no_mangle]
14+
pub extern "C" fn f_scalar_0(a: bool) -> bool {
15+
a
16+
}
17+
18+
// CHECK: define signext i8 @f_scalar_1(i8 signext %x)
19+
#[no_mangle]
20+
pub extern "C" fn f_scalar_1(x: i8) -> i8 {
21+
x
22+
}
23+
24+
// CHECK: define zeroext i8 @f_scalar_2(i8 zeroext %x)
25+
#[no_mangle]
26+
pub extern "C" fn f_scalar_2(x: u8) -> u8 {
27+
x
28+
}
29+
30+
// CHECK: define signext i32 @f_scalar_3(i32 signext %x)
31+
#[no_mangle]
32+
pub extern "C" fn f_scalar_3(x: i32) -> u32 {
33+
x as u32
34+
}
35+
36+
// CHECK: define i64 @f_scalar_4(i64 %x)
37+
#[no_mangle]
38+
pub extern "C" fn f_scalar_4(x: i64) -> i64 {
39+
x
40+
}
41+
42+
// CHECK: define float @f_fp_scalar_1(float)
43+
#[no_mangle]
44+
pub extern "C" fn f_fp_scalar_1(x: f32) -> f32 {
45+
x
46+
}
47+
// CHECK: define double @f_fp_scalar_2(double)
48+
#[no_mangle]
49+
pub extern "C" fn f_fp_scalar_2(x: f64) -> f64 {
50+
x
51+
}
52+
53+
#[repr(C)]
54+
pub struct Empty {}
55+
56+
// CHECK: define void @f_agg_empty_struct()
57+
#[no_mangle]
58+
pub extern "C" fn f_agg_empty_struct(e: Empty) -> Empty {
59+
e
60+
}
61+
62+
#[repr(C)]
63+
pub struct Tiny {
64+
a: u16,
65+
b: u16,
66+
c: u16,
67+
d: u16,
68+
}
69+
70+
// CHECK: define void @f_agg_tiny(i64)
71+
#[no_mangle]
72+
pub extern "C" fn f_agg_tiny(mut e: Tiny) {
73+
e.a += e.b;
74+
e.c += e.d;
75+
}
76+
77+
// CHECK: define i64 @f_agg_tiny_ret()
78+
#[no_mangle]
79+
pub extern "C" fn f_agg_tiny_ret() -> Tiny {
80+
Tiny { a: 1, b: 2, c: 3, d: 4 }
81+
}
82+
83+
#[repr(C)]
84+
pub struct Small {
85+
a: i64,
86+
b: *mut i64,
87+
}
88+
89+
// CHECK: define void @f_agg_small([2 x i64])
90+
#[no_mangle]
91+
pub extern "C" fn f_agg_small(mut x: Small) {
92+
x.a += unsafe { *x.b };
93+
x.b = &mut x.a;
94+
}
95+
96+
// CHECK: define [2 x i64] @f_agg_small_ret()
97+
#[no_mangle]
98+
pub extern "C" fn f_agg_small_ret() -> Small {
99+
Small { a: 1, b: core::ptr::null_mut() }
100+
}
101+
102+
#[repr(C)]
103+
pub struct SmallAligned {
104+
a: i128,
105+
}
106+
107+
// CHECK: define void @f_agg_small_aligned(i128)
108+
#[no_mangle]
109+
pub extern "C" fn f_agg_small_aligned(mut x: SmallAligned) {
110+
x.a += x.a;
111+
}
112+
113+
#[repr(C)]
114+
pub struct Large {
115+
a: i64,
116+
b: i64,
117+
c: i64,
118+
d: i64,
119+
}
120+
121+
// CHECK: define void @f_agg_large(%Large* {{.*}}%x)
122+
#[no_mangle]
123+
pub extern "C" fn f_agg_large(mut x: Large) {
124+
x.a = x.b + x.c + x.d;
125+
}
126+
127+
// CHECK: define void @f_agg_large_ret(%Large* {{.*}}sret{{.*}}, i32 signext %i, i8 signext %j)
128+
#[no_mangle]
129+
pub extern "C" fn f_agg_large_ret(i: i32, j: i8) -> Large {
130+
Large { a: 1, b: 2, c: 3, d: 4 }
131+
}
132+
133+
// CHECK: define void @f_scalar_stack_1(i64, [2 x i64], i128, %Large* {{.*}}%d, i8 zeroext %e, i8 signext %f, i8 %g, i8 %h)
134+
#[no_mangle]
135+
pub extern "C" fn f_scalar_stack_1(
136+
a: Tiny,
137+
b: Small,
138+
c: SmallAligned,
139+
d: Large,
140+
e: u8,
141+
f: i8,
142+
g: u8,
143+
h: i8,
144+
) {
145+
}
146+
147+
// CHECK: define void @f_scalar_stack_2(%Large* {{.*}}sret{{.*}}, i64 %a, i128, i128, i64 %d, i8 zeroext %e, i8 %f, i8 %g)
148+
#[no_mangle]
149+
pub extern "C" fn f_scalar_stack_2(
150+
a: u64,
151+
b: SmallAligned,
152+
c: SmallAligned,
153+
d: u64,
154+
e: u8,
155+
f: i8,
156+
g: u8,
157+
) -> Large {
158+
Large { a: a as i64, b: e as i64, c: f as i64, d: g as i64 }
159+
}
160+
161+
extern "C" {
162+
fn f_va_callee(_: i32, ...) -> i32;
163+
}
164+
165+
#[no_mangle]
166+
pub unsafe extern "C" fn f_va_caller() {
167+
// CHECK: call signext i32 (i32, ...) @f_va_callee(i32 signext 1, i32 signext 2, i64 3, double {{.*}}, double {{.*}}, i64 {{.*}}, [2 x i64] {{.*}}, i128 {{.*}}, %Large* {{.*}})
168+
f_va_callee(
169+
1,
170+
2i32,
171+
3i64,
172+
4.0f64,
173+
5.0f64,
174+
Tiny { a: 1, b: 2, c: 3, d: 4 },
175+
Small { a: 10, b: core::ptr::null_mut() },
176+
SmallAligned { a: 11 },
177+
Large { a: 12, b: 13, c: 14, d: 15 },
178+
);
179+
// CHECK: call signext i32 (i32, ...) @f_va_callee(i32 signext 1, i32 signext 2, i32 signext 3, i32 signext 4, i128 {{.*}}, i32 signext 6, i32 signext 7, i32 8, i32 9)
180+
f_va_callee(1, 2i32, 3i32, 4i32, SmallAligned { a: 5 }, 6i32, 7i32, 8i32, 9i32);
181+
}

0 commit comments

Comments
 (0)