1 /* CGEN generic disassembler support code.
3 Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc.
5 This file is part of the GNU Binutils and GDB, the GNU debugger.
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)
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.
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. */
24 #include "libiberty.h"
26 #include "opcode/cgen.h"
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;
32 /* Disassembler instruction hash table. */
33 static CGEN_INSN_LIST **dis_hash_table;
40 free (dis_hash_table);
41 dis_hash_table = NULL;
45 /* Build the disassembler instruction hash table. */
48 build_dis_hash_table ()
51 int big_p = cgen_current_endian == CGEN_ENDIAN_BIG;
55 int count = cgen_insn_count ();
56 CGEN_OPCODE_DATA *data = cgen_current_opcode_data;
57 CGEN_INSN_TABLE *insn_table = data->insn_table;
58 unsigned int entry_size = insn_table->entry_size;
59 unsigned int hash_size = insn_table->dis_hash_table_size;
60 const CGEN_INSN *insn;
61 CGEN_INSN_LIST *insn_lists,*new_insns;
63 /* The space allocated for the hash table consists of two parts:
64 the hash table and the hash lists. */
66 dis_hash_table = (CGEN_INSN_LIST **)
67 xmalloc (hash_size * sizeof (CGEN_INSN_LIST *)
68 + count * sizeof (CGEN_INSN_LIST));
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);
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.
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. */
82 for (insn = (CGEN_INSN *)
83 ((char *) insn_table->init_entries
84 + entry_size * (insn_table->num_init_entries - 1));
85 insn > insn_table->init_entries;
86 insn = (CGEN_INSN *) ((char *) insn - entry_size), ++insn_lists)
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 = CGEN_INSN_VALUE (insn);
91 switch (CGEN_INSN_MASK_BITSIZE (insn))
98 bfd_putb16 ((bfd_vma) value, buf);
100 bfd_putl16 ((bfd_vma) value, buf);
104 bfd_putb32 ((bfd_vma) value, buf);
106 bfd_putl32 ((bfd_vma) value, buf);
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;
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;
122 new_insns = new_insns->next, ++insn_lists)
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 = CGEN_INSN_VALUE (new_insns->insn);
127 switch (CGEN_INSN_MASK_BITSIZE (new_insns->insn))
134 bfd_putb16 ((bfd_vma) value, buf);
136 bfd_putl16 ((bfd_vma) value, buf);
140 bfd_putb32 ((bfd_vma) value, buf);
142 bfd_putl32 ((bfd_vma) value, buf);
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;
154 /* Return the first entry in the hash list for INSN. */
157 cgen_dis_lookup_insn (buf, value)
163 if (dis_hash_table == NULL)
164 build_dis_hash_table ();
166 hash = (*cgen_current_opcode_data->insn_table->dis_hash) (buf, value);
167 return dis_hash_table[hash];