blob: 236de7c31d26d547239bb712d6fe7b64d9c65129 [file] [log] [blame]
Lang Hames9528bba2015-03-25 12:11:48 +00001//===------ OrcLazyJIT.cpp - Basic Orc-based JIT for lazy execution -------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "OrcLazyJIT.h"
11#include "llvm/ExecutionEngine/Orc/OrcTargetSupport.h"
Lang Hames5a9808b2015-04-01 04:42:56 +000012#include "llvm/Support/DynamicLibrary.h"
Lang Hames9528bba2015-03-25 12:11:48 +000013
14using namespace llvm;
15
Lang Hames3c9e20d2015-03-30 18:37:01 +000016OrcLazyJIT::CallbackManagerBuilder
17OrcLazyJIT::createCallbackManagerBuilder(Triple T) {
Lang Hames9528bba2015-03-25 12:11:48 +000018 switch (T.getArch()) {
Lang Hames3c9e20d2015-03-30 18:37:01 +000019 default: return nullptr;
Lang Hames9528bba2015-03-25 12:11:48 +000020
21 case Triple::x86_64: {
22 typedef orc::JITCompileCallbackManager<CompileLayerT,
23 orc::OrcX86_64> CCMgrT;
Lang Hames3c9e20d2015-03-30 18:37:01 +000024 return [](CompileLayerT &CompileLayer, RuntimeDyld::MemoryManager &MemMgr,
25 LLVMContext &Context) {
26 return make_unique<CCMgrT>(CompileLayer, MemMgr, Context, 0, 64);
27 };
Lang Hames9528bba2015-03-25 12:11:48 +000028 }
29 }
30}
31
32int llvm::runOrcLazyJIT(std::unique_ptr<Module> M, int ArgC, char* ArgV[]) {
Lang Hames5a9808b2015-04-01 04:42:56 +000033 // Add the program's symbols into the JIT's search space.
34 if (sys::DynamicLibrary::LoadLibraryPermanently(nullptr)) {
35 errs() << "Error loading program symbols.\n";
36 return 1;
37 }
38
39 // Grab a target machine and try to build a factory function for the
40 // target-specific Orc callback manager.
Lang Hames3c9e20d2015-03-30 18:37:01 +000041 auto TM = std::unique_ptr<TargetMachine>(EngineBuilder().selectTarget());
42 auto &Context = getGlobalContext();
43 auto CallbackMgrBuilder =
44 OrcLazyJIT::createCallbackManagerBuilder(Triple(TM->getTargetTriple()));
Lang Hames9528bba2015-03-25 12:11:48 +000045
Lang Hames5a9808b2015-04-01 04:42:56 +000046 // If we couldn't build the factory function then there must not be a callback
47 // manager for this target. Bail out.
Lang Hames3c9e20d2015-03-30 18:37:01 +000048 if (!CallbackMgrBuilder) {
49 errs() << "No callback manager available for target '"
50 << TM->getTargetTriple() << "'.\n";
Lang Hames9528bba2015-03-25 12:11:48 +000051 return 1;
52 }
53
Lang Hames5a9808b2015-04-01 04:42:56 +000054 // Everything looks good. Build the JIT.
Lang Hames3c9e20d2015-03-30 18:37:01 +000055 OrcLazyJIT J(std::move(TM), Context, CallbackMgrBuilder);
56
Lang Hames5a9808b2015-04-01 04:42:56 +000057 // Add the module, look up main and run it.
Lang Hames9528bba2015-03-25 12:11:48 +000058 auto MainHandle = J.addModule(std::move(M));
59 auto MainSym = J.findSymbolIn(MainHandle, "main");
60
61 if (!MainSym) {
62 errs() << "Could not find main function.\n";
63 return 1;
64 }
65
66 typedef int (*MainFnPtr)(int, char*[]);
Lang Hamesb1cd98a2015-04-02 04:34:45 +000067 auto Main = OrcLazyJIT::fromTargetAddress<MainFnPtr>(MainSym.getAddress());
Lang Hames9528bba2015-03-25 12:11:48 +000068 return Main(ArgC, ArgV);
69}