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