14514f5e3Sopenharmony_ci/* 24514f5e3Sopenharmony_ci * Copyright (c) 2023-2024 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/aot_snapshot.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_civoid AOTSnapshot::InitSnapshot(uint32_t compileFilesCount) 234514f5e3Sopenharmony_ci{ 244514f5e3Sopenharmony_ci JSHandle<TaggedArray> data = factory_->NewTaggedArray(compileFilesCount * 254514f5e3Sopenharmony_ci AOTSnapshotConstants::SNAPSHOT_DATA_ITEM_SIZE); 264514f5e3Sopenharmony_ci snapshotData_.SetData(data.GetTaggedValue()); 274514f5e3Sopenharmony_ci} 284514f5e3Sopenharmony_ci 294514f5e3Sopenharmony_ciJSHandle<ConstantPool> AOTSnapshot::NewSnapshotConstantPool(uint32_t cacheSize) 304514f5e3Sopenharmony_ci{ 314514f5e3Sopenharmony_ci JSHandle<ConstantPool> cp = factory_->NewConstantPool(cacheSize); 324514f5e3Sopenharmony_ci 334514f5e3Sopenharmony_ci ASSERT(!snapshotData_.GetHClassInfo().IsHole()); 344514f5e3Sopenharmony_ci cp->SetAotSymbolInfo(vm_->GetJSThread(), snapshotData_.GetSymbolInfo()); 354514f5e3Sopenharmony_ci cp->SetAotHClassInfo(vm_->GetJSThread(), snapshotData_.GetHClassInfo()); 364514f5e3Sopenharmony_ci cp->SetAotArrayInfo(vm_->GetJSThread(), snapshotData_.GetArrayInfo()); 374514f5e3Sopenharmony_ci cp->SetConstantIndexInfo(vm_->GetJSThread(), snapshotData_.GetConstantIndexInfo()); 384514f5e3Sopenharmony_ci cp->SetProtoTransTableInfo(vm_->GetJSThread(), snapshotData_.GetProtoTransTableInfo()); 394514f5e3Sopenharmony_ci return cp; 404514f5e3Sopenharmony_ci} 414514f5e3Sopenharmony_ci 424514f5e3Sopenharmony_civoid AOTSnapshot::GenerateSnapshotConstantPools(const CMap<int32_t, JSTaggedValue> &allConstantPools, 434514f5e3Sopenharmony_ci const CString &fileName, uint32_t fileIndex) 444514f5e3Sopenharmony_ci{ 454514f5e3Sopenharmony_ci JSHandle<TaggedArray> snapshotCpArr = factory_->NewTaggedArray(allConstantPools.size() * 464514f5e3Sopenharmony_ci AOTSnapshotConstants::SNAPSHOT_CP_ARRAY_ITEM_SIZE); 474514f5e3Sopenharmony_ci snapshotData_.AddSnapshotCpArrayToData(thread_, fileName, fileIndex, snapshotCpArr); 484514f5e3Sopenharmony_ci 494514f5e3Sopenharmony_ci JSMutableHandle<ConstantPool> cp(thread_, thread_->GlobalConstants()->GetUndefined()); 504514f5e3Sopenharmony_ci uint32_t pos = 0; 514514f5e3Sopenharmony_ci for (auto &iter : allConstantPools) { 524514f5e3Sopenharmony_ci int32_t cpId = iter.first; 534514f5e3Sopenharmony_ci cp.Update(iter.second); 544514f5e3Sopenharmony_ci // cachedSize should not have extra data included 554514f5e3Sopenharmony_ci uint32_t cacheSize = cp->GetCacheLength() - ConstantPool::EXTEND_DATA_NUM; 564514f5e3Sopenharmony_ci if (vm_->GetJSOptions().IsEnableCompilerLogSnapshot()) { 574514f5e3Sopenharmony_ci LOG_COMPILER(INFO) << "[aot-snapshot] constantPoolID: " << cpId; 584514f5e3Sopenharmony_ci LOG_COMPILER(INFO) << "[aot-snapshot] cacheSize: " << cacheSize; 594514f5e3Sopenharmony_ci } 604514f5e3Sopenharmony_ci 614514f5e3Sopenharmony_ci JSHandle<ConstantPool> newCp = NewSnapshotConstantPool(cacheSize); 624514f5e3Sopenharmony_ci snapshotCpArr->Set(thread_, pos++, JSTaggedValue(cpId)); 634514f5e3Sopenharmony_ci snapshotData_.RecordCpArrIdx(cpId, pos); 644514f5e3Sopenharmony_ci snapshotCpArr->Set(thread_, pos++, newCp.GetTaggedValue()); 654514f5e3Sopenharmony_ci } 664514f5e3Sopenharmony_ci} 674514f5e3Sopenharmony_ci 684514f5e3Sopenharmony_civoid AOTSnapshot::StoreConstantPoolInfo(BytecodeInfoCollector *bcInfoCollector) 694514f5e3Sopenharmony_ci{ 704514f5e3Sopenharmony_ci const JSPandaFile *jsPandaFile = bcInfoCollector->GetJSPandaFile(); 714514f5e3Sopenharmony_ci const CMap<int32_t, JSTaggedValue> &allConstantPools = vm_->GetJSThread()-> 724514f5e3Sopenharmony_ci GetCurrentEcmaContext()->FindConstpools(jsPandaFile).value(); 734514f5e3Sopenharmony_ci pgo::ApEntityId fileId = INVALID_INDEX; 744514f5e3Sopenharmony_ci if (!pgo::PGOProfilerManager::GetInstance()->GetPandaFileId(jsPandaFile->GetJSPandaFileDesc(), fileId)) { 754514f5e3Sopenharmony_ci LOG_COMPILER(ERROR) << "StoreConstantPoolInfo failed. no file id found for " 764514f5e3Sopenharmony_ci << jsPandaFile->GetJSPandaFileDesc(); 774514f5e3Sopenharmony_ci return; 784514f5e3Sopenharmony_ci } 794514f5e3Sopenharmony_ci GenerateSnapshotConstantPools(allConstantPools, jsPandaFile->GetNormalizedFileDesc(), fileId); 804514f5e3Sopenharmony_ci bcInfoCollector->StoreDataToGlobalData(snapshotData_); 814514f5e3Sopenharmony_ci} 824514f5e3Sopenharmony_ci} // namespace panda::ecmascript 83