• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use std::fmt;
2 
3 use rustc_macros::HashStable_Generic;
4 use rustc_span::symbol::sym;
5 use rustc_span::{Span, Symbol};
6 
7 #[cfg(test)]
8 mod tests;
9 
10 #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Debug)]
11 #[derive(HashStable_Generic, Encodable, Decodable)]
12 pub enum Abi {
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.
16     Rust,
17     C { unwind: bool },
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 },
26     PtxKernel,
27     Msp430Interrupt,
28     X86Interrupt,
29     AmdGpuKernel,
30     EfiApi,
31     AvrInterrupt,
32     AvrNonBlockingInterrupt,
33     CCmseNonSecureCall,
34     Wasm,
35     System { unwind: bool },
36     RustIntrinsic,
37     RustCall,
38     PlatformIntrinsic,
39     Unadjusted,
40     RustCold,
41 }
42 
43 impl Abi {
supports_varargs(self) -> bool44     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.
48         //
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.
54         match self {
55             Self::C { .. }
56             | Self::Cdecl { .. }
57             | Self::Win64 { .. }
58             | Self::SysV64 { .. }
59             | Self::EfiApi => true,
60             _ => false,
61         }
62     }
63 }
64 
65 #[derive(Copy, Clone)]
66 pub struct AbiData {
67     abi: Abi,
68 
69     /// Name of this ABI as we like it called.
70     name: &'static str,
71 }
72 
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" },
110 ];
111 
112 /// Returns the ABI with the given name (if any).
lookup(name: &str) -> Option<Abi>113 pub fn lookup(name: &str) -> Option<Abi> {
114     AbiDatas.iter().find(|abi_data| name == abi_data.name).map(|&x| x.abi)
115 }
116 
all_names() -> Vec<&'static str>117 pub fn all_names() -> Vec<&'static str> {
118     AbiDatas.iter().map(|d| d.name).collect()
119 }
120 
enabled_names(features: &rustc_feature::Features, span: Span) -> Vec<&'static str>121 pub fn enabled_names(features: &rustc_feature::Features, span: Span) -> Vec<&'static str> {
122     AbiDatas
123         .iter()
124         .map(|d| d.name)
125         .filter(|name| is_enabled(features, span, name).is_ok())
126         .collect()
127 }
128 
129 pub enum AbiDisabled {
130     Unstable { feature: Symbol, explain: &'static str },
131     Unrecognized,
132 }
133 
is_enabled( features: &rustc_feature::Features, span: Span, name: &str, ) -> Result<(), AbiDisabled>134 pub fn is_enabled(
135     features: &rustc_feature::Features,
136     span: Span,
137     name: &str,
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) {
142             return Ok(());
143         }
144     }
145     s
146 }
147 
is_stable(name: &str) -> Result<(), AbiDisabled>148 pub fn is_stable(name: &str) -> Result<(), AbiDisabled> {
149     match name {
150         // Stable
151         "Rust" | "C" | "C-unwind" | "cdecl" | "cdecl-unwind" | "stdcall" | "stdcall-unwind"
152         | "fastcall" | "fastcall-unwind" | "aapcs" | "aapcs-unwind" | "win64" | "win64-unwind"
153         | "sysv64" | "sysv64-unwind" | "system" | "system-unwind" | "efiapi" => Ok(()),
154         "rust-intrinsic" => Err(AbiDisabled::Unstable {
155             feature: sym::intrinsics,
156             explain: "intrinsics are subject to change",
157         }),
158         "platform-intrinsic" => Err(AbiDisabled::Unstable {
159             feature: sym::platform_intrinsics,
160             explain: "platform intrinsics are experimental and possibly buggy",
161         }),
162         "vectorcall" => Err(AbiDisabled::Unstable {
163             feature: sym::abi_vectorcall,
164             explain: "vectorcall is experimental and subject to change",
165         }),
166         "vectorcall-unwind" => Err(AbiDisabled::Unstable {
167             feature: sym::abi_vectorcall,
168             explain: "vectorcall-unwind ABI is experimental and subject to change",
169         }),
170         "thiscall" => Err(AbiDisabled::Unstable {
171             feature: sym::abi_thiscall,
172             explain: "thiscall is experimental and subject to change",
173         }),
174         "thiscall-unwind" => Err(AbiDisabled::Unstable {
175             feature: sym::abi_thiscall,
176             explain: "thiscall-unwind ABI is experimental and subject to change",
177         }),
178         "rust-call" => Err(AbiDisabled::Unstable {
179             feature: sym::unboxed_closures,
180             explain: "rust-call ABI is subject to change",
181         }),
182         "rust-cold" => Err(AbiDisabled::Unstable {
183             feature: sym::rust_cold_cc,
184             explain: "rust-cold is experimental and subject to change",
185         }),
186         "ptx-kernel" => Err(AbiDisabled::Unstable {
187             feature: sym::abi_ptx,
188             explain: "PTX ABIs are experimental and subject to change",
189         }),
190         "unadjusted" => Err(AbiDisabled::Unstable {
191             feature: sym::abi_unadjusted,
192             explain: "unadjusted ABI is an implementation detail and perma-unstable",
193         }),
194         "msp430-interrupt" => Err(AbiDisabled::Unstable {
195             feature: sym::abi_msp430_interrupt,
196             explain: "msp430-interrupt ABI is experimental and subject to change",
197         }),
198         "x86-interrupt" => Err(AbiDisabled::Unstable {
199             feature: sym::abi_x86_interrupt,
200             explain: "x86-interrupt ABI is experimental and subject to change",
201         }),
202         "amdgpu-kernel" => Err(AbiDisabled::Unstable {
203             feature: sym::abi_amdgpu_kernel,
204             explain: "amdgpu-kernel ABI is experimental and subject to change",
205         }),
206         "avr-interrupt" | "avr-non-blocking-interrupt" => Err(AbiDisabled::Unstable {
207             feature: sym::abi_avr_interrupt,
208             explain: "avr-interrupt and avr-non-blocking-interrupt ABIs are experimental and subject to change",
209         }),
210         "C-cmse-nonsecure-call" => Err(AbiDisabled::Unstable {
211             feature: sym::abi_c_cmse_nonsecure_call,
212             explain: "C-cmse-nonsecure-call ABI is experimental and subject to change",
213         }),
214         "wasm" => Err(AbiDisabled::Unstable {
215             feature: sym::wasm_abi,
216             explain: "wasm ABI is experimental and subject to change",
217         }),
218         _ => Err(AbiDisabled::Unrecognized),
219     }
220 }
221 
222 impl Abi {
223     /// Default ABI chosen for `extern fn` declarations without an explicit ABI.
224     pub const FALLBACK: Abi = Abi::C { unwind: false };
225 
226     #[inline]
index(self) -> usize227     pub fn index(self) -> usize {
228         // N.B., this ordering MUST match the AbiDatas array above.
229         // (This is ensured by the test indices_are_correct().)
230         use Abi::*;
231         let i = match self {
232             // Cross-platform ABIs
233             Rust => 0,
234             C { unwind: false } => 1,
235             C { unwind: true } => 2,
236             // Platform-specific ABIs
237             Cdecl { unwind: false } => 3,
238             Cdecl { unwind: true } => 4,
239             Stdcall { unwind: false } => 5,
240             Stdcall { unwind: true } => 6,
241             Fastcall { unwind: false } => 7,
242             Fastcall { unwind: true } => 8,
243             Vectorcall { unwind: false } => 9,
244             Vectorcall { unwind: true } => 10,
245             Thiscall { unwind: false } => 11,
246             Thiscall { unwind: true } => 12,
247             Aapcs { unwind: false } => 13,
248             Aapcs { unwind: true } => 14,
249             Win64 { unwind: false } => 15,
250             Win64 { unwind: true } => 16,
251             SysV64 { unwind: false } => 17,
252             SysV64 { unwind: true } => 18,
253             PtxKernel => 19,
254             Msp430Interrupt => 20,
255             X86Interrupt => 21,
256             AmdGpuKernel => 22,
257             EfiApi => 23,
258             AvrInterrupt => 24,
259             AvrNonBlockingInterrupt => 25,
260             CCmseNonSecureCall => 26,
261             Wasm => 27,
262             // Cross-platform ABIs
263             System { unwind: false } => 28,
264             System { unwind: true } => 29,
265             RustIntrinsic => 30,
266             RustCall => 31,
267             PlatformIntrinsic => 32,
268             Unadjusted => 33,
269             RustCold => 34,
270         };
271         debug_assert!(
272             AbiDatas
273                 .iter()
274                 .enumerate()
275                 .find(|(_, AbiData { abi, .. })| *abi == self)
276                 .map(|(index, _)| index)
277                 .expect("abi variant has associated data")
278                 == i,
279             "Abi index did not match `AbiDatas` ordering"
280         );
281         i
282     }
283 
284     #[inline]
data(self) -> &'static AbiData285     pub fn data(self) -> &'static AbiData {
286         &AbiDatas[self.index()]
287     }
288 
name(self) -> &'static str289     pub fn name(self) -> &'static str {
290         self.data().name
291     }
292 }
293 
294 impl fmt::Display for Abi {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result295     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
296         write!(f, "\"{}\"", self.name())
297     }
298 }
299