1 /* 2 * Copyright (c) 2023 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_GLOBAL_INDEX_MAP_H 17 #define ECMASCRIPT_GLOBAL_INDEX_MAP_H 18 19 #include "ecmascript/tagged_dictionary.h" 20 #include "utils/bit_field.h" 21 22 namespace panda::ecmascript { 23 class GlobalIndex { 24 public: 25 static constexpr uint32_t GLOBAL_CONST_BITFILED_NUM = 10; 26 static constexpr uint32_t GLOBAL_ENV_BITFILED_NUM = 10; 27 static constexpr uint32_t BUILTIN_ENTRIES_BITFILED_NUM = 10; 28 using GlobalConstBits = BitField<size_t, 0, GLOBAL_CONST_BITFILED_NUM>; 29 using GlobalEnvBits = GlobalConstBits::NextField<size_t, GLOBAL_ENV_BITFILED_NUM>; 30 using BuiltinEntriesBits = GlobalEnvBits::NextField<size_t, BUILTIN_ENTRIES_BITFILED_NUM>; 31 32 GlobalIndex() = default; GlobalIndex(uint32_t index)33 explicit GlobalIndex(uint32_t index) : index_(index) {} GlobalIndex(int32_t index)34 explicit GlobalIndex(int32_t index) : index_(static_cast<uint32_t>(index)) {} 35 GetGlobalConstId()36 int GetGlobalConstId() const 37 { 38 int id = GlobalConstBits::Decode(index_); 39 return id - 1; 40 } 41 UpdateGlobalConstId(size_t id)42 void UpdateGlobalConstId(size_t id) 43 { 44 index_ = GlobalConstBits::Update(index_, id + 1); 45 } 46 IsGlobalConstId()47 bool IsGlobalConstId() const 48 { 49 return GlobalConstBits::Decode(index_); 50 } 51 GetGlobalEnvId()52 int GetGlobalEnvId() const 53 { 54 int id = GlobalEnvBits::Decode(index_); 55 return id - 1; 56 } 57 UpdateGlobalEnvId(size_t id)58 void UpdateGlobalEnvId(size_t id) 59 { 60 index_ = GlobalEnvBits::Update(index_, id + 1); 61 } 62 IsGlobalEnvId()63 bool IsGlobalEnvId() const 64 { 65 return GlobalEnvBits::Decode(index_); 66 } 67 GetBuiltinEntriesId()68 int GetBuiltinEntriesId() const 69 { 70 int id = BuiltinEntriesBits::Decode(index_); 71 return id - 1; 72 } 73 UpdateBuiltinEntriesId(size_t id)74 void UpdateBuiltinEntriesId(size_t id) 75 { 76 index_ = BuiltinEntriesBits::Update(index_, id + 1); 77 } 78 IsBuiltinEntriesId()79 bool IsBuiltinEntriesId() const 80 { 81 return BuiltinEntriesBits::Decode(index_); 82 } 83 GetGlobalIndex()84 uint32_t GetGlobalIndex() const 85 { 86 return index_; 87 } 88 UpdateGlobalIndex(size_t index)89 void UpdateGlobalIndex(size_t index) 90 { 91 index_ = index; 92 } 93 94 bool operator!=(const GlobalIndex &right) const 95 { 96 return index_ != right.index_; 97 } 98 99 bool operator==(const GlobalIndex &right) const 100 { 101 return index_ == right.index_; 102 } 103 104 private: 105 uint32_t index_ {0}; 106 }; 107 108 // HeapObject's Pointer To IndexHashmap 109 class GlobalIndexMap { 110 public: 111 static void Initialize(const JSThread *thread, 112 JSMutableHandle<PointerToIndexDictionary> globalIndexMap); 113 static void InitGlobalIndexMap(const JSThread *thread, 114 JSMutableHandle<PointerToIndexDictionary> globalIndexMap); 115 static void InitGlobalConst(const JSThread *thread, 116 JSMutableHandle<PointerToIndexDictionary> globalIndexMap); 117 static void InitGlobalEnv(const JSThread *thread, 118 JSMutableHandle<PointerToIndexDictionary> globalIndexMap); 119 static void InitBuiltinEntries(const JSThread *thread, 120 JSMutableHandle<PointerToIndexDictionary> globalIndexMap); 121 static void FindGlobalIndex(JSHandle<PointerToIndexDictionary> globalIndexMap, 122 JSTaggedValue obj, GlobalIndex *globalIndex); GetGlobalIndexMap(EcmaContext * context)123 static inline JSHandle<PointerToIndexDictionary> GetGlobalIndexMap(EcmaContext *context) 124 { 125 return JSHandle<PointerToIndexDictionary>(reinterpret_cast<uintptr_t>(&context->pointerToIndexDictionary_)); 126 } 127 }; 128 } // namespace panda::ecmascript 129 130 #endif // ECMASCRIPT_GLOBAL_INDEX_MAP_H