* m68k-pinsn.c: Minor fix in style of output (don't use
[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/fpsr/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 #ifdef HAVE_68881
470 case 'x':
471 ieee_extended_to_double (&ext_format_68881, p, &flval);
472 p += 12;
473 break;
474 #endif
475
476 case 'p':
477 p += 12;
478 flval = 0; /* FIXME, handle packed decimal someday. */
479 break;
480
481 default:
482 error ("Invalid arg format in opcode table: \"%c%c\".",
483 *d, place);
484 }
485 if ( flt_p ) /* Print a float? */
486 fprintf_filtered (stream, "#%g", flval);
487 else
488 fprintf_filtered (stream, "#%d", val);
489 break;
490
491 default:
492 fprintf_filtered (stream, "<invalid address mode 0%o>", val);
493 }
494 }
495 break;
496
497 case 'L':
498 case 'l':
499 if (place == 'w')
500 {
501 char doneany;
502 p1 = buffer + 2;
503 val = NEXTWORD (p1);
504 /* Move the pointer ahead if this point is farther ahead
505 than the last. */
506 p = p1 > p ? p1 : p;
507 if (val == 0)
508 {
509 fputs_filtered ("#0", stream);
510 break;
511 }
512 if (*d == 'l')
513 {
514 register int newval = 0;
515 for (regno = 0; regno < 16; ++regno)
516 if (val & (0x8000 >> regno))
517 newval |= 1 << regno;
518 val = newval;
519 }
520 val &= 0xffff;
521 doneany = 0;
522 for (regno = 0; regno < 16; ++regno)
523 if (val & (1 << regno))
524 {
525 int first_regno;
526 if (doneany)
527 fputs_filtered ("/", stream);
528 doneany = 1;
529 fprintf_filtered (stream, "%s", reg_names[regno]);
530 first_regno = regno;
531 while (val & (1 << (regno + 1)))
532 ++regno;
533 if (regno > first_regno)
534 fprintf_filtered (stream, "-%s", reg_names[regno]);
535 }
536 }
537 else if (place == '3')
538 {
539 /* `fmovem' insn. */
540 char doneany;
541 val = fetch_arg (buffer, place, 8);
542 if (val == 0)
543 {
544 fputs_filtered ("#0", stream);
545 break;
546 }
547 if (*d == 'l')
548 {
549 register int newval = 0;
550 for (regno = 0; regno < 8; ++regno)
551 if (val & (0x80 >> regno))
552 newval |= 1 << regno;
553 val = newval;
554 }
555 val &= 0xff;
556 doneany = 0;
557 for (regno = 0; regno < 8; ++regno)
558 if (val & (1 << regno))
559 {
560 int first_regno;
561 if (doneany)
562 fputs_filtered ("/", stream);
563 doneany = 1;
564 fprintf_filtered (stream, "fp%d", regno);
565 first_regno = regno;
566 while (val & (1 << (regno + 1)))
567 ++regno;
568 if (regno > first_regno)
569 fprintf_filtered (stream, "-fp%d", regno);
570 }
571 }
572 else
573 goto de_fault;
574 break;
575
576 default: de_fault:
577 error ("Invalid arg format in opcode table: \"%c\".", *d);
578 }
579
580 return (unsigned char *) p;
581 }
582
583 /* Fetch BITS bits from a position in the instruction specified by CODE.
584 CODE is a "place to put an argument", or 'x' for a destination
585 that is a general address (mode and register).
586 BUFFER contains the instruction. */
587
588 static int
589 fetch_arg (buffer, code, bits)
590 unsigned char *buffer;
591 int code;
592 int bits;
593 {
594 register int val;
595 switch (code)
596 {
597 case 's':
598 val = buffer[1];
599 break;
600
601 case 'd': /* Destination, for register or quick. */
602 val = (buffer[0] << 8) + buffer[1];
603 val >>= 9;
604 break;
605
606 case 'x': /* Destination, for general arg */
607 val = (buffer[0] << 8) + buffer[1];
608 val >>= 6;
609 break;
610
611 case 'k':
612 val = (buffer[3] >> 4);
613 break;
614
615 case 'C':
616 val = buffer[3];
617 break;
618
619 case '1':
620 val = (buffer[2] << 8) + buffer[3];
621 val >>= 12;
622 break;
623
624 case '2':
625 val = (buffer[2] << 8) + buffer[3];
626 val >>= 6;
627 break;
628
629 case '3':
630 case 'j':
631 val = (buffer[2] << 8) + buffer[3];
632 break;
633
634 case '4':
635 val = (buffer[4] << 8) + buffer[5];
636 val >>= 12;
637 break;
638
639 case '5':
640 val = (buffer[4] << 8) + buffer[5];
641 val >>= 6;
642 break;
643
644 case '6':
645 val = (buffer[4] << 8) + buffer[5];
646 break;
647
648 case '7':
649 val = (buffer[2] << 8) + buffer[3];
650 val >>= 7;
651 break;
652
653 case '8':
654 val = (buffer[2] << 8) + buffer[3];
655 val >>= 10;
656 break;
657
658 default:
659 abort ();
660 }
661
662 switch (bits)
663 {
664 case 3:
665 return val & 7;
666 case 4:
667 return val & 017;
668 case 5:
669 return val & 037;
670 case 6:
671 return val & 077;
672 case 7:
673 return val & 0177;
674 case 8:
675 return val & 0377;
676 case 12:
677 return val & 07777;
678 default:
679 abort ();
680 }
681 }
682
683 /* Print an indexed argument. The base register is BASEREG (-1 for pc).
684 P points to extension word, in buffer.
685 ADDR is the nominal core address of that extension word. */
686
687 static unsigned char *
688 print_indexed (basereg, p, addr, stream)
689 int basereg;
690 unsigned char *p;
691 CORE_ADDR addr;
692 FILE *stream;
693 {
694 register int word;
695 static char *scales[] = {"", "*2", "*4", "*8"};
696 register int base_disp;
697 register int outer_disp;
698 char buf[40];
699
700 word = NEXTWORD (p);
701
702 /* Generate the text for the index register.
703 Where this will be output is not yet determined. */
704 sprintf (buf, "[%s.%c%s]",
705 reg_names[(word >> 12) & 0xf],
706 (word & 0x800) ? 'l' : 'w',
707 scales[(word >> 9) & 3]);
708
709 /* Handle the 68000 style of indexing. */
710
711 if ((word & 0x100) == 0)
712 {
713 print_base (basereg,
714 ((word & 0x80) ? word | 0xff00 : word & 0xff)
715 + ((basereg == -1) ? addr : 0),
716 stream);
717 fputs_filtered (buf, stream);
718 return p;
719 }
720
721 /* Handle the generalized kind. */
722 /* First, compute the displacement to add to the base register. */
723
724 if (word & 0200)
725 basereg = -2;
726 if (word & 0100)
727 buf[0] = 0;
728 base_disp = 0;
729 switch ((word >> 4) & 3)
730 {
731 case 2:
732 base_disp = NEXTWORD (p);
733 break;
734 case 3:
735 base_disp = NEXTLONG (p);
736 }
737 if (basereg == -1)
738 base_disp += addr;
739
740 /* Handle single-level case (not indirect) */
741
742 if ((word & 7) == 0)
743 {
744 print_base (basereg, base_disp, stream);
745 fputs_filtered (buf, stream);
746 return p;
747 }
748
749 /* Two level. Compute displacement to add after indirection. */
750
751 outer_disp = 0;
752 switch (word & 3)
753 {
754 case 2:
755 outer_disp = NEXTWORD (p);
756 break;
757 case 3:
758 outer_disp = NEXTLONG (p);
759 }
760
761 fprintf_filtered (stream, "%d(", outer_disp);
762 print_base (basereg, base_disp, stream);
763
764 /* If postindexed, print the closeparen before the index. */
765 if (word & 4)
766 fprintf_filtered (stream, ")%s", buf);
767 /* If preindexed, print the closeparen after the index. */
768 else
769 fprintf_filtered (stream, "%s)", buf);
770
771 return p;
772 }
773
774 /* Print a base register REGNO and displacement DISP, on STREAM.
775 REGNO = -1 for pc, -2 for none (suppressed). */
776
777 static void
778 print_base (regno, disp, stream)
779 int regno;
780 int disp;
781 FILE *stream;
782 {
783 if (regno == -2)
784 fprintf_filtered (stream, "%d", disp);
785 else if (regno == -1)
786 fprintf_filtered (stream, "0x%x", disp);
787 else
788 fprintf_filtered (stream, "%d(%s)", disp, reg_names[regno]);
789 }