Skip to main content

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::{CanonAbi, TyAbiInterface};
8
9use crate::callconv::{ArgAbi, ComputeCallConv, Reg, Uniform};
10
11fn classify_ret<Ty>(ret: &mut ArgAbi<'_, Ty>) {
12    if !ret.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 ret.layout.is_aggregate() {
19        let total = ret.layout.size;
20        if total.bits() > 64 {
21            ret.make_indirect();
22        } else if total.bits() > 32 {
23            ret.cast_to(Uniform::new(Reg::i32(), total));
24        } else {
25            ret.cast_to(Reg::i32());
26        }
27    } else {
28        ret.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) struct CSkyCallConv;
60
61impl<'a, Ty, C> ComputeCallConv<'a, Ty, C> for CSkyCallConv
62where
63    Ty: TyAbiInterface<'a, C> + Copy,
64{
65    fn new(_cx: &C, _conv: CanonAbi, _c_variadic: bool) -> Self {
66        Self
67    }
68
69    fn compute_ret_abi(&mut self, _cx: &C, ret: &mut ArgAbi<'a, Ty>) {
70        classify_ret(ret);
71    }
72
73    fn compute_arg_abi(&mut self, cx: &C, arg: &mut ArgAbi<'a, Ty>, _is_vararg: bool) {
74        classify_arg(cx, arg);
75    }
76}