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#include "ecmascript/deoptimizer/calleeReg.h"
16#include <iostream>
17
18namespace panda::ecmascript::kungfu {
19CalleeReg::CalleeReg()
20{
21#if defined(PANDA_TARGET_AMD64)
22    reg2Location_ = {
23        {DwarfReg::RBX, 0},
24        {DwarfReg::R15, 1},
25        {DwarfReg::R14, 2},
26        {DwarfReg::R13, 3},
27        {DwarfReg::R12, 4},
28    };
29#elif defined(PANDA_TARGET_ARM64)
30    reg2Location_ = {
31        {DwarfReg::D8, 0},
32        {DwarfReg::D9, 1},
33        {DwarfReg::D10, 2},
34        {DwarfReg::D11, 3},
35        {DwarfReg::D12, 4},
36        {DwarfReg::D13, 5},
37        {DwarfReg::D14, 6},
38        {DwarfReg::D15, 7},
39
40        {DwarfReg::X19, 8},
41        {DwarfReg::X20, 9},
42        {DwarfReg::X21, 10},
43        {DwarfReg::X22, 11},
44        {DwarfReg::X23, 12},
45        {DwarfReg::X24, 13},
46        {DwarfReg::X25, 14},
47        {DwarfReg::X26, 15},
48        {DwarfReg::X27, 16},
49        {DwarfReg::X28, 17},
50    };
51#endif
52}
53
54int CalleeReg::FindCallRegOrder(const LLVMStackMapType::DwarfRegType reg) const
55{
56    auto it = reg2Location_.find(static_cast<DwarfReg>(reg));
57    if (it != reg2Location_.end()) {
58        return it->second;
59    } else {
60        LOG_FULL(FATAL) << "reg:" << std::dec << reg;
61        UNREACHABLE();
62    }
63}
64
65int CalleeReg::FindCallRegOrder(const DwarfReg reg) const
66{
67    auto order = FindCallRegOrder(static_cast<LLVMStackMapType::DwarfRegType>(reg));
68    return order;
69}
70
71int CalleeReg::GetCallRegNum() const
72{
73    return reg2Location_.size();
74}
75} // namespace panda::ecmascript::kungfu
76