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 Category | Instructions | Purpose |
|---|---|---|
| Stack | Push, Pop, Dup | Value stack management for operands and results. |
| Arithmetic | Add, Sub, Mul, Div, Mod, Pow | Numeric operations on the top of stack. |
| Logical | And, Or, Not | Boolean short-circuit and negation operations. |
| Bitwise | BitwiseAnd, BitwiseOr, BitwiseXor, Shift | Low-level bit manipulation instructions. |
| Comparison | Equal, NotEqual, Less, Greater, LessEqual, GreaterEqual | Relational comparisons pushing boolean results. |
| Control Flow | Jump, JumpIfFalse, JumpIfTrue, Call, Return | Unconditional and conditional branches, function calls. |
| Variables | GetLocal, SetLocal, GetGlobal, SetGlobal | Scope-aware variable read/write operations. |
| Objects/Arrays | GetProperty, SetProperty, NewObject, NewArray | Object field access, array construction and mutation. |
| Classes | DefineClass, NewInstance, CallMethod | Class 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