Coverage Report

Created: 2025-10-14 06:42

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/capstonenext/arch/RISCV/RISCVInstPrinter.c
Line
Count
Source
1
//===-- RISCVInstPrinter.cpp - Convert RISCV MCInst to asm syntax ---------===//
2
//
3
//                     The LLVM Compiler Infrastructure
4
//
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
7
//
8
//===----------------------------------------------------------------------===//
9
//
10
// This class prints an RISCV MCInst to a .s file.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#ifdef CAPSTONE_HAS_RISCV
15
16
#include <stdio.h> // DEBUG
17
#include <stdlib.h>
18
#include <string.h>
19
#include <capstone/platform.h>
20
21
#include "RISCVInstPrinter.h"
22
#include "RISCVBaseInfo.h"
23
#include "../../MCInst.h"
24
#include "../../SStream.h"
25
#include "../../MCRegisterInfo.h"
26
#include "../../utils.h"
27
#include "../../Mapping.h"
28
#include "RISCVMapping.h"
29
30
//#include "RISCVDisassembler.h"
31
32
#define GET_REGINFO_ENUM
33
#define GET_REGINFO_MC_DESC
34
#include "RISCVGenRegisterInfo.inc"
35
#define GET_INSTRINFO_ENUM
36
#include "RISCVGenInstrInfo.inc"
37
38
// Autogenerated by tblgen.
39
static void printInstruction(MCInst *MI, SStream *O, MCRegisterInfo *MRI);
40
static bool printAliasInstr(MCInst *MI, SStream *OS, void *info);
41
static void printOperand(MCInst *MI, unsigned OpNo, SStream *O);
42
static void printFenceArg(MCInst *MI, unsigned OpNo, SStream *O);
43
static void printCSRSystemRegister(MCInst *, unsigned, SStream *);
44
static void printFRMArg(MCInst *MI, unsigned OpNo, SStream *O);
45
static void printCustomAliasOperand(MCInst *, unsigned, unsigned, SStream *);
46
/// getRegisterName - This method is automatically generated by tblgen
47
/// from the register set description.  This returns the assembler name
48
/// for the specified register.
49
static const char *getRegisterName(unsigned RegNo, unsigned AltIdx);
50
51
// Include the auto-generated portion of the assembly writer.
52
#define PRINT_ALIAS_INSTR
53
#include "RISCVGenAsmWriter.inc"
54
55
static void fixDetailOfEffectiveAddr(MCInst *MI)
56
9.46k
{
57
  // Operands for load and store instructions in RISCV vary widely
58
9.46k
  unsigned id = MI->flat_insn->id;
59
9.46k
  unsigned reg = 0;
60
9.46k
  int64_t imm = 0;
61
9.46k
  uint8_t access = 0;
62
63
9.46k
  switch (id) {
64
0
  case RISCV_INS_C_FLD:
65
0
  case RISCV_INS_C_LW:
66
0
  case RISCV_INS_C_FLW:
67
0
  case RISCV_INS_C_LD:
68
0
  case RISCV_INS_C_FSD:
69
0
  case RISCV_INS_C_SW:
70
0
  case RISCV_INS_C_FSW:
71
0
  case RISCV_INS_C_SD:
72
0
  case RISCV_INS_C_FLDSP:
73
0
  case RISCV_INS_C_LWSP:
74
0
  case RISCV_INS_C_FLWSP:
75
0
  case RISCV_INS_C_LDSP:
76
0
  case RISCV_INS_C_FSDSP:
77
0
  case RISCV_INS_C_SWSP:
78
0
  case RISCV_INS_C_FSWSP:
79
0
  case RISCV_INS_C_SDSP:
80
281
  case RISCV_INS_FLW:
81
601
  case RISCV_INS_FSW:
82
808
  case RISCV_INS_FLD:
83
832
  case RISCV_INS_FSD:
84
1.12k
  case RISCV_INS_LB:
85
1.23k
  case RISCV_INS_LBU:
86
1.50k
  case RISCV_INS_LD:
87
1.61k
  case RISCV_INS_LH:
88
1.85k
  case RISCV_INS_LHU:
89
2.31k
  case RISCV_INS_LW:
90
2.42k
  case RISCV_INS_LWU:
91
2.53k
  case RISCV_INS_SB:
92
2.78k
  case RISCV_INS_SD:
93
3.20k
  case RISCV_INS_SH:
94
4.10k
  case RISCV_INS_SW: {
95
4.10k
    CS_ASSERT(3 == MI->flat_insn->detail->riscv.op_count);
96
4.10k
    CS_ASSERT(RISCV_OP_REG == RISCV_get_detail_op(MI, -3)->type);
97
4.10k
    CS_ASSERT(RISCV_OP_IMM == RISCV_get_detail_op(MI, -2)->type);
98
4.10k
    CS_ASSERT(RISCV_OP_REG == RISCV_get_detail_op(MI, -1)->type);
99
100
4.10k
    imm = RISCV_get_detail_op(MI, -2)->imm;
101
4.10k
    reg = RISCV_get_detail_op(MI, -1)->reg;
102
4.10k
    access = RISCV_get_detail_op(MI, -1)->access;
103
104
4.10k
    RISCV_get_detail_op(MI, -2)->type = RISCV_OP_MEM;
105
4.10k
    RISCV_get_detail_op(MI, -2)->mem.base = reg;
106
4.10k
    RISCV_get_detail_op(MI, -2)->mem.disp = imm;
107
4.10k
    RISCV_get_detail_op(MI, -2)->access = access;
108
109
4.10k
    RISCV_dec_op_count(MI);
110
111
4.10k
    break;
112
3.20k
  }
113
37
  case RISCV_INS_LR_W:
114
74
  case RISCV_INS_LR_W_AQ:
115
212
  case RISCV_INS_LR_W_AQ_RL:
116
249
  case RISCV_INS_LR_W_RL:
117
268
  case RISCV_INS_LR_D:
118
302
  case RISCV_INS_LR_D_AQ:
119
832
  case RISCV_INS_LR_D_AQ_RL:
120
974
  case RISCV_INS_LR_D_RL: {
121
974
    CS_ASSERT(2 == MI->flat_insn->detail->riscv.op_count);
122
974
    CS_ASSERT(RISCV_OP_REG == RISCV_get_detail_op(MI, -1)->type);
123
974
    CS_ASSERT(RISCV_OP_REG == RISCV_get_detail_op(MI, -2)->type);
124
125
974
    reg = RISCV_get_detail_op(MI, -1)->reg;
126
127
974
    RISCV_get_detail_op(MI, -1)->type = RISCV_OP_MEM;
128
974
    RISCV_get_detail_op(MI, -1)->mem.base = reg;
129
974
    RISCV_get_detail_op(MI, -1)->mem.disp = 0;
130
131
974
    break;
132
832
  }
133
36
  case RISCV_INS_SC_W:
134
109
  case RISCV_INS_SC_W_AQ:
135
213
  case RISCV_INS_SC_W_AQ_RL:
136
250
  case RISCV_INS_SC_W_RL:
137
284
  case RISCV_INS_SC_D:
138
306
  case RISCV_INS_SC_D_AQ:
139
407
  case RISCV_INS_SC_D_AQ_RL:
140
443
  case RISCV_INS_SC_D_RL:
141
529
  case RISCV_INS_AMOADD_D:
142
540
  case RISCV_INS_AMOADD_D_AQ:
143
721
  case RISCV_INS_AMOADD_D_AQ_RL:
144
748
  case RISCV_INS_AMOADD_D_RL:
145
776
  case RISCV_INS_AMOADD_W:
146
818
  case RISCV_INS_AMOADD_W_AQ:
147
932
  case RISCV_INS_AMOADD_W_AQ_RL:
148
1.06k
  case RISCV_INS_AMOADD_W_RL:
149
1.10k
  case RISCV_INS_AMOAND_D:
150
1.14k
  case RISCV_INS_AMOAND_D_AQ:
151
1.16k
  case RISCV_INS_AMOAND_D_AQ_RL:
152
1.17k
  case RISCV_INS_AMOAND_D_RL:
153
1.19k
  case RISCV_INS_AMOAND_W:
154
1.21k
  case RISCV_INS_AMOAND_W_AQ:
155
1.28k
  case RISCV_INS_AMOAND_W_AQ_RL:
156
1.30k
  case RISCV_INS_AMOAND_W_RL:
157
1.32k
  case RISCV_INS_AMOMAXU_D:
158
1.38k
  case RISCV_INS_AMOMAXU_D_AQ:
159
1.45k
  case RISCV_INS_AMOMAXU_D_AQ_RL:
160
1.47k
  case RISCV_INS_AMOMAXU_D_RL:
161
1.48k
  case RISCV_INS_AMOMAXU_W:
162
1.51k
  case RISCV_INS_AMOMAXU_W_AQ:
163
1.58k
  case RISCV_INS_AMOMAXU_W_AQ_RL:
164
1.65k
  case RISCV_INS_AMOMAXU_W_RL:
165
1.68k
  case RISCV_INS_AMOMAX_D:
166
1.72k
  case RISCV_INS_AMOMAX_D_AQ:
167
1.75k
  case RISCV_INS_AMOMAX_D_AQ_RL:
168
1.82k
  case RISCV_INS_AMOMAX_D_RL:
169
1.89k
  case RISCV_INS_AMOMAX_W:
170
1.98k
  case RISCV_INS_AMOMAX_W_AQ:
171
2.04k
  case RISCV_INS_AMOMAX_W_AQ_RL:
172
2.09k
  case RISCV_INS_AMOMAX_W_RL:
173
2.10k
  case RISCV_INS_AMOMINU_D:
174
2.14k
  case RISCV_INS_AMOMINU_D_AQ:
175
2.18k
  case RISCV_INS_AMOMINU_D_AQ_RL:
176
2.25k
  case RISCV_INS_AMOMINU_D_RL:
177
2.28k
  case RISCV_INS_AMOMINU_W:
178
2.36k
  case RISCV_INS_AMOMINU_W_AQ:
179
2.67k
  case RISCV_INS_AMOMINU_W_AQ_RL:
180
2.75k
  case RISCV_INS_AMOMINU_W_RL:
181
2.96k
  case RISCV_INS_AMOMIN_D:
182
3.02k
  case RISCV_INS_AMOMIN_D_AQ:
183
3.09k
  case RISCV_INS_AMOMIN_D_AQ_RL:
184
3.12k
  case RISCV_INS_AMOMIN_D_RL:
185
3.16k
  case RISCV_INS_AMOMIN_W:
186
3.17k
  case RISCV_INS_AMOMIN_W_AQ:
187
3.21k
  case RISCV_INS_AMOMIN_W_AQ_RL:
188
3.25k
  case RISCV_INS_AMOMIN_W_RL:
189
3.26k
  case RISCV_INS_AMOOR_D:
190
3.27k
  case RISCV_INS_AMOOR_D_AQ:
191
3.36k
  case RISCV_INS_AMOOR_D_AQ_RL:
192
3.38k
  case RISCV_INS_AMOOR_D_RL:
193
3.45k
  case RISCV_INS_AMOOR_W:
194
3.51k
  case RISCV_INS_AMOOR_W_AQ:
195
3.53k
  case RISCV_INS_AMOOR_W_AQ_RL:
196
3.60k
  case RISCV_INS_AMOOR_W_RL:
197
3.62k
  case RISCV_INS_AMOSWAP_D:
198
3.65k
  case RISCV_INS_AMOSWAP_D_AQ:
199
3.70k
  case RISCV_INS_AMOSWAP_D_AQ_RL:
200
3.75k
  case RISCV_INS_AMOSWAP_D_RL:
201
3.76k
  case RISCV_INS_AMOSWAP_W:
202
3.80k
  case RISCV_INS_AMOSWAP_W_AQ:
203
3.82k
  case RISCV_INS_AMOSWAP_W_AQ_RL:
204
3.87k
  case RISCV_INS_AMOSWAP_W_RL:
205
4.00k
  case RISCV_INS_AMOXOR_D:
206
4.04k
  case RISCV_INS_AMOXOR_D_AQ:
207
4.08k
  case RISCV_INS_AMOXOR_D_AQ_RL:
208
4.12k
  case RISCV_INS_AMOXOR_D_RL:
209
4.19k
  case RISCV_INS_AMOXOR_W:
210
4.21k
  case RISCV_INS_AMOXOR_W_AQ:
211
4.31k
  case RISCV_INS_AMOXOR_W_AQ_RL:
212
4.38k
  case RISCV_INS_AMOXOR_W_RL: {
213
4.38k
    CS_ASSERT(3 == MI->flat_insn->detail->riscv.op_count);
214
4.38k
    CS_ASSERT(RISCV_OP_REG == RISCV_get_detail_op(MI, -3)->type);
215
4.38k
    CS_ASSERT(RISCV_OP_REG == RISCV_get_detail_op(MI, -2)->type);
216
4.38k
    CS_ASSERT(RISCV_OP_REG == RISCV_get_detail_op(MI, -1)->type);
217
218
4.38k
    reg = RISCV_get_detail_op(MI, -1)->reg;
219
220
4.38k
    RISCV_get_detail_op(MI, -1)->type = RISCV_OP_MEM;
221
4.38k
    RISCV_get_detail_op(MI, -1)->mem.base = reg;
222
4.38k
    RISCV_get_detail_op(MI, -1)->mem.disp = 0;
223
224
4.38k
    break;
225
4.31k
  }
226
0
  default: {
227
0
    CS_ASSERT(0 && "id is not a RISC-V memory instruction");
228
0
    break;
229
4.31k
  }
230
9.46k
  }
231
9.46k
  return;
232
9.46k
}
233
234
//void RISCVInstPrinter::printInst(const MCInst *MI, raw_ostream &O,
235
//                                 StringRef Annot, const MCSubtargetInfo &STI)
236
void RISCV_printInst(MCInst *MI, SStream *O, void *info)
237
200k
{
238
200k
  MCRegisterInfo *MRI = (MCRegisterInfo *)info;
239
  //bool Res = false;
240
  //MCInst *NewMI = MI;
241
  // TODO: RISCV compressd instructions.
242
  //MCInst UncompressedMI;
243
  //if (!NoAliases)
244
  //Res = uncompressInst(UncompressedMI, *MI, MRI, STI);
245
  //if (Res)
246
  //NewMI = const_cast<MCInst *>(&UncompressedMI);
247
200k
  if (/*NoAliases ||*/ !printAliasInstr(MI, O, info))
248
148k
    printInstruction(MI, O, MRI);
249
  //printAnnotation(O, Annot);
250
  // fix load/store type insttuction
251
200k
  if (MI->csh->detail_opt &&
252
200k
      MI->flat_insn->detail->riscv.need_effective_addr)
253
10.9k
    fixDetailOfEffectiveAddr(MI);
254
255
200k
  return;
256
200k
}
257
258
static void printRegName(SStream *OS, unsigned RegNo)
259
348k
{
260
348k
  SStream_concat0(OS, getRegisterName(RegNo, RISCV_ABIRegAltName));
261
348k
}
262
263
/**
264
void RISCVInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
265
                                    raw_ostream &O, const char *Modifier) 
266
*/
267
static void printOperand(MCInst *MI, unsigned OpNo, SStream *O)
268
180k
{
269
180k
  unsigned reg;
270
180k
  int64_t Imm = 0;
271
272
180k
  RISCV_add_cs_detail(MI, OpNo);
273
274
180k
  MCOperand *MO = MCInst_getOperand(MI, OpNo);
275
276
180k
  if (MCOperand_isReg(MO)) {
277
153k
    reg = MCOperand_getReg(MO);
278
153k
    printRegName(O, reg);
279
153k
  } else {
280
27.6k
    CS_ASSERT(MCOperand_isImm(MO) &&
281
27.6k
        "Unknown operand kind in printOperand");
282
27.6k
    Imm = MCOperand_getImm(MO);
283
27.6k
    if (Imm >= 0) {
284
23.9k
      if (Imm > HEX_THRESHOLD)
285
15.0k
        SStream_concat(O, "0x%" PRIx64, Imm);
286
8.90k
      else
287
8.90k
        SStream_concat(O, "%" PRIu64, Imm);
288
23.9k
    } else {
289
3.67k
      if (Imm < -HEX_THRESHOLD)
290
3.62k
        SStream_concat(O, "-0x%" PRIx64, -Imm);
291
49
      else
292
49
        SStream_concat(O, "-%" PRIu64, -Imm);
293
3.67k
    }
294
27.6k
  }
295
296
  //CS_ASSERT(MO.isExpr() && "Unknown operand kind in printOperand");
297
298
180k
  return;
299
180k
}
300
301
static const char *getCSRSystemRegisterName(unsigned CsrNo)
302
113k
{
303
113k
  switch (CsrNo) {
304
  /*
305
   * From RISC-V Privileged Architecture Version 1.10.
306
   * In the same order as Table 2.5.
307
   */
308
649
  case 0x0000:
309
649
    return "ustatus";
310
366
  case 0x0004:
311
366
    return "uie";
312
240
  case 0x0005:
313
240
    return "utvec";
314
315
123
  case 0x0040:
316
123
    return "uscratch";
317
150
  case 0x0041:
318
150
    return "uepc";
319
611
  case 0x0042:
320
611
    return "ucause";
321
294
  case 0x0043:
322
294
    return "utval";
323
408
  case 0x0044:
324
408
    return "uip";
325
326
875
  case 0x0001:
327
875
    return "fflags";
328
884
  case 0x0002:
329
884
    return "frm";
330
1.07k
  case 0x0003:
331
1.07k
    return "fcsr";
332
333
1.36k
  case 0x0c00:
334
1.36k
    return "cycle";
335
2.31k
  case 0x0c01:
336
2.31k
    return "time";
337
645
  case 0x0c02:
338
645
    return "instret";
339
137
  case 0x0c03:
340
137
    return "hpmcounter3";
341
844
  case 0x0c04:
342
844
    return "hpmcounter4";
343
419
  case 0x0c05:
344
419
    return "hpmcounter5";
345
388
  case 0x0c06:
346
388
    return "hpmcounter6";
347
801
  case 0x0c07:
348
801
    return "hpmcounter7";
349
756
  case 0x0c08:
350
756
    return "hpmcounter8";
351
607
  case 0x0c09:
352
607
    return "hpmcounter9";
353
505
  case 0x0c0a:
354
505
    return "hpmcounter10";
355
849
  case 0x0c0b:
356
849
    return "hpmcounter11";
357
941
  case 0x0c0c:
358
941
    return "hpmcounter12";
359
559
  case 0x0c0d:
360
559
    return "hpmcounter13";
361
281
  case 0x0c0e:
362
281
    return "hpmcounter14";
363
501
  case 0x0c0f:
364
501
    return "hpmcounter15";
365
450
  case 0x0c10:
366
450
    return "hpmcounter16";
367
289
  case 0x0c11:
368
289
    return "hpmcounter17";
369
187
  case 0x0c12:
370
187
    return "hpmcounter18";
371
136
  case 0x0c13:
372
136
    return "hpmcounter19";
373
755
  case 0x0c14:
374
755
    return "hpmcounter20";
375
163
  case 0x0c15:
376
163
    return "hpmcounter21";
377
121
  case 0x0c16:
378
121
    return "hpmcounter22";
379
1.21k
  case 0x0c17:
380
1.21k
    return "hpmcounter23";
381
334
  case 0x0c18:
382
334
    return "hpmcounter24";
383
782
  case 0x0c19:
384
782
    return "hpmcounter25";
385
573
  case 0x0c1a:
386
573
    return "hpmcounter26";
387
1.11k
  case 0x0c1b:
388
1.11k
    return "hpmcounter27";
389
318
  case 0x0c1c:
390
318
    return "hpmcounter28";
391
141
  case 0x0c1d:
392
141
    return "hpmcounter29";
393
875
  case 0x0c1e:
394
875
    return "hpmcounter30";
395
205
  case 0x0c1f:
396
205
    return "hpmcounter31";
397
566
  case 0x0c80:
398
566
    return "cycleh";
399
187
  case 0x0c81:
400
187
    return "timeh";
401
1.60k
  case 0x0c82:
402
1.60k
    return "instreth";
403
468
  case 0x0c83:
404
468
    return "hpmcounter3h";
405
188
  case 0x0c84:
406
188
    return "hpmcounter4h";
407
108
  case 0x0c85:
408
108
    return "hpmcounter5h";
409
605
  case 0x0c86:
410
605
    return "hpmcounter6h";
411
325
  case 0x0c87:
412
325
    return "hpmcounter7h";
413
416
  case 0x0c88:
414
416
    return "hpmcounter8h";
415
217
  case 0x0c89:
416
217
    return "hpmcounter9h";
417
570
  case 0x0c8a:
418
570
    return "hpmcounter10h";
419
420
  case 0x0c8b:
420
420
    return "hpmcounter11h";
421
678
  case 0x0c8c:
422
678
    return "hpmcounter12h";
423
989
  case 0x0c8d:
424
989
    return "hpmcounter13h";
425
252
  case 0x0c8e:
426
252
    return "hpmcounter14h";
427
841
  case 0x0c8f:
428
841
    return "hpmcounter15h";
429
893
  case 0x0c90:
430
893
    return "hpmcounter16h";
431
148
  case 0x0c91:
432
148
    return "hpmcounter17h";
433
826
  case 0x0c92:
434
826
    return "hpmcounter18h";
435
320
  case 0x0c93:
436
320
    return "hpmcounter19h";
437
251
  case 0x0c94:
438
251
    return "hpmcounter20h";
439
354
  case 0x0c95:
440
354
    return "hpmcounter21h";
441
144
  case 0x0c96:
442
144
    return "hpmcounter22h";
443
143
  case 0x0c97:
444
143
    return "hpmcounter23h";
445
211
  case 0x0c98:
446
211
    return "hpmcounter24h";
447
720
  case 0x0c99:
448
720
    return "hpmcounter25h";
449
377
  case 0x0c9a:
450
377
    return "hpmcounter26h";
451
943
  case 0x0c9b:
452
943
    return "hpmcounter27h";
453
1.38k
  case 0x0c9c:
454
1.38k
    return "hpmcounter28h";
455
1.36k
  case 0x0c9d:
456
1.36k
    return "hpmcounter29h";
457
439
  case 0x0c9e:
458
439
    return "hpmcounter30h";
459
779
  case 0x0c9f:
460
779
    return "hpmcounter31h";
461
462
453
  case 0x0100:
463
453
    return "sstatus";
464
173
  case 0x0102:
465
173
    return "sedeleg";
466
802
  case 0x0103:
467
802
    return "sideleg";
468
369
  case 0x0104:
469
369
    return "sie";
470
510
  case 0x0105:
471
510
    return "stvec";
472
372
  case 0x0106:
473
372
    return "scounteren";
474
475
166
  case 0x0140:
476
166
    return "sscratch";
477
155
  case 0x0141:
478
155
    return "sepc";
479
160
  case 0x0142:
480
160
    return "scause";
481
382
  case 0x0143:
482
382
    return "stval";
483
641
  case 0x0144:
484
641
    return "sip";
485
486
100
  case 0x0180:
487
100
    return "satp";
488
489
128
  case 0x0f11:
490
128
    return "mvendorid";
491
302
  case 0x0f12:
492
302
    return "marchid";
493
520
  case 0x0f13:
494
520
    return "mimpid";
495
28
  case 0x0f14:
496
28
    return "mhartid";
497
498
133
  case 0x0300:
499
133
    return "mstatus";
500
155
  case 0x0301:
501
155
    return "misa";
502
856
  case 0x0302:
503
856
    return "medeleg";
504
127
  case 0x0303:
505
127
    return "mideleg";
506
220
  case 0x0304:
507
220
    return "mie";
508
498
  case 0x0305:
509
498
    return "mtvec";
510
151
  case 0x0306:
511
151
    return "mcounteren";
512
513
295
  case 0x0340:
514
295
    return "mscratch";
515
869
  case 0x0341:
516
869
    return "mepc";
517
228
  case 0x0342:
518
228
    return "mcause";
519
192
  case 0x0343:
520
192
    return "mtval";
521
1.36k
  case 0x0344:
522
1.36k
    return "mip";
523
524
100
  case 0x03a0:
525
100
    return "pmpcfg0";
526
247
  case 0x03a1:
527
247
    return "pmpcfg1";
528
589
  case 0x03a2:
529
589
    return "pmpcfg2";
530
192
  case 0x03a3:
531
192
    return "pmpcfg3";
532
561
  case 0x03b0:
533
561
    return "pmpaddr0";
534
327
  case 0x03b1:
535
327
    return "pmpaddr1";
536
603
  case 0x03b2:
537
603
    return "pmpaddr2";
538
501
  case 0x03b3:
539
501
    return "pmpaddr3";
540
107
  case 0x03b4:
541
107
    return "pmpaddr4";
542
511
  case 0x03b5:
543
511
    return "pmpaddr5";
544
74
  case 0x03b6:
545
74
    return "pmpaddr6";
546
121
  case 0x03b7:
547
121
    return "pmpaddr7";
548
118
  case 0x03b8:
549
118
    return "pmpaddr8";
550
561
  case 0x03b9:
551
561
    return "pmpaddr9";
552
155
  case 0x03ba:
553
155
    return "pmpaddr10";
554
311
  case 0x03bb:
555
311
    return "pmpaddr11";
556
565
  case 0x03bc:
557
565
    return "pmpaddr12";
558
203
  case 0x03bd:
559
203
    return "pmpaddr13";
560
370
  case 0x03be:
561
370
    return "pmpaddr14";
562
857
  case 0x03bf:
563
857
    return "pmpaddr15";
564
565
256
  case 0x0b00:
566
256
    return "mcycle";
567
248
  case 0x0b02:
568
248
    return "minstret";
569
177
  case 0x0b03:
570
177
    return "mhpmcounter3";
571
761
  case 0x0b04:
572
761
    return "mhpmcounter4";
573
358
  case 0x0b05:
574
358
    return "mhpmcounter5";
575
159
  case 0x0b06:
576
159
    return "mhpmcounter6";
577
740
  case 0x0b07:
578
740
    return "mhpmcounter7";
579
154
  case 0x0b08:
580
154
    return "mhpmcounter8";
581
92
  case 0x0b09:
582
92
    return "mhpmcounter9";
583
109
  case 0x0b0a:
584
109
    return "mhpmcounter10";
585
231
  case 0x0b0b:
586
231
    return "mhpmcounter11";
587
301
  case 0x0b0c:
588
301
    return "mhpmcounter12";
589
836
  case 0x0b0d:
590
836
    return "mhpmcounter13";
591
184
  case 0x0b0e:
592
184
    return "mhpmcounter14";
593
125
  case 0x0b0f:
594
125
    return "mhpmcounter15";
595
381
  case 0x0b10:
596
381
    return "mhpmcounter16";
597
292
  case 0x0b11:
598
292
    return "mhpmcounter17";
599
498
  case 0x0b12:
600
498
    return "mhpmcounter18";
601
174
  case 0x0b13:
602
174
    return "mhpmcounter19";
603
118
  case 0x0b14:
604
118
    return "mhpmcounter20";
605
112
  case 0x0b15:
606
112
    return "mhpmcounter21";
607
96
  case 0x0b16:
608
96
    return "mhpmcounter22";
609
405
  case 0x0b17:
610
405
    return "mhpmcounter23";
611
144
  case 0x0b18:
612
144
    return "mhpmcounter24";
613
187
  case 0x0b19:
614
187
    return "mhpmcounter25";
615
184
  case 0x0b1a:
616
184
    return "mhpmcounter26";
617
248
  case 0x0b1b:
618
248
    return "mhpmcounter27";
619
297
  case 0x0b1c:
620
297
    return "mhpmcounter28";
621
365
  case 0x0b1d:
622
365
    return "mhpmcounter29";
623
157
  case 0x0b1e:
624
157
    return "mhpmcounter30";
625
236
  case 0x0b1f:
626
236
    return "mhpmcounter31";
627
875
  case 0x0b80:
628
875
    return "mcycleh";
629
124
  case 0x0b82:
630
124
    return "minstreth";
631
112
  case 0x0b83:
632
112
    return "mhpmcounter3h";
633
222
  case 0x0b84:
634
222
    return "mhpmcounter4h";
635
139
  case 0x0b85:
636
139
    return "mhpmcounter5h";
637
116
  case 0x0b86:
638
116
    return "mhpmcounter6h";
639
476
  case 0x0b87:
640
476
    return "mhpmcounter7h";
641
184
  case 0x0b88:
642
184
    return "mhpmcounter8h";
643
167
  case 0x0b89:
644
167
    return "mhpmcounter9h";
645
354
  case 0x0b8a:
646
354
    return "mhpmcounter10h";
647
1.24k
  case 0x0b8b:
648
1.24k
    return "mhpmcounter11h";
649
58
  case 0x0b8c:
650
58
    return "mhpmcounter12h";
651
110
  case 0x0b8d:
652
110
    return "mhpmcounter13h";
653
415
  case 0x0b8e:
654
415
    return "mhpmcounter14h";
655
341
  case 0x0b8f:
656
341
    return "mhpmcounter15h";
657
298
  case 0x0b90:
658
298
    return "mhpmcounter16h";
659
114
  case 0x0b91:
660
114
    return "mhpmcounter17h";
661
348
  case 0x0b92:
662
348
    return "mhpmcounter18h";
663
455
  case 0x0b93:
664
455
    return "mhpmcounter19h";
665
128
  case 0x0b94:
666
128
    return "mhpmcounter20h";
667
103
  case 0x0b95:
668
103
    return "mhpmcounter21h";
669
239
  case 0x0b96:
670
239
    return "mhpmcounter22h";
671
101
  case 0x0b97:
672
101
    return "mhpmcounter23h";
673
324
  case 0x0b98:
674
324
    return "mhpmcounter24h";
675
617
  case 0x0b99:
676
617
    return "mhpmcounter25h";
677
242
  case 0x0b9a:
678
242
    return "mhpmcounter26h";
679
745
  case 0x0b9b:
680
745
    return "mhpmcounter27h";
681
737
  case 0x0b9c:
682
737
    return "mhpmcounter28h";
683
466
  case 0x0b9d:
684
466
    return "mhpmcounter29h";
685
259
  case 0x0b9e:
686
259
    return "mhpmcounter30h";
687
670
  case 0x0b9f:
688
670
    return "mhpmcounter31h";
689
690
87
  case 0x0323:
691
87
    return "mhpmevent3";
692
183
  case 0x0324:
693
183
    return "mhpmevent4";
694
387
  case 0x0325:
695
387
    return "mhpmevent5";
696
259
  case 0x0326:
697
259
    return "mhpmevent6";
698
148
  case 0x0327:
699
148
    return "mhpmevent7";
700
1.50k
  case 0x0328:
701
1.50k
    return "mhpmevent8";
702
265
  case 0x0329:
703
265
    return "mhpmevent9";
704
452
  case 0x032a:
705
452
    return "mhpmevent10";
706
423
  case 0x032b:
707
423
    return "mhpmevent11";
708
280
  case 0x032c:
709
280
    return "mhpmevent12";
710
284
  case 0x032d:
711
284
    return "mhpmevent13";
712
285
  case 0x032e:
713
285
    return "mhpmevent14";
714
145
  case 0x032f:
715
145
    return "mhpmevent15";
716
289
  case 0x0330:
717
289
    return "mhpmevent16";
718
555
  case 0x0331:
719
555
    return "mhpmevent17";
720
685
  case 0x0332:
721
685
    return "mhpmevent18";
722
193
  case 0x0333:
723
193
    return "mhpmevent19";
724
657
  case 0x0334:
725
657
    return "mhpmevent20";
726
472
  case 0x0335:
727
472
    return "mhpmevent21";
728
69
  case 0x0336:
729
69
    return "mhpmevent22";
730
179
  case 0x0337:
731
179
    return "mhpmevent23";
732
94
  case 0x0338:
733
94
    return "mhpmevent24";
734
564
  case 0x0339:
735
564
    return "mhpmevent25";
736
129
  case 0x033a:
737
129
    return "mhpmevent26";
738
535
  case 0x033b:
739
535
    return "mhpmevent27";
740
226
  case 0x033c:
741
226
    return "mhpmevent28";
742
1.01k
  case 0x033d:
743
1.01k
    return "mhpmevent29";
744
328
  case 0x033e:
745
328
    return "mhpmevent30";
746
399
  case 0x033f:
747
399
    return "mhpmevent31";
748
749
126
  case 0x07a0:
750
126
    return "tselect";
751
128
  case 0x07a1:
752
128
    return "tdata1";
753
280
  case 0x07a2:
754
280
    return "tdata2";
755
70
  case 0x07a3:
756
70
    return "tdata3";
757
758
185
  case 0x07b0:
759
185
    return "dcsr";
760
250
  case 0x07b1:
761
250
    return "dpc";
762
84
  case 0x07b2:
763
84
    return "dscratch";
764
113k
  }
765
21.1k
  return NULL;
766
113k
}
767
768
static void printCSRSystemRegister(MCInst *MI, unsigned OpNo,
769
           //const MCSubtargetInfo &STI,
770
           SStream *O)
771
113k
{
772
113k
  unsigned Imm = MCOperand_getImm(MCInst_getOperand(MI, OpNo));
773
113k
  const char *Name = getCSRSystemRegisterName(Imm);
774
775
113k
  if (Name) {
776
92.0k
    SStream_concat0(O, Name);
777
92.0k
  } else {
778
21.1k
    SStream_concat(O, "%u", Imm);
779
21.1k
  }
780
113k
}
781
782
static void printFenceArg(MCInst *MI, unsigned OpNo, SStream *O)
783
3.43k
{
784
3.43k
  unsigned FenceArg = MCOperand_getImm(MCInst_getOperand(MI, OpNo));
785
  //CS_ASSERT (((FenceArg >> 4) == 0) && "Invalid immediate in printFenceArg");
786
787
3.43k
  if ((FenceArg & RISCVFenceField_I) != 0)
788
1.61k
    SStream_concat0(O, "i");
789
3.43k
  if ((FenceArg & RISCVFenceField_O) != 0)
790
1.40k
    SStream_concat0(O, "o");
791
3.43k
  if ((FenceArg & RISCVFenceField_R) != 0)
792
1.37k
    SStream_concat0(O, "r");
793
3.43k
  if ((FenceArg & RISCVFenceField_W) != 0)
794
1.54k
    SStream_concat0(O, "w");
795
3.43k
  if (FenceArg == 0)
796
948
    SStream_concat0(O, "unknown");
797
3.43k
}
798
799
static void printFRMArg(MCInst *MI, unsigned OpNo, SStream *O)
800
23.6k
{
801
23.6k
  enum RoundingMode FRMArg = (enum RoundingMode)MCOperand_getImm(
802
23.6k
    MCInst_getOperand(MI, OpNo));
803
#if 0
804
  auto FRMArg =
805
      static_cast<RISCVFPRndMode::RoundingMode>(MI->getOperand(OpNo).getImm());
806
  O << RISCVFPRndMode::roundingModeToString(FRMArg);
807
#endif
808
23.6k
  SStream_concat0(O, roundingModeToString(FRMArg));
809
23.6k
}
810
811
#endif // CAPSTONE_HAS_RISCV