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/ts_types/ts_manager.h" 20 21 namespace panda::ecmascript::kungfu { 22 class ObjectAccessInfo final { 23 public: 24 explicit ObjectAccessInfo(GateType type, int hclassIndex = -1, PropertyLookupResult plr = PropertyLookupResult()) type_(type)25 : type_(type), hclassIndex_(hclassIndex), plr_(plr) {} 26 ~ObjectAccessInfo() = default; 27 Set(int hclassIndex,PropertyLookupResult plr)28 void Set(int hclassIndex, PropertyLookupResult plr) 29 { 30 hclassIndex_ = hclassIndex; 31 plr_ = plr; 32 } 33 Type()34 GateType Type() const 35 { 36 return type_; 37 } 38 HClassIndex()39 int HClassIndex() const 40 { 41 return hclassIndex_; 42 } 43 Plr()44 PropertyLookupResult Plr() const 45 { 46 return plr_; 47 } 48 49 private: 50 GateType type_ {GateType::AnyType()}; 51 int hclassIndex_ {-1}; 52 PropertyLookupResult plr_ {}; 53 }; 54 55 // An auxiliary class serving TSHCRLowering, used for named object property access, 56 // invoking TSManager and HClass. 57 class ObjectAccessHelper final { 58 public: 59 static constexpr size_t POLYMORPHIC_MAX_SIZE = 4; 60 61 enum AccessMode : uint8_t { 62 LOAD = 0, 63 STORE 64 }; 65 ObjectAccessHelper(TSManager * tsManager,AccessMode mode,GateRef receiver,GateType type,JSTaggedValue key,GateRef value)66 explicit ObjectAccessHelper(TSManager *tsManager, AccessMode mode, GateRef receiver, GateType type, 67 JSTaggedValue key, GateRef value) 68 : tsManager_(tsManager), 69 thread_(tsManager_->GetThread()), 70 mode_(mode), 71 receiver_(receiver), 72 type_(type), 73 key_(key), 74 value_(value) {} 75 76 ~ObjectAccessHelper() = default; 77 78 bool Compute(ChunkVector<ObjectAccessInfo> &infos); 79 GetAccessMode()80 AccessMode GetAccessMode() const 81 { 82 return mode_; 83 } 84 IsLoading()85 bool IsLoading() const 86 { 87 return mode_ == AccessMode::LOAD; 88 } 89 GetReceiver()90 GateRef GetReceiver() const 91 { 92 return receiver_; 93 } 94 GetValue()95 GateRef GetValue() const 96 { 97 return value_; 98 } 99 100 private: 101 bool ComputeForClassInstance(ObjectAccessInfo &info); 102 bool ComputeForClassOrObject(ObjectAccessInfo &info); 103 bool ComputePolymorphism(ChunkVector<ObjectAccessInfo> &infos); 104 105 TSManager *tsManager_ {nullptr}; 106 const JSThread *thread_ {nullptr}; 107 AccessMode mode_ {}; 108 GateRef receiver_ {Circuit::NullGate()}; 109 GateType type_ {GateType::AnyType()}; 110 JSTaggedValue key_ {JSTaggedValue::Hole()}; 111 GateRef value_ {Circuit::NullGate()}; 112 }; 113 } // panda::ecmascript::kungfu 114 #endif // ECMASCRIPT_COMPILER_OBJECT_ACCESS_HELPER_H 115