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_HCR_GATE_META_DATA_H 17 #define ECMASCRIPT_COMPILER_HCR_GATE_META_DATA_H 18 19 #include <string> 20 21 #include "ecmascript/compiler/bytecodes.h" 22 #include "ecmascript/compiler/ecma_opcode_des.h" 23 #include "ecmascript/compiler/type.h" 24 #include "ecmascript/mem/chunk.h" 25 #include "ecmascript/mem/chunk_containers.h" 26 27 #include "ecmascript/elements.h" 28 #include "ecmascript/pgo_profiler/types/pgo_profiler_type.h" 29 #include "ecmascript/on_heap.h" 30 #include "libpandabase/macros.h" 31 32 #include "ecmascript/compiler/share_gate_meta_data.h" 33 34 namespace panda::ecmascript::kungfu { 35 36 class JSBytecodeMetaData : public GateMetaData { 37 public: JSBytecodeMetaData(size_t valuesIn,uint32_t methodId,EcmaOpcode opcode,uint32_t pcOffset,uint32_t bcIndex,GateFlags flags)38 explicit JSBytecodeMetaData( 39 size_t valuesIn, uint32_t methodId, EcmaOpcode opcode, uint32_t pcOffset, uint32_t bcIndex, GateFlags flags) 40 : GateMetaData(OpCode::JS_BYTECODE, flags, 1, 1, valuesIn), methodId_(methodId), opcode_(opcode), 41 pcOffset_(pcOffset), bcIndex_(bcIndex) 42 { 43 SetKind(GateMetaData::Kind::JSBYTECODE); 44 } 45 equal(const GateMetaData & other)46 bool equal(const GateMetaData &other) const override 47 { 48 if (!GateMetaData::equal(other)) { 49 return false; 50 } 51 auto cast_other = static_cast<const JSBytecodeMetaData *>(&other); 52 if (opcode_ == cast_other->opcode_ && 53 pcOffset_ == cast_other->pcOffset_ && type_ == cast_other->type_ && 54 elementsKinds_ == cast_other->elementsKinds_) { 55 return true; 56 } 57 return false; 58 } 59 Cast(const GateMetaData * meta)60 static const JSBytecodeMetaData* Cast(const GateMetaData* meta) 61 { 62 meta->AssertKind(GateMetaData::Kind::JSBYTECODE); 63 return static_cast<const JSBytecodeMetaData*>(meta); 64 } 65 GetMethodId()66 uint32_t GetMethodId() const 67 { 68 return methodId_; 69 } 70 GetPcOffset()71 uint32_t GetPcOffset() const 72 { 73 return pcOffset_; 74 } 75 GetBcIndex()76 uint32_t GetBcIndex() const 77 { 78 return bcIndex_; 79 } 80 SetType(PGOTypeRef type)81 void SetType(PGOTypeRef type) 82 { 83 type_ = type; 84 } 85 GetType()86 PGOTypeRef GetType() const 87 { 88 return type_; 89 } 90 GetByteCodeOpcode()91 EcmaOpcode GetByteCodeOpcode() const 92 { 93 return opcode_; 94 } 95 SetElementsKind(ElementsKind kind)96 void SetElementsKind(ElementsKind kind) 97 { 98 elementsKinds_.emplace_back(kind); 99 } 100 GetElementsKind()101 ElementsKind GetElementsKind() const 102 { 103 auto size = elementsKinds_.size(); 104 if (size == 0) { 105 return ElementsKind::GENERIC; 106 } 107 return elementsKinds_[0]; 108 } 109 GetElementsKinds()110 std::vector<ElementsKind> GetElementsKinds() const 111 { 112 return elementsKinds_; 113 } 114 SetTransitionElementsKind(ElementsKind kind)115 void SetTransitionElementsKind(ElementsKind kind) 116 { 117 transitionElementsKinds_.emplace_back(kind); 118 } 119 GetTransitionElementsKind()120 ElementsKind GetTransitionElementsKind() const 121 { 122 auto size = transitionElementsKinds_.size(); 123 if (size == 0) { 124 return ElementsKind::GENERIC; 125 } 126 return transitionElementsKinds_[0]; 127 } 128 GetTransitionElementsKinds()129 std::vector<ElementsKind> GetTransitionElementsKinds() const 130 { 131 return transitionElementsKinds_; 132 } 133 SetElementsLength(uint32_t length)134 void SetElementsLength(uint32_t length) 135 { 136 elementsLength_ = length; 137 } 138 GetElementsLength()139 uint32_t GetElementsLength() const 140 { 141 return elementsLength_; 142 } 143 Str()144 std::string Str() const 145 { 146 return GetEcmaOpcodeStr(opcode_); 147 } 148 SetOnHeapMode(OnHeapMode onHeapMode)149 void SetOnHeapMode(OnHeapMode onHeapMode) 150 { 151 onHeapMode_ = onHeapMode; 152 } 153 GetOnHeapMode()154 OnHeapMode GetOnHeapMode() const 155 { 156 return onHeapMode_; 157 } 158 159 private: 160 uint32_t methodId_; 161 EcmaOpcode opcode_; 162 uint32_t pcOffset_; 163 uint32_t bcIndex_; 164 uint32_t elementsLength_ { 0 }; 165 PGOTypeRef type_; 166 std::vector<ElementsKind> elementsKinds_ {}; 167 std::vector<ElementsKind> transitionElementsKinds_ {}; 168 OnHeapMode onHeapMode_ {OnHeapMode::NONE}; 169 }; 170 171 172 class FrameStateOutput { 173 public: 174 static constexpr uint32_t INVALID_INDEX = static_cast<uint32_t>(-1); FrameStateOutput(uint32_t value)175 explicit FrameStateOutput(uint32_t value) : index_(value) {} 176 Invalid()177 static FrameStateOutput Invalid() 178 { 179 return FrameStateOutput(INVALID_INDEX); 180 } 181 IsInvalid()182 bool IsInvalid() const 183 { 184 return index_ == INVALID_INDEX; 185 } 186 GetValue()187 uint32_t GetValue() const 188 { 189 return index_; 190 } 191 private: 192 uint32_t index_; 193 }; 194 195 } 196 197 #endif // ECMASCRIPT_COMPILER_HCR_GATE_META_DATA_H 198