Crate bf[−][src]
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
Brainfuck programs.
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
JIT enabled:
$ 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
jit
feature is enabled (nightly only), the peephole output can be just-in-time compiled to x64 machine code. -
Or, if the
llvm
feature 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
Interpretable
trait.
Modules
ast |
Parsing and interpretation for unoptimized Brainfuck abstract syntax trees. |
bytecode |
Brainfuck bytecode instead of an abstract syntax tree. |
common |
Definitions common to multiple passes. |
jit |
Just-in-time compiles Brainfuck AST to x64 machine code ( |
llvm |
JIT compiler for Brainfuck based on LLVM. |
peephole |
The peephole optimizer, which replaces common loop forms with single (non-Brainfuck) instructions. |
rle |
Run-length encodes Brainfuck commands. |
rts |
Minimal run-time system, which does I/O. |
state |
The Brainfuck machine state. |
test_helpers |
Helper definitions for testing both inside and outside (e.g., benches) the crate. |
traits |
Contains the Interpretable trait, which provides a common interface for running a Brainfuck program. |