Coverage Report

Created: 2026-04-12 06:30

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
4.23k
#define CONCAT(a, b) CONCAT_(a, b)
54
4.23k
#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
31.5k
{
69
31.5k
  bool IsRVE = RISCV_getFeatureBits(Inst->csh->mode, RISCV_FeatureRVE);
70
71
31.5k
  if (RegNo >= 32 || (IsRVE && RegNo >= 16))
72
0
    return MCDisassembler_Fail;
73
74
31.5k
  MCRegister Reg = RISCV_X0 + RegNo;
75
31.5k
  MCOperand_CreateReg0(Inst, (Reg));
76
31.5k
  return MCDisassembler_Success;
77
31.5k
}
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
2.92k
{
95
2.92k
  if (RegNo >= 32)
96
0
    return MCDisassembler_Fail;
97
98
2.92k
  MCRegister Reg = RISCV_F0_H + RegNo;
99
2.92k
  MCOperand_CreateReg0(Inst, (Reg));
100
2.92k
  return MCDisassembler_Success;
101
2.92k
}
102
103
static DecodeStatus DecodeFPR32RegisterClass(MCInst *Inst, uint32_t RegNo,
104
               uint64_t Address,
105
               const void *Decoder)
106
1.11k
{
107
1.11k
  if (RegNo >= 32)
108
0
    return MCDisassembler_Fail;
109
110
1.11k
  MCRegister Reg = RISCV_F0_F + RegNo;
111
1.11k
  MCOperand_CreateReg0(Inst, (Reg));
112
1.11k
  return MCDisassembler_Success;
113
1.11k
}
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
0
{
131
0
  if (RegNo >= 32)
132
0
    return MCDisassembler_Fail;
133
134
0
  MCRegister Reg = RISCV_F0_D + RegNo;
135
0
  MCOperand_CreateReg0(Inst, (Reg));
136
0
  return MCDisassembler_Success;
137
0
}
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
13.8k
{
155
13.8k
  if (RegNo == 0) {
156
11
    return MCDisassembler_Fail;
157
11
  }
158
159
13.8k
  return DecodeGPRRegisterClass(Inst, RegNo, Address, Decoder);
160
13.8k
}
161
162
static DecodeStatus DecodeGPRNoX0X2RegisterClass(MCInst *Inst, uint64_t RegNo,
163
             uint32_t Address,
164
             const void *Decoder)
165
1.85k
{
166
1.85k
  if (RegNo == 2) {
167
0
    return MCDisassembler_Fail;
168
0
  }
169
170
1.85k
  return DecodeGPRNoX0RegisterClass(Inst, RegNo, Address, Decoder);
171
1.85k
}
172
173
static DecodeStatus DecodeGPRCRegisterClass(MCInst *Inst, uint32_t RegNo,
174
              uint64_t Address,
175
              const void *Decoder)
176
15.4k
{
177
15.4k
  if (RegNo >= 8)
178
0
    return MCDisassembler_Fail;
179
180
15.4k
  MCRegister Reg = RISCV_X8 + RegNo;
181
15.4k
  MCOperand_CreateReg0(Inst, (Reg));
182
15.4k
  return MCDisassembler_Success;
183
15.4k
}
184
185
static DecodeStatus DecodeGPRPairRegisterClass(MCInst *Inst, uint32_t RegNo,
186
                 uint64_t Address,
187
                 const void *Decoder)
188
478
{
189
478
  if (RegNo >= 32 || RegNo & 1)
190
1
    return MCDisassembler_Fail;
191
192
477
  MCRegister Reg = RISCV_X0 + RegNo;
193
477
  MCOperand_CreateReg0(Inst, (Reg));
194
477
  return MCDisassembler_Success;
195
478
}
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
6.24k
{
213
6.24k
  if (RegNo >= 32)
214
0
    return MCDisassembler_Fail;
215
216
6.24k
  MCRegister Reg = RISCV_V0 + RegNo;
217
6.24k
  MCOperand_CreateReg0(Inst, (Reg));
218
6.24k
  return MCDisassembler_Success;
219
6.24k
}
220
221
static DecodeStatus DecodeVRM2RegisterClass(MCInst *Inst, uint32_t RegNo,
222
              uint64_t Address,
223
              const void *Decoder)
224
46
{
225
46
  if (RegNo >= 32 || RegNo % 2)
226
5
    return MCDisassembler_Fail;
227
228
41
  MCRegister Reg = MCRegisterInfo_getMatchingSuperReg(
229
41
    Inst->MRI, RISCV_V0 + RegNo, RISCV_sub_vrm1_0,
230
41
    MCRegisterInfo_getRegClass(Inst->MRI, RISCV_VRM2RegClassID));
231
232
41
  MCOperand_CreateReg0(Inst, (Reg));
233
41
  return MCDisassembler_Success;
234
46
}
235
236
static DecodeStatus DecodeVRM4RegisterClass(MCInst *Inst, uint32_t RegNo,
237
              uint64_t Address,
238
              const void *Decoder)
239
31
{
240
31
  if (RegNo >= 32 || RegNo % 4)
241
4
    return MCDisassembler_Fail;
242
243
27
  MCRegister Reg = MCRegisterInfo_getMatchingSuperReg(
244
27
    Inst->MRI, RISCV_V0 + RegNo, RISCV_sub_vrm1_0,
245
27
    MCRegisterInfo_getRegClass(Inst->MRI, RISCV_VRM4RegClassID));
246
247
27
  MCOperand_CreateReg0(Inst, (Reg));
248
27
  return MCDisassembler_Success;
249
31
}
250
251
static DecodeStatus DecodeVRM8RegisterClass(MCInst *Inst, uint32_t RegNo,
252
              uint64_t Address,
253
              const void *Decoder)
254
128
{
255
128
  if (RegNo >= 32 || RegNo % 8)
256
2
    return MCDisassembler_Fail;
257
258
126
  MCRegister Reg = MCRegisterInfo_getMatchingSuperReg(
259
126
    Inst->MRI, RISCV_V0 + RegNo, RISCV_sub_vrm1_0,
260
126
    MCRegisterInfo_getRegClass(Inst->MRI, RISCV_VRM8RegClassID));
261
262
126
  MCOperand_CreateReg0(Inst, (Reg));
263
126
  return MCDisassembler_Success;
264
128
}
265
266
static DecodeStatus decodeVMaskReg(MCInst *Inst, uint64_t RegNo,
267
           uint64_t Address, const void *Decoder)
268
2.05k
{
269
2.05k
  if (RegNo > 2) {
270
0
    return MCDisassembler_Fail;
271
0
  }
272
2.05k
  MCRegister Reg = (RegNo == 0) ? RISCV_V0 : RISCV_NoRegister;
273
274
2.05k
  MCOperand_CreateReg0(Inst, (Reg));
275
2.05k
  return MCDisassembler_Success;
276
2.05k
}
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
13.8k
  { \
283
13.8k
    CS_ASSERT(isUIntN(N, Imm) && "Invalid immediate"); \
284
13.8k
    MCOperand_CreateImm0(Inst, (Imm)); \
285
13.8k
    return MCDisassembler_Success; \
286
13.8k
  }
RISCVDisassembler.c:decodeUImmOperand_8
Line
Count
Source
282
2.27k
  { \
283
2.27k
    CS_ASSERT(isUIntN(N, Imm) && "Invalid immediate"); \
284
2.27k
    MCOperand_CreateImm0(Inst, (Imm)); \
285
2.27k
    return MCDisassembler_Success; \
286
2.27k
  }
RISCVDisassembler.c:decodeUImmOperand_9
Line
Count
Source
282
1.25k
  { \
283
1.25k
    CS_ASSERT(isUIntN(N, Imm) && "Invalid immediate"); \
284
1.25k
    MCOperand_CreateImm0(Inst, (Imm)); \
285
1.25k
    return MCDisassembler_Success; \
286
1.25k
  }
RISCVDisassembler.c:decodeUImmOperand_7
Line
Count
Source
282
2.37k
  { \
283
2.37k
    CS_ASSERT(isUIntN(N, Imm) && "Invalid immediate"); \
284
2.37k
    MCOperand_CreateImm0(Inst, (Imm)); \
285
2.37k
    return MCDisassembler_Success; \
286
2.37k
  }
RISCVDisassembler.c:decodeUImmOperand_2
Line
Count
Source
282
891
  { \
283
891
    CS_ASSERT(isUIntN(N, Imm) && "Invalid immediate"); \
284
891
    MCOperand_CreateImm0(Inst, (Imm)); \
285
891
    return MCDisassembler_Success; \
286
891
  }
RISCVDisassembler.c:decodeUImmOperand_4
Line
Count
Source
282
1.54k
  { \
283
1.54k
    CS_ASSERT(isUIntN(N, Imm) && "Invalid immediate"); \
284
1.54k
    MCOperand_CreateImm0(Inst, (Imm)); \
285
1.54k
    return MCDisassembler_Success; \
286
1.54k
  }
RISCVDisassembler.c:decodeUImmOperand_20
Line
Count
Source
282
500
  { \
283
500
    CS_ASSERT(isUIntN(N, Imm) && "Invalid immediate"); \
284
500
    MCOperand_CreateImm0(Inst, (Imm)); \
285
500
    return MCDisassembler_Success; \
286
500
  }
RISCVDisassembler.c:decodeUImmOperand_5
Line
Count
Source
282
1.47k
  { \
283
1.47k
    CS_ASSERT(isUIntN(N, Imm) && "Invalid immediate"); \
284
1.47k
    MCOperand_CreateImm0(Inst, (Imm)); \
285
1.47k
    return MCDisassembler_Success; \
286
1.47k
  }
RISCVDisassembler.c:decodeUImmOperand_6
Line
Count
Source
282
285
  { \
283
285
    CS_ASSERT(isUIntN(N, Imm) && "Invalid immediate"); \
284
285
    MCOperand_CreateImm0(Inst, (Imm)); \
285
285
    return MCDisassembler_Success; \
286
285
  }
RISCVDisassembler.c:decodeUImmOperand_11
Line
Count
Source
282
317
  { \
283
317
    CS_ASSERT(isUIntN(N, Imm) && "Invalid immediate"); \
284
317
    MCOperand_CreateImm0(Inst, (Imm)); \
285
317
    return MCDisassembler_Success; \
286
317
  }
RISCVDisassembler.c:decodeUImmOperand_10
Line
Count
Source
282
1.84k
  { \
283
1.84k
    CS_ASSERT(isUIntN(N, Imm) && "Invalid immediate"); \
284
1.84k
    MCOperand_CreateImm0(Inst, (Imm)); \
285
1.84k
    return MCDisassembler_Success; \
286
1.84k
  }
RISCVDisassembler.c:decodeUImmOperand_12
Line
Count
Source
282
975
  { \
283
975
    CS_ASSERT(isUIntN(N, Imm) && "Invalid immediate"); \
284
975
    MCOperand_CreateImm0(Inst, (Imm)); \
285
975
    return MCDisassembler_Success; \
286
975
  }
RISCVDisassembler.c:decodeUImmOperand_3
Line
Count
Source
282
84
  { \
283
84
    CS_ASSERT(isUIntN(N, Imm) && "Invalid immediate"); \
284
84
    MCOperand_CreateImm0(Inst, (Imm)); \
285
84
    return MCDisassembler_Success; \
286
84
  }
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
1.82k
  { \
306
1.82k
    if (Imm == 0) \
307
1.82k
      return MCDisassembler_Fail; \
308
1.82k
    return CONCAT(decodeUImmOperand, N)(Inst, Imm, Address, \
309
1.82k
                Decoder); \
310
1.82k
  }
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
6.30k
  { \
318
6.30k
    CS_ASSERT(isUIntN(N, Imm) && "Invalid immediate"); \
319
6.30k
\
320
6.30k
    MCOperand_CreateImm0(Inst, (SignExtend64((Imm), N))); \
321
6.30k
    return MCDisassembler_Success; \
322
6.30k
  }
RISCVDisassembler.c:decodeSImmOperand_6
Line
Count
Source
317
3.96k
  { \
318
3.96k
    CS_ASSERT(isUIntN(N, Imm) && "Invalid immediate"); \
319
3.96k
\
320
3.96k
    MCOperand_CreateImm0(Inst, (SignExtend64((Imm), N))); \
321
3.96k
    return MCDisassembler_Success; \
322
3.96k
  }
RISCVDisassembler.c:decodeSImmOperand_10
Line
Count
Source
317
624
  { \
318
624
    CS_ASSERT(isUIntN(N, Imm) && "Invalid immediate"); \
319
624
\
320
624
    MCOperand_CreateImm0(Inst, (SignExtend64((Imm), N))); \
321
624
    return MCDisassembler_Success; \
322
624
  }
RISCVDisassembler.c:decodeSImmOperand_12
Line
Count
Source
317
1.40k
  { \
318
1.40k
    CS_ASSERT(isUIntN(N, Imm) && "Invalid immediate"); \
319
1.40k
\
320
1.40k
    MCOperand_CreateImm0(Inst, (SignExtend64((Imm), N))); \
321
1.40k
    return MCDisassembler_Success; \
322
1.40k
  }
RISCVDisassembler.c:decodeSImmOperand_5
Line
Count
Source
317
317
  { \
318
317
    CS_ASSERT(isUIntN(N, Imm) && "Invalid immediate"); \
319
317
\
320
317
    MCOperand_CreateImm0(Inst, (SignExtend64((Imm), N))); \
321
317
    return MCDisassembler_Success; \
322
317
  }
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
2.21k
  { \
333
2.21k
    if (Imm == 0) \
334
2.21k
      return MCDisassembler_Fail; \
335
2.21k
    return CONCAT(decodeSImmOperand, N)(Inst, Imm, Address, \
336
2.21k
                Decoder); \
337
2.21k
  }
RISCVDisassembler.c:decodeSImmNonZeroOperand_6
Line
Count
Source
332
1.58k
  { \
333
1.58k
    if (Imm == 0) \
334
1.58k
      return MCDisassembler_Fail; \
335
1.58k
    return CONCAT(decodeSImmOperand, N)(Inst, Imm, Address, \
336
1.58k
                Decoder); \
337
1.58k
  }
RISCVDisassembler.c:decodeSImmNonZeroOperand_10
Line
Count
Source
332
624
  { \
333
624
    if (Imm == 0) \
334
624
      return MCDisassembler_Fail; \
335
624
    return CONCAT(decodeSImmOperand, N)(Inst, Imm, Address, \
336
624
                Decoder); \
337
624
  }
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
3.61k
  { \
346
3.61k
    CS_ASSERT(isUIntN(N, Imm) && "Invalid immediate"); \
347
3.61k
\
348
3.61k
    MCOperand_CreateImm0(Inst, (SignExtend64((Imm << 1), N))); \
349
3.61k
    return MCDisassembler_Success; \
350
3.61k
  }
RISCVDisassembler.c:decodeSImmOperandAndLsl1_12
Line
Count
Source
345
900
  { \
346
900
    CS_ASSERT(isUIntN(N, Imm) && "Invalid immediate"); \
347
900
\
348
900
    MCOperand_CreateImm0(Inst, (SignExtend64((Imm << 1), N))); \
349
900
    return MCDisassembler_Success; \
350
900
  }
RISCVDisassembler.c:decodeSImmOperandAndLsl1_9
Line
Count
Source
345
1.15k
  { \
346
1.15k
    CS_ASSERT(isUIntN(N, Imm) && "Invalid immediate"); \
347
1.15k
\
348
1.15k
    MCOperand_CreateImm0(Inst, (SignExtend64((Imm << 1), N))); \
349
1.15k
    return MCDisassembler_Success; \
350
1.15k
  }
RISCVDisassembler.c:decodeSImmOperandAndLsl1_13
Line
Count
Source
345
184
  { \
346
184
    CS_ASSERT(isUIntN(N, Imm) && "Invalid immediate"); \
347
184
\
348
184
    MCOperand_CreateImm0(Inst, (SignExtend64((Imm << 1), N))); \
349
184
    return MCDisassembler_Success; \
350
184
  }
RISCVDisassembler.c:decodeSImmOperandAndLsl1_21
Line
Count
Source
345
1.37k
  { \
346
1.37k
    CS_ASSERT(isUIntN(N, Imm) && "Invalid immediate"); \
347
1.37k
\
348
1.37k
    MCOperand_CreateImm0(Inst, (SignExtend64((Imm << 1), N))); \
349
1.37k
    return MCDisassembler_Success; \
350
1.37k
  }
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.85k
{
359
1.85k
  CS_ASSERT(isUIntN(6, Imm) && "Invalid immediate");
360
1.85k
  if (Imm > 31) {
361
647
    Imm = (SignExtend64((Imm), 6) & 0xfffff);
362
647
  }
363
1.85k
  MCOperand_CreateImm0(Inst, (Imm));
364
1.85k
  return MCDisassembler_Success;
365
1.85k
}
366
367
static DecodeStatus decodeFRMArg(MCInst *Inst, uint32_t Imm, int64_t Address,
368
         const void *Decoder)
369
1.11k
{
370
1.11k
  CS_ASSERT(isUIntN(3, Imm) && "Invalid immediate");
371
1.11k
  if (!RISCVFPRndMode_isValidRoundingMode(Imm))
372
6
    return MCDisassembler_Fail;
373
374
1.11k
  MCOperand_CreateImm0(Inst, (Imm));
375
1.11k
  return MCDisassembler_Success;
376
1.11k
}
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
1.21k
{
415
1.21k
  CS_ASSERT(isUIntN(6, Imm) && "Invalid immediate");
416
417
1.21k
  if (!RISCV_getFeatureBits(Inst->csh->mode, RISCV_Feature64Bit) &&
418
338
      !isUIntN(5, Imm))
419
6
    return MCDisassembler_Fail;
420
421
1.21k
  MCOperand_CreateImm0(Inst, (Imm));
422
1.21k
  return MCDisassembler_Success;
423
1.21k
}
424
425
static DecodeStatus decodeUImmLog2XLenNonZeroOperand(MCInst *Inst, uint32_t Imm,
426
                 int64_t Address,
427
                 const void *Decoder)
428
1.11k
{
429
1.11k
  if (Imm == 0)
430
0
    return MCDisassembler_Fail;
431
1.11k
  return decodeUImmLog2XLenOperand(Inst, Imm, Address, Decoder);
432
1.11k
}
433
434
#include "RISCVGenDisassemblerTables.inc"
435
436
static DecodeStatus decodeRVCInstrRdRs1ImmZero(MCInst *Inst, uint32_t Insn,
437
                 uint64_t Address,
438
                 const void *Decoder)
439
445
{
440
445
  uint32_t Rd = fieldFromInstruction_4(Insn, 7, 5);
441
445
  DecodeStatus Result =
442
445
    DecodeGPRNoX0RegisterClass(Inst, Rd, Address, Decoder);
443
445
  (void)Result;
444
445
  CS_ASSERT(Result == MCDisassembler_Success && "Invalid register");
445
445
  MCInst_addOperand2(Inst, (MCInst_getOperand(Inst, (0))));
446
445
  MCOperand_CreateImm0(Inst, (0));
447
445
  return MCDisassembler_Success;
448
445
}
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
63
{
464
63
  MCOperand_CreateReg0(Inst, (RISCV_X0));
465
63
  uint32_t SImm6 = fieldFromInstruction_4(Insn, 12, 1) << 5 |
466
63
       fieldFromInstruction_4(Insn, 2, 5);
467
63
  DecodeStatus Result =
468
63
    CONCAT(decodeSImmOperand, 6)(Inst, SImm6, Address, Decoder);
469
63
  (void)Result;
470
63
  CS_ASSERT(Result == MCDisassembler_Success && "Invalid immediate");
471
63
  return MCDisassembler_Success;
472
63
}
473
474
static DecodeStatus decodeRVCInstrRdRs1UImm(MCInst *Inst, uint32_t Insn,
475
              uint64_t Address,
476
              const void *Decoder)
477
137
{
478
137
  MCOperand_CreateReg0(Inst, (RISCV_X0));
479
137
  MCInst_addOperand2(Inst, (MCInst_getOperand(Inst, (0))));
480
137
  uint32_t UImm6 = fieldFromInstruction_4(Insn, 12, 1) << 5 |
481
137
       fieldFromInstruction_4(Insn, 2, 5);
482
137
  DecodeStatus Result =
483
137
    CONCAT(decodeUImmOperand, 6)(Inst, UImm6, Address, Decoder);
484
137
  (void)Result;
485
137
  CS_ASSERT(Result == MCDisassembler_Success && "Invalid immediate");
486
137
  return MCDisassembler_Success;
487
137
}
488
489
static DecodeStatus decodeRVCInstrRdRs2(MCInst *Inst, uint32_t Insn,
490
          uint64_t Address, const void *Decoder)
491
118
{
492
118
  uint32_t Rd = fieldFromInstruction_4(Insn, 7, 5);
493
118
  uint32_t Rs2 = fieldFromInstruction_4(Insn, 2, 5);
494
118
  DecodeGPRRegisterClass(Inst, Rd, Address, Decoder);
495
118
  DecodeGPRRegisterClass(Inst, Rs2, Address, Decoder);
496
118
  return MCDisassembler_Success;
497
118
}
498
499
static DecodeStatus decodeRVCInstrRdRs1Rs2(MCInst *Inst, uint32_t Insn,
500
             uint64_t Address,
501
             const void *Decoder)
502
98
{
503
98
  uint32_t Rd = fieldFromInstruction_4(Insn, 7, 5);
504
98
  uint32_t Rs2 = fieldFromInstruction_4(Insn, 2, 5);
505
98
  DecodeGPRRegisterClass(Inst, Rd, Address, Decoder);
506
98
  MCInst_addOperand2(Inst, (MCInst_getOperand(Inst, (0))));
507
98
  DecodeGPRRegisterClass(Inst, Rs2, Address, Decoder);
508
98
  return MCDisassembler_Success;
509
98
}
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
277
{
550
277
  uint32_t Rs1 = fieldFromInstruction_4(Insn, 0, 5);
551
277
  uint32_t Rs2 = fieldFromInstruction_4(Insn, 5, 5);
552
277
  DecodeGPRRegisterClass(Inst, Rs1, Address, Decoder);
553
277
  DecodeGPRRegisterClass(Inst, Rs2, Address, Decoder);
554
277
  return MCDisassembler_Success;
555
277
}
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
24.2k
{
568
24.2k
  const MCInstrDesc *MCID = MCInstrDesc_get(MCInst_getOpcode(MI),
569
24.2k
              RISCVDescs.Insts,
570
24.2k
              ARR_SIZE(RISCVDescs.Insts));
571
24.2k
  MCOperand SPReg;
572
24.2k
  SPReg.MachineOperandType = kRegister;
573
24.2k
  SPReg.Kind = kRegister;
574
24.2k
  SPReg.RegVal = RISCV_X2;
575
80.4k
  for (unsigned i = 0; i < MCID->NumOperands; i++)
576
56.2k
    if (MCID->OpInfo[i].RegClass == RISCV_SPRegClassID)
577
5.55k
      MCInst_insert0(MI, i, &SPReg);
578
24.2k
}
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
38.0k
{
584
  // TODO: This will need modification when supporting instruction set
585
  // extensions with instructions > 32-bits (up to 176 bits wide).
586
38.0k
  uint32_t Insn;
587
38.0k
  DecodeStatus Result;
588
589
38.0k
#define TRY_TO_DECODE_WITH_ADDITIONAL_OPERATION( \
590
38.0k
  width, FEATURE_CHECKS, DECODER_TABLE, DESC, ADDITIONAL_OPERATION) \
591
485k
  do { \
592
525k
    if (FEATURE_CHECKS) { \
593
141k
      Result = decodeInstruction_##width( \
594
141k
        DECODER_TABLE, MI, Insn, Address, NULL); \
595
141k
      if (Result != MCDisassembler_Fail) { \
596
37.3k
        ADDITIONAL_OPERATION; \
597
37.3k
        return Result; \
598
37.3k
      } \
599
141k
    } \
600
485k
  } while (false)
601
38.0k
#define TRY_TO_DECODE_AND_ADD_SP(width, FEATURE_CHECKS, DECODER_TABLE, DESC) \
602
48.2k
  TRY_TO_DECODE_WITH_ADDITIONAL_OPERATION( \
603
48.2k
    width, FEATURE_CHECKS, DECODER_TABLE, DESC, addSPOperands(MI))
604
38.0k
#define TRY_TO_DECODE(width, FEATURE_CHECKS, DECODER_TABLE, DESC) \
605
436k
  TRY_TO_DECODE_WITH_ADDITIONAL_OPERATION( \
606
436k
    width, FEATURE_CHECKS, DECODER_TABLE, DESC, (void)NULL)
607
38.0k
#define TRY_TO_DECODE_FEATURE(width, FEATURE, DECODER_TABLE, DESC) \
608
398k
  TRY_TO_DECODE(width, RISCV_getFeatureBits(MI->csh->mode, FEATURE), \
609
398k
          DECODER_TABLE, DESC)
610
611
  // It's a 32 bit instruction if bit 0 and 1 are 1.
612
38.0k
  if ((Bytes[0] & 0x3) == 0x3) {
613
13.4k
    if (BytesLen < 4) {
614
130
      *Size = 0;
615
130
      return MCDisassembler_Fail;
616
130
    }
617
13.3k
    *Size = 4;
618
619
13.3k
    Insn = readBytes32(MI, Bytes);
620
621
13.3k
    TRY_TO_DECODE(4,
622
13.3k
            RISCV_getFeatureBits(MI->csh->mode,
623
13.3k
               RISCV_FeatureStdExtZdinx) &&
624
13.3k
              !RISCV_getFeatureBits(MI->csh->mode,
625
13.3k
                  RISCV_Feature64Bit),
626
13.3k
            DecoderTableRV32Zdinx32,
627
13.3k
            "RV32Zdinx table (Double in Integer and rv32)");
628
13.3k
    TRY_TO_DECODE(4,
629
13.2k
            RISCV_getFeatureBits(MI->csh->mode,
630
13.2k
               RISCV_FeatureStdExtZacas) &&
631
13.2k
              !RISCV_getFeatureBits(MI->csh->mode,
632
13.2k
                  RISCV_Feature64Bit),
633
13.2k
            DecoderTableRV32Zacas32,
634
13.2k
            "RV32Zacas table (Compare-And-Swap and rv32)");
635
13.2k
    TRY_TO_DECODE_FEATURE(4, RISCV_FeatureStdExtZfinx,
636
13.2k
              DecoderTableRVZfinx32,
637
13.2k
              "RVZfinx table (Float in Integer)");
638
13.2k
    TRY_TO_DECODE_FEATURE(4, RISCV_FeatureVendorXVentanaCondOps,
639
13.2k
              DecoderTableXVentana32,
640
13.2k
              "Ventana custom opcode table");
641
13.2k
    TRY_TO_DECODE_FEATURE(4, RISCV_FeatureVendorXTHeadBa,
642
13.2k
              DecoderTableXTHeadBa32,
643
13.2k
              "XTHeadBa custom opcode table");
644
13.2k
    TRY_TO_DECODE_FEATURE(4, RISCV_FeatureVendorXTHeadBb,
645
13.2k
              DecoderTableXTHeadBb32,
646
13.2k
              "XTHeadBb custom opcode table");
647
13.2k
    TRY_TO_DECODE_FEATURE(4, RISCV_FeatureVendorXTHeadBs,
648
13.2k
              DecoderTableXTHeadBs32,
649
13.2k
              "XTHeadBs custom opcode table");
650
13.2k
    TRY_TO_DECODE_FEATURE(4, RISCV_FeatureVendorXTHeadCondMov,
651
13.2k
              DecoderTableXTHeadCondMov32,
652
13.2k
              "XTHeadCondMov custom opcode table");
653
13.2k
    TRY_TO_DECODE_FEATURE(4, RISCV_FeatureVendorXTHeadCmo,
654
13.2k
              DecoderTableXTHeadCmo32,
655
13.2k
              "XTHeadCmo custom opcode table");
656
13.2k
    TRY_TO_DECODE_FEATURE(4, RISCV_FeatureVendorXTHeadFMemIdx,
657
13.2k
              DecoderTableXTHeadFMemIdx32,
658
13.2k
              "XTHeadFMemIdx custom opcode table");
659
13.2k
    TRY_TO_DECODE_FEATURE(4, RISCV_FeatureVendorXTHeadMac,
660
13.2k
              DecoderTableXTHeadMac32,
661
13.2k
              "XTHeadMac custom opcode table");
662
13.2k
    TRY_TO_DECODE_FEATURE(4, RISCV_FeatureVendorXTHeadMemIdx,
663
13.2k
              DecoderTableXTHeadMemIdx32,
664
13.2k
              "XTHeadMemIdx custom opcode table");
665
13.2k
    TRY_TO_DECODE_FEATURE(4, RISCV_FeatureVendorXTHeadMemPair,
666
13.2k
              DecoderTableXTHeadMemPair32,
667
13.2k
              "XTHeadMemPair custom opcode table");
668
13.2k
    TRY_TO_DECODE_FEATURE(4, RISCV_FeatureVendorXTHeadSync,
669
13.2k
              DecoderTableXTHeadSync32,
670
13.2k
              "XTHeadSync custom opcode table");
671
13.2k
    TRY_TO_DECODE_FEATURE(4, RISCV_FeatureVendorXTHeadVdot,
672
13.2k
              DecoderTableXTHeadVdot32,
673
13.2k
              "XTHeadVdot custom opcode table");
674
13.2k
    TRY_TO_DECODE_FEATURE(4, RISCV_FeatureVendorXSfvcp,
675
13.2k
              DecoderTableXSfvcp32,
676
13.2k
              "SiFive VCIX custom opcode table");
677
13.2k
    TRY_TO_DECODE_FEATURE(
678
13.2k
      4, RISCV_FeatureVendorXSfvqmaccdod,
679
13.2k
      DecoderTableXSfvqmaccdod32,
680
13.2k
      "SiFive Matrix Multiplication (2x8 and 8x2) Instruction opcode table");
681
13.2k
    TRY_TO_DECODE_FEATURE(
682
13.2k
      4, RISCV_FeatureVendorXSfvqmaccqoq,
683
13.2k
      DecoderTableXSfvqmaccqoq32,
684
13.2k
      "SiFive Matrix Multiplication (4x8 and 8x4) Instruction opcode table");
685
13.2k
    TRY_TO_DECODE_FEATURE(
686
13.2k
      4, RISCV_FeatureVendorXSfvfwmaccqqq,
687
13.2k
      DecoderTableXSfvfwmaccqqq32,
688
13.2k
      "SiFive Matrix Multiplication Instruction opcode table");
689
13.2k
    TRY_TO_DECODE_FEATURE(
690
13.2k
      4, RISCV_FeatureVendorXSfvfnrclipxfqf,
691
13.2k
      DecoderTableXSfvfnrclipxfqf32,
692
13.2k
      "SiFive FP32-to-int8 Ranged Clip Instructions opcode table");
693
13.2k
    TRY_TO_DECODE_FEATURE(
694
13.2k
      4, RISCV_FeatureVendorXCVbitmanip,
695
13.2k
      DecoderTableXCVbitmanip32,
696
13.2k
      "CORE-V Bit Manipulation custom opcode table");
697
13.2k
    TRY_TO_DECODE_FEATURE(4, RISCV_FeatureVendorXCVelw,
698
13.2k
              DecoderTableXCVelw32,
699
13.2k
              "CORE-V Event load custom opcode table");
700
13.2k
    TRY_TO_DECODE_FEATURE(4, RISCV_FeatureVendorXCVmac,
701
12.8k
              DecoderTableXCVmac32,
702
12.8k
              "CORE-V MAC custom opcode table");
703
12.8k
    TRY_TO_DECODE_FEATURE(4, RISCV_FeatureVendorXCVmem,
704
12.1k
              DecoderTableXCVmem32,
705
12.1k
              "CORE-V MEM custom opcode table");
706
12.1k
    TRY_TO_DECODE_FEATURE(4, RISCV_FeatureVendorXCValu,
707
12.0k
              DecoderTableXCValu32,
708
12.0k
              "CORE-V ALU custom opcode table");
709
12.0k
    TRY_TO_DECODE_FEATURE(
710
11.8k
      4, RISCV_FeatureVendorXCVsimd, DecoderTableXCVsimd32,
711
11.8k
      "CORE-V SIMD extensions custom opcode table");
712
11.8k
    TRY_TO_DECODE_FEATURE(
713
11.7k
      4, RISCV_FeatureVendorXCVbi, DecoderTableXCVbi32,
714
11.7k
      "CORE-V Immediate Branching custom opcode table");
715
11.7k
    TRY_TO_DECODE(4, true, DecoderTable32, "RISCV32 table");
716
717
207
    return MCDisassembler_Fail;
718
11.7k
  }
719
720
24.5k
  if (BytesLen < 2) {
721
140
    *Size = 0;
722
140
    return MCDisassembler_Fail;
723
140
  }
724
24.4k
  *Size = 2;
725
726
24.4k
  Insn = readBytes16(MI, Bytes);
727
24.4k
  TRY_TO_DECODE_AND_ADD_SP(
728
23.8k
    2, !RISCV_getFeatureBits(MI->csh->mode, RISCV_Feature64Bit),
729
23.8k
    DecoderTableRISCV32Only_16,
730
23.8k
    "RISCV32Only_16 table (16-bit Instruction)");
731
23.8k
  TRY_TO_DECODE_FEATURE(2, RISCV_FeatureStdExtZicfiss,
732
23.8k
            DecoderTableZicfiss16,
733
23.8k
            "RVZicfiss table (Shadow Stack)");
734
23.8k
  TRY_TO_DECODE_FEATURE(2, RISCV_FeatureStdExtZcmt, DecoderTableRVZcmt16,
735
23.8k
            "Zcmt table (16-bit Table Jump Instructions)");
736
23.8k
  TRY_TO_DECODE_FEATURE(
737
23.8k
    2, RISCV_FeatureStdExtZcmp, DecoderTableRVZcmp16,
738
23.8k
    "Zcmp table (16-bit Push/Pop & Double Move Instructions)");
739
23.8k
  TRY_TO_DECODE_AND_ADD_SP(2, true, DecoderTable16,
740
185
         "RISCV_C table (16-bit Instruction)");
741
742
185
  return MCDisassembler_Fail;
743
23.8k
}
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
38.0k
{
749
38.0k
  RISCV_init_cs_detail(MI);
750
38.0k
  MI->MRI = (MCRegisterInfo *)Info;
751
  return RISCV_getInstruction(MI, Size, Bytes, ByteLen, Address, NULL) !=
752
38.0k
         MCDisassembler_Fail;
753
38.0k
}