14514f5e3Sopenharmony_ci/* 24514f5e3Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License. 54514f5e3Sopenharmony_ci * You may obtain a copy of the License at 64514f5e3Sopenharmony_ci * 74514f5e3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 84514f5e3Sopenharmony_ci * 94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and 134514f5e3Sopenharmony_ci * limitations under the License. 144514f5e3Sopenharmony_ci */ 154514f5e3Sopenharmony_ci 164514f5e3Sopenharmony_ci#include "ecmascript/compiler/aot_snapshot/snapshot_global_data.h" 174514f5e3Sopenharmony_ci 184514f5e3Sopenharmony_ci#include "ecmascript/compiler/aot_snapshot/aot_snapshot_constants.h" 194514f5e3Sopenharmony_ci#include "ecmascript/jspandafile/program_object.h" 204514f5e3Sopenharmony_ci 214514f5e3Sopenharmony_cinamespace panda::ecmascript::kungfu { 224514f5e3Sopenharmony_ciJSHandle<ConstantPool> ReviseData::GetConstantPoolFromSnapshotData(JSThread *thread, 234514f5e3Sopenharmony_ci const SnapshotGlobalData *globalData, 244514f5e3Sopenharmony_ci uint32_t dataIdx, uint32_t cpArrayIdx) 254514f5e3Sopenharmony_ci{ 264514f5e3Sopenharmony_ci JSHandle<TaggedArray> data(thread, globalData->GetData()); 274514f5e3Sopenharmony_ci auto cpArrayOffset = SnapshotGlobalData::Cast(SnapshotGlobalData::CP_TOP_ITEM::CP_ARRAY_ID); 284514f5e3Sopenharmony_ci JSHandle<TaggedArray> cpArr(thread, data->Get(dataIdx + cpArrayOffset)); 294514f5e3Sopenharmony_ci return JSHandle<ConstantPool>(thread, cpArr->Get(cpArrayIdx)); 304514f5e3Sopenharmony_ci} 314514f5e3Sopenharmony_ci 324514f5e3Sopenharmony_civoid ReviseData::Resolve(JSThread *thread, const SnapshotGlobalData *globalData, 334514f5e3Sopenharmony_ci const CMap<std::pair<std::string, uint32_t>, uint32_t> &methodToEntryIndexMap) 344514f5e3Sopenharmony_ci{ 354514f5e3Sopenharmony_ci for (auto &item: data_) { 364514f5e3Sopenharmony_ci JSHandle<ConstantPool> newCP = GetConstantPoolFromSnapshotData(thread, globalData, 374514f5e3Sopenharmony_ci item.dataIdx_, item.cpArrayIdx_); 384514f5e3Sopenharmony_ci 394514f5e3Sopenharmony_ci JSTaggedValue val = newCP->GetObjectFromCache(item.constpoolIdx_); 404514f5e3Sopenharmony_ci if (val.IsHole()) { 414514f5e3Sopenharmony_ci continue; 424514f5e3Sopenharmony_ci } 434514f5e3Sopenharmony_ci std::string name = globalData->GetFileNameByDataIdx(item.dataIdx_).c_str(); 444514f5e3Sopenharmony_ci // For MethodSnaphotInfo which does not have ihc info, we insert JSTaggedValue(methodOffset) as revervation. 454514f5e3Sopenharmony_ci // We need to set JSTaggedValue(codeEntry); to replace JSTaggedValue(methodOffset). 464514f5e3Sopenharmony_ci if (val.IsInt()) { 474514f5e3Sopenharmony_ci if (val.GetInt() == static_cast<int>(AOTLiteralInfo::NO_FUNC_ENTRY_VALUE)) { 484514f5e3Sopenharmony_ci continue; 494514f5e3Sopenharmony_ci } 504514f5e3Sopenharmony_ci uint32_t methodOffset = static_cast<uint32_t>(val.GetInt()); 514514f5e3Sopenharmony_ci if (thread->GetEcmaVM()->GetJSOptions().IsEnableCompilerLogSnapshot()) { 524514f5e3Sopenharmony_ci LOG_COMPILER(INFO) << "[aot-snapshot] store AOT entry index of method (offset: " 534514f5e3Sopenharmony_ci << methodOffset << ") "; 544514f5e3Sopenharmony_ci } 554514f5e3Sopenharmony_ci AnFileInfo::FuncEntryIndexKey key = std::make_pair(name, methodOffset); 564514f5e3Sopenharmony_ci uint32_t entryIndex = methodToEntryIndexMap.at(key); 574514f5e3Sopenharmony_ci newCP->SetObjectToCache(thread, item.constpoolIdx_, JSTaggedValue(entryIndex)); 584514f5e3Sopenharmony_ci continue; 594514f5e3Sopenharmony_ci } 604514f5e3Sopenharmony_ci AOTLiteralInfo *aotLiteralInfo = AOTLiteralInfo::Cast(val.GetTaggedObject()); 614514f5e3Sopenharmony_ci uint32_t aotLiteralInfoLen = aotLiteralInfo->GetCacheLength(); 624514f5e3Sopenharmony_ci for (uint32_t i = 0; i < aotLiteralInfoLen; ++i) { 634514f5e3Sopenharmony_ci JSTaggedValue methodOffsetVal = aotLiteralInfo->GetObjectFromCache(i); 644514f5e3Sopenharmony_ci if (methodOffsetVal.GetInt() == static_cast<int>(AOTLiteralInfo::NO_FUNC_ENTRY_VALUE)) { 654514f5e3Sopenharmony_ci continue; 664514f5e3Sopenharmony_ci } 674514f5e3Sopenharmony_ci uint32_t methodOffset = static_cast<uint32_t>(methodOffsetVal.GetInt()); 684514f5e3Sopenharmony_ci if (thread->GetEcmaVM()->GetJSOptions().IsEnableCompilerLogSnapshot()) { 694514f5e3Sopenharmony_ci LOG_COMPILER(INFO) << "[aot-snapshot] store AOT entry index of method (offset: " 704514f5e3Sopenharmony_ci << methodOffset << ") "; 714514f5e3Sopenharmony_ci } 724514f5e3Sopenharmony_ci AnFileInfo::FuncEntryIndexKey key = std::make_pair(name, methodOffset); 734514f5e3Sopenharmony_ci uint32_t entryIndex = methodToEntryIndexMap.at(key); 744514f5e3Sopenharmony_ci aotLiteralInfo->SetObjectToCache(thread, i, JSTaggedValue(entryIndex)); 754514f5e3Sopenharmony_ci } 764514f5e3Sopenharmony_ci } 774514f5e3Sopenharmony_ci} 784514f5e3Sopenharmony_ci 794514f5e3Sopenharmony_civoid SnapshotGlobalData::AddSnapshotCpArrayToData(JSThread *thread, CString fileName, uint32_t fileIndex, 804514f5e3Sopenharmony_ci JSHandle<TaggedArray> snapshotCpArray) 814514f5e3Sopenharmony_ci{ 824514f5e3Sopenharmony_ci if (isFirstData_) { 834514f5e3Sopenharmony_ci isFirstData_ = false; 844514f5e3Sopenharmony_ci } else { 854514f5e3Sopenharmony_ci curDataIdx_ += AOTSnapshotConstants::SNAPSHOT_DATA_ITEM_SIZE; 864514f5e3Sopenharmony_ci } 874514f5e3Sopenharmony_ci // handle file info 884514f5e3Sopenharmony_ci JSHandle<EcmaString> nameStr = thread->GetEcmaVM()->GetFactory()->NewFromStdString(fileName.c_str()); 894514f5e3Sopenharmony_ci auto fileInfo = thread->GetEcmaVM()->GetFactory()->NewTaggedArray(Cast(CP_PANDA_INFO_ITEM::COUNT)); 904514f5e3Sopenharmony_ci fileInfo->Set(thread, Cast(CP_PANDA_INFO_ITEM::NAME_ID), nameStr); 914514f5e3Sopenharmony_ci fileInfo->Set(thread, Cast(CP_PANDA_INFO_ITEM::INDEX_ID), JSTaggedValue(fileIndex)); 924514f5e3Sopenharmony_ci 934514f5e3Sopenharmony_ci JSHandle<TaggedArray> dataHandle(thread, data_); 944514f5e3Sopenharmony_ci dataHandle->Set(thread, curDataIdx_ + Cast(CP_TOP_ITEM::PANDA_INFO_ID), fileInfo); 954514f5e3Sopenharmony_ci 964514f5e3Sopenharmony_ci // handle constant pool 974514f5e3Sopenharmony_ci curSnapshotCpArray_ = snapshotCpArray.GetTaggedValue(); 984514f5e3Sopenharmony_ci dataHandle->Set(thread, curDataIdx_ + Cast(CP_TOP_ITEM::CP_ARRAY_ID), curSnapshotCpArray_); 994514f5e3Sopenharmony_ci dataIdxToFileNameMap_[curDataIdx_] = fileName; 1004514f5e3Sopenharmony_ci} 1014514f5e3Sopenharmony_ci 1024514f5e3Sopenharmony_ciCString SnapshotGlobalData::GetFileNameByDataIdx(uint32_t dataIdx) const 1034514f5e3Sopenharmony_ci{ 1044514f5e3Sopenharmony_ci auto it = dataIdxToFileNameMap_.find(dataIdx); 1054514f5e3Sopenharmony_ci if (it != dataIdxToFileNameMap_.end()) { 1064514f5e3Sopenharmony_ci return it->second; 1074514f5e3Sopenharmony_ci } 1084514f5e3Sopenharmony_ci LOG_COMPILER(FATAL) << "Can't find snapshot data by index '" << dataIdx << "'"; 1094514f5e3Sopenharmony_ci UNREACHABLE(); 1104514f5e3Sopenharmony_ci} 1114514f5e3Sopenharmony_ci} // namespace panda::ecmascript 112