2015-01-29 08:19:28 +01:00
|
|
|
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
|
2013-03-13 22:25:28 -04: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 18:56:33 -08:00
|
|
|
use std::fmt;
|
2013-03-13 22:25:28 -04:00
|
|
|
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(PartialEq, Eq, Hash, RustcEncodable, RustcDecodable, Clone, Copy, Debug)]
|
2013-03-13 22:25:28 -04:00
|
|
|
pub enum Abi {
|
|
|
|
|
// NB: This ordering MUST match the AbiDatas array below.
|
|
|
|
|
// (This is ensured by the test indices_are_correct().)
|
|
|
|
|
|
2016-10-24 11:04:04 +02:00
|
|
|
// Single platform ABIs
|
2013-03-13 22:25:28 -04:00
|
|
|
Cdecl,
|
|
|
|
|
Stdcall,
|
|
|
|
|
Fastcall,
|
2015-12-26 21:29:28 +01:00
|
|
|
Vectorcall,
|
2013-03-13 22:25:28 -04:00
|
|
|
Aapcs,
|
2013-11-16 18:25:56 -05:00
|
|
|
Win64,
|
2016-06-27 02:34:02 +02:00
|
|
|
SysV64,
|
2016-12-22 16:24:29 -05:00
|
|
|
PtxKernel,
|
2016-12-18 23:45:20 -05:00
|
|
|
Msp430Interrupt,
|
2013-03-13 22:25:28 -04:00
|
|
|
|
2016-10-24 11:04:04 +02:00
|
|
|
// Multiplatform / generic ABIs
|
2013-03-13 22:25:28 -04:00
|
|
|
Rust,
|
|
|
|
|
C,
|
2013-11-08 11:06:57 -08:00
|
|
|
System,
|
2013-03-13 22:25:28 -04:00
|
|
|
RustIntrinsic,
|
2014-05-28 22:26:56 -07:00
|
|
|
RustCall,
|
2015-08-06 11:11:26 -07:00
|
|
|
PlatformIntrinsic,
|
2016-12-23 10:05:41 +02:00
|
|
|
Unadjusted
|
2013-03-13 22:25:28 -04:00
|
|
|
}
|
|
|
|
|
|
2015-03-30 09:38:59 -04:00
|
|
|
#[derive(Copy, Clone)]
|
2014-02-27 18:48:21 +11:00
|
|
|
pub struct AbiData {
|
2013-03-13 22:25:28 -04:00
|
|
|
abi: Abi,
|
|
|
|
|
|
2016-10-24 11:04:04 +02:00
|
|
|
/// Name of this ABI as we like it called.
|
2013-03-13 22:25:28 -04:00
|
|
|
name: &'static str,
|
|
|
|
|
|
2016-10-24 11:04:04 +02:00
|
|
|
/// A generic ABI is supported on all platforms.
|
|
|
|
|
generic: bool,
|
2013-03-13 22:25:28 -04:00
|
|
|
}
|
|
|
|
|
|
2014-10-27 15:37:07 -07:00
|
|
|
#[allow(non_upper_case_globals)]
|
2015-02-27 15:36:53 +01:00
|
|
|
const AbiDatas: &'static [AbiData] = &[
|
2013-03-13 22:25:28 -04:00
|
|
|
// Platform-specific ABIs
|
2016-10-24 11:04:04 +02:00
|
|
|
AbiData {abi: Abi::Cdecl, name: "cdecl", generic: false },
|
|
|
|
|
AbiData {abi: Abi::Stdcall, name: "stdcall", generic: false },
|
|
|
|
|
AbiData {abi: Abi::Fastcall, name: "fastcall", generic: false },
|
|
|
|
|
AbiData {abi: Abi::Vectorcall, name: "vectorcall", generic: false},
|
|
|
|
|
AbiData {abi: Abi::Aapcs, name: "aapcs", generic: false },
|
|
|
|
|
AbiData {abi: Abi::Win64, name: "win64", generic: false },
|
|
|
|
|
AbiData {abi: Abi::SysV64, name: "sysv64", generic: false },
|
2016-12-22 16:24:29 -05:00
|
|
|
AbiData {abi: Abi::PtxKernel, name: "ptx-kernel", generic: false },
|
2016-12-18 23:45:20 -05:00
|
|
|
AbiData {abi: Abi::Msp430Interrupt, name: "msp430-interrupt", generic: false },
|
2013-03-13 22:25:28 -04:00
|
|
|
|
|
|
|
|
// Cross-platform ABIs
|
2016-10-24 11:04:04 +02:00
|
|
|
AbiData {abi: Abi::Rust, name: "Rust", generic: true },
|
|
|
|
|
AbiData {abi: Abi::C, name: "C", generic: true },
|
|
|
|
|
AbiData {abi: Abi::System, name: "system", generic: true },
|
|
|
|
|
AbiData {abi: Abi::RustIntrinsic, name: "rust-intrinsic", generic: true },
|
|
|
|
|
AbiData {abi: Abi::RustCall, name: "rust-call", generic: true },
|
|
|
|
|
AbiData {abi: Abi::PlatformIntrinsic, name: "platform-intrinsic", generic: true },
|
2016-12-23 10:05:41 +02:00
|
|
|
AbiData {abi: Abi::Unadjusted, name: "unadjusted", generic: true },
|
2013-03-13 22:25:28 -04:00
|
|
|
];
|
|
|
|
|
|
2014-06-09 13:12:30 -07:00
|
|
|
/// Returns the ABI with the given name (if any).
|
2013-03-13 22:25:28 -04:00
|
|
|
pub fn lookup(name: &str) -> Option<Abi> {
|
2014-06-12 06:13:25 -04:00
|
|
|
AbiDatas.iter().find(|abi_data| name == abi_data.name).map(|&x| x.abi)
|
2013-03-13 22:25:28 -04:00
|
|
|
}
|
|
|
|
|
|
2014-02-28 13:09:09 -08:00
|
|
|
pub fn all_names() -> Vec<&'static str> {
|
2014-02-28 12:54:01 -08:00
|
|
|
AbiDatas.iter().map(|d| d.name).collect()
|
2013-03-13 22:25:28 -04:00
|
|
|
}
|
|
|
|
|
|
2013-05-31 15:17:22 -07:00
|
|
|
impl Abi {
|
2013-03-13 22:25:28 -04:00
|
|
|
#[inline]
|
2015-01-17 23:33:05 +00:00
|
|
|
pub fn index(&self) -> usize {
|
|
|
|
|
*self as usize
|
2013-03-13 22:25:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
2013-05-31 15:17:22 -07:00
|
|
|
pub fn data(&self) -> &'static AbiData {
|
2013-03-13 22:25:28 -04:00
|
|
|
&AbiDatas[self.index()]
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-31 15:17:22 -07:00
|
|
|
pub fn name(&self) -> &'static str {
|
2013-03-13 22:25:28 -04:00
|
|
|
self.data().name
|
|
|
|
|
}
|
2016-10-24 11:04:04 +02:00
|
|
|
|
|
|
|
|
pub fn generic(&self) -> bool {
|
|
|
|
|
self.data().generic
|
|
|
|
|
}
|
2013-03-13 22:25:28 -04:00
|
|
|
}
|
|
|
|
|
|
2015-01-20 15:45:07 -08:00
|
|
|
impl fmt::Display for Abi {
|
2014-02-19 18:56:33 -08:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2014-05-10 14:05:06 -07:00
|
|
|
write!(f, "\"{}\"", self.name())
|
2013-03-13 22:25:28 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-19 00:45:17 +12:00
|
|
|
#[allow(non_snake_case)]
|
2013-03-13 22:25:28 -04:00
|
|
|
#[test]
|
|
|
|
|
fn lookup_Rust() {
|
|
|
|
|
let abi = lookup("Rust");
|
2013-08-04 01:59:24 +02:00
|
|
|
assert!(abi.is_some() && abi.unwrap().data().name == "Rust");
|
2013-03-13 22:25:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn lookup_cdecl() {
|
|
|
|
|
let abi = lookup("cdecl");
|
2013-08-04 01:59:24 +02:00
|
|
|
assert!(abi.is_some() && abi.unwrap().data().name == "cdecl");
|
2013-03-13 22:25:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn lookup_baz() {
|
|
|
|
|
let abi = lookup("baz");
|
|
|
|
|
assert!(abi.is_none());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn indices_are_correct() {
|
2013-08-03 12:45:23 -04:00
|
|
|
for (i, abi_data) in AbiDatas.iter().enumerate() {
|
2013-11-08 11:06:57 -08:00
|
|
|
assert_eq!(i, abi_data.abi.index());
|
2013-03-13 22:25:28 -04:00
|
|
|
}
|
|
|
|
|
}
|