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" |
Chris Lattner | da06288 | 2009-06-17 16:48:44 +0000 | [diff] [blame] | 37 | #include "llvm/Target/TargetSelect.h" |
Chris Lattner | bef8e0b | 2007-09-12 18:24:00 +0000 | [diff] [blame] | 38 | #include <fstream> |
| 39 | #include <iostream> |
Chris Lattner | bef8e0b | 2007-09-12 18:24:00 +0000 | [diff] [blame] | 40 | using namespace llvm; |
| 41 | |
| 42 | //Command line options |
| 43 | |
| 44 | static cl::opt<std::string> |
| 45 | InputFilename(cl::Positional, cl::desc("<input brainf>")); |
| 46 | |
| 47 | static cl::opt<std::string> |
| 48 | OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename")); |
| 49 | |
| 50 | static cl::opt<bool> |
| 51 | ArrayBoundsChecking("abc", cl::desc("Enable array bounds checking")); |
| 52 | |
| 53 | static cl::opt<bool> |
| 54 | JIT("jit", cl::desc("Run program Just-In-Time")); |
| 55 | |
| 56 | |
| 57 | //Add main function so can be fully compiled |
| 58 | void addMainFunction(Module *mod) { |
| 59 | //define i32 @main(i32 %argc, i8 **%argv) |
| 60 | Function *main_func = cast<Function>(mod-> |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 61 | getOrInsertFunction("main", IntegerType::getInt32Ty(mod->getContext()), |
| 62 | IntegerType::getInt32Ty(mod->getContext()), |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 63 | PointerType::getUnqual(PointerType::getUnqual( |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 64 | IntegerType::getInt8Ty(mod->getContext()))), NULL)); |
Chris Lattner | bef8e0b | 2007-09-12 18:24:00 +0000 | [diff] [blame] | 65 | { |
| 66 | Function::arg_iterator args = main_func->arg_begin(); |
| 67 | Value *arg_0 = args++; |
| 68 | arg_0->setName("argc"); |
| 69 | Value *arg_1 = args++; |
| 70 | arg_1->setName("argv"); |
| 71 | } |
| 72 | |
| 73 | //main.0: |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 74 | BasicBlock *bb = BasicBlock::Create(mod->getContext(), "main.0", main_func); |
Chris Lattner | bef8e0b | 2007-09-12 18:24:00 +0000 | [diff] [blame] | 75 | |
| 76 | //call void @brainf() |
| 77 | { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 78 | CallInst *brainf_call = CallInst::Create(mod->getFunction("brainf"), |
| 79 | "", bb); |
Chris Lattner | bef8e0b | 2007-09-12 18:24:00 +0000 | [diff] [blame] | 80 | brainf_call->setTailCall(false); |
| 81 | } |
| 82 | |
| 83 | //ret i32 0 |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 84 | ReturnInst::Create(mod->getContext(), |
| 85 | ConstantInt::get(mod->getContext(), APInt(32, 0)), bb); |
Chris Lattner | bef8e0b | 2007-09-12 18:24:00 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | int main(int argc, char **argv) { |
| 89 | cl::ParseCommandLineOptions(argc, argv, " BrainF compiler\n"); |
| 90 | |
Owen Anderson | 001dbfe | 2009-07-16 18:04:31 +0000 | [diff] [blame] | 91 | LLVMContext &Context = getGlobalContext(); |
Owen Anderson | 8b477ed | 2009-07-01 16:58:40 +0000 | [diff] [blame] | 92 | |
Chris Lattner | bef8e0b | 2007-09-12 18:24:00 +0000 | [diff] [blame] | 93 | if (InputFilename == "") { |
Chris Lattner | ef5dc36 | 2008-08-23 22:00:15 +0000 | [diff] [blame] | 94 | std::cerr<<"Error: You must specify the filename of the program to " |
| 95 | "be compiled. Use --help to see the options.\n"; |
Chris Lattner | bef8e0b | 2007-09-12 18:24:00 +0000 | [diff] [blame] | 96 | abort(); |
| 97 | } |
| 98 | |
| 99 | //Get the output stream |
| 100 | std::ostream *out = &std::cout; |
| 101 | if (!JIT) { |
| 102 | if (OutputFilename == "") { |
| 103 | std::string base = InputFilename; |
| 104 | if (InputFilename == "-") {base = "a";} |
| 105 | |
| 106 | //Use default filename |
| 107 | const char *suffix = ".bc"; |
| 108 | OutputFilename = base+suffix; |
| 109 | } |
| 110 | if (OutputFilename != "-") { |
| 111 | out = new std:: |
| 112 | ofstream(OutputFilename.c_str(), |
| 113 | std::ios::out | std::ios::trunc | std::ios::binary); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | //Get the input stream |
| 118 | std::istream *in = &std::cin; |
| 119 | if (InputFilename != "-") { |
| 120 | in = new std::ifstream(InputFilename.c_str()); |
| 121 | } |
| 122 | |
| 123 | //Gather the compile flags |
| 124 | BrainF::CompileFlags cf = BrainF::flag_off; |
| 125 | if (ArrayBoundsChecking) { |
| 126 | cf = BrainF::CompileFlags(cf | BrainF::flag_arraybounds); |
| 127 | } |
| 128 | |
| 129 | //Read the BrainF program |
| 130 | BrainF bf; |
Owen Anderson | 31895e7 | 2009-07-01 21:22:36 +0000 | [diff] [blame] | 131 | Module *mod = bf.parse(in, 65536, cf, Context); //64 KiB |
Chris Lattner | bef8e0b | 2007-09-12 18:24:00 +0000 | [diff] [blame] | 132 | if (in != &std::cin) {delete in;} |
| 133 | addMainFunction(mod); |
| 134 | |
| 135 | //Verify generated code |
| 136 | if (verifyModule(*mod)) { |
Chris Lattner | ef5dc36 | 2008-08-23 22:00:15 +0000 | [diff] [blame] | 137 | std::cerr<<"Error: module failed verification. This shouldn't happen.\n"; |
Chris Lattner | bef8e0b | 2007-09-12 18:24:00 +0000 | [diff] [blame] | 138 | abort(); |
| 139 | } |
| 140 | |
| 141 | //Write it out |
| 142 | if (JIT) { |
Chris Lattner | da06288 | 2009-06-17 16:48:44 +0000 | [diff] [blame] | 143 | InitializeNativeTarget(); |
| 144 | |
Chris Lattner | ef5dc36 | 2008-08-23 22:00:15 +0000 | [diff] [blame] | 145 | std::cout << "------- Running JIT -------\n"; |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 146 | ExecutionEngine *ee = EngineBuilder(mod).create(); |
Chris Lattner | bef8e0b | 2007-09-12 18:24:00 +0000 | [diff] [blame] | 147 | std::vector<GenericValue> args; |
| 148 | Function *brainf_func = mod->getFunction("brainf"); |
| 149 | GenericValue gv = ee->runFunction(brainf_func, args); |
| 150 | } else { |
| 151 | WriteBitcodeToFile(mod, *out); |
| 152 | } |
| 153 | |
| 154 | //Clean up |
| 155 | if (out != &std::cout) {delete out;} |
| 156 | delete mod; |
| 157 | |
| 158 | llvm_shutdown(); |
| 159 | |
| 160 | return 0; |
| 161 | } |