From 0d1aa1e0346630189b779da0939e8138a8e6d668 Mon Sep 17 00:00:00 2001 From: est31 Date: Wed, 14 Oct 2020 18:22:10 +0200 Subject: [PATCH] Rename target_pointer_width to pointer_width and turn it into an u32 Rename target_pointer_width to pointer_width because it is already member of the Target struct. The compiler supports only three valid values for target_pointer_width: 16, 32, 64. Thus it can safely be turned into an int. This means less allocations and clones as well as easier handling of the type. --- compiler/rustc_codegen_llvm/src/allocator.rs | 8 ++++---- compiler/rustc_codegen_ssa/src/back/write.rs | 4 ++-- compiler/rustc_session/src/config.rs | 21 ++++++++++---------- compiler/rustc_target/src/abi/mod.rs | 4 ++-- compiler/rustc_target/src/spec/mod.rs | 15 +++++++++----- 5 files changed, 28 insertions(+), 24 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/allocator.rs b/compiler/rustc_codegen_llvm/src/allocator.rs index e028b2c2dc7..237ef688f88 100644 --- a/compiler/rustc_codegen_llvm/src/allocator.rs +++ b/compiler/rustc_codegen_llvm/src/allocator.rs @@ -16,10 +16,10 @@ pub(crate) unsafe fn codegen( ) { let llcx = &*mods.llcx; let llmod = mods.llmod(); - let usize = match &tcx.sess.target.target.target_pointer_width[..] { - "16" => llvm::LLVMInt16TypeInContext(llcx), - "32" => llvm::LLVMInt32TypeInContext(llcx), - "64" => llvm::LLVMInt64TypeInContext(llcx), + let usize = match tcx.sess.target.target.pointer_width { + 16 => llvm::LLVMInt16TypeInContext(llcx), + 32 => llvm::LLVMInt32TypeInContext(llcx), + 64 => llvm::LLVMInt64TypeInContext(llcx), tws => bug!("Unsupported target word size for int: {}", tws), }; let i8 = llvm::LLVMInt8TypeInContext(llcx); diff --git a/compiler/rustc_codegen_ssa/src/back/write.rs b/compiler/rustc_codegen_ssa/src/back/write.rs index a586b37361e..a2cd030ac0e 100644 --- a/compiler/rustc_codegen_ssa/src/back/write.rs +++ b/compiler/rustc_codegen_ssa/src/back/write.rs @@ -307,7 +307,7 @@ pub struct CodegenContext { pub allocator_module_config: Arc, pub tm_factory: TargetMachineFactory, pub msvc_imps_needed: bool, - pub target_pointer_width: String, + pub target_pointer_width: u32, pub target_arch: String, pub debuginfo: config::DebugInfo, @@ -1022,7 +1022,7 @@ fn start_executing_work( tm_factory: TargetMachineFactory(backend.target_machine_factory(tcx.sess, ol)), total_cgus, msvc_imps_needed: msvc_imps_needed(tcx), - target_pointer_width: tcx.sess.target.target.target_pointer_width.clone(), + target_pointer_width: tcx.sess.target.target.pointer_width, target_arch: tcx.sess.target.target.arch.clone(), debuginfo: tcx.sess.opts.debuginfo, }; diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs index 231e315a22f..a70ae62539f 100644 --- a/compiler/rustc_session/src/config.rs +++ b/compiler/rustc_session/src/config.rs @@ -742,7 +742,7 @@ pub const fn default_lib_output() -> CrateType { pub fn default_configuration(sess: &Session) -> CrateConfig { let end = &sess.target.target.target_endian; let arch = &sess.target.target.arch; - let wordsz = &sess.target.target.target_pointer_width; + let wordsz = sess.target.target.pointer_width.to_string(); let os = &sess.target.target.target_os; let env = &sess.target.target.target_env; let vendor = &sess.target.target.target_vendor; @@ -767,7 +767,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_pointer_width, Some(Symbol::intern(wordsz)))); + 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)))); if sess.target.target.options.has_elf_tls { @@ -792,7 +792,7 @@ pub fn default_configuration(sess: &Session) -> CrateConfig { }; let s = i.to_string(); insert_atomic(&s, align); - if &s == wordsz { + if s == wordsz { insert_atomic("ptr", layout.pointer_align.abi); } } @@ -844,19 +844,18 @@ pub fn build_target_config(opts: &Options, target_override: Option) -> C ) }); - let ptr_width = match &target.target_pointer_width[..] { - "16" => 16, - "32" => 32, - "64" => 64, - w => early_error( + if !matches!(target.pointer_width, 16 | 32 | 64) { + early_error( opts.error_format, &format!( "target specification was invalid: \ unrecognized target-pointer-width {}", - w + target.pointer_width ), - ), - }; + ) + } + + let ptr_width = target.pointer_width; Config { target, ptr_width } } diff --git a/compiler/rustc_target/src/abi/mod.rs b/compiler/rustc_target/src/abi/mod.rs index 3c1a2ea39d3..047b8cf5cdb 100644 --- a/compiler/rustc_target/src/abi/mod.rs +++ b/compiler/rustc_target/src/abi/mod.rs @@ -164,12 +164,12 @@ impl TargetDataLayout { )); } - if dl.pointer_size.bits().to_string() != target.target_pointer_width { + 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 `{}`", dl.pointer_size.bits(), - target.target_pointer_width + target.pointer_width )); } diff --git a/compiler/rustc_target/src/spec/mod.rs b/compiler/rustc_target/src/spec/mod.rs index 6f400854ec6..2d93b6d5fd4 100644 --- a/compiler/rustc_target/src/spec/mod.rs +++ b/compiler/rustc_target/src/spec/mod.rs @@ -665,8 +665,8 @@ pub struct Target { pub llvm_target: String, /// String to use as the `target_endian` `cfg` variable. pub target_endian: String, - /// String to use as the `target_pointer_width` `cfg` variable. - pub target_pointer_width: String, + /// Number of bits in a pointer. Influences the `target_pointer_width` `cfg` variable. + pub pointer_width: u32, /// Width of c_int type pub target_c_int_width: String, /// OS name to use for conditional compilation. @@ -1111,7 +1111,7 @@ impl Target { /// Maximum integer size in bits that this target can perform atomic /// operations on. pub fn max_atomic_width(&self) -> u64 { - self.options.max_atomic_width.unwrap_or_else(|| self.target_pointer_width.parse().unwrap()) + self.options.max_atomic_width.unwrap_or_else(|| self.pointer_width.into()) } pub fn is_abi_supported(&self, abi: Abi) -> bool { @@ -1144,7 +1144,9 @@ impl Target { let mut base = Target { llvm_target: get_req_field("llvm-target")?, target_endian: get_req_field("target-endian")?, - target_pointer_width: get_req_field("target-pointer-width")?, + pointer_width: get_req_field("target-pointer-width")? + .parse::() + .map_err(|_| "target-pointer-width must be an integer".to_string())?, target_c_int_width: get_req_field("target-c-int-width")?, data_layout: get_req_field("data-layout")?, arch: get_req_field("arch")?, @@ -1617,7 +1619,10 @@ impl ToJson for Target { target_val!(llvm_target); target_val!(target_endian); - target_val!(target_pointer_width); + d.insert( + "target-pointer-width".to_string(), + self.pointer_width.to_string().to_json(), + ); target_val!(target_c_int_width); target_val!(arch); target_val!(target_os, "os");