Module cranelift_codegen::machinst::buffer[][src]

In-memory representation of compiled machine code, with labels and fixups to refer to those labels. Handles constant-pool island insertion and also veneer insertion for out-of-range jumps.

This code exists to solve three problems:

This “buffer” implements a single-pass code emission strategy (with a later “fixup” pass, but only through recorded fixups, not all instructions). The basic idea is:

That satisfies label references, but we still may have suboptimal branch patterns. To clean up the branches, we do a simple “peephole”-style optimization on the fly. To do so, the emitter (e.g., Inst::emit()) informs the buffer of branches in the code and, in the case of conditionals, the code that would have been emitted to invert this branch’s condition. We track the “latest branches”: these are branches that are contiguous up to the current offset. (If any code is emitted after a branch, that branch or run of contiguous branches is no longer “latest”.) The latest branches are those that we can edit by simply truncating the buffer and doing something else instead.

To optimize branches, we implement several simple rules, and try to apply them to the “latest branches” when possible:

Branch-optimization Correctness

The branch-optimization mechanism depends on a few data structures with invariants, which are always held outside the scope of top-level public methods:

We argue below, at each method, how the invariants in these data structures are maintained (grep for “Post-invariant”).

Given these invariants, we argue why each optimization preserves execution semantics below (grep for “Preserves execution semantics”).

Structs

MachBuffer

A buffer of output to be produced, fixed up, and then emitted to a CodeSink in bulk.

MachBufferFinalized

A MachBuffer once emission is completed: holds generated code and records, without fixups. This allows the type to be independent of the backend.

MachLabel

A label refers to some offset in a MachBuffer. It may not be resolved at the point at which it is used by emitted code; the buffer records “fixups” for references to the label, and will come back and patch the code appropriately when the label’s location is eventually known.

MachSrcLoc

A source-location mapping resulting from a compilation.

MachStackMap

Record of stack map metadata: stack offsets containing references.

Enums

StackMapExtent

A stack map extent, when creating a stack map.