VDBE opcode porting
The execution engine runs a VDBE-style bytecode program (Turso calls these
Insn; Ahtola calls them VdbeOpcode). Programs are built by the
Compilation layer and executed by the Execution layer. Opcode semantics
must match Turso/SQLite exactly — the conformance corpus checks the
observable behavior, and the on-disk/transactional invariants depend on it.
Where things live
| Concern | Ahtola (C#) | Turso (Rust) |
|---|---|---|
| Opcode enum | src/Ahtola.Core/Execution/VdbeProgram.cs → enum VdbeOpcode |
turso-src/core/vdbe/insn.rs → pub enum Insn |
| Execution loop | src/Ahtola.Core/Execution/ (VdbeProgram.cs, VdbeArithmetic.cs, VdbeTransaction.cs, VdbeParameterBinding.cs) |
turso-src/core/vdbe/execute.rs |
| Program builder | src/Ahtola.Core/Compilation/ (*ProgramBuilder.cs, DmlStatementCompiler.cs, SelectStatementCompiler.cs) |
turso-src/core/vdbe/builder.rs |
| Explain | src/Ahtola.Core/Execution/VdbeExplain.cs |
turso-src/core/vdbe/explain.rs |
| Sorter / rowset / hash table | Execution/ + Compilation/BufferedWindowProgramBuilder.cs |
turso-src/core/vdbe/sorter.rs, rowset.rs, hash_table.rs, bloom_filter.rs |
Porting an opcode
- Find the Rust
Insninturso-src/core/vdbe/insn.rsand its execute arm inexecute.rs. Read the full arm — including the?/error paths and anyLabel/jumpresolution. - Find or add the C#
VdbeOpcodeinVdbeProgram.cs. The integer values are stable (consumed by the builder and serialized in explain output) — do not renumber existing opcodes; append new ones. - Mirror the semantics, not the call style: register/cursor/sorter indices
map to Ahtola's
Register/Cursor/Sorter/Accumulatorrecord structs. Preserve operand order, jump-target resolution, and the exact error conditions. Replace RustIOResult/yield withasync/sync managed flow (see theasync-io-portskill). - Wire the builder: if the opcode is emitted by a program builder, mirror
the builder side in
Compilation/so the compiler emits it in the same situations Turso does. - Exercise it: add or find a conformance case that hits the opcode, run
it through
Invoke-ManagedTestSuite.ps1 -Filter ... -MinimumExecutedTests 1, then the full conformance lane.
Do not
- Do not invent opcodes that have no upstream counterpart without a strong
reason and a doc comment explaining why. If you must, mark it clearly as an
Ahtola extension in
VdbeExplainoutput. - Do not change an opcode's observable behavior to "simplify" it. If Turso raises an error at step N, Ahtola must raise the same error at the same step.
- Do not break explain output: the opcode names/values are part of the debugging contract.