LLVM API Documentation

JITEmitter.cpp

Go to the documentation of this file.
00001 //===-- JITEmitter.cpp - Write machine code to executable memory ----------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines a MachineCodeEmitter object that is used by the JIT to
00011 // write machine code to memory and remember where relocatable values are.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #define DEBUG_TYPE "jit"
00016 #include "JIT.h"
00017 #include "llvm/Constant.h"
00018 #include "llvm/Module.h"
00019 #include "llvm/Type.h"
00020 #include "llvm/CodeGen/MachineCodeEmitter.h"
00021 #include "llvm/CodeGen/MachineFunction.h"
00022 #include "llvm/CodeGen/MachineConstantPool.h"
00023 #include "llvm/CodeGen/MachineRelocation.h"
00024 #include "llvm/Target/TargetData.h"
00025 #include "llvm/Target/TargetJITInfo.h"
00026 #include "llvm/Support/Debug.h"
00027 #include "llvm/ADT/Statistic.h"
00028 #include "llvm/System/Memory.h"
00029 #include <algorithm>
00030 #include <iostream>
00031 #include <list>
00032 using namespace llvm;
00033 
00034 namespace {
00035   Statistic<> NumBytes("jit", "Number of bytes of machine code compiled");
00036   Statistic<> NumRelos("jit", "Number of relocations applied");
00037   JIT *TheJIT = 0;
00038 }
00039 
00040 
00041 //===----------------------------------------------------------------------===//
00042 // JITMemoryManager code.
00043 //
00044 namespace {
00045   /// JITMemoryManager - Manage memory for the JIT code generation in a logical,
00046   /// sane way.  This splits a large block of MAP_NORESERVE'd memory into two
00047   /// sections, one for function stubs, one for the functions themselves.  We
00048   /// have to do this because we may need to emit a function stub while in the
00049   /// middle of emitting a function, and we don't know how large the function we
00050   /// are emitting is.  This never bothers to release the memory, because when
00051   /// we are ready to destroy the JIT, the program exits.
00052   class JITMemoryManager {
00053     std::list<sys::MemoryBlock> Blocks; // List of blocks allocated by the JIT
00054     unsigned char *FunctionBase; // Start of the function body area
00055     unsigned char *GlobalBase; // Start of the Global area
00056     unsigned char *ConstantBase; // Memory allocated for constant pools
00057     unsigned char *CurStubPtr, *CurFunctionPtr, *CurConstantPtr, *CurGlobalPtr;
00058     unsigned char *GOTBase; //Target Specific reserved memory
00059 
00060     // centralize memory block allocation
00061     sys::MemoryBlock getNewMemoryBlock(unsigned size);
00062   public:
00063     JITMemoryManager(bool useGOT);
00064     ~JITMemoryManager();
00065 
00066     inline unsigned char *allocateStub(unsigned StubSize);
00067     inline unsigned char *allocateConstant(unsigned ConstantSize,
00068                                            unsigned Alignment);
00069     inline unsigned char* allocateGlobal(unsigned Size,
00070                                          unsigned Alignment);
00071     inline unsigned char *startFunctionBody();
00072     inline void endFunctionBody(unsigned char *FunctionEnd);
00073     inline unsigned char* getGOTBase() const;
00074 
00075     inline bool isManagingGOT() const;
00076   };
00077 }
00078 
00079 JITMemoryManager::JITMemoryManager(bool useGOT) {
00080   // Allocate a 16M block of memory for functions
00081   sys::MemoryBlock FunBlock = getNewMemoryBlock(16 << 20);
00082   // Allocate a 1M block of memory for Constants
00083   sys::MemoryBlock ConstBlock = getNewMemoryBlock(1 << 20);
00084   // Allocate a 1M Block of memory for Globals
00085   sys::MemoryBlock GVBlock = getNewMemoryBlock(1 << 20);
00086 
00087   Blocks.push_front(FunBlock);
00088   Blocks.push_front(ConstBlock);
00089   Blocks.push_front(GVBlock);
00090 
00091   FunctionBase = reinterpret_cast<unsigned char*>(FunBlock.base());
00092   ConstantBase = reinterpret_cast<unsigned char*>(ConstBlock.base());
00093   GlobalBase = reinterpret_cast<unsigned char*>(GVBlock.base());
00094 
00095   // Allocate stubs backwards from the base, allocate functions forward
00096   // from the base.
00097   CurStubPtr = CurFunctionPtr = FunctionBase + 512*1024;// Use 512k for stubs
00098 
00099   CurConstantPtr = ConstantBase + ConstBlock.size();
00100   CurGlobalPtr = GlobalBase + GVBlock.size();
00101 
00102   //Allocate the GOT just like a global array
00103   GOTBase = NULL;
00104   if (useGOT)
00105     GOTBase = allocateGlobal(sizeof(void*) * 8192, 8);
00106 }
00107 
00108 JITMemoryManager::~JITMemoryManager() {
00109   for (std::list<sys::MemoryBlock>::iterator ib = Blocks.begin(),
00110        ie = Blocks.end(); ib != ie; ++ib)
00111     sys::Memory::ReleaseRWX(*ib);
00112   Blocks.clear();
00113 }
00114 
00115 unsigned char *JITMemoryManager::allocateStub(unsigned StubSize) {
00116   CurStubPtr -= StubSize;
00117   if (CurStubPtr < FunctionBase) {
00118     //FIXME: allocate a new block
00119     std::cerr << "JIT ran out of memory for function stubs!\n";
00120     abort();
00121   }
00122   return CurStubPtr;
00123 }
00124 
00125 unsigned char *JITMemoryManager::allocateConstant(unsigned ConstantSize,
00126                                                   unsigned Alignment) {
00127   // Reserve space and align pointer.
00128   CurConstantPtr -= ConstantSize;
00129   CurConstantPtr =
00130     (unsigned char *)((intptr_t)CurConstantPtr & ~((intptr_t)Alignment - 1));
00131 
00132   if (CurConstantPtr < ConstantBase) {
00133     //Either allocate another MB or 2xConstantSize
00134     sys::MemoryBlock ConstBlock = getNewMemoryBlock(2 * ConstantSize);
00135     ConstantBase = reinterpret_cast<unsigned char*>(ConstBlock.base());
00136     CurConstantPtr = ConstantBase + ConstBlock.size();
00137     return allocateConstant(ConstantSize, Alignment);
00138   }
00139   return CurConstantPtr;
00140 }
00141 
00142 unsigned char *JITMemoryManager::allocateGlobal(unsigned Size,
00143                                                 unsigned Alignment) {
00144  // Reserve space and align pointer.
00145   CurGlobalPtr -= Size;
00146   CurGlobalPtr =
00147     (unsigned char *)((intptr_t)CurGlobalPtr & ~((intptr_t)Alignment - 1));
00148 
00149   if (CurGlobalPtr < GlobalBase) {
00150     //Either allocate another MB or 2xSize
00151     sys::MemoryBlock GVBlock =  getNewMemoryBlock(2 * Size);
00152     GlobalBase = reinterpret_cast<unsigned char*>(GVBlock.base());
00153     CurGlobalPtr = GlobalBase + GVBlock.size();
00154     return allocateGlobal(Size, Alignment);
00155   }
00156   return CurGlobalPtr;
00157 }
00158 
00159 unsigned char *JITMemoryManager::startFunctionBody() {
00160   // Round up to an even multiple of 8 bytes, this should eventually be target
00161   // specific.
00162   return (unsigned char*)(((intptr_t)CurFunctionPtr + 7) & ~7);
00163 }
00164 
00165 void JITMemoryManager::endFunctionBody(unsigned char *FunctionEnd) {
00166   assert(FunctionEnd > CurFunctionPtr);
00167   CurFunctionPtr = FunctionEnd;
00168 }
00169 
00170 unsigned char* JITMemoryManager::getGOTBase() const {
00171   return GOTBase;
00172 }
00173 
00174 bool JITMemoryManager::isManagingGOT() const {
00175   return GOTBase != NULL;
00176 }
00177 
00178 sys::MemoryBlock JITMemoryManager::getNewMemoryBlock(unsigned size) {
00179   const sys::MemoryBlock* BOld = 0;
00180   if (Blocks.size())
00181     BOld = &Blocks.front();
00182   //never allocate less than 1 MB
00183   sys::MemoryBlock B;
00184   try {
00185     B = sys::Memory::AllocateRWX(std::max(((unsigned)1 << 20), size), BOld);
00186   } catch (std::string& err) {
00187     std::cerr << "Allocation failed when allocating new memory in the JIT\n";
00188     std::cerr << err << "\n";
00189     abort();
00190   }
00191   Blocks.push_front(B);
00192   return B;
00193 }
00194 
00195 //===----------------------------------------------------------------------===//
00196 // JIT lazy compilation code.
00197 //
00198 namespace {
00199   class JITResolverState {
00200   private:
00201     /// FunctionToStubMap - Keep track of the stub created for a particular
00202     /// function so that we can reuse them if necessary.
00203     std::map<Function*, void*> FunctionToStubMap;
00204 
00205     /// StubToFunctionMap - Keep track of the function that each stub
00206     /// corresponds to.
00207     std::map<void*, Function*> StubToFunctionMap;
00208 
00209   public:
00210     std::map<Function*, void*>& getFunctionToStubMap(const MutexGuard& locked) {
00211       assert(locked.holds(TheJIT->lock));
00212       return FunctionToStubMap;
00213     }
00214 
00215     std::map<void*, Function*>& getStubToFunctionMap(const MutexGuard& locked) {
00216       assert(locked.holds(TheJIT->lock));
00217       return StubToFunctionMap;
00218     }
00219   };
00220 
00221   /// JITResolver - Keep track of, and resolve, call sites for functions that
00222   /// have not yet been compiled.
00223   class JITResolver {
00224     /// MCE - The MachineCodeEmitter to use to emit stubs with.
00225     MachineCodeEmitter &MCE;
00226 
00227     /// LazyResolverFn - The target lazy resolver function that we actually
00228     /// rewrite instructions to use.
00229     TargetJITInfo::LazyResolverFn LazyResolverFn;
00230 
00231     JITResolverState state;
00232 
00233     /// ExternalFnToStubMap - This is the equivalent of FunctionToStubMap for
00234     /// external functions.
00235     std::map<void*, void*> ExternalFnToStubMap;
00236 
00237     //map addresses to indexes in the GOT
00238     std::map<void*, unsigned> revGOTMap;
00239     unsigned nextGOTIndex;
00240 
00241   public:
00242     JITResolver(MachineCodeEmitter &mce) : MCE(mce), nextGOTIndex(0) {
00243       LazyResolverFn =
00244         TheJIT->getJITInfo().getLazyResolverFunction(JITCompilerFn);
00245     }
00246 
00247     /// getFunctionStub - This returns a pointer to a function stub, creating
00248     /// one on demand as needed.
00249     void *getFunctionStub(Function *F);
00250 
00251     /// getExternalFunctionStub - Return a stub for the function at the
00252     /// specified address, created lazily on demand.
00253     void *getExternalFunctionStub(void *FnAddr);
00254 
00255     /// AddCallbackAtLocation - If the target is capable of rewriting an
00256     /// instruction without the use of a stub, record the location of the use so
00257     /// we know which function is being used at the location.
00258     void *AddCallbackAtLocation(Function *F, void *Location) {
00259       MutexGuard locked(TheJIT->lock);
00260       /// Get the target-specific JIT resolver function.
00261       state.getStubToFunctionMap(locked)[Location] = F;
00262       return (void*)LazyResolverFn;
00263     }
00264 
00265     /// getGOTIndexForAddress - Return a new or existing index in the GOT for
00266     /// and address.  This function only manages slots, it does not manage the
00267     /// contents of the slots or the memory associated with the GOT.
00268     unsigned getGOTIndexForAddr(void* addr);
00269 
00270     /// JITCompilerFn - This function is called to resolve a stub to a compiled
00271     /// address.  If the LLVM Function corresponding to the stub has not yet
00272     /// been compiled, this function compiles it first.
00273     static void *JITCompilerFn(void *Stub);
00274   };
00275 }
00276 
00277 /// getJITResolver - This function returns the one instance of the JIT resolver.
00278 ///
00279 static JITResolver &getJITResolver(MachineCodeEmitter *MCE = 0) {
00280   static JITResolver TheJITResolver(*MCE);
00281   return TheJITResolver;
00282 }
00283 
00284 /// getFunctionStub - This returns a pointer to a function stub, creating
00285 /// one on demand as needed.
00286 void *JITResolver::getFunctionStub(Function *F) {
00287   MutexGuard locked(TheJIT->lock);
00288 
00289   // If we already have a stub for this function, recycle it.
00290   void *&Stub = state.getFunctionToStubMap(locked)[F];
00291   if (Stub) return Stub;
00292 
00293   // Call the lazy resolver function unless we already KNOW it is an external
00294   // function, in which case we just skip the lazy resolution step.
00295   void *Actual = (void*)LazyResolverFn;
00296   if (F->isExternal() && F->hasExternalLinkage())
00297     Actual = TheJIT->getPointerToFunction(F);
00298 
00299   // Otherwise, codegen a new stub.  For now, the stub will call the lazy
00300   // resolver function.
00301   Stub = TheJIT->getJITInfo().emitFunctionStub(Actual, MCE);
00302 
00303   if (Actual != (void*)LazyResolverFn) {
00304     // If we are getting the stub for an external function, we really want the
00305     // address of the stub in the GlobalAddressMap for the JIT, not the address
00306     // of the external function.
00307     TheJIT->updateGlobalMapping(F, Stub);
00308   }
00309 
00310   DEBUG(std::cerr << "JIT: Stub emitted at [" << Stub << "] for function '"
00311                   << F->getName() << "'\n");
00312 
00313   // Finally, keep track of the stub-to-Function mapping so that the
00314   // JITCompilerFn knows which function to compile!
00315   state.getStubToFunctionMap(locked)[Stub] = F;
00316   return Stub;
00317 }
00318 
00319 /// getExternalFunctionStub - Return a stub for the function at the
00320 /// specified address, created lazily on demand.
00321 void *JITResolver::getExternalFunctionStub(void *FnAddr) {
00322   // If we already have a stub for this function, recycle it.
00323   void *&Stub = ExternalFnToStubMap[FnAddr];
00324   if (Stub) return Stub;
00325 
00326   Stub = TheJIT->getJITInfo().emitFunctionStub(FnAddr, MCE);
00327   DEBUG(std::cerr << "JIT: Stub emitted at [" << Stub
00328         << "] for external function at '" << FnAddr << "'\n");
00329   return Stub;
00330 }
00331 
00332 unsigned JITResolver::getGOTIndexForAddr(void* addr) {
00333   unsigned idx = revGOTMap[addr];
00334   if (!idx) {
00335     idx = ++nextGOTIndex;
00336     revGOTMap[addr] = idx;
00337     DEBUG(std::cerr << "Adding GOT entry " << idx
00338           << " for addr " << addr << "\n");
00339     //    ((void**)MemMgr.getGOTBase())[idx] = addr;
00340   }
00341   return idx;
00342 }
00343 
00344 /// JITCompilerFn - This function is called when a lazy compilation stub has
00345 /// been entered.  It looks up which function this stub corresponds to, compiles
00346 /// it if necessary, then returns the resultant function pointer.
00347 void *JITResolver::JITCompilerFn(void *Stub) {
00348   JITResolver &JR = getJITResolver();
00349 
00350   MutexGuard locked(TheJIT->lock);
00351 
00352   // The address given to us for the stub may not be exactly right, it might be
00353   // a little bit after the stub.  As such, use upper_bound to find it.
00354   std::map<void*, Function*>::iterator I =
00355     JR.state.getStubToFunctionMap(locked).upper_bound(Stub);
00356   assert(I != JR.state.getStubToFunctionMap(locked).begin() &&
00357          "This is not a known stub!");
00358   Function *F = (--I)->second;
00359 
00360   // We might like to remove the stub from the StubToFunction map.
00361   // We can't do that! Multiple threads could be stuck, waiting to acquire the
00362   // lock above. As soon as the 1st function finishes compiling the function,
00363   // the next one will be released, and needs to be able to find the function it
00364   // needs to call.
00365   //JR.state.getStubToFunctionMap(locked).erase(I);
00366 
00367   DEBUG(std::cerr << "JIT: Lazily resolving function '" << F->getName()
00368                   << "' In stub ptr = " << Stub << " actual ptr = "
00369                   << I->first << "\n");
00370 
00371   void *Result = TheJIT->getPointerToFunction(F);
00372 
00373   // We don't need to reuse this stub in the future, as F is now compiled.
00374   JR.state.getFunctionToStubMap(locked).erase(F);
00375 
00376   // FIXME: We could rewrite all references to this stub if we knew them.
00377 
00378   // What we will do is set the compiled function address to map to the
00379   // same GOT entry as the stub so that later clients may update the GOT
00380   // if they see it still using the stub address.
00381   // Note: this is done so the Resolver doesn't have to manage GOT memory
00382   // Do this without allocating map space if the target isn't using a GOT
00383   if(JR.revGOTMap.find(Stub) != JR.revGOTMap.end())
00384     JR.revGOTMap[Result] = JR.revGOTMap[Stub];
00385 
00386   return Result;
00387 }
00388 
00389 
00390 // getPointerToFunctionOrStub - If the specified function has been
00391 // code-gen'd, return a pointer to the function.  If not, compile it, or use
00392 // a stub to implement lazy compilation if available.
00393 //
00394 void *JIT::getPointerToFunctionOrStub(Function *F) {
00395   // If we have already code generated the function, just return the address.
00396   if (void *Addr = getPointerToGlobalIfAvailable(F))
00397     return Addr;
00398 
00399   // Get a stub if the target supports it
00400   return getJITResolver(MCE).getFunctionStub(F);
00401 }
00402 
00403 
00404 
00405 //===----------------------------------------------------------------------===//
00406 // JITEmitter code.
00407 //
00408 namespace {
00409   /// JITEmitter - The JIT implementation of the MachineCodeEmitter, which is
00410   /// used to output functions to memory for execution.
00411   class JITEmitter : public MachineCodeEmitter {
00412     JITMemoryManager MemMgr;
00413 
00414     // CurBlock - The start of the current block of memory.  CurByte - The
00415     // current byte being emitted to.
00416     unsigned char *CurBlock, *CurByte;
00417 
00418     // When outputting a function stub in the context of some other function, we
00419     // save CurBlock and CurByte here.
00420     unsigned char *SavedCurBlock, *SavedCurByte;
00421 
00422     /// Relocations - These are the relocations that the function needs, as
00423     /// emitted.
00424     std::vector<MachineRelocation> Relocations;
00425 
00426     /// ConstantPool - The constant pool for the current function.
00427     ///
00428     MachineConstantPool *ConstantPool;
00429 
00430     /// ConstantPoolBase - A pointer to the first entry in the constant pool.
00431     ///
00432     void *ConstantPoolBase;
00433   public:
00434     JITEmitter(JIT &jit) : MemMgr(jit.getJITInfo().needsGOT()) {
00435       TheJIT = &jit;
00436       DEBUG(std::cerr <<
00437             (MemMgr.isManagingGOT() ? "JIT is managing GOT\n"
00438              : "JIT is not managing GOT\n"));
00439     }
00440 
00441     virtual void startFunction(MachineFunction &F);
00442     virtual void finishFunction(MachineFunction &F);
00443     virtual void emitConstantPool(MachineConstantPool *MCP);
00444     virtual void startFunctionStub(unsigned StubSize);
00445     virtual void* finishFunctionStub(const Function *F);
00446     virtual void emitByte(unsigned char B);
00447     virtual void emitWord(unsigned W);
00448     virtual void emitWordAt(unsigned W, unsigned *Ptr);
00449 
00450     virtual void addRelocation(const MachineRelocation &MR) {
00451       Relocations.push_back(MR);
00452     }
00453 
00454     virtual uint64_t getCurrentPCValue();
00455     virtual uint64_t getCurrentPCOffset();
00456     virtual uint64_t getConstantPoolEntryAddress(unsigned Entry);
00457     virtual unsigned char* allocateGlobal(unsigned size, unsigned alignment);
00458 
00459   private:
00460     void *getPointerToGlobal(GlobalValue *GV, void *Reference, bool NoNeedStub);
00461   };
00462 }
00463 
00464 MachineCodeEmitter *JIT::createEmitter(JIT &jit) {
00465   return new JITEmitter(jit);
00466 }
00467 
00468 void *JITEmitter::getPointerToGlobal(GlobalValue *V, void *Reference,
00469                                      bool DoesntNeedStub) {
00470   if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
00471     /// FIXME: If we straightened things out, this could actually emit the
00472     /// global immediately instead of queuing it for codegen later!
00473     return TheJIT->getOrEmitGlobalVariable(GV);
00474   }
00475 
00476   // If we have already compiled the function, return a pointer to its body.
00477   Function *F = cast<Function>(V);
00478   void *ResultPtr = TheJIT->getPointerToGlobalIfAvailable(F);
00479   if (ResultPtr) return ResultPtr;
00480 
00481   if (F->hasExternalLinkage() && F->isExternal()) {
00482     // If this is an external function pointer, we can force the JIT to
00483     // 'compile' it, which really just adds it to the map.
00484     if (DoesntNeedStub)
00485       return TheJIT->getPointerToFunction(F);
00486 
00487     return getJITResolver(this).getFunctionStub(F);
00488   }
00489 
00490   // Okay, the function has not been compiled yet, if the target callback
00491   // mechanism is capable of rewriting the instruction directly, prefer to do
00492   // that instead of emitting a stub.
00493   if (DoesntNeedStub)
00494     return getJITResolver(this).AddCallbackAtLocation(F, Reference);
00495 
00496   // Otherwise, we have to emit a lazy resolving stub.
00497   return getJITResolver(this).getFunctionStub(F);
00498 }
00499 
00500 void JITEmitter::startFunction(MachineFunction &F) {
00501   CurByte = CurBlock = MemMgr.startFunctionBody();
00502   TheJIT->addGlobalMapping(F.getFunction(), CurBlock);
00503 }
00504 
00505 void JITEmitter::finishFunction(MachineFunction &F) {
00506   MemMgr.endFunctionBody(CurByte);
00507   NumBytes += CurByte-CurBlock;
00508 
00509   if (!Relocations.empty()) {
00510     NumRelos += Relocations.size();
00511 
00512     // Resolve the relocations to concrete pointers.
00513     for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
00514       MachineRelocation &MR = Relocations[i];
00515       void *ResultPtr;
00516       if (MR.isString()) {
00517         ResultPtr = TheJIT->getPointerToNamedFunction(MR.getString());
00518 
00519         // If the target REALLY wants a stub for this function, emit it now.
00520         if (!MR.doesntNeedFunctionStub())
00521           ResultPtr = getJITResolver(this).getExternalFunctionStub(ResultPtr);
00522       } else if (MR.isGlobalValue())
00523         ResultPtr = getPointerToGlobal(MR.getGlobalValue(),
00524                                        CurBlock+MR.getMachineCodeOffset(),
00525                                        MR.doesntNeedFunctionStub());
00526       else //ConstantPoolIndex
00527         ResultPtr =
00528        (void*)(intptr_t)getConstantPoolEntryAddress(MR.getConstantPoolIndex());
00529 
00530       MR.setResultPointer(ResultPtr);
00531 
00532       // if we are managing the GOT and the relocation wants an index,
00533       // give it one
00534       if (MemMgr.isManagingGOT() && !MR.isConstantPoolIndex() &&
00535           MR.isGOTRelative()) {
00536         unsigned idx = getJITResolver(this).getGOTIndexForAddr(ResultPtr);
00537         MR.setGOTIndex(idx);
00538         if (((void**)MemMgr.getGOTBase())[idx] != ResultPtr) {
00539           DEBUG(std::cerr << "GOT was out of date for " << ResultPtr
00540                 << " pointing at " << ((void**)MemMgr.getGOTBase())[idx]
00541                 << "\n");
00542           ((void**)MemMgr.getGOTBase())[idx] = ResultPtr;
00543         }
00544       }
00545     }
00546 
00547     TheJIT->getJITInfo().relocate(CurBlock, &Relocations[0],
00548                                   Relocations.size(), MemMgr.getGOTBase());
00549   }
00550 
00551   //Update the GOT entry for F to point to the new code.
00552   if(MemMgr.isManagingGOT()) {
00553     unsigned idx = getJITResolver(this).getGOTIndexForAddr((void*)CurBlock);
00554     if (((void**)MemMgr.getGOTBase())[idx] != (void*)CurBlock) {
00555       DEBUG(std::cerr << "GOT was out of date for " << (void*)CurBlock
00556             << " pointing at " << ((void**)MemMgr.getGOTBase())[idx] << "\n");
00557       ((void**)MemMgr.getGOTBase())[idx] = (void*)CurBlock;
00558     }
00559   }
00560 
00561   DEBUG(std::cerr << "JIT: Finished CodeGen of [" << (void*)CurBlock
00562                   << "] Function: " << F.getFunction()->getName()
00563                   << ": " << CurByte-CurBlock << " bytes of text, "
00564                   << Relocations.size() << " relocations\n");
00565   Relocations.clear();
00566 }
00567 
00568 void JITEmitter::emitConstantPool(MachineConstantPool *MCP) {
00569   const std::vector<MachineConstantPoolEntry> &Constants = MCP->getConstants();
00570   if (Constants.empty()) return;
00571 
00572   unsigned Size = Constants.back().Offset;
00573   Size += TheJIT->getTargetData().getTypeSize(Constants.back().Val->getType());
00574 
00575   ConstantPoolBase = MemMgr.allocateConstant(Size, 
00576                                        1 << MCP->getConstantPoolAlignment());
00577   ConstantPool = MCP;
00578   
00579   // Initialize the memory for all of the constant pool entries.
00580   for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
00581     void *CAddr = (char*)ConstantPoolBase+Constants[i].Offset;
00582     TheJIT->InitializeMemory(Constants[i].Val, CAddr);
00583   }
00584 }
00585 
00586 void JITEmitter::startFunctionStub(unsigned StubSize) {
00587   SavedCurBlock = CurBlock;  SavedCurByte = CurByte;
00588   CurByte = CurBlock = MemMgr.allocateStub(StubSize);
00589 }
00590 
00591 void *JITEmitter::finishFunctionStub(const Function *F) {
00592   NumBytes += CurByte-CurBlock;
00593   std::swap(CurBlock, SavedCurBlock);
00594   CurByte = SavedCurByte;
00595   return SavedCurBlock;
00596 }
00597 
00598 void JITEmitter::emitByte(unsigned char B) {
00599   *CurByte++ = B;   // Write the byte to memory
00600 }
00601 
00602 void JITEmitter::emitWord(unsigned W) {
00603   // This won't work if the endianness of the host and target don't agree!  (For
00604   // a JIT this can't happen though.  :)
00605   *(unsigned*)CurByte = W;
00606   CurByte += sizeof(unsigned);
00607 }
00608 
00609 void JITEmitter::emitWordAt(unsigned W, unsigned *Ptr) {
00610   *Ptr = W;
00611 }
00612 
00613 // getConstantPoolEntryAddress - Return the address of the 'ConstantNum' entry
00614 // in the constant pool that was last emitted with the 'emitConstantPool'
00615 // method.
00616 //
00617 uint64_t JITEmitter::getConstantPoolEntryAddress(unsigned ConstantNum) {
00618   assert(ConstantNum < ConstantPool->getConstants().size() &&
00619          "Invalid ConstantPoolIndex!");
00620   return (intptr_t)ConstantPoolBase +
00621          ConstantPool->getConstants()[ConstantNum].Offset;
00622 }
00623 
00624 unsigned char* JITEmitter::allocateGlobal(unsigned size, unsigned alignment)
00625 {
00626   return MemMgr.allocateGlobal(size, alignment);
00627 }
00628 
00629 // getCurrentPCValue - This returns the address that the next emitted byte
00630 // will be output to.
00631 //
00632 uint64_t JITEmitter::getCurrentPCValue() {
00633   return (intptr_t)CurByte;
00634 }
00635 
00636 uint64_t JITEmitter::getCurrentPCOffset() {
00637   return (intptr_t)CurByte-(intptr_t)CurBlock;
00638 }
00639 
00640 // getPointerToNamedFunction - This function is used as a global wrapper to
00641 // JIT::getPointerToNamedFunction for the purpose of resolving symbols when
00642 // bugpoint is debugging the JIT. In that scenario, we are loading an .so and
00643 // need to resolve function(s) that are being mis-codegenerated, so we need to
00644 // resolve their addresses at runtime, and this is the way to do it.
00645 extern "C" {
00646   void *getPointerToNamedFunction(const char *Name) {
00647     Module &M = TheJIT->getModule();
00648     if (Function *F = M.getNamedFunction(Name))
00649       return TheJIT->getPointerToFunction(F);
00650     return TheJIT->getPointerToNamedFunction(Name);
00651   }
00652 }