Skip to content

Commit 303aefb

Browse files
committed
Prefer enum Endian instead of arbitrary String
1 parent 95b4a4f commit 303aefb

File tree

4 files changed

+53
-12
lines changed

4 files changed

+53
-12
lines changed

compiler/rustc_codegen_llvm/src/va_arg.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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.target_endian == "big" {
55+
if size.bytes() < slot_size.bytes() && bx.tcx().sess.target.target_endian.is_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.target_endian == "little");
108+
assert!(bx.tcx().sess.target.target_endian.is_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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ pub fn default_configuration(sess: &Session) -> CrateConfig {
761761
}
762762
}
763763
ret.insert((sym::target_arch, Some(Symbol::intern(arch))));
764-
ret.insert((sym::target_endian, Some(Symbol::intern(end))));
764+
ret.insert((sym::target_endian, Some(Symbol::intern(end.name()))));
765765
ret.insert((sym::target_pointer_width, Some(Symbol::intern(&wordsz))));
766766
ret.insert((sym::target_env, Some(Symbol::intern(env))));
767767
ret.insert((sym::target_vendor, Some(Symbol::intern(vendor))));

compiler/rustc_target/src/abi/mod.rs

Lines changed: 48 additions & 7 deletions
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,15 +155,12 @@ 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.target_endian {
158+
if dl.endian != target.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.target_endian
161+
architecture is {}-endian, while \"target-endian\" is `{}`",
162+
dl.endian.name(),
163+
target.target_endian.name()
164164
));
165165
}
166166

@@ -234,6 +234,47 @@ pub enum Endian {
234234
Big,
235235
}
236236

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

compiler/rustc_target/src/spec/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ pub struct Target {
664664
/// Target triple to pass to LLVM.
665665
pub llvm_target: String,
666666
/// String to use as the `target_endian` `cfg` variable.
667-
pub target_endian: String,
667+
pub target_endian: crate::abi::Endian,
668668
/// Number of bits in a pointer. Influences the `target_pointer_width` `cfg` variable.
669669
pub pointer_width: u32,
670670
/// Width of c_int type
@@ -1143,7 +1143,7 @@ impl Target {
11431143

11441144
let mut base = Target {
11451145
llvm_target: get_req_field("llvm-target")?,
1146-
target_endian: get_req_field("target-endian")?,
1146+
target_endian: get_req_field("target-endian")?.parse()?,
11471147
pointer_width: get_req_field("target-pointer-width")?
11481148
.parse::<u32>()
11491149
.map_err(|_| "target-pointer-width must be an integer".to_string())?,

0 commit comments

Comments
 (0)