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