/src/capstonenext/arch/M68K/M68KInstPrinter.c
Line | Count | Source |
1 | | /* Capstone Disassembly Engine */ |
2 | | /* M68K Backend by Daniel Collin <daniel@collin.com> 2015-2016 */ |
3 | | |
4 | | #include <stdio.h> |
5 | | #include <stdlib.h> |
6 | | #include <string.h> |
7 | | |
8 | | #include "M68KInstPrinter.h" |
9 | | |
10 | | #include "M68KDisassembler.h" |
11 | | |
12 | | #include "../../Mapping.h" |
13 | | #include "../../cs_priv.h" |
14 | | #include "../../utils.h" |
15 | | |
16 | | #include "../../MCInst.h" |
17 | | #include "../../MCInstrDesc.h" |
18 | | #include "../../MCRegisterInfo.h" |
19 | | #include "../../MathExtras.h" |
20 | | |
21 | | #ifndef CAPSTONE_DIET |
22 | | static const char s_spacing[] = " "; |
23 | | |
24 | | static const char *const s_reg_names[] = { |
25 | | "invalid", "d0", "d1", "d2", "d3", "d4", "d5", "d6", |
26 | | "d7", "a0", "a1", "a2", "a3", "a4", "a5", "a6", |
27 | | "a7", "fp0", "fp1", "fp2", "fp3", "fp4", "fp5", "fp6", |
28 | | "fp7", "pc", "sr", "ccr", "sfc", "dfc", "usp", "vbr", |
29 | | "cacr", "caar", "msp", "isp", "tc", "itt0", "itt1", "dtt0", |
30 | | "dtt1", "mmusr", "urp", "srp", |
31 | | |
32 | | "fpcr", "fpsr", "fpiar", |
33 | | |
34 | | "tt0", "tt1", "crp", "acc", "acc0", "acc1", "acc2", "acc3", |
35 | | "accext01", "accext23", "macsr", "mask", |
36 | | }; |
37 | | |
38 | | static const char *const s_instruction_names[] = { |
39 | | "invalid", "abcd", "add", "adda", "addi", |
40 | | "addq", "addx", "and", "andi", "asl", |
41 | | "asr", "bhs", "blo", "bhi", "bls", |
42 | | "bcc", "bcs", "bne", "beq", "bvc", |
43 | | "bvs", "bpl", "bmi", "bge", "blt", |
44 | | "bgt", "ble", "bra", "bsr", "bchg", |
45 | | "bclr", "bset", "btst", "bitrev", "byterev", |
46 | | "bfchg", "bfclr", "bfexts", "bfextu", "bfffo", |
47 | | "bfins", "bfset", "bftst", "bkpt", "callm", |
48 | | "cas", "cas2", "chk", "chk2", "clr", |
49 | | "cmp", "cmpa", "cmpi", "cmpm", "cmp2", |
50 | | "cinvl", "cinvp", "cinva", "cpushl", "cpushp", |
51 | | "cpusha", "dbt", "dbf", "dbhi", "dbls", |
52 | | "dbcc", "dbcs", "dbne", "dbeq", "dbvc", |
53 | | "dbvs", "dbpl", "dbmi", "dbge", "dblt", |
54 | | "dbgt", "dble", "dbra", "divs", "divsl", |
55 | | "divu", "divul", "eor", "eori", "exg", |
56 | | "ext", "extb", "ff1", "fabs", "fsabs", |
57 | | "fdabs", "facos", "fadd", "fsadd", "fdadd", |
58 | | "fasin", "fatan", "fatanh", "fbf", "fbeq", |
59 | | "fbogt", "fboge", "fbolt", "fbole", "fbogl", |
60 | | "fbor", "fbun", "fbueq", "fbugt", "fbuge", |
61 | | "fbult", "fbule", "fbne", "fbt", "fbsf", |
62 | | "fbseq", "fbgt", "fbge", "fblt", "fble", |
63 | | "fbgl", "fbgle", "fbngle", "fbngl", "fbnle", |
64 | | "fbnlt", "fbnge", "fbngt", "fbsne", "fbst", |
65 | | "fcmp", "fcos", "fcosh", "fdbf", "fdbeq", |
66 | | "fdbogt", "fdboge", "fdbolt", "fdbole", "fdbogl", |
67 | | "fdbor", "fdbun", "fdbueq", "fdbugt", "fdbuge", |
68 | | "fdbult", "fdbule", "fdbne", "fdbt", "fdbsf", |
69 | | "fdbseq", "fdbgt", "fdbge", "fdblt", "fdble", |
70 | | "fdbgl", "fdbgle", "fdbngle", "fdbngl", "fdbnle", |
71 | | "fdbnlt", "fdbnge", "fdbngt", "fdbsne", "fdbst", |
72 | | "fdiv", "fsdiv", "fddiv", "fetox", "fetoxm1", |
73 | | "fgetexp", "fgetman", "fint", "fintrz", "flog10", |
74 | | "flog2", "flogn", "flognp1", "fmod", "fmove", |
75 | | "fsmove", "fdmove", "fmovecr", "fmovem", "fmul", |
76 | | "fsmul", "fdmul", "fneg", "fsneg", "fdneg", |
77 | | "fnop", "frem", "frestore", "fsave", "fscale", |
78 | | "fsgldiv", "fsglmul", "fsin", "fsincos", "fsinh", |
79 | | "fsqrt", "fssqrt", "fdsqrt", "fsf", "fseq", |
80 | | "fsogt", "fsoge", "fsolt", "fsole", "fsogl", |
81 | | "fsor", "fsun", "fsueq", "fsugt", "fsuge", |
82 | | "fsult", "fsule", "fsne", "fst", "fssf", |
83 | | "fsseq", "fsgt", "fsge", "fslt", "fsle", |
84 | | "fsgl", "fsgle", "fsngle", "fsngl", "fsnle", |
85 | | "fsnlt", "fsnge", "fsngt", "fssne", "fsst", |
86 | | "fsub", "fssub", "fdsub", "ftan", "ftanh", |
87 | | "ftentox", "ftrapf", "ftrapeq", "ftrapogt", "ftrapoge", |
88 | | "ftrapolt", "ftrapole", "ftrapogl", "ftrapor", "ftrapun", |
89 | | "ftrapueq", "ftrapugt", "ftrapuge", "ftrapult", "ftrapule", |
90 | | "ftrapne", "ftrapt", "ftrapsf", "ftrapseq", "ftrapgt", |
91 | | "ftrapge", "ftraplt", "ftraple", "ftrapgl", "ftrapgle", |
92 | | "ftrapngle", "ftrapngl", "ftrapnle", "ftrapnlt", "ftrapnge", |
93 | | "ftrapngt", "ftrapsne", "ftrapst", "ftst", "ftwotox", |
94 | | "halt", "illegal", "intouch", "jmp", "jsr", |
95 | | "lea", "link", "lpstop", "lsl", "lsr", |
96 | | "mac", "move", "movea", "movec", "movem", |
97 | | "movep", "moveq", "moves", "move16", "mov3q", |
98 | | "movclr", "msac", "muls", "mulu", "mvs", |
99 | | "mvz", "nbcd", "neg", "negx", "nop", |
100 | | "not", "or", "ori", "pack", "pea", |
101 | | "pflush", "pflusha", "pflushan", "pflushn", "ploadr", |
102 | | "ploadw", "plpar", "plpaw", "pmove", "pmovefd", |
103 | | "ptestr", "ptestw", "pulse", "rems", "remu", |
104 | | "reset", "rol", "ror", "roxl", "roxr", |
105 | | "rtd", "rte", "rtm", "rtr", "rts", |
106 | | "sats", "sbcd", "st", "sf", "shi", |
107 | | "sls", "scc", "shs", "scs", "slo", |
108 | | "sne", "seq", "svc", "svs", "spl", |
109 | | "smi", "sge", "slt", "sgt", "sle", |
110 | | "stop", "strldsr", "sub", "suba", "subi", |
111 | | "subq", "subx", "swap", "tas", "trap", |
112 | | "trapv", "trapt", "trapf", "traphi", "trapls", |
113 | | "trapcc", "traphs", "trapcs", "traplo", "trapne", |
114 | | "trapeq", "trapvc", "trapvs", "trappl", "trapmi", |
115 | | "trapge", "traplt", "trapgt", "traple", "tst", |
116 | | "unlk", "unpk", "wddata", "wdebug", "bgnd", |
117 | | "tbls", "tblu", "tblsn", "tblun", "cp0bcbusy", |
118 | | "cp0ld", "cp0nop", "cp0st", "cp1bcbusy", "cp1ld", |
119 | | "cp1nop", "cp1st", "tpf", "maaac", "masac", |
120 | | "msaac", "mssac", "pbbs", "pbbc", "pbls", |
121 | | "pblc", "pbss", "pbsc", "pbas", "pbac", |
122 | | "pbws", "pbwc", "pbis", "pbic", "pbgs", |
123 | | "pbgc", "pbcs", "pbcc", |
124 | | }; |
125 | | #endif |
126 | | |
127 | | #ifndef CAPSTONE_DIET |
128 | | static const char *getRegName(m68k_reg reg) |
129 | 221k | { |
130 | 221k | return s_reg_names[(int)reg]; |
131 | 221k | } |
132 | | |
133 | | static void printRegbits(SStream *O, bool *need_sep, uint32_t data, |
134 | | const char *prefix) |
135 | 16.7k | { |
136 | 16.7k | unsigned int first; |
137 | 16.7k | int i; |
138 | | |
139 | 133k | for (i = 0; i < 8; ++i) { |
140 | 116k | if (!(data & (1 << i))) |
141 | 99.5k | continue; |
142 | | |
143 | 16.6k | first = i; |
144 | 34.7k | while (i < 7 && (data & (1 << (i + 1)))) |
145 | 18.0k | i++; |
146 | | |
147 | 16.6k | if (*need_sep) |
148 | 11.0k | SStream_concat1(O, '/'); |
149 | 16.6k | *need_sep = true; |
150 | | |
151 | 16.6k | SStream_concat(O, "%s%" PRIu32, prefix, first); |
152 | | |
153 | 16.6k | if ((unsigned int)i > first) |
154 | 6.71k | SStream_concat(O, "-%s%" PRIu32, prefix, |
155 | 6.71k | (unsigned int)i); |
156 | 16.6k | } |
157 | 16.7k | } |
158 | | |
159 | | static void registerBits(SStream *O, const cs_m68k_op *op) |
160 | 10.9k | { |
161 | 10.9k | unsigned int data = op->register_bits; |
162 | 10.9k | bool need_sep = false; |
163 | | |
164 | 10.9k | if (!data) { |
165 | 802 | SStream_concat(O, "%s", "#$0"); |
166 | 802 | return; |
167 | 802 | } |
168 | | |
169 | 10.1k | printRegbits(O, &need_sep, data & 0xff, "d"); |
170 | 10.1k | printRegbits(O, &need_sep, (data >> 8) & 0xff, "a"); |
171 | 10.1k | printRegbits(O, &need_sep, (data >> 16) & 0xff, "fp"); |
172 | 10.1k | } |
173 | | |
174 | | static void registerPair(SStream *O, const cs_m68k_op *op) |
175 | 7.25k | { |
176 | 7.25k | SStream_concat(O, "%s:%s", s_reg_names[op->reg_pair.reg_0], |
177 | 7.25k | s_reg_names[op->reg_pair.reg_1]); |
178 | 7.25k | } |
179 | | |
180 | | static void printRegisterName(SStream *O, const cs_m68k_op *op) |
181 | 177k | { |
182 | 177k | SStream_concat(O, "%s", getRegName(op->reg)); |
183 | 177k | if (op->flags & M68K_OP_FLAG_REG_LOWER) |
184 | 0 | SStream_concat0(O, "l"); |
185 | 177k | else if (op->flags & M68K_OP_FLAG_REG_UPPER) |
186 | 0 | SStream_concat0(O, "u"); |
187 | 177k | } |
188 | | |
189 | | static void printScaleFactor(SStream *O, uint8_t scale, int threshold) |
190 | 16.3k | { |
191 | 16.3k | if (scale > threshold) |
192 | 8.42k | SStream_concat(O, "%s*%s%" PRId8, s_spacing, s_spacing, scale); |
193 | 16.3k | } |
194 | | |
195 | | static void printIndexReg(SStream *O, const cs_m68k_op *op) |
196 | 14.4k | { |
197 | 14.4k | SStream_concat(O, "%s.%c", getRegName(op->mem.index_reg), |
198 | 14.4k | op->mem.index_size ? 'l' : 'w'); |
199 | 14.4k | } |
200 | | |
201 | | static void printBitfield(SStream *O, const cs_m68k_op *op) |
202 | 380k | { |
203 | 380k | if (!op->mem.bitfield) |
204 | 378k | return; |
205 | 1.95k | SStream_concat0(O, "{"); |
206 | 1.95k | if (M68K_BF_IS_REG(op->mem.offset)) |
207 | 1.12k | SStream_concat(O, "d%" PRId8, M68K_BF_REG_NUM(op->mem.offset)); |
208 | 830 | else |
209 | 830 | SStream_concat(O, "%" PRId8, op->mem.offset); |
210 | 1.95k | SStream_concat0(O, ":"); |
211 | 1.95k | if (M68K_BF_IS_REG(op->mem.width)) |
212 | 1.07k | SStream_concat(O, "d%" PRId8, M68K_BF_REG_NUM(op->mem.width)); |
213 | 881 | else |
214 | 881 | SStream_concat(O, "%" PRId8, op->mem.width); |
215 | 1.95k | SStream_concat0(O, "}"); |
216 | 1.95k | } |
217 | | |
218 | | static bool packed_bcd_to_string(uint64_t packed, char digits[17]) |
219 | 544 | { |
220 | 544 | unsigned int i; |
221 | | |
222 | 3.79k | for (i = 0; i < 16; ++i) { |
223 | 3.68k | unsigned int digit = |
224 | 3.68k | (unsigned int)((packed >> ((15 - i) * 4)) & 0xf); |
225 | 3.68k | if (digit > 9) |
226 | 440 | return false; |
227 | 3.24k | digits[i] = (char)('0' + digit); |
228 | 3.24k | } |
229 | 104 | digits[16] = '\0'; |
230 | 104 | return true; |
231 | 544 | } |
232 | | |
233 | | static bool packed_exponent_to_string(uint16_t packed, char digits[4]) |
234 | 905 | { |
235 | 905 | unsigned int i; |
236 | | |
237 | 2.97k | for (i = 0; i < 3; ++i) { |
238 | 2.42k | unsigned int digit = |
239 | 2.42k | (unsigned int)((packed >> ((2 - i) * 4)) & 0xf); |
240 | 2.42k | if (digit > 9) |
241 | 361 | return false; |
242 | 2.06k | digits[i] = (char)('0' + digit); |
243 | 2.06k | } |
244 | 544 | digits[3] = '\0'; |
245 | 544 | return true; |
246 | 905 | } |
247 | | |
248 | | static void printPackedImmediate(SStream *O, const m68k_op_fp_packed *value) |
249 | 1.25k | { |
250 | 1.25k | const uint32_t header = value->header; |
251 | 1.25k | const bool negative = (header & 0x80000000U) != 0; |
252 | 1.25k | const bool negative_exponent = (header & 0x40000000U) != 0; |
253 | 1.25k | const uint16_t exponent = (uint16_t)((header >> 16) & 0x0fff); |
254 | 1.25k | const unsigned int integer_digit = header & 0xf; |
255 | 1.25k | char exponent_digits[4]; |
256 | 1.25k | char fraction_digits[17]; |
257 | | |
258 | | /* SE=1, y=3, and exponent=FFF encode infinity or NaN. */ |
259 | 1.25k | if (((header >> 16) & 0x7fff) == 0x7fff) { |
260 | 160 | SStream_concat(O, "#0e%s%s", negative ? "-" : "", |
261 | 160 | value->fraction == 0 ? "inf" : "nan"); |
262 | 160 | return; |
263 | 160 | } |
264 | | |
265 | 1.09k | if (integer_digit == 0 && value->fraction == 0) { |
266 | 67 | SStream_concat(O, "#0e%s0", negative ? "-" : ""); |
267 | 67 | return; |
268 | 67 | } |
269 | | |
270 | 1.02k | if (integer_digit > 9 || |
271 | 905 | !packed_exponent_to_string(exponent, exponent_digits) || |
272 | 924 | !packed_bcd_to_string(value->fraction, fraction_digits)) { |
273 | 924 | SStream_concat(O, "#:%08" PRIx32 "%016" PRIx64, header, |
274 | 924 | value->fraction); |
275 | 924 | return; |
276 | 924 | } |
277 | | |
278 | 104 | SStream_concat(O, "#0e%s%u.%se%c%s", negative ? "-" : "", integer_digit, |
279 | 104 | fraction_digits, negative_exponent ? '-' : '+', |
280 | 104 | exponent_digits); |
281 | 104 | } |
282 | | |
283 | | /* Issue #681: Windows kernel printf-style functions do not support |
284 | | * floating-point format specifiers, so the text conversion is unused there. */ |
285 | | #if !defined(_KERNEL_MODE) |
286 | | /* Round `value >> shift` to nearest, ties to even. Requires 1 <= shift <= 64. */ |
287 | | static uint64_t round_right_to_even(uint64_t value, unsigned int shift) |
288 | 1.64k | { |
289 | 1.64k | if (shift < 64) { |
290 | 1.50k | uint64_t quotient = value >> shift; |
291 | 1.50k | uint64_t remainder = value & ((1ULL << shift) - 1); |
292 | 1.50k | uint64_t halfway = 1ULL << (shift - 1); |
293 | 1.50k | if (remainder > halfway || |
294 | 1.03k | (remainder == halfway && (quotient & 1))) |
295 | 938 | return quotient + 1; |
296 | 563 | return quotient; |
297 | 1.50k | } |
298 | 144 | if (value > 0x8000000000000000ULL) |
299 | 110 | return 1; |
300 | 34 | return 0; |
301 | 144 | } |
302 | | |
303 | | /* Convert only for assembly-text rendering. The detail operand retains the |
304 | | * complete external representation in fp_extended. */ |
305 | | static double extended_to_double(const m68k_op_fp_extended *value) |
306 | 2.83k | { |
307 | 2.83k | const uint16_t sign_exp = value->sign_exp; |
308 | 2.83k | const uint64_t significand = value->significand; |
309 | 2.83k | const uint64_t sign = ((uint64_t)(sign_exp & 0x8000)) << 48; |
310 | 2.83k | const unsigned int E = sign_exp & 0x7fff; |
311 | | |
312 | 2.83k | if (E == 0x7fff) { |
313 | 211 | uint64_t fraction = significand & 0x7fffffffffffffffULL; |
314 | 211 | if (fraction == 0) |
315 | 34 | return BitsToDouble(sign | 0x7ff0000000000000ULL); |
316 | 177 | return BitsToDouble(sign | 0x7ff0000000000000ULL | |
317 | 177 | ((fraction >> 11) | 0x0008000000000000ULL)); |
318 | 211 | } |
319 | | |
320 | 2.62k | if (E == 0 || significand == 0) |
321 | 345 | return BitsToDouble(sign); |
322 | | |
323 | 2.28k | { |
324 | 2.28k | unsigned int leading = CountLeadingZeros_64(significand); |
325 | 2.28k | uint64_t normalized = significand << leading; |
326 | 2.28k | int64_t e = (int64_t)E - 16383 - (int64_t)leading; |
327 | | |
328 | 2.28k | if (e > 1023) |
329 | 237 | return BitsToDouble(sign | 0x7ff0000000000000ULL); |
330 | | |
331 | 2.04k | if (e >= -1022) { |
332 | 862 | uint64_t rounded = round_right_to_even(normalized, 11); |
333 | 862 | if (rounded == (1ULL << 53)) { |
334 | 376 | rounded >>= 1; |
335 | 376 | ++e; |
336 | 376 | if (e > 1023) |
337 | 68 | return BitsToDouble( |
338 | 68 | sign | 0x7ff0000000000000ULL); |
339 | 376 | } |
340 | 794 | return BitsToDouble(sign | |
341 | 794 | ((uint64_t)(e + 1023) << 52) | |
342 | 794 | (rounded & 0x000fffffffffffffULL)); |
343 | 862 | } |
344 | | |
345 | 1.18k | { |
346 | 1.18k | uint64_t shift = (uint64_t)(-e - 1011); |
347 | 1.18k | uint64_t fraction = |
348 | 1.18k | shift > 64 ? 0 : |
349 | 1.18k | round_right_to_even( |
350 | 783 | normalized, |
351 | 783 | (unsigned int)shift); |
352 | 1.18k | return BitsToDouble(sign | fraction); |
353 | 2.04k | } |
354 | 2.04k | } |
355 | 2.04k | } |
356 | | #endif |
357 | | |
358 | | static void printImmediate(SStream *O, const cs_m68k *inst, |
359 | | const cs_m68k_op *op) |
360 | 56.4k | { |
361 | 56.4k | if (inst->op_size.type == M68K_SIZE_TYPE_FPU) { |
362 | 4.91k | if (op->type == M68K_OP_FP_PACKED) { |
363 | 1.25k | printPackedImmediate(O, &op->fp_packed); |
364 | 1.25k | return; |
365 | 1.25k | } |
366 | | #if defined(_KERNEL_MODE) |
367 | | SStream_concat(O, "#<float_point_unsupported>"); |
368 | | return; |
369 | | #else |
370 | | /* Dispatch on the operand storage type. Extended immediates |
371 | | * are converted only for GNU-compatible assembly text. */ |
372 | 3.66k | if (op->type == M68K_OP_FP_SINGLE) |
373 | 340 | SStream_concat(O, "#%f", op->simm); |
374 | 3.32k | else if (op->type == M68K_OP_FP_DOUBLE) |
375 | 484 | SStream_concat(O, "#%f", op->dimm); |
376 | 2.83k | else if (op->type == M68K_OP_FP_EXTENDED) |
377 | 2.83k | SStream_concat(O, "#0e%g", |
378 | 2.83k | extended_to_double(&op->fp_extended)); |
379 | 0 | else |
380 | 0 | SStream_concat(O, "#<unsupported>"); |
381 | 3.66k | return; |
382 | 4.91k | #endif |
383 | 4.91k | } |
384 | 51.5k | SStream_concat(O, "#$%" PRIx64, op->imm); |
385 | 51.5k | } |
386 | | |
387 | | static void printIndex8BitDisp(SStream *O, uint32_t pc, const cs_m68k_op *op) |
388 | 10.4k | { |
389 | 10.4k | if (op->address_mode == M68K_AM_PCI_INDEX_8_BIT_DISP) { |
390 | 1.25k | SStream_concat(O, "$%" PRIx32 "(pc,%s", pc + 2 + op->mem.disp, |
391 | 1.25k | s_spacing); |
392 | 9.24k | } else { |
393 | 9.24k | SStream_concat(O, "%s$%" PRIx16 "(%s,%s", |
394 | 9.24k | op->mem.disp < 0 ? "-" : "", abs(op->mem.disp), |
395 | 9.24k | getRegName(op->mem.base_reg), s_spacing); |
396 | 9.24k | } |
397 | 10.4k | printIndexReg(O, op); |
398 | 10.4k | printScaleFactor(O, op->mem.scale, 1); |
399 | 10.4k | SStream_concat0(O, ")"); |
400 | 10.4k | } |
401 | | |
402 | | static void printRegAddrMode(SStream *O, uint32_t pc, const cs_m68k_op *op) |
403 | 239k | { |
404 | 239k | m68k_reg base_reg = op->type == M68K_OP_MEM ? op->mem.base_reg : |
405 | 239k | op->reg; |
406 | | |
407 | 239k | switch (op->address_mode) { |
408 | 125k | case M68K_AM_REG_DIRECT_DATA: |
409 | 125k | printRegisterName(O, op); |
410 | 125k | break; |
411 | 15.5k | case M68K_AM_REG_DIRECT_ADDR: |
412 | 15.5k | printRegisterName(O, op); |
413 | 15.5k | break; |
414 | 23.9k | case M68K_AM_REGI_ADDR: |
415 | 23.9k | SStream_concat(O, "(a%" PRId32 ")", (base_reg - M68K_REG_A0)); |
416 | 23.9k | break; |
417 | 25.0k | case M68K_AM_REGI_ADDR_POST_INC: |
418 | 25.0k | SStream_concat(O, "(a%" PRId32 ")+", (base_reg - M68K_REG_A0)); |
419 | 25.0k | break; |
420 | 34.7k | case M68K_AM_REGI_ADDR_PRE_DEC: |
421 | 34.7k | SStream_concat(O, "-(a%" PRId32 ")", (base_reg - M68K_REG_A0)); |
422 | 34.7k | break; |
423 | 12.4k | case M68K_AM_REGI_ADDR_DISP: |
424 | 12.4k | SStream_concat(O, "%s$%" PRIx16 "(a%" PRId32 ")", |
425 | 12.4k | op->mem.disp < 0 ? "-" : "", abs(op->mem.disp), |
426 | 12.4k | (base_reg - M68K_REG_A0)); |
427 | 12.4k | break; |
428 | 2.08k | case M68K_AM_PCI_DISP: |
429 | 2.08k | SStream_concat(O, "$%" PRIx32 "(pc)", pc + 2 + op->mem.disp); |
430 | 2.08k | break; |
431 | 0 | default: |
432 | 0 | break; |
433 | 239k | } |
434 | 239k | } |
435 | | |
436 | | static void printBaseDisp(SStream *O, uint32_t pc, const cs_m68k_op *op) |
437 | 2.38k | { |
438 | 2.38k | int is_pc = (op->address_mode == M68K_AM_PCI_INDEX_BASE_DISP); |
439 | | |
440 | 2.38k | if (is_pc) { |
441 | 441 | SStream_concat(O, "$%" PRIx32, pc + 2 + op->mem.in_disp); |
442 | 1.94k | } else if (op->mem.in_disp != 0) { |
443 | 778 | SStream_concat(O, "%s$%" PRIx32, |
444 | 778 | op->mem.in_disp >= 0 ? "" : "-", |
445 | 778 | abs(op->mem.in_disp)); |
446 | 778 | } |
447 | | |
448 | 2.38k | SStream_concat0(O, "("); |
449 | | |
450 | 2.38k | if (is_pc) { |
451 | 441 | SStream_concat0(O, "pc"); |
452 | 1.94k | } else if (op->mem.base_reg != M68K_REG_INVALID) { |
453 | 1.39k | SStream_concat(O, "a%" PRId32, op->mem.base_reg - M68K_REG_A0); |
454 | 1.39k | } |
455 | | |
456 | 2.38k | if ((is_pc || op->mem.base_reg != M68K_REG_INVALID) && |
457 | 1.83k | op->mem.index_reg != M68K_REG_INVALID) |
458 | 1.39k | SStream_concat(O, ",%s", s_spacing); |
459 | | |
460 | 2.38k | if (op->mem.index_reg != M68K_REG_INVALID) { |
461 | 1.74k | printIndexReg(O, op); |
462 | 1.74k | printScaleFactor(O, op->mem.scale, 0); |
463 | 1.74k | } |
464 | | |
465 | 2.38k | SStream_concat0(O, ")"); |
466 | 2.38k | } |
467 | | |
468 | | static void printMemIndirect(SStream *O, uint32_t pc, const cs_m68k_op *op) |
469 | 4.11k | { |
470 | 4.11k | int is_pc = (op->address_mode == M68K_AM_PC_MEMI_POST_INDEX || |
471 | 3.71k | op->address_mode == M68K_AM_PC_MEMI_PRE_INDEX); |
472 | 4.11k | int is_post = (op->address_mode == M68K_AM_MEMI_POST_INDEX || |
473 | 2.35k | op->address_mode == M68K_AM_PC_MEMI_POST_INDEX); |
474 | 4.11k | int is_pre = (op->address_mode == M68K_AM_MEMI_PRE_INDEX || |
475 | 2.63k | op->address_mode == M68K_AM_PC_MEMI_PRE_INDEX); |
476 | | |
477 | 4.11k | SStream_concat0(O, "(["); |
478 | | |
479 | 4.11k | if (is_pc) { |
480 | 863 | SStream_concat(O, "$%" PRIx32, pc + 2 + op->mem.in_disp); |
481 | 3.25k | } else if (op->mem.in_disp != 0) { |
482 | 1.57k | SStream_concat(O, "%s$%" PRIx32, |
483 | 1.57k | op->mem.in_disp >= 0 ? "" : "-", |
484 | 1.57k | abs(op->mem.in_disp)); |
485 | 1.57k | } |
486 | | |
487 | 4.11k | if (op->mem.base_reg != M68K_REG_INVALID) { |
488 | 2.53k | if (op->mem.in_disp != 0) |
489 | 1.07k | SStream_concat(O, ",%s%s", s_spacing, |
490 | 1.07k | getRegName(op->mem.base_reg)); |
491 | 1.46k | else |
492 | 1.46k | SStream_concat(O, "%s", getRegName(op->mem.base_reg)); |
493 | 2.53k | } |
494 | | |
495 | 4.11k | if (is_post) |
496 | 2.17k | SStream_concat0(O, "]"); |
497 | | |
498 | 4.11k | if (op->mem.index_reg != M68K_REG_INVALID) { |
499 | 2.18k | SStream_concat(O, ",%s", s_spacing); |
500 | 2.18k | printIndexReg(O, op); |
501 | 2.18k | } |
502 | | |
503 | 4.11k | printScaleFactor(O, op->mem.scale, 0); |
504 | | |
505 | 4.11k | if (is_pre) |
506 | 1.94k | SStream_concat0(O, "]"); |
507 | | |
508 | 4.11k | if (op->mem.out_disp != 0) { |
509 | 1.93k | SStream_concat(O, ",%s%s$%" PRIx32, s_spacing, |
510 | 1.93k | op->mem.out_disp >= 0 ? "" : "-", |
511 | 1.93k | abs(op->mem.out_disp)); |
512 | 1.93k | } |
513 | | |
514 | 4.11k | SStream_concat0(O, ")"); |
515 | 4.11k | } |
516 | | |
517 | | static void printAddressingMode(SStream *O, uint32_t pc, const cs_m68k *inst, |
518 | | const cs_m68k_op *op) |
519 | 380k | { |
520 | 380k | switch (op->address_mode) { |
521 | 47.4k | case M68K_AM_NONE: |
522 | 47.4k | switch (op->type) { |
523 | 6.00k | case M68K_OP_REG_BITS: |
524 | 6.00k | registerBits(O, op); |
525 | 6.00k | break; |
526 | 4.81k | case M68K_OP_REG_PAIR: |
527 | 4.81k | registerPair(O, op); |
528 | 4.81k | break; |
529 | 36.5k | case M68K_OP_REG: |
530 | 36.5k | printRegisterName(O, op); |
531 | 36.5k | break; |
532 | 0 | case M68K_OP_SHIFT: |
533 | 0 | if (op->flags & M68K_OP_FLAG_SHIFT_LEFT) |
534 | 0 | SStream_concat0(O, "<<"); |
535 | 0 | else if (op->flags & M68K_OP_FLAG_SHIFT_RIGHT) |
536 | 0 | SStream_concat0(O, ">>"); |
537 | 0 | break; |
538 | 0 | default: |
539 | 0 | break; |
540 | 47.4k | } |
541 | 47.4k | break; |
542 | | |
543 | 125k | case M68K_AM_REG_DIRECT_DATA: |
544 | 140k | case M68K_AM_REG_DIRECT_ADDR: |
545 | 164k | case M68K_AM_REGI_ADDR: |
546 | 189k | case M68K_AM_REGI_ADDR_POST_INC: |
547 | 224k | case M68K_AM_REGI_ADDR_PRE_DEC: |
548 | 237k | case M68K_AM_REGI_ADDR_DISP: |
549 | 239k | case M68K_AM_PCI_DISP: |
550 | 239k | printRegAddrMode(O, pc, op); |
551 | 239k | break; |
552 | 2.06k | case M68K_AM_ABSOLUTE_DATA_SHORT: |
553 | 2.06k | SStream_concat(O, "$%" PRIx32 ".w", (uint32_t)op->mem.address); |
554 | 2.06k | break; |
555 | 1.65k | case M68K_AM_ABSOLUTE_DATA_LONG: |
556 | 1.65k | SStream_concat(O, "$%" PRIx64 ".l", (uint64_t)op->mem.address); |
557 | 1.65k | break; |
558 | 56.4k | case M68K_AM_IMMEDIATE: |
559 | 56.4k | printImmediate(O, inst, op); |
560 | 56.4k | break; |
561 | 1.25k | case M68K_AM_PCI_INDEX_8_BIT_DISP: |
562 | 10.4k | case M68K_AM_AREGI_INDEX_8_BIT_DISP: |
563 | 10.4k | printIndex8BitDisp(O, pc, op); |
564 | 10.4k | break; |
565 | 441 | case M68K_AM_PCI_INDEX_BASE_DISP: |
566 | 2.38k | case M68K_AM_AREGI_INDEX_BASE_DISP: |
567 | 2.38k | printBaseDisp(O, pc, op); |
568 | 2.38k | break; |
569 | 406 | case M68K_AM_PC_MEMI_POST_INDEX: |
570 | 863 | case M68K_AM_PC_MEMI_PRE_INDEX: |
571 | 2.35k | case M68K_AM_MEMI_PRE_INDEX: |
572 | 4.11k | case M68K_AM_MEMI_POST_INDEX: |
573 | 4.11k | printMemIndirect(O, pc, op); |
574 | 4.11k | break; |
575 | 17.0k | case M68K_AM_BRANCH_DISPLACEMENT: |
576 | 17.0k | SStream_concat(O, "$%" PRIx32, pc + 2 + op->br_disp.disp); |
577 | 17.0k | default: |
578 | 17.0k | break; |
579 | 380k | } |
580 | | |
581 | 380k | printBitfield(O, op); |
582 | 380k | if (op->flags & M68K_OP_FLAG_MEM_UPDATE) |
583 | 0 | SStream_concat0(O, "&"); |
584 | 380k | } |
585 | | |
586 | | static void printCAS2(SStream *O, uint32_t pc, const cs_m68k *ext) |
587 | 1.34k | { |
588 | 1.34k | printAddressingMode(O, pc, ext, &ext->operands[0]); |
589 | 1.34k | SStream_concat0(O, ","); |
590 | 1.34k | printAddressingMode(O, pc, ext, &ext->operands[1]); |
591 | 1.34k | SStream_concat0(O, ","); |
592 | | |
593 | 1.34k | SStream_concat(O, "(%s):(%s)", |
594 | 1.34k | s_reg_names[ext->operands[2].reg_pair.reg_0], |
595 | 1.34k | s_reg_names[ext->operands[2].reg_pair.reg_1]); |
596 | 1.34k | } |
597 | | |
598 | | static void printCacheOp(SStream *O, uint32_t pc, const cs_m68k *ext) |
599 | 890 | { |
600 | 890 | static const char *const cache_names[] = { "nc", "dc", "ic", "bc" }; |
601 | 890 | unsigned int sel = (unsigned int)ext->operands[0].imm; |
602 | 890 | int i; |
603 | | |
604 | 890 | if (sel < ARR_SIZE(cache_names)) |
605 | 890 | SStream_concat0(O, cache_names[sel]); |
606 | 0 | else |
607 | 0 | SStream_concat(O, "#$%" PRIx64, ext->operands[0].imm); |
608 | | |
609 | 1.53k | for (i = 1; i < ext->op_count; ++i) { |
610 | 647 | SStream_concat(O, ",%s", s_spacing); |
611 | 647 | printAddressingMode(O, pc, ext, &ext->operands[i]); |
612 | 647 | } |
613 | 890 | } |
614 | | |
615 | | static void printPackedMoveDestination(SStream *O, uint32_t pc, |
616 | | const cs_m68k *ext) |
617 | 1.04k | { |
618 | 1.04k | const cs_m68k_op *k_factor = &ext->operands[2]; |
619 | | |
620 | 1.04k | printAddressingMode(O, pc, ext, &ext->operands[0]); |
621 | 1.04k | SStream_concat(O, ",%s", s_spacing); |
622 | 1.04k | printAddressingMode(O, pc, ext, &ext->operands[1]); |
623 | 1.04k | SStream_concat0(O, "{"); |
624 | 1.04k | if (k_factor->type == M68K_OP_IMM) |
625 | 689 | SStream_concat(O, "#%" PRId64, (int64_t)k_factor->imm); |
626 | 358 | else |
627 | 358 | printAddressingMode(O, pc, ext, k_factor); |
628 | 1.04k | SStream_concat0(O, "}"); |
629 | 1.04k | } |
630 | | |
631 | | #endif |
632 | | |
633 | | static void printOpSize(SStream *O, const cs_m68k *ext) |
634 | 210k | { |
635 | 210k | switch (ext->op_size.type) { |
636 | 0 | case M68K_SIZE_TYPE_INVALID: |
637 | 0 | break; |
638 | 200k | case M68K_SIZE_TYPE_CPU: |
639 | 200k | switch (ext->op_size.cpu_size) { |
640 | 64.5k | case M68K_CPU_SIZE_BYTE: |
641 | 64.5k | SStream_concat0(O, ".b"); |
642 | 64.5k | break; |
643 | 49.0k | case M68K_CPU_SIZE_WORD: |
644 | 49.0k | SStream_concat0(O, ".w"); |
645 | 49.0k | break; |
646 | 51.4k | case M68K_CPU_SIZE_LONG: |
647 | 51.4k | SStream_concat0(O, ".l"); |
648 | 51.4k | break; |
649 | 35.3k | case M68K_CPU_SIZE_NONE: |
650 | 35.3k | break; |
651 | 200k | } |
652 | 200k | break; |
653 | 200k | case M68K_SIZE_TYPE_FPU: |
654 | 9.61k | switch (ext->op_size.fpu_size) { |
655 | 702 | case M68K_FPU_SIZE_SINGLE: |
656 | 702 | SStream_concat0(O, ".s"); |
657 | 702 | break; |
658 | 1.73k | case M68K_FPU_SIZE_DOUBLE: |
659 | 1.73k | SStream_concat0(O, ".d"); |
660 | 1.73k | break; |
661 | 3.87k | case M68K_FPU_SIZE_EXTENDED: |
662 | 3.87k | SStream_concat0(O, ".x"); |
663 | 3.87k | break; |
664 | 3.30k | case M68K_FPU_SIZE_PACKED: |
665 | 3.30k | SStream_concat0(O, ".p"); |
666 | 3.30k | break; |
667 | 0 | case M68K_FPU_SIZE_NONE: |
668 | 0 | break; |
669 | 9.61k | } |
670 | 9.61k | break; |
671 | 210k | } |
672 | 210k | } |
673 | | |
674 | | void M68K_printInst(MCInst *MI, SStream *O, void *PrinterInfo) |
675 | 259k | { |
676 | 259k | #ifndef CAPSTONE_DIET |
677 | 259k | m68k_info *info = (m68k_info *)PrinterInfo; |
678 | 259k | cs_m68k *ext = &info->extension; |
679 | 259k | cs_detail *detail = NULL; |
680 | 259k | int i = 0; |
681 | | |
682 | 259k | if (detail_is_set(MI)) { |
683 | 259k | detail = get_detail(MI); |
684 | 259k | int regs_read_count = MIN((int)ARR_SIZE(detail->regs_read), |
685 | 259k | info->regs_read_count); |
686 | 259k | int regs_write_count = MIN((int)ARR_SIZE(detail->regs_write), |
687 | 259k | info->regs_write_count); |
688 | 259k | int groups_count = |
689 | 259k | MIN((int)ARR_SIZE(detail->groups), info->groups_count); |
690 | | |
691 | 259k | memcpy(&detail->m68k, ext, sizeof(cs_m68k)); |
692 | 259k | memcpy(&detail->regs_read, &info->regs_read, |
693 | 259k | regs_read_count * sizeof(info->regs_read[0])); |
694 | 259k | detail->regs_read_count = regs_read_count; |
695 | | |
696 | 259k | memcpy(&detail->regs_write, &info->regs_write, |
697 | 259k | regs_write_count * sizeof(info->regs_write[0])); |
698 | 259k | detail->regs_write_count = regs_write_count; |
699 | | |
700 | 259k | memcpy(&detail->groups, &info->groups, groups_count); |
701 | 259k | detail->groups_count = groups_count; |
702 | 259k | } |
703 | | |
704 | 259k | if (MI->Opcode == M68K_INS_INVALID) { |
705 | 49.0k | if (ext->op_count) |
706 | 49.0k | SStream_concat(O, "dc.w $%" PRIx32, |
707 | 49.0k | (uint32_t)ext->operands[0].imm); |
708 | 0 | else |
709 | 0 | SStream_concat(O, "dc.w $<unknown>"); |
710 | 49.0k | return; |
711 | 49.0k | } |
712 | | |
713 | 210k | SStream_concat0(O, (char *)s_instruction_names[MI->Opcode]); |
714 | 210k | printOpSize(O, ext); |
715 | 210k | SStream_concat0(O, " "); |
716 | | |
717 | 210k | if (MI->Opcode == M68K_INS_CAS2) { |
718 | 1.34k | printCAS2(O, info->pc, ext); |
719 | 1.34k | return; |
720 | 1.34k | } |
721 | | |
722 | 208k | if (MI->Opcode >= M68K_INS_CINVL && MI->Opcode <= M68K_INS_CPUSHA) { |
723 | 890 | printCacheOp(O, info->pc, ext); |
724 | 890 | return; |
725 | 890 | } |
726 | | |
727 | 207k | if (MI->Opcode == M68K_INS_FMOVE && |
728 | 6.24k | ext->op_size.type == M68K_SIZE_TYPE_FPU && |
729 | 4.73k | ext->op_size.fpu_size == M68K_FPU_SIZE_PACKED && |
730 | 2.06k | ext->op_count == 3) { |
731 | 1.04k | printPackedMoveDestination(O, info->pc, ext); |
732 | 1.04k | return; |
733 | 1.04k | } |
734 | | |
735 | 581k | for (i = 0; i < ext->op_count; ++i) { |
736 | 375k | printAddressingMode(O, info->pc, ext, &ext->operands[i]); |
737 | 375k | if ((i + 1) != ext->op_count) |
738 | 170k | SStream_concat(O, ",%s", s_spacing); |
739 | 375k | } |
740 | 206k | #endif |
741 | 206k | } |
742 | | |
743 | | const char *M68K_reg_name(csh handle, unsigned int reg) |
744 | 591k | { |
745 | | #ifdef CAPSTONE_DIET |
746 | | return NULL; |
747 | | #else |
748 | 591k | if (reg >= ARR_SIZE(s_reg_names)) { |
749 | 0 | return NULL; |
750 | 0 | } |
751 | 591k | return s_reg_names[(int)reg]; |
752 | 591k | #endif |
753 | 591k | } |
754 | | |
755 | | void M68K_get_insn_id(cs_struct *h, cs_insn *insn, unsigned int id) |
756 | 450k | { |
757 | 450k | insn->id = id; // These id's matches for 68k |
758 | 450k | } |
759 | | |
760 | | const char *M68K_insn_name(csh handle, unsigned int id) |
761 | 259k | { |
762 | | #ifdef CAPSTONE_DIET |
763 | | return NULL; |
764 | | #else |
765 | 259k | if (id >= ARR_SIZE(s_instruction_names)) { |
766 | 0 | return NULL; |
767 | 0 | } |
768 | 259k | return s_instruction_names[id]; |
769 | 259k | #endif |
770 | 259k | } |
771 | | |
772 | | #ifndef CAPSTONE_DIET |
773 | | static const name_map group_name_maps[] = { |
774 | | { M68K_GRP_INVALID, NULL }, |
775 | | { M68K_GRP_JUMP, "jump" }, |
776 | | { M68K_GRP_RET, "ret" }, |
777 | | { M68K_GRP_IRET, "iret" }, |
778 | | { M68K_GRP_BRANCH_RELATIVE, "branch_relative" }, |
779 | | }; |
780 | | #endif |
781 | | |
782 | | const char *M68K_group_name(csh handle, unsigned int id) |
783 | 72.0k | { |
784 | 72.0k | #ifndef CAPSTONE_DIET |
785 | 72.0k | return id2name(group_name_maps, ARR_SIZE(group_name_maps), id); |
786 | | #else |
787 | | return NULL; |
788 | | #endif |
789 | 72.0k | } |
790 | | |
791 | | #ifndef CAPSTONE_DIET |
792 | | void M68K_reg_access(const cs_insn *insn, cs_regs regs_read, |
793 | | uint8_t *regs_read_count, cs_regs regs_write, |
794 | | uint8_t *regs_write_count) |
795 | 0 | { |
796 | 0 | uint8_t read_count, write_count; |
797 | |
|
798 | 0 | read_count = insn->detail->regs_read_count; |
799 | 0 | write_count = insn->detail->regs_write_count; |
800 | | |
801 | | // implicit registers |
802 | 0 | memcpy(regs_read, insn->detail->regs_read, |
803 | 0 | read_count * sizeof(insn->detail->regs_read[0])); |
804 | 0 | memcpy(regs_write, insn->detail->regs_write, |
805 | 0 | write_count * sizeof(insn->detail->regs_write[0])); |
806 | |
|
807 | 0 | *regs_read_count = read_count; |
808 | 0 | *regs_write_count = write_count; |
809 | 0 | } |
810 | | #endif |