2 * Copyright (C) 2015 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.
29 #include "DirectArgumentsOffset.h"
30 #include "ScopeOffset.h"
31 #include "VirtualRegister.h"
32 #include <wtf/HashMap.h>
36 enum class VarKind
: uint8_t {
46 : m_kind(VarKind::Invalid
)
51 VarOffset(WTF::HashTableDeletedValueType
)
52 : m_kind(VarKind::Invalid
)
57 explicit VarOffset(VirtualRegister stackOffset
)
59 if (!stackOffset
.isValid()) {
60 m_kind
= VarKind::Invalid
;
63 m_kind
= VarKind::Stack
;
64 m_offset
= stackOffset
.offset();
68 explicit VarOffset(ScopeOffset scopeOffset
)
71 m_kind
= VarKind::Invalid
;
74 m_kind
= VarKind::Scope
;
75 m_offset
= scopeOffset
.offset();
79 explicit VarOffset(DirectArgumentsOffset capturedArgumentsOffset
)
81 if (!capturedArgumentsOffset
) {
82 m_kind
= VarKind::Invalid
;
85 m_kind
= VarKind::DirectArgument
;
86 m_offset
= capturedArgumentsOffset
.offset();
90 static VarOffset
assemble(VarKind kind
, unsigned offset
)
94 result
.m_offset
= offset
;
101 return m_kind
!= VarKind::Invalid
;
104 bool operator!() const
109 VarKind
kind() const { return m_kind
; }
113 return m_kind
== VarKind::Stack
;
118 return m_kind
== VarKind::Scope
;
121 bool isDirectArgument() const
123 return m_kind
== VarKind::DirectArgument
;
126 VirtualRegister
stackOffsetUnchecked() const
129 return VirtualRegister();
130 return VirtualRegister(m_offset
);
133 ScopeOffset
scopeOffsetUnchecked() const
136 return ScopeOffset();
137 return ScopeOffset(m_offset
);
140 DirectArgumentsOffset
capturedArgumentsOffsetUnchecked() const
142 if (!isDirectArgument())
143 return DirectArgumentsOffset();
144 return DirectArgumentsOffset(m_offset
);
147 VirtualRegister
stackOffset() const
150 return VirtualRegister(m_offset
);
153 ScopeOffset
scopeOffset() const
156 return ScopeOffset(m_offset
);
159 DirectArgumentsOffset
capturedArgumentsOffset() const
161 ASSERT(isDirectArgument());
162 return DirectArgumentsOffset(m_offset
);
165 unsigned rawOffset() const
171 void checkSanity() const
177 case VarKind::Invalid
:
178 ASSERT(m_offset
== UINT_MAX
);
181 ASSERT(scopeOffset());
184 ASSERT(stackOffset().isValid());
186 case VarKind::DirectArgument
:
187 ASSERT(capturedArgumentsOffset());
191 ASSERT_NOT_REACHED();
194 bool operator==(const VarOffset
& other
) const
196 return m_kind
== other
.m_kind
197 && m_offset
== other
.m_offset
;
200 bool operator!=(const VarOffset
& other
) const
202 return !(*this == other
);
205 unsigned hash() const
207 return WTF::IntHash
<unsigned>::hash((static_cast<unsigned>(m_kind
) << 20) + m_offset
);
210 bool isHashTableDeletedValue() const
212 return m_kind
== VarKind::Invalid
&& !m_offset
;
215 void dump(PrintStream
&) const;
222 struct VarOffsetHash
{
223 static unsigned hash(const VarOffset
& key
) { return key
.hash(); }
224 static bool equal(const VarOffset
& a
, const VarOffset
& b
) { return a
== b
; }
225 static const bool safeToCompareToEmptyOrDeleted
= true;
232 void printInternal(PrintStream
&, JSC::VarKind
);
234 template<typename T
> struct DefaultHash
;
235 template<> struct DefaultHash
<JSC::VarOffset
> {
236 typedef JSC::VarOffsetHash Hash
;
239 template<typename T
> struct HashTraits
;
240 template<> struct HashTraits
<JSC::VarOffset
> : SimpleClassHashTraits
<JSC::VarOffset
> {
241 static const bool emptyValueIsZero
= false;
246 #endif // VarOffset_h