Commit | Line | Data |
---|---|---|
252b5132 RH |
1 | /* CGEN generic disassembler support code. |
2 | ||
060d22b0 NC |
3 | Copyright 1996, 1997, 1998, 1999, 2000, 2001 |
4 | Free Software Foundation, Inc. | |
252b5132 RH |
5 | |
6 | This file is part of the GNU Binutils and GDB, the GNU debugger. | |
7 | ||
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. | |
12 | ||
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. | |
17 | ||
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. */ | |
21 | ||
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" | |
29 | ||
d5b2f4d6 NC |
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)); | |
33 | ||
252b5132 RH |
34 | /* Subroutine of build_dis_hash_table to add INSNS to the hash table. |
35 | ||
36 | COUNT is the number of elements in INSNS. | |
37 | ENTSIZE is sizeof (CGEN_IBASE) for the target. | |
38 | ??? No longer used but leave in for now. | |
39 | HTABLE points to the hash table. | |
40 | HENTBUF is a pointer to sufficiently large buffer of hash entries. | |
41 | The result is a pointer to the next entry to use. | |
42 | ||
43 | The table is scanned backwards as additions are made to the front of the | |
44 | list and we want earlier ones to be prefered. */ | |
45 | ||
46 | static CGEN_INSN_LIST * | |
47 | hash_insn_array (cd, insns, count, entsize, htable, hentbuf) | |
48 | CGEN_CPU_DESC cd; | |
49 | const CGEN_INSN * insns; | |
50 | int count; | |
510925d3 | 51 | int entsize ATTRIBUTE_UNUSED; |
252b5132 RH |
52 | CGEN_INSN_LIST ** htable; |
53 | CGEN_INSN_LIST * hentbuf; | |
54 | { | |
55 | int big_p = CGEN_CPU_ENDIAN (cd) == CGEN_ENDIAN_BIG; | |
56 | int i; | |
57 | ||
58 | for (i = count - 1; i >= 0; --i, ++hentbuf) | |
59 | { | |
60 | unsigned int hash; | |
61 | char buf [4]; | |
62 | unsigned long value; | |
63 | const CGEN_INSN *insn = &insns[i]; | |
64 | ||
65 | if (! (* cd->dis_hash_p) (insn)) | |
66 | continue; | |
67 | ||
68 | /* We don't know whether the target uses the buffer or the base insn | |
69 | to hash on, so set both up. */ | |
70 | ||
71 | value = CGEN_INSN_BASE_VALUE (insn); | |
aed80dae FCE |
72 | bfd_put_bits ((bfd_vma) value, |
73 | buf, | |
74 | CGEN_INSN_MASK_BITSIZE (insn), | |
75 | big_p); | |
252b5132 RH |
76 | hash = (* cd->dis_hash) (buf, value); |
77 | hentbuf->next = htable[hash]; | |
78 | hentbuf->insn = insn; | |
79 | htable[hash] = hentbuf; | |
80 | } | |
81 | ||
82 | return hentbuf; | |
83 | } | |
84 | ||
85 | /* Subroutine of build_dis_hash_table to add INSNS to the hash table. | |
86 | This function is identical to hash_insn_array except the insns are | |
87 | in a list. */ | |
88 | ||
89 | static CGEN_INSN_LIST * | |
90 | hash_insn_list (cd, insns, htable, hentbuf) | |
91 | CGEN_CPU_DESC cd; | |
92 | const CGEN_INSN_LIST *insns; | |
93 | CGEN_INSN_LIST **htable; | |
94 | CGEN_INSN_LIST *hentbuf; | |
95 | { | |
96 | int big_p = CGEN_CPU_ENDIAN (cd) == CGEN_ENDIAN_BIG; | |
97 | const CGEN_INSN_LIST *ilist; | |
98 | ||
99 | for (ilist = insns; ilist != NULL; ilist = ilist->next, ++ hentbuf) | |
100 | { | |
101 | unsigned int hash; | |
102 | char buf[4]; | |
103 | unsigned long value; | |
104 | ||
105 | if (! (* cd->dis_hash_p) (ilist->insn)) | |
106 | continue; | |
107 | ||
108 | /* We don't know whether the target uses the buffer or the base insn | |
109 | to hash on, so set both up. */ | |
110 | ||
111 | value = CGEN_INSN_BASE_VALUE (ilist->insn); | |
aed80dae FCE |
112 | bfd_put_bits((bfd_vma) value, |
113 | buf, | |
114 | CGEN_INSN_MASK_BITSIZE (ilist->insn), | |
115 | big_p); | |
252b5132 RH |
116 | hash = (* cd->dis_hash) (buf, value); |
117 | hentbuf->next = htable [hash]; | |
118 | hentbuf->insn = ilist->insn; | |
119 | htable [hash] = hentbuf; | |
120 | } | |
121 | ||
122 | return hentbuf; | |
123 | } | |
124 | ||
125 | /* Build the disassembler instruction hash table. */ | |
126 | ||
127 | static void | |
128 | build_dis_hash_table (cd) | |
129 | CGEN_CPU_DESC cd; | |
130 | { | |
131 | int count = cgen_insn_count (cd) + cgen_macro_insn_count (cd); | |
132 | CGEN_INSN_TABLE *insn_table = & cd->insn_table; | |
133 | CGEN_INSN_TABLE *macro_insn_table = & cd->macro_insn_table; | |
134 | unsigned int hash_size = cd->dis_hash_size; | |
135 | CGEN_INSN_LIST *hash_entry_buf; | |
136 | CGEN_INSN_LIST **dis_hash_table; | |
137 | CGEN_INSN_LIST *dis_hash_table_entries; | |
138 | ||
139 | /* The space allocated for the hash table consists of two parts: | |
140 | the hash table and the hash lists. */ | |
141 | ||
142 | dis_hash_table = (CGEN_INSN_LIST **) | |
143 | xmalloc (hash_size * sizeof (CGEN_INSN_LIST *)); | |
144 | memset (dis_hash_table, 0, hash_size * sizeof (CGEN_INSN_LIST *)); | |
145 | dis_hash_table_entries = hash_entry_buf = (CGEN_INSN_LIST *) | |
146 | xmalloc (count * sizeof (CGEN_INSN_LIST)); | |
147 | ||
148 | /* Add compiled in insns. | |
149 | Don't include the first one as it is a reserved entry. */ | |
150 | /* ??? It was the end of all hash chains, and also the special | |
151 | "invalid insn" marker. May be able to do it differently now. */ | |
152 | ||
153 | hash_entry_buf = hash_insn_array (cd, | |
154 | insn_table->init_entries + 1, | |
155 | insn_table->num_init_entries - 1, | |
156 | insn_table->entry_size, | |
157 | dis_hash_table, hash_entry_buf); | |
158 | ||
159 | /* Add compiled in macro-insns. */ | |
160 | ||
161 | hash_entry_buf = hash_insn_array (cd, macro_insn_table->init_entries, | |
162 | macro_insn_table->num_init_entries, | |
163 | macro_insn_table->entry_size, | |
164 | dis_hash_table, hash_entry_buf); | |
165 | ||
166 | /* Add runtime added insns. | |
167 | Later added insns will be prefered over earlier ones. */ | |
168 | ||
169 | hash_entry_buf = hash_insn_list (cd, insn_table->new_entries, | |
170 | dis_hash_table, hash_entry_buf); | |
171 | ||
172 | /* Add runtime added macro-insns. */ | |
173 | ||
174 | hash_insn_list (cd, macro_insn_table->new_entries, | |
175 | dis_hash_table, hash_entry_buf); | |
176 | ||
177 | cd->dis_hash_table = dis_hash_table; | |
178 | cd->dis_hash_table_entries = dis_hash_table_entries; | |
179 | } | |
180 | ||
181 | /* Return the first entry in the hash list for INSN. */ | |
182 | ||
183 | CGEN_INSN_LIST * | |
184 | cgen_dis_lookup_insn (cd, buf, value) | |
185 | CGEN_CPU_DESC cd; | |
186 | const char * buf; | |
187 | CGEN_INSN_INT value; | |
188 | { | |
189 | unsigned int hash; | |
190 | ||
191 | if (cd->dis_hash_table == NULL) | |
192 | build_dis_hash_table (cd); | |
193 | ||
194 | hash = (* cd->dis_hash) (buf, value); | |
195 | ||
196 | return cd->dis_hash_table[hash]; | |
197 | } |