Path: blob/main/cranelift/codegen/src/isa/call_conv.rs
1693 views
use crate::ir::Type;1use crate::ir::types;2use crate::settings::{self, LibcallCallConv};3use core::fmt;4use core::str;5use target_lexicon::{CallingConvention, Triple};67#[cfg(feature = "enable-serde")]8use serde_derive::{Deserialize, Serialize};910/// Calling convention identifiers.11#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]12#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]13pub enum CallConv {14/// Best performance, not ABI-stable.15Fast,16/// Smallest caller code size, not ABI-stable.17Cold,18/// Supports tail calls, not ABI-stable except for exception19/// payload registers.20///21/// On exception resume, a caller to a `tail`-convention function22/// assumes that the exception payload values are in the following23/// registers (per platform):24/// - x86-64: rax, rdx25/// - aarch64: x0, x126/// - riscv64: a0, a127/// - pulley{32,64}: x0, x128//29// Currently, this is basically sys-v except that callees pop stack30// arguments, rather than callers. Expected to change even more in the31// future, however!32Tail,33/// System V-style convention used on many platforms.34SystemV,35/// Windows "fastcall" convention, also used for x64 and ARM.36WindowsFastcall,37/// Mac aarch64 calling convention, which is a tweaked aarch64 ABI.38AppleAarch64,39/// Specialized convention for the probestack function.40Probestack,41/// The winch calling convention, not ABI-stable.42///43/// The main difference to SystemV is that the winch calling convention44/// defines no callee-save registers, and restricts the number of return45/// registers to one integer, and one floating point.46Winch,47}4849impl CallConv {50/// Return the default calling convention for the given target triple.51pub fn triple_default(triple: &Triple) -> Self {52match triple.default_calling_convention() {53// Default to System V for unknown targets because most everything54// uses System V.55Ok(CallingConvention::SystemV) | Err(()) => Self::SystemV,56Ok(CallingConvention::AppleAarch64) => Self::AppleAarch64,57Ok(CallingConvention::WindowsFastcall) => Self::WindowsFastcall,58Ok(unimp) => unimplemented!("calling convention: {:?}", unimp),59}60}6162/// Returns the calling convention used for libcalls according to the current flags.63pub fn for_libcall(flags: &settings::Flags, default_call_conv: CallConv) -> Self {64match flags.libcall_call_conv() {65LibcallCallConv::IsaDefault => default_call_conv,66LibcallCallConv::Fast => Self::Fast,67LibcallCallConv::Cold => Self::Cold,68LibcallCallConv::SystemV => Self::SystemV,69LibcallCallConv::WindowsFastcall => Self::WindowsFastcall,70LibcallCallConv::AppleAarch64 => Self::AppleAarch64,71LibcallCallConv::Probestack => Self::Probestack,72}73}7475/// Does this calling convention support tail calls?76pub fn supports_tail_calls(&self) -> bool {77match self {78CallConv::Tail => true,79_ => false,80}81}8283/// Does this calling convention support exceptions?84pub fn supports_exceptions(&self) -> bool {85match self {86CallConv::Tail | CallConv::SystemV => true,87_ => false,88}89}9091/// What types do the exception payload value(s) have?92///93/// Note that this function applies to the *callee* of a `try_call`94/// instruction. The calling convention of the callee may differ from the95/// caller, but the exceptional payload types available are defined by the96/// callee calling convention.97///98/// Also note that individual backends are responsible for reporting99/// register destinations for exceptional types. Internally Cranelift100/// asserts that the backend supports the exact same number of register101/// destinations as this return value.102pub fn exception_payload_types(&self, pointer_ty: Type) -> &[Type] {103match self {104CallConv::Tail | CallConv::SystemV => match pointer_ty {105types::I32 => &[types::I32, types::I32],106types::I64 => &[types::I64, types::I64],107_ => unreachable!(),108},109_ => &[],110}111}112}113114impl fmt::Display for CallConv {115fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {116f.write_str(match *self {117Self::Fast => "fast",118Self::Cold => "cold",119Self::Tail => "tail",120Self::SystemV => "system_v",121Self::WindowsFastcall => "windows_fastcall",122Self::AppleAarch64 => "apple_aarch64",123Self::Probestack => "probestack",124Self::Winch => "winch",125})126}127}128129impl str::FromStr for CallConv {130type Err = ();131fn from_str(s: &str) -> Result<Self, Self::Err> {132match s {133"fast" => Ok(Self::Fast),134"cold" => Ok(Self::Cold),135"tail" => Ok(Self::Tail),136"system_v" => Ok(Self::SystemV),137"windows_fastcall" => Ok(Self::WindowsFastcall),138"apple_aarch64" => Ok(Self::AppleAarch64),139"probestack" => Ok(Self::Probestack),140"winch" => Ok(Self::Winch),141_ => Err(()),142}143}144}145146147