本文整理汇总了C++中ExecutionEngine::getPointerToFunction方法的典型用法代码示例。如果您正苦于以下问题:C++ ExecutionEngine::getPointerToFunction方法的具体用法?C++ ExecutionEngine::getPointerToFunction怎么用?C++ ExecutionEngine::getPointerToFunction使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ExecutionEngine
的用法示例。
在下文中一共展示了ExecutionEngine::getPointerToFunction方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main()
{
InitializeNativeTarget();
llvm_start_multithreaded();
LLVMContext context;
string error;
OwningPtr<llvm::MemoryBuffer> fileBuf;
MemoryBuffer::getFile("hw.bc", fileBuf);
ErrorOr<Module*> m = parseBitcodeFile(fileBuf.get(), context);
ExecutionEngine *ee = ExecutionEngine::create(m.get());
Function* func = ee->FindFunctionNamed("main");
std::cout << "hop " << m.get() << " ee " << ee << " f " << func << std::endl;
typedef void (*PFN)();
PFN pfn = reinterpret_cast<PFN>(ee->getPointerToFunction(func));
pfn();
Function* f = ee->FindFunctionNamed("fib");
std::cout << "big " << f << std::endl;
// typedef std::function<int(int)> fibType;
typedef int (*fibType)(int);
fibType ffib = reinterpret_cast<fibType>(ee->getPointerToFunction(f));
std::cout << "fib " << ffib(7) << std::endl;
delete ee;
}
示例2: main
int main()
{
TestClass tc;
tc.setHealth(100);
tc.printHealth();
InitializeNativeTarget();
LLVMContext context;
string error;
auto mb = MemoryBuffer::getFile("bitcode/damage.bc");
if (!mb) {
cout << "ERROR: Failed to getFile" << endl;
return 0;
}
auto m = parseBitcodeFile(mb->get(), context);
if(!m) {
cout << "ERROR: Failed to load script file." << endl;
return 0;
}
ExecutionEngine *ee = ExecutionEngine::create(m.get());
// NOTE: Function names are mangled by the compiler.
Function* init_func = ee->FindFunctionNamed("_Z9initilizev");
if(!init_func) {
cout << "ERROR: Failed to find 'initilize' function." << endl;
return 0;
}
Function* attack_func = ee->FindFunctionNamed("_Z6attackP9TestClass");
if(!attack_func) {
cout << "ERROR: Failed to find 'attack' function." << endl;
return 0;
}
typedef void (*init_pfn)();
init_pfn initilize = reinterpret_cast<init_pfn>(ee->getPointerToFunction(init_func));
if(!initilize) {
cout << "ERROR: Failed to cast 'initilize' function." << endl;
return 0;
}
initilize();
cout << "Running attacking script..." << endl;
typedef void (*attack_pfn)(TestClass*);
attack_pfn attack = reinterpret_cast<attack_pfn>(ee->getPointerToFunction(attack_func));
if(!attack) {
cout << "ERROR: Failed to cast 'attack' function." << endl;
return 0;
}
attack(&tc);
tc.printHealth();
delete ee;
}
示例3: main
int main(int argc, char **argv)
{
using namespace llvm;
std::cout << "hello_world_ir: " << std::endl;
std::cout << std::endl;
std::cout << hello_world_ir << std::endl;
std::cout << std::endl;
InitializeNativeTarget();
LLVMContext context;
SMDiagnostic error;
Module *m = ParseIR(MemoryBuffer::getMemBuffer(StringRef(hello_world_ir)), error, context);
if(!m)
{
error.print(argv[0], errs());
}
ExecutionEngine *ee = ExecutionEngine::create(m);
Function *func = ee->FindFunctionNamed("hello_world");
typedef void (*fcn_ptr)();
fcn_ptr hello_world = reinterpret_cast<fcn_ptr>(ee->getPointerToFunction(func));
hello_world();
delete ee;
return 0;
}
示例4: main
int main(int argc, char **argv) {
int n = argc > 1 ? atol(argv[1]) : 10;
InitializeNativeTarget();
LLVMContext Context;
Module *M = new Module("test", Context);
ExecutionEngine* EE = llvm::EngineBuilder(M).setEngineKind(EngineKind::JIT).create();
for (int i = 0; i < n; ++i) {
SMDiagnostic error;
ParseAssemblyString(function_assembly,
M,
error,
Context);
Function *func = M->getFunction( "factorial" );
typedef int(*func_t)(int);
func_t f = (func_t)(uintptr_t)(EE->getPointerToFunction(func));
EE->freeMachineCodeForFunction(func);
func->eraseFromParent();
}
delete EE;
llvm_shutdown();
return 0;
}
示例5: caseFoldCodeGen
casefoldFunctionType caseFoldCodeGen(void) {
Module * M = new Module("casefold", getGlobalContext());
IDISA::IDISA_Builder * idb = GetIDISA_Builder(M);
kernel::PipelineBuilder pipelineBuilder(M, idb);
Encoding encoding(Encoding::Type::UTF_8, 8);
pablo::PabloFunction * function = casefold2pablo(encoding);
pipelineBuilder.CreateKernels(function);
pipelineBuilder.ExecuteKernels();
//std::cerr << "ExecuteKernels(); done\n";
llvm::Function * main_IR = M->getFunction("Main");
ExecutionEngine * mEngine = JIT_to_ExecutionEngine(M);
mEngine->finalizeObject();
//std::cerr << "finalizeObject(); done\n";
delete idb;
return reinterpret_cast<casefoldFunctionType>(mEngine->getPointerToFunction(main_IR));
}
示例6: getPointerToFunction
void* getPointerToFunction(Module * mod, Function * func)
{
ExecutionEngine * execEngine = createExecutionEngine(mod);
if (!execEngine) exit(-1);
return execEngine->getPointerToFunction(func);
}
示例7: InitializeNativeTarget
extern "C" void*
LLVMRustExecuteJIT(void* mem,
LLVMPassManagerRef PMR,
LLVMModuleRef M,
CodeGenOpt::Level OptLevel,
bool EnableSegmentedStacks) {
InitializeNativeTarget();
InitializeNativeTargetAsmPrinter();
InitializeNativeTargetAsmParser();
std::string Err;
TargetOptions Options;
Options.JITExceptionHandling = true;
Options.JITEmitDebugInfo = true;
Options.NoFramePointerElim = true;
Options.EnableSegmentedStacks = EnableSegmentedStacks;
PassManager *PM = unwrap<PassManager>(PMR);
RustMCJITMemoryManager* MM = (RustMCJITMemoryManager*) mem;
assert(MM);
PM->add(createBasicAliasAnalysisPass());
PM->add(createInstructionCombiningPass());
PM->add(createReassociatePass());
PM->add(createGVNPass());
PM->add(createCFGSimplificationPass());
PM->add(createFunctionInliningPass());
PM->add(createPromoteMemoryToRegisterPass());
PM->run(*unwrap(M));
ExecutionEngine* EE = EngineBuilder(unwrap(M))
.setErrorStr(&Err)
.setTargetOptions(Options)
.setJITMemoryManager(MM)
.setOptLevel(OptLevel)
.setUseMCJIT(true)
.setAllocateGVsWithCode(false)
.create();
if(!EE || Err != "") {
LLVMRustError = Err.c_str();
return 0;
}
MM->invalidateInstructionCache();
Function* func = EE->FindFunctionNamed("_rust_main");
if(!func || Err != "") {
LLVMRustError = Err.c_str();
return 0;
}
void* entry = EE->getPointerToFunction(func);
assert(entry);
return entry;
}
示例8: compile
void * compile(llvm::Module * m) {
ExecutionEngine * engine =
EngineBuilder(std::unique_ptr<Module>(m))
.create();
engine->finalizeObject();
return engine->getPointerToFunction(
m->getFunction("aFun"));
}
示例9: makeLLVMModule
int
main (int argc, char const *argv[])
{
Module* mod = makeLLVMModule();
verifyModule(*mod, PrintMessageAction);
ExecutionEngine* jit = ExecutionEngine::create(mod);
Function* factorial = mod->getFunction("factorial");
int (*native_factorial)(int) = (int (*)(int)) jit->getPointerToFunction(factorial);
int arg1 = argc > 1 ? atoi(argv[1]) : 1;
printf("Factorial(%d) = %d.\n", arg1, native_factorial(arg1));
return 0;
}
示例10: Module
Ret(*compile(const pegsolitaire::ast::Function<Ret, Types...> & function))(Types...) {
using namespace llvm;
Module * module = new Module("compiledModule", getGlobalContext());
IRBuilder<> builder(getGlobalContext());
pegsolitaire::codegen::CodeGenerator cg(module, builder);
auto f = cg(function);
#ifndef NDEBUG
module->dump();
#endif
ExecutionEngine * executionEngine = EngineBuilder(module).create();
// TODO we actually need a mapping from Types... to NativeTypes... here:
return reinterpret_cast<Ret(*)(Types...)>(executionEngine->getPointerToFunction(f));
}
示例11: compile
main_func_type jit_engine::compile(const ast::Program &prog) {
llvm::LLVMContext &c=llvm::getGlobalContext();
llvm::Module *m=new llvm::Module("brainfuck", c);
brainfuck::codegen(*m, prog);
//m->dump();
std::string ErrStr;
ExecutionEngine *TheExecutionEngine = EngineBuilder(m).setErrorStr(&ErrStr).create();
if (!TheExecutionEngine) {
fprintf(stderr, "Could not create ExecutionEngine: %s\n", ErrStr.c_str());
exit(1);
}
Function *f_main=m->getFunction("main");
// Optimize, target dependant
if(optimization_level_>0)
{
FunctionPassManager OurFPM(m);
// Set up the optimizer pipeline. Start with registering info about how the
// target lays out data structures.
OurFPM.add(new TargetData(*TheExecutionEngine->getTargetData()));
// Provide basic AliasAnalysis support for GVN.
OurFPM.add(createBasicAliasAnalysisPass());
// Do simple "peephole" optimizations and bit-twiddling optzns.
OurFPM.add(createInstructionCombiningPass());
// Reassociate expressions.
OurFPM.add(createReassociatePass());
// Eliminate Common SubExpressions.
OurFPM.add(createGVNPass());
// Simplify the control flow graph (deleting unreachable blocks, etc).
OurFPM.add(createCFGSimplificationPass());
//OurFPM.add(createLoopSimplifyPass());
//OurFPM.add(createBlockPlacementPass());
//OurFPM.add(createConstantPropagationPass());
OurFPM.doInitialization();
OurFPM.run(*f_main);
}
//m->dump();
void *rp=TheExecutionEngine->getPointerToFunction(f_main);
main_func_type fp=reinterpret_cast<main_func_type>(rp);
return fp;
}
示例12: main
int main() {
// The registers.
int registers[2] = {0, 0};
// Our program.
int program[20] = {0, 0, 3,
1, 0, 0, 4,
2, 0};
int* ops = (int*)program;
// Create our function and give us the Module and Function back.
Module* jit;
Function* func = create(&jit);
// Add in optimizations. These were taken from a list that 'opt', LLVMs optimization tool, uses.
PassManager p;
/* Comment out optimize
p.add(new TargetData(jit));
p.add(createVerifierPass());
p.add(createLowerSetJmpPass());
p.add(createRaiseAllocationsPass());
p.add(createCFGSimplificationPass());
p.add(createPromoteMemoryToRegisterPass());
p.add(createGlobalOptimizerPass());
p.add(createGlobalDCEPass());
p.add(createFunctionInliningPass());
*/
// Run these optimizations on our Module
p.run(*jit);
// Setup for JIT
ExistingModuleProvider* mp = new ExistingModuleProvider(jit);
ExecutionEngine* engine = ExecutionEngine::create(mp);
// Show us what we've created!
std::cout << "Created\n" << *jit;
// Have our function JIT'd into machine code and return. We cast it to a particular C function pointer signature so we can call in nicely.
void (*fp)(int*, int*) = (void (*)(int*, int*))engine->getPointerToFunction(func);
// Call what we've created!
fp(ops, registers);
}
示例13: main
int main(int argc, char **argv)
{
InitializeNativeTarget();
LLVMContext &Context = getGlobalContext();
Module *m = new Module("test", Context);
Type *intTy = Type::getInt64Ty(Context);
StructType* structTy = StructType::create(Context, "struct.list");
std::vector<Type*> fields;
fields.push_back(intTy);
fields.push_back(PointerType::get(structTy, 0));
if (structTy->isOpaque()) {
structTy->setBody(fields, false);
}
/*
* int f1(struct x *p) { return p->next->l1; }
*/
std::vector<Type*> args_type;
args_type.push_back(PointerType::get(structTy, 0));
FunctionType *fnTy = FunctionType::get(intTy, args_type, false);
Function *func = Function::Create(fnTy,
GlobalValue::ExternalLinkage, "f1", m);
Value *v = func->arg_begin();
BasicBlock *bb = BasicBlock::Create(Context, "EntryBlock", func);
IRBuilder<> *builder = new IRBuilder<>(bb);
v = builder->CreateStructGEP(v, 1);
v = builder->CreateLoad(v, "load0");
v = builder->CreateStructGEP(v, 0);
v = builder->CreateLoad(v, "load1");
builder->CreateRet(v);
(*m).dump();
{
ExecutionEngine *ee = EngineBuilder(m).
setEngineKind(EngineKind::JIT).create();
void *f = ee->getPointerToFunction(func);
typedef int (*func_t) (struct x *);
struct x o = {10, NULL}, v = {};
v.next = &o;
std::cout << ((func_t)f)(&v) << std::endl;
}
return 0;
}
示例14: main
int main()
{
// Module Construction
LLVMContext &context = llvm::getGlobalContext();
Module *module = new Module("test", context);
//declare 'value' and 'foo' in module 'test'
GlobalVariable *v = cast<GlobalVariable>(module->getOrInsertGlobal("value", Type::getInt32Ty(context)));
// prototype of foo is: int foo(int x)
Function *f = cast<Function>(module->getOrInsertFunction("foo", Type::getInt32Ty(context),
Type::getInt32Ty(context), NULL));
//create a LLVM function 'bar'
Function* bar = cast<Function>(module->getOrInsertFunction("bar", Type::getInt32Ty(context),NULL));
//basic block construction
BasicBlock* entry = BasicBlock::Create(context, "entry", bar);
IRBuilder<> builder(entry);
//read 'value'
Value * v_IR = builder.CreateLoad(v);
//call foo(value)
Value * ret = builder.CreateCall(f, v_IR);
//return return value of 'foo'
builder.CreateRet(ret);
//now bind global value and global function
//create execution engine first
InitializeNativeTarget();
ExecutionEngine *ee = EngineBuilder(module).setEngineKind(EngineKind::JIT).create();
//map global variable
ee->addGlobalMapping(v, &value);
//map global function
ee->addGlobalMapping(f, (void *)foo);
// JIT and run
void *barAddr = ee->getPointerToFunction(bar);
typedef int (*FuncType)();
FuncType barFunc = (FuncType)barAddr;
std::cout << barFunc() << std::endl;
return 0;
}
示例15: int
pair<int, string> execute( string input ){
out = "";
CodeGenerator * codeGen = new CodeGenerator();
node_struct data;
Node * ast = Node::createAST( data );
makeLLVMModule( *ast );
verifyModule( *theModule, PrintMessageAction );
ExecutionEngine *ee = EngineBuilder( theModule ).create();
Function* func = ee -> FindFunctionNamed("main");
typedef int (*PFN)();
PFN pfn = reinterpret_cast<PFN>(ee->getPointerToFunction(func));
int result = pfn();
//printf( "%s\n", out.c_str() );
delete theModule;
return make_pair( result, out );
}