Skip to content

Commit 28140e8

Browse files
compiler: reorganize rustc_abi to be more internally uniform
General housekeeping: - Use less reexports from its rustc_target era - Unify some imports as a result - Split the Reg(ister) types into their own files Generally moving stuff around because it makes the crate more consistent.
1 parent 89da361 commit 28140e8

File tree

4 files changed

+78
-74
lines changed

4 files changed

+78
-74
lines changed

compiler/rustc_abi/src/callconv.rs

+7-70
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,10 @@
1-
mod abi {
2-
pub(crate) use crate::Primitive::*;
3-
pub(crate) use crate::Variants;
4-
}
5-
6-
#[cfg(feature = "nightly")]
7-
use rustc_macros::HashStable_Generic;
8-
9-
use crate::{Align, HasDataLayout, Size};
101
#[cfg(feature = "nightly")]
112
use crate::{BackendRepr, FieldsShape, TyAbiInterface, TyAndLayout};
3+
use crate::{Primitive, Size, Variants};
124

13-
#[cfg_attr(feature = "nightly", derive(HashStable_Generic))]
14-
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
15-
pub enum RegKind {
16-
Integer,
17-
Float,
18-
Vector,
19-
}
20-
21-
#[cfg_attr(feature = "nightly", derive(HashStable_Generic))]
22-
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
23-
pub struct Reg {
24-
pub kind: RegKind,
25-
pub size: Size,
26-
}
27-
28-
macro_rules! reg_ctor {
29-
($name:ident, $kind:ident, $bits:expr) => {
30-
pub fn $name() -> Reg {
31-
Reg { kind: RegKind::$kind, size: Size::from_bits($bits) }
32-
}
33-
};
34-
}
35-
36-
impl Reg {
37-
reg_ctor!(i8, Integer, 8);
38-
reg_ctor!(i16, Integer, 16);
39-
reg_ctor!(i32, Integer, 32);
40-
reg_ctor!(i64, Integer, 64);
41-
reg_ctor!(i128, Integer, 128);
42-
43-
reg_ctor!(f32, Float, 32);
44-
reg_ctor!(f64, Float, 64);
45-
}
5+
mod reg;
466

47-
impl Reg {
48-
pub fn align<C: HasDataLayout>(&self, cx: &C) -> Align {
49-
let dl = cx.data_layout();
50-
match self.kind {
51-
RegKind::Integer => match self.size.bits() {
52-
1 => dl.i1_align.abi,
53-
2..=8 => dl.i8_align.abi,
54-
9..=16 => dl.i16_align.abi,
55-
17..=32 => dl.i32_align.abi,
56-
33..=64 => dl.i64_align.abi,
57-
65..=128 => dl.i128_align.abi,
58-
_ => panic!("unsupported integer: {self:?}"),
59-
},
60-
RegKind::Float => match self.size.bits() {
61-
16 => dl.f16_align.abi,
62-
32 => dl.f32_align.abi,
63-
64 => dl.f64_align.abi,
64-
128 => dl.f128_align.abi,
65-
_ => panic!("unsupported float: {self:?}"),
66-
},
67-
RegKind::Vector => dl.vector_align(self.size).abi,
68-
}
69-
}
70-
}
7+
pub use reg::{Reg, RegKind};
718

729
/// Return value from the `homogeneous_aggregate` test function.
7310
#[derive(Copy, Clone, Debug)]
@@ -134,8 +71,8 @@ impl<'a, Ty> TyAndLayout<'a, Ty> {
13471
// The primitive for this algorithm.
13572
BackendRepr::Scalar(scalar) => {
13673
let kind = match scalar.primitive() {
137-
abi::Int(..) | abi::Pointer(_) => RegKind::Integer,
138-
abi::Float(_) => RegKind::Float,
74+
Primitive::Int(..) | Primitive::Pointer(_) => RegKind::Integer,
75+
Primitive::Float(_) => RegKind::Float,
13976
};
14077
Ok(HomogeneousAggregate::Homogeneous(Reg { kind, size: self.size }))
14178
}
@@ -206,8 +143,8 @@ impl<'a, Ty> TyAndLayout<'a, Ty> {
206143
let (mut result, mut total) = from_fields_at(*self, Size::ZERO)?;
207144

208145
match &self.variants {
209-
abi::Variants::Single { .. } | abi::Variants::Empty => {}
210-
abi::Variants::Multiple { variants, .. } => {
146+
Variants::Single { .. } | Variants::Empty => {}
147+
Variants::Multiple { variants, .. } => {
211148
// Treat enum variants like union members.
212149
// HACK(eddyb) pretend the `enum` field (discriminant)
213150
// is at the start of every variant (otherwise the gap
+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#[cfg(feature = "nightly")]
2+
use rustc_macros::HashStable_Generic;
3+
4+
use crate::{Align, HasDataLayout, Size};
5+
6+
#[cfg_attr(feature = "nightly", derive(HashStable_Generic))]
7+
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
8+
pub enum RegKind {
9+
Integer,
10+
Float,
11+
Vector,
12+
}
13+
14+
#[cfg_attr(feature = "nightly", derive(HashStable_Generic))]
15+
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
16+
pub struct Reg {
17+
pub kind: RegKind,
18+
pub size: Size,
19+
}
20+
21+
macro_rules! reg_ctor {
22+
($name:ident, $kind:ident, $bits:expr) => {
23+
pub fn $name() -> Reg {
24+
Reg { kind: RegKind::$kind, size: Size::from_bits($bits) }
25+
}
26+
};
27+
}
28+
29+
impl Reg {
30+
reg_ctor!(i8, Integer, 8);
31+
reg_ctor!(i16, Integer, 16);
32+
reg_ctor!(i32, Integer, 32);
33+
reg_ctor!(i64, Integer, 64);
34+
reg_ctor!(i128, Integer, 128);
35+
36+
reg_ctor!(f32, Float, 32);
37+
reg_ctor!(f64, Float, 64);
38+
}
39+
40+
impl Reg {
41+
pub fn align<C: HasDataLayout>(&self, cx: &C) -> Align {
42+
let dl = cx.data_layout();
43+
match self.kind {
44+
RegKind::Integer => match self.size.bits() {
45+
1 => dl.i1_align.abi,
46+
2..=8 => dl.i8_align.abi,
47+
9..=16 => dl.i16_align.abi,
48+
17..=32 => dl.i32_align.abi,
49+
33..=64 => dl.i64_align.abi,
50+
65..=128 => dl.i128_align.abi,
51+
_ => panic!("unsupported integer: {self:?}"),
52+
},
53+
RegKind::Float => match self.size.bits() {
54+
16 => dl.f16_align.abi,
55+
32 => dl.f32_align.abi,
56+
64 => dl.f64_align.abi,
57+
128 => dl.f128_align.abi,
58+
_ => panic!("unsupported float: {self:?}"),
59+
},
60+
RegKind::Vector => dl.vector_align(self.size).abi,
61+
}
62+
}
63+
}
File renamed without changes.

compiler/rustc_abi/src/layout/ty.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
use std::fmt;
22
use std::ops::Deref;
33

4-
use Float::*;
5-
use Primitive::*;
64
use rustc_data_structures::intern::Interned;
75
use rustc_macros::HashStable_Generic;
86

7+
use crate::{
8+
AbiAndPrefAlign, Align, BackendRepr, FieldsShape, Float, HasDataLayout, LayoutData, Niche,
9+
PointeeInfo, Primitive, Scalar, Size, TargetDataLayout, Variants,
10+
};
11+
912
// Explicitly import `Float` to avoid ambiguity with `Primitive::Float`.
10-
use crate::{Float, *};
1113

1214
rustc_index::newtype_index! {
1315
/// The *source-order* index of a field in a variant.
@@ -197,7 +199,9 @@ impl<'a, Ty> TyAndLayout<'a, Ty> {
197199
C: HasDataLayout,
198200
{
199201
match self.backend_repr {
200-
BackendRepr::Scalar(scalar) => matches!(scalar.primitive(), Float(F32 | F64)),
202+
BackendRepr::Scalar(scalar) => {
203+
matches!(scalar.primitive(), Primitive::Float(Float::F32 | Float::F64))
204+
}
201205
BackendRepr::Memory { .. } => {
202206
if self.fields.count() == 1 && self.fields.offset(0).bytes() == 0 {
203207
self.field(cx, 0).is_single_fp_element(cx)

0 commit comments

Comments
 (0)