spu-dis.c 7.3 KB

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