1 /*
2 * Copyright (c) 2021 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/tagged_array-inl.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 fixed_idx = GetKeyIndex(index);
54 TaggedArray::Set(thread, fixed_idx, key);
55 TaggedArray::Set(thread, fixed_idx + 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 fixed_idx = GetAttrIndex(index);
61 PropertyAttributes oldAttr(TaggedArray::Get(fixed_idx));
62 oldAttr.SetNormalAttr(attr.GetNormalAttr());
63 TaggedArray::Set(thread, fixed_idx, oldAttr.GetTaggedValue());
64 }
65
GetKey(int index)66 inline JSTaggedValue LayoutInfo::GetKey(int index) const
67 {
68 uint32_t fixed_idx = GetKeyIndex(index);
69 return TaggedArray::Get(fixed_idx);
70 }
71
GetAttr(int index)72 inline PropertyAttributes LayoutInfo::GetAttr(int index) const
73 {
74 uint32_t fixed_idx = GetAttrIndex(index);
75 return PropertyAttributes(TaggedArray::Get(fixed_idx));
76 }
77
GetSortedKey(int index)78 inline JSTaggedValue LayoutInfo::GetSortedKey(int index) const
79 {
80 uint32_t fixed_idx = GetSortedIndex(index);
81 return GetKey(fixed_idx);
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 fixed_idx = GetAttrIndex(index);
92 PropertyAttributes attr(TaggedArray::Get(fixed_idx));
93 attr.SetSortedIndex(sortedIndex);
94 TaggedArray::Set(thread, fixed_idx, attr.GetTaggedValue());
95 }
96
FindElementWithCache(JSThread * thread,JSHClass * cls,JSTaggedValue key,int propertiesNumber)97 inline int LayoutInfo::FindElementWithCache(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 PropertiesCache *cache = thread->GetPropertiesCache();
113 int index = cache->Get(cls, key);
114 if (index == PropertiesCache::NOT_FOUND) {
115 index = BinarySearch(key, propertiesNumber);
116 cache->Set(cls, key, index);
117 }
118 return index;
119 }
120
BinarySearch(JSTaggedValue key,int propertiesNumber)121 inline int LayoutInfo::BinarySearch(JSTaggedValue key, int propertiesNumber)
122 {
123 ASSERT(NumberOfElements() >= propertiesNumber);
124 int low = 0;
125 int elements = NumberOfElements();
126 int high = elements - 1;
127 uint32_t keyHash = key.GetKeyHashCode();
128
129 ASSERT(low <= high);
130
131 while (low <= high) {
132 int mid = low + (high - low) / 2; // 2: half
133 JSTaggedValue midKey = GetSortedKey(mid);
134 uint32_t midHash = midKey.GetKeyHashCode();
135 if (midHash > keyHash) {
136 high = mid - 1;
137 } else if (midHash < keyHash) {
138 low = mid + 1;
139 } else {
140 int sortIndex = static_cast<int>(GetSortedIndex(mid));
141 JSTaggedValue currentKey = GetKey(sortIndex);
142 if (currentKey == key) {
143 return sortIndex < propertiesNumber ? sortIndex : -1;
144 }
145 int midLeft = mid;
146 int midRight = mid;
147 while (midLeft - 1 >= 0) {
148 sortIndex = static_cast<int>(GetSortedIndex(--midLeft));
149 currentKey = GetKey(sortIndex);
150 if (currentKey.GetKeyHashCode() == keyHash) {
151 if (currentKey == key) {
152 return sortIndex < propertiesNumber ? sortIndex : -1;
153 }
154 } else {
155 break;
156 }
157 }
158 while (midRight + 1 < elements) {
159 sortIndex = static_cast<int>(GetSortedIndex(++midRight));
160 currentKey = GetKey(sortIndex);
161 if (currentKey.GetKeyHashCode() == keyHash) {
162 if (currentKey == key) {
163 return sortIndex < propertiesNumber ? sortIndex : -1;
164 }
165 } else {
166 break;
167 }
168 }
169 return -1;
170 }
171 }
172 return -1;
173 }
174 } // namespace panda::ecmascript
175 #endif // ECMASCRIPT_LAYOUT_INFO_INL_H
176