Skip to main content

rustc_target/callconv/
x86_win32.rs

1use rustc_abi::{
2    Align, BackendRepr, Float, HasDataLayout, Primitive, Reg, RegKind, TyAbiInterface,
3};
4
5use crate::callconv::FnAbi;
6use crate::callconv::x86::Flavor;
7use crate::spec::{HasTargetSpec, RustcAbi};
8
9pub(crate) fn compute_abi_info<'a, Ty, C>(
10    cx: &C,
11    fn_abi: &mut FnAbi<'a, Ty>,
12    opts: super::x86::X86Options,
13) where
14    Ty: TyAbiInterface<'a, C> + Copy,
15    C: HasDataLayout + HasTargetSpec,
16{
17    if !fn_abi.ret.is_ignore() {
18        if fn_abi.ret.layout.is_aggregate() && fn_abi.ret.layout.is_sized() {
19            // Returning a structure. Most often, this will use
20            // a hidden first argument. On some platforms, though,
21            // small structs are returned as integers.
22            //
23            // Some links:
24            // https://www.angelcode.com/dev/callconv/callconv.html
25            // Clang's ABI handling is in lib/CodeGen/TargetInfo.cpp
26            let t = cx.target_spec();
27            // MSVC does not special-case 1-element float aggregates, unlike others.
28            // GCC used to apply the SysV rule here, breaking windows-gnu's ABI, but was fixed:
29            // - reported in https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82028
30            // - fixed in https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85667
31            if let Some(Float::F16) = fn_abi.ret.layout.complex_float(cx) {
32                // `_Complex _Float16` is returned as `<2 x half>`.
33                let kind = RegKind::Vector { hint_vector_elem: Primitive::Float(Float::F16) };
34                fn_abi.ret.cast_to(Reg { kind, size: fn_abi.ret.layout.size });
35            } else if t.abi_return_struct_as_int || opts.reg_struct_return {
36                match fn_abi.ret.layout.size.bytes() {
37                    1 => fn_abi.ret.cast_to(Reg::i8()),
38                    2 => fn_abi.ret.cast_to(Reg::i16()),
39                    4 => fn_abi.ret.cast_to(Reg::i32()),
40                    8 => fn_abi.ret.cast_to(Reg::i64()),
41                    _ => fn_abi.ret.make_indirect(),
42                }
43            } else {
44                fn_abi.ret.make_indirect();
45            }
46        } else if cx.target_spec().rustc_abi != Some(RustcAbi::Softfloat)
47            && opts.flavor != Flavor::Vectorcall
48            && let BackendRepr::Scalar(scalar) = fn_abi.ret.layout.backend_repr
49            && #[allow(non_exhaustive_omitted_patterns)] match scalar.primitive() {
    Primitive::Float(Float::F32 | Float::F64) => true,
    _ => false,
}matches!(scalar.primitive(), Primitive::Float(Float::F32 | Float::F64))
50        {
51            super::x86::pass_on_x87_floating_point_stack(cx, &mut fn_abi.ret);
52        } else {
53            fn_abi.ret.extend_integer_width_to(32);
54        }
55    }
56
57    for arg in fn_abi.args.iter_mut() {
58        if arg.is_ignore() || !arg.layout.is_sized() {
59            continue;
60        }
61
62        if arg.layout.pass_indirectly_in_non_rustic_abis(cx) {
63            arg.make_indirect();
64            continue;
65        }
66
67        // FIXME: MSVC 2015+ will pass the first 3 vector arguments in [XYZ]MM0-2
68        // See https://reviews.llvm.org/D72114 for Clang behavior
69
70        let align_4 = Align::from_bytes(4).unwrap();
71
72        if arg.layout.is_adt()
73            && let Some(max_repr_align) = arg.layout.max_repr_align
74            && max_repr_align > align_4
75        {
76            // MSVC has special rules for overaligned arguments: https://reviews.llvm.org/D72114.
77            // Summarized here:
78            // - Arguments with _requested_ alignment > 4 are passed indirectly.
79            // - For backwards compatibility, arguments with natural alignment > 4 are still passed
80            //   on stack (via `byval`). For example, this includes `double`, `int64_t`,
81            //   and structs containing them, provided they lack an explicit alignment attribute.
82            if !(arg.layout.align >= max_repr_align) {
    {
        ::core::panicking::panic_fmt(format_args!("abi alignment {0:?} less than requested alignment {1:?}",
                arg.layout.align, max_repr_align));
    }
};assert!(
83                arg.layout.align >= max_repr_align,
84                "abi alignment {:?} less than requested alignment {max_repr_align:?}",
85                arg.layout.align,
86            );
87            arg.make_indirect();
88        } else if arg.layout.is_aggregate() {
89            // Alignment of the `byval` argument.
90            // The rules can be found in `X86_32ABIInfo::getTypeStackAlignInBytes` in Clang's `TargetInfo.cpp`.
91            let byval_align = align_4;
92            arg.pass_by_stack_offset(Some(byval_align));
93        } else {
94            arg.extend_integer_width_to(32);
95        }
96    }
97
98    super::x86::fill_inregs(cx, fn_abi, opts, false);
99}