ggaren@apple.com | 58f417e | 2008-11-17 06:27:06 +0000 | [diff] [blame] | 1 | /* |
mark.lam@apple.com | d27553f | 2019-09-18 00:36:19 +0000 | [diff] [blame] | 2 | * Copyright (C) 2008-2019 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> |
sbarati@apple.com | 80c907f | 2018-09-28 05:34:38 +0000 | [diff] [blame] | 36 | #if CPU(ARM64E) |
| 37 | #include <wtf/PtrTag.h> |
| 38 | #endif |
loki@webkit.org | 7534a3f | 2010-08-13 10:14:36 +0000 | [diff] [blame] | 39 | #include <wtf/StdLibExtras.h> |
msaboff@apple.com | 89c891b | 2020-05-31 14:56:06 +0000 | [diff] [blame] | 40 | #include <wtf/ThreadSpecific.h> |
yusukesuzuki@slowstart.org | 6836640 | 2018-08-19 22:50:05 +0000 | [diff] [blame] | 41 | #include <wtf/UnalignedAccess.h> |
ggaren@apple.com | 58f417e | 2008-11-17 06:27:06 +0000 | [diff] [blame] | 42 | |
| 43 | namespace JSC { |
msaboff@apple.com | 89c891b | 2020-05-31 14:56:06 +0000 | [diff] [blame] | 44 | class AssemblerData; |
| 45 | |
| 46 | typedef ThreadSpecific<AssemblerData, WTF::CanBeGCThread::True> ThreadSpecificAssemblerData; |
| 47 | |
| 48 | JS_EXPORT_PRIVATE ThreadSpecificAssemblerData& threadSpecificAssemblerData(); |
ggaren@apple.com | 58f417e | 2008-11-17 06:27:06 +0000 | [diff] [blame] | 49 | |
sbarati@apple.com | 80c907f | 2018-09-28 05:34:38 +0000 | [diff] [blame] | 50 | class LinkBuffer; |
| 51 | |
ysuzuki@apple.com | 4642e11 | 2020-01-03 02:36:43 +0000 | [diff] [blame] | 52 | DECLARE_ALLOCATOR_WITH_HEAP_IDENTIFIER(AssemblerData); |
| 53 | |
barraclough@apple.com | 4836c7a | 2011-05-01 22:20:59 +0000 | [diff] [blame] | 54 | struct AssemblerLabel { |
barraclough@apple.com | c5390ae | 2011-05-02 18:30:03 +0000 | [diff] [blame] | 55 | AssemblerLabel() |
| 56 | : m_offset(std::numeric_limits<uint32_t>::max()) |
| 57 | { |
| 58 | } |
| 59 | |
| 60 | explicit AssemblerLabel(uint32_t offset) |
barraclough@apple.com | 4836c7a | 2011-05-01 22:20:59 +0000 | [diff] [blame] | 61 | : m_offset(offset) |
| 62 | { |
| 63 | } |
| 64 | |
| 65 | bool isSet() const { return (m_offset != std::numeric_limits<uint32_t>::max()); } |
| 66 | |
barraclough@apple.com | c5390ae | 2011-05-02 18:30:03 +0000 | [diff] [blame] | 67 | AssemblerLabel labelAtOffset(int offset) const |
| 68 | { |
| 69 | return AssemblerLabel(m_offset + offset); |
| 70 | } |
| 71 | |
sbarati@apple.com | d3d0c00 | 2016-01-30 01:11:05 +0000 | [diff] [blame] | 72 | bool operator==(const AssemblerLabel& other) const { return m_offset == other.m_offset; } |
| 73 | |
barraclough@apple.com | 4836c7a | 2011-05-01 22:20:59 +0000 | [diff] [blame] | 74 | uint32_t m_offset; |
| 75 | }; |
| 76 | |
benjamin@webkit.org | f6de077 | 2014-07-15 23:59:14 +0000 | [diff] [blame] | 77 | class AssemblerData { |
sbarati@apple.com | b5bee81 | 2016-06-19 19:42:18 +0000 | [diff] [blame] | 78 | WTF_MAKE_NONCOPYABLE(AssemblerData); |
mark.lam@apple.com | d27553f | 2019-09-18 00:36:19 +0000 | [diff] [blame] | 79 | static constexpr size_t InlineCapacity = 128; |
ggaren@apple.com | 58f417e | 2008-11-17 06:27:06 +0000 | [diff] [blame] | 80 | public: |
benjamin@webkit.org | f6de077 | 2014-07-15 23:59:14 +0000 | [diff] [blame] | 81 | AssemblerData() |
sbarati@apple.com | b5bee81 | 2016-06-19 19:42:18 +0000 | [diff] [blame] | 82 | : m_buffer(m_inlineBuffer) |
| 83 | , m_capacity(InlineCapacity) |
ggaren@apple.com | 58f417e | 2008-11-17 06:27:06 +0000 | [diff] [blame] | 84 | { |
| 85 | } |
| 86 | |
sbarati@apple.com | b5bee81 | 2016-06-19 19:42:18 +0000 | [diff] [blame] | 87 | AssemblerData(size_t initialCapacity) |
benjamin@webkit.org | f6de077 | 2014-07-15 23:59:14 +0000 | [diff] [blame] | 88 | { |
sbarati@apple.com | b5bee81 | 2016-06-19 19:42:18 +0000 | [diff] [blame] | 89 | if (initialCapacity <= InlineCapacity) { |
| 90 | m_capacity = InlineCapacity; |
| 91 | m_buffer = m_inlineBuffer; |
| 92 | } else { |
| 93 | m_capacity = initialCapacity; |
ysuzuki@apple.com | 4642e11 | 2020-01-03 02:36:43 +0000 | [diff] [blame] | 94 | m_buffer = static_cast<char*>(AssemblerDataMalloc::malloc(m_capacity)); |
sbarati@apple.com | b5bee81 | 2016-06-19 19:42:18 +0000 | [diff] [blame] | 95 | } |
benjamin@webkit.org | f6de077 | 2014-07-15 23:59:14 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | AssemblerData(AssemblerData&& other) |
| 99 | { |
sbarati@apple.com | b5bee81 | 2016-06-19 19:42:18 +0000 | [diff] [blame] | 100 | if (other.isInlineBuffer()) { |
| 101 | ASSERT(other.m_capacity == InlineCapacity); |
| 102 | memcpy(m_inlineBuffer, other.m_inlineBuffer, InlineCapacity); |
| 103 | m_buffer = m_inlineBuffer; |
| 104 | } else |
| 105 | m_buffer = other.m_buffer; |
benjamin@webkit.org | f6de077 | 2014-07-15 23:59:14 +0000 | [diff] [blame] | 106 | m_capacity = other.m_capacity; |
sbarati@apple.com | b5bee81 | 2016-06-19 19:42:18 +0000 | [diff] [blame] | 107 | |
msaboff@apple.com | 89c891b | 2020-05-31 14:56:06 +0000 | [diff] [blame] | 108 | other.m_buffer = other.m_inlineBuffer; |
| 109 | other.m_capacity = InlineCapacity; |
benjamin@webkit.org | f6de077 | 2014-07-15 23:59:14 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | AssemblerData& operator=(AssemblerData&& other) |
| 113 | { |
sbarati@apple.com | b5bee81 | 2016-06-19 19:42:18 +0000 | [diff] [blame] | 114 | if (m_buffer && !isInlineBuffer()) |
ysuzuki@apple.com | 4642e11 | 2020-01-03 02:36:43 +0000 | [diff] [blame] | 115 | AssemblerDataMalloc::free(m_buffer); |
sbarati@apple.com | b5bee81 | 2016-06-19 19:42:18 +0000 | [diff] [blame] | 116 | |
| 117 | if (other.isInlineBuffer()) { |
| 118 | ASSERT(other.m_capacity == InlineCapacity); |
| 119 | memcpy(m_inlineBuffer, other.m_inlineBuffer, InlineCapacity); |
| 120 | m_buffer = m_inlineBuffer; |
| 121 | } else |
| 122 | m_buffer = other.m_buffer; |
benjamin@webkit.org | f6de077 | 2014-07-15 23:59:14 +0000 | [diff] [blame] | 123 | m_capacity = other.m_capacity; |
sbarati@apple.com | b5bee81 | 2016-06-19 19:42:18 +0000 | [diff] [blame] | 124 | |
msaboff@apple.com | 89c891b | 2020-05-31 14:56:06 +0000 | [diff] [blame] | 125 | other.m_buffer = other.m_inlineBuffer; |
| 126 | other.m_capacity = InlineCapacity; |
benjamin@webkit.org | f6de077 | 2014-07-15 23:59:14 +0000 | [diff] [blame] | 127 | return *this; |
| 128 | } |
| 129 | |
msaboff@apple.com | 89c891b | 2020-05-31 14:56:06 +0000 | [diff] [blame] | 130 | void takeBufferIfLarger(AssemblerData&& other) |
| 131 | { |
| 132 | if (other.isInlineBuffer()) |
| 133 | return; |
| 134 | |
| 135 | if (m_capacity >= other.m_capacity) |
| 136 | return; |
| 137 | |
| 138 | if (m_buffer && !isInlineBuffer()) |
| 139 | AssemblerDataMalloc::free(m_buffer); |
| 140 | |
| 141 | m_buffer = other.m_buffer; |
| 142 | m_capacity = other.m_capacity; |
| 143 | |
| 144 | other.m_buffer = other.m_inlineBuffer; |
| 145 | other.m_capacity = InlineCapacity; |
| 146 | } |
| 147 | |
benjamin@webkit.org | f6de077 | 2014-07-15 23:59:14 +0000 | [diff] [blame] | 148 | ~AssemblerData() |
| 149 | { |
msaboff@apple.com | 89c891b | 2020-05-31 14:56:06 +0000 | [diff] [blame] | 150 | clear(); |
| 151 | } |
| 152 | |
| 153 | void clear() |
| 154 | { |
| 155 | if (m_buffer && !isInlineBuffer()) { |
ysuzuki@apple.com | 4642e11 | 2020-01-03 02:36:43 +0000 | [diff] [blame] | 156 | AssemblerDataMalloc::free(m_buffer); |
msaboff@apple.com | 89c891b | 2020-05-31 14:56:06 +0000 | [diff] [blame] | 157 | m_capacity = InlineCapacity; |
| 158 | m_buffer = m_inlineBuffer; |
| 159 | } |
benjamin@webkit.org | f6de077 | 2014-07-15 23:59:14 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | char* buffer() const { return m_buffer; } |
| 163 | |
| 164 | unsigned capacity() const { return m_capacity; } |
| 165 | |
| 166 | void grow(unsigned extraCapacity = 0) |
| 167 | { |
benjamin@webkit.org | 26dd417 | 2015-05-18 06:26:10 +0000 | [diff] [blame] | 168 | m_capacity = m_capacity + m_capacity / 2 + extraCapacity; |
sbarati@apple.com | b5bee81 | 2016-06-19 19:42:18 +0000 | [diff] [blame] | 169 | if (isInlineBuffer()) { |
ysuzuki@apple.com | 4642e11 | 2020-01-03 02:36:43 +0000 | [diff] [blame] | 170 | m_buffer = static_cast<char*>(AssemblerDataMalloc::malloc(m_capacity)); |
sbarati@apple.com | b5bee81 | 2016-06-19 19:42:18 +0000 | [diff] [blame] | 171 | memcpy(m_buffer, m_inlineBuffer, InlineCapacity); |
| 172 | } else |
ysuzuki@apple.com | 4642e11 | 2020-01-03 02:36:43 +0000 | [diff] [blame] | 173 | m_buffer = static_cast<char*>(AssemblerDataMalloc::realloc(m_buffer, m_capacity)); |
benjamin@webkit.org | f6de077 | 2014-07-15 23:59:14 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | private: |
sbarati@apple.com | b5bee81 | 2016-06-19 19:42:18 +0000 | [diff] [blame] | 177 | bool isInlineBuffer() const { return m_buffer == m_inlineBuffer; } |
benjamin@webkit.org | f6de077 | 2014-07-15 23:59:14 +0000 | [diff] [blame] | 178 | char* m_buffer; |
sbarati@apple.com | b5bee81 | 2016-06-19 19:42:18 +0000 | [diff] [blame] | 179 | char m_inlineBuffer[InlineCapacity]; |
benjamin@webkit.org | f6de077 | 2014-07-15 23:59:14 +0000 | [diff] [blame] | 180 | unsigned m_capacity; |
| 181 | }; |
| 182 | |
sbarati@apple.com | 80c907f | 2018-09-28 05:34:38 +0000 | [diff] [blame] | 183 | #if CPU(ARM64E) |
| 184 | class ARM64EHash { |
| 185 | public: |
sbarati@apple.com | fc044cd | 2018-12-19 02:27:00 +0000 | [diff] [blame] | 186 | ARM64EHash() = default; |
| 187 | ALWAYS_INLINE void update(uint32_t value) |
sbarati@apple.com | 80c907f | 2018-09-28 05:34:38 +0000 | [diff] [blame] | 188 | { |
sbarati@apple.com | fc044cd | 2018-12-19 02:27:00 +0000 | [diff] [blame] | 189 | uint64_t input = value ^ m_hash; |
| 190 | uint64_t a = static_cast<uint32_t>(tagInt(input, static_cast<PtrTag>(0)) >> 39); |
| 191 | uint64_t b = tagInt(input, static_cast<PtrTag>(0xb7e151628aed2a6a)) >> 23; |
sbarati@apple.com | 0f1b423 | 2019-01-24 22:07:33 +0000 | [diff] [blame] | 192 | m_hash = a ^ b; |
sbarati@apple.com | 80c907f | 2018-09-28 05:34:38 +0000 | [diff] [blame] | 193 | } |
sbarati@apple.com | fc044cd | 2018-12-19 02:27:00 +0000 | [diff] [blame] | 194 | uint32_t finalHash() const |
| 195 | { |
| 196 | uint64_t hash = m_hash; |
| 197 | uint64_t a = static_cast<uint32_t>(tagInt(hash, static_cast<PtrTag>(0xbf7158809cf4f3c7)) >> 39); |
| 198 | uint64_t b = tagInt(hash, static_cast<PtrTag>(0x62e7160f38b4da56)) >> 23; |
sbarati@apple.com | 0f1b423 | 2019-01-24 22:07:33 +0000 | [diff] [blame] | 199 | return static_cast<uint32_t>(a ^ b); |
sbarati@apple.com | fc044cd | 2018-12-19 02:27:00 +0000 | [diff] [blame] | 200 | } |
sbarati@apple.com | 80c907f | 2018-09-28 05:34:38 +0000 | [diff] [blame] | 201 | private: |
sbarati@apple.com | fc044cd | 2018-12-19 02:27:00 +0000 | [diff] [blame] | 202 | uint32_t m_hash { 0 }; |
sbarati@apple.com | 80c907f | 2018-09-28 05:34:38 +0000 | [diff] [blame] | 203 | }; |
| 204 | #endif |
| 205 | |
benjamin@webkit.org | f6de077 | 2014-07-15 23:59:14 +0000 | [diff] [blame] | 206 | class AssemblerBuffer { |
benjamin@webkit.org | f6de077 | 2014-07-15 23:59:14 +0000 | [diff] [blame] | 207 | public: |
sbarati@apple.com | fc044cd | 2018-12-19 02:27:00 +0000 | [diff] [blame] | 208 | AssemblerBuffer() |
sbarati@apple.com | b5bee81 | 2016-06-19 19:42:18 +0000 | [diff] [blame] | 209 | : m_storage() |
benjamin@webkit.org | f6de077 | 2014-07-15 23:59:14 +0000 | [diff] [blame] | 210 | , m_index(0) |
ggaren@apple.com | 58f417e | 2008-11-17 06:27:06 +0000 | [diff] [blame] | 211 | { |
sbarati@apple.com | 48d5276 | 2020-06-02 00:51:53 +0000 | [diff] [blame] | 212 | auto& threadSpecific = threadSpecificAssemblerData(); |
msaboff@apple.com | 89c891b | 2020-05-31 14:56:06 +0000 | [diff] [blame] | 213 | m_storage.takeBufferIfLarger(WTFMove(*threadSpecific)); |
| 214 | } |
| 215 | |
| 216 | ~AssemblerBuffer() |
| 217 | { |
sbarati@apple.com | 48d5276 | 2020-06-02 00:51:53 +0000 | [diff] [blame] | 218 | auto& threadSpecific = threadSpecificAssemblerData(); |
msaboff@apple.com | 89c891b | 2020-05-31 14:56:06 +0000 | [diff] [blame] | 219 | threadSpecific->takeBufferIfLarger(WTFMove(m_storage)); |
barraclough@apple.com | c5390ae | 2011-05-02 18:30:03 +0000 | [diff] [blame] | 220 | } |
| 221 | |
commit-queue@webkit.org | 932ba2d | 2016-03-26 03:47:23 +0000 | [diff] [blame] | 222 | bool isAvailable(unsigned space) |
barraclough@apple.com | c5390ae | 2011-05-02 18:30:03 +0000 | [diff] [blame] | 223 | { |
benjamin@webkit.org | f6de077 | 2014-07-15 23:59:14 +0000 | [diff] [blame] | 224 | return m_index + space <= m_storage.capacity(); |
ggaren@apple.com | 58f417e | 2008-11-17 06:27:06 +0000 | [diff] [blame] | 225 | } |
| 226 | |
commit-queue@webkit.org | 932ba2d | 2016-03-26 03:47:23 +0000 | [diff] [blame] | 227 | void ensureSpace(unsigned space) |
ggaren@apple.com | 58f417e | 2008-11-17 06:27:06 +0000 | [diff] [blame] | 228 | { |
sbarati@apple.com | b5bee81 | 2016-06-19 19:42:18 +0000 | [diff] [blame] | 229 | while (!isAvailable(space)) |
commit-queue@webkit.org | 932ba2d | 2016-03-26 03:47:23 +0000 | [diff] [blame] | 230 | outOfLineGrow(); |
ggaren@apple.com | 58f417e | 2008-11-17 06:27:06 +0000 | [diff] [blame] | 231 | } |
| 232 | |
barraclough@apple.com | 167270d | 2008-12-15 23:38:19 +0000 | [diff] [blame] | 233 | bool isAligned(int alignment) const |
ggaren@apple.com | 58f417e | 2008-11-17 06:27:06 +0000 | [diff] [blame] | 234 | { |
barraclough@apple.com | c5390ae | 2011-05-02 18:30:03 +0000 | [diff] [blame] | 235 | return !(m_index & (alignment - 1)); |
ggaren@apple.com | 58f417e | 2008-11-17 06:27:06 +0000 | [diff] [blame] | 236 | } |
| 237 | |
sbarati@apple.com | 80c907f | 2018-09-28 05:34:38 +0000 | [diff] [blame] | 238 | #if !CPU(ARM64) |
barraclough@apple.com | c5390ae | 2011-05-02 18:30:03 +0000 | [diff] [blame] | 239 | void putByteUnchecked(int8_t value) { putIntegralUnchecked(value); } |
| 240 | void putByte(int8_t value) { putIntegral(value); } |
| 241 | void putShortUnchecked(int16_t value) { putIntegralUnchecked(value); } |
| 242 | void putShort(int16_t value) { putIntegral(value); } |
barraclough@apple.com | c5390ae | 2011-05-02 18:30:03 +0000 | [diff] [blame] | 243 | void putInt64Unchecked(int64_t value) { putIntegralUnchecked(value); } |
| 244 | void putInt64(int64_t value) { putIntegral(value); } |
sbarati@apple.com | 80c907f | 2018-09-28 05:34:38 +0000 | [diff] [blame] | 245 | #endif |
| 246 | void putIntUnchecked(int32_t value) { putIntegralUnchecked(value); } |
| 247 | void putInt(int32_t value) { putIntegral(value); } |
ggaren@apple.com | 58f417e | 2008-11-17 06:27:06 +0000 | [diff] [blame] | 248 | |
barraclough@apple.com | e00c8ce | 2011-04-30 23:59:17 +0000 | [diff] [blame] | 249 | size_t codeSize() const |
| 250 | { |
barraclough@apple.com | c5390ae | 2011-05-02 18:30:03 +0000 | [diff] [blame] | 251 | return m_index; |
barraclough@apple.com | e00c8ce | 2011-04-30 23:59:17 +0000 | [diff] [blame] | 252 | } |
| 253 | |
sbarati@apple.com | 80c907f | 2018-09-28 05:34:38 +0000 | [diff] [blame] | 254 | #if !CPU(ARM64) |
sbarati@apple.com | b5bee81 | 2016-06-19 19:42:18 +0000 | [diff] [blame] | 255 | void setCodeSize(size_t index) |
| 256 | { |
| 257 | // Warning: Only use this if you know exactly what you are doing. |
| 258 | // For example, say you want 40 bytes of nops, it's ok to grow |
| 259 | // and then fill 40 bytes of nops using bigger instructions. |
| 260 | m_index = index; |
| 261 | ASSERT(m_index <= m_storage.capacity()); |
| 262 | } |
sbarati@apple.com | 80c907f | 2018-09-28 05:34:38 +0000 | [diff] [blame] | 263 | #endif |
sbarati@apple.com | b5bee81 | 2016-06-19 19:42:18 +0000 | [diff] [blame] | 264 | |
barraclough@apple.com | 0ec8712 | 2011-05-02 01:04:17 +0000 | [diff] [blame] | 265 | AssemblerLabel label() const |
ggaren@apple.com | 58f417e | 2008-11-17 06:27:06 +0000 | [diff] [blame] | 266 | { |
barraclough@apple.com | c5390ae | 2011-05-02 18:30:03 +0000 | [diff] [blame] | 267 | return AssemblerLabel(m_index); |
ggaren@apple.com | 58f417e | 2008-11-17 06:27:06 +0000 | [diff] [blame] | 268 | } |
| 269 | |
barraclough@apple.com | c5390ae | 2011-05-02 18:30:03 +0000 | [diff] [blame] | 270 | unsigned debugOffset() { return m_index; } |
benjamin@webkit.org | f6de077 | 2014-07-15 23:59:14 +0000 | [diff] [blame] | 271 | |
sbarati@apple.com | b5bee81 | 2016-06-19 19:42:18 +0000 | [diff] [blame] | 272 | AssemblerData&& releaseAssemblerData() { return WTFMove(m_storage); } |
barraclough@apple.com | 9cb663d | 2011-04-15 00:25:59 +0000 | [diff] [blame] | 273 | |
commit-queue@webkit.org | 932ba2d | 2016-03-26 03:47:23 +0000 | [diff] [blame] | 274 | // LocalWriter is a trick to keep the storage buffer and the index |
| 275 | // in memory while issuing multiple Stores. |
| 276 | // It is created in a block scope and its attribute can stay live |
| 277 | // between writes. |
| 278 | // |
| 279 | // LocalWriter *CANNOT* be mixed with other types of access to AssemblerBuffer. |
| 280 | // AssemblerBuffer cannot be used until its LocalWriter goes out of scope. |
sbarati@apple.com | 80c907f | 2018-09-28 05:34:38 +0000 | [diff] [blame] | 281 | #if !CPU(ARM64) // If we ever need to use this on arm64e, we would need to make the checksum aware of this. |
commit-queue@webkit.org | 932ba2d | 2016-03-26 03:47:23 +0000 | [diff] [blame] | 282 | class LocalWriter { |
| 283 | public: |
| 284 | LocalWriter(AssemblerBuffer& buffer, unsigned requiredSpace) |
| 285 | : m_buffer(buffer) |
| 286 | { |
| 287 | buffer.ensureSpace(requiredSpace); |
| 288 | m_storageBuffer = buffer.m_storage.buffer(); |
| 289 | m_index = buffer.m_index; |
mark.lam@apple.com | 6572436 | 2020-01-06 22:24:50 +0000 | [diff] [blame] | 290 | #if ASSERT_ENABLED |
commit-queue@webkit.org | 932ba2d | 2016-03-26 03:47:23 +0000 | [diff] [blame] | 291 | m_initialIndex = m_index; |
| 292 | m_requiredSpace = requiredSpace; |
| 293 | #endif |
| 294 | } |
| 295 | |
| 296 | ~LocalWriter() |
| 297 | { |
| 298 | ASSERT(m_index - m_initialIndex <= m_requiredSpace); |
| 299 | ASSERT(m_buffer.m_index == m_initialIndex); |
| 300 | ASSERT(m_storageBuffer == m_buffer.m_storage.buffer()); |
| 301 | m_buffer.m_index = m_index; |
| 302 | } |
| 303 | |
| 304 | void putByteUnchecked(int8_t value) { putIntegralUnchecked(value); } |
| 305 | void putShortUnchecked(int16_t value) { putIntegralUnchecked(value); } |
| 306 | void putIntUnchecked(int32_t value) { putIntegralUnchecked(value); } |
| 307 | void putInt64Unchecked(int64_t value) { putIntegralUnchecked(value); } |
| 308 | private: |
| 309 | template<typename IntegralType> |
| 310 | void putIntegralUnchecked(IntegralType value) |
| 311 | { |
| 312 | ASSERT(m_index + sizeof(IntegralType) <= m_buffer.m_storage.capacity()); |
yusukesuzuki@slowstart.org | 6836640 | 2018-08-19 22:50:05 +0000 | [diff] [blame] | 313 | WTF::unalignedStore<IntegralType>(m_storageBuffer + m_index, value); |
commit-queue@webkit.org | 932ba2d | 2016-03-26 03:47:23 +0000 | [diff] [blame] | 314 | m_index += sizeof(IntegralType); |
| 315 | } |
| 316 | AssemblerBuffer& m_buffer; |
| 317 | char* m_storageBuffer; |
| 318 | unsigned m_index; |
mark.lam@apple.com | 6572436 | 2020-01-06 22:24:50 +0000 | [diff] [blame] | 319 | #if ASSERT_ENABLED |
commit-queue@webkit.org | 932ba2d | 2016-03-26 03:47:23 +0000 | [diff] [blame] | 320 | unsigned m_initialIndex; |
| 321 | unsigned m_requiredSpace; |
| 322 | #endif |
| 323 | }; |
sbarati@apple.com | 80c907f | 2018-09-28 05:34:38 +0000 | [diff] [blame] | 324 | #endif // !CPU(ARM64) |
| 325 | |
| 326 | #if CPU(ARM64E) |
| 327 | ARM64EHash hash() const { return m_hash; } |
| 328 | #endif |
| 329 | |
| 330 | #if !CPU(ARM64) // If we were to define this on arm64e, we'd need a way to update the hash as we write directly into the buffer. |
| 331 | void* data() const { return m_storage.buffer(); } |
| 332 | #endif |
| 333 | |
commit-queue@webkit.org | 932ba2d | 2016-03-26 03:47:23 +0000 | [diff] [blame] | 334 | |
barraclough@apple.com | 3fa07df | 2009-07-17 21:17:45 +0000 | [diff] [blame] | 335 | protected: |
benjamin@webkit.org | f6de077 | 2014-07-15 23:59:14 +0000 | [diff] [blame] | 336 | template<typename IntegralType> |
| 337 | void putIntegral(IntegralType value) |
| 338 | { |
| 339 | unsigned nextIndex = m_index + sizeof(IntegralType); |
| 340 | if (UNLIKELY(nextIndex > m_storage.capacity())) |
commit-queue@webkit.org | 932ba2d | 2016-03-26 03:47:23 +0000 | [diff] [blame] | 341 | outOfLineGrow(); |
yusukesuzuki@slowstart.org | 6836640 | 2018-08-19 22:50:05 +0000 | [diff] [blame] | 342 | putIntegralUnchecked<IntegralType>(value); |
benjamin@webkit.org | f6de077 | 2014-07-15 23:59:14 +0000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | template<typename IntegralType> |
| 346 | void putIntegralUnchecked(IntegralType value) |
| 347 | { |
sbarati@apple.com | 80c907f | 2018-09-28 05:34:38 +0000 | [diff] [blame] | 348 | #if CPU(ARM64) |
| 349 | static_assert(sizeof(value) == 4, ""); |
| 350 | #if CPU(ARM64E) |
sbarati@apple.com | fc044cd | 2018-12-19 02:27:00 +0000 | [diff] [blame] | 351 | m_hash.update(value); |
sbarati@apple.com | 80c907f | 2018-09-28 05:34:38 +0000 | [diff] [blame] | 352 | #endif |
| 353 | #endif |
benjamin@webkit.org | f6de077 | 2014-07-15 23:59:14 +0000 | [diff] [blame] | 354 | ASSERT(isAvailable(sizeof(IntegralType))); |
yusukesuzuki@slowstart.org | 6836640 | 2018-08-19 22:50:05 +0000 | [diff] [blame] | 355 | WTF::unalignedStore<IntegralType>(m_storage.buffer() + m_index, value); |
benjamin@webkit.org | f6de077 | 2014-07-15 23:59:14 +0000 | [diff] [blame] | 356 | m_index += sizeof(IntegralType); |
| 357 | } |
| 358 | |
sbarati@apple.com | 80c907f | 2018-09-28 05:34:38 +0000 | [diff] [blame] | 359 | private: |
barraclough@apple.com | 3fa07df | 2009-07-17 21:17:45 +0000 | [diff] [blame] | 360 | void grow(int extraCapacity = 0) |
| 361 | { |
benjamin@webkit.org | f6de077 | 2014-07-15 23:59:14 +0000 | [diff] [blame] | 362 | m_storage.grow(extraCapacity); |
ggaren@apple.com | 58f417e | 2008-11-17 06:27:06 +0000 | [diff] [blame] | 363 | } |
| 364 | |
commit-queue@webkit.org | 932ba2d | 2016-03-26 03:47:23 +0000 | [diff] [blame] | 365 | NEVER_INLINE void outOfLineGrow() |
| 366 | { |
| 367 | m_storage.grow(); |
| 368 | } |
| 369 | |
sbarati@apple.com | 80c907f | 2018-09-28 05:34:38 +0000 | [diff] [blame] | 370 | #if !CPU(ARM64) |
commit-queue@webkit.org | 932ba2d | 2016-03-26 03:47:23 +0000 | [diff] [blame] | 371 | friend LocalWriter; |
sbarati@apple.com | 80c907f | 2018-09-28 05:34:38 +0000 | [diff] [blame] | 372 | #endif |
| 373 | friend LinkBuffer; |
commit-queue@webkit.org | 932ba2d | 2016-03-26 03:47:23 +0000 | [diff] [blame] | 374 | |
benjamin@webkit.org | f6de077 | 2014-07-15 23:59:14 +0000 | [diff] [blame] | 375 | AssemblerData m_storage; |
| 376 | unsigned m_index; |
sbarati@apple.com | 80c907f | 2018-09-28 05:34:38 +0000 | [diff] [blame] | 377 | #if CPU(ARM64E) |
| 378 | ARM64EHash m_hash; |
| 379 | #endif |
ggaren@apple.com | 58f417e | 2008-11-17 06:27:06 +0000 | [diff] [blame] | 380 | }; |
| 381 | |
| 382 | } // namespace JSC |
| 383 | |
| 384 | #endif // ENABLE(ASSEMBLER) |