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