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