Crate cranelift_assembler_x64

Source
Expand description

A Cranelift-specific x64 assembler.

All instructions known to this assembler are listed in the inst module. The Inst enumeration contains a variant for each, allowing matching over all these instructions. All of this is parameterized by a Registers trait, allowing users of this assembler to plug in their own register types.

// Tell the assembler the type of registers we're using; we can always
// encode a HW register as a `u8` (e.g., `eax = 0`).
pub struct Regs;
impl Registers for Regs {
    type ReadGpr = u8;
    type ReadWriteGpr = u8;
    type ReadXmm = u8;
    type ReadWriteXmm = u8;
}

// Then, build one of the `AND` instructions; this one operates on an
// implicit `AL` register with an immediate. We can collect a sequence of
// instructions by converting to the `Inst` type.
let and = inst::andb_i::new(Imm8::new(0b10101010));
let seq: Vec<Inst<Regs>> = vec![and.into()];

// Now we can encode this sequence into a code buffer, checking that each
// instruction is valid in 64-bit mode.
let mut buffer = vec![];
let offsets = vec![];
for inst in seq {
    if inst.features().contains(&Feature::_64b) {
        inst.encode(&mut buffer, &offsets);
    }
}
assert_eq!(buffer, vec![0x24, 0b10101010]);

With an Inst, we can encode the instruction into a code buffer; see the example.

Modules§

  • Expose all known instructions as Rust structs; this is generated in build.rs.
  • Expose the ISLE-related code generated in build.rs.

Macros§

Structs§

Enums§

  • x64 memory addressing modes.
  • For RIP-relative addressing, keep track of the CodeSink-specific target.
  • Define the ways an immediate may be sign- or zero-extended.
  • A CPU feature.
  • A general-purpose register or memory operand.
  • An assembly instruction; contains all instructions known to the assembler.
  • The scaling factor for the index register in certain Amodes.
  • A single x64 register encoding can access a different number of bits.
  • An XMM register or memory operand.

Traits§

  • Describe how to interact with an external register type.
  • Describe how an instruction is emitted into a code buffer.
  • A table mapping KnownOffset identifiers to their i32 offset values.
  • Describe a visitor for the register operands of an instruction.
  • A type set fixing the register types used in the assembler.

Functions§

Type Aliases§

  • A KnownOffset is a unique identifier for a specific offset known only at emission time.