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. */
24 #include "opcode/m68k.h"
27 /* Local function prototypes */
30 fetch_arg
PARAMS ((unsigned char *, int, int));
33 print_base
PARAMS ((int, int, FILE *));
35 static unsigned char *
36 print_indexed
PARAMS ((int, unsigned char *, CORE_ADDR
, FILE *));
38 static unsigned char *
39 print_insn_arg
PARAMS ((char *, unsigned char *, unsigned char *, CORE_ADDR
,
42 /* 68k instructions are never longer than this many bytes. */
45 /* Number of elements in the opcode table. */
46 #define NOPCODES (sizeof m68k_opcodes / sizeof m68k_opcodes[0])
48 extern char *reg_names
[];
49 char *fpcr_names
[] = { "", "fpiar", "fpsr", "fpiar/fpsr", "fpcr",
50 "fpiar/fpcr", "fpsr/fpcr", "fpiar-fpcr"};
53 #define NEXTBYTE(p) (p += 2, ((char *)p)[-1])
56 (p += 2, ((((char *)p)[-2]) << 8) + p[-1])
59 (p += 4, (((((p[-4] << 8) + p[-3]) << 8) + p[-2]) << 8) + p[-1])
61 #define NEXTSINGLE(p) \
62 (p += 4, *((float *)(p - 4)))
64 #define NEXTDOUBLE(p) \
65 (p += 8, *((double *)(p - 8)))
67 #define NEXTEXTEND(p) \
68 (p += 12, 0.0) /* Need a function to convert from extended to double
71 #define NEXTPACKED(p) \
72 (p += 12, 0.0) /* Need a function to convert from packed to double
73 precision. Actually, it's easier to print a
74 packed number than a double anyway, so maybe
75 there should be a special case to handle this... */
77 /* Print the m68k instruction at address MEMADDR in debugged memory,
78 on STREAM. Returns length of the instruction, in bytes. */
81 print_insn (memaddr
, stream
)
85 unsigned char buffer
[MAXLEN
];
87 register unsigned char *p
;
89 register int bestmask
;
92 read_memory (memaddr
, (char *) buffer
, MAXLEN
);
96 for (i
= 0; i
< NOPCODES
; i
++)
98 register unsigned int opcode
= m68k_opcodes
[i
].opcode
;
99 register unsigned int match
= m68k_opcodes
[i
].match
;
100 if (((0xff & buffer
[0] & (match
>> 24)) == (0xff & (opcode
>> 24)))
101 && ((0xff & buffer
[1] & (match
>> 16)) == (0xff & (opcode
>> 16)))
102 && ((0xff & buffer
[2] & (match
>> 8)) == (0xff & (opcode
>> 8)))
103 && ((0xff & buffer
[3] & match
) == (0xff & opcode
)))
105 /* Don't use for printout the variants of divul and divsl
106 that have the same register number in two places.
107 The more general variants will match instead. */
108 for (d
= m68k_opcodes
[i
].args
; *d
; d
+= 2)
112 /* Don't use for printout the variants of most floating
113 point coprocessor instructions which use the same
114 register number in two places, as above. */
116 for (d
= m68k_opcodes
[i
].args
; *d
; d
+= 2)
120 if (*d
== 0 && match
> bestmask
)
128 /* Handle undefined instructions. */
131 fprintf_filtered (stream
, "0%o", (buffer
[0] << 8) + buffer
[1]);
135 fprintf_filtered (stream
, "%s", m68k_opcodes
[best
].name
);
137 /* Point at first word of argument data,
138 and at descriptor for first argument. */
141 /* Why do this this way? -MelloN */
142 for (d
= m68k_opcodes
[best
].args
; *d
; d
+= 2)
146 if (d
[1] == 'l' && p
- buffer
< 6)
148 else if (p
- buffer
< 4 && d
[1] != 'C' && d
[1] != '8' )
151 if (d
[1] >= '1' && d
[1] <= '3' && p
- buffer
< 4)
153 if (d
[1] >= '4' && d
[1] <= '6' && p
- buffer
< 6)
155 if ((d
[0] == 'L' || d
[0] == 'l') && d
[1] == 'w' && p
- buffer
< 4)
159 d
= m68k_opcodes
[best
].args
;
162 fputs_filtered (" ", stream
);
166 p
= print_insn_arg (d
, buffer
, p
, memaddr
+ p
- buffer
, stream
);
168 if (*d
&& *(d
- 2) != 'I' && *d
!= 'k')
169 fputs_filtered (",", stream
);
174 static unsigned char *
175 print_insn_arg (d
, buffer
, p
, addr
, stream
)
177 unsigned char *buffer
;
178 register unsigned char *p
;
179 CORE_ADDR addr
; /* PC for this arg to be relative to */
183 register int place
= d
[1];
185 register char *regname
;
186 register unsigned char *p1
;
187 register double flval
;
193 fprintf_filtered (stream
, "ccr");
197 fprintf_filtered (stream
, "sr");
201 fprintf_filtered (stream
, "usp");
206 static struct { char *name
; int value
; } names
[]
207 = {{"sfc", 0x000}, {"dfc", 0x001}, {"cacr", 0x002},
208 {"usp", 0x800}, {"vbr", 0x801}, {"caar", 0x802},
209 {"msp", 0x803}, {"isp", 0x804}};
211 val
= fetch_arg (buffer
, place
, 12);
212 for (regno
= sizeof names
/ sizeof names
[0] - 1; regno
>= 0; regno
--)
213 if (names
[regno
].value
== val
)
215 fprintf_filtered (stream
, names
[regno
].name
);
219 fprintf_filtered (stream
, "%d", val
);
224 val
= fetch_arg (buffer
, place
, 3);
225 /* 0 means 8, except for the bkpt instruction... */
226 if (val
== 0 && d
[1] != 's')
228 fprintf_filtered (stream
, "#%d", val
);
232 val
= fetch_arg (buffer
, place
, 8);
235 fprintf_filtered (stream
, "#%d", val
);
239 val
= fetch_arg (buffer
, place
, 4);
240 fprintf_filtered (stream
, "#%d", val
);
244 fprintf_filtered (stream
, "%s", reg_names
[fetch_arg (buffer
, place
, 3)]);
248 fprintf_filtered (stream
, "%s",
249 reg_names
[fetch_arg (buffer
, place
, 3) + 010]);
253 fprintf_filtered (stream
, "%s", reg_names
[fetch_arg (buffer
, place
, 4)]);
257 fprintf_filtered (stream
, "fp%d", fetch_arg (buffer
, place
, 3));
261 val
= fetch_arg (buffer
, place
, 6);
263 fprintf_filtered (stream
, "%s", reg_names
[val
& 7]);
265 fprintf_filtered (stream
, "%d", val
);
269 fprintf_filtered (stream
, "%s@+",
270 reg_names
[fetch_arg (buffer
, place
, 3) + 8]);
274 fprintf_filtered (stream
, "%s@-",
275 reg_names
[fetch_arg (buffer
, place
, 3) + 8]);
280 fprintf_filtered (stream
, "{%s}", reg_names
[fetch_arg (buffer
, place
, 3)]);
281 else if (place
== 'C')
283 val
= fetch_arg (buffer
, place
, 7);
284 if ( val
> 63 ) /* This is a signed constant. */
286 fprintf_filtered (stream
, "{#%d}", val
);
289 error ("Invalid arg format in opcode table: \"%c%c\".",
295 p1
= buffer
+ (*d
== '#' ? 2 : 4);
297 val
= fetch_arg (buffer
, place
, 4);
298 else if (place
== 'C')
299 val
= fetch_arg (buffer
, place
, 7);
300 else if (place
== '8')
301 val
= fetch_arg (buffer
, place
, 3);
302 else if (place
== '3')
303 val
= fetch_arg (buffer
, place
, 8);
304 else if (place
== 'b')
306 else if (place
== 'w')
308 else if (place
== 'l')
311 error ("Invalid arg format in opcode table: \"%c%c\".",
313 fprintf_filtered (stream
, "#%d", val
);
319 else if (place
== 'B')
321 else if (place
== 'w' || place
== 'W')
323 else if (place
== 'l' || place
== 'L')
325 else if (place
== 'g')
327 val
= ((char *)buffer
)[1];
333 else if (place
== 'c')
335 if (buffer
[1] & 0x40) /* If bit six is one, long offset */
341 error ("Invalid arg format in opcode table: \"%c%c\".",
344 print_address (addr
+ val
, stream
);
349 fprintf_filtered (stream
, "%s@(%d)",
350 reg_names
[fetch_arg (buffer
, place
, 3)], val
);
354 fprintf_filtered (stream
, "%s",
355 fpcr_names
[fetch_arg (buffer
, place
, 3)]);
359 val
= fetch_arg (buffer
, 'd', 3); /* Get coprocessor ID... */
360 if (val
!= 1) /* Unusual coprocessor ID? */
361 fprintf_filtered (stream
, "(cpid=%d) ", val
);
363 p
+= 2; /* Skip coprocessor extended operands */
379 val
= fetch_arg (buffer
, 'x', 6);
380 val
= ((val
& 7) << 3) + ((val
>> 3) & 7);
383 val
= fetch_arg (buffer
, 's', 6);
385 /* Get register number assuming address register. */
386 regno
= (val
& 7) + 8;
387 regname
= reg_names
[regno
];
391 fprintf_filtered (stream
, "%s", reg_names
[val
]);
395 fprintf_filtered (stream
, "%s", regname
);
399 fprintf_filtered (stream
, "%s@", regname
);
403 fprintf_filtered (stream
, "%s@+", regname
);
407 fprintf_filtered (stream
, "%s@-", regname
);
412 fprintf_filtered (stream
, "%s@(%d)", regname
, val
);
416 p
= print_indexed (regno
, p
, addr
, stream
);
424 fprintf_filtered (stream
, "@#");
425 print_address (val
, stream
);
430 fprintf_filtered (stream
, "@#");
431 print_address (val
, stream
);
436 print_address (addr
+ val
, stream
);
440 p
= print_indexed (-1, p
, addr
, stream
);
444 flt_p
= 1; /* Assume it's a float... */
463 flval
= NEXTSINGLE(p
);
467 flval
= NEXTDOUBLE(p
);
471 flval
= NEXTEXTEND(p
);
475 flval
= NEXTPACKED(p
);
479 error ("Invalid arg format in opcode table: \"%c%c\".",
482 if ( flt_p
) /* Print a float? */
483 fprintf_filtered (stream
, "#%g", flval
);
485 fprintf_filtered (stream
, "#%d", val
);
489 fprintf_filtered (stream
, "<invalid address mode 0%o>", val
);
501 /* Move the pointer ahead if this point is farther ahead
506 fputs_filtered ("#0", stream
);
511 register int newval
= 0;
512 for (regno
= 0; regno
< 16; ++regno
)
513 if (val
& (0x8000 >> regno
))
514 newval
|= 1 << regno
;
519 for (regno
= 0; regno
< 16; ++regno
)
520 if (val
& (1 << regno
))
524 fputs_filtered ("/", stream
);
526 fprintf_filtered (stream
, "%s", reg_names
[regno
]);
528 while (val
& (1 << (regno
+ 1)))
530 if (regno
> first_regno
)
531 fprintf_filtered (stream
, "-%s", reg_names
[regno
]);
534 else if (place
== '3')
538 val
= fetch_arg (buffer
, place
, 8);
541 fputs_filtered ("#0", stream
);
546 register int newval
= 0;
547 for (regno
= 0; regno
< 8; ++regno
)
548 if (val
& (0x80 >> regno
))
549 newval
|= 1 << regno
;
554 for (regno
= 0; regno
< 8; ++regno
)
555 if (val
& (1 << regno
))
559 fputs_filtered ("/", stream
);
561 fprintf_filtered (stream
, "fp%d", regno
);
563 while (val
& (1 << (regno
+ 1)))
565 if (regno
> first_regno
)
566 fprintf_filtered (stream
, "-fp%d", regno
);
574 error ("Invalid arg format in opcode table: \"%c\".", *d
);
577 return (unsigned char *) p
;
580 /* Fetch BITS bits from a position in the instruction specified by CODE.
581 CODE is a "place to put an argument", or 'x' for a destination
582 that is a general address (mode and register).
583 BUFFER contains the instruction. */
586 fetch_arg (buffer
, code
, bits
)
587 unsigned char *buffer
;
598 case 'd': /* Destination, for register or quick. */
599 val
= (buffer
[0] << 8) + buffer
[1];
603 case 'x': /* Destination, for general arg */
604 val
= (buffer
[0] << 8) + buffer
[1];
609 val
= (buffer
[3] >> 4);
617 val
= (buffer
[2] << 8) + buffer
[3];
622 val
= (buffer
[2] << 8) + buffer
[3];
628 val
= (buffer
[2] << 8) + buffer
[3];
632 val
= (buffer
[4] << 8) + buffer
[5];
637 val
= (buffer
[4] << 8) + buffer
[5];
642 val
= (buffer
[4] << 8) + buffer
[5];
646 val
= (buffer
[2] << 8) + buffer
[3];
651 val
= (buffer
[2] << 8) + buffer
[3];
680 /* Print an indexed argument. The base register is BASEREG (-1 for pc).
681 P points to extension word, in buffer.
682 ADDR is the nominal core address of that extension word. */
684 static unsigned char *
685 print_indexed (basereg
, p
, addr
, stream
)
692 static char *scales
[] = {"", "*2", "*4", "*8"};
693 register int base_disp
;
694 register int outer_disp
;
699 /* Generate the text for the index register.
700 Where this will be output is not yet determined. */
701 sprintf (buf
, "[%s.%c%s]",
702 reg_names
[(word
>> 12) & 0xf],
703 (word
& 0x800) ? 'l' : 'w',
704 scales
[(word
>> 9) & 3]);
706 /* Handle the 68000 style of indexing. */
708 if ((word
& 0x100) == 0)
711 ((word
& 0x80) ? word
| 0xff00 : word
& 0xff)
712 + ((basereg
== -1) ? addr
: 0),
714 fputs_filtered (buf
, stream
);
718 /* Handle the generalized kind. */
719 /* First, compute the displacement to add to the base register. */
726 switch ((word
>> 4) & 3)
729 base_disp
= NEXTWORD (p
);
732 base_disp
= NEXTLONG (p
);
737 /* Handle single-level case (not indirect) */
741 print_base (basereg
, base_disp
, stream
);
742 fputs_filtered (buf
, stream
);
746 /* Two level. Compute displacement to add after indirection. */
752 outer_disp
= NEXTWORD (p
);
755 outer_disp
= NEXTLONG (p
);
758 fprintf_filtered (stream
, "%d(", outer_disp
);
759 print_base (basereg
, base_disp
, stream
);
761 /* If postindexed, print the closeparen before the index. */
763 fprintf_filtered (stream
, ")%s", buf
);
764 /* If preindexed, print the closeparen after the index. */
766 fprintf_filtered (stream
, "%s)", buf
);
771 /* Print a base register REGNO and displacement DISP, on STREAM.
772 REGNO = -1 for pc, -2 for none (suppressed). */
775 print_base (regno
, disp
, stream
)
781 fprintf_filtered (stream
, "%d", disp
);
782 else if (regno
== -1)
783 fprintf_filtered (stream
, "0x%x", disp
);
785 fprintf_filtered (stream
, "%d(%s)", disp
, reg_names
[regno
]);
This page took 0.04486 seconds and 5 git commands to generate.