Skip to content

Commit 0d1aa1e

Browse files
committed
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.
1 parent 64ba25d commit 0d1aa1e

File tree

5 files changed

+28
-24
lines changed

5 files changed

+28
-24
lines changed

compiler/rustc_codegen_llvm/src/allocator.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ pub(crate) unsafe fn codegen(
1616
) {
1717
let llcx = &*mods.llcx;
1818
let llmod = mods.llmod();
19-
let usize = match &tcx.sess.target.target.target_pointer_width[..] {
20-
"16" => llvm::LLVMInt16TypeInContext(llcx),
21-
"32" => llvm::LLVMInt32TypeInContext(llcx),
22-
"64" => llvm::LLVMInt64TypeInContext(llcx),
19+
let usize = match tcx.sess.target.target.pointer_width {
20+
16 => llvm::LLVMInt16TypeInContext(llcx),
21+
32 => llvm::LLVMInt32TypeInContext(llcx),
22+
64 => llvm::LLVMInt64TypeInContext(llcx),
2323
tws => bug!("Unsupported target word size for int: {}", tws),
2424
};
2525
let i8 = llvm::LLVMInt8TypeInContext(llcx);

compiler/rustc_codegen_ssa/src/back/write.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ pub struct CodegenContext<B: WriteBackendMethods> {
307307
pub allocator_module_config: Arc<ModuleConfig>,
308308
pub tm_factory: TargetMachineFactory<B>,
309309
pub msvc_imps_needed: bool,
310-
pub target_pointer_width: String,
310+
pub target_pointer_width: u32,
311311
pub target_arch: String,
312312
pub debuginfo: config::DebugInfo,
313313

@@ -1022,7 +1022,7 @@ fn start_executing_work<B: ExtraBackendMethods>(
10221022
tm_factory: TargetMachineFactory(backend.target_machine_factory(tcx.sess, ol)),
10231023
total_cgus,
10241024
msvc_imps_needed: msvc_imps_needed(tcx),
1025-
target_pointer_width: tcx.sess.target.target.target_pointer_width.clone(),
1025+
target_pointer_width: tcx.sess.target.target.pointer_width,
10261026
target_arch: tcx.sess.target.target.arch.clone(),
10271027
debuginfo: tcx.sess.opts.debuginfo,
10281028
};

compiler/rustc_session/src/config.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,7 @@ pub const fn default_lib_output() -> CrateType {
742742
pub fn default_configuration(sess: &Session) -> CrateConfig {
743743
let end = &sess.target.target.target_endian;
744744
let arch = &sess.target.target.arch;
745-
let wordsz = &sess.target.target.target_pointer_width;
745+
let wordsz = sess.target.target.pointer_width.to_string();
746746
let os = &sess.target.target.target_os;
747747
let env = &sess.target.target.target_env;
748748
let vendor = &sess.target.target.target_vendor;
@@ -767,7 +767,7 @@ pub fn default_configuration(sess: &Session) -> CrateConfig {
767767
}
768768
ret.insert((sym::target_arch, Some(Symbol::intern(arch))));
769769
ret.insert((sym::target_endian, Some(Symbol::intern(end))));
770-
ret.insert((sym::target_pointer_width, Some(Symbol::intern(wordsz))));
770+
ret.insert((sym::target_pointer_width, Some(Symbol::intern(&wordsz))));
771771
ret.insert((sym::target_env, Some(Symbol::intern(env))));
772772
ret.insert((sym::target_vendor, Some(Symbol::intern(vendor))));
773773
if sess.target.target.options.has_elf_tls {
@@ -792,7 +792,7 @@ pub fn default_configuration(sess: &Session) -> CrateConfig {
792792
};
793793
let s = i.to_string();
794794
insert_atomic(&s, align);
795-
if &s == wordsz {
795+
if s == wordsz {
796796
insert_atomic("ptr", layout.pointer_align.abi);
797797
}
798798
}
@@ -844,19 +844,18 @@ pub fn build_target_config(opts: &Options, target_override: Option<Target>) -> C
844844
)
845845
});
846846

847-
let ptr_width = match &target.target_pointer_width[..] {
848-
"16" => 16,
849-
"32" => 32,
850-
"64" => 64,
851-
w => early_error(
847+
if !matches!(target.pointer_width, 16 | 32 | 64) {
848+
early_error(
852849
opts.error_format,
853850
&format!(
854851
"target specification was invalid: \
855852
unrecognized target-pointer-width {}",
856-
w
853+
target.pointer_width
857854
),
858-
),
859-
};
855+
)
856+
}
857+
858+
let ptr_width = target.pointer_width;
860859

861860
Config { target, ptr_width }
862861
}

compiler/rustc_target/src/abi/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,12 @@ impl TargetDataLayout {
164164
));
165165
}
166166

167-
if dl.pointer_size.bits().to_string() != target.target_pointer_width {
167+
if dl.pointer_size.bits() != target.pointer_width.into() {
168168
return Err(format!(
169169
"inconsistent target specification: \"data-layout\" claims \
170170
pointers are {}-bit, while \"target-pointer-width\" is `{}`",
171171
dl.pointer_size.bits(),
172-
target.target_pointer_width
172+
target.pointer_width
173173
));
174174
}
175175

compiler/rustc_target/src/spec/mod.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -665,8 +665,8 @@ pub struct Target {
665665
pub llvm_target: String,
666666
/// String to use as the `target_endian` `cfg` variable.
667667
pub target_endian: String,
668-
/// String to use as the `target_pointer_width` `cfg` variable.
669-
pub target_pointer_width: String,
668+
/// Number of bits in a pointer. Influences the `target_pointer_width` `cfg` variable.
669+
pub pointer_width: u32,
670670
/// Width of c_int type
671671
pub target_c_int_width: String,
672672
/// OS name to use for conditional compilation.
@@ -1111,7 +1111,7 @@ impl Target {
11111111
/// Maximum integer size in bits that this target can perform atomic
11121112
/// operations on.
11131113
pub fn max_atomic_width(&self) -> u64 {
1114-
self.options.max_atomic_width.unwrap_or_else(|| self.target_pointer_width.parse().unwrap())
1114+
self.options.max_atomic_width.unwrap_or_else(|| self.pointer_width.into())
11151115
}
11161116

11171117
pub fn is_abi_supported(&self, abi: Abi) -> bool {
@@ -1144,7 +1144,9 @@ impl Target {
11441144
let mut base = Target {
11451145
llvm_target: get_req_field("llvm-target")?,
11461146
target_endian: get_req_field("target-endian")?,
1147-
target_pointer_width: get_req_field("target-pointer-width")?,
1147+
pointer_width: get_req_field("target-pointer-width")?
1148+
.parse::<u32>()
1149+
.map_err(|_| "target-pointer-width must be an integer".to_string())?,
11481150
target_c_int_width: get_req_field("target-c-int-width")?,
11491151
data_layout: get_req_field("data-layout")?,
11501152
arch: get_req_field("arch")?,
@@ -1617,7 +1619,10 @@ impl ToJson for Target {
16171619

16181620
target_val!(llvm_target);
16191621
target_val!(target_endian);
1620-
target_val!(target_pointer_width);
1622+
d.insert(
1623+
"target-pointer-width".to_string(),
1624+
self.pointer_width.to_string().to_json(),
1625+
);
16211626
target_val!(target_c_int_width);
16221627
target_val!(arch);
16231628
target_val!(target_os, "os");

0 commit comments

Comments
 (0)