cgen-dis.c revision 38889
1/* CGEN generic disassembler support code. 2 3 Copyright (C) 1996, 1997, 1998 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 "sysdep.h" 22#include <stdio.h> 23#include "ansidecl.h" 24#include "libiberty.h" 25#include "bfd.h" 26#include "symcat.h" 27#include "opcode/cgen.h" 28 29/* This is not published as part of the public interface so we don't 30 declare this in cgen.h. */ 31extern CGEN_OPCODE_DATA * cgen_current_opcode_data; 32 33/* Disassembler instruction hash table. */ 34static CGEN_INSN_LIST ** dis_hash_table; 35 36void 37cgen_dis_init () 38{ 39 if (dis_hash_table) 40 { 41 free (dis_hash_table); 42 dis_hash_table = NULL; 43 } 44} 45 46/* Build the disassembler instruction hash table. */ 47 48static void 49build_dis_hash_table () 50{ 51 int bigend = 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; 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; 62 CGEN_INSN_LIST * new_insns; 63 64 /* The space allocated for the hash table consists of two parts: 65 the hash table and the hash lists. */ 66 67 dis_hash_table = (CGEN_INSN_LIST **) 68 xmalloc (hash_size * sizeof (CGEN_INSN_LIST *) 69 + count * sizeof (CGEN_INSN_LIST)); 70 memset (dis_hash_table, 0, 71 hash_size * sizeof (CGEN_INSN_LIST *) 72 + count * sizeof (CGEN_INSN_LIST)); 73 insn_lists = (CGEN_INSN_LIST *) (dis_hash_table + hash_size); 74 75 /* Add compiled in insns. 76 The table is scanned backwards as later additions are inserted in 77 front of earlier ones and we want earlier ones to be prefered. 78 We stop at the first one as it is a reserved entry. 79 This is a bit tricky as the attribute member of CGEN_INSN is variable 80 among architectures. This code could be moved to cgen-asm.in, but 81 I prefer to keep it here for now. */ 82 83 for (insn = (CGEN_INSN *) 84 ((char *) insn_table->init_entries 85 + entry_size * (insn_table->num_init_entries - 1)); 86 insn > insn_table->init_entries; 87 insn = (CGEN_INSN *) ((char *) insn - entry_size), ++ insn_lists) 88 { 89 /* We don't know whether the target uses the buffer or the base insn 90 to hash on, so set both up. */ 91 value = CGEN_INSN_VALUE (insn); 92 switch (CGEN_INSN_MASK_BITSIZE (insn)) 93 { 94 case 8: 95 buf[0] = value; 96 break; 97 case 16: 98 if (bigend) 99 bfd_putb16 ((bfd_vma) value, buf); 100 else 101 bfd_putl16 ((bfd_vma) value, buf); 102 break; 103 case 32: 104 if (bigend) 105 bfd_putb32 ((bfd_vma) value, buf); 106 else 107 bfd_putl32 ((bfd_vma) value, buf); 108 break; 109 default: 110 abort (); 111 } 112 113 hash = insn_table->dis_hash (buf, value); 114 115 insn_lists->next = dis_hash_table [hash]; 116 insn_lists->insn = insn; 117 118 dis_hash_table [hash] = insn_lists; 119 } 120 121 /* Add runtime added insns. 122 ??? Currently later added insns will be prefered over earlier ones. 123 Not sure this is a bug or not. */ 124 for (new_insns = insn_table->new_entries; 125 new_insns != NULL; 126 new_insns = new_insns->next, ++ insn_lists) 127 { 128 /* We don't know whether the target uses the buffer or the base insn 129 to hash on, so set both up. */ 130 value = CGEN_INSN_VALUE (new_insns->insn); 131 switch (CGEN_INSN_MASK_BITSIZE (new_insns->insn)) 132 { 133 case 8: 134 buf[0] = value; 135 break; 136 case 16: 137 if (bigend) 138 bfd_putb16 ((bfd_vma) value, buf); 139 else 140 bfd_putl16 ((bfd_vma) value, buf); 141 break; 142 case 32: 143 if (bigend) 144 bfd_putb32 ((bfd_vma) value, buf); 145 else 146 bfd_putl32 ((bfd_vma) value, buf); 147 break; 148 default: 149 abort (); 150 } 151 152 hash = insn_table->dis_hash (buf, value); 153 154 insn_lists->next = dis_hash_table [hash]; 155 insn_lists->insn = new_insns->insn; 156 157 dis_hash_table [hash] = insn_lists; 158 } 159} 160 161/* Return the first entry in the hash list for INSN. */ 162 163CGEN_INSN_LIST * 164cgen_dis_lookup_insn (buf, value) 165 const char * buf; 166 unsigned long value; 167{ 168 unsigned int hash; 169 170 if (dis_hash_table == NULL) 171 build_dis_hash_table (); 172 173 hash = cgen_current_opcode_data->insn_table->dis_hash (buf, value); 174 175 return dis_hash_table [hash]; 176} 177