pub struct Chunk {
pub code: Vec<u8>,
pub constants: Vec<Value>,
spans: Vec<SourceSpan>,
last_op: usize,
}
Expand description
A chunk is a representation of a sequence of bytecode instructions, associated constants and additional metadata as emitted by the compiler.
Fields§
§code: Vec<u8>
§constants: Vec<Value>
§spans: Vec<SourceSpan>
§last_op: usize
Index of the last operation (i.e. not data) written to the code vector. Some operations (e.g. jump patching) need to know this.
Implementations§
source§impl Chunk
impl Chunk
pub fn push_op(&mut self, op: Op, span: Span) -> usize
pub fn push_uvarint(&mut self, data: u64)
pub fn read_uvarint(&self, idx: usize) -> (u64, usize)
pub fn push_u16(&mut self, data: u16)
sourcepub fn patch_jump(&mut self, idx: usize)
pub fn patch_jump(&mut self, idx: usize)
Patches the argument to the jump operand of the jump at the given index to point to the next instruction that will be emitted.
pub fn read_u16(&self, idx: usize) -> u16
sourcepub fn first_span(&self) -> Span
pub fn first_span(&self) -> Span
Get the first span of a chunk, no questions asked.
sourcepub fn last_op(&self) -> Option<(Op, usize)>
pub fn last_op(&self) -> Option<(Op, usize)>
Return the last op in the chunk together with its index, if any.
pub fn push_constant(&mut self, data: Value) -> ConstantIdx
sourcepub fn get_constant(&self, constant: ConstantIdx) -> Option<&Value>
pub fn get_constant(&self, constant: ConstantIdx) -> Option<&Value>
Return a reference to the constant at the given ConstantIdx
fn push_span(&mut self, span: Span, start: usize)
sourcepub fn get_span(&self, offset: CodeIdx) -> Span
pub fn get_span(&self, offset: CodeIdx) -> Span
Retrieve the codemap::Span from which the instruction at
offset
was compiled.
sourcepub fn disassemble_op<W: Write>(
&self,
writer: &mut W,
source: &SourceCode,
width: usize,
idx: CodeIdx,
) -> Result<usize, Error>
pub fn disassemble_op<W: Write>( &self, writer: &mut W, source: &SourceCode, width: usize, idx: CodeIdx, ) -> Result<usize, Error>
Write the disassembler representation of the operation at
idx
to the specified writer, and return how many bytes in the code to
skip for the next op.