2007-09-30 Mike Frysinger <vapier@gentoo.org>
[binutils.git] / opcodes / m68k-dis.c
blob77820ef8a0f7bf5f2fc8f5b75a090fabe913f40f
1 /* Print Motorola 68k instructions.
2 Copyright 1986, 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
4 Free Software Foundation, Inc.
6 This file is part of the GNU opcodes library.
8 This library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 It is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
16 License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
23 #include "sysdep.h"
24 #include "dis-asm.h"
25 #include "floatformat.h"
26 #include "libiberty.h"
27 #include "opintl.h"
29 #include "opcode/m68k.h"
31 /* Local function prototypes. */
33 const char * const fpcr_names[] =
35 "", "%fpiar", "%fpsr", "%fpiar/%fpsr", "%fpcr",
36 "%fpiar/%fpcr", "%fpsr/%fpcr", "%fpiar/%fpsr/%fpcr"
39 static char *const reg_names[] =
41 "%d0", "%d1", "%d2", "%d3", "%d4", "%d5", "%d6", "%d7",
42 "%a0", "%a1", "%a2", "%a3", "%a4", "%a5", "%fp", "%sp",
43 "%ps", "%pc"
46 /* Name of register halves for MAC/EMAC.
47 Seperate from reg_names since 'spu', 'fpl' look weird. */
48 static char *const reg_half_names[] =
50 "%d0", "%d1", "%d2", "%d3", "%d4", "%d5", "%d6", "%d7",
51 "%a0", "%a1", "%a2", "%a3", "%a4", "%a5", "%a6", "%a7",
52 "%ps", "%pc"
55 /* Sign-extend an (unsigned char). */
56 #if __STDC__ == 1
57 #define COERCE_SIGNED_CHAR(ch) ((signed char) (ch))
58 #else
59 #define COERCE_SIGNED_CHAR(ch) ((int) (((ch) ^ 0x80) & 0xFF) - 128)
60 #endif
62 /* Get a 1 byte signed integer. */
63 #define NEXTBYTE(p) (p += 2, FETCH_DATA (info, p), COERCE_SIGNED_CHAR(p[-1]))
65 /* Get a 2 byte signed integer. */
66 #define COERCE16(x) ((int) (((x) ^ 0x8000) - 0x8000))
67 #define NEXTWORD(p) \
68 (p += 2, FETCH_DATA (info, p), \
69 COERCE16 ((p[-2] << 8) + p[-1]))
71 /* Get a 4 byte signed integer. */
72 #define COERCE32(x) ((bfd_signed_vma) ((x) ^ 0x80000000) - 0x80000000)
73 #define NEXTLONG(p) \
74 (p += 4, FETCH_DATA (info, p), \
75 (COERCE32 ((((((p[-4] << 8) + p[-3]) << 8) + p[-2]) << 8) + p[-1])))
77 /* Get a 4 byte unsigned integer. */
78 #define NEXTULONG(p) \
79 (p += 4, FETCH_DATA (info, p), \
80 (unsigned int) ((((((p[-4] << 8) + p[-3]) << 8) + p[-2]) << 8) + p[-1]))
82 /* Get a single precision float. */
83 #define NEXTSINGLE(val, p) \
84 (p += 4, FETCH_DATA (info, p), \
85 floatformat_to_double (&floatformat_ieee_single_big, (char *) p - 4, &val))
87 /* Get a double precision float. */
88 #define NEXTDOUBLE(val, p) \
89 (p += 8, FETCH_DATA (info, p), \
90 floatformat_to_double (&floatformat_ieee_double_big, (char *) p - 8, &val))
92 /* Get an extended precision float. */
93 #define NEXTEXTEND(val, p) \
94 (p += 12, FETCH_DATA (info, p), \
95 floatformat_to_double (&floatformat_m68881_ext, (char *) p - 12, &val))
97 /* Need a function to convert from packed to double
98 precision. Actually, it's easier to print a
99 packed number than a double anyway, so maybe
100 there should be a special case to handle this... */
101 #define NEXTPACKED(p) \
102 (p += 12, FETCH_DATA (info, p), 0.0)
104 /* Maximum length of an instruction. */
105 #define MAXLEN 22
107 #include <setjmp.h>
109 struct private
111 /* Points to first byte not fetched. */
112 bfd_byte *max_fetched;
113 bfd_byte the_buffer[MAXLEN];
114 bfd_vma insn_start;
115 jmp_buf bailout;
118 /* Make sure that bytes from INFO->PRIVATE_DATA->BUFFER (inclusive)
119 to ADDR (exclusive) are valid. Returns 1 for success, longjmps
120 on error. */
121 #define FETCH_DATA(info, addr) \
122 ((addr) <= ((struct private *) (info->private_data))->max_fetched \
123 ? 1 : fetch_data ((info), (addr)))
125 static int
126 fetch_data (struct disassemble_info *info, bfd_byte *addr)
128 int status;
129 struct private *priv = (struct private *)info->private_data;
130 bfd_vma start = priv->insn_start + (priv->max_fetched - priv->the_buffer);
132 status = (*info->read_memory_func) (start,
133 priv->max_fetched,
134 addr - priv->max_fetched,
135 info);
136 if (status != 0)
138 (*info->memory_error_func) (status, start, info);
139 longjmp (priv->bailout, 1);
141 else
142 priv->max_fetched = addr;
143 return 1;
146 /* This function is used to print to the bit-bucket. */
147 static int
148 dummy_printer (FILE *file ATTRIBUTE_UNUSED,
149 const char *format ATTRIBUTE_UNUSED,
150 ...)
152 return 0;
155 static void
156 dummy_print_address (bfd_vma vma ATTRIBUTE_UNUSED,
157 struct disassemble_info *info ATTRIBUTE_UNUSED)
161 /* Fetch BITS bits from a position in the instruction specified by CODE.
162 CODE is a "place to put an argument", or 'x' for a destination
163 that is a general address (mode and register).
164 BUFFER contains the instruction. */
166 static int
167 fetch_arg (unsigned char *buffer,
168 int code,
169 int bits,
170 disassemble_info *info)
172 int val = 0;
174 switch (code)
176 case '/': /* MAC/EMAC mask bit. */
177 val = buffer[3] >> 5;
178 break;
180 case 'G': /* EMAC ACC load. */
181 val = ((buffer[3] >> 3) & 0x2) | ((~buffer[1] >> 7) & 0x1);
182 break;
184 case 'H': /* EMAC ACC !load. */
185 val = ((buffer[3] >> 3) & 0x2) | ((buffer[1] >> 7) & 0x1);
186 break;
188 case ']': /* EMAC ACCEXT bit. */
189 val = buffer[0] >> 2;
190 break;
192 case 'I': /* MAC/EMAC scale factor. */
193 val = buffer[2] >> 1;
194 break;
196 case 'F': /* EMAC ACCx. */
197 val = buffer[0] >> 1;
198 break;
200 case 'f':
201 val = buffer[1];
202 break;
204 case 's':
205 val = buffer[1];
206 break;
208 case 'd': /* Destination, for register or quick. */
209 val = (buffer[0] << 8) + buffer[1];
210 val >>= 9;
211 break;
213 case 'x': /* Destination, for general arg. */
214 val = (buffer[0] << 8) + buffer[1];
215 val >>= 6;
216 break;
218 case 'k':
219 FETCH_DATA (info, buffer + 3);
220 val = (buffer[3] >> 4);
221 break;
223 case 'C':
224 FETCH_DATA (info, buffer + 3);
225 val = buffer[3];
226 break;
228 case '1':
229 FETCH_DATA (info, buffer + 3);
230 val = (buffer[2] << 8) + buffer[3];
231 val >>= 12;
232 break;
234 case '2':
235 FETCH_DATA (info, buffer + 3);
236 val = (buffer[2] << 8) + buffer[3];
237 val >>= 6;
238 break;
240 case '3':
241 case 'j':
242 FETCH_DATA (info, buffer + 3);
243 val = (buffer[2] << 8) + buffer[3];
244 break;
246 case '4':
247 FETCH_DATA (info, buffer + 5);
248 val = (buffer[4] << 8) + buffer[5];
249 val >>= 12;
250 break;
252 case '5':
253 FETCH_DATA (info, buffer + 5);
254 val = (buffer[4] << 8) + buffer[5];
255 val >>= 6;
256 break;
258 case '6':
259 FETCH_DATA (info, buffer + 5);
260 val = (buffer[4] << 8) + buffer[5];
261 break;
263 case '7':
264 FETCH_DATA (info, buffer + 3);
265 val = (buffer[2] << 8) + buffer[3];
266 val >>= 7;
267 break;
269 case '8':
270 FETCH_DATA (info, buffer + 3);
271 val = (buffer[2] << 8) + buffer[3];
272 val >>= 10;
273 break;
275 case '9':
276 FETCH_DATA (info, buffer + 3);
277 val = (buffer[2] << 8) + buffer[3];
278 val >>= 5;
279 break;
281 case 'e':
282 val = (buffer[1] >> 6);
283 break;
285 case 'E':
286 FETCH_DATA (info, buffer + 3);
287 val = (buffer[2] >> 1);
288 break;
290 case 'm':
291 val = (buffer[1] & 0x40 ? 0x8 : 0)
292 | ((buffer[0] >> 1) & 0x7)
293 | (buffer[3] & 0x80 ? 0x10 : 0);
294 break;
296 case 'n':
297 val = (buffer[1] & 0x40 ? 0x8 : 0) | ((buffer[0] >> 1) & 0x7);
298 break;
300 case 'o':
301 val = (buffer[2] >> 4) | (buffer[3] & 0x80 ? 0x10 : 0);
302 break;
304 case 'M':
305 val = (buffer[1] & 0xf) | (buffer[3] & 0x40 ? 0x10 : 0);
306 break;
308 case 'N':
309 val = (buffer[3] & 0xf) | (buffer[3] & 0x40 ? 0x10 : 0);
310 break;
312 case 'h':
313 val = buffer[2] >> 2;
314 break;
316 default:
317 abort ();
320 /* bits is never too big. */
321 return val & ((1 << bits) - 1);
324 /* Check if an EA is valid for a particular code. This is required
325 for the EMAC instructions since the type of source address determines
326 if it is a EMAC-load instruciton if the EA is mode 2-5, otherwise it
327 is a non-load EMAC instruction and the bits mean register Ry.
328 A similar case exists for the movem instructions where the register
329 mask is interpreted differently for different EAs. */
331 static bfd_boolean
332 m68k_valid_ea (char code, int val)
334 int mode, mask;
335 #define M(n0,n1,n2,n3,n4,n5,n6,n70,n71,n72,n73,n74) \
336 (n0 | n1 << 1 | n2 << 2 | n3 << 3 | n4 << 4 | n5 << 5 | n6 << 6 \
337 | n70 << 7 | n71 << 8 | n72 << 9 | n73 << 10 | n74 << 11)
339 switch (code)
341 case '*':
342 mask = M (1,1,1,1,1,1,1,1,1,1,1,1);
343 break;
344 case '~':
345 mask = M (0,0,1,1,1,1,1,1,1,0,0,0);
346 break;
347 case '%':
348 mask = M (1,1,1,1,1,1,1,1,1,0,0,0);
349 break;
350 case ';':
351 mask = M (1,0,1,1,1,1,1,1,1,1,1,1);
352 break;
353 case '@':
354 mask = M (1,0,1,1,1,1,1,1,1,1,1,0);
355 break;
356 case '!':
357 mask = M (0,0,1,0,0,1,1,1,1,1,1,0);
358 break;
359 case '&':
360 mask = M (0,0,1,0,0,1,1,1,1,0,0,0);
361 break;
362 case '$':
363 mask = M (1,0,1,1,1,1,1,1,1,0,0,0);
364 break;
365 case '?':
366 mask = M (1,0,1,0,0,1,1,1,1,0,0,0);
367 break;
368 case '/':
369 mask = M (1,0,1,0,0,1,1,1,1,1,1,0);
370 break;
371 case '|':
372 mask = M (0,0,1,0,0,1,1,1,1,1,1,0);
373 break;
374 case '>':
375 mask = M (0,0,1,0,1,1,1,1,1,0,0,0);
376 break;
377 case '<':
378 mask = M (0,0,1,1,0,1,1,1,1,1,1,0);
379 break;
380 case 'm':
381 mask = M (1,1,1,1,1,0,0,0,0,0,0,0);
382 break;
383 case 'n':
384 mask = M (0,0,0,0,0,1,0,0,0,1,0,0);
385 break;
386 case 'o':
387 mask = M (0,0,0,0,0,0,1,1,1,0,1,1);
388 break;
389 case 'p':
390 mask = M (1,1,1,1,1,1,0,0,0,0,0,0);
391 break;
392 case 'q':
393 mask = M (1,0,1,1,1,1,0,0,0,0,0,0);
394 break;
395 case 'v':
396 mask = M (1,0,1,1,1,1,0,1,1,0,0,0);
397 break;
398 case 'b':
399 mask = M (1,0,1,1,1,1,0,0,0,1,0,0);
400 break;
401 case 'w':
402 mask = M (0,0,1,1,1,1,0,0,0,1,0,0);
403 break;
404 case 'y':
405 mask = M (0,0,1,0,0,1,0,0,0,0,0,0);
406 break;
407 case 'z':
408 mask = M (0,0,1,0,0,1,0,0,0,1,0,0);
409 break;
410 case '4':
411 mask = M (0,0,1,1,1,1,0,0,0,0,0,0);
412 break;
413 default:
414 abort ();
416 #undef M
418 mode = (val >> 3) & 7;
419 if (mode == 7)
420 mode += val & 7;
421 return (mask & (1 << mode)) != 0;
424 /* Print a base register REGNO and displacement DISP, on INFO->STREAM.
425 REGNO = -1 for pc, -2 for none (suppressed). */
427 static void
428 print_base (int regno, bfd_vma disp, disassemble_info *info)
430 if (regno == -1)
432 (*info->fprintf_func) (info->stream, "%%pc@(");
433 (*info->print_address_func) (disp, info);
435 else
437 char buf[50];
439 if (regno == -2)
440 (*info->fprintf_func) (info->stream, "@(");
441 else if (regno == -3)
442 (*info->fprintf_func) (info->stream, "%%zpc@(");
443 else
444 (*info->fprintf_func) (info->stream, "%s@(", reg_names[regno]);
446 sprintf_vma (buf, disp);
447 (*info->fprintf_func) (info->stream, "%s", buf);
451 /* Print an indexed argument. The base register is BASEREG (-1 for pc).
452 P points to extension word, in buffer.
453 ADDR is the nominal core address of that extension word. */
455 static unsigned char *
456 print_indexed (int basereg,
457 unsigned char *p,
458 bfd_vma addr,
459 disassemble_info *info)
461 int word;
462 static char *const scales[] = { "", ":2", ":4", ":8" };
463 bfd_vma base_disp;
464 bfd_vma outer_disp;
465 char buf[40];
466 char vmabuf[50];
468 word = NEXTWORD (p);
470 /* Generate the text for the index register.
471 Where this will be output is not yet determined. */
472 sprintf (buf, "%s:%c%s",
473 reg_names[(word >> 12) & 0xf],
474 (word & 0x800) ? 'l' : 'w',
475 scales[(word >> 9) & 3]);
477 /* Handle the 68000 style of indexing. */
479 if ((word & 0x100) == 0)
481 base_disp = word & 0xff;
482 if ((base_disp & 0x80) != 0)
483 base_disp -= 0x100;
484 if (basereg == -1)
485 base_disp += addr;
486 print_base (basereg, base_disp, info);
487 (*info->fprintf_func) (info->stream, ",%s)", buf);
488 return p;
491 /* Handle the generalized kind. */
492 /* First, compute the displacement to add to the base register. */
493 if (word & 0200)
495 if (basereg == -1)
496 basereg = -3;
497 else
498 basereg = -2;
500 if (word & 0100)
501 buf[0] = '\0';
502 base_disp = 0;
503 switch ((word >> 4) & 3)
505 case 2:
506 base_disp = NEXTWORD (p);
507 break;
508 case 3:
509 base_disp = NEXTLONG (p);
511 if (basereg == -1)
512 base_disp += addr;
514 /* Handle single-level case (not indirect). */
515 if ((word & 7) == 0)
517 print_base (basereg, base_disp, info);
518 if (buf[0] != '\0')
519 (*info->fprintf_func) (info->stream, ",%s", buf);
520 (*info->fprintf_func) (info->stream, ")");
521 return p;
524 /* Two level. Compute displacement to add after indirection. */
525 outer_disp = 0;
526 switch (word & 3)
528 case 2:
529 outer_disp = NEXTWORD (p);
530 break;
531 case 3:
532 outer_disp = NEXTLONG (p);
535 print_base (basereg, base_disp, info);
536 if ((word & 4) == 0 && buf[0] != '\0')
538 (*info->fprintf_func) (info->stream, ",%s", buf);
539 buf[0] = '\0';
541 sprintf_vma (vmabuf, outer_disp);
542 (*info->fprintf_func) (info->stream, ")@(%s", vmabuf);
543 if (buf[0] != '\0')
544 (*info->fprintf_func) (info->stream, ",%s", buf);
545 (*info->fprintf_func) (info->stream, ")");
547 return p;
550 /* Returns number of bytes "eaten" by the operand, or
551 return -1 if an invalid operand was found, or -2 if
552 an opcode tabe error was found.
553 ADDR is the pc for this arg to be relative to. */
555 static int
556 print_insn_arg (const char *d,
557 unsigned char *buffer,
558 unsigned char *p0,
559 bfd_vma addr,
560 disassemble_info *info)
562 int val = 0;
563 int place = d[1];
564 unsigned char *p = p0;
565 int regno;
566 const char *regname;
567 unsigned char *p1;
568 double flval;
569 int flt_p;
570 bfd_signed_vma disp;
571 unsigned int uval;
573 switch (*d)
575 case 'c': /* Cache identifier. */
577 static char *const cacheFieldName[] = { "nc", "dc", "ic", "bc" };
578 val = fetch_arg (buffer, place, 2, info);
579 (*info->fprintf_func) (info->stream, cacheFieldName[val]);
580 break;
583 case 'a': /* Address register indirect only. Cf. case '+'. */
585 (*info->fprintf_func)
586 (info->stream,
587 "%s@",
588 reg_names[fetch_arg (buffer, place, 3, info) + 8]);
589 break;
592 case '_': /* 32-bit absolute address for move16. */
594 uval = NEXTULONG (p);
595 (*info->print_address_func) (uval, info);
596 break;
599 case 'C':
600 (*info->fprintf_func) (info->stream, "%%ccr");
601 break;
603 case 'S':
604 (*info->fprintf_func) (info->stream, "%%sr");
605 break;
607 case 'U':
608 (*info->fprintf_func) (info->stream, "%%usp");
609 break;
611 case 'E':
612 (*info->fprintf_func) (info->stream, "%%acc");
613 break;
615 case 'G':
616 (*info->fprintf_func) (info->stream, "%%macsr");
617 break;
619 case 'H':
620 (*info->fprintf_func) (info->stream, "%%mask");
621 break;
623 case 'J':
625 /* FIXME: There's a problem here, different m68k processors call the
626 same address different names. This table can't get it right
627 because it doesn't know which processor it's disassembling for. */
628 static const struct { char *name; int value; } names[]
629 = {{"%sfc", 0x000}, {"%dfc", 0x001}, {"%cacr", 0x002},
630 {"%tc", 0x003}, {"%itt0",0x004}, {"%itt1", 0x005},
631 {"%dtt0",0x006}, {"%dtt1",0x007}, {"%buscr",0x008},
632 {"%usp", 0x800}, {"%vbr", 0x801}, {"%caar", 0x802},
633 {"%msp", 0x803}, {"%isp", 0x804},
634 /* reg c04 is sometimes called flashbar or rambar.
635 rec c05 is also sometimes called rambar. */
636 {"%rambar0", 0xc04}, {"%rambar1", 0xc05},
638 /* Should we be calling this psr like we do in case 'Y'? */
639 {"%mmusr",0x805},
641 {"%urp", 0x806}, {"%srp", 0x807}, {"%pcr", 0x808},
643 /* Fido added these. */
644 {"%cac", 0xffe}, {"%mbo", 0xfff}};
646 val = fetch_arg (buffer, place, 12, info);
647 for (regno = sizeof names / sizeof names[0] - 1; regno >= 0; regno--)
648 if (names[regno].value == val)
650 (*info->fprintf_func) (info->stream, "%s", names[regno].name);
651 break;
653 if (regno < 0)
654 (*info->fprintf_func) (info->stream, "%d", val);
656 break;
658 case 'Q':
659 val = fetch_arg (buffer, place, 3, info);
660 /* 0 means 8, except for the bkpt instruction... */
661 if (val == 0 && d[1] != 's')
662 val = 8;
663 (*info->fprintf_func) (info->stream, "#%d", val);
664 break;
666 case 'x':
667 val = fetch_arg (buffer, place, 3, info);
668 /* 0 means -1. */
669 if (val == 0)
670 val = -1;
671 (*info->fprintf_func) (info->stream, "#%d", val);
672 break;
674 case 'j':
675 val = fetch_arg (buffer, place, 3, info);
676 (*info->fprintf_func) (info->stream, "#%d", val+1);
677 break;
679 case 'K':
680 val = fetch_arg (buffer, place, 9, info);
681 (*info->fprintf_func) (info->stream, "#%d", val);
682 break;
684 case 'M':
685 if (place == 'h')
687 static char *const scalefactor_name[] = { "<<", ">>" };
688 val = fetch_arg (buffer, place, 1, info);
689 (*info->fprintf_func) (info->stream, scalefactor_name[val]);
691 else
693 val = fetch_arg (buffer, place, 8, info);
694 if (val & 0x80)
695 val = val - 0x100;
696 (*info->fprintf_func) (info->stream, "#%d", val);
698 break;
700 case 'T':
701 val = fetch_arg (buffer, place, 4, info);
702 (*info->fprintf_func) (info->stream, "#%d", val);
703 break;
705 case 'D':
706 (*info->fprintf_func) (info->stream, "%s",
707 reg_names[fetch_arg (buffer, place, 3, info)]);
708 break;
710 case 'A':
711 (*info->fprintf_func)
712 (info->stream, "%s",
713 reg_names[fetch_arg (buffer, place, 3, info) + 010]);
714 break;
716 case 'R':
717 (*info->fprintf_func)
718 (info->stream, "%s",
719 reg_names[fetch_arg (buffer, place, 4, info)]);
720 break;
722 case 'r':
723 regno = fetch_arg (buffer, place, 4, info);
724 if (regno > 7)
725 (*info->fprintf_func) (info->stream, "%s@", reg_names[regno]);
726 else
727 (*info->fprintf_func) (info->stream, "@(%s)", reg_names[regno]);
728 break;
730 case 'F':
731 (*info->fprintf_func)
732 (info->stream, "%%fp%d",
733 fetch_arg (buffer, place, 3, info));
734 break;
736 case 'O':
737 val = fetch_arg (buffer, place, 6, info);
738 if (val & 0x20)
739 (*info->fprintf_func) (info->stream, "%s", reg_names[val & 7]);
740 else
741 (*info->fprintf_func) (info->stream, "%d", val);
742 break;
744 case '+':
745 (*info->fprintf_func)
746 (info->stream, "%s@+",
747 reg_names[fetch_arg (buffer, place, 3, info) + 8]);
748 break;
750 case '-':
751 (*info->fprintf_func)
752 (info->stream, "%s@-",
753 reg_names[fetch_arg (buffer, place, 3, info) + 8]);
754 break;
756 case 'k':
757 if (place == 'k')
758 (*info->fprintf_func)
759 (info->stream, "{%s}",
760 reg_names[fetch_arg (buffer, place, 3, info)]);
761 else if (place == 'C')
763 val = fetch_arg (buffer, place, 7, info);
764 if (val > 63) /* This is a signed constant. */
765 val -= 128;
766 (*info->fprintf_func) (info->stream, "{#%d}", val);
768 else
769 return -2;
770 break;
772 case '#':
773 case '^':
774 p1 = buffer + (*d == '#' ? 2 : 4);
775 if (place == 's')
776 val = fetch_arg (buffer, place, 4, info);
777 else if (place == 'C')
778 val = fetch_arg (buffer, place, 7, info);
779 else if (place == '8')
780 val = fetch_arg (buffer, place, 3, info);
781 else if (place == '3')
782 val = fetch_arg (buffer, place, 8, info);
783 else if (place == 'b')
784 val = NEXTBYTE (p1);
785 else if (place == 'w' || place == 'W')
786 val = NEXTWORD (p1);
787 else if (place == 'l')
788 val = NEXTLONG (p1);
789 else
790 return -2;
791 (*info->fprintf_func) (info->stream, "#%d", val);
792 break;
794 case 'B':
795 if (place == 'b')
796 disp = NEXTBYTE (p);
797 else if (place == 'B')
798 disp = COERCE_SIGNED_CHAR (buffer[1]);
799 else if (place == 'w' || place == 'W')
800 disp = NEXTWORD (p);
801 else if (place == 'l' || place == 'L' || place == 'C')
802 disp = NEXTLONG (p);
803 else if (place == 'g')
805 disp = NEXTBYTE (buffer);
806 if (disp == 0)
807 disp = NEXTWORD (p);
808 else if (disp == -1)
809 disp = NEXTLONG (p);
811 else if (place == 'c')
813 if (buffer[1] & 0x40) /* If bit six is one, long offset. */
814 disp = NEXTLONG (p);
815 else
816 disp = NEXTWORD (p);
818 else
819 return -2;
821 (*info->print_address_func) (addr + disp, info);
822 break;
824 case 'd':
825 val = NEXTWORD (p);
826 (*info->fprintf_func)
827 (info->stream, "%s@(%d)",
828 reg_names[fetch_arg (buffer, place, 3, info) + 8], val);
829 break;
831 case 's':
832 (*info->fprintf_func) (info->stream, "%s",
833 fpcr_names[fetch_arg (buffer, place, 3, info)]);
834 break;
836 case 'e':
837 val = fetch_arg(buffer, place, 2, info);
838 (*info->fprintf_func) (info->stream, "%%acc%d", val);
839 break;
841 case 'g':
842 val = fetch_arg(buffer, place, 1, info);
843 (*info->fprintf_func) (info->stream, "%%accext%s", val==0 ? "01" : "23");
844 break;
846 case 'i':
847 val = fetch_arg(buffer, place, 2, info);
848 if (val == 1)
849 (*info->fprintf_func) (info->stream, "<<");
850 else if (val == 3)
851 (*info->fprintf_func) (info->stream, ">>");
852 else
853 return -1;
854 break;
856 case 'I':
857 /* Get coprocessor ID... */
858 val = fetch_arg (buffer, 'd', 3, info);
860 if (val != 1) /* Unusual coprocessor ID? */
861 (*info->fprintf_func) (info->stream, "(cpid=%d) ", val);
862 break;
864 case '4':
865 case '*':
866 case '~':
867 case '%':
868 case ';':
869 case '@':
870 case '!':
871 case '$':
872 case '?':
873 case '/':
874 case '&':
875 case '|':
876 case '<':
877 case '>':
878 case 'm':
879 case 'n':
880 case 'o':
881 case 'p':
882 case 'q':
883 case 'v':
884 case 'b':
885 case 'w':
886 case 'y':
887 case 'z':
888 if (place == 'd')
890 val = fetch_arg (buffer, 'x', 6, info);
891 val = ((val & 7) << 3) + ((val >> 3) & 7);
893 else
894 val = fetch_arg (buffer, 's', 6, info);
896 /* If the <ea> is invalid for *d, then reject this match. */
897 if (!m68k_valid_ea (*d, val))
898 return -1;
900 /* Get register number assuming address register. */
901 regno = (val & 7) + 8;
902 regname = reg_names[regno];
903 switch (val >> 3)
905 case 0:
906 (*info->fprintf_func) (info->stream, "%s", reg_names[val]);
907 break;
909 case 1:
910 (*info->fprintf_func) (info->stream, "%s", regname);
911 break;
913 case 2:
914 (*info->fprintf_func) (info->stream, "%s@", regname);
915 break;
917 case 3:
918 (*info->fprintf_func) (info->stream, "%s@+", regname);
919 break;
921 case 4:
922 (*info->fprintf_func) (info->stream, "%s@-", regname);
923 break;
925 case 5:
926 val = NEXTWORD (p);
927 (*info->fprintf_func) (info->stream, "%s@(%d)", regname, val);
928 break;
930 case 6:
931 p = print_indexed (regno, p, addr, info);
932 break;
934 case 7:
935 switch (val & 7)
937 case 0:
938 val = NEXTWORD (p);
939 (*info->print_address_func) (val, info);
940 break;
942 case 1:
943 uval = NEXTULONG (p);
944 (*info->print_address_func) (uval, info);
945 break;
947 case 2:
948 val = NEXTWORD (p);
949 (*info->fprintf_func) (info->stream, "%%pc@(");
950 (*info->print_address_func) (addr + val, info);
951 (*info->fprintf_func) (info->stream, ")");
952 break;
954 case 3:
955 p = print_indexed (-1, p, addr, info);
956 break;
958 case 4:
959 flt_p = 1; /* Assume it's a float... */
960 switch (place)
962 case 'b':
963 val = NEXTBYTE (p);
964 flt_p = 0;
965 break;
967 case 'w':
968 val = NEXTWORD (p);
969 flt_p = 0;
970 break;
972 case 'l':
973 val = NEXTLONG (p);
974 flt_p = 0;
975 break;
977 case 'f':
978 NEXTSINGLE (flval, p);
979 break;
981 case 'F':
982 NEXTDOUBLE (flval, p);
983 break;
985 case 'x':
986 NEXTEXTEND (flval, p);
987 break;
989 case 'p':
990 flval = NEXTPACKED (p);
991 break;
993 default:
994 return -1;
996 if (flt_p) /* Print a float? */
997 (*info->fprintf_func) (info->stream, "#%g", flval);
998 else
999 (*info->fprintf_func) (info->stream, "#%d", val);
1000 break;
1002 default:
1003 return -1;
1007 /* If place is '/', then this is the case of the mask bit for
1008 mac/emac loads. Now that the arg has been printed, grab the
1009 mask bit and if set, add a '&' to the arg. */
1010 if (place == '/')
1012 val = fetch_arg (buffer, place, 1, info);
1013 if (val)
1014 info->fprintf_func (info->stream, "&");
1016 break;
1018 case 'L':
1019 case 'l':
1020 if (place == 'w')
1022 char doneany;
1023 p1 = buffer + 2;
1024 val = NEXTWORD (p1);
1025 /* Move the pointer ahead if this point is farther ahead
1026 than the last. */
1027 p = p1 > p ? p1 : p;
1028 if (val == 0)
1030 (*info->fprintf_func) (info->stream, "#0");
1031 break;
1033 if (*d == 'l')
1035 int newval = 0;
1037 for (regno = 0; regno < 16; ++regno)
1038 if (val & (0x8000 >> regno))
1039 newval |= 1 << regno;
1040 val = newval;
1042 val &= 0xffff;
1043 doneany = 0;
1044 for (regno = 0; regno < 16; ++regno)
1045 if (val & (1 << regno))
1047 int first_regno;
1049 if (doneany)
1050 (*info->fprintf_func) (info->stream, "/");
1051 doneany = 1;
1052 (*info->fprintf_func) (info->stream, "%s", reg_names[regno]);
1053 first_regno = regno;
1054 while (val & (1 << (regno + 1)))
1055 ++regno;
1056 if (regno > first_regno)
1057 (*info->fprintf_func) (info->stream, "-%s",
1058 reg_names[regno]);
1061 else if (place == '3')
1063 /* `fmovem' insn. */
1064 char doneany;
1065 val = fetch_arg (buffer, place, 8, info);
1066 if (val == 0)
1068 (*info->fprintf_func) (info->stream, "#0");
1069 break;
1071 if (*d == 'l')
1073 int newval = 0;
1075 for (regno = 0; regno < 8; ++regno)
1076 if (val & (0x80 >> regno))
1077 newval |= 1 << regno;
1078 val = newval;
1080 val &= 0xff;
1081 doneany = 0;
1082 for (regno = 0; regno < 8; ++regno)
1083 if (val & (1 << regno))
1085 int first_regno;
1086 if (doneany)
1087 (*info->fprintf_func) (info->stream, "/");
1088 doneany = 1;
1089 (*info->fprintf_func) (info->stream, "%%fp%d", regno);
1090 first_regno = regno;
1091 while (val & (1 << (regno + 1)))
1092 ++regno;
1093 if (regno > first_regno)
1094 (*info->fprintf_func) (info->stream, "-%%fp%d", regno);
1097 else if (place == '8')
1099 /* fmoveml for FP status registers. */
1100 (*info->fprintf_func) (info->stream, "%s",
1101 fpcr_names[fetch_arg (buffer, place, 3,
1102 info)]);
1104 else
1105 return -2;
1106 break;
1108 case 'X':
1109 place = '8';
1110 case 'Y':
1111 case 'Z':
1112 case 'W':
1113 case '0':
1114 case '1':
1115 case '2':
1116 case '3':
1118 int val = fetch_arg (buffer, place, 5, info);
1119 char *name = 0;
1121 switch (val)
1123 case 2: name = "%tt0"; break;
1124 case 3: name = "%tt1"; break;
1125 case 0x10: name = "%tc"; break;
1126 case 0x11: name = "%drp"; break;
1127 case 0x12: name = "%srp"; break;
1128 case 0x13: name = "%crp"; break;
1129 case 0x14: name = "%cal"; break;
1130 case 0x15: name = "%val"; break;
1131 case 0x16: name = "%scc"; break;
1132 case 0x17: name = "%ac"; break;
1133 case 0x18: name = "%psr"; break;
1134 case 0x19: name = "%pcsr"; break;
1135 case 0x1c:
1136 case 0x1d:
1138 int break_reg = ((buffer[3] >> 2) & 7);
1140 (*info->fprintf_func)
1141 (info->stream, val == 0x1c ? "%%bad%d" : "%%bac%d",
1142 break_reg);
1144 break;
1145 default:
1146 (*info->fprintf_func) (info->stream, "<mmu register %d>", val);
1148 if (name)
1149 (*info->fprintf_func) (info->stream, "%s", name);
1151 break;
1153 case 'f':
1155 int fc = fetch_arg (buffer, place, 5, info);
1157 if (fc == 1)
1158 (*info->fprintf_func) (info->stream, "%%dfc");
1159 else if (fc == 0)
1160 (*info->fprintf_func) (info->stream, "%%sfc");
1161 else
1162 /* xgettext:c-format */
1163 (*info->fprintf_func) (info->stream, _("<function code %d>"), fc);
1165 break;
1167 case 'V':
1168 (*info->fprintf_func) (info->stream, "%%val");
1169 break;
1171 case 't':
1173 int level = fetch_arg (buffer, place, 3, info);
1175 (*info->fprintf_func) (info->stream, "%d", level);
1177 break;
1179 case 'u':
1181 short is_upper = 0;
1182 int reg = fetch_arg (buffer, place, 5, info);
1184 if (reg & 0x10)
1186 is_upper = 1;
1187 reg &= 0xf;
1189 (*info->fprintf_func) (info->stream, "%s%s",
1190 reg_half_names[reg],
1191 is_upper ? "u" : "l");
1193 break;
1195 default:
1196 return -2;
1199 return p - p0;
1202 /* Try to match the current instruction to best and if so, return the
1203 number of bytes consumed from the instruction stream, else zero. */
1205 static int
1206 match_insn_m68k (bfd_vma memaddr,
1207 disassemble_info * info,
1208 const struct m68k_opcode * best)
1210 unsigned char *save_p;
1211 unsigned char *p;
1212 const char *d;
1213 const char *args = best->args;
1215 struct private *priv = (struct private *) info->private_data;
1216 bfd_byte *buffer = priv->the_buffer;
1217 fprintf_ftype save_printer = info->fprintf_func;
1218 void (* save_print_address) (bfd_vma, struct disassemble_info *)
1219 = info->print_address_func;
1221 if (*args == '.')
1222 args++;
1224 /* Point at first word of argument data,
1225 and at descriptor for first argument. */
1226 p = buffer + 2;
1228 /* Figure out how long the fixed-size portion of the instruction is.
1229 The only place this is stored in the opcode table is
1230 in the arguments--look for arguments which specify fields in the 2nd
1231 or 3rd words of the instruction. */
1232 for (d = args; *d; d += 2)
1234 /* I don't think it is necessary to be checking d[0] here;
1235 I suspect all this could be moved to the case statement below. */
1236 if (d[0] == '#')
1238 if (d[1] == 'l' && p - buffer < 6)
1239 p = buffer + 6;
1240 else if (p - buffer < 4 && d[1] != 'C' && d[1] != '8')
1241 p = buffer + 4;
1244 if ((d[0] == 'L' || d[0] == 'l') && d[1] == 'w' && p - buffer < 4)
1245 p = buffer + 4;
1247 switch (d[1])
1249 case '1':
1250 case '2':
1251 case '3':
1252 case '7':
1253 case '8':
1254 case '9':
1255 case 'i':
1256 if (p - buffer < 4)
1257 p = buffer + 4;
1258 break;
1259 case '4':
1260 case '5':
1261 case '6':
1262 if (p - buffer < 6)
1263 p = buffer + 6;
1264 break;
1265 default:
1266 break;
1270 /* pflusha is an exceptions. It takes no arguments but is two words
1271 long. Recognize it by looking at the lower 16 bits of the mask. */
1272 if (p - buffer < 4 && (best->match & 0xFFFF) != 0)
1273 p = buffer + 4;
1275 /* lpstop is another exception. It takes a one word argument but is
1276 three words long. */
1277 if (p - buffer < 6
1278 && (best->match & 0xffff) == 0xffff
1279 && args[0] == '#'
1280 && args[1] == 'w')
1282 /* Copy the one word argument into the usual location for a one
1283 word argument, to simplify printing it. We can get away with
1284 this because we know exactly what the second word is, and we
1285 aren't going to print anything based on it. */
1286 p = buffer + 6;
1287 FETCH_DATA (info, p);
1288 buffer[2] = buffer[4];
1289 buffer[3] = buffer[5];
1292 FETCH_DATA (info, p);
1294 save_p = p;
1295 info->print_address_func = dummy_print_address;
1296 info->fprintf_func = (fprintf_ftype) dummy_printer;
1298 /* We scan the operands twice. The first time we don't print anything,
1299 but look for errors. */
1300 for (d = args; *d; d += 2)
1302 int eaten = print_insn_arg (d, buffer, p, memaddr + (p - buffer), info);
1304 if (eaten >= 0)
1305 p += eaten;
1306 else if (eaten == -1)
1308 info->fprintf_func = save_printer;
1309 info->print_address_func = save_print_address;
1310 return 0;
1312 else
1314 /* We must restore the print functions before trying to print the
1315 error message. */
1316 info->fprintf_func = save_printer;
1317 info->print_address_func = save_print_address;
1318 info->fprintf_func (info->stream,
1319 /* xgettext:c-format */
1320 _("<internal error in opcode table: %s %s>\n"),
1321 best->name, best->args);
1322 return 2;
1326 p = save_p;
1327 info->fprintf_func = save_printer;
1328 info->print_address_func = save_print_address;
1330 d = args;
1332 info->fprintf_func (info->stream, "%s", best->name);
1334 if (*d)
1335 info->fprintf_func (info->stream, " ");
1337 while (*d)
1339 p += print_insn_arg (d, buffer, p, memaddr + (p - buffer), info);
1340 d += 2;
1342 if (*d && *(d - 2) != 'I' && *d != 'k')
1343 info->fprintf_func (info->stream, ",");
1346 return p - buffer;
1349 /* Try to interpret the instruction at address MEMADDR as one that
1350 can execute on a processor with the features given by ARCH_MASK.
1351 If successful, print the instruction to INFO->STREAM and return
1352 its length in bytes. Return 0 otherwise. */
1354 static int
1355 m68k_scan_mask (bfd_vma memaddr, disassemble_info *info,
1356 unsigned int arch_mask)
1358 int i;
1359 const char *d;
1360 static const struct m68k_opcode **opcodes[16];
1361 static int numopcodes[16];
1362 int val;
1363 int major_opcode;
1365 struct private *priv = (struct private *) info->private_data;
1366 bfd_byte *buffer = priv->the_buffer;
1368 if (!opcodes[0])
1370 /* Speed up the matching by sorting the opcode
1371 table on the upper four bits of the opcode. */
1372 const struct m68k_opcode **opc_pointer[16];
1374 /* First count how many opcodes are in each of the sixteen buckets. */
1375 for (i = 0; i < m68k_numopcodes; i++)
1376 numopcodes[(m68k_opcodes[i].opcode >> 28) & 15]++;
1378 /* Then create a sorted table of pointers
1379 that point into the unsorted table. */
1380 opc_pointer[0] = xmalloc (sizeof (struct m68k_opcode *)
1381 * m68k_numopcodes);
1382 opcodes[0] = opc_pointer[0];
1384 for (i = 1; i < 16; i++)
1386 opc_pointer[i] = opc_pointer[i - 1] + numopcodes[i - 1];
1387 opcodes[i] = opc_pointer[i];
1390 for (i = 0; i < m68k_numopcodes; i++)
1391 *opc_pointer[(m68k_opcodes[i].opcode >> 28) & 15]++ = &m68k_opcodes[i];
1394 FETCH_DATA (info, buffer + 2);
1395 major_opcode = (buffer[0] >> 4) & 15;
1397 for (i = 0; i < numopcodes[major_opcode]; i++)
1399 const struct m68k_opcode *opc = opcodes[major_opcode][i];
1400 unsigned long opcode = opc->opcode;
1401 unsigned long match = opc->match;
1402 const char *args = opc->args;
1404 if (*args == '.')
1405 args++;
1407 if (((0xff & buffer[0] & (match >> 24)) == (0xff & (opcode >> 24)))
1408 && ((0xff & buffer[1] & (match >> 16)) == (0xff & (opcode >> 16)))
1409 /* Only fetch the next two bytes if we need to. */
1410 && (((0xffff & match) == 0)
1412 (FETCH_DATA (info, buffer + 4)
1413 && ((0xff & buffer[2] & (match >> 8)) == (0xff & (opcode >> 8)))
1414 && ((0xff & buffer[3] & match) == (0xff & opcode)))
1416 && (opc->arch & arch_mask) != 0)
1418 /* Don't use for printout the variants of divul and divsl
1419 that have the same register number in two places.
1420 The more general variants will match instead. */
1421 for (d = args; *d; d += 2)
1422 if (d[1] == 'D')
1423 break;
1425 /* Don't use for printout the variants of most floating
1426 point coprocessor instructions which use the same
1427 register number in two places, as above. */
1428 if (*d == '\0')
1429 for (d = args; *d; d += 2)
1430 if (d[1] == 't')
1431 break;
1433 /* Don't match fmovel with more than one register;
1434 wait for fmoveml. */
1435 if (*d == '\0')
1437 for (d = args; *d; d += 2)
1439 if (d[0] == 's' && d[1] == '8')
1441 val = fetch_arg (buffer, d[1], 3, info);
1442 if ((val & (val - 1)) != 0)
1443 break;
1448 /* Don't match FPU insns with non-default coprocessor ID. */
1449 if (*d == '\0')
1451 for (d = args; *d; d += 2)
1453 if (d[0] == 'I')
1455 val = fetch_arg (buffer, 'd', 3, info);
1456 if (val != 1)
1457 break;
1462 if (*d == '\0')
1463 if ((val = match_insn_m68k (memaddr, info, opc)))
1464 return val;
1467 return 0;
1470 /* Print the m68k instruction at address MEMADDR in debugged memory,
1471 on INFO->STREAM. Returns length of the instruction, in bytes. */
1474 print_insn_m68k (bfd_vma memaddr, disassemble_info *info)
1476 unsigned int arch_mask;
1477 struct private priv;
1478 int val;
1480 bfd_byte *buffer = priv.the_buffer;
1482 /* Save these printing functions in case we need to restore them
1483 later. */
1484 fprintf_ftype save_printer = info->fprintf_func;
1485 void (* save_print_address) (bfd_vma, struct disassemble_info *)
1486 = info->print_address_func;
1488 info->private_data = (PTR) &priv;
1489 /* Tell objdump to use two bytes per chunk
1490 and six bytes per line for displaying raw data. */
1491 info->bytes_per_chunk = 2;
1492 info->bytes_per_line = 6;
1493 info->display_endian = BFD_ENDIAN_BIG;
1494 priv.max_fetched = priv.the_buffer;
1495 priv.insn_start = memaddr;
1497 if (setjmp (priv.bailout) != 0)
1499 /* longjmp may be called while these printing functions are
1500 temporarily replaced with dummy functions. Restore them
1501 before we leave.
1503 Admittedly, this save-and-restore operation is somewhat ugly
1504 in that we are exposing the fact that match_insn_m68k
1505 temporarily replaces insn->fprintf_func and
1506 insn->print_address_func. Perhaps, a real fix is to report a
1507 FETCH_DATA failure with a return value of some sort, without
1508 using setjmp/longjmp. A better fix may be to teach the m68k
1509 disassembler do its job without temporarily replacing
1510 insn->fprintf_func and insn->print_address_func, but that's a
1511 task for another day. */
1512 info->fprintf_func = save_printer;
1513 info->print_address_func = save_print_address;
1515 /* Error return. */
1516 return -1;
1519 arch_mask = bfd_m68k_mach_to_features (info->mach);
1520 if (!arch_mask)
1522 /* First try printing an m680x0 instruction. Try printing a Coldfire
1523 one if that fails. */
1524 val = m68k_scan_mask (memaddr, info, m68k_mask);
1525 if (val)
1526 return val;
1528 val = m68k_scan_mask (memaddr, info, mcf_mask);
1529 if (val)
1530 return val;
1532 else
1534 val = m68k_scan_mask (memaddr, info, arch_mask);
1535 if (val)
1536 return val;
1539 /* Handle undefined instructions. */
1540 info->fprintf_func (info->stream, "0%o", (buffer[0] << 8) + buffer[1]);
1541 return 2;