1 /* Print m68k instructions for objdump
2 Copyright (C) 1986, 1987, 1989, 1991 Free Software Foundation, Inc.
5 This file is part of the binutils.
7 The binutils are free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 The binutils are distributed in the hope that they will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with the binutils; see the file COPYING. If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
25 #include "opcode/m68k.h"
27 /* Sign-extend an (unsigned char). */
29 #define COERCE_SIGNED_CHAR(ch) ((signed char)(ch))
31 #define COERCE_SIGNED_CHAR(ch) ((int)(((ch) ^ 0x80) & 0xFF) - 128)
34 extern void print_address();
36 /* 68k instructions are never longer than this many bytes. */
39 /* Number of elements in the opcode table. */
40 #define NOPCODES (sizeof m68k_opcodes / sizeof m68k_opcodes[0])
42 extern char *reg_names[];
43 char *fpcr_names[] = { "", "fpiar", "fpsr", "fpiar/fpsr", "fpcr",
44 "fpiar/fpcr", "fpsr/fpcr", "fpiar-fpcr"};
46 char *reg_names[] = {"d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7", "a0", "a1", "a2", "a3", "a4", "a5", "fp", "sp", "ps", "pc"};
47 static unsigned char *print_insn_arg ();
48 static unsigned char *print_indexed ();
49 static void print_base ();
50 static int fetch_arg ();
52 #define NEXTBYTE(p) (p += 2, COERCE_SIGNED_CHAR(p[-1]))
55 (p += 2, ((((char *)p)[-2]) << 8) + p[-1])
58 (p += 4, (((((p[-4] << 8) + p[-3]) << 8) + p[-2]) << 8) + p[-1])
60 #define NEXTSINGLE(p) \
61 (p += 4, *((float *)(p - 4)))
63 #define NEXTDOUBLE(p) \
64 (p += 8, *((double *)(p - 8)))
66 #define NEXTEXTEND(p) \
67 (p += 12, 0.0) /* Need a function to convert from extended to double
70 #define NEXTPACKED(p) \
71 (p += 12, 0.0) /* Need a function to convert from packed to double
72 precision. Actually, it's easier to print a
73 packed number than a double anyway, so maybe
74 there should be a special case to handle this... */
76 /* Print the m68k instruction at address MEMADDR in debugged memory,
77 on STREAM. Returns length of the instruction, in bytes. */
80 print_insn_m68k(addr, buffer, stream)
82 unsigned char *buffer;
85 register unsigned int i;
86 register unsigned char *p;
88 register unsigned int bestmask;
95 for (i = 0; i < NOPCODES; i++)
97 register unsigned int opcode = m68k_opcodes[i].opcode;
98 register unsigned int match = m68k_opcodes[i].match;
99 if (((0xff & buffer[0] & (match >> 24)) == (0xff & (opcode >> 24)))
100 && ((0xff & buffer[1] & (match >> 16)) == (0xff & (opcode >> 16)))
101 && ((0xff & buffer[2] & (match >> 8)) == (0xff & (opcode >> 8)))
102 && ((0xff & buffer[3] & match) == (0xff & opcode)))
104 /* Don't use for printout the variants of divul and divsl
105 that have the same register number in two places.
106 The more general variants will match instead. */
107 for (d = m68k_opcodes[i].args; *d; d += 2)
111 /* Don't use for printout the variants of most floating
112 point coprocessor instructions which use the same
113 register number in two places, as above. */
115 for (d = m68k_opcodes[i].args; *d; d += 2)
119 if (*d == 0 && match > bestmask)
127 /* Handle undefined instructions. */
130 fprintf (stream, "0%o", (unsigned) (buffer[0] << 8) + buffer[1]);
134 fprintf (stream, "%s", m68k_opcodes[best].name);
136 /* Point at first word of argument data,
137 and at descriptor for first argument. */
140 /* Why do this this way? -MelloN */
141 for (d = m68k_opcodes[best].args; *d; d += 2)
145 if (d[1] == 'l' && p - buffer < 6)
147 else if (p - buffer < 4 && d[1] != 'C' && d[1] != '8' )
150 if (d[1] >= '1' && d[1] <= '3' && p - buffer < 4)
152 if (d[1] >= '4' && d[1] <= '6' && p - buffer < 6)
154 if ((d[0] == 'L' || d[0] == 'l') && d[1] == 'w' && p - buffer < 4)
158 d = m68k_opcodes[best].args;
165 p = print_insn_arg (d, buffer, p, addr + p - buffer, stream);
167 if (*d && *(d - 2) != 'I' && *d != 'k')
173 static unsigned char *
174 print_insn_arg (d, buffer, p, addr, stream)
176 unsigned char *buffer;
177 register unsigned char *p;
178 bfd_vma addr; /* PC for this arg to be relative to */
181 register int val = 0;
182 register int place = d[1];
184 register char *regname;
185 register unsigned char *p1;
186 register double flval = 0;
192 fprintf (stream, "ccr");
196 fprintf (stream, "sr");
200 fprintf (stream, "usp");
205 static struct { char *name; int value; } names[]
206 = {{"sfc", 0x000}, {"dfc", 0x001}, {"cacr", 0x002},
207 {"usp", 0x800}, {"vbr", 0x801}, {"caar", 0x802},
208 {"msp", 0x803}, {"isp", 0x804}};
210 val = fetch_arg (buffer, place, 12);
211 for (regno = sizeof names / sizeof names[0] - 1; regno >= 0; regno--)
212 if (names[regno].value == val)
214 fprintf (stream, names[regno].name);
218 fprintf (stream, "%d", val);
223 val = fetch_arg (buffer, place, 3);
224 if (val == 0) val = 8;
225 fprintf (stream, "#%d", val);
229 val = fetch_arg (buffer, place, 8);
232 fprintf (stream, "#%d", val);
236 val = fetch_arg (buffer, place, 4);
237 fprintf (stream, "#%d", val);
241 fprintf (stream, "%s", reg_names[fetch_arg (buffer, place, 3)]);
245 fprintf (stream, "%s",
246 reg_names[fetch_arg (buffer, place, 3) + 010]);
250 fprintf (stream, "%s", reg_names[fetch_arg (buffer, place, 4)]);
254 fprintf (stream, "fp%d", fetch_arg (buffer, place, 3));
258 val = fetch_arg (buffer, place, 6);
260 fprintf (stream, "%s", reg_names [val & 7]);
262 fprintf (stream, "%d", val);
266 fprintf (stream, "%s@+",
267 reg_names[fetch_arg (buffer, place, 3) + 8]);
271 fprintf (stream, "%s@-",
272 reg_names[fetch_arg (buffer, place, 3) + 8]);
277 fprintf (stream, "{%s}", reg_names[fetch_arg (buffer, place, 3)]);
278 else if (place == 'C')
280 val = fetch_arg (buffer, place, 7);
281 if ( val > 63 ) /* This is a signed constant. */
283 fprintf (stream, "{#%d}", val);
286 fprintf(stderr, "Invalid arg format in opcode table: \"%c%c\".",
292 p1 = buffer + (*d == '#' ? 2 : 4);
294 val = fetch_arg (buffer, place, 4);
295 else if (place == 'C')
296 val = fetch_arg (buffer, place, 7);
297 else if (place == '8')
298 val = fetch_arg (buffer, place, 3);
299 else if (place == '3')
300 val = fetch_arg (buffer, place, 8);
301 else if (place == 'b')
303 else if (place == 'w')
305 else if (place == 'l')
308 fprintf(stderr, "Invalid arg format in opcode table: \"%c%c\".",
310 fprintf (stream, "#%d", val);
316 else if (place == 'B')
317 val = COERCE_SIGNED_CHAR(buffer[1]);
318 else if (place == 'w' || place == 'W')
320 else if (place == 'l' || place == 'L')
322 else if (place == 'g')
324 val = ((char *)buffer)[1];
330 else if (place == 'c')
332 if (buffer[1] & 0x40) /* If bit six is one, long offset */
338 fprintf(stderr, "Invalid arg format in opcode table: \"%c%c\".",
340 print_address (addr + val, stream);
345 fprintf (stream, "%s@(%d)",
346 reg_names[fetch_arg (buffer, place, 3)], val);
350 fprintf (stream, "%s",
351 fpcr_names[fetch_arg (buffer, place, 3)]);
355 val = fetch_arg (buffer, 'd', 3); /* Get coprocessor ID... */
356 if (val != 1) /* Unusual coprocessor ID? */
357 fprintf (stream, "(cpid=%d) ", val);
359 p += 2; /* Skip coprocessor extended operands */
375 val = fetch_arg (buffer, 'x', 6);
376 val = ((val & 7) << 3) + ((val >> 3) & 7);
379 val = fetch_arg (buffer, 's', 6);
381 /* Get register number assuming address register. */
382 regno = (val & 7) + 8;
383 regname = reg_names[regno];
387 fprintf (stream, "%s", reg_names[val]);
391 fprintf (stream, "%s", regname);
395 fprintf (stream, "%s@", regname);
399 fprintf (stream, "%s@+", regname);
403 fprintf (stream, "%s@-", regname);
408 fprintf (stream, "%s@(%d)", regname, val);
412 p = print_indexed (regno, p, addr, stream);
420 fprintf (stream, "@#");
421 print_address (val, stream);
426 fprintf (stream, "@#");
427 print_address (val, stream);
432 print_address (addr + val, stream);
436 p = print_indexed (-1, p, addr, stream);
440 flt_p = 1; /* Assume it's a float... */
459 flval = NEXTSINGLE(p);
463 flval = NEXTDOUBLE(p);
467 flval = NEXTEXTEND(p);
471 flval = NEXTPACKED(p);
475 fprintf(stderr, "Invalid arg format in opcode table: \"%c%c\".",
478 if ( flt_p ) /* Print a float? */
479 fprintf (stream, "#%g", flval);
481 fprintf (stream, "#%d", val);
485 fprintf (stream, "<invalid address mode 0%o>", (unsigned) val);
497 /* Move the pointer ahead if this point is farther ahead
502 fputs ("#0", stream);
507 register int newval = 0;
508 for (regno = 0; regno < 16; ++regno)
509 if (val & (0x8000 >> regno))
510 newval |= 1 << regno;
515 for (regno = 0; regno < 16; ++regno)
516 if (val & (1 << regno))
522 fprintf (stream, "%s", reg_names[regno]);
524 while (val & (1 << (regno + 1)))
526 if (regno > first_regno)
527 fprintf (stream, "-%s", reg_names[regno]);
530 else if (place == '3')
534 val = fetch_arg (buffer, place, 8);
537 fputs ("#0", stream);
542 register int newval = 0;
543 for (regno = 0; regno < 8; ++regno)
544 if (val & (0x80 >> regno))
545 newval |= 1 << regno;
550 for (regno = 0; regno < 8; ++regno)
551 if (val & (1 << regno))
557 fprintf (stream, "fp%d", regno);
559 while (val & (1 << (regno + 1)))
561 if (regno > first_regno)
562 fprintf (stream, "-fp%d", regno);
570 fprintf(stderr, "Invalid arg format in opcode table: \"%c\".", *d);
573 return (unsigned char *) p;
576 /* Fetch BITS bits from a position in the instruction specified by CODE.
577 CODE is a "place to put an argument", or 'x' for a destination
578 that is a general address (mode and register).
579 BUFFER contains the instruction. */
582 fetch_arg (buffer, code, bits)
583 unsigned char *buffer;
587 register int val = 0;
594 case 'd': /* Destination, for register or quick. */
595 val = (buffer[0] << 8) + buffer[1];
599 case 'x': /* Destination, for general arg */
600 val = (buffer[0] << 8) + buffer[1];
605 val = (buffer[3] >> 4);
613 val = (buffer[2] << 8) + buffer[3];
618 val = (buffer[2] << 8) + buffer[3];
624 val = (buffer[2] << 8) + buffer[3];
628 val = (buffer[4] << 8) + buffer[5];
633 val = (buffer[4] << 8) + buffer[5];
638 val = (buffer[4] << 8) + buffer[5];
642 val = (buffer[2] << 8) + buffer[3];
647 val = (buffer[2] << 8) + buffer[3];
677 /* Print an indexed argument. The base register is BASEREG (-1 for pc).
678 P points to extension word, in buffer.
679 ADDR is the nominal core address of that extension word. */
681 static unsigned char *
682 print_indexed (basereg, p, addr, stream)
689 static char *scales[] = {"", "*2", "*4", "*8"};
690 register int base_disp;
691 register int outer_disp;
696 /* Generate the text for the index register.
697 Where this will be output is not yet determined. */
698 sprintf (buf, "[%s.%c%s]",
699 reg_names[(word >> 12) & 0xf],
700 (word & 0x800) ? 'l' : 'w',
701 scales[(word >> 9) & 3]);
703 /* Handle the 68000 style of indexing. */
705 if ((word & 0x100) == 0)
708 ((word & 0x80) ? word | 0xff00 : word & 0xff)
709 + ((basereg == -1) ? addr : 0),
715 /* Handle the generalized kind. */
716 /* First, compute the displacement to add to the base register. */
723 switch ((word >> 4) & 3)
726 base_disp = NEXTWORD (p);
729 base_disp = NEXTLONG (p);
734 /* Handle single-level case (not indirect) */
738 print_base (basereg, base_disp, stream);
743 /* Two level. Compute displacement to add after indirection. */
749 outer_disp = NEXTWORD (p);
752 outer_disp = NEXTLONG (p);
755 fprintf (stream, "%d(", outer_disp);
756 print_base (basereg, base_disp, stream);
758 /* If postindexed, print the closeparen before the index. */
760 fprintf (stream, ")%s", buf);
761 /* If preindexed, print the closeparen after the index. */
763 fprintf (stream, "%s)", buf);
768 /* Print a base register REGNO and displacement DISP, on STREAM.
769 REGNO = -1 for pc, -2 for none (suppressed). */
772 print_base (regno, disp, stream)
778 fprintf (stream, "%d", disp);
779 else if (regno == -1)
780 fprintf (stream, "0x%x", (unsigned) disp);
782 fprintf (stream, "%d(%s)", disp, reg_names[regno]);