Path: blob/main/cranelift/codegen/src/isa/aarch64/mod.rs
1693 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::{boxed::Box, vec::Vec};15use core::fmt;16use cranelift_control::ControlPlane;17use std::string::String;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 frame_size = emit_result.frame_size;77let value_labels_ranges = emit_result.value_labels_ranges;78let buffer = emit_result.buffer;79let sized_stackslot_offsets = emit_result.sized_stackslot_offsets;80let dynamic_stackslot_offsets = emit_result.dynamic_stackslot_offsets;8182if let Some(disasm) = emit_result.disasm.as_ref() {83log::debug!("disassembly:\n{disasm}");84}8586Ok(CompiledCodeStencil {87buffer,88frame_size,89vcode: emit_result.disasm,90value_labels_ranges,91sized_stackslot_offsets,92dynamic_stackslot_offsets,93bb_starts: emit_result.bb_offsets,94bb_edges: emit_result.bb_edges,95})96}9798fn name(&self) -> &'static str {99"aarch64"100}101102fn triple(&self) -> &Triple {103&self.triple104}105106fn flags(&self) -> &shared_settings::Flags {107&self.flags108}109110fn isa_flags(&self) -> Vec<shared_settings::Value> {111self.isa_flags.iter().collect()112}113114fn isa_flags_hash_key(&self) -> IsaFlagsHashKey<'_> {115IsaFlagsHashKey(self.isa_flags.hash_key())116}117118fn is_branch_protection_enabled(&self) -> bool {119self.isa_flags.use_bti()120}121122fn dynamic_vector_bytes(&self, _dyn_ty: Type) -> u32 {12316124}125126#[cfg(feature = "unwind")]127fn emit_unwind_info(128&self,129result: &CompiledCode,130kind: crate::isa::unwind::UnwindInfoKind,131) -> CodegenResult<Option<crate::isa::unwind::UnwindInfo>> {132use crate::isa::unwind::UnwindInfo;133use crate::isa::unwind::UnwindInfoKind;134Ok(match kind {135UnwindInfoKind::SystemV => {136let mapper = self::inst::unwind::systemv::RegisterMapper;137Some(UnwindInfo::SystemV(138crate::isa::unwind::systemv::create_unwind_info_from_insts(139&result.buffer.unwind_info[..],140result.buffer.data().len(),141&mapper,142)?,143))144}145UnwindInfoKind::Windows => Some(UnwindInfo::WindowsArm64(146crate::isa::unwind::winarm64::create_unwind_info_from_insts(147&result.buffer.unwind_info[..],148)?,149)),150_ => None,151})152}153154#[cfg(feature = "unwind")]155fn create_systemv_cie(&self) -> Option<gimli::write::CommonInformationEntry> {156let is_apple_os = match self.triple.operating_system {157OperatingSystem::Darwin(_)158| OperatingSystem::IOS(_)159| OperatingSystem::MacOSX { .. }160| OperatingSystem::TvOS(_) => true,161_ => false,162};163164if self.isa_flags.sign_return_address()165&& self.isa_flags.sign_return_address_with_bkey()166&& !is_apple_os167{168unimplemented!(169"Specifying that the B key is used with pointer authentication instructions in the CIE is not implemented."170);171}172173Some(inst::unwind::systemv::create_cie())174}175176fn text_section_builder(&self, num_funcs: usize) -> Box<dyn TextSectionBuilder> {177Box::new(MachTextSectionBuilder::<inst::Inst>::new(num_funcs))178}179180#[cfg(feature = "unwind")]181fn map_regalloc_reg_to_dwarf(&self, reg: Reg) -> Result<u16, systemv::RegisterMappingError> {182inst::unwind::systemv::map_reg(reg).map(|reg| reg.0)183}184185fn function_alignment(&self) -> FunctionAlignment {186inst::Inst::function_alignment()187}188189fn page_size_align_log2(&self) -> u8 {190use target_lexicon::*;191match self.triple().operating_system {192OperatingSystem::MacOSX { .. }193| OperatingSystem::Darwin(_)194| OperatingSystem::IOS(_)195| OperatingSystem::TvOS(_) => {196debug_assert_eq!(1 << 14, 0x4000);19714198}199_ => {200debug_assert_eq!(1 << 16, 0x10000);20116202}203}204}205206#[cfg(feature = "disas")]207fn to_capstone(&self) -> Result<capstone::Capstone, capstone::Error> {208use capstone::prelude::*;209let mut cs = Capstone::new()210.arm64()211.mode(arch::arm64::ArchMode::Arm)212.detail(true)213.build()?;214// AArch64 uses inline constants rather than a separate constant pool right now.215// Without this option, Capstone will stop disassembling as soon as it sees216// an inline constant that is not also a valid instruction. With this option,217// Capstone will print a `.byte` directive with the bytes of the inline constant218// and continue to the next instruction.219cs.set_skipdata(true)?;220Ok(cs)221}222223fn pretty_print_reg(&self, reg: Reg, _size: u8) -> String {224inst::regs::pretty_print_reg(reg)225}226227fn has_native_fma(&self) -> bool {228true229}230231fn has_round(&self) -> bool {232true233}234235fn has_x86_blendv_lowering(&self, _: Type) -> bool {236false237}238239fn has_x86_pshufb_lowering(&self) -> bool {240false241}242243fn has_x86_pmulhrsw_lowering(&self) -> bool {244false245}246247fn has_x86_pmaddubsw_lowering(&self) -> bool {248false249}250251fn default_argument_extension(&self) -> ir::ArgumentExtension {252// This is copied/carried over from a historical piece of code in253// Wasmtime:254//255// https://github.com/bytecodealliance/wasmtime/blob/a018a5a9addb77d5998021a0150192aa955c71bf/crates/cranelift/src/lib.rs#L366-L374256//257// Whether or not it is still applicable here is unsure, but it's left258// the same as-is for now to reduce the likelihood of problems arising.259ir::ArgumentExtension::Uext260}261}262263impl fmt::Display for AArch64Backend {264fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {265f.debug_struct("MachBackend")266.field("name", &self.name())267.field("triple", &self.triple())268.field("flags", &format!("{}", self.flags()))269.finish()270}271}272273/// Create a new `isa::Builder`.274pub fn isa_builder(triple: Triple) -> IsaBuilder {275assert!(triple.architecture == Architecture::Aarch64(Aarch64Architecture::Aarch64));276IsaBuilder {277triple,278setup: aarch64_settings::builder(),279constructor: |triple, shared_flags, builder| {280let isa_flags = aarch64_settings::Flags::new(&shared_flags, builder);281let backend = AArch64Backend::new_with_flags(triple, shared_flags, isa_flags);282Ok(backend.wrapped())283},284}285}286287288