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_MEM_SLOTS_H 17 #define ECMASCRIPT_MEM_SLOTS_H 18 19 #include "ecmascript/js_tagged_value.h" 20 #include "ecmascript/mem/mem.h" 21 22 namespace panda::ecmascript { 23 enum class SlotStatus : bool { 24 KEEP_SLOT, 25 CLEAR_SLOT, 26 }; 27 28 class ObjectSlot { 29 public: ObjectSlot(uintptr_t slotAddr)30 explicit ObjectSlot(uintptr_t slotAddr) : slotAddress_(slotAddr) {} 31 ~ObjectSlot() = default; 32 33 DEFAULT_COPY_SEMANTIC(ObjectSlot); 34 DEFAULT_MOVE_SEMANTIC(ObjectSlot); 35 Update(TaggedObject * header)36 void Update(TaggedObject *header) 37 { 38 Update(static_cast<JSTaggedType>(ToUintPtr(header))); 39 } 40 GetRefFieldAddr()41 uintptr_t* GetRefFieldAddr() 42 { 43 return reinterpret_cast<uintptr_t*>(slotAddress_); 44 } 45 Update(JSTaggedType value)46 void Update(JSTaggedType value) 47 { 48 // NOLINTNEXTLINE(clang-analyzer-core.NullDereference) 49 *reinterpret_cast<JSTaggedType *>(slotAddress_) = value; 50 } 51 UpdateWeak(uintptr_t value)52 void UpdateWeak(uintptr_t value) 53 { 54 // NOLINTNEXTLINE(clang-analyzer-core.NullDereference) 55 *reinterpret_cast<JSTaggedType *>(slotAddress_) = value | JSTaggedValue::TAG_WEAK; 56 } 57 Clear()58 void Clear() 59 { 60 *reinterpret_cast<JSTaggedType *>(slotAddress_) = JSTaggedValue::VALUE_UNDEFINED; 61 } 62 GetTaggedObject()63 TaggedObject *GetTaggedObject() const 64 { 65 return reinterpret_cast<TaggedObject *>(GetTaggedType()); 66 } 67 GetTaggedType()68 JSTaggedType GetTaggedType() const 69 { 70 // NOLINTNEXTLINE(clang-analyzer-core.NullDereference) 71 return *reinterpret_cast<JSTaggedType *>(slotAddress_); 72 } 73 GetTaggedValue()74 JSTaggedValue GetTaggedValue() const 75 { 76 // NOLINTNEXTLINE(clang-analyzer-core.NullDereference) 77 return *reinterpret_cast<JSTaggedValue *>(slotAddress_); 78 } 79 80 ObjectSlot &operator++() 81 { 82 slotAddress_ += sizeof(JSTaggedType); 83 return *this; 84 } 85 86 // NOLINTNEXTLINE(cert-dcl21-cpp) 87 ObjectSlot operator++(int) 88 { 89 ObjectSlot ret = *this; 90 slotAddress_ += sizeof(JSTaggedType); 91 return ret; 92 } 93 94 ObjectSlot operator+=(size_t length) 95 { 96 ObjectSlot ret = *this; 97 slotAddress_ += sizeof(JSTaggedType) * length; 98 return ret; 99 } 100 SlotAddress()101 uintptr_t SlotAddress() const 102 { 103 return slotAddress_; 104 } 105 106 bool operator<(const ObjectSlot &other) const 107 { 108 return slotAddress_ < other.slotAddress_; 109 } 110 bool operator<=(const ObjectSlot &other) const 111 { 112 return slotAddress_ <= other.slotAddress_; 113 } 114 bool operator>(const ObjectSlot &other) const 115 { 116 return slotAddress_ > other.slotAddress_; 117 } 118 bool operator>=(const ObjectSlot &other) const 119 { 120 return slotAddress_ >= other.slotAddress_; 121 } 122 bool operator==(const ObjectSlot &other) const 123 { 124 return slotAddress_ == other.slotAddress_; 125 } 126 bool operator!=(const ObjectSlot &other) const 127 { 128 return slotAddress_ != other.slotAddress_; 129 } 130 131 private: 132 uintptr_t slotAddress_; 133 }; 134 } // namespace panda::ecmascript 135 136 #endif // ECMASCRIPT_MEM_SLOTS_H 137