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