Skip to content

Commit e02b0f4

Browse files
committedJan 7, 2021
Auto merge of rust-lang#80709 - lzutao:target-enumerate, r=petrochenkov
Limit target endian to an enum instead of free string This is rust-lang#77604 revived.
2 parents c8915ee + 8ee804d commit e02b0f4

29 files changed

+102
-53
lines changed
 

‎compiler/rustc_codegen_llvm/src/va_arg.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_codegen_ssa::{
99
};
1010
use rustc_middle::ty::layout::HasTyCtxt;
1111
use rustc_middle::ty::Ty;
12-
use rustc_target::abi::{Align, HasDataLayout, LayoutOf, Size};
12+
use rustc_target::abi::{Align, Endian, HasDataLayout, LayoutOf, Size};
1313

1414
fn round_pointer_up_to_alignment(
1515
bx: &mut Builder<'a, 'll, 'tcx>,
@@ -52,7 +52,7 @@ fn emit_direct_ptr_va_arg(
5252
let next = bx.inbounds_gep(addr, &[full_direct_size]);
5353
bx.store(next, va_list_addr, bx.tcx().data_layout.pointer_align.abi);
5454

55-
if size.bytes() < slot_size.bytes() && &*bx.tcx().sess.target.endian == "big" {
55+
if size.bytes() < slot_size.bytes() && bx.tcx().sess.target.endian == Endian::Big {
5656
let adjusted_size = bx.cx().const_i32((slot_size.bytes() - size.bytes()) as i32);
5757
let adjusted = bx.inbounds_gep(addr, &[adjusted_size]);
5858
(bx.bitcast(adjusted, bx.cx().type_ptr_to(llty)), addr_align)
@@ -105,7 +105,7 @@ fn emit_aapcs_va_arg(
105105
let mut end = bx.build_sibling_block("va_arg.end");
106106
let zero = bx.const_i32(0);
107107
let offset_align = Align::from_bytes(4).unwrap();
108-
assert!(&*bx.tcx().sess.target.endian == "little");
108+
assert_eq!(bx.tcx().sess.target.endian, Endian::Little);
109109

110110
let gr_type = target_ty.is_any_ptr() || target_ty.is_integral();
111111
let (reg_off, reg_top_index, slot_size) = if gr_type {

‎compiler/rustc_session/src/config.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,7 @@ pub fn default_configuration(sess: &Session) -> CrateConfig {
819819
}
820820
}
821821
ret.insert((sym::target_arch, Some(Symbol::intern(arch))));
822-
ret.insert((sym::target_endian, Some(Symbol::intern(end))));
822+
ret.insert((sym::target_endian, Some(Symbol::intern(end.as_str()))));
823823
ret.insert((sym::target_pointer_width, Some(Symbol::intern(&wordsz))));
824824
ret.insert((sym::target_env, Some(Symbol::intern(env))));
825825
ret.insert((sym::target_vendor, Some(Symbol::intern(vendor))));

‎compiler/rustc_target/src/abi/mod.rs

+41-8
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ pub use Primitive::*;
44
use crate::spec::Target;
55

66
use std::convert::{TryFrom, TryInto};
7+
use std::fmt;
78
use std::num::NonZeroUsize;
89
use std::ops::{Add, AddAssign, Deref, Mul, Range, RangeInclusive, Sub};
10+
use std::str::FromStr;
911

1012
use rustc_index::vec::{Idx, IndexVec};
1113
use rustc_macros::HashStable_Generic;
14+
use rustc_serialize::json::{Json, ToJson};
1215
use rustc_span::Span;
1316

1417
pub mod call;
@@ -152,22 +155,19 @@ impl TargetDataLayout {
152155
}
153156

154157
// Perform consistency checks against the Target information.
155-
let endian_str = match dl.endian {
156-
Endian::Little => "little",
157-
Endian::Big => "big",
158-
};
159-
if endian_str != target.endian {
158+
if dl.endian != target.endian {
160159
return Err(format!(
161160
"inconsistent target specification: \"data-layout\" claims \
162-
architecture is {}-endian, while \"target-endian\" is `{}`",
163-
endian_str, target.endian
161+
architecture is {}-endian, while \"target-endian\" is `{}`",
162+
dl.endian.as_str(),
163+
target.endian.as_str(),
164164
));
165165
}
166166

167167
if dl.pointer_size.bits() != target.pointer_width.into() {
168168
return Err(format!(
169169
"inconsistent target specification: \"data-layout\" claims \
170-
pointers are {}-bit, while \"target-pointer-width\" is `{}`",
170+
pointers are {}-bit, while \"target-pointer-width\" is `{}`",
171171
dl.pointer_size.bits(),
172172
target.pointer_width
173173
));
@@ -234,6 +234,39 @@ pub enum Endian {
234234
Big,
235235
}
236236

237+
impl Endian {
238+
pub fn as_str(&self) -> &'static str {
239+
match self {
240+
Self::Little => "little",
241+
Self::Big => "big",
242+
}
243+
}
244+
}
245+
246+
impl fmt::Debug for Endian {
247+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
248+
f.write_str(self.as_str())
249+
}
250+
}
251+
252+
impl FromStr for Endian {
253+
type Err = String;
254+
255+
fn from_str(s: &str) -> Result<Self, Self::Err> {
256+
match s {
257+
"little" => Ok(Self::Little),
258+
"big" => Ok(Self::Big),
259+
_ => Err(format!(r#"unknown endian: "{}""#, s)),
260+
}
261+
}
262+
}
263+
264+
impl ToJson for Endian {
265+
fn to_json(&self) -> Json {
266+
self.as_str().to_json()
267+
}
268+
}
269+
237270
/// Size of a type in bytes.
238271
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Encodable, Decodable)]
239272
#[derive(HashStable_Generic)]

‎compiler/rustc_target/src/spec/armebv7r_none_eabi.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Targets the Big endian Cortex-R4/R5 processor (ARMv7-R)
22

3+
use crate::abi::Endian;
34
use crate::spec::{LinkerFlavor, LldFlavor, PanicStrategy, RelocModel};
45
use crate::spec::{Target, TargetOptions};
56

@@ -11,7 +12,7 @@ pub fn target() -> Target {
1112
arch: "arm".to_string(),
1213

1314
options: TargetOptions {
14-
endian: "big".to_string(),
15+
endian: Endian::Big,
1516
linker_flavor: LinkerFlavor::Lld(LldFlavor::Ld),
1617
executables: true,
1718
linker: Some("rust-lld".to_owned()),

‎compiler/rustc_target/src/spec/armebv7r_none_eabihf.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Targets the Cortex-R4F/R5F processor (ARMv7-R)
22

3+
use crate::abi::Endian;
34
use crate::spec::{LinkerFlavor, LldFlavor, PanicStrategy, RelocModel};
45
use crate::spec::{Target, TargetOptions};
56

@@ -11,7 +12,7 @@ pub fn target() -> Target {
1112
arch: "arm".to_string(),
1213

1314
options: TargetOptions {
14-
endian: "big".to_string(),
15+
endian: Endian::Big,
1516
linker_flavor: LinkerFlavor::Lld(LldFlavor::Ld),
1617
executables: true,
1718
linker: Some("rust-lld".to_owned()),

‎compiler/rustc_target/src/spec/mips64_unknown_linux_gnuabi64.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::abi::Endian;
12
use crate::spec::{Target, TargetOptions};
23

34
pub fn target() -> Target {
@@ -7,7 +8,7 @@ pub fn target() -> Target {
78
data_layout: "E-m:e-i8:8:32-i16:16:32-i64:64-n32:64-S128".to_string(),
89
arch: "mips64".to_string(),
910
options: TargetOptions {
10-
endian: "big".to_string(),
11+
endian: Endian::Big,
1112
// NOTE(mips64r2) matches C toolchain
1213
cpu: "mips64r2".to_string(),
1314
features: "+mips64r2".to_string(),

‎compiler/rustc_target/src/spec/mips64_unknown_linux_muslabi64.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::abi::Endian;
12
use crate::spec::{Target, TargetOptions};
23

34
pub fn target() -> Target {
@@ -11,6 +12,6 @@ pub fn target() -> Target {
1112
pointer_width: 64,
1213
data_layout: "E-m:e-i8:8:32-i16:16:32-i64:64-n32:64-S128".to_string(),
1314
arch: "mips64".to_string(),
14-
options: TargetOptions { endian: "big".to_string(), mcount: "_mcount".to_string(), ..base },
15+
options: TargetOptions { endian: Endian::Big, mcount: "_mcount".to_string(), ..base },
1516
}
1617
}

‎compiler/rustc_target/src/spec/mips_unknown_linux_gnu.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::abi::Endian;
12
use crate::spec::{Target, TargetOptions};
23

34
pub fn target() -> Target {
@@ -7,7 +8,7 @@ pub fn target() -> Target {
78
data_layout: "E-m:m-p:32:32-i8:8:32-i16:16:32-i64:64-n32-S64".to_string(),
89
arch: "mips".to_string(),
910
options: TargetOptions {
10-
endian: "big".to_string(),
11+
endian: Endian::Big,
1112
cpu: "mips32r2".to_string(),
1213
features: "+mips32r2,+fpxx,+nooddspreg".to_string(),
1314
max_atomic_width: Some(32),

‎compiler/rustc_target/src/spec/mips_unknown_linux_musl.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::abi::Endian;
12
use crate::spec::{Target, TargetOptions};
23

34
pub fn target() -> Target {
@@ -11,6 +12,6 @@ pub fn target() -> Target {
1112
pointer_width: 32,
1213
data_layout: "E-m:m-p:32:32-i8:8:32-i16:16:32-i64:64-n32-S64".to_string(),
1314
arch: "mips".to_string(),
14-
options: TargetOptions { endian: "big".to_string(), mcount: "_mcount".to_string(), ..base },
15+
options: TargetOptions { endian: Endian::Big, mcount: "_mcount".to_string(), ..base },
1516
}
1617
}

‎compiler/rustc_target/src/spec/mips_unknown_linux_uclibc.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::abi::Endian;
12
use crate::spec::{Target, TargetOptions};
23

34
pub fn target() -> Target {
@@ -7,7 +8,7 @@ pub fn target() -> Target {
78
data_layout: "E-m:m-p:32:32-i8:8:32-i16:16:32-i64:64-n32-S64".to_string(),
89
arch: "mips".to_string(),
910
options: TargetOptions {
10-
endian: "big".to_string(),
11+
endian: Endian::Big,
1112
cpu: "mips32r2".to_string(),
1213
features: "+mips32r2,+soft-float".to_string(),
1314
max_atomic_width: Some(32),

‎compiler/rustc_target/src/spec/mipsisa32r6_unknown_linux_gnu.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::abi::Endian;
12
use crate::spec::{Target, TargetOptions};
23

34
pub fn target() -> Target {
@@ -7,7 +8,7 @@ pub fn target() -> Target {
78
data_layout: "E-m:m-p:32:32-i8:8:32-i16:16:32-i64:64-n32-S64".to_string(),
89
arch: "mips".to_string(),
910
options: TargetOptions {
10-
endian: "big".to_string(),
11+
endian: Endian::Big,
1112
cpu: "mips32r6".to_string(),
1213
features: "+mips32r6".to_string(),
1314
max_atomic_width: Some(32),

‎compiler/rustc_target/src/spec/mipsisa64r6_unknown_linux_gnuabi64.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::abi::Endian;
12
use crate::spec::{Target, TargetOptions};
23

34
pub fn target() -> Target {
@@ -7,7 +8,7 @@ pub fn target() -> Target {
78
data_layout: "E-m:e-i8:8:32-i16:16:32-i64:64-n32:64-S128".to_string(),
89
arch: "mips64".to_string(),
910
options: TargetOptions {
10-
endian: "big".to_string(),
11+
endian: Endian::Big,
1112
// NOTE(mips64r6) matches C toolchain
1213
cpu: "mips64r6".to_string(),
1314
features: "+mips64r6".to_string(),

‎compiler/rustc_target/src/spec/mod.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
//! the target's settings, though `target-feature` and `link-args` will *add*
3535
//! to the list specified by the target, rather than replace.
3636
37+
use crate::abi::Endian;
3738
use crate::spec::abi::{lookup as lookup_abi, Abi};
3839
use crate::spec::crt_objects::{CrtObjects, CrtObjectsFallback};
3940
use rustc_serialize::json::{Json, ToJson};
@@ -705,8 +706,8 @@ pub struct TargetOptions {
705706
/// Whether the target is built-in or loaded from a custom target specification.
706707
pub is_builtin: bool,
707708

708-
/// String to use as the `target_endian` `cfg` variable. Defaults to "little".
709-
pub endian: String,
709+
/// Used as the `target_endian` `cfg` variable. Defaults to little endian.
710+
pub endian: Endian,
710711
/// Width of c_int type. Defaults to "32".
711712
pub c_int_width: String,
712713
/// OS name to use for conditional compilation (`target_os`). Defaults to "none".
@@ -1010,7 +1011,7 @@ impl Default for TargetOptions {
10101011
fn default() -> TargetOptions {
10111012
TargetOptions {
10121013
is_builtin: false,
1013-
endian: "little".to_string(),
1014+
endian: Endian::Little,
10141015
c_int_width: "32".to_string(),
10151016
os: "none".to_string(),
10161017
env: String::new(),
@@ -1439,8 +1440,10 @@ impl Target {
14391440
} );
14401441
}
14411442

1443+
if let Some(s) = obj.find("target-endian").and_then(Json::as_string) {
1444+
base.endian = s.parse()?;
1445+
}
14421446
key!(is_builtin, bool);
1443-
key!(endian = "target-endian");
14441447
key!(c_int_width = "target-c-int-width");
14451448
key!(os);
14461449
key!(env);

‎compiler/rustc_target/src/spec/powerpc64_unknown_freebsd.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::abi::Endian;
12
use crate::spec::{LinkerFlavor, Target, TargetOptions};
23

34
pub fn target() -> Target {
@@ -11,6 +12,6 @@ pub fn target() -> Target {
1112
pointer_width: 64,
1213
data_layout: "E-m:e-i64:64-n32:64".to_string(),
1314
arch: "powerpc64".to_string(),
14-
options: TargetOptions { endian: "big".to_string(), mcount: "_mcount".to_string(), ..base },
15+
options: TargetOptions { endian: Endian::Big, mcount: "_mcount".to_string(), ..base },
1516
}
1617
}

‎compiler/rustc_target/src/spec/powerpc64_unknown_linux_gnu.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::abi::Endian;
12
use crate::spec::{LinkerFlavor, RelroLevel, Target, TargetOptions};
23

34
pub fn target() -> Target {
@@ -15,6 +16,6 @@ pub fn target() -> Target {
1516
pointer_width: 64,
1617
data_layout: "E-m:e-i64:64-n32:64".to_string(),
1718
arch: "powerpc64".to_string(),
18-
options: TargetOptions { endian: "big".to_string(), mcount: "_mcount".to_string(), ..base },
19+
options: TargetOptions { endian: Endian::Big, mcount: "_mcount".to_string(), ..base },
1920
}
2021
}

‎compiler/rustc_target/src/spec/powerpc64_unknown_linux_musl.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::abi::Endian;
12
use crate::spec::{LinkerFlavor, Target, TargetOptions};
23

34
pub fn target() -> Target {
@@ -11,6 +12,6 @@ pub fn target() -> Target {
1112
pointer_width: 64,
1213
data_layout: "E-m:e-i64:64-n32:64".to_string(),
1314
arch: "powerpc64".to_string(),
14-
options: TargetOptions { endian: "big".to_string(), mcount: "_mcount".to_string(), ..base },
15+
options: TargetOptions { endian: Endian::Big, mcount: "_mcount".to_string(), ..base },
1516
}
1617
}

‎compiler/rustc_target/src/spec/powerpc64_wrs_vxworks.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::abi::Endian;
12
use crate::spec::{LinkerFlavor, Target, TargetOptions};
23

34
pub fn target() -> Target {
@@ -11,6 +12,6 @@ pub fn target() -> Target {
1112
pointer_width: 64,
1213
data_layout: "E-m:e-i64:64-n32:64".to_string(),
1314
arch: "powerpc64".to_string(),
14-
options: TargetOptions { endian: "big".to_string(), ..base },
15+
options: TargetOptions { endian: Endian::Big, ..base },
1516
}
1617
}

‎compiler/rustc_target/src/spec/powerpc_unknown_linux_gnu.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::abi::Endian;
12
use crate::spec::{LinkerFlavor, Target, TargetOptions};
23

34
pub fn target() -> Target {
@@ -10,6 +11,6 @@ pub fn target() -> Target {
1011
pointer_width: 32,
1112
data_layout: "E-m:e-p:32:32-i64:64-n32".to_string(),
1213
arch: "powerpc".to_string(),
13-
options: TargetOptions { endian: "big".to_string(), mcount: "_mcount".to_string(), ..base },
14+
options: TargetOptions { endian: Endian::Big, mcount: "_mcount".to_string(), ..base },
1415
}
1516
}

‎compiler/rustc_target/src/spec/powerpc_unknown_linux_gnuspe.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::abi::Endian;
12
use crate::spec::{LinkerFlavor, Target, TargetOptions};
23

34
pub fn target() -> Target {
@@ -10,6 +11,6 @@ pub fn target() -> Target {
1011
pointer_width: 32,
1112
data_layout: "E-m:e-p:32:32-i64:64-n32".to_string(),
1213
arch: "powerpc".to_string(),
13-
options: TargetOptions { endian: "big".to_string(), mcount: "_mcount".to_string(), ..base },
14+
options: TargetOptions { endian: Endian::Big, mcount: "_mcount".to_string(), ..base },
1415
}
1516
}

‎compiler/rustc_target/src/spec/powerpc_unknown_linux_musl.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::abi::Endian;
12
use crate::spec::{LinkerFlavor, Target, TargetOptions};
23

34
pub fn target() -> Target {
@@ -10,6 +11,6 @@ pub fn target() -> Target {
1011
pointer_width: 32,
1112
data_layout: "E-m:e-p:32:32-i64:64-n32".to_string(),
1213
arch: "powerpc".to_string(),
13-
options: TargetOptions { endian: "big".to_string(), mcount: "_mcount".to_string(), ..base },
14+
options: TargetOptions { endian: Endian::Big, mcount: "_mcount".to_string(), ..base },
1415
}
1516
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::abi::Endian;
12
use crate::spec::{LinkerFlavor, Target, TargetOptions};
23

34
pub fn target() -> Target {
@@ -10,10 +11,6 @@ pub fn target() -> Target {
1011
pointer_width: 32,
1112
data_layout: "E-m:e-p:32:32-i64:64-n32".to_string(),
1213
arch: "powerpc".to_string(),
13-
options: TargetOptions {
14-
endian: "big".to_string(),
15-
mcount: "__mcount".to_string(),
16-
..base
17-
},
14+
options: TargetOptions { endian: Endian::Big, mcount: "__mcount".to_string(), ..base },
1815
}
1916
}

0 commit comments

Comments
 (0)