Path: blob/main/cranelift/codegen/src/isa/s390x/mod.rs
3092 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::string::String;15use alloc::{boxed::Box, vec::Vec};16use core::fmt;17use cranelift_control::ControlPlane;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 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"s390x"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 dynamic_vector_bytes(&self, _dyn_ty: Type) -> u32 {11316114}115116#[cfg(feature = "unwind")]117fn emit_unwind_info(118&self,119result: &CompiledCode,120kind: crate::isa::unwind::UnwindInfoKind,121) -> CodegenResult<Option<crate::isa::unwind::UnwindInfo>> {122use crate::isa::unwind::UnwindInfo;123use crate::isa::unwind::UnwindInfoKind;124Ok(match kind {125UnwindInfoKind::SystemV => {126let mapper = self::inst::unwind::systemv::RegisterMapper;127Some(UnwindInfo::SystemV(128crate::isa::unwind::systemv::create_unwind_info_from_insts(129&result.buffer.unwind_info[..],130result.buffer.data().len(),131&mapper,132)?,133))134}135_ => None,136})137}138139#[cfg(feature = "unwind")]140fn create_systemv_cie(&self) -> Option<gimli::write::CommonInformationEntry> {141Some(inst::unwind::systemv::create_cie())142}143144#[cfg(feature = "unwind")]145fn map_regalloc_reg_to_dwarf(&self, reg: Reg) -> Result<u16, RegisterMappingError> {146inst::unwind::systemv::map_reg(reg).map(|reg| reg.0)147}148149fn text_section_builder(&self, num_funcs: usize) -> Box<dyn TextSectionBuilder> {150Box::new(MachTextSectionBuilder::<inst::Inst>::new(num_funcs))151}152153fn function_alignment(&self) -> FunctionAlignment {154inst::Inst::function_alignment()155}156157fn page_size_align_log2(&self) -> u8 {158debug_assert_eq!(1 << 12, 0x1000);15912160}161162#[cfg(feature = "disas")]163fn to_capstone(&self) -> Result<capstone::Capstone, capstone::Error> {164use capstone::prelude::*;165let mut cs = Capstone::new()166.sysz()167.mode(arch::sysz::ArchMode::Default)168.build()?;169170cs.set_skipdata(true)?;171172Ok(cs)173}174175fn pretty_print_reg(&self, reg: Reg, _size: u8) -> String {176inst::regs::pretty_print_reg(reg)177}178179fn has_native_fma(&self) -> bool {180true181}182183fn has_round(&self) -> bool {184true185}186187fn has_blendv_lowering(&self, _: Type) -> bool {188self.isa_flags.has_vxrs_ext3()189}190191fn has_x86_pshufb_lowering(&self) -> bool {192false193}194195fn has_x86_pmulhrsw_lowering(&self) -> bool {196false197}198199fn has_x86_pmaddubsw_lowering(&self) -> bool {200false201}202203fn default_argument_extension(&self) -> ir::ArgumentExtension {204// This is copied/carried over from a historical piece of code in205// Wasmtime:206//207// https://github.com/bytecodealliance/wasmtime/blob/a018a5a9addb77d5998021a0150192aa955c71bf/crates/cranelift/src/lib.rs#L366-L374208//209// Whether or not it is still applicable here is unsure, but it's left210// the same as-is for now to reduce the likelihood of problems arising.211ir::ArgumentExtension::Uext212}213}214215impl fmt::Display for S390xBackend {216fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {217f.debug_struct("MachBackend")218.field("name", &self.name())219.field("triple", &self.triple())220.field("flags", &format!("{}", self.flags()))221.finish()222}223}224225/// Create a new `isa::Builder`.226pub fn isa_builder(triple: Triple) -> IsaBuilder {227assert!(triple.architecture == Architecture::S390x);228IsaBuilder {229triple,230setup: s390x_settings::builder(),231constructor: |triple, shared_flags, builder| {232let isa_flags = s390x_settings::Flags::new(&shared_flags, builder);233let backend = S390xBackend::new_with_flags(triple, shared_flags, isa_flags);234Ok(backend.wrapped())235},236}237}238239240