]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - opcodes/cgen-dis.c
Update year range in copyright notice of binutils files
[thirdparty/binutils-gdb.git] / opcodes / cgen-dis.c
CommitLineData
252b5132 1/* CGEN generic disassembler support code.
250d07de 2 Copyright (C) 1996-2021 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
29static CGEN_INSN_LIST * hash_insn_array (CGEN_CPU_DESC, const CGEN_INSN *, int, int, CGEN_INSN_LIST **, CGEN_INSN_LIST *);
30static CGEN_INSN_LIST * hash_insn_list (CGEN_CPU_DESC, const CGEN_INSN_LIST *, CGEN_INSN_LIST **, CGEN_INSN_LIST *);
31static void build_dis_hash_table (CGEN_CPU_DESC);
32static int count_decodable_bits (const CGEN_INSN *);
33static 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. */
39static int
10e05405 40count_decodable_bits (const CGEN_INSN *insn)
1c009264
DB
41{
42 unsigned mask = CGEN_INSN_BASE_MASK (insn);
43 int bits = 0;
d8c823c8
MM
44 unsigned m;
45
1c009264
DB
46 for (m = 1; m != 0; m <<= 1)
47 {
48 if (mask & m)
49 ++bits;
50 }
51 return bits;
52}
53
43e65147 54/* Add an instruction to the hash chain. */
1c009264 55static void
10e05405
MM
56add_insn_to_hash_chain (CGEN_INSN_LIST *hentbuf,
57 const CGEN_INSN *insn,
58 CGEN_INSN_LIST **htable,
59 unsigned int hash)
1c009264
DB
60{
61 CGEN_INSN_LIST *current_buf;
62 CGEN_INSN_LIST *previous_buf;
63 int insn_decodable_bits;
64
65 /* Add insns sorted by the number of decodable bits, in decreasing order.
66 This ensures that any insn which is a special case of another will be
67 checked first. */
68 insn_decodable_bits = count_decodable_bits (insn);
69 previous_buf = NULL;
70 for (current_buf = htable[hash]; current_buf != NULL;
71 current_buf = current_buf->next)
72 {
73 int current_decodable_bits = count_decodable_bits (current_buf->insn);
74 if (insn_decodable_bits >= current_decodable_bits)
75 break;
76 previous_buf = current_buf;
77 }
78
79 /* Now insert the new insn. */
80 hentbuf->insn = insn;
81 hentbuf->next = current_buf;
82 if (previous_buf == NULL)
83 htable[hash] = hentbuf;
84 else
85 previous_buf->next = hentbuf;
86}
87
252b5132
RH
88/* Subroutine of build_dis_hash_table to add INSNS to the hash table.
89
90 COUNT is the number of elements in INSNS.
91 ENTSIZE is sizeof (CGEN_IBASE) for the target.
92 ??? No longer used but leave in for now.
93 HTABLE points to the hash table.
94 HENTBUF is a pointer to sufficiently large buffer of hash entries.
95 The result is a pointer to the next entry to use.
96
97 The table is scanned backwards as additions are made to the front of the
0cc79db2 98 list and we want earlier ones to be preferred. */
252b5132
RH
99
100static CGEN_INSN_LIST *
10e05405
MM
101hash_insn_array (CGEN_CPU_DESC cd,
102 const CGEN_INSN * insns,
103 int count,
104 int entsize ATTRIBUTE_UNUSED,
105 CGEN_INSN_LIST ** htable,
106 CGEN_INSN_LIST * hentbuf)
252b5132 107{
17310e56 108 int big_p = CGEN_CPU_INSN_ENDIAN (cd) == CGEN_ENDIAN_BIG;
252b5132
RH
109 int i;
110
111 for (i = count - 1; i >= 0; --i, ++hentbuf)
112 {
113 unsigned int hash;
d3d1cc7b 114 char buf [8];
252b5132
RH
115 unsigned long value;
116 const CGEN_INSN *insn = &insns[i];
d3d1cc7b 117 size_t size;
252b5132
RH
118
119 if (! (* cd->dis_hash_p) (insn))
120 continue;
121
122 /* We don't know whether the target uses the buffer or the base insn
123 to hash on, so set both up. */
124
125 value = CGEN_INSN_BASE_VALUE (insn);
d3d1cc7b
AM
126 size = CGEN_INSN_MASK_BITSIZE (insn);
127 OPCODES_ASSERT (size <= sizeof (buf) * 8);
128 bfd_put_bits ((bfd_vma) value, buf, size, big_p);
252b5132 129 hash = (* cd->dis_hash) (buf, value);
1c009264 130 add_insn_to_hash_chain (hentbuf, insn, htable, hash);
252b5132
RH
131 }
132
133 return hentbuf;
134}
135
136/* Subroutine of build_dis_hash_table to add INSNS to the hash table.
137 This function is identical to hash_insn_array except the insns are
138 in a list. */
139
140static CGEN_INSN_LIST *
10e05405
MM
141hash_insn_list (CGEN_CPU_DESC cd,
142 const CGEN_INSN_LIST *insns,
143 CGEN_INSN_LIST **htable,
144 CGEN_INSN_LIST *hentbuf)
252b5132 145{
17310e56 146 int big_p = CGEN_CPU_INSN_ENDIAN (cd) == CGEN_ENDIAN_BIG;
252b5132
RH
147 const CGEN_INSN_LIST *ilist;
148
149 for (ilist = insns; ilist != NULL; ilist = ilist->next, ++ hentbuf)
150 {
151 unsigned int hash;
152 char buf[4];
153 unsigned long value;
154
155 if (! (* cd->dis_hash_p) (ilist->insn))
156 continue;
157
158 /* We don't know whether the target uses the buffer or the base insn
159 to hash on, so set both up. */
160
161 value = CGEN_INSN_BASE_VALUE (ilist->insn);
aed80dae
FCE
162 bfd_put_bits((bfd_vma) value,
163 buf,
164 CGEN_INSN_MASK_BITSIZE (ilist->insn),
165 big_p);
252b5132 166 hash = (* cd->dis_hash) (buf, value);
1c009264 167 add_insn_to_hash_chain (hentbuf, ilist->insn, htable, hash);
252b5132
RH
168 }
169
170 return hentbuf;
171}
172
173/* Build the disassembler instruction hash table. */
174
175static void
10e05405 176build_dis_hash_table (CGEN_CPU_DESC cd)
252b5132
RH
177{
178 int count = cgen_insn_count (cd) + cgen_macro_insn_count (cd);
179 CGEN_INSN_TABLE *insn_table = & cd->insn_table;
180 CGEN_INSN_TABLE *macro_insn_table = & cd->macro_insn_table;
181 unsigned int hash_size = cd->dis_hash_size;
182 CGEN_INSN_LIST *hash_entry_buf;
183 CGEN_INSN_LIST **dis_hash_table;
184 CGEN_INSN_LIST *dis_hash_table_entries;
185
186 /* The space allocated for the hash table consists of two parts:
187 the hash table and the hash lists. */
188
189 dis_hash_table = (CGEN_INSN_LIST **)
190 xmalloc (hash_size * sizeof (CGEN_INSN_LIST *));
191 memset (dis_hash_table, 0, hash_size * sizeof (CGEN_INSN_LIST *));
192 dis_hash_table_entries = hash_entry_buf = (CGEN_INSN_LIST *)
193 xmalloc (count * sizeof (CGEN_INSN_LIST));
194
195 /* Add compiled in insns.
196 Don't include the first one as it is a reserved entry. */
197 /* ??? It was the end of all hash chains, and also the special
198 "invalid insn" marker. May be able to do it differently now. */
199
200 hash_entry_buf = hash_insn_array (cd,
201 insn_table->init_entries + 1,
202 insn_table->num_init_entries - 1,
203 insn_table->entry_size,
204 dis_hash_table, hash_entry_buf);
205
206 /* Add compiled in macro-insns. */
207
208 hash_entry_buf = hash_insn_array (cd, macro_insn_table->init_entries,
209 macro_insn_table->num_init_entries,
210 macro_insn_table->entry_size,
211 dis_hash_table, hash_entry_buf);
212
213 /* Add runtime added insns.
0cc79db2 214 Later added insns will be preferred over earlier ones. */
252b5132
RH
215
216 hash_entry_buf = hash_insn_list (cd, insn_table->new_entries,
217 dis_hash_table, hash_entry_buf);
218
219 /* Add runtime added macro-insns. */
220
221 hash_insn_list (cd, macro_insn_table->new_entries,
222 dis_hash_table, hash_entry_buf);
223
224 cd->dis_hash_table = dis_hash_table;
225 cd->dis_hash_table_entries = dis_hash_table_entries;
226}
227
228/* Return the first entry in the hash list for INSN. */
229
230CGEN_INSN_LIST *
10e05405 231cgen_dis_lookup_insn (CGEN_CPU_DESC cd, const char * buf, CGEN_INSN_INT value)
252b5132
RH
232{
233 unsigned int hash;
234
235 if (cd->dis_hash_table == NULL)
236 build_dis_hash_table (cd);
237
238 hash = (* cd->dis_hash) (buf, value);
239
240 return cd->dis_hash_table[hash];
241}