2 * Copyright (C) 2008 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #ifndef AssemblerBuffer_h
27 #define AssemblerBuffer_h
31 #include "JSGlobalData.h"
34 #include <jit/ExecutableAllocator.h>
35 #include <wtf/Assertions.h>
36 #include <wtf/FastMalloc.h>
37 #include <wtf/StdLibExtras.h>
41 struct AssemblerLabel {
43 : m_offset(std::numeric_limits<uint32_t>::max())
47 explicit AssemblerLabel(uint32_t offset)
52 bool isSet() const { return (m_offset != std::numeric_limits<uint32_t>::max()); }
54 AssemblerLabel labelAtOffset(int offset) const
56 return AssemblerLabel(m_offset + offset);
62 class AssemblerBuffer {
63 static const int inlineCapacity = 128;
66 : m_storage(inlineCapacity)
67 , m_buffer(m_storage.begin())
68 , m_capacity(inlineCapacity)
77 bool isAvailable(int space)
79 return m_index + space <= m_capacity;
82 void ensureSpace(int space)
84 if (!isAvailable(space))
88 bool isAligned(int alignment) const
90 return !(m_index & (alignment - 1));
93 template<typename IntegralType>
94 void putIntegral(IntegralType value)
96 ensureSpace(sizeof(IntegralType));
97 putIntegralUnchecked(value);
100 template<typename IntegralType>
101 void putIntegralUnchecked(IntegralType value)
103 ASSERT(isAvailable(sizeof(IntegralType)));
104 *reinterpret_cast_ptr<IntegralType*>(m_buffer + m_index) = value;
105 m_index += sizeof(IntegralType);
108 void putByteUnchecked(int8_t value) { putIntegralUnchecked(value); }
109 void putByte(int8_t value) { putIntegral(value); }
110 void putShortUnchecked(int16_t value) { putIntegralUnchecked(value); }
111 void putShort(int16_t value) { putIntegral(value); }
112 void putIntUnchecked(int32_t value) { putIntegralUnchecked(value); }
113 void putInt(int32_t value) { putIntegral(value); }
114 void putInt64Unchecked(int64_t value) { putIntegralUnchecked(value); }
115 void putInt64(int64_t value) { putIntegral(value); }
122 size_t codeSize() const
127 AssemblerLabel label() const
129 return AssemblerLabel(m_index);
132 PassRefPtr<ExecutableMemoryHandle> executableCopy(JSGlobalData& globalData)
137 RefPtr<ExecutableMemoryHandle> result = globalData.executableAllocator.allocate(globalData, m_index);
142 ExecutableAllocator::makeWritable(result->start(), result->sizeInBytes());
144 memcpy(result->start(), m_buffer, m_index);
146 return result.release();
149 void rewindToLabel(AssemblerLabel label)
151 m_index = label.m_offset;
155 unsigned debugOffset() { return m_index; }
159 void append(const char* data, int size)
161 if (!isAvailable(size))
164 memcpy(m_buffer + m_index, data, size);
168 void grow(int extraCapacity = 0)
170 m_capacity += m_capacity / 2 + extraCapacity;
172 m_storage.grow(m_capacity);
173 m_buffer = m_storage.begin();
177 Vector<char, inlineCapacity> m_storage;
185 #endif // ENABLE(ASSEMBLER)
187 #endif // AssemblerBuffer_h