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