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_PROFILER_OPERATION_H 17 #define ECMASCRIPT_COMPILER_PROFILER_OPERATION_H 18 19 #include <functional> 20 #include <initializer_list> 21 22 #include "ecmascript/compiler/gate_meta_data.h" 23 24 namespace panda::ecmascript::kungfu { 25 enum class OperationType : uint8_t { 26 CALL, 27 OPERATION_TYPE, 28 DEFINE_CLASS, 29 CREATE_OBJECT, 30 STORE_LAYOUT, 31 LOAD_LAYOUT, 32 }; 33 34 #define COMBINE_TYPE_CALL_BACK(curType, type) \ 35 callback.ProfileCombineOpType( \ 36 *(curType), type, [this](GateRef curType, GateRef type)->GateRef{ return Int32Or(curType, type); }) 37 38 using Callback = std::function<void(const std::initializer_list<GateRef> &, OperationType)>; 39 class ProfileOperation { 40 public: ProfileOperation()41 ProfileOperation() : callback_(nullptr) {} ProfileOperation(Callback callback)42 explicit ProfileOperation(Callback callback) : callback_(callback) {} 43 IsEmpty()44 inline bool IsEmpty() const 45 { 46 return callback_ == nullptr; 47 } 48 ProfileCall(GateRef func)49 inline void ProfileCall(GateRef func) const 50 { 51 if (callback_) { 52 callback_({ func }, OperationType::CALL); 53 } 54 } 55 ProfileOpType(GateRef type)56 inline void ProfileOpType(GateRef type) const 57 { 58 if (callback_) { 59 callback_({ type }, OperationType::OPERATION_TYPE); 60 } 61 } 62 63 template <typename TypeCombine> ProfileCombineOpType(GateRef curType,GateRef type,TypeCombine combine)64 inline void ProfileCombineOpType(GateRef curType, GateRef type, TypeCombine combine) const 65 { 66 if (callback_) { 67 GateRef ret = combine(curType, type); 68 callback_({ ret }, OperationType::OPERATION_TYPE); 69 } 70 } 71 ProfileDefineClass(GateRef constructor)72 inline void ProfileDefineClass(GateRef constructor) const 73 { 74 if (callback_) { 75 callback_({ constructor }, OperationType::DEFINE_CLASS); 76 } 77 } 78 ProfileCreateObject(GateRef newObj)79 inline void ProfileCreateObject(GateRef newObj) const 80 { 81 if (callback_) { 82 callback_({ newObj }, OperationType::CREATE_OBJECT); 83 } 84 } 85 ProfileObjLayoutByStore(GateRef object)86 inline void ProfileObjLayoutByStore(GateRef object) const 87 { 88 if (callback_) { 89 callback_({ object }, OperationType::STORE_LAYOUT); 90 } 91 } 92 ProfileObjLayoutByLoad(GateRef object)93 inline void ProfileObjLayoutByLoad(GateRef object) const 94 { 95 if (callback_) { 96 callback_({ object }, OperationType::LOAD_LAYOUT); 97 } 98 } 99 100 private: 101 Callback callback_; 102 }; 103 } // namespace panda::ecmascript::kungfu 104 #endif // ECMASCRIPT_COMPILER_PROFILER_OPERATION_H 105