cranelift_codegen/isa/
call_conv.rs1use crate::settings::{self, LibcallCallConv};
2use core::fmt;
3use core::str;
4use target_lexicon::{CallingConvention, Triple};
5
6#[cfg(feature = "enable-serde")]
7use serde_derive::{Deserialize, Serialize};
8
9#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
11#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
12pub enum CallConv {
13 Fast,
15 Cold,
17 Tail,
23 SystemV,
25 WindowsFastcall,
27 AppleAarch64,
29 Probestack,
31 WasmtimeSystemV,
36 Winch,
42}
43
44impl CallConv {
45 pub fn triple_default(triple: &Triple) -> Self {
47 match triple.default_calling_convention() {
48 Ok(CallingConvention::SystemV) | Err(()) => Self::SystemV,
51 Ok(CallingConvention::AppleAarch64) => Self::AppleAarch64,
52 Ok(CallingConvention::WindowsFastcall) => Self::WindowsFastcall,
53 Ok(unimp) => unimplemented!("calling convention: {:?}", unimp),
54 }
55 }
56
57 pub fn for_libcall(flags: &settings::Flags, default_call_conv: CallConv) -> Self {
59 match flags.libcall_call_conv() {
60 LibcallCallConv::IsaDefault => default_call_conv,
61 LibcallCallConv::Fast => Self::Fast,
62 LibcallCallConv::Cold => Self::Cold,
63 LibcallCallConv::SystemV => Self::SystemV,
64 LibcallCallConv::WindowsFastcall => Self::WindowsFastcall,
65 LibcallCallConv::AppleAarch64 => Self::AppleAarch64,
66 LibcallCallConv::Probestack => Self::Probestack,
67 }
68 }
69
70 pub fn supports_tail_calls(&self) -> bool {
72 match self {
73 CallConv::Tail => true,
74 _ => false,
75 }
76 }
77
78 pub fn extends_windows_fastcall(self) -> bool {
80 match self {
81 Self::WindowsFastcall => true,
82 _ => false,
83 }
84 }
85
86 pub fn extends_apple_aarch64(self) -> bool {
88 match self {
89 Self::AppleAarch64 => true,
90 _ => false,
91 }
92 }
93}
94
95impl fmt::Display for CallConv {
96 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
97 f.write_str(match *self {
98 Self::Fast => "fast",
99 Self::Cold => "cold",
100 Self::Tail => "tail",
101 Self::SystemV => "system_v",
102 Self::WindowsFastcall => "windows_fastcall",
103 Self::AppleAarch64 => "apple_aarch64",
104 Self::Probestack => "probestack",
105 Self::WasmtimeSystemV => "wasmtime_system_v",
106 Self::Winch => "winch",
107 })
108 }
109}
110
111impl str::FromStr for CallConv {
112 type Err = ();
113 fn from_str(s: &str) -> Result<Self, Self::Err> {
114 match s {
115 "fast" => Ok(Self::Fast),
116 "cold" => Ok(Self::Cold),
117 "tail" => Ok(Self::Tail),
118 "system_v" => Ok(Self::SystemV),
119 "windows_fastcall" => Ok(Self::WindowsFastcall),
120 "apple_aarch64" => Ok(Self::AppleAarch64),
121 "probestack" => Ok(Self::Probestack),
122 "wasmtime_system_v" => Ok(Self::WasmtimeSystemV),
123 "winch" => Ok(Self::Winch),
124 _ => Err(()),
125 }
126 }
127}