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