x86: fold several AVX512VL templates
[external/binutils.git] / opcodes / ppc-dis.c
1 /* ppc-dis.c -- Disassemble PowerPC instructions
2    Copyright (C) 1994-2018 Free Software Foundation, Inc.
3    Written by Ian Lance Taylor, Cygnus Support
4
5    This file is part of the GNU opcodes library.
6
7    This library is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3, or (at your option)
10    any later version.
11
12    It is distributed in the hope that it will be useful, but WITHOUT
13    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
15    License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this file; see the file COPYING.  If not, write to the
19    Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston,
20    MA 02110-1301, USA.  */
21
22 #include "sysdep.h"
23 #include <stdio.h>
24 #include "disassemble.h"
25 #include "elf-bfd.h"
26 #include "elf/ppc.h"
27 #include "opintl.h"
28 #include "opcode/ppc.h"
29 #include "libiberty.h"
30
31 /* This file provides several disassembler functions, all of which use
32    the disassembler interface defined in dis-asm.h.  Several functions
33    are provided because this file handles disassembly for the PowerPC
34    in both big and little endian mode and also for the POWER (RS/6000)
35    chip.  */
36 static int print_insn_powerpc (bfd_vma, struct disassemble_info *, int,
37                                ppc_cpu_t);
38
39 struct dis_private
40 {
41   /* Stash the result of parsing disassembler_options here.  */
42   ppc_cpu_t dialect;
43 } private;
44
45 #define POWERPC_DIALECT(INFO) \
46   (((struct dis_private *) ((INFO)->private_data))->dialect)
47
48 struct ppc_mopt {
49   /* Option string, without -m or -M prefix.  */
50   const char *opt;
51   /* CPU option flags.  */
52   ppc_cpu_t cpu;
53   /* Flags that should stay on, even when combined with another cpu
54      option.  This should only be used for generic options like
55      "-many" or "-maltivec" where it is reasonable to add some
56      capability to another cpu selection.  The added flags are sticky
57      so that, for example, "-many -me500" and "-me500 -many" result in
58      the same assembler or disassembler behaviour.  Do not use
59      "sticky" for specific cpus, as this will prevent that cpu's flags
60      from overriding the defaults set in powerpc_init_dialect or a
61      prior -m option.  */
62   ppc_cpu_t sticky;
63 };
64
65 struct ppc_mopt ppc_opts[] = {
66   { "403",     PPC_OPCODE_PPC | PPC_OPCODE_403,
67     0 },
68   { "405",     PPC_OPCODE_PPC | PPC_OPCODE_403 | PPC_OPCODE_405,
69     0 },
70   { "440",     (PPC_OPCODE_PPC | PPC_OPCODE_BOOKE | PPC_OPCODE_440
71                 | PPC_OPCODE_ISEL | PPC_OPCODE_RFMCI),
72     0 },
73   { "464",     (PPC_OPCODE_PPC | PPC_OPCODE_BOOKE | PPC_OPCODE_440
74                 | PPC_OPCODE_ISEL | PPC_OPCODE_RFMCI),
75     0 },
76   { "476",     (PPC_OPCODE_PPC | PPC_OPCODE_ISEL | PPC_OPCODE_476
77                 | PPC_OPCODE_POWER4 | PPC_OPCODE_POWER5),
78     0 },
79   { "601",     PPC_OPCODE_PPC | PPC_OPCODE_601,
80     0 },
81   { "603",     PPC_OPCODE_PPC,
82     0 },
83   { "604",     PPC_OPCODE_PPC,
84     0 },
85   { "620",     PPC_OPCODE_PPC | PPC_OPCODE_64,
86     0 },
87   { "7400",    PPC_OPCODE_PPC | PPC_OPCODE_ALTIVEC,
88     0 },
89   { "7410",    PPC_OPCODE_PPC | PPC_OPCODE_ALTIVEC,
90     0 },
91   { "7450",    PPC_OPCODE_PPC | PPC_OPCODE_7450 | PPC_OPCODE_ALTIVEC,
92     0 },
93   { "7455",    PPC_OPCODE_PPC | PPC_OPCODE_ALTIVEC,
94     0 },
95   { "750cl",   PPC_OPCODE_PPC | PPC_OPCODE_750 | PPC_OPCODE_PPCPS
96     , 0 },
97   { "821",     PPC_OPCODE_PPC | PPC_OPCODE_860,
98     0 },
99   { "850",     PPC_OPCODE_PPC | PPC_OPCODE_860,
100     0 },
101   { "860",     PPC_OPCODE_PPC | PPC_OPCODE_860,
102     0 },
103   { "a2",      (PPC_OPCODE_PPC | PPC_OPCODE_ISEL | PPC_OPCODE_POWER4
104                 | PPC_OPCODE_POWER5 | PPC_OPCODE_CACHELCK | PPC_OPCODE_64
105                 | PPC_OPCODE_A2),
106     0 },
107   { "altivec", PPC_OPCODE_PPC,
108     PPC_OPCODE_ALTIVEC },
109   { "any",     PPC_OPCODE_PPC,
110     PPC_OPCODE_ANY },
111   { "booke",   PPC_OPCODE_PPC | PPC_OPCODE_BOOKE,
112     0 },
113   { "booke32", PPC_OPCODE_PPC | PPC_OPCODE_BOOKE,
114     0 },
115   { "cell",    (PPC_OPCODE_PPC | PPC_OPCODE_64 | PPC_OPCODE_POWER4
116                 | PPC_OPCODE_CELL | PPC_OPCODE_ALTIVEC),
117     0 },
118   { "com",     PPC_OPCODE_COMMON,
119     0 },
120   { "e200z4",  (PPC_OPCODE_PPC | PPC_OPCODE_BOOKE| PPC_OPCODE_SPE
121                 | PPC_OPCODE_ISEL | PPC_OPCODE_EFS | PPC_OPCODE_BRLOCK
122                 | PPC_OPCODE_PMR | PPC_OPCODE_CACHELCK | PPC_OPCODE_RFMCI
123                 | PPC_OPCODE_E500 | PPC_OPCODE_VLE | PPC_OPCODE_E200Z4
124                 | PPC_OPCODE_EFS2 | PPC_OPCODE_LSP),
125     0 },
126   { "e300",    PPC_OPCODE_PPC | PPC_OPCODE_E300,
127     0 },
128   { "e500",    (PPC_OPCODE_PPC | PPC_OPCODE_BOOKE | PPC_OPCODE_SPE
129                 | PPC_OPCODE_ISEL | PPC_OPCODE_EFS | PPC_OPCODE_BRLOCK
130                 | PPC_OPCODE_PMR | PPC_OPCODE_CACHELCK | PPC_OPCODE_RFMCI
131                 | PPC_OPCODE_E500),
132     0 },
133   { "e500mc",  (PPC_OPCODE_PPC | PPC_OPCODE_BOOKE | PPC_OPCODE_ISEL
134                 | PPC_OPCODE_PMR | PPC_OPCODE_CACHELCK | PPC_OPCODE_RFMCI
135                 | PPC_OPCODE_E500MC),
136     0 },
137   { "e500mc64",  (PPC_OPCODE_PPC | PPC_OPCODE_BOOKE | PPC_OPCODE_ISEL
138                 | PPC_OPCODE_PMR | PPC_OPCODE_CACHELCK | PPC_OPCODE_RFMCI
139                 | PPC_OPCODE_E500MC | PPC_OPCODE_64 | PPC_OPCODE_POWER5
140                 | PPC_OPCODE_POWER6 | PPC_OPCODE_POWER7),
141     0 },
142   { "e5500",    (PPC_OPCODE_PPC | PPC_OPCODE_BOOKE | PPC_OPCODE_ISEL
143                 | PPC_OPCODE_PMR | PPC_OPCODE_CACHELCK | PPC_OPCODE_RFMCI
144                 | PPC_OPCODE_E500MC | PPC_OPCODE_64 | PPC_OPCODE_POWER4
145                 | PPC_OPCODE_POWER5 | PPC_OPCODE_POWER6 | PPC_OPCODE_POWER7),
146     0 },
147   { "e6500",   (PPC_OPCODE_PPC | PPC_OPCODE_BOOKE | PPC_OPCODE_ISEL
148                 | PPC_OPCODE_PMR | PPC_OPCODE_CACHELCK | PPC_OPCODE_RFMCI
149                 | PPC_OPCODE_E500MC | PPC_OPCODE_64 | PPC_OPCODE_ALTIVEC
150                 | PPC_OPCODE_E6500 | PPC_OPCODE_TMR | PPC_OPCODE_POWER4
151                 | PPC_OPCODE_POWER5 | PPC_OPCODE_POWER6 | PPC_OPCODE_POWER7),
152     0 },
153   { "e500x2",  (PPC_OPCODE_PPC | PPC_OPCODE_BOOKE | PPC_OPCODE_SPE
154                 | PPC_OPCODE_ISEL | PPC_OPCODE_EFS | PPC_OPCODE_BRLOCK
155                 | PPC_OPCODE_PMR | PPC_OPCODE_CACHELCK | PPC_OPCODE_RFMCI
156                 | PPC_OPCODE_E500),
157     0 },
158   { "efs",     PPC_OPCODE_PPC | PPC_OPCODE_EFS,
159     0 },
160   { "efs2",    PPC_OPCODE_PPC | PPC_OPCODE_EFS | PPC_OPCODE_EFS2,
161     0 },
162   { "power4",  PPC_OPCODE_PPC | PPC_OPCODE_64 | PPC_OPCODE_POWER4,
163     0 },
164   { "power5",  (PPC_OPCODE_PPC | PPC_OPCODE_64 | PPC_OPCODE_POWER4
165                 | PPC_OPCODE_POWER5),
166     0 },
167   { "power6",  (PPC_OPCODE_PPC | PPC_OPCODE_64 | PPC_OPCODE_POWER4
168                 | PPC_OPCODE_POWER5 | PPC_OPCODE_POWER6 | PPC_OPCODE_ALTIVEC),
169     0 },
170   { "power7",  (PPC_OPCODE_PPC | PPC_OPCODE_ISEL | PPC_OPCODE_64
171                 | PPC_OPCODE_POWER4 | PPC_OPCODE_POWER5 | PPC_OPCODE_POWER6
172                 | PPC_OPCODE_POWER7 | PPC_OPCODE_ALTIVEC | PPC_OPCODE_VSX),
173     0 },
174   { "power8",  (PPC_OPCODE_PPC | PPC_OPCODE_ISEL | PPC_OPCODE_64
175                 | PPC_OPCODE_POWER4 | PPC_OPCODE_POWER5 | PPC_OPCODE_POWER6
176                 | PPC_OPCODE_POWER7 | PPC_OPCODE_POWER8
177                 | PPC_OPCODE_ALTIVEC | PPC_OPCODE_VSX),
178     0 },
179   { "power9",  (PPC_OPCODE_PPC | PPC_OPCODE_ISEL | PPC_OPCODE_64
180                 | PPC_OPCODE_POWER4 | PPC_OPCODE_POWER5 | PPC_OPCODE_POWER6
181                 | PPC_OPCODE_POWER7 | PPC_OPCODE_POWER8 | PPC_OPCODE_POWER9
182                 | PPC_OPCODE_ALTIVEC | PPC_OPCODE_VSX),
183     0 },
184   { "ppc",     PPC_OPCODE_PPC,
185     0 },
186   { "ppc32",   PPC_OPCODE_PPC,
187     0 },
188   { "32",      PPC_OPCODE_PPC,
189     0 },
190   { "ppc64",   PPC_OPCODE_PPC | PPC_OPCODE_64,
191     0 },
192   { "64",      PPC_OPCODE_PPC | PPC_OPCODE_64,
193     0 },
194   { "ppc64bridge", PPC_OPCODE_PPC | PPC_OPCODE_64_BRIDGE,
195     0 },
196   { "ppcps",   PPC_OPCODE_PPC | PPC_OPCODE_PPCPS,
197     0 },
198   { "pwr",     PPC_OPCODE_POWER,
199     0 },
200   { "pwr2",    PPC_OPCODE_POWER | PPC_OPCODE_POWER2,
201     0 },
202   { "pwr4",    PPC_OPCODE_PPC | PPC_OPCODE_64 | PPC_OPCODE_POWER4,
203     0 },
204   { "pwr5",    (PPC_OPCODE_PPC | PPC_OPCODE_64 | PPC_OPCODE_POWER4
205                 | PPC_OPCODE_POWER5),
206     0 },
207   { "pwr5x",   (PPC_OPCODE_PPC | PPC_OPCODE_64 | PPC_OPCODE_POWER4
208                 | PPC_OPCODE_POWER5),
209     0 },
210   { "pwr6",    (PPC_OPCODE_PPC | PPC_OPCODE_64 | PPC_OPCODE_POWER4
211                 | PPC_OPCODE_POWER5 | PPC_OPCODE_POWER6 | PPC_OPCODE_ALTIVEC),
212     0 },
213   { "pwr7",    (PPC_OPCODE_PPC | PPC_OPCODE_ISEL | PPC_OPCODE_64
214                 | PPC_OPCODE_POWER4 | PPC_OPCODE_POWER5 | PPC_OPCODE_POWER6
215                 | PPC_OPCODE_POWER7 | PPC_OPCODE_ALTIVEC | PPC_OPCODE_VSX),
216     0 },
217   { "pwr8",    (PPC_OPCODE_PPC | PPC_OPCODE_ISEL | PPC_OPCODE_64
218                 | PPC_OPCODE_POWER4 | PPC_OPCODE_POWER5 | PPC_OPCODE_POWER6
219                 | PPC_OPCODE_POWER7 | PPC_OPCODE_POWER8
220                 | PPC_OPCODE_ALTIVEC | PPC_OPCODE_VSX),
221     0 },
222   { "pwr9",    (PPC_OPCODE_PPC | PPC_OPCODE_ISEL | PPC_OPCODE_64
223                 | PPC_OPCODE_POWER4 | PPC_OPCODE_POWER5 | PPC_OPCODE_POWER6
224                 | PPC_OPCODE_POWER7 | PPC_OPCODE_POWER8 | PPC_OPCODE_POWER9
225                 | PPC_OPCODE_ALTIVEC | PPC_OPCODE_VSX),
226     0 },
227   { "pwrx",    PPC_OPCODE_POWER | PPC_OPCODE_POWER2,
228     0 },
229   { "raw",     PPC_OPCODE_PPC,
230     PPC_OPCODE_RAW },
231   { "spe",     PPC_OPCODE_PPC | PPC_OPCODE_EFS,
232     PPC_OPCODE_SPE },
233   { "spe2",     PPC_OPCODE_PPC | PPC_OPCODE_EFS | PPC_OPCODE_EFS2 | PPC_OPCODE_SPE,
234     PPC_OPCODE_SPE2 },
235   { "titan",   (PPC_OPCODE_PPC | PPC_OPCODE_BOOKE | PPC_OPCODE_PMR
236                 | PPC_OPCODE_RFMCI | PPC_OPCODE_TITAN),
237     0 },
238   { "vle",     (PPC_OPCODE_PPC | PPC_OPCODE_BOOKE| PPC_OPCODE_SPE
239                 | PPC_OPCODE_ISEL | PPC_OPCODE_EFS | PPC_OPCODE_BRLOCK
240                 | PPC_OPCODE_PMR | PPC_OPCODE_CACHELCK | PPC_OPCODE_RFMCI
241                 | PPC_OPCODE_LSP | PPC_OPCODE_EFS2 | PPC_OPCODE_SPE2),
242     PPC_OPCODE_VLE },
243   { "vsx",     PPC_OPCODE_PPC,
244     PPC_OPCODE_VSX },
245 };
246
247 /* Switch between Booke and VLE dialects for interlinked dumps.  */
248 static ppc_cpu_t
249 get_powerpc_dialect (struct disassemble_info *info)
250 {
251   ppc_cpu_t dialect = 0;
252
253   dialect = POWERPC_DIALECT (info);
254
255   /* Disassemble according to the section headers flags for VLE-mode.  */
256   if (dialect & PPC_OPCODE_VLE
257       && info->section != NULL && info->section->owner != NULL
258       && bfd_get_flavour (info->section->owner) == bfd_target_elf_flavour
259       && elf_object_id (info->section->owner) == PPC32_ELF_DATA
260       && (elf_section_flags (info->section) & SHF_PPC_VLE) != 0)
261     return dialect;
262   else
263     return dialect & ~ PPC_OPCODE_VLE;
264 }
265
266 /* Handle -m and -M options that set cpu type, and .machine arg.  */
267
268 ppc_cpu_t
269 ppc_parse_cpu (ppc_cpu_t ppc_cpu, ppc_cpu_t *sticky, const char *arg)
270 {
271   unsigned int i;
272
273   for (i = 0; i < ARRAY_SIZE (ppc_opts); i++)
274     if (disassembler_options_cmp (ppc_opts[i].opt, arg) == 0)
275       {
276         if (ppc_opts[i].sticky)
277           {
278             *sticky |= ppc_opts[i].sticky;
279             if ((ppc_cpu & ~*sticky) != 0)
280               break;
281           }
282         ppc_cpu = ppc_opts[i].cpu;
283         break;
284       }
285   if (i >= ARRAY_SIZE (ppc_opts))
286     return 0;
287
288   ppc_cpu |= *sticky;
289   return ppc_cpu;
290 }
291
292 /* Determine which set of machines to disassemble for.  */
293
294 static void
295 powerpc_init_dialect (struct disassemble_info *info)
296 {
297   ppc_cpu_t dialect = 0;
298   ppc_cpu_t sticky = 0;
299   struct dis_private *priv = calloc (sizeof (*priv), 1);
300
301   if (priv == NULL)
302     priv = &private;
303
304   switch (info->mach)
305     {
306     case bfd_mach_ppc_403:
307     case bfd_mach_ppc_403gc:
308       dialect = ppc_parse_cpu (dialect, &sticky, "403");
309       break;
310     case bfd_mach_ppc_405:
311       dialect = ppc_parse_cpu (dialect, &sticky, "405");
312       break;
313     case bfd_mach_ppc_601:
314       dialect = ppc_parse_cpu (dialect, &sticky, "601");
315       break;
316     case bfd_mach_ppc_a35:
317     case bfd_mach_ppc_rs64ii:
318     case bfd_mach_ppc_rs64iii:
319       dialect = ppc_parse_cpu (dialect, &sticky, "pwr2") | PPC_OPCODE_64;
320       break;
321     case bfd_mach_ppc_e500:
322       dialect = ppc_parse_cpu (dialect, &sticky, "e500");
323       break;
324     case bfd_mach_ppc_e500mc:
325       dialect = ppc_parse_cpu (dialect, &sticky, "e500mc");
326       break;
327     case bfd_mach_ppc_e500mc64:
328       dialect = ppc_parse_cpu (dialect, &sticky, "e500mc64");
329       break;
330     case bfd_mach_ppc_e5500:
331       dialect = ppc_parse_cpu (dialect, &sticky, "e5500");
332       break;
333     case bfd_mach_ppc_e6500:
334       dialect = ppc_parse_cpu (dialect, &sticky, "e6500");
335       break;
336     case bfd_mach_ppc_titan:
337       dialect = ppc_parse_cpu (dialect, &sticky, "titan");
338       break;
339     case bfd_mach_ppc_vle:
340       dialect = ppc_parse_cpu (dialect, &sticky, "vle");
341       break;
342     default:
343       if (info->arch == bfd_arch_powerpc)
344         dialect = ppc_parse_cpu (dialect, &sticky, "power9") | PPC_OPCODE_ANY;
345       else
346         dialect = ppc_parse_cpu (dialect, &sticky, "pwr");
347       break;
348     }
349
350   const char *opt;
351   FOR_EACH_DISASSEMBLER_OPTION (opt, info->disassembler_options)
352     {
353       ppc_cpu_t new_cpu = 0;
354
355       if (disassembler_options_cmp (opt, "32") == 0)
356         dialect &= ~(ppc_cpu_t) PPC_OPCODE_64;
357       else if (disassembler_options_cmp (opt, "64") == 0)
358         dialect |= PPC_OPCODE_64;
359       else if ((new_cpu = ppc_parse_cpu (dialect, &sticky, opt)) != 0)
360         dialect = new_cpu;
361       else
362         /* xgettext: c-format */
363         opcodes_error_handler (_("warning: ignoring unknown -M%s option"), opt);
364     }
365
366   info->private_data = priv;
367   POWERPC_DIALECT(info) = dialect;
368 }
369
370 #define PPC_OPCD_SEGS 64
371 static unsigned short powerpc_opcd_indices[PPC_OPCD_SEGS+1];
372 #define VLE_OPCD_SEGS 32
373 static unsigned short vle_opcd_indices[VLE_OPCD_SEGS+1];
374 #define SPE2_OPCD_SEGS 13
375 static unsigned short spe2_opcd_indices[SPE2_OPCD_SEGS+1];
376
377 /* Calculate opcode table indices to speed up disassembly,
378    and init dialect.  */
379
380 void
381 disassemble_init_powerpc (struct disassemble_info *info)
382 {
383   int i;
384   unsigned short last;
385
386   if (powerpc_opcd_indices[PPC_OPCD_SEGS] == 0)
387     {
388       i = powerpc_num_opcodes;
389       while (--i >= 0)
390         {
391           unsigned op = PPC_OP (powerpc_opcodes[i].opcode);
392           powerpc_opcd_indices[op] = i;
393         }
394
395       last = powerpc_num_opcodes;
396       for (i = PPC_OPCD_SEGS; i > 0; --i)
397         {
398           if (powerpc_opcd_indices[i] == 0)
399             powerpc_opcd_indices[i] = last;
400           last = powerpc_opcd_indices[i];
401         }
402
403       i = vle_num_opcodes;
404       while (--i >= 0)
405         {
406           unsigned op = VLE_OP (vle_opcodes[i].opcode, vle_opcodes[i].mask);
407           unsigned seg = VLE_OP_TO_SEG (op);
408           vle_opcd_indices[seg] = i;
409         }
410
411       last = vle_num_opcodes;
412       for (i = VLE_OPCD_SEGS; i > 0; --i)
413         {
414           if (vle_opcd_indices[i] == 0)
415             vle_opcd_indices[i] = last;
416           last = vle_opcd_indices[i];
417         }
418     }
419
420   /* SPE2 opcodes */
421   i = spe2_num_opcodes;
422   while (--i >= 0)
423     {
424       unsigned xop = SPE2_XOP (spe2_opcodes[i].opcode);
425       unsigned seg = SPE2_XOP_TO_SEG (xop);
426       spe2_opcd_indices[seg] = i;
427     }
428
429   last = spe2_num_opcodes;
430   for (i = SPE2_OPCD_SEGS; i > 1; --i)
431     {
432       if (spe2_opcd_indices[i] == 0)
433         spe2_opcd_indices[i] = last;
434       last = spe2_opcd_indices[i];
435     }
436
437   powerpc_init_dialect (info);
438 }
439
440 /* Print a big endian PowerPC instruction.  */
441
442 int
443 print_insn_big_powerpc (bfd_vma memaddr, struct disassemble_info *info)
444 {
445   return print_insn_powerpc (memaddr, info, 1, get_powerpc_dialect (info));
446 }
447
448 /* Print a little endian PowerPC instruction.  */
449
450 int
451 print_insn_little_powerpc (bfd_vma memaddr, struct disassemble_info *info)
452 {
453   return print_insn_powerpc (memaddr, info, 0, get_powerpc_dialect (info));
454 }
455
456 /* Extract the operand value from the PowerPC or POWER instruction.  */
457
458 static int64_t
459 operand_value_powerpc (const struct powerpc_operand *operand,
460                        uint64_t insn, ppc_cpu_t dialect)
461 {
462   int64_t value;
463   int invalid;
464   /* Extract the value from the instruction.  */
465   if (operand->extract)
466     value = (*operand->extract) (insn, dialect, &invalid);
467   else
468     {
469       if (operand->shift >= 0)
470         value = (insn >> operand->shift) & operand->bitm;
471       else
472         value = (insn << -operand->shift) & operand->bitm;
473       if ((operand->flags & PPC_OPERAND_SIGNED) != 0)
474         {
475           /* BITM is always some number of zeros followed by some
476              number of ones, followed by some number of zeros.  */
477           uint64_t top = operand->bitm;
478           /* top & -top gives the rightmost 1 bit, so this
479              fills in any trailing zeros.  */
480           top |= (top & -top) - 1;
481           top &= ~(top >> 1);
482           value = (value ^ top) - top;
483         }
484     }
485
486   return value;
487 }
488
489 /* Determine whether the optional operand(s) should be printed.  */
490
491 static int
492 skip_optional_operands (const unsigned char *opindex,
493                         uint64_t insn, ppc_cpu_t dialect)
494 {
495   const struct powerpc_operand *operand;
496
497   for (; *opindex != 0; opindex++)
498     {
499       operand = &powerpc_operands[*opindex];
500       if ((operand->flags & PPC_OPERAND_NEXT) != 0
501           || ((operand->flags & PPC_OPERAND_OPTIONAL) != 0
502               && operand_value_powerpc (operand, insn, dialect) !=
503                  ppc_optional_operand_value (operand)))
504         return 0;
505     }
506
507   return 1;
508 }
509
510 /* Find a match for INSN in the opcode table, given machine DIALECT.  */
511
512 static const struct powerpc_opcode *
513 lookup_powerpc (uint64_t insn, ppc_cpu_t dialect)
514 {
515   const struct powerpc_opcode *opcode, *opcode_end, *last;
516   unsigned long op;
517
518   /* Get the major opcode of the instruction.  */
519   op = PPC_OP (insn);
520
521   /* Find the first match in the opcode table for this major opcode.  */
522   opcode_end = powerpc_opcodes + powerpc_opcd_indices[op + 1];
523   last = NULL;
524   for (opcode = powerpc_opcodes + powerpc_opcd_indices[op];
525        opcode < opcode_end;
526        ++opcode)
527     {
528       const unsigned char *opindex;
529       const struct powerpc_operand *operand;
530       int invalid;
531
532       if ((insn & opcode->mask) != opcode->opcode
533           || ((dialect & PPC_OPCODE_ANY) == 0
534               && ((opcode->flags & dialect) == 0
535                   || (opcode->deprecated & dialect) != 0)))
536         continue;
537
538       /* Check validity of operands.  */
539       invalid = 0;
540       for (opindex = opcode->operands; *opindex != 0; opindex++)
541         {
542           operand = powerpc_operands + *opindex;
543           if (operand->extract)
544             (*operand->extract) (insn, dialect, &invalid);
545         }
546       if (invalid)
547         continue;
548
549       if ((dialect & PPC_OPCODE_RAW) == 0)
550         return opcode;
551
552       /* The raw machine insn is one that is not a specialization.  */
553       if (last == NULL
554           || (last->mask & ~opcode->mask) != 0)
555         last = opcode;
556     }
557
558   return last;
559 }
560
561 /* Find a match for INSN in the VLE opcode table.  */
562
563 static const struct powerpc_opcode *
564 lookup_vle (uint64_t insn)
565 {
566   const struct powerpc_opcode *opcode;
567   const struct powerpc_opcode *opcode_end;
568   unsigned op, seg;
569
570   op = PPC_OP (insn);
571   if (op >= 0x20 && op <= 0x37)
572     {
573       /* This insn has a 4-bit opcode.  */
574       op &= 0x3c;
575     }
576   seg = VLE_OP_TO_SEG (op);
577
578   /* Find the first match in the opcode table for this major opcode.  */
579   opcode_end = vle_opcodes + vle_opcd_indices[seg + 1];
580   for (opcode = vle_opcodes + vle_opcd_indices[seg];
581        opcode < opcode_end;
582        ++opcode)
583     {
584       uint64_t table_opcd = opcode->opcode;
585       uint64_t table_mask = opcode->mask;
586       bfd_boolean table_op_is_short = PPC_OP_SE_VLE(table_mask);
587       uint64_t insn2;
588       const unsigned char *opindex;
589       const struct powerpc_operand *operand;
590       int invalid;
591
592       insn2 = insn;
593       if (table_op_is_short)
594         insn2 >>= 16;
595       if ((insn2 & table_mask) != table_opcd)
596         continue;
597
598       /* Check validity of operands.  */
599       invalid = 0;
600       for (opindex = opcode->operands; *opindex != 0; ++opindex)
601         {
602           operand = powerpc_operands + *opindex;
603           if (operand->extract)
604             (*operand->extract) (insn, (ppc_cpu_t)0, &invalid);
605         }
606       if (invalid)
607         continue;
608
609       return opcode;
610     }
611
612   return NULL;
613 }
614
615 /* Find a match for INSN in the SPE2 opcode table.  */
616
617 static const struct powerpc_opcode *
618 lookup_spe2 (uint64_t insn)
619 {
620   const struct powerpc_opcode *opcode, *opcode_end;
621   unsigned op, xop, seg;
622
623   op = PPC_OP (insn);
624   if (op != 0x4)
625     {
626       /* This is not SPE2 insn.
627        * All SPE2 instructions have OP=4 and differs by XOP  */
628       return NULL;
629     }
630   xop = SPE2_XOP (insn);
631   seg = SPE2_XOP_TO_SEG (xop);
632
633   /* Find the first match in the opcode table for this major opcode.  */
634   opcode_end = spe2_opcodes + spe2_opcd_indices[seg + 1];
635   for (opcode = spe2_opcodes + spe2_opcd_indices[seg];
636        opcode < opcode_end;
637        ++opcode)
638     {
639       uint64_t table_opcd = opcode->opcode;
640       uint64_t table_mask = opcode->mask;
641       uint64_t insn2;
642       const unsigned char *opindex;
643       const struct powerpc_operand *operand;
644       int invalid;
645
646       insn2 = insn;
647       if ((insn2 & table_mask) != table_opcd)
648         continue;
649
650       /* Check validity of operands.  */
651       invalid = 0;
652       for (opindex = opcode->operands; *opindex != 0; ++opindex)
653         {
654           operand = powerpc_operands + *opindex;
655           if (operand->extract)
656             (*operand->extract) (insn, (ppc_cpu_t)0, &invalid);
657         }
658       if (invalid)
659         continue;
660
661       return opcode;
662     }
663
664   return NULL;
665 }
666
667 /* Print a PowerPC or POWER instruction.  */
668
669 static int
670 print_insn_powerpc (bfd_vma memaddr,
671                     struct disassemble_info *info,
672                     int bigendian,
673                     ppc_cpu_t dialect)
674 {
675   bfd_byte buffer[4];
676   int status;
677   uint64_t insn;
678   const struct powerpc_opcode *opcode;
679   bfd_boolean insn_is_short;
680
681   status = (*info->read_memory_func) (memaddr, buffer, 4, info);
682   if (status != 0)
683     {
684       /* The final instruction may be a 2-byte VLE insn.  */
685       if ((dialect & PPC_OPCODE_VLE) != 0)
686         {
687           /* Clear buffer so unused bytes will not have garbage in them.  */
688           buffer[0] = buffer[1] = buffer[2] = buffer[3] = 0;
689           status = (*info->read_memory_func) (memaddr, buffer, 2, info);
690           if (status != 0)
691             {
692               (*info->memory_error_func) (status, memaddr, info);
693               return -1;
694             }
695         }
696       else
697         {
698           (*info->memory_error_func) (status, memaddr, info);
699           return -1;
700         }
701     }
702
703   if (bigendian)
704     insn = bfd_getb32 (buffer);
705   else
706     insn = bfd_getl32 (buffer);
707
708   /* Get the major opcode of the insn.  */
709   opcode = NULL;
710   insn_is_short = FALSE;
711   if ((dialect & PPC_OPCODE_VLE) != 0)
712     {
713       opcode = lookup_vle (insn);
714       if (opcode != NULL)
715         insn_is_short = PPC_OP_SE_VLE(opcode->mask);
716     }
717   if (opcode == NULL && (dialect & PPC_OPCODE_SPE2) != 0)
718     opcode = lookup_spe2 (insn);
719   if (opcode == NULL)
720     opcode = lookup_powerpc (insn, dialect & ~PPC_OPCODE_ANY);
721   if (opcode == NULL && (dialect & PPC_OPCODE_ANY) != 0)
722     opcode = lookup_powerpc (insn, dialect);
723
724   if (opcode != NULL)
725     {
726       const unsigned char *opindex;
727       const struct powerpc_operand *operand;
728       int need_comma;
729       int need_paren;
730       int skip_optional;
731
732       if (opcode->operands[0] != 0)
733         (*info->fprintf_func) (info->stream, "%-7s ", opcode->name);
734       else
735         (*info->fprintf_func) (info->stream, "%s", opcode->name);
736
737       if (insn_is_short)
738         /* The operands will be fetched out of the 16-bit instruction.  */
739         insn >>= 16;
740
741       /* Now extract and print the operands.  */
742       need_comma = 0;
743       need_paren = 0;
744       skip_optional = -1;
745       for (opindex = opcode->operands; *opindex != 0; opindex++)
746         {
747           int64_t value;
748
749           operand = powerpc_operands + *opindex;
750
751           /* Operands that are marked FAKE are simply ignored.  We
752              already made sure that the extract function considered
753              the instruction to be valid.  */
754           if ((operand->flags & PPC_OPERAND_FAKE) != 0)
755             continue;
756
757           /* If all of the optional operands have the value zero,
758              then don't print any of them.  */
759           if ((operand->flags & PPC_OPERAND_OPTIONAL) != 0)
760             {
761               if (skip_optional < 0)
762                 skip_optional = skip_optional_operands (opindex, insn,
763                                                         dialect);
764               if (skip_optional)
765                 continue;
766             }
767
768           value = operand_value_powerpc (operand, insn, dialect);
769
770           if (need_comma)
771             {
772               (*info->fprintf_func) (info->stream, ",");
773               need_comma = 0;
774             }
775
776           /* Print the operand as directed by the flags.  */
777           if ((operand->flags & PPC_OPERAND_GPR) != 0
778               || ((operand->flags & PPC_OPERAND_GPR_0) != 0 && value != 0))
779             (*info->fprintf_func) (info->stream, "r%" PPC_INT_FMT "d", value);
780           else if ((operand->flags & PPC_OPERAND_FPR) != 0)
781             (*info->fprintf_func) (info->stream, "f%" PPC_INT_FMT "d", value);
782           else if ((operand->flags & PPC_OPERAND_VR) != 0)
783             (*info->fprintf_func) (info->stream, "v%" PPC_INT_FMT "d", value);
784           else if ((operand->flags & PPC_OPERAND_VSR) != 0)
785             (*info->fprintf_func) (info->stream, "vs%" PPC_INT_FMT "d", value);
786           else if ((operand->flags & PPC_OPERAND_RELATIVE) != 0)
787             (*info->print_address_func) (memaddr + value, info);
788           else if ((operand->flags & PPC_OPERAND_ABSOLUTE) != 0)
789             (*info->print_address_func) ((bfd_vma) value & 0xffffffff, info);
790           else if ((operand->flags & PPC_OPERAND_FSL) != 0)
791             (*info->fprintf_func) (info->stream, "fsl%" PPC_INT_FMT "d", value);
792           else if ((operand->flags & PPC_OPERAND_FCR) != 0)
793             (*info->fprintf_func) (info->stream, "fcr%" PPC_INT_FMT "d", value);
794           else if ((operand->flags & PPC_OPERAND_UDI) != 0)
795             (*info->fprintf_func) (info->stream, "%" PPC_INT_FMT "d", value);
796           else if ((operand->flags & PPC_OPERAND_CR_REG) != 0
797                    && (((dialect & PPC_OPCODE_PPC) != 0)
798                        || ((dialect & PPC_OPCODE_VLE) != 0)))
799             (*info->fprintf_func) (info->stream, "cr%" PPC_INT_FMT "d", value);
800           else if (((operand->flags & PPC_OPERAND_CR_BIT) != 0)
801                    && (((dialect & PPC_OPCODE_PPC) != 0)
802                        || ((dialect & PPC_OPCODE_VLE) != 0)))
803             {
804               static const char *cbnames[4] = { "lt", "gt", "eq", "so" };
805               int cr;
806               int cc;
807
808               cr = value >> 2;
809               if (cr != 0)
810                 (*info->fprintf_func) (info->stream, "4*cr%d+", cr);
811               cc = value & 3;
812               (*info->fprintf_func) (info->stream, "%s", cbnames[cc]);
813             }
814           else
815             (*info->fprintf_func) (info->stream, "%" PPC_INT_FMT "d", value);
816
817           if (need_paren)
818             {
819               (*info->fprintf_func) (info->stream, ")");
820               need_paren = 0;
821             }
822
823           if ((operand->flags & PPC_OPERAND_PARENS) == 0)
824             need_comma = 1;
825           else
826             {
827               (*info->fprintf_func) (info->stream, "(");
828               need_paren = 1;
829             }
830         }
831
832       /* We have found and printed an instruction.
833          If it was a short VLE instruction we have more to do.  */
834       if (insn_is_short)
835         {
836           memaddr += 2;
837           return 2;
838         }
839       else
840         /* Otherwise, return.  */
841         return 4;
842     }
843
844   /* We could not find a match.  */
845   (*info->fprintf_func) (info->stream, ".long 0x%" PPC_INT_FMT "x", insn);
846
847   return 4;
848 }
849
850 const disasm_options_t *
851 disassembler_options_powerpc (void)
852 {
853   static disasm_options_t *opts = NULL;
854
855   if (opts == NULL)
856     {
857       size_t i, num_options = ARRAY_SIZE (ppc_opts);
858       opts = XNEW (disasm_options_t);
859       opts->name = XNEWVEC (const char *, num_options + 1);
860       for (i = 0; i < num_options; i++)
861         opts->name[i] = ppc_opts[i].opt;
862       /* The array we return must be NULL terminated.  */
863       opts->name[i] = NULL;
864       opts->description = NULL;
865     }
866
867   return opts;
868 }
869
870 void
871 print_ppc_disassembler_options (FILE *stream)
872 {
873   unsigned int i, col;
874
875   fprintf (stream, _("\n\
876 The following PPC specific disassembler options are supported for use with\n\
877 the -M switch:\n"));
878
879   for (col = 0, i = 0; i < ARRAY_SIZE (ppc_opts); i++)
880     {
881       col += fprintf (stream, " %s,", ppc_opts[i].opt);
882       if (col > 66)
883         {
884           fprintf (stream, "\n");
885           col = 0;
886         }
887     }
888   fprintf (stream, "\n");
889 }