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_AOT_SNAPSHOT_H
16#define ECMASCRIPT_COMPILER_AOT_SNAPSHOT_AOT_SNAPSHOT_H
17
18#include "ecmascript/ecma_vm.h"
19#include "ecmascript/object_factory.h"
20#include "ecmascript/compiler/aot_snapshot/snapshot_global_data.h"
21#include "ecmascript/compiler/bytecode_info_collector.h"
22
23namespace panda::ecmascript::kungfu {
24class AOTSnapshot {
25public:
26    explicit AOTSnapshot(EcmaVM *vm)
27        : vm_(vm), factory_(vm->GetFactory()), thread_(vm_->GetJSThread()) {}
28
29    void Iterate(const RootVisitor &v)
30    {
31        snapshotData_.Iterate(v);
32    }
33
34    JSTaggedValue GetSnapshotData() const
35    {
36        return snapshotData_.GetData();
37    }
38
39    void StoreSymbolInfo(JSHandle<TaggedArray> info)
40    {
41        snapshotData_.StoreSymbolInfo(info);
42    }
43
44    JSTaggedValue GetSymbolInfo() const
45    {
46        return snapshotData_.GetSymbolInfo();
47    }
48
49    void StoreHClassInfo(JSHandle<TaggedArray> info)
50    {
51        snapshotData_.StoreHClassInfo(info);
52    }
53
54    void StoreArrayInfo(JSHandle<TaggedArray> info)
55    {
56        snapshotData_.StoreArrayInfo(info);
57    }
58
59    void StoreProtoTransTableInfo(JSHandle<JSTaggedValue> info)
60    {
61        snapshotData_.StoreProtoTransTableInfo(info);
62    }
63
64    JSTaggedValue GetArrayInfo()
65    {
66        return snapshotData_.GetArrayInfo();
67    }
68
69    void StoreConstantIndexInfo(JSHandle<TaggedArray> info)
70    {
71        snapshotData_.StoreConstantIndexInfo(info);
72    }
73
74    void InitSnapshot(uint32_t compileFilesCount);
75
76    void PUBLIC_API StoreConstantPoolInfo(BytecodeInfoCollector *bcInfoCollector);
77
78    void ResolveSnapshotData(const CMap<std::pair<std::string, uint32_t>, uint32_t> &methodToEntryIndexMap)
79    {
80        snapshotData_.ResolveSnapshotData(thread_, methodToEntryIndexMap);
81    }
82
83private:
84    JSHandle<ConstantPool> NewSnapshotConstantPool(uint32_t cacheSize);
85
86    void GenerateSnapshotConstantPools(
87        const CMap<int32_t, JSTaggedValue> &allConstantPools, const CString &fileName, uint32_t fileIndex);
88
89    EcmaVM *vm_ {nullptr};
90    ObjectFactory *factory_ {nullptr};
91    JSThread *thread_ {nullptr};
92    SnapshotGlobalData snapshotData_ {};
93};
94}  // panda::ecmascript::kungfu
95#endif // ECMASCRIPT_COMPILER_AOT_SNAPSHOT_AOT_SNAPSHOT_H
96