pub struct Builder { /* private fields */ }
Expand description
Mechanism for constructing a bytecode Assembly
by allowing
instructions to be patched before the final assembly is built.
For example, consider the problem of constructing an assembly from
this assembly language:
push lab
jump
stop
lab:
jumpdest
The challenge is that, when constructing the push lab
instruction, we don’t yet know the instruction offset of lab
.
A Builder
allows one to register a label and create an instance
of PUSH
which is later patched to ensure it has the correct
instruction offset.
Implementations§
source§impl Builder
impl Builder
pub fn new() -> Self
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Determine the number of instructions currently pushed into this builder.
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true
if no instructions have yet been pushed into
this builder.
sourcepub fn get_label(&mut self, label: &str) -> usize
pub fn get_label(&mut self, label: &str) -> usize
Get the label index associated with a particular label. If such an index does not already exist, then a new label is registered.
sourcepub fn set_label(&mut self, label: &str, offset: usize) -> Result<(), ()>
pub fn set_label(&mut self, label: &str, offset: usize) -> Result<(), ()>
Set the instruction offset associated with a given label. If the label does not yet exist (i.e. as not yet been assigned an index), then it will be.
sourcepub fn mark_label(&mut self, label: &str) -> Result<(), ()>
pub fn mark_label(&mut self, label: &str) -> Result<(), ()>
Mark a label at the current instruction offset. If the label does not yet exist (i.e. as not yet been assigned an index), then it will be.
sourcepub fn push_labeled(&mut self, insn: Instruction)
pub fn push_labeled(&mut self, insn: Instruction)
Push a new instruction onto the builder.
sourcepub fn push(&mut self, insn: Instruction)
pub fn push(&mut self, insn: Instruction)
Push a new instruction onto the builder.
sourcepub fn to_insns(self) -> Vec<Instruction>
pub fn to_insns(self) -> Vec<Instruction>
Construct the final assembly by patching all labels used within instructions.