1use rustc_abi::{
2 AddressSpace, Align, BackendRepr, Float, HasDataLayout, Primitive, Reg, RegKind, TyAndLayout,
3};
4
5use crate::callconv::{ArgAttribute, FnAbi, PassMode, TyAbiInterface};
6use crate::spec::{HasTargetSpec, RustcAbi};
7
8fn is_single_fp_element<'a, Ty, C>(mut layout: TyAndLayout<'a, Ty>, cx: &C) -> bool
10where
11 Ty: TyAbiInterface<'a, C> + Copy,
12 C: HasDataLayout,
13{
14 let outer_size = layout.layout.size();
16
17 loop {
18 layout = layout.peel_transparent_wrappers(cx);
19
20 return match layout.backend_repr {
21 BackendRepr::Scalar(scalar) => match scalar.primitive() {
22 Primitive::Float(float) => float.size() == outer_size,
23 Primitive::Int(_, _) | Primitive::Pointer(_) => false,
24 },
25 BackendRepr::Memory { .. } => {
26 if let Some((_idx, field)) = layout.non_zst_field_ignore_alignment(cx) {
28 layout = field;
30 continue;
31 } else {
32 false
33 }
34 }
35 _ => false,
36 };
37 }
38}
39
40#[derive(#[automatically_derived]
impl ::core::marker::StructuralPartialEq for Flavor { }
#[automatically_derived]
impl ::core::cmp::PartialEq for Flavor {
#[inline]
fn eq(&self, other: &Self) -> bool {
::core::intrinsics::discriminant_value(self) ==
::core::intrinsics::discriminant_value(other)
}
}PartialEq)]
41pub(crate) enum Flavor {
42 General,
43 FastcallOrVectorcall,
44}
45
46pub(crate) struct X86Options {
47 pub flavor: Flavor,
48 pub regparm: Option<u32>,
49 pub reg_struct_return: bool,
50}
51
52pub(crate) fn compute_abi_info<'a, Ty, C>(cx: &C, fn_abi: &mut FnAbi<'a, Ty>, opts: X86Options)
53where
54 Ty: TyAbiInterface<'a, C> + Copy,
55 C: HasDataLayout + HasTargetSpec,
56{
57 if !fn_abi.ret.is_ignore() {
58 if fn_abi.ret.layout.is_aggregate() && fn_abi.ret.layout.is_sized() {
59 let t = cx.target_spec();
67 if let Some(Float::F16) = fn_abi.ret.layout.complex_float(cx) {
68 let kind = RegKind::Vector { hint_vector_elem: Primitive::Float(Float::F16) };
70 fn_abi.ret.cast_to(Reg { kind, size: fn_abi.ret.layout.size });
71 } else if t.abi_return_struct_as_int
72 || opts.reg_struct_return
73 || fn_abi.ret.layout.is_complex_number(cx)
74 {
75 if is_single_fp_element(fn_abi.ret.layout, cx) {
78 match fn_abi.ret.layout.size.bytes() {
79 2 => fn_abi.ret.cast_to(Reg::f16()),
80 4 => fn_abi.ret.cast_to(Reg::f32()),
81 8 => fn_abi.ret.cast_to(Reg::f64()),
82 _ => fn_abi.ret.make_indirect(),
83 }
84 } else {
85 match fn_abi.ret.layout.size.bytes() {
86 1 => fn_abi.ret.cast_to(Reg::i8()),
87 2 => fn_abi.ret.cast_to(Reg::i16()),
88 4 => fn_abi.ret.cast_to(Reg::i32()),
89 8 => fn_abi.ret.cast_to(Reg::i64()),
90 _ => fn_abi.ret.make_indirect(),
91 }
92 }
93 } else {
94 fn_abi.ret.make_indirect();
95 }
96 } else {
97 fn_abi.ret.extend_integer_width_to(32);
98 }
99 }
100
101 for arg in fn_abi.args.iter_mut() {
102 if arg.is_ignore() || !arg.layout.is_sized() {
103 continue;
104 }
105
106 if arg.layout.pass_indirectly_in_non_rustic_abis(cx) {
107 arg.make_indirect();
108 continue;
109 }
110
111 let t = cx.target_spec();
112 let align_4 = Align::from_bytes(4).unwrap();
113 let align_16 = Align::from_bytes(16).unwrap();
114
115 if arg.layout.is_aggregate() {
116 fn contains_vector<'a, Ty, C>(cx: &C, layout: TyAndLayout<'a, Ty>) -> bool
131 where
132 Ty: TyAbiInterface<'a, C> + Copy,
133 {
134 match layout.backend_repr {
135 BackendRepr::Scalar(_) | BackendRepr::ScalarPair { .. } => false,
136 BackendRepr::SimdVector { .. } => true,
137 BackendRepr::Memory { .. } => {
138 for i in 0..layout.fields.count() {
139 if contains_vector(cx, layout.field(cx, i)) {
140 return true;
141 }
142 }
143 false
144 }
145 BackendRepr::SimdScalableVector { .. } => {
146 {
::core::panicking::panic_fmt(format_args!("scalable vectors are unsupported"));
}panic!("scalable vectors are unsupported")
147 }
148 }
149 }
150
151 let byval_align = if arg.layout.align.abi < align_4 {
152 align_4
154 } else if t.is_like_darwin && contains_vector(cx, arg.layout) {
155 align_16
157 } else {
158 align_4
160 };
161
162 arg.pass_by_stack_offset(Some(byval_align));
163 } else {
164 arg.extend_integer_width_to(32);
165 }
166 }
167
168 fill_inregs(cx, fn_abi, opts, false);
169}
170
171pub(crate) fn fill_inregs<'a, Ty, C>(
172 cx: &C,
173 fn_abi: &mut FnAbi<'a, Ty>,
174 opts: X86Options,
175 rust_abi: bool,
176) where
177 Ty: TyAbiInterface<'a, C> + Copy,
178{
179 if opts.flavor != Flavor::FastcallOrVectorcall && opts.regparm.is_none_or(|x| x == 0) {
180 return;
181 }
182 let mut free_regs = opts.regparm.unwrap_or(2).into();
193
194 let has_casts = fn_abi.args.iter().any(|arg| #[allow(non_exhaustive_omitted_patterns)] match arg.mode {
PassMode::Cast { .. } => true,
_ => false,
}matches!(arg.mode, PassMode::Cast { .. }));
197 if has_casts && rust_abi {
198 return;
199 }
200
201 for arg in fn_abi.args.iter_mut() {
202 let attrs = match arg.mode {
203 PassMode::Ignore
204 | PassMode::Indirect { attrs: _, meta_attrs: None, address_space: _, mode: _ } => {
205 continue;
206 }
207 PassMode::Direct(ref mut attrs) => attrs,
208 PassMode::Pair(..)
209 | PassMode::Indirect { attrs: _, meta_attrs: Some(_), address_space: _, mode: _ }
210 | PassMode::Cast { .. } => {
211 {
::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
format_args!("x86 shouldn\'t be passing arguments by {0:?}",
arg.mode)));
}unreachable!("x86 shouldn't be passing arguments by {:?}", arg.mode)
212 }
213 };
214
215 let unit = arg.layout.homogeneous_aggregate(cx).unwrap().unit().unwrap();
217 {
match (&unit.size, &arg.layout.size) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
}
};assert_eq!(unit.size, arg.layout.size);
218 if #[allow(non_exhaustive_omitted_patterns)] match unit.kind {
RegKind::Float | RegKind::Vector { .. } => true,
_ => false,
}matches!(unit.kind, RegKind::Float | RegKind::Vector { .. }) {
219 continue;
220 }
221
222 let size_in_regs = arg.layout.size.bits().div_ceil(32);
223
224 if size_in_regs == 0 {
225 continue;
226 }
227
228 if size_in_regs > free_regs {
229 break;
230 }
231
232 free_regs -= size_in_regs;
233
234 if arg.layout.size.bits() <= 32 && unit.kind == RegKind::Integer {
235 attrs.set(ArgAttribute::InReg);
236 }
237
238 if free_regs == 0 {
239 break;
240 }
241 }
242}
243
244pub(crate) fn compute_rust_abi_info<'a, Ty, C>(cx: &C, fn_abi: &mut FnAbi<'a, Ty>)
245where
246 Ty: TyAbiInterface<'a, C> + Copy,
247 C: HasDataLayout + HasTargetSpec,
248{
249 if !fn_abi.ret.is_ignore() {
253 let has_float = match fn_abi.ret.layout.backend_repr {
254 BackendRepr::Scalar(s) => #[allow(non_exhaustive_omitted_patterns)] match s.primitive() {
Primitive::Float(_) => true,
_ => false,
}matches!(s.primitive(), Primitive::Float(_)),
255 BackendRepr::ScalarPair { a: s1, b: s2, b_offset: _ } => {
256 #[allow(non_exhaustive_omitted_patterns)] match s1.primitive() {
Primitive::Float(_) => true,
_ => false,
}matches!(s1.primitive(), Primitive::Float(_))
257 || #[allow(non_exhaustive_omitted_patterns)] match s2.primitive() {
Primitive::Float(_) => true,
_ => false,
}matches!(s2.primitive(), Primitive::Float(_))
258 }
259 _ => false, };
261 if has_float {
262 if cx.target_spec().rustc_abi == Some(RustcAbi::X86Sse2)
263 && fn_abi.ret.layout.backend_repr.is_scalar()
264 && fn_abi.ret.layout.size.bits() <= 128
265 {
266 fn_abi.ret.cast_to(Reg::opaque_vector(fn_abi.ret.layout.size));
270 } else if fn_abi.ret.layout.size <= Primitive::Pointer(AddressSpace::ZERO).size(cx) {
271 fn_abi.ret.cast_to(Reg { kind: RegKind::Integer, size: fn_abi.ret.layout.size });
273 } else {
274 fn_abi.ret.make_indirect();
276 }
277 return;
278 }
279 }
280}