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 #include "ecmascript/pgo_profiler/pgo_profiler_layout.h"
17
18 namespace panda::ecmascript {
UpdateElementKind(const ElementsKind kind)19 void PGOHClassLayoutDesc::UpdateElementKind(const ElementsKind kind)
20 {
21 kind_ = kind;
22 }
23
UpdateKeyAndDesc(const CString & key,const PGOHandler & handler,PGOObjKind kind)24 void PGOHClassLayoutDesc::UpdateKeyAndDesc(const CString &key, const PGOHandler &handler, PGOObjKind kind)
25 {
26 switch (kind) {
27 case PGOObjKind::LOCAL:
28 case PGOObjKind::ELEMENT:
29 UpdateKeyAndDesc(key, handler, layoutDesc_);
30 break;
31 case PGOObjKind::PROTOTYPE:
32 UpdateKeyAndDesc(key, handler, ptLayoutDesc_);
33 break;
34 case PGOObjKind::CONSTRUCTOR:
35 UpdateKeyAndDesc(key, handler, ctorLayoutDesc_);
36 break;
37 default:
38 UNREACHABLE();
39 break;
40 }
41 }
42
FindDescWithKey(const CString & key,PGOHandler & handler) const43 bool PGOHClassLayoutDesc::FindDescWithKey(const CString &key, PGOHandler &handler) const
44 {
45 PropertyDesc desc;
46 if (!FindProperty(key, desc)) {
47 return false;
48 }
49 handler = desc.second;
50 return true;
51 }
52
Merge(const PGOHClassLayoutDesc & from)53 void PGOHClassLayoutDesc::Merge(const PGOHClassLayoutDesc &from)
54 {
55 for (const auto &iter : from.layoutDesc_) {
56 UpdateKeyAndDesc(iter.first, iter.second, PGOObjKind::LOCAL);
57 }
58 for (const auto &iter : from.ptLayoutDesc_) {
59 UpdateKeyAndDesc(iter.first, iter.second, PGOObjKind::PROTOTYPE);
60 }
61 for (const auto &iter : from.ctorLayoutDesc_) {
62 UpdateKeyAndDesc(iter.first, iter.second, PGOObjKind::CONSTRUCTOR);
63 }
64 }
65
UpdateKeyAndDesc(const CString & key,const PGOHandler & handler,LayoutDesc & layoutDesc)66 void PGOHClassLayoutDesc::UpdateKeyAndDesc(const CString &key, const PGOHandler &handler, LayoutDesc &layoutDesc)
67 {
68 for (auto &iter : layoutDesc) {
69 if (iter.first == key) {
70 PGOHandler oldHandler = iter.second;
71 if (oldHandler == handler) {
72 return;
73 }
74 auto oldTrackType = oldHandler.GetTrackType();
75 auto newTrackType = handler.GetTrackType();
76 if (oldTrackType == newTrackType) {
77 iter.second.SetIsAccessor(handler.IsAccessor());
78 return;
79 }
80
81 switch (oldTrackType) {
82 case TrackType::TAGGED:
83 iter.second.SetIsAccessor(handler.IsAccessor());
84 break;
85 case TrackType::NONE:
86 case TrackType::INT:
87 case TrackType::DOUBLE:
88 if (newTrackType != TrackType::TAGGED) {
89 newTrackType = static_cast<TrackType>(static_cast<uint8_t>(newTrackType) |
90 static_cast<uint8_t>(oldTrackType));
91 }
92 iter.second = PGOHandler(newTrackType, handler.IsAccessor());
93 break;
94 default:
95 break;
96 }
97 return;
98 }
99 }
100 layoutDesc.emplace_back(key, handler);
101 }
102 } // namespace panda::ecmascript
103