1/*
2 * Copyright (C) 2014-2018 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 * 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
26#include "config.h"
27#include "GetByIdVariant.h"
28
29#include "CallLinkStatus.h"
30#include "JSCInlines.h"
31#include <wtf/ListDump.h>
32
33namespace JSC {
34
35GetByIdVariant::GetByIdVariant(
36 Box<Identifier> identifier,
37 const StructureSet& structureSet, PropertyOffset offset,
38 const ObjectPropertyConditionSet& conditionSet,
39 std::unique_ptr<CallLinkStatus> callLinkStatus,
40 JSFunction* intrinsicFunction,
41 FunctionPtr<OperationPtrTag> customAccessorGetter,
42 std::unique_ptr<DOMAttributeAnnotation> domAttribute)
43 : m_structureSet(structureSet)
44 , m_conditionSet(conditionSet)
45 , m_offset(offset)
46 , m_callLinkStatus(WTFMove(callLinkStatus))
47 , m_intrinsicFunction(intrinsicFunction)
48 , m_customAccessorGetter(customAccessorGetter)
49 , m_domAttribute(WTFMove(domAttribute))
50 , m_identifier(WTFMove(identifier))
51{
52 if (!structureSet.size()) {
53 ASSERT(offset == invalidOffset);
54 ASSERT(conditionSet.isEmpty());
55 }
56 if (intrinsicFunction)
57 ASSERT(intrinsic() != NoIntrinsic);
58}
59
60GetByIdVariant::~GetByIdVariant() { }
61
62GetByIdVariant::GetByIdVariant(const GetByIdVariant& other)
63 : GetByIdVariant(other.m_identifier)
64{
65 *this = other;
66}
67
68GetByIdVariant& GetByIdVariant::operator=(const GetByIdVariant& other)
69{
70 m_identifier = other.m_identifier;
71 m_structureSet = other.m_structureSet;
72 m_conditionSet = other.m_conditionSet;
73 m_offset = other.m_offset;
74 m_intrinsicFunction = other.m_intrinsicFunction;
75 m_customAccessorGetter = other.m_customAccessorGetter;
76 if (other.m_domAttribute)
77 m_domAttribute = WTF::makeUnique<DOMAttributeAnnotation>(*other.m_domAttribute);
78 else
79 m_domAttribute = nullptr;
80 if (other.m_callLinkStatus)
81 m_callLinkStatus = makeUnique<CallLinkStatus>(*other.m_callLinkStatus);
82 else
83 m_callLinkStatus = nullptr;
84 return *this;
85}
86
87inline bool GetByIdVariant::canMergeIntrinsicStructures(const GetByIdVariant& other) const
88{
89 if (m_intrinsicFunction != other.m_intrinsicFunction)
90 return false;
91 switch (intrinsic()) {
92 case TypedArrayByteLengthIntrinsic: {
93 // We can merge these sets as long as the element size of the two sets is the same.
94 TypedArrayType thisType = (*m_structureSet.begin())->classInfo()->typedArrayStorageType;
95 TypedArrayType otherType = (*other.m_structureSet.begin())->classInfo()->typedArrayStorageType;
96
97 ASSERT(isTypedView(thisType) && isTypedView(otherType));
98
99 return logElementSize(thisType) == logElementSize(otherType);
100 }
101
102 default:
103 return true;
104 }
105 RELEASE_ASSERT_NOT_REACHED();
106}
107
108bool GetByIdVariant::attemptToMerge(const GetByIdVariant& other)
109{
110 if (!!m_identifier != !!other.m_identifier)
111 return false;
112
113 if (m_identifier && (*m_identifier != *other.m_identifier))
114 return false;
115
116 if (m_offset != other.m_offset)
117 return false;
118
119 if (m_callLinkStatus || other.m_callLinkStatus) {
120 if (!(m_callLinkStatus && other.m_callLinkStatus))
121 return false;
122 }
123
124 if (!canMergeIntrinsicStructures(other))
125 return false;
126
127 if (m_customAccessorGetter != other.m_customAccessorGetter)
128 return false;
129
130 if (m_domAttribute || other.m_domAttribute) {
131 if (!(m_domAttribute && other.m_domAttribute))
132 return false;
133 if (*m_domAttribute != *other.m_domAttribute)
134 return false;
135 }
136
137 if (m_conditionSet.isEmpty() != other.m_conditionSet.isEmpty())
138 return false;
139
140 ObjectPropertyConditionSet mergedConditionSet;
141 if (!m_conditionSet.isEmpty()) {
142 mergedConditionSet = m_conditionSet.mergedWith(other.m_conditionSet);
143 if (!mergedConditionSet.isValid())
144 return false;
145 // If this is a hit variant, one slot base should exist. If this is not a hit variant, the slot base is not necessary.
146 if (!isPropertyUnset() && !mergedConditionSet.hasOneSlotBaseCondition())
147 return false;
148 }
149 m_conditionSet = mergedConditionSet;
150
151 m_structureSet.merge(other.m_structureSet);
152
153 if (m_callLinkStatus)
154 m_callLinkStatus->merge(*other.m_callLinkStatus);
155
156 return true;
157}
158
159void GetByIdVariant::markIfCheap(SlotVisitor& visitor)
160{
161 m_structureSet.markIfCheap(visitor);
162}
163
164bool GetByIdVariant::finalize(VM& vm)
165{
166 if (!m_structureSet.isStillAlive(vm))
167 return false;
168 if (!m_conditionSet.areStillLive(vm))
169 return false;
170 if (m_callLinkStatus && !m_callLinkStatus->finalize(vm))
171 return false;
172 if (m_intrinsicFunction && !vm.heap.isMarked(m_intrinsicFunction))
173 return false;
174 return true;
175}
176
177void GetByIdVariant::dump(PrintStream& out) const
178{
179 dumpInContext(out, 0);
180}
181
182void GetByIdVariant::dumpInContext(PrintStream& out, DumpContext* context) const
183{
184 out.print("<");
185 out.print("id='", m_identifier ? *m_identifier : Identifier(), "', ");
186 if (!isSet()) {
187 out.print("empty>");
188 return;
189 }
190 out.print(inContext(structureSet(), context), ", ", inContext(m_conditionSet, context));
191 out.print(", offset = ", offset());
192 if (m_callLinkStatus)
193 out.print(", call = ", *m_callLinkStatus);
194 if (m_intrinsicFunction)
195 out.print(", intrinsic = ", *m_intrinsicFunction);
196 if (m_customAccessorGetter)
197 out.print(", customaccessorgetter = ", RawPointer(m_customAccessorGetter.executableAddress()));
198 if (m_domAttribute) {
199 out.print(", domclass = ", RawPointer(m_domAttribute->classInfo));
200 if (m_domAttribute->domJIT)
201 out.print(", domjit = ", RawPointer(m_domAttribute->domJIT));
202 }
203 out.print(">");
204}
205
206} // namespace JSC
207
208