* lexsup.c (parse_args): Don't pass shortopts to second call to
[binutils.git] / opcodes / cgen-dis.c
blob4622c8c47c32e3ab994a680d65c98ef1d2a7a488
1 /* CGEN generic disassembler support code.
3 Copyright 1996, 1997, 1998, 1999, 2000, 2001
4 Free Software Foundation, Inc.
6 This file is part of the GNU Binutils and GDB, the GNU debugger.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 #include "sysdep.h"
23 #include <stdio.h>
24 #include "ansidecl.h"
25 #include "libiberty.h"
26 #include "bfd.h"
27 #include "symcat.h"
28 #include "opcode/cgen.h"
30 static CGEN_INSN_LIST * hash_insn_array PARAMS ((CGEN_CPU_DESC, const CGEN_INSN *, int, int, CGEN_INSN_LIST **, CGEN_INSN_LIST *));
31 static CGEN_INSN_LIST * hash_insn_list PARAMS ((CGEN_CPU_DESC, const CGEN_INSN_LIST *, CGEN_INSN_LIST **, CGEN_INSN_LIST *));
32 static void build_dis_hash_table PARAMS ((CGEN_CPU_DESC));
34 /* Return the number of decodable bits in this insn. */
35 static int
36 count_decodable_bits (insn)
37 const CGEN_INSN *insn;
39 unsigned mask = CGEN_INSN_BASE_MASK (insn);
40 int bits = 0;
41 int m;
42 for (m = 1; m != 0; m <<= 1)
44 if (mask & m)
45 ++bits;
47 return bits;
50 /* Add an instruction to the hash chain. */
51 static void
52 add_insn_to_hash_chain (hentbuf, insn, htable, hash)
53 CGEN_INSN_LIST *hentbuf;
54 const CGEN_INSN *insn;
55 CGEN_INSN_LIST **htable;
56 unsigned int hash;
58 CGEN_INSN_LIST *current_buf;
59 CGEN_INSN_LIST *previous_buf;
60 int insn_decodable_bits;
62 /* Add insns sorted by the number of decodable bits, in decreasing order.
63 This ensures that any insn which is a special case of another will be
64 checked first. */
65 insn_decodable_bits = count_decodable_bits (insn);
66 previous_buf = NULL;
67 for (current_buf = htable[hash]; current_buf != NULL;
68 current_buf = current_buf->next)
70 int current_decodable_bits = count_decodable_bits (current_buf->insn);
71 if (insn_decodable_bits >= current_decodable_bits)
72 break;
73 previous_buf = current_buf;
76 /* Now insert the new insn. */
77 hentbuf->insn = insn;
78 hentbuf->next = current_buf;
79 if (previous_buf == NULL)
80 htable[hash] = hentbuf;
81 else
82 previous_buf->next = hentbuf;
85 /* Subroutine of build_dis_hash_table to add INSNS to the hash table.
87 COUNT is the number of elements in INSNS.
88 ENTSIZE is sizeof (CGEN_IBASE) for the target.
89 ??? No longer used but leave in for now.
90 HTABLE points to the hash table.
91 HENTBUF is a pointer to sufficiently large buffer of hash entries.
92 The result is a pointer to the next entry to use.
94 The table is scanned backwards as additions are made to the front of the
95 list and we want earlier ones to be prefered. */
97 static CGEN_INSN_LIST *
98 hash_insn_array (cd, insns, count, entsize, htable, hentbuf)
99 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;
106 int big_p = CGEN_CPU_ENDIAN (cd) == CGEN_ENDIAN_BIG;
107 int i;
109 for (i = count - 1; i >= 0; --i, ++hentbuf)
111 unsigned int hash;
112 char buf [4];
113 unsigned long value;
114 const CGEN_INSN *insn = &insns[i];
116 if (! (* cd->dis_hash_p) (insn))
117 continue;
119 /* We don't know whether the target uses the buffer or the base insn
120 to hash on, so set both up. */
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);
131 return hentbuf;
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. */
138 static CGEN_INSN_LIST *
139 hash_insn_list (cd, insns, htable, hentbuf)
140 CGEN_CPU_DESC cd;
141 const CGEN_INSN_LIST *insns;
142 CGEN_INSN_LIST **htable;
143 CGEN_INSN_LIST *hentbuf;
145 int big_p = CGEN_CPU_ENDIAN (cd) == CGEN_ENDIAN_BIG;
146 const CGEN_INSN_LIST *ilist;
148 for (ilist = insns; ilist != NULL; ilist = ilist->next, ++ hentbuf)
150 unsigned int hash;
151 char buf[4];
152 unsigned long value;
154 if (! (* cd->dis_hash_p) (ilist->insn))
155 continue;
157 /* We don't know whether the target uses the buffer or the base insn
158 to hash on, so set both up. */
160 value = CGEN_INSN_BASE_VALUE (ilist->insn);
161 bfd_put_bits((bfd_vma) value,
162 buf,
163 CGEN_INSN_MASK_BITSIZE (ilist->insn),
164 big_p);
165 hash = (* cd->dis_hash) (buf, value);
166 add_insn_to_hash_chain (hentbuf, ilist->insn, htable, hash);
169 return hentbuf;
172 /* Build the disassembler instruction hash table. */
174 static void
175 build_dis_hash_table (cd)
176 CGEN_CPU_DESC cd;
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;
186 /* The space allocated for the hash table consists of two parts:
187 the hash table and the hash lists. */
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));
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. */
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);
206 /* Add compiled in macro-insns. */
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);
213 /* Add runtime added insns.
214 Later added insns will be prefered over earlier ones. */
216 hash_entry_buf = hash_insn_list (cd, insn_table->new_entries,
217 dis_hash_table, hash_entry_buf);
219 /* Add runtime added macro-insns. */
221 hash_insn_list (cd, macro_insn_table->new_entries,
222 dis_hash_table, hash_entry_buf);
224 cd->dis_hash_table = dis_hash_table;
225 cd->dis_hash_table_entries = dis_hash_table_entries;
228 /* Return the first entry in the hash list for INSN. */
230 CGEN_INSN_LIST *
231 cgen_dis_lookup_insn (cd, buf, value)
232 CGEN_CPU_DESC cd;
233 const char * buf;
234 CGEN_INSN_INT value;
236 unsigned int hash;
238 if (cd->dis_hash_table == NULL)
239 build_dis_hash_table (cd);
241 hash = (* cd->dis_hash) (buf, value);
243 return cd->dis_hash_table[hash];