• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef ECMASCRIPT_COMPILER_AOT_SNAPSHOT_SNAPSHOT_GLOBAL_DATA_H
16 #define ECMASCRIPT_COMPILER_AOT_SNAPSHOT_SNAPSHOT_GLOBAL_DATA_H
17 
18 #include "ecmascript/ecma_vm.h"
19 #include "ecmascript/object_factory.h"
20 
21 namespace panda::ecmascript::kungfu {
22 class SnapshotGlobalData;
23 /*
24  * The information that needs to be revised before saving the 'ai' file is recorded in SnapshotReviseData.
25  * Currently, the revised information includes the entry index of each method in the 'an' file.
26  */
27 class ReviseData {
28 public:
29     struct ItemData {
30         uint32_t dataIdx_;
31         uint32_t cpArrayIdx_;
32         int32_t constpoolIdx_;
33     };
34 
35     ReviseData() = default;
36     virtual ~ReviseData() = default;
37 
Record(ItemData data)38     void Record(ItemData data)
39     {
40         data_.emplace_back(data);
41     }
42 
43     void Resolve(JSThread *thread, const SnapshotGlobalData *globalData,
44                  const CMap<std::pair<std::string, uint32_t>, uint32_t> &methodToEntryIndexMap);
45 
46 protected:
47     JSHandle<ConstantPool> GetConstantPoolFromSnapshotData(JSThread *thread, const SnapshotGlobalData *globalData,
48                                                            uint32_t dataIdx, uint32_t cpArrayIdx);
49     std::vector<ItemData> data_;
50 };
51 
52 class SnapshotReviseInfo {
53 public:
54     SnapshotReviseInfo() = default;
55     ~SnapshotReviseInfo() = default;
56 
Record(ReviseData::ItemData data)57     void Record(ReviseData::ItemData data)
58     {
59         reviseData_.Record(data);
60     }
61 
ResolveData(JSThread * thread,const SnapshotGlobalData * globalData,const CMap<std::pair<std::string,uint32_t>,uint32_t> & methodToEntryIndexMap)62     void ResolveData(JSThread *thread, const SnapshotGlobalData *globalData,
63                      const CMap<std::pair<std::string, uint32_t>, uint32_t> &methodToEntryIndexMap)
64     {
65         reviseData_.Resolve(thread, globalData, methodToEntryIndexMap);
66     }
67 
68 private:
69     ReviseData reviseData_ {};
70 };
71 
72 class SnapshotGlobalData {
73 public:
74     // top level specified field
75     enum class CP_TOP_ITEM : int8_t {
76         PANDA_INFO_ID = 0,
77         CP_ARRAY_ID,
78         COUNT
79     };
80 
81     // file specified field
82     enum class CP_PANDA_INFO_ITEM : int8_t {
83         NAME_ID = 0,
84         INDEX_ID,
85         COUNT
86     };
87 
Cast(CP_TOP_ITEM value)88     static int8_t Cast(CP_TOP_ITEM value)
89     {
90         return static_cast<int8_t>(value);
91     }
92 
Cast(CP_PANDA_INFO_ITEM value)93     static int8_t Cast(CP_PANDA_INFO_ITEM value)
94     {
95         return static_cast<int8_t>(value);
96     }
97 
98     SnapshotGlobalData() = default;
99     ~SnapshotGlobalData() = default;
100 
Iterate(const RootVisitor & v)101     void Iterate(const RootVisitor &v)
102     {
103         v(Root::ROOT_VM, ObjectSlot(reinterpret_cast<uintptr_t>(&data_)));
104         v(Root::ROOT_VM, ObjectSlot(reinterpret_cast<uintptr_t>(&curSnapshotCpArray_)));
105         v(Root::ROOT_VM, ObjectSlot(reinterpret_cast<uintptr_t>(&hclassInfo_)));
106         v(Root::ROOT_VM, ObjectSlot(reinterpret_cast<uintptr_t>(&arrayInfo_)));
107         v(Root::ROOT_VM, ObjectSlot(reinterpret_cast<uintptr_t>(&constantIndexInfo_)));
108     }
109 
SetData(JSTaggedValue data)110     void SetData(JSTaggedValue data)
111     {
112         data_ = data;
113     }
114 
GetData()115     JSTaggedValue GetData() const
116     {
117         return data_;
118     }
119 
GetCurDataIdx()120     uint32_t GetCurDataIdx() const
121     {
122         return curDataIdx_;
123     }
124 
GetCurSnapshotCpArray()125     JSTaggedValue GetCurSnapshotCpArray() const
126     {
127         return curSnapshotCpArray_;
128     }
129 
130     void AddSnapshotCpArrayToData(JSThread *thread, CString fileName, uint32_t fileIndex,
131                                   JSHandle<TaggedArray> snapshotCpArray);
132 
133     CString GetFileNameByDataIdx(uint32_t dataIdx) const;
134 
RecordReviseData(ReviseData::ItemData data)135     void RecordReviseData(ReviseData::ItemData data)
136     {
137         reviseInfo_.Record(data);
138     }
139 
ResolveSnapshotData(JSThread * thread,const CMap<std::pair<std::string,uint32_t>,uint32_t> & methodToEntryIndexMap)140     void ResolveSnapshotData(JSThread *thread,
141                              const CMap<std::pair<std::string, uint32_t>, uint32_t> &methodToEntryIndexMap)
142     {
143         reviseInfo_.ResolveData(thread, this, methodToEntryIndexMap);
144     }
145 
RecordCpArrIdx(int32_t constantPoolId,uint32_t cpArrIdx)146     void RecordCpArrIdx(int32_t constantPoolId, uint32_t cpArrIdx)
147     {
148         dataIdxToCpArrIdxMap_[curDataIdx_][constantPoolId] = cpArrIdx;
149     }
150 
GetCpArrIdxByConstanPoolId(int32_t constantPoolId)151     uint32_t GetCpArrIdxByConstanPoolId(int32_t constantPoolId)
152     {
153         return GetCpIdToCpArrIdxMap().at(constantPoolId);
154     }
155 
GetCpIdToCpArrIdxMap()156     const CUnorderedMap<int32_t, uint32_t>& GetCpIdToCpArrIdxMap()
157     {
158         return dataIdxToCpArrIdxMap_.at(curDataIdx_);
159     }
160 
GetHClassInfo()161     JSTaggedValue GetHClassInfo()
162     {
163         return hclassInfo_;
164     }
165 
GetArrayInfo()166     JSTaggedValue GetArrayInfo()
167     {
168         return arrayInfo_;
169     }
170 
GetConstantIndexInfo()171     JSTaggedValue GetConstantIndexInfo()
172     {
173         return constantIndexInfo_;
174     }
175 
StoreHClassInfo(JSHandle<TaggedArray> info)176     void StoreHClassInfo(JSHandle<TaggedArray> info)
177     {
178         hclassInfo_ = info.GetTaggedValue();
179     }
180 
StoreArrayInfo(JSHandle<TaggedArray> info)181     void StoreArrayInfo(JSHandle<TaggedArray> info)
182     {
183         arrayInfo_ = info.GetTaggedValue();
184     }
185 
StoreConstantIndexInfo(JSHandle<TaggedArray> info)186     void StoreConstantIndexInfo(JSHandle<TaggedArray> info)
187     {
188         constantIndexInfo_ = info.GetTaggedValue();
189     }
190 
191 private:
192     using CpIdToCpArrIdxMap = CUnorderedMap<int32_t, uint32_t>;
193 
194     bool isFirstData_ {true};
195     uint32_t curDataIdx_ {0};
196     JSTaggedValue data_ {JSTaggedValue::Hole()};
197     JSTaggedValue curSnapshotCpArray_ {JSTaggedValue::Hole()};
198     CUnorderedMap<uint32_t, CpIdToCpArrIdxMap> dataIdxToCpArrIdxMap_;
199     CUnorderedMap<uint32_t, CString> dataIdxToFileNameMap_ {};
200 
201     SnapshotReviseInfo reviseInfo_;
202     JSTaggedValue hclassInfo_ {JSTaggedValue::Hole()};
203     JSTaggedValue arrayInfo_ {JSTaggedValue::Hole()};
204     JSTaggedValue constantIndexInfo_ {JSTaggedValue::Hole()};
205 };
206 }  // panda::ecmascript::kungfu
207 #endif // ECMASCRIPT_COMPILER_AOT_SNAPSHOT_SNAPSHOT_GLOBAL_DATA_H
208