merge from gcc
[binutils.git] / opcodes / m10300-dis.c
blobacd5e4cd91a2e995f0f65bd6bbe6daabfa6506fc
1 /* Disassemble MN10300 instructions.
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2003
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, 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 static void disassemble PARAMS ((bfd_vma, struct disassemble_info *,
28 unsigned long insn, unsigned int));
30 #define HAVE_AM33_2 (info->mach == AM33_2)
31 #define HAVE_AM33 (info->mach == AM33 || HAVE_AM33_2)
32 #define HAVE_AM30 (info->mach == AM30)
34 int
35 print_insn_mn10300 (memaddr, info)
36 bfd_vma memaddr;
37 struct disassemble_info *info;
39 int status;
40 bfd_byte buffer[4];
41 unsigned long insn;
42 unsigned int consume;
44 /* First figure out how big the opcode is. */
45 status = (*info->read_memory_func) (memaddr, buffer, 1, info);
46 if (status != 0)
48 (*info->memory_error_func) (status, memaddr, info);
49 return -1;
51 insn = *(unsigned char *) buffer;
53 /* These are one byte insns. */
54 if ((insn & 0xf3) == 0x00
55 || (insn & 0xf0) == 0x10
56 || (insn & 0xfc) == 0x3c
57 || (insn & 0xf3) == 0x41
58 || (insn & 0xf3) == 0x40
59 || (insn & 0xfc) == 0x50
60 || (insn & 0xfc) == 0x54
61 || (insn & 0xf0) == 0x60
62 || (insn & 0xf0) == 0x70
63 || ((insn & 0xf0) == 0x80
64 && (insn & 0x0c) >> 2 != (insn & 0x03))
65 || ((insn & 0xf0) == 0x90
66 && (insn & 0x0c) >> 2 != (insn & 0x03))
67 || ((insn & 0xf0) == 0xa0
68 && (insn & 0x0c) >> 2 != (insn & 0x03))
69 || ((insn & 0xf0) == 0xb0
70 && (insn & 0x0c) >> 2 != (insn & 0x03))
71 || (insn & 0xff) == 0xcb
72 || (insn & 0xfc) == 0xd0
73 || (insn & 0xfc) == 0xd4
74 || (insn & 0xfc) == 0xd8
75 || (insn & 0xf0) == 0xe0
76 || (insn & 0xff) == 0xff)
78 consume = 1;
81 /* These are two byte insns. */
82 else if ((insn & 0xf0) == 0x80
83 || (insn & 0xf0) == 0x90
84 || (insn & 0xf0) == 0xa0
85 || (insn & 0xf0) == 0xb0
86 || (insn & 0xfc) == 0x20
87 || (insn & 0xfc) == 0x28
88 || (insn & 0xf3) == 0x43
89 || (insn & 0xf3) == 0x42
90 || (insn & 0xfc) == 0x58
91 || (insn & 0xfc) == 0x5c
92 || ((insn & 0xf0) == 0xc0
93 && (insn & 0xff) != 0xcb
94 && (insn & 0xff) != 0xcc
95 && (insn & 0xff) != 0xcd)
96 || (insn & 0xff) == 0xf0
97 || (insn & 0xff) == 0xf1
98 || (insn & 0xff) == 0xf2
99 || (insn & 0xff) == 0xf3
100 || (insn & 0xff) == 0xf4
101 || (insn & 0xff) == 0xf5
102 || (insn & 0xff) == 0xf6)
104 status = (*info->read_memory_func) (memaddr, buffer, 2, info);
105 if (status != 0)
107 (*info->memory_error_func) (status, memaddr, info);
108 return -1;
110 insn = bfd_getb16 (buffer);
111 consume = 2;
114 /* These are three byte insns. */
115 else if ((insn & 0xff) == 0xf8
116 || (insn & 0xff) == 0xcc
117 || (insn & 0xff) == 0xf9
118 || (insn & 0xf3) == 0x01
119 || (insn & 0xf3) == 0x02
120 || (insn & 0xf3) == 0x03
121 || (insn & 0xfc) == 0x24
122 || (insn & 0xfc) == 0x2c
123 || (insn & 0xfc) == 0x30
124 || (insn & 0xfc) == 0x34
125 || (insn & 0xfc) == 0x38
126 || (insn & 0xff) == 0xde
127 || (insn & 0xff) == 0xdf
128 || (insn & 0xff) == 0xf9
129 || (insn & 0xff) == 0xcc)
131 status = (*info->read_memory_func) (memaddr, buffer, 2, info);
132 if (status != 0)
134 (*info->memory_error_func) (status, memaddr, info);
135 return -1;
137 insn = bfd_getb16 (buffer);
138 insn <<= 8;
139 status = (*info->read_memory_func) (memaddr + 2, buffer, 1, info);
140 if (status != 0)
142 (*info->memory_error_func) (status, memaddr, info);
143 return -1;
145 insn |= *(unsigned char *) buffer;
146 consume = 3;
149 /* These are four byte insns. */
150 else if ((insn & 0xff) == 0xfa
151 || (insn & 0xff) == 0xf7
152 || (insn & 0xff) == 0xfb)
154 status = (*info->read_memory_func) (memaddr, buffer, 4, info);
155 if (status != 0)
157 (*info->memory_error_func) (status, memaddr, info);
158 return -1;
160 insn = bfd_getb32 (buffer);
161 consume = 4;
164 /* These are five byte insns. */
165 else if ((insn & 0xff) == 0xcd
166 || (insn & 0xff) == 0xdc)
168 status = (*info->read_memory_func) (memaddr, buffer, 4, info);
169 if (status != 0)
171 (*info->memory_error_func) (status, memaddr, info);
172 return -1;
174 insn = bfd_getb32 (buffer);
175 consume = 5;
178 /* These are six byte insns. */
179 else if ((insn & 0xff) == 0xfd
180 || (insn & 0xff) == 0xfc)
182 status = (*info->read_memory_func) (memaddr, buffer, 4, info);
183 if (status != 0)
185 (*info->memory_error_func) (status, memaddr, info);
186 return -1;
189 insn = bfd_getb32 (buffer);
190 consume = 6;
193 /* Else its a seven byte insns (in theory). */
194 else
196 status = (*info->read_memory_func) (memaddr, buffer, 4, info);
197 if (status != 0)
199 (*info->memory_error_func) (status, memaddr, info);
200 return -1;
203 insn = bfd_getb32 (buffer);
204 consume = 7;
205 /* Handle the 5-byte extended instruction codes. */
206 if ((insn & 0xfff80000) == 0xfe800000)
207 consume = 5;
210 disassemble (memaddr, info, insn, consume);
212 return consume;
215 static void
216 disassemble (memaddr, info, insn, size)
217 bfd_vma memaddr;
218 struct disassemble_info *info;
219 unsigned long insn;
220 unsigned int size;
222 struct mn10300_opcode *op = (struct mn10300_opcode *)mn10300_opcodes;
223 const struct mn10300_operand *operand;
224 bfd_byte buffer[4];
225 unsigned long extension = 0;
226 int status, match = 0;
228 /* Find the opcode. */
229 while (op->name)
231 int mysize, extra_shift;
233 if (op->format == FMT_S0)
234 mysize = 1;
235 else if (op->format == FMT_S1
236 || op->format == FMT_D0)
237 mysize = 2;
238 else if (op->format == FMT_S2
239 || op->format == FMT_D1)
240 mysize = 3;
241 else if (op->format == FMT_S4)
242 mysize = 5;
243 else if (op->format == FMT_D2)
244 mysize = 4;
245 else if (op->format == FMT_D3)
246 mysize = 5;
247 else if (op->format == FMT_D4)
248 mysize = 6;
249 else if (op->format == FMT_D6)
250 mysize = 3;
251 else if (op->format == FMT_D7 || op->format == FMT_D10)
252 mysize = 4;
253 else if (op->format == FMT_D8)
254 mysize = 6;
255 else if (op->format == FMT_D9)
256 mysize = 7;
257 else
258 mysize = 7;
260 if ((op->mask & insn) == op->opcode
261 && size == (unsigned int) mysize
262 && (op->machine == 0
263 || (op->machine == AM33_2 && HAVE_AM33_2)
264 || (op->machine == AM33 && HAVE_AM33)
265 || (op->machine == AM30 && HAVE_AM30)))
267 const unsigned char *opindex_ptr;
268 unsigned int nocomma;
269 int paren = 0;
271 if (op->format == FMT_D1 || op->format == FMT_S1)
272 extra_shift = 8;
273 else if (op->format == FMT_D2 || op->format == FMT_D4
274 || op->format == FMT_S2 || op->format == FMT_S4
275 || op->format == FMT_S6 || op->format == FMT_D5)
276 extra_shift = 16;
277 else if (op->format == FMT_D7
278 || op->format == FMT_D8
279 || op->format == FMT_D9)
280 extra_shift = 8;
281 else
282 extra_shift = 0;
284 if (size == 1 || size == 2)
286 extension = 0;
288 else if (size == 3
289 && (op->format == FMT_D1
290 || op->opcode == 0xdf0000
291 || op->opcode == 0xde0000))
293 extension = 0;
295 else if (size == 3
296 && op->format == FMT_D6)
298 extension = 0;
300 else if (size == 3)
302 insn &= 0xff0000;
303 status = (*info->read_memory_func) (memaddr + 1, buffer, 2, info);
304 if (status != 0)
306 (*info->memory_error_func) (status, memaddr, info);
307 return;
310 insn |= bfd_getl16 (buffer);
311 extension = 0;
313 else if (size == 4
314 && (op->opcode == 0xfaf80000
315 || op->opcode == 0xfaf00000
316 || op->opcode == 0xfaf40000))
318 extension = 0;
320 else if (size == 4
321 && (op->format == FMT_D7
322 || op->format == FMT_D10))
324 extension = 0;
326 else if (size == 4)
328 insn &= 0xffff0000;
329 status = (*info->read_memory_func) (memaddr + 2, buffer, 2, info);
330 if (status != 0)
332 (*info->memory_error_func) (status, memaddr, info);
333 return;
336 insn |= bfd_getl16 (buffer);
337 extension = 0;
339 else if (size == 5 && op->opcode == 0xdc000000)
341 unsigned long temp = 0;
342 status = (*info->read_memory_func) (memaddr + 1, buffer, 4, info);
343 if (status != 0)
345 (*info->memory_error_func) (status, memaddr, info);
346 return;
348 temp |= bfd_getl32 (buffer);
350 insn &= 0xff000000;
351 insn |= (temp & 0xffffff00) >> 8;
352 extension = temp & 0xff;
354 else if (size == 5 && op->format == FMT_D3)
356 status = (*info->read_memory_func) (memaddr + 2, buffer, 2, info);
357 if (status != 0)
359 (*info->memory_error_func) (status, memaddr, info);
360 return;
362 insn &= 0xffff0000;
363 insn |= bfd_getl16 (buffer);
365 status = (*info->read_memory_func) (memaddr + 4, buffer, 1, info);
366 if (status != 0)
368 (*info->memory_error_func) (status, memaddr, info);
369 return;
371 extension = *(unsigned char *) buffer;
373 else if (size == 5)
375 unsigned long temp = 0;
376 status = (*info->read_memory_func) (memaddr + 1, buffer, 2, info);
377 if (status != 0)
379 (*info->memory_error_func) (status, memaddr, info);
380 return;
382 temp |= bfd_getl16 (buffer);
384 insn &= 0xff0000ff;
385 insn |= temp << 8;
387 status = (*info->read_memory_func) (memaddr + 4, buffer, 1, info);
388 if (status != 0)
390 (*info->memory_error_func) (status, memaddr, info);
391 return;
393 extension = *(unsigned char *) buffer;
395 else if (size == 6 && op->format == FMT_D8)
397 insn &= 0xffffff00;
398 status = (*info->read_memory_func) (memaddr + 5, buffer, 1, info);
399 if (status != 0)
401 (*info->memory_error_func) (status, memaddr, info);
402 return;
404 insn |= *(unsigned char *) buffer;
406 status = (*info->read_memory_func) (memaddr + 3, buffer, 2, info);
407 if (status != 0)
409 (*info->memory_error_func) (status, memaddr, info);
410 return;
412 extension = bfd_getl16 (buffer);
414 else if (size == 6)
416 unsigned long temp = 0;
417 status = (*info->read_memory_func) (memaddr + 2, buffer, 4, info);
418 if (status != 0)
420 (*info->memory_error_func) (status, memaddr, info);
421 return;
423 temp |= bfd_getl32 (buffer);
425 insn &= 0xffff0000;
426 insn |= (temp >> 16) & 0xffff;
427 extension = temp & 0xffff;
429 else if (size == 7 && op->format == FMT_D9)
431 insn &= 0xffffff00;
432 status = (*info->read_memory_func) (memaddr + 3, buffer, 4, info);
433 if (status != 0)
435 (*info->memory_error_func) (status, memaddr, info);
436 return;
438 extension = bfd_getl32 (buffer);
439 insn |= (extension & 0xff000000) >> 24;
440 extension &= 0xffffff;
442 else if (size == 7 && op->opcode == 0xdd000000)
444 unsigned long temp = 0;
445 status = (*info->read_memory_func) (memaddr + 1, buffer, 4, info);
446 if (status != 0)
448 (*info->memory_error_func) (status, memaddr, info);
449 return;
451 temp |= bfd_getl32 (buffer);
453 insn &= 0xff000000;
454 insn |= (temp >> 8) & 0xffffff;
455 extension = (temp & 0xff) << 16;
457 status = (*info->read_memory_func) (memaddr + 5, buffer, 2, info);
458 if (status != 0)
460 (*info->memory_error_func) (status, memaddr, info);
461 return;
463 extension |= bfd_getb16 (buffer);
465 else if (size == 7)
467 unsigned long temp = 0;
468 status = (*info->read_memory_func) (memaddr + 2, buffer, 4, info);
469 if (status != 0)
471 (*info->memory_error_func) (status, memaddr, info);
472 return;
474 temp |= bfd_getl32 (buffer);
476 insn &= 0xffff0000;
477 insn |= (temp >> 16) & 0xffff;
478 extension = (temp & 0xffff) << 8;
480 status = (*info->read_memory_func) (memaddr + 6, buffer, 1, info);
481 if (status != 0)
483 (*info->memory_error_func) (status, memaddr, info);
484 return;
486 extension |= *(unsigned char *) buffer;
489 match = 1;
490 (*info->fprintf_func) (info->stream, "%s\t", op->name);
492 /* Now print the operands. */
493 for (opindex_ptr = op->operands, nocomma = 1;
494 *opindex_ptr != 0;
495 opindex_ptr++)
497 unsigned long value;
499 operand = &mn10300_operands[*opindex_ptr];
501 /* If this operand is a PLUS (autoincrement), then do not emit
502 a comma before emitting the plus. */
503 if ((operand->flags & MN10300_OPERAND_PLUS) != 0)
504 nocomma = 1;
506 if ((operand->flags & MN10300_OPERAND_SPLIT) != 0)
508 unsigned long temp;
509 value = insn & ((1 << operand->bits) - 1);
510 value <<= (32 - operand->bits);
511 temp = extension >> operand->shift;
512 temp &= ((1 << (32 - operand->bits)) - 1);
513 value |= temp;
514 value = ((value ^ (((unsigned long) 1) << 31))
515 - (((unsigned long) 1) << 31));
517 else if ((operand->flags & MN10300_OPERAND_24BIT) != 0)
519 unsigned long temp;
520 value = insn & ((1 << operand->bits) - 1);
521 value <<= (24 - operand->bits);
522 temp = extension >> operand->shift;
523 temp &= ((1 << (24 - operand->bits)) - 1);
524 value |= temp;
525 if ((operand->flags & MN10300_OPERAND_SIGNED) != 0)
526 value = ((value & 0xffffff) ^ 0x800000) - 0x800000;
528 else if ((operand->flags & (MN10300_OPERAND_FSREG
529 | MN10300_OPERAND_FDREG)))
531 /* See m10300-opc.c just before #define FSM0 for an
532 explanation of these variables. Note that
533 FMT-implied shifts are not taken into account for
534 FP registers. */
535 unsigned long mask_low, mask_high;
536 int shl_low, shr_high, shl_high;
538 switch (operand->bits)
540 case 5:
541 /* Handle regular FP registers. */
542 if (operand->shift >= 0)
544 /* This is an `m' register. */
545 shl_low = operand->shift;
546 shl_high = 8 + (8 & shl_low) + (shl_low & 4) / 4;
548 else
550 /* This is an `n' register. */
551 shl_low = -operand->shift;
552 shl_high = shl_low / 4;
554 mask_low = 0x0f;
555 mask_high = 0x10;
556 shr_high = 4;
557 break;
559 case 3:
560 /* Handle accumulators. */
561 shl_low = -operand->shift;
562 shl_high = 0;
563 mask_low = 0x03;
564 mask_high = 0x04;
565 shr_high = 2;
566 break;
568 default:
569 abort ();
571 value = ((((insn >> shl_high) << shr_high) & mask_high)
572 | ((insn >> shl_low) & mask_low));
574 else if ((operand->flags & MN10300_OPERAND_EXTENDED) != 0)
576 value = ((extension >> (operand->shift))
577 & ((1 << operand->bits) - 1));
579 else
581 value = ((insn >> (operand->shift))
582 & ((1 << operand->bits) - 1));
585 if ((operand->flags & MN10300_OPERAND_SIGNED) != 0
586 /* These are properly extended by the code above. */
587 && ((operand->flags & MN10300_OPERAND_24BIT) == 0))
588 value = ((value ^ (((unsigned long) 1) << (operand->bits - 1)))
589 - (((unsigned long) 1) << (operand->bits - 1)));
591 if (!nocomma
592 && (!paren
593 || ((operand->flags & MN10300_OPERAND_PAREN) == 0)))
594 (*info->fprintf_func) (info->stream, ",");
596 nocomma = 0;
598 if ((operand->flags & MN10300_OPERAND_DREG) != 0)
600 value = ((insn >> (operand->shift + extra_shift))
601 & ((1 << operand->bits) - 1));
602 (*info->fprintf_func) (info->stream, "d%d", (int) value);
605 else if ((operand->flags & MN10300_OPERAND_AREG) != 0)
607 value = ((insn >> (operand->shift + extra_shift))
608 & ((1 << operand->bits) - 1));
609 (*info->fprintf_func) (info->stream, "a%d", (int) value);
612 else if ((operand->flags & MN10300_OPERAND_SP) != 0)
613 (*info->fprintf_func) (info->stream, "sp");
615 else if ((operand->flags & MN10300_OPERAND_PSW) != 0)
616 (*info->fprintf_func) (info->stream, "psw");
618 else if ((operand->flags & MN10300_OPERAND_MDR) != 0)
619 (*info->fprintf_func) (info->stream, "mdr");
621 else if ((operand->flags & MN10300_OPERAND_RREG) != 0)
623 value = ((insn >> (operand->shift + extra_shift))
624 & ((1 << operand->bits) - 1));
625 if (value < 8)
626 (*info->fprintf_func) (info->stream, "r%d", (int) value);
627 else if (value < 12)
628 (*info->fprintf_func) (info->stream, "a%d", (int) value - 8);
629 else
630 (*info->fprintf_func) (info->stream, "d%d", (int) value - 12);
633 else if ((operand->flags & MN10300_OPERAND_XRREG) != 0)
635 value = ((insn >> (operand->shift + extra_shift))
636 & ((1 << operand->bits) - 1));
637 if (value == 0)
638 (*info->fprintf_func) (info->stream, "sp", value);
639 else
640 (*info->fprintf_func) (info->stream, "xr%d", (int) value);
643 else if ((operand->flags & MN10300_OPERAND_FSREG) != 0)
644 (*info->fprintf_func) (info->stream, "fs%d", (int) value);
646 else if ((operand->flags & MN10300_OPERAND_FDREG) != 0)
647 (*info->fprintf_func) (info->stream, "fd%d", (int) value);
649 else if ((operand->flags & MN10300_OPERAND_FPCR) != 0)
650 (*info->fprintf_func) (info->stream, "fpcr");
652 else if ((operand->flags & MN10300_OPERAND_USP) != 0)
653 (*info->fprintf_func) (info->stream, "usp");
655 else if ((operand->flags & MN10300_OPERAND_SSP) != 0)
656 (*info->fprintf_func) (info->stream, "ssp");
658 else if ((operand->flags & MN10300_OPERAND_MSP) != 0)
659 (*info->fprintf_func) (info->stream, "msp");
661 else if ((operand->flags & MN10300_OPERAND_PC) != 0)
662 (*info->fprintf_func) (info->stream, "pc");
664 else if ((operand->flags & MN10300_OPERAND_EPSW) != 0)
665 (*info->fprintf_func) (info->stream, "epsw");
667 else if ((operand->flags & MN10300_OPERAND_PLUS) != 0)
668 (*info->fprintf_func) (info->stream, "+");
670 else if ((operand->flags & MN10300_OPERAND_PAREN) != 0)
672 if (paren)
673 (*info->fprintf_func) (info->stream, ")");
674 else
676 (*info->fprintf_func) (info->stream, "(");
677 nocomma = 1;
679 paren = !paren;
682 else if ((operand->flags & MN10300_OPERAND_PCREL) != 0)
683 (*info->print_address_func) ((long) value + memaddr, info);
685 else if ((operand->flags & MN10300_OPERAND_MEMADDR) != 0)
686 (*info->print_address_func) (value, info);
688 else if ((operand->flags & MN10300_OPERAND_REG_LIST) != 0)
690 int comma = 0;
692 (*info->fprintf_func) (info->stream, "[");
693 if (value & 0x80)
695 (*info->fprintf_func) (info->stream, "d2");
696 comma = 1;
699 if (value & 0x40)
701 if (comma)
702 (*info->fprintf_func) (info->stream, ",");
703 (*info->fprintf_func) (info->stream, "d3");
704 comma = 1;
707 if (value & 0x20)
709 if (comma)
710 (*info->fprintf_func) (info->stream, ",");
711 (*info->fprintf_func) (info->stream, "a2");
712 comma = 1;
715 if (value & 0x10)
717 if (comma)
718 (*info->fprintf_func) (info->stream, ",");
719 (*info->fprintf_func) (info->stream, "a3");
720 comma = 1;
723 if (value & 0x08)
725 if (comma)
726 (*info->fprintf_func) (info->stream, ",");
727 (*info->fprintf_func) (info->stream, "other");
728 comma = 1;
731 if (value & 0x04)
733 if (comma)
734 (*info->fprintf_func) (info->stream, ",");
735 (*info->fprintf_func) (info->stream, "exreg0");
736 comma = 1;
738 if (value & 0x02)
740 if (comma)
741 (*info->fprintf_func) (info->stream, ",");
742 (*info->fprintf_func) (info->stream, "exreg1");
743 comma = 1;
745 if (value & 0x01)
747 if (comma)
748 (*info->fprintf_func) (info->stream, ",");
749 (*info->fprintf_func) (info->stream, "exother");
750 comma = 1;
752 (*info->fprintf_func) (info->stream, "]");
755 else
756 (*info->fprintf_func) (info->stream, "%ld", (long) value);
758 /* All done. */
759 break;
761 op++;
764 if (!match)
766 /* xgettext:c-format */
767 (*info->fprintf_func) (info->stream, _("unknown\t0x%04x"), insn);