cranelift_codegen/isa/aarch64/
mod.rs1use crate::dominator_tree::DominatorTree;
4use crate::ir::{self, Function, Type};
5use crate::isa::aarch64::settings as aarch64_settings;
6#[cfg(feature = "unwind")]
7use crate::isa::unwind::systemv;
8use crate::isa::{Builder as IsaBuilder, FunctionAlignment, IsaFlagsHashKey, TargetIsa};
9use crate::machinst::{
10 CompiledCode, CompiledCodeStencil, MachInst, MachTextSectionBuilder, Reg, SigSet,
11 TextSectionBuilder, VCode, compile,
12};
13use crate::result::CodegenResult;
14use crate::settings as shared_settings;
15use alloc::{boxed::Box, vec::Vec};
16use core::fmt;
17use cranelift_control::ControlPlane;
18use std::string::String;
19use target_lexicon::{Aarch64Architecture, Architecture, OperatingSystem, Triple};
20
21mod abi;
23pub mod inst;
24mod lower;
25mod pcc;
26pub mod settings;
27
28use self::inst::EmitInfo;
29
30pub struct AArch64Backend {
32 triple: Triple,
33 flags: shared_settings::Flags,
34 isa_flags: aarch64_settings::Flags,
35}
36
37impl AArch64Backend {
38 pub fn new_with_flags(
40 triple: Triple,
41 flags: shared_settings::Flags,
42 isa_flags: aarch64_settings::Flags,
43 ) -> AArch64Backend {
44 AArch64Backend {
45 triple,
46 flags,
47 isa_flags,
48 }
49 }
50
51 fn compile_vcode(
54 &self,
55 func: &Function,
56 domtree: &DominatorTree,
57 ctrl_plane: &mut ControlPlane,
58 ) -> CodegenResult<(VCode<inst::Inst>, regalloc2::Output)> {
59 let emit_info = EmitInfo::new(self.flags.clone());
60 let sigs = SigSet::new::<abi::AArch64MachineDeps>(func, &self.flags)?;
61 let abi = abi::AArch64Callee::new(func, self, &self.isa_flags, &sigs)?;
62 compile::compile::<AArch64Backend>(func, domtree, self, abi, emit_info, sigs, ctrl_plane)
63 }
64}
65
66impl TargetIsa for AArch64Backend {
67 fn compile_function(
68 &self,
69 func: &Function,
70 domtree: &DominatorTree,
71 want_disasm: bool,
72 ctrl_plane: &mut ControlPlane,
73 ) -> CodegenResult<CompiledCodeStencil> {
74 let (vcode, regalloc_result) = self.compile_vcode(func, domtree, ctrl_plane)?;
75
76 let emit_result = vcode.emit(®alloc_result, want_disasm, &self.flags, ctrl_plane);
77 let frame_size = emit_result.frame_size;
78 let value_labels_ranges = emit_result.value_labels_ranges;
79 let buffer = emit_result.buffer;
80 let sized_stackslot_offsets = emit_result.sized_stackslot_offsets;
81 let dynamic_stackslot_offsets = emit_result.dynamic_stackslot_offsets;
82
83 if let Some(disasm) = emit_result.disasm.as_ref() {
84 log::debug!("disassembly:\n{disasm}");
85 }
86
87 Ok(CompiledCodeStencil {
88 buffer,
89 frame_size,
90 vcode: emit_result.disasm,
91 value_labels_ranges,
92 sized_stackslot_offsets,
93 dynamic_stackslot_offsets,
94 bb_starts: emit_result.bb_offsets,
95 bb_edges: emit_result.bb_edges,
96 })
97 }
98
99 fn name(&self) -> &'static str {
100 "aarch64"
101 }
102
103 fn triple(&self) -> &Triple {
104 &self.triple
105 }
106
107 fn flags(&self) -> &shared_settings::Flags {
108 &self.flags
109 }
110
111 fn isa_flags(&self) -> Vec<shared_settings::Value> {
112 self.isa_flags.iter().collect()
113 }
114
115 fn isa_flags_hash_key(&self) -> IsaFlagsHashKey<'_> {
116 IsaFlagsHashKey(self.isa_flags.hash_key())
117 }
118
119 fn is_branch_protection_enabled(&self) -> bool {
120 self.isa_flags.use_bti()
121 }
122
123 fn dynamic_vector_bytes(&self, _dyn_ty: Type) -> u32 {
124 16
125 }
126
127 #[cfg(feature = "unwind")]
128 fn emit_unwind_info(
129 &self,
130 result: &CompiledCode,
131 kind: crate::isa::unwind::UnwindInfoKind,
132 ) -> CodegenResult<Option<crate::isa::unwind::UnwindInfo>> {
133 use crate::isa::unwind::UnwindInfo;
134 use crate::isa::unwind::UnwindInfoKind;
135 Ok(match kind {
136 UnwindInfoKind::SystemV => {
137 let mapper = self::inst::unwind::systemv::RegisterMapper;
138 Some(UnwindInfo::SystemV(
139 crate::isa::unwind::systemv::create_unwind_info_from_insts(
140 &result.buffer.unwind_info[..],
141 result.buffer.data().len(),
142 &mapper,
143 )?,
144 ))
145 }
146 UnwindInfoKind::Windows => Some(UnwindInfo::WindowsArm64(
147 crate::isa::unwind::winarm64::create_unwind_info_from_insts(
148 &result.buffer.unwind_info[..],
149 )?,
150 )),
151 _ => None,
152 })
153 }
154
155 #[cfg(feature = "unwind")]
156 fn create_systemv_cie(&self) -> Option<gimli::write::CommonInformationEntry> {
157 let is_apple_os = match self.triple.operating_system {
158 OperatingSystem::Darwin(_)
159 | OperatingSystem::IOS(_)
160 | OperatingSystem::MacOSX { .. }
161 | OperatingSystem::TvOS(_) => true,
162 _ => false,
163 };
164
165 if self.isa_flags.sign_return_address()
166 && self.isa_flags.sign_return_address_with_bkey()
167 && !is_apple_os
168 {
169 unimplemented!(
170 "Specifying that the B key is used with pointer authentication instructions in the CIE is not implemented."
171 );
172 }
173
174 Some(inst::unwind::systemv::create_cie())
175 }
176
177 fn text_section_builder(&self, num_funcs: usize) -> Box<dyn TextSectionBuilder> {
178 Box::new(MachTextSectionBuilder::<inst::Inst>::new(num_funcs))
179 }
180
181 #[cfg(feature = "unwind")]
182 fn map_regalloc_reg_to_dwarf(&self, reg: Reg) -> Result<u16, systemv::RegisterMappingError> {
183 inst::unwind::systemv::map_reg(reg).map(|reg| reg.0)
184 }
185
186 fn function_alignment(&self) -> FunctionAlignment {
187 inst::Inst::function_alignment()
188 }
189
190 fn page_size_align_log2(&self) -> u8 {
191 use target_lexicon::*;
192 match self.triple().operating_system {
193 OperatingSystem::MacOSX { .. }
194 | OperatingSystem::Darwin(_)
195 | OperatingSystem::IOS(_)
196 | OperatingSystem::TvOS(_) => {
197 debug_assert_eq!(1 << 14, 0x4000);
198 14
199 }
200 _ => {
201 debug_assert_eq!(1 << 16, 0x10000);
202 16
203 }
204 }
205 }
206
207 #[cfg(feature = "disas")]
208 fn to_capstone(&self) -> Result<capstone::Capstone, capstone::Error> {
209 use capstone::prelude::*;
210 let mut cs = Capstone::new()
211 .arm64()
212 .mode(arch::arm64::ArchMode::Arm)
213 .detail(true)
214 .build()?;
215 cs.set_skipdata(true)?;
221 Ok(cs)
222 }
223
224 fn pretty_print_reg(&self, reg: Reg, _size: u8) -> String {
225 inst::regs::pretty_print_reg(reg)
226 }
227
228 fn has_native_fma(&self) -> bool {
229 true
230 }
231
232 fn has_round(&self) -> bool {
233 true
234 }
235
236 fn has_x86_blendv_lowering(&self, _: Type) -> bool {
237 false
238 }
239
240 fn has_x86_pshufb_lowering(&self) -> bool {
241 false
242 }
243
244 fn has_x86_pmulhrsw_lowering(&self) -> bool {
245 false
246 }
247
248 fn has_x86_pmaddubsw_lowering(&self) -> bool {
249 false
250 }
251
252 fn default_argument_extension(&self) -> ir::ArgumentExtension {
253 ir::ArgumentExtension::Uext
261 }
262}
263
264impl fmt::Display for AArch64Backend {
265 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
266 f.debug_struct("MachBackend")
267 .field("name", &self.name())
268 .field("triple", &self.triple())
269 .field("flags", &format!("{}", self.flags()))
270 .finish()
271 }
272}
273
274pub fn isa_builder(triple: Triple) -> IsaBuilder {
276 assert!(triple.architecture == Architecture::Aarch64(Aarch64Architecture::Aarch64));
277 IsaBuilder {
278 triple,
279 setup: aarch64_settings::builder(),
280 constructor: |triple, shared_flags, builder| {
281 let isa_flags = aarch64_settings::Flags::new(&shared_flags, builder);
282 let backend = AArch64Backend::new_with_flags(triple, shared_flags, isa_flags);
283 Ok(backend.wrapped())
284 },
285 }
286}