Skip to content

Commit aa94ee0

Browse files
committed
Auto merge of rust-lang#137363 - workingjubilee:untangle-x86-abi-impl, r=<try>
compiler: untangle Windows x86-32 ABI impl While it shares more than zero code with the SysV x86-32 ABI impl, there is no particular reason to organize different ABIs using if-else. r? `@ghost` try-job: i686-msvc-1 try-job: i686-msvc-2 try-job: i686-mingw-1 try-job: i686-mingw-2 try-job: i686-mingw-3
2 parents a18bd8a + b2be9cf commit aa94ee0

File tree

3 files changed

+87
-23
lines changed

3 files changed

+87
-23
lines changed

compiler/rustc_target/src/callconv/mod.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ mod sparc64;
3131
mod wasm;
3232
mod x86;
3333
mod x86_64;
34+
mod x86_win32;
3435
mod x86_win64;
3536
mod xtensa;
3637

@@ -649,7 +650,11 @@ impl<'a, Ty> FnAbi<'a, Ty> {
649650
};
650651
let reg_struct_return = cx.x86_abi_opt().reg_struct_return;
651652
let opts = x86::X86Options { flavor, regparm, reg_struct_return };
652-
x86::compute_abi_info(cx, self, opts);
653+
if spec.is_like_msvc {
654+
x86_win32::compute_abi_info(cx, self, opts);
655+
} else {
656+
x86::compute_abi_info(cx, self, opts);
657+
}
653658
}
654659
"x86_64" => match abi {
655660
ExternAbi::SysV64 { .. } => x86_64::compute_abi_info(cx, self),

compiler/rustc_target/src/callconv/x86.rs

+2-22
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ where
3636
if t.abi_return_struct_as_int || opts.reg_struct_return {
3737
// According to Clang, everyone but MSVC returns single-element
3838
// float aggregates directly in a floating-point register.
39-
if !t.is_like_msvc && fn_abi.ret.layout.is_single_fp_element(cx) {
39+
if fn_abi.ret.layout.is_single_fp_element(cx) {
4040
match fn_abi.ret.layout.size.bytes() {
4141
4 => fn_abi.ret.cast_to(Reg::f32()),
4242
8 => fn_abi.ret.cast_to(Reg::f64()),
@@ -64,31 +64,11 @@ where
6464
continue;
6565
}
6666

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-
7067
let t = cx.target_spec();
7168
let align_4 = Align::from_bytes(4).unwrap();
7269
let align_16 = Align::from_bytes(16).unwrap();
7370

74-
if t.is_like_msvc
75-
&& arg.layout.is_adt()
76-
&& let Some(max_repr_align) = arg.layout.max_repr_align
77-
&& max_repr_align > align_4
78-
{
79-
// MSVC has special rules for overaligned arguments: https://reviews.llvm.org/D72114.
80-
// Summarized here:
81-
// - Arguments with _requested_ alignment > 4 are passed indirectly.
82-
// - For backwards compatibility, arguments with natural alignment > 4 are still passed
83-
// on stack (via `byval`). For example, this includes `double`, `int64_t`,
84-
// and structs containing them, provided they lack an explicit alignment attribute.
85-
assert!(
86-
arg.layout.align.abi >= max_repr_align,
87-
"abi alignment {:?} less than requested alignment {max_repr_align:?}",
88-
arg.layout.align.abi,
89-
);
90-
arg.make_indirect();
91-
} else if arg.layout.is_aggregate() {
71+
if arg.layout.is_aggregate() {
9272
// We need to compute the alignment of the `byval` argument. The rules can be found in
9373
// `X86_32ABIInfo::getTypeStackAlignInBytes` in Clang's `TargetInfo.cpp`. Summarized
9474
// here, they are:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
use rustc_abi::{Align, HasDataLayout, Reg, TyAbiInterface};
2+
3+
use crate::callconv::FnAbi;
4+
use crate::spec::HasTargetSpec;
5+
6+
pub(crate) fn compute_abi_info<'a, Ty, C>(
7+
cx: &C,
8+
fn_abi: &mut FnAbi<'a, Ty>,
9+
opts: super::x86::X86Options,
10+
) where
11+
Ty: TyAbiInterface<'a, C> + Copy,
12+
C: HasDataLayout + HasTargetSpec,
13+
{
14+
if !fn_abi.ret.is_ignore() {
15+
if fn_abi.ret.layout.is_aggregate() && fn_abi.ret.layout.is_sized() {
16+
// Returning a structure. Most often, this will use
17+
// a hidden first argument. On some platforms, though,
18+
// small structs are returned as integers.
19+
//
20+
// Some links:
21+
// https://www.angelcode.com/dev/callconv/callconv.html
22+
// Clang's ABI handling is in lib/CodeGen/TargetInfo.cpp
23+
let t = cx.target_spec();
24+
if t.abi_return_struct_as_int || opts.reg_struct_return {
25+
// According to Clang, everyone but MSVC returns single-element
26+
// float aggregates directly in a floating-point register.
27+
match fn_abi.ret.layout.size.bytes() {
28+
1 => fn_abi.ret.cast_to(Reg::i8()),
29+
2 => fn_abi.ret.cast_to(Reg::i16()),
30+
4 => fn_abi.ret.cast_to(Reg::i32()),
31+
8 => fn_abi.ret.cast_to(Reg::i64()),
32+
_ => fn_abi.ret.make_indirect(),
33+
}
34+
} else {
35+
fn_abi.ret.make_indirect();
36+
}
37+
} else {
38+
fn_abi.ret.extend_integer_width_to(32);
39+
}
40+
}
41+
42+
for arg in fn_abi.args.iter_mut() {
43+
if arg.is_ignore() || !arg.layout.is_sized() {
44+
continue;
45+
}
46+
47+
// FIXME: MSVC 2015+ will pass the first 3 vector arguments in [XYZ]MM0-2
48+
// See https://reviews.llvm.org/D72114 for Clang behavior
49+
50+
let align_4 = Align::from_bytes(4).unwrap();
51+
52+
if arg.layout.is_adt()
53+
&& let Some(max_repr_align) = arg.layout.max_repr_align
54+
&& max_repr_align > align_4
55+
{
56+
// MSVC has special rules for overaligned arguments: https://reviews.llvm.org/D72114.
57+
// Summarized here:
58+
// - Arguments with _requested_ alignment > 4 are passed indirectly.
59+
// - For backwards compatibility, arguments with natural alignment > 4 are still passed
60+
// on stack (via `byval`). For example, this includes `double`, `int64_t`,
61+
// and structs containing them, provided they lack an explicit alignment attribute.
62+
assert!(
63+
arg.layout.align.abi >= max_repr_align,
64+
"abi alignment {:?} less than requested alignment {max_repr_align:?}",
65+
arg.layout.align.abi,
66+
);
67+
arg.make_indirect();
68+
} else if arg.layout.is_aggregate() {
69+
// Alignment of the `byval` argument.
70+
// The rules can be found in `X86_32ABIInfo::getTypeStackAlignInBytes` in Clang's `TargetInfo.cpp`.
71+
let byval_align = align_4;
72+
arg.pass_by_stack_offset(Some(byval_align));
73+
} else {
74+
arg.extend_integer_width_to(32);
75+
}
76+
}
77+
78+
super::x86::fill_inregs(cx, fn_abi, opts, false);
79+
}

0 commit comments

Comments
 (0)