Path: blob/main/cranelift/codegen/src/isa/aarch64/mod.rs
3073 views
//! ARM 64-bit Instruction Set Architecture.12use crate::dominator_tree::DominatorTree;3use crate::ir::{self, Function, Type};4use crate::isa::aarch64::settings as aarch64_settings;5#[cfg(feature = "unwind")]6use crate::isa::unwind::systemv;7use crate::isa::{Builder as IsaBuilder, FunctionAlignment, IsaFlagsHashKey, TargetIsa};8use crate::machinst::{9CompiledCode, CompiledCodeStencil, MachInst, MachTextSectionBuilder, Reg, SigSet,10TextSectionBuilder, VCode, compile,11};12use crate::result::CodegenResult;13use crate::settings as shared_settings;14use alloc::string::String;15use alloc::{boxed::Box, vec::Vec};16use core::fmt;17use cranelift_control::ControlPlane;18use target_lexicon::{Aarch64Architecture, Architecture, OperatingSystem, Triple};1920// New backend:21mod abi;22pub mod inst;23mod lower;24mod pcc;25pub mod settings;2627use self::inst::EmitInfo;2829/// An AArch64 backend.30pub struct AArch64Backend {31triple: Triple,32flags: shared_settings::Flags,33isa_flags: aarch64_settings::Flags,34}3536impl AArch64Backend {37/// Create a new AArch64 backend with the given (shared) flags.38pub fn new_with_flags(39triple: Triple,40flags: shared_settings::Flags,41isa_flags: aarch64_settings::Flags,42) -> AArch64Backend {43AArch64Backend {44triple,45flags,46isa_flags,47}48}4950/// This performs lowering to VCode, register-allocates the code, computes block layout and51/// finalizes branches. The result is ready for binary emission.52fn compile_vcode(53&self,54func: &Function,55domtree: &DominatorTree,56ctrl_plane: &mut ControlPlane,57) -> CodegenResult<(VCode<inst::Inst>, regalloc2::Output)> {58let emit_info = EmitInfo::new(self.flags.clone());59let sigs = SigSet::new::<abi::AArch64MachineDeps>(func, &self.flags)?;60let abi = abi::AArch64Callee::new(func, self, &self.isa_flags, &sigs)?;61compile::compile::<AArch64Backend>(func, domtree, self, abi, emit_info, sigs, ctrl_plane)62}63}6465impl TargetIsa for AArch64Backend {66fn compile_function(67&self,68func: &Function,69domtree: &DominatorTree,70want_disasm: bool,71ctrl_plane: &mut ControlPlane,72) -> CodegenResult<CompiledCodeStencil> {73let (vcode, regalloc_result) = self.compile_vcode(func, domtree, ctrl_plane)?;7475let emit_result = vcode.emit(®alloc_result, want_disasm, &self.flags, ctrl_plane);76let value_labels_ranges = emit_result.value_labels_ranges;77let buffer = emit_result.buffer;7879if let Some(disasm) = emit_result.disasm.as_ref() {80log::debug!("disassembly:\n{disasm}");81}8283Ok(CompiledCodeStencil {84buffer,85vcode: emit_result.disasm,86value_labels_ranges,87bb_starts: emit_result.bb_offsets,88bb_edges: emit_result.bb_edges,89})90}9192fn name(&self) -> &'static str {93"aarch64"94}9596fn triple(&self) -> &Triple {97&self.triple98}99100fn flags(&self) -> &shared_settings::Flags {101&self.flags102}103104fn isa_flags(&self) -> Vec<shared_settings::Value> {105self.isa_flags.iter().collect()106}107108fn isa_flags_hash_key(&self) -> IsaFlagsHashKey<'_> {109IsaFlagsHashKey(self.isa_flags.hash_key())110}111112fn is_branch_protection_enabled(&self) -> bool {113self.isa_flags.use_bti()114}115116fn dynamic_vector_bytes(&self, _dyn_ty: Type) -> u32 {11716118}119120#[cfg(feature = "unwind")]121fn emit_unwind_info(122&self,123result: &CompiledCode,124kind: crate::isa::unwind::UnwindInfoKind,125) -> CodegenResult<Option<crate::isa::unwind::UnwindInfo>> {126use crate::isa::unwind::UnwindInfo;127use crate::isa::unwind::UnwindInfoKind;128Ok(match kind {129UnwindInfoKind::SystemV => {130let mapper = self::inst::unwind::systemv::RegisterMapper;131Some(UnwindInfo::SystemV(132crate::isa::unwind::systemv::create_unwind_info_from_insts(133&result.buffer.unwind_info[..],134result.buffer.data().len(),135&mapper,136)?,137))138}139UnwindInfoKind::Windows => Some(UnwindInfo::WindowsArm64(140crate::isa::unwind::winarm64::create_unwind_info_from_insts(141&result.buffer.unwind_info[..],142)?,143)),144_ => None,145})146}147148#[cfg(feature = "unwind")]149fn create_systemv_cie(&self) -> Option<gimli::write::CommonInformationEntry> {150let is_apple_os = match self.triple.operating_system {151OperatingSystem::Darwin(_)152| OperatingSystem::IOS(_)153| OperatingSystem::MacOSX { .. }154| OperatingSystem::TvOS(_) => true,155_ => false,156};157158if self.isa_flags.sign_return_address()159&& self.isa_flags.sign_return_address_with_bkey()160&& !is_apple_os161{162unimplemented!(163"Specifying that the B key is used with pointer authentication instructions in the CIE is not implemented."164);165}166167Some(inst::unwind::systemv::create_cie())168}169170fn text_section_builder(&self, num_funcs: usize) -> Box<dyn TextSectionBuilder> {171Box::new(MachTextSectionBuilder::<inst::Inst>::new(num_funcs))172}173174#[cfg(feature = "unwind")]175fn map_regalloc_reg_to_dwarf(&self, reg: Reg) -> Result<u16, systemv::RegisterMappingError> {176inst::unwind::systemv::map_reg(reg).map(|reg| reg.0)177}178179fn function_alignment(&self) -> FunctionAlignment {180inst::Inst::function_alignment()181}182183fn page_size_align_log2(&self) -> u8 {184use target_lexicon::*;185match self.triple().operating_system {186OperatingSystem::MacOSX { .. }187| OperatingSystem::Darwin(_)188| OperatingSystem::IOS(_)189| OperatingSystem::TvOS(_) => {190debug_assert_eq!(1 << 14, 0x4000);19114192}193_ => {194debug_assert_eq!(1 << 16, 0x10000);19516196}197}198}199200#[cfg(feature = "disas")]201fn to_capstone(&self) -> Result<capstone::Capstone, capstone::Error> {202use capstone::prelude::*;203let mut cs = Capstone::new()204.arm64()205.mode(arch::arm64::ArchMode::Arm)206.detail(true)207.build()?;208// AArch64 uses inline constants rather than a separate constant pool right now.209// Without this option, Capstone will stop disassembling as soon as it sees210// an inline constant that is not also a valid instruction. With this option,211// Capstone will print a `.byte` directive with the bytes of the inline constant212// and continue to the next instruction.213cs.set_skipdata(true)?;214Ok(cs)215}216217fn pretty_print_reg(&self, reg: Reg, _size: u8) -> String {218inst::regs::pretty_print_reg(reg)219}220221fn has_native_fma(&self) -> bool {222true223}224225fn has_round(&self) -> bool {226true227}228229fn has_blendv_lowering(&self, _: Type) -> bool {230false231}232233fn has_x86_pshufb_lowering(&self) -> bool {234false235}236237fn has_x86_pmulhrsw_lowering(&self) -> bool {238false239}240241fn has_x86_pmaddubsw_lowering(&self) -> bool {242false243}244245fn default_argument_extension(&self) -> ir::ArgumentExtension {246// This is copied/carried over from a historical piece of code in247// Wasmtime:248//249// https://github.com/bytecodealliance/wasmtime/blob/a018a5a9addb77d5998021a0150192aa955c71bf/crates/cranelift/src/lib.rs#L366-L374250//251// Whether or not it is still applicable here is unsure, but it's left252// the same as-is for now to reduce the likelihood of problems arising.253ir::ArgumentExtension::Uext254}255}256257impl fmt::Display for AArch64Backend {258fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {259f.debug_struct("MachBackend")260.field("name", &self.name())261.field("triple", &self.triple())262.field("flags", &format!("{}", self.flags()))263.finish()264}265}266267/// Create a new `isa::Builder`.268pub fn isa_builder(triple: Triple) -> IsaBuilder {269assert!(triple.architecture == Architecture::Aarch64(Aarch64Architecture::Aarch64));270IsaBuilder {271triple,272setup: aarch64_settings::builder(),273constructor: |triple, shared_flags, builder| {274let isa_flags = aarch64_settings::Flags::new(&shared_flags, builder);275let backend = AArch64Backend::new_with_flags(triple, shared_flags, isa_flags);276Ok(backend.wrapped())277},278}279}280281282