1 /* 2 * Copyright (c) 2021-2024 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_IC_IC_RUNTIME_H 17 #define ECMASCRIPT_IC_IC_RUNTIME_H 18 19 #include "ecmascript/ic/profile_type_info.h" 20 #include "ecmascript/accessor_data.h" 21 #include "ecmascript/ecma_vm.h" 22 #include "ecmascript/js_tagged_value.h" 23 #include "ecmascript/js_handle.h" 24 #include "ecmascript/object_operator.h" 25 26 namespace panda::ecmascript { 27 class ProfileTypeInfo; 28 class JSThread; 29 class ObjectOperator; 30 31 class ICRuntime { 32 public: ICRuntime(JSThread * thread,JSHandle<ProfileTypeInfo> profileTypeInfo,uint32_t slotId,ICKind kind)33 ICRuntime(JSThread *thread, JSHandle<ProfileTypeInfo> profileTypeInfo, uint32_t slotId, ICKind kind) 34 : thread_(thread), icAccessor_(thread, profileTypeInfo, slotId, kind) 35 { 36 } 37 38 ~ICRuntime() = default; 39 40 bool GetHandler(const ObjectOperator &op, const JSHandle<JSHClass> &hclass, 41 JSHandle<JSTaggedValue> &handlerValue); 42 void UpdateLoadHandler(const ObjectOperator &op, JSHandle<JSTaggedValue> key, JSHandle<JSTaggedValue> receiver); 43 void UpdateLoadStringHandler(JSHandle<JSTaggedValue> receiver); 44 void UpdateTypedArrayHandler(JSHandle<JSTaggedValue> receiver); 45 void UpdateStoreHandler(const ObjectOperator &op, JSHandle<JSTaggedValue> key, JSHandle<JSTaggedValue> receiver); 46 IsMegaIC()47 bool IsMegaIC() 48 { 49 return icAccessor_.GetICState() == ProfileTypeAccessor::ICState::IC_MEGA; 50 } 51 GetThread()52 JSThread *GetThread() const 53 { 54 return thread_; 55 } 56 UpdateReceiverHClass(JSHandle<JSTaggedValue> receiverHClass)57 void UpdateReceiverHClass(JSHandle<JSTaggedValue> receiverHClass) 58 { 59 receiverHClass_ = receiverHClass; 60 } 61 GetICKind()62 ICKind GetICKind() const 63 { 64 return icAccessor_.GetKind(); 65 } 66 GetSlotId()67 uint32_t GetSlotId() const 68 { 69 return icAccessor_.GetSlotId(); 70 } 71 72 void TraceIC(JSThread *thread, JSHandle<JSTaggedValue> receiver, JSHandle<JSTaggedValue> key) const; 73 74 protected: 75 JSThread *thread_; 76 JSHandle<JSTaggedValue> receiverHClass_{}; 77 ProfileTypeAccessor icAccessor_; 78 }; 79 80 class LoadICRuntime : public ICRuntime { 81 public: LoadICRuntime(JSThread * thread,JSHandle<ProfileTypeInfo> profileTypeInfo,uint32_t slotId,ICKind kind)82 LoadICRuntime(JSThread *thread, JSHandle<ProfileTypeInfo> profileTypeInfo, uint32_t slotId, ICKind kind) 83 : ICRuntime(thread, profileTypeInfo, slotId, kind) 84 { 85 } 86 87 ~LoadICRuntime() = default; 88 89 JSTaggedValue LoadMiss(JSHandle<JSTaggedValue> receiver, JSHandle<JSTaggedValue> key); 90 JSTaggedValue LoadValueMiss(JSHandle<JSTaggedValue> receiver, JSHandle<JSTaggedValue> key); 91 JSTaggedValue LoadTypedArrayValueMiss(JSHandle<JSTaggedValue> receiver, JSHandle<JSTaggedValue> key); 92 private: 93 JSTaggedValue LoadOrdinaryGet(JSHandle<JSTaggedValue> receiver, JSHandle<JSTaggedValue> key); 94 inline JSTaggedValue CallPrivateGetter(JSHandle<JSTaggedValue> receiver, JSHandle<JSTaggedValue> key); 95 }; 96 97 class StoreICRuntime : public ICRuntime { 98 public: StoreICRuntime(JSThread * thread,JSHandle<ProfileTypeInfo> profileTypeInfo,uint32_t slotId,ICKind kind)99 StoreICRuntime(JSThread *thread, JSHandle<ProfileTypeInfo> profileTypeInfo, uint32_t slotId, ICKind kind) 100 : ICRuntime(thread, profileTypeInfo, slotId, kind) 101 { 102 } 103 104 ~StoreICRuntime() = default; 105 106 JSTaggedValue StoreMiss(JSHandle<JSTaggedValue> receiver, JSHandle<JSTaggedValue> key, 107 JSHandle<JSTaggedValue> value, bool isOwn = false); 108 109 JSTaggedValue StoreTypedArrayValueMiss(JSHandle<JSTaggedValue> receiver, JSHandle<JSTaggedValue> key, 110 JSHandle<JSTaggedValue> value); 111 private: 112 inline JSTaggedValue CallPrivateSetter(JSHandle<JSTaggedValue> receiver, JSHandle<JSTaggedValue> key, 113 JSHandle<JSTaggedValue> value); 114 JSTaggedValue HandleAccesor(ObjectOperator *op, const JSHandle<JSTaggedValue> &value); 115 }; 116 } // namespace panda::ecmascript 117 118 #endif // ECMASCRIPT_IC_IC_RUNTIME_H 119