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