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/types/pgo_profile_type.h"
17
18
19 #include "ecmascript/pgo_profiler/pgo_profiler_info.h"
20
21 namespace panda::ecmascript::pgo {
22 const ProfileType ProfileType::PROFILE_TYPE_NONE = ProfileType(0, 0);
23
ProfileTypeRef(PGOContext & context,const ProfileType & type)24 ProfileTypeRef::ProfileTypeRef(PGOContext &context, const ProfileType &type)
25 {
26 ApEntityId apId(0);
27 context.GetProfileTypePool()->TryAdd(type, apId);
28 UpdateId(apId);
29 }
30
Remap(const PGOContext & context)31 ProfileTypeRef &ProfileTypeRef::Remap([[maybe_unused]] const PGOContext &context)
32 {
33 LOG_ECMA(ERROR) << "ProfileTypeRef do not support remapping";
34 UNREACHABLE();
35 }
36
ProfileType(PGOContext & context,ProfileTypeRef typeRef)37 ProfileType::ProfileType(PGOContext &context, ProfileTypeRef typeRef)
38 {
39 if (!context.GetHeader()->SupportWideProfileType()) {
40 ProfileTypeLegacy legacy(typeRef);
41 UpdateId(legacy.GetId());
42 UpdateKind(legacy.GetKind());
43 } else {
44 const auto *typeEntry = context.GetProfileTypePool()->GetEntry(typeRef.GetId());
45 if (typeEntry == nullptr) {
46 LOG_ECMA(ERROR) << "Profile type ref: " << typeRef.GetTypeString() << " not found in ap file.";
47 } else {
48 type_ = typeEntry->GetProfileType().GetRaw();
49 }
50 }
51 }
52
CreateFromProfileTypeRef(PGOContext & context,ProfileTypeRef typeRef)53 std::optional<ProfileType> ProfileType::CreateFromProfileTypeRef(PGOContext &context, ProfileTypeRef typeRef)
54 {
55 if (!context.GetHeader()->SupportWideProfileType()) {
56 ProfileType type;
57 ProfileTypeLegacy legacy(typeRef);
58 type.UpdateId(legacy.GetId());
59 type.UpdateKind(legacy.GetKind());
60 return type;
61 }
62
63 const auto *typeEntry = context.GetProfileTypePool()->GetEntry(typeRef.GetId());
64 if (typeEntry == nullptr) {
65 return std::nullopt;
66 }
67 return ProfileType(typeEntry->GetProfileType().GetRaw());
68 }
69
Remap(const PGOContext & context)70 ProfileType &ProfileType::Remap([[maybe_unused]]const PGOContext &context)
71 {
72 if ((GetAbcId() >= PGOAbcFilePool::RESERVED_COUNT) && (!context.GetAbcIdRemap().empty())) {
73 UpdateAbcId(context.GetAbcIdRemap().at(GetAbcId()));
74 }
75 return *this;
76 }
77 } // namespace panda::ecmascript::pgo
78