2014-05-25 18:27:36 -05:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2013-03-13 21:25:28 -05:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-02-19 20:56:33 -06:00
|
|
|
use std::fmt;
|
2013-03-13 21:25:28 -05:00
|
|
|
|
2014-05-29 19:45:07 -05:00
|
|
|
#[deriving(PartialEq)]
|
2014-08-11 18:32:11 -05:00
|
|
|
pub enum Os { OsWindows, OsMacos, OsLinux, OsAndroid, OsFreebsd, OsiOS,
|
2014-07-29 09:44:39 -05:00
|
|
|
OsDragonfly }
|
2013-11-08 13:06:57 -06:00
|
|
|
|
2014-05-31 12:43:52 -05:00
|
|
|
#[deriving(PartialEq, Eq, Hash, Encodable, Decodable, Clone)]
|
2013-03-13 21:25:28 -05:00
|
|
|
pub enum Abi {
|
|
|
|
// NB: This ordering MUST match the AbiDatas array below.
|
|
|
|
// (This is ensured by the test indices_are_correct().)
|
|
|
|
|
|
|
|
// Single platform ABIs come first (`for_arch()` relies on this)
|
|
|
|
Cdecl,
|
|
|
|
Stdcall,
|
|
|
|
Fastcall,
|
|
|
|
Aapcs,
|
2013-11-16 17:25:56 -06:00
|
|
|
Win64,
|
2013-03-13 21:25:28 -05:00
|
|
|
|
|
|
|
// Multiplatform ABIs second
|
|
|
|
Rust,
|
|
|
|
C,
|
2013-11-08 13:06:57 -06:00
|
|
|
System,
|
2013-03-13 21:25:28 -05:00
|
|
|
RustIntrinsic,
|
2014-05-29 00:26:56 -05:00
|
|
|
RustCall,
|
2013-03-13 21:25:28 -05:00
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
#[allow(non_camel_case_types)]
|
2014-05-29 19:45:07 -05:00
|
|
|
#[deriving(PartialEq)]
|
2013-03-13 21:25:28 -05:00
|
|
|
pub enum Architecture {
|
|
|
|
// NB. You cannot change the ordering of these
|
|
|
|
// constants without adjusting IntelBits below.
|
|
|
|
// (This is ensured by the test indices_are_correct().)
|
|
|
|
X86,
|
|
|
|
X86_64,
|
|
|
|
Arm,
|
2014-06-17 02:16:03 -05:00
|
|
|
Mips,
|
|
|
|
Mipsel
|
2013-03-13 21:25:28 -05:00
|
|
|
}
|
|
|
|
|
2013-03-30 21:28:50 -05:00
|
|
|
static IntelBits: u32 = (1 << (X86 as uint)) | (1 << (X86_64 as uint));
|
|
|
|
static ArmBits: u32 = (1 << (Arm as uint));
|
2013-03-13 21:25:28 -05:00
|
|
|
|
2014-02-27 01:48:21 -06:00
|
|
|
pub struct AbiData {
|
2013-03-13 21:25:28 -05:00
|
|
|
abi: Abi,
|
|
|
|
|
|
|
|
// Name of this ABI as we like it called.
|
|
|
|
name: &'static str,
|
|
|
|
|
|
|
|
// Is it specific to a platform? If so, which one? Also, what is
|
|
|
|
// the name that LLVM gives it (in case we disagree)
|
|
|
|
abi_arch: AbiArchitecture
|
|
|
|
}
|
|
|
|
|
2014-02-27 01:48:21 -06:00
|
|
|
pub enum AbiArchitecture {
|
2014-06-09 15:12:30 -05:00
|
|
|
/// Not a real ABI (e.g., intrinsic)
|
|
|
|
RustArch,
|
|
|
|
/// An ABI that specifies cross-platform defaults (e.g., "C")
|
|
|
|
AllArch,
|
|
|
|
/// Multiple architectures (bitset)
|
|
|
|
Archs(u32)
|
2013-03-13 21:25:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static AbiDatas: &'static [AbiData] = &[
|
|
|
|
// Platform-specific ABIs
|
|
|
|
AbiData {abi: Cdecl, name: "cdecl", abi_arch: Archs(IntelBits)},
|
|
|
|
AbiData {abi: Stdcall, name: "stdcall", abi_arch: Archs(IntelBits)},
|
|
|
|
AbiData {abi: Fastcall, name:"fastcall", abi_arch: Archs(IntelBits)},
|
|
|
|
AbiData {abi: Aapcs, name: "aapcs", abi_arch: Archs(ArmBits)},
|
2013-11-16 17:25:56 -06:00
|
|
|
AbiData {abi: Win64, name: "win64",
|
|
|
|
abi_arch: Archs(1 << (X86_64 as uint))},
|
2013-03-13 21:25:28 -05:00
|
|
|
|
|
|
|
// Cross-platform ABIs
|
|
|
|
//
|
|
|
|
// NB: Do not adjust this ordering without
|
|
|
|
// adjusting the indices below.
|
|
|
|
AbiData {abi: Rust, name: "Rust", abi_arch: RustArch},
|
|
|
|
AbiData {abi: C, name: "C", abi_arch: AllArch},
|
2013-11-08 13:06:57 -06:00
|
|
|
AbiData {abi: System, name: "system", abi_arch: AllArch},
|
2013-03-13 21:25:28 -05:00
|
|
|
AbiData {abi: RustIntrinsic, name: "rust-intrinsic", abi_arch: RustArch},
|
2014-05-29 00:26:56 -05:00
|
|
|
AbiData {abi: RustCall, name: "rust-call", abi_arch: RustArch},
|
2013-03-13 21:25:28 -05:00
|
|
|
];
|
|
|
|
|
2014-06-09 15:12:30 -05:00
|
|
|
/// Returns the ABI with the given name (if any).
|
2013-03-13 21:25:28 -05:00
|
|
|
pub fn lookup(name: &str) -> Option<Abi> {
|
2014-06-12 05:13:25 -05:00
|
|
|
AbiDatas.iter().find(|abi_data| name == abi_data.name).map(|&x| x.abi)
|
2013-03-13 21:25:28 -05:00
|
|
|
}
|
|
|
|
|
2014-02-28 15:09:09 -06:00
|
|
|
pub fn all_names() -> Vec<&'static str> {
|
2014-02-28 14:54:01 -06:00
|
|
|
AbiDatas.iter().map(|d| d.name).collect()
|
2013-03-13 21:25:28 -05:00
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
impl Abi {
|
2013-03-13 21:25:28 -05:00
|
|
|
#[inline]
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn index(&self) -> uint {
|
2013-03-13 21:25:28 -05:00
|
|
|
*self as uint
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn data(&self) -> &'static AbiData {
|
2013-03-13 21:25:28 -05:00
|
|
|
&AbiDatas[self.index()]
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn name(&self) -> &'static str {
|
2013-03-13 21:25:28 -05:00
|
|
|
self.data().name
|
|
|
|
}
|
2013-11-08 13:06:57 -06:00
|
|
|
|
2014-04-02 03:19:41 -05:00
|
|
|
pub fn for_target(&self, os: Os, arch: Architecture) -> Option<Abi> {
|
|
|
|
// If this ABI isn't actually for the specified architecture, then we
|
|
|
|
// short circuit early
|
|
|
|
match self.data().abi_arch {
|
|
|
|
Archs(a) if a & arch.bit() == 0 => return None,
|
|
|
|
Archs(_) | RustArch | AllArch => {}
|
|
|
|
}
|
|
|
|
// Transform this ABI as appropriate for the requested os/arch
|
|
|
|
// combination.
|
|
|
|
Some(match (*self, os, arch) {
|
2014-08-11 18:32:11 -05:00
|
|
|
(System, OsWindows, X86) => Stdcall,
|
2013-11-08 13:06:57 -06:00
|
|
|
(System, _, _) => C,
|
|
|
|
(me, _, _) => me,
|
2014-04-02 03:19:41 -05:00
|
|
|
})
|
2013-11-08 13:06:57 -06:00
|
|
|
}
|
2013-03-13 21:25:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Architecture {
|
|
|
|
fn bit(&self) -> u32 {
|
2014-04-21 16:58:52 -05:00
|
|
|
1 << (*self as uint)
|
2013-03-13 21:25:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-19 20:56:33 -06:00
|
|
|
impl fmt::Show for Abi {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2014-05-10 16:05:06 -05:00
|
|
|
write!(f, "\"{}\"", self.name())
|
2013-03-13 21:25:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-11 02:48:17 -05:00
|
|
|
impl fmt::Show for Os {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match *self {
|
|
|
|
OsLinux => "linux".fmt(f),
|
2014-08-10 23:26:45 -05:00
|
|
|
OsWindows => "windows".fmt(f),
|
2014-06-11 02:48:17 -05:00
|
|
|
OsMacos => "macos".fmt(f),
|
|
|
|
OsiOS => "ios".fmt(f),
|
|
|
|
OsAndroid => "android".fmt(f),
|
2014-07-29 09:44:39 -05:00
|
|
|
OsFreebsd => "freebsd".fmt(f),
|
|
|
|
OsDragonfly => "dragonfly".fmt(f)
|
2014-06-11 02:48:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-18 07:45:17 -05:00
|
|
|
#[allow(non_snake_case)]
|
2013-03-13 21:25:28 -05:00
|
|
|
#[test]
|
|
|
|
fn lookup_Rust() {
|
|
|
|
let abi = lookup("Rust");
|
2013-08-03 18:59:24 -05:00
|
|
|
assert!(abi.is_some() && abi.unwrap().data().name == "Rust");
|
2013-03-13 21:25:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn lookup_cdecl() {
|
|
|
|
let abi = lookup("cdecl");
|
2013-08-03 18:59:24 -05:00
|
|
|
assert!(abi.is_some() && abi.unwrap().data().name == "cdecl");
|
2013-03-13 21:25:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn lookup_baz() {
|
|
|
|
let abi = lookup("baz");
|
|
|
|
assert!(abi.is_none());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn indices_are_correct() {
|
2013-08-03 11:45:23 -05:00
|
|
|
for (i, abi_data) in AbiDatas.iter().enumerate() {
|
2013-11-08 13:06:57 -06:00
|
|
|
assert_eq!(i, abi_data.abi.index());
|
2013-03-13 21:25:28 -05:00
|
|
|
}
|
|
|
|
|
2014-04-21 16:58:52 -05:00
|
|
|
let bits = 1 << (X86 as uint);
|
|
|
|
let bits = bits | 1 << (X86_64 as uint);
|
2013-11-08 13:06:57 -06:00
|
|
|
assert_eq!(IntelBits, bits);
|
2013-03-13 21:25:28 -05:00
|
|
|
|
2014-04-21 16:58:52 -05:00
|
|
|
let bits = 1 << (Arm as uint);
|
2013-11-08 13:06:57 -06:00
|
|
|
assert_eq!(ArmBits, bits);
|
2013-03-13 21:25:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn pick_uniplatform() {
|
2014-04-03 16:09:16 -05:00
|
|
|
assert_eq!(Stdcall.for_target(OsLinux, X86), Some(Stdcall));
|
|
|
|
assert_eq!(Stdcall.for_target(OsLinux, Arm), None);
|
|
|
|
assert_eq!(System.for_target(OsLinux, X86), Some(C));
|
2014-08-11 18:32:11 -05:00
|
|
|
assert_eq!(System.for_target(OsWindows, X86), Some(Stdcall));
|
|
|
|
assert_eq!(System.for_target(OsWindows, X86_64), Some(C));
|
|
|
|
assert_eq!(System.for_target(OsWindows, Arm), Some(C));
|
|
|
|
assert_eq!(Stdcall.for_target(OsWindows, X86), Some(Stdcall));
|
|
|
|
assert_eq!(Stdcall.for_target(OsWindows, X86_64), Some(Stdcall));
|
2013-03-13 21:25:28 -05:00
|
|
|
}
|