Coverage Report

Created: 2025-07-18 06:43

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