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