]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_target/src/spec/abi.rs
Add support for clobber_abi to asm!
[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,
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).
81 pub fn lookup(name: &str) -> Option<Abi> {
82     AbiDatas.iter().find(|abi_data| name == abi_data.name).map(|&x| x.abi)
83 }
84
85 pub fn all_names() -> Vec<&'static str> {
86     AbiDatas.iter().map(|d| d.name).collect()
87 }
88
89 impl Abi {
90     #[inline]
91     pub fn index(self) -> usize {
92         // N.B., this ordering MUST match the AbiDatas array above.
93         // (This is ensured by the test indices_are_correct().)
94         use Abi::*;
95         let i = match self {
96             // Cross-platform ABIs
97             Rust => 0,
98             C { unwind: false } => 1,
99             C { unwind: true } => 2,
100             // Platform-specific ABIs
101             Cdecl => 3,
102             Stdcall { unwind: false } => 4,
103             Stdcall { unwind: true } => 5,
104             Fastcall => 6,
105             Vectorcall => 7,
106             Thiscall { unwind: false } => 8,
107             Thiscall { unwind: true } => 9,
108             Aapcs => 10,
109             Win64 => 11,
110             SysV64 => 12,
111             PtxKernel => 13,
112             Msp430Interrupt => 14,
113             X86Interrupt => 15,
114             AmdGpuKernel => 16,
115             EfiApi => 17,
116             AvrInterrupt => 18,
117             AvrNonBlockingInterrupt => 19,
118             CCmseNonSecureCall => 20,
119             Wasm => 21,
120             // Cross-platform ABIs
121             System { unwind: false } => 22,
122             System { unwind: true } => 23,
123             RustIntrinsic => 24,
124             RustCall => 25,
125             PlatformIntrinsic => 26,
126             Unadjusted => 27,
127         };
128         debug_assert!(
129             AbiDatas
130                 .iter()
131                 .enumerate()
132                 .find(|(_, AbiData { abi, .. })| *abi == self)
133                 .map(|(index, _)| index)
134                 .expect("abi variant has associated data")
135                 == i,
136             "Abi index did not match `AbiDatas` ordering"
137         );
138         i
139     }
140
141     #[inline]
142     pub fn data(self) -> &'static AbiData {
143         &AbiDatas[self.index()]
144     }
145
146     pub fn name(self) -> &'static str {
147         self.data().name
148     }
149 }
150
151 impl fmt::Display for Abi {
152     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
153         match self {
154             abi => write!(f, "\"{}\"", abi.name()),
155         }
156     }
157 }