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