Beispiel #1
0
ExecutionEngine* createExecutionEngine(Module* mod) {

    if (globalExecEngine == 0)
    {
        //we first have to initialize the native target for code generation
        const bool initFailed = InitializeNativeTarget();

        if (initFailed) {
            errs() << "ERROR: could not initialize native target (required for "
                << "LLVM execution engine)\n";
            return NULL;
        }

        std::string errorMessage = "";

        EngineBuilder eb = EngineBuilder(mod);
        eb.setEngineKind(EngineKind::JIT);
        eb.setErrorStr(&errorMessage);
        eb.setJITMemoryManager(JITMemoryManager::CreateDefaultMemManager());
        eb.setOptLevel(CodeGenOpt::Aggressive);
        eb.setAllocateGVsWithCode(false);
        eb.setCodeModel(CodeModel::Default);
        //eb.setMArch("x86-64");
        //eb.setMCPU("corei7");
        //std::vector<std::string> attrs;
        //attrs.push_back("+sse41");
        //eb.setMAttrs(attrs);


        globalExecEngine = eb.create();

        if (errorMessage != "") {
            errs() << "ERROR: could not create execution engine for module "
                << mod->getModuleIdentifier() << ": " << errorMessage << "\n";
            return NULL;
        }

        if (!globalExecEngine) {
            errs() << "ERROR: could not create execution engine for module "
                << mod->getModuleIdentifier() << "!\n";
            return NULL;
        }
    }

    return globalExecEngine;
}
Beispiel #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;
}