2015-03-25 12:11:48 +00:00
|
|
|
//===------ OrcLazyJIT.cpp - Basic Orc-based JIT for lazy execution -------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "OrcLazyJIT.h"
|
|
|
|
#include "llvm/ExecutionEngine/Orc/OrcTargetSupport.h"
|
2015-04-01 04:42:56 +00:00
|
|
|
#include "llvm/Support/DynamicLibrary.h"
|
2015-03-25 12:11:48 +00:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2015-03-30 18:37:01 +00:00
|
|
|
OrcLazyJIT::CallbackManagerBuilder
|
|
|
|
OrcLazyJIT::createCallbackManagerBuilder(Triple T) {
|
2015-03-25 12:11:48 +00:00
|
|
|
switch (T.getArch()) {
|
2015-03-30 18:37:01 +00:00
|
|
|
default: return nullptr;
|
2015-03-25 12:11:48 +00:00
|
|
|
|
|
|
|
case Triple::x86_64: {
|
|
|
|
typedef orc::JITCompileCallbackManager<CompileLayerT,
|
|
|
|
orc::OrcX86_64> CCMgrT;
|
2015-03-30 18:37:01 +00:00
|
|
|
return [](CompileLayerT &CompileLayer, RuntimeDyld::MemoryManager &MemMgr,
|
|
|
|
LLVMContext &Context) {
|
|
|
|
return make_unique<CCMgrT>(CompileLayer, MemMgr, Context, 0, 64);
|
|
|
|
};
|
2015-03-25 12:11:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int llvm::runOrcLazyJIT(std::unique_ptr<Module> M, int ArgC, char* ArgV[]) {
|
2015-04-01 04:42:56 +00:00
|
|
|
// Add the program's symbols into the JIT's search space.
|
|
|
|
if (sys::DynamicLibrary::LoadLibraryPermanently(nullptr)) {
|
|
|
|
errs() << "Error loading program symbols.\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Grab a target machine and try to build a factory function for the
|
|
|
|
// target-specific Orc callback manager.
|
2015-03-30 18:37:01 +00:00
|
|
|
auto TM = std::unique_ptr<TargetMachine>(EngineBuilder().selectTarget());
|
|
|
|
auto &Context = getGlobalContext();
|
|
|
|
auto CallbackMgrBuilder =
|
|
|
|
OrcLazyJIT::createCallbackManagerBuilder(Triple(TM->getTargetTriple()));
|
|
|
|
|
2015-04-01 04:42:56 +00:00
|
|
|
// If we couldn't build the factory function then there must not be a callback
|
|
|
|
// manager for this target. Bail out.
|
2015-03-30 18:37:01 +00:00
|
|
|
if (!CallbackMgrBuilder) {
|
|
|
|
errs() << "No callback manager available for target '"
|
|
|
|
<< TM->getTargetTriple() << "'.\n";
|
2015-03-25 12:11:48 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-04-01 04:42:56 +00:00
|
|
|
// Everything looks good. Build the JIT.
|
2015-03-30 18:37:01 +00:00
|
|
|
OrcLazyJIT J(std::move(TM), Context, CallbackMgrBuilder);
|
|
|
|
|
2015-04-01 04:42:56 +00:00
|
|
|
// Add the module, look up main and run it.
|
2015-03-25 12:11:48 +00:00
|
|
|
auto MainHandle = J.addModule(std::move(M));
|
|
|
|
auto MainSym = J.findSymbolIn(MainHandle, "main");
|
|
|
|
|
|
|
|
if (!MainSym) {
|
|
|
|
errs() << "Could not find main function.\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef int (*MainFnPtr)(int, char*[]);
|
2015-04-02 04:34:45 +00:00
|
|
|
auto Main = OrcLazyJIT::fromTargetAddress<MainFnPtr>(MainSym.getAddress());
|
2015-03-25 12:11:48 +00:00
|
|
|
return Main(ArgC, ArgV);
|
|
|
|
}
|