cd934216c9354350dcd031ce7c3802b1c43c9fcd
[oota-llvm.git] / include / llvm / ExecutionEngine / Orc / OrcArchitectureSupport.h
1 //===-- OrcArchitectureSupport.h - Architecture support code  ---*- C++ -*-===//
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 // Architecture specific code for Orc, e.g. callback assembly.
11 //
12 // Architecture classes should be part of the JIT *target* process, not the host
13 // process (except where you're doing hosted JITing and the two are one and the
14 // same).
15 //
16 //===----------------------------------------------------------------------===//
17
18 #ifndef LLVM_EXECUTIONENGINE_ORC_ORCARCHITECTURESUPPORT_H
19 #define LLVM_EXECUTIONENGINE_ORC_ORCARCHITECTURESUPPORT_H
20
21 #include "IndirectionUtils.h"
22 #include "llvm/Support/Memory.h"
23 #include "llvm/Support/Process.h"
24
25 namespace llvm {
26 namespace orc {
27
28 class OrcX86_64 {
29 public:
30   static const unsigned PointerSize = 8;
31   static const unsigned TrampolineSize = 8;
32   static const unsigned ResolverCodeSize = 0x78;
33
34   typedef TargetAddress (*JITReentryFn)(void *CallbackMgr,
35                                         void *TrampolineId);
36
37   /// @brief Write the resolver code into the given memory. The user is be
38   ///        responsible for allocating the memory and setting permissions.
39   static void writeResolverCode(uint8_t *ResolveMem, JITReentryFn Reentry,
40                                 void *CallbackMgr);
41
42   /// @brief Write the requsted number of trampolines into the given memory,
43   ///        which must be big enough to hold 1 pointer, plus NumTrampolines
44   ///        trampolines.
45   static void writeTrampolines(uint8_t *TrampolineMem, void *ResolverAddr,
46                                unsigned NumTrampolines);
47
48   /// @brief Provide information about stub blocks generated by the
49   ///        makeIndirectStubsBlock function.
50   class IndirectStubsInfo {
51     friend class OrcX86_64;
52   public:
53     const static unsigned StubSize = 8;
54
55     IndirectStubsInfo() : NumStubs(0) {}
56     IndirectStubsInfo(IndirectStubsInfo &&Other)
57         : NumStubs(Other.NumStubs), StubsMem(std::move(Other.StubsMem)) {
58       Other.NumStubs = 0;
59     }
60     IndirectStubsInfo& operator=(IndirectStubsInfo &&Other) {
61       NumStubs = Other.NumStubs;
62       Other.NumStubs = 0;
63       StubsMem = std::move(Other.StubsMem);
64       return *this;
65     }
66
67     /// @brief Number of stubs in this block.
68     unsigned getNumStubs() const { return NumStubs; }
69
70     /// @brief Get a pointer to the stub at the given index, which must be in
71     ///        the range 0 .. getNumStubs() - 1.
72     void* getStub(unsigned Idx) const {
73       return static_cast<uint64_t*>(StubsMem.base()) + Idx;
74     }
75
76     /// @brief Get a pointer to the implementation-pointer at the given index,
77     ///        which must be in the range 0 .. getNumStubs() - 1.
78     void** getPtr(unsigned Idx) const {
79       char *PtrsBase =
80         static_cast<char*>(StubsMem.base()) + NumStubs * StubSize;
81       return reinterpret_cast<void**>(PtrsBase) + Idx;
82     }
83   private:
84     unsigned NumStubs;
85     sys::OwningMemoryBlock StubsMem;
86   };
87
88   /// @brief Emit at least MinStubs worth of indirect call stubs, rounded out to
89   ///        the nearest page size.
90   ///
91   ///   E.g. Asking for 4 stubs on x86-64, where stubs are 8-bytes, with 4k
92   /// pages will return a block of 512 stubs (4096 / 8 = 512). Asking for 513
93   /// will return a block of 1024 (2-pages worth).
94   static std::error_code emitIndirectStubsBlock(IndirectStubsInfo &StubsInfo,
95                                                 unsigned MinStubs,
96                                                 void *InitialPtrVal);
97 };
98
99 } // End namespace orc.
100 } // End namespace llvm.
101
102 #endif // LLVM_EXECUTIONENGINE_ORC_ORCARCHITECTURESUPPORT_H