cgen-dis.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* CGEN generic disassembler support code.
  2. Copyright (C) 1996-2022 Free Software Foundation, Inc.
  3. This file is part of libopcodes.
  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 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 "sysdep.h"
  16. #include <stdio.h>
  17. #include "ansidecl.h"
  18. #include "libiberty.h"
  19. #include "bfd.h"
  20. #include "symcat.h"
  21. #include "opcode/cgen.h"
  22. #include "disassemble.h"
  23. static CGEN_INSN_LIST * hash_insn_array (CGEN_CPU_DESC, const CGEN_INSN *, int, int, CGEN_INSN_LIST **, CGEN_INSN_LIST *);
  24. static CGEN_INSN_LIST * hash_insn_list (CGEN_CPU_DESC, const CGEN_INSN_LIST *, CGEN_INSN_LIST **, CGEN_INSN_LIST *);
  25. static void build_dis_hash_table (CGEN_CPU_DESC);
  26. static int count_decodable_bits (const CGEN_INSN *);
  27. static void add_insn_to_hash_chain (CGEN_INSN_LIST *,
  28. const CGEN_INSN *,
  29. CGEN_INSN_LIST **,
  30. unsigned int);
  31. /* Return the number of decodable bits in this insn. */
  32. static int
  33. count_decodable_bits (const CGEN_INSN *insn)
  34. {
  35. unsigned mask = CGEN_INSN_BASE_MASK (insn);
  36. #if GCC_VERSION >= 3004
  37. return __builtin_popcount (mask);
  38. #else
  39. int bits = 0;
  40. unsigned m;
  41. for (m = 1; m != 0; m <<= 1)
  42. {
  43. if (mask & m)
  44. ++bits;
  45. }
  46. return bits;
  47. #endif
  48. }
  49. /* Add an instruction to the hash chain. */
  50. static void
  51. add_insn_to_hash_chain (CGEN_INSN_LIST *hentbuf,
  52. const CGEN_INSN *insn,
  53. CGEN_INSN_LIST **htable,
  54. unsigned int hash)
  55. {
  56. CGEN_INSN_LIST *current_buf;
  57. CGEN_INSN_LIST *previous_buf;
  58. int insn_decodable_bits;
  59. /* Add insns sorted by the number of decodable bits, in decreasing order.
  60. This ensures that any insn which is a special case of another will be
  61. checked first. */
  62. insn_decodable_bits = count_decodable_bits (insn);
  63. previous_buf = NULL;
  64. for (current_buf = htable[hash]; current_buf != NULL;
  65. current_buf = current_buf->next)
  66. {
  67. int current_decodable_bits = count_decodable_bits (current_buf->insn);
  68. if (insn_decodable_bits >= current_decodable_bits)
  69. break;
  70. previous_buf = current_buf;
  71. }
  72. /* Now insert the new insn. */
  73. hentbuf->insn = insn;
  74. hentbuf->next = current_buf;
  75. if (previous_buf == NULL)
  76. htable[hash] = hentbuf;
  77. else
  78. previous_buf->next = hentbuf;
  79. }
  80. /* Subroutine of build_dis_hash_table to add INSNS to the hash table.
  81. COUNT is the number of elements in INSNS.
  82. ENTSIZE is sizeof (CGEN_IBASE) for the target.
  83. ??? No longer used but leave in for now.
  84. HTABLE points to the hash table.
  85. HENTBUF is a pointer to sufficiently large buffer of hash entries.
  86. The result is a pointer to the next entry to use.
  87. The table is scanned backwards as additions are made to the front of the
  88. list and we want earlier ones to be preferred. */
  89. static CGEN_INSN_LIST *
  90. hash_insn_array (CGEN_CPU_DESC cd,
  91. const CGEN_INSN * insns,
  92. int count,
  93. int entsize ATTRIBUTE_UNUSED,
  94. CGEN_INSN_LIST ** htable,
  95. CGEN_INSN_LIST * hentbuf)
  96. {
  97. int big_p = CGEN_CPU_INSN_ENDIAN (cd) == CGEN_ENDIAN_BIG;
  98. int i;
  99. for (i = count - 1; i >= 0; --i, ++hentbuf)
  100. {
  101. unsigned int hash;
  102. char buf [8];
  103. unsigned long value;
  104. const CGEN_INSN *insn = &insns[i];
  105. size_t size;
  106. if (! (* cd->dis_hash_p) (insn))
  107. continue;
  108. /* We don't know whether the target uses the buffer or the base insn
  109. to hash on, so set both up. */
  110. value = CGEN_INSN_BASE_VALUE (insn);
  111. size = CGEN_INSN_MASK_BITSIZE (insn);
  112. OPCODES_ASSERT (size <= sizeof (buf) * 8);
  113. bfd_put_bits ((bfd_vma) value, buf, size, big_p);
  114. hash = (* cd->dis_hash) (buf, value);
  115. add_insn_to_hash_chain (hentbuf, insn, htable, hash);
  116. }
  117. return hentbuf;
  118. }
  119. /* Subroutine of build_dis_hash_table to add INSNS to the hash table.
  120. This function is identical to hash_insn_array except the insns are
  121. in a list. */
  122. static CGEN_INSN_LIST *
  123. hash_insn_list (CGEN_CPU_DESC cd,
  124. const CGEN_INSN_LIST *insns,
  125. CGEN_INSN_LIST **htable,
  126. CGEN_INSN_LIST *hentbuf)
  127. {
  128. int big_p = CGEN_CPU_INSN_ENDIAN (cd) == CGEN_ENDIAN_BIG;
  129. const CGEN_INSN_LIST *ilist;
  130. for (ilist = insns; ilist != NULL; ilist = ilist->next, ++ hentbuf)
  131. {
  132. unsigned int hash;
  133. char buf[4];
  134. unsigned long value;
  135. if (! (* cd->dis_hash_p) (ilist->insn))
  136. continue;
  137. /* We don't know whether the target uses the buffer or the base insn
  138. to hash on, so set both up. */
  139. value = CGEN_INSN_BASE_VALUE (ilist->insn);
  140. bfd_put_bits((bfd_vma) value,
  141. buf,
  142. CGEN_INSN_MASK_BITSIZE (ilist->insn),
  143. big_p);
  144. hash = (* cd->dis_hash) (buf, value);
  145. add_insn_to_hash_chain (hentbuf, ilist->insn, htable, hash);
  146. }
  147. return hentbuf;
  148. }
  149. /* Build the disassembler instruction hash table. */
  150. static void
  151. build_dis_hash_table (CGEN_CPU_DESC cd)
  152. {
  153. int count = cgen_insn_count (cd) + cgen_macro_insn_count (cd);
  154. CGEN_INSN_TABLE *insn_table = & cd->insn_table;
  155. CGEN_INSN_TABLE *macro_insn_table = & cd->macro_insn_table;
  156. unsigned int hash_size = cd->dis_hash_size;
  157. CGEN_INSN_LIST *hash_entry_buf;
  158. CGEN_INSN_LIST **dis_hash_table;
  159. CGEN_INSN_LIST *dis_hash_table_entries;
  160. /* The space allocated for the hash table consists of two parts:
  161. the hash table and the hash lists. */
  162. dis_hash_table = (CGEN_INSN_LIST **)
  163. xmalloc (hash_size * sizeof (CGEN_INSN_LIST *));
  164. memset (dis_hash_table, 0, hash_size * sizeof (CGEN_INSN_LIST *));
  165. dis_hash_table_entries = hash_entry_buf = (CGEN_INSN_LIST *)
  166. xmalloc (count * sizeof (CGEN_INSN_LIST));
  167. /* Add compiled in insns.
  168. Don't include the first one as it is a reserved entry. */
  169. /* ??? It was the end of all hash chains, and also the special
  170. "invalid insn" marker. May be able to do it differently now. */
  171. hash_entry_buf = hash_insn_array (cd,
  172. insn_table->init_entries + 1,
  173. insn_table->num_init_entries - 1,
  174. insn_table->entry_size,
  175. dis_hash_table, hash_entry_buf);
  176. /* Add compiled in macro-insns. */
  177. hash_entry_buf = hash_insn_array (cd, macro_insn_table->init_entries,
  178. macro_insn_table->num_init_entries,
  179. macro_insn_table->entry_size,
  180. dis_hash_table, hash_entry_buf);
  181. /* Add runtime added insns.
  182. Later added insns will be preferred over earlier ones. */
  183. hash_entry_buf = hash_insn_list (cd, insn_table->new_entries,
  184. dis_hash_table, hash_entry_buf);
  185. /* Add runtime added macro-insns. */
  186. hash_insn_list (cd, macro_insn_table->new_entries,
  187. dis_hash_table, hash_entry_buf);
  188. cd->dis_hash_table = dis_hash_table;
  189. cd->dis_hash_table_entries = dis_hash_table_entries;
  190. }
  191. /* Return the first entry in the hash list for INSN. */
  192. CGEN_INSN_LIST *
  193. cgen_dis_lookup_insn (CGEN_CPU_DESC cd, const char * buf, CGEN_INSN_INT value)
  194. {
  195. unsigned int hash;
  196. if (cd->dis_hash_table == NULL)
  197. build_dis_hash_table (cd);
  198. hash = (* cd->dis_hash) (buf, value);
  199. return cd->dis_hash_table[hash];
  200. }