Restructure LinkBuffer to allow for alternate allocation strategies
authorfpizlo@apple.com <fpizlo@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sun, 20 Oct 2013 02:07:39 +0000 (02:07 +0000)
committerfpizlo@apple.com <fpizlo@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sun, 20 Oct 2013 02:07:39 +0000 (02:07 +0000)
https://bugs.webkit.org/show_bug.cgi?id=123071

Reviewed by Oliver Hunt.

The idea is to eventually allow a LinkBuffer to place the code into an already
allocated region of memory.  That region of memory could be the nop-slide left behind
by a llvm.webkit.patchpoint.

* assembler/ARM64Assembler.h:
(JSC::ARM64Assembler::buffer):
* assembler/AssemblerBuffer.h:
* assembler/LinkBuffer.cpp:
(JSC::LinkBuffer::copyCompactAndLinkCode):
(JSC::LinkBuffer::linkCode):
(JSC::LinkBuffer::allocate):
(JSC::LinkBuffer::shrink):
* assembler/LinkBuffer.h:
(JSC::LinkBuffer::LinkBuffer):
(JSC::LinkBuffer::didFailToAllocate):
* assembler/X86Assembler.h:
(JSC::X86Assembler::buffer):
(JSC::X86Assembler::X86InstructionFormatter::memoryModRM):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@157690 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/JavaScriptCore/ChangeLog
Source/JavaScriptCore/assembler/ARM64Assembler.h
Source/JavaScriptCore/assembler/AssemblerBuffer.h
Source/JavaScriptCore/assembler/LinkBuffer.cpp
Source/JavaScriptCore/assembler/LinkBuffer.h
Source/JavaScriptCore/assembler/X86Assembler.h

index a152088..7a927f7 100644 (file)
@@ -1,3 +1,29 @@
+2013-10-19  Filip Pizlo  <fpizlo@apple.com>
+
+        Restructure LinkBuffer to allow for alternate allocation strategies
+        https://bugs.webkit.org/show_bug.cgi?id=123071
+
+        Reviewed by Oliver Hunt.
+        
+        The idea is to eventually allow a LinkBuffer to place the code into an already
+        allocated region of memory.  That region of memory could be the nop-slide left behind
+        by a llvm.webkit.patchpoint.
+
+        * assembler/ARM64Assembler.h:
+        (JSC::ARM64Assembler::buffer):
+        * assembler/AssemblerBuffer.h:
+        * assembler/LinkBuffer.cpp:
+        (JSC::LinkBuffer::copyCompactAndLinkCode):
+        (JSC::LinkBuffer::linkCode):
+        (JSC::LinkBuffer::allocate):
+        (JSC::LinkBuffer::shrink):
+        * assembler/LinkBuffer.h:
+        (JSC::LinkBuffer::LinkBuffer):
+        (JSC::LinkBuffer::didFailToAllocate):
+        * assembler/X86Assembler.h:
+        (JSC::X86Assembler::buffer):
+        (JSC::X86Assembler::X86InstructionFormatter::memoryModRM):
+
 2013-10-19  Alexey Proskuryakov  <ap@apple.com>
 
         Some includes in JSC seem to use an incorrect style
 2013-10-19  Alexey Proskuryakov  <ap@apple.com>
 
         Some includes in JSC seem to use an incorrect style
index 55f2eaf..11ba171 100644 (file)
@@ -453,6 +453,8 @@ public:
         , m_indexOfTailOfLastWatchpoint(INT_MIN)
     {
     }
         , m_indexOfTailOfLastWatchpoint(INT_MIN)
     {
     }
+    
+    AssemblerBuffer& buffer() { return m_buffer; }
 
     // (HS, LO, HI, LS) -> (AE, B, A, BE)
     // (VS, VC) -> (O, NO)
 
     // (HS, LO, HI, LS) -> (AE, B, A, BE)
     // (VS, VC) -> (O, NO)
index d82c0b9..1881ada 100644 (file)
@@ -130,23 +130,6 @@ namespace JSC {
             return AssemblerLabel(m_index);
         }
 
             return AssemblerLabel(m_index);
         }
 
-        PassRefPtr<ExecutableMemoryHandle> executableCopy(VM& vm, void* ownerUID, JITCompilationEffort effort)
-        {
-            if (!m_index)
-                return 0;
-
-            RefPtr<ExecutableMemoryHandle> result = vm.executableAllocator.allocate(vm, m_index, ownerUID, effort);
-
-            if (!result)
-                return 0;
-
-            ExecutableAllocator::makeWritable(result->start(), result->sizeInBytes());
-
-            memcpy(result->start(), m_buffer, m_index);
-            
-            return result.release();
-        }
-
         unsigned debugOffset() { return m_index; }
 
     protected:
         unsigned debugOffset() { return m_index; }
 
     protected:
index ed6899a..a49417e 100644 (file)
@@ -64,12 +64,7 @@ template <typename InstructionType>
 void LinkBuffer::copyCompactAndLinkCode(void* ownerUID, JITCompilationEffort effort)
 {
     m_initialSize = m_assembler->m_assembler.codeSize();
 void LinkBuffer::copyCompactAndLinkCode(void* ownerUID, JITCompilationEffort effort)
 {
     m_initialSize = m_assembler->m_assembler.codeSize();
-    m_executableMemory = m_vm->executableAllocator.allocate(*m_vm, m_initialSize, ownerUID, effort);
-    if (!m_executableMemory)
-        return;
-    m_code = (uint8_t*)m_executableMemory->start();
-    ASSERT(m_code);
-    ExecutableAllocator::makeWritable(m_code, m_initialSize);
+    allocate(m_initialSize, ownerUID, effort);
     uint8_t* inData = (uint8_t*)m_assembler->unlinkedCode();
     uint8_t* outData = reinterpret_cast<uint8_t*>(m_code);
     int readPtr = 0;
     uint8_t* inData = (uint8_t*)m_assembler->unlinkedCode();
     uint8_t* outData = reinterpret_cast<uint8_t*>(m_code);
     int readPtr = 0;
@@ -125,8 +120,7 @@ void LinkBuffer::copyCompactAndLinkCode(void* ownerUID, JITCompilationEffort eff
     }
 
     jumpsToLink.clear();
     }
 
     jumpsToLink.clear();
-    m_size = writePtr + m_initialSize - readPtr;
-    m_executableMemory->shrink(m_size);
+    shrink(writePtr + m_initialSize - readPtr);
 
 #if DUMP_LINK_STATISTICS
     dumpLinkStatistics(m_code, m_initialSize, m_size);
 
 #if DUMP_LINK_STATISTICS
     dumpLinkStatistics(m_code, m_initialSize, m_size);
@@ -142,12 +136,12 @@ void LinkBuffer::linkCode(void* ownerUID, JITCompilationEffort effort)
 {
     ASSERT(!m_code);
 #if !ENABLE(BRANCH_COMPACTION)
 {
     ASSERT(!m_code);
 #if !ENABLE(BRANCH_COMPACTION)
-    m_executableMemory = m_assembler->m_assembler.executableCopy(*m_vm, ownerUID, effort);
-    if (!m_executableMemory)
+    AssemblerBuffer& buffer = m_assembler->m_assembler.buffer();
+    allocate(buffer.codeSize(), ownerUID, effort);
+    if (!m_didAllocate)
         return;
         return;
-    m_code = m_executableMemory->start();
-    m_size = m_assembler->m_assembler.codeSize();
     ASSERT(m_code);
     ASSERT(m_code);
+    memcpy(m_code, buffer.data(), buffer.codeSize());
 #elif CPU(ARM_THUMB2)
     copyCompactAndLinkCode<uint16_t>(ownerUID, effort);
 #elif CPU(ARM64)
 #elif CPU(ARM_THUMB2)
     copyCompactAndLinkCode<uint16_t>(ownerUID, effort);
 #elif CPU(ARM64)
@@ -155,6 +149,23 @@ void LinkBuffer::linkCode(void* ownerUID, JITCompilationEffort effort)
 #endif
 }
 
 #endif
 }
 
+void LinkBuffer::allocate(size_t initialSize, void* ownerUID, JITCompilationEffort effort)
+{
+    m_executableMemory = m_vm->executableAllocator.allocate(*m_vm, initialSize, ownerUID, effort);
+    if (!m_executableMemory)
+        return;
+    ExecutableAllocator::makeWritable(m_executableMemory->start(), m_executableMemory->sizeInBytes());
+    m_code = m_executableMemory->start();
+    m_size = initialSize;
+    m_didAllocate = true;
+}
+
+void LinkBuffer::shrink(size_t newSize)
+{
+    m_size = newSize;
+    m_executableMemory->shrink(m_size);
+}
+
 void LinkBuffer::performFinalization()
 {
 #ifndef NDEBUG
 void LinkBuffer::performFinalization()
 {
 #ifndef NDEBUG
index 7005278..655a744 100644 (file)
@@ -83,6 +83,7 @@ public:
 #if ENABLE(BRANCH_COMPACTION)
         , m_initialSize(0)
 #endif
 #if ENABLE(BRANCH_COMPACTION)
         , m_initialSize(0)
 #endif
+        , m_didAllocate(false)
         , m_code(0)
         , m_assembler(masm)
         , m_vm(&vm)
         , m_code(0)
         , m_assembler(masm)
         , m_vm(&vm)
@@ -99,7 +100,7 @@ public:
     
     bool didFailToAllocate() const
     {
     
     bool didFailToAllocate() const
     {
-        return !m_executableMemory;
+        return !m_didAllocate;
     }
 
     bool isValid() const
     }
 
     bool isValid() const
@@ -241,6 +242,9 @@ private:
     {
         return m_code;
     }
     {
         return m_code;
     }
+    
+    void allocate(size_t initialSize, void* ownerUID, JITCompilationEffort);
+    void shrink(size_t newSize);
 
     void linkCode(void* ownerUID, JITCompilationEffort);
 #if ENABLE(BRANCH_COMPACTION)
 
     void linkCode(void* ownerUID, JITCompilationEffort);
 #if ENABLE(BRANCH_COMPACTION)
@@ -263,6 +267,7 @@ private:
 #if ENABLE(BRANCH_COMPACTION)
     size_t m_initialSize;
 #endif
 #if ENABLE(BRANCH_COMPACTION)
     size_t m_initialSize;
 #endif
+    bool m_didAllocate;
     void* m_code;
     MacroAssembler* m_assembler;
     VM* m_vm;
     void* m_code;
     MacroAssembler* m_assembler;
     VM* m_vm;
index f7738a1..23cbaf5 100644 (file)
@@ -333,6 +333,8 @@ public:
         , m_indexOfTailOfLastWatchpoint(INT_MIN)
     {
     }
         , m_indexOfTailOfLastWatchpoint(INT_MIN)
     {
     }
+    
+    AssemblerBuffer& buffer() { return m_formatter.m_buffer; }
 
     // Stack operations:
 
 
     // Stack operations:
 
@@ -2122,11 +2124,6 @@ public:
         return b.m_offset - a.m_offset;
     }
     
         return b.m_offset - a.m_offset;
     }
     
-    PassRefPtr<ExecutableMemoryHandle> executableCopy(VM& vm, void* ownerUID, JITCompilationEffort effort)
-    {
-        return m_formatter.executableCopy(vm, ownerUID, effort);
-    }
-
     unsigned debugOffset() { return m_formatter.debugOffset(); }
 
     void nop()
     unsigned debugOffset() { return m_formatter.debugOffset(); }
 
     void nop()
@@ -2480,11 +2477,6 @@ private:
         bool isAligned(int alignment) const { return m_buffer.isAligned(alignment); }
         void* data() const { return m_buffer.data(); }
 
         bool isAligned(int alignment) const { return m_buffer.isAligned(alignment); }
         void* data() const { return m_buffer.data(); }
 
-        PassRefPtr<ExecutableMemoryHandle> executableCopy(VM& vm, void* ownerUID, JITCompilationEffort effort)
-        {
-            return m_buffer.executableCopy(vm, ownerUID, effort);
-        }
-
         unsigned debugOffset() { return m_buffer.debugOffset(); }
 
     private:
         unsigned debugOffset() { return m_buffer.debugOffset(); }
 
     private:
@@ -2658,6 +2650,7 @@ private:
         }
 #endif
 
         }
 #endif
 
+    public:
         AssemblerBuffer m_buffer;
     } m_formatter;
     int m_indexOfLastWatchpoint;
         AssemblerBuffer m_buffer;
     } m_formatter;
     int m_indexOfLastWatchpoint;