1 /* ppc-dis.c -- Disassemble PowerPC instructions
2 Copyright 1994, 1995, 2000, 2001, 2002, 2003, 2004, 2005, 2006
3 Free Software Foundation, Inc.
4 Written by Ian Lance Taylor, Cygnus Support
6 This file is part of GDB, GAS, and the GNU binutils.
8 GDB, GAS, and the GNU binutils are free software; you can redistribute
9 them and/or modify them under the terms of the GNU General Public
10 License as published by the Free Software Foundation; either version
11 2, or (at your option) any later version.
13 GDB, GAS, and the GNU binutils are distributed in the hope that they
14 will be useful, but WITHOUT ANY WARRANTY; without even the implied
15 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
16 the GNU General Public 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 Free
20 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
22 #include <asm/cputable.h>
28 /* Print a PowerPC or POWER instruction. */
31 print_insn_powerpc (unsigned long insn, unsigned long memaddr)
33 const struct powerpc_opcode *opcode;
34 const struct powerpc_opcode *opcode_end;
38 dialect = PPC_OPCODE_PPC | PPC_OPCODE_CLASSIC | PPC_OPCODE_COMMON
39 | PPC_OPCODE_64 | PPC_OPCODE_POWER4 | PPC_OPCODE_ALTIVEC;
41 if (cpu_has_feature(CPU_FTRS_POWER5))
42 dialect |= PPC_OPCODE_POWER5;
44 if (cpu_has_feature(CPU_FTRS_CELL))
45 dialect |= PPC_OPCODE_CELL | PPC_OPCODE_ALTIVEC;
47 if (cpu_has_feature(CPU_FTRS_POWER6))
48 dialect |= PPC_OPCODE_POWER5 | PPC_OPCODE_POWER6 | PPC_OPCODE_ALTIVEC;
50 /* Get the major opcode of the instruction. */
53 /* Find the first match in the opcode table. We could speed this up
54 a bit by doing a binary search on the major opcode. */
55 opcode_end = powerpc_opcodes + powerpc_num_opcodes;
57 for (opcode = powerpc_opcodes; opcode < opcode_end; opcode++)
59 unsigned long table_op;
60 const unsigned char *opindex;
61 const struct powerpc_operand *operand;
66 table_op = PPC_OP (opcode->opcode);
72 if ((insn & opcode->mask) != opcode->opcode
73 || (opcode->flags & dialect) == 0)
76 /* Make two passes over the operands. First see if any of them
77 have extraction functions, and, if they do, make sure the
78 instruction is valid. */
80 for (opindex = opcode->operands; *opindex != 0; opindex++)
82 operand = powerpc_operands + *opindex;
84 (*operand->extract) (insn, dialect, &invalid);
89 /* The instruction is valid. */
90 printf("%s", opcode->name);
91 if (opcode->operands[0] != 0)
94 /* Now extract and print the operands. */
97 for (opindex = opcode->operands; *opindex != 0; opindex++)
101 operand = powerpc_operands + *opindex;
103 /* Operands that are marked FAKE are simply ignored. We
104 already made sure that the extract function considered
105 the instruction to be valid. */
106 if ((operand->flags & PPC_OPERAND_FAKE) != 0)
109 /* Extract the value from the instruction. */
110 if (operand->extract)
111 value = (*operand->extract) (insn, dialect, &invalid);
114 value = (insn >> operand->shift) & ((1 << operand->bits) - 1);
115 if ((operand->flags & PPC_OPERAND_SIGNED) != 0
116 && (value & (1 << (operand->bits - 1))) != 0)
117 value -= 1 << operand->bits;
120 /* If the operand is optional, and the value is zero, don't
122 if ((operand->flags & PPC_OPERAND_OPTIONAL) != 0
123 && (operand->flags & PPC_OPERAND_NEXT) == 0
133 /* Print the operand as directed by the flags. */
134 if ((operand->flags & PPC_OPERAND_GPR) != 0
135 || ((operand->flags & PPC_OPERAND_GPR_0) != 0 && value != 0))
136 printf("r%ld", value);
137 else if ((operand->flags & PPC_OPERAND_FPR) != 0)
138 printf("f%ld", value);
139 else if ((operand->flags & PPC_OPERAND_VR) != 0)
140 printf("v%ld", value);
141 else if ((operand->flags & PPC_OPERAND_RELATIVE) != 0)
142 print_address (memaddr + value);
143 else if ((operand->flags & PPC_OPERAND_ABSOLUTE) != 0)
144 print_address (value & 0xffffffff);
145 else if ((operand->flags & PPC_OPERAND_CR) == 0
146 || (dialect & PPC_OPCODE_PPC) == 0)
147 printf("%ld", value);
150 if (operand->bits == 3)
151 printf("cr%ld", value);
154 static const char *cbnames[4] = { "lt", "gt", "eq", "so" };
160 printf("4*cr%d+", cr);
162 printf("%s", cbnames[cc]);
172 if ((operand->flags & PPC_OPERAND_PARENS) == 0)
181 /* We have found and printed an instruction; return. */
185 if ((dialect & PPC_OPCODE_ANY) != 0)
187 dialect = ~PPC_OPCODE_ANY;
191 /* We could not find a match. */
192 printf(".long 0x%lx", insn);