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.
57    PreserveAll,
58}
59
60impl CallConv {
61    /// Return the default calling convention for the given target triple.
62    pub fn triple_default(triple: &Triple) -> Self {
63        match triple.default_calling_convention() {
64            // Default to System V for unknown targets because most everything
65            // uses System V.
66            Ok(CallingConvention::SystemV) | Err(()) => Self::SystemV,
67            Ok(CallingConvention::AppleAarch64) => Self::AppleAarch64,
68            Ok(CallingConvention::WindowsFastcall) => Self::WindowsFastcall,
69            Ok(unimp) => unimplemented!("calling convention: {:?}", unimp),
70        }
71    }
72
73    /// Returns the calling convention used for libcalls according to the current flags.
74    pub fn for_libcall(flags: &settings::Flags, default_call_conv: CallConv) -> Self {
75        match flags.libcall_call_conv() {
76            LibcallCallConv::IsaDefault => default_call_conv,
77            LibcallCallConv::Fast => Self::Fast,
78            LibcallCallConv::SystemV => Self::SystemV,
79            LibcallCallConv::WindowsFastcall => Self::WindowsFastcall,
80            LibcallCallConv::AppleAarch64 => Self::AppleAarch64,
81            LibcallCallConv::PreserveAll => Self::PreserveAll,
82            LibcallCallConv::Probestack => Self::Probestack,
83        }
84    }
85
86    /// Does this calling convention support tail calls?
87    pub fn supports_tail_calls(&self) -> bool {
88        match self {
89            CallConv::Tail => true,
90            _ => false,
91        }
92    }
93
94    /// Does this calling convention support exceptions?
95    pub fn supports_exceptions(&self) -> bool {
96        match self {
97            CallConv::Tail | CallConv::SystemV | CallConv::Winch | CallConv::PreserveAll => true,
98            _ => false,
99        }
100    }
101
102    /// What types do the exception payload value(s) have?
103    ///
104    /// Note that this function applies to the *callee* of a `try_call`
105    /// instruction. The calling convention of the callee may differ from the
106    /// caller, but the exceptional payload types available are defined by the
107    /// callee calling convention.
108    ///
109    /// Also note that individual backends are responsible for reporting
110    /// register destinations for exceptional types. Internally Cranelift
111    /// asserts that the backend supports the exact same number of register
112    /// destinations as this return value.
113    pub fn exception_payload_types(&self, pointer_ty: Type) -> &[Type] {
114        match self {
115            CallConv::Tail | CallConv::SystemV | CallConv::PreserveAll => match pointer_ty {
116                types::I32 => &[types::I32, types::I32],
117                types::I64 => &[types::I64, types::I64],
118                _ => unreachable!(),
119            },
120            _ => &[],
121        }
122    }
123}
124
125impl fmt::Display for CallConv {
126    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
127        f.write_str(match *self {
128            Self::Fast => "fast",
129            Self::Tail => "tail",
130            Self::SystemV => "system_v",
131            Self::WindowsFastcall => "windows_fastcall",
132            Self::AppleAarch64 => "apple_aarch64",
133            Self::Probestack => "probestack",
134            Self::Winch => "winch",
135            Self::PreserveAll => "preserve_all",
136        })
137    }
138}
139
140impl str::FromStr for CallConv {
141    type Err = ();
142    fn from_str(s: &str) -> Result<Self, Self::Err> {
143        match s {
144            "fast" => Ok(Self::Fast),
145            "tail" => Ok(Self::Tail),
146            "system_v" => Ok(Self::SystemV),
147            "windows_fastcall" => Ok(Self::WindowsFastcall),
148            "apple_aarch64" => Ok(Self::AppleAarch64),
149            "probestack" => Ok(Self::Probestack),
150            "winch" => Ok(Self::Winch),
151            "preserve_all" => Ok(Self::PreserveAll),
152            _ => Err(()),
153        }
154    }
155}