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.
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.
13 #[deriving(PartialEq)]
14 pub enum Os { OsWin32, OsMacos, OsLinux, OsAndroid, OsFreebsd, OsiOS, }
16 #[deriving(PartialEq, Eq, Hash, Encodable, Decodable, Clone)]
18 // NB: This ordering MUST match the AbiDatas array below.
19 // (This is ensured by the test indices_are_correct().)
21 // Single platform ABIs come first (`for_arch()` relies on this)
28 // Multiplatform ABIs second
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().)
49 static IntelBits: u32 = (1 << (X86 as uint)) | (1 << (X86_64 as uint));
50 static ArmBits: u32 = (1 << (Arm as uint));
55 // Name of this ABI as we like it called.
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
63 pub enum AbiArchitecture {
64 /// Not a real ABI (e.g., intrinsic)
66 /// An ABI that specifies cross-platform defaults (e.g., "C")
68 /// Multiple architectures (bitset)
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))},
81 // Cross-platform ABIs
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},
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)
97 pub fn all_names() -> Vec<&'static str> {
98 AbiDatas.iter().map(|d| d.name).collect()
103 pub fn index(&self) -> uint {
108 pub fn data(&self) -> &'static AbiData {
109 &AbiDatas[self.index()]
112 pub fn name(&self) -> &'static str {
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 => {}
123 // Transform this ABI as appropriate for the requested os/arch
125 Some(match (*self, os, arch) {
126 (System, OsWin32, X86) => Stdcall,
134 fn bit(&self) -> u32 {
139 impl fmt::Show for Abi {
140 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
141 write!(f, "\"{}\"", self.name())
145 impl fmt::Show for Os {
146 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
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)
158 #[allow(non_snake_case_functions)]
161 let abi = lookup("Rust");
162 assert!(abi.is_some() && abi.unwrap().data().name == "Rust");
167 let abi = lookup("cdecl");
168 assert!(abi.is_some() && abi.unwrap().data().name == "cdecl");
173 let abi = lookup("baz");
174 assert!(abi.is_none());
178 fn indices_are_correct() {
179 for (i, abi_data) in AbiDatas.iter().enumerate() {
180 assert_eq!(i, abi_data.abi.index());
183 let bits = 1 << (X86 as uint);
184 let bits = bits | 1 << (X86_64 as uint);
185 assert_eq!(IntelBits, bits);
187 let bits = 1 << (Arm as uint);
188 assert_eq!(ArmBits, bits);
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));