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