Guides · 05

Compile to Native Machine Code

Compile supported Ochre programs to native executables via Cranelift AOT with ochre build, understand prerequisites, and benchmark against the VM.

ochre build compiles supported Ochre programs directly to native machine code using Cranelift (written in pure Rust — no LLVM installation needed) and links a native executable via your system C compiler (cc).

Step 1: Write a Compute-Heavy Program (fib.och)

fn fib(n: Int) -> Int {
    if n <= 1 {
        return n
    }
    return fib(n - 1) + fib(n - 2)
}

fn main() {
    println(fib(22))
}

Step 2: Build and Run the Native Binary

# Compile and link a native executable
ochre build fib.och -o fib

# Execute the compiled binary
./fib

Expected Output

17711

Step 3: Emit a Relocatable Object File (Optional)

If you want to inspect machine code or link with custom flags:

ochre build fib.och --emit-obj -o fib.o
objdump -d fib.o | head -n 40

Step 4: Benchmark VM vs. Native AOT

On examples/bench_fib.och (fib(35) = 9,227,465):

Execution ModeCommandMeasured Runtime
Bytecode VMochre examples/bench_fib.och61.62s
Cranelift Native AOTochre build examples/bench_fib.och -o fib && ./fib0.37s (~166x faster)

Semantic Parity with the VM

As of v0.1.0-alpha.4, ochre-codegen lowers && and || with true short-circuit basic blocks and is verified with automated VM-vs-AOT differential tests so supported programs produce byte-for-byte identical output on both backends.