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
16#ifndef ECMASCRIPT_ARK_STACKMAP_H
17#define ECMASCRIPT_ARK_STACKMAP_H
18
19#include "ecmascript/stackmap/llvm/llvm_stackmap_type.h"
20
21namespace panda::ecmascript::kungfu {
22struct ARKConst {
23    LLVMStackMapType::IntType offset;
24};
25
26struct ArkStackMapHeader {
27    uint32_t secSize;
28    uint32_t callsiteNum;
29};
30
31#pragma pack(1)
32struct CallsiteHeader {
33    uint32_t calliteOffsetInTxtSec {0}; // relative text start addr
34    uint32_t stackmapOffsetInSMSec{0}; // relative stackmap start addr
35    uint32_t deoptOffset {0};
36    uint16_t stackmapNum {0};
37    uint16_t deoptNum {0};
38};
39#pragma pack ()
40
41/* <vregId and kind, value> */
42struct ARKDeopt {
43    LLVMStackMapType::VRegId id; // deoptid
44    LocationTy::Kind kind;
45    std::variant<LLVMStackMapType::IntType, LLVMStackMapType::LargeInt, LLVMStackMapType::DwarfRegAndOffsetType> value;
46};
47
48struct ARKCallsite {
49    CallsiteHeader head;
50    LLVMStackMapType::CallSiteInfo stackmaps;
51    std::vector<kungfu::ARKDeopt> callsite2Deopt;
52    bool operator < (const ARKCallsite & x) const
53    {
54        return head.calliteOffsetInTxtSec < x.head.calliteOffsetInTxtSec;
55    }
56    uint32_t CalHeadSize() const;
57    uint32_t CalStackMapSize(Triple triple) const;
58};
59
60struct ARKCallsiteAOTFileInfo {
61    ArkStackMapHeader secHead;
62    std::vector<ARKCallsite> callsites;
63};
64using ArkStackMap = LLVMStackMapType::CallSiteInfo;
65using CalleeRegAndOffsetVec = std::vector<LLVMStackMapType::DwarfRegAndOffsetType>;
66// * ArkStackMap layout as the following:
67//               +-----------------------------------------+ ---------
68//               |                 secSize                 |         ^
69//               |-----------------------------------------|   ArkStackMapHeader
70//               |               callsiteNum               |         v
71//               +-----------------------------------------+ ---------
72//               |           calliteOffsetInTxtSec         |         ^
73//               |-----------------------------------------|         |
74//               |            stackmapOffsetInSMSec        |         |
75//               |-----------------------------------------|         |
76//               |               deoptOffset               |   CallsiteHeader[0]
77//               |-----------------------------------------|         |
78//               |               stackmapNum               |         |
79//               |-----------------------------------------|         |
80//               |                deoptNum                 |         v
81//               |-----------------------------------------| ---------
82//               |                . . . . .                |
83//               +-----------------------------------------+ ---------
84//               |          dwarfRegAndOff(sleb128)        |     StackMaps[0]
85//               |-----------------------------------------| ---------
86//               |                . . . . .                |
87//               +-----------------------------------------+ ---------
88//               |          vregIdAndKind(sleb128)         |         ^
89//               |-----------------------------------------|     Deopts[0]
90//               |              value(sleb128)             |         v
91//               |-----------------------------------------| ---------
92//               |                . . . . .                |
93//               +-----------------------------------------+ ---------
94//
95// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
96} // namespace panda::ecmascript::kungfu
97#endif  // ECMASCRIPT_ARK_STACKMAP_H