Coverage Report

Created: 2026-08-31 06:58

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