Undo 58778 but makes the binary dump prettier.
[oota-llvm.git] / lib / ExecutionEngine / JIT / JITMemoryManager.cpp
index 18507acd113c6ed058da31812df37dc1cc114a95..cc072a896c22512eda7e661ec5fbb1d1d7c315ea 100644 (file)
@@ -2,8 +2,8 @@
 //
 //                     The LLVM Compiler Infrastructure
 //
-// This file was developed by Chris Lattner and is distributed under
-// the University of Illinois Open Source License. See LICENSE.TXT for details.
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
 //
 //===----------------------------------------------------------------------===//
 //
 //
 //===----------------------------------------------------------------------===//
 
+#include "llvm/GlobalValue.h"
 #include "llvm/ExecutionEngine/JITMemoryManager.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/System/Memory.h"
 #include <map>
 #include <vector>
+#include <cassert>
+#include <cstdio>
+#include <cstdlib>
+#include <cstring>
 using namespace llvm;
 
 
@@ -198,6 +203,9 @@ TrimAllocationToSize(FreeRangeHeader *FreeList, uint64_t NewSize) {
   assert(ThisAllocated && getBlockAfter().PrevAllocated &&
          "Cannot deallocate part of an allocated block!");
 
+  // Don't allow blocks to be trimmed below minimum required size
+  NewSize = std::max<uint64_t>(FreeRangeHeader::getMinBlockSize(), NewSize);
+
   // Round up size for alignment of header.
   unsigned HeaderAlign = __alignof(FreeRangeHeader);
   NewSize = (NewSize+ (HeaderAlign-1)) & ~(HeaderAlign-1);
@@ -255,13 +263,15 @@ namespace {
     sys::MemoryBlock getNewMemoryBlock(unsigned size);
     
     std::map<const Function*, MemoryRangeHeader*> FunctionBlocks;
+    std::map<const Function*, MemoryRangeHeader*> TableBlocks;
   public:
     DefaultJITMemoryManager();
     ~DefaultJITMemoryManager();
 
     void AllocateGOT();
 
-    unsigned char *allocateStub(unsigned StubSize, unsigned Alignment);
+    unsigned char *allocateStub(const GlobalValue* F, unsigned StubSize,
+                                unsigned Alignment);
     
     /// startFunctionBody - When a function starts, allocate a block of free
     /// executable memory, returning a pointer to it and its actual size.
@@ -281,13 +291,53 @@ namespace {
       assert(FunctionEnd > FunctionStart);
       assert(FunctionStart == (unsigned char *)(CurBlock+1) &&
              "Mismatched function start/end!");
-      
+
       uintptr_t BlockSize = FunctionEnd - (unsigned char *)CurBlock;
       FunctionBlocks[F] = CurBlock;
 
       // Release the memory at the end of this block that isn't needed.
       FreeMemoryList =CurBlock->TrimAllocationToSize(FreeMemoryList, BlockSize);
     }
+
+    /// allocateSpace - Allocate a memory block of the given size.
+    unsigned char *allocateSpace(intptr_t Size, unsigned Alignment) {
+      CurBlock = FreeMemoryList;
+      FreeMemoryList = FreeMemoryList->AllocateBlock();
+
+      unsigned char *result = (unsigned char *)CurBlock+1;
+
+      if (Alignment == 0) Alignment = 1;
+      result = (unsigned char*)(((intptr_t)result+Alignment-1) &
+               ~(intptr_t)(Alignment-1));
+
+      uintptr_t BlockSize = result + Size - (unsigned char *)CurBlock;
+      FreeMemoryList =CurBlock->TrimAllocationToSize(FreeMemoryList, BlockSize);
+
+      return result;
+    }
+
+    /// startExceptionTable - Use startFunctionBody to allocate memory for the 
+    /// function's exception table.
+    unsigned char* startExceptionTable(const Function* F, 
+                                       uintptr_t &ActualSize) {
+      return startFunctionBody(F, ActualSize);
+    }
+
+    /// endExceptionTable - The exception table of F is now allocated, 
+    /// and takes the memory in the range [TableStart,TableEnd).
+    void endExceptionTable(const Function *F, unsigned char *TableStart,
+                           unsigned char *TableEnd, 
+                           unsigned char* FrameRegister) {
+      assert(TableEnd > TableStart);
+      assert(TableStart == (unsigned char *)(CurBlock+1) &&
+             "Mismatched table start/end!");
+      
+      uintptr_t BlockSize = TableEnd - (unsigned char *)CurBlock;
+      TableBlocks[F] = CurBlock;
+
+      // Release the memory at the end of this block that isn't needed.
+      FreeMemoryList =CurBlock->TrimAllocationToSize(FreeMemoryList, BlockSize);
+    }
     
     unsigned char *getGOTBase() const {
       return GOTBase;
@@ -314,15 +364,52 @@ namespace {
       
       // Finally, remove this entry from FunctionBlocks.
       FunctionBlocks.erase(I);
+      
+      I = TableBlocks.find(F);
+      if (I == TableBlocks.end()) return;
+      
+      // Find the block that is allocated for this function.
+      MemRange = I->second;
+      assert(MemRange->ThisAllocated && "Block isn't allocated!");
+      
+      // Fill the buffer with garbage!
+#ifndef NDEBUG
+      memset(MemRange+1, 0xCD, MemRange->BlockSize-sizeof(*MemRange));
+#endif
+      
+      // Free the memory.
+      FreeMemoryList = MemRange->FreeBlock(FreeMemoryList);
+      
+      // Finally, remove this entry from TableBlocks.
+      TableBlocks.erase(I);
+    }
+
+    /// setMemoryWritable - When code generation is in progress,
+    /// the code pages may need permissions changed.
+    void setMemoryWritable(void)
+    {
+      for (unsigned i = 0, e = Blocks.size(); i != e; ++i)
+        sys::Memory::setWritable(Blocks[i]);
+    }
+    /// setMemoryExecutable - When code generation is done and we're ready to
+    /// start execution, the code pages may need permissions changed.
+    void setMemoryExecutable(void)
+    {
+      for (unsigned i = 0, e = Blocks.size(); i != e; ++i)
+        sys::Memory::setExecutable(Blocks[i]);
     }
   };
 }
 
 DefaultJITMemoryManager::DefaultJITMemoryManager() {
   // Allocate a 16M block of memory for functions.
+#if defined(__APPLE__) && defined(__arm__)
+  sys::MemoryBlock MemBlock = getNewMemoryBlock(4 << 20);
+#else
   sys::MemoryBlock MemBlock = getNewMemoryBlock(16 << 20);
+#endif
 
-  unsigned char *MemBase = reinterpret_cast<unsigned char*>(MemBlock.base());
+  unsigned char *MemBase = static_cast<unsigned char*>(MemBlock.base());
 
   // Allocate stubs backwards from the base, allocate functions forward
   // from the base.
@@ -393,7 +480,8 @@ DefaultJITMemoryManager::~DefaultJITMemoryManager() {
   Blocks.clear();
 }
 
-unsigned char *DefaultJITMemoryManager::allocateStub(unsigned StubSize,
+unsigned char *DefaultJITMemoryManager::allocateStub(const GlobalValue* F,
+                                                     unsigned StubSize,
                                                      unsigned Alignment) {
   CurStubPtr -= StubSize;
   CurStubPtr = (unsigned char*)(((intptr_t)CurStubPtr) &