Coverage Report

Created: 2025-07-18 06:43

/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
44.8k
{
17
44.8k
  if (!detail_is_set(MI)) {
18
0
    return;
19
0
  }
20
44.8k
  memset(get_detail(MI), 0, offsetof(cs_detail, sparc) + sizeof(cs_sparc));
21
44.8k
  Sparc_get_detail(MI)->cc = SPARC_CC_UNDEF;
22
44.8k
  Sparc_get_detail(MI)->cc_field = SPARC_CC_FIELD_NONE;
23
44.8k
}
24
25
const insn_map sparc_insns[] = {
26
#include "SparcGenCSMappingInsn.inc"
27
};
28
29
void Sparc_set_instr_map_data(MCInst *MI)
30
44.8k
{
31
44.8k
  map_cs_id(MI, sparc_insns, ARR_SIZE(sparc_insns));
32
44.8k
  map_implicit_reads(MI, sparc_insns);
33
44.8k
  map_implicit_writes(MI, sparc_insns);
34
44.8k
  map_groups(MI, sparc_insns);
35
44.8k
  const sparc_suppl_info *suppl_info =
36
44.8k
    map_get_suppl_info(MI, sparc_insns);
37
44.8k
  if (suppl_info) {
38
44.8k
    Sparc_get_detail(MI)->format = suppl_info->form;
39
44.8k
  }
40
44.8k
}
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
44.8k
{
47
44.8k
  if (!Bytes || BytesLen < 4 || !detail_is_set(MI)) {
48
652
    return;
49
652
  }
50
44.2k
  uint32_t insn = readBytes32(MI, Bytes);
51
52
  // CC field
53
44.2k
  cs_sparc *detail = Sparc_get_detail(MI);
54
44.2k
  switch (detail->format) {
55
32.0k
  default:
56
32.0k
    break;
57
32.0k
  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
4.96k
    size_t op2 = get_insn_field_r(insn, 22, 24);
65
4.96k
    detail->cc_field = op2 == 6 ? SPARC_CC_FIELD_FCC0 : SPARC_CC_FIELD_ICC;
66
4.96k
    break;
67
0
  }
68
5.18k
  case SPARC_INSN_FORM_F2_3:
69
5.18k
    detail->cc_field = 0x4 | get_insn_field_r(insn, 20, 21);
70
5.18k
    break;
71
560
  case SPARC_INSN_FORM_TRAPSP:
72
560
    detail->cc_field = 0x4 | get_insn_field_r(insn, 11, 12);
73
560
    break;
74
721
  case SPARC_INSN_FORM_F4_1:
75
905
  case SPARC_INSN_FORM_F4_2:
76
905
    detail->cc_field = get_insn_field_r(insn, 11, 12);
77
905
    detail->cc_field |= get_insn_field_r(insn, 18, 18) << 2;
78
905
    break;
79
587
  case SPARC_INSN_FORM_F4_3:
80
587
    detail->cc_field = get_insn_field_r(insn, 11, 13);
81
587
    break;
82
44.2k
  }
83
84
  // Condition codes
85
44.2k
  switch (detail->format) {
86
25.3k
  default:
87
25.3k
    break;
88
25.3k
  case SPARC_INSN_FORM_F2_1:
89
9.64k
  case SPARC_INSN_FORM_F2_2:
90
14.8k
  case SPARC_INSN_FORM_F2_3:
91
15.3k
  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
15.3k
    sparc_cc cc = get_insn_field_r(insn, 25, 28);
96
15.3k
    if (MCInst_getOpcode(MI) == Sparc_CBCOND ||
97
15.3k
        MCInst_getOpcode(MI) == Sparc_CBCONDA) {
98
2.50k
      cc += SPARC_CC_CPCC_BEGIN;
99
2.50k
    }
100
15.3k
    detail->cc = cc;
101
15.3k
    break;
102
14.8k
  }
103
721
  case SPARC_INSN_FORM_F4_1:
104
905
  case SPARC_INSN_FORM_F4_2:
105
1.49k
  case SPARC_INSN_FORM_F4_3: {
106
1.49k
    sparc_cc cc = get_insn_field_r(insn, 14, 17);
107
1.49k
    detail->cc = cc;
108
1.49k
    break;
109
905
  }
110
1.59k
  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.59k
    sparc_cc rcc = get_insn_field_r(insn, 25, 27);
115
1.59k
    detail->cc = rcc + SPARC_CC_REG_BEGIN;
116
1.59k
    break;
117
905
  }
118
309
  case SPARC_INSN_FORM_F4_4R:
119
412
  case SPARC_INSN_FORM_F4_4I: {
120
412
    sparc_cc rcc = get_insn_field_r(insn, 10, 12);
121
412
    detail->cc = rcc + SPARC_CC_REG_BEGIN;
122
412
    break;
123
309
  }
124
44.2k
  }
125
44.2k
  switch (detail->cc_field) {
126
32.9k
  default:
127
39.5k
  case SPARC_CC_FIELD_ICC:
128
41.7k
  case SPARC_CC_FIELD_XCC:
129
41.7k
    break;
130
1.94k
  case SPARC_CC_FIELD_FCC0:
131
2.32k
  case SPARC_CC_FIELD_FCC1:
132
2.37k
  case SPARC_CC_FIELD_FCC2:
133
2.43k
  case SPARC_CC_FIELD_FCC3:
134
2.43k
    detail->cc += SPARC_CC_FCC_BEGIN;
135
2.43k
    break;
136
44.2k
  }
137
138
  // Hints
139
44.2k
  switch (detail->format) {
140
32.4k
  default:
141
32.4k
    break;
142
32.4k
  case SPARC_INSN_FORM_F2_2:
143
4.96k
    detail->hint = get_insn_field_r(insn, 29, 29);
144
4.96k
    break;
145
5.18k
  case SPARC_INSN_FORM_F2_3:
146
6.78k
  case SPARC_INSN_FORM_F2_4:
147
6.78k
    detail->hint = get_insn_field_r(insn, 29, 29);
148
6.78k
    detail->hint |=
149
6.78k
      get_insn_field_r(insn, 19, 19) == 0 ? SPARC_HINT_PN :
150
6.78k
                    SPARC_HINT_PT;
151
6.78k
    break;
152
44.2k
  }
153
44.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
44.8k
{
159
44.8k
  Sparc_init_cs_detail(instr);
160
44.8k
  bool Result = Sparc_LLVM_getInstruction(handle, code, code_len, instr,
161
44.8k
            size, address,
162
44.8k
            info) != MCDisassembler_Fail;
163
44.8k
  Sparc_set_instr_map_data(instr);
164
165
44.8k
  Sparc_add_bit_details(instr, code, code_len);
166
44.8k
  return Result;
167
44.8k
}
168
169
void Sparc_init_mri(MCRegisterInfo *MRI)
170
1.68k
{
171
1.68k
  MCRegisterInfo_InitMCRegisterInfo(
172
1.68k
    MRI, SparcRegDesc, sizeof(SparcRegDesc), 0, 0,
173
1.68k
    SparcMCRegisterClasses, ARR_SIZE(SparcMCRegisterClasses), 0, 0,
174
1.68k
    SparcRegDiffLists, 0, SparcSubRegIdxLists,
175
1.68k
    ARR_SIZE(SparcSubRegIdxLists), 0);
176
1.68k
}
177
178
const char *Sparc_reg_name(csh handle, unsigned int reg)
179
20.3k
{
180
20.3k
  int syntax_opt = ((cs_struct *)(uintptr_t)handle)->syntax;
181
182
20.3k
  if (syntax_opt & CS_OPT_SYNTAX_NOREGNAME) {
183
0
    return Sparc_LLVM_getRegisterName(reg, Sparc_NoRegAltName);
184
0
  }
185
20.3k
  return Sparc_LLVM_getRegisterName(reg, Sparc_RegNamesStateReg);
186
20.3k
}
187
188
void Sparc_get_insn_id(cs_struct *h, cs_insn *insn, unsigned int id)
189
43.8k
{
190
  // Not used by Sparc. Information is set after disassembly.
191
43.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
411
{
207
411
  if (!detail_is_set(MI)) {
208
0
    return;
209
0
  }
210
411
  Sparc_check_safe_inc(MI);
211
212
411
  cs_sparc_op *ops = Sparc_get_detail(MI)->operands;
213
411
  int i = Sparc_get_detail(MI)->op_count;
214
411
  if (index == -1) {
215
364
    ops[i] = op;
216
364
    Sparc_inc_op_count(MI);
217
364
    return;
218
364
  }
219
94
  for (; i > 0 && i > index; --i) {
220
47
    ops[i] = ops[i - 1];
221
47
  }
222
47
  ops[index] = op;
223
47
  Sparc_inc_op_count(MI);
224
47
}
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
411
{
232
411
  if (!detail_is_set(MI))
233
0
    return;
234
235
411
  cs_sparc_op op = { 0 };
236
411
  op.type = SPARC_OP_REG;
237
411
  op.reg = Reg;
238
411
  op.access = access;
239
411
  insert_op(MI, index, op);
240
411
}
241
242
static void Sparc_correct_details(MCInst *MI)
243
43.8k
{
244
43.8k
  if (!detail_is_set(MI)) {
245
0
    return;
246
0
  }
247
43.8k
  switch (MCInst_getOpcode(MI)) {
248
42.5k
  default:
249
42.5k
    return;
250
42.5k
  case Sparc_LDSTUBri:
251
475
  case Sparc_LDSTUBrr:
252
832
  case Sparc_LDSTUBAri:
253
955
  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
955
    Sparc_get_detail(MI)->operands[0].access = CS_AC_READ_WRITE;
258
955
    break;
259
18
  case Sparc_RDPSR:
260
18
    Sparc_insert_detail_op_reg_at(MI, 0, SPARC_REG_PSR, CS_AC_READ);
261
18
    break;
262
18
  case Sparc_PWRPSRri:
263
37
  case Sparc_PWRPSRrr:
264
119
  case Sparc_WRPSRri:
265
190
  case Sparc_WRPSRrr:
266
190
    Sparc_insert_detail_op_reg_at(MI, -1, SPARC_REG_PSR, CS_AC_WRITE);
267
190
    break;
268
10
  case Sparc_RDWIM:
269
10
    Sparc_insert_detail_op_reg_at(MI, 0, SPARC_REG_WIM, CS_AC_READ);
270
10
    break;
271
35
  case Sparc_WRWIMri:
272
53
  case Sparc_WRWIMrr:
273
53
    Sparc_insert_detail_op_reg_at(MI, -1, SPARC_REG_WIM, CS_AC_WRITE);
274
53
    break;
275
19
  case Sparc_RDTBR:
276
19
    Sparc_insert_detail_op_reg_at(MI, 0, SPARC_REG_TBR, CS_AC_READ);
277
19
    break;
278
79
  case Sparc_WRTBRri:
279
121
  case Sparc_WRTBRrr:
280
121
    Sparc_insert_detail_op_reg_at(MI, -1, SPARC_REG_TBR, CS_AC_WRITE);
281
121
    break;
282
43.8k
  }
283
43.8k
}
284
285
void Sparc_printer(MCInst *MI, SStream *O, void * /* MCRegisterInfo* */ info)
286
43.8k
{
287
43.8k
  MCRegisterInfo *MRI = (MCRegisterInfo *)info;
288
43.8k
  MI->MRI = MRI;
289
43.8k
  MI->flat_insn->usesAliasDetails = map_use_alias_details(MI);
290
43.8k
  Sparc_LLVM_printInst(MI, MI->address, "", O);
291
292
43.8k
#ifndef CAPSTONE_DIET
293
43.8k
  map_set_alias_id(MI, O, insn_alias_mnem_map,
294
43.8k
       ARR_SIZE(insn_alias_mnem_map));
295
43.8k
  Sparc_correct_details(MI);
296
43.8k
#endif
297
43.8k
}
298
299
const char *Sparc_insn_name(csh handle, unsigned int id)
300
43.8k
{
301
43.8k
#ifndef CAPSTONE_DIET
302
43.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
43.8k
  if (id >= SPARC_INS_ENDING)
309
0
    return NULL;
310
311
43.8k
  if (id < ARR_SIZE(insn_name_maps))
312
43.8k
    return insn_name_maps[id];
313
  // not found
314
0
  return NULL;
315
#else
316
  return NULL;
317
#endif
318
43.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
101k
{
339
101k
#ifndef CAPSTONE_DIET
340
101k
  return id2name(group_name_maps, ARR_SIZE(group_name_maps), id);
341
#else
342
  return NULL;
343
#endif
344
101k
}
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
25.9k
{
353
25.9k
  if (!detail_is_set(MI))
354
0
    return;
355
25.9k
  CS_ASSERT_RET((map_get_op_type(MI, OpNum) & ~CS_OP_MEM) == CS_OP_IMM);
356
25.9k
  CS_ASSERT_RET(ImmType == SPARC_OP_IMM);
357
358
25.9k
  Sparc_get_detail_op(MI, 0)->type = ImmType;
359
25.9k
  Sparc_get_detail_op(MI, 0)->imm = Imm;
360
25.9k
  Sparc_get_detail_op(MI, 0)->access = map_get_op_access(MI, OpNum);
361
25.9k
  Sparc_inc_op_count(MI);
362
25.9k
}
363
364
void Sparc_set_detail_op_reg(MCInst *MI, unsigned OpNum, sparc_reg Reg)
365
34.3k
{
366
34.3k
  if (!detail_is_set(MI))
367
0
    return;
368
34.3k
  CS_ASSERT_RET((map_get_op_type(MI, OpNum) & ~CS_OP_MEM) == CS_OP_REG);
369
370
34.3k
  switch (Reg) {
371
31.4k
  default:
372
31.4k
    Sparc_get_detail_op(MI, 0)->type = SPARC_OP_REG;
373
31.4k
    Sparc_get_detail_op(MI, 0)->reg = Reg;
374
31.4k
    Sparc_get_detail_op(MI, 0)->access = map_get_op_access(MI, OpNum);
375
31.4k
    Sparc_inc_op_count(MI);
376
31.4k
    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
273
  case SPARC_REG_FCC0:
384
273
    Sparc_get_detail(MI)->cc_field = SPARC_CC_FIELD_FCC0;
385
273
    break;
386
710
  case SPARC_REG_FCC1:
387
710
    Sparc_get_detail(MI)->cc_field = SPARC_CC_FIELD_FCC1;
388
710
    break;
389
1.11k
  case SPARC_REG_FCC2:
390
1.11k
    Sparc_get_detail(MI)->cc_field = SPARC_CC_FIELD_FCC2;
391
1.11k
    break;
392
710
  case SPARC_REG_FCC3:
393
710
    Sparc_get_detail(MI)->cc_field = SPARC_CC_FIELD_FCC3;
394
710
    break;
395
34.3k
  }
396
34.3k
}
397
398
static inline bool is_single_reg_mem_case(MCInst *MI, unsigned OpNo)
399
22.1k
{
400
22.1k
  if (map_get_op_type(MI, OpNo) != CS_OP_MEM_REG) {
401
6.64k
    return false;
402
6.64k
  }
403
15.4k
  if (MI->size == 1) {
404
0
    return true;
405
15.4k
  } 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
7.49k
    return !(map_get_op_type(MI, OpNo + 1) & SPARC_OP_MEM);
408
7.49k
  }
409
7.97k
  return false;
410
15.4k
}
411
412
void Sparc_add_cs_detail_0(MCInst *MI, sparc_op_group op_group, unsigned OpNo)
413
108k
{
414
108k
  if (!detail_is_set(MI) || !map_fill_detail_ops(MI))
415
0
    return;
416
417
108k
  cs_op_type op_type = map_get_op_type(MI, OpNo);
418
419
108k
  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
82.3k
  case Sparc_OP_GROUP_Operand:
425
82.3k
    if (op_type & CS_OP_MEM) {
426
22.1k
      if (is_single_reg_mem_case(MI, OpNo)) {
427
2.14k
        Sparc_get_detail_op(MI, 0)->type = SPARC_OP_MEM;
428
2.14k
        Sparc_get_detail_op(MI, 0)->mem.base =
429
2.14k
          MCInst_getOpVal(MI, OpNo);
430
2.14k
        Sparc_get_detail_op(MI, 0)->access =
431
2.14k
          map_get_op_access(MI, OpNo);
432
2.14k
        Sparc_inc_op_count(MI);
433
2.14k
      }
434
22.1k
      break;
435
22.1k
    }
436
60.2k
    if (op_type == CS_OP_IMM) {
437
25.9k
      Sparc_set_detail_op_imm(MI, OpNo, SPARC_OP_IMM,
438
25.9k
            MCInst_getOpVal(MI, OpNo));
439
34.3k
    } else if (op_type == CS_OP_REG) {
440
34.3k
      Sparc_set_detail_op_reg(MI, OpNo,
441
34.3k
            MCInst_getOpVal(MI, OpNo));
442
34.3k
    } else {
443
0
      CS_ASSERT_RET(0 && "Op type not handled.");
444
0
    }
445
60.2k
    Sparc_get_detail_op(MI, 0)->access =
446
60.2k
      map_get_op_access(MI, OpNo);
447
60.2k
    break;
448
10.4k
  case Sparc_OP_GROUP_CCOperand: {
449
    // Handled in Sparc_add_bit_details().
450
10.4k
    break;
451
82.3k
  }
452
11.7k
  case Sparc_OP_GROUP_MemOperand: {
453
11.7k
    MCOperand *Op1 = MCInst_getOperand(MI, (OpNo));
454
11.7k
    MCOperand *Op2 = MCInst_getOperand(MI, (OpNo + 1));
455
11.7k
    if (!MCOperand_isReg(Op1) ||
456
11.7k
        MCOperand_getReg(Op1) == Sparc_G0) {
457
      // Ignored
458
423
      return;
459
423
    }
460
11.3k
    Sparc_get_detail_op(MI, 0)->type = SPARC_OP_MEM;
461
11.3k
    Sparc_get_detail_op(MI, 0)->access =
462
11.3k
      map_get_op_access(MI, OpNo);
463
11.3k
    Sparc_get_detail_op(MI, 0)->mem.base = MCOperand_getReg(Op1);
464
465
11.3k
    if (MCOperand_isReg(Op2) && MCOperand_getReg(Op2) != Sparc_G0) {
466
3.54k
      Sparc_get_detail_op(MI, 0)->mem.index =
467
3.54k
        MCOperand_getReg(Op2);
468
7.78k
    } else if (MCOperand_isImm(Op2) && MCOperand_getImm(Op2) != 0) {
469
6.42k
      Sparc_get_detail_op(MI, 0)->mem.disp =
470
6.42k
        MCOperand_getImm(Op2);
471
6.42k
    }
472
11.3k
    Sparc_inc_op_count(MI);
473
11.3k
    break;
474
11.7k
  }
475
4.10k
  case Sparc_OP_GROUP_ASITag:
476
4.10k
    Sparc_get_detail_op(MI, 0)->type = SPARC_OP_ASI;
477
4.10k
    Sparc_get_detail_op(MI, 0)->access =
478
4.10k
      map_get_op_access(MI, OpNo);
479
4.10k
    Sparc_get_detail_op(MI, 0)->asi =
480
4.10k
      MCOperand_getImm(MCInst_getOperand(MI, OpNo));
481
4.10k
    Sparc_inc_op_count(MI);
482
4.10k
    break;
483
259
  case Sparc_OP_GROUP_MembarTag:
484
259
    Sparc_get_detail_op(MI, 0)->type = SPARC_OP_MEMBAR_TAG;
485
259
    Sparc_get_detail_op(MI, 0)->access =
486
259
      map_get_op_access(MI, OpNo);
487
259
    Sparc_get_detail_op(MI, 0)->membar_tag =
488
259
      MCOperand_getImm(MCInst_getOperand(MI, OpNo));
489
259
    Sparc_inc_op_count(MI);
490
259
    break;
491
108k
  }
492
108k
}
493
494
#endif