gdb: add target_ops::supports_displaced_step
[deliverable/binutils-gdb.git] / opcodes / m68k-dis.c
CommitLineData
252b5132 1/* Print Motorola 68k instructions.
b3adc24a 2 Copyright (C) 1986-2020 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
412static bfd_boolean
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 {
518 char buf[50];
252b5132 519
47b0e7ad
NC
520 if (regno == -2)
521 (*info->fprintf_func) (info->stream, "@(");
522 else if (regno == -3)
523 (*info->fprintf_func) (info->stream, "%%zpc@(");
524 else
525 (*info->fprintf_func) (info->stream, "%s@(", reg_names[regno]);
252b5132 526
47b0e7ad
NC
527 sprintf_vma (buf, disp);
528 (*info->fprintf_func) (info->stream, "%s", buf);
252b5132 529 }
252b5132
RH
530}
531
47b0e7ad
NC
532/* Print an indexed argument. The base register is BASEREG (-1 for pc).
533 P points to extension word, in buffer.
62443ade
NC
534 ADDR is the nominal core address of that extension word.
535 Returns NULL upon error. */
cc16ba8c 536
47b0e7ad
NC
537static unsigned char *
538print_indexed (int basereg,
539 unsigned char *p,
540 bfd_vma addr,
541 disassemble_info *info)
252b5132 542{
47b0e7ad
NC
543 int word;
544 static char *const scales[] = { "", ":2", ":4", ":8" };
545 bfd_vma base_disp;
546 bfd_vma outer_disp;
547 char buf[40];
548 char vmabuf[50];
549
62443ade 550 NEXTWORD (p, word, NULL);
47b0e7ad
NC
551
552 /* Generate the text for the index register.
553 Where this will be output is not yet determined. */
554 sprintf (buf, "%s:%c%s",
555 reg_names[(word >> 12) & 0xf],
556 (word & 0x800) ? 'l' : 'w',
557 scales[(word >> 9) & 3]);
558
559 /* Handle the 68000 style of indexing. */
560
561 if ((word & 0x100) == 0)
562 {
563 base_disp = word & 0xff;
564 if ((base_disp & 0x80) != 0)
565 base_disp -= 0x100;
566 if (basereg == -1)
567 base_disp += addr;
568 print_base (basereg, base_disp, info);
569 (*info->fprintf_func) (info->stream, ",%s)", buf);
570 return p;
571 }
572
573 /* Handle the generalized kind. */
574 /* First, compute the displacement to add to the base register. */
575 if (word & 0200)
576 {
577 if (basereg == -1)
578 basereg = -3;
579 else
580 basereg = -2;
581 }
582 if (word & 0100)
583 buf[0] = '\0';
584 base_disp = 0;
585 switch ((word >> 4) & 3)
586 {
587 case 2:
62443ade 588 NEXTWORD (p, base_disp, NULL);
47b0e7ad
NC
589 break;
590 case 3:
62443ade 591 NEXTLONG (p, base_disp, NULL);
47b0e7ad
NC
592 }
593 if (basereg == -1)
594 base_disp += addr;
595
596 /* Handle single-level case (not indirect). */
597 if ((word & 7) == 0)
598 {
599 print_base (basereg, base_disp, info);
600 if (buf[0] != '\0')
601 (*info->fprintf_func) (info->stream, ",%s", buf);
602 (*info->fprintf_func) (info->stream, ")");
603 return p;
604 }
605
606 /* Two level. Compute displacement to add after indirection. */
607 outer_disp = 0;
608 switch (word & 3)
609 {
610 case 2:
62443ade 611 NEXTWORD (p, outer_disp, NULL);
47b0e7ad
NC
612 break;
613 case 3:
62443ade 614 NEXTLONG (p, outer_disp, NULL);
47b0e7ad
NC
615 }
616
617 print_base (basereg, base_disp, info);
618 if ((word & 4) == 0 && buf[0] != '\0')
619 {
620 (*info->fprintf_func) (info->stream, ",%s", buf);
621 buf[0] = '\0';
622 }
623 sprintf_vma (vmabuf, outer_disp);
624 (*info->fprintf_func) (info->stream, ")@(%s", vmabuf);
625 if (buf[0] != '\0')
626 (*info->fprintf_func) (info->stream, ",%s", buf);
627 (*info->fprintf_func) (info->stream, ")");
628
629 return p;
630}
631
62443ade
NC
632#define FETCH_ARG(size, val) \
633 do \
634 { \
635 val = fetch_arg (buffer, place, size, info); \
636 if (val < 0) \
f622ea96 637 return PRINT_INSN_ARG_MEMORY_ERROR; \
62443ade
NC
638 } \
639 while (0)
640
47b0e7ad 641/* Returns number of bytes "eaten" by the operand, or
f622ea96
YQ
642 return enum print_insn_arg_error. ADDR is the pc for this arg to be
643 relative to. */
47b0e7ad
NC
644
645static int
646print_insn_arg (const char *d,
647 unsigned char *buffer,
648 unsigned char *p0,
649 bfd_vma addr,
650 disassemble_info *info)
651{
652 int val = 0;
653 int place = d[1];
654 unsigned char *p = p0;
655 int regno;
be8c092b
NC
656 const char *regname;
657 unsigned char *p1;
252b5132
RH
658 double flval;
659 int flt_p;
660 bfd_signed_vma disp;
661 unsigned int uval;
662
663 switch (*d)
664 {
be8c092b 665 case 'c': /* Cache identifier. */
252b5132
RH
666 {
667 static char *const cacheFieldName[] = { "nc", "dc", "ic", "bc" };
62443ade 668 FETCH_ARG (2, val);
d908c8af 669 (*info->fprintf_func) (info->stream, "%s", cacheFieldName[val]);
252b5132
RH
670 break;
671 }
672
be8c092b 673 case 'a': /* Address register indirect only. Cf. case '+'. */
252b5132 674 {
62443ade
NC
675 FETCH_ARG (3, val);
676 (*info->fprintf_func) (info->stream, "%s@", reg_names[val + 8]);
252b5132
RH
677 break;
678 }
679
be8c092b 680 case '_': /* 32-bit absolute address for move16. */
252b5132 681 {
62443ade 682 NEXTULONG (p, uval);
252b5132
RH
683 (*info->print_address_func) (uval, info);
684 break;
685 }
686
687 case 'C':
688 (*info->fprintf_func) (info->stream, "%%ccr");
689 break;
690
691 case 'S':
692 (*info->fprintf_func) (info->stream, "%%sr");
693 break;
694
695 case 'U':
696 (*info->fprintf_func) (info->stream, "%%usp");
697 break;
698
461d5ddd
ILT
699 case 'E':
700 (*info->fprintf_func) (info->stream, "%%acc");
701 break;
702
703 case 'G':
704 (*info->fprintf_func) (info->stream, "%%macsr");
705 break;
706
707 case 'H':
708 (*info->fprintf_func) (info->stream, "%%mask");
709 break;
710
252b5132
RH
711 case 'J':
712 {
3e602632 713 /* FIXME: There's a problem here, different m68k processors call the
f7922329
NC
714 same address different names. The tables below try to get it right
715 using info->mach, but only for v4e. */
716 struct regname { char * name; int value; };
717 static const struct regname names[] =
718 {
719 {"%sfc", 0x000}, {"%dfc", 0x001}, {"%cacr", 0x002},
720 {"%tc", 0x003}, {"%itt0",0x004}, {"%itt1", 0x005},
721 {"%dtt0",0x006}, {"%dtt1",0x007}, {"%buscr",0x008},
0d999f33
MK
722 {"%rgpiobar", 0x009}, {"%acr4",0x00c},
723 {"%acr5",0x00d}, {"%acr6",0x00e}, {"%acr7", 0x00f},
f7922329
NC
724 {"%usp", 0x800}, {"%vbr", 0x801}, {"%caar", 0x802},
725 {"%msp", 0x803}, {"%isp", 0x804},
726 {"%pc", 0x80f},
727 /* Reg c04 is sometimes called flashbar or rambar.
0d999f33 728 Reg c05 is also sometimes called rambar. */
f7922329
NC
729 {"%rambar0", 0xc04}, {"%rambar1", 0xc05},
730
0d999f33
MK
731 /* reg c0e is sometimes called mbar2 or secmbar.
732 reg c0f is sometimes called mbar. */
733 {"%mbar0", 0xc0e}, {"%mbar1", 0xc0f},
f7922329
NC
734
735 /* Should we be calling this psr like we do in case 'Y'? */
736 {"%mmusr",0x805},
737
738 {"%urp", 0x806}, {"%srp", 0x807}, {"%pcr", 0x808},
739
740 /* Fido added these. */
741 {"%cac", 0xffe}, {"%mbo", 0xfff}
742 };
743 /* Alternate names for v4e (MCF5407/5445x/MCF547x/MCF548x), at least. */
744 static const struct regname names_v4e[] =
745 {
746 {"%asid",0x003}, {"%acr0",0x004}, {"%acr1",0x005},
747 {"%acr2",0x006}, {"%acr3",0x007}, {"%mmubar",0x008},
748 };
749 unsigned int arch_mask;
750
751 arch_mask = bfd_m68k_mach_to_features (info->mach);
62443ade 752 FETCH_ARG (12, val);
f7922329
NC
753 if (arch_mask & (mcfisa_b | mcfisa_c))
754 {
755 for (regno = ARRAY_SIZE (names_v4e); --regno >= 0;)
756 if (names_v4e[regno].value == val)
757 {
758 (*info->fprintf_func) (info->stream, "%s", names_v4e[regno].name);
759 break;
760 }
761 if (regno >= 0)
762 break;
763 }
764 for (regno = ARRAY_SIZE (names) - 1; regno >= 0; regno--)
252b5132
RH
765 if (names[regno].value == val)
766 {
767 (*info->fprintf_func) (info->stream, "%s", names[regno].name);
768 break;
769 }
770 if (regno < 0)
f7922329 771 (*info->fprintf_func) (info->stream, "0x%x", val);
252b5132
RH
772 }
773 break;
774
775 case 'Q':
62443ade 776 FETCH_ARG (3, val);
252b5132
RH
777 /* 0 means 8, except for the bkpt instruction... */
778 if (val == 0 && d[1] != 's')
779 val = 8;
780 (*info->fprintf_func) (info->stream, "#%d", val);
781 break;
782
3e602632 783 case 'x':
62443ade 784 FETCH_ARG (3, val);
3e602632
NC
785 /* 0 means -1. */
786 if (val == 0)
787 val = -1;
788 (*info->fprintf_func) (info->stream, "#%d", val);
789 break;
790
afa2158f 791 case 'j':
62443ade 792 FETCH_ARG (3, val);
afa2158f
NS
793 (*info->fprintf_func) (info->stream, "#%d", val+1);
794 break;
795
796 case 'K':
62443ade 797 FETCH_ARG (9, val);
afa2158f
NS
798 (*info->fprintf_func) (info->stream, "#%d", val);
799 break;
800
252b5132 801 case 'M':
461d5ddd
ILT
802 if (place == 'h')
803 {
804 static char *const scalefactor_name[] = { "<<", ">>" };
62443ade
NC
805
806 FETCH_ARG (1, val);
d908c8af 807 (*info->fprintf_func) (info->stream, "%s", scalefactor_name[val]);
461d5ddd
ILT
808 }
809 else
810 {
62443ade 811 FETCH_ARG (8, val);
461d5ddd
ILT
812 if (val & 0x80)
813 val = val - 0x100;
814 (*info->fprintf_func) (info->stream, "#%d", val);
815 }
252b5132
RH
816 break;
817
818 case 'T':
62443ade 819 FETCH_ARG (4, val);
252b5132
RH
820 (*info->fprintf_func) (info->stream, "#%d", val);
821 break;
822
823 case 'D':
62443ade
NC
824 FETCH_ARG (3, val);
825 (*info->fprintf_func) (info->stream, "%s", reg_names[val]);
252b5132
RH
826 break;
827
828 case 'A':
62443ade
NC
829 FETCH_ARG (3, val);
830 (*info->fprintf_func) (info->stream, "%s", reg_names[val + 010]);
252b5132
RH
831 break;
832
833 case 'R':
62443ade
NC
834 FETCH_ARG (4, val);
835 (*info->fprintf_func) (info->stream, "%s", reg_names[val]);
252b5132
RH
836 break;
837
838 case 'r':
62443ade 839 FETCH_ARG (4, regno);
252b5132
RH
840 if (regno > 7)
841 (*info->fprintf_func) (info->stream, "%s@", reg_names[regno]);
842 else
843 (*info->fprintf_func) (info->stream, "@(%s)", reg_names[regno]);
844 break;
845
846 case 'F':
62443ade
NC
847 FETCH_ARG (3, val);
848 (*info->fprintf_func) (info->stream, "%%fp%d", val);
252b5132
RH
849 break;
850
851 case 'O':
62443ade 852 FETCH_ARG (6, val);
252b5132 853 if (val & 0x20)
ec22bdda 854 (*info->fprintf_func) (info->stream, "%s", reg_names[val & 7]);
252b5132
RH
855 else
856 (*info->fprintf_func) (info->stream, "%d", val);
857 break;
858
859 case '+':
62443ade
NC
860 FETCH_ARG (3, val);
861 (*info->fprintf_func) (info->stream, "%s@+", reg_names[val + 8]);
252b5132
RH
862 break;
863
864 case '-':
62443ade
NC
865 FETCH_ARG (3, val);
866 (*info->fprintf_func) (info->stream, "%s@-", reg_names[val + 8]);
252b5132
RH
867 break;
868
869 case 'k':
870 if (place == 'k')
62443ade
NC
871 {
872 FETCH_ARG (3, val);
873 (*info->fprintf_func) (info->stream, "{%s}", reg_names[val]);
874 }
252b5132
RH
875 else if (place == 'C')
876 {
62443ade 877 FETCH_ARG (7, val);
47b0e7ad 878 if (val > 63) /* This is a signed constant. */
252b5132
RH
879 val -= 128;
880 (*info->fprintf_func) (info->stream, "{#%d}", val);
881 }
882 else
f622ea96 883 return PRINT_INSN_ARG_INVALID_OPERAND;
252b5132
RH
884 break;
885
886 case '#':
887 case '^':
888 p1 = buffer + (*d == '#' ? 2 : 4);
889 if (place == 's')
62443ade 890 FETCH_ARG (4, val);
252b5132 891 else if (place == 'C')
62443ade 892 FETCH_ARG (7, val);
252b5132 893 else if (place == '8')
62443ade 894 FETCH_ARG (3, val);
252b5132 895 else if (place == '3')
62443ade 896 FETCH_ARG (8, val);
252b5132 897 else if (place == 'b')
62443ade 898 NEXTBYTE (p1, val);
252b5132 899 else if (place == 'w' || place == 'W')
f622ea96 900 NEXTWORD (p1, val, PRINT_INSN_ARG_MEMORY_ERROR);
252b5132 901 else if (place == 'l')
f622ea96 902 NEXTLONG (p1, val, PRINT_INSN_ARG_MEMORY_ERROR);
252b5132 903 else
f622ea96 904 return PRINT_INSN_ARG_INVALID_OP_TABLE;
62443ade 905
252b5132
RH
906 (*info->fprintf_func) (info->stream, "#%d", val);
907 break;
908
909 case 'B':
910 if (place == 'b')
62443ade 911 NEXTBYTE (p, disp);
252b5132 912 else if (place == 'B')
ec22bdda 913 disp = COERCE_SIGNED_CHAR (buffer[1]);
252b5132 914 else if (place == 'w' || place == 'W')
f622ea96 915 NEXTWORD (p, disp, PRINT_INSN_ARG_MEMORY_ERROR);
252b5132 916 else if (place == 'l' || place == 'L' || place == 'C')
f622ea96 917 NEXTLONG (p, disp, PRINT_INSN_ARG_MEMORY_ERROR);
252b5132
RH
918 else if (place == 'g')
919 {
62443ade 920 NEXTBYTE (buffer, disp);
252b5132 921 if (disp == 0)
f622ea96 922 NEXTWORD (p, disp, PRINT_INSN_ARG_MEMORY_ERROR);
252b5132 923 else if (disp == -1)
f622ea96 924 NEXTLONG (p, disp, PRINT_INSN_ARG_MEMORY_ERROR);
252b5132
RH
925 }
926 else if (place == 'c')
927 {
47b0e7ad 928 if (buffer[1] & 0x40) /* If bit six is one, long offset. */
f622ea96 929 NEXTLONG (p, disp, PRINT_INSN_ARG_MEMORY_ERROR);
252b5132 930 else
f622ea96 931 NEXTWORD (p, disp, PRINT_INSN_ARG_MEMORY_ERROR);
252b5132
RH
932 }
933 else
f622ea96 934 return PRINT_INSN_ARG_INVALID_OP_TABLE;
252b5132
RH
935
936 (*info->print_address_func) (addr + disp, info);
937 break;
938
939 case 'd':
62443ade
NC
940 {
941 int val1;
942
f622ea96 943 NEXTWORD (p, val, PRINT_INSN_ARG_MEMORY_ERROR);
62443ade
NC
944 FETCH_ARG (3, val1);
945 (*info->fprintf_func) (info->stream, "%s@(%d)", reg_names[val1 + 8], val);
946 break;
947 }
252b5132
RH
948
949 case 's':
62443ade
NC
950 FETCH_ARG (3, val);
951 (*info->fprintf_func) (info->stream, "%s", fpcr_names[val]);
252b5132
RH
952 break;
953
fd99574b 954 case 'e':
62443ade 955 FETCH_ARG (2, val);
fd99574b
NC
956 (*info->fprintf_func) (info->stream, "%%acc%d", val);
957 break;
958
959 case 'g':
62443ade
NC
960 FETCH_ARG (1, val);
961 (*info->fprintf_func) (info->stream, "%%accext%s", val == 0 ? "01" : "23");
fd99574b 962 break;
47b0e7ad 963
fd99574b 964 case 'i':
62443ade 965 FETCH_ARG (2, val);
fd99574b
NC
966 if (val == 1)
967 (*info->fprintf_func) (info->stream, "<<");
968 else if (val == 3)
969 (*info->fprintf_func) (info->stream, ">>");
be8c092b 970 else
f622ea96 971 return PRINT_INSN_ARG_INVALID_OPERAND;
fd99574b
NC
972 break;
973
252b5132
RH
974 case 'I':
975 /* Get coprocessor ID... */
976 val = fetch_arg (buffer, 'd', 3, info);
62443ade 977 if (val < 0)
f622ea96 978 return PRINT_INSN_ARG_MEMORY_ERROR;
47b0e7ad 979 if (val != 1) /* Unusual coprocessor ID? */
252b5132
RH
980 (*info->fprintf_func) (info->stream, "(cpid=%d) ", val);
981 break;
982
fd99574b 983 case '4':
252b5132
RH
984 case '*':
985 case '~':
986 case '%':
987 case ';':
988 case '@':
989 case '!':
990 case '$':
991 case '?':
992 case '/':
993 case '&':
994 case '|':
995 case '<':
996 case '>':
997 case 'm':
998 case 'n':
999 case 'o':
1000 case 'p':
1001 case 'q':
1002 case 'v':
3e602632
NC
1003 case 'b':
1004 case 'w':
1005 case 'y':
1006 case 'z':
252b5132
RH
1007 if (place == 'd')
1008 {
1009 val = fetch_arg (buffer, 'x', 6, info);
62443ade 1010 if (val < 0)
f622ea96 1011 return PRINT_INSN_ARG_MEMORY_ERROR;
252b5132
RH
1012 val = ((val & 7) << 3) + ((val >> 3) & 7);
1013 }
1014 else
62443ade
NC
1015 {
1016 val = fetch_arg (buffer, 's', 6, info);
1017 if (val < 0)
f622ea96 1018 return PRINT_INSN_ARG_MEMORY_ERROR;
62443ade 1019 }
252b5132 1020
be8c092b 1021 /* If the <ea> is invalid for *d, then reject this match. */
8577e690 1022 if (!m68k_valid_ea (*d, val))
f622ea96 1023 return PRINT_INSN_ARG_INVALID_OPERAND;
be8c092b 1024
252b5132
RH
1025 /* Get register number assuming address register. */
1026 regno = (val & 7) + 8;
1027 regname = reg_names[regno];
1028 switch (val >> 3)
1029 {
1030 case 0:
1031 (*info->fprintf_func) (info->stream, "%s", reg_names[val]);
1032 break;
1033
1034 case 1:
1035 (*info->fprintf_func) (info->stream, "%s", regname);
1036 break;
1037
1038 case 2:
1039 (*info->fprintf_func) (info->stream, "%s@", regname);
1040 break;
1041
1042 case 3:
1043 (*info->fprintf_func) (info->stream, "%s@+", regname);
1044 break;
1045
1046 case 4:
1047 (*info->fprintf_func) (info->stream, "%s@-", regname);
1048 break;
1049
1050 case 5:
f622ea96 1051 NEXTWORD (p, val, PRINT_INSN_ARG_MEMORY_ERROR);
252b5132
RH
1052 (*info->fprintf_func) (info->stream, "%s@(%d)", regname, val);
1053 break;
1054
1055 case 6:
1056 p = print_indexed (regno, p, addr, info);
62443ade 1057 if (p == NULL)
f622ea96 1058 return PRINT_INSN_ARG_MEMORY_ERROR;
252b5132
RH
1059 break;
1060
1061 case 7:
1062 switch (val & 7)
1063 {
1064 case 0:
f622ea96 1065 NEXTWORD (p, val, PRINT_INSN_ARG_MEMORY_ERROR);
252b5132
RH
1066 (*info->print_address_func) (val, info);
1067 break;
1068
1069 case 1:
62443ade 1070 NEXTULONG (p, uval);
252b5132
RH
1071 (*info->print_address_func) (uval, info);
1072 break;
1073
1074 case 2:
f622ea96 1075 NEXTWORD (p, val, PRINT_INSN_ARG_MEMORY_ERROR);
252b5132
RH
1076 (*info->fprintf_func) (info->stream, "%%pc@(");
1077 (*info->print_address_func) (addr + val, info);
1078 (*info->fprintf_func) (info->stream, ")");
1079 break;
1080
1081 case 3:
1082 p = print_indexed (-1, p, addr, info);
62443ade 1083 if (p == NULL)
f622ea96 1084 return PRINT_INSN_ARG_MEMORY_ERROR;
252b5132
RH
1085 break;
1086
1087 case 4:
1088 flt_p = 1; /* Assume it's a float... */
ec22bdda 1089 switch (place)
252b5132
RH
1090 {
1091 case 'b':
62443ade 1092 NEXTBYTE (p, val);
252b5132
RH
1093 flt_p = 0;
1094 break;
1095
1096 case 'w':
f622ea96 1097 NEXTWORD (p, val, PRINT_INSN_ARG_MEMORY_ERROR);
252b5132
RH
1098 flt_p = 0;
1099 break;
1100
1101 case 'l':
f622ea96 1102 NEXTLONG (p, val, PRINT_INSN_ARG_MEMORY_ERROR);
252b5132
RH
1103 flt_p = 0;
1104 break;
1105
1106 case 'f':
ec22bdda 1107 NEXTSINGLE (flval, p);
252b5132
RH
1108 break;
1109
1110 case 'F':
ec22bdda 1111 NEXTDOUBLE (flval, p);
252b5132
RH
1112 break;
1113
1114 case 'x':
ec22bdda 1115 NEXTEXTEND (flval, p);
252b5132
RH
1116 break;
1117
1118 case 'p':
62443ade 1119 NEXTPACKED (p, flval);
252b5132
RH
1120 break;
1121
1122 default:
f622ea96 1123 return PRINT_INSN_ARG_INVALID_OPERAND;
252b5132 1124 }
ec22bdda 1125 if (flt_p) /* Print a float? */
09ec0d17 1126 (*info->fprintf_func) (info->stream, "#0e%g", flval);
252b5132
RH
1127 else
1128 (*info->fprintf_func) (info->stream, "#%d", val);
1129 break;
1130
1131 default:
f622ea96 1132 return PRINT_INSN_ARG_INVALID_OPERAND;
252b5132
RH
1133 }
1134 }
fd99574b
NC
1135
1136 /* If place is '/', then this is the case of the mask bit for
1137 mac/emac loads. Now that the arg has been printed, grab the
1138 mask bit and if set, add a '&' to the arg. */
1139 if (place == '/')
1140 {
62443ade 1141 FETCH_ARG (1, val);
fd99574b 1142 if (val)
be8c092b 1143 info->fprintf_func (info->stream, "&");
fd99574b 1144 }
252b5132
RH
1145 break;
1146
1147 case 'L':
1148 case 'l':
1149 if (place == 'w')
1150 {
1151 char doneany;
1152 p1 = buffer + 2;
f622ea96 1153 NEXTWORD (p1, val, PRINT_INSN_ARG_MEMORY_ERROR);
252b5132
RH
1154 /* Move the pointer ahead if this point is farther ahead
1155 than the last. */
1156 p = p1 > p ? p1 : p;
1157 if (val == 0)
1158 {
1159 (*info->fprintf_func) (info->stream, "#0");
1160 break;
1161 }
1162 if (*d == 'l')
1163 {
47b0e7ad
NC
1164 int newval = 0;
1165
252b5132
RH
1166 for (regno = 0; regno < 16; ++regno)
1167 if (val & (0x8000 >> regno))
1168 newval |= 1 << regno;
1169 val = newval;
1170 }
1171 val &= 0xffff;
1172 doneany = 0;
1173 for (regno = 0; regno < 16; ++regno)
1174 if (val & (1 << regno))
1175 {
1176 int first_regno;
47b0e7ad 1177
252b5132
RH
1178 if (doneany)
1179 (*info->fprintf_func) (info->stream, "/");
1180 doneany = 1;
1181 (*info->fprintf_func) (info->stream, "%s", reg_names[regno]);
1182 first_regno = regno;
1183 while (val & (1 << (regno + 1)))
1184 ++regno;
1185 if (regno > first_regno)
1186 (*info->fprintf_func) (info->stream, "-%s",
1187 reg_names[regno]);
1188 }
1189 }
1190 else if (place == '3')
1191 {
1192 /* `fmovem' insn. */
1193 char doneany;
62443ade
NC
1194
1195 FETCH_ARG (8, val);
252b5132
RH
1196 if (val == 0)
1197 {
1198 (*info->fprintf_func) (info->stream, "#0");
1199 break;
1200 }
1201 if (*d == 'l')
1202 {
47b0e7ad
NC
1203 int newval = 0;
1204
252b5132
RH
1205 for (regno = 0; regno < 8; ++regno)
1206 if (val & (0x80 >> regno))
1207 newval |= 1 << regno;
1208 val = newval;
1209 }
1210 val &= 0xff;
1211 doneany = 0;
1212 for (regno = 0; regno < 8; ++regno)
1213 if (val & (1 << regno))
1214 {
1215 int first_regno;
1216 if (doneany)
1217 (*info->fprintf_func) (info->stream, "/");
1218 doneany = 1;
1219 (*info->fprintf_func) (info->stream, "%%fp%d", regno);
1220 first_regno = regno;
1221 while (val & (1 << (regno + 1)))
1222 ++regno;
1223 if (regno > first_regno)
1224 (*info->fprintf_func) (info->stream, "-%%fp%d", regno);
1225 }
1226 }
1227 else if (place == '8')
1228 {
62443ade 1229 FETCH_ARG (3, val);
47b0e7ad 1230 /* fmoveml for FP status registers. */
62443ade 1231 (*info->fprintf_func) (info->stream, "%s", fpcr_names[val]);
252b5132
RH
1232 }
1233 else
f622ea96 1234 return PRINT_INSN_ARG_INVALID_OP_TABLE;
252b5132
RH
1235 break;
1236
1237 case 'X':
1238 place = '8';
1a0670f3 1239 /* Fall through. */
252b5132
RH
1240 case 'Y':
1241 case 'Z':
1242 case 'W':
1243 case '0':
1244 case '1':
1245 case '2':
1246 case '3':
1247 {
252b5132 1248 char *name = 0;
47b0e7ad 1249
62443ade 1250 FETCH_ARG (5, val);
252b5132
RH
1251 switch (val)
1252 {
1253 case 2: name = "%tt0"; break;
1254 case 3: name = "%tt1"; break;
1255 case 0x10: name = "%tc"; break;
1256 case 0x11: name = "%drp"; break;
1257 case 0x12: name = "%srp"; break;
1258 case 0x13: name = "%crp"; break;
1259 case 0x14: name = "%cal"; break;
1260 case 0x15: name = "%val"; break;
1261 case 0x16: name = "%scc"; break;
1262 case 0x17: name = "%ac"; break;
1263 case 0x18: name = "%psr"; break;
1264 case 0x19: name = "%pcsr"; break;
1265 case 0x1c:
1266 case 0x1d:
1267 {
1268 int break_reg = ((buffer[3] >> 2) & 7);
47b0e7ad 1269
252b5132
RH
1270 (*info->fprintf_func)
1271 (info->stream, val == 0x1c ? "%%bad%d" : "%%bac%d",
1272 break_reg);
1273 }
1274 break;
1275 default:
1276 (*info->fprintf_func) (info->stream, "<mmu register %d>", val);
1277 }
1278 if (name)
1279 (*info->fprintf_func) (info->stream, "%s", name);
1280 }
1281 break;
1282
1283 case 'f':
1284 {
62443ade 1285 int fc;
47b0e7ad 1286
62443ade 1287 FETCH_ARG (5, fc);
252b5132
RH
1288 if (fc == 1)
1289 (*info->fprintf_func) (info->stream, "%%dfc");
1290 else if (fc == 0)
1291 (*info->fprintf_func) (info->stream, "%%sfc");
1292 else
1293 /* xgettext:c-format */
1294 (*info->fprintf_func) (info->stream, _("<function code %d>"), fc);
1295 }
1296 break;
1297
1298 case 'V':
1299 (*info->fprintf_func) (info->stream, "%%val");
1300 break;
1301
1302 case 't':
1303 {
62443ade 1304 int level;
47b0e7ad 1305
62443ade 1306 FETCH_ARG (3, level);
252b5132
RH
1307 (*info->fprintf_func) (info->stream, "%d", level);
1308 }
1309 break;
1310
461d5ddd
ILT
1311 case 'u':
1312 {
1313 short is_upper = 0;
62443ade 1314 int reg;
3e602632 1315
62443ade 1316 FETCH_ARG (5, reg);
461d5ddd
ILT
1317 if (reg & 0x10)
1318 {
1319 is_upper = 1;
1320 reg &= 0xf;
1321 }
1322 (*info->fprintf_func) (info->stream, "%s%s",
be8c092b 1323 reg_half_names[reg],
461d5ddd
ILT
1324 is_upper ? "u" : "l");
1325 }
1326 break;
3e602632 1327
252b5132 1328 default:
f622ea96 1329 return PRINT_INSN_ARG_INVALID_OP_TABLE;
252b5132
RH
1330 }
1331
1332 return p - p0;
1333}
1334
47b0e7ad 1335/* Try to match the current instruction to best and if so, return the
9608051a
YQ
1336 number of bytes consumed from the instruction stream, else zero.
1337 Return -1 on memory error. */
252b5132
RH
1338
1339static int
47b0e7ad
NC
1340match_insn_m68k (bfd_vma memaddr,
1341 disassemble_info * info,
a596001e 1342 const struct m68k_opcode * best)
252b5132 1343{
47b0e7ad
NC
1344 unsigned char *save_p;
1345 unsigned char *p;
1346 const char *d;
afa2158f 1347 const char *args = best->args;
be8c092b 1348
a596001e 1349 struct private *priv = (struct private *) info->private_data;
47b0e7ad
NC
1350 bfd_byte *buffer = priv->the_buffer;
1351 fprintf_ftype save_printer = info->fprintf_func;
1352 void (* save_print_address) (bfd_vma, struct disassemble_info *)
1353 = info->print_address_func;
fd99574b 1354
afa2158f
NS
1355 if (*args == '.')
1356 args++;
43e65147 1357
47b0e7ad
NC
1358 /* Point at first word of argument data,
1359 and at descriptor for first argument. */
1360 p = buffer + 2;
fd99574b 1361
47b0e7ad
NC
1362 /* Figure out how long the fixed-size portion of the instruction is.
1363 The only place this is stored in the opcode table is
1364 in the arguments--look for arguments which specify fields in the 2nd
1365 or 3rd words of the instruction. */
afa2158f 1366 for (d = args; *d; d += 2)
47b0e7ad
NC
1367 {
1368 /* I don't think it is necessary to be checking d[0] here;
1369 I suspect all this could be moved to the case statement below. */
1370 if (d[0] == '#')
1371 {
1372 if (d[1] == 'l' && p - buffer < 6)
1373 p = buffer + 6;
1374 else if (p - buffer < 4 && d[1] != 'C' && d[1] != '8')
1375 p = buffer + 4;
1376 }
fd99574b 1377
47b0e7ad
NC
1378 if ((d[0] == 'L' || d[0] == 'l') && d[1] == 'w' && p - buffer < 4)
1379 p = buffer + 4;
fd99574b 1380
47b0e7ad
NC
1381 switch (d[1])
1382 {
1383 case '1':
1384 case '2':
1385 case '3':
1386 case '7':
1387 case '8':
1388 case '9':
1389 case 'i':
1390 if (p - buffer < 4)
1391 p = buffer + 4;
1392 break;
1393 case '4':
1394 case '5':
1395 case '6':
1396 if (p - buffer < 6)
1397 p = buffer + 6;
1398 break;
1399 default:
1400 break;
1401 }
1402 }
252b5132 1403
47b0e7ad
NC
1404 /* pflusha is an exceptions. It takes no arguments but is two words
1405 long. Recognize it by looking at the lower 16 bits of the mask. */
1406 if (p - buffer < 4 && (best->match & 0xFFFF) != 0)
1407 p = buffer + 4;
252b5132 1408
47b0e7ad
NC
1409 /* lpstop is another exception. It takes a one word argument but is
1410 three words long. */
1411 if (p - buffer < 6
1412 && (best->match & 0xffff) == 0xffff
afa2158f
NS
1413 && args[0] == '#'
1414 && args[1] == 'w')
47b0e7ad
NC
1415 {
1416 /* Copy the one word argument into the usual location for a one
1417 word argument, to simplify printing it. We can get away with
1418 this because we know exactly what the second word is, and we
1419 aren't going to print anything based on it. */
1420 p = buffer + 6;
9608051a
YQ
1421 if (!FETCH_DATA (info, p))
1422 return -1;
47b0e7ad
NC
1423 buffer[2] = buffer[4];
1424 buffer[3] = buffer[5];
1425 }
252b5132 1426
9608051a
YQ
1427 if (!FETCH_DATA (info, p))
1428 return -1;
3e602632 1429
47b0e7ad
NC
1430 save_p = p;
1431 info->print_address_func = dummy_print_address;
1432 info->fprintf_func = (fprintf_ftype) dummy_printer;
252b5132 1433
47b0e7ad
NC
1434 /* We scan the operands twice. The first time we don't print anything,
1435 but look for errors. */
afa2158f 1436 for (d = args; *d; d += 2)
47b0e7ad
NC
1437 {
1438 int eaten = print_insn_arg (d, buffer, p, memaddr + (p - buffer), info);
252b5132 1439
47b0e7ad
NC
1440 if (eaten >= 0)
1441 p += eaten;
f622ea96
YQ
1442 else if (eaten == PRINT_INSN_ARG_INVALID_OPERAND
1443 || eaten == PRINT_INSN_ARG_MEMORY_ERROR)
47b0e7ad
NC
1444 {
1445 info->fprintf_func = save_printer;
1446 info->print_address_func = save_print_address;
9608051a 1447 return eaten == PRINT_INSN_ARG_MEMORY_ERROR ? -1 : 0;
47b0e7ad
NC
1448 }
1449 else
1450 {
f095b97b
JW
1451 /* We must restore the print functions before trying to print the
1452 error message. */
1453 info->fprintf_func = save_printer;
1454 info->print_address_func = save_print_address;
47b0e7ad
NC
1455 info->fprintf_func (info->stream,
1456 /* xgettext:c-format */
1457 _("<internal error in opcode table: %s %s>\n"),
62443ade 1458 best->name, best->args);
47b0e7ad
NC
1459 return 2;
1460 }
1461 }
461d5ddd 1462
47b0e7ad
NC
1463 p = save_p;
1464 info->fprintf_func = save_printer;
1465 info->print_address_func = save_print_address;
461d5ddd 1466
afa2158f 1467 d = args;
461d5ddd 1468
47b0e7ad 1469 info->fprintf_func (info->stream, "%s", best->name);
461d5ddd 1470
47b0e7ad
NC
1471 if (*d)
1472 info->fprintf_func (info->stream, " ");
461d5ddd 1473
47b0e7ad
NC
1474 while (*d)
1475 {
1476 p += print_insn_arg (d, buffer, p, memaddr + (p - buffer), info);
1477 d += 2;
461d5ddd 1478
47b0e7ad
NC
1479 if (*d && *(d - 2) != 'I' && *d != 'k')
1480 info->fprintf_func (info->stream, ",");
252b5132
RH
1481 }
1482
47b0e7ad 1483 return p - buffer;
252b5132
RH
1484}
1485
a596001e
RS
1486/* Try to interpret the instruction at address MEMADDR as one that
1487 can execute on a processor with the features given by ARCH_MASK.
1488 If successful, print the instruction to INFO->STREAM and return
9608051a
YQ
1489 its length in bytes. Return 0 otherwise. Return -1 on memory
1490 error. */
252b5132 1491
a596001e
RS
1492static int
1493m68k_scan_mask (bfd_vma memaddr, disassemble_info *info,
1494 unsigned int arch_mask)
252b5132 1495{
47b0e7ad
NC
1496 int i;
1497 const char *d;
47b0e7ad 1498 static const struct m68k_opcode **opcodes[16];
a596001e 1499 static int numopcodes[16];
47b0e7ad 1500 int val;
a596001e
RS
1501 int major_opcode;
1502
1503 struct private *priv = (struct private *) info->private_data;
1504 bfd_byte *buffer = priv->the_buffer;
252b5132 1505
47b0e7ad 1506 if (!opcodes[0])
252b5132 1507 {
47b0e7ad
NC
1508 /* Speed up the matching by sorting the opcode
1509 table on the upper four bits of the opcode. */
1510 const struct m68k_opcode **opc_pointer[16];
252b5132 1511
47b0e7ad
NC
1512 /* First count how many opcodes are in each of the sixteen buckets. */
1513 for (i = 0; i < m68k_numopcodes; i++)
1514 numopcodes[(m68k_opcodes[i].opcode >> 28) & 15]++;
252b5132 1515
47b0e7ad
NC
1516 /* Then create a sorted table of pointers
1517 that point into the unsorted table. */
1518 opc_pointer[0] = xmalloc (sizeof (struct m68k_opcode *)
1519 * m68k_numopcodes);
1520 opcodes[0] = opc_pointer[0];
252b5132 1521
47b0e7ad
NC
1522 for (i = 1; i < 16; i++)
1523 {
1524 opc_pointer[i] = opc_pointer[i - 1] + numopcodes[i - 1];
1525 opcodes[i] = opc_pointer[i];
1526 }
252b5132 1527
47b0e7ad
NC
1528 for (i = 0; i < m68k_numopcodes; i++)
1529 *opc_pointer[(m68k_opcodes[i].opcode >> 28) & 15]++ = &m68k_opcodes[i];
252b5132
RH
1530 }
1531
9608051a
YQ
1532 if (!FETCH_DATA (info, buffer + 2))
1533 return -1;
47b0e7ad 1534 major_opcode = (buffer[0] >> 4) & 15;
252b5132 1535
47b0e7ad
NC
1536 for (i = 0; i < numopcodes[major_opcode]; i++)
1537 {
1538 const struct m68k_opcode *opc = opcodes[major_opcode][i];
1539 unsigned long opcode = opc->opcode;
1540 unsigned long match = opc->match;
afa2158f
NS
1541 const char *args = opc->args;
1542
1543 if (*args == '.')
1544 args++;
252b5132 1545
47b0e7ad
NC
1546 if (((0xff & buffer[0] & (match >> 24)) == (0xff & (opcode >> 24)))
1547 && ((0xff & buffer[1] & (match >> 16)) == (0xff & (opcode >> 16)))
1548 /* Only fetch the next two bytes if we need to. */
1549 && (((0xffff & match) == 0)
1550 ||
1551 (FETCH_DATA (info, buffer + 4)
1552 && ((0xff & buffer[2] & (match >> 8)) == (0xff & (opcode >> 8)))
1553 && ((0xff & buffer[3] & match) == (0xff & opcode)))
1554 )
1555 && (opc->arch & arch_mask) != 0)
1556 {
1557 /* Don't use for printout the variants of divul and divsl
1558 that have the same register number in two places.
1559 The more general variants will match instead. */
afa2158f 1560 for (d = args; *d; d += 2)
47b0e7ad
NC
1561 if (d[1] == 'D')
1562 break;
252b5132 1563
47b0e7ad
NC
1564 /* Don't use for printout the variants of most floating
1565 point coprocessor instructions which use the same
1566 register number in two places, as above. */
1567 if (*d == '\0')
afa2158f 1568 for (d = args; *d; d += 2)
47b0e7ad
NC
1569 if (d[1] == 't')
1570 break;
252b5132 1571
47b0e7ad
NC
1572 /* Don't match fmovel with more than one register;
1573 wait for fmoveml. */
1574 if (*d == '\0')
1575 {
afa2158f 1576 for (d = args; *d; d += 2)
47b0e7ad
NC
1577 {
1578 if (d[0] == 's' && d[1] == '8')
1579 {
1580 val = fetch_arg (buffer, d[1], 3, info);
62443ade
NC
1581 if (val < 0)
1582 return 0;
47b0e7ad
NC
1583 if ((val & (val - 1)) != 0)
1584 break;
1585 }
1586 }
1587 }
252b5132 1588
dc82c973
AS
1589 /* Don't match FPU insns with non-default coprocessor ID. */
1590 if (*d == '\0')
1591 {
afa2158f 1592 for (d = args; *d; d += 2)
dc82c973
AS
1593 {
1594 if (d[0] == 'I')
1595 {
1596 val = fetch_arg (buffer, 'd', 3, info);
1597 if (val != 1)
1598 break;
1599 }
1600 }
1601 }
1602
47b0e7ad 1603 if (*d == '\0')
a596001e 1604 if ((val = match_insn_m68k (memaddr, info, opc)))
47b0e7ad
NC
1605 return val;
1606 }
252b5132 1607 }
a596001e 1608 return 0;
43e65147 1609}
a596001e
RS
1610
1611/* Print the m68k instruction at address MEMADDR in debugged memory,
1612 on INFO->STREAM. Returns length of the instruction, in bytes. */
1613
1614int
1615print_insn_m68k (bfd_vma memaddr, disassemble_info *info)
1616{
1617 unsigned int arch_mask;
1618 struct private priv;
1619 int val;
1620
1621 bfd_byte *buffer = priv.the_buffer;
1622
62443ade 1623 info->private_data = & priv;
a596001e
RS
1624 /* Tell objdump to use two bytes per chunk
1625 and six bytes per line for displaying raw data. */
1626 info->bytes_per_chunk = 2;
1627 info->bytes_per_line = 6;
1628 info->display_endian = BFD_ENDIAN_BIG;
1629 priv.max_fetched = priv.the_buffer;
1630 priv.insn_start = memaddr;
1631
a596001e
RS
1632 arch_mask = bfd_m68k_mach_to_features (info->mach);
1633 if (!arch_mask)
1634 {
1635 /* First try printing an m680x0 instruction. Try printing a Coldfire
1636 one if that fails. */
1637 val = m68k_scan_mask (memaddr, info, m68k_mask);
9608051a 1638 if (val <= 0)
62443ade 1639 val = m68k_scan_mask (memaddr, info, mcf_mask);
a596001e
RS
1640 }
1641 else
1642 {
1643 val = m68k_scan_mask (memaddr, info, arch_mask);
a596001e 1644 }
47b0e7ad 1645
62443ade
NC
1646 if (val == 0)
1647 /* Handle undefined instructions. */
d8b24b95 1648 info->fprintf_func (info->stream, ".short 0x%04x", (buffer[0] << 8) + buffer[1]);
62443ade 1649
62443ade 1650 return val ? val : 2;
252b5132 1651}
This page took 1.044254 seconds and 4 git commands to generate.