3 use rustc_macros::HashStable_Generic;
4 use rustc_span::symbol::sym;
5 use rustc_span::{Span, Symbol};
10 #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Debug)]
11 #[derive(HashStable_Generic, Encodable, Decodable)]
13 // Some of the ABIs come first because every time we add a new ABI, we have to re-bless all the
14 // hashing tests. These are used in many places, so giving them stable values reduces test
15 // churn. The specific values are meaningless.
18 Cdecl { unwind: bool },
19 Stdcall { unwind: bool },
20 Fastcall { unwind: bool },
21 Vectorcall { unwind: bool },
22 Thiscall { unwind: bool },
23 Aapcs { unwind: bool },
24 Win64 { unwind: bool },
25 SysV64 { unwind: bool },
32 AvrNonBlockingInterrupt,
35 System { unwind: bool },
44 pub fn supports_varargs(self) -> bool {
45 // * C and Cdecl obviously support varargs.
46 // * C can be based on SysV64 or Win64, so they must support varargs.
47 // * EfiApi is based on Win64 or C, so it also supports it.
49 // * Stdcall does not, because it would be impossible for the callee to clean
50 // up the arguments. (callee doesn't know how many arguments are there)
51 // * Same for Fastcall, Vectorcall and Thiscall.
52 // * System can become Stdcall, so is also a no-no.
53 // * Other calling conventions are related to hardware or the compiler itself.
59 | Self::EfiApi => true,
65 #[derive(Copy, Clone)]
69 /// Name of this ABI as we like it called.
73 #[allow(non_upper_case_globals)]
74 const AbiDatas: &[AbiData] = &[
75 AbiData { abi: Abi::Rust, name: "Rust" },
76 AbiData { abi: Abi::C { unwind: false }, name: "C" },
77 AbiData { abi: Abi::C { unwind: true }, name: "C-unwind" },
78 AbiData { abi: Abi::Cdecl { unwind: false }, name: "cdecl" },
79 AbiData { abi: Abi::Cdecl { unwind: true }, name: "cdecl-unwind" },
80 AbiData { abi: Abi::Stdcall { unwind: false }, name: "stdcall" },
81 AbiData { abi: Abi::Stdcall { unwind: true }, name: "stdcall-unwind" },
82 AbiData { abi: Abi::Fastcall { unwind: false }, name: "fastcall" },
83 AbiData { abi: Abi::Fastcall { unwind: true }, name: "fastcall-unwind" },
84 AbiData { abi: Abi::Vectorcall { unwind: false }, name: "vectorcall" },
85 AbiData { abi: Abi::Vectorcall { unwind: true }, name: "vectorcall-unwind" },
86 AbiData { abi: Abi::Thiscall { unwind: false }, name: "thiscall" },
87 AbiData { abi: Abi::Thiscall { unwind: true }, name: "thiscall-unwind" },
88 AbiData { abi: Abi::Aapcs { unwind: false }, name: "aapcs" },
89 AbiData { abi: Abi::Aapcs { unwind: true }, name: "aapcs-unwind" },
90 AbiData { abi: Abi::Win64 { unwind: false }, name: "win64" },
91 AbiData { abi: Abi::Win64 { unwind: true }, name: "win64-unwind" },
92 AbiData { abi: Abi::SysV64 { unwind: false }, name: "sysv64" },
93 AbiData { abi: Abi::SysV64 { unwind: true }, name: "sysv64-unwind" },
94 AbiData { abi: Abi::PtxKernel, name: "ptx-kernel" },
95 AbiData { abi: Abi::Msp430Interrupt, name: "msp430-interrupt" },
96 AbiData { abi: Abi::X86Interrupt, name: "x86-interrupt" },
97 AbiData { abi: Abi::AmdGpuKernel, name: "amdgpu-kernel" },
98 AbiData { abi: Abi::EfiApi, name: "efiapi" },
99 AbiData { abi: Abi::AvrInterrupt, name: "avr-interrupt" },
100 AbiData { abi: Abi::AvrNonBlockingInterrupt, name: "avr-non-blocking-interrupt" },
101 AbiData { abi: Abi::CCmseNonSecureCall, name: "C-cmse-nonsecure-call" },
102 AbiData { abi: Abi::Wasm, name: "wasm" },
103 AbiData { abi: Abi::System { unwind: false }, name: "system" },
104 AbiData { abi: Abi::System { unwind: true }, name: "system-unwind" },
105 AbiData { abi: Abi::RustIntrinsic, name: "rust-intrinsic" },
106 AbiData { abi: Abi::RustCall, name: "rust-call" },
107 AbiData { abi: Abi::PlatformIntrinsic, name: "platform-intrinsic" },
108 AbiData { abi: Abi::Unadjusted, name: "unadjusted" },
109 AbiData { abi: Abi::RustCold, name: "rust-cold" },
112 /// Returns the ABI with the given name (if any).
113 pub fn lookup(name: &str) -> Option<Abi> {
114 AbiDatas.iter().find(|abi_data| name == abi_data.name).map(|&x| x.abi)
117 pub fn all_names() -> Vec<&'static str> {
118 AbiDatas.iter().map(|d| d.name).collect()
121 pub fn enabled_names(features: &rustc_feature::Features, span: Span) -> Vec<&'static str> {
125 .filter(|name| is_enabled(features, span, name).is_ok())
129 pub enum AbiDisabled {
130 Unstable { feature: Symbol, explain: &'static str },
135 features: &rustc_feature::Features,
138 ) -> Result<(), AbiDisabled> {
139 let s = is_stable(name);
140 if let Err(AbiDisabled::Unstable { feature, .. }) = s {
141 if features.enabled(feature) || span.allows_unstable(feature) {
148 pub fn is_stable(name: &str) -> Result<(), AbiDisabled> {
151 "Rust" | "C" | "cdecl" | "stdcall" | "fastcall" | "aapcs" | "win64" | "sysv64"
152 | "system" => Ok(()),
153 "rust-intrinsic" => Err(AbiDisabled::Unstable {
154 feature: sym::intrinsics,
155 explain: "intrinsics are subject to change",
157 "platform-intrinsic" => Err(AbiDisabled::Unstable {
158 feature: sym::platform_intrinsics,
159 explain: "platform intrinsics are experimental and possibly buggy",
161 "vectorcall" => Err(AbiDisabled::Unstable {
162 feature: sym::abi_vectorcall,
163 explain: "vectorcall is experimental and subject to change",
165 "thiscall" => Err(AbiDisabled::Unstable {
166 feature: sym::abi_thiscall,
167 explain: "thiscall is experimental and subject to change",
169 "rust-call" => Err(AbiDisabled::Unstable {
170 feature: sym::unboxed_closures,
171 explain: "rust-call ABI is subject to change",
173 "rust-cold" => Err(AbiDisabled::Unstable {
174 feature: sym::rust_cold_cc,
175 explain: "rust-cold is experimental and subject to change",
177 "ptx-kernel" => Err(AbiDisabled::Unstable {
178 feature: sym::abi_ptx,
179 explain: "PTX ABIs are experimental and subject to change",
181 "unadjusted" => Err(AbiDisabled::Unstable {
182 feature: sym::abi_unadjusted,
183 explain: "unadjusted ABI is an implementation detail and perma-unstable",
185 "msp430-interrupt" => Err(AbiDisabled::Unstable {
186 feature: sym::abi_msp430_interrupt,
187 explain: "msp430-interrupt ABI is experimental and subject to change",
189 "x86-interrupt" => Err(AbiDisabled::Unstable {
190 feature: sym::abi_x86_interrupt,
191 explain: "x86-interrupt ABI is experimental and subject to change",
193 "amdgpu-kernel" => Err(AbiDisabled::Unstable {
194 feature: sym::abi_amdgpu_kernel,
195 explain: "amdgpu-kernel ABI is experimental and subject to change",
197 "avr-interrupt" | "avr-non-blocking-interrupt" => Err(AbiDisabled::Unstable {
198 feature: sym::abi_avr_interrupt,
199 explain: "avr-interrupt and avr-non-blocking-interrupt ABIs are experimental and subject to change",
201 "efiapi" => Err(AbiDisabled::Unstable {
202 feature: sym::abi_efiapi,
203 explain: "efiapi ABI is experimental and subject to change",
205 "C-cmse-nonsecure-call" => Err(AbiDisabled::Unstable {
206 feature: sym::abi_c_cmse_nonsecure_call,
207 explain: "C-cmse-nonsecure-call ABI is experimental and subject to change",
209 "C-unwind" => Err(AbiDisabled::Unstable {
210 feature: sym::c_unwind,
211 explain: "C-unwind ABI is experimental and subject to change",
213 "stdcall-unwind" => Err(AbiDisabled::Unstable {
214 feature: sym::c_unwind,
215 explain: "stdcall-unwind ABI is experimental and subject to change",
217 "system-unwind" => Err(AbiDisabled::Unstable {
218 feature: sym::c_unwind,
219 explain: "system-unwind ABI is experimental and subject to change",
221 "thiscall-unwind" => Err(AbiDisabled::Unstable {
222 feature: sym::c_unwind,
223 explain: "thiscall-unwind ABI is experimental and subject to change",
225 "cdecl-unwind" => Err(AbiDisabled::Unstable {
226 feature: sym::c_unwind,
227 explain: "cdecl-unwind ABI is experimental and subject to change",
229 "fastcall-unwind" => Err(AbiDisabled::Unstable {
230 feature: sym::c_unwind,
231 explain: "fastcall-unwind ABI is experimental and subject to change",
233 "vectorcall-unwind" => Err(AbiDisabled::Unstable {
234 feature: sym::c_unwind,
235 explain: "vectorcall-unwind ABI is experimental and subject to change",
237 "aapcs-unwind" => Err(AbiDisabled::Unstable {
238 feature: sym::c_unwind,
239 explain: "aapcs-unwind ABI is experimental and subject to change",
241 "win64-unwind" => Err(AbiDisabled::Unstable {
242 feature: sym::c_unwind,
243 explain: "win64-unwind ABI is experimental and subject to change",
245 "sysv64-unwind" => Err(AbiDisabled::Unstable {
246 feature: sym::c_unwind,
247 explain: "sysv64-unwind ABI is experimental and subject to change",
249 "wasm" => Err(AbiDisabled::Unstable {
250 feature: sym::wasm_abi,
251 explain: "wasm ABI is experimental and subject to change",
253 _ => Err(AbiDisabled::Unrecognized),
258 /// Default ABI chosen for `extern fn` declarations without an explicit ABI.
259 pub const FALLBACK: Abi = Abi::C { unwind: false };
262 pub fn index(self) -> usize {
263 // N.B., this ordering MUST match the AbiDatas array above.
264 // (This is ensured by the test indices_are_correct().)
267 // Cross-platform ABIs
269 C { unwind: false } => 1,
270 C { unwind: true } => 2,
271 // Platform-specific ABIs
272 Cdecl { unwind: false } => 3,
273 Cdecl { unwind: true } => 4,
274 Stdcall { unwind: false } => 5,
275 Stdcall { unwind: true } => 6,
276 Fastcall { unwind: false } => 7,
277 Fastcall { unwind: true } => 8,
278 Vectorcall { unwind: false } => 9,
279 Vectorcall { unwind: true } => 10,
280 Thiscall { unwind: false } => 11,
281 Thiscall { unwind: true } => 12,
282 Aapcs { unwind: false } => 13,
283 Aapcs { unwind: true } => 14,
284 Win64 { unwind: false } => 15,
285 Win64 { unwind: true } => 16,
286 SysV64 { unwind: false } => 17,
287 SysV64 { unwind: true } => 18,
289 Msp430Interrupt => 20,
294 AvrNonBlockingInterrupt => 25,
295 CCmseNonSecureCall => 26,
297 // Cross-platform ABIs
298 System { unwind: false } => 28,
299 System { unwind: true } => 29,
302 PlatformIntrinsic => 32,
310 .find(|(_, AbiData { abi, .. })| *abi == self)
311 .map(|(index, _)| index)
312 .expect("abi variant has associated data")
314 "Abi index did not match `AbiDatas` ordering"
320 pub fn data(self) -> &'static AbiData {
321 &AbiDatas[self.index()]
324 pub fn name(self) -> &'static str {
329 impl fmt::Display for Abi {
330 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
332 abi => write!(f, "\"{}\"", abi.name()),