# How the native JIT works

Guillotine can compile a frequently called contract into a native function on AArch64 and x86-64 hosts running macOS or Linux. It chooses the execution tier when a call frame starts. Tier 0 executes bytecode, Tier 1 executes a schedule of opcode handlers, and Tier 2 executes machine code emitted directly from the contract's bytecode.

## When a call runs natively

The JIT is off by default. Enable it on a Zig EVM type with the following configuration.

```zig
const Evm = @import("evm").Evm;

const NativeEvm = Evm(.{
    .enable_jit = true,
    .jit_hot_threshold = 2,
    .jit_cache_capacity = 64,
});
```

Consider three calls to the same deployed bytecode on one `NativeEvm` instance, starting with an empty cache.

| Call | What happens |
| --- | --- |
| First | The cache records the code hash and the frame interprets. |
| Second | The lookup reaches the threshold. If compilation succeeds, this call runs the new native function immediately. |
| Third | The cache returns the compiled function. The call pays no compilation cost while the entry is resident. |

Each instance keys entries by code hash and compiler options, including the fork rules. At capacity it evicts the least recently used entry that is free. A running frame pins its entry so a nested call can compile another contract while the caller's machine code stays mapped.

The bridge requires 256-bit words, the standard 1,024-slot stack, flat frame memory, and gas checks. Frames under instruction tracing or inspection interpret. So do frames without a code hash, including deployment init code. An ineligible program or a compilation failure leaves the frame available to the interpreter.

## What the compiler emits

The compiler scans the bytecode for basic blocks and computes the stack depth each block requires and the space it can consume. Each backend emits its own machine instructions, patches branches, and maps the result into executable memory. A jump to a known valid `JUMPDEST` becomes a direct branch. For a computed destination, the generated code checks a table of valid targets. A byte inside a `PUSH` immediate cannot serve as a jump target.

On AArch64 the compiler models the top of the EVM stack with constants and CPU registers. Consider the following fragment.

```text
PUSH1 0x02
PUSH1 0x03
ADD
```

The compiler knows the result is `5` before the call runs. It can pass that constant to the next operation without emitting two stack writes and an addition. With values known only at runtime, arithmetic can keep intermediate results in registers. The x86-64 backend also tracks constants and materializes stack values in memory at block boundaries.

### Gas still follows the bytecode

The fragment costs **9 gas**, with 3 for each push and 3 for `ADD`. Constant folding changes the CPU's work while preserving that charge. Generated code checks stack bounds at block entry and charges fixed gas in groups. A group ends at `GAS` or an operation whose cost depends on runtime state. This gives `GAS` the correct value to push and gives helpers the correct balance before they charge for work such as memory expansion.

## How native code accesses state

The native function receives a `JitContext` with the frame's stack, memory, calldata, gas, and a table of runtime helpers. Storage operations and logs have dedicated helpers. An opcode such as `CALL` can invoke the existing frame handler. Its callee makes its own tier decision, so a native caller can call an interpreted contract or another compiled contract.

When the function exits, the bridge synchronizes stack, memory, and remaining gas with the frame and maps the native result to the usual EVM stop, return, revert, or exceptional halt. A stateful frame that has entered native code completes through this path. Restarting it in the interpreter could apply a storage write or emit a log twice.

## Checking which path ran

For a supported C API handle with the JIT enabled, `guillotine_get_jit_stats` reports native frames, interpreted frames for deployed code, frames without a code hash, and compilation failures. These counters let an embedder distinguish a cold call from a compilation error. To check execution semantics in the repository, run `zig build test-jit`. The differential suites compare status, remaining gas, and output, and the stateful tests also compare storage and logs.

For timing methodology and measured workloads, see [Benchmarks and native JIT](/guides/benchmarks-and-jit). For transitions between the interpreter tiers, see [Three-tier adaptive execution](/concepts/three-tier-execution).
