Skip to main content

Crate offlineasm_ir

Crate offlineasm_ir 

Source
Expand description

Defines the target-independent IR for offlineasm mnemonics, shared by the offlineasm and onlineasm assemblers.

This crate is the single place where mnemonic algorithms live. Both assemblers run the same front half:

spelling -`Mnemonic`-> `expand()` + `Op`s -> [`Expansion`] -backend-> machine code | asm text
  • Mnemonic names the operation, one variant per operation rather than one per spelling: addp/addq are the same value, and mnem holds one constant per accepted spelling. Mnemonic::from_str is the crate’s only string comparison, and each frontend calls it once per instruction.
  • expand() turns one Mnemonic plus its Ops into core Inst nodes. Multi-instruction sequences (truncatef2q’s bias path, cq2f’s halving path, bcd2i, …) are expanded here once, using only core nodes plus internal labels, so every backend inherits them.
  • Backends implement the core Inst variants with their ISA. Adding a new backend means implementing Inst, never re-deriving 400+ mnemonics. onlineasm’s backends (x86/aarch64/riscv64) emit machine code; offlineasm’s backends emit assembly text.

§Operand order

Mnemonics read destination first: move dst, src, and addq dst, lhs, rhs means dst = lhs + rhs. Branches and tests have no destination, so they keep lhs, rhs, lbl / value, lbl. Sources keep their relative order after the destination.

§Registers

Gpr/Fpr hold architecture physical numbers (x86 rax = 0, aarch64 x0 = 0, riscv x10 = 10, …). Name resolution (offlineasm vreg to number, including the per-arch default maps) lives in regs, so both frontends share one vocabulary.

§Labels and symbols

Label and SymId are opaque handles: each frontend owns a table and interprets the index its own way (onlineasm holds asmkit label handles and offlineasm holds symbol text). The IR never names either.

§Implicit pairs

A few nodes name no explicit register for ISA-implicit operands and use the accumulator convention instead: the dividend/quotient lives in the t0 role, the high half / CAS expected value in the t2 role (x86 rax/rdx, aarch64 x0/x2, riscv x10/x12). Each backend maps the roles to its own registers. User code keeps t0/t2 mapped at their ABI homes (as the default maps do).

§Pending flags

*S ALU nodes (Inst::AluS) and every compare/test leave pending flags. Inst::FlagBranch and Inst::Adc/Inst::Sbb consume them. Nodes from one mnemonic expansion run back-to-back, so the state is always well-defined there. Across mnemonics it matches existing usage (bo/bs/bz/bnz directly after a flag-setting op). RISC-V has no flags: its backend materializes the predicate into a reserved scratch register, which nothing else clobbers.

Re-exports§

pub use expand::aligned_push_bytes;
pub use expand::aligned_push_offset;
pub use expand::check_callee_saved;
pub use expand::expand;
pub use expand::patch_kind;
pub use expand::Expansion;
pub use expand::PatchKind;
pub use mnemonic::mnem;
pub use mnemonic::LeaForm;
pub use mnemonic::Mnemonic;
pub use mnemonic::SUPPORTED_MNEMONICS;
pub use regs::RegInfo;
pub use regs::RegKind;
pub use regs::TargetRegs;
pub use regs::VReg;
pub use regs::TARGETS;
pub use inst::*;

Modules§

expand
Expands mnemonic invocations into Inst sequences.
inst
mnemonic
regs
Maps portable register names to per-target physical registers.
riscv
RISC-V 64 helpers both backends share.

Structs§

Extern
Refers to an external symbol.
ExternId
Identifies an external symbol with a frontend-owned handle.
Fpr
Names a floating-point or vector register by its architecture physical number.
Gpr
Names a general-purpose register by its architecture physical number.
Label
Identifies a code label with a frontend-owned handle.
Mem
Holds a resolved machine address: disp(base, index, scale). Every backend lowers this. RISC backends synthesize the index/scale part with the expansion scratch registers.
Scratch
Supplies the scratch registers an expansion may use.
SymId
Identifies a symbolic operand with a frontend-owned handle.

Enums§

Disp
Holds a memory displacement: a folded constant, a frontend-owned symbol, or the address of a code label.
ExpandError
Reports a mnemonic expansion failure.
FCc
Names a float comparison condition.
FKind
Names a scalar float kind.
IntCc
Names an integer comparison condition.
Lbl
Refers to a code label. Bound comes from the frontend (a concrete handle). Fresh labels are created by expand() for internal sequences and are scoped to one expansion. Backends must resolve them per mnemonic lowering.
Op
Holds one operand of a mnemonic invocation.
PredFlag
Names a pending-flag predicate.
Reach
Says how generated code reaches an external symbol.
RoundMode
Names a float rounding mode.
ShiftOp
Names a shift or rotate operation.
Target
Names a jump or call target.
TestKind
Names a value test.
Width
Names an integer operand width.