ggaren@apple.com | 58f417e | 2008-11-17 06:27:06 +0000 | [diff] [blame] | 1 | /* |
benjamin@webkit.org | f6de077 | 2014-07-15 23:59:14 +0000 | [diff] [blame] | 2 | * Copyright (C) 2008, 2012, 2014 Apple Inc. All rights reserved. |
ggaren@apple.com | 58f417e | 2008-11-17 06:27:06 +0000 | [diff] [blame] | 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 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. |
| 12 | * |
| 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. |
| 24 | */ |
| 25 | |
ryanhaddad@apple.com | 22104f5 | 2016-09-28 17:08:17 +0000 | [diff] [blame] | 26 | #pragma once |
ggaren@apple.com | 58f417e | 2008-11-17 06:27:06 +0000 | [diff] [blame] | 27 | |
ggaren@apple.com | 58f417e | 2008-11-17 06:27:06 +0000 | [diff] [blame] | 28 | #if ENABLE(ASSEMBLER) |
| 29 | |
fpizlo@apple.com | b75911b | 2012-06-13 20:53:52 +0000 | [diff] [blame] | 30 | #include "ExecutableAllocator.h" |
fpizlo@apple.com | 0f25ee8 | 2012-03-01 05:46:20 +0000 | [diff] [blame] | 31 | #include "JITCompilationEffort.h" |
barraclough@apple.com | 780b75b | 2008-12-17 01:27:13 +0000 | [diff] [blame] | 32 | #include "stdint.h" |
ggaren@apple.com | 3e9d413 | 2008-11-18 03:25:03 +0000 | [diff] [blame] | 33 | #include <string.h> |
ggaren@apple.com | 58f417e | 2008-11-17 06:27:06 +0000 | [diff] [blame] | 34 | #include <wtf/Assertions.h> |
| 35 | #include <wtf/FastMalloc.h> |
loki@webkit.org | 7534a3f | 2010-08-13 10:14:36 +0000 | [diff] [blame] | 36 | #include <wtf/StdLibExtras.h> |
ggaren@apple.com | 58f417e | 2008-11-17 06:27:06 +0000 | [diff] [blame] | 37 | |
| 38 | namespace JSC { |
| 39 | |
barraclough@apple.com | 4836c7a | 2011-05-01 22:20:59 +0000 | [diff] [blame] | 40 | struct AssemblerLabel { |
barraclough@apple.com | c5390ae | 2011-05-02 18:30:03 +0000 | [diff] [blame] | 41 | AssemblerLabel() |
| 42 | : m_offset(std::numeric_limits<uint32_t>::max()) |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | explicit AssemblerLabel(uint32_t offset) |
barraclough@apple.com | 4836c7a | 2011-05-01 22:20:59 +0000 | [diff] [blame] | 47 | : m_offset(offset) |
| 48 | { |
| 49 | } |
| 50 | |
| 51 | bool isSet() const { return (m_offset != std::numeric_limits<uint32_t>::max()); } |
| 52 | |
barraclough@apple.com | c5390ae | 2011-05-02 18:30:03 +0000 | [diff] [blame] | 53 | AssemblerLabel labelAtOffset(int offset) const |
| 54 | { |
| 55 | return AssemblerLabel(m_offset + offset); |
| 56 | } |
| 57 | |
sbarati@apple.com | d3d0c00 | 2016-01-30 01:11:05 +0000 | [diff] [blame] | 58 | bool operator==(const AssemblerLabel& other) const { return m_offset == other.m_offset; } |
| 59 | |
barraclough@apple.com | 4836c7a | 2011-05-01 22:20:59 +0000 | [diff] [blame] | 60 | uint32_t m_offset; |
| 61 | }; |
| 62 | |
benjamin@webkit.org | f6de077 | 2014-07-15 23:59:14 +0000 | [diff] [blame] | 63 | class AssemblerData { |
sbarati@apple.com | b5bee81 | 2016-06-19 19:42:18 +0000 | [diff] [blame] | 64 | WTF_MAKE_NONCOPYABLE(AssemblerData); |
| 65 | static const size_t InlineCapacity = 128; |
ggaren@apple.com | 58f417e | 2008-11-17 06:27:06 +0000 | [diff] [blame] | 66 | public: |
benjamin@webkit.org | f6de077 | 2014-07-15 23:59:14 +0000 | [diff] [blame] | 67 | AssemblerData() |
sbarati@apple.com | b5bee81 | 2016-06-19 19:42:18 +0000 | [diff] [blame] | 68 | : m_buffer(m_inlineBuffer) |
| 69 | , m_capacity(InlineCapacity) |
ggaren@apple.com | 58f417e | 2008-11-17 06:27:06 +0000 | [diff] [blame] | 70 | { |
| 71 | } |
| 72 | |
sbarati@apple.com | b5bee81 | 2016-06-19 19:42:18 +0000 | [diff] [blame] | 73 | AssemblerData(size_t initialCapacity) |
benjamin@webkit.org | f6de077 | 2014-07-15 23:59:14 +0000 | [diff] [blame] | 74 | { |
sbarati@apple.com | b5bee81 | 2016-06-19 19:42:18 +0000 | [diff] [blame] | 75 | if (initialCapacity <= InlineCapacity) { |
| 76 | m_capacity = InlineCapacity; |
| 77 | m_buffer = m_inlineBuffer; |
| 78 | } else { |
| 79 | m_capacity = initialCapacity; |
| 80 | m_buffer = static_cast<char*>(fastMalloc(m_capacity)); |
| 81 | } |
benjamin@webkit.org | f6de077 | 2014-07-15 23:59:14 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | AssemblerData(AssemblerData&& other) |
| 85 | { |
sbarati@apple.com | b5bee81 | 2016-06-19 19:42:18 +0000 | [diff] [blame] | 86 | if (other.isInlineBuffer()) { |
| 87 | ASSERT(other.m_capacity == InlineCapacity); |
| 88 | memcpy(m_inlineBuffer, other.m_inlineBuffer, InlineCapacity); |
| 89 | m_buffer = m_inlineBuffer; |
| 90 | } else |
| 91 | m_buffer = other.m_buffer; |
benjamin@webkit.org | f6de077 | 2014-07-15 23:59:14 +0000 | [diff] [blame] | 92 | m_capacity = other.m_capacity; |
sbarati@apple.com | b5bee81 | 2016-06-19 19:42:18 +0000 | [diff] [blame] | 93 | |
| 94 | other.m_buffer = nullptr; |
benjamin@webkit.org | f6de077 | 2014-07-15 23:59:14 +0000 | [diff] [blame] | 95 | other.m_capacity = 0; |
| 96 | } |
| 97 | |
| 98 | AssemblerData& operator=(AssemblerData&& other) |
| 99 | { |
sbarati@apple.com | b5bee81 | 2016-06-19 19:42:18 +0000 | [diff] [blame] | 100 | if (m_buffer && !isInlineBuffer()) |
| 101 | fastFree(m_buffer); |
| 102 | |
| 103 | if (other.isInlineBuffer()) { |
| 104 | ASSERT(other.m_capacity == InlineCapacity); |
| 105 | memcpy(m_inlineBuffer, other.m_inlineBuffer, InlineCapacity); |
| 106 | m_buffer = m_inlineBuffer; |
| 107 | } else |
| 108 | m_buffer = other.m_buffer; |
benjamin@webkit.org | f6de077 | 2014-07-15 23:59:14 +0000 | [diff] [blame] | 109 | m_capacity = other.m_capacity; |
sbarati@apple.com | b5bee81 | 2016-06-19 19:42:18 +0000 | [diff] [blame] | 110 | |
| 111 | other.m_buffer = nullptr; |
benjamin@webkit.org | f6de077 | 2014-07-15 23:59:14 +0000 | [diff] [blame] | 112 | other.m_capacity = 0; |
| 113 | return *this; |
| 114 | } |
| 115 | |
| 116 | ~AssemblerData() |
| 117 | { |
sbarati@apple.com | b5bee81 | 2016-06-19 19:42:18 +0000 | [diff] [blame] | 118 | if (m_buffer && !isInlineBuffer()) |
| 119 | fastFree(m_buffer); |
benjamin@webkit.org | f6de077 | 2014-07-15 23:59:14 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | char* buffer() const { return m_buffer; } |
| 123 | |
| 124 | unsigned capacity() const { return m_capacity; } |
| 125 | |
| 126 | void grow(unsigned extraCapacity = 0) |
| 127 | { |
benjamin@webkit.org | 26dd417 | 2015-05-18 06:26:10 +0000 | [diff] [blame] | 128 | m_capacity = m_capacity + m_capacity / 2 + extraCapacity; |
sbarati@apple.com | b5bee81 | 2016-06-19 19:42:18 +0000 | [diff] [blame] | 129 | if (isInlineBuffer()) { |
| 130 | m_buffer = static_cast<char*>(fastMalloc(m_capacity)); |
| 131 | memcpy(m_buffer, m_inlineBuffer, InlineCapacity); |
| 132 | } else |
| 133 | m_buffer = static_cast<char*>(fastRealloc(m_buffer, m_capacity)); |
benjamin@webkit.org | f6de077 | 2014-07-15 23:59:14 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | private: |
sbarati@apple.com | b5bee81 | 2016-06-19 19:42:18 +0000 | [diff] [blame] | 137 | bool isInlineBuffer() const { return m_buffer == m_inlineBuffer; } |
benjamin@webkit.org | f6de077 | 2014-07-15 23:59:14 +0000 | [diff] [blame] | 138 | char* m_buffer; |
sbarati@apple.com | b5bee81 | 2016-06-19 19:42:18 +0000 | [diff] [blame] | 139 | char m_inlineBuffer[InlineCapacity]; |
benjamin@webkit.org | f6de077 | 2014-07-15 23:59:14 +0000 | [diff] [blame] | 140 | unsigned m_capacity; |
| 141 | }; |
| 142 | |
| 143 | class AssemblerBuffer { |
benjamin@webkit.org | f6de077 | 2014-07-15 23:59:14 +0000 | [diff] [blame] | 144 | public: |
| 145 | AssemblerBuffer() |
sbarati@apple.com | b5bee81 | 2016-06-19 19:42:18 +0000 | [diff] [blame] | 146 | : m_storage() |
benjamin@webkit.org | f6de077 | 2014-07-15 23:59:14 +0000 | [diff] [blame] | 147 | , m_index(0) |
ggaren@apple.com | 58f417e | 2008-11-17 06:27:06 +0000 | [diff] [blame] | 148 | { |
barraclough@apple.com | c5390ae | 2011-05-02 18:30:03 +0000 | [diff] [blame] | 149 | } |
| 150 | |
commit-queue@webkit.org | 932ba2d | 2016-03-26 03:47:23 +0000 | [diff] [blame] | 151 | bool isAvailable(unsigned space) |
barraclough@apple.com | c5390ae | 2011-05-02 18:30:03 +0000 | [diff] [blame] | 152 | { |
benjamin@webkit.org | f6de077 | 2014-07-15 23:59:14 +0000 | [diff] [blame] | 153 | return m_index + space <= m_storage.capacity(); |
ggaren@apple.com | 58f417e | 2008-11-17 06:27:06 +0000 | [diff] [blame] | 154 | } |
| 155 | |
commit-queue@webkit.org | 932ba2d | 2016-03-26 03:47:23 +0000 | [diff] [blame] | 156 | void ensureSpace(unsigned space) |
ggaren@apple.com | 58f417e | 2008-11-17 06:27:06 +0000 | [diff] [blame] | 157 | { |
sbarati@apple.com | b5bee81 | 2016-06-19 19:42:18 +0000 | [diff] [blame] | 158 | while (!isAvailable(space)) |
commit-queue@webkit.org | 932ba2d | 2016-03-26 03:47:23 +0000 | [diff] [blame] | 159 | outOfLineGrow(); |
ggaren@apple.com | 58f417e | 2008-11-17 06:27:06 +0000 | [diff] [blame] | 160 | } |
| 161 | |
barraclough@apple.com | 167270d | 2008-12-15 23:38:19 +0000 | [diff] [blame] | 162 | bool isAligned(int alignment) const |
ggaren@apple.com | 58f417e | 2008-11-17 06:27:06 +0000 | [diff] [blame] | 163 | { |
barraclough@apple.com | c5390ae | 2011-05-02 18:30:03 +0000 | [diff] [blame] | 164 | return !(m_index & (alignment - 1)); |
ggaren@apple.com | 58f417e | 2008-11-17 06:27:06 +0000 | [diff] [blame] | 165 | } |
| 166 | |
barraclough@apple.com | c5390ae | 2011-05-02 18:30:03 +0000 | [diff] [blame] | 167 | void putByteUnchecked(int8_t value) { putIntegralUnchecked(value); } |
| 168 | void putByte(int8_t value) { putIntegral(value); } |
| 169 | void putShortUnchecked(int16_t value) { putIntegralUnchecked(value); } |
| 170 | void putShort(int16_t value) { putIntegral(value); } |
| 171 | void putIntUnchecked(int32_t value) { putIntegralUnchecked(value); } |
| 172 | void putInt(int32_t value) { putIntegral(value); } |
| 173 | void putInt64Unchecked(int64_t value) { putIntegralUnchecked(value); } |
| 174 | void putInt64(int64_t value) { putIntegral(value); } |
| 175 | |
barraclough@apple.com | 167270d | 2008-12-15 23:38:19 +0000 | [diff] [blame] | 176 | void* data() const |
ggaren@apple.com | 58f417e | 2008-11-17 06:27:06 +0000 | [diff] [blame] | 177 | { |
benjamin@webkit.org | f6de077 | 2014-07-15 23:59:14 +0000 | [diff] [blame] | 178 | return m_storage.buffer(); |
ggaren@apple.com | 58f417e | 2008-11-17 06:27:06 +0000 | [diff] [blame] | 179 | } |
| 180 | |
barraclough@apple.com | e00c8ce | 2011-04-30 23:59:17 +0000 | [diff] [blame] | 181 | size_t codeSize() const |
| 182 | { |
barraclough@apple.com | c5390ae | 2011-05-02 18:30:03 +0000 | [diff] [blame] | 183 | return m_index; |
barraclough@apple.com | e00c8ce | 2011-04-30 23:59:17 +0000 | [diff] [blame] | 184 | } |
| 185 | |
sbarati@apple.com | b5bee81 | 2016-06-19 19:42:18 +0000 | [diff] [blame] | 186 | void setCodeSize(size_t index) |
| 187 | { |
| 188 | // Warning: Only use this if you know exactly what you are doing. |
| 189 | // For example, say you want 40 bytes of nops, it's ok to grow |
| 190 | // and then fill 40 bytes of nops using bigger instructions. |
| 191 | m_index = index; |
| 192 | ASSERT(m_index <= m_storage.capacity()); |
| 193 | } |
| 194 | |
barraclough@apple.com | 0ec8712 | 2011-05-02 01:04:17 +0000 | [diff] [blame] | 195 | AssemblerLabel label() const |
ggaren@apple.com | 58f417e | 2008-11-17 06:27:06 +0000 | [diff] [blame] | 196 | { |
barraclough@apple.com | c5390ae | 2011-05-02 18:30:03 +0000 | [diff] [blame] | 197 | return AssemblerLabel(m_index); |
ggaren@apple.com | 58f417e | 2008-11-17 06:27:06 +0000 | [diff] [blame] | 198 | } |
| 199 | |
barraclough@apple.com | c5390ae | 2011-05-02 18:30:03 +0000 | [diff] [blame] | 200 | unsigned debugOffset() { return m_index; } |
benjamin@webkit.org | f6de077 | 2014-07-15 23:59:14 +0000 | [diff] [blame] | 201 | |
sbarati@apple.com | b5bee81 | 2016-06-19 19:42:18 +0000 | [diff] [blame] | 202 | AssemblerData&& releaseAssemblerData() { return WTFMove(m_storage); } |
barraclough@apple.com | 9cb663d | 2011-04-15 00:25:59 +0000 | [diff] [blame] | 203 | |
commit-queue@webkit.org | 932ba2d | 2016-03-26 03:47:23 +0000 | [diff] [blame] | 204 | // LocalWriter is a trick to keep the storage buffer and the index |
| 205 | // in memory while issuing multiple Stores. |
| 206 | // It is created in a block scope and its attribute can stay live |
| 207 | // between writes. |
| 208 | // |
| 209 | // LocalWriter *CANNOT* be mixed with other types of access to AssemblerBuffer. |
| 210 | // AssemblerBuffer cannot be used until its LocalWriter goes out of scope. |
| 211 | class LocalWriter { |
| 212 | public: |
| 213 | LocalWriter(AssemblerBuffer& buffer, unsigned requiredSpace) |
| 214 | : m_buffer(buffer) |
| 215 | { |
| 216 | buffer.ensureSpace(requiredSpace); |
| 217 | m_storageBuffer = buffer.m_storage.buffer(); |
| 218 | m_index = buffer.m_index; |
| 219 | #if !defined(NDEBUG) |
| 220 | m_initialIndex = m_index; |
| 221 | m_requiredSpace = requiredSpace; |
| 222 | #endif |
| 223 | } |
| 224 | |
| 225 | ~LocalWriter() |
| 226 | { |
| 227 | ASSERT(m_index - m_initialIndex <= m_requiredSpace); |
| 228 | ASSERT(m_buffer.m_index == m_initialIndex); |
| 229 | ASSERT(m_storageBuffer == m_buffer.m_storage.buffer()); |
| 230 | m_buffer.m_index = m_index; |
| 231 | } |
| 232 | |
| 233 | void putByteUnchecked(int8_t value) { putIntegralUnchecked(value); } |
| 234 | void putShortUnchecked(int16_t value) { putIntegralUnchecked(value); } |
| 235 | void putIntUnchecked(int32_t value) { putIntegralUnchecked(value); } |
| 236 | void putInt64Unchecked(int64_t value) { putIntegralUnchecked(value); } |
| 237 | private: |
| 238 | template<typename IntegralType> |
| 239 | void putIntegralUnchecked(IntegralType value) |
| 240 | { |
| 241 | ASSERT(m_index + sizeof(IntegralType) <= m_buffer.m_storage.capacity()); |
| 242 | *reinterpret_cast_ptr<IntegralType*>(m_storageBuffer + m_index) = value; |
| 243 | m_index += sizeof(IntegralType); |
| 244 | } |
| 245 | AssemblerBuffer& m_buffer; |
| 246 | char* m_storageBuffer; |
| 247 | unsigned m_index; |
| 248 | #if !defined(NDEBUG) |
| 249 | unsigned m_initialIndex; |
| 250 | unsigned m_requiredSpace; |
| 251 | #endif |
| 252 | }; |
| 253 | |
barraclough@apple.com | 3fa07df | 2009-07-17 21:17:45 +0000 | [diff] [blame] | 254 | protected: |
benjamin@webkit.org | f6de077 | 2014-07-15 23:59:14 +0000 | [diff] [blame] | 255 | template<typename IntegralType> |
| 256 | void putIntegral(IntegralType value) |
| 257 | { |
| 258 | unsigned nextIndex = m_index + sizeof(IntegralType); |
| 259 | if (UNLIKELY(nextIndex > m_storage.capacity())) |
commit-queue@webkit.org | 932ba2d | 2016-03-26 03:47:23 +0000 | [diff] [blame] | 260 | outOfLineGrow(); |
benjamin@webkit.org | f6de077 | 2014-07-15 23:59:14 +0000 | [diff] [blame] | 261 | ASSERT(isAvailable(sizeof(IntegralType))); |
| 262 | *reinterpret_cast_ptr<IntegralType*>(m_storage.buffer() + m_index) = value; |
| 263 | m_index = nextIndex; |
| 264 | } |
| 265 | |
| 266 | template<typename IntegralType> |
| 267 | void putIntegralUnchecked(IntegralType value) |
| 268 | { |
| 269 | ASSERT(isAvailable(sizeof(IntegralType))); |
| 270 | *reinterpret_cast_ptr<IntegralType*>(m_storage.buffer() + m_index) = value; |
| 271 | m_index += sizeof(IntegralType); |
| 272 | } |
| 273 | |
barraclough@apple.com | 3fa07df | 2009-07-17 21:17:45 +0000 | [diff] [blame] | 274 | void append(const char* data, int size) |
ggaren@apple.com | 58f417e | 2008-11-17 06:27:06 +0000 | [diff] [blame] | 275 | { |
barraclough@apple.com | c5390ae | 2011-05-02 18:30:03 +0000 | [diff] [blame] | 276 | if (!isAvailable(size)) |
barraclough@apple.com | 3fa07df | 2009-07-17 21:17:45 +0000 | [diff] [blame] | 277 | grow(size); |
| 278 | |
benjamin@webkit.org | f6de077 | 2014-07-15 23:59:14 +0000 | [diff] [blame] | 279 | memcpy(m_storage.buffer() + m_index, data, size); |
barraclough@apple.com | c5390ae | 2011-05-02 18:30:03 +0000 | [diff] [blame] | 280 | m_index += size; |
barraclough@apple.com | 3fa07df | 2009-07-17 21:17:45 +0000 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | void grow(int extraCapacity = 0) |
| 284 | { |
benjamin@webkit.org | f6de077 | 2014-07-15 23:59:14 +0000 | [diff] [blame] | 285 | m_storage.grow(extraCapacity); |
ggaren@apple.com | 58f417e | 2008-11-17 06:27:06 +0000 | [diff] [blame] | 286 | } |
| 287 | |
barraclough@apple.com | c5390ae | 2011-05-02 18:30:03 +0000 | [diff] [blame] | 288 | private: |
commit-queue@webkit.org | 932ba2d | 2016-03-26 03:47:23 +0000 | [diff] [blame] | 289 | NEVER_INLINE void outOfLineGrow() |
| 290 | { |
| 291 | m_storage.grow(); |
| 292 | } |
| 293 | |
| 294 | friend LocalWriter; |
| 295 | |
benjamin@webkit.org | f6de077 | 2014-07-15 23:59:14 +0000 | [diff] [blame] | 296 | AssemblerData m_storage; |
| 297 | unsigned m_index; |
ggaren@apple.com | 58f417e | 2008-11-17 06:27:06 +0000 | [diff] [blame] | 298 | }; |
| 299 | |
| 300 | } // namespace JSC |
| 301 | |
| 302 | #endif // ENABLE(ASSEMBLER) |