Initial revision
[binutils.git] / opcodes / arm-dis.c
blob4aabf7286bcc760212b987d765ec060f1698f075
1 /* Instruction printing code for the ARM
2 Copyright (C) 1994, 95, 96, 97, 1998 Free Software Foundation, Inc.
3 Contributed by Richard Earnshaw (rwe@pegasus.esprit.ec.org)
4 Modification by James G. Smith (jsmith@cygnus.co.uk)
6 This file is part of libopcodes.
8 This program is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2 of the License, or (at your option)
11 any later version.
13 This program is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 #include "dis-asm.h"
23 #define DEFINE_TABLE
24 #include "arm-opc.h"
25 #include "coff/internal.h"
26 #include "libcoff.h"
27 #include "opintl.h"
29 /* FIXME: This shouldn't be done here */
30 #include "elf-bfd.h"
31 #include "elf/internal.h"
32 #include "elf/arm.h"
34 static char *arm_conditional[] =
35 {"eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
36 "hi", "ls", "ge", "lt", "gt", "le", "", "nv"};
38 static char *arm_regnames[] =
39 {"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
40 "r8", "r9", "sl", "fp", "ip", "sp", "lr", "pc"};
42 static char *arm_fp_const[] =
43 {"0.0", "1.0", "2.0", "3.0", "4.0", "5.0", "0.5", "10.0"};
45 static char *arm_shift[] =
46 {"lsl", "lsr", "asr", "ror"};
48 static int print_insn_arm PARAMS ((bfd_vma, struct disassemble_info *,
49 long));
51 static void
52 arm_decode_shift (given, func, stream)
53 long given;
54 fprintf_ftype func;
55 void *stream;
57 func (stream, "%s", arm_regnames[given & 0xf]);
58 if ((given & 0xff0) != 0)
60 if ((given & 0x10) == 0)
62 int amount = (given & 0xf80) >> 7;
63 int shift = (given & 0x60) >> 5;
64 if (amount == 0)
66 if (shift == 3)
68 func (stream, ", rrx");
69 return;
71 amount = 32;
73 func (stream, ", %s #%d", arm_shift[shift], amount);
75 else
76 func (stream, ", %s %s", arm_shift[(given & 0x60) >> 5],
77 arm_regnames[(given & 0xf00) >> 8]);
81 /* Print one instruction from PC on INFO->STREAM.
82 Return the size of the instruction (always 4 on ARM). */
84 static int
85 print_insn_arm (pc, info, given)
86 bfd_vma pc;
87 struct disassemble_info *info;
88 long given;
90 struct arm_opcode * insn;
91 void * stream = info->stream;
92 fprintf_ftype func = info->fprintf_func;
94 for (insn = arm_opcodes; insn->assembler; insn++)
96 if ((given & insn->mask) == insn->value)
98 char * c;
100 for (c = insn->assembler; *c; c++)
102 if (*c == '%')
104 switch (*++c)
106 case '%':
107 func (stream, "%%");
108 break;
110 case 'a':
111 if (((given & 0x000f0000) == 0x000f0000)
112 && ((given & 0x02000000) == 0))
114 int offset = given & 0xfff;
116 func (stream, "[pc");
118 if (given & 0x01000000)
120 if ((given & 0x00800000) == 0)
121 offset = - offset;
123 /* pre-indexed */
124 func (stream, ", #%x]", offset);
126 offset += pc + 8;
128 /* Cope with the possibility of write-back being used.
129 Probably a very dangerous thing for the programmer
130 to do, but who are we to argue ? */
131 if (given & 0x00200000)
132 func (stream, "!");
134 else
136 /* post indexed */
137 func (stream, "], #%x", offset);
139 offset = pc + 8; /* ie ignore the offset */
142 func (stream, "\t; ");
143 info->print_address_func (offset, info);
145 else
147 func (stream, "[%s",
148 arm_regnames[(given >> 16) & 0xf]);
149 if ((given & 0x01000000) != 0)
151 if ((given & 0x02000000) == 0)
153 int offset = given & 0xfff;
154 if (offset)
155 func (stream, ", %s#%d",
156 (((given & 0x00800000) == 0)
157 ? "-" : ""), offset);
159 else
161 func (stream, ", %s",
162 (((given & 0x00800000) == 0)
163 ? "-" : ""));
164 arm_decode_shift (given, func, stream);
167 func (stream, "]%s",
168 ((given & 0x00200000) != 0) ? "!" : "");
170 else
172 if ((given & 0x02000000) == 0)
174 int offset = given & 0xfff;
175 if (offset)
176 func (stream, "], %s#%d",
177 (((given & 0x00800000) == 0)
178 ? "-" : ""), offset);
179 else
180 func (stream, "]");
182 else
184 func (stream, "], %s",
185 (((given & 0x00800000) == 0)
186 ? "-" : ""));
187 arm_decode_shift (given, func, stream);
191 break;
193 case 's':
194 if ((given & 0x004f0000) == 0x004f0000)
196 /* PC relative with immediate offset */
197 int offset = ((given & 0xf00) >> 4) | (given & 0xf);
198 if ((given & 0x00800000) == 0)
199 offset = -offset;
200 (*info->print_address_func)
201 (offset + pc + 8, info);
203 else
205 func (stream, "[%s",
206 arm_regnames[(given >> 16) & 0xf]);
207 if ((given & 0x01000000) != 0)
209 /* pre-indexed */
210 if ((given & 0x00400000) == 0x00400000)
212 /* immediate */
213 int offset = ((given & 0xf00) >> 4) | (given & 0xf);
214 if (offset)
215 func (stream, ", %s#%d",
216 (((given & 0x00800000) == 0)
217 ? "-" : ""), offset);
219 else
221 /* register */
222 func (stream, ", %s%s",
223 (((given & 0x00800000) == 0)
224 ? "-" : ""),
225 arm_regnames[given & 0xf]);
228 func (stream, "]%s",
229 ((given & 0x00200000) != 0) ? "!" : "");
231 else
233 /* post-indexed */
234 if ((given & 0x00400000) == 0x00400000)
236 /* immediate */
237 int offset = ((given & 0xf00) >> 4) | (given & 0xf);
238 if (offset)
239 func (stream, "], %s#%d",
240 (((given & 0x00800000) == 0)
241 ? "-" : ""), offset);
242 else
243 func (stream, "]");
245 else
247 /* register */
248 func (stream, "], %s%s",
249 (((given & 0x00800000) == 0)
250 ? "-" : ""),
251 arm_regnames[given & 0xf]);
255 break;
257 case 'b':
258 (*info->print_address_func)
259 (BDISP (given) * 4 + pc + 8, info);
260 break;
262 case 'c':
263 func (stream, "%s",
264 arm_conditional [(given >> 28) & 0xf]);
265 break;
267 case 'm':
269 int started = 0;
270 int reg;
272 func (stream, "{");
273 for (reg = 0; reg < 16; reg++)
274 if ((given & (1 << reg)) != 0)
276 if (started)
277 func (stream, ", ");
278 started = 1;
279 func (stream, "%s", arm_regnames[reg]);
281 func (stream, "}");
283 break;
285 case 'o':
286 if ((given & 0x02000000) != 0)
288 int rotate = (given & 0xf00) >> 7;
289 int immed = (given & 0xff);
290 func (stream, "#%d",
291 ((immed << (32 - rotate))
292 | (immed >> rotate)) & 0xffffffff);
294 else
295 arm_decode_shift (given, func, stream);
296 break;
298 case 'p':
299 if ((given & 0x0000f000) == 0x0000f000)
300 func (stream, "p");
301 break;
303 case 't':
304 if ((given & 0x01200000) == 0x00200000)
305 func (stream, "t");
306 break;
308 case 'h':
309 if ((given & 0x00000020) == 0x00000020)
310 func (stream, "h");
311 else
312 func (stream, "b");
313 break;
315 case 'A':
316 func (stream, "[%s", arm_regnames [(given >> 16) & 0xf]);
317 if ((given & 0x01000000) != 0)
319 int offset = given & 0xff;
320 if (offset)
321 func (stream, ", %s#%d]%s",
322 ((given & 0x00800000) == 0 ? "-" : ""),
323 offset * 4,
324 ((given & 0x00200000) != 0 ? "!" : ""));
325 else
326 func (stream, "]");
328 else
330 int offset = given & 0xff;
331 if (offset)
332 func (stream, "], %s#%d",
333 ((given & 0x00800000) == 0 ? "-" : ""),
334 offset * 4);
335 else
336 func (stream, "]");
338 break;
340 case 'C':
341 switch (given & 0x00090000)
343 default:
344 func (stream, "_???");
345 break;
346 case 0x90000:
347 func (stream, "_all");
348 break;
349 case 0x10000:
350 func (stream, "_ctl");
351 break;
352 case 0x80000:
353 func (stream, "_flg");
354 break;
356 break;
358 case 'F':
359 switch (given & 0x00408000)
361 case 0:
362 func (stream, "4");
363 break;
364 case 0x8000:
365 func (stream, "1");
366 break;
367 case 0x00400000:
368 func (stream, "2");
369 break;
370 default:
371 func (stream, "3");
373 break;
375 case 'P':
376 switch (given & 0x00080080)
378 case 0:
379 func (stream, "s");
380 break;
381 case 0x80:
382 func (stream, "d");
383 break;
384 case 0x00080000:
385 func (stream, "e");
386 break;
387 default:
388 func (stream, _("<illegal precision>"));
389 break;
391 break;
392 case 'Q':
393 switch (given & 0x00408000)
395 case 0:
396 func (stream, "s");
397 break;
398 case 0x8000:
399 func (stream, "d");
400 break;
401 case 0x00400000:
402 func (stream, "e");
403 break;
404 default:
405 func (stream, "p");
406 break;
408 break;
409 case 'R':
410 switch (given & 0x60)
412 case 0:
413 break;
414 case 0x20:
415 func (stream, "p");
416 break;
417 case 0x40:
418 func (stream, "m");
419 break;
420 default:
421 func (stream, "z");
422 break;
424 break;
426 case '0': case '1': case '2': case '3': case '4':
427 case '5': case '6': case '7': case '8': case '9':
429 int bitstart = *c++ - '0';
430 int bitend = 0;
431 while (*c >= '0' && *c <= '9')
432 bitstart = (bitstart * 10) + *c++ - '0';
434 switch (*c)
436 case '-':
437 c++;
438 while (*c >= '0' && *c <= '9')
439 bitend = (bitend * 10) + *c++ - '0';
440 if (!bitend)
441 abort ();
442 switch (*c)
444 case 'r':
446 long reg;
447 reg = given >> bitstart;
448 reg &= (2 << (bitend - bitstart)) - 1;
449 func (stream, "%s", arm_regnames[reg]);
451 break;
452 case 'd':
454 long reg;
455 reg = given >> bitstart;
456 reg &= (2 << (bitend - bitstart)) - 1;
457 func (stream, "%d", reg);
459 break;
460 case 'x':
462 long reg;
463 reg = given >> bitstart;
464 reg &= (2 << (bitend - bitstart)) - 1;
465 func (stream, "0x%08x", reg);
467 break;
468 case 'f':
470 long reg;
471 reg = given >> bitstart;
472 reg &= (2 << (bitend - bitstart)) - 1;
473 if (reg > 7)
474 func (stream, "#%s",
475 arm_fp_const[reg & 7]);
476 else
477 func (stream, "f%d", reg);
479 break;
480 default:
481 abort ();
483 break;
484 case '`':
485 c++;
486 if ((given & (1 << bitstart)) == 0)
487 func (stream, "%c", *c);
488 break;
489 case '\'':
490 c++;
491 if ((given & (1 << bitstart)) != 0)
492 func (stream, "%c", *c);
493 break;
494 case '?':
495 ++c;
496 if ((given & (1 << bitstart)) != 0)
497 func (stream, "%c", *c++);
498 else
499 func (stream, "%c", *++c);
500 break;
501 default:
502 abort ();
504 break;
506 default:
507 abort ();
511 else
512 func (stream, "%c", *c);
514 return 4;
517 abort ();
520 /* Print one instruction from PC on INFO->STREAM.
521 Return the size of the instruction. */
523 static int
524 print_insn_thumb (pc, info, given)
525 bfd_vma pc;
526 struct disassemble_info *info;
527 long given;
529 struct thumb_opcode *insn;
530 void *stream = info->stream;
531 fprintf_ftype func = info->fprintf_func;
533 for (insn = thumb_opcodes; insn->assembler; insn++)
535 if ((given & insn->mask) == insn->value)
537 char *c = insn->assembler;
539 /* Special processing for Thumb 2 instruction BL sequence: */
540 if (!*c) /* check for empty (not NULL) assembler string */
542 info->bytes_per_chunk = 4;
543 info->bytes_per_line = 4;
545 func (stream, "%04x\tbl\t", given & 0xffff);
546 (*info->print_address_func)
547 (BDISP23 (given) * 2 + pc + 4, info);
548 return 4;
550 else
552 info->bytes_per_chunk = 2;
553 info->bytes_per_line = 4;
555 given &= 0xffff;
556 func (stream, "%04x\t", given);
557 for (; *c; c++)
559 if (*c == '%')
561 int domaskpc = 0;
562 int domasklr = 0;
563 switch (*++c)
565 case '%':
566 func (stream, "%%");
567 break;
569 case 'S':
571 long reg;
572 reg = (given >> 3) & 0x7;
573 if (given & (1 << 6))
574 reg += 8;
575 func (stream, "%s", arm_regnames[reg]);
577 break;
579 case 'D':
581 long reg;
582 reg = given & 0x7;
583 if (given & (1 << 7))
584 reg += 8;
585 func (stream, "%s", arm_regnames[reg]);
587 break;
589 case 'T':
590 func (stream, "%s",
591 arm_conditional [(given >> 8) & 0xf]);
592 break;
594 case 'N':
595 if (given & (1 << 8))
596 domasklr = 1;
597 /* fall through */
598 case 'O':
599 if (*c == 'O' && (given & (1 << 8)))
600 domaskpc = 1;
601 /* fall through */
602 case 'M':
604 int started = 0;
605 int reg;
606 func (stream, "{");
607 /* It would be nice if we could spot
608 ranges, and generate the rS-rE format: */
609 for (reg = 0; (reg < 8); reg++)
610 if ((given & (1 << reg)) != 0)
612 if (started)
613 func (stream, ", ");
614 started = 1;
615 func (stream, "%s", arm_regnames[reg]);
618 if (domasklr)
620 if (started)
621 func (stream, ", ");
622 started = 1;
623 func (stream, "lr");
626 if (domaskpc)
628 if (started)
629 func (stream, ", ");
630 func (stream, "pc");
633 func (stream, "}");
635 break;
638 case '0': case '1': case '2': case '3': case '4':
639 case '5': case '6': case '7': case '8': case '9':
641 int bitstart = *c++ - '0';
642 int bitend = 0;
643 while (*c >= '0' && *c <= '9')
644 bitstart = (bitstart * 10) + *c++ - '0';
646 switch (*c)
648 case '-':
650 long reg;
651 c++;
652 while (*c >= '0' && *c <= '9')
653 bitend = (bitend * 10) + *c++ - '0';
654 if (!bitend)
655 abort ();
656 reg = given >> bitstart;
657 reg &= (2 << (bitend - bitstart)) - 1;
658 switch (*c)
660 case 'r':
661 func (stream, "%s", arm_regnames[reg]);
662 break;
664 case 'd':
665 func (stream, "%d", reg);
666 break;
668 case 'H':
669 func (stream, "%d", reg << 1);
670 break;
672 case 'W':
673 func (stream, "%d", reg << 2);
674 break;
676 case 'a':
677 /* PC-relative address -- the bottom two
678 bits of the address are dropped before
679 the calculation. */
680 info->print_address_func
681 (((pc + 4) & ~3) + (reg << 2), info);
682 break;
684 case 'x':
685 func (stream, "0x%04x", reg);
686 break;
688 case 'I':
689 reg = ((reg ^ (1 << bitend)) - (1 << bitend));
690 func (stream, "%d", reg);
691 break;
693 case 'B':
694 reg = ((reg ^ (1 << bitend)) - (1 << bitend));
695 (*info->print_address_func)
696 (reg * 2 + pc + 4, info);
697 break;
699 default:
700 abort();
703 break;
705 case '\'':
706 c++;
707 if ((given & (1 << bitstart)) != 0)
708 func (stream, "%c", *c);
709 break;
711 case '?':
712 ++c;
713 if ((given & (1 << bitstart)) != 0)
714 func (stream, "%c", *c++);
715 else
716 func (stream, "%c", *++c);
717 break;
719 default:
720 abort();
723 break;
725 default:
726 abort ();
729 else
730 func (stream, "%c", *c);
733 return 2;
737 /* no match */
738 abort ();
741 /* NOTE: There are no checks in these routines that the relevant number of data bytes exist */
744 print_insn_big_arm (pc, info)
745 bfd_vma pc;
746 struct disassemble_info *info;
748 unsigned char b[4];
749 long given;
750 int status;
751 coff_symbol_type *cs;
752 elf_symbol_type *es;
753 int is_thumb;
755 is_thumb = false;
756 if (info->symbols != NULL)
758 if (bfd_asymbol_flavour (*info->symbols) == bfd_target_coff_flavour)
760 cs = coffsymbol (*info->symbols);
761 is_thumb = (cs->native->u.syment.n_sclass == C_THUMBEXT
762 || cs->native->u.syment.n_sclass == C_THUMBSTAT
763 || cs->native->u.syment.n_sclass == C_THUMBLABEL
764 || cs->native->u.syment.n_sclass == C_THUMBEXTFUNC
765 || cs->native->u.syment.n_sclass == C_THUMBSTATFUNC);
768 else if (bfd_asymbol_flavour (*info->symbols) == bfd_target_elf_flavour)
770 es = *(elf_symbol_type **)(info->symbols);
771 is_thumb = ELF_ST_TYPE (es->internal_elf_sym.st_info) ==
772 STT_ARM_TFUNC;
776 info->bytes_per_chunk = 4;
777 info->display_endian = BFD_ENDIAN_BIG;
779 /* Always fetch word aligned values. */
781 status = (*info->read_memory_func) (pc & ~ 0x3, (bfd_byte *) &b[0], 4, info);
782 if (status != 0)
784 (*info->memory_error_func) (status, pc, info);
785 return -1;
788 if (is_thumb)
790 if (pc & 0x2)
792 given = (b[2] << 8) | b[3];
794 status = info->read_memory_func ((pc + 4) & ~ 0x3, (bfd_byte *) b, 4, info);
795 if (status != 0)
797 info->memory_error_func (status, pc + 4, info);
798 return -1;
801 given |= (b[0] << 24) | (b[1] << 16);
803 else
805 given = (b[0] << 8) | b[1] | (b[2] << 24) | (b[3] << 16);
808 else
810 given = (b[0] << 24) | (b[1] << 16) | (b[2] << 8) | (b[3]);
813 if (is_thumb)
815 status = print_insn_thumb (pc, info, given);
817 else
819 status = print_insn_arm (pc, info, given);
822 return status;
826 print_insn_little_arm (pc, info)
827 bfd_vma pc;
828 struct disassemble_info * info;
830 unsigned char b[4];
831 long given;
832 int status;
833 coff_symbol_type *cs;
834 elf_symbol_type *es;
835 int is_thumb;
837 is_thumb = false;
838 if (info->symbols != NULL)
840 if (bfd_asymbol_flavour (*info->symbols) == bfd_target_coff_flavour)
842 cs = coffsymbol (*info->symbols);
843 is_thumb = (cs->native->u.syment.n_sclass == C_THUMBEXT
844 || cs->native->u.syment.n_sclass == C_THUMBSTAT
845 || cs->native->u.syment.n_sclass == C_THUMBLABEL
846 || cs->native->u.syment.n_sclass == C_THUMBEXTFUNC
847 || cs->native->u.syment.n_sclass == C_THUMBSTATFUNC);
850 else if (bfd_asymbol_flavour (*info->symbols) == bfd_target_elf_flavour)
852 es = *(elf_symbol_type **)(info->symbols);
853 is_thumb = ELF_ST_TYPE (es->internal_elf_sym.st_info) ==
854 STT_ARM_TFUNC;
858 info->bytes_per_chunk = 4;
859 info->display_endian = BFD_ENDIAN_LITTLE;
861 status = (*info->read_memory_func) (pc, (bfd_byte *) &b[0], 4, info);
862 if (status != 0 && is_thumb)
864 info->bytes_per_chunk = 2;
866 status = info->read_memory_func (pc, (bfd_byte *) b, 2, info);
867 b[3] = b[2] = 0;
869 if (status != 0)
871 (*info->memory_error_func) (status, pc, info);
872 return -1;
875 given = (b[0]) | (b[1] << 8) | (b[2] << 16) | (b[3] << 24);
877 if (is_thumb)
879 status = print_insn_thumb (pc, info, given);
881 else
883 status = print_insn_arm (pc, info, given);
886 return status;