A small language with a JIT compiler that emits real x86-64 machine code into executable memory and jumps to it: no LLVM and no interpreter in the execution path.
Source passes through a hand-written lexer, a recursive-descent parser with semantic validation, and nutjit's own x86-64 code generator. The emitted bytes are copied into writable pages, those pages become read/execute-only, and the host calls the generated function. The result really was computed by machine code written at runtime.
source → lexer → parser + validation → AST → optimise → x86-64 bytes → W^X memory → CALL
The front end produces a validated AST shared by two backends. The interpreter is the semantic oracle; the JIT lowers the same tree into System V AMD64 instructions, resolves branches and calls, copies the bytes into writable pages, changes those pages to read/execute, and invokes the generated entry point. Differential tests compare both paths while byte-level tests pin the important encodings.
make bench measures fib(30) with three interpreter runs, 21 compile samples,
and 21 executions of each JIT backend. All three must return 832040 before
any timing is reported. Compile time, execution time and emitted code size are
reported separately. Results depend on the host and workload; the optimized
backend is not guaranteed to win on every program.
The optimized backend combines constant folding, compact immediate encodings,
and a small R10/R11 scratch-register allocator. It spills when an expression
exhausts the pool or must preserve a value across a call. A positive 32-bit
literal needs five bytes rather than a ten-byte movabs; a negative signed
32-bit literal needs seven. Checked division and call-error propagation add
instructions to both backends in exchange for recoverable runtime errors.
./build/nutjit --dump "2 + 3 * 4 - 1;" # arithmetic folds to mov eax, 13
./build/nutjit --no-opt --dump "2 + 3 * 4 - 1;" # inspect the baseline
make benchfn fib(n) {
if (n < 2) { return n; }
return fib(n - 1) + fib(n - 2);
}
let total = 0;
let i = 0;
while (i < 10) {
total = total + fib(i);
i = i + 1;
}
total;
The language has signed 64-bit integers, let, assignment, arithmetic,
comparisons, if/else, while, and functions with up to six parameters and
recursion. The validator rejects unknown names, duplicate functions or
parameters, wrong call arity, top-level return, and variables that are not
definitely declared on every control-flow path. Addition, subtraction and
multiplication wrap modulo 2^64. Division truncates toward zero and reports
zero divisors or the unrepresentable INT64_MIN / -1 quotient.
Blocks evaluate to their last statement, or zero when empty. A while
statement evaluates to its last executed body, or zero if it never runs.
Functions without an explicit return use their body value.
- The complete compiler pipeline is small enough to read.
- The backend hand-encodes REX prefixes, ModR/M bytes, signed division, comparisons, relative calls, and backpatched jumps.
- Generated functions follow the System V AMD64 ABI. Temporary expression
spills are tracked so every generated
callis correctly 16-byte aligned. - JIT pages obey W^X: writable while filled, read/execute while called.
- A tree-walking interpreter provides an independent oracle. Behavioral tests compare the interpreter and both JIT modes.
- Byte-exact golden tests also lock down important instruction encodings and call-alignment sequences.
See the architecture guide, the code generator, and the calling-convention notes.
sudo apt-get install -y g++ make
make run
make test # three-backend cases, encoding goldens, API tests
make sanitize # host undefined-behavior checks
make bench
./build/nutjit "let x = 5; x * 2;"
./build/nutjit --dump "1 + 2;"
./build/nutjit --interp "1 + 2;"
./build/nutjit --file program.nut
./build/nutjit --repl # successful variables/functions persist| # | Milestone | State |
|---|---|---|
| 0 | Arithmetic JIT | ✅ done |
| 1 | Variables and stack frames | ✅ done |
| 2 | Comparisons and conditionals | ✅ done |
| 3 | Loops | ✅ done |
| 4 | Functions and recursion | ✅ done |
| 5 | Optimisation and interpreter oracle | ✅ done |
| 6 | REPL, benchmark, tests, CI, and releases | ✅ done |
The release build is warning-free. make test checks the interpreter, naive
JIT and optimized JIT, including runtime failures and persistent REPL recovery.
It also checks instruction encodings and executable-memory API boundaries.
./build/nutjit --dump "1 + 2;" 2>&1 |
grep -E '^[0-9a-f ]+$' | xxd -r -p > /tmp/nutjit-code.bin
objdump -D -b binary -m i386:x86-64 /tmp/nutjit-code.binLinux or WSL2 on x86-64 with g++ (C++17) and make. The JIT deliberately
targets the System V x86-64 ABI and uses mmap/mprotect.
The language uses signed 64-bit integers and targets only the System V x86-64 ABI. It has no floating-point values, aggregates, garbage collector, native object-file output, debugger integration, or cross-platform backend. Benchmark claims cover the documented recursive integer workload, not general compiler performance.
This is not a sandbox for untrusted code. W^X prevents writes to executable pages, but does not bound parser depth, recursion, runtime or memory use. Use process isolation and external resource limits for untrusted inputs. Division by zero and signed division overflow are diagnosed with a source byte offset, without terminating the REPL. Other resource exhaustion remains outside that guarantee.
Read the architecture, then the lexer and parser, x86-64 code generator, JIT memory policy, calling convention, and optimization notes. Testing and disassembly workflows are in docs/09.
MIT: see LICENSE.