Coverage Report

Created: 2026-03-13 06:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/capstonenext/arch/RISCV/RISCVInstPrinter.c
Line
Count
Source
1
/* Capstone Disassembly Engine, http://www.capstone-engine.org */
2
/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2022, */
3
/*    Rot127 <unisono@quyllur.org> 2022-2023 */
4
/* Automatically translated source file from LLVM. */
5
6
/* LLVM-commit: <commit> */
7
/* LLVM-tag: <tag> */
8
9
/* Only small edits allowed. */
10
/* For multiple similar edits, please create a Patch for the translator. */
11
12
/* Capstone's C++ file translator: */
13
/* https://github.com/capstone-engine/capstone/tree/next/suite/auto-sync */
14
15
//===-- RISCVInstPrinter.cpp - Convert RISC-V MCInst to asm syntax --------===//
16
//
17
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
18
// See https://llvm.org/LICENSE.txt for license information.
19
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
20
//
21
//===----------------------------------------------------------------------===//
22
//
23
// This class prints an RISC-V MCInst to a .s file.
24
//
25
//===----------------------------------------------------------------------===//
26
27
#include <capstone/platform.h>
28
#include "../../MathExtras.h"
29
30
#include "RISCVMapping.h"
31
#include "RISCVInstPrinter.h"
32
33
#define GET_SUBTARGETINFO_ENUM
34
#include "RISCVGenSubtargetInfo.inc"
35
36
#define GET_INSTRINFO_ENUM
37
#include "RISCVGenInstrInfo.inc"
38
39
#define GET_REGINFO_ENUM
40
#include "RISCVGenRegisterInfo.inc"
41
42
#define GET_SysRegsList_IMPL
43
#include "RISCVGenSystemOperands.inc"
44
45
#define GEN_UNCOMPRESS_INSTR
46
#include "RISCVGenCompressedInstructionsInfo.inc"
47
48
#include "RISCVMapping.h"
49
#include "../../Mapping.h"
50
51
#define CONCAT(a, b) CONCAT_(a, b)
52
#define CONCAT_(a, b) a##_##b
53
54
#define DEBUG_TYPE "asm-printer"
55
56
static void printCustomAliasOperand(MCInst *MI, uint64_t Address,
57
            unsigned OpIdx, unsigned PrintMethodIdx,
58
            SStream *OS);
59
static inline void printRegName(SStream *O, MCRegister Reg);
60
static inline void printOperand(MCInst *MI, unsigned OpNo, SStream *O);
61
// Include the auto-generated portion of the assembly writer.
62
#define PRINT_ALIAS_INSTR
63
#include "RISCVGenAsmWriter.inc"
64
65
// Print architectural register names rather than the ABI names (such as x2
66
// instead of sp).
67
// TODO: Make RISCVInstPrinter_doGetRegisterName non-static so that this can a
68
// member.
69
static bool ArchRegNames;
70
71
const char *doGetRegisterName(MCRegister Reg)
72
105k
{
73
105k
  return getRegisterName(Reg, ArchRegNames ? RISCV_NoRegAltName :
74
105k
               RISCV_ABIRegAltName);
75
105k
}
76
77
static inline void printRegName(SStream *O, MCRegister Reg)
78
226k
{
79
226k
  SStream_concat0(markup_OS(O, Markup_Register), doGetRegisterName(Reg));
80
226k
}
81
82
bool haveRequiredFeatures(const RISCV_SysReg *Reg, MCInst *MI)
83
653
{
84
  // Not in 32-bit mode.
85
653
  if (Reg->isRV32Only &&
86
531
      RISCV_getFeatureBits(MI->csh->mode, RISCV_Feature64Bit))
87
410
    return false;
88
89
243
  return true;
90
653
}
91
92
static inline void printOperand(MCInst *MI, unsigned OpNo, SStream *O)
93
139k
{
94
139k
  RISCV_add_cs_detail_0(MI, RISCV_OP_GROUP_Operand, OpNo);
95
96
139k
  MCOperand *MO = MCInst_getOperand(MI, (OpNo));
97
98
139k
  if (MCOperand_isReg(MO)) {
99
101k
    printRegName(O, MCOperand_getReg(MO));
100
101k
    return;
101
101k
  }
102
103
38.1k
  if (MCOperand_isImm(MO)) {
104
38.1k
    printInt64(markup_OS(O, Markup_Immediate),
105
38.1k
         MCOperand_getImm(MO));
106
38.1k
    return;
107
38.1k
  }
108
109
0
  CS_ASSERT(MCOperand_isExpr(MO) &&
110
0
      "Unknown operand kind in printOperand");
111
0
  printExpr(O, MCOperand_getExpr(MO));
112
0
}
113
114
void printBranchOperand(MCInst *MI, uint64_t Address, unsigned OpNo, SStream *O)
115
5.61k
{
116
5.61k
  RISCV_add_cs_detail_0(MI, RISCV_OP_GROUP_BranchOperand, OpNo);
117
5.61k
  MCOperand *MO = MCInst_getOperand(MI, (OpNo));
118
5.61k
  if (!MCOperand_isImm(MO))
119
0
    return printOperand(MI, OpNo, O);
120
121
5.61k
  if (MI->csh->PrintBranchImmAsAddress) {
122
5.61k
    uint64_t Target = Address + MCOperand_getImm(MO);
123
5.61k
    if (!RISCV_getFeatureBits(MI->csh->mode, RISCV_Feature64Bit))
124
3.55k
      Target &= 0xffffffff;
125
5.61k
    printUInt64(markup_OS(O, Markup_Target), Target);
126
5.61k
  } else {
127
0
    printInt64(markup_OS(O, Markup_Target), MCOperand_getImm(MO));
128
0
  }
129
5.61k
}
130
131
void printCSRSystemRegister(MCInst *MI, unsigned OpNo, SStream *O)
132
1.36k
{
133
1.36k
  RISCV_add_cs_detail_0(MI, RISCV_OP_GROUP_CSRSystemRegister, OpNo);
134
1.36k
  unsigned Imm = MCOperand_getImm(MCInst_getOperand(MI, (OpNo)));
135
1.36k
  const RISCV_SysReg *SysReg = RISCV_lookupSysRegByEncoding(Imm);
136
1.36k
  if (SysReg && haveRequiredFeatures(SysReg, MI))
137
243
    SStream_concat0(markup_OS(O, Markup_Register), SysReg->Name);
138
1.12k
  else
139
1.12k
    printUInt64(markup_OS(O, Markup_Register), Imm);
140
1.36k
}
141
142
void printFenceArg(MCInst *MI, unsigned OpNo, SStream *O)
143
1.48k
{
144
1.48k
  RISCV_add_cs_detail_0(MI, RISCV_OP_GROUP_FenceArg, OpNo);
145
1.48k
  unsigned FenceArg = MCOperand_getImm(MCInst_getOperand(MI, (OpNo)));
146
1.48k
  CS_ASSERT(((FenceArg >> 4) == 0) &&
147
1.48k
      "Invalid immediate in printFenceArg");
148
149
1.48k
  if ((FenceArg & RISCVFenceField_I) != 0)
150
750
    SStream_concat0(O, "i");
151
152
1.48k
  if ((FenceArg & RISCVFenceField_O) != 0)
153
395
    SStream_concat0(O, "o");
154
155
1.48k
  if ((FenceArg & RISCVFenceField_R) != 0)
156
761
    SStream_concat0(O, "r");
157
158
1.48k
  if ((FenceArg & RISCVFenceField_W) != 0)
159
706
    SStream_concat0(O, "w");
160
161
1.48k
  if (FenceArg == 0)
162
367
    SStream_concat0(O, "0");
163
1.48k
}
164
165
void printFRMArg(MCInst *MI, unsigned OpNo, SStream *O)
166
1.54k
{
167
1.54k
  RISCV_add_cs_detail_0(MI, RISCV_OP_GROUP_FRMArg, OpNo);
168
1.54k
  unsigned FRMArg = MCOperand_getImm(MCInst_getOperand(MI, (OpNo)));
169
1.54k
  if (!(MI->csh->syntax & CS_OPT_SYNTAX_NO_ALIAS_TEXT) &&
170
1.54k
      FRMArg == RISCVFPRndMode_DYN)
171
223
    return;
172
1.32k
  SStream_concat(O, "%s", ", ");
173
1.32k
  SStream_concat0(O, RISCVFPRndMode_roundingModeToString(FRMArg));
174
1.32k
}
175
176
void printFRMArgLegacy(MCInst *MI, unsigned OpNo, SStream *O)
177
123
{
178
123
  RISCV_add_cs_detail_0(MI, RISCV_OP_GROUP_FRMArgLegacy, OpNo);
179
123
  unsigned FRMArg = MCOperand_getImm(MCInst_getOperand(MI, (OpNo)));
180
  // Never print rounding mode if it's the default 'rne'. This ensures the
181
  // output can still be parsed by older tools that erroneously failed to
182
  // accept a rounding mode.
183
123
  if (FRMArg == RISCVFPRndMode_RNE)
184
37
    return;
185
86
  SStream_concat(O, "%s", ", ");
186
86
  SStream_concat0(O, RISCVFPRndMode_roundingModeToString(FRMArg));
187
86
}
188
189
void printFPImmOperand(MCInst *MI, unsigned OpNo, SStream *O)
190
313
{
191
313
  RISCV_add_cs_detail_0(MI, RISCV_OP_GROUP_FPImmOperand, OpNo);
192
313
  unsigned Imm = MCOperand_getImm(MCInst_getOperand(MI, (OpNo)));
193
313
  if (Imm == 1) {
194
117
    SStream_concat0(markup_OS(O, Markup_Immediate), "min");
195
196
  } else if (Imm == 30) {
196
91
    SStream_concat0(markup_OS(O, Markup_Immediate), "inf");
197
105
  } else if (Imm == 31) {
198
18
    SStream_concat0(markup_OS(O, Markup_Immediate), "nan");
199
87
  } else {
200
87
    float FPVal = getFPImm(Imm);
201
    // If the value is an integer, print a .0 fraction. Otherwise, use %g to
202
    // which will not print trailing zeros and will use scientific notation
203
    // if it is shorter than printing as a decimal. The smallest value requires
204
    // 12 digits of precision including the decimal.
205
87
    if (FPVal == (int)(FPVal))
206
41
      printfFloat(markup_OS(O, Markup_Immediate), "%.1f",
207
41
            FPVal);
208
46
    else
209
46
      printfFloat(markup_OS(O, Markup_Immediate), "%.12g",
210
46
            FPVal);
211
87
  }
212
313
}
213
214
void printZeroOffsetMemOp(MCInst *MI, unsigned OpNo, SStream *O)
215
1.32k
{
216
1.32k
  RISCV_add_cs_detail_0(MI, RISCV_OP_GROUP_ZeroOffsetMemOp, OpNo);
217
1.32k
  MCOperand *MO = MCInst_getOperand(MI, (OpNo));
218
219
1.32k
  CS_ASSERT(MCOperand_isReg(MO) &&
220
1.32k
      "printZeroOffsetMemOp can only print register operands");
221
1.32k
  SStream_concat0(O, "(");
222
1.32k
  printRegName(O, MCOperand_getReg(MO));
223
1.32k
  SStream_concat0(O, ")");
224
1.32k
}
225
226
void printVTypeI(MCInst *MI, unsigned OpNo, SStream *O)
227
436
{
228
436
  RISCV_add_cs_detail_0(MI, RISCV_OP_GROUP_VTypeI, OpNo);
229
436
  unsigned Imm = MCOperand_getImm(MCInst_getOperand(MI, (OpNo)));
230
  // Print the raw immediate for reserved values: vlmul[2:0]=4, vsew[2:0]=0b1xx,
231
  // or non-zero in bits 8 and above.
232
436
  if (RISCVVType_getVLMUL(Imm) == RISCVII_LMUL_RESERVED ||
233
430
      RISCVVType_getSEW(Imm) > 64 || (Imm >> 8) != 0) {
234
210
    printUInt64(O, Imm);
235
210
    return;
236
210
  }
237
  // Print the text form.
238
226
  printVType(Imm, O);
239
226
}
240
241
void printRlist(MCInst *MI, unsigned OpNo, SStream *O)
242
0
{
243
0
  RISCV_add_cs_detail_0(MI, RISCV_OP_GROUP_Rlist, OpNo);
244
0
  unsigned Imm = MCOperand_getImm(MCInst_getOperand(MI, (OpNo)));
245
0
  SStream_concat0(O, "{");
246
0
  switch (Imm) {
247
0
  case RISCVZC_RLISTENCODE_RA:
248
0
    SStream_concat0(markup_OS(O, Markup_Register),
249
0
        (ArchRegNames ? "x1" : "ra"));
250
0
    break;
251
0
  case RISCVZC_RLISTENCODE_RA_S0:
252
0
    SStream_concat0(markup_OS(O, Markup_Register),
253
0
        (ArchRegNames ? "x1" : "ra"));
254
0
    SStream_concat0(O, ", ");
255
0
    SStream_concat0(markup_OS(O, Markup_Register),
256
0
        (ArchRegNames ? "x8" : "s0"));
257
0
    break;
258
0
  case RISCVZC_RLISTENCODE_RA_S0_S1:
259
0
    SStream_concat0(markup_OS(O, Markup_Register),
260
0
        (ArchRegNames ? "x1" : "ra"));
261
0
    SStream_concat0(O, ", ");
262
0
    SStream_concat0(markup_OS(O, Markup_Register),
263
0
        (ArchRegNames ? "x8" : "s0"));
264
0
    SStream_concat0(O, "-");
265
266
0
    SStream_concat0(markup_OS(O, Markup_Register),
267
0
        (ArchRegNames ? "x9" : "s1"));
268
0
    break;
269
0
  case RISCVZC_RLISTENCODE_RA_S0_S2:
270
0
    SStream_concat0(markup_OS(O, Markup_Register),
271
0
        (ArchRegNames ? "x1" : "ra"));
272
0
    SStream_concat0(O, ", ");
273
0
    SStream_concat0(markup_OS(O, Markup_Register),
274
0
        (ArchRegNames ? "x8" : "s0"));
275
0
    SStream_concat0(O, "-");
276
277
0
    SStream_concat0(markup_OS(O, Markup_Register),
278
0
        (ArchRegNames ? "x9" : "s2"));
279
0
    if (ArchRegNames) {
280
0
      SStream_concat0(O, ", ");
281
0
      SStream_concat0(markup_OS(O, Markup_Register), "x18");
282
0
    }
283
0
    break;
284
0
  case RISCVZC_RLISTENCODE_RA_S0_S3:
285
0
  case RISCVZC_RLISTENCODE_RA_S0_S4:
286
0
  case RISCVZC_RLISTENCODE_RA_S0_S5:
287
0
  case RISCVZC_RLISTENCODE_RA_S0_S6:
288
0
  case RISCVZC_RLISTENCODE_RA_S0_S7:
289
0
  case RISCVZC_RLISTENCODE_RA_S0_S8:
290
0
  case RISCVZC_RLISTENCODE_RA_S0_S9:
291
0
  case RISCVZC_RLISTENCODE_RA_S0_S11:
292
0
    SStream_concat0(markup_OS(O, Markup_Register),
293
0
        (ArchRegNames ? "x1" : "ra"));
294
0
    SStream_concat0(O, ", ");
295
0
    SStream_concat0(markup_OS(O, Markup_Register),
296
0
        (ArchRegNames ? "x8" : "s0"));
297
0
    SStream_concat0(O, "-");
298
299
0
    if (ArchRegNames) {
300
0
      SStream_concat0(markup_OS(O, Markup_Register), "x9");
301
0
      SStream_concat0(O, ", ");
302
0
      SStream_concat0(markup_OS(O, Markup_Register), "x18");
303
0
      SStream_concat0(O, "-");
304
0
    }
305
0
    SStream_concat0(
306
0
      markup_OS(O, Markup_Register),
307
0
      doGetRegisterName(
308
0
        RISCV_X19 +
309
0
        (Imm == RISCVZC_RLISTENCODE_RA_S0_S11 ?
310
0
           8 :
311
0
           Imm - RISCVZC_RLISTENCODE_RA_S0_S3)));
312
0
    break;
313
0
  default:
314
0
    CS_ASSERT(0 && "invalid register list");
315
0
  }
316
0
  SStream_concat0(O, "}");
317
0
}
318
319
void printRegReg(MCInst *MI, unsigned OpNo, SStream *O)
320
501
{
321
501
  RISCV_add_cs_detail_0(MI, RISCV_OP_GROUP_RegReg, OpNo);
322
501
  MCOperand *MO = MCInst_getOperand(MI, (OpNo));
323
324
501
  CS_ASSERT(MCOperand_isReg(MO) &&
325
501
      "printRegReg can only print register operands");
326
501
  if (MCOperand_getReg(MO) == RISCV_NoRegister)
327
0
    return;
328
501
  printRegName(O, MCOperand_getReg(MO));
329
330
501
  SStream_concat0(O, "(");
331
501
  MCOperand *MO1 = MCInst_getOperand(MI, (OpNo + 1));
332
501
  CS_ASSERT(MCOperand_isReg(MO1) &&
333
501
      "printRegReg can only print register operands");
334
501
  printRegName(O, MCOperand_getReg(MO1));
335
501
  SStream_concat0(O, ")");
336
501
}
337
338
void printSpimm(MCInst *MI, unsigned OpNo, SStream *O)
339
0
{
340
0
  RISCV_add_cs_detail_0(MI, RISCV_OP_GROUP_Spimm, OpNo);
341
0
  int64_t Imm = MCOperand_getImm(MCInst_getOperand(MI, (OpNo)));
342
0
  unsigned Opcode = MCInst_getOpcode(MI);
343
0
  bool IsRV64 = RISCV_getFeatureBits(MI->csh->mode, RISCV_Feature64Bit);
344
0
  bool IsEABI = RISCV_getFeatureBits(MI->csh->mode, RISCV_FeatureRVE);
345
0
  int64_t Spimm = 0;
346
0
  int64_t RlistVal = MCOperand_getImm(MCInst_getOperand(MI, (0)));
347
0
  CS_ASSERT(RlistVal != 16 && "Incorrect rlist.");
348
0
  unsigned Base = RISCVZC_getStackAdjBase(RlistVal, IsRV64, IsEABI);
349
0
  Spimm = Imm + Base;
350
0
  CS_ASSERT((Spimm >= Base && Spimm <= Base + 48) && "Incorrect spimm");
351
0
  if (Opcode == RISCV_CM_PUSH)
352
0
    Spimm = -Spimm;
353
354
0
  RISCVZC_printSpimm(Spimm, markup_OS(O, Markup_Immediate));
355
0
}
356
357
void printVMaskReg(MCInst *MI, unsigned OpNo, SStream *O)
358
3.09k
{
359
3.09k
  RISCV_add_cs_detail_0(MI, RISCV_OP_GROUP_VMaskReg, OpNo);
360
3.09k
  MCOperand *MO = MCInst_getOperand(MI, (OpNo));
361
362
3.09k
  CS_ASSERT(MCOperand_isReg(MO) &&
363
3.09k
      "printVMaskReg can only print register operands");
364
3.09k
  if (MCOperand_getReg(MO) == RISCV_NoRegister)
365
1.68k
    return;
366
1.40k
  SStream_concat0(O, ", ");
367
1.40k
  printRegName(O, MCOperand_getReg(MO));
368
1.40k
  SStream_concat0(O, ".t");
369
1.40k
}
370
371
void RISCV_LLVM_printInstruction(MCInst *MI, SStream *O,
372
         void * /* MCRegisterInfo* */ info)
373
61.2k
{
374
61.2k
  MI->MRI = (MCRegisterInfo *)info;
375
376
61.2k
  MCInst_setIsAlias(MI, false);
377
378
  /* check for a non-compressed instruction */
379
61.2k
  MCInst Uncompressed;
380
61.2k
  MCInst_Init(&Uncompressed, MI->csh->arch);
381
382
61.2k
  MCInst *McInstr = MI;
383
61.2k
  bool is_uncompressed = false;
384
  // side-effectful check for compressed instructions that creates the equivalent uncompressed instruction in case of true
385
  // (LLVM doesn't generate an API for doing a pure check)
386
61.2k
  if (uncompressInst(&Uncompressed, MI)) {
387
39.2k
    McInstr = &Uncompressed;
388
39.2k
    Uncompressed.address = MI->address;
389
39.2k
    Uncompressed.MRI = MI->MRI;
390
39.2k
    Uncompressed.csh = MI->csh;
391
39.2k
    Uncompressed.flat_insn = MI->flat_insn;
392
39.2k
    is_uncompressed = true;
393
39.2k
  }
394
395
  // print the exact instruction text and done
396
61.2k
  bool print_exact_text =
397
61.2k
    (MI->csh->syntax & CS_OPT_SYNTAX_NO_ALIAS_TEXT) ||
398
61.2k
    (is_uncompressed &&
399
39.2k
     MI->csh->syntax & CS_OPT_SYNTAX_NO_ALIAS_TEXT_COMPRESSED);
400
61.2k
  if (print_exact_text) {
401
0
    printInstruction(MI, MI->address, O);
402
61.2k
  } else {
403
    // side-effectful check for alias instructions that prints to the SStream if true
404
61.2k
    if (printAliasInstr(McInstr, MI->address, O)) {
405
9.75k
      MCInst_setIsAlias(MI, true);
406
      // do we still want the exact details even if the text is alias ?
407
9.75k
      if (!map_use_alias_details(MI) && detail_is_set(MI)) {
408
        // disable actual printing
409
0
        SStream_Close(O);
410
        // discard the alias operands
411
0
        memset(MI->flat_insn->detail->riscv.operands, 0,
412
0
               sizeof(MI->flat_insn->detail->riscv
413
0
                  .operands));
414
0
        MI->flat_insn->detail->riscv.op_count = 0;
415
        // re-disassemble again with no printing in order to obtain the full details
416
        // including the whole operands array
417
0
        printInstruction(MI, MI->address, O);
418
        // re-open the stream to restore the usual state
419
0
        SStream_Open(O);
420
0
      }
421
9.75k
    } else // the instruction is not an alias
422
51.5k
      printInstruction(McInstr, MI->address, O);
423
61.2k
  }
424
61.2k
  RISCV_add_groups(MI);
425
61.2k
  RISCV_add_missing_write_access(MI);
426
61.2k
  RISCV_compact_operands(MI);
427
61.2k
}
428
429
const char *getSysRegName(unsigned reg)
430
0
{
431
0
  const RISCV_SysReg *SysReg = RISCV_lookupSysRegByEncoding(reg);
432
0
  return SysReg->Name;
433
0
}
434
435
const char *RISCV_LLVM_getRegisterName(unsigned RegNo, unsigned AltIdx)
436
13.3k
{
437
13.3k
  return getRegisterName(RegNo, AltIdx);
438
13.3k
}
439
440
bool isCompressed(MCInst *MI)
441
61.2k
{
442
61.2k
  MCInst unused;
443
61.2k
  MCInst_Init(&unused, MI->csh->arch);
444
61.2k
  return uncompressInst(&unused, MI);
445
61.2k
}