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 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // 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" |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 25 | #include "llvm/ExecutionEngine/JITMemoryManager.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" |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 28 | #include "llvm/Target/TargetMachine.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Debug.h" |
Chris Lattner | e7fd553 | 2006-05-08 22:00:52 +0000 | [diff] [blame] | 30 | #include "llvm/Support/MutexGuard.h" |
Anton Korobeynikov | fd58e6e | 2007-01-23 10:26:08 +0000 | [diff] [blame] | 31 | #include "llvm/System/Disassembler.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/Statistic.h" |
Andrew Lenharth | a00269b | 2005-07-29 23:40:16 +0000 | [diff] [blame] | 33 | #include <algorithm> |
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 | 3634373 | 2006-12-19 22:43:32 +0000 | [diff] [blame] | 36 | STATISTIC(NumBytes, "Number of bytes of machine code compiled"); |
| 37 | STATISTIC(NumRelos, "Number of relocations applied"); |
| 38 | static JIT *TheJIT = 0; |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 39 | |
Andrew Lenharth | a00269b | 2005-07-29 23:40:16 +0000 | [diff] [blame] | 40 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 41 | //===----------------------------------------------------------------------===// |
| 42 | // JIT lazy compilation code. |
| 43 | // |
| 44 | namespace { |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 45 | class JITResolverState { |
| 46 | private: |
| 47 | /// FunctionToStubMap - Keep track of the stub created for a particular |
| 48 | /// function so that we can reuse them if necessary. |
| 49 | std::map<Function*, void*> FunctionToStubMap; |
| 50 | |
| 51 | /// StubToFunctionMap - Keep track of the function that each stub |
| 52 | /// corresponds to. |
| 53 | std::map<void*, Function*> StubToFunctionMap; |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 54 | |
Evan Cheng | be8c03f | 2008-01-04 10:46:51 +0000 | [diff] [blame] | 55 | /// GlobalToLazyPtrMap - Keep track of the lazy pointer created for a |
| 56 | /// particular GlobalVariable so that we can reuse them if necessary. |
| 57 | std::map<GlobalValue*, void*> GlobalToLazyPtrMap; |
| 58 | |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 59 | public: |
| 60 | std::map<Function*, void*>& getFunctionToStubMap(const MutexGuard& locked) { |
| 61 | assert(locked.holds(TheJIT->lock)); |
| 62 | return FunctionToStubMap; |
| 63 | } |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 64 | |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 65 | std::map<void*, Function*>& getStubToFunctionMap(const MutexGuard& locked) { |
| 66 | assert(locked.holds(TheJIT->lock)); |
| 67 | return StubToFunctionMap; |
| 68 | } |
Evan Cheng | be8c03f | 2008-01-04 10:46:51 +0000 | [diff] [blame] | 69 | |
| 70 | std::map<GlobalValue*, void*>& |
| 71 | getGlobalToLazyPtrMap(const MutexGuard& locked) { |
| 72 | assert(locked.holds(TheJIT->lock)); |
| 73 | return GlobalToLazyPtrMap; |
| 74 | } |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 75 | }; |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 76 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 77 | /// JITResolver - Keep track of, and resolve, call sites for functions that |
| 78 | /// have not yet been compiled. |
| 79 | class JITResolver { |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 80 | /// LazyResolverFn - The target lazy resolver function that we actually |
| 81 | /// rewrite instructions to use. |
| 82 | TargetJITInfo::LazyResolverFn LazyResolverFn; |
| 83 | |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 84 | JITResolverState state; |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 85 | |
Chris Lattner | d91ff7c | 2005-04-18 01:44:27 +0000 | [diff] [blame] | 86 | /// ExternalFnToStubMap - This is the equivalent of FunctionToStubMap for |
| 87 | /// external functions. |
| 88 | std::map<void*, void*> ExternalFnToStubMap; |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 89 | |
| 90 | //map addresses to indexes in the GOT |
| 91 | std::map<void*, unsigned> revGOTMap; |
| 92 | unsigned nextGOTIndex; |
| 93 | |
Chris Lattner | e748401 | 2007-02-24 02:57:03 +0000 | [diff] [blame] | 94 | static JITResolver *TheJITResolver; |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 95 | public: |
Chris Lattner | e748401 | 2007-02-24 02:57:03 +0000 | [diff] [blame] | 96 | JITResolver(JIT &jit) : nextGOTIndex(0) { |
| 97 | TheJIT = &jit; |
| 98 | |
| 99 | LazyResolverFn = jit.getJITInfo().getLazyResolverFunction(JITCompilerFn); |
| 100 | assert(TheJITResolver == 0 && "Multiple JIT resolvers?"); |
| 101 | TheJITResolver = this; |
| 102 | } |
| 103 | |
| 104 | ~JITResolver() { |
| 105 | TheJITResolver = 0; |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 106 | } |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 107 | |
| 108 | /// getFunctionStub - This returns a pointer to a function stub, creating |
| 109 | /// one on demand as needed. |
| 110 | void *getFunctionStub(Function *F); |
| 111 | |
Chris Lattner | d91ff7c | 2005-04-18 01:44:27 +0000 | [diff] [blame] | 112 | /// getExternalFunctionStub - Return a stub for the function at the |
| 113 | /// specified address, created lazily on demand. |
| 114 | void *getExternalFunctionStub(void *FnAddr); |
| 115 | |
Evan Cheng | be8c03f | 2008-01-04 10:46:51 +0000 | [diff] [blame] | 116 | /// getGlobalValueLazyPtr - Return a lazy pointer containing the specified |
| 117 | /// GV address. |
| 118 | void *getGlobalValueLazyPtr(GlobalValue *V, void *GVAddress); |
| 119 | |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 120 | /// AddCallbackAtLocation - If the target is capable of rewriting an |
| 121 | /// instruction without the use of a stub, record the location of the use so |
| 122 | /// we know which function is being used at the location. |
| 123 | void *AddCallbackAtLocation(Function *F, void *Location) { |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 124 | MutexGuard locked(TheJIT->lock); |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 125 | /// Get the target-specific JIT resolver function. |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 126 | state.getStubToFunctionMap(locked)[Location] = F; |
Chris Lattner | 870286a | 2006-06-01 17:29:22 +0000 | [diff] [blame] | 127 | return (void*)(intptr_t)LazyResolverFn; |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 130 | /// getGOTIndexForAddress - Return a new or existing index in the GOT for |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 131 | /// an address. This function only manages slots, it does not manage the |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 132 | /// contents of the slots or the memory associated with the GOT. |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 133 | unsigned getGOTIndexForAddr(void *addr); |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 134 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 135 | /// JITCompilerFn - This function is called to resolve a stub to a compiled |
| 136 | /// address. If the LLVM Function corresponding to the stub has not yet |
| 137 | /// been compiled, this function compiles it first. |
| 138 | static void *JITCompilerFn(void *Stub); |
| 139 | }; |
| 140 | } |
| 141 | |
Chris Lattner | e748401 | 2007-02-24 02:57:03 +0000 | [diff] [blame] | 142 | JITResolver *JITResolver::TheJITResolver = 0; |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 143 | |
Evan Cheng | 55b5053 | 2006-07-27 06:33:55 +0000 | [diff] [blame] | 144 | #if (defined(__POWERPC__) || defined (__ppc__) || defined(_POWER)) && \ |
| 145 | defined(__APPLE__) |
| 146 | extern "C" void sys_icache_invalidate(const void *Addr, size_t len); |
| 147 | #endif |
| 148 | |
| 149 | /// synchronizeICache - On some targets, the JIT emitted code must be |
| 150 | /// explicitly refetched to ensure correct execution. |
| 151 | static void synchronizeICache(const void *Addr, size_t len) { |
| 152 | #if (defined(__POWERPC__) || defined (__ppc__) || defined(_POWER)) && \ |
| 153 | defined(__APPLE__) |
Jim Laskey | 2e9f368 | 2006-07-27 13:40:34 +0000 | [diff] [blame] | 154 | sys_icache_invalidate(Addr, len); |
Evan Cheng | 55b5053 | 2006-07-27 06:33:55 +0000 | [diff] [blame] | 155 | #endif |
| 156 | } |
| 157 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 158 | /// getFunctionStub - This returns a pointer to a function stub, creating |
| 159 | /// one on demand as needed. |
| 160 | void *JITResolver::getFunctionStub(Function *F) { |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 161 | MutexGuard locked(TheJIT->lock); |
| 162 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 163 | // If we already have a stub for this function, recycle it. |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 164 | void *&Stub = state.getFunctionToStubMap(locked)[F]; |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 165 | if (Stub) return Stub; |
| 166 | |
Chris Lattner | b43dbdc | 2004-11-22 07:24:43 +0000 | [diff] [blame] | 167 | // Call the lazy resolver function unless we already KNOW it is an external |
| 168 | // function, in which case we just skip the lazy resolution step. |
Chris Lattner | 870286a | 2006-06-01 17:29:22 +0000 | [diff] [blame] | 169 | void *Actual = (void*)(intptr_t)LazyResolverFn; |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 170 | if (F->isDeclaration() && !F->hasNotBeenReadFromBitcode()) |
Chris Lattner | b43dbdc | 2004-11-22 07:24:43 +0000 | [diff] [blame] | 171 | Actual = TheJIT->getPointerToFunction(F); |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 172 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 173 | // Otherwise, codegen a new stub. For now, the stub will call the lazy |
| 174 | // resolver function. |
Chris Lattner | e748401 | 2007-02-24 02:57:03 +0000 | [diff] [blame] | 175 | Stub = TheJIT->getJITInfo().emitFunctionStub(Actual, |
| 176 | *TheJIT->getCodeEmitter()); |
Chris Lattner | b43dbdc | 2004-11-22 07:24:43 +0000 | [diff] [blame] | 177 | |
Chris Lattner | 870286a | 2006-06-01 17:29:22 +0000 | [diff] [blame] | 178 | if (Actual != (void*)(intptr_t)LazyResolverFn) { |
Chris Lattner | b43dbdc | 2004-11-22 07:24:43 +0000 | [diff] [blame] | 179 | // If we are getting the stub for an external function, we really want the |
| 180 | // address of the stub in the GlobalAddressMap for the JIT, not the address |
| 181 | // of the external function. |
| 182 | TheJIT->updateGlobalMapping(F, Stub); |
| 183 | } |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 184 | |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 185 | DOUT << "JIT: Stub emitted at [" << Stub << "] for function '" |
| 186 | << F->getName() << "'\n"; |
Chris Lattner | cb47941 | 2004-11-21 03:44:32 +0000 | [diff] [blame] | 187 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 188 | // Finally, keep track of the stub-to-Function mapping so that the |
| 189 | // JITCompilerFn knows which function to compile! |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 190 | state.getStubToFunctionMap(locked)[Stub] = F; |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 191 | return Stub; |
| 192 | } |
| 193 | |
Evan Cheng | be8c03f | 2008-01-04 10:46:51 +0000 | [diff] [blame] | 194 | /// getGlobalValueLazyPtr - Return a lazy pointer containing the specified |
| 195 | /// GV address. |
| 196 | void *JITResolver::getGlobalValueLazyPtr(GlobalValue *GV, void *GVAddress) { |
| 197 | MutexGuard locked(TheJIT->lock); |
| 198 | |
| 199 | // If we already have a stub for this global variable, recycle it. |
| 200 | void *&LazyPtr = state.getGlobalToLazyPtrMap(locked)[GV]; |
| 201 | if (LazyPtr) return LazyPtr; |
| 202 | |
| 203 | // Otherwise, codegen a new lazy pointer. |
| 204 | LazyPtr = TheJIT->getJITInfo().emitGlobalValueLazyPtr(GVAddress, |
| 205 | *TheJIT->getCodeEmitter()); |
| 206 | |
| 207 | DOUT << "JIT: Stub emitted at [" << LazyPtr << "] for GV '" |
| 208 | << GV->getName() << "'\n"; |
| 209 | |
| 210 | return LazyPtr; |
| 211 | } |
| 212 | |
Chris Lattner | d91ff7c | 2005-04-18 01:44:27 +0000 | [diff] [blame] | 213 | /// getExternalFunctionStub - Return a stub for the function at the |
| 214 | /// specified address, created lazily on demand. |
| 215 | void *JITResolver::getExternalFunctionStub(void *FnAddr) { |
| 216 | // If we already have a stub for this function, recycle it. |
| 217 | void *&Stub = ExternalFnToStubMap[FnAddr]; |
| 218 | if (Stub) return Stub; |
| 219 | |
Chris Lattner | e748401 | 2007-02-24 02:57:03 +0000 | [diff] [blame] | 220 | Stub = TheJIT->getJITInfo().emitFunctionStub(FnAddr, |
| 221 | *TheJIT->getCodeEmitter()); |
Evan Cheng | 55fc280 | 2006-07-25 20:40:54 +0000 | [diff] [blame] | 222 | |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 223 | DOUT << "JIT: Stub emitted at [" << Stub |
| 224 | << "] for external function at '" << FnAddr << "'\n"; |
Chris Lattner | d91ff7c | 2005-04-18 01:44:27 +0000 | [diff] [blame] | 225 | return Stub; |
| 226 | } |
| 227 | |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 228 | unsigned JITResolver::getGOTIndexForAddr(void* addr) { |
| 229 | unsigned idx = revGOTMap[addr]; |
| 230 | if (!idx) { |
| 231 | idx = ++nextGOTIndex; |
| 232 | revGOTMap[addr] = idx; |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 233 | DOUT << "Adding GOT entry " << idx |
| 234 | << " for addr " << addr << "\n"; |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 235 | } |
| 236 | return idx; |
| 237 | } |
Chris Lattner | d91ff7c | 2005-04-18 01:44:27 +0000 | [diff] [blame] | 238 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 239 | /// JITCompilerFn - This function is called when a lazy compilation stub has |
| 240 | /// been entered. It looks up which function this stub corresponds to, compiles |
| 241 | /// it if necessary, then returns the resultant function pointer. |
| 242 | void *JITResolver::JITCompilerFn(void *Stub) { |
Chris Lattner | e748401 | 2007-02-24 02:57:03 +0000 | [diff] [blame] | 243 | JITResolver &JR = *TheJITResolver; |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 244 | |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 245 | MutexGuard locked(TheJIT->lock); |
| 246 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 247 | // The address given to us for the stub may not be exactly right, it might be |
| 248 | // a little bit after the stub. As such, use upper_bound to find it. |
| 249 | std::map<void*, Function*>::iterator I = |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 250 | JR.state.getStubToFunctionMap(locked).upper_bound(Stub); |
Chris Lattner | 2199877 | 2006-01-07 06:20:51 +0000 | [diff] [blame] | 251 | assert(I != JR.state.getStubToFunctionMap(locked).begin() && |
| 252 | "This is not a known stub!"); |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 253 | Function *F = (--I)->second; |
| 254 | |
Evan Cheng | 9da60f9 | 2007-06-30 00:10:37 +0000 | [diff] [blame] | 255 | // If we have already code generated the function, just return the address. |
| 256 | void *Result = TheJIT->getPointerToGlobalIfAvailable(F); |
Chris Lattner | 9cab56d | 2006-11-09 19:32:13 +0000 | [diff] [blame] | 257 | |
Evan Cheng | 9da60f9 | 2007-06-30 00:10:37 +0000 | [diff] [blame] | 258 | if (!Result) { |
| 259 | // Otherwise we don't have it, do lazy compilation now. |
| 260 | |
| 261 | // If lazy compilation is disabled, emit a useful error message and abort. |
| 262 | if (TheJIT->isLazyCompilationDisabled()) { |
| 263 | cerr << "LLVM JIT requested to do lazy compilation of function '" |
| 264 | << F->getName() << "' when lazy compiles are disabled!\n"; |
| 265 | abort(); |
| 266 | } |
| 267 | |
| 268 | // We might like to remove the stub from the StubToFunction map. |
| 269 | // We can't do that! Multiple threads could be stuck, waiting to acquire the |
| 270 | // lock above. As soon as the 1st function finishes compiling the function, |
| 271 | // the next one will be released, and needs to be able to find the function |
| 272 | // it needs to call. |
| 273 | //JR.state.getStubToFunctionMap(locked).erase(I); |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 274 | |
Evan Cheng | 9da60f9 | 2007-06-30 00:10:37 +0000 | [diff] [blame] | 275 | DOUT << "JIT: Lazily resolving function '" << F->getName() |
| 276 | << "' In stub ptr = " << Stub << " actual ptr = " |
| 277 | << I->first << "\n"; |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 278 | |
Evan Cheng | 9da60f9 | 2007-06-30 00:10:37 +0000 | [diff] [blame] | 279 | Result = TheJIT->getPointerToFunction(F); |
| 280 | } |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 281 | |
| 282 | // 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] | 283 | JR.state.getFunctionToStubMap(locked).erase(F); |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 284 | |
| 285 | // 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] | 286 | |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 287 | // What we will do is set the compiled function address to map to the |
| 288 | // 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] | 289 | // if they see it still using the stub address. |
| 290 | // Note: this is done so the Resolver doesn't have to manage GOT memory |
| 291 | // Do this without allocating map space if the target isn't using a GOT |
| 292 | if(JR.revGOTMap.find(Stub) != JR.revGOTMap.end()) |
| 293 | JR.revGOTMap[Result] = JR.revGOTMap[Stub]; |
| 294 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 295 | return Result; |
| 296 | } |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 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 | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 306 | JITMemoryManager *MemMgr; |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 307 | |
Chris Lattner | 6125fdd | 2003-05-09 03:30:07 +0000 | [diff] [blame] | 308 | // 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] | 309 | // save BufferBegin/BufferEnd/CurBufferPtr here. |
| 310 | unsigned char *SavedBufferBegin, *SavedBufferEnd, *SavedCurBufferPtr; |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 311 | |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 312 | /// Relocations - These are the relocations that the function needs, as |
| 313 | /// emitted. |
| 314 | std::vector<MachineRelocation> Relocations; |
Chris Lattner | b4432f3 | 2006-05-03 17:10:41 +0000 | [diff] [blame] | 315 | |
| 316 | /// MBBLocations - This vector is a mapping from MBB ID's to their address. |
| 317 | /// It is filled in by the StartMachineBasicBlock callback and queried by |
| 318 | /// the getMachineBasicBlockAddress callback. |
| 319 | std::vector<intptr_t> MBBLocations; |
Andrew Lenharth | 16ec33c | 2005-07-22 20:48:12 +0000 | [diff] [blame] | 320 | |
Chris Lattner | 239862c | 2006-02-09 04:49:59 +0000 | [diff] [blame] | 321 | /// ConstantPool - The constant pool for the current function. |
| 322 | /// |
| 323 | MachineConstantPool *ConstantPool; |
| 324 | |
| 325 | /// ConstantPoolBase - A pointer to the first entry in the constant pool. |
| 326 | /// |
| 327 | void *ConstantPoolBase; |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 328 | |
Nate Begeman | 019f851 | 2006-09-10 23:03:44 +0000 | [diff] [blame] | 329 | /// JumpTable - The jump tables for the current function. |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 330 | /// |
| 331 | MachineJumpTableInfo *JumpTable; |
| 332 | |
| 333 | /// JumpTableBase - A pointer to the first entry in the jump table. |
| 334 | /// |
| 335 | void *JumpTableBase; |
Evan Cheng | 2a3e08b | 2008-01-05 02:26:58 +0000 | [diff] [blame] | 336 | |
Chris Lattner | e748401 | 2007-02-24 02:57:03 +0000 | [diff] [blame] | 337 | /// Resolver - This contains info about the currently resolved functions. |
| 338 | JITResolver Resolver; |
| 339 | public: |
Chris Lattner | 9f2f142 | 2007-12-06 01:08:09 +0000 | [diff] [blame] | 340 | JITEmitter(JIT &jit, JITMemoryManager *JMM) : Resolver(jit) { |
| 341 | MemMgr = JMM ? JMM : JITMemoryManager::CreateDefaultMemManager(); |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 342 | if (jit.getJITInfo().needsGOT()) { |
| 343 | MemMgr->AllocateGOT(); |
| 344 | DOUT << "JIT is managing a GOT\n"; |
| 345 | } |
| 346 | } |
| 347 | ~JITEmitter() { |
| 348 | delete MemMgr; |
Andrew Lenharth | 16ec33c | 2005-07-22 20:48:12 +0000 | [diff] [blame] | 349 | } |
Chris Lattner | e748401 | 2007-02-24 02:57:03 +0000 | [diff] [blame] | 350 | |
| 351 | JITResolver &getJITResolver() { return Resolver; } |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 352 | |
| 353 | virtual void startFunction(MachineFunction &F); |
Chris Lattner | 43b429b | 2006-05-02 18:27:26 +0000 | [diff] [blame] | 354 | virtual bool finishFunction(MachineFunction &F); |
Chris Lattner | f75f9be | 2006-05-02 23:22:24 +0000 | [diff] [blame] | 355 | |
| 356 | void emitConstantPool(MachineConstantPool *MCP); |
| 357 | void initJumpTableInfo(MachineJumpTableInfo *MJTI); |
Jim Laskey | b92767a | 2006-12-14 22:53:42 +0000 | [diff] [blame] | 358 | void emitJumpTableInfo(MachineJumpTableInfo *MJTI); |
Chris Lattner | f75f9be | 2006-05-02 23:22:24 +0000 | [diff] [blame] | 359 | |
Evan Cheng | 9a1e9b9 | 2006-11-16 20:04:54 +0000 | [diff] [blame] | 360 | virtual void startFunctionStub(unsigned StubSize, unsigned Alignment = 1); |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 361 | virtual void* finishFunctionStub(const Function *F); |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 362 | |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 363 | virtual void addRelocation(const MachineRelocation &MR) { |
| 364 | Relocations.push_back(MR); |
| 365 | } |
Chris Lattner | b4432f3 | 2006-05-03 17:10:41 +0000 | [diff] [blame] | 366 | |
| 367 | virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) { |
| 368 | if (MBBLocations.size() <= (unsigned)MBB->getNumber()) |
| 369 | MBBLocations.resize((MBB->getNumber()+1)*2); |
| 370 | MBBLocations[MBB->getNumber()] = getCurrentPCValue(); |
| 371 | } |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 372 | |
Chris Lattner | b4432f3 | 2006-05-03 17:10:41 +0000 | [diff] [blame] | 373 | virtual intptr_t getConstantPoolEntryAddress(unsigned Entry) const; |
| 374 | virtual intptr_t getJumpTableEntryAddress(unsigned Entry) const; |
Evan Cheng | 2a3e08b | 2008-01-05 02:26:58 +0000 | [diff] [blame] | 375 | |
Chris Lattner | b4432f3 | 2006-05-03 17:10:41 +0000 | [diff] [blame] | 376 | virtual intptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const { |
| 377 | assert(MBBLocations.size() > (unsigned)MBB->getNumber() && |
| 378 | MBBLocations[MBB->getNumber()] && "MBB not emitted!"); |
| 379 | return MBBLocations[MBB->getNumber()]; |
| 380 | } |
| 381 | |
Chris Lattner | e993cc2 | 2006-05-11 23:08:08 +0000 | [diff] [blame] | 382 | /// deallocateMemForFunction - Deallocate all memory for the specified |
| 383 | /// function body. |
| 384 | void deallocateMemForFunction(Function *F) { |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 385 | MemMgr->deallocateMemForFunction(F); |
Chris Lattner | e993cc2 | 2006-05-11 23:08:08 +0000 | [diff] [blame] | 386 | } |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 387 | private: |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 388 | void *getPointerToGlobal(GlobalValue *GV, void *Reference, bool NoNeedStub); |
Evan Cheng | be8c03f | 2008-01-04 10:46:51 +0000 | [diff] [blame] | 389 | void *getPointerToGVLazyPtr(GlobalValue *V, void *Reference, |
| 390 | bool NoNeedStub); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 391 | }; |
| 392 | } |
| 393 | |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 394 | void *JITEmitter::getPointerToGlobal(GlobalValue *V, void *Reference, |
| 395 | bool DoesntNeedStub) { |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 396 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) { |
| 397 | /// FIXME: If we straightened things out, this could actually emit the |
| 398 | /// global immediately instead of queuing it for codegen later! |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 399 | return TheJIT->getOrEmitGlobalVariable(GV); |
| 400 | } |
| 401 | |
| 402 | // If we have already compiled the function, return a pointer to its body. |
| 403 | Function *F = cast<Function>(V); |
| 404 | void *ResultPtr = TheJIT->getPointerToGlobalIfAvailable(F); |
| 405 | if (ResultPtr) return ResultPtr; |
| 406 | |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 407 | if (F->isDeclaration() && !F->hasNotBeenReadFromBitcode()) { |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 408 | // If this is an external function pointer, we can force the JIT to |
| 409 | // 'compile' it, which really just adds it to the map. |
Chris Lattner | b43dbdc | 2004-11-22 07:24:43 +0000 | [diff] [blame] | 410 | if (DoesntNeedStub) |
| 411 | return TheJIT->getPointerToFunction(F); |
| 412 | |
Chris Lattner | e748401 | 2007-02-24 02:57:03 +0000 | [diff] [blame] | 413 | return Resolver.getFunctionStub(F); |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 416 | // Okay, the function has not been compiled yet, if the target callback |
| 417 | // mechanism is capable of rewriting the instruction directly, prefer to do |
| 418 | // that instead of emitting a stub. |
| 419 | if (DoesntNeedStub) |
Chris Lattner | e748401 | 2007-02-24 02:57:03 +0000 | [diff] [blame] | 420 | return Resolver.AddCallbackAtLocation(F, Reference); |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 421 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 422 | // Otherwise, we have to emit a lazy resolving stub. |
Chris Lattner | e748401 | 2007-02-24 02:57:03 +0000 | [diff] [blame] | 423 | return Resolver.getFunctionStub(F); |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 424 | } |
| 425 | |
Evan Cheng | be8c03f | 2008-01-04 10:46:51 +0000 | [diff] [blame] | 426 | void *JITEmitter::getPointerToGVLazyPtr(GlobalValue *V, void *Reference, |
| 427 | bool DoesntNeedStub) { |
| 428 | // Make sure GV is emitted first. |
| 429 | // FIXME: For now, if the GV is an external function we force the JIT to |
| 430 | // compile it so the lazy pointer will contain the fully resolved address. |
| 431 | void *GVAddress = getPointerToGlobal(V, Reference, true); |
| 432 | return Resolver.getGlobalValueLazyPtr(V, GVAddress); |
| 433 | } |
| 434 | |
| 435 | |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 436 | void JITEmitter::startFunction(MachineFunction &F) { |
Chris Lattner | e993cc2 | 2006-05-11 23:08:08 +0000 | [diff] [blame] | 437 | uintptr_t ActualSize; |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 438 | BufferBegin = CurBufferPtr = MemMgr->startFunctionBody(F.getFunction(), |
| 439 | ActualSize); |
Chris Lattner | e993cc2 | 2006-05-11 23:08:08 +0000 | [diff] [blame] | 440 | BufferEnd = BufferBegin+ActualSize; |
Chris Lattner | f75f9be | 2006-05-02 23:22:24 +0000 | [diff] [blame] | 441 | |
Evan Cheng | 9a1e9b9 | 2006-11-16 20:04:54 +0000 | [diff] [blame] | 442 | // Ensure the constant pool/jump table info is at least 4-byte aligned. |
| 443 | emitAlignment(16); |
| 444 | |
Chris Lattner | f75f9be | 2006-05-02 23:22:24 +0000 | [diff] [blame] | 445 | emitConstantPool(F.getConstantPool()); |
| 446 | initJumpTableInfo(F.getJumpTableInfo()); |
| 447 | |
| 448 | // About to start emitting the machine code for the function. |
Chris Lattner | 0eb4d6b | 2006-05-03 01:03:20 +0000 | [diff] [blame] | 449 | emitAlignment(std::max(F.getFunction()->getAlignment(), 8U)); |
Chris Lattner | f75f9be | 2006-05-02 23:22:24 +0000 | [diff] [blame] | 450 | TheJIT->updateGlobalMapping(F.getFunction(), CurBufferPtr); |
Evan Cheng | 55fc280 | 2006-07-25 20:40:54 +0000 | [diff] [blame] | 451 | |
Chris Lattner | b4432f3 | 2006-05-03 17:10:41 +0000 | [diff] [blame] | 452 | MBBLocations.clear(); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 453 | } |
| 454 | |
Chris Lattner | 43b429b | 2006-05-02 18:27:26 +0000 | [diff] [blame] | 455 | bool JITEmitter::finishFunction(MachineFunction &F) { |
Chris Lattner | e993cc2 | 2006-05-11 23:08:08 +0000 | [diff] [blame] | 456 | if (CurBufferPtr == BufferEnd) { |
| 457 | // FIXME: Allocate more space, then try again. |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 458 | cerr << "JIT: Ran out of space for generated machine code!\n"; |
Chris Lattner | e993cc2 | 2006-05-11 23:08:08 +0000 | [diff] [blame] | 459 | abort(); |
| 460 | } |
| 461 | |
Jim Laskey | b92767a | 2006-12-14 22:53:42 +0000 | [diff] [blame] | 462 | emitJumpTableInfo(F.getJumpTableInfo()); |
Chris Lattner | b4432f3 | 2006-05-03 17:10:41 +0000 | [diff] [blame] | 463 | |
Chris Lattner | a827953 | 2006-06-16 18:09:26 +0000 | [diff] [blame] | 464 | // FnStart is the start of the text, not the start of the constant pool and |
| 465 | // other per-function data. |
| 466 | unsigned char *FnStart = |
| 467 | (unsigned char *)TheJIT->getPointerToGlobalIfAvailable(F.getFunction()); |
| 468 | unsigned char *FnEnd = CurBufferPtr; |
| 469 | |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 470 | MemMgr->endFunctionBody(F.getFunction(), BufferBegin, FnEnd); |
Chris Lattner | a827953 | 2006-06-16 18:09:26 +0000 | [diff] [blame] | 471 | NumBytes += FnEnd-FnStart; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 472 | |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 473 | if (!Relocations.empty()) { |
Chris Lattner | e884dc2 | 2005-07-20 16:29:20 +0000 | [diff] [blame] | 474 | NumRelos += Relocations.size(); |
| 475 | |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 476 | // Resolve the relocations to concrete pointers. |
| 477 | for (unsigned i = 0, e = Relocations.size(); i != e; ++i) { |
| 478 | MachineRelocation &MR = Relocations[i]; |
| 479 | void *ResultPtr; |
Chris Lattner | d91ff7c | 2005-04-18 01:44:27 +0000 | [diff] [blame] | 480 | if (MR.isString()) { |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 481 | ResultPtr = TheJIT->getPointerToNamedFunction(MR.getString()); |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 482 | |
Chris Lattner | d91ff7c | 2005-04-18 01:44:27 +0000 | [diff] [blame] | 483 | // If the target REALLY wants a stub for this function, emit it now. |
Evan Cheng | 02aabbf | 2008-01-03 02:56:28 +0000 | [diff] [blame] | 484 | if (!MR.doesntNeedStub()) |
Chris Lattner | e748401 | 2007-02-24 02:57:03 +0000 | [diff] [blame] | 485 | ResultPtr = Resolver.getExternalFunctionStub(ResultPtr); |
Chris Lattner | d2d5c76 | 2006-05-03 18:55:56 +0000 | [diff] [blame] | 486 | } else if (MR.isGlobalValue()) { |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 487 | ResultPtr = getPointerToGlobal(MR.getGlobalValue(), |
Chris Lattner | 43b429b | 2006-05-02 18:27:26 +0000 | [diff] [blame] | 488 | BufferBegin+MR.getMachineCodeOffset(), |
Evan Cheng | 02aabbf | 2008-01-03 02:56:28 +0000 | [diff] [blame] | 489 | MR.doesntNeedStub()); |
Evan Cheng | be8c03f | 2008-01-04 10:46:51 +0000 | [diff] [blame] | 490 | } else if (MR.isGlobalValueLazyPtr()) { |
| 491 | ResultPtr = getPointerToGVLazyPtr(MR.getGlobalValue(), |
| 492 | BufferBegin+MR.getMachineCodeOffset(), |
| 493 | MR.doesntNeedStub()); |
Evan Cheng | f141cc4 | 2006-07-27 18:21:10 +0000 | [diff] [blame] | 494 | } else if (MR.isBasicBlock()) { |
| 495 | ResultPtr = (void*)getMachineBasicBlockAddress(MR.getBasicBlock()); |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 496 | } else if (MR.isConstantPoolIndex()) { |
Chris Lattner | d2d5c76 | 2006-05-03 18:55:56 +0000 | [diff] [blame] | 497 | ResultPtr=(void*)getConstantPoolEntryAddress(MR.getConstantPoolIndex()); |
Evan Cheng | 52b510b | 2006-06-23 01:02:37 +0000 | [diff] [blame] | 498 | } else { |
| 499 | assert(MR.isJumpTableIndex()); |
| 500 | ResultPtr=(void*)getJumpTableEntryAddress(MR.getJumpTableIndex()); |
Chris Lattner | d2d5c76 | 2006-05-03 18:55:56 +0000 | [diff] [blame] | 501 | } |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 502 | |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 503 | MR.setResultPointer(ResultPtr); |
Andrew Lenharth | 16ec33c | 2005-07-22 20:48:12 +0000 | [diff] [blame] | 504 | |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 505 | // if we are managing the GOT and the relocation wants an index, |
| 506 | // give it one |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 507 | if (MR.isGOTRelative() && MemMgr->isManagingGOT()) { |
Chris Lattner | e748401 | 2007-02-24 02:57:03 +0000 | [diff] [blame] | 508 | unsigned idx = Resolver.getGOTIndexForAddr(ResultPtr); |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 509 | MR.setGOTIndex(idx); |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 510 | if (((void**)MemMgr->getGOTBase())[idx] != ResultPtr) { |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 511 | DOUT << "GOT was out of date for " << ResultPtr |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 512 | << " pointing at " << ((void**)MemMgr->getGOTBase())[idx] |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 513 | << "\n"; |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 514 | ((void**)MemMgr->getGOTBase())[idx] = ResultPtr; |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 515 | } |
Andrew Lenharth | 16ec33c | 2005-07-22 20:48:12 +0000 | [diff] [blame] | 516 | } |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Chris Lattner | 43b429b | 2006-05-02 18:27:26 +0000 | [diff] [blame] | 519 | TheJIT->getJITInfo().relocate(BufferBegin, &Relocations[0], |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 520 | Relocations.size(), MemMgr->getGOTBase()); |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 521 | } |
| 522 | |
Chris Lattner | d2d5c76 | 2006-05-03 18:55:56 +0000 | [diff] [blame] | 523 | // Update the GOT entry for F to point to the new code. |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 524 | if (MemMgr->isManagingGOT()) { |
Chris Lattner | e748401 | 2007-02-24 02:57:03 +0000 | [diff] [blame] | 525 | unsigned idx = Resolver.getGOTIndexForAddr((void*)BufferBegin); |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 526 | if (((void**)MemMgr->getGOTBase())[idx] != (void*)BufferBegin) { |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 527 | DOUT << "GOT was out of date for " << (void*)BufferBegin |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 528 | << " pointing at " << ((void**)MemMgr->getGOTBase())[idx] << "\n"; |
| 529 | ((void**)MemMgr->getGOTBase())[idx] = (void*)BufferBegin; |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | |
Evan Cheng | 55fc280 | 2006-07-25 20:40:54 +0000 | [diff] [blame] | 533 | // Invalidate the icache if necessary. |
Evan Cheng | 55b5053 | 2006-07-27 06:33:55 +0000 | [diff] [blame] | 534 | synchronizeICache(FnStart, FnEnd-FnStart); |
Evan Cheng | 55fc280 | 2006-07-25 20:40:54 +0000 | [diff] [blame] | 535 | |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 536 | DOUT << "JIT: Finished CodeGen of [" << (void*)FnStart |
| 537 | << "] Function: " << F.getFunction()->getName() |
| 538 | << ": " << (FnEnd-FnStart) << " bytes of text, " |
| 539 | << Relocations.size() << " relocations\n"; |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 540 | Relocations.clear(); |
Anton Korobeynikov | 8cd4c3e | 2007-01-19 17:25:17 +0000 | [diff] [blame] | 541 | |
Chris Lattner | c5633c2 | 2007-01-20 20:51:43 +0000 | [diff] [blame] | 542 | #ifndef NDEBUG |
Anton Korobeynikov | c6551ff | 2007-03-06 05:32:48 +0000 | [diff] [blame] | 543 | if (sys::hasDisassembler()) |
| 544 | DOUT << "Disassembled code:\n" |
| 545 | << sys::disassembleBuffer(FnStart, FnEnd-FnStart, (uintptr_t)FnStart); |
Chris Lattner | c5633c2 | 2007-01-20 20:51:43 +0000 | [diff] [blame] | 546 | #endif |
Anton Korobeynikov | 8cd4c3e | 2007-01-19 17:25:17 +0000 | [diff] [blame] | 547 | |
Chris Lattner | 43b429b | 2006-05-02 18:27:26 +0000 | [diff] [blame] | 548 | return false; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 549 | } |
| 550 | |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 551 | void JITEmitter::emitConstantPool(MachineConstantPool *MCP) { |
Chris Lattner | fa77d43 | 2006-02-09 04:22:52 +0000 | [diff] [blame] | 552 | const std::vector<MachineConstantPoolEntry> &Constants = MCP->getConstants(); |
Chris Lattner | 2c0a6a1 | 2003-11-30 04:23:21 +0000 | [diff] [blame] | 553 | if (Constants.empty()) return; |
| 554 | |
Evan Cheng | cd5731d | 2006-09-12 20:59:59 +0000 | [diff] [blame] | 555 | MachineConstantPoolEntry CPE = Constants.back(); |
| 556 | unsigned Size = CPE.Offset; |
| 557 | const Type *Ty = CPE.isMachineConstantPoolEntry() |
Chris Lattner | 8a65009 | 2006-09-13 16:21:10 +0000 | [diff] [blame] | 558 | ? CPE.Val.MachineCPVal->getType() : CPE.Val.ConstVal->getType(); |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 559 | Size += TheJIT->getTargetData()->getABITypeSize(Ty); |
Chris Lattner | 2c0a6a1 | 2003-11-30 04:23:21 +0000 | [diff] [blame] | 560 | |
Chris Lattner | f75f9be | 2006-05-02 23:22:24 +0000 | [diff] [blame] | 561 | ConstantPoolBase = allocateSpace(Size, 1 << MCP->getConstantPoolAlignment()); |
Chris Lattner | 239862c | 2006-02-09 04:49:59 +0000 | [diff] [blame] | 562 | ConstantPool = MCP; |
Chris Lattner | f75f9be | 2006-05-02 23:22:24 +0000 | [diff] [blame] | 563 | |
| 564 | if (ConstantPoolBase == 0) return; // Buffer overflow. |
| 565 | |
Chris Lattner | 239862c | 2006-02-09 04:49:59 +0000 | [diff] [blame] | 566 | // Initialize the memory for all of the constant pool entries. |
Chris Lattner | 3029f92 | 2006-02-09 04:46:04 +0000 | [diff] [blame] | 567 | for (unsigned i = 0, e = Constants.size(); i != e; ++i) { |
Chris Lattner | 239862c | 2006-02-09 04:49:59 +0000 | [diff] [blame] | 568 | void *CAddr = (char*)ConstantPoolBase+Constants[i].Offset; |
Evan Cheng | cd5731d | 2006-09-12 20:59:59 +0000 | [diff] [blame] | 569 | if (Constants[i].isMachineConstantPoolEntry()) { |
| 570 | // FIXME: add support to lower machine constant pool values into bytes! |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 571 | cerr << "Initialize memory with machine specific constant pool entry" |
| 572 | << " has not been implemented!\n"; |
Evan Cheng | cd5731d | 2006-09-12 20:59:59 +0000 | [diff] [blame] | 573 | abort(); |
| 574 | } |
| 575 | TheJIT->InitializeMemory(Constants[i].Val.ConstVal, CAddr); |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 576 | } |
| 577 | } |
| 578 | |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 579 | void JITEmitter::initJumpTableInfo(MachineJumpTableInfo *MJTI) { |
| 580 | const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables(); |
| 581 | if (JT.empty()) return; |
| 582 | |
Chris Lattner | f75f9be | 2006-05-02 23:22:24 +0000 | [diff] [blame] | 583 | unsigned NumEntries = 0; |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 584 | for (unsigned i = 0, e = JT.size(); i != e; ++i) |
Chris Lattner | f75f9be | 2006-05-02 23:22:24 +0000 | [diff] [blame] | 585 | NumEntries += JT[i].MBBs.size(); |
| 586 | |
| 587 | unsigned EntrySize = MJTI->getEntrySize(); |
| 588 | |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 589 | // Just allocate space for all the jump tables now. We will fix up the actual |
| 590 | // MBB entries in the tables after we emit the code for each block, since then |
| 591 | // we will know the final locations of the MBBs in memory. |
| 592 | JumpTable = MJTI; |
Chris Lattner | f75f9be | 2006-05-02 23:22:24 +0000 | [diff] [blame] | 593 | JumpTableBase = allocateSpace(NumEntries * EntrySize, MJTI->getAlignment()); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 594 | } |
| 595 | |
Jim Laskey | b92767a | 2006-12-14 22:53:42 +0000 | [diff] [blame] | 596 | void JITEmitter::emitJumpTableInfo(MachineJumpTableInfo *MJTI) { |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 597 | const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables(); |
Chris Lattner | f75f9be | 2006-05-02 23:22:24 +0000 | [diff] [blame] | 598 | if (JT.empty() || JumpTableBase == 0) return; |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 599 | |
Jim Laskey | b92767a | 2006-12-14 22:53:42 +0000 | [diff] [blame] | 600 | if (TargetMachine::getRelocationModel() == Reloc::PIC_) { |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 601 | assert(MJTI->getEntrySize() == 4 && "Cross JIT'ing?"); |
| 602 | // For each jump table, place the offset from the beginning of the table |
| 603 | // to the target address. |
| 604 | int *SlotPtr = (int*)JumpTableBase; |
Chris Lattner | 32ca55f | 2006-05-03 00:13:06 +0000 | [diff] [blame] | 605 | |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 606 | for (unsigned i = 0, e = JT.size(); i != e; ++i) { |
| 607 | const std::vector<MachineBasicBlock*> &MBBs = JT[i].MBBs; |
| 608 | // Store the offset of the basic block for this jump table slot in the |
| 609 | // memory we allocated for the jump table in 'initJumpTableInfo' |
| 610 | intptr_t Base = (intptr_t)SlotPtr; |
Evan Cheng | 2a3e08b | 2008-01-05 02:26:58 +0000 | [diff] [blame] | 611 | for (unsigned mi = 0, me = MBBs.size(); mi != me; ++mi) { |
| 612 | intptr_t MBBAddr = getMachineBasicBlockAddress(MBBs[mi]); |
| 613 | *SlotPtr++ = TheJIT->getJITInfo().getPICJumpTableEntry(MBBAddr, Base); |
| 614 | } |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 615 | } |
| 616 | } else { |
| 617 | assert(MJTI->getEntrySize() == sizeof(void*) && "Cross JIT'ing?"); |
| 618 | |
| 619 | // For each jump table, map each target in the jump table to the address of |
| 620 | // an emitted MachineBasicBlock. |
| 621 | intptr_t *SlotPtr = (intptr_t*)JumpTableBase; |
| 622 | |
| 623 | for (unsigned i = 0, e = JT.size(); i != e; ++i) { |
| 624 | const std::vector<MachineBasicBlock*> &MBBs = JT[i].MBBs; |
| 625 | // Store the address of the basic block for this jump table slot in the |
| 626 | // memory we allocated for the jump table in 'initJumpTableInfo' |
| 627 | for (unsigned mi = 0, me = MBBs.size(); mi != me; ++mi) |
| 628 | *SlotPtr++ = getMachineBasicBlockAddress(MBBs[mi]); |
| 629 | } |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 630 | } |
| 631 | } |
| 632 | |
Evan Cheng | 9a1e9b9 | 2006-11-16 20:04:54 +0000 | [diff] [blame] | 633 | void JITEmitter::startFunctionStub(unsigned StubSize, unsigned Alignment) { |
Chris Lattner | 43b429b | 2006-05-02 18:27:26 +0000 | [diff] [blame] | 634 | SavedBufferBegin = BufferBegin; |
| 635 | SavedBufferEnd = BufferEnd; |
| 636 | SavedCurBufferPtr = CurBufferPtr; |
| 637 | |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 638 | BufferBegin = CurBufferPtr = MemMgr->allocateStub(StubSize, Alignment); |
Chris Lattner | 43b429b | 2006-05-02 18:27:26 +0000 | [diff] [blame] | 639 | BufferEnd = BufferBegin+StubSize+1; |
Chris Lattner | 6125fdd | 2003-05-09 03:30:07 +0000 | [diff] [blame] | 640 | } |
| 641 | |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 642 | void *JITEmitter::finishFunctionStub(const Function *F) { |
Chris Lattner | 43b429b | 2006-05-02 18:27:26 +0000 | [diff] [blame] | 643 | NumBytes += getCurrentPCOffset(); |
| 644 | std::swap(SavedBufferBegin, BufferBegin); |
| 645 | BufferEnd = SavedBufferEnd; |
| 646 | CurBufferPtr = SavedCurBufferPtr; |
| 647 | return SavedBufferBegin; |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 648 | } |
| 649 | |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 650 | // getConstantPoolEntryAddress - Return the address of the 'ConstantNum' entry |
| 651 | // in the constant pool that was last emitted with the 'emitConstantPool' |
| 652 | // method. |
| 653 | // |
Chris Lattner | b4432f3 | 2006-05-03 17:10:41 +0000 | [diff] [blame] | 654 | intptr_t JITEmitter::getConstantPoolEntryAddress(unsigned ConstantNum) const { |
Chris Lattner | 239862c | 2006-02-09 04:49:59 +0000 | [diff] [blame] | 655 | assert(ConstantNum < ConstantPool->getConstants().size() && |
Misha Brukman | 3c94497 | 2005-04-22 04:08:30 +0000 | [diff] [blame] | 656 | "Invalid ConstantPoolIndex!"); |
Chris Lattner | 239862c | 2006-02-09 04:49:59 +0000 | [diff] [blame] | 657 | return (intptr_t)ConstantPoolBase + |
| 658 | ConstantPool->getConstants()[ConstantNum].Offset; |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 659 | } |
| 660 | |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 661 | // getJumpTableEntryAddress - Return the address of the JumpTable with index |
| 662 | // 'Index' in the jumpp table that was last initialized with 'initJumpTableInfo' |
| 663 | // |
Chris Lattner | b4432f3 | 2006-05-03 17:10:41 +0000 | [diff] [blame] | 664 | intptr_t JITEmitter::getJumpTableEntryAddress(unsigned Index) const { |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 665 | const std::vector<MachineJumpTableEntry> &JT = JumpTable->getJumpTables(); |
| 666 | assert(Index < JT.size() && "Invalid jump table index!"); |
| 667 | |
| 668 | unsigned Offset = 0; |
| 669 | unsigned EntrySize = JumpTable->getEntrySize(); |
| 670 | |
| 671 | for (unsigned i = 0; i < Index; ++i) |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 672 | Offset += JT[i].MBBs.size(); |
| 673 | |
| 674 | Offset *= EntrySize; |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 675 | |
Nate Begeman | c34b227 | 2006-04-25 17:46:32 +0000 | [diff] [blame] | 676 | return (intptr_t)((char *)JumpTableBase + Offset); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 677 | } |
| 678 | |
Chris Lattner | e993cc2 | 2006-05-11 23:08:08 +0000 | [diff] [blame] | 679 | //===----------------------------------------------------------------------===// |
| 680 | // Public interface to this file |
| 681 | //===----------------------------------------------------------------------===// |
| 682 | |
Chris Lattner | 9f2f142 | 2007-12-06 01:08:09 +0000 | [diff] [blame] | 683 | MachineCodeEmitter *JIT::createEmitter(JIT &jit, JITMemoryManager *JMM) { |
| 684 | return new JITEmitter(jit, JMM); |
Chris Lattner | e993cc2 | 2006-05-11 23:08:08 +0000 | [diff] [blame] | 685 | } |
| 686 | |
Misha Brukman | d69c1e6 | 2003-07-28 19:09:06 +0000 | [diff] [blame] | 687 | // getPointerToNamedFunction - This function is used as a global wrapper to |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 688 | // JIT::getPointerToNamedFunction for the purpose of resolving symbols when |
Misha Brukman | d69c1e6 | 2003-07-28 19:09:06 +0000 | [diff] [blame] | 689 | // bugpoint is debugging the JIT. In that scenario, we are loading an .so and |
| 690 | // need to resolve function(s) that are being mis-codegenerated, so we need to |
| 691 | // resolve their addresses at runtime, and this is the way to do it. |
| 692 | extern "C" { |
| 693 | void *getPointerToNamedFunction(const char *Name) { |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 694 | if (Function *F = TheJIT->FindFunctionNamed(Name)) |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 695 | return TheJIT->getPointerToFunction(F); |
| 696 | return TheJIT->getPointerToNamedFunction(Name); |
Misha Brukman | d69c1e6 | 2003-07-28 19:09:06 +0000 | [diff] [blame] | 697 | } |
| 698 | } |
Chris Lattner | e993cc2 | 2006-05-11 23:08:08 +0000 | [diff] [blame] | 699 | |
| 700 | // getPointerToFunctionOrStub - If the specified function has been |
| 701 | // code-gen'd, return a pointer to the function. If not, compile it, or use |
| 702 | // a stub to implement lazy compilation if available. |
| 703 | // |
| 704 | void *JIT::getPointerToFunctionOrStub(Function *F) { |
| 705 | // If we have already code generated the function, just return the address. |
| 706 | if (void *Addr = getPointerToGlobalIfAvailable(F)) |
| 707 | return Addr; |
| 708 | |
Chris Lattner | e748401 | 2007-02-24 02:57:03 +0000 | [diff] [blame] | 709 | // Get a stub if the target supports it. |
| 710 | assert(dynamic_cast<JITEmitter*>(MCE) && "Unexpected MCE?"); |
| 711 | JITEmitter *JE = static_cast<JITEmitter*>(getCodeEmitter()); |
| 712 | return JE->getJITResolver().getFunctionStub(F); |
Chris Lattner | e993cc2 | 2006-05-11 23:08:08 +0000 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | /// freeMachineCodeForFunction - release machine code memory for given Function. |
| 716 | /// |
| 717 | void JIT::freeMachineCodeForFunction(Function *F) { |
| 718 | // Delete translation for this from the ExecutionEngine, so it will get |
| 719 | // retranslated next time it is used. |
| 720 | updateGlobalMapping(F, 0); |
| 721 | |
| 722 | // Free the actual memory for the function body and related stuff. |
| 723 | assert(dynamic_cast<JITEmitter*>(MCE) && "Unexpected MCE?"); |
Chris Lattner | e748401 | 2007-02-24 02:57:03 +0000 | [diff] [blame] | 724 | static_cast<JITEmitter*>(MCE)->deallocateMemForFunction(F); |
Chris Lattner | e993cc2 | 2006-05-11 23:08:08 +0000 | [diff] [blame] | 725 | } |
| 726 | |