Coverage Report

Created: 2025-11-09 07:00

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