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 SetElementsLength(uint32_t length)115 void SetElementsLength(uint32_t length) 116 { 117 elementsLength_ = length; 118 } 119 GetElementsLength()120 uint32_t GetElementsLength() const 121 { 122 return elementsLength_; 123 } 124 Str()125 std::string Str() const 126 { 127 return GetEcmaOpcodeStr(opcode_); 128 } 129 SetOnHeapMode(OnHeapMode onHeapMode)130 void SetOnHeapMode(OnHeapMode onHeapMode) 131 { 132 onHeapMode_ = onHeapMode; 133 } 134 GetOnHeapMode()135 OnHeapMode GetOnHeapMode() const 136 { 137 return onHeapMode_; 138 } 139 140 private: 141 uint32_t methodId_; 142 EcmaOpcode opcode_; 143 uint32_t pcOffset_; 144 uint32_t bcIndex_; 145 uint32_t elementsLength_ { 0 }; 146 PGOTypeRef type_; 147 std::vector<ElementsKind> elementsKinds_ {}; 148 OnHeapMode onHeapMode_ {OnHeapMode::NONE}; 149 }; 150 151 152 class FrameStateOutput { 153 public: 154 static constexpr uint32_t INVALID_INDEX = static_cast<uint32_t>(-1); FrameStateOutput(uint32_t value)155 explicit FrameStateOutput(uint32_t value) : index_(value) {} 156 Invalid()157 static FrameStateOutput Invalid() 158 { 159 return FrameStateOutput(INVALID_INDEX); 160 } 161 IsInvalid()162 bool IsInvalid() const 163 { 164 return index_ == INVALID_INDEX; 165 } 166 GetValue()167 uint32_t GetValue() const 168 { 169 return index_; 170 } 171 private: 172 uint32_t index_; 173 }; 174 175 } 176 177 #endif // ECMASCRIPT_COMPILER_HCR_GATE_META_DATA_H 178