Expand description
Typed SSA-style p-code IR for binary analysis.
qcode models the semantics of lifted machine code. It is inspired by
Ghidra’s p-code, with additional first-class values for instructions, basic
blocks, functions, and literals. The crate contains the IR, its builder,
the QCode text-format lowering API, and integrity checks. Optimization and
execution live separately: see qcode_passes for block-local cleanup,
qcode_emulator to interpret QCode, and qcode_vm to run a guest
program under an MMU.
§Getting started
A Context owns a QCode module. Create one, then construct IR with a
Builder or parse QCode source through lower::lower_str.
use qcode::context::Context;
let _context = Context::new();§Core concepts
- A
Spaceis a uniformly addressed memory region, such as RAM or a register file. - A
ValueIdidentifies every IR value: literals, SSA instructions, varnodes, blocks, and functions. - A
FunctionBodyowns a function’s instructions, blocks, and block parameters; module-wide values are owned by theContext. - A
Builderemits instructions and constructs control flow in a block.
Reference types such as InstructionRef, BlockRef, and
FunctionRef borrow their owning context, so they cannot outlive the IR
arena.
§QCode source
lower::lower_str parses QCode source at runtime. For source literals,
the re-exported qcode! macro performs the same lowering and binds names
declared in the source into the surrounding Rust scope.
Modules§
- address_
index - Disposable address lookup over an immutable qcode module snapshot.
- assumption
- Heuristic assumptions and proven knowledge shared by analysis passes.
- builder
- Fluent IR builder: emit instructions into a
BasicBlock. - context
- The central arena for all IR state:
Context. - discovery
- Typed queue of code addresses discovered during lifting/analysis but not yet lifted into the IR.
- error
- intrinsics
- Built-in pure intrinsics.
- lower
- Runtime lowering of parsed QCode text into qcode IR.
- memory_
image - A serializable snapshot of a binary’s initialized memory.
- obligation
- Stable identity for a reconstruction obligation: an unresolved control transfer that reconstruction still owes an answer for.
- pass_
scope - Thread-local identity of the currently-running analysis pass.
- space
- Memory spaces: uniformly-addressed regions that varnodes live in.
- types
- First-class type system for qcode IR values.
- value
- IR value types and the central
ValueIddiscriminant.
Macros§
- composite_
id - Declares a composite IR ID: a
{ func: FunctionId, local: LocalX }pair. - pass_
log - Log a message attributed to the current pass: the
logtarget is the pass name (soRUST_LOG=mem2reg=debugfilters per pass) and the message is prefixed with it. - qcode
- register_
intrinsic - Register a built-in intrinsic with the global registry.
- stat
- Add to a named counter of the current pass; the driver aggregates and logs
them per round.
stat!("slots_promoted")increments by 1.
Functions§
- verify_
body_ arena_ integrity - Validate the ownership and cross-reference invariants of every function body’s block, instruction, parameter, CFG-edge, and temporary arenas.
- verify_
body_ arena_ integrity_ scoped verify_body_arena_integrity, restricted to the functions inscope(Nonemeans every function). Arena invariants are strictly per-body, so a caller that knows which functions changed can skip the rest.