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 #include "ecmascript/tagged_array-inl.h"
21 #include "ecmascript/ic/properties_cache-inl.h"
22
23 namespace panda::ecmascript {
GetPropertiesCapacity()24 inline int LayoutInfo::GetPropertiesCapacity() const
25 {
26 return static_cast<int>((GetLength() - ELEMENTS_START_INDEX) >> 1U);
27 }
28
NumberOfElements()29 inline int LayoutInfo::NumberOfElements() const
30 {
31 return TaggedArray::Get(NUMBER_OF_PROPERTIES_INDEX).GetInt();
32 }
33
SetNumberOfElements(const JSThread * thread,int properties)34 inline void LayoutInfo::SetNumberOfElements(const JSThread *thread, int properties)
35 {
36 return TaggedArray::Set(thread, NUMBER_OF_PROPERTIES_INDEX, JSTaggedValue(properties));
37 }
38
GetKeyIndex(int index)39 inline uint32_t LayoutInfo::GetKeyIndex(int index) const
40 {
41 return ELEMENTS_START_INDEX + (static_cast<uint32_t>(index) << 1U);
42 }
43
GetAttrIndex(int index)44 inline uint32_t LayoutInfo::GetAttrIndex(int index) const
45 {
46 return ELEMENTS_START_INDEX + (static_cast<uint32_t>(index) << 1U) + 1;
47 }
48
SetPropertyInit(const JSThread * thread,int index,const JSTaggedValue & key,const PropertyAttributes & attr)49 inline void LayoutInfo::SetPropertyInit(const JSThread *thread, int index, const JSTaggedValue &key,
50 const PropertyAttributes &attr)
51 {
52 uint32_t fixed_idx = GetKeyIndex(index);
53 TaggedArray::Set(thread, fixed_idx, key);
54 TaggedArray::Set(thread, fixed_idx + 1, attr.GetNormalTagged());
55 }
56
SetNormalAttr(const JSThread * thread,int index,const PropertyAttributes & attr)57 inline void LayoutInfo::SetNormalAttr(const JSThread *thread, int index, const PropertyAttributes &attr)
58 {
59 uint32_t fixed_idx = GetAttrIndex(index);
60 PropertyAttributes oldAttr(TaggedArray::Get(fixed_idx));
61 oldAttr.SetNormalAttr(attr.GetNormalAttr());
62 TaggedArray::Set(thread, fixed_idx, oldAttr.GetTaggedValue());
63 }
64
GetKey(int index)65 inline JSTaggedValue LayoutInfo::GetKey(int index) const
66 {
67 uint32_t fixed_idx = GetKeyIndex(index);
68 return TaggedArray::Get(fixed_idx);
69 }
70
GetAttr(int index)71 inline PropertyAttributes LayoutInfo::GetAttr(int index) const
72 {
73 uint32_t fixed_idx = GetAttrIndex(index);
74 return PropertyAttributes(TaggedArray::Get(fixed_idx));
75 }
76
GetSortedKey(int index)77 inline JSTaggedValue LayoutInfo::GetSortedKey(int index) const
78 {
79 uint32_t fixed_idx = GetSortedIndex(index);
80 return GetKey(fixed_idx);
81 }
82
GetSortedIndex(int index)83 inline uint32_t LayoutInfo::GetSortedIndex(int index) const
84 {
85 return GetAttr(index).GetSortedIndex();
86 }
87
SetSortedIndex(const JSThread * thread,int index,int sortedIndex)88 inline void LayoutInfo::SetSortedIndex(const JSThread *thread, int index, int sortedIndex)
89 {
90 uint32_t fixed_idx = GetAttrIndex(index);
91 PropertyAttributes attr(TaggedArray::Get(fixed_idx));
92 attr.SetSortedIndex(sortedIndex);
93 TaggedArray::Set(thread, fixed_idx, attr.GetTaggedValue());
94 }
95
FindElementWithCache(JSThread * thread,JSHClass * cls,JSTaggedValue key,int propertiesNumber)96 inline int LayoutInfo::FindElementWithCache(JSThread *thread, JSHClass *cls, JSTaggedValue key,
97 int propertiesNumber)
98 {
99 ASSERT(NumberOfElements() >= propertiesNumber);
100 const int MAX_ELEMENTS_LINER_SEARCH = 9; // 9: Builtins Object properties number is nine;
101 if (propertiesNumber <= MAX_ELEMENTS_LINER_SEARCH) {
102 Span<struct Properties> sp(GetProperties(), propertiesNumber);
103 for (int i = 0; i < propertiesNumber; i++) {
104 if (sp[i].key_ == key) {
105 return i;
106 }
107 }
108 return -1;
109 }
110
111 PropertiesCache *cache = thread->GetPropertiesCache();
112 int index = cache->Get(cls, key);
113 if (index == PropertiesCache::NOT_FOUND) {
114 index = BinarySearch(key, propertiesNumber);
115 cache->Set(cls, key, index);
116 }
117 return index;
118 }
119
BinarySearch(JSTaggedValue key,int propertiesNumber)120 inline int LayoutInfo::BinarySearch(JSTaggedValue key, int propertiesNumber)
121 {
122 ASSERT(NumberOfElements() >= propertiesNumber);
123 int low = 0;
124 int elements = NumberOfElements();
125 int high = elements - 1;
126 uint32_t keyHash = key.GetKeyHashCode();
127
128 ASSERT(low <= high);
129
130 while (low <= high) {
131 int mid = low + (high - low) / 2; // 2: half
132 JSTaggedValue midKey = GetSortedKey(mid);
133 uint32_t midHash = midKey.GetKeyHashCode();
134 if (midHash > keyHash) {
135 high = mid - 1;
136 } else if (midHash < keyHash) {
137 low = mid + 1;
138 } else {
139 int sortIndex = GetSortedIndex(mid);
140 JSTaggedValue currentKey = GetKey(sortIndex);
141 if (currentKey == key) {
142 return sortIndex < propertiesNumber ? sortIndex : -1;
143 }
144 int midLeft = mid;
145 int midRight = mid;
146 while (midLeft - 1 >= 0) {
147 sortIndex = GetSortedIndex(--midLeft);
148 currentKey = GetKey(sortIndex);
149 if (currentKey.GetKeyHashCode() == keyHash) {
150 if (currentKey == key) {
151 return sortIndex < propertiesNumber ? sortIndex : -1;
152 }
153 } else {
154 break;
155 }
156 }
157 while (midRight + 1 < elements) {
158 sortIndex = GetSortedIndex(++midRight);
159 currentKey = GetKey(sortIndex);
160 if (currentKey.GetKeyHashCode() == keyHash) {
161 if (currentKey == key) {
162 return sortIndex < propertiesNumber ? sortIndex : -1;
163 }
164 } else {
165 break;
166 }
167 }
168 return -1;
169 }
170 }
171 return -1;
172 }
173 } // namespace panda::ecmascript
174 #endif // ECMASCRIPT_LAYOUT_INFO_INL_H
175