• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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_ARK_STACKMAP_BUILD_H
16 #define ECMASCRIPT_ARK_STACKMAP_BUILD_H
17 
18 #include <iostream>
19 #include <memory>
20 #include <set>
21 #include <tuple>
22 #include <unordered_map>
23 #include <vector>
24 #include <iomanip>
25 
26 #include "ecmascript/frames.h"
27 #include "ecmascript/stackmap/ark_stackmap.h"
28 #include "ecmascript/stackmap/llvm_stackmap_type.h"
29 
30 #include "libpandabase/utils/leb128.h"
31 namespace panda::ecmascript {
32     class BinaryBufferParser;
33 }
34 
35 namespace panda::ecmascript::kungfu {
36 class BinaryBufferWriter {
37 public:
BinaryBufferWriter(uint8_t * buffer,uint32_t length)38     BinaryBufferWriter(uint8_t *buffer, uint32_t length) : buffer_(buffer), length_(length) {}
39     ~BinaryBufferWriter() = default;
40     void WriteBuffer(const uint8_t *src, uint32_t count, bool flag = false);
GetOffset()41     uint32_t GetOffset() const
42     {
43         return offset_;
44     }
AlignOffset()45     void AlignOffset()
46     {
47         offset_ = AlignUp(offset_, LLVMStackMapType::STACKMAP_ALIGN_BYTES);
48     }
49 private:
50     uint8_t *buffer_ {nullptr};
51     uint32_t length_ {0};
52     uint32_t offset_ {0};
53 };
54 
55 struct StackMapDumper {
56     uint32_t callsiteHeadSize {0};
57     uint32_t arkStackMapSize {0};
58     uint32_t deoptSize {0};
59     uint32_t callsiteNum {0};
60     uint32_t stackmapNum {0};
61     uint32_t deoptNum {0};
62 };
63 
64 class ArkStackMapBuilder {
65 public:
66     ArkStackMapBuilder() = default;
67     ~ArkStackMapBuilder() = default;
68     std::pair<std::shared_ptr<uint8_t>, uint32_t> PUBLIC_API Run(std::unique_ptr<uint8_t []> stackMapAddr,
69         uintptr_t hostCodeSectionAddr, Triple triple);
70     std::pair<std::shared_ptr<uint8_t>, uint32_t> GenerateArkStackMap(
71         CGStackMapInfo &stackMapInfo, Triple triple);
72     void Collect(std::unique_ptr<uint8_t []> stackMapAddr,
73                  uintptr_t hostCodeSectionAddr,
74                  uintptr_t hostCodeSectionOffset,
75                  CGStackMapInfo &stackMapInfo);
SetTraceStackMap(bool flag)76     void SetTraceStackMap(bool flag)
77     {
78         traceStackMap_ = flag;
79     }
80 
81 private:
82     static constexpr int DECIMAL_LENS = 2;
83     static constexpr size_t DEOPT_ENTRY_SIZE = 2;
84     static constexpr size_t GC_ENTRY_SIZE = 2;
85 
86     template <class Vec>
87     void SortCallSite(const std::vector<std::unordered_map<uintptr_t, Vec>> &infos,
88         std::vector<std::pair<uintptr_t, Vec>>& result);
89     void CalcCallsitePc(std::vector<std::pair<uintptr_t, LLVMStackMapType::DeoptInfoType>> &pc2Deopt,
90                         std::vector<std::pair<uintptr_t, LLVMStackMapType::CallSiteInfo>> &pc2StackMap,
91                         std::vector<intptr_t> &callsitePcs);
92     void GenArkCallsiteAOTFileInfo(const CGStackMapInfo &stackMapInfo,
93                                    ARKCallsiteAOTFileInfo &result, Triple triple);
94     void SaveArkDeopt(const ARKCallsiteAOTFileInfo& info, BinaryBufferWriter& writer, Triple triple);
95     void SaveArkStackMap(const ARKCallsiteAOTFileInfo& info, BinaryBufferWriter& writer, Triple triple);
96     void SaveArkCallsiteAOTFileInfo(uint8_t *ptr, uint32_t length,
97                                     const ARKCallsiteAOTFileInfo& info, Triple triple);
98     int FindLoc(std::vector<intptr_t> &CallsitePcs, intptr_t pc);
99     void GenARKDeopt(const LLVMStackMapType::DeoptInfoType& deopt,
100         std::pair<uint32_t, std::vector<ARKDeopt>> &sizeAndArkDeopt, Triple triple);
101     void Dump(const StackMapDumper& dumpInfo) const;
102 
103     bool traceStackMap_{false};
104     StackMapDumper dumper_;
105 };
106 } // namespace panda::ecmascript::kungfu
107 #endif  // ECMASCRIPT_ARK_STACKMAP_BUILD_H
108