X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=examples%2FBrainF%2FBrainFDriver.cpp;h=cd6eabfdffaa5e07135301ee6e6d8cda48cb195c;hb=0a230e0d985625a3909cb78fd867a3abaf434565;hp=06e77d2e2a3ec2a6338490d0ecc6d3d4fe08eab6;hpb=da06288aeb28393b937e17dcd180658c3737a6e5;p=oota-llvm.git diff --git a/examples/BrainF/BrainFDriver.cpp b/examples/BrainF/BrainFDriver.cpp index 06e77d2e2a3..cd6eabfdffa 100644 --- a/examples/BrainF/BrainFDriver.cpp +++ b/examples/BrainF/BrainFDriver.cpp @@ -21,20 +21,19 @@ // ./BrainF prog.bf #Write as BitCode // // lli prog.bf.bc #Run generated BitCode -// llvm-ld -native -o=prog prog.bf.bc #Compile BitCode into native executable // //===--------------------------------------------------------------------===// #include "BrainF.h" -#include "llvm/Constants.h" -#include "llvm/ModuleProvider.h" #include "llvm/Analysis/Verifier.h" #include "llvm/Bitcode/ReaderWriter.h" #include "llvm/ExecutionEngine/GenericValue.h" #include "llvm/ExecutionEngine/JIT.h" +#include "llvm/IR/Constants.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/ManagedStatic.h" -#include "llvm/Target/TargetSelect.h" +#include "llvm/Support/TargetSelect.h" +#include "llvm/Support/raw_ostream.h" #include #include using namespace llvm; @@ -58,9 +57,10 @@ JIT("jit", cl::desc("Run program Just-In-Time")); void addMainFunction(Module *mod) { //define i32 @main(i32 %argc, i8 **%argv) Function *main_func = cast(mod-> - getOrInsertFunction("main", IntegerType::Int32Ty, IntegerType::Int32Ty, + getOrInsertFunction("main", IntegerType::getInt32Ty(mod->getContext()), + IntegerType::getInt32Ty(mod->getContext()), PointerType::getUnqual(PointerType::getUnqual( - IntegerType::Int8Ty)), NULL)); + IntegerType::getInt8Ty(mod->getContext()))), NULL)); { Function::arg_iterator args = main_func->arg_begin(); Value *arg_0 = args++; @@ -70,7 +70,7 @@ void addMainFunction(Module *mod) { } //main.0: - BasicBlock *bb = BasicBlock::Create("main.0", main_func); + BasicBlock *bb = BasicBlock::Create(mod->getContext(), "main.0", main_func); //call void @brainf() { @@ -80,57 +80,58 @@ void addMainFunction(Module *mod) { } //ret i32 0 - ReturnInst::Create(ConstantInt::get(APInt(32, 0)), bb); + ReturnInst::Create(mod->getContext(), + ConstantInt::get(mod->getContext(), APInt(32, 0)), bb); } int main(int argc, char **argv) { cl::ParseCommandLineOptions(argc, argv, " BrainF compiler\n"); + LLVMContext &Context = getGlobalContext(); + if (InputFilename == "") { - std::cerr<<"Error: You must specify the filename of the program to " + errs() << "Error: You must specify the filename of the program to " "be compiled. Use --help to see the options.\n"; abort(); } //Get the output stream - std::ostream *out = &std::cout; + raw_ostream *out = &outs(); if (!JIT) { if (OutputFilename == "") { std::string base = InputFilename; - if (InputFilename == "-") {base = "a";} + if (InputFilename == "-") { base = "a"; } - //Use default filename - const char *suffix = ".bc"; - OutputFilename = base+suffix; + // Use default filename. + OutputFilename = base+".bc"; } if (OutputFilename != "-") { - out = new std:: - ofstream(OutputFilename.c_str(), - std::ios::out | std::ios::trunc | std::ios::binary); + std::string ErrInfo; + out = new raw_fd_ostream(OutputFilename.c_str(), ErrInfo, + raw_fd_ostream::F_Binary); } } //Get the input stream std::istream *in = &std::cin; - if (InputFilename != "-") { + if (InputFilename != "-") in = new std::ifstream(InputFilename.c_str()); - } //Gather the compile flags BrainF::CompileFlags cf = BrainF::flag_off; - if (ArrayBoundsChecking) { + if (ArrayBoundsChecking) cf = BrainF::CompileFlags(cf | BrainF::flag_arraybounds); - } //Read the BrainF program BrainF bf; - Module *mod = bf.parse(in, 65536, cf); //64 KiB - if (in != &std::cin) {delete in;} + Module *mod = bf.parse(in, 65536, cf, Context); //64 KiB + if (in != &std::cin) + delete in; addMainFunction(mod); //Verify generated code if (verifyModule(*mod)) { - std::cerr<<"Error: module failed verification. This shouldn't happen.\n"; + errs() << "Error: module failed verification. This shouldn't happen.\n"; abort(); } @@ -138,9 +139,8 @@ int main(int argc, char **argv) { if (JIT) { InitializeNativeTarget(); - std::cout << "------- Running JIT -------\n"; - ExistingModuleProvider *mp = new ExistingModuleProvider(mod); - ExecutionEngine *ee = ExecutionEngine::create(mp, false); + outs() << "------- Running JIT -------\n"; + ExecutionEngine *ee = EngineBuilder(mod).create(); std::vector args; Function *brainf_func = mod->getFunction("brainf"); GenericValue gv = ee->runFunction(brainf_func, args); @@ -149,7 +149,8 @@ int main(int argc, char **argv) { } //Clean up - if (out != &std::cout) {delete out;} + if (out != &outs()) + delete out; delete mod; llvm_shutdown();