Git Product home page Git Product logo

brainfuck-tools's Introduction

brainfuck-tools

Brainfuck Compiler, JIT, Interpreter and Debugger

Building

  mkdir build && cd build
  cmake ..
  make

How to use it

  ./Brainfuck {-i | -d | -c | -co | -j | -jo} program.bf [inputs.txt]

-i for the Interpreter

-d for the Debugger

-c for the Compiler

-co for the optimized Compiler

-j for the JIT

-jo for the optimized JIT

program.bf must be a text file containing the Brainfuck program to execute/compile

inputs.txt is an optional file containing inputs instead of using stdin (useful for debugging, see Notes below)

Interpreter

  ./Brainfuck -i program.bf

Simple interpretation of the program (much slower than Compiler and JIT)

Compiler

  ./Brainfuck -c program.bf > compiled_brainfuck.s
  gcc compiled_brainfuck.s -o executable_brainfuck
  ./executable_brainfuck

Compiles the program for x86_64 (amd64) Linux following System V ABI.

JIT

./Brainfuck -j program.bf

Just-In-Time compilation and direct execution for x86_64 (amd64) Linux following System V ABI

Optimized Compiler & JIT

Using -co or -jo will result in optimized code that merge repeated Brainfuck instructions together.

For example, the sequence + + + compiled/jitted without optimizations will result in the following x86 instructions:

add $1, (%rbx)
add $1, (%rbx)
add $1, (%rbx)

On the other hand, using the optimized compiler/JIT will result in a single instruction:

add $3, (%rbx)

Debugger

./Brainfuck -d program.bf

Debug the program using the following commands:

s<x>:     executes x brainfuck instructions (if x is not specified then x = 1)
g<x>:     executes instructions until instruction pointer = x (if x is negative, then until x instructions from end)
r:        run until end of the program
d<x>-<y>: dump memory content between address x and y (y is optional to print the content of one cell only)

Example debugging:

Brainfuck program to execute 2+3 (Non-Brainfuck characters are comments)

+++    sets first operand to 3
>++<   sets second operand to 2
[->+<] add first op (3) to second (2) (yield 5)

Sample debugging trace

./Brainfuck -d ../samples/add.bf
Brainfuck Debugger
'exit' to exit

IP = 0	DP = 0
IP:	  v
L1:	  +++    sets first operand to 3
DP:   v
MEM: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]

> s2
IP = 2	DP = 0
IP:	  v                        
L1:	+++ sets first operand to 3
DP: 	 v
MEM:	[2, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]

> g11
IP = 11	DP = 1
IP:	    v                                          
L3:	[->+<] add first op (3) to second (2) (yield 5)
DP: 	    v
MEM:	[2, 3, 0, 0, 0, 0, 0, 0, 0, 0, ...]

Benchmarks

Using samples/mandelbrot.bf program which prints the mandelbrot set

Method Time
Interpreter 78.40s
Compiler 3.66s
JIT 3.59s
Opt. JIT 1.36s
Opt. Compiler 1.23s

alt text

brainfuck-tools's People

Stargazers

 avatar りすりす/TwoSquirrels avatar TFKls avatar Hitoshi Mitake avatar Florian Depraz avatar Philémon Favrod avatar  avatar Sacha Bron avatar

Watchers

James Cloos avatar  avatar

Forkers

fiking

brainfuck-tools's Issues

Windows 64-bit Support

The JIT and the Compiler only support the System V ABI. Would be nice to add some #ifdefs to support Win64 ABI.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.