Coverage Report

Created: 2025-07-04 06:11

/src/capstonenext/arch/RISCV/RISCVInstPrinter.c
Line
Count
Source (jump to first uncovered line)
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
56
static void fixDetailOfEffectiveAddr(MCInst *MI)
57
8.35k
{
58
  // Operands for load and store instructions in RISCV vary widely
59
8.35k
  unsigned id = MI->flat_insn->id;
60
8.35k
  unsigned reg = 0;
61
8.35k
  int64_t imm = 0;
62
8.35k
    uint8_t access = 0;
63
  
64
8.35k
  switch (id) {
65
0
    case RISCV_INS_C_FLD:
66
0
    case RISCV_INS_C_LW:
67
0
    case RISCV_INS_C_FLW:
68
0
    case RISCV_INS_C_LD:
69
0
    case RISCV_INS_C_FSD:
70
0
    case RISCV_INS_C_SW:
71
0
    case RISCV_INS_C_FSW:
72
0
    case RISCV_INS_C_SD:
73
0
    case RISCV_INS_C_FLDSP:
74
0
    case RISCV_INS_C_LWSP:
75
0
    case RISCV_INS_C_FLWSP:
76
0
    case RISCV_INS_C_LDSP:
77
0
    case RISCV_INS_C_FSDSP:
78
0
    case RISCV_INS_C_SWSP:
79
0
    case RISCV_INS_C_FSWSP:
80
0
    case RISCV_INS_C_SDSP:
81
92
    case RISCV_INS_FLW:
82
381
    case RISCV_INS_FSW:
83
452
    case RISCV_INS_FLD:
84
470
    case RISCV_INS_FSD:
85
933
    case RISCV_INS_LB:
86
1.07k
    case RISCV_INS_LBU:
87
1.12k
    case RISCV_INS_LD:
88
1.60k
    case RISCV_INS_LH:
89
1.69k
    case RISCV_INS_LHU:
90
1.78k
    case RISCV_INS_LW:
91
1.83k
    case RISCV_INS_LWU:
92
1.86k
    case RISCV_INS_SB:
93
2.31k
    case RISCV_INS_SD:
94
2.44k
    case RISCV_INS_SH:
95
2.92k
    case RISCV_INS_SW: {
96
2.92k
      CS_ASSERT(3 == MI->flat_insn->detail->riscv.op_count);
97
2.92k
      CS_ASSERT(RISCV_OP_REG == RISCV_get_detail_op(MI, -3)->type);
98
2.92k
      CS_ASSERT(RISCV_OP_IMM == RISCV_get_detail_op(MI, -2)->type);
99
2.92k
      CS_ASSERT(RISCV_OP_REG == RISCV_get_detail_op(MI, -1)->type);
100
101
2.92k
      imm = RISCV_get_detail_op(MI, -2)->imm;
102
2.92k
      reg = RISCV_get_detail_op(MI, -1)->reg;
103
2.92k
      access = RISCV_get_detail_op(MI, -1)->access;
104
105
2.92k
      RISCV_get_detail_op(MI, -2)->type = RISCV_OP_MEM;
106
2.92k
      RISCV_get_detail_op(MI, -2)->mem.base = reg;
107
2.92k
      RISCV_get_detail_op(MI, -2)->mem.disp = imm;
108
2.92k
      RISCV_get_detail_op(MI, -2)->access = access;
109
110
2.92k
      RISCV_dec_op_count(MI);
111
112
2.92k
      break;
113
2.44k
    }
114
41
    case RISCV_INS_LR_W:
115
53
    case RISCV_INS_LR_W_AQ:
116
77
    case RISCV_INS_LR_W_AQ_RL:
117
83
    case RISCV_INS_LR_W_RL:
118
137
    case RISCV_INS_LR_D:
119
147
    case RISCV_INS_LR_D_AQ:
120
329
    case RISCV_INS_LR_D_AQ_RL:
121
347
    case RISCV_INS_LR_D_RL: {
122
347
      CS_ASSERT(2 == MI->flat_insn->detail->riscv.op_count);
123
347
      CS_ASSERT(RISCV_OP_REG == RISCV_get_detail_op(MI, -1)->type);
124
347
      CS_ASSERT(RISCV_OP_REG == RISCV_get_detail_op(MI, -2)->type);
125
126
347
      reg = RISCV_get_detail_op(MI, -1)->reg;
127
128
347
      RISCV_get_detail_op(MI, -1)->type = RISCV_OP_MEM;
129
347
      RISCV_get_detail_op(MI, -1)->mem.base = reg;
130
347
      RISCV_get_detail_op(MI, -1)->mem.disp = 0;
131
132
347
      break;
133
329
    }
134
206
    case RISCV_INS_SC_W:
135
224
    case RISCV_INS_SC_W_AQ:
136
236
    case RISCV_INS_SC_W_AQ_RL:
137
247
    case RISCV_INS_SC_W_RL:
138
314
    case RISCV_INS_SC_D:
139
333
    case RISCV_INS_SC_D_AQ:
140
379
    case RISCV_INS_SC_D_AQ_RL:
141
445
    case RISCV_INS_SC_D_RL:
142
461
    case RISCV_INS_AMOADD_D:
143
476
    case RISCV_INS_AMOADD_D_AQ:
144
803
    case RISCV_INS_AMOADD_D_AQ_RL:
145
894
    case RISCV_INS_AMOADD_D_RL:
146
941
    case RISCV_INS_AMOADD_W:
147
951
    case RISCV_INS_AMOADD_W_AQ:
148
1.00k
    case RISCV_INS_AMOADD_W_AQ_RL:
149
1.24k
    case RISCV_INS_AMOADD_W_RL:
150
1.42k
    case RISCV_INS_AMOAND_D:
151
1.46k
    case RISCV_INS_AMOAND_D_AQ:
152
1.49k
    case RISCV_INS_AMOAND_D_AQ_RL:
153
1.50k
    case RISCV_INS_AMOAND_D_RL:
154
1.51k
    case RISCV_INS_AMOAND_W:
155
1.54k
    case RISCV_INS_AMOAND_W_AQ:
156
1.58k
    case RISCV_INS_AMOAND_W_AQ_RL:
157
1.60k
    case RISCV_INS_AMOAND_W_RL:
158
1.62k
    case RISCV_INS_AMOMAXU_D:
159
1.66k
    case RISCV_INS_AMOMAXU_D_AQ:
160
1.67k
    case RISCV_INS_AMOMAXU_D_AQ_RL:
161
1.68k
    case RISCV_INS_AMOMAXU_D_RL:
162
1.69k
    case RISCV_INS_AMOMAXU_W:
163
1.70k
    case RISCV_INS_AMOMAXU_W_AQ:
164
1.77k
    case RISCV_INS_AMOMAXU_W_AQ_RL:
165
1.78k
    case RISCV_INS_AMOMAXU_W_RL:
166
1.79k
    case RISCV_INS_AMOMAX_D:
167
1.82k
    case RISCV_INS_AMOMAX_D_AQ:
168
2.14k
    case RISCV_INS_AMOMAX_D_AQ_RL:
169
2.16k
    case RISCV_INS_AMOMAX_D_RL:
170
2.19k
    case RISCV_INS_AMOMAX_W:
171
2.24k
    case RISCV_INS_AMOMAX_W_AQ:
172
2.25k
    case RISCV_INS_AMOMAX_W_AQ_RL:
173
2.29k
    case RISCV_INS_AMOMAX_W_RL:
174
2.44k
    case RISCV_INS_AMOMINU_D:
175
2.47k
    case RISCV_INS_AMOMINU_D_AQ:
176
2.55k
    case RISCV_INS_AMOMINU_D_AQ_RL:
177
2.75k
    case RISCV_INS_AMOMINU_D_RL:
178
2.92k
    case RISCV_INS_AMOMINU_W:
179
2.96k
    case RISCV_INS_AMOMINU_W_AQ:
180
3.04k
    case RISCV_INS_AMOMINU_W_AQ_RL:
181
3.07k
    case RISCV_INS_AMOMINU_W_RL:
182
3.19k
    case RISCV_INS_AMOMIN_D:
183
3.22k
    case RISCV_INS_AMOMIN_D_AQ:
184
3.23k
    case RISCV_INS_AMOMIN_D_AQ_RL:
185
3.24k
    case RISCV_INS_AMOMIN_D_RL:
186
3.28k
    case RISCV_INS_AMOMIN_W:
187
3.35k
    case RISCV_INS_AMOMIN_W_AQ:
188
3.38k
    case RISCV_INS_AMOMIN_W_AQ_RL:
189
3.42k
    case RISCV_INS_AMOMIN_W_RL:
190
3.55k
    case RISCV_INS_AMOOR_D:
191
3.69k
    case RISCV_INS_AMOOR_D_AQ:
192
3.96k
    case RISCV_INS_AMOOR_D_AQ_RL:
193
3.99k
    case RISCV_INS_AMOOR_D_RL:
194
4.00k
    case RISCV_INS_AMOOR_W:
195
4.07k
    case RISCV_INS_AMOOR_W_AQ:
196
4.42k
    case RISCV_INS_AMOOR_W_AQ_RL:
197
4.42k
    case RISCV_INS_AMOOR_W_RL:
198
4.43k
    case RISCV_INS_AMOSWAP_D:
199
4.45k
    case RISCV_INS_AMOSWAP_D_AQ:
200
4.52k
    case RISCV_INS_AMOSWAP_D_AQ_RL:
201
4.60k
    case RISCV_INS_AMOSWAP_D_RL:
202
4.60k
    case RISCV_INS_AMOSWAP_W:
203
4.61k
    case RISCV_INS_AMOSWAP_W_AQ:
204
4.65k
    case RISCV_INS_AMOSWAP_W_AQ_RL:
205
4.66k
    case RISCV_INS_AMOSWAP_W_RL:
206
4.69k
    case RISCV_INS_AMOXOR_D:
207
4.71k
    case RISCV_INS_AMOXOR_D_AQ:
208
4.75k
    case RISCV_INS_AMOXOR_D_AQ_RL:
209
4.76k
    case RISCV_INS_AMOXOR_D_RL:
210
4.78k
    case RISCV_INS_AMOXOR_W:
211
4.98k
    case RISCV_INS_AMOXOR_W_AQ:
212
5.00k
    case RISCV_INS_AMOXOR_W_AQ_RL:
213
5.07k
    case RISCV_INS_AMOXOR_W_RL: {
214
5.07k
      CS_ASSERT(3 == MI->flat_insn->detail->riscv.op_count);
215
5.07k
      CS_ASSERT(RISCV_OP_REG == RISCV_get_detail_op(MI, -3)->type);
216
5.07k
      CS_ASSERT(RISCV_OP_REG == RISCV_get_detail_op(MI, -2)->type);
217
5.07k
      CS_ASSERT(RISCV_OP_REG == RISCV_get_detail_op(MI, -1)->type);
218
219
5.07k
      reg = RISCV_get_detail_op(MI, -1)->reg;
220
221
5.07k
      RISCV_get_detail_op(MI, -1)->type = RISCV_OP_MEM;
222
5.07k
      RISCV_get_detail_op(MI, -1)->mem.base = reg;
223
5.07k
      RISCV_get_detail_op(MI, -1)->mem.disp = 0;
224
225
5.07k
      break;
226
5.00k
    }
227
0
    default: {
228
0
      CS_ASSERT(0 && "id is not a RISC-V memory instruction");
229
0
      break;
230
5.00k
    }
231
8.35k
  }
232
8.35k
  return;
233
8.35k
}
234
235
236
//void RISCVInstPrinter::printInst(const MCInst *MI, raw_ostream &O,
237
//                                 StringRef Annot, const MCSubtargetInfo &STI) 
238
void RISCV_printInst(MCInst *MI, SStream *O, void *info) 
239
141k
{
240
141k
    MCRegisterInfo *MRI = (MCRegisterInfo *) info;
241
    //bool Res = false;
242
    //MCInst *NewMI = MI;
243
    // TODO: RISCV compressd instructions.
244
    //MCInst UncompressedMI;
245
    //if (!NoAliases)
246
      //Res = uncompressInst(UncompressedMI, *MI, MRI, STI);
247
    //if (Res)
248
      //NewMI = const_cast<MCInst *>(&UncompressedMI);
249
141k
    if (/*NoAliases ||*/ !printAliasInstr(MI, O, info))
250
106k
        printInstruction(MI, O, MRI);
251
      //printAnnotation(O, Annot);
252
  // fix load/store type insttuction
253
141k
      if (MI->csh->detail_opt && 
254
141k
      MI->flat_insn->detail->riscv.need_effective_addr)
255
9.80k
    fixDetailOfEffectiveAddr(MI);
256
  
257
141k
  return;
258
141k
}
259
260
static void printRegName(SStream *OS, unsigned RegNo) 
261
241k
{
262
241k
    SStream_concat0(OS, getRegisterName(RegNo, RISCV_ABIRegAltName));
263
241k
}
264
265
/**
266
void RISCVInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
267
                                    raw_ostream &O, const char *Modifier) 
268
*/
269
static void printOperand(MCInst *MI, unsigned OpNo, SStream *O) 
270
118k
{
271
118k
    unsigned reg;
272
118k
    int64_t Imm = 0;
273
274
118k
    RISCV_add_cs_detail(MI, OpNo);
275
276
118k
    MCOperand *MO = MCInst_getOperand(MI, OpNo);
277
  
278
118k
    if (MCOperand_isReg(MO)) {
279
100k
        reg = MCOperand_getReg(MO);
280
100k
        printRegName(O, reg);
281
100k
    } else {
282
18.3k
    CS_ASSERT(MCOperand_isImm(MO) && "Unknown operand kind in printOperand");
283
18.3k
        Imm = MCOperand_getImm(MO);
284
18.3k
        if (Imm >= 0) {
285
16.4k
            if (Imm > HEX_THRESHOLD)
286
9.72k
              SStream_concat(O, "0x%" PRIx64, Imm);
287
6.72k
            else
288
6.72k
        SStream_concat(O, "%" PRIu64, Imm);
289
16.4k
        } else {
290
1.88k
            if (Imm < -HEX_THRESHOLD)
291
1.83k
        SStream_concat(O, "-0x%" PRIx64, -Imm);
292
48
            else
293
48
        SStream_concat(O, "-%" PRIu64, -Imm);
294
1.88k
        }
295
18.3k
      }
296
297
    //CS_ASSERT(MO.isExpr() && "Unknown operand kind in printOperand");
298
  
299
118k
  return;
300
118k
}
301
302
static const char *getCSRSystemRegisterName(unsigned CsrNo)
303
81.6k
{
304
81.6k
  switch (CsrNo) {
305
  /*
306
   * From RISC-V Privileged Architecture Version 1.10.
307
   * In the same order as Table 2.5.
308
   */
309
330
  case 0x0000: return "ustatus";
310
493
  case 0x0004: return "uie";
311
249
  case 0x0005: return "utvec";
312
313
73
  case 0x0040: return "uscratch";
314
130
  case 0x0041: return "uepc";
315
441
  case 0x0042: return "ucause";
316
180
  case 0x0043: return "utval";
317
194
  case 0x0044: return "uip";
318
319
441
  case 0x0001: return "fflags";
320
1.26k
  case 0x0002: return "frm";
321
1.25k
  case 0x0003: return "fcsr";
322
323
1.50k
  case 0x0c00: return "cycle";
324
1.98k
  case 0x0c01: return "time";
325
350
  case 0x0c02: return "instret";
326
334
  case 0x0c03: return "hpmcounter3";
327
612
  case 0x0c04: return "hpmcounter4";
328
747
  case 0x0c05: return "hpmcounter5";
329
343
  case 0x0c06: return "hpmcounter6";
330
298
  case 0x0c07: return "hpmcounter7";
331
240
  case 0x0c08: return "hpmcounter8";
332
553
  case 0x0c09: return "hpmcounter9";
333
326
  case 0x0c0a: return "hpmcounter10";
334
360
  case 0x0c0b: return "hpmcounter11";
335
74
  case 0x0c0c: return "hpmcounter12";
336
349
  case 0x0c0d: return "hpmcounter13";
337
220
  case 0x0c0e: return "hpmcounter14";
338
142
  case 0x0c0f: return "hpmcounter15";
339
171
  case 0x0c10: return "hpmcounter16";
340
160
  case 0x0c11: return "hpmcounter17";
341
164
  case 0x0c12: return "hpmcounter18";
342
128
  case 0x0c13: return "hpmcounter19";
343
402
  case 0x0c14: return "hpmcounter20";
344
250
  case 0x0c15: return "hpmcounter21";
345
271
  case 0x0c16: return "hpmcounter22";
346
194
  case 0x0c17: return "hpmcounter23";
347
371
  case 0x0c18: return "hpmcounter24";
348
135
  case 0x0c19: return "hpmcounter25";
349
313
  case 0x0c1a: return "hpmcounter26";
350
363
  case 0x0c1b: return "hpmcounter27";
351
101
  case 0x0c1c: return "hpmcounter28";
352
357
  case 0x0c1d: return "hpmcounter29";
353
392
  case 0x0c1e: return "hpmcounter30";
354
281
  case 0x0c1f: return "hpmcounter31";
355
222
  case 0x0c80: return "cycleh";
356
238
  case 0x0c81: return "timeh";
357
1.17k
  case 0x0c82: return "instreth";
358
255
  case 0x0c83: return "hpmcounter3h";
359
140
  case 0x0c84: return "hpmcounter4h";
360
127
  case 0x0c85: return "hpmcounter5h";
361
505
  case 0x0c86: return "hpmcounter6h";
362
338
  case 0x0c87: return "hpmcounter7h";
363
250
  case 0x0c88: return "hpmcounter8h";
364
42
  case 0x0c89: return "hpmcounter9h";
365
164
  case 0x0c8a: return "hpmcounter10h";
366
436
  case 0x0c8b: return "hpmcounter11h";
367
506
  case 0x0c8c: return "hpmcounter12h";
368
339
  case 0x0c8d: return "hpmcounter13h";
369
164
  case 0x0c8e: return "hpmcounter14h";
370
71
  case 0x0c8f: return "hpmcounter15h";
371
367
  case 0x0c90: return "hpmcounter16h";
372
282
  case 0x0c91: return "hpmcounter17h";
373
188
  case 0x0c92: return "hpmcounter18h";
374
165
  case 0x0c93: return "hpmcounter19h";
375
112
  case 0x0c94: return "hpmcounter20h";
376
130
  case 0x0c95: return "hpmcounter21h";
377
336
  case 0x0c96: return "hpmcounter22h";
378
85
  case 0x0c97: return "hpmcounter23h";
379
60
  case 0x0c98: return "hpmcounter24h";
380
264
  case 0x0c99: return "hpmcounter25h";
381
439
  case 0x0c9a: return "hpmcounter26h";
382
623
  case 0x0c9b: return "hpmcounter27h";
383
1.61k
  case 0x0c9c: return "hpmcounter28h";
384
824
  case 0x0c9d: return "hpmcounter29h";
385
65
  case 0x0c9e: return "hpmcounter30h";
386
586
  case 0x0c9f: return "hpmcounter31h";
387
388
178
  case 0x0100: return "sstatus";
389
240
  case 0x0102: return "sedeleg";
390
289
  case 0x0103: return "sideleg";
391
232
  case 0x0104: return "sie";
392
53
  case 0x0105: return "stvec";
393
117
  case 0x0106: return "scounteren";
394
395
220
  case 0x0140: return "sscratch";
396
96
  case 0x0141: return "sepc";
397
93
  case 0x0142: return "scause";
398
118
  case 0x0143: return "stval";
399
57
  case 0x0144: return "sip";
400
401
75
  case 0x0180: return "satp";
402
403
923
  case 0x0f11: return "mvendorid";
404
922
  case 0x0f12: return "marchid";
405
217
  case 0x0f13: return "mimpid";
406
98
  case 0x0f14: return "mhartid";
407
408
110
  case 0x0300: return "mstatus";
409
62
  case 0x0301: return "misa";
410
485
  case 0x0302: return "medeleg";
411
83
  case 0x0303: return "mideleg";
412
265
  case 0x0304: return "mie";
413
331
  case 0x0305: return "mtvec";
414
83
  case 0x0306: return "mcounteren";
415
416
190
  case 0x0340: return "mscratch";
417
1.09k
  case 0x0341: return "mepc";
418
144
  case 0x0342: return "mcause";
419
235
  case 0x0343: return "mtval";
420
271
  case 0x0344: return "mip";
421
422
81
  case 0x03a0: return "pmpcfg0";
423
743
  case 0x03a1: return "pmpcfg1";
424
567
  case 0x03a2: return "pmpcfg2";
425
92
  case 0x03a3: return "pmpcfg3";
426
153
  case 0x03b0: return "pmpaddr0";
427
76
  case 0x03b1: return "pmpaddr1";
428
349
  case 0x03b2: return "pmpaddr2";
429
79
  case 0x03b3: return "pmpaddr3";
430
77
  case 0x03b4: return "pmpaddr4";
431
86
  case 0x03b5: return "pmpaddr5";
432
149
  case 0x03b6: return "pmpaddr6";
433
988
  case 0x03b7: return "pmpaddr7";
434
91
  case 0x03b8: return "pmpaddr8";
435
407
  case 0x03b9: return "pmpaddr9";
436
82
  case 0x03ba: return "pmpaddr10";
437
215
  case 0x03bb: return "pmpaddr11";
438
111
  case 0x03bc: return "pmpaddr12";
439
180
  case 0x03bd: return "pmpaddr13";
440
512
  case 0x03be: return "pmpaddr14";
441
90
  case 0x03bf: return "pmpaddr15";
442
443
723
  case 0x0b00: return "mcycle";
444
529
  case 0x0b02: return "minstret";
445
562
  case 0x0b03: return "mhpmcounter3";
446
236
  case 0x0b04: return "mhpmcounter4";
447
62
  case 0x0b05: return "mhpmcounter5";
448
78
  case 0x0b06: return "mhpmcounter6";
449
192
  case 0x0b07: return "mhpmcounter7";
450
355
  case 0x0b08: return "mhpmcounter8";
451
74
  case 0x0b09: return "mhpmcounter9";
452
833
  case 0x0b0a: return "mhpmcounter10";
453
334
  case 0x0b0b: return "mhpmcounter11";
454
308
  case 0x0b0c: return "mhpmcounter12";
455
443
  case 0x0b0d: return "mhpmcounter13";
456
172
  case 0x0b0e: return "mhpmcounter14";
457
680
  case 0x0b0f: return "mhpmcounter15";
458
274
  case 0x0b10: return "mhpmcounter16";
459
237
  case 0x0b11: return "mhpmcounter17";
460
434
  case 0x0b12: return "mhpmcounter18";
461
174
  case 0x0b13: return "mhpmcounter19";
462
138
  case 0x0b14: return "mhpmcounter20";
463
108
  case 0x0b15: return "mhpmcounter21";
464
31
  case 0x0b16: return "mhpmcounter22";
465
66
  case 0x0b17: return "mhpmcounter23";
466
25
  case 0x0b18: return "mhpmcounter24";
467
185
  case 0x0b19: return "mhpmcounter25";
468
163
  case 0x0b1a: return "mhpmcounter26";
469
390
  case 0x0b1b: return "mhpmcounter27";
470
282
  case 0x0b1c: return "mhpmcounter28";
471
354
  case 0x0b1d: return "mhpmcounter29";
472
83
  case 0x0b1e: return "mhpmcounter30";
473
177
  case 0x0b1f: return "mhpmcounter31";
474
483
  case 0x0b80: return "mcycleh";
475
258
  case 0x0b82: return "minstreth";
476
172
  case 0x0b83: return "mhpmcounter3h";
477
127
  case 0x0b84: return "mhpmcounter4h";
478
149
  case 0x0b85: return "mhpmcounter5h";
479
255
  case 0x0b86: return "mhpmcounter6h";
480
186
  case 0x0b87: return "mhpmcounter7h";
481
29
  case 0x0b88: return "mhpmcounter8h";
482
118
  case 0x0b89: return "mhpmcounter9h";
483
291
  case 0x0b8a: return "mhpmcounter10h";
484
443
  case 0x0b8b: return "mhpmcounter11h";
485
29
  case 0x0b8c: return "mhpmcounter12h";
486
45
  case 0x0b8d: return "mhpmcounter13h";
487
611
  case 0x0b8e: return "mhpmcounter14h";
488
608
  case 0x0b8f: return "mhpmcounter15h";
489
557
  case 0x0b90: return "mhpmcounter16h";
490
309
  case 0x0b91: return "mhpmcounter17h";
491
306
  case 0x0b92: return "mhpmcounter18h";
492
81
  case 0x0b93: return "mhpmcounter19h";
493
62
  case 0x0b94: return "mhpmcounter20h";
494
130
  case 0x0b95: return "mhpmcounter21h";
495
121
  case 0x0b96: return "mhpmcounter22h";
496
120
  case 0x0b97: return "mhpmcounter23h";
497
183
  case 0x0b98: return "mhpmcounter24h";
498
192
  case 0x0b99: return "mhpmcounter25h";
499
144
  case 0x0b9a: return "mhpmcounter26h";
500
213
  case 0x0b9b: return "mhpmcounter27h";
501
226
  case 0x0b9c: return "mhpmcounter28h";
502
400
  case 0x0b9d: return "mhpmcounter29h";
503
286
  case 0x0b9e: return "mhpmcounter30h";
504
50
  case 0x0b9f: return "mhpmcounter31h";
505
506
80
  case 0x0323: return "mhpmevent3";
507
147
  case 0x0324: return "mhpmevent4";
508
479
  case 0x0325: return "mhpmevent5";
509
64
  case 0x0326: return "mhpmevent6";
510
187
  case 0x0327: return "mhpmevent7";
511
202
  case 0x0328: return "mhpmevent8";
512
309
  case 0x0329: return "mhpmevent9";
513
123
  case 0x032a: return "mhpmevent10";
514
242
  case 0x032b: return "mhpmevent11";
515
59
  case 0x032c: return "mhpmevent12";
516
152
  case 0x032d: return "mhpmevent13";
517
156
  case 0x032e: return "mhpmevent14";
518
542
  case 0x032f: return "mhpmevent15";
519
314
  case 0x0330: return "mhpmevent16";
520
563
  case 0x0331: return "mhpmevent17";
521
994
  case 0x0332: return "mhpmevent18";
522
96
  case 0x0333: return "mhpmevent19";
523
331
  case 0x0334: return "mhpmevent20";
524
260
  case 0x0335: return "mhpmevent21";
525
338
  case 0x0336: return "mhpmevent22";
526
196
  case 0x0337: return "mhpmevent23";
527
342
  case 0x0338: return "mhpmevent24";
528
888
  case 0x0339: return "mhpmevent25";
529
351
  case 0x033a: return "mhpmevent26";
530
311
  case 0x033b: return "mhpmevent27";
531
286
  case 0x033c: return "mhpmevent28";
532
419
  case 0x033d: return "mhpmevent29";
533
622
  case 0x033e: return "mhpmevent30";
534
372
  case 0x033f: return "mhpmevent31";
535
536
12
  case 0x07a0: return "tselect";
537
225
  case 0x07a1: return "tdata1";
538
111
  case 0x07a2: return "tdata2";
539
29
  case 0x07a3: return "tdata3";
540
541
152
  case 0x07b0: return "dcsr";
542
70
  case 0x07b1: return "dpc";
543
77
  case 0x07b2: return "dscratch";
544
81.6k
  }
545
14.6k
  return NULL;
546
81.6k
}
547
548
static void printCSRSystemRegister(MCInst *MI, unsigned OpNo,
549
                                   //const MCSubtargetInfo &STI,
550
                                   SStream *O) 
551
81.6k
{
552
81.6k
  unsigned Imm = MCOperand_getImm(MCInst_getOperand(MI, OpNo));
553
81.6k
  const char *Name = getCSRSystemRegisterName(Imm);
554
555
81.6k
  if (Name) {
556
67.0k
    SStream_concat0(O, Name);
557
67.0k
  } else {
558
14.6k
    SStream_concat(O, "%u", Imm);
559
14.6k
  }
560
81.6k
}
561
562
static void printFenceArg(MCInst *MI, unsigned OpNo, SStream *O) 
563
4.10k
{
564
4.10k
    unsigned FenceArg = MCOperand_getImm(MCInst_getOperand(MI, OpNo));
565
    //CS_ASSERT (((FenceArg >> 4) == 0) && "Invalid immediate in printFenceArg");
566
567
4.10k
    if ((FenceArg & RISCVFenceField_I) != 0)
568
1.89k
        SStream_concat0(O, "i");
569
4.10k
    if ((FenceArg & RISCVFenceField_O) != 0)
570
1.69k
        SStream_concat0(O, "o");
571
4.10k
  if ((FenceArg & RISCVFenceField_R) != 0)
572
1.64k
        SStream_concat0(O, "r");
573
4.10k
    if ((FenceArg & RISCVFenceField_W) != 0)
574
1.78k
        SStream_concat0(O, "w");
575
4.10k
    if (FenceArg == 0)
576
1.24k
        SStream_concat0(O, "unknown");
577
4.10k
}
578
579
static void printFRMArg(MCInst *MI, unsigned OpNo, SStream *O) 
580
14.6k
{
581
14.6k
    enum RoundingMode FRMArg = 
582
14.6k
        (enum RoundingMode)MCOperand_getImm(MCInst_getOperand(MI, OpNo));
583
#if 0
584
  auto FRMArg =
585
      static_cast<RISCVFPRndMode::RoundingMode>(MI->getOperand(OpNo).getImm());
586
  O << RISCVFPRndMode::roundingModeToString(FRMArg);
587
#endif
588
14.6k
    SStream_concat0(O, roundingModeToString(FRMArg));
589
14.6k
}
590
  
591
#endif        // CAPSTONE_HAS_RISCV