[MCJIT][Orc] Refactor RTDyldMemoryManager, weave RuntimeDyld::SymbolInfo through
[oota-llvm.git] / tools / lli / OrcLazyJIT.cpp
1 //===------ 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"
12
13 using namespace llvm;
14
15 std::unique_ptr<OrcLazyJIT::CompileCallbackMgr>
16 OrcLazyJIT::createCallbackMgr(Triple T, LLVMContext &Context) {
17   switch (T.getArch()) {
18     default:
19       // Flag error.
20       Error = true;
21       return nullptr;
22
23     case Triple::x86_64: {
24       typedef orc::JITCompileCallbackManager<CompileLayerT,
25                                              orc::OrcX86_64> CCMgrT;
26       return make_unique<CCMgrT>(CompileLayer, CCMgrMemMgr, Context, 0, 64);
27     }
28   }
29 }
30
31 int llvm::runOrcLazyJIT(std::unique_ptr<Module> M, int ArgC, char* ArgV[]) {
32   OrcLazyJIT J(std::unique_ptr<TargetMachine>(EngineBuilder().selectTarget()),
33                getGlobalContext());
34
35   if (!J.Ok()) {
36     errs() << "Could not construct JIT.\n";
37     return 1;
38   }
39
40   auto MainHandle = J.addModule(std::move(M));
41   auto MainSym = J.findSymbolIn(MainHandle, "main");
42
43   if (!MainSym) {
44     errs() << "Could not find main function.\n";
45     return 1;
46   }
47
48   typedef int (*MainFnPtr)(int, char*[]);
49   auto Main = reinterpret_cast<MainFnPtr>(
50                 static_cast<uintptr_t>(MainSym.getAddress()));
51
52   return Main(ArgC, ArgV);
53 }