• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2024 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_AOT_FILE_INFO_H
16 #define ECMASCRIPT_COMPILER_AOT_FILE_AOT_FILE_INFO_H
17 
18 #include "ecmascript/common.h"
19 #include "ecmascript/compiler/aot_file/executed_memory_allocator.h"
20 #include "ecmascript/compiler/aot_file/module_section_des.h"
21 #include "ecmascript/compiler/bc_call_signature.h"
22 #include "ecmascript/deoptimizer/calleeReg.h"
23 #include "ecmascript/compiler/aot_file/func_entry_des.h"
24 #include "ecmascript/stackmap/ark_stackmap.h"
25 
26 namespace panda::ecmascript {
27 class PUBLIC_API AOTFileInfo {
28 public:
29     using FuncEntryDes = ecmascript::FuncEntryDes;
30     using CallSignature = kungfu::CallSignature;
31     using CalleeRegAndOffsetVec = kungfu::CalleeRegAndOffsetVec;
32     using DwarfRegType = kungfu::LLVMStackMapType::DwarfRegType;
33     using OffsetType = kungfu::LLVMStackMapType::OffsetType;
34     using DwarfRegAndOffsetType = kungfu::LLVMStackMapType::DwarfRegAndOffsetType;
35     AOTFileInfo() = default;
36     virtual ~AOTFileInfo() = default;
37 
38     static constexpr uint32_t TEXT_SEC_ALIGN = 16;
39     static constexpr uint32_t RODATA_SEC_ALIGN = 16;
40     static constexpr uint32_t DATA_SEC_ALIGN = 8;
41     static constexpr uint32_t PAGE_ALIGN = 4096;
42 
GetStubDes(int index)43     const FuncEntryDes &GetStubDes(int index) const
44     {
45         return entries_[index];
46     }
47 
GetEntrySize()48     uint32_t GetEntrySize() const
49     {
50         return entries_.size();
51     }
52 
GetStubs()53     const std::vector<FuncEntryDes> &GetStubs() const
54     {
55         return entries_;
56     }
57 
GetCodeUnits()58     const std::vector<ModuleSectionDes> &GetCodeUnits() const
59     {
60         return des_;
61     }
62 
GetStubNum()63     uint32_t GetStubNum() const
64     {
65         return entryNum_;
66     }
67 
SetStubNum(uint32_t n)68     void SetStubNum(uint32_t n)
69     {
70         entryNum_ = n;
71     }
72 
73     void AddEntry(CallSignature::TargetKind kind, bool isMainFunc, bool isFastCall, int indexInKind, uint64_t offset,
74                   uint32_t fileIndex, uint32_t moduleIndex, int delta, uint32_t size, CalleeRegAndOffsetVec info = {})
75     {
76         FuncEntryDes des;
77         if (memset_s(&des, sizeof(des), 0, sizeof(des)) != EOK) {
78             LOG_FULL(FATAL) << "memset_s failed";
79             return;
80         }
81         des.kind_ = kind;
82         des.isMainFunc_ = isMainFunc;
83         des.isFastCall_ = isFastCall;
84         des.indexInKindOrMethodId_ = static_cast<uint32_t>(indexInKind);
85         des.abcIndexInAi_ = fileIndex;
86         des.codeAddr_ = offset;
87         des.moduleIndex_ = moduleIndex;
88         des.fpDeltaPrevFrameSp_ = delta;
89         des.funcSize_ = size;
90         des.calleeRegisterNum_ = info.size();
91         DwarfRegType reg = 0;
92         OffsetType regOffset = 0;
93         for (size_t i = 0; i < info.size(); i++) {
94             std::tie(reg, regOffset) = info[i];
95             des.CalleeReg2Offset_[2 * i] = static_cast<int32_t>(reg);
96             des.CalleeReg2Offset_[2 * i + 1] = static_cast<int32_t>(regOffset);
97         }
98         entries_.emplace_back(des);
99     }
100 
GetModuleSectionDes()101     const std::vector<ModuleSectionDes> &GetModuleSectionDes() const
102     {
103         return des_;
104     }
105 
UpdateStackMap(std::shared_ptr<uint8_t> ptr,uint32_t size,uint32_t moduleIdx)106     void UpdateStackMap(std::shared_ptr<uint8_t> ptr, uint32_t size, uint32_t moduleIdx)
107     {
108         ASSERT(moduleIdx < des_.size());
109         ModuleSectionDes &des = des_[moduleIdx];
110         des.SetArkStackMapPtr(ptr);
111         des.SetArkStackMapSize(size);
112     }
113 
GetCodeUnitsNum()114     size_t GetCodeUnitsNum() const
115     {
116         return des_.size();
117     }
118 
accumulateTotalSize(uint32_t size)119     void accumulateTotalSize(uint32_t size)
120     {
121         totalCodeSize_ += size;
122     }
123 
GetTotalCodeSize()124     uint32_t GetTotalCodeSize() const
125     {
126         return totalCodeSize_;
127     }
128 
129     using CallSiteInfo = std::tuple<uint64_t, uint8_t *, int, CalleeRegAndOffsetVec>;
130 
131     bool CalCallSiteInfo(uintptr_t retAddr, CallSiteInfo &ret, bool isInStub, bool isDeopt) const;
132 
133     virtual void Destroy();
134 
135 protected:
GetStubsMem()136     ExecutedMemoryAllocator::ExeMem &GetStubsMem()
137     {
138         return stubsMem_;
139     }
140 
GetFileMapMem()141     MemMap &GetFileMapMem()
142     {
143         return fileMapMem_;
144     }
145 
146     uint32_t entryNum_ {0};
147     uint32_t moduleNum_ {0};
148     uint32_t totalCodeSize_ {0};
149     std::vector<FuncEntryDes> entries_ {};
150     std::vector<ModuleSectionDes> des_ {};
151     std::vector<char> checksumDataVector_ {};
152     ExecutedMemoryAllocator::ExeMem stubsMem_ {};
153     MemMap fileMapMem_ {};
154 
155 private:
156     AOTFileInfo::FuncEntryDes GetFuncEntryDesWithCallsite(uintptr_t codeAddr, uint32_t startIndex,
157                                                           uint32_t funcCount) const;
158 
159     void StoreCalleeRegInfo(uint32_t calleeRegNum, int32_t *calleeReg2Offset,
160                             CalleeRegAndOffsetVec &calleeRegInfo) const;
161 };
162 }  // namespace panda::ecmascript
163 #endif  // ECMASCRIPT_COMPILER_AOT_FILE_AOT_FILE_INFO_H
164