Docs / Architecture

13. Bytecode & Intermediate Representation

The Dryad Bytecode VM is a stack-based virtual machine that compiles AST into linear OpCodes for efficient sequential execution. The Intermediate Representation (IR) sits between bytecode and native assembly — it is register-based (unlike the stack-based bytecode), architecture-independent, and enables classical optimizations before code generation.

OpCode CategoryInstructionsPurpose
StackPush, Pop, DupValue stack management for operands and results.
ArithmeticAdd, Sub, Mul, Div, Mod, PowNumeric operations on the top of stack.
LogicalAnd, Or, NotBoolean short-circuit and negation operations.
BitwiseBitwiseAnd, BitwiseOr, BitwiseXor, ShiftLow-level bit manipulation instructions.
ComparisonEqual, NotEqual, Less, Greater, LessEqual, GreaterEqualRelational comparisons pushing boolean results.
Control FlowJump, JumpIfFalse, JumpIfTrue, Call, ReturnUnconditional and conditional branches, function calls.
VariablesGetLocal, SetLocal, GetGlobal, SetGlobalScope-aware variable read/write operations.
Objects/ArraysGetProperty, SetProperty, NewObject, NewArrayObject field access, array construction and mutation.
ClassesDefineClass, NewInstance, CallMethodClass definition, instantiation, and virtual method dispatch.
Bytecode Execution Cycle
 1 // The VM follows a strict Fetch-Decode-Execute-Advance cycle:
 2 //
 3 // 1. FETCH:   Read OpCode at current Instruction Pointer (IP)
 4 // 2. DECODE:  Identify operation and operands
 5 // 3. EXECUTE: Perform the operation (manipulate the value stack)
 6 // 4. ADVANCE: Increment IP to the next instruction
 7 // 5. REPEAT:  Continue until Return or program end
 8 
 9 // Example: compiled representation of a + b
10 LOAD_FAST a       // Push local 'a' onto the value stack
11 LOAD_FAST b       // Push local 'b' onto the value stack
12 ADD               // Pop two values, add, push result
13 RETURN_VALUE      // Pop result and return from function

The bytecode is strictly linear — no nested structure. All control flow is resolved via Jump, JumpIfFalse, and JumpIfTrue OpCodes.

Found an error in the spec? Open a PR.
Official Version: 1.0 · May 2026