Engine Internals

Nebula achieves high performance through several low-level optimizations.

Architecture Overview

Source Code (.na)
      ↓
   Lexer → Tokens
      ↓
   Parser → AST
      ↓
   Compiler → Bytecode
      ↓
   VM → Execution

NanBoxing

All values in Nebula are stored in 64 bits using NaN-boxing:

  • Floats: Stored as-is (IEEE 754 double)
  • Integers, Pointers, Booleans: Encoded in the NaN space

Benefits:

  • No heap allocation for primitives
  • All values fit in a single register
  • Efficient value passing
64-bit IEEE 754 Float:
┌─────────────────────────────────────────────────────┐
│ Sign │ Exponent (11) │ Mantissa (52)               │
└─────────────────────────────────────────────────────┘

NaN Space (for encoding other types):
┌─────────────────────────────────────────────────────┐
│  1  │ 11111111111 │ Type Tag │ Payload (48 bits)   │
└─────────────────────────────────────────────────────┘

String Interning

All strings are immutable and interned:

perm a = "hello"
perm b = "hello"
# a and b point to the SAME memory location

Benefits:

  • String comparison is O(1) (pointer comparison)
  • Memory efficient for repeated strings
  • Cache-friendly access patterns

Peephole Optimization

The compiler performs bytecode optimization:

Before:                    After:
LOAD_CONST 2              LOAD_CONST 5
LOAD_CONST 3              
ADD                       

Before:                    After:
LOAD_LOCAL 0              LOAD_LOCAL_0
                          (single-byte instruction)

Optimizations:

  • Constant folding
  • Dead code elimination
  • Specialized load/store instructions
  • Integer-specific arithmetic

Global Indexing

Variables are assigned numeric indices at compile time:

# Source
perm x = 10
perm y = 20
log(x + y)
# Compiled bytecode uses indices
LOAD_GLOBAL 0    # x is index 0
LOAD_GLOBAL 1    # y is index 1
ADD
CALL_BUILTIN 0   # log is builtin 0

No hash map lookups at runtime.

Bytecode Instructions

The VM supports ~50 opcodes:

Stack Operations

OpcodeDescription
PUSH_CONSTPush constant to stack
PUSH_NILPush nil value
PUSH_TRUE/FALSEPush boolean
POPPop and discard
DUPDuplicate top value

Arithmetic

OpcodeDescription
ADD/SUB/MUL/DIVArithmetic ops
MODModulus
POWPower
NEGNegation

Comparison

OpcodeDescription
EQ/NEEquality
LT/GT/LE/GEComparison

Control Flow

OpcodeDescription
JUMPUnconditional jump
JUMP_IF_FALSEConditional jump
LOOPLoop back
CALLCall function
RETURNReturn from function

Collections

OpcodeDescription
LISTCreate list
MAPCreate map
INDEXGet element
STORE_INDEXSet element
LENGet length

Memory Safety

Nebula is written in Rust:

  • No use-after-free bugs
  • No buffer overflows
  • No data races
  • Sandboxed script execution

The VM does not allow arbitrary pointer manipulation from scripts.

Value Types

Internal runtime type representation:

TypeRust TypeDescription
nbf64Generic number
inti64Integer
flf64Float
boolboolBoolean
wrdStringString (interned)
lstVec<Value>List
mapHashMapMap
tupVec<Value>Tuple
fnRc<Function>Function