Coverage Report

Created: 2025-07-04 06:11

/src/capstonenext/MCInstPrinter.c
Line
Count
Source (jump to first uncovered line)
1
/* Capstone Disassembly Engine */
2
/* By Rot127 <unisono@quyllur.org>, 2023 */
3
4
#include "MCInstPrinter.h"
5
#include "cs_priv.h"
6
#include <capstone/platform.h>
7
8
extern bool ARM_getFeatureBits(unsigned int mode, unsigned int feature);
9
extern bool PPC_getFeatureBits(unsigned int mode, unsigned int feature);
10
extern bool Mips_getFeatureBits(unsigned int mode, unsigned int feature);
11
extern bool AArch64_getFeatureBits(unsigned int mode, unsigned int feature);
12
extern bool TriCore_getFeatureBits(unsigned int mode, unsigned int feature);
13
extern bool Sparc_getFeatureBits(unsigned int mode, unsigned int feature);
14
15
static bool testFeatureBits(const MCInst *MI, uint32_t Value)
16
71.3k
{
17
71.3k
  assert(MI && MI->csh);
18
71.3k
  switch (MI->csh->arch) {
19
0
  default:
20
0
    assert(0 && "Not implemented for current arch.");
21
0
    return false;
22
0
#ifdef CAPSTONE_HAS_ARM
23
5.53k
  case CS_ARCH_ARM:
24
5.53k
    return ARM_getFeatureBits(MI->csh->mode, Value);
25
0
#endif
26
0
#ifdef CAPSTONE_HAS_POWERPC
27
585
  case CS_ARCH_PPC:
28
585
    return PPC_getFeatureBits(MI->csh->mode, Value);
29
0
#endif
30
0
#ifdef CAPSTONE_HAS_MIPS
31
11.0k
  case CS_ARCH_MIPS:
32
11.0k
    return Mips_getFeatureBits(MI->csh->mode, Value);
33
0
#endif
34
0
#ifdef CAPSTONE_HAS_AARCH64
35
49.6k
  case CS_ARCH_AARCH64:
36
49.6k
    return AArch64_getFeatureBits(MI->csh->mode, Value);
37
0
#endif
38
0
#ifdef CAPSTONE_HAS_TRICORE
39
0
  case CS_ARCH_TRICORE:
40
0
    return TriCore_getFeatureBits(MI->csh->mode, Value);
41
0
#endif
42
0
#ifdef CAPSTONE_HAS_SPARC
43
4.48k
  case CS_ARCH_SPARC:
44
4.48k
    return Sparc_getFeatureBits(MI->csh->mode, Value);
45
71.3k
#endif
46
71.3k
  }
47
71.3k
}
48
49
static bool matchAliasCondition(MCInst *MI, const MCRegisterInfo *MRI,
50
                unsigned *OpIdx, const AliasMatchingData *M,
51
                const AliasPatternCond *C,
52
                bool *OrPredicateResult)
53
710k
{
54
  // Feature tests are special, they don't consume operands.
55
710k
  if (C->Kind == AliasPatternCond_K_Feature)
56
13.8k
    return testFeatureBits(MI, C->Value);
57
696k
  if (C->Kind == AliasPatternCond_K_NegFeature)
58
7.37k
    return !testFeatureBits(MI, C->Value);
59
  // For feature tests where just one feature is required in a list, set the
60
  // predicate result bit to whether the expression will return true, and only
61
  // return the real result at the end of list marker.
62
689k
  if (C->Kind == AliasPatternCond_K_OrFeature) {
63
49.8k
    *OrPredicateResult |= testFeatureBits(MI, C->Value);
64
49.8k
    return true;
65
49.8k
  }
66
639k
  if (C->Kind == AliasPatternCond_K_OrNegFeature) {
67
191
    *OrPredicateResult |= !(testFeatureBits(MI, C->Value));
68
191
    return true;
69
191
  }
70
639k
  if (C->Kind == AliasPatternCond_K_EndOrFeatures) {
71
20.3k
    bool Res = *OrPredicateResult;
72
20.3k
    *OrPredicateResult = false;
73
20.3k
    return Res;
74
20.3k
  }
75
76
  // Get and consume an operand.
77
618k
  MCOperand *Opnd = MCInst_getOperand(MI, *OpIdx);
78
618k
  ++(*OpIdx);
79
80
  // Check the specific condition for the operand.
81
618k
  switch (C->Kind) {
82
0
  default:
83
0
    assert(0 && "invalid kind");
84
215k
  case AliasPatternCond_K_Imm:
85
    // Operand must be a specific immediate.
86
215k
    return MCOperand_isImm(Opnd) &&
87
215k
         MCOperand_getImm(Opnd) == (int32_t)C->Value;
88
43.7k
  case AliasPatternCond_K_Reg:
89
    // Operand must be a specific register.
90
43.7k
    return MCOperand_isReg(Opnd) && MCOperand_getReg(Opnd) == C->Value;
91
2.01k
  case AliasPatternCond_K_TiedReg:
92
    // Operand must match the register of another operand.
93
2.01k
    return MCOperand_isReg(Opnd) &&
94
2.01k
         MCOperand_getReg(Opnd) ==
95
2.01k
           MCOperand_getReg(MCInst_getOperand(MI, C->Value));
96
245k
  case AliasPatternCond_K_RegClass:
97
    // Operand must be a register in this class. Value is a register class
98
    // id.
99
245k
    return MCOperand_isReg(Opnd) &&
100
245k
         MCRegisterClass_contains(
101
245k
           MCRegisterInfo_getRegClass(MRI, C->Value),
102
245k
           MCOperand_getReg(Opnd));
103
18.7k
  case AliasPatternCond_K_Custom:
104
    // Operand must match some custom criteria.
105
18.7k
    assert(M->ValidateMCOperand && "A custom validator should be set but isn't.");
106
18.7k
    return M->ValidateMCOperand(Opnd, C->Value);
107
93.8k
  case AliasPatternCond_K_Ignore:
108
    // Operand can be anything.
109
93.8k
    return true;
110
0
  case AliasPatternCond_K_Feature:
111
0
  case AliasPatternCond_K_NegFeature:
112
0
  case AliasPatternCond_K_OrFeature:
113
0
  case AliasPatternCond_K_OrNegFeature:
114
0
  case AliasPatternCond_K_EndOrFeatures:
115
0
    assert(0 && "handled earlier");
116
618k
  }
117
0
  return false;
118
618k
}
119
120
/// Check if PatternsForOpcode is all zero.
121
static inline bool validOpToPatter(const PatternsForOpcode *P)
122
156M
{
123
156M
  return !(P->Opcode == 0 && P->PatternStart == 0 && P->NumPatterns == 0);
124
156M
}
125
126
const char *matchAliasPatterns(MCInst *MI, const AliasMatchingData *M)
127
1.45M
{
128
  // TODO Rewrite to C
129
130
  // auto It = lower_bound(M.OpToPatterns, MI->getOpcode(),
131
  //                       [](const PatternsForOpcode &L, unsigned Opcode) {
132
  //                         return L.Opcode < Opcode;
133
  //                       });
134
  // if (It == M.OpToPatterns.end() || It->Opcode != MI->getOpcode())
135
  //   return nullptr;
136
137
  // Binary search by opcode. Return false if there are no aliases for this
138
  // opcode.
139
1.45M
  unsigned MIOpcode = MI->Opcode;
140
1.45M
  size_t i = 0;
141
1.45M
  uint32_t PatternOpcode = M->OpToPatterns[i].Opcode;
142
157M
  while (PatternOpcode < MIOpcode && validOpToPatter(&M->OpToPatterns[i]))
143
156M
    PatternOpcode = M->OpToPatterns[++i].Opcode;
144
1.45M
  if (PatternOpcode != MI->Opcode || !validOpToPatter(&M->OpToPatterns[i]))
145
1.34M
    return NULL;
146
147
  // // Try all patterns for this opcode.
148
111k
  uint32_t AsmStrOffset = ~0U;
149
111k
  const AliasPattern *Patterns = M->Patterns + M->OpToPatterns[i].PatternStart;
150
111k
  for (const AliasPattern *P = Patterns;
151
349k
    P != Patterns + M->OpToPatterns[i].NumPatterns; ++P) {
152
    // Check operand count first.
153
280k
    if (MCInst_getNumOperands(MI) != P->NumOperands)
154
0
      return NULL;
155
156
    // Test all conditions for this pattern.
157
280k
    const AliasPatternCond *Conds = M->PatternConds + P->AliasCondStart;
158
280k
    unsigned OpIdx = 0;
159
280k
    bool OrPredicateResult = false;
160
280k
    bool allMatch = true;
161
752k
    for (const AliasPatternCond *C = Conds; C != Conds + P->NumConds; ++C) {
162
710k
      if (!matchAliasCondition(MI, MI->MRI, &OpIdx, M, C, &OrPredicateResult)) {
163
237k
        allMatch = false;
164
237k
        break;
165
237k
      }
166
710k
    }
167
280k
    if (allMatch) {
168
42.4k
      AsmStrOffset = P->AsmStrOffset;
169
42.4k
      break;
170
42.4k
    }
171
280k
  }
172
  // If no alias matched, don't print an alias.
173
111k
  if (AsmStrOffset == ~0U)
174
68.8k
    return NULL;
175
176
  // Go to offset AsmStrOffset and use the null terminated string there. The
177
  // offset should point to the beginning of an alias string, so it should
178
  // either be zero or be preceded by a null byte.
179
42.4k
  return M->AsmStrings + AsmStrOffset;
180
111k
}
181
182
// TODO Add functionality to toggle the flag.
183
7.75M
bool getUseMarkup(void) { return false; }
184
185
/// Utility functions to make adding mark ups simpler.
186
const char *markup(const char *s)
187
7.61M
{
188
7.61M
  static const char *no_markup = "";
189
7.61M
  if (getUseMarkup())
190
0
    return s;
191
7.61M
  else
192
7.61M
    return no_markup;
193
7.61M
}
194
195
// binary search for encoding in IndexType array
196
// return -1 if not found, or index if found
197
unsigned int binsearch_IndexTypeEncoding(const struct IndexType *index, size_t size, uint16_t encoding)
198
85.3k
{
199
  // binary searching since the index is sorted in encoding order
200
85.3k
  size_t left, right, m;
201
202
85.3k
  right = size - 1;
203
204
85.3k
  if (encoding < index[0].encoding || encoding > index[right].encoding)
205
    // not found
206
17.3k
    return -1;
207
208
68.0k
  left = 0;
209
210
332k
  while(left <= right) {
211
308k
    m = (left + right) / 2;
212
308k
    if (encoding == index[m].encoding) {
213
      // LLVM actually uses lower_bound for the index table search
214
      // Here we need to check if a previous entry is of the same encoding
215
      // and return the first one.
216
44.0k
      while (m > 0 && encoding == index[m - 1].encoding)
217
0
        --m;
218
44.0k
      return m;
219
44.0k
    }
220
221
264k
    if (encoding < index[m].encoding)
222
99.9k
      right = m - 1;
223
164k
    else
224
164k
      left = m + 1;
225
264k
  }
226
227
  // not found
228
24.0k
  return -1;
229
68.0k
}
230
231
// binary search for encoding in IndexTypeStr array
232
// return -1 if not found, or index if found
233
unsigned int binsearch_IndexTypeStrEncoding(const struct IndexTypeStr *index, size_t size, const char *name)
234
1.58k
{
235
  // binary searching since the index is sorted in encoding order
236
1.58k
  size_t left, right, m;
237
238
1.58k
  right = size - 1;
239
240
1.58k
  int str_left_cmp = strcmp(name, index[0].name);
241
1.58k
  int str_right_cmp = strcmp(name, index[right].name);
242
1.58k
  if (str_left_cmp < 0 || str_right_cmp > 0)
243
    // not found
244
0
    return -1;
245
246
1.58k
  left = 0;
247
248
16.4k
  while(left <= right) {
249
16.4k
    m = (left + right) / 2;
250
16.4k
    if (strcmp(name, index[m].name) == 0) {
251
      // LLVM actually uses lower_bound for the index table search
252
      // Here we need to check if a previous entry is of the same encoding
253
      // and return the first one.
254
1.58k
      while (m > 0 && (strcmp(name, index[m - 1].name) == 0))
255
0
        --m;
256
1.58k
      return m;
257
1.58k
    }
258
259
14.8k
    if (strcmp(name, index[m].name) < 0)
260
6.44k
      right = m - 1;
261
8.45k
    else
262
8.45k
      left = m + 1;
263
14.8k
  }
264
265
  // not found
266
6
  return -1;
267
1.58k
}