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