Skip to content

Commit fd116c8

Browse files
committed
Add c_enum_min_bits to target spec
1 parent ae90dcf commit fd116c8

File tree

6 files changed

+515
-16
lines changed

6 files changed

+515
-16
lines changed

compiler/rustc_middle/src/ty/layout.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,6 @@ impl IntegerExt for Integer {
112112
let unsigned_fit = Integer::fit_unsigned(cmp::max(min as u128, max as u128));
113113
let signed_fit = cmp::max(Integer::fit_signed(min), Integer::fit_signed(max));
114114

115-
let mut min_from_extern = None;
116-
let min_default = I8;
117-
118115
if let Some(ity) = repr.int {
119116
let discr = Integer::from_attr(&tcx, ity);
120117
let fit = if ity.is_signed() { signed_fit } else { unsigned_fit };
@@ -128,19 +125,14 @@ impl IntegerExt for Integer {
128125
return (discr, ity.is_signed());
129126
}
130127

131-
if repr.c() {
132-
match &tcx.sess.target.arch[..] {
133-
"hexagon" => min_from_extern = Some(I8),
134-
// WARNING: the ARM EABI has two variants; the one corresponding
135-
// to `at_least == I32` appears to be used on Linux and NetBSD,
136-
// but some systems may use the variant corresponding to no
137-
// lower bound. However, we don't run on those yet...?
138-
"arm" => min_from_extern = Some(I32),
139-
_ => min_from_extern = Some(I32),
140-
}
141-
}
142-
143-
let at_least = min_from_extern.unwrap_or(min_default);
128+
let at_least = if repr.c() {
129+
// This is usually I32, however it can be different on some platforms,
130+
// notably hexagon and arm-none/thumb-none
131+
tcx.data_layout().c_enum_min_size
132+
} else {
133+
// repr(Rust) enums try to be as small as possible
134+
I8
135+
};
144136

145137
// If there are no negative values, we can use the unsigned fit.
146138
if min >= 0 {

compiler/rustc_target/src/abi/mod.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ pub struct TargetDataLayout {
3636
pub vector_align: Vec<(Size, AbiAndPrefAlign)>,
3737

3838
pub instruction_address_space: AddressSpace,
39+
40+
/// Minimum size of #[repr(C)] enums (default I32 bits)
41+
pub c_enum_min_size: Integer,
3942
}
4043

4144
impl Default for TargetDataLayout {
@@ -60,6 +63,7 @@ impl Default for TargetDataLayout {
6063
(Size::from_bits(128), AbiAndPrefAlign::new(align(128))),
6164
],
6265
instruction_address_space: AddressSpace::DATA,
66+
c_enum_min_size: Integer::I32,
6367
}
6468
}
6569
}
@@ -173,6 +177,8 @@ impl TargetDataLayout {
173177
));
174178
}
175179

180+
dl.c_enum_min_size = Integer::from_size(Size::from_bits(target.c_enum_min_bits))?;
181+
176182
Ok(dl)
177183
}
178184

@@ -610,6 +616,17 @@ impl Integer {
610616
}
611617
I8
612618
}
619+
620+
fn from_size(size: Size) -> Result<Self, String> {
621+
match size.bits() {
622+
8 => Ok(Integer::I8),
623+
16 => Ok(Integer::I16),
624+
32 => Ok(Integer::I32),
625+
64 => Ok(Integer::I64),
626+
128 => Ok(Integer::I128),
627+
_ => Err(format!("rust does not support integers with {} bits", size.bits())),
628+
}
629+
}
613630
}
614631

615632
/// Fundamental unit of memory access and layout.

compiler/rustc_target/src/spec/hexagon_unknown_linux_musl.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ pub fn target() -> Target {
1313
base.dynamic_linking = true;
1414
base.executables = true;
1515

16+
base.c_enum_min_bits = 8;
17+
1618
Target {
1719
llvm_target: "hexagon-unknown-linux-musl".to_string(),
1820
pointer_width: 32,

compiler/rustc_target/src/spec/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,6 +1335,9 @@ pub struct TargetOptions {
13351335

13361336
/// If present it's a default value to use for adjusting the C ABI.
13371337
pub default_adjusted_cabi: Option<Abi>,
1338+
1339+
/// Minimum number of bits in #[repr(C)] enum. Defaults to 32.
1340+
pub c_enum_min_bits: u64,
13381341
}
13391342

13401343
impl Default for TargetOptions {
@@ -1439,6 +1442,7 @@ impl Default for TargetOptions {
14391442
split_debuginfo: SplitDebuginfo::Off,
14401443
supported_sanitizers: SanitizerSet::empty(),
14411444
default_adjusted_cabi: None,
1445+
c_enum_min_bits: 32,
14421446
}
14431447
}
14441448
}
@@ -1603,6 +1607,12 @@ impl Target {
16031607
base.$key_name = s;
16041608
}
16051609
} );
1610+
($key_name:ident, u64) => ( {
1611+
let name = (stringify!($key_name)).replace("_", "-");
1612+
if let Some(s) = obj.remove_key(&name).and_then(|j| Json::as_u64(&j)) {
1613+
base.$key_name = s;
1614+
}
1615+
} );
16061616
($key_name:ident, Option<u32>) => ( {
16071617
let name = (stringify!($key_name)).replace("_", "-");
16081618
if let Some(s) = obj.remove_key(&name).and_then(|j| Json::as_u64(&j)) {
@@ -2016,6 +2026,7 @@ impl Target {
20162026
key!(split_debuginfo, SplitDebuginfo)?;
20172027
key!(supported_sanitizers, SanitizerSet)?;
20182028
key!(default_adjusted_cabi, Option<Abi>)?;
2029+
key!(c_enum_min_bits, u64);
20192030

20202031
if base.is_builtin {
20212032
// This can cause unfortunate ICEs later down the line.
@@ -2254,6 +2265,7 @@ impl ToJson for Target {
22542265
target_option_val!(has_thumb_interworking);
22552266
target_option_val!(split_debuginfo);
22562267
target_option_val!(supported_sanitizers);
2268+
target_option_val!(c_enum_min_bits);
22572269

22582270
if let Some(abi) = self.default_adjusted_cabi {
22592271
d.insert("default-adjusted-cabi".to_string(), Abi::name(abi).to_json());

src/test/ui/layout/thumb-enum.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// compile-flags: --target thumbv8m.main-none-eabihf
2+
// needs-llvm-components: arm
3+
//
4+
// Verify that thumb targets implement the repr(C) for enums correctly.
5+
//
6+
// See #87917
7+
#![feature(never_type, rustc_attrs, no_core, lang_items)]
8+
#![crate_type = "lib"]
9+
#![no_core]
10+
11+
#[lang="sized"]
12+
trait Sized {}
13+
14+
#[rustc_layout(debug)]
15+
#[repr(C)]
16+
enum A { Apple } //~ ERROR: layout_of
17+
18+
#[rustc_layout(debug)]
19+
#[repr(C)]
20+
enum B { Banana = 255, } //~ ERROR: layout_of
21+
22+
#[rustc_layout(debug)]
23+
#[repr(C)]
24+
enum C { Chaenomeles = 256, } //~ ERROR: layout_of
25+
26+
#[rustc_layout(debug)]
27+
#[repr(C)]
28+
enum P { Peach = 0x1000_0000isize, } //~ ERROR: layout_of
29+
30+
const TANGERINE: usize = 0x8100_0000; // hack to get negative numbers without negation operator!
31+
32+
#[rustc_layout(debug)]
33+
#[repr(C)]
34+
enum T { Tangerine = TANGERINE as isize } //~ ERROR: layout_of

0 commit comments

Comments
 (0)