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