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