Coverage Report

Created: 2025-10-10 06:20

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
45.1k
{
68
45.1k
  if ((MI->csh->syntax & CS_OPT_SYNTAX_NOREGNAME) ||
69
45.1k
      !(MI->csh->syntax & CS_OPT_SYNTAX_PERCENT) ||
70
0
      PPC_getFeatureBits(MI->csh->mode, PPC_FeatureModernAIXAs))
71
45.1k
    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
45.1k
{
91
45.1k
  if (MI->csh->syntax & CS_OPT_SYNTAX_NOREGNAME)
92
0
    return NULL;
93
45.1k
  if (RegNum < PPC_CR0EQ || RegNum > PPC_CR7UN)
94
40.6k
    return NULL;
95
4.47k
  const char *CRBits[] = {
96
4.47k
    "lt",     "gt", "eq",     "un", "4*cr1+lt",
97
4.47k
    "4*cr1+gt", "4*cr1+eq", "4*cr1+un", "4*cr2+lt", "4*cr2+gt",
98
4.47k
    "4*cr2+eq", "4*cr2+un", "4*cr3+lt", "4*cr3+gt", "4*cr3+eq",
99
4.47k
    "4*cr3+un", "4*cr4+lt", "4*cr4+gt", "4*cr4+eq", "4*cr4+un",
100
4.47k
    "4*cr5+lt", "4*cr5+gt", "4*cr5+eq", "4*cr5+un", "4*cr6+lt",
101
4.47k
    "4*cr6+gt", "4*cr6+eq", "4*cr6+un", "4*cr7+lt", "4*cr7+gt",
102
4.47k
    "4*cr7+eq", "4*cr7+un"
103
4.47k
  };
104
4.47k
  return CRBits[RegEncoding];
105
45.1k
}
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
45.1k
{
111
45.1k
  return !(MI->csh->syntax & CS_OPT_SYNTAX_NOREGNAME);
112
45.1k
}
113
114
static inline void printOperand(MCInst *MI, unsigned OpNo, SStream *O)
115
45.7k
{
116
45.7k
  PPC_add_cs_detail_0(MI, PPC_OP_GROUP_Operand, OpNo);
117
45.7k
  MCOperand *Op = MCInst_getOperand(MI, (OpNo));
118
45.7k
  if (MCOperand_isReg(Op)) {
119
45.1k
    unsigned Reg = MCOperand_getReg(Op);
120
45.1k
    if (!MI->csh->ShowVSRNumsAsVR)
121
45.1k
      Reg = PPCInstrInfo_getRegNumForOperand(
122
45.1k
        MCInstrDesc_get(MCInst_getOpcode(MI),
123
45.1k
            PPCDescs.Insts,
124
45.1k
            ARR_SIZE(PPCDescs.Insts)),
125
45.1k
        Reg, OpNo);
126
127
45.1k
    const char *RegName;
128
45.1k
    RegName = getVerboseConditionRegName(
129
45.1k
      MI, Reg, MI->MRI->RegEncodingTable[Reg]);
130
45.1k
    if (RegName == NULL)
131
40.6k
      RegName = getRegisterName(Reg);
132
45.1k
    if (showRegistersWithPercentPrefix(MI, RegName))
133
0
      SStream_concat0(O, "%");
134
45.1k
    if (!showRegistersWithPrefix(MI))
135
0
      RegName = PPCRegisterInfo_stripRegisterPrefix(RegName);
136
137
45.1k
    SStream_concat0(O, RegName);
138
45.1k
    return;
139
45.1k
  }
140
141
674
  if (MCOperand_isImm(Op)) {
142
674
    printInt64(O, MCOperand_getImm(Op));
143
674
    return;
144
674
  }
145
674
}
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
1.07k
{
259
1.07k
  PPC_add_cs_detail_0(MI, PPC_OP_GROUP_U1ImmOperand, OpNo);
260
1.07k
  unsigned int Value = MCOperand_getImm(MCInst_getOperand(MI, (OpNo)));
261
1.07k
  CS_ASSERT(Value <= 1 && "Invalid u1imm argument!");
262
1.07k
  printUInt32(O, (unsigned int)Value);
263
1.07k
}
264
265
static inline void printU2ImmOperand(MCInst *MI, unsigned OpNo, SStream *O)
266
643
{
267
643
  PPC_add_cs_detail_0(MI, PPC_OP_GROUP_U2ImmOperand, OpNo);
268
643
  unsigned int Value = MCOperand_getImm(MCInst_getOperand(MI, (OpNo)));
269
643
  CS_ASSERT(Value <= 3 && "Invalid u2imm argument!");
270
643
  printUInt32(O, (unsigned int)Value);
271
643
}
272
273
static inline void printU3ImmOperand(MCInst *MI, unsigned OpNo, SStream *O)
274
730
{
275
730
  PPC_add_cs_detail_0(MI, PPC_OP_GROUP_U3ImmOperand, OpNo);
276
730
  unsigned int Value = MCOperand_getImm(MCInst_getOperand(MI, (OpNo)));
277
730
  CS_ASSERT(Value <= 8 && "Invalid u3imm argument!");
278
730
  printUInt32(O, (unsigned int)Value);
279
730
}
280
281
static inline void printU4ImmOperand(MCInst *MI, unsigned OpNo, SStream *O)
282
534
{
283
534
  PPC_add_cs_detail_0(MI, PPC_OP_GROUP_U4ImmOperand, OpNo);
284
534
  unsigned int Value = MCOperand_getImm(MCInst_getOperand(MI, (OpNo)));
285
534
  CS_ASSERT(Value <= 15 && "Invalid u4imm argument!");
286
534
  printUInt32(O, (unsigned int)Value);
287
534
}
288
289
static inline void printS5ImmOperand(MCInst *MI, unsigned OpNo, SStream *O)
290
95
{
291
95
  PPC_add_cs_detail_0(MI, PPC_OP_GROUP_S5ImmOperand, OpNo);
292
95
  int Value = MCOperand_getImm(MCInst_getOperand(MI, (OpNo)));
293
95
  Value = SignExtend32((Value), 5);
294
95
  printInt32(O, (int)Value);
295
95
}
296
297
static inline void printImmZeroOperand(MCInst *MI, unsigned OpNo, SStream *O)
298
153
{
299
153
  PPC_add_cs_detail_0(MI, PPC_OP_GROUP_ImmZeroOperand, OpNo);
300
153
  unsigned int Value = MCOperand_getImm(MCInst_getOperand(MI, (OpNo)));
301
153
  CS_ASSERT(Value == 0 && "Operand must be zero");
302
153
  printUInt32(O, (unsigned int)Value);
303
153
}
304
305
static inline void printU5ImmOperand(MCInst *MI, unsigned OpNo, SStream *O)
306
3.34k
{
307
3.34k
  PPC_add_cs_detail_0(MI, PPC_OP_GROUP_U5ImmOperand, OpNo);
308
3.34k
  unsigned int Value = MCOperand_getImm(MCInst_getOperand(MI, (OpNo)));
309
3.34k
  CS_ASSERT(Value <= 31 && "Invalid u5imm argument!");
310
3.34k
  printUInt32(O, (unsigned int)Value);
311
3.34k
}
312
313
static inline void printU6ImmOperand(MCInst *MI, unsigned OpNo, SStream *O)
314
919
{
315
919
  PPC_add_cs_detail_0(MI, PPC_OP_GROUP_U6ImmOperand, OpNo);
316
919
  unsigned int Value = MCOperand_getImm(MCInst_getOperand(MI, (OpNo)));
317
919
  CS_ASSERT(Value <= 63 && "Invalid u6imm argument!");
318
919
  printUInt32(O, (unsigned int)Value);
319
919
}
320
321
static inline void printU7ImmOperand(MCInst *MI, unsigned OpNo, SStream *O)
322
133
{
323
133
  PPC_add_cs_detail_0(MI, PPC_OP_GROUP_U7ImmOperand, OpNo);
324
133
  unsigned int Value = MCOperand_getImm(MCInst_getOperand(MI, (OpNo)));
325
133
  CS_ASSERT(Value <= 127 && "Invalid u7imm argument!");
326
133
  printUInt32(O, (unsigned int)Value);
327
133
}
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
7
{
334
7
  PPC_add_cs_detail_0(MI, PPC_OP_GROUP_U8ImmOperand, OpNo);
335
7
  unsigned char Value = MCOperand_getImm(MCInst_getOperand(MI, (OpNo)));
336
7
  CS_ASSERT(Value <= 255 && "Invalid u8imm argument!");
337
7
  printUInt32(O, (unsigned int)Value);
338
7
}
339
340
static inline void printU10ImmOperand(MCInst *MI, unsigned OpNo, SStream *O)
341
8
{
342
8
  PPC_add_cs_detail_0(MI, PPC_OP_GROUP_U10ImmOperand, OpNo);
343
8
  unsigned short Value = MCOperand_getImm(MCInst_getOperand(MI, (OpNo)));
344
8
  CS_ASSERT(Value <= 1023 && "Invalid u10imm argument!");
345
8
  printUInt32(O, (unsigned short)Value);
346
8
}
347
348
static inline void printU12ImmOperand(MCInst *MI, unsigned OpNo, SStream *O)
349
68
{
350
68
  PPC_add_cs_detail_0(MI, PPC_OP_GROUP_U12ImmOperand, OpNo);
351
68
  unsigned short Value = MCOperand_getImm(MCInst_getOperand(MI, (OpNo)));
352
68
  CS_ASSERT(Value <= 4095 && "Invalid u12imm argument!");
353
68
  printUInt32(O, (unsigned short)Value);
354
68
}
355
356
static inline void printS12ImmOperand(MCInst *MI, unsigned OpNo, SStream *O)
357
159
{
358
159
  PPC_add_cs_detail_0(MI, PPC_OP_GROUP_S12ImmOperand, OpNo);
359
159
  if (MCOperand_isImm(MCInst_getOperand(MI, OpNo))) {
360
159
    int Imm = (int)MCOperand_getImm(MCInst_getOperand(MI, OpNo));
361
159
    Imm = SignExtend32(Imm, 12);
362
159
    printInt32(O, Imm);
363
159
  } else
364
0
    printOperand(MI, OpNo, O);
365
159
}
366
367
static inline void printMemRegImmPS(MCInst *MI, unsigned OpNo, SStream *O)
368
159
{
369
159
  set_mem_access(MI, true);
370
371
159
  printS12ImmOperand(MI, OpNo, O);
372
159
  SStream_concat0(O, "(");
373
159
  printOperand(MI, OpNo + 1, O);
374
159
  SStream_concat0(O, ")");
375
376
159
  set_mem_access(MI, false);
377
159
}
378
379
static inline void printS16ImmOperand(MCInst *MI, unsigned OpNo, SStream *O)
380
5.98k
{
381
5.98k
  PPC_add_cs_detail_0(MI, PPC_OP_GROUP_S16ImmOperand, OpNo);
382
5.98k
  if (MCOperand_isImm(MCInst_getOperand(MI, (OpNo))))
383
5.98k
    printInt32(O, (short)MCOperand_getImm(
384
5.98k
              MCInst_getOperand(MI, (OpNo))));
385
0
  else
386
0
    printOperand(MI, OpNo, O);
387
5.98k
}
388
389
static inline void printS34ImmOperand(MCInst *MI, unsigned OpNo, SStream *O)
390
305
{
391
305
  PPC_add_cs_detail_0(MI, PPC_OP_GROUP_S34ImmOperand, OpNo);
392
305
  if (MCOperand_isImm(MCInst_getOperand(MI, (OpNo)))) {
393
305
    long long Value =
394
305
      MCOperand_getImm(MCInst_getOperand(MI, (OpNo)));
395
396
305
    printInt64(O, (long long)Value);
397
305
  } else
398
0
    printOperand(MI, OpNo, O);
399
305
}
400
401
static inline void printU16ImmOperand(MCInst *MI, unsigned OpNo, SStream *O)
402
1.11k
{
403
1.11k
  PPC_add_cs_detail_0(MI, PPC_OP_GROUP_U16ImmOperand, OpNo);
404
1.11k
  if (MCOperand_isImm(MCInst_getOperand(MI, (OpNo))))
405
1.11k
    printUInt32(O, (unsigned short)MCOperand_getImm(
406
1.11k
               MCInst_getOperand(MI, (OpNo))));
407
0
  else
408
0
    printOperand(MI, OpNo, O);
409
1.11k
}
410
411
static inline void printBranchOperand(MCInst *MI, uint64_t Address,
412
              unsigned OpNo, SStream *O)
413
2.36k
{
414
2.36k
  PPC_add_cs_detail_0(MI, PPC_OP_GROUP_BranchOperand, OpNo);
415
2.36k
  if (!MCOperand_isImm(MCInst_getOperand(MI, (OpNo)))) {
416
0
    printOperand(MI, OpNo, O);
417
0
    return;
418
0
  }
419
2.36k
  int32_t Imm = SignExtend32(
420
2.36k
    ((unsigned)MCOperand_getImm(MCInst_getOperand(MI, (OpNo)))
421
2.36k
     << 2),
422
2.36k
    32);
423
2.36k
  if (MI->csh->PrintBranchImmAsAddress) {
424
2.36k
    uint64_t Target = Address + Imm;
425
2.36k
    if (!IS_64BIT(MI->csh->mode))
426
577
      Target &= 0xffffffff;
427
2.36k
    printUInt64(O, (Target));
428
2.36k
  } 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
2.36k
}
442
443
static inline void printAbsBranchOperand(MCInst *MI, unsigned OpNo, SStream *O)
444
1.93k
{
445
1.93k
  PPC_add_cs_detail_0(MI, PPC_OP_GROUP_AbsBranchOperand, OpNo);
446
1.93k
  if (!MCOperand_isImm(MCInst_getOperand(MI, (OpNo)))) {
447
0
    printOperand(MI, OpNo, O);
448
0
    return;
449
0
  }
450
451
1.93k
  printUInt64(O,
452
1.93k
        ((unsigned)MCOperand_getImm(MCInst_getOperand(MI, (OpNo)))
453
1.93k
         << 2));
454
1.93k
}
455
456
static inline void printcrbitm(MCInst *MI, unsigned OpNo, SStream *O)
457
1.08k
{
458
1.08k
  PPC_add_cs_detail_0(MI, PPC_OP_GROUP_crbitm, OpNo);
459
1.08k
  unsigned CCReg = MCOperand_getReg(MCInst_getOperand(MI, (OpNo)));
460
1.08k
  unsigned RegNo;
461
1.08k
  switch (CCReg) {
462
0
  default:
463
0
    CS_ASSERT_RET(0 && "Unknown CR register");
464
171
  case PPC_CR0:
465
171
    RegNo = 0;
466
171
    break;
467
97
  case PPC_CR1:
468
97
    RegNo = 1;
469
97
    break;
470
196
  case PPC_CR2:
471
196
    RegNo = 2;
472
196
    break;
473
19
  case PPC_CR3:
474
19
    RegNo = 3;
475
19
    break;
476
185
  case PPC_CR4:
477
185
    RegNo = 4;
478
185
    break;
479
208
  case PPC_CR5:
480
208
    RegNo = 5;
481
208
    break;
482
121
  case PPC_CR6:
483
121
    RegNo = 6;
484
121
    break;
485
87
  case PPC_CR7:
486
87
    RegNo = 7;
487
87
    break;
488
1.08k
  }
489
1.08k
  printUInt32(O, (0x80 >> RegNo));
490
1.08k
}
491
492
static inline void printMemRegImm(MCInst *MI, unsigned OpNo, SStream *O)
493
9.75k
{
494
9.75k
  set_mem_access(MI, true);
495
9.75k
  PPC_add_cs_detail_0(MI, PPC_OP_GROUP_MemRegImm, OpNo);
496
9.75k
  printS16ImmOperand(MI, OpNo, O);
497
9.75k
  SStream_concat0(O, "(");
498
499
9.75k
  if (MCOperand_getReg(MCInst_getOperand(MI, (OpNo + 1))) == PPC_R0)
500
0
    SStream_concat0(O, "0");
501
9.75k
  else
502
9.75k
    printOperand(MI, OpNo + 1, O);
503
9.75k
  SStream_concat0(O, ")");
504
9.75k
  set_mem_access(MI, false);
505
9.75k
}
506
507
static inline void printMemRegImmHash(MCInst *MI, unsigned OpNo, SStream *O)
508
13
{
509
13
  set_mem_access(MI, true);
510
13
  PPC_add_cs_detail_0(MI, PPC_OP_GROUP_MemRegImmHash, OpNo);
511
13
  printInt32(O, MCOperand_getImm(MCInst_getOperand(MI, (OpNo))));
512
13
  SStream_concat0(O, "(");
513
514
13
  printOperand(MI, OpNo + 1, O);
515
13
  SStream_concat0(O, ")");
516
13
  set_mem_access(MI, false);
517
13
}
518
519
static inline void printMemRegImm34PCRel(MCInst *MI, unsigned OpNo, SStream *O)
520
144
{
521
144
  set_mem_access(MI, true);
522
144
  PPC_add_cs_detail_0(MI, PPC_OP_GROUP_MemRegImm34PCRel, OpNo);
523
144
  printS34ImmOperand(MI, OpNo, O);
524
144
  SStream_concat0(O, "(");
525
526
144
  printImmZeroOperand(MI, OpNo + 1, O);
527
144
  SStream_concat0(O, ")");
528
144
  set_mem_access(MI, false);
529
144
}
530
531
static inline void printMemRegImm34(MCInst *MI, unsigned OpNo, SStream *O)
532
80
{
533
80
  set_mem_access(MI, true);
534
80
  PPC_add_cs_detail_0(MI, PPC_OP_GROUP_MemRegImm34, OpNo);
535
80
  printS34ImmOperand(MI, OpNo, O);
536
80
  SStream_concat0(O, "(");
537
538
80
  printOperand(MI, OpNo + 1, O);
539
80
  SStream_concat0(O, ")");
540
80
  set_mem_access(MI, false);
541
80
}
542
543
static inline void printMemRegReg(MCInst *MI, unsigned OpNo, SStream *O)
544
2.82k
{
545
2.82k
  set_mem_access(MI, true);
546
2.82k
  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
2.82k
  if (MCOperand_getReg(MCInst_getOperand(MI, (OpNo))) == PPC_R0)
551
0
    SStream_concat0(O, "0");
552
2.82k
  else
553
2.82k
    printOperand(MI, OpNo, O);
554
2.82k
  SStream_concat0(O, ", ");
555
2.82k
  printOperand(MI, OpNo + 1, O);
556
2.82k
  set_mem_access(MI, false);
557
2.82k
}
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
23.7k
{
579
23.7k
  bool isAlias = false;
580
23.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
23.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
23.7k
  unsigned LastOp = MCInst_getNumOperands(MI) - 1;
603
23.7k
  if (MCInst_getNumOperands(MI) > 1) {
604
22.8k
    MCOperand *Operand = MCInst_getOperand(MI, (LastOp));
605
22.8k
    if (MCOperand_isExpr(Operand)) {
606
0
      CS_ASSERT_RET(0 && "Expressions not supported.");
607
0
    }
608
22.8k
  }
609
610
  // Check for slwi/srwi mnemonics.
611
23.7k
  if (MCInst_getOpcode(MI) == PPC_RLWINM) {
612
177
    unsigned char SH = MCOperand_getImm(MCInst_getOperand(MI, (2)));
613
177
    unsigned char MB = MCOperand_getImm(MCInst_getOperand(MI, (3)));
614
177
    unsigned char ME = MCOperand_getImm(MCInst_getOperand(MI, (4)));
615
177
    bool useSubstituteMnemonic = false;
616
177
    if (SH <= 31 && MB == 0 && ME == (31 - SH)) {
617
16
      SStream_concat0(O, "slwi ");
618
16
      useSubstituteMnemonic = true;
619
16
    }
620
177
    if (SH <= 31 && MB == (32 - SH) && ME == 31) {
621
24
      SStream_concat0(O, "srwi ");
622
24
      useSubstituteMnemonic = true;
623
24
      SH = 32 - SH;
624
24
    }
625
177
    useAliasDetails |= map_use_alias_details(MI);
626
177
    map_set_fill_detail_ops(MI, useAliasDetails &&
627
177
                useSubstituteMnemonic);
628
177
    if (useSubstituteMnemonic) {
629
40
      isAlias |= true;
630
40
      MCInst_setIsAlias(MI, isAlias);
631
632
40
      printOperand(MI, 0, O);
633
40
      SStream_concat0(O, ", ");
634
40
      printOperand(MI, 1, O);
635
40
      SStream_concat(O, "%s", ", ");
636
40
      printUInt32(O, (unsigned int)SH);
637
40
      PPC_insert_detail_op_imm_at(MI, 2, SH, CS_AC_READ);
638
639
40
      if (useAliasDetails)
640
40
        return;
641
40
    }
642
177
  }
643
644
23.7k
  if (MCInst_getOpcode(MI) == PPC_RLDICR ||
645
23.7k
      MCInst_getOpcode(MI) == PPC_RLDICR_32) {
646
31
    unsigned char SH = MCOperand_getImm(MCInst_getOperand(MI, (2)));
647
31
    unsigned char ME = MCOperand_getImm(MCInst_getOperand(MI, (3)));
648
649
31
    useAliasDetails |= map_use_alias_details(MI);
650
31
    map_set_fill_detail_ops(MI, useAliasDetails && 63 - SH == ME);
651
    // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
652
31
    if (63 - SH == ME) {
653
14
      isAlias |= true;
654
14
      MCInst_setIsAlias(MI, isAlias);
655
14
      SStream_concat0(O, "sldi ");
656
14
      printOperand(MI, 0, O);
657
14
      SStream_concat0(O, ", ");
658
14
      printOperand(MI, 1, O);
659
14
      SStream_concat(O, "%s", ", ");
660
14
      printUInt32(O, (unsigned int)SH);
661
14
      PPC_insert_detail_op_imm_at(MI, 2, SH, CS_AC_READ);
662
663
14
      if (useAliasDetails)
664
14
        return;
665
14
    }
666
31
  }
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
23.7k
  if ((MCInst_getOpcode(MI) == PPC_DCBT ||
679
23.7k
       MCInst_getOpcode(MI) == PPC_DCBTST) &&
680
66
      (!PPC_getFeatureBits(MI->csh->mode, PPC_FeatureModernAIXAs))) {
681
66
    unsigned char TH = MCOperand_getImm(MCInst_getOperand(MI, (0)));
682
66
    SStream_concat0(O, "dcbt");
683
66
    if (MCInst_getOpcode(MI) == PPC_DCBTST)
684
63
      SStream_concat0(O, "st");
685
66
    if (TH == 16)
686
27
      SStream_concat0(O, "t");
687
66
    SStream_concat0(O, " ");
688
689
66
    bool IsBookE =
690
66
      PPC_getFeatureBits(MI->csh->mode, PPC_FeatureBookE);
691
66
    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
66
    set_mem_access(MI, true);
697
66
    printOperand(MI, 1, O);
698
66
    SStream_concat0(O, ", ");
699
66
    printOperand(MI, 2, O);
700
66
    set_mem_access(MI, false);
701
702
66
    if (!IsBookE && TH != 0 && TH != 16) {
703
38
      SStream_concat(O, "%s", ", ");
704
38
      printUInt32(O, (unsigned int)TH);
705
38
      PPC_set_detail_op_imm(MI, 0, TH);
706
38
    }
707
708
66
    return;
709
66
  }
710
711
23.6k
  if (MCInst_getOpcode(MI) == PPC_DCBF) {
712
318
    unsigned char L = MCOperand_getImm(MCInst_getOperand(MI, (0)));
713
318
    if (!L || L == 1 || L == 3 || L == 4 || L == 6) {
714
200
      SStream_concat0(O, "dcb");
715
200
      if (L != 6)
716
64
        SStream_concat0(O, "f");
717
200
      if (L == 1)
718
6
        SStream_concat0(O, "l");
719
200
      if (L == 3)
720
10
        SStream_concat0(O, "lp");
721
200
      if (L == 4)
722
13
        SStream_concat0(O, "ps");
723
200
      if (L == 6)
724
136
        SStream_concat0(O, "stps");
725
200
      SStream_concat0(O, " ");
726
727
200
      printOperand(MI, 1, O);
728
200
      SStream_concat0(O, ", ");
729
200
      printOperand(MI, 2, O);
730
731
200
      return;
732
200
    }
733
318
  }
734
735
  // isAlias/useAliasDetails could have been set before.
736
23.4k
  useAliasDetails |= map_use_alias_details(MI);
737
23.4k
  map_set_fill_detail_ops(MI, useAliasDetails);
738
23.4k
  isAlias |= printAliasInstr(MI, Address, O);
739
23.4k
  MCInst_setIsAlias(MI, isAlias);
740
741
23.4k
  if (!isAlias || !useAliasDetails) {
742
19.6k
    map_set_fill_detail_ops(MI, true);
743
19.6k
    if (isAlias)
744
0
      SStream_Close(O);
745
19.6k
    printInstruction(MI, Address, O);
746
19.6k
    if (isAlias)
747
0
      SStream_Open(O);
748
19.6k
  }
749
23.4k
}
750
751
const char *PPC_LLVM_getRegisterName(unsigned RegNo)
752
20.2k
{
753
20.2k
  return getRegisterName(RegNo);
754
20.2k
}
755
756
void PPC_LLVM_printInst(MCInst *MI, uint64_t Address, const char *Annot,
757
      SStream *O)
758
23.7k
{
759
23.7k
  printInst(MI, Address, Annot, O);
760
23.7k
}