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_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 25 namespace panda::ecmascript { 26 class ProfileTypeInfo; 27 class JSThread; 28 class ObjectOperator; 29 30 class ICRuntime { 31 public: ICRuntime(JSThread * thread,JSHandle<ProfileTypeInfo> profileTypeInfo,uint32_t slotId,ICKind kind)32 ICRuntime(JSThread *thread, JSHandle<ProfileTypeInfo> profileTypeInfo, uint32_t slotId, ICKind kind) 33 : thread_(thread), icAccessor_(thread, profileTypeInfo, slotId, kind) 34 { 35 } 36 37 ~ICRuntime() = default; 38 39 void UpdateLoadHandler(const ObjectOperator &op, JSHandle<JSTaggedValue> key, JSHandle<JSTaggedValue> receiver); 40 void UpdateLoadStringHandler(JSHandle<JSTaggedValue> receiver); 41 void UpdateTypedArrayHandler(JSHandle<JSTaggedValue> receiver); 42 void UpdateStoreHandler(const ObjectOperator &op, JSHandle<JSTaggedValue> key, JSHandle<JSTaggedValue> receiver); 43 GetThread()44 JSThread *GetThread() const 45 { 46 return thread_; 47 } 48 UpdateReceiverHClass(JSHandle<JSTaggedValue> receiverHClass)49 void UpdateReceiverHClass(JSHandle<JSTaggedValue> receiverHClass) 50 { 51 receiverHClass_ = receiverHClass; 52 } 53 GetICKind()54 ICKind GetICKind() const 55 { 56 return icAccessor_.GetKind(); 57 } 58 ConstructOp(JSHandle<JSTaggedValue> receiver,JSHandle<JSTaggedValue> key,JSHandle<JSTaggedValue> value,bool isOwn)59 ObjectOperator ConstructOp(JSHandle<JSTaggedValue> receiver, JSHandle<JSTaggedValue> key, 60 JSHandle<JSTaggedValue> value, bool isOwn) const 61 { 62 ObjectOperator op(GetThread(), receiver, key, 63 isOwn ? OperatorType::OWN : OperatorType::PROTOTYPE_CHAIN); 64 if (isOwn) { 65 bool enumerable = !(receiver->IsClassPrototype() || receiver->IsClassConstructor()); 66 PropertyDescriptor desc(GetThread(), value, true, enumerable, true); 67 PropertyAttributes attr(desc); 68 op.SetAttr(attr); 69 } 70 return op; 71 } 72 73 void TraceIC(JSHandle<JSTaggedValue> receiver, JSHandle<JSTaggedValue> key) const; 74 75 protected: 76 JSThread *thread_; 77 JSHandle<JSTaggedValue> receiverHClass_{}; 78 ProfileTypeAccessor icAccessor_; 79 }; 80 81 class LoadICRuntime : public ICRuntime { 82 public: LoadICRuntime(JSThread * thread,JSHandle<ProfileTypeInfo> profileTypeInfo,uint32_t slotId,ICKind kind)83 LoadICRuntime(JSThread *thread, JSHandle<ProfileTypeInfo> profileTypeInfo, uint32_t slotId, ICKind kind) 84 : ICRuntime(thread, profileTypeInfo, slotId, kind) 85 { 86 } 87 88 ~LoadICRuntime() = default; 89 90 JSTaggedValue LoadMiss(JSHandle<JSTaggedValue> receiver, JSHandle<JSTaggedValue> key); 91 JSTaggedValue LoadValueMiss(JSHandle<JSTaggedValue> receiver, JSHandle<JSTaggedValue> key); 92 JSTaggedValue LoadTypedArrayValueMiss(JSHandle<JSTaggedValue> receiver, JSHandle<JSTaggedValue> key); 93 }; 94 95 class StoreICRuntime : public ICRuntime { 96 public: StoreICRuntime(JSThread * thread,JSHandle<ProfileTypeInfo> profileTypeInfo,uint32_t slotId,ICKind kind)97 StoreICRuntime(JSThread *thread, JSHandle<ProfileTypeInfo> profileTypeInfo, uint32_t slotId, ICKind kind) 98 : ICRuntime(thread, profileTypeInfo, slotId, kind) 99 { 100 } 101 102 ~StoreICRuntime() = default; 103 104 JSTaggedValue StoreMiss(JSHandle<JSTaggedValue> receiver, JSHandle<JSTaggedValue> key, 105 JSHandle<JSTaggedValue> value, bool isOwn = false); 106 107 JSTaggedValue StoreTypedArrayValueMiss(JSHandle<JSTaggedValue> receiver, JSHandle<JSTaggedValue> key, 108 JSHandle<JSTaggedValue> value); 109 }; 110 } // namespace panda::ecmascript 111 112 #endif // ECMASCRIPT_IC_IC_RUNTIME_H 113