Skip to content

Commit 0dda4fa

Browse files
rustc_target: stow an AbiMap and expose through accessor
1 parent 867d001 commit 0dda4fa

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

compiler/rustc_target/src/spec/abi_map.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::spec::Target;
66
///
77
/// A maybe-transitional structure circa 2025 for hosting future experiments in
88
/// encapsulating arch-specific ABI lowering details to make them more testable.
9-
#[derive(Clone, Debug)]
9+
#[derive(Clone, Debug, PartialEq)]
1010
pub struct AbiMap {
1111
arch: Arch,
1212
os: OsKind,
@@ -47,10 +47,15 @@ impl AbiMap {
4747
/// create an AbiMap according to arbitrary fields on the [Target]
4848
pub fn from_target(target: &Target) -> Self {
4949
// the purpose of this little exercise is to force listing what affects these mappings
50-
let arch = match &*target.arch {
50+
AbiMap::from_args(&target.arch, &target.llvm_target, target.is_like_windows)
51+
}
52+
53+
/// implementation detail of `AbiMap::from_target`, may replace it entirely soon?
54+
pub(super) fn from_args(arch: &str, llvm_target: &str, is_like_windows: bool) -> Self {
55+
let arch = match arch {
5156
"aarch64" => Arch::Aarch64,
5257
"amdgpu" => Arch::Amdgpu,
53-
"arm" if target.llvm_target.starts_with("thumbv8m") => Arch::Arm(ArmVer::ThumbV8M),
58+
"arm" if llvm_target.starts_with("thumbv8m") => Arch::Arm(ArmVer::ThumbV8M),
5459
"arm" => Arch::Arm(ArmVer::Other),
5560
"avr" => Arch::Avr,
5661
"msp430" => Arch::Msp430,
@@ -60,7 +65,7 @@ impl AbiMap {
6065
"x86_64" => Arch::X86_64,
6166
_ => Arch::Other,
6267
};
63-
let os = if target.is_like_windows { OsKind::Windows } else { OsKind::Other };
68+
let os = if is_like_windows { OsKind::Windows } else { OsKind::Other };
6469
AbiMap { arch, os }
6570
}
6671

compiler/rustc_target/src/spec/json.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -675,14 +675,15 @@ impl Target {
675675
key!(entry_name);
676676
key!(supports_xray, bool);
677677

678-
// we're going to run `update_from_cli`, but that won't change the target's AbiMap
679-
// FIXME: better factor the Target definition so we enforce this on a type level
680-
let abi_map = AbiMap::from_target(&base);
678+
// This API is exposed almost entirely for this
679+
base.abi_map = AbiMap::from_args(&base.arch, &base.llvm_target, base.is_like_windows);
681680

682681
if let Some(abi_str) = obj.remove("entry-abi") {
683682
if let Json::String(abi_str) = abi_str {
684683
match abi_str.parse::<ExternAbi>() {
685-
Ok(abi) => base.options.entry_abi = abi_map.canonize_abi(abi, false).unwrap(),
684+
Ok(abi) => {
685+
base.options.entry_abi = base.abi_map.canonize_abi(abi, false).unwrap()
686+
}
686687
Err(_) => return Err(format!("{abi_str} is not a valid ExternAbi")),
687688
}
688689
} else {

compiler/rustc_target/src/spec/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2259,6 +2259,8 @@ type StaticCow<T> = Cow<'static, T>;
22592259
/// through `Deref` impls.
22602260
#[derive(PartialEq, Clone, Debug)]
22612261
pub struct TargetOptions {
2262+
/// How to translate ABIs for this target
2263+
abi_map: AbiMap,
22622264
/// Used as the `target_endian` `cfg` variable. Defaults to little endian.
22632265
pub endian: Endian,
22642266
/// Width of c_int type. Defaults to "32".
@@ -2758,6 +2760,10 @@ impl Default for TargetOptions {
27582760
/// incomplete, and if used for compilation, will certainly not work.
27592761
fn default() -> TargetOptions {
27602762
TargetOptions {
2763+
// FIXME(jubilee): We shouldn't do this, and this code indicates a backwards ontology.
2764+
// There is no "default target" so we shouldn't let this type be incrementally built.
2765+
// Rather, we should validate the construction in a builder's `.build()`.
2766+
abi_map: AbiMap::from_args("", "", false),
27612767
endian: Endian::Little,
27622768
c_int_width: 32,
27632769
os: "none".into(),
@@ -2904,6 +2910,11 @@ impl Target {
29042910
abi_map.canonize_abi(abi, false).is_mapped()
29052911
}
29062912

2913+
/// how the target translates ABI strings to "real" ABIs
2914+
pub fn abi_map(&self) -> &AbiMap {
2915+
&self.abi_map
2916+
}
2917+
29072918
/// Minimum integer size in bits that this target can perform atomic
29082919
/// operations on.
29092920
pub fn min_atomic_width(&self) -> u64 {

0 commit comments

Comments
 (0)