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