Coverage Report

Created: 2025-10-10 06:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/capstonenext/arch/Sparc/SparcInstPrinter.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
//===-- 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(MCInst *MI, uint64_t Address,
46
            unsigned OpIdx, unsigned PrintMethodIdx,
47
            SStream *OS);
48
static void printOperand(MCInst *MI, int opNum, SStream *O);
49
50
#define GET_INSTRUCTION_NAME
51
#define PRINT_ALIAS_INSTR
52
#include "SparcGenAsmWriter.inc"
53
54
static void printRegName(SStream *OS, MCRegister Reg)
55
3.44k
{
56
3.44k
  SStream_concat1(OS, '%');
57
3.44k
  SStream_concat0(OS, getRegisterName(Reg, Sparc_NoRegAltName));
58
3.44k
}
59
60
static void printRegNameAlt(SStream *OS, MCRegister Reg, unsigned AltIdx)
61
22.3k
{
62
22.3k
  SStream_concat1(OS, '%');
63
22.3k
  SStream_concat0(OS, getRegisterName(Reg, AltIdx));
64
22.3k
}
65
66
static void printInst(MCInst *MI, uint64_t Address, SStream *O)
67
17.6k
{
68
17.6k
  bool isAlias = false;
69
17.6k
  bool useAliasDetails = map_use_alias_details(MI);
70
17.6k
  map_set_fill_detail_ops(MI, useAliasDetails);
71
72
17.6k
  if (!printAliasInstr(MI, Address, O) && !printSparcAliasInstr(MI, O)) {
73
15.5k
    MCInst_setIsAlias(MI, false);
74
15.5k
  } else {
75
2.14k
    isAlias = true;
76
2.14k
    MCInst_setIsAlias(MI, isAlias);
77
2.14k
    if (useAliasDetails) {
78
2.14k
      return;
79
2.14k
    }
80
2.14k
  }
81
82
15.5k
  if (!isAlias || !useAliasDetails) {
83
15.5k
    map_set_fill_detail_ops(MI, !(isAlias && useAliasDetails));
84
15.5k
    if (isAlias)
85
0
      SStream_Close(O);
86
15.5k
    printInstruction(MI, Address, O);
87
15.5k
    if (isAlias)
88
0
      SStream_Open(O);
89
15.5k
  }
90
15.5k
}
91
92
bool printSparcAliasInstr(MCInst *MI, SStream *O)
93
16.5k
{
94
16.5k
  switch (MCInst_getOpcode(MI)) {
95
15.0k
  default:
96
15.0k
    return false;
97
28
  case Sparc_JMPLrr:
98
1.02k
  case Sparc_JMPLri: {
99
1.02k
    if (MCInst_getNumOperands(MI) != 3)
100
0
      return false;
101
1.02k
    if (!MCOperand_isReg(MCInst_getOperand(MI, (0))))
102
0
      return false;
103
1.02k
    switch (MCOperand_getReg(MCInst_getOperand(MI, (0)))) {
104
42
    default:
105
42
      return false;
106
969
    case Sparc_G0: // jmp $addr | ret | retl
107
969
      if (MCOperand_isImm(MCInst_getOperand(MI, (2))) &&
108
942
          MCOperand_getImm(MCInst_getOperand(MI, (2))) == 8) {
109
787
        switch (MCOperand_getReg(
110
787
          MCInst_getOperand(MI, (1)))) {
111
301
        default:
112
301
          break;
113
301
        case Sparc_I7:
114
12
          SStream_concat0(O, "\tret");
115
12
          return true;
116
474
        case Sparc_O7:
117
474
          SStream_concat0(O, "\tretl");
118
474
          return true;
119
787
        }
120
787
      }
121
483
      SStream_concat0(O, "\tjmp ");
122
483
      printMemOperand(MI, 1, O);
123
483
      return true;
124
17
    case Sparc_O7: // call $addr
125
17
      SStream_concat0(O, "\tcall ");
126
17
      printMemOperand(MI, 1, O);
127
17
      return true;
128
1.02k
    }
129
1.02k
  }
130
163
  case Sparc_V9FCMPS:
131
179
  case Sparc_V9FCMPD:
132
206
  case Sparc_V9FCMPQ:
133
430
  case Sparc_V9FCMPES:
134
476
  case Sparc_V9FCMPED:
135
478
  case Sparc_V9FCMPEQ: {
136
478
    if (Sparc_getFeatureBits(MI->csh->mode, Sparc_FeatureV9) ||
137
57
        (MCInst_getNumOperands(MI) != 3) ||
138
57
        (!MCOperand_isReg(MCInst_getOperand(MI, (0)))) ||
139
57
        (MCOperand_getReg(MCInst_getOperand(MI, (0))) !=
140
57
         Sparc_FCC0))
141
478
      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
16.5k
  }
170
16.5k
}
171
172
static void printOperand(MCInst *MI, int opNum, SStream *O)
173
35.3k
{
174
35.3k
  Sparc_add_cs_detail_0(MI, Sparc_OP_GROUP_Operand, opNum);
175
35.3k
  MCOperand *MO = MCInst_getOperand(MI, (opNum));
176
177
35.3k
  if (MCOperand_isReg(MO)) {
178
25.8k
    unsigned Reg = MCOperand_getReg(MO);
179
25.8k
    if (Sparc_getFeatureBits(MI->csh->mode, Sparc_FeatureV9))
180
22.3k
      printRegNameAlt(O, Reg, Sparc_RegNamesStateReg);
181
3.44k
    else
182
3.44k
      printRegName(O, Reg);
183
25.8k
    return;
184
25.8k
  }
185
186
9.52k
  if (MCOperand_isImm(MO)) {
187
9.52k
    switch (MCInst_getOpcode(MI)) {
188
9.49k
    default:
189
9.49k
      printInt32(O, (int)MCOperand_getImm(MO));
190
9.49k
      return;
191
192
14
    case Sparc_TICCri: // Fall through
193
14
    case Sparc_TICCrr: // Fall through
194
15
    case Sparc_TRAPri: // Fall through
195
15
    case Sparc_TRAPrr: // Fall through
196
23
    case Sparc_TXCCri: // Fall through
197
23
    case Sparc_TXCCrr: // Fall through
198
      // Only seven-bit values up to 127.
199
23
      printInt8(O, ((int)MCOperand_getImm(MO) & 0x7f));
200
23
      return;
201
9.52k
    }
202
9.52k
  }
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
5.27k
{
210
5.27k
  Sparc_add_cs_detail_0(MI, Sparc_OP_GROUP_MemOperand, opNum);
211
5.27k
  MCOperand *Op1 = MCInst_getOperand(MI, (opNum));
212
5.27k
  MCOperand *Op2 = MCInst_getOperand(MI, (opNum + 1));
213
214
5.27k
  bool PrintedFirstOperand = false;
215
5.27k
  if (MCOperand_isReg(Op1) && MCOperand_getReg(Op1) != Sparc_G0) {
216
5.10k
    printOperand(MI, opNum, O);
217
5.10k
    PrintedFirstOperand = true;
218
5.10k
  }
219
220
  // Skip the second operand iff it adds nothing (literal 0 or %g0) and we've
221
  // already printed the first one
222
5.27k
  const bool SkipSecondOperand =
223
5.27k
    PrintedFirstOperand &&
224
5.10k
    ((MCOperand_isReg(Op2) && MCOperand_getReg(Op2) == Sparc_G0) ||
225
4.26k
     (MCOperand_isImm(Op2) && MCOperand_getImm(Op2) == 0));
226
227
5.27k
  if (!SkipSecondOperand) {
228
4.26k
    if (PrintedFirstOperand)
229
4.09k
      SStream_concat0(O, "+");
230
231
4.26k
    printOperand(MI, opNum + 1, O);
232
4.26k
  }
233
5.27k
}
234
235
void printCCOperand(MCInst *MI, int opNum, SStream *O)
236
2.72k
{
237
2.72k
  Sparc_add_cs_detail_0(MI, Sparc_OP_GROUP_CCOperand, opNum);
238
2.72k
  int CC = (int)MCOperand_getImm(MCInst_getOperand(MI, (opNum)));
239
2.72k
  switch (MCInst_getOpcode(MI)) {
240
488
  default:
241
488
    break;
242
488
  case Sparc_FBCOND:
243
546
  case Sparc_FBCONDA:
244
550
  case Sparc_FBCOND_V9:
245
587
  case Sparc_FBCONDA_V9:
246
631
  case Sparc_BPFCC:
247
754
  case Sparc_BPFCCA:
248
754
  case Sparc_BPFCCNT:
249
754
  case Sparc_BPFCCANT:
250
875
  case Sparc_MOVFCCrr:
251
875
  case Sparc_V9MOVFCCrr:
252
931
  case Sparc_MOVFCCri:
253
931
  case Sparc_V9MOVFCCri:
254
935
  case Sparc_FMOVS_FCC:
255
935
  case Sparc_V9FMOVS_FCC:
256
1.04k
  case Sparc_FMOVD_FCC:
257
1.04k
  case Sparc_V9FMOVD_FCC:
258
1.17k
  case Sparc_FMOVQ_FCC:
259
1.17k
  case Sparc_V9FMOVQ_FCC:
260
    // Make sure CC is a fp conditional flag.
261
1.17k
    CC = (CC < SPARC_CC_FCC_BEGIN) ? (CC + SPARC_CC_FCC_BEGIN) : CC;
262
1.17k
    break;
263
406
  case Sparc_CBCOND:
264
578
  case Sparc_CBCONDA:
265
    // Make sure CC is a cp conditional flag.
266
578
    CC = (CC < SPARC_CC_CPCC_BEGIN) ? (CC + SPARC_CC_CPCC_BEGIN) :
267
578
              CC;
268
578
    break;
269
57
  case Sparc_BPR:
270
162
  case Sparc_BPRA:
271
353
  case Sparc_BPRNT:
272
354
  case Sparc_BPRANT:
273
365
  case Sparc_MOVRri:
274
407
  case Sparc_MOVRrr:
275
432
  case Sparc_FMOVRS:
276
444
  case Sparc_FMOVRD:
277
483
  case Sparc_FMOVRQ:
278
    // Make sure CC is a register conditional flag.
279
483
    CC = (CC < SPARC_CC_REG_BEGIN) ? (CC + SPARC_CC_REG_BEGIN) : CC;
280
483
    break;
281
2.72k
  }
282
2.72k
  SStream_concat0(O, SPARCCondCodeToString((sparc_cc)CC));
283
2.72k
}
284
285
bool printGetPCX(MCInst *MI, unsigned opNum, SStream *O)
286
0
{
287
0
  printf("FIXME: Implement SparcInstPrinter::printGetPCX.");
288
0
  return true;
289
0
}
290
291
void printMembarTag(MCInst *MI, int opNum, SStream *O)
292
222
{
293
222
  Sparc_add_cs_detail_0(MI, Sparc_OP_GROUP_MembarTag, opNum);
294
222
  static const char *const TagNames[] = { "#LoadLoad",  "#StoreLoad",
295
222
            "#LoadStore", "#StoreStore",
296
222
            "#Lookaside", "#MemIssue",
297
222
            "#Sync" };
298
299
222
  unsigned Imm = MCOperand_getImm(MCInst_getOperand(MI, (opNum)));
300
301
222
  if (Imm > 127) {
302
14
    printUInt32(O, Imm);
303
14
    return;
304
14
  }
305
306
222
  bool First = true;
307
11.8k
  for (unsigned i = 0; i < sizeof(TagNames); i++) {
308
11.6k
    if (Imm & (1ull << i)) {
309
828
      SStream_concat(O, "%s", (First ? "" : " | "));
310
828
      SStream_concat0(O, TagNames[i]);
311
828
      First = false;
312
828
    }
313
11.6k
  }
314
208
}
315
316
#define GET_ASITAG_IMPL
317
#include "SparcGenSystemOperands.inc"
318
319
void printASITag(MCInst *MI, int opNum, SStream *O)
320
1.88k
{
321
1.88k
  Sparc_add_cs_detail_0(MI, Sparc_OP_GROUP_ASITag, opNum);
322
1.88k
  unsigned Imm = MCOperand_getImm(MCInst_getOperand(MI, (opNum)));
323
1.88k
  const Sparc_ASITag_ASITag *ASITag =
324
1.88k
    Sparc_ASITag_lookupASITagByEncoding(Imm);
325
1.88k
  if (Sparc_getFeatureBits(MI->csh->mode, Sparc_FeatureV9) && ASITag) {
326
229
    SStream_concat1(O, '#');
327
229
    SStream_concat0(O, ASITag->Name);
328
229
  } else
329
1.65k
    printUInt32(O, Imm);
330
1.88k
}
331
332
void Sparc_LLVM_printInst(MCInst *MI, uint64_t Address, const char *Annot,
333
        SStream *O)
334
17.6k
{
335
17.6k
  printInst(MI, Address, O);
336
17.6k
}
337
338
const char *Sparc_LLVM_getRegisterName(unsigned RegNo, unsigned AltIdx)
339
5.59k
{
340
5.59k
  return getRegisterName(RegNo, AltIdx);
341
5.59k
}