Coverage Report

Created: 2025-07-18 06:43

/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
101k
{
17
101k
  assert(MI && MI->csh);
18
101k
  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
7.76k
  case CS_ARCH_ARM:
24
7.76k
    return ARM_getFeatureBits(MI->csh->mode, Value);
25
0
#endif
26
0
#ifdef CAPSTONE_HAS_POWERPC
27
747
  case CS_ARCH_PPC:
28
747
    return PPC_getFeatureBits(MI->csh->mode, Value);
29
0
#endif
30
0
#ifdef CAPSTONE_HAS_MIPS
31
13.5k
  case CS_ARCH_MIPS:
32
13.5k
    return Mips_getFeatureBits(MI->csh->mode, Value);
33
0
#endif
34
0
#ifdef CAPSTONE_HAS_AARCH64
35
76.5k
  case CS_ARCH_AARCH64:
36
76.5k
    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
2.92k
  case CS_ARCH_SPARC:
44
2.92k
    return Sparc_getFeatureBits(MI->csh->mode, Value);
45
101k
#endif
46
101k
  }
47
101k
}
48
49
static bool matchAliasCondition(MCInst *MI, const MCRegisterInfo *MRI,
50
                unsigned *OpIdx, const AliasMatchingData *M,
51
                const AliasPatternCond *C,
52
                bool *OrPredicateResult)
53
928k
{
54
  // Feature tests are special, they don't consume operands.
55
928k
  if (C->Kind == AliasPatternCond_K_Feature)
56
14.9k
    return testFeatureBits(MI, C->Value);
57
913k
  if (C->Kind == AliasPatternCond_K_NegFeature)
58
9.59k
    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
904k
  if (C->Kind == AliasPatternCond_K_OrFeature) {
63
76.7k
    *OrPredicateResult |= testFeatureBits(MI, C->Value);
64
76.7k
    return true;
65
76.7k
  }
66
827k
  if (C->Kind == AliasPatternCond_K_OrNegFeature) {
67
219
    *OrPredicateResult |= !(testFeatureBits(MI, C->Value));
68
219
    return true;
69
219
  }
70
827k
  if (C->Kind == AliasPatternCond_K_EndOrFeatures) {
71
30.8k
    bool Res = *OrPredicateResult;
72
30.8k
    *OrPredicateResult = false;
73
30.8k
    return Res;
74
30.8k
  }
75
76
  // Get and consume an operand.
77
796k
  MCOperand *Opnd = MCInst_getOperand(MI, *OpIdx);
78
796k
  ++(*OpIdx);
79
80
  // Check the specific condition for the operand.
81
796k
  switch (C->Kind) {
82
0
  default:
83
0
    assert(0 && "invalid kind");
84
261k
  case AliasPatternCond_K_Imm:
85
    // Operand must be a specific immediate.
86
261k
    return MCOperand_isImm(Opnd) &&
87
261k
         MCOperand_getImm(Opnd) == (int32_t)C->Value;
88
59.4k
  case AliasPatternCond_K_Reg:
89
    // Operand must be a specific register.
90
59.4k
    return MCOperand_isReg(Opnd) && MCOperand_getReg(Opnd) == C->Value;
91
2.61k
  case AliasPatternCond_K_TiedReg:
92
    // Operand must match the register of another operand.
93
2.61k
    return MCOperand_isReg(Opnd) &&
94
2.61k
         MCOperand_getReg(Opnd) ==
95
2.61k
           MCOperand_getReg(MCInst_getOperand(MI, C->Value));
96
320k
  case AliasPatternCond_K_RegClass:
97
    // Operand must be a register in this class. Value is a register class
98
    // id.
99
320k
    return MCOperand_isReg(Opnd) &&
100
320k
         MCRegisterClass_contains(
101
320k
           MCRegisterInfo_getRegClass(MRI, C->Value),
102
320k
           MCOperand_getReg(Opnd));
103
35.9k
  case AliasPatternCond_K_Custom:
104
    // Operand must match some custom criteria.
105
35.9k
    assert(M->ValidateMCOperand && "A custom validator should be set but isn't.");
106
35.9k
    return M->ValidateMCOperand(Opnd, C->Value);
107
117k
  case AliasPatternCond_K_Ignore:
108
    // Operand can be anything.
109
117k
    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
796k
  }
117
0
  return false;
118
796k
}
119
120
/// Check if PatternsForOpcode is all zero.
121
static inline bool validOpToPatter(const PatternsForOpcode *P)
122
199M
{
123
199M
  return !(P->Opcode == 0 && P->PatternStart == 0 && P->NumPatterns == 0);
124
199M
}
125
126
const char *matchAliasPatterns(MCInst *MI, const AliasMatchingData *M)
127
1.73M
{
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.73M
  unsigned MIOpcode = MI->Opcode;
140
1.73M
  size_t i = 0;
141
1.73M
  uint32_t PatternOpcode = M->OpToPatterns[i].Opcode;
142
200M
  while (PatternOpcode < MIOpcode && validOpToPatter(&M->OpToPatterns[i]))
143
198M
    PatternOpcode = M->OpToPatterns[++i].Opcode;
144
1.73M
  if (PatternOpcode != MI->Opcode || !validOpToPatter(&M->OpToPatterns[i]))
145
1.58M
    return NULL;
146
147
  // // Try all patterns for this opcode.
148
146k
  uint32_t AsmStrOffset = ~0U;
149
146k
  const AliasPattern *Patterns = M->Patterns + M->OpToPatterns[i].PatternStart;
150
146k
  for (const AliasPattern *P = Patterns;
151
452k
    P != Patterns + M->OpToPatterns[i].NumPatterns; ++P) {
152
    // Check operand count first.
153
361k
    if (MCInst_getNumOperands(MI) != P->NumOperands)
154
0
      return NULL;
155
156
    // Test all conditions for this pattern.
157
361k
    const AliasPatternCond *Conds = M->PatternConds + P->AliasCondStart;
158
361k
    unsigned OpIdx = 0;
159
361k
    bool OrPredicateResult = false;
160
361k
    bool allMatch = true;
161
984k
    for (const AliasPatternCond *C = Conds; C != Conds + P->NumConds; ++C) {
162
928k
      if (!matchAliasCondition(MI, MI->MRI, &OpIdx, M, C, &OrPredicateResult)) {
163
305k
        allMatch = false;
164
305k
        break;
165
305k
      }
166
928k
    }
167
361k
    if (allMatch) {
168
55.5k
      AsmStrOffset = P->AsmStrOffset;
169
55.5k
      break;
170
55.5k
    }
171
361k
  }
172
  // If no alias matched, don't print an alias.
173
146k
  if (AsmStrOffset == ~0U)
174
91.2k
    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
55.5k
  return M->AsmStrings + AsmStrOffset;
180
146k
}
181
182
// TODO Add functionality to toggle the flag.
183
9.32M
bool getUseMarkup(void) { return false; }
184
185
/// Utility functions to make adding mark ups simpler.
186
const char *markup(const char *s)
187
9.16M
{
188
9.16M
  static const char *no_markup = "";
189
9.16M
  if (getUseMarkup())
190
0
    return s;
191
9.16M
  else
192
9.16M
    return no_markup;
193
9.16M
}
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
125k
{
199
  // binary searching since the index is sorted in encoding order
200
125k
  size_t left, right, m;
201
202
125k
  right = size - 1;
203
204
125k
  if (encoding < index[0].encoding || encoding > index[right].encoding)
205
    // not found
206
23.5k
    return -1;
207
208
101k
  left = 0;
209
210
487k
  while(left <= right) {
211
452k
    m = (left + right) / 2;
212
452k
    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
66.7k
      while (m > 0 && encoding == index[m - 1].encoding)
217
0
        --m;
218
66.7k
      return m;
219
66.7k
    }
220
221
385k
    if (encoding < index[m].encoding)
222
147k
      right = m - 1;
223
237k
    else
224
237k
      left = m + 1;
225
385k
  }
226
227
  // not found
228
34.9k
  return -1;
229
101k
}
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
2.40k
{
235
  // binary searching since the index is sorted in encoding order
236
2.40k
  size_t left, right, m;
237
238
2.40k
  right = size - 1;
239
240
2.40k
  int str_left_cmp = strcmp(name, index[0].name);
241
2.40k
  int str_right_cmp = strcmp(name, index[right].name);
242
2.40k
  if (str_left_cmp < 0 || str_right_cmp > 0)
243
    // not found
244
0
    return -1;
245
246
2.40k
  left = 0;
247
248
24.9k
  while(left <= right) {
249
24.9k
    m = (left + right) / 2;
250
24.9k
    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
2.39k
      while (m > 0 && (strcmp(name, index[m - 1].name) == 0))
255
0
        --m;
256
2.39k
      return m;
257
2.39k
    }
258
259
22.5k
    if (strcmp(name, index[m].name) < 0)
260
9.58k
      right = m - 1;
261
12.9k
    else
262
12.9k
      left = m + 1;
263
22.5k
  }
264
265
  // not found
266
14
  return -1;
267
2.40k
}