fsqlite-vdbe
Virtual Database Engine (VDBE) bytecode interpreter, program builder, register allocator, coroutine mechanism, and vectorized execution engine for FrankenSQLite.
Overview
fsqlite-vdbe is the execution backend of FrankenSQLite. It takes a finalized
VdbeProgram (produced by the planner's code generator) and runs it
instruction-by-instruction through a register-based virtual machine. The engine
maintains a register file of SqliteValue cells, manages cursors over B-tree
storage, and emits result rows.
Key subsystems:
- Program builder (
ProgramBuilder) -- Construct bytecode programs by emitting instructions, creating forward-reference labels, and allocating registers. Callfinish()to validate and extract aVdbeProgram. - Label system -- Opaque
Labelhandles allow jump instructions to be emitted before the target address is known. All labels must be resolved before execution. - Register allocator (
RegisterAllocator) -- Sequential allocation starting at register 1, with a temporary register reuse pool. - Coroutines (
CoroutineState) -- Cooperative PC-swap state machines for subquery evaluation (NOT async).InitCoroutine/Yield/EndCoroutineopcodes drive the protocol. - Heap frame stack (
VdbeFrame) -- Supports nested trigger/subprogram execution without Rust call-stack recursion, enforcingSQLITE_MAX_TRIGGER_DEPTH. - Vectorized execution -- Columnar batch processing for scans, joins (nested-loop and hash), aggregation, sorting, and dispatch. Benchmarks included.
- VDBE engine (
VdbeEngine) -- The fetch-execute interpreter. Supports expression evaluation, control flow, arithmetic, comparison, row output, and cursor-based table operations throughMemDatabase(in-memory) and B-tree backends.
Position in the dependency graph:
fsqlite-planner (codegen)
--> fsqlite-vdbe (this crate)
--> fsqlite-btree (cursor I/O)
--> fsqlite-pager (page cache)
--> fsqlite-wal (write-ahead log)
--> fsqlite-mvcc (concurrency)
--> fsqlite-func (SQL functions)
Dependencies: fsqlite-types, fsqlite-error, fsqlite-pager, fsqlite-btree,
fsqlite-ast, fsqlite-mvcc, fsqlite-wal, fsqlite-func,
crossbeam-deque, tempfile.
Key Types
ProgramBuilder-- Bytecode program under construction. Emits instructions, manages labels, allocates registers.VdbeProgram-- Finalized, immutable bytecode program ready for execution.Label-- Opaque forward-reference handle for jump targets.RegisterAllocator-- Sequential register allocator with temp reuse pool.KeyInfo-- Multi-column key structure for index comparisons (collation and sort order per field).SortOrder--AscorDescfor key comparison direction.CoroutineState-- Tracks cooperative coroutine execution (yield register, saved PC, exhaustion flag).VdbeFrame-- Heap-allocated execution frame for trigger/subprogram nesting.RaiseResult-- Result of RAISE() inside trigger bodies (Ignore, Rollback, Abort, Fail).VdbeEngine(inengine) -- The bytecode interpreter. Maintains the register file, bindings, cursors, and result rows.MemDatabase/MemTable(inengine) -- In-memory row store for cursor-based operations without the full B-tree stack.
Usage
use ;
use ;
// Build a simple program
let mut builder = new;
let result_reg = builder.alloc_reg;
// Emit: Integer 42 -> result_reg
builder.emit_op;
// Emit: ResultRow result_reg, 1
builder.emit_op;
// Emit: Halt
builder.emit_op;
let program: VdbeProgram = builder.finish.expect;
// Execute via VdbeEngine (requires MemDatabase or B-tree backend)
// use fsqlite_vdbe::engine::{VdbeEngine, MemDatabase};
// let rows = engine.execute(&program)?;
License
MIT (with OpenAI/Anthropic Rider) -- see workspace root LICENSE file.