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