2015-10-26 14:40:28 +08:00
|
|
|
//===------- OrcTargetSupport.cpp - Target support utilities for Orc ------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
[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.
llvm-svn: 226940
2015-01-24 05:25:00 +08:00
|
|
|
#include "llvm/ADT/Triple.h"
|
2015-02-17 09:18:38 +08:00
|
|
|
#include "llvm/ExecutionEngine/Orc/OrcTargetSupport.h"
|
2015-10-20 01:43:51 +08:00
|
|
|
#include "llvm/Support/Process.h"
|
[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.
llvm-svn: 226940
2015-01-24 05:25:00 +08:00
|
|
|
#include <array>
|
|
|
|
|
2015-02-22 04:44:36 +08:00
|
|
|
using namespace llvm::orc;
|
[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.
llvm-svn: 226940
2015-01-24 05:25:00 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2015-03-25 10:45:50 +08:00
|
|
|
uint64_t executeCompileCallback(JITCompileCallbackManagerBase *JCBM,
|
2015-02-17 09:18:38 +08:00
|
|
|
TargetAddress CallbackID) {
|
|
|
|
return JCBM->executeCompileCallback(CallbackID);
|
[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.
llvm-svn: 226940
2015-01-24 05:25:00 +08:00
|
|
|
}
|
2015-02-17 09:18:38 +08:00
|
|
|
|
[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.
llvm-svn: 226940
2015-01-24 05:25:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace llvm {
|
2015-02-22 04:44:36 +08:00
|
|
|
namespace orc {
|
[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.
llvm-svn: 226940
2015-01-24 05:25:00 +08:00
|
|
|
|
2015-02-17 09:18:38 +08:00
|
|
|
const char* OrcX86_64::ResolverBlockName = "orc_resolver_block";
|
[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.
llvm-svn: 226940
2015-01-24 05:25:00 +08:00
|
|
|
|
2015-02-17 09:18:38 +08:00
|
|
|
void OrcX86_64::insertResolverBlock(
|
2015-03-25 10:45:50 +08:00
|
|
|
Module &M, JITCompileCallbackManagerBase &JCBM) {
|
2015-04-06 11:01:29 +08:00
|
|
|
|
|
|
|
// Trampoline code-sequence length, used to get trampoline address from return
|
|
|
|
// address.
|
2015-03-24 12:27:02 +08:00
|
|
|
const unsigned X86_64_TrampolineLength = 6;
|
2015-04-06 11:01:29 +08:00
|
|
|
|
2015-04-07 14:12:21 +08:00
|
|
|
// List of x86-64 GPRs to save. Note - RBP saved separately below.
|
|
|
|
std::array<const char *, 14> GPRs = {{
|
|
|
|
"rax", "rbx", "rcx", "rdx",
|
|
|
|
"rsi", "rdi", "r8", "r9",
|
|
|
|
"r10", "r11", "r12", "r13",
|
|
|
|
"r14", "r15"
|
2015-04-06 11:01:29 +08:00
|
|
|
}};
|
|
|
|
|
|
|
|
// Address of the executeCompileCallback function.
|
2015-02-17 20:53:16 +08:00
|
|
|
uint64_t CallbackAddr =
|
2015-04-06 11:01:29 +08:00
|
|
|
static_cast<uint64_t>(
|
|
|
|
reinterpret_cast<uintptr_t>(executeCompileCallback));
|
[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.
llvm-svn: 226940
2015-01-24 05:25:00 +08:00
|
|
|
|
2015-02-17 09:18:38 +08:00
|
|
|
std::ostringstream AsmStream;
|
[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.
llvm-svn: 226940
2015-01-24 05:25:00 +08:00
|
|
|
Triple TT(M.getTargetTriple());
|
|
|
|
|
2015-04-06 11:01:29 +08:00
|
|
|
// Switch to text section.
|
[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.
llvm-svn: 226940
2015-01-24 05:25:00 +08:00
|
|
|
if (TT.getOS() == Triple::Darwin)
|
2015-02-17 09:18:38 +08:00
|
|
|
AsmStream << ".section __TEXT,__text,regular,pure_instructions\n"
|
|
|
|
<< ".align 4, 0x90\n";
|
[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.
llvm-svn: 226940
2015-01-24 05:25:00 +08:00
|
|
|
else
|
2015-02-17 09:18:38 +08:00
|
|
|
AsmStream << ".text\n"
|
|
|
|
<< ".align 16, 0x90\n";
|
[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.
llvm-svn: 226940
2015-01-24 05:25:00 +08:00
|
|
|
|
2015-04-06 11:01:29 +08:00
|
|
|
// Bake in a pointer to the callback manager immediately before the
|
|
|
|
// start of the resolver function.
|
2015-02-17 09:18:38 +08:00
|
|
|
AsmStream << "jit_callback_manager_addr:\n"
|
2015-04-06 11:01:29 +08:00
|
|
|
<< " .quad " << &JCBM << "\n";
|
[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.
llvm-svn: 226940
2015-01-24 05:25:00 +08:00
|
|
|
|
2015-04-06 11:01:29 +08:00
|
|
|
// Start the resolver function.
|
|
|
|
AsmStream << ResolverBlockName << ":\n"
|
2015-04-21 02:25:44 +08:00
|
|
|
<< " pushq %rbp\n"
|
|
|
|
<< " movq %rsp, %rbp\n";
|
[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.
llvm-svn: 226940
2015-01-24 05:25:00 +08:00
|
|
|
|
2015-04-06 11:01:29 +08:00
|
|
|
// Store the GPRs.
|
|
|
|
for (const auto &GPR : GPRs)
|
2015-04-21 02:25:44 +08:00
|
|
|
AsmStream << " pushq %" << GPR << "\n";
|
2015-04-06 11:01:29 +08:00
|
|
|
|
|
|
|
// Store floating-point state with FXSAVE.
|
2015-04-07 14:12:21 +08:00
|
|
|
// Note: We need to keep the stack 16-byte aligned, so if we've emitted an odd
|
|
|
|
// number of 64-bit pushes so far (GPRs.size() plus 1 for RBP) then add
|
|
|
|
// an extra 64 bits of padding to the FXSave area.
|
|
|
|
unsigned Padding = (GPRs.size() + 1) % 2 ? 8 : 0;
|
|
|
|
unsigned FXSaveSize = 512 + Padding;
|
2015-04-21 02:25:44 +08:00
|
|
|
AsmStream << " subq $" << FXSaveSize << ", %rsp\n"
|
|
|
|
<< " fxsave64 (%rsp)\n"
|
2015-04-06 11:01:29 +08:00
|
|
|
|
|
|
|
// Load callback manager address, compute trampoline address, call JIT.
|
2015-04-21 02:25:44 +08:00
|
|
|
<< " lea jit_callback_manager_addr(%rip), %rdi\n"
|
|
|
|
<< " movq (%rdi), %rdi\n"
|
|
|
|
<< " movq 0x8(%rbp), %rsi\n"
|
|
|
|
<< " subq $" << X86_64_TrampolineLength << ", %rsi\n"
|
|
|
|
<< " movabsq $" << CallbackAddr << ", %rax\n"
|
|
|
|
<< " callq *%rax\n"
|
2015-02-17 09:18:38 +08:00
|
|
|
|
2015-04-06 11:01:29 +08:00
|
|
|
// Replace the return to the trampoline with the return address of the
|
|
|
|
// compiled function body.
|
2015-04-21 02:25:44 +08:00
|
|
|
<< " movq %rax, 0x8(%rbp)\n"
|
2015-04-06 11:01:29 +08:00
|
|
|
|
|
|
|
// Restore the floating point state.
|
2015-04-21 02:25:44 +08:00
|
|
|
<< " fxrstor64 (%rsp)\n"
|
|
|
|
<< " addq $" << FXSaveSize << ", %rsp\n";
|
2015-04-06 11:01:29 +08:00
|
|
|
|
|
|
|
for (const auto &GPR : make_range(GPRs.rbegin(), GPRs.rend()))
|
2015-04-21 02:25:44 +08:00
|
|
|
AsmStream << " popq %" << GPR << "\n";
|
2015-02-17 09:18:38 +08:00
|
|
|
|
2015-04-06 11:01:29 +08:00
|
|
|
// Restore original RBP and return to compiled function body.
|
2015-04-21 02:25:44 +08:00
|
|
|
AsmStream << " popq %rbp\n"
|
2015-04-06 11:01:29 +08:00
|
|
|
<< " retq\n";
|
2015-02-17 09:18:38 +08:00
|
|
|
|
|
|
|
M.appendModuleInlineAsm(AsmStream.str());
|
[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.
llvm-svn: 226940
2015-01-24 05:25:00 +08:00
|
|
|
}
|
2015-02-17 09:18:38 +08:00
|
|
|
|
|
|
|
OrcX86_64::LabelNameFtor
|
|
|
|
OrcX86_64::insertCompileCallbackTrampolines(Module &M,
|
|
|
|
TargetAddress ResolverBlockAddr,
|
|
|
|
unsigned NumCalls,
|
|
|
|
unsigned StartIndex) {
|
|
|
|
const char *ResolverBlockPtrName = "Lorc_resolve_block_addr";
|
|
|
|
|
|
|
|
std::ostringstream AsmStream;
|
|
|
|
Triple TT(M.getTargetTriple());
|
|
|
|
|
|
|
|
if (TT.getOS() == Triple::Darwin)
|
|
|
|
AsmStream << ".section __TEXT,__text,regular,pure_instructions\n"
|
|
|
|
<< ".align 4, 0x90\n";
|
|
|
|
else
|
|
|
|
AsmStream << ".text\n"
|
|
|
|
<< ".align 16, 0x90\n";
|
|
|
|
|
|
|
|
AsmStream << ResolverBlockPtrName << ":\n"
|
|
|
|
<< " .quad " << ResolverBlockAddr << "\n";
|
|
|
|
|
|
|
|
auto GetLabelName =
|
|
|
|
[=](unsigned I) {
|
|
|
|
std::ostringstream LabelStream;
|
|
|
|
LabelStream << "orc_jcc_" << (StartIndex + I);
|
|
|
|
return LabelStream.str();
|
|
|
|
};
|
|
|
|
|
|
|
|
for (unsigned I = 0; I < NumCalls; ++I)
|
|
|
|
AsmStream << GetLabelName(I) << ":\n"
|
|
|
|
<< " callq *" << ResolverBlockPtrName << "(%rip)\n";
|
|
|
|
|
|
|
|
M.appendModuleInlineAsm(AsmStream.str());
|
|
|
|
|
|
|
|
return GetLabelName;
|
|
|
|
}
|
|
|
|
|
2015-10-30 06:04:22 +08:00
|
|
|
OrcX86_64::IndirectStubsInfo::IndirectStubsInfo(IndirectStubsInfo &&Other) {
|
|
|
|
StubsBlock = std::move(Other.StubsBlock);
|
|
|
|
PtrsBlock = std::move(Other.PtrsBlock);
|
|
|
|
Other.StubsBlock = sys::MemoryBlock();
|
|
|
|
Other.PtrsBlock = sys::MemoryBlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
OrcX86_64::IndirectStubsInfo&
|
|
|
|
OrcX86_64::IndirectStubsInfo::operator=(IndirectStubsInfo &&Other) {
|
|
|
|
StubsBlock = std::move(Other.StubsBlock);
|
|
|
|
PtrsBlock = std::move(Other.PtrsBlock);
|
|
|
|
Other.StubsBlock = sys::MemoryBlock();
|
|
|
|
Other.PtrsBlock = sys::MemoryBlock();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-10-20 01:43:51 +08:00
|
|
|
OrcX86_64::IndirectStubsInfo::~IndirectStubsInfo() {
|
|
|
|
sys::Memory::releaseMappedMemory(StubsBlock);
|
|
|
|
sys::Memory::releaseMappedMemory(PtrsBlock);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::error_code OrcX86_64::emitIndirectStubsBlock(IndirectStubsInfo &StubsInfo,
|
|
|
|
unsigned MinStubs,
|
|
|
|
void *InitialPtrVal) {
|
|
|
|
// Stub format is:
|
|
|
|
//
|
|
|
|
// .section __orc_stubs
|
|
|
|
// stub1:
|
|
|
|
// jmpq *ptr1(%rip)
|
|
|
|
// .byte 0xC4 ; <- Invalid opcode padding.
|
|
|
|
// .byte 0xF1
|
|
|
|
// stub2:
|
|
|
|
// jmpq *ptr2(%rip)
|
|
|
|
//
|
|
|
|
// ...
|
|
|
|
//
|
|
|
|
// .section __orc_ptrs
|
|
|
|
// ptr1:
|
|
|
|
// .quad 0x0
|
|
|
|
// ptr2:
|
|
|
|
// .quad 0x0
|
|
|
|
//
|
|
|
|
// ...
|
|
|
|
|
|
|
|
const unsigned StubSize = IndirectStubsInfo::StubSize;
|
|
|
|
|
|
|
|
// Emit at least MinStubs, rounded up to fill the pages allocated.
|
|
|
|
unsigned PageSize = sys::Process::getPageSize();
|
|
|
|
unsigned NumPages = ((MinStubs * StubSize) + (PageSize - 1)) / PageSize;
|
|
|
|
unsigned NumStubs = (NumPages * PageSize) / StubSize;
|
|
|
|
|
|
|
|
// Allocate memory for stubs and pointers in one call.
|
|
|
|
std::error_code EC;
|
|
|
|
auto InitialBlock = sys::Memory::allocateMappedMemory(2 * NumPages * PageSize,
|
|
|
|
nullptr,
|
|
|
|
sys::Memory::MF_READ |
|
|
|
|
sys::Memory::MF_WRITE,
|
|
|
|
EC);
|
|
|
|
|
|
|
|
if (EC)
|
|
|
|
return EC;
|
|
|
|
|
|
|
|
// Create separate MemoryBlocks representing the stubs and pointers.
|
|
|
|
sys::MemoryBlock StubsBlock(InitialBlock.base(), NumPages * PageSize);
|
|
|
|
sys::MemoryBlock PtrsBlock(static_cast<char*>(InitialBlock.base()) +
|
|
|
|
NumPages * PageSize,
|
|
|
|
NumPages * PageSize);
|
|
|
|
|
|
|
|
// Populate the stubs page stubs and mark it executable.
|
|
|
|
uint64_t *Stub = reinterpret_cast<uint64_t*>(StubsBlock.base());
|
|
|
|
uint64_t PtrOffsetField =
|
|
|
|
static_cast<uint64_t>(NumPages * PageSize - 6) << 16;
|
|
|
|
for (unsigned I = 0; I < NumStubs; ++I)
|
|
|
|
Stub[I] = 0xF1C40000000025ff | PtrOffsetField;
|
|
|
|
|
|
|
|
if (auto EC = sys::Memory::protectMappedMemory(StubsBlock,
|
|
|
|
sys::Memory::MF_READ |
|
|
|
|
sys::Memory::MF_EXEC))
|
|
|
|
return EC;
|
|
|
|
|
|
|
|
// Initialize all pointers to point at FailureAddress.
|
|
|
|
void **Ptr = reinterpret_cast<void**>(PtrsBlock.base());
|
|
|
|
for (unsigned I = 0; I < NumStubs; ++I)
|
|
|
|
Ptr[I] = InitialPtrVal;
|
|
|
|
|
|
|
|
StubsInfo.NumStubs = NumStubs;
|
|
|
|
StubsInfo.StubsBlock = std::move(StubsBlock);
|
|
|
|
StubsInfo.PtrsBlock = std::move(PtrsBlock);
|
|
|
|
|
|
|
|
return std::error_code();
|
|
|
|
}
|
|
|
|
|
2015-02-22 04:44:36 +08:00
|
|
|
} // End namespace orc.
|
|
|
|
} // End namespace llvm.
|