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