1 /*
2 * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef ECMASCRIPT_LAYOUT_INFO_INL_H
17 #define ECMASCRIPT_LAYOUT_INFO_INL_H
18
19 #include "ecmascript/layout_info.h"
20
21 #include "ecmascript/ic/properties_cache.h"
22 #include "ecmascript/js_thread.h"
23
24 namespace panda::ecmascript {
GetPropertiesCapacity()25 inline int LayoutInfo::GetPropertiesCapacity() const
26 {
27 return static_cast<int>((GetLength()) >> ELEMENTS_INDEX_LOG2);
28 }
29
NumberOfElements()30 inline int LayoutInfo::NumberOfElements() const
31 {
32 return GetExtraLength();
33 }
34
SetNumberOfElements(const JSThread * thread,int properties)35 inline void LayoutInfo::SetNumberOfElements([[maybe_unused]] const JSThread *thread, int properties)
36 {
37 SetExtraLength(properties);
38 }
39
GetKeyIndex(int index)40 inline uint32_t LayoutInfo::GetKeyIndex(int index) const
41 {
42 return static_cast<uint32_t>(index) << ELEMENTS_INDEX_LOG2;
43 }
44
GetAttrIndex(int index)45 inline uint32_t LayoutInfo::GetAttrIndex(int index) const
46 {
47 return (static_cast<uint32_t>(index) << ELEMENTS_INDEX_LOG2) + ATTR_INDEX_OFFSET;
48 }
49
SetPropertyInit(const JSThread * thread,int index,const JSTaggedValue & key,const PropertyAttributes & attr)50 inline void LayoutInfo::SetPropertyInit(const JSThread *thread, int index, const JSTaggedValue &key,
51 const PropertyAttributes &attr)
52 {
53 uint32_t fixedIdx = GetKeyIndex(index);
54 TaggedArray::Set(thread, fixedIdx, key);
55 TaggedArray::Set(thread, fixedIdx + ATTR_INDEX_OFFSET, attr.GetNormalTagged());
56 }
57
SetNormalAttr(const JSThread * thread,int index,const PropertyAttributes & attr)58 inline void LayoutInfo::SetNormalAttr(const JSThread *thread, int index, const PropertyAttributes &attr)
59 {
60 uint32_t fixedIdx = GetAttrIndex(index);
61 PropertyAttributes oldAttr(TaggedArray::Get(fixedIdx));
62 oldAttr.SetNormalAttr(attr.GetNormalAttr());
63 TaggedArray::Set(thread, fixedIdx, oldAttr.GetTaggedValue());
64 }
65
GetKey(int index)66 inline JSTaggedValue LayoutInfo::GetKey(int index) const
67 {
68 uint32_t fixedIdx = GetKeyIndex(index);
69 return TaggedArray::Get(fixedIdx);
70 }
71
GetAttr(int index)72 inline PropertyAttributes LayoutInfo::GetAttr(int index) const
73 {
74 uint32_t fixedIdx = GetAttrIndex(index);
75 return PropertyAttributes(TaggedArray::Get(fixedIdx));
76 }
77
GetSortedKey(int index)78 inline JSTaggedValue LayoutInfo::GetSortedKey(int index) const
79 {
80 uint32_t fixedIdx = GetSortedIndex(index);
81 return GetKey(fixedIdx);
82 }
83
GetSortedIndex(int index)84 inline uint32_t LayoutInfo::GetSortedIndex(int index) const
85 {
86 return GetAttr(index).GetSortedIndex();
87 }
88
SetSortedIndex(const JSThread * thread,int index,int sortedIndex)89 inline void LayoutInfo::SetSortedIndex(const JSThread *thread, int index, int sortedIndex)
90 {
91 uint32_t fixedIdx = GetAttrIndex(index);
92 PropertyAttributes attr(TaggedArray::Get(fixedIdx));
93 attr.SetSortedIndex(sortedIndex);
94 TaggedArray::Set(thread, fixedIdx, attr.GetTaggedValue());
95 }
96
FindElementWithCache(const JSThread * thread,JSHClass * cls,JSTaggedValue key,int propertiesNumber)97 inline int LayoutInfo::FindElementWithCache(const JSThread *thread, JSHClass *cls, JSTaggedValue key,
98 int propertiesNumber)
99 {
100 ASSERT(NumberOfElements() >= propertiesNumber);
101 const int MAX_ELEMENTS_LINER_SEARCH = 9; // 9: Builtins Object properties number is nine;
102 if (propertiesNumber <= MAX_ELEMENTS_LINER_SEARCH) {
103 Span<struct Properties> sp(GetProperties(), propertiesNumber);
104 for (int i = 0; i < propertiesNumber; i++) {
105 if (sp[i].key_ == key) {
106 return i;
107 }
108 }
109 return -1;
110 }
111
112 // jit compile thread not use cache
113 if (thread->IsJitThread()) {
114 return BinarySearch(key, propertiesNumber);
115 }
116
117 PropertiesCache *cache = thread->GetPropertiesCache();
118 int index = cache->Get(cls, key);
119 if (index == PropertiesCache::NOT_FOUND) {
120 index = BinarySearch(key, propertiesNumber);
121 if (index != -1) {
122 cache->Set(cls, key, index);
123 }
124 }
125 return index;
126 }
127
BinarySearch(JSTaggedValue key,int propertiesNumber)128 inline int LayoutInfo::BinarySearch(JSTaggedValue key, int propertiesNumber)
129 {
130 ASSERT(NumberOfElements() >= propertiesNumber);
131 int low = 0;
132 int elements = NumberOfElements();
133 int high = elements - 1;
134 uint32_t keyHash = key.GetKeyHashCode();
135
136 ASSERT(low <= high);
137
138 while (low <= high) {
139 int mid = low + (high - low) / 2; // 2: half
140 JSTaggedValue midKey = GetSortedKey(mid);
141 uint32_t midHash = midKey.GetKeyHashCode();
142 if (midHash > keyHash) {
143 high = mid - 1;
144 } else if (midHash < keyHash) {
145 low = mid + 1;
146 } else {
147 int sortIndex = static_cast<int>(GetSortedIndex(mid));
148 JSTaggedValue currentKey = GetKey(sortIndex);
149 if (currentKey == key) {
150 return sortIndex < propertiesNumber ? sortIndex : -1;
151 }
152 int midLeft = mid;
153 int midRight = mid;
154 while (midLeft - 1 >= 0) {
155 sortIndex = static_cast<int>(GetSortedIndex(--midLeft));
156 currentKey = GetKey(sortIndex);
157 if (currentKey.GetKeyHashCode() == keyHash) {
158 if (currentKey == key) {
159 return sortIndex < propertiesNumber ? sortIndex : -1;
160 }
161 } else {
162 break;
163 }
164 }
165 while (midRight + 1 < elements) {
166 sortIndex = static_cast<int>(GetSortedIndex(++midRight));
167 currentKey = GetKey(sortIndex);
168 if (currentKey.GetKeyHashCode() == keyHash) {
169 if (currentKey == key) {
170 return sortIndex < propertiesNumber ? sortIndex : -1;
171 }
172 } else {
173 break;
174 }
175 }
176 return -1;
177 }
178 }
179 return -1;
180 }
181
SetIsNotHole(const JSThread * thread,int index)182 inline void LayoutInfo::SetIsNotHole(const JSThread *thread, int index)
183 {
184 uint32_t fixedIdx = GetAttrIndex(index);
185 PropertyAttributes attr(TaggedArray::Get(fixedIdx));
186 attr.SetIsNotHole(true);
187 TaggedArray::Set(thread, fixedIdx, attr.GetTaggedValue());
188 }
189
UpdateTrackTypeAttr(int index,const PropertyAttributes & attr)190 inline void LayoutInfo::UpdateTrackTypeAttr(int index, const PropertyAttributes &attr)
191 {
192 uint32_t fixedIdx = GetAttrIndex(index);
193 PropertyAttributes oldAttr(TaggedArray::Get(fixedIdx));
194 oldAttr.SetNormalAttr(attr.GetNormalAttr());
195 oldAttr.SetIsPGODumped(false);
196 TaggedArray::Set(fixedIdx, oldAttr.GetTaggedValue());
197 }
198
SetIsPGODumped(int index)199 inline void LayoutInfo::SetIsPGODumped(int index)
200 {
201 uint32_t fixedIdx = GetAttrIndex(index);
202 PropertyAttributes attr(TaggedArray::Get(fixedIdx));
203 attr.SetIsPGODumped(true);
204 TaggedArray::Set(fixedIdx, attr.GetTaggedValue());
205 }
206
207 template<bool checkDuplicateKeys /* = false*/>
AddKey(const JSThread * thread,int index,const JSTaggedValue & key,const PropertyAttributes & attr)208 void LayoutInfo::AddKey(const JSThread *thread, [[maybe_unused]] int index, const JSTaggedValue &key,
209 const PropertyAttributes &attr)
210 {
211 DISALLOW_GARBAGE_COLLECTION;
212 int number = NumberOfElements();
213 ASSERT(attr.GetOffset() == static_cast<uint32_t>(number));
214 ASSERT(number + 1 <= GetPropertiesCapacity());
215 ASSERT(number == index);
216 SetNumberOfElements(thread, number + 1);
217 SetPropertyInit(thread, number, key, attr);
218
219 uint32_t keyHash = key.GetKeyHashCode();
220 int insertIndex = number;
221 for (; insertIndex > 0; --insertIndex) {
222 JSTaggedValue prevKey = GetSortedKey(insertIndex - 1);
223 if (prevKey.GetKeyHashCode() <= keyHash) {
224 break;
225 }
226 SetSortedIndex(thread, insertIndex, GetSortedIndex(insertIndex - 1));
227 }
228 SetSortedIndex(thread, insertIndex, number);
229 if constexpr (checkDuplicateKeys) {
230 while (insertIndex > 0) {
231 JSTaggedValue prevKey = GetSortedKey(--insertIndex);
232 if (prevKey.GetKeyHashCode() < keyHash) {
233 return;
234 }
235 if (prevKey == key) {
236 THROW_TYPE_ERROR(const_cast<JSThread *>(thread), "property keys can not duplicate");
237 }
238 }
239 }
240 }
241 } // namespace panda::ecmascript
242 #endif // ECMASCRIPT_LAYOUT_INFO_INL_H
243