public inbox for binutils-cvs@sourceware.org
help / color / mirror / Atom feed
* [binutils-gdb] macro.c: use string hash from hash.h for macro_hash
@ 2022-07-09 12:35 Alan Modra
  0 siblings, 0 replies; only message in thread
From: Alan Modra @ 2022-07-09 12:35 UTC (permalink / raw)
  To: bfd-cvs

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=bdcc1de1ecfabc7d2560aa56cbe5425cb43e9cac

commit bdcc1de1ecfabc7d2560aa56cbe5425cb43e9cac
Author: Alan Modra <amodra@gmail.com>
Date:   Thu Jul 7 08:37:10 2022 +0930

    macro.c: use string hash from hash.h for macro_hash
    
    Another case of duplicated hash.h code, the only minor difference
    being that macro->format_hash was created with 7 entries vs. str_hash
    with 16 entries.
    
            * macro.c (macro_init, define_macro): Use str_htab_create.
            (do_formals, define_macro, macro_expand_body): Use str_hash_insert
            (macro_expand_body): Use str_hash_find and str_hash_delete.
            (delete_macro): Likewise.
            (sub_actual, macro_expand, check_macro): Use str_hash_find.
            (expand_irp): Use str_htab_create and str_hash_insert.
            * macro.h (struct macro_struct): Tidy.
            (struct macro_hash_entry, macro_hash_entry_t, hash_macro_entry),
            (eq_macro_entry, macro_entry_alloc, macro_entry_find),
            (struct formal_hash_entry, formal_hash_entry_t),
            (hash_formal_entry, eq_formal_entry, formal_entry_alloc),
            (formal_entry_find): Delete.
            * config/tc-iq2000.c (iq2000_add_macro): Use str_htab_create
            and str_hash_insert.

Diff:
---
 gas/config/tc-iq2000.c | 13 +++----
 gas/macro.c            | 62 +++++++++++---------------------
 gas/macro.h            | 98 +++-----------------------------------------------
 3 files changed, 29 insertions(+), 144 deletions(-)

diff --git a/gas/config/tc-iq2000.c b/gas/config/tc-iq2000.c
index 307b6b0902d..8a9b88f9f4f 100644
--- a/gas/config/tc-iq2000.c
+++ b/gas/config/tc-iq2000.c
@@ -246,10 +246,7 @@ iq2000_add_macro (const char *  name,
       formal_entry ** p = &macro->formals;
 
       macro->formal_count = 0;
-      macro->formal_hash = htab_create_alloc (7, hash_formal_entry,
-					      eq_formal_entry,
-					      NULL, xcalloc, free);
-
+      macro->formal_hash = str_htab_create ();
 
       while (*arguments != NULL)
 	{
@@ -275,10 +272,8 @@ iq2000_add_macro (const char *  name,
 	    sb_add_string (& formal->name, *arguments);
 
 	  /* Add to macro's hash table.  */
-	  htab_insert (macro->formal_hash,
-		       formal_entry_alloc (sb_terminate (& formal->name),
-					   formal),
-		       1);
+	  str_hash_insert (macro->formal_hash,
+			   sb_terminate (&formal->name), formal, 1);
 	  formal->index = macro->formal_count;
 	  macro->formal_count++;
 	  *p = formal;
@@ -290,7 +285,7 @@ iq2000_add_macro (const char *  name,
 
   sb_add_string (&macro_name, name);
   namestr = sb_terminate (&macro_name);
-  htab_insert (macro_hash, macro_entry_alloc (namestr, macro), 1);
+  str_hash_insert (macro_hash, namestr, macro, 1);
 
   macro_defined = 1;
 }
diff --git a/gas/macro.c b/gas/macro.c
index 3aba78f6bfe..b360b2ec038 100644
--- a/gas/macro.c
+++ b/gas/macro.c
@@ -44,7 +44,7 @@
 
 /* The macro hash table.  */
 
-struct htab *macro_hash;
+htab_t macro_hash;
 
 /* Whether any macros have been defined.  */
 
@@ -76,8 +76,7 @@ void
 macro_init (int alternate, int mri, int strip_at,
 	    size_t (*exp) (const char *, size_t, sb *, offsetT *))
 {
-  macro_hash = htab_create_alloc (16, hash_macro_entry, eq_macro_entry,
-				  NULL, xcalloc, free);
+  macro_hash = str_htab_create ();
   macro_defined = 0;
   macro_alternate = alternate;
   macro_mri = mri;
@@ -536,7 +535,6 @@ do_formals (macro_entry *macro, size_t idx, sb *in)
     {
       formal_entry *formal = new_formal ();
       size_t cidx;
-      formal_hash_entry_t *elt;
 
       idx = get_token (idx, in, &formal->name);
       if (formal->name.len == 0)
@@ -599,10 +597,8 @@ do_formals (macro_entry *macro, size_t idx, sb *in)
 	}
 
       /* Add to macro's hash table.  */
-      elt = formal_entry_alloc (name, formal);
-      if (htab_insert (macro->formal_hash, elt, 0) != NULL)
+      if (str_hash_insert (macro->formal_hash, name, formal, 0) != NULL)
 	{
-	  free (elt);
 	  as_bad_where (macro->file, macro->line,
 			_("A parameter named `%s' "
 			  "already exists for macro `%s'"),
@@ -626,7 +622,6 @@ do_formals (macro_entry *macro, size_t idx, sb *in)
   if (macro_mri)
     {
       formal_entry *formal = new_formal ();
-      formal_hash_entry_t *elt;
 
       /* Add a special NARG formal, which macro_expand will set to the
 	 number of arguments.  */
@@ -640,10 +635,8 @@ do_formals (macro_entry *macro, size_t idx, sb *in)
       sb_add_string (&formal->name, name);
 
       /* Add to macro's hash table.  */
-      elt = formal_entry_alloc (name, formal);
-      if (htab_insert (macro->formal_hash, elt, 0) != NULL)
+      if (str_hash_insert (macro->formal_hash, name, formal, 0) != NULL)
 	{
-	  free (elt);
 	  as_bad_where (macro->file, macro->line,
 			_("Reserved word `%s' used as parameter in macro `%s'"),
 			name, macro->name);
@@ -698,8 +691,7 @@ define_macro (size_t idx, sb *in, sb *label,
 
   macro->formal_count = 0;
   macro->formals = 0;
-  macro->formal_hash = htab_create_alloc (7, hash_formal_entry, eq_formal_entry,
-					  NULL, xcalloc, free);
+  macro->formal_hash = str_htab_create ();
 
   idx = sb_skip_white (idx, in);
   if (! buffer_and_nest ("MACRO", "ENDM", &macro->sub, get_line))
@@ -746,12 +738,8 @@ define_macro (size_t idx, sb *in, sb *label,
     name.ptr[idx] = TOLOWER (name.ptr[idx]);
   if (!error)
     {
-      macro_hash_entry_t *elt = macro_entry_alloc (macro->name, macro);
-      if (htab_insert (macro_hash, elt, 0) != NULL)
-	{
-	  free (elt);
-	  error = _("Macro `%s' was already defined");
-	}
+      if (str_hash_insert (macro_hash, macro->name, macro, 0) != NULL)
+	error = _("Macro `%s' was already defined");
     }
 
   if (namep != NULL)
@@ -796,7 +784,7 @@ sub_actual (size_t start, sb *in, sb *t, struct htab *formal_hash,
       && (src == start || in->ptr[src - 1] != '@'))
     ptr = NULL;
   else
-    ptr = formal_entry_find (formal_hash, sb_terminate (t));
+    ptr = str_hash_find (formal_hash, sb_terminate (t));
   if (ptr)
     {
       if (ptr->actual.len)
@@ -951,14 +939,11 @@ macro_expand_body (sb *in, sb *out, formal_entry *formals,
 		{
 		  const char *name;
 		  formal_entry *f = new_formal ();
-		  formal_hash_entry_t *elt;
 
 		  src = get_token (src, in, &f->name);
 		  name = sb_terminate (&f->name);
-		  elt = formal_entry_alloc (name, f);
-		  if (htab_insert (formal_hash, elt, 0) != NULL)
+		  if (str_hash_insert (formal_hash, name, f, 0) != NULL)
 		    {
-		      free (elt);
 		      as_bad_where (macro->file, macro->line + macro_line,
 				    _("`%s' was already used as parameter "
 				      "(or another local) name"), name);
@@ -1006,7 +991,7 @@ macro_expand_body (sb *in, sb *out, formal_entry *formals,
 
 	  sb_reset (&t);
 	  src = get_token (src + 2, in, &t);
-	  ptr = formal_entry_find (formal_hash, sb_terminate (&t));
+	  ptr = str_hash_find (formal_hash, sb_terminate (&t));
 	  if (ptr == NULL)
 	    {
 	      /* FIXME: We should really return a warning string here,
@@ -1050,8 +1035,7 @@ macro_expand_body (sb *in, sb *out, formal_entry *formals,
 
       f = loclist->next;
       name = sb_terminate (&loclist->name);
-      formal_hash_entry_t needle = { name, NULL };
-      htab_remove_elt (formal_hash, &needle);
+      str_hash_delete (formal_hash, name);
       del_formal (loclist);
       loclist = f;
     }
@@ -1136,7 +1120,7 @@ macro_expand (size_t idx, sb *in, macro_entry *m, sb *out)
 	    }
 
 	  /* Lookup the formal in the macro's list.  */
-	  ptr = formal_entry_find (m->formal_hash, sb_terminate (&t));
+	  ptr = str_hash_find (m->formal_hash, sb_terminate (&t));
 	  if (!ptr)
 	    {
 	      as_bad (_("Parameter named `%s' does not exist for macro `%s'"),
@@ -1234,7 +1218,7 @@ macro_expand (size_t idx, sb *in, macro_entry *m, sb *out)
 
 	  sb_reset (&t);
 	  sb_add_string (&t, macro_strip_at ? "$NARG" : "NARG");
-	  ptr = formal_entry_find (m->formal_hash, sb_terminate (&t));
+	  ptr = str_hash_find (m->formal_hash, sb_terminate (&t));
 	  sprintf (buffer, "%d", narg);
 	  sb_add_string (&ptr->actual, buffer);
 	}
@@ -1294,7 +1278,7 @@ check_macro (const char *line, sb *expand,
   for (cls = copy; *cls != '\0'; cls ++)
     *cls = TOLOWER (*cls);
 
-  macro = macro_entry_find (macro_hash, copy);
+  macro = str_hash_find (macro_hash, copy);
   free (copy);
 
   if (macro == NULL)
@@ -1324,8 +1308,7 @@ delete_macro (const char *name)
 {
   char *copy;
   size_t i, len;
-  void **slot;
-  macro_hash_entry_t needle;
+  macro_entry *macro;
 
   len = strlen (name);
   copy = XNEWVEC (char, len + 1);
@@ -1333,13 +1316,11 @@ delete_macro (const char *name)
     copy[i] = TOLOWER (name[i]);
   copy[i] = '\0';
 
-  needle.name = copy;
-  needle.macro = NULL;
-  slot = htab_find_slot (macro_hash, &needle, NO_INSERT);
-  if (slot)
+  macro = str_hash_find (macro_hash, copy);
+  if (macro != NULL)
     {
-      free_macro (((macro_hash_entry_t *) *slot)->macro);
-      htab_clear_slot (macro_hash, slot);
+      free_macro (macro);
+      str_hash_delete (macro_hash, copy);
     }
   else
     as_warn (_("Attempt to purge non-existing macro `%s'"), copy);
@@ -1372,10 +1353,9 @@ expand_irp (int irpc, size_t idx, sb *in, sb *out, size_t (*get_line) (sb *))
   if (f.name.len == 0)
     return _("missing model parameter");
 
-  h = htab_create_alloc (16, hash_formal_entry, eq_formal_entry,
-			 NULL, xcalloc, free);
+  h = str_htab_create ();
 
-  htab_insert (h, formal_entry_alloc (sb_terminate (&f.name), &f), 0);
+  str_hash_insert (h, sb_terminate (&f.name), &f, 0);
 
   f.index = 1;
   f.next = NULL;
diff --git a/gas/macro.h b/gas/macro.h
index 893b0c6660b..227c9cb5c93 100644
--- a/gas/macro.h
+++ b/gas/macro.h
@@ -62,10 +62,10 @@ typedef struct macro_struct
 {
   sb sub;				/* Substitution text.  */
   int formal_count;			/* Number of formal args.  */
-  formal_entry *formals;		/* Pointer to list of formal_structs.  */
-  struct htab *formal_hash;		/* Hash table of formals.  */
+  formal_entry *formals;		/* List of formal_structs.  */
+  htab_t formal_hash;			/* Hash table of formals.  */
   const char *name;			/* Macro name.  */
-  const char *file;				/* File the macro was defined in.  */
+  const char *file;			/* File the macro was defined in.  */
   unsigned int line;			/* Line number of definition.  */
 } macro_entry;
 
@@ -79,97 +79,7 @@ extern int macro_nest;
 
 /* The macro hash table.  */
 
-extern struct htab *macro_hash;
-
-struct macro_hash_entry
-{
-  const char *name;
-  macro_entry *macro;
-};
-
-typedef struct macro_hash_entry macro_hash_entry_t;
-
-/* Hash function for a macro_hash_entry.  */
-
-static inline hashval_t
-hash_macro_entry (const void *e)
-{
-  const macro_hash_entry_t *entry = (const macro_hash_entry_t *) e;
-  return htab_hash_string (entry->name);
-}
-
-/* Equality function for a macro_hash_entry.  */
-
-static inline int
-eq_macro_entry (const void *a, const void *b)
-{
-  const macro_hash_entry_t *ea = (const macro_hash_entry_t *) a;
-  const macro_hash_entry_t *eb = (const macro_hash_entry_t *) b;
-
-  return strcmp (ea->name, eb->name) == 0;
-}
-
-static inline macro_hash_entry_t *
-macro_entry_alloc (const char *name, macro_entry *macro)
-{
-  macro_hash_entry_t *entry = XNEW (macro_hash_entry_t);
-  entry->name = name;
-  entry->macro = macro;
-  return entry;
-}
-
-static inline macro_entry *
-macro_entry_find (htab_t table, const char *name)
-{
-  macro_hash_entry_t needle = { name, NULL };
-  macro_hash_entry_t *entry = htab_find (table, &needle);
-  return entry != NULL ? entry->macro : NULL;
-}
-
-struct formal_hash_entry
-{
-  const char *name;
-  formal_entry *formal;
-};
-
-typedef struct formal_hash_entry formal_hash_entry_t;
-
-/* Hash function for a macro_hash_entry.  */
-
-static inline hashval_t
-hash_formal_entry (const void *e)
-{
-  const formal_hash_entry_t *entry = (const formal_hash_entry_t *) e;
-  return htab_hash_string (entry->name);
-}
-
-/* Equality function for a formal_hash_entry.  */
-
-static inline int
-eq_formal_entry (const void *a, const void *b)
-{
-  const formal_hash_entry_t *ea = (const formal_hash_entry_t *) a;
-  const formal_hash_entry_t *eb = (const formal_hash_entry_t *) b;
-
-  return strcmp (ea->name, eb->name) == 0;
-}
-
-static inline formal_hash_entry_t *
-formal_entry_alloc (const char *name, formal_entry *formal)
-{
-  formal_hash_entry_t *entry = XNEW (formal_hash_entry_t);
-  entry->name = name;
-  entry->formal = formal;
-  return entry;
-}
-
-static inline formal_entry *
-formal_entry_find (htab_t table, const char *name)
-{
-  formal_hash_entry_t needle = { name, NULL };
-  formal_hash_entry_t *entry = htab_find (table, &needle);
-  return entry != NULL ? entry->formal : NULL;
-}
+extern htab_t macro_hash;
 
 extern int buffer_and_nest (const char *, const char *, sb *,
 			    size_t (*) (sb *));


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-07-09 12:35 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-09 12:35 [binutils-gdb] macro.c: use string hash from hash.h for macro_hash Alan Modra

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).