Skip to main content

Module builder

Module builder 

Source
Expand description

Fluent IR builder: emit instructions into a BasicBlock.

The Builder is the primary way to construct IR. It holds a mutable reference to a block inside a Context and exposes typed push_* methods for every instruction kind.

Terminating the block is the caller’s responsibility (Builder::finalize pushes the final branch for the common case); the invariant that every rostered block ends in a terminator is enforced by the IR verifier, not at builder drop. Appending past a terminator, however, panics immediately. A builder may freely be dropped mid-block — e.g. after splicing instructions before an existing anchor via Builder::set_insert_point_before.

§Typical usage

use qcode::context::Context;

let mut ctx = Context::new();

// Create a builder positioned at machine address 0x1000.
let source = ctx.builder_at(0x1000).current_block();
let target = ctx.get_or_make_block(0x1010, source.func);
let b = ctx.builder(source);

// Emit instructions …

// Terminate the block with an unconditional branch to `target`.
// This consumes the builder, so there is no need to call drop explicitly.
b.finalize(target);

§Namespaces

The builder maintains a local namespace: a map from string names to ValueIds. This is used by the qcode! macro and the parser to resolve identifiers within a single block. Names in the namespace do not need to match the IR-level name hints stored on values.

Structs§

Builder
A builder for constructing instructions in a block. This provides a convenient API for creating instructions, and automatically manages temporary values and labels.