Coverage Report

Created: 2026-03-13 06:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/capstonenext/arch/RISCV/RISCVDisassembler.c
Line
Count
Source
1
/* Capstone Disassembly Engine, http://www.capstone-engine.org */
2
/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2022, */
3
/*    Rot127 <unisono@quyllur.org> 2022-2023 */
4
/* Automatically translated source file from LLVM. */
5
6
/* LLVM-commit: <commit> */
7
/* LLVM-tag: <tag> */
8
9
/* Only small edits allowed. */
10
/* For multiple similar edits, please create a Patch for the translator. */
11
12
/* Capstone's C++ file translator: */
13
/* https://github.com/capstone-engine/capstone/tree/next/suite/auto-sync */
14
15
//===-- RISCVDisassembler.cpp - Disassembler for RISC-V -------------------===//
16
//
17
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
18
// See https://llvm.org/LICENSE.txt for license information.
19
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
20
//
21
//===----------------------------------------------------------------------===//
22
//
23
// This file implements the RISCVDisassembler class.
24
//
25
//===----------------------------------------------------------------------===//
26
27
#include <stdio.h>
28
#include <string.h>
29
#include <stdlib.h>
30
#include <capstone/platform.h>
31
32
#include "../../MCInst.h"
33
#include "../../MathExtras.h"
34
#include "../../MCInstPrinter.h"
35
#include "../../MCDisassembler.h"
36
#include "../../MCFixedLenDisassembler.h"
37
#include "../../cs_priv.h"
38
#include "../../utils.h"
39
#include "RISCVDisassemblerExtension.h"
40
#include "RISCVBaseInfo.h"
41
42
#define GET_SUBTARGETINFO_ENUM
43
#include "RISCVGenSubtargetInfo.inc"
44
45
#define GET_REGINFO_ENUM
46
#include "RISCVGenRegisterInfo.inc"
47
48
#define GET_INSTRINFO_ENUM
49
#define GET_INSTRINFO_MC_DESC
50
#include "RISCVGenInstrInfo.inc"
51
52
7.13k
#define CONCAT(a, b) CONCAT_(a, b)
53
7.13k
#define CONCAT_(a, b) a##_##b
54
55
#define DEBUG_TYPE "riscv-disassembler"
56
57
DecodeStatus RISCV_getInstruction(MCInst *Instr, uint16_t *Size,
58
          const uint8_t *Bytes, size_t BytesLen,
59
          uint64_t Address, SStream *CStream);
60
void addSPOperands(MCInst *MI);
61
;
62
// end anonymous namespace
63
64
static DecodeStatus DecodeGPRRegisterClass(MCInst *Inst, uint32_t RegNo,
65
             uint64_t Address,
66
             const void *Decoder)
67
50.9k
{
68
50.9k
  bool IsRVE = RISCV_getFeatureBits(Inst->csh->mode, RISCV_FeatureRVE);
69
70
50.9k
  if (RegNo >= 32 || (IsRVE && RegNo >= 16))
71
0
    return MCDisassembler_Fail;
72
73
50.9k
  MCRegister Reg = RISCV_X0 + RegNo;
74
50.9k
  MCOperand_CreateReg0(Inst, (Reg));
75
50.9k
  return MCDisassembler_Success;
76
50.9k
}
77
78
static DecodeStatus DecodeGPRX1X5RegisterClass(MCInst *Inst, uint32_t RegNo,
79
                 uint64_t Address,
80
                 const void *Decoder)
81
0
{
82
0
  MCRegister Reg = RISCV_X0 + RegNo;
83
0
  if (Reg != RISCV_X1 && Reg != RISCV_X5)
84
0
    return MCDisassembler_Fail;
85
86
0
  MCOperand_CreateReg0(Inst, (Reg));
87
0
  return MCDisassembler_Success;
88
0
}
89
90
static DecodeStatus DecodeFPR16RegisterClass(MCInst *Inst, uint32_t RegNo,
91
               uint64_t Address,
92
               const void *Decoder)
93
4.53k
{
94
4.53k
  if (RegNo >= 32)
95
0
    return MCDisassembler_Fail;
96
97
4.53k
  MCRegister Reg = RISCV_F0_H + RegNo;
98
4.53k
  MCOperand_CreateReg0(Inst, (Reg));
99
4.53k
  return MCDisassembler_Success;
100
4.53k
}
101
102
static DecodeStatus DecodeFPR32RegisterClass(MCInst *Inst, uint32_t RegNo,
103
               uint64_t Address,
104
               const void *Decoder)
105
1.49k
{
106
1.49k
  if (RegNo >= 32)
107
0
    return MCDisassembler_Fail;
108
109
1.49k
  MCRegister Reg = RISCV_F0_F + RegNo;
110
1.49k
  MCOperand_CreateReg0(Inst, (Reg));
111
1.49k
  return MCDisassembler_Success;
112
1.49k
}
113
114
static DecodeStatus DecodeFPR32CRegisterClass(MCInst *Inst, uint32_t RegNo,
115
                uint64_t Address,
116
                const void *Decoder)
117
0
{
118
0
  if (RegNo >= 8) {
119
0
    return MCDisassembler_Fail;
120
0
  }
121
0
  MCRegister Reg = RISCV_F8_F + RegNo;
122
0
  MCOperand_CreateReg0(Inst, (Reg));
123
0
  return MCDisassembler_Success;
124
0
}
125
126
static DecodeStatus DecodeFPR64RegisterClass(MCInst *Inst, uint32_t RegNo,
127
               uint64_t Address,
128
               const void *Decoder)
129
0
{
130
0
  if (RegNo >= 32)
131
0
    return MCDisassembler_Fail;
132
133
0
  MCRegister Reg = RISCV_F0_D + RegNo;
134
0
  MCOperand_CreateReg0(Inst, (Reg));
135
0
  return MCDisassembler_Success;
136
0
}
137
138
static DecodeStatus DecodeFPR64CRegisterClass(MCInst *Inst, uint32_t RegNo,
139
                uint64_t Address,
140
                const void *Decoder)
141
0
{
142
0
  if (RegNo >= 8) {
143
0
    return MCDisassembler_Fail;
144
0
  }
145
0
  MCRegister Reg = RISCV_F8_D + RegNo;
146
0
  MCOperand_CreateReg0(Inst, (Reg));
147
0
  return MCDisassembler_Success;
148
0
}
149
150
static DecodeStatus DecodeGPRNoX0RegisterClass(MCInst *Inst, uint32_t RegNo,
151
                 uint64_t Address,
152
                 const void *Decoder)
153
20.5k
{
154
20.5k
  if (RegNo == 0) {
155
6
    return MCDisassembler_Fail;
156
6
  }
157
158
20.5k
  return DecodeGPRRegisterClass(Inst, RegNo, Address, Decoder);
159
20.5k
}
160
161
static DecodeStatus DecodeGPRNoX0X2RegisterClass(MCInst *Inst, uint64_t RegNo,
162
             uint32_t Address,
163
             const void *Decoder)
164
2.33k
{
165
2.33k
  if (RegNo == 2) {
166
0
    return MCDisassembler_Fail;
167
0
  }
168
169
2.33k
  return DecodeGPRNoX0RegisterClass(Inst, RegNo, Address, Decoder);
170
2.33k
}
171
172
static DecodeStatus DecodeGPRCRegisterClass(MCInst *Inst, uint32_t RegNo,
173
              uint64_t Address,
174
              const void *Decoder)
175
31.3k
{
176
31.3k
  if (RegNo >= 8)
177
0
    return MCDisassembler_Fail;
178
179
31.3k
  MCRegister Reg = RISCV_X8 + RegNo;
180
31.3k
  MCOperand_CreateReg0(Inst, (Reg));
181
31.3k
  return MCDisassembler_Success;
182
31.3k
}
183
184
static DecodeStatus DecodeGPRPairRegisterClass(MCInst *Inst, uint32_t RegNo,
185
                 uint64_t Address,
186
                 const void *Decoder)
187
340
{
188
340
  if (RegNo >= 32 || RegNo & 1)
189
3
    return MCDisassembler_Fail;
190
191
337
  MCRegister Reg = RISCV_X0 + RegNo;
192
337
  MCOperand_CreateReg0(Inst, (Reg));
193
337
  return MCDisassembler_Success;
194
340
}
195
196
static DecodeStatus DecodeSR07RegisterClass(MCInst *Inst, uint64_t RegNo,
197
              uint64_t Address,
198
              const void *Decoder)
199
0
{
200
0
  if (RegNo >= 8)
201
0
    return MCDisassembler_Fail;
202
203
0
  MCRegister Reg = (RegNo < 2) ? (RegNo + RISCV_X8) :
204
0
               (RegNo - 2 + RISCV_X18);
205
0
  MCOperand_CreateReg0(Inst, (Reg));
206
0
  return MCDisassembler_Success;
207
0
}
208
209
static DecodeStatus DecodeVRRegisterClass(MCInst *Inst, uint32_t RegNo,
210
            uint64_t Address, const void *Decoder)
211
7.98k
{
212
7.98k
  if (RegNo >= 32)
213
0
    return MCDisassembler_Fail;
214
215
7.98k
  MCRegister Reg = RISCV_V0 + RegNo;
216
7.98k
  MCOperand_CreateReg0(Inst, (Reg));
217
7.98k
  return MCDisassembler_Success;
218
7.98k
}
219
220
static DecodeStatus DecodeVRM2RegisterClass(MCInst *Inst, uint32_t RegNo,
221
              uint64_t Address,
222
              const void *Decoder)
223
136
{
224
136
  if (RegNo >= 32 || RegNo % 2)
225
2
    return MCDisassembler_Fail;
226
227
134
  MCRegister Reg = MCRegisterInfo_getMatchingSuperReg(
228
134
    Inst->MRI, RISCV_V0 + RegNo, RISCV_sub_vrm1_0,
229
134
    MCRegisterInfo_getRegClass(Inst->MRI, RISCV_VRM2RegClassID));
230
231
134
  MCOperand_CreateReg0(Inst, (Reg));
232
134
  return MCDisassembler_Success;
233
136
}
234
235
static DecodeStatus DecodeVRM4RegisterClass(MCInst *Inst, uint32_t RegNo,
236
              uint64_t Address,
237
              const void *Decoder)
238
84
{
239
84
  if (RegNo >= 32 || RegNo % 4)
240
3
    return MCDisassembler_Fail;
241
242
81
  MCRegister Reg = MCRegisterInfo_getMatchingSuperReg(
243
81
    Inst->MRI, RISCV_V0 + RegNo, RISCV_sub_vrm1_0,
244
81
    MCRegisterInfo_getRegClass(Inst->MRI, RISCV_VRM4RegClassID));
245
246
81
  MCOperand_CreateReg0(Inst, (Reg));
247
81
  return MCDisassembler_Success;
248
84
}
249
250
static DecodeStatus DecodeVRM8RegisterClass(MCInst *Inst, uint32_t RegNo,
251
              uint64_t Address,
252
              const void *Decoder)
253
698
{
254
698
  if (RegNo >= 32 || RegNo % 8)
255
2
    return MCDisassembler_Fail;
256
257
696
  MCRegister Reg = MCRegisterInfo_getMatchingSuperReg(
258
696
    Inst->MRI, RISCV_V0 + RegNo, RISCV_sub_vrm1_0,
259
696
    MCRegisterInfo_getRegClass(Inst->MRI, RISCV_VRM8RegClassID));
260
261
696
  MCOperand_CreateReg0(Inst, (Reg));
262
696
  return MCDisassembler_Success;
263
698
}
264
265
static DecodeStatus decodeVMaskReg(MCInst *Inst, uint64_t RegNo,
266
           uint64_t Address, const void *Decoder)
267
3.09k
{
268
3.09k
  if (RegNo > 2) {
269
0
    return MCDisassembler_Fail;
270
0
  }
271
3.09k
  MCRegister Reg = (RegNo == 0) ? RISCV_V0 : RISCV_NoRegister;
272
273
3.09k
  MCOperand_CreateReg0(Inst, (Reg));
274
3.09k
  return MCDisassembler_Success;
275
3.09k
}
276
277
#define DEFINE_decodeUImmOperand(N) \
278
  static DecodeStatus CONCAT(decodeUImmOperand, \
279
           N)(MCInst * Inst, uint32_t Imm, \
280
              int64_t Address, const void *Decoder) \
281
26.6k
  { \
282
26.6k
    CS_ASSERT(isUIntN(N, Imm) && "Invalid immediate"); \
283
26.6k
    MCOperand_CreateImm0(Inst, (Imm)); \
284
26.6k
    return MCDisassembler_Success; \
285
26.6k
  }
RISCVDisassembler.c:decodeUImmOperand_8
Line
Count
Source
281
5.65k
  { \
282
5.65k
    CS_ASSERT(isUIntN(N, Imm) && "Invalid immediate"); \
283
5.65k
    MCOperand_CreateImm0(Inst, (Imm)); \
284
5.65k
    return MCDisassembler_Success; \
285
5.65k
  }
RISCVDisassembler.c:decodeUImmOperand_9
Line
Count
Source
281
2.36k
  { \
282
2.36k
    CS_ASSERT(isUIntN(N, Imm) && "Invalid immediate"); \
283
2.36k
    MCOperand_CreateImm0(Inst, (Imm)); \
284
2.36k
    return MCDisassembler_Success; \
285
2.36k
  }
RISCVDisassembler.c:decodeUImmOperand_7
Line
Count
Source
281
4.59k
  { \
282
4.59k
    CS_ASSERT(isUIntN(N, Imm) && "Invalid immediate"); \
283
4.59k
    MCOperand_CreateImm0(Inst, (Imm)); \
284
4.59k
    return MCDisassembler_Success; \
285
4.59k
  }
RISCVDisassembler.c:decodeUImmOperand_2
Line
Count
Source
281
2.03k
  { \
282
2.03k
    CS_ASSERT(isUIntN(N, Imm) && "Invalid immediate"); \
283
2.03k
    MCOperand_CreateImm0(Inst, (Imm)); \
284
2.03k
    return MCDisassembler_Success; \
285
2.03k
  }
RISCVDisassembler.c:decodeUImmOperand_4
Line
Count
Source
281
1.53k
  { \
282
1.53k
    CS_ASSERT(isUIntN(N, Imm) && "Invalid immediate"); \
283
1.53k
    MCOperand_CreateImm0(Inst, (Imm)); \
284
1.53k
    return MCDisassembler_Success; \
285
1.53k
  }
RISCVDisassembler.c:decodeUImmOperand_20
Line
Count
Source
281
839
  { \
282
839
    CS_ASSERT(isUIntN(N, Imm) && "Invalid immediate"); \
283
839
    MCOperand_CreateImm0(Inst, (Imm)); \
284
839
    return MCDisassembler_Success; \
285
839
  }
RISCVDisassembler.c:decodeUImmOperand_5
Line
Count
Source
281
4.03k
  { \
282
4.03k
    CS_ASSERT(isUIntN(N, Imm) && "Invalid immediate"); \
283
4.03k
    MCOperand_CreateImm0(Inst, (Imm)); \
284
4.03k
    return MCDisassembler_Success; \
285
4.03k
  }
RISCVDisassembler.c:decodeUImmOperand_6
Line
Count
Source
281
839
  { \
282
839
    CS_ASSERT(isUIntN(N, Imm) && "Invalid immediate"); \
283
839
    MCOperand_CreateImm0(Inst, (Imm)); \
284
839
    return MCDisassembler_Success; \
285
839
  }
RISCVDisassembler.c:decodeUImmOperand_11
Line
Count
Source
281
354
  { \
282
354
    CS_ASSERT(isUIntN(N, Imm) && "Invalid immediate"); \
283
354
    MCOperand_CreateImm0(Inst, (Imm)); \
284
354
    return MCDisassembler_Success; \
285
354
  }
RISCVDisassembler.c:decodeUImmOperand_10
Line
Count
Source
281
3.05k
  { \
282
3.05k
    CS_ASSERT(isUIntN(N, Imm) && "Invalid immediate"); \
283
3.05k
    MCOperand_CreateImm0(Inst, (Imm)); \
284
3.05k
    return MCDisassembler_Success; \
285
3.05k
  }
RISCVDisassembler.c:decodeUImmOperand_12
Line
Count
Source
281
1.36k
  { \
282
1.36k
    CS_ASSERT(isUIntN(N, Imm) && "Invalid immediate"); \
283
1.36k
    MCOperand_CreateImm0(Inst, (Imm)); \
284
1.36k
    return MCDisassembler_Success; \
285
1.36k
  }
RISCVDisassembler.c:decodeUImmOperand_3
Line
Count
Source
281
19
  { \
282
19
    CS_ASSERT(isUIntN(N, Imm) && "Invalid immediate"); \
283
19
    MCOperand_CreateImm0(Inst, (Imm)); \
284
19
    return MCDisassembler_Success; \
285
19
  }
Unexecuted instantiation: RISCVDisassembler.c:decodeUImmOperand_1
286
DEFINE_decodeUImmOperand(6);
287
DEFINE_decodeUImmOperand(2);
288
DEFINE_decodeUImmOperand(8);
289
DEFINE_decodeUImmOperand(9);
290
DEFINE_decodeUImmOperand(7);
291
DEFINE_decodeUImmOperand(4);
292
DEFINE_decodeUImmOperand(20);
293
DEFINE_decodeUImmOperand(5);
294
DEFINE_decodeUImmOperand(11);
295
DEFINE_decodeUImmOperand(10);
296
DEFINE_decodeUImmOperand(12);
297
DEFINE_decodeUImmOperand(3);
298
DEFINE_decodeUImmOperand(1);
299
300
#define DEFINE_decodeUImmNonZeroOperand(N) \
301
  static DecodeStatus CONCAT(decodeUImmNonZeroOperand, \
302
           N)(MCInst * Inst, uint32_t Imm, \
303
              int64_t Address, const void *Decoder) \
304
2.98k
  { \
305
2.98k
    if (Imm == 0) \
306
2.98k
      return MCDisassembler_Fail; \
307
2.98k
    return CONCAT(decodeUImmOperand, N)(Inst, Imm, Address, \
308
2.97k
                Decoder); \
309
2.98k
  }
310
DEFINE_decodeUImmNonZeroOperand(10);
311
312
#define DEFINE_decodeSImmOperand(N) \
313
  static DecodeStatus CONCAT(decodeSImmOperand, \
314
           N)(MCInst * Inst, uint32_t Imm, \
315
              int64_t Address, const void *Decoder) \
316
9.81k
  { \
317
9.81k
    CS_ASSERT(isUIntN(N, Imm) && "Invalid immediate"); \
318
9.81k
\
319
9.81k
    MCOperand_CreateImm0(Inst, (SignExtend64((Imm), N))); \
320
9.81k
    return MCDisassembler_Success; \
321
9.81k
  }
RISCVDisassembler.c:decodeSImmOperand_6
Line
Count
Source
316
5.97k
  { \
317
5.97k
    CS_ASSERT(isUIntN(N, Imm) && "Invalid immediate"); \
318
5.97k
\
319
5.97k
    MCOperand_CreateImm0(Inst, (SignExtend64((Imm), N))); \
320
5.97k
    return MCDisassembler_Success; \
321
5.97k
  }
RISCVDisassembler.c:decodeSImmOperand_10
Line
Count
Source
316
528
  { \
317
528
    CS_ASSERT(isUIntN(N, Imm) && "Invalid immediate"); \
318
528
\
319
528
    MCOperand_CreateImm0(Inst, (SignExtend64((Imm), N))); \
320
528
    return MCDisassembler_Success; \
321
528
  }
RISCVDisassembler.c:decodeSImmOperand_12
Line
Count
Source
316
2.97k
  { \
317
2.97k
    CS_ASSERT(isUIntN(N, Imm) && "Invalid immediate"); \
318
2.97k
\
319
2.97k
    MCOperand_CreateImm0(Inst, (SignExtend64((Imm), N))); \
320
2.97k
    return MCDisassembler_Success; \
321
2.97k
  }
RISCVDisassembler.c:decodeSImmOperand_5
Line
Count
Source
316
335
  { \
317
335
    CS_ASSERT(isUIntN(N, Imm) && "Invalid immediate"); \
318
335
\
319
335
    MCOperand_CreateImm0(Inst, (SignExtend64((Imm), N))); \
320
335
    return MCDisassembler_Success; \
321
335
  }
322
DEFINE_decodeSImmOperand(6);
323
DEFINE_decodeSImmOperand(12);
324
DEFINE_decodeSImmOperand(5);
325
DEFINE_decodeSImmOperand(10);
326
327
#define DEFINE_decodeSImmNonZeroOperand(N) \
328
  static DecodeStatus CONCAT(decodeSImmNonZeroOperand, \
329
           N)(MCInst * Inst, uint32_t Imm, \
330
              int64_t Address, const void *Decoder) \
331
3.23k
  { \
332
3.23k
    if (Imm == 0) \
333
3.23k
      return MCDisassembler_Fail; \
334
3.23k
    return CONCAT(decodeSImmOperand, N)(Inst, Imm, Address, \
335
3.23k
                Decoder); \
336
3.23k
  }
RISCVDisassembler.c:decodeSImmNonZeroOperand_6
Line
Count
Source
331
2.70k
  { \
332
2.70k
    if (Imm == 0) \
333
2.70k
      return MCDisassembler_Fail; \
334
2.70k
    return CONCAT(decodeSImmOperand, N)(Inst, Imm, Address, \
335
2.70k
                Decoder); \
336
2.70k
  }
RISCVDisassembler.c:decodeSImmNonZeroOperand_10
Line
Count
Source
331
529
  { \
332
529
    if (Imm == 0) \
333
529
      return MCDisassembler_Fail; \
334
529
    return CONCAT(decodeSImmOperand, N)(Inst, Imm, Address, \
335
528
                Decoder); \
336
529
  }
337
DEFINE_decodeSImmNonZeroOperand(6);
338
DEFINE_decodeSImmNonZeroOperand(10);
339
340
#define DEFINE_decodeSImmOperandAndLsl1(N) \
341
  static DecodeStatus CONCAT(decodeSImmOperandAndLsl1, \
342
           N)(MCInst * Inst, uint32_t Imm, \
343
              int64_t Address, const void *Decoder) \
344
5.61k
  { \
345
5.61k
    CS_ASSERT(isUIntN(N, Imm) && "Invalid immediate"); \
346
5.61k
\
347
5.61k
    MCOperand_CreateImm0(Inst, (SignExtend64((Imm << 1), N))); \
348
5.61k
    return MCDisassembler_Success; \
349
5.61k
  }
RISCVDisassembler.c:decodeSImmOperandAndLsl1_12
Line
Count
Source
344
2.68k
  { \
345
2.68k
    CS_ASSERT(isUIntN(N, Imm) && "Invalid immediate"); \
346
2.68k
\
347
2.68k
    MCOperand_CreateImm0(Inst, (SignExtend64((Imm << 1), N))); \
348
2.68k
    return MCDisassembler_Success; \
349
2.68k
  }
RISCVDisassembler.c:decodeSImmOperandAndLsl1_9
Line
Count
Source
344
1.94k
  { \
345
1.94k
    CS_ASSERT(isUIntN(N, Imm) && "Invalid immediate"); \
346
1.94k
\
347
1.94k
    MCOperand_CreateImm0(Inst, (SignExtend64((Imm << 1), N))); \
348
1.94k
    return MCDisassembler_Success; \
349
1.94k
  }
RISCVDisassembler.c:decodeSImmOperandAndLsl1_13
Line
Count
Source
344
275
  { \
345
275
    CS_ASSERT(isUIntN(N, Imm) && "Invalid immediate"); \
346
275
\
347
275
    MCOperand_CreateImm0(Inst, (SignExtend64((Imm << 1), N))); \
348
275
    return MCDisassembler_Success; \
349
275
  }
RISCVDisassembler.c:decodeSImmOperandAndLsl1_21
Line
Count
Source
344
718
  { \
345
718
    CS_ASSERT(isUIntN(N, Imm) && "Invalid immediate"); \
346
718
\
347
718
    MCOperand_CreateImm0(Inst, (SignExtend64((Imm << 1), N))); \
348
718
    return MCDisassembler_Success; \
349
718
  }
350
DEFINE_decodeSImmOperandAndLsl1(12);
351
DEFINE_decodeSImmOperandAndLsl1(9);
352
DEFINE_decodeSImmOperandAndLsl1(13);
353
DEFINE_decodeSImmOperandAndLsl1(21);
354
355
static DecodeStatus decodeCLUIImmOperand(MCInst *Inst, uint32_t Imm,
356
           int64_t Address, const void *Decoder)
357
2.33k
{
358
2.33k
  CS_ASSERT(isUIntN(6, Imm) && "Invalid immediate");
359
2.33k
  if (Imm > 31) {
360
945
    Imm = (SignExtend64((Imm), 6) & 0xfffff);
361
945
  }
362
2.33k
  MCOperand_CreateImm0(Inst, (Imm));
363
2.33k
  return MCDisassembler_Success;
364
2.33k
}
365
366
static DecodeStatus decodeFRMArg(MCInst *Inst, uint32_t Imm, int64_t Address,
367
         const void *Decoder)
368
1.67k
{
369
1.67k
  CS_ASSERT(isUIntN(3, Imm) && "Invalid immediate");
370
1.67k
  if (!RISCVFPRndMode_isValidRoundingMode(Imm))
371
7
    return MCDisassembler_Fail;
372
373
1.67k
  MCOperand_CreateImm0(Inst, (Imm));
374
1.67k
  return MCDisassembler_Success;
375
1.67k
}
376
377
static DecodeStatus decodeRVCInstrRdRs1ImmZero(MCInst *Inst, uint32_t Insn,
378
                 uint64_t Address,
379
                 const void *Decoder);
380
381
static DecodeStatus decodeRVCInstrRdSImm(MCInst *Inst, uint32_t Insn,
382
           uint64_t Address, const void *Decoder);
383
384
static DecodeStatus decodeRVCInstrRdRs1UImm(MCInst *Inst, uint32_t Insn,
385
              uint64_t Address,
386
              const void *Decoder);
387
388
static DecodeStatus decodeRVCInstrRdRs2(MCInst *Inst, uint32_t Insn,
389
          uint64_t Address, const void *Decoder);
390
391
static DecodeStatus decodeRVCInstrRdRs1Rs2(MCInst *Inst, uint32_t Insn,
392
             uint64_t Address,
393
             const void *Decoder);
394
395
static DecodeStatus decodeXTHeadMemPair(MCInst *Inst, uint32_t Insn,
396
          uint64_t Address, const void *Decoder);
397
398
static DecodeStatus decodeZcmpRlist(MCInst *Inst, unsigned Imm,
399
            uint64_t Address, const void *Decoder);
400
401
static DecodeStatus decodeRegReg(MCInst *Inst, uint32_t Insn, uint64_t Address,
402
         const void *Decoder);
403
404
static DecodeStatus decodeZcmpSpimm(MCInst *Inst, unsigned Imm,
405
            uint64_t Address, const void *Decoder);
406
407
static DecodeStatus decodeCSSPushPopchk(MCInst *Inst, uint32_t Insn,
408
          uint64_t Address, const void *Decoder);
409
410
static DecodeStatus decodeUImmLog2XLenOperand(MCInst *Inst, uint32_t Imm,
411
                int64_t Address,
412
                const void *Decoder)
413
2.46k
{
414
2.46k
  CS_ASSERT(isUIntN(6, Imm) && "Invalid immediate");
415
416
2.46k
  if (!RISCV_getFeatureBits(Inst->csh->mode, RISCV_Feature64Bit) &&
417
365
      !isUIntN(5, Imm))
418
5
    return MCDisassembler_Fail;
419
420
2.46k
  MCOperand_CreateImm0(Inst, (Imm));
421
2.46k
  return MCDisassembler_Success;
422
2.46k
}
423
424
static DecodeStatus decodeUImmLog2XLenNonZeroOperand(MCInst *Inst, uint32_t Imm,
425
                 int64_t Address,
426
                 const void *Decoder)
427
2.37k
{
428
2.37k
  if (Imm == 0)
429
0
    return MCDisassembler_Fail;
430
2.37k
  return decodeUImmLog2XLenOperand(Inst, Imm, Address, Decoder);
431
2.37k
}
432
433
#include "RISCVGenDisassemblerTables.inc"
434
435
static DecodeStatus decodeRVCInstrRdRs1ImmZero(MCInst *Inst, uint32_t Insn,
436
                 uint64_t Address,
437
                 const void *Decoder)
438
383
{
439
383
  uint32_t Rd = fieldFromInstruction_4(Insn, 7, 5);
440
383
  DecodeStatus Result =
441
383
    DecodeGPRNoX0RegisterClass(Inst, Rd, Address, Decoder);
442
383
  (void)Result;
443
383
  CS_ASSERT(Result == MCDisassembler_Success && "Invalid register");
444
383
  MCInst_addOperand2(Inst, (MCInst_getOperand(Inst, (0))));
445
383
  MCOperand_CreateImm0(Inst, (0));
446
383
  return MCDisassembler_Success;
447
383
}
448
449
static DecodeStatus decodeCSSPushPopchk(MCInst *Inst, uint32_t Insn,
450
          uint64_t Address, const void *Decoder)
451
0
{
452
0
  uint32_t Rs1 = fieldFromInstruction_4(Insn, 7, 5);
453
0
  DecodeStatus Result =
454
0
    DecodeGPRX1X5RegisterClass(Inst, Rs1, Address, Decoder);
455
0
  (void)Result;
456
0
  CS_ASSERT(Result == MCDisassembler_Success && "Invalid register");
457
0
  return MCDisassembler_Success;
458
0
}
459
460
static DecodeStatus decodeRVCInstrRdSImm(MCInst *Inst, uint32_t Insn,
461
           uint64_t Address, const void *Decoder)
462
179
{
463
179
  MCOperand_CreateReg0(Inst, (RISCV_X0));
464
179
  uint32_t SImm6 = fieldFromInstruction_4(Insn, 12, 1) << 5 |
465
179
       fieldFromInstruction_4(Insn, 2, 5);
466
179
  DecodeStatus Result =
467
179
    CONCAT(decodeSImmOperand, 6)(Inst, SImm6, Address, Decoder);
468
179
  (void)Result;
469
179
  CS_ASSERT(Result == MCDisassembler_Success && "Invalid immediate");
470
179
  return MCDisassembler_Success;
471
179
}
472
473
static DecodeStatus decodeRVCInstrRdRs1UImm(MCInst *Inst, uint32_t Insn,
474
              uint64_t Address,
475
              const void *Decoder)
476
743
{
477
743
  MCOperand_CreateReg0(Inst, (RISCV_X0));
478
743
  MCInst_addOperand2(Inst, (MCInst_getOperand(Inst, (0))));
479
743
  uint32_t UImm6 = fieldFromInstruction_4(Insn, 12, 1) << 5 |
480
743
       fieldFromInstruction_4(Insn, 2, 5);
481
743
  DecodeStatus Result =
482
743
    CONCAT(decodeUImmOperand, 6)(Inst, UImm6, Address, Decoder);
483
743
  (void)Result;
484
743
  CS_ASSERT(Result == MCDisassembler_Success && "Invalid immediate");
485
743
  return MCDisassembler_Success;
486
743
}
487
488
static DecodeStatus decodeRVCInstrRdRs2(MCInst *Inst, uint32_t Insn,
489
          uint64_t Address, const void *Decoder)
490
144
{
491
144
  uint32_t Rd = fieldFromInstruction_4(Insn, 7, 5);
492
144
  uint32_t Rs2 = fieldFromInstruction_4(Insn, 2, 5);
493
144
  DecodeGPRRegisterClass(Inst, Rd, Address, Decoder);
494
144
  DecodeGPRRegisterClass(Inst, Rs2, Address, Decoder);
495
144
  return MCDisassembler_Success;
496
144
}
497
498
static DecodeStatus decodeRVCInstrRdRs1Rs2(MCInst *Inst, uint32_t Insn,
499
             uint64_t Address,
500
             const void *Decoder)
501
95
{
502
95
  uint32_t Rd = fieldFromInstruction_4(Insn, 7, 5);
503
95
  uint32_t Rs2 = fieldFromInstruction_4(Insn, 2, 5);
504
95
  DecodeGPRRegisterClass(Inst, Rd, Address, Decoder);
505
95
  MCInst_addOperand2(Inst, (MCInst_getOperand(Inst, (0))));
506
95
  DecodeGPRRegisterClass(Inst, Rs2, Address, Decoder);
507
95
  return MCDisassembler_Success;
508
95
}
509
510
static DecodeStatus decodeXTHeadMemPair(MCInst *Inst, uint32_t Insn,
511
          uint64_t Address, const void *Decoder)
512
0
{
513
0
  uint32_t Rd1 = fieldFromInstruction_4(Insn, 7, 5);
514
0
  uint32_t Rs1 = fieldFromInstruction_4(Insn, 15, 5);
515
0
  uint32_t Rd2 = fieldFromInstruction_4(Insn, 20, 5);
516
0
  uint32_t UImm2 = fieldFromInstruction_4(Insn, 25, 2);
517
0
  DecodeGPRRegisterClass(Inst, Rd1, Address, Decoder);
518
0
  DecodeGPRRegisterClass(Inst, Rd2, Address, Decoder);
519
0
  DecodeGPRRegisterClass(Inst, Rs1, Address, Decoder);
520
0
  DecodeStatus Result =
521
0
    CONCAT(decodeUImmOperand, 2)(Inst, UImm2, Address, Decoder);
522
0
  (void)Result;
523
0
  CS_ASSERT(Result == MCDisassembler_Success && "Invalid immediate");
524
525
  // Disassemble the final operand which is implicit.
526
0
  unsigned Opcode = MCInst_getOpcode(Inst);
527
0
  bool IsWordOp = (Opcode == RISCV_TH_LWD || Opcode == RISCV_TH_LWUD ||
528
0
       Opcode == RISCV_TH_SWD);
529
0
  if (IsWordOp)
530
0
    MCOperand_CreateImm0(Inst, (3));
531
0
  else
532
0
    MCOperand_CreateImm0(Inst, (4));
533
534
0
  return MCDisassembler_Success;
535
0
}
536
537
static DecodeStatus decodeZcmpRlist(MCInst *Inst, unsigned Imm,
538
            uint64_t Address, const void *Decoder)
539
0
{
540
0
  if (Imm <= 3)
541
0
    return MCDisassembler_Fail;
542
0
  MCOperand_CreateImm0(Inst, (Imm));
543
0
  return MCDisassembler_Success;
544
0
}
545
546
static DecodeStatus decodeRegReg(MCInst *Inst, uint32_t Insn, uint64_t Address,
547
         const void *Decoder)
548
501
{
549
501
  uint32_t Rs1 = fieldFromInstruction_4(Insn, 0, 5);
550
501
  uint32_t Rs2 = fieldFromInstruction_4(Insn, 5, 5);
551
501
  DecodeGPRRegisterClass(Inst, Rs1, Address, Decoder);
552
501
  DecodeGPRRegisterClass(Inst, Rs2, Address, Decoder);
553
501
  return MCDisassembler_Success;
554
501
}
555
556
static DecodeStatus decodeZcmpSpimm(MCInst *Inst, unsigned Imm,
557
            uint64_t Address, const void *Decoder)
558
0
{
559
0
  MCOperand_CreateImm0(Inst, (Imm));
560
0
  return MCDisassembler_Success;
561
0
}
562
563
// Add implied SP operand for C.*SP compressed instructions. The SP operand
564
// isn't explicitly encoded in the instruction.
565
void addSPOperands(MCInst *MI)
566
42.3k
{
567
42.3k
  const MCInstrDesc *MCID = MCInstrDesc_get(MCInst_getOpcode(MI),
568
42.3k
              RISCVDescs.Insts,
569
42.3k
              ARR_SIZE(RISCVDescs.Insts));
570
42.3k
  MCOperand SPReg;
571
42.3k
  SPReg.MachineOperandType = kRegister;
572
42.3k
  SPReg.Kind = kRegister;
573
42.3k
  SPReg.RegVal = RISCV_X2;
574
144k
  for (unsigned i = 0; i < MCID->NumOperands; i++)
575
102k
    if (MCID->OpInfo[i].RegClass == RISCV_SPRegClassID)
576
10.0k
      MCInst_insert0(MI, i, &SPReg);
577
42.3k
}
578
579
DecodeStatus RISCV_getInstruction(MCInst *MI, uint16_t *Size,
580
          const uint8_t *Bytes, size_t BytesLen,
581
          uint64_t Address, SStream *CS)
582
62.0k
{
583
  // TODO: This will need modification when supporting instruction set
584
  // extensions with instructions > 32-bits (up to 176 bits wide).
585
62.0k
  uint32_t Insn;
586
62.0k
  DecodeStatus Result;
587
588
62.0k
#define TRY_TO_DECODE_WITH_ADDITIONAL_OPERATION( \
589
62.0k
  width, FEATURE_CHECKS, DECODER_TABLE, DESC, ADDITIONAL_OPERATION) \
590
724k
  do { \
591
782k
    if (FEATURE_CHECKS) { \
592
208k
      Result = decodeInstruction_##width( \
593
208k
        DECODER_TABLE, MI, Insn, Address, NULL); \
594
208k
      if (Result != MCDisassembler_Fail) { \
595
61.2k
        ADDITIONAL_OPERATION; \
596
61.2k
        return Result; \
597
61.2k
      } \
598
208k
    } \
599
724k
  } while (false)
600
62.0k
#define TRY_TO_DECODE_AND_ADD_SP(width, FEATURE_CHECKS, DECODER_TABLE, DESC) \
601
83.2k
  TRY_TO_DECODE_WITH_ADDITIONAL_OPERATION( \
602
83.2k
    width, FEATURE_CHECKS, DECODER_TABLE, DESC, addSPOperands(MI))
603
62.0k
#define TRY_TO_DECODE(width, FEATURE_CHECKS, DECODER_TABLE, DESC) \
604
641k
  TRY_TO_DECODE_WITH_ADDITIONAL_OPERATION( \
605
641k
    width, FEATURE_CHECKS, DECODER_TABLE, DESC, (void)NULL)
606
62.0k
#define TRY_TO_DECODE_FEATURE(width, FEATURE, DECODER_TABLE, DESC) \
607
587k
  TRY_TO_DECODE(width, RISCV_getFeatureBits(MI->csh->mode, FEATURE), \
608
587k
          DECODER_TABLE, DESC)
609
610
  // It's a 32 bit instruction if bit 0 and 1 are 1.
611
62.0k
  if ((Bytes[0] & 0x3) == 0x3) {
612
19.3k
    if (BytesLen < 4) {
613
103
      *Size = 0;
614
103
      return MCDisassembler_Fail;
615
103
    }
616
19.2k
    *Size = 4;
617
618
19.2k
    Insn = readBytes32(MI, Bytes);
619
620
19.2k
    TRY_TO_DECODE(4,
621
19.2k
            RISCV_getFeatureBits(MI->csh->mode,
622
19.2k
               RISCV_FeatureStdExtZdinx) &&
623
19.2k
              !RISCV_getFeatureBits(MI->csh->mode,
624
19.2k
                  RISCV_Feature64Bit),
625
19.2k
            DecoderTableRV32Zdinx32,
626
19.2k
            "RV32Zdinx table (Double in Integer and rv32)");
627
19.2k
    TRY_TO_DECODE(4,
628
19.1k
            RISCV_getFeatureBits(MI->csh->mode,
629
19.1k
               RISCV_FeatureStdExtZacas) &&
630
19.1k
              !RISCV_getFeatureBits(MI->csh->mode,
631
19.1k
                  RISCV_Feature64Bit),
632
19.1k
            DecoderTableRV32Zacas32,
633
19.1k
            "RV32Zacas table (Compare-And-Swap and rv32)");
634
19.1k
    TRY_TO_DECODE_FEATURE(4, RISCV_FeatureStdExtZfinx,
635
19.1k
              DecoderTableRVZfinx32,
636
19.1k
              "RVZfinx table (Float in Integer)");
637
19.1k
    TRY_TO_DECODE_FEATURE(4, RISCV_FeatureVendorXVentanaCondOps,
638
19.1k
              DecoderTableXVentana32,
639
19.1k
              "Ventana custom opcode table");
640
19.1k
    TRY_TO_DECODE_FEATURE(4, RISCV_FeatureVendorXTHeadBa,
641
19.1k
              DecoderTableXTHeadBa32,
642
19.1k
              "XTHeadBa custom opcode table");
643
19.1k
    TRY_TO_DECODE_FEATURE(4, RISCV_FeatureVendorXTHeadBb,
644
19.1k
              DecoderTableXTHeadBb32,
645
19.1k
              "XTHeadBb custom opcode table");
646
19.1k
    TRY_TO_DECODE_FEATURE(4, RISCV_FeatureVendorXTHeadBs,
647
19.1k
              DecoderTableXTHeadBs32,
648
19.1k
              "XTHeadBs custom opcode table");
649
19.1k
    TRY_TO_DECODE_FEATURE(4, RISCV_FeatureVendorXTHeadCondMov,
650
19.1k
              DecoderTableXTHeadCondMov32,
651
19.1k
              "XTHeadCondMov custom opcode table");
652
19.1k
    TRY_TO_DECODE_FEATURE(4, RISCV_FeatureVendorXTHeadCmo,
653
19.1k
              DecoderTableXTHeadCmo32,
654
19.1k
              "XTHeadCmo custom opcode table");
655
19.1k
    TRY_TO_DECODE_FEATURE(4, RISCV_FeatureVendorXTHeadFMemIdx,
656
19.1k
              DecoderTableXTHeadFMemIdx32,
657
19.1k
              "XTHeadFMemIdx custom opcode table");
658
19.1k
    TRY_TO_DECODE_FEATURE(4, RISCV_FeatureVendorXTHeadMac,
659
19.1k
              DecoderTableXTHeadMac32,
660
19.1k
              "XTHeadMac custom opcode table");
661
19.1k
    TRY_TO_DECODE_FEATURE(4, RISCV_FeatureVendorXTHeadMemIdx,
662
19.1k
              DecoderTableXTHeadMemIdx32,
663
19.1k
              "XTHeadMemIdx custom opcode table");
664
19.1k
    TRY_TO_DECODE_FEATURE(4, RISCV_FeatureVendorXTHeadMemPair,
665
19.1k
              DecoderTableXTHeadMemPair32,
666
19.1k
              "XTHeadMemPair custom opcode table");
667
19.1k
    TRY_TO_DECODE_FEATURE(4, RISCV_FeatureVendorXTHeadSync,
668
19.1k
              DecoderTableXTHeadSync32,
669
19.1k
              "XTHeadSync custom opcode table");
670
19.1k
    TRY_TO_DECODE_FEATURE(4, RISCV_FeatureVendorXTHeadVdot,
671
19.1k
              DecoderTableXTHeadVdot32,
672
19.1k
              "XTHeadVdot custom opcode table");
673
19.1k
    TRY_TO_DECODE_FEATURE(4, RISCV_FeatureVendorXSfvcp,
674
19.1k
              DecoderTableXSfvcp32,
675
19.1k
              "SiFive VCIX custom opcode table");
676
19.1k
    TRY_TO_DECODE_FEATURE(
677
19.1k
      4, RISCV_FeatureVendorXSfvqmaccdod,
678
19.1k
      DecoderTableXSfvqmaccdod32,
679
19.1k
      "SiFive Matrix Multiplication (2x8 and 8x2) Instruction opcode table");
680
19.1k
    TRY_TO_DECODE_FEATURE(
681
19.1k
      4, RISCV_FeatureVendorXSfvqmaccqoq,
682
19.1k
      DecoderTableXSfvqmaccqoq32,
683
19.1k
      "SiFive Matrix Multiplication (4x8 and 8x4) Instruction opcode table");
684
19.1k
    TRY_TO_DECODE_FEATURE(
685
19.1k
      4, RISCV_FeatureVendorXSfvfwmaccqqq,
686
19.1k
      DecoderTableXSfvfwmaccqqq32,
687
19.1k
      "SiFive Matrix Multiplication Instruction opcode table");
688
19.1k
    TRY_TO_DECODE_FEATURE(
689
19.1k
      4, RISCV_FeatureVendorXSfvfnrclipxfqf,
690
19.1k
      DecoderTableXSfvfnrclipxfqf32,
691
19.1k
      "SiFive FP32-to-int8 Ranged Clip Instructions opcode table");
692
19.1k
    TRY_TO_DECODE_FEATURE(
693
18.2k
      4, RISCV_FeatureVendorXCVbitmanip,
694
18.2k
      DecoderTableXCVbitmanip32,
695
18.2k
      "CORE-V Bit Manipulation custom opcode table");
696
18.2k
    TRY_TO_DECODE_FEATURE(4, RISCV_FeatureVendorXCVelw,
697
18.2k
              DecoderTableXCVelw32,
698
18.2k
              "CORE-V Event load custom opcode table");
699
18.2k
    TRY_TO_DECODE_FEATURE(4, RISCV_FeatureVendorXCVmac,
700
16.9k
              DecoderTableXCVmac32,
701
16.9k
              "CORE-V MAC custom opcode table");
702
16.9k
    TRY_TO_DECODE_FEATURE(4, RISCV_FeatureVendorXCVmem,
703
15.8k
              DecoderTableXCVmem32,
704
15.8k
              "CORE-V MEM custom opcode table");
705
15.8k
    TRY_TO_DECODE_FEATURE(4, RISCV_FeatureVendorXCValu,
706
15.7k
              DecoderTableXCValu32,
707
15.7k
              "CORE-V ALU custom opcode table");
708
15.7k
    TRY_TO_DECODE_FEATURE(
709
15.6k
      4, RISCV_FeatureVendorXCVsimd, DecoderTableXCVsimd32,
710
15.6k
      "CORE-V SIMD extensions custom opcode table");
711
15.6k
    TRY_TO_DECODE_FEATURE(
712
15.5k
      4, RISCV_FeatureVendorXCVbi, DecoderTableXCVbi32,
713
15.5k
      "CORE-V Immediate Branching custom opcode table");
714
15.5k
    TRY_TO_DECODE(4, true, DecoderTable32, "RISCV32 table");
715
716
274
    return MCDisassembler_Fail;
717
15.5k
  }
718
719
42.7k
  if (BytesLen < 2) {
720
127
    *Size = 0;
721
127
    return MCDisassembler_Fail;
722
127
  }
723
42.5k
  *Size = 2;
724
725
42.5k
  Insn = readBytes16(MI, Bytes);
726
42.5k
  TRY_TO_DECODE_AND_ADD_SP(
727
40.7k
    2, !RISCV_getFeatureBits(MI->csh->mode, RISCV_Feature64Bit),
728
40.7k
    DecoderTableRISCV32Only_16,
729
40.7k
    "RISCV32Only_16 table (16-bit Instruction)");
730
40.7k
  TRY_TO_DECODE_FEATURE(2, RISCV_FeatureStdExtZicfiss,
731
40.7k
            DecoderTableZicfiss16,
732
40.7k
            "RVZicfiss table (Shadow Stack)");
733
40.7k
  TRY_TO_DECODE_FEATURE(2, RISCV_FeatureStdExtZcmt, DecoderTableRVZcmt16,
734
40.7k
            "Zcmt table (16-bit Table Jump Instructions)");
735
40.7k
  TRY_TO_DECODE_FEATURE(
736
40.7k
    2, RISCV_FeatureStdExtZcmp, DecoderTableRVZcmp16,
737
40.7k
    "Zcmp table (16-bit Push/Pop & Double Move Instructions)");
738
40.7k
  TRY_TO_DECODE_AND_ADD_SP(2, true, DecoderTable16,
739
251
         "RISCV_C table (16-bit Instruction)");
740
741
251
  return MCDisassembler_Fail;
742
40.7k
}
743
744
bool RISCV_LLVM_getInstruction(csh handle, const uint8_t *Bytes, size_t ByteLen,
745
             MCInst *MI, uint16_t *Size, uint64_t Address,
746
             void *Info)
747
62.0k
{
748
62.0k
  MI->MRI = (MCRegisterInfo *)Info;
749
  return RISCV_getInstruction(MI, Size, Bytes, ByteLen, Address, NULL) !=
750
62.0k
         MCDisassembler_Fail;
751
62.0k
}