ppc-dis.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. This file is part of GDB, GAS, and the GNU binutils.
  6. GDB, GAS, and the GNU binutils are free software; you can redistribute
  7. them and/or modify them under the terms of the GNU General Public
  8. License as published by the Free Software Foundation; either version
  9. 2, or (at your option) any later version.
  10. GDB, GAS, and the GNU binutils are distributed in the hope that they
  11. will be useful, but WITHOUT ANY WARRANTY; without even the implied
  12. warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  13. the GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this file; see the file COPYING. If not, write to the Free
  16. Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
  17. #include <asm/cputable.h>
  18. #include <asm/cpu_has_feature.h>
  19. #include "nonstdio.h"
  20. #include "ansidecl.h"
  21. #include "ppc.h"
  22. #include "dis-asm.h"
  23. /* Print a PowerPC or POWER instruction. */
  24. int
  25. print_insn_powerpc (unsigned long insn, unsigned long memaddr)
  26. {
  27. const struct powerpc_opcode *opcode;
  28. const struct powerpc_opcode *opcode_end;
  29. unsigned long op;
  30. int dialect;
  31. dialect = PPC_OPCODE_PPC | PPC_OPCODE_CLASSIC | PPC_OPCODE_COMMON
  32. | PPC_OPCODE_64 | PPC_OPCODE_POWER4 | PPC_OPCODE_ALTIVEC;
  33. if (cpu_has_feature(CPU_FTRS_POWER5))
  34. dialect |= PPC_OPCODE_POWER5;
  35. if (cpu_has_feature(CPU_FTRS_CELL))
  36. dialect |= PPC_OPCODE_CELL | PPC_OPCODE_ALTIVEC;
  37. if (cpu_has_feature(CPU_FTRS_POWER6))
  38. dialect |= PPC_OPCODE_POWER5 | PPC_OPCODE_POWER6 | PPC_OPCODE_ALTIVEC;
  39. /* Get the major opcode of the instruction. */
  40. op = PPC_OP (insn);
  41. /* Find the first match in the opcode table. We could speed this up
  42. a bit by doing a binary search on the major opcode. */
  43. opcode_end = powerpc_opcodes + powerpc_num_opcodes;
  44. again:
  45. for (opcode = powerpc_opcodes; opcode < opcode_end; opcode++)
  46. {
  47. unsigned long table_op;
  48. const unsigned char *opindex;
  49. const struct powerpc_operand *operand;
  50. int invalid;
  51. int need_comma;
  52. int need_paren;
  53. table_op = PPC_OP (opcode->opcode);
  54. if (op < table_op)
  55. break;
  56. if (op > table_op)
  57. continue;
  58. if ((insn & opcode->mask) != opcode->opcode
  59. || (opcode->flags & dialect) == 0)
  60. continue;
  61. /* Make two passes over the operands. First see if any of them
  62. have extraction functions, and, if they do, make sure the
  63. instruction is valid. */
  64. invalid = 0;
  65. for (opindex = opcode->operands; *opindex != 0; opindex++)
  66. {
  67. operand = powerpc_operands + *opindex;
  68. if (operand->extract)
  69. (*operand->extract) (insn, dialect, &invalid);
  70. }
  71. if (invalid)
  72. continue;
  73. /* The instruction is valid. */
  74. printf("%s", opcode->name);
  75. if (opcode->operands[0] != 0)
  76. printf("\t");
  77. /* Now extract and print the operands. */
  78. need_comma = 0;
  79. need_paren = 0;
  80. for (opindex = opcode->operands; *opindex != 0; opindex++)
  81. {
  82. long value;
  83. operand = powerpc_operands + *opindex;
  84. /* Operands that are marked FAKE are simply ignored. We
  85. already made sure that the extract function considered
  86. the instruction to be valid. */
  87. if ((operand->flags & PPC_OPERAND_FAKE) != 0)
  88. continue;
  89. /* Extract the value from the instruction. */
  90. if (operand->extract)
  91. value = (*operand->extract) (insn, dialect, &invalid);
  92. else
  93. {
  94. value = (insn >> operand->shift) & ((1 << operand->bits) - 1);
  95. if ((operand->flags & PPC_OPERAND_SIGNED) != 0
  96. && (value & (1 << (operand->bits - 1))) != 0)
  97. value -= 1 << operand->bits;
  98. }
  99. /* If the operand is optional, and the value is zero, don't
  100. print anything. */
  101. if ((operand->flags & PPC_OPERAND_OPTIONAL) != 0
  102. && (operand->flags & PPC_OPERAND_NEXT) == 0
  103. && value == 0)
  104. continue;
  105. if (need_comma)
  106. {
  107. printf(",");
  108. need_comma = 0;
  109. }
  110. /* Print the operand as directed by the flags. */
  111. if ((operand->flags & PPC_OPERAND_GPR) != 0
  112. || ((operand->flags & PPC_OPERAND_GPR_0) != 0 && value != 0))
  113. printf("r%ld", value);
  114. else if ((operand->flags & PPC_OPERAND_FPR) != 0)
  115. printf("f%ld", value);
  116. else if ((operand->flags & PPC_OPERAND_VR) != 0)
  117. printf("v%ld", value);
  118. else if ((operand->flags & PPC_OPERAND_RELATIVE) != 0)
  119. print_address (memaddr + value);
  120. else if ((operand->flags & PPC_OPERAND_ABSOLUTE) != 0)
  121. print_address (value & 0xffffffff);
  122. else if ((operand->flags & PPC_OPERAND_CR) == 0
  123. || (dialect & PPC_OPCODE_PPC) == 0)
  124. printf("%ld", value);
  125. else
  126. {
  127. if (operand->bits == 3)
  128. printf("cr%ld", value);
  129. else
  130. {
  131. static const char *cbnames[4] = { "lt", "gt", "eq", "so" };
  132. int cr;
  133. int cc;
  134. cr = value >> 2;
  135. if (cr != 0)
  136. printf("4*cr%d+", cr);
  137. cc = value & 3;
  138. printf("%s", cbnames[cc]);
  139. }
  140. }
  141. if (need_paren)
  142. {
  143. printf(")");
  144. need_paren = 0;
  145. }
  146. if ((operand->flags & PPC_OPERAND_PARENS) == 0)
  147. need_comma = 1;
  148. else
  149. {
  150. printf("(");
  151. need_paren = 1;
  152. }
  153. }
  154. /* We have found and printed an instruction; return. */
  155. return 4;
  156. }
  157. if ((dialect & PPC_OPCODE_ANY) != 0)
  158. {
  159. dialect = ~PPC_OPCODE_ANY;
  160. goto again;
  161. }
  162. /* We could not find a match. */
  163. printf(".long 0x%lx", insn);
  164. return 4;
  165. }