DOCUMENTATION

Documentation

DOCUMENTATION

VM - Opcodes and Chunk

The bytecode compiler produces a flat array of OpCode values packed into a Chunk. This document covers every opcode, the Chunk structure, MethodKind, and the loop-detection helper.

Module Layout

$ text
_ ⛶ ×
1src/vm/opcode/
2├── mod.rs - re-exports
3├── opcode.rs - OpCode enum, MethodKind enum, TypeTag
4├── chunk.rs - Chunk struct
5└── decode.rs - Decoder stub (currently identity; reserved for future use)

Chunk

$ rust
_ ⛶ ×
1pub struct Chunk {
2 pub bytecode: Arc<Vec<OpCode>>,
3 pub spans: Arc<Vec<crate::error::Span>>,
4 pub is_fiber: bool,
5 pub max_locals: usize,
6 pub call_count: Arc<AtomicUsize>,
7 pub jit_ptr: Arc<AtomicPtr<c_void>>,
8 pub name: String,
9 pub arity: usize,
10 pub uses_heap: Arc<AtomicBool>,
11 pub used_locals: Arc<Vec<u8>>,
12 pub param_types: Vec<TypeTag>,
13}

Each function and the top-level program are compiled into a Chunk. Key fields:

  • bytecode - the instruction array. Indexed directly by instruction pointer (IP).
  • spans - one Span per bytecode instruction; same length as bytecode. Used for runtime error reporting.
  • is_fiber - true for fiber (coroutine) chunks, which have different call/return semantics.
  • max_locals - number of local register slots required. The executor allocates exactly this many slots per call frame.
  • call_count - incremented on each call. When it crosses the dynamic threshold vm.jit_threshold (default 50), the JIT compiles the chunk.
  • jit_ptr - atomic pointer to the JIT-compiled native function; null if not yet compiled.
  • used_locals - bitmask of which local slots are actually referenced, produced by jit::analysis::analyze_chunk_locals. Used by the JIT to skip zeroing unused slots.
  • param_types (4.4) - the declared signature type of each parameter. The JIT type inference seeds parameter registers from these declared types instead of Unknown; other registers keep flat inference.

Chunk is Clone; all fields use Arc so clones share the same underlying data.

TypeTag

Used in OpCode::Input to specify what type the runtime should parse from stdin:

$ rust
_ ⛶ ×
1pub enum TypeTag {
2 Int, Float, String, Bool, Date, Array, BoolArray, Set, Map,
3 Table, Function, Row, Json, Fiber, Database, Unknown
4}

MethodKind

A u8-indexed enum of all built-in methods callable via MethodCall. The full list:

ValueNameApplies to
0PushArray
1PopArray
2LenString, Array, Set, Map
3CountTable, Array, Set
4SizeArray, Set, Map, Table
5IsEmptyArray, Set, Map, Table
6ClearArray, Set, Map, Table
7ContainsArray, Set, Map
8GetArray, Map, Table, Store
9InsertSet, Map, Table, Database
10UpdateMap, Table, Database
11DeleteMap, Set, Table, Database
12FindArray, Table
13JoinTable
14ShowTable (print formatted grid)
15SortArray
16ReverseArray
17AddSet
18RemoveArray, Set
19HasSet, Map, String, Array
20LengthString
21UpperString
22LowerString
23TrimString
24IndexOfString, Array
25LastIndexOfString
26ReplaceString
27SliceString, Array
28SplitString
29StartsWithString
30EndsWithString
31ToIntString, Float, Bool, Date
32ToFloatString, Int, Bool
33SetMap (upsert)
34KeysMap, Json
35ValuesMap, Json
36WhereTable, Array
37YearDate
38MonthDate
39DayDate
40HourDate
41MinuteDate
42SecondDate
43FormatDate, String
44ExistsStore
45AppendStore, String
46InjectJson → Table
47ToStrAny (calls to_string)
48ToJsonAny (converts to JSON)
49NextFiber
50RunFiber
51IsDoneFiber
52CloseDatabase, Fiber
53BeginDatabase (transaction)
54CommitDatabase
55RollbackDatabase
56QueryDatabase
57QueryRawDatabase
58SyncDatabase, Store
59DropDatabase, Table
60FetchDatabase
61SaveStore
62TruncateDatabase, Table
63ExecDatabase
64IsOpenDatabase
65FirstTable, Array
66KeyMap
67ReadyIO (terminal ready-check)
68PutStore, Map
69StringifyJson, Any
70StartFiber
71ResultNet response
72TableDatabase (create table handle)
73ExecuteDatabase
74StatusNet response
75MsDate (milliseconds)

MethodKind::from_u8 maps the u8 discriminant back to the enum. Unknown values return None.

OpCode - Full Reference

All operand registers are u8 indices into the locals array unless otherwise noted. dst is always written; src, src1, src2 are read-only.

Data Movement

OpcodeFieldsDescription
Movedst, srcCopy local src to dst. Handles refcounting.
LoadConstdst, idx: u32Load constant at ctx.constants[idx] into dst.
GetVardst, idx: u32Load global variable at globals[idx] into dst.
SetVaridx: u32, srcStore src into globals[idx].
SetNamesrc, name_idx: u32Associates a debug name with a value (no runtime effect on execution).
EnvGetdst, srcRead the environment variable named src into dst.
EnvArgsdstLoad command-line arguments array into dst.

Arithmetic

All arithmetic writes result to dst and reads operands from src1, src2. Type dispatch is performed at runtime via Value::add, .sub, etc.

OpcodeOperation
Addsrc1 + src2 (int, float, string concat, date+int). For mixed Float/Int operands, Value::add (and .sub/.mul/.div/.rem/.pow) converts the non-float operand via .cast_float() rather than reinterpreting its raw bits as float bits - the same rule the JIT's fast-path selection follows, see compiler/jit/jit_codegen.md.
Subsrc1 - src2 (int, float, date-int, date-date→days)
Mulsrc1 * src2
Divsrc1 / src2 (halts on division by zero)
Modsrc1 % src2 (halts on modulo by zero)
Powsrc1 ^ src2
Negdst = -src
IntConcatsrc1 ++ src2 - integer concatenation (joins digits: 12 ++ 34 = 1234)

String Append

Compiled from the self-concatenation assignment pattern x = x + expr (see compiler/compiler/compiler_stmt.md) instead of a generic Add followed by a store - this avoids allocating for both the read and the result on every iteration of a loop that accumulates a string.

OpcodeFieldsDescription
StrAppendVarvar_idx, srcAppend locals[src] to the string in global var_idx, in place if uniquely owned.
StrAppendLocallocal_idx, srcAppend locals[src] to the string in local local_idx, in place if uniquely owned.
StrAppendMembercontainer, name_idx, srcAppend locals[src] to the string stored at JSON field name_idx of container.
StrAppendElementcontainer, index, srcAppend locals[src] to the string element at index of array container.

All four opcodes are backed by copy-on-write string concatenation logic. Ownership rules differ by site (4.4):

  • StrAppendVar (interpreter, get_global_raw): in-place extension only when the globals slot is the buffer's sole owner (strong_count <= 1 && weak_count == 0). A string literal loaded from the chunk's constant pool is a real second owner (LoadConst shares the pool's Arc), so the first append to a literal-initialized global re-allocates; the re-allocation path releases the construction reference after the store so the count reflects real owners. Mutating a shared buffer would corrupt the constant pool - every later load of the same literal would inherit the appended content.
  • StrAppendLocal (interpreter): same sole-owner rule (<= 1); locals store by transfer, so freshly built buffers are exactly singly owned.
  • JIT helpers (xcx_jit_str_append_var / _local): the var variant keeps threshold <= 2 - the second reference there is the JIT's own managed register copy, released by reload_globals right after the call. A constant-pool co-owner surfaces as count ≥ 3 and re-allocates.
  • StrAppendMember / StrAppendElement: in-place extension only under Arc::get_mut / strong_count == 1 respectively, otherwise clone-on-write.

Comparison

All write bool to dst. Every comparison opcode carries an int-int fast path (4.4) that compares the raw 64-bit payloads directly when both operands are TAG_INT, instead of ranking the operands first.

OpcodeOperation
Equalsrc1 == src2
NotEqualsrc1 != src2
Greatersrc1 > src2
Lesssrc1 < src2
GreaterEqualsrc1 >= src2
LessEqualsrc1 <= src2
Hassrc2 HAS src1 - membership test

Logic

OpcodeFieldsDescription
Anddst, src1, src2Boolean AND
Ordst, src1, src2Boolean OR
Notdst, srcBoolean NOT

Control Flow

OpcodeFieldsDescription
Jumptarget: u32Unconditional jump to instruction index target.
JumpIfFalsesrc, target: u32Jump if src is falsy.
JumpIfTruesrc, target: u32Jump if src is truthy.
Halt-Immediately stop execution.
ReturnsrcReturn value in src from current function.
ReturnVoid-Return with no value.

Loop Opcodes

Specialized combined increment-and-jump instructions for loop optimization:

OpcodeFieldsDescription
LoopNextreg, limit_reg, targetIncrement local reg, jump back to target while still in range (forward iteration).
LoopPrevreg, limit_reg, targetDecrement local reg, jump back to target while still in range (reverse iteration).
IncLocalregIncrement local reg by 1.
DecLocalregDecrement local reg by 1.
IncVaridxIncrement global idx by 1.
DecVaridxDecrement global idx by 1.
IncLocalLoopNextinc_reg, reg, limit_reg, targetIncrement local inc_reg and jump if still in range.
DecLocalLoopPrevdec_reg, reg, limit_reg, targetDecrement local dec_reg and jump if still in range.
IncVarLoopNextg_idx, reg, limit_reg, targetGlobal variable loop increment and jump.
DecVarLoopPrevg_idx, reg, limit_reg, targetGlobal variable loop decrement and jump.
ArrayLoopNextidx_reg, size_reg, targetArray/set iteration loop step: advance idx_reg, jump back to target while idx_reg < size_reg.

Set iteration is compiled through a MethodCall { kind: Values } conversion followed by the same ArrayLoopNext; there is no dedicated set-loop opcode.

Function Calls

OpcodeFieldsDescription
Calldst, func_idx: u32, base: u8, arg_count: u8Call function func_idx. Arguments are locals [base..base+arg_count]. Result stored in dst.
MethodCalldst, kind: MethodKind, base, arg_countCall a built-in method on the value at locals[base]. More than 16 arguments is rejected at compile time (arg_count: u8 cannot overflow at runtime).
MethodCallNameddst, kind: MethodKind, base, arg_count, names_idx: u32MethodCall with named arguments; names_idx indexes a constant-pool string array holding per-argument names (empty string for positional slots).
MethodCallCustomdst, method_name_idx: u32, base, arg_countMethod dispatch by string name from the constant pool. Used for dynamic JSON path access and row column resolution.

I/O

OpcodeFieldsDescription
PrintsrcPrint locals[src] to stdout with newline (\x1b[K\r\n on Unix).
Inputdst, ty: TypeTagRead from stdin, parse as ty, store in dst.
InputKeydstRead a single keypress (non-blocking).
InputKeyWaitdstRead a single keypress (blocking).
InputReadydstCheck whether input is available; write bool to dst.

Terminal Control

OpcodeFieldsDescription
TerminalRawdstEnable raw terminal mode.
TerminalNormaldstDisable raw terminal mode.
TerminalCleardstClear the terminal screen.
TerminalCursordst, on: boolShow or hide the cursor.
TerminalMovedst, x_src, y_srcMove cursor to (x, y).
TerminalWritedst, srcWrite src to terminal without newline.
TerminalRundst, cmd_srcExecute a shell command; result to dst.
TerminalExitdstExit the process.

Halt

OpcodeFieldsDescription
HaltAlertsrcEmit alert message (non-fatal; execution continues).
HaltErrorsrcPrint error and halt the current execution. Increments error_count.
HaltFatalsrcPrint fatal error and call process::exit(1).

Collections - Initialization

OpcodeFieldsDescription
ArrayInitdst, base, count: u32Create an array from count values starting at locals[base].
BoolArrayInitdstCreate an empty array:b (BoolArrayObj); elements are added via method calls.
SetInitdst, base, count: u32Create a set from count values.
MapInitdst, base, count: u32Create a map from alternating key/value pairs; count is the number of pairs.
TableInitdst, skeleton_idx, base, row_count, col_countInitialize a table from a skeleton constant and row data in locals.
TableBegindst, skeleton_idxBegin streaming construction of a large table (>200 cells) from a skeleton constant; rows follow via TableInitRow.
TableInitRowtbl_dst, base, col_countAppend one row (col_count values starting at locals[base]) to a table begun with TableBegin.
DatabaseInitdst, engine_src, path_src, tables_base_reg, table_countCreate a DatabaseObj from engine/path registers and a contiguous span of table_count table-name registers.

Collections - Operations

OpcodeFieldsDescription
GetIndexdst, container, indexdst = container[index] - works on arrays, sets (by position), maps, strings.
SetIndexcontainer, index, srccontainer[index] = src - mutable index assignment.
GetMemberdst, container, name_idx: u32Access a named member; name_idx is a constant pool index.
SetMembercontainer, name_idx: u32, srcAssign to a named member.
SetUniondst, src1, src2Mathematical set union.
SetIntersectiondst, src1, src2Mathematical set intersection.
SetDifferencedst, src1, src2Set difference src1 \ src2.
SetSymDifferencedst, src1, src2Symmetric difference.
SetRangedst, start, end, step, has_stepBuild a set from a numeric range.

Array Specifics

There are no dedicated array opcodes. Element access and mutation compile to MethodCall with MethodKind::Get / Set / Update, size queries to MethodKind::Size / Len, and appends to MethodKind::Push (see the MethodKind table above). The generic GetIndex/SetIndex opcodes (under "Collections - Operations") are used only for container indexing outside the typed method paths.

Table Specifics

OpcodeFieldsDescription
RowGetdst, row_reg, col_idx: u16Read column col_idx from row row_reg.
TableItertbl_reg, idx_reg, row_reg, limit_reg, target: u32Loop step for table iteration. Advances idx_reg, loads the row, jumps back to target.
TablePushRowtbl_reg, row_regDeep-copy row row_reg into table tbl_reg.
TableCloneSkeletondst, srcClone the schema of table src into a new empty table at dst.

Row counts are obtained via MethodCall with MethodKind::Count / Len / Size, not a dedicated opcode.

Fiber

OpcodeFieldsDescription
FiberCreatedst, func_idx: u32, base, arg_countInstantiate a fiber from function func_idx with arguments locals[base..base+arg_count].
YieldsrcYield the value src from the current fiber.
YieldWithTargetdst, srcYield the value src from the current fiber, storing caller's injected response in dst.
YieldVoid-Bare yield; - signals the end of a fiber's current iteration.

Fiber state queries and resumption are method calls (MethodKind::IsDone / Next / Close), not dedicated opcodes.

Set Specifics

There are no dedicated set opcodes beyond the algebraic SetUnion/SetIntersection/SetDifference/SetSymDifference/SetRange above. Cardinality compiles to MethodCall (MethodKind::Size / Len / Count); membership compiles to the Has opcode.

Random

OpcodeFieldsDescription
RandomIntdst, min, max, step, has_stepGenerate a random integer in [min, max] with optional step.
RandomFloatdst, min, max, step, has_stepSame for floats.
RandomChoicedst, srcPick a random element from the set src.

JSON / Date

OpcodeFieldsDescription
JsonParsedst, srcParse src as a JSON string; store JsonObj in dst.
JsonBindLocaldst, json_src, path_srcExtract path from JSON; store in local dst.
JsonBindidx: u32, json_src, path_srcExtract path from JSON; store in global idx.
JsonInjecttable_idx: u32, json_src, mapping_srcInject JSON data into a global table using a mapping.
JsonInjectLocaltable_reg, json_src, mapping_srcSame with a local table register.
JsonFastGetPushjson_src, path_src, val_srcFast JSON get and push; used in injection pipelines.
DateNowdstWrite current UTC timestamp (millis) as a date value into dst.
PerfMs / PerfUs / PerfNsdstWrite VM uptime in milliseconds / microseconds / nanoseconds to dst.

Store (File System)

All store opcodes follow the pattern dst, base where base is the register containing the path expression.

OpcodeOperation
StoreWriteWrite value to file
StoreReadRead file contents
StoreAppendAppend to file
StoreExistsCheck existence
StoreDeleteDelete file or directory
StoreListList directory contents
StoreIsDirCheck if path is a directory
StoreSizeGet file size in bytes
StoreMkdirCreate directory
StoreGlobGlob pattern match
StoreZipCreate zip archive
StoreUnzipExtract zip archive

Crypto

OpcodeFieldsDescription
CryptoHashdst, pass_src, alg_srcHash a password using the specified algorithm.
CryptoVerifydst, pass_src, hash_src, alg_srcVerify password against hash.
CryptoTokendst, len_srcGenerate a random token of specified length.

Networking

OpcodeFieldsDescription
HttpCalldst, method_idx: u32, url_src, body_srcExecute an HTTP request; result JSON in dst.
HttpServefunc_idx, port_src, host_src, workers_src, routes_srcStart HTTP server; does not return.
HttpResponddst, status_src, body_src, headers_srcSend HTTP response from a route handler.
HttpRequestdst, arg_srcAccess the current HTTP request object.

Misc

OpcodeFieldsDescription
Typeofdst, srcWrite the type name string of src into dst.
WaitsrcSleep for locals[src] milliseconds (the @wait statement and trailing @wait on method calls).
CastInt / CastFloat / CastString / CastBooldst, srcType casts emitted for the i()/f()/s()/b() built-ins.