Coverage Report

Created: 2025-07-11 06:32

/src/capstonev5/arch/Sparc/SparcInstPrinter.c
Line
Count
Source (jump to first uncovered line)
1
//===-- SparcInstPrinter.cpp - Convert Sparc MCInst to assembly syntax --------===//
2
//
3
//                     The LLVM Compiler Infrastructure
4
//
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
7
//
8
//===----------------------------------------------------------------------===//
9
//
10
// This class prints an Sparc MCInst to a .s file.
11
//
12
//===----------------------------------------------------------------------===//
13
14
/* Capstone Disassembly Engine */
15
/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2015 */
16
17
#ifdef CAPSTONE_HAS_SPARC
18
19
#ifdef _MSC_VER
20
#define _CRT_SECURE_NO_WARNINGS
21
#endif
22
23
#include <stdio.h>
24
#include <stdlib.h>
25
#include <string.h>
26
#include <limits.h>
27
28
#include "SparcInstPrinter.h"
29
#include "../../MCInst.h"
30
#include "../../utils.h"
31
#include "../../SStream.h"
32
#include "../../MCRegisterInfo.h"
33
#include "../../MathExtras.h"
34
#include "SparcMapping.h"
35
36
#include "Sparc.h"
37
38
static const char *getRegisterName(unsigned RegNo);
39
static void printInstruction(MCInst *MI, SStream *O, const MCRegisterInfo *MRI);
40
static void printMemOperand(MCInst *MI, int opNum, SStream *O, const char *Modifier);
41
static void printOperand(MCInst *MI, int opNum, SStream *O);
42
43
static void Sparc_add_hint(MCInst *MI, unsigned int hint)
44
1.82k
{
45
1.82k
  if (MI->csh->detail) {
46
1.82k
    MI->flat_insn->detail->sparc.hint = hint;
47
1.82k
  }
48
1.82k
}
49
50
static void Sparc_add_reg(MCInst *MI, unsigned int reg)
51
4.72k
{
52
4.72k
  if (MI->csh->detail) {
53
4.72k
    MI->flat_insn->detail->sparc.operands[MI->flat_insn->detail->sparc.op_count].type = SPARC_OP_REG;
54
4.72k
    MI->flat_insn->detail->sparc.operands[MI->flat_insn->detail->sparc.op_count].reg = reg;
55
4.72k
    MI->flat_insn->detail->sparc.op_count++;
56
4.72k
  }
57
4.72k
}
58
59
static void set_mem_access(MCInst *MI, bool status)
60
10.1k
{
61
10.1k
  if (MI->csh->detail != CS_OPT_ON)
62
0
    return;
63
64
10.1k
  MI->csh->doing_mem = status;
65
66
10.1k
  if (status) {
67
5.07k
    MI->flat_insn->detail->sparc.operands[MI->flat_insn->detail->sparc.op_count].type = SPARC_OP_MEM;
68
5.07k
    MI->flat_insn->detail->sparc.operands[MI->flat_insn->detail->sparc.op_count].mem.base = SPARC_REG_INVALID;
69
5.07k
    MI->flat_insn->detail->sparc.operands[MI->flat_insn->detail->sparc.op_count].mem.disp = 0;
70
5.07k
  } else {
71
    // done, create the next operand slot
72
5.07k
    MI->flat_insn->detail->sparc.op_count++;
73
5.07k
  }
74
10.1k
}
75
76
void Sparc_post_printer(csh ud, cs_insn *insn, char *insn_asm, MCInst *mci)
77
74.4k
{
78
74.4k
  if (((cs_struct *)ud)->detail != CS_OPT_ON)
79
0
    return;
80
81
  // fix up some instructions
82
74.4k
  if (insn->id == SPARC_INS_CASX) {
83
    // first op is actually a memop, not regop
84
67
    insn->detail->sparc.operands[0].type = SPARC_OP_MEM;
85
67
    insn->detail->sparc.operands[0].mem.base = (uint8_t)insn->detail->sparc.operands[0].reg;
86
67
    insn->detail->sparc.operands[0].mem.disp = 0;
87
67
  }
88
74.4k
}
89
90
static void printRegName(SStream *OS, unsigned RegNo)
91
63.6k
{
92
63.6k
  SStream_concat0(OS, "%");
93
63.6k
  SStream_concat0(OS, getRegisterName(RegNo));
94
63.6k
}
95
96
#define GET_INSTRINFO_ENUM
97
#include "SparcGenInstrInfo.inc"
98
99
#define GET_REGINFO_ENUM
100
#include "SparcGenRegisterInfo.inc"
101
102
static bool printSparcAliasInstr(MCInst *MI, SStream *O)
103
42.2k
{
104
42.2k
  switch (MCInst_getOpcode(MI)) {
105
39.5k
    default: return false;
106
412
    case SP_JMPLrr:
107
1.63k
    case SP_JMPLri:
108
1.63k
         if (MCInst_getNumOperands(MI) != 3)
109
0
           return false;
110
1.63k
         if (!MCOperand_isReg(MCInst_getOperand(MI, 0)))
111
0
           return false;
112
113
1.63k
         switch (MCOperand_getReg(MCInst_getOperand(MI, 0))) {
114
102
           default: return false;
115
1.45k
           case SP_G0: // jmp $addr | ret | retl
116
1.45k
                if (MCOperand_isImm(MCInst_getOperand(MI, 2)) &&
117
1.45k
                  MCOperand_getImm(MCInst_getOperand(MI, 2)) == 8) {
118
369
                  switch(MCOperand_getReg(MCInst_getOperand(MI, 1))) {
119
72
                    default: break;
120
287
                    case SP_I7: SStream_concat0(O, "ret"); MCInst_setOpcodePub(MI, SPARC_INS_RET); return true;
121
10
                    case SP_O7: SStream_concat0(O, "retl"); MCInst_setOpcodePub(MI, SPARC_INS_RETL); return true;
122
369
                  }
123
369
                }
124
125
1.16k
                SStream_concat0(O, "jmp\t");
126
1.16k
                MCInst_setOpcodePub(MI, SPARC_INS_JMP);
127
1.16k
                printMemOperand(MI, 1, O, NULL);
128
1.16k
                return true;
129
73
           case SP_O7: // call $addr
130
73
                SStream_concat0(O, "call ");
131
73
                MCInst_setOpcodePub(MI, SPARC_INS_CALL);
132
73
                printMemOperand(MI, 1, O, NULL);
133
73
                return true;
134
1.63k
         }
135
357
    case SP_V9FCMPS:
136
423
    case SP_V9FCMPD:
137
495
    case SP_V9FCMPQ:
138
564
    case SP_V9FCMPES:
139
599
    case SP_V9FCMPED:
140
1.08k
    case SP_V9FCMPEQ:
141
1.08k
         if (MI->csh->mode & CS_MODE_V9 || (MCInst_getNumOperands(MI) != 3) ||
142
1.08k
             (!MCOperand_isReg(MCInst_getOperand(MI, 0))) ||
143
1.08k
             (MCOperand_getReg(MCInst_getOperand(MI, 0)) != SP_FCC0))
144
1.08k
             return false;
145
         // if V8, skip printing %fcc0.
146
0
         switch(MCInst_getOpcode(MI)) {
147
0
           default:
148
0
           case SP_V9FCMPS:  SStream_concat0(O, "fcmps\t"); MCInst_setOpcodePub(MI, SPARC_INS_FCMPS); break;
149
0
           case SP_V9FCMPD:  SStream_concat0(O, "fcmpd\t"); MCInst_setOpcodePub(MI, SPARC_INS_FCMPD); break;
150
0
           case SP_V9FCMPQ:  SStream_concat0(O, "fcmpq\t"); MCInst_setOpcodePub(MI, SPARC_INS_FCMPQ); break;
151
0
           case SP_V9FCMPES: SStream_concat0(O, "fcmpes\t"); MCInst_setOpcodePub(MI, SPARC_INS_FCMPES); break;
152
0
           case SP_V9FCMPED: SStream_concat0(O, "fcmped\t"); MCInst_setOpcodePub(MI, SPARC_INS_FCMPED); break;
153
0
           case SP_V9FCMPEQ: SStream_concat0(O, "fcmpeq\t"); MCInst_setOpcodePub(MI, SPARC_INS_FCMPEQ); break;
154
0
         }
155
0
         printOperand(MI, 1, O);
156
0
         SStream_concat0(O, ", ");
157
0
         printOperand(MI, 2, O);
158
0
         return true;
159
42.2k
  }
160
42.2k
}
161
162
static void printOperand(MCInst *MI, int opNum, SStream *O)
163
122k
{
164
122k
  int64_t Imm;
165
122k
  unsigned reg;
166
122k
  MCOperand *MO = MCInst_getOperand(MI, opNum);
167
168
122k
  if (MCOperand_isReg(MO)) {
169
63.6k
    reg = MCOperand_getReg(MO);
170
63.6k
    printRegName(O, reg);
171
63.6k
    reg = Sparc_map_register(reg);
172
173
63.6k
    if (MI->csh->detail) {
174
63.6k
      if (MI->csh->doing_mem) {
175
6.60k
        if (MI->flat_insn->detail->sparc.operands[MI->flat_insn->detail->sparc.op_count].mem.base)
176
1.52k
          MI->flat_insn->detail->sparc.operands[MI->flat_insn->detail->sparc.op_count].mem.index = (uint8_t)reg;
177
5.07k
        else
178
5.07k
          MI->flat_insn->detail->sparc.operands[MI->flat_insn->detail->sparc.op_count].mem.base = (uint8_t)reg;
179
57.0k
      } else {
180
57.0k
        MI->flat_insn->detail->sparc.operands[MI->flat_insn->detail->sparc.op_count].type = SPARC_OP_REG;
181
57.0k
        MI->flat_insn->detail->sparc.operands[MI->flat_insn->detail->sparc.op_count].reg = reg;
182
57.0k
        MI->flat_insn->detail->sparc.op_count++;
183
57.0k
      }
184
63.6k
    }
185
186
63.6k
    return;
187
63.6k
  }
188
189
58.8k
  if (MCOperand_isImm(MO)) {
190
58.6k
    Imm = (int)MCOperand_getImm(MO);
191
192
    // Conditional branches displacements needs to be signextended to be
193
    // able to jump backwards.
194
    //
195
    // Displacements are measured as the number of instructions forward or
196
    // backward, so they need to be multiplied by 4
197
58.6k
    switch (MI->Opcode) {
198
12.7k
      case SP_CALL:
199
        // Imm = SignExtend32(Imm, 30);
200
12.7k
        Imm += MI->address;
201
12.7k
        break;
202
203
      // Branch on integer condition with prediction (BPcc)
204
      // Branch on floating point condition with prediction (FBPfcc)
205
262
      case SP_BPICC:
206
495
      case SP_BPICCA:
207
2.77k
      case SP_BPICCANT:
208
5.67k
      case SP_BPICCNT:
209
5.95k
      case SP_BPXCC:
210
6.18k
      case SP_BPXCCA:
211
7.98k
      case SP_BPXCCANT:
212
10.1k
      case SP_BPXCCNT:
213
10.5k
      case SP_BPFCC:
214
11.0k
      case SP_BPFCCA:
215
14.5k
      case SP_BPFCCANT:
216
17.8k
      case SP_BPFCCNT:
217
17.8k
        Imm = SignExtend32(Imm, 19);
218
17.8k
        Imm = MI->address + Imm * 4;
219
17.8k
        break;
220
221
      // Branch on integer condition (Bicc)
222
      // Branch on floating point condition (FBfcc)
223
350
      case SP_BA:
224
3.24k
      case SP_BCOND:
225
5.50k
      case SP_BCONDA:
226
6.04k
      case SP_FBCOND:
227
6.87k
      case SP_FBCONDA:
228
6.87k
        Imm = SignExtend32(Imm, 22);
229
6.87k
        Imm = MI->address + Imm * 4;
230
6.87k
        break;
231
232
      // Branch on integer register with prediction (BPr)
233
369
      case SP_BPGEZapn:
234
579
      case SP_BPGEZapt:
235
674
      case SP_BPGEZnapn:
236
744
      case SP_BPGEZnapt:
237
819
      case SP_BPGZapn:
238
889
      case SP_BPGZapt:
239
1.08k
      case SP_BPGZnapn:
240
1.28k
      case SP_BPGZnapt:
241
1.47k
      case SP_BPLEZapn:
242
1.67k
      case SP_BPLEZapt:
243
1.74k
      case SP_BPLEZnapn:
244
1.81k
      case SP_BPLEZnapt:
245
1.87k
      case SP_BPLZapn:
246
2.07k
      case SP_BPLZapt:
247
2.15k
      case SP_BPLZnapn:
248
2.37k
      case SP_BPLZnapt:
249
2.68k
      case SP_BPNZapn:
250
2.76k
      case SP_BPNZapt:
251
2.96k
      case SP_BPNZnapn:
252
3.03k
      case SP_BPNZnapt:
253
3.10k
      case SP_BPZapn:
254
3.29k
      case SP_BPZapt:
255
3.37k
      case SP_BPZnapn:
256
3.45k
      case SP_BPZnapt:
257
3.45k
        Imm = SignExtend32(Imm, 16);
258
3.45k
        Imm = MI->address + Imm * 4;
259
3.45k
        break;
260
58.6k
    }
261
    
262
58.6k
    printInt64(O, Imm);
263
264
58.6k
    if (MI->csh->detail) {
265
58.6k
      if (MI->csh->doing_mem) {
266
2.89k
        MI->flat_insn->detail->sparc.operands[MI->flat_insn->detail->sparc.op_count].mem.disp = Imm;
267
55.7k
      } else {
268
55.7k
        MI->flat_insn->detail->sparc.operands[MI->flat_insn->detail->sparc.op_count].type = SPARC_OP_IMM;
269
55.7k
        MI->flat_insn->detail->sparc.operands[MI->flat_insn->detail->sparc.op_count].imm = Imm;
270
55.7k
        MI->flat_insn->detail->sparc.op_count++;
271
55.7k
      }
272
58.6k
    }
273
58.6k
  }
274
275
58.8k
  return;
276
58.8k
}
277
278
static void printMemOperand(MCInst *MI, int opNum, SStream *O, const char *Modifier)
279
5.07k
{
280
5.07k
  MCOperand *MO;
281
282
5.07k
  set_mem_access(MI, true);
283
5.07k
  printOperand(MI, opNum, O);
284
285
  // If this is an ADD operand, emit it like normal operands.
286
5.07k
  if (Modifier && !strcmp(Modifier, "arith")) {
287
0
    SStream_concat0(O, ", ");
288
0
    printOperand(MI, opNum + 1, O);
289
0
    set_mem_access(MI, false);
290
0
    return;
291
0
  }
292
293
5.07k
  MO = MCInst_getOperand(MI, opNum + 1);
294
295
5.07k
  if (MCOperand_isReg(MO) && (MCOperand_getReg(MO) == SP_G0)) {
296
300
    set_mem_access(MI, false);
297
300
    return;   // don't print "+%g0"
298
300
  }
299
300
4.77k
  if (MCOperand_isImm(MO) && (MCOperand_getImm(MO) == 0)) {
301
360
    set_mem_access(MI, false);
302
360
    return;   // don't print "+0"
303
360
  }
304
305
4.41k
  SStream_concat0(O, "+");  // qq
306
307
4.41k
  printOperand(MI, opNum + 1, O);
308
4.41k
  set_mem_access(MI, false);
309
4.41k
}
310
311
static void printCCOperand(MCInst *MI, int opNum, SStream *O)
312
7.66k
{
313
7.66k
  int CC = (int)MCOperand_getImm(MCInst_getOperand(MI, opNum)) + 256;
314
315
7.66k
  switch (MCInst_getOpcode(MI)) {
316
3.46k
    default: break;
317
3.46k
    case SP_FBCOND:
318
1.37k
    case SP_FBCONDA:
319
1.74k
    case SP_BPFCC:
320
2.27k
    case SP_BPFCCA:
321
2.27k
    case SP_BPFCCNT:
322
2.27k
    case SP_BPFCCANT:
323
2.96k
    case SP_MOVFCCrr:  case SP_V9MOVFCCrr:
324
3.44k
    case SP_MOVFCCri:  case SP_V9MOVFCCri:
325
3.72k
    case SP_FMOVS_FCC: case SP_V9FMOVS_FCC:
326
3.99k
    case SP_FMOVD_FCC: case SP_V9FMOVD_FCC:
327
4.19k
    case SP_FMOVQ_FCC: case SP_V9FMOVQ_FCC:
328
         // Make sure CC is a fp conditional flag.
329
4.19k
         CC = (CC < 16+256) ? (CC + 16) : CC;
330
4.19k
         break;
331
7.66k
  }
332
333
7.66k
  SStream_concat0(O, SPARCCondCodeToString((sparc_cc)CC));
334
335
7.66k
  if (MI->csh->detail)
336
7.66k
    MI->flat_insn->detail->sparc.cc = (sparc_cc)CC;
337
7.66k
}
338
339
340
static bool printGetPCX(MCInst *MI, unsigned opNum, SStream *O)
341
0
{
342
0
  return true;
343
0
}
344
345
346
#define PRINT_ALIAS_INSTR
347
#include "SparcGenAsmWriter.inc"
348
349
void Sparc_printInst(MCInst *MI, SStream *O, void *Info)
350
74.4k
{
351
74.4k
  char *mnem, *p;
352
74.4k
  char instr[64]; // Sparc has no instruction this long
353
354
74.4k
  mnem = printAliasInstr(MI, O, Info);
355
74.4k
  if (mnem) {
356
    // fixup instruction id due to the change in alias instruction
357
32.1k
    unsigned cpy_len = sizeof(instr) - 1 < strlen(mnem) ? sizeof(instr) - 1 : strlen(mnem);
358
32.1k
    memcpy(instr, mnem, cpy_len);
359
32.1k
    instr[cpy_len] = '\0';
360
    // does this contains hint with a coma?
361
32.1k
    p = strchr(instr, ',');
362
32.1k
    if (p)
363
18.2k
      *p = '\0'; // now instr only has instruction mnemonic
364
32.1k
    MCInst_setOpcodePub(MI, Sparc_map_insn(instr));
365
32.1k
    switch(MCInst_getOpcode(MI)) {
366
2.89k
      case SP_BCOND:
367
5.15k
      case SP_BCONDA:
368
7.43k
      case SP_BPICCANT:
369
10.3k
      case SP_BPICCNT:
370
12.1k
      case SP_BPXCCANT:
371
14.3k
      case SP_BPXCCNT:
372
20.1k
      case SP_TXCCri:
373
23.4k
      case SP_TXCCrr:
374
23.4k
        if (MI->csh->detail) {
375
          // skip 'b', 't'
376
23.4k
          MI->flat_insn->detail->sparc.cc = Sparc_map_ICC(instr + 1);
377
23.4k
          MI->flat_insn->detail->sparc.hint = Sparc_map_hint(mnem);
378
23.4k
        }
379
23.4k
        break;
380
3.45k
      case SP_BPFCCANT:
381
6.76k
      case SP_BPFCCNT:
382
6.76k
        if (MI->csh->detail) {
383
          // skip 'fb'
384
6.76k
          MI->flat_insn->detail->sparc.cc = Sparc_map_FCC(instr + 2);
385
6.76k
          MI->flat_insn->detail->sparc.hint = Sparc_map_hint(mnem);
386
6.76k
        }
387
6.76k
        break;
388
0
      case SP_FMOVD_ICC:
389
0
      case SP_FMOVD_XCC:
390
0
      case SP_FMOVQ_ICC:
391
0
      case SP_FMOVQ_XCC:
392
0
      case SP_FMOVS_ICC:
393
0
      case SP_FMOVS_XCC:
394
0
        if (MI->csh->detail) {
395
          // skip 'fmovd', 'fmovq', 'fmovs'
396
0
          MI->flat_insn->detail->sparc.cc = Sparc_map_ICC(instr + 5);
397
0
          MI->flat_insn->detail->sparc.hint = Sparc_map_hint(mnem);
398
0
        }
399
0
        break;
400
0
      case SP_MOVICCri:
401
0
      case SP_MOVICCrr:
402
0
      case SP_MOVXCCri:
403
0
      case SP_MOVXCCrr:
404
0
        if (MI->csh->detail) {
405
          // skip 'mov'
406
0
          MI->flat_insn->detail->sparc.cc = Sparc_map_ICC(instr + 3);
407
0
          MI->flat_insn->detail->sparc.hint = Sparc_map_hint(mnem);
408
0
        }
409
0
        break;
410
0
      case SP_V9FMOVD_FCC:
411
0
      case SP_V9FMOVQ_FCC:
412
0
      case SP_V9FMOVS_FCC:
413
0
        if (MI->csh->detail) {
414
          // skip 'fmovd', 'fmovq', 'fmovs'
415
0
          MI->flat_insn->detail->sparc.cc = Sparc_map_FCC(instr + 5);
416
0
          MI->flat_insn->detail->sparc.hint = Sparc_map_hint(mnem);
417
0
        }
418
0
        break;
419
0
      case SP_V9MOVFCCri:
420
0
      case SP_V9MOVFCCrr:
421
0
        if (MI->csh->detail) {
422
          // skip 'mov'
423
0
          MI->flat_insn->detail->sparc.cc = Sparc_map_FCC(instr + 3);
424
0
          MI->flat_insn->detail->sparc.hint = Sparc_map_hint(mnem);
425
0
        }
426
0
        break;
427
2.00k
      default:
428
2.00k
        break;
429
32.1k
    }
430
32.1k
    cs_mem_free(mnem);
431
42.2k
  } else {
432
42.2k
    if (!printSparcAliasInstr(MI, O))
433
40.7k
      printInstruction(MI, O, NULL);
434
42.2k
  }
435
74.4k
}
436
437
void Sparc_addReg(MCInst *MI, int reg)
438
18.2k
{
439
18.2k
  if (MI->csh->detail) {
440
18.2k
    MI->flat_insn->detail->sparc.operands[MI->flat_insn->detail->sparc.op_count].type = SPARC_OP_REG;
441
18.2k
    MI->flat_insn->detail->sparc.operands[MI->flat_insn->detail->sparc.op_count].reg = reg;
442
18.2k
    MI->flat_insn->detail->sparc.op_count++;
443
18.2k
  }
444
18.2k
}
445
446
#endif