projects
/
oota-llvm.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Merging r258436:
[oota-llvm.git]
/
examples
/
BrainF
/
BrainFDriver.cpp
diff --git
a/examples/BrainF/BrainFDriver.cpp
b/examples/BrainF/BrainFDriver.cpp
index 0a24d7b665f8919f61b762f011a296f6c192b824..1a38c67b0d4a0fc23b46dea5188a8266268435a8 100644
(file)
--- a/
examples/BrainF/BrainFDriver.cpp
+++ b/
examples/BrainF/BrainFDriver.cpp
@@
-21,22
+21,22
@@
// ./BrainF prog.bf #Write as BitCode
//
// lli prog.bf.bc #Run generated BitCode
// ./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 "BrainF.h"
-#include "llvm/Constants.h"
-#include "llvm/ModuleProvider.h"
-#include "llvm/Analysis/Verifier.h"
#include "llvm/Bitcode/ReaderWriter.h"
#include "llvm/Bitcode/ReaderWriter.h"
+#include "llvm/ExecutionEngine/ExecutionEngine.h"
#include "llvm/ExecutionEngine/GenericValue.h"
#include "llvm/ExecutionEngine/GenericValue.h"
-#include "llvm/ExecutionEngine/JIT.h"
+#include "llvm/IR/Constants.h"
+#include "llvm/IR/Verifier.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/FileSystem.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/ManagedStatic.h"
+#include "llvm/Support/TargetSelect.h"
+#include "llvm/Support/raw_ostream.h"
#include <fstream>
#include <iostream>
#include <fstream>
#include <iostream>
-
using namespace llvm;
//Command line options
using namespace llvm;
//Command line options
@@
-58,19
+58,20
@@
JIT("jit", cl::desc("Run program Just-In-Time"));
void addMainFunction(Module *mod) {
//define i32 @main(i32 %argc, i8 **%argv)
Function *main_func = cast<Function>(mod->
void addMainFunction(Module *mod) {
//define i32 @main(i32 %argc, i8 **%argv)
Function *main_func = cast<Function>(mod->
- getOrInsertFunction("main", IntegerType::Int32Ty, IntegerType::Int32Ty,
+ getOrInsertFunction("main", IntegerType::getInt32Ty(mod->getContext()),
+ IntegerType::getInt32Ty(mod->getContext()),
PointerType::getUnqual(PointerType::getUnqual(
PointerType::getUnqual(PointerType::getUnqual(
- IntegerType::
Int8Ty
)), NULL));
+ IntegerType::
getInt8Ty(mod->getContext())
)), NULL));
{
Function::arg_iterator args = main_func->arg_begin();
{
Function::arg_iterator args = main_func->arg_begin();
- Value *arg_0 = args++;
+ Value *arg_0 =
&*
args++;
arg_0->setName("argc");
arg_0->setName("argc");
- Value *arg_1 = args++;
+ Value *arg_1 =
&*
args++;
arg_1->setName("argv");
}
//main.0:
arg_1->setName("argv");
}
//main.0:
- BasicBlock *bb = BasicBlock::Create("main.0", main_func);
+ BasicBlock *bb = BasicBlock::Create(
mod->getContext(),
"main.0", main_func);
//call void @brainf()
{
//call void @brainf()
{
@@
-80,75
+81,77
@@
void addMainFunction(Module *mod) {
}
//ret i32 0
}
//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");
}
int main(int argc, char **argv) {
cl::ParseCommandLineOptions(argc, argv, " BrainF compiler\n");
+ LLVMContext &Context = getGlobalContext();
+
if (InputFilename == "") {
if (InputFilename == "") {
-
cerr<<
"Error: You must specify the filename of the program to "
-
"be compiled. Use --help to see the options.\n";
+
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
abort();
}
//Get the output stream
-
std::ostream *out = &std::cout
;
+
raw_ostream *out = &outs()
;
if (!JIT) {
if (OutputFilename == "") {
std::string base = InputFilename;
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 != "-") {
}
if (OutputFilename != "-") {
- out = new std::
- ofstream(OutputFilename.c_str(),
- std::ios::out | std::ios::trunc | std::ios::binary);
+ std::error_code EC;
+ out = new raw_fd_ostream(OutputFilename, EC, sys::fs::F_None);
}
}
//Get the input stream
std::istream *in = &std::cin;
}
}
//Get the input stream
std::istream *in = &std::cin;
- if (InputFilename != "-")
{
+ if (InputFilename != "-")
in = new std::ifstream(InputFilename.c_str());
in = new std::ifstream(InputFilename.c_str());
- }
//Gather the compile flags
BrainF::CompileFlags cf = BrainF::flag_off;
//Gather the compile flags
BrainF::CompileFlags cf = BrainF::flag_off;
- if (ArrayBoundsChecking)
{
+ if (ArrayBoundsChecking)
cf = BrainF::CompileFlags(cf | BrainF::flag_arraybounds);
cf = BrainF::CompileFlags(cf | BrainF::flag_arraybounds);
- }
//Read the BrainF program
BrainF bf;
//Read the BrainF program
BrainF bf;
- Module *mod = bf.parse(in, 65536, cf); //64 KiB
- if (in != &std::cin) {delete in;}
- addMainFunction(mod);
+ std::unique_ptr<Module> Mod(bf.parse(in, 65536, cf, Context)); // 64 KiB
+ if (in != &std::cin)
+ delete in;
+ addMainFunction(Mod.get());
//Verify generated code
//Verify generated code
- if (verifyModule(*
m
od)) {
-
cerr<<
"Error: module failed verification. This shouldn't happen.\n";
+ if (verifyModule(*
M
od)) {
+
errs() <<
"Error: module failed verification. This shouldn't happen.\n";
abort();
}
//Write it out
if (JIT) {
abort();
}
//Write it out
if (JIT) {
- cout<<"------- Running JIT -------\n";
- ExistingModuleProvider *mp = new ExistingModuleProvider(mod);
- ExecutionEngine *ee = ExecutionEngine::create(mp, false);
+ InitializeNativeTarget();
+
+ outs() << "------- Running JIT -------\n";
+ Module &M = *Mod;
+ ExecutionEngine *ee = EngineBuilder(std::move(Mod)).create();
std::vector<GenericValue> args;
std::vector<GenericValue> args;
- Function *brainf_func =
mod->
getFunction("brainf");
+ Function *brainf_func =
M.
getFunction("brainf");
GenericValue gv = ee->runFunction(brainf_func, args);
} else {
GenericValue gv = ee->runFunction(brainf_func, args);
} else {
- WriteBitcodeToFile(
mod
, *out);
+ WriteBitcodeToFile(
Mod.get()
, *out);
}
//Clean up
}
//Clean up
- if (out != &
std::cout) {delete out;}
-
delete mod
;
+ if (out != &
outs())
+
delete out
;
llvm_shutdown();
llvm_shutdown();