1 //! Instruction Set Architectures. 2 //! 3 //! The `isa` module provides a `TargetIsa` trait which provides the behavior specialization needed 4 //! by the ISA-independent code generator. The sub-modules of this module provide definitions for 5 //! the instruction sets that Cranelift can target. Each sub-module has it's own implementation of 6 //! `TargetIsa`. 7 //! 8 //! # Constructing a `TargetIsa` instance 9 //! 10 //! The target ISA is built from the following information: 11 //! 12 //! - The name of the target ISA as a string. Cranelift is a cross-compiler, so the ISA to target 13 //! can be selected dynamically. Individual ISAs can be left out when Cranelift is compiled, so a 14 //! string is used to identify the proper sub-module. 15 //! - Values for settings that apply to all ISAs. This is represented by a `settings::Flags` 16 //! instance. 17 //! - Values for ISA-specific settings. 18 //! 19 //! The `isa::lookup()` function is the main entry point which returns an `isa::Builder` 20 //! appropriate for the requested ISA: 21 //! 22 //! ``` 23 //! # #[macro_use] extern crate target_lexicon; 24 //! use cranelift_codegen::isa; 25 //! use cranelift_codegen::settings::{self, Configurable}; 26 //! use std::str::FromStr; 27 //! use target_lexicon::Triple; 28 //! 29 //! let shared_builder = settings::builder(); 30 //! let shared_flags = settings::Flags::new(shared_builder); 31 //! 32 //! match isa::lookup(triple!("x86_64")) { 33 //! Err(_) => { 34 //! // The x86_64 target ISA is not available. 35 //! } 36 //! Ok(mut isa_builder) => { 37 //! isa_builder.set("use_popcnt", "on"); 38 //! let isa = isa_builder.finish(shared_flags); 39 //! } 40 //! } 41 //! ``` 42 //! 43 //! The configured target ISA trait object is a `Box<TargetIsa>` which can be used for multiple 44 //! concurrent function compilations. 45 46 use crate::dominator_tree::DominatorTree; 47 pub use crate::isa::call_conv::CallConv; 48 49 use crate::ir::{self, Function, Type}; 50 #[cfg(feature = "unwind")] 51 use crate::isa::unwind::{systemv::RegisterMappingError, UnwindInfoKind}; 52 use crate::machinst::{CompiledCode, CompiledCodeStencil, TextSectionBuilder}; 53 use crate::settings; 54 use crate::settings::Configurable; 55 use crate::settings::SetResult; 56 use crate::CodegenResult; 57 use crate::{flowgraph, Reg}; 58 use alloc::{boxed::Box, sync::Arc, vec::Vec}; 59 use core::fmt; 60 use core::fmt::{Debug, Formatter}; 61 use cranelift_control::ControlPlane; 62 use std::string::String; 63 use target_lexicon::{triple, Architecture, PointerWidth, Triple}; 64 65 // This module is made public here for benchmarking purposes. No guarantees are 66 // made regarding API stability. 67 #[cfg(feature = "x86")] 68 pub mod x64; 69 70 #[cfg(feature = "arm64")] 71 pub mod aarch64; 72 73 #[cfg(feature = "riscv64")] 74 pub mod riscv64; 75 76 #[cfg(feature = "s390x")] 77 mod s390x; 78 79 #[cfg(feature = "pulley")] 80 mod pulley32; 81 #[cfg(feature = "pulley")] 82 mod pulley64; 83 #[cfg(feature = "pulley")] 84 mod pulley_shared; 85 86 pub mod unwind; 87 88 mod call_conv; 89 mod winch; 90 91 /// Returns a builder that can create a corresponding `TargetIsa` 92 /// or `Err(LookupError::SupportDisabled)` if not enabled. 93 macro_rules! isa_builder { 94 ($name: ident, $cfg_terms: tt, $triple: ident) => {{ 95 #[cfg $cfg_terms] 96 { 97 Ok($name::isa_builder($triple)) 98 } 99 #[cfg(not $cfg_terms)] 100 { 101 Err(LookupError::SupportDisabled) 102 } 103 }}; 104 } 105 106 /// Look for an ISA for the given `triple`. 107 /// Return a builder that can create a corresponding `TargetIsa`. 108 pub fn lookup(triple: Triple) -> Result<Builder, LookupError> { 109 match triple.architecture { 110 Architecture::X86_64 => { 111 isa_builder!(x64, (feature = "x86"), triple) 112 } 113 Architecture::Aarch64 { .. } => isa_builder!(aarch64, (feature = "arm64"), triple), 114 Architecture::S390x { .. } => isa_builder!(s390x, (feature = "s390x"), triple), 115 Architecture::Riscv64 { .. } => isa_builder!(riscv64, (feature = "riscv64"), triple), 116 Architecture::Pulley32 | Architecture::Pulley32be => { 117 isa_builder!(pulley32, (feature = "pulley"), triple) 118 } 119 Architecture::Pulley64 | Architecture::Pulley64be => { 120 isa_builder!(pulley64, (feature = "pulley"), triple) 121 } 122 _ => Err(LookupError::Unsupported), 123 } 124 } 125 126 /// The string names of all the supported, but possibly not enabled, architectures. The elements of 127 /// this slice are suitable to be passed to the [lookup_by_name] function to obtain the default 128 /// configuration for that architecture. 129 pub const ALL_ARCHITECTURES: &[&str] = &["x86_64", "aarch64", "s390x", "riscv64"]; 130 131 /// Look for a supported ISA with the given `name`. 132 /// Return a builder that can create a corresponding `TargetIsa`. 133 pub fn lookup_by_name(name: &str) -> Result<Builder, LookupError> { 134 lookup(triple!(name)) 135 } 136 137 /// Describes reason for target lookup failure 138 #[derive(PartialEq, Eq, Copy, Clone, Debug)] 139 pub enum LookupError { 140 /// Support for this target was disabled in the current build. 141 SupportDisabled, 142 143 /// Support for this target has not yet been implemented. 144 Unsupported, 145 } 146 147 // This is manually implementing Error and Display instead of using thiserror to reduce the amount 148 // of dependencies used by Cranelift. 149 impl std::error::Error for LookupError {} 150 151 impl fmt::Display for LookupError { 152 fn fmt(&self, f: &mut Formatter) -> fmt::Result { 153 match self { 154 LookupError::SupportDisabled => write!(f, "Support for this target is disabled"), 155 LookupError::Unsupported => { 156 write!(f, "Support for this target has not been implemented yet") 157 } 158 } 159 } 160 } 161 162 /// The type of a polymorphic TargetISA object which is 'static. 163 pub type OwnedTargetIsa = Arc<dyn TargetIsa>; 164 165 /// Type alias of `IsaBuilder` used for building Cranelift's ISAs. 166 pub type Builder = IsaBuilder<CodegenResult<OwnedTargetIsa>>; 167 168 /// Builder for a `TargetIsa`. 169 /// Modify the ISA-specific settings before creating the `TargetIsa` trait object with `finish`. 170 #[derive(Clone)] 171 pub struct IsaBuilder<T> { 172 triple: Triple, 173 setup: settings::Builder, 174 constructor: fn(Triple, settings::Flags, &settings::Builder) -> T, 175 } 176 177 impl<T> IsaBuilder<T> { 178 /// Creates a new ISA-builder from its components, namely the `triple` for 179 /// the ISA, the ISA-specific settings builder, and a final constructor 180 /// function to generate the ISA from its components. 181 pub fn new( 182 triple: Triple, 183 setup: settings::Builder, 184 constructor: fn(Triple, settings::Flags, &settings::Builder) -> T, 185 ) -> Self { 186 IsaBuilder { 187 triple, 188 setup, 189 constructor, 190 } 191 } 192 193 /// Creates a new [Builder] from a [TargetIsa], copying all flags in the 194 /// process. 195 pub fn from_target_isa(target_isa: &dyn TargetIsa) -> Builder { 196 // We should always be able to find the builder for the TargetISA, since presumably we 197 // also generated the previous TargetISA at some point 198 let triple = target_isa.triple().clone(); 199 let mut builder = self::lookup(triple).expect("Could not find triple for target ISA"); 200 201 // Copy ISA Flags 202 for flag in target_isa.isa_flags() { 203 builder.set(&flag.name, &flag.value_string()).unwrap(); 204 } 205 206 builder 207 } 208 209 /// Gets the triple for the builder. 210 pub fn triple(&self) -> &Triple { 211 &self.triple 212 } 213 214 /// Iterates the available settings in the builder. 215 pub fn iter(&self) -> impl Iterator<Item = settings::Setting> + use<T> { 216 self.setup.iter() 217 } 218 219 /// Combine the ISA-specific settings with the provided 220 /// ISA-independent settings and allocate a fully configured 221 /// `TargetIsa` trait object. May return an error if some of the 222 /// flags are inconsistent or incompatible: for example, some 223 /// platform-independent features, like general SIMD support, may 224 /// need certain ISA extensions to be enabled. 225 pub fn finish(&self, shared_flags: settings::Flags) -> T { 226 (self.constructor)(self.triple.clone(), shared_flags, &self.setup) 227 } 228 } 229 230 impl<T> settings::Configurable for IsaBuilder<T> { 231 fn set(&mut self, name: &str, value: &str) -> SetResult<()> { 232 self.setup.set(name, value) 233 } 234 235 fn enable(&mut self, name: &str) -> SetResult<()> { 236 self.setup.enable(name) 237 } 238 } 239 240 /// After determining that an instruction doesn't have an encoding, how should we proceed to 241 /// legalize it? 242 /// 243 /// The `Encodings` iterator returns a legalization function to call. 244 pub type Legalize = 245 fn(ir::Inst, &mut ir::Function, &mut flowgraph::ControlFlowGraph, &dyn TargetIsa) -> bool; 246 247 /// This struct provides information that a frontend may need to know about a target to 248 /// produce Cranelift IR for the target. 249 #[derive(Clone, Copy, Hash)] 250 pub struct TargetFrontendConfig { 251 /// The default calling convention of the target. 252 pub default_call_conv: CallConv, 253 254 /// The pointer width of the target. 255 pub pointer_width: PointerWidth, 256 257 /// The log2 of the target's page size and alignment. 258 /// 259 /// Note that this may be an upper-bound that is larger than necessary for 260 /// some platforms since it may depend on runtime configuration. 261 pub page_size_align_log2: u8, 262 } 263 264 impl TargetFrontendConfig { 265 /// Get the pointer type of this target. 266 pub fn pointer_type(self) -> ir::Type { 267 ir::Type::int(self.pointer_bits() as u16).unwrap() 268 } 269 270 /// Get the width of pointers on this target, in units of bits. 271 pub fn pointer_bits(self) -> u8 { 272 self.pointer_width.bits() 273 } 274 275 /// Get the width of pointers on this target, in units of bytes. 276 pub fn pointer_bytes(self) -> u8 { 277 self.pointer_width.bytes() 278 } 279 } 280 281 /// Methods that are specialized to a target ISA. 282 /// 283 /// Implies a Display trait that shows the shared flags, as well as any ISA-specific flags. 284 pub trait TargetIsa: fmt::Display + Send + Sync { 285 /// Get the name of this ISA. 286 fn name(&self) -> &'static str; 287 288 /// Get the target triple that was used to make this trait object. 289 fn triple(&self) -> &Triple; 290 291 /// Get the ISA-independent flags that were used to make this trait object. 292 fn flags(&self) -> &settings::Flags; 293 294 /// Get the ISA-dependent flag values that were used to make this trait object. 295 fn isa_flags(&self) -> Vec<settings::Value>; 296 297 /// Get a flag indicating whether branch protection is enabled. 298 fn is_branch_protection_enabled(&self) -> bool { 299 false 300 } 301 302 /// Get the ISA-dependent maximum vector register size, in bytes. 303 fn dynamic_vector_bytes(&self, dynamic_ty: ir::Type) -> u32; 304 305 /// Compile the given function. 306 fn compile_function( 307 &self, 308 func: &Function, 309 domtree: &DominatorTree, 310 want_disasm: bool, 311 ctrl_plane: &mut ControlPlane, 312 ) -> CodegenResult<CompiledCodeStencil>; 313 314 #[cfg(feature = "unwind")] 315 /// Map a regalloc::Reg to its corresponding DWARF register. 316 fn map_regalloc_reg_to_dwarf( 317 &self, 318 _: crate::machinst::Reg, 319 ) -> Result<u16, RegisterMappingError> { 320 Err(RegisterMappingError::UnsupportedArchitecture) 321 } 322 323 /// Creates unwind information for the function. 324 /// 325 /// Returns `None` if there is no unwind information for the function. 326 #[cfg(feature = "unwind")] 327 fn emit_unwind_info( 328 &self, 329 result: &CompiledCode, 330 kind: UnwindInfoKind, 331 ) -> CodegenResult<Option<crate::isa::unwind::UnwindInfo>>; 332 333 /// Creates a new System V Common Information Entry for the ISA. 334 /// 335 /// Returns `None` if the ISA does not support System V unwind information. 336 #[cfg(feature = "unwind")] 337 fn create_systemv_cie(&self) -> Option<gimli::write::CommonInformationEntry> { 338 // By default, an ISA cannot create a System V CIE 339 None 340 } 341 342 /// Returns an object that can be used to build the text section of an 343 /// executable. 344 /// 345 /// This object will internally attempt to handle as many relocations as 346 /// possible using relative calls/jumps/etc between functions. 347 /// 348 /// The `num_labeled_funcs` argument here is the number of functions which 349 /// will be "labeled" or might have calls between them, typically the number 350 /// of defined functions in the object file. 351 fn text_section_builder(&self, num_labeled_funcs: usize) -> Box<dyn TextSectionBuilder>; 352 353 /// Returns the minimum function alignment and the preferred function 354 /// alignment, for performance, required by this ISA. 355 fn function_alignment(&self) -> FunctionAlignment; 356 357 /// The log2 of the target's page size and alignment. 358 /// 359 /// Note that this may be an upper-bound that is larger than necessary for 360 /// some platforms since it may depend on runtime configuration. 361 fn page_size_align_log2(&self) -> u8; 362 363 /// Create a polymorphic TargetIsa from this specific implementation. 364 fn wrapped(self) -> OwnedTargetIsa 365 where 366 Self: Sized + 'static, 367 { 368 Arc::new(self) 369 } 370 371 /// Generate a `Capstone` context for disassembling bytecode for this architecture. 372 #[cfg(feature = "disas")] 373 fn to_capstone(&self) -> Result<capstone::Capstone, capstone::Error> { 374 Err(capstone::Error::UnsupportedArch) 375 } 376 377 /// Return the string representation of "reg" accessed as "size" bytes. 378 /// The returned string will match the usual disassemly view of "reg". 379 fn pretty_print_reg(&self, reg: Reg, size: u8) -> String; 380 381 /// Returns whether this ISA has a native fused-multiply-and-add instruction 382 /// for floats. 383 /// 384 /// Currently this only returns false on x86 when some native features are 385 /// not detected. 386 fn has_native_fma(&self) -> bool; 387 388 /// Returns whether the CLIF `x86_blendv` instruction is implemented for 389 /// this ISA for the specified type. 390 fn has_x86_blendv_lowering(&self, ty: Type) -> bool; 391 392 /// Returns whether the CLIF `x86_pshufb` instruction is implemented for 393 /// this ISA. 394 fn has_x86_pshufb_lowering(&self) -> bool; 395 396 /// Returns whether the CLIF `x86_pmulhrsw` instruction is implemented for 397 /// this ISA. 398 fn has_x86_pmulhrsw_lowering(&self) -> bool; 399 400 /// Returns whether the CLIF `x86_pmaddubsw` instruction is implemented for 401 /// this ISA. 402 fn has_x86_pmaddubsw_lowering(&self) -> bool; 403 404 /// Returns the mode of extension used for integer arguments smaller than 405 /// the pointer width in function signatures. 406 /// 407 /// Some platform ABIs require that smaller-than-pointer-width values are 408 /// either zero or sign-extended to the full register width. This value is 409 /// propagated to the `AbiParam` value created for signatures. Note that not 410 /// all ABIs for all platforms require extension of any form, so this is 411 /// generally only necessary for the `default_call_conv`. 412 fn default_argument_extension(&self) -> ir::ArgumentExtension; 413 } 414 415 /// Function alignment specifications as required by an ISA, returned by 416 /// [`TargetIsa::function_alignment`]. 417 #[derive(Copy, Clone)] 418 pub struct FunctionAlignment { 419 /// The minimum alignment required by an ISA, where all functions must be 420 /// aligned to at least this amount. 421 pub minimum: u32, 422 /// A "preferred" alignment which should be used for more 423 /// performance-sensitive situations. This can involve cache-line-aligning 424 /// for example to get more of a small function into fewer cache lines. 425 pub preferred: u32, 426 } 427 428 /// Methods implemented for free for target ISA! 429 impl<'a> dyn TargetIsa + 'a { 430 /// Get the default calling convention of this target. 431 pub fn default_call_conv(&self) -> CallConv { 432 CallConv::triple_default(self.triple()) 433 } 434 435 /// Get the endianness of this ISA. 436 pub fn endianness(&self) -> ir::Endianness { 437 match self.triple().endianness().unwrap() { 438 target_lexicon::Endianness::Little => ir::Endianness::Little, 439 target_lexicon::Endianness::Big => ir::Endianness::Big, 440 } 441 } 442 443 /// Returns the minimum symbol alignment for this ISA. 444 pub fn symbol_alignment(&self) -> u64 { 445 match self.triple().architecture { 446 // All symbols need to be aligned to at least 2 on s390x. 447 Architecture::S390x => 2, 448 _ => 1, 449 } 450 } 451 452 /// Get the pointer type of this ISA. 453 pub fn pointer_type(&self) -> ir::Type { 454 ir::Type::int(self.pointer_bits() as u16).unwrap() 455 } 456 457 /// Get the width of pointers on this ISA. 458 pub(crate) fn pointer_width(&self) -> PointerWidth { 459 self.triple().pointer_width().unwrap() 460 } 461 462 /// Get the width of pointers on this ISA, in units of bits. 463 pub fn pointer_bits(&self) -> u8 { 464 self.pointer_width().bits() 465 } 466 467 /// Get the width of pointers on this ISA, in units of bytes. 468 pub fn pointer_bytes(&self) -> u8 { 469 self.pointer_width().bytes() 470 } 471 472 /// Get the information needed by frontends producing Cranelift IR. 473 pub fn frontend_config(&self) -> TargetFrontendConfig { 474 TargetFrontendConfig { 475 default_call_conv: self.default_call_conv(), 476 pointer_width: self.pointer_width(), 477 page_size_align_log2: self.page_size_align_log2(), 478 } 479 } 480 } 481 482 impl Debug for &dyn TargetIsa { 483 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { 484 write!( 485 f, 486 "TargetIsa {{ triple: {:?}, pointer_width: {:?}}}", 487 self.triple(), 488 self.pointer_width() 489 ) 490 } 491 } 492