Chris Lattner | bef8e0b | 2007-09-12 18:24:00 +0000 | [diff] [blame] | 1 | //===-- BrainFDriver.cpp - BrainF compiler driver -----------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | fc001bb | 2007-12-29 20:37:57 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | bef8e0b | 2007-09-12 18:24:00 +0000 | [diff] [blame] | 7 | // |
| 8 | //===--------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This program converts the BrainF language into LLVM assembly, |
| 11 | // which it can then run using the JIT or output as BitCode. |
| 12 | // |
| 13 | // This implementation has a tape of 65536 bytes, |
| 14 | // with the head starting in the middle. |
| 15 | // Range checking is off by default, so be careful. |
| 16 | // It can be enabled with -abc. |
| 17 | // |
| 18 | // Use: |
| 19 | // ./BrainF -jit prog.bf #Run program now |
| 20 | // ./BrainF -jit -abc prog.bf #Run program now safely |
| 21 | // ./BrainF prog.bf #Write as BitCode |
| 22 | // |
| 23 | // lli prog.bf.bc #Run generated BitCode |
| 24 | // llvm-ld -native -o=prog prog.bf.bc #Compile BitCode into native executable |
| 25 | // |
| 26 | //===--------------------------------------------------------------------===// |
| 27 | |
| 28 | #include "BrainF.h" |
| 29 | #include "llvm/Constants.h" |
| 30 | #include "llvm/ModuleProvider.h" |
| 31 | #include "llvm/Analysis/Verifier.h" |
| 32 | #include "llvm/Bitcode/ReaderWriter.h" |
| 33 | #include "llvm/ExecutionEngine/GenericValue.h" |
| 34 | #include "llvm/ExecutionEngine/JIT.h" |
| 35 | #include "llvm/Support/CommandLine.h" |
| 36 | #include "llvm/Support/ManagedStatic.h" |
| 37 | #include <fstream> |
| 38 | #include <iostream> |
Chris Lattner | bef8e0b | 2007-09-12 18:24:00 +0000 | [diff] [blame] | 39 | using namespace llvm; |
| 40 | |
| 41 | //Command line options |
| 42 | |
| 43 | static cl::opt<std::string> |
| 44 | InputFilename(cl::Positional, cl::desc("<input brainf>")); |
| 45 | |
| 46 | static cl::opt<std::string> |
| 47 | OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename")); |
| 48 | |
| 49 | static cl::opt<bool> |
| 50 | ArrayBoundsChecking("abc", cl::desc("Enable array bounds checking")); |
| 51 | |
| 52 | static cl::opt<bool> |
| 53 | JIT("jit", cl::desc("Run program Just-In-Time")); |
| 54 | |
| 55 | |
| 56 | //Add main function so can be fully compiled |
| 57 | void addMainFunction(Module *mod) { |
| 58 | //define i32 @main(i32 %argc, i8 **%argv) |
| 59 | Function *main_func = cast<Function>(mod-> |
| 60 | getOrInsertFunction("main", IntegerType::Int32Ty, IntegerType::Int32Ty, |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 61 | PointerType::getUnqual(PointerType::getUnqual( |
Chris Lattner | bef8e0b | 2007-09-12 18:24:00 +0000 | [diff] [blame] | 62 | IntegerType::Int8Ty)), NULL)); |
| 63 | { |
| 64 | Function::arg_iterator args = main_func->arg_begin(); |
| 65 | Value *arg_0 = args++; |
| 66 | arg_0->setName("argc"); |
| 67 | Value *arg_1 = args++; |
| 68 | arg_1->setName("argv"); |
| 69 | } |
| 70 | |
| 71 | //main.0: |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 72 | BasicBlock *bb = BasicBlock::Create("main.0", main_func); |
Chris Lattner | bef8e0b | 2007-09-12 18:24:00 +0000 | [diff] [blame] | 73 | |
| 74 | //call void @brainf() |
| 75 | { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 76 | CallInst *brainf_call = CallInst::Create(mod->getFunction("brainf"), |
| 77 | "", bb); |
Chris Lattner | bef8e0b | 2007-09-12 18:24:00 +0000 | [diff] [blame] | 78 | brainf_call->setTailCall(false); |
| 79 | } |
| 80 | |
| 81 | //ret i32 0 |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 82 | ReturnInst::Create(ConstantInt::get(APInt(32, 0)), bb); |
Chris Lattner | bef8e0b | 2007-09-12 18:24:00 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | int main(int argc, char **argv) { |
| 86 | cl::ParseCommandLineOptions(argc, argv, " BrainF compiler\n"); |
| 87 | |
| 88 | if (InputFilename == "") { |
Chris Lattner | ef5dc36 | 2008-08-23 22:00:15 +0000 | [diff] [blame^] | 89 | std::cerr<<"Error: You must specify the filename of the program to " |
| 90 | "be compiled. Use --help to see the options.\n"; |
Chris Lattner | bef8e0b | 2007-09-12 18:24:00 +0000 | [diff] [blame] | 91 | abort(); |
| 92 | } |
| 93 | |
| 94 | //Get the output stream |
| 95 | std::ostream *out = &std::cout; |
| 96 | if (!JIT) { |
| 97 | if (OutputFilename == "") { |
| 98 | std::string base = InputFilename; |
| 99 | if (InputFilename == "-") {base = "a";} |
| 100 | |
| 101 | //Use default filename |
| 102 | const char *suffix = ".bc"; |
| 103 | OutputFilename = base+suffix; |
| 104 | } |
| 105 | if (OutputFilename != "-") { |
| 106 | out = new std:: |
| 107 | ofstream(OutputFilename.c_str(), |
| 108 | std::ios::out | std::ios::trunc | std::ios::binary); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | //Get the input stream |
| 113 | std::istream *in = &std::cin; |
| 114 | if (InputFilename != "-") { |
| 115 | in = new std::ifstream(InputFilename.c_str()); |
| 116 | } |
| 117 | |
| 118 | //Gather the compile flags |
| 119 | BrainF::CompileFlags cf = BrainF::flag_off; |
| 120 | if (ArrayBoundsChecking) { |
| 121 | cf = BrainF::CompileFlags(cf | BrainF::flag_arraybounds); |
| 122 | } |
| 123 | |
| 124 | //Read the BrainF program |
| 125 | BrainF bf; |
| 126 | Module *mod = bf.parse(in, 65536, cf); //64 KiB |
| 127 | if (in != &std::cin) {delete in;} |
| 128 | addMainFunction(mod); |
| 129 | |
| 130 | //Verify generated code |
| 131 | if (verifyModule(*mod)) { |
Chris Lattner | ef5dc36 | 2008-08-23 22:00:15 +0000 | [diff] [blame^] | 132 | std::cerr<<"Error: module failed verification. This shouldn't happen.\n"; |
Chris Lattner | bef8e0b | 2007-09-12 18:24:00 +0000 | [diff] [blame] | 133 | abort(); |
| 134 | } |
| 135 | |
| 136 | //Write it out |
| 137 | if (JIT) { |
Chris Lattner | ef5dc36 | 2008-08-23 22:00:15 +0000 | [diff] [blame^] | 138 | std::cout << "------- Running JIT -------\n"; |
Chris Lattner | bef8e0b | 2007-09-12 18:24:00 +0000 | [diff] [blame] | 139 | ExistingModuleProvider *mp = new ExistingModuleProvider(mod); |
| 140 | ExecutionEngine *ee = ExecutionEngine::create(mp, false); |
| 141 | std::vector<GenericValue> args; |
| 142 | Function *brainf_func = mod->getFunction("brainf"); |
| 143 | GenericValue gv = ee->runFunction(brainf_func, args); |
| 144 | } else { |
| 145 | WriteBitcodeToFile(mod, *out); |
| 146 | } |
| 147 | |
| 148 | //Clean up |
| 149 | if (out != &std::cout) {delete out;} |
| 150 | delete mod; |
| 151 | |
| 152 | llvm_shutdown(); |
| 153 | |
| 154 | return 0; |
| 155 | } |