From 921996dc9f1d1322fcad32822125546a7c6a6e93 Mon Sep 17 00:00:00 2001
From: Jens Remus <jremus@linux.ibm.com>
Date: Fri, 7 Mar 2025 13:06:53 +0100
Subject: [PATCH 09/10] s390: Treat addressing operand sequence as one in
 disassembler

Reuse logic introduced with the preceding commit in the assembler to
treat addressing operand sequences D(X,B), D(B), and D(L,B) as one
with regards to optional last operands (i.e. optparm and optparm2).

With this "nop" now disassembles into "nop" instead of "nop 0".

opcodes/
	* s390-dis.c (operand_count): New helper to count the remaining
	operands, treating D(X,B), D(B), and D(L,B) as one.
	(skip_optargs_p): New helper to test whether remaining operands
	 are optional.
	(skip_optargs_zero_p): New helper to test whether remaining
	operands are optional and their values are zero.
	(s390_print_insn_with_opcode): Use skip_optargs_zero_p to skip
	optional last operands with a value of zero.

gas/testsuite/
	* gas/s390/zarch-optargs.d (nop): Adjust test case accordingly.

Signed-off-by: Jens Remus <jremus@linux.ibm.com>

(cherry picked from commit 36bbf8646c8)
(cherry pick dropped: gas/testsuite/gas/s390/zarch-optargs.d)
---
 opcodes/s390-dis.c | 84 ++++++++++++++++++++++++++++++++++++----------
 1 file changed, 66 insertions(+), 18 deletions(-)

diff --git a/opcodes/s390-dis.c b/opcodes/s390-dis.c
index 1a23afc8f40..8e7b9838f81 100644
--- a/opcodes/s390-dis.c
+++ b/opcodes/s390-dis.c
@@ -180,6 +180,69 @@ s390_extract_operand (const bfd_byte *insn,
   return ret;
 }
 
+/* Return remaining operand count.  */
+
+static unsigned int
+operand_count (const unsigned char *opindex_ptr)
+{
+  unsigned int count = 0;
+
+  for (; *opindex_ptr != 0; opindex_ptr++)
+    {
+      /* Count D(X,B), D(B), and D(L,B) as one operand.  Assuming correct
+	 instruction operand definitions simply do not count D, X, and L.  */
+      if (!(s390_operands[*opindex_ptr].flags & (S390_OPERAND_DISP
+						| S390_OPERAND_INDEX
+						| S390_OPERAND_LENGTH)))
+	count++;
+    }
+
+  return count;
+}
+
+/* Return true if all remaining instruction operands are optional.  */
+
+static bool
+skip_optargs_p (unsigned int opcode_flags, const unsigned char *opindex_ptr)
+{
+  if ((opcode_flags & (S390_INSTR_FLAG_OPTPARM | S390_INSTR_FLAG_OPTPARM2)))
+    {
+      unsigned int opcount = operand_count (opindex_ptr);
+
+      if (opcount == 1)
+	return true;
+
+      if ((opcode_flags & S390_INSTR_FLAG_OPTPARM2) && opcount == 2)
+	return true;
+    }
+
+  return false;
+}
+
+/* Return true if all remaining instruction operands are optional
+   and their values are zero.  */
+
+static bool
+skip_optargs_zero_p (const bfd_byte *buffer, unsigned int opcode_flags,
+		     const unsigned char *opindex_ptr)
+{
+  /* Test if remaining operands are optional.  */
+  if (!skip_optargs_p (opcode_flags, opindex_ptr))
+    return false;
+
+  /* Test if remaining operand values are zero.  */
+  for (; *opindex_ptr != 0; opindex_ptr++)
+    {
+      const struct s390_operand *operand = &s390_operands[*opindex_ptr];
+      union operand_value value = s390_extract_operand (buffer, operand);
+
+      if (value.u != 0)
+	return false;
+    }
+
+  return true;
+}
+
 /* Print the S390 instruction in BUFFER, assuming that it matches the
    given OPCODE.  */
 
@@ -220,24 +283,9 @@ s390_print_insn_with_opcode (bfd_vma memaddr,
 	 handled separately, as they may not be omitted unconditionally.  */
       if (!(operand->flags & (S390_OPERAND_BASE
 			      | S390_OPERAND_INDEX
-			      | S390_OPERAND_LENGTH)))
-	{
-	  if ((opcode->flags & (S390_INSTR_FLAG_OPTPARM | S390_INSTR_FLAG_OPTPARM2))
-	      && val.u == 0
-	      && opindex[1] == 0)
-	    break;
-
-	  if ((opcode->flags & S390_INSTR_FLAG_OPTPARM2)
-	      && val.u == 0
-	      && opindex[1] != 0 && opindex[2] == 0)
-	    {
-	      union operand_value next_op_val =
-		s390_extract_operand (buffer, s390_operands + opindex[1]);
-
-	      if (next_op_val.u == 0)
-		break;
-	    }
-	}
+			      | S390_OPERAND_LENGTH))
+	  && skip_optargs_zero_p (buffer, opcode->flags, opindex))
+	break;
 
       if (flags & S390_OPERAND_GPR)
 	{
-- 
2.43.0

