1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use std::fmt;

use rustc_macros::HashStable_Generic;

#[cfg(test)]
mod tests;

#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Debug)]
#[derive(HashStable_Generic, Encodable, Decodable)]
pub enum Abi {
    // Multiplatform / generic ABIs
    //
    // These ABIs come first because every time we add a new ABI, we
    // have to re-bless all the hashing tests. These are used in many
    // places, so giving them stable values reduces test churn. The
    // specific values are meaningless.
    Rust,
    C { unwind: bool },

    // Single platform ABIs
    Cdecl,
    Stdcall { unwind: bool },
    Fastcall,
    Vectorcall,
    Thiscall { unwind: bool },
    Aapcs,
    Win64,
    SysV64,
    PtxKernel,
    Msp430Interrupt,
    X86Interrupt,
    AmdGpuKernel,
    EfiApi,
    AvrInterrupt,
    AvrNonBlockingInterrupt,
    CCmseNonSecureCall,
    Wasm,

    // Multiplatform / generic ABIs
    System { unwind: bool },
    RustIntrinsic,
    RustCall,
    PlatformIntrinsic,
    Unadjusted,
}

#[derive(Copy, Clone)]
pub struct AbiData {
    abi: Abi,

    /// Name of this ABI as we like it called.
    name: &'static str,

    /// A generic ABI is supported on all platforms.
    generic: bool,
}

#[allow(non_upper_case_globals)]
const AbiDatas: &[AbiData] = &[
    // Cross-platform ABIs
    AbiData { abi: Abi::Rust, name: "Rust", generic: true },
    AbiData { abi: Abi::C { unwind: false }, name: "C", generic: true },
    AbiData { abi: Abi::C { unwind: true }, name: "C-unwind", generic: true },
    // Platform-specific ABIs
    AbiData { abi: Abi::Cdecl, name: "cdecl", generic: false },
    AbiData { abi: Abi::Stdcall { unwind: false }, name: "stdcall", generic: false },
    AbiData { abi: Abi::Stdcall { unwind: true }, name: "stdcall-unwind", generic: false },
    AbiData { abi: Abi::Fastcall, name: "fastcall", generic: false },
    AbiData { abi: Abi::Vectorcall, name: "vectorcall", generic: false },
    AbiData { abi: Abi::Thiscall { unwind: false }, name: "thiscall", generic: false },
    AbiData { abi: Abi::Thiscall { unwind: true }, name: "thiscall-unwind", 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 },
    AbiData { abi: Abi::PtxKernel, name: "ptx-kernel", generic: false },
    AbiData { abi: Abi::Msp430Interrupt, name: "msp430-interrupt", generic: false },
    AbiData { abi: Abi::X86Interrupt, name: "x86-interrupt", generic: false },
    AbiData { abi: Abi::AmdGpuKernel, name: "amdgpu-kernel", generic: false },
    AbiData { abi: Abi::EfiApi, name: "efiapi", generic: false },
    AbiData { abi: Abi::AvrInterrupt, name: "avr-interrupt", generic: false },
    AbiData {
        abi: Abi::AvrNonBlockingInterrupt,
        name: "avr-non-blocking-interrupt",
        generic: false,
    },
    AbiData { abi: Abi::CCmseNonSecureCall, name: "C-cmse-nonsecure-call", generic: false },
    AbiData { abi: Abi::Wasm, name: "wasm", generic: false },
    // Cross-platform ABIs
    AbiData { abi: Abi::System { unwind: false }, name: "system", generic: true },
    AbiData { abi: Abi::System { unwind: true }, name: "system-unwind", 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 },
    AbiData { abi: Abi::Unadjusted, name: "unadjusted", generic: true },
];

/// Returns the ABI with the given name (if any).
pub fn lookup(name: &str) -> Option<Abi> {
    AbiDatas.iter().find(|abi_data| name == abi_data.name).map(|&x| x.abi)
}

pub fn all_names() -> Vec<&'static str> {
    AbiDatas.iter().map(|d| d.name).collect()
}

impl Abi {
    #[inline]
    pub fn index(self) -> usize {
        // N.B., this ordering MUST match the AbiDatas array above.
        // (This is ensured by the test indices_are_correct().)
        use Abi::*;
        let i = match self {
            // Cross-platform ABIs
            Rust => 0,
            C { unwind: false } => 1,
            C { unwind: true } => 2,
            // Platform-specific ABIs
            Cdecl => 3,
            Stdcall { unwind: false } => 4,
            Stdcall { unwind: true } => 5,
            Fastcall => 6,
            Vectorcall => 7,
            Thiscall { unwind: false } => 8,
            Thiscall { unwind: true } => 9,
            Aapcs => 10,
            Win64 => 11,
            SysV64 => 12,
            PtxKernel => 13,
            Msp430Interrupt => 14,
            X86Interrupt => 15,
            AmdGpuKernel => 16,
            EfiApi => 17,
            AvrInterrupt => 18,
            AvrNonBlockingInterrupt => 19,
            CCmseNonSecureCall => 20,
            Wasm => 21,
            // Cross-platform ABIs
            System { unwind: false } => 22,
            System { unwind: true } => 23,
            RustIntrinsic => 24,
            RustCall => 25,
            PlatformIntrinsic => 26,
            Unadjusted => 27,
        };
        debug_assert!(
            AbiDatas
                .iter()
                .enumerate()
                .find(|(_, AbiData { abi, .. })| *abi == self)
                .map(|(index, _)| index)
                .expect("abi variant has associated data")
                == i,
            "Abi index did not match `AbiDatas` ordering"
        );
        i
    }

    #[inline]
    pub fn data(self) -> &'static AbiData {
        &AbiDatas[self.index()]
    }

    pub fn name(self) -> &'static str {
        self.data().name
    }

    pub fn generic(self) -> bool {
        self.data().generic
    }
}

impl fmt::Display for Abi {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            abi => write!(f, "\"{}\"", abi.name()),
        }
    }
}