cranelift_codegen/isa/
call_conv.rs

1use crate::settings::{self, LibcallCallConv};
2use core::fmt;
3use core::str;
4use target_lexicon::{CallingConvention, Triple};
5
6#[cfg(feature = "enable-serde")]
7use serde::{Deserialize, Serialize};
8
9/// Calling convention identifiers.
10#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
11#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
12pub enum CallConv {
13    /// Best performance, not ABI-stable.
14    Fast,
15    /// Smallest caller code size, not ABI-stable.
16    Cold,
17    /// Supports tail calls, not ABI-stable.
18    Tail,
19    /// System V-style convention used on many platforms.
20    SystemV,
21    /// Windows "fastcall" convention, also used for x64 and ARM.
22    WindowsFastcall,
23    /// Mac aarch64 calling convention, which is a tweaked aarch64 ABI.
24    AppleAarch64,
25    /// Specialized convention for the probestack function.
26    Probestack,
27    /// Wasmtime equivalent of SystemV, not ABI-stable.
28    ///
29    /// Currently only differs in how multiple return values are handled,
30    /// returning the first return value in a register and everything else
31    /// through a return-pointer.
32    WasmtimeSystemV,
33    /// Wasmtime equivalent of WindowsFastcall, not ABI-stable.
34    ///
35    /// Differs from fastcall in the same way as `WasmtimeSystemV`.
36    WasmtimeFastcall,
37    /// Wasmtime equivalent of AppleAarch64, not ABI-stable.
38    ///
39    /// Differs from apple-aarch64 in the same way as `WasmtimeSystemV`.
40    WasmtimeAppleAarch64,
41}
42
43impl CallConv {
44    /// Return the default calling convention for the given target triple.
45    pub fn triple_default(triple: &Triple) -> Self {
46        match triple.default_calling_convention() {
47            // Default to System V for unknown targets because most everything
48            // uses System V.
49            Ok(CallingConvention::SystemV) | Err(()) => Self::SystemV,
50            Ok(CallingConvention::AppleAarch64) => Self::AppleAarch64,
51            Ok(CallingConvention::WindowsFastcall) => Self::WindowsFastcall,
52            Ok(unimp) => unimplemented!("calling convention: {:?}", unimp),
53        }
54    }
55
56    /// Returns the calling convention used for libcalls according to the current flags.
57    pub fn for_libcall(flags: &settings::Flags, default_call_conv: CallConv) -> Self {
58        match flags.libcall_call_conv() {
59            LibcallCallConv::IsaDefault => default_call_conv,
60            LibcallCallConv::Fast => Self::Fast,
61            LibcallCallConv::Cold => Self::Cold,
62            LibcallCallConv::SystemV => Self::SystemV,
63            LibcallCallConv::WindowsFastcall => Self::WindowsFastcall,
64            LibcallCallConv::AppleAarch64 => Self::AppleAarch64,
65            LibcallCallConv::Probestack => Self::Probestack,
66        }
67    }
68
69    /// Does this calling convention support tail calls?
70    pub fn supports_tail_calls(&self) -> bool {
71        match self {
72            CallConv::Tail => true,
73            _ => false,
74        }
75    }
76
77    /// Is the calling convention extending the Windows Fastcall ABI?
78    pub fn extends_windows_fastcall(self) -> bool {
79        match self {
80            Self::WindowsFastcall | Self::WasmtimeFastcall => true,
81            _ => false,
82        }
83    }
84
85    /// Is the calling convention extending the Apple aarch64 ABI?
86    pub fn extends_apple_aarch64(self) -> bool {
87        match self {
88            Self::AppleAarch64 | Self::WasmtimeAppleAarch64 => true,
89            _ => false,
90        }
91    }
92
93    /// Is the calling convention extending the Wasmtime ABI?
94    pub fn extends_wasmtime(self) -> bool {
95        match self {
96            Self::WasmtimeSystemV | Self::WasmtimeFastcall | Self::WasmtimeAppleAarch64 => true,
97            _ => false,
98        }
99    }
100}
101
102impl fmt::Display for CallConv {
103    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
104        f.write_str(match *self {
105            Self::Fast => "fast",
106            Self::Cold => "cold",
107            Self::Tail => "tail",
108            Self::SystemV => "system_v",
109            Self::WindowsFastcall => "windows_fastcall",
110            Self::AppleAarch64 => "apple_aarch64",
111            Self::Probestack => "probestack",
112            Self::WasmtimeSystemV => "wasmtime_system_v",
113            Self::WasmtimeFastcall => "wasmtime_fastcall",
114            Self::WasmtimeAppleAarch64 => "wasmtime_apple_aarch64",
115        })
116    }
117}
118
119impl str::FromStr for CallConv {
120    type Err = ();
121    fn from_str(s: &str) -> Result<Self, Self::Err> {
122        match s {
123            "fast" => Ok(Self::Fast),
124            "cold" => Ok(Self::Cold),
125            "tail" => Ok(Self::Tail),
126            "system_v" => Ok(Self::SystemV),
127            "windows_fastcall" => Ok(Self::WindowsFastcall),
128            "apple_aarch64" => Ok(Self::AppleAarch64),
129            "probestack" => Ok(Self::Probestack),
130            "wasmtime_system_v" => Ok(Self::WasmtimeSystemV),
131            "wasmtime_fastcall" => Ok(Self::WasmtimeFastcall),
132            "wasmtime_apple_aarch64" => Ok(Self::WasmtimeAppleAarch64),
133            _ => Err(()),
134        }
135    }
136}