Coverage Report

Created: 2025-07-18 06:43

/src/capstonenext/arch/PowerPC/PPCMapping.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
#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.46k
{
117
2.46k
  MCRegisterInfo_InitMCRegisterInfo(MRI, PPCRegDesc, PPC_REG_ENDING, 0, 0,
118
2.46k
            PPCMCRegisterClasses,
119
2.46k
            ARR_SIZE(PPCMCRegisterClasses), 0, 0,
120
2.46k
            PPCRegDiffLists, 0, PPCSubRegIdxLists,
121
2.46k
            ARR_SIZE(PPCSubRegIdxLists),
122
2.46k
            PPCRegEncodingTable);
123
2.46k
}
124
125
const char *PPC_reg_name(csh handle, unsigned int reg)
126
46.8k
{
127
46.8k
  if (reg > PPC_REG_INVALID && reg < PPC_REG_ENDING)
128
46.8k
    return PPC_LLVM_getRegisterName(reg);
129
0
  return NULL;
130
46.8k
}
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
61.3k
{
135
  // We do this after Instruction disassembly.
136
61.3k
}
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
61.3k
{
156
61.3k
#ifndef CAPSTONE_DIET
157
61.3k
  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
61.3k
  if (id >= PPC_INS_ENDING)
164
0
    return NULL;
165
166
61.3k
  return insn_name_maps[id];
167
#else
168
  return NULL;
169
#endif
170
61.3k
}
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
29.9k
{
191
29.9k
#ifndef CAPSTONE_DIET
192
29.9k
  return id2name(group_name_maps, ARR_SIZE(group_name_maps), id);
193
#else
194
  return NULL;
195
#endif
196
29.9k
}
197
198
const insn_map ppc_insns[] = {
199
#include "PPCGenCSMappingInsn.inc"
200
};
201
202
void PPC_check_updates_cr0(MCInst *MI)
203
62.7k
{
204
62.7k
#ifndef CAPSTONE_DIET
205
62.7k
  if (!detail_is_set(MI))
206
0
    return;
207
62.7k
  cs_detail *detail = get_detail(MI);
208
80.3k
  for (int i = 0; i < detail->regs_write_count; ++i) {
209
20.2k
    if (detail->regs_write[i] == 0)
210
0
      return;
211
20.2k
    if (detail->regs_write[i] == PPC_REG_CR0) {
212
2.68k
      PPC_get_detail(MI)->update_cr0 = true;
213
2.68k
      return;
214
2.68k
    }
215
20.2k
  }
216
62.7k
#endif // CAPSTONE_DIET
217
62.7k
}
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
62.7k
{
223
62.7k
  if (!detail_is_set(MI))
224
0
    return;
225
62.7k
#ifndef CAPSTONE_DIET
226
62.7k
  CS_ASSERT_RET(MI && Bytes);
227
62.7k
  if (BytesLen < 4)
228
950
    return;
229
230
61.8k
  ppc_insn_form form = ppc_insns[MI->Opcode].suppl_info.ppc.form;
231
61.8k
  bool b_form = ppc_is_b_form(form);
232
61.8k
  if (!(b_form || form == PPC_INSN_FORM_XLFORM_2))
233
51.7k
    return;
234
235
10.0k
  uint32_t Inst = readBytes32(MI, Bytes);
236
237
10.0k
  uint8_t bi = 0;
238
10.0k
  if (b_form)
239
8.72k
    bi = (Inst & PPC_INSN_FORM_B_BI_MASK) >> 16;
240
1.34k
  else
241
1.34k
    bi = (Inst & PPC_INSN_FORM_XL_BI_MASK) >> 16;
242
243
10.0k
  uint8_t bo = 0;
244
10.0k
  if (b_form)
245
8.72k
    bo = (Inst & PPC_INSN_FORM_B_BO_MASK) >> 21;
246
1.34k
  else
247
1.34k
    bo = (Inst & PPC_INSN_FORM_XL_BO_MASK) >> 21;
248
249
10.0k
  PPC_get_detail(MI)->bc.bo = bo;
250
10.0k
  PPC_get_detail(MI)->bc.bi = bi;
251
10.0k
  PPC_get_detail(MI)->bc.crX_bit = bi % 4;
252
10.0k
  PPC_get_detail(MI)->bc.crX = PPC_REG_CR0 + (bi / 4);
253
10.0k
  PPC_get_detail(MI)->bc.hint = PPC_get_hint(bo);
254
10.0k
  PPC_get_detail(MI)->bc.pred_cr = PPC_get_branch_pred(bi, bo, true);
255
10.0k
  PPC_get_detail(MI)->bc.pred_ctr = PPC_get_branch_pred(bi, bo, false);
256
257
10.0k
  if (ppc_is_b_form(form))
258
8.72k
    return;
259
260
1.34k
  uint8_t bh = (Inst & PPC_INSN_FORM_XL_BH_MASK) >> 11;
261
1.34k
  uint16_t xo = (Inst & PPC_INSN_FORM_XL_XO_MASK) >> 1;
262
  // Pre-defined values for XO fields (PowerISA v3.1B)
263
1.34k
  uint16_t bcctr_xo_field = 528;
264
1.34k
  uint16_t bctar_xo_field = 560;
265
1.34k
  bool cond = (xo == bcctr_xo_field || xo == bctar_xo_field);
266
1.34k
  switch (bh) {
267
0
  default:
268
0
    CS_ASSERT_RET(0 && "Invalid BH value.");
269
339
  case 0:
270
339
    PPC_get_detail(MI)->bc.bh = cond ? PPC_BH_NO_SUBROUTINE_RET :
271
339
               PPC_BH_SUBROUTINE_RET;
272
339
    break;
273
805
  case 1:
274
805
    PPC_get_detail(MI)->bc.bh = cond ? PPC_BH_RESERVED :
275
805
               PPC_BH_NO_SUBROUTINE_RET;
276
805
    break;
277
141
  case 2:
278
141
    PPC_get_detail(MI)->bc.bh = PPC_BH_RESERVED;
279
141
    break;
280
56
  case 3:
281
56
    PPC_get_detail(MI)->bc.bh = PPC_BH_NOT_PREDICTABLE;
282
56
    break;
283
1.34k
  }
284
1.34k
#endif // CAPSTONE_DIET
285
1.34k
}
286
287
void PPC_set_instr_map_data(MCInst *MI, const uint8_t *Bytes, size_t BytesLen)
288
62.7k
{
289
62.7k
  map_cs_id(MI, ppc_insns, ARR_SIZE(ppc_insns));
290
62.7k
  map_implicit_reads(MI, ppc_insns);
291
62.7k
  map_implicit_writes(MI, ppc_insns);
292
62.7k
  map_groups(MI, ppc_insns);
293
62.7k
  PPC_add_branch_predicates(MI, Bytes, BytesLen);
294
62.7k
  PPC_check_updates_cr0(MI);
295
62.7k
  const ppc_suppl_info *suppl_info = map_get_suppl_info(MI, ppc_insns);
296
62.7k
  if (suppl_info) {
297
62.7k
    PPC_get_detail(MI)->format = suppl_info->form;
298
62.7k
  }
299
62.7k
}
300
301
/// Initialize PPCs detail.
302
void PPC_init_cs_detail(MCInst *MI)
303
62.7k
{
304
62.7k
  if (!detail_is_set(MI))
305
0
    return;
306
62.7k
  memset(get_detail(MI), 0, offsetof(cs_detail, ppc) + sizeof(cs_ppc));
307
62.7k
  PPC_get_detail(MI)->bc.bi = UINT8_MAX;
308
62.7k
  PPC_get_detail(MI)->bc.bo = UINT8_MAX;
309
62.7k
  PPC_get_detail(MI)->bc.crX = PPC_REG_INVALID;
310
62.7k
  PPC_get_detail(MI)->bc.crX_bit = PPC_BI_INVALID;
311
62.7k
  PPC_get_detail(MI)->bc.pred_cr = PPC_PRED_INVALID;
312
62.7k
  PPC_get_detail(MI)->bc.pred_ctr = PPC_PRED_INVALID;
313
62.7k
  PPC_get_detail(MI)->bc.hint = PPC_BR_NOT_GIVEN;
314
62.7k
  PPC_get_detail(MI)->bc.bh = PPC_BH_INVALID;
315
62.7k
}
316
317
void PPC_printer(MCInst *MI, SStream *O, void * /* MCRegisterInfo* */ info)
318
61.3k
{
319
61.3k
  MI->MRI = (MCRegisterInfo *)info;
320
61.3k
  MI->fillDetailOps = detail_is_set(MI);
321
61.3k
  MI->flat_insn->usesAliasDetails = map_use_alias_details(MI);
322
61.3k
  PPC_LLVM_printInst(MI, MI->address, "", O);
323
61.3k
#ifndef CAPSTONE_DIET
324
61.3k
  map_set_alias_id(MI, O, insn_alias_mnem_map,
325
61.3k
       ARR_SIZE(insn_alias_mnem_map));
326
61.3k
#endif
327
61.3k
}
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
62.7k
{
333
62.7k
  PPC_init_cs_detail(instr);
334
62.7k
  DecodeStatus result = PPC_LLVM_getInstruction(
335
62.7k
    handle, bytes, bytes_len, instr, size, address, info);
336
62.7k
  PPC_set_instr_map_data(instr, bytes, bytes_len);
337
62.7k
  return result != MCDisassembler_Fail;
338
62.7k
}
339
340
bool PPC_getFeatureBits(unsigned int mode, unsigned int feature)
341
276k
{
342
276k
  if (feature == PPC_FeatureQPX) {
343
67.5k
    return (mode & CS_MODE_QPX) != 0;
344
208k
  } else if (feature == PPC_FeatureSPE) {
345
35.0k
    return (mode & CS_MODE_SPE) != 0;
346
173k
  } else if (feature == PPC_FeatureBookE) {
347
1.19k
    return (mode & CS_MODE_BOOKE) != 0;
348
172k
  } else if (feature == PPC_FeaturePS) {
349
36.2k
    return (mode & CS_MODE_PS) != 0;
350
136k
  } else if (feature == PPC_FeatureModernAIXAs) {
351
62.4k
    return (mode & CS_MODE_MODERN_AIX_AS) != 0;
352
73.5k
  } else if (feature == PPC_AIXOS) {
353
219
    return (mode & CS_MODE_AIX_OS) != 0 || (mode & CS_MODE_MODERN_AIX_AS) != 0;
354
73.3k
  } else if (feature == PPC_FeatureMSYNC) {
355
18
    return (mode & CS_MODE_MSYNC) != 0;
356
18
  }
357
73.3k
  if ((mode & (CS_MODE_PWR7 | CS_MODE_PWR8 | CS_MODE_PWR9 | CS_MODE_PWR10 | CS_MODE_PPC_ISA_FUTURE)) == 0) {
358
    // By default support everything
359
73.3k
    return true;
360
73.3k
  }
361
362
0
  if (is_feature_of(feature, P7Features, ARR_SIZE(P7Features))) {
363
0
    return (mode & (CS_MODE_PWR7 | CS_MODE_PWR8 | CS_MODE_PWR9 | CS_MODE_PWR10 | CS_MODE_PPC_ISA_FUTURE));
364
0
  }
365
0
  if (is_feature_of(feature, P8Features, ARR_SIZE(P8Features))) {
366
0
    return (mode & (CS_MODE_PWR8 | CS_MODE_PWR9 | CS_MODE_PWR10 | CS_MODE_PPC_ISA_FUTURE));
367
0
  }
368
0
  if (is_feature_of(feature, P9Features, ARR_SIZE(P9Features))) {
369
0
    return (mode & (CS_MODE_PWR9 | CS_MODE_PWR10 | CS_MODE_PPC_ISA_FUTURE));
370
0
  }
371
0
  if (is_feature_of(feature, P10Features, ARR_SIZE(P10Features))) {
372
0
    return (mode & (CS_MODE_PWR10 | CS_MODE_PPC_ISA_FUTURE));
373
0
  }
374
0
  if (is_feature_of(feature, FutureFeatures, ARR_SIZE(FutureFeatures))) {
375
0
    return (mode & CS_MODE_PPC_ISA_FUTURE);
376
0
  }
377
378
  // By default support everything
379
0
  return true;
380
0
}
381
382
#ifndef CAPSTONE_DIET
383
static const map_insn_ops insn_operands[] = {
384
#include "PPCGenCSMappingInsnOp.inc"
385
};
386
#endif
387
388
/// @brief Handles memory operands.
389
/// @param MI The MCInst.
390
/// @param OpNum The operand index.
391
static void handle_memory_operand(MCInst *MI, unsigned OpNum)
392
21.5k
{
393
21.5k
  cs_op_type op_type = map_get_op_type(MI, OpNum) & ~CS_OP_MEM;
394
395
  // If this is called from printOperand() we do not know if a
396
  // register is a base or an offset reg (imm is always disponent).
397
  // So we assume the base register is always added before the offset register
398
  // and set the flag appropriately.
399
21.5k
  bool is_off_reg =
400
21.5k
    ((op_type == CS_OP_REG) &&
401
21.5k
     PPC_get_detail_op(MI, 0)->mem.base != PPC_REG_INVALID);
402
21.5k
  PPC_set_detail_op_mem(MI, OpNum, MCInst_getOpVal(MI, OpNum),
403
21.5k
            is_off_reg);
404
21.5k
}
405
406
static void add_cs_detail_general(MCInst *MI, ppc_op_group op_group,
407
          unsigned OpNum)
408
189k
{
409
189k
  if (!detail_is_set(MI))
410
0
    return;
411
412
189k
  switch (op_group) {
413
0
  default:
414
0
    printf("General operand group %d not handled!\n", op_group);
415
0
    return;
416
121k
  case PPC_OP_GROUP_Operand: {
417
121k
    cs_op_type op_type = map_get_op_type(MI, OpNum);
418
419
    // Check for memory operands emitted via printOperand()
420
121k
    if (doing_mem(MI) && !(op_type & CS_OP_MEM)) {
421
      // Close previous memory operand
422
444
      set_mem_access(MI, false);
423
120k
    } else if (doing_mem(MI) || (op_type & CS_OP_MEM)) {
424
      // The memory operands use printOperand() to
425
      // emit their register and immediates.
426
21.5k
      if (!doing_mem(MI))
427
1.16k
        set_mem_access(MI, true);
428
21.5k
      handle_memory_operand(MI, OpNum);
429
21.5k
      return;
430
21.5k
    }
431
432
99.4k
    CS_ASSERT_RET((op_type & CS_OP_MEM) ==
433
99.4k
            0); // doing_mem should have been true.
434
435
99.4k
    if (op_type == CS_OP_REG)
436
97.7k
      PPC_set_detail_op_reg(MI, OpNum,
437
97.7k
                MCInst_getOpVal(MI, OpNum));
438
1.66k
    else if (op_type == CS_OP_IMM)
439
1.66k
      PPC_set_detail_op_imm(MI, OpNum,
440
1.66k
                MCInst_getOpVal(MI, OpNum));
441
0
    else
442
0
      CS_ASSERT_RET(0 && "Operand type not handled.");
443
99.4k
    break;
444
121k
  }
445
131
  case PPC_OP_GROUP_ImmZeroOperand:
446
2.55k
  case PPC_OP_GROUP_U1ImmOperand:
447
3.79k
  case PPC_OP_GROUP_U2ImmOperand:
448
5.23k
  case PPC_OP_GROUP_U3ImmOperand:
449
5.90k
  case PPC_OP_GROUP_U4ImmOperand:
450
17.5k
  case PPC_OP_GROUP_U5ImmOperand:
451
19.8k
  case PPC_OP_GROUP_U6ImmOperand:
452
20.1k
  case PPC_OP_GROUP_U7ImmOperand:
453
20.2k
  case PPC_OP_GROUP_U8ImmOperand:
454
20.3k
  case PPC_OP_GROUP_U10ImmOperand:
455
20.5k
  case PPC_OP_GROUP_U12ImmOperand:
456
20.5k
    PPC_set_detail_op_imm(MI, OpNum,
457
20.5k
              (uint32_t)MCInst_getOpVal(MI, OpNum));
458
20.5k
    break;
459
3.29k
  case PPC_OP_GROUP_U16ImmOperand:
460
3.29k
    if (!MCOperand_isImm(MCInst_getOperand(MI, OpNum)))
461
      // Handled in printOperand()
462
0
      return;
463
3.29k
    PPC_set_detail_op_imm(MI, OpNum,
464
3.29k
              (uint32_t)MCInst_getOpVal(MI, OpNum));
465
3.29k
    break;
466
354
  case PPC_OP_GROUP_S5ImmOperand: {
467
354
    int Imm = MCOperand_getImm(MCInst_getOperand(MI, (OpNum)));
468
354
    Imm = SignExtend32((Imm), 5);
469
354
    PPC_set_detail_op_imm(MI, OpNum, Imm);
470
354
    break;
471
3.29k
  }
472
763
  case PPC_OP_GROUP_S12ImmOperand: {
473
763
    int64_t Imm = SignExtend64(
474
763
      MCOperand_getImm(MCInst_getOperand(MI, (OpNum))), 12);
475
763
    if (doing_mem(MI)) {
476
763
      PPC_set_detail_op_mem(MI, OpNum, Imm, true);
477
763
      break;
478
763
    }
479
0
    PPC_set_detail_op_imm(MI, OpNum, Imm);
480
0
    break;
481
763
  }
482
18.5k
  case PPC_OP_GROUP_S16ImmOperand: {
483
18.5k
    if (!MCOperand_isImm(MCInst_getOperand(MI, OpNum)))
484
      // Handled in printOperand()
485
0
      return;
486
18.5k
    int16_t Imm = MCOperand_getImm(MCInst_getOperand(MI, (OpNum)));
487
18.5k
    if (doing_mem(MI)) {
488
12.0k
      PPC_set_detail_op_mem(MI, OpNum, Imm, true);
489
12.0k
      break;
490
12.0k
    }
491
6.46k
    PPC_set_detail_op_imm(MI, OpNum, Imm);
492
6.46k
    break;
493
18.5k
  }
494
514
  case PPC_OP_GROUP_S34ImmOperand: {
495
514
    if (!MCOperand_isImm(MCInst_getOperand(MI, OpNum)))
496
      // Handled in printOperand()
497
0
      return;
498
514
    int64_t Imm = MCOperand_getImm(MCInst_getOperand(MI, (OpNum)));
499
514
    if (doing_mem(MI)) {
500
372
      PPC_set_detail_op_mem(MI, OpNum, Imm, true);
501
372
      break;
502
372
    }
503
142
    PPC_set_detail_op_imm(MI, OpNum, Imm);
504
142
    break;
505
514
  }
506
0
  case PPC_OP_GROUP_ATBitsAsHint: {
507
0
    PPC_get_detail(MI)->bc.hint =
508
0
      (ppc_br_hint)MCInst_getOpVal(MI, OpNum);
509
0
    break;
510
514
  }
511
3.27k
  case PPC_OP_GROUP_AbsBranchOperand: {
512
3.27k
    if (!MCOperand_isImm(MCInst_getOperand(MI, OpNum)))
513
      // Handled in printOperand()
514
0
      return;
515
3.27k
    unsigned Val = MCInst_getOpVal(MI, OpNum) << 2;
516
3.27k
    PPC_check_safe_inc(MI);
517
3.27k
    PPC_get_detail_op(MI, 0)->type = PPC_OP_IMM;
518
3.27k
    PPC_get_detail_op(MI, 0)->imm = Val;
519
3.27k
    PPC_get_detail_op(MI, 0)->access = map_get_op_access(MI, OpNum);
520
3.27k
    PPC_inc_op_count(MI);
521
3.27k
    break;
522
3.27k
  }
523
0
  case PPC_OP_GROUP_TLSCall:
524
    // Handled in PPCInstPrinter and printOperand.
525
0
    return;
526
198
  case PPC_OP_GROUP_crbitm: {
527
198
    unsigned CCReg = MCInst_getOpVal(MI, OpNum);
528
198
    PPC_set_detail_op_reg(MI, OpNum, CCReg);
529
198
    break;
530
3.27k
  }
531
6.42k
  case PPC_OP_GROUP_BranchOperand: {
532
6.42k
    if (!MCOperand_isImm(MCInst_getOperand(MI, (OpNum))))
533
      // Handled in printOperand()
534
0
      return;
535
6.42k
    int32_t Imm = SignExtend32(
536
6.42k
      ((unsigned)MCInst_getOpVal(MI, (OpNum)) << 2), 32);
537
6.42k
    uint64_t Address = MI->address + Imm;
538
6.42k
    if (IS_32BIT(MI->csh->mode))
539
1.83k
      Address &= 0xffffffff;
540
6.42k
    PPC_check_safe_inc(MI);
541
6.42k
    PPC_get_detail_op(MI, 0)->type = PPC_OP_IMM;
542
6.42k
    PPC_get_detail_op(MI, 0)->imm = Address;
543
6.42k
    PPC_get_detail_op(MI, 0)->access = map_get_op_access(MI, OpNum);
544
6.42k
    PPC_inc_op_count(MI);
545
6.42k
    break;
546
6.42k
  }
547
  // Memory operands have their `set_mem_access()` calls
548
  // in PPCInstPrinter.
549
12.0k
  case PPC_OP_GROUP_MemRegImm:
550
14.3k
  case PPC_OP_GROUP_MemRegReg: {
551
    // These cases print 0 if the base register is R0.
552
    // So no printOperand() function is called.
553
    // We must handle the zero case here.
554
14.3k
    unsigned OpNumReg = 0;
555
14.3k
    if (op_group == PPC_OP_GROUP_MemRegImm)
556
12.0k
      OpNumReg = OpNum + 1;
557
2.32k
    else
558
2.32k
      OpNumReg = OpNum;
559
560
14.3k
    MCOperand *Op = MCInst_getOperand(MI, OpNumReg);
561
14.3k
    if (MCOperand_isReg(Op) && MCOperand_getReg(Op) == PPC_R0) {
562
0
      PPC_get_detail_op(MI, 0)->mem.base = PPC_REG_ZERO;
563
0
      PPC_get_detail_op(MI, 0)->type = PPC_OP_MEM;
564
0
      PPC_get_detail_op(MI, 0)->access =
565
0
        map_get_op_access(MI, OpNum);
566
0
    }
567
14.3k
    break;
568
12.0k
  }
569
52
  case PPC_OP_GROUP_MemRegImmHash:
570
311
  case PPC_OP_GROUP_MemRegImm34:
571
424
  case PPC_OP_GROUP_MemRegImm34PCRel:
572
    // Handled in other printOperand functions.
573
424
    break;
574
189k
  }
575
189k
}
576
577
void PPC_add_cs_detail_1(MCInst *MI, ppc_op_group op_group, unsigned OpNum,
578
       const char *Modifier)
579
0
{
580
0
  if (!detail_is_set(MI) || !map_fill_detail_ops(MI))
581
0
    return;
582
583
0
  switch (op_group) {
584
0
  default:
585
0
    printf("Operand group %d not handled!\n", op_group);
586
0
    return;
587
0
  case PPC_OP_GROUP_PredicateOperand: {
588
0
    if ((strcmp(Modifier, "cc") == 0) ||
589
0
        (strcmp(Modifier, "pm") == 0)) {
590
0
      unsigned Val = MCInst_getOpVal(MI, OpNum);
591
0
      unsigned bo = Val & 0x1f;
592
0
      unsigned bi = (Val & 0x1e0) >> 5;
593
0
      PPC_get_detail(MI)->bc.bo = bo;
594
0
      PPC_get_detail(MI)->bc.bi = bi;
595
0
      PPC_get_detail(MI)->bc.crX_bit = bi % 4;
596
0
      PPC_get_detail(MI)->bc.crX = PPC_REG_CR0 + (bi / 4);
597
0
      PPC_get_detail(MI)->bc.pred_cr =
598
0
        PPC_get_branch_pred(bi, bo, true);
599
0
      PPC_get_detail(MI)->bc.pred_ctr =
600
0
        PPC_get_branch_pred(bi, bo, false);
601
0
      PPC_get_detail(MI)->bc.hint = PPC_get_hint(bo);
602
0
    }
603
0
    return;
604
0
  }
605
0
  }
606
0
}
607
608
/// Fills cs_detail with the data of the operand.
609
/// Calls to this function should not be added by hand! Please checkout the
610
/// patch `AddCSDetail` of the CppTranslator.
611
void PPC_add_cs_detail_0(MCInst *MI, ppc_op_group op_group, unsigned OpNo)
612
189k
{
613
189k
  if (!detail_is_set(MI) || !map_fill_detail_ops(MI))
614
0
    return;
615
616
189k
  switch (op_group) {
617
0
  default:
618
0
    printf("Operand group %d not handled!\n", op_group);
619
0
    return;
620
763
  case PPC_OP_GROUP_S12ImmOperand:
621
121k
  case PPC_OP_GROUP_Operand:
622
124k
  case PPC_OP_GROUP_MemRegReg:
623
126k
  case PPC_OP_GROUP_U6ImmOperand:
624
138k
  case PPC_OP_GROUP_U5ImmOperand:
625
150k
  case PPC_OP_GROUP_MemRegImm:
626
168k
  case PPC_OP_GROUP_S16ImmOperand:
627
169k
  case PPC_OP_GROUP_U2ImmOperand:
628
173k
  case PPC_OP_GROUP_U16ImmOperand:
629
179k
  case PPC_OP_GROUP_BranchOperand:
630
182k
  case PPC_OP_GROUP_AbsBranchOperand:
631
185k
  case PPC_OP_GROUP_U1ImmOperand:
632
185k
  case PPC_OP_GROUP_TLSCall:
633
186k
  case PPC_OP_GROUP_U3ImmOperand:
634
187k
  case PPC_OP_GROUP_S5ImmOperand:
635
187k
  case PPC_OP_GROUP_MemRegImmHash:
636
187k
  case PPC_OP_GROUP_U4ImmOperand:
637
188k
  case PPC_OP_GROUP_U10ImmOperand:
638
188k
  case PPC_OP_GROUP_crbitm:
639
188k
  case PPC_OP_GROUP_S34ImmOperand:
640
188k
  case PPC_OP_GROUP_ImmZeroOperand:
641
189k
  case PPC_OP_GROUP_MemRegImm34:
642
189k
  case PPC_OP_GROUP_MemRegImm34PCRel:
643
189k
  case PPC_OP_GROUP_U8ImmOperand:
644
189k
  case PPC_OP_GROUP_U12ImmOperand:
645
189k
  case PPC_OP_GROUP_U7ImmOperand:
646
189k
  case PPC_OP_GROUP_ATBitsAsHint: {
647
189k
    add_cs_detail_general(MI, op_group, OpNo);
648
189k
    return;
649
189k
  }
650
189k
  }
651
189k
}
652
653
void PPC_set_detail_op_mem(MCInst *MI, unsigned OpNum, uint64_t Val,
654
         bool is_off_reg)
655
34.7k
{
656
34.7k
  if (!detail_is_set(MI))
657
0
    return;
658
659
34.7k
  CS_ASSERT_RET(map_get_op_type(MI, OpNum) & CS_OP_MEM);
660
34.7k
  cs_op_type secondary_type = map_get_op_type(MI, OpNum) & ~CS_OP_MEM;
661
662
34.7k
  switch (secondary_type) {
663
0
  default:
664
0
    CS_ASSERT_RET(0 && "Secondary type not supported yet.");
665
21.5k
  case CS_OP_REG:
666
21.5k
    if (is_off_reg) {
667
3.98k
      PPC_get_detail_op(MI, 0)->mem.offset = Val;
668
3.98k
      if (PPC_get_detail_op(MI, 0)->mem.base !=
669
3.98k
          PPC_REG_INVALID)
670
3.98k
        set_mem_access(MI, false);
671
17.5k
    } else {
672
17.5k
      PPC_get_detail_op(MI, 0)->mem.base = Val;
673
17.5k
      if (MCInst_opIsTying(MI, OpNum))
674
0
        map_add_implicit_write(
675
0
          MI, MCInst_getOpVal(MI, OpNum));
676
17.5k
    }
677
21.5k
    break;
678
13.2k
  case CS_OP_IMM:
679
13.2k
    PPC_get_detail_op(MI, 0)->mem.disp = Val;
680
13.2k
    if (PPC_get_detail_op(MI, 0)->mem.base != PPC_REG_INVALID)
681
0
      set_mem_access(MI, false);
682
13.2k
    break;
683
34.7k
  }
684
685
34.7k
  PPC_get_detail_op(MI, 0)->type = PPC_OP_MEM;
686
34.7k
  PPC_get_detail_op(MI, 0)->access = map_get_op_access(MI, OpNum);
687
34.7k
}
688
689
/// Adds a register PPC operand at position OpNum and increases the op_count by
690
/// one.
691
void PPC_set_detail_op_reg(MCInst *MI, unsigned OpNum, ppc_reg Reg)
692
97.9k
{
693
97.9k
  if (!detail_is_set(MI))
694
0
    return;
695
97.9k
  PPC_check_safe_inc(MI);
696
97.9k
  CS_ASSERT_RET(!(map_get_op_type(MI, OpNum) & CS_OP_MEM));
697
97.9k
  CS_ASSERT_RET(map_get_op_type(MI, OpNum) == CS_OP_REG);
698
699
97.9k
  PPC_get_detail_op(MI, 0)->type = PPC_OP_REG;
700
97.9k
  PPC_get_detail_op(MI, 0)->reg = Reg;
701
97.9k
  PPC_get_detail_op(MI, 0)->access = map_get_op_access(MI, OpNum);
702
97.9k
  PPC_inc_op_count(MI);
703
97.9k
}
704
705
/// Adds an immediate PPC operand at position OpNum and increases the op_count
706
/// by one.
707
void PPC_set_detail_op_imm(MCInst *MI, unsigned OpNum, int64_t Imm)
708
33.1k
{
709
33.1k
  if (!detail_is_set(MI))
710
0
    return;
711
33.1k
  PPC_check_safe_inc(MI);
712
33.1k
  CS_ASSERT_RET(!(map_get_op_type(MI, OpNum) & CS_OP_MEM));
713
33.1k
  CS_ASSERT_RET(map_get_op_type(MI, OpNum) == CS_OP_IMM);
714
715
33.1k
  PPC_get_detail_op(MI, 0)->type = PPC_OP_IMM;
716
33.1k
  PPC_get_detail_op(MI, 0)->imm = Imm;
717
33.1k
  PPC_get_detail_op(MI, 0)->access = map_get_op_access(MI, OpNum);
718
33.1k
  PPC_inc_op_count(MI);
719
33.1k
}
720
721
void PPC_set_mem_access(MCInst *MI, bool status)
722
38.6k
{
723
38.6k
  if (!detail_is_set(MI))
724
0
    return;
725
38.6k
  PPC_check_safe_inc(MI);
726
38.6k
  if ((!status && !doing_mem(MI)) || (status && doing_mem(MI)))
727
3.27k
    return; // Nothing to do
728
729
35.3k
  set_doing_mem(MI, status);
730
35.3k
  if (status) {
731
17.6k
    PPC_get_detail_op(MI, 0)->type = PPC_OP_MEM;
732
17.6k
    PPC_get_detail_op(MI, 0)->mem.base = PPC_REG_INVALID;
733
17.6k
    PPC_get_detail_op(MI, 0)->mem.offset = PPC_REG_INVALID;
734
17.6k
    PPC_get_detail_op(MI, 0)->mem.disp = 0;
735
736
17.6k
#ifndef CAPSTONE_DIET
737
17.6k
    uint8_t access =
738
17.6k
      map_get_op_access(MI, PPC_get_detail(MI)->op_count);
739
17.6k
    PPC_get_detail_op(MI, 0)->access = access;
740
17.6k
#endif
741
17.6k
  } else {
742
    // done, select the next operand slot
743
17.6k
    PPC_inc_op_count(MI);
744
17.6k
  }
745
35.3k
}
746
747
void PPC_setup_op(cs_ppc_op *op)
748
192
{
749
192
  memset(op, 0, sizeof(cs_ppc_op));
750
192
  op->type = PPC_OP_INVALID;
751
192
}
752
753
/// Inserts a immediate to the detail operands at @index.
754
/// Already present operands are moved.
755
void PPC_insert_detail_op_imm_at(MCInst *MI, unsigned index, int64_t Val,
756
         cs_ac_type access)
757
192
{
758
192
  if (!detail_is_set(MI) || !map_fill_detail_ops(MI))
759
0
    return;
760
761
192
  PPC_check_safe_inc(MI);
762
763
192
  cs_ppc_op op;
764
192
  PPC_setup_op(&op);
765
192
  op.type = PPC_OP_IMM;
766
192
  op.imm = Val;
767
192
  op.access = access;
768
769
192
  cs_ppc_op *ops = PPC_get_detail(MI)->operands;
770
192
  int i = PPC_get_detail(MI)->op_count - 1;
771
192
  for (; i >= index; --i) {
772
0
    ops[i + 1] = ops[i];
773
0
    if (i == index)
774
0
      break;
775
0
  }
776
192
  ops[index] = op;
777
192
  PPC_inc_op_count(MI);
778
192
}
779
780
#endif