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_JSSYMBOL_H 174514f5e3Sopenharmony_ci#define ECMASCRIPT_JSSYMBOL_H 184514f5e3Sopenharmony_ci 194514f5e3Sopenharmony_ci#include "ecmascript/ecma_string.h" 204514f5e3Sopenharmony_ci#include "ecmascript/js_object.h" 214514f5e3Sopenharmony_ci#include "ecmascript/pgo_profiler/types/pgo_profile_type.h" 224514f5e3Sopenharmony_ci 234514f5e3Sopenharmony_cinamespace panda { 244514f5e3Sopenharmony_cinamespace ecmascript { 254514f5e3Sopenharmony_ciusing ProfileType = pgo::ProfileType; 264514f5e3Sopenharmony_ci 274514f5e3Sopenharmony_ciclass JSSymbol : public TaggedObject { 284514f5e3Sopenharmony_cipublic: 294514f5e3Sopenharmony_ci static constexpr uint32_t IS_PRIVATE = 1U << 0U; 304514f5e3Sopenharmony_ci static constexpr uint32_t IS_WELL_KNOWN_SYMBOL = 1U << 1U; 314514f5e3Sopenharmony_ci static constexpr uint32_t IS_IN_PUBLIC_SYMBOL_TABLE = 1U << 2U; 324514f5e3Sopenharmony_ci static constexpr uint32_t IS_INTERESTING_SYMBOL = 1U << 3U; 334514f5e3Sopenharmony_ci static constexpr uint32_t IS_PRIVATE_NAME = 1U << 4U; 344514f5e3Sopenharmony_ci static constexpr uint32_t IS_PRIVATE_BRAND = 1U << 5U; 354514f5e3Sopenharmony_ci static constexpr uint32_t HAS_ID = 1U << 6U; 364514f5e3Sopenharmony_ci 374514f5e3Sopenharmony_ci static constexpr int SYMBOL_HAS_INSTANCE_TYPE = 0; 384514f5e3Sopenharmony_ci static constexpr int SYMBOL_TO_PRIMITIVE_TYPE = 1; 394514f5e3Sopenharmony_ci static constexpr int SYMBOL_DEFAULT_TYPE = 2; 404514f5e3Sopenharmony_ci 414514f5e3Sopenharmony_ci static constexpr const uint32_t LINEAR_X = 1103515245U; 424514f5e3Sopenharmony_ci static constexpr const uint32_t LINEAR_Y = 12345U; 434514f5e3Sopenharmony_ci static constexpr const uint32_t LINEAR_SEED = 987654321U; 444514f5e3Sopenharmony_ci 454514f5e3Sopenharmony_ci // 48: high 16 bits need to be double encoded as a valid number tagged value 464514f5e3Sopenharmony_ci static constexpr size_t SYMBOL_ID_BITFIELD_NUM = 48; 474514f5e3Sopenharmony_ci static constexpr size_t ABC_ID_OFFSET_BIT = SYMBOL_ID_BITFIELD_NUM - ProfileType::ABC_ID_BITFIELD_NUM; 484514f5e3Sopenharmony_ci static constexpr size_t LITERAL_ID_BITFIELD_NUM = 16; 494514f5e3Sopenharmony_ci static constexpr size_t LITERAL_ID_OFFSET_BIT = ABC_ID_OFFSET_BIT - LITERAL_ID_BITFIELD_NUM; 504514f5e3Sopenharmony_ci 514514f5e3Sopenharmony_cipublic: 524514f5e3Sopenharmony_ci CAST_CHECK(JSSymbol, IsSymbol); 534514f5e3Sopenharmony_ci 544514f5e3Sopenharmony_ci static inline uint32_t ComputeHash() 554514f5e3Sopenharmony_ci { 564514f5e3Sopenharmony_ci uint32_t hashSeed = static_cast<uint32_t>(LINEAR_SEED + std::time(nullptr)); 574514f5e3Sopenharmony_ci uint32_t hash = hashSeed * LINEAR_X + LINEAR_Y; 584514f5e3Sopenharmony_ci return hash; 594514f5e3Sopenharmony_ci } 604514f5e3Sopenharmony_ci 614514f5e3Sopenharmony_ci bool HasId() const 624514f5e3Sopenharmony_ci { 634514f5e3Sopenharmony_ci return (GetFlags() & HAS_ID) != 0U; 644514f5e3Sopenharmony_ci } 654514f5e3Sopenharmony_ci 664514f5e3Sopenharmony_ci void SetHasId() 674514f5e3Sopenharmony_ci { 684514f5e3Sopenharmony_ci SetFlags(GetFlags() | HAS_ID); 694514f5e3Sopenharmony_ci } 704514f5e3Sopenharmony_ci 714514f5e3Sopenharmony_ci bool IsPrivate() const 724514f5e3Sopenharmony_ci { 734514f5e3Sopenharmony_ci return (GetFlags() & IS_PRIVATE) != 0U; 744514f5e3Sopenharmony_ci } 754514f5e3Sopenharmony_ci 764514f5e3Sopenharmony_ci void SetPrivate() 774514f5e3Sopenharmony_ci { 784514f5e3Sopenharmony_ci SetFlags(GetFlags() | IS_PRIVATE); 794514f5e3Sopenharmony_ci } 804514f5e3Sopenharmony_ci 814514f5e3Sopenharmony_ci bool IsWellKnownSymbol() const 824514f5e3Sopenharmony_ci { 834514f5e3Sopenharmony_ci return (GetFlags() & IS_WELL_KNOWN_SYMBOL) != 0U; 844514f5e3Sopenharmony_ci } 854514f5e3Sopenharmony_ci 864514f5e3Sopenharmony_ci void SetWellKnownSymbol() 874514f5e3Sopenharmony_ci { 884514f5e3Sopenharmony_ci SetFlags(GetFlags() | IS_WELL_KNOWN_SYMBOL); 894514f5e3Sopenharmony_ci } 904514f5e3Sopenharmony_ci 914514f5e3Sopenharmony_ci bool IsInPublicSymbolTable() const 924514f5e3Sopenharmony_ci { 934514f5e3Sopenharmony_ci return (GetFlags() & IS_IN_PUBLIC_SYMBOL_TABLE) != 0U; 944514f5e3Sopenharmony_ci } 954514f5e3Sopenharmony_ci 964514f5e3Sopenharmony_ci void SetInPublicSymbolTable() 974514f5e3Sopenharmony_ci { 984514f5e3Sopenharmony_ci SetFlags(GetFlags() | IS_IN_PUBLIC_SYMBOL_TABLE); 994514f5e3Sopenharmony_ci } 1004514f5e3Sopenharmony_ci 1014514f5e3Sopenharmony_ci bool IsInterestingSymbol() const 1024514f5e3Sopenharmony_ci { 1034514f5e3Sopenharmony_ci return (GetFlags() & IS_INTERESTING_SYMBOL) != 0U; 1044514f5e3Sopenharmony_ci } 1054514f5e3Sopenharmony_ci 1064514f5e3Sopenharmony_ci void SetInterestingSymbol() 1074514f5e3Sopenharmony_ci { 1084514f5e3Sopenharmony_ci SetFlags(GetFlags() | IS_INTERESTING_SYMBOL); 1094514f5e3Sopenharmony_ci } 1104514f5e3Sopenharmony_ci 1114514f5e3Sopenharmony_ci bool IsPrivateNameSymbol() const 1124514f5e3Sopenharmony_ci { 1134514f5e3Sopenharmony_ci return (GetFlags() & IS_PRIVATE_NAME) != 0U; 1144514f5e3Sopenharmony_ci } 1154514f5e3Sopenharmony_ci 1164514f5e3Sopenharmony_ci void SetPrivateNameSymbol() 1174514f5e3Sopenharmony_ci { 1184514f5e3Sopenharmony_ci SetFlags(GetFlags() | IS_PRIVATE_NAME); 1194514f5e3Sopenharmony_ci } 1204514f5e3Sopenharmony_ci 1214514f5e3Sopenharmony_ci static bool Equal(const JSSymbol &src, const JSSymbol &dst) 1224514f5e3Sopenharmony_ci { 1234514f5e3Sopenharmony_ci if (src.GetFlags() != dst.GetFlags()) { 1244514f5e3Sopenharmony_ci return false; 1254514f5e3Sopenharmony_ci } 1264514f5e3Sopenharmony_ci EcmaString *srcString = EcmaString::Cast(src.GetDescription().GetTaggedObject()); 1274514f5e3Sopenharmony_ci EcmaString *dstString = EcmaString::Cast(dst.GetDescription().GetTaggedObject()); 1284514f5e3Sopenharmony_ci return EcmaStringAccessor::StringsAreEqual(srcString, dstString); 1294514f5e3Sopenharmony_ci } 1304514f5e3Sopenharmony_ci 1314514f5e3Sopenharmony_ci uint64_t GetPrivateId() 1324514f5e3Sopenharmony_ci { 1334514f5e3Sopenharmony_ci return GetId(); 1344514f5e3Sopenharmony_ci } 1354514f5e3Sopenharmony_ci 1364514f5e3Sopenharmony_ci void SetPrivateId(uint64_t id) 1374514f5e3Sopenharmony_ci { 1384514f5e3Sopenharmony_ci SetHasId(); 1394514f5e3Sopenharmony_ci SetId(id); 1404514f5e3Sopenharmony_ci } 1414514f5e3Sopenharmony_ci 1424514f5e3Sopenharmony_ci static uint64_t GeneratePrivateId(uint64_t abcId, uint64_t literalId, uint64_t slotIndex) 1434514f5e3Sopenharmony_ci { 1444514f5e3Sopenharmony_ci return JSTaggedValueInternals::DOUBLE_ENCODE_OFFSET | (abcId << ABC_ID_OFFSET_BIT) | 1454514f5e3Sopenharmony_ci (literalId << LITERAL_ID_OFFSET_BIT) | slotIndex; 1464514f5e3Sopenharmony_ci } 1474514f5e3Sopenharmony_ci 1484514f5e3Sopenharmony_ci static uint64_t GetSlotIndex(uint64_t id) 1494514f5e3Sopenharmony_ci { 1504514f5e3Sopenharmony_ci uint64_t mask = (1ULL << LITERAL_ID_OFFSET_BIT) - 1; 1514514f5e3Sopenharmony_ci return id & mask; 1524514f5e3Sopenharmony_ci } 1534514f5e3Sopenharmony_ci 1544514f5e3Sopenharmony_ci static uint64_t GetLiteralId(uint64_t id) 1554514f5e3Sopenharmony_ci { 1564514f5e3Sopenharmony_ci uint64_t mask = (1ULL << LITERAL_ID_BITFIELD_NUM) - 1; 1574514f5e3Sopenharmony_ci return (id >> LITERAL_ID_OFFSET_BIT) & mask; 1584514f5e3Sopenharmony_ci } 1594514f5e3Sopenharmony_ci 1604514f5e3Sopenharmony_ci static uint64_t GetAbcId(uint64_t id) 1614514f5e3Sopenharmony_ci { 1624514f5e3Sopenharmony_ci uint64_t mask = (1ULL << ProfileType::ABC_ID_BITFIELD_NUM) - 1; 1634514f5e3Sopenharmony_ci return (id >> ABC_ID_OFFSET_BIT) & mask; 1644514f5e3Sopenharmony_ci } 1654514f5e3Sopenharmony_ci 1664514f5e3Sopenharmony_cipublic: 1674514f5e3Sopenharmony_ci static constexpr size_t DESCRIPTION_OFFSET = TaggedObjectSize(); 1684514f5e3Sopenharmony_ci ACCESSORS(Description, DESCRIPTION_OFFSET, HASHFIELD_OFFSET) 1694514f5e3Sopenharmony_ci ACCESSORS_PRIMITIVE_FIELD(HashField, uint32_t, HASHFIELD_OFFSET, FLAGS_OFFSET) 1704514f5e3Sopenharmony_ci ACCESSORS_PRIMITIVE_FIELD(Flags, uint32_t, FLAGS_OFFSET, ID_OFFSET) 1714514f5e3Sopenharmony_ci ACCESSORS_PRIMITIVE_FIELD(Id, uint64_t, ID_OFFSET, LAST_OFFSET) 1724514f5e3Sopenharmony_ci DEFINE_ALIGN_SIZE(LAST_OFFSET); 1734514f5e3Sopenharmony_ci 1744514f5e3Sopenharmony_ci DECL_DUMP() 1754514f5e3Sopenharmony_ci 1764514f5e3Sopenharmony_ci DECL_VISIT_OBJECT(DESCRIPTION_OFFSET, HASHFIELD_OFFSET) 1774514f5e3Sopenharmony_ci}; 1784514f5e3Sopenharmony_ci} // namespace ecmascript 1794514f5e3Sopenharmony_ci} // namespace panda 1804514f5e3Sopenharmony_ci#endif // ECMASCRIPT_NAME_H 181