[PATCH 18/28] aarch64: Add more alias validation to aarch64-gen

Alice Carlotti alice.carlotti@arm.com
Wed Nov 4 18:46:24 GMT 2026


Verify that all opcodes marked F_ALIAS alias some opcode marked
F_HAS_ALIAS, and all opcodes marked F_HAS_ALIAS have at least one alias
(as determined based upon their opcodes and masks).  Additionally,
verify that each individual opcode uses a consistent subset of the
F_ALIAS, F_HAS_ALIAS and F_PSEUDO flags.

Remove some error checks elsewhere that are now redundant.


diff --git a/opcodes/aarch64-gen.c b/opcodes/aarch64-gen.c
index fe33f5fa17d3efb1027b6250b41008d12c5673e2..89cb1683d6940557858189f92fbebe4ae7c71ff0 100644
--- a/opcodes/aarch64-gen.c
+++ b/opcodes/aarch64-gen.c
@@ -81,6 +81,18 @@ new_opcode_node (void)
   return ent;
 }
 
+/* Free an entire opcode linked list.  HEAD is a pointer to the first
+   opcode_node in the list.  */
+static void free_opcode_list (opcode_node *head)
+{
+  while (head)
+    {
+      opcode_node *next = head->next;
+      free (head);
+      head = next;
+    }
+}
+
 /* Given OPCODE_NODE, return the corresponding aarch64_opcode*.  */
 static const aarch64_opcode*
 get_aarch64_opcode (const opcode_node *opcode_node)
@@ -90,6 +102,46 @@ get_aarch64_opcode (const opcode_node *opcode_node)
   return &aarch64_opcode_table[opcode_node->index];
 }
 
+/* Create a linked list of OPCODE_NODEs containing all opcodes marked
+   F_HAS_ALIAS.  */
+static opcode_node*
+generate_has_alias_list (void)
+{
+  opcode_node *head;
+  opcode_node **tail = &head;
+  for (unsigned int index = 0; ; index++)
+    {
+      const struct aarch64_opcode *ent = &aarch64_opcode_table[index];
+      if (!ent->name)
+	break;
+
+      if (!opcode_has_alias (ent))
+	  continue;
+
+      opcode_node *node = new_opcode_node ();
+      node->opcode = ent->opcode;
+      node->mask = ent->mask;
+      node->index = index;
+      *tail = node;
+      tail = &node->next;
+    }
+  return head;
+}
+
+/* Return the first opcode_node in has_alias_list aliased by *ENT, or NULL if
+   no such opcode exists.  */
+static opcode_node*
+get_aliased_opcode (aarch64_insn opcode, aarch64_insn mask,
+		    opcode_node *has_alias_list)
+{
+  opcode_node *node;
+  for (node = has_alias_list; node != NULL; node = node->next)
+    if ((mask & node->mask) == node->mask
+	&& (opcode & node->mask) == (node->opcode & node->mask))
+      return node;
+  return NULL;
+}
+
 static unsigned int table_error_count = 0;
 
 static void __attribute__ ((format (printf, 2, 3)))
@@ -108,7 +160,7 @@ report_error (const struct aarch64_opcode *ent, const char *format, ...)
 /* Verify various properties of the opcode table.  This allows some errors to
    be detected at build time.  */
 static void
-validate_table (void)
+validate_table (opcode_node *has_alias_list)
 {
   const struct aarch64_opcode *ent;
 
@@ -126,7 +178,35 @@ validate_table (void)
       /* F_PSEUDO needs to be used together with F_ALIAS to indicate an alias
 	 opcode is a programmer friendly pseudo instruction available only in
 	 the assembly code (thus will not show up in the disassembly).  */
-      assert (!pseudo_opcode_p (ent) || alias_opcode_p (ent));
+      if (pseudo_opcode_p (ent) && !alias_opcode_p (ent))
+	report_error (ent, "pseudo instruction not marked as alias.");
+
+      /* The alias hierarchy is flat-structured to keep algorithms simple.  */
+      if (alias_opcode_p (ent) && opcode_has_alias (ent))
+	report_error (ent, "alias cannot also be marked F_HAS_ALIAS.");
+
+      if (alias_opcode_p (ent)
+	  && !get_aliased_opcode (ent->opcode, ent->mask, has_alias_list))
+	report_error (ent, "alias has no compatible aliased opcode.");
+
+      /* Check that opcodes marked F_HAS_ALIAS actually have an alias.  */
+      if (opcode_has_alias (ent))
+	{
+	  bool alias_found = false;
+	  const struct aarch64_opcode *ent2;
+	  for (ent2 = aarch64_opcode_table; ent2->name; ent2++)
+	    {
+	      if (alias_opcode_p (ent2)
+		  && (ent->mask & ent2->mask) == ent->mask
+		  && (ent->opcode & ent->mask) == (ent2->opcode & ent->mask))
+		{
+		  alias_found = true;
+		  break;
+		}
+	    }
+	  if (!alias_found)
+	    report_error (ent, "marked F_HAS_ALIAS with no aliases.");
+	}
 
       /* Check tied_operand against operands[].  */
       for (unsigned int i = 1; i < ARRAY_SIZE (ent->operands); ++i)
@@ -807,13 +887,7 @@ create_alias_info (int *num_ptr)
   while (ent->name != NULL)
     {
       if (opcode_has_alias (ent))
-	{
-	  /* Assert the alias relationship be flat-structured to keep
-	     algorithms simple; not allow F_ALIAS and F_HAS_ALIAS both
-	     specified.  */
-	  assert (!alias_opcode_p (ent));
-	  ++num;
-	}
+	++num;
       ++ent;
     }
   assert (num_ptr);
@@ -1357,7 +1431,9 @@ main (int argc, char **argv)
       xexit (2);
     }
 
-  validate_table ();
+  opcode_node *has_alias_list = generate_has_alias_list ();
+
+  validate_table (has_alias_list);
 
   label_opcode_nodes ();
 
@@ -1429,6 +1505,8 @@ main (int argc, char **argv)
       release_resource_alias_info (alias_info, num);
     }
 
+  free_opcode_list (has_alias_list);
+
   /* Generate operand table.  */
   process_operand_table ();
 


More information about the Binutils mailing list