ppc-dis.c 5.4 KB

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