Coverage Report

Created: 2025-10-28 07:02

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