Skip to main content

rustc_target/callconv/
powerpc.rs

1use rustc_abi::{BackendRepr, CanonAbi, Float, Primitive, Reg, RegKind, TyAbiInterface};
2use rustc_span::bug;
3
4use crate::callconv::{ArgAbi, CastTarget, ComputeCallConv, Uniform};
5use crate::spec::{Env, HasTargetSpec, Os, RustcAbi};
6
7const NUM_ARG_GPRS: u64 = 8;
8
9/// How to cast `Complex<T>` so that we match the GCC ABI.
10fn complex_cast_target<Ty>(arg: &ArgAbi<'_, Ty>) -> CastTarget {
11    let size = arg.layout.size;
12
13    if size.bytes() <= 4 {
14        // Coerce to an integer for `Complex<i8>` and `Complex<i16>`.
15        CastTarget::from(Reg { kind: RegKind::Integer, size })
16    } else if size.bytes() == 8 {
17        // Coerce to a single `i64` for `Complex<f32>` and `Complex<i32>`, which has the correct
18        // register alignment of 8 bytes.
19        CastTarget::from(Reg::i64())
20    } else {
21        // Coerce to an array `[N x i32]` for everything wider. An array of i32 gives the correct
22        // 4-byte register alignment.
23        CastTarget::from(Uniform::new(Reg::i32(), size))
24    }
25}
26
27fn classify_ret<'a, Ty, C>(cx: &C, ret: &mut ArgAbi<'a, Ty>, gprs_in_use: &mut u64)
28where
29    Ty: TyAbiInterface<'a, C> + Copy,
30{
31    if ret.layout.is_complex_number(cx) {
32        ret.cast_to(complex_cast_target(ret));
33    } else if ret.layout.is_aggregate() {
34        *gprs_in_use += 1;
35        ret.make_indirect();
36    } else {
37        ret.extend_integer_width_to(32);
38    }
39}
40
41fn classify_arg<'a, Ty, C: HasTargetSpec>(cx: &C, arg: &mut ArgAbi<'a, Ty>, gprs_in_use: &mut u64)
42where
43    Ty: TyAbiInterface<'a, C> + Copy,
44{
45    if arg.is_ignore() {
46        // powerpc-unknown-linux-{gnu,musl,uclibc} doesn't ignore ZSTs.
47        if cx.target_spec().os == Os::Linux
48            && #[allow(non_exhaustive_omitted_patterns)] match cx.target_spec().env {
    Env::Gnu | Env::Musl | Env::Uclibc => true,
    _ => false,
}matches!(cx.target_spec().env, Env::Gnu | Env::Musl | Env::Uclibc)
49        {
50            *gprs_in_use += 1;
51            arg.make_indirect_from_ignore();
52        }
53        return;
54    }
55
56    // The powerpc ABI in GCC hardcodes a special rule for values of size 8. It remarks
57    //
58    // > V.4 wants long longs and doubles to be double word aligned. Just
59    // > testing the mode size is a boneheaded way to do this as it means
60    // > that other types such as complex int are also double word aligned.
61    // > However, we're stuck with this because changing the ABI might break
62    // > existing library interfaces.
63    //
64    // LLVM aligns every value that it splits over multiple GPRs, but a `Complex<T>` is only
65    // aligned when it is cast to an `i64` (i.e. has a size of exactly 8).
66
67    if arg.layout.is_complex_number(cx) {
68        let cast_target = complex_cast_target(arg);
69
70        // Complex values are only register aligned when the size is exactly 8.
71        let is_aligned_pair = arg.layout.size.bytes() == 8;
72        if is_aligned_pair && !gprs_in_use.is_multiple_of(2) {
73            *gprs_in_use += 1;
74        }
75
76        let registers_used = arg.layout.size.bytes().div_ceil(4);
77        if *gprs_in_use + registers_used > NUM_ARG_GPRS {
78            let pad_i32_count = u8::try_from(NUM_ARG_GPRS.saturating_sub(*gprs_in_use)).unwrap();
79            arg.cast_to_and_pad_i32(cast_target, pad_i32_count);
80        } else {
81            arg.cast_to(cast_target);
82        }
83
84        // A value that does not fit uses up the remaining GPRs.
85        *gprs_in_use += registers_used;
86    } else if arg.layout.pass_indirectly_in_non_rustic_abis(cx) || arg.layout.is_aggregate() {
87        *gprs_in_use += 1;
88        arg.make_indirect();
89    } else {
90        if backend_repr_uses_gprs(cx, arg) {
91            // Other types (i64, i128, etc.) are register aligned when >= 8.
92            let is_aligned_pair = arg.layout.size.bytes() >= 8;
93            if is_aligned_pair && !gprs_in_use.is_multiple_of(2) {
94                *gprs_in_use += 1;
95            }
96
97            *gprs_in_use += arg.layout.size.bytes().div_ceil(4)
98        }
99
100        arg.extend_integer_width_to(32);
101    }
102}
103
104/// Are GPRs used to pass this argument (assuming it is passed directly)?
105fn backend_repr_uses_gprs<'a, Ty, C: HasTargetSpec>(cx: &C, arg: &ArgAbi<'a, Ty>) -> bool {
106    let is_softfloat = match cx.target_spec().rustc_abi {
107        Some(RustcAbi::Softfloat) => ::rustc_span::macros::bug_impl(None,
    format_args!("there are currently no powerpc softfloat targets"),
    Location::caller())bug!("there are currently no powerpc softfloat targets"),
108        Some(RustcAbi::PowerPcSpe) => true, // SPE is like softfloat in terms of ABI
109        None => false,
110        Some(other) => ::rustc_span::macros::bug_impl(None,
    format_args!("unexpected powerpc abi {0:?}", other), Location::caller())bug!("unexpected powerpc abi {other:?}"),
111    };
112
113    match arg.layout.backend_repr {
114        BackendRepr::ScalarPair { .. } | BackendRepr::SimdScalableVector { .. } => {
115            ::rustc_span::macros::bug_impl(None,
    format_args!("unexpected backend_repr for 32-bit powerpc: {0:?}",
        arg.layout.backend_repr), Location::caller())bug!("unexpected backend_repr for 32-bit powerpc: {:?}", arg.layout.backend_repr)
116        }
117        BackendRepr::Memory { .. } => {
118            ::rustc_span::macros::bug_impl(None,
    format_args!("unexpected backend_repr for PassMode::Direct: {0:?}",
        arg.layout.backend_repr), Location::caller())bug!("unexpected backend_repr for PassMode::Direct: {:?}", arg.layout.backend_repr)
119        }
120        BackendRepr::Scalar(scalar) => match scalar.primitive() {
121            Primitive::Float(Float::F32 | Float::F64 | Float::F128 | Float::PpcF128) => {
122                is_softfloat
123            }
124
125            _ => true,
126        },
127        BackendRepr::SimdVector { .. } => {
128            // When vector types are legal, they are passed in vector registers.
129            false
130        }
131    }
132}
133
134pub(crate) struct PowerPCCallConv {
135    /// Keeps track of how many GPRs are in use so far to pass arguments. This is used to exhaust
136    /// the remaining argument registers when a complex value, that doesn't fit fully in the
137    /// remaining registers, is passed via the stack. Subsequent arguments must then also be passed
138    /// via the stack.
139    gprs_in_use: u64,
140}
141
142impl<'a, Ty, C> ComputeCallConv<'a, Ty, C> for PowerPCCallConv
143where
144    Ty: TyAbiInterface<'a, C> + Copy,
145    C: HasTargetSpec,
146{
147    fn new(_cx: &C, _conv: CanonAbi, _c_variadic: bool) -> Self {
148        Self { gprs_in_use: 0 }
149    }
150
151    fn passes_zsts(&self) -> bool {
152        true
153    }
154
155    fn compute_ret_abi(&mut self, cx: &C, ret: &mut ArgAbi<'a, Ty>) {
156        if !ret.is_ignore() {
157            classify_ret(cx, ret, &mut self.gprs_in_use);
158        }
159    }
160
161    fn compute_arg_abi(&mut self, cx: &C, arg: &mut ArgAbi<'a, Ty>, _is_vararg: bool) {
162        classify_arg(cx, arg, &mut self.gprs_in_use);
163    }
164}