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 core::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::CodegenResult;
50 use crate::ir::{self, Function, Type};
51 #[cfg(feature = "unwind")]
52 use crate::isa::unwind::{UnwindInfoKind, systemv::RegisterMappingError};
53 use crate::machinst::{CompiledCodeStencil, TextSectionBuilder};
54 use crate::settings;
55 use crate::settings::Configurable;
56 use crate::settings::SetResult;
57 use crate::{Reg, flowgraph};
58 use alloc::string::String;
59 use alloc::{boxed::Box, sync::Arc, vec::Vec};
60 use core::fmt;
61 use core::fmt::{Debug, Formatter};
62 use cranelift_control::ControlPlane;
63 use target_lexicon::{Architecture, PointerWidth, Triple, 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`.
lookup(triple: Triple) -> Result<Builder, LookupError>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`.
lookup_by_name(name: &str) -> Result<Builder, LookupError>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 core::error::Error for LookupError {}
150 
151 impl fmt::Display for LookupError {
fmt(&self, f: &mut Formatter) -> fmt::Result152     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.
new( triple: Triple, setup: settings::Builder, constructor: fn(Triple, settings::Flags, &settings::Builder) -> T, ) -> Self181     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.
from_target_isa(target_isa: &dyn TargetIsa) -> Builder195     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.
triple(&self) -> &Triple210     pub fn triple(&self) -> &Triple {
211         &self.triple
212     }
213 
214     /// Iterates the available settings in the builder.
iter(&self) -> impl Iterator<Item = settings::Setting> + use<T>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.
finish(&self, shared_flags: settings::Flags) -> T225     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> {
set(&mut self, name: &str, value: &str) -> SetResult<()>231     fn set(&mut self, name: &str, value: &str) -> SetResult<()> {
232         self.setup.set(name, value)
233     }
234 
enable(&mut self, name: &str) -> SetResult<()>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.
pointer_type(self) -> ir::Type266     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.
pointer_bits(self) -> u8271     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.
pointer_bytes(self) -> u8276     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.
name(&self) -> &'static str286     fn name(&self) -> &'static str;
287 
288     /// Get the target triple that was used to make this trait object.
triple(&self) -> &Triple289     fn triple(&self) -> &Triple;
290 
291     /// Get the ISA-independent flags that were used to make this trait object.
flags(&self) -> &settings::Flags292     fn flags(&self) -> &settings::Flags;
293 
294     /// Get the ISA-dependent flag values that were used to make this trait object.
isa_flags(&self) -> Vec<settings::Value>295     fn isa_flags(&self) -> Vec<settings::Value>;
296 
297     /// Get the ISA-dependent flag values as raw bytes for hashing.
isa_flags_hash_key(&self) -> IsaFlagsHashKey<'_>298     fn isa_flags_hash_key(&self) -> IsaFlagsHashKey<'_>;
299 
300     /// Get a flag indicating whether branch protection is enabled.
is_branch_protection_enabled(&self) -> bool301     fn is_branch_protection_enabled(&self) -> bool {
302         false
303     }
304 
305     /// Get the ISA-dependent maximum vector register size, in bytes.
dynamic_vector_bytes(&self, dynamic_ty: ir::Type) -> u32306     fn dynamic_vector_bytes(&self, dynamic_ty: ir::Type) -> u32;
307 
308     /// Compile the given function.
compile_function( &self, func: &Function, domtree: &DominatorTree, want_disasm: bool, ctrl_plane: &mut ControlPlane, ) -> CodegenResult<CompiledCodeStencil>309     fn compile_function(
310         &self,
311         func: &Function,
312         domtree: &DominatorTree,
313         want_disasm: bool,
314         ctrl_plane: &mut ControlPlane,
315     ) -> CodegenResult<CompiledCodeStencil>;
316 
317     #[cfg(feature = "unwind")]
318     /// Map a regalloc::Reg to its corresponding DWARF register.
map_regalloc_reg_to_dwarf( &self, _: crate::machinst::Reg, ) -> Result<u16, RegisterMappingError>319     fn map_regalloc_reg_to_dwarf(
320         &self,
321         _: crate::machinst::Reg,
322     ) -> Result<u16, RegisterMappingError> {
323         Err(RegisterMappingError::UnsupportedArchitecture)
324     }
325 
326     /// Creates unwind information for the function.
327     ///
328     /// Returns `None` if there is no unwind information for the function.
329     #[cfg(feature = "unwind")]
emit_unwind_info( &self, result: &crate::machinst::CompiledCode, kind: UnwindInfoKind, ) -> CodegenResult<Option<crate::isa::unwind::UnwindInfo>>330     fn emit_unwind_info(
331         &self,
332         result: &crate::machinst::CompiledCode,
333         kind: UnwindInfoKind,
334     ) -> CodegenResult<Option<crate::isa::unwind::UnwindInfo>>;
335 
336     /// Creates a new System V Common Information Entry for the ISA.
337     ///
338     /// Returns `None` if the ISA does not support System V unwind information.
339     #[cfg(feature = "unwind")]
create_systemv_cie(&self) -> Option<gimli::write::CommonInformationEntry>340     fn create_systemv_cie(&self) -> Option<gimli::write::CommonInformationEntry> {
341         // By default, an ISA cannot create a System V CIE
342         None
343     }
344 
345     /// Returns an object that can be used to build the text section of an
346     /// executable.
347     ///
348     /// This object will internally attempt to handle as many relocations as
349     /// possible using relative calls/jumps/etc between functions.
350     ///
351     /// The `num_labeled_funcs` argument here is the number of functions which
352     /// will be "labeled" or might have calls between them, typically the number
353     /// of defined functions in the object file.
text_section_builder(&self, num_labeled_funcs: usize) -> Box<dyn TextSectionBuilder>354     fn text_section_builder(&self, num_labeled_funcs: usize) -> Box<dyn TextSectionBuilder>;
355 
356     /// Returns the minimum function alignment and the preferred function
357     /// alignment, for performance, required by this ISA.
function_alignment(&self) -> FunctionAlignment358     fn function_alignment(&self) -> FunctionAlignment;
359 
360     /// The log2 of the target's page size and alignment.
361     ///
362     /// Note that this may be an upper-bound that is larger than necessary for
363     /// some platforms since it may depend on runtime configuration.
page_size_align_log2(&self) -> u8364     fn page_size_align_log2(&self) -> u8;
365 
366     /// Create a polymorphic TargetIsa from this specific implementation.
wrapped(self) -> OwnedTargetIsa where Self: Sized + 'static,367     fn wrapped(self) -> OwnedTargetIsa
368     where
369         Self: Sized + 'static,
370     {
371         Arc::new(self)
372     }
373 
374     /// Generate a `Capstone` context for disassembling bytecode for this architecture.
375     #[cfg(feature = "disas")]
to_capstone(&self) -> Result<capstone::Capstone, capstone::Error>376     fn to_capstone(&self) -> Result<capstone::Capstone, capstone::Error> {
377         Err(capstone::Error::UnsupportedArch)
378     }
379 
380     /// Return the string representation of "reg" accessed as "size" bytes.
381     /// The returned string will match the usual disassemly view of "reg".
pretty_print_reg(&self, reg: Reg, size: u8) -> String382     fn pretty_print_reg(&self, reg: Reg, size: u8) -> String;
383 
384     /// Returns whether this ISA has a native fused-multiply-and-add instruction
385     /// for floats.
386     ///
387     /// Currently this only returns false on x86 when some native features are
388     /// not detected.
has_native_fma(&self) -> bool389     fn has_native_fma(&self) -> bool;
390 
391     /// Returns whether this ISA has instructions for `ceil`, `floor`, etc.
has_round(&self) -> bool392     fn has_round(&self) -> bool;
393 
394     /// Returns whether the CLIF `blendv` instruction is implemented for
395     /// this ISA for the specified type.
has_blendv_lowering(&self, ty: Type) -> bool396     fn has_blendv_lowering(&self, ty: Type) -> bool;
397 
398     /// Returns whether the CLIF `x86_pshufb` instruction is implemented for
399     /// this ISA.
has_x86_pshufb_lowering(&self) -> bool400     fn has_x86_pshufb_lowering(&self) -> bool;
401 
402     /// Returns whether the CLIF `x86_pmulhrsw` instruction is implemented for
403     /// this ISA.
has_x86_pmulhrsw_lowering(&self) -> bool404     fn has_x86_pmulhrsw_lowering(&self) -> bool;
405 
406     /// Returns whether the CLIF `x86_pmaddubsw` instruction is implemented for
407     /// this ISA.
has_x86_pmaddubsw_lowering(&self) -> bool408     fn has_x86_pmaddubsw_lowering(&self) -> bool;
409 
410     /// Returns the mode of extension used for integer arguments smaller than
411     /// the pointer width in function signatures.
412     ///
413     /// Some platform ABIs require that smaller-than-pointer-width values are
414     /// either zero or sign-extended to the full register width. This value is
415     /// propagated to the `AbiParam` value created for signatures. Note that not
416     /// all ABIs for all platforms require extension of any form, so this is
417     /// generally only necessary for the `default_call_conv`.
default_argument_extension(&self) -> ir::ArgumentExtension418     fn default_argument_extension(&self) -> ir::ArgumentExtension;
419 }
420 
421 /// A wrapper around the ISA-dependent flags types which only implements `Hash`.
422 #[derive(Hash)]
423 pub struct IsaFlagsHashKey<'a>(&'a [u8]);
424 
425 /// Function alignment specifications as required by an ISA, returned by
426 /// [`TargetIsa::function_alignment`].
427 #[derive(Copy, Clone)]
428 pub struct FunctionAlignment {
429     /// The minimum alignment required by an ISA, where all functions must be
430     /// aligned to at least this amount.
431     pub minimum: u32,
432     /// A "preferred" alignment which should be used for more
433     /// performance-sensitive situations. This can involve cache-line-aligning
434     /// for example to get more of a small function into fewer cache lines.
435     pub preferred: u32,
436 }
437 
438 /// Methods implemented for free for target ISA!
439 impl<'a> dyn TargetIsa + 'a {
440     /// Get the default calling convention of this target.
default_call_conv(&self) -> CallConv441     pub fn default_call_conv(&self) -> CallConv {
442         CallConv::triple_default(self.triple())
443     }
444 
445     /// Get the endianness of this ISA.
endianness(&self) -> ir::Endianness446     pub fn endianness(&self) -> ir::Endianness {
447         match self.triple().endianness().unwrap() {
448             target_lexicon::Endianness::Little => ir::Endianness::Little,
449             target_lexicon::Endianness::Big => ir::Endianness::Big,
450         }
451     }
452 
453     /// Returns the minimum symbol alignment for this ISA.
symbol_alignment(&self) -> u64454     pub fn symbol_alignment(&self) -> u64 {
455         match self.triple().architecture {
456             // All symbols need to be aligned to at least 2 on s390x.
457             Architecture::S390x => 2,
458             _ => 1,
459         }
460     }
461 
462     /// Get the pointer type of this ISA.
pointer_type(&self) -> ir::Type463     pub fn pointer_type(&self) -> ir::Type {
464         ir::Type::int(self.pointer_bits() as u16).unwrap()
465     }
466 
467     /// Get the width of pointers on this ISA.
pointer_width(&self) -> PointerWidth468     pub(crate) fn pointer_width(&self) -> PointerWidth {
469         self.triple().pointer_width().unwrap()
470     }
471 
472     /// Get the width of pointers on this ISA, in units of bits.
pointer_bits(&self) -> u8473     pub fn pointer_bits(&self) -> u8 {
474         self.pointer_width().bits()
475     }
476 
477     /// Get the width of pointers on this ISA, in units of bytes.
pointer_bytes(&self) -> u8478     pub fn pointer_bytes(&self) -> u8 {
479         self.pointer_width().bytes()
480     }
481 
482     /// Get the information needed by frontends producing Cranelift IR.
frontend_config(&self) -> TargetFrontendConfig483     pub fn frontend_config(&self) -> TargetFrontendConfig {
484         TargetFrontendConfig {
485             default_call_conv: self.default_call_conv(),
486             pointer_width: self.pointer_width(),
487             page_size_align_log2: self.page_size_align_log2(),
488         }
489     }
490 }
491 
492 impl Debug for &dyn TargetIsa {
fmt(&self, f: &mut Formatter<'_>) -> fmt::Result493     fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
494         write!(
495             f,
496             "TargetIsa {{ triple: {:?}, pointer_width: {:?}}}",
497             self.triple(),
498             self.pointer_width()
499         )
500     }
501 }
502