Coverage Report

Created: 2025-08-29 06:29

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