bf-rs is a optimizing Brainfuck interpreter and JIT compiler
inspired by Eli Bendersky’s series on JIT compilation.
It includes a library crate
bf that exports most of the functionality,
and an executable
bfi that provides a command-line interface for executing
This crate supports Rust version 1.20 and later. However,
by default, installing
bf does not enable the JIT compiler because
that requires nightly Rust. To build and install from crates.io with the native x86-64
$ cargo +nightly install bf --features=jit
This library implements a number of compilation passes:
First, Brainfuck concrete syntax is parsed into an abstract syntax tree.
Then, repeated sequences of the same command are run-length encoded.
Then, common loop forms are converted to new (non-Brainfuck) instructions by the peephole optimizer.
The peephole output can be flattened to bytecode, which is then interpreted.
Or, if the
jitfeature is enabled (nightly only), the peephole output can be just-in-time compiled to x64 machine code.
Or, if the
llvmfeature is enabled (LLVM ≥ 3.8 must be in the PATH to build), the peephole output can be JIT compiled using LLVM. (This is quite slow right now.)
Interpreters are provided for the intermediate forms as well. In particular,
all representations of Brainfuck programs implement the
Parsing and interpretation for unoptimized Brainfuck abstract syntax trees.
Brainfuck bytecode instead of an abstract syntax tree.
Definitions common to multiple passes.
Just-in-time compiles Brainfuck AST to x64 machine code (
The peephole optimizer, which replaces common loop forms with single (non-Brainfuck) instructions.
Run-length encodes Brainfuck commands.
Minimal run-time system, which does I/O.
The Brainfuck machine state.
Helper definitions for testing both inside and outside (e.g., benches) the crate.
Contains the Interpretable trait, which provides a common interface for running a Brainfuck program.