2006-05-01 H.J. Lu <hongjiu.lu@intel.com>
[binutils.git] / opcodes / m10300-dis.c
blob3b0617b06172962f8f57b1743bc471b420268389
1 /* Disassemble MN10300 instructions.
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2003, 2005
3 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
18 MA 02110-1301, USA. */
20 #include <stdio.h>
22 #include "sysdep.h"
23 #include "opcode/mn10300.h"
24 #include "dis-asm.h"
25 #include "opintl.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)
31 static void
32 disassemble (bfd_vma memaddr,
33 struct disassemble_info *info,
34 unsigned long insn,
35 unsigned int size)
37 struct mn10300_opcode *op = (struct mn10300_opcode *) mn10300_opcodes;
38 const struct mn10300_operand *operand;
39 bfd_byte buffer[4];
40 unsigned long extension = 0;
41 int status, match = 0;
43 /* Find the opcode. */
44 while (op->name)
46 int mysize, extra_shift;
48 if (op->format == FMT_S0)
49 mysize = 1;
50 else if (op->format == FMT_S1
51 || op->format == FMT_D0)
52 mysize = 2;
53 else if (op->format == FMT_S2
54 || op->format == FMT_D1)
55 mysize = 3;
56 else if (op->format == FMT_S4)
57 mysize = 5;
58 else if (op->format == FMT_D2)
59 mysize = 4;
60 else if (op->format == FMT_D3)
61 mysize = 5;
62 else if (op->format == FMT_D4)
63 mysize = 6;
64 else if (op->format == FMT_D6)
65 mysize = 3;
66 else if (op->format == FMT_D7 || op->format == FMT_D10)
67 mysize = 4;
68 else if (op->format == FMT_D8)
69 mysize = 6;
70 else if (op->format == FMT_D9)
71 mysize = 7;
72 else
73 mysize = 7;
75 if ((op->mask & insn) == op->opcode
76 && size == (unsigned int) mysize
77 && (op->machine == 0
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;
83 unsigned int nocomma;
84 int paren = 0;
86 if (op->format == FMT_D1 || op->format == FMT_S1)
87 extra_shift = 8;
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)
91 extra_shift = 16;
92 else if (op->format == FMT_D7
93 || op->format == FMT_D8
94 || op->format == FMT_D9)
95 extra_shift = 8;
96 else
97 extra_shift = 0;
99 if (size == 1 || size == 2)
100 extension = 0;
102 else if (size == 3
103 && (op->format == FMT_D1
104 || op->opcode == 0xdf0000
105 || op->opcode == 0xde0000))
106 extension = 0;
108 else if (size == 3
109 && op->format == FMT_D6)
110 extension = 0;
112 else if (size == 3)
114 insn &= 0xff0000;
115 status = (*info->read_memory_func) (memaddr + 1, buffer, 2, info);
116 if (status != 0)
118 (*info->memory_error_func) (status, memaddr, info);
119 return;
122 insn |= bfd_getl16 (buffer);
123 extension = 0;
125 else if (size == 4
126 && (op->opcode == 0xfaf80000
127 || op->opcode == 0xfaf00000
128 || op->opcode == 0xfaf40000))
129 extension = 0;
131 else if (size == 4
132 && (op->format == FMT_D7
133 || op->format == FMT_D10))
134 extension = 0;
136 else if (size == 4)
138 insn &= 0xffff0000;
139 status = (*info->read_memory_func) (memaddr + 2, buffer, 2, info);
140 if (status != 0)
142 (*info->memory_error_func) (status, memaddr, info);
143 return;
146 insn |= bfd_getl16 (buffer);
147 extension = 0;
149 else if (size == 5 && op->opcode == 0xdc000000)
151 unsigned long temp = 0;
153 status = (*info->read_memory_func) (memaddr + 1, buffer, 4, info);
154 if (status != 0)
156 (*info->memory_error_func) (status, memaddr, info);
157 return;
159 temp |= bfd_getl32 (buffer);
161 insn &= 0xff000000;
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);
168 if (status != 0)
170 (*info->memory_error_func) (status, memaddr, info);
171 return;
173 insn &= 0xffff0000;
174 insn |= bfd_getl16 (buffer);
176 status = (*info->read_memory_func) (memaddr + 4, buffer, 1, info);
177 if (status != 0)
179 (*info->memory_error_func) (status, memaddr, info);
180 return;
182 extension = *(unsigned char *) buffer;
184 else if (size == 5)
186 unsigned long temp = 0;
188 status = (*info->read_memory_func) (memaddr + 1, buffer, 2, info);
189 if (status != 0)
191 (*info->memory_error_func) (status, memaddr, info);
192 return;
194 temp |= bfd_getl16 (buffer);
196 insn &= 0xff0000ff;
197 insn |= temp << 8;
199 status = (*info->read_memory_func) (memaddr + 4, buffer, 1, info);
200 if (status != 0)
202 (*info->memory_error_func) (status, memaddr, info);
203 return;
205 extension = *(unsigned char *) buffer;
207 else if (size == 6 && op->format == FMT_D8)
209 insn &= 0xffffff00;
210 status = (*info->read_memory_func) (memaddr + 5, buffer, 1, info);
211 if (status != 0)
213 (*info->memory_error_func) (status, memaddr, info);
214 return;
216 insn |= *(unsigned char *) buffer;
218 status = (*info->read_memory_func) (memaddr + 3, buffer, 2, info);
219 if (status != 0)
221 (*info->memory_error_func) (status, memaddr, info);
222 return;
224 extension = bfd_getl16 (buffer);
226 else if (size == 6)
228 unsigned long temp = 0;
230 status = (*info->read_memory_func) (memaddr + 2, buffer, 4, info);
231 if (status != 0)
233 (*info->memory_error_func) (status, memaddr, info);
234 return;
236 temp |= bfd_getl32 (buffer);
238 insn &= 0xffff0000;
239 insn |= (temp >> 16) & 0xffff;
240 extension = temp & 0xffff;
242 else if (size == 7 && op->format == FMT_D9)
244 insn &= 0xffffff00;
245 status = (*info->read_memory_func) (memaddr + 3, buffer, 4, info);
246 if (status != 0)
248 (*info->memory_error_func) (status, memaddr, info);
249 return;
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);
260 if (status != 0)
262 (*info->memory_error_func) (status, memaddr, info);
263 return;
265 temp |= bfd_getl32 (buffer);
267 insn &= 0xff000000;
268 insn |= (temp >> 8) & 0xffffff;
269 extension = (temp & 0xff) << 16;
271 status = (*info->read_memory_func) (memaddr + 5, buffer, 2, info);
272 if (status != 0)
274 (*info->memory_error_func) (status, memaddr, info);
275 return;
277 extension |= bfd_getb16 (buffer);
279 else if (size == 7)
281 unsigned long temp = 0;
283 status = (*info->read_memory_func) (memaddr + 2, buffer, 4, info);
284 if (status != 0)
286 (*info->memory_error_func) (status, memaddr, info);
287 return;
289 temp |= bfd_getl32 (buffer);
291 insn &= 0xffff0000;
292 insn |= (temp >> 16) & 0xffff;
293 extension = (temp & 0xffff) << 8;
295 status = (*info->read_memory_func) (memaddr + 6, buffer, 1, info);
296 if (status != 0)
298 (*info->memory_error_func) (status, memaddr, info);
299 return;
301 extension |= *(unsigned char *) buffer;
304 match = 1;
305 (*info->fprintf_func) (info->stream, "%s\t", op->name);
307 /* Now print the operands. */
308 for (opindex_ptr = op->operands, nocomma = 1;
309 *opindex_ptr != 0;
310 opindex_ptr++)
312 unsigned long value;
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)
319 nocomma = 1;
321 if ((operand->flags & MN10300_OPERAND_SPLIT) != 0)
323 unsigned long temp;
325 value = insn & ((1 << operand->bits) - 1);
326 value <<= (32 - operand->bits);
327 temp = extension >> operand->shift;
328 temp &= ((1 << (32 - operand->bits)) - 1);
329 value |= temp;
330 value = ((value ^ (((unsigned long) 1) << 31))
331 - (((unsigned long) 1) << 31));
333 else if ((operand->flags & MN10300_OPERAND_24BIT) != 0)
335 unsigned long temp;
337 value = insn & ((1 << operand->bits) - 1);
338 value <<= (24 - operand->bits);
339 temp = extension >> operand->shift;
340 temp &= ((1 << (24 - operand->bits)) - 1);
341 value |= temp;
342 if ((operand->flags & MN10300_OPERAND_SIGNED) != 0)
343 value = ((value & 0xffffff) ^ 0x800000) - 0x800000;
345 else if ((operand->flags & (MN10300_OPERAND_FSREG
346 | MN10300_OPERAND_FDREG)))
348 /* See m10300-opc.c just before #define FSM0 for an
349 explanation of these variables. Note that
350 FMT-implied shifts are not taken into account for
351 FP registers. */
352 unsigned long mask_low, mask_high;
353 int shl_low, shr_high, shl_high;
355 switch (operand->bits)
357 case 5:
358 /* Handle regular FP registers. */
359 if (operand->shift >= 0)
361 /* This is an `m' register. */
362 shl_low = operand->shift;
363 shl_high = 8 + (8 & shl_low) + (shl_low & 4) / 4;
365 else
367 /* This is an `n' register. */
368 shl_low = -operand->shift;
369 shl_high = shl_low / 4;
371 mask_low = 0x0f;
372 mask_high = 0x10;
373 shr_high = 4;
374 break;
376 case 3:
377 /* Handle accumulators. */
378 shl_low = -operand->shift;
379 shl_high = 0;
380 mask_low = 0x03;
381 mask_high = 0x04;
382 shr_high = 2;
383 break;
385 default:
386 abort ();
388 value = ((((insn >> shl_high) << shr_high) & mask_high)
389 | ((insn >> shl_low) & mask_low));
391 else if ((operand->flags & MN10300_OPERAND_EXTENDED) != 0)
392 value = ((extension >> (operand->shift))
393 & ((1 << operand->bits) - 1));
395 else
396 value = ((insn >> (operand->shift))
397 & ((1 << operand->bits) - 1));
399 if ((operand->flags & MN10300_OPERAND_SIGNED) != 0
400 /* These are properly extended by the code above. */
401 && ((operand->flags & MN10300_OPERAND_24BIT) == 0))
402 value = ((value ^ (((unsigned long) 1) << (operand->bits - 1)))
403 - (((unsigned long) 1) << (operand->bits - 1)));
405 if (!nocomma
406 && (!paren
407 || ((operand->flags & MN10300_OPERAND_PAREN) == 0)))
408 (*info->fprintf_func) (info->stream, ",");
410 nocomma = 0;
412 if ((operand->flags & MN10300_OPERAND_DREG) != 0)
414 value = ((insn >> (operand->shift + extra_shift))
415 & ((1 << operand->bits) - 1));
416 (*info->fprintf_func) (info->stream, "d%d", (int) value);
419 else if ((operand->flags & MN10300_OPERAND_AREG) != 0)
421 value = ((insn >> (operand->shift + extra_shift))
422 & ((1 << operand->bits) - 1));
423 (*info->fprintf_func) (info->stream, "a%d", (int) value);
426 else if ((operand->flags & MN10300_OPERAND_SP) != 0)
427 (*info->fprintf_func) (info->stream, "sp");
429 else if ((operand->flags & MN10300_OPERAND_PSW) != 0)
430 (*info->fprintf_func) (info->stream, "psw");
432 else if ((operand->flags & MN10300_OPERAND_MDR) != 0)
433 (*info->fprintf_func) (info->stream, "mdr");
435 else if ((operand->flags & MN10300_OPERAND_RREG) != 0)
437 value = ((insn >> (operand->shift + extra_shift))
438 & ((1 << operand->bits) - 1));
439 if (value < 8)
440 (*info->fprintf_func) (info->stream, "r%d", (int) value);
441 else if (value < 12)
442 (*info->fprintf_func) (info->stream, "a%d", (int) value - 8);
443 else
444 (*info->fprintf_func) (info->stream, "d%d", (int) value - 12);
447 else if ((operand->flags & MN10300_OPERAND_XRREG) != 0)
449 value = ((insn >> (operand->shift + extra_shift))
450 & ((1 << operand->bits) - 1));
451 if (value == 0)
452 (*info->fprintf_func) (info->stream, "sp");
453 else
454 (*info->fprintf_func) (info->stream, "xr%d", (int) value);
457 else if ((operand->flags & MN10300_OPERAND_FSREG) != 0)
458 (*info->fprintf_func) (info->stream, "fs%d", (int) value);
460 else if ((operand->flags & MN10300_OPERAND_FDREG) != 0)
461 (*info->fprintf_func) (info->stream, "fd%d", (int) value);
463 else if ((operand->flags & MN10300_OPERAND_FPCR) != 0)
464 (*info->fprintf_func) (info->stream, "fpcr");
466 else if ((operand->flags & MN10300_OPERAND_USP) != 0)
467 (*info->fprintf_func) (info->stream, "usp");
469 else if ((operand->flags & MN10300_OPERAND_SSP) != 0)
470 (*info->fprintf_func) (info->stream, "ssp");
472 else if ((operand->flags & MN10300_OPERAND_MSP) != 0)
473 (*info->fprintf_func) (info->stream, "msp");
475 else if ((operand->flags & MN10300_OPERAND_PC) != 0)
476 (*info->fprintf_func) (info->stream, "pc");
478 else if ((operand->flags & MN10300_OPERAND_EPSW) != 0)
479 (*info->fprintf_func) (info->stream, "epsw");
481 else if ((operand->flags & MN10300_OPERAND_PLUS) != 0)
482 (*info->fprintf_func) (info->stream, "+");
484 else if ((operand->flags & MN10300_OPERAND_PAREN) != 0)
486 if (paren)
487 (*info->fprintf_func) (info->stream, ")");
488 else
490 (*info->fprintf_func) (info->stream, "(");
491 nocomma = 1;
493 paren = !paren;
496 else if ((operand->flags & MN10300_OPERAND_PCREL) != 0)
497 (*info->print_address_func) ((long) value + memaddr, info);
499 else if ((operand->flags & MN10300_OPERAND_MEMADDR) != 0)
500 (*info->print_address_func) (value, info);
502 else if ((operand->flags & MN10300_OPERAND_REG_LIST) != 0)
504 int comma = 0;
506 (*info->fprintf_func) (info->stream, "[");
507 if (value & 0x80)
509 (*info->fprintf_func) (info->stream, "d2");
510 comma = 1;
513 if (value & 0x40)
515 if (comma)
516 (*info->fprintf_func) (info->stream, ",");
517 (*info->fprintf_func) (info->stream, "d3");
518 comma = 1;
521 if (value & 0x20)
523 if (comma)
524 (*info->fprintf_func) (info->stream, ",");
525 (*info->fprintf_func) (info->stream, "a2");
526 comma = 1;
529 if (value & 0x10)
531 if (comma)
532 (*info->fprintf_func) (info->stream, ",");
533 (*info->fprintf_func) (info->stream, "a3");
534 comma = 1;
537 if (value & 0x08)
539 if (comma)
540 (*info->fprintf_func) (info->stream, ",");
541 (*info->fprintf_func) (info->stream, "other");
542 comma = 1;
545 if (value & 0x04)
547 if (comma)
548 (*info->fprintf_func) (info->stream, ",");
549 (*info->fprintf_func) (info->stream, "exreg0");
550 comma = 1;
552 if (value & 0x02)
554 if (comma)
555 (*info->fprintf_func) (info->stream, ",");
556 (*info->fprintf_func) (info->stream, "exreg1");
557 comma = 1;
559 if (value & 0x01)
561 if (comma)
562 (*info->fprintf_func) (info->stream, ",");
563 (*info->fprintf_func) (info->stream, "exother");
564 comma = 1;
566 (*info->fprintf_func) (info->stream, "]");
569 else
570 (*info->fprintf_func) (info->stream, "%ld", (long) value);
572 /* All done. */
573 break;
575 op++;
578 if (!match)
579 /* xgettext:c-format */
580 (*info->fprintf_func) (info->stream, _("unknown\t0x%04lx"), insn);
584 print_insn_mn10300 (bfd_vma memaddr, struct disassemble_info *info)
586 int status;
587 bfd_byte buffer[4];
588 unsigned long insn;
589 unsigned int consume;
591 /* First figure out how big the opcode is. */
592 status = (*info->read_memory_func) (memaddr, buffer, 1, info);
593 if (status != 0)
595 (*info->memory_error_func) (status, memaddr, info);
596 return -1;
598 insn = *(unsigned char *) buffer;
600 /* These are one byte insns. */
601 if ((insn & 0xf3) == 0x00
602 || (insn & 0xf0) == 0x10
603 || (insn & 0xfc) == 0x3c
604 || (insn & 0xf3) == 0x41
605 || (insn & 0xf3) == 0x40
606 || (insn & 0xfc) == 0x50
607 || (insn & 0xfc) == 0x54
608 || (insn & 0xf0) == 0x60
609 || (insn & 0xf0) == 0x70
610 || ((insn & 0xf0) == 0x80
611 && (insn & 0x0c) >> 2 != (insn & 0x03))
612 || ((insn & 0xf0) == 0x90
613 && (insn & 0x0c) >> 2 != (insn & 0x03))
614 || ((insn & 0xf0) == 0xa0
615 && (insn & 0x0c) >> 2 != (insn & 0x03))
616 || ((insn & 0xf0) == 0xb0
617 && (insn & 0x0c) >> 2 != (insn & 0x03))
618 || (insn & 0xff) == 0xcb
619 || (insn & 0xfc) == 0xd0
620 || (insn & 0xfc) == 0xd4
621 || (insn & 0xfc) == 0xd8
622 || (insn & 0xf0) == 0xe0
623 || (insn & 0xff) == 0xff)
625 consume = 1;
628 /* These are two byte insns. */
629 else if ((insn & 0xf0) == 0x80
630 || (insn & 0xf0) == 0x90
631 || (insn & 0xf0) == 0xa0
632 || (insn & 0xf0) == 0xb0
633 || (insn & 0xfc) == 0x20
634 || (insn & 0xfc) == 0x28
635 || (insn & 0xf3) == 0x43
636 || (insn & 0xf3) == 0x42
637 || (insn & 0xfc) == 0x58
638 || (insn & 0xfc) == 0x5c
639 || ((insn & 0xf0) == 0xc0
640 && (insn & 0xff) != 0xcb
641 && (insn & 0xff) != 0xcc
642 && (insn & 0xff) != 0xcd)
643 || (insn & 0xff) == 0xf0
644 || (insn & 0xff) == 0xf1
645 || (insn & 0xff) == 0xf2
646 || (insn & 0xff) == 0xf3
647 || (insn & 0xff) == 0xf4
648 || (insn & 0xff) == 0xf5
649 || (insn & 0xff) == 0xf6)
651 status = (*info->read_memory_func) (memaddr, buffer, 2, info);
652 if (status != 0)
654 (*info->memory_error_func) (status, memaddr, info);
655 return -1;
657 insn = bfd_getb16 (buffer);
658 consume = 2;
661 /* These are three byte insns. */
662 else if ((insn & 0xff) == 0xf8
663 || (insn & 0xff) == 0xcc
664 || (insn & 0xff) == 0xf9
665 || (insn & 0xf3) == 0x01
666 || (insn & 0xf3) == 0x02
667 || (insn & 0xf3) == 0x03
668 || (insn & 0xfc) == 0x24
669 || (insn & 0xfc) == 0x2c
670 || (insn & 0xfc) == 0x30
671 || (insn & 0xfc) == 0x34
672 || (insn & 0xfc) == 0x38
673 || (insn & 0xff) == 0xde
674 || (insn & 0xff) == 0xdf
675 || (insn & 0xff) == 0xf9
676 || (insn & 0xff) == 0xcc)
678 status = (*info->read_memory_func) (memaddr, buffer, 2, info);
679 if (status != 0)
681 (*info->memory_error_func) (status, memaddr, info);
682 return -1;
684 insn = bfd_getb16 (buffer);
685 insn <<= 8;
686 status = (*info->read_memory_func) (memaddr + 2, buffer, 1, info);
687 if (status != 0)
689 (*info->memory_error_func) (status, memaddr, info);
690 return -1;
692 insn |= *(unsigned char *) buffer;
693 consume = 3;
696 /* These are four byte insns. */
697 else if ((insn & 0xff) == 0xfa
698 || (insn & 0xff) == 0xf7
699 || (insn & 0xff) == 0xfb)
701 status = (*info->read_memory_func) (memaddr, buffer, 4, info);
702 if (status != 0)
704 (*info->memory_error_func) (status, memaddr, info);
705 return -1;
707 insn = bfd_getb32 (buffer);
708 consume = 4;
711 /* These are five byte insns. */
712 else if ((insn & 0xff) == 0xcd
713 || (insn & 0xff) == 0xdc)
715 status = (*info->read_memory_func) (memaddr, buffer, 4, info);
716 if (status != 0)
718 (*info->memory_error_func) (status, memaddr, info);
719 return -1;
721 insn = bfd_getb32 (buffer);
722 consume = 5;
725 /* These are six byte insns. */
726 else if ((insn & 0xff) == 0xfd
727 || (insn & 0xff) == 0xfc)
729 status = (*info->read_memory_func) (memaddr, buffer, 4, info);
730 if (status != 0)
732 (*info->memory_error_func) (status, memaddr, info);
733 return -1;
736 insn = bfd_getb32 (buffer);
737 consume = 6;
740 /* Else its a seven byte insns (in theory). */
741 else
743 status = (*info->read_memory_func) (memaddr, buffer, 4, info);
744 if (status != 0)
746 (*info->memory_error_func) (status, memaddr, info);
747 return -1;
750 insn = bfd_getb32 (buffer);
751 consume = 7;
752 /* Handle the 5-byte extended instruction codes. */
753 if ((insn & 0xfff80000) == 0xfe800000)
754 consume = 5;
757 disassemble (memaddr, info, insn, consume);
759 return consume;