Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Opcode Reference

Every instruction is a sequence of ints in the bytecode stream: one opcode int, followed by zero or more operand ints. “Size” below is the total instruction length (opcode + operands), i.e. what getOpCodeOffset() returns and how far PC advances by default.

Control & data movement

OpcodeMnemonicSizeOperandsBehavior
0x01CALL2target offsetPush PC + 2 onto the return stack, jump to target offset (used for call)
0x02POP2variable slotPop the stack top into the given variable slot
0x03PUSH3type tag, valuePush a value onto the stack — see below
0x04EXEC2function indexCall a native function (println, print, inputInt, inputStr) — see Built-in Functions
0x05JUMP2target offsetUnconditional jump (used for jump)
0xFERET1Pop the return stack and jump there (used for endroutine)
0xFFHLT1Halt execution

PUSH type tags

The second operand of PUSH selects what the third operand means:

TagMeaning
0x01String — third operand is an index into the string pool
0x02Integer literal — third operand is the value itself
0x03Variable — third operand is a variable slot to read from

Arithmetic

All arithmetic opcodes are size 1 (no operands) — they pop two int64 values off the stack, combine them, and push an int64 result. Operand order: the value pushed second (b) is popped first, so a OP b is computed correctly for non-commutative operators.

OpcodeMnemonicOperation
0xA0ADDa + b
0xA1SUBa - b
0xA2MULa * b
0xA3DIVa / b (integer division)
0xA4POWa ^ b
0xA5MODa % b

Comparison / conditional jump

Size 2 — operand is the offset to jump to if the comparison is false. All pop two values, compare as int64, and either fall through (true) or jump to the operand (false). This is exactly how if blocks compile.

OpcodeMnemonicComparison
0xB0JEQ==
0xB1JGR>
0xB2JLS<
0xB3JGE>=
0xB4JLE<=
0xB5JNE!=

Strings

OpcodeMnemonicSizeBehavior
0xAAJOIN1Pop a count n, then pop n strings off the stack and concatenate them in original order, push the result — this is what .. compiles to

Notes

  • There is no dedicated float arithmetic opcode set yet — TAG_FLOAT exists in the type system (see Bytecode Format) but the current arithmetic opcodes operate on int64_t only.
  • Unrecognized opcodes cause the VM to print Invalid opcode and halt with an error.