Fix -pedantic warnings.
[oota-llvm.git] / lib / ExecutionEngine / JIT / JITEmitter.cpp
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/Support/MutexGuard.h"
30 #include "llvm/ADT/Statistic.h"
31 #include "llvm/System/Memory.h"
32 #include <algorithm>
33 #include <iostream>
34 using namespace llvm;
35
36 namespace {
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 //
46 namespace {
47   /// MemoryRangeHeader - For a range of memory, this is the header that we put
48   /// on the block of memory.  It is carefully crafted to be one word of memory.
49   /// Allocated blocks have just this header, free'd blocks have FreeRangeHeader
50   /// which starts with this.
51   struct FreeRangeHeader;
52   struct MemoryRangeHeader {
53     /// ThisAllocated - This is true if this block is currently allocated.  If
54     /// not, this can be converted to a FreeRangeHeader.
55     intptr_t ThisAllocated : 1;
56     
57     /// PrevAllocated - Keep track of whether the block immediately before us is
58     /// allocated.  If not, the word immediately before this header is the size
59     /// of the previous block.
60     intptr_t PrevAllocated : 1;
61     
62     /// BlockSize - This is the size in bytes of this memory block,
63     /// including this header.
64     uintptr_t BlockSize : (sizeof(intptr_t)*8 - 2);
65     
66
67     /// getBlockAfter - Return the memory block immediately after this one.
68     ///
69     MemoryRangeHeader &getBlockAfter() const {
70       return *(MemoryRangeHeader*)((char*)this+BlockSize);
71     }
72     
73     /// getFreeBlockBefore - If the block before this one is free, return it,
74     /// otherwise return null.
75     FreeRangeHeader *getFreeBlockBefore() const {
76       if (PrevAllocated) return 0;
77       intptr_t PrevSize = ((intptr_t *)this)[-1];
78       return (FreeRangeHeader*)((char*)this-PrevSize);
79     }
80     
81     /// FreeBlock - Turn an allocated block into a free block, adjusting
82     /// bits in the object headers, and adding an end of region memory block.
83     FreeRangeHeader *FreeBlock(FreeRangeHeader *FreeList);
84     
85     /// TrimAllocationToSize - If this allocated block is significantly larger
86     /// than NewSize, split it into two pieces (where the former is NewSize
87     /// bytes, including the header), and add the new block to the free list.
88     FreeRangeHeader *TrimAllocationToSize(FreeRangeHeader *FreeList, 
89                                           uint64_t NewSize);
90   };
91
92   /// FreeRangeHeader - For a memory block that isn't already allocated, this
93   /// keeps track of the current block and has a pointer to the next free block.
94   /// Free blocks are kept on a circularly linked list.
95   struct FreeRangeHeader : public MemoryRangeHeader {
96     FreeRangeHeader *Prev;
97     FreeRangeHeader *Next;
98     
99     /// getMinBlockSize - Get the minimum size for a memory block.  Blocks
100     /// smaller than this size cannot be created.
101     static unsigned getMinBlockSize() {
102       return sizeof(FreeRangeHeader)+sizeof(intptr_t);
103     }
104     
105     /// SetEndOfBlockSizeMarker - The word at the end of every free block is
106     /// known to be the size of the free block.  Set it for this block.
107     void SetEndOfBlockSizeMarker() {
108       void *EndOfBlock = (char*)this + BlockSize;
109       ((intptr_t *)EndOfBlock)[-1] = BlockSize;
110     }
111
112     FreeRangeHeader *RemoveFromFreeList() {
113       assert(Next->Prev == this && Prev->Next == this && "Freelist broken!");
114       Next->Prev = Prev;
115       return Prev->Next = Next;
116     }
117     
118     void AddToFreeList(FreeRangeHeader *FreeList) {
119       Next = FreeList;
120       Prev = FreeList->Prev;
121       Prev->Next = this;
122       Next->Prev = this;
123     }
124
125     /// GrowBlock - The block after this block just got deallocated.  Merge it
126     /// into the current block.
127     void GrowBlock(uintptr_t NewSize);
128     
129     /// AllocateBlock - Mark this entire block allocated, updating freelists
130     /// etc.  This returns a pointer to the circular free-list.
131     FreeRangeHeader *AllocateBlock();
132   };
133 }
134
135
136 /// AllocateBlock - Mark this entire block allocated, updating freelists
137 /// etc.  This returns a pointer to the circular free-list.
138 FreeRangeHeader *FreeRangeHeader::AllocateBlock() {
139   assert(!ThisAllocated && !getBlockAfter().PrevAllocated &&
140          "Cannot allocate an allocated block!");
141   // Mark this block allocated.
142   ThisAllocated = 1;
143   getBlockAfter().PrevAllocated = 1;
144  
145   // Remove it from the free list.
146   return RemoveFromFreeList();
147 }
148
149 /// FreeBlock - Turn an allocated block into a free block, adjusting
150 /// bits in the object headers, and adding an end of region memory block.
151 /// If possible, coallesce this block with neighboring blocks.  Return the
152 /// FreeRangeHeader to allocate from.
153 FreeRangeHeader *MemoryRangeHeader::FreeBlock(FreeRangeHeader *FreeList) {
154   MemoryRangeHeader *FollowingBlock = &getBlockAfter();
155   assert(ThisAllocated && "This block is already allocated!");
156   assert(FollowingBlock->PrevAllocated && "Flags out of sync!");
157   
158   FreeRangeHeader *FreeListToReturn = FreeList;
159   
160   // If the block after this one is free, merge it into this block.
161   if (!FollowingBlock->ThisAllocated) {
162     FreeRangeHeader &FollowingFreeBlock = *(FreeRangeHeader *)FollowingBlock;
163     // "FreeList" always needs to be a valid free block.  If we're about to
164     // coallesce with it, update our notion of what the free list is.
165     if (&FollowingFreeBlock == FreeList) {
166       FreeList = FollowingFreeBlock.Next;
167       FreeListToReturn = 0;
168       assert(&FollowingFreeBlock != FreeList && "No tombstone block?");
169     }
170     FollowingFreeBlock.RemoveFromFreeList();
171     
172     // Include the following block into this one.
173     BlockSize += FollowingFreeBlock.BlockSize;
174     FollowingBlock = &FollowingFreeBlock.getBlockAfter();
175     
176     // Tell the block after the block we are coallescing that this block is
177     // allocated.
178     FollowingBlock->PrevAllocated = 1;
179   }
180   
181   assert(FollowingBlock->ThisAllocated && "Missed coallescing?");
182   
183   if (FreeRangeHeader *PrevFreeBlock = getFreeBlockBefore()) {
184     PrevFreeBlock->GrowBlock(PrevFreeBlock->BlockSize + BlockSize);
185     return FreeListToReturn ? FreeListToReturn : PrevFreeBlock;
186   }
187
188   // Otherwise, mark this block free.
189   FreeRangeHeader &FreeBlock = *(FreeRangeHeader*)this;
190   FollowingBlock->PrevAllocated = 0;
191   FreeBlock.ThisAllocated = 0;
192
193   // Link this into the linked list of free blocks.
194   FreeBlock.AddToFreeList(FreeList);
195
196   // Add a marker at the end of the block, indicating the size of this free
197   // block.
198   FreeBlock.SetEndOfBlockSizeMarker();
199   return FreeListToReturn ? FreeListToReturn : &FreeBlock;
200 }
201
202 /// GrowBlock - The block after this block just got deallocated.  Merge it
203 /// into the current block.
204 void FreeRangeHeader::GrowBlock(uintptr_t NewSize) {
205   assert(NewSize > BlockSize && "Not growing block?");
206   BlockSize = NewSize;
207   SetEndOfBlockSizeMarker();
208   getBlockAfter().PrevAllocated = 0;
209 }
210
211 /// TrimAllocationToSize - If this allocated block is significantly larger
212 /// than NewSize, split it into two pieces (where the former is NewSize
213 /// bytes, including the header), and add the new block to the free list.
214 FreeRangeHeader *MemoryRangeHeader::
215 TrimAllocationToSize(FreeRangeHeader *FreeList, uint64_t NewSize) {
216   assert(ThisAllocated && getBlockAfter().PrevAllocated &&
217          "Cannot deallocate part of an allocated block!");
218
219   // Round up size for alignment of header.
220   unsigned HeaderAlign = __alignof(FreeRangeHeader);
221   NewSize = (NewSize+ (HeaderAlign-1)) & ~(HeaderAlign-1);
222   
223   // Size is now the size of the block we will remove from the start of the
224   // current block.
225   assert(NewSize <= BlockSize &&
226          "Allocating more space from this block than exists!");
227   
228   // If splitting this block will cause the remainder to be too small, do not
229   // split the block.
230   if (BlockSize <= NewSize+FreeRangeHeader::getMinBlockSize())
231     return FreeList;
232   
233   // Otherwise, we splice the required number of bytes out of this block, form
234   // a new block immediately after it, then mark this block allocated.
235   MemoryRangeHeader &FormerNextBlock = getBlockAfter();
236   
237   // Change the size of this block.
238   BlockSize = NewSize;
239   
240   // Get the new block we just sliced out and turn it into a free block.
241   FreeRangeHeader &NewNextBlock = (FreeRangeHeader &)getBlockAfter();
242   NewNextBlock.BlockSize = (char*)&FormerNextBlock - (char*)&NewNextBlock;
243   NewNextBlock.ThisAllocated = 0;
244   NewNextBlock.PrevAllocated = 1;
245   NewNextBlock.SetEndOfBlockSizeMarker();
246   FormerNextBlock.PrevAllocated = 0;
247   NewNextBlock.AddToFreeList(FreeList);
248   return &NewNextBlock;
249 }
250
251  
252 namespace {  
253   /// JITMemoryManager - Manage memory for the JIT code generation in a logical,
254   /// sane way.  This splits a large block of MAP_NORESERVE'd memory into two
255   /// sections, one for function stubs, one for the functions themselves.  We
256   /// have to do this because we may need to emit a function stub while in the
257   /// middle of emitting a function, and we don't know how large the function we
258   /// are emitting is.  This never bothers to release the memory, because when
259   /// we are ready to destroy the JIT, the program exits.
260   class JITMemoryManager {
261     std::vector<sys::MemoryBlock> Blocks; // Memory blocks allocated by the JIT
262     FreeRangeHeader *FreeMemoryList;      // Circular list of free blocks.
263     
264     // When emitting code into a memory block, this is the block.
265     MemoryRangeHeader *CurBlock;
266     
267     unsigned char *CurStubPtr, *StubBase;
268     unsigned char *GOTBase;      // Target Specific reserved memory
269
270     // Centralize memory block allocation.
271     sys::MemoryBlock getNewMemoryBlock(unsigned size);
272     
273     std::map<const Function*, MemoryRangeHeader*> FunctionBlocks;
274   public:
275     JITMemoryManager(bool useGOT);
276     ~JITMemoryManager();
277
278     inline unsigned char *allocateStub(unsigned StubSize);
279     
280     /// startFunctionBody - When a function starts, allocate a block of free
281     /// executable memory, returning a pointer to it and its actual size.
282     unsigned char *startFunctionBody(uintptr_t &ActualSize) {
283       CurBlock = FreeMemoryList;
284       
285       // Allocate the entire memory block.
286       FreeMemoryList = FreeMemoryList->AllocateBlock();
287       ActualSize = CurBlock->BlockSize-sizeof(MemoryRangeHeader);
288       return (unsigned char *)(CurBlock+1);
289     }
290     
291     /// endFunctionBody - The function F is now allocated, and takes the memory
292     /// in the range [FunctionStart,FunctionEnd).
293     void endFunctionBody(const Function *F, unsigned char *FunctionStart,
294                          unsigned char *FunctionEnd) {
295       assert(FunctionEnd > FunctionStart);
296       assert(FunctionStart == (unsigned char *)(CurBlock+1) &&
297              "Mismatched function start/end!");
298       
299       uintptr_t BlockSize = FunctionEnd - (unsigned char *)CurBlock;
300       FunctionBlocks[F] = CurBlock;
301
302       // Release the memory at the end of this block that isn't needed.
303       FreeMemoryList =CurBlock->TrimAllocationToSize(FreeMemoryList, BlockSize);
304     }
305     
306     unsigned char *getGOTBase() const {
307       return GOTBase;
308     }
309     bool isManagingGOT() const {
310       return GOTBase != NULL;
311     }
312     
313     /// deallocateMemForFunction - Deallocate all memory for the specified
314     /// function body.
315     void deallocateMemForFunction(const Function *F) {
316       std::map<const Function*, MemoryRangeHeader*>::iterator
317         I = FunctionBlocks.find(F);
318       if (I == FunctionBlocks.end()) return;
319       
320       // Find the block that is allocated for this function.
321       MemoryRangeHeader *MemRange = I->second;
322       assert(MemRange->ThisAllocated && "Block isn't allocated!");
323       
324       // Fill the buffer with garbage!
325       DEBUG(memset(MemRange+1, 0xCD, MemRange->BlockSize-sizeof(*MemRange)));
326       
327       // Free the memory.
328       FreeMemoryList = MemRange->FreeBlock(FreeMemoryList);
329       
330       // Finally, remove this entry from FunctionBlocks.
331       FunctionBlocks.erase(I);
332     }
333   };
334 }
335
336 JITMemoryManager::JITMemoryManager(bool useGOT) {
337   // Allocate a 16M block of memory for functions.
338   sys::MemoryBlock MemBlock = getNewMemoryBlock(16 << 20);
339
340   unsigned char *MemBase = reinterpret_cast<unsigned char*>(MemBlock.base());
341
342   // Allocate stubs backwards from the base, allocate functions forward
343   // from the base.
344   StubBase   = MemBase;
345   CurStubPtr = MemBase + 512*1024; // Use 512k for stubs, working backwards.
346   
347   // We set up the memory chunk with 4 mem regions, like this:
348   //  [ START
349   //    [ Free      #0 ] -> Large space to allocate functions from.
350   //    [ Allocated #1 ] -> Tiny space to separate regions.
351   //    [ Free      #2 ] -> Tiny space so there is always at least 1 free block.
352   //    [ Allocated #3 ] -> Tiny space to prevent looking past end of block.
353   //  END ]
354   //
355   // The last three blocks are never deallocated or touched.
356   
357   // Add MemoryRangeHeader to the end of the memory region, indicating that
358   // the space after the block of memory is allocated.  This is block #3.
359   MemoryRangeHeader *Mem3 = (MemoryRangeHeader*)(MemBase+MemBlock.size())-1;
360   Mem3->ThisAllocated = 1;
361   Mem3->PrevAllocated = 0;
362   Mem3->BlockSize     = 0;
363   
364   /// Add a tiny free region so that the free list always has one entry.
365   FreeRangeHeader *Mem2 = 
366     (FreeRangeHeader *)(((char*)Mem3)-FreeRangeHeader::getMinBlockSize());
367   Mem2->ThisAllocated = 0;
368   Mem2->PrevAllocated = 1;
369   Mem2->BlockSize     = FreeRangeHeader::getMinBlockSize();
370   Mem2->SetEndOfBlockSizeMarker();
371   Mem2->Prev = Mem2;   // Mem2 *is* the free list for now.
372   Mem2->Next = Mem2;
373
374   /// Add a tiny allocated region so that Mem2 is never coallesced away.
375   MemoryRangeHeader *Mem1 = (MemoryRangeHeader*)Mem2-1;
376   Mem1->ThisAllocated = 1;
377   Mem1->PrevAllocated = 0;
378   Mem1->BlockSize     = (char*)Mem2 - (char*)Mem1;
379   
380   // Add a FreeRangeHeader to the start of the function body region, indicating
381   // that the space is free.  Mark the previous block allocated so we never look
382   // at it.
383   FreeRangeHeader *Mem0 = (FreeRangeHeader*)CurStubPtr;
384   Mem0->ThisAllocated = 0;
385   Mem0->PrevAllocated = 1;
386   Mem0->BlockSize = (char*)Mem1-(char*)Mem0;
387   Mem0->SetEndOfBlockSizeMarker();
388   Mem0->AddToFreeList(Mem2);
389   
390   // Start out with the freelist pointing to Mem0.
391   FreeMemoryList = Mem0;
392
393   // Allocate the GOT.
394   GOTBase = NULL;
395   if (useGOT) GOTBase = new unsigned char[sizeof(void*) * 8192];
396 }
397
398 JITMemoryManager::~JITMemoryManager() {
399   for (unsigned i = 0, e = Blocks.size(); i != e; ++i)
400     sys::Memory::ReleaseRWX(Blocks[i]);
401   
402   delete[] GOTBase;
403   Blocks.clear();
404 }
405
406 unsigned char *JITMemoryManager::allocateStub(unsigned StubSize) {
407   CurStubPtr -= StubSize;
408   if (CurStubPtr < StubBase) {
409     // FIXME: allocate a new block
410     std::cerr << "JIT ran out of memory for function stubs!\n";
411     abort();
412   }
413   return CurStubPtr;
414 }
415
416 sys::MemoryBlock JITMemoryManager::getNewMemoryBlock(unsigned size) {
417   try {
418     // Allocate a new block close to the last one.
419     const sys::MemoryBlock *BOld = Blocks.empty() ? 0 : &Blocks.front();
420     sys::MemoryBlock B = sys::Memory::AllocateRWX(size, BOld);
421     Blocks.push_back(B);
422     return B;
423   } catch (std::string &err) {
424     std::cerr << "Allocation failed when allocating new memory in the JIT\n";
425     std::cerr << err << "\n";
426     abort();
427   }
428 }
429
430 //===----------------------------------------------------------------------===//
431 // JIT lazy compilation code.
432 //
433 namespace {
434   class JITResolverState {
435   private:
436     /// FunctionToStubMap - Keep track of the stub created for a particular
437     /// function so that we can reuse them if necessary.
438     std::map<Function*, void*> FunctionToStubMap;
439
440     /// StubToFunctionMap - Keep track of the function that each stub
441     /// corresponds to.
442     std::map<void*, Function*> StubToFunctionMap;
443
444   public:
445     std::map<Function*, void*>& getFunctionToStubMap(const MutexGuard& locked) {
446       assert(locked.holds(TheJIT->lock));
447       return FunctionToStubMap;
448     }
449
450     std::map<void*, Function*>& getStubToFunctionMap(const MutexGuard& locked) {
451       assert(locked.holds(TheJIT->lock));
452       return StubToFunctionMap;
453     }
454   };
455
456   /// JITResolver - Keep track of, and resolve, call sites for functions that
457   /// have not yet been compiled.
458   class JITResolver {
459     /// MCE - The MachineCodeEmitter to use to emit stubs with.
460     MachineCodeEmitter &MCE;
461
462     /// LazyResolverFn - The target lazy resolver function that we actually
463     /// rewrite instructions to use.
464     TargetJITInfo::LazyResolverFn LazyResolverFn;
465
466     JITResolverState state;
467
468     /// ExternalFnToStubMap - This is the equivalent of FunctionToStubMap for
469     /// external functions.
470     std::map<void*, void*> ExternalFnToStubMap;
471
472     //map addresses to indexes in the GOT
473     std::map<void*, unsigned> revGOTMap;
474     unsigned nextGOTIndex;
475
476   public:
477     JITResolver(MachineCodeEmitter &mce) : MCE(mce), nextGOTIndex(0) {
478       LazyResolverFn =
479         TheJIT->getJITInfo().getLazyResolverFunction(JITCompilerFn);
480     }
481
482     /// getFunctionStub - This returns a pointer to a function stub, creating
483     /// one on demand as needed.
484     void *getFunctionStub(Function *F);
485
486     /// getExternalFunctionStub - Return a stub for the function at the
487     /// specified address, created lazily on demand.
488     void *getExternalFunctionStub(void *FnAddr);
489
490     /// AddCallbackAtLocation - If the target is capable of rewriting an
491     /// instruction without the use of a stub, record the location of the use so
492     /// we know which function is being used at the location.
493     void *AddCallbackAtLocation(Function *F, void *Location) {
494       MutexGuard locked(TheJIT->lock);
495       /// Get the target-specific JIT resolver function.
496       state.getStubToFunctionMap(locked)[Location] = F;
497       return (void*)(intptr_t)LazyResolverFn;
498     }
499
500     /// getGOTIndexForAddress - Return a new or existing index in the GOT for
501     /// and address.  This function only manages slots, it does not manage the
502     /// contents of the slots or the memory associated with the GOT.
503     unsigned getGOTIndexForAddr(void* addr);
504
505     /// JITCompilerFn - This function is called to resolve a stub to a compiled
506     /// address.  If the LLVM Function corresponding to the stub has not yet
507     /// been compiled, this function compiles it first.
508     static void *JITCompilerFn(void *Stub);
509   };
510 }
511
512 /// getJITResolver - This function returns the one instance of the JIT resolver.
513 ///
514 static JITResolver &getJITResolver(MachineCodeEmitter *MCE = 0) {
515   static JITResolver TheJITResolver(*MCE);
516   return TheJITResolver;
517 }
518
519 /// getFunctionStub - This returns a pointer to a function stub, creating
520 /// one on demand as needed.
521 void *JITResolver::getFunctionStub(Function *F) {
522   MutexGuard locked(TheJIT->lock);
523
524   // If we already have a stub for this function, recycle it.
525   void *&Stub = state.getFunctionToStubMap(locked)[F];
526   if (Stub) return Stub;
527
528   // Call the lazy resolver function unless we already KNOW it is an external
529   // function, in which case we just skip the lazy resolution step.
530   void *Actual = (void*)(intptr_t)LazyResolverFn;
531   if (F->isExternal() && F->hasExternalLinkage())
532     Actual = TheJIT->getPointerToFunction(F);
533
534   // Otherwise, codegen a new stub.  For now, the stub will call the lazy
535   // resolver function.
536   Stub = TheJIT->getJITInfo().emitFunctionStub(Actual, MCE);
537
538   if (Actual != (void*)(intptr_t)LazyResolverFn) {
539     // If we are getting the stub for an external function, we really want the
540     // address of the stub in the GlobalAddressMap for the JIT, not the address
541     // of the external function.
542     TheJIT->updateGlobalMapping(F, Stub);
543   }
544
545   DEBUG(std::cerr << "JIT: Stub emitted at [" << Stub << "] for function '"
546                   << F->getName() << "'\n");
547
548   // Finally, keep track of the stub-to-Function mapping so that the
549   // JITCompilerFn knows which function to compile!
550   state.getStubToFunctionMap(locked)[Stub] = F;
551   return Stub;
552 }
553
554 /// getExternalFunctionStub - Return a stub for the function at the
555 /// specified address, created lazily on demand.
556 void *JITResolver::getExternalFunctionStub(void *FnAddr) {
557   // If we already have a stub for this function, recycle it.
558   void *&Stub = ExternalFnToStubMap[FnAddr];
559   if (Stub) return Stub;
560
561   Stub = TheJIT->getJITInfo().emitFunctionStub(FnAddr, MCE);
562   DEBUG(std::cerr << "JIT: Stub emitted at [" << Stub
563         << "] for external function at '" << FnAddr << "'\n");
564   return Stub;
565 }
566
567 unsigned JITResolver::getGOTIndexForAddr(void* addr) {
568   unsigned idx = revGOTMap[addr];
569   if (!idx) {
570     idx = ++nextGOTIndex;
571     revGOTMap[addr] = idx;
572     DEBUG(std::cerr << "Adding GOT entry " << idx
573           << " for addr " << addr << "\n");
574     //    ((void**)MemMgr.getGOTBase())[idx] = addr;
575   }
576   return idx;
577 }
578
579 /// JITCompilerFn - This function is called when a lazy compilation stub has
580 /// been entered.  It looks up which function this stub corresponds to, compiles
581 /// it if necessary, then returns the resultant function pointer.
582 void *JITResolver::JITCompilerFn(void *Stub) {
583   JITResolver &JR = getJITResolver();
584
585   MutexGuard locked(TheJIT->lock);
586
587   // The address given to us for the stub may not be exactly right, it might be
588   // a little bit after the stub.  As such, use upper_bound to find it.
589   std::map<void*, Function*>::iterator I =
590     JR.state.getStubToFunctionMap(locked).upper_bound(Stub);
591   assert(I != JR.state.getStubToFunctionMap(locked).begin() &&
592          "This is not a known stub!");
593   Function *F = (--I)->second;
594
595   // We might like to remove the stub from the StubToFunction map.
596   // We can't do that! Multiple threads could be stuck, waiting to acquire the
597   // lock above. As soon as the 1st function finishes compiling the function,
598   // the next one will be released, and needs to be able to find the function it
599   // needs to call.
600   //JR.state.getStubToFunctionMap(locked).erase(I);
601
602   DEBUG(std::cerr << "JIT: Lazily resolving function '" << F->getName()
603                   << "' In stub ptr = " << Stub << " actual ptr = "
604                   << I->first << "\n");
605
606   void *Result = TheJIT->getPointerToFunction(F);
607
608   // We don't need to reuse this stub in the future, as F is now compiled.
609   JR.state.getFunctionToStubMap(locked).erase(F);
610
611   // FIXME: We could rewrite all references to this stub if we knew them.
612
613   // What we will do is set the compiled function address to map to the
614   // same GOT entry as the stub so that later clients may update the GOT
615   // if they see it still using the stub address.
616   // Note: this is done so the Resolver doesn't have to manage GOT memory
617   // Do this without allocating map space if the target isn't using a GOT
618   if(JR.revGOTMap.find(Stub) != JR.revGOTMap.end())
619     JR.revGOTMap[Result] = JR.revGOTMap[Stub];
620
621   return Result;
622 }
623
624
625 //===----------------------------------------------------------------------===//
626 // JITEmitter code.
627 //
628 namespace {
629   /// JITEmitter - The JIT implementation of the MachineCodeEmitter, which is
630   /// used to output functions to memory for execution.
631   class JITEmitter : public MachineCodeEmitter {
632     JITMemoryManager MemMgr;
633
634     // When outputting a function stub in the context of some other function, we
635     // save BufferBegin/BufferEnd/CurBufferPtr here.
636     unsigned char *SavedBufferBegin, *SavedBufferEnd, *SavedCurBufferPtr;
637
638     /// Relocations - These are the relocations that the function needs, as
639     /// emitted.
640     std::vector<MachineRelocation> Relocations;
641     
642     /// MBBLocations - This vector is a mapping from MBB ID's to their address.
643     /// It is filled in by the StartMachineBasicBlock callback and queried by
644     /// the getMachineBasicBlockAddress callback.
645     std::vector<intptr_t> MBBLocations;
646
647     /// ConstantPool - The constant pool for the current function.
648     ///
649     MachineConstantPool *ConstantPool;
650
651     /// ConstantPoolBase - A pointer to the first entry in the constant pool.
652     ///
653     void *ConstantPoolBase;
654
655     /// ConstantPool - The constant pool for the current function.
656     ///
657     MachineJumpTableInfo *JumpTable;
658     
659     /// JumpTableBase - A pointer to the first entry in the jump table.
660     ///
661     void *JumpTableBase;
662 public:
663     JITEmitter(JIT &jit) : MemMgr(jit.getJITInfo().needsGOT()) {
664       TheJIT = &jit;
665       DEBUG(if (MemMgr.isManagingGOT()) std::cerr << "JIT is managing a GOT\n");
666     }
667
668     virtual void startFunction(MachineFunction &F);
669     virtual bool finishFunction(MachineFunction &F);
670     
671     void emitConstantPool(MachineConstantPool *MCP);
672     void initJumpTableInfo(MachineJumpTableInfo *MJTI);
673     void emitJumpTableInfo(MachineJumpTableInfo *MJTI);
674     
675     virtual void startFunctionStub(unsigned StubSize);
676     virtual void* finishFunctionStub(const Function *F);
677
678     virtual void addRelocation(const MachineRelocation &MR) {
679       Relocations.push_back(MR);
680     }
681     
682     virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) {
683       if (MBBLocations.size() <= (unsigned)MBB->getNumber())
684         MBBLocations.resize((MBB->getNumber()+1)*2);
685       MBBLocations[MBB->getNumber()] = getCurrentPCValue();
686     }
687
688     virtual intptr_t getConstantPoolEntryAddress(unsigned Entry) const;
689     virtual intptr_t getJumpTableEntryAddress(unsigned Entry) const;
690     
691     virtual intptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const {
692       assert(MBBLocations.size() > (unsigned)MBB->getNumber() && 
693              MBBLocations[MBB->getNumber()] && "MBB not emitted!");
694       return MBBLocations[MBB->getNumber()];
695     }
696
697     /// deallocateMemForFunction - Deallocate all memory for the specified
698     /// function body.
699     void deallocateMemForFunction(Function *F) {
700       MemMgr.deallocateMemForFunction(F);
701     }
702   private:
703     void *getPointerToGlobal(GlobalValue *GV, void *Reference, bool NoNeedStub);
704   };
705 }
706
707 void *JITEmitter::getPointerToGlobal(GlobalValue *V, void *Reference,
708                                      bool DoesntNeedStub) {
709   if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
710     /// FIXME: If we straightened things out, this could actually emit the
711     /// global immediately instead of queuing it for codegen later!
712     return TheJIT->getOrEmitGlobalVariable(GV);
713   }
714
715   // If we have already compiled the function, return a pointer to its body.
716   Function *F = cast<Function>(V);
717   void *ResultPtr = TheJIT->getPointerToGlobalIfAvailable(F);
718   if (ResultPtr) return ResultPtr;
719
720   if (F->hasExternalLinkage() && F->isExternal()) {
721     // If this is an external function pointer, we can force the JIT to
722     // 'compile' it, which really just adds it to the map.
723     if (DoesntNeedStub)
724       return TheJIT->getPointerToFunction(F);
725
726     return getJITResolver(this).getFunctionStub(F);
727   }
728
729   // Okay, the function has not been compiled yet, if the target callback
730   // mechanism is capable of rewriting the instruction directly, prefer to do
731   // that instead of emitting a stub.
732   if (DoesntNeedStub)
733     return getJITResolver(this).AddCallbackAtLocation(F, Reference);
734
735   // Otherwise, we have to emit a lazy resolving stub.
736   return getJITResolver(this).getFunctionStub(F);
737 }
738
739 void JITEmitter::startFunction(MachineFunction &F) {
740   uintptr_t ActualSize;
741   BufferBegin = CurBufferPtr = MemMgr.startFunctionBody(ActualSize);
742   BufferEnd = BufferBegin+ActualSize;
743   
744   emitConstantPool(F.getConstantPool());
745   initJumpTableInfo(F.getJumpTableInfo());
746
747   // About to start emitting the machine code for the function.
748   emitAlignment(std::max(F.getFunction()->getAlignment(), 8U));
749   TheJIT->updateGlobalMapping(F.getFunction(), CurBufferPtr);
750   
751   MBBLocations.clear();
752 }
753
754 bool JITEmitter::finishFunction(MachineFunction &F) {
755   if (CurBufferPtr == BufferEnd) {
756     // FIXME: Allocate more space, then try again.
757     std::cerr << "JIT: Ran out of space for generated machine code!\n";
758     abort();
759   }
760   
761   emitJumpTableInfo(F.getJumpTableInfo());
762   
763   MemMgr.endFunctionBody(F.getFunction(), BufferBegin, CurBufferPtr);
764   NumBytes += getCurrentPCOffset();
765
766   if (!Relocations.empty()) {
767     NumRelos += Relocations.size();
768
769     // Resolve the relocations to concrete pointers.
770     for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
771       MachineRelocation &MR = Relocations[i];
772       void *ResultPtr;
773       if (MR.isString()) {
774         ResultPtr = TheJIT->getPointerToNamedFunction(MR.getString());
775
776         // If the target REALLY wants a stub for this function, emit it now.
777         if (!MR.doesntNeedFunctionStub())
778           ResultPtr = getJITResolver(this).getExternalFunctionStub(ResultPtr);
779       } else if (MR.isGlobalValue()) {
780         ResultPtr = getPointerToGlobal(MR.getGlobalValue(),
781                                        BufferBegin+MR.getMachineCodeOffset(),
782                                        MR.doesntNeedFunctionStub());
783       } else {
784         assert(MR.isConstantPoolIndex());
785         ResultPtr=(void*)getConstantPoolEntryAddress(MR.getConstantPoolIndex());
786       }
787
788       MR.setResultPointer(ResultPtr);
789
790       // if we are managing the GOT and the relocation wants an index,
791       // give it one
792       if (MemMgr.isManagingGOT() && MR.isGOTRelative()) {
793         unsigned idx = getJITResolver(this).getGOTIndexForAddr(ResultPtr);
794         MR.setGOTIndex(idx);
795         if (((void**)MemMgr.getGOTBase())[idx] != ResultPtr) {
796           DEBUG(std::cerr << "GOT was out of date for " << ResultPtr
797                 << " pointing at " << ((void**)MemMgr.getGOTBase())[idx]
798                 << "\n");
799           ((void**)MemMgr.getGOTBase())[idx] = ResultPtr;
800         }
801       }
802     }
803
804     TheJIT->getJITInfo().relocate(BufferBegin, &Relocations[0],
805                                   Relocations.size(), MemMgr.getGOTBase());
806   }
807
808   // Update the GOT entry for F to point to the new code.
809   if(MemMgr.isManagingGOT()) {
810     unsigned idx = getJITResolver(this).getGOTIndexForAddr((void*)BufferBegin);
811     if (((void**)MemMgr.getGOTBase())[idx] != (void*)BufferBegin) {
812       DEBUG(std::cerr << "GOT was out of date for " << (void*)BufferBegin
813             << " pointing at " << ((void**)MemMgr.getGOTBase())[idx] << "\n");
814       ((void**)MemMgr.getGOTBase())[idx] = (void*)BufferBegin;
815     }
816   }
817
818   DEBUG(void *FnStart = TheJIT->getPointerToGlobalIfAvailable(F.getFunction());
819         char *FnEnd   = (char*)getCurrentPCOffset();
820         std::cerr << "JIT: Finished CodeGen of [" << FnStart
821                   << "] Function: " << F.getFunction()->getName()
822                   << ": " << (FnEnd-(char*)FnStart) << " bytes of text, "
823                   << Relocations.size() << " relocations\n");
824   Relocations.clear();
825   return false;
826 }
827
828 void JITEmitter::emitConstantPool(MachineConstantPool *MCP) {
829   const std::vector<MachineConstantPoolEntry> &Constants = MCP->getConstants();
830   if (Constants.empty()) return;
831
832   unsigned Size = Constants.back().Offset;
833   Size += TheJIT->getTargetData()->getTypeSize(Constants.back().Val->getType());
834
835   ConstantPoolBase = allocateSpace(Size, 1 << MCP->getConstantPoolAlignment());
836   ConstantPool = MCP;
837
838   if (ConstantPoolBase == 0) return;  // Buffer overflow.
839
840   // Initialize the memory for all of the constant pool entries.
841   for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
842     void *CAddr = (char*)ConstantPoolBase+Constants[i].Offset;
843     TheJIT->InitializeMemory(Constants[i].Val, CAddr);
844   }
845 }
846
847 void JITEmitter::initJumpTableInfo(MachineJumpTableInfo *MJTI) {
848   const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
849   if (JT.empty()) return;
850   
851   unsigned NumEntries = 0;
852   for (unsigned i = 0, e = JT.size(); i != e; ++i)
853     NumEntries += JT[i].MBBs.size();
854
855   unsigned EntrySize = MJTI->getEntrySize();
856
857   // Just allocate space for all the jump tables now.  We will fix up the actual
858   // MBB entries in the tables after we emit the code for each block, since then
859   // we will know the final locations of the MBBs in memory.
860   JumpTable = MJTI;
861   JumpTableBase = allocateSpace(NumEntries * EntrySize, MJTI->getAlignment());
862 }
863
864 void JITEmitter::emitJumpTableInfo(MachineJumpTableInfo *MJTI) {
865   const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
866   if (JT.empty() || JumpTableBase == 0) return;
867
868   unsigned Offset = 0;
869   assert(MJTI->getEntrySize() == sizeof(void*) && "Cross JIT'ing?");
870   
871   // For each jump table, map each target in the jump table to the address of 
872   // an emitted MachineBasicBlock.
873   intptr_t *SlotPtr = (intptr_t*)JumpTableBase;
874
875   for (unsigned i = 0, e = JT.size(); i != e; ++i) {
876     const std::vector<MachineBasicBlock*> &MBBs = JT[i].MBBs;
877     // Store the address of the basic block for this jump table slot in the
878     // memory we allocated for the jump table in 'initJumpTableInfo'
879     for (unsigned mi = 0, me = MBBs.size(); mi != me; ++mi)
880       *SlotPtr++ = getMachineBasicBlockAddress(MBBs[mi]);
881   }
882 }
883
884 void JITEmitter::startFunctionStub(unsigned StubSize) {
885   SavedBufferBegin = BufferBegin;
886   SavedBufferEnd = BufferEnd;
887   SavedCurBufferPtr = CurBufferPtr;
888   
889   BufferBegin = CurBufferPtr = MemMgr.allocateStub(StubSize);
890   BufferEnd = BufferBegin+StubSize+1;
891 }
892
893 void *JITEmitter::finishFunctionStub(const Function *F) {
894   NumBytes += getCurrentPCOffset();
895   std::swap(SavedBufferBegin, BufferBegin);
896   BufferEnd = SavedBufferEnd;
897   CurBufferPtr = SavedCurBufferPtr;
898   return SavedBufferBegin;
899 }
900
901 // getConstantPoolEntryAddress - Return the address of the 'ConstantNum' entry
902 // in the constant pool that was last emitted with the 'emitConstantPool'
903 // method.
904 //
905 intptr_t JITEmitter::getConstantPoolEntryAddress(unsigned ConstantNum) const {
906   assert(ConstantNum < ConstantPool->getConstants().size() &&
907          "Invalid ConstantPoolIndex!");
908   return (intptr_t)ConstantPoolBase +
909          ConstantPool->getConstants()[ConstantNum].Offset;
910 }
911
912 // getJumpTableEntryAddress - Return the address of the JumpTable with index
913 // 'Index' in the jumpp table that was last initialized with 'initJumpTableInfo'
914 //
915 intptr_t JITEmitter::getJumpTableEntryAddress(unsigned Index) const {
916   const std::vector<MachineJumpTableEntry> &JT = JumpTable->getJumpTables();
917   assert(Index < JT.size() && "Invalid jump table index!");
918   
919   unsigned Offset = 0;
920   unsigned EntrySize = JumpTable->getEntrySize();
921   
922   for (unsigned i = 0; i < Index; ++i)
923     Offset += JT[i].MBBs.size() * EntrySize;
924   
925   return (intptr_t)((char *)JumpTableBase + Offset);
926 }
927
928 //===----------------------------------------------------------------------===//
929 //  Public interface to this file
930 //===----------------------------------------------------------------------===//
931
932 MachineCodeEmitter *JIT::createEmitter(JIT &jit) {
933   return new JITEmitter(jit);
934 }
935
936 // getPointerToNamedFunction - This function is used as a global wrapper to
937 // JIT::getPointerToNamedFunction for the purpose of resolving symbols when
938 // bugpoint is debugging the JIT. In that scenario, we are loading an .so and
939 // need to resolve function(s) that are being mis-codegenerated, so we need to
940 // resolve their addresses at runtime, and this is the way to do it.
941 extern "C" {
942   void *getPointerToNamedFunction(const char *Name) {
943     Module &M = TheJIT->getModule();
944     if (Function *F = M.getNamedFunction(Name))
945       return TheJIT->getPointerToFunction(F);
946     return TheJIT->getPointerToNamedFunction(Name);
947   }
948 }
949
950 // getPointerToFunctionOrStub - If the specified function has been
951 // code-gen'd, return a pointer to the function.  If not, compile it, or use
952 // a stub to implement lazy compilation if available.
953 //
954 void *JIT::getPointerToFunctionOrStub(Function *F) {
955   // If we have already code generated the function, just return the address.
956   if (void *Addr = getPointerToGlobalIfAvailable(F))
957     return Addr;
958   
959   // Get a stub if the target supports it
960   return getJITResolver(MCE).getFunctionStub(F);
961 }
962
963 /// freeMachineCodeForFunction - release machine code memory for given Function.
964 ///
965 void JIT::freeMachineCodeForFunction(Function *F) {
966   // Delete translation for this from the ExecutionEngine, so it will get
967   // retranslated next time it is used.
968   updateGlobalMapping(F, 0);
969
970   // Free the actual memory for the function body and related stuff.
971   assert(dynamic_cast<JITEmitter*>(MCE) && "Unexpected MCE?");
972   dynamic_cast<JITEmitter*>(MCE)->deallocateMemForFunction(F);
973 }
974