1 //! ARM 64-bit Instruction Set Architecture.
2 
3 use crate::dominator_tree::DominatorTree;
4 use crate::ir::{self, Function, Type};
5 use crate::isa::aarch64::settings as aarch64_settings;
6 #[cfg(feature = "unwind")]
7 use crate::isa::unwind::systemv;
8 use crate::isa::{Builder as IsaBuilder, FunctionAlignment, IsaFlagsHashKey, TargetIsa};
9 #[cfg(feature = "unwind")]
10 use crate::machinst::CompiledCode;
11 use crate::machinst::{
12     CompiledCodeStencil, MachInst, MachTextSectionBuilder, Reg, SigSet, TextSectionBuilder, VCode,
13     compile,
14 };
15 use crate::result::CodegenResult;
16 use crate::settings as shared_settings;
17 use alloc::string::String;
18 use alloc::{boxed::Box, vec::Vec};
19 use core::fmt;
20 use cranelift_control::ControlPlane;
21 #[cfg(feature = "unwind")]
22 use target_lexicon::OperatingSystem;
23 use target_lexicon::{Aarch64Architecture, Architecture, Triple};
24 
25 // New backend:
26 mod abi;
27 pub mod inst;
28 mod lower;
29 pub mod settings;
30 
31 use self::inst::EmitInfo;
32 
33 /// An AArch64 backend.
34 pub struct AArch64Backend {
35     triple: Triple,
36     flags: shared_settings::Flags,
37     isa_flags: aarch64_settings::Flags,
38 }
39 
40 impl AArch64Backend {
41     /// Create a new AArch64 backend with the given (shared) flags.
new_with_flags( triple: Triple, flags: shared_settings::Flags, isa_flags: aarch64_settings::Flags, ) -> AArch64Backend42     pub fn new_with_flags(
43         triple: Triple,
44         flags: shared_settings::Flags,
45         isa_flags: aarch64_settings::Flags,
46     ) -> AArch64Backend {
47         AArch64Backend {
48             triple,
49             flags,
50             isa_flags,
51         }
52     }
53 
54     /// This performs lowering to VCode, register-allocates the code, computes block layout and
55     /// finalizes branches. The result is ready for binary emission.
compile_vcode( &self, func: &Function, domtree: &DominatorTree, ctrl_plane: &mut ControlPlane, ) -> CodegenResult<(VCode<inst::Inst>, regalloc2::Output)>56     fn compile_vcode(
57         &self,
58         func: &Function,
59         domtree: &DominatorTree,
60         ctrl_plane: &mut ControlPlane,
61     ) -> CodegenResult<(VCode<inst::Inst>, regalloc2::Output)> {
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>(func, domtree, self, abi, emit_info, sigs, ctrl_plane)
66     }
67 }
68 
69 impl TargetIsa for AArch64Backend {
compile_function( &self, func: &Function, domtree: &DominatorTree, want_disasm: bool, ctrl_plane: &mut ControlPlane, ) -> CodegenResult<CompiledCodeStencil>70     fn compile_function(
71         &self,
72         func: &Function,
73         domtree: &DominatorTree,
74         want_disasm: bool,
75         ctrl_plane: &mut ControlPlane,
76     ) -> CodegenResult<CompiledCodeStencil> {
77         let (vcode, regalloc_result) = self.compile_vcode(func, domtree, ctrl_plane)?;
78 
79         let emit_result = vcode.emit(&regalloc_result, want_disasm, &self.flags, ctrl_plane);
80         let value_labels_ranges = emit_result.value_labels_ranges;
81         let buffer = emit_result.buffer;
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             vcode: emit_result.disasm,
90             value_labels_ranges,
91             bb_starts: emit_result.bb_offsets,
92             bb_edges: emit_result.bb_edges,
93         })
94     }
95 
name(&self) -> &'static str96     fn name(&self) -> &'static str {
97         "aarch64"
98     }
99 
triple(&self) -> &Triple100     fn triple(&self) -> &Triple {
101         &self.triple
102     }
103 
flags(&self) -> &shared_settings::Flags104     fn flags(&self) -> &shared_settings::Flags {
105         &self.flags
106     }
107 
isa_flags(&self) -> Vec<shared_settings::Value>108     fn isa_flags(&self) -> Vec<shared_settings::Value> {
109         self.isa_flags.iter().collect()
110     }
111 
isa_flags_hash_key(&self) -> IsaFlagsHashKey<'_>112     fn isa_flags_hash_key(&self) -> IsaFlagsHashKey<'_> {
113         IsaFlagsHashKey(self.isa_flags.hash_key())
114     }
115 
is_branch_protection_enabled(&self) -> bool116     fn is_branch_protection_enabled(&self) -> bool {
117         self.isa_flags.use_bti()
118     }
119 
dynamic_vector_bytes(&self, _dyn_ty: Type) -> u32120     fn dynamic_vector_bytes(&self, _dyn_ty: Type) -> u32 {
121         16
122     }
123 
124     #[cfg(feature = "unwind")]
emit_unwind_info( &self, result: &CompiledCode, kind: crate::isa::unwind::UnwindInfoKind, ) -> CodegenResult<Option<crate::isa::unwind::UnwindInfo>>125     fn emit_unwind_info(
126         &self,
127         result: &CompiledCode,
128         kind: crate::isa::unwind::UnwindInfoKind,
129     ) -> CodegenResult<Option<crate::isa::unwind::UnwindInfo>> {
130         use crate::isa::unwind::UnwindInfo;
131         use crate::isa::unwind::UnwindInfoKind;
132         Ok(match kind {
133             UnwindInfoKind::SystemV => {
134                 let mapper = self::inst::unwind::systemv::RegisterMapper;
135                 Some(UnwindInfo::SystemV(
136                     crate::isa::unwind::systemv::create_unwind_info_from_insts(
137                         &result.buffer.unwind_info[..],
138                         result.buffer.data().len(),
139                         &mapper,
140                     )?,
141                 ))
142             }
143             UnwindInfoKind::Windows => Some(UnwindInfo::WindowsArm64(
144                 crate::isa::unwind::winarm64::create_unwind_info_from_insts(
145                     &result.buffer.unwind_info[..],
146                 )?,
147             )),
148             _ => None,
149         })
150     }
151 
152     #[cfg(feature = "unwind")]
create_systemv_cie(&self) -> Option<gimli::write::CommonInformationEntry>153     fn create_systemv_cie(&self) -> Option<gimli::write::CommonInformationEntry> {
154         let is_apple_os = match self.triple.operating_system {
155             OperatingSystem::Darwin(_)
156             | OperatingSystem::IOS(_)
157             | OperatingSystem::MacOSX { .. }
158             | OperatingSystem::TvOS(_) => true,
159             _ => false,
160         };
161 
162         if self.isa_flags.sign_return_address()
163             && self.isa_flags.sign_return_address_with_bkey()
164             && !is_apple_os
165         {
166             unimplemented!(
167                 "Specifying that the B key is used with pointer authentication instructions in the CIE is not implemented."
168             );
169         }
170 
171         Some(inst::unwind::systemv::create_cie())
172     }
173 
text_section_builder(&self, num_funcs: usize) -> Box<dyn TextSectionBuilder>174     fn text_section_builder(&self, num_funcs: usize) -> Box<dyn TextSectionBuilder> {
175         Box::new(MachTextSectionBuilder::<inst::Inst>::new(num_funcs))
176     }
177 
178     #[cfg(feature = "unwind")]
map_regalloc_reg_to_dwarf(&self, reg: Reg) -> Result<u16, systemv::RegisterMappingError>179     fn map_regalloc_reg_to_dwarf(&self, reg: Reg) -> Result<u16, systemv::RegisterMappingError> {
180         inst::unwind::systemv::map_reg(reg).map(|reg| reg.0)
181     }
182 
function_alignment(&self) -> FunctionAlignment183     fn function_alignment(&self) -> FunctionAlignment {
184         inst::Inst::function_alignment()
185     }
186 
page_size_align_log2(&self) -> u8187     fn page_size_align_log2(&self) -> u8 {
188         use target_lexicon::*;
189         match self.triple().operating_system {
190             OperatingSystem::MacOSX { .. }
191             | OperatingSystem::Darwin(_)
192             | OperatingSystem::IOS(_)
193             | OperatingSystem::TvOS(_) => {
194                 debug_assert_eq!(1 << 14, 0x4000);
195                 14
196             }
197             _ => {
198                 debug_assert_eq!(1 << 16, 0x10000);
199                 16
200             }
201         }
202     }
203 
204     #[cfg(feature = "disas")]
to_capstone(&self) -> Result<capstone::Capstone, capstone::Error>205     fn to_capstone(&self) -> Result<capstone::Capstone, capstone::Error> {
206         use capstone::prelude::*;
207         let mut cs = Capstone::new()
208             .arm64()
209             .mode(arch::arm64::ArchMode::Arm)
210             .detail(true)
211             .build()?;
212         // AArch64 uses inline constants rather than a separate constant pool right now.
213         // Without this option, Capstone will stop disassembling as soon as it sees
214         // an inline constant that is not also a valid instruction. With this option,
215         // Capstone will print a `.byte` directive with the bytes of the inline constant
216         // and continue to the next instruction.
217         cs.set_skipdata(true)?;
218         Ok(cs)
219     }
220 
pretty_print_reg(&self, reg: Reg, _size: u8) -> String221     fn pretty_print_reg(&self, reg: Reg, _size: u8) -> String {
222         inst::regs::pretty_print_reg(reg)
223     }
224 
has_native_fma(&self) -> bool225     fn has_native_fma(&self) -> bool {
226         true
227     }
228 
has_round(&self) -> bool229     fn has_round(&self) -> bool {
230         true
231     }
232 
has_blendv_lowering(&self, _: Type) -> bool233     fn has_blendv_lowering(&self, _: Type) -> bool {
234         false
235     }
236 
has_x86_pshufb_lowering(&self) -> bool237     fn has_x86_pshufb_lowering(&self) -> bool {
238         false
239     }
240 
has_x86_pmulhrsw_lowering(&self) -> bool241     fn has_x86_pmulhrsw_lowering(&self) -> bool {
242         false
243     }
244 
has_x86_pmaddubsw_lowering(&self) -> bool245     fn has_x86_pmaddubsw_lowering(&self) -> bool {
246         false
247     }
248 
default_argument_extension(&self) -> ir::ArgumentExtension249     fn default_argument_extension(&self) -> ir::ArgumentExtension {
250         // This is copied/carried over from a historical piece of code in
251         // Wasmtime:
252         //
253         // https://github.com/bytecodealliance/wasmtime/blob/a018a5a9addb77d5998021a0150192aa955c71bf/crates/cranelift/src/lib.rs#L366-L374
254         //
255         // Whether or not it is still applicable here is unsure, but it's left
256         // the same as-is for now to reduce the likelihood of problems arising.
257         ir::ArgumentExtension::Uext
258     }
259 }
260 
261 impl fmt::Display for AArch64Backend {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result262     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
263         f.debug_struct("MachBackend")
264             .field("name", &self.name())
265             .field("triple", &self.triple())
266             .field("flags", &format!("{}", self.flags()))
267             .finish()
268     }
269 }
270 
271 /// Create a new `isa::Builder`.
isa_builder(triple: Triple) -> IsaBuilder272 pub fn isa_builder(triple: Triple) -> IsaBuilder {
273     assert!(triple.architecture == Architecture::Aarch64(Aarch64Architecture::Aarch64));
274     IsaBuilder {
275         triple,
276         setup: aarch64_settings::builder(),
277         constructor: |triple, shared_flags, builder| {
278             let isa_flags = aarch64_settings::Flags::new(&shared_flags, builder);
279             let backend = AArch64Backend::new_with_flags(triple, shared_flags, isa_flags);
280             Ok(backend.wrapped())
281         },
282     }
283 }
284