/src/capstonenext/arch/Sparc/SparcInstPrinter.c
Line  | Count  | Source  | 
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  |  | //===-- SparcInstPrinter.cpp - Convert Sparc 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 Sparc 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 "../../MCInstPrinter.h"  | 
33  |  | #include "../../Mapping.h"  | 
34  |  | #include "SparcInstPrinter.h"  | 
35  |  | #include "SparcLinkage.h"  | 
36  |  | #include "SparcMCTargetDesc.h"  | 
37  |  | #include "SparcMapping.h"  | 
38  |  | #include "SparcDisassemblerExtension.h"  | 
39  |  |  | 
40  |  | #define CONCAT(a, b) CONCAT_(a, b)  | 
41  |  | #define CONCAT_(a, b) a##_##b  | 
42  |  |  | 
43  |  | #define DEBUG_TYPE "asm-printer"  | 
44  |  |  | 
45  |  | static void printCustomAliasOperand(MCInst *MI, uint64_t Address,  | 
46  |  |             unsigned OpIdx, unsigned PrintMethodIdx,  | 
47  |  |             SStream *OS);  | 
48  |  | static void printOperand(MCInst *MI, int opNum, SStream *O);  | 
49  |  |  | 
50  |  | #define GET_INSTRUCTION_NAME  | 
51  |  | #define PRINT_ALIAS_INSTR  | 
52  |  | #include "SparcGenAsmWriter.inc"  | 
53  |  |  | 
54  |  | static void printRegName(SStream *OS, MCRegister Reg)  | 
55  | 7.76k  | { | 
56  | 7.76k  |   SStream_concat1(OS, '%');  | 
57  | 7.76k  |   SStream_concat0(OS, getRegisterName(Reg, Sparc_NoRegAltName));  | 
58  | 7.76k  | }  | 
59  |  |  | 
60  |  | static void printRegNameAlt(SStream *OS, MCRegister Reg, unsigned AltIdx)  | 
61  | 52.2k  | { | 
62  | 52.2k  |   SStream_concat1(OS, '%');  | 
63  | 52.2k  |   SStream_concat0(OS, getRegisterName(Reg, AltIdx));  | 
64  | 52.2k  | }  | 
65  |  |  | 
66  |  | static void printInst(MCInst *MI, uint64_t Address, SStream *O)  | 
67  | 43.5k  | { | 
68  | 43.5k  |   bool isAlias = false;  | 
69  | 43.5k  |   bool useAliasDetails = map_use_alias_details(MI);  | 
70  | 43.5k  |   map_set_fill_detail_ops(MI, useAliasDetails);  | 
71  |  |  | 
72  | 43.5k  |   if (!printAliasInstr(MI, Address, O) && !printSparcAliasInstr(MI, O)) { | 
73  | 37.6k  |     MCInst_setIsAlias(MI, false);  | 
74  | 37.6k  |   } else { | 
75  | 5.88k  |     isAlias = true;  | 
76  | 5.88k  |     MCInst_setIsAlias(MI, isAlias);  | 
77  | 5.88k  |     if (useAliasDetails) { | 
78  | 5.88k  |       return;  | 
79  | 5.88k  |     }  | 
80  | 5.88k  |   }  | 
81  |  |  | 
82  | 37.6k  |   if (!isAlias || !useAliasDetails) { | 
83  | 37.6k  |     map_set_fill_detail_ops(MI, !(isAlias && useAliasDetails));  | 
84  | 37.6k  |     if (isAlias)  | 
85  | 0  |       SStream_Close(O);  | 
86  | 37.6k  |     printInstruction(MI, Address, O);  | 
87  | 37.6k  |     if (isAlias)  | 
88  | 0  |       SStream_Open(O);  | 
89  | 37.6k  |   }  | 
90  | 37.6k  | }  | 
91  |  |  | 
92  |  | bool printSparcAliasInstr(MCInst *MI, SStream *O)  | 
93  | 39.8k  | { | 
94  | 39.8k  |   switch (MCInst_getOpcode(MI)) { | 
95  | 37.0k  |   default:  | 
96  | 37.0k  |     return false;  | 
97  | 76  |   case Sparc_JMPLrr:  | 
98  | 2.22k  |   case Sparc_JMPLri: { | 
99  | 2.22k  |     if (MCInst_getNumOperands(MI) != 3)  | 
100  | 0  |       return false;  | 
101  | 2.22k  |     if (!MCOperand_isReg(MCInst_getOperand(MI, (0))))  | 
102  | 0  |       return false;  | 
103  | 2.22k  |     switch (MCOperand_getReg(MCInst_getOperand(MI, (0)))) { | 
104  | 89  |     default:  | 
105  | 89  |       return false;  | 
106  | 2.05k  |     case Sparc_G0: // jmp $addr | ret | retl  | 
107  | 2.05k  |       if (MCOperand_isImm(MCInst_getOperand(MI, (2))) &&  | 
108  | 1.98k  |           MCOperand_getImm(MCInst_getOperand(MI, (2))) == 8) { | 
109  | 1.65k  |         switch (MCOperand_getReg(  | 
110  | 1.65k  |           MCInst_getOperand(MI, (1)))) { | 
111  | 641  |         default:  | 
112  | 641  |           break;  | 
113  | 641  |         case Sparc_I7:  | 
114  | 28  |           SStream_concat0(O, "\tret");  | 
115  | 28  |           return true;  | 
116  | 983  |         case Sparc_O7:  | 
117  | 983  |           SStream_concat0(O, "\tretl");  | 
118  | 983  |           return true;  | 
119  | 1.65k  |         }  | 
120  | 1.65k  |       }  | 
121  | 1.03k  |       SStream_concat0(O, "\tjmp ");  | 
122  | 1.03k  |       printMemOperand(MI, 1, O);  | 
123  | 1.03k  |       return true;  | 
124  | 87  |     case Sparc_O7: // call $addr  | 
125  | 87  |       SStream_concat0(O, "\tcall ");  | 
126  | 87  |       printMemOperand(MI, 1, O);  | 
127  | 87  |       return true;  | 
128  | 2.22k  |     }  | 
129  | 2.22k  |   }  | 
130  | 158  |   case Sparc_V9FCMPS:  | 
131  | 218  |   case Sparc_V9FCMPD:  | 
132  | 284  |   case Sparc_V9FCMPQ:  | 
133  | 418  |   case Sparc_V9FCMPES:  | 
134  | 486  |   case Sparc_V9FCMPED:  | 
135  | 536  |   case Sparc_V9FCMPEQ: { | 
136  | 536  |     if (Sparc_getFeatureBits(MI->csh->mode, Sparc_FeatureV9) ||  | 
137  | 191  |         (MCInst_getNumOperands(MI) != 3) ||  | 
138  | 191  |         (!MCOperand_isReg(MCInst_getOperand(MI, (0)))) ||  | 
139  | 191  |         (MCOperand_getReg(MCInst_getOperand(MI, (0))) !=  | 
140  | 191  |          Sparc_FCC0))  | 
141  | 536  |       return false;  | 
142  |  |     // if V8, skip printing %fcc0.  | 
143  | 0  |     switch (MCInst_getOpcode(MI)) { | 
144  | 0  |     default:  | 
145  | 0  |     case Sparc_V9FCMPS:  | 
146  | 0  |       SStream_concat0(O, "\tfcmps ");  | 
147  | 0  |       break;  | 
148  | 0  |     case Sparc_V9FCMPD:  | 
149  | 0  |       SStream_concat0(O, "\tfcmpd ");  | 
150  | 0  |       break;  | 
151  | 0  |     case Sparc_V9FCMPQ:  | 
152  | 0  |       SStream_concat0(O, "\tfcmpq ");  | 
153  | 0  |       break;  | 
154  | 0  |     case Sparc_V9FCMPES:  | 
155  | 0  |       SStream_concat0(O, "\tfcmpes ");  | 
156  | 0  |       break;  | 
157  | 0  |     case Sparc_V9FCMPED:  | 
158  | 0  |       SStream_concat0(O, "\tfcmped ");  | 
159  | 0  |       break;  | 
160  | 0  |     case Sparc_V9FCMPEQ:  | 
161  | 0  |       SStream_concat0(O, "\tfcmpeq ");  | 
162  | 0  |       break;  | 
163  | 0  |     }  | 
164  | 0  |     printOperand(MI, 1, O);  | 
165  | 0  |     SStream_concat0(O, ", ");  | 
166  | 0  |     printOperand(MI, 2, O);  | 
167  | 0  |     return true;  | 
168  | 0  |   }  | 
169  | 39.8k  |   }  | 
170  | 39.8k  | }  | 
171  |  |  | 
172  |  | static void printOperand(MCInst *MI, int opNum, SStream *O)  | 
173  | 86.5k  | { | 
174  | 86.5k  |   Sparc_add_cs_detail_0(MI, Sparc_OP_GROUP_Operand, opNum);  | 
175  | 86.5k  |   MCOperand *MO = MCInst_getOperand(MI, (opNum));  | 
176  |  |  | 
177  | 86.5k  |   if (MCOperand_isReg(MO)) { | 
178  | 60.0k  |     unsigned Reg = MCOperand_getReg(MO);  | 
179  | 60.0k  |     if (Sparc_getFeatureBits(MI->csh->mode, Sparc_FeatureV9))  | 
180  | 52.2k  |       printRegNameAlt(O, Reg, Sparc_RegNamesStateReg);  | 
181  | 7.76k  |     else  | 
182  | 7.76k  |       printRegName(O, Reg);  | 
183  | 60.0k  |     return;  | 
184  | 60.0k  |   }  | 
185  |  |  | 
186  | 26.4k  |   if (MCOperand_isImm(MO)) { | 
187  | 26.4k  |     switch (MCInst_getOpcode(MI)) { | 
188  | 26.2k  |     default:  | 
189  | 26.2k  |       printInt32(O, (int)MCOperand_getImm(MO));  | 
190  | 26.2k  |       return;  | 
191  |  |  | 
192  | 113  |     case Sparc_TICCri: // Fall through  | 
193  | 113  |     case Sparc_TICCrr: // Fall through  | 
194  | 180  |     case Sparc_TRAPri: // Fall through  | 
195  | 180  |     case Sparc_TRAPrr: // Fall through  | 
196  | 250  |     case Sparc_TXCCri: // Fall through  | 
197  | 250  |     case Sparc_TXCCrr: // Fall through  | 
198  |  |       // Only seven-bit values up to 127.  | 
199  | 250  |       printInt8(O, ((int)MCOperand_getImm(MO) & 0x7f));  | 
200  | 250  |       return;  | 
201  | 26.4k  |     }  | 
202  | 26.4k  |   }  | 
203  |  |  | 
204  | 0  |   CS_ASSERT(MCOperand_isExpr(MO) &&  | 
205  | 0  |       "Unknown operand kind in printOperand");  | 
206  | 0  | }  | 
207  |  |  | 
208  |  | void printMemOperand(MCInst *MI, int opNum, SStream *O)  | 
209  | 12.9k  | { | 
210  | 12.9k  |   Sparc_add_cs_detail_0(MI, Sparc_OP_GROUP_MemOperand, opNum);  | 
211  | 12.9k  |   MCOperand *Op1 = MCInst_getOperand(MI, (opNum));  | 
212  | 12.9k  |   MCOperand *Op2 = MCInst_getOperand(MI, (opNum + 1));  | 
213  |  |  | 
214  | 12.9k  |   bool PrintedFirstOperand = false;  | 
215  | 12.9k  |   if (MCOperand_isReg(Op1) && MCOperand_getReg(Op1) != Sparc_G0) { | 
216  | 12.0k  |     printOperand(MI, opNum, O);  | 
217  | 12.0k  |     PrintedFirstOperand = true;  | 
218  | 12.0k  |   }  | 
219  |  |  | 
220  |  |   // Skip the second operand iff it adds nothing (literal 0 or %g0) and we've  | 
221  |  |   // already printed the first one  | 
222  | 12.9k  |   const bool SkipSecondOperand =  | 
223  | 12.9k  |     PrintedFirstOperand &&  | 
224  | 12.0k  |     ((MCOperand_isReg(Op2) && MCOperand_getReg(Op2) == Sparc_G0) ||  | 
225  | 10.6k  |      (MCOperand_isImm(Op2) && MCOperand_getImm(Op2) == 0));  | 
226  |  |  | 
227  | 12.9k  |   if (!SkipSecondOperand) { | 
228  | 11.3k  |     if (PrintedFirstOperand)  | 
229  | 10.4k  |       SStream_concat0(O, "+");  | 
230  |  |  | 
231  | 11.3k  |     printOperand(MI, opNum + 1, O);  | 
232  | 11.3k  |   }  | 
233  | 12.9k  | }  | 
234  |  |  | 
235  |  | void printCCOperand(MCInst *MI, int opNum, SStream *O)  | 
236  | 8.11k  | { | 
237  | 8.11k  |   Sparc_add_cs_detail_0(MI, Sparc_OP_GROUP_CCOperand, opNum);  | 
238  | 8.11k  |   int CC = (int)MCOperand_getImm(MCInst_getOperand(MI, (opNum)));  | 
239  | 8.11k  |   switch (MCInst_getOpcode(MI)) { | 
240  | 1.62k  |   default:  | 
241  | 1.62k  |     break;  | 
242  | 1.62k  |   case Sparc_FBCOND:  | 
243  | 987  |   case Sparc_FBCONDA:  | 
244  | 1.18k  |   case Sparc_FBCOND_V9:  | 
245  | 1.39k  |   case Sparc_FBCONDA_V9:  | 
246  | 1.63k  |   case Sparc_BPFCC:  | 
247  | 1.98k  |   case Sparc_BPFCCA:  | 
248  | 1.98k  |   case Sparc_BPFCCNT:  | 
249  | 1.98k  |   case Sparc_BPFCCANT:  | 
250  | 2.45k  |   case Sparc_MOVFCCrr:  | 
251  | 2.45k  |   case Sparc_V9MOVFCCrr:  | 
252  | 2.90k  |   case Sparc_MOVFCCri:  | 
253  | 2.90k  |   case Sparc_V9MOVFCCri:  | 
254  | 2.93k  |   case Sparc_FMOVS_FCC:  | 
255  | 2.93k  |   case Sparc_V9FMOVS_FCC:  | 
256  | 3.38k  |   case Sparc_FMOVD_FCC:  | 
257  | 3.38k  |   case Sparc_V9FMOVD_FCC:  | 
258  | 3.94k  |   case Sparc_FMOVQ_FCC:  | 
259  | 3.94k  |   case Sparc_V9FMOVQ_FCC:  | 
260  |  |     // Make sure CC is a fp conditional flag.  | 
261  | 3.94k  |     CC = (CC < SPARC_CC_FCC_BEGIN) ? (CC + SPARC_CC_FCC_BEGIN) : CC;  | 
262  | 3.94k  |     break;  | 
263  | 869  |   case Sparc_CBCOND:  | 
264  | 1.19k  |   case Sparc_CBCONDA:  | 
265  |  |     // Make sure CC is a cp conditional flag.  | 
266  | 1.19k  |     CC = (CC < SPARC_CC_CPCC_BEGIN) ? (CC + SPARC_CC_CPCC_BEGIN) :  | 
267  | 1.19k  |               CC;  | 
268  | 1.19k  |     break;  | 
269  | 333  |   case Sparc_BPR:  | 
270  | 562  |   case Sparc_BPRA:  | 
271  | 853  |   case Sparc_BPRNT:  | 
272  | 1.06k  |   case Sparc_BPRANT:  | 
273  | 1.14k  |   case Sparc_MOVRri:  | 
274  | 1.20k  |   case Sparc_MOVRrr:  | 
275  | 1.27k  |   case Sparc_FMOVRS:  | 
276  | 1.32k  |   case Sparc_FMOVRD:  | 
277  | 1.35k  |   case Sparc_FMOVRQ:  | 
278  |  |     // Make sure CC is a register conditional flag.  | 
279  | 1.35k  |     CC = (CC < SPARC_CC_REG_BEGIN) ? (CC + SPARC_CC_REG_BEGIN) : CC;  | 
280  | 1.35k  |     break;  | 
281  | 8.11k  |   }  | 
282  | 8.11k  |   SStream_concat0(O, SPARCCondCodeToString((sparc_cc)CC));  | 
283  | 8.11k  | }  | 
284  |  |  | 
285  |  | bool printGetPCX(MCInst *MI, unsigned opNum, SStream *O)  | 
286  | 0  | { | 
287  | 0  |   printf("FIXME: Implement SparcInstPrinter::printGetPCX."); | 
288  | 0  |   return true;  | 
289  | 0  | }  | 
290  |  |  | 
291  |  | void printMembarTag(MCInst *MI, int opNum, SStream *O)  | 
292  | 633  | { | 
293  | 633  |   Sparc_add_cs_detail_0(MI, Sparc_OP_GROUP_MembarTag, opNum);  | 
294  | 633  |   static const char *const TagNames[] = { "#LoadLoad",  "#StoreLoad", | 
295  | 633  |             "#LoadStore", "#StoreStore",  | 
296  | 633  |             "#Lookaside", "#MemIssue",  | 
297  | 633  |             "#Sync" };  | 
298  |  |  | 
299  | 633  |   unsigned Imm = MCOperand_getImm(MCInst_getOperand(MI, (opNum)));  | 
300  |  |  | 
301  | 633  |   if (Imm > 127) { | 
302  | 77  |     printUInt32(O, Imm);  | 
303  | 77  |     return;  | 
304  | 77  |   }  | 
305  |  |  | 
306  | 633  |   bool First = true;  | 
307  | 31.6k  |   for (unsigned i = 0; i < sizeof(TagNames); i++) { | 
308  | 31.1k  |     if (Imm & (1ull << i)) { | 
309  | 2.20k  |       SStream_concat(O, "%s", (First ? "" : " | "));  | 
310  | 2.20k  |       SStream_concat0(O, TagNames[i]);  | 
311  | 2.20k  |       First = false;  | 
312  | 2.20k  |     }  | 
313  | 31.1k  |   }  | 
314  | 556  | }  | 
315  |  |  | 
316  |  | #define GET_ASITAG_IMPL  | 
317  |  | #include "SparcGenSystemOperands.inc"  | 
318  |  |  | 
319  |  | void printASITag(MCInst *MI, int opNum, SStream *O)  | 
320  | 4.30k  | { | 
321  | 4.30k  |   Sparc_add_cs_detail_0(MI, Sparc_OP_GROUP_ASITag, opNum);  | 
322  | 4.30k  |   unsigned Imm = MCOperand_getImm(MCInst_getOperand(MI, (opNum)));  | 
323  | 4.30k  |   const Sparc_ASITag_ASITag *ASITag =  | 
324  | 4.30k  |     Sparc_ASITag_lookupASITagByEncoding(Imm);  | 
325  | 4.30k  |   if (Sparc_getFeatureBits(MI->csh->mode, Sparc_FeatureV9) && ASITag) { | 
326  | 296  |     SStream_concat1(O, '#');  | 
327  | 296  |     SStream_concat0(O, ASITag->Name);  | 
328  | 296  |   } else  | 
329  | 4.00k  |     printUInt32(O, Imm);  | 
330  | 4.30k  | }  | 
331  |  |  | 
332  |  | void Sparc_LLVM_printInst(MCInst *MI, uint64_t Address, const char *Annot,  | 
333  |  |         SStream *O)  | 
334  | 43.5k  | { | 
335  | 43.5k  |   printInst(MI, Address, O);  | 
336  | 43.5k  | }  | 
337  |  |  | 
338  |  | const char *Sparc_LLVM_getRegisterName(unsigned RegNo, unsigned AltIdx)  | 
339  | 19.5k  | { | 
340  | 19.5k  |   return getRegisterName(RegNo, AltIdx);  | 
341  | 19.5k  | }  |