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