Skip to content

Commit a130e29

Browse files
committed
implement C ABI lowering for CSKY
1 parent 2e4e2a8 commit a130e29

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

compiler/rustc_target/src/abi/call/csky.rs

+31-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,40 @@
1-
// See https://github.com/llvm/llvm-project/blob/d85b94bf0080dcd780656c0f5e6342800720eba9/llvm/lib/Target/CSKY/CSKYCallingConv.td
2-
use crate::abi::call::{ArgAbi, FnAbi};
1+
// Reference: CSKY ABI Manual
2+
// https://occ-oss-prod.oss-cn-hangzhou.aliyuncs.com/resource//1695027452256/T-HEAD_800_Series_ABI_Standards_Manual.pdf
3+
//
4+
// Reference: Clang CSKY lowering code
5+
// https://github.com/llvm/llvm-project/blob/4a074f32a6914f2a8d7215d78758c24942dddc3d/clang/lib/CodeGen/Targets/CSKY.cpp#L76-L162
36

4-
fn classify_ret<Ty>(ret: &mut ArgAbi<'_, Ty>) {
5-
if ret.layout.is_aggregate() || ret.layout.size.bits() > 64 {
6-
ret.make_indirect();
7+
use crate::abi::call::{ArgAbi, FnAbi, Reg, Uniform};
8+
9+
fn classify_ret<Ty>(arg: &mut ArgAbi<'_, Ty>) {
10+
// For argument type, the first 4*XLen parts of aggregate will be passed
11+
// in registers, and the rest will be passed in stack.
12+
// So we can coerce to integers directly and let backend handle it correctly.
13+
// For return type, aggregate which <= 2*XLen will be returned in registers.
14+
// Otherwise, aggregate will be returned indirectly.
15+
if arg.layout.is_aggregate() {
16+
let total = arg.layout.size;
17+
if total.bits() > 64 {
18+
// We rely on the LLVM backend lowering code to lower passing a scalar larger than 2*XLEN.
19+
arg.make_indirect();
20+
} else if total.bits() > 32 {
21+
arg.cast_to(Uniform { unit: Reg::i32(), total });
22+
} else {
23+
arg.cast_to(Reg::i32());
24+
}
725
} else {
8-
ret.extend_integer_width_to(32);
26+
arg.extend_integer_width_to(32);
927
}
1028
}
1129

1230
fn classify_arg<Ty>(arg: &mut ArgAbi<'_, Ty>) {
13-
if arg.layout.is_aggregate() || arg.layout.size.bits() > 64 {
14-
arg.make_indirect();
31+
if arg.layout.is_aggregate() {
32+
let total = arg.layout.size;
33+
if total.bits() > 32 {
34+
arg.cast_to(Uniform { unit: Reg::i32(), total });
35+
} else {
36+
arg.cast_to(Reg::i32());
37+
}
1538
} else {
1639
arg.extend_integer_width_to(32);
1740
}

src/doc/rustc/src/platform-support/csky-unknown-linux-gnuabiv2.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,15 @@ target | std | host | notes
1010
`csky-unknown-linux-gnuabiv2hf` | ✓ | | C-SKY abiv2 Linux, hardfloat (little endian)
1111

1212
Reference:
13-
https://c-sky.github.io/
1413

15-
https://gitlab.com/c-sky/
14+
- [CSKY ABI Manual](https://occ-oss-prod.oss-cn-hangzhou.aliyuncs.com/resource//1695027452256/T-HEAD_800_Series_ABI_Standards_Manual.pdf)
15+
- [csky-linux-gnuabiv2-toolchain](https://occ-oss-prod.oss-cn-hangzhou.aliyuncs.com/resource/1356021/1619528643136/csky-linux-gnuabiv2-tools-x86_64-glibc-linux-4.9.56-20210423.tar.gz)
16+
- [csky-linux-gnuabiv2-qemu](https://occ-oss-prod.oss-cn-hangzhou.aliyuncs.com/resource//1689324918932/xuantie-qemu-x86_64-Ubuntu-18.04-20230714-0202.tar.gz)
17+
18+
other links:
19+
20+
- https://c-sky.github.io/
21+
- https://gitlab.com/c-sky/
1622

1723
## Target maintainers
1824

0 commit comments

Comments
 (0)