[binutils-gdb] Use libiberty hash in gas/macro.c.

Alan Modra amodra@sourceware.org
Thu Aug 20 01:31:56 GMT 2020


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

commit 2b272f449e8664977d0aafba82c416b27b5f2d6f
Author: Martin Liska <mliska@suse.cz>
Date:   Sat Aug 15 19:47:01 2020 +0200

    Use libiberty hash in gas/macro.c.
    
            * config/tc-iq2000.c (iq2000_add_macro): Use htab hash table.
            * macro.c (struct hash_control): Use htab.
            (macro_init): Likewise.
            (do_formals): Likewise.
            (free_macro): Likewise.
            (define_macro): Likewise.
            (sub_actual): Likewise.
            (macro_expand_body): Likewise.
            (macro_expand): Likewise.
            (check_macro): Likewise.
            (delete_macro): Likewise.
            (expand_irp): Likewise.
            * macro.h (struct macro_hash_entry): New struct.
            (hash_macro_entry): New.
            (eq_macro_entry): Likewise.
            (macro_entry_alloc): Likewise.
            (macro_entry_find): Likewise.
            (struct formal_hash_entry): Likewise.
            (hash_formal_entry): Likewise.
            (eq_formal_entry): Likewise.
            (formal_entry_alloc): Likewise.
            (formal_entry_find): Likewise.

Diff:
---
 gas/ChangeLog          | 25 ++++++++++++++
 gas/config/tc-iq2000.c | 14 +++++---
 gas/macro.c            | 65 +++++++++++++++++-----------------
 gas/macro.h            | 94 ++++++++++++++++++++++++++++++++++++++++++++++++--
 4 files changed, 159 insertions(+), 39 deletions(-)

diff --git a/gas/ChangeLog b/gas/ChangeLog
index acc5e01cc97..e474edd12e4 100644
--- a/gas/ChangeLog
+++ b/gas/ChangeLog
@@ -1,3 +1,28 @@
+2020-08-20  Martin Liska  <mliska@suse.cz>
+
+	* config/tc-iq2000.c (iq2000_add_macro): Use htab hash table.
+	* macro.c (struct hash_control): Use htab.
+	(macro_init): Likewise.
+	(do_formals): Likewise.
+	(free_macro): Likewise.
+	(define_macro): Likewise.
+	(sub_actual): Likewise.
+	(macro_expand_body): Likewise.
+	(macro_expand): Likewise.
+	(check_macro): Likewise.
+	(delete_macro): Likewise.
+	(expand_irp): Likewise.
+	* macro.h (struct macro_hash_entry): New struct.
+	(hash_macro_entry): New.
+	(eq_macro_entry): Likewise.
+	(macro_entry_alloc): Likewise.
+	(macro_entry_find): Likewise.
+	(struct formal_hash_entry): Likewise.
+	(hash_formal_entry): Likewise.
+	(eq_formal_entry): Likewise.
+	(formal_entry_alloc): Likewise.
+	(formal_entry_find): Likewise.
+
 2020-08-20  Martin Liska  <mliska@suse.cz>
 
 	* as.h: Include hashtab.h.
diff --git a/gas/config/tc-iq2000.c b/gas/config/tc-iq2000.c
index 4310f522827..3df89949f2d 100644
--- a/gas/config/tc-iq2000.c
+++ b/gas/config/tc-iq2000.c
@@ -106,7 +106,7 @@ struct iq2000_hi_fixup
 static struct iq2000_hi_fixup * iq2000_hi_fixup_list;
 
 /* Macro hash table, which we will add to.  */
-extern struct hash_control *macro_hash;
+extern struct htab *macro_hash;
 
 const char *md_shortopts = "";
 struct option md_longopts[] =
@@ -246,7 +246,10 @@ iq2000_add_macro (const char *  name,
       formal_entry ** p = &macro->formals;
 
       macro->formal_count = 0;
-      macro->formal_hash = hash_new ();
+      macro->formal_hash = htab_create_alloc (7, hash_formal_entry,
+					      eq_formal_entry,
+					      NULL, xcalloc, free);
+
 
       while (*arguments != NULL)
 	{
@@ -272,8 +275,9 @@ iq2000_add_macro (const char *  name,
 	    sb_add_string (& formal->name, *arguments);
 
 	  /* Add to macro's hash table.  */
-	  hash_jam (macro->formal_hash, sb_terminate (& formal->name), formal);
-
+	  htab_insert (macro->formal_hash,
+		       formal_entry_alloc (sb_terminate (& formal->name),
+					   formal));
 	  formal->index = macro->formal_count;
 	  macro->formal_count++;
 	  *p = formal;
@@ -285,7 +289,7 @@ iq2000_add_macro (const char *  name,
 
   sb_add_string (&macro_name, name);
   namestr = sb_terminate (&macro_name);
-  hash_jam (macro_hash, namestr, macro);
+  htab_insert (macro_hash, macro_entry_alloc (namestr, macro));
 
   macro_defined = 1;
 }
diff --git a/gas/macro.c b/gas/macro.c
index de6b4172ec2..d99430ea4d5 100644
--- a/gas/macro.c
+++ b/gas/macro.c
@@ -44,7 +44,7 @@
 
 /* The macro hash table.  */
 
-struct hash_control *macro_hash;
+struct htab *macro_hash;
 
 /* Whether any macros have been defined.  */
 
@@ -76,7 +76,8 @@ void
 macro_init (int alternate, int mri, int strip_at,
 	    size_t (*exp) (const char *, size_t, sb *, offsetT *))
 {
-  macro_hash = hash_new ();
+  macro_hash = htab_create_alloc (16, hash_macro_entry, eq_macro_entry,
+				  NULL, xcalloc, free);
   macro_defined = 0;
   macro_alternate = alternate;
   macro_mri = mri;
@@ -566,8 +567,8 @@ do_formals (macro_entry *macro, size_t idx, sb *in)
 	}
 
       /* Add to macro's hash table.  */
-      if (! hash_find (macro->formal_hash, name))
-	hash_jam (macro->formal_hash, name, formal);
+      if (formal_entry_find (macro->formal_hash, name) == NULL)
+	htab_insert (macro->formal_hash, formal_entry_alloc (name, formal));
       else
 	as_bad_where (macro->file,
 		      macro->line,
@@ -605,13 +606,13 @@ do_formals (macro_entry *macro, size_t idx, sb *in)
       sb_add_string (&formal->name, name);
 
       /* Add to macro's hash table.  */
-      if (hash_find (macro->formal_hash, name))
+      if (formal_entry_find (macro->formal_hash, name))
 	as_bad_where (macro->file,
 		      macro->line,
 		      _("Reserved word `%s' used as parameter in macro `%s'"),
 		      name,
 		      macro->name);
-      hash_jam (macro->formal_hash, name, formal);
+      htab_insert (macro->formal_hash, formal_entry_alloc (name, formal));
 
       formal->index = NARG_INDEX;
       *p = formal;
@@ -635,7 +636,7 @@ free_macro (macro_entry *macro)
       formal = formal->next;
       del_formal (f);
     }
-  hash_die (macro->formal_hash);
+  htab_delete (macro->formal_hash);
   sb_kill (&macro->sub);
   free (macro);
 }
@@ -662,7 +663,8 @@ define_macro (size_t idx, sb *in, sb *label,
 
   macro->formal_count = 0;
   macro->formals = 0;
-  macro->formal_hash = hash_new_sized (7);
+  macro->formal_hash = htab_create_alloc (7, hash_formal_entry, eq_formal_entry,
+					  NULL, xcalloc, free);
 
   idx = sb_skip_white (idx, in);
   if (! buffer_and_nest ("MACRO", "ENDM", &macro->sub, get_line))
@@ -707,10 +709,10 @@ define_macro (size_t idx, sb *in, sb *label,
   /* And stick it in the macro hash table.  */
   for (idx = 0; idx < name.len; idx++)
     name.ptr[idx] = TOLOWER (name.ptr[idx]);
-  if (hash_find (macro_hash, macro->name))
+  if (macro_entry_find (macro_hash, macro->name))
     error = _("Macro `%s' was already defined");
   if (!error)
-    error = hash_jam (macro_hash, macro->name, (void *) macro);
+    htab_insert (macro_hash, macro_entry_alloc (macro->name, macro));
 
   if (namep != NULL)
     *namep = macro->name;
@@ -740,7 +742,7 @@ get_apost_token (size_t idx, sb *in, sb *name, int kind)
 /* Substitute the actual value for a formal parameter.  */
 
 static size_t
-sub_actual (size_t start, sb *in, sb *t, struct hash_control *formal_hash,
+sub_actual (size_t start, sb *in, sb *t, struct htab *formal_hash,
 	    int kind, sb *out, int copyifnotthere)
 {
   size_t src;
@@ -754,7 +756,7 @@ sub_actual (size_t start, sb *in, sb *t, struct hash_control *formal_hash,
       && (src == start || in->ptr[src - 1] != '@'))
     ptr = NULL;
   else
-    ptr = (formal_entry *) hash_find (formal_hash, sb_terminate (t));
+    ptr = formal_entry_find (formal_hash, sb_terminate (t));
   if (ptr)
     {
       if (ptr->actual.len)
@@ -790,7 +792,7 @@ sub_actual (size_t start, sb *in, sb *t, struct hash_control *formal_hash,
 
 static const char *
 macro_expand_body (sb *in, sb *out, formal_entry *formals,
-		   struct hash_control *formal_hash, const macro_entry *macro)
+		   struct htab *formal_hash, const macro_entry *macro)
 {
   sb t;
   size_t src = 0;
@@ -912,7 +914,7 @@ macro_expand_body (sb *in, sb *out, formal_entry *formals,
 
 		  src = get_token (src, in, &f->name);
 		  name = sb_terminate (&f->name);
-		  if (! hash_find (formal_hash, name))
+		  if (formal_entry_find (formal_hash, name) == NULL)
 		    {
 		      static int loccnt;
 		      char buf[20];
@@ -924,9 +926,7 @@ macro_expand_body (sb *in, sb *out, formal_entry *formals,
 		      sprintf (buf, IS_ELF ? ".LL%04x" : "LL%04x", ++loccnt);
 		      sb_add_string (&f->actual, buf);
 
-		      err = hash_jam (formal_hash, name, f);
-		      if (err != NULL)
-			break;
+		      htab_insert (formal_hash, formal_entry_alloc (name, f));
 		    }
 		  else
 		    {
@@ -966,7 +966,7 @@ macro_expand_body (sb *in, sb *out, formal_entry *formals,
 
 	  sb_reset (&t);
 	  src = get_token (src + 2, in, &t);
-	  ptr = (formal_entry *) hash_find (formal_hash, sb_terminate (&t));
+	  ptr = formal_entry_find (formal_hash, sb_terminate (&t));
 	  if (ptr == NULL)
 	    {
 	      /* FIXME: We should really return a warning string here,
@@ -1010,7 +1010,8 @@ macro_expand_body (sb *in, sb *out, formal_entry *formals,
 
       f = loclist->next;
       name = sb_terminate (&loclist->name);
-      hash_delete (formal_hash, name, f == NULL);
+      formal_hash_entry_t needle = { name, NULL };
+      htab_remove_elt (formal_hash, &needle);
       del_formal (loclist);
       loclist = f;
     }
@@ -1095,7 +1096,7 @@ macro_expand (size_t idx, sb *in, macro_entry *m, sb *out)
 	    }
 
 	  /* Lookup the formal in the macro's list.  */
-	  ptr = (formal_entry *) hash_find (m->formal_hash, sb_terminate (&t));
+	  ptr = formal_entry_find (m->formal_hash, sb_terminate (&t));
 	  if (!ptr)
 	    {
 	      as_bad (_("Parameter named `%s' does not exist for macro `%s'"),
@@ -1193,7 +1194,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 *) hash_find (m->formal_hash, sb_terminate (&t));
+	  ptr = formal_entry_find (m->formal_hash, sb_terminate (&t));
 	  sprintf (buffer, "%d", narg);
 	  sb_add_string (&ptr->actual, buffer);
 	}
@@ -1253,7 +1254,7 @@ check_macro (const char *line, sb *expand,
   for (cls = copy; *cls != '\0'; cls ++)
     *cls = TOLOWER (*cls);
 
-  macro = (macro_entry *) hash_find (macro_hash, copy);
+  macro = macro_entry_find (macro_hash, copy);
   free (copy);
 
   if (macro == NULL)
@@ -1294,14 +1295,14 @@ delete_macro (const char *name)
   /* We can only ask hash_delete to free memory if we are deleting
      macros in reverse order to their definition.
      So just clear out the entry.  */
-  if ((macro = (macro_entry *) hash_find (macro_hash, copy)) != NULL)
+  macro = macro_entry_find (macro_hash, copy);
+  if (macro)
     {
-      hash_jam (macro_hash, copy, NULL);
+      htab_insert (macro_hash, macro_entry_alloc (copy, NULL));
       free_macro (macro);
     }
   else
     as_warn (_("Attempt to purge non-existing macro `%s'"), copy);
-  free (copy);
 }
 
 /* Handle the MRI IRP and IRPC pseudo-ops.  These are handled as a
@@ -1313,8 +1314,8 @@ expand_irp (int irpc, size_t idx, sb *in, sb *out, size_t (*get_line) (sb *))
 {
   sb sub;
   formal_entry f;
-  struct hash_control *h;
-  const char *err;
+  struct htab *h;
+  const char *err = NULL;
 
   idx = sb_skip_white (idx, in);
 
@@ -1330,10 +1331,10 @@ 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 = hash_new ();
-  err = hash_jam (h, sb_terminate (&f.name), &f);
-  if (err != NULL)
-    return err;
+  h = htab_create_alloc (16, hash_formal_entry, eq_formal_entry,
+			 NULL, xcalloc, free);
+
+  htab_insert (h, formal_entry_alloc (sb_terminate (&f.name), &f));
 
   f.index = 1;
   f.next = NULL;
@@ -1392,7 +1393,7 @@ expand_irp (int irpc, size_t idx, sb *in, sb *out, size_t (*get_line) (sb *))
 	}
     }
 
-  hash_die (h);
+  htab_delete (h);
   sb_kill (&f.actual);
   sb_kill (&f.def);
   sb_kill (&f.name);
diff --git a/gas/macro.h b/gas/macro.h
index e9bd9e1110c..532642e803e 100644
--- a/gas/macro.h
+++ b/gas/macro.h
@@ -63,7 +63,7 @@ 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 hash_control *formal_hash;	/* Hash table of formals.  */
+  struct htab *formal_hash;		/* Hash table of formals.  */
   const char *name;			/* Macro name.  */
   const char *file;				/* File the macro was defined in.  */
   unsigned int line;			/* Line number of definition.  */
@@ -79,7 +79,97 @@ extern int macro_nest;
 
 /* The macro hash table.  */
 
-extern struct hash_control *macro_hash;
+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 int buffer_and_nest (const char *, const char *, sb *,
 			    size_t (*) (sb *));


More information about the Binutils-cvs mailing list