llvm-6502/lib/ExecutionEngine/Orc/OrcTargetSupport.cpp
Lang Hames 63cc4f56a9 [Orc] New JIT APIs.
This patch adds a new set of JIT APIs to LLVM. The aim of these new APIs is to
cleanly support a wider range of JIT use cases in LLVM, and encourage the
development and contribution of re-usable infrastructure for LLVM JIT use-cases.

These APIs are intended to live alongside the MCJIT APIs, and should not affect
existing clients.

Included in this patch:

1) New headers in include/llvm/ExecutionEngine/Orc that provide a set of
   components for building JIT infrastructure.
   Implementation code for these headers lives in lib/ExecutionEngine/Orc.

2) A prototype re-implementation of MCJIT (OrcMCJITReplacement) built out of the
   new components.

3) Minor changes to RTDyldMemoryManager needed to support the new components.
   These changes should not impact existing clients.

4) A new flag for lli, -use-orcmcjit, which will cause lli to use the
   OrcMCJITReplacement class as its underlying execution engine, rather than
   MCJIT itself.

Tests to follow shortly.

Special thanks to Michael Ilseman, Pete Cooper, David Blaikie, Eric Christopher,
Justin Bogner, and Jim Grosbach for extensive feedback and discussion.



git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@226940 91177308-0d34-0410-b5e6-96231b3b80d8
2015-01-23 21:25:00 +00:00

103 lines
3.4 KiB
C++

#include "llvm/ADT/Triple.h"
#include "llvm/ExecutionEngine/Orc/IndirectionUtils.h"
#include <array>
using namespace llvm;
namespace {
const char *JITCallbackFuncName = "call_jit_for_lazy_compile";
const char *JITCallbackIndexLabelPrefix = "jit_resolve_";
std::array<const char *, 12> X86GPRsToSave = {{
"rbp", "rbx", "r12", "r13", "r14", "r15", // Callee saved.
"rdi", "rsi", "rdx", "rcx", "r8", "r9", // Int args.
}};
std::array<const char *, 8> X86XMMsToSave = {{
"xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7" // FP args
}};
template <typename OStream> unsigned saveX86Regs(OStream &OS) {
for (const auto &GPR : X86GPRsToSave)
OS << " pushq %" << GPR << "\n";
OS << " subq $" << (16 * X86XMMsToSave.size()) << ", %rsp\n";
for (unsigned i = 0; i < X86XMMsToSave.size(); ++i)
OS << " movdqu %" << X86XMMsToSave[i] << ", "
<< (16 * (X86XMMsToSave.size() - i - 1)) << "(%rsp)\n";
return (8 * X86GPRsToSave.size()) + (16 * X86XMMsToSave.size());
}
template <typename OStream> void restoreX86Regs(OStream &OS) {
for (unsigned i = 0; i < X86XMMsToSave.size(); ++i)
OS << " movdqu " << (16 * i) << "(%rsp), %"
<< X86XMMsToSave[(X86XMMsToSave.size() - i - 1)] << "\n";
OS << " addq $" << (16 * X86XMMsToSave.size()) << ", %rsp\n";
for (unsigned i = 0; i < X86GPRsToSave.size(); ++i)
OS << " popq %" << X86GPRsToSave[X86GPRsToSave.size() - i - 1] << "\n";
}
uint64_t call_jit_for_fn(JITResolveCallbackHandler *J, uint64_t FuncIdx) {
return J->resolve(FuncIdx);
}
}
namespace llvm {
std::string getJITResolveCallbackIndexLabel(unsigned I) {
std::ostringstream LabelStream;
LabelStream << JITCallbackIndexLabelPrefix << I;
return LabelStream.str();
}
void insertX86CallbackAsm(Module &M, JITResolveCallbackHandler &J) {
uint64_t CallbackAddr =
static_cast<uint64_t>(reinterpret_cast<uintptr_t>(call_jit_for_fn));
std::ostringstream JITCallbackAsm;
Triple TT(M.getTargetTriple());
if (TT.getOS() == Triple::Darwin)
JITCallbackAsm << ".section __TEXT,__text,regular,pure_instructions\n"
<< ".align 4, 0x90\n";
else
JITCallbackAsm << ".text\n"
<< ".align 16, 0x90\n";
JITCallbackAsm << "jit_object_addr:\n"
<< " .quad " << &J << "\n" << JITCallbackFuncName << ":\n";
uint64_t ReturnAddrOffset = saveX86Regs(JITCallbackAsm);
// Compute index, load object address, and call JIT.
JITCallbackAsm << " movq " << ReturnAddrOffset << "(%rsp), %rax\n"
<< " leaq (jit_indices_start+5)(%rip), %rbx\n"
<< " subq %rbx, %rax\n"
<< " xorq %rdx, %rdx\n"
<< " movq $5, %rbx\n"
<< " divq %rbx\n"
<< " movq %rax, %rsi\n"
<< " leaq jit_object_addr(%rip), %rdi\n"
<< " movq (%rdi), %rdi\n"
<< " movabsq $" << CallbackAddr << ", %rax\n"
<< " callq *%rax\n"
<< " movq %rax, " << ReturnAddrOffset << "(%rsp)\n";
restoreX86Regs(JITCallbackAsm);
JITCallbackAsm << " retq\n"
<< "jit_indices_start:\n";
for (JITResolveCallbackHandler::StubIndex I = 0; I < J.getNumFuncs(); ++I)
JITCallbackAsm << getJITResolveCallbackIndexLabel(I) << ":\n"
<< " callq " << JITCallbackFuncName << "\n";
M.appendModuleInlineAsm(JITCallbackAsm.str());
}
}