Path: blob/main/cranelift/codegen/src/isa/s390x/mod.rs
1693 views
//! IBM Z 64-bit Instruction Set Architecture.12use crate::dominator_tree::DominatorTree;3use crate::ir::{self, Function, Type};4use crate::isa::s390x::settings as s390x_settings;5#[cfg(feature = "unwind")]6use crate::isa::unwind::systemv::RegisterMappingError;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::{Architecture, Triple};1920// New backend:21mod abi;22pub(crate) mod inst;23mod lower;24mod settings;2526use self::inst::EmitInfo;2728/// A IBM Z backend.29pub struct S390xBackend {30triple: Triple,31flags: shared_settings::Flags,32isa_flags: s390x_settings::Flags,33}3435impl S390xBackend {36/// Create a new IBM Z backend with the given (shared) flags.37pub fn new_with_flags(38triple: Triple,39flags: shared_settings::Flags,40isa_flags: s390x_settings::Flags,41) -> S390xBackend {42S390xBackend {43triple,44flags,45isa_flags,46}47}4849/// This performs lowering to VCode, register-allocates the code, computes block layout and50/// finalizes branches. The result is ready for binary emission.51fn compile_vcode(52&self,53func: &Function,54domtree: &DominatorTree,55ctrl_plane: &mut ControlPlane,56) -> CodegenResult<(VCode<inst::Inst>, regalloc2::Output)> {57let emit_info = EmitInfo::new(self.isa_flags.clone());58let sigs = SigSet::new::<abi::S390xMachineDeps>(func, &self.flags)?;59let abi = abi::S390xCallee::new(func, self, &self.isa_flags, &sigs)?;60compile::compile::<S390xBackend>(func, domtree, self, abi, emit_info, sigs, ctrl_plane)61}62}6364impl TargetIsa for S390xBackend {65fn compile_function(66&self,67func: &Function,68domtree: &DominatorTree,69want_disasm: bool,70ctrl_plane: &mut ControlPlane,71) -> CodegenResult<CompiledCodeStencil> {72let flags = self.flags();73let (vcode, regalloc_result) = self.compile_vcode(func, domtree, ctrl_plane)?;7475let emit_result = vcode.emit(®alloc_result, want_disasm, 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"s390x"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 dynamic_vector_bytes(&self, _dyn_ty: Type) -> u32 {11916120}121122#[cfg(feature = "unwind")]123fn emit_unwind_info(124&self,125result: &CompiledCode,126kind: crate::isa::unwind::UnwindInfoKind,127) -> CodegenResult<Option<crate::isa::unwind::UnwindInfo>> {128use crate::isa::unwind::UnwindInfo;129use crate::isa::unwind::UnwindInfoKind;130Ok(match kind {131UnwindInfoKind::SystemV => {132let mapper = self::inst::unwind::systemv::RegisterMapper;133Some(UnwindInfo::SystemV(134crate::isa::unwind::systemv::create_unwind_info_from_insts(135&result.buffer.unwind_info[..],136result.buffer.data().len(),137&mapper,138)?,139))140}141_ => None,142})143}144145#[cfg(feature = "unwind")]146fn create_systemv_cie(&self) -> Option<gimli::write::CommonInformationEntry> {147Some(inst::unwind::systemv::create_cie())148}149150#[cfg(feature = "unwind")]151fn map_regalloc_reg_to_dwarf(&self, reg: Reg) -> Result<u16, RegisterMappingError> {152inst::unwind::systemv::map_reg(reg).map(|reg| reg.0)153}154155fn text_section_builder(&self, num_funcs: usize) -> Box<dyn TextSectionBuilder> {156Box::new(MachTextSectionBuilder::<inst::Inst>::new(num_funcs))157}158159fn function_alignment(&self) -> FunctionAlignment {160inst::Inst::function_alignment()161}162163fn page_size_align_log2(&self) -> u8 {164debug_assert_eq!(1 << 12, 0x1000);16512166}167168#[cfg(feature = "disas")]169fn to_capstone(&self) -> Result<capstone::Capstone, capstone::Error> {170use capstone::prelude::*;171let mut cs = Capstone::new()172.sysz()173.mode(arch::sysz::ArchMode::Default)174.build()?;175176cs.set_skipdata(true)?;177178Ok(cs)179}180181fn pretty_print_reg(&self, reg: Reg, _size: u8) -> String {182inst::regs::pretty_print_reg(reg)183}184185fn has_native_fma(&self) -> bool {186true187}188189fn has_round(&self) -> bool {190true191}192193fn has_x86_blendv_lowering(&self, _: Type) -> bool {194false195}196197fn has_x86_pshufb_lowering(&self) -> bool {198false199}200201fn has_x86_pmulhrsw_lowering(&self) -> bool {202false203}204205fn has_x86_pmaddubsw_lowering(&self) -> bool {206false207}208209fn default_argument_extension(&self) -> ir::ArgumentExtension {210// This is copied/carried over from a historical piece of code in211// Wasmtime:212//213// https://github.com/bytecodealliance/wasmtime/blob/a018a5a9addb77d5998021a0150192aa955c71bf/crates/cranelift/src/lib.rs#L366-L374214//215// Whether or not it is still applicable here is unsure, but it's left216// the same as-is for now to reduce the likelihood of problems arising.217ir::ArgumentExtension::Uext218}219}220221impl fmt::Display for S390xBackend {222fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {223f.debug_struct("MachBackend")224.field("name", &self.name())225.field("triple", &self.triple())226.field("flags", &format!("{}", self.flags()))227.finish()228}229}230231/// Create a new `isa::Builder`.232pub fn isa_builder(triple: Triple) -> IsaBuilder {233assert!(triple.architecture == Architecture::S390x);234IsaBuilder {235triple,236setup: s390x_settings::builder(),237constructor: |triple, shared_flags, builder| {238let isa_flags = s390x_settings::Flags::new(&shared_flags, builder);239let backend = S390xBackend::new_with_flags(triple, shared_flags, isa_flags);240Ok(backend.wrapped())241},242}243}244245246