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)
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. */
25 #include "libiberty.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 /* Subroutine of build_dis_hash_table to add INSNS to the hash table.
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.
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. */
46 static CGEN_INSN_LIST
*
47 hash_insn_array (cd
, insns
, count
, entsize
, htable
, hentbuf
)
49 const CGEN_INSN
* insns
;
51 int entsize ATTRIBUTE_UNUSED
;
52 CGEN_INSN_LIST
** htable
;
53 CGEN_INSN_LIST
* hentbuf
;
55 int big_p
= CGEN_CPU_ENDIAN (cd
) == CGEN_ENDIAN_BIG
;
58 for (i
= count
- 1; i
>= 0; --i
, ++hentbuf
)
63 const CGEN_INSN
*insn
= &insns
[i
];
65 if (! (* cd
->dis_hash_p
) (insn
))
68 /* We don't know whether the target uses the buffer or the base insn
69 to hash on, so set both up. */
71 value
= CGEN_INSN_BASE_VALUE (insn
);
72 bfd_put_bits ((bfd_vma
) value
,
74 CGEN_INSN_MASK_BITSIZE (insn
),
76 hash
= (* cd
->dis_hash
) (buf
, value
);
77 hentbuf
->next
= htable
[hash
];
79 htable
[hash
] = hentbuf
;
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
89 static CGEN_INSN_LIST
*
90 hash_insn_list (cd
, insns
, htable
, hentbuf
)
92 const CGEN_INSN_LIST
*insns
;
93 CGEN_INSN_LIST
**htable
;
94 CGEN_INSN_LIST
*hentbuf
;
96 int big_p
= CGEN_CPU_ENDIAN (cd
) == CGEN_ENDIAN_BIG
;
97 const CGEN_INSN_LIST
*ilist
;
99 for (ilist
= insns
; ilist
!= NULL
; ilist
= ilist
->next
, ++ hentbuf
)
105 if (! (* cd
->dis_hash_p
) (ilist
->insn
))
108 /* We don't know whether the target uses the buffer or the base insn
109 to hash on, so set both up. */
111 value
= CGEN_INSN_BASE_VALUE (ilist
->insn
);
112 bfd_put_bits((bfd_vma
) value
,
114 CGEN_INSN_MASK_BITSIZE (ilist
->insn
),
116 hash
= (* cd
->dis_hash
) (buf
, value
);
117 hentbuf
->next
= htable
[hash
];
118 hentbuf
->insn
= ilist
->insn
;
119 htable
[hash
] = hentbuf
;
125 /* Build the disassembler instruction hash table. */
128 build_dis_hash_table (cd
)
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
;
139 /* The space allocated for the hash table consists of two parts:
140 the hash table and the hash lists. */
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
));
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. */
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
);
159 /* Add compiled in macro-insns. */
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
);
166 /* Add runtime added insns.
167 Later added insns will be prefered over earlier ones. */
169 hash_entry_buf
= hash_insn_list (cd
, insn_table
->new_entries
,
170 dis_hash_table
, hash_entry_buf
);
172 /* Add runtime added macro-insns. */
174 hash_insn_list (cd
, macro_insn_table
->new_entries
,
175 dis_hash_table
, hash_entry_buf
);
177 cd
->dis_hash_table
= dis_hash_table
;
178 cd
->dis_hash_table_entries
= dis_hash_table_entries
;
181 /* Return the first entry in the hash list for INSN. */
184 cgen_dis_lookup_insn (cd
, buf
, value
)
191 if (cd
->dis_hash_table
== NULL
)
192 build_dis_hash_table (cd
);
194 hash
= (* cd
->dis_hash
) (buf
, value
);
196 return cd
->dis_hash_table
[hash
];