]> git.proxmox.com Git - rustc.git/blob - src/librustc_target/spec/abi.rs
New upstream version 1.30.0~beta.7+dfsg1
[rustc.git] / src / librustc_target / spec / abi.rs
1 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use std::fmt;
12
13 #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable, Clone, Copy, Debug)]
14 pub enum Abi {
15 // NB: This ordering MUST match the AbiDatas array below.
16 // (This is ensured by the test indices_are_correct().)
17
18 // Single platform ABIs
19 Cdecl,
20 Stdcall,
21 Fastcall,
22 Vectorcall,
23 Thiscall,
24 Aapcs,
25 Win64,
26 SysV64,
27 PtxKernel,
28 Msp430Interrupt,
29 X86Interrupt,
30 AmdGpuKernel,
31
32 // Multiplatform / generic ABIs
33 Rust,
34 C,
35 System,
36 RustIntrinsic,
37 RustCall,
38 PlatformIntrinsic,
39 Unadjusted
40 }
41
42 #[derive(Copy, Clone)]
43 pub struct AbiData {
44 abi: Abi,
45
46 /// Name of this ABI as we like it called.
47 name: &'static str,
48
49 /// A generic ABI is supported on all platforms.
50 generic: bool,
51 }
52
53 #[allow(non_upper_case_globals)]
54 const AbiDatas: &[AbiData] = &[
55 // Platform-specific ABIs
56 AbiData {abi: Abi::Cdecl, name: "cdecl", generic: false },
57 AbiData {abi: Abi::Stdcall, name: "stdcall", generic: false },
58 AbiData {abi: Abi::Fastcall, name: "fastcall", generic: false },
59 AbiData {abi: Abi::Vectorcall, name: "vectorcall", generic: false},
60 AbiData {abi: Abi::Thiscall, name: "thiscall", generic: false},
61 AbiData {abi: Abi::Aapcs, name: "aapcs", generic: false },
62 AbiData {abi: Abi::Win64, name: "win64", generic: false },
63 AbiData {abi: Abi::SysV64, name: "sysv64", generic: false },
64 AbiData {abi: Abi::PtxKernel, name: "ptx-kernel", generic: false },
65 AbiData {abi: Abi::Msp430Interrupt, name: "msp430-interrupt", generic: false },
66 AbiData {abi: Abi::X86Interrupt, name: "x86-interrupt", generic: false },
67 AbiData {abi: Abi::AmdGpuKernel, name: "amdgpu-kernel", generic: false },
68
69 // Cross-platform ABIs
70 AbiData {abi: Abi::Rust, name: "Rust", generic: true },
71 AbiData {abi: Abi::C, name: "C", generic: true },
72 AbiData {abi: Abi::System, name: "system", generic: true },
73 AbiData {abi: Abi::RustIntrinsic, name: "rust-intrinsic", generic: true },
74 AbiData {abi: Abi::RustCall, name: "rust-call", generic: true },
75 AbiData {abi: Abi::PlatformIntrinsic, name: "platform-intrinsic", generic: true },
76 AbiData {abi: Abi::Unadjusted, name: "unadjusted", generic: true },
77 ];
78
79 /// Returns the ABI with the given name (if any).
80 pub fn lookup(name: &str) -> Option<Abi> {
81 AbiDatas.iter().find(|abi_data| name == abi_data.name).map(|&x| x.abi)
82 }
83
84 pub fn all_names() -> Vec<&'static str> {
85 AbiDatas.iter().map(|d| d.name).collect()
86 }
87
88 impl Abi {
89 #[inline]
90 pub fn index(self) -> usize {
91 self as usize
92 }
93
94 #[inline]
95 pub fn data(self) -> &'static AbiData {
96 &AbiDatas[self.index()]
97 }
98
99 pub fn name(self) -> &'static str {
100 self.data().name
101 }
102
103 pub fn generic(self) -> bool {
104 self.data().generic
105 }
106 }
107
108 impl fmt::Display for Abi {
109 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
110 write!(f, "\"{}\"", self.name())
111 }
112 }
113
114 #[allow(non_snake_case)]
115 #[test]
116 fn lookup_Rust() {
117 let abi = lookup("Rust");
118 assert!(abi.is_some() && abi.unwrap().data().name == "Rust");
119 }
120
121 #[test]
122 fn lookup_cdecl() {
123 let abi = lookup("cdecl");
124 assert!(abi.is_some() && abi.unwrap().data().name == "cdecl");
125 }
126
127 #[test]
128 fn lookup_baz() {
129 let abi = lookup("baz");
130 assert!(abi.is_none());
131 }
132
133 #[test]
134 fn indices_are_correct() {
135 for (i, abi_data) in AbiDatas.iter().enumerate() {
136 assert_eq!(i, abi_data.abi.index());
137 }
138 }