• 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_PARSER_H
16 #define ECMASCRIPT_ARK_STACKMAP_PARSER_H
17 
18 #include <cstdint>
19 
20 #include "ecmascript/frames.h"
21 #include "ecmascript/mem/visitor.h"
22 #include "ecmascript/stackmap/ark_stackmap.h"
23 #include "ecmascript/stackmap/llvm/llvm_stackmap_type.h"
24 
25 namespace panda::ecmascript {
26     class BinaryBufferParser;
27 }
28 
29 namespace panda::ecmascript::kungfu {
30 class ArkStackMapParser {
31 public:
32     explicit ArkStackMapParser(bool enableLog = false)
33     {
34         enableLog_ = enableLog;
35     }
~ArkStackMapParser()36     ~ArkStackMapParser()
37     {
38         enableLog_ = false;
39     }
40     size_t GetInlineDepth(const std::vector<ARKDeopt> &deopts) const;
41     size_t GetInlineDepth(uintptr_t callSiteAddr, uint8_t *stackmapAddr) const;
42     void CollectStackTraceInfos(uintptr_t callSiteAddr,
43                                 std::vector<std::pair<JSTaggedType, uint32_t>> &info,
44                                 uintptr_t callsiteSp,
45                                 uintptr_t callsiteFp,
46                                 uint8_t *stackmapAddr) const;
47     JSTaggedType GetFunction(const std::vector<ARKDeopt> &deopts, size_t currentDepth, size_t shift,
48                              uintptr_t callsiteSp, uintptr_t callsiteFp) const;
49     int32_t GetPcOffset(const std::vector<ARKDeopt> &deopts, size_t currentDepth, size_t shift) const;
50     void IteratorStackMap(RootVisitor& visitor, uintptr_t callsiteFp,
51                           uintptr_t callSiteSp, uint8_t *stackmapAddr,
52                           uint32_t offset, uint16_t stackmapNum,
53                           std::map<uintptr_t, uintptr_t> &baseSet) const;
54     void IteratorDeopt(RootVisitor& visitor, uintptr_t callsiteFp,
55                        uintptr_t callSiteSp, uint8_t *stackmapAddr,
56                        uint32_t offset, uint16_t num,
57                        std::map<uintptr_t, uintptr_t> &baseSet) const;
58     bool IteratorStackMapAndDeopt(RootVisitor& visitor,
59                                   uintptr_t callSiteAddr, uintptr_t callsiteFp,
60                                   uintptr_t callSiteSp, uint8_t *stackmapAddr) const;
61     void GetArkDeopt(uintptr_t callSiteAddr, uint8_t *stackmapAddr, std::vector<ARKDeopt>& deopts) const;
62 
63 private:
64     static constexpr size_t DEOPT_ENTRY_SIZE = 2;
65     static constexpr size_t GC_ENTRY_SIZE = 2;
66     static constexpr int BINARY_SEARCH_DIVISOR = 2;
67     int BinaraySearch(CallsiteHeader *callsiteHead, uint32_t callsiteNum, uintptr_t callSiteAddr) const;
68     void GetArkDeopt(uint8_t *stackmapAddr, const CallsiteHeader& callsiteHead,
69                      std::vector<ARKDeopt>& deopt) const;
70     void ParseArkDeopt(const CallsiteHeader& callsiteHead, uint8_t *ptr, std::vector<ARKDeopt>& deopts) const;
71 #ifndef NDEBUG
72     void ParseArkStackMap(const CallsiteHeader& callsiteHead, uint8_t *ptr, ArkStackMap& stackMap) const;
73     void ParseArkStackMapAndDeopt(uint8_t *ptr, uint32_t length) const;
74 #endif
75     uintptr_t GetStackSlotAddress(const LLVMStackMapType::DwarfRegAndOffsetType info,
76                                   uintptr_t callSiteSp,
77                                   uintptr_t callsiteFp) const;
78     uintptr_t GetStackSlotAddress(uint8_t *stackmapAddr, uintptr_t callSiteSp, uintptr_t callsiteFp,
79                                   uint32_t &offset, bool &nextIsBase, size_t &regOffsetSize) const;
80     uintptr_t GetDeoptStackSlotAddress(uint8_t *stackmapAddr, uintptr_t callSiteSp,
81                                        uintptr_t callsiteFp, uint32_t &offset) const;
82     friend class ArkStackMapBuilder;
83     bool enableLog_ {false};
84 };
85 }
86 #endif  // ECMASCRIPT_ARK_STACKMAP_PARSER_H