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