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 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source 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" |
Chris Lattner | 2c0a6a1 | 2003-11-30 04:23:21 +0000 | [diff] [blame] | 17 | #include "llvm/Constant.h" |
| 18 | #include "llvm/Module.h" |
Chris Lattner | 5b3a455 | 2005-03-17 15:38:16 +0000 | [diff] [blame] | 19 | #include "llvm/Type.h" |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineCodeEmitter.h" |
| 21 | #include "llvm/CodeGen/MachineFunction.h" |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineConstantPool.h" |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineRelocation.h" |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 25 | #include "llvm/ExecutionEngine/GenericValue.h" |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 27 | #include "llvm/Target/TargetJITInfo.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Debug.h" |
| 29 | #include "llvm/ADT/Statistic.h" |
Reid Spencer | 52b0ba6 | 2004-09-11 04:31:03 +0000 | [diff] [blame] | 30 | #include "llvm/System/Memory.h" |
Andrew Lenharth | a00269b | 2005-07-29 23:40:16 +0000 | [diff] [blame] | 31 | #include <algorithm> |
Chris Lattner | ca26180 | 2006-01-22 23:41:42 +0000 | [diff] [blame] | 32 | #include <iostream> |
Chris Lattner | c19aade | 2003-12-08 08:06:28 +0000 | [diff] [blame] | 33 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 34 | |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 35 | namespace { |
Chris Lattner | e738656 | 2003-10-20 05:45:49 +0000 | [diff] [blame] | 36 | Statistic<> NumBytes("jit", "Number of bytes of machine code compiled"); |
Chris Lattner | e884dc2 | 2005-07-20 16:29:20 +0000 | [diff] [blame] | 37 | Statistic<> NumRelos("jit", "Number of relocations applied"); |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 38 | JIT *TheJIT = 0; |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 39 | } |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 40 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 41 | |
| 42 | //===----------------------------------------------------------------------===// |
| 43 | // JITMemoryManager code. |
| 44 | // |
| 45 | namespace { |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 46 | /// JITMemoryManager - Manage memory for the JIT code generation in a logical, |
| 47 | /// sane way. This splits a large block of MAP_NORESERVE'd memory into two |
| 48 | /// sections, one for function stubs, one for the functions themselves. We |
| 49 | /// have to do this because we may need to emit a function stub while in the |
| 50 | /// middle of emitting a function, and we don't know how large the function we |
| 51 | /// are emitting is. This never bothers to release the memory, because when |
| 52 | /// we are ready to destroy the JIT, the program exits. |
| 53 | class JITMemoryManager { |
Chris Lattner | e6fdcbf | 2006-05-03 00:54:49 +0000 | [diff] [blame] | 54 | std::vector<sys::MemoryBlock> Blocks; // Memory blocks allocated by the JIT |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 55 | unsigned char *FunctionBase; // Start of the function body area |
Chris Lattner | f75f9be | 2006-05-02 23:22:24 +0000 | [diff] [blame] | 56 | unsigned char *CurStubPtr, *CurFunctionPtr; |
Chris Lattner | a726c7f | 2006-05-02 21:44:14 +0000 | [diff] [blame] | 57 | unsigned char *GOTBase; // Target Specific reserved memory |
Andrew Lenharth | a00269b | 2005-07-29 23:40:16 +0000 | [diff] [blame] | 58 | |
| 59 | // centralize memory block allocation |
| 60 | sys::MemoryBlock getNewMemoryBlock(unsigned size); |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 61 | public: |
Andrew Lenharth | 16ec33c | 2005-07-22 20:48:12 +0000 | [diff] [blame] | 62 | JITMemoryManager(bool useGOT); |
Reid Spencer | 4af3da6 | 2004-12-13 16:04:04 +0000 | [diff] [blame] | 63 | ~JITMemoryManager(); |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 64 | |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 65 | inline unsigned char *allocateStub(unsigned StubSize); |
| 66 | inline unsigned char *startFunctionBody(); |
Chris Lattner | 281a601 | 2005-01-10 18:23:22 +0000 | [diff] [blame] | 67 | inline void endFunctionBody(unsigned char *FunctionEnd); |
Chris Lattner | a726c7f | 2006-05-02 21:44:14 +0000 | [diff] [blame] | 68 | |
| 69 | unsigned char *getGOTBase() const { |
| 70 | return GOTBase; |
| 71 | } |
| 72 | bool isManagingGOT() const { |
| 73 | return GOTBase != NULL; |
| 74 | } |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 75 | }; |
| 76 | } |
| 77 | |
Andrew Lenharth | 16ec33c | 2005-07-22 20:48:12 +0000 | [diff] [blame] | 78 | JITMemoryManager::JITMemoryManager(bool useGOT) { |
Andrew Lenharth | a00269b | 2005-07-29 23:40:16 +0000 | [diff] [blame] | 79 | // Allocate a 16M block of memory for functions |
| 80 | sys::MemoryBlock FunBlock = getNewMemoryBlock(16 << 20); |
Andrew Lenharth | 16ec33c | 2005-07-22 20:48:12 +0000 | [diff] [blame] | 81 | |
Andrew Lenharth | a00269b | 2005-07-29 23:40:16 +0000 | [diff] [blame] | 82 | FunctionBase = reinterpret_cast<unsigned char*>(FunBlock.base()); |
Chris Lattner | 281a601 | 2005-01-10 18:23:22 +0000 | [diff] [blame] | 83 | |
Andrew Lenharth | a00269b | 2005-07-29 23:40:16 +0000 | [diff] [blame] | 84 | // Allocate stubs backwards from the base, allocate functions forward |
| 85 | // from the base. |
| 86 | CurStubPtr = CurFunctionPtr = FunctionBase + 512*1024;// Use 512k for stubs |
| 87 | |
Chris Lattner | f5d438c | 2006-05-02 21:57:51 +0000 | [diff] [blame] | 88 | // Allocate the GOT. |
Andrew Lenharth | 2b3b89c | 2005-08-01 17:35:40 +0000 | [diff] [blame] | 89 | GOTBase = NULL; |
Chris Lattner | f5d438c | 2006-05-02 21:57:51 +0000 | [diff] [blame] | 90 | if (useGOT) GOTBase = (unsigned char*)malloc(sizeof(void*) * 8192); |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Reid Spencer | 4af3da6 | 2004-12-13 16:04:04 +0000 | [diff] [blame] | 93 | JITMemoryManager::~JITMemoryManager() { |
Chris Lattner | e6fdcbf | 2006-05-03 00:54:49 +0000 | [diff] [blame] | 94 | for (unsigned i = 0, e = Blocks.size(); i != e; ++i) |
| 95 | sys::Memory::ReleaseRWX(Blocks[i]); |
Andrew Lenharth | a00269b | 2005-07-29 23:40:16 +0000 | [diff] [blame] | 96 | Blocks.clear(); |
Reid Spencer | 4af3da6 | 2004-12-13 16:04:04 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 99 | unsigned char *JITMemoryManager::allocateStub(unsigned StubSize) { |
| 100 | CurStubPtr -= StubSize; |
Andrew Lenharth | a00269b | 2005-07-29 23:40:16 +0000 | [diff] [blame] | 101 | if (CurStubPtr < FunctionBase) { |
Chris Lattner | a726c7f | 2006-05-02 21:44:14 +0000 | [diff] [blame] | 102 | // FIXME: allocate a new block |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 103 | std::cerr << "JIT ran out of memory for function stubs!\n"; |
| 104 | abort(); |
| 105 | } |
| 106 | return CurStubPtr; |
| 107 | } |
| 108 | |
| 109 | unsigned char *JITMemoryManager::startFunctionBody() { |
Chris Lattner | 0eb4d6b | 2006-05-03 01:03:20 +0000 | [diff] [blame] | 110 | return CurFunctionPtr; |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | void JITMemoryManager::endFunctionBody(unsigned char *FunctionEnd) { |
| 114 | assert(FunctionEnd > CurFunctionPtr); |
| 115 | CurFunctionPtr = FunctionEnd; |
| 116 | } |
| 117 | |
Andrew Lenharth | a00269b | 2005-07-29 23:40:16 +0000 | [diff] [blame] | 118 | sys::MemoryBlock JITMemoryManager::getNewMemoryBlock(unsigned size) { |
Andrew Lenharth | a00269b | 2005-07-29 23:40:16 +0000 | [diff] [blame] | 119 | try { |
Chris Lattner | e6fdcbf | 2006-05-03 00:54:49 +0000 | [diff] [blame] | 120 | const sys::MemoryBlock *BOld = Blocks.empty() ? 0 : &Blocks.front(); |
| 121 | sys::MemoryBlock B = sys::Memory::AllocateRWX(size, BOld); |
| 122 | Blocks.push_back(B); |
| 123 | return B; |
| 124 | } catch (std::string &err) { |
Andrew Lenharth | a00269b | 2005-07-29 23:40:16 +0000 | [diff] [blame] | 125 | std::cerr << "Allocation failed when allocating new memory in the JIT\n"; |
| 126 | std::cerr << err << "\n"; |
| 127 | abort(); |
| 128 | } |
Andrew Lenharth | a00269b | 2005-07-29 23:40:16 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 131 | //===----------------------------------------------------------------------===// |
| 132 | // JIT lazy compilation code. |
| 133 | // |
| 134 | namespace { |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 135 | class JITResolverState { |
| 136 | private: |
| 137 | /// FunctionToStubMap - Keep track of the stub created for a particular |
| 138 | /// function so that we can reuse them if necessary. |
| 139 | std::map<Function*, void*> FunctionToStubMap; |
| 140 | |
| 141 | /// StubToFunctionMap - Keep track of the function that each stub |
| 142 | /// corresponds to. |
| 143 | std::map<void*, Function*> StubToFunctionMap; |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 144 | |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 145 | public: |
| 146 | std::map<Function*, void*>& getFunctionToStubMap(const MutexGuard& locked) { |
| 147 | assert(locked.holds(TheJIT->lock)); |
| 148 | return FunctionToStubMap; |
| 149 | } |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 150 | |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 151 | std::map<void*, Function*>& getStubToFunctionMap(const MutexGuard& locked) { |
| 152 | assert(locked.holds(TheJIT->lock)); |
| 153 | return StubToFunctionMap; |
| 154 | } |
| 155 | }; |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 156 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 157 | /// JITResolver - Keep track of, and resolve, call sites for functions that |
| 158 | /// have not yet been compiled. |
| 159 | class JITResolver { |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 160 | /// MCE - The MachineCodeEmitter to use to emit stubs with. |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 161 | MachineCodeEmitter &MCE; |
| 162 | |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 163 | /// LazyResolverFn - The target lazy resolver function that we actually |
| 164 | /// rewrite instructions to use. |
| 165 | TargetJITInfo::LazyResolverFn LazyResolverFn; |
| 166 | |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 167 | JITResolverState state; |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 168 | |
Chris Lattner | d91ff7c | 2005-04-18 01:44:27 +0000 | [diff] [blame] | 169 | /// ExternalFnToStubMap - This is the equivalent of FunctionToStubMap for |
| 170 | /// external functions. |
| 171 | std::map<void*, void*> ExternalFnToStubMap; |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 172 | |
| 173 | //map addresses to indexes in the GOT |
| 174 | std::map<void*, unsigned> revGOTMap; |
| 175 | unsigned nextGOTIndex; |
| 176 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 177 | public: |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 178 | JITResolver(MachineCodeEmitter &mce) : MCE(mce), nextGOTIndex(0) { |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 179 | LazyResolverFn = |
| 180 | TheJIT->getJITInfo().getLazyResolverFunction(JITCompilerFn); |
| 181 | } |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 182 | |
| 183 | /// getFunctionStub - This returns a pointer to a function stub, creating |
| 184 | /// one on demand as needed. |
| 185 | void *getFunctionStub(Function *F); |
| 186 | |
Chris Lattner | d91ff7c | 2005-04-18 01:44:27 +0000 | [diff] [blame] | 187 | /// getExternalFunctionStub - Return a stub for the function at the |
| 188 | /// specified address, created lazily on demand. |
| 189 | void *getExternalFunctionStub(void *FnAddr); |
| 190 | |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 191 | /// AddCallbackAtLocation - If the target is capable of rewriting an |
| 192 | /// instruction without the use of a stub, record the location of the use so |
| 193 | /// we know which function is being used at the location. |
| 194 | void *AddCallbackAtLocation(Function *F, void *Location) { |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 195 | MutexGuard locked(TheJIT->lock); |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 196 | /// Get the target-specific JIT resolver function. |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 197 | state.getStubToFunctionMap(locked)[Location] = F; |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 198 | return (void*)LazyResolverFn; |
| 199 | } |
| 200 | |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 201 | /// getGOTIndexForAddress - Return a new or existing index in the GOT for |
| 202 | /// and address. This function only manages slots, it does not manage the |
| 203 | /// contents of the slots or the memory associated with the GOT. |
| 204 | unsigned getGOTIndexForAddr(void* addr); |
| 205 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 206 | /// JITCompilerFn - This function is called to resolve a stub to a compiled |
| 207 | /// address. If the LLVM Function corresponding to the stub has not yet |
| 208 | /// been compiled, this function compiles it first. |
| 209 | static void *JITCompilerFn(void *Stub); |
| 210 | }; |
| 211 | } |
| 212 | |
| 213 | /// getJITResolver - This function returns the one instance of the JIT resolver. |
| 214 | /// |
| 215 | static JITResolver &getJITResolver(MachineCodeEmitter *MCE = 0) { |
| 216 | static JITResolver TheJITResolver(*MCE); |
| 217 | return TheJITResolver; |
| 218 | } |
| 219 | |
| 220 | /// getFunctionStub - This returns a pointer to a function stub, creating |
| 221 | /// one on demand as needed. |
| 222 | void *JITResolver::getFunctionStub(Function *F) { |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 223 | MutexGuard locked(TheJIT->lock); |
| 224 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 225 | // If we already have a stub for this function, recycle it. |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 226 | void *&Stub = state.getFunctionToStubMap(locked)[F]; |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 227 | if (Stub) return Stub; |
| 228 | |
Chris Lattner | b43dbdc | 2004-11-22 07:24:43 +0000 | [diff] [blame] | 229 | // Call the lazy resolver function unless we already KNOW it is an external |
| 230 | // function, in which case we just skip the lazy resolution step. |
| 231 | void *Actual = (void*)LazyResolverFn; |
Chris Lattner | 6943570 | 2005-02-20 18:43:35 +0000 | [diff] [blame] | 232 | if (F->isExternal() && F->hasExternalLinkage()) |
Chris Lattner | b43dbdc | 2004-11-22 07:24:43 +0000 | [diff] [blame] | 233 | Actual = TheJIT->getPointerToFunction(F); |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 234 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 235 | // Otherwise, codegen a new stub. For now, the stub will call the lazy |
| 236 | // resolver function. |
Chris Lattner | b43dbdc | 2004-11-22 07:24:43 +0000 | [diff] [blame] | 237 | Stub = TheJIT->getJITInfo().emitFunctionStub(Actual, MCE); |
| 238 | |
Chris Lattner | 6943570 | 2005-02-20 18:43:35 +0000 | [diff] [blame] | 239 | if (Actual != (void*)LazyResolverFn) { |
Chris Lattner | b43dbdc | 2004-11-22 07:24:43 +0000 | [diff] [blame] | 240 | // If we are getting the stub for an external function, we really want the |
| 241 | // address of the stub in the GlobalAddressMap for the JIT, not the address |
| 242 | // of the external function. |
| 243 | TheJIT->updateGlobalMapping(F, Stub); |
| 244 | } |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 245 | |
Chris Lattner | cb47941 | 2004-11-21 03:44:32 +0000 | [diff] [blame] | 246 | DEBUG(std::cerr << "JIT: Stub emitted at [" << Stub << "] for function '" |
Chris Lattner | 6f71720 | 2004-11-22 21:48:33 +0000 | [diff] [blame] | 247 | << F->getName() << "'\n"); |
Chris Lattner | cb47941 | 2004-11-21 03:44:32 +0000 | [diff] [blame] | 248 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 249 | // Finally, keep track of the stub-to-Function mapping so that the |
| 250 | // JITCompilerFn knows which function to compile! |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 251 | state.getStubToFunctionMap(locked)[Stub] = F; |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 252 | return Stub; |
| 253 | } |
| 254 | |
Chris Lattner | d91ff7c | 2005-04-18 01:44:27 +0000 | [diff] [blame] | 255 | /// getExternalFunctionStub - Return a stub for the function at the |
| 256 | /// specified address, created lazily on demand. |
| 257 | void *JITResolver::getExternalFunctionStub(void *FnAddr) { |
| 258 | // If we already have a stub for this function, recycle it. |
| 259 | void *&Stub = ExternalFnToStubMap[FnAddr]; |
| 260 | if (Stub) return Stub; |
| 261 | |
| 262 | Stub = TheJIT->getJITInfo().emitFunctionStub(FnAddr, MCE); |
| 263 | DEBUG(std::cerr << "JIT: Stub emitted at [" << Stub |
| 264 | << "] for external function at '" << FnAddr << "'\n"); |
| 265 | return Stub; |
| 266 | } |
| 267 | |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 268 | unsigned JITResolver::getGOTIndexForAddr(void* addr) { |
| 269 | unsigned idx = revGOTMap[addr]; |
| 270 | if (!idx) { |
| 271 | idx = ++nextGOTIndex; |
| 272 | revGOTMap[addr] = idx; |
| 273 | DEBUG(std::cerr << "Adding GOT entry " << idx |
| 274 | << " for addr " << addr << "\n"); |
| 275 | // ((void**)MemMgr.getGOTBase())[idx] = addr; |
| 276 | } |
| 277 | return idx; |
| 278 | } |
Chris Lattner | d91ff7c | 2005-04-18 01:44:27 +0000 | [diff] [blame] | 279 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 280 | /// JITCompilerFn - This function is called when a lazy compilation stub has |
| 281 | /// been entered. It looks up which function this stub corresponds to, compiles |
| 282 | /// it if necessary, then returns the resultant function pointer. |
| 283 | void *JITResolver::JITCompilerFn(void *Stub) { |
| 284 | JITResolver &JR = getJITResolver(); |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 285 | |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 286 | MutexGuard locked(TheJIT->lock); |
| 287 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 288 | // The address given to us for the stub may not be exactly right, it might be |
| 289 | // a little bit after the stub. As such, use upper_bound to find it. |
| 290 | std::map<void*, Function*>::iterator I = |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 291 | JR.state.getStubToFunctionMap(locked).upper_bound(Stub); |
Chris Lattner | 2199877 | 2006-01-07 06:20:51 +0000 | [diff] [blame] | 292 | assert(I != JR.state.getStubToFunctionMap(locked).begin() && |
| 293 | "This is not a known stub!"); |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 294 | Function *F = (--I)->second; |
| 295 | |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 296 | // We might like to remove the stub from the StubToFunction map. |
| 297 | // We can't do that! Multiple threads could be stuck, waiting to acquire the |
| 298 | // lock above. As soon as the 1st function finishes compiling the function, |
Chris Lattner | 2199877 | 2006-01-07 06:20:51 +0000 | [diff] [blame] | 299 | // the next one will be released, and needs to be able to find the function it |
| 300 | // needs to call. |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 301 | //JR.state.getStubToFunctionMap(locked).erase(I); |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 302 | |
Chris Lattner | cb47941 | 2004-11-21 03:44:32 +0000 | [diff] [blame] | 303 | DEBUG(std::cerr << "JIT: Lazily resolving function '" << F->getName() |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 304 | << "' In stub ptr = " << Stub << " actual ptr = " |
| 305 | << I->first << "\n"); |
| 306 | |
| 307 | void *Result = TheJIT->getPointerToFunction(F); |
| 308 | |
| 309 | // 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] | 310 | JR.state.getFunctionToStubMap(locked).erase(F); |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 311 | |
| 312 | // 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] | 313 | |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 314 | // What we will do is set the compiled function address to map to the |
| 315 | // 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] | 316 | // if they see it still using the stub address. |
| 317 | // Note: this is done so the Resolver doesn't have to manage GOT memory |
| 318 | // Do this without allocating map space if the target isn't using a GOT |
| 319 | if(JR.revGOTMap.find(Stub) != JR.revGOTMap.end()) |
| 320 | JR.revGOTMap[Result] = JR.revGOTMap[Stub]; |
| 321 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 322 | return Result; |
| 323 | } |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 324 | |
| 325 | |
Chris Lattner | e518b71 | 2004-12-05 07:19:16 +0000 | [diff] [blame] | 326 | // getPointerToFunctionOrStub - If the specified function has been |
| 327 | // code-gen'd, return a pointer to the function. If not, compile it, or use |
| 328 | // a stub to implement lazy compilation if available. |
| 329 | // |
| 330 | void *JIT::getPointerToFunctionOrStub(Function *F) { |
| 331 | // If we have already code generated the function, just return the address. |
| 332 | if (void *Addr = getPointerToGlobalIfAvailable(F)) |
| 333 | return Addr; |
| 334 | |
| 335 | // Get a stub if the target supports it |
| 336 | return getJITResolver(MCE).getFunctionStub(F); |
| 337 | } |
| 338 | |
| 339 | |
| 340 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 341 | //===----------------------------------------------------------------------===// |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 342 | // JITEmitter code. |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 343 | // |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 344 | namespace { |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 345 | /// JITEmitter - The JIT implementation of the MachineCodeEmitter, which is |
| 346 | /// used to output functions to memory for execution. |
| 347 | class JITEmitter : public MachineCodeEmitter { |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 348 | JITMemoryManager MemMgr; |
| 349 | |
Chris Lattner | 6125fdd | 2003-05-09 03:30:07 +0000 | [diff] [blame] | 350 | // 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] | 351 | // save BufferBegin/BufferEnd/CurBufferPtr here. |
| 352 | unsigned char *SavedBufferBegin, *SavedBufferEnd, *SavedCurBufferPtr; |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 353 | |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 354 | /// Relocations - These are the relocations that the function needs, as |
| 355 | /// emitted. |
| 356 | std::vector<MachineRelocation> Relocations; |
Chris Lattner | b4432f3 | 2006-05-03 17:10:41 +0000 | [diff] [blame] | 357 | |
| 358 | /// MBBLocations - This vector is a mapping from MBB ID's to their address. |
| 359 | /// It is filled in by the StartMachineBasicBlock callback and queried by |
| 360 | /// the getMachineBasicBlockAddress callback. |
| 361 | std::vector<intptr_t> MBBLocations; |
Andrew Lenharth | 16ec33c | 2005-07-22 20:48:12 +0000 | [diff] [blame] | 362 | |
Chris Lattner | 239862c | 2006-02-09 04:49:59 +0000 | [diff] [blame] | 363 | /// ConstantPool - The constant pool for the current function. |
| 364 | /// |
| 365 | MachineConstantPool *ConstantPool; |
| 366 | |
| 367 | /// ConstantPoolBase - A pointer to the first entry in the constant pool. |
| 368 | /// |
| 369 | void *ConstantPoolBase; |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 370 | |
| 371 | /// ConstantPool - The constant pool for the current function. |
| 372 | /// |
| 373 | MachineJumpTableInfo *JumpTable; |
| 374 | |
| 375 | /// JumpTableBase - A pointer to the first entry in the jump table. |
| 376 | /// |
| 377 | void *JumpTableBase; |
| 378 | public: |
Chris Lattner | 239862c | 2006-02-09 04:49:59 +0000 | [diff] [blame] | 379 | JITEmitter(JIT &jit) : MemMgr(jit.getJITInfo().needsGOT()) { |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 380 | TheJIT = &jit; |
Chris Lattner | a726c7f | 2006-05-02 21:44:14 +0000 | [diff] [blame] | 381 | DEBUG(if (MemMgr.isManagingGOT()) std::cerr << "JIT is managing a GOT\n"); |
Andrew Lenharth | 16ec33c | 2005-07-22 20:48:12 +0000 | [diff] [blame] | 382 | } |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 383 | |
| 384 | virtual void startFunction(MachineFunction &F); |
Chris Lattner | 43b429b | 2006-05-02 18:27:26 +0000 | [diff] [blame] | 385 | virtual bool finishFunction(MachineFunction &F); |
Chris Lattner | f75f9be | 2006-05-02 23:22:24 +0000 | [diff] [blame] | 386 | |
| 387 | void emitConstantPool(MachineConstantPool *MCP); |
| 388 | void initJumpTableInfo(MachineJumpTableInfo *MJTI); |
Chris Lattner | b4432f3 | 2006-05-03 17:10:41 +0000 | [diff] [blame] | 389 | void emitJumpTableInfo(MachineJumpTableInfo *MJTI); |
Chris Lattner | f75f9be | 2006-05-02 23:22:24 +0000 | [diff] [blame] | 390 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 391 | virtual void startFunctionStub(unsigned StubSize); |
| 392 | virtual void* finishFunctionStub(const Function *F); |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 393 | |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 394 | virtual void addRelocation(const MachineRelocation &MR) { |
| 395 | Relocations.push_back(MR); |
| 396 | } |
Chris Lattner | b4432f3 | 2006-05-03 17:10:41 +0000 | [diff] [blame] | 397 | |
| 398 | virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) { |
| 399 | if (MBBLocations.size() <= (unsigned)MBB->getNumber()) |
| 400 | MBBLocations.resize((MBB->getNumber()+1)*2); |
| 401 | MBBLocations[MBB->getNumber()] = getCurrentPCValue(); |
| 402 | } |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 403 | |
Chris Lattner | b4432f3 | 2006-05-03 17:10:41 +0000 | [diff] [blame] | 404 | virtual intptr_t getConstantPoolEntryAddress(unsigned Entry) const; |
| 405 | virtual intptr_t getJumpTableEntryAddress(unsigned Entry) const; |
| 406 | |
| 407 | virtual intptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const { |
| 408 | assert(MBBLocations.size() > (unsigned)MBB->getNumber() && |
| 409 | MBBLocations[MBB->getNumber()] && "MBB not emitted!"); |
| 410 | return MBBLocations[MBB->getNumber()]; |
| 411 | } |
| 412 | |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 413 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 414 | private: |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 415 | void *getPointerToGlobal(GlobalValue *GV, void *Reference, bool NoNeedStub); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 416 | }; |
| 417 | } |
| 418 | |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 419 | MachineCodeEmitter *JIT::createEmitter(JIT &jit) { |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 420 | return new JITEmitter(jit); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 421 | } |
| 422 | |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 423 | void *JITEmitter::getPointerToGlobal(GlobalValue *V, void *Reference, |
| 424 | bool DoesntNeedStub) { |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 425 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) { |
| 426 | /// FIXME: If we straightened things out, this could actually emit the |
| 427 | /// global immediately instead of queuing it for codegen later! |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 428 | return TheJIT->getOrEmitGlobalVariable(GV); |
| 429 | } |
| 430 | |
| 431 | // If we have already compiled the function, return a pointer to its body. |
| 432 | Function *F = cast<Function>(V); |
| 433 | void *ResultPtr = TheJIT->getPointerToGlobalIfAvailable(F); |
| 434 | if (ResultPtr) return ResultPtr; |
| 435 | |
Chris Lattner | 532343b | 2004-11-30 17:41:49 +0000 | [diff] [blame] | 436 | if (F->hasExternalLinkage() && F->isExternal()) { |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 437 | // If this is an external function pointer, we can force the JIT to |
| 438 | // 'compile' it, which really just adds it to the map. |
Chris Lattner | b43dbdc | 2004-11-22 07:24:43 +0000 | [diff] [blame] | 439 | if (DoesntNeedStub) |
| 440 | return TheJIT->getPointerToFunction(F); |
| 441 | |
| 442 | return getJITResolver(this).getFunctionStub(F); |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 443 | } |
| 444 | |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 445 | // Okay, the function has not been compiled yet, if the target callback |
| 446 | // mechanism is capable of rewriting the instruction directly, prefer to do |
| 447 | // that instead of emitting a stub. |
| 448 | if (DoesntNeedStub) |
| 449 | return getJITResolver(this).AddCallbackAtLocation(F, Reference); |
| 450 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 451 | // Otherwise, we have to emit a lazy resolving stub. |
| 452 | return getJITResolver(this).getFunctionStub(F); |
| 453 | } |
| 454 | |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 455 | void JITEmitter::startFunction(MachineFunction &F) { |
Chris Lattner | 43b429b | 2006-05-02 18:27:26 +0000 | [diff] [blame] | 456 | BufferBegin = CurBufferPtr = MemMgr.startFunctionBody(); |
Chris Lattner | 43b429b | 2006-05-02 18:27:26 +0000 | [diff] [blame] | 457 | |
| 458 | /// FIXME: implement out of space handling correctly! |
| 459 | BufferEnd = (unsigned char*)(intptr_t)~0ULL; |
Chris Lattner | f75f9be | 2006-05-02 23:22:24 +0000 | [diff] [blame] | 460 | |
| 461 | emitConstantPool(F.getConstantPool()); |
| 462 | initJumpTableInfo(F.getJumpTableInfo()); |
| 463 | |
| 464 | // About to start emitting the machine code for the function. |
Chris Lattner | 0eb4d6b | 2006-05-03 01:03:20 +0000 | [diff] [blame] | 465 | emitAlignment(std::max(F.getFunction()->getAlignment(), 8U)); |
Chris Lattner | f75f9be | 2006-05-02 23:22:24 +0000 | [diff] [blame] | 466 | TheJIT->updateGlobalMapping(F.getFunction(), CurBufferPtr); |
Chris Lattner | b4432f3 | 2006-05-03 17:10:41 +0000 | [diff] [blame] | 467 | |
| 468 | MBBLocations.clear(); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 469 | } |
| 470 | |
Chris Lattner | 43b429b | 2006-05-02 18:27:26 +0000 | [diff] [blame] | 471 | bool JITEmitter::finishFunction(MachineFunction &F) { |
Chris Lattner | b4432f3 | 2006-05-03 17:10:41 +0000 | [diff] [blame] | 472 | emitJumpTableInfo(F.getJumpTableInfo()); |
| 473 | |
Chris Lattner | 43b429b | 2006-05-02 18:27:26 +0000 | [diff] [blame] | 474 | MemMgr.endFunctionBody(CurBufferPtr); |
| 475 | NumBytes += getCurrentPCOffset(); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 476 | |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 477 | if (!Relocations.empty()) { |
Chris Lattner | e884dc2 | 2005-07-20 16:29:20 +0000 | [diff] [blame] | 478 | NumRelos += Relocations.size(); |
| 479 | |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 480 | // Resolve the relocations to concrete pointers. |
| 481 | for (unsigned i = 0, e = Relocations.size(); i != e; ++i) { |
| 482 | MachineRelocation &MR = Relocations[i]; |
| 483 | void *ResultPtr; |
Chris Lattner | d91ff7c | 2005-04-18 01:44:27 +0000 | [diff] [blame] | 484 | if (MR.isString()) { |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 485 | ResultPtr = TheJIT->getPointerToNamedFunction(MR.getString()); |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 486 | |
Chris Lattner | d91ff7c | 2005-04-18 01:44:27 +0000 | [diff] [blame] | 487 | // If the target REALLY wants a stub for this function, emit it now. |
| 488 | if (!MR.doesntNeedFunctionStub()) |
| 489 | ResultPtr = getJITResolver(this).getExternalFunctionStub(ResultPtr); |
Chris Lattner | d2d5c76 | 2006-05-03 18:55:56 +0000 | [diff] [blame^] | 490 | } else if (MR.isGlobalValue()) { |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 491 | ResultPtr = getPointerToGlobal(MR.getGlobalValue(), |
Chris Lattner | 43b429b | 2006-05-02 18:27:26 +0000 | [diff] [blame] | 492 | BufferBegin+MR.getMachineCodeOffset(), |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 493 | MR.doesntNeedFunctionStub()); |
Chris Lattner | d2d5c76 | 2006-05-03 18:55:56 +0000 | [diff] [blame^] | 494 | } else { |
| 495 | assert(MR.isConstantPoolIndex()); |
| 496 | ResultPtr=(void*)getConstantPoolEntryAddress(MR.getConstantPoolIndex()); |
| 497 | } |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 498 | |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 499 | MR.setResultPointer(ResultPtr); |
Andrew Lenharth | 16ec33c | 2005-07-22 20:48:12 +0000 | [diff] [blame] | 500 | |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 501 | // if we are managing the GOT and the relocation wants an index, |
| 502 | // give it one |
Chris Lattner | d2d5c76 | 2006-05-03 18:55:56 +0000 | [diff] [blame^] | 503 | if (MemMgr.isManagingGOT() && MR.isGOTRelative()) { |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 504 | unsigned idx = getJITResolver(this).getGOTIndexForAddr(ResultPtr); |
| 505 | MR.setGOTIndex(idx); |
| 506 | if (((void**)MemMgr.getGOTBase())[idx] != ResultPtr) { |
| 507 | DEBUG(std::cerr << "GOT was out of date for " << ResultPtr |
Chris Lattner | 2199877 | 2006-01-07 06:20:51 +0000 | [diff] [blame] | 508 | << " pointing at " << ((void**)MemMgr.getGOTBase())[idx] |
| 509 | << "\n"); |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 510 | ((void**)MemMgr.getGOTBase())[idx] = ResultPtr; |
| 511 | } |
Andrew Lenharth | 16ec33c | 2005-07-22 20:48:12 +0000 | [diff] [blame] | 512 | } |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 513 | } |
| 514 | |
Chris Lattner | 43b429b | 2006-05-02 18:27:26 +0000 | [diff] [blame] | 515 | TheJIT->getJITInfo().relocate(BufferBegin, &Relocations[0], |
Andrew Lenharth | 16ec33c | 2005-07-22 20:48:12 +0000 | [diff] [blame] | 516 | Relocations.size(), MemMgr.getGOTBase()); |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Chris Lattner | d2d5c76 | 2006-05-03 18:55:56 +0000 | [diff] [blame^] | 519 | // Update the GOT entry for F to point to the new code. |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 520 | if(MemMgr.isManagingGOT()) { |
Chris Lattner | 43b429b | 2006-05-02 18:27:26 +0000 | [diff] [blame] | 521 | unsigned idx = getJITResolver(this).getGOTIndexForAddr((void*)BufferBegin); |
| 522 | if (((void**)MemMgr.getGOTBase())[idx] != (void*)BufferBegin) { |
| 523 | DEBUG(std::cerr << "GOT was out of date for " << (void*)BufferBegin |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 524 | << " pointing at " << ((void**)MemMgr.getGOTBase())[idx] << "\n"); |
Chris Lattner | 43b429b | 2006-05-02 18:27:26 +0000 | [diff] [blame] | 525 | ((void**)MemMgr.getGOTBase())[idx] = (void*)BufferBegin; |
Andrew Lenharth | 6a97461 | 2005-07-28 12:44:13 +0000 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | |
Chris Lattner | 43b429b | 2006-05-02 18:27:26 +0000 | [diff] [blame] | 529 | DEBUG(std::cerr << "JIT: Finished CodeGen of [" << (void*)BufferBegin |
Misha Brukman | 1d44085 | 2003-06-06 06:52:35 +0000 | [diff] [blame] | 530 | << "] Function: " << F.getFunction()->getName() |
Chris Lattner | 43b429b | 2006-05-02 18:27:26 +0000 | [diff] [blame] | 531 | << ": " << getCurrentPCOffset() << " bytes of text, " |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 532 | << Relocations.size() << " relocations\n"); |
| 533 | Relocations.clear(); |
Chris Lattner | 43b429b | 2006-05-02 18:27:26 +0000 | [diff] [blame] | 534 | return false; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 535 | } |
| 536 | |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 537 | void JITEmitter::emitConstantPool(MachineConstantPool *MCP) { |
Chris Lattner | fa77d43 | 2006-02-09 04:22:52 +0000 | [diff] [blame] | 538 | const std::vector<MachineConstantPoolEntry> &Constants = MCP->getConstants(); |
Chris Lattner | 2c0a6a1 | 2003-11-30 04:23:21 +0000 | [diff] [blame] | 539 | if (Constants.empty()) return; |
| 540 | |
Chris Lattner | 3029f92 | 2006-02-09 04:46:04 +0000 | [diff] [blame] | 541 | unsigned Size = Constants.back().Offset; |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 542 | Size += TheJIT->getTargetData()->getTypeSize(Constants.back().Val->getType()); |
Chris Lattner | 2c0a6a1 | 2003-11-30 04:23:21 +0000 | [diff] [blame] | 543 | |
Chris Lattner | f75f9be | 2006-05-02 23:22:24 +0000 | [diff] [blame] | 544 | ConstantPoolBase = allocateSpace(Size, 1 << MCP->getConstantPoolAlignment()); |
Chris Lattner | 239862c | 2006-02-09 04:49:59 +0000 | [diff] [blame] | 545 | ConstantPool = MCP; |
Chris Lattner | f75f9be | 2006-05-02 23:22:24 +0000 | [diff] [blame] | 546 | |
| 547 | if (ConstantPoolBase == 0) return; // Buffer overflow. |
| 548 | |
Chris Lattner | 239862c | 2006-02-09 04:49:59 +0000 | [diff] [blame] | 549 | // Initialize the memory for all of the constant pool entries. |
Chris Lattner | 3029f92 | 2006-02-09 04:46:04 +0000 | [diff] [blame] | 550 | for (unsigned i = 0, e = Constants.size(); i != e; ++i) { |
Chris Lattner | 239862c | 2006-02-09 04:49:59 +0000 | [diff] [blame] | 551 | void *CAddr = (char*)ConstantPoolBase+Constants[i].Offset; |
Chris Lattner | 3029f92 | 2006-02-09 04:46:04 +0000 | [diff] [blame] | 552 | TheJIT->InitializeMemory(Constants[i].Val, CAddr); |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 553 | } |
| 554 | } |
| 555 | |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 556 | void JITEmitter::initJumpTableInfo(MachineJumpTableInfo *MJTI) { |
| 557 | const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables(); |
| 558 | if (JT.empty()) return; |
| 559 | |
Chris Lattner | f75f9be | 2006-05-02 23:22:24 +0000 | [diff] [blame] | 560 | unsigned NumEntries = 0; |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 561 | for (unsigned i = 0, e = JT.size(); i != e; ++i) |
Chris Lattner | f75f9be | 2006-05-02 23:22:24 +0000 | [diff] [blame] | 562 | NumEntries += JT[i].MBBs.size(); |
| 563 | |
| 564 | unsigned EntrySize = MJTI->getEntrySize(); |
| 565 | |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 566 | // Just allocate space for all the jump tables now. We will fix up the actual |
| 567 | // MBB entries in the tables after we emit the code for each block, since then |
| 568 | // we will know the final locations of the MBBs in memory. |
| 569 | JumpTable = MJTI; |
Chris Lattner | f75f9be | 2006-05-02 23:22:24 +0000 | [diff] [blame] | 570 | JumpTableBase = allocateSpace(NumEntries * EntrySize, MJTI->getAlignment()); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 571 | } |
| 572 | |
Chris Lattner | b4432f3 | 2006-05-03 17:10:41 +0000 | [diff] [blame] | 573 | void JITEmitter::emitJumpTableInfo(MachineJumpTableInfo *MJTI) { |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 574 | const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables(); |
Chris Lattner | f75f9be | 2006-05-02 23:22:24 +0000 | [diff] [blame] | 575 | if (JT.empty() || JumpTableBase == 0) return; |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 576 | |
| 577 | unsigned Offset = 0; |
Chris Lattner | 32ca55f | 2006-05-03 00:13:06 +0000 | [diff] [blame] | 578 | assert(MJTI->getEntrySize() == sizeof(void*) && "Cross JIT'ing?"); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 579 | |
| 580 | // For each jump table, map each target in the jump table to the address of |
| 581 | // an emitted MachineBasicBlock. |
Chris Lattner | 32ca55f | 2006-05-03 00:13:06 +0000 | [diff] [blame] | 582 | intptr_t *SlotPtr = (intptr_t*)JumpTableBase; |
| 583 | |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 584 | for (unsigned i = 0, e = JT.size(); i != e; ++i) { |
| 585 | const std::vector<MachineBasicBlock*> &MBBs = JT[i].MBBs; |
Chris Lattner | 32ca55f | 2006-05-03 00:13:06 +0000 | [diff] [blame] | 586 | // Store the address of the basic block for this jump table slot in the |
| 587 | // memory we allocated for the jump table in 'initJumpTableInfo' |
| 588 | for (unsigned mi = 0, me = MBBs.size(); mi != me; ++mi) |
Chris Lattner | b4432f3 | 2006-05-03 17:10:41 +0000 | [diff] [blame] | 589 | *SlotPtr++ = getMachineBasicBlockAddress(MBBs[mi]); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 593 | void JITEmitter::startFunctionStub(unsigned StubSize) { |
Chris Lattner | 43b429b | 2006-05-02 18:27:26 +0000 | [diff] [blame] | 594 | SavedBufferBegin = BufferBegin; |
| 595 | SavedBufferEnd = BufferEnd; |
| 596 | SavedCurBufferPtr = CurBufferPtr; |
| 597 | |
| 598 | BufferBegin = CurBufferPtr = MemMgr.allocateStub(StubSize); |
| 599 | BufferEnd = BufferBegin+StubSize+1; |
Chris Lattner | 6125fdd | 2003-05-09 03:30:07 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 602 | void *JITEmitter::finishFunctionStub(const Function *F) { |
Chris Lattner | 43b429b | 2006-05-02 18:27:26 +0000 | [diff] [blame] | 603 | NumBytes += getCurrentPCOffset(); |
| 604 | std::swap(SavedBufferBegin, BufferBegin); |
| 605 | BufferEnd = SavedBufferEnd; |
| 606 | CurBufferPtr = SavedCurBufferPtr; |
| 607 | return SavedBufferBegin; |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 608 | } |
| 609 | |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 610 | // getConstantPoolEntryAddress - Return the address of the 'ConstantNum' entry |
| 611 | // in the constant pool that was last emitted with the 'emitConstantPool' |
| 612 | // method. |
| 613 | // |
Chris Lattner | b4432f3 | 2006-05-03 17:10:41 +0000 | [diff] [blame] | 614 | intptr_t JITEmitter::getConstantPoolEntryAddress(unsigned ConstantNum) const { |
Chris Lattner | 239862c | 2006-02-09 04:49:59 +0000 | [diff] [blame] | 615 | assert(ConstantNum < ConstantPool->getConstants().size() && |
Misha Brukman | 3c94497 | 2005-04-22 04:08:30 +0000 | [diff] [blame] | 616 | "Invalid ConstantPoolIndex!"); |
Chris Lattner | 239862c | 2006-02-09 04:49:59 +0000 | [diff] [blame] | 617 | return (intptr_t)ConstantPoolBase + |
| 618 | ConstantPool->getConstants()[ConstantNum].Offset; |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 619 | } |
| 620 | |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 621 | // getJumpTableEntryAddress - Return the address of the JumpTable with index |
| 622 | // 'Index' in the jumpp table that was last initialized with 'initJumpTableInfo' |
| 623 | // |
Chris Lattner | b4432f3 | 2006-05-03 17:10:41 +0000 | [diff] [blame] | 624 | intptr_t JITEmitter::getJumpTableEntryAddress(unsigned Index) const { |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 625 | const std::vector<MachineJumpTableEntry> &JT = JumpTable->getJumpTables(); |
| 626 | assert(Index < JT.size() && "Invalid jump table index!"); |
| 627 | |
| 628 | unsigned Offset = 0; |
| 629 | unsigned EntrySize = JumpTable->getEntrySize(); |
| 630 | |
| 631 | for (unsigned i = 0; i < Index; ++i) |
| 632 | Offset += JT[i].MBBs.size() * EntrySize; |
| 633 | |
Nate Begeman | c34b227 | 2006-04-25 17:46:32 +0000 | [diff] [blame] | 634 | return (intptr_t)((char *)JumpTableBase + Offset); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 635 | } |
| 636 | |
Misha Brukman | d69c1e6 | 2003-07-28 19:09:06 +0000 | [diff] [blame] | 637 | // getPointerToNamedFunction - This function is used as a global wrapper to |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 638 | // JIT::getPointerToNamedFunction for the purpose of resolving symbols when |
Misha Brukman | d69c1e6 | 2003-07-28 19:09:06 +0000 | [diff] [blame] | 639 | // bugpoint is debugging the JIT. In that scenario, we are loading an .so and |
| 640 | // need to resolve function(s) that are being mis-codegenerated, so we need to |
| 641 | // resolve their addresses at runtime, and this is the way to do it. |
| 642 | extern "C" { |
| 643 | void *getPointerToNamedFunction(const char *Name) { |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 644 | Module &M = TheJIT->getModule(); |
Misha Brukman | d69c1e6 | 2003-07-28 19:09:06 +0000 | [diff] [blame] | 645 | if (Function *F = M.getNamedFunction(Name)) |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 646 | return TheJIT->getPointerToFunction(F); |
| 647 | return TheJIT->getPointerToNamedFunction(Name); |
Misha Brukman | d69c1e6 | 2003-07-28 19:09:06 +0000 | [diff] [blame] | 648 | } |
| 649 | } |