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_FILE_AOT_FILE_INFO_H
164514f5e3Sopenharmony_ci#define ECMASCRIPT_COMPILER_AOT_FILE_AOT_FILE_INFO_H
174514f5e3Sopenharmony_ci
184514f5e3Sopenharmony_ci#include "ecmascript/common.h"
194514f5e3Sopenharmony_ci#include "ecmascript/compiler/aot_file/executed_memory_allocator.h"
204514f5e3Sopenharmony_ci#include "ecmascript/compiler/aot_file/module_section_des.h"
214514f5e3Sopenharmony_ci#include "ecmascript/compiler/bc_call_signature.h"
224514f5e3Sopenharmony_ci#include "ecmascript/deoptimizer/calleeReg.h"
234514f5e3Sopenharmony_ci#include "ecmascript/compiler/aot_file/func_entry_des.h"
244514f5e3Sopenharmony_ci#include "ecmascript/stackmap/ark_stackmap.h"
254514f5e3Sopenharmony_ci
264514f5e3Sopenharmony_cinamespace panda::ecmascript {
274514f5e3Sopenharmony_ciclass PUBLIC_API AOTFileInfo {
284514f5e3Sopenharmony_cipublic:
294514f5e3Sopenharmony_ci    using FuncEntryDes = ecmascript::FuncEntryDes;
304514f5e3Sopenharmony_ci    using CallSignature = kungfu::CallSignature;
314514f5e3Sopenharmony_ci    using CalleeRegAndOffsetVec = kungfu::CalleeRegAndOffsetVec;
324514f5e3Sopenharmony_ci    using DwarfRegType = kungfu::LLVMStackMapType::DwarfRegType;
334514f5e3Sopenharmony_ci    using OffsetType = kungfu::LLVMStackMapType::OffsetType;
344514f5e3Sopenharmony_ci    using DwarfRegAndOffsetType = kungfu::LLVMStackMapType::DwarfRegAndOffsetType;
354514f5e3Sopenharmony_ci    AOTFileInfo() = default;
364514f5e3Sopenharmony_ci    virtual ~AOTFileInfo() = default;
374514f5e3Sopenharmony_ci
384514f5e3Sopenharmony_ci    static constexpr uint32_t TEXT_SEC_ALIGN = 16;
394514f5e3Sopenharmony_ci    static constexpr uint32_t DATA_SEC_ALIGN = 8;
404514f5e3Sopenharmony_ci    static constexpr uint32_t PAGE_ALIGN = 4096;
414514f5e3Sopenharmony_ci
424514f5e3Sopenharmony_ci    const FuncEntryDes &GetStubDes(int index) const
434514f5e3Sopenharmony_ci    {
444514f5e3Sopenharmony_ci        return entries_[index];
454514f5e3Sopenharmony_ci    }
464514f5e3Sopenharmony_ci
474514f5e3Sopenharmony_ci    uint32_t GetEntrySize() const
484514f5e3Sopenharmony_ci    {
494514f5e3Sopenharmony_ci        return entries_.size();
504514f5e3Sopenharmony_ci    }
514514f5e3Sopenharmony_ci
524514f5e3Sopenharmony_ci    const std::vector<FuncEntryDes> &GetStubs() const
534514f5e3Sopenharmony_ci    {
544514f5e3Sopenharmony_ci        return entries_;
554514f5e3Sopenharmony_ci    }
564514f5e3Sopenharmony_ci
574514f5e3Sopenharmony_ci    const std::vector<ModuleSectionDes> &GetCodeUnits() const
584514f5e3Sopenharmony_ci    {
594514f5e3Sopenharmony_ci        return des_;
604514f5e3Sopenharmony_ci    }
614514f5e3Sopenharmony_ci
624514f5e3Sopenharmony_ci    uint32_t GetStubNum() const
634514f5e3Sopenharmony_ci    {
644514f5e3Sopenharmony_ci        return entryNum_;
654514f5e3Sopenharmony_ci    }
664514f5e3Sopenharmony_ci
674514f5e3Sopenharmony_ci    void SetStubNum(uint32_t n)
684514f5e3Sopenharmony_ci    {
694514f5e3Sopenharmony_ci        entryNum_ = n;
704514f5e3Sopenharmony_ci    }
714514f5e3Sopenharmony_ci
724514f5e3Sopenharmony_ci    void AddEntry(CallSignature::TargetKind kind, bool isMainFunc, bool isFastCall, int indexInKind, uint64_t offset,
734514f5e3Sopenharmony_ci                  uint32_t fileIndex, uint32_t moduleIndex, int delta, uint32_t size, CalleeRegAndOffsetVec info = {})
744514f5e3Sopenharmony_ci    {
754514f5e3Sopenharmony_ci        FuncEntryDes des;
764514f5e3Sopenharmony_ci        if (memset_s(&des, sizeof(des), 0, sizeof(des)) != EOK) {
774514f5e3Sopenharmony_ci            LOG_FULL(FATAL) << "memset_s failed";
784514f5e3Sopenharmony_ci            return;
794514f5e3Sopenharmony_ci        }
804514f5e3Sopenharmony_ci        des.kind_ = kind;
814514f5e3Sopenharmony_ci        des.isMainFunc_ = isMainFunc;
824514f5e3Sopenharmony_ci        des.isFastCall_ = isFastCall;
834514f5e3Sopenharmony_ci        des.indexInKindOrMethodId_ = static_cast<uint32_t>(indexInKind);
844514f5e3Sopenharmony_ci        des.abcIndexInAi_ = fileIndex;
854514f5e3Sopenharmony_ci        des.codeAddr_ = offset;
864514f5e3Sopenharmony_ci        des.moduleIndex_ = moduleIndex;
874514f5e3Sopenharmony_ci        des.fpDeltaPrevFrameSp_ = delta;
884514f5e3Sopenharmony_ci        des.funcSize_ = size;
894514f5e3Sopenharmony_ci        des.calleeRegisterNum_ = info.size();
904514f5e3Sopenharmony_ci        DwarfRegType reg = 0;
914514f5e3Sopenharmony_ci        OffsetType regOffset = 0;
924514f5e3Sopenharmony_ci        for (size_t i = 0; i < info.size(); i++) {
934514f5e3Sopenharmony_ci            std::tie(reg, regOffset) = info[i];
944514f5e3Sopenharmony_ci            des.CalleeReg2Offset_[2 * i] = static_cast<int32_t>(reg);
954514f5e3Sopenharmony_ci            des.CalleeReg2Offset_[2 * i + 1] = static_cast<int32_t>(regOffset);
964514f5e3Sopenharmony_ci        }
974514f5e3Sopenharmony_ci        entries_.emplace_back(des);
984514f5e3Sopenharmony_ci    }
994514f5e3Sopenharmony_ci
1004514f5e3Sopenharmony_ci    const std::vector<ModuleSectionDes> &GetModuleSectionDes() const
1014514f5e3Sopenharmony_ci    {
1024514f5e3Sopenharmony_ci        return des_;
1034514f5e3Sopenharmony_ci    }
1044514f5e3Sopenharmony_ci
1054514f5e3Sopenharmony_ci    void UpdateStackMap(std::shared_ptr<uint8_t> ptr, uint32_t size, uint32_t moduleIdx)
1064514f5e3Sopenharmony_ci    {
1074514f5e3Sopenharmony_ci        ASSERT(moduleIdx < des_.size());
1084514f5e3Sopenharmony_ci        ModuleSectionDes &des = des_[moduleIdx];
1094514f5e3Sopenharmony_ci        des.SetArkStackMapPtr(ptr);
1104514f5e3Sopenharmony_ci        des.SetArkStackMapSize(size);
1114514f5e3Sopenharmony_ci    }
1124514f5e3Sopenharmony_ci
1134514f5e3Sopenharmony_ci    size_t GetCodeUnitsNum() const
1144514f5e3Sopenharmony_ci    {
1154514f5e3Sopenharmony_ci        return des_.size();
1164514f5e3Sopenharmony_ci    }
1174514f5e3Sopenharmony_ci
1184514f5e3Sopenharmony_ci    void accumulateTotalSize(uint32_t size)
1194514f5e3Sopenharmony_ci    {
1204514f5e3Sopenharmony_ci        totalCodeSize_ += size;
1214514f5e3Sopenharmony_ci    }
1224514f5e3Sopenharmony_ci
1234514f5e3Sopenharmony_ci    uint32_t GetTotalCodeSize() const
1244514f5e3Sopenharmony_ci    {
1254514f5e3Sopenharmony_ci        return totalCodeSize_;
1264514f5e3Sopenharmony_ci    }
1274514f5e3Sopenharmony_ci
1284514f5e3Sopenharmony_ci    using CallSiteInfo = std::tuple<uint64_t, uint8_t *, int, CalleeRegAndOffsetVec>;
1294514f5e3Sopenharmony_ci
1304514f5e3Sopenharmony_ci    bool CalCallSiteInfo(uintptr_t retAddr, CallSiteInfo &ret, bool isInStub, bool isDeopt) const;
1314514f5e3Sopenharmony_ci
1324514f5e3Sopenharmony_ci    virtual void Destroy();
1334514f5e3Sopenharmony_ci
1344514f5e3Sopenharmony_ciprotected:
1354514f5e3Sopenharmony_ci    ExecutedMemoryAllocator::ExeMem &GetStubsMem()
1364514f5e3Sopenharmony_ci    {
1374514f5e3Sopenharmony_ci        return stubsMem_;
1384514f5e3Sopenharmony_ci    }
1394514f5e3Sopenharmony_ci
1404514f5e3Sopenharmony_ci    MemMap &GetFileMapMem()
1414514f5e3Sopenharmony_ci    {
1424514f5e3Sopenharmony_ci        return fileMapMem_;
1434514f5e3Sopenharmony_ci    }
1444514f5e3Sopenharmony_ci
1454514f5e3Sopenharmony_ci    uint32_t entryNum_ {0};
1464514f5e3Sopenharmony_ci    uint32_t moduleNum_ {0};
1474514f5e3Sopenharmony_ci    uint32_t totalCodeSize_ {0};
1484514f5e3Sopenharmony_ci    std::vector<FuncEntryDes> entries_ {};
1494514f5e3Sopenharmony_ci    std::vector<ModuleSectionDes> des_ {};
1504514f5e3Sopenharmony_ci    ExecutedMemoryAllocator::ExeMem stubsMem_ {};
1514514f5e3Sopenharmony_ci    MemMap fileMapMem_ {};
1524514f5e3Sopenharmony_ci
1534514f5e3Sopenharmony_ciprivate:
1544514f5e3Sopenharmony_ci    AOTFileInfo::FuncEntryDes GetFuncEntryDesWithCallsite(uintptr_t codeAddr, uint32_t startIndex,
1554514f5e3Sopenharmony_ci                                                          uint32_t funcCount) const;
1564514f5e3Sopenharmony_ci
1574514f5e3Sopenharmony_ci    void StoreCalleeRegInfo(uint32_t calleeRegNum, int32_t *calleeReg2Offset,
1584514f5e3Sopenharmony_ci                            CalleeRegAndOffsetVec &calleeRegInfo) const;
1594514f5e3Sopenharmony_ci};
1604514f5e3Sopenharmony_ci}  // namespace panda::ecmascript
1614514f5e3Sopenharmony_ci#endif  // ECMASCRIPT_COMPILER_AOT_FILE_AOT_FILE_INFO_H
162