1 //! A Cranelift-specific x64 assembler. 2 //! 3 //! All instructions known to this assembler are listed in the [`inst`] module. 4 //! The [`Inst`] enumeration contains a variant for each, allowing matching over 5 //! all these instructions. All of this is parameterized by a [`Registers`] 6 //! trait, allowing users of this assembler to plug in their own register types. 7 //! 8 //! ``` 9 //! # use cranelift_assembler_x64::{Fixed, Imm8, inst, Inst, Registers}; 10 //! // Tell the assembler the type of registers we're using; we can always 11 //! // encode a HW register as a `u8` (e.g., `eax = 0`). 12 //! pub struct Regs; 13 //! impl Registers for Regs { 14 //! type ReadGpr = u8; 15 //! type ReadWriteGpr = u8; 16 //! type WriteGpr = u8; 17 //! type ReadXmm = u8; 18 //! type ReadWriteXmm = u8; 19 //! type WriteXmm = u8; 20 //! } 21 //! 22 //! // Then, build one of the `AND` instructions; this one operates on an 23 //! // implicit `AL` register with an immediate. We can collect a sequence of 24 //! // instructions by converting to the `Inst` type. 25 //! let rax: u8 = 0; 26 //! let and = inst::andb_i::new(Fixed(rax), Imm8::new(0b10101010)); 27 //! let seq: Vec<Inst<Regs>> = vec![and.into()]; 28 //! 29 //! // Now we can encode this sequence into a code buffer. 30 //! let mut buffer = vec![]; 31 //! for inst in seq { 32 //! inst.encode(&mut buffer); 33 //! } 34 //! assert_eq!(buffer, vec![0x24, 0b10101010]); 35 //! ``` 36 //! 37 //! With an [`Inst`], we can encode the instruction into a code buffer; see the 38 //! [example](Inst). 39 40 #![allow( 41 non_camel_case_types, 42 reason = "all of the generated struct names use snake case" 43 )] 44 #![no_std] 45 extern crate alloc; 46 #[cfg(any(test, feature = "fuzz"))] 47 extern crate std; 48 49 mod api; 50 mod custom; 51 mod evex; 52 mod features; 53 mod fixed; 54 pub mod gpr; 55 mod imm; 56 pub mod inst; 57 mod mem; 58 mod rex; 59 mod vex; 60 pub mod xmm; 61 62 #[cfg(any(test, feature = "fuzz"))] 63 pub mod fuzz; 64 65 /// An assembly instruction; contains all instructions known to the assembler. 66 /// 67 /// This wraps all [`inst`] structures into a single enumeration for collecting 68 /// instructions. 69 #[doc(inline)] 70 // This re-exports, and documents, a module that is more convenient to use at 71 // the library top-level. 72 pub use inst::Inst; 73 74 pub use api::{ 75 AsReg, CodeSink, Constant, KnownOffset, Label, RegisterVisitor, Registers, TrapCode, 76 }; 77 pub use features::{AvailableFeatures, Feature, Features}; 78 pub use fixed::Fixed; 79 pub use gpr::{Gpr, NonRspGpr, Size}; 80 pub use imm::{Extension, Imm8, Imm16, Imm32, Imm64, Simm8, Simm16, Simm32}; 81 pub use mem::{ 82 Amode, AmodeOffset, AmodeOffsetPlusKnownOffset, DeferredTarget, GprMem, Scale, XmmMem, 83 }; 84 pub use rex::RexPrefix; 85 pub use xmm::Xmm; 86