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