Skip to main content

Chunk

Struct Chunk 

Source
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

Source

pub fn new() -> Self

Source

pub fn emit(&mut self, byte: u8, line: usize)

Emit a single byte with source location.

Source

pub fn emit_span(&mut self, byte: u8, line: usize, col: usize)

Emit a single byte with full source span (line + col).

Source

pub fn emit_op(&mut self, op: Op, line: usize)

Emit an opcode.

Source

pub fn emit_op_span(&mut self, op: Op, line: usize, col: usize)

Emit an opcode with full span.

Source

pub fn emit_op_u16(&mut self, op: Op, operand: u16, line: usize)

Emit an opcode followed by a u16 operand.

Source

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.

Source

pub fn emit_op_u8(&mut self, op: Op, operand: u8, line: usize)

Emit an opcode followed by a u8 operand.

Source

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.

Source

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.

Source

pub fn emit_constant(&mut self, value: Value, line: usize)

Emit a constant load instruction.

Source

pub fn len(&self) -> usize

Current code offset.

Source

pub fn is_empty(&self) -> bool

Returns true if the chunk contains no bytecode.

Source

pub fn emit_jump(&mut self, op: Op, line: usize) -> usize

Emit a jump instruction, returning the offset to patch later.

Source

pub fn patch_jump(&mut self, offset: usize)

Patch a previously emitted jump to target the current position.

Source

pub fn read_u16(&self, offset: usize) -> u16

Read a u16 operand at the given offset.

Source

pub fn read_u8(&self, offset: usize) -> u8

Read a u8 operand at the given offset.

Source

pub fn instruction_size(code: &[u8], offset: usize) -> usize

Return the total size (opcode + operands) of the instruction at offset.

Source

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)

Trait Implementations§

Source§

impl Clone for Chunk

Source§

fn clone(&self) -> Chunk

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Chunk

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Chunk

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for Chunk

§

impl !RefUnwindSafe for Chunk

§

impl Send for Chunk

§

impl Sync for Chunk

§

impl Unpin for Chunk

§

impl UnsafeUnpin for Chunk

§

impl !UnwindSafe for Chunk

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.