14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2023 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_FILE_AN_FILE_INFO_H
164514f5e3Sopenharmony_ci#define ECMASCRIPT_COMPILER_AOT_FILE_AN_FILE_INFO_H
174514f5e3Sopenharmony_ci
184514f5e3Sopenharmony_ci#include "ecmascript/compiler/aot_file/aot_file_info.h"
194514f5e3Sopenharmony_ci#include "ecmascript/compiler/assembler/assembler.h"
204514f5e3Sopenharmony_ci
214514f5e3Sopenharmony_cinamespace panda::ecmascript {
224514f5e3Sopenharmony_cistruct MainFuncEntry {
234514f5e3Sopenharmony_ci    uint64_t mainEntry {0};
244514f5e3Sopenharmony_ci    int32_t fpDelta {0};
254514f5e3Sopenharmony_ci    bool isFastCall {false};
264514f5e3Sopenharmony_ci};
274514f5e3Sopenharmony_ci
284514f5e3Sopenharmony_ciclass PUBLIC_API AnFileInfo : public AOTFileInfo {
294514f5e3Sopenharmony_cipublic:
304514f5e3Sopenharmony_ci    using FuncEntryIndexKey = std::pair<std::string, uint32_t>; // (compilefileName, MethodID)
314514f5e3Sopenharmony_ci    AnFileInfo() = default;
324514f5e3Sopenharmony_ci    ~AnFileInfo() override = default;
334514f5e3Sopenharmony_ci    bool PUBLIC_API Save(const std::string &filename, Triple triple);
344514f5e3Sopenharmony_ci    void AddModuleDes(ModuleSectionDes &moduleDes)
354514f5e3Sopenharmony_ci    {
364514f5e3Sopenharmony_ci        des_.emplace_back(moduleDes);
374514f5e3Sopenharmony_ci        for (auto &s : moduleDes.GetSectionsInfo()) {
384514f5e3Sopenharmony_ci            auto sec = ElfSection(s.first);
394514f5e3Sopenharmony_ci            if (sec.isSequentialAOTSec()) {
404514f5e3Sopenharmony_ci                accumulateTotalSize(s.second.second);
414514f5e3Sopenharmony_ci            }
424514f5e3Sopenharmony_ci        }
434514f5e3Sopenharmony_ci        accumulateTotalSize(moduleDes.GetArkStackMapSize());
444514f5e3Sopenharmony_ci    }
454514f5e3Sopenharmony_ci
464514f5e3Sopenharmony_ci    MainFuncEntry GetMainFuncEntry(uint32_t fileIndex, uint32_t methodId) const
474514f5e3Sopenharmony_ci    {
484514f5e3Sopenharmony_ci        auto it = mainEntryMap_.find(std::make_pair(fileIndex, methodId));
494514f5e3Sopenharmony_ci        if (it == mainEntryMap_.end()) {
504514f5e3Sopenharmony_ci            return MainFuncEntry { 0, 0, false };
514514f5e3Sopenharmony_ci        }
524514f5e3Sopenharmony_ci        return it->second;
534514f5e3Sopenharmony_ci    }
544514f5e3Sopenharmony_ci
554514f5e3Sopenharmony_ci    void TryRemoveAnFile(const char *filename);
564514f5e3Sopenharmony_ci
574514f5e3Sopenharmony_ci    void AlignTextSec(uint32_t alignSize)
584514f5e3Sopenharmony_ci    {
594514f5e3Sopenharmony_ci        curTextSecOffset_ = AlignUp(curTextSecOffset_, alignSize);
604514f5e3Sopenharmony_ci    }
614514f5e3Sopenharmony_ci
624514f5e3Sopenharmony_ci    void UpdateCurTextSecOffset(uint64_t size)
634514f5e3Sopenharmony_ci    {
644514f5e3Sopenharmony_ci        curTextSecOffset_ += size;
654514f5e3Sopenharmony_ci    }
664514f5e3Sopenharmony_ci
674514f5e3Sopenharmony_ci    uint64_t GetCurTextSecOffset() const
684514f5e3Sopenharmony_ci    {
694514f5e3Sopenharmony_ci        return curTextSecOffset_;
704514f5e3Sopenharmony_ci    }
714514f5e3Sopenharmony_ci
724514f5e3Sopenharmony_ci    bool IsLoadMain(uint32_t fileIndex, const JSPandaFile *jsPandaFile, const CString &entry) const;
734514f5e3Sopenharmony_ci
744514f5e3Sopenharmony_ci    bool IsLoad() const
754514f5e3Sopenharmony_ci    {
764514f5e3Sopenharmony_ci        return isLoad_;
774514f5e3Sopenharmony_ci    }
784514f5e3Sopenharmony_ci
794514f5e3Sopenharmony_ci    void Destroy() override;
804514f5e3Sopenharmony_ci
814514f5e3Sopenharmony_ci    void MappingEntryFuncsToAbcFiles(std::string curCompileFileName, uint32_t start, uint32_t end)
824514f5e3Sopenharmony_ci    {
834514f5e3Sopenharmony_ci        while (start < end) {
844514f5e3Sopenharmony_ci            entryIdxToFileNameMap_[start] = curCompileFileName;
854514f5e3Sopenharmony_ci            ++start;
864514f5e3Sopenharmony_ci        }
874514f5e3Sopenharmony_ci    }
884514f5e3Sopenharmony_ci
894514f5e3Sopenharmony_ci    const CMap<FuncEntryIndexKey, uint32_t>& GetMethodToEntryIndexMap() const
904514f5e3Sopenharmony_ci    {
914514f5e3Sopenharmony_ci        return methodToEntryIndexMap_;
924514f5e3Sopenharmony_ci    }
934514f5e3Sopenharmony_ci
944514f5e3Sopenharmony_ci    void PUBLIC_API GenerateMethodToEntryIndexMap();
954514f5e3Sopenharmony_ci
964514f5e3Sopenharmony_ci    void Dump() const;
974514f5e3Sopenharmony_ci
984514f5e3Sopenharmony_ciprivate:
994514f5e3Sopenharmony_ci    static const std::vector<ElfSecName> &GetDumpSectionNames();
1004514f5e3Sopenharmony_ci    using EntryKey = std::pair<uint32_t, uint32_t>;
1014514f5e3Sopenharmony_ci    bool LoadInternal(const std::string &filename);
1024514f5e3Sopenharmony_ci    bool Load(const std::string &filename);
1034514f5e3Sopenharmony_ci#if defined(CROSS_PLATFORM) && defined(ANDROID_PLATFORM)
1044514f5e3Sopenharmony_ci    bool Load(const std::string &filename, [[maybe_unused]] std::function<bool
1054514f5e3Sopenharmony_ci        (std::string fileName, uint8_t **buff, size_t *buffSize)> ReadAOTCallBack);
1064514f5e3Sopenharmony_ci#endif
1074514f5e3Sopenharmony_ci    void ParseFunctionEntrySection(ModuleSectionDes &moduleDes);
1084514f5e3Sopenharmony_ci    void UpdateFuncEntries();
1094514f5e3Sopenharmony_ci    void AddFuncEntrySec();
1104514f5e3Sopenharmony_ci    uint64_t curTextSecOffset_ {0};
1114514f5e3Sopenharmony_ci    // Future work: add main entry mapping to ai file
1124514f5e3Sopenharmony_ci    std::map<EntryKey, MainFuncEntry> mainEntryMap_ {};
1134514f5e3Sopenharmony_ci    bool isLoad_ {false};
1144514f5e3Sopenharmony_ci    CUnorderedMap<uint32_t, std::string> entryIdxToFileNameMap_ {};
1154514f5e3Sopenharmony_ci    CMap<FuncEntryIndexKey, uint32_t> methodToEntryIndexMap_ {};
1164514f5e3Sopenharmony_ci
1174514f5e3Sopenharmony_ci    friend class AnFileDataManager;
1184514f5e3Sopenharmony_ci};
1194514f5e3Sopenharmony_ci}  // namespace panda::ecmascript
1204514f5e3Sopenharmony_ci#endif // ECMASCRIPT_COMPILER_AOT_FILE_AN_FILE_INFO_H
121