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 Winch,
37}
38
39impl CallConv {
40 pub fn triple_default(triple: &Triple) -> Self {
42 match triple.default_calling_convention() {
43 Ok(CallingConvention::SystemV) | Err(()) => Self::SystemV,
46 Ok(CallingConvention::AppleAarch64) => Self::AppleAarch64,
47 Ok(CallingConvention::WindowsFastcall) => Self::WindowsFastcall,
48 Ok(unimp) => unimplemented!("calling convention: {:?}", unimp),
49 }
50 }
51
52 pub fn for_libcall(flags: &settings::Flags, default_call_conv: CallConv) -> Self {
54 match flags.libcall_call_conv() {
55 LibcallCallConv::IsaDefault => default_call_conv,
56 LibcallCallConv::Fast => Self::Fast,
57 LibcallCallConv::Cold => Self::Cold,
58 LibcallCallConv::SystemV => Self::SystemV,
59 LibcallCallConv::WindowsFastcall => Self::WindowsFastcall,
60 LibcallCallConv::AppleAarch64 => Self::AppleAarch64,
61 LibcallCallConv::Probestack => Self::Probestack,
62 }
63 }
64
65 pub fn supports_tail_calls(&self) -> bool {
67 match self {
68 CallConv::Tail => true,
69 _ => false,
70 }
71 }
72}
73
74impl fmt::Display for CallConv {
75 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
76 f.write_str(match *self {
77 Self::Fast => "fast",
78 Self::Cold => "cold",
79 Self::Tail => "tail",
80 Self::SystemV => "system_v",
81 Self::WindowsFastcall => "windows_fastcall",
82 Self::AppleAarch64 => "apple_aarch64",
83 Self::Probestack => "probestack",
84 Self::Winch => "winch",
85 })
86 }
87}
88
89impl str::FromStr for CallConv {
90 type Err = ();
91 fn from_str(s: &str) -> Result<Self, Self::Err> {
92 match s {
93 "fast" => Ok(Self::Fast),
94 "cold" => Ok(Self::Cold),
95 "tail" => Ok(Self::Tail),
96 "system_v" => Ok(Self::SystemV),
97 "windows_fastcall" => Ok(Self::WindowsFastcall),
98 "apple_aarch64" => Ok(Self::AppleAarch64),
99 "probestack" => Ok(Self::Probestack),
100 "winch" => Ok(Self::Winch),
101 _ => Err(()),
102 }
103 }
104}