Coverage Report

Created: 2026-03-03 06:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/capstonenext/arch/PowerPC/PPCInstPrinter.c
Line
Count
Source
1
/* Capstone Disassembly Engine, http://www.capstone-engine.org */
2
/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2022, */
3
/*    Rot127 <unisono@quyllur.org> 2022-2023 */
4
/* Automatically translated source file from LLVM. */
5
6
/* LLVM-commit: <commit> */
7
/* LLVM-tag: <tag> */
8
9
/* Only small edits allowed. */
10
/* For multiple similar edits, please create a Patch for the translator. */
11
12
/* Capstone's C++ file translator: */
13
/* https://github.com/capstone-engine/capstone/tree/next/suite/auto-sync */
14
15
//===-- PPCInstPrinter.cpp - Convert PPC MCInst to assembly syntax --------===//
16
//
17
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
18
// See https://llvm.org/LICENSE.txt for license information.
19
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
20
//
21
//===----------------------------------------------------------------------===//
22
//
23
// This class prints an PPC MCInst to a .s file.
24
//
25
//===----------------------------------------------------------------------===//
26
27
#include <capstone/platform.h>
28
#include <stdio.h>
29
#include <stdlib.h>
30
#include <string.h>
31
32
#include "../../utils.h"
33
#include "../../LEB128.h"
34
#include "../../Mapping.h"
35
#include "../../MCInst.h"
36
#include "../../MCInstPrinter.h"
37
#include "../../MCInstrDesc.h"
38
#include "../../MCRegisterInfo.h"
39
#include "PPCInstrInfo.h"
40
#include "PPCLinkage.h"
41
#include "PPCMCTargetDesc.h"
42
#include "PPCMapping.h"
43
#include "PPCPredicates.h"
44
#include "PPCRegisterInfo.h"
45
46
#define CONCAT(a, b) CONCAT_(a, b)
47
#define CONCAT_(a, b) a##_##b
48
49
#define DEBUG_TYPE "asm-printer"
50
51
extern const MCInstrDesc PPCInsts[];
52
53
// Static function declarations. These are functions which have the same identifiers
54
// over all architectures. Therefor they need to be static.
55
#ifndef CAPSTONE_DIET
56
static void printCustomAliasOperand(MCInst *MI, uint64_t Address,
57
            unsigned OpIdx, unsigned PrintMethodIdx,
58
            SStream *O);
59
#endif
60
61
static const char *getRegisterName(unsigned RegNo);
62
63
/// showRegistersWithPercentPrefix - Check if this register name should be
64
/// printed with a percentage symbol as prefix.
65
static inline bool showRegistersWithPercentPrefix(const MCInst *MI,
66
              const char *RegName)
67
31.6k
{
68
31.6k
  if ((MI->csh->syntax & CS_OPT_SYNTAX_NOREGNAME) ||
69
31.6k
      !(MI->csh->syntax & CS_OPT_SYNTAX_PERCENT) ||
70
0
      PPC_getFeatureBits(MI->csh->mode, PPC_FeatureModernAIXAs))
71
31.6k
    return false;
72
73
0
  switch (RegName[0]) {
74
0
  default:
75
0
    return false;
76
0
  case 'r':
77
0
  case 'f':
78
0
  case 'q':
79
0
  case 'v':
80
0
  case 'c':
81
0
    return true;
82
0
  }
83
0
}
84
85
/// getVerboseConditionalRegName - This method expands the condition register
86
/// when requested explicitly or targeting Darwin.
87
static inline const char *getVerboseConditionRegName(const MCInst *MI,
88
                 unsigned RegNum,
89
                 unsigned RegEncoding)
90
31.6k
{
91
31.6k
  if (MI->csh->syntax & CS_OPT_SYNTAX_NOREGNAME)
92
0
    return NULL;
93
31.6k
  if (RegNum < PPC_CR0EQ || RegNum > PPC_CR7UN)
94
28.8k
    return NULL;
95
2.78k
  const char *CRBits[] = {
96
2.78k
    "lt",     "gt", "eq",     "un", "4*cr1+lt",
97
2.78k
    "4*cr1+gt", "4*cr1+eq", "4*cr1+un", "4*cr2+lt", "4*cr2+gt",
98
2.78k
    "4*cr2+eq", "4*cr2+un", "4*cr3+lt", "4*cr3+gt", "4*cr3+eq",
99
2.78k
    "4*cr3+un", "4*cr4+lt", "4*cr4+gt", "4*cr4+eq", "4*cr4+un",
100
2.78k
    "4*cr5+lt", "4*cr5+gt", "4*cr5+eq", "4*cr5+un", "4*cr6+lt",
101
2.78k
    "4*cr6+gt", "4*cr6+eq", "4*cr6+un", "4*cr7+lt", "4*cr7+gt",
102
2.78k
    "4*cr7+eq", "4*cr7+un"
103
2.78k
  };
104
2.78k
  return CRBits[RegEncoding];
105
31.6k
}
106
107
// showRegistersWithPrefix - This method determines whether registers
108
// should be number-only or include the prefix.
109
static inline bool showRegistersWithPrefix(const MCInst *MI)
110
31.6k
{
111
31.6k
  return !(MI->csh->syntax & CS_OPT_SYNTAX_NOREGNAME);
112
31.6k
}
113
114
static inline void printOperand(MCInst *MI, unsigned OpNo, SStream *O)
115
32.2k
{
116
32.2k
  PPC_add_cs_detail_0(MI, PPC_OP_GROUP_Operand, OpNo);
117
32.2k
  MCOperand *Op = MCInst_getOperand(MI, (OpNo));
118
32.2k
  if (MCOperand_isReg(Op)) {
119
31.6k
    unsigned Reg = MCOperand_getReg(Op);
120
31.6k
    if (!MI->csh->ShowVSRNumsAsVR)
121
31.6k
      Reg = PPCInstrInfo_getRegNumForOperand(
122
31.6k
        MCInstrDesc_get(MCInst_getOpcode(MI),
123
31.6k
            PPCDescs.Insts,
124
31.6k
            ARR_SIZE(PPCDescs.Insts)),
125
31.6k
        Reg, OpNo);
126
127
31.6k
    const char *RegName;
128
31.6k
    RegName = getVerboseConditionRegName(
129
31.6k
      MI, Reg, MI->MRI->RegEncodingTable[Reg]);
130
31.6k
    if (RegName == NULL)
131
28.8k
      RegName = getRegisterName(Reg);
132
31.6k
    if (showRegistersWithPercentPrefix(MI, RegName))
133
0
      SStream_concat0(O, "%");
134
31.6k
    if (!showRegistersWithPrefix(MI))
135
0
      RegName = PPCRegisterInfo_stripRegisterPrefix(RegName);
136
137
31.6k
    SStream_concat0(O, RegName);
138
31.6k
    return;
139
31.6k
  }
140
141
564
  if (MCOperand_isImm(Op)) {
142
564
    printInt64(O, MCOperand_getImm(Op));
143
564
    return;
144
564
  }
145
564
}
146
147
static inline void printPredicateOperand(MCInst *MI, unsigned OpNo, SStream *O,
148
           const char *Modifier)
149
0
{
150
0
  PPC_add_cs_detail_1(MI, PPC_OP_GROUP_PredicateOperand, OpNo, Modifier);
151
0
  unsigned Code = MCOperand_getImm(MCInst_getOperand(MI, (OpNo)));
152
153
0
  if (strcmp(Modifier, "cc") == 0) {
154
0
    switch ((PPC_Predicate)Code) {
155
0
    default:
156
0
      CS_ASSERT_RET(0 && "Invalid predicate code");
157
0
    case PPC_PRED_LT_MINUS:
158
0
    case PPC_PRED_LT_PLUS:
159
0
    case PPC_PRED_LT:
160
0
      SStream_concat0(O, "lt");
161
0
      return;
162
0
    case PPC_PRED_LE_MINUS:
163
0
    case PPC_PRED_LE_PLUS:
164
0
    case PPC_PRED_LE:
165
0
      SStream_concat0(O, "le");
166
0
      return;
167
0
    case PPC_PRED_EQ_MINUS:
168
0
    case PPC_PRED_EQ_PLUS:
169
0
    case PPC_PRED_EQ:
170
0
      SStream_concat0(O, "eq");
171
0
      return;
172
0
    case PPC_PRED_GE_MINUS:
173
0
    case PPC_PRED_GE_PLUS:
174
0
    case PPC_PRED_GE:
175
0
      SStream_concat0(O, "ge");
176
0
      return;
177
0
    case PPC_PRED_GT_MINUS:
178
0
    case PPC_PRED_GT_PLUS:
179
0
    case PPC_PRED_GT:
180
0
      SStream_concat0(O, "gt");
181
0
      return;
182
0
    case PPC_PRED_NE_MINUS:
183
0
    case PPC_PRED_NE_PLUS:
184
0
    case PPC_PRED_NE:
185
0
      SStream_concat0(O, "ne");
186
0
      return;
187
0
    case PPC_PRED_UN_MINUS:
188
0
    case PPC_PRED_UN_PLUS:
189
0
    case PPC_PRED_UN:
190
0
      SStream_concat0(O, "un");
191
0
      return;
192
0
    case PPC_PRED_NU_MINUS:
193
0
    case PPC_PRED_NU_PLUS:
194
0
    case PPC_PRED_NU:
195
0
      SStream_concat0(O, "nu");
196
0
      return;
197
0
    case PPC_PRED_BIT_SET:
198
0
    case PPC_PRED_BIT_UNSET:
199
0
      CS_ASSERT_RET(0 && "Invalid use of bit predicate code");
200
0
    }
201
0
    CS_ASSERT_RET(0 && "Invalid predicate code");
202
0
  }
203
204
0
  if (strcmp(Modifier, "pm") == 0) {
205
0
    switch ((PPC_Predicate)Code) {
206
0
    default:
207
0
      CS_ASSERT_RET(0 && "Invalid predicate code");
208
0
    case PPC_PRED_LT:
209
0
    case PPC_PRED_LE:
210
0
    case PPC_PRED_EQ:
211
0
    case PPC_PRED_GE:
212
0
    case PPC_PRED_GT:
213
0
    case PPC_PRED_NE:
214
0
    case PPC_PRED_UN:
215
0
    case PPC_PRED_NU:
216
0
      return;
217
0
    case PPC_PRED_LT_MINUS:
218
0
    case PPC_PRED_LE_MINUS:
219
0
    case PPC_PRED_EQ_MINUS:
220
0
    case PPC_PRED_GE_MINUS:
221
0
    case PPC_PRED_GT_MINUS:
222
0
    case PPC_PRED_NE_MINUS:
223
0
    case PPC_PRED_UN_MINUS:
224
0
    case PPC_PRED_NU_MINUS:
225
0
      SStream_concat0(O, "-");
226
0
      return;
227
0
    case PPC_PRED_LT_PLUS:
228
0
    case PPC_PRED_LE_PLUS:
229
0
    case PPC_PRED_EQ_PLUS:
230
0
    case PPC_PRED_GE_PLUS:
231
0
    case PPC_PRED_GT_PLUS:
232
0
    case PPC_PRED_NE_PLUS:
233
0
    case PPC_PRED_UN_PLUS:
234
0
    case PPC_PRED_NU_PLUS:
235
0
      SStream_concat0(O, "+");
236
0
      return;
237
0
    case PPC_PRED_BIT_SET:
238
0
    case PPC_PRED_BIT_UNSET:
239
0
      CS_ASSERT_RET(0 && "Invalid use of bit predicate code");
240
0
    }
241
0
    CS_ASSERT_RET(0 && "Invalid predicate code");
242
0
  }
243
244
0
  printOperand(MI, OpNo + 1, O);
245
0
}
246
247
static inline void printATBitsAsHint(MCInst *MI, unsigned OpNo, SStream *O)
248
0
{
249
0
  PPC_add_cs_detail_0(MI, PPC_OP_GROUP_ATBitsAsHint, OpNo);
250
0
  unsigned Code = MCOperand_getImm(MCInst_getOperand(MI, (OpNo)));
251
0
  if (Code == 2)
252
0
    SStream_concat0(O, "-");
253
0
  else if (Code == 3)
254
0
    SStream_concat0(O, "+");
255
0
}
256
257
static inline void printU1ImmOperand(MCInst *MI, unsigned OpNo, SStream *O)
258
279
{
259
279
  PPC_add_cs_detail_0(MI, PPC_OP_GROUP_U1ImmOperand, OpNo);
260
279
  unsigned int Value = MCOperand_getImm(MCInst_getOperand(MI, (OpNo)));
261
279
  CS_ASSERT(Value <= 1 && "Invalid u1imm argument!");
262
279
  printUInt32(O, (unsigned int)Value);
263
279
}
264
265
static inline void printU2ImmOperand(MCInst *MI, unsigned OpNo, SStream *O)
266
575
{
267
575
  PPC_add_cs_detail_0(MI, PPC_OP_GROUP_U2ImmOperand, OpNo);
268
575
  unsigned int Value = MCOperand_getImm(MCInst_getOperand(MI, (OpNo)));
269
575
  CS_ASSERT(Value <= 3 && "Invalid u2imm argument!");
270
575
  printUInt32(O, (unsigned int)Value);
271
575
}
272
273
static inline void printU3ImmOperand(MCInst *MI, unsigned OpNo, SStream *O)
274
362
{
275
362
  PPC_add_cs_detail_0(MI, PPC_OP_GROUP_U3ImmOperand, OpNo);
276
362
  unsigned int Value = MCOperand_getImm(MCInst_getOperand(MI, (OpNo)));
277
362
  CS_ASSERT(Value <= 8 && "Invalid u3imm argument!");
278
362
  printUInt32(O, (unsigned int)Value);
279
362
}
280
281
static inline void printU4ImmOperand(MCInst *MI, unsigned OpNo, SStream *O)
282
260
{
283
260
  PPC_add_cs_detail_0(MI, PPC_OP_GROUP_U4ImmOperand, OpNo);
284
260
  unsigned int Value = MCOperand_getImm(MCInst_getOperand(MI, (OpNo)));
285
260
  CS_ASSERT(Value <= 15 && "Invalid u4imm argument!");
286
260
  printUInt32(O, (unsigned int)Value);
287
260
}
288
289
static inline void printS5ImmOperand(MCInst *MI, unsigned OpNo, SStream *O)
290
96
{
291
96
  PPC_add_cs_detail_0(MI, PPC_OP_GROUP_S5ImmOperand, OpNo);
292
96
  int Value = MCOperand_getImm(MCInst_getOperand(MI, (OpNo)));
293
96
  Value = SignExtend32((Value), 5);
294
96
  printInt32(O, (int)Value);
295
96
}
296
297
static inline void printImmZeroOperand(MCInst *MI, unsigned OpNo, SStream *O)
298
56
{
299
56
  PPC_add_cs_detail_0(MI, PPC_OP_GROUP_ImmZeroOperand, OpNo);
300
56
  unsigned int Value = MCOperand_getImm(MCInst_getOperand(MI, (OpNo)));
301
56
  CS_ASSERT(Value == 0 && "Operand must be zero");
302
56
  printUInt32(O, (unsigned int)Value);
303
56
}
304
305
static inline void printU5ImmOperand(MCInst *MI, unsigned OpNo, SStream *O)
306
3.50k
{
307
3.50k
  PPC_add_cs_detail_0(MI, PPC_OP_GROUP_U5ImmOperand, OpNo);
308
3.50k
  unsigned int Value = MCOperand_getImm(MCInst_getOperand(MI, (OpNo)));
309
3.50k
  CS_ASSERT(Value <= 31 && "Invalid u5imm argument!");
310
3.50k
  printUInt32(O, (unsigned int)Value);
311
3.50k
}
312
313
static inline void printU6ImmOperand(MCInst *MI, unsigned OpNo, SStream *O)
314
290
{
315
290
  PPC_add_cs_detail_0(MI, PPC_OP_GROUP_U6ImmOperand, OpNo);
316
290
  unsigned int Value = MCOperand_getImm(MCInst_getOperand(MI, (OpNo)));
317
290
  CS_ASSERT(Value <= 63 && "Invalid u6imm argument!");
318
290
  printUInt32(O, (unsigned int)Value);
319
290
}
320
321
static inline void printU7ImmOperand(MCInst *MI, unsigned OpNo, SStream *O)
322
4
{
323
4
  PPC_add_cs_detail_0(MI, PPC_OP_GROUP_U7ImmOperand, OpNo);
324
4
  unsigned int Value = MCOperand_getImm(MCInst_getOperand(MI, (OpNo)));
325
4
  CS_ASSERT(Value <= 127 && "Invalid u7imm argument!");
326
4
  printUInt32(O, (unsigned int)Value);
327
4
}
328
329
// Operands of BUILD_VECTOR are signed and we use this to print operands
330
// of XXSPLTIB which are unsigned. So we simply truncate to 8 bits and
331
// print as unsigned.
332
static inline void printU8ImmOperand(MCInst *MI, unsigned OpNo, SStream *O)
333
13
{
334
13
  PPC_add_cs_detail_0(MI, PPC_OP_GROUP_U8ImmOperand, OpNo);
335
13
  unsigned char Value = MCOperand_getImm(MCInst_getOperand(MI, (OpNo)));
336
13
  CS_ASSERT(Value <= 255 && "Invalid u8imm argument!");
337
13
  printUInt32(O, (unsigned int)Value);
338
13
}
339
340
static inline void printU10ImmOperand(MCInst *MI, unsigned OpNo, SStream *O)
341
0
{
342
0
  PPC_add_cs_detail_0(MI, PPC_OP_GROUP_U10ImmOperand, OpNo);
343
0
  unsigned short Value = MCOperand_getImm(MCInst_getOperand(MI, (OpNo)));
344
0
  CS_ASSERT(Value <= 1023 && "Invalid u10imm argument!");
345
0
  printUInt32(O, (unsigned short)Value);
346
0
}
347
348
static inline void printU12ImmOperand(MCInst *MI, unsigned OpNo, SStream *O)
349
8
{
350
8
  PPC_add_cs_detail_0(MI, PPC_OP_GROUP_U12ImmOperand, OpNo);
351
8
  unsigned short Value = MCOperand_getImm(MCInst_getOperand(MI, (OpNo)));
352
8
  CS_ASSERT(Value <= 4095 && "Invalid u12imm argument!");
353
8
  printUInt32(O, (unsigned short)Value);
354
8
}
355
356
static inline void printS12ImmOperand(MCInst *MI, unsigned OpNo, SStream *O)
357
53
{
358
53
  PPC_add_cs_detail_0(MI, PPC_OP_GROUP_S12ImmOperand, OpNo);
359
53
  if (MCOperand_isImm(MCInst_getOperand(MI, OpNo))) {
360
53
    int Imm = (int)MCOperand_getImm(MCInst_getOperand(MI, OpNo));
361
53
    Imm = SignExtend32(Imm, 12);
362
53
    printInt32(O, Imm);
363
53
  } else
364
0
    printOperand(MI, OpNo, O);
365
53
}
366
367
static inline void printMemRegImmPS(MCInst *MI, unsigned OpNo, SStream *O)
368
53
{
369
53
  set_mem_access(MI, true);
370
371
53
  printS12ImmOperand(MI, OpNo, O);
372
53
  SStream_concat0(O, "(");
373
53
  printOperand(MI, OpNo + 1, O);
374
53
  SStream_concat0(O, ")");
375
376
53
  set_mem_access(MI, false);
377
53
}
378
379
static inline void printS16ImmOperand(MCInst *MI, unsigned OpNo, SStream *O)
380
5.55k
{
381
5.55k
  PPC_add_cs_detail_0(MI, PPC_OP_GROUP_S16ImmOperand, OpNo);
382
5.55k
  if (MCOperand_isImm(MCInst_getOperand(MI, (OpNo))))
383
5.55k
    printInt32(O, (short)MCOperand_getImm(
384
5.55k
              MCInst_getOperand(MI, (OpNo))));
385
0
  else
386
0
    printOperand(MI, OpNo, O);
387
5.55k
}
388
389
static inline void printS34ImmOperand(MCInst *MI, unsigned OpNo, SStream *O)
390
73
{
391
73
  PPC_add_cs_detail_0(MI, PPC_OP_GROUP_S34ImmOperand, OpNo);
392
73
  if (MCOperand_isImm(MCInst_getOperand(MI, (OpNo)))) {
393
73
    long long Value =
394
73
      MCOperand_getImm(MCInst_getOperand(MI, (OpNo)));
395
396
73
    printInt64(O, (long long)Value);
397
73
  } else
398
0
    printOperand(MI, OpNo, O);
399
73
}
400
401
static inline void printU16ImmOperand(MCInst *MI, unsigned OpNo, SStream *O)
402
980
{
403
980
  PPC_add_cs_detail_0(MI, PPC_OP_GROUP_U16ImmOperand, OpNo);
404
980
  if (MCOperand_isImm(MCInst_getOperand(MI, (OpNo))))
405
980
    printUInt32(O, (unsigned short)MCOperand_getImm(
406
980
               MCInst_getOperand(MI, (OpNo))));
407
0
  else
408
0
    printOperand(MI, OpNo, O);
409
980
}
410
411
static inline void printBranchOperand(MCInst *MI, uint64_t Address,
412
              unsigned OpNo, SStream *O)
413
1.78k
{
414
1.78k
  PPC_add_cs_detail_0(MI, PPC_OP_GROUP_BranchOperand, OpNo);
415
1.78k
  if (!MCOperand_isImm(MCInst_getOperand(MI, (OpNo)))) {
416
0
    printOperand(MI, OpNo, O);
417
0
    return;
418
0
  }
419
1.78k
  int32_t Imm = SignExtend32(
420
1.78k
    ((unsigned)MCOperand_getImm(MCInst_getOperand(MI, (OpNo)))
421
1.78k
     << 2),
422
1.78k
    32);
423
1.78k
  if (MI->csh->PrintBranchImmAsAddress) {
424
1.78k
    uint64_t Target = Address + Imm;
425
1.78k
    if (!IS_64BIT(MI->csh->mode))
426
148
      Target &= 0xffffffff;
427
1.78k
    printUInt64(O, (Target));
428
1.78k
  } else {
429
    // Branches can take an immediate operand. This is used by the branch
430
    // selection pass to print, for example `.+8` (for ELF) or `$+8` (for
431
    // AIX) to express an eight byte displacement from the program counter.
432
0
    if (!PPC_getFeatureBits(MI->csh->mode, PPC_FeatureModernAIXAs))
433
0
      SStream_concat0(O, ".");
434
0
    else
435
0
      SStream_concat0(O, "$");
436
437
0
    if (Imm >= 0)
438
0
      SStream_concat0(O, "+");
439
0
    printInt32(O, Imm);
440
0
  }
441
1.78k
}
442
443
static inline void printAbsBranchOperand(MCInst *MI, unsigned OpNo, SStream *O)
444
898
{
445
898
  PPC_add_cs_detail_0(MI, PPC_OP_GROUP_AbsBranchOperand, OpNo);
446
898
  if (!MCOperand_isImm(MCInst_getOperand(MI, (OpNo)))) {
447
0
    printOperand(MI, OpNo, O);
448
0
    return;
449
0
  }
450
451
898
  printUInt64(O,
452
898
        ((unsigned)MCOperand_getImm(MCInst_getOperand(MI, (OpNo)))
453
898
         << 2));
454
898
}
455
456
static inline void printcrbitm(MCInst *MI, unsigned OpNo, SStream *O)
457
172
{
458
172
  PPC_add_cs_detail_0(MI, PPC_OP_GROUP_crbitm, OpNo);
459
172
  unsigned CCReg = MCOperand_getReg(MCInst_getOperand(MI, (OpNo)));
460
172
  unsigned RegNo;
461
172
  switch (CCReg) {
462
0
  default:
463
0
    CS_ASSERT_RET(0 && "Unknown CR register");
464
24
  case PPC_CR0:
465
24
    RegNo = 0;
466
24
    break;
467
4
  case PPC_CR1:
468
4
    RegNo = 1;
469
4
    break;
470
15
  case PPC_CR2:
471
15
    RegNo = 2;
472
15
    break;
473
16
  case PPC_CR3:
474
16
    RegNo = 3;
475
16
    break;
476
16
  case PPC_CR4:
477
16
    RegNo = 4;
478
16
    break;
479
14
  case PPC_CR5:
480
14
    RegNo = 5;
481
14
    break;
482
42
  case PPC_CR6:
483
42
    RegNo = 6;
484
42
    break;
485
41
  case PPC_CR7:
486
41
    RegNo = 7;
487
41
    break;
488
172
  }
489
172
  printUInt32(O, (0x80 >> RegNo));
490
172
}
491
492
static inline void printMemRegImm(MCInst *MI, unsigned OpNo, SStream *O)
493
3.37k
{
494
3.37k
  set_mem_access(MI, true);
495
3.37k
  PPC_add_cs_detail_0(MI, PPC_OP_GROUP_MemRegImm, OpNo);
496
3.37k
  printS16ImmOperand(MI, OpNo, O);
497
3.37k
  SStream_concat0(O, "(");
498
499
3.37k
  if (MCOperand_getReg(MCInst_getOperand(MI, (OpNo + 1))) == PPC_R0)
500
0
    SStream_concat0(O, "0");
501
3.37k
  else
502
3.37k
    printOperand(MI, OpNo + 1, O);
503
3.37k
  SStream_concat0(O, ")");
504
3.37k
  set_mem_access(MI, false);
505
3.37k
}
506
507
static inline void printMemRegImmHash(MCInst *MI, unsigned OpNo, SStream *O)
508
1
{
509
1
  set_mem_access(MI, true);
510
1
  PPC_add_cs_detail_0(MI, PPC_OP_GROUP_MemRegImmHash, OpNo);
511
1
  printInt32(O, MCOperand_getImm(MCInst_getOperand(MI, (OpNo))));
512
1
  SStream_concat0(O, "(");
513
514
1
  printOperand(MI, OpNo + 1, O);
515
1
  SStream_concat0(O, ")");
516
1
  set_mem_access(MI, false);
517
1
}
518
519
static inline void printMemRegImm34PCRel(MCInst *MI, unsigned OpNo, SStream *O)
520
56
{
521
56
  set_mem_access(MI, true);
522
56
  PPC_add_cs_detail_0(MI, PPC_OP_GROUP_MemRegImm34PCRel, OpNo);
523
56
  printS34ImmOperand(MI, OpNo, O);
524
56
  SStream_concat0(O, "(");
525
526
56
  printImmZeroOperand(MI, OpNo + 1, O);
527
56
  SStream_concat0(O, ")");
528
56
  set_mem_access(MI, false);
529
56
}
530
531
static inline void printMemRegImm34(MCInst *MI, unsigned OpNo, SStream *O)
532
12
{
533
12
  set_mem_access(MI, true);
534
12
  PPC_add_cs_detail_0(MI, PPC_OP_GROUP_MemRegImm34, OpNo);
535
12
  printS34ImmOperand(MI, OpNo, O);
536
12
  SStream_concat0(O, "(");
537
538
12
  printOperand(MI, OpNo + 1, O);
539
12
  SStream_concat0(O, ")");
540
12
  set_mem_access(MI, false);
541
12
}
542
543
static inline void printMemRegReg(MCInst *MI, unsigned OpNo, SStream *O)
544
426
{
545
426
  set_mem_access(MI, true);
546
426
  PPC_add_cs_detail_0(MI, PPC_OP_GROUP_MemRegReg, OpNo);
547
  // When used as the base register, r0 reads constant zero rather than
548
  // the value contained in the register.  For this reason, the darwin
549
  // assembler requires that we print r0 as 0 (no r) when used as the base.
550
426
  if (MCOperand_getReg(MCInst_getOperand(MI, (OpNo))) == PPC_R0)
551
0
    SStream_concat0(O, "0");
552
426
  else
553
426
    printOperand(MI, OpNo, O);
554
426
  SStream_concat0(O, ", ");
555
426
  printOperand(MI, OpNo + 1, O);
556
426
  set_mem_access(MI, false);
557
426
}
558
559
static inline void printTLSCall(MCInst *MI, unsigned OpNo, SStream *O)
560
0
{
561
0
  PPC_add_cs_detail_0(MI, PPC_OP_GROUP_TLSCall, OpNo);
562
563
  // Expression logic removed.
564
565
0
  set_mem_access(MI, true);
566
0
  SStream_concat0(O, "(");
567
568
0
  printOperand(MI, OpNo + 1, O);
569
0
  SStream_concat0(O, ")");
570
0
  set_mem_access(MI, false);
571
0
}
572
573
#define PRINT_ALIAS_INSTR
574
#include "PPCGenAsmWriter.inc"
575
576
static void printInst(MCInst *MI, uint64_t Address, const char *Annot,
577
          SStream *O)
578
16.7k
{
579
16.7k
  bool isAlias = false;
580
16.7k
  bool useAliasDetails = false;
581
  // Customize printing of the addis instruction on AIX. When an operand is a
582
  // symbol reference, the instruction syntax is changed to look like a load
583
  // operation, i.e:
584
  //     Transform:  addis $rD, $rA, $src --> addis $rD, $src($rA).
585
16.7k
  if (PPC_getFeatureBits(MI->csh->mode, PPC_FeatureModernAIXAs) &&
586
0
      (MCInst_getOpcode(MI) == PPC_ADDIS8 ||
587
0
       MCInst_getOpcode(MI) == PPC_ADDIS) &&
588
0
      MCOperand_isExpr(MCInst_getOperand(MI, (2)))) {
589
0
    SStream_concat0(O, "\taddis ");
590
0
    printOperand(MI, 0, O);
591
0
    SStream_concat0(O, ", ");
592
0
    printOperand(MI, 2, O);
593
0
    SStream_concat0(O, "(");
594
0
    printOperand(MI, 1, O);
595
0
    SStream_concat0(O, ")");
596
0
    return;
597
0
  }
598
599
  // Check if the last operand is an expression with the variant kind
600
  // VK_PPC_PCREL_OPT. If this is the case then this is a linker optimization
601
  // relocation and the .reloc directive needs to be added.
602
16.7k
  unsigned LastOp = MCInst_getNumOperands(MI) - 1;
603
16.7k
  if (MCInst_getNumOperands(MI) > 1) {
604
16.3k
    MCOperand *Operand = MCInst_getOperand(MI, (LastOp));
605
16.3k
    if (MCOperand_isExpr(Operand)) {
606
0
      CS_ASSERT_RET(0 && "Expressions not supported.");
607
0
    }
608
16.3k
  }
609
610
  // Check for slwi/srwi mnemonics.
611
16.7k
  if (MCInst_getOpcode(MI) == PPC_RLWINM) {
612
178
    unsigned char SH = MCOperand_getImm(MCInst_getOperand(MI, (2)));
613
178
    unsigned char MB = MCOperand_getImm(MCInst_getOperand(MI, (3)));
614
178
    unsigned char ME = MCOperand_getImm(MCInst_getOperand(MI, (4)));
615
178
    bool useSubstituteMnemonic = false;
616
178
    if (SH <= 31 && MB == 0 && ME == (31 - SH)) {
617
50
      SStream_concat0(O, "slwi ");
618
50
      useSubstituteMnemonic = true;
619
50
    }
620
178
    if (SH <= 31 && MB == (32 - SH) && ME == 31) {
621
1
      SStream_concat0(O, "srwi ");
622
1
      useSubstituteMnemonic = true;
623
1
      SH = 32 - SH;
624
1
    }
625
178
    useAliasDetails |= map_use_alias_details(MI);
626
178
    map_set_fill_detail_ops(MI, useAliasDetails &&
627
178
                useSubstituteMnemonic);
628
178
    if (useSubstituteMnemonic) {
629
51
      isAlias |= true;
630
51
      MCInst_setIsAlias(MI, isAlias);
631
632
51
      printOperand(MI, 0, O);
633
51
      SStream_concat0(O, ", ");
634
51
      printOperand(MI, 1, O);
635
51
      SStream_concat(O, "%s", ", ");
636
51
      printUInt32(O, (unsigned int)SH);
637
51
      PPC_insert_detail_op_imm_at(MI, 2, SH, CS_AC_READ);
638
639
51
      if (useAliasDetails)
640
51
        return;
641
51
    }
642
178
  }
643
644
16.6k
  if (MCInst_getOpcode(MI) == PPC_RLDICR ||
645
16.6k
      MCInst_getOpcode(MI) == PPC_RLDICR_32) {
646
92
    unsigned char SH = MCOperand_getImm(MCInst_getOperand(MI, (2)));
647
92
    unsigned char ME = MCOperand_getImm(MCInst_getOperand(MI, (3)));
648
649
92
    useAliasDetails |= map_use_alias_details(MI);
650
92
    map_set_fill_detail_ops(MI, useAliasDetails && 63 - SH == ME);
651
    // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
652
92
    if (63 - SH == ME) {
653
35
      isAlias |= true;
654
35
      MCInst_setIsAlias(MI, isAlias);
655
35
      SStream_concat0(O, "sldi ");
656
35
      printOperand(MI, 0, O);
657
35
      SStream_concat0(O, ", ");
658
35
      printOperand(MI, 1, O);
659
35
      SStream_concat(O, "%s", ", ");
660
35
      printUInt32(O, (unsigned int)SH);
661
35
      PPC_insert_detail_op_imm_at(MI, 2, SH, CS_AC_READ);
662
663
35
      if (useAliasDetails)
664
35
        return;
665
35
    }
666
92
  }
667
668
  // dcbt[st] is printed manually here because:
669
  //  1. The assembly syntax is different between embedded and server targets
670
  //  2. We must print the short mnemonics for TH == 0 because the
671
  //     embedded/server syntax default will not be stable across assemblers
672
  //  The syntax for dcbt is:
673
  //    dcbt ra, rb, th [server]
674
  //    dcbt th, ra, rb [embedded]
675
  //  where th can be omitted when it is 0. dcbtst is the same.
676
  // On AIX, only emit the extended mnemonics for dcbt and dcbtst if
677
  // the "modern assembler" is available.
678
16.6k
  if ((MCInst_getOpcode(MI) == PPC_DCBT ||
679
16.5k
       MCInst_getOpcode(MI) == PPC_DCBTST) &&
680
129
      (!PPC_getFeatureBits(MI->csh->mode, PPC_FeatureModernAIXAs))) {
681
129
    unsigned char TH = MCOperand_getImm(MCInst_getOperand(MI, (0)));
682
129
    SStream_concat0(O, "dcbt");
683
129
    if (MCInst_getOpcode(MI) == PPC_DCBTST)
684
19
      SStream_concat0(O, "st");
685
129
    if (TH == 16)
686
85
      SStream_concat0(O, "t");
687
129
    SStream_concat0(O, " ");
688
689
129
    bool IsBookE =
690
129
      PPC_getFeatureBits(MI->csh->mode, PPC_FeatureBookE);
691
129
    if (IsBookE && TH != 0 && TH != 16) {
692
0
      printUInt32(O, (unsigned int)TH);
693
0
      SStream_concat0(O, ", ");
694
0
      PPC_set_detail_op_imm(MI, 0, TH);
695
0
    }
696
129
    set_mem_access(MI, true);
697
129
    printOperand(MI, 1, O);
698
129
    SStream_concat0(O, ", ");
699
129
    printOperand(MI, 2, O);
700
129
    set_mem_access(MI, false);
701
702
129
    if (!IsBookE && TH != 0 && TH != 16) {
703
17
      SStream_concat(O, "%s", ", ");
704
17
      printUInt32(O, (unsigned int)TH);
705
17
      PPC_set_detail_op_imm(MI, 0, TH);
706
17
    }
707
708
129
    return;
709
129
  }
710
711
16.5k
  if (MCInst_getOpcode(MI) == PPC_DCBF) {
712
103
    unsigned char L = MCOperand_getImm(MCInst_getOperand(MI, (0)));
713
103
    if (!L || L == 1 || L == 3 || L == 4 || L == 6) {
714
83
      SStream_concat0(O, "dcb");
715
83
      if (L != 6)
716
54
        SStream_concat0(O, "f");
717
83
      if (L == 1)
718
3
        SStream_concat0(O, "l");
719
83
      if (L == 3)
720
18
        SStream_concat0(O, "lp");
721
83
      if (L == 4)
722
10
        SStream_concat0(O, "ps");
723
83
      if (L == 6)
724
29
        SStream_concat0(O, "stps");
725
83
      SStream_concat0(O, " ");
726
727
83
      printOperand(MI, 1, O);
728
83
      SStream_concat0(O, ", ");
729
83
      printOperand(MI, 2, O);
730
731
83
      return;
732
83
    }
733
103
  }
734
735
  // isAlias/useAliasDetails could have been set before.
736
16.4k
  useAliasDetails |= map_use_alias_details(MI);
737
16.4k
  map_set_fill_detail_ops(MI, useAliasDetails);
738
16.4k
  isAlias |= printAliasInstr(MI, Address, O);
739
16.4k
  MCInst_setIsAlias(MI, isAlias);
740
741
16.4k
  if (!isAlias || !useAliasDetails) {
742
13.9k
    map_set_fill_detail_ops(MI, true);
743
13.9k
    if (isAlias)
744
0
      SStream_Close(O);
745
13.9k
    printInstruction(MI, Address, O);
746
13.9k
    if (isAlias)
747
0
      SStream_Open(O);
748
13.9k
  }
749
16.4k
}
750
751
const char *PPC_LLVM_getRegisterName(unsigned RegNo)
752
14.1k
{
753
14.1k
  return getRegisterName(RegNo);
754
14.1k
}
755
756
void PPC_LLVM_printInst(MCInst *MI, uint64_t Address, const char *Annot,
757
      SStream *O)
758
16.7k
{
759
16.7k
  printInst(MI, Address, Annot, O);
760
16.7k
}