rustc_abi/
canon_abi.rs

1use std::fmt;
2
3#[cfg(feature = "nightly")]
4use rustc_macros::HashStable_Generic;
5
6use crate::ExternAbi;
7
8/// Calling convention to determine codegen
9///
10/// CanonAbi erases certain distinctions ExternAbi preserves, but remains target-dependent.
11/// There are still both target-specific variants and aliasing variants, though much fewer.
12/// The reason for this step is the frontend may wish to show an ExternAbi but implement that ABI
13/// using a different ABI than the string per se, or describe irrelevant differences, e.g.
14/// - extern "system"
15/// - extern "cdecl"
16/// - extern "C-unwind"
17/// In that sense, this erases mere syntactic distinctions to create a canonical *directive*,
18/// rather than picking the "actual" ABI.
19#[derive(Copy, Clone, Debug)]
20#[derive(PartialOrd, Ord, PartialEq, Eq, Hash)]
21#[cfg_attr(feature = "nightly", derive(HashStable_Generic))]
22pub enum CanonAbi {
23    // NOTE: the use of nested variants for some ABIs is for many targets they don't matter,
24    // and this pushes the complexity of their reasoning to target-specific code,
25    // allowing a `match` to easily exhaustively ignore these subcategories of variants.
26    // Otherwise it is very tempting to avoid matching exhaustively!
27    C,
28    Rust,
29    RustCold,
30
31    /// An ABI that rustc does not know how to call or define.
32    Custom,
33
34    /// ABIs relevant to 32-bit Arm targets
35    Arm(ArmCall),
36    /// ABI relevant to GPUs: the entry point for a GPU kernel
37    GpuKernel,
38
39    /// ABIs relevant to bare-metal interrupt targets
40    // FIXME(workingjubilee): a particular reason for this nesting is we might not need these?
41    // interrupt ABIs should have the same properties:
42    // - uncallable by Rust calls, as LLVM rejects it in most cases
43    // - uses a preserve-all-registers *callee* convention
44    // - should always return `-> !` (effectively... it can't use normal `ret`)
45    // what differs between targets is
46    // - allowed arguments: x86 differs slightly, having 2-3 arguments which are handled magically
47    // - may need special prologues/epilogues for some interrupts, without affecting "call ABI"
48    Interrupt(InterruptKind),
49
50    /// ABIs relevant to Windows or x86 targets
51    X86(X86Call),
52}
53
54impl fmt::Display for CanonAbi {
55    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56        // convert to the ExternAbi that *shares a string* with this CanonAbi.
57        // FIXME: ideally we'd avoid printing `CanonAbi`, and preserve `ExternAbi` everywhere
58        // that we need to generate error messages.
59        let erased_abi = match self {
60            CanonAbi::C => ExternAbi::C { unwind: false },
61            CanonAbi::Rust => ExternAbi::Rust,
62            CanonAbi::RustCold => ExternAbi::RustCold,
63            CanonAbi::Custom => ExternAbi::Custom,
64            CanonAbi::Arm(arm_call) => match arm_call {
65                ArmCall::Aapcs => ExternAbi::Aapcs { unwind: false },
66                ArmCall::CCmseNonSecureCall => ExternAbi::CmseNonSecureCall,
67                ArmCall::CCmseNonSecureEntry => ExternAbi::CmseNonSecureEntry,
68            },
69            CanonAbi::GpuKernel => ExternAbi::GpuKernel,
70            CanonAbi::Interrupt(interrupt_kind) => match interrupt_kind {
71                InterruptKind::Avr => ExternAbi::AvrInterrupt,
72                InterruptKind::AvrNonBlocking => ExternAbi::AvrNonBlockingInterrupt,
73                InterruptKind::Msp430 => ExternAbi::Msp430Interrupt,
74                InterruptKind::RiscvMachine => ExternAbi::RiscvInterruptM,
75                InterruptKind::RiscvSupervisor => ExternAbi::RiscvInterruptS,
76                InterruptKind::X86 => ExternAbi::X86Interrupt,
77            },
78            CanonAbi::X86(x86_call) => match x86_call {
79                X86Call::Fastcall => ExternAbi::Fastcall { unwind: false },
80                X86Call::Stdcall => ExternAbi::Stdcall { unwind: false },
81                X86Call::SysV64 => ExternAbi::SysV64 { unwind: false },
82                X86Call::Thiscall => ExternAbi::Thiscall { unwind: false },
83                X86Call::Vectorcall => ExternAbi::Vectorcall { unwind: false },
84                X86Call::Win64 => ExternAbi::Win64 { unwind: false },
85            },
86        };
87        erased_abi.as_str().fmt(f)
88    }
89}
90
91/// Callee codegen for interrupts
92///
93/// This is named differently from the "Call" enums because it is different:
94/// these "ABI" differences are not relevant to callers, since there is "no caller".
95/// These only affect callee codegen. making their categorization as distinct ABIs a bit peculiar.
96#[derive(Copy, Clone, Debug)]
97#[derive(PartialOrd, Ord, PartialEq, Eq, Hash)]
98#[cfg_attr(feature = "nightly", derive(HashStable_Generic))]
99pub enum InterruptKind {
100    Avr,
101    AvrNonBlocking,
102    Msp430,
103    RiscvMachine,
104    RiscvSupervisor,
105    X86,
106}
107
108/// ABIs defined for x86-{32,64}
109///
110/// One of SysV64 or Win64 may alias the C ABI, and arguably Win64 is cross-platform now?
111#[derive(Clone, Copy, Debug)]
112#[derive(PartialOrd, Ord, PartialEq, Eq, Hash)]
113#[cfg_attr(feature = "nightly", derive(HashStable_Generic))]
114pub enum X86Call {
115    /// "fastcall" has both GNU and Windows variants
116    Fastcall,
117    /// "stdcall" has both GNU and Windows variants
118    Stdcall,
119    SysV64,
120    Thiscall,
121    Vectorcall,
122    Win64,
123}
124
125/// ABIs defined for 32-bit Arm
126#[derive(Copy, Clone, Debug)]
127#[derive(PartialOrd, Ord, PartialEq, Eq, Hash)]
128#[cfg_attr(feature = "nightly", derive(HashStable_Generic))]
129pub enum ArmCall {
130    Aapcs,
131    CCmseNonSecureCall,
132    CCmseNonSecureEntry,
133}