rustc_target/callconv/
csky.rs

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
6
7use rustc_abi::TyAbiInterface;
8
9use crate::callconv::{ArgAbi, FnAbi, Reg, Uniform};
10
11fn classify_ret<Ty>(arg: &mut ArgAbi<'_, Ty>) {
12    if !arg.layout.is_sized() {
13        // Not touching this...
14        return;
15    }
16    // For return type, aggregate which <= 2*XLen will be returned in registers.
17    // Otherwise, aggregate will be returned indirectly.
18    if arg.layout.is_aggregate() {
19        let total = arg.layout.size;
20        if total.bits() > 64 {
21            arg.make_indirect();
22        } else if total.bits() > 32 {
23            arg.cast_to(Uniform::new(Reg::i32(), total));
24        } else {
25            arg.cast_to(Reg::i32());
26        }
27    } else {
28        arg.extend_integer_width_to(32);
29    }
30}
31
32fn classify_arg<'a, Ty, C>(cx: &C, arg: &mut ArgAbi<'a, Ty>)
33where
34    Ty: TyAbiInterface<'a, C> + Copy,
35{
36    if !arg.layout.is_sized() {
37        // Not touching this...
38        return;
39    }
40    if arg.layout.pass_indirectly_in_non_rustic_abis(cx) {
41        arg.make_indirect();
42        return;
43    }
44    // For argument type, the first 4*XLen parts of aggregate will be passed
45    // in registers, and the rest will be passed in stack.
46    // So we can coerce to integers directly and let backend handle it correctly.
47    if arg.layout.is_aggregate() {
48        let total = arg.layout.size;
49        if total.bits() > 32 {
50            arg.cast_to(Uniform::new(Reg::i32(), total));
51        } else {
52            arg.cast_to(Reg::i32());
53        }
54    } else {
55        arg.extend_integer_width_to(32);
56    }
57}
58
59pub(crate) fn compute_abi_info<'a, Ty, C>(cx: &C, fn_abi: &mut FnAbi<'a, Ty>)
60where
61    Ty: TyAbiInterface<'a, C> + Copy,
62{
63    if !fn_abi.ret.is_ignore() {
64        classify_ret(&mut fn_abi.ret);
65    }
66
67    for arg in fn_abi.args.iter_mut() {
68        if arg.is_ignore() {
69            continue;
70        }
71        classify_arg(cx, arg);
72    }
73}