Coverage Report

Created: 2025-07-01 07:03

/src/capstonenext/arch/Mips/MipsInstPrinter.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
//===-- MipsInstPrinter.cpp - Convert Mips 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 Mips 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 "MipsMapping.h"
33
#include "MipsInstPrinter.h"
34
35
#define GET_SUBTARGETINFO_ENUM
36
#include "MipsGenSubtargetInfo.inc"
37
38
#define GET_INSTRINFO_ENUM
39
#include "MipsGenInstrInfo.inc"
40
41
#define GET_REGINFO_ENUM
42
#include "MipsGenRegisterInfo.inc"
43
44
8.38k
#define CONCAT(a, b) CONCAT_(a, b)
45
8.38k
#define CONCAT_(a, b) a##_##b
46
47
#define DEBUG_TYPE "asm-printer"
48
49
#define PRINT_ALIAS_INSTR
50
#include "MipsGenAsmWriter.inc"
51
52
static bool isReg(const MCInst *MI, unsigned OpNo, unsigned R)
53
3.57k
{
54
3.57k
  return MCOperand_getReg(MCInst_getOperand((MCInst *)MI, (OpNo))) == R;
55
3.57k
}
56
57
static const char *MipsFCCToString(Mips_CondCode CC)
58
0
{
59
0
  switch (CC) {
60
0
  case Mips_FCOND_F:
61
0
  case Mips_FCOND_T:
62
0
    return "f";
63
0
  case Mips_FCOND_UN:
64
0
  case Mips_FCOND_OR:
65
0
    return "un";
66
0
  case Mips_FCOND_OEQ:
67
0
  case Mips_FCOND_UNE:
68
0
    return "eq";
69
0
  case Mips_FCOND_UEQ:
70
0
  case Mips_FCOND_ONE:
71
0
    return "ueq";
72
0
  case Mips_FCOND_OLT:
73
0
  case Mips_FCOND_UGE:
74
0
    return "olt";
75
0
  case Mips_FCOND_ULT:
76
0
  case Mips_FCOND_OGE:
77
0
    return "ult";
78
0
  case Mips_FCOND_OLE:
79
0
  case Mips_FCOND_UGT:
80
0
    return "ole";
81
0
  case Mips_FCOND_ULE:
82
0
  case Mips_FCOND_OGT:
83
0
    return "ule";
84
0
  case Mips_FCOND_SF:
85
0
  case Mips_FCOND_ST:
86
0
    return "sf";
87
0
  case Mips_FCOND_NGLE:
88
0
  case Mips_FCOND_GLE:
89
0
    return "ngle";
90
0
  case Mips_FCOND_SEQ:
91
0
  case Mips_FCOND_SNE:
92
0
    return "seq";
93
0
  case Mips_FCOND_NGL:
94
0
  case Mips_FCOND_GL:
95
0
    return "ngl";
96
0
  case Mips_FCOND_LT:
97
0
  case Mips_FCOND_NLT:
98
0
    return "lt";
99
0
  case Mips_FCOND_NGE:
100
0
  case Mips_FCOND_GE:
101
0
    return "nge";
102
0
  case Mips_FCOND_LE:
103
0
  case Mips_FCOND_NLE:
104
0
    return "le";
105
0
  case Mips_FCOND_NGT:
106
0
  case Mips_FCOND_GT:
107
0
    return "ngt";
108
0
  }
109
0
  CS_ASSERT_RET_VAL(0 && "Impossible condition code!", NULL);
110
0
  return "";
111
0
}
112
113
const char *Mips_LLVM_getRegisterName(unsigned RegNo, bool noRegName);
114
115
static void printRegName(MCInst *MI, SStream *OS, MCRegister Reg)
116
145k
{
117
145k
  int syntax_opt = MI->csh->syntax;
118
145k
  if (!(syntax_opt & CS_OPT_SYNTAX_NO_DOLLAR)) {
119
145k
    SStream_concat1(OS, '$');
120
145k
  }
121
145k
  SStream_concat0(OS, Mips_LLVM_getRegisterName(Reg, syntax_opt & CS_OPT_SYNTAX_NOREGNAME));
122
145k
}
123
124
77.8k
static void patch_cs_printer(MCInst *MI, SStream *O) {
125
  // replace '# 16 bit inst' to empty.
126
77.8k
  SStream_replc(O, '#', 0);
127
77.8k
  SStream_trimls(O);
128
129
77.8k
  if (MI->csh->syntax & CS_OPT_SYNTAX_NO_DOLLAR) {
130
0
    char *dollar = strchr(O->buffer, '$');
131
0
    if (!dollar) {
132
0
      return;
133
0
    }
134
0
    size_t dollar_len = strlen(dollar + 1);
135
    // to include `\0`
136
0
    memmove(dollar, dollar + 1, dollar_len + 1);
137
0
  }
138
77.8k
}
139
140
321
static void patch_cs_detail_operand_reg(cs_mips_op *op, unsigned reg, unsigned access) {
141
321
  op->type = MIPS_OP_REG;
142
321
  op->reg = reg;
143
321
  op->is_reglist = false;
144
321
  op->access = access;
145
321
}
146
147
77.8k
static void patch_cs_details(MCInst *MI) {
148
77.8k
  if (!detail_is_set(MI))
149
0
    return;
150
151
77.8k
  cs_mips_op *op0 = NULL, *op1 = NULL, *op2 = NULL;
152
77.8k
  unsigned opcode = MCInst_getOpcode(MI);
153
77.8k
  unsigned n_ops = MCInst_getNumOperands(MI);
154
155
77.8k
  switch(opcode) {
156
  /* mips r2 to r5 only 64bit */
157
23
  case Mips_DSDIV: /// ddiv $$zero, $rs, $rt
158
    /* fall-thru */
159
64
  case Mips_DUDIV: /// ddivu $$zero, $rs, $rt
160
64
    if (n_ops != 2) {
161
0
      return;
162
0
    }
163
64
    Mips_inc_op_count(MI);
164
64
    op0 = Mips_get_detail_op(MI, -3);
165
64
    op1 = Mips_get_detail_op(MI, -2);
166
64
    op2 = Mips_get_detail_op(MI, -1);
167
    // move all details by one and add $zero reg
168
64
    *op2 = *op1;
169
64
    *op1 = *op0;
170
64
    patch_cs_detail_operand_reg(op0, MIPS_REG_ZERO_64, CS_AC_WRITE);
171
64
    return;
172
173
  /* mips r2 to r5 only */
174
10
  case Mips_SDIV: /// div $$zero, $rs, $rt
175
    /* fall-thru */
176
166
  case Mips_UDIV: /// divu $$zero, $rs, $rt
177
    /* fall-thru */
178
  /* microMIPS only */
179
210
  case Mips_SDIV_MM: /// div $$zero, $rs, $rt
180
    /* fall-thru */
181
257
  case Mips_UDIV_MM: /// divu $$zero, $rs, $rt
182
    /* fall-thru */
183
184
  /* MIPS16 only */
185
257
  case Mips_DivRxRy16: /// div $$zero, $rx, $ry
186
    /* fall-thru */
187
257
  case Mips_DivuRxRy16: /// divu $$zero, $rx, $ry
188
257
    if (n_ops != 2) {
189
0
      return;
190
0
    }
191
257
    Mips_inc_op_count(MI);
192
257
    op0 = Mips_get_detail_op(MI, -3);
193
257
    op1 = Mips_get_detail_op(MI, -2);
194
257
    op2 = Mips_get_detail_op(MI, -1);
195
    // move all details by one and add $zero reg
196
257
    *op2 = *op1;
197
257
    *op1 = *op0;
198
257
    patch_cs_detail_operand_reg(op0, MIPS_REG_ZERO, CS_AC_WRITE);
199
257
    return;
200
0
  case Mips_AddiuSpImm16: /// addiu $$sp, imm8
201
    /* fall-thru */
202
0
  case Mips_AddiuSpImmX16: /// addiu $$sp, imm8
203
0
    if (n_ops != 1) {
204
0
      return;
205
0
    }
206
0
    Mips_inc_op_count(MI);
207
0
    op0 = Mips_get_detail_op(MI, -2);
208
0
    op1 = Mips_get_detail_op(MI, -1);
209
    // move all details by one and add $sp reg
210
0
    *op1 = *op0;
211
0
    patch_cs_detail_operand_reg(op0, MIPS_REG_SP, CS_AC_READ_WRITE);
212
0
    return;
213
0
  case Mips_JrcRa16: /// jrc $ra
214
    /* fall-thru */
215
0
  case Mips_JrRa16: /// jr $ra
216
0
    if (n_ops > 0) {
217
0
      return;
218
0
    }
219
0
    Mips_inc_op_count(MI);
220
0
    op0 = Mips_get_detail_op(MI, -1);
221
0
    patch_cs_detail_operand_reg(op0, MIPS_REG_RA, CS_AC_READ);
222
0
    return;
223
77.5k
  default:
224
77.5k
    return;
225
77.8k
  }
226
77.8k
}
227
228
77.8k
void Mips_LLVM_printInst(MCInst *MI, uint64_t Address, SStream *O) {
229
77.8k
  bool useAliasDetails = map_use_alias_details(MI);
230
77.8k
  if (!useAliasDetails) {
231
0
    SStream_Close(O);
232
0
    printInstruction(MI, Address, O);
233
0
    SStream_Open(O);
234
0
    map_set_fill_detail_ops(MI, false);
235
0
  }
236
237
77.8k
  if (printAliasInstr(MI, Address, O) ||
238
77.8k
    printAlias4(MI, Address, O)) {
239
3.77k
    MCInst_setIsAlias(MI, true);
240
74.1k
  } else {
241
74.1k
    printInstruction(MI, Address, O);
242
74.1k
  }
243
244
77.8k
  patch_cs_printer(MI, O);
245
77.8k
  patch_cs_details(MI);
246
247
77.8k
  if (!useAliasDetails) {
248
0
    map_set_fill_detail_ops(MI, true);
249
0
  }
250
77.8k
}
251
252
void printOperand(MCInst *MI, unsigned OpNo, SStream *O)
253
180k
{
254
180k
  switch (MCInst_getOpcode(MI)) {
255
180k
  default:
256
180k
    break;
257
180k
  case Mips_AND16_NM:
258
0
  case Mips_XOR16_NM:
259
0
  case Mips_OR16_NM:
260
0
    if (MCInst_getNumOperands(MI) == 2 && OpNo == 2)
261
0
      OpNo = 0; // rt, rs -> rt, rs, rt
262
0
    break;
263
0
  case Mips_ADDu4x4_NM:
264
0
  case Mips_MUL4x4_NM:
265
0
    if (MCInst_getNumOperands(MI) == 2 && OpNo > 0)
266
0
      OpNo = OpNo - 1; // rt, rs -> rt, rt, rs
267
0
    break;
268
180k
  }
269
270
180k
  MCOperand *Op = MCInst_getOperand(MI, (OpNo));
271
180k
  if (MCOperand_isReg(Op)) {
272
142k
    add_cs_detail(MI, Mips_OP_GROUP_Operand, OpNo);
273
142k
    printRegName(MI, O, MCOperand_getReg(Op));
274
142k
    return;
275
142k
  }
276
277
278
37.6k
  if (MCOperand_isImm(Op)) {
279
37.6k
    switch (MCInst_getOpcode(MI)) {
280
0
    case Mips_LI48_NM:
281
0
    case Mips_ANDI16_NM:
282
0
    case Mips_ANDI_NM:
283
0
    case Mips_ORI_NM:
284
0
    case Mips_XORI_NM:
285
0
    case Mips_TEQ_NM:
286
0
    case Mips_TNE_NM:
287
0
    case Mips_SIGRIE_NM:
288
0
    case Mips_SDBBP_NM:
289
0
    case Mips_SDBBP16_NM:
290
0
    case Mips_BREAK_NM:
291
0
    case Mips_BREAK16_NM:
292
0
    case Mips_SYSCALL_NM:
293
0
    case Mips_SYSCALL16_NM:
294
0
    case Mips_WAIT_NM:
295
0
      CONCAT(printUImm, CONCAT(32, 0))
296
0
      (MI, OpNo, O);
297
0
      break;
298
37.6k
    default:
299
37.6k
      add_cs_detail(MI, Mips_OP_GROUP_Operand, OpNo);
300
37.6k
      printInt64(O, MCOperand_getImm(Op));
301
37.6k
      break;
302
37.6k
    }
303
37.6k
    return;
304
37.6k
  }
305
37.6k
}
306
307
static void printJumpOperand(MCInst *MI, unsigned OpNo, SStream *O)
308
1.58k
{
309
1.58k
  add_cs_detail(MI, Mips_OP_GROUP_JumpOperand, OpNo);
310
1.58k
  MCOperand *Op = MCInst_getOperand(MI, (OpNo));
311
1.58k
  if (MCOperand_isReg(Op))
312
0
    return printRegName(MI, O, MCOperand_getReg(Op));
313
314
  // only the upper bits are needed.
315
1.58k
  uint64_t Base = MI->address & ~0x0fffffffull;
316
1.58k
  uint64_t Target = MCOperand_getImm(Op);
317
1.58k
  printInt64(O, Base | Target);
318
1.58k
}
319
320
static void printBranchOperand(MCInst *MI, uint64_t Address, unsigned OpNo, SStream *O)
321
13.9k
{
322
13.9k
  add_cs_detail(MI, Mips_OP_GROUP_BranchOperand, OpNo);
323
13.9k
  MCOperand *Op = MCInst_getOperand(MI, (OpNo));
324
13.9k
  if (MCOperand_isReg(Op))
325
36
    return printRegName(MI, O, MCOperand_getReg(Op));
326
327
13.9k
    uint64_t Target = Address + MCOperand_getImm(Op);
328
13.9k
  printInt64(O, Target);
329
13.9k
}
330
331
#define DEFINE_printUImm(Bits) \
332
  static void CONCAT(printUImm, CONCAT(Bits, 0))(MCInst * MI, int opNum, \
333
                 SStream *O) \
334
7.92k
  { \
335
7.92k
    add_cs_detail(MI, CONCAT(Mips_OP_GROUP_UImm, CONCAT(Bits, 0)), opNum); \
336
7.92k
    MCOperand *MO = MCInst_getOperand(MI, (opNum)); \
337
7.92k
    if (MCOperand_isImm(MO)) { \
338
7.92k
      uint64_t Imm = MCOperand_getImm(MO); \
339
7.92k
      Imm &= (((uint64_t)1) << Bits) - 1; \
340
7.92k
      printUInt64(O, Imm); \
341
7.92k
      return; \
342
7.92k
    } \
343
7.92k
    MCOperand *Op = MCInst_getOperand(MI, (opNum)); \
344
0
    printRegName(MI, O, MCOperand_getReg(Op)); \
345
0
  }
MipsInstPrinter.c:printUImm_10_0
Line
Count
Source
334
1.13k
  { \
335
1.13k
    add_cs_detail(MI, CONCAT(Mips_OP_GROUP_UImm, CONCAT(Bits, 0)), opNum); \
336
1.13k
    MCOperand *MO = MCInst_getOperand(MI, (opNum)); \
337
1.13k
    if (MCOperand_isImm(MO)) { \
338
1.13k
      uint64_t Imm = MCOperand_getImm(MO); \
339
1.13k
      Imm &= (((uint64_t)1) << Bits) - 1; \
340
1.13k
      printUInt64(O, Imm); \
341
1.13k
      return; \
342
1.13k
    } \
343
1.13k
    MCOperand *Op = MCInst_getOperand(MI, (opNum)); \
344
0
    printRegName(MI, O, MCOperand_getReg(Op)); \
345
0
  }
MipsInstPrinter.c:printUImm_4_0
Line
Count
Source
334
409
  { \
335
409
    add_cs_detail(MI, CONCAT(Mips_OP_GROUP_UImm, CONCAT(Bits, 0)), opNum); \
336
409
    MCOperand *MO = MCInst_getOperand(MI, (opNum)); \
337
409
    if (MCOperand_isImm(MO)) { \
338
409
      uint64_t Imm = MCOperand_getImm(MO); \
339
409
      Imm &= (((uint64_t)1) << Bits) - 1; \
340
409
      printUInt64(O, Imm); \
341
409
      return; \
342
409
    } \
343
409
    MCOperand *Op = MCInst_getOperand(MI, (opNum)); \
344
0
    printRegName(MI, O, MCOperand_getReg(Op)); \
345
0
  }
MipsInstPrinter.c:printUImm_5_0
Line
Count
Source
334
1.89k
  { \
335
1.89k
    add_cs_detail(MI, CONCAT(Mips_OP_GROUP_UImm, CONCAT(Bits, 0)), opNum); \
336
1.89k
    MCOperand *MO = MCInst_getOperand(MI, (opNum)); \
337
1.89k
    if (MCOperand_isImm(MO)) { \
338
1.89k
      uint64_t Imm = MCOperand_getImm(MO); \
339
1.89k
      Imm &= (((uint64_t)1) << Bits) - 1; \
340
1.89k
      printUInt64(O, Imm); \
341
1.89k
      return; \
342
1.89k
    } \
343
1.89k
    MCOperand *Op = MCInst_getOperand(MI, (opNum)); \
344
0
    printRegName(MI, O, MCOperand_getReg(Op)); \
345
0
  }
Unexecuted instantiation: MipsInstPrinter.c:printUImm_26_0
MipsInstPrinter.c:printUImm_8_0
Line
Count
Source
334
159
  { \
335
159
    add_cs_detail(MI, CONCAT(Mips_OP_GROUP_UImm, CONCAT(Bits, 0)), opNum); \
336
159
    MCOperand *MO = MCInst_getOperand(MI, (opNum)); \
337
159
    if (MCOperand_isImm(MO)) { \
338
159
      uint64_t Imm = MCOperand_getImm(MO); \
339
159
      Imm &= (((uint64_t)1) << Bits) - 1; \
340
159
      printUInt64(O, Imm); \
341
159
      return; \
342
159
    } \
343
159
    MCOperand *Op = MCInst_getOperand(MI, (opNum)); \
344
0
    printRegName(MI, O, MCOperand_getReg(Op)); \
345
0
  }
Unexecuted instantiation: MipsInstPrinter.c:printUImm_12_0
MipsInstPrinter.c:printUImm_20_0
Line
Count
Source
334
26
  { \
335
26
    add_cs_detail(MI, CONCAT(Mips_OP_GROUP_UImm, CONCAT(Bits, 0)), opNum); \
336
26
    MCOperand *MO = MCInst_getOperand(MI, (opNum)); \
337
26
    if (MCOperand_isImm(MO)) { \
338
26
      uint64_t Imm = MCOperand_getImm(MO); \
339
26
      Imm &= (((uint64_t)1) << Bits) - 1; \
340
26
      printUInt64(O, Imm); \
341
26
      return; \
342
26
    } \
343
26
    MCOperand *Op = MCInst_getOperand(MI, (opNum)); \
344
0
    printRegName(MI, O, MCOperand_getReg(Op)); \
345
0
  }
MipsInstPrinter.c:printUImm_16_0
Line
Count
Source
334
2.54k
  { \
335
2.54k
    add_cs_detail(MI, CONCAT(Mips_OP_GROUP_UImm, CONCAT(Bits, 0)), opNum); \
336
2.54k
    MCOperand *MO = MCInst_getOperand(MI, (opNum)); \
337
2.54k
    if (MCOperand_isImm(MO)) { \
338
2.54k
      uint64_t Imm = MCOperand_getImm(MO); \
339
2.54k
      Imm &= (((uint64_t)1) << Bits) - 1; \
340
2.54k
      printUInt64(O, Imm); \
341
2.54k
      return; \
342
2.54k
    } \
343
2.54k
    MCOperand *Op = MCInst_getOperand(MI, (opNum)); \
344
0
    printRegName(MI, O, MCOperand_getReg(Op)); \
345
0
  }
Unexecuted instantiation: MipsInstPrinter.c:printUImm_32_0
MipsInstPrinter.c:printUImm_7_0
Line
Count
Source
334
287
  { \
335
287
    add_cs_detail(MI, CONCAT(Mips_OP_GROUP_UImm, CONCAT(Bits, 0)), opNum); \
336
287
    MCOperand *MO = MCInst_getOperand(MI, (opNum)); \
337
287
    if (MCOperand_isImm(MO)) { \
338
287
      uint64_t Imm = MCOperand_getImm(MO); \
339
287
      Imm &= (((uint64_t)1) << Bits) - 1; \
340
287
      printUInt64(O, Imm); \
341
287
      return; \
342
287
    } \
343
287
    MCOperand *Op = MCInst_getOperand(MI, (opNum)); \
344
0
    printRegName(MI, O, MCOperand_getReg(Op)); \
345
0
  }
MipsInstPrinter.c:printUImm_2_0
Line
Count
Source
334
315
  { \
335
315
    add_cs_detail(MI, CONCAT(Mips_OP_GROUP_UImm, CONCAT(Bits, 0)), opNum); \
336
315
    MCOperand *MO = MCInst_getOperand(MI, (opNum)); \
337
315
    if (MCOperand_isImm(MO)) { \
338
315
      uint64_t Imm = MCOperand_getImm(MO); \
339
315
      Imm &= (((uint64_t)1) << Bits) - 1; \
340
315
      printUInt64(O, Imm); \
341
315
      return; \
342
315
    } \
343
315
    MCOperand *Op = MCInst_getOperand(MI, (opNum)); \
344
0
    printRegName(MI, O, MCOperand_getReg(Op)); \
345
0
  }
MipsInstPrinter.c:printUImm_1_0
Line
Count
Source
334
270
  { \
335
270
    add_cs_detail(MI, CONCAT(Mips_OP_GROUP_UImm, CONCAT(Bits, 0)), opNum); \
336
270
    MCOperand *MO = MCInst_getOperand(MI, (opNum)); \
337
270
    if (MCOperand_isImm(MO)) { \
338
270
      uint64_t Imm = MCOperand_getImm(MO); \
339
270
      Imm &= (((uint64_t)1) << Bits) - 1; \
340
270
      printUInt64(O, Imm); \
341
270
      return; \
342
270
    } \
343
270
    MCOperand *Op = MCInst_getOperand(MI, (opNum)); \
344
0
    printRegName(MI, O, MCOperand_getReg(Op)); \
345
0
  }
MipsInstPrinter.c:printUImm_3_0
Line
Count
Source
334
598
  { \
335
598
    add_cs_detail(MI, CONCAT(Mips_OP_GROUP_UImm, CONCAT(Bits, 0)), opNum); \
336
598
    MCOperand *MO = MCInst_getOperand(MI, (opNum)); \
337
598
    if (MCOperand_isImm(MO)) { \
338
598
      uint64_t Imm = MCOperand_getImm(MO); \
339
598
      Imm &= (((uint64_t)1) << Bits) - 1; \
340
598
      printUInt64(O, Imm); \
341
598
      return; \
342
598
    } \
343
598
    MCOperand *Op = MCInst_getOperand(MI, (opNum)); \
344
0
    printRegName(MI, O, MCOperand_getReg(Op)); \
345
0
  }
MipsInstPrinter.c:printUImm_0_0
Line
Count
Source
334
120
  { \
335
120
    add_cs_detail(MI, CONCAT(Mips_OP_GROUP_UImm, CONCAT(Bits, 0)), opNum); \
336
120
    MCOperand *MO = MCInst_getOperand(MI, (opNum)); \
337
120
    if (MCOperand_isImm(MO)) { \
338
120
      uint64_t Imm = MCOperand_getImm(MO); \
339
120
      Imm &= (((uint64_t)1) << Bits) - 1; \
340
120
      printUInt64(O, Imm); \
341
120
      return; \
342
120
    } \
343
120
    MCOperand *Op = MCInst_getOperand(MI, (opNum)); \
344
0
    printRegName(MI, O, MCOperand_getReg(Op)); \
345
0
  }
MipsInstPrinter.c:printUImm_6_0
Line
Count
Source
334
169
  { \
335
169
    add_cs_detail(MI, CONCAT(Mips_OP_GROUP_UImm, CONCAT(Bits, 0)), opNum); \
336
169
    MCOperand *MO = MCInst_getOperand(MI, (opNum)); \
337
169
    if (MCOperand_isImm(MO)) { \
338
169
      uint64_t Imm = MCOperand_getImm(MO); \
339
169
      Imm &= (((uint64_t)1) << Bits) - 1; \
340
169
      printUInt64(O, Imm); \
341
169
      return; \
342
169
    } \
343
169
    MCOperand *Op = MCInst_getOperand(MI, (opNum)); \
344
0
    printRegName(MI, O, MCOperand_getReg(Op)); \
345
0
  }
346
347
#define DEFINE_printUImm_2(Bits, Offset) \
348
  static void CONCAT(printUImm, CONCAT(Bits, Offset))(MCInst * MI, int opNum, \
349
                 SStream *O) \
350
454
  { \
351
454
    add_cs_detail(MI, CONCAT(Mips_OP_GROUP_UImm, CONCAT(Bits, Offset)), \
352
454
            opNum); \
353
454
    MCOperand *MO = MCInst_getOperand(MI, (opNum)); \
354
454
    if (MCOperand_isImm(MO)) { \
355
454
      uint64_t Imm = MCOperand_getImm(MO); \
356
454
      Imm -= Offset; \
357
454
      Imm &= (1 << Bits) - 1; \
358
454
      Imm += Offset; \
359
454
      printUInt64(O, Imm); \
360
454
      return; \
361
454
    } \
362
454
    MCOperand *Op = MCInst_getOperand(MI, (opNum)); \
363
0
    printRegName(MI, O, MCOperand_getReg(Op)); \
364
0
  }
MipsInstPrinter.c:printUImm_2_1
Line
Count
Source
350
133
  { \
351
133
    add_cs_detail(MI, CONCAT(Mips_OP_GROUP_UImm, CONCAT(Bits, Offset)), \
352
133
            opNum); \
353
133
    MCOperand *MO = MCInst_getOperand(MI, (opNum)); \
354
133
    if (MCOperand_isImm(MO)) { \
355
133
      uint64_t Imm = MCOperand_getImm(MO); \
356
133
      Imm -= Offset; \
357
133
      Imm &= (1 << Bits) - 1; \
358
133
      Imm += Offset; \
359
133
      printUInt64(O, Imm); \
360
133
      return; \
361
133
    } \
362
133
    MCOperand *Op = MCInst_getOperand(MI, (opNum)); \
363
0
    printRegName(MI, O, MCOperand_getReg(Op)); \
364
0
  }
Unexecuted instantiation: MipsInstPrinter.c:printUImm_5_32
MipsInstPrinter.c:printUImm_5_1
Line
Count
Source
350
321
  { \
351
321
    add_cs_detail(MI, CONCAT(Mips_OP_GROUP_UImm, CONCAT(Bits, Offset)), \
352
321
            opNum); \
353
321
    MCOperand *MO = MCInst_getOperand(MI, (opNum)); \
354
321
    if (MCOperand_isImm(MO)) { \
355
321
      uint64_t Imm = MCOperand_getImm(MO); \
356
321
      Imm -= Offset; \
357
321
      Imm &= (1 << Bits) - 1; \
358
321
      Imm += Offset; \
359
321
      printUInt64(O, Imm); \
360
321
      return; \
361
321
    } \
362
321
    MCOperand *Op = MCInst_getOperand(MI, (opNum)); \
363
0
    printRegName(MI, O, MCOperand_getReg(Op)); \
364
0
  }
Unexecuted instantiation: MipsInstPrinter.c:printUImm_6_1
Unexecuted instantiation: MipsInstPrinter.c:printUImm_5_33
Unexecuted instantiation: MipsInstPrinter.c:printUImm_6_2
365
366
DEFINE_printUImm(0);
367
DEFINE_printUImm(1);
368
DEFINE_printUImm(10);
369
DEFINE_printUImm(12);
370
DEFINE_printUImm(16);
371
DEFINE_printUImm(2);
372
DEFINE_printUImm(20);
373
DEFINE_printUImm(26);
374
DEFINE_printUImm(3);
375
DEFINE_printUImm(32);
376
DEFINE_printUImm(4);
377
DEFINE_printUImm(5);
378
DEFINE_printUImm(6);
379
DEFINE_printUImm(7);
380
DEFINE_printUImm(8);
381
DEFINE_printUImm_2(2, 1);
382
DEFINE_printUImm_2(5, 1);
383
DEFINE_printUImm_2(5, 32);
384
DEFINE_printUImm_2(5, 33);
385
DEFINE_printUImm_2(6, 1);
386
DEFINE_printUImm_2(6, 2);
387
388
static void printMemOperand(MCInst *MI, int opNum, SStream *O)
389
25.9k
{
390
  // Load/Store memory operands -- imm($reg)
391
  // If PIC target the target is loaded as the
392
  // pattern lw $25,%call16($28)
393
394
  // opNum can be invalid if instruction had reglist as operand.
395
  // MemOperand is always last operand of instruction (base + offset).
396
25.9k
  switch (MCInst_getOpcode(MI)) {
397
25.4k
  default:
398
25.4k
    break;
399
25.4k
  case Mips_SWM32_MM:
400
187
  case Mips_LWM32_MM:
401
314
  case Mips_SWM16_MM:
402
442
  case Mips_SWM16_MMR6:
403
488
  case Mips_LWM16_MM:
404
584
  case Mips_LWM16_MMR6:
405
584
    opNum = MCInst_getNumOperands(MI) - 2;
406
584
    break;
407
25.9k
  }
408
409
25.9k
  set_mem_access(MI, true);
410
  // Index register is encoded as immediate value
411
  // in case of nanoMIPS indexed instructions
412
25.9k
  switch (MCInst_getOpcode(MI)) {
413
  // No offset needed for paired LL/SC
414
0
  case Mips_LLWP_NM:
415
0
  case Mips_SCWP_NM:
416
0
    break;
417
0
  case Mips_LWX_NM:
418
0
  case Mips_LWXS_NM:
419
0
  case Mips_LWXS16_NM:
420
0
  case Mips_LBX_NM:
421
0
  case Mips_LBUX_NM:
422
0
  case Mips_LHX_NM:
423
0
  case Mips_LHUX_NM:
424
0
  case Mips_LHXS_NM:
425
0
  case Mips_LHUXS_NM:
426
0
  case Mips_SWX_NM:
427
0
  case Mips_SWXS_NM:
428
0
  case Mips_SBX_NM:
429
0
  case Mips_SHX_NM:
430
0
  case Mips_SHXS_NM:
431
0
    if (!MCOperand_isReg(MCInst_getOperand(MI, (opNum + 1)))) {
432
0
      add_cs_detail(MI, Mips_OP_GROUP_MemOperand, (opNum + 1));
433
0
      printRegName(MI, O, MCOperand_getImm(MCInst_getOperand(
434
0
            MI, (opNum + 1))));
435
0
      break;
436
0
    }
437
    // Fall through
438
25.9k
  default:
439
25.9k
    printOperand((MCInst *)MI, opNum + 1, O);
440
25.9k
    break;
441
25.9k
  }
442
25.9k
  SStream_concat0(O, "(");
443
25.9k
  printOperand((MCInst *)MI, opNum, O);
444
25.9k
  SStream_concat0(O, ")");
445
25.9k
  set_mem_access(MI, false);
446
25.9k
}
447
448
static void printMemOperandEA(MCInst *MI, int opNum, SStream *O)
449
0
{
450
  // when using stack locations for not load/store instructions
451
  // print the same way as all normal 3 operand instructions.
452
0
  printOperand((MCInst *)MI, opNum, O);
453
0
  SStream_concat0(O, ", ");
454
0
  printOperand((MCInst *)MI, opNum + 1, O);
455
0
}
456
457
static void printFCCOperand(MCInst *MI, int opNum, SStream *O)
458
0
{
459
0
  MCOperand *MO = MCInst_getOperand(MI, (opNum));
460
0
  SStream_concat0(O,
461
0
      MipsFCCToString((Mips_CondCode)MCOperand_getImm(MO)));
462
0
}
463
464
static bool printAlias(const char *Str, const MCInst *MI, uint64_t Address,
465
    unsigned OpNo, SStream *OS, bool IsBranch)
466
221
{
467
221
  SStream_concat(OS, "%s%s", "\t", Str);
468
221
  SStream_concat0(OS, "\t");
469
221
  if (IsBranch)
470
113
    printBranchOperand((MCInst *)MI, Address, OpNo, OS);
471
108
  else
472
108
    printOperand((MCInst *)MI, OpNo, OS);
473
221
  return true;
474
221
}
475
476
static bool printAlias2(const char *Str, const MCInst *MI, uint64_t Address,
477
    unsigned OpNo0, unsigned OpNo1, SStream *OS, bool IsBranch)
478
116
{
479
116
  printAlias(Str, MI, Address, OpNo0, OS, IsBranch);
480
116
  SStream_concat0(OS, ", ");
481
116
  if (IsBranch)
482
36
    printBranchOperand((MCInst *)MI, Address, OpNo1, OS);
483
80
  else
484
80
    printOperand((MCInst *)MI, OpNo1, OS);
485
116
  return true;
486
116
}
487
488
static bool printAlias3(const char *Str, const MCInst *MI, uint64_t Address,
489
    unsigned OpNo0, unsigned OpNo1, unsigned OpNo2, SStream *OS)
490
0
{
491
0
  printAlias(Str, MI, Address, OpNo0, OS, false);
492
0
  SStream_concat0(OS, ", ");
493
0
  printOperand((MCInst *)MI, OpNo1, OS);
494
0
  SStream_concat0(OS, ", ");
495
0
  printOperand((MCInst *)MI, OpNo2, OS);
496
0
  return true;
497
0
}
498
499
static bool printAlias4(const MCInst *MI, uint64_t Address, SStream *OS)
500
74.3k
{
501
74.3k
  switch (MCInst_getOpcode(MI)) {
502
670
  case Mips_BEQ:
503
1.03k
  case Mips_BEQ_MM:
504
    // beq $zero, $zero, $L2 => b $L2
505
    // beq $r0, $zero, $L2 => beqz $r0, $L2
506
1.03k
    return (isReg(MI, 0, Mips_ZERO) &&
507
1.03k
      isReg(MI, 1, Mips_ZERO) &&
508
1.03k
      printAlias("b", MI, Address, 2, OS, true)) ||
509
1.03k
           (isReg(MI, 1, Mips_ZERO) &&
510
953
      printAlias2("beqz", MI, Address, 0, 2, OS, true));
511
0
  case Mips_BEQ64:
512
    // beq $r0, $zero, $L2 => beqz $r0, $L2
513
0
    return isReg(MI, 1, Mips_ZERO_64) &&
514
0
           printAlias2("beqz", MI, Address, 0, 2, OS, true);
515
208
  case Mips_BNE:
516
441
  case Mips_BNE_MM:
517
    // bne $r0, $zero, $L2 => bnez $r0, $L2
518
441
    return isReg(MI, 1, Mips_ZERO) &&
519
441
           printAlias2("bnez", MI, Address, 0, 2, OS, true);
520
0
  case Mips_BNE64:
521
    // bne $r0, $zero, $L2 => bnez $r0, $L2
522
0
    return isReg(MI, 1, Mips_ZERO_64) &&
523
0
           printAlias2("bnez", MI, Address, 0, 2, OS, true);
524
133
  case Mips_BGEZAL:
525
    // bgezal $zero, $L1 => bal $L1
526
133
    return isReg(MI, 0, Mips_ZERO) &&
527
133
           printAlias("bal", MI, Address, 1, OS, true);
528
144
  case Mips_BC1T:
529
    // bc1t $fcc0, $L1 => bc1t $L1
530
144
    return isReg(MI, 0, Mips_FCC0) &&
531
144
           printAlias("bc1t", MI, Address, 1, OS, true);
532
70
  case Mips_BC1F:
533
    // bc1f $fcc0, $L1 => bc1f $L1
534
70
    return isReg(MI, 0, Mips_FCC0) &&
535
70
           printAlias("bc1f", MI, Address, 1, OS, true);
536
94
  case Mips_JALR:
537
    // jalr $zero, $r1 => jr $r1
538
    // jalr $ra, $r1 => jalr $r1
539
94
    return (isReg(MI, 0, Mips_ZERO) &&
540
94
      printAlias("jr", MI, Address, 1, OS, false)) ||
541
94
           (isReg(MI, 0, Mips_RA) &&
542
73
      printAlias("jalr", MI, Address, 1, OS, false));
543
0
  case Mips_JALR64:
544
    // jalr $zero, $r1 => jr $r1
545
    // jalr $ra, $r1 => jalr $r1
546
0
    return (isReg(MI, 0, Mips_ZERO_64) &&
547
0
      printAlias("jr", MI, Address, 1, OS, false)) ||
548
0
           (isReg(MI, 0, Mips_RA_64) &&
549
0
      printAlias("jalr", MI, Address, 1, OS, false));
550
193
  case Mips_NOR:
551
200
  case Mips_NOR_MM:
552
231
  case Mips_NOR_MMR6:
553
    // nor $r0, $r1, $zero => not $r0, $r1
554
231
    return isReg(MI, 2, Mips_ZERO) &&
555
231
           printAlias2("not", MI, Address, 0, 1, OS, false);
556
0
  case Mips_NOR64:
557
    // nor $r0, $r1, $zero => not $r0, $r1
558
0
    return isReg(MI, 2, Mips_ZERO_64) &&
559
0
           printAlias2("not", MI, Address, 0, 1, OS, false);
560
48
  case Mips_OR:
561
89
  case Mips_ADDu:
562
    // or $r0, $r1, $zero => move $r0, $r1
563
    // addu $r0, $r1, $zero => move $r0, $r1
564
89
    return isReg(MI, 2, Mips_ZERO) &&
565
89
           printAlias2("move", MI, Address, 0, 1, OS, false);
566
0
  case Mips_LI48_NM:
567
0
  case Mips_LI16_NM:
568
    // li[16/48] $r0, imm => li $r0, imm
569
0
    return printAlias2("li", MI, Address, 0, 1, OS, false);
570
0
  case Mips_ADDIU_NM:
571
0
  case Mips_ADDIUNEG_NM:
572
0
    if (isReg(MI, 1, Mips_ZERO_NM))
573
0
      return printAlias2("li", MI, Address, 0, 2, OS, false);
574
0
    else
575
0
      return printAlias3("addiu", MI, Address, 0, 1, 2, OS);
576
0
  case Mips_ADDIU48_NM:
577
0
  case Mips_ADDIURS5_NM:
578
0
  case Mips_ADDIUR1SP_NM:
579
0
  case Mips_ADDIUR2_NM:
580
0
  case Mips_ADDIUGPB_NM:
581
0
  case Mips_ADDIUGPW_NM:
582
0
    return printAlias3("addiu", MI, Address, 0, 1, 2, OS);
583
0
  case Mips_ANDI16_NM:
584
0
  case Mips_ANDI_NM:
585
    // andi[16/32] $r0, $r1, imm => andi $r0, $r1, imm
586
0
    return printAlias3("andi", MI, Address, 0, 1, 2, OS);
587
72.1k
  default:
588
72.1k
    return false;
589
74.3k
  }
590
74.3k
}
591
592
static void printRegisterList(MCInst *MI, int opNum, SStream *O)
593
584
{
594
  // - 2 because register List is always first operand of instruction and it is
595
  // always followed by memory operand (base + offset).
596
584
  add_cs_detail(MI, Mips_OP_GROUP_RegisterList, opNum);
597
2.90k
  for (int i = opNum, e = MCInst_getNumOperands(MI) - 2; i != e; ++i) {
598
2.31k
    if (i != opNum)
599
1.73k
      SStream_concat0(O, ", ");
600
2.31k
    printRegName(MI, O, MCOperand_getReg(MCInst_getOperand(MI, (i))));
601
2.31k
  }
602
584
}
603
604
static void printNanoMipsRegisterList(MCInst *MI, int OpNum, SStream *O)
605
0
{
606
0
  add_cs_detail(MI, Mips_OP_GROUP_NanoMipsRegisterList, OpNum);
607
0
  for (unsigned I = OpNum; I < MCInst_getNumOperands(MI); I++) {
608
0
    SStream_concat0(O, ", ");
609
0
    printRegName(MI, O, MCOperand_getReg(MCInst_getOperand(MI, (I))));
610
0
  }
611
0
}
612
613
static void printHi20(MCInst *MI, int OpNum, SStream *O)
614
0
{
615
0
  MCOperand *MO = MCInst_getOperand(MI, (OpNum));
616
0
  if (MCOperand_isImm(MO)) {
617
0
    add_cs_detail(MI, Mips_OP_GROUP_Hi20, OpNum);
618
0
    SStream_concat0(O, "%hi(");
619
0
    printUInt64(O, MCOperand_getImm(MO));
620
0
    SStream_concat0(O, ")");
621
0
  } else
622
0
    printOperand(MI, OpNum, O);
623
0
}
624
625
static void printHi20PCRel(MCInst *MI, uint64_t Address, int OpNum, SStream *O)
626
0
{
627
0
  MCOperand *MO = MCInst_getOperand(MI, (OpNum));
628
0
  if (MCOperand_isImm(MO)) {
629
0
    add_cs_detail(MI, Mips_OP_GROUP_Hi20PCRel, OpNum);
630
0
    SStream_concat0(O, "%pcrel_hi(");
631
0
    printUInt64(O, MCOperand_getImm(MO) + Address);
632
0
    SStream_concat0(O, ")");
633
0
  } else
634
0
    printOperand(MI, OpNum, O);
635
0
}
636
637
static void printPCRel(MCInst *MI, uint64_t Address, int OpNum, SStream *O)
638
0
{
639
0
  MCOperand *MO = MCInst_getOperand(MI, (OpNum));
640
0
  if (MCOperand_isImm(MO)) {
641
0
    add_cs_detail(MI, Mips_OP_GROUP_PCRel, OpNum);
642
0
    printUInt64(O, MCOperand_getImm(MO) + Address);
643
0
  }
644
0
  else
645
0
    printOperand(MI, OpNum, O);
646
0
}
647
648
const char *Mips_LLVM_getRegisterName(unsigned RegNo, bool noRegName)
649
162k
{
650
162k
  if (!RegNo || RegNo >= MIPS_REG_ENDING) {
651
0
    return NULL;
652
0
  }
653
162k
  if (noRegName) {
654
0
    return getRegisterName(RegNo);
655
0
  }
656
162k
  switch(RegNo) {
657
16.2k
  case MIPS_REG_AT:
658
16.6k
  case MIPS_REG_AT_64:
659
16.6k
    return "at";
660
6.89k
  case MIPS_REG_A0:
661
7.02k
  case MIPS_REG_A0_64:
662
7.02k
    return "a0";
663
4.71k
  case MIPS_REG_A1:
664
4.83k
  case MIPS_REG_A1_64:
665
4.83k
    return "a1";
666
10.5k
  case MIPS_REG_A2:
667
10.7k
  case MIPS_REG_A2_64:
668
10.7k
    return "a2";
669
5.70k
  case MIPS_REG_A3:
670
5.97k
  case MIPS_REG_A3_64:
671
5.97k
    return "a3";
672
1.08k
  case MIPS_REG_K0:
673
1.13k
  case MIPS_REG_K0_64:
674
1.13k
    return "k0";
675
2.76k
  case MIPS_REG_K1:
676
3.05k
  case MIPS_REG_K1_64:
677
3.05k
    return "k1";
678
13.8k
  case MIPS_REG_S0:
679
14.0k
  case MIPS_REG_S0_64:
680
14.0k
    return "s0";
681
4.83k
  case MIPS_REG_S1:
682
4.93k
  case MIPS_REG_S1_64:
683
4.93k
    return "s1";
684
1.76k
  case MIPS_REG_S2:
685
1.82k
  case MIPS_REG_S2_64:
686
1.82k
    return "s2";
687
2.42k
  case MIPS_REG_S3:
688
3.05k
  case MIPS_REG_S3_64:
689
3.05k
    return "s3";
690
1.82k
  case MIPS_REG_S4:
691
1.90k
  case MIPS_REG_S4_64:
692
1.90k
    return "s4";
693
1.09k
  case MIPS_REG_S5:
694
1.10k
  case MIPS_REG_S5_64:
695
1.10k
    return "s5";
696
2.59k
  case MIPS_REG_S6:
697
2.79k
  case MIPS_REG_S6_64:
698
2.79k
    return "s6";
699
1.90k
  case MIPS_REG_S7:
700
1.94k
  case MIPS_REG_S7_64:
701
1.94k
    return "s7";
702
3.02k
  case MIPS_REG_T0:
703
3.28k
  case MIPS_REG_T0_64:
704
3.28k
    return "t0";
705
1.62k
  case MIPS_REG_T1:
706
1.68k
  case MIPS_REG_T1_64:
707
1.68k
    return "t1";
708
1.36k
  case MIPS_REG_T2:
709
1.39k
  case MIPS_REG_T2_64:
710
1.39k
    return "t2";
711
1.21k
  case MIPS_REG_T3:
712
1.60k
  case MIPS_REG_T3_64:
713
1.60k
    return "t3";
714
1.11k
  case MIPS_REG_T4:
715
1.18k
  case MIPS_REG_T4_64:
716
1.18k
    return "t4";
717
1.93k
  case MIPS_REG_T5:
718
2.14k
  case MIPS_REG_T5_64:
719
2.14k
    return "t5";
720
2.20k
  case MIPS_REG_T6:
721
2.30k
  case MIPS_REG_T6_64:
722
2.30k
    return "t6";
723
1.84k
  case MIPS_REG_T7:
724
1.98k
  case MIPS_REG_T7_64:
725
1.98k
    return "t7";
726
2.54k
  case MIPS_REG_T8:
727
2.64k
  case MIPS_REG_T8_64:
728
2.64k
    return "t8";
729
3.13k
  case MIPS_REG_T9:
730
3.18k
  case MIPS_REG_T9_64:
731
3.18k
    return "t9";
732
6.23k
  case MIPS_REG_V0:
733
6.62k
  case MIPS_REG_V0_64:
734
6.62k
    return "v0";
735
7.18k
  case MIPS_REG_V1:
736
7.34k
  case MIPS_REG_V1_64:
737
7.34k
    return "v1";
738
46.1k
  default:
739
46.1k
    return getRegisterName(RegNo);
740
162k
  }
741
162k
}