1 /* Print Motorola 68k instructions.
2 Copyright 1986, 1987, 1989, 1991, 1992, 1993 Free Software Foundation, Inc.
4 This file is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19 #include "ieee-float.h"
21 extern CONST
struct ext_format ext_format_68881
;
23 /* Opcode/m68k.h is a massive table. As a kludge, break it up into
24 two pieces. This makes nonportable C -- FIXME -- it assumes that
25 two data items declared near each other will be contiguous in
26 memory. This kludge can be removed, FIXME, when GCC is fixed to not
27 be a hog about initializers. */
30 #define BREAK_UP_BIG_DECL }; \
31 struct m68k_opcode m68k_opcodes_2[] = {
32 #define AND_OTHER_PART sizeof (m68k_opcodes_2)
35 #include "opcode/m68k.h"
38 /* Local function prototypes */
41 fetch_arg
PARAMS ((unsigned char *, int, int, disassemble_info
*));
44 print_base
PARAMS ((int, int, disassemble_info
*));
46 static unsigned char *
47 print_indexed
PARAMS ((int, unsigned char *, bfd_vma
, disassemble_info
*));
49 static unsigned char *
50 print_insn_arg
PARAMS ((char *, unsigned char *, unsigned char *, bfd_vma
,
53 /* Sign-extend an (unsigned char). */
55 #define COERCE_SIGNED_CHAR(ch) ((signed char)(ch))
57 #define COERCE_SIGNED_CHAR(ch) ((int)(((ch) ^ 0x80) & 0xFF) - 128)
60 CONST
char * CONST fpcr_names
[] = {
61 "", "fpiar", "fpsr", "fpiar/fpsr", "fpcr",
62 "fpiar/fpcr", "fpsr/fpcr", "fpiar/fpsr/fpcr"};
64 static char *reg_names
[] = {
65 "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7", "a0",
66 "a1", "a2", "a3", "a4", "a5", "fp", "sp", "ps", "pc"};
68 /* Define accessors for 68K's 1, 2, and 4-byte signed quantities.
69 The _SHIFT values move the quantity to the high order end of an
70 `int' value, so it will sign-extend. Probably a few more casts
71 are needed to make it compile without warnings on finicky systems. */
72 #define BITS_PER_BYTE 8
73 #define WORD_SHIFT (BITS_PER_BYTE * ((sizeof (int)) - 2))
74 #define LONG_SHIFT (BITS_PER_BYTE * ((sizeof (int)) - 4))
76 #define NEXTBYTE(p) (p += 2, FETCH_DATA (info, p), COERCE_SIGNED_CHAR(p[-1]))
79 (p += 2, FETCH_DATA (info, p), \
80 (((int)((p[-2] << 8) + p[-1])) << WORD_SHIFT) >> WORD_SHIFT)
83 (p += 4, FETCH_DATA (info, p), \
84 (((int)((((((p[-4] << 8) + p[-3]) << 8) + p[-2]) << 8) + p[-1])) \
85 << LONG_SHIFT) >> LONG_SHIFT)
87 /* NEXTSINGLE and NEXTDOUBLE handle alignment problems, but not
88 * byte-swapping or other float format differences. FIXME! */
96 #define NEXTSINGLE(val, p) \
97 { int i; union number u;\
98 FETCH_DATA (info, p + sizeof (float));\
99 for (i = 0; i < sizeof(float); i++) u.c[i] = *p++; \
102 #define NEXTDOUBLE(val, p) \
103 { int i; union number u;\
104 FETCH_DATA (info, p + sizeof (double));\
105 for (i = 0; i < sizeof(double); i++) u.c[i] = *p++; \
108 /* Need a function to convert from extended to double precision... */
109 #define NEXTEXTEND(p) \
110 (p += 12, FETCH_DATA (info, p), 0.0)
112 /* Need a function to convert from packed to double
113 precision. Actually, it's easier to print a
114 packed number than a double anyway, so maybe
115 there should be a special case to handle this... */
116 #define NEXTPACKED(p) \
117 (p += 12, FETCH_DATA (info, p), 0.0)
120 /* Maximum length of an instruction. */
127 /* Points to first byte not fetched. */
128 bfd_byte
*max_fetched
;
129 bfd_byte the_buffer
[MAXLEN
];
134 /* Make sure that bytes from INFO->PRIVATE_DATA->BUFFER (inclusive)
135 to ADDR (exclusive) are valid. Returns 1 for success, longjmps
137 #define FETCH_DATA(info, addr) \
138 ((addr) <= ((struct private *)(info->private_data))->max_fetched \
139 ? 1 : fetch_data ((info), (addr)))
142 fetch_data (info
, addr
)
143 struct disassemble_info
*info
;
147 struct private *priv
= (struct private *)info
->private_data
;
148 bfd_vma start
= priv
->insn_start
+ (priv
->max_fetched
- priv
->the_buffer
);
150 status
= (*info
->read_memory_func
) (start
,
152 addr
- priv
->max_fetched
,
156 (*info
->memory_error_func
) (status
, start
, info
);
157 longjmp (priv
->bailout
);
160 priv
->max_fetched
= addr
;
165 m68k_opcode_error(info
, code
, place
)
166 struct disassemble_info
*info
;
169 (*info
->fprintf_func
)(info
->stream
,
170 "<internal error in opcode table: \"%c%c\">",
174 /* Print the m68k instruction at address MEMADDR in debugged memory,
175 on STREAM. Returns length of the instruction, in bytes. */
178 print_insn_m68k (memaddr
, info
)
180 disassemble_info
*info
;
183 register unsigned char *p
;
185 register unsigned long bestmask
;
188 bfd_byte
*buffer
= priv
.the_buffer
;
190 info
->private_data
= (PTR
) &priv
;
191 priv
.max_fetched
= priv
.the_buffer
;
192 priv
.insn_start
= memaddr
;
193 if (setjmp (priv
.bailout
) != 0)
199 FETCH_DATA (info
, buffer
+ 2);
200 for (i
= 0; i
< numopcodes
; i
++)
202 register unsigned long opcode
= m68k_opcodes
[i
].opcode
;
203 register unsigned long match
= m68k_opcodes
[i
].match
;
204 if (((0xff & buffer
[0] & (match
>> 24)) == (0xff & (opcode
>> 24)))
205 && ((0xff & buffer
[1] & (match
>> 16)) == (0xff & (opcode
>> 16)))
206 /* Only fetch the next two bytes if we need to. */
207 && (((0xffff & match
) == 0)
209 (FETCH_DATA (info
, buffer
+ 4)
210 && ((0xff & buffer
[2] & (match
>> 8)) == (0xff & (opcode
>> 8)))
211 && ((0xff & buffer
[3] & match
) == (0xff & opcode
)))
214 /* Don't use for printout the variants of divul and divsl
215 that have the same register number in two places.
216 The more general variants will match instead. */
217 for (d
= m68k_opcodes
[i
].args
; *d
; d
+= 2)
221 /* Don't use for printout the variants of most floating
222 point coprocessor instructions which use the same
223 register number in two places, as above. */
225 for (d
= m68k_opcodes
[i
].args
; *d
; d
+= 2)
229 if (*d
== 0 && match
> bestmask
)
237 /* Handle undefined instructions. */
240 (*info
->fprintf_func
) (info
->stream
, "0%o",
241 (buffer
[0] << 8) + buffer
[1]);
245 (*info
->fprintf_func
) (info
->stream
, "%s", m68k_opcodes
[best
].name
);
247 /* Point at first word of argument data,
248 and at descriptor for first argument. */
251 /* Why do this this way? -MelloN */
252 for (d
= m68k_opcodes
[best
].args
; *d
; d
+= 2)
256 if (d
[1] == 'l' && p
- buffer
< 6)
258 else if (p
- buffer
< 4 && d
[1] != 'C' && d
[1] != '8' )
261 if (d
[1] >= '1' && d
[1] <= '3' && p
- buffer
< 4)
263 if (d
[1] >= '4' && d
[1] <= '6' && p
- buffer
< 6)
265 if ((d
[0] == 'L' || d
[0] == 'l') && d
[1] == 'w' && p
- buffer
< 4)
269 FETCH_DATA (info
, p
);
271 d
= m68k_opcodes
[best
].args
;
274 (*info
->fprintf_func
) (info
->stream
, " ");
278 p
= print_insn_arg (d
, buffer
, p
, memaddr
+ p
- buffer
, info
);
280 if (*d
&& *(d
- 2) != 'I' && *d
!= 'k')
281 (*info
->fprintf_func
) (info
->stream
, ",");
286 static unsigned char *
287 print_insn_arg (d
, buffer
, p
, addr
, info
)
289 unsigned char *buffer
;
290 register unsigned char *p
;
291 bfd_vma addr
; /* PC for this arg to be relative to */
292 disassemble_info
*info
;
294 register int val
= 0;
295 register int place
= d
[1];
297 register CONST
char *regname
;
298 register unsigned char *p1
;
304 case 'c': /* cache identifier */
306 static char *cacheFieldName
[] = { "NOP", "dc", "ic", "bc" };
307 val
= fetch_arg (buffer
, place
, 2, info
);
308 (*info
->fprintf_func
) (info
->stream
, cacheFieldName
[val
]);
312 case 'a': /* address register indirect only. Cf. case '+'. */
314 (*info
->fprintf_func
)
317 reg_names
[fetch_arg (buffer
, place
, 3, info
) + 8]);
321 case '_': /* 32-bit absolute address for move16. */
324 (*info
->fprintf_func
) (info
->stream
, "@#");
325 print_address (val
, info
->stream
);
330 (*info
->fprintf_func
) (info
->stream
, "ccr");
334 (*info
->fprintf_func
) (info
->stream
, "sr");
338 (*info
->fprintf_func
) (info
->stream
, "usp");
343 static struct { char *name
; int value
; } names
[]
344 = {{"sfc", 0x000}, {"dfc", 0x001}, {"cacr", 0x002},
345 {"tc", 0x003}, {"itt0",0x004}, {"itt1", 0x005},
346 {"dtt0",0x006}, {"dtt1",0x007},
347 {"usp", 0x800}, {"vbr", 0x801}, {"caar", 0x802},
348 {"msp", 0x803}, {"isp", 0x804}, {"mmusr",0x805},
349 {"urp", 0x806}, {"srp", 0x807}};
351 val
= fetch_arg (buffer
, place
, 12, info
);
352 for (regno
= sizeof names
/ sizeof names
[0] - 1; regno
>= 0; regno
--)
353 if (names
[regno
].value
== val
)
355 (*info
->fprintf_func
) (info
->stream
, names
[regno
].name
);
359 (*info
->fprintf_func
) (info
->stream
, "%d", val
);
364 val
= fetch_arg (buffer
, place
, 3, info
);
365 /* 0 means 8, except for the bkpt instruction... */
366 if (val
== 0 && d
[1] != 's')
368 (*info
->fprintf_func
) (info
->stream
, "#%d", val
);
372 val
= fetch_arg (buffer
, place
, 8, info
);
375 (*info
->fprintf_func
) (info
->stream
, "#%d", val
);
379 val
= fetch_arg (buffer
, place
, 4, info
);
380 (*info
->fprintf_func
) (info
->stream
, "#%d", val
);
384 (*info
->fprintf_func
) (info
->stream
, "%s",
385 reg_names
[fetch_arg (buffer
, place
, 3, info
)]);
389 (*info
->fprintf_func
)
391 reg_names
[fetch_arg (buffer
, place
, 3, info
) + 010]);
395 (*info
->fprintf_func
)
397 reg_names
[fetch_arg (buffer
, place
, 4, info
)]);
401 (*info
->fprintf_func
)
402 (info
->stream
, "%s@",
403 reg_names
[fetch_arg (buffer
, place
, 4, info
)]);
407 (*info
->fprintf_func
)
408 (info
->stream
, "fp%d",
409 fetch_arg (buffer
, place
, 3, info
));
413 val
= fetch_arg (buffer
, place
, 6, info
);
415 (*info
->fprintf_func
) (info
->stream
, "%s", reg_names
[val
& 7]);
417 (*info
->fprintf_func
) (info
->stream
, "%d", val
);
421 (*info
->fprintf_func
)
422 (info
->stream
, "%s@+",
423 reg_names
[fetch_arg (buffer
, place
, 3, info
) + 8]);
427 (*info
->fprintf_func
)
428 (info
->stream
, "%s@-",
429 reg_names
[fetch_arg (buffer
, place
, 3, info
) + 8]);
434 (*info
->fprintf_func
)
435 (info
->stream
, "{%s}",
436 reg_names
[fetch_arg (buffer
, place
, 3, info
)]);
437 else if (place
== 'C')
439 val
= fetch_arg (buffer
, place
, 7, info
);
440 if ( val
> 63 ) /* This is a signed constant. */
442 (*info
->fprintf_func
) (info
->stream
, "{#%d}", val
);
445 m68k_opcode_error (info
, *d
, place
);
450 p1
= buffer
+ (*d
== '#' ? 2 : 4);
452 val
= fetch_arg (buffer
, place
, 4, info
);
453 else if (place
== 'C')
454 val
= fetch_arg (buffer
, place
, 7, info
);
455 else if (place
== '8')
456 val
= fetch_arg (buffer
, place
, 3, info
);
457 else if (place
== '3')
458 val
= fetch_arg (buffer
, place
, 8, info
);
459 else if (place
== 'b')
461 else if (place
== 'w')
463 else if (place
== 'l')
466 m68k_opcode_error (info
, *d
, place
);
467 (*info
->fprintf_func
) (info
->stream
, "#%d", val
);
473 else if (place
== 'B')
474 val
= COERCE_SIGNED_CHAR(buffer
[1]);
475 else if (place
== 'w' || place
== 'W')
477 else if (place
== 'l' || place
== 'L')
479 else if (place
== 'g')
481 val
= NEXTBYTE (buffer
);
487 else if (place
== 'c')
489 if (buffer
[1] & 0x40) /* If bit six is one, long offset */
495 m68k_opcode_error (info
, *d
, place
);
497 print_address (addr
+ val
, info
->stream
);
502 (*info
->fprintf_func
)
503 (info
->stream
, "%s@(%d)",
504 reg_names
[fetch_arg (buffer
, place
, 3, info
)], val
);
508 (*info
->fprintf_func
) (info
->stream
, "%s",
509 fpcr_names
[fetch_arg (buffer
, place
, 3, info
)]);
513 /* Get coprocessor ID... */
514 val
= fetch_arg (buffer
, 'd', 3, info
);
516 if (val
!= 1) /* Unusual coprocessor ID? */
517 (*info
->fprintf_func
) (info
->stream
, "(cpid=%d) ", val
);
519 p
+= 2; /* Skip coprocessor extended operands */
536 val
= fetch_arg (buffer
, 'x', 6, info
);
537 val
= ((val
& 7) << 3) + ((val
>> 3) & 7);
540 val
= fetch_arg (buffer
, 's', 6, info
);
542 /* Get register number assuming address register. */
543 regno
= (val
& 7) + 8;
544 regname
= reg_names
[regno
];
548 (*info
->fprintf_func
) (info
->stream
, "%s", reg_names
[val
]);
552 (*info
->fprintf_func
) (info
->stream
, "%s", regname
);
556 (*info
->fprintf_func
) (info
->stream
, "%s@", regname
);
560 (*info
->fprintf_func
) (info
->stream
, "%s@+", regname
);
564 (*info
->fprintf_func
) (info
->stream
, "%s@-", regname
);
569 (*info
->fprintf_func
) (info
->stream
, "%s@(%d)", regname
, val
);
573 p
= print_indexed (regno
, p
, addr
, info
);
581 (*info
->fprintf_func
) (info
->stream
, "@#");
582 print_address (val
, info
->stream
);
587 (*info
->fprintf_func
) (info
->stream
, "@#");
588 print_address (val
, info
->stream
);
593 print_address (addr
+ val
, info
->stream
);
597 p
= print_indexed (-1, p
, addr
, info
);
601 flt_p
= 1; /* Assume it's a float... */
620 NEXTSINGLE(flval
, p
);
624 NEXTDOUBLE(flval
, p
);
628 ieee_extended_to_double (&ext_format_68881
,
634 flval
= NEXTPACKED(p
);
638 m68k_opcode_error (info
, *d
, place
);
640 if ( flt_p
) /* Print a float? */
641 (*info
->fprintf_func
) (info
->stream
, "#%g", flval
);
643 (*info
->fprintf_func
) (info
->stream
, "#%d", val
);
647 (*info
->fprintf_func
) (info
->stream
,
648 "<invalid address mode 0%o>",
661 /* Move the pointer ahead if this point is farther ahead
666 (*info
->fprintf_func
) (info
->stream
, "#0");
671 register int newval
= 0;
672 for (regno
= 0; regno
< 16; ++regno
)
673 if (val
& (0x8000 >> regno
))
674 newval
|= 1 << regno
;
679 for (regno
= 0; regno
< 16; ++regno
)
680 if (val
& (1 << regno
))
684 (*info
->fprintf_func
) (info
->stream
, "/");
686 (*info
->fprintf_func
) (info
->stream
, "%s", reg_names
[regno
]);
688 while (val
& (1 << (regno
+ 1)))
690 if (regno
> first_regno
)
691 (*info
->fprintf_func
) (info
->stream
, "-%s",
695 else if (place
== '3')
699 val
= fetch_arg (buffer
, place
, 8, info
);
702 (*info
->fprintf_func
) (info
->stream
, "#0");
707 register int newval
= 0;
708 for (regno
= 0; regno
< 8; ++regno
)
709 if (val
& (0x80 >> regno
))
710 newval
|= 1 << regno
;
715 for (regno
= 0; regno
< 8; ++regno
)
716 if (val
& (1 << regno
))
720 (*info
->fprintf_func
) (info
->stream
, "/");
722 (*info
->fprintf_func
) (info
->stream
, "fp%d", regno
);
724 while (val
& (1 << (regno
+ 1)))
726 if (regno
> first_regno
)
727 (*info
->fprintf_func
) (info
->stream
, "-fp%d", regno
);
735 m68k_opcode_error (info
, *d
, ' ');
738 return (unsigned char *) p
;
741 /* Fetch BITS bits from a position in the instruction specified by CODE.
742 CODE is a "place to put an argument", or 'x' for a destination
743 that is a general address (mode and register).
744 BUFFER contains the instruction. */
747 fetch_arg (buffer
, code
, bits
, info
)
748 unsigned char *buffer
;
751 disassemble_info
*info
;
753 register int val
= 0;
760 case 'd': /* Destination, for register or quick. */
761 val
= (buffer
[0] << 8) + buffer
[1];
765 case 'x': /* Destination, for general arg */
766 val
= (buffer
[0] << 8) + buffer
[1];
771 FETCH_DATA (info
, buffer
+ 3);
772 val
= (buffer
[3] >> 4);
776 FETCH_DATA (info
, buffer
+ 3);
781 FETCH_DATA (info
, buffer
+ 3);
782 val
= (buffer
[2] << 8) + buffer
[3];
787 FETCH_DATA (info
, buffer
+ 3);
788 val
= (buffer
[2] << 8) + buffer
[3];
794 FETCH_DATA (info
, buffer
+ 3);
795 val
= (buffer
[2] << 8) + buffer
[3];
799 FETCH_DATA (info
, buffer
+ 5);
800 val
= (buffer
[4] << 8) + buffer
[5];
805 FETCH_DATA (info
, buffer
+ 5);
806 val
= (buffer
[4] << 8) + buffer
[5];
811 FETCH_DATA (info
, buffer
+ 5);
812 val
= (buffer
[4] << 8) + buffer
[5];
816 FETCH_DATA (info
, buffer
+ 3);
817 val
= (buffer
[2] << 8) + buffer
[3];
822 FETCH_DATA (info
, buffer
+ 3);
823 val
= (buffer
[2] << 8) + buffer
[3];
828 FETCH_DATA (info
, buffer
+ 3);
829 val
= (buffer
[2] << 8) + buffer
[3];
834 val
= (buffer
[1] >> 6);
864 /* Print an indexed argument. The base register is BASEREG (-1 for pc).
865 P points to extension word, in buffer.
866 ADDR is the nominal core address of that extension word. */
868 static unsigned char *
869 print_indexed (basereg
, p
, addr
, info
)
873 disassemble_info
*info
;
876 static char *scales
[] = {"", "*2", "*4", "*8"};
877 register int base_disp
;
878 register int outer_disp
;
883 /* Generate the text for the index register.
884 Where this will be output is not yet determined. */
885 sprintf (buf
, "[%s.%c%s]",
886 reg_names
[(word
>> 12) & 0xf],
887 (word
& 0x800) ? 'l' : 'w',
888 scales
[(word
>> 9) & 3]);
890 /* Handle the 68000 style of indexing. */
892 if ((word
& 0x100) == 0)
895 ((word
& 0x80) ? word
| 0xff00 : word
& 0xff)
896 + ((basereg
== -1) ? addr
: 0),
898 (*info
->fprintf_func
) (info
->stream
, "%s", buf
);
902 /* Handle the generalized kind. */
903 /* First, compute the displacement to add to the base register. */
910 switch ((word
>> 4) & 3)
913 base_disp
= NEXTWORD (p
);
916 base_disp
= NEXTLONG (p
);
921 /* Handle single-level case (not indirect) */
925 print_base (basereg
, base_disp
, info
);
926 (*info
->fprintf_func
) (info
->stream
, "%s", buf
);
930 /* Two level. Compute displacement to add after indirection. */
936 outer_disp
= NEXTWORD (p
);
939 outer_disp
= NEXTLONG (p
);
942 (*info
->fprintf_func
) (info
->stream
, "%d(", outer_disp
);
943 print_base (basereg
, base_disp
, info
);
945 /* If postindexed, print the closeparen before the index. */
947 (*info
->fprintf_func
) (info
->stream
, ")%s", buf
);
948 /* If preindexed, print the closeparen after the index. */
950 (*info
->fprintf_func
) (info
->stream
, "%s)", buf
);
955 /* Print a base register REGNO and displacement DISP, on INFO->STREAM.
956 REGNO = -1 for pc, -2 for none (suppressed). */
959 print_base (regno
, disp
, info
)
962 disassemble_info
*info
;
965 (*info
->fprintf_func
) (info
->stream
, "%d", disp
);
966 else if (regno
== -1)
967 (*info
->fprintf_func
) (info
->stream
, "0x%x", (unsigned) disp
);
969 (*info
->fprintf_func
) (info
->stream
, "%d(%s)", disp
, reg_names
[regno
]);