Coverage Report

Created: 2025-07-11 06:32

/src/capstonenext/arch/Sparc/SparcInstPrinter.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
//===-- SparcInstPrinter.cpp - Convert Sparc 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 Sparc MCInst to a .s file.
24
//
25
//===----------------------------------------------------------------------===//
26
27
#include <stdio.h>
28
#include <string.h>
29
#include <stdlib.h>
30
#include <capstone/platform.h>
31
32
#include "../../MCInstPrinter.h"
33
#include "../../Mapping.h"
34
#include "SparcInstPrinter.h"
35
#include "SparcLinkage.h"
36
#include "SparcMCTargetDesc.h"
37
#include "SparcMapping.h"
38
#include "SparcDisassemblerExtension.h"
39
40
#define CONCAT(a, b) CONCAT_(a, b)
41
#define CONCAT_(a, b) a##_##b
42
43
#define DEBUG_TYPE "asm-printer"
44
45
static void printCustomAliasOperand(
46
         MCInst *MI, uint64_t Address, unsigned OpIdx,
47
         unsigned PrintMethodIdx,
48
         SStream *OS);
49
static void printOperand(MCInst *MI, int opNum, SStream *O);
50
51
#define GET_INSTRUCTION_NAME
52
#define PRINT_ALIAS_INSTR
53
#include "SparcGenAsmWriter.inc"
54
55
static void printRegName(SStream *OS, MCRegister Reg)
56
70.3k
{
57
70.3k
  SStream_concat1(OS, '%');
58
70.3k
  SStream_concat0(OS, getRegisterName(Reg, Sparc_NoRegAltName));
59
70.3k
}
60
61
static void printRegNameAlt(SStream *OS, MCRegister Reg, unsigned AltIdx)
62
33.2k
{
63
33.2k
  SStream_concat1(OS, '%');
64
33.2k
  SStream_concat0(OS, getRegisterName(Reg, AltIdx));
65
33.2k
}
66
67
static void printInst(MCInst *MI, uint64_t Address, SStream *O)
68
38.7k
{
69
38.7k
  bool isAlias = false;
70
38.7k
  bool useAliasDetails = map_use_alias_details(MI);
71
38.7k
  map_set_fill_detail_ops(MI, useAliasDetails);
72
73
38.7k
  if (!printAliasInstr(MI, Address, O) &&
74
38.7k
    !printSparcAliasInstr(MI, O)) {
75
33.2k
    MCInst_setIsAlias(MI, false);
76
33.2k
  } else {
77
5.49k
    isAlias = true;
78
5.49k
    MCInst_setIsAlias(MI, isAlias);
79
5.49k
    if (useAliasDetails) {
80
5.49k
      return;
81
5.49k
    }
82
5.49k
  }
83
84
33.2k
  if (!isAlias || !useAliasDetails) {
85
33.2k
    map_set_fill_detail_ops(MI, !(isAlias && useAliasDetails));
86
33.2k
    if (isAlias)
87
0
      SStream_Close(O);
88
33.2k
    printInstruction(MI, Address, O);
89
33.2k
    if (isAlias)
90
0
      SStream_Open(O);
91
33.2k
  }
92
33.2k
}
93
94
bool printSparcAliasInstr(MCInst *MI, SStream *O)
95
33.5k
{
96
33.5k
  switch (MCInst_getOpcode(MI)) {
97
32.6k
  default:
98
32.6k
    return false;
99
43
  case Sparc_JMPLrr:
100
261
  case Sparc_JMPLri: {
101
261
    if (MCInst_getNumOperands(MI) != 3)
102
0
      return false;
103
261
    if (!MCOperand_isReg(MCInst_getOperand(MI, (0))))
104
0
      return false;
105
261
    switch (MCOperand_getReg(MCInst_getOperand(MI, (0)))) {
106
40
    default:
107
40
      return false;
108
187
    case Sparc_G0: // jmp $addr | ret | retl
109
187
      if (MCOperand_isImm(MCInst_getOperand(MI, (2))) &&
110
187
          MCOperand_getImm(MCInst_getOperand(MI, (2))) == 8) {
111
104
        switch (MCOperand_getReg(
112
104
          MCInst_getOperand(MI, (1)))) {
113
17
        default:
114
17
          break;
115
22
        case Sparc_I7:
116
22
          SStream_concat0(O, "\tret");
117
22
          return true;
118
65
        case Sparc_O7:
119
65
          SStream_concat0(O, "\tretl");
120
65
          return true;
121
104
        }
122
104
      }
123
100
      SStream_concat0(O, "\tjmp ");
124
100
      printMemOperand(MI, 1, O);
125
100
      return true;
126
34
    case Sparc_O7: // call $addr
127
34
      SStream_concat0(O, "\tcall ");
128
34
      printMemOperand(MI, 1, O);
129
34
      return true;
130
261
    }
131
261
  }
132
11
  case Sparc_V9FCMPS:
133
231
  case Sparc_V9FCMPD:
134
352
  case Sparc_V9FCMPQ:
135
380
  case Sparc_V9FCMPES:
136
583
  case Sparc_V9FCMPED:
137
620
  case Sparc_V9FCMPEQ: {
138
620
    if (Sparc_getFeatureBits(MI->csh->mode, Sparc_FeatureV9) || (MCInst_getNumOperands(MI) != 3) ||
139
620
        (!MCOperand_isReg(MCInst_getOperand(MI, (0)))) ||
140
620
        (MCOperand_getReg(MCInst_getOperand(MI, (0))) != Sparc_FCC0))
141
620
      return false;
142
    // if V8, skip printing %fcc0.
143
0
    switch (MCInst_getOpcode(MI)) {
144
0
    default:
145
0
    case Sparc_V9FCMPS:
146
0
      SStream_concat0(O, "\tfcmps ");
147
0
      break;
148
0
    case Sparc_V9FCMPD:
149
0
      SStream_concat0(O, "\tfcmpd ");
150
0
      break;
151
0
    case Sparc_V9FCMPQ:
152
0
      SStream_concat0(O, "\tfcmpq ");
153
0
      break;
154
0
    case Sparc_V9FCMPES:
155
0
      SStream_concat0(O, "\tfcmpes ");
156
0
      break;
157
0
    case Sparc_V9FCMPED:
158
0
      SStream_concat0(O, "\tfcmped ");
159
0
      break;
160
0
    case Sparc_V9FCMPEQ:
161
0
      SStream_concat0(O, "\tfcmpeq ");
162
0
      break;
163
0
    }
164
0
    printOperand(MI, 1, O);
165
0
    SStream_concat0(O, ", ");
166
0
    printOperand(MI, 2, O);
167
0
    return true;
168
0
  }
169
33.5k
  }
170
33.5k
}
171
172
static void printOperand(MCInst *MI, int opNum, SStream *O)
173
70.3k
{
174
70.3k
  Sparc_add_cs_detail_0(MI, Sparc_OP_GROUP_Operand, opNum);
175
70.3k
  MCOperand *MO = MCInst_getOperand(MI, (opNum));
176
177
70.3k
  if (MCOperand_isReg(MO)) {
178
39.9k
    unsigned Reg = MCOperand_getReg(MO);
179
39.9k
    if (Sparc_getFeatureBits(MI->csh->mode, Sparc_FeatureV9))
180
33.2k
      printRegNameAlt(O, Reg, Sparc_RegNamesStateReg);
181
6.66k
    else
182
6.66k
      printRegName(O, Reg);
183
39.9k
    return;
184
39.9k
  }
185
186
30.4k
  if (MCOperand_isImm(MO)) {
187
30.4k
    switch (MCInst_getOpcode(MI)) {
188
30.2k
    default:
189
30.2k
      printInt32(O, (int)MCOperand_getImm(MO));
190
30.2k
      return;
191
192
19
    case Sparc_TICCri: // Fall through
193
19
    case Sparc_TICCrr: // Fall through
194
29
    case Sparc_TRAPri: // Fall through
195
29
    case Sparc_TRAPrr: // Fall through
196
154
    case Sparc_TXCCri: // Fall through
197
154
    case Sparc_TXCCrr: // Fall through
198
      // Only seven-bit values up to 127.
199
154
      printInt8(O, ((int)MCOperand_getImm(MO) & 0x7f));
200
154
      return;
201
30.4k
    }
202
30.4k
  }
203
204
0
  CS_ASSERT(MCOperand_isExpr(MO) &&
205
0
      "Unknown operand kind in printOperand");
206
0
}
207
208
void printMemOperand(MCInst *MI, int opNum, SStream *O)
209
8.43k
{
210
8.43k
  Sparc_add_cs_detail_0(MI, Sparc_OP_GROUP_MemOperand, opNum);
211
8.43k
  MCOperand *Op1 = MCInst_getOperand(MI, (opNum));
212
8.43k
  MCOperand *Op2 = MCInst_getOperand(MI, (opNum + 1));
213
214
8.43k
  bool PrintedFirstOperand = false;
215
8.43k
  if (MCOperand_isReg(Op1) && MCOperand_getReg(Op1) != Sparc_G0) {
216
7.85k
    printOperand(MI, opNum, O);
217
7.85k
    PrintedFirstOperand = true;
218
7.85k
  }
219
220
  // Skip the second operand iff it adds nothing (literal 0 or %g0) and we've
221
  // already printed the first one
222
8.43k
  const bool SkipSecondOperand =
223
8.43k
    PrintedFirstOperand &&
224
8.43k
    ((MCOperand_isReg(Op2) && MCOperand_getReg(Op2) == Sparc_G0) ||
225
7.85k
     (MCOperand_isImm(Op2) && MCOperand_getImm(Op2) == 0));
226
227
8.43k
  if (!SkipSecondOperand) {
228
7.57k
    if (PrintedFirstOperand)
229
6.99k
      SStream_concat0(O, "+");
230
231
7.57k
    printOperand(MI, opNum + 1, O);
232
7.57k
  }
233
8.43k
}
234
235
void printCCOperand(MCInst *MI, int opNum, SStream *O)
236
8.05k
{
237
8.05k
  Sparc_add_cs_detail_0(MI, Sparc_OP_GROUP_CCOperand, opNum);
238
8.05k
  int CC = (int)MCOperand_getImm(MCInst_getOperand(MI, (opNum)));
239
8.05k
  switch (MCInst_getOpcode(MI)) {
240
1.78k
  default:
241
1.78k
    break;
242
1.78k
  case Sparc_FBCOND:
243
1.35k
  case Sparc_FBCONDA:
244
1.59k
  case Sparc_FBCOND_V9:
245
1.71k
  case Sparc_FBCONDA_V9:
246
2.46k
  case Sparc_BPFCC:
247
2.76k
  case Sparc_BPFCCA:
248
2.76k
  case Sparc_BPFCCNT:
249
2.76k
  case Sparc_BPFCCANT:
250
2.85k
  case Sparc_MOVFCCrr:
251
2.85k
  case Sparc_V9MOVFCCrr:
252
2.93k
  case Sparc_MOVFCCri:
253
2.93k
  case Sparc_V9MOVFCCri:
254
2.96k
  case Sparc_FMOVS_FCC:
255
2.96k
  case Sparc_V9FMOVS_FCC:
256
2.97k
  case Sparc_FMOVD_FCC:
257
2.97k
  case Sparc_V9FMOVD_FCC:
258
2.98k
  case Sparc_FMOVQ_FCC:
259
2.98k
  case Sparc_V9FMOVQ_FCC:
260
    // Make sure CC is a fp conditional flag.
261
2.98k
    CC = (CC < SPARC_CC_FCC_BEGIN) ? (CC + SPARC_CC_FCC_BEGIN) : CC;
262
2.98k
    break;
263
1.02k
  case Sparc_CBCOND:
264
1.68k
  case Sparc_CBCONDA:
265
    // Make sure CC is a cp conditional flag.
266
1.68k
    CC = (CC < SPARC_CC_CPCC_BEGIN) ? (CC + SPARC_CC_CPCC_BEGIN) : CC;
267
1.68k
    break;
268
658
  case Sparc_BPR:
269
1.08k
  case Sparc_BPRA:
270
1.40k
  case Sparc_BPRNT:
271
1.47k
  case Sparc_BPRANT:
272
1.50k
  case Sparc_MOVRri:
273
1.54k
  case Sparc_MOVRrr:
274
1.56k
  case Sparc_FMOVRS:
275
1.58k
  case Sparc_FMOVRD:
276
1.60k
  case Sparc_FMOVRQ:
277
    // Make sure CC is a register conditional flag.
278
1.60k
    CC = (CC < SPARC_CC_REG_BEGIN) ? (CC + SPARC_CC_REG_BEGIN) : CC;
279
1.60k
    break;
280
8.05k
  }
281
8.05k
  SStream_concat0(O, SPARCCondCodeToString((sparc_cc)CC));
282
8.05k
}
283
284
bool printGetPCX(MCInst *MI, unsigned opNum, SStream *O)
285
0
{
286
0
  printf("FIXME: Implement SparcInstPrinter::printGetPCX.");
287
0
  return true;
288
0
}
289
290
void printMembarTag(MCInst *MI, int opNum, SStream *O)
291
121
{
292
121
  Sparc_add_cs_detail_0(MI, Sparc_OP_GROUP_MembarTag, opNum);
293
121
  static const char *const TagNames[] = { "#LoadLoad",  "#StoreLoad",
294
121
            "#LoadStore", "#StoreStore",
295
121
            "#Lookaside", "#MemIssue",
296
121
            "#Sync" };
297
298
121
  unsigned Imm = MCOperand_getImm(MCInst_getOperand(MI, (opNum)));
299
300
121
  if (Imm > 127) {
301
12
    printUInt32(O, Imm);
302
12
    return;
303
12
  }
304
305
109
  bool First = true;
306
6.21k
  for (unsigned i = 0; i < sizeof(TagNames); i++) {
307
6.10k
    if (Imm & (1ull << i)) {
308
285
      SStream_concat(O, "%s", (First ? "" : " | "));
309
285
      SStream_concat0(O, TagNames[i]);
310
285
      First = false;
311
285
    }
312
6.10k
  }
313
109
}
314
315
#define GET_ASITAG_IMPL
316
#include "SparcGenSystemOperands.inc"
317
318
void printASITag(MCInst *MI, int opNum, SStream *O)
319
2.81k
{
320
2.81k
  Sparc_add_cs_detail_0(MI, Sparc_OP_GROUP_ASITag, opNum);
321
2.81k
  unsigned Imm = MCOperand_getImm(MCInst_getOperand(MI, (opNum)));
322
2.81k
  const Sparc_ASITag_ASITag *ASITag = Sparc_ASITag_lookupASITagByEncoding(Imm);
323
2.81k
  if (Sparc_getFeatureBits(MI->csh->mode, Sparc_FeatureV9) && ASITag) {
324
147
    SStream_concat1(O, '#');
325
147
    SStream_concat0(O, ASITag->Name);
326
147
  } else
327
2.66k
    printUInt32(O, Imm);
328
2.81k
}
329
330
331
void Sparc_LLVM_printInst(MCInst *MI, uint64_t Address, const char *Annot,
332
            SStream *O)
333
38.7k
{
334
38.7k
  printInst(MI, Address, O);
335
38.7k
}
336
337
const char *Sparc_LLVM_getRegisterName(unsigned RegNo, unsigned AltIdx)
338
17.6k
{
339
17.6k
  return getRegisterName(RegNo, AltIdx);
340
17.6k
}