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