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#ifndef ECMASCRIPT_COMPILER_AOT_SNAPSHOT_AOT_SNAPSHOT_DATA_H
164514f5e3Sopenharmony_ci#define ECMASCRIPT_COMPILER_AOT_SNAPSHOT_AOT_SNAPSHOT_DATA_H
174514f5e3Sopenharmony_ci
184514f5e3Sopenharmony_ci#include "ecmascript/compiler/aot_snapshot/snapshot_global_data.h"
194514f5e3Sopenharmony_ci#include "ecmascript/compiler/pgo_type/pgo_type_location.h"
204514f5e3Sopenharmony_ci#include "ecmascript/jspandafile/js_pandafile.h"
214514f5e3Sopenharmony_ci#include "ecmascript/mem/c_containers.h"
224514f5e3Sopenharmony_ci#include "ecmascript/pgo_profiler/pgo_profiler_decoder.h"
234514f5e3Sopenharmony_ci#include "libpandafile/bytecode_instruction.h"
244514f5e3Sopenharmony_ci
254514f5e3Sopenharmony_cinamespace panda::ecmascript::kungfu {
264514f5e3Sopenharmony_ciusing ApEntityId = pgo::ApEntityId;
274514f5e3Sopenharmony_ciusing PGOProfilerDecoder = pgo::PGOProfilerDecoder;
284514f5e3Sopenharmony_ci
294514f5e3Sopenharmony_ci#define DATA_TYPE_LIST(V)                     \
304514f5e3Sopenharmony_ci    V(STRING, StringSnapshot)                 \
314514f5e3Sopenharmony_ci    V(METHOD, MethodSnapshot)                 \
324514f5e3Sopenharmony_ci    V(CLASS_LITERAL, ClassLiteralSnapshot)    \
334514f5e3Sopenharmony_ci    V(OBJECT_LITERAL, ObjectLiteralSnapshot)  \
344514f5e3Sopenharmony_ci    V(ARRAY_LITERAL, ArrayLiteralSnapshot)
354514f5e3Sopenharmony_ci
364514f5e3Sopenharmony_ciclass BaseSnapshotInfo {
374514f5e3Sopenharmony_cipublic:
384514f5e3Sopenharmony_ci    struct ItemData {
394514f5e3Sopenharmony_ci        CString recordName_;
404514f5e3Sopenharmony_ci        int32_t constantPoolId_ {0};
414514f5e3Sopenharmony_ci        uint32_t constantPoolIdx_ {0};
424514f5e3Sopenharmony_ci        uint32_t methodOffset_ {0};
434514f5e3Sopenharmony_ci        uint32_t bcIndex_ {0};
444514f5e3Sopenharmony_ci        uint32_t ctorMethodOffset_ {0}; // class constructor
454514f5e3Sopenharmony_ci    };
464514f5e3Sopenharmony_ci
474514f5e3Sopenharmony_ci    BaseSnapshotInfo(EcmaVM *vm,
484514f5e3Sopenharmony_ci                     const JSPandaFile *jsPandaFile,
494514f5e3Sopenharmony_ci                     const PGOProfilerDecoder *pfDecoder)
504514f5e3Sopenharmony_ci        : vm_(vm),
514514f5e3Sopenharmony_ci          thread_(vm->GetJSThread()),
524514f5e3Sopenharmony_ci          jsPandaFile_(jsPandaFile),
534514f5e3Sopenharmony_ci          pfDecoder_(pfDecoder)
544514f5e3Sopenharmony_ci    {}
554514f5e3Sopenharmony_ci
564514f5e3Sopenharmony_ci    virtual ~BaseSnapshotInfo() = default;
574514f5e3Sopenharmony_ci
584514f5e3Sopenharmony_ci    virtual void StoreDataToGlobalData(SnapshotGlobalData &globalData, const std::set<uint32_t> &skippedMethods) = 0;
594514f5e3Sopenharmony_ci
604514f5e3Sopenharmony_ci    void Record(ItemData &data);
614514f5e3Sopenharmony_ci
624514f5e3Sopenharmony_ciprotected:
634514f5e3Sopenharmony_ci    using ItemKey = uint64_t;
644514f5e3Sopenharmony_ci
654514f5e3Sopenharmony_ci    static constexpr uint32_t CONSTPOOL_MASK = 32;
664514f5e3Sopenharmony_ci
674514f5e3Sopenharmony_ci    static ItemKey GetItemKey(uint32_t constantPoolId, uint32_t constantPoolIdx);
684514f5e3Sopenharmony_ci
694514f5e3Sopenharmony_ci    bool TryGetABCId(ApEntityId &abcId);
704514f5e3Sopenharmony_ci
714514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> TryGetIHClass(ProfileType rootType, ProfileType childType, const ItemData &data,
724514f5e3Sopenharmony_ci        const JSHandle<TaggedArray> &properties, const SnapshotGlobalData &globalData) const;
734514f5e3Sopenharmony_ci
744514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> TryGetHClass(ProfileType rootType, ProfileType childType) const;
754514f5e3Sopenharmony_ci
764514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> TryGetHClassByPGOTypeLocation(PGOTypeLocation loc) const;
774514f5e3Sopenharmony_ci
784514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> TryGetHClassFromCached(const JSHandle<TaggedArray> &properties,
794514f5e3Sopenharmony_ci                                                   const SnapshotGlobalData &globalData) const;
804514f5e3Sopenharmony_ci
814514f5e3Sopenharmony_ci    void CollectLiteralInfo(JSHandle<TaggedArray> array, uint32_t constantPoolIndex,
824514f5e3Sopenharmony_ci                            JSHandle<ConstantPool> snapshotConstantPool, const std::set<uint32_t> &skippedMethods,
834514f5e3Sopenharmony_ci                            JSHandle<JSTaggedValue> ihc, JSHandle<JSTaggedValue> chc);
844514f5e3Sopenharmony_ci    JSHandle<ConstantPool> GetUnsharedConstpool(const ItemData &data);
854514f5e3Sopenharmony_ci
864514f5e3Sopenharmony_ci    static bool CheckAOTPropertiesForRep(const JSHandle<TaggedArray> &properties, const JSHandle<JSHClass> &hclass);
874514f5e3Sopenharmony_ci
884514f5e3Sopenharmony_ci    static bool CheckAOTIhcPropertiesForRep(JSThread *thread, const JSHandle<JSTaggedValue> &ihc,
894514f5e3Sopenharmony_ci                                            const JSHandle<ClassInfoExtractor> &extractor);
904514f5e3Sopenharmony_ci
914514f5e3Sopenharmony_ci    static bool CheckAOTChcPropertiesForRep(JSThread *thread, const JSHandle<JSTaggedValue> &chc,
924514f5e3Sopenharmony_ci                                            const JSHandle<ClassInfoExtractor> &extractor);
934514f5e3Sopenharmony_ci
944514f5e3Sopenharmony_ci    CUnorderedMap<ItemKey, ItemData> info_ {};
954514f5e3Sopenharmony_ci    EcmaVM *vm_ {nullptr};
964514f5e3Sopenharmony_ci    JSThread *thread_ {nullptr};
974514f5e3Sopenharmony_ci    const JSPandaFile *jsPandaFile_ {nullptr};
984514f5e3Sopenharmony_ci    const PGOProfilerDecoder *pfDecoder_ {nullptr};
994514f5e3Sopenharmony_ci};
1004514f5e3Sopenharmony_ci
1014514f5e3Sopenharmony_ci#define DEFINE_INFO_CLASS(V, name)                                          \
1024514f5e3Sopenharmony_ci    class PUBLIC_API name##Info final : public BaseSnapshotInfo {           \
1034514f5e3Sopenharmony_ci    public:                                                                 \
1044514f5e3Sopenharmony_ci        name##Info(EcmaVM *vm,                                              \
1054514f5e3Sopenharmony_ci                   const JSPandaFile *jsPandaFile,                          \
1064514f5e3Sopenharmony_ci                   const PGOProfilerDecoder *pfDecoder)                     \
1074514f5e3Sopenharmony_ci        : BaseSnapshotInfo(vm, jsPandaFile, pfDecoder) {}                   \
1084514f5e3Sopenharmony_ci                                                                            \
1094514f5e3Sopenharmony_ci        virtual void StoreDataToGlobalData(SnapshotGlobalData &globalData,  \
1104514f5e3Sopenharmony_ci            const std::set<uint32_t> &skippedMethods) override;             \
1114514f5e3Sopenharmony_ci    };
1124514f5e3Sopenharmony_ci
1134514f5e3Sopenharmony_ci    DATA_TYPE_LIST(DEFINE_INFO_CLASS)
1144514f5e3Sopenharmony_ci#undef DEFINE_INFO_CLASS
1154514f5e3Sopenharmony_ci
1164514f5e3Sopenharmony_ciclass SnapshotConstantPoolData {
1174514f5e3Sopenharmony_cipublic:
1184514f5e3Sopenharmony_ci    SnapshotConstantPoolData(EcmaVM *vm, const JSPandaFile *jsPandaFile, const PGOProfilerDecoder *pfDecoder)
1194514f5e3Sopenharmony_ci        : jsPandaFile_(jsPandaFile)
1204514f5e3Sopenharmony_ci    {
1214514f5e3Sopenharmony_ci#define ADD_INFO(V, name)                               \
1224514f5e3Sopenharmony_ci    infos_.emplace_back(std::make_unique<name##Info>(vm, jsPandaFile, pfDecoder));
1234514f5e3Sopenharmony_ci    DATA_TYPE_LIST(ADD_INFO)
1244514f5e3Sopenharmony_ci#undef ADD_INFO
1254514f5e3Sopenharmony_ci    }
1264514f5e3Sopenharmony_ci    ~SnapshotConstantPoolData() = default;
1274514f5e3Sopenharmony_ci
1284514f5e3Sopenharmony_ci    void PUBLIC_API Record(const BytecodeInstruction &bcIns, int32_t bcIndex,
1294514f5e3Sopenharmony_ci                           const CString &recordName, const MethodLiteral *method);
1304514f5e3Sopenharmony_ci
1314514f5e3Sopenharmony_ci    void StoreDataToGlobalData(SnapshotGlobalData &snapshotData, const std::set<uint32_t> &skippedMethods) const;
1324514f5e3Sopenharmony_ci
1334514f5e3Sopenharmony_ciprivate:
1344514f5e3Sopenharmony_ci    enum class Type {
1354514f5e3Sopenharmony_ci#define DEFINE_TYPE(type, ...) type,
1364514f5e3Sopenharmony_ci    DATA_TYPE_LIST(DEFINE_TYPE)
1374514f5e3Sopenharmony_ci#undef DEFINE_TYPE
1384514f5e3Sopenharmony_ci    };
1394514f5e3Sopenharmony_ci
1404514f5e3Sopenharmony_ci    void RecordInfo(Type type, BaseSnapshotInfo::ItemData &itemData)
1414514f5e3Sopenharmony_ci    {
1424514f5e3Sopenharmony_ci        size_t infoIdx = static_cast<size_t>(type);
1434514f5e3Sopenharmony_ci        infos_.at(infoIdx)->Record(itemData);
1444514f5e3Sopenharmony_ci    }
1454514f5e3Sopenharmony_ci
1464514f5e3Sopenharmony_ci    const JSPandaFile *jsPandaFile_;
1474514f5e3Sopenharmony_ci    CVector<std::unique_ptr<BaseSnapshotInfo>> infos_ {};
1484514f5e3Sopenharmony_ci};
1494514f5e3Sopenharmony_ci#undef DATA_TYPE_LIST
1504514f5e3Sopenharmony_ci}  // panda::ecmascript::kungfu
1514514f5e3Sopenharmony_ci#endif // ECMASCRIPT_COMPILER_AOT_SNAPSHOT_AOT_SNAPSHOT_DATA_H
1524514f5e3Sopenharmony_ci
153