Skip to content

Repository contains code for interpreter, compiler and optimizations for brainfuck language.

Notifications You must be signed in to change notification settings

Sameeranjoshi/brainfuck_toolchain

Repository files navigation

Brainfuck Interpreter C++ and Python

This repository contains two implementations of a Brainfuck language processor one in C++ and other in Python. The Python interpreter is way slow! Switched to C++ then.

Table of Contents

Requirements

  • C++ Compiler: A C++17 compliant compiler.

Installation

1. Clone the Repository

git clone https://github.com/Sameeranjoshi/brainfuck_toolchain.git
cd brainfuck_toolchain/

3. Build the Interpreter

Manual Compilation

  • Brainfuck Interpreter

    clang++ -std=c++17 -O3 -o bfi inter_cpp.cpp

Usage

Brainfuck Interpreter

Run the interpreter: This runs all the benchmarks

./bfi

Run the interpreter with profiling enabled:

./bfi -p

This generates a profiling_output.txt file in the current directory.

Brainfunk Compiler for X86-64

Run the compiler:

  1. Generates the driver binary
  2. runs assembler (as)
  3. Links with C library using gcc
  4. Executes the binary
bash runassembler.sh

Above command runs the compiler and generates assembly with and without using optimization flags and generates the timing results in timing_results/ folder.

To control the optimization flags use

clang++ -std=c++17 -O3 -o compiler compiler_x86_64.cpp
./compiler <input_file> [--optimize-simple-loops] [--optimize-memory-scans]

Brainfunk to LLVM

Run the compiler:

  1. Generates the LLVM output.ll file from BF code.
  2. runs llc
  3. Links with C library using gcc
  4. Executes the binary

Note: This command will also run AOT compiler for benchmarking and store results in timing_results_llvm

bash runllvm.sh

To manually build and run a single test:

mkdir -p build
cd build
cmake -DLLVM_DIR=/usr/lib64/cmake/llvm/ -G Ninja ..
ninja

./aot_llvm ../benches/hello.b
llc -march=x86-64 output.ll -o output.s
gcc output.s -o out

./out

About

Repository contains code for interpreter, compiler and optimizations for brainfuck language.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published