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_FILE_AN_FILE_INFO_H
16#define ECMASCRIPT_COMPILER_AOT_FILE_AN_FILE_INFO_H
17
18#include "ecmascript/compiler/aot_file/aot_file_info.h"
19#include "ecmascript/compiler/assembler/assembler.h"
20
21namespace panda::ecmascript {
22struct MainFuncEntry {
23    uint64_t mainEntry {0};
24    int32_t fpDelta {0};
25    bool isFastCall {false};
26};
27
28class PUBLIC_API AnFileInfo : public AOTFileInfo {
29public:
30    using FuncEntryIndexKey = std::pair<std::string, uint32_t>; // (compilefileName, MethodID)
31    AnFileInfo() = default;
32    ~AnFileInfo() override = default;
33    bool PUBLIC_API Save(const std::string &filename, Triple triple);
34    void AddModuleDes(ModuleSectionDes &moduleDes)
35    {
36        des_.emplace_back(moduleDes);
37        for (auto &s : moduleDes.GetSectionsInfo()) {
38            auto sec = ElfSection(s.first);
39            if (sec.isSequentialAOTSec()) {
40                accumulateTotalSize(s.second.second);
41            }
42        }
43        accumulateTotalSize(moduleDes.GetArkStackMapSize());
44    }
45
46    MainFuncEntry GetMainFuncEntry(uint32_t fileIndex, uint32_t methodId) const
47    {
48        auto it = mainEntryMap_.find(std::make_pair(fileIndex, methodId));
49        if (it == mainEntryMap_.end()) {
50            return MainFuncEntry { 0, 0, false };
51        }
52        return it->second;
53    }
54
55    void TryRemoveAnFile(const char *filename);
56
57    void AlignTextSec(uint32_t alignSize)
58    {
59        curTextSecOffset_ = AlignUp(curTextSecOffset_, alignSize);
60    }
61
62    void UpdateCurTextSecOffset(uint64_t size)
63    {
64        curTextSecOffset_ += size;
65    }
66
67    uint64_t GetCurTextSecOffset() const
68    {
69        return curTextSecOffset_;
70    }
71
72    bool IsLoadMain(uint32_t fileIndex, const JSPandaFile *jsPandaFile, const CString &entry) const;
73
74    bool IsLoad() const
75    {
76        return isLoad_;
77    }
78
79    void Destroy() override;
80
81    void MappingEntryFuncsToAbcFiles(std::string curCompileFileName, uint32_t start, uint32_t end)
82    {
83        while (start < end) {
84            entryIdxToFileNameMap_[start] = curCompileFileName;
85            ++start;
86        }
87    }
88
89    const CMap<FuncEntryIndexKey, uint32_t>& GetMethodToEntryIndexMap() const
90    {
91        return methodToEntryIndexMap_;
92    }
93
94    void PUBLIC_API GenerateMethodToEntryIndexMap();
95
96    void Dump() const;
97
98private:
99    static const std::vector<ElfSecName> &GetDumpSectionNames();
100    using EntryKey = std::pair<uint32_t, uint32_t>;
101    bool LoadInternal(const std::string &filename);
102    bool Load(const std::string &filename);
103#if defined(CROSS_PLATFORM) && defined(ANDROID_PLATFORM)
104    bool Load(const std::string &filename, [[maybe_unused]] std::function<bool
105        (std::string fileName, uint8_t **buff, size_t *buffSize)> ReadAOTCallBack);
106#endif
107    void ParseFunctionEntrySection(ModuleSectionDes &moduleDes);
108    void UpdateFuncEntries();
109    void AddFuncEntrySec();
110    uint64_t curTextSecOffset_ {0};
111    // Future work: add main entry mapping to ai file
112    std::map<EntryKey, MainFuncEntry> mainEntryMap_ {};
113    bool isLoad_ {false};
114    CUnorderedMap<uint32_t, std::string> entryIdxToFileNameMap_ {};
115    CMap<FuncEntryIndexKey, uint32_t> methodToEntryIndexMap_ {};
116
117    friend class AnFileDataManager;
118};
119}  // namespace panda::ecmascript
120#endif // ECMASCRIPT_COMPILER_AOT_FILE_AN_FILE_INFO_H
121