1 /* Disassemble MN10300 instructions.
2 Copyright (C) 1996-2025 Free Software Foundation, Inc.
4 This file is part of the GNU opcodes library.
6 This library is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
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.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
23 #include "opcode/mn10300.h"
24 #include "disassemble.h"
27 #define HAVE_AM33_2 (info->mach == AM33_2)
28 #define HAVE_AM33 (info->mach == AM33 || HAVE_AM33_2)
29 #define HAVE_AM30 (info->mach == AM30)
32 disassemble (bfd_vma memaddr
,
33 struct disassemble_info
*info
,
37 struct mn10300_opcode
*op
= (struct mn10300_opcode
*) mn10300_opcodes
;
38 const struct mn10300_operand
*operand
;
40 unsigned long extension
= 0;
41 int status
, match
= 0;
43 /* Find the opcode. */
46 int mysize
, extra_shift
;
48 if (op
->format
== FMT_S0
)
50 else if (op
->format
== FMT_S1
51 || op
->format
== FMT_D0
)
53 else if (op
->format
== FMT_S2
54 || op
->format
== FMT_D1
)
56 else if (op
->format
== FMT_S4
)
58 else if (op
->format
== FMT_D2
)
60 else if (op
->format
== FMT_D3
)
62 else if (op
->format
== FMT_D4
)
64 else if (op
->format
== FMT_D6
)
66 else if (op
->format
== FMT_D7
|| op
->format
== FMT_D10
)
68 else if (op
->format
== FMT_D8
)
70 else if (op
->format
== FMT_D9
)
75 if ((op
->mask
& insn
) == op
->opcode
76 && size
== (unsigned int) mysize
78 || (op
->machine
== AM33_2
&& HAVE_AM33_2
)
79 || (op
->machine
== AM33
&& HAVE_AM33
)
80 || (op
->machine
== AM30
&& HAVE_AM30
)))
82 const unsigned char *opindex_ptr
;
86 if (op
->format
== FMT_D1
|| op
->format
== FMT_S1
)
88 else if (op
->format
== FMT_D2
|| op
->format
== FMT_D4
89 || op
->format
== FMT_S2
|| op
->format
== FMT_S4
90 || op
->format
== FMT_S6
|| op
->format
== FMT_D5
)
92 else if (op
->format
== FMT_D7
93 || op
->format
== FMT_D8
94 || op
->format
== FMT_D9
)
99 if (size
== 1 || size
== 2)
103 && (op
->format
== FMT_D1
104 || op
->opcode
== 0xdf0000
105 || op
->opcode
== 0xde0000))
109 && op
->format
== FMT_D6
)
115 status
= (*info
->read_memory_func
) (memaddr
+ 1, buffer
, 2, info
);
118 (*info
->memory_error_func
) (status
, memaddr
, info
);
122 insn
|= bfd_getl16 (buffer
);
126 && (op
->opcode
== 0xfaf80000
127 || op
->opcode
== 0xfaf00000
128 || op
->opcode
== 0xfaf40000))
132 && (op
->format
== FMT_D7
133 || op
->format
== FMT_D10
))
139 status
= (*info
->read_memory_func
) (memaddr
+ 2, buffer
, 2, info
);
142 (*info
->memory_error_func
) (status
, memaddr
, info
);
146 insn
|= bfd_getl16 (buffer
);
149 else if (size
== 5 && op
->opcode
== 0xdc000000)
151 unsigned long temp
= 0;
153 status
= (*info
->read_memory_func
) (memaddr
+ 1, buffer
, 4, info
);
156 (*info
->memory_error_func
) (status
, memaddr
, info
);
159 temp
|= bfd_getl32 (buffer
);
162 insn
|= (temp
& 0xffffff00) >> 8;
163 extension
= temp
& 0xff;
165 else if (size
== 5 && op
->format
== FMT_D3
)
167 status
= (*info
->read_memory_func
) (memaddr
+ 2, buffer
, 2, info
);
170 (*info
->memory_error_func
) (status
, memaddr
, info
);
174 insn
|= bfd_getl16 (buffer
);
176 status
= (*info
->read_memory_func
) (memaddr
+ 4, buffer
, 1, info
);
179 (*info
->memory_error_func
) (status
, memaddr
, info
);
182 extension
= *(unsigned char *) buffer
;
186 unsigned long temp
= 0;
188 status
= (*info
->read_memory_func
) (memaddr
+ 1, buffer
, 2, info
);
191 (*info
->memory_error_func
) (status
, memaddr
, info
);
194 temp
|= bfd_getl16 (buffer
);
199 status
= (*info
->read_memory_func
) (memaddr
+ 4, buffer
, 1, info
);
202 (*info
->memory_error_func
) (status
, memaddr
, info
);
205 extension
= *(unsigned char *) buffer
;
207 else if (size
== 6 && op
->format
== FMT_D8
)
210 status
= (*info
->read_memory_func
) (memaddr
+ 5, buffer
, 1, info
);
213 (*info
->memory_error_func
) (status
, memaddr
, info
);
216 insn
|= *(unsigned char *) buffer
;
218 status
= (*info
->read_memory_func
) (memaddr
+ 3, buffer
, 2, info
);
221 (*info
->memory_error_func
) (status
, memaddr
, info
);
224 extension
= bfd_getl16 (buffer
);
228 unsigned long temp
= 0;
230 status
= (*info
->read_memory_func
) (memaddr
+ 2, buffer
, 4, info
);
233 (*info
->memory_error_func
) (status
, memaddr
, info
);
236 temp
|= bfd_getl32 (buffer
);
239 insn
|= (temp
>> 16) & 0xffff;
240 extension
= temp
& 0xffff;
242 else if (size
== 7 && op
->format
== FMT_D9
)
245 status
= (*info
->read_memory_func
) (memaddr
+ 3, buffer
, 4, info
);
248 (*info
->memory_error_func
) (status
, memaddr
, info
);
251 extension
= bfd_getl32 (buffer
);
252 insn
|= (extension
& 0xff000000) >> 24;
253 extension
&= 0xffffff;
255 else if (size
== 7 && op
->opcode
== 0xdd000000)
257 unsigned long temp
= 0;
259 status
= (*info
->read_memory_func
) (memaddr
+ 1, buffer
, 4, info
);
262 (*info
->memory_error_func
) (status
, memaddr
, info
);
265 temp
|= bfd_getl32 (buffer
);
268 insn
|= (temp
>> 8) & 0xffffff;
269 extension
= (temp
& 0xff) << 16;
271 status
= (*info
->read_memory_func
) (memaddr
+ 5, buffer
, 2, info
);
274 (*info
->memory_error_func
) (status
, memaddr
, info
);
277 extension
|= bfd_getb16 (buffer
);
281 unsigned long temp
= 0;
283 status
= (*info
->read_memory_func
) (memaddr
+ 2, buffer
, 4, info
);
286 (*info
->memory_error_func
) (status
, memaddr
, info
);
289 temp
|= bfd_getl32 (buffer
);
292 insn
|= (temp
>> 16) & 0xffff;
293 extension
= (temp
& 0xffff) << 8;
295 status
= (*info
->read_memory_func
) (memaddr
+ 6, buffer
, 1, info
);
298 (*info
->memory_error_func
) (status
, memaddr
, info
);
301 extension
|= *(unsigned char *) buffer
;
305 (*info
->fprintf_func
) (info
->stream
, "%s\t", op
->name
);
307 /* Now print the operands. */
308 for (opindex_ptr
= op
->operands
, nocomma
= 1;
314 operand
= &mn10300_operands
[*opindex_ptr
];
316 /* If this operand is a PLUS (autoincrement), then do not emit
317 a comma before emitting the plus. */
318 if ((operand
->flags
& MN10300_OPERAND_PLUS
) != 0)
321 if ((operand
->flags
& MN10300_OPERAND_DREG
) != 0
322 || (operand
->flags
& MN10300_OPERAND_AREG
) != 0
323 || (operand
->flags
& MN10300_OPERAND_RREG
) != 0
324 || (operand
->flags
& MN10300_OPERAND_XRREG
) != 0)
325 value
= ((insn
>> (operand
->shift
+ extra_shift
))
326 & ((1 << operand
->bits
) - 1));
327 else if ((operand
->flags
& MN10300_OPERAND_SPLIT
) != 0)
331 value
= insn
& ((1 << operand
->bits
) - 1);
332 value
<<= (32 - operand
->bits
);
333 temp
= extension
>> operand
->shift
;
334 temp
&= ((1 << (32 - operand
->bits
)) - 1);
336 value
= ((value
^ (((unsigned long) 1) << 31))
337 - (((unsigned long) 1) << 31));
339 else if ((operand
->flags
& MN10300_OPERAND_24BIT
) != 0)
343 value
= insn
& ((1 << operand
->bits
) - 1);
344 value
<<= (24 - operand
->bits
);
345 temp
= extension
>> operand
->shift
;
346 temp
&= ((1 << (24 - operand
->bits
)) - 1);
348 if ((operand
->flags
& MN10300_OPERAND_SIGNED
) != 0)
349 value
= ((value
& 0xffffff) ^ 0x800000) - 0x800000;
351 else if ((operand
->flags
& (MN10300_OPERAND_FSREG
352 | MN10300_OPERAND_FDREG
)))
354 /* See m10300-opc.c just before #define FSM0 for an
355 explanation of these variables. Note that
356 FMT-implied shifts are not taken into account for
358 unsigned long mask_low
, mask_high
;
359 int shl_low
, shr_high
, shl_high
;
361 switch (operand
->bits
)
364 /* Handle regular FP registers. */
365 if (operand
->shift
>= 0)
367 /* This is an `m' register. */
368 shl_low
= operand
->shift
;
369 shl_high
= 8 + (8 & shl_low
) + (shl_low
& 4) / 4;
373 /* This is an `n' register. */
374 shl_low
= -operand
->shift
;
375 shl_high
= shl_low
/ 4;
383 /* Handle accumulators. */
384 shl_low
= -operand
->shift
;
394 value
= ((((insn
>> shl_high
) << shr_high
) & mask_high
)
395 | ((insn
>> shl_low
) & mask_low
));
397 else if ((operand
->flags
& MN10300_OPERAND_EXTENDED
) != 0)
398 value
= ((extension
>> (operand
->shift
))
399 & ((1 << operand
->bits
) - 1));
402 value
= ((insn
>> (operand
->shift
))
403 & ((1 << operand
->bits
) - 1));
405 if ((operand
->flags
& MN10300_OPERAND_SIGNED
) != 0
406 /* These are properly extended by the code above. */
407 && ((operand
->flags
& MN10300_OPERAND_24BIT
) == 0))
408 value
= ((value
^ (((unsigned long) 1) << (operand
->bits
- 1)))
409 - (((unsigned long) 1) << (operand
->bits
- 1)));
413 || ((operand
->flags
& MN10300_OPERAND_PAREN
) == 0)))
414 (*info
->fprintf_func
) (info
->stream
, ",");
418 if ((operand
->flags
& MN10300_OPERAND_DREG
) != 0)
419 (*info
->fprintf_func
) (info
->stream
, "d%d", (int) value
);
421 else if ((operand
->flags
& MN10300_OPERAND_AREG
) != 0)
422 (*info
->fprintf_func
) (info
->stream
, "a%d", (int) value
);
424 else if ((operand
->flags
& MN10300_OPERAND_SP
) != 0)
425 (*info
->fprintf_func
) (info
->stream
, "sp");
427 else if ((operand
->flags
& MN10300_OPERAND_PSW
) != 0)
428 (*info
->fprintf_func
) (info
->stream
, "psw");
430 else if ((operand
->flags
& MN10300_OPERAND_MDR
) != 0)
431 (*info
->fprintf_func
) (info
->stream
, "mdr");
433 else if ((operand
->flags
& MN10300_OPERAND_RREG
) != 0)
436 (*info
->fprintf_func
) (info
->stream
, "r%d", (int) value
);
438 (*info
->fprintf_func
) (info
->stream
, "a%d", (int) value
- 8);
440 (*info
->fprintf_func
) (info
->stream
, "d%d", (int) value
- 12);
443 else if ((operand
->flags
& MN10300_OPERAND_XRREG
) != 0)
446 (*info
->fprintf_func
) (info
->stream
, "sp");
448 (*info
->fprintf_func
) (info
->stream
, "xr%d", (int) value
);
451 else if ((operand
->flags
& MN10300_OPERAND_FSREG
) != 0)
452 (*info
->fprintf_func
) (info
->stream
, "fs%d", (int) value
);
454 else if ((operand
->flags
& MN10300_OPERAND_FDREG
) != 0)
455 (*info
->fprintf_func
) (info
->stream
, "fd%d", (int) value
);
457 else if ((operand
->flags
& MN10300_OPERAND_FPCR
) != 0)
458 (*info
->fprintf_func
) (info
->stream
, "fpcr");
460 else if ((operand
->flags
& MN10300_OPERAND_USP
) != 0)
461 (*info
->fprintf_func
) (info
->stream
, "usp");
463 else if ((operand
->flags
& MN10300_OPERAND_SSP
) != 0)
464 (*info
->fprintf_func
) (info
->stream
, "ssp");
466 else if ((operand
->flags
& MN10300_OPERAND_MSP
) != 0)
467 (*info
->fprintf_func
) (info
->stream
, "msp");
469 else if ((operand
->flags
& MN10300_OPERAND_PC
) != 0)
470 (*info
->fprintf_func
) (info
->stream
, "pc");
472 else if ((operand
->flags
& MN10300_OPERAND_EPSW
) != 0)
473 (*info
->fprintf_func
) (info
->stream
, "epsw");
475 else if ((operand
->flags
& MN10300_OPERAND_PLUS
) != 0)
476 (*info
->fprintf_func
) (info
->stream
, "+");
478 else if ((operand
->flags
& MN10300_OPERAND_PAREN
) != 0)
481 (*info
->fprintf_func
) (info
->stream
, ")");
484 (*info
->fprintf_func
) (info
->stream
, "(");
490 else if ((operand
->flags
& MN10300_OPERAND_PCREL
) != 0)
491 (*info
->print_address_func
) ((long) value
+ memaddr
, info
);
493 else if ((operand
->flags
& MN10300_OPERAND_MEMADDR
) != 0)
494 (*info
->print_address_func
) (value
, info
);
496 else if ((operand
->flags
& MN10300_OPERAND_REG_LIST
) != 0)
500 (*info
->fprintf_func
) (info
->stream
, "[");
503 (*info
->fprintf_func
) (info
->stream
, "d2");
510 (*info
->fprintf_func
) (info
->stream
, ",");
511 (*info
->fprintf_func
) (info
->stream
, "d3");
518 (*info
->fprintf_func
) (info
->stream
, ",");
519 (*info
->fprintf_func
) (info
->stream
, "a2");
526 (*info
->fprintf_func
) (info
->stream
, ",");
527 (*info
->fprintf_func
) (info
->stream
, "a3");
534 (*info
->fprintf_func
) (info
->stream
, ",");
535 (*info
->fprintf_func
) (info
->stream
, "other");
542 (*info
->fprintf_func
) (info
->stream
, ",");
543 (*info
->fprintf_func
) (info
->stream
, "exreg0");
549 (*info
->fprintf_func
) (info
->stream
, ",");
550 (*info
->fprintf_func
) (info
->stream
, "exreg1");
556 (*info
->fprintf_func
) (info
->stream
, ",");
557 (*info
->fprintf_func
) (info
->stream
, "exother");
560 (*info
->fprintf_func
) (info
->stream
, "]");
564 (*info
->fprintf_func
) (info
->stream
, "%ld", (long) value
);
573 /* xgettext:c-format */
574 (*info
->fprintf_func
) (info
->stream
, _("unknown\t0x%04lx"), insn
);
578 print_insn_mn10300 (bfd_vma memaddr
, struct disassemble_info
*info
)
583 unsigned int consume
;
585 /* First figure out how big the opcode is. */
586 status
= (*info
->read_memory_func
) (memaddr
, buffer
, 1, info
);
589 (*info
->memory_error_func
) (status
, memaddr
, info
);
592 insn
= *(unsigned char *) buffer
;
594 /* These are one byte insns. */
595 if ((insn
& 0xf3) == 0x00
596 || (insn
& 0xf0) == 0x10
597 || (insn
& 0xfc) == 0x3c
598 || (insn
& 0xf3) == 0x41
599 || (insn
& 0xf3) == 0x40
600 || (insn
& 0xfc) == 0x50
601 || (insn
& 0xfc) == 0x54
602 || (insn
& 0xf0) == 0x60
603 || (insn
& 0xf0) == 0x70
604 || ((insn
& 0xf0) == 0x80
605 && (insn
& 0x0c) >> 2 != (insn
& 0x03))
606 || ((insn
& 0xf0) == 0x90
607 && (insn
& 0x0c) >> 2 != (insn
& 0x03))
608 || ((insn
& 0xf0) == 0xa0
609 && (insn
& 0x0c) >> 2 != (insn
& 0x03))
610 || ((insn
& 0xf0) == 0xb0
611 && (insn
& 0x0c) >> 2 != (insn
& 0x03))
612 || (insn
& 0xff) == 0xcb
613 || (insn
& 0xfc) == 0xd0
614 || (insn
& 0xfc) == 0xd4
615 || (insn
& 0xfc) == 0xd8
616 || (insn
& 0xf0) == 0xe0
617 || (insn
& 0xff) == 0xff)
622 /* These are two byte insns. */
623 else if ((insn
& 0xf0) == 0x80
624 || (insn
& 0xf0) == 0x90
625 || (insn
& 0xf0) == 0xa0
626 || (insn
& 0xf0) == 0xb0
627 || (insn
& 0xfc) == 0x20
628 || (insn
& 0xfc) == 0x28
629 || (insn
& 0xf3) == 0x43
630 || (insn
& 0xf3) == 0x42
631 || (insn
& 0xfc) == 0x58
632 || (insn
& 0xfc) == 0x5c
633 || ((insn
& 0xf0) == 0xc0
634 && (insn
& 0xff) != 0xcb
635 && (insn
& 0xff) != 0xcc
636 && (insn
& 0xff) != 0xcd)
637 || (insn
& 0xff) == 0xf0
638 || (insn
& 0xff) == 0xf1
639 || (insn
& 0xff) == 0xf2
640 || (insn
& 0xff) == 0xf3
641 || (insn
& 0xff) == 0xf4
642 || (insn
& 0xff) == 0xf5
643 || (insn
& 0xff) == 0xf6)
645 status
= (*info
->read_memory_func
) (memaddr
, buffer
, 2, info
);
648 (*info
->memory_error_func
) (status
, memaddr
, info
);
651 insn
= bfd_getb16 (buffer
);
655 /* These are three byte insns. */
656 else if ((insn
& 0xff) == 0xf8
657 || (insn
& 0xff) == 0xcc
658 || (insn
& 0xff) == 0xf9
659 || (insn
& 0xf3) == 0x01
660 || (insn
& 0xf3) == 0x02
661 || (insn
& 0xf3) == 0x03
662 || (insn
& 0xfc) == 0x24
663 || (insn
& 0xfc) == 0x2c
664 || (insn
& 0xfc) == 0x30
665 || (insn
& 0xfc) == 0x34
666 || (insn
& 0xfc) == 0x38
667 || (insn
& 0xff) == 0xde
668 || (insn
& 0xff) == 0xdf
669 || (insn
& 0xff) == 0xf9
670 || (insn
& 0xff) == 0xcc)
672 status
= (*info
->read_memory_func
) (memaddr
, buffer
, 2, info
);
675 (*info
->memory_error_func
) (status
, memaddr
, info
);
678 insn
= bfd_getb16 (buffer
);
680 status
= (*info
->read_memory_func
) (memaddr
+ 2, buffer
, 1, info
);
683 (*info
->memory_error_func
) (status
, memaddr
, info
);
686 insn
|= *(unsigned char *) buffer
;
690 /* These are four byte insns. */
691 else if ((insn
& 0xff) == 0xfa
692 || (insn
& 0xff) == 0xf7
693 || (insn
& 0xff) == 0xfb)
695 status
= (*info
->read_memory_func
) (memaddr
, buffer
, 4, info
);
698 (*info
->memory_error_func
) (status
, memaddr
, info
);
701 insn
= bfd_getb32 (buffer
);
705 /* These are five byte insns. */
706 else if ((insn
& 0xff) == 0xcd
707 || (insn
& 0xff) == 0xdc)
709 status
= (*info
->read_memory_func
) (memaddr
, buffer
, 4, info
);
712 (*info
->memory_error_func
) (status
, memaddr
, info
);
715 insn
= bfd_getb32 (buffer
);
719 /* These are six byte insns. */
720 else if ((insn
& 0xff) == 0xfd
721 || (insn
& 0xff) == 0xfc)
723 status
= (*info
->read_memory_func
) (memaddr
, buffer
, 4, info
);
726 (*info
->memory_error_func
) (status
, memaddr
, info
);
730 insn
= bfd_getb32 (buffer
);
734 /* Else its a seven byte insns (in theory). */
737 status
= (*info
->read_memory_func
) (memaddr
, buffer
, 4, info
);
740 (*info
->memory_error_func
) (status
, memaddr
, info
);
744 insn
= bfd_getb32 (buffer
);
746 /* Handle the 5-byte extended instruction codes. */
747 if ((insn
& 0xfff80000) == 0xfe800000)
751 disassemble (memaddr
, info
, insn
, consume
);