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