]>
| Commit | Line | Data |
|---|---|---|
| 252b5132 | 1 | /* CGEN generic disassembler support code. |
| e8e7cf2a | 2 | Copyright (C) 1996-2025 Free Software Foundation, Inc. |
| 252b5132 | 3 | |
| 9b201bb5 | 4 | This file is part of libopcodes. |
| 252b5132 | 5 | |
| 9b201bb5 | 6 | This library is free software; you can redistribute it and/or modify |
| 252b5132 | 7 | it under the terms of the GNU General Public License as published by |
| 9b201bb5 | 8 | the Free Software Foundation; either version 3, or (at your option) |
| 252b5132 RH |
9 | any later version. |
| 10 | ||
| 9b201bb5 NC |
11 | It is distributed in the hope that it will be useful, but WITHOUT |
| 12 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | |
| 13 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public | |
| 14 | License for more details. | |
| 252b5132 RH |
15 | |
| 16 | You should have received a copy of the GNU General Public License along | |
| 17 | with this program; if not, write to the Free Software Foundation, Inc., | |
| f4321104 | 18 | 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ |
| 252b5132 RH |
19 | |
| 20 | #include "sysdep.h" | |
| 21 | #include <stdio.h> | |
| 22 | #include "ansidecl.h" | |
| 23 | #include "libiberty.h" | |
| 24 | #include "bfd.h" | |
| 25 | #include "symcat.h" | |
| 26 | #include "opcode/cgen.h" | |
| d3d1cc7b | 27 | #include "disassemble.h" |
| 252b5132 | 28 | |
| 10e05405 MM |
29 | static CGEN_INSN_LIST * hash_insn_array (CGEN_CPU_DESC, const CGEN_INSN *, int, int, CGEN_INSN_LIST **, CGEN_INSN_LIST *); |
| 30 | static CGEN_INSN_LIST * hash_insn_list (CGEN_CPU_DESC, const CGEN_INSN_LIST *, CGEN_INSN_LIST **, CGEN_INSN_LIST *); | |
| 31 | static void build_dis_hash_table (CGEN_CPU_DESC); | |
| 32 | static int count_decodable_bits (const CGEN_INSN *); | |
| 33 | static void add_insn_to_hash_chain (CGEN_INSN_LIST *, | |
| 34 | const CGEN_INSN *, | |
| 35 | CGEN_INSN_LIST **, | |
| 36 | unsigned int); | |
| 1c009264 DB |
37 | |
| 38 | /* Return the number of decodable bits in this insn. */ | |
| 39 | static int | |
| 10e05405 | 40 | count_decodable_bits (const CGEN_INSN *insn) |
| 1c009264 | 41 | { |
| 7f6ebecd | 42 | CGEN_INSN_LGUINT mask = CGEN_INSN_BASE_MASK (insn); |
| 46b8b3d6 AB |
43 | #if GCC_VERSION >= 3004 |
| 44 | return __builtin_popcount (mask); | |
| 45 | #else | |
| 1c009264 | 46 | int bits = 0; |
| d8c823c8 MM |
47 | unsigned m; |
| 48 | ||
| 1c009264 DB |
49 | for (m = 1; m != 0; m <<= 1) |
| 50 | { | |
| 51 | if (mask & m) | |
| 52 | ++bits; | |
| 53 | } | |
| 54 | return bits; | |
| 46b8b3d6 | 55 | #endif |
| 1c009264 DB |
56 | } |
| 57 | ||
| 43e65147 | 58 | /* Add an instruction to the hash chain. */ |
| 1c009264 | 59 | static void |
| 10e05405 MM |
60 | add_insn_to_hash_chain (CGEN_INSN_LIST *hentbuf, |
| 61 | const CGEN_INSN *insn, | |
| 62 | CGEN_INSN_LIST **htable, | |
| 63 | unsigned int hash) | |
| 1c009264 DB |
64 | { |
| 65 | CGEN_INSN_LIST *current_buf; | |
| 66 | CGEN_INSN_LIST *previous_buf; | |
| 67 | int insn_decodable_bits; | |
| 68 | ||
| 69 | /* Add insns sorted by the number of decodable bits, in decreasing order. | |
| 70 | This ensures that any insn which is a special case of another will be | |
| 71 | checked first. */ | |
| 72 | insn_decodable_bits = count_decodable_bits (insn); | |
| 73 | previous_buf = NULL; | |
| 74 | for (current_buf = htable[hash]; current_buf != NULL; | |
| 75 | current_buf = current_buf->next) | |
| 76 | { | |
| 77 | int current_decodable_bits = count_decodable_bits (current_buf->insn); | |
| 78 | if (insn_decodable_bits >= current_decodable_bits) | |
| 79 | break; | |
| 80 | previous_buf = current_buf; | |
| 81 | } | |
| 82 | ||
| 83 | /* Now insert the new insn. */ | |
| 84 | hentbuf->insn = insn; | |
| 85 | hentbuf->next = current_buf; | |
| 86 | if (previous_buf == NULL) | |
| 87 | htable[hash] = hentbuf; | |
| 88 | else | |
| 89 | previous_buf->next = hentbuf; | |
| 90 | } | |
| 91 | ||
| 252b5132 RH |
92 | /* Subroutine of build_dis_hash_table to add INSNS to the hash table. |
| 93 | ||
| 94 | COUNT is the number of elements in INSNS. | |
| 95 | ENTSIZE is sizeof (CGEN_IBASE) for the target. | |
| 96 | ??? No longer used but leave in for now. | |
| 97 | HTABLE points to the hash table. | |
| 98 | HENTBUF is a pointer to sufficiently large buffer of hash entries. | |
| 99 | The result is a pointer to the next entry to use. | |
| 100 | ||
| 101 | The table is scanned backwards as additions are made to the front of the | |
| 0cc79db2 | 102 | list and we want earlier ones to be preferred. */ |
| 252b5132 RH |
103 | |
| 104 | static CGEN_INSN_LIST * | |
| 10e05405 MM |
105 | hash_insn_array (CGEN_CPU_DESC cd, |
| 106 | const CGEN_INSN * insns, | |
| 107 | int count, | |
| 108 | int entsize ATTRIBUTE_UNUSED, | |
| 109 | CGEN_INSN_LIST ** htable, | |
| 110 | CGEN_INSN_LIST * hentbuf) | |
| 252b5132 | 111 | { |
| 17310e56 | 112 | int big_p = CGEN_CPU_INSN_ENDIAN (cd) == CGEN_ENDIAN_BIG; |
| 252b5132 RH |
113 | int i; |
| 114 | ||
| 115 | for (i = count - 1; i >= 0; --i, ++hentbuf) | |
| 116 | { | |
| 117 | unsigned int hash; | |
| d3d1cc7b | 118 | char buf [8]; |
| 252b5132 RH |
119 | unsigned long value; |
| 120 | const CGEN_INSN *insn = &insns[i]; | |
| d3d1cc7b | 121 | size_t size; |
| 252b5132 RH |
122 | |
| 123 | if (! (* cd->dis_hash_p) (insn)) | |
| 124 | continue; | |
| 125 | ||
| 126 | /* We don't know whether the target uses the buffer or the base insn | |
| 127 | to hash on, so set both up. */ | |
| 128 | ||
| 129 | value = CGEN_INSN_BASE_VALUE (insn); | |
| d3d1cc7b AM |
130 | size = CGEN_INSN_MASK_BITSIZE (insn); |
| 131 | OPCODES_ASSERT (size <= sizeof (buf) * 8); | |
| 132 | bfd_put_bits ((bfd_vma) value, buf, size, big_p); | |
| 252b5132 | 133 | hash = (* cd->dis_hash) (buf, value); |
| 1c009264 | 134 | add_insn_to_hash_chain (hentbuf, insn, htable, hash); |
| 252b5132 RH |
135 | } |
| 136 | ||
| 137 | return hentbuf; | |
| 138 | } | |
| 139 | ||
| 140 | /* Subroutine of build_dis_hash_table to add INSNS to the hash table. | |
| 141 | This function is identical to hash_insn_array except the insns are | |
| 142 | in a list. */ | |
| 143 | ||
| 144 | static CGEN_INSN_LIST * | |
| 10e05405 MM |
145 | hash_insn_list (CGEN_CPU_DESC cd, |
| 146 | const CGEN_INSN_LIST *insns, | |
| 147 | CGEN_INSN_LIST **htable, | |
| 148 | CGEN_INSN_LIST *hentbuf) | |
| 252b5132 | 149 | { |
| 17310e56 | 150 | int big_p = CGEN_CPU_INSN_ENDIAN (cd) == CGEN_ENDIAN_BIG; |
| 252b5132 RH |
151 | const CGEN_INSN_LIST *ilist; |
| 152 | ||
| 153 | for (ilist = insns; ilist != NULL; ilist = ilist->next, ++ hentbuf) | |
| 154 | { | |
| 155 | unsigned int hash; | |
| 5e39ef33 | 156 | char buf[8]; |
| 252b5132 | 157 | unsigned long value; |
| 5e39ef33 | 158 | size_t size; |
| 252b5132 RH |
159 | |
| 160 | if (! (* cd->dis_hash_p) (ilist->insn)) | |
| 161 | continue; | |
| 162 | ||
| 163 | /* We don't know whether the target uses the buffer or the base insn | |
| 164 | to hash on, so set both up. */ | |
| 165 | ||
| 166 | value = CGEN_INSN_BASE_VALUE (ilist->insn); | |
| 5e39ef33 AM |
167 | size = CGEN_INSN_MASK_BITSIZE (ilist->insn); |
| 168 | OPCODES_ASSERT (size <= sizeof (buf) * 8); | |
| 169 | bfd_put_bits ((bfd_vma) value, buf, size, big_p); | |
| 252b5132 | 170 | hash = (* cd->dis_hash) (buf, value); |
| 1c009264 | 171 | add_insn_to_hash_chain (hentbuf, ilist->insn, htable, hash); |
| 252b5132 RH |
172 | } |
| 173 | ||
| 174 | return hentbuf; | |
| 175 | } | |
| 176 | ||
| 177 | /* Build the disassembler instruction hash table. */ | |
| 178 | ||
| 179 | static void | |
| 10e05405 | 180 | build_dis_hash_table (CGEN_CPU_DESC cd) |
| 252b5132 RH |
181 | { |
| 182 | int count = cgen_insn_count (cd) + cgen_macro_insn_count (cd); | |
| 183 | CGEN_INSN_TABLE *insn_table = & cd->insn_table; | |
| 184 | CGEN_INSN_TABLE *macro_insn_table = & cd->macro_insn_table; | |
| 185 | unsigned int hash_size = cd->dis_hash_size; | |
| 186 | CGEN_INSN_LIST *hash_entry_buf; | |
| 187 | CGEN_INSN_LIST **dis_hash_table; | |
| 188 | CGEN_INSN_LIST *dis_hash_table_entries; | |
| 189 | ||
| 190 | /* The space allocated for the hash table consists of two parts: | |
| 191 | the hash table and the hash lists. */ | |
| 192 | ||
| 193 | dis_hash_table = (CGEN_INSN_LIST **) | |
| 194 | xmalloc (hash_size * sizeof (CGEN_INSN_LIST *)); | |
| 195 | memset (dis_hash_table, 0, hash_size * sizeof (CGEN_INSN_LIST *)); | |
| 196 | dis_hash_table_entries = hash_entry_buf = (CGEN_INSN_LIST *) | |
| 197 | xmalloc (count * sizeof (CGEN_INSN_LIST)); | |
| 198 | ||
| 199 | /* Add compiled in insns. | |
| 200 | Don't include the first one as it is a reserved entry. */ | |
| 201 | /* ??? It was the end of all hash chains, and also the special | |
| 202 | "invalid insn" marker. May be able to do it differently now. */ | |
| 203 | ||
| 204 | hash_entry_buf = hash_insn_array (cd, | |
| 205 | insn_table->init_entries + 1, | |
| 206 | insn_table->num_init_entries - 1, | |
| 207 | insn_table->entry_size, | |
| 208 | dis_hash_table, hash_entry_buf); | |
| 209 | ||
| 210 | /* Add compiled in macro-insns. */ | |
| 211 | ||
| 212 | hash_entry_buf = hash_insn_array (cd, macro_insn_table->init_entries, | |
| 213 | macro_insn_table->num_init_entries, | |
| 214 | macro_insn_table->entry_size, | |
| 215 | dis_hash_table, hash_entry_buf); | |
| 216 | ||
| 217 | /* Add runtime added insns. | |
| 0cc79db2 | 218 | Later added insns will be preferred over earlier ones. */ |
| 252b5132 RH |
219 | |
| 220 | hash_entry_buf = hash_insn_list (cd, insn_table->new_entries, | |
| 221 | dis_hash_table, hash_entry_buf); | |
| 222 | ||
| 223 | /* Add runtime added macro-insns. */ | |
| 224 | ||
| 225 | hash_insn_list (cd, macro_insn_table->new_entries, | |
| 226 | dis_hash_table, hash_entry_buf); | |
| 227 | ||
| 228 | cd->dis_hash_table = dis_hash_table; | |
| 229 | cd->dis_hash_table_entries = dis_hash_table_entries; | |
| 230 | } | |
| 231 | ||
| 232 | /* Return the first entry in the hash list for INSN. */ | |
| 233 | ||
| 234 | CGEN_INSN_LIST * | |
| 10e05405 | 235 | cgen_dis_lookup_insn (CGEN_CPU_DESC cd, const char * buf, CGEN_INSN_INT value) |
| 252b5132 RH |
236 | { |
| 237 | unsigned int hash; | |
| 238 | ||
| 239 | if (cd->dis_hash_table == NULL) | |
| 240 | build_dis_hash_table (cd); | |
| 241 | ||
| 242 | hash = (* cd->dis_hash) (buf, value); | |
| 243 | ||
| 244 | return cd->dis_hash_table[hash]; | |
| 245 | } |