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