1 /* Print Motorola 68k instructions for GDB, the GNU debugger.
2 Copyright 1986, 1987, 1989, 1991, 1992 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"
24 #include "ieee-float.h"
26 /* Local function prototypes */
29 fetch_arg
PARAMS ((unsigned char *, int, int));
32 print_base
PARAMS ((int, int, FILE *));
34 static unsigned char *
35 print_indexed
PARAMS ((int, unsigned char *, CORE_ADDR
, FILE *));
37 static unsigned char *
38 print_insn_arg
PARAMS ((char *, unsigned char *, unsigned char *, CORE_ADDR
,
41 /* 68k instructions are never longer than this many bytes. */
44 /* Number of elements in the opcode table. */
45 #define NOPCODES (sizeof m68k_opcodes / sizeof m68k_opcodes[0])
47 extern char *reg_names
[];
48 char *fpcr_names
[] = { "", "fpiar", "fpsr", "fpiar/fpsr", "fpcr",
49 "fpiar/fpcr", "fpsr/fpcr", "fpiar-fpcr"};
51 /* Define accessors for 68K's 1, 2, and 4-byte signed quantities.
52 The _SHIFT values move the quantity to the high order end of an
53 `int' value, so it will sign-extend. Probably a few more casts
54 are needed to make it compile without warnings on finicky systems. */
55 #define BITS_PER_BYTE 8
56 #define BYTE_SHIFT (BITS_PER_BYTE * ((sizeof (int)) - 1))
57 #define WORD_SHIFT (BITS_PER_BYTE * ((sizeof (int)) - 2))
58 #define LONG_SHIFT (BITS_PER_BYTE * ((sizeof (int)) - 4))
60 #define NEXTBYTE(p) (p += 2, ((int)(p[-1]) << BYTE_SHIFT) >> BYTE_SHIFT)
63 (p += 2, (((int)((p[-2] << 8) + p[-1])) << WORD_SHIFT) >> WORD_SHIFT)
66 (p += 4, (((int)((((((p[-4] << 8) + p[-3]) << 8) + p[-2]) << 8) + p[-1])) \
67 << LONG_SHIFT) >> LONG_SHIFT)
69 /* Ecch -- assumes host == target float formats. FIXME. */
70 #define NEXTSINGLE(p) \
71 (p += 4, *((float *)(p - 4)))
73 #define NEXTDOUBLE(p) \
74 (p += 8, *((double *)(p - 8)))
76 /* Print the m68k instruction at address MEMADDR in debugged memory,
77 on STREAM. Returns length of the instruction, in bytes. */
80 print_insn (memaddr
, stream
)
84 unsigned char buffer
[MAXLEN
];
86 register unsigned char *p
;
88 register int bestmask
;
91 read_memory (memaddr
, (char *) buffer
, MAXLEN
);
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_filtered (stream
, "0%o", (buffer
[0] << 8) + buffer
[1]);
134 fprintf_filtered (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
;
161 fputs_filtered (" ", stream
);
165 p
= print_insn_arg (d
, buffer
, p
, memaddr
+ p
- buffer
, stream
);
167 if (*d
&& *(d
- 2) != 'I' && *d
!= 'k')
168 fputs_filtered (",", stream
);
173 static unsigned char *
174 print_insn_arg (d
, buffer
, p
, addr
, stream
)
176 unsigned char *buffer
;
177 register unsigned char *p
;
178 CORE_ADDR addr
; /* PC for this arg to be relative to */
182 register int place
= d
[1];
184 register char *regname
;
185 register unsigned char *p1
;
192 fprintf_filtered (stream
, "ccr");
196 fprintf_filtered (stream
, "sr");
200 fprintf_filtered (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_filtered (stream
, names
[regno
].name
);
218 fprintf_filtered (stream
, "%d", val
);
223 val
= fetch_arg (buffer
, place
, 3);
224 /* 0 means 8, except for the bkpt instruction... */
225 if (val
== 0 && d
[1] != 's')
227 fprintf_filtered (stream
, "#%d", val
);
231 val
= fetch_arg (buffer
, place
, 8);
234 fprintf_filtered (stream
, "#%d", val
);
238 val
= fetch_arg (buffer
, place
, 4);
239 fprintf_filtered (stream
, "#%d", val
);
243 fprintf_filtered (stream
, "%s", reg_names
[fetch_arg (buffer
, place
, 3)]);
247 fprintf_filtered (stream
, "%s",
248 reg_names
[fetch_arg (buffer
, place
, 3) + 010]);
252 fprintf_filtered (stream
, "%s", reg_names
[fetch_arg (buffer
, place
, 4)]);
256 fprintf_filtered (stream
, "fp%d", fetch_arg (buffer
, place
, 3));
260 val
= fetch_arg (buffer
, place
, 6);
262 fprintf_filtered (stream
, "%s", reg_names
[val
& 7]);
264 fprintf_filtered (stream
, "%d", val
);
268 fprintf_filtered (stream
, "%s@+",
269 reg_names
[fetch_arg (buffer
, place
, 3) + 8]);
273 fprintf_filtered (stream
, "%s@-",
274 reg_names
[fetch_arg (buffer
, place
, 3) + 8]);
279 fprintf_filtered (stream
, "{%s}", reg_names
[fetch_arg (buffer
, place
, 3)]);
280 else if (place
== 'C')
282 val
= fetch_arg (buffer
, place
, 7);
283 if ( val
> 63 ) /* This is a signed constant. */
285 fprintf_filtered (stream
, "{#%d}", val
);
288 error ("Invalid arg format in opcode table: \"%c%c\".",
294 p1
= buffer
+ (*d
== '#' ? 2 : 4);
296 val
= fetch_arg (buffer
, place
, 4);
297 else if (place
== 'C')
298 val
= fetch_arg (buffer
, place
, 7);
299 else if (place
== '8')
300 val
= fetch_arg (buffer
, place
, 3);
301 else if (place
== '3')
302 val
= fetch_arg (buffer
, place
, 8);
303 else if (place
== 'b')
305 else if (place
== 'w')
307 else if (place
== 'l')
310 error ("Invalid arg format in opcode table: \"%c%c\".",
312 fprintf_filtered (stream
, "#%d", val
);
318 else if (place
== 'B')
319 val
= NEXTBYTE (buffer
); /* from the opcode word */
320 else if (place
== 'w' || place
== 'W')
322 else if (place
== 'l' || place
== 'L')
324 else if (place
== 'g')
326 val
= NEXTBYTE (buffer
);
332 else if (place
== 'c')
334 if (buffer
[1] & 0x40) /* If bit six is one, long offset */
340 error ("Invalid arg format in opcode table: \"%c%c\".",
343 print_address (addr
+ val
, stream
);
348 fprintf_filtered (stream
, "%s@(%d)",
349 reg_names
[fetch_arg (buffer
, place
, 3)], val
);
353 fprintf_filtered (stream
, "%s",
354 fpcr_names
[fetch_arg (buffer
, place
, 3)]);
358 val
= fetch_arg (buffer
, 'd', 3); /* Get coprocessor ID... */
359 if (val
!= 1) /* Unusual coprocessor ID? */
360 fprintf_filtered (stream
, "(cpid=%d) ", val
);
362 p
+= 2; /* Skip coprocessor extended operands */
378 val
= fetch_arg (buffer
, 'x', 6);
379 val
= ((val
& 7) << 3) + ((val
>> 3) & 7);
382 val
= fetch_arg (buffer
, 's', 6);
384 /* Get register number assuming address register. */
385 regno
= (val
& 7) + 8;
386 regname
= reg_names
[regno
];
390 fprintf_filtered (stream
, "%s", reg_names
[val
]);
394 fprintf_filtered (stream
, "%s", regname
);
398 fprintf_filtered (stream
, "%s@", regname
);
402 fprintf_filtered (stream
, "%s@+", regname
);
406 fprintf_filtered (stream
, "%s@-", regname
);
411 fprintf_filtered (stream
, "%s@(%d)", regname
, val
);
415 p
= print_indexed (regno
, p
, addr
, stream
);
423 fprintf_filtered (stream
, "@#");
424 print_address (val
, stream
);
429 fprintf_filtered (stream
, "@#");
430 print_address (val
, stream
);
435 print_address (addr
+ val
, stream
);
439 p
= print_indexed (-1, p
, addr
, stream
);
443 flt_p
= 1; /* Assume it's a float... */
462 flval
= NEXTSINGLE(p
);
466 flval
= NEXTDOUBLE(p
);
470 ieee_extended_to_double (&ext_format_68881
, p
, &flval
);
476 flval
= 0; /* FIXME, handle packed decimal someday. */
480 error ("Invalid arg format in opcode table: \"%c%c\".",
483 if ( flt_p
) /* Print a float? */
484 fprintf_filtered (stream
, "#%g", flval
);
486 fprintf_filtered (stream
, "#%d", val
);
490 fprintf_filtered (stream
, "<invalid address mode 0%o>", val
);
502 /* Move the pointer ahead if this point is farther ahead
507 fputs_filtered ("#0", stream
);
512 register int newval
= 0;
513 for (regno
= 0; regno
< 16; ++regno
)
514 if (val
& (0x8000 >> regno
))
515 newval
|= 1 << regno
;
520 for (regno
= 0; regno
< 16; ++regno
)
521 if (val
& (1 << regno
))
525 fputs_filtered ("/", stream
);
527 fprintf_filtered (stream
, "%s", reg_names
[regno
]);
529 while (val
& (1 << (regno
+ 1)))
531 if (regno
> first_regno
)
532 fprintf_filtered (stream
, "-%s", reg_names
[regno
]);
535 else if (place
== '3')
539 val
= fetch_arg (buffer
, place
, 8);
542 fputs_filtered ("#0", stream
);
547 register int newval
= 0;
548 for (regno
= 0; regno
< 8; ++regno
)
549 if (val
& (0x80 >> regno
))
550 newval
|= 1 << regno
;
555 for (regno
= 0; regno
< 8; ++regno
)
556 if (val
& (1 << regno
))
560 fputs_filtered ("/", stream
);
562 fprintf_filtered (stream
, "fp%d", regno
);
564 while (val
& (1 << (regno
+ 1)))
566 if (regno
> first_regno
)
567 fprintf_filtered (stream
, "-fp%d", regno
);
575 error ("Invalid arg format in opcode table: \"%c\".", *d
);
578 return (unsigned char *) p
;
581 /* Fetch BITS bits from a position in the instruction specified by CODE.
582 CODE is a "place to put an argument", or 'x' for a destination
583 that is a general address (mode and register).
584 BUFFER contains the instruction. */
587 fetch_arg (buffer
, code
, bits
)
588 unsigned char *buffer
;
599 case 'd': /* Destination, for register or quick. */
600 val
= (buffer
[0] << 8) + buffer
[1];
604 case 'x': /* Destination, for general arg */
605 val
= (buffer
[0] << 8) + buffer
[1];
610 val
= (buffer
[3] >> 4);
618 val
= (buffer
[2] << 8) + buffer
[3];
623 val
= (buffer
[2] << 8) + buffer
[3];
629 val
= (buffer
[2] << 8) + buffer
[3];
633 val
= (buffer
[4] << 8) + buffer
[5];
638 val
= (buffer
[4] << 8) + buffer
[5];
643 val
= (buffer
[4] << 8) + buffer
[5];
647 val
= (buffer
[2] << 8) + buffer
[3];
652 val
= (buffer
[2] << 8) + buffer
[3];
681 /* Print an indexed argument. The base register is BASEREG (-1 for pc).
682 P points to extension word, in buffer.
683 ADDR is the nominal core address of that extension word. */
685 static unsigned char *
686 print_indexed (basereg
, p
, addr
, stream
)
693 static char *scales
[] = {"", "*2", "*4", "*8"};
694 register int base_disp
;
695 register int outer_disp
;
700 /* Generate the text for the index register.
701 Where this will be output is not yet determined. */
702 sprintf (buf
, "[%s.%c%s]",
703 reg_names
[(word
>> 12) & 0xf],
704 (word
& 0x800) ? 'l' : 'w',
705 scales
[(word
>> 9) & 3]);
707 /* Handle the 68000 style of indexing. */
709 if ((word
& 0x100) == 0)
712 ((word
& 0x80) ? word
| 0xff00 : word
& 0xff)
713 + ((basereg
== -1) ? addr
: 0),
715 fputs_filtered (buf
, stream
);
719 /* Handle the generalized kind. */
720 /* First, compute the displacement to add to the base register. */
727 switch ((word
>> 4) & 3)
730 base_disp
= NEXTWORD (p
);
733 base_disp
= NEXTLONG (p
);
738 /* Handle single-level case (not indirect) */
742 print_base (basereg
, base_disp
, stream
);
743 fputs_filtered (buf
, stream
);
747 /* Two level. Compute displacement to add after indirection. */
753 outer_disp
= NEXTWORD (p
);
756 outer_disp
= NEXTLONG (p
);
759 fprintf_filtered (stream
, "%d(", outer_disp
);
760 print_base (basereg
, base_disp
, stream
);
762 /* If postindexed, print the closeparen before the index. */
764 fprintf_filtered (stream
, ")%s", buf
);
765 /* If preindexed, print the closeparen after the index. */
767 fprintf_filtered (stream
, "%s)", buf
);
772 /* Print a base register REGNO and displacement DISP, on STREAM.
773 REGNO = -1 for pc, -2 for none (suppressed). */
776 print_base (regno
, disp
, stream
)
782 fprintf_filtered (stream
, "%d", disp
);
783 else if (regno
== -1)
784 fprintf_filtered (stream
, "0x%x", disp
);
786 fprintf_filtered (stream
, "%d(%s)", disp
, reg_names
[regno
]);
This page took 0.044522 seconds and 4 git commands to generate.