pub struct Chunk {
pub code: Vec<u8>,
pub constants: Vec<Value>,
pub lines: Vec<usize>,
pub cols: Vec<usize>,
}Expand description
A compiled bytecode chunk.
Fields§
§code: Vec<u8>The bytecode instructions.
constants: Vec<Value>Constant pool.
lines: Vec<usize>Line number for each instruction (for error reporting).
cols: Vec<usize>Column number for each instruction (for error reporting).
Implementations§
Source§impl Chunk
impl Chunk
pub fn new() -> Self
Sourcepub fn emit_span(&mut self, byte: u8, line: usize, col: usize)
pub fn emit_span(&mut self, byte: u8, line: usize, col: usize)
Emit a single byte with full source span (line + col).
Sourcepub fn emit_op_span(&mut self, op: Op, line: usize, col: usize)
pub fn emit_op_span(&mut self, op: Op, line: usize, col: usize)
Emit an opcode with full span.
Sourcepub fn emit_op_u16(&mut self, op: Op, operand: u16, line: usize)
pub fn emit_op_u16(&mut self, op: Op, operand: u16, line: usize)
Emit an opcode followed by a u16 operand.
Sourcepub fn emit_op_u16_span(
&mut self,
op: Op,
operand: u16,
line: usize,
col: usize,
)
pub fn emit_op_u16_span( &mut self, op: Op, operand: u16, line: usize, col: usize, )
Emit an opcode followed by a u16 operand with full span.
Sourcepub fn emit_op_u8(&mut self, op: Op, operand: u8, line: usize)
pub fn emit_op_u8(&mut self, op: Op, operand: u8, line: usize)
Emit an opcode followed by a u8 operand.
Sourcepub fn emit_op_u8_span(&mut self, op: Op, operand: u8, line: usize, col: usize)
pub fn emit_op_u8_span(&mut self, op: Op, operand: u8, line: usize, col: usize)
Emit an opcode followed by a u8 operand with full span.
Sourcepub fn add_constant(&mut self, value: Value) -> u16
pub fn add_constant(&mut self, value: Value) -> u16
Add a constant to the pool, returning its index. Deduplicates string, int, float, and bool constants.
Sourcepub fn emit_constant(&mut self, value: Value, line: usize)
pub fn emit_constant(&mut self, value: Value, line: usize)
Emit a constant load instruction.
Sourcepub fn emit_jump(&mut self, op: Op, line: usize) -> usize
pub fn emit_jump(&mut self, op: Op, line: usize) -> usize
Emit a jump instruction, returning the offset to patch later.
Sourcepub fn patch_jump(&mut self, offset: usize)
pub fn patch_jump(&mut self, offset: usize)
Patch a previously emitted jump to target the current position.
Sourcepub fn instruction_size(code: &[u8], offset: usize) -> usize
pub fn instruction_size(code: &[u8], offset: usize) -> usize
Return the total size (opcode + operands) of the instruction at offset.
Sourcepub fn peephole_optimize(&mut self)
pub fn peephole_optimize(&mut self)
Peephole optimization pass: removes dead instruction sequences and adjusts jump targets accordingly.
Not; Not→ remove both (double negation)Neg; Neg→ remove both (double arithmetic negation)Jump 0→ remove (nop jump to next instruction)- Pure push +
Pop→ remove both (dead value)