1 use crate::ir::Type;
2 use crate::ir::types;
3 use crate::settings::{self, LibcallCallConv};
4 use core::fmt;
5 use core::str;
6 use target_lexicon::{CallingConvention, Triple};
7 
8 #[cfg(feature = "enable-serde")]
9 use serde_derive::{Deserialize, Serialize};
10 
11 /// Calling convention identifiers.
12 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
13 #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
14 pub enum CallConv {
15     /// Best performance, not ABI-stable.
16     Fast,
17     /// Smallest caller code size, not ABI-stable.
18     Cold,
19     /// Supports tail calls, not ABI-stable except for exception
20     /// payload registers.
21     ///
22     /// On exception resume, a caller to a `tail`-convention function
23     /// assumes that the exception payload values are in the following
24     /// registers (per platform):
25     /// - x86-64: rax, rdx
26     /// - aarch64: x0, x1
27     /// - riscv64: a0, a1
28     /// - pulley{32,64}: x0, x1
29     //
30     // Currently, this is basically sys-v except that callees pop stack
31     // arguments, rather than callers. Expected to change even more in the
32     // future, however!
33     Tail,
34     /// System V-style convention used on many platforms.
35     SystemV,
36     /// Windows "fastcall" convention, also used for x64 and ARM.
37     WindowsFastcall,
38     /// Mac aarch64 calling convention, which is a tweaked aarch64 ABI.
39     AppleAarch64,
40     /// Specialized convention for the probestack function.
41     Probestack,
42     /// The winch calling convention, not ABI-stable.
43     ///
44     /// The main difference to SystemV is that the winch calling convention
45     /// defines no callee-save registers, and restricts the number of return
46     /// registers to one integer, and one floating point.
47     Winch,
48 }
49 
50 impl CallConv {
51     /// Return the default calling convention for the given target triple.
52     pub fn triple_default(triple: &Triple) -> Self {
53         match triple.default_calling_convention() {
54             // Default to System V for unknown targets because most everything
55             // uses System V.
56             Ok(CallingConvention::SystemV) | Err(()) => Self::SystemV,
57             Ok(CallingConvention::AppleAarch64) => Self::AppleAarch64,
58             Ok(CallingConvention::WindowsFastcall) => Self::WindowsFastcall,
59             Ok(unimp) => unimplemented!("calling convention: {:?}", unimp),
60         }
61     }
62 
63     /// Returns the calling convention used for libcalls according to the current flags.
64     pub fn for_libcall(flags: &settings::Flags, default_call_conv: CallConv) -> Self {
65         match flags.libcall_call_conv() {
66             LibcallCallConv::IsaDefault => default_call_conv,
67             LibcallCallConv::Fast => Self::Fast,
68             LibcallCallConv::Cold => Self::Cold,
69             LibcallCallConv::SystemV => Self::SystemV,
70             LibcallCallConv::WindowsFastcall => Self::WindowsFastcall,
71             LibcallCallConv::AppleAarch64 => Self::AppleAarch64,
72             LibcallCallConv::Probestack => Self::Probestack,
73         }
74     }
75 
76     /// Does this calling convention support tail calls?
77     pub fn supports_tail_calls(&self) -> bool {
78         match self {
79             CallConv::Tail => true,
80             _ => false,
81         }
82     }
83 
84     /// Does this calling convention support exceptions?
85     pub fn supports_exceptions(&self) -> bool {
86         match self {
87             CallConv::Tail | CallConv::SystemV | CallConv::Winch => true,
88             _ => false,
89         }
90     }
91 
92     /// What types do the exception payload value(s) have?
93     ///
94     /// Note that this function applies to the *callee* of a `try_call`
95     /// instruction. The calling convention of the callee may differ from the
96     /// caller, but the exceptional payload types available are defined by the
97     /// callee calling convention.
98     ///
99     /// Also note that individual backends are responsible for reporting
100     /// register destinations for exceptional types. Internally Cranelift
101     /// asserts that the backend supports the exact same number of register
102     /// destinations as this return value.
103     pub fn exception_payload_types(&self, pointer_ty: Type) -> &[Type] {
104         match self {
105             CallConv::Tail | CallConv::SystemV => match pointer_ty {
106                 types::I32 => &[types::I32, types::I32],
107                 types::I64 => &[types::I64, types::I64],
108                 _ => unreachable!(),
109             },
110             _ => &[],
111         }
112     }
113 }
114 
115 impl fmt::Display for CallConv {
116     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
117         f.write_str(match *self {
118             Self::Fast => "fast",
119             Self::Cold => "cold",
120             Self::Tail => "tail",
121             Self::SystemV => "system_v",
122             Self::WindowsFastcall => "windows_fastcall",
123             Self::AppleAarch64 => "apple_aarch64",
124             Self::Probestack => "probestack",
125             Self::Winch => "winch",
126         })
127     }
128 }
129 
130 impl str::FromStr for CallConv {
131     type Err = ();
132     fn from_str(s: &str) -> Result<Self, Self::Err> {
133         match s {
134             "fast" => Ok(Self::Fast),
135             "cold" => Ok(Self::Cold),
136             "tail" => Ok(Self::Tail),
137             "system_v" => Ok(Self::SystemV),
138             "windows_fastcall" => Ok(Self::WindowsFastcall),
139             "apple_aarch64" => Ok(Self::AppleAarch64),
140             "probestack" => Ok(Self::Probestack),
141             "winch" => Ok(Self::Winch),
142             _ => Err(()),
143         }
144     }
145 }
146