1 /*
2 * Copyright (c) 2023-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/compiler/aot_snapshot/snapshot_constantpool_data.h"
17
18 #include "ecmascript/compiler/pgo_type/pgo_type_manager.h"
19 #include "ecmascript/js_object-inl.h"
20 #include "ecmascript/jspandafile/program_object.h"
21
22 namespace panda::ecmascript::kungfu {
GetItemKey(uint32_t constantPoolId,uint32_t constantPoolIdx)23 uint64_t BaseSnapshotInfo::GetItemKey(uint32_t constantPoolId, uint32_t constantPoolIdx)
24 {
25 uint64_t result = constantPoolId;
26 result = result << CONSTPOOL_MASK;
27 result |= constantPoolIdx;
28 return result;
29 }
30
TryGetABCId(ApEntityId & abcId)31 bool BaseSnapshotInfo::TryGetABCId(ApEntityId &abcId)
32 {
33 return pfDecoder_->GetABCIdByJSPandaFile(jsPandaFile_, abcId);
34 }
35
TryGetIHClass(ProfileType rootType,ProfileType childType,const ItemData & data,const JSHandle<TaggedArray> & properties,const SnapshotGlobalData & globalData) const36 JSHandle<JSTaggedValue> BaseSnapshotInfo::TryGetIHClass(ProfileType rootType, ProfileType childType,
37 const ItemData &data, const JSHandle<TaggedArray> &properties, const SnapshotGlobalData &globalData) const
38 {
39 JSHandle<JSTaggedValue> ihc = TryGetHClass(rootType, childType);
40 if (ihc->IsUndefined()) {
41 PGOTypeLocation loc(jsPandaFile_, data.methodOffset_, data.bcIndex_);
42 ihc = TryGetHClassByPGOTypeLocation(loc);
43 if (ihc->IsUndefined()) {
44 ihc = TryGetHClassFromCached(properties, globalData);
45 }
46 }
47 return ihc;
48 }
49
TryGetHClass(ProfileType rootType,ProfileType childType) const50 JSHandle<JSTaggedValue> BaseSnapshotInfo::TryGetHClass(ProfileType rootType, ProfileType childType) const
51 {
52 PGOTypeManager* ptManager = thread_->GetEcmaVM()->GetPTManager();
53 JSTaggedValue hclass = ptManager->QueryHClass(rootType, childType);
54 return JSHandle<JSTaggedValue>(thread_, hclass);
55 }
56
TryGetHClassByPGOTypeLocation(PGOTypeLocation loc) const57 JSHandle<JSTaggedValue> BaseSnapshotInfo::TryGetHClassByPGOTypeLocation(PGOTypeLocation loc) const
58 {
59 PGOTypeManager* ptManager = thread_->GetEcmaVM()->GetPTManager();
60 ProfileType pt = ptManager->GetRootIdByLocation(loc);
61 return TryGetHClass(pt, pt);
62 }
63
TryGetHClassFromCached(const JSHandle<TaggedArray> & properties,const SnapshotGlobalData & globalData) const64 JSHandle<JSTaggedValue> BaseSnapshotInfo::TryGetHClassFromCached(const JSHandle<TaggedArray> &properties,
65 const SnapshotGlobalData &globalData) const
66 {
67 DISALLOW_GARBAGE_COLLECTION;
68 JSHandle<JSTaggedValue> maybeCache(thread_, globalData.GetObjectLiteralHClassCache(thread_));
69 if (maybeCache->IsTaggedArray()) {
70 size_t length = properties->GetLength();
71 uint32_t propsLen = 0;
72 for (size_t i = 0; i < length; i += 2) { // 2: skip a pair of key and value
73 if (properties->Get(thread_, i).IsHole()) {
74 break;
75 }
76 propsLen++;
77 }
78
79 if (!ObjectFactory::CanObjectLiteralHClassCache(propsLen)) {
80 return thread_->GlobalConstants()->GetHandledUndefined();
81 }
82
83 JSHandle<TaggedArray> hclassCacheArr = JSHandle<TaggedArray>::Cast(maybeCache);
84 JSTaggedValue maybeHClass = hclassCacheArr->Get(thread_, propsLen);
85 if (!maybeHClass.IsJSHClass()) {
86 return thread_->GlobalConstants()->GetHandledUndefined();
87 }
88 JSHClass *newClass = JSHClass::Cast(maybeHClass.GetTaggedObject());
89 JSMutableHandle<JSTaggedValue> key(thread_, JSTaggedValue::Undefined());
90
91 for (size_t fieldOffset = 0; fieldOffset < propsLen; fieldOffset++) {
92 key.Update(properties->Get(thread_, fieldOffset * 2)); // 2 : pair of key and value
93 ASSERT_PRINT(JSTaggedValue::IsPropertyKey(key), "Key is not a property key");
94 PropertyAttributes attributes = PropertyAttributes::Default();
95 auto value = properties->Get(thread_, fieldOffset * 2 + 1);
96 if (value.IsAccessor()) { // 2: Meaning to double
97 attributes.SetIsAccessor(true);
98 }
99 attributes.SetIsInlinedProps(true);
100 attributes.SetRepresentation(Representation::TAGGED);
101 attributes.SetOffset(fieldOffset);
102
103 auto metadata = JSTaggedValue(attributes.GetPropertyMetaData());
104 auto rep = PropertyAttributes::TranslateToRep(value);
105 newClass = newClass->FindTransitions(thread_, key.GetTaggedValue(), metadata, rep);
106 if (newClass == nullptr) {
107 return thread_->GlobalConstants()->GetHandledUndefined();
108 }
109 }
110
111 auto result = JSHandle<JSHClass>(thread_, newClass);
112 if (JSObject::CheckPropertiesForRep(thread_, properties, propsLen, result)) {
113 return JSHandle<JSTaggedValue>(result);
114 }
115 }
116 return thread_->GlobalConstants()->GetHandledUndefined();
117 }
118
Record(ItemData & data)119 void BaseSnapshotInfo::Record(ItemData &data)
120 {
121 ItemKey key = GetItemKey(data.constantPoolId_, data.constantPoolIdx_);
122 info_.emplace(key, data);
123 }
124
CollectLiteralInfo(JSHandle<TaggedArray> array,uint32_t constantPoolIndex,JSHandle<ConstantPool> snapshotConstantPool,const std::set<uint32_t> & skippedMethods,JSHandle<JSTaggedValue> ihc,JSHandle<JSTaggedValue> chc)125 void BaseSnapshotInfo::CollectLiteralInfo(JSHandle<TaggedArray> array, uint32_t constantPoolIndex,
126 JSHandle<ConstantPool> snapshotConstantPool,
127 const std::set<uint32_t> &skippedMethods,
128 JSHandle<JSTaggedValue> ihc, JSHandle<JSTaggedValue> chc)
129 {
130 ObjectFactory *factory = vm_->GetFactory();
131 JSMutableHandle<JSTaggedValue> valueHandle(thread_, JSTaggedValue::Undefined());
132 uint32_t len = array->GetLength();
133 std::vector<int> methodOffsetVec;
134 for (uint32_t i = 0; i < len; i++) {
135 valueHandle.Update(array->Get(thread_, i));
136 uint32_t methodOffset = 0;
137 if (valueHandle->IsJSFunction()) {
138 methodOffset = JSHandle<JSFunction>(valueHandle)->GetCallTarget(thread_)->GetMethodId().GetOffset();
139 } else if (valueHandle->IsFunctionTemplate()) {
140 auto method = Method::Cast(JSHandle<FunctionTemplate>(valueHandle)->GetMethod(thread_));
141 methodOffset = method->GetMethodId().GetOffset();
142 }
143 if (methodOffset != 0) {
144 if (skippedMethods.find(methodOffset) != skippedMethods.end()) {
145 methodOffsetVec.emplace_back(AOTLiteralInfo::NO_FUNC_ENTRY_VALUE);
146 } else {
147 methodOffsetVec.emplace_back(methodOffset);
148 }
149 }
150 }
151
152 uint32_t methodSize = methodOffsetVec.size();
153 JSHandle<AOTLiteralInfo> aotLiteralInfo = factory->NewAOTLiteralInfo(methodSize);
154 for (uint32_t i = 0; i < methodSize; ++i) {
155 auto methodOffset = methodOffsetVec[i];
156 aotLiteralInfo->SetObjectToCache(thread_, i, JSTaggedValue(methodOffset));
157 }
158
159 if (!ihc->IsUndefined()) {
160 aotLiteralInfo->SetIhc(thread_, ihc.GetTaggedValue());
161 }
162
163 if (!chc->IsUndefined()) {
164 aotLiteralInfo->SetChc(thread_, chc.GetTaggedValue());
165 }
166
167 snapshotConstantPool->SetObjectToCache(thread_, constantPoolIndex, aotLiteralInfo.GetTaggedValue());
168 }
169
CheckAOTPropertiesForRep(const JSThread * thread,const JSHandle<TaggedArray> & properties,const JSHandle<JSHClass> & hclass)170 bool BaseSnapshotInfo::CheckAOTPropertiesForRep(const JSThread *thread,
171 const JSHandle<TaggedArray> &properties,
172 const JSHandle<JSHClass> &hclass)
173 {
174 auto layout = LayoutInfo::Cast(hclass->GetLayout(thread).GetTaggedObject());
175 for (size_t i = 0; i < properties->GetLength(); i++) {
176 auto attr = layout->GetAttr(thread, i);
177 auto value = JSObject::ConvertValueWithRep(attr, properties->Get(thread, i));
178 // If value.first is false, indicating that value cannot be converted to the expected value of
179 // representation. For example, the representation is INT, but the value type is string.
180 if (!value.first) {
181 return false;
182 }
183 }
184 return true;
185 }
186
CheckAOTIhcPropertiesForRep(JSThread * thread,const JSHandle<JSTaggedValue> & ihc,const JSHandle<ClassInfoExtractor> & extractor)187 bool BaseSnapshotInfo::CheckAOTIhcPropertiesForRep(JSThread *thread, const JSHandle<JSTaggedValue> &ihc,
188 const JSHandle<ClassInfoExtractor> &extractor)
189 {
190 if (ihc->IsUndefinedOrNull()) {
191 return false;
192 }
193 JSHandle<JSObject> prototype;
194 if (ihc->IsJSHClass()) {
195 JSHandle<JSHClass> ihclass(ihc);
196 prototype = JSHandle<JSObject>(thread, ihclass->GetProto(thread));
197 } else {
198 prototype = JSHandle<JSObject>(ihc);
199 }
200
201 ASSERT(!prototype->GetJSHClass()->IsDictionaryMode());
202 JSHandle<TaggedArray> nonStaticProperties(thread, extractor->GetNonStaticProperties(thread));
203 JSHandle<JSHClass> protohclass(thread, prototype->GetJSHClass());
204 return CheckAOTPropertiesForRep(thread, nonStaticProperties, protohclass);
205 }
206
CheckAOTChcPropertiesForRep(JSThread * thread,const JSHandle<JSTaggedValue> & chc,const JSHandle<ClassInfoExtractor> & extractor)207 bool BaseSnapshotInfo::CheckAOTChcPropertiesForRep(JSThread *thread, const JSHandle<JSTaggedValue> &chc,
208 const JSHandle<ClassInfoExtractor> &extractor)
209 {
210 if (chc->IsUndefinedOrNull()) {
211 return false;
212 }
213 JSHandle<JSHClass> chclass(thread, JSHClass::Cast(chc->GetTaggedObject()));
214 ASSERT(!chclass->IsDictionaryMode());
215 JSHandle<TaggedArray> staticProperties(thread, extractor->GetStaticProperties(thread));
216 return CheckAOTPropertiesForRep(thread, staticProperties, chclass);
217 }
218
StoreDataToGlobalData(SnapshotGlobalData & globalData,const std::set<uint32_t> &)219 void StringSnapshotInfo::StoreDataToGlobalData(SnapshotGlobalData &globalData, const std::set<uint32_t>&)
220 {
221 for (auto item : info_) {
222 const ItemData &data = item.second;
223 uint32_t snapshotCpArrIdx = globalData.GetCpArrIdxByConstanPoolId(data.constantPoolId_);
224 JSHandle<TaggedArray> snapshotCpArr(thread_, globalData.GetCurSnapshotCpArray());
225 JSHandle<ConstantPool> snapshotCp(thread_, snapshotCpArr->Get(thread_, snapshotCpArrIdx));
226 // Lazy ConstantPool String Loading
227 snapshotCp->SetObjectToCache(thread_, data.constantPoolIdx_, JSTaggedValue::Hole());
228 }
229
230 if (vm_->GetJSOptions().IsEnableCompilerLogSnapshot()) {
231 vm_->AddAOTSnapShotStats("String", info_.size());
232 }
233 }
234
StoreDataToGlobalData(SnapshotGlobalData & globalData,const std::set<uint32_t> & skippedMethods)235 void MethodSnapshotInfo::StoreDataToGlobalData(SnapshotGlobalData &globalData,
236 const std::set<uint32_t> &skippedMethods)
237 {
238 ApEntityId abcId = INVALID_INDEX;
239 bool hasAbcId = TryGetABCId(abcId);
240 ObjectFactory *factory = vm_->GetFactory();
241 for (auto item : info_) {
242 const ItemData &data = item.second;
243 JSHandle<ConstantPool> cp(thread_,
244 thread_->GetEcmaVM()->FindConstpool(jsPandaFile_, data.constantPoolId_));
245 uint32_t methodOffset = cp->GetEntityId(data.constantPoolIdx_).GetOffset();
246
247 uint32_t snapshotCpArrIdx = globalData.GetCpArrIdxByConstanPoolId(data.constantPoolId_);
248 JSHandle<TaggedArray> snapshotCpArr(thread_, globalData.GetCurSnapshotCpArray());
249 JSHandle<ConstantPool> snapshotCp(thread_, snapshotCpArr->Get(thread_, snapshotCpArrIdx));
250
251 JSHandle<JSTaggedValue> ihc = thread_->GlobalConstants()->GetHandledUndefined();
252 if (hasAbcId) {
253 ProfileType pt(abcId, methodOffset, ProfileType::Kind::ClassId, true);
254 ihc = TryGetHClass(pt, pt);
255 }
256 JSHandle<AOTLiteralInfo> aotLiteralInfo = factory->NewAOTLiteralInfo(1); // 1: only one method
257 int initValue = static_cast<int>(AOTLiteralInfo::NO_FUNC_ENTRY_VALUE);
258 aotLiteralInfo->SetObjectToCache(thread_, 0, JSTaggedValue(initValue));
259 aotLiteralInfo->SetLiteralType(JSTaggedValue(AOTLiteralInfo::METHOD_LITERAL_TYPE));
260 if (!ihc->IsUndefined()) {
261 aotLiteralInfo->SetIhc(thread_, ihc.GetTaggedValue());
262 if (skippedMethods.find(methodOffset) == skippedMethods.end()) {
263 aotLiteralInfo->SetObjectToCache(thread_, 0, JSTaggedValue(methodOffset));
264 globalData.RecordReviseData(
265 ReviseData::ItemData {globalData.GetCurDataIdx(), snapshotCpArrIdx, data.constantPoolIdx_});
266 }
267 snapshotCp->SetObjectToCache(thread_, data.constantPoolIdx_, aotLiteralInfo.GetTaggedValue());
268 } else if (skippedMethods.find(methodOffset) == skippedMethods.end()) {
269 // For MethodSnaphotInfo which does not have ihc info, we insert JSTaggedValue(methodOffset) as revervation.
270 // For the purpose of reducing ai size.
271 globalData.RecordReviseData(
272 ReviseData::ItemData {globalData.GetCurDataIdx(), snapshotCpArrIdx, data.constantPoolIdx_});
273 snapshotCp->SetObjectToCache(thread_, data.constantPoolIdx_, JSTaggedValue(methodOffset));
274 } else {
275 snapshotCp->SetObjectToCache(thread_, data.constantPoolIdx_, JSTaggedValue::Hole());
276 }
277 }
278 if (vm_->GetJSOptions().IsEnableCompilerLogSnapshot()) {
279 vm_->AddAOTSnapShotStats("Method", info_.size());
280 }
281 }
282
StoreDataToGlobalData(SnapshotGlobalData & globalData,const std::set<uint32_t> & skippedMethods)283 void ClassLiteralSnapshotInfo::StoreDataToGlobalData(SnapshotGlobalData &globalData,
284 const std::set<uint32_t> &skippedMethods)
285 {
286 ApEntityId abcId = INVALID_INDEX;
287 bool hasAbcId = TryGetABCId(abcId);
288 for (auto item : info_) {
289 const ItemData &data = item.second;
290 JSHandle<ConstantPool> cp = GetUnsharedConstpool(data);
291 auto literalObj = ConstantPool::GetClassLiteralFromCache(thread_, cp, data.constantPoolIdx_, data.recordName_);
292 JSHandle<ClassLiteral> classLiteral(thread_, literalObj);
293 JSHandle<TaggedArray> arrayHandle(thread_, classLiteral->GetArray(thread_));
294
295 uint32_t snapshotCpArrIdx = globalData.GetCpArrIdxByConstanPoolId(data.constantPoolId_);
296 JSHandle<TaggedArray> snapshotCpArr(thread_, globalData.GetCurSnapshotCpArray());
297 JSHandle<ConstantPool> snapshotCp(thread_, snapshotCpArr->Get(thread_, snapshotCpArrIdx));
298
299 uint32_t methodId = cp->GetEntityId(data.ctorMethodOffset_).GetOffset();
300 JSHandle<JSTaggedValue> undefinedHandle = thread_->GlobalConstants()->GetHandledUndefined();
301 JSHandle<JSTaggedValue> ihc = undefinedHandle;
302 JSHandle<JSTaggedValue> chc = undefinedHandle;
303 if (hasAbcId) {
304 ProfileType pt(abcId, methodId, ProfileType::Kind::ClassId, true);
305 ProfileType ctorPt(abcId, methodId, ProfileType::Kind::ConstructorId, true);
306 ihc = TryGetHClass(pt, pt);
307 chc = TryGetHClass(ctorPt, ctorPt);
308
309 auto method = ConstantPool::GetMethodFromCache(thread_, cp.GetTaggedValue(), data.ctorMethodOffset_);
310 auto *factory = thread_->GetEcmaVM()->GetFactory();
311 auto extractor = factory->NewClassInfoExtractor(JSHandle<JSTaggedValue>(thread_, method));
312 ClassInfoExtractor::BuildClassInfoExtractorFromLiteral(thread_, extractor, arrayHandle,
313 arrayHandle->GetLength());
314 if (!CheckAOTIhcPropertiesForRep(thread_, ihc, extractor)) {
315 ihc = undefinedHandle;
316 }
317 if (!CheckAOTChcPropertiesForRep(thread_, chc, extractor)) {
318 chc = undefinedHandle;
319 }
320 }
321
322 CollectLiteralInfo(arrayHandle, data.constantPoolIdx_, snapshotCp, skippedMethods, ihc, chc);
323 globalData.RecordReviseData(
324 ReviseData::ItemData {globalData.GetCurDataIdx(), snapshotCpArrIdx, data.constantPoolIdx_});
325 }
326 if (vm_->GetJSOptions().IsEnableCompilerLogSnapshot()) {
327 vm_->AddAOTSnapShotStats("ClassLiteral", info_.size());
328 }
329 }
330
StoreDataToGlobalData(SnapshotGlobalData & globalData,const std::set<uint32_t> & skippedMethods)331 void ObjectLiteralSnapshotInfo::StoreDataToGlobalData(SnapshotGlobalData &globalData,
332 const std::set<uint32_t> &skippedMethods)
333 {
334 ApEntityId abcId = INVALID_INDEX;
335 bool hasAbcId = TryGetABCId(abcId);
336 for (auto item : info_) {
337 const ItemData &data = item.second;
338 JSHandle<ConstantPool> cp = GetUnsharedConstpool(data);
339 panda_file::File::EntityId id = cp->GetEntityId(data.constantPoolIdx_);
340 JSMutableHandle<TaggedArray> elements(thread_, JSTaggedValue::Undefined());
341 JSMutableHandle<TaggedArray> properties(thread_, JSTaggedValue::Undefined());
342 LiteralDataExtractor::ExtractObjectDatas(thread_, jsPandaFile_, id, elements,
343 properties, cp, data.recordName_);
344
345 uint32_t snapshotCpArrIdx = globalData.GetCpArrIdxByConstanPoolId(data.constantPoolId_);
346 JSHandle<TaggedArray> snapshotCpArr(thread_, globalData.GetCurSnapshotCpArray());
347 JSHandle<ConstantPool> snapshotCp(thread_, snapshotCpArr->Get(thread_, snapshotCpArrIdx));
348
349 JSHandle<JSTaggedValue> ihc = thread_->GlobalConstants()->GetHandledUndefined();
350 JSHandle<JSTaggedValue> chc = thread_->GlobalConstants()->GetHandledUndefined();
351 if (hasAbcId) {
352 ProfileType pt(abcId, id.GetOffset(), ProfileType::Kind::ObjectLiteralId, true);
353 ProfileType ctorPt(abcId, id.GetOffset(), ProfileType::Kind::ConstructorId, true);
354 chc = TryGetHClass(ctorPt, ctorPt);
355 ihc = TryGetIHClass(pt, pt, data, properties, globalData);
356 }
357
358 CollectLiteralInfo(properties, data.constantPoolIdx_, snapshotCp, skippedMethods, ihc, chc);
359 globalData.RecordReviseData(
360 ReviseData::ItemData {globalData.GetCurDataIdx(), snapshotCpArrIdx, data.constantPoolIdx_});
361 }
362 if (vm_->GetJSOptions().IsEnableCompilerLogSnapshot()) {
363 vm_->AddAOTSnapShotStats("ObjectLiteral", info_.size());
364 }
365 }
366
StoreDataToGlobalData(SnapshotGlobalData & globalData,const std::set<uint32_t> & skippedMethods)367 void ArrayLiteralSnapshotInfo::StoreDataToGlobalData(SnapshotGlobalData &globalData,
368 const std::set<uint32_t> &skippedMethods)
369 {
370 for (auto item : info_) {
371 const ItemData &data = item.second;
372 JSHandle<ConstantPool> cp = GetUnsharedConstpool(data);
373 panda_file::File::EntityId id = cp->GetEntityId(data.constantPoolIdx_);
374 JSHandle<TaggedArray> literal = LiteralDataExtractor::GetDatasIgnoreType(
375 thread_, jsPandaFile_, id, cp, data.recordName_);
376 uint32_t snapshotCpArrIdx = globalData.GetCpArrIdxByConstanPoolId(data.constantPoolId_);
377 JSHandle<TaggedArray> snapshotCpArr(thread_, globalData.GetCurSnapshotCpArray());
378 JSHandle<ConstantPool> snapshotCp(thread_, snapshotCpArr->Get(thread_, snapshotCpArrIdx));
379 JSHandle<JSTaggedValue> ihc = thread_->GlobalConstants()->GetHandledUndefined();
380 JSHandle<JSTaggedValue> chc = thread_->GlobalConstants()->GetHandledUndefined();
381 CollectLiteralInfo(literal, data.constantPoolIdx_, snapshotCp, skippedMethods, ihc, chc);
382 globalData.RecordReviseData(
383 ReviseData::ItemData {globalData.GetCurDataIdx(), snapshotCpArrIdx, data.constantPoolIdx_});
384 }
385 if (vm_->GetJSOptions().IsEnableCompilerLogSnapshot()) {
386 vm_->AddAOTSnapShotStats("ArrayLiteral", info_.size());
387 }
388 }
389
GetUnsharedConstpool(const ItemData & data)390 JSHandle<ConstantPool> BaseSnapshotInfo::GetUnsharedConstpool(const ItemData &data)
391 {
392 EcmaVM *vm = thread_->GetEcmaVM();
393 JSTaggedValue shareCp = vm->FindConstpool(jsPandaFile_, data.constantPoolId_);
394 JSHandle<ConstantPool> cp(thread_, vm->FindOrCreateUnsharedConstpool(shareCp));
395 return cp;
396 }
397
Record(const BytecodeInstruction & bcIns,int32_t bcIndex,const CString & recordName,const MethodLiteral * method)398 void SnapshotConstantPoolData::Record(const BytecodeInstruction &bcIns, int32_t bcIndex,
399 const CString &recordName, const MethodLiteral *method)
400 {
401 BytecodeInstruction::Opcode opcode = static_cast<BytecodeInstruction::Opcode>(bcIns.GetOpcode());
402 uint32_t methodOffset = method->GetMethodId().GetOffset();
403 panda_file::IndexAccessor indexAccessor(*jsPandaFile_->GetPandaFile(),
404 panda_file::File::EntityId(methodOffset));
405 uint32_t constantPoolId = static_cast<uint32_t>(indexAccessor.GetHeaderIndex());
406
407 switch (opcode) {
408 case BytecodeInstruction::Opcode::LDA_STR_ID16:
409 case BytecodeInstruction::Opcode::STOWNBYNAME_IMM8_ID16_V8:
410 case BytecodeInstruction::Opcode::STOWNBYNAME_IMM16_ID16_V8:
411 case BytecodeInstruction::Opcode::CREATEREGEXPWITHLITERAL_IMM8_ID16_IMM8:
412 case BytecodeInstruction::Opcode::CREATEREGEXPWITHLITERAL_IMM16_ID16_IMM8:
413 case BytecodeInstruction::Opcode::STCONSTTOGLOBALRECORD_IMM16_ID16:
414 case BytecodeInstruction::Opcode::TRYLDGLOBALBYNAME_IMM8_ID16:
415 case BytecodeInstruction::Opcode::TRYLDGLOBALBYNAME_IMM16_ID16:
416 case BytecodeInstruction::Opcode::TRYSTGLOBALBYNAME_IMM8_ID16:
417 case BytecodeInstruction::Opcode::TRYSTGLOBALBYNAME_IMM16_ID16:
418 case BytecodeInstruction::Opcode::STTOGLOBALRECORD_IMM16_ID16:
419 case BytecodeInstruction::Opcode::STOWNBYNAMEWITHNAMESET_IMM8_ID16_V8:
420 case BytecodeInstruction::Opcode::STOWNBYNAMEWITHNAMESET_IMM16_ID16_V8:
421 case BytecodeInstruction::Opcode::LDTHISBYNAME_IMM8_ID16:
422 case BytecodeInstruction::Opcode::LDTHISBYNAME_IMM16_ID16:
423 case BytecodeInstruction::Opcode::STTHISBYNAME_IMM8_ID16:
424 case BytecodeInstruction::Opcode::STTHISBYNAME_IMM16_ID16:
425 case BytecodeInstruction::Opcode::LDGLOBALVAR_IMM16_ID16:
426 case BytecodeInstruction::Opcode::LDOBJBYNAME_IMM8_ID16:
427 case BytecodeInstruction::Opcode::LDOBJBYNAME_IMM16_ID16:
428 case BytecodeInstruction::Opcode::STOBJBYNAME_IMM8_ID16_V8:
429 case BytecodeInstruction::Opcode::STOBJBYNAME_IMM16_ID16_V8:
430 case BytecodeInstruction::Opcode::DEFINEFIELDBYNAME_IMM8_ID16_V8:
431 case BytecodeInstruction::Opcode::DEFINEPROPERTYBYNAME_IMM8_ID16_V8:
432 case BytecodeInstruction::Opcode::LDSUPERBYNAME_IMM8_ID16:
433 case BytecodeInstruction::Opcode::LDSUPERBYNAME_IMM16_ID16:
434 case BytecodeInstruction::Opcode::STSUPERBYNAME_IMM8_ID16_V8:
435 case BytecodeInstruction::Opcode::STSUPERBYNAME_IMM16_ID16_V8:
436 case BytecodeInstruction::Opcode::STGLOBALVAR_IMM16_ID16:
437 case BytecodeInstruction::Opcode::LDBIGINT_ID16: {
438 auto constantPoolIdx = bcIns.GetId().AsRawValue();
439 BaseSnapshotInfo::ItemData itemData = {recordName, constantPoolId, constantPoolIdx, methodOffset, bcIndex};
440 RecordInfo(Type::STRING, itemData);
441 break;
442 }
443 case BytecodeInstruction::Opcode::DEFINEFUNC_IMM8_ID16_IMM8:
444 case BytecodeInstruction::Opcode::DEFINEFUNC_IMM16_ID16_IMM8:
445 case BytecodeInstruction::Opcode::DEFINEMETHOD_IMM8_ID16_IMM8:
446 case BytecodeInstruction::Opcode::DEFINEMETHOD_IMM16_ID16_IMM8: {
447 auto constantPoolIdx = bcIns.GetId().AsRawValue();
448 BaseSnapshotInfo::ItemData itemData = {recordName, constantPoolId, constantPoolIdx, methodOffset, bcIndex};
449 RecordInfo(Type::METHOD, itemData);
450 break;
451 }
452 case BytecodeInstruction::Opcode::CREATEOBJECTWITHBUFFER_IMM8_ID16:
453 case BytecodeInstruction::Opcode::CREATEOBJECTWITHBUFFER_IMM16_ID16: {
454 auto constantPoolIdx = bcIns.GetId().AsRawValue();
455 BaseSnapshotInfo::ItemData itemData = {recordName, constantPoolId, constantPoolIdx, methodOffset, bcIndex};
456 RecordInfo(Type::OBJECT_LITERAL, itemData);
457 break;
458 }
459 case BytecodeInstruction::Opcode::CREATEARRAYWITHBUFFER_IMM8_ID16:
460 case BytecodeInstruction::Opcode::CREATEARRAYWITHBUFFER_IMM16_ID16: {
461 auto constantPoolIdx = bcIns.GetId().AsRawValue();
462 BaseSnapshotInfo::ItemData itemData = {recordName, constantPoolId, constantPoolIdx, methodOffset, bcIndex};
463 RecordInfo(Type::ARRAY_LITERAL, itemData);
464 break;
465 }
466 case BytecodeInstruction::Opcode::DEFINECLASSWITHBUFFER_IMM8_ID16_ID16_IMM16_V8: {
467 auto methodCPIdx = (bcIns.GetId <BytecodeInstruction::Format::IMM8_ID16_ID16_IMM16_V8, 0>()).AsRawValue();
468 BaseSnapshotInfo::ItemData methodItemData = {recordName, constantPoolId,
469 methodCPIdx, methodOffset, bcIndex};
470 RecordInfo(Type::METHOD, methodItemData);
471
472 auto literalCPIdx = (bcIns.GetId <BytecodeInstruction::Format::IMM8_ID16_ID16_IMM16_V8, 1>()).AsRawValue();
473 BaseSnapshotInfo::ItemData literalItemData = {recordName, constantPoolId,
474 literalCPIdx, methodOffset, bcIndex, methodCPIdx};
475 RecordInfo(Type::CLASS_LITERAL, literalItemData);
476 break;
477 }
478 case BytecodeInstruction::Opcode::DEFINECLASSWITHBUFFER_IMM16_ID16_ID16_IMM16_V8: {
479 auto methodCPIdx = (bcIns.GetId <BytecodeInstruction::Format::IMM16_ID16_ID16_IMM16_V8, 0>()).AsRawValue();
480 BaseSnapshotInfo::ItemData methodItemData = {recordName, constantPoolId,
481 methodCPIdx, methodOffset, bcIndex};
482 RecordInfo(Type::METHOD, methodItemData);
483
484 auto literalCPIdx = (bcIns.GetId <BytecodeInstruction::Format::IMM16_ID16_ID16_IMM16_V8, 1>()).AsRawValue();
485 BaseSnapshotInfo::ItemData literalItemData = {recordName, constantPoolId,
486 literalCPIdx, methodOffset, bcIndex, methodCPIdx};
487 RecordInfo(Type::CLASS_LITERAL, literalItemData);
488 break;
489 }
490 default:
491 break;
492 }
493 }
494
StoreDataToGlobalData(SnapshotGlobalData & snapshotData,const std::set<uint32_t> & skippedMethods) const495 void SnapshotConstantPoolData::StoreDataToGlobalData(SnapshotGlobalData &snapshotData,
496 const std::set<uint32_t> &skippedMethods) const
497 {
498 for (auto &info : infos_) {
499 info->StoreDataToGlobalData(snapshotData, skippedMethods);
500 }
501 }
502 } // namespace panda::ecmascript
503