Coverage Report

Created: 2026-10-17 07:17

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 "RISCVBaseInfo.h"
15
#include "RISCVMapping.h"
16
17
#define GET_INSTRINFO_ENUM
18
#include "RISCVGenInstrInfo.inc"
19
20
#define GET_REGINFO_ENUM
21
#define GET_REGINFO_MC_DESC
22
#include "RISCVGenRegisterInfo.inc"
23
24
#include "RISCVInstPrinter.h"
25
26
const char *RISCV_reg_name(csh handle, unsigned int reg)
27
27.7k
{
28
27.7k
  int syntax_opt = ((cs_struct *)(uintptr_t)handle)->syntax;
29
30
27.7k
  if (syntax_opt & CS_OPT_SYNTAX_NOREGNAME) {
31
0
    return RISCV_LLVM_getRegisterName(reg, RISCV_NoRegAltName);
32
0
  }
33
27.7k
  return RISCV_LLVM_getRegisterName(reg, RISCV_ABIRegAltName);
34
27.7k
}
35
36
static const insn_map insns[] = {
37
#include "RISCVGenCSMappingInsn.inc"
38
};
39
40
const insn_map *RISCV_insns = insns;
41
const unsigned int RISCV_insn_count = ARR_SIZE(insns);
42
43
#ifndef CAPSTONE_DIET
44
static const map_insn_ops insn_operands[] = {
45
#include "RISCVGenCSMappingInsnOp.inc"
46
};
47
48
static const name_map insn_alias_mnem_map[] = {
49
#include "RISCVGenCSAliasMnemMap.inc"
50
};
51
#endif
52
53
void RISCV_add_cs_detail_0(MCInst *MI, riscv_op_group opgroup, unsigned OpNum)
54
200k
{
55
200k
  if (!detail_is_set(MI))
56
0
    return;
57
  // Rounding mode: store in detail, not as a regular operand.
58
200k
  if (opgroup == RISCV_OP_GROUP_FRMArg ||
59
198k
      opgroup == RISCV_OP_GROUP_FRMArgLegacy) {
60
2.92k
    unsigned frm = (unsigned)MCInst_getOperand(MI, OpNum)->ImmVal;
61
2.92k
    riscv_rounding_mode rm;
62
2.92k
    switch (frm) {
63
1.17k
    case RISCVFPRndMode_RNE:
64
1.17k
      rm = RISCV_RM_RNE;
65
1.17k
      break;
66
480
    case RISCVFPRndMode_RTZ:
67
480
      rm = RISCV_RM_RTZ;
68
480
      break;
69
538
    case RISCVFPRndMode_RDN:
70
538
      rm = RISCV_RM_RDN;
71
538
      break;
72
204
    case RISCVFPRndMode_RUP:
73
204
      rm = RISCV_RM_RUP;
74
204
      break;
75
49
    case RISCVFPRndMode_RMM:
76
49
      rm = RISCV_RM_RMM;
77
49
      break;
78
477
    case RISCVFPRndMode_DYN:
79
477
      rm = RISCV_RM_DYN;
80
477
      break;
81
0
    default:
82
0
      rm = RISCV_RM_INVALID;
83
0
      break;
84
2.92k
    }
85
2.92k
    RISCV_get_detail(MI)->rounding_mode = rm;
86
2.92k
    return;
87
2.92k
  }
88
89
  // unmasked instructions, the mask register is not real
90
197k
  if (opgroup == RISCV_OP_GROUP_VMaskReg) {
91
5.40k
    MCOperand *mask = MCInst_getOperand(MI, OpNum);
92
5.40k
    if (MCOperand_isReg(mask) &&
93
5.40k
        MCOperand_getReg(mask) == RISCV_NoRegister)
94
2.75k
      return;
95
5.40k
  }
96
97
194k
  if (opgroup == RISCV_OP_GROUP_FPImmOperand) {
98
1.77k
    unsigned Imm = (unsigned)MCInst_getOperand(MI, OpNum)->ImmVal;
99
1.77k
    cs_riscv_op *op = RISCV_get_detail_op_at(MI, OpNum);
100
1.77k
    op->type = RISCV_OP_FP;
101
1.77k
    op->access = (cs_ac_type)map_get_op_access(MI, OpNum);
102
1.77k
    switch (Imm) {
103
329
    case 1: // min
104
329
      switch (MI->Opcode) {
105
286
      case RISCV_FLI_S:
106
286
        op->dimm = (double)FLT_MIN;
107
286
        break;
108
0
      case RISCV_FLI_D:
109
0
        op->dimm = (double)DBL_MIN;
110
0
        break;
111
43
      case RISCV_FLI_H:
112
43
        op->dimm = 6.103515625e-05;
113
43
        break;
114
0
      default:
115
0
        op->dimm = 0.0;
116
0
        break;
117
329
      }
118
329
      break;
119
533
    case 30: // inf
120
533
      op->dimm = INFINITY;
121
533
      break;
122
67
    case 31: // nan
123
67
      op->dimm = NAN;
124
67
      break;
125
845
    default:
126
845
      op->dimm = (double)getFPImm(Imm);
127
845
      break;
128
1.77k
    }
129
1.77k
    RISCV_inc_op_count(MI);
130
1.77k
    return;
131
1.77k
  }
132
192k
  cs_riscv_op *op = RISCV_get_detail_op_at(MI, OpNum);
133
192k
  op->type = (riscv_op_type)map_get_op_type(MI, OpNum);
134
192k
  op->access = (cs_ac_type)map_get_op_access(MI, OpNum);
135
192k
  switch (map_get_op_type(MI, OpNum)) {
136
116k
  case CS_OP_REG:
137
116k
    op->reg = MCInst_getOperand(MI, OpNum)->RegVal;
138
116k
    break;
139
0
  case CS_OP_MEM:
140
0
    op->mem.base = 0;
141
0
    op->mem.disp = MCInst_getOperand(MI, OpNum)->ImmVal;
142
0
    break;
143
44.8k
  case CS_OP_IMM: {
144
44.8k
    uint64_t val = MCInst_getOperand(MI, OpNum)->ImmVal;
145
44.8k
    if (opgroup != RISCV_OP_GROUP_CSRSystemRegister) {
146
42.6k
      op->imm = val;
147
42.6k
      if (opgroup == RISCV_OP_GROUP_BranchOperand) {
148
5.24k
        op->imm += MI->address;
149
5.24k
      }
150
42.6k
    } else /* system register read-write */ {
151
2.21k
      op->type = RISCV_OP_CSR;
152
2.21k
      op->csr = val;
153
      // CSR instruction always read-writes the system operand
154
2.21k
      op->access = CS_AC_READ_WRITE;
155
2.21k
    }
156
44.8k
    break;
157
0
  }
158
17.5k
  case CS_OP_MEM_REG:
159
17.5k
    op->type = (riscv_op_type)CS_OP_MEM;
160
17.5k
    op->mem.base = MCInst_getOperand(MI, OpNum)->RegVal;
161
17.5k
    break;
162
13.6k
  case CS_OP_MEM_IMM:
163
    // fill in the disp in the last operand
164
13.6k
    op = RISCV_get_detail_op_at(MI, OpNum - 1);
165
13.6k
    op->type = (riscv_op_type)CS_OP_MEM;
166
13.6k
    op->mem.disp = MCInst_getOperand(MI, OpNum)->ImmVal;
167
13.6k
    RISCV_dec_op_count(
168
13.6k
      MI); // don't increase the count, cancel the coming increment
169
13.6k
    break;
170
286
  case CS_OP_INVALID:
171
286
    break;
172
0
  default: {
173
0
    CS_ASSERT(0 && "unhandled operand type");
174
0
  }
175
192k
  }
176
192k
  RISCV_inc_op_count(MI);
177
192k
}
178
179
static inline void RISCV_add_adhoc_groups(MCInst *MI);
180
181
void RISCV_add_groups(MCInst *MI)
182
80.0k
{
183
80.0k
  if (!detail_is_set(MI))
184
0
    return;
185
186
80.0k
  get_detail(MI)->groups_count = 0;
187
188
80.0k
#ifndef CAPSTONE_DIET
189
80.0k
  int i = 0;
190
177k
  while (insns[MI->Opcode].groups[i] != 0) {
191
97.7k
    add_group(MI, insns[MI->Opcode].groups[i]);
192
97.7k
    i++;
193
97.7k
  }
194
80.0k
#endif
195
196
80.0k
  RISCV_add_adhoc_groups(MI);
197
80.0k
}
198
199
enum {
200
#define GET_ENUM_VALUES_RISCVOpcode
201
#include "RISCVGenCSSystemOperandsEnum.inc"
202
};
203
204
static inline void RISCV_add_privileged_group(MCInst *MI)
205
80.0k
{
206
80.0k
  const uint8_t *bytes = MI->flat_insn->bytes;
207
80.0k
  uint8_t opcode = bytes[0] & 0x80;
208
  // no privileged instruction has a major opcode other than SYSTEM
209
80.0k
  if (opcode != RISCV_RISCVOPCODE_SYSTEM) {
210
80.0k
    return;
211
80.0k
  }
212
0
  uint8_t func3 = (bytes[1] >> 4) & 0x7;
213
  // no privileged instruction has a minor opcode other than PRIV or PRIVM
214
0
  if (func3 != 0 && func3 != 0x4) {
215
0
    return;
216
0
  }
217
0
  uint16_t func12 = readBytes16(MI, &(bytes[2])) >> 4;
218
  // ecall and ebreak has SYSTEM and PRIV but aren't privileged
219
0
  if (func12 == 0 || func12 == 1) {
220
0
    return;
221
0
  }
222
0
  uint8_t func6 = func12 >> 6;
223
  // a subspace under extension-defined custom SYSTEM instructions that is not privileged
224
0
  if (func6 == 0x23 || func6 == 0x33) {
225
0
    return;
226
0
  }
227
0
  add_group(MI, RISCV_GRP_PRIVILEGE);
228
0
}
229
230
static inline void RISCV_add_interrupt_group(MCInst *MI)
231
80.0k
{
232
80.0k
  if (MI->Opcode == RISCV_ECALL || MI->Opcode == RISCV_EBREAK) {
233
313
    add_group(MI, RISCV_GRP_INT);
234
313
  }
235
80.0k
}
236
237
static inline void RISCV_add_interrupt_ret_group(MCInst *MI)
238
80.0k
{
239
80.0k
  if (MI->Opcode == RISCV_MRET || MI->Opcode == RISCV_SRET) {
240
109
    add_group(MI, RISCV_GRP_IRET);
241
109
  }
242
80.0k
}
243
244
// calls are implemented in RISCV as plain jumps that happen to set a link register containing the return address
245
// but this link register could be given as the null register x0, discarding the return address and making them jumps
246
static inline void RISCV_add_call_group(MCInst *MI)
247
80.0k
{
248
80.0k
  if (MI->Opcode == RISCV_JAL || MI->Opcode == RISCV_JALR) {
249
1.79k
    cs_riscv_op *op = RISCV_get_detail_op_at(MI, 0);
250
1.79k
    if ((op->type == (riscv_op_type)CS_OP_REG) &&
251
1.16k
        op->reg != RISCV_REG_X0 && (op->access & CS_AC_WRITE)) {
252
1.16k
      add_group(MI, RISCV_GRP_CALL);
253
1.16k
    }
254
1.79k
    if (MI->Opcode == RISCV_JAL) {
255
701
      add_group(MI, RISCV_GRP_BRANCH_RELATIVE);
256
701
    }
257
1.79k
  }
258
80.0k
}
259
260
// returns are implemented in RISCV as a plain indirect jump that happen to reference the return address register ra == x1
261
static inline void RISCV_add_ret_group(MCInst *MI)
262
80.0k
{
263
80.0k
  if (MI->Opcode == RISCV_C_JR) {
264
    // indirect jumps whose source is ra
265
322
    cs_riscv_op *op = RISCV_get_detail_op_at(MI, 0);
266
322
    if ((op->type == (riscv_op_type)CS_OP_REG) &&
267
0
        op->reg == RISCV_REG_X1) {
268
0
      add_group(MI, RISCV_GRP_RET);
269
322
    } else {
270
322
      add_group(MI, RISCV_GRP_JUMP);
271
322
    }
272
322
  }
273
80.0k
  if (MI->Opcode == RISCV_JALR) {
274
    // indirect jumps whose source is ra
275
1.09k
    cs_riscv_op *dstreg = RISCV_get_detail_op_at(MI, 0);
276
1.09k
    cs_riscv_op *op = RISCV_get_detail_op_at(MI, 1);
277
1.09k
    cs_riscv_op *op2 = RISCV_get_detail_op_at(MI, 2);
278
1.09k
    if ((op->type == (riscv_op_type)CS_OP_REG) &&
279
853
        op->reg == RISCV_REG_X1 &&
280
378
        op2->type == (riscv_op_type)CS_OP_IMM && op2->imm == 0 &&
281
0
        dstreg->type == (riscv_op_type)CS_OP_REG &&
282
0
        dstreg->reg == RISCV_REG_X0) {
283
0
      add_group(MI, RISCV_GRP_RET);
284
1.09k
    } else {
285
1.09k
      if (!((dstreg->type == (riscv_op_type)CS_OP_REG) &&
286
496
            dstreg->reg != RISCV_REG_X0 &&
287
601
            (dstreg->access & CS_AC_WRITE))) {
288
601
        add_group(MI, RISCV_GRP_JUMP);
289
601
      }
290
1.09k
    }
291
1.09k
  }
292
80.0k
}
293
294
static inline void RISCV_add_adhoc_groups(MCInst *MI)
295
80.0k
{
296
80.0k
  RISCV_add_privileged_group(MI);
297
80.0k
  RISCV_add_interrupt_group(MI);
298
80.0k
  RISCV_add_interrupt_ret_group(MI);
299
80.0k
  RISCV_add_call_group(MI);
300
80.0k
  RISCV_add_ret_group(MI);
301
80.0k
}
302
303
// memset all stalled values in the detail struct to 0 before disassembling any next instruction
304
void RISCV_init_cs_detail(MCInst *MI)
305
80.9k
{
306
80.9k
  if (detail_is_set(MI))
307
80.9k
    memset(get_detail(MI), 0,
308
80.9k
           offsetof(cs_detail, riscv) + sizeof(cs_riscv));
309
80.9k
}
310
311
// for weird reasons some instructions end up with valid operands that are
312
// interspersed with invalid operands, i.e. the operands array is an "island"
313
// of valid operands with invalid gaps between them, this function will compactify
314
// all the valid operands and pad the rest of the array to invalid
315
void RISCV_compact_operands(MCInst *MI)
316
80.0k
{
317
80.0k
  if (!detail_is_set(MI))
318
0
    return;
319
80.0k
  cs_riscv_op *ops = RISCV_get_detail(MI)->operands;
320
80.0k
  unsigned int write_pos = 0;
321
322
  // Move valid elements to front
323
720k
  for (unsigned int read_pos = 0; read_pos < NUM_RISCV_OPS; read_pos++) {
324
640k
    if (ops[read_pos].type != (riscv_op_type)CS_OP_INVALID) {
325
194k
      if (write_pos != read_pos) {
326
18.2k
        ops[write_pos] = ops[read_pos];
327
18.2k
      }
328
194k
      write_pos++;
329
194k
    }
330
640k
  }
331
  // fill the rest, if any, with invalid
332
80.0k
  memset((void *)(&ops[write_pos]), CS_OP_INVALID,
333
80.0k
         (NUM_RISCV_OPS - write_pos) * sizeof(cs_riscv_op));
334
80.0k
}
335
336
// given internal insn id, return public instruction info
337
void RISCV_get_insn_id(cs_struct *h, cs_insn *insn, unsigned int id)
338
80.0k
{
339
80.0k
  insn_map const *insn_map = NULL;
340
341
80.0k
  if ((insn_map = lookup_insn_map(h, id))) {
342
80.0k
    insn->id = insn_map->mapid;
343
344
80.0k
    if (h->detail_opt) {
345
80.0k
#ifndef CAPSTONE_DIET
346
80.0k
      memcpy(insn->detail->regs_read, insn_map->regs_use,
347
80.0k
             sizeof(insn_map->regs_use));
348
80.0k
      insn->detail->regs_read_count =
349
80.0k
        (uint8_t)count_positive(insn_map->regs_use);
350
351
80.0k
      memcpy(insn->detail->regs_write, insn_map->regs_mod,
352
80.0k
             sizeof(insn_map->regs_mod));
353
80.0k
      insn->detail->regs_write_count =
354
80.0k
        (uint8_t)count_positive(insn_map->regs_mod);
355
356
80.0k
      memcpy(insn->detail->groups, insn_map->groups,
357
80.0k
             sizeof(insn_map->groups));
358
80.0k
      insn->detail->groups_count =
359
80.0k
        (uint8_t)count_positive8(insn_map->groups);
360
361
80.0k
      if (insn_map->branch || insn_map->indirect_branch) {
362
        // this insn also belongs to JUMP group. add JUMP group
363
3.41k
        insn->detail
364
3.41k
          ->groups[insn->detail->groups_count] =
365
3.41k
          RISCV_GRP_JUMP;
366
3.41k
        insn->detail->groups_count++;
367
3.41k
      }
368
80.0k
#endif
369
80.0k
    }
370
80.0k
  }
371
80.0k
}
372
373
static const char *const insn_name_maps[] = {
374
#include "RISCVGenCSMappingInsnName.inc"
375
};
376
377
// called from RISCV_LLVM_printInstruction() to avoid exporting
378
// insn_alias_mnem_map and its size via extern declarations
379
void RISCV_set_alias_id(MCInst *MI, SStream *O)
380
80.0k
{
381
80.0k
#ifndef CAPSTONE_DIET
382
80.0k
  map_set_alias_id(MI, O, insn_alias_mnem_map,
383
80.0k
       ARR_SIZE(insn_alias_mnem_map));
384
80.0k
#endif
385
80.0k
}
386
387
const char *RISCV_insn_name(csh handle, unsigned int id)
388
80.0k
{
389
80.0k
#ifndef CAPSTONE_DIET
390
80.0k
  if (id < RISCV_INS_ENDING)
391
80.0k
    return insn_name_maps[id];
392
393
0
  if (id > RISCV_INS_ALIAS_BEGIN && id < RISCV_INS_ALIAS_END)
394
0
    return insn_alias_mnem_map[id - RISCV_INS_ALIAS_BEGIN - 1].name;
395
0
#endif
396
0
  return NULL;
397
0
}
398
399
#ifndef CAPSTONE_DIET
400
static const name_map group_name_maps[] = {
401
  // generic groups
402
  { RISCV_GRP_INVALID, NULL },
403
  { RISCV_GRP_JUMP, "jump" },
404
  { RISCV_GRP_CALL, "call" },
405
  { RISCV_GRP_RET, "ret" },
406
  { RISCV_GRP_INT, "int" },
407
  { RISCV_GRP_IRET, "iret" },
408
  { RISCV_GRP_PRIVILEGE, "privileged" },
409
  { RISCV_GRP_BRANCH_RELATIVE, "branch_relative" },
410
411
// architecture specific
412
#include "RISCVGenCSFeatureName.inc"
413
414
  { RISCV_GRP_ENDING, NULL }
415
};
416
#endif
417
418
const char *RISCV_group_name(csh handle, unsigned int id)
419
151k
{
420
151k
#ifndef CAPSTONE_DIET
421
  // verify group id
422
  // if past the end
423
151k
  if (id >= RISCV_GRP_ENDING ||
424
      // or in the encoding gap between generic groups and arch-specific groups
425
151k
      (id > RISCV_GRP_BRANCH_RELATIVE && id < RISCV_FEATURE_HASSTDEXTI))
426
0
    return NULL;
427
151k
  return id2name(group_name_maps, ARR_SIZE(group_name_maps), id);
428
#else
429
  return NULL;
430
#endif
431
151k
}
432
433
// map instruction name to public instruction ID
434
riscv_insn RISCV_map_insn(const char *name)
435
0
{
436
0
  unsigned int i;
437
0
  for (i = 1; i < ARR_SIZE(insn_name_maps); i++) {
438
0
    if (!strcmp(name, insn_name_maps[i]))
439
0
      return i;
440
0
  }
441
0
#ifndef CAPSTONE_DIET
442
0
  for (i = 0; i < ARR_SIZE(insn_alias_mnem_map); i++) {
443
0
    if (!strcmp(name, insn_alias_mnem_map[i].name))
444
0
      return insn_alias_mnem_map[i].id;
445
0
  }
446
0
#endif
447
0
  return RISCV_INS_INVALID;
448
0
}
449
450
void RISCV_reg_access(const cs_insn *insn, cs_regs regs_read,
451
          uint8_t *regs_read_count, cs_regs regs_write,
452
          uint8_t *regs_write_count)
453
0
{
454
0
  const cs_riscv *riscv = &(insn->detail->riscv);
455
0
  uint8_t read_count = 0;
456
0
  uint8_t write_count = 0;
457
458
0
  for (int j = 0; j < riscv->op_count; j++) {
459
0
    const cs_riscv_op *op = &riscv->operands[j];
460
461
0
    if (op->type == RISCV_OP_REG) {
462
0
      if ((op->access & CS_AC_WRITE) &&
463
0
          !arr_exist(regs_write, write_count, op->reg)) {
464
0
        regs_write[write_count++] = (uint16_t)op->reg;
465
0
      }
466
0
      if ((op->access & CS_AC_READ) &&
467
0
          !arr_exist(regs_read, read_count, op->reg)) {
468
0
        regs_read[read_count++] = (uint16_t)op->reg;
469
0
      }
470
0
    } else if (op->type == RISCV_OP_MEM) {
471
0
      if (op->mem.base != RISCV_REG_INVALID &&
472
0
          !arr_exist(regs_read, read_count, op->mem.base)) {
473
0
        regs_read[read_count++] =
474
0
          (uint16_t)op->mem.base;
475
0
      }
476
0
    }
477
0
  }
478
479
0
  *regs_read_count = read_count;
480
0
  *regs_write_count = write_count;
481
0
}
482
483
void RISCV_init(MCRegisterInfo *MRI)
484
5.03k
{
485
5.03k
  MCRegisterInfo_InitMCRegisterInfo(MRI, RISCVRegDesc, RISCV_REG_ENDING,
486
5.03k
            0, 0, RISCVMCRegisterClasses,
487
5.03k
            ARR_SIZE(RISCVMCRegisterClasses), 0,
488
5.03k
            0, RISCVRegDiffLists, 0,
489
5.03k
            RISCVSubRegIdxLists,
490
5.03k
            ARR_SIZE(RISCVSubRegIdxLists), 0);
491
5.03k
}
492
493
#endif