Coverage Report

Created: 2025-07-01 07:03

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