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" |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 17 | #include "JITDwarfEmitter.h" |
Chris Lattner | 2c0a6a1 | 2003-11-30 04:23:21 +0000 | [diff] [blame] | 18 | #include "llvm/Constant.h" |
| 19 | #include "llvm/Module.h" |
Chris Lattner | 5b3a455 | 2005-03-17 15:38:16 +0000 | [diff] [blame] | 20 | #include "llvm/Type.h" |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineCodeEmitter.h" |
| 22 | #include "llvm/CodeGen/MachineFunction.h" |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineConstantPool.h" |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineRelocation.h" |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 27 | #include "llvm/ExecutionEngine/JITMemoryManager.h" |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 28 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 29 | #include "llvm/Target/TargetJITInfo.h" |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 30 | #include "llvm/Target/TargetMachine.h" |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 31 | #include "llvm/Target/TargetOptions.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 32 | #include "llvm/Support/Debug.h" |
Chris Lattner | e7fd553 | 2006-05-08 22:00:52 +0000 | [diff] [blame] | 33 | #include "llvm/Support/MutexGuard.h" |
Anton Korobeynikov | fd58e6e | 2007-01-23 10:26:08 +0000 | [diff] [blame] | 34 | #include "llvm/System/Disassembler.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/Statistic.h" |
Andrew Lenharth | a00269b | 2005-07-29 23:40:16 +0000 | [diff] [blame] | 36 | #include <algorithm> |
Chris Lattner | c19aade | 2003-12-08 08:06:28 +0000 | [diff] [blame] | 37 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 38 | |
Chris Lattner | 3634373 | 2006-12-19 22:43:32 +0000 | [diff] [blame] | 39 | STATISTIC(NumBytes, "Number of bytes of machine code compiled"); |
| 40 | STATISTIC(NumRelos, "Number of relocations applied"); |
| 41 | static JIT *TheJIT = 0; |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 42 | |
Andrew Lenharth | a00269b | 2005-07-29 23:40:16 +0000 | [diff] [blame] | 43 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 44 | //===----------------------------------------------------------------------===// |
| 45 | // JIT lazy compilation code. |
| 46 | // |
| 47 | namespace { |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 48 | class JITResolverState { |
| 49 | private: |
| 50 | /// FunctionToStubMap - Keep track of the stub created for a particular |
| 51 | /// function so that we can reuse them if necessary. |
| 52 | std::map<Function*, void*> FunctionToStubMap; |
| 53 | |
| 54 | /// StubToFunctionMap - Keep track of the function that each stub |
| 55 | /// corresponds to. |
| 56 | std::map<void*, Function*> StubToFunctionMap; |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 57 | |
Evan Cheng | be8c03f | 2008-01-04 10:46:51 +0000 | [diff] [blame] | 58 | /// GlobalToLazyPtrMap - Keep track of the lazy pointer created for a |
| 59 | /// particular GlobalVariable so that we can reuse them if necessary. |
| 60 | std::map<GlobalValue*, void*> GlobalToLazyPtrMap; |
| 61 | |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 62 | public: |
| 63 | std::map<Function*, void*>& getFunctionToStubMap(const MutexGuard& locked) { |
| 64 | assert(locked.holds(TheJIT->lock)); |
| 65 | return FunctionToStubMap; |
| 66 | } |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 67 | |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 68 | std::map<void*, Function*>& getStubToFunctionMap(const MutexGuard& locked) { |
| 69 | assert(locked.holds(TheJIT->lock)); |
| 70 | return StubToFunctionMap; |
| 71 | } |
Evan Cheng | be8c03f | 2008-01-04 10:46:51 +0000 | [diff] [blame] | 72 | |
| 73 | std::map<GlobalValue*, void*>& |
| 74 | getGlobalToLazyPtrMap(const MutexGuard& locked) { |
| 75 | assert(locked.holds(TheJIT->lock)); |
| 76 | return GlobalToLazyPtrMap; |
| 77 | } |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 78 | }; |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 79 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 80 | /// JITResolver - Keep track of, and resolve, call sites for functions that |
| 81 | /// have not yet been compiled. |
| 82 | class JITResolver { |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 83 | /// LazyResolverFn - The target lazy resolver function that we actually |
| 84 | /// rewrite instructions to use. |
| 85 | TargetJITInfo::LazyResolverFn LazyResolverFn; |
| 86 | |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 87 | JITResolverState state; |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 88 | |
Chris Lattner | d91ff7c | 2005-04-18 01:44:27 +0000 | [diff] [blame] | 89 | /// ExternalFnToStubMap - This is the equivalent of FunctionToStubMap for |
| 90 | /// external functions. |
| 91 | std::map<void*, void*> ExternalFnToStubMap; |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 92 | |
| 93 | //map addresses to indexes in the GOT |
| 94 | std::map<void*, unsigned> revGOTMap; |
| 95 | unsigned nextGOTIndex; |
| 96 | |
Chris Lattner | e748401 | 2007-02-24 02:57:03 +0000 | [diff] [blame] | 97 | static JITResolver *TheJITResolver; |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 98 | public: |
Dan Gohman | 950a4c4 | 2008-03-25 22:06:05 +0000 | [diff] [blame] | 99 | explicit JITResolver(JIT &jit) : nextGOTIndex(0) { |
Chris Lattner | e748401 | 2007-02-24 02:57:03 +0000 | [diff] [blame] | 100 | TheJIT = &jit; |
| 101 | |
| 102 | LazyResolverFn = jit.getJITInfo().getLazyResolverFunction(JITCompilerFn); |
| 103 | assert(TheJITResolver == 0 && "Multiple JIT resolvers?"); |
| 104 | TheJITResolver = this; |
| 105 | } |
| 106 | |
| 107 | ~JITResolver() { |
| 108 | TheJITResolver = 0; |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 109 | } |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 110 | |
| 111 | /// getFunctionStub - This returns a pointer to a function stub, creating |
| 112 | /// one on demand as needed. |
| 113 | void *getFunctionStub(Function *F); |
| 114 | |
Chris Lattner | d91ff7c | 2005-04-18 01:44:27 +0000 | [diff] [blame] | 115 | /// getExternalFunctionStub - Return a stub for the function at the |
| 116 | /// specified address, created lazily on demand. |
| 117 | void *getExternalFunctionStub(void *FnAddr); |
| 118 | |
Evan Cheng | be8c03f | 2008-01-04 10:46:51 +0000 | [diff] [blame] | 119 | /// getGlobalValueLazyPtr - Return a lazy pointer containing the specified |
| 120 | /// GV address. |
| 121 | void *getGlobalValueLazyPtr(GlobalValue *V, void *GVAddress); |
| 122 | |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 123 | /// AddCallbackAtLocation - If the target is capable of rewriting an |
| 124 | /// instruction without the use of a stub, record the location of the use so |
| 125 | /// we know which function is being used at the location. |
| 126 | void *AddCallbackAtLocation(Function *F, void *Location) { |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 127 | MutexGuard locked(TheJIT->lock); |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 128 | /// Get the target-specific JIT resolver function. |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 129 | state.getStubToFunctionMap(locked)[Location] = F; |
Chris Lattner | 870286a | 2006-06-01 17:29:22 +0000 | [diff] [blame] | 130 | return (void*)(intptr_t)LazyResolverFn; |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 133 | /// getGOTIndexForAddress - Return a new or existing index in the GOT for |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 134 | /// an address. This function only manages slots, it does not manage the |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 135 | /// contents of the slots or the memory associated with the GOT. |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 136 | unsigned getGOTIndexForAddr(void *addr); |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 137 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 138 | /// JITCompilerFn - This function is called to resolve a stub to a compiled |
| 139 | /// address. If the LLVM Function corresponding to the stub has not yet |
| 140 | /// been compiled, this function compiles it first. |
| 141 | static void *JITCompilerFn(void *Stub); |
| 142 | }; |
| 143 | } |
| 144 | |
Chris Lattner | e748401 | 2007-02-24 02:57:03 +0000 | [diff] [blame] | 145 | JITResolver *JITResolver::TheJITResolver = 0; |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 146 | |
Evan Cheng | 55b5053 | 2006-07-27 06:33:55 +0000 | [diff] [blame] | 147 | #if (defined(__POWERPC__) || defined (__ppc__) || defined(_POWER)) && \ |
| 148 | defined(__APPLE__) |
| 149 | extern "C" void sys_icache_invalidate(const void *Addr, size_t len); |
| 150 | #endif |
| 151 | |
| 152 | /// synchronizeICache - On some targets, the JIT emitted code must be |
| 153 | /// explicitly refetched to ensure correct execution. |
| 154 | static void synchronizeICache(const void *Addr, size_t len) { |
| 155 | #if (defined(__POWERPC__) || defined (__ppc__) || defined(_POWER)) && \ |
| 156 | defined(__APPLE__) |
Jim Laskey | 2e9f368 | 2006-07-27 13:40:34 +0000 | [diff] [blame] | 157 | sys_icache_invalidate(Addr, len); |
Evan Cheng | 55b5053 | 2006-07-27 06:33:55 +0000 | [diff] [blame] | 158 | #endif |
| 159 | } |
| 160 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 161 | /// getFunctionStub - This returns a pointer to a function stub, creating |
| 162 | /// one on demand as needed. |
| 163 | void *JITResolver::getFunctionStub(Function *F) { |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 164 | MutexGuard locked(TheJIT->lock); |
| 165 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 166 | // If we already have a stub for this function, recycle it. |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 167 | void *&Stub = state.getFunctionToStubMap(locked)[F]; |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 168 | if (Stub) return Stub; |
| 169 | |
Chris Lattner | b43dbdc | 2004-11-22 07:24:43 +0000 | [diff] [blame] | 170 | // Call the lazy resolver function unless we already KNOW it is an external |
| 171 | // function, in which case we just skip the lazy resolution step. |
Chris Lattner | 870286a | 2006-06-01 17:29:22 +0000 | [diff] [blame] | 172 | void *Actual = (void*)(intptr_t)LazyResolverFn; |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 173 | if (F->isDeclaration() && !F->hasNotBeenReadFromBitcode()) |
Chris Lattner | b43dbdc | 2004-11-22 07:24:43 +0000 | [diff] [blame] | 174 | Actual = TheJIT->getPointerToFunction(F); |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 175 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 176 | // Otherwise, codegen a new stub. For now, the stub will call the lazy |
| 177 | // resolver function. |
Nicolas Geoffray | 51cc3c1 | 2008-04-16 20:46:05 +0000 | [diff] [blame^] | 178 | Stub = TheJIT->getJITInfo().emitFunctionStub(F, Actual, |
Chris Lattner | e748401 | 2007-02-24 02:57:03 +0000 | [diff] [blame] | 179 | *TheJIT->getCodeEmitter()); |
Chris Lattner | b43dbdc | 2004-11-22 07:24:43 +0000 | [diff] [blame] | 180 | |
Chris Lattner | 870286a | 2006-06-01 17:29:22 +0000 | [diff] [blame] | 181 | if (Actual != (void*)(intptr_t)LazyResolverFn) { |
Chris Lattner | b43dbdc | 2004-11-22 07:24:43 +0000 | [diff] [blame] | 182 | // If we are getting the stub for an external function, we really want the |
| 183 | // address of the stub in the GlobalAddressMap for the JIT, not the address |
| 184 | // of the external function. |
| 185 | TheJIT->updateGlobalMapping(F, Stub); |
| 186 | } |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 187 | |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 188 | DOUT << "JIT: Stub emitted at [" << Stub << "] for function '" |
| 189 | << F->getName() << "'\n"; |
Chris Lattner | cb47941 | 2004-11-21 03:44:32 +0000 | [diff] [blame] | 190 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 191 | // Finally, keep track of the stub-to-Function mapping so that the |
| 192 | // JITCompilerFn knows which function to compile! |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 193 | state.getStubToFunctionMap(locked)[Stub] = F; |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 194 | return Stub; |
| 195 | } |
| 196 | |
Evan Cheng | be8c03f | 2008-01-04 10:46:51 +0000 | [diff] [blame] | 197 | /// getGlobalValueLazyPtr - Return a lazy pointer containing the specified |
| 198 | /// GV address. |
| 199 | void *JITResolver::getGlobalValueLazyPtr(GlobalValue *GV, void *GVAddress) { |
| 200 | MutexGuard locked(TheJIT->lock); |
| 201 | |
| 202 | // If we already have a stub for this global variable, recycle it. |
| 203 | void *&LazyPtr = state.getGlobalToLazyPtrMap(locked)[GV]; |
| 204 | if (LazyPtr) return LazyPtr; |
| 205 | |
| 206 | // Otherwise, codegen a new lazy pointer. |
Nicolas Geoffray | 51cc3c1 | 2008-04-16 20:46:05 +0000 | [diff] [blame^] | 207 | LazyPtr = TheJIT->getJITInfo().emitGlobalValueLazyPtr(GV, GVAddress, |
Evan Cheng | be8c03f | 2008-01-04 10:46:51 +0000 | [diff] [blame] | 208 | *TheJIT->getCodeEmitter()); |
| 209 | |
| 210 | DOUT << "JIT: Stub emitted at [" << LazyPtr << "] for GV '" |
| 211 | << GV->getName() << "'\n"; |
| 212 | |
| 213 | return LazyPtr; |
| 214 | } |
| 215 | |
Chris Lattner | d91ff7c | 2005-04-18 01:44:27 +0000 | [diff] [blame] | 216 | /// getExternalFunctionStub - Return a stub for the function at the |
| 217 | /// specified address, created lazily on demand. |
| 218 | void *JITResolver::getExternalFunctionStub(void *FnAddr) { |
| 219 | // If we already have a stub for this function, recycle it. |
| 220 | void *&Stub = ExternalFnToStubMap[FnAddr]; |
| 221 | if (Stub) return Stub; |
| 222 | |
Nicolas Geoffray | 51cc3c1 | 2008-04-16 20:46:05 +0000 | [diff] [blame^] | 223 | Stub = TheJIT->getJITInfo().emitFunctionStub(0, FnAddr, |
Chris Lattner | e748401 | 2007-02-24 02:57:03 +0000 | [diff] [blame] | 224 | *TheJIT->getCodeEmitter()); |
Evan Cheng | 55fc280 | 2006-07-25 20:40:54 +0000 | [diff] [blame] | 225 | |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 226 | DOUT << "JIT: Stub emitted at [" << Stub |
| 227 | << "] for external function at '" << FnAddr << "'\n"; |
Chris Lattner | d91ff7c | 2005-04-18 01:44:27 +0000 | [diff] [blame] | 228 | return Stub; |
| 229 | } |
| 230 | |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 231 | unsigned JITResolver::getGOTIndexForAddr(void* addr) { |
| 232 | unsigned idx = revGOTMap[addr]; |
| 233 | if (!idx) { |
| 234 | idx = ++nextGOTIndex; |
| 235 | revGOTMap[addr] = idx; |
Evan Cheng | 97b8c40 | 2008-04-12 00:22:01 +0000 | [diff] [blame] | 236 | DOUT << "Adding GOT entry " << idx << " for addr " << addr << "\n"; |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 237 | } |
| 238 | return idx; |
| 239 | } |
Chris Lattner | d91ff7c | 2005-04-18 01:44:27 +0000 | [diff] [blame] | 240 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 241 | /// JITCompilerFn - This function is called when a lazy compilation stub has |
| 242 | /// been entered. It looks up which function this stub corresponds to, compiles |
| 243 | /// it if necessary, then returns the resultant function pointer. |
| 244 | void *JITResolver::JITCompilerFn(void *Stub) { |
Chris Lattner | e748401 | 2007-02-24 02:57:03 +0000 | [diff] [blame] | 245 | JITResolver &JR = *TheJITResolver; |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 246 | |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 247 | MutexGuard locked(TheJIT->lock); |
| 248 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 249 | // The address given to us for the stub may not be exactly right, it might be |
| 250 | // a little bit after the stub. As such, use upper_bound to find it. |
| 251 | std::map<void*, Function*>::iterator I = |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 252 | JR.state.getStubToFunctionMap(locked).upper_bound(Stub); |
Chris Lattner | 2199877 | 2006-01-07 06:20:51 +0000 | [diff] [blame] | 253 | assert(I != JR.state.getStubToFunctionMap(locked).begin() && |
| 254 | "This is not a known stub!"); |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 255 | Function *F = (--I)->second; |
| 256 | |
Evan Cheng | 9da60f9 | 2007-06-30 00:10:37 +0000 | [diff] [blame] | 257 | // If we have already code generated the function, just return the address. |
| 258 | void *Result = TheJIT->getPointerToGlobalIfAvailable(F); |
Chris Lattner | 9cab56d | 2006-11-09 19:32:13 +0000 | [diff] [blame] | 259 | |
Evan Cheng | 9da60f9 | 2007-06-30 00:10:37 +0000 | [diff] [blame] | 260 | if (!Result) { |
| 261 | // Otherwise we don't have it, do lazy compilation now. |
| 262 | |
| 263 | // If lazy compilation is disabled, emit a useful error message and abort. |
| 264 | if (TheJIT->isLazyCompilationDisabled()) { |
| 265 | cerr << "LLVM JIT requested to do lazy compilation of function '" |
| 266 | << F->getName() << "' when lazy compiles are disabled!\n"; |
| 267 | abort(); |
| 268 | } |
| 269 | |
| 270 | // We might like to remove the stub from the StubToFunction map. |
| 271 | // We can't do that! Multiple threads could be stuck, waiting to acquire the |
| 272 | // lock above. As soon as the 1st function finishes compiling the function, |
| 273 | // the next one will be released, and needs to be able to find the function |
| 274 | // it needs to call. |
| 275 | //JR.state.getStubToFunctionMap(locked).erase(I); |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 276 | |
Evan Cheng | 9da60f9 | 2007-06-30 00:10:37 +0000 | [diff] [blame] | 277 | DOUT << "JIT: Lazily resolving function '" << F->getName() |
| 278 | << "' In stub ptr = " << Stub << " actual ptr = " |
| 279 | << I->first << "\n"; |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 280 | |
Evan Cheng | 9da60f9 | 2007-06-30 00:10:37 +0000 | [diff] [blame] | 281 | Result = TheJIT->getPointerToFunction(F); |
| 282 | } |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 283 | |
| 284 | // 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] | 285 | JR.state.getFunctionToStubMap(locked).erase(F); |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 286 | |
| 287 | // 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] | 288 | |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 289 | // What we will do is set the compiled function address to map to the |
| 290 | // 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] | 291 | // if they see it still using the stub address. |
| 292 | // Note: this is done so the Resolver doesn't have to manage GOT memory |
| 293 | // Do this without allocating map space if the target isn't using a GOT |
| 294 | if(JR.revGOTMap.find(Stub) != JR.revGOTMap.end()) |
| 295 | JR.revGOTMap[Result] = JR.revGOTMap[Stub]; |
| 296 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 297 | return Result; |
| 298 | } |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 299 | |
Chris Lattner | 8ac66c1 | 2008-04-04 05:51:42 +0000 | [diff] [blame] | 300 | //===----------------------------------------------------------------------===// |
| 301 | // Function Index Support |
| 302 | |
| 303 | // On MacOS we generate an index of currently JIT'd functions so that |
| 304 | // performance tools can determine a symbol name and accurate code range for a |
| 305 | // PC value. Because performance tools are generally asynchronous, the code |
| 306 | // below is written with the hope that it could be interrupted at any time and |
| 307 | // have useful answers. However, we don't go crazy with atomic operations, we |
| 308 | // just do a "reasonable effort". |
| 309 | #ifdef __APPLE__ |
Chris Lattner | 6098e4b | 2008-04-11 18:11:56 +0000 | [diff] [blame] | 310 | #define ENABLE_JIT_SYMBOL_TABLE 1 |
Chris Lattner | 8ac66c1 | 2008-04-04 05:51:42 +0000 | [diff] [blame] | 311 | #endif |
| 312 | |
| 313 | /// JitSymbolEntry - Each function that is JIT compiled results in one of these |
| 314 | /// being added to an array of symbols. This indicates the name of the function |
| 315 | /// as well as the address range it occupies. This allows the client to map |
| 316 | /// from a PC value to the name of the function. |
| 317 | struct JitSymbolEntry { |
| 318 | const char *FnName; // FnName - a strdup'd string. |
| 319 | void *FnStart; |
| 320 | intptr_t FnSize; |
| 321 | }; |
| 322 | |
| 323 | |
| 324 | struct JitSymbolTable { |
| 325 | /// NextPtr - This forms a linked list of JitSymbolTable entries. This |
| 326 | /// pointer is not used right now, but might be used in the future. Consider |
| 327 | /// it reserved for future use. |
| 328 | JitSymbolTable *NextPtr; |
| 329 | |
| 330 | /// Symbols - This is an array of JitSymbolEntry entries. Only the first |
| 331 | /// 'NumSymbols' symbols are valid. |
| 332 | JitSymbolEntry *Symbols; |
| 333 | |
| 334 | /// NumSymbols - This indicates the number entries in the Symbols array that |
| 335 | /// are valid. |
| 336 | unsigned NumSymbols; |
| 337 | |
| 338 | /// NumAllocated - This indicates the amount of space we have in the Symbols |
| 339 | /// array. This is a private field that should not be read by external tools. |
| 340 | unsigned NumAllocated; |
| 341 | }; |
| 342 | |
| 343 | #if ENABLE_JIT_SYMBOL_TABLE |
| 344 | JitSymbolTable *__jitSymbolTable; |
| 345 | #endif |
| 346 | |
| 347 | static void AddFunctionToSymbolTable(const char *FnName, |
| 348 | void *FnStart, intptr_t FnSize) { |
| 349 | assert(FnName != 0 && FnStart != 0 && "Bad symbol to add"); |
| 350 | JitSymbolTable **SymTabPtrPtr = 0; |
| 351 | #if !ENABLE_JIT_SYMBOL_TABLE |
| 352 | return; |
| 353 | #else |
| 354 | SymTabPtrPtr = &__jitSymbolTable; |
| 355 | #endif |
| 356 | |
| 357 | // If this is the first entry in the symbol table, add the JitSymbolTable |
| 358 | // index. |
| 359 | if (*SymTabPtrPtr == 0) { |
| 360 | JitSymbolTable *New = new JitSymbolTable(); |
| 361 | New->NextPtr = 0; |
| 362 | New->Symbols = 0; |
| 363 | New->NumSymbols = 0; |
| 364 | New->NumAllocated = 0; |
| 365 | *SymTabPtrPtr = New; |
| 366 | } |
| 367 | |
| 368 | JitSymbolTable *SymTabPtr = *SymTabPtrPtr; |
| 369 | |
| 370 | // If we have space in the table, reallocate the table. |
| 371 | if (SymTabPtr->NumSymbols >= SymTabPtr->NumAllocated) { |
| 372 | // If we don't have space, reallocate the table. |
Chris Lattner | 3b37448 | 2008-04-13 07:04:56 +0000 | [diff] [blame] | 373 | unsigned NewSize = std::max(64U, SymTabPtr->NumAllocated*2); |
Chris Lattner | 8ac66c1 | 2008-04-04 05:51:42 +0000 | [diff] [blame] | 374 | JitSymbolEntry *NewSymbols = new JitSymbolEntry[NewSize]; |
| 375 | JitSymbolEntry *OldSymbols = SymTabPtr->Symbols; |
| 376 | |
| 377 | // Copy the old entries over. |
| 378 | memcpy(NewSymbols, OldSymbols, |
Chris Lattner | 3b37448 | 2008-04-13 07:04:56 +0000 | [diff] [blame] | 379 | SymTabPtr->NumSymbols*sizeof(OldSymbols[0])); |
Chris Lattner | 8ac66c1 | 2008-04-04 05:51:42 +0000 | [diff] [blame] | 380 | |
| 381 | // Swap the new symbols in, delete the old ones. |
| 382 | SymTabPtr->Symbols = NewSymbols; |
Chris Lattner | 3b37448 | 2008-04-13 07:04:56 +0000 | [diff] [blame] | 383 | SymTabPtr->NumAllocated = NewSize; |
Chris Lattner | 8ac66c1 | 2008-04-04 05:51:42 +0000 | [diff] [blame] | 384 | delete [] OldSymbols; |
| 385 | } |
| 386 | |
| 387 | // Otherwise, we have enough space, just tack it onto the end of the array. |
| 388 | JitSymbolEntry &Entry = SymTabPtr->Symbols[SymTabPtr->NumSymbols]; |
| 389 | Entry.FnName = strdup(FnName); |
| 390 | Entry.FnStart = FnStart; |
| 391 | Entry.FnSize = FnSize; |
| 392 | ++SymTabPtr->NumSymbols; |
| 393 | } |
| 394 | |
| 395 | static void RemoveFunctionFromSymbolTable(void *FnStart) { |
| 396 | assert(FnStart && "Invalid function pointer"); |
| 397 | JitSymbolTable **SymTabPtrPtr = 0; |
| 398 | #if !ENABLE_JIT_SYMBOL_TABLE |
| 399 | return; |
| 400 | #else |
| 401 | SymTabPtrPtr = &__jitSymbolTable; |
| 402 | #endif |
| 403 | |
| 404 | JitSymbolTable *SymTabPtr = *SymTabPtrPtr; |
| 405 | JitSymbolEntry *Symbols = SymTabPtr->Symbols; |
| 406 | |
| 407 | // Scan the table to find its index. The table is not sorted, so do a linear |
| 408 | // scan. |
| 409 | unsigned Index; |
| 410 | for (Index = 0; Symbols[Index].FnStart != FnStart; ++Index) |
| 411 | assert(Index != SymTabPtr->NumSymbols && "Didn't find function!"); |
| 412 | |
| 413 | // Once we have an index, we know to nuke this entry, overwrite it with the |
| 414 | // entry at the end of the array, making the last entry redundant. |
| 415 | const char *OldName = Symbols[Index].FnName; |
| 416 | Symbols[Index] = Symbols[SymTabPtr->NumSymbols-1]; |
| 417 | free((void*)OldName); |
| 418 | |
| 419 | // Drop the number of symbols in the table. |
| 420 | --SymTabPtr->NumSymbols; |
| 421 | |
| 422 | // Finally, if we deleted the final symbol, deallocate the table itself. |
| 423 | if (SymTabPtr->NumSymbols == 0) |
| 424 | return; |
| 425 | |
| 426 | *SymTabPtrPtr = 0; |
| 427 | delete [] Symbols; |
| 428 | delete SymTabPtr; |
| 429 | } |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 430 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 431 | //===----------------------------------------------------------------------===// |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 432 | // JITEmitter code. |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 433 | // |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 434 | namespace { |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 435 | /// JITEmitter - The JIT implementation of the MachineCodeEmitter, which is |
| 436 | /// used to output functions to memory for execution. |
| 437 | class JITEmitter : public MachineCodeEmitter { |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 438 | JITMemoryManager *MemMgr; |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 439 | |
Chris Lattner | 6125fdd | 2003-05-09 03:30:07 +0000 | [diff] [blame] | 440 | // 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] | 441 | // save BufferBegin/BufferEnd/CurBufferPtr here. |
| 442 | unsigned char *SavedBufferBegin, *SavedBufferEnd, *SavedCurBufferPtr; |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 443 | |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 444 | /// Relocations - These are the relocations that the function needs, as |
| 445 | /// emitted. |
| 446 | std::vector<MachineRelocation> Relocations; |
Chris Lattner | b4432f3 | 2006-05-03 17:10:41 +0000 | [diff] [blame] | 447 | |
| 448 | /// MBBLocations - This vector is a mapping from MBB ID's to their address. |
| 449 | /// It is filled in by the StartMachineBasicBlock callback and queried by |
| 450 | /// the getMachineBasicBlockAddress callback. |
| 451 | std::vector<intptr_t> MBBLocations; |
Andrew Lenharth | 16ec33c | 2005-07-22 20:48:12 +0000 | [diff] [blame] | 452 | |
Chris Lattner | 239862c | 2006-02-09 04:49:59 +0000 | [diff] [blame] | 453 | /// ConstantPool - The constant pool for the current function. |
| 454 | /// |
| 455 | MachineConstantPool *ConstantPool; |
| 456 | |
| 457 | /// ConstantPoolBase - A pointer to the first entry in the constant pool. |
| 458 | /// |
| 459 | void *ConstantPoolBase; |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 460 | |
Nate Begeman | 019f851 | 2006-09-10 23:03:44 +0000 | [diff] [blame] | 461 | /// JumpTable - The jump tables for the current function. |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 462 | /// |
| 463 | MachineJumpTableInfo *JumpTable; |
| 464 | |
| 465 | /// JumpTableBase - A pointer to the first entry in the jump table. |
| 466 | /// |
| 467 | void *JumpTableBase; |
Evan Cheng | 2a3e08b | 2008-01-05 02:26:58 +0000 | [diff] [blame] | 468 | |
Chris Lattner | e748401 | 2007-02-24 02:57:03 +0000 | [diff] [blame] | 469 | /// Resolver - This contains info about the currently resolved functions. |
| 470 | JITResolver Resolver; |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 471 | |
| 472 | /// DE - The dwarf emitter for the jit. |
| 473 | JITDwarfEmitter *DE; |
| 474 | |
| 475 | /// LabelLocations - This vector is a mapping from Label ID's to their |
| 476 | /// address. |
| 477 | std::vector<intptr_t> LabelLocations; |
| 478 | |
| 479 | /// MMI - Machine module info for exception informations |
| 480 | MachineModuleInfo* MMI; |
| 481 | |
Chris Lattner | e748401 | 2007-02-24 02:57:03 +0000 | [diff] [blame] | 482 | public: |
Chris Lattner | 9f2f142 | 2007-12-06 01:08:09 +0000 | [diff] [blame] | 483 | JITEmitter(JIT &jit, JITMemoryManager *JMM) : Resolver(jit) { |
| 484 | MemMgr = JMM ? JMM : JITMemoryManager::CreateDefaultMemManager(); |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 485 | if (jit.getJITInfo().needsGOT()) { |
| 486 | MemMgr->AllocateGOT(); |
| 487 | DOUT << "JIT is managing a GOT\n"; |
| 488 | } |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 489 | |
| 490 | if (ExceptionHandling) DE = new JITDwarfEmitter(jit); |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 491 | } |
| 492 | ~JITEmitter() { |
| 493 | delete MemMgr; |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 494 | if (ExceptionHandling) delete DE; |
Andrew Lenharth | 16ec33c | 2005-07-22 20:48:12 +0000 | [diff] [blame] | 495 | } |
Chris Lattner | e748401 | 2007-02-24 02:57:03 +0000 | [diff] [blame] | 496 | |
| 497 | JITResolver &getJITResolver() { return Resolver; } |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 498 | |
| 499 | virtual void startFunction(MachineFunction &F); |
Chris Lattner | 43b429b | 2006-05-02 18:27:26 +0000 | [diff] [blame] | 500 | virtual bool finishFunction(MachineFunction &F); |
Chris Lattner | f75f9be | 2006-05-02 23:22:24 +0000 | [diff] [blame] | 501 | |
| 502 | void emitConstantPool(MachineConstantPool *MCP); |
| 503 | void initJumpTableInfo(MachineJumpTableInfo *MJTI); |
Jim Laskey | b92767a | 2006-12-14 22:53:42 +0000 | [diff] [blame] | 504 | void emitJumpTableInfo(MachineJumpTableInfo *MJTI); |
Chris Lattner | f75f9be | 2006-05-02 23:22:24 +0000 | [diff] [blame] | 505 | |
Nicolas Geoffray | 51cc3c1 | 2008-04-16 20:46:05 +0000 | [diff] [blame^] | 506 | virtual void startFunctionStub(const GlobalValue* F, unsigned StubSize, |
| 507 | unsigned Alignment = 1); |
| 508 | virtual void* finishFunctionStub(const GlobalValue *F); |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 509 | |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 510 | virtual void addRelocation(const MachineRelocation &MR) { |
| 511 | Relocations.push_back(MR); |
| 512 | } |
Chris Lattner | b4432f3 | 2006-05-03 17:10:41 +0000 | [diff] [blame] | 513 | |
| 514 | virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) { |
| 515 | if (MBBLocations.size() <= (unsigned)MBB->getNumber()) |
| 516 | MBBLocations.resize((MBB->getNumber()+1)*2); |
| 517 | MBBLocations[MBB->getNumber()] = getCurrentPCValue(); |
| 518 | } |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 519 | |
Chris Lattner | b4432f3 | 2006-05-03 17:10:41 +0000 | [diff] [blame] | 520 | virtual intptr_t getConstantPoolEntryAddress(unsigned Entry) const; |
| 521 | virtual intptr_t getJumpTableEntryAddress(unsigned Entry) const; |
Evan Cheng | 2a3e08b | 2008-01-05 02:26:58 +0000 | [diff] [blame] | 522 | |
Chris Lattner | b4432f3 | 2006-05-03 17:10:41 +0000 | [diff] [blame] | 523 | virtual intptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const { |
| 524 | assert(MBBLocations.size() > (unsigned)MBB->getNumber() && |
| 525 | MBBLocations[MBB->getNumber()] && "MBB not emitted!"); |
| 526 | return MBBLocations[MBB->getNumber()]; |
| 527 | } |
| 528 | |
Chris Lattner | e993cc2 | 2006-05-11 23:08:08 +0000 | [diff] [blame] | 529 | /// deallocateMemForFunction - Deallocate all memory for the specified |
| 530 | /// function body. |
| 531 | void deallocateMemForFunction(Function *F) { |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 532 | MemMgr->deallocateMemForFunction(F); |
Chris Lattner | e993cc2 | 2006-05-11 23:08:08 +0000 | [diff] [blame] | 533 | } |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 534 | |
| 535 | virtual void emitLabel(uint64_t LabelID) { |
| 536 | if (LabelLocations.size() <= LabelID) |
| 537 | LabelLocations.resize((LabelID+1)*2); |
| 538 | LabelLocations[LabelID] = getCurrentPCValue(); |
| 539 | } |
| 540 | |
| 541 | virtual intptr_t getLabelAddress(uint64_t LabelID) const { |
| 542 | assert(LabelLocations.size() > (unsigned)LabelID && |
| 543 | LabelLocations[LabelID] && "Label not emitted!"); |
| 544 | return LabelLocations[LabelID]; |
| 545 | } |
| 546 | |
| 547 | virtual void setModuleInfo(MachineModuleInfo* Info) { |
| 548 | MMI = Info; |
| 549 | if (ExceptionHandling) DE->setModuleInfo(Info); |
| 550 | } |
| 551 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 552 | private: |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 553 | void *getPointerToGlobal(GlobalValue *GV, void *Reference, bool NoNeedStub); |
Evan Cheng | be8c03f | 2008-01-04 10:46:51 +0000 | [diff] [blame] | 554 | void *getPointerToGVLazyPtr(GlobalValue *V, void *Reference, |
| 555 | bool NoNeedStub); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 556 | }; |
| 557 | } |
| 558 | |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 559 | void *JITEmitter::getPointerToGlobal(GlobalValue *V, void *Reference, |
| 560 | bool DoesntNeedStub) { |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 561 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) { |
| 562 | /// FIXME: If we straightened things out, this could actually emit the |
| 563 | /// global immediately instead of queuing it for codegen later! |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 564 | return TheJIT->getOrEmitGlobalVariable(GV); |
| 565 | } |
| 566 | |
| 567 | // If we have already compiled the function, return a pointer to its body. |
| 568 | Function *F = cast<Function>(V); |
| 569 | void *ResultPtr = TheJIT->getPointerToGlobalIfAvailable(F); |
| 570 | if (ResultPtr) return ResultPtr; |
| 571 | |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 572 | if (F->isDeclaration() && !F->hasNotBeenReadFromBitcode()) { |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 573 | // If this is an external function pointer, we can force the JIT to |
| 574 | // 'compile' it, which really just adds it to the map. |
Chris Lattner | b43dbdc | 2004-11-22 07:24:43 +0000 | [diff] [blame] | 575 | if (DoesntNeedStub) |
| 576 | return TheJIT->getPointerToFunction(F); |
| 577 | |
Chris Lattner | e748401 | 2007-02-24 02:57:03 +0000 | [diff] [blame] | 578 | return Resolver.getFunctionStub(F); |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 579 | } |
| 580 | |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 581 | // Okay, the function has not been compiled yet, if the target callback |
| 582 | // mechanism is capable of rewriting the instruction directly, prefer to do |
| 583 | // that instead of emitting a stub. |
| 584 | if (DoesntNeedStub) |
Chris Lattner | e748401 | 2007-02-24 02:57:03 +0000 | [diff] [blame] | 585 | return Resolver.AddCallbackAtLocation(F, Reference); |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 586 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 587 | // Otherwise, we have to emit a lazy resolving stub. |
Chris Lattner | e748401 | 2007-02-24 02:57:03 +0000 | [diff] [blame] | 588 | return Resolver.getFunctionStub(F); |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Evan Cheng | be8c03f | 2008-01-04 10:46:51 +0000 | [diff] [blame] | 591 | void *JITEmitter::getPointerToGVLazyPtr(GlobalValue *V, void *Reference, |
| 592 | bool DoesntNeedStub) { |
| 593 | // Make sure GV is emitted first. |
| 594 | // FIXME: For now, if the GV is an external function we force the JIT to |
| 595 | // compile it so the lazy pointer will contain the fully resolved address. |
| 596 | void *GVAddress = getPointerToGlobal(V, Reference, true); |
| 597 | return Resolver.getGlobalValueLazyPtr(V, GVAddress); |
| 598 | } |
| 599 | |
| 600 | |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 601 | void JITEmitter::startFunction(MachineFunction &F) { |
Chris Lattner | e993cc2 | 2006-05-11 23:08:08 +0000 | [diff] [blame] | 602 | uintptr_t ActualSize; |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 603 | BufferBegin = CurBufferPtr = MemMgr->startFunctionBody(F.getFunction(), |
| 604 | ActualSize); |
Chris Lattner | e993cc2 | 2006-05-11 23:08:08 +0000 | [diff] [blame] | 605 | BufferEnd = BufferBegin+ActualSize; |
Chris Lattner | f75f9be | 2006-05-02 23:22:24 +0000 | [diff] [blame] | 606 | |
Evan Cheng | 9a1e9b9 | 2006-11-16 20:04:54 +0000 | [diff] [blame] | 607 | // Ensure the constant pool/jump table info is at least 4-byte aligned. |
| 608 | emitAlignment(16); |
| 609 | |
Chris Lattner | f75f9be | 2006-05-02 23:22:24 +0000 | [diff] [blame] | 610 | emitConstantPool(F.getConstantPool()); |
| 611 | initJumpTableInfo(F.getJumpTableInfo()); |
| 612 | |
| 613 | // About to start emitting the machine code for the function. |
Chris Lattner | 0eb4d6b | 2006-05-03 01:03:20 +0000 | [diff] [blame] | 614 | emitAlignment(std::max(F.getFunction()->getAlignment(), 8U)); |
Chris Lattner | f75f9be | 2006-05-02 23:22:24 +0000 | [diff] [blame] | 615 | TheJIT->updateGlobalMapping(F.getFunction(), CurBufferPtr); |
Evan Cheng | 55fc280 | 2006-07-25 20:40:54 +0000 | [diff] [blame] | 616 | |
Chris Lattner | b4432f3 | 2006-05-03 17:10:41 +0000 | [diff] [blame] | 617 | MBBLocations.clear(); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 618 | } |
| 619 | |
Chris Lattner | 43b429b | 2006-05-02 18:27:26 +0000 | [diff] [blame] | 620 | bool JITEmitter::finishFunction(MachineFunction &F) { |
Chris Lattner | e993cc2 | 2006-05-11 23:08:08 +0000 | [diff] [blame] | 621 | if (CurBufferPtr == BufferEnd) { |
| 622 | // FIXME: Allocate more space, then try again. |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 623 | cerr << "JIT: Ran out of space for generated machine code!\n"; |
Chris Lattner | e993cc2 | 2006-05-11 23:08:08 +0000 | [diff] [blame] | 624 | abort(); |
| 625 | } |
| 626 | |
Jim Laskey | b92767a | 2006-12-14 22:53:42 +0000 | [diff] [blame] | 627 | emitJumpTableInfo(F.getJumpTableInfo()); |
Chris Lattner | b4432f3 | 2006-05-03 17:10:41 +0000 | [diff] [blame] | 628 | |
Chris Lattner | a827953 | 2006-06-16 18:09:26 +0000 | [diff] [blame] | 629 | // FnStart is the start of the text, not the start of the constant pool and |
| 630 | // other per-function data. |
| 631 | unsigned char *FnStart = |
| 632 | (unsigned char *)TheJIT->getPointerToGlobalIfAvailable(F.getFunction()); |
| 633 | unsigned char *FnEnd = CurBufferPtr; |
| 634 | |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 635 | MemMgr->endFunctionBody(F.getFunction(), BufferBegin, FnEnd); |
Chris Lattner | a827953 | 2006-06-16 18:09:26 +0000 | [diff] [blame] | 636 | NumBytes += FnEnd-FnStart; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 637 | |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 638 | if (!Relocations.empty()) { |
Chris Lattner | e884dc2 | 2005-07-20 16:29:20 +0000 | [diff] [blame] | 639 | NumRelos += Relocations.size(); |
| 640 | |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 641 | // Resolve the relocations to concrete pointers. |
| 642 | for (unsigned i = 0, e = Relocations.size(); i != e; ++i) { |
| 643 | MachineRelocation &MR = Relocations[i]; |
| 644 | void *ResultPtr; |
Chris Lattner | d91ff7c | 2005-04-18 01:44:27 +0000 | [diff] [blame] | 645 | if (MR.isString()) { |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 646 | ResultPtr = TheJIT->getPointerToNamedFunction(MR.getString()); |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 647 | |
Chris Lattner | d91ff7c | 2005-04-18 01:44:27 +0000 | [diff] [blame] | 648 | // 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] | 649 | if (!MR.doesntNeedStub()) |
Chris Lattner | e748401 | 2007-02-24 02:57:03 +0000 | [diff] [blame] | 650 | ResultPtr = Resolver.getExternalFunctionStub(ResultPtr); |
Chris Lattner | d2d5c76 | 2006-05-03 18:55:56 +0000 | [diff] [blame] | 651 | } else if (MR.isGlobalValue()) { |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 652 | ResultPtr = getPointerToGlobal(MR.getGlobalValue(), |
Chris Lattner | 43b429b | 2006-05-02 18:27:26 +0000 | [diff] [blame] | 653 | BufferBegin+MR.getMachineCodeOffset(), |
Evan Cheng | 02aabbf | 2008-01-03 02:56:28 +0000 | [diff] [blame] | 654 | MR.doesntNeedStub()); |
Evan Cheng | be8c03f | 2008-01-04 10:46:51 +0000 | [diff] [blame] | 655 | } else if (MR.isGlobalValueLazyPtr()) { |
| 656 | ResultPtr = getPointerToGVLazyPtr(MR.getGlobalValue(), |
| 657 | BufferBegin+MR.getMachineCodeOffset(), |
| 658 | MR.doesntNeedStub()); |
Evan Cheng | f141cc4 | 2006-07-27 18:21:10 +0000 | [diff] [blame] | 659 | } else if (MR.isBasicBlock()) { |
| 660 | ResultPtr = (void*)getMachineBasicBlockAddress(MR.getBasicBlock()); |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 661 | } else if (MR.isConstantPoolIndex()) { |
Chris Lattner | d2d5c76 | 2006-05-03 18:55:56 +0000 | [diff] [blame] | 662 | ResultPtr=(void*)getConstantPoolEntryAddress(MR.getConstantPoolIndex()); |
Evan Cheng | 52b510b | 2006-06-23 01:02:37 +0000 | [diff] [blame] | 663 | } else { |
| 664 | assert(MR.isJumpTableIndex()); |
| 665 | ResultPtr=(void*)getJumpTableEntryAddress(MR.getJumpTableIndex()); |
Chris Lattner | d2d5c76 | 2006-05-03 18:55:56 +0000 | [diff] [blame] | 666 | } |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 667 | |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 668 | MR.setResultPointer(ResultPtr); |
Andrew Lenharth | 16ec33c | 2005-07-22 20:48:12 +0000 | [diff] [blame] | 669 | |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 670 | // if we are managing the GOT and the relocation wants an index, |
| 671 | // give it one |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 672 | if (MR.isGOTRelative() && MemMgr->isManagingGOT()) { |
Chris Lattner | e748401 | 2007-02-24 02:57:03 +0000 | [diff] [blame] | 673 | unsigned idx = Resolver.getGOTIndexForAddr(ResultPtr); |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 674 | MR.setGOTIndex(idx); |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 675 | if (((void**)MemMgr->getGOTBase())[idx] != ResultPtr) { |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 676 | DOUT << "GOT was out of date for " << ResultPtr |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 677 | << " pointing at " << ((void**)MemMgr->getGOTBase())[idx] |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 678 | << "\n"; |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 679 | ((void**)MemMgr->getGOTBase())[idx] = ResultPtr; |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 680 | } |
Andrew Lenharth | 16ec33c | 2005-07-22 20:48:12 +0000 | [diff] [blame] | 681 | } |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 682 | } |
| 683 | |
Chris Lattner | 43b429b | 2006-05-02 18:27:26 +0000 | [diff] [blame] | 684 | TheJIT->getJITInfo().relocate(BufferBegin, &Relocations[0], |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 685 | Relocations.size(), MemMgr->getGOTBase()); |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 686 | } |
| 687 | |
Chris Lattner | d2d5c76 | 2006-05-03 18:55:56 +0000 | [diff] [blame] | 688 | // Update the GOT entry for F to point to the new code. |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 689 | if (MemMgr->isManagingGOT()) { |
Chris Lattner | e748401 | 2007-02-24 02:57:03 +0000 | [diff] [blame] | 690 | unsigned idx = Resolver.getGOTIndexForAddr((void*)BufferBegin); |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 691 | if (((void**)MemMgr->getGOTBase())[idx] != (void*)BufferBegin) { |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 692 | DOUT << "GOT was out of date for " << (void*)BufferBegin |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 693 | << " pointing at " << ((void**)MemMgr->getGOTBase())[idx] << "\n"; |
| 694 | ((void**)MemMgr->getGOTBase())[idx] = (void*)BufferBegin; |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 695 | } |
| 696 | } |
| 697 | |
Evan Cheng | 55fc280 | 2006-07-25 20:40:54 +0000 | [diff] [blame] | 698 | // Invalidate the icache if necessary. |
Evan Cheng | 55b5053 | 2006-07-27 06:33:55 +0000 | [diff] [blame] | 699 | synchronizeICache(FnStart, FnEnd-FnStart); |
Chris Lattner | 8ac66c1 | 2008-04-04 05:51:42 +0000 | [diff] [blame] | 700 | |
| 701 | // Add it to the JIT symbol table if the host wants it. |
| 702 | AddFunctionToSymbolTable(F.getFunction()->getNameStart(), |
| 703 | FnStart, FnEnd-FnStart); |
Evan Cheng | 55fc280 | 2006-07-25 20:40:54 +0000 | [diff] [blame] | 704 | |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 705 | DOUT << "JIT: Finished CodeGen of [" << (void*)FnStart |
| 706 | << "] Function: " << F.getFunction()->getName() |
| 707 | << ": " << (FnEnd-FnStart) << " bytes of text, " |
| 708 | << Relocations.size() << " relocations\n"; |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 709 | Relocations.clear(); |
Anton Korobeynikov | 8cd4c3e | 2007-01-19 17:25:17 +0000 | [diff] [blame] | 710 | |
Chris Lattner | c5633c2 | 2007-01-20 20:51:43 +0000 | [diff] [blame] | 711 | #ifndef NDEBUG |
Anton Korobeynikov | c6551ff | 2007-03-06 05:32:48 +0000 | [diff] [blame] | 712 | if (sys::hasDisassembler()) |
| 713 | DOUT << "Disassembled code:\n" |
| 714 | << sys::disassembleBuffer(FnStart, FnEnd-FnStart, (uintptr_t)FnStart); |
Chris Lattner | c5633c2 | 2007-01-20 20:51:43 +0000 | [diff] [blame] | 715 | #endif |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 716 | if (ExceptionHandling) { |
| 717 | uintptr_t ActualSize; |
| 718 | SavedBufferBegin = BufferBegin; |
| 719 | SavedBufferEnd = BufferEnd; |
| 720 | SavedCurBufferPtr = CurBufferPtr; |
| 721 | |
| 722 | BufferBegin = CurBufferPtr = MemMgr->startExceptionTable(F.getFunction(), |
| 723 | ActualSize); |
| 724 | BufferEnd = BufferBegin+ActualSize; |
| 725 | unsigned char* FrameRegister = DE->EmitDwarfTable(F, *this, FnStart, FnEnd); |
Chris Lattner | 0fdaa0b | 2008-03-07 20:05:43 +0000 | [diff] [blame] | 726 | MemMgr->endExceptionTable(F.getFunction(), BufferBegin, CurBufferPtr, |
| 727 | FrameRegister); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 728 | BufferBegin = SavedBufferBegin; |
| 729 | BufferEnd = SavedBufferEnd; |
| 730 | CurBufferPtr = SavedCurBufferPtr; |
| 731 | |
| 732 | TheJIT->RegisterTable(FrameRegister); |
| 733 | } |
| 734 | MMI->EndFunction(); |
| 735 | |
Chris Lattner | 43b429b | 2006-05-02 18:27:26 +0000 | [diff] [blame] | 736 | return false; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 737 | } |
| 738 | |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 739 | void JITEmitter::emitConstantPool(MachineConstantPool *MCP) { |
Chris Lattner | fa77d43 | 2006-02-09 04:22:52 +0000 | [diff] [blame] | 740 | const std::vector<MachineConstantPoolEntry> &Constants = MCP->getConstants(); |
Chris Lattner | 2c0a6a1 | 2003-11-30 04:23:21 +0000 | [diff] [blame] | 741 | if (Constants.empty()) return; |
| 742 | |
Evan Cheng | cd5731d | 2006-09-12 20:59:59 +0000 | [diff] [blame] | 743 | MachineConstantPoolEntry CPE = Constants.back(); |
| 744 | unsigned Size = CPE.Offset; |
| 745 | const Type *Ty = CPE.isMachineConstantPoolEntry() |
Chris Lattner | 8a65009 | 2006-09-13 16:21:10 +0000 | [diff] [blame] | 746 | ? CPE.Val.MachineCPVal->getType() : CPE.Val.ConstVal->getType(); |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 747 | Size += TheJIT->getTargetData()->getABITypeSize(Ty); |
Chris Lattner | 2c0a6a1 | 2003-11-30 04:23:21 +0000 | [diff] [blame] | 748 | |
Evan Cheng | 97b8c40 | 2008-04-12 00:22:01 +0000 | [diff] [blame] | 749 | unsigned Align = 1 << MCP->getConstantPoolAlignment(); |
| 750 | ConstantPoolBase = allocateSpace(Size, Align); |
Chris Lattner | 239862c | 2006-02-09 04:49:59 +0000 | [diff] [blame] | 751 | ConstantPool = MCP; |
Chris Lattner | f75f9be | 2006-05-02 23:22:24 +0000 | [diff] [blame] | 752 | |
| 753 | if (ConstantPoolBase == 0) return; // Buffer overflow. |
| 754 | |
Evan Cheng | 97b8c40 | 2008-04-12 00:22:01 +0000 | [diff] [blame] | 755 | DOUT << "JIT: Emitted constant pool at [" << ConstantPoolBase |
| 756 | << "] (size: " << Size << ", alignment: " << Align << ")\n"; |
| 757 | |
Chris Lattner | 239862c | 2006-02-09 04:49:59 +0000 | [diff] [blame] | 758 | // Initialize the memory for all of the constant pool entries. |
Chris Lattner | 3029f92 | 2006-02-09 04:46:04 +0000 | [diff] [blame] | 759 | for (unsigned i = 0, e = Constants.size(); i != e; ++i) { |
Chris Lattner | 239862c | 2006-02-09 04:49:59 +0000 | [diff] [blame] | 760 | void *CAddr = (char*)ConstantPoolBase+Constants[i].Offset; |
Evan Cheng | cd5731d | 2006-09-12 20:59:59 +0000 | [diff] [blame] | 761 | if (Constants[i].isMachineConstantPoolEntry()) { |
| 762 | // FIXME: add support to lower machine constant pool values into bytes! |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 763 | cerr << "Initialize memory with machine specific constant pool entry" |
| 764 | << " has not been implemented!\n"; |
Evan Cheng | cd5731d | 2006-09-12 20:59:59 +0000 | [diff] [blame] | 765 | abort(); |
| 766 | } |
| 767 | TheJIT->InitializeMemory(Constants[i].Val.ConstVal, CAddr); |
Evan Cheng | 97b8c40 | 2008-04-12 00:22:01 +0000 | [diff] [blame] | 768 | DOUT << "JIT: CP" << i << " at [" << CAddr << "]\n"; |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 769 | } |
| 770 | } |
| 771 | |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 772 | void JITEmitter::initJumpTableInfo(MachineJumpTableInfo *MJTI) { |
| 773 | const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables(); |
| 774 | if (JT.empty()) return; |
| 775 | |
Chris Lattner | f75f9be | 2006-05-02 23:22:24 +0000 | [diff] [blame] | 776 | unsigned NumEntries = 0; |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 777 | for (unsigned i = 0, e = JT.size(); i != e; ++i) |
Chris Lattner | f75f9be | 2006-05-02 23:22:24 +0000 | [diff] [blame] | 778 | NumEntries += JT[i].MBBs.size(); |
| 779 | |
| 780 | unsigned EntrySize = MJTI->getEntrySize(); |
| 781 | |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 782 | // Just allocate space for all the jump tables now. We will fix up the actual |
| 783 | // MBB entries in the tables after we emit the code for each block, since then |
| 784 | // we will know the final locations of the MBBs in memory. |
| 785 | JumpTable = MJTI; |
Chris Lattner | f75f9be | 2006-05-02 23:22:24 +0000 | [diff] [blame] | 786 | JumpTableBase = allocateSpace(NumEntries * EntrySize, MJTI->getAlignment()); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 787 | } |
| 788 | |
Jim Laskey | b92767a | 2006-12-14 22:53:42 +0000 | [diff] [blame] | 789 | void JITEmitter::emitJumpTableInfo(MachineJumpTableInfo *MJTI) { |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 790 | const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables(); |
Chris Lattner | f75f9be | 2006-05-02 23:22:24 +0000 | [diff] [blame] | 791 | if (JT.empty() || JumpTableBase == 0) return; |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 792 | |
Jim Laskey | b92767a | 2006-12-14 22:53:42 +0000 | [diff] [blame] | 793 | if (TargetMachine::getRelocationModel() == Reloc::PIC_) { |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 794 | assert(MJTI->getEntrySize() == 4 && "Cross JIT'ing?"); |
| 795 | // For each jump table, place the offset from the beginning of the table |
| 796 | // to the target address. |
| 797 | int *SlotPtr = (int*)JumpTableBase; |
Chris Lattner | 32ca55f | 2006-05-03 00:13:06 +0000 | [diff] [blame] | 798 | |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 799 | for (unsigned i = 0, e = JT.size(); i != e; ++i) { |
| 800 | const std::vector<MachineBasicBlock*> &MBBs = JT[i].MBBs; |
| 801 | // Store the offset of the basic block for this jump table slot in the |
| 802 | // memory we allocated for the jump table in 'initJumpTableInfo' |
| 803 | intptr_t Base = (intptr_t)SlotPtr; |
Evan Cheng | 2a3e08b | 2008-01-05 02:26:58 +0000 | [diff] [blame] | 804 | for (unsigned mi = 0, me = MBBs.size(); mi != me; ++mi) { |
| 805 | intptr_t MBBAddr = getMachineBasicBlockAddress(MBBs[mi]); |
| 806 | *SlotPtr++ = TheJIT->getJITInfo().getPICJumpTableEntry(MBBAddr, Base); |
| 807 | } |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 808 | } |
| 809 | } else { |
| 810 | assert(MJTI->getEntrySize() == sizeof(void*) && "Cross JIT'ing?"); |
| 811 | |
| 812 | // For each jump table, map each target in the jump table to the address of |
| 813 | // an emitted MachineBasicBlock. |
| 814 | intptr_t *SlotPtr = (intptr_t*)JumpTableBase; |
| 815 | |
| 816 | for (unsigned i = 0, e = JT.size(); i != e; ++i) { |
| 817 | const std::vector<MachineBasicBlock*> &MBBs = JT[i].MBBs; |
| 818 | // Store the address of the basic block for this jump table slot in the |
| 819 | // memory we allocated for the jump table in 'initJumpTableInfo' |
| 820 | for (unsigned mi = 0, me = MBBs.size(); mi != me; ++mi) |
| 821 | *SlotPtr++ = getMachineBasicBlockAddress(MBBs[mi]); |
| 822 | } |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 823 | } |
| 824 | } |
| 825 | |
Nicolas Geoffray | 51cc3c1 | 2008-04-16 20:46:05 +0000 | [diff] [blame^] | 826 | void JITEmitter::startFunctionStub(const GlobalValue* F, unsigned StubSize, |
| 827 | unsigned Alignment) { |
Chris Lattner | 43b429b | 2006-05-02 18:27:26 +0000 | [diff] [blame] | 828 | SavedBufferBegin = BufferBegin; |
| 829 | SavedBufferEnd = BufferEnd; |
| 830 | SavedCurBufferPtr = CurBufferPtr; |
| 831 | |
Nicolas Geoffray | 51cc3c1 | 2008-04-16 20:46:05 +0000 | [diff] [blame^] | 832 | BufferBegin = CurBufferPtr = MemMgr->allocateStub(F, StubSize, Alignment); |
Chris Lattner | 43b429b | 2006-05-02 18:27:26 +0000 | [diff] [blame] | 833 | BufferEnd = BufferBegin+StubSize+1; |
Chris Lattner | 6125fdd | 2003-05-09 03:30:07 +0000 | [diff] [blame] | 834 | } |
| 835 | |
Nicolas Geoffray | 51cc3c1 | 2008-04-16 20:46:05 +0000 | [diff] [blame^] | 836 | void *JITEmitter::finishFunctionStub(const GlobalValue* F) { |
Chris Lattner | 43b429b | 2006-05-02 18:27:26 +0000 | [diff] [blame] | 837 | NumBytes += getCurrentPCOffset(); |
| 838 | std::swap(SavedBufferBegin, BufferBegin); |
| 839 | BufferEnd = SavedBufferEnd; |
| 840 | CurBufferPtr = SavedCurBufferPtr; |
| 841 | return SavedBufferBegin; |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 842 | } |
| 843 | |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 844 | // getConstantPoolEntryAddress - Return the address of the 'ConstantNum' entry |
| 845 | // in the constant pool that was last emitted with the 'emitConstantPool' |
| 846 | // method. |
| 847 | // |
Chris Lattner | b4432f3 | 2006-05-03 17:10:41 +0000 | [diff] [blame] | 848 | intptr_t JITEmitter::getConstantPoolEntryAddress(unsigned ConstantNum) const { |
Chris Lattner | 239862c | 2006-02-09 04:49:59 +0000 | [diff] [blame] | 849 | assert(ConstantNum < ConstantPool->getConstants().size() && |
Misha Brukman | 3c94497 | 2005-04-22 04:08:30 +0000 | [diff] [blame] | 850 | "Invalid ConstantPoolIndex!"); |
Chris Lattner | 239862c | 2006-02-09 04:49:59 +0000 | [diff] [blame] | 851 | return (intptr_t)ConstantPoolBase + |
| 852 | ConstantPool->getConstants()[ConstantNum].Offset; |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 853 | } |
| 854 | |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 855 | // getJumpTableEntryAddress - Return the address of the JumpTable with index |
| 856 | // 'Index' in the jumpp table that was last initialized with 'initJumpTableInfo' |
| 857 | // |
Chris Lattner | b4432f3 | 2006-05-03 17:10:41 +0000 | [diff] [blame] | 858 | intptr_t JITEmitter::getJumpTableEntryAddress(unsigned Index) const { |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 859 | const std::vector<MachineJumpTableEntry> &JT = JumpTable->getJumpTables(); |
| 860 | assert(Index < JT.size() && "Invalid jump table index!"); |
| 861 | |
| 862 | unsigned Offset = 0; |
| 863 | unsigned EntrySize = JumpTable->getEntrySize(); |
| 864 | |
| 865 | for (unsigned i = 0; i < Index; ++i) |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 866 | Offset += JT[i].MBBs.size(); |
| 867 | |
| 868 | Offset *= EntrySize; |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 869 | |
Nate Begeman | c34b227 | 2006-04-25 17:46:32 +0000 | [diff] [blame] | 870 | return (intptr_t)((char *)JumpTableBase + Offset); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 871 | } |
| 872 | |
Chris Lattner | e993cc2 | 2006-05-11 23:08:08 +0000 | [diff] [blame] | 873 | //===----------------------------------------------------------------------===// |
| 874 | // Public interface to this file |
| 875 | //===----------------------------------------------------------------------===// |
| 876 | |
Chris Lattner | 9f2f142 | 2007-12-06 01:08:09 +0000 | [diff] [blame] | 877 | MachineCodeEmitter *JIT::createEmitter(JIT &jit, JITMemoryManager *JMM) { |
| 878 | return new JITEmitter(jit, JMM); |
Chris Lattner | e993cc2 | 2006-05-11 23:08:08 +0000 | [diff] [blame] | 879 | } |
| 880 | |
Misha Brukman | d69c1e6 | 2003-07-28 19:09:06 +0000 | [diff] [blame] | 881 | // getPointerToNamedFunction - This function is used as a global wrapper to |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 882 | // JIT::getPointerToNamedFunction for the purpose of resolving symbols when |
Misha Brukman | d69c1e6 | 2003-07-28 19:09:06 +0000 | [diff] [blame] | 883 | // bugpoint is debugging the JIT. In that scenario, we are loading an .so and |
| 884 | // need to resolve function(s) that are being mis-codegenerated, so we need to |
| 885 | // resolve their addresses at runtime, and this is the way to do it. |
| 886 | extern "C" { |
| 887 | void *getPointerToNamedFunction(const char *Name) { |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 888 | if (Function *F = TheJIT->FindFunctionNamed(Name)) |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 889 | return TheJIT->getPointerToFunction(F); |
| 890 | return TheJIT->getPointerToNamedFunction(Name); |
Misha Brukman | d69c1e6 | 2003-07-28 19:09:06 +0000 | [diff] [blame] | 891 | } |
| 892 | } |
Chris Lattner | e993cc2 | 2006-05-11 23:08:08 +0000 | [diff] [blame] | 893 | |
| 894 | // getPointerToFunctionOrStub - If the specified function has been |
| 895 | // code-gen'd, return a pointer to the function. If not, compile it, or use |
| 896 | // a stub to implement lazy compilation if available. |
| 897 | // |
| 898 | void *JIT::getPointerToFunctionOrStub(Function *F) { |
| 899 | // If we have already code generated the function, just return the address. |
| 900 | if (void *Addr = getPointerToGlobalIfAvailable(F)) |
| 901 | return Addr; |
| 902 | |
Chris Lattner | e748401 | 2007-02-24 02:57:03 +0000 | [diff] [blame] | 903 | // Get a stub if the target supports it. |
| 904 | assert(dynamic_cast<JITEmitter*>(MCE) && "Unexpected MCE?"); |
| 905 | JITEmitter *JE = static_cast<JITEmitter*>(getCodeEmitter()); |
| 906 | return JE->getJITResolver().getFunctionStub(F); |
Chris Lattner | e993cc2 | 2006-05-11 23:08:08 +0000 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | /// freeMachineCodeForFunction - release machine code memory for given Function. |
| 910 | /// |
| 911 | void JIT::freeMachineCodeForFunction(Function *F) { |
Chris Lattner | 8ac66c1 | 2008-04-04 05:51:42 +0000 | [diff] [blame] | 912 | |
Chris Lattner | e993cc2 | 2006-05-11 23:08:08 +0000 | [diff] [blame] | 913 | // Delete translation for this from the ExecutionEngine, so it will get |
| 914 | // retranslated next time it is used. |
Chris Lattner | 8ac66c1 | 2008-04-04 05:51:42 +0000 | [diff] [blame] | 915 | void *OldPtr = updateGlobalMapping(F, 0); |
| 916 | |
| 917 | if (OldPtr) |
| 918 | RemoveFunctionFromSymbolTable(OldPtr); |
Chris Lattner | e993cc2 | 2006-05-11 23:08:08 +0000 | [diff] [blame] | 919 | |
| 920 | // Free the actual memory for the function body and related stuff. |
| 921 | assert(dynamic_cast<JITEmitter*>(MCE) && "Unexpected MCE?"); |
Chris Lattner | e748401 | 2007-02-24 02:57:03 +0000 | [diff] [blame] | 922 | static_cast<JITEmitter*>(MCE)->deallocateMemForFunction(F); |
Chris Lattner | e993cc2 | 2006-05-11 23:08:08 +0000 | [diff] [blame] | 923 | } |
| 924 | |