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