[Orc] Move Orc code into a namespace (llvm::orc), update Kaleidoscope code.
[oota-llvm.git] / lib / ExecutionEngine / Orc / OrcTargetSupport.cpp
1 #include "llvm/ADT/Triple.h"
2 #include "llvm/ExecutionEngine/Orc/OrcTargetSupport.h"
3 #include <array>
4
5 using namespace llvm::orc;
6
7 namespace {
8
9 std::array<const char *, 12> X86GPRsToSave = {{
10     "rbp", "rbx", "r12", "r13", "r14", "r15", // Callee saved.
11     "rdi", "rsi", "rdx", "rcx", "r8", "r9",   // Int args.
12 }};
13
14 std::array<const char *, 8> X86XMMsToSave = {{
15     "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7" // FP args
16 }};
17
18 template <typename OStream> unsigned saveX86Regs(OStream &OS) {
19   for (const auto &GPR : X86GPRsToSave)
20     OS << "  pushq   %" << GPR << "\n";
21
22   OS << "  subq    $" << (16 * X86XMMsToSave.size()) << ", %rsp\n";
23
24   for (unsigned i = 0; i < X86XMMsToSave.size(); ++i)
25     OS << "  movdqu  %" << X86XMMsToSave[i] << ", "
26        << (16 * (X86XMMsToSave.size() - i - 1)) << "(%rsp)\n";
27
28   return (8 * X86GPRsToSave.size()) + (16 * X86XMMsToSave.size());
29 }
30
31 template <typename OStream> void restoreX86Regs(OStream &OS) {
32   for (unsigned i = 0; i < X86XMMsToSave.size(); ++i)
33     OS << "  movdqu  " << (16 * i) << "(%rsp), %"
34        << X86XMMsToSave[(X86XMMsToSave.size() - i - 1)] << "\n";
35   OS << "  addq    $" << (16 * X86XMMsToSave.size()) << ", %rsp\n";
36
37   for (unsigned i = 0; i < X86GPRsToSave.size(); ++i)
38     OS << "  popq    %" << X86GPRsToSave[X86GPRsToSave.size() - i - 1] << "\n";
39 }
40
41 template <typename TargetT>
42 uint64_t executeCompileCallback(JITCompileCallbackManagerBase<TargetT> *JCBM,
43                                 TargetAddress CallbackID) {
44   return JCBM->executeCompileCallback(CallbackID);
45 }
46
47 }
48
49 namespace llvm {
50 namespace orc {
51
52 const char* OrcX86_64::ResolverBlockName = "orc_resolver_block";
53
54 void OrcX86_64::insertResolverBlock(
55     Module &M, JITCompileCallbackManagerBase<OrcX86_64> &JCBM) {
56   auto CallbackPtr = executeCompileCallback<OrcX86_64>;
57   uint64_t CallbackAddr =
58       static_cast<uint64_t>(reinterpret_cast<uintptr_t>(CallbackPtr));
59
60   std::ostringstream AsmStream;
61   Triple TT(M.getTargetTriple());
62
63   if (TT.getOS() == Triple::Darwin)
64     AsmStream << ".section __TEXT,__text,regular,pure_instructions\n"
65               << ".align 4, 0x90\n";
66   else
67     AsmStream << ".text\n"
68               << ".align 16, 0x90\n";
69
70   AsmStream << "jit_callback_manager_addr:\n"
71             << "  .quad " << &JCBM << "\n"
72             << ResolverBlockName << ":\n";
73
74   uint64_t ReturnAddrOffset = saveX86Regs(AsmStream);
75
76   // Compute index, load object address, and call JIT.
77   AsmStream << "  leaq    jit_callback_manager_addr(%rip), %rdi\n"
78             << "  movq    (%rdi), %rdi\n"
79             << "  movq    " << ReturnAddrOffset << "(%rsp), %rsi\n"
80             << "  movabsq $" << CallbackAddr << ", %rax\n"
81             << "  callq   *%rax\n"
82             << "  movq    %rax, " << ReturnAddrOffset << "(%rsp)\n";
83
84   restoreX86Regs(AsmStream);
85
86   AsmStream << "  retq\n";
87
88   M.appendModuleInlineAsm(AsmStream.str());
89 }
90
91 OrcX86_64::LabelNameFtor
92 OrcX86_64::insertCompileCallbackTrampolines(Module &M,
93                                             TargetAddress ResolverBlockAddr,
94                                             unsigned NumCalls,
95                                             unsigned StartIndex) {
96   const char *ResolverBlockPtrName = "Lorc_resolve_block_addr";
97
98   std::ostringstream AsmStream;
99   Triple TT(M.getTargetTriple());
100
101   if (TT.getOS() == Triple::Darwin)
102     AsmStream << ".section __TEXT,__text,regular,pure_instructions\n"
103               << ".align 4, 0x90\n";
104   else
105     AsmStream << ".text\n"
106               << ".align 16, 0x90\n";
107
108   AsmStream << ResolverBlockPtrName << ":\n"
109             << "  .quad " << ResolverBlockAddr << "\n";
110
111   auto GetLabelName =
112     [=](unsigned I) {
113       std::ostringstream LabelStream;
114       LabelStream << "orc_jcc_" << (StartIndex + I);
115       return LabelStream.str();
116   };
117
118   for (unsigned I = 0; I < NumCalls; ++I)
119     AsmStream << GetLabelName(I) << ":\n"
120               << "  callq *" << ResolverBlockPtrName << "(%rip)\n";
121
122   M.appendModuleInlineAsm(AsmStream.str());
123
124   return GetLabelName;
125 }
126
127 } // End namespace orc.
128 } // End namespace llvm.