Coverage Report

Created: 2026-12-01 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/capstonenext/arch/PowerPC/PPCMapping.c
Line
Count
Source
1
/* Capstone Disassembly Engine */
2
/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2015 */
3
4
#include "capstone/capstone.h"
5
#ifdef CAPSTONE_HAS_POWERPC
6
7
#include <stdio.h> // debug
8
#include <string.h>
9
10
#include "../../cs_simple_types.h"
11
#include "../../Mapping.h"
12
#include "../../MCDisassembler.h"
13
#include "../../utils.h"
14
15
#include "PPCLinkage.h"
16
#include "PPCMapping.h"
17
#include "PPCMCTargetDesc.h"
18
19
static int P7InheritableFeatures[] = {
20
  PPC_DirectivePwr7,     PPC_FeatureAltivec,
21
  PPC_FeatureVSX,        PPC_FeatureMFOCRF,
22
  PPC_FeatureFCPSGN,     PPC_FeatureFSqrt,
23
  PPC_FeatureFRE,        PPC_FeatureFRES,
24
  PPC_FeatureFRSQRTE,    PPC_FeatureFRSQRTES,
25
  PPC_FeatureRecipPrec,  PPC_FeatureSTFIWX,
26
  PPC_FeatureLFIWAX,     PPC_FeatureFPRND,
27
  PPC_FeatureFPCVT,      PPC_FeatureISEL,
28
  PPC_FeaturePOPCNTD,    PPC_FeatureCMPB,
29
  PPC_FeatureLDBRX,      PPC_Feature64Bit,
30
  PPC_FeatureBPERMD,     PPC_FeatureExtDiv,
31
  PPC_FeatureMFTB,       PPC_DeprecatedDST,
32
  PPC_FeatureTwoConstNR, PPC_FeatureUnalignedFloats,
33
  PPC_FeatureISA2_06,    INT_MAX
34
};
35
36
static int *P7Features[] = { P7InheritableFeatures };
37
38
// Power8
39
static int P8AdditionalFeatures[] = { PPC_DirectivePwr8,
40
              PPC_FeatureP8Altivec,
41
              PPC_FeatureP8Vector,
42
              PPC_FeatureP8Crypto,
43
              PPC_FeatureHTM,
44
              PPC_FeatureDirectMove,
45
              PPC_FeatureICBT,
46
              PPC_FeaturePartwordAtomic,
47
              PPC_FeatureQuadwordAtomic,
48
              PPC_FeaturePredictableSelectIsExpensive,
49
              PPC_FeatureISA2_07,
50
              PPC_FeatureCRBits,
51
              INT_MAX };
52
53
static int P8SpecificFeatures[] = { PPC_FeatureAddiLoadFusion,
54
            PPC_FeatureAddisLoadFusion, INT_MAX };
55
56
static int *P8Features[] = { P7InheritableFeatures, P8AdditionalFeatures,
57
           P8SpecificFeatures };
58
59
static int P9AdditionalFeatures[] = { PPC_DirectivePwr9,
60
              PPC_FeatureP9Altivec,
61
              PPC_FeatureP9Vector,
62
              PPC_FeaturePPCPreRASched,
63
              PPC_FeaturePPCPostRASched,
64
              PPC_FeatureISA3_0,
65
              PPC_FeaturePredictableSelectIsExpensive,
66
              INT_MAX };
67
68
static int P9SpecificFeatures[] = { PPC_FeatureVectorsUseTwoUnits, INT_MAX };
69
70
static int *P9Features[] = { P7InheritableFeatures, P8AdditionalFeatures,
71
           P9AdditionalFeatures, P9SpecificFeatures };
72
73
static int P10AdditionalFeatures[] = { PPC_FeatureStoreFusion,
74
               PPC_FeatureAddLogicalFusion,
75
               PPC_FeatureLogicalAddFusion,
76
               PPC_FeatureLogicalFusion,
77
               PPC_FeatureArithAddFusion,
78
               PPC_FeatureSha3Fusion,
79
               PPC_DirectivePwr10,
80
               PPC_FeatureISA3_1,
81
               PPC_FeaturePrefixInstrs,
82
               PPC_FeaturePCRelativeMemops,
83
               PPC_FeatureP10Vector,
84
               PPC_FeatureMMA,
85
               PPC_FeaturePairedVectorMemops,
86
               PPC_FeatureFastMFLR,
87
               INT_MAX };
88
89
static int *P10Features[] = { P7InheritableFeatures, P8AdditionalFeatures,
90
            P9AdditionalFeatures, P10AdditionalFeatures };
91
92
static int FutureAdditionalFeatures[] = { PPC_FeatureISAFuture, INT_MAX };
93
94
static int *FutureFeatures[] = { P7InheritableFeatures, P8AdditionalFeatures,
95
         P9AdditionalFeatures, P10AdditionalFeatures,
96
         FutureAdditionalFeatures };
97
98
static inline bool is_feature_of(int feature, int **feature_set, int set_size)
99
0
{
100
0
  for (size_t i = 0; i < set_size; ++i) {
101
0
    size_t j = 0;
102
0
    while (feature_set[i][j] != INT_MAX) {
103
0
      if (feature == feature_set[i][j]) {
104
0
        return true;
105
0
      }
106
0
      ++j;
107
0
    }
108
0
  }
109
0
  return false;
110
0
}
111
112
#define GET_REGINFO_MC_DESC
113
#include "PPCGenRegisterInfo.inc"
114
115
void PPC_init_mri(MCRegisterInfo *MRI)
116
2.78k
{
117
2.78k
  MCRegisterInfo_InitMCRegisterInfo(MRI, PPCRegDesc, PPC_REG_ENDING, 0, 0,
118
2.78k
            PPCMCRegisterClasses,
119
2.78k
            ARR_SIZE(PPCMCRegisterClasses), 0, 0,
120
2.78k
            PPCRegDiffLists, 0, PPCSubRegIdxLists,
121
2.78k
            ARR_SIZE(PPCSubRegIdxLists),
122
2.78k
            PPCRegEncodingTable);
123
2.78k
}
124
125
const char *PPC_reg_name(csh handle, unsigned int reg)
126
45.6k
{
127
45.6k
  if (reg > PPC_REG_INVALID && reg < PPC_REG_ENDING)
128
45.6k
    return PPC_LLVM_getRegisterName(reg);
129
0
  return NULL;
130
45.6k
}
131
132
// given internal insn id, return public instruction info
133
void PPC_get_insn_id(cs_struct *h, cs_insn *insn, unsigned int id)
134
76.8k
{
135
  // We do this after Instruction disassembly.
136
76.8k
}
137
138
#ifndef CAPSTONE_DIET
139
140
static const char *const insn_name_maps[] = {
141
#include "PPCGenCSMappingInsnName.inc"
142
};
143
144
static const name_map insn_alias_mnem_map[] = {
145
#include "PPCGenCSAliasMnemMap.inc"
146
  { PPC_INS_ALIAS_SLWI, "slwi" },
147
  { PPC_INS_ALIAS_SRWI, "srwi" },
148
  { PPC_INS_ALIAS_SLDI, "sldi" },
149
  { PPC_INS_ALIAS_END, NULL },
150
};
151
152
#endif
153
154
const char *PPC_insn_name(csh handle, unsigned int id)
155
76.8k
{
156
76.8k
#ifndef CAPSTONE_DIET
157
76.8k
  if (id < PPC_INS_ALIAS_END && id > PPC_INS_ALIAS_BEGIN) {
158
0
    if (id - PPC_INS_ALIAS_BEGIN >= ARR_SIZE(insn_alias_mnem_map))
159
0
      return NULL;
160
161
0
    return insn_alias_mnem_map[id - PPC_INS_ALIAS_BEGIN - 1].name;
162
0
  }
163
76.8k
  if (id >= PPC_INS_ENDING)
164
0
    return NULL;
165
166
76.8k
  return insn_name_maps[id];
167
#else
168
  return NULL;
169
#endif
170
76.8k
}
171
172
#ifndef CAPSTONE_DIET
173
static const name_map group_name_maps[] = {
174
  // generic groups
175
  { PPC_GRP_INVALID, NULL },
176
  { PPC_GRP_JUMP, "jump" },
177
  { PPC_GRP_CALL, "call" },
178
  { PPC_GRP_RET, "ret" },
179
  { PPC_GRP_INT, "int" },
180
  { PPC_GRP_IRET, "iret" },
181
  { PPC_GRP_PRIVILEGE, "privilege" },
182
  { PPC_GRP_BRANCH_RELATIVE, "branch_relative" },
183
184
// architecture-specific groups
185
#include "PPCGenCSFeatureName.inc"
186
};
187
#endif
188
189
const char *PPC_group_name(csh handle, unsigned int id)
190
31.7k
{
191
31.7k
#ifndef CAPSTONE_DIET
192
31.7k
  return id2name(group_name_maps, ARR_SIZE(group_name_maps), id);
193
#else
194
  return NULL;
195
#endif
196
31.7k
}
197
198
const insn_map ppc_insns[] = {
199
#include "PPCGenCSMappingInsn.inc"
200
};
201
202
void PPC_check_updates_cr0(MCInst *MI)
203
77.9k
{
204
77.9k
#ifndef CAPSTONE_DIET
205
77.9k
  if (!detail_is_set(MI))
206
0
    return;
207
77.9k
  cs_detail *detail = get_detail(MI);
208
95.4k
  for (int i = 0; i < detail->regs_write_count; ++i) {
209
21.1k
    if (detail->regs_write[i] == 0)
210
0
      return;
211
21.1k
    if (detail->regs_write[i] == PPC_REG_CR0) {
212
3.59k
      PPC_get_detail(MI)->update_cr0 = true;
213
3.59k
      return;
214
3.59k
    }
215
21.1k
  }
216
77.9k
#endif // CAPSTONE_DIET
217
77.9k
}
218
219
/// Parses and adds the branch predicate information and the BH field.
220
static void PPC_add_branch_predicates(MCInst *MI, const uint8_t *Bytes,
221
              size_t BytesLen)
222
77.9k
{
223
77.9k
  if (!detail_is_set(MI))
224
0
    return;
225
77.9k
#ifndef CAPSTONE_DIET
226
77.9k
  CS_ASSERT_RET(MI && Bytes);
227
77.9k
  if (BytesLen < 4)
228
779
    return;
229
230
77.1k
  ppc_insn_form form = ppc_insns[MI->Opcode].suppl_info.ppc.form;
231
77.1k
  bool b_form = ppc_is_b_form(form);
232
77.1k
  if (!(b_form || form == PPC_INSN_FORM_XLFORM_2))
233
69.1k
    return;
234
235
8.00k
  uint32_t Inst = readBytes32(MI, Bytes);
236
237
8.00k
  uint8_t bi = 0;
238
8.00k
  if (b_form)
239
7.06k
    bi = (Inst & PPC_INSN_FORM_B_BI_MASK) >> 16;
240
935
  else
241
935
    bi = (Inst & PPC_INSN_FORM_XL_BI_MASK) >> 16;
242
243
8.00k
  uint8_t bo = 0;
244
8.00k
  if (b_form)
245
7.06k
    bo = (Inst & PPC_INSN_FORM_B_BO_MASK) >> 21;
246
935
  else
247
935
    bo = (Inst & PPC_INSN_FORM_XL_BO_MASK) >> 21;
248
249
8.00k
  PPC_get_detail(MI)->bc.bo = bo;
250
8.00k
  PPC_get_detail(MI)->bc.bi = bi;
251
8.00k
  PPC_get_detail(MI)->bc.crX_bit = bi % 4;
252
8.00k
  PPC_get_detail(MI)->bc.crX = PPC_REG_CR0 + (bi / 4);
253
8.00k
  PPC_get_detail(MI)->bc.hint = PPC_get_hint(bo);
254
8.00k
  PPC_get_detail(MI)->bc.pred_cr = PPC_get_branch_pred(bi, bo, true);
255
8.00k
  PPC_get_detail(MI)->bc.pred_ctr = PPC_get_branch_pred(bi, bo, false);
256
257
8.00k
  if (ppc_is_b_form(form))
258
7.06k
    return;
259
260
935
  uint8_t bh = (Inst & PPC_INSN_FORM_XL_BH_MASK) >> 11;
261
935
  uint16_t xo = (Inst & PPC_INSN_FORM_XL_XO_MASK) >> 1;
262
  // Pre-defined values for XO fields (PowerISA v3.1B)
263
935
  uint16_t bcctr_xo_field = 528;
264
935
  uint16_t bctar_xo_field = 560;
265
935
  bool cond = (xo == bcctr_xo_field || xo == bctar_xo_field);
266
935
  switch (bh) {
267
0
  default:
268
0
    CS_ASSERT_RET(0 && "Invalid BH value.");
269
303
  case 0:
270
303
    PPC_get_detail(MI)->bc.bh = cond ? PPC_BH_NO_SUBROUTINE_RET :
271
303
               PPC_BH_SUBROUTINE_RET;
272
303
    break;
273
236
  case 1:
274
236
    PPC_get_detail(MI)->bc.bh = cond ? PPC_BH_RESERVED :
275
236
               PPC_BH_NO_SUBROUTINE_RET;
276
236
    break;
277
196
  case 2:
278
196
    PPC_get_detail(MI)->bc.bh = PPC_BH_RESERVED;
279
196
    break;
280
200
  case 3:
281
200
    PPC_get_detail(MI)->bc.bh = PPC_BH_NOT_PREDICTABLE;
282
200
    break;
283
935
  }
284
935
#endif // CAPSTONE_DIET
285
935
}
286
287
void PPC_set_instr_map_data(MCInst *MI, const uint8_t *Bytes, size_t BytesLen)
288
77.9k
{
289
77.9k
  map_cs_id(MI, ppc_insns, ARR_SIZE(ppc_insns));
290
77.9k
  map_implicit_reads(MI, ppc_insns);
291
77.9k
  map_implicit_writes(MI, ppc_insns);
292
77.9k
  map_groups(MI, ppc_insns);
293
77.9k
  PPC_add_branch_predicates(MI, Bytes, BytesLen);
294
77.9k
  PPC_check_updates_cr0(MI);
295
77.9k
  const ppc_suppl_info *suppl_info = map_get_suppl_info(MI, ppc_insns);
296
77.9k
  if (suppl_info) {
297
77.9k
    PPC_get_detail(MI)->format = suppl_info->form;
298
77.9k
  }
299
77.9k
}
300
301
/// Initialize PPCs detail.
302
void PPC_init_cs_detail(MCInst *MI)
303
77.9k
{
304
77.9k
  if (!detail_is_set(MI))
305
0
    return;
306
77.9k
  memset(get_detail(MI), 0, offsetof(cs_detail, ppc) + sizeof(cs_ppc));
307
77.9k
  PPC_get_detail(MI)->bc.bi = UINT8_MAX;
308
77.9k
  PPC_get_detail(MI)->bc.bo = UINT8_MAX;
309
77.9k
  PPC_get_detail(MI)->bc.crX = PPC_REG_INVALID;
310
77.9k
  PPC_get_detail(MI)->bc.crX_bit = PPC_BI_INVALID;
311
77.9k
  PPC_get_detail(MI)->bc.pred_cr = PPC_PRED_INVALID;
312
77.9k
  PPC_get_detail(MI)->bc.pred_ctr = PPC_PRED_INVALID;
313
77.9k
  PPC_get_detail(MI)->bc.hint = PPC_BR_NOT_GIVEN;
314
77.9k
  PPC_get_detail(MI)->bc.bh = PPC_BH_INVALID;
315
77.9k
}
316
317
void PPC_printer(MCInst *MI, SStream *O, void * /* MCRegisterInfo* */ info)
318
76.8k
{
319
76.8k
  MI->MRI = (MCRegisterInfo *)info;
320
76.8k
  MI->fillDetailOps = detail_is_set(MI);
321
76.8k
  MI->flat_insn->usesAliasDetails = map_use_alias_details(MI);
322
76.8k
  PPC_LLVM_printInst(MI, MI->address, "", O);
323
76.8k
#ifndef CAPSTONE_DIET
324
76.8k
  map_set_alias_id(MI, O, insn_alias_mnem_map,
325
76.8k
       ARR_SIZE(insn_alias_mnem_map));
326
76.8k
#endif
327
76.8k
}
328
329
bool PPC_getInstruction(csh handle, const uint8_t *bytes, size_t bytes_len,
330
      MCInst *instr, uint16_t *size, uint64_t address,
331
      void *info)
332
77.9k
{
333
77.9k
  PPC_init_cs_detail(instr);
334
77.9k
  DecodeStatus result = PPC_LLVM_getInstruction(
335
77.9k
    handle, bytes, bytes_len, instr, size, address, info);
336
77.9k
  PPC_set_instr_map_data(instr, bytes, bytes_len);
337
77.9k
  return result != MCDisassembler_Fail;
338
77.9k
}
339
340
bool PPC_getFeatureBits(unsigned int mode, unsigned int feature)
341
356k
{
342
356k
  if (feature == PPC_FeatureQPX) {
343
81.5k
    return (mode & CS_MODE_QPX) != 0;
344
275k
  } else if (feature == PPC_FeatureSPE) {
345
50.5k
    return (mode & CS_MODE_SPE) != 0;
346
224k
  } else if (feature == PPC_FeatureBookE) {
347
1.15k
    return (mode & CS_MODE_BOOKE) != 0;
348
223k
  } else if (feature == PPC_FeaturePS) {
349
51.5k
    return (mode & CS_MODE_PS) != 0;
350
172k
  } else if (feature == PPC_FeatureModernAIXAs) {
351
78.0k
    return (mode & CS_MODE_MODERN_AIX_AS) != 0;
352
94.0k
  } else if (feature == PPC_AIXOS) {
353
671
    return (mode & CS_MODE_AIX_OS) != 0 ||
354
671
           (mode & CS_MODE_MODERN_AIX_AS) != 0;
355
93.3k
  } else if (feature == PPC_FeatureMSYNC) {
356
169
    return (mode & CS_MODE_MSYNC) != 0;
357
169
  }
358
93.2k
  if ((mode & (CS_MODE_PWR7 | CS_MODE_PWR8 | CS_MODE_PWR9 |
359
93.2k
         CS_MODE_PWR10 | CS_MODE_PPC_ISA_FUTURE)) == 0) {
360
    // By default support everything
361
93.2k
    return true;
362
93.2k
  }
363
364
0
  if (is_feature_of(feature, P7Features, ARR_SIZE(P7Features))) {
365
0
    return (mode & (CS_MODE_PWR7 | CS_MODE_PWR8 | CS_MODE_PWR9 |
366
0
        CS_MODE_PWR10 | CS_MODE_PPC_ISA_FUTURE));
367
0
  }
368
0
  if (is_feature_of(feature, P8Features, ARR_SIZE(P8Features))) {
369
0
    return (mode & (CS_MODE_PWR8 | CS_MODE_PWR9 | CS_MODE_PWR10 |
370
0
        CS_MODE_PPC_ISA_FUTURE));
371
0
  }
372
0
  if (is_feature_of(feature, P9Features, ARR_SIZE(P9Features))) {
373
0
    return (mode & (CS_MODE_PWR9 | CS_MODE_PWR10 |
374
0
        CS_MODE_PPC_ISA_FUTURE));
375
0
  }
376
0
  if (is_feature_of(feature, P10Features, ARR_SIZE(P10Features))) {
377
0
    return (mode & (CS_MODE_PWR10 | CS_MODE_PPC_ISA_FUTURE));
378
0
  }
379
0
  if (is_feature_of(feature, FutureFeatures, ARR_SIZE(FutureFeatures))) {
380
0
    return (mode & CS_MODE_PPC_ISA_FUTURE);
381
0
  }
382
383
  // By default support everything
384
0
  return true;
385
0
}
386
387
#ifndef CAPSTONE_DIET
388
static const map_insn_ops insn_operands[] = {
389
#include "PPCGenCSMappingInsnOp.inc"
390
};
391
#endif
392
393
/// @brief Handles memory operands.
394
/// @param MI The MCInst.
395
/// @param OpNum The operand index.
396
static void handle_memory_operand(MCInst *MI, unsigned OpNum)
397
27.3k
{
398
27.3k
  cs_op_type op_type = map_get_op_type(MI, OpNum) & ~CS_OP_MEM;
399
400
  // If this is called from printOperand() we do not know if a
401
  // register is a base or an offset reg (imm is always disponent).
402
  // So we assume the base register is always added before the offset register
403
  // and set the flag appropriately.
404
27.3k
  bool is_off_reg =
405
27.3k
    ((op_type == CS_OP_REG) &&
406
27.3k
     PPC_get_detail_op(MI, 0)->mem.base != PPC_REG_INVALID);
407
27.3k
  PPC_set_detail_op_mem(MI, OpNum, MCInst_getOpVal(MI, OpNum),
408
27.3k
            is_off_reg);
409
27.3k
}
410
411
static void add_cs_detail_general(MCInst *MI, ppc_op_group op_group,
412
          unsigned OpNum)
413
234k
{
414
234k
  if (!detail_is_set(MI))
415
0
    return;
416
417
234k
  switch (op_group) {
418
0
  default:
419
0
    printf("General operand group %d not handled!\n", op_group);
420
0
    return;
421
159k
  case PPC_OP_GROUP_Operand: {
422
159k
    cs_op_type op_type = map_get_op_type(MI, OpNum);
423
424
    // Check for memory operands emitted via printOperand()
425
159k
    if (doing_mem(MI) && !(op_type & CS_OP_MEM)) {
426
      // Close previous memory operand
427
232
      set_mem_access(MI, false);
428
159k
    } else if (doing_mem(MI) || (op_type & CS_OP_MEM)) {
429
      // The memory operands use printOperand() to
430
      // emit their register and immediates.
431
27.3k
      if (!doing_mem(MI))
432
1.95k
        set_mem_access(MI, true);
433
27.3k
      handle_memory_operand(MI, OpNum);
434
27.3k
      return;
435
27.3k
    }
436
437
132k
    CS_ASSERT_RET((op_type & CS_OP_MEM) ==
438
132k
            0); // doing_mem should have been true.
439
440
132k
    if (op_type == CS_OP_REG)
441
130k
      PPC_set_detail_op_reg(MI, OpNum,
442
130k
                MCInst_getOpVal(MI, OpNum));
443
1.64k
    else if (op_type == CS_OP_IMM)
444
1.64k
      PPC_set_detail_op_imm(MI, OpNum,
445
1.64k
                MCInst_getOpVal(MI, OpNum));
446
0
    else
447
0
      CS_ASSERT_RET(0 && "Operand type not handled.");
448
132k
    break;
449
132k
  }
450
132k
  case PPC_OP_GROUP_ImmZeroOperand:
451
371
    if (doing_mem(MI))
452
      // PC-relative memory operand. The zero is not a base register.
453
305
      break;
454
    // fallthrough
455
1.98k
  case PPC_OP_GROUP_U1ImmOperand:
456
4.53k
  case PPC_OP_GROUP_U2ImmOperand:
457
6.85k
  case PPC_OP_GROUP_U3ImmOperand:
458
8.82k
  case PPC_OP_GROUP_U4ImmOperand:
459
20.4k
  case PPC_OP_GROUP_U5ImmOperand:
460
23.5k
  case PPC_OP_GROUP_U6ImmOperand:
461
23.8k
  case PPC_OP_GROUP_U7ImmOperand:
462
23.9k
  case PPC_OP_GROUP_U8ImmOperand:
463
24.2k
  case PPC_OP_GROUP_U10ImmOperand:
464
24.8k
  case PPC_OP_GROUP_U12ImmOperand:
465
24.8k
    PPC_set_detail_op_imm(MI, OpNum,
466
24.8k
              (uint32_t)MCInst_getOpVal(MI, OpNum));
467
24.8k
    break;
468
6.32k
  case PPC_OP_GROUP_U16ImmOperand:
469
6.32k
    if (!MCOperand_isImm(MCInst_getOperand(MI, OpNum)))
470
      // Handled in printOperand()
471
0
      return;
472
6.32k
    PPC_set_detail_op_imm(MI, OpNum,
473
6.32k
              (uint32_t)MCInst_getOpVal(MI, OpNum));
474
6.32k
    break;
475
364
  case PPC_OP_GROUP_S5ImmOperand: {
476
364
    int Imm = MCOperand_getImm(MCInst_getOperand(MI, (OpNum)));
477
364
    Imm = SignExtend32((Imm), 5);
478
364
    PPC_set_detail_op_imm(MI, OpNum, Imm);
479
364
    break;
480
6.32k
  }
481
410
  case PPC_OP_GROUP_S12ImmOperand: {
482
410
    int64_t Imm = SignExtend64(
483
410
      MCOperand_getImm(MCInst_getOperand(MI, (OpNum))), 12);
484
410
    if (doing_mem(MI)) {
485
410
      PPC_set_detail_op_mem(MI, OpNum, Imm, true);
486
410
      break;
487
410
    }
488
0
    PPC_set_detail_op_imm(MI, OpNum, Imm);
489
0
    break;
490
410
  }
491
15.7k
  case PPC_OP_GROUP_S16ImmOperand: {
492
15.7k
    if (!MCOperand_isImm(MCInst_getOperand(MI, OpNum)))
493
      // Handled in printOperand()
494
0
      return;
495
15.7k
    int16_t Imm = MCOperand_getImm(MCInst_getOperand(MI, (OpNum)));
496
15.7k
    if (doing_mem(MI)) {
497
10.7k
      PPC_set_detail_op_mem(MI, OpNum, Imm, true);
498
10.7k
      break;
499
10.7k
    }
500
4.97k
    PPC_set_detail_op_imm(MI, OpNum, Imm);
501
4.97k
    break;
502
15.7k
  }
503
1.65k
  case PPC_OP_GROUP_S34ImmOperand: {
504
1.65k
    if (!MCOperand_isImm(MCInst_getOperand(MI, OpNum)))
505
      // Handled in printOperand()
506
0
      return;
507
1.65k
    int64_t Imm = MCOperand_getImm(MCInst_getOperand(MI, (OpNum)));
508
1.65k
    if (doing_mem(MI)) {
509
945
      PPC_set_detail_op_mem(MI, OpNum, Imm, true);
510
945
      break;
511
945
    }
512
710
    PPC_set_detail_op_imm(MI, OpNum, Imm);
513
710
    break;
514
1.65k
  }
515
0
  case PPC_OP_GROUP_ATBitsAsHint: {
516
0
    PPC_get_detail(MI)->bc.hint =
517
0
      (ppc_br_hint)MCInst_getOpVal(MI, OpNum);
518
0
    break;
519
1.65k
  }
520
3.12k
  case PPC_OP_GROUP_AbsBranchOperand: {
521
3.12k
    if (!MCOperand_isImm(MCInst_getOperand(MI, OpNum)))
522
      // Handled in printOperand()
523
0
      return;
524
3.12k
    unsigned Val = MCInst_getOpVal(MI, OpNum) << 2;
525
3.12k
    PPC_check_safe_inc(MI);
526
3.12k
    PPC_get_detail_op(MI, 0)->type = PPC_OP_IMM;
527
3.12k
    PPC_get_detail_op(MI, 0)->imm = Val;
528
3.12k
    PPC_get_detail_op(MI, 0)->access = map_get_op_access(MI, OpNum);
529
3.12k
    PPC_inc_op_count(MI);
530
3.12k
    break;
531
3.12k
  }
532
0
  case PPC_OP_GROUP_TLSCall:
533
    // Handled in PPCInstPrinter and printOperand.
534
0
    return;
535
262
  case PPC_OP_GROUP_crbitm: {
536
262
    unsigned CCReg = MCInst_getOpVal(MI, OpNum);
537
262
    PPC_set_detail_op_reg(MI, OpNum, CCReg);
538
262
    break;
539
3.12k
  }
540
4.78k
  case PPC_OP_GROUP_BranchOperand: {
541
4.78k
    if (!MCOperand_isImm(MCInst_getOperand(MI, (OpNum))))
542
      // Handled in printOperand()
543
0
      return;
544
4.78k
    int32_t Imm = SignExtend32(
545
4.78k
      ((unsigned)MCInst_getOpVal(MI, (OpNum)) << 2), 32);
546
4.78k
    uint64_t Address = MI->address + Imm;
547
4.78k
    if (IS_32BIT(MI->csh->mode))
548
1.36k
      Address &= 0xffffffff;
549
4.78k
    PPC_check_safe_inc(MI);
550
4.78k
    PPC_get_detail_op(MI, 0)->type = PPC_OP_IMM;
551
4.78k
    PPC_get_detail_op(MI, 0)->imm = Address;
552
4.78k
    PPC_get_detail_op(MI, 0)->access = map_get_op_access(MI, OpNum);
553
4.78k
    PPC_inc_op_count(MI);
554
4.78k
    break;
555
4.78k
  }
556
  // Memory operands have their `set_mem_access()` calls
557
  // in PPCInstPrinter.
558
10.7k
  case PPC_OP_GROUP_MemRegImm:
559
16.0k
  case PPC_OP_GROUP_MemRegReg: {
560
    // These cases print 0 if the base register is R0.
561
    // So no printOperand() function is called.
562
    // We must handle the zero case here.
563
16.0k
    unsigned OpNumReg = 0;
564
16.0k
    if (op_group == PPC_OP_GROUP_MemRegImm)
565
10.7k
      OpNumReg = OpNum + 1;
566
5.30k
    else
567
5.30k
      OpNumReg = OpNum;
568
569
16.0k
    MCOperand *Op = MCInst_getOperand(MI, OpNumReg);
570
16.0k
    if (MCOperand_isReg(Op) && MCOperand_getReg(Op) == PPC_R0) {
571
0
      PPC_get_detail_op(MI, 0)->mem.base = PPC_REG_ZERO;
572
0
      PPC_get_detail_op(MI, 0)->type = PPC_OP_MEM;
573
0
      PPC_get_detail_op(MI, 0)->access =
574
0
        map_get_op_access(MI, OpNum);
575
0
    }
576
16.0k
    break;
577
10.7k
  }
578
244
  case PPC_OP_GROUP_MemRegImmHash:
579
884
  case PPC_OP_GROUP_MemRegImm34:
580
1.18k
  case PPC_OP_GROUP_MemRegImm34PCRel:
581
    // Handled in other printOperand functions.
582
1.18k
    break;
583
234k
  }
584
234k
}
585
586
void PPC_add_cs_detail_1(MCInst *MI, ppc_op_group op_group, unsigned OpNum,
587
       const char *Modifier)
588
0
{
589
0
  if (!detail_is_set(MI) || !map_fill_detail_ops(MI))
590
0
    return;
591
592
0
  switch (op_group) {
593
0
  default:
594
0
    printf("Operand group %d not handled!\n", op_group);
595
0
    return;
596
0
  case PPC_OP_GROUP_PredicateOperand: {
597
0
    if ((strcmp(Modifier, "cc") == 0) ||
598
0
        (strcmp(Modifier, "pm") == 0)) {
599
0
      unsigned Val = MCInst_getOpVal(MI, OpNum);
600
0
      unsigned bo = Val & 0x1f;
601
0
      unsigned bi = (Val & 0x1e0) >> 5;
602
0
      PPC_get_detail(MI)->bc.bo = bo;
603
0
      PPC_get_detail(MI)->bc.bi = bi;
604
0
      PPC_get_detail(MI)->bc.crX_bit = bi % 4;
605
0
      PPC_get_detail(MI)->bc.crX = PPC_REG_CR0 + (bi / 4);
606
0
      PPC_get_detail(MI)->bc.pred_cr =
607
0
        PPC_get_branch_pred(bi, bo, true);
608
0
      PPC_get_detail(MI)->bc.pred_ctr =
609
0
        PPC_get_branch_pred(bi, bo, false);
610
0
      PPC_get_detail(MI)->bc.hint = PPC_get_hint(bo);
611
0
    }
612
0
    return;
613
0
  }
614
0
  }
615
0
}
616
617
/// Fills cs_detail with the data of the operand.
618
/// Calls to this function should not be added by hand! Please checkout the
619
/// patch `AddCSDetail` of the CppTranslator.
620
void PPC_add_cs_detail_0(MCInst *MI, ppc_op_group op_group, unsigned OpNo)
621
234k
{
622
234k
  if (!detail_is_set(MI) || !map_fill_detail_ops(MI))
623
0
    return;
624
625
234k
  switch (op_group) {
626
0
  default:
627
0
    printf("Operand group %d not handled!\n", op_group);
628
0
    return;
629
410
  case PPC_OP_GROUP_S12ImmOperand:
630
160k
  case PPC_OP_GROUP_Operand:
631
165k
  case PPC_OP_GROUP_MemRegReg:
632
168k
  case PPC_OP_GROUP_U6ImmOperand:
633
180k
  case PPC_OP_GROUP_U5ImmOperand:
634
190k
  case PPC_OP_GROUP_MemRegImm:
635
206k
  case PPC_OP_GROUP_S16ImmOperand:
636
209k
  case PPC_OP_GROUP_U2ImmOperand:
637
215k
  case PPC_OP_GROUP_U16ImmOperand:
638
220k
  case PPC_OP_GROUP_BranchOperand:
639
223k
  case PPC_OP_GROUP_AbsBranchOperand:
640
225k
  case PPC_OP_GROUP_U1ImmOperand:
641
225k
  case PPC_OP_GROUP_TLSCall:
642
227k
  case PPC_OP_GROUP_U3ImmOperand:
643
227k
  case PPC_OP_GROUP_S5ImmOperand:
644
228k
  case PPC_OP_GROUP_MemRegImmHash:
645
230k
  case PPC_OP_GROUP_U4ImmOperand:
646
230k
  case PPC_OP_GROUP_U10ImmOperand:
647
230k
  case PPC_OP_GROUP_crbitm:
648
232k
  case PPC_OP_GROUP_S34ImmOperand:
649
232k
  case PPC_OP_GROUP_ImmZeroOperand:
650
233k
  case PPC_OP_GROUP_MemRegImm34:
651
233k
  case PPC_OP_GROUP_MemRegImm34PCRel:
652
233k
  case PPC_OP_GROUP_U8ImmOperand:
653
234k
  case PPC_OP_GROUP_U12ImmOperand:
654
234k
  case PPC_OP_GROUP_U7ImmOperand:
655
234k
  case PPC_OP_GROUP_ATBitsAsHint: {
656
234k
    add_cs_detail_general(MI, op_group, OpNo);
657
234k
    return;
658
234k
  }
659
234k
  }
660
234k
}
661
662
void PPC_set_detail_op_mem(MCInst *MI, unsigned OpNum, uint64_t Val,
663
         bool is_off_reg)
664
39.4k
{
665
39.4k
  if (!detail_is_set(MI))
666
0
    return;
667
668
39.4k
  CS_ASSERT_RET(map_get_op_type(MI, OpNum) & CS_OP_MEM);
669
39.4k
  cs_op_type secondary_type = map_get_op_type(MI, OpNum) & ~CS_OP_MEM;
670
671
39.4k
  switch (secondary_type) {
672
0
  default:
673
0
    CS_ASSERT_RET(0 && "Secondary type not supported yet.");
674
27.3k
  case CS_OP_REG:
675
27.3k
    if (is_off_reg) {
676
7.57k
      PPC_get_detail_op(MI, 0)->mem.offset = Val;
677
7.57k
      if (PPC_get_detail_op(MI, 0)->mem.base !=
678
7.57k
          PPC_REG_INVALID)
679
7.57k
        set_mem_access(MI, false);
680
19.8k
    } else {
681
19.8k
      PPC_get_detail_op(MI, 0)->mem.base = Val;
682
19.8k
      if (MCInst_opIsTying(MI, OpNum))
683
0
        map_add_implicit_write(
684
0
          MI, MCInst_getOpVal(MI, OpNum));
685
19.8k
    }
686
27.3k
    break;
687
12.0k
  case CS_OP_IMM:
688
12.0k
    PPC_get_detail_op(MI, 0)->mem.disp = Val;
689
12.0k
    if (PPC_get_detail_op(MI, 0)->mem.base != PPC_REG_INVALID)
690
0
      set_mem_access(MI, false);
691
12.0k
    break;
692
39.4k
  }
693
694
39.4k
  PPC_get_detail_op(MI, 0)->type = PPC_OP_MEM;
695
39.4k
  PPC_get_detail_op(MI, 0)->access = map_get_op_access(MI, OpNum);
696
39.4k
}
697
698
/// Adds a register PPC operand at position OpNum and increases the op_count by
699
/// one.
700
void PPC_set_detail_op_reg(MCInst *MI, unsigned OpNum, ppc_reg Reg)
701
130k
{
702
130k
  if (!detail_is_set(MI))
703
0
    return;
704
130k
  PPC_check_safe_inc(MI);
705
130k
  CS_ASSERT_RET(!(map_get_op_type(MI, OpNum) & CS_OP_MEM));
706
130k
  CS_ASSERT_RET(map_get_op_type(MI, OpNum) == CS_OP_REG);
707
708
130k
  PPC_get_detail_op(MI, 0)->type = PPC_OP_REG;
709
130k
  PPC_get_detail_op(MI, 0)->reg = Reg;
710
130k
  PPC_get_detail_op(MI, 0)->access = map_get_op_access(MI, OpNum);
711
130k
  PPC_inc_op_count(MI);
712
130k
}
713
714
/// Adds an immediate PPC operand at position OpNum and increases the op_count
715
/// by one.
716
void PPC_set_detail_op_imm(MCInst *MI, unsigned OpNum, int64_t Imm)
717
38.9k
{
718
38.9k
  if (!detail_is_set(MI))
719
0
    return;
720
38.9k
  PPC_check_safe_inc(MI);
721
38.9k
  CS_ASSERT_RET(!(map_get_op_type(MI, OpNum) & CS_OP_MEM));
722
38.9k
  CS_ASSERT_RET(map_get_op_type(MI, OpNum) == CS_OP_IMM);
723
724
38.9k
  PPC_get_detail_op(MI, 0)->type = PPC_OP_IMM;
725
38.9k
  PPC_get_detail_op(MI, 0)->imm = Imm;
726
38.9k
  PPC_get_detail_op(MI, 0)->access = map_get_op_access(MI, OpNum);
727
38.9k
  PPC_inc_op_count(MI);
728
38.9k
}
729
730
void PPC_set_mem_access(MCInst *MI, bool status)
731
46.1k
{
732
46.1k
  if (!detail_is_set(MI))
733
0
    return;
734
46.1k
  PPC_check_safe_inc(MI);
735
46.1k
  if ((!status && !doing_mem(MI)) || (status && doing_mem(MI)))
736
5.84k
    return; // Nothing to do
737
738
40.2k
  set_doing_mem(MI, status);
739
40.2k
  if (status) {
740
20.1k
    PPC_get_detail_op(MI, 0)->type = PPC_OP_MEM;
741
20.1k
    PPC_get_detail_op(MI, 0)->mem.base = PPC_REG_INVALID;
742
20.1k
    PPC_get_detail_op(MI, 0)->mem.offset = PPC_REG_INVALID;
743
20.1k
    PPC_get_detail_op(MI, 0)->mem.disp = 0;
744
745
20.1k
#ifndef CAPSTONE_DIET
746
20.1k
    uint8_t access =
747
20.1k
      map_get_op_access(MI, PPC_get_detail(MI)->op_count);
748
20.1k
    PPC_get_detail_op(MI, 0)->access = access;
749
20.1k
#endif
750
20.1k
  } else {
751
    // done, select the next operand slot
752
20.1k
    PPC_inc_op_count(MI);
753
20.1k
  }
754
40.2k
}
755
756
void PPC_setup_op(cs_ppc_op *op)
757
423
{
758
423
  memset(op, 0, sizeof(cs_ppc_op));
759
423
  op->type = PPC_OP_INVALID;
760
423
}
761
762
/// Inserts a immediate to the detail operands at @index.
763
/// Already present operands are moved.
764
void PPC_insert_detail_op_imm_at(MCInst *MI, unsigned index, int64_t Val,
765
         cs_ac_type access)
766
423
{
767
423
  if (!detail_is_set(MI) || !map_fill_detail_ops(MI))
768
0
    return;
769
770
423
  PPC_check_safe_inc(MI);
771
772
423
  cs_ppc_op op;
773
423
  PPC_setup_op(&op);
774
423
  op.type = PPC_OP_IMM;
775
423
  op.imm = Val;
776
423
  op.access = access;
777
778
423
  cs_ppc_op *ops = PPC_get_detail(MI)->operands;
779
423
  int i = PPC_get_detail(MI)->op_count - 1;
780
423
  for (; i >= index; --i) {
781
0
    ops[i + 1] = ops[i];
782
0
    if (i == index)
783
0
      break;
784
0
  }
785
423
  ops[index] = op;
786
423
  PPC_inc_op_count(MI);
787
423
}
788
789
#endif