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