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 5 6 This file is part of GDB, GAS, and the GNU binutils. 7 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. 12 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. 17 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. */ 21 22 #include <asm/cputable.h> 23 #include <asm/cpu_has_feature.h> 24 #include "nonstdio.h" 25 #include "ansidecl.h" 26 #include "ppc.h" 27 #include "dis-asm.h" 28 29 /* Print a PowerPC or POWER instruction. */ 30 31 int 32 print_insn_powerpc (unsigned long insn, unsigned long memaddr) 33 { 34 const struct powerpc_opcode *opcode; 35 const struct powerpc_opcode *opcode_end; 36 unsigned long op; 37 int dialect; 38 39 dialect = PPC_OPCODE_PPC | PPC_OPCODE_CLASSIC | PPC_OPCODE_COMMON 40 | PPC_OPCODE_64 | PPC_OPCODE_POWER4 | PPC_OPCODE_ALTIVEC; 41 42 if (cpu_has_feature(CPU_FTRS_POWER5)) 43 dialect |= PPC_OPCODE_POWER5; 44 45 if (cpu_has_feature(CPU_FTRS_CELL)) 46 dialect |= PPC_OPCODE_CELL | PPC_OPCODE_ALTIVEC; 47 48 if (cpu_has_feature(CPU_FTRS_POWER6)) 49 dialect |= PPC_OPCODE_POWER5 | PPC_OPCODE_POWER6 | PPC_OPCODE_ALTIVEC; 50 51 /* Get the major opcode of the instruction. */ 52 op = PPC_OP (insn); 53 54 /* Find the first match in the opcode table. We could speed this up 55 a bit by doing a binary search on the major opcode. */ 56 opcode_end = powerpc_opcodes + powerpc_num_opcodes; 57 again: 58 for (opcode = powerpc_opcodes; opcode < opcode_end; opcode++) 59 { 60 unsigned long table_op; 61 const unsigned char *opindex; 62 const struct powerpc_operand *operand; 63 int invalid; 64 int need_comma; 65 int need_paren; 66 67 table_op = PPC_OP (opcode->opcode); 68 if (op < table_op) 69 break; 70 if (op > table_op) 71 continue; 72 73 if ((insn & opcode->mask) != opcode->opcode 74 || (opcode->flags & dialect) == 0) 75 continue; 76 77 /* Make two passes over the operands. First see if any of them 78 have extraction functions, and, if they do, make sure the 79 instruction is valid. */ 80 invalid = 0; 81 for (opindex = opcode->operands; *opindex != 0; opindex++) 82 { 83 operand = powerpc_operands + *opindex; 84 if (operand->extract) 85 (*operand->extract) (insn, dialect, &invalid); 86 } 87 if (invalid) 88 continue; 89 90 /* The instruction is valid. */ 91 printf("%s", opcode->name); 92 if (opcode->operands[0] != 0) 93 printf("\t"); 94 95 /* Now extract and print the operands. */ 96 need_comma = 0; 97 need_paren = 0; 98 for (opindex = opcode->operands; *opindex != 0; opindex++) 99 { 100 long value; 101 102 operand = powerpc_operands + *opindex; 103 104 /* Operands that are marked FAKE are simply ignored. We 105 already made sure that the extract function considered 106 the instruction to be valid. */ 107 if ((operand->flags & PPC_OPERAND_FAKE) != 0) 108 continue; 109 110 /* Extract the value from the instruction. */ 111 if (operand->extract) 112 value = (*operand->extract) (insn, dialect, &invalid); 113 else 114 { 115 value = (insn >> operand->shift) & ((1 << operand->bits) - 1); 116 if ((operand->flags & PPC_OPERAND_SIGNED) != 0 117 && (value & (1 << (operand->bits - 1))) != 0) 118 value -= 1 << operand->bits; 119 } 120 121 /* If the operand is optional, and the value is zero, don't 122 print anything. */ 123 if ((operand->flags & PPC_OPERAND_OPTIONAL) != 0 124 && (operand->flags & PPC_OPERAND_NEXT) == 0 125 && value == 0) 126 continue; 127 128 if (need_comma) 129 { 130 printf(","); 131 need_comma = 0; 132 } 133 134 /* Print the operand as directed by the flags. */ 135 if ((operand->flags & PPC_OPERAND_GPR) != 0 136 || ((operand->flags & PPC_OPERAND_GPR_0) != 0 && value != 0)) 137 printf("r%ld", value); 138 else if ((operand->flags & PPC_OPERAND_FPR) != 0) 139 printf("f%ld", value); 140 else if ((operand->flags & PPC_OPERAND_VR) != 0) 141 printf("v%ld", value); 142 else if ((operand->flags & PPC_OPERAND_RELATIVE) != 0) 143 print_address (memaddr + value); 144 else if ((operand->flags & PPC_OPERAND_ABSOLUTE) != 0) 145 print_address (value & 0xffffffff); 146 else if ((operand->flags & PPC_OPERAND_CR) == 0 147 || (dialect & PPC_OPCODE_PPC) == 0) 148 printf("%ld", value); 149 else 150 { 151 if (operand->bits == 3) 152 printf("cr%ld", value); 153 else 154 { 155 static const char *cbnames[4] = { "lt", "gt", "eq", "so" }; 156 int cr; 157 int cc; 158 159 cr = value >> 2; 160 if (cr != 0) 161 printf("4*cr%d+", cr); 162 cc = value & 3; 163 printf("%s", cbnames[cc]); 164 } 165 } 166 167 if (need_paren) 168 { 169 printf(")"); 170 need_paren = 0; 171 } 172 173 if ((operand->flags & PPC_OPERAND_PARENS) == 0) 174 need_comma = 1; 175 else 176 { 177 printf("("); 178 need_paren = 1; 179 } 180 } 181 182 /* We have found and printed an instruction; return. */ 183 return 4; 184 } 185 186 if ((dialect & PPC_OPCODE_ANY) != 0) 187 { 188 dialect = ~PPC_OPCODE_ANY; 189 goto again; 190 } 191 192 /* We could not find a match. */ 193 printf(".long 0x%lx", insn); 194 195 return 4; 196 } 197