• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
GetHashCode()60     uint64_t GetHashCode() const override
61     {
62         auto hash = GateMetaData::GetHashCode();
63         hash = base::HashCombiner::HashCombine(hash, static_cast<size_t>(opcode_));
64         return hash;
65     }
66 
Cast(const GateMetaData * meta)67     static const JSBytecodeMetaData* Cast(const GateMetaData* meta)
68     {
69         meta->AssertKind(GateMetaData::Kind::JSBYTECODE);
70         return static_cast<const JSBytecodeMetaData*>(meta);
71     }
72 
GetMethodId()73     uint32_t GetMethodId() const
74     {
75         return methodId_;
76     }
77 
GetPcOffset()78     uint32_t GetPcOffset() const
79     {
80         return pcOffset_;
81     }
82 
GetBcIndex()83     uint32_t GetBcIndex() const
84     {
85         return bcIndex_;
86     }
87 
SetType(PGOTypeRef type)88     void SetType(PGOTypeRef type)
89     {
90         type_ = type;
91     }
92 
GetType()93     PGOTypeRef GetType() const
94     {
95         return type_;
96     }
97 
GetByteCodeOpcode()98     EcmaOpcode GetByteCodeOpcode() const
99     {
100         return opcode_;
101     }
102 
SetElementsKind(ElementsKind kind)103     void SetElementsKind(ElementsKind kind)
104     {
105         elementsKinds_.emplace_back(kind);
106     }
107 
GetElementsKind()108     ElementsKind GetElementsKind() const
109     {
110         auto size = elementsKinds_.size();
111         if (size == 0) {
112             return ElementsKind::GENERIC;
113         }
114         return elementsKinds_[0];
115     }
116 
GetElementsKinds()117     std::vector<ElementsKind> GetElementsKinds() const
118     {
119         return elementsKinds_;
120     }
121 
SetTransitionElementsKind(ElementsKind kind)122     void SetTransitionElementsKind(ElementsKind kind)
123     {
124         transitionElementsKinds_.emplace_back(kind);
125     }
126 
GetTransitionElementsKind()127     ElementsKind GetTransitionElementsKind() const
128     {
129         auto size = transitionElementsKinds_.size();
130         if (size == 0) {
131             return ElementsKind::GENERIC;
132         }
133         return transitionElementsKinds_[0];
134     }
135 
GetTransitionElementsKinds()136     std::vector<ElementsKind> GetTransitionElementsKinds() const
137     {
138         return transitionElementsKinds_;
139     }
140 
SetElementsLength(uint32_t length)141     void SetElementsLength(uint32_t length)
142     {
143         elementsLength_ = length;
144     }
145 
GetElementsLength()146     uint32_t GetElementsLength() const
147     {
148         return elementsLength_;
149     }
150 
SetRegionSpaceFlag(RegionSpaceFlag flag)151     void SetRegionSpaceFlag(RegionSpaceFlag flag)
152     {
153         regionSpaceFlag_ = flag;
154     }
155 
GetRegionSpaceFlag()156     RegionSpaceFlag GetRegionSpaceFlag() const
157     {
158         return regionSpaceFlag_;
159     }
160 
Str()161     std::string Str() const
162     {
163         return GetEcmaOpcodeStr(opcode_);
164     }
165 
SetOnHeapMode(OnHeapMode onHeapMode)166     void SetOnHeapMode(OnHeapMode onHeapMode)
167     {
168         onHeapMode_ = onHeapMode;
169     }
170 
GetOnHeapMode()171     OnHeapMode GetOnHeapMode() const
172     {
173         return onHeapMode_;
174     }
175 
176 private:
177     uint32_t methodId_;
178     EcmaOpcode opcode_;
179     uint32_t pcOffset_;
180     uint32_t bcIndex_;
181     uint32_t elementsLength_ { 0 };
182     PGOTypeRef type_;
183     std::vector<ElementsKind> elementsKinds_ {};
184     std::vector<ElementsKind> transitionElementsKinds_ {};
185     OnHeapMode onHeapMode_ {OnHeapMode::NONE};
186     RegionSpaceFlag regionSpaceFlag_ {RegionSpaceFlag::IN_YOUNG_SPACE};
187 };
188 
189 
190 class FrameStateOutput {
191 public:
192     static constexpr uint32_t INVALID_INDEX = static_cast<uint32_t>(-1);
FrameStateOutput(uint32_t value)193     explicit FrameStateOutput(uint32_t value) : index_(value) {}
194 
Invalid()195     static FrameStateOutput Invalid()
196     {
197         return FrameStateOutput(INVALID_INDEX);
198     }
199 
IsInvalid()200     bool IsInvalid() const
201     {
202         return index_ == INVALID_INDEX;
203     }
204 
GetValue()205     uint32_t GetValue() const
206     {
207         return index_;
208     }
209 private:
210     uint32_t index_;
211 };
212 
213 }
214 
215 #endif  // ECMASCRIPT_COMPILER_HCR_GATE_META_DATA_H
216