remove attributions from examples.
[oota-llvm.git] / examples / BrainF / BrainFDriver.cpp
1 //===-- BrainFDriver.cpp - BrainF compiler driver -----------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
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>
39
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->
61     getOrInsertFunction("main", IntegerType::Int32Ty, IntegerType::Int32Ty,
62                         PointerType::getUnqual(PointerType::getUnqual(
63                           IntegerType::Int8Ty)), NULL));
64   {
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");
70   }
71
72   //main.0:
73   BasicBlock *bb = new BasicBlock("main.0", main_func);
74
75   //call void @brainf()
76   {
77     CallInst *brainf_call = new CallInst(mod->getFunction("brainf"),
78                                          "", bb);
79     brainf_call->setTailCall(false);
80   }
81
82   //ret i32 0
83   new ReturnInst(ConstantInt::get(APInt(32, 0)), bb);
84 }
85
86 int main(int argc, char **argv) {
87   cl::ParseCommandLineOptions(argc, argv, " BrainF compiler\n");
88
89   if (InputFilename == "") {
90     cerr<<"Error: You must specify the filename of the program to "
91           "be compiled.  Use --help to see the options.\n";
92     abort();
93   }
94
95   //Get the output stream
96   std::ostream *out = &std::cout;
97   if (!JIT) {
98     if (OutputFilename == "") {
99       std::string base = InputFilename;
100       if (InputFilename == "-") {base = "a";}
101
102       //Use default filename
103       const char *suffix = ".bc";
104       OutputFilename = base+suffix;
105     }
106     if (OutputFilename != "-") {
107       out = new std::
108         ofstream(OutputFilename.c_str(),
109                  std::ios::out | std::ios::trunc | std::ios::binary);
110     }
111   }
112
113   //Get the input stream
114   std::istream *in = &std::cin;
115   if (InputFilename != "-") {
116     in = new std::ifstream(InputFilename.c_str());
117   }
118
119   //Gather the compile flags
120   BrainF::CompileFlags cf = BrainF::flag_off;
121   if (ArrayBoundsChecking) {
122     cf = BrainF::CompileFlags(cf | BrainF::flag_arraybounds);
123   }
124
125   //Read the BrainF program
126   BrainF bf;
127   Module *mod = bf.parse(in, 65536, cf); //64 KiB
128   if (in != &std::cin) {delete in;}
129   addMainFunction(mod);
130
131   //Verify generated code
132   if (verifyModule(*mod)) {
133     cerr<<"Error: module failed verification.  This shouldn't happen.\n";
134     abort();
135   }
136
137   //Write it out
138   if (JIT) {
139     cout<<"------- Running JIT -------\n";
140     ExistingModuleProvider *mp = new ExistingModuleProvider(mod);
141     ExecutionEngine *ee = ExecutionEngine::create(mp, false);
142     std::vector<GenericValue> args;
143     Function *brainf_func = mod->getFunction("brainf");
144     GenericValue gv = ee->runFunction(brainf_func, args);
145   } else {
146     WriteBitcodeToFile(mod, *out);
147   }
148
149   //Clean up
150   if (out != &std::cout) {delete out;}
151   delete mod;
152
153   llvm_shutdown();
154
155   return 0;
156 }