1 /* Print Motorola 68k instructions for GDB, the GNU debugger.
2 Copyright 1986, 1987, 1989, 1991 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program 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 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public 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., 675 Mass Ave, Cambridge, MA 02139, USA. */
22 #include "opcode/m68k.h"
25 /* Local function prototypes */
28 fetch_arg PARAMS ((unsigned char *, int, int));
31 print_base PARAMS ((int, int, FILE *));
33 static unsigned char *
34 print_indexed PARAMS ((int, unsigned char *, CORE_ADDR, FILE *));
36 static unsigned char *
37 print_insn_arg PARAMS ((char *, unsigned char *, unsigned char *, CORE_ADDR,
40 /* 68k instructions are never longer than this many bytes. */
43 /* Number of elements in the opcode table. */
44 #define NOPCODES (sizeof m68k_opcodes / sizeof m68k_opcodes[0])
46 extern char *reg_names[];
47 char *fpcr_names[] = { "", "fpiar", "fpsr", "fpiar/fpsr", "fpcr",
48 "fpiar/fpcr", "fpsr/fpcr", "fpiar-fpcr"};
51 #define NEXTBYTE(p) (p += 2, ((char *)p)[-1])
54 (p += 2, ((((char *)p)[-2]) << 8) + p[-1])
57 (p += 4, (((((p[-4] << 8) + p[-3]) << 8) + p[-2]) << 8) + p[-1])
59 #define NEXTSINGLE(p) \
60 (p += 4, *((float *)(p - 4)))
62 #define NEXTDOUBLE(p) \
63 (p += 8, *((double *)(p - 8)))
65 #define NEXTEXTEND(p) \
66 (p += 12, 0.0) /* Need a function to convert from extended to double
69 #define NEXTPACKED(p) \
70 (p += 12, 0.0) /* Need a function to convert from packed to double
71 precision. Actually, it's easier to print a
72 packed number than a double anyway, so maybe
73 there should be a special case to handle this... */
75 /* Print the m68k instruction at address MEMADDR in debugged memory,
76 on STREAM. Returns length of the instruction, in bytes. */
79 print_insn (memaddr, stream)
83 unsigned char buffer[MAXLEN];
85 register unsigned char *p;
87 register int bestmask;
90 read_memory (memaddr, (char *) buffer, MAXLEN);
94 for (i = 0; i < NOPCODES; i++)
96 register unsigned int opcode = m68k_opcodes[i].opcode;
97 register unsigned int match = m68k_opcodes[i].match;
98 if (((0xff & buffer[0] & (match >> 24)) == (0xff & (opcode >> 24)))
99 && ((0xff & buffer[1] & (match >> 16)) == (0xff & (opcode >> 16)))
100 && ((0xff & buffer[2] & (match >> 8)) == (0xff & (opcode >> 8)))
101 && ((0xff & buffer[3] & match) == (0xff & opcode)))
103 /* Don't use for printout the variants of divul and divsl
104 that have the same register number in two places.
105 The more general variants will match instead. */
106 for (d = m68k_opcodes[i].args; *d; d += 2)
110 /* Don't use for printout the variants of most floating
111 point coprocessor instructions which use the same
112 register number in two places, as above. */
114 for (d = m68k_opcodes[i].args; *d; d += 2)
118 if (*d == 0 && match > bestmask)
126 /* Handle undefined instructions. */
129 fprintf_filtered (stream, "0%o", (buffer[0] << 8) + buffer[1]);
133 fprintf_filtered (stream, "%s", m68k_opcodes[best].name);
135 /* Point at first word of argument data,
136 and at descriptor for first argument. */
139 /* Why do this this way? -MelloN */
140 for (d = m68k_opcodes[best].args; *d; d += 2)
144 if (d[1] == 'l' && p - buffer < 6)
146 else if (p - buffer < 4 && d[1] != 'C' && d[1] != '8' )
149 if (d[1] >= '1' && d[1] <= '3' && p - buffer < 4)
151 if (d[1] >= '4' && d[1] <= '6' && p - buffer < 6)
153 if ((d[0] == 'L' || d[0] == 'l') && d[1] == 'w' && p - buffer < 4)
157 d = m68k_opcodes[best].args;
160 fputs_filtered (" ", stream);
164 p = print_insn_arg (d, buffer, p, memaddr + p - buffer, stream);
166 if (*d && *(d - 2) != 'I' && *d != 'k')
167 fputs_filtered (",", stream);
172 static unsigned char *
173 print_insn_arg (d, buffer, p, addr, stream)
175 unsigned char *buffer;
176 register unsigned char *p;
177 CORE_ADDR addr; /* PC for this arg to be relative to */
181 register int place = d[1];
183 register char *regname;
184 register unsigned char *p1;
185 register double flval;
191 fprintf_filtered (stream, "ccr");
195 fprintf_filtered (stream, "sr");
199 fprintf_filtered (stream, "usp");
204 static struct { char *name; int value; } names[]
205 = {{"sfc", 0x000}, {"dfc", 0x001}, {"cacr", 0x002},
206 {"usp", 0x800}, {"vbr", 0x801}, {"caar", 0x802},
207 {"msp", 0x803}, {"isp", 0x804}};
209 val = fetch_arg (buffer, place, 12);
210 for (regno = sizeof names / sizeof names[0] - 1; regno >= 0; regno--)
211 if (names[regno].value == val)
213 fprintf_filtered (stream, names[regno].name);
217 fprintf_filtered (stream, "%d", val);
222 val = fetch_arg (buffer, place, 3);
223 /* 0 means 8, except for the bkpt instruction... */
224 if (val == 0 && d[1] != 's')
226 fprintf_filtered (stream, "#%d", val);
230 val = fetch_arg (buffer, place, 8);
233 fprintf_filtered (stream, "#%d", val);
237 val = fetch_arg (buffer, place, 4);
238 fprintf_filtered (stream, "#%d", val);
242 fprintf_filtered (stream, "%s", reg_names[fetch_arg (buffer, place, 3)]);
246 fprintf_filtered (stream, "%s",
247 reg_names[fetch_arg (buffer, place, 3) + 010]);
251 fprintf_filtered (stream, "%s", reg_names[fetch_arg (buffer, place, 4)]);
255 fprintf_filtered (stream, "fp%d", fetch_arg (buffer, place, 3));
259 val = fetch_arg (buffer, place, 6);
261 fprintf_filtered (stream, "%s", reg_names [val & 7]);
263 fprintf_filtered (stream, "%d", val);
267 fprintf_filtered (stream, "%s@+",
268 reg_names[fetch_arg (buffer, place, 3) + 8]);
272 fprintf_filtered (stream, "%s@-",
273 reg_names[fetch_arg (buffer, place, 3) + 8]);
278 fprintf_filtered (stream, "{%s}", reg_names[fetch_arg (buffer, place, 3)]);
279 else if (place == 'C')
281 val = fetch_arg (buffer, place, 7);
282 if ( val > 63 ) /* This is a signed constant. */
284 fprintf_filtered (stream, "{#%d}", val);
287 error ("Invalid arg format in opcode table: \"%c%c\".",
293 p1 = buffer + (*d == '#' ? 2 : 4);
295 val = fetch_arg (buffer, place, 4);
296 else if (place == 'C')
297 val = fetch_arg (buffer, place, 7);
298 else if (place == '8')
299 val = fetch_arg (buffer, place, 3);
300 else if (place == '3')
301 val = fetch_arg (buffer, place, 8);
302 else if (place == 'b')
304 else if (place == 'w')
306 else if (place == 'l')
309 error ("Invalid arg format in opcode table: \"%c%c\".",
311 fprintf_filtered (stream, "#%d", val);
317 else if (place == 'B')
318 val = ((char *)buffer)[1];
319 else if (place == 'w' || place == 'W')
321 else if (place == 'l' || place == 'L')
323 else if (place == 'g')
325 val = ((char *)buffer)[1];
331 else if (place == 'c')
333 if (buffer[1] & 0x40) /* If bit six is one, long offset */
339 error ("Invalid arg format in opcode table: \"%c%c\".",
342 print_address (addr + val, stream);
347 fprintf_filtered (stream, "%s@(%d)",
348 reg_names[fetch_arg (buffer, place, 3)], val);
352 fprintf_filtered (stream, "%s",
353 fpcr_names[fetch_arg (buffer, place, 3)]);
357 val = fetch_arg (buffer, 'd', 3); /* Get coprocessor ID... */
358 if (val != 1) /* Unusual coprocessor ID? */
359 fprintf_filtered (stream, "(cpid=%d) ", val);
361 p += 2; /* Skip coprocessor extended operands */
377 val = fetch_arg (buffer, 'x', 6);
378 val = ((val & 7) << 3) + ((val >> 3) & 7);
381 val = fetch_arg (buffer, 's', 6);
383 /* Get register number assuming address register. */
384 regno = (val & 7) + 8;
385 regname = reg_names[regno];
389 fprintf_filtered (stream, "%s", reg_names[val]);
393 fprintf_filtered (stream, "%s", regname);
397 fprintf_filtered (stream, "%s@", regname);
401 fprintf_filtered (stream, "%s@+", regname);
405 fprintf_filtered (stream, "%s@-", regname);
410 fprintf_filtered (stream, "%s@(%d)", regname, val);
414 p = print_indexed (regno, p, addr, stream);
422 fprintf_filtered (stream, "@#");
423 print_address (val, stream);
428 fprintf_filtered (stream, "@#");
429 print_address (val, stream);
434 print_address (addr + val, stream);
438 p = print_indexed (-1, p, addr, stream);
442 flt_p = 1; /* Assume it's a float... */
461 flval = NEXTSINGLE(p);
465 flval = NEXTDOUBLE(p);
469 flval = NEXTEXTEND(p);
473 flval = NEXTPACKED(p);
477 error ("Invalid arg format in opcode table: \"%c%c\".",
480 if ( flt_p ) /* Print a float? */
481 fprintf_filtered (stream, "#%g", flval);
483 fprintf_filtered (stream, "#%d", val);
487 fprintf_filtered (stream, "<invalid address mode 0%o>", val);
499 /* Move the pointer ahead if this point is farther ahead
504 fputs_filtered ("#0", stream);
509 register int newval = 0;
510 for (regno = 0; regno < 16; ++regno)
511 if (val & (0x8000 >> regno))
512 newval |= 1 << regno;
517 for (regno = 0; regno < 16; ++regno)
518 if (val & (1 << regno))
522 fputs_filtered ("/", stream);
524 fprintf_filtered (stream, "%s", reg_names[regno]);
526 while (val & (1 << (regno + 1)))
528 if (regno > first_regno)
529 fprintf_filtered (stream, "-%s", reg_names[regno]);
532 else if (place == '3')
536 val = fetch_arg (buffer, place, 8);
539 fputs_filtered ("#0", stream);
544 register int newval = 0;
545 for (regno = 0; regno < 8; ++regno)
546 if (val & (0x80 >> regno))
547 newval |= 1 << regno;
552 for (regno = 0; regno < 8; ++regno)
553 if (val & (1 << regno))
557 fputs_filtered ("/", stream);
559 fprintf_filtered (stream, "fp%d", regno);
561 while (val & (1 << (regno + 1)))
563 if (regno > first_regno)
564 fprintf_filtered (stream, "-fp%d", regno);
572 error ("Invalid arg format in opcode table: \"%c\".", *d);
575 return (unsigned char *) p;
578 /* Fetch BITS bits from a position in the instruction specified by CODE.
579 CODE is a "place to put an argument", or 'x' for a destination
580 that is a general address (mode and register).
581 BUFFER contains the instruction. */
584 fetch_arg (buffer, code, bits)
585 unsigned char *buffer;
596 case 'd': /* Destination, for register or quick. */
597 val = (buffer[0] << 8) + buffer[1];
601 case 'x': /* Destination, for general arg */
602 val = (buffer[0] << 8) + buffer[1];
607 val = (buffer[3] >> 4);
615 val = (buffer[2] << 8) + buffer[3];
620 val = (buffer[2] << 8) + buffer[3];
626 val = (buffer[2] << 8) + buffer[3];
630 val = (buffer[4] << 8) + buffer[5];
635 val = (buffer[4] << 8) + buffer[5];
640 val = (buffer[4] << 8) + buffer[5];
644 val = (buffer[2] << 8) + buffer[3];
649 val = (buffer[2] << 8) + buffer[3];
678 /* Print an indexed argument. The base register is BASEREG (-1 for pc).
679 P points to extension word, in buffer.
680 ADDR is the nominal core address of that extension word. */
682 static unsigned char *
683 print_indexed (basereg, p, addr, stream)
690 static char *scales[] = {"", "*2", "*4", "*8"};
691 register int base_disp;
692 register int outer_disp;
697 /* Generate the text for the index register.
698 Where this will be output is not yet determined. */
699 sprintf (buf, "[%s.%c%s]",
700 reg_names[(word >> 12) & 0xf],
701 (word & 0x800) ? 'l' : 'w',
702 scales[(word >> 9) & 3]);
704 /* Handle the 68000 style of indexing. */
706 if ((word & 0x100) == 0)
709 ((word & 0x80) ? word | 0xff00 : word & 0xff)
710 + ((basereg == -1) ? addr : 0),
712 fputs_filtered (buf, stream);
716 /* Handle the generalized kind. */
717 /* First, compute the displacement to add to the base register. */
724 switch ((word >> 4) & 3)
727 base_disp = NEXTWORD (p);
730 base_disp = NEXTLONG (p);
735 /* Handle single-level case (not indirect) */
739 print_base (basereg, base_disp, stream);
740 fputs_filtered (buf, stream);
744 /* Two level. Compute displacement to add after indirection. */
750 outer_disp = NEXTWORD (p);
753 outer_disp = NEXTLONG (p);
756 fprintf_filtered (stream, "%d(", outer_disp);
757 print_base (basereg, base_disp, stream);
759 /* If postindexed, print the closeparen before the index. */
761 fprintf_filtered (stream, ")%s", buf);
762 /* If preindexed, print the closeparen after the index. */
764 fprintf_filtered (stream, "%s)", buf);
769 /* Print a base register REGNO and displacement DISP, on STREAM.
770 REGNO = -1 for pc, -2 for none (suppressed). */
773 print_base (regno, disp, stream)
779 fprintf_filtered (stream, "%d", disp);
780 else if (regno == -1)
781 fprintf_filtered (stream, "0x%x", disp);
783 fprintf_filtered (stream, "%d(%s)", disp, reg_names[regno]);