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
SetWithoutBarrier(uint32_t idx,const JSTaggedValue & value)50 inline void LayoutInfo::SetWithoutBarrier(uint32_t idx, const JSTaggedValue &value)
51 {
52 ASSERT(idx < GetLength());
53 ASSERT(!value.IsHeapObject());
54 size_t offset = JSTaggedValue::TaggedTypeSize() * idx;
55
56 Barriers::SetPrimitive<JSTaggedType>(GetData(), offset, value.GetRawData());
57 }
58
SetPropertyInit(const JSThread * thread,int index,const JSTaggedValue & key,const PropertyAttributes & attr)59 inline void LayoutInfo::SetPropertyInit(const JSThread *thread, int index, const JSTaggedValue &key,
60 const PropertyAttributes &attr)
61 {
62 uint32_t fixedIdx = GetKeyIndex(index);
63 TaggedArray::Set(thread, fixedIdx, key);
64 TaggedArray::Set(thread, fixedIdx + ATTR_INDEX_OFFSET, attr.GetNormalTagged());
65 }
66
SetNormalAttr(const JSThread * thread,int index,const PropertyAttributes & attr)67 inline void LayoutInfo::SetNormalAttr(const JSThread *thread, int index, const PropertyAttributes &attr)
68 {
69 uint32_t fixedIdx = GetAttrIndex(index);
70 PropertyAttributes oldAttr(TaggedArray::Get(thread, fixedIdx));
71 oldAttr.SetNormalAttr(attr.GetNormalAttr());
72 TaggedArray::Set(thread, fixedIdx, oldAttr.GetTaggedValue());
73 }
74
GetKey(const JSThread * thread,int index)75 inline JSTaggedValue LayoutInfo::GetKey(const JSThread *thread, int index) const
76 {
77 uint32_t fixedIdx = GetKeyIndex(index);
78 return TaggedArray::Get(thread, fixedIdx);
79 }
80
GetAttr(const JSThread * thread,int index)81 inline PropertyAttributes LayoutInfo::GetAttr(const JSThread *thread, int index) const
82 {
83 uint32_t fixedIdx = GetAttrIndex(index);
84 return PropertyAttributes(TaggedArray::Get(thread, fixedIdx));
85 }
86
87 template<RBMode mode>
GetAttr(const JSThread * thread,int index)88 inline PropertyAttributes LayoutInfo::GetAttr(const JSThread *thread, int index) const
89 {
90 uint32_t fixedIdx = GetAttrIndex(index);
91 return PropertyAttributes(TaggedArray::Get<mode>(thread, fixedIdx));
92 }
93
GetSortedKey(const JSThread * thread,int index)94 inline JSTaggedValue LayoutInfo::GetSortedKey(const JSThread *thread, int index) const
95 {
96 uint32_t fixedIdx = GetSortedIndex(thread, index);
97 return GetKey(thread, fixedIdx);
98 }
99
GetSortedIndex(const JSThread * thread,int index)100 inline uint32_t LayoutInfo::GetSortedIndex(const JSThread *thread, int index) const
101 {
102 return GetAttr(thread, index).GetSortedIndex();
103 }
104
SetSortedIndex(const JSThread * thread,int index,int sortedIndex)105 inline void LayoutInfo::SetSortedIndex(const JSThread *thread, int index, int sortedIndex)
106 {
107 uint32_t fixedIdx = GetAttrIndex(index);
108 PropertyAttributes attr(TaggedArray::Get(thread, fixedIdx));
109 attr.SetSortedIndex(sortedIndex);
110 TaggedArray::Set(thread, fixedIdx, attr.GetTaggedValue());
111 }
112
FindElementWithCache(const JSThread * thread,JSHClass * cls,JSTaggedValue key,int propertiesNumber)113 inline int LayoutInfo::FindElementWithCache(const JSThread *thread, JSHClass *cls, JSTaggedValue key,
114 int propertiesNumber)
115 {
116 ASSERT(NumberOfElements() >= propertiesNumber);
117 const int MAX_ELEMENTS_LINER_SEARCH = 9; // 9: Builtins Object properties number is nine;
118 if (propertiesNumber <= MAX_ELEMENTS_LINER_SEARCH) {
119 void *properties = reinterpret_cast<void *>(GetProperties());
120 size_t keyOffset = 0;
121 if (thread->NeedReadBarrier()) {
122 for (int i = 0; i < propertiesNumber; i++) {
123 JSTaggedValue propKey(Barriers::GetTaggedValue<RBMode::FAST_CMC_RB>(thread,
124 ToUintPtr(properties) + i * sizeof(Properties) + keyOffset));
125 if (propKey == key) {
126 return i;
127 }
128 }
129 } else {
130 for (int i = 0; i < propertiesNumber; i++) {
131 JSTaggedValue propKey(Barriers::GetTaggedValue<RBMode::FAST_NO_RB>(thread,
132 ToUintPtr(properties) + i * sizeof(Properties) + keyOffset));
133 if (propKey == key) {
134 return i;
135 }
136 }
137 }
138 return -1;
139 }
140
141 // jit compile thread not use cache
142 if (thread->IsJitThread()) {
143 return BinarySearch(thread, key, propertiesNumber);
144 }
145
146 PropertiesCache *cache = thread->GetPropertiesCache();
147 int index = cache->Get(thread, cls, key);
148 if (index == PropertiesCache::NOT_FOUND) {
149 index = BinarySearch(thread, key, propertiesNumber);
150 if (index != -1) {
151 cache->Set(thread, cls, key, index);
152 }
153 }
154 return index;
155 }
156
BinarySearch(const JSThread * thread,JSTaggedValue key,int propertiesNumber)157 inline int LayoutInfo::BinarySearch(const JSThread *thread, JSTaggedValue key, int propertiesNumber)
158 {
159 ASSERT(NumberOfElements() >= propertiesNumber);
160 int low = 0;
161 int elements = NumberOfElements();
162 int high = elements - 1;
163 uint32_t keyHash = key.GetKeyHashCode(thread);
164
165 ASSERT(low <= high);
166
167 while (low <= high) {
168 int mid = low + (high - low) / 2; // 2: half
169 JSTaggedValue midKey = GetSortedKey(thread, mid);
170 uint32_t midHash = midKey.GetKeyHashCode(thread);
171 if (midHash > keyHash) {
172 high = mid - 1;
173 } else if (midHash < keyHash) {
174 low = mid + 1;
175 } else {
176 int sortIndex = static_cast<int>(GetSortedIndex(thread, mid));
177 JSTaggedValue currentKey = GetKey(thread, sortIndex);
178 if (currentKey == key) {
179 return sortIndex < propertiesNumber ? sortIndex : -1;
180 }
181 int midLeft = mid;
182 int midRight = mid;
183 while (midLeft - 1 >= 0) {
184 sortIndex = static_cast<int>(GetSortedIndex(thread, --midLeft));
185 currentKey = GetKey(thread, sortIndex);
186 if (currentKey.GetKeyHashCode(thread) == keyHash) {
187 if (currentKey == key) {
188 return sortIndex < propertiesNumber ? sortIndex : -1;
189 }
190 } else {
191 break;
192 }
193 }
194 while (midRight + 1 < elements) {
195 sortIndex = static_cast<int>(GetSortedIndex(thread, ++midRight));
196 currentKey = GetKey(thread, sortIndex);
197 if (currentKey.GetKeyHashCode(thread) == keyHash) {
198 if (currentKey == key) {
199 return sortIndex < propertiesNumber ? sortIndex : -1;
200 }
201 } else {
202 break;
203 }
204 }
205 return -1;
206 }
207 }
208 return -1;
209 }
210
SetIsNotHole(const JSThread * thread,int index)211 inline void LayoutInfo::SetIsNotHole(const JSThread *thread, int index)
212 {
213 uint32_t fixedIdx = GetAttrIndex(index);
214 PropertyAttributes attr(TaggedArray::Get(thread, fixedIdx));
215 attr.SetIsNotHole(true);
216 TaggedArray::Set(thread, fixedIdx, attr.GetTaggedValue());
217 }
218
UpdateTrackTypeAttr(const JSThread * thread,int index,const PropertyAttributes & attr)219 inline void LayoutInfo::UpdateTrackTypeAttr(const JSThread *thread, int index, const PropertyAttributes &attr)
220 {
221 uint32_t fixedIdx = GetAttrIndex(index);
222 PropertyAttributes oldAttr(TaggedArray::Get(thread, fixedIdx));
223 oldAttr.SetNormalAttr(attr.GetNormalAttr());
224 oldAttr.SetIsPGODumped(false);
225 SetWithoutBarrier(fixedIdx, oldAttr.GetTaggedValue());
226 }
227
SetIsPGODumped(const JSThread * thread,int index)228 inline void LayoutInfo::SetIsPGODumped(const JSThread *thread, int index)
229 {
230 uint32_t fixedIdx = GetAttrIndex(index);
231 PropertyAttributes attr(TaggedArray::Get(thread, fixedIdx));
232 attr.SetIsPGODumped(true);
233 SetWithoutBarrier(fixedIdx, attr.GetTaggedValue());
234 }
235
CheckIsDuplicateKey(const JSThread * thread,int curKeyIdx,uint32_t curKeyHashCode,const JSTaggedValue & key)236 inline bool LayoutInfo::CheckIsDuplicateKey(const JSThread *thread, int curKeyIdx, uint32_t curKeyHashCode,
237 const JSTaggedValue &key)
238 {
239 while (curKeyIdx > 0) {
240 JSTaggedValue prevKey = GetSortedKey(thread, --curKeyIdx);
241 if (prevKey.GetKeyHashCode(thread) < curKeyHashCode) {
242 return false;
243 }
244 if (prevKey == key) {
245 return true;
246 }
247 }
248 return false;
249 }
250
251 template<bool checkDuplicateKeys /* = false*/>
AddKey(const JSThread * thread,int index,const JSTaggedValue & key,const PropertyAttributes & attr)252 void LayoutInfo::AddKey(const JSThread *thread, [[maybe_unused]] int index, const JSTaggedValue &key,
253 const PropertyAttributes &attr)
254 {
255 int number = NumberOfElements();
256 ASSERT(attr.GetOffset() == static_cast<uint32_t>(number));
257 ASSERT(number + 1 <= GetPropertiesCapacity());
258 ASSERT(number == index);
259 SetNumberOfElements(thread, number + 1);
260 SetPropertyInit(thread, number, key, attr);
261
262 uint32_t keyHash = key.GetKeyHashCode(thread);
263 int insertIndex = number;
264 for (; insertIndex > 0; --insertIndex) {
265 auto sortedIndex = GetSortedIndex(thread, insertIndex - 1);
266 JSTaggedValue prevKey = GetKey(thread, sortedIndex);
267 if (prevKey.GetKeyHashCode(thread) <= keyHash) {
268 break;
269 }
270 SetSortedIndex(thread, insertIndex, sortedIndex);
271 }
272 SetSortedIndex(thread, insertIndex, number);
273 if constexpr (checkDuplicateKeys) {
274 if (CheckIsDuplicateKey(thread, insertIndex, keyHash, key)) {
275 THROW_TYPE_ERROR(const_cast<JSThread *>(thread), "property keys can not duplicate");
276 }
277 }
278 }
279 } // namespace panda::ecmascript
280 #endif // ECMASCRIPT_LAYOUT_INFO_INL_H
281