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/aot_snapshot.h" 17 18#include "ecmascript/compiler/aot_snapshot/aot_snapshot_constants.h" 19#include "ecmascript/jspandafile/program_object.h" 20 21namespace panda::ecmascript::kungfu { 22void AOTSnapshot::InitSnapshot(uint32_t compileFilesCount) 23{ 24 JSHandle<TaggedArray> data = factory_->NewTaggedArray(compileFilesCount * 25 AOTSnapshotConstants::SNAPSHOT_DATA_ITEM_SIZE); 26 snapshotData_.SetData(data.GetTaggedValue()); 27} 28 29JSHandle<ConstantPool> AOTSnapshot::NewSnapshotConstantPool(uint32_t cacheSize) 30{ 31 JSHandle<ConstantPool> cp = factory_->NewConstantPool(cacheSize); 32 33 ASSERT(!snapshotData_.GetHClassInfo().IsHole()); 34 cp->SetAotSymbolInfo(vm_->GetJSThread(), snapshotData_.GetSymbolInfo()); 35 cp->SetAotHClassInfo(vm_->GetJSThread(), snapshotData_.GetHClassInfo()); 36 cp->SetAotArrayInfo(vm_->GetJSThread(), snapshotData_.GetArrayInfo()); 37 cp->SetConstantIndexInfo(vm_->GetJSThread(), snapshotData_.GetConstantIndexInfo()); 38 cp->SetProtoTransTableInfo(vm_->GetJSThread(), snapshotData_.GetProtoTransTableInfo()); 39 return cp; 40} 41 42void AOTSnapshot::GenerateSnapshotConstantPools(const CMap<int32_t, JSTaggedValue> &allConstantPools, 43 const CString &fileName, uint32_t fileIndex) 44{ 45 JSHandle<TaggedArray> snapshotCpArr = factory_->NewTaggedArray(allConstantPools.size() * 46 AOTSnapshotConstants::SNAPSHOT_CP_ARRAY_ITEM_SIZE); 47 snapshotData_.AddSnapshotCpArrayToData(thread_, fileName, fileIndex, snapshotCpArr); 48 49 JSMutableHandle<ConstantPool> cp(thread_, thread_->GlobalConstants()->GetUndefined()); 50 uint32_t pos = 0; 51 for (auto &iter : allConstantPools) { 52 int32_t cpId = iter.first; 53 cp.Update(iter.second); 54 // cachedSize should not have extra data included 55 uint32_t cacheSize = cp->GetCacheLength() - ConstantPool::EXTEND_DATA_NUM; 56 if (vm_->GetJSOptions().IsEnableCompilerLogSnapshot()) { 57 LOG_COMPILER(INFO) << "[aot-snapshot] constantPoolID: " << cpId; 58 LOG_COMPILER(INFO) << "[aot-snapshot] cacheSize: " << cacheSize; 59 } 60 61 JSHandle<ConstantPool> newCp = NewSnapshotConstantPool(cacheSize); 62 snapshotCpArr->Set(thread_, pos++, JSTaggedValue(cpId)); 63 snapshotData_.RecordCpArrIdx(cpId, pos); 64 snapshotCpArr->Set(thread_, pos++, newCp.GetTaggedValue()); 65 } 66} 67 68void AOTSnapshot::StoreConstantPoolInfo(BytecodeInfoCollector *bcInfoCollector) 69{ 70 const JSPandaFile *jsPandaFile = bcInfoCollector->GetJSPandaFile(); 71 const CMap<int32_t, JSTaggedValue> &allConstantPools = vm_->GetJSThread()-> 72 GetCurrentEcmaContext()->FindConstpools(jsPandaFile).value(); 73 pgo::ApEntityId fileId = INVALID_INDEX; 74 if (!pgo::PGOProfilerManager::GetInstance()->GetPandaFileId(jsPandaFile->GetJSPandaFileDesc(), fileId)) { 75 LOG_COMPILER(ERROR) << "StoreConstantPoolInfo failed. no file id found for " 76 << jsPandaFile->GetJSPandaFileDesc(); 77 return; 78 } 79 GenerateSnapshotConstantPools(allConstantPools, jsPandaFile->GetNormalizedFileDesc(), fileId); 80 bcInfoCollector->StoreDataToGlobalData(snapshotData_); 81} 82} // namespace panda::ecmascript 83