Path: blob/main/cranelift/assembler-x64/src/lib.rs
3069 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)]43#![no_std]44extern crate alloc;45#[cfg(any(test, feature = "fuzz"))]46extern crate std;4748mod api;49mod custom;50mod evex;51mod features;52mod fixed;53pub mod gpr;54mod imm;55pub mod inst;56mod mem;57mod rex;58mod vex;59pub mod xmm;6061#[cfg(any(test, feature = "fuzz"))]62pub mod fuzz;6364/// An assembly instruction; contains all instructions known to the assembler.65///66/// This wraps all [`inst`] structures into a single enumeration for collecting67/// instructions.68#[doc(inline)]69// This re-exports, and documents, a module that is more convenient to use at70// the library top-level.71pub use inst::Inst;7273pub use api::{74AsReg, CodeSink, Constant, KnownOffset, Label, RegisterVisitor, Registers, TrapCode,75};76pub use features::{AvailableFeatures, Feature, Features};77pub use fixed::Fixed;78pub use gpr::{Gpr, NonRspGpr, Size};79pub use imm::{Extension, Imm8, Imm16, Imm32, Imm64, Simm8, Simm16, Simm32};80pub use mem::{81Amode, AmodeOffset, AmodeOffsetPlusKnownOffset, DeferredTarget, GprMem, Scale, XmmMem,82};83pub use rex::RexPrefix;84pub use xmm::Xmm;858687