1 /* ppc-dis.c -- Disassemble PowerPC instructions
2 Copyright 1994, 1995, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
3 2008, 2009 Free Software Foundation, Inc.
4 Written by Ian Lance Taylor, Cygnus Support
6 This file is part of the GNU opcodes library.
8 This library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 It is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
16 License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this file; see the file COPYING. If not, write to the
20 Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
27 #include "opcode/ppc.h"
29 /* This file provides several disassembler functions, all of which use
30 the disassembler interface defined in dis-asm.h. Several functions
31 are provided because this file handles disassembly for the PowerPC
32 in both big and little endian mode and also for the POWER (RS/6000)
34 static int print_insn_powerpc (bfd_vma
, struct disassemble_info
*, int,
39 /* Stash the result of parsing disassembler_options here. */
43 #define POWERPC_DIALECT(INFO) \
44 (((struct dis_private *) ((INFO)->private_data))->dialect)
52 struct ppc_mopt ppc_opts
[] = {
53 { "403", (PPC_OPCODE_PPC
| PPC_OPCODE_CLASSIC
| PPC_OPCODE_403
56 { "405", (PPC_OPCODE_PPC
| PPC_OPCODE_CLASSIC
| PPC_OPCODE_403
57 | PPC_OPCODE_405
| PPC_OPCODE_32
),
59 { "440", (PPC_OPCODE_PPC
| PPC_OPCODE_BOOKE
| PPC_OPCODE_32
60 | PPC_OPCODE_440
| PPC_OPCODE_ISEL
| PPC_OPCODE_RFMCI
),
62 { "464", (PPC_OPCODE_PPC
| PPC_OPCODE_BOOKE
| PPC_OPCODE_32
63 | PPC_OPCODE_440
| PPC_OPCODE_ISEL
| PPC_OPCODE_RFMCI
),
65 { "601", (PPC_OPCODE_PPC
| PPC_OPCODE_CLASSIC
| PPC_OPCODE_601
68 { "603", (PPC_OPCODE_PPC
| PPC_OPCODE_CLASSIC
| PPC_OPCODE_32
),
70 { "604", (PPC_OPCODE_PPC
| PPC_OPCODE_CLASSIC
| PPC_OPCODE_32
),
72 { "620", (PPC_OPCODE_PPC
| PPC_OPCODE_CLASSIC
| PPC_OPCODE_64
),
74 { "7400", (PPC_OPCODE_PPC
| PPC_OPCODE_CLASSIC
| PPC_OPCODE_ALTIVEC
77 { "7410", (PPC_OPCODE_PPC
| PPC_OPCODE_CLASSIC
| PPC_OPCODE_ALTIVEC
80 { "7450", (PPC_OPCODE_PPC
| PPC_OPCODE_CLASSIC
| PPC_OPCODE_ALTIVEC
83 { "7455", (PPC_OPCODE_PPC
| PPC_OPCODE_CLASSIC
| PPC_OPCODE_ALTIVEC
86 { "750cl", (PPC_OPCODE_PPC
| PPC_OPCODE_PPCPS
)
88 { "altivec", (PPC_OPCODE_PPC
| PPC_OPCODE_CLASSIC
),
92 { "booke", (PPC_OPCODE_PPC
| PPC_OPCODE_BOOKE
| PPC_OPCODE_32
),
94 { "booke32", (PPC_OPCODE_PPC
| PPC_OPCODE_BOOKE
| PPC_OPCODE_32
),
96 { "cell", (PPC_OPCODE_PPC
| PPC_OPCODE_CLASSIC
| PPC_OPCODE_64
97 | PPC_OPCODE_POWER4
| PPC_OPCODE_CELL
| PPC_OPCODE_ALTIVEC
),
99 { "com", (PPC_OPCODE_COMMON
| PPC_OPCODE_32
),
101 { "e300", (PPC_OPCODE_PPC
| PPC_OPCODE_CLASSIC
| PPC_OPCODE_32
104 { "e500", (PPC_OPCODE_PPC
| PPC_OPCODE_BOOKE
| PPC_OPCODE_SPE
105 | PPC_OPCODE_ISEL
| PPC_OPCODE_EFS
| PPC_OPCODE_BRLOCK
106 | PPC_OPCODE_PMR
| PPC_OPCODE_CACHELCK
| PPC_OPCODE_RFMCI
107 | PPC_OPCODE_E500MC
),
109 { "e500mc", (PPC_OPCODE_PPC
| PPC_OPCODE_BOOKE
| PPC_OPCODE_ISEL
110 | PPC_OPCODE_PMR
| PPC_OPCODE_CACHELCK
| PPC_OPCODE_RFMCI
111 | PPC_OPCODE_E500MC
),
113 { "e500x2", (PPC_OPCODE_PPC
| PPC_OPCODE_BOOKE
| PPC_OPCODE_SPE
114 | PPC_OPCODE_ISEL
| PPC_OPCODE_EFS
| PPC_OPCODE_BRLOCK
115 | PPC_OPCODE_PMR
| PPC_OPCODE_CACHELCK
| PPC_OPCODE_RFMCI
116 | PPC_OPCODE_E500MC
),
118 { "efs", (PPC_OPCODE_PPC
| PPC_OPCODE_EFS
),
120 { "power4", (PPC_OPCODE_PPC
| PPC_OPCODE_CLASSIC
| PPC_OPCODE_64
121 | PPC_OPCODE_POWER4
),
123 { "power5", (PPC_OPCODE_PPC
| PPC_OPCODE_CLASSIC
| PPC_OPCODE_64
124 | PPC_OPCODE_POWER4
| PPC_OPCODE_POWER5
),
126 { "power6", (PPC_OPCODE_PPC
| PPC_OPCODE_CLASSIC
| PPC_OPCODE_64
127 | PPC_OPCODE_POWER4
| PPC_OPCODE_POWER5
| PPC_OPCODE_POWER6
128 | PPC_OPCODE_ALTIVEC
),
130 { "power7", (PPC_OPCODE_PPC
| PPC_OPCODE_CLASSIC
| PPC_OPCODE_ISEL
131 | PPC_OPCODE_64
| PPC_OPCODE_POWER4
| PPC_OPCODE_POWER5
132 | PPC_OPCODE_POWER6
| PPC_OPCODE_POWER7
| PPC_OPCODE_ALTIVEC
135 { "ppc", (PPC_OPCODE_PPC
| PPC_OPCODE_CLASSIC
| PPC_OPCODE_32
),
137 { "ppc32", (PPC_OPCODE_PPC
| PPC_OPCODE_CLASSIC
| PPC_OPCODE_32
),
139 { "ppc64", (PPC_OPCODE_PPC
| PPC_OPCODE_CLASSIC
| PPC_OPCODE_64
),
141 { "ppc64bridge", (PPC_OPCODE_PPC
| PPC_OPCODE_CLASSIC
| PPC_OPCODE_64_BRIDGE
144 { "ppcps", (PPC_OPCODE_PPC
| PPC_OPCODE_PPCPS
),
146 { "pwr", (PPC_OPCODE_POWER
| PPC_OPCODE_32
),
148 { "pwr2", (PPC_OPCODE_POWER
| PPC_OPCODE_POWER2
| PPC_OPCODE_32
),
150 { "pwrx", (PPC_OPCODE_POWER
| PPC_OPCODE_POWER2
| PPC_OPCODE_32
),
152 { "spe", (PPC_OPCODE_PPC
| PPC_OPCODE_EFS
),
154 { "vsx", (PPC_OPCODE_PPC
| PPC_OPCODE_CLASSIC
),
158 /* Handle -m and -M options that set cpu type, and .machine arg. */
161 ppc_parse_cpu (ppc_cpu_t ppc_cpu
, const char *arg
)
164 ppc_cpu_t retain_flags
= ppc_cpu
& (PPC_OPCODE_ALTIVEC
| PPC_OPCODE_VSX
165 | PPC_OPCODE_SPE
| PPC_OPCODE_ANY
);
168 for (i
= 0; i
< sizeof (ppc_opts
) / sizeof (ppc_opts
[0]); i
++)
169 if (strcmp (ppc_opts
[i
].opt
, arg
) == 0)
171 if (ppc_opts
[i
].sticky
)
173 retain_flags
|= ppc_opts
[i
].sticky
;
174 if ((ppc_cpu
& ~(PPC_OPCODE_ALTIVEC
| PPC_OPCODE_VSX
175 | PPC_OPCODE_SPE
| PPC_OPCODE_ANY
)) != 0)
178 ppc_cpu
= ppc_opts
[i
].cpu
;
181 if (i
>= sizeof (ppc_opts
) / sizeof (ppc_opts
[0]))
184 ppc_cpu
|= retain_flags
;
188 /* Determine which set of machines to disassemble for. */
191 powerpc_init_dialect (struct disassemble_info
*info
)
193 ppc_cpu_t dialect
= 0;
195 struct dis_private
*priv
= calloc (sizeof (*priv
), 1);
200 arg
= info
->disassembler_options
;
203 ppc_cpu_t new_cpu
= 0;
204 char *end
= strchr (arg
, ',');
209 if ((new_cpu
= ppc_parse_cpu (dialect
, arg
)) != 0)
211 else if (strcmp (arg
, "32") == 0)
213 dialect
&= ~PPC_OPCODE_64
;
214 dialect
|= PPC_OPCODE_32
;
216 else if (strcmp (arg
, "64") == 0)
218 dialect
|= PPC_OPCODE_64
;
219 dialect
&= ~PPC_OPCODE_32
;
222 fprintf (stderr
, _("warning: ignoring unknown -M%s option\n"), arg
);
229 if ((dialect
& ~(PPC_OPCODE_32
| PPC_OPCODE_64
)) == 0)
231 if (info
->mach
== bfd_mach_ppc64
)
232 dialect
|= PPC_OPCODE_64
;
234 dialect
|= PPC_OPCODE_32
;
235 /* Choose a reasonable default. */
236 dialect
|= (PPC_OPCODE_PPC
| PPC_OPCODE_COMMON
| PPC_OPCODE_CLASSIC
237 | PPC_OPCODE_601
| PPC_OPCODE_ALTIVEC
);
240 info
->private_data
= priv
;
241 POWERPC_DIALECT(info
) = dialect
;
246 /* Print a big endian PowerPC instruction. */
249 print_insn_big_powerpc (bfd_vma memaddr
, struct disassemble_info
*info
)
251 if (info
->private_data
== NULL
&& !powerpc_init_dialect (info
))
253 return print_insn_powerpc (memaddr
, info
, 1, POWERPC_DIALECT(info
));
256 /* Print a little endian PowerPC instruction. */
259 print_insn_little_powerpc (bfd_vma memaddr
, struct disassemble_info
*info
)
261 if (info
->private_data
== NULL
&& !powerpc_init_dialect (info
))
263 return print_insn_powerpc (memaddr
, info
, 0, POWERPC_DIALECT(info
));
266 /* Print a POWER (RS/6000) instruction. */
269 print_insn_rs6000 (bfd_vma memaddr
, struct disassemble_info
*info
)
271 return print_insn_powerpc (memaddr
, info
, 1, PPC_OPCODE_POWER
);
274 /* Extract the operand value from the PowerPC or POWER instruction. */
277 operand_value_powerpc (const struct powerpc_operand
*operand
,
278 unsigned long insn
, ppc_cpu_t dialect
)
282 /* Extract the value from the instruction. */
283 if (operand
->extract
)
284 value
= (*operand
->extract
) (insn
, dialect
, &invalid
);
287 value
= (insn
>> operand
->shift
) & operand
->bitm
;
288 if ((operand
->flags
& PPC_OPERAND_SIGNED
) != 0)
290 /* BITM is always some number of zeros followed by some
291 number of ones, followed by some numer of zeros. */
292 unsigned long top
= operand
->bitm
;
293 /* top & -top gives the rightmost 1 bit, so this
294 fills in any trailing zeros. */
295 top
|= (top
& -top
) - 1;
297 value
= (value
^ top
) - top
;
304 /* Determine whether the optional operand(s) should be printed. */
307 skip_optional_operands (const unsigned char *opindex
,
308 unsigned long insn
, ppc_cpu_t dialect
)
310 const struct powerpc_operand
*operand
;
312 for (; *opindex
!= 0; opindex
++)
314 operand
= &powerpc_operands
[*opindex
];
315 if ((operand
->flags
& PPC_OPERAND_NEXT
) != 0
316 || ((operand
->flags
& PPC_OPERAND_OPTIONAL
) != 0
317 && operand_value_powerpc (operand
, insn
, dialect
) != 0))
324 /* Print a PowerPC or POWER instruction. */
327 print_insn_powerpc (bfd_vma memaddr
,
328 struct disassemble_info
*info
,
335 const struct powerpc_opcode
*opcode
;
336 const struct powerpc_opcode
*opcode_end
;
338 ppc_cpu_t dialect_orig
= dialect
;
340 status
= (*info
->read_memory_func
) (memaddr
, buffer
, 4, info
);
343 (*info
->memory_error_func
) (status
, memaddr
, info
);
348 insn
= bfd_getb32 (buffer
);
350 insn
= bfd_getl32 (buffer
);
352 /* Get the major opcode of the instruction. */
355 /* Find the first match in the opcode table. We could speed this up
356 a bit by doing a binary search on the major opcode. */
357 opcode_end
= powerpc_opcodes
+ powerpc_num_opcodes
;
359 for (opcode
= powerpc_opcodes
; opcode
< opcode_end
; opcode
++)
361 unsigned long table_op
;
362 const unsigned char *opindex
;
363 const struct powerpc_operand
*operand
;
369 table_op
= PPC_OP (opcode
->opcode
);
375 if ((insn
& opcode
->mask
) != opcode
->opcode
376 || (opcode
->flags
& dialect
) == 0
377 || (opcode
->deprecated
& dialect_orig
) != 0)
380 /* Make two passes over the operands. First see if any of them
381 have extraction functions, and, if they do, make sure the
382 instruction is valid. */
384 for (opindex
= opcode
->operands
; *opindex
!= 0; opindex
++)
386 operand
= powerpc_operands
+ *opindex
;
387 if (operand
->extract
)
388 (*operand
->extract
) (insn
, dialect
, &invalid
);
393 /* The instruction is valid. */
394 if (opcode
->operands
[0] != 0)
395 (*info
->fprintf_func
) (info
->stream
, "%-7s ", opcode
->name
);
397 (*info
->fprintf_func
) (info
->stream
, "%s", opcode
->name
);
399 /* Now extract and print the operands. */
403 for (opindex
= opcode
->operands
; *opindex
!= 0; opindex
++)
407 operand
= powerpc_operands
+ *opindex
;
409 /* Operands that are marked FAKE are simply ignored. We
410 already made sure that the extract function considered
411 the instruction to be valid. */
412 if ((operand
->flags
& PPC_OPERAND_FAKE
) != 0)
415 /* If all of the optional operands have the value zero,
416 then don't print any of them. */
417 if ((operand
->flags
& PPC_OPERAND_OPTIONAL
) != 0)
419 if (skip_optional
< 0)
420 skip_optional
= skip_optional_operands (opindex
, insn
,
426 value
= operand_value_powerpc (operand
, insn
, dialect
);
430 (*info
->fprintf_func
) (info
->stream
, ",");
434 /* Print the operand as directed by the flags. */
435 if ((operand
->flags
& PPC_OPERAND_GPR
) != 0
436 || ((operand
->flags
& PPC_OPERAND_GPR_0
) != 0 && value
!= 0))
437 (*info
->fprintf_func
) (info
->stream
, "r%ld", value
);
438 else if ((operand
->flags
& PPC_OPERAND_FPR
) != 0)
439 (*info
->fprintf_func
) (info
->stream
, "f%ld", value
);
440 else if ((operand
->flags
& PPC_OPERAND_VR
) != 0)
441 (*info
->fprintf_func
) (info
->stream
, "v%ld", value
);
442 else if ((operand
->flags
& PPC_OPERAND_VSR
) != 0)
443 (*info
->fprintf_func
) (info
->stream
, "vs%ld", value
);
444 else if ((operand
->flags
& PPC_OPERAND_RELATIVE
) != 0)
445 (*info
->print_address_func
) (memaddr
+ value
, info
);
446 else if ((operand
->flags
& PPC_OPERAND_ABSOLUTE
) != 0)
447 (*info
->print_address_func
) ((bfd_vma
) value
& 0xffffffff, info
);
448 else if ((operand
->flags
& PPC_OPERAND_FSL
) != 0)
449 (*info
->fprintf_func
) (info
->stream
, "fsl%ld", value
);
450 else if ((operand
->flags
& PPC_OPERAND_FCR
) != 0)
451 (*info
->fprintf_func
) (info
->stream
, "fcr%ld", value
);
452 else if ((operand
->flags
& PPC_OPERAND_UDI
) != 0)
453 (*info
->fprintf_func
) (info
->stream
, "%ld", value
);
454 else if ((operand
->flags
& PPC_OPERAND_CR
) != 0
455 && (dialect
& PPC_OPCODE_PPC
) != 0)
457 if (operand
->bitm
== 7)
458 (*info
->fprintf_func
) (info
->stream
, "cr%ld", value
);
461 static const char *cbnames
[4] = { "lt", "gt", "eq", "so" };
467 (*info
->fprintf_func
) (info
->stream
, "4*cr%d+", cr
);
469 (*info
->fprintf_func
) (info
->stream
, "%s", cbnames
[cc
]);
473 (*info
->fprintf_func
) (info
->stream
, "%ld", value
);
477 (*info
->fprintf_func
) (info
->stream
, ")");
481 if ((operand
->flags
& PPC_OPERAND_PARENS
) == 0)
485 (*info
->fprintf_func
) (info
->stream
, "(");
490 /* We have found and printed an instruction; return. */
494 if ((dialect
& PPC_OPCODE_ANY
) != 0)
496 dialect
= ~PPC_OPCODE_ANY
;
500 /* We could not find a match. */
501 (*info
->fprintf_func
) (info
->stream
, ".long 0x%lx", insn
);
507 print_ppc_disassembler_options (FILE *stream
)
511 fprintf (stream
, _("\n\
512 The following PPC specific disassembler options are supported for use with\n\
515 for (col
= 0, i
= 0; i
< sizeof (ppc_opts
) / sizeof (ppc_opts
[0]); i
++)
517 col
+= fprintf (stream
, " %s,", ppc_opts
[i
].opt
);
520 fprintf (stream
, "\n");
524 fprintf (stream
, " 32, 64\n");