Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer enum Endian instead of arbitrary String in rustc_target::Target #77604

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions compiler/rustc_codegen_llvm/src/va_arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fn emit_direct_ptr_va_arg(
let next = bx.inbounds_gep(addr, &[full_direct_size]);
bx.store(next, va_list_addr, bx.tcx().data_layout.pointer_align.abi);

if size.bytes() < slot_size.bytes() && &*bx.tcx().sess.target.target_endian == "big" {
if size.bytes() < slot_size.bytes() && bx.tcx().sess.target.target_endian.is_big() {
let adjusted_size = bx.cx().const_i32((slot_size.bytes() - size.bytes()) as i32);
let adjusted = bx.inbounds_gep(addr, &[adjusted_size]);
(bx.bitcast(adjusted, bx.cx().type_ptr_to(llty)), addr_align)
Expand Down Expand Up @@ -105,7 +105,7 @@ fn emit_aapcs_va_arg(
let mut end = bx.build_sibling_block("va_arg.end");
let zero = bx.const_i32(0);
let offset_align = Align::from_bytes(4).unwrap();
assert!(&*bx.tcx().sess.target.target_endian == "little");
assert!(bx.tcx().sess.target.target_endian.is_little());

let gr_type = target_ty.is_any_ptr() || target_ty.is_integral();
let (reg_off, reg_top_index, slot_size) = if gr_type {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_session/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ pub fn default_configuration(sess: &Session) -> CrateConfig {
}
}
ret.insert((sym::target_arch, Some(Symbol::intern(arch))));
ret.insert((sym::target_endian, Some(Symbol::intern(end))));
ret.insert((sym::target_endian, Some(Symbol::intern(end.as_str()))));
ret.insert((sym::target_pointer_width, Some(Symbol::intern(&wordsz))));
ret.insert((sym::target_env, Some(Symbol::intern(env))));
ret.insert((sym::target_vendor, Some(Symbol::intern(vendor))));
Expand Down
55 changes: 48 additions & 7 deletions compiler/rustc_target/src/abi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ pub use Primitive::*;
use crate::spec::Target;

use std::convert::{TryFrom, TryInto};
use std::fmt;
use std::num::NonZeroUsize;
use std::ops::{Add, AddAssign, Deref, Mul, Range, RangeInclusive, Sub};
use std::str::FromStr;

use rustc_index::vec::{Idx, IndexVec};
use rustc_macros::HashStable_Generic;
use rustc_serialize::json::{Json, ToJson};
use rustc_span::Span;

pub mod call;
Expand Down Expand Up @@ -152,15 +155,12 @@ impl TargetDataLayout {
}

// Perform consistency checks against the Target information.
let endian_str = match dl.endian {
Endian::Little => "little",
Endian::Big => "big",
};
if endian_str != target.target_endian {
if dl.endian != target.target_endian {
return Err(format!(
"inconsistent target specification: \"data-layout\" claims \
architecture is {}-endian, while \"target-endian\" is `{}`",
endian_str, target.target_endian
architecture is {}-endian, while \"target-endian\" is `{}`",
dl.endian.as_str(),
target.target_endian.as_str()
));
}

Expand Down Expand Up @@ -234,6 +234,47 @@ pub enum Endian {
Big,
}

impl Endian {
pub fn as_str(&self) -> &str {
match self {
Self::Little => "little",
Self::Big => "big",
}
}

pub fn is_big(&self) -> bool {
Self::Big == *self
}

pub fn is_little(&self) -> bool {
Self::Little == *self
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remove the trivial methods is_big and is_little and use comparisons like e == Endian::Little instead?

Copy link
Contributor Author

@tesuji tesuji Oct 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could, but then we have to import the enum type everywhere
while its only usecase is a member of a struct.
With this boring trivial methods, we don't need import the enum type to check its fieldless variants.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think importing it would probably be better, is it a totally different import?

Copy link
Contributor Author

@tesuji tesuji Oct 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How? If it's used to assign value to a variable, I would import them. Here we only check the property of it.

Also I haven't talked about there are already existing cases in the compiler.

}

impl fmt::Debug for Endian {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}

impl FromStr for Endian {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"little" => Ok(Self::Little),
"big" => Ok(Self::Big),
_ => Err(format!(r#"unknown endian: "{}""#, s)),
}
}
}

impl ToJson for Endian {
fn to_json(&self) -> Json {
self.as_str().to_json()
}
}

/// Size of a type in bytes.
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Encodable, Decodable)]
#[derive(HashStable_Generic)]
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_target/src/spec/aarch64_apple_darwin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub fn target() -> Target {

Target {
llvm_target,
target_endian: "little".to_string(),
target_endian: crate::abi::Endian::Little,
pointer_width: 64,
target_c_int_width: "32".to_string(),
data_layout: "e-m:o-i64:64-i128:128-n32:64-S128".to_string(),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_target/src/spec/aarch64_apple_ios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub fn target() -> Target {
let base = opts(Arch::Arm64);
Target {
llvm_target: "arm64-apple-ios".to_string(),
target_endian: "little".to_string(),
target_endian: crate::abi::Endian::Little,
pointer_width: 64,
target_c_int_width: "32".to_string(),
data_layout: "e-m:o-i64:64-i128:128-n32:64-S128".to_string(),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_target/src/spec/aarch64_apple_tvos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub fn target() -> Target {
let base = opts(Arch::Arm64);
Target {
llvm_target: "arm64-apple-tvos".to_string(),
target_endian: "little".to_string(),
target_endian: crate::abi::Endian::Little,
pointer_width: 64,
target_c_int_width: "32".to_string(),
data_layout: "e-m:o-i64:64-i128:128-n32:64-S128".to_string(),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_target/src/spec/aarch64_fuchsia.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub fn target() -> Target {

Target {
llvm_target: "aarch64-fuchsia".to_string(),
target_endian: "little".to_string(),
target_endian: crate::abi::Endian::Little,
pointer_width: 64,
target_c_int_width: "32".to_string(),
data_layout: "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128".to_string(),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_target/src/spec/aarch64_linux_android.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub fn target() -> Target {
base.features = "+neon,+fp-armv8".to_string();
Target {
llvm_target: "aarch64-linux-android".to_string(),
target_endian: "little".to_string(),
target_endian: crate::abi::Endian::Little,
pointer_width: 64,
target_c_int_width: "32".to_string(),
data_layout: "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128".to_string(),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_target/src/spec/aarch64_pc_windows_msvc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub fn target() -> Target {

Target {
llvm_target: "aarch64-pc-windows-msvc".to_string(),
target_endian: "little".to_string(),
target_endian: crate::abi::Endian::Little,
pointer_width: 64,
target_c_int_width: "32".to_string(),
data_layout: "e-m:w-p:64:64-i32:32-i64:64-i128:128-n32:64-S128".to_string(),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_target/src/spec/aarch64_unknown_cloudabi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub fn target() -> Target {

Target {
llvm_target: "aarch64-unknown-cloudabi".to_string(),
target_endian: "little".to_string(),
target_endian: crate::abi::Endian::Little,
pointer_width: 64,
target_c_int_width: "32".to_string(),
data_layout: "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128".to_string(),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_target/src/spec/aarch64_unknown_freebsd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub fn target() -> Target {

Target {
llvm_target: "aarch64-unknown-freebsd".to_string(),
target_endian: "little".to_string(),
target_endian: crate::abi::Endian::Little,
pointer_width: 64,
target_c_int_width: "32".to_string(),
data_layout: "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128".to_string(),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_target/src/spec/aarch64_unknown_hermit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub fn target() -> Target {

Target {
llvm_target: "aarch64-unknown-hermit".to_string(),
target_endian: "little".to_string(),
target_endian: crate::abi::Endian::Little,
pointer_width: 64,
target_c_int_width: "32".to_string(),
data_layout: "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128".to_string(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub fn target() -> Target {

Target {
llvm_target: "aarch64-unknown-linux-gnu".to_string(),
target_endian: "little".to_string(),
target_endian: crate::abi::Endian::Little,
pointer_width: 64,
target_c_int_width: "32".to_string(),
target_env: "gnu".to_string(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub fn target() -> Target {

Target {
llvm_target: "aarch64-unknown-linux-musl".to_string(),
target_endian: "little".to_string(),
target_endian: crate::abi::Endian::Little,
pointer_width: 64,
target_c_int_width: "32".to_string(),
target_env: "musl".to_string(),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_target/src/spec/aarch64_unknown_netbsd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub fn target() -> Target {

Target {
llvm_target: "aarch64-unknown-netbsd".to_string(),
target_endian: "little".to_string(),
target_endian: crate::abi::Endian::Little,
pointer_width: 64,
target_c_int_width: "32".to_string(),
data_layout: "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128".to_string(),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_target/src/spec/aarch64_unknown_none.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn target() -> Target {
};
Target {
llvm_target: "aarch64-unknown-none".to_string(),
target_endian: "little".to_string(),
target_endian: crate::abi::Endian::Little,
pointer_width: 64,
target_c_int_width: "32".to_string(),
target_os: "none".to_string(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn target() -> Target {
};
Target {
llvm_target: "aarch64-unknown-none".to_string(),
target_endian: "little".to_string(),
target_endian: crate::abi::Endian::Little,
pointer_width: 64,
target_c_int_width: "32".to_string(),
target_os: "none".to_string(),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_target/src/spec/aarch64_unknown_openbsd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub fn target() -> Target {

Target {
llvm_target: "aarch64-unknown-openbsd".to_string(),
target_endian: "little".to_string(),
target_endian: crate::abi::Endian::Little,
pointer_width: 64,
target_c_int_width: "32".to_string(),
data_layout: "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128".to_string(),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_target/src/spec/aarch64_unknown_redox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub fn target() -> Target {

Target {
llvm_target: "aarch64-unknown-redox".to_string(),
target_endian: "little".to_string(),
target_endian: crate::abi::Endian::Little,
pointer_width: 64,
target_c_int_width: "32".to_string(),
data_layout: "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128".to_string(),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_target/src/spec/aarch64_uwp_windows_msvc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub fn target() -> Target {

Target {
llvm_target: "aarch64-pc-windows-msvc".to_string(),
target_endian: "little".to_string(),
target_endian: crate::abi::Endian::Little,
pointer_width: 64,
target_c_int_width: "32".to_string(),
data_layout: "e-m:w-p:64:64-i32:32-i64:64-i128:128-n32:64-S128".to_string(),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_target/src/spec/aarch64_wrs_vxworks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub fn target() -> Target {

Target {
llvm_target: "aarch64-unknown-linux-gnu".to_string(),
target_endian: "little".to_string(),
target_endian: crate::abi::Endian::Little,
pointer_width: 64,
target_c_int_width: "32".to_string(),
data_layout: "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128".to_string(),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_target/src/spec/arm_linux_androideabi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub fn target() -> Target {

Target {
llvm_target: "arm-linux-androideabi".to_string(),
target_endian: "little".to_string(),
target_endian: crate::abi::Endian::Little,
pointer_width: 32,
target_c_int_width: "32".to_string(),
data_layout: "e-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64".to_string(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub fn target() -> Target {
base.max_atomic_width = Some(64);
Target {
llvm_target: "arm-unknown-linux-gnueabi".to_string(),
target_endian: "little".to_string(),
target_endian: crate::abi::Endian::Little,
pointer_width: 32,
target_c_int_width: "32".to_string(),
data_layout: "e-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64".to_string(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub fn target() -> Target {
base.max_atomic_width = Some(64);
Target {
llvm_target: "arm-unknown-linux-gnueabihf".to_string(),
target_endian: "little".to_string(),
target_endian: crate::abi::Endian::Little,
pointer_width: 32,
target_c_int_width: "32".to_string(),
data_layout: "e-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64".to_string(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn target() -> Target {
// to determine the calling convention and float ABI, and it doesn't
// support the "musleabi" value.
llvm_target: "arm-unknown-linux-gnueabi".to_string(),
target_endian: "little".to_string(),
target_endian: crate::abi::Endian::Little,
pointer_width: 32,
target_c_int_width: "32".to_string(),
data_layout: "e-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64".to_string(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn target() -> Target {
// uses it to determine the calling convention and float ABI, and it
// doesn't support the "musleabihf" value.
llvm_target: "arm-unknown-linux-gnueabihf".to_string(),
target_endian: "little".to_string(),
target_endian: crate::abi::Endian::Little,
pointer_width: 32,
target_c_int_width: "32".to_string(),
data_layout: "e-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64".to_string(),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_target/src/spec/armebv7r_none_eabi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::spec::{Target, TargetOptions};
pub fn target() -> Target {
Target {
llvm_target: "armebv7r-unknown-none-eabi".to_string(),
target_endian: "big".to_string(),
target_endian: crate::abi::Endian::Big,
pointer_width: 32,
target_c_int_width: "32".to_string(),
data_layout: "E-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64".to_string(),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_target/src/spec/armebv7r_none_eabihf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::spec::{Target, TargetOptions};
pub fn target() -> Target {
Target {
llvm_target: "armebv7r-unknown-none-eabihf".to_string(),
target_endian: "big".to_string(),
target_endian: crate::abi::Endian::Big,
pointer_width: 32,
target_c_int_width: "32".to_string(),
data_layout: "E-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64".to_string(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub fn target() -> Target {
let base = super::linux_base::opts();
Target {
llvm_target: "armv4t-unknown-linux-gnueabi".to_string(),
target_endian: "little".to_string(),
target_endian: crate::abi::Endian::Little,
pointer_width: 32,
target_c_int_width: "32".to_string(),
data_layout: "e-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64".to_string(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub fn target() -> Target {
let base = super::linux_base::opts();
Target {
llvm_target: "armv5te-unknown-linux-gnueabi".to_string(),
target_endian: "little".to_string(),
target_endian: crate::abi::Endian::Little,
pointer_width: 32,
target_c_int_width: "32".to_string(),
data_layout: "e-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64".to_string(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub fn target() -> Target {
// uses it to determine the calling convention and float ABI, and LLVM
// doesn't support the "musleabihf" value.
llvm_target: "armv5te-unknown-linux-gnueabi".to_string(),
target_endian: "little".to_string(),
target_endian: crate::abi::Endian::Little,
pointer_width: 32,
target_c_int_width: "32".to_string(),
data_layout: "e-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64".to_string(),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_target/src/spec/armv6_unknown_freebsd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub fn target() -> Target {
let base = super::freebsd_base::opts();
Target {
llvm_target: "armv6-unknown-freebsd-gnueabihf".to_string(),
target_endian: "little".to_string(),
target_endian: crate::abi::Endian::Little,
pointer_width: 32,
target_c_int_width: "32".to_string(),
data_layout: "e-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64".to_string(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub fn target() -> Target {
base.max_atomic_width = Some(64);
Target {
llvm_target: "armv6-unknown-netbsdelf-eabihf".to_string(),
target_endian: "little".to_string(),
target_endian: crate::abi::Endian::Little,
pointer_width: 32,
target_c_int_width: "32".to_string(),
data_layout: "e-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64".to_string(),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_target/src/spec/armv7_apple_ios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub fn target() -> Target {
let base = opts(Arch::Armv7);
Target {
llvm_target: "armv7-apple-ios".to_string(),
target_endian: "little".to_string(),
target_endian: crate::abi::Endian::Little,
pointer_width: 32,
target_c_int_width: "32".to_string(),
data_layout: "e-m:o-p:32:32-Fi8-f64:32:64-v64:32:64-v128:32:128-a:0:32-n32-S32".to_string(),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_target/src/spec/armv7_linux_androideabi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub fn target() -> Target {

Target {
llvm_target: "armv7-none-linux-android".to_string(),
target_endian: "little".to_string(),
target_endian: crate::abi::Endian::Little,
pointer_width: 32,
target_c_int_width: "32".to_string(),
data_layout: "e-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64".to_string(),
Expand Down
Loading