caozhanhao/llvm-brainfuck

A LLVM pass that lowers LLVM IR to Brainfuck.

C++

7

14 commits

updated Jan 8, 2026

See the code

See what people are saying

SourceMessageScoreDate

I wrote a ray tracer in Brainfuck

There have been some other attempts to build compilers that output brainfuck. This one supports some LLVM IR instructions: https://github.com/caozhanhao/llvm-brainfuck There's a list of others here, with asm2bf seeming the most complete: https://esolangs.org/wiki/Brainfuck_code_generation

0

Sep 26, 2026

README

llvm-brainfuck

A LLVM pass that lowers LLVM IR to Brainfuck.

Work in progress -- not all instructions and types are supported yet.

Quick Start

mkdir build && cd build
cmake .. && make
clang -O1 -S -fpass-plugin=./lib/libLLVMBrainfuck.so -o /dev/null ../tests/c/helloworld.c > helloworld.bf
./interpreter/BFInterp helloworld.bf
 
# For opt, use `-load-pass-plugin` 
# opt -load-pass-plugin ./lib/libLLVMBrainfuck.so -disable-output xxx.ll

Progress

  • Basic support for arithmetic, memory and function calls.
  • Currently, all types are treated as i8 and conversion are simply ignored.
  • ⚑ Fun Fact: Now it can compile a Brainfuck interpreter to Brainfuck. (See: tests/c/bf-interp.c)

Supported Instructions

Terminator Instructions

  • ret
  • br
  • switch
  • indirectbr
  • invoke
  • callbr
  • resume
  • catchswitch
  • catchret
  • cleanupret
  • unreachable

Unary Operations

  • fneg

Binary Operations

  • add
  • fadd
  • sub
  • fsub
  • mul
  • fmul
  • udiv
  • sdiv
  • fdiv
  • urem
  • srem
  • frem

Bitwise Binary Operations

  • shl
  • lshr
  • ashr
  • and
  • or
  • xor

Vector Operations

  • extractelement
  • insertelement
  • shufflevector

Aggregate Operations

  • extractvalue
  • insertvalue

Memory Access and Addressing Operations

  • alloca
  • load
  • store
  • fence
  • cmpxchg
  • atomicrmw
  • getelementptr

Conversion Operations

  • trunc ... to
  • zext ... to
  • sext ... to
  • fptrunc ... to
  • fpext ... to
  • fptoui ... to
  • fptosi ... to
  • uitofp ... to
  • sitofp ... to
  • ptrtoint ... to
  • inttoptr ... to
  • bitcast ... to
  • addrspacecast ... to

Other Operations

  • icmp
  • fcmp
  • phi (demote to memory)
  • select
  • freeze
  • call
  • va_arg
  • landingpad
  • catchpad
  • cleanuppad

Supported Types

  • i8
  • ...

License

This project is licensed under the MIT License.
See LICENSE for details.

brainfuck
esolang
llvm
llvm-ir
llvm-pass

Contributors

caozhanhao

14 commits

caozhanhao/llvm-brainfuck

A LLVM pass that lowers LLVM IR to Brainfuck.

C++

7

14 commits

updated Jan 8, 2026

See the code

See what people are saying

SourceMessageScoreDate

I wrote a ray tracer in Brainfuck

There have been some other attempts to build compilers that output brainfuck. This one supports some LLVM IR instructions: https://github.com/caozhanhao/llvm-brainfuck There's a list of others here, with asm2bf seeming the most complete: https://esolangs.org/wiki/Brainfuck_code_generation

0

Sep 26, 2026

README

llvm-brainfuck

A LLVM pass that lowers LLVM IR to Brainfuck.

Work in progress -- not all instructions and types are supported yet.

Quick Start

mkdir build && cd build
cmake .. && make
clang -O1 -S -fpass-plugin=./lib/libLLVMBrainfuck.so -o /dev/null ../tests/c/helloworld.c > helloworld.bf
./interpreter/BFInterp helloworld.bf
 
# For opt, use `-load-pass-plugin` 
# opt -load-pass-plugin ./lib/libLLVMBrainfuck.so -disable-output xxx.ll

Progress

  • Basic support for arithmetic, memory and function calls.
  • Currently, all types are treated as i8 and conversion are simply ignored.
  • ⚑ Fun Fact: Now it can compile a Brainfuck interpreter to Brainfuck. (See: tests/c/bf-interp.c)

Supported Instructions

Terminator Instructions

  • ret
  • br
  • switch
  • indirectbr
  • invoke
  • callbr
  • resume
  • catchswitch
  • catchret
  • cleanupret
  • unreachable

Unary Operations

  • fneg

Binary Operations

  • add
  • fadd
  • sub
  • fsub
  • mul
  • fmul
  • udiv
  • sdiv
  • fdiv
  • urem
  • srem
  • frem

Bitwise Binary Operations

  • shl
  • lshr
  • ashr
  • and
  • or
  • xor

Vector Operations

  • extractelement
  • insertelement
  • shufflevector

Aggregate Operations

  • extractvalue
  • insertvalue

Memory Access and Addressing Operations

  • alloca
  • load
  • store
  • fence
  • cmpxchg
  • atomicrmw
  • getelementptr

Conversion Operations

  • trunc ... to
  • zext ... to
  • sext ... to
  • fptrunc ... to
  • fpext ... to
  • fptoui ... to
  • fptosi ... to
  • uitofp ... to
  • sitofp ... to
  • ptrtoint ... to
  • inttoptr ... to
  • bitcast ... to
  • addrspacecast ... to

Other Operations

  • icmp
  • fcmp
  • phi (demote to memory)
  • select
  • freeze
  • call
  • va_arg
  • landingpad
  • catchpad
  • cleanuppad

Supported Types

  • i8
  • ...

License

This project is licensed under the MIT License.
See LICENSE for details.

brainfuck
esolang
llvm
llvm-ir
llvm-pass

Contributors

caozhanhao

14 commits

Languages

C++

92.4%

C

6.4%