Skip to main content

rustc_target/callconv/
x86_win64.rs

1use rustc_abi::{BackendRepr, CanonAbi, Integer, Primitive, Size, TyAbiInterface};
2
3use crate::callconv::{ArgAbi, ComputeCallConv, Reg};
4use crate::spec::{HasTargetSpec, RustcAbi};
5
6// Win64 ABI: https://docs.microsoft.com/en-us/cpp/build/parameter-passing
7
8pub(crate) struct X86Win64CallConv;
9
10impl X86Win64CallConv {
11    fn fixup<'a, Ty, C>(cx: &C, a: &mut ArgAbi<'_, Ty>, is_ret: bool)
12    where
13        Ty: TyAbiInterface<'a, C> + Copy,
14        C: HasTargetSpec,
15    {
16        match a.layout.backend_repr {
17            BackendRepr::Memory { sized: false } => {}
18            BackendRepr::ScalarPair { .. } | BackendRepr::Memory { sized: true } => {
19                match a.layout.size.bits() {
20                    8 => a.cast_to(Reg::i8()),
21                    16 => a.cast_to(Reg::i16()),
22                    32 => a.cast_to(Reg::i32()),
23                    64 => a.cast_to(Reg::i64()),
24                    _ => a.make_indirect(),
25                }
26            }
27            BackendRepr::SimdVector { .. } => {
28                // FIXME(eddyb) there should be a size cap here
29                // (probably what clang calls "illegal vectors").
30            }
31            BackendRepr::SimdScalableVector { .. } => {
    ::core::panicking::panic_fmt(format_args!("scalable vectors are unsupported"));
}panic!("scalable vectors are unsupported"),
32            BackendRepr::Scalar(scalar) => {
33                if is_ret && #[allow(non_exhaustive_omitted_patterns)] match scalar.primitive() {
    Primitive::Int(Integer::I128, _) => true,
    _ => false,
}matches!(scalar.primitive(), Primitive::Int(Integer::I128, _)) {
34                    if cx.target_spec().rustc_abi == Some(RustcAbi::Softfloat) {
35                        // Use the native `i128` LLVM type for the softfloat ABI -- in other words, adjust nothing.
36                    } else {
37                        // `i128` is returned in xmm0 by Clang and GCC
38                        // FIXME(#134288): This may change for the `-msvc` targets in the future.
39                        a.cast_to(Reg::opaque_vector(Size::from_bits(128)));
40                    }
41                } else if a.layout.size.bytes() > 8 {
42                    a.make_indirect();
43                } else {
44                    a.extend_integer_width_to(32);
45                }
46            }
47        }
48    }
49}
50
51impl<'a, Ty, C> ComputeCallConv<'a, Ty, C> for X86Win64CallConv
52where
53    Ty: TyAbiInterface<'a, C> + Copy,
54    C: HasTargetSpec,
55{
56    fn new(_cx: &C, _conv: CanonAbi, _c_variadic: bool) -> Self {
57        Self
58    }
59
60    // Windows ABIs do not talk about ZST since such types do not exist in MSVC.
61    // However, clang and gcc allow ZST in their windows-gnu targets, and pass them by pointer indirection.
62    // We follow that for `repr(C)` ZSTs (and `repr(transparent)` wrappers around them),
63    // but `repr(Rust)` ones are always ignored (ensuring that `()` matches C `void`).
64
65    fn passes_zsts(&self) -> bool {
66        true
67    }
68
69    fn compute_ret_abi(&mut self, cx: &C, ret: &mut ArgAbi<'a, Ty>) {
70        if ret.is_ignore() {
71            ret.make_indirect_from_ignore();
72        } else {
73            Self::fixup(cx, ret, true);
74        }
75    }
76
77    fn compute_arg_abi(&mut self, cx: &C, arg: &mut ArgAbi<'a, Ty>, _is_vararg: bool) {
78        if arg.is_ignore() {
79            arg.make_indirect_from_ignore();
80
81            return;
82        }
83        if arg.layout.pass_indirectly_in_non_rustic_abis(cx) {
84            arg.make_indirect();
85            return;
86        }
87        Self::fixup(cx, arg, false);
88    }
89}