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
25namespace panda::ecmascript {
26    class BinaryBufferParser;
27}
28
29namespace panda::ecmascript::kungfu {
30class ArkStackMapParser {
31public:
32    explicit ArkStackMapParser(bool enableLog = false)
33    {
34        enableLog_ = enableLog;
35    }
36    ~ArkStackMapParser()
37    {
38        enableLog_ = false;
39    }
40    void GetConstInfo(uintptr_t callsite, LLVMStackMapType::ConstInfo& info, uint8_t *stackmapAddr = nullptr) const;
41    void GetMethodOffsetInfo(uintptr_t callSiteAddr, std::map<uint32_t, uint32_t>& info,
42                             uint8_t *stackmapAddr) const;
43    bool IteratorStackMap(const RootVisitor& visitor,
44                          const RootBaseAndDerivedVisitor& derivedVisitor,
45                          uintptr_t callSiteAddr, uintptr_t callsiteFp,
46                          uintptr_t callSiteSp, uint8_t *stackmapAddr) const;
47    void GetArkDeopt(uintptr_t callSiteAddr, uint8_t *stackmapAddr, std::vector<ARKDeopt>& deopts) const;
48
49private:
50    static constexpr size_t DEOPT_ENTRY_SIZE = 2;
51    static constexpr size_t GC_ENTRY_SIZE = 2;
52
53    int BinaraySearch(CallsiteHeader *callsiteHead, uint32_t callsiteNum, uintptr_t callSiteAddr) const;
54    void GetArkDeopt(uint8_t *stackmapAddr, const CallsiteHeader& callsiteHead,
55                     std::vector<ARKDeopt>& deopt) const;
56    void ParseArkDeopt(const CallsiteHeader& callsiteHead, uint8_t *ptr, std::vector<ARKDeopt>& deopts) const;
57    void ParseArkStackMap(const CallsiteHeader& callsiteHead, uint8_t *ptr, ArkStackMap& stackMap) const;
58#ifndef NDEBUG
59    void ParseArkStackMapAndDeopt(uint8_t *ptr, uint32_t length) const;
60#endif
61    uintptr_t GetStackSlotAddress(uint8_t *stackmapAddr, uintptr_t callSiteSp, uintptr_t callsiteFp,
62                                  uint32_t &offset) const;
63    friend class ArkStackMapBuilder;
64    bool enableLog_ {false};
65};
66}
67#endif  // ECMASCRIPT_ARK_STACKMAP_PARSER_H