14514f5e3Sopenharmony_ci/* 24514f5e3Sopenharmony_ci * Copyright (c) 2021-2024 Huawei Device Co., Ltd. 34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License. 54514f5e3Sopenharmony_ci * You may obtain a copy of the License at 64514f5e3Sopenharmony_ci * 74514f5e3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 84514f5e3Sopenharmony_ci * 94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and 134514f5e3Sopenharmony_ci * limitations under the License. 144514f5e3Sopenharmony_ci */ 154514f5e3Sopenharmony_ci 164514f5e3Sopenharmony_ci#ifndef ECMASCRIPT_IC_PROPERTIES_CACHE_H 174514f5e3Sopenharmony_ci#define ECMASCRIPT_IC_PROPERTIES_CACHE_H 184514f5e3Sopenharmony_ci 194514f5e3Sopenharmony_ci#include <array> 204514f5e3Sopenharmony_ci 214514f5e3Sopenharmony_ci#include "ecmascript/js_hclass.h" 224514f5e3Sopenharmony_ci#include "ecmascript/js_tagged_value.h" 234514f5e3Sopenharmony_ci#include "ecmascript/js_tagged_value-inl.h" 244514f5e3Sopenharmony_ci#include "ecmascript/ecma_macros.h" 254514f5e3Sopenharmony_ci 264514f5e3Sopenharmony_cinamespace panda::ecmascript { 274514f5e3Sopenharmony_ciclass EcmaVM; 284514f5e3Sopenharmony_ciclass PropertiesCache { 294514f5e3Sopenharmony_cipublic: 304514f5e3Sopenharmony_ci inline int Get(JSHClass *jsHclass, JSTaggedValue key) 314514f5e3Sopenharmony_ci { 324514f5e3Sopenharmony_ci int hash = Hash(jsHclass, key); 334514f5e3Sopenharmony_ci PropertyKey &prop = keys_[hash]; 344514f5e3Sopenharmony_ci if ((prop.hclass_ == jsHclass) && (prop.key_ == key)) { 354514f5e3Sopenharmony_ci return keys_[hash].results_; 364514f5e3Sopenharmony_ci } 374514f5e3Sopenharmony_ci return NOT_FOUND; 384514f5e3Sopenharmony_ci } 394514f5e3Sopenharmony_ci inline void Set(JSHClass *jsHclass, JSTaggedValue key, int index) 404514f5e3Sopenharmony_ci { 414514f5e3Sopenharmony_ci int hash = Hash(jsHclass, key); 424514f5e3Sopenharmony_ci PropertyKey &prop = keys_[hash]; 434514f5e3Sopenharmony_ci prop.hclass_ = jsHclass; 444514f5e3Sopenharmony_ci prop.key_ = key; 454514f5e3Sopenharmony_ci keys_[hash].results_ = index; 464514f5e3Sopenharmony_ci } 474514f5e3Sopenharmony_ci inline void Clear() 484514f5e3Sopenharmony_ci { 494514f5e3Sopenharmony_ci for (auto &key : keys_) { 504514f5e3Sopenharmony_ci key.hclass_ = nullptr; 514514f5e3Sopenharmony_ci key.key_ = JSTaggedValue::Hole(); 524514f5e3Sopenharmony_ci } 534514f5e3Sopenharmony_ci } 544514f5e3Sopenharmony_ci inline bool IsCleared() const 554514f5e3Sopenharmony_ci { 564514f5e3Sopenharmony_ci for (auto &key : keys_) { 574514f5e3Sopenharmony_ci if (key.hclass_ != nullptr) { 584514f5e3Sopenharmony_ci return false; 594514f5e3Sopenharmony_ci } 604514f5e3Sopenharmony_ci } 614514f5e3Sopenharmony_ci return true; 624514f5e3Sopenharmony_ci } 634514f5e3Sopenharmony_ci static const int NOT_FOUND = -1; 644514f5e3Sopenharmony_ci static const uint32_t CACHE_LENGTH_BIT = 10; 654514f5e3Sopenharmony_ci static const uint32_t CACHE_LENGTH = (1U << CACHE_LENGTH_BIT); 664514f5e3Sopenharmony_ci static const uint32_t CACHE_LENGTH_MASK = CACHE_LENGTH - 1; 674514f5e3Sopenharmony_ci 684514f5e3Sopenharmony_ci struct PropertyKey : public base::AlignedStruct<JSTaggedValue::TaggedTypeSize(), 694514f5e3Sopenharmony_ci base::AlignedPointer, 704514f5e3Sopenharmony_ci JSTaggedValue, 714514f5e3Sopenharmony_ci base::AlignedUint32> { 724514f5e3Sopenharmony_ci enum class Index : size_t { 734514f5e3Sopenharmony_ci HclassIndex = 0, 744514f5e3Sopenharmony_ci KeyIndex, 754514f5e3Sopenharmony_ci ResultsIndex, 764514f5e3Sopenharmony_ci NumOfMembers 774514f5e3Sopenharmony_ci }; 784514f5e3Sopenharmony_ci static size_t GetHclassOffset(bool isArch32 = false) 794514f5e3Sopenharmony_ci { 804514f5e3Sopenharmony_ci return GetOffset<static_cast<size_t>(Index::HclassIndex)>(isArch32); 814514f5e3Sopenharmony_ci } 824514f5e3Sopenharmony_ci 834514f5e3Sopenharmony_ci static size_t GetKeyOffset(bool isArch32 = false) 844514f5e3Sopenharmony_ci { 854514f5e3Sopenharmony_ci return GetOffset<static_cast<size_t>(Index::KeyIndex)>(isArch32); 864514f5e3Sopenharmony_ci } 874514f5e3Sopenharmony_ci 884514f5e3Sopenharmony_ci static size_t GetResultsOffset(bool isArch32 = false) 894514f5e3Sopenharmony_ci { 904514f5e3Sopenharmony_ci return GetOffset<static_cast<size_t>(Index::ResultsIndex)>(isArch32); 914514f5e3Sopenharmony_ci } 924514f5e3Sopenharmony_ci 934514f5e3Sopenharmony_ci static size_t GetPropertyKeySize() 944514f5e3Sopenharmony_ci { 954514f5e3Sopenharmony_ci return static_cast<size_t>(Index::NumOfMembers) * static_cast<size_t>(JSTaggedValue::TaggedTypeSize()); 964514f5e3Sopenharmony_ci } 974514f5e3Sopenharmony_ci 984514f5e3Sopenharmony_ci static_assert(static_cast<size_t>(Index::NumOfMembers) == NumOfTypes); 994514f5e3Sopenharmony_ci alignas(EAS) JSHClass *hclass_ {nullptr}; 1004514f5e3Sopenharmony_ci alignas(EAS) JSTaggedValue key_ {JSTaggedValue::Hole()}; 1014514f5e3Sopenharmony_ci alignas(EAS) int results_ {NOT_FOUND}; 1024514f5e3Sopenharmony_ci }; 1034514f5e3Sopenharmony_ci 1044514f5e3Sopenharmony_ciprivate: 1054514f5e3Sopenharmony_ci PropertiesCache() 1064514f5e3Sopenharmony_ci { 1074514f5e3Sopenharmony_ci for (uint32_t i = 0; i < CACHE_LENGTH; ++i) { 1084514f5e3Sopenharmony_ci keys_[i].hclass_ = nullptr; 1094514f5e3Sopenharmony_ci keys_[i].key_ = JSTaggedValue::Hole(); 1104514f5e3Sopenharmony_ci keys_[i].results_ = NOT_FOUND; 1114514f5e3Sopenharmony_ci } 1124514f5e3Sopenharmony_ci } 1134514f5e3Sopenharmony_ci ~PropertiesCache() = default; 1144514f5e3Sopenharmony_ci 1154514f5e3Sopenharmony_ci // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init) 1164514f5e3Sopenharmony_ci 1174514f5e3Sopenharmony_ci static inline int Hash(JSHClass *cls, JSTaggedValue key) 1184514f5e3Sopenharmony_ci { 1194514f5e3Sopenharmony_ci uint32_t clsHash = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(cls)) >> 3U; // skip 8bytes 1204514f5e3Sopenharmony_ci uint32_t keyHash = key.GetKeyHashCode(); 1214514f5e3Sopenharmony_ci return static_cast<int>((clsHash ^ keyHash) & CACHE_LENGTH_MASK); 1224514f5e3Sopenharmony_ci } 1234514f5e3Sopenharmony_ci 1244514f5e3Sopenharmony_ci std::array<PropertyKey, CACHE_LENGTH> keys_{}; 1254514f5e3Sopenharmony_ci 1264514f5e3Sopenharmony_ci friend class EcmaContext; 1274514f5e3Sopenharmony_ci}; 1284514f5e3Sopenharmony_ci} // namespace panda::ecmascript 1294514f5e3Sopenharmony_ci#endif // ECMASCRIPT_IC_PROPERTIES_CACHE_H 130