Path: blob/main/cranelift/codegen/src/isa/call_conv.rs
3050 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/// Supports tail calls, not ABI-stable except for exception17/// payload registers.18///19/// On exception resume, a caller to a `tail`-convention function20/// assumes that the exception payload values are in the following21/// registers (per platform):22/// - x86-64: rax, rdx23/// - aarch64: x0, x124/// - riscv64: a0, a125/// - pulley{32,64}: x0, x126//27// Currently, this is basically sys-v except that callees pop stack28// arguments, rather than callers. Expected to change even more in the29// future, however!30Tail,31/// System V-style convention used on many platforms.32SystemV,33/// Windows "fastcall" convention, also used for x64 and ARM.34WindowsFastcall,35/// Mac aarch64 calling convention, which is a tweaked aarch64 ABI.36AppleAarch64,37/// Specialized convention for the probestack function.38Probestack,39/// The winch calling convention, not ABI-stable.40///41/// The main difference to SystemV is that the winch calling convention42/// defines no callee-save registers, and restricts the number of return43/// registers to one integer, and one floating point.44Winch,45/// Calling convention optimized for callsite efficiency, at the46/// cost of the callee. It does so by not clobbering any47/// registers.48///49/// This is designed for a very specific need: we want callsites50/// that we can insert as instrumentation (perhaps patchable)51/// while affecting surrounding instructions' register allocation52/// as little as possible.53///54/// The ABI is based on the native register-argument ABI on each55/// respective platform. It does not support tail-calls. It also56/// does not support return values.57PreserveAll,58}5960impl CallConv {61/// Return the default calling convention for the given target triple.62pub fn triple_default(triple: &Triple) -> Self {63match triple.default_calling_convention() {64// Default to System V for unknown targets because most everything65// uses System V.66Ok(CallingConvention::SystemV) | Err(()) => Self::SystemV,67Ok(CallingConvention::AppleAarch64) => Self::AppleAarch64,68Ok(CallingConvention::WindowsFastcall) => Self::WindowsFastcall,69Ok(unimp) => unimplemented!("calling convention: {:?}", unimp),70}71}7273/// Returns the calling convention used for libcalls according to the current flags.74pub fn for_libcall(flags: &settings::Flags, default_call_conv: CallConv) -> Self {75match flags.libcall_call_conv() {76LibcallCallConv::IsaDefault => default_call_conv,77LibcallCallConv::Fast => Self::Fast,78LibcallCallConv::SystemV => Self::SystemV,79LibcallCallConv::WindowsFastcall => Self::WindowsFastcall,80LibcallCallConv::AppleAarch64 => Self::AppleAarch64,81LibcallCallConv::PreserveAll => Self::PreserveAll,82LibcallCallConv::Probestack => Self::Probestack,83}84}8586/// Does this calling convention support tail calls?87pub fn supports_tail_calls(&self) -> bool {88match self {89CallConv::Tail => true,90_ => false,91}92}9394/// Does this calling convention support exceptions?95pub fn supports_exceptions(&self) -> bool {96match self {97CallConv::Tail | CallConv::SystemV | CallConv::Winch | CallConv::PreserveAll => true,98_ => false,99}100}101102/// 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 the106/// caller, but the exceptional payload types available are defined by the107/// callee calling convention.108///109/// Also note that individual backends are responsible for reporting110/// register destinations for exceptional types. Internally Cranelift111/// asserts that the backend supports the exact same number of register112/// destinations as this return value.113pub fn exception_payload_types(&self, pointer_ty: Type) -> &[Type] {114match self {115CallConv::Tail | CallConv::SystemV | CallConv::PreserveAll => match pointer_ty {116types::I32 => &[types::I32, types::I32],117types::I64 => &[types::I64, types::I64],118_ => unreachable!(),119},120_ => &[],121}122}123}124125impl fmt::Display for CallConv {126fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {127f.write_str(match *self {128Self::Fast => "fast",129Self::Tail => "tail",130Self::SystemV => "system_v",131Self::WindowsFastcall => "windows_fastcall",132Self::AppleAarch64 => "apple_aarch64",133Self::Probestack => "probestack",134Self::Winch => "winch",135Self::PreserveAll => "preserve_all",136})137}138}139140impl str::FromStr for CallConv {141type Err = ();142fn from_str(s: &str) -> Result<Self, Self::Err> {143match 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}156157158