1/*
2 * Copyright (c) 2021-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
16#ifndef ECMASCRIPT_LLVM_STACKMAP_PARSER_H
17#define ECMASCRIPT_LLVM_STACKMAP_PARSER_H
18
19#include <iostream>
20#include <memory>
21#include <tuple>
22#include <unordered_map>
23#include <vector>
24
25#include "ecmascript/common.h"
26#include "ecmascript/ecma_macros.h"
27#include "ecmascript/stackmap/ark_stackmap.h"
28#include "ecmascript/stackmap/ark_stackmap_builder.h"
29#include "ecmascript/stackmap/llvm/llvm_stackmap_type.h"
30
31namespace panda::ecmascript::kungfu {
32class LLVMStackMapParser {
33public:
34    bool PUBLIC_API CalculateStackMap(std::unique_ptr<uint8_t []> stackMapAddr);
35    bool PUBLIC_API CalculateStackMap(std::unique_ptr<uint8_t []> stackMapAddr,
36                                      uintptr_t hostCodeSectionAddr,
37                                      uintptr_t hostCodeSectionOffset);
38    void PUBLIC_API Print() const
39    {
40        if (IsLogEnabled()) {
41            llvmStackMap_.Print();
42        }
43    }
44
45    bool IsLogEnabled() const
46    {
47        return enableLog_;
48    }
49
50    LLVMStackMapParser(LLVMStackMapInfo &stackMapInfo, bool enableLog = false)
51        : stackMapInfo(stackMapInfo),
52          enableLog_(enableLog)
53    {
54        fun2RecordNum_.clear();
55        dataInfo_ = nullptr;
56        funAddr_.clear();
57        fun2FpDelta_.clear();
58        llvmStackMap_.head.stackmapversion = 0;
59        llvmStackMap_.head.reserved0 = 0;
60        llvmStackMap_.head.reserved1 = 0;
61    }
62    ~LLVMStackMapParser()
63    {
64        fun2RecordNum_.clear();
65        dataInfo_ = nullptr;
66        funAddr_.clear();
67        fun2FpDelta_.clear();
68    }
69
70private:
71    static const size_t GC_PAIR_SIZE = 2;
72    void FilterCallSiteInfo(LLVMStackMapType::CallSiteInfo &info);
73    void CalcCallSite();
74    struct LLVMStackMap llvmStackMap_;
75    // use reference here to avoid extra copy
76    LLVMStackMapInfo &stackMapInfo;
77    std::vector<std::pair<uintptr_t, uint64_t>> fun2RecordNum_;
78    std::unique_ptr<DataInfo> dataInfo_;
79    bool enableLog_ {false};
80    std::set<uintptr_t> funAddr_;
81    std::vector<LLVMStackMapType::Func2FpDelta> fun2FpDelta_;
82    std::map<uint32_t, std::vector<LLVMStackMapType::Func2FpDelta>> module2fun2FpDelta_;
83    std::map<uint32_t, std::set<uintptr_t>> module2funAddr_;
84};
85} // namespace panda::ecmascript::kungfu
86#endif  // ECMASCRIPT_LLVM_STACKMAP_PARSER_H
87