1*bdd1243dSDimitry Andric //===-- RegisterInfoPOSIX_loongarch64.cpp --------------------------------===//
2*bdd1243dSDimitry Andric //
3*bdd1243dSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*bdd1243dSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*bdd1243dSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*bdd1243dSDimitry Andric //
7*bdd1243dSDimitry Andric //===---------------------------------------------------------------------===//
8*bdd1243dSDimitry Andric 
9*bdd1243dSDimitry Andric #include <cassert>
10*bdd1243dSDimitry Andric #include <lldb/Utility/Flags.h>
11*bdd1243dSDimitry Andric #include <stddef.h>
12*bdd1243dSDimitry Andric 
13*bdd1243dSDimitry Andric #include "lldb/lldb-defines.h"
14*bdd1243dSDimitry Andric #include "llvm/Support/Compiler.h"
15*bdd1243dSDimitry Andric 
16*bdd1243dSDimitry Andric #include "RegisterInfoPOSIX_loongarch64.h"
17*bdd1243dSDimitry Andric 
18*bdd1243dSDimitry Andric #define GPR_OFFSET(idx) ((idx)*8 + 0)
19*bdd1243dSDimitry Andric #define FPR_OFFSET(idx) ((idx)*8 + sizeof(RegisterInfoPOSIX_loongarch64::GPR))
20*bdd1243dSDimitry Andric #define FCC_OFFSET(idx) ((idx)*1 + 32 * 8 + sizeof(RegisterInfoPOSIX_loongarch64::GPR))
21*bdd1243dSDimitry Andric #define FCSR_OFFSET (8 * 1 + 32 * 8 + sizeof(RegisterInfoPOSIX_loongarch64::GPR))
22*bdd1243dSDimitry Andric 
23*bdd1243dSDimitry Andric #define REG_CONTEXT_SIZE                                                       \
24*bdd1243dSDimitry Andric   (sizeof(RegisterInfoPOSIX_loongarch64::GPR) +                                \
25*bdd1243dSDimitry Andric    sizeof(RegisterInfoPOSIX_loongarch64::FPR))
26*bdd1243dSDimitry Andric 
27*bdd1243dSDimitry Andric #define DECLARE_REGISTER_INFOS_LOONGARCH64_STRUCT
28*bdd1243dSDimitry Andric #include "RegisterInfos_loongarch64.h"
29*bdd1243dSDimitry Andric #undef DECLARE_REGISTER_INFOS_LOONGARCH64_STRUCT
30*bdd1243dSDimitry Andric 
31*bdd1243dSDimitry Andric const lldb_private::RegisterInfo *
GetRegisterInfoPtr(const lldb_private::ArchSpec & target_arch)32*bdd1243dSDimitry Andric RegisterInfoPOSIX_loongarch64::GetRegisterInfoPtr(
33*bdd1243dSDimitry Andric     const lldb_private::ArchSpec &target_arch) {
34*bdd1243dSDimitry Andric   switch (target_arch.GetMachine()) {
35*bdd1243dSDimitry Andric   case llvm::Triple::loongarch64:
36*bdd1243dSDimitry Andric     return g_register_infos_loongarch64;
37*bdd1243dSDimitry Andric   default:
38*bdd1243dSDimitry Andric     assert(false && "Unhandled target architecture.");
39*bdd1243dSDimitry Andric     return nullptr;
40*bdd1243dSDimitry Andric   }
41*bdd1243dSDimitry Andric }
42*bdd1243dSDimitry Andric 
GetRegisterInfoCount(const lldb_private::ArchSpec & target_arch)43*bdd1243dSDimitry Andric uint32_t RegisterInfoPOSIX_loongarch64::GetRegisterInfoCount(
44*bdd1243dSDimitry Andric     const lldb_private::ArchSpec &target_arch) {
45*bdd1243dSDimitry Andric   switch (target_arch.GetMachine()) {
46*bdd1243dSDimitry Andric   case llvm::Triple::loongarch64:
47*bdd1243dSDimitry Andric     return static_cast<uint32_t>(sizeof(g_register_infos_loongarch64) /
48*bdd1243dSDimitry Andric                                  sizeof(g_register_infos_loongarch64[0]));
49*bdd1243dSDimitry Andric   default:
50*bdd1243dSDimitry Andric     assert(false && "Unhandled target architecture.");
51*bdd1243dSDimitry Andric     return 0;
52*bdd1243dSDimitry Andric   }
53*bdd1243dSDimitry Andric }
54*bdd1243dSDimitry Andric 
55*bdd1243dSDimitry Andric // Number of register sets provided by this context.
56*bdd1243dSDimitry Andric enum {
57*bdd1243dSDimitry Andric   k_num_gpr_registers = gpr_last_loongarch - gpr_first_loongarch + 1,
58*bdd1243dSDimitry Andric   k_num_fpr_registers = fpr_last_loongarch - fpr_first_loongarch + 1,
59*bdd1243dSDimitry Andric   k_num_register_sets = 2
60*bdd1243dSDimitry Andric };
61*bdd1243dSDimitry Andric 
62*bdd1243dSDimitry Andric // LoongArch64 general purpose registers.
63*bdd1243dSDimitry Andric static const uint32_t g_gpr_regnums_loongarch64[] = {
64*bdd1243dSDimitry Andric     gpr_r0_loongarch,        gpr_r1_loongarch,        gpr_r2_loongarch,
65*bdd1243dSDimitry Andric     gpr_r3_loongarch,        gpr_r4_loongarch,        gpr_r5_loongarch,
66*bdd1243dSDimitry Andric     gpr_r6_loongarch,        gpr_r7_loongarch,        gpr_r8_loongarch,
67*bdd1243dSDimitry Andric     gpr_r9_loongarch,        gpr_r10_loongarch,       gpr_r11_loongarch,
68*bdd1243dSDimitry Andric     gpr_r12_loongarch,       gpr_r13_loongarch,       gpr_r14_loongarch,
69*bdd1243dSDimitry Andric     gpr_r15_loongarch,       gpr_r16_loongarch,       gpr_r17_loongarch,
70*bdd1243dSDimitry Andric     gpr_r18_loongarch,       gpr_r19_loongarch,       gpr_r20_loongarch,
71*bdd1243dSDimitry Andric     gpr_r21_loongarch,       gpr_r22_loongarch,       gpr_r23_loongarch,
72*bdd1243dSDimitry Andric     gpr_r24_loongarch,       gpr_r25_loongarch,       gpr_r26_loongarch,
73*bdd1243dSDimitry Andric     gpr_r27_loongarch,       gpr_r28_loongarch,       gpr_r29_loongarch,
74*bdd1243dSDimitry Andric     gpr_r30_loongarch,       gpr_r31_loongarch,       gpr_orig_a0_loongarch,
75*bdd1243dSDimitry Andric     gpr_pc_loongarch,        gpr_badv_loongarch,      gpr_reserved0_loongarch,
76*bdd1243dSDimitry Andric     gpr_reserved1_loongarch, gpr_reserved2_loongarch, gpr_reserved3_loongarch,
77*bdd1243dSDimitry Andric     gpr_reserved4_loongarch, gpr_reserved5_loongarch, gpr_reserved6_loongarch,
78*bdd1243dSDimitry Andric     gpr_reserved7_loongarch, gpr_reserved8_loongarch, gpr_reserved9_loongarch,
79*bdd1243dSDimitry Andric     LLDB_INVALID_REGNUM};
80*bdd1243dSDimitry Andric 
81*bdd1243dSDimitry Andric static_assert(((sizeof g_gpr_regnums_loongarch64 /
82*bdd1243dSDimitry Andric                 sizeof g_gpr_regnums_loongarch64[0]) -
83*bdd1243dSDimitry Andric                1) == k_num_gpr_registers,
84*bdd1243dSDimitry Andric               "g_gpr_regnums_loongarch64 has wrong number of register infos");
85*bdd1243dSDimitry Andric 
86*bdd1243dSDimitry Andric // LoongArch64 floating point registers.
87*bdd1243dSDimitry Andric static const uint32_t g_fpr_regnums_loongarch64[] = {
88*bdd1243dSDimitry Andric     fpr_f0_loongarch,   fpr_f1_loongarch,   fpr_f2_loongarch,
89*bdd1243dSDimitry Andric     fpr_f3_loongarch,   fpr_f4_loongarch,   fpr_f5_loongarch,
90*bdd1243dSDimitry Andric     fpr_f6_loongarch,   fpr_f7_loongarch,   fpr_f8_loongarch,
91*bdd1243dSDimitry Andric     fpr_f9_loongarch,   fpr_f10_loongarch,  fpr_f11_loongarch,
92*bdd1243dSDimitry Andric     fpr_f12_loongarch,  fpr_f13_loongarch,  fpr_f14_loongarch,
93*bdd1243dSDimitry Andric     fpr_f15_loongarch,  fpr_f16_loongarch,  fpr_f17_loongarch,
94*bdd1243dSDimitry Andric     fpr_f18_loongarch,  fpr_f19_loongarch,  fpr_f20_loongarch,
95*bdd1243dSDimitry Andric     fpr_f21_loongarch,  fpr_f22_loongarch,  fpr_f23_loongarch,
96*bdd1243dSDimitry Andric     fpr_f24_loongarch,  fpr_f25_loongarch,  fpr_f26_loongarch,
97*bdd1243dSDimitry Andric     fpr_f27_loongarch,  fpr_f28_loongarch,  fpr_f29_loongarch,
98*bdd1243dSDimitry Andric     fpr_f30_loongarch,  fpr_f31_loongarch,  fpr_fcc0_loongarch,
99*bdd1243dSDimitry Andric     fpr_fcc1_loongarch, fpr_fcc2_loongarch, fpr_fcc3_loongarch,
100*bdd1243dSDimitry Andric     fpr_fcc4_loongarch, fpr_fcc5_loongarch, fpr_fcc6_loongarch,
101*bdd1243dSDimitry Andric     fpr_fcc7_loongarch, fpr_fcsr_loongarch, LLDB_INVALID_REGNUM};
102*bdd1243dSDimitry Andric 
103*bdd1243dSDimitry Andric static_assert(((sizeof g_fpr_regnums_loongarch64 /
104*bdd1243dSDimitry Andric                 sizeof g_fpr_regnums_loongarch64[0]) -
105*bdd1243dSDimitry Andric                1) == k_num_fpr_registers,
106*bdd1243dSDimitry Andric               "g_fpr_regnums_loongarch64 has wrong number of register infos");
107*bdd1243dSDimitry Andric 
108*bdd1243dSDimitry Andric // Register sets for LoongArch64.
109*bdd1243dSDimitry Andric static const lldb_private::RegisterSet
110*bdd1243dSDimitry Andric     g_reg_sets_loongarch64[k_num_register_sets] = {
111*bdd1243dSDimitry Andric         {"General Purpose Registers", "gpr", k_num_gpr_registers,
112*bdd1243dSDimitry Andric          g_gpr_regnums_loongarch64},
113*bdd1243dSDimitry Andric         {"Floating Point Registers", "fpr", k_num_fpr_registers,
114*bdd1243dSDimitry Andric          g_fpr_regnums_loongarch64}};
115*bdd1243dSDimitry Andric 
RegisterInfoPOSIX_loongarch64(const lldb_private::ArchSpec & target_arch,lldb_private::Flags flags)116*bdd1243dSDimitry Andric RegisterInfoPOSIX_loongarch64::RegisterInfoPOSIX_loongarch64(
117*bdd1243dSDimitry Andric     const lldb_private::ArchSpec &target_arch, lldb_private::Flags flags)
118*bdd1243dSDimitry Andric     : lldb_private::RegisterInfoAndSetInterface(target_arch),
119*bdd1243dSDimitry Andric       m_register_info_p(GetRegisterInfoPtr(target_arch)),
120*bdd1243dSDimitry Andric       m_register_info_count(GetRegisterInfoCount(target_arch)) {}
121*bdd1243dSDimitry Andric 
GetRegisterCount() const122*bdd1243dSDimitry Andric uint32_t RegisterInfoPOSIX_loongarch64::GetRegisterCount() const {
123*bdd1243dSDimitry Andric   return m_register_info_count;
124*bdd1243dSDimitry Andric }
125*bdd1243dSDimitry Andric 
GetGPRSize() const126*bdd1243dSDimitry Andric size_t RegisterInfoPOSIX_loongarch64::GetGPRSize() const {
127*bdd1243dSDimitry Andric   return sizeof(struct RegisterInfoPOSIX_loongarch64::GPR);
128*bdd1243dSDimitry Andric }
129*bdd1243dSDimitry Andric 
GetFPRSize() const130*bdd1243dSDimitry Andric size_t RegisterInfoPOSIX_loongarch64::GetFPRSize() const {
131*bdd1243dSDimitry Andric   return sizeof(struct RegisterInfoPOSIX_loongarch64::FPR);
132*bdd1243dSDimitry Andric }
133*bdd1243dSDimitry Andric 
134*bdd1243dSDimitry Andric const lldb_private::RegisterInfo *
GetRegisterInfo() const135*bdd1243dSDimitry Andric RegisterInfoPOSIX_loongarch64::GetRegisterInfo() const {
136*bdd1243dSDimitry Andric   return m_register_info_p;
137*bdd1243dSDimitry Andric }
138*bdd1243dSDimitry Andric 
GetRegisterSetCount() const139*bdd1243dSDimitry Andric size_t RegisterInfoPOSIX_loongarch64::GetRegisterSetCount() const {
140*bdd1243dSDimitry Andric   return k_num_register_sets;
141*bdd1243dSDimitry Andric }
142*bdd1243dSDimitry Andric 
GetRegisterSetFromRegisterIndex(uint32_t reg_index) const143*bdd1243dSDimitry Andric size_t RegisterInfoPOSIX_loongarch64::GetRegisterSetFromRegisterIndex(
144*bdd1243dSDimitry Andric     uint32_t reg_index) const {
145*bdd1243dSDimitry Andric   // coverity[unsigned_compare]
146*bdd1243dSDimitry Andric   if (reg_index >= gpr_first_loongarch && reg_index <= gpr_last_loongarch)
147*bdd1243dSDimitry Andric     return GPRegSet;
148*bdd1243dSDimitry Andric   if (reg_index >= fpr_first_loongarch && reg_index <= fpr_last_loongarch)
149*bdd1243dSDimitry Andric     return FPRegSet;
150*bdd1243dSDimitry Andric   return LLDB_INVALID_REGNUM;
151*bdd1243dSDimitry Andric }
152*bdd1243dSDimitry Andric 
153*bdd1243dSDimitry Andric const lldb_private::RegisterSet *
GetRegisterSet(size_t set_index) const154*bdd1243dSDimitry Andric RegisterInfoPOSIX_loongarch64::GetRegisterSet(size_t set_index) const {
155*bdd1243dSDimitry Andric   if (set_index < GetRegisterSetCount())
156*bdd1243dSDimitry Andric     return &g_reg_sets_loongarch64[set_index];
157*bdd1243dSDimitry Andric   return nullptr;
158*bdd1243dSDimitry Andric }
159