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/share_gate_meta_data.h" 23 24 namespace panda::ecmascript::kungfu { 25 enum class OperationType : uint8_t { 26 CALL, 27 NATIVE_CALL, 28 OPERATION_TYPE, 29 DEFINE_CLASS, 30 CREATE_OBJECT, 31 TRUE_BRANCH, 32 FALSE_BRANCH, 33 TRY_DUMP, 34 TRY_PREDUMP, 35 ITERATOR_FUNC_KIND, 36 TRY_JIT 37 }; 38 39 using SlotIDFormat = BytecodeInstruction::Format; 40 41 #define COMBINE_TYPE_CALL_BACK(curType, type) \ 42 callback.ProfileCombineOpType( \ 43 *(curType), type, [this](GateRef curType, GateRef type)->GateRef{ return Int32Or(curType, type); }) 44 45 using Callback = std::function<void(const std::initializer_list<GateRef> &, OperationType)>; 46 class ProfileOperation { 47 public: ProfileOperation()48 ProfileOperation() : callback_(nullptr), jitCallback_(nullptr) {} ProfileOperation(Callback callback,Callback jitCallback)49 explicit ProfileOperation(Callback callback, Callback jitCallback) : callback_(callback), 50 jitCallback_(jitCallback) {} 51 IsEmpty()52 inline bool IsEmpty() const 53 { 54 return callback_ == nullptr; 55 } 56 IsJitEmpty()57 inline bool IsJitEmpty() const 58 { 59 return jitCallback_ == nullptr; 60 } 61 ProfileCall(GateRef func)62 inline void ProfileCall(GateRef func) const 63 { 64 if (callback_) { 65 callback_({ func }, OperationType::CALL); 66 } 67 } 68 ProfileNativeCall(GateRef func)69 inline void ProfileNativeCall(GateRef func) const 70 { 71 if (callback_) { 72 callback_({ func }, OperationType::NATIVE_CALL); 73 } 74 } 75 ProfileOpType(GateRef type)76 inline void ProfileOpType(GateRef type) const 77 { 78 if (callback_) { 79 callback_({ type }, OperationType::OPERATION_TYPE); 80 } 81 } 82 83 template <typename TypeCombine> ProfileCombineOpType(GateRef curType,GateRef type,TypeCombine combine)84 inline void ProfileCombineOpType(GateRef curType, GateRef type, TypeCombine combine) const 85 { 86 if (callback_) { 87 GateRef ret = combine(curType, type); 88 callback_({ ret }, OperationType::OPERATION_TYPE); 89 } 90 } 91 ProfileDefineClass(GateRef constructor)92 inline void ProfileDefineClass(GateRef constructor) const 93 { 94 if (callback_) { 95 callback_({ constructor }, OperationType::DEFINE_CLASS); 96 } 97 } 98 ProfileCreateObject(GateRef newObj)99 inline void ProfileCreateObject(GateRef newObj) const 100 { 101 if (callback_) { 102 callback_({ newObj }, OperationType::CREATE_OBJECT); 103 } 104 } 105 TryDump()106 inline void TryDump() const 107 { 108 if (callback_) { 109 callback_({ }, OperationType::TRY_DUMP); 110 } 111 } 112 TryJitCompile()113 inline void TryJitCompile() const 114 { 115 if (jitCallback_) { 116 jitCallback_({ }, OperationType::TRY_JIT); 117 } 118 } 119 TryPreDump()120 inline void TryPreDump() const 121 { 122 if (callback_) { 123 callback_({ }, OperationType::TRY_PREDUMP); 124 } 125 } 126 ProfileBranch(bool isTrue)127 inline void ProfileBranch(bool isTrue) const 128 { 129 if (callback_) { 130 callback_({}, isTrue ? OperationType::TRUE_BRANCH : OperationType::FALSE_BRANCH); 131 } 132 } 133 ProfileGetIterator(GateRef iterator)134 inline void ProfileGetIterator(GateRef iterator) const 135 { 136 if (callback_) { 137 callback_({ iterator }, OperationType::ITERATOR_FUNC_KIND); 138 } 139 } 140 141 private: 142 Callback callback_; 143 Callback jitCallback_; 144 }; 145 } // namespace panda::ecmascript::kungfu 146 #endif // ECMASCRIPT_COMPILER_PROFILER_OPERATION_H 147