├── .gitignore ├── LICENSE ├── Makefile ├── README.md ├── llvm-brainfuck-pipeline ├── main.cpp └── shim.c /.gitignore: -------------------------------------------------------------------------------- 1 | llvm-brainfuck 2 | *.o 3 | *.a 4 | *.swp 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (C) 2012 Jeremy Roman 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | PROGRAM := llvm-brainfuck 2 | OBJECTS := main.o 3 | 4 | SHIM := shim.a 5 | SHIM_OBJECTS := shim.o 6 | 7 | CC := clang 8 | CXX := clang++ 9 | CXXFLAGS := $(shell llvm-config --cppflags) -Wall -Werror -pedantic 10 | LDFLAGS := $(shell llvm-config --ldflags --libs core) 11 | 12 | all: $(PROGRAM) $(SHIM) 13 | 14 | $(PROGRAM): $(OBJECTS) 15 | $(CXX) -o $@ $^ $(LDFLAGS) 16 | 17 | $(SHIM): $(SHIM_OBJECTS) 18 | ar rcs $@ $^ 19 | 20 | clean: 21 | rm -f $(PROGRAM) $(OBJECTS) 22 | 23 | .PHONY: clean all 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | llvm-brainfuck 2 | ============== 3 | 4 | An LLVM-based compiler for Brainfuck. Created as an experiment to try out the LLVM API. 5 | 6 | For more information, see my [blog post][blog] about it. 7 | 8 | [blog]: http://www.jeremyroman.com/2012/12/11/building-a-brainfuck-compiler-with-llvm 9 | -------------------------------------------------------------------------------- /llvm-brainfuck-pipeline: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | ./llvm-brainfuck | llvm-as | opt -instcombine -print-module | llc 3 | -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace llvm; 11 | 12 | static const uint64_t DATA_SIZE = 30000; 13 | static const char *DATA_NAME = "data"; 14 | static const char *PTR_NAME = "ptr"; 15 | 16 | struct Loop { 17 | BasicBlock *Entry, *Body, *Exit; 18 | PHINode *DataPtrBody, *DataPtrExit; 19 | }; 20 | 21 | int main() { 22 | // LLVM context. 23 | LLVMContext &Context = getGlobalContext(); 24 | Module MainModule("brainfuck program", Context); 25 | IRBuilder<> Builder(Context); 26 | 27 | // Some useful constants. 28 | Type *CellType = Type::getInt8Ty(Context); 29 | Constant *One = ConstantInt::get(CellType, 1); 30 | 31 | // Function prototypes for the shim. 32 | FunctionType *PutFunctionType = FunctionType::get( 33 | Type::getVoidTy(Context) /* return type */, 34 | std::vector(1, CellType) /* argument types */, 35 | false /* var args */); 36 | Function *PutFunction = Function::Create(PutFunctionType, 37 | Function::ExternalLinkage, "brainfuck_put", &MainModule); 38 | FunctionType *GetFunctionType = FunctionType::get( 39 | CellType /* return type */, 40 | std::vector() /* argument types */, 41 | false /* var args */); 42 | Function *GetFunction = Function::Create(GetFunctionType, 43 | Function::ExternalLinkage, "brainfuck_get", &MainModule); 44 | 45 | // Global data for a brainfuck program. 46 | ArrayType *DataType = ArrayType::get(CellType, DATA_SIZE); 47 | GlobalVariable *Data = new GlobalVariable( 48 | MainModule, DataType, false /* constant */, 49 | GlobalVariable::InternalLinkage, Constant::getNullValue(DataType), 50 | DATA_NAME); 51 | Value *DataPtr = Builder.CreateConstInBoundsGEP2_32(Data, 0, 0, PTR_NAME); 52 | 53 | // Main function definition. 54 | FunctionType *MainFunctionType = FunctionType::get( 55 | Type::getVoidTy(Context) /* return type */, 56 | std::vector() /* argument types */, 57 | false /* var args */); 58 | Function *MainFunction = Function::Create(MainFunctionType, 59 | Function::ExternalLinkage, "brainfuck_main", &MainModule); 60 | BasicBlock *MainBlock = BasicBlock::Create(Context, "entry", MainFunction); 61 | Builder.SetInsertPoint(MainBlock); 62 | 63 | // Code generation. 64 | int c; 65 | Value *Value; 66 | std::stack Loops; 67 | Loop ThisLoop; 68 | while ((c = getchar()) != EOF) { 69 | switch (c) { 70 | case '>': 71 | DataPtr = Builder.CreateConstGEP1_32(DataPtr, 1, PTR_NAME); 72 | break; 73 | case '<': 74 | DataPtr = Builder.CreateConstGEP1_32(DataPtr, -1, PTR_NAME); 75 | break; 76 | case '+': 77 | Value = Builder.CreateLoad(DataPtr); 78 | Value = Builder.CreateAdd(Value, One); 79 | Builder.CreateStore(Value, DataPtr); 80 | break; 81 | case '-': 82 | Value = Builder.CreateLoad(DataPtr); 83 | Value = Builder.CreateSub(Value, One); 84 | Builder.CreateStore(Value, DataPtr); 85 | break; 86 | case '.': 87 | Value = Builder.CreateLoad(DataPtr); 88 | Builder.CreateCall(PutFunction, Value); 89 | break; 90 | case ',': 91 | Value = Builder.CreateCall(GetFunction); 92 | Builder.CreateStore(Value, DataPtr); 93 | break; 94 | 95 | case '[': 96 | // Prepare data for the stack. 97 | ThisLoop.Entry = Builder.GetInsertBlock(); 98 | ThisLoop.Body = BasicBlock::Create(Context, "loop", MainFunction); 99 | ThisLoop.Exit = BasicBlock::Create(Context, "exit", MainFunction); 100 | 101 | // Emit the beginning conditional branch. 102 | Value = Builder.CreateLoad(DataPtr); 103 | Value = Builder.CreateIsNotNull(Value); 104 | Builder.CreateCondBr(Value, ThisLoop.Body, ThisLoop.Exit); 105 | 106 | // Define the pointer after the loop. 107 | Builder.SetInsertPoint(ThisLoop.Exit); 108 | ThisLoop.DataPtrExit = Builder.CreatePHI(DataPtr->getType(), 2, 109 | PTR_NAME); 110 | ThisLoop.DataPtrExit->addIncoming(DataPtr, ThisLoop.Entry); 111 | 112 | // Define the pointer within the loop. 113 | Builder.SetInsertPoint(ThisLoop.Body); 114 | ThisLoop.DataPtrBody = Builder.CreatePHI(DataPtr->getType(), 2, 115 | PTR_NAME); 116 | ThisLoop.DataPtrBody->addIncoming(DataPtr, ThisLoop.Entry); 117 | 118 | // Continue generating code within the loop. 119 | DataPtr = ThisLoop.DataPtrBody; 120 | Loops.push(ThisLoop); 121 | break; 122 | 123 | case ']': 124 | // Pop data off the stack. 125 | if (Loops.empty()) { 126 | fputs("] requires matching [\n", stderr); 127 | abort(); 128 | } 129 | ThisLoop = Loops.top(); 130 | Loops.pop(); 131 | 132 | // Finish off the phi nodes. 133 | ThisLoop.DataPtrBody->addIncoming(DataPtr, Builder.GetInsertBlock()); 134 | ThisLoop.DataPtrExit->addIncoming(DataPtr, Builder.GetInsertBlock()); 135 | 136 | // Emit the ending conditional branch. 137 | Value = Builder.CreateLoad(DataPtr); 138 | Value = Builder.CreateIsNotNull(Value); 139 | Builder.CreateCondBr(Value, ThisLoop.Body, ThisLoop.Exit); 140 | 141 | // Move insertion after the loop. 142 | ThisLoop.Exit->moveAfter(Builder.GetInsertBlock()); 143 | DataPtr = ThisLoop.DataPtrExit; 144 | Builder.SetInsertPoint(ThisLoop.Exit); 145 | break; 146 | } 147 | } 148 | 149 | // Ensure all loops have been closed. 150 | if (!Loops.empty()) { 151 | fputs("[ requires matching ]\n", stderr); 152 | abort(); 153 | } 154 | 155 | // Finish off brainfuck_main and dump. 156 | Builder.CreateRetVoid(); 157 | MainModule.print(outs(), NULL /* assembly annotation writer */); 158 | } 159 | -------------------------------------------------------------------------------- /shim.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | extern void brainfuck_main(); 4 | 5 | int main(int argc, char **argv) { 6 | brainfuck_main(); 7 | return 0; 8 | } 9 | 10 | void brainfuck_put(char c) { 11 | putchar(c); 12 | } 13 | 14 | char brainfuck_get() { 15 | int c = getchar(); 16 | return c >= 0 ? c : 0; 17 | } 18 | --------------------------------------------------------------------------------