当前位置: 首页>>代码示例>>C++>>正文


C++ ExecutionEngine::getPointerToFunction方法代码示例

本文整理汇总了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;
}
开发者ID:kayhman,项目名称:llisp,代码行数:30,代码来源:run-bc.cpp

示例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;
}
开发者ID:dcbishop,项目名称:llvmscriptdemo,代码行数:59,代码来源:llvmscriptdemo.cpp

示例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;
}
开发者ID:jaredhoberock,项目名称:llvm,代码行数:31,代码来源:ir_jit_hello_world.cpp

示例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;
}
开发者ID:naosuke,项目名称:how-to-free-memory-of-JIT-function,代码行数:32,代码来源:main.cpp

示例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));
}
开发者ID:fdebeyan,项目名称:simdProject,代码行数:28,代码来源:casefold.cpp

示例6: getPointerToFunction

void* getPointerToFunction(Module * mod, Function * func)
{
    ExecutionEngine * execEngine = createExecutionEngine(mod);
    if (!execEngine) exit(-1);

    return execEngine->getPointerToFunction(func);
}
开发者ID:erwincoumans,项目名称:wfvopencl,代码行数:7,代码来源:llvmTools.cpp

示例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;
}
开发者ID:BrandonBarrett,项目名称:rust,代码行数:58,代码来源:RustWrapper.cpp

示例8: compile

void * compile(llvm::Module * m) {
    ExecutionEngine * engine =
      EngineBuilder(std::unique_ptr<Module>(m))
      .create();
    engine->finalizeObject();

    return engine->getPointerToFunction(
        m->getFunction("aFun"));
}
开发者ID:rift-lecture,项目名称:warmup,代码行数:9,代码来源:warmup.cpp

示例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;
}
开发者ID:DawidvC,项目名称:eve-language,代码行数:13,代码来源:factorial.cpp

示例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));
}
开发者ID:foobar27,项目名称:cpp-peg-solitaire,代码行数:14,代码来源:CodeGenerationTest.cpp

示例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;
 }
开发者ID:Phuehvk,项目名称:brainfuck,代码行数:47,代码来源:bfjit.cpp

示例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);
}
开发者ID:caasi,项目名称:godfat-sandbox,代码行数:46,代码来源:evanphx.cpp

示例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;
}
开发者ID:NariyoshiChida,项目名称:llvm-sample,代码行数:45,代码来源:list.cpp

示例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;
}
开发者ID:RichardUSTC,项目名称:llvm-ir-global-mapping,代码行数:45,代码来源:llvm-ir-global-mapping.cpp

示例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 );
}
开发者ID:piotrbar,项目名称:MAlice,代码行数:21,代码来源:test.cpp


注:本文中的ExecutionEngine::getPointerToFunction方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。