Skip to content

A blazing fast JIT powered brainfuck interpreter written in Go.

Notifications You must be signed in to change notification settings

EdwinHoksberg/gobf

Repository files navigation

Gobf - A JIT powered Brainfuck interpreter written in Go

Support

  • Apple Silicon (aarch64)
  • Linux x86_64

Benchmarks

Ran on a 2021 Apple M1 Pro (16GB):

Benchmark 1: ./gobf examples/bench.b
  Time (mean ± σ):      69.1 ms ±   1.8 ms    [User: 65.4 ms, System: 2.9 ms]
  Range (min … max):    66.7 ms …  72.7 ms    37 runs

Benchmark 2: ./gobf examples/mandelbrot.b
  Time (mean ± σ):     722.8 ms ±   6.3 ms    [User: 712.8 ms, System: 8.7 ms]
  Range (min … max):   714.5 ms … 735.0 ms    10 runs

How to use

Execute brainfuck instructions from a file:

$ ./gobf examples/hello-world.b

Or piping instructions into it:

$ echo "+-[..." | ./gobf -

Flags:

-disable-instruction-optimizer
    Disable optimizer of JIT code

-dump-jit
    Dump generated JIT code to stderr

-memory-size uint
    Size (in bytes) of the memory available to the program (default 30000)

Optimizations

Jump linking

At parse-time, the program figures out which jumps are linked to each-other and stores that offset with the instruction, so jump instructions can be executed without calculating the required jump location at run-time.

Instruction minification

An instruction optimizer can be run to find any consecutive >, <, + or - instructions and will merge them into a single instruction. This can save many operations and (usually a lot of) time for large programs.

For example, the following 12 instructions: ++>>++<<<<-- will result in only 5 instructions: Increment(2), MoveRight(2), Increment(2), MoveRight(4), Decrement(2).

Can be disabled with the -disable-instruction-optimizer flag.

Clear instruction

A common brainfuck idiom is the clear loop: [-]. This code decrements the current value by 1 until it reaches 0. The instruction optimizer can optimize to a single, branch-less instruction: the (unofficial) Clear instruction.

About

A blazing fast JIT powered brainfuck interpreter written in Go.

Topics

Resources

Stars

Watchers

Forks

Languages