Ignore:
Timestamp:
May 2, 2011, 11:30:03 AM (14 years ago)
Author:
[email protected]
Message:

https://bugs.webkit.org/show_bug.cgi?id=59950
Clean up AssemblerBuffer to use a Vector internally.

Reviewed by Oliver Hunt.

AssemblerBuffer handles reallocing a byte array itself - stop that.

  • assembler/ARMAssembler.cpp:

(JSC::ARMAssembler::executableCopy):

  • assembler/AssemblerBuffer.h:

(JSC::AssemblerLabel::AssemblerLabel):
(JSC::AssemblerLabel::labelAtOffset):
(JSC::AssemblerBuffer::AssemblerBuffer):
(JSC::AssemblerBuffer::~AssemblerBuffer):
(JSC::AssemblerBuffer::isAvailable):
(JSC::AssemblerBuffer::ensureSpace):
(JSC::AssemblerBuffer::isAligned):
(JSC::AssemblerBuffer::putIntegral):
(JSC::AssemblerBuffer::putIntegralUnchecked):
(JSC::AssemblerBuffer::putByteUnchecked):
(JSC::AssemblerBuffer::putByte):
(JSC::AssemblerBuffer::putShortUnchecked):
(JSC::AssemblerBuffer::putShort):
(JSC::AssemblerBuffer::putIntUnchecked):
(JSC::AssemblerBuffer::putInt):
(JSC::AssemblerBuffer::putInt64Unchecked):
(JSC::AssemblerBuffer::putInt64):
(JSC::AssemblerBuffer::codeSize):
(JSC::AssemblerBuffer::label):
(JSC::AssemblerBuffer::executableCopy):
(JSC::AssemblerBuffer::rewindToLabel):
(JSC::AssemblerBuffer::debugOffset):
(JSC::AssemblerBuffer::append):
(JSC::AssemblerBuffer::grow):

  • assembler/AssemblerBufferWithConstantPool.h:
  • assembler/MacroAssemblerX86_64.h:

(JSC::MacroAssemblerX86_64::linkCall):

  • assembler/X86Assembler.h:

(JSC::X86Assembler::X86InstructionFormatter::rewindToLabel):

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/assembler/AssemblerBuffer.h

    r85448 r85497  
    3939
    4040    struct AssemblerLabel {
    41         AssemblerLabel(uint32_t offset = std::numeric_limits<uint32_t>::max())
     41        AssemblerLabel()
     42            : m_offset(std::numeric_limits<uint32_t>::max())
     43        {
     44        }
     45
     46        explicit AssemblerLabel(uint32_t offset)
    4247            : m_offset(offset)
    4348        {
     
    4651        bool isSet() const { return (m_offset != std::numeric_limits<uint32_t>::max()); }
    4752
     53        AssemblerLabel labelAtOffset(int offset) const
     54        {
     55            return AssemblerLabel(m_offset + offset);
     56        }
     57
    4858        uint32_t m_offset;
    4959    };
    5060
    5161    class AssemblerBuffer {
    52         static const int inlineCapacity = 128 - sizeof(char*) - 2 * sizeof(int);
     62        static const int inlineCapacity = 128;
    5363    public:
    5464        AssemblerBuffer()
    55             : m_buffer(m_inlineBuffer)
     65            : m_storage(inlineCapacity)
     66            , m_buffer(m_storage.begin())
    5667            , m_capacity(inlineCapacity)
    57             , m_size(0)
     68            , m_index(0)
    5869        {
    59             COMPILE_ASSERT(sizeof(AssemblerBuffer) == 128, AssemblerBuffer_should_be_128_bytes);
    6070        }
    6171
    6272        ~AssemblerBuffer()
    6373        {
    64             if (m_buffer != m_inlineBuffer)
    65                 fastFree(m_buffer);
     74        }
     75
     76        bool isAvailable(int space)
     77        {
     78            return m_index + space <= m_capacity;
    6679        }
    6780
    6881        void ensureSpace(int space)
    6982        {
    70             if (m_size > m_capacity - space)
     83            if (!isAvailable(space))
    7184                grow();
    7285        }
     
    7487        bool isAligned(int alignment) const
    7588        {
    76             return !(m_size & (alignment - 1));
    77         }
    78 
    79         void putByteUnchecked(int value)
    80         {
    81             ASSERT(!(m_size > m_capacity - 4));
    82             m_buffer[m_size] = value;
    83             m_size++;
    84         }
    85 
    86         void putByte(int value)
    87         {
    88             if (m_size > m_capacity - 4)
    89                 grow();
    90             putByteUnchecked(value);
    91         }
    92 
    93         void putShortUnchecked(int value)
    94         {
    95             ASSERT(!(m_size > m_capacity - 4));
    96             *reinterpret_cast_ptr<short*>(&m_buffer[m_size]) = value;
    97             m_size += 2;
    98         }
    99 
    100         void putShort(int value)
    101         {
    102             if (m_size > m_capacity - 4)
    103                 grow();
    104             putShortUnchecked(value);
    105         }
    106 
    107         void putIntUnchecked(int value)
    108         {
    109             ASSERT(!(m_size > m_capacity - 4));
    110             *reinterpret_cast_ptr<int*>(&m_buffer[m_size]) = value;
    111             m_size += 4;
    112         }
    113 
    114         void putInt64Unchecked(int64_t value)
    115         {
    116             ASSERT(!(m_size > m_capacity - 8));
    117             *reinterpret_cast_ptr<int64_t*>(&m_buffer[m_size]) = value;
    118             m_size += 8;
    119         }
    120 
    121         void putInt(int value)
    122         {
    123             if (m_size > m_capacity - 4)
    124                 grow();
    125             putIntUnchecked(value);
     89            return !(m_index & (alignment - 1));
    12690        }
    12791
     
    12993        void putIntegral(IntegralType value)
    13094        {
    131             if (m_size > m_capacity - sizeof(IntegralType))
    132                 grow();
     95            ensureSpace(sizeof(IntegralType));
    13396            putIntegralUnchecked(value);
    13497        }
     
    137100        void putIntegralUnchecked(IntegralType value)
    138101        {
    139             *reinterpret_cast_ptr<IntegralType*>(&m_buffer[m_size]) = value;
    140             m_size += sizeof(IntegralType);
     102            ASSERT(isAvailable(sizeof(IntegralType)));
     103            *reinterpret_cast_ptr<IntegralType*>(m_buffer + m_index) = value;
     104            m_index += sizeof(IntegralType);
    141105        }
     106
     107        void putByteUnchecked(int8_t value) { putIntegralUnchecked(value); }
     108        void putByte(int8_t value) { putIntegral(value); }
     109        void putShortUnchecked(int16_t value) { putIntegralUnchecked(value); }
     110        void putShort(int16_t value) { putIntegral(value); }
     111        void putIntUnchecked(int32_t value) { putIntegralUnchecked(value); }
     112        void putInt(int32_t value) { putIntegral(value); }
     113        void putInt64Unchecked(int64_t value) { putIntegralUnchecked(value); }
     114        void putInt64(int64_t value) { putIntegral(value); }
    142115
    143116        void* data() const
     
    148121        size_t codeSize() const
    149122        {
    150             return m_size;
     123            return m_index;
    151124        }
    152125
    153126        AssemblerLabel label() const
    154127        {
    155             return AssemblerLabel(m_size);
     128            return AssemblerLabel(m_index);
    156129        }
    157130
    158131        void* executableCopy(ExecutablePool* allocator)
    159132        {
    160             if (!m_size)
     133            if (!m_index)
    161134                return 0;
    162135
    163             void* result = allocator->alloc(m_size);
     136            void* result = allocator->alloc(m_index);
    164137
    165138            if (!result)
    166139                return 0;
    167140
    168             ExecutableAllocator::makeWritable(result, m_size);
     141            ExecutableAllocator::makeWritable(result, m_index);
    169142
    170             return memcpy(result, m_buffer, m_size);
     143            return memcpy(result, m_buffer, m_index);
    171144        }
    172145
    173         void rewindToOffset(int offset)
     146        void rewindToLabel(AssemblerLabel label)
    174147        {
    175             ASSERT(offset >= 0);
    176             m_size = offset;
     148            m_index = label.m_offset;
    177149        }
    178150
    179151#ifndef NDEBUG
    180         unsigned debugOffset() { return m_size; }
     152        unsigned debugOffset() { return m_index; }
    181153#endif
    182154
     
    184156        void append(const char* data, int size)
    185157        {
    186             if (m_size > m_capacity - size)
     158            if (!isAvailable(size))
    187159                grow(size);
    188160
    189             memcpy(m_buffer + m_size, data, size);
    190             m_size += size;
     161            memcpy(m_buffer + m_index, data, size);
     162            m_index += size;
    191163        }
    192164
     
    195167            m_capacity += m_capacity / 2 + extraCapacity;
    196168
    197             if (m_buffer == m_inlineBuffer) {
    198                 char* newBuffer = static_cast<char*>(fastMalloc(m_capacity));
    199                 m_buffer = static_cast<char*>(memcpy(newBuffer, m_buffer, m_size));
    200             } else
    201                 m_buffer = static_cast<char*>(fastRealloc(m_buffer, m_capacity));
     169            m_storage.grow(m_capacity);
     170            m_buffer = m_storage.begin();
    202171        }
    203172
    204         char m_inlineBuffer[inlineCapacity];
     173    private:
     174        Vector<char, inlineCapacity> m_storage;
    205175        char* m_buffer;
    206176        int m_capacity;
    207         int m_size;
     177        int m_index;
    208178    };
    209179
Note: See TracChangeset for help on using the changeset viewer.