spu-dis.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /* Disassemble SPU instructions
  2. Copyright 2006 Free Software Foundation, Inc.
  3. This file is part of GDB, GAS, and the GNU binutils.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
  15. #include <linux/string.h>
  16. #include "nonstdio.h"
  17. #include "ansidecl.h"
  18. #include "spu.h"
  19. #include "dis-asm.h"
  20. /* This file provides a disassembler function which uses
  21. the disassembler interface defined in dis-asm.h. */
  22. extern const struct spu_opcode spu_opcodes[];
  23. extern const int spu_num_opcodes;
  24. #define SPU_DISASM_TBL_SIZE (1 << 11)
  25. static const struct spu_opcode *spu_disassemble_table[SPU_DISASM_TBL_SIZE];
  26. static void
  27. init_spu_disassemble (void)
  28. {
  29. int i;
  30. /* If two instructions have the same opcode then we prefer the first
  31. * one. In most cases it is just an alternate mnemonic. */
  32. for (i = 0; i < spu_num_opcodes; i++)
  33. {
  34. int o = spu_opcodes[i].opcode;
  35. if (o >= SPU_DISASM_TBL_SIZE)
  36. continue; /* abort (); */
  37. if (spu_disassemble_table[o] == 0)
  38. spu_disassemble_table[o] = &spu_opcodes[i];
  39. }
  40. }
  41. /* Determine the instruction from the 10 least significant bits. */
  42. static const struct spu_opcode *
  43. get_index_for_opcode (unsigned int insn)
  44. {
  45. const struct spu_opcode *index;
  46. unsigned int opcode = insn >> (32-11);
  47. /* Init the table. This assumes that element 0/opcode 0 (currently
  48. * NOP) is always used */
  49. if (spu_disassemble_table[0] == 0)
  50. init_spu_disassemble ();
  51. if ((index = spu_disassemble_table[opcode & 0x780]) != 0
  52. && index->insn_type == RRR)
  53. return index;
  54. if ((index = spu_disassemble_table[opcode & 0x7f0]) != 0
  55. && (index->insn_type == RI18 || index->insn_type == LBT))
  56. return index;
  57. if ((index = spu_disassemble_table[opcode & 0x7f8]) != 0
  58. && index->insn_type == RI10)
  59. return index;
  60. if ((index = spu_disassemble_table[opcode & 0x7fc]) != 0
  61. && (index->insn_type == RI16))
  62. return index;
  63. if ((index = spu_disassemble_table[opcode & 0x7fe]) != 0
  64. && (index->insn_type == RI8))
  65. return index;
  66. if ((index = spu_disassemble_table[opcode & 0x7ff]) != 0)
  67. return index;
  68. return NULL;
  69. }
  70. /* Print a Spu instruction. */
  71. int
  72. print_insn_spu (unsigned long insn, unsigned long memaddr)
  73. {
  74. int value;
  75. int hex_value;
  76. const struct spu_opcode *index;
  77. enum spu_insns tag;
  78. index = get_index_for_opcode (insn);
  79. if (index == 0)
  80. {
  81. printf(".long 0x%x", insn);
  82. }
  83. else
  84. {
  85. int i;
  86. int paren = 0;
  87. tag = (enum spu_insns)(index - spu_opcodes);
  88. printf("%s", index->mnemonic);
  89. if (tag == M_BI || tag == M_BISL || tag == M_IRET || tag == M_BISLED
  90. || tag == M_BIHNZ || tag == M_BIHZ || tag == M_BINZ || tag == M_BIZ
  91. || tag == M_SYNC || tag == M_HBR)
  92. {
  93. int fb = (insn >> (32-18)) & 0x7f;
  94. if (fb & 0x40)
  95. printf(tag == M_SYNC ? "c" : "p");
  96. if (fb & 0x20)
  97. printf("d");
  98. if (fb & 0x10)
  99. printf("e");
  100. }
  101. if (index->arg[0] != 0)
  102. printf("\t");
  103. hex_value = 0;
  104. for (i = 1; i <= index->arg[0]; i++)
  105. {
  106. int arg = index->arg[i];
  107. if (arg != A_P && !paren && i > 1)
  108. printf(",");
  109. switch (arg)
  110. {
  111. case A_T:
  112. printf("$%d",
  113. DECODE_INSN_RT (insn));
  114. break;
  115. case A_A:
  116. printf("$%d",
  117. DECODE_INSN_RA (insn));
  118. break;
  119. case A_B:
  120. printf("$%d",
  121. DECODE_INSN_RB (insn));
  122. break;
  123. case A_C:
  124. printf("$%d",
  125. DECODE_INSN_RC (insn));
  126. break;
  127. case A_S:
  128. printf("$sp%d",
  129. DECODE_INSN_RA (insn));
  130. break;
  131. case A_H:
  132. printf("$ch%d",
  133. DECODE_INSN_RA (insn));
  134. break;
  135. case A_P:
  136. paren++;
  137. printf("(");
  138. break;
  139. case A_U7A:
  140. printf("%d",
  141. 173 - DECODE_INSN_U8 (insn));
  142. break;
  143. case A_U7B:
  144. printf("%d",
  145. 155 - DECODE_INSN_U8 (insn));
  146. break;
  147. case A_S3:
  148. case A_S6:
  149. case A_S7:
  150. case A_S7N:
  151. case A_U3:
  152. case A_U5:
  153. case A_U6:
  154. case A_U7:
  155. hex_value = DECODE_INSN_I7 (insn);
  156. printf("%d", hex_value);
  157. break;
  158. case A_S11:
  159. print_address(memaddr + DECODE_INSN_I9a (insn) * 4);
  160. break;
  161. case A_S11I:
  162. print_address(memaddr + DECODE_INSN_I9b (insn) * 4);
  163. break;
  164. case A_S10:
  165. case A_S10B:
  166. hex_value = DECODE_INSN_I10 (insn);
  167. printf("%d", hex_value);
  168. break;
  169. case A_S14:
  170. hex_value = DECODE_INSN_I10 (insn) * 16;
  171. printf("%d", hex_value);
  172. break;
  173. case A_S16:
  174. hex_value = DECODE_INSN_I16 (insn);
  175. printf("%d", hex_value);
  176. break;
  177. case A_X16:
  178. hex_value = DECODE_INSN_U16 (insn);
  179. printf("%u", hex_value);
  180. break;
  181. case A_R18:
  182. value = DECODE_INSN_I16 (insn) * 4;
  183. if (value == 0)
  184. printf("%d", value);
  185. else
  186. {
  187. hex_value = memaddr + value;
  188. print_address(hex_value & 0x3ffff);
  189. }
  190. break;
  191. case A_S18:
  192. value = DECODE_INSN_U16 (insn) * 4;
  193. if (value == 0)
  194. printf("%d", value);
  195. else
  196. print_address(value);
  197. break;
  198. case A_U18:
  199. value = DECODE_INSN_U18 (insn);
  200. if (value == 0 || 1)
  201. {
  202. hex_value = value;
  203. printf("%u", value);
  204. }
  205. else
  206. print_address(value);
  207. break;
  208. case A_U14:
  209. hex_value = DECODE_INSN_U14 (insn);
  210. printf("%u", hex_value);
  211. break;
  212. }
  213. if (arg != A_P && paren)
  214. {
  215. printf(")");
  216. paren--;
  217. }
  218. }
  219. if (hex_value > 16)
  220. printf("\t# %x", hex_value);
  221. }
  222. return 4;
  223. }