tilepro-dis.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /* tilepro-dis.c. Disassembly routines for the TILEPro architecture.
  2. Copyright (C) 2011-2022 Free Software Foundation, Inc.
  3. This file is part of the GNU opcodes library.
  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 3 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
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  15. MA 02110-1301, USA. */
  16. #include "sysdep.h"
  17. #include <stddef.h>
  18. #include <assert.h>
  19. #include "bfd.h"
  20. #include "elf/tilepro.h"
  21. #include "elf-bfd.h"
  22. #include "disassemble.h"
  23. #include "opcode/tilepro.h"
  24. #define TREG_ZERO 63
  25. static int
  26. contains_insn (tilepro_mnemonic expected_mnemonic,
  27. int expected_first_operand,
  28. int expected_second_operand,
  29. bfd_vma memaddr,
  30. int *last_operand_ret,
  31. disassemble_info *info)
  32. {
  33. struct tilepro_decoded_instruction
  34. decoded[TILEPRO_MAX_INSTRUCTIONS_PER_BUNDLE];
  35. bfd_byte opbuf[TILEPRO_BUNDLE_SIZE_IN_BYTES];
  36. int i, num_instructions;
  37. if ((*info->read_memory_func) (memaddr, opbuf,
  38. TILEPRO_BUNDLE_SIZE_IN_BYTES, info) != 0)
  39. /* If we cannot even read the memory, it obviously does not have the
  40. instruction for which we are looking. */
  41. return 0;
  42. /* Parse the instructions in the bundle. */
  43. num_instructions = parse_insn_tilepro (bfd_getl64 (opbuf), memaddr, decoded);
  44. for (i = 0; i < num_instructions; i++)
  45. {
  46. const struct tilepro_opcode *opcode = decoded[i].opcode;
  47. if (opcode->mnemonic != expected_mnemonic)
  48. continue;
  49. if (expected_first_operand != -1
  50. && decoded[i].operand_values[0] != expected_first_operand)
  51. continue;
  52. if (expected_second_operand != -1
  53. && decoded[i].operand_values[1] != expected_second_operand)
  54. continue;
  55. *last_operand_ret = decoded[i].operand_values[opcode->num_operands - 1];
  56. return 1;
  57. }
  58. /* No match. */
  59. return 0;
  60. }
  61. int
  62. print_insn_tilepro (bfd_vma memaddr, disassemble_info *info)
  63. {
  64. struct tilepro_decoded_instruction
  65. decoded[TILEPRO_MAX_INSTRUCTIONS_PER_BUNDLE];
  66. bfd_byte opbuf[TILEPRO_BUNDLE_SIZE_IN_BYTES];
  67. int status, i, num_instructions, num_printed;
  68. tilepro_mnemonic padding_mnemonic;
  69. status = (*info->read_memory_func) (memaddr, opbuf,
  70. TILEPRO_BUNDLE_SIZE_IN_BYTES, info);
  71. if (status != 0)
  72. {
  73. (*info->memory_error_func) (status, memaddr, info);
  74. return -1;
  75. }
  76. info->bytes_per_line = TILEPRO_BUNDLE_SIZE_IN_BYTES;
  77. info->bytes_per_chunk = TILEPRO_BUNDLE_SIZE_IN_BYTES;
  78. info->octets_per_byte = 1;
  79. info->display_endian = BFD_ENDIAN_LITTLE;
  80. /* Parse the instructions in the bundle. */
  81. num_instructions = parse_insn_tilepro (bfd_getl64 (opbuf), memaddr, decoded);
  82. /* Print the instructions in the bundle. */
  83. info->fprintf_func (info->stream, "{ ");
  84. num_printed = 0;
  85. /* Determine which nop opcode is used for padding and should be skipped. */
  86. padding_mnemonic = TILEPRO_OPC_FNOP;
  87. for (i = 0; i < num_instructions; i++)
  88. {
  89. if (!decoded[i].opcode->can_bundle)
  90. {
  91. /* Instructions that cannot be bundled are padded out with nops,
  92. rather than fnops. Displaying them is always clutter. */
  93. padding_mnemonic = TILEPRO_OPC_NOP;
  94. break;
  95. }
  96. }
  97. for (i = 0; i < num_instructions; i++)
  98. {
  99. const struct tilepro_opcode *opcode = decoded[i].opcode;
  100. const char *name;
  101. int j;
  102. /* Do not print out fnops, unless everything is an fnop, in
  103. which case we will print out just the last one. */
  104. if (opcode->mnemonic == padding_mnemonic
  105. && (num_printed > 0 || i + 1 < num_instructions))
  106. continue;
  107. if (num_printed > 0)
  108. info->fprintf_func (info->stream, " ; ");
  109. ++num_printed;
  110. name = opcode->name;
  111. if (name == NULL)
  112. name = "<invalid>";
  113. info->fprintf_func (info->stream, "%s", name);
  114. for (j = 0; j < opcode->num_operands; j++)
  115. {
  116. int num;
  117. const struct tilepro_operand *op;
  118. const char *spr_name;
  119. if (j > 0)
  120. info->fprintf_func (info->stream, ",");
  121. info->fprintf_func (info->stream, " ");
  122. num = decoded[i].operand_values[j];
  123. op = decoded[i].operands[j];
  124. switch (op->type)
  125. {
  126. case TILEPRO_OP_TYPE_REGISTER:
  127. info->fprintf_func (info->stream, "%s",
  128. tilepro_register_names[num]);
  129. break;
  130. case TILEPRO_OP_TYPE_SPR:
  131. spr_name = get_tilepro_spr_name(num);
  132. if (spr_name != NULL)
  133. info->fprintf_func (info->stream, "%s", spr_name);
  134. else
  135. info->fprintf_func (info->stream, "%d", num);
  136. break;
  137. case TILEPRO_OP_TYPE_IMMEDIATE:
  138. {
  139. bfd_vma addr = 0;
  140. int found_addr = 0;
  141. int addr_piece;
  142. switch (opcode->mnemonic)
  143. {
  144. case TILEPRO_OPC_ADDLI:
  145. if (contains_insn (TILEPRO_OPC_AULI,
  146. decoded[i].operand_values[1],
  147. TREG_ZERO,
  148. memaddr - TILEPRO_BUNDLE_SIZE_IN_BYTES,
  149. &addr_piece,
  150. info))
  151. {
  152. addr = num + (addr_piece << 16);
  153. found_addr = 1;
  154. }
  155. break;
  156. case TILEPRO_OPC_AULI:
  157. if (contains_insn (TILEPRO_OPC_MOVELI,
  158. decoded[i].operand_values[1],
  159. -1,
  160. memaddr - TILEPRO_BUNDLE_SIZE_IN_BYTES,
  161. &addr_piece,
  162. info))
  163. {
  164. addr = (num << 16) + addr_piece;
  165. found_addr = 1;
  166. }
  167. break;
  168. default:
  169. /* Operand does not look like a constructed address. */
  170. break;
  171. }
  172. info->fprintf_func (info->stream, "%d", num);
  173. if (found_addr)
  174. {
  175. info->fprintf_func (info->stream, " /* ");
  176. info->print_address_func (addr, info);
  177. info->fprintf_func (info->stream, " */");
  178. }
  179. }
  180. break;
  181. case TILEPRO_OP_TYPE_ADDRESS:
  182. info->print_address_func ((bfd_vma)(unsigned int) num, info);
  183. break;
  184. default:
  185. abort ();
  186. }
  187. }
  188. }
  189. info->fprintf_func (info->stream, " }");
  190. return TILEPRO_BUNDLE_SIZE_IN_BYTES;
  191. }