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