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