Coverage Report

Created: 2025-12-05 06:11

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