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_COMPILER_OBJECT_ACCESS_HELPER_H 17 #define ECMASCRIPT_COMPILER_OBJECT_ACCESS_HELPER_H 18 19 #include "ecmascript/compiler/pgo_type/pgo_type_location.h" 20 #include "ecmascript/compiler/share_gate_meta_data.h" 21 #include "ecmascript/ts_types/ts_manager.h" 22 23 namespace panda::ecmascript::kungfu { 24 class ObjectAccessInfo final { 25 public: 26 explicit ObjectAccessInfo(GateType type, int hclassIndex = -1, PropertyLookupResult plr = PropertyLookupResult()) type_(type)27 : type_(type), hclassIndex_(hclassIndex), plr_(plr) {} 28 ~ObjectAccessInfo() = default; 29 Set(int hclassIndex,PropertyLookupResult plr)30 void Set(int hclassIndex, PropertyLookupResult plr) 31 { 32 hclassIndex_ = hclassIndex; 33 plr_ = plr; 34 } 35 Type()36 GateType Type() const 37 { 38 return type_; 39 } 40 HClassIndex()41 int HClassIndex() const 42 { 43 return hclassIndex_; 44 } 45 Plr()46 PropertyLookupResult Plr() const 47 { 48 return plr_; 49 } 50 51 private: 52 GateType type_ {GateType::AnyType()}; 53 int hclassIndex_ {-1}; 54 PropertyLookupResult plr_ {}; 55 }; 56 57 class PGOObjectAccessInfo final { 58 public: 59 explicit PGOObjectAccessInfo( 60 ProfileTyper type, int hclassIndex = -1, PropertyLookupResult plr = PropertyLookupResult()) type_(type)61 : type_(type), hclassIndex_(hclassIndex), plr_(plr) {} 62 ~PGOObjectAccessInfo() = default; 63 Set(int hclassIndex,PropertyLookupResult plr)64 void Set(int hclassIndex, PropertyLookupResult plr) 65 { 66 hclassIndex_ = hclassIndex; 67 plr_ = plr; 68 } 69 Type()70 ProfileTyper Type() const 71 { 72 return type_; 73 } 74 HClassIndex()75 int HClassIndex() const 76 { 77 return hclassIndex_; 78 } 79 Plr()80 PropertyLookupResult Plr() const 81 { 82 return plr_; 83 } 84 85 private: 86 ProfileTyper type_ {ProfileTyper()}; 87 int hclassIndex_ {-1}; 88 PropertyLookupResult plr_ {}; 89 }; 90 91 // An auxiliary class serving TypedBytecodeLowering, used for named object property access, 92 // invoking TSManager and HClass. 93 class ObjectAccessHelper final { 94 public: 95 static constexpr size_t POLYMORPHIC_MAX_SIZE = 4; 96 97 enum AccessMode : uint8_t { 98 LOAD = 0, 99 STORE 100 }; 101 ObjectAccessHelper(TSManager * tsManager,AccessMode mode,GateRef receiver,GateType type,JSTaggedValue key,GateRef value)102 explicit ObjectAccessHelper(TSManager *tsManager, AccessMode mode, GateRef receiver, GateType type, 103 JSTaggedValue key, GateRef value) 104 : tsManager_(tsManager), 105 thread_(tsManager_->GetThread()), 106 mode_(mode), 107 receiver_(receiver), 108 type_(type), 109 key_(key), 110 value_(value) {} 111 112 ~ObjectAccessHelper() = default; 113 114 bool Compute(ChunkVector<ObjectAccessInfo> &infos); 115 GetAccessMode()116 AccessMode GetAccessMode() const 117 { 118 return mode_; 119 } 120 IsLoading()121 bool IsLoading() const 122 { 123 return mode_ == AccessMode::LOAD; 124 } 125 GetReceiver()126 GateRef GetReceiver() const 127 { 128 return receiver_; 129 } 130 GetValue()131 GateRef GetValue() const 132 { 133 return value_; 134 } 135 136 private: 137 bool ComputeForClassInstance(ObjectAccessInfo &info); 138 bool ComputeForClassOrObject(ObjectAccessInfo &info); 139 bool ComputePolymorphism(ChunkVector<ObjectAccessInfo> &infos); 140 141 TSManager *tsManager_ {nullptr}; 142 const JSThread *thread_ {nullptr}; 143 AccessMode mode_ {}; 144 GateRef receiver_ {Circuit::NullGate()}; 145 GateType type_ {GateType::AnyType()}; 146 JSTaggedValue key_ {JSTaggedValue::Hole()}; 147 GateRef value_ {Circuit::NullGate()}; 148 }; 149 150 class PGOObjectAccessHelper final { 151 public: 152 static constexpr size_t POLYMORPHIC_MAX_SIZE = 4; 153 154 enum AccessMode : uint8_t { 155 LOAD = 0, 156 STORE 157 }; 158 PGOObjectAccessHelper(TSManager * tsManager,AccessMode mode,GateRef receiver,ProfileTyper type,JSTaggedValue key,GateRef value)159 explicit PGOObjectAccessHelper(TSManager *tsManager, AccessMode mode, GateRef receiver, ProfileTyper type, 160 JSTaggedValue key, GateRef value) 161 : tsManager_(tsManager), 162 thread_(tsManager_->GetThread()), 163 mode_(mode), 164 receiver_(receiver), 165 holder_(receiver), 166 type_(type), 167 key_(key), 168 value_(value) {} 169 170 ~PGOObjectAccessHelper() = default; 171 GetAccessMode()172 AccessMode GetAccessMode() const 173 { 174 return mode_; 175 } 176 IsLoading()177 bool IsLoading() const 178 { 179 return mode_ == AccessMode::LOAD; 180 } 181 GetReceiver()182 GateRef GetReceiver() const 183 { 184 return receiver_; 185 } 186 GetHolder()187 GateRef GetHolder() const 188 { 189 return holder_; 190 } 191 SetHolder(GateRef holder)192 void SetHolder(GateRef holder) 193 { 194 holder_ = holder; 195 } 196 GetValue()197 GateRef GetValue() const 198 { 199 return value_; 200 } 201 bool ComputeForClassInstance(PGOObjectAccessInfo &info); 202 bool ClassInstanceIsCallable(PGOObjectAccessInfo &info); 203 204 private: 205 206 TSManager *tsManager_ {nullptr}; 207 const JSThread *thread_ {nullptr}; 208 AccessMode mode_ {}; 209 GateRef receiver_ {Circuit::NullGate()}; 210 GateRef holder_ {Circuit::NullGate()}; 211 ProfileTyper type_ {ProfileTyper()}; 212 JSTaggedValue key_ {JSTaggedValue::Hole()}; 213 GateRef value_ {Circuit::NullGate()}; 214 }; 215 } // panda::ecmascript::kungfu 216 #endif // ECMASCRIPT_COMPILER_OBJECT_ACCESS_HELPER_H 217