xref: /freebsd-src/contrib/llvm-project/lldb/source/Plugins/Instruction/RISCV/RISCVCInstructions.h (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
1*bdd1243dSDimitry Andric //===-- RISCVCInstructions.h ----------------------------------------------===//
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 #ifndef LLDB_SOURCE_PLUGINS_INSTRUCTION_RISCV_RISCVCINSTRUCTION_H
10*bdd1243dSDimitry Andric #define LLDB_SOURCE_PLUGINS_INSTRUCTION_RISCV_RISCVCINSTRUCTION_H
11*bdd1243dSDimitry Andric 
12*bdd1243dSDimitry Andric #include <cstdint>
13*bdd1243dSDimitry Andric #include <variant>
14*bdd1243dSDimitry Andric 
15*bdd1243dSDimitry Andric #include "Plugins/Process/Utility/lldb-riscv-register-enums.h"
16*bdd1243dSDimitry Andric #include "RISCVInstructions.h"
17*bdd1243dSDimitry Andric 
18*bdd1243dSDimitry Andric namespace lldb_private {
19*bdd1243dSDimitry Andric 
20*bdd1243dSDimitry Andric /// Unified RISC-V C register encoding.
21*bdd1243dSDimitry Andric struct RxC {
22*bdd1243dSDimitry Andric   uint32_t rd;
23*bdd1243dSDimitry Andric   bool shift = true;
24*bdd1243dSDimitry Andric   operator int() { return rd; }
RdRxC25*bdd1243dSDimitry Andric   operator Rd() { return Rd{rd + (shift ? 8 : 0)}; }
RsRxC26*bdd1243dSDimitry Andric   operator Rs() { return Rs{rd + (shift ? 8 : 0)}; }
27*bdd1243dSDimitry Andric };
28*bdd1243dSDimitry Andric 
29*bdd1243dSDimitry Andric // decode register for RVC
DecodeCR_RD(uint32_t inst)30*bdd1243dSDimitry Andric constexpr RxC DecodeCR_RD(uint32_t inst) { return RxC{DecodeRD(inst), false}; }
DecodeCI_RD(uint32_t inst)31*bdd1243dSDimitry Andric constexpr RxC DecodeCI_RD(uint32_t inst) { return RxC{DecodeRD(inst), false}; }
DecodeCR_RS1(uint32_t inst)32*bdd1243dSDimitry Andric constexpr RxC DecodeCR_RS1(uint32_t inst) { return RxC{DecodeRD(inst), false}; }
DecodeCI_RS1(uint32_t inst)33*bdd1243dSDimitry Andric constexpr RxC DecodeCI_RS1(uint32_t inst) { return RxC{DecodeRD(inst), false}; }
DecodeCR_RS2(uint32_t inst)34*bdd1243dSDimitry Andric constexpr RxC DecodeCR_RS2(uint32_t inst) {
35*bdd1243dSDimitry Andric   return RxC{(inst & 0x7C) >> 2, false};
36*bdd1243dSDimitry Andric }
37*bdd1243dSDimitry Andric 
DecodeCIW_RD(uint32_t inst)38*bdd1243dSDimitry Andric constexpr RxC DecodeCIW_RD(uint32_t inst) { return RxC{(inst & 0x1C) >> 2}; }
DecodeCL_RD(uint32_t inst)39*bdd1243dSDimitry Andric constexpr RxC DecodeCL_RD(uint32_t inst) { return RxC{DecodeCIW_RD(inst)}; }
DecodeCA_RD(uint32_t inst)40*bdd1243dSDimitry Andric constexpr RxC DecodeCA_RD(uint32_t inst) { return RxC{(inst & 0x380) >> 7}; }
DecodeCB_RD(uint32_t inst)41*bdd1243dSDimitry Andric constexpr RxC DecodeCB_RD(uint32_t inst) { return RxC{DecodeCA_RD(inst)}; }
42*bdd1243dSDimitry Andric 
DecodeCL_RS1(uint32_t inst)43*bdd1243dSDimitry Andric constexpr RxC DecodeCL_RS1(uint32_t inst) { return RxC{DecodeCA_RD(inst)}; }
DecodeCS_RS1(uint32_t inst)44*bdd1243dSDimitry Andric constexpr RxC DecodeCS_RS1(uint32_t inst) { return RxC{DecodeCA_RD(inst)}; }
DecodeCA_RS1(uint32_t inst)45*bdd1243dSDimitry Andric constexpr RxC DecodeCA_RS1(uint32_t inst) { return RxC{DecodeCA_RD(inst)}; }
DecodeCB_RS1(uint32_t inst)46*bdd1243dSDimitry Andric constexpr RxC DecodeCB_RS1(uint32_t inst) { return RxC{DecodeCA_RD(inst)}; }
47*bdd1243dSDimitry Andric 
DecodeCSS_RS2(uint32_t inst)48*bdd1243dSDimitry Andric constexpr RxC DecodeCSS_RS2(uint32_t inst) { return DecodeCR_RS2(inst); }
DecodeCS_RS2(uint32_t inst)49*bdd1243dSDimitry Andric constexpr RxC DecodeCS_RS2(uint32_t inst) { return RxC{DecodeCIW_RD(inst)}; }
DecodeCA_RS2(uint32_t inst)50*bdd1243dSDimitry Andric constexpr RxC DecodeCA_RS2(uint32_t inst) { return RxC{DecodeCIW_RD(inst)}; }
51*bdd1243dSDimitry Andric 
DecodeC_LWSP(uint32_t inst)52*bdd1243dSDimitry Andric RISCVInst DecodeC_LWSP(uint32_t inst) {
53*bdd1243dSDimitry Andric   auto rd = DecodeCI_RD(inst);
54*bdd1243dSDimitry Andric   uint16_t offset = ((inst << 4) & 0xc0)    // offset[7:6]
55*bdd1243dSDimitry Andric                     | ((inst >> 7) & 0x20)  // offset[5]
56*bdd1243dSDimitry Andric                     | ((inst >> 2) & 0x1c); // offset[4:2]
57*bdd1243dSDimitry Andric   if (rd == 0)
58*bdd1243dSDimitry Andric     return RESERVED{inst};
59*bdd1243dSDimitry Andric   return LW{rd, Rs{gpr_sp_riscv}, uint32_t(offset)};
60*bdd1243dSDimitry Andric }
61*bdd1243dSDimitry Andric 
DecodeC_LDSP(uint32_t inst)62*bdd1243dSDimitry Andric RISCVInst DecodeC_LDSP(uint32_t inst) {
63*bdd1243dSDimitry Andric   auto rd = DecodeCI_RD(inst);
64*bdd1243dSDimitry Andric   uint16_t offset = ((inst << 4) & 0x1c0)   // offset[8:6]
65*bdd1243dSDimitry Andric                     | ((inst >> 7) & 0x20)  // offset[5]
66*bdd1243dSDimitry Andric                     | ((inst >> 2) & 0x18); // offset[4:3]
67*bdd1243dSDimitry Andric   if (rd == 0)
68*bdd1243dSDimitry Andric     return RESERVED{inst};
69*bdd1243dSDimitry Andric   return LD{rd, Rs{gpr_sp_riscv}, uint32_t(offset)};
70*bdd1243dSDimitry Andric }
71*bdd1243dSDimitry Andric 
DecodeC_SWSP(uint32_t inst)72*bdd1243dSDimitry Andric RISCVInst DecodeC_SWSP(uint32_t inst) {
73*bdd1243dSDimitry Andric   uint16_t offset = ((inst >> 1) & 0xc0)    // offset[7:6]
74*bdd1243dSDimitry Andric                     | ((inst >> 7) & 0x3c); // offset[5:2]
75*bdd1243dSDimitry Andric   return SW{Rs{gpr_sp_riscv}, DecodeCSS_RS2(inst), uint32_t(offset)};
76*bdd1243dSDimitry Andric }
77*bdd1243dSDimitry Andric 
DecodeC_SDSP(uint32_t inst)78*bdd1243dSDimitry Andric RISCVInst DecodeC_SDSP(uint32_t inst) {
79*bdd1243dSDimitry Andric   uint16_t offset = ((inst >> 1) & 0x1c0)   // offset[8:6]
80*bdd1243dSDimitry Andric                     | ((inst >> 7) & 0x38); // offset[5:3]
81*bdd1243dSDimitry Andric   return SD{Rs{gpr_sp_riscv}, DecodeCSS_RS2(inst), uint32_t(offset)};
82*bdd1243dSDimitry Andric }
83*bdd1243dSDimitry Andric 
DecodeC_LW(uint32_t inst)84*bdd1243dSDimitry Andric RISCVInst DecodeC_LW(uint32_t inst) {
85*bdd1243dSDimitry Andric   uint16_t offset = ((inst << 1) & 0x40)   // imm[6]
86*bdd1243dSDimitry Andric                     | ((inst >> 7) & 0x38) // imm[5:3]
87*bdd1243dSDimitry Andric                     | ((inst >> 4) & 0x4); // imm[2]
88*bdd1243dSDimitry Andric   return LW{DecodeCL_RD(inst), DecodeCL_RS1(inst), uint32_t(offset)};
89*bdd1243dSDimitry Andric }
90*bdd1243dSDimitry Andric 
DecodeC_LD(uint32_t inst)91*bdd1243dSDimitry Andric RISCVInst DecodeC_LD(uint32_t inst) {
92*bdd1243dSDimitry Andric   uint16_t offset = ((inst << 1) & 0xc0)    // imm[7:6]
93*bdd1243dSDimitry Andric                     | ((inst >> 7) & 0x38); // imm[5:3]
94*bdd1243dSDimitry Andric   return LD{DecodeCL_RD(inst), DecodeCL_RS1(inst), uint32_t(offset)};
95*bdd1243dSDimitry Andric }
96*bdd1243dSDimitry Andric 
DecodeC_SW(uint32_t inst)97*bdd1243dSDimitry Andric RISCVInst DecodeC_SW(uint32_t inst) {
98*bdd1243dSDimitry Andric   uint16_t offset = ((inst << 1) & 0x40)   // imm[6]
99*bdd1243dSDimitry Andric                     | ((inst >> 7) & 0x38) // imm[5:3]
100*bdd1243dSDimitry Andric                     | ((inst >> 4) & 0x4); // imm[2]
101*bdd1243dSDimitry Andric   return SW{DecodeCS_RS1(inst), DecodeCS_RS2(inst), uint32_t(offset)};
102*bdd1243dSDimitry Andric }
103*bdd1243dSDimitry Andric 
DecodeC_SD(uint32_t inst)104*bdd1243dSDimitry Andric RISCVInst DecodeC_SD(uint32_t inst) {
105*bdd1243dSDimitry Andric   uint16_t offset = ((inst << 1) & 0xc0)    // imm[7:6]
106*bdd1243dSDimitry Andric                     | ((inst >> 7) & 0x38); // imm[5:3]
107*bdd1243dSDimitry Andric   return SD{DecodeCS_RS1(inst), DecodeCS_RS2(inst), uint32_t(offset)};
108*bdd1243dSDimitry Andric }
109*bdd1243dSDimitry Andric 
DecodeC_J(uint32_t inst)110*bdd1243dSDimitry Andric RISCVInst DecodeC_J(uint32_t inst) {
111*bdd1243dSDimitry Andric   uint16_t offset = ((inst >> 1) & 0x800)   // offset[11]
112*bdd1243dSDimitry Andric                     | ((inst << 2) & 0x400) // offset[10]
113*bdd1243dSDimitry Andric                     | ((inst >> 1) & 0x300) // offset[9:8]
114*bdd1243dSDimitry Andric                     | ((inst << 1) & 0x80)  // offset[7]
115*bdd1243dSDimitry Andric                     | ((inst >> 1) & 0x40)  // offset[6]
116*bdd1243dSDimitry Andric                     | ((inst << 3) & 0x20)  // offset[5]
117*bdd1243dSDimitry Andric                     | ((inst >> 7) & 0x10)  // offset[4]
118*bdd1243dSDimitry Andric                     | ((inst >> 2) & 0xe);  // offset[3:1]
119*bdd1243dSDimitry Andric   if ((offset & 0x800) == 0)
120*bdd1243dSDimitry Andric     return JAL{Rd{0}, uint32_t(offset)};
121*bdd1243dSDimitry Andric   return JAL{Rd{0}, uint32_t(int32_t(int16_t(offset | 0xf000)))};
122*bdd1243dSDimitry Andric }
123*bdd1243dSDimitry Andric 
DecodeC_JR(uint32_t inst)124*bdd1243dSDimitry Andric RISCVInst DecodeC_JR(uint32_t inst) {
125*bdd1243dSDimitry Andric   auto rs1 = DecodeCR_RS1(inst);
126*bdd1243dSDimitry Andric   if (rs1 == 0)
127*bdd1243dSDimitry Andric     return RESERVED{inst};
128*bdd1243dSDimitry Andric   return JALR{Rd{0}, rs1, 0};
129*bdd1243dSDimitry Andric }
130*bdd1243dSDimitry Andric 
DecodeC_JALR(uint32_t inst)131*bdd1243dSDimitry Andric RISCVInst DecodeC_JALR(uint32_t inst) {
132*bdd1243dSDimitry Andric   auto rs1 = DecodeCR_RS1(inst);
133*bdd1243dSDimitry Andric   if (rs1 == 0)
134*bdd1243dSDimitry Andric     return EBREAK{inst};
135*bdd1243dSDimitry Andric   return JALR{Rd{1}, rs1, 0};
136*bdd1243dSDimitry Andric }
137*bdd1243dSDimitry Andric 
BOffset(uint32_t inst)138*bdd1243dSDimitry Andric constexpr uint16_t BOffset(uint32_t inst) {
139*bdd1243dSDimitry Andric   return ((inst >> 4) & 0x100)  // offset[8]
140*bdd1243dSDimitry Andric          | ((inst << 1) & 0xc0) // offset[7:6]
141*bdd1243dSDimitry Andric          | ((inst << 3) & 0x20) // offset[5]
142*bdd1243dSDimitry Andric          | ((inst >> 7) & 0x18) // offset[4:3]
143*bdd1243dSDimitry Andric          | ((inst >> 2) & 0x6); // offset[2:1]
144*bdd1243dSDimitry Andric }
145*bdd1243dSDimitry Andric 
DecodeC_BNEZ(uint32_t inst)146*bdd1243dSDimitry Andric RISCVInst DecodeC_BNEZ(uint32_t inst) {
147*bdd1243dSDimitry Andric   auto rs1 = DecodeCB_RS1(inst);
148*bdd1243dSDimitry Andric   uint16_t offset = BOffset(inst);
149*bdd1243dSDimitry Andric   if ((offset & 0x100) == 0)
150*bdd1243dSDimitry Andric     return B{rs1, Rs{0}, uint32_t(offset), 0b001};
151*bdd1243dSDimitry Andric   return B{rs1, Rs{0}, uint32_t(int32_t(int16_t(offset | 0xfe00))), 0b001};
152*bdd1243dSDimitry Andric }
153*bdd1243dSDimitry Andric 
DecodeC_BEQZ(uint32_t inst)154*bdd1243dSDimitry Andric RISCVInst DecodeC_BEQZ(uint32_t inst) {
155*bdd1243dSDimitry Andric   auto rs1 = DecodeCB_RS1(inst);
156*bdd1243dSDimitry Andric   uint16_t offset = BOffset(inst);
157*bdd1243dSDimitry Andric   if ((offset & 0x100) == 0)
158*bdd1243dSDimitry Andric     return B{rs1, Rs{0}, uint32_t(offset), 0b000};
159*bdd1243dSDimitry Andric   return B{rs1, Rs{0}, uint32_t(int32_t(int16_t(offset | 0xfe00))), 0b000};
160*bdd1243dSDimitry Andric }
161*bdd1243dSDimitry Andric 
DecodeC_LI(uint32_t inst)162*bdd1243dSDimitry Andric RISCVInst DecodeC_LI(uint32_t inst) {
163*bdd1243dSDimitry Andric   auto rd = DecodeCI_RD(inst);
164*bdd1243dSDimitry Andric   uint16_t imm = ((inst >> 7) & 0x20) | ((inst >> 2) & 0x1f);
165*bdd1243dSDimitry Andric   if ((imm & 0x20) == 0)
166*bdd1243dSDimitry Andric     return ADDI{rd, Rs{0}, uint32_t(imm)};
167*bdd1243dSDimitry Andric   return ADDI{rd, Rs{0}, uint32_t(int32_t(int8_t(imm | 0xc0)))};
168*bdd1243dSDimitry Andric }
169*bdd1243dSDimitry Andric 
DecodeC_LUI_ADDI16SP(uint32_t inst)170*bdd1243dSDimitry Andric RISCVInst DecodeC_LUI_ADDI16SP(uint32_t inst) {
171*bdd1243dSDimitry Andric   auto rd = DecodeCI_RD(inst);
172*bdd1243dSDimitry Andric   if (rd == 0)
173*bdd1243dSDimitry Andric     return HINT{inst};
174*bdd1243dSDimitry Andric   if (rd == 2) {
175*bdd1243dSDimitry Andric     uint16_t nzimm = ((inst >> 3) & 0x200)   // nzimm[9]
176*bdd1243dSDimitry Andric                      | ((inst >> 2) & 0x10)  // nzimm[4]
177*bdd1243dSDimitry Andric                      | ((inst << 1) & 0x40)  // nzimm[6]
178*bdd1243dSDimitry Andric                      | ((inst << 4) & 0x180) // nzimm[8:7]
179*bdd1243dSDimitry Andric                      | ((inst << 3) & 0x20); // nzimm[5]
180*bdd1243dSDimitry Andric     if (nzimm == 0)
181*bdd1243dSDimitry Andric       return RESERVED{inst};
182*bdd1243dSDimitry Andric     if ((nzimm & 0x200) == 0)
183*bdd1243dSDimitry Andric       return ADDI{Rd{gpr_sp_riscv}, Rs{gpr_sp_riscv}, uint32_t(nzimm)};
184*bdd1243dSDimitry Andric     return ADDI{Rd{gpr_sp_riscv}, Rs{gpr_sp_riscv},
185*bdd1243dSDimitry Andric                 uint32_t(int32_t(int16_t(nzimm | 0xfc00)))};
186*bdd1243dSDimitry Andric   }
187*bdd1243dSDimitry Andric   uint32_t imm =
188*bdd1243dSDimitry Andric       ((uint32_t(inst) << 5) & 0x20000) | ((uint32_t(inst) << 10) & 0x1f000);
189*bdd1243dSDimitry Andric   if ((imm & 0x20000) == 0)
190*bdd1243dSDimitry Andric     return LUI{rd, imm};
191*bdd1243dSDimitry Andric   return LUI{rd, uint32_t(int32_t(imm | 0xfffc0000))};
192*bdd1243dSDimitry Andric }
193*bdd1243dSDimitry Andric 
DecodeC_ADDI(uint32_t inst)194*bdd1243dSDimitry Andric RISCVInst DecodeC_ADDI(uint32_t inst) {
195*bdd1243dSDimitry Andric   auto rd = DecodeCI_RD(inst);
196*bdd1243dSDimitry Andric   if (rd == 0)
197*bdd1243dSDimitry Andric     return NOP{inst};
198*bdd1243dSDimitry Andric   uint16_t imm = ((inst >> 7) & 0x20) | ((inst >> 2) & 0x1f);
199*bdd1243dSDimitry Andric   if ((imm & 0x20) == 0)
200*bdd1243dSDimitry Andric     return ADDI{rd, rd, uint32_t(imm)};
201*bdd1243dSDimitry Andric   return ADDI{rd, rd, uint32_t(int32_t(int8_t(imm | 0xc0)))};
202*bdd1243dSDimitry Andric }
203*bdd1243dSDimitry Andric 
DecodeC_ADDIW(uint32_t inst)204*bdd1243dSDimitry Andric RISCVInst DecodeC_ADDIW(uint32_t inst) {
205*bdd1243dSDimitry Andric   auto rd = DecodeCI_RD(inst);
206*bdd1243dSDimitry Andric   if (rd == 0)
207*bdd1243dSDimitry Andric     return RESERVED{inst};
208*bdd1243dSDimitry Andric   uint16_t imm = ((inst >> 7) & 0x20) | ((inst >> 2) & 0x1f);
209*bdd1243dSDimitry Andric   if ((imm & 0x20) == 0)
210*bdd1243dSDimitry Andric     return ADDIW{rd, rd, uint32_t(imm)};
211*bdd1243dSDimitry Andric   return ADDIW{rd, rd, uint32_t(int32_t(int8_t(imm | 0xc0)))};
212*bdd1243dSDimitry Andric }
213*bdd1243dSDimitry Andric 
DecodeC_ADDI4SPN(uint32_t inst)214*bdd1243dSDimitry Andric RISCVInst DecodeC_ADDI4SPN(uint32_t inst) {
215*bdd1243dSDimitry Andric   auto rd = DecodeCIW_RD(inst);
216*bdd1243dSDimitry Andric   uint16_t nzuimm = ((inst >> 1) & 0x3c0)  // nzuimm[9:6]
217*bdd1243dSDimitry Andric                     | ((inst >> 7) & 0x30) // nzuimm[5:4]
218*bdd1243dSDimitry Andric                     | ((inst >> 2) & 0x8)  // nzuimm[3]
219*bdd1243dSDimitry Andric                     | ((inst >> 4) & 0x4); // nzuimm[2]
220*bdd1243dSDimitry Andric 
221*bdd1243dSDimitry Andric   if (rd == 0 && nzuimm == 0)
222*bdd1243dSDimitry Andric     return INVALID{inst};
223*bdd1243dSDimitry Andric   if (nzuimm == 0)
224*bdd1243dSDimitry Andric     return RESERVED{inst};
225*bdd1243dSDimitry Andric   return ADDI{rd, Rs{gpr_sp_riscv}, uint32_t(nzuimm)};
226*bdd1243dSDimitry Andric }
227*bdd1243dSDimitry Andric 
DecodeC_SLLI(uint32_t inst)228*bdd1243dSDimitry Andric RISCVInst DecodeC_SLLI(uint32_t inst) {
229*bdd1243dSDimitry Andric   auto rd = DecodeCI_RD(inst);
230*bdd1243dSDimitry Andric   uint16_t shamt = ((inst >> 7) & 0x20) | ((inst >> 2) & 0x1f);
231*bdd1243dSDimitry Andric   if (rd == 0 || shamt == 0)
232*bdd1243dSDimitry Andric     return HINT{inst};
233*bdd1243dSDimitry Andric   return SLLI{rd, rd, uint8_t(shamt)};
234*bdd1243dSDimitry Andric }
235*bdd1243dSDimitry Andric 
DecodeC_SRLI(uint32_t inst)236*bdd1243dSDimitry Andric RISCVInst DecodeC_SRLI(uint32_t inst) {
237*bdd1243dSDimitry Andric   auto rd = DecodeCB_RD(inst);
238*bdd1243dSDimitry Andric   uint16_t shamt = ((inst >> 7) & 0x20) | ((inst >> 2) & 0x1f);
239*bdd1243dSDimitry Andric   if (shamt == 0)
240*bdd1243dSDimitry Andric     return HINT{inst};
241*bdd1243dSDimitry Andric   return SRLI{rd, rd, uint8_t(shamt)};
242*bdd1243dSDimitry Andric }
243*bdd1243dSDimitry Andric 
DecodeC_SRAI(uint32_t inst)244*bdd1243dSDimitry Andric RISCVInst DecodeC_SRAI(uint32_t inst) {
245*bdd1243dSDimitry Andric   auto rd = DecodeCB_RD(inst);
246*bdd1243dSDimitry Andric   uint16_t shamt = ((inst >> 7) & 0x20) | ((inst >> 2) & 0x1f);
247*bdd1243dSDimitry Andric   if (shamt == 0)
248*bdd1243dSDimitry Andric     return HINT{inst};
249*bdd1243dSDimitry Andric   return SRAI{rd, rd, uint8_t(shamt)};
250*bdd1243dSDimitry Andric }
251*bdd1243dSDimitry Andric 
DecodeC_ANDI(uint32_t inst)252*bdd1243dSDimitry Andric RISCVInst DecodeC_ANDI(uint32_t inst) {
253*bdd1243dSDimitry Andric   auto rd = DecodeCB_RD(inst);
254*bdd1243dSDimitry Andric   uint16_t imm = ((inst >> 7) & 0x20) | ((inst >> 2) & 0x1f);
255*bdd1243dSDimitry Andric   if ((imm & 0x20) == 0)
256*bdd1243dSDimitry Andric     return ANDI{rd, rd, uint32_t(imm)};
257*bdd1243dSDimitry Andric   return ANDI{rd, rd, uint32_t(int32_t(int8_t(imm | 0xc0)))};
258*bdd1243dSDimitry Andric }
259*bdd1243dSDimitry Andric 
DecodeC_MV(uint32_t inst)260*bdd1243dSDimitry Andric RISCVInst DecodeC_MV(uint32_t inst) {
261*bdd1243dSDimitry Andric   auto rd = DecodeCR_RD(inst);
262*bdd1243dSDimitry Andric   auto rs2 = DecodeCR_RS2(inst);
263*bdd1243dSDimitry Andric   if (rd == 0)
264*bdd1243dSDimitry Andric     return HINT{inst};
265*bdd1243dSDimitry Andric   return ADD{rd, Rs{0}, rs2};
266*bdd1243dSDimitry Andric }
267*bdd1243dSDimitry Andric 
DecodeC_ADD(uint32_t inst)268*bdd1243dSDimitry Andric RISCVInst DecodeC_ADD(uint32_t inst) {
269*bdd1243dSDimitry Andric   auto rd = DecodeCR_RD(inst);
270*bdd1243dSDimitry Andric   return ADD{rd, rd, DecodeCR_RS2(inst)};
271*bdd1243dSDimitry Andric }
272*bdd1243dSDimitry Andric 
DecodeC_AND(uint32_t inst)273*bdd1243dSDimitry Andric RISCVInst DecodeC_AND(uint32_t inst) {
274*bdd1243dSDimitry Andric   auto rd = DecodeCA_RD(inst);
275*bdd1243dSDimitry Andric   return AND{rd, rd, DecodeCA_RS2(inst)};
276*bdd1243dSDimitry Andric }
277*bdd1243dSDimitry Andric 
DecodeC_OR(uint32_t inst)278*bdd1243dSDimitry Andric RISCVInst DecodeC_OR(uint32_t inst) {
279*bdd1243dSDimitry Andric   auto rd = DecodeCA_RD(inst);
280*bdd1243dSDimitry Andric   return OR{rd, rd, DecodeCA_RS2(inst)};
281*bdd1243dSDimitry Andric }
282*bdd1243dSDimitry Andric 
DecodeC_XOR(uint32_t inst)283*bdd1243dSDimitry Andric RISCVInst DecodeC_XOR(uint32_t inst) {
284*bdd1243dSDimitry Andric   auto rd = DecodeCA_RD(inst);
285*bdd1243dSDimitry Andric   return XOR{rd, rd, DecodeCA_RS2(inst)};
286*bdd1243dSDimitry Andric }
287*bdd1243dSDimitry Andric 
DecodeC_SUB(uint32_t inst)288*bdd1243dSDimitry Andric RISCVInst DecodeC_SUB(uint32_t inst) {
289*bdd1243dSDimitry Andric   auto rd = DecodeCA_RD(inst);
290*bdd1243dSDimitry Andric   return SUB{rd, rd, DecodeCA_RS2(inst)};
291*bdd1243dSDimitry Andric }
292*bdd1243dSDimitry Andric 
DecodeC_SUBW(uint32_t inst)293*bdd1243dSDimitry Andric RISCVInst DecodeC_SUBW(uint32_t inst) {
294*bdd1243dSDimitry Andric   auto rd = DecodeCA_RD(inst);
295*bdd1243dSDimitry Andric   return SUBW{rd, rd, DecodeCA_RS2(inst)};
296*bdd1243dSDimitry Andric }
297*bdd1243dSDimitry Andric 
DecodeC_ADDW(uint32_t inst)298*bdd1243dSDimitry Andric RISCVInst DecodeC_ADDW(uint32_t inst) {
299*bdd1243dSDimitry Andric   auto rd = DecodeCA_RD(inst);
300*bdd1243dSDimitry Andric   return ADDW{rd, rd, DecodeCA_RS2(inst)};
301*bdd1243dSDimitry Andric }
DecodeC_FLW(uint32_t inst)302*bdd1243dSDimitry Andric RISCVInst DecodeC_FLW(uint32_t inst) {
303*bdd1243dSDimitry Andric   uint16_t offset = ((inst << 1) & 0x40)   // imm[6]
304*bdd1243dSDimitry Andric                     | ((inst >> 7) & 0x38) // imm[5:3]
305*bdd1243dSDimitry Andric                     | ((inst >> 4) & 0x4); // imm[2]
306*bdd1243dSDimitry Andric   return FLW{DecodeCL_RD(inst), DecodeCL_RS1(inst), uint32_t(offset)};
307*bdd1243dSDimitry Andric }
308*bdd1243dSDimitry Andric 
DecodeC_FSW(uint32_t inst)309*bdd1243dSDimitry Andric RISCVInst DecodeC_FSW(uint32_t inst) {
310*bdd1243dSDimitry Andric   uint16_t offset = ((inst << 1) & 0x40)   // imm[6]
311*bdd1243dSDimitry Andric                     | ((inst >> 7) & 0x38) // imm[5:3]
312*bdd1243dSDimitry Andric                     | ((inst >> 4) & 0x4); // imm[2]
313*bdd1243dSDimitry Andric   return FSW{DecodeCS_RS1(inst), DecodeCS_RS2(inst), uint32_t(offset)};
314*bdd1243dSDimitry Andric }
315*bdd1243dSDimitry Andric 
DecodeC_FLWSP(uint32_t inst)316*bdd1243dSDimitry Andric RISCVInst DecodeC_FLWSP(uint32_t inst) {
317*bdd1243dSDimitry Andric   auto rd = DecodeCI_RD(inst);
318*bdd1243dSDimitry Andric   uint16_t offset = ((inst << 4) & 0xc0)    // offset[7:6]
319*bdd1243dSDimitry Andric                     | ((inst >> 7) & 0x20)  // offset[5]
320*bdd1243dSDimitry Andric                     | ((inst >> 2) & 0x1c); // offset[4:2]
321*bdd1243dSDimitry Andric   return FLW{rd, Rs{gpr_sp_riscv}, uint32_t(offset)};
322*bdd1243dSDimitry Andric }
323*bdd1243dSDimitry Andric 
DecodeC_FSWSP(uint32_t inst)324*bdd1243dSDimitry Andric RISCVInst DecodeC_FSWSP(uint32_t inst) {
325*bdd1243dSDimitry Andric   uint16_t offset = ((inst >> 1) & 0xc0)    // offset[7:6]
326*bdd1243dSDimitry Andric                     | ((inst >> 7) & 0x3c); // offset[5:2]
327*bdd1243dSDimitry Andric   return FSW{Rs{gpr_sp_riscv}, DecodeCSS_RS2(inst), uint32_t(offset)};
328*bdd1243dSDimitry Andric }
329*bdd1243dSDimitry Andric 
DecodeC_FLDSP(uint32_t inst)330*bdd1243dSDimitry Andric RISCVInst DecodeC_FLDSP(uint32_t inst) {
331*bdd1243dSDimitry Andric   auto rd = DecodeCI_RD(inst);
332*bdd1243dSDimitry Andric   uint16_t offset = ((inst << 4) & 0x1c0)   // offset[8:6]
333*bdd1243dSDimitry Andric                     | ((inst >> 7) & 0x20)  // offset[5]
334*bdd1243dSDimitry Andric                     | ((inst >> 2) & 0x18); // offset[4:3]
335*bdd1243dSDimitry Andric   return FLD{rd, Rs{gpr_sp_riscv}, uint32_t(offset)};
336*bdd1243dSDimitry Andric }
337*bdd1243dSDimitry Andric 
DecodeC_FSDSP(uint32_t inst)338*bdd1243dSDimitry Andric RISCVInst DecodeC_FSDSP(uint32_t inst) {
339*bdd1243dSDimitry Andric   uint16_t offset = ((inst >> 1) & 0x1c0)   // offset[8:6]
340*bdd1243dSDimitry Andric                     | ((inst >> 7) & 0x38); // offset[5:3]
341*bdd1243dSDimitry Andric   return FSD{Rs{gpr_sp_riscv}, DecodeCSS_RS2(inst), uint32_t(offset)};
342*bdd1243dSDimitry Andric }
343*bdd1243dSDimitry Andric 
DecodeC_FLD(uint32_t inst)344*bdd1243dSDimitry Andric RISCVInst DecodeC_FLD(uint32_t inst) {
345*bdd1243dSDimitry Andric   uint16_t offset = ((inst << 1) & 0xc0)    // imm[7:6]
346*bdd1243dSDimitry Andric                     | ((inst >> 7) & 0x38); // imm[5:3]
347*bdd1243dSDimitry Andric   return FLD{DecodeCL_RD(inst), DecodeCL_RS1(inst), uint32_t(offset)};
348*bdd1243dSDimitry Andric }
349*bdd1243dSDimitry Andric 
DecodeC_FSD(uint32_t inst)350*bdd1243dSDimitry Andric RISCVInst DecodeC_FSD(uint32_t inst) {
351*bdd1243dSDimitry Andric   uint16_t offset = ((inst << 1) & 0xc0)    // imm[7:6]
352*bdd1243dSDimitry Andric                     | ((inst >> 7) & 0x38); // imm[5:3]
353*bdd1243dSDimitry Andric   return FSD{DecodeCS_RS1(inst), DecodeCS_RS2(inst), uint32_t(offset)};
354*bdd1243dSDimitry Andric }
355*bdd1243dSDimitry Andric 
356*bdd1243dSDimitry Andric } // namespace lldb_private
357*bdd1243dSDimitry Andric #endif // LLDB_SOURCE_PLUGINS_INSTRUCTION_RISCV_RISCVCINSTRUCTION_H
358