Coverage Report

Created: 2026-03-13 06:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/capstonenext/arch/RISCV/RISCVMapping.c
Line
Count
Source
1
#include "capstone/cs_operand.h"
2
#include "capstone/riscv.h"
3
#include <stdint.h>
4
#include <float.h>
5
#include <math.h>
6
#ifdef CAPSTONE_HAS_RISCV
7
8
#include <string.h>
9
10
#include "../../Mapping.h"
11
#include "../../cs_simple_types.h"
12
#include "../../utils.h"
13
14
#include "RISCVMapping.h"
15
16
#define GET_INSTRINFO_ENUM
17
#include "RISCVGenInstrInfo.inc"
18
19
#define GET_REGINFO_ENUM
20
#define GET_REGINFO_MC_DESC
21
#include "RISCVGenRegisterInfo.inc"
22
23
#include "RISCVInstPrinter.h"
24
25
const char *RISCV_reg_name(csh handle, unsigned int reg)
26
13.3k
{
27
13.3k
  int syntax_opt = ((cs_struct *)(uintptr_t)handle)->syntax;
28
29
13.3k
  if (syntax_opt & CS_OPT_SYNTAX_NOREGNAME) {
30
0
    return RISCV_LLVM_getRegisterName(reg, RISCV_NoRegAltName);
31
0
  }
32
13.3k
  return RISCV_LLVM_getRegisterName(reg, RISCV_ABIRegAltName);
33
13.3k
}
34
35
static const insn_map insns[] = {
36
#include "RISCVGenCSMappingInsn.inc"
37
};
38
39
const insn_map *RISCV_insns = insns;
40
const unsigned int RISCV_insn_count = ARR_SIZE(insns);
41
42
#ifndef CAPSTONE_DIET
43
44
static const map_insn_ops insn_operands[] = {
45
#include "RISCVGenCSMappingInsnOp.inc"
46
};
47
48
#endif
49
50
void RISCV_add_cs_detail_0(MCInst *MI, riscv_op_group opgroup, unsigned OpNum)
51
155k
{
52
155k
  if (!detail_is_set(MI))
53
0
    return;
54
  // are not "true" arguments and has no Capstone equivalent
55
155k
  if (opgroup == RISCV_OP_GROUP_FRMArg ||
56
153k
      opgroup == RISCV_OP_GROUP_FRMArgLegacy)
57
1.67k
    return;
58
59
153k
  if (opgroup == RISCV_OP_GROUP_FPImmOperand) {
60
313
    unsigned Imm = (unsigned)MCInst_getOperand(MI, OpNum)->ImmVal;
61
313
    cs_riscv_op *op = RISCV_get_detail_op_at(MI, OpNum);
62
313
    op->type = RISCV_OP_FP;
63
313
    op->access = (cs_ac_type)map_get_op_access(MI, OpNum);
64
313
    switch (Imm) {
65
117
    case 1: // min
66
117
      switch (MI->Opcode) {
67
115
      case RISCV_FLI_S:
68
115
        op->dimm = (double)FLT_MIN;
69
115
        break;
70
0
      case RISCV_FLI_D:
71
0
        op->dimm = (double)DBL_MIN;
72
0
        break;
73
2
      case RISCV_FLI_H:
74
2
        op->dimm = 6.103515625e-05;
75
2
        break;
76
0
      default:
77
0
        op->dimm = 0.0;
78
0
        break;
79
117
      }
80
117
      break;
81
117
    case 30: // inf
82
91
      op->dimm = INFINITY;
83
91
      break;
84
18
    case 31: // nan
85
18
      op->dimm = NAN;
86
18
      break;
87
87
    default:
88
87
      op->dimm = (double)getFPImm(Imm);
89
87
      break;
90
313
    }
91
313
    RISCV_inc_op_count(MI);
92
313
    return;
93
313
  }
94
153k
  cs_riscv_op *op = RISCV_get_detail_op_at(MI, OpNum);
95
153k
  op->type = (riscv_op_type)map_get_op_type(MI, OpNum);
96
153k
  op->access = (cs_ac_type)map_get_op_access(MI, OpNum);
97
153k
  switch (map_get_op_type(MI, OpNum)) {
98
87.9k
  case CS_OP_REG:
99
87.9k
    op->reg = MCInst_getOperand(MI, OpNum)->RegVal;
100
87.9k
    break;
101
0
  case CS_OP_MEM:
102
0
    op->mem.base = 0;
103
0
    op->mem.disp = MCInst_getOperand(MI, OpNum)->ImmVal;
104
0
    break;
105
31.1k
  case CS_OP_IMM: {
106
31.1k
    uint64_t val = MCInst_getOperand(MI, OpNum)->ImmVal;
107
31.1k
    if (opgroup != RISCV_OP_GROUP_CSRSystemRegister) {
108
29.7k
      op->imm = val;
109
29.7k
      if (opgroup == RISCV_OP_GROUP_BranchOperand) {
110
5.61k
        op->imm += MI->address;
111
5.61k
      }
112
29.7k
    } else /* system register read-write */ {
113
1.36k
      op->type = RISCV_OP_CSR;
114
1.36k
      op->csr = val;
115
      // CSR instruction always read-writes the system operand
116
1.36k
      op->access = CS_AC_READ_WRITE;
117
1.36k
    }
118
31.1k
    break;
119
0
  }
120
17.9k
  case CS_OP_MEM_REG:
121
17.9k
    op->type = (riscv_op_type)CS_OP_MEM;
122
17.9k
    op->mem.base = MCInst_getOperand(MI, OpNum)->RegVal;
123
17.9k
    break;
124
15.9k
  case CS_OP_MEM_IMM:
125
    // fill in the disp in the last operand
126
15.9k
    op = RISCV_get_detail_op_at(MI, OpNum - 1);
127
15.9k
    op->type = (riscv_op_type)CS_OP_MEM;
128
15.9k
    op->mem.disp = MCInst_getOperand(MI, OpNum)->ImmVal;
129
15.9k
    RISCV_dec_op_count(
130
15.9k
      MI); // don't increase the count, cancel the coming increment
131
15.9k
    break;
132
501
  case CS_OP_INVALID:
133
501
    break;
134
0
  default: {
135
0
    CS_ASSERT(0 && "unhandled operand type");
136
0
  }
137
153k
  }
138
153k
  RISCV_inc_op_count(MI);
139
153k
}
140
141
static inline void RISCV_add_adhoc_groups(MCInst *MI);
142
143
void RISCV_add_groups(MCInst *MI)
144
61.2k
{
145
61.2k
  if (!detail_is_set(MI))
146
0
    return;
147
148
61.2k
  get_detail(MI)->groups_count = 0;
149
150
61.2k
#ifndef CAPSTONE_DIET
151
61.2k
  int i = 0;
152
141k
  while (insns[MI->Opcode].groups[i] != 0) {
153
80.4k
    add_group(MI, insns[MI->Opcode].groups[i]);
154
80.4k
    i++;
155
80.4k
  }
156
61.2k
#endif
157
158
61.2k
  RISCV_add_adhoc_groups(MI);
159
61.2k
}
160
161
enum {
162
#define GET_ENUM_VALUES_RISCVOpcode
163
#include "RISCVGenCSSystemOperandsEnum.inc"
164
};
165
166
static inline void RISCV_add_privileged_group(MCInst *MI)
167
61.2k
{
168
61.2k
  const uint8_t *bytes = MI->flat_insn->bytes;
169
61.2k
  uint8_t opcode = bytes[0] & 0x80;
170
  // no privileged instruction has a major opcode other than SYSTEM
171
61.2k
  if (opcode != RISCV_RISCVOPCODE_SYSTEM) {
172
61.2k
    return;
173
61.2k
  }
174
0
  uint8_t func3 = (bytes[1] >> 4) & 0x7;
175
  // no privileged instruction has a minor opcode other than PRIV or PRIVM
176
0
  if (func3 != 0 && func3 != 0x4) {
177
0
    return;
178
0
  }
179
0
  uint16_t func12 = readBytes16(MI, &(bytes[2])) >> 4;
180
  // ecall and ebreak has SYSTEM and PRIV but aren't privileged
181
0
  if (func12 == 0 || func12 == 1) {
182
0
    return;
183
0
  }
184
0
  uint8_t func6 = func12 >> 6;
185
  // a subspace under extension-defined custom SYSTEM instructions that is not privileged
186
0
  if (func6 == 0x23 || func6 == 0x33) {
187
0
    return;
188
0
  }
189
0
  add_group(MI, RISCV_GRP_PRIVILEGE);
190
0
}
191
192
static inline void RISCV_add_interrupt_group(MCInst *MI)
193
61.2k
{
194
61.2k
  if (MI->Opcode == RISCV_ECALL || MI->Opcode == RISCV_EBREAK) {
195
13
    add_group(MI, RISCV_GRP_INT);
196
13
  }
197
61.2k
}
198
199
static inline void RISCV_add_interrupt_ret_group(MCInst *MI)
200
61.2k
{
201
61.2k
  if (MI->Opcode == RISCV_MRET || MI->Opcode == RISCV_SRET) {
202
26
    add_group(MI, RISCV_GRP_IRET);
203
26
  }
204
61.2k
}
205
206
// calls are implemented in RISCV as plain jumps that happen to set a link register containing the return address
207
// but this link register could be given as the null register x0, discarding the return address and making them jumps
208
static inline void RISCV_add_call_group(MCInst *MI)
209
61.2k
{
210
61.2k
  if (MI->Opcode == RISCV_JAL || MI->Opcode == RISCV_JALR) {
211
1.00k
    cs_riscv_op *op = RISCV_get_detail_op_at(MI, 0);
212
1.00k
    if ((op->type == (riscv_op_type)CS_OP_REG) &&
213
534
        op->reg != RISCV_REG_X0 && (op->access & CS_AC_WRITE)) {
214
534
      add_group(MI, RISCV_GRP_CALL);
215
534
    }
216
1.00k
    if (MI->Opcode == RISCV_JAL) {
217
718
      add_group(MI, RISCV_GRP_BRANCH_RELATIVE);
218
718
    }
219
1.00k
  }
220
61.2k
}
221
222
// returns are implemented in RISCV as a plain indirect jump that happen to reference the return address register ra == x1
223
static inline void RISCV_add_ret_group(MCInst *MI)
224
61.2k
{
225
61.2k
  if (MI->Opcode == RISCV_C_JR) {
226
    // indirect jumps whose source is ra
227
479
    cs_riscv_op *op = RISCV_get_detail_op_at(MI, 0);
228
479
    if ((op->type == (riscv_op_type)CS_OP_REG) &&
229
0
        op->reg == RISCV_REG_X1) {
230
0
      add_group(MI, RISCV_GRP_RET);
231
479
    } else {
232
479
      add_group(MI, RISCV_GRP_JUMP);
233
479
    }
234
479
  }
235
61.2k
  if (MI->Opcode == RISCV_JALR) {
236
    // indirect jumps whose source is ra
237
285
    cs_riscv_op *dstreg = RISCV_get_detail_op_at(MI, 0);
238
285
    cs_riscv_op *op = RISCV_get_detail_op_at(MI, 1);
239
285
    cs_riscv_op *op2 = RISCV_get_detail_op_at(MI, 2);
240
285
    if ((op->type == (riscv_op_type)CS_OP_REG) &&
241
167
        op->reg == RISCV_REG_X1 &&
242
27
        op2->type == (riscv_op_type)CS_OP_IMM && op2->imm == 0 &&
243
0
        dstreg->type == (riscv_op_type)CS_OP_REG &&
244
0
        dstreg->reg == RISCV_REG_X0) {
245
0
      add_group(MI, RISCV_GRP_RET);
246
285
    } else {
247
285
      if (!((dstreg->type == (riscv_op_type)CS_OP_REG) &&
248
86
            dstreg->reg != RISCV_REG_X0 &&
249
199
            (dstreg->access & CS_AC_WRITE))) {
250
199
        add_group(MI, RISCV_GRP_JUMP);
251
199
      }
252
285
    }
253
285
  }
254
61.2k
}
255
256
static inline void RISCV_add_adhoc_groups(MCInst *MI)
257
61.2k
{
258
61.2k
  RISCV_add_privileged_group(MI);
259
61.2k
  RISCV_add_interrupt_group(MI);
260
61.2k
  RISCV_add_interrupt_ret_group(MI);
261
61.2k
  RISCV_add_call_group(MI);
262
61.2k
  RISCV_add_ret_group(MI);
263
61.2k
}
264
265
// for weird reasons some instructions end up with valid operands that are
266
// interspersed with invalid operands, i.e. the operands array is an "island"
267
// of valid operands with invalid gaps between them, this function will compactify
268
// all the valid operands and pad the rest of the array to invalid
269
void RISCV_compact_operands(MCInst *MI)
270
61.2k
{
271
61.2k
  if (!detail_is_set(MI))
272
0
    return;
273
61.2k
  cs_riscv_op *ops = RISCV_get_detail(MI)->operands;
274
61.2k
  unsigned int write_pos = 0;
275
276
  // Move valid elements to front
277
551k
  for (unsigned int read_pos = 0; read_pos < NUM_RISCV_OPS; read_pos++) {
278
490k
    if (ops[read_pos].type != (riscv_op_type)CS_OP_INVALID) {
279
153k
      if (write_pos != read_pos) {
280
16.7k
        ops[write_pos] = ops[read_pos];
281
16.7k
      }
282
153k
      write_pos++;
283
153k
    }
284
490k
  }
285
  // fill the rest, if any, with invalid
286
61.2k
  memset((void *)(&ops[write_pos]), CS_OP_INVALID,
287
61.2k
         (NUM_RISCV_OPS - write_pos) * sizeof(cs_riscv_op));
288
61.2k
}
289
290
// some RISC-V instructions have only 2 apparent operands, one of them is read-write
291
// the actual operand information for those instruction should have 3 operands, the first and second are the same operand,
292
// but once with read and once write access
293
// when those instructions are disassembled only the operand entry with the read access is used,
294
// and therefore the read-write operand is wrongly classified as only-read
295
// this logic tries to correct that
296
void RISCV_add_missing_write_access(MCInst *MI)
297
61.2k
{
298
61.2k
  if (!detail_is_set(MI))
299
0
    return;
300
61.2k
  if (!isCompressed(MI))
301
21.9k
    return;
302
303
39.2k
  cs_riscv *riscv_details = RISCV_get_detail(MI);
304
39.2k
  cs_riscv_op *ops = riscv_details->operands;
305
  // make the detection condition as specific as possible
306
  // so it doesn't accidentally trigger for other cases
307
39.2k
  if (riscv_details->op_count == 2 && ops[0].type == RISCV_OP_INVALID &&
308
0
      ops[1].type == RISCV_OP_REG && ops[1].access == CS_AC_READ) {
309
0
    ops[1].access |= CS_AC_WRITE;
310
0
  }
311
39.2k
}
312
313
// given internal insn id, return public instruction info
314
void RISCV_get_insn_id(cs_struct *h, cs_insn *insn, unsigned int id)
315
61.2k
{
316
61.2k
  insn_map const *insn_map = NULL;
317
318
61.2k
  if ((insn_map = lookup_insn_map(h, id))) {
319
61.2k
    insn->id = insn_map->mapid;
320
321
61.2k
    if (h->detail_opt) {
322
61.2k
#ifndef CAPSTONE_DIET
323
61.2k
      memcpy(insn->detail->regs_read, insn_map->regs_use,
324
61.2k
             sizeof(insn_map->regs_use));
325
61.2k
      insn->detail->regs_read_count =
326
61.2k
        (uint8_t)count_positive(insn_map->regs_use);
327
328
61.2k
      memcpy(insn->detail->regs_write, insn_map->regs_mod,
329
61.2k
             sizeof(insn_map->regs_mod));
330
61.2k
      insn->detail->regs_write_count =
331
61.2k
        (uint8_t)count_positive(insn_map->regs_mod);
332
333
61.2k
      memcpy(insn->detail->groups, insn_map->groups,
334
61.2k
             sizeof(insn_map->groups));
335
61.2k
      insn->detail->groups_count =
336
61.2k
        (uint8_t)count_positive8(insn_map->groups);
337
338
61.2k
      if (insn_map->branch || insn_map->indirect_branch) {
339
        // this insn also belongs to JUMP group. add JUMP group
340
3.05k
        insn->detail
341
3.05k
          ->groups[insn->detail->groups_count] =
342
3.05k
          RISCV_GRP_JUMP;
343
3.05k
        insn->detail->groups_count++;
344
3.05k
      }
345
61.2k
#endif
346
61.2k
    }
347
61.2k
  }
348
61.2k
}
349
350
static const char *const insn_name_maps[] = {
351
#include "RISCVGenCSMappingInsnName.inc"
352
};
353
354
const char *RISCV_insn_name(csh handle, unsigned int id)
355
61.2k
{
356
61.2k
#ifndef CAPSTONE_DIET
357
61.2k
  if (id >= RISCV_INS_ENDING)
358
0
    return NULL;
359
360
61.2k
  return insn_name_maps[id];
361
#else
362
  return NULL;
363
#endif
364
61.2k
}
365
366
#ifndef CAPSTONE_DIET
367
static const name_map group_name_maps[] = {
368
  // generic groups
369
  { RISCV_GRP_INVALID, NULL },
370
  { RISCV_GRP_JUMP, "jump" },
371
  { RISCV_GRP_CALL, "call" },
372
  { RISCV_GRP_RET, "ret" },
373
  { RISCV_GRP_INT, "int" },
374
  { RISCV_GRP_IRET, "iret" },
375
  { RISCV_GRP_PRIVILEGE, "privileged" },
376
  { RISCV_GRP_BRANCH_RELATIVE, "branch_relative" },
377
378
// architecture specific
379
#include "RISCVGenCSFeatureName.inc"
380
381
  { RISCV_GRP_ENDING, NULL }
382
};
383
#endif
384
385
const char *RISCV_group_name(csh handle, unsigned int id)
386
82.4k
{
387
82.4k
#ifndef CAPSTONE_DIET
388
  // verify group id
389
  // if past the end
390
82.4k
  if (id >= RISCV_GRP_ENDING ||
391
      // or in the encoding gap between generic groups and arch-specific groups
392
82.4k
      (id > RISCV_GRP_BRANCH_RELATIVE && id < RISCV_FEATURE_HASSTDEXTI))
393
0
    return NULL;
394
82.4k
  return id2name(group_name_maps, ARR_SIZE(group_name_maps), id);
395
#else
396
  return NULL;
397
#endif
398
82.4k
}
399
400
// map instruction name to public instruction ID
401
riscv_insn RISCV_map_insn(const char *name)
402
0
{
403
0
  unsigned int i;
404
0
  for (i = 1; i < ARR_SIZE(insn_name_maps); i++) {
405
0
    if (!strcmp(name, insn_name_maps[i]))
406
0
      return i;
407
0
  }
408
0
  return RISCV_INS_INVALID;
409
0
}
410
411
void RISCV_init(MCRegisterInfo *MRI)
412
932
{
413
932
  MCRegisterInfo_InitMCRegisterInfo(MRI, RISCVRegDesc, RISCV_REG_ENDING,
414
932
            0, 0, RISCVMCRegisterClasses,
415
932
            ARR_SIZE(RISCVMCRegisterClasses), 0,
416
932
            0, RISCVRegDiffLists, 0,
417
932
            RISCVSubRegIdxLists,
418
932
            ARR_SIZE(RISCVSubRegIdxLists), 0);
419
932
}
420
421
#endif