Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 1 | //===-- JITEmitter.cpp - Write machine code to executable memory ----------===// |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 10 | // This file defines a MachineCodeEmitter object that is used by the JIT to |
| 11 | // write machine code to memory and remember where relocatable values are. |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chris Lattner | 3785fad | 2003-08-05 17:00:32 +0000 | [diff] [blame] | 15 | #define DEBUG_TYPE "jit" |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 16 | #include "JIT.h" |
Chris Lattner | 2c0a6a1 | 2003-11-30 04:23:21 +0000 | [diff] [blame] | 17 | #include "llvm/Constant.h" |
| 18 | #include "llvm/Module.h" |
Chris Lattner | 5b3a455 | 2005-03-17 15:38:16 +0000 | [diff] [blame] | 19 | #include "llvm/Type.h" |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineCodeEmitter.h" |
| 21 | #include "llvm/CodeGen/MachineFunction.h" |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineConstantPool.h" |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineRelocation.h" |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetJITInfo.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Debug.h" |
| 27 | #include "llvm/ADT/Statistic.h" |
Reid Spencer | 52b0ba6 | 2004-09-11 04:31:03 +0000 | [diff] [blame] | 28 | #include "llvm/System/Memory.h" |
Chris Lattner | c19aade | 2003-12-08 08:06:28 +0000 | [diff] [blame] | 29 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 30 | |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 31 | namespace { |
Chris Lattner | e738656 | 2003-10-20 05:45:49 +0000 | [diff] [blame] | 32 | Statistic<> NumBytes("jit", "Number of bytes of machine code compiled"); |
Chris Lattner | e884dc2 | 2005-07-20 16:29:20 +0000 | [diff] [blame^] | 33 | Statistic<> NumRelos("jit", "Number of relocations applied"); |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 34 | JIT *TheJIT = 0; |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 35 | } |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 36 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 37 | |
| 38 | //===----------------------------------------------------------------------===// |
| 39 | // JITMemoryManager code. |
| 40 | // |
| 41 | namespace { |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 42 | /// JITMemoryManager - Manage memory for the JIT code generation in a logical, |
| 43 | /// sane way. This splits a large block of MAP_NORESERVE'd memory into two |
| 44 | /// sections, one for function stubs, one for the functions themselves. We |
| 45 | /// have to do this because we may need to emit a function stub while in the |
| 46 | /// middle of emitting a function, and we don't know how large the function we |
| 47 | /// are emitting is. This never bothers to release the memory, because when |
| 48 | /// we are ready to destroy the JIT, the program exits. |
| 49 | class JITMemoryManager { |
Reid Spencer | 33189e7 | 2004-09-13 22:38:11 +0000 | [diff] [blame] | 50 | sys::MemoryBlock MemBlock; // Virtual memory block allocated RWX |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 51 | unsigned char *MemBase; // Base of block of memory, start of stub mem |
| 52 | unsigned char *FunctionBase; // Start of the function body area |
Chris Lattner | 281a601 | 2005-01-10 18:23:22 +0000 | [diff] [blame] | 53 | unsigned char *ConstantPool; // Memory allocated for constant pools |
| 54 | unsigned char *CurStubPtr, *CurFunctionPtr, *CurConstantPtr; |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 55 | public: |
| 56 | JITMemoryManager(); |
Reid Spencer | 4af3da6 | 2004-12-13 16:04:04 +0000 | [diff] [blame] | 57 | ~JITMemoryManager(); |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 58 | |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 59 | inline unsigned char *allocateStub(unsigned StubSize); |
Chris Lattner | 281a601 | 2005-01-10 18:23:22 +0000 | [diff] [blame] | 60 | inline unsigned char *allocateConstant(unsigned ConstantSize, |
| 61 | unsigned Alignment); |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 62 | inline unsigned char *startFunctionBody(); |
Chris Lattner | 281a601 | 2005-01-10 18:23:22 +0000 | [diff] [blame] | 63 | inline void endFunctionBody(unsigned char *FunctionEnd); |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 64 | }; |
| 65 | } |
| 66 | |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 67 | JITMemoryManager::JITMemoryManager() { |
| 68 | // Allocate a 16M block of memory... |
Reid Spencer | 33189e7 | 2004-09-13 22:38:11 +0000 | [diff] [blame] | 69 | MemBlock = sys::Memory::AllocateRWX((16 << 20)); |
Reid Spencer | 52b0ba6 | 2004-09-11 04:31:03 +0000 | [diff] [blame] | 70 | MemBase = reinterpret_cast<unsigned char*>(MemBlock.base()); |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 71 | FunctionBase = MemBase + 512*1024; // Use 512k for stubs |
| 72 | |
| 73 | // Allocate stubs backwards from the function base, allocate functions forward |
| 74 | // from the function base. |
| 75 | CurStubPtr = CurFunctionPtr = FunctionBase; |
Chris Lattner | 281a601 | 2005-01-10 18:23:22 +0000 | [diff] [blame] | 76 | |
| 77 | ConstantPool = new unsigned char [512*1024]; // Use 512k for constant pools |
| 78 | CurConstantPtr = ConstantPool + 512*1024; |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Reid Spencer | 4af3da6 | 2004-12-13 16:04:04 +0000 | [diff] [blame] | 81 | JITMemoryManager::~JITMemoryManager() { |
| 82 | sys::Memory::ReleaseRWX(MemBlock); |
Chris Lattner | 281a601 | 2005-01-10 18:23:22 +0000 | [diff] [blame] | 83 | delete[] ConstantPool; |
Reid Spencer | 4af3da6 | 2004-12-13 16:04:04 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 86 | unsigned char *JITMemoryManager::allocateStub(unsigned StubSize) { |
| 87 | CurStubPtr -= StubSize; |
| 88 | if (CurStubPtr < MemBase) { |
| 89 | std::cerr << "JIT ran out of memory for function stubs!\n"; |
| 90 | abort(); |
| 91 | } |
| 92 | return CurStubPtr; |
| 93 | } |
| 94 | |
Chris Lattner | 281a601 | 2005-01-10 18:23:22 +0000 | [diff] [blame] | 95 | unsigned char *JITMemoryManager::allocateConstant(unsigned ConstantSize, |
| 96 | unsigned Alignment) { |
| 97 | // Reserve space and align pointer. |
| 98 | CurConstantPtr -= ConstantSize; |
| 99 | CurConstantPtr = |
| 100 | (unsigned char *)((intptr_t)CurConstantPtr & ~((intptr_t)Alignment - 1)); |
| 101 | |
| 102 | if (CurConstantPtr < ConstantPool) { |
| 103 | std::cerr << "JIT ran out of memory for constant pools!\n"; |
| 104 | abort(); |
| 105 | } |
| 106 | return CurConstantPtr; |
| 107 | } |
| 108 | |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 120 | //===----------------------------------------------------------------------===// |
| 121 | // JIT lazy compilation code. |
| 122 | // |
| 123 | namespace { |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 124 | class JITResolverState { |
| 125 | private: |
| 126 | /// FunctionToStubMap - Keep track of the stub created for a particular |
| 127 | /// function so that we can reuse them if necessary. |
| 128 | std::map<Function*, void*> FunctionToStubMap; |
| 129 | |
| 130 | /// StubToFunctionMap - Keep track of the function that each stub |
| 131 | /// corresponds to. |
| 132 | std::map<void*, Function*> StubToFunctionMap; |
| 133 | |
| 134 | public: |
| 135 | std::map<Function*, void*>& getFunctionToStubMap(const MutexGuard& locked) { |
| 136 | assert(locked.holds(TheJIT->lock)); |
| 137 | return FunctionToStubMap; |
| 138 | } |
| 139 | |
| 140 | std::map<void*, Function*>& getStubToFunctionMap(const MutexGuard& locked) { |
| 141 | assert(locked.holds(TheJIT->lock)); |
| 142 | return StubToFunctionMap; |
| 143 | } |
| 144 | }; |
| 145 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 146 | /// JITResolver - Keep track of, and resolve, call sites for functions that |
| 147 | /// have not yet been compiled. |
| 148 | class JITResolver { |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 149 | /// MCE - The MachineCodeEmitter to use to emit stubs with. |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 150 | MachineCodeEmitter &MCE; |
| 151 | |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 152 | /// LazyResolverFn - The target lazy resolver function that we actually |
| 153 | /// rewrite instructions to use. |
| 154 | TargetJITInfo::LazyResolverFn LazyResolverFn; |
| 155 | |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 156 | JITResolverState state; |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 157 | |
Chris Lattner | d91ff7c | 2005-04-18 01:44:27 +0000 | [diff] [blame] | 158 | /// ExternalFnToStubMap - This is the equivalent of FunctionToStubMap for |
| 159 | /// external functions. |
| 160 | std::map<void*, void*> ExternalFnToStubMap; |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 161 | public: |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 162 | JITResolver(MachineCodeEmitter &mce) : MCE(mce) { |
| 163 | LazyResolverFn = |
| 164 | TheJIT->getJITInfo().getLazyResolverFunction(JITCompilerFn); |
| 165 | } |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 166 | |
| 167 | /// getFunctionStub - This returns a pointer to a function stub, creating |
| 168 | /// one on demand as needed. |
| 169 | void *getFunctionStub(Function *F); |
| 170 | |
Chris Lattner | d91ff7c | 2005-04-18 01:44:27 +0000 | [diff] [blame] | 171 | /// getExternalFunctionStub - Return a stub for the function at the |
| 172 | /// specified address, created lazily on demand. |
| 173 | void *getExternalFunctionStub(void *FnAddr); |
| 174 | |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 175 | /// AddCallbackAtLocation - If the target is capable of rewriting an |
| 176 | /// instruction without the use of a stub, record the location of the use so |
| 177 | /// we know which function is being used at the location. |
| 178 | void *AddCallbackAtLocation(Function *F, void *Location) { |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 179 | MutexGuard locked(TheJIT->lock); |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 180 | /// Get the target-specific JIT resolver function. |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 181 | state.getStubToFunctionMap(locked)[Location] = F; |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 182 | return (void*)LazyResolverFn; |
| 183 | } |
| 184 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 185 | /// JITCompilerFn - This function is called to resolve a stub to a compiled |
| 186 | /// address. If the LLVM Function corresponding to the stub has not yet |
| 187 | /// been compiled, this function compiles it first. |
| 188 | static void *JITCompilerFn(void *Stub); |
| 189 | }; |
| 190 | } |
| 191 | |
| 192 | /// getJITResolver - This function returns the one instance of the JIT resolver. |
| 193 | /// |
| 194 | static JITResolver &getJITResolver(MachineCodeEmitter *MCE = 0) { |
| 195 | static JITResolver TheJITResolver(*MCE); |
| 196 | return TheJITResolver; |
| 197 | } |
| 198 | |
| 199 | /// getFunctionStub - This returns a pointer to a function stub, creating |
| 200 | /// one on demand as needed. |
| 201 | void *JITResolver::getFunctionStub(Function *F) { |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 202 | MutexGuard locked(TheJIT->lock); |
| 203 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 204 | // If we already have a stub for this function, recycle it. |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 205 | void *&Stub = state.getFunctionToStubMap(locked)[F]; |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 206 | if (Stub) return Stub; |
| 207 | |
Chris Lattner | b43dbdc | 2004-11-22 07:24:43 +0000 | [diff] [blame] | 208 | // Call the lazy resolver function unless we already KNOW it is an external |
| 209 | // function, in which case we just skip the lazy resolution step. |
| 210 | void *Actual = (void*)LazyResolverFn; |
Chris Lattner | 6943570 | 2005-02-20 18:43:35 +0000 | [diff] [blame] | 211 | if (F->isExternal() && F->hasExternalLinkage()) |
Chris Lattner | b43dbdc | 2004-11-22 07:24:43 +0000 | [diff] [blame] | 212 | Actual = TheJIT->getPointerToFunction(F); |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 213 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 214 | // Otherwise, codegen a new stub. For now, the stub will call the lazy |
| 215 | // resolver function. |
Chris Lattner | b43dbdc | 2004-11-22 07:24:43 +0000 | [diff] [blame] | 216 | Stub = TheJIT->getJITInfo().emitFunctionStub(Actual, MCE); |
| 217 | |
Chris Lattner | 6943570 | 2005-02-20 18:43:35 +0000 | [diff] [blame] | 218 | if (Actual != (void*)LazyResolverFn) { |
Chris Lattner | b43dbdc | 2004-11-22 07:24:43 +0000 | [diff] [blame] | 219 | // If we are getting the stub for an external function, we really want the |
| 220 | // address of the stub in the GlobalAddressMap for the JIT, not the address |
| 221 | // of the external function. |
| 222 | TheJIT->updateGlobalMapping(F, Stub); |
| 223 | } |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 224 | |
Chris Lattner | cb47941 | 2004-11-21 03:44:32 +0000 | [diff] [blame] | 225 | DEBUG(std::cerr << "JIT: Stub emitted at [" << Stub << "] for function '" |
Chris Lattner | 6f71720 | 2004-11-22 21:48:33 +0000 | [diff] [blame] | 226 | << F->getName() << "'\n"); |
Chris Lattner | cb47941 | 2004-11-21 03:44:32 +0000 | [diff] [blame] | 227 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 228 | // Finally, keep track of the stub-to-Function mapping so that the |
| 229 | // JITCompilerFn knows which function to compile! |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 230 | state.getStubToFunctionMap(locked)[Stub] = F; |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 231 | return Stub; |
| 232 | } |
| 233 | |
Chris Lattner | d91ff7c | 2005-04-18 01:44:27 +0000 | [diff] [blame] | 234 | /// getExternalFunctionStub - Return a stub for the function at the |
| 235 | /// specified address, created lazily on demand. |
| 236 | void *JITResolver::getExternalFunctionStub(void *FnAddr) { |
| 237 | // If we already have a stub for this function, recycle it. |
| 238 | void *&Stub = ExternalFnToStubMap[FnAddr]; |
| 239 | if (Stub) return Stub; |
| 240 | |
| 241 | Stub = TheJIT->getJITInfo().emitFunctionStub(FnAddr, MCE); |
| 242 | DEBUG(std::cerr << "JIT: Stub emitted at [" << Stub |
| 243 | << "] for external function at '" << FnAddr << "'\n"); |
| 244 | return Stub; |
| 245 | } |
| 246 | |
| 247 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 248 | /// JITCompilerFn - This function is called when a lazy compilation stub has |
| 249 | /// been entered. It looks up which function this stub corresponds to, compiles |
| 250 | /// it if necessary, then returns the resultant function pointer. |
| 251 | void *JITResolver::JITCompilerFn(void *Stub) { |
| 252 | JITResolver &JR = getJITResolver(); |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 253 | |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 254 | MutexGuard locked(TheJIT->lock); |
| 255 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 256 | // The address given to us for the stub may not be exactly right, it might be |
| 257 | // a little bit after the stub. As such, use upper_bound to find it. |
| 258 | std::map<void*, Function*>::iterator I = |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 259 | JR.state.getStubToFunctionMap(locked).upper_bound(Stub); |
| 260 | assert(I != JR.state.getStubToFunctionMap(locked).begin() && "This is not a known stub!"); |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 261 | Function *F = (--I)->second; |
| 262 | |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 263 | // We might like to remove the stub from the StubToFunction map. |
| 264 | // We can't do that! Multiple threads could be stuck, waiting to acquire the |
| 265 | // lock above. As soon as the 1st function finishes compiling the function, |
| 266 | // the next one will be released, and needs to be able to find the function it needs |
| 267 | // to call. |
| 268 | //JR.state.getStubToFunctionMap(locked).erase(I); |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 269 | |
Chris Lattner | cb47941 | 2004-11-21 03:44:32 +0000 | [diff] [blame] | 270 | DEBUG(std::cerr << "JIT: Lazily resolving function '" << F->getName() |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 271 | << "' In stub ptr = " << Stub << " actual ptr = " |
| 272 | << I->first << "\n"); |
| 273 | |
| 274 | void *Result = TheJIT->getPointerToFunction(F); |
| 275 | |
| 276 | // 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] | 277 | JR.state.getFunctionToStubMap(locked).erase(F); |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 278 | |
| 279 | // FIXME: We could rewrite all references to this stub if we knew them. |
| 280 | return Result; |
| 281 | } |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 282 | |
| 283 | |
Chris Lattner | e518b71 | 2004-12-05 07:19:16 +0000 | [diff] [blame] | 284 | // getPointerToFunctionOrStub - If the specified function has been |
| 285 | // code-gen'd, return a pointer to the function. If not, compile it, or use |
| 286 | // a stub to implement lazy compilation if available. |
| 287 | // |
| 288 | void *JIT::getPointerToFunctionOrStub(Function *F) { |
| 289 | // If we have already code generated the function, just return the address. |
| 290 | if (void *Addr = getPointerToGlobalIfAvailable(F)) |
| 291 | return Addr; |
| 292 | |
| 293 | // Get a stub if the target supports it |
| 294 | return getJITResolver(MCE).getFunctionStub(F); |
| 295 | } |
| 296 | |
| 297 | |
| 298 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 299 | //===----------------------------------------------------------------------===// |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 300 | // JITEmitter code. |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 301 | // |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 302 | namespace { |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 303 | /// JITEmitter - The JIT implementation of the MachineCodeEmitter, which is |
| 304 | /// used to output functions to memory for execution. |
| 305 | class JITEmitter : public MachineCodeEmitter { |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 306 | JITMemoryManager MemMgr; |
| 307 | |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 308 | // CurBlock - The start of the current block of memory. CurByte - The |
| 309 | // current byte being emitted to. |
Chris Lattner | 6125fdd | 2003-05-09 03:30:07 +0000 | [diff] [blame] | 310 | unsigned char *CurBlock, *CurByte; |
| 311 | |
| 312 | // When outputting a function stub in the context of some other function, we |
| 313 | // save CurBlock and CurByte here. |
| 314 | unsigned char *SavedCurBlock, *SavedCurByte; |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 315 | |
| 316 | // ConstantPoolAddresses - Contains the location for each entry in the |
| 317 | // constant pool. |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 318 | std::vector<void*> ConstantPoolAddresses; |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 319 | |
| 320 | /// Relocations - These are the relocations that the function needs, as |
| 321 | /// emitted. |
| 322 | std::vector<MachineRelocation> Relocations; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 323 | public: |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 324 | JITEmitter(JIT &jit) { TheJIT = &jit; } |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 325 | |
| 326 | virtual void startFunction(MachineFunction &F); |
| 327 | virtual void finishFunction(MachineFunction &F); |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 328 | virtual void emitConstantPool(MachineConstantPool *MCP); |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 329 | virtual void startFunctionStub(unsigned StubSize); |
| 330 | virtual void* finishFunctionStub(const Function *F); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 331 | virtual void emitByte(unsigned char B); |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 332 | virtual void emitWord(unsigned W); |
Brian Gaeke | aea1b58 | 2004-04-23 17:11:14 +0000 | [diff] [blame] | 333 | virtual void emitWordAt(unsigned W, unsigned *Ptr); |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 334 | |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 335 | virtual void addRelocation(const MachineRelocation &MR) { |
| 336 | Relocations.push_back(MR); |
| 337 | } |
| 338 | |
| 339 | virtual uint64_t getCurrentPCValue(); |
| 340 | virtual uint64_t getCurrentPCOffset(); |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 341 | virtual uint64_t getConstantPoolEntryAddress(unsigned Entry); |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 342 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 343 | private: |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 344 | void *getPointerToGlobal(GlobalValue *GV, void *Reference, bool NoNeedStub); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 345 | }; |
| 346 | } |
| 347 | |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 348 | MachineCodeEmitter *JIT::createEmitter(JIT &jit) { |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 349 | return new JITEmitter(jit); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 352 | void *JITEmitter::getPointerToGlobal(GlobalValue *V, void *Reference, |
| 353 | bool DoesntNeedStub) { |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 354 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) { |
| 355 | /// FIXME: If we straightened things out, this could actually emit the |
| 356 | /// global immediately instead of queuing it for codegen later! |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 357 | return TheJIT->getOrEmitGlobalVariable(GV); |
| 358 | } |
| 359 | |
| 360 | // If we have already compiled the function, return a pointer to its body. |
| 361 | Function *F = cast<Function>(V); |
| 362 | void *ResultPtr = TheJIT->getPointerToGlobalIfAvailable(F); |
| 363 | if (ResultPtr) return ResultPtr; |
| 364 | |
Chris Lattner | 532343b | 2004-11-30 17:41:49 +0000 | [diff] [blame] | 365 | if (F->hasExternalLinkage() && F->isExternal()) { |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 366 | // If this is an external function pointer, we can force the JIT to |
| 367 | // 'compile' it, which really just adds it to the map. |
Chris Lattner | b43dbdc | 2004-11-22 07:24:43 +0000 | [diff] [blame] | 368 | if (DoesntNeedStub) |
| 369 | return TheJIT->getPointerToFunction(F); |
| 370 | |
| 371 | return getJITResolver(this).getFunctionStub(F); |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 374 | // Okay, the function has not been compiled yet, if the target callback |
| 375 | // mechanism is capable of rewriting the instruction directly, prefer to do |
| 376 | // that instead of emitting a stub. |
| 377 | if (DoesntNeedStub) |
| 378 | return getJITResolver(this).AddCallbackAtLocation(F, Reference); |
| 379 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 380 | // Otherwise, we have to emit a lazy resolving stub. |
| 381 | return getJITResolver(this).getFunctionStub(F); |
| 382 | } |
| 383 | |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 384 | void JITEmitter::startFunction(MachineFunction &F) { |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 385 | CurByte = CurBlock = MemMgr.startFunctionBody(); |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 386 | TheJIT->addGlobalMapping(F.getFunction(), CurBlock); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 389 | void JITEmitter::finishFunction(MachineFunction &F) { |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 390 | MemMgr.endFunctionBody(CurByte); |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 391 | ConstantPoolAddresses.clear(); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 392 | NumBytes += CurByte-CurBlock; |
| 393 | |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 394 | if (!Relocations.empty()) { |
Chris Lattner | e884dc2 | 2005-07-20 16:29:20 +0000 | [diff] [blame^] | 395 | NumRelos += Relocations.size(); |
| 396 | |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 397 | // Resolve the relocations to concrete pointers. |
| 398 | for (unsigned i = 0, e = Relocations.size(); i != e; ++i) { |
| 399 | MachineRelocation &MR = Relocations[i]; |
| 400 | void *ResultPtr; |
Chris Lattner | d91ff7c | 2005-04-18 01:44:27 +0000 | [diff] [blame] | 401 | if (MR.isString()) { |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 402 | ResultPtr = TheJIT->getPointerToNamedFunction(MR.getString()); |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 403 | |
Chris Lattner | d91ff7c | 2005-04-18 01:44:27 +0000 | [diff] [blame] | 404 | // If the target REALLY wants a stub for this function, emit it now. |
| 405 | if (!MR.doesntNeedFunctionStub()) |
| 406 | ResultPtr = getJITResolver(this).getExternalFunctionStub(ResultPtr); |
| 407 | } else |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 408 | ResultPtr = getPointerToGlobal(MR.getGlobalValue(), |
| 409 | CurBlock+MR.getMachineCodeOffset(), |
| 410 | MR.doesntNeedFunctionStub()); |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 411 | MR.setResultPointer(ResultPtr); |
| 412 | } |
| 413 | |
| 414 | TheJIT->getJITInfo().relocate(CurBlock, &Relocations[0], |
| 415 | Relocations.size()); |
| 416 | } |
| 417 | |
Chris Lattner | cb47941 | 2004-11-21 03:44:32 +0000 | [diff] [blame] | 418 | DEBUG(std::cerr << "JIT: Finished CodeGen of [" << (void*)CurBlock |
Misha Brukman | 1d44085 | 2003-06-06 06:52:35 +0000 | [diff] [blame] | 419 | << "] Function: " << F.getFunction()->getName() |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 420 | << ": " << CurByte-CurBlock << " bytes of text, " |
| 421 | << Relocations.size() << " relocations\n"); |
| 422 | Relocations.clear(); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 425 | void JITEmitter::emitConstantPool(MachineConstantPool *MCP) { |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 426 | const std::vector<Constant*> &Constants = MCP->getConstants(); |
Chris Lattner | 2c0a6a1 | 2003-11-30 04:23:21 +0000 | [diff] [blame] | 427 | if (Constants.empty()) return; |
| 428 | |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 429 | for (unsigned i = 0, e = Constants.size(); i != e; ++i) { |
Chris Lattner | 2c0a6a1 | 2003-11-30 04:23:21 +0000 | [diff] [blame] | 430 | const Type *Ty = Constants[i]->getType(); |
Chris Lattner | a8101c1 | 2005-01-08 20:07:03 +0000 | [diff] [blame] | 431 | unsigned Size = (unsigned)TheJIT->getTargetData().getTypeSize(Ty); |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 432 | unsigned Alignment = TheJIT->getTargetData().getTypeAlignment(Ty); |
Chris Lattner | 2c0a6a1 | 2003-11-30 04:23:21 +0000 | [diff] [blame] | 433 | |
Chris Lattner | 281a601 | 2005-01-10 18:23:22 +0000 | [diff] [blame] | 434 | void *Addr = MemMgr.allocateConstant(Size, Alignment); |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 435 | TheJIT->InitializeMemory(Constants[i], Addr); |
Misha Brukman | 91de352 | 2003-11-30 00:50:53 +0000 | [diff] [blame] | 436 | ConstantPoolAddresses.push_back(Addr); |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 440 | void JITEmitter::startFunctionStub(unsigned StubSize) { |
Chris Lattner | 6125fdd | 2003-05-09 03:30:07 +0000 | [diff] [blame] | 441 | SavedCurBlock = CurBlock; SavedCurByte = CurByte; |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 442 | CurByte = CurBlock = MemMgr.allocateStub(StubSize); |
Chris Lattner | 6125fdd | 2003-05-09 03:30:07 +0000 | [diff] [blame] | 443 | } |
| 444 | |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 445 | void *JITEmitter::finishFunctionStub(const Function *F) { |
Chris Lattner | 6125fdd | 2003-05-09 03:30:07 +0000 | [diff] [blame] | 446 | NumBytes += CurByte-CurBlock; |
Chris Lattner | 6125fdd | 2003-05-09 03:30:07 +0000 | [diff] [blame] | 447 | std::swap(CurBlock, SavedCurBlock); |
| 448 | CurByte = SavedCurByte; |
| 449 | return SavedCurBlock; |
| 450 | } |
| 451 | |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 452 | void JITEmitter::emitByte(unsigned char B) { |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 453 | *CurByte++ = B; // Write the byte to memory |
| 454 | } |
| 455 | |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 456 | void JITEmitter::emitWord(unsigned W) { |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 457 | // This won't work if the endianness of the host and target don't agree! (For |
| 458 | // a JIT this can't happen though. :) |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 459 | *(unsigned*)CurByte = W; |
| 460 | CurByte += sizeof(unsigned); |
| 461 | } |
| 462 | |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 463 | void JITEmitter::emitWordAt(unsigned W, unsigned *Ptr) { |
Brian Gaeke | aea1b58 | 2004-04-23 17:11:14 +0000 | [diff] [blame] | 464 | *Ptr = W; |
| 465 | } |
| 466 | |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 467 | // getConstantPoolEntryAddress - Return the address of the 'ConstantNum' entry |
| 468 | // in the constant pool that was last emitted with the 'emitConstantPool' |
| 469 | // method. |
| 470 | // |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 471 | uint64_t JITEmitter::getConstantPoolEntryAddress(unsigned ConstantNum) { |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 472 | assert(ConstantNum < ConstantPoolAddresses.size() && |
Misha Brukman | 3c94497 | 2005-04-22 04:08:30 +0000 | [diff] [blame] | 473 | "Invalid ConstantPoolIndex!"); |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 474 | return (intptr_t)ConstantPoolAddresses[ConstantNum]; |
| 475 | } |
| 476 | |
| 477 | // getCurrentPCValue - This returns the address that the next emitted byte |
| 478 | // will be output to. |
| 479 | // |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 480 | uint64_t JITEmitter::getCurrentPCValue() { |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 481 | return (intptr_t)CurByte; |
| 482 | } |
| 483 | |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 484 | uint64_t JITEmitter::getCurrentPCOffset() { |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 485 | return (intptr_t)CurByte-(intptr_t)CurBlock; |
| 486 | } |
| 487 | |
Misha Brukman | d69c1e6 | 2003-07-28 19:09:06 +0000 | [diff] [blame] | 488 | // getPointerToNamedFunction - This function is used as a global wrapper to |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 489 | // JIT::getPointerToNamedFunction for the purpose of resolving symbols when |
Misha Brukman | d69c1e6 | 2003-07-28 19:09:06 +0000 | [diff] [blame] | 490 | // bugpoint is debugging the JIT. In that scenario, we are loading an .so and |
| 491 | // need to resolve function(s) that are being mis-codegenerated, so we need to |
| 492 | // resolve their addresses at runtime, and this is the way to do it. |
| 493 | extern "C" { |
| 494 | void *getPointerToNamedFunction(const char *Name) { |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 495 | Module &M = TheJIT->getModule(); |
Misha Brukman | d69c1e6 | 2003-07-28 19:09:06 +0000 | [diff] [blame] | 496 | if (Function *F = M.getNamedFunction(Name)) |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 497 | return TheJIT->getPointerToFunction(F); |
| 498 | return TheJIT->getPointerToNamedFunction(Name); |
Misha Brukman | d69c1e6 | 2003-07-28 19:09:06 +0000 | [diff] [blame] | 499 | } |
| 500 | } |