Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mrjameshamilton/bf-llvm
A LLVM brainf*ck compiler
https://github.com/mrjameshamilton/bf-llvm
brainfuck brainfuck-compiler compiler llvm
Last synced: about 6 hours ago
JSON representation
A LLVM brainf*ck compiler
- Host: GitHub
- URL: https://github.com/mrjameshamilton/bf-llvm
- Owner: mrjameshamilton
- License: mit
- Created: 2023-09-18T20:29:16.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2024-01-17T20:52:06.000Z (10 months ago)
- Last Synced: 2024-01-18T04:15:44.310Z (10 months ago)
- Topics: brainfuck, brainfuck-compiler, compiler, llvm
- Language: C++
- Homepage:
- Size: 44.9 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# An optimizing LLVM Brainf*ck compiler
An optimizing [brainf*ck](http://brainfuck.org/brainfuck.html) compiler using LLVM.
Some optimizations are applied before code generation:
* Zero-ing loops (`[+]` / `[-]`) are represented as a single instruction
* Consecutive move and add instructions are merged into single instructions with
an amount parameterThen LLVM optimizations are applied on the generated LLVM IR. As an example of these
powerful optimizations, brainf*ck HelloWorld will be optimized to simple `putchar` calls:```llvm
// Input
>++++++++[<+++++++++>-]<.>++++[<+++++++>-]<+.+++++++..+++.
>>++++++[<+++++++>-]<++.------------.>++++++[<+++++++++>-]
<+.<.+++.------.--------.>>>++++[<++++++++>-]<+.// Optimized IR output
define i32 @main() local_unnamed_addr #0 {
entry:
%0 = tail call i32 @putchar(i32 72)
%1 = tail call i32 @putchar(i32 101)
%2 = tail call i32 @putchar(i32 108)
%3 = tail call i32 @putchar(i32 108)
%4 = tail call i32 @putchar(i32 111)
%5 = tail call i32 @putchar(i32 44)
%6 = tail call i32 @putchar(i32 32)
%7 = tail call i32 @putchar(i32 87)
%8 = tail call i32 @putchar(i32 111)
%9 = tail call i32 @putchar(i32 114)
%10 = tail call i32 @putchar(i32 108)
%11 = tail call i32 @putchar(i32 100)
%12 = tail call i32 @putchar(i32 33)
ret i32 0
}
```## Building and Executing
```shell
$ cmake -G Ninja -B build
$ ninja -C build
$ build/bf examples/helloworld.bf -o build/helloworld.ll
$ lli build/helloworld.ll
```## Other targets
This project is a port of [bf](https://github.com/mrjameshamilton/bf) which is a brainf*ck compiler with backends for:
* JVM
* Smali
* Dex
* C
* LLVM IR
* ARM assembly
* WASM
* JavaScript
* Lox# Useful brainf*ck resources
* [Brainf*ck language reference](http://brainfuck.org/brainfuck.html)
* [Sample programs by Daniel B Cristofani](http://brainfuck.org/)
* [Optimizing brainf*ck programs](http://calmerthanyouare.org/2015/01/07/optimizing-brainfuck.html)
* [Brainf*ck Wikipedia article](https://en.wikipedia.org/wiki/Brainfuck)