]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/abi.rs
Add a few more derivings to AST types
[rust.git] / src / libsyntax / abi.rs
1 // Copyright 2012-2014 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 #[deriving(PartialEq)]
14 pub enum Os { OsWin32, OsMacos, OsLinux, OsAndroid, OsFreebsd, OsiOS, }
15
16 #[deriving(PartialEq, Eq, Hash, Encodable, Decodable, Clone)]
17 pub enum Abi {
18     // NB: This ordering MUST match the AbiDatas array below.
19     // (This is ensured by the test indices_are_correct().)
20
21     // Single platform ABIs come first (`for_arch()` relies on this)
22     Cdecl,
23     Stdcall,
24     Fastcall,
25     Aapcs,
26     Win64,
27
28     // Multiplatform ABIs second
29     Rust,
30     C,
31     System,
32     RustIntrinsic,
33     RustCall,
34 }
35
36 #[allow(non_camel_case_types)]
37 #[deriving(PartialEq)]
38 pub enum Architecture {
39     // NB. You cannot change the ordering of these
40     // constants without adjusting IntelBits below.
41     // (This is ensured by the test indices_are_correct().)
42     X86,
43     X86_64,
44     Arm,
45     Mips,
46     Mipsel
47 }
48
49 static IntelBits: u32 = (1 << (X86 as uint)) | (1 << (X86_64 as uint));
50 static ArmBits: u32 = (1 << (Arm as uint));
51
52 pub struct AbiData {
53     abi: Abi,
54
55     // Name of this ABI as we like it called.
56     name: &'static str,
57
58     // Is it specific to a platform? If so, which one?  Also, what is
59     // the name that LLVM gives it (in case we disagree)
60     abi_arch: AbiArchitecture
61 }
62
63 pub enum AbiArchitecture {
64     /// Not a real ABI (e.g., intrinsic)
65     RustArch,
66     /// An ABI that specifies cross-platform defaults (e.g., "C")
67     AllArch,
68     /// Multiple architectures (bitset)
69     Archs(u32)
70 }
71
72 static AbiDatas: &'static [AbiData] = &[
73     // Platform-specific ABIs
74     AbiData {abi: Cdecl, name: "cdecl", abi_arch: Archs(IntelBits)},
75     AbiData {abi: Stdcall, name: "stdcall", abi_arch: Archs(IntelBits)},
76     AbiData {abi: Fastcall, name:"fastcall", abi_arch: Archs(IntelBits)},
77     AbiData {abi: Aapcs, name: "aapcs", abi_arch: Archs(ArmBits)},
78     AbiData {abi: Win64, name: "win64",
79              abi_arch: Archs(1 << (X86_64 as uint))},
80
81     // Cross-platform ABIs
82     //
83     // NB: Do not adjust this ordering without
84     // adjusting the indices below.
85     AbiData {abi: Rust, name: "Rust", abi_arch: RustArch},
86     AbiData {abi: C, name: "C", abi_arch: AllArch},
87     AbiData {abi: System, name: "system", abi_arch: AllArch},
88     AbiData {abi: RustIntrinsic, name: "rust-intrinsic", abi_arch: RustArch},
89     AbiData {abi: RustCall, name: "rust-call", abi_arch: RustArch},
90 ];
91
92 /// Returns the ABI with the given name (if any).
93 pub fn lookup(name: &str) -> Option<Abi> {
94     AbiDatas.iter().find(|abi_data| name == abi_data.name).map(|&x| x.abi)
95 }
96
97 pub fn all_names() -> Vec<&'static str> {
98     AbiDatas.iter().map(|d| d.name).collect()
99 }
100
101 impl Abi {
102     #[inline]
103     pub fn index(&self) -> uint {
104         *self as uint
105     }
106
107     #[inline]
108     pub fn data(&self) -> &'static AbiData {
109         &AbiDatas[self.index()]
110     }
111
112     pub fn name(&self) -> &'static str {
113         self.data().name
114     }
115
116     pub fn for_target(&self, os: Os, arch: Architecture) -> Option<Abi> {
117         // If this ABI isn't actually for the specified architecture, then we
118         // short circuit early
119         match self.data().abi_arch {
120             Archs(a) if a & arch.bit() == 0 => return None,
121             Archs(_) | RustArch | AllArch => {}
122         }
123         // Transform this ABI as appropriate for the requested os/arch
124         // combination.
125         Some(match (*self, os, arch) {
126             (System, OsWin32, X86) => Stdcall,
127             (System, _, _) => C,
128             (me, _, _) => me,
129         })
130     }
131 }
132
133 impl Architecture {
134     fn bit(&self) -> u32 {
135         1 << (*self as uint)
136     }
137 }
138
139 impl fmt::Show for Abi {
140     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
141         write!(f, "\"{}\"", self.name())
142     }
143 }
144
145 impl fmt::Show for Os {
146     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
147         match *self {
148             OsLinux => "linux".fmt(f),
149             OsWin32 => "win32".fmt(f),
150             OsMacos => "macos".fmt(f),
151             OsiOS => "ios".fmt(f),
152             OsAndroid => "android".fmt(f),
153             OsFreebsd => "freebsd".fmt(f)
154         }
155     }
156 }
157
158 #[allow(non_snake_case_functions)]
159 #[test]
160 fn lookup_Rust() {
161     let abi = lookup("Rust");
162     assert!(abi.is_some() && abi.unwrap().data().name == "Rust");
163 }
164
165 #[test]
166 fn lookup_cdecl() {
167     let abi = lookup("cdecl");
168     assert!(abi.is_some() && abi.unwrap().data().name == "cdecl");
169 }
170
171 #[test]
172 fn lookup_baz() {
173     let abi = lookup("baz");
174     assert!(abi.is_none());
175 }
176
177 #[test]
178 fn indices_are_correct() {
179     for (i, abi_data) in AbiDatas.iter().enumerate() {
180         assert_eq!(i, abi_data.abi.index());
181     }
182
183     let bits = 1 << (X86 as uint);
184     let bits = bits | 1 << (X86_64 as uint);
185     assert_eq!(IntelBits, bits);
186
187     let bits = 1 << (Arm as uint);
188     assert_eq!(ArmBits, bits);
189 }
190
191 #[test]
192 fn pick_uniplatform() {
193     assert_eq!(Stdcall.for_target(OsLinux, X86), Some(Stdcall));
194     assert_eq!(Stdcall.for_target(OsLinux, Arm), None);
195     assert_eq!(System.for_target(OsLinux, X86), Some(C));
196     assert_eq!(System.for_target(OsWin32, X86), Some(Stdcall));
197     assert_eq!(System.for_target(OsWin32, X86_64), Some(C));
198     assert_eq!(System.for_target(OsWin32, Arm), Some(C));
199     assert_eq!(Stdcall.for_target(OsWin32, X86), Some(Stdcall));
200     assert_eq!(Stdcall.for_target(OsWin32, X86_64), Some(Stdcall));
201 }