Skip to content

Commit

Permalink
Prefer enum Endian in rustc_target::Target
Browse files Browse the repository at this point in the history
  • Loading branch information
tesuji committed Jan 6, 2021
1 parent 9530fdc commit fa4d8bc
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 16 deletions.
6 changes: 3 additions & 3 deletions compiler/rustc_codegen_llvm/src/va_arg.rs
Expand Up @@ -9,7 +9,7 @@ use rustc_codegen_ssa::{
};
use rustc_middle::ty::layout::HasTyCtxt;
use rustc_middle::ty::Ty;
use rustc_target::abi::{Align, HasDataLayout, LayoutOf, Size};
use rustc_target::abi::{Align, Endian, HasDataLayout, LayoutOf, Size};

fn round_pointer_up_to_alignment(
bx: &mut Builder<'a, 'll, 'tcx>,
Expand Down 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.endian == "big" {
if size.bytes() < slot_size.bytes() && bx.tcx().sess.target.endian == Endian::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.endian == "little");
assert_eq!(bx.tcx().sess.target.endian, Endian::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
Expand Up @@ -819,7 +819,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
49 changes: 41 additions & 8 deletions compiler/rustc_target/src/abi/mod.rs
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,22 +155,19 @@ 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.endian {
if dl.endian != target.endian {
return Err(format!(
"inconsistent target specification: \"data-layout\" claims \
architecture is {}-endian, while \"target-endian\" is `{}`",
endian_str, target.endian
architecture is {}-endian, while \"target-endian\" is `{}`",
dl.endian.as_str(),
target.endian.as_str(),
));
}

if dl.pointer_size.bits() != target.pointer_width.into() {
return Err(format!(
"inconsistent target specification: \"data-layout\" claims \
pointers are {}-bit, while \"target-pointer-width\" is `{}`",
pointers are {}-bit, while \"target-pointer-width\" is `{}`",
dl.pointer_size.bits(),
target.pointer_width
));
Expand Down Expand Up @@ -234,6 +234,39 @@ pub enum Endian {
Big,
}

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

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
11 changes: 7 additions & 4 deletions compiler/rustc_target/src/spec/mod.rs
Expand Up @@ -34,6 +34,7 @@
//! the target's settings, though `target-feature` and `link-args` will *add*
//! to the list specified by the target, rather than replace.

use crate::abi::Endian;
use crate::spec::abi::{lookup as lookup_abi, Abi};
use crate::spec::crt_objects::{CrtObjects, CrtObjectsFallback};
use rustc_serialize::json::{Json, ToJson};
Expand Down Expand Up @@ -705,8 +706,8 @@ pub struct TargetOptions {
/// Whether the target is built-in or loaded from a custom target specification.
pub is_builtin: bool,

/// String to use as the `target_endian` `cfg` variable. Defaults to "little".
pub endian: String,
/// Used as the `target_endian` `cfg` variable. Defaults to little endian.
pub endian: Endian,
/// Width of c_int type. Defaults to "32".
pub c_int_width: String,
/// OS name to use for conditional compilation (`target_os`). Defaults to "none".
Expand Down Expand Up @@ -1010,7 +1011,7 @@ impl Default for TargetOptions {
fn default() -> TargetOptions {
TargetOptions {
is_builtin: false,
endian: "little".to_string(),
endian: Endian::Little,
c_int_width: "32".to_string(),
os: "none".to_string(),
env: String::new(),
Expand Down Expand Up @@ -1439,8 +1440,10 @@ impl Target {
} );
}

if let Some(s) = obj.find("target-endian").and_then(Json::as_string) {
base.endian = s.parse()?;
}
key!(is_builtin, bool);
key!(endian = "target-endian");
key!(c_int_width = "target-c-int-width");
key!(os);
key!(env);
Expand Down

0 comments on commit fa4d8bc

Please sign in to comment.