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