Coverage Report

Created: 2025-07-04 06:11

/src/capstonenext/arch/Sparc/SparcMapping.c
Line
Count
Source (jump to first uncovered line)
1
/* Capstone Disassembly Engine */
2
/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2015 */
3
4
#ifdef CAPSTONE_HAS_SPARC
5
6
#include <stdio.h> // debug
7
#include <string.h>
8
9
#include "../../Mapping.h"
10
#include "../../utils.h"
11
#include "../../cs_simple_types.h"
12
13
#include "SparcMapping.h"
14
15
void Sparc_init_cs_detail(MCInst *MI)
16
32.7k
{
17
32.7k
  if (!detail_is_set(MI)) {
18
0
    return;
19
0
  }
20
32.7k
  memset(get_detail(MI), 0, offsetof(cs_detail, sparc) + sizeof(cs_sparc));
21
32.7k
  Sparc_get_detail(MI)->cc = SPARC_CC_UNDEF;
22
32.7k
  Sparc_get_detail(MI)->cc_field = SPARC_CC_FIELD_NONE;
23
32.7k
}
24
25
const insn_map sparc_insns[] = {
26
#include "SparcGenCSMappingInsn.inc"
27
};
28
29
void Sparc_set_instr_map_data(MCInst *MI)
30
32.7k
{
31
32.7k
  map_cs_id(MI, sparc_insns, ARR_SIZE(sparc_insns));
32
32.7k
  map_implicit_reads(MI, sparc_insns);
33
32.7k
  map_implicit_writes(MI, sparc_insns);
34
32.7k
  map_groups(MI, sparc_insns);
35
32.7k
  const sparc_suppl_info *suppl_info =
36
32.7k
    map_get_suppl_info(MI, sparc_insns);
37
32.7k
  if (suppl_info) {
38
32.7k
    Sparc_get_detail(MI)->format = suppl_info->form;
39
32.7k
  }
40
32.7k
}
41
42
/// Adds details which are not defined consistently as LLVM operands like
43
/// condition codes for alias instructions or branch hint bits.
44
static void Sparc_add_bit_details(MCInst *MI, const uint8_t *Bytes,
45
          size_t BytesLen)
46
32.7k
{
47
32.7k
  if (!Bytes || BytesLen < 4 || !detail_is_set(MI)) {
48
496
    return;
49
496
  }
50
32.2k
  uint32_t insn = readBytes32(MI, Bytes);
51
52
  // CC field
53
32.2k
  cs_sparc *detail = Sparc_get_detail(MI);
54
32.2k
  switch (detail->format) {
55
23.3k
  default:
56
23.3k
    break;
57
23.3k
  case SPARC_INSN_FORM_F2_2: {
58
    // This format is used either by B or FB instructions.
59
    // The op2 == 6 for the FB and 2 for B.
60
    // This is the only indicator we have here to determine which CC field is used
61
    // if we don't want big switch cases.
62
    //
63
    // See: Opcode Maps - Table 39 - Sparc V9 ISA
64
2.66k
    size_t op2 = get_insn_field_r(insn, 22, 24);
65
2.66k
    detail->cc_field = op2 == 6 ? SPARC_CC_FIELD_FCC0 : SPARC_CC_FIELD_ICC;
66
2.66k
    break;
67
0
  }
68
4.78k
  case SPARC_INSN_FORM_F2_3:
69
4.78k
    detail->cc_field = 0x4 | get_insn_field_r(insn, 20, 21);
70
4.78k
    break;
71
512
  case SPARC_INSN_FORM_TRAPSP:
72
512
    detail->cc_field = 0x4 | get_insn_field_r(insn, 11, 12);
73
512
    break;
74
378
  case SPARC_INSN_FORM_F4_1:
75
564
  case SPARC_INSN_FORM_F4_2:
76
564
    detail->cc_field = get_insn_field_r(insn, 11, 12);
77
564
    detail->cc_field |= get_insn_field_r(insn, 18, 18) << 2;
78
564
    break;
79
367
  case SPARC_INSN_FORM_F4_3:
80
367
    detail->cc_field = get_insn_field_r(insn, 11, 13);
81
367
    break;
82
32.2k
  }
83
84
  // Condition codes
85
32.2k
  switch (detail->format) {
86
16.1k
  default:
87
16.1k
    break;
88
16.1k
  case SPARC_INSN_FORM_F2_1:
89
8.26k
  case SPARC_INSN_FORM_F2_2:
90
13.0k
  case SPARC_INSN_FORM_F2_3:
91
13.5k
  case SPARC_INSN_FORM_TRAPSP: {
92
    // cond
93
    // Alias instructions don't define the conditions as operands.
94
    // We need to add them here to the details again.
95
13.5k
    sparc_cc cc = get_insn_field_r(insn, 25, 28);
96
13.5k
    if (MCInst_getOpcode(MI) == Sparc_CBCOND ||
97
13.5k
        MCInst_getOpcode(MI) == Sparc_CBCONDA) {
98
1.16k
      cc += SPARC_CC_CPCC_BEGIN;
99
1.16k
    }
100
13.5k
    detail->cc = cc;
101
13.5k
    break;
102
13.0k
  }
103
378
  case SPARC_INSN_FORM_F4_1:
104
564
  case SPARC_INSN_FORM_F4_2:
105
931
  case SPARC_INSN_FORM_F4_3: {
106
931
    sparc_cc cc = get_insn_field_r(insn, 14, 17);
107
931
    detail->cc = cc;
108
931
    break;
109
564
  }
110
1.44k
  case SPARC_INSN_FORM_F2_4: {
111
    // cond
112
    // Alias instructions don't define the conditions as operands.
113
    // We need to add them here to the details again.
114
1.44k
    sparc_cc rcc = get_insn_field_r(insn, 25, 27);
115
1.44k
    detail->cc = rcc + SPARC_CC_REG_BEGIN;
116
1.44k
    break;
117
564
  }
118
77
  case SPARC_INSN_FORM_F4_4R:
119
169
  case SPARC_INSN_FORM_F4_4I: {
120
169
    sparc_cc rcc = get_insn_field_r(insn, 10, 12);
121
169
    detail->cc = rcc + SPARC_CC_REG_BEGIN;
122
169
    break;
123
77
  }
124
32.2k
  }
125
32.2k
  switch (detail->cc_field) {
126
23.9k
  default:
127
28.6k
  case SPARC_CC_FIELD_ICC:
128
30.7k
  case SPARC_CC_FIELD_XCC:
129
30.7k
    break;
130
1.12k
  case SPARC_CC_FIELD_FCC0:
131
1.39k
  case SPARC_CC_FIELD_FCC1:
132
1.45k
  case SPARC_CC_FIELD_FCC2:
133
1.48k
  case SPARC_CC_FIELD_FCC3:
134
1.48k
    detail->cc += SPARC_CC_FCC_BEGIN;
135
1.48k
    break;
136
32.2k
  }
137
138
  // Hints
139
32.2k
  switch (detail->format) {
140
23.3k
  default:
141
23.3k
    break;
142
23.3k
  case SPARC_INSN_FORM_F2_2:
143
2.66k
    detail->hint = get_insn_field_r(insn, 29, 29);
144
2.66k
    break;
145
4.78k
  case SPARC_INSN_FORM_F2_3:
146
6.22k
  case SPARC_INSN_FORM_F2_4:
147
6.22k
    detail->hint = get_insn_field_r(insn, 29, 29);
148
6.22k
    detail->hint |=
149
6.22k
      get_insn_field_r(insn, 19, 19) == 0 ? SPARC_HINT_PN :
150
6.22k
                    SPARC_HINT_PT;
151
6.22k
    break;
152
32.2k
  }
153
32.2k
}
154
155
bool Sparc_getInstruction(csh handle, const uint8_t *code, size_t code_len,
156
        MCInst *instr, uint16_t *size, uint64_t address,
157
        void *info)
158
32.7k
{
159
32.7k
  Sparc_init_cs_detail(instr);
160
32.7k
  bool Result = Sparc_LLVM_getInstruction(handle, code, code_len, instr,
161
32.7k
            size, address,
162
32.7k
            info) != MCDisassembler_Fail;
163
32.7k
  Sparc_set_instr_map_data(instr);
164
165
32.7k
  Sparc_add_bit_details(instr, code, code_len);
166
32.7k
  return Result;
167
32.7k
}
168
169
void Sparc_init_mri(MCRegisterInfo *MRI)
170
1.40k
{
171
1.40k
  MCRegisterInfo_InitMCRegisterInfo(
172
1.40k
    MRI, SparcRegDesc, sizeof(SparcRegDesc), 0, 0,
173
1.40k
    SparcMCRegisterClasses, ARR_SIZE(SparcMCRegisterClasses), 0, 0,
174
1.40k
    SparcRegDiffLists, 0, SparcSubRegIdxLists,
175
1.40k
    ARR_SIZE(SparcSubRegIdxLists), 0);
176
1.40k
}
177
178
const char *Sparc_reg_name(csh handle, unsigned int reg)
179
13.8k
{
180
13.8k
  int syntax_opt = ((cs_struct *)(uintptr_t)handle)->syntax;
181
182
13.8k
  if (syntax_opt & CS_OPT_SYNTAX_NOREGNAME) {
183
0
    return Sparc_LLVM_getRegisterName(reg, Sparc_NoRegAltName);
184
0
  }
185
13.8k
  return Sparc_LLVM_getRegisterName(reg, Sparc_RegNamesStateReg);
186
13.8k
}
187
188
void Sparc_get_insn_id(cs_struct *h, cs_insn *insn, unsigned int id)
189
31.8k
{
190
  // Not used by Sparc. Information is set after disassembly.
191
31.8k
}
192
193
static const char *const insn_name_maps[] = {
194
#include "SparcGenCSMappingInsnName.inc"
195
};
196
197
#ifndef CAPSTONE_DIET
198
static const name_map insn_alias_mnem_map[] = {
199
#include "SparcGenCSAliasMnemMap.inc"
200
  { SPARC_INS_ALIAS_CALL, "call" },
201
  { SPARC_INS_ALIAS_END, NULL },
202
};
203
#endif
204
205
static void insert_op(MCInst *MI, unsigned index, cs_sparc_op op)
206
149
{
207
149
  if (!detail_is_set(MI)) {
208
0
    return;
209
0
  }
210
149
  Sparc_check_safe_inc(MI);
211
212
149
  cs_sparc_op *ops = Sparc_get_detail(MI)->operands;
213
149
  int i = Sparc_get_detail(MI)->op_count;
214
149
  if (index == -1) {
215
114
    ops[i] = op;
216
114
    Sparc_inc_op_count(MI);
217
114
    return;
218
114
  }
219
70
  for (; i > 0 && i > index; --i) {
220
35
    ops[i] = ops[i - 1];
221
35
  }
222
35
  ops[index] = op;
223
35
  Sparc_inc_op_count(MI);
224
35
}
225
226
/// Inserts a register to the detail operands at @index.
227
/// Already present operands are moved.
228
/// If @index is -1 the operand is appended.
229
static void Sparc_insert_detail_op_reg_at(MCInst *MI, unsigned index, sparc_reg Reg,
230
         cs_ac_type access)
231
149
{
232
149
  if (!detail_is_set(MI))
233
0
    return;
234
235
149
  cs_sparc_op op = { 0 };
236
149
  op.type = SPARC_OP_REG;
237
149
  op.reg = Reg;
238
149
  op.access = access;
239
149
  insert_op(MI, index, op);
240
149
}
241
242
static void Sparc_correct_details(MCInst *MI)
243
31.8k
{
244
31.8k
  if (!detail_is_set(MI)) {
245
0
    return;
246
0
  }
247
31.8k
  switch (MCInst_getOpcode(MI)) {
248
31.1k
  default:
249
31.1k
    return;
250
31.1k
  case Sparc_LDSTUBri:
251
83
  case Sparc_LDSTUBrr:
252
525
  case Sparc_LDSTUBAri:
253
605
  case Sparc_LDSTUBArr:
254
    // The memory gets written back with ones
255
    // but there is not write back memory operand defined
256
    // (if even possible).
257
605
    Sparc_get_detail(MI)->operands[0].access = CS_AC_READ_WRITE;
258
605
    break;
259
10
  case Sparc_RDPSR:
260
10
    Sparc_insert_detail_op_reg_at(MI, 0, SPARC_REG_PSR, CS_AC_READ);
261
10
    break;
262
10
  case Sparc_PWRPSRri:
263
19
  case Sparc_PWRPSRrr:
264
32
  case Sparc_WRPSRri:
265
40
  case Sparc_WRPSRrr:
266
40
    Sparc_insert_detail_op_reg_at(MI, -1, SPARC_REG_PSR, CS_AC_WRITE);
267
40
    break;
268
7
  case Sparc_RDWIM:
269
7
    Sparc_insert_detail_op_reg_at(MI, 0, SPARC_REG_WIM, CS_AC_READ);
270
7
    break;
271
22
  case Sparc_WRWIMri:
272
28
  case Sparc_WRWIMrr:
273
28
    Sparc_insert_detail_op_reg_at(MI, -1, SPARC_REG_WIM, CS_AC_WRITE);
274
28
    break;
275
18
  case Sparc_RDTBR:
276
18
    Sparc_insert_detail_op_reg_at(MI, 0, SPARC_REG_TBR, CS_AC_READ);
277
18
    break;
278
16
  case Sparc_WRTBRri:
279
46
  case Sparc_WRTBRrr:
280
46
    Sparc_insert_detail_op_reg_at(MI, -1, SPARC_REG_TBR, CS_AC_WRITE);
281
46
    break;
282
31.8k
  }
283
31.8k
}
284
285
void Sparc_printer(MCInst *MI, SStream *O, void * /* MCRegisterInfo* */ info)
286
31.8k
{
287
31.8k
  MCRegisterInfo *MRI = (MCRegisterInfo *)info;
288
31.8k
  MI->MRI = MRI;
289
31.8k
  MI->flat_insn->usesAliasDetails = map_use_alias_details(MI);
290
31.8k
  Sparc_LLVM_printInst(MI, MI->address, "", O);
291
292
31.8k
#ifndef CAPSTONE_DIET
293
31.8k
  map_set_alias_id(MI, O, insn_alias_mnem_map,
294
31.8k
       ARR_SIZE(insn_alias_mnem_map));
295
31.8k
  Sparc_correct_details(MI);
296
31.8k
#endif
297
31.8k
}
298
299
const char *Sparc_insn_name(csh handle, unsigned int id)
300
31.8k
{
301
31.8k
#ifndef CAPSTONE_DIET
302
31.8k
  if (id < SPARC_INS_ALIAS_END && id > SPARC_INS_ALIAS_BEGIN) {
303
0
    if (id - SPARC_INS_ALIAS_BEGIN >= ARR_SIZE(insn_alias_mnem_map))
304
0
      return NULL;
305
306
0
    return insn_alias_mnem_map[id - SPARC_INS_ALIAS_BEGIN - 1].name;
307
0
  }
308
31.8k
  if (id >= SPARC_INS_ENDING)
309
0
    return NULL;
310
311
31.8k
  if (id < ARR_SIZE(insn_name_maps))
312
31.8k
    return insn_name_maps[id];
313
  // not found
314
0
  return NULL;
315
#else
316
  return NULL;
317
#endif
318
31.8k
}
319
320
#ifndef CAPSTONE_DIET
321
static const name_map group_name_maps[] = {
322
  { SPARC_GRP_INVALID, NULL },
323
324
  { SPARC_GRP_JUMP, "jump" },
325
  { SPARC_GRP_CALL, "call" },
326
  { SPARC_GRP_RET, "return" },
327
  { SPARC_GRP_INT, "int" },
328
  { SPARC_GRP_IRET, "iret" },
329
  { SPARC_GRP_PRIVILEGE, "privilege" },
330
  { SPARC_GRP_BRANCH_RELATIVE, "branch_relative" },
331
332
// architecture-specific groups
333
#include "SparcGenCSFeatureName.inc"
334
};
335
#endif
336
337
const char *Sparc_group_name(csh handle, unsigned int id)
338
87.8k
{
339
87.8k
#ifndef CAPSTONE_DIET
340
87.8k
  return id2name(group_name_maps, ARR_SIZE(group_name_maps), id);
341
#else
342
  return NULL;
343
#endif
344
87.8k
}
345
346
static const map_insn_ops insn_operands[] = {
347
#include "SparcGenCSMappingInsnOp.inc"
348
};
349
350
void Sparc_set_detail_op_imm(MCInst *MI, unsigned OpNum, sparc_op_type ImmType,
351
           int64_t Imm)
352
21.3k
{
353
21.3k
  if (!detail_is_set(MI))
354
0
    return;
355
21.3k
  CS_ASSERT_RET((map_get_op_type(MI, OpNum) & ~CS_OP_MEM) == CS_OP_IMM);
356
21.3k
  CS_ASSERT_RET(ImmType == SPARC_OP_IMM);
357
358
21.3k
  Sparc_get_detail_op(MI, 0)->type = ImmType;
359
21.3k
  Sparc_get_detail_op(MI, 0)->imm = Imm;
360
21.3k
  Sparc_get_detail_op(MI, 0)->access = map_get_op_access(MI, OpNum);
361
21.3k
  Sparc_inc_op_count(MI);
362
21.3k
}
363
364
void Sparc_set_detail_op_reg(MCInst *MI, unsigned OpNum, sparc_reg Reg)
365
23.8k
{
366
23.8k
  if (!detail_is_set(MI))
367
0
    return;
368
23.8k
  CS_ASSERT_RET((map_get_op_type(MI, OpNum) & ~CS_OP_MEM) == CS_OP_REG);
369
370
23.8k
  switch (Reg) {
371
21.0k
  default:
372
21.0k
    Sparc_get_detail_op(MI, 0)->type = SPARC_OP_REG;
373
21.0k
    Sparc_get_detail_op(MI, 0)->reg = Reg;
374
21.0k
    Sparc_get_detail_op(MI, 0)->access = map_get_op_access(MI, OpNum);
375
21.0k
    Sparc_inc_op_count(MI);
376
21.0k
    return;
377
  // The LLVM definition is inconsistent with the cc fields.
378
  // Sometimes they are encoded as register, sometimes not at all.
379
  // For Capstone they are always saved in the cc_field field for now.
380
0
  case SPARC_REG_ICC:
381
0
    Sparc_get_detail(MI)->cc_field = SPARC_CC_FIELD_ICC;
382
0
    break;
383
1.00k
  case SPARC_REG_FCC0:
384
1.00k
    Sparc_get_detail(MI)->cc_field = SPARC_CC_FIELD_FCC0;
385
1.00k
    break;
386
549
  case SPARC_REG_FCC1:
387
549
    Sparc_get_detail(MI)->cc_field = SPARC_CC_FIELD_FCC1;
388
549
    break;
389
845
  case SPARC_REG_FCC2:
390
845
    Sparc_get_detail(MI)->cc_field = SPARC_CC_FIELD_FCC2;
391
845
    break;
392
404
  case SPARC_REG_FCC3:
393
404
    Sparc_get_detail(MI)->cc_field = SPARC_CC_FIELD_FCC3;
394
404
    break;
395
23.8k
  }
396
23.8k
}
397
398
static inline bool is_single_reg_mem_case(MCInst *MI, unsigned OpNo)
399
12.4k
{
400
12.4k
  if (map_get_op_type(MI, OpNo) != CS_OP_MEM_REG) {
401
3.47k
    return false;
402
3.47k
  }
403
8.99k
  if (MI->size == 1) {
404
0
    return true;
405
8.99k
  } else if (MI->size > OpNo + 1 && Sparc_get_detail(MI)->operands[0].type != SPARC_OP_MEM) {
406
    // Next operand is not a memory operand (disponent or index reg).
407
2.23k
    return !(map_get_op_type(MI, OpNo + 1) & SPARC_OP_MEM);
408
2.23k
  }
409
6.76k
  return false;
410
8.99k
}
411
412
void Sparc_add_cs_detail_0(MCInst *MI, sparc_op_group op_group, unsigned OpNo)
413
72.4k
{
414
72.4k
  if (!detail_is_set(MI) || !map_fill_detail_ops(MI))
415
0
    return;
416
417
72.4k
  cs_op_type op_type = map_get_op_type(MI, OpNo);
418
419
72.4k
  switch (op_group) {
420
0
  default:
421
0
  case Sparc_OP_GROUP_GetPCX:
422
0
    printf("Operand group %d not handled!\n", op_group);
423
0
    return;
424
57.7k
  case Sparc_OP_GROUP_Operand:
425
57.7k
    if (op_type & CS_OP_MEM) {
426
12.4k
      if (is_single_reg_mem_case(MI, OpNo)) {
427
583
        Sparc_get_detail_op(MI, 0)->type = SPARC_OP_MEM;
428
583
        Sparc_get_detail_op(MI, 0)->mem.base =
429
583
          MCInst_getOpVal(MI, OpNo);
430
583
        Sparc_get_detail_op(MI, 0)->access =
431
583
          map_get_op_access(MI, OpNo);
432
583
        Sparc_inc_op_count(MI);
433
583
      }
434
12.4k
      break;
435
12.4k
    }
436
45.2k
    if (op_type == CS_OP_IMM) {
437
21.3k
      Sparc_set_detail_op_imm(MI, OpNo, SPARC_OP_IMM,
438
21.3k
            MCInst_getOpVal(MI, OpNo));
439
23.8k
    } else if (op_type == CS_OP_REG) {
440
23.8k
      Sparc_set_detail_op_reg(MI, OpNo,
441
23.8k
            MCInst_getOpVal(MI, OpNo));
442
23.8k
    } else {
443
0
      CS_ASSERT_RET(0 && "Op type not handled.");
444
0
    }
445
45.2k
    Sparc_get_detail_op(MI, 0)->access =
446
45.2k
      map_get_op_access(MI, OpNo);
447
45.2k
    break;
448
5.47k
  case Sparc_OP_GROUP_CCOperand: {
449
    // Handled in Sparc_add_bit_details().
450
5.47k
    break;
451
57.7k
  }
452
6.68k
  case Sparc_OP_GROUP_MemOperand: {
453
6.68k
    MCOperand *Op1 = MCInst_getOperand(MI, (OpNo));
454
6.68k
    MCOperand *Op2 = MCInst_getOperand(MI, (OpNo + 1));
455
6.68k
    if (!MCOperand_isReg(Op1) ||
456
6.68k
        MCOperand_getReg(Op1) == Sparc_G0) {
457
      // Ignored
458
392
      return;
459
392
    }
460
6.29k
    Sparc_get_detail_op(MI, 0)->type = SPARC_OP_MEM;
461
6.29k
    Sparc_get_detail_op(MI, 0)->access =
462
6.29k
      map_get_op_access(MI, OpNo);
463
6.29k
    Sparc_get_detail_op(MI, 0)->mem.base = MCOperand_getReg(Op1);
464
465
6.29k
    if (MCOperand_isReg(Op2) && MCOperand_getReg(Op2) != Sparc_G0) {
466
2.26k
      Sparc_get_detail_op(MI, 0)->mem.index =
467
2.26k
        MCOperand_getReg(Op2);
468
4.02k
    } else if (MCOperand_isImm(Op2) && MCOperand_getImm(Op2) != 0) {
469
3.35k
      Sparc_get_detail_op(MI, 0)->mem.disp =
470
3.35k
        MCOperand_getImm(Op2);
471
3.35k
    }
472
6.29k
    Sparc_inc_op_count(MI);
473
6.29k
    break;
474
6.68k
  }
475
2.54k
  case Sparc_OP_GROUP_ASITag:
476
2.54k
    Sparc_get_detail_op(MI, 0)->type = SPARC_OP_ASI;
477
2.54k
    Sparc_get_detail_op(MI, 0)->access =
478
2.54k
      map_get_op_access(MI, OpNo);
479
2.54k
    Sparc_get_detail_op(MI, 0)->asi =
480
2.54k
      MCOperand_getImm(MCInst_getOperand(MI, OpNo));
481
2.54k
    Sparc_inc_op_count(MI);
482
2.54k
    break;
483
43
  case Sparc_OP_GROUP_MembarTag:
484
43
    Sparc_get_detail_op(MI, 0)->type = SPARC_OP_MEMBAR_TAG;
485
43
    Sparc_get_detail_op(MI, 0)->access =
486
43
      map_get_op_access(MI, OpNo);
487
43
    Sparc_get_detail_op(MI, 0)->membar_tag =
488
43
      MCOperand_getImm(MCInst_getOperand(MI, OpNo));
489
43
    Sparc_inc_op_count(MI);
490
43
    break;
491
72.4k
  }
492
72.4k
}
493
494
#endif