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;
}
Example #2
0
int main(int argc, char**argv) {
    InitializeNativeTarget();
    InitializeNativeTargetAsmPrinter();
    Module* Mod = makeLLVMModule();
    verifyModule(*Mod, PrintMessageAction);
    PassManager PM;
    PM.add(createPrintModulePass(&outs()));
    PM.run(*Mod);
    //ExecutionEngine *exe=::llvm::Interpreter::create(Mod);
    //ExecutionEngine *exe = EngineBuilder(Mod).create();
    //printf("----%p\n",exe);
    EngineBuilder eb = EngineBuilder(Mod);

#if LLVM_VERSION >= 33
    eb.setEngineKind(EngineKind::JIT);
    eb.setJITMemoryManager(JITMemoryManager::CreateDefaultMemManager());
    eb.setAllocateGVsWithCode(false);
    eb.setOptLevel(CodeGenOpt::Aggressive);
    eb.setCodeModel(CodeModel::JITDefault);
#endif

    eb.setMArch("x86-64");
    eb.setMCPU("corei7-avx");
    eb.setUseMCJIT(true);
    ExecutionEngine *exe = eb.create();

    std::vector<GenericValue> args;
    GenericValue GVArgc;
    GVArgc.IntVal = APInt(32, 24);
    args.push_back(GVArgc);
    //printf("xxxx:%p,%p\n",func_factorial,(void*)(&exe->runFunction));
    GenericValue ret=exe->runFunction(func_factorial, args);
    printf("ret=%llu\n",ret.IntVal.getZExtValue());

#if LLVM_VERSION < 33
    exe->freeMachineCodeForFunction(func_factorial);
#endif

    delete exe;
    //llvm_shutdown();
    return 0;
}
int main() {
  
  InitializeNativeTarget();

  LLVMContext Context;
  
  // Create some module to put our function into it.
  Module *M = new Module("test", Context);

  // Create the add1 function entry and insert this entry into module M.  The
  // function will have a return type of "int" and take an argument of "int".
  // The '0' terminates the list of argument types.
  Function *Add1F =
    cast<Function>(M->getOrInsertFunction("add1", Type::getInt32Ty(Context),
                                          Type::getInt32Ty(Context),
                                          (Type *)0));

  // Add a basic block to the function. As before, it automatically inserts
  // because of the last argument.
  BasicBlock *BB = BasicBlock::Create(Context, "EntryBlock", Add1F);

  // Create a basic block builder with default parameters.  The builder will
  // automatically append instructions to the basic block `BB'.
  IRBuilder<> builder(BB);

  // Get pointers to the constant `1'.
  Value *One = builder.getInt32(1);

  // Get pointers to the integer argument of the add1 function...
  assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an arg
  Argument *ArgX = Add1F->arg_begin();  // Get the arg
  ArgX->setName("AnArg");            // Give it a nice symbolic name for fun.

  // Create the add instruction, inserting it into the end of BB.
  Value *Add = builder.CreateAdd(One, ArgX);

  // Create the return instruction and add it to the basic block
  builder.CreateRet(Add);

  // Now, function add1 is ready.


  // Now we're going to create function `foo', which returns an int and takes no
  // arguments.
  Function *FooF =
    cast<Function>(M->getOrInsertFunction("foo", Type::getInt32Ty(Context),
                                          (Type *)0));

  // Add a basic block to the FooF function.
  BB = BasicBlock::Create(Context, "EntryBlock", FooF);

  // Tell the basic block builder to attach itself to the new basic block
  builder.SetInsertPoint(BB);

  // Get pointer to the constant `10'.
  Value *Ten = builder.getInt32(10);

  // Pass Ten to the call to Add1F
  CallInst *Add1CallRes = builder.CreateCall(Add1F, Ten);
  Add1CallRes->setTailCall(true);

  // Create the return instruction and add it to the basic block.
  builder.CreateRet(Add1CallRes);

  // Now we create the JIT.
  ExecutionEngine* EE = EngineBuilder(M).create();

  outs() << "We just constructed this LLVM module:\n\n" << *M;
  outs() << "\n\nRunning foo: ";
  outs().flush();

  // Call the `foo' function with no arguments:
  std::vector<GenericValue> noargs;
  GenericValue gv = EE->runFunction(FooF, noargs);

  // Import result of execution:
  outs() << "Result: " << gv.IntVal << "\n";
  EE->freeMachineCodeForFunction(FooF);
  delete EE;
  llvm_shutdown();
  return 0;
}