]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_target/src/spec/abi.rs
Rollup merge of #89685 - DeveloperC286:iter_fields_to_private, r=oli-obk
[rust.git] / compiler / rustc_target / src / spec / abi.rs
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 { unwind: bool },
17     Stdcall { unwind: bool },
18     Fastcall { unwind: bool },
19     Vectorcall { unwind: bool },
20     Thiscall { unwind: bool },
21     Aapcs { unwind: bool },
22     Win64 { unwind: bool },
23     SysV64 { unwind: bool },
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 { unwind: false }, name: "cdecl" },
54     AbiData { abi: Abi::Cdecl { unwind: true }, name: "cdecl-unwind" },
55     AbiData { abi: Abi::Stdcall { unwind: false }, name: "stdcall" },
56     AbiData { abi: Abi::Stdcall { unwind: true }, name: "stdcall-unwind" },
57     AbiData { abi: Abi::Fastcall { unwind: false }, name: "fastcall" },
58     AbiData { abi: Abi::Fastcall { unwind: true }, name: "fastcall-unwind" },
59     AbiData { abi: Abi::Vectorcall { unwind: false }, name: "vectorcall" },
60     AbiData { abi: Abi::Vectorcall { unwind: true }, name: "vectorcall-unwind" },
61     AbiData { abi: Abi::Thiscall { unwind: false }, name: "thiscall" },
62     AbiData { abi: Abi::Thiscall { unwind: true }, name: "thiscall-unwind" },
63     AbiData { abi: Abi::Aapcs { unwind: false }, name: "aapcs" },
64     AbiData { abi: Abi::Aapcs { unwind: true }, name: "aapcs-unwind" },
65     AbiData { abi: Abi::Win64 { unwind: false }, name: "win64" },
66     AbiData { abi: Abi::Win64 { unwind: true }, name: "win64-unwind" },
67     AbiData { abi: Abi::SysV64 { unwind: false }, name: "sysv64" },
68     AbiData { abi: Abi::SysV64 { unwind: true }, name: "sysv64-unwind" },
69     AbiData { abi: Abi::PtxKernel, name: "ptx-kernel" },
70     AbiData { abi: Abi::Msp430Interrupt, name: "msp430-interrupt" },
71     AbiData { abi: Abi::X86Interrupt, name: "x86-interrupt" },
72     AbiData { abi: Abi::AmdGpuKernel, name: "amdgpu-kernel" },
73     AbiData { abi: Abi::EfiApi, name: "efiapi" },
74     AbiData { abi: Abi::AvrInterrupt, name: "avr-interrupt" },
75     AbiData { abi: Abi::AvrNonBlockingInterrupt, name: "avr-non-blocking-interrupt" },
76     AbiData { abi: Abi::CCmseNonSecureCall, name: "C-cmse-nonsecure-call" },
77     AbiData { abi: Abi::Wasm, name: "wasm" },
78     AbiData { abi: Abi::System { unwind: false }, name: "system" },
79     AbiData { abi: Abi::System { unwind: true }, name: "system-unwind" },
80     AbiData { abi: Abi::RustIntrinsic, name: "rust-intrinsic" },
81     AbiData { abi: Abi::RustCall, name: "rust-call" },
82     AbiData { abi: Abi::PlatformIntrinsic, name: "platform-intrinsic" },
83     AbiData { abi: Abi::Unadjusted, name: "unadjusted" },
84 ];
85
86 /// Returns the ABI with the given name (if any).
87 pub fn lookup(name: &str) -> Option<Abi> {
88     AbiDatas.iter().find(|abi_data| name == abi_data.name).map(|&x| x.abi)
89 }
90
91 pub fn all_names() -> Vec<&'static str> {
92     AbiDatas.iter().map(|d| d.name).collect()
93 }
94
95 impl Abi {
96     /// Default ABI chosen for `extern fn` declarations without an explicit ABI.
97     pub const FALLBACK: Abi = Abi::C { unwind: false };
98
99     #[inline]
100     pub fn index(self) -> usize {
101         // N.B., this ordering MUST match the AbiDatas array above.
102         // (This is ensured by the test indices_are_correct().)
103         use Abi::*;
104         let i = match self {
105             // Cross-platform ABIs
106             Rust => 0,
107             C { unwind: false } => 1,
108             C { unwind: true } => 2,
109             // Platform-specific ABIs
110             Cdecl { unwind: false } => 3,
111             Cdecl { unwind: true } => 4,
112             Stdcall { unwind: false } => 5,
113             Stdcall { unwind: true } => 6,
114             Fastcall { unwind: false } => 7,
115             Fastcall { unwind: true } => 8,
116             Vectorcall { unwind: false } => 9,
117             Vectorcall { unwind: true } => 10,
118             Thiscall { unwind: false } => 11,
119             Thiscall { unwind: true } => 12,
120             Aapcs { unwind: false } => 13,
121             Aapcs { unwind: true } => 14,
122             Win64 { unwind: false } => 15,
123             Win64 { unwind: true } => 16,
124             SysV64 { unwind: false } => 17,
125             SysV64 { unwind: true } => 18,
126             PtxKernel => 19,
127             Msp430Interrupt => 20,
128             X86Interrupt => 21,
129             AmdGpuKernel => 22,
130             EfiApi => 23,
131             AvrInterrupt => 24,
132             AvrNonBlockingInterrupt => 25,
133             CCmseNonSecureCall => 26,
134             Wasm => 27,
135             // Cross-platform ABIs
136             System { unwind: false } => 28,
137             System { unwind: true } => 29,
138             RustIntrinsic => 30,
139             RustCall => 31,
140             PlatformIntrinsic => 32,
141             Unadjusted => 33,
142         };
143         debug_assert!(
144             AbiDatas
145                 .iter()
146                 .enumerate()
147                 .find(|(_, AbiData { abi, .. })| *abi == self)
148                 .map(|(index, _)| index)
149                 .expect("abi variant has associated data")
150                 == i,
151             "Abi index did not match `AbiDatas` ordering"
152         );
153         i
154     }
155
156     #[inline]
157     pub fn data(self) -> &'static AbiData {
158         &AbiDatas[self.index()]
159     }
160
161     pub fn name(self) -> &'static str {
162         self.data().name
163     }
164 }
165
166 impl fmt::Display for Abi {
167     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
168         match self {
169             abi => write!(f, "\"{}\"", abi.name()),
170         }
171     }
172 }