Coverage Report

Created: 2025-07-01 07:03

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