One interpreter, four outputs: Futamura projections in a JavaScript engine
Quench writes the meaning of each JavaScript operation once and derives the interpreter, a copy-and-patch JIT, an optimizing JIT and an AOT compiler from it. How that works, and an honest count of how far the code is from the design.
In 1971 Yoshihiko Futamura observed that if you have a program specializer and an interpreter, then specializing the interpreter to a particular source program gives you a compiled program. You never write a compiler. You write an interpreter and a specializer, and the compiler falls out.
It is one of the most elegant ideas in programming languages and it has mostly stayed in papers, with a few notable exceptions such as Truffle and, recently, weval for WebAssembly. Quench is built on it. This post explains how, and how much of it is real so far.
The problem it solves
A JavaScript engine with an interpreter and two JIT tiers normally has three implementations of every operation. a + b is written once in the interpreter, once in the baseline compiler, and once in the optimizing compiler. They are supposed to agree. Engines have whole teams and fuzzers dedicated to finding where they do not.
Our own history shows the cost. In early November our JIT was slower than our interpreter on several benchmarks, and an audit found that that each improvement to the interpreter had to be made again, by hand, in each tier.
One row per operation
In Quench the meaning of an operation is one generic function, called a row. It is written against an interface called a stage, which supplies the primitive steps: load a register, test a tag, add two integers, branch.
// Simplified. One definition of integer add with overflow check.
fn add<S: Stage>(s: &mut S, dst: Reg, a: Reg, b: Reg) {
let x = s.read(a);
let y = s.read(b);
if s.both_int32(x, y) {
let (sum, overflow) = s.add_overflow(x, y);
s.branch_cold(overflow, |s| s.generic_add(dst, x, y));
s.write_int32(dst, sum);
} else {
s.generic_add(dst, x, y);
}
}
What that function does depends on which stage runs it.
- On the Concrete stage,
s.read(a)reads a register ands.add_overflowadds. The row is the interpreter's handler. - On the Stencil stage, at build time, operands are left as holes. The compiler produces a snippet of machine code per operation, and the b-jit later copies snippets end to end and patches the holes. That is copy-and-patch compilation, and compiling this way costs little more than a
memcpy. - On the Emit stage,
s.add_overflowdoes not add anything. It appends an add instruction to Cranelift's intermediate representation. Run over a whole function, the rows write out an optimizing compiler's input. - The same Emit stage with relocatable constants produces code that can be written to disk, which is AOT.
Binding times: what each tier is allowed to know
The tiers differ only in how much they know when they generate code. We classify every operand by its binding time, the earliest moment its value is fixed.
| Binding time | Example | Folded by |
|---|---|---|
| Static | register numbers, constants, jump targets | every native tier |
| Stable | an object's shape, a prototype, which function a name refers to | u-jit and AOT, under a registered dependency |
| Site | the type seen at one call site | u-jit, under one guard hoisted to where it is first proved |
| Dynamic | the actual number in a register | nothing; stays a run-time value |
A Stable fact is the interesting one. It is not checked with a guard on every use. The compiled code registers a dependency, and if the fact ever changes (a prototype is modified, a function is reassigned) the code that relied on it is invalidated. Users of the fact pay nothing per use. The project's rule for this is "reactive, not guarded".
Replacing thresholds with a price
When should a function move up a tier? Most engines use counters and constants. We had about thirty of them: warm up after 400 entries, re-optimize after 64 exits, inline up to 96 instructions.
The replacement is a single estimate of whether compiling pays for itself:
// simplified: the full rule also charges a per-entry cost
net(f, t) = remaining_work(f) × (1 − cost_ratio(t)) × interpreter_cost
− compile_cost(f, t) × (1 + expected_deopts(f, t))
Remaining work is estimated from how often the function has run so far, on the assumption that code which has run n times will run about n more. For the irreversible decision to compile, a pessimistic bound of n/4 is used. Compile cost is a running fit of microseconds per instruction of IR. The tier with the highest positive net wins. Because the rule only chooses which tier runs and never changes the output, a wrong choice costs time and never correctness.
This is running as a shadow log beside the old thresholds, and on one benchmark it disagrees with them on 15 of 25 decisions. Working out which side is right is the current step.
How far the code is from the design
Here is the honest count, from the repository's own census in mid-November.
with their meaning in a shared row
of opcode logic still inside the tiers
37,800 lines, written by hand
An audit on 17 November put it more sharply: Futamura "holds on paper only". The meaning of an operation existed in four places: the general interpreter loop, 601 hand-written fast handlers, the shared rows, and a runtime operations module. The check that was supposed to stop tiers from re-implementing semantics counted opcode names, not logic, so logic could move under a different name and pass.
That audit is why the work now in progress has a simple target: one row per opcode, the interpreter's handler being that row on the Concrete stage, and a shrink-only count of every per-opcode decision that lives anywhere else. The list of hand-written arms can only get shorter.
Why bother
Because the alternative does not converge. With three hand-written implementations, every optimization is three pieces of work and every bug fix is three chances to miss one. With one, an interpreter improvement is automatically a JIT improvement and an AOT improvement. The design does not make any single tier faster by itself. It makes the engine something a small team can keep correct while making it fast.
Further reading
- Futamura, "Partial evaluation of computation process: an approach to a compiler-compiler" (1971)
- Würthinger et al., "Practical partial evaluation for high-performance dynamic language runtimes" (PLDI 2017)
- Xu and Kjolstad, "Copy-and-patch compilation" (OOPSLA 2021, arXiv 2011.13127)
- Xu and Kjolstad, "Deegen: a JIT-capable VM generator for dynamic languages" (arXiv 2411.11469)
- Fallin and Bernstein, "Partial evaluation, whole-program compilation" (weval, PLDI 2025, arXiv 2411.10559)