Skip to content

An optimizing brainf*ck compiler with multiple target backends: JVM, smali, dex, C, LLVM IR, ARM, WASM, JavaScript and Lox.

License

Notifications You must be signed in to change notification settings

mrjameshamilton/bf

Repository files navigation

An optimizing Brainf*ck compiler

An optimizing brainf*ck compiler with multiple target backends: JVM using ProGuardCORE for code generation, smali, dex using BAT, C, LLVM IR, ARM assembly, WASM and JavaScript.

Some optimizations are applied before code generation:

  • Zero-ing loops ([+] / [-]) are represented as a single instruction
  • Consecutive zero-ing instructions are merged
  • Consecutive move and add instructions are merged into single instructions with an amount parameter
  • Zero moves/adds are removed
  • Top-level loops before memory updates are removed

Building

./gradlew build

The build task will execute all tests and create an output jar lib/bf.jar.

Executing

A wrapper script bin/bf is provided for convenience in the bin/ directory:

$ bin/bf --help
Usage: bf options_list
Arguments: 
    script -> brainf*ck script { String }
Options: 
    --output, -o -> output { String }
    --target, -t [JVM] -> target { Value should be one of [jvm, c, llvm, smali] }
    --debug, -d [false] 
    --help, -h -> Usage info

By default, bf will compile a provided brainf*ck script for the JVM and execute it. The compiler can instead generate a jar file with the -o option.

JVM (default)

$ bin/bf examples/helloworld.bf -t jvm -o helloworld.jar
$ java -jar helloworld.jar

Smali (Dalvik assembler)

$ bin/bf examples/helloworld.bf -t smali -o helloworld.smali
$ smali a helloworld.smali -o classes.dex
$ adb push classes.dex /sdcard/Download/classes.dex
$ adb shell dalvikvm -cp /sdcard/Download/classes.dex Main

Dex

$ bin/bf examples/helloworld.bf -t dex -o helloworld.dex
$ adb push helloworld.dex /sdcard/Download/helloworld.dex
$ adb shell dalvikvm -cp /sdcard/Download/helloworld.dex Main

C

$ bin/bf examples/helloworld.bf -t c -o helloworld.c
$ gcc helloworld.c -o helloworld && ./helloworld

LLVM IR

$ bin/bf examples/helloworld.bf -t llvm -o helloworld.ll
$ lli helloworld.ll

ARM assembly

$ bin/bf examples/helloworld.bf -t arm -o helloworld.s
$ arm-none-eabi-as helloworld.s -o helloworld.o
$ arm-none-eabi-ld helloworld.o -o helloworld
$ qemu-arm ./helloworld

WASM

$ bin/bf examples/helloworld.bf -t wasm -o helloworld.wat
$ wasmtime helloworld.wat

JavaScript

$ bin/bf examples/helloworld.bf -t js -o helloworld.js
$ nodejs helloworld.js

Inputs for the Javascript version are passed as command-line arguments.

Lox

Lox doesn't provide any built-in way to convert ASCII character codes to characters, and the built-in print function always prints newlines, but we can use awk to convert ASCII codes to characters while merging all the lines together.

$ bin/bf examples/helloworld.bf -t lox -o helloworld.lox
$ jlox helloworld.lox | awk '{printf("%c", $1)}' ORS=' '

Lox also doesn't provide any ability to receive inputs so the inputs to a program can be compiled directly into it by using the bf --input option.

Useful brainf*ck resources

About

An optimizing brainf*ck compiler with multiple target backends: JVM, smali, dex, C, LLVM IR, ARM, WASM, JavaScript and Lox.

Topics

Resources

License

Stars

Watchers

Forks

Languages