A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
use crate::ir::Type;
use crate::ir::types;
use crate::settings::{self, LibcallCallConv};
use core::fmt;
use core::str;
use target_lexicon::{CallingConvention, Triple};
#[cfg(feature = "enable-serde")]
use serde_derive::{Deserialize, Serialize};
/// Calling convention identifiers.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
pub enum CallConv {
/// Best performance, not ABI-stable.
Fast,
/// Supports tail calls, not ABI-stable except as stated here.
///
/// On exception resume, a caller to a `tail`-convention function assumes
/// that the exception payload values are in the following registers (per
/// platform):
/// - x86-64: rax, rdx
/// - aarch64: x0, x1
/// - riscv64: a0, a1
/// - pulley{32,64}: x0, x1
///
/// The `interrupt_poll` instruction uses registers as follows:
/// - x86-64: `context` in rdi, `load_ptr` and `next_load_ptr` in r11,
/// scratch in r10
/// - aarch64: `context` in x0, `load_ptr` and `next_load_ptr` in x10,
/// scratch in x9
//
// Currently, this is basically sys-v except that callees pop stack
// arguments, rather than callers. Expected to change even more in the
// future, however!
Tail,
/// System V-style convention used on many platforms.
SystemV,
/// Windows "fastcall" convention, also used for x64 and ARM.
WindowsFastcall,
/// Mac aarch64 calling convention, which is a tweaked aarch64 ABI.
AppleAarch64,
/// Specialized convention for the probestack function.
Probestack,
/// Internal calling convention for Winch-generated functions.
/// Cranelift-generated trampolines use the same convention to
/// interoperate with them. This convention is not ABI-stable.
///
/// Callees reclaim their aligned incoming stack-argument area
/// before returning.
///
/// Only the last result is returned in a register; any preceding
/// results are written to the return area in memory.
Winch,
/// Calling convention optimized for callsite efficiency, at the
/// cost of the callee. It does so by not clobbering any
/// registers.
///
/// This is designed for a very specific need: we want callsites
/// that we can insert as instrumentation (perhaps patchable)
/// while affecting surrounding instructions' register allocation
/// as little as possible.
///
/// The ABI is based on the native register-argument ABI on each
/// respective platform. It does not support tail-calls. It also
/// does not support return values.
PreserveAll,
}
impl CallConv {
/// Return the default calling convention for the given target triple.
pub fn triple_default(triple: &Triple) -> Self {
match triple.default_calling_convention() {
// Default to System V for unknown targets because most everything
// uses System V.
Ok(CallingConvention::SystemV) | Err(()) => Self::SystemV,
Ok(CallingConvention::AppleAarch64) => Self::AppleAarch64,
Ok(CallingConvention::WindowsFastcall) => Self::WindowsFastcall,
Ok(unimp) => unimplemented!("calling convention: {:?}", unimp),
}
}
/// Returns the calling convention used for libcalls according to the current flags.
pub fn for_libcall(flags: &settings::Flags, default_call_conv: CallConv) -> Self {
match flags.libcall_call_conv() {
LibcallCallConv::IsaDefault => default_call_conv,
LibcallCallConv::Fast => Self::Fast,
LibcallCallConv::SystemV => Self::SystemV,
LibcallCallConv::WindowsFastcall => Self::WindowsFastcall,
LibcallCallConv::AppleAarch64 => Self::AppleAarch64,
LibcallCallConv::PreserveAll => Self::PreserveAll,
LibcallCallConv::Probestack => Self::Probestack,
}
}
/// Does this calling convention support tail calls?
pub fn supports_tail_calls(&self) -> bool {
match self {
CallConv::Tail => true,
_ => false,
}
}
/// Does this calling convention support exceptions?
pub fn supports_exceptions(&self) -> bool {
match self {
CallConv::Tail
| CallConv::SystemV
| CallConv::Winch
| CallConv::PreserveAll
| CallConv::AppleAarch64 => true,
_ => false,
}
}
/// What types do the exception payload value(s) have?
///
/// Note that this function applies to the *callee* of a `try_call`
/// instruction. The calling convention of the callee may differ from the
/// caller, but the exceptional payload types available are defined by the
/// callee calling convention.
///
/// Also note that individual backends are responsible for reporting
/// register destinations for exceptional types. Internally Cranelift
/// asserts that the backend supports the exact same number of register
/// destinations as this return value.
pub fn exception_payload_types(&self, pointer_ty: Type) -> &[Type] {
match self {
CallConv::Tail | CallConv::SystemV | CallConv::PreserveAll | CallConv::AppleAarch64 => {
match pointer_ty {
types::I32 => &[types::I32, types::I32],
types::I64 => &[types::I64, types::I64],
_ => unreachable!(),
}
}
_ => &[],
}
}
}
impl fmt::Display for CallConv {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(match *self {
Self::Fast => "fast",
Self::Tail => "tail",
Self::SystemV => "system_v",
Self::WindowsFastcall => "windows_fastcall",
Self::AppleAarch64 => "apple_aarch64",
Self::Probestack => "probestack",
Self::Winch => "winch",
Self::PreserveAll => "preserve_all",
})
}
}
impl str::FromStr for CallConv {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"fast" => Ok(Self::Fast),
"tail" => Ok(Self::Tail),
"system_v" => Ok(Self::SystemV),
"windows_fastcall" => Ok(Self::WindowsFastcall),
"apple_aarch64" => Ok(Self::AppleAarch64),
"probestack" => Ok(Self::Probestack),
"winch" => Ok(Self::Winch),
"preserve_all" => Ok(Self::PreserveAll),
_ => Err(()),
}
}
}