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