| 1 | /* |
| 2 | * Copyright (C) 2008-2019 Apple Inc. All rights reserved. |
| 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 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * 3. Neither the name of Apple Inc. ("Apple") nor the names of |
| 14 | * its contributors may be used to endorse or promote products derived |
| 15 | * from this software without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
| 18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 20 | * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
| 21 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #pragma once |
| 30 | |
| 31 | #include <wtf/Assertions.h> |
| 32 | #include <wtf/Vector.h> |
| 33 | #include <limits.h> |
| 34 | |
| 35 | namespace JSC { |
| 36 | template<typename Traits> |
| 37 | class BytecodeGeneratorBase; |
| 38 | struct JSGeneratorTraits; |
| 39 | |
| 40 | template<typename Traits> |
| 41 | class GenericLabel; |
| 42 | |
| 43 | template<typename Traits> |
| 44 | class GenericBoundLabel { |
| 45 | using BytecodeGenerator = BytecodeGeneratorBase<Traits>; |
| 46 | using Label = GenericLabel<Traits>; |
| 47 | |
| 48 | public: |
| 49 | GenericBoundLabel() |
| 50 | : m_type(Offset) |
| 51 | , m_generator(nullptr) |
| 52 | , m_target(0) |
| 53 | { } |
| 54 | |
| 55 | explicit GenericBoundLabel(int target) |
| 56 | : m_type(Offset) |
| 57 | , m_generator(nullptr) |
| 58 | , m_target(target) |
| 59 | { } |
| 60 | |
| 61 | GenericBoundLabel(BytecodeGenerator* generator, Label* label) |
| 62 | : m_type(GeneratorForward) |
| 63 | , m_generator(generator) |
| 64 | , m_label(label) |
| 65 | { } |
| 66 | |
| 67 | GenericBoundLabel(BytecodeGenerator* generator, int offset) |
| 68 | : m_type(GeneratorBackward) |
| 69 | , m_generator(generator) |
| 70 | , m_target(offset) |
| 71 | { } |
| 72 | |
| 73 | int target() |
| 74 | { |
| 75 | switch (m_type) { |
| 76 | case Offset: |
| 77 | return m_target; |
| 78 | case GeneratorBackward: |
| 79 | return m_target - m_generator->m_writer.position(); |
| 80 | case GeneratorForward: |
| 81 | return 0; |
| 82 | default: |
| 83 | RELEASE_ASSERT_NOT_REACHED(); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | int saveTarget() |
| 88 | { |
| 89 | if (m_type == GeneratorForward) { |
| 90 | m_savedTarget = m_generator->m_writer.position(); |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | m_savedTarget = target(); |
| 95 | return m_savedTarget; |
| 96 | } |
| 97 | |
| 98 | int commitTarget() |
| 99 | { |
| 100 | if (m_type == GeneratorForward) { |
| 101 | m_label->m_unresolvedJumps.append(m_savedTarget); |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | return m_savedTarget; |
| 106 | } |
| 107 | |
| 108 | operator int() { return target(); } |
| 109 | |
| 110 | private: |
| 111 | enum Type : uint8_t { |
| 112 | Offset, |
| 113 | GeneratorForward, |
| 114 | GeneratorBackward, |
| 115 | }; |
| 116 | |
| 117 | Type m_type; |
| 118 | int m_savedTarget { 0 }; |
| 119 | BytecodeGenerator* m_generator; |
| 120 | union { |
| 121 | Label* m_label; |
| 122 | int m_target; |
| 123 | }; |
| 124 | }; |
| 125 | |
| 126 | template<typename Traits> |
| 127 | class GenericLabel { |
| 128 | WTF_MAKE_NONCOPYABLE(GenericLabel); |
| 129 | using BytecodeGenerator = BytecodeGeneratorBase<Traits>; |
| 130 | using BoundLabel = GenericBoundLabel<Traits>; |
| 131 | |
| 132 | public: |
| 133 | GenericLabel() = default; |
| 134 | |
| 135 | void setLocation(BytecodeGenerator&, unsigned location); |
| 136 | |
| 137 | BoundLabel bind(BytecodeGenerator* generator) |
| 138 | { |
| 139 | m_bound = true; |
| 140 | if (!isForward()) |
| 141 | return BoundLabel(generator, m_location); |
| 142 | return BoundLabel(generator, this); |
| 143 | } |
| 144 | |
| 145 | BoundLabel bind(unsigned offset) |
| 146 | { |
| 147 | m_bound = true; |
| 148 | if (!isForward()) |
| 149 | return BoundLabel(m_location - offset); |
| 150 | m_unresolvedJumps.append(offset); |
| 151 | return BoundLabel(); |
| 152 | } |
| 153 | |
| 154 | BoundLabel bind() |
| 155 | { |
| 156 | ASSERT(!isForward()); |
| 157 | return bind(0u); |
| 158 | } |
| 159 | |
| 160 | void ref() { ++m_refCount; } |
| 161 | void deref() |
| 162 | { |
| 163 | --m_refCount; |
| 164 | ASSERT(m_refCount >= 0); |
| 165 | } |
| 166 | int refCount() const { return m_refCount; } |
| 167 | bool hasOneRef() const { return m_refCount == 1; } |
| 168 | |
| 169 | bool isForward() const { return m_location == invalidLocation; } |
| 170 | |
| 171 | bool isBound() const { return m_bound; } |
| 172 | |
| 173 | private: |
| 174 | friend BoundLabel; |
| 175 | |
| 176 | typedef Vector<int, 8> JumpVector; |
| 177 | |
| 178 | static constexpr unsigned invalidLocation = UINT_MAX; |
| 179 | |
| 180 | int m_refCount { 0 }; |
| 181 | unsigned m_location { invalidLocation }; |
| 182 | mutable bool m_bound { false }; |
| 183 | mutable JumpVector m_unresolvedJumps; |
| 184 | }; |
| 185 | |
| 186 | using Label = GenericLabel<JSGeneratorTraits>; |
| 187 | using BoundLabel = GenericBoundLabel<JSGeneratorTraits>; |
| 188 | |
| 189 | namespace Wasm { |
| 190 | struct GeneratorTraits; |
| 191 | using Label = GenericLabel<Wasm::GeneratorTraits>; |
| 192 | } |
| 193 | |
| 194 | // This cannot be declared in the Wasm namespace as it conflicts with the |
| 195 | // ruby Wasm namespace when referencing it from BytecodeList.rb |
| 196 | using WasmBoundLabel = GenericBoundLabel<Wasm::GeneratorTraits>; |
| 197 | |
| 198 | } // namespace JSC |
| 199 | |