testsuite: skip confirmation in 'gdb_reinitialize_dir'
[binutils-gdb.git] / opcodes / m68k-dis.c
blob7e9b087951e2c3e6533b613b7601cfd793d65a8a
1 /* Print Motorola 68k instructions.
2 Copyright (C) 1986-2024 Free Software Foundation, Inc.
4 This file is part of the GNU opcodes library.
6 This library is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
11 It is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
21 #include "sysdep.h"
22 #include "disassemble.h"
23 #include "floatformat.h"
24 #include "libiberty.h"
25 #include "opintl.h"
26 #include "cpu-m68k.h"
27 #include "opcode/m68k.h"
29 /* Local function prototypes. */
31 const char * const fpcr_names[] =
33 "", "%fpiar", "%fpsr", "%fpiar/%fpsr", "%fpcr",
34 "%fpiar/%fpcr", "%fpsr/%fpcr", "%fpiar/%fpsr/%fpcr"
37 static char *const reg_names[] =
39 "%d0", "%d1", "%d2", "%d3", "%d4", "%d5", "%d6", "%d7",
40 "%a0", "%a1", "%a2", "%a3", "%a4", "%a5", "%fp", "%sp",
41 "%ps", "%pc"
44 /* Name of register halves for MAC/EMAC.
45 Seperate from reg_names since 'spu', 'fpl' look weird. */
46 static char *const reg_half_names[] =
48 "%d0", "%d1", "%d2", "%d3", "%d4", "%d5", "%d6", "%d7",
49 "%a0", "%a1", "%a2", "%a3", "%a4", "%a5", "%a6", "%a7",
50 "%ps", "%pc"
53 /* Sign-extend an (unsigned char). */
54 #if __STDC__ == 1
55 #define COERCE_SIGNED_CHAR(ch) ((signed char) (ch))
56 #else
57 #define COERCE_SIGNED_CHAR(ch) ((int) (((ch) ^ 0x80) & 0xFF) - 128)
58 #endif
60 /* Error code of print_insn_arg's return value. */
62 enum print_insn_arg_error
64 /* An invalid operand is found. */
65 PRINT_INSN_ARG_INVALID_OPERAND = -1,
67 /* An opcode table error. */
68 PRINT_INSN_ARG_INVALID_OP_TABLE = -2,
70 /* A memory error. */
71 PRINT_INSN_ARG_MEMORY_ERROR = -3,
74 /* Get a 1 byte signed integer. */
75 #define NEXTBYTE(p, val) \
76 do \
77 { \
78 p += 2; \
79 if (!FETCH_DATA (info, p)) \
80 return PRINT_INSN_ARG_MEMORY_ERROR; \
81 val = COERCE_SIGNED_CHAR (p[-1]); \
82 } \
83 while (0)
85 /* Get a 2 byte signed integer. */
86 #define COERCE16(x) ((int) (((x) ^ 0x8000) - 0x8000))
88 #define NEXTWORD(p, val, ret_val) \
89 do \
90 { \
91 p += 2; \
92 if (!FETCH_DATA (info, p)) \
93 return ret_val; \
94 val = COERCE16 ((p[-2] << 8) + p[-1]); \
95 } \
96 while (0)
98 /* Get a 4 byte signed integer. */
99 #define COERCE32(x) (((bfd_vma) (x) ^ 0x80000000) - 0x80000000)
101 #define NEXTLONG(p, val, ret_val) \
102 do \
104 p += 4; \
105 if (!FETCH_DATA (info, p)) \
106 return ret_val; \
107 val = COERCE32 (((((((unsigned) p[-4] << 8) + p[-3]) << 8) \
108 + p[-2]) << 8) + p[-1]); \
110 while (0)
112 /* Get a 4 byte unsigned integer. */
113 #define NEXTULONG(p, val) \
114 do \
116 p += 4; \
117 if (!FETCH_DATA (info, p)) \
118 return PRINT_INSN_ARG_MEMORY_ERROR; \
119 val = (((((((unsigned) p[-4] << 8) + p[-3]) << 8) \
120 + p[-2]) << 8) + p[-1]); \
122 while (0)
124 /* Get a single precision float. */
125 #define NEXTSINGLE(val, p) \
126 do \
128 p += 4; \
129 if (!FETCH_DATA (info, p)) \
130 return PRINT_INSN_ARG_MEMORY_ERROR; \
131 floatformat_to_double (& floatformat_ieee_single_big, \
132 (char *) p - 4, & val); \
134 while (0)
136 /* Get a double precision float. */
137 #define NEXTDOUBLE(val, p) \
138 do \
140 p += 8; \
141 if (!FETCH_DATA (info, p)) \
142 return PRINT_INSN_ARG_MEMORY_ERROR; \
143 floatformat_to_double (& floatformat_ieee_double_big, \
144 (char *) p - 8, & val); \
146 while (0)
148 /* Get an extended precision float. */
149 #define NEXTEXTEND(val, p) \
150 do \
152 p += 12; \
153 if (!FETCH_DATA (info, p)) \
154 return PRINT_INSN_ARG_MEMORY_ERROR; \
155 floatformat_to_double (& floatformat_m68881_ext, \
156 (char *) p - 12, & val); \
158 while (0)
160 /* Need a function to convert from packed to double
161 precision. Actually, it's easier to print a
162 packed number than a double anyway, so maybe
163 there should be a special case to handle this... */
164 #define NEXTPACKED(p, val) \
165 do \
167 p += 12; \
168 if (!FETCH_DATA (info, p)) \
169 return PRINT_INSN_ARG_MEMORY_ERROR; \
170 val = 0.0; \
172 while (0)
175 /* Maximum length of an instruction. */
176 #define MAXLEN 22
178 struct private
180 /* Points to first byte not fetched. */
181 bfd_byte *max_fetched;
182 bfd_byte the_buffer[MAXLEN];
183 bfd_vma insn_start;
186 /* Make sure that bytes from INFO->PRIVATE_DATA->BUFFER (inclusive)
187 to ADDR (exclusive) are valid. Returns 1 for success, 0 on memory
188 error. */
189 #define FETCH_DATA(info, addr) \
190 ((addr) <= ((struct private *) (info->private_data))->max_fetched \
191 ? 1 : fetch_data ((info), (addr)))
193 static int
194 fetch_data (struct disassemble_info *info, bfd_byte *addr)
196 int status;
197 struct private *priv = (struct private *)info->private_data;
198 bfd_vma start = priv->insn_start + (priv->max_fetched - priv->the_buffer);
200 status = (*info->read_memory_func) (start,
201 priv->max_fetched,
202 addr - priv->max_fetched,
203 info);
204 if (status != 0)
206 (*info->memory_error_func) (status, start, info);
207 return 0;
209 else
210 priv->max_fetched = addr;
211 return 1;
214 /* This function is used to print to the bit-bucket. */
215 static int
216 dummy_printer (void *file ATTRIBUTE_UNUSED,
217 enum disassembler_style style ATTRIBUTE_UNUSED,
218 const char *format ATTRIBUTE_UNUSED,
219 ...)
221 return 0;
224 static void
225 dummy_print_address (bfd_vma vma ATTRIBUTE_UNUSED,
226 struct disassemble_info *info ATTRIBUTE_UNUSED)
230 /* Fetch BITS bits from a position in the instruction specified by CODE.
231 CODE is a "place to put an argument", or 'x' for a destination
232 that is a general address (mode and register).
233 BUFFER contains the instruction.
234 Returns -1 on failure. */
236 static int
237 fetch_arg (unsigned char *buffer,
238 int code,
239 int bits,
240 disassemble_info *info)
242 int val = 0;
244 switch (code)
246 case '/': /* MAC/EMAC mask bit. */
247 val = buffer[3] >> 5;
248 break;
250 case 'G': /* EMAC ACC load. */
251 val = ((buffer[3] >> 3) & 0x2) | ((~buffer[1] >> 7) & 0x1);
252 break;
254 case 'H': /* EMAC ACC !load. */
255 val = ((buffer[3] >> 3) & 0x2) | ((buffer[1] >> 7) & 0x1);
256 break;
258 case ']': /* EMAC ACCEXT bit. */
259 val = buffer[0] >> 2;
260 break;
262 case 'I': /* MAC/EMAC scale factor. */
263 val = buffer[2] >> 1;
264 break;
266 case 'F': /* EMAC ACCx. */
267 val = buffer[0] >> 1;
268 break;
270 case 'f':
271 val = buffer[1];
272 break;
274 case 's':
275 val = buffer[1];
276 break;
278 case 'd': /* Destination, for register or quick. */
279 val = (buffer[0] << 8) + buffer[1];
280 val >>= 9;
281 break;
283 case 'x': /* Destination, for general arg. */
284 val = (buffer[0] << 8) + buffer[1];
285 val >>= 6;
286 break;
288 case 'k':
289 if (! FETCH_DATA (info, buffer + 3))
290 return -1;
291 val = (buffer[3] >> 4);
292 break;
294 case 'C':
295 if (! FETCH_DATA (info, buffer + 3))
296 return -1;
297 val = buffer[3];
298 break;
300 case '1':
301 if (! FETCH_DATA (info, buffer + 3))
302 return -1;
303 val = (buffer[2] << 8) + buffer[3];
304 val >>= 12;
305 break;
307 case '2':
308 if (! FETCH_DATA (info, buffer + 3))
309 return -1;
310 val = (buffer[2] << 8) + buffer[3];
311 val >>= 6;
312 break;
314 case '3':
315 case 'j':
316 if (! FETCH_DATA (info, buffer + 3))
317 return -1;
318 val = (buffer[2] << 8) + buffer[3];
319 break;
321 case '4':
322 if (! FETCH_DATA (info, buffer + 5))
323 return -1;
324 val = (buffer[4] << 8) + buffer[5];
325 val >>= 12;
326 break;
328 case '5':
329 if (! FETCH_DATA (info, buffer + 5))
330 return -1;
331 val = (buffer[4] << 8) + buffer[5];
332 val >>= 6;
333 break;
335 case '6':
336 if (! FETCH_DATA (info, buffer + 5))
337 return -1;
338 val = (buffer[4] << 8) + buffer[5];
339 break;
341 case '7':
342 if (! FETCH_DATA (info, buffer + 3))
343 return -1;
344 val = (buffer[2] << 8) + buffer[3];
345 val >>= 7;
346 break;
348 case '8':
349 if (! FETCH_DATA (info, buffer + 3))
350 return -1;
351 val = (buffer[2] << 8) + buffer[3];
352 val >>= 10;
353 break;
355 case '9':
356 if (! FETCH_DATA (info, buffer + 3))
357 return -1;
358 val = (buffer[2] << 8) + buffer[3];
359 val >>= 5;
360 break;
362 case 'e':
363 val = (buffer[1] >> 6);
364 break;
366 case 'E':
367 if (! FETCH_DATA (info, buffer + 3))
368 return -1;
369 val = (buffer[2] >> 1);
370 break;
372 case 'm':
373 val = (buffer[1] & 0x40 ? 0x8 : 0)
374 | ((buffer[0] >> 1) & 0x7)
375 | (buffer[3] & 0x80 ? 0x10 : 0);
376 break;
378 case 'n':
379 val = (buffer[1] & 0x40 ? 0x8 : 0) | ((buffer[0] >> 1) & 0x7);
380 break;
382 case 'o':
383 val = (buffer[2] >> 4) | (buffer[3] & 0x80 ? 0x10 : 0);
384 break;
386 case 'M':
387 val = (buffer[1] & 0xf) | (buffer[3] & 0x40 ? 0x10 : 0);
388 break;
390 case 'N':
391 val = (buffer[3] & 0xf) | (buffer[3] & 0x40 ? 0x10 : 0);
392 break;
394 case 'h':
395 val = buffer[2] >> 2;
396 break;
398 default:
399 abort ();
402 /* bits is never too big. */
403 return val & ((1 << bits) - 1);
406 /* Check if an EA is valid for a particular code. This is required
407 for the EMAC instructions since the type of source address determines
408 if it is a EMAC-load instruciton if the EA is mode 2-5, otherwise it
409 is a non-load EMAC instruction and the bits mean register Ry.
410 A similar case exists for the movem instructions where the register
411 mask is interpreted differently for different EAs. */
413 static bool
414 m68k_valid_ea (char code, int val)
416 int mode, mask;
417 #define M(n0,n1,n2,n3,n4,n5,n6,n70,n71,n72,n73,n74) \
418 (n0 | n1 << 1 | n2 << 2 | n3 << 3 | n4 << 4 | n5 << 5 | n6 << 6 \
419 | n70 << 7 | n71 << 8 | n72 << 9 | n73 << 10 | n74 << 11)
421 switch (code)
423 case '*':
424 mask = M (1,1,1,1,1,1,1,1,1,1,1,1);
425 break;
426 case '~':
427 mask = M (0,0,1,1,1,1,1,1,1,0,0,0);
428 break;
429 case '%':
430 mask = M (1,1,1,1,1,1,1,1,1,0,0,0);
431 break;
432 case ';':
433 mask = M (1,0,1,1,1,1,1,1,1,1,1,1);
434 break;
435 case '@':
436 mask = M (1,0,1,1,1,1,1,1,1,1,1,0);
437 break;
438 case '!':
439 mask = M (0,0,1,0,0,1,1,1,1,1,1,0);
440 break;
441 case '&':
442 mask = M (0,0,1,0,0,1,1,1,1,0,0,0);
443 break;
444 case '$':
445 mask = M (1,0,1,1,1,1,1,1,1,0,0,0);
446 break;
447 case '?':
448 mask = M (1,0,1,0,0,1,1,1,1,0,0,0);
449 break;
450 case '/':
451 mask = M (1,0,1,0,0,1,1,1,1,1,1,0);
452 break;
453 case '|':
454 mask = M (0,0,1,0,0,1,1,1,1,1,1,0);
455 break;
456 case '>':
457 mask = M (0,0,1,0,1,1,1,1,1,0,0,0);
458 break;
459 case '<':
460 mask = M (0,0,1,1,0,1,1,1,1,1,1,0);
461 break;
462 case 'm':
463 mask = M (1,1,1,1,1,0,0,0,0,0,0,0);
464 break;
465 case 'n':
466 mask = M (0,0,0,0,0,1,0,0,0,1,0,0);
467 break;
468 case 'o':
469 mask = M (0,0,0,0,0,0,1,1,1,0,1,1);
470 break;
471 case 'p':
472 mask = M (1,1,1,1,1,1,0,0,0,0,0,0);
473 break;
474 case 'q':
475 mask = M (1,0,1,1,1,1,0,0,0,0,0,0);
476 break;
477 case 'v':
478 mask = M (1,0,1,1,1,1,0,1,1,0,0,0);
479 break;
480 case 'b':
481 mask = M (1,0,1,1,1,1,0,0,0,1,0,0);
482 break;
483 case 'w':
484 mask = M (0,0,1,1,1,1,0,0,0,1,0,0);
485 break;
486 case 'y':
487 mask = M (0,0,1,0,0,1,0,0,0,0,0,0);
488 break;
489 case 'z':
490 mask = M (0,0,1,0,0,1,0,0,0,1,0,0);
491 break;
492 case '4':
493 mask = M (0,0,1,1,1,1,0,0,0,0,0,0);
494 break;
495 default:
496 abort ();
498 #undef M
500 mode = (val >> 3) & 7;
501 if (mode == 7)
502 mode += val & 7;
503 return (mask & (1 << mode)) != 0;
506 /* Print a base register REGNO and displacement DISP, on INFO->STREAM.
507 REGNO = -1 for pc, -2 for none (suppressed). */
509 static void
510 print_base (int regno, bfd_vma disp, disassemble_info *info)
512 if (regno == -1)
514 (*info->fprintf_styled_func) (info->stream, dis_style_register, "%%pc");
515 (*info->fprintf_styled_func) (info->stream, dis_style_text, "@(");
516 (*info->print_address_func) (disp, info);
518 else
520 if (regno == -3)
521 (*info->fprintf_styled_func) (info->stream, dis_style_register,
522 "%%zpc");
523 else if (regno != -2)
524 (*info->fprintf_styled_func) (info->stream, dis_style_register,
525 "%s", reg_names[regno]);
526 (*info->fprintf_styled_func) (info->stream, dis_style_text, "@(");
527 (*info->fprintf_styled_func) (info->stream, dis_style_immediate,
528 "%" PRIx64, (uint64_t) disp);
532 /* Print the index register of an indexed argument, as encoded in the
533 extension word. */
535 static void
536 print_index_register (int ext, disassemble_info *info)
538 (*info->fprintf_styled_func) (info->stream, dis_style_register,
539 "%s", reg_names[(ext >> 12) & 0xf]);
540 (*info->fprintf_styled_func) (info->stream, dis_style_text,
541 ":%c", ext & 0x800 ? 'l' : 'w');
542 if ((ext >> 9) & 3)
544 (*info->fprintf_styled_func) (info->stream, dis_style_text, ":");
545 (*info->fprintf_styled_func) (info->stream, dis_style_immediate,
546 "%d", 1 << ((ext >> 9) & 3));
550 /* Print an indexed argument. The base register is BASEREG (-1 for pc).
551 P points to extension word, in buffer.
552 ADDR is the nominal core address of that extension word.
553 Returns NULL upon error. */
555 static unsigned char *
556 print_indexed (int basereg,
557 unsigned char *p,
558 bfd_vma addr,
559 disassemble_info *info)
561 int word;
562 bfd_vma base_disp;
563 bfd_vma outer_disp;
564 bool print_index = true;
566 NEXTWORD (p, word, NULL);
568 /* Handle the 68000 style of indexing. */
570 if ((word & 0x100) == 0)
572 base_disp = word & 0xff;
573 if ((base_disp & 0x80) != 0)
574 base_disp -= 0x100;
575 if (basereg == -1)
576 base_disp += addr;
577 print_base (basereg, base_disp, info);
578 (*info->fprintf_styled_func) (info->stream, dis_style_text, ",");
579 print_index_register (word, info);
580 (*info->fprintf_styled_func) (info->stream, dis_style_text, ")");
581 return p;
584 /* Handle the generalized kind. */
585 /* First, compute the displacement to add to the base register. */
586 if (word & 0200)
588 if (basereg == -1)
589 basereg = -3;
590 else
591 basereg = -2;
593 if (word & 0100)
594 print_index = false;
595 base_disp = 0;
596 switch ((word >> 4) & 3)
598 case 2:
599 NEXTWORD (p, base_disp, NULL);
600 break;
601 case 3:
602 NEXTLONG (p, base_disp, NULL);
604 if (basereg == -1)
605 base_disp += addr;
607 /* Handle single-level case (not indirect). */
608 if ((word & 7) == 0)
610 print_base (basereg, base_disp, info);
611 if (print_index)
613 (*info->fprintf_styled_func) (info->stream, dis_style_text, ",");
614 print_index_register (word, info);
616 (*info->fprintf_styled_func) (info->stream, dis_style_text, ")");
617 return p;
620 /* Two level. Compute displacement to add after indirection. */
621 outer_disp = 0;
622 switch (word & 3)
624 case 2:
625 NEXTWORD (p, outer_disp, NULL);
626 break;
627 case 3:
628 NEXTLONG (p, outer_disp, NULL);
631 print_base (basereg, base_disp, info);
632 if ((word & 4) == 0 && print_index)
634 (*info->fprintf_styled_func) (info->stream, dis_style_text, ",");
635 print_index_register (word, info);
636 print_index = false;
638 (*info->fprintf_styled_func) (info->stream, dis_style_text,
639 ")@(");
640 (*info->fprintf_styled_func) (info->stream, dis_style_address_offset,
641 "%" PRIx64, (uint64_t) outer_disp);
642 if (print_index)
644 (*info->fprintf_styled_func) (info->stream, dis_style_text, ",");
645 print_index_register (word, info);
647 (*info->fprintf_styled_func) (info->stream, dis_style_text, ")");
649 return p;
652 #define FETCH_ARG(size, val) \
653 do \
655 val = fetch_arg (buffer, place, size, info); \
656 if (val < 0) \
657 return PRINT_INSN_ARG_MEMORY_ERROR; \
659 while (0)
661 /* Returns number of bytes "eaten" by the operand, or
662 return enum print_insn_arg_error. ADDR is the pc for this arg to be
663 relative to. */
665 static int
666 print_insn_arg (const char *d,
667 unsigned char *buffer,
668 unsigned char *p0,
669 bfd_vma addr,
670 disassemble_info *info)
672 int val = 0;
673 int place = d[1];
674 unsigned char *p = p0;
675 int regno;
676 const char *regname;
677 unsigned char *p1;
678 double flval;
679 int flt_p;
680 bfd_signed_vma disp;
681 unsigned int uval;
683 switch (*d)
685 case 'c': /* Cache identifier. */
687 static char *const cacheFieldName[] = { "nc", "dc", "ic", "bc" };
688 FETCH_ARG (2, val);
689 (*info->fprintf_styled_func) (info->stream, dis_style_sub_mnemonic,
690 "%s", cacheFieldName[val]);
691 break;
694 case 'a': /* Address register indirect only. Cf. case '+'. */
696 FETCH_ARG (3, val);
697 (*info->fprintf_styled_func) (info->stream, dis_style_register, "%s",
698 reg_names[val + 8]);
699 (*info->fprintf_styled_func) (info->stream, dis_style_text, "@");
700 break;
703 case '_': /* 32-bit absolute address for move16. */
705 NEXTULONG (p, uval);
706 (*info->print_address_func) (uval, info);
707 break;
710 case 'C':
711 (*info->fprintf_styled_func) (info->stream, dis_style_register, "%%ccr");
712 break;
714 case 'S':
715 (*info->fprintf_styled_func) (info->stream, dis_style_register, "%%sr");
716 break;
718 case 'U':
719 (*info->fprintf_styled_func) (info->stream, dis_style_register, "%%usp");
720 break;
722 case 'E':
723 (*info->fprintf_styled_func) (info->stream, dis_style_register, "%%acc");
724 break;
726 case 'G':
727 (*info->fprintf_styled_func) (info->stream, dis_style_register, "%%macsr");
728 break;
730 case 'H':
731 (*info->fprintf_styled_func) (info->stream, dis_style_register, "%%mask");
732 break;
734 case 'J':
736 /* FIXME: There's a problem here, different m68k processors call the
737 same address different names. The tables below try to get it right
738 using info->mach, but only for v4e. */
739 struct regname { char * name; int value; };
740 static const struct regname names[] =
742 {"%sfc", 0x000}, {"%dfc", 0x001}, {"%cacr", 0x002},
743 {"%tc", 0x003}, {"%itt0",0x004}, {"%itt1", 0x005},
744 {"%dtt0",0x006}, {"%dtt1",0x007}, {"%buscr",0x008},
745 {"%rgpiobar", 0x009}, {"%acr4",0x00c},
746 {"%acr5",0x00d}, {"%acr6",0x00e}, {"%acr7", 0x00f},
747 {"%usp", 0x800}, {"%vbr", 0x801}, {"%caar", 0x802},
748 {"%msp", 0x803}, {"%isp", 0x804},
749 {"%pc", 0x80f},
750 /* Reg c04 is sometimes called flashbar or rambar.
751 Reg c05 is also sometimes called rambar. */
752 {"%rambar0", 0xc04}, {"%rambar1", 0xc05},
754 /* reg c0e is sometimes called mbar2 or secmbar.
755 reg c0f is sometimes called mbar. */
756 {"%mbar0", 0xc0e}, {"%mbar1", 0xc0f},
758 /* Should we be calling this psr like we do in case 'Y'? */
759 {"%mmusr",0x805},
761 {"%urp", 0x806}, {"%srp", 0x807}, {"%pcr", 0x808},
763 /* Fido added these. */
764 {"%cac", 0xffe}, {"%mbo", 0xfff}
766 /* Alternate names for v4e (MCF5407/5445x/MCF547x/MCF548x), at least. */
767 static const struct regname names_v4e[] =
769 {"%asid",0x003}, {"%acr0",0x004}, {"%acr1",0x005},
770 {"%acr2",0x006}, {"%acr3",0x007}, {"%mmubar",0x008},
772 unsigned int arch_mask;
774 arch_mask = bfd_m68k_mach_to_features (info->mach);
775 FETCH_ARG (12, val);
776 if (arch_mask & (mcfisa_b | mcfisa_c))
778 for (regno = ARRAY_SIZE (names_v4e); --regno >= 0;)
779 if (names_v4e[regno].value == val)
781 (*info->fprintf_styled_func) (info->stream, dis_style_register,
782 "%s", names_v4e[regno].name);
783 break;
785 if (regno >= 0)
786 break;
788 for (regno = ARRAY_SIZE (names) - 1; regno >= 0; regno--)
789 if (names[regno].value == val)
791 (*info->fprintf_styled_func) (info->stream, dis_style_register,
792 "%s", names[regno].name);
793 break;
795 if (regno < 0)
796 (*info->fprintf_styled_func) (info->stream, dis_style_text, "0x%x", val);
798 break;
800 case 'Q':
801 FETCH_ARG (3, val);
802 /* 0 means 8, except for the bkpt instruction... */
803 if (val == 0 && d[1] != 's')
804 val = 8;
805 (*info->fprintf_styled_func) (info->stream, dis_style_immediate,
806 "#%d", val);
807 break;
809 case 'x':
810 FETCH_ARG (3, val);
811 /* 0 means -1. */
812 if (val == 0)
813 val = -1;
814 (*info->fprintf_styled_func) (info->stream, dis_style_immediate,
815 "#%d", val);
816 break;
818 case 'j':
819 FETCH_ARG (3, val);
820 (*info->fprintf_styled_func) (info->stream, dis_style_immediate,
821 "#%d", val+1);
822 break;
824 case 'K':
825 FETCH_ARG (9, val);
826 (*info->fprintf_styled_func) (info->stream, dis_style_immediate,
827 "#%d", val);
828 break;
830 case 'M':
831 if (place == 'h')
833 static char *const scalefactor_name[] = { "<<", ">>" };
835 FETCH_ARG (1, val);
836 (*info->fprintf_styled_func) (info->stream, dis_style_sub_mnemonic,
837 "%s", scalefactor_name[val]);
839 else
841 FETCH_ARG (8, val);
842 if (val & 0x80)
843 val = val - 0x100;
844 (*info->fprintf_styled_func) (info->stream, dis_style_immediate,
845 "#%d", val);
847 break;
849 case 'T':
850 FETCH_ARG (4, val);
851 (*info->fprintf_styled_func) (info->stream, dis_style_immediate,
852 "#%d", val);
853 break;
855 case 'D':
856 FETCH_ARG (3, val);
857 (*info->fprintf_styled_func) (info->stream, dis_style_register,
858 "%s", reg_names[val]);
859 break;
861 case 'A':
862 FETCH_ARG (3, val);
863 (*info->fprintf_styled_func) (info->stream, dis_style_register,
864 "%s", reg_names[val + 010]);
865 break;
867 case 'R':
868 FETCH_ARG (4, val);
869 (*info->fprintf_styled_func) (info->stream, dis_style_register,
870 "%s", reg_names[val]);
871 break;
873 case 'r':
874 FETCH_ARG (4, regno);
875 if (regno > 7)
877 (*info->fprintf_styled_func) (info->stream, dis_style_register,
878 "%s", reg_names[regno]);
879 (*info->fprintf_styled_func) (info->stream, dis_style_text, "@");
881 else
883 (*info->fprintf_styled_func) (info->stream, dis_style_text, "@(");
884 (*info->fprintf_styled_func) (info->stream, dis_style_register,
885 "%s", reg_names[regno]);
886 (*info->fprintf_styled_func) (info->stream, dis_style_text, ")");
888 break;
890 case 'F':
891 FETCH_ARG (3, val);
892 (*info->fprintf_styled_func) (info->stream, dis_style_register,
893 "%%fp%d", val);
894 break;
896 case 'O':
897 FETCH_ARG (6, val);
898 if (val & 0x20)
899 (*info->fprintf_styled_func) (info->stream, dis_style_register,
900 "%s", reg_names[val & 7]);
901 else
902 (*info->fprintf_styled_func) (info->stream, dis_style_immediate,
903 "%d", val);
904 break;
906 case '+':
907 FETCH_ARG (3, val);
908 (*info->fprintf_styled_func) (info->stream, dis_style_register,
909 "%s", reg_names[val + 8]);
910 (*info->fprintf_styled_func) (info->stream, dis_style_text, "@+");
911 break;
913 case '-':
914 FETCH_ARG (3, val);
915 (*info->fprintf_styled_func) (info->stream, dis_style_register,
916 "%s", reg_names[val + 8]);
917 (*info->fprintf_styled_func) (info->stream, dis_style_text, "@-");
918 break;
920 case 'k':
921 if (place == 'k')
923 FETCH_ARG (3, val);
924 (*info->fprintf_styled_func) (info->stream, dis_style_text, "{");
925 (*info->fprintf_styled_func) (info->stream, dis_style_register,
926 "%s", reg_names[val]);
927 (*info->fprintf_styled_func) (info->stream, dis_style_text, "}");
929 else if (place == 'C')
931 FETCH_ARG (7, val);
932 if (val > 63) /* This is a signed constant. */
933 val -= 128;
934 (*info->fprintf_styled_func) (info->stream, dis_style_text, "{");
935 (*info->fprintf_styled_func) (info->stream, dis_style_immediate,
936 "#%d", val);
937 (*info->fprintf_styled_func) (info->stream, dis_style_text, "}");
939 else
940 return PRINT_INSN_ARG_INVALID_OPERAND;
941 break;
943 case '#':
944 case '^':
945 p1 = buffer + (*d == '#' ? 2 : 4);
946 if (place == 's')
947 FETCH_ARG (4, val);
948 else if (place == 'C')
949 FETCH_ARG (7, val);
950 else if (place == '8')
951 FETCH_ARG (3, val);
952 else if (place == '3')
953 FETCH_ARG (8, val);
954 else if (place == 'b')
955 NEXTBYTE (p1, val);
956 else if (place == 'w' || place == 'W')
957 NEXTWORD (p1, val, PRINT_INSN_ARG_MEMORY_ERROR);
958 else if (place == 'l')
959 NEXTLONG (p1, val, PRINT_INSN_ARG_MEMORY_ERROR);
960 else
961 return PRINT_INSN_ARG_INVALID_OP_TABLE;
963 (*info->fprintf_styled_func) (info->stream, dis_style_immediate,
964 "#%d", val);
965 break;
967 case 'B':
968 if (place == 'b')
969 NEXTBYTE (p, disp);
970 else if (place == 'B')
971 disp = COERCE_SIGNED_CHAR (buffer[1]);
972 else if (place == 'w' || place == 'W')
973 NEXTWORD (p, disp, PRINT_INSN_ARG_MEMORY_ERROR);
974 else if (place == 'l' || place == 'L' || place == 'C')
975 NEXTLONG (p, disp, PRINT_INSN_ARG_MEMORY_ERROR);
976 else if (place == 'g')
978 NEXTBYTE (buffer, disp);
979 if (disp == 0)
980 NEXTWORD (p, disp, PRINT_INSN_ARG_MEMORY_ERROR);
981 else if (disp == -1)
982 NEXTLONG (p, disp, PRINT_INSN_ARG_MEMORY_ERROR);
984 else if (place == 'c')
986 if (buffer[1] & 0x40) /* If bit six is one, long offset. */
987 NEXTLONG (p, disp, PRINT_INSN_ARG_MEMORY_ERROR);
988 else
989 NEXTWORD (p, disp, PRINT_INSN_ARG_MEMORY_ERROR);
991 else
992 return PRINT_INSN_ARG_INVALID_OP_TABLE;
994 info->target = addr + disp;
996 (*info->print_address_func) (addr + disp, info);
997 break;
999 case 'd':
1001 int val1;
1003 NEXTWORD (p, val, PRINT_INSN_ARG_MEMORY_ERROR);
1004 FETCH_ARG (3, val1);
1005 (*info->fprintf_styled_func) (info->stream, dis_style_register,
1006 "%s", reg_names[val1 + 8]);
1007 (*info->fprintf_styled_func) (info->stream, dis_style_text, "@(");
1008 (*info->fprintf_styled_func) (info->stream, dis_style_address_offset,
1009 "%d", val);
1010 (*info->fprintf_styled_func) (info->stream, dis_style_text, ")");
1011 break;
1014 case 's':
1015 FETCH_ARG (3, val);
1016 (*info->fprintf_styled_func) (info->stream, dis_style_register,
1017 "%s", fpcr_names[val]);
1018 break;
1020 case 'e':
1021 FETCH_ARG (2, val);
1022 (*info->fprintf_styled_func) (info->stream, dis_style_register,
1023 "%%acc%d", val);
1024 break;
1026 case 'g':
1027 FETCH_ARG (1, val);
1028 (*info->fprintf_styled_func) (info->stream, dis_style_register,
1029 "%%accext%s", val == 0 ? "01" : "23");
1030 break;
1032 case 'i':
1033 FETCH_ARG (2, val);
1034 if (val == 1)
1035 (*info->fprintf_styled_func) (info->stream, dis_style_sub_mnemonic,
1036 "<<");
1037 else if (val == 3)
1038 (*info->fprintf_styled_func) (info->stream, dis_style_sub_mnemonic,
1039 ">>");
1040 else
1041 return PRINT_INSN_ARG_INVALID_OPERAND;
1042 break;
1044 case 'I':
1045 /* Get coprocessor ID... */
1046 val = fetch_arg (buffer, 'd', 3, info);
1047 if (val < 0)
1048 return PRINT_INSN_ARG_MEMORY_ERROR;
1049 if (val != 1) /* Unusual coprocessor ID? */
1050 (*info->fprintf_styled_func) (info->stream, dis_style_text,
1051 "(cpid=%d) ", val);
1052 break;
1054 case '4':
1055 case '*':
1056 case '~':
1057 case '%':
1058 case ';':
1059 case '@':
1060 case '!':
1061 case '$':
1062 case '?':
1063 case '/':
1064 case '&':
1065 case '|':
1066 case '<':
1067 case '>':
1068 case 'm':
1069 case 'n':
1070 case 'o':
1071 case 'p':
1072 case 'q':
1073 case 'v':
1074 case 'b':
1075 case 'w':
1076 case 'y':
1077 case 'z':
1078 if (place == 'd')
1080 val = fetch_arg (buffer, 'x', 6, info);
1081 if (val < 0)
1082 return PRINT_INSN_ARG_MEMORY_ERROR;
1083 val = ((val & 7) << 3) + ((val >> 3) & 7);
1085 else
1087 val = fetch_arg (buffer, 's', 6, info);
1088 if (val < 0)
1089 return PRINT_INSN_ARG_MEMORY_ERROR;
1092 /* If the <ea> is invalid for *d, then reject this match. */
1093 if (!m68k_valid_ea (*d, val))
1094 return PRINT_INSN_ARG_INVALID_OPERAND;
1096 /* Get register number assuming address register. */
1097 regno = (val & 7) + 8;
1098 regname = reg_names[regno];
1099 switch (val >> 3)
1101 case 0:
1102 (*info->fprintf_styled_func) (info->stream, dis_style_register,
1103 "%s", reg_names[val]);
1104 break;
1106 case 1:
1107 (*info->fprintf_styled_func) (info->stream, dis_style_register,
1108 "%s", regname);
1109 break;
1111 case 2:
1112 (*info->fprintf_styled_func) (info->stream, dis_style_register,
1113 "%s", regname);
1114 (*info->fprintf_styled_func) (info->stream, dis_style_text, "@");
1115 break;
1117 case 3:
1118 (*info->fprintf_styled_func) (info->stream, dis_style_register,
1119 "%s", regname);
1120 (*info->fprintf_styled_func) (info->stream, dis_style_text, "@+");
1121 break;
1123 case 4:
1124 (*info->fprintf_styled_func) (info->stream, dis_style_register,
1125 "%s", regname);
1126 (*info->fprintf_styled_func) (info->stream, dis_style_text, "@-");
1127 break;
1129 case 5:
1130 NEXTWORD (p, val, PRINT_INSN_ARG_MEMORY_ERROR);
1131 (*info->fprintf_styled_func) (info->stream, dis_style_register,
1132 "%s", regname);
1133 (*info->fprintf_styled_func) (info->stream, dis_style_text, "@(");
1134 (*info->fprintf_styled_func) (info->stream, dis_style_address_offset,
1135 "%d", val);
1136 (*info->fprintf_styled_func) (info->stream, dis_style_text, ")");
1137 break;
1139 case 6:
1140 p = print_indexed (regno, p, addr, info);
1141 if (p == NULL)
1142 return PRINT_INSN_ARG_MEMORY_ERROR;
1143 break;
1145 case 7:
1146 switch (val & 7)
1148 case 0:
1149 NEXTWORD (p, val, PRINT_INSN_ARG_MEMORY_ERROR);
1150 (*info->print_address_func) (val, info);
1151 break;
1153 case 1:
1154 NEXTULONG (p, uval);
1155 (*info->print_address_func) (uval, info);
1156 break;
1158 case 2:
1159 NEXTWORD (p, val, PRINT_INSN_ARG_MEMORY_ERROR);
1160 (*info->fprintf_styled_func) (info->stream, dis_style_register,
1161 "%%pc");
1162 (*info->fprintf_styled_func) (info->stream, dis_style_text, "@(");
1163 (*info->print_address_func) (addr + val, info);
1164 (*info->fprintf_styled_func) (info->stream, dis_style_text, ")");
1165 break;
1167 case 3:
1168 p = print_indexed (-1, p, addr, info);
1169 if (p == NULL)
1170 return PRINT_INSN_ARG_MEMORY_ERROR;
1171 break;
1173 case 4:
1174 flt_p = 1; /* Assume it's a float... */
1175 switch (place)
1177 case 'b':
1178 NEXTBYTE (p, val);
1179 flt_p = 0;
1180 break;
1182 case 'w':
1183 NEXTWORD (p, val, PRINT_INSN_ARG_MEMORY_ERROR);
1184 flt_p = 0;
1185 break;
1187 case 'l':
1188 NEXTLONG (p, val, PRINT_INSN_ARG_MEMORY_ERROR);
1189 flt_p = 0;
1190 break;
1192 case 'f':
1193 NEXTSINGLE (flval, p);
1194 break;
1196 case 'F':
1197 NEXTDOUBLE (flval, p);
1198 break;
1200 case 'x':
1201 NEXTEXTEND (flval, p);
1202 break;
1204 case 'p':
1205 NEXTPACKED (p, flval);
1206 break;
1208 default:
1209 return PRINT_INSN_ARG_INVALID_OPERAND;
1211 if (flt_p) /* Print a float? */
1212 (*info->fprintf_styled_func) (info->stream, dis_style_immediate,
1213 "#0e%g", flval);
1214 else
1215 (*info->fprintf_styled_func) (info->stream, dis_style_immediate,
1216 "#%d", val);
1217 break;
1219 default:
1220 return PRINT_INSN_ARG_INVALID_OPERAND;
1224 /* If place is '/', then this is the case of the mask bit for
1225 mac/emac loads. Now that the arg has been printed, grab the
1226 mask bit and if set, add a '&' to the arg. */
1227 if (place == '/')
1229 FETCH_ARG (1, val);
1230 if (val)
1231 info->fprintf_styled_func (info->stream, dis_style_text, "&");
1233 break;
1235 case 'L':
1236 case 'l':
1237 if (place == 'w')
1239 char doneany;
1240 p1 = buffer + 2;
1241 NEXTWORD (p1, val, PRINT_INSN_ARG_MEMORY_ERROR);
1242 /* Move the pointer ahead if this point is farther ahead
1243 than the last. */
1244 p = p1 > p ? p1 : p;
1245 if (val == 0)
1247 (*info->fprintf_styled_func) (info->stream, dis_style_immediate,
1248 "#0");
1249 break;
1251 if (*d == 'l')
1253 int newval = 0;
1255 for (regno = 0; regno < 16; ++regno)
1256 if (val & (0x8000 >> regno))
1257 newval |= 1 << regno;
1258 val = newval;
1260 val &= 0xffff;
1261 doneany = 0;
1262 for (regno = 0; regno < 16; ++regno)
1263 if (val & (1 << regno))
1265 int first_regno;
1267 if (doneany)
1268 (*info->fprintf_styled_func) (info->stream, dis_style_text,
1269 "/");
1270 doneany = 1;
1271 (*info->fprintf_styled_func) (info->stream, dis_style_register,
1272 "%s", reg_names[regno]);
1273 first_regno = regno;
1274 while (val & (1 << (regno + 1)))
1275 ++regno;
1276 if (regno > first_regno)
1278 (*info->fprintf_styled_func) (info->stream,
1279 dis_style_text, "-");
1280 (*info->fprintf_styled_func) (info->stream,
1281 dis_style_register, "%s",
1282 reg_names[regno]);
1286 else if (place == '3')
1288 /* `fmovem' insn. */
1289 char doneany;
1291 FETCH_ARG (8, val);
1292 if (val == 0)
1294 (*info->fprintf_styled_func) (info->stream, dis_style_immediate,
1295 "#0");
1296 break;
1298 if (*d == 'l')
1300 int newval = 0;
1302 for (regno = 0; regno < 8; ++regno)
1303 if (val & (0x80 >> regno))
1304 newval |= 1 << regno;
1305 val = newval;
1307 val &= 0xff;
1308 doneany = 0;
1309 for (regno = 0; regno < 8; ++regno)
1310 if (val & (1 << regno))
1312 int first_regno;
1313 if (doneany)
1314 (*info->fprintf_styled_func) (info->stream, dis_style_text,
1315 "/");
1316 doneany = 1;
1317 (*info->fprintf_styled_func) (info->stream, dis_style_register,
1318 "%%fp%d", regno);
1319 first_regno = regno;
1320 while (val & (1 << (regno + 1)))
1321 ++regno;
1322 if (regno > first_regno)
1324 (*info->fprintf_styled_func) (info->stream,
1325 dis_style_text, "-");
1326 (*info->fprintf_styled_func) (info->stream,
1327 dis_style_register,
1328 "%%fp%d", regno);
1332 else if (place == '8')
1334 FETCH_ARG (3, val);
1335 /* fmoveml for FP status registers. */
1336 (*info->fprintf_styled_func) (info->stream, dis_style_register,
1337 "%s", fpcr_names[val]);
1339 else
1340 return PRINT_INSN_ARG_INVALID_OP_TABLE;
1341 break;
1343 case 'X':
1344 place = '8';
1345 /* Fall through. */
1346 case 'Y':
1347 case 'Z':
1348 case 'W':
1349 case '0':
1350 case '1':
1351 case '2':
1352 case '3':
1354 char *name = 0;
1356 FETCH_ARG (5, val);
1357 switch (val)
1359 case 2: name = "%tt0"; break;
1360 case 3: name = "%tt1"; break;
1361 case 0x10: name = "%tc"; break;
1362 case 0x11: name = "%drp"; break;
1363 case 0x12: name = "%srp"; break;
1364 case 0x13: name = "%crp"; break;
1365 case 0x14: name = "%cal"; break;
1366 case 0x15: name = "%val"; break;
1367 case 0x16: name = "%scc"; break;
1368 case 0x17: name = "%ac"; break;
1369 case 0x18: name = "%psr"; break;
1370 case 0x19: name = "%pcsr"; break;
1371 case 0x1c:
1372 case 0x1d:
1374 int break_reg = ((buffer[3] >> 2) & 7);
1376 (*info->fprintf_styled_func)
1377 (info->stream, dis_style_register,
1378 val == 0x1c ? "%%bad%d" : "%%bac%d", break_reg);
1380 break;
1381 default:
1382 (*info->fprintf_styled_func) (info->stream, dis_style_text,
1383 "<mmu register %d>", val);
1385 if (name)
1386 (*info->fprintf_styled_func) (info->stream, dis_style_register,
1387 "%s", name);
1389 break;
1391 case 'f':
1393 int fc;
1395 FETCH_ARG (5, fc);
1396 if (fc == 1)
1397 (*info->fprintf_styled_func) (info->stream, dis_style_register,
1398 "%%dfc");
1399 else if (fc == 0)
1400 (*info->fprintf_styled_func) (info->stream, dis_style_register,
1401 "%%sfc");
1402 else
1403 /* xgettext:c-format */
1404 (*info->fprintf_styled_func) (info->stream, dis_style_text,
1405 _("<function code %d>"), fc);
1407 break;
1409 case 'V':
1410 (*info->fprintf_styled_func) (info->stream, dis_style_register, "%%val");
1411 break;
1413 case 't':
1415 int level;
1417 FETCH_ARG (3, level);
1418 (*info->fprintf_styled_func) (info->stream, dis_style_immediate,
1419 "%d", level);
1421 break;
1423 case 'u':
1425 short is_upper = 0;
1426 int reg;
1428 FETCH_ARG (5, reg);
1429 if (reg & 0x10)
1431 is_upper = 1;
1432 reg &= 0xf;
1434 (*info->fprintf_styled_func) (info->stream, dis_style_register, "%s%s",
1435 reg_half_names[reg],
1436 is_upper ? "u" : "l");
1438 break;
1440 default:
1441 return PRINT_INSN_ARG_INVALID_OP_TABLE;
1444 return p - p0;
1447 /* Return the insn type determined from the opcode information. */
1449 static enum dis_insn_type
1450 m68k_opcode_to_insn_type (const struct m68k_opcode *opc)
1452 /* All branches have an operand in 'B' format (the 'B' place only comes
1453 with the 'B' format). */
1454 if (strchr (opc->args, 'B') == NULL)
1455 return dis_nonbranch;
1457 /* Most branches are conditional branches, detect the ones that aren't
1458 from the opcode name. */
1459 if (strncmp (opc->name, "bra", 3) == 0)
1460 return dis_branch;
1462 if (strncmp (opc->name, "bsr", 3) == 0)
1463 return dis_jsr;
1465 return dis_condbranch;
1468 /* Try to match the current instruction to best and if so, return the
1469 number of bytes consumed from the instruction stream, else zero.
1470 Return -1 on memory error. */
1472 static int
1473 match_insn_m68k (bfd_vma memaddr,
1474 disassemble_info * info,
1475 const struct m68k_opcode * best)
1477 unsigned char *save_p;
1478 unsigned char *p;
1479 const char *d;
1480 const char *args = best->args;
1482 struct private *priv = (struct private *) info->private_data;
1483 bfd_byte *buffer = priv->the_buffer;
1484 fprintf_styled_ftype save_printer = info->fprintf_styled_func;
1485 void (* save_print_address) (bfd_vma, struct disassemble_info *)
1486 = info->print_address_func;
1488 if (*args == '.')
1489 args++;
1491 /* Point at first word of argument data,
1492 and at descriptor for first argument. */
1493 p = buffer + 2;
1495 /* Figure out how long the fixed-size portion of the instruction is.
1496 The only place this is stored in the opcode table is
1497 in the arguments--look for arguments which specify fields in the 2nd
1498 or 3rd words of the instruction. */
1499 for (d = args; *d; d += 2)
1501 /* I don't think it is necessary to be checking d[0] here;
1502 I suspect all this could be moved to the case statement below. */
1503 if (d[0] == '#')
1505 if (d[1] == 'l' && p - buffer < 6)
1506 p = buffer + 6;
1507 else if (p - buffer < 4 && d[1] != 'C' && d[1] != '8')
1508 p = buffer + 4;
1511 if ((d[0] == 'L' || d[0] == 'l') && d[1] == 'w' && p - buffer < 4)
1512 p = buffer + 4;
1514 switch (d[1])
1516 case '1':
1517 case '2':
1518 case '3':
1519 case '7':
1520 case '8':
1521 case '9':
1522 case 'i':
1523 if (p - buffer < 4)
1524 p = buffer + 4;
1525 break;
1526 case '4':
1527 case '5':
1528 case '6':
1529 if (p - buffer < 6)
1530 p = buffer + 6;
1531 break;
1532 default:
1533 break;
1537 /* pflusha is an exceptions. It takes no arguments but is two words
1538 long. Recognize it by looking at the lower 16 bits of the mask. */
1539 if (p - buffer < 4 && (best->match & 0xFFFF) != 0)
1540 p = buffer + 4;
1542 /* lpstop is another exception. It takes a one word argument but is
1543 three words long. */
1544 if (p - buffer < 6
1545 && (best->match & 0xffff) == 0xffff
1546 && args[0] == '#'
1547 && args[1] == 'w')
1549 /* Copy the one word argument into the usual location for a one
1550 word argument, to simplify printing it. We can get away with
1551 this because we know exactly what the second word is, and we
1552 aren't going to print anything based on it. */
1553 p = buffer + 6;
1554 if (!FETCH_DATA (info, p))
1555 return -1;
1556 buffer[2] = buffer[4];
1557 buffer[3] = buffer[5];
1560 if (!FETCH_DATA (info, p))
1561 return -1;
1563 save_p = p;
1564 info->print_address_func = dummy_print_address;
1565 info->fprintf_styled_func = dummy_printer;
1567 /* We scan the operands twice. The first time we don't print anything,
1568 but look for errors. */
1569 for (d = args; *d; d += 2)
1571 int eaten = print_insn_arg (d, buffer, p, memaddr + (p - buffer), info);
1573 if (eaten >= 0)
1574 p += eaten;
1575 else if (eaten == PRINT_INSN_ARG_INVALID_OPERAND
1576 || eaten == PRINT_INSN_ARG_MEMORY_ERROR)
1578 info->fprintf_styled_func = save_printer;
1579 info->print_address_func = save_print_address;
1580 return eaten == PRINT_INSN_ARG_MEMORY_ERROR ? -1 : 0;
1582 else
1584 /* We must restore the print functions before trying to print the
1585 error message. */
1586 info->fprintf_styled_func = save_printer;
1587 info->print_address_func = save_print_address;
1588 info->fprintf_styled_func (info->stream, dis_style_text,
1589 /* xgettext:c-format */
1590 _("<internal error in opcode table: %s %s>\n"),
1591 best->name, best->args);
1592 return 2;
1596 p = save_p;
1597 info->fprintf_styled_func = save_printer;
1598 info->print_address_func = save_print_address;
1599 info->insn_type = m68k_opcode_to_insn_type (best);
1601 d = args;
1603 info->fprintf_styled_func (info->stream, dis_style_mnemonic, "%s", best->name);
1605 if (*d)
1606 info->fprintf_styled_func (info->stream, dis_style_text, " ");
1608 while (*d)
1610 p += print_insn_arg (d, buffer, p, memaddr + (p - buffer), info);
1611 d += 2;
1613 if (*d && *(d - 2) != 'I' && *d != 'k')
1614 info->fprintf_styled_func (info->stream, dis_style_text, ",");
1617 return p - buffer;
1620 /* Try to interpret the instruction at address MEMADDR as one that
1621 can execute on a processor with the features given by ARCH_MASK.
1622 If successful, print the instruction to INFO->STREAM and return
1623 its length in bytes. Return 0 otherwise. Return -1 on memory
1624 error. */
1626 static int
1627 m68k_scan_mask (bfd_vma memaddr, disassemble_info *info,
1628 unsigned int arch_mask)
1630 int i;
1631 const char *d;
1632 static const struct m68k_opcode **opcodes[16];
1633 static int numopcodes[16];
1634 int val;
1635 int major_opcode;
1637 struct private *priv = (struct private *) info->private_data;
1638 bfd_byte *buffer = priv->the_buffer;
1640 if (!opcodes[0])
1642 /* Speed up the matching by sorting the opcode
1643 table on the upper four bits of the opcode. */
1644 const struct m68k_opcode **opc_pointer[16];
1646 /* First count how many opcodes are in each of the sixteen buckets. */
1647 for (i = 0; i < m68k_numopcodes; i++)
1648 numopcodes[(m68k_opcodes[i].opcode >> 28) & 15]++;
1650 /* Then create a sorted table of pointers
1651 that point into the unsorted table. */
1652 opc_pointer[0] = xmalloc (sizeof (struct m68k_opcode *)
1653 * m68k_numopcodes);
1654 opcodes[0] = opc_pointer[0];
1656 for (i = 1; i < 16; i++)
1658 opc_pointer[i] = opc_pointer[i - 1] + numopcodes[i - 1];
1659 opcodes[i] = opc_pointer[i];
1662 for (i = 0; i < m68k_numopcodes; i++)
1663 *opc_pointer[(m68k_opcodes[i].opcode >> 28) & 15]++ = &m68k_opcodes[i];
1666 if (!FETCH_DATA (info, buffer + 2))
1667 return -1;
1668 major_opcode = (buffer[0] >> 4) & 15;
1670 for (i = 0; i < numopcodes[major_opcode]; i++)
1672 const struct m68k_opcode *opc = opcodes[major_opcode][i];
1673 unsigned long opcode = opc->opcode;
1674 unsigned long match = opc->match;
1675 const char *args = opc->args;
1677 if (*args == '.')
1678 args++;
1680 if (((0xff & buffer[0] & (match >> 24)) == (0xff & (opcode >> 24)))
1681 && ((0xff & buffer[1] & (match >> 16)) == (0xff & (opcode >> 16)))
1682 /* Only fetch the next two bytes if we need to. */
1683 && (((0xffff & match) == 0)
1685 (FETCH_DATA (info, buffer + 4)
1686 && ((0xff & buffer[2] & (match >> 8)) == (0xff & (opcode >> 8)))
1687 && ((0xff & buffer[3] & match) == (0xff & opcode)))
1689 && (opc->arch & arch_mask) != 0)
1691 /* Don't use for printout the variants of divul and divsl
1692 that have the same register number in two places.
1693 The more general variants will match instead. */
1694 for (d = args; *d; d += 2)
1695 if (d[1] == 'D')
1696 break;
1698 /* Don't use for printout the variants of most floating
1699 point coprocessor instructions which use the same
1700 register number in two places, as above. */
1701 if (*d == '\0')
1702 for (d = args; *d; d += 2)
1703 if (d[1] == 't')
1704 break;
1706 /* Don't match fmovel with more than one register;
1707 wait for fmoveml. */
1708 if (*d == '\0')
1710 for (d = args; *d; d += 2)
1712 if (d[0] == 's' && d[1] == '8')
1714 val = fetch_arg (buffer, d[1], 3, info);
1715 if (val < 0)
1716 return 0;
1717 if ((val & (val - 1)) != 0)
1718 break;
1723 /* Don't match FPU insns with non-default coprocessor ID. */
1724 if (*d == '\0')
1726 for (d = args; *d; d += 2)
1728 if (d[0] == 'I')
1730 val = fetch_arg (buffer, 'd', 3, info);
1731 if (val != 1)
1732 break;
1737 if (*d == '\0')
1738 if ((val = match_insn_m68k (memaddr, info, opc)))
1739 return val;
1742 return 0;
1745 /* Print the m68k instruction at address MEMADDR in debugged memory,
1746 on INFO->STREAM. Returns length of the instruction, in bytes. */
1749 print_insn_m68k (bfd_vma memaddr, disassemble_info *info)
1751 unsigned int arch_mask;
1752 struct private priv;
1753 int val;
1755 bfd_byte *buffer = priv.the_buffer;
1757 info->insn_info_valid = 1;
1758 info->private_data = & priv;
1759 /* Tell objdump to use two bytes per chunk
1760 and six bytes per line for displaying raw data. */
1761 info->bytes_per_chunk = 2;
1762 info->bytes_per_line = 6;
1763 info->display_endian = BFD_ENDIAN_BIG;
1764 priv.max_fetched = priv.the_buffer;
1765 priv.insn_start = memaddr;
1767 arch_mask = bfd_m68k_mach_to_features (info->mach);
1768 if (!arch_mask)
1770 /* First try printing an m680x0 instruction. Try printing a Coldfire
1771 one if that fails. */
1772 val = m68k_scan_mask (memaddr, info, m68k_mask);
1773 if (val <= 0)
1774 val = m68k_scan_mask (memaddr, info, mcf_mask);
1776 else
1778 val = m68k_scan_mask (memaddr, info, arch_mask);
1781 if (val == 0)
1783 /* Handle undefined instructions. */
1784 info->fprintf_styled_func (info->stream, dis_style_assembler_directive,
1785 ".short");
1786 info->fprintf_styled_func (info->stream, dis_style_text, " ");
1787 info->fprintf_styled_func (info->stream, dis_style_immediate,
1788 "0x%04x", (buffer[0] << 8) + buffer[1]);
1790 info->insn_type = dis_noninsn;
1793 return val ? val : 2;