1 //===-- BrainFDriver.cpp - BrainF compiler driver -----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===--------------------------------------------------------------------===//
10 // This program converts the BrainF language into LLVM assembly,
11 // which it can then run using the JIT or output as BitCode.
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.
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
23 // lli prog.bf.bc #Run generated BitCode
25 //===--------------------------------------------------------------------===//
28 #include "llvm/Analysis/Verifier.h"
29 #include "llvm/Bitcode/ReaderWriter.h"
30 #include "llvm/ExecutionEngine/GenericValue.h"
31 #include "llvm/ExecutionEngine/JIT.h"
32 #include "llvm/IR/Constants.h"
33 #include "llvm/Support/CommandLine.h"
34 #include "llvm/Support/ManagedStatic.h"
35 #include "llvm/Support/TargetSelect.h"
36 #include "llvm/Support/raw_ostream.h"
41 //Command line options
43 static cl::opt<std::string>
44 InputFilename(cl::Positional, cl::desc("<input brainf>"));
46 static cl::opt<std::string>
47 OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
50 ArrayBoundsChecking("abc", cl::desc("Enable array bounds checking"));
53 JIT("jit", cl::desc("Run program Just-In-Time"));
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::getInt32Ty(mod->getContext()),
61 IntegerType::getInt32Ty(mod->getContext()),
62 PointerType::getUnqual(PointerType::getUnqual(
63 IntegerType::getInt8Ty(mod->getContext()))), NULL));
65 Function::arg_iterator args = main_func->arg_begin();
66 Value *arg_0 = args++;
67 arg_0->setName("argc");
68 Value *arg_1 = args++;
69 arg_1->setName("argv");
73 BasicBlock *bb = BasicBlock::Create(mod->getContext(), "main.0", main_func);
77 CallInst *brainf_call = CallInst::Create(mod->getFunction("brainf"),
79 brainf_call->setTailCall(false);
83 ReturnInst::Create(mod->getContext(),
84 ConstantInt::get(mod->getContext(), APInt(32, 0)), bb);
87 int main(int argc, char **argv) {
88 cl::ParseCommandLineOptions(argc, argv, " BrainF compiler\n");
90 LLVMContext &Context = getGlobalContext();
92 if (InputFilename == "") {
93 errs() << "Error: You must specify the filename of the program to "
94 "be compiled. Use --help to see the options.\n";
98 //Get the output stream
99 raw_ostream *out = &outs();
101 if (OutputFilename == "") {
102 std::string base = InputFilename;
103 if (InputFilename == "-") { base = "a"; }
105 // Use default filename.
106 OutputFilename = base+".bc";
108 if (OutputFilename != "-") {
110 out = new raw_fd_ostream(OutputFilename.c_str(), ErrInfo,
111 raw_fd_ostream::F_Binary);
115 //Get the input stream
116 std::istream *in = &std::cin;
117 if (InputFilename != "-")
118 in = new std::ifstream(InputFilename.c_str());
120 //Gather the compile flags
121 BrainF::CompileFlags cf = BrainF::flag_off;
122 if (ArrayBoundsChecking)
123 cf = BrainF::CompileFlags(cf | BrainF::flag_arraybounds);
125 //Read the BrainF program
127 Module *mod = bf.parse(in, 65536, cf, Context); //64 KiB
130 addMainFunction(mod);
132 //Verify generated code
133 if (verifyModule(*mod)) {
134 errs() << "Error: module failed verification. This shouldn't happen.\n";
140 InitializeNativeTarget();
142 outs() << "------- Running JIT -------\n";
143 ExecutionEngine *ee = EngineBuilder(mod).create();
144 std::vector<GenericValue> args;
145 Function *brainf_func = mod->getFunction("brainf");
146 GenericValue gv = ee->runFunction(brainf_func, args);
148 WriteBitcodeToFile(mod, *out);