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