Coverage Report

Created: 2025-08-29 06:29

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