rustc_target/callconv/
powerpc.rs1use 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
9fn complex_cast_target<Ty>(arg: &ArgAbi<'_, Ty>) -> CastTarget {
11 let size = arg.layout.size;
12
13 if size.bytes() <= 4 {
14 CastTarget::from(Reg { kind: RegKind::Integer, size })
16 } else if size.bytes() == 8 {
17 CastTarget::from(Reg::i64())
20 } else {
21 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 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 if arg.layout.is_complex_number(cx) {
68 let cast_target = complex_cast_target(arg);
69
70 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 *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 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
104fn 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, 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 false
130 }
131 }
132}
133
134pub(crate) struct PowerPCCallConv {
135 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}