Coverage Report

Created: 2025-07-18 06:43

/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
64.3k
{
57
64.3k
  SStream_concat1(OS, '%');
58
64.3k
  SStream_concat0(OS, getRegisterName(Reg, Sparc_NoRegAltName));
59
64.3k
}
60
61
static void printRegNameAlt(SStream *OS, MCRegister Reg, unsigned AltIdx)
62
43.2k
{
63
43.2k
  SStream_concat1(OS, '%');
64
43.2k
  SStream_concat0(OS, getRegisterName(Reg, AltIdx));
65
43.2k
}
66
67
static void printInst(MCInst *MI, uint64_t Address, SStream *O)
68
43.8k
{
69
43.8k
  bool isAlias = false;
70
43.8k
  bool useAliasDetails = map_use_alias_details(MI);
71
43.8k
  map_set_fill_detail_ops(MI, useAliasDetails);
72
73
43.8k
  if (!printAliasInstr(MI, Address, O) &&
74
43.8k
    !printSparcAliasInstr(MI, O)) {
75
39.2k
    MCInst_setIsAlias(MI, false);
76
39.2k
  } else {
77
4.67k
    isAlias = true;
78
4.67k
    MCInst_setIsAlias(MI, isAlias);
79
4.67k
    if (useAliasDetails) {
80
4.67k
      return;
81
4.67k
    }
82
4.67k
  }
83
84
39.2k
  if (!isAlias || !useAliasDetails) {
85
39.2k
    map_set_fill_detail_ops(MI, !(isAlias && useAliasDetails));
86
39.2k
    if (isAlias)
87
0
      SStream_Close(O);
88
39.2k
    printInstruction(MI, Address, O);
89
39.2k
    if (isAlias)
90
0
      SStream_Open(O);
91
39.2k
  }
92
39.2k
}
93
94
bool printSparcAliasInstr(MCInst *MI, SStream *O)
95
39.9k
{
96
39.9k
  switch (MCInst_getOpcode(MI)) {
97
38.5k
  default:
98
38.5k
    return false;
99
41
  case Sparc_JMPLrr:
100
855
  case Sparc_JMPLri: {
101
855
    if (MCInst_getNumOperands(MI) != 3)
102
0
      return false;
103
855
    if (!MCOperand_isReg(MCInst_getOperand(MI, (0))))
104
0
      return false;
105
855
    switch (MCOperand_getReg(MCInst_getOperand(MI, (0)))) {
106
77
    default:
107
77
      return false;
108
752
    case Sparc_G0: // jmp $addr | ret | retl
109
752
      if (MCOperand_isImm(MCInst_getOperand(MI, (2))) &&
110
752
          MCOperand_getImm(MCInst_getOperand(MI, (2))) == 8) {
111
613
        switch (MCOperand_getReg(
112
613
          MCInst_getOperand(MI, (1)))) {
113
191
        default:
114
191
          break;
115
191
        case Sparc_I7:
116
10
          SStream_concat0(O, "\tret");
117
10
          return true;
118
412
        case Sparc_O7:
119
412
          SStream_concat0(O, "\tretl");
120
412
          return true;
121
613
        }
122
613
      }
123
330
      SStream_concat0(O, "\tjmp ");
124
330
      printMemOperand(MI, 1, O);
125
330
      return true;
126
26
    case Sparc_O7: // call $addr
127
26
      SStream_concat0(O, "\tcall ");
128
26
      printMemOperand(MI, 1, O);
129
26
      return true;
130
855
    }
131
855
  }
132
10
  case Sparc_V9FCMPS:
133
233
  case Sparc_V9FCMPD:
134
306
  case Sparc_V9FCMPQ:
135
338
  case Sparc_V9FCMPES:
136
545
  case Sparc_V9FCMPED:
137
563
  case Sparc_V9FCMPEQ: {
138
563
    if (Sparc_getFeatureBits(MI->csh->mode, Sparc_FeatureV9) || (MCInst_getNumOperands(MI) != 3) ||
139
563
        (!MCOperand_isReg(MCInst_getOperand(MI, (0)))) ||
140
563
        (MCOperand_getReg(MCInst_getOperand(MI, (0))) != Sparc_FCC0))
141
563
      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
39.9k
  }
170
39.9k
}
171
172
static void printOperand(MCInst *MI, int opNum, SStream *O)
173
82.3k
{
174
82.3k
  Sparc_add_cs_detail_0(MI, Sparc_OP_GROUP_Operand, opNum);
175
82.3k
  MCOperand *MO = MCInst_getOperand(MI, (opNum));
176
177
82.3k
  if (MCOperand_isReg(MO)) {
178
49.7k
    unsigned Reg = MCOperand_getReg(MO);
179
49.7k
    if (Sparc_getFeatureBits(MI->csh->mode, Sparc_FeatureV9))
180
43.2k
      printRegNameAlt(O, Reg, Sparc_RegNamesStateReg);
181
6.56k
    else
182
6.56k
      printRegName(O, Reg);
183
49.7k
    return;
184
49.7k
  }
185
186
32.6k
  if (MCOperand_isImm(MO)) {
187
32.6k
    switch (MCInst_getOpcode(MI)) {
188
32.3k
    default:
189
32.3k
      printInt32(O, (int)MCOperand_getImm(MO));
190
32.3k
      return;
191
192
70
    case Sparc_TICCri: // Fall through
193
70
    case Sparc_TICCrr: // Fall through
194
136
    case Sparc_TRAPri: // Fall through
195
136
    case Sparc_TRAPrr: // Fall through
196
241
    case Sparc_TXCCri: // Fall through
197
241
    case Sparc_TXCCrr: // Fall through
198
      // Only seven-bit values up to 127.
199
241
      printInt8(O, ((int)MCOperand_getImm(MO) & 0x7f));
200
241
      return;
201
32.6k
    }
202
32.6k
  }
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
11.7k
{
210
11.7k
  Sparc_add_cs_detail_0(MI, Sparc_OP_GROUP_MemOperand, opNum);
211
11.7k
  MCOperand *Op1 = MCInst_getOperand(MI, (opNum));
212
11.7k
  MCOperand *Op2 = MCInst_getOperand(MI, (opNum + 1));
213
214
11.7k
  bool PrintedFirstOperand = false;
215
11.7k
  if (MCOperand_isReg(Op1) && MCOperand_getReg(Op1) != Sparc_G0) {
216
11.3k
    printOperand(MI, opNum, O);
217
11.3k
    PrintedFirstOperand = true;
218
11.3k
  }
219
220
  // Skip the second operand iff it adds nothing (literal 0 or %g0) and we've
221
  // already printed the first one
222
11.7k
  const bool SkipSecondOperand =
223
11.7k
    PrintedFirstOperand &&
224
11.7k
    ((MCOperand_isReg(Op2) && MCOperand_getReg(Op2) == Sparc_G0) ||
225
11.3k
     (MCOperand_isImm(Op2) && MCOperand_getImm(Op2) == 0));
226
227
11.7k
  if (!SkipSecondOperand) {
228
10.3k
    if (PrintedFirstOperand)
229
9.97k
      SStream_concat0(O, "+");
230
231
10.3k
    printOperand(MI, opNum + 1, O);
232
10.3k
  }
233
11.7k
}
234
235
void printCCOperand(MCInst *MI, int opNum, SStream *O)
236
10.4k
{
237
10.4k
  Sparc_add_cs_detail_0(MI, Sparc_OP_GROUP_CCOperand, opNum);
238
10.4k
  int CC = (int)MCOperand_getImm(MCInst_getOperand(MI, (opNum)));
239
10.4k
  switch (MCInst_getOpcode(MI)) {
240
2.26k
  default:
241
2.26k
    break;
242
2.26k
  case Sparc_FBCOND:
243
1.67k
  case Sparc_FBCONDA:
244
1.93k
  case Sparc_FBCOND_V9:
245
2.21k
  case Sparc_FBCONDA_V9:
246
3.10k
  case Sparc_BPFCC:
247
3.60k
  case Sparc_BPFCCA:
248
3.60k
  case Sparc_BPFCCNT:
249
3.60k
  case Sparc_BPFCCANT:
250
3.71k
  case Sparc_MOVFCCrr:
251
3.71k
  case Sparc_V9MOVFCCrr:
252
3.79k
  case Sparc_MOVFCCri:
253
3.79k
  case Sparc_V9MOVFCCri:
254
3.80k
  case Sparc_FMOVS_FCC:
255
3.80k
  case Sparc_V9FMOVS_FCC:
256
3.84k
  case Sparc_FMOVD_FCC:
257
3.84k
  case Sparc_V9FMOVD_FCC:
258
3.87k
  case Sparc_FMOVQ_FCC:
259
3.87k
  case Sparc_V9FMOVQ_FCC:
260
    // Make sure CC is a fp conditional flag.
261
3.87k
    CC = (CC < SPARC_CC_FCC_BEGIN) ? (CC + SPARC_CC_FCC_BEGIN) : CC;
262
3.87k
    break;
263
1.41k
  case Sparc_CBCOND:
264
2.50k
  case Sparc_CBCONDA:
265
    // Make sure CC is a cp conditional flag.
266
2.50k
    CC = (CC < SPARC_CC_CPCC_BEGIN) ? (CC + SPARC_CC_CPCC_BEGIN) : CC;
267
2.50k
    break;
268
413
  case Sparc_BPR:
269
957
  case Sparc_BPRA:
270
1.29k
  case Sparc_BPRNT:
271
1.50k
  case Sparc_BPRANT:
272
1.55k
  case Sparc_MOVRri:
273
1.63k
  case Sparc_MOVRrr:
274
1.66k
  case Sparc_FMOVRS:
275
1.75k
  case Sparc_FMOVRD:
276
1.82k
  case Sparc_FMOVRQ:
277
    // Make sure CC is a register conditional flag.
278
1.82k
    CC = (CC < SPARC_CC_REG_BEGIN) ? (CC + SPARC_CC_REG_BEGIN) : CC;
279
1.82k
    break;
280
10.4k
  }
281
10.4k
  SStream_concat0(O, SPARCCondCodeToString((sparc_cc)CC));
282
10.4k
}
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
259
{
292
259
  Sparc_add_cs_detail_0(MI, Sparc_OP_GROUP_MembarTag, opNum);
293
259
  static const char *const TagNames[] = { "#LoadLoad",  "#StoreLoad",
294
259
            "#LoadStore", "#StoreStore",
295
259
            "#Lookaside", "#MemIssue",
296
259
            "#Sync" };
297
298
259
  unsigned Imm = MCOperand_getImm(MCInst_getOperand(MI, (opNum)));
299
300
259
  if (Imm > 127) {
301
39
    printUInt32(O, Imm);
302
39
    return;
303
39
  }
304
305
220
  bool First = true;
306
12.5k
  for (unsigned i = 0; i < sizeof(TagNames); i++) {
307
12.3k
    if (Imm & (1ull << i)) {
308
606
      SStream_concat(O, "%s", (First ? "" : " | "));
309
606
      SStream_concat0(O, TagNames[i]);
310
606
      First = false;
311
606
    }
312
12.3k
  }
313
220
}
314
315
#define GET_ASITAG_IMPL
316
#include "SparcGenSystemOperands.inc"
317
318
void printASITag(MCInst *MI, int opNum, SStream *O)
319
4.10k
{
320
4.10k
  Sparc_add_cs_detail_0(MI, Sparc_OP_GROUP_ASITag, opNum);
321
4.10k
  unsigned Imm = MCOperand_getImm(MCInst_getOperand(MI, (opNum)));
322
4.10k
  const Sparc_ASITag_ASITag *ASITag = Sparc_ASITag_lookupASITagByEncoding(Imm);
323
4.10k
  if (Sparc_getFeatureBits(MI->csh->mode, Sparc_FeatureV9) && ASITag) {
324
127
    SStream_concat1(O, '#');
325
127
    SStream_concat0(O, ASITag->Name);
326
127
  } else
327
3.98k
    printUInt32(O, Imm);
328
4.10k
}
329
330
331
void Sparc_LLVM_printInst(MCInst *MI, uint64_t Address, const char *Annot,
332
            SStream *O)
333
43.8k
{
334
43.8k
  printInst(MI, Address, O);
335
43.8k
}
336
337
const char *Sparc_LLVM_getRegisterName(unsigned RegNo, unsigned AltIdx)
338
20.3k
{
339
20.3k
  return getRegisterName(RegNo, AltIdx);
340
20.3k
}