| 1 | /* |
| 2 | * Copyright (C) 2008 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 "Instruction.h" |
| 32 | #include <wtf/Assertions.h> |
| 33 | #include <wtf/Vector.h> |
| 34 | #include <limits.h> |
| 35 | |
| 36 | namespace JSC { |
| 37 | class BytecodeGenerator; |
| 38 | class Label; |
| 39 | |
| 40 | class BoundLabel { |
| 41 | public: |
| 42 | BoundLabel() |
| 43 | : m_type(Offset) |
| 44 | , m_generator(nullptr) |
| 45 | , m_target(0) |
| 46 | { } |
| 47 | |
| 48 | explicit BoundLabel(int target) |
| 49 | : m_type(Offset) |
| 50 | , m_generator(nullptr) |
| 51 | , m_target(target) |
| 52 | { } |
| 53 | |
| 54 | BoundLabel(BytecodeGenerator* generator, Label* label) |
| 55 | : m_type(GeneratorForward) |
| 56 | , m_generator(generator) |
| 57 | , m_label(label) |
| 58 | { } |
| 59 | |
| 60 | BoundLabel(BytecodeGenerator* generator, int offset) |
| 61 | : m_type(GeneratorBackward) |
| 62 | , m_generator(generator) |
| 63 | , m_target(offset) |
| 64 | { } |
| 65 | |
| 66 | int target(); |
| 67 | int saveTarget(); |
| 68 | int commitTarget(); |
| 69 | |
| 70 | operator int() { return target(); } |
| 71 | |
| 72 | private: |
| 73 | enum Type : uint8_t { |
| 74 | Offset, |
| 75 | GeneratorForward, |
| 76 | GeneratorBackward, |
| 77 | }; |
| 78 | |
| 79 | Type m_type; |
| 80 | int m_savedTarget { 0 }; |
| 81 | BytecodeGenerator* m_generator; |
| 82 | union { |
| 83 | Label* m_label; |
| 84 | int m_target; |
| 85 | }; |
| 86 | }; |
| 87 | |
| 88 | class Label { |
| 89 | WTF_MAKE_NONCOPYABLE(Label); |
| 90 | public: |
| 91 | Label() = default; |
| 92 | |
| 93 | void setLocation(BytecodeGenerator&, unsigned); |
| 94 | |
| 95 | BoundLabel bind(BytecodeGenerator* generator) |
| 96 | { |
| 97 | m_bound = true; |
| 98 | if (!isForward()) |
| 99 | return BoundLabel(generator, m_location); |
| 100 | return BoundLabel(generator, this); |
| 101 | } |
| 102 | |
| 103 | BoundLabel bind(unsigned offset) |
| 104 | { |
| 105 | m_bound = true; |
| 106 | if (!isForward()) |
| 107 | return BoundLabel(m_location - offset); |
| 108 | m_unresolvedJumps.append(offset); |
| 109 | return BoundLabel(); |
| 110 | } |
| 111 | |
| 112 | BoundLabel bind() |
| 113 | { |
| 114 | ASSERT(!isForward()); |
| 115 | return bind(0u); |
| 116 | } |
| 117 | |
| 118 | void ref() { ++m_refCount; } |
| 119 | void deref() |
| 120 | { |
| 121 | --m_refCount; |
| 122 | ASSERT(m_refCount >= 0); |
| 123 | } |
| 124 | int refCount() const { return m_refCount; } |
| 125 | bool hasOneRef() const { return m_refCount == 1; } |
| 126 | |
| 127 | bool isForward() const { return m_location == invalidLocation; } |
| 128 | |
| 129 | bool isBound() const { return m_bound; } |
| 130 | |
| 131 | private: |
| 132 | friend class BoundLabel; |
| 133 | |
| 134 | typedef Vector<int, 8> JumpVector; |
| 135 | |
| 136 | static const unsigned invalidLocation = UINT_MAX; |
| 137 | |
| 138 | int m_refCount { 0 }; |
| 139 | unsigned m_location { invalidLocation }; |
| 140 | mutable bool m_bound { false }; |
| 141 | mutable JumpVector m_unresolvedJumps; |
| 142 | }; |
| 143 | |
| 144 | } // namespace JSC |
| 145 | |