Commit | Line | Data |
---|---|---|
9c03036a DE |
1 | /* CGEN generic disassembler support code. |
2 | ||
c062b103 | 3 | Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc. |
9c03036a | 4 | |
c062b103 | 5 | This file is part of the GNU Binutils and GDB, the GNU debugger. |
9c03036a | 6 | |
c062b103 DE |
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. | |
9c03036a | 11 | |
c062b103 DE |
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. | |
9c03036a | 16 | |
c062b103 DE |
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. */ | |
9c03036a | 20 | |
c062b103 | 21 | #include "sysdep.h" |
9c03036a | 22 | #include <stdio.h> |
9c03036a DE |
23 | #include "ansidecl.h" |
24 | #include "libiberty.h" | |
25 | #include "bfd.h" | |
26 | #include "opcode/cgen.h" | |
27 | ||
28 | /* This is not published as part of the public interface so we don't | |
29 | declare this in cgen.h. */ | |
30 | extern CGEN_OPCODE_DATA *cgen_current_opcode_data; | |
31 | ||
32 | /* Disassembler instruction hash table. */ | |
33 | static CGEN_INSN_LIST **dis_hash_table; | |
34 | ||
35 | void | |
36 | cgen_dis_init () | |
37 | { | |
38 | if (dis_hash_table) | |
39 | { | |
40 | free (dis_hash_table); | |
41 | dis_hash_table = NULL; | |
42 | } | |
43 | } | |
44 | ||
45 | /* Build the disassembler instruction hash table. */ | |
46 | ||
47 | static void | |
48 | build_dis_hash_table () | |
49 | { | |
50 | int i; | |
51 | int big_p = cgen_current_endian == CGEN_ENDIAN_BIG; | |
52 | unsigned int hash; | |
53 | char buf[4]; | |
54 | unsigned long value; | |
55 | int count = cgen_insn_count (); | |
56 | CGEN_OPCODE_DATA *data = cgen_current_opcode_data; | |
57 | CGEN_INSN_TABLE *insn_table = data->insn_table; | |
c062b103 | 58 | unsigned int entry_size = insn_table->entry_size; |
9c03036a DE |
59 | unsigned int hash_size = insn_table->dis_hash_table_size; |
60 | const CGEN_INSN *insn; | |
61 | CGEN_INSN_LIST *insn_lists,*new_insns; | |
62 | ||
63 | /* The space allocated for the hash table consists of two parts: | |
64 | the hash table and the hash lists. */ | |
65 | ||
66 | dis_hash_table = (CGEN_INSN_LIST **) | |
67 | xmalloc (hash_size * sizeof (CGEN_INSN_LIST *) | |
21b4ac17 | 68 | + count * sizeof (CGEN_INSN_LIST)); |
9c03036a DE |
69 | memset (dis_hash_table, 0, |
70 | hash_size * sizeof (CGEN_INSN_LIST *) | |
71 | + count * sizeof (CGEN_INSN_LIST)); | |
72 | insn_lists = (CGEN_INSN_LIST *) (dis_hash_table + hash_size); | |
73 | ||
74 | /* Add compiled in insns. | |
75 | The table is scanned backwards as later additions are inserted in | |
76 | front of earlier ones and we want earlier ones to be prefered. | |
c062b103 DE |
77 | We stop at the first one as it is a reserved entry. |
78 | This is a bit tricky as the attribute member of CGEN_INSN is variable | |
79 | among architectures. This code could be moved to cgen-asm.in, but | |
80 | I prefer to keep it here for now. */ | |
81 | ||
82 | for (insn = (CGEN_INSN *) | |
83 | ((char *) insn_table->init_entries | |
84 | + entry_size * (insn_table->num_init_entries - 1)); | |
9c03036a | 85 | insn > insn_table->init_entries; |
c062b103 | 86 | insn = (CGEN_INSN *) ((char *) insn - entry_size), ++insn_lists) |
9c03036a DE |
87 | { |
88 | /* We don't know whether the target uses the buffer or the base insn | |
89 | to hash on, so set both up. */ | |
c062b103 DE |
90 | value = CGEN_INSN_VALUE (insn); |
91 | switch (CGEN_INSN_MASK_BITSIZE (insn)) | |
9c03036a DE |
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. */ | |
c062b103 DE |
126 | value = CGEN_INSN_VALUE (new_insns->insn); |
127 | switch (CGEN_INSN_MASK_BITSIZE (new_insns->insn)) | |
9c03036a DE |
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 | } |