1use arrayvec::ArrayVec;
2use rustc_abi::{
3 BackendRepr, CanonAbi, FieldsShape, Float, HasDataLayout, Integer, Numeric, Primitive, Reg,
4 RegKind, Size, TyAbiInterface,
5};
6
7use crate::callconv::{
8 ArgAbi, ArgAttribute, ArgExtension, CastTarget, ComputeCallConv, PassMode, Uniform,
9};
10
11fn extend_integer_width_mips<Ty>(arg: &mut ArgAbi<'_, Ty>, bits: u64) {
12 if let BackendRepr::Scalar(scalar) = arg.layout.backend_repr
14 && let Primitive::Int(i, signed) = scalar.primitive()
15 && !signed
16 && i.size().bits() == 32
17 && let PassMode::Direct(ref mut attrs) = arg.mode
18 {
19 attrs.ext(ArgExtension::Sext);
20 return;
21 }
22
23 arg.extend_integer_width_to(bits);
24}
25
26fn float_reg<'a, Ty, C>(cx: &C, ret: &ArgAbi<'a, Ty>, i: usize) -> Option<Reg>
27where
28 Ty: TyAbiInterface<'a, C> + Copy,
29 C: HasDataLayout,
30{
31 match ret.layout.field(cx, i).backend_repr {
32 BackendRepr::Scalar(scalar) => match scalar.primitive() {
33 Primitive::Float(float) => {
34 match float {
35 Float::F16 => None,
37 Float::F32 => Some(Reg::f32()),
38 Float::F64 => Some(Reg::f64()),
39 Float::F128 => Some(Reg::f128()),
40 Float::F16B => {
::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
format_args!("`f16b` unsupported on mips64")));
}unreachable!("`f16b` unsupported on mips64"),
41 Float::X87F80 => {
::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
format_args!("`x87_f80` unsupported on mips64")));
}unreachable!("`x87_f80` unsupported on mips64"),
42 Float::PpcF128 => {
::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
format_args!("`ppcf128` unsupported on mips64")));
}unreachable!("`ppcf128` unsupported on mips64"),
43 }
44 }
45 _ => None,
46 },
47 _ => None,
48 }
49}
50
51fn classify_ret<'a, Ty, C>(cx: &C, ret: &mut ArgAbi<'a, Ty>, offset: &mut Size)
52where
53 Ty: TyAbiInterface<'a, C> + Copy,
54 C: HasDataLayout,
55{
56 if !ret.layout.is_aggregate() {
57 extend_integer_width_mips(ret, 64);
58 return;
59 }
60
61 let size = ret.layout.size;
62 let bits = size.bits();
63 if bits <= 128 {
64 if let Some(component) = ret.layout.complex_number(cx) {
66 match component {
67 Numeric::Int(Integer::I8 | Integer::I16 | Integer::I32, _) => {
68 ret.cast_to(Reg { kind: RegKind::Integer, size });
70 }
71 _ => {
72 let reg = Reg { kind: component.reg_kind(), size: component.size() };
74 ret.cast_to(CastTarget::pair(reg, reg));
75 }
76 }
77 return;
78 }
79
80 if let FieldsShape::Arbitrary { .. } = ret.layout.fields {
85 if ret.layout.fields.count() == 1 {
86 if let Some(reg) = float_reg(cx, ret, 0) {
87 ret.cast_to_with_attrs(reg, ArgAttribute::InReg.into());
91 return;
92 }
93 } else if ret.layout.fields.count() == 2
94 && let Some(reg0) = float_reg(cx, ret, 0)
95 && let Some(reg1) = float_reg(cx, ret, 1)
96 {
97 ret.cast_to_with_attrs(CastTarget::pair(reg0, reg1), ArgAttribute::InReg.into());
98 return;
99 }
100 }
101
102 ret.cast_to(Uniform::new(Reg::i64(), size));
104 } else {
105 ret.make_indirect();
106 *offset += cx.data_layout().pointer_size();
107 }
108}
109
110fn classify_arg<'a, Ty, C>(cx: &C, arg: &mut ArgAbi<'a, Ty>, offset: &mut Size)
111where
112 Ty: TyAbiInterface<'a, C> + Copy,
113 C: HasDataLayout,
114{
115 let dl = cx.data_layout();
116 let size = arg.layout.size;
117 let mut prefix = ArrayVec::new();
118
119 let align = Ord::clamp(arg.layout.align, dl.i64_align, dl.i128_align);
121 let pad_i32 = u8::from(!offset.is_aligned(align));
122
123 if !arg.layout.is_aggregate() {
124 extend_integer_width_mips(arg, 64);
125
126 if let BackendRepr::Scalar(scalar) = arg.layout.backend_repr {
133 let kind = RegKind::from_primitive(scalar.primitive());
134 arg.cast_to_and_pad_i32(CastTarget::from(Reg { kind, size }), pad_i32);
135 }
136 } else if arg.layout.pass_indirectly_in_non_rustic_abis(cx) {
137 arg.make_indirect();
138 } else if let Some(component) = arg.layout.complex_number(cx)
139 && !#[allow(non_exhaustive_omitted_patterns)] match component {
Numeric::Float(Float::F16) => true,
_ => false,
}matches!(component, Numeric::Float(Float::F16))
140 {
141 let slot = dl.pointer_size();
142 let curr_offset = offset.align_to(align);
143
144 const NUM_ARG_SLOTS: u64 = 8;
145
146 match component {
147 Numeric::Float(Float::F16B) => {
::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
format_args!("Complex<f16b> is not C-compatible")));
}unreachable!("Complex<f16b> is not C-compatible"),
148 Numeric::Float(Float::F16 | Float::X87F80 | Float::PpcF128) => {
149 {
::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
format_args!("not supported on mips64")));
}unreachable!("not supported on mips64")
150 }
151 Numeric::Float(Float::F32 | Float::F64) => {
152 if curr_offset.bytes() / slot.bytes() + 2 <= NUM_ARG_SLOTS {
154 *offset = curr_offset + slot * 2;
160 let unit = Reg { kind: RegKind::Float, size: component.size() };
161 let cast_target = CastTarget::from(Uniform::new(unit, size));
162 arg.cast_to(cast_target);
163 return;
164 }
165
166 arg.cast_to_and_pad_i32(Uniform::new(Reg::i64(), size), pad_i32);
168 }
169 Numeric::Float(Float::F128) => {
170 let reg = Reg { kind: RegKind::Float, size: arg.layout.field(cx, 0).size };
172 arg.cast_to_and_pad_i32(CastTarget::pair(reg, reg), pad_i32);
173 }
174 Numeric::Int(Integer::I8 | Integer::I16 | Integer::I32, _) => {
175 let cast_target = CastTarget::from(Reg { kind: RegKind::Integer, size });
177 arg.cast_to(cast_target.with_attrs(ArgAttribute::InReg.into()));
179 }
180 Numeric::Int(Integer::I64 | Integer::I128, _) => {
181 let cast_target = CastTarget::from(Reg { kind: RegKind::Integer, size });
183 arg.cast_to(cast_target);
184 }
185 }
186 } else {
187 match arg.layout.fields {
188 FieldsShape::Primitive => ::core::panicking::unreachable_default_msg()unreachable!(),
189 FieldsShape::Array { .. } => {
190 arg.make_indirect();
192 }
193 FieldsShape::Union(_) => {
194 }
196 FieldsShape::Arbitrary { .. } => {
197 let mut last_offset = Size::ZERO;
200
201 'outer: for i in 0..arg.layout.fields.count() {
202 let field = arg.layout.field(cx, i);
203 let offset = arg.layout.fields.offset(i);
204
205 if let BackendRepr::Scalar(scalar) = field.backend_repr {
207 if scalar.primitive() == Primitive::Float(Float::F64) {
208 if offset.is_aligned(dl.f64_align) {
209 if !last_offset.is_aligned(dl.f64_align) {
::core::panicking::panic("assertion failed: last_offset.is_aligned(dl.f64_align)")
};assert!(last_offset.is_aligned(dl.f64_align));
211 for _ in 0..((offset - last_offset).bits() / 64) {
212 if prefix.try_push(Reg::i64()).is_err() {
213 break 'outer;
214 }
215 }
216
217 if prefix.try_push(Reg::f64()).is_err() {
218 break;
219 }
220 last_offset = offset + Reg::f64().size;
221 }
222 }
223 }
224 }
225 }
226 };
227
228 let rest_size = size - Size::from_bytes(8) * prefix.len() as u64;
230 arg.cast_to_and_pad_i32(
231 CastTarget::prefixed(prefix, Uniform::new(Reg::i64(), rest_size)),
232 pad_i32,
233 );
234 }
235 *offset = offset.align_to(align) + size.align_to(align);
236}
237
238pub(crate) struct Mips64CallConv {
239 offset: Size,
240}
241
242impl<'a, Ty, C> ComputeCallConv<'a, Ty, C> for Mips64CallConv
243where
244 Ty: TyAbiInterface<'a, C> + Copy,
245 C: HasDataLayout,
246{
247 fn new(_cx: &C, _conv: CanonAbi, _c_variadic: bool) -> Self {
248 Self { offset: Size::ZERO }
251 }
252
253 fn compute_ret_abi(&mut self, cx: &C, ret: &mut ArgAbi<'a, Ty>) {
254 classify_ret(cx, ret, &mut self.offset);
255 }
256
257 fn compute_arg_abi(&mut self, cx: &C, arg: &mut ArgAbi<'a, Ty>, _is_vararg: bool) {
258 if !arg.layout.is_sized() {
259 return;
260 }
261 classify_arg(cx, arg, &mut self.offset);
262 }
263}