/src/capstonenext/MCInstPrinter.c
Line  | Count  | Source  | 
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  | 118k  | { | 
17  | 118k  |   assert(MI && MI->csh);  | 
18  | 118k  |   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  | 8.99k  |   case CS_ARCH_ARM:  | 
24  | 8.99k  |     return ARM_getFeatureBits(MI->csh->mode, Value);  | 
25  | 0  | #endif  | 
26  | 0  | #ifdef CAPSTONE_HAS_POWERPC  | 
27  | 2.12k  |   case CS_ARCH_PPC:  | 
28  | 2.12k  |     return PPC_getFeatureBits(MI->csh->mode, Value);  | 
29  | 0  | #endif  | 
30  | 0  | #ifdef CAPSTONE_HAS_MIPS  | 
31  | 12.4k  |   case CS_ARCH_MIPS:  | 
32  | 12.4k  |     return Mips_getFeatureBits(MI->csh->mode, Value);  | 
33  | 0  | #endif  | 
34  | 0  | #ifdef CAPSTONE_HAS_AARCH64  | 
35  | 92.3k  |   case CS_ARCH_AARCH64:  | 
36  | 92.3k  |     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.32k  |   case CS_ARCH_SPARC:  | 
44  | 2.32k  |     return Sparc_getFeatureBits(MI->csh->mode, Value);  | 
45  | 118k  | #endif  | 
46  | 118k  |   }  | 
47  | 118k  | }  | 
48  |  |  | 
49  |  | static bool matchAliasCondition(MCInst *MI, const MCRegisterInfo *MRI,  | 
50  |  |         unsigned *OpIdx, const AliasMatchingData *M,  | 
51  |  |         const AliasPatternCond *C,  | 
52  |  |         bool *OrPredicateResult)  | 
53  | 1.05M  | { | 
54  |  |   // Feature tests are special, they don't consume operands.  | 
55  | 1.05M  |   if (C->Kind == AliasPatternCond_K_Feature)  | 
56  | 15.6k  |     return testFeatureBits(MI, C->Value);  | 
57  | 1.04M  |   if (C->Kind == AliasPatternCond_K_NegFeature)  | 
58  | 9.15k  |     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  | 1.03M  |   if (C->Kind == AliasPatternCond_K_OrFeature) { | 
63  | 92.8k  |     *OrPredicateResult |= testFeatureBits(MI, C->Value);  | 
64  | 92.8k  |     return true;  | 
65  | 92.8k  |   }  | 
66  | 940k  |   if (C->Kind == AliasPatternCond_K_OrNegFeature) { | 
67  | 521  |     *OrPredicateResult |= !(testFeatureBits(MI, C->Value));  | 
68  | 521  |     return true;  | 
69  | 521  |   }  | 
70  | 940k  |   if (C->Kind == AliasPatternCond_K_EndOrFeatures) { | 
71  | 38.3k  |     bool Res = *OrPredicateResult;  | 
72  | 38.3k  |     *OrPredicateResult = false;  | 
73  | 38.3k  |     return Res;  | 
74  | 38.3k  |   }  | 
75  |  |  | 
76  |  |   // Get and consume an operand.  | 
77  | 901k  |   MCOperand *Opnd = MCInst_getOperand(MI, *OpIdx);  | 
78  | 901k  |   ++(*OpIdx);  | 
79  |  |  | 
80  |  |   // Check the specific condition for the operand.  | 
81  | 901k  |   switch (C->Kind) { | 
82  | 0  |   default:  | 
83  | 0  |     assert(0 && "invalid kind");  | 
84  | 276k  |   case AliasPatternCond_K_Imm:  | 
85  |  |     // Operand must be a specific immediate.  | 
86  | 276k  |     return MCOperand_isImm(Opnd) &&  | 
87  | 276k  |            MCOperand_getImm(Opnd) == (int32_t)C->Value;  | 
88  | 63.6k  |   case AliasPatternCond_K_Reg:  | 
89  |  |     // Operand must be a specific register.  | 
90  | 63.6k  |     return MCOperand_isReg(Opnd) &&  | 
91  | 63.6k  |            MCOperand_getReg(Opnd) == C->Value;  | 
92  | 6.18k  |   case AliasPatternCond_K_TiedReg:  | 
93  |  |     // Operand must match the register of another operand.  | 
94  | 6.18k  |     return MCOperand_isReg(Opnd) &&  | 
95  | 6.18k  |            MCOperand_getReg(Opnd) ==  | 
96  | 6.18k  |              MCOperand_getReg(  | 
97  | 6.18k  |                MCInst_getOperand(MI, C->Value));  | 
98  | 397k  |   case AliasPatternCond_K_RegClass:  | 
99  |  |     // Operand must be a register in this class. Value is a register class  | 
100  |  |     // id.  | 
101  | 397k  |     return MCOperand_isReg(Opnd) &&  | 
102  | 397k  |            MCRegisterClass_contains(  | 
103  | 397k  |              MCRegisterInfo_getRegClass(MRI, C->Value),  | 
104  | 397k  |              MCOperand_getReg(Opnd));  | 
105  | 34.8k  |   case AliasPatternCond_K_Custom:  | 
106  |  |     // Operand must match some custom criteria.  | 
107  | 34.8k  |     assert(M->ValidateMCOperand &&  | 
108  | 34.8k  |            "A custom validator should be set but isn't.");  | 
109  | 34.8k  |     return M->ValidateMCOperand(Opnd, C->Value);  | 
110  | 122k  |   case AliasPatternCond_K_Ignore:  | 
111  |  |     // Operand can be anything.  | 
112  | 122k  |     return true;  | 
113  | 0  |   case AliasPatternCond_K_Feature:  | 
114  | 0  |   case AliasPatternCond_K_NegFeature:  | 
115  | 0  |   case AliasPatternCond_K_OrFeature:  | 
116  | 0  |   case AliasPatternCond_K_OrNegFeature:  | 
117  | 0  |   case AliasPatternCond_K_EndOrFeatures:  | 
118  | 0  |     assert(0 && "handled earlier");  | 
119  | 901k  |   }  | 
120  | 0  |   return false;  | 
121  | 901k  | }  | 
122  |  |  | 
123  |  | /// Check if PatternsForOpcode is all zero.  | 
124  |  | static inline bool validOpToPatter(const PatternsForOpcode *P)  | 
125  | 223M  | { | 
126  | 223M  |   return !(P->Opcode == 0 && P->PatternStart == 0 && P->NumPatterns == 0);  | 
127  | 223M  | }  | 
128  |  |  | 
129  |  | const char *matchAliasPatterns(MCInst *MI, const AliasMatchingData *M)  | 
130  | 1.61M  | { | 
131  |  |   // TODO Rewrite to C  | 
132  |  |  | 
133  |  |   // auto It = lower_bound(M.OpToPatterns, MI->getOpcode(),  | 
134  |  |   //                       [](const PatternsForOpcode &L, unsigned Opcode) { | 
135  |  |   //                         return L.Opcode < Opcode;  | 
136  |  |   //                       });  | 
137  |  |   // if (It == M.OpToPatterns.end() || It->Opcode != MI->getOpcode())  | 
138  |  |   //   return nullptr;  | 
139  |  |  | 
140  |  |   // Binary search by opcode. Return false if there are no aliases for this  | 
141  |  |   // opcode.  | 
142  | 1.61M  |   unsigned MIOpcode = MI->Opcode;  | 
143  | 1.61M  |   size_t i = 0;  | 
144  | 1.61M  |   uint32_t PatternOpcode = M->OpToPatterns[i].Opcode;  | 
145  | 224M  |   while (PatternOpcode < MIOpcode && validOpToPatter(&M->OpToPatterns[i]))  | 
146  | 223M  |     PatternOpcode = M->OpToPatterns[++i].Opcode;  | 
147  | 1.61M  |   if (PatternOpcode != MI->Opcode ||  | 
148  | 170k  |       !validOpToPatter(&M->OpToPatterns[i]))  | 
149  | 1.43M  |     return NULL;  | 
150  |  |  | 
151  |  |   // // Try all patterns for this opcode.  | 
152  | 170k  |   uint32_t AsmStrOffset = ~0U;  | 
153  | 170k  |   const AliasPattern *Patterns =  | 
154  | 170k  |     M->Patterns + M->OpToPatterns[i].PatternStart;  | 
155  | 170k  |   for (const AliasPattern *P = Patterns;  | 
156  | 497k  |        P != Patterns + M->OpToPatterns[i].NumPatterns; ++P) { | 
157  |  |     // Check operand count first.  | 
158  | 391k  |     if (MCInst_getNumOperands(MI) != P->NumOperands)  | 
159  | 0  |       return NULL;  | 
160  |  |  | 
161  |  |     // Test all conditions for this pattern.  | 
162  | 391k  |     const AliasPatternCond *Conds =  | 
163  | 391k  |       M->PatternConds + P->AliasCondStart;  | 
164  | 391k  |     unsigned OpIdx = 0;  | 
165  | 391k  |     bool OrPredicateResult = false;  | 
166  | 391k  |     bool allMatch = true;  | 
167  | 391k  |     for (const AliasPatternCond *C = Conds;  | 
168  | 1.12M  |          C != Conds + P->NumConds; ++C) { | 
169  | 1.05M  |       if (!matchAliasCondition(MI, MI->MRI, &OpIdx, M, C,  | 
170  | 1.05M  |              &OrPredicateResult)) { | 
171  | 327k  |         allMatch = false;  | 
172  | 327k  |         break;  | 
173  | 327k  |       }  | 
174  | 1.05M  |     }  | 
175  | 391k  |     if (allMatch) { | 
176  | 64.1k  |       AsmStrOffset = P->AsmStrOffset;  | 
177  | 64.1k  |       break;  | 
178  | 64.1k  |     }  | 
179  | 391k  |   }  | 
180  |  |   // If no alias matched, don't print an alias.  | 
181  | 170k  |   if (AsmStrOffset == ~0U)  | 
182  | 106k  |     return NULL;  | 
183  |  |  | 
184  |  |   // Go to offset AsmStrOffset and use the null terminated string there. The  | 
185  |  |   // offset should point to the beginning of an alias string, so it should  | 
186  |  |   // either be zero or be preceded by a null byte.  | 
187  | 64.1k  |   return M->AsmStrings + AsmStrOffset;  | 
188  | 170k  | }  | 
189  |  |  | 
190  |  | // TODO Add functionality to toggle the flag.  | 
191  |  | bool getUseMarkup(void)  | 
192  | 8.65M  | { | 
193  | 8.65M  |   return false;  | 
194  | 8.65M  | }  | 
195  |  |  | 
196  |  | /// Utility functions to make adding mark ups simpler.  | 
197  |  | const char *markup(const char *s)  | 
198  | 8.51M  | { | 
199  | 8.51M  |   static const char *no_markup = "";  | 
200  | 8.51M  |   if (getUseMarkup())  | 
201  | 0  |     return s;  | 
202  | 8.51M  |   else  | 
203  | 8.51M  |     return no_markup;  | 
204  | 8.51M  | }  | 
205  |  |  | 
206  |  | // binary search for encoding in IndexType array  | 
207  |  | // return -1 if not found, or index if found  | 
208  |  | unsigned int binsearch_IndexTypeEncoding(const struct IndexType *index,  | 
209  |  |            size_t size, uint16_t encoding)  | 
210  | 143k  | { | 
211  |  |   // binary searching since the index is sorted in encoding order  | 
212  | 143k  |   size_t left, right, m;  | 
213  |  |  | 
214  | 143k  |   right = size - 1;  | 
215  |  |  | 
216  | 143k  |   if (encoding < index[0].encoding || encoding > index[right].encoding)  | 
217  |  |     // not found  | 
218  | 28.0k  |     return -1;  | 
219  |  |  | 
220  | 115k  |   left = 0;  | 
221  |  |  | 
222  | 551k  |   while (left <= right) { | 
223  | 518k  |     m = (left + right) / 2;  | 
224  | 518k  |     if (encoding == index[m].encoding) { | 
225  |  |       // LLVM actually uses lower_bound for the index table search  | 
226  |  |       // Here we need to check if a previous entry is of the same encoding  | 
227  |  |       // and return the first one.  | 
228  | 81.7k  |       while (m > 0 && encoding == index[m - 1].encoding)  | 
229  | 0  |         --m;  | 
230  | 81.7k  |       return m;  | 
231  | 81.7k  |     }  | 
232  |  |  | 
233  | 436k  |     if (encoding < index[m].encoding)  | 
234  | 169k  |       right = m - 1;  | 
235  | 267k  |     else  | 
236  | 267k  |       left = m + 1;  | 
237  | 436k  |   }  | 
238  |  |  | 
239  |  |   // not found  | 
240  | 33.6k  |   return -1;  | 
241  | 115k  | }  | 
242  |  |  | 
243  |  | // binary search for encoding in IndexTypeStr array  | 
244  |  | // return -1 if not found, or index if found  | 
245  |  | unsigned int binsearch_IndexTypeStrEncoding(const struct IndexTypeStr *index,  | 
246  |  |               size_t size, const char *name)  | 
247  | 3.35k  | { | 
248  |  |   // binary searching since the index is sorted in encoding order  | 
249  | 3.35k  |   size_t left, right, m;  | 
250  |  |  | 
251  | 3.35k  |   right = size - 1;  | 
252  |  |  | 
253  | 3.35k  |   int str_left_cmp = strcmp(name, index[0].name);  | 
254  | 3.35k  |   int str_right_cmp = strcmp(name, index[right].name);  | 
255  | 3.35k  |   if (str_left_cmp < 0 || str_right_cmp > 0)  | 
256  |  |     // not found  | 
257  | 0  |     return -1;  | 
258  |  |  | 
259  | 3.35k  |   left = 0;  | 
260  |  |  | 
261  | 34.6k  |   while (left <= right) { | 
262  | 34.6k  |     m = (left + right) / 2;  | 
263  | 34.6k  |     if (strcmp(name, index[m].name) == 0) { | 
264  |  |       // LLVM actually uses lower_bound for the index table search  | 
265  |  |       // Here we need to check if a previous entry is of the same encoding  | 
266  |  |       // and return the first one.  | 
267  | 3.29k  |       while (m > 0 && (strcmp(name, index[m - 1].name) == 0))  | 
268  | 0  |         --m;  | 
269  | 3.29k  |       return m;  | 
270  | 3.29k  |     }  | 
271  |  |  | 
272  | 31.3k  |     if (strcmp(name, index[m].name) < 0)  | 
273  | 12.8k  |       right = m - 1;  | 
274  | 18.4k  |     else  | 
275  | 18.4k  |       left = m + 1;  | 
276  | 31.3k  |   }  | 
277  |  |  | 
278  |  |   // not found  | 
279  | 64  |   return -1;  | 
280  | 3.35k  | }  |