Commit | Line | Data |
---|---|---|
9c03036a DE |
1 | /* CGEN generic disassembler support code. |
2 | ||
3 | Copyright (C) 1996, 1997 Free Software Foundation, Inc. | |
4 | ||
5 | This file is part of the GNU Binutils and GDB, the GNU debugger. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2, or (at your option) | |
10 | any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License along | |
18 | with this program; if not, write to the Free Software Foundation, Inc., | |
19 | 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
20 | ||
21 | #include "config.h" | |
22 | #include <stdio.h> | |
23 | #ifdef HAVE_STRING_H | |
24 | #include <string.h> | |
25 | #endif | |
26 | #ifdef HAVE_STRINGS_H | |
27 | #include <strings.h> | |
28 | #endif | |
29 | #include "ansidecl.h" | |
30 | #include "libiberty.h" | |
31 | #include "bfd.h" | |
32 | #include "opcode/cgen.h" | |
33 | ||
34 | /* This is not published as part of the public interface so we don't | |
35 | declare this in cgen.h. */ | |
36 | extern CGEN_OPCODE_DATA *cgen_current_opcode_data; | |
37 | ||
38 | /* Disassembler instruction hash table. */ | |
39 | static CGEN_INSN_LIST **dis_hash_table; | |
40 | ||
41 | void | |
42 | cgen_dis_init () | |
43 | { | |
44 | if (dis_hash_table) | |
45 | { | |
46 | free (dis_hash_table); | |
47 | dis_hash_table = NULL; | |
48 | } | |
49 | } | |
50 | ||
51 | /* Build the disassembler instruction hash table. */ | |
52 | ||
53 | static void | |
54 | build_dis_hash_table () | |
55 | { | |
56 | int i; | |
57 | int big_p = cgen_current_endian == CGEN_ENDIAN_BIG; | |
58 | unsigned int hash; | |
59 | char buf[4]; | |
60 | unsigned long value; | |
61 | int count = cgen_insn_count (); | |
62 | CGEN_OPCODE_DATA *data = cgen_current_opcode_data; | |
63 | CGEN_INSN_TABLE *insn_table = data->insn_table; | |
64 | unsigned int hash_size = insn_table->dis_hash_table_size; | |
65 | const CGEN_INSN *insn; | |
66 | CGEN_INSN_LIST *insn_lists,*new_insns; | |
67 | ||
68 | /* The space allocated for the hash table consists of two parts: | |
69 | the hash table and the hash lists. */ | |
70 | ||
71 | dis_hash_table = (CGEN_INSN_LIST **) | |
72 | xmalloc (hash_size * sizeof (CGEN_INSN_LIST *) | |
21b4ac17 | 73 | + count * sizeof (CGEN_INSN_LIST)); |
9c03036a DE |
74 | memset (dis_hash_table, 0, |
75 | hash_size * sizeof (CGEN_INSN_LIST *) | |
76 | + count * sizeof (CGEN_INSN_LIST)); | |
77 | insn_lists = (CGEN_INSN_LIST *) (dis_hash_table + hash_size); | |
78 | ||
79 | /* Add compiled in insns. | |
80 | The table is scanned backwards as later additions are inserted in | |
81 | front of earlier ones and we want earlier ones to be prefered. | |
82 | We stop at the first one as it is a reserved entry. */ | |
83 | ||
84 | for (insn = insn_table->init_entries + insn_table->num_init_entries - 1; | |
85 | insn > insn_table->init_entries; | |
86 | --insn, ++insn_lists) | |
87 | { | |
88 | /* We don't know whether the target uses the buffer or the base insn | |
89 | to hash on, so set both up. */ | |
90 | value = insn->syntax.value; | |
91 | switch (CGEN_INSN_BITSIZE (insn)) | |
92 | { | |
93 | case 8: | |
94 | buf[0] = value; | |
95 | break; | |
96 | case 16: | |
97 | if (big_p) | |
98 | bfd_putb16 ((bfd_vma) value, buf); | |
99 | else | |
100 | bfd_putl16 ((bfd_vma) value, buf); | |
101 | break; | |
102 | case 32: | |
103 | if (big_p) | |
104 | bfd_putb32 ((bfd_vma) value, buf); | |
105 | else | |
106 | bfd_putl32 ((bfd_vma) value, buf); | |
107 | break; | |
108 | default: | |
109 | abort (); | |
110 | } | |
111 | hash = (*insn_table->dis_hash) (buf, value); | |
112 | insn_lists->next = dis_hash_table[hash]; | |
113 | insn_lists->insn = insn; | |
114 | dis_hash_table[hash] = insn_lists; | |
115 | } | |
116 | ||
117 | /* Add runtime added insns. | |
118 | ??? Currently later added insns will be prefered over earlier ones. | |
119 | Not sure this is a bug or not. */ | |
120 | for (new_insns = insn_table->new_entries; | |
121 | new_insns != NULL; | |
122 | new_insns = new_insns->next, ++insn_lists) | |
123 | { | |
124 | /* We don't know whether the target uses the buffer or the base insn | |
125 | to hash on, so set both up. */ | |
126 | value = new_insns->insn->syntax.value; | |
127 | switch (CGEN_INSN_BITSIZE (new_insns->insn)) | |
128 | { | |
129 | case 8: | |
130 | buf[0] = value; | |
131 | break; | |
132 | case 16: | |
133 | if (big_p) | |
134 | bfd_putb16 ((bfd_vma) value, buf); | |
135 | else | |
136 | bfd_putl16 ((bfd_vma) value, buf); | |
137 | break; | |
138 | case 32: | |
139 | if (big_p) | |
140 | bfd_putb32 ((bfd_vma) value, buf); | |
141 | else | |
142 | bfd_putl32 ((bfd_vma) value, buf); | |
143 | break; | |
144 | default: | |
145 | abort (); | |
146 | } | |
147 | hash = (*insn_table->dis_hash) (buf, value); | |
148 | insn_lists->next = dis_hash_table[hash]; | |
149 | insn_lists->insn = new_insns->insn; | |
150 | dis_hash_table[hash] = insn_lists; | |
151 | } | |
152 | } | |
153 | ||
154 | /* Return the first entry in the hash list for INSN. */ | |
155 | ||
156 | CGEN_INSN_LIST * | |
157 | cgen_dis_lookup_insn (buf, value) | |
158 | const char *buf; | |
159 | unsigned long value; | |
160 | { | |
161 | unsigned int hash; | |
162 | ||
163 | if (dis_hash_table == NULL) | |
164 | build_dis_hash_table (); | |
165 | ||
166 | hash = (*cgen_current_opcode_data->insn_table->dis_hash) (buf, value); | |
167 | return dis_hash_table[hash]; | |
168 | } |