ppc-dis.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /* ppc-dis.c -- Disassemble PowerPC instructions
  2. Copyright (C) 1994-2016 Free Software Foundation, Inc.
  3. Written by Ian Lance Taylor, Cygnus Support
  4. This file is part of GDB, GAS, and the GNU binutils.
  5. GDB, GAS, and the GNU binutils are free software; you can redistribute
  6. them and/or modify them under the terms of the GNU General Public
  7. License as published by the Free Software Foundation; either version
  8. 2, or (at your option) any later version.
  9. GDB, GAS, and the GNU binutils are distributed in the hope that they
  10. will be useful, but WITHOUT ANY WARRANTY; without even the implied
  11. warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  12. the GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this file; see the file COPYING. If not, write to the Free
  15. Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
  16. #include <asm/cputable.h>
  17. #include <asm/cpu_has_feature.h>
  18. #include "nonstdio.h"
  19. #include "ansidecl.h"
  20. #include "ppc.h"
  21. #include "dis-asm.h"
  22. /* This file provides several disassembler functions, all of which use
  23. the disassembler interface defined in dis-asm.h. Several functions
  24. are provided because this file handles disassembly for the PowerPC
  25. in both big and little endian mode and also for the POWER (RS/6000)
  26. chip. */
  27. /* Extract the operand value from the PowerPC or POWER instruction. */
  28. static long
  29. operand_value_powerpc (const struct powerpc_operand *operand,
  30. unsigned long insn, ppc_cpu_t dialect)
  31. {
  32. long value;
  33. int invalid;
  34. /* Extract the value from the instruction. */
  35. if (operand->extract)
  36. value = (*operand->extract) (insn, dialect, &invalid);
  37. else
  38. {
  39. if (operand->shift >= 0)
  40. value = (insn >> operand->shift) & operand->bitm;
  41. else
  42. value = (insn << -operand->shift) & operand->bitm;
  43. if ((operand->flags & PPC_OPERAND_SIGNED) != 0)
  44. {
  45. /* BITM is always some number of zeros followed by some
  46. number of ones, followed by some number of zeros. */
  47. unsigned long top = operand->bitm;
  48. /* top & -top gives the rightmost 1 bit, so this
  49. fills in any trailing zeros. */
  50. top |= (top & -top) - 1;
  51. top &= ~(top >> 1);
  52. value = (value ^ top) - top;
  53. }
  54. }
  55. return value;
  56. }
  57. /* Determine whether the optional operand(s) should be printed. */
  58. static int
  59. skip_optional_operands (const unsigned char *opindex,
  60. unsigned long insn, ppc_cpu_t dialect)
  61. {
  62. const struct powerpc_operand *operand;
  63. for (; *opindex != 0; opindex++)
  64. {
  65. operand = &powerpc_operands[*opindex];
  66. if ((operand->flags & PPC_OPERAND_NEXT) != 0
  67. || ((operand->flags & PPC_OPERAND_OPTIONAL) != 0
  68. && operand_value_powerpc (operand, insn, dialect) !=
  69. ppc_optional_operand_value (operand)))
  70. return 0;
  71. }
  72. return 1;
  73. }
  74. /* Find a match for INSN in the opcode table, given machine DIALECT.
  75. A DIALECT of -1 is special, matching all machine opcode variations. */
  76. static const struct powerpc_opcode *
  77. lookup_powerpc (unsigned long insn, ppc_cpu_t dialect)
  78. {
  79. const struct powerpc_opcode *opcode;
  80. const struct powerpc_opcode *opcode_end;
  81. opcode_end = powerpc_opcodes + powerpc_num_opcodes;
  82. /* Find the first match in the opcode table for this major opcode. */
  83. for (opcode = powerpc_opcodes; opcode < opcode_end; ++opcode)
  84. {
  85. const unsigned char *opindex;
  86. const struct powerpc_operand *operand;
  87. int invalid;
  88. if ((insn & opcode->mask) != opcode->opcode
  89. || (dialect != (ppc_cpu_t) -1
  90. && ((opcode->flags & dialect) == 0
  91. || (opcode->deprecated & dialect) != 0)))
  92. continue;
  93. /* Check validity of operands. */
  94. invalid = 0;
  95. for (opindex = opcode->operands; *opindex != 0; opindex++)
  96. {
  97. operand = powerpc_operands + *opindex;
  98. if (operand->extract)
  99. (*operand->extract) (insn, dialect, &invalid);
  100. }
  101. if (invalid)
  102. continue;
  103. return opcode;
  104. }
  105. return NULL;
  106. }
  107. /* Print a PowerPC or POWER instruction. */
  108. int print_insn_powerpc (unsigned long insn, unsigned long memaddr)
  109. {
  110. const struct powerpc_opcode *opcode;
  111. bool insn_is_short;
  112. ppc_cpu_t dialect;
  113. dialect = PPC_OPCODE_PPC | PPC_OPCODE_COMMON
  114. | PPC_OPCODE_64 | PPC_OPCODE_POWER4 | PPC_OPCODE_ALTIVEC;
  115. if (cpu_has_feature(CPU_FTRS_POWER5))
  116. dialect |= PPC_OPCODE_POWER5;
  117. if (cpu_has_feature(CPU_FTRS_CELL))
  118. dialect |= (PPC_OPCODE_CELL | PPC_OPCODE_ALTIVEC);
  119. if (cpu_has_feature(CPU_FTRS_POWER6))
  120. dialect |= (PPC_OPCODE_POWER5 | PPC_OPCODE_POWER6 | PPC_OPCODE_ALTIVEC);
  121. if (cpu_has_feature(CPU_FTRS_POWER7))
  122. dialect |= (PPC_OPCODE_POWER5 | PPC_OPCODE_POWER6 | PPC_OPCODE_POWER7
  123. | PPC_OPCODE_ALTIVEC | PPC_OPCODE_VSX);
  124. if (cpu_has_feature(CPU_FTRS_POWER8))
  125. dialect |= (PPC_OPCODE_POWER5 | PPC_OPCODE_POWER6 | PPC_OPCODE_POWER7
  126. | PPC_OPCODE_POWER8 | PPC_OPCODE_HTM
  127. | PPC_OPCODE_ALTIVEC | PPC_OPCODE_ALTIVEC2 | PPC_OPCODE_VSX);
  128. if (cpu_has_feature(CPU_FTRS_POWER9))
  129. dialect |= (PPC_OPCODE_POWER5 | PPC_OPCODE_POWER6 | PPC_OPCODE_POWER7
  130. | PPC_OPCODE_POWER8 | PPC_OPCODE_POWER9 | PPC_OPCODE_HTM
  131. | PPC_OPCODE_ALTIVEC | PPC_OPCODE_ALTIVEC2
  132. | PPC_OPCODE_VSX | PPC_OPCODE_VSX3),
  133. /* Get the major opcode of the insn. */
  134. opcode = NULL;
  135. insn_is_short = false;
  136. if (opcode == NULL)
  137. opcode = lookup_powerpc (insn, dialect);
  138. if (opcode == NULL && (dialect & PPC_OPCODE_ANY) != 0)
  139. opcode = lookup_powerpc (insn, (ppc_cpu_t) -1);
  140. if (opcode != NULL)
  141. {
  142. const unsigned char *opindex;
  143. const struct powerpc_operand *operand;
  144. int need_comma;
  145. int need_paren;
  146. int skip_optional;
  147. if (opcode->operands[0] != 0)
  148. printf("%-7s ", opcode->name);
  149. else
  150. printf("%s", opcode->name);
  151. if (insn_is_short)
  152. /* The operands will be fetched out of the 16-bit instruction. */
  153. insn >>= 16;
  154. /* Now extract and print the operands. */
  155. need_comma = 0;
  156. need_paren = 0;
  157. skip_optional = -1;
  158. for (opindex = opcode->operands; *opindex != 0; opindex++)
  159. {
  160. long value;
  161. operand = powerpc_operands + *opindex;
  162. /* Operands that are marked FAKE are simply ignored. We
  163. already made sure that the extract function considered
  164. the instruction to be valid. */
  165. if ((operand->flags & PPC_OPERAND_FAKE) != 0)
  166. continue;
  167. /* If all of the optional operands have the value zero,
  168. then don't print any of them. */
  169. if ((operand->flags & PPC_OPERAND_OPTIONAL) != 0)
  170. {
  171. if (skip_optional < 0)
  172. skip_optional = skip_optional_operands (opindex, insn,
  173. dialect);
  174. if (skip_optional)
  175. continue;
  176. }
  177. value = operand_value_powerpc (operand, insn, dialect);
  178. if (need_comma)
  179. {
  180. printf(",");
  181. need_comma = 0;
  182. }
  183. /* Print the operand as directed by the flags. */
  184. if ((operand->flags & PPC_OPERAND_GPR) != 0
  185. || ((operand->flags & PPC_OPERAND_GPR_0) != 0 && value != 0))
  186. printf("r%ld", value);
  187. else if ((operand->flags & PPC_OPERAND_FPR) != 0)
  188. printf("f%ld", value);
  189. else if ((operand->flags & PPC_OPERAND_VR) != 0)
  190. printf("v%ld", value);
  191. else if ((operand->flags & PPC_OPERAND_VSR) != 0)
  192. printf("vs%ld", value);
  193. else if ((operand->flags & PPC_OPERAND_RELATIVE) != 0)
  194. print_address(memaddr + value);
  195. else if ((operand->flags & PPC_OPERAND_ABSOLUTE) != 0)
  196. print_address(value & 0xffffffff);
  197. else if ((operand->flags & PPC_OPERAND_FSL) != 0)
  198. printf("fsl%ld", value);
  199. else if ((operand->flags & PPC_OPERAND_FCR) != 0)
  200. printf("fcr%ld", value);
  201. else if ((operand->flags & PPC_OPERAND_UDI) != 0)
  202. printf("%ld", value);
  203. else if ((operand->flags & PPC_OPERAND_CR_REG) != 0
  204. && (((dialect & PPC_OPCODE_PPC) != 0)
  205. || ((dialect & PPC_OPCODE_VLE) != 0)))
  206. printf("cr%ld", value);
  207. else if (((operand->flags & PPC_OPERAND_CR_BIT) != 0)
  208. && (((dialect & PPC_OPCODE_PPC) != 0)
  209. || ((dialect & PPC_OPCODE_VLE) != 0)))
  210. {
  211. static const char *cbnames[4] = { "lt", "gt", "eq", "so" };
  212. int cr;
  213. int cc;
  214. cr = value >> 2;
  215. if (cr != 0)
  216. printf("4*cr%d+", cr);
  217. cc = value & 3;
  218. printf("%s", cbnames[cc]);
  219. }
  220. else
  221. printf("%d", (int) value);
  222. if (need_paren)
  223. {
  224. printf(")");
  225. need_paren = 0;
  226. }
  227. if ((operand->flags & PPC_OPERAND_PARENS) == 0)
  228. need_comma = 1;
  229. else
  230. {
  231. printf("(");
  232. need_paren = 1;
  233. }
  234. }
  235. /* We have found and printed an instruction.
  236. If it was a short VLE instruction we have more to do. */
  237. if (insn_is_short)
  238. {
  239. memaddr += 2;
  240. return 2;
  241. }
  242. else
  243. /* Otherwise, return. */
  244. return 4;
  245. }
  246. /* We could not find a match. */
  247. printf(".long 0x%lx", insn);
  248. return 4;
  249. }