1 use std::fmt;
2
3 use rustc_macros::HashStable_Generic;
4
5 #[cfg(test)]
6 mod tests;
7
8 #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Debug)]
9 #[derive(HashStable_Generic, Encodable, Decodable)]
10 pub enum Abi {
11 // Some of the ABIs come first because every time we add a new ABI, we have to re-bless all the
12 // hashing tests. These are used in many places, so giving them stable values reduces test
13 // churn. The specific values are meaningless.
14 Rust,
15 C { unwind: bool },
16 Cdecl,
17 Stdcall { unwind: bool },
18 Fastcall,
19 Vectorcall,
20 Thiscall { unwind: bool },
21 Aapcs,
22 Win64,
23 SysV64,
24 PtxKernel,
25 Msp430Interrupt,
26 X86Interrupt,
27 AmdGpuKernel,
28 EfiApi,
29 AvrInterrupt,
30 AvrNonBlockingInterrupt,
31 CCmseNonSecureCall,
32 Wasm,
33 System { unwind: bool },
34 RustIntrinsic,
35 RustCall,
36 PlatformIntrinsic,
37 Unadjusted,
38 }
39
40 #[derive(Copy, Clone)]
41 pub struct AbiData {
42 abi: Abi,
43
44 /// Name of this ABI as we like it called.
45 name: &'static str,
46 }
47
48 #[allow(non_upper_case_globals)]
49 const AbiDatas: &[AbiData] = &[
50 AbiData { abi: Abi::Rust, name: "Rust" },
51 AbiData { abi: Abi::C { unwind: false }, name: "C" },
52 AbiData { abi: Abi::C { unwind: true }, name: "C-unwind" },
53 AbiData { abi: Abi::Cdecl, name: "cdecl" },
54 AbiData { abi: Abi::Stdcall { unwind: false }, name: "stdcall" },
55 AbiData { abi: Abi::Stdcall { unwind: true }, name: "stdcall-unwind" },
56 AbiData { abi: Abi::Fastcall, name: "fastcall" },
57 AbiData { abi: Abi::Vectorcall, name: "vectorcall" },
58 AbiData { abi: Abi::Thiscall { unwind: false }, name: "thiscall" },
59 AbiData { abi: Abi::Thiscall { unwind: true }, name: "thiscall-unwind" },
60 AbiData { abi: Abi::Aapcs, name: "aapcs" },
61 AbiData { abi: Abi::Win64, name: "win64" },
62 AbiData { abi: Abi::SysV64, name: "sysv64" },
63 AbiData { abi: Abi::PtxKernel, name: "ptx-kernel" },
64 AbiData { abi: Abi::Msp430Interrupt, name: "msp430-interrupt" },
65 AbiData { abi: Abi::X86Interrupt, name: "x86-interrupt" },
66 AbiData { abi: Abi::AmdGpuKernel, name: "amdgpu-kernel" },
67 AbiData { abi: Abi::EfiApi, name: "efiapi" },
68 AbiData { abi: Abi::AvrInterrupt, name: "avr-interrupt" },
69 AbiData { abi: Abi::AvrNonBlockingInterrupt, name: "avr-non-blocking-interrupt" },
70 AbiData { abi: Abi::CCmseNonSecureCall, name: "C-cmse-nonsecure-call" },
71 AbiData { abi: Abi::Wasm, name: "wasm" },
72 AbiData { abi: Abi::System { unwind: false }, name: "system" },
73 AbiData { abi: Abi::System { unwind: true }, name: "system-unwind" },
74 AbiData { abi: Abi::RustIntrinsic, name: "rust-intrinsic" },
75 AbiData { abi: Abi::RustCall, name: "rust-call" },
76 AbiData { abi: Abi::PlatformIntrinsic, name: "platform-intrinsic" },
77 AbiData { abi: Abi::Unadjusted, name: "unadjusted" },
78 ];
79
80 /// Returns the ABI with the given name (if any).
lookup(name: &str) -> Option<Abi>81 pub fn lookup(name: &str) -> Option<Abi> {
82 AbiDatas.iter().find(|abi_data| name == abi_data.name).map(|&x| x.abi)
83 }
84
all_names() -> Vec<&'static str>85 pub fn all_names() -> Vec<&'static str> {
86 AbiDatas.iter().map(|d| d.name).collect()
87 }
88
89 impl Abi {
90 /// Default ABI chosen for `extern fn` declarations without an explicit ABI.
91 pub const FALLBACK: Abi = Abi::C { unwind: false };
92
93 #[inline]
index(self) -> usize94 pub fn index(self) -> usize {
95 // N.B., this ordering MUST match the AbiDatas array above.
96 // (This is ensured by the test indices_are_correct().)
97 use Abi::*;
98 let i = match self {
99 // Cross-platform ABIs
100 Rust => 0,
101 C { unwind: false } => 1,
102 C { unwind: true } => 2,
103 // Platform-specific ABIs
104 Cdecl => 3,
105 Stdcall { unwind: false } => 4,
106 Stdcall { unwind: true } => 5,
107 Fastcall => 6,
108 Vectorcall => 7,
109 Thiscall { unwind: false } => 8,
110 Thiscall { unwind: true } => 9,
111 Aapcs => 10,
112 Win64 => 11,
113 SysV64 => 12,
114 PtxKernel => 13,
115 Msp430Interrupt => 14,
116 X86Interrupt => 15,
117 AmdGpuKernel => 16,
118 EfiApi => 17,
119 AvrInterrupt => 18,
120 AvrNonBlockingInterrupt => 19,
121 CCmseNonSecureCall => 20,
122 Wasm => 21,
123 // Cross-platform ABIs
124 System { unwind: false } => 22,
125 System { unwind: true } => 23,
126 RustIntrinsic => 24,
127 RustCall => 25,
128 PlatformIntrinsic => 26,
129 Unadjusted => 27,
130 };
131 debug_assert!(
132 AbiDatas
133 .iter()
134 .enumerate()
135 .find(|(_, AbiData { abi, .. })| *abi == self)
136 .map(|(index, _)| index)
137 .expect("abi variant has associated data")
138 == i,
139 "Abi index did not match `AbiDatas` ordering"
140 );
141 i
142 }
143
144 #[inline]
data(self) -> &'static AbiData145 pub fn data(self) -> &'static AbiData {
146 &AbiDatas[self.index()]
147 }
148
name(self) -> &'static str149 pub fn name(self) -> &'static str {
150 self.data().name
151 }
152 }
153
154 impl fmt::Display for Abi {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result155 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
156 match self {
157 abi => write!(f, "\"{}\"", abi.name()),
158 }
159 }
160 }
161