cgen-opc.c
来自「基于4个mips核的noc设计」· C语言 代码 · 共 586 行 · 第 1/2 页
C
586 行
/* CGEN generic opcode support. Copyright 1996, 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc. This file is part of the GNU Binutils and GDB, the GNU debugger. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */#include "sysdep.h"#include <ctype.h>#include <stdio.h>#include "ansidecl.h"#include "libiberty.h"#include "bfd.h"#include "symcat.h"#include "opcode/cgen.h"#ifdef HAVE_ALLOCA_H#include <alloca.h>#endifstatic unsigned int hash_keyword_name PARAMS ((const CGEN_KEYWORD *, const char *, int));static unsigned int hash_keyword_value PARAMS ((const CGEN_KEYWORD *, unsigned int));static void build_keyword_hash_tables PARAMS ((CGEN_KEYWORD *));/* Return number of hash table entries to use for N elements. */#define KEYWORD_HASH_SIZE(n) ((n) <= 31 ? 17 : 31)/* Look up *NAMEP in the keyword table KT. The result is the keyword entry or NULL if not found. */const CGEN_KEYWORD_ENTRY *cgen_keyword_lookup_name (kt, name) CGEN_KEYWORD *kt; const char *name;{ const CGEN_KEYWORD_ENTRY *ke; const char *p,*n; if (kt->name_hash_table == NULL) build_keyword_hash_tables (kt); ke = kt->name_hash_table[hash_keyword_name (kt, name, 0)]; /* We do case insensitive comparisons. If that ever becomes a problem, add an attribute that denotes "do case sensitive comparisons". */ while (ke != NULL) { n = name; p = ke->name; while (*p && (*p == *n || (isalpha ((unsigned char) *p) && (tolower ((unsigned char) *p) == tolower ((unsigned char) *n))))) ++n, ++p; if (!*p && !*n) return ke; ke = ke->next_name; } if (kt->null_entry) return kt->null_entry; return NULL;}/* Look up VALUE in the keyword table KT. The result is the keyword entry or NULL if not found. */const CGEN_KEYWORD_ENTRY *cgen_keyword_lookup_value (kt, value) CGEN_KEYWORD *kt; int value;{ const CGEN_KEYWORD_ENTRY *ke; if (kt->name_hash_table == NULL) build_keyword_hash_tables (kt); ke = kt->value_hash_table[hash_keyword_value (kt, value)]; while (ke != NULL) { if (value == ke->value) return ke; ke = ke->next_value; } return NULL;}/* Add an entry to a keyword table. */voidcgen_keyword_add (kt, ke) CGEN_KEYWORD *kt; CGEN_KEYWORD_ENTRY *ke;{ unsigned int hash; if (kt->name_hash_table == NULL) build_keyword_hash_tables (kt); hash = hash_keyword_name (kt, ke->name, 0); ke->next_name = kt->name_hash_table[hash]; kt->name_hash_table[hash] = ke; hash = hash_keyword_value (kt, ke->value); ke->next_value = kt->value_hash_table[hash]; kt->value_hash_table[hash] = ke; if (ke->name[0] == 0) kt->null_entry = ke;}/* FIXME: Need function to return count of keywords. *//* Initialize a keyword table search. SPEC is a specification of what to search for. A value of NULL means to find every keyword. Currently NULL is the only acceptable value [further specification deferred]. The result is an opaque data item used to record the search status. It is passed to each call to cgen_keyword_search_next. */CGEN_KEYWORD_SEARCHcgen_keyword_search_init (kt, spec) CGEN_KEYWORD *kt; const char *spec;{ CGEN_KEYWORD_SEARCH search; /* FIXME: Need to specify format of PARAMS. */ if (spec != NULL) abort (); if (kt->name_hash_table == NULL) build_keyword_hash_tables (kt); search.table = kt; search.spec = spec; search.current_hash = 0; search.current_entry = NULL; return search;}/* Return the next keyword specified by SEARCH. The result is the next entry or NULL if there are no more. */const CGEN_KEYWORD_ENTRY *cgen_keyword_search_next (search) CGEN_KEYWORD_SEARCH *search;{ /* Has search finished? */ if (search->current_hash == search->table->hash_table_size) return NULL; /* Search in progress? */ if (search->current_entry != NULL /* Anything left on this hash chain? */ && search->current_entry->next_name != NULL) { search->current_entry = search->current_entry->next_name; return search->current_entry; } /* Move to next hash chain [unless we haven't started yet]. */ if (search->current_entry != NULL) ++search->current_hash; while (search->current_hash < search->table->hash_table_size) { search->current_entry = search->table->name_hash_table[search->current_hash]; if (search->current_entry != NULL) return search->current_entry; ++search->current_hash; } return NULL;}/* Return first entry in hash chain for NAME. If CASE_SENSITIVE_P is non-zero, return a case sensitive hash. */static unsigned inthash_keyword_name (kt, name, case_sensitive_p) const CGEN_KEYWORD *kt; const char *name; int case_sensitive_p;{ unsigned int hash; if (case_sensitive_p) for (hash = 0; *name; ++name) hash = (hash * 97) + (unsigned char) *name; else for (hash = 0; *name; ++name) hash = (hash * 97) + (unsigned char) tolower (*name); return hash % kt->hash_table_size;}/* Return first entry in hash chain for VALUE. */static unsigned inthash_keyword_value (kt, value) const CGEN_KEYWORD *kt; unsigned int value;{ return value % kt->hash_table_size;}/* Build a keyword table's hash tables. We probably needn't build the value hash table for the assembler when we're using the disassembler, but we keep things simple. */static voidbuild_keyword_hash_tables (kt) CGEN_KEYWORD *kt;{ int i; /* Use the number of compiled in entries as an estimate for the typical sized table [not too many added at runtime]. */ unsigned int size = KEYWORD_HASH_SIZE (kt->num_init_entries); kt->hash_table_size = size; kt->name_hash_table = (CGEN_KEYWORD_ENTRY **) xmalloc (size * sizeof (CGEN_KEYWORD_ENTRY *)); memset (kt->name_hash_table, 0, size * sizeof (CGEN_KEYWORD_ENTRY *)); kt->value_hash_table = (CGEN_KEYWORD_ENTRY **) xmalloc (size * sizeof (CGEN_KEYWORD_ENTRY *)); memset (kt->value_hash_table, 0, size * sizeof (CGEN_KEYWORD_ENTRY *)); /* The table is scanned backwards as we want keywords appearing earlier to be prefered over later ones. */ for (i = kt->num_init_entries - 1; i >= 0; --i) cgen_keyword_add (kt, &kt->init_entries[i]);}/* Hardware support. *//* Lookup a hardware element by its name. Returns NULL if NAME is not supported by the currently selected mach/isa. */const CGEN_HW_ENTRY *cgen_hw_lookup_by_name (cd, name) CGEN_CPU_DESC cd; const char *name;{ unsigned int i; const CGEN_HW_ENTRY **hw = cd->hw_table.entries; for (i = 0; i < cd->hw_table.num_entries; ++i) if (hw[i] && strcmp (name, hw[i]->name) == 0) return hw[i]; return NULL;}/* Lookup a hardware element by its number. Hardware elements are enumerated, however it may be possible to add some at runtime, thus HWNUM is not an enum type but rather an int. Returns NULL if HWNUM is not supported by the currently selected mach. */const CGEN_HW_ENTRY *cgen_hw_lookup_by_num (cd, hwnum) CGEN_CPU_DESC cd; unsigned int hwnum;{ unsigned int i; const CGEN_HW_ENTRY **hw = cd->hw_table.entries;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?