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