Skip to content

Commit 9212ced

Browse files
make entry_abi a lowering of the ABI string
this allows removing CanonAbi::try_from_extern_abi and its FromStr
1 parent 4fcc795 commit 9212ced

File tree

2 files changed

+18
-76
lines changed

2 files changed

+18
-76
lines changed

compiler/rustc_abi/src/canon_abi.rs

Lines changed: 1 addition & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use std::cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd};
22
use std::fmt;
33
use std::hash::{Hash, Hasher};
4-
use std::str::FromStr;
54

65
#[cfg(feature = "nightly")]
76
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, StableOrd};
87

9-
use crate::{AbiFromStrErr, ExternAbi};
8+
use crate::ExternAbi;
109

1110
/// Calling convention to determine codegen
1211
///
@@ -134,69 +133,6 @@ impl CanonAbi {
134133
},
135134
}
136135
}
137-
138-
/// convert an ExternAbi to a CanonAbi if it maps directly
139-
///
140-
/// Like `to_erased_extern_abi`, this is only intended for internal usage by
141-
/// CanonAbi's implementation, as it is not "target-aware".
142-
const fn try_from_extern_abi(extern_abi: ExternAbi) -> Result<Self, AbiFromStrErr> {
143-
match extern_abi {
144-
ExternAbi::C { unwind: false } => Ok(Self::C),
145-
ExternAbi::Rust => Ok(Self::Rust),
146-
ExternAbi::RustCold => Ok(Self::RustCold),
147-
148-
// arm
149-
ExternAbi::Aapcs { unwind: false } => Ok(Self::Arm(ArmCall::Aapcs)),
150-
ExternAbi::CCmseNonSecureCall => Ok(Self::Arm(ArmCall::CCmseNonSecureCall)),
151-
ExternAbi::CCmseNonSecureEntry => Ok(Self::Arm(ArmCall::CCmseNonSecureEntry)),
152-
// gpu
153-
ExternAbi::GpuKernel => Ok(Self::GpuKernel),
154-
// interrupt
155-
ExternAbi::AvrInterrupt => Ok(Self::Interrupt(InterruptKind::Avr)),
156-
ExternAbi::AvrNonBlockingInterrupt => {
157-
Ok(Self::Interrupt(InterruptKind::AvrNonBlocking))
158-
}
159-
ExternAbi::Msp430Interrupt => Ok(Self::Interrupt(InterruptKind::Msp430)),
160-
ExternAbi::RiscvInterruptM => Ok(Self::Interrupt(InterruptKind::RiscvMachine)),
161-
ExternAbi::RiscvInterruptS => Ok(Self::Interrupt(InterruptKind::RiscvSupervisor)),
162-
ExternAbi::X86Interrupt => Ok(Self::Interrupt(InterruptKind::X86)),
163-
164-
// x86
165-
ExternAbi::Stdcall { unwind: false } => Ok(Self::X86(X86Call::Stdcall)),
166-
ExternAbi::Fastcall { unwind: false } => Ok(Self::X86(X86Call::Fastcall)),
167-
ExternAbi::Vectorcall { unwind: false } => Ok(Self::X86(X86Call::Vectorcall)),
168-
ExternAbi::Thiscall { unwind: false } => Ok(Self::X86(X86Call::Thiscall)),
169-
ExternAbi::Win64 { unwind: false } => Ok(Self::X86(X86Call::Win64)),
170-
ExternAbi::SysV64 { unwind: false } => Ok(Self::X86(X86Call::SysV64)),
171-
172-
// fanon
173-
ExternAbi::Cdecl { unwind: _ }
174-
| ExternAbi::EfiApi
175-
| ExternAbi::PtxKernel
176-
| ExternAbi::RustCall
177-
| ExternAbi::System { unwind: _ }
178-
| ExternAbi::Unadjusted => Err(AbiFromStrErr::Unknown),
179-
// unwind where it should not unwind
180-
ExternAbi::Aapcs { unwind: true }
181-
| ExternAbi::C { unwind: true }
182-
| ExternAbi::Stdcall { unwind: true }
183-
| ExternAbi::Fastcall { unwind: true }
184-
| ExternAbi::Vectorcall { unwind: true }
185-
| ExternAbi::Thiscall { unwind: true }
186-
| ExternAbi::Win64 { unwind: true }
187-
| ExternAbi::SysV64 { unwind: true } => Err(AbiFromStrErr::NoUnwind),
188-
}
189-
}
190-
}
191-
192-
impl FromStr for CanonAbi {
193-
type Err = AbiFromStrErr;
194-
195-
fn from_str(s: &str) -> Result<Self, Self::Err> {
196-
// parse as ExternAbi and propagate out errors, as CanonAbi uses the same errors
197-
let extern_abi = s.parse()?;
198-
CanonAbi::try_from_extern_abi(extern_abi)
199-
}
200136
}
201137

202138
/// Callee codegen for interrupts

compiler/rustc_target/src/spec/json.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ use std::borrow::Cow;
22
use std::collections::BTreeMap;
33
use std::str::FromStr;
44

5+
use rustc_abi::ExternAbi;
56
use serde_json::Value;
67

78
use super::{Target, TargetKind, TargetOptions, TargetWarnings};
89
use crate::json::{Json, ToJson};
10+
use crate::spec::AbiMap;
911

1012
impl Target {
1113
/// Loads a target descriptor from a JSON object.
@@ -535,17 +537,6 @@ impl Target {
535537
}
536538
}
537539

538-
if let Some(abi_str) = obj.remove("entry-abi") {
539-
if let Json::String(abi_str) = abi_str {
540-
match abi_str.parse() {
541-
Ok(canon_abi) => base.options.entry_abi = canon_abi,
542-
Err(_) => return Err(format!("{abi_str} is not a valid CanonAbi")),
543-
}
544-
} else {
545-
incorrect_type.push("entry-abi".to_owned())
546-
}
547-
}
548-
549540
key!(c_int_width = "target-c-int-width");
550541
key!(c_enum_min_bits, Option<u64>); // if None, matches c_int_width
551542
key!(os);
@@ -661,6 +652,21 @@ impl Target {
661652
key!(entry_name);
662653
key!(supports_xray, bool);
663654

655+
// we're going to run `update_from_cli`, but that won't change the target's AbiMap
656+
// FIXME: better factor the Target definition so we enforce this on a type level
657+
let abi_map = AbiMap::from_target(&base);
658+
659+
if let Some(abi_str) = obj.remove("entry-abi") {
660+
if let Json::String(abi_str) = abi_str {
661+
match abi_str.parse::<ExternAbi>() {
662+
Ok(abi) => base.options.entry_abi = abi_map.canonize_abi(abi, false).unwrap(),
663+
Err(_) => return Err(format!("{abi_str} is not a valid ExternAbi")),
664+
}
665+
} else {
666+
incorrect_type.push("entry-abi".to_owned())
667+
}
668+
}
669+
664670
base.update_from_cli();
665671
base.check_consistency(TargetKind::Json)?;
666672

0 commit comments

Comments
 (0)