Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 1 | //===-- JITEmitter.cpp - Write machine code to executable memory ----------===// |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 10 | // This file defines a MachineCodeEmitter object that is used by the JIT to |
| 11 | // write machine code to memory and remember where relocatable values are. |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chris Lattner | 3785fad | 2003-08-05 17:00:32 +0000 | [diff] [blame] | 15 | #define DEBUG_TYPE "jit" |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 16 | #include "JIT.h" |
Chris Lattner | 2c0a6a1 | 2003-11-30 04:23:21 +0000 | [diff] [blame] | 17 | #include "llvm/Constant.h" |
| 18 | #include "llvm/Module.h" |
Chris Lattner | 5b3a455 | 2005-03-17 15:38:16 +0000 | [diff] [blame] | 19 | #include "llvm/Type.h" |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineCodeEmitter.h" |
| 21 | #include "llvm/CodeGen/MachineFunction.h" |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineConstantPool.h" |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineRelocation.h" |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetJITInfo.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Debug.h" |
| 27 | #include "llvm/ADT/Statistic.h" |
Reid Spencer | 52b0ba6 | 2004-09-11 04:31:03 +0000 | [diff] [blame] | 28 | #include "llvm/System/Memory.h" |
Andrew Lenharth | a00269b | 2005-07-29 23:40:16 +0000 | [diff] [blame] | 29 | #include <list> |
| 30 | #include <algorithm> |
Chris Lattner | c19aade | 2003-12-08 08:06:28 +0000 | [diff] [blame] | 31 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 32 | |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 33 | namespace { |
Chris Lattner | e738656 | 2003-10-20 05:45:49 +0000 | [diff] [blame] | 34 | Statistic<> NumBytes("jit", "Number of bytes of machine code compiled"); |
Chris Lattner | e884dc2 | 2005-07-20 16:29:20 +0000 | [diff] [blame] | 35 | Statistic<> NumRelos("jit", "Number of relocations applied"); |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 36 | JIT *TheJIT = 0; |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 37 | } |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 38 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 39 | |
| 40 | //===----------------------------------------------------------------------===// |
| 41 | // JITMemoryManager code. |
| 42 | // |
| 43 | namespace { |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 44 | /// JITMemoryManager - Manage memory for the JIT code generation in a logical, |
| 45 | /// sane way. This splits a large block of MAP_NORESERVE'd memory into two |
| 46 | /// sections, one for function stubs, one for the functions themselves. We |
| 47 | /// have to do this because we may need to emit a function stub while in the |
| 48 | /// middle of emitting a function, and we don't know how large the function we |
| 49 | /// are emitting is. This never bothers to release the memory, because when |
| 50 | /// we are ready to destroy the JIT, the program exits. |
| 51 | class JITMemoryManager { |
Andrew Lenharth | a00269b | 2005-07-29 23:40:16 +0000 | [diff] [blame] | 52 | std::list<sys::MemoryBlock> Blocks; // List of blocks allocated by the JIT |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 53 | unsigned char *FunctionBase; // Start of the function body area |
Andrew Lenharth | a00269b | 2005-07-29 23:40:16 +0000 | [diff] [blame] | 54 | unsigned char *GlobalBase; // Start of the Global area |
| 55 | unsigned char *ConstantBase; // Memory allocated for constant pools |
| 56 | unsigned char *CurStubPtr, *CurFunctionPtr, *CurConstantPtr, *CurGlobalPtr; |
Andrew Lenharth | 16ec33c | 2005-07-22 20:48:12 +0000 | [diff] [blame] | 57 | unsigned char *GOTBase; //Target Specific reserved memory |
Andrew Lenharth | a00269b | 2005-07-29 23:40:16 +0000 | [diff] [blame] | 58 | |
| 59 | // centralize memory block allocation |
| 60 | sys::MemoryBlock getNewMemoryBlock(unsigned size); |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 61 | public: |
Andrew Lenharth | 16ec33c | 2005-07-22 20:48:12 +0000 | [diff] [blame] | 62 | JITMemoryManager(bool useGOT); |
Reid Spencer | 4af3da6 | 2004-12-13 16:04:04 +0000 | [diff] [blame] | 63 | ~JITMemoryManager(); |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 64 | |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 65 | inline unsigned char *allocateStub(unsigned StubSize); |
Chris Lattner | 281a601 | 2005-01-10 18:23:22 +0000 | [diff] [blame] | 66 | inline unsigned char *allocateConstant(unsigned ConstantSize, |
| 67 | unsigned Alignment); |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame^] | 68 | inline unsigned char* allocateGlobal(unsigned Size, |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 69 | unsigned Alignment); |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 70 | inline unsigned char *startFunctionBody(); |
Chris Lattner | 281a601 | 2005-01-10 18:23:22 +0000 | [diff] [blame] | 71 | inline void endFunctionBody(unsigned char *FunctionEnd); |
Andrew Lenharth | 16ec33c | 2005-07-22 20:48:12 +0000 | [diff] [blame] | 72 | inline unsigned char* getGOTBase() const; |
| 73 | |
| 74 | inline bool isManagingGOT() const; |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 75 | }; |
| 76 | } |
| 77 | |
Andrew Lenharth | 16ec33c | 2005-07-22 20:48:12 +0000 | [diff] [blame] | 78 | JITMemoryManager::JITMemoryManager(bool useGOT) { |
Andrew Lenharth | a00269b | 2005-07-29 23:40:16 +0000 | [diff] [blame] | 79 | // Allocate a 16M block of memory for functions |
| 80 | sys::MemoryBlock FunBlock = getNewMemoryBlock(16 << 20); |
| 81 | // Allocate a 1M block of memory for Constants |
| 82 | sys::MemoryBlock ConstBlock = getNewMemoryBlock(1 << 20); |
| 83 | // Allocate a 1M Block of memory for Globals |
| 84 | sys::MemoryBlock GVBlock = getNewMemoryBlock(1 << 20); |
Andrew Lenharth | 16ec33c | 2005-07-22 20:48:12 +0000 | [diff] [blame] | 85 | |
Andrew Lenharth | a00269b | 2005-07-29 23:40:16 +0000 | [diff] [blame] | 86 | Blocks.push_front(FunBlock); |
| 87 | Blocks.push_front(ConstBlock); |
| 88 | Blocks.push_front(GVBlock); |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 89 | |
Andrew Lenharth | a00269b | 2005-07-29 23:40:16 +0000 | [diff] [blame] | 90 | FunctionBase = reinterpret_cast<unsigned char*>(FunBlock.base()); |
| 91 | ConstantBase = reinterpret_cast<unsigned char*>(ConstBlock.base()); |
| 92 | GlobalBase = reinterpret_cast<unsigned char*>(GVBlock.base()); |
Chris Lattner | 281a601 | 2005-01-10 18:23:22 +0000 | [diff] [blame] | 93 | |
Andrew Lenharth | a00269b | 2005-07-29 23:40:16 +0000 | [diff] [blame] | 94 | //Allocate the GOT just like a global array |
| 95 | GOTBase = NULL; |
| 96 | if (useGOT) |
| 97 | GOTBase = allocateGlobal(sizeof(void*) * 8192, 8); |
| 98 | |
| 99 | // Allocate stubs backwards from the base, allocate functions forward |
| 100 | // from the base. |
| 101 | CurStubPtr = CurFunctionPtr = FunctionBase + 512*1024;// Use 512k for stubs |
| 102 | |
| 103 | CurConstantPtr = ConstantBase + ConstBlock.size(); |
| 104 | CurGlobalPtr = GlobalBase + GVBlock.size(); |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Reid Spencer | 4af3da6 | 2004-12-13 16:04:04 +0000 | [diff] [blame] | 107 | JITMemoryManager::~JITMemoryManager() { |
Andrew Lenharth | a00269b | 2005-07-29 23:40:16 +0000 | [diff] [blame] | 108 | for (std::list<sys::MemoryBlock>::iterator ib = Blocks.begin(), ie = Blocks.end(); |
| 109 | ib != ie; ++ib) |
| 110 | sys::Memory::ReleaseRWX(*ib); |
| 111 | Blocks.clear(); |
Reid Spencer | 4af3da6 | 2004-12-13 16:04:04 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 114 | unsigned char *JITMemoryManager::allocateStub(unsigned StubSize) { |
| 115 | CurStubPtr -= StubSize; |
Andrew Lenharth | a00269b | 2005-07-29 23:40:16 +0000 | [diff] [blame] | 116 | if (CurStubPtr < FunctionBase) { |
| 117 | //FIXME: allocate a new block |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 118 | std::cerr << "JIT ran out of memory for function stubs!\n"; |
| 119 | abort(); |
| 120 | } |
| 121 | return CurStubPtr; |
| 122 | } |
| 123 | |
Chris Lattner | 281a601 | 2005-01-10 18:23:22 +0000 | [diff] [blame] | 124 | unsigned char *JITMemoryManager::allocateConstant(unsigned ConstantSize, |
| 125 | unsigned Alignment) { |
| 126 | // Reserve space and align pointer. |
| 127 | CurConstantPtr -= ConstantSize; |
| 128 | CurConstantPtr = |
| 129 | (unsigned char *)((intptr_t)CurConstantPtr & ~((intptr_t)Alignment - 1)); |
| 130 | |
Andrew Lenharth | a00269b | 2005-07-29 23:40:16 +0000 | [diff] [blame] | 131 | if (CurConstantPtr < ConstantBase) { |
| 132 | //Either allocate another MB or 2xConstantSize |
| 133 | sys::MemoryBlock ConstBlock = getNewMemoryBlock(2 * ConstantSize); |
| 134 | ConstantBase = reinterpret_cast<unsigned char*>(ConstBlock.base()); |
| 135 | CurConstantPtr = ConstantBase + ConstBlock.size(); |
| 136 | return allocateConstant(ConstantSize, Alignment); |
Chris Lattner | 281a601 | 2005-01-10 18:23:22 +0000 | [diff] [blame] | 137 | } |
| 138 | return CurConstantPtr; |
| 139 | } |
| 140 | |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 141 | unsigned char *JITMemoryManager::allocateGlobal(unsigned Size, |
| 142 | unsigned Alignment) { |
Andrew Lenharth | a00269b | 2005-07-29 23:40:16 +0000 | [diff] [blame] | 143 | // Reserve space and align pointer. |
| 144 | CurGlobalPtr -= Size; |
| 145 | CurGlobalPtr = |
| 146 | (unsigned char *)((intptr_t)CurGlobalPtr & ~((intptr_t)Alignment - 1)); |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 147 | |
Andrew Lenharth | a00269b | 2005-07-29 23:40:16 +0000 | [diff] [blame] | 148 | if (CurGlobalPtr < GlobalBase) { |
| 149 | //Either allocate another MB or 2xSize |
| 150 | sys::MemoryBlock GVBlock = getNewMemoryBlock(2 * Size); |
| 151 | GlobalBase = reinterpret_cast<unsigned char*>(GVBlock.base()); |
| 152 | CurGlobalPtr = GlobalBase + GVBlock.size(); |
| 153 | return allocateGlobal(Size, Alignment); |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 154 | } |
Andrew Lenharth | a00269b | 2005-07-29 23:40:16 +0000 | [diff] [blame] | 155 | return CurGlobalPtr; |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 158 | unsigned char *JITMemoryManager::startFunctionBody() { |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 159 | // Round up to an even multiple of 8 bytes, this should eventually be target |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 160 | // specific. |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 161 | return (unsigned char*)(((intptr_t)CurFunctionPtr + 7) & ~7); |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | void JITMemoryManager::endFunctionBody(unsigned char *FunctionEnd) { |
| 165 | assert(FunctionEnd > CurFunctionPtr); |
| 166 | CurFunctionPtr = FunctionEnd; |
| 167 | } |
| 168 | |
Andrew Lenharth | 16ec33c | 2005-07-22 20:48:12 +0000 | [diff] [blame] | 169 | unsigned char* JITMemoryManager::getGOTBase() const { |
| 170 | return GOTBase; |
| 171 | } |
| 172 | |
| 173 | bool JITMemoryManager::isManagingGOT() const { |
| 174 | return GOTBase != NULL; |
| 175 | } |
| 176 | |
Andrew Lenharth | a00269b | 2005-07-29 23:40:16 +0000 | [diff] [blame] | 177 | sys::MemoryBlock JITMemoryManager::getNewMemoryBlock(unsigned size) { |
| 178 | const sys::MemoryBlock* BOld = 0; |
| 179 | if (Blocks.size()) |
| 180 | BOld = &Blocks.front(); |
| 181 | //never allocate less than 1 MB |
| 182 | sys::MemoryBlock B; |
| 183 | try { |
| 184 | B = sys::Memory::AllocateRWX(std::max(((unsigned)1 << 20), size), BOld); |
| 185 | } catch (std::string& err) { |
| 186 | std::cerr << "Allocation failed when allocating new memory in the JIT\n"; |
| 187 | std::cerr << err << "\n"; |
| 188 | abort(); |
| 189 | } |
| 190 | Blocks.push_front(B); |
| 191 | return B; |
| 192 | } |
| 193 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 194 | //===----------------------------------------------------------------------===// |
| 195 | // JIT lazy compilation code. |
| 196 | // |
| 197 | namespace { |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 198 | class JITResolverState { |
| 199 | private: |
| 200 | /// FunctionToStubMap - Keep track of the stub created for a particular |
| 201 | /// function so that we can reuse them if necessary. |
| 202 | std::map<Function*, void*> FunctionToStubMap; |
| 203 | |
| 204 | /// StubToFunctionMap - Keep track of the function that each stub |
| 205 | /// corresponds to. |
| 206 | std::map<void*, Function*> StubToFunctionMap; |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 207 | |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 208 | public: |
| 209 | std::map<Function*, void*>& getFunctionToStubMap(const MutexGuard& locked) { |
| 210 | assert(locked.holds(TheJIT->lock)); |
| 211 | return FunctionToStubMap; |
| 212 | } |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 213 | |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 214 | std::map<void*, Function*>& getStubToFunctionMap(const MutexGuard& locked) { |
| 215 | assert(locked.holds(TheJIT->lock)); |
| 216 | return StubToFunctionMap; |
| 217 | } |
| 218 | }; |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 219 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 220 | /// JITResolver - Keep track of, and resolve, call sites for functions that |
| 221 | /// have not yet been compiled. |
| 222 | class JITResolver { |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 223 | /// MCE - The MachineCodeEmitter to use to emit stubs with. |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 224 | MachineCodeEmitter &MCE; |
| 225 | |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 226 | /// LazyResolverFn - The target lazy resolver function that we actually |
| 227 | /// rewrite instructions to use. |
| 228 | TargetJITInfo::LazyResolverFn LazyResolverFn; |
| 229 | |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 230 | JITResolverState state; |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 231 | |
Chris Lattner | d91ff7c | 2005-04-18 01:44:27 +0000 | [diff] [blame] | 232 | /// ExternalFnToStubMap - This is the equivalent of FunctionToStubMap for |
| 233 | /// external functions. |
| 234 | std::map<void*, void*> ExternalFnToStubMap; |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 235 | |
| 236 | //map addresses to indexes in the GOT |
| 237 | std::map<void*, unsigned> revGOTMap; |
| 238 | unsigned nextGOTIndex; |
| 239 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 240 | public: |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 241 | JITResolver(MachineCodeEmitter &mce) : MCE(mce), nextGOTIndex(0) { |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 242 | LazyResolverFn = |
| 243 | TheJIT->getJITInfo().getLazyResolverFunction(JITCompilerFn); |
| 244 | } |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 245 | |
| 246 | /// getFunctionStub - This returns a pointer to a function stub, creating |
| 247 | /// one on demand as needed. |
| 248 | void *getFunctionStub(Function *F); |
| 249 | |
Chris Lattner | d91ff7c | 2005-04-18 01:44:27 +0000 | [diff] [blame] | 250 | /// getExternalFunctionStub - Return a stub for the function at the |
| 251 | /// specified address, created lazily on demand. |
| 252 | void *getExternalFunctionStub(void *FnAddr); |
| 253 | |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 254 | /// AddCallbackAtLocation - If the target is capable of rewriting an |
| 255 | /// instruction without the use of a stub, record the location of the use so |
| 256 | /// we know which function is being used at the location. |
| 257 | void *AddCallbackAtLocation(Function *F, void *Location) { |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 258 | MutexGuard locked(TheJIT->lock); |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 259 | /// Get the target-specific JIT resolver function. |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 260 | state.getStubToFunctionMap(locked)[Location] = F; |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 261 | return (void*)LazyResolverFn; |
| 262 | } |
| 263 | |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 264 | /// getGOTIndexForAddress - Return a new or existing index in the GOT for |
| 265 | /// and address. This function only manages slots, it does not manage the |
| 266 | /// contents of the slots or the memory associated with the GOT. |
| 267 | unsigned getGOTIndexForAddr(void* addr); |
| 268 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 269 | /// JITCompilerFn - This function is called to resolve a stub to a compiled |
| 270 | /// address. If the LLVM Function corresponding to the stub has not yet |
| 271 | /// been compiled, this function compiles it first. |
| 272 | static void *JITCompilerFn(void *Stub); |
| 273 | }; |
| 274 | } |
| 275 | |
| 276 | /// getJITResolver - This function returns the one instance of the JIT resolver. |
| 277 | /// |
| 278 | static JITResolver &getJITResolver(MachineCodeEmitter *MCE = 0) { |
| 279 | static JITResolver TheJITResolver(*MCE); |
| 280 | return TheJITResolver; |
| 281 | } |
| 282 | |
| 283 | /// getFunctionStub - This returns a pointer to a function stub, creating |
| 284 | /// one on demand as needed. |
| 285 | void *JITResolver::getFunctionStub(Function *F) { |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 286 | MutexGuard locked(TheJIT->lock); |
| 287 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 288 | // If we already have a stub for this function, recycle it. |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 289 | void *&Stub = state.getFunctionToStubMap(locked)[F]; |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 290 | if (Stub) return Stub; |
| 291 | |
Chris Lattner | b43dbdc | 2004-11-22 07:24:43 +0000 | [diff] [blame] | 292 | // Call the lazy resolver function unless we already KNOW it is an external |
| 293 | // function, in which case we just skip the lazy resolution step. |
| 294 | void *Actual = (void*)LazyResolverFn; |
Chris Lattner | 6943570 | 2005-02-20 18:43:35 +0000 | [diff] [blame] | 295 | if (F->isExternal() && F->hasExternalLinkage()) |
Chris Lattner | b43dbdc | 2004-11-22 07:24:43 +0000 | [diff] [blame] | 296 | Actual = TheJIT->getPointerToFunction(F); |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 297 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 298 | // Otherwise, codegen a new stub. For now, the stub will call the lazy |
| 299 | // resolver function. |
Chris Lattner | b43dbdc | 2004-11-22 07:24:43 +0000 | [diff] [blame] | 300 | Stub = TheJIT->getJITInfo().emitFunctionStub(Actual, MCE); |
| 301 | |
Chris Lattner | 6943570 | 2005-02-20 18:43:35 +0000 | [diff] [blame] | 302 | if (Actual != (void*)LazyResolverFn) { |
Chris Lattner | b43dbdc | 2004-11-22 07:24:43 +0000 | [diff] [blame] | 303 | // If we are getting the stub for an external function, we really want the |
| 304 | // address of the stub in the GlobalAddressMap for the JIT, not the address |
| 305 | // of the external function. |
| 306 | TheJIT->updateGlobalMapping(F, Stub); |
| 307 | } |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 308 | |
Chris Lattner | cb47941 | 2004-11-21 03:44:32 +0000 | [diff] [blame] | 309 | DEBUG(std::cerr << "JIT: Stub emitted at [" << Stub << "] for function '" |
Chris Lattner | 6f71720 | 2004-11-22 21:48:33 +0000 | [diff] [blame] | 310 | << F->getName() << "'\n"); |
Chris Lattner | cb47941 | 2004-11-21 03:44:32 +0000 | [diff] [blame] | 311 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 312 | // Finally, keep track of the stub-to-Function mapping so that the |
| 313 | // JITCompilerFn knows which function to compile! |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 314 | state.getStubToFunctionMap(locked)[Stub] = F; |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 315 | return Stub; |
| 316 | } |
| 317 | |
Chris Lattner | d91ff7c | 2005-04-18 01:44:27 +0000 | [diff] [blame] | 318 | /// getExternalFunctionStub - Return a stub for the function at the |
| 319 | /// specified address, created lazily on demand. |
| 320 | void *JITResolver::getExternalFunctionStub(void *FnAddr) { |
| 321 | // If we already have a stub for this function, recycle it. |
| 322 | void *&Stub = ExternalFnToStubMap[FnAddr]; |
| 323 | if (Stub) return Stub; |
| 324 | |
| 325 | Stub = TheJIT->getJITInfo().emitFunctionStub(FnAddr, MCE); |
| 326 | DEBUG(std::cerr << "JIT: Stub emitted at [" << Stub |
| 327 | << "] for external function at '" << FnAddr << "'\n"); |
| 328 | return Stub; |
| 329 | } |
| 330 | |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 331 | unsigned JITResolver::getGOTIndexForAddr(void* addr) { |
| 332 | unsigned idx = revGOTMap[addr]; |
| 333 | if (!idx) { |
| 334 | idx = ++nextGOTIndex; |
| 335 | revGOTMap[addr] = idx; |
| 336 | DEBUG(std::cerr << "Adding GOT entry " << idx |
| 337 | << " for addr " << addr << "\n"); |
| 338 | // ((void**)MemMgr.getGOTBase())[idx] = addr; |
| 339 | } |
| 340 | return idx; |
| 341 | } |
Chris Lattner | d91ff7c | 2005-04-18 01:44:27 +0000 | [diff] [blame] | 342 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 343 | /// JITCompilerFn - This function is called when a lazy compilation stub has |
| 344 | /// been entered. It looks up which function this stub corresponds to, compiles |
| 345 | /// it if necessary, then returns the resultant function pointer. |
| 346 | void *JITResolver::JITCompilerFn(void *Stub) { |
| 347 | JITResolver &JR = getJITResolver(); |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 348 | |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 349 | MutexGuard locked(TheJIT->lock); |
| 350 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 351 | // The address given to us for the stub may not be exactly right, it might be |
| 352 | // a little bit after the stub. As such, use upper_bound to find it. |
| 353 | std::map<void*, Function*>::iterator I = |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 354 | JR.state.getStubToFunctionMap(locked).upper_bound(Stub); |
| 355 | assert(I != JR.state.getStubToFunctionMap(locked).begin() && "This is not a known stub!"); |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 356 | Function *F = (--I)->second; |
| 357 | |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 358 | // We might like to remove the stub from the StubToFunction map. |
| 359 | // We can't do that! Multiple threads could be stuck, waiting to acquire the |
| 360 | // lock above. As soon as the 1st function finishes compiling the function, |
| 361 | // the next one will be released, and needs to be able to find the function it needs |
| 362 | // to call. |
| 363 | //JR.state.getStubToFunctionMap(locked).erase(I); |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 364 | |
Chris Lattner | cb47941 | 2004-11-21 03:44:32 +0000 | [diff] [blame] | 365 | DEBUG(std::cerr << "JIT: Lazily resolving function '" << F->getName() |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 366 | << "' In stub ptr = " << Stub << " actual ptr = " |
| 367 | << I->first << "\n"); |
| 368 | |
| 369 | void *Result = TheJIT->getPointerToFunction(F); |
| 370 | |
| 371 | // We don't need to reuse this stub in the future, as F is now compiled. |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 372 | JR.state.getFunctionToStubMap(locked).erase(F); |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 373 | |
| 374 | // FIXME: We could rewrite all references to this stub if we knew them. |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 375 | |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame^] | 376 | // What we will do is set the compiled function address to map to the |
| 377 | // same GOT entry as the stub so that later clients may update the GOT |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 378 | // if they see it still using the stub address. |
| 379 | // Note: this is done so the Resolver doesn't have to manage GOT memory |
| 380 | // Do this without allocating map space if the target isn't using a GOT |
| 381 | if(JR.revGOTMap.find(Stub) != JR.revGOTMap.end()) |
| 382 | JR.revGOTMap[Result] = JR.revGOTMap[Stub]; |
| 383 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 384 | return Result; |
| 385 | } |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 386 | |
| 387 | |
Chris Lattner | e518b71 | 2004-12-05 07:19:16 +0000 | [diff] [blame] | 388 | // getPointerToFunctionOrStub - If the specified function has been |
| 389 | // code-gen'd, return a pointer to the function. If not, compile it, or use |
| 390 | // a stub to implement lazy compilation if available. |
| 391 | // |
| 392 | void *JIT::getPointerToFunctionOrStub(Function *F) { |
| 393 | // If we have already code generated the function, just return the address. |
| 394 | if (void *Addr = getPointerToGlobalIfAvailable(F)) |
| 395 | return Addr; |
| 396 | |
| 397 | // Get a stub if the target supports it |
| 398 | return getJITResolver(MCE).getFunctionStub(F); |
| 399 | } |
| 400 | |
| 401 | |
| 402 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 403 | //===----------------------------------------------------------------------===// |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 404 | // JITEmitter code. |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 405 | // |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 406 | namespace { |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 407 | /// JITEmitter - The JIT implementation of the MachineCodeEmitter, which is |
| 408 | /// used to output functions to memory for execution. |
| 409 | class JITEmitter : public MachineCodeEmitter { |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 410 | JITMemoryManager MemMgr; |
| 411 | |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 412 | // CurBlock - The start of the current block of memory. CurByte - The |
| 413 | // current byte being emitted to. |
Chris Lattner | 6125fdd | 2003-05-09 03:30:07 +0000 | [diff] [blame] | 414 | unsigned char *CurBlock, *CurByte; |
| 415 | |
| 416 | // When outputting a function stub in the context of some other function, we |
| 417 | // save CurBlock and CurByte here. |
| 418 | unsigned char *SavedCurBlock, *SavedCurByte; |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 419 | |
| 420 | // ConstantPoolAddresses - Contains the location for each entry in the |
| 421 | // constant pool. |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 422 | std::vector<void*> ConstantPoolAddresses; |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 423 | |
| 424 | /// Relocations - These are the relocations that the function needs, as |
| 425 | /// emitted. |
| 426 | std::vector<MachineRelocation> Relocations; |
Andrew Lenharth | 16ec33c | 2005-07-22 20:48:12 +0000 | [diff] [blame] | 427 | |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 428 | public: |
Andrew Lenharth | 16ec33c | 2005-07-22 20:48:12 +0000 | [diff] [blame] | 429 | JITEmitter(JIT &jit) |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 430 | :MemMgr(jit.getJITInfo().needsGOT()) |
Andrew Lenharth | 16ec33c | 2005-07-22 20:48:12 +0000 | [diff] [blame] | 431 | { |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 432 | TheJIT = &jit; |
| 433 | DEBUG(std::cerr << |
| 434 | (MemMgr.isManagingGOT() ? "JIT is managing GOT\n" |
Andrew Lenharth | 16ec33c | 2005-07-22 20:48:12 +0000 | [diff] [blame] | 435 | : "JIT is not managing GOT\n")); |
| 436 | } |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 437 | |
| 438 | virtual void startFunction(MachineFunction &F); |
| 439 | virtual void finishFunction(MachineFunction &F); |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 440 | virtual void emitConstantPool(MachineConstantPool *MCP); |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 441 | virtual void startFunctionStub(unsigned StubSize); |
| 442 | virtual void* finishFunctionStub(const Function *F); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 443 | virtual void emitByte(unsigned char B); |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 444 | virtual void emitWord(unsigned W); |
Brian Gaeke | aea1b58 | 2004-04-23 17:11:14 +0000 | [diff] [blame] | 445 | virtual void emitWordAt(unsigned W, unsigned *Ptr); |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 446 | |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 447 | virtual void addRelocation(const MachineRelocation &MR) { |
| 448 | Relocations.push_back(MR); |
| 449 | } |
| 450 | |
| 451 | virtual uint64_t getCurrentPCValue(); |
| 452 | virtual uint64_t getCurrentPCOffset(); |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 453 | virtual uint64_t getConstantPoolEntryAddress(unsigned Entry); |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 454 | virtual unsigned char* allocateGlobal(unsigned size, unsigned alignment); |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 455 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 456 | private: |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 457 | void *getPointerToGlobal(GlobalValue *GV, void *Reference, bool NoNeedStub); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 458 | }; |
| 459 | } |
| 460 | |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 461 | MachineCodeEmitter *JIT::createEmitter(JIT &jit) { |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 462 | return new JITEmitter(jit); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 465 | void *JITEmitter::getPointerToGlobal(GlobalValue *V, void *Reference, |
| 466 | bool DoesntNeedStub) { |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 467 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) { |
| 468 | /// FIXME: If we straightened things out, this could actually emit the |
| 469 | /// global immediately instead of queuing it for codegen later! |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 470 | return TheJIT->getOrEmitGlobalVariable(GV); |
| 471 | } |
| 472 | |
| 473 | // If we have already compiled the function, return a pointer to its body. |
| 474 | Function *F = cast<Function>(V); |
| 475 | void *ResultPtr = TheJIT->getPointerToGlobalIfAvailable(F); |
| 476 | if (ResultPtr) return ResultPtr; |
| 477 | |
Chris Lattner | 532343b | 2004-11-30 17:41:49 +0000 | [diff] [blame] | 478 | if (F->hasExternalLinkage() && F->isExternal()) { |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 479 | // If this is an external function pointer, we can force the JIT to |
| 480 | // 'compile' it, which really just adds it to the map. |
Chris Lattner | b43dbdc | 2004-11-22 07:24:43 +0000 | [diff] [blame] | 481 | if (DoesntNeedStub) |
| 482 | return TheJIT->getPointerToFunction(F); |
| 483 | |
| 484 | return getJITResolver(this).getFunctionStub(F); |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 485 | } |
| 486 | |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 487 | // Okay, the function has not been compiled yet, if the target callback |
| 488 | // mechanism is capable of rewriting the instruction directly, prefer to do |
| 489 | // that instead of emitting a stub. |
| 490 | if (DoesntNeedStub) |
| 491 | return getJITResolver(this).AddCallbackAtLocation(F, Reference); |
| 492 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 493 | // Otherwise, we have to emit a lazy resolving stub. |
| 494 | return getJITResolver(this).getFunctionStub(F); |
| 495 | } |
| 496 | |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 497 | void JITEmitter::startFunction(MachineFunction &F) { |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 498 | CurByte = CurBlock = MemMgr.startFunctionBody(); |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 499 | TheJIT->addGlobalMapping(F.getFunction(), CurBlock); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 500 | } |
| 501 | |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 502 | void JITEmitter::finishFunction(MachineFunction &F) { |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 503 | MemMgr.endFunctionBody(CurByte); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 504 | NumBytes += CurByte-CurBlock; |
| 505 | |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 506 | if (!Relocations.empty()) { |
Chris Lattner | e884dc2 | 2005-07-20 16:29:20 +0000 | [diff] [blame] | 507 | NumRelos += Relocations.size(); |
| 508 | |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 509 | // Resolve the relocations to concrete pointers. |
| 510 | for (unsigned i = 0, e = Relocations.size(); i != e; ++i) { |
| 511 | MachineRelocation &MR = Relocations[i]; |
| 512 | void *ResultPtr; |
Chris Lattner | d91ff7c | 2005-04-18 01:44:27 +0000 | [diff] [blame] | 513 | if (MR.isString()) { |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 514 | ResultPtr = TheJIT->getPointerToNamedFunction(MR.getString()); |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 515 | |
Chris Lattner | d91ff7c | 2005-04-18 01:44:27 +0000 | [diff] [blame] | 516 | // If the target REALLY wants a stub for this function, emit it now. |
| 517 | if (!MR.doesntNeedFunctionStub()) |
| 518 | ResultPtr = getJITResolver(this).getExternalFunctionStub(ResultPtr); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 519 | } else if (MR.isGlobalValue()) |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 520 | ResultPtr = getPointerToGlobal(MR.getGlobalValue(), |
| 521 | CurBlock+MR.getMachineCodeOffset(), |
| 522 | MR.doesntNeedFunctionStub()); |
Andrew Lenharth | 16ec33c | 2005-07-22 20:48:12 +0000 | [diff] [blame] | 523 | else //ConstantPoolIndex |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 524 | ResultPtr = |
Chris Lattner | d6bbac5 | 2005-07-25 23:42:58 +0000 | [diff] [blame] | 525 | (void*)(intptr_t)getConstantPoolEntryAddress(MR.getConstantPoolIndex()); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 526 | |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 527 | MR.setResultPointer(ResultPtr); |
Andrew Lenharth | 16ec33c | 2005-07-22 20:48:12 +0000 | [diff] [blame] | 528 | |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 529 | // if we are managing the GOT and the relocation wants an index, |
| 530 | // give it one |
| 531 | if (MemMgr.isManagingGOT() && !MR.isConstantPoolIndex() && |
| 532 | MR.isGOTRelative()) { |
| 533 | unsigned idx = getJITResolver(this).getGOTIndexForAddr(ResultPtr); |
| 534 | MR.setGOTIndex(idx); |
| 535 | if (((void**)MemMgr.getGOTBase())[idx] != ResultPtr) { |
| 536 | DEBUG(std::cerr << "GOT was out of date for " << ResultPtr |
| 537 | << " pointing at " << ((void**)MemMgr.getGOTBase())[idx] << "\n"); |
| 538 | ((void**)MemMgr.getGOTBase())[idx] = ResultPtr; |
| 539 | } |
Andrew Lenharth | 16ec33c | 2005-07-22 20:48:12 +0000 | [diff] [blame] | 540 | } |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | TheJIT->getJITInfo().relocate(CurBlock, &Relocations[0], |
Andrew Lenharth | 16ec33c | 2005-07-22 20:48:12 +0000 | [diff] [blame] | 544 | Relocations.size(), MemMgr.getGOTBase()); |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 545 | } |
| 546 | |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 547 | //Update the GOT entry for F to point to the new code. |
| 548 | if(MemMgr.isManagingGOT()) { |
| 549 | unsigned idx = getJITResolver(this).getGOTIndexForAddr((void*)CurBlock); |
| 550 | if (((void**)MemMgr.getGOTBase())[idx] != (void*)CurBlock) { |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame^] | 551 | DEBUG(std::cerr << "GOT was out of date for " << (void*)CurBlock |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 552 | << " pointing at " << ((void**)MemMgr.getGOTBase())[idx] << "\n"); |
| 553 | ((void**)MemMgr.getGOTBase())[idx] = (void*)CurBlock; |
| 554 | } |
| 555 | } |
| 556 | |
Chris Lattner | cb47941 | 2004-11-21 03:44:32 +0000 | [diff] [blame] | 557 | DEBUG(std::cerr << "JIT: Finished CodeGen of [" << (void*)CurBlock |
Misha Brukman | 1d44085 | 2003-06-06 06:52:35 +0000 | [diff] [blame] | 558 | << "] Function: " << F.getFunction()->getName() |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 559 | << ": " << CurByte-CurBlock << " bytes of text, " |
| 560 | << Relocations.size() << " relocations\n"); |
| 561 | Relocations.clear(); |
Andrew Lenharth | 16ec33c | 2005-07-22 20:48:12 +0000 | [diff] [blame] | 562 | ConstantPoolAddresses.clear(); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 563 | } |
| 564 | |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 565 | void JITEmitter::emitConstantPool(MachineConstantPool *MCP) { |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 566 | const std::vector<Constant*> &Constants = MCP->getConstants(); |
Chris Lattner | 2c0a6a1 | 2003-11-30 04:23:21 +0000 | [diff] [blame] | 567 | if (Constants.empty()) return; |
| 568 | |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 569 | for (unsigned i = 0, e = Constants.size(); i != e; ++i) { |
Chris Lattner | 2c0a6a1 | 2003-11-30 04:23:21 +0000 | [diff] [blame] | 570 | const Type *Ty = Constants[i]->getType(); |
Chris Lattner | a8101c1 | 2005-01-08 20:07:03 +0000 | [diff] [blame] | 571 | unsigned Size = (unsigned)TheJIT->getTargetData().getTypeSize(Ty); |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 572 | unsigned Alignment = TheJIT->getTargetData().getTypeAlignment(Ty); |
Chris Lattner | 2c0a6a1 | 2003-11-30 04:23:21 +0000 | [diff] [blame] | 573 | |
Chris Lattner | 281a601 | 2005-01-10 18:23:22 +0000 | [diff] [blame] | 574 | void *Addr = MemMgr.allocateConstant(Size, Alignment); |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 575 | TheJIT->InitializeMemory(Constants[i], Addr); |
Misha Brukman | 91de352 | 2003-11-30 00:50:53 +0000 | [diff] [blame] | 576 | ConstantPoolAddresses.push_back(Addr); |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 577 | } |
| 578 | } |
| 579 | |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 580 | void JITEmitter::startFunctionStub(unsigned StubSize) { |
Chris Lattner | 6125fdd | 2003-05-09 03:30:07 +0000 | [diff] [blame] | 581 | SavedCurBlock = CurBlock; SavedCurByte = CurByte; |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 582 | CurByte = CurBlock = MemMgr.allocateStub(StubSize); |
Chris Lattner | 6125fdd | 2003-05-09 03:30:07 +0000 | [diff] [blame] | 583 | } |
| 584 | |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 585 | void *JITEmitter::finishFunctionStub(const Function *F) { |
Chris Lattner | 6125fdd | 2003-05-09 03:30:07 +0000 | [diff] [blame] | 586 | NumBytes += CurByte-CurBlock; |
Chris Lattner | 6125fdd | 2003-05-09 03:30:07 +0000 | [diff] [blame] | 587 | std::swap(CurBlock, SavedCurBlock); |
| 588 | CurByte = SavedCurByte; |
| 589 | return SavedCurBlock; |
| 590 | } |
| 591 | |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 592 | void JITEmitter::emitByte(unsigned char B) { |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 593 | *CurByte++ = B; // Write the byte to memory |
| 594 | } |
| 595 | |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 596 | void JITEmitter::emitWord(unsigned W) { |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 597 | // This won't work if the endianness of the host and target don't agree! (For |
| 598 | // a JIT this can't happen though. :) |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 599 | *(unsigned*)CurByte = W; |
| 600 | CurByte += sizeof(unsigned); |
| 601 | } |
| 602 | |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 603 | void JITEmitter::emitWordAt(unsigned W, unsigned *Ptr) { |
Brian Gaeke | aea1b58 | 2004-04-23 17:11:14 +0000 | [diff] [blame] | 604 | *Ptr = W; |
| 605 | } |
| 606 | |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 607 | // getConstantPoolEntryAddress - Return the address of the 'ConstantNum' entry |
| 608 | // in the constant pool that was last emitted with the 'emitConstantPool' |
| 609 | // method. |
| 610 | // |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 611 | uint64_t JITEmitter::getConstantPoolEntryAddress(unsigned ConstantNum) { |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 612 | assert(ConstantNum < ConstantPoolAddresses.size() && |
Misha Brukman | 3c94497 | 2005-04-22 04:08:30 +0000 | [diff] [blame] | 613 | "Invalid ConstantPoolIndex!"); |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 614 | return (intptr_t)ConstantPoolAddresses[ConstantNum]; |
| 615 | } |
| 616 | |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 617 | unsigned char* JITEmitter::allocateGlobal(unsigned size, unsigned alignment) |
| 618 | { |
| 619 | return MemMgr.allocateGlobal(size, alignment); |
| 620 | } |
| 621 | |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 622 | // getCurrentPCValue - This returns the address that the next emitted byte |
| 623 | // will be output to. |
| 624 | // |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 625 | uint64_t JITEmitter::getCurrentPCValue() { |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 626 | return (intptr_t)CurByte; |
| 627 | } |
| 628 | |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 629 | uint64_t JITEmitter::getCurrentPCOffset() { |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 630 | return (intptr_t)CurByte-(intptr_t)CurBlock; |
| 631 | } |
| 632 | |
Misha Brukman | d69c1e6 | 2003-07-28 19:09:06 +0000 | [diff] [blame] | 633 | // getPointerToNamedFunction - This function is used as a global wrapper to |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 634 | // JIT::getPointerToNamedFunction for the purpose of resolving symbols when |
Misha Brukman | d69c1e6 | 2003-07-28 19:09:06 +0000 | [diff] [blame] | 635 | // bugpoint is debugging the JIT. In that scenario, we are loading an .so and |
| 636 | // need to resolve function(s) that are being mis-codegenerated, so we need to |
| 637 | // resolve their addresses at runtime, and this is the way to do it. |
| 638 | extern "C" { |
| 639 | void *getPointerToNamedFunction(const char *Name) { |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 640 | Module &M = TheJIT->getModule(); |
Misha Brukman | d69c1e6 | 2003-07-28 19:09:06 +0000 | [diff] [blame] | 641 | if (Function *F = M.getNamedFunction(Name)) |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 642 | return TheJIT->getPointerToFunction(F); |
| 643 | return TheJIT->getPointerToNamedFunction(Name); |
Misha Brukman | d69c1e6 | 2003-07-28 19:09:06 +0000 | [diff] [blame] | 644 | } |
| 645 | } |