Opps - forgot to include change to Makefile.am in the ChangeLog entry
[deliverable/binutils-gdb.git] / opcodes / m68k-dis.c
CommitLineData
252b5132 1/* Print Motorola 68k instructions.
060d22b0 2 Copyright 1986, 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
266abb8f 3 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
252b5132
RH
4 Free Software Foundation, Inc.
5
3e602632
NC
6 This file 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.
252b5132 10
3e602632
NC
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.
252b5132 15
3e602632
NC
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
47b0e7ad
NC
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
252b5132 20
0d8dfecf 21#include "sysdep.h"
252b5132
RH
22#include "dis-asm.h"
23#include "floatformat.h"
19f33eee 24#include "libiberty.h"
252b5132
RH
25#include "opintl.h"
26
27#include "opcode/m68k.h"
28
47b0e7ad 29/* Local function prototypes. */
be8c092b
NC
30
31const char * const fpcr_names[] =
32{
47b0e7ad
NC
33 "", "%fpiar", "%fpsr", "%fpiar/%fpsr", "%fpcr",
34 "%fpiar/%fpcr", "%fpsr/%fpcr", "%fpiar/%fpsr/%fpcr"
ec22bdda 35};
252b5132 36
be8c092b
NC
37static char *const reg_names[] =
38{
47b0e7ad
NC
39 "%d0", "%d1", "%d2", "%d3", "%d4", "%d5", "%d6", "%d7",
40 "%a0", "%a1", "%a2", "%a3", "%a4", "%a5", "%fp", "%sp",
41 "%ps", "%pc"
ec22bdda 42};
252b5132 43
be8c092b
NC
44/* Name of register halves for MAC/EMAC.
45 Seperate from reg_names since 'spu', 'fpl' look weird. */
46static char *const reg_half_names[] =
47{
47b0e7ad
NC
48 "%d0", "%d1", "%d2", "%d3", "%d4", "%d5", "%d6", "%d7",
49 "%a0", "%a1", "%a2", "%a3", "%a4", "%a5", "%a6", "%a7",
50 "%ps", "%pc"
be8c092b
NC
51};
52
53/* Sign-extend an (unsigned char). */
252b5132 54#if __STDC__ == 1
ec22bdda 55#define COERCE_SIGNED_CHAR(ch) ((signed char) (ch))
252b5132 56#else
ec22bdda 57#define COERCE_SIGNED_CHAR(ch) ((int) (((ch) ^ 0x80) & 0xFF) - 128)
252b5132
RH
58#endif
59
60/* Get a 1 byte signed integer. */
61#define NEXTBYTE(p) (p += 2, FETCH_DATA (info, p), COERCE_SIGNED_CHAR(p[-1]))
62
63/* Get a 2 byte signed integer. */
64#define COERCE16(x) ((int) (((x) ^ 0x8000) - 0x8000))
65#define NEXTWORD(p) \
66 (p += 2, FETCH_DATA (info, p), \
67 COERCE16 ((p[-2] << 8) + p[-1]))
68
69/* Get a 4 byte signed integer. */
70#define COERCE32(x) ((bfd_signed_vma) ((x) ^ 0x80000000) - 0x80000000)
71#define NEXTLONG(p) \
72 (p += 4, FETCH_DATA (info, p), \
73 (COERCE32 ((((((p[-4] << 8) + p[-3]) << 8) + p[-2]) << 8) + p[-1])))
74
75/* Get a 4 byte unsigned integer. */
76#define NEXTULONG(p) \
77 (p += 4, FETCH_DATA (info, p), \
78 (unsigned int) ((((((p[-4] << 8) + p[-3]) << 8) + p[-2]) << 8) + p[-1]))
79
80/* Get a single precision float. */
81#define NEXTSINGLE(val, p) \
82 (p += 4, FETCH_DATA (info, p), \
83 floatformat_to_double (&floatformat_ieee_single_big, (char *) p - 4, &val))
84
85/* Get a double precision float. */
86#define NEXTDOUBLE(val, p) \
87 (p += 8, FETCH_DATA (info, p), \
88 floatformat_to_double (&floatformat_ieee_double_big, (char *) p - 8, &val))
89
90/* Get an extended precision float. */
91#define NEXTEXTEND(val, p) \
92 (p += 12, FETCH_DATA (info, p), \
93 floatformat_to_double (&floatformat_m68881_ext, (char *) p - 12, &val))
94
95/* Need a function to convert from packed to double
96 precision. Actually, it's easier to print a
97 packed number than a double anyway, so maybe
98 there should be a special case to handle this... */
99#define NEXTPACKED(p) \
100 (p += 12, FETCH_DATA (info, p), 0.0)
252b5132
RH
101\f
102/* Maximum length of an instruction. */
103#define MAXLEN 22
104
105#include <setjmp.h>
106
47b0e7ad
NC
107struct private
108{
252b5132
RH
109 /* Points to first byte not fetched. */
110 bfd_byte *max_fetched;
111 bfd_byte the_buffer[MAXLEN];
112 bfd_vma insn_start;
113 jmp_buf bailout;
114};
115
116/* Make sure that bytes from INFO->PRIVATE_DATA->BUFFER (inclusive)
117 to ADDR (exclusive) are valid. Returns 1 for success, longjmps
118 on error. */
119#define FETCH_DATA(info, addr) \
ec22bdda 120 ((addr) <= ((struct private *) (info->private_data))->max_fetched \
252b5132
RH
121 ? 1 : fetch_data ((info), (addr)))
122
123static int
cc16ba8c 124fetch_data (struct disassemble_info *info, bfd_byte *addr)
252b5132
RH
125{
126 int status;
127 struct private *priv = (struct private *)info->private_data;
128 bfd_vma start = priv->insn_start + (priv->max_fetched - priv->the_buffer);
129
130 status = (*info->read_memory_func) (start,
131 priv->max_fetched,
132 addr - priv->max_fetched,
133 info);
134 if (status != 0)
135 {
136 (*info->memory_error_func) (status, start, info);
137 longjmp (priv->bailout, 1);
138 }
139 else
140 priv->max_fetched = addr;
141 return 1;
142}
143\f
47b0e7ad 144/* This function is used to print to the bit-bucket. */
252b5132 145static int
ec22bdda 146dummy_printer (FILE *file ATTRIBUTE_UNUSED,
47b0e7ad
NC
147 const char *format ATTRIBUTE_UNUSED,
148 ...)
ec22bdda
KH
149{
150 return 0;
151}
252b5132
RH
152
153static void
47b0e7ad
NC
154dummy_print_address (bfd_vma vma ATTRIBUTE_UNUSED,
155 struct disassemble_info *info ATTRIBUTE_UNUSED)
252b5132
RH
156{
157}
158
47b0e7ad
NC
159/* Fetch BITS bits from a position in the instruction specified by CODE.
160 CODE is a "place to put an argument", or 'x' for a destination
161 that is a general address (mode and register).
162 BUFFER contains the instruction. */
be8c092b
NC
163
164static int
47b0e7ad
NC
165fetch_arg (unsigned char *buffer,
166 int code,
167 int bits,
168 disassemble_info *info)
be8c092b 169{
47b0e7ad 170 int val = 0;
be8c092b 171
47b0e7ad 172 switch (code)
be8c092b 173 {
47b0e7ad
NC
174 case '/': /* MAC/EMAC mask bit. */
175 val = buffer[3] >> 5;
176 break;
be8c092b 177
47b0e7ad
NC
178 case 'G': /* EMAC ACC load. */
179 val = ((buffer[3] >> 3) & 0x2) | ((~buffer[1] >> 7) & 0x1);
180 break;
be8c092b 181
47b0e7ad
NC
182 case 'H': /* EMAC ACC !load. */
183 val = ((buffer[3] >> 3) & 0x2) | ((buffer[1] >> 7) & 0x1);
184 break;
be8c092b 185
47b0e7ad
NC
186 case ']': /* EMAC ACCEXT bit. */
187 val = buffer[0] >> 2;
188 break;
be8c092b 189
47b0e7ad
NC
190 case 'I': /* MAC/EMAC scale factor. */
191 val = buffer[2] >> 1;
192 break;
be8c092b 193
47b0e7ad
NC
194 case 'F': /* EMAC ACCx. */
195 val = buffer[0] >> 1;
196 break;
be8c092b 197
47b0e7ad
NC
198 case 'f':
199 val = buffer[1];
200 break;
be8c092b 201
47b0e7ad
NC
202 case 's':
203 val = buffer[1];
204 break;
be8c092b 205
47b0e7ad
NC
206 case 'd': /* Destination, for register or quick. */
207 val = (buffer[0] << 8) + buffer[1];
208 val >>= 9;
209 break;
be8c092b 210
47b0e7ad
NC
211 case 'x': /* Destination, for general arg. */
212 val = (buffer[0] << 8) + buffer[1];
213 val >>= 6;
214 break;
be8c092b 215
47b0e7ad
NC
216 case 'k':
217 FETCH_DATA (info, buffer + 3);
218 val = (buffer[3] >> 4);
219 break;
be8c092b 220
47b0e7ad
NC
221 case 'C':
222 FETCH_DATA (info, buffer + 3);
223 val = buffer[3];
224 break;
be8c092b 225
47b0e7ad
NC
226 case '1':
227 FETCH_DATA (info, buffer + 3);
228 val = (buffer[2] << 8) + buffer[3];
229 val >>= 12;
230 break;
be8c092b 231
47b0e7ad
NC
232 case '2':
233 FETCH_DATA (info, buffer + 3);
234 val = (buffer[2] << 8) + buffer[3];
235 val >>= 6;
236 break;
be8c092b 237
47b0e7ad
NC
238 case '3':
239 case 'j':
240 FETCH_DATA (info, buffer + 3);
241 val = (buffer[2] << 8) + buffer[3];
242 break;
be8c092b 243
47b0e7ad
NC
244 case '4':
245 FETCH_DATA (info, buffer + 5);
246 val = (buffer[4] << 8) + buffer[5];
247 val >>= 12;
248 break;
be8c092b 249
47b0e7ad
NC
250 case '5':
251 FETCH_DATA (info, buffer + 5);
252 val = (buffer[4] << 8) + buffer[5];
253 val >>= 6;
254 break;
be8c092b 255
47b0e7ad
NC
256 case '6':
257 FETCH_DATA (info, buffer + 5);
258 val = (buffer[4] << 8) + buffer[5];
259 break;
252b5132 260
47b0e7ad
NC
261 case '7':
262 FETCH_DATA (info, buffer + 3);
263 val = (buffer[2] << 8) + buffer[3];
264 val >>= 7;
265 break;
252b5132 266
47b0e7ad
NC
267 case '8':
268 FETCH_DATA (info, buffer + 3);
269 val = (buffer[2] << 8) + buffer[3];
270 val >>= 10;
271 break;
252b5132 272
47b0e7ad
NC
273 case '9':
274 FETCH_DATA (info, buffer + 3);
275 val = (buffer[2] << 8) + buffer[3];
276 val >>= 5;
277 break;
252b5132 278
47b0e7ad
NC
279 case 'e':
280 val = (buffer[1] >> 6);
281 break;
be8c092b 282
afa2158f
NS
283 case 'E':
284 FETCH_DATA (info, buffer + 3);
285 val = (buffer[2] >> 1);
286 break;
287
47b0e7ad
NC
288 case 'm':
289 val = (buffer[1] & 0x40 ? 0x8 : 0)
290 | ((buffer[0] >> 1) & 0x7)
291 | (buffer[3] & 0x80 ? 0x10 : 0);
292 break;
252b5132 293
47b0e7ad
NC
294 case 'n':
295 val = (buffer[1] & 0x40 ? 0x8 : 0) | ((buffer[0] >> 1) & 0x7);
296 break;
252b5132 297
47b0e7ad
NC
298 case 'o':
299 val = (buffer[2] >> 4) | (buffer[3] & 0x80 ? 0x10 : 0);
300 break;
be8c092b 301
47b0e7ad
NC
302 case 'M':
303 val = (buffer[1] & 0xf) | (buffer[3] & 0x40 ? 0x10 : 0);
304 break;
252b5132 305
47b0e7ad
NC
306 case 'N':
307 val = (buffer[3] & 0xf) | (buffer[3] & 0x40 ? 0x10 : 0);
308 break;
309
310 case 'h':
311 val = buffer[2] >> 2;
312 break;
313
314 default:
315 abort ();
316 }
317
afa2158f
NS
318 /* bits is never too big. */
319 return val & ((1 << bits) - 1);
47b0e7ad
NC
320}
321
322/* Check if an EA is valid for a particular code. This is required
323 for the EMAC instructions since the type of source address determines
324 if it is a EMAC-load instruciton if the EA is mode 2-5, otherwise it
325 is a non-load EMAC instruction and the bits mean register Ry.
326 A similar case exists for the movem instructions where the register
327 mask is interpreted differently for different EAs. */
328
329static bfd_boolean
330m68k_valid_ea (char code, int val)
331{
332 int mode, mask;
333#define M(n0,n1,n2,n3,n4,n5,n6,n70,n71,n72,n73,n74) \
334 (n0 | n1 << 1 | n2 << 2 | n3 << 3 | n4 << 4 | n5 << 5 | n6 << 6 \
335 | n70 << 7 | n71 << 8 | n72 << 9 | n73 << 10 | n74 << 11)
336
337 switch (code)
338 {
339 case '*':
340 mask = M (1,1,1,1,1,1,1,1,1,1,1,1);
252b5132 341 break;
47b0e7ad
NC
342 case '~':
343 mask = M (0,0,1,1,1,1,1,1,1,0,0,0);
252b5132 344 break;
47b0e7ad
NC
345 case '%':
346 mask = M (1,1,1,1,1,1,1,1,1,0,0,0);
252b5132 347 break;
47b0e7ad
NC
348 case ';':
349 mask = M (1,0,1,1,1,1,1,1,1,1,1,1);
252b5132 350 break;
47b0e7ad
NC
351 case '@':
352 mask = M (1,0,1,1,1,1,1,1,1,1,1,0);
252b5132 353 break;
47b0e7ad
NC
354 case '!':
355 mask = M (0,0,1,0,0,1,1,1,1,1,1,0);
252b5132 356 break;
47b0e7ad
NC
357 case '&':
358 mask = M (0,0,1,0,0,1,1,1,1,0,0,0);
252b5132 359 break;
47b0e7ad
NC
360 case '$':
361 mask = M (1,0,1,1,1,1,1,1,1,0,0,0);
252b5132 362 break;
47b0e7ad
NC
363 case '?':
364 mask = M (1,0,1,0,0,1,1,1,1,0,0,0);
9d29e1b3 365 break;
47b0e7ad
NC
366 case '/':
367 mask = M (1,0,1,0,0,1,1,1,1,1,1,0);
3e602632 368 break;
47b0e7ad
NC
369 case '|':
370 mask = M (0,0,1,0,0,1,1,1,1,1,1,0);
6b6e92f4 371 break;
47b0e7ad
NC
372 case '>':
373 mask = M (0,0,1,0,1,1,1,1,1,0,0,0);
9d29e1b3 374 break;
47b0e7ad
NC
375 case '<':
376 mask = M (0,0,1,1,0,1,1,1,1,1,1,0);
9d29e1b3 377 break;
47b0e7ad
NC
378 case 'm':
379 mask = M (1,1,1,1,1,0,0,0,0,0,0,0);
fd99574b 380 break;
47b0e7ad
NC
381 case 'n':
382 mask = M (0,0,0,0,0,1,0,0,0,1,0,0);
383 break;
384 case 'o':
385 mask = M (0,0,0,0,0,0,1,1,1,0,1,1);
386 break;
387 case 'p':
388 mask = M (1,1,1,1,1,1,0,0,0,0,0,0);
389 break;
390 case 'q':
391 mask = M (1,0,1,1,1,1,0,0,0,0,0,0);
392 break;
393 case 'v':
394 mask = M (1,0,1,1,1,1,0,1,1,0,0,0);
395 break;
396 case 'b':
397 mask = M (1,0,1,1,1,1,0,0,0,1,0,0);
398 break;
399 case 'w':
400 mask = M (0,0,1,1,1,1,0,0,0,1,0,0);
401 break;
402 case 'y':
403 mask = M (0,0,1,0,0,1,0,0,0,0,0,0);
404 break;
405 case 'z':
406 mask = M (0,0,1,0,0,1,0,0,0,1,0,0);
407 break;
408 case '4':
409 mask = M (0,0,1,1,1,1,0,0,0,0,0,0);
9d29e1b3 410 break;
47b0e7ad
NC
411 default:
412 abort ();
252b5132 413 }
47b0e7ad 414#undef M
252b5132 415
47b0e7ad
NC
416 mode = (val >> 3) & 7;
417 if (mode == 7)
418 mode += val & 7;
419 return (mask & (1 << mode)) != 0;
420}
252b5132 421
47b0e7ad
NC
422/* Print a base register REGNO and displacement DISP, on INFO->STREAM.
423 REGNO = -1 for pc, -2 for none (suppressed). */
252b5132 424
47b0e7ad
NC
425static void
426print_base (int regno, bfd_vma disp, disassemble_info *info)
427{
428 if (regno == -1)
429 {
430 (*info->fprintf_func) (info->stream, "%%pc@(");
431 (*info->print_address_func) (disp, info);
432 }
433 else
434 {
435 char buf[50];
252b5132 436
47b0e7ad
NC
437 if (regno == -2)
438 (*info->fprintf_func) (info->stream, "@(");
439 else if (regno == -3)
440 (*info->fprintf_func) (info->stream, "%%zpc@(");
441 else
442 (*info->fprintf_func) (info->stream, "%s@(", reg_names[regno]);
252b5132 443
47b0e7ad
NC
444 sprintf_vma (buf, disp);
445 (*info->fprintf_func) (info->stream, "%s", buf);
252b5132 446 }
252b5132
RH
447}
448
47b0e7ad
NC
449/* Print an indexed argument. The base register is BASEREG (-1 for pc).
450 P points to extension word, in buffer.
451 ADDR is the nominal core address of that extension word. */
cc16ba8c 452
47b0e7ad
NC
453static unsigned char *
454print_indexed (int basereg,
455 unsigned char *p,
456 bfd_vma addr,
457 disassemble_info *info)
252b5132 458{
47b0e7ad
NC
459 int word;
460 static char *const scales[] = { "", ":2", ":4", ":8" };
461 bfd_vma base_disp;
462 bfd_vma outer_disp;
463 char buf[40];
464 char vmabuf[50];
465
466 word = NEXTWORD (p);
467
468 /* Generate the text for the index register.
469 Where this will be output is not yet determined. */
470 sprintf (buf, "%s:%c%s",
471 reg_names[(word >> 12) & 0xf],
472 (word & 0x800) ? 'l' : 'w',
473 scales[(word >> 9) & 3]);
474
475 /* Handle the 68000 style of indexing. */
476
477 if ((word & 0x100) == 0)
478 {
479 base_disp = word & 0xff;
480 if ((base_disp & 0x80) != 0)
481 base_disp -= 0x100;
482 if (basereg == -1)
483 base_disp += addr;
484 print_base (basereg, base_disp, info);
485 (*info->fprintf_func) (info->stream, ",%s)", buf);
486 return p;
487 }
488
489 /* Handle the generalized kind. */
490 /* First, compute the displacement to add to the base register. */
491 if (word & 0200)
492 {
493 if (basereg == -1)
494 basereg = -3;
495 else
496 basereg = -2;
497 }
498 if (word & 0100)
499 buf[0] = '\0';
500 base_disp = 0;
501 switch ((word >> 4) & 3)
502 {
503 case 2:
504 base_disp = NEXTWORD (p);
505 break;
506 case 3:
507 base_disp = NEXTLONG (p);
508 }
509 if (basereg == -1)
510 base_disp += addr;
511
512 /* Handle single-level case (not indirect). */
513 if ((word & 7) == 0)
514 {
515 print_base (basereg, base_disp, info);
516 if (buf[0] != '\0')
517 (*info->fprintf_func) (info->stream, ",%s", buf);
518 (*info->fprintf_func) (info->stream, ")");
519 return p;
520 }
521
522 /* Two level. Compute displacement to add after indirection. */
523 outer_disp = 0;
524 switch (word & 3)
525 {
526 case 2:
527 outer_disp = NEXTWORD (p);
528 break;
529 case 3:
530 outer_disp = NEXTLONG (p);
531 }
532
533 print_base (basereg, base_disp, info);
534 if ((word & 4) == 0 && buf[0] != '\0')
535 {
536 (*info->fprintf_func) (info->stream, ",%s", buf);
537 buf[0] = '\0';
538 }
539 sprintf_vma (vmabuf, outer_disp);
540 (*info->fprintf_func) (info->stream, ")@(%s", vmabuf);
541 if (buf[0] != '\0')
542 (*info->fprintf_func) (info->stream, ",%s", buf);
543 (*info->fprintf_func) (info->stream, ")");
544
545 return p;
546}
547
548/* Returns number of bytes "eaten" by the operand, or
549 return -1 if an invalid operand was found, or -2 if
550 an opcode tabe error was found.
551 ADDR is the pc for this arg to be relative to. */
552
553static int
554print_insn_arg (const char *d,
555 unsigned char *buffer,
556 unsigned char *p0,
557 bfd_vma addr,
558 disassemble_info *info)
559{
560 int val = 0;
561 int place = d[1];
562 unsigned char *p = p0;
563 int regno;
be8c092b
NC
564 const char *regname;
565 unsigned char *p1;
252b5132
RH
566 double flval;
567 int flt_p;
568 bfd_signed_vma disp;
569 unsigned int uval;
570
571 switch (*d)
572 {
be8c092b 573 case 'c': /* Cache identifier. */
252b5132
RH
574 {
575 static char *const cacheFieldName[] = { "nc", "dc", "ic", "bc" };
576 val = fetch_arg (buffer, place, 2, info);
577 (*info->fprintf_func) (info->stream, cacheFieldName[val]);
578 break;
579 }
580
be8c092b 581 case 'a': /* Address register indirect only. Cf. case '+'. */
252b5132
RH
582 {
583 (*info->fprintf_func)
584 (info->stream,
585 "%s@",
ec22bdda 586 reg_names[fetch_arg (buffer, place, 3, info) + 8]);
252b5132
RH
587 break;
588 }
589
be8c092b 590 case '_': /* 32-bit absolute address for move16. */
252b5132
RH
591 {
592 uval = NEXTULONG (p);
593 (*info->print_address_func) (uval, info);
594 break;
595 }
596
597 case 'C':
598 (*info->fprintf_func) (info->stream, "%%ccr");
599 break;
600
601 case 'S':
602 (*info->fprintf_func) (info->stream, "%%sr");
603 break;
604
605 case 'U':
606 (*info->fprintf_func) (info->stream, "%%usp");
607 break;
608
461d5ddd
ILT
609 case 'E':
610 (*info->fprintf_func) (info->stream, "%%acc");
611 break;
612
613 case 'G':
614 (*info->fprintf_func) (info->stream, "%%macsr");
615 break;
616
617 case 'H':
618 (*info->fprintf_func) (info->stream, "%%mask");
619 break;
620
252b5132
RH
621 case 'J':
622 {
3e602632
NC
623 /* FIXME: There's a problem here, different m68k processors call the
624 same address different names. This table can't get it right
625 because it doesn't know which processor it's disassembling for. */
252b5132
RH
626 static const struct { char *name; int value; } names[]
627 = {{"%sfc", 0x000}, {"%dfc", 0x001}, {"%cacr", 0x002},
628 {"%tc", 0x003}, {"%itt0",0x004}, {"%itt1", 0x005},
629 {"%dtt0",0x006}, {"%dtt1",0x007}, {"%buscr",0x008},
630 {"%usp", 0x800}, {"%vbr", 0x801}, {"%caar", 0x802},
631 {"%msp", 0x803}, {"%isp", 0x804},
78336706
NS
632 /* reg c04 is sometimes called flashbar or rambar.
633 rec c05 is also sometimes called rambar. */
634 {"%rambar0", 0xc04}, {"%rambar1", 0xc05},
252b5132
RH
635
636 /* Should we be calling this psr like we do in case 'Y'? */
637 {"%mmusr",0x805},
638
f7ec513b
KH
639 {"%urp", 0x806}, {"%srp", 0x807}, {"%pcr", 0x808},
640
641 /* Fido added these. */
642 {"%cac", 0xffe}, {"%mbb", 0xfff}};
252b5132
RH
643
644 val = fetch_arg (buffer, place, 12, info);
645 for (regno = sizeof names / sizeof names[0] - 1; regno >= 0; regno--)
646 if (names[regno].value == val)
647 {
648 (*info->fprintf_func) (info->stream, "%s", names[regno].name);
649 break;
650 }
651 if (regno < 0)
652 (*info->fprintf_func) (info->stream, "%d", val);
653 }
654 break;
655
656 case 'Q':
657 val = fetch_arg (buffer, place, 3, info);
658 /* 0 means 8, except for the bkpt instruction... */
659 if (val == 0 && d[1] != 's')
660 val = 8;
661 (*info->fprintf_func) (info->stream, "#%d", val);
662 break;
663
3e602632
NC
664 case 'x':
665 val = fetch_arg (buffer, place, 3, info);
666 /* 0 means -1. */
667 if (val == 0)
668 val = -1;
669 (*info->fprintf_func) (info->stream, "#%d", val);
670 break;
671
afa2158f
NS
672 case 'j':
673 val = fetch_arg (buffer, place, 3, info);
674 (*info->fprintf_func) (info->stream, "#%d", val+1);
675 break;
676
677 case 'K':
678 val = fetch_arg (buffer, place, 9, info);
679 (*info->fprintf_func) (info->stream, "#%d", val);
680 break;
681
252b5132 682 case 'M':
461d5ddd
ILT
683 if (place == 'h')
684 {
685 static char *const scalefactor_name[] = { "<<", ">>" };
686 val = fetch_arg (buffer, place, 1, info);
687 (*info->fprintf_func) (info->stream, scalefactor_name[val]);
688 }
689 else
690 {
691 val = fetch_arg (buffer, place, 8, info);
692 if (val & 0x80)
693 val = val - 0x100;
694 (*info->fprintf_func) (info->stream, "#%d", val);
695 }
252b5132
RH
696 break;
697
698 case 'T':
699 val = fetch_arg (buffer, place, 4, info);
700 (*info->fprintf_func) (info->stream, "#%d", val);
701 break;
702
703 case 'D':
704 (*info->fprintf_func) (info->stream, "%s",
705 reg_names[fetch_arg (buffer, place, 3, info)]);
706 break;
707
708 case 'A':
709 (*info->fprintf_func)
710 (info->stream, "%s",
711 reg_names[fetch_arg (buffer, place, 3, info) + 010]);
712 break;
713
714 case 'R':
715 (*info->fprintf_func)
716 (info->stream, "%s",
717 reg_names[fetch_arg (buffer, place, 4, info)]);
718 break;
719
720 case 'r':
721 regno = fetch_arg (buffer, place, 4, info);
722 if (regno > 7)
723 (*info->fprintf_func) (info->stream, "%s@", reg_names[regno]);
724 else
725 (*info->fprintf_func) (info->stream, "@(%s)", reg_names[regno]);
726 break;
727
728 case 'F':
729 (*info->fprintf_func)
730 (info->stream, "%%fp%d",
731 fetch_arg (buffer, place, 3, info));
732 break;
733
734 case 'O':
735 val = fetch_arg (buffer, place, 6, info);
736 if (val & 0x20)
ec22bdda 737 (*info->fprintf_func) (info->stream, "%s", reg_names[val & 7]);
252b5132
RH
738 else
739 (*info->fprintf_func) (info->stream, "%d", val);
740 break;
741
742 case '+':
743 (*info->fprintf_func)
744 (info->stream, "%s@+",
745 reg_names[fetch_arg (buffer, place, 3, info) + 8]);
746 break;
747
748 case '-':
749 (*info->fprintf_func)
750 (info->stream, "%s@-",
751 reg_names[fetch_arg (buffer, place, 3, info) + 8]);
752 break;
753
754 case 'k':
755 if (place == 'k')
756 (*info->fprintf_func)
757 (info->stream, "{%s}",
758 reg_names[fetch_arg (buffer, place, 3, info)]);
759 else if (place == 'C')
760 {
761 val = fetch_arg (buffer, place, 7, info);
47b0e7ad 762 if (val > 63) /* This is a signed constant. */
252b5132
RH
763 val -= 128;
764 (*info->fprintf_func) (info->stream, "{#%d}", val);
765 }
766 else
767 return -2;
768 break;
769
770 case '#':
771 case '^':
772 p1 = buffer + (*d == '#' ? 2 : 4);
773 if (place == 's')
774 val = fetch_arg (buffer, place, 4, info);
775 else if (place == 'C')
776 val = fetch_arg (buffer, place, 7, info);
777 else if (place == '8')
778 val = fetch_arg (buffer, place, 3, info);
779 else if (place == '3')
780 val = fetch_arg (buffer, place, 8, info);
781 else if (place == 'b')
782 val = NEXTBYTE (p1);
783 else if (place == 'w' || place == 'W')
784 val = NEXTWORD (p1);
785 else if (place == 'l')
786 val = NEXTLONG (p1);
787 else
788 return -2;
789 (*info->fprintf_func) (info->stream, "#%d", val);
790 break;
791
792 case 'B':
793 if (place == 'b')
794 disp = NEXTBYTE (p);
795 else if (place == 'B')
ec22bdda 796 disp = COERCE_SIGNED_CHAR (buffer[1]);
252b5132
RH
797 else if (place == 'w' || place == 'W')
798 disp = NEXTWORD (p);
799 else if (place == 'l' || place == 'L' || place == 'C')
800 disp = NEXTLONG (p);
801 else if (place == 'g')
802 {
803 disp = NEXTBYTE (buffer);
804 if (disp == 0)
805 disp = NEXTWORD (p);
806 else if (disp == -1)
807 disp = NEXTLONG (p);
808 }
809 else if (place == 'c')
810 {
47b0e7ad 811 if (buffer[1] & 0x40) /* If bit six is one, long offset. */
252b5132
RH
812 disp = NEXTLONG (p);
813 else
814 disp = NEXTWORD (p);
815 }
816 else
817 return -2;
818
819 (*info->print_address_func) (addr + disp, info);
820 break;
821
822 case 'd':
823 val = NEXTWORD (p);
824 (*info->fprintf_func)
825 (info->stream, "%s@(%d)",
826 reg_names[fetch_arg (buffer, place, 3, info) + 8], val);
827 break;
828
829 case 's':
830 (*info->fprintf_func) (info->stream, "%s",
831 fpcr_names[fetch_arg (buffer, place, 3, info)]);
832 break;
833
fd99574b
NC
834 case 'e':
835 val = fetch_arg(buffer, place, 2, info);
836 (*info->fprintf_func) (info->stream, "%%acc%d", val);
837 break;
838
839 case 'g':
be8c092b 840 val = fetch_arg(buffer, place, 1, info);
fd99574b
NC
841 (*info->fprintf_func) (info->stream, "%%accext%s", val==0 ? "01" : "23");
842 break;
47b0e7ad 843
fd99574b
NC
844 case 'i':
845 val = fetch_arg(buffer, place, 2, info);
846 if (val == 1)
847 (*info->fprintf_func) (info->stream, "<<");
848 else if (val == 3)
849 (*info->fprintf_func) (info->stream, ">>");
be8c092b
NC
850 else
851 return -1;
fd99574b
NC
852 break;
853
252b5132
RH
854 case 'I':
855 /* Get coprocessor ID... */
856 val = fetch_arg (buffer, 'd', 3, info);
3e602632 857
47b0e7ad 858 if (val != 1) /* Unusual coprocessor ID? */
252b5132
RH
859 (*info->fprintf_func) (info->stream, "(cpid=%d) ", val);
860 break;
861
fd99574b 862 case '4':
252b5132
RH
863 case '*':
864 case '~':
865 case '%':
866 case ';':
867 case '@':
868 case '!':
869 case '$':
870 case '?':
871 case '/':
872 case '&':
873 case '|':
874 case '<':
875 case '>':
876 case 'm':
877 case 'n':
878 case 'o':
879 case 'p':
880 case 'q':
881 case 'v':
3e602632
NC
882 case 'b':
883 case 'w':
884 case 'y':
885 case 'z':
252b5132
RH
886 if (place == 'd')
887 {
888 val = fetch_arg (buffer, 'x', 6, info);
889 val = ((val & 7) << 3) + ((val >> 3) & 7);
890 }
891 else
892 val = fetch_arg (buffer, 's', 6, info);
893
be8c092b 894 /* If the <ea> is invalid for *d, then reject this match. */
8577e690 895 if (!m68k_valid_ea (*d, val))
be8c092b
NC
896 return -1;
897
252b5132
RH
898 /* Get register number assuming address register. */
899 regno = (val & 7) + 8;
900 regname = reg_names[regno];
901 switch (val >> 3)
902 {
903 case 0:
904 (*info->fprintf_func) (info->stream, "%s", reg_names[val]);
905 break;
906
907 case 1:
908 (*info->fprintf_func) (info->stream, "%s", regname);
909 break;
910
911 case 2:
912 (*info->fprintf_func) (info->stream, "%s@", regname);
913 break;
914
915 case 3:
916 (*info->fprintf_func) (info->stream, "%s@+", regname);
917 break;
918
919 case 4:
920 (*info->fprintf_func) (info->stream, "%s@-", regname);
921 break;
922
923 case 5:
924 val = NEXTWORD (p);
925 (*info->fprintf_func) (info->stream, "%s@(%d)", regname, val);
926 break;
927
928 case 6:
929 p = print_indexed (regno, p, addr, info);
930 break;
931
932 case 7:
933 switch (val & 7)
934 {
935 case 0:
936 val = NEXTWORD (p);
937 (*info->print_address_func) (val, info);
938 break;
939
940 case 1:
941 uval = NEXTULONG (p);
942 (*info->print_address_func) (uval, info);
943 break;
944
945 case 2:
946 val = NEXTWORD (p);
947 (*info->fprintf_func) (info->stream, "%%pc@(");
948 (*info->print_address_func) (addr + val, info);
949 (*info->fprintf_func) (info->stream, ")");
950 break;
951
952 case 3:
953 p = print_indexed (-1, p, addr, info);
954 break;
955
956 case 4:
957 flt_p = 1; /* Assume it's a float... */
ec22bdda 958 switch (place)
252b5132
RH
959 {
960 case 'b':
961 val = NEXTBYTE (p);
962 flt_p = 0;
963 break;
964
965 case 'w':
966 val = NEXTWORD (p);
967 flt_p = 0;
968 break;
969
970 case 'l':
971 val = NEXTLONG (p);
972 flt_p = 0;
973 break;
974
975 case 'f':
ec22bdda 976 NEXTSINGLE (flval, p);
252b5132
RH
977 break;
978
979 case 'F':
ec22bdda 980 NEXTDOUBLE (flval, p);
252b5132
RH
981 break;
982
983 case 'x':
ec22bdda 984 NEXTEXTEND (flval, p);
252b5132
RH
985 break;
986
987 case 'p':
ec22bdda 988 flval = NEXTPACKED (p);
252b5132
RH
989 break;
990
991 default:
992 return -1;
993 }
ec22bdda 994 if (flt_p) /* Print a float? */
252b5132
RH
995 (*info->fprintf_func) (info->stream, "#%g", flval);
996 else
997 (*info->fprintf_func) (info->stream, "#%d", val);
998 break;
999
1000 default:
1001 return -1;
1002 }
1003 }
fd99574b
NC
1004
1005 /* If place is '/', then this is the case of the mask bit for
1006 mac/emac loads. Now that the arg has been printed, grab the
1007 mask bit and if set, add a '&' to the arg. */
1008 if (place == '/')
1009 {
1010 val = fetch_arg (buffer, place, 1, info);
1011 if (val)
be8c092b 1012 info->fprintf_func (info->stream, "&");
fd99574b 1013 }
252b5132
RH
1014 break;
1015
1016 case 'L':
1017 case 'l':
1018 if (place == 'w')
1019 {
1020 char doneany;
1021 p1 = buffer + 2;
1022 val = NEXTWORD (p1);
1023 /* Move the pointer ahead if this point is farther ahead
1024 than the last. */
1025 p = p1 > p ? p1 : p;
1026 if (val == 0)
1027 {
1028 (*info->fprintf_func) (info->stream, "#0");
1029 break;
1030 }
1031 if (*d == 'l')
1032 {
47b0e7ad
NC
1033 int newval = 0;
1034
252b5132
RH
1035 for (regno = 0; regno < 16; ++regno)
1036 if (val & (0x8000 >> regno))
1037 newval |= 1 << regno;
1038 val = newval;
1039 }
1040 val &= 0xffff;
1041 doneany = 0;
1042 for (regno = 0; regno < 16; ++regno)
1043 if (val & (1 << regno))
1044 {
1045 int first_regno;
47b0e7ad 1046
252b5132
RH
1047 if (doneany)
1048 (*info->fprintf_func) (info->stream, "/");
1049 doneany = 1;
1050 (*info->fprintf_func) (info->stream, "%s", reg_names[regno]);
1051 first_regno = regno;
1052 while (val & (1 << (regno + 1)))
1053 ++regno;
1054 if (regno > first_regno)
1055 (*info->fprintf_func) (info->stream, "-%s",
1056 reg_names[regno]);
1057 }
1058 }
1059 else if (place == '3')
1060 {
1061 /* `fmovem' insn. */
1062 char doneany;
1063 val = fetch_arg (buffer, place, 8, info);
1064 if (val == 0)
1065 {
1066 (*info->fprintf_func) (info->stream, "#0");
1067 break;
1068 }
1069 if (*d == 'l')
1070 {
47b0e7ad
NC
1071 int newval = 0;
1072
252b5132
RH
1073 for (regno = 0; regno < 8; ++regno)
1074 if (val & (0x80 >> regno))
1075 newval |= 1 << regno;
1076 val = newval;
1077 }
1078 val &= 0xff;
1079 doneany = 0;
1080 for (regno = 0; regno < 8; ++regno)
1081 if (val & (1 << regno))
1082 {
1083 int first_regno;
1084 if (doneany)
1085 (*info->fprintf_func) (info->stream, "/");
1086 doneany = 1;
1087 (*info->fprintf_func) (info->stream, "%%fp%d", regno);
1088 first_regno = regno;
1089 while (val & (1 << (regno + 1)))
1090 ++regno;
1091 if (regno > first_regno)
1092 (*info->fprintf_func) (info->stream, "-%%fp%d", regno);
1093 }
1094 }
1095 else if (place == '8')
1096 {
47b0e7ad 1097 /* fmoveml for FP status registers. */
252b5132
RH
1098 (*info->fprintf_func) (info->stream, "%s",
1099 fpcr_names[fetch_arg (buffer, place, 3,
1100 info)]);
1101 }
1102 else
1103 return -2;
1104 break;
1105
1106 case 'X':
1107 place = '8';
1108 case 'Y':
1109 case 'Z':
1110 case 'W':
1111 case '0':
1112 case '1':
1113 case '2':
1114 case '3':
1115 {
1116 int val = fetch_arg (buffer, place, 5, info);
1117 char *name = 0;
47b0e7ad 1118
252b5132
RH
1119 switch (val)
1120 {
1121 case 2: name = "%tt0"; break;
1122 case 3: name = "%tt1"; break;
1123 case 0x10: name = "%tc"; break;
1124 case 0x11: name = "%drp"; break;
1125 case 0x12: name = "%srp"; break;
1126 case 0x13: name = "%crp"; break;
1127 case 0x14: name = "%cal"; break;
1128 case 0x15: name = "%val"; break;
1129 case 0x16: name = "%scc"; break;
1130 case 0x17: name = "%ac"; break;
1131 case 0x18: name = "%psr"; break;
1132 case 0x19: name = "%pcsr"; break;
1133 case 0x1c:
1134 case 0x1d:
1135 {
1136 int break_reg = ((buffer[3] >> 2) & 7);
47b0e7ad 1137
252b5132
RH
1138 (*info->fprintf_func)
1139 (info->stream, val == 0x1c ? "%%bad%d" : "%%bac%d",
1140 break_reg);
1141 }
1142 break;
1143 default:
1144 (*info->fprintf_func) (info->stream, "<mmu register %d>", val);
1145 }
1146 if (name)
1147 (*info->fprintf_func) (info->stream, "%s", name);
1148 }
1149 break;
1150
1151 case 'f':
1152 {
1153 int fc = fetch_arg (buffer, place, 5, info);
47b0e7ad 1154
252b5132
RH
1155 if (fc == 1)
1156 (*info->fprintf_func) (info->stream, "%%dfc");
1157 else if (fc == 0)
1158 (*info->fprintf_func) (info->stream, "%%sfc");
1159 else
1160 /* xgettext:c-format */
1161 (*info->fprintf_func) (info->stream, _("<function code %d>"), fc);
1162 }
1163 break;
1164
1165 case 'V':
1166 (*info->fprintf_func) (info->stream, "%%val");
1167 break;
1168
1169 case 't':
1170 {
1171 int level = fetch_arg (buffer, place, 3, info);
47b0e7ad 1172
252b5132
RH
1173 (*info->fprintf_func) (info->stream, "%d", level);
1174 }
1175 break;
1176
461d5ddd
ILT
1177 case 'u':
1178 {
1179 short is_upper = 0;
1180 int reg = fetch_arg (buffer, place, 5, info);
3e602632 1181
461d5ddd
ILT
1182 if (reg & 0x10)
1183 {
1184 is_upper = 1;
1185 reg &= 0xf;
1186 }
1187 (*info->fprintf_func) (info->stream, "%s%s",
be8c092b 1188 reg_half_names[reg],
461d5ddd
ILT
1189 is_upper ? "u" : "l");
1190 }
1191 break;
3e602632 1192
252b5132
RH
1193 default:
1194 return -2;
1195 }
1196
1197 return p - p0;
1198}
1199
47b0e7ad
NC
1200/* Try to match the current instruction to best and if so, return the
1201 number of bytes consumed from the instruction stream, else zero. */
252b5132
RH
1202
1203static int
47b0e7ad
NC
1204match_insn_m68k (bfd_vma memaddr,
1205 disassemble_info * info,
a596001e 1206 const struct m68k_opcode * best)
252b5132 1207{
47b0e7ad
NC
1208 unsigned char *save_p;
1209 unsigned char *p;
1210 const char *d;
afa2158f 1211 const char *args = best->args;
be8c092b 1212
a596001e 1213 struct private *priv = (struct private *) info->private_data;
47b0e7ad
NC
1214 bfd_byte *buffer = priv->the_buffer;
1215 fprintf_ftype save_printer = info->fprintf_func;
1216 void (* save_print_address) (bfd_vma, struct disassemble_info *)
1217 = info->print_address_func;
fd99574b 1218
afa2158f
NS
1219 if (*args == '.')
1220 args++;
1221
47b0e7ad
NC
1222 /* Point at first word of argument data,
1223 and at descriptor for first argument. */
1224 p = buffer + 2;
fd99574b 1225
47b0e7ad
NC
1226 /* Figure out how long the fixed-size portion of the instruction is.
1227 The only place this is stored in the opcode table is
1228 in the arguments--look for arguments which specify fields in the 2nd
1229 or 3rd words of the instruction. */
afa2158f 1230 for (d = args; *d; d += 2)
47b0e7ad
NC
1231 {
1232 /* I don't think it is necessary to be checking d[0] here;
1233 I suspect all this could be moved to the case statement below. */
1234 if (d[0] == '#')
1235 {
1236 if (d[1] == 'l' && p - buffer < 6)
1237 p = buffer + 6;
1238 else if (p - buffer < 4 && d[1] != 'C' && d[1] != '8')
1239 p = buffer + 4;
1240 }
fd99574b 1241
47b0e7ad
NC
1242 if ((d[0] == 'L' || d[0] == 'l') && d[1] == 'w' && p - buffer < 4)
1243 p = buffer + 4;
fd99574b 1244
47b0e7ad
NC
1245 switch (d[1])
1246 {
1247 case '1':
1248 case '2':
1249 case '3':
1250 case '7':
1251 case '8':
1252 case '9':
1253 case 'i':
1254 if (p - buffer < 4)
1255 p = buffer + 4;
1256 break;
1257 case '4':
1258 case '5':
1259 case '6':
1260 if (p - buffer < 6)
1261 p = buffer + 6;
1262 break;
1263 default:
1264 break;
1265 }
1266 }
252b5132 1267
47b0e7ad
NC
1268 /* pflusha is an exceptions. It takes no arguments but is two words
1269 long. Recognize it by looking at the lower 16 bits of the mask. */
1270 if (p - buffer < 4 && (best->match & 0xFFFF) != 0)
1271 p = buffer + 4;
252b5132 1272
47b0e7ad
NC
1273 /* lpstop is another exception. It takes a one word argument but is
1274 three words long. */
1275 if (p - buffer < 6
1276 && (best->match & 0xffff) == 0xffff
afa2158f
NS
1277 && args[0] == '#'
1278 && args[1] == 'w')
47b0e7ad
NC
1279 {
1280 /* Copy the one word argument into the usual location for a one
1281 word argument, to simplify printing it. We can get away with
1282 this because we know exactly what the second word is, and we
1283 aren't going to print anything based on it. */
1284 p = buffer + 6;
1285 FETCH_DATA (info, p);
1286 buffer[2] = buffer[4];
1287 buffer[3] = buffer[5];
1288 }
252b5132 1289
47b0e7ad 1290 FETCH_DATA (info, p);
3e602632 1291
47b0e7ad
NC
1292 save_p = p;
1293 info->print_address_func = dummy_print_address;
1294 info->fprintf_func = (fprintf_ftype) dummy_printer;
252b5132 1295
47b0e7ad
NC
1296 /* We scan the operands twice. The first time we don't print anything,
1297 but look for errors. */
afa2158f 1298 for (d = args; *d; d += 2)
47b0e7ad
NC
1299 {
1300 int eaten = print_insn_arg (d, buffer, p, memaddr + (p - buffer), info);
252b5132 1301
47b0e7ad
NC
1302 if (eaten >= 0)
1303 p += eaten;
1304 else if (eaten == -1)
1305 {
1306 info->fprintf_func = save_printer;
1307 info->print_address_func = save_print_address;
1308 return 0;
1309 }
1310 else
1311 {
f095b97b
JW
1312 /* We must restore the print functions before trying to print the
1313 error message. */
1314 info->fprintf_func = save_printer;
1315 info->print_address_func = save_print_address;
47b0e7ad
NC
1316 info->fprintf_func (info->stream,
1317 /* xgettext:c-format */
1318 _("<internal error in opcode table: %s %s>\n"),
1319 best->name, best->args);
47b0e7ad
NC
1320 return 2;
1321 }
1322 }
461d5ddd 1323
47b0e7ad
NC
1324 p = save_p;
1325 info->fprintf_func = save_printer;
1326 info->print_address_func = save_print_address;
461d5ddd 1327
afa2158f 1328 d = args;
461d5ddd 1329
47b0e7ad 1330 info->fprintf_func (info->stream, "%s", best->name);
461d5ddd 1331
47b0e7ad
NC
1332 if (*d)
1333 info->fprintf_func (info->stream, " ");
461d5ddd 1334
47b0e7ad
NC
1335 while (*d)
1336 {
1337 p += print_insn_arg (d, buffer, p, memaddr + (p - buffer), info);
1338 d += 2;
461d5ddd 1339
47b0e7ad
NC
1340 if (*d && *(d - 2) != 'I' && *d != 'k')
1341 info->fprintf_func (info->stream, ",");
252b5132
RH
1342 }
1343
47b0e7ad 1344 return p - buffer;
252b5132
RH
1345}
1346
a596001e
RS
1347/* Try to interpret the instruction at address MEMADDR as one that
1348 can execute on a processor with the features given by ARCH_MASK.
1349 If successful, print the instruction to INFO->STREAM and return
1350 its length in bytes. Return 0 otherwise. */
252b5132 1351
a596001e
RS
1352static int
1353m68k_scan_mask (bfd_vma memaddr, disassemble_info *info,
1354 unsigned int arch_mask)
252b5132 1355{
47b0e7ad
NC
1356 int i;
1357 const char *d;
47b0e7ad 1358 static const struct m68k_opcode **opcodes[16];
a596001e 1359 static int numopcodes[16];
47b0e7ad 1360 int val;
a596001e
RS
1361 int major_opcode;
1362
1363 struct private *priv = (struct private *) info->private_data;
1364 bfd_byte *buffer = priv->the_buffer;
252b5132 1365
47b0e7ad 1366 if (!opcodes[0])
252b5132 1367 {
47b0e7ad
NC
1368 /* Speed up the matching by sorting the opcode
1369 table on the upper four bits of the opcode. */
1370 const struct m68k_opcode **opc_pointer[16];
252b5132 1371
47b0e7ad
NC
1372 /* First count how many opcodes are in each of the sixteen buckets. */
1373 for (i = 0; i < m68k_numopcodes; i++)
1374 numopcodes[(m68k_opcodes[i].opcode >> 28) & 15]++;
252b5132 1375
47b0e7ad
NC
1376 /* Then create a sorted table of pointers
1377 that point into the unsorted table. */
1378 opc_pointer[0] = xmalloc (sizeof (struct m68k_opcode *)
1379 * m68k_numopcodes);
1380 opcodes[0] = opc_pointer[0];
252b5132 1381
47b0e7ad
NC
1382 for (i = 1; i < 16; i++)
1383 {
1384 opc_pointer[i] = opc_pointer[i - 1] + numopcodes[i - 1];
1385 opcodes[i] = opc_pointer[i];
1386 }
252b5132 1387
47b0e7ad
NC
1388 for (i = 0; i < m68k_numopcodes; i++)
1389 *opc_pointer[(m68k_opcodes[i].opcode >> 28) & 15]++ = &m68k_opcodes[i];
252b5132
RH
1390 }
1391
47b0e7ad
NC
1392 FETCH_DATA (info, buffer + 2);
1393 major_opcode = (buffer[0] >> 4) & 15;
252b5132 1394
47b0e7ad
NC
1395 for (i = 0; i < numopcodes[major_opcode]; i++)
1396 {
1397 const struct m68k_opcode *opc = opcodes[major_opcode][i];
1398 unsigned long opcode = opc->opcode;
1399 unsigned long match = opc->match;
afa2158f
NS
1400 const char *args = opc->args;
1401
1402 if (*args == '.')
1403 args++;
252b5132 1404
47b0e7ad
NC
1405 if (((0xff & buffer[0] & (match >> 24)) == (0xff & (opcode >> 24)))
1406 && ((0xff & buffer[1] & (match >> 16)) == (0xff & (opcode >> 16)))
1407 /* Only fetch the next two bytes if we need to. */
1408 && (((0xffff & match) == 0)
1409 ||
1410 (FETCH_DATA (info, buffer + 4)
1411 && ((0xff & buffer[2] & (match >> 8)) == (0xff & (opcode >> 8)))
1412 && ((0xff & buffer[3] & match) == (0xff & opcode)))
1413 )
1414 && (opc->arch & arch_mask) != 0)
1415 {
1416 /* Don't use for printout the variants of divul and divsl
1417 that have the same register number in two places.
1418 The more general variants will match instead. */
afa2158f 1419 for (d = args; *d; d += 2)
47b0e7ad
NC
1420 if (d[1] == 'D')
1421 break;
252b5132 1422
47b0e7ad
NC
1423 /* Don't use for printout the variants of most floating
1424 point coprocessor instructions which use the same
1425 register number in two places, as above. */
1426 if (*d == '\0')
afa2158f 1427 for (d = args; *d; d += 2)
47b0e7ad
NC
1428 if (d[1] == 't')
1429 break;
252b5132 1430
47b0e7ad
NC
1431 /* Don't match fmovel with more than one register;
1432 wait for fmoveml. */
1433 if (*d == '\0')
1434 {
afa2158f 1435 for (d = args; *d; d += 2)
47b0e7ad
NC
1436 {
1437 if (d[0] == 's' && d[1] == '8')
1438 {
1439 val = fetch_arg (buffer, d[1], 3, info);
1440 if ((val & (val - 1)) != 0)
1441 break;
1442 }
1443 }
1444 }
252b5132 1445
dc82c973
AS
1446 /* Don't match FPU insns with non-default coprocessor ID. */
1447 if (*d == '\0')
1448 {
afa2158f 1449 for (d = args; *d; d += 2)
dc82c973
AS
1450 {
1451 if (d[0] == 'I')
1452 {
1453 val = fetch_arg (buffer, 'd', 3, info);
1454 if (val != 1)
1455 break;
1456 }
1457 }
1458 }
1459
47b0e7ad 1460 if (*d == '\0')
a596001e 1461 if ((val = match_insn_m68k (memaddr, info, opc)))
47b0e7ad
NC
1462 return val;
1463 }
252b5132 1464 }
a596001e
RS
1465 return 0;
1466}
1467
1468/* Print the m68k instruction at address MEMADDR in debugged memory,
1469 on INFO->STREAM. Returns length of the instruction, in bytes. */
1470
1471int
1472print_insn_m68k (bfd_vma memaddr, disassemble_info *info)
1473{
1474 unsigned int arch_mask;
1475 struct private priv;
1476 int val;
1477
1478 bfd_byte *buffer = priv.the_buffer;
1479
56dc1f8a
KH
1480 /* Save these printing functions in case we need to restore them
1481 later. */
1482 fprintf_ftype save_printer = info->fprintf_func;
1483 void (* save_print_address) (bfd_vma, struct disassemble_info *)
1484 = info->print_address_func;
1485
a596001e
RS
1486 info->private_data = (PTR) &priv;
1487 /* Tell objdump to use two bytes per chunk
1488 and six bytes per line for displaying raw data. */
1489 info->bytes_per_chunk = 2;
1490 info->bytes_per_line = 6;
1491 info->display_endian = BFD_ENDIAN_BIG;
1492 priv.max_fetched = priv.the_buffer;
1493 priv.insn_start = memaddr;
1494
1495 if (setjmp (priv.bailout) != 0)
56dc1f8a
KH
1496 {
1497 /* longjmp may be called while these printing functions are
1498 temporarily replaced with dummy functions. Restore them
1499 before we leave.
1500
1501 Admittedly, this save-and-restore operation is somewhat ugly
1502 in that we are exposing the fact that match_insn_m68k
1503 temporarily replaces insn->fprintf_func and
1504 insn->print_address_func. Perhaps, a real fix is to report a
1505 FETCH_DATA failure with a return value of some sort, without
1506 using setjmp/longjmp. A better fix may be to teach the m68k
1507 disassembler do its job without temporarily replacing
1508 insn->fprintf_func and insn->print_address_func, but that's a
1509 task for another day. */
1510 info->fprintf_func = save_printer;
1511 info->print_address_func = save_print_address;
1512
1513 /* Error return. */
1514 return -1;
1515 }
a596001e
RS
1516
1517 arch_mask = bfd_m68k_mach_to_features (info->mach);
1518 if (!arch_mask)
1519 {
1520 /* First try printing an m680x0 instruction. Try printing a Coldfire
1521 one if that fails. */
1522 val = m68k_scan_mask (memaddr, info, m68k_mask);
1523 if (val)
1524 return val;
1525
1526 val = m68k_scan_mask (memaddr, info, mcf_mask);
1527 if (val)
1528 return val;
1529 }
1530 else
1531 {
1532 val = m68k_scan_mask (memaddr, info, arch_mask);
1533 if (val)
1534 return val;
1535 }
47b0e7ad
NC
1536
1537 /* Handle undefined instructions. */
1538 info->fprintf_func (info->stream, "0%o", (buffer[0] << 8) + buffer[1]);
1539 return 2;
252b5132 1540}
This page took 0.727763 seconds and 4 git commands to generate.