Skip to main content

cranelift_codegen/isa/
call_conv.rs

1use crate::ir::Type;
2use crate::ir::types;
3use crate::settings::{self, LibcallCallConv};
4use core::fmt;
5use core::str;
6use target_lexicon::{CallingConvention, Triple};
7
8#[cfg(feature = "enable-serde")]
9use serde_derive::{Deserialize, Serialize};
10
11/// Calling convention identifiers.
12#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
13#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
14pub enum CallConv {
15    /// Best performance, not ABI-stable.
16    Fast,
17    /// Supports tail calls, not ABI-stable except for exception
18    /// payload registers.
19    ///
20    /// On exception resume, a caller to a `tail`-convention function
21    /// assumes that the exception payload values are in the following
22    /// registers (per platform):
23    /// - x86-64: rax, rdx
24    /// - aarch64: x0, x1
25    /// - riscv64: a0, a1
26    /// - pulley{32,64}: x0, x1
27    //
28    // Currently, this is basically sys-v except that callees pop stack
29    // arguments, rather than callers. Expected to change even more in the
30    // future, however!
31    Tail,
32    /// System V-style convention used on many platforms.
33    SystemV,
34    /// Windows "fastcall" convention, also used for x64 and ARM.
35    WindowsFastcall,
36    /// Mac aarch64 calling convention, which is a tweaked aarch64 ABI.
37    AppleAarch64,
38    /// Specialized convention for the probestack function.
39    Probestack,
40    /// The winch calling convention, not ABI-stable.
41    ///
42    /// The main difference to SystemV is that the winch calling convention
43    /// defines no callee-save registers, and restricts the number of return
44    /// registers to one integer, and one floating point.
45    Winch,
46    /// Calling convention optimized for callsite efficiency, at the
47    /// cost of the callee. It does so by not clobbering any
48    /// registers.
49    ///
50    /// This is designed for a very specific need: we want callsites
51    /// that we can insert as instrumentation (perhaps patchable)
52    /// while affecting surrounding instructions' register allocation
53    /// as little as possible.
54    ///
55    /// The ABI is based on the native register-argument ABI on each
56    /// respective platform. It does not support tail-calls. It also
57    /// does not support return values.
58    PreserveAll,
59}
60
61impl CallConv {
62    /// Return the default calling convention for the given target triple.
63    pub fn triple_default(triple: &Triple) -> Self {
64        match triple.default_calling_convention() {
65            // Default to System V for unknown targets because most everything
66            // uses System V.
67            Ok(CallingConvention::SystemV) | Err(()) => Self::SystemV,
68            Ok(CallingConvention::AppleAarch64) => Self::AppleAarch64,
69            Ok(CallingConvention::WindowsFastcall) => Self::WindowsFastcall,
70            Ok(unimp) => unimplemented!("calling convention: {:?}", unimp),
71        }
72    }
73
74    /// Returns the calling convention used for libcalls according to the current flags.
75    pub fn for_libcall(flags: &settings::Flags, default_call_conv: CallConv) -> Self {
76        match flags.libcall_call_conv() {
77            LibcallCallConv::IsaDefault => default_call_conv,
78            LibcallCallConv::Fast => Self::Fast,
79            LibcallCallConv::SystemV => Self::SystemV,
80            LibcallCallConv::WindowsFastcall => Self::WindowsFastcall,
81            LibcallCallConv::AppleAarch64 => Self::AppleAarch64,
82            LibcallCallConv::PreserveAll => Self::PreserveAll,
83            LibcallCallConv::Probestack => Self::Probestack,
84        }
85    }
86
87    /// Does this calling convention support tail calls?
88    pub fn supports_tail_calls(&self) -> bool {
89        match self {
90            CallConv::Tail => true,
91            _ => false,
92        }
93    }
94
95    /// Does this calling convention support exceptions?
96    pub fn supports_exceptions(&self) -> bool {
97        match self {
98            CallConv::Tail
99            | CallConv::SystemV
100            | CallConv::Winch
101            | CallConv::PreserveAll
102            | CallConv::AppleAarch64 => true,
103            _ => false,
104        }
105    }
106
107    /// What types do the exception payload value(s) have?
108    ///
109    /// Note that this function applies to the *callee* of a `try_call`
110    /// instruction. The calling convention of the callee may differ from the
111    /// caller, but the exceptional payload types available are defined by the
112    /// callee calling convention.
113    ///
114    /// Also note that individual backends are responsible for reporting
115    /// register destinations for exceptional types. Internally Cranelift
116    /// asserts that the backend supports the exact same number of register
117    /// destinations as this return value.
118    pub fn exception_payload_types(&self, pointer_ty: Type) -> &[Type] {
119        match self {
120            CallConv::Tail | CallConv::SystemV | CallConv::PreserveAll | CallConv::AppleAarch64 => {
121                match pointer_ty {
122                    types::I32 => &[types::I32, types::I32],
123                    types::I64 => &[types::I64, types::I64],
124                    _ => unreachable!(),
125                }
126            }
127            _ => &[],
128        }
129    }
130}
131
132impl fmt::Display for CallConv {
133    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
134        f.write_str(match *self {
135            Self::Fast => "fast",
136            Self::Tail => "tail",
137            Self::SystemV => "system_v",
138            Self::WindowsFastcall => "windows_fastcall",
139            Self::AppleAarch64 => "apple_aarch64",
140            Self::Probestack => "probestack",
141            Self::Winch => "winch",
142            Self::PreserveAll => "preserve_all",
143        })
144    }
145}
146
147impl str::FromStr for CallConv {
148    type Err = ();
149    fn from_str(s: &str) -> Result<Self, Self::Err> {
150        match s {
151            "fast" => Ok(Self::Fast),
152            "tail" => Ok(Self::Tail),
153            "system_v" => Ok(Self::SystemV),
154            "windows_fastcall" => Ok(Self::WindowsFastcall),
155            "apple_aarch64" => Ok(Self::AppleAarch64),
156            "probestack" => Ok(Self::Probestack),
157            "winch" => Ok(Self::Winch),
158            "preserve_all" => Ok(Self::PreserveAll),
159            _ => Err(()),
160        }
161    }
162}