MIPS16/opcodes: Free up `M' operand code
[external/binutils.git] / opcodes / disassemble.c
1 /* Select disassembly routine for specified architecture.
2    Copyright (C) 1994-2018 Free Software Foundation, Inc.
3
4    This file is part of the GNU opcodes library.
5
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 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
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.  */
20
21 #include "sysdep.h"
22 #include "disassemble.h"
23 #include "safe-ctype.h"
24 #include <assert.h>
25
26 #ifdef ARCH_all
27 #define ARCH_aarch64
28 #define ARCH_alpha
29 #define ARCH_arc
30 #define ARCH_arm
31 #define ARCH_avr
32 #define ARCH_bfin
33 #define ARCH_cr16
34 #define ARCH_cris
35 #define ARCH_crx
36 #define ARCH_d10v
37 #define ARCH_d30v
38 #define ARCH_dlx
39 #define ARCH_epiphany
40 #define ARCH_fr30
41 #define ARCH_frv
42 #define ARCH_ft32
43 #define ARCH_h8300
44 #define ARCH_h8500
45 #define ARCH_hppa
46 #define ARCH_i370
47 #define ARCH_i386
48 #define ARCH_i860
49 #define ARCH_i960
50 #define ARCH_ia64
51 #define ARCH_ip2k
52 #define ARCH_iq2000
53 #define ARCH_lm32
54 #define ARCH_m32c
55 #define ARCH_m32r
56 #define ARCH_m68hc11
57 #define ARCH_m68hc12
58 #define ARCH_m68k
59 #define ARCH_m88k
60 #define ARCH_mcore
61 #define ARCH_mep
62 #define ARCH_metag
63 #define ARCH_microblaze
64 #define ARCH_mips
65 #define ARCH_mmix
66 #define ARCH_mn10200
67 #define ARCH_mn10300
68 #define ARCH_moxie
69 #define ARCH_mt
70 #define ARCH_msp430
71 #define ARCH_nds32
72 #define ARCH_nios2
73 #define ARCH_ns32k
74 #define ARCH_or1k
75 #define ARCH_pdp11
76 #define ARCH_pj
77 #define ARCH_powerpc
78 #define ARCH_pru
79 #define ARCH_rs6000
80 #define ARCH_rl78
81 #define ARCH_rx
82 #define ARCH_s390
83 #define ARCH_score
84 #define ARCH_sh
85 #define ARCH_sparc
86 #define ARCH_spu
87 #define ARCH_tic30
88 #define ARCH_tic4x
89 #define ARCH_tic54x
90 #define ARCH_tic6x
91 #define ARCH_tic80
92 #define ARCH_tilegx
93 #define ARCH_tilepro
94 #define ARCH_v850
95 #define ARCH_vax
96 #define ARCH_visium
97 #define ARCH_w65
98 #define ARCH_wasm32
99 #define ARCH_xstormy16
100 #define ARCH_xc16x
101 #define ARCH_xgate
102 #define ARCH_xtensa
103 #define ARCH_z80
104 #define ARCH_z8k
105 #define INCLUDE_SHMEDIA
106 #endif
107
108 #ifdef ARCH_m32c
109 #include "m32c-desc.h"
110 #endif
111
112 disassembler_ftype
113 disassembler (enum bfd_architecture a,
114               bfd_boolean big ATTRIBUTE_UNUSED,
115               unsigned long mach ATTRIBUTE_UNUSED,
116               bfd *abfd ATTRIBUTE_UNUSED)
117 {
118   disassembler_ftype disassemble;
119
120   switch (a)
121     {
122       /* If you add a case to this table, also add it to the
123          ARCH_all definition right above this function.  */
124 #ifdef ARCH_aarch64
125     case bfd_arch_aarch64:
126       disassemble = print_insn_aarch64;
127       break;
128 #endif
129 #ifdef ARCH_alpha
130     case bfd_arch_alpha:
131       disassemble = print_insn_alpha;
132       break;
133 #endif
134 #ifdef ARCH_arc
135     case bfd_arch_arc:
136       disassemble = arc_get_disassembler (abfd);
137       break;
138 #endif
139 #ifdef ARCH_arm
140     case bfd_arch_arm:
141       if (big)
142         disassemble = print_insn_big_arm;
143       else
144         disassemble = print_insn_little_arm;
145       break;
146 #endif
147 #ifdef ARCH_avr
148     case bfd_arch_avr:
149       disassemble = print_insn_avr;
150       break;
151 #endif
152 #ifdef ARCH_bfin
153     case bfd_arch_bfin:
154       disassemble = print_insn_bfin;
155       break;
156 #endif
157 #ifdef ARCH_cr16
158     case bfd_arch_cr16:
159       disassemble = print_insn_cr16;
160       break;
161 #endif
162 #ifdef ARCH_cris
163     case bfd_arch_cris:
164       disassemble = cris_get_disassembler (abfd);
165       break;
166 #endif
167 #ifdef ARCH_crx
168     case bfd_arch_crx:
169       disassemble = print_insn_crx;
170       break;
171 #endif
172 #ifdef ARCH_d10v
173     case bfd_arch_d10v:
174       disassemble = print_insn_d10v;
175       break;
176 #endif
177 #ifdef ARCH_d30v
178     case bfd_arch_d30v:
179       disassemble = print_insn_d30v;
180       break;
181 #endif
182 #ifdef ARCH_dlx
183     case bfd_arch_dlx:
184       /* As far as I know we only handle big-endian DLX objects.  */
185       disassemble = print_insn_dlx;
186       break;
187 #endif
188 #ifdef ARCH_h8300
189     case bfd_arch_h8300:
190       if (mach == bfd_mach_h8300h || mach == bfd_mach_h8300hn)
191         disassemble = print_insn_h8300h;
192       else if (mach == bfd_mach_h8300s
193                || mach == bfd_mach_h8300sn
194                || mach == bfd_mach_h8300sx
195                || mach == bfd_mach_h8300sxn)
196         disassemble = print_insn_h8300s;
197       else
198         disassemble = print_insn_h8300;
199       break;
200 #endif
201 #ifdef ARCH_h8500
202     case bfd_arch_h8500:
203       disassemble = print_insn_h8500;
204       break;
205 #endif
206 #ifdef ARCH_hppa
207     case bfd_arch_hppa:
208       disassemble = print_insn_hppa;
209       break;
210 #endif
211 #ifdef ARCH_i370
212     case bfd_arch_i370:
213       disassemble = print_insn_i370;
214       break;
215 #endif
216 #ifdef ARCH_i386
217     case bfd_arch_i386:
218     case bfd_arch_iamcu:
219     case bfd_arch_l1om:
220     case bfd_arch_k1om:
221       disassemble = print_insn_i386;
222       break;
223 #endif
224 #ifdef ARCH_i860
225     case bfd_arch_i860:
226       disassemble = print_insn_i860;
227       break;
228 #endif
229 #ifdef ARCH_i960
230     case bfd_arch_i960:
231       disassemble = print_insn_i960;
232       break;
233 #endif
234 #ifdef ARCH_ia64
235     case bfd_arch_ia64:
236       disassemble = print_insn_ia64;
237       break;
238 #endif
239 #ifdef ARCH_ip2k
240     case bfd_arch_ip2k:
241       disassemble = print_insn_ip2k;
242       break;
243 #endif
244 #ifdef ARCH_epiphany
245     case bfd_arch_epiphany:
246       disassemble = print_insn_epiphany;
247       break;
248 #endif
249 #ifdef ARCH_fr30
250     case bfd_arch_fr30:
251       disassemble = print_insn_fr30;
252       break;
253 #endif
254 #ifdef ARCH_lm32
255     case bfd_arch_lm32:
256       disassemble = print_insn_lm32;
257       break;
258 #endif
259 #ifdef ARCH_m32r
260     case bfd_arch_m32r:
261       disassemble = print_insn_m32r;
262       break;
263 #endif
264 #if defined(ARCH_m68hc11) || defined(ARCH_m68hc12) \
265     || defined(ARCH_9s12x) || defined(ARCH_m9s12xg)
266     case bfd_arch_m68hc11:
267       disassemble = print_insn_m68hc11;
268       break;
269     case bfd_arch_m68hc12:
270       disassemble = print_insn_m68hc12;
271       break;
272     case bfd_arch_m9s12x:
273       disassemble = print_insn_m9s12x;
274       break;
275     case bfd_arch_m9s12xg:
276       disassemble = print_insn_m9s12xg;
277       break;
278 #endif
279 #ifdef ARCH_m68k
280     case bfd_arch_m68k:
281       disassemble = print_insn_m68k;
282       break;
283 #endif
284 #ifdef ARCH_m88k
285     case bfd_arch_m88k:
286       disassemble = print_insn_m88k;
287       break;
288 #endif
289 #ifdef ARCH_mt
290     case bfd_arch_mt:
291       disassemble = print_insn_mt;
292       break;
293 #endif
294 #ifdef ARCH_microblaze
295     case bfd_arch_microblaze:
296       disassemble = print_insn_microblaze;
297       break;
298 #endif
299 #ifdef ARCH_msp430
300     case bfd_arch_msp430:
301       disassemble = print_insn_msp430;
302       break;
303 #endif
304 #ifdef ARCH_nds32
305     case bfd_arch_nds32:
306       disassemble = print_insn_nds32;
307       break;
308 #endif
309 #ifdef ARCH_ns32k
310     case bfd_arch_ns32k:
311       disassemble = print_insn_ns32k;
312       break;
313 #endif
314 #ifdef ARCH_mcore
315     case bfd_arch_mcore:
316       disassemble = print_insn_mcore;
317       break;
318 #endif
319 #ifdef ARCH_mep
320     case bfd_arch_mep:
321       disassemble = print_insn_mep;
322       break;
323 #endif
324 #ifdef ARCH_metag
325     case bfd_arch_metag:
326       disassemble = print_insn_metag;
327       break;
328 #endif
329 #ifdef ARCH_mips
330     case bfd_arch_mips:
331       if (big)
332         disassemble = print_insn_big_mips;
333       else
334         disassemble = print_insn_little_mips;
335       break;
336 #endif
337 #ifdef ARCH_mmix
338     case bfd_arch_mmix:
339       disassemble = print_insn_mmix;
340       break;
341 #endif
342 #ifdef ARCH_mn10200
343     case bfd_arch_mn10200:
344       disassemble = print_insn_mn10200;
345       break;
346 #endif
347 #ifdef ARCH_mn10300
348     case bfd_arch_mn10300:
349       disassemble = print_insn_mn10300;
350       break;
351 #endif
352 #ifdef ARCH_nios2
353     case bfd_arch_nios2:
354       if (big)
355         disassemble = print_insn_big_nios2;
356       else
357         disassemble = print_insn_little_nios2;
358       break;
359 #endif
360 #ifdef ARCH_or1k
361     case bfd_arch_or1k:
362       disassemble = print_insn_or1k;
363       break;
364 #endif
365 #ifdef ARCH_pdp11
366     case bfd_arch_pdp11:
367       disassemble = print_insn_pdp11;
368       break;
369 #endif
370 #ifdef ARCH_pj
371     case bfd_arch_pj:
372       disassemble = print_insn_pj;
373       break;
374 #endif
375 #ifdef ARCH_powerpc
376     case bfd_arch_powerpc:
377       if (big)
378         disassemble = print_insn_big_powerpc;
379       else
380         disassemble = print_insn_little_powerpc;
381       break;
382 #endif
383 #ifdef ARCH_pru
384     case bfd_arch_pru:
385       disassemble = print_insn_pru;
386       break;
387 #endif
388 #ifdef ARCH_riscv
389     case bfd_arch_riscv:
390       disassemble = print_insn_riscv;
391       break;
392 #endif
393 #ifdef ARCH_rs6000
394     case bfd_arch_rs6000:
395       if (mach == bfd_mach_ppc_620)
396         disassemble = print_insn_big_powerpc;
397       else
398         disassemble = print_insn_rs6000;
399       break;
400 #endif
401 #ifdef ARCH_rl78
402     case bfd_arch_rl78:
403       disassemble = rl78_get_disassembler (abfd);
404       break;
405 #endif
406 #ifdef ARCH_rx
407     case bfd_arch_rx:
408       disassemble = print_insn_rx;
409       break;
410 #endif
411 #ifdef ARCH_s390
412     case bfd_arch_s390:
413       disassemble = print_insn_s390;
414       break;
415 #endif
416 #ifdef ARCH_score
417     case bfd_arch_score:
418       if (big)
419         disassemble = print_insn_big_score;
420       else
421         disassemble = print_insn_little_score;
422      break;
423 #endif
424 #ifdef ARCH_sh
425     case bfd_arch_sh:
426       disassemble = print_insn_sh;
427       break;
428 #endif
429 #ifdef ARCH_sparc
430     case bfd_arch_sparc:
431       disassemble = print_insn_sparc;
432       break;
433 #endif
434 #ifdef ARCH_spu
435     case bfd_arch_spu:
436       disassemble = print_insn_spu;
437       break;
438 #endif
439 #ifdef ARCH_tic30
440     case bfd_arch_tic30:
441       disassemble = print_insn_tic30;
442       break;
443 #endif
444 #ifdef ARCH_tic4x
445     case bfd_arch_tic4x:
446       disassemble = print_insn_tic4x;
447       break;
448 #endif
449 #ifdef ARCH_tic54x
450     case bfd_arch_tic54x:
451       disassemble = print_insn_tic54x;
452       break;
453 #endif
454 #ifdef ARCH_tic6x
455     case bfd_arch_tic6x:
456       disassemble = print_insn_tic6x;
457       break;
458 #endif
459 #ifdef ARCH_tic80
460     case bfd_arch_tic80:
461       disassemble = print_insn_tic80;
462       break;
463 #endif
464 #ifdef ARCH_ft32
465     case bfd_arch_ft32:
466       disassemble = print_insn_ft32;
467       break;
468 #endif
469 #ifdef ARCH_v850
470     case bfd_arch_v850:
471     case bfd_arch_v850_rh850:
472       disassemble = print_insn_v850;
473       break;
474 #endif
475 #ifdef ARCH_w65
476     case bfd_arch_w65:
477       disassemble = print_insn_w65;
478       break;
479 #endif
480 #ifdef ARCH_wasm32
481     case bfd_arch_wasm32:
482       disassemble = print_insn_wasm32;
483       break;
484 #endif
485 #ifdef ARCH_xgate
486     case bfd_arch_xgate:
487       disassemble = print_insn_xgate;
488       break;
489 #endif
490 #ifdef ARCH_xstormy16
491     case bfd_arch_xstormy16:
492       disassemble = print_insn_xstormy16;
493       break;
494 #endif
495 #ifdef ARCH_xc16x
496     case bfd_arch_xc16x:
497       disassemble = print_insn_xc16x;
498       break;
499 #endif
500 #ifdef ARCH_xtensa
501     case bfd_arch_xtensa:
502       disassemble = print_insn_xtensa;
503       break;
504 #endif
505 #ifdef ARCH_z80
506     case bfd_arch_z80:
507       disassemble = print_insn_z80;
508       break;
509 #endif
510 #ifdef ARCH_z8k
511     case bfd_arch_z8k:
512       if (mach == bfd_mach_z8001)
513         disassemble = print_insn_z8001;
514       else
515         disassemble = print_insn_z8002;
516       break;
517 #endif
518 #ifdef ARCH_vax
519     case bfd_arch_vax:
520       disassemble = print_insn_vax;
521       break;
522 #endif
523 #ifdef ARCH_visium
524      case bfd_arch_visium:
525        disassemble = print_insn_visium;
526        break;
527 #endif
528 #ifdef ARCH_frv
529     case bfd_arch_frv:
530       disassemble = print_insn_frv;
531       break;
532 #endif
533 #ifdef ARCH_moxie
534     case bfd_arch_moxie:
535       disassemble = print_insn_moxie;
536       break;
537 #endif
538 #ifdef ARCH_iq2000
539     case bfd_arch_iq2000:
540       disassemble = print_insn_iq2000;
541       break;
542 #endif
543 #ifdef ARCH_m32c
544     case bfd_arch_m32c:
545       disassemble = print_insn_m32c;
546       break;
547 #endif
548 #ifdef ARCH_tilegx
549     case bfd_arch_tilegx:
550       disassemble = print_insn_tilegx;
551       break;
552 #endif
553 #ifdef ARCH_tilepro
554     case bfd_arch_tilepro:
555       disassemble = print_insn_tilepro;
556       break;
557 #endif
558     default:
559       return 0;
560     }
561   return disassemble;
562 }
563
564 void
565 disassembler_usage (FILE *stream ATTRIBUTE_UNUSED)
566 {
567 #ifdef ARCH_aarch64
568   print_aarch64_disassembler_options (stream);
569 #endif
570 #ifdef ARCH_arc
571   print_arc_disassembler_options (stream);
572 #endif
573 #ifdef ARCH_arm
574   print_arm_disassembler_options (stream);
575 #endif
576 #ifdef ARCH_mips
577   print_mips_disassembler_options (stream);
578 #endif
579 #ifdef ARCH_powerpc
580   print_ppc_disassembler_options (stream);
581 #endif
582 #ifdef ARCH_riscv
583   print_riscv_disassembler_options (stream);
584 #endif
585 #ifdef ARCH_i386
586   print_i386_disassembler_options (stream);
587 #endif
588 #ifdef ARCH_s390
589   print_s390_disassembler_options (stream);
590 #endif
591 #ifdef ARCH_wasm32
592   print_wasm32_disassembler_options (stream);
593 #endif
594
595   return;
596 }
597
598 void
599 disassemble_init_for_target (struct disassemble_info * info)
600 {
601   if (info == NULL)
602     return;
603
604   switch (info->arch)
605     {
606 #ifdef ARCH_aarch64
607     case bfd_arch_aarch64:
608       info->symbol_is_valid = aarch64_symbol_is_valid;
609       info->disassembler_needs_relocs = TRUE;
610       break;
611 #endif
612 #ifdef ARCH_arm
613     case bfd_arch_arm:
614       info->symbol_is_valid = arm_symbol_is_valid;
615       info->disassembler_needs_relocs = TRUE;
616       break;
617 #endif
618 #ifdef ARCH_ia64
619     case bfd_arch_ia64:
620       info->skip_zeroes = 16;
621       break;
622 #endif
623 #ifdef ARCH_tic4x
624     case bfd_arch_tic4x:
625       info->skip_zeroes = 32;
626       break;
627 #endif
628 #ifdef ARCH_mep
629     case bfd_arch_mep:
630       info->skip_zeroes = 256;
631       info->skip_zeroes_at_end = 0;
632       break;
633 #endif
634 #ifdef ARCH_metag
635     case bfd_arch_metag:
636       info->disassembler_needs_relocs = TRUE;
637       break;
638 #endif
639 #ifdef ARCH_m32c
640     case bfd_arch_m32c:
641       /* This processor in fact is little endian.  The value set here
642          reflects the way opcodes are written in the cgen description.  */
643       info->endian = BFD_ENDIAN_BIG;
644       if (! info->insn_sets)
645         {
646           info->insn_sets = cgen_bitset_create (ISA_MAX);
647           if (info->mach == bfd_mach_m16c)
648             cgen_bitset_set (info->insn_sets, ISA_M16C);
649           else
650             cgen_bitset_set (info->insn_sets, ISA_M32C);
651         }
652       break;
653 #endif
654 #ifdef ARCH_pru
655     case bfd_arch_pru:
656       info->disassembler_needs_relocs = TRUE;
657       break;
658 #endif
659 #ifdef ARCH_powerpc
660     case bfd_arch_powerpc:
661 #endif
662 #ifdef ARCH_rs6000
663     case bfd_arch_rs6000:
664 #endif
665 #if defined (ARCH_powerpc) || defined (ARCH_rs6000)
666       disassemble_init_powerpc (info);
667       break;
668 #endif
669 #ifdef ARCH_wasm32
670     case bfd_arch_wasm32:
671       disassemble_init_wasm32 (info);
672       break;
673 #endif
674 #ifdef ARCH_s390
675     case bfd_arch_s390:
676       disassemble_init_s390 (info);
677       break;
678 #endif
679     default:
680       break;
681     }
682 }
683
684 /* Remove whitespace and consecutive commas from OPTIONS.  */
685
686 char *
687 remove_whitespace_and_extra_commas (char *options)
688 {
689   char *str;
690   size_t i, len;
691
692   if (options == NULL)
693     return NULL;
694
695   /* Strip off all trailing whitespace and commas.  */
696   for (len = strlen (options); len > 0; len--)
697     {
698       if (!ISSPACE (options[len - 1]) && options[len - 1] != ',')
699         break;
700       options[len - 1] = '\0';
701     }
702
703   /* Convert all remaining whitespace to commas.  */
704   for (i = 0; options[i] != '\0'; i++)
705     if (ISSPACE (options[i]))
706       options[i] = ',';
707
708   /* Remove consecutive commas.  */
709   for (str = options; *str != '\0'; str++)
710     if (*str == ',' && (*(str + 1) == ',' || str == options))
711       {
712         char *next = str + 1;
713         while (*next == ',')
714           next++;
715         len = strlen (next);
716         if (str != options)
717           str++;
718         memmove (str, next, len);
719         next[len - (size_t)(next - str)] = '\0';
720       }
721   return (strlen (options) != 0) ? options : NULL;
722 }
723
724 /* Like STRCMP, but treat ',' the same as '\0' so that we match
725    strings like "foobar" against "foobar,xxyyzz,...".  */
726
727 int
728 disassembler_options_cmp (const char *s1, const char *s2)
729 {
730   unsigned char c1, c2;
731
732   do
733     {
734       c1 = (unsigned char) *s1++;
735       if (c1 == ',')
736         c1 = '\0';
737       c2 = (unsigned char) *s2++;
738       if (c2 == ',')
739         c2 = '\0';
740       if (c1 == '\0')
741         return c1 - c2;
742     }
743   while (c1 == c2);
744
745   return c1 - c2;
746 }