1 /*
2 * Copyright (c) 2022-2024 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/jspandafile/method_literal.h"
17
18 #include "ecmascript/interpreter/interpreter.h"
19 #include "ecmascript/jspandafile/js_pandafile.h"
20
21 #include "libpandafile/class_data_accessor.h"
22 #include "libpandafile/code_data_accessor-inl.h"
23 #include "libpandafile/method_data_accessor-inl.h"
24
25 namespace panda::ecmascript {
MethodLiteral(EntityId methodId)26 MethodLiteral::MethodLiteral(EntityId methodId)
27 {
28 ASSERT(methodId.IsValid());
29 SetMethodId(methodId);
30 }
31
Initialize(const JSPandaFile * jsPandaFile,const JSThread * thread,const uint32_t offset)32 void MethodLiteral::Initialize(const JSPandaFile *jsPandaFile, const JSThread *thread, const uint32_t offset)
33 {
34 const panda_file::File *pf = jsPandaFile->GetPandaFile();
35 EntityId methodId = GetMethodId();
36 if (UNLIKELY(offset != 0 && methodId.GetOffset() != offset)) {
37 LOG_ECMA(FATAL) << "Invalid methodId, expected methodId: " << offset << ", actual methodId: " << methodId;
38 }
39 panda_file::MethodDataAccessor mda(*pf, methodId);
40 auto codeId = mda.GetCodeId().value();
41 ASSERT(codeId.IsValid());
42
43 panda_file::CodeDataAccessor cda(*pf, codeId);
44 nativePointerOrBytecodeArray_ = cda.GetInstructions();
45 uint32_t codeSize = cda.GetCodeSize();
46 // When triggering jit compile through the execution count of the js function, set the hotness counter value to 0
47 // to ensure that the profile type info object can be created on the first execution of the js function.
48 bool cancelThreshold = (thread != nullptr && thread->GetEcmaVM()->GetJSOptions().GetJitCallThreshold() != 0);
49 SetHotnessCounter(EcmaInterpreter::GetHotnessCounter(codeSize, cancelThreshold));
50
51 uint32_t callType = UINT32_MAX; // UINT32_MAX means not found
52 uint32_t slotSize = 0;
53 mda.EnumerateAnnotations([&](EntityId annotationId) {
54 panda_file::AnnotationDataAccessor ada(*pf, annotationId);
55 auto *annotationName = reinterpret_cast<const char *>(pf->GetStringData(ada.GetClassId()).data);
56 if (::strcmp("L_ESCallTypeAnnotation;", annotationName) == 0) {
57 uint32_t elemCount = ada.GetCount();
58 for (uint32_t i = 0; i < elemCount; i++) {
59 panda_file::AnnotationDataAccessor::Elem adae = ada.GetElement(i);
60 auto *elemName = reinterpret_cast<const char *>(pf->GetStringData(adae.GetNameId()).data);
61 if (::strcmp("callType", elemName) == 0) {
62 callType = adae.GetScalarValue().GetValue();
63 }
64 }
65 } else if (::strcmp("L_ESSlotNumberAnnotation;", annotationName) == 0) {
66 uint32_t elemCount = ada.GetCount();
67 for (uint32_t i = 0; i < elemCount; i++) {
68 panda_file::AnnotationDataAccessor::Elem adae = ada.GetElement(i);
69 auto *elemName = reinterpret_cast<const char *>(pf->GetStringData(adae.GetNameId()).data);
70 if (::strcmp("SlotNumber", elemName) == 0) {
71 slotSize = adae.GetScalarValue().GetValue();
72 }
73 }
74 }
75 });
76
77 uint32_t numVregs = cda.GetNumVregs();
78 uint32_t numArgs = cda.GetNumArgs();
79 ASSERT((numArgs - HaveFuncBit::Decode(callType) -
80 HaveNewTargetBit::Decode(callType) - HaveThisBit::Decode(callType)) >= 0);
81 // Needed info for call can be got by loading callField only once.
82 // Native bit will be set in NewMethodForNativeFunction();
83 callField_ = (callType & CALL_TYPE_MASK) |
84 NumVregsBits::Encode(numVregs) |
85 NumArgsBits::Encode(numArgs - HaveFuncBit::Decode(callType) // exclude func
86 - HaveNewTargetBit::Decode(callType) // exclude new target
87 - HaveThisBit::Decode(callType)); // exclude this
88 SetSlotSize(slotSize);
89 }
90
91 // It's not allowed '#' token appear in ECMA function(method) name, which discriminates same names in panda methods.
ParseFunctionName(const JSPandaFile * jsPandaFile,EntityId methodId)92 std::string MethodLiteral::ParseFunctionName(const JSPandaFile* jsPandaFile, EntityId methodId)
93 {
94 std::string_view methodName = ParseFunctionNameView(jsPandaFile, methodId).first;
95 return std::string(methodName);
96 }
97
98 // It's not allowed '#' token appear in ECMA function(method) name, which discriminates same names in panda methods.
ParseFunctionNameView(const JSPandaFile * jsPandaFile,EntityId methodId)99 std::pair<std::string_view, bool> MethodLiteral::ParseFunctionNameView(
100 const JSPandaFile* jsPandaFile, EntityId methodId)
101 {
102 if (UNLIKELY(jsPandaFile == nullptr)) {
103 return {"", true};
104 }
105
106 auto [methodName, isASCII] = GetMethodNameView(jsPandaFile, methodId);
107 if (LIKELY(methodName[0] != '#')) {
108 return {methodName, isASCII};
109 }
110
111 size_t index = methodName.find_last_of('#');
112 methodName = methodName.substr(index + 1); // #...#functionName
113 if (index = methodName.find_last_of('^'); index != std::string::npos) {
114 methodName = methodName.substr(0, index); // #...#functionName^1
115 }
116 return {methodName, isASCII};
117 }
118
119 // It's not allowed '#' token appear in ECMA function(method) name, which discriminates same names in panda methods.
ParseFunctionNameToCString(const JSPandaFile * jsPandaFile,EntityId methodId)120 CString MethodLiteral::ParseFunctionNameToCString(const JSPandaFile *jsPandaFile, EntityId methodId)
121 {
122 if (jsPandaFile == nullptr) {
123 return "";
124 }
125
126 CString methodName(GetMethodName(jsPandaFile, methodId));
127 if (LIKELY(methodName[0] != '#')) {
128 return methodName;
129 }
130
131 size_t index = methodName.find_last_of('#');
132 methodName = methodName.substr(index + 1); // #...#functionName
133 if (methodName.find('^') != std::string::npos) {
134 index = methodName.find_last_of('^');
135 methodName = methodName.substr(0, index); // #...#functionName^1
136 }
137 return methodName;
138 }
139
GetMethodName(const JSPandaFile * jsPandaFile,EntityId methodId,bool cpuProfiler)140 const char* MethodLiteral::GetMethodName(const JSPandaFile* jsPandaFile, EntityId methodId, bool cpuProfiler)
141 {
142 if (jsPandaFile == nullptr) {
143 return "";
144 }
145 return GetMethodNameView(jsPandaFile, methodId, cpuProfiler).first.data();
146 }
147
GetMethodNameView(const JSPandaFile * jsPandaFile,EntityId methodId,bool cpuProfiler)148 std::pair<std::string_view, bool> MethodLiteral::GetMethodNameView(
149 const JSPandaFile* jsPandaFile, EntityId methodId, bool cpuProfiler)
150 {
151 ASSERT(jsPandaFile != nullptr && "jsPandaFile is null");
152 if (cpuProfiler) {
153 return jsPandaFile->GetCpuProfilerMethodName(methodId);
154 }
155 return const_cast<JSPandaFile*>(jsPandaFile)->GetMethodName(methodId);
156 }
157
GetRecordName(const JSPandaFile * jsPandaFile,EntityId methodId)158 CString MethodLiteral::GetRecordName(const JSPandaFile *jsPandaFile, EntityId methodId)
159 {
160 if (jsPandaFile == nullptr) {
161 return "";
162 }
163
164 return const_cast<JSPandaFile *>(jsPandaFile)->GetRecordName(methodId);
165 }
166
GetRecordNameWithSymbol(const JSPandaFile * jsPandaFile,EntityId methodId)167 const char *MethodLiteral::GetRecordNameWithSymbol(const JSPandaFile *jsPandaFile, EntityId methodId)
168 {
169 if (jsPandaFile == nullptr) {
170 return "";
171 }
172
173 const panda_file::File *pf = jsPandaFile->GetPandaFile();
174 panda_file::MethodDataAccessor mda(*pf, methodId);
175 panda_file::ClassDataAccessor cda(*pf, mda.GetClassId());
176 return utf::Mutf8AsCString(cda.GetDescriptor());
177 }
178
GetCodeSize(const JSPandaFile * jsPandaFile,EntityId methodId)179 uint32_t MethodLiteral::GetCodeSize(const JSPandaFile *jsPandaFile, EntityId methodId)
180 {
181 if (jsPandaFile == nullptr) {
182 return 0;
183 }
184
185 const panda_file::File *pandaFile = jsPandaFile->GetPandaFile();
186 panda_file::MethodDataAccessor mda(*pandaFile, methodId);
187 auto codeId = mda.GetCodeId().value();
188 if (!codeId.IsValid()) {
189 return 0;
190 }
191
192 panda_file::CodeDataAccessor cda(*pandaFile, codeId);
193 return cda.GetCodeSize();
194 }
195
GetConcurrentRequestedModules(const JSPandaFile * jsPandaFile) const196 std::optional<std::set<uint32_t>> MethodLiteral::GetConcurrentRequestedModules(const JSPandaFile *jsPandaFile) const
197 {
198 ASSERT(jsPandaFile != nullptr);
199 const panda_file::File *pf = jsPandaFile->GetPandaFile();
200 EntityId methodId = GetMethodId();
201 panda_file::MethodDataAccessor mda(*pf, methodId);
202 std::set<uint32_t> requestedModules;
203 bool hasRequestedModules = false;
204 mda.EnumerateAnnotations([&](EntityId annotationId) {
205 panda_file::AnnotationDataAccessor ada(*pf, annotationId);
206 auto *annotationName = reinterpret_cast<const char *>(pf->GetStringData(ada.GetClassId()).data);
207 if (::strcmp("L_ESConcurrentModuleRequestsAnnotation;", annotationName) == 0) {
208 hasRequestedModules = true;
209 uint32_t elemCount = ada.GetCount();
210 for (uint32_t i = 0; i < elemCount; i++) {
211 panda_file::AnnotationDataAccessor::Elem adae = ada.GetElement(i);
212 auto *elemName = reinterpret_cast<const char *>(pf->GetStringData(adae.GetNameId()).data);
213 if (::strcmp("ConcurrentModuleRequest", elemName) == 0) {
214 uint32_t index = adae.GetScalarValue().GetValue();
215 requestedModules.insert(index);
216 }
217 }
218 }
219 });
220 if (!hasRequestedModules) {
221 return std::nullopt;
222 }
223 return requestedModules;
224 }
225 } // namespace panda::ecmascript
226