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, short-circuit logic, 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.
! tests for zero, && evaluates its right operand only when the left
is nonzero, and || evaluates its right operand only when the left is zero.
Each returns 0 or 1. Comparisons bind tighter than &&, which binds tighter
than ||. Unary ! binds as tightly as unary minus.
fn safe_ratio(n, d) { return d != 0 && n / d > 3; }
safe_ratio(12, 0) || safe_ratio(12, 2);
This program returns 1. The first call skips division by zero.
./build/nutjit --budget 1000 'while (1) { continue; }'The command exits with a source-positioned budget error. A budget spends
one unit before each while condition and each function entry, including tail
calls. Both JIT modes and --interp use the same rule. In the REPL the budget
applies to the full replay of each input. Failed input is discarded.
The optimized JIT and interpreter both return 1 for this mutual recursion.
The 12,001-unit budget covers every entry, including the initial call and
the base case. With --budget 12000 the same program stops at the last
function entry.
make all
program='fn even(n) { if (n == 0) { 1; } else { odd(n-1); } }
fn odd(n) { if (n == 0) { 0; } else { even(n-1); } }
even(12000);'
./build/nutjit --budget 12001 "$program"
./build/nutjit --interp --budget 12001 "$program"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 optimized JIT can reuse a frame for a direct call that supplies that
value, including a call in the final branch of an if or a trailing block.
break; exits the innermost while loop. continue; starts that loop's next
condition check. Both keep any effects already performed in the iteration.
The loop result comes from the last iteration that reached the end of its body,
or zero if no iteration completed.
- 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. The interpreter reuses its function frame
for explicit and implicit tail calls, so it can check deep recursion with
--interpand the same optional execution budget. - 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.binCompare optimized and baseline division.
Linux 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. The optional budget limits loop and call checks. External limits still govern compilation, stack depth, time and memory. 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.