• 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_PGO_PROFILER_LAYOUT_H
17 #define ECMASCRIPT_PGO_PROFILER_LAYOUT_H
18 
19 #include <stdint.h>
20 #include <string>
21 
22 #include "ecmascript/elements.h"
23 #include "ecmascript/mem/c_containers.h"
24 #include "ecmascript/pgo_profiler/pgo_profiler_type.h"
25 #include "ecmascript/property_attributes.h"
26 
27 namespace panda::ecmascript {
28 class PGOHandler {
29 public:
30     using TrackTypeField = BitField<TrackType, 0, PropertyAttributes::TRACK_TYPE_NUM>; // 3 : three binary bits
31     using IsAccessorField = TrackTypeField::NextFlag;
32 
PGOHandler()33     PGOHandler()
34     {
35         SetTrackType(TrackType::NONE);
36         SetIsAccessor(false);
37     }
38 
PGOHandler(TrackType type,bool isAccessor)39     PGOHandler(TrackType type, bool isAccessor)
40     {
41         SetTrackType(type);
42         SetIsAccessor(isAccessor);
43     }
44 
GetValue()45     uint32_t GetValue() const
46     {
47         return value_;
48     }
49 
SetAttribute(PropertyAttributes & attr)50     bool SetAttribute(PropertyAttributes &attr) const
51     {
52         bool ret = false;
53         switch (GetTrackType()) {
54             case TrackType::DOUBLE:
55             case TrackType::NUMBER:
56                 attr.SetRepresentation(Representation::DOUBLE);
57                 ret = true;
58                 break;
59             case TrackType::INT:
60                 attr.SetRepresentation(Representation::INT);
61                 ret = true;
62                 break;
63             case TrackType::TAGGED:
64                 attr.SetRepresentation(Representation::TAGGED);
65                 break;
66             default:
67                 break;
68         }
69         return ret;
70     }
71 
SetTrackType(TrackType type)72     void SetTrackType(TrackType type)
73     {
74         TrackTypeField::Set(type, &value_);
75     }
76 
GetTrackType()77     TrackType GetTrackType() const
78     {
79         return TrackTypeField::Get(value_);
80     }
81 
SetIsAccessor(bool accessor)82     void SetIsAccessor(bool accessor)
83     {
84         IsAccessorField::Set(accessor, &value_);
85     }
86 
IsAccessor()87     bool IsAccessor() const
88     {
89         return IsAccessorField::Get(value_);
90     }
91 
92     bool operator!=(const PGOHandler &right) const
93     {
94         return value_ != right.value_;
95     }
96 
97     bool operator==(const PGOHandler &right) const
98     {
99         return value_ == right.value_;
100     }
101 
102 private:
103     uint32_t value_ { 0 };
104 };
105 
106 using PropertyDesc = std::pair<CString, PGOHandler>;
107 using LayoutDesc = CVector<PropertyDesc>;
108 
109 class PGOHClassLayoutDesc {
110 public:
PGOHClassLayoutDesc()111     PGOHClassLayoutDesc() {};
PGOHClassLayoutDesc(ClassType type)112     explicit PGOHClassLayoutDesc(ClassType type) : type_(type) {}
113 
SetSuperClassType(ClassType superType)114     void SetSuperClassType(ClassType superType)
115     {
116         superType_ = superType;
117     }
118 
GetSuperClassType()119     ClassType GetSuperClassType() const
120     {
121         return superType_;
122     }
123 
GetClassType()124     ClassType GetClassType() const
125     {
126         return type_;
127     }
128 
GetLayoutDesc()129     LayoutDesc GetLayoutDesc() const
130     {
131         return layoutDesc_;
132     }
133 
SetLayoutDesc(LayoutDesc & layoutDesc)134     void SetLayoutDesc(LayoutDesc &layoutDesc)
135     {
136         layoutDesc_ = layoutDesc;
137     }
138 
GetPtLayoutDesc()139     LayoutDesc GetPtLayoutDesc() const
140     {
141         return ptLayoutDesc_;
142     }
143 
GetCtorLayoutDesc()144     LayoutDesc GetCtorLayoutDesc() const
145     {
146         return ctorLayoutDesc_;
147     }
148 
GetElementsKind()149     ElementsKind GetElementsKind() const
150     {
151         return kind_;
152     }
153 
SetElementsKind(ElementsKind kind)154     void SetElementsKind(ElementsKind kind)
155     {
156         kind_ = kind;
157     }
158 
FindProperty(const CString & key,PropertyDesc & desc)159     bool FindProperty(const CString &key, PropertyDesc &desc) const
160     {
161         for (const auto &iter : layoutDesc_) {
162             if (iter.first == key) {
163                 desc = iter;
164                 return true;
165             }
166         }
167         return false;
168     }
169 
AddKeyAndDesc(const CString & key,const PGOHandler & handler)170     void AddKeyAndDesc(const CString &key, const PGOHandler &handler)
171     {
172         layoutDesc_.emplace_back(key, handler);
173     }
174 
AddPtKeyAndDesc(const CString & key,const PGOHandler & handler)175     void AddPtKeyAndDesc(const CString &key, const PGOHandler &handler)
176     {
177         ptLayoutDesc_.emplace_back(key, handler);
178     }
179 
AddCtorKeyAndDesc(const CString & key,const PGOHandler & handler)180     void AddCtorKeyAndDesc(const CString &key, const PGOHandler &handler)
181     {
182         ctorLayoutDesc_.emplace_back(key, handler);
183     }
184 
185     void UpdateElementKind(const ElementsKind kind);
186     void UpdateKeyAndDesc(const CString &key, const PGOHandler &handler, PGOObjKind kind);
187 
188     bool FindDescWithKey(const CString &key, PGOHandler &handler) const;
189 
190     void Merge(const PGOHClassLayoutDesc &from);
191 
192     bool operator<(const PGOHClassLayoutDesc &right) const
193     {
194         return type_ < right.type_;
195     }
196 
197 private:
198     void UpdateKeyAndDesc(const CString &key, const PGOHandler &handler, LayoutDesc &layoutDesc);
199 
200     ClassType type_;
201     ClassType superType_;
202     ElementsKind kind_;
203     LayoutDesc layoutDesc_;
204     LayoutDesc ptLayoutDesc_;
205     LayoutDesc ctorLayoutDesc_;
206 };
207 } // namespace panda::ecmascript
208 #endif // ECMASCRIPT_PGO_PROFILER_LAYOUT_H
209