/src/capstonenext/arch/TMS320C64x/TMS320C64xInstPrinter.c
Line | Count | Source |
1 | | /* Capstone Disassembly Engine */ |
2 | | /* TMS320C64x Backend by Fotis Loukos <me@fotisl.com> 2016 */ |
3 | | |
4 | | #ifdef CAPSTONE_HAS_TMS320C64X |
5 | | |
6 | | #include <ctype.h> |
7 | | #include <string.h> |
8 | | |
9 | | #include "TMS320C64xInstPrinter.h" |
10 | | #include "../../MCInst.h" |
11 | | #include "../../Mapping.h" |
12 | | #include "../../utils.h" |
13 | | #include "../../SStream.h" |
14 | | #include "../../MCRegisterInfo.h" |
15 | | #include "../../MathExtras.h" |
16 | | #include "TMS320C64xMapping.h" |
17 | | |
18 | | #include "capstone/tms320c64x.h" |
19 | | |
20 | | static const char *getRegisterName(unsigned RegNo); |
21 | | static void printOperand(MCInst *MI, unsigned OpNo, SStream *O); |
22 | | static void printMemOperand(MCInst *MI, unsigned OpNo, SStream *O); |
23 | | static void printMemOperand2(MCInst *MI, unsigned OpNo, SStream *O); |
24 | | static void printRegPair(MCInst *MI, unsigned OpNo, SStream *O); |
25 | | |
26 | | void TMS320C64x_post_printer(csh ud, cs_insn *insn, SStream *insn_asm, |
27 | | MCInst *mci) |
28 | 25.1k | { |
29 | 25.1k | #ifndef CAPSTONE_DIET |
30 | 25.1k | SStream ss; |
31 | 25.1k | const char *op_str_ptr, *p2; |
32 | 25.1k | char tmp[8] = { 0 }; |
33 | 25.1k | unsigned int unit = 0; |
34 | 25.1k | const insn_map *map = lookup_insn_map(mci->csh, MCInst_getOpcode(mci)); |
35 | | |
36 | 25.1k | for (size_t i = 0; map && i < ARR_SIZE(map->groups) && map->groups[i]; |
37 | 25.1k | i++) { |
38 | 25.1k | switch (map->groups[i]) { |
39 | 7.83k | case TMS320C64X_GRP_FUNIT_D: |
40 | 7.83k | unit = TMS320C64X_FUNIT_D; |
41 | 7.83k | break; |
42 | 6.26k | case TMS320C64X_GRP_FUNIT_L: |
43 | 6.26k | unit = TMS320C64X_FUNIT_L; |
44 | 6.26k | break; |
45 | 1.12k | case TMS320C64X_GRP_FUNIT_M: |
46 | 1.12k | unit = TMS320C64X_FUNIT_M; |
47 | 1.12k | break; |
48 | 9.15k | case TMS320C64X_GRP_FUNIT_S: |
49 | 9.15k | unit = TMS320C64X_FUNIT_S; |
50 | 9.15k | break; |
51 | 789 | case TMS320C64X_GRP_FUNIT_NO: |
52 | 789 | unit = TMS320C64X_FUNIT_NO; |
53 | 789 | break; |
54 | 25.1k | } |
55 | 25.1k | if (unit != 0) |
56 | 25.1k | break; |
57 | 25.1k | } |
58 | 25.1k | if (insn->detail) |
59 | 25.1k | insn->detail->tms320c64x.funit.unit = unit; |
60 | | |
61 | 25.1k | SStream_Init(&ss); |
62 | 25.1k | if (mci->tms320c64x.condition_reg != TMS320C64X_REG_INVALID) |
63 | 13.9k | SStream_concat(&ss, "[%c%s]|", |
64 | 13.9k | mci->tms320c64x.condition_zero ? '!' : '|', |
65 | 13.9k | cs_reg_name(ud, mci->tms320c64x.condition_reg)); |
66 | | |
67 | | // Sorry for all the fixes below. I don't have time to add more helper SStream functions. |
68 | | // Before that they messed around with the private buffer of the stream. |
69 | | // So it is better now. But still not efficient. |
70 | 25.1k | op_str_ptr = strchr(SStream_rbuf(insn_asm), '\t'); |
71 | | |
72 | 25.1k | if ((op_str_ptr != NULL) && |
73 | 24.6k | (((p2 = strchr(op_str_ptr, '[')) != NULL) || |
74 | 18.8k | ((p2 = strchr(op_str_ptr, '(')) != NULL))) { |
75 | 26.0k | while ((p2 > op_str_ptr) && ((*p2 != 'a') && (*p2 != 'b'))) |
76 | 19.8k | p2--; |
77 | 6.13k | if (p2 == op_str_ptr) { |
78 | 0 | SStream_Flush(insn_asm, NULL); |
79 | 0 | SStream_concat0(insn_asm, "Invalid!"); |
80 | 0 | return; |
81 | 0 | } |
82 | 6.13k | if (*p2 == 'a') |
83 | 3.70k | strncpy(tmp, "1T", sizeof(tmp)); |
84 | 2.42k | else |
85 | 2.42k | strncpy(tmp, "2T", sizeof(tmp)); |
86 | 19.0k | } else { |
87 | 19.0k | tmp[0] = '\0'; |
88 | 19.0k | } |
89 | 25.1k | SStream mnem_post = { 0 }; |
90 | 25.1k | SStream_Init(&mnem_post); |
91 | 25.1k | switch (unit) { |
92 | 7.83k | case TMS320C64X_FUNIT_D: |
93 | 7.83k | SStream_concat(&mnem_post, ".D%s%u", tmp, mci->tms320c64x.side); |
94 | 7.83k | break; |
95 | 6.26k | case TMS320C64X_FUNIT_L: |
96 | 6.26k | SStream_concat(&mnem_post, ".L%s%u", tmp, mci->tms320c64x.side); |
97 | 6.26k | break; |
98 | 1.12k | case TMS320C64X_FUNIT_M: |
99 | 1.12k | SStream_concat(&mnem_post, ".M%s%u", tmp, mci->tms320c64x.side); |
100 | 1.12k | break; |
101 | 9.15k | case TMS320C64X_FUNIT_S: |
102 | 9.15k | SStream_concat(&mnem_post, ".S%s%u", tmp, mci->tms320c64x.side); |
103 | 9.15k | break; |
104 | 25.1k | } |
105 | 25.1k | if (mci->tms320c64x.crosspath > 0) |
106 | 7.22k | SStream_concat0(&mnem_post, "X"); |
107 | | |
108 | 25.1k | if (op_str_ptr != NULL) { |
109 | | // There is an op_str |
110 | 24.6k | SStream_concat1(&mnem_post, '\t'); |
111 | 24.6k | SStream_replc_str(insn_asm, '\t', SStream_rbuf(&mnem_post)); |
112 | 24.6k | } |
113 | | |
114 | 25.1k | if (mci->tms320c64x.parallel) |
115 | 11.9k | SStream_concat0(insn_asm, "\t||"); |
116 | 25.1k | SStream_concat0(&ss, SStream_rbuf(insn_asm)); |
117 | 25.1k | SStream_Flush(insn_asm, NULL); |
118 | 25.1k | SStream_concat0(insn_asm, SStream_rbuf(&ss)); |
119 | 25.1k | #endif |
120 | 25.1k | } |
121 | | |
122 | | #define PRINT_ALIAS_INSTR |
123 | | #include "TMS320C64xGenAsmWriter.inc" |
124 | | |
125 | | #define GET_INSTRINFO_ENUM |
126 | | #include "TMS320C64xGenInstrInfo.inc" |
127 | | |
128 | | static void printOperand(MCInst *MI, unsigned OpNo, SStream *O) |
129 | 106k | { |
130 | 106k | MCOperand *Op = MCInst_getOperand(MI, OpNo); |
131 | 106k | unsigned reg; |
132 | | |
133 | 106k | if (MCOperand_isReg(Op)) { |
134 | 76.6k | reg = MCOperand_getReg(Op); |
135 | 76.6k | if ((MCInst_getOpcode(MI) == TMS320C64x_MVC_s1_rr) && |
136 | 5.01k | (OpNo == 1)) { |
137 | 2.50k | switch (reg) { |
138 | 681 | case TMS320C64X_REG_EFR: |
139 | 681 | SStream_concat0(O, "EFR"); |
140 | 681 | break; |
141 | 223 | case TMS320C64X_REG_IFR: |
142 | 223 | SStream_concat0(O, "IFR"); |
143 | 223 | break; |
144 | 1.60k | default: |
145 | 1.60k | SStream_concat0(O, getRegisterName(reg)); |
146 | 1.60k | break; |
147 | 2.50k | } |
148 | 74.1k | } else { |
149 | 74.1k | SStream_concat0(O, getRegisterName(reg)); |
150 | 74.1k | } |
151 | | |
152 | 76.6k | if (MI->csh->detail_opt) { |
153 | 76.6k | MI->flat_insn->detail->tms320c64x |
154 | 76.6k | .operands[MI->flat_insn->detail->tms320c64x |
155 | 76.6k | .op_count] |
156 | 76.6k | .type = TMS320C64X_OP_REG; |
157 | 76.6k | MI->flat_insn->detail->tms320c64x |
158 | 76.6k | .operands[MI->flat_insn->detail->tms320c64x |
159 | 76.6k | .op_count] |
160 | 76.6k | .reg = reg; |
161 | 76.6k | MI->flat_insn->detail->tms320c64x.op_count++; |
162 | 76.6k | } |
163 | 76.6k | } else if (MCOperand_isImm(Op)) { |
164 | 29.7k | int64_t Imm = MCOperand_getImm(Op); |
165 | | |
166 | 29.7k | if (Imm >= 0) { |
167 | 24.0k | if (Imm > HEX_THRESHOLD) |
168 | 14.3k | SStream_concat(O, "0x%" PRIx64, Imm); |
169 | 9.76k | else |
170 | 9.76k | SStream_concat(O, "%" PRIu64, Imm); |
171 | 24.0k | } else { |
172 | 5.66k | if (Imm < -HEX_THRESHOLD) |
173 | 3.85k | SStream_concat(O, "-0x%" PRIx64, -Imm); |
174 | 1.80k | else |
175 | 1.80k | SStream_concat(O, "-%" PRIu64, -Imm); |
176 | 5.66k | } |
177 | | |
178 | 29.7k | if (MI->csh->detail_opt) { |
179 | 29.7k | MI->flat_insn->detail->tms320c64x |
180 | 29.7k | .operands[MI->flat_insn->detail->tms320c64x |
181 | 29.7k | .op_count] |
182 | 29.7k | .type = TMS320C64X_OP_IMM; |
183 | 29.7k | MI->flat_insn->detail->tms320c64x |
184 | 29.7k | .operands[MI->flat_insn->detail->tms320c64x |
185 | 29.7k | .op_count] |
186 | 29.7k | .imm = Imm; |
187 | 29.7k | MI->flat_insn->detail->tms320c64x.op_count++; |
188 | 29.7k | } |
189 | 29.7k | } |
190 | 106k | } |
191 | | |
192 | | static void printMemOperand(MCInst *MI, unsigned OpNo, SStream *O) |
193 | 7.01k | { |
194 | 7.01k | MCOperand *Op = MCInst_getOperand(MI, OpNo); |
195 | 7.01k | int64_t Val = MCOperand_getImm(Op); |
196 | 7.01k | unsigned scaled, base, offset, mode, unit; |
197 | 7.01k | cs_tms320c64x *tms320c64x; |
198 | 7.01k | char st, nd; |
199 | | |
200 | 7.01k | scaled = (Val >> 19) & 1; |
201 | 7.01k | base = (Val >> 12) & 0x7f; |
202 | 7.01k | offset = (Val >> 5) & 0x7f; |
203 | 7.01k | mode = (Val >> 1) & 0xf; |
204 | 7.01k | unit = Val & 1; |
205 | | |
206 | 7.01k | if (scaled) { |
207 | 6.11k | st = '['; |
208 | 6.11k | nd = ']'; |
209 | 6.11k | } else { |
210 | 897 | st = '('; |
211 | 897 | nd = ')'; |
212 | 897 | } |
213 | | |
214 | 7.01k | switch (mode) { |
215 | 617 | case 0: |
216 | 617 | SStream_concat(O, "*-%s%c%u%c", getRegisterName(base), st, |
217 | 617 | offset, nd); |
218 | 617 | break; |
219 | 587 | case 1: |
220 | 587 | SStream_concat(O, "*+%s%c%u%c", getRegisterName(base), st, |
221 | 587 | offset, nd); |
222 | 587 | break; |
223 | 550 | case 4: |
224 | 550 | SStream_concat(O, "*-%s%c%s%c", getRegisterName(base), st, |
225 | 550 | getRegisterName(offset), nd); |
226 | 550 | break; |
227 | 266 | case 5: |
228 | 266 | SStream_concat(O, "*+%s%c%s%c", getRegisterName(base), st, |
229 | 266 | getRegisterName(offset), nd); |
230 | 266 | break; |
231 | 445 | case 8: |
232 | 445 | SStream_concat(O, "*--%s%c%u%c", getRegisterName(base), st, |
233 | 445 | offset, nd); |
234 | 445 | break; |
235 | 965 | case 9: |
236 | 965 | SStream_concat(O, "*++%s%c%u%c", getRegisterName(base), st, |
237 | 965 | offset, nd); |
238 | 965 | break; |
239 | 913 | case 10: |
240 | 913 | SStream_concat(O, "*%s--%c%u%c", getRegisterName(base), st, |
241 | 913 | offset, nd); |
242 | 913 | break; |
243 | 430 | case 11: |
244 | 430 | SStream_concat(O, "*%s++%c%u%c", getRegisterName(base), st, |
245 | 430 | offset, nd); |
246 | 430 | break; |
247 | 701 | case 12: |
248 | 701 | SStream_concat(O, "*--%s%c%s%c", getRegisterName(base), st, |
249 | 701 | getRegisterName(offset), nd); |
250 | 701 | break; |
251 | 502 | case 13: |
252 | 502 | SStream_concat(O, "*++%s%c%s%c", getRegisterName(base), st, |
253 | 502 | getRegisterName(offset), nd); |
254 | 502 | break; |
255 | 587 | case 14: |
256 | 587 | SStream_concat(O, "*%s--%c%s%c", getRegisterName(base), st, |
257 | 587 | getRegisterName(offset), nd); |
258 | 587 | break; |
259 | 452 | case 15: |
260 | 452 | SStream_concat(O, "*%s++%c%s%c", getRegisterName(base), st, |
261 | 452 | getRegisterName(offset), nd); |
262 | 452 | break; |
263 | 7.01k | } |
264 | | |
265 | 7.01k | if (MI->csh->detail_opt) { |
266 | 7.01k | tms320c64x = &MI->flat_insn->detail->tms320c64x; |
267 | | |
268 | 7.01k | tms320c64x->operands[tms320c64x->op_count].type = |
269 | 7.01k | TMS320C64X_OP_MEM; |
270 | 7.01k | tms320c64x->operands[tms320c64x->op_count].mem.base = base; |
271 | 7.01k | tms320c64x->operands[tms320c64x->op_count].mem.disp = offset; |
272 | 7.01k | tms320c64x->operands[tms320c64x->op_count].mem.unit = unit + 1; |
273 | 7.01k | tms320c64x->operands[tms320c64x->op_count].mem.scaled = scaled; |
274 | 7.01k | switch (mode) { |
275 | 617 | case 0: |
276 | 617 | tms320c64x->operands[tms320c64x->op_count].mem.disptype = |
277 | 617 | TMS320C64X_MEM_DISP_CONSTANT; |
278 | 617 | tms320c64x->operands[tms320c64x->op_count] |
279 | 617 | .mem.direction = TMS320C64X_MEM_DIR_BW; |
280 | 617 | tms320c64x->operands[tms320c64x->op_count].mem.modify = |
281 | 617 | TMS320C64X_MEM_MOD_NO; |
282 | 617 | break; |
283 | 587 | case 1: |
284 | 587 | tms320c64x->operands[tms320c64x->op_count].mem.disptype = |
285 | 587 | TMS320C64X_MEM_DISP_CONSTANT; |
286 | 587 | tms320c64x->operands[tms320c64x->op_count] |
287 | 587 | .mem.direction = TMS320C64X_MEM_DIR_FW; |
288 | 587 | tms320c64x->operands[tms320c64x->op_count].mem.modify = |
289 | 587 | TMS320C64X_MEM_MOD_NO; |
290 | 587 | break; |
291 | 550 | case 4: |
292 | 550 | tms320c64x->operands[tms320c64x->op_count].mem.disptype = |
293 | 550 | TMS320C64X_MEM_DISP_REGISTER; |
294 | 550 | tms320c64x->operands[tms320c64x->op_count] |
295 | 550 | .mem.direction = TMS320C64X_MEM_DIR_BW; |
296 | 550 | tms320c64x->operands[tms320c64x->op_count].mem.modify = |
297 | 550 | TMS320C64X_MEM_MOD_NO; |
298 | 550 | break; |
299 | 266 | case 5: |
300 | 266 | tms320c64x->operands[tms320c64x->op_count].mem.disptype = |
301 | 266 | TMS320C64X_MEM_DISP_REGISTER; |
302 | 266 | tms320c64x->operands[tms320c64x->op_count] |
303 | 266 | .mem.direction = TMS320C64X_MEM_DIR_FW; |
304 | 266 | tms320c64x->operands[tms320c64x->op_count].mem.modify = |
305 | 266 | TMS320C64X_MEM_MOD_NO; |
306 | 266 | break; |
307 | 445 | case 8: |
308 | 445 | tms320c64x->operands[tms320c64x->op_count].mem.disptype = |
309 | 445 | TMS320C64X_MEM_DISP_CONSTANT; |
310 | 445 | tms320c64x->operands[tms320c64x->op_count] |
311 | 445 | .mem.direction = TMS320C64X_MEM_DIR_BW; |
312 | 445 | tms320c64x->operands[tms320c64x->op_count].mem.modify = |
313 | 445 | TMS320C64X_MEM_MOD_PRE; |
314 | 445 | break; |
315 | 965 | case 9: |
316 | 965 | tms320c64x->operands[tms320c64x->op_count].mem.disptype = |
317 | 965 | TMS320C64X_MEM_DISP_CONSTANT; |
318 | 965 | tms320c64x->operands[tms320c64x->op_count] |
319 | 965 | .mem.direction = TMS320C64X_MEM_DIR_FW; |
320 | 965 | tms320c64x->operands[tms320c64x->op_count].mem.modify = |
321 | 965 | TMS320C64X_MEM_MOD_PRE; |
322 | 965 | break; |
323 | 913 | case 10: |
324 | 913 | tms320c64x->operands[tms320c64x->op_count].mem.disptype = |
325 | 913 | TMS320C64X_MEM_DISP_CONSTANT; |
326 | 913 | tms320c64x->operands[tms320c64x->op_count] |
327 | 913 | .mem.direction = TMS320C64X_MEM_DIR_BW; |
328 | 913 | tms320c64x->operands[tms320c64x->op_count].mem.modify = |
329 | 913 | TMS320C64X_MEM_MOD_POST; |
330 | 913 | break; |
331 | 430 | case 11: |
332 | 430 | tms320c64x->operands[tms320c64x->op_count].mem.disptype = |
333 | 430 | TMS320C64X_MEM_DISP_CONSTANT; |
334 | 430 | tms320c64x->operands[tms320c64x->op_count] |
335 | 430 | .mem.direction = TMS320C64X_MEM_DIR_FW; |
336 | 430 | tms320c64x->operands[tms320c64x->op_count].mem.modify = |
337 | 430 | TMS320C64X_MEM_MOD_POST; |
338 | 430 | break; |
339 | 701 | case 12: |
340 | 701 | tms320c64x->operands[tms320c64x->op_count].mem.disptype = |
341 | 701 | TMS320C64X_MEM_DISP_REGISTER; |
342 | 701 | tms320c64x->operands[tms320c64x->op_count] |
343 | 701 | .mem.direction = TMS320C64X_MEM_DIR_BW; |
344 | 701 | tms320c64x->operands[tms320c64x->op_count].mem.modify = |
345 | 701 | TMS320C64X_MEM_MOD_PRE; |
346 | 701 | break; |
347 | 502 | case 13: |
348 | 502 | tms320c64x->operands[tms320c64x->op_count].mem.disptype = |
349 | 502 | TMS320C64X_MEM_DISP_REGISTER; |
350 | 502 | tms320c64x->operands[tms320c64x->op_count] |
351 | 502 | .mem.direction = TMS320C64X_MEM_DIR_FW; |
352 | 502 | tms320c64x->operands[tms320c64x->op_count].mem.modify = |
353 | 502 | TMS320C64X_MEM_MOD_PRE; |
354 | 502 | break; |
355 | 587 | case 14: |
356 | 587 | tms320c64x->operands[tms320c64x->op_count].mem.disptype = |
357 | 587 | TMS320C64X_MEM_DISP_REGISTER; |
358 | 587 | tms320c64x->operands[tms320c64x->op_count] |
359 | 587 | .mem.direction = TMS320C64X_MEM_DIR_BW; |
360 | 587 | tms320c64x->operands[tms320c64x->op_count].mem.modify = |
361 | 587 | TMS320C64X_MEM_MOD_POST; |
362 | 587 | break; |
363 | 452 | case 15: |
364 | 452 | tms320c64x->operands[tms320c64x->op_count].mem.disptype = |
365 | 452 | TMS320C64X_MEM_DISP_REGISTER; |
366 | 452 | tms320c64x->operands[tms320c64x->op_count] |
367 | 452 | .mem.direction = TMS320C64X_MEM_DIR_FW; |
368 | 452 | tms320c64x->operands[tms320c64x->op_count].mem.modify = |
369 | 452 | TMS320C64X_MEM_MOD_POST; |
370 | 452 | break; |
371 | 7.01k | } |
372 | 7.01k | tms320c64x->op_count++; |
373 | 7.01k | } |
374 | 7.01k | } |
375 | | |
376 | | static void printMemOperand2(MCInst *MI, unsigned OpNo, SStream *O) |
377 | 5.34k | { |
378 | 5.34k | MCOperand *Op = MCInst_getOperand(MI, OpNo); |
379 | 5.34k | int64_t Val = MCOperand_getImm(Op); |
380 | 5.34k | uint16_t offset; |
381 | 5.34k | unsigned basereg; |
382 | 5.34k | cs_tms320c64x *tms320c64x; |
383 | | |
384 | 5.34k | basereg = Val & 0x7f; |
385 | 5.34k | offset = (Val >> 7) & 0x7fff; |
386 | 5.34k | SStream_concat(O, "*+%s[0x%x]", getRegisterName(basereg), offset); |
387 | | |
388 | 5.34k | if (MI->csh->detail_opt) { |
389 | 5.34k | tms320c64x = &MI->flat_insn->detail->tms320c64x; |
390 | | |
391 | 5.34k | tms320c64x->operands[tms320c64x->op_count].type = |
392 | 5.34k | TMS320C64X_OP_MEM; |
393 | 5.34k | tms320c64x->operands[tms320c64x->op_count].mem.base = basereg; |
394 | 5.34k | tms320c64x->operands[tms320c64x->op_count].mem.unit = 2; |
395 | 5.34k | tms320c64x->operands[tms320c64x->op_count].mem.disp = offset; |
396 | 5.34k | tms320c64x->operands[tms320c64x->op_count].mem.disptype = |
397 | 5.34k | TMS320C64X_MEM_DISP_CONSTANT; |
398 | 5.34k | tms320c64x->operands[tms320c64x->op_count].mem.direction = |
399 | 5.34k | TMS320C64X_MEM_DIR_FW; |
400 | 5.34k | tms320c64x->operands[tms320c64x->op_count].mem.modify = |
401 | 5.34k | TMS320C64X_MEM_MOD_NO; |
402 | 5.34k | tms320c64x->op_count++; |
403 | 5.34k | } |
404 | 5.34k | } |
405 | | |
406 | | static void printRegPair(MCInst *MI, unsigned OpNo, SStream *O) |
407 | 19.5k | { |
408 | 19.5k | MCOperand *Op = MCInst_getOperand(MI, OpNo); |
409 | 19.5k | unsigned reg = MCOperand_getReg(Op); |
410 | 19.5k | cs_tms320c64x *tms320c64x; |
411 | | |
412 | 19.5k | SStream_concat(O, "%s:%s", getRegisterName(reg + 1), |
413 | 19.5k | getRegisterName(reg)); |
414 | | |
415 | 19.5k | if (MI->csh->detail_opt) { |
416 | 19.5k | tms320c64x = &MI->flat_insn->detail->tms320c64x; |
417 | | |
418 | 19.5k | tms320c64x->operands[tms320c64x->op_count].type = |
419 | 19.5k | TMS320C64X_OP_REGPAIR; |
420 | 19.5k | tms320c64x->operands[tms320c64x->op_count].reg = reg; |
421 | 19.5k | tms320c64x->op_count++; |
422 | 19.5k | } |
423 | 19.5k | } |
424 | | |
425 | | static bool printAliasInstruction(MCInst *MI, SStream *O, MCRegisterInfo *MRI) |
426 | 60.7k | { |
427 | 60.7k | unsigned opcode = MCInst_getOpcode(MI); |
428 | 60.7k | MCOperand *op; |
429 | | |
430 | 60.7k | switch (opcode) { |
431 | | /* ADD.Dx -i, x, y -> SUB.Dx x, i, y */ |
432 | 791 | case TMS320C64x_ADD_d2_rir: |
433 | | /* ADD.L -i, x, y -> SUB.L x, i, y */ |
434 | 1.42k | case TMS320C64x_ADD_l1_irr: |
435 | 2.16k | case TMS320C64x_ADD_l1_ipp: |
436 | | /* ADD.S -i, x, y -> SUB.S x, i, y */ |
437 | 2.69k | case TMS320C64x_ADD_s1_irr: |
438 | 2.69k | if ((MCInst_getNumOperands(MI) == 3) && |
439 | 2.69k | MCOperand_isReg(MCInst_getOperand(MI, 0)) && |
440 | 2.69k | MCOperand_isReg(MCInst_getOperand(MI, 1)) && |
441 | 2.69k | MCOperand_isImm(MCInst_getOperand(MI, 2)) && |
442 | 2.69k | (MCOperand_getImm(MCInst_getOperand(MI, 2)) < 0)) { |
443 | 610 | MCInst_setOpcodePub(MI, TMS320C64X_INS_SUB); |
444 | 610 | op = MCInst_getOperand(MI, 2); |
445 | 610 | MCOperand_setImm(op, -MCOperand_getImm(op)); |
446 | | |
447 | 610 | SStream_concat0(O, "SUB\t"); |
448 | 610 | printOperand(MI, 1, O); |
449 | 610 | SStream_concat0(O, ", "); |
450 | 610 | printOperand(MI, 2, O); |
451 | 610 | SStream_concat0(O, ", "); |
452 | 610 | printOperand(MI, 0, O); |
453 | | |
454 | 610 | return true; |
455 | 610 | } |
456 | 2.08k | break; |
457 | 60.7k | } |
458 | 60.1k | switch (opcode) { |
459 | | /* ADD.D 0, x, y -> MV.D x, y */ |
460 | 367 | case TMS320C64x_ADD_d1_rir: |
461 | | /* OR.D x, 0, y -> MV.D x, y */ |
462 | 730 | case TMS320C64x_OR_d2_rir: |
463 | | /* ADD.L 0, x, y -> MV.L x, y */ |
464 | 1.30k | case TMS320C64x_ADD_l1_irr: |
465 | 1.70k | case TMS320C64x_ADD_l1_ipp: |
466 | | /* OR.L 0, x, y -> MV.L x, y */ |
467 | 1.82k | case TMS320C64x_OR_l1_irr: |
468 | | /* ADD.S 0, x, y -> MV.S x, y */ |
469 | 2.33k | case TMS320C64x_ADD_s1_irr: |
470 | | /* OR.S 0, x, y -> MV.S x, y */ |
471 | 2.61k | case TMS320C64x_OR_s1_irr: |
472 | 2.61k | if ((MCInst_getNumOperands(MI) == 3) && |
473 | 2.61k | MCOperand_isReg(MCInst_getOperand(MI, 0)) && |
474 | 2.61k | MCOperand_isReg(MCInst_getOperand(MI, 1)) && |
475 | 2.61k | MCOperand_isImm(MCInst_getOperand(MI, 2)) && |
476 | 2.61k | (MCOperand_getImm(MCInst_getOperand(MI, 2)) == 0)) { |
477 | 511 | MCInst_setOpcodePub(MI, TMS320C64X_INS_MV); |
478 | 511 | MI->size--; |
479 | | |
480 | 511 | SStream_concat0(O, "MV\t"); |
481 | 511 | printOperand(MI, 1, O); |
482 | 511 | SStream_concat0(O, ", "); |
483 | 511 | printOperand(MI, 0, O); |
484 | | |
485 | 511 | return true; |
486 | 511 | } |
487 | 2.10k | break; |
488 | 60.1k | } |
489 | 59.6k | switch (opcode) { |
490 | | /* XOR.D -1, x, y -> NOT.D x, y */ |
491 | 469 | case TMS320C64x_XOR_d2_rir: |
492 | | /* XOR.L -1, x, y -> NOT.L x, y */ |
493 | 936 | case TMS320C64x_XOR_l1_irr: |
494 | | /* XOR.S -1, x, y -> NOT.S x, y */ |
495 | 1.39k | case TMS320C64x_XOR_s1_irr: |
496 | 1.39k | if ((MCInst_getNumOperands(MI) == 3) && |
497 | 1.39k | MCOperand_isReg(MCInst_getOperand(MI, 0)) && |
498 | 1.39k | MCOperand_isReg(MCInst_getOperand(MI, 1)) && |
499 | 1.39k | MCOperand_isImm(MCInst_getOperand(MI, 2)) && |
500 | 1.39k | (MCOperand_getImm(MCInst_getOperand(MI, 2)) == -1)) { |
501 | 326 | MCInst_setOpcodePub(MI, TMS320C64X_INS_NOT); |
502 | 326 | MI->size--; |
503 | | |
504 | 326 | SStream_concat0(O, "NOT\t"); |
505 | 326 | printOperand(MI, 1, O); |
506 | 326 | SStream_concat0(O, ", "); |
507 | 326 | printOperand(MI, 0, O); |
508 | | |
509 | 326 | return true; |
510 | 326 | } |
511 | 1.07k | break; |
512 | 59.6k | } |
513 | 59.3k | switch (opcode) { |
514 | | /* MVK.D 0, x -> ZERO.D x */ |
515 | 1.00k | case TMS320C64x_MVK_d1_rr: |
516 | | /* MVK.L 0, x -> ZERO.L x */ |
517 | 3.04k | case TMS320C64x_MVK_l2_ir: |
518 | 3.04k | if ((MCInst_getNumOperands(MI) == 2) && |
519 | 3.04k | MCOperand_isReg(MCInst_getOperand(MI, 0)) && |
520 | 3.04k | MCOperand_isImm(MCInst_getOperand(MI, 1)) && |
521 | 3.04k | (MCOperand_getImm(MCInst_getOperand(MI, 1)) == 0)) { |
522 | 837 | MCInst_setOpcodePub(MI, TMS320C64X_INS_ZERO); |
523 | 837 | MI->size--; |
524 | | |
525 | 837 | SStream_concat0(O, "ZERO\t"); |
526 | 837 | printOperand(MI, 0, O); |
527 | | |
528 | 837 | return true; |
529 | 837 | } |
530 | 2.21k | break; |
531 | 59.3k | } |
532 | 58.5k | switch (opcode) { |
533 | | /* SUB.L x, x, y -> ZERO.L y */ |
534 | 824 | case TMS320C64x_SUB_l1_rrp_x1: |
535 | | /* SUB.S x, x, y -> ZERO.S y */ |
536 | 1.55k | case TMS320C64x_SUB_s1_rrr: |
537 | 1.55k | if ((MCInst_getNumOperands(MI) == 3) && |
538 | 1.55k | MCOperand_isReg(MCInst_getOperand(MI, 0)) && |
539 | 1.55k | MCOperand_isReg(MCInst_getOperand(MI, 1)) && |
540 | 1.55k | MCOperand_isReg(MCInst_getOperand(MI, 2)) && |
541 | 1.55k | (MCOperand_getReg(MCInst_getOperand(MI, 1)) == |
542 | 1.55k | MCOperand_getReg(MCInst_getOperand(MI, 2)))) { |
543 | 579 | MCInst_setOpcodePub(MI, TMS320C64X_INS_ZERO); |
544 | 579 | MI->size -= 2; |
545 | | |
546 | 579 | SStream_concat0(O, "ZERO\t"); |
547 | 579 | printOperand(MI, 0, O); |
548 | | |
549 | 579 | return true; |
550 | 579 | } |
551 | 971 | break; |
552 | 58.5k | } |
553 | 57.9k | switch (opcode) { |
554 | | /* SUB.L 0, x, y -> NEG.L x, y */ |
555 | 750 | case TMS320C64x_SUB_l1_irr: |
556 | 1.17k | case TMS320C64x_SUB_l1_ipp: |
557 | | /* SUB.S 0, x, y -> NEG.S x, y */ |
558 | 1.47k | case TMS320C64x_SUB_s1_irr: |
559 | 1.47k | if ((MCInst_getNumOperands(MI) == 3) && |
560 | 1.47k | MCOperand_isReg(MCInst_getOperand(MI, 0)) && |
561 | 1.47k | MCOperand_isReg(MCInst_getOperand(MI, 1)) && |
562 | 1.47k | MCOperand_isImm(MCInst_getOperand(MI, 2)) && |
563 | 1.47k | (MCOperand_getImm(MCInst_getOperand(MI, 2)) == 0)) { |
564 | 416 | MCInst_setOpcodePub(MI, TMS320C64X_INS_NEG); |
565 | 416 | MI->size--; |
566 | | |
567 | 416 | SStream_concat0(O, "NEG\t"); |
568 | 416 | printOperand(MI, 1, O); |
569 | 416 | SStream_concat0(O, ", "); |
570 | 416 | printOperand(MI, 0, O); |
571 | | |
572 | 416 | return true; |
573 | 416 | } |
574 | 1.06k | break; |
575 | 57.9k | } |
576 | 57.5k | switch (opcode) { |
577 | | /* PACKLH2.L x, x, y -> SWAP2.L x, y */ |
578 | 484 | case TMS320C64x_PACKLH2_l1_rrr_x2: |
579 | | /* PACKLH2.S x, x, y -> SWAP2.S x, y */ |
580 | 847 | case TMS320C64x_PACKLH2_s1_rrr: |
581 | 847 | if ((MCInst_getNumOperands(MI) == 3) && |
582 | 847 | MCOperand_isReg(MCInst_getOperand(MI, 0)) && |
583 | 847 | MCOperand_isReg(MCInst_getOperand(MI, 1)) && |
584 | 847 | MCOperand_isReg(MCInst_getOperand(MI, 2)) && |
585 | 847 | (MCOperand_getReg(MCInst_getOperand(MI, 1)) == |
586 | 847 | MCOperand_getReg(MCInst_getOperand(MI, 2)))) { |
587 | 153 | MCInst_setOpcodePub(MI, TMS320C64X_INS_SWAP2); |
588 | 153 | MI->size--; |
589 | | |
590 | 153 | SStream_concat0(O, "SWAP2\t"); |
591 | 153 | printOperand(MI, 1, O); |
592 | 153 | SStream_concat0(O, ", "); |
593 | 153 | printOperand(MI, 0, O); |
594 | | |
595 | 153 | return true; |
596 | 153 | } |
597 | 694 | break; |
598 | 57.5k | } |
599 | 57.3k | switch (opcode) { |
600 | | /* NOP 16 -> IDLE */ |
601 | | /* NOP 1 -> NOP */ |
602 | 1.80k | case TMS320C64x_NOP_n: |
603 | 1.80k | if ((MCInst_getNumOperands(MI) == 1) && |
604 | 1.80k | MCOperand_isImm(MCInst_getOperand(MI, 0)) && |
605 | 1.80k | (MCOperand_getReg(MCInst_getOperand(MI, 0)) == 16)) { |
606 | 315 | MCInst_setOpcodePub(MI, TMS320C64X_INS_IDLE); |
607 | 315 | MI->size--; |
608 | | |
609 | 315 | SStream_concat0(O, "IDLE"); |
610 | | |
611 | 315 | return true; |
612 | 315 | } |
613 | 1.48k | if ((MCInst_getNumOperands(MI) == 1) && |
614 | 1.48k | MCOperand_isImm(MCInst_getOperand(MI, 0)) && |
615 | 1.48k | (MCOperand_getReg(MCInst_getOperand(MI, 0)) == 1)) { |
616 | 976 | MI->size--; |
617 | | |
618 | 976 | SStream_concat0(O, "NOP"); |
619 | | |
620 | 976 | return true; |
621 | 976 | } |
622 | 512 | break; |
623 | 57.3k | } |
624 | | |
625 | 56.0k | return false; |
626 | 57.3k | } |
627 | | |
628 | | void TMS320C64x_printInst(MCInst *MI, SStream *O, void *Info) |
629 | 60.7k | { |
630 | 60.7k | if (!printAliasInstruction(MI, O, Info)) |
631 | 56.0k | printInstruction(MI, O, Info); |
632 | 60.7k | } |
633 | | |
634 | | #endif |