HPPA merge.
[deliverable/binutils-gdb.git] / gdb / m68k-pinsn.c
1 /* Print Motorola 68k instructions for GDB, the GNU debugger.
2 Copyright 1986, 1987, 1989, 1991, 1992 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
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.
10
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.
15
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. */
19
20 #include "defs.h"
21 #include "symtab.h"
22 #include "opcode/m68k.h"
23 #include "gdbcore.h"
24 #include "ieee-float.h"
25
26 /* Local function prototypes */
27
28 static int
29 fetch_arg PARAMS ((unsigned char *, int, int));
30
31 static void
32 print_base PARAMS ((int, int, FILE *));
33
34 static unsigned char *
35 print_indexed PARAMS ((int, unsigned char *, CORE_ADDR, FILE *));
36
37 static unsigned char *
38 print_insn_arg PARAMS ((char *, unsigned char *, unsigned char *, CORE_ADDR,
39 FILE *));
40
41 /* 68k instructions are never longer than this many bytes. */
42 #define MAXLEN 22
43
44 /* Number of elements in the opcode table. */
45 #define NOPCODES (sizeof m68k_opcodes / sizeof m68k_opcodes[0])
46
47 const char * const fpcr_names[] = {
48 "", "fpiar", "fpsr", "fpiar/fpsr", "fpcr",
49 "fpiar/fpcr", "fpsr/fpcr", "fpiar-fpcr"};
50
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))
59
60 #define NEXTBYTE(p) (p += 2, ((int)(p[-1]) << BYTE_SHIFT) >> BYTE_SHIFT)
61
62 #define NEXTWORD(p) \
63 (p += 2, (((int)((p[-2] << 8) + p[-1])) << WORD_SHIFT) >> WORD_SHIFT)
64
65 #define NEXTLONG(p) \
66 (p += 4, (((int)((((((p[-4] << 8) + p[-3]) << 8) + p[-2]) << 8) + p[-1])) \
67 << LONG_SHIFT) >> LONG_SHIFT)
68
69 /* Ecch -- assumes host == target float formats. FIXME. */
70 #define NEXTSINGLE(p) \
71 (p += 4, *((float *)(p - 4)))
72
73 #define NEXTDOUBLE(p) \
74 (p += 8, *((double *)(p - 8)))
75 \f
76 /* Print the m68k instruction at address MEMADDR in debugged memory,
77 on STREAM. Returns length of the instruction, in bytes. */
78
79 int
80 print_insn (memaddr, stream)
81 CORE_ADDR memaddr;
82 FILE *stream;
83 {
84 unsigned char buffer[MAXLEN];
85 register int i;
86 register unsigned char *p;
87 register char *d;
88 register int bestmask;
89 int best;
90
91 read_memory (memaddr, (char *) buffer, MAXLEN);
92
93 bestmask = 0;
94 best = -1;
95 for (i = 0; i < NOPCODES; i++)
96 {
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)))
103 {
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)
108 if (d[1] == 'D')
109 break;
110
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. */
114 if (*d == 0)
115 for (d = m68k_opcodes[i].args; *d; d += 2)
116 if (d[1] == 't')
117 break;
118
119 if (*d == 0 && match > bestmask)
120 {
121 best = i;
122 bestmask = match;
123 }
124 }
125 }
126
127 /* Handle undefined instructions. */
128 if (best < 0)
129 {
130 fprintf_filtered (stream, "0%o", (buffer[0] << 8) + buffer[1]);
131 return 2;
132 }
133
134 fprintf_filtered (stream, "%s", m68k_opcodes[best].name);
135
136 /* Point at first word of argument data,
137 and at descriptor for first argument. */
138 p = buffer + 2;
139
140 /* Why do this this way? -MelloN */
141 for (d = m68k_opcodes[best].args; *d; d += 2)
142 {
143 if (d[0] == '#')
144 {
145 if (d[1] == 'l' && p - buffer < 6)
146 p = buffer + 6;
147 else if (p - buffer < 4 && d[1] != 'C' && d[1] != '8' )
148 p = buffer + 4;
149 }
150 if (d[1] >= '1' && d[1] <= '3' && p - buffer < 4)
151 p = buffer + 4;
152 if (d[1] >= '4' && d[1] <= '6' && p - buffer < 6)
153 p = buffer + 6;
154 if ((d[0] == 'L' || d[0] == 'l') && d[1] == 'w' && p - buffer < 4)
155 p = buffer + 4;
156 }
157
158 d = m68k_opcodes[best].args;
159
160 if (*d)
161 fputs_filtered (" ", stream);
162
163 while (*d)
164 {
165 p = print_insn_arg (d, buffer, p, memaddr + p - buffer, stream);
166 d += 2;
167 if (*d && *(d - 2) != 'I' && *d != 'k')
168 fputs_filtered (",", stream);
169 }
170 return p - buffer;
171 }
172
173 static unsigned char *
174 print_insn_arg (d, buffer, p, addr, stream)
175 char *d;
176 unsigned char *buffer;
177 register unsigned char *p;
178 CORE_ADDR addr; /* PC for this arg to be relative to */
179 FILE *stream;
180 {
181 register int val;
182 register int place = d[1];
183 int regno;
184 register const char *regname;
185 register unsigned char *p1;
186 double flval;
187 int flt_p;
188
189 switch (*d)
190 {
191 case 'C':
192 fprintf_filtered (stream, "ccr");
193 break;
194
195 case 'S':
196 fprintf_filtered (stream, "sr");
197 break;
198
199 case 'U':
200 fprintf_filtered (stream, "usp");
201 break;
202
203 case 'J':
204 {
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}};
209
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)
213 {
214 fprintf_filtered (stream, names[regno].name);
215 break;
216 }
217 if (regno < 0)
218 fprintf_filtered (stream, "%d", val);
219 }
220 break;
221
222 case 'Q':
223 val = fetch_arg (buffer, place, 3);
224 /* 0 means 8, except for the bkpt instruction... */
225 if (val == 0 && d[1] != 's')
226 val = 8;
227 fprintf_filtered (stream, "#%d", val);
228 break;
229
230 case 'M':
231 val = fetch_arg (buffer, place, 8);
232 if (val & 0x80)
233 val = val - 0x100;
234 fprintf_filtered (stream, "#%d", val);
235 break;
236
237 case 'T':
238 val = fetch_arg (buffer, place, 4);
239 fprintf_filtered (stream, "#%d", val);
240 break;
241
242 case 'D':
243 fprintf_filtered (stream, "%s", reg_names[fetch_arg (buffer, place, 3)]);
244 break;
245
246 case 'A':
247 fprintf_filtered (stream, "%s",
248 reg_names[fetch_arg (buffer, place, 3) + 010]);
249 break;
250
251 case 'R':
252 fprintf_filtered (stream, "%s", reg_names[fetch_arg (buffer, place, 4)]);
253 break;
254
255 case 'F':
256 fprintf_filtered (stream, "fp%d", fetch_arg (buffer, place, 3));
257 break;
258
259 case 'O':
260 val = fetch_arg (buffer, place, 6);
261 if (val & 0x20)
262 fprintf_filtered (stream, "%s", reg_names [val & 7]);
263 else
264 fprintf_filtered (stream, "%d", val);
265 break;
266
267 case '+':
268 fprintf_filtered (stream, "%s@+",
269 reg_names[fetch_arg (buffer, place, 3) + 8]);
270 break;
271
272 case '-':
273 fprintf_filtered (stream, "%s@-",
274 reg_names[fetch_arg (buffer, place, 3) + 8]);
275 break;
276
277 case 'k':
278 if (place == 'k')
279 fprintf_filtered (stream, "{%s}", reg_names[fetch_arg (buffer, place, 3)]);
280 else if (place == 'C')
281 {
282 val = fetch_arg (buffer, place, 7);
283 if ( val > 63 ) /* This is a signed constant. */
284 val -= 128;
285 fprintf_filtered (stream, "{#%d}", val);
286 }
287 else
288 error ("Invalid arg format in opcode table: \"%c%c\".",
289 *d, place);
290 break;
291
292 case '#':
293 case '^':
294 p1 = buffer + (*d == '#' ? 2 : 4);
295 if (place == 's')
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')
304 val = NEXTBYTE (p1);
305 else if (place == 'w')
306 val = NEXTWORD (p1);
307 else if (place == 'l')
308 val = NEXTLONG (p1);
309 else
310 error ("Invalid arg format in opcode table: \"%c%c\".",
311 *d, place);
312 fprintf_filtered (stream, "#%d", val);
313 break;
314
315 case 'B':
316 if (place == 'b')
317 val = NEXTBYTE (p);
318 else if (place == 'B')
319 val = NEXTBYTE (buffer); /* from the opcode word */
320 else if (place == 'w' || place == 'W')
321 val = NEXTWORD (p);
322 else if (place == 'l' || place == 'L')
323 val = NEXTLONG (p);
324 else if (place == 'g')
325 {
326 val = NEXTBYTE (buffer);
327 if (val == 0)
328 val = NEXTWORD (p);
329 else if (val == -1)
330 val = NEXTLONG (p);
331 }
332 else if (place == 'c')
333 {
334 if (buffer[1] & 0x40) /* If bit six is one, long offset */
335 val = NEXTLONG (p);
336 else
337 val = NEXTWORD (p);
338 }
339 else
340 error ("Invalid arg format in opcode table: \"%c%c\".",
341 *d, place);
342
343 print_address (addr + val, stream);
344 break;
345
346 case 'd':
347 val = NEXTWORD (p);
348 fprintf_filtered (stream, "%s@(%d)",
349 reg_names[fetch_arg (buffer, place, 3)], val);
350 break;
351
352 case 's':
353 fprintf_filtered (stream, "%s",
354 fpcr_names[fetch_arg (buffer, place, 3)]);
355 break;
356
357 case 'I':
358 val = fetch_arg (buffer, 'd', 3); /* Get coprocessor ID... */
359 if (val != 1) /* Unusual coprocessor ID? */
360 fprintf_filtered (stream, "(cpid=%d) ", val);
361 if (place == 'i')
362 p += 2; /* Skip coprocessor extended operands */
363 break;
364
365 case '*':
366 case '~':
367 case '%':
368 case ';':
369 case '@':
370 case '!':
371 case '$':
372 case '?':
373 case '/':
374 case '&':
375
376 if (place == 'd')
377 {
378 val = fetch_arg (buffer, 'x', 6);
379 val = ((val & 7) << 3) + ((val >> 3) & 7);
380 }
381 else
382 val = fetch_arg (buffer, 's', 6);
383
384 /* Get register number assuming address register. */
385 regno = (val & 7) + 8;
386 regname = reg_names[regno];
387 switch (val >> 3)
388 {
389 case 0:
390 fprintf_filtered (stream, "%s", reg_names[val]);
391 break;
392
393 case 1:
394 fprintf_filtered (stream, "%s", regname);
395 break;
396
397 case 2:
398 fprintf_filtered (stream, "%s@", regname);
399 break;
400
401 case 3:
402 fprintf_filtered (stream, "%s@+", regname);
403 break;
404
405 case 4:
406 fprintf_filtered (stream, "%s@-", regname);
407 break;
408
409 case 5:
410 val = NEXTWORD (p);
411 fprintf_filtered (stream, "%s@(%d)", regname, val);
412 break;
413
414 case 6:
415 p = print_indexed (regno, p, addr, stream);
416 break;
417
418 case 7:
419 switch (val & 7)
420 {
421 case 0:
422 val = NEXTWORD (p);
423 fprintf_filtered (stream, "@#");
424 print_address (val, stream);
425 break;
426
427 case 1:
428 val = NEXTLONG (p);
429 fprintf_filtered (stream, "@#");
430 print_address (val, stream);
431 break;
432
433 case 2:
434 val = NEXTWORD (p);
435 print_address (addr + val, stream);
436 break;
437
438 case 3:
439 p = print_indexed (-1, p, addr, stream);
440 break;
441
442 case 4:
443 flt_p = 1; /* Assume it's a float... */
444 switch( place )
445 {
446 case 'b':
447 val = NEXTBYTE (p);
448 flt_p = 0;
449 break;
450
451 case 'w':
452 val = NEXTWORD (p);
453 flt_p = 0;
454 break;
455
456 case 'l':
457 val = NEXTLONG (p);
458 flt_p = 0;
459 break;
460
461 case 'f':
462 flval = NEXTSINGLE(p);
463 break;
464
465 case 'F':
466 flval = NEXTDOUBLE(p);
467 break;
468
469 case 'x':
470 ieee_extended_to_double (&ext_format_68881, p, &flval);
471 p += 12;
472 break;
473
474 case 'p':
475 p += 12;
476 flval = 0; /* FIXME, handle packed decimal someday. */
477 break;
478
479 default:
480 error ("Invalid arg format in opcode table: \"%c%c\".",
481 *d, place);
482 }
483 if ( flt_p ) /* Print a float? */
484 fprintf_filtered (stream, "#%g", flval);
485 else
486 fprintf_filtered (stream, "#%d", val);
487 break;
488
489 default:
490 fprintf_filtered (stream, "<invalid address mode 0%o>", val);
491 }
492 }
493 break;
494
495 case 'L':
496 case 'l':
497 if (place == 'w')
498 {
499 char doneany;
500 p1 = buffer + 2;
501 val = NEXTWORD (p1);
502 /* Move the pointer ahead if this point is farther ahead
503 than the last. */
504 p = p1 > p ? p1 : p;
505 if (val == 0)
506 {
507 fputs_filtered ("#0", stream);
508 break;
509 }
510 if (*d == 'l')
511 {
512 register int newval = 0;
513 for (regno = 0; regno < 16; ++regno)
514 if (val & (0x8000 >> regno))
515 newval |= 1 << regno;
516 val = newval;
517 }
518 val &= 0xffff;
519 doneany = 0;
520 for (regno = 0; regno < 16; ++regno)
521 if (val & (1 << regno))
522 {
523 int first_regno;
524 if (doneany)
525 fputs_filtered ("/", stream);
526 doneany = 1;
527 fprintf_filtered (stream, "%s", reg_names[regno]);
528 first_regno = regno;
529 while (val & (1 << (regno + 1)))
530 ++regno;
531 if (regno > first_regno)
532 fprintf_filtered (stream, "-%s", reg_names[regno]);
533 }
534 }
535 else if (place == '3')
536 {
537 /* `fmovem' insn. */
538 char doneany;
539 val = fetch_arg (buffer, place, 8);
540 if (val == 0)
541 {
542 fputs_filtered ("#0", stream);
543 break;
544 }
545 if (*d == 'l')
546 {
547 register int newval = 0;
548 for (regno = 0; regno < 8; ++regno)
549 if (val & (0x80 >> regno))
550 newval |= 1 << regno;
551 val = newval;
552 }
553 val &= 0xff;
554 doneany = 0;
555 for (regno = 0; regno < 8; ++regno)
556 if (val & (1 << regno))
557 {
558 int first_regno;
559 if (doneany)
560 fputs_filtered ("/", stream);
561 doneany = 1;
562 fprintf_filtered (stream, "fp%d", regno);
563 first_regno = regno;
564 while (val & (1 << (regno + 1)))
565 ++regno;
566 if (regno > first_regno)
567 fprintf_filtered (stream, "-fp%d", regno);
568 }
569 }
570 else
571 goto de_fault;
572 break;
573
574 default: de_fault:
575 error ("Invalid arg format in opcode table: \"%c\".", *d);
576 }
577
578 return (unsigned char *) p;
579 }
580
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. */
585
586 static int
587 fetch_arg (buffer, code, bits)
588 unsigned char *buffer;
589 int code;
590 int bits;
591 {
592 register int val;
593 switch (code)
594 {
595 case 's':
596 val = buffer[1];
597 break;
598
599 case 'd': /* Destination, for register or quick. */
600 val = (buffer[0] << 8) + buffer[1];
601 val >>= 9;
602 break;
603
604 case 'x': /* Destination, for general arg */
605 val = (buffer[0] << 8) + buffer[1];
606 val >>= 6;
607 break;
608
609 case 'k':
610 val = (buffer[3] >> 4);
611 break;
612
613 case 'C':
614 val = buffer[3];
615 break;
616
617 case '1':
618 val = (buffer[2] << 8) + buffer[3];
619 val >>= 12;
620 break;
621
622 case '2':
623 val = (buffer[2] << 8) + buffer[3];
624 val >>= 6;
625 break;
626
627 case '3':
628 case 'j':
629 val = (buffer[2] << 8) + buffer[3];
630 break;
631
632 case '4':
633 val = (buffer[4] << 8) + buffer[5];
634 val >>= 12;
635 break;
636
637 case '5':
638 val = (buffer[4] << 8) + buffer[5];
639 val >>= 6;
640 break;
641
642 case '6':
643 val = (buffer[4] << 8) + buffer[5];
644 break;
645
646 case '7':
647 val = (buffer[2] << 8) + buffer[3];
648 val >>= 7;
649 break;
650
651 case '8':
652 val = (buffer[2] << 8) + buffer[3];
653 val >>= 10;
654 break;
655
656 default:
657 abort ();
658 }
659
660 switch (bits)
661 {
662 case 3:
663 return val & 7;
664 case 4:
665 return val & 017;
666 case 5:
667 return val & 037;
668 case 6:
669 return val & 077;
670 case 7:
671 return val & 0177;
672 case 8:
673 return val & 0377;
674 case 12:
675 return val & 07777;
676 default:
677 abort ();
678 }
679 }
680
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. */
684
685 static unsigned char *
686 print_indexed (basereg, p, addr, stream)
687 int basereg;
688 unsigned char *p;
689 CORE_ADDR addr;
690 FILE *stream;
691 {
692 register int word;
693 static char *scales[] = {"", "*2", "*4", "*8"};
694 register int base_disp;
695 register int outer_disp;
696 char buf[40];
697
698 word = NEXTWORD (p);
699
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]);
706
707 /* Handle the 68000 style of indexing. */
708
709 if ((word & 0x100) == 0)
710 {
711 print_base (basereg,
712 ((word & 0x80) ? word | 0xff00 : word & 0xff)
713 + ((basereg == -1) ? addr : 0),
714 stream);
715 fputs_filtered (buf, stream);
716 return p;
717 }
718
719 /* Handle the generalized kind. */
720 /* First, compute the displacement to add to the base register. */
721
722 if (word & 0200)
723 basereg = -2;
724 if (word & 0100)
725 buf[0] = 0;
726 base_disp = 0;
727 switch ((word >> 4) & 3)
728 {
729 case 2:
730 base_disp = NEXTWORD (p);
731 break;
732 case 3:
733 base_disp = NEXTLONG (p);
734 }
735 if (basereg == -1)
736 base_disp += addr;
737
738 /* Handle single-level case (not indirect) */
739
740 if ((word & 7) == 0)
741 {
742 print_base (basereg, base_disp, stream);
743 fputs_filtered (buf, stream);
744 return p;
745 }
746
747 /* Two level. Compute displacement to add after indirection. */
748
749 outer_disp = 0;
750 switch (word & 3)
751 {
752 case 2:
753 outer_disp = NEXTWORD (p);
754 break;
755 case 3:
756 outer_disp = NEXTLONG (p);
757 }
758
759 fprintf_filtered (stream, "%d(", outer_disp);
760 print_base (basereg, base_disp, stream);
761
762 /* If postindexed, print the closeparen before the index. */
763 if (word & 4)
764 fprintf_filtered (stream, ")%s", buf);
765 /* If preindexed, print the closeparen after the index. */
766 else
767 fprintf_filtered (stream, "%s)", buf);
768
769 return p;
770 }
771
772 /* Print a base register REGNO and displacement DISP, on STREAM.
773 REGNO = -1 for pc, -2 for none (suppressed). */
774
775 static void
776 print_base (regno, disp, stream)
777 int regno;
778 int disp;
779 FILE *stream;
780 {
781 if (regno == -2)
782 fprintf_filtered (stream, "%d", disp);
783 else if (regno == -1)
784 fprintf_filtered (stream, "0x%x", disp);
785 else
786 fprintf_filtered (stream, "%d(%s)", disp, reg_names[regno]);
787 }
This page took 0.064593 seconds and 4 git commands to generate.