[binutils-gdb] s390: Add support for three optional operands in dis-/assembly

Jens Remus jremus@sourceware.org
Tue Nov 10 16:48:37 GMT 2026


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

commit 25918061fa67ec214941698bcb61787ece862a83
Author: Jens Remus <jremus@linux.ibm.com>
Date:   Tue Nov 10 17:46:02 2026 +0100

    s390: Add support for three optional operands in dis-/assembly
    
    This enables to add support for .insn pseudo-mnemonics with three
    optional trailing operands, such as in instruction format VRR variants
    VRR-a and VRR-c.
    
    While at it, fix the omission of trailing optional operands with
    constant bits in the instruction template in disassembly.  This was
    only supported for instructions with one trailing optional operand.
    Support it for instructions with two or three trailing optional
    operands as well.
    
    Introduce s390_extract_operand_masked, which masks the constant
    operand bits set in the instruction template of unsigned integer
    operands.  Use it for both testing for omission and printing.
    
    include/
            * opcode/s390.h (S390_INSTR_FLAG_OPTPARM3,
            S390_INSTR_FLAG_OPTPARM_MASK): New defines.
    
    gas/
            * config/tc-s390.c (skip_optargs_p): Simplify using
            S390_INSTR_FLAG_OPTPARM_MASK.  Handle
            S390_INSTR_FLAG_OPTPARM3.
    
    opcodes/
            * s390-dis.c (skip_optargs_p): Simplify using
            S390_INSTR_FLAG_OPTPARM_MASK.  Handle
            S390_INSTR_FLAG_OPTPARM3.
            (s390_extract_operand_masked): New function.
            (skip_optargs_zero_p): Use s390_extract_operand_masked to ignore
            constant operand bits set in insn template of unsigned integer
            operands when testing for zero.
            (s390_print_insn_with_opcode): Use s390_extract_operand_masked
            as replacement for masking of constant operand bits.  Remove
            S390_INSTR_FLAG_OPTPARM specific omission of last optional
            operand, as that is now handled by skip_optargs_zero_p.
            * s390-mkopc.c (main): Handle "optparm3" flag.
    
    Signed-off-by: Jens Remus <jremus@linux.ibm.com>

Diff:
---
 gas/config/tc-s390.c  | 16 ++++-------
 include/opcode/s390.h |  4 ++-
 opcodes/s390-dis.c    | 76 ++++++++++++++++++++++++++++++---------------------
 opcodes/s390-mkopc.c  |  4 +++
 4 files changed, 57 insertions(+), 43 deletions(-)

diff --git a/gas/config/tc-s390.c b/gas/config/tc-s390.c
index 5fb4dab1c14..096ea259490 100644
--- a/gas/config/tc-s390.c
+++ b/gas/config/tc-s390.c
@@ -1419,18 +1419,12 @@ operand_count (const unsigned char *opindex_ptr)
 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;
+  unsigned int optparm = opcode_flags & S390_INSTR_FLAG_OPTPARM_MASK;
+  unsigned int opcount;
 
-      if ((opcode_flags & S390_INSTR_FLAG_OPTPARM2) && opcount == 2)
-	return true;
-    }
-
-  return false;
+  return (optparm > 0
+	  && (opcount = operand_count (opindex_ptr)) > 0
+	  && opcount <= optparm);
 }
 
 /* We need to keep a list of fixups.  We can't simply generate them as
diff --git a/include/opcode/s390.h b/include/opcode/s390.h
index 85c34a76614..b31f50c3920 100644
--- a/include/opcode/s390.h
+++ b/include/opcode/s390.h
@@ -51,9 +51,11 @@ enum s390_opcode_cpu_val
 
 /* Values defined for the flags field of a struct s390_opcode.  */
 
-/* Last one or two instruction operands are optional.  */
+/* Trailing one, two, or three instruction operands are optional.  */
 #define S390_INSTR_FLAG_OPTPARM 0x1
 #define S390_INSTR_FLAG_OPTPARM2 0x2
+#define S390_INSTR_FLAG_OPTPARM3 0x3
+#define S390_INSTR_FLAG_OPTPARM_MASK 0x3
 
 /* Instruction requires a specific facility.  */
 #define S390_INSTR_FLAG_HTM 0x4
diff --git a/opcodes/s390-dis.c b/opcodes/s390-dis.c
index f3ea0032ded..59eac3df25b 100644
--- a/opcodes/s390-dis.c
+++ b/opcodes/s390-dis.c
@@ -180,6 +180,37 @@ s390_extract_operand (const bfd_byte *insn,
   return ret;
 }
 
+/* Extract an operand value from an instruction.  Additionally mask any
+   constant operand bits set in the instruction template of unsigned
+   integer operands.  Constant operand bits do not make sense for registers,
+   displacements, PC-relative offsets, lengths, and signed values.  */
+
+static inline union operand_value
+s390_extract_operand_masked (const bfd_byte *insn,
+			     const struct s390_opcode *opcode,
+			     const struct s390_operand *operand)
+{
+  union operand_value val = s390_extract_operand (insn, operand);
+
+  if (!(operand->flags & (S390_OPERAND_GPR
+			  | S390_OPERAND_FPR
+			  | S390_OPERAND_VR
+			  | S390_OPERAND_AR
+			  | S390_OPERAND_CR
+			  | S390_OPERAND_DISP
+			  | S390_OPERAND_PCREL
+			  | S390_OPERAND_LENGTH
+			  | S390_OPERAND_SIGNED)))
+    {
+      union operand_value insn_opval
+	= s390_extract_operand (opcode->opcode, operand);
+
+      val.u &= ~insn_opval.u;
+    }
+
+  return val;
+}
+
 /* Return remaining operand count.  */
 
 static unsigned int
@@ -205,36 +236,32 @@ operand_count (const unsigned char *opindex_ptr)
 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;
+  unsigned int optparm = opcode_flags & S390_INSTR_FLAG_OPTPARM_MASK;
+  unsigned int opcount;
 
-      if ((opcode_flags & S390_INSTR_FLAG_OPTPARM2) && opcount == 2)
-	return true;
-    }
-
-  return false;
+  return (optparm > 0
+	  && (opcount = operand_count (opindex_ptr)) > 0
+	  && opcount <= optparm);
 }
 
 /* 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,
+skip_optargs_zero_p (const bfd_byte *buffer, const struct s390_opcode *opcode,
 		     const unsigned char *opindex_ptr)
 {
   /* Test if remaining operands are optional.  */
-  if (!skip_optargs_p (opcode_flags, opindex_ptr))
+  if (!skip_optargs_p (opcode->flags, opindex_ptr))
     return false;
 
-  /* Test if remaining operand values are zero.  */
+  /* Test if remaining operand values are zero, ignoring any constant
+     operand bits set in the insn template.  */
   for (; *opindex_ptr != 0; opindex_ptr++)
     {
       const struct s390_operand *operand = &s390_operands[*opindex_ptr];
-      union operand_value value = s390_extract_operand (buffer, operand);
+      union operand_value value
+	= s390_extract_operand_masked (buffer, opcode, operand);
 
       if (value.u != 0)
 	return false;
@@ -264,7 +291,8 @@ s390_print_insn_with_opcode (bfd_vma memaddr,
   for (opindex = opcode->operands; *opindex != 0; opindex++)
     {
       const struct s390_operand *operand = s390_operands + *opindex;
-      union operand_value val = s390_extract_operand (buffer, operand);
+      union operand_value val
+	= s390_extract_operand_masked (buffer, opcode, operand);
       unsigned long flags = operand->flags;
 
       /* Omit index register 0, except for vector index register 0.  */
@@ -285,7 +313,7 @@ s390_print_insn_with_opcode (bfd_vma memaddr,
       if (!(operand->flags & (S390_OPERAND_BASE
 			      | S390_OPERAND_INDEX
 			      | S390_OPERAND_LENGTH))
-	  && skip_optargs_zero_p (buffer, opcode->flags, opindex))
+	  && skip_optargs_zero_p (buffer, opcode, opindex))
 	break;
 
       if (flags & S390_OPERAND_GPR)
@@ -353,20 +381,6 @@ s390_print_insn_with_opcode (bfd_vma memaddr,
 	{
 	  enum disassembler_style style;
 
-	  if (!(flags & S390_OPERAND_LENGTH))
-	    {
-	      union operand_value insn_opval;
-
-	      /* Mask any constant operand bits set in insn template.  */
-	      insn_opval = s390_extract_operand (opcode->opcode, operand);
-	      val.u &= ~insn_opval.u;
-	    }
-
-	  if ((opcode->flags & S390_INSTR_FLAG_OPTPARM)
-	      && val.u == 0
-	      && opindex[1] == 0)
-	    break;
-
 	  info->fprintf_styled_func (info->stream, dis_style_text,
 				     "%c", separator);
 	  style = ((flags & S390_OPERAND_DISP)
diff --git a/opcodes/s390-mkopc.c b/opcodes/s390-mkopc.c
index 4c31cb7320e..447bedb6781 100644
--- a/opcodes/s390-mkopc.c
+++ b/opcodes/s390-mkopc.c
@@ -486,6 +486,10 @@ main (void)
 		       && (str[8] == 0 || str[8] == ',')) {
 	      flag_bits |= S390_INSTR_FLAG_OPTPARM2;
 	      str += 8;
+	    } else if (strncmp (str, "optparm3", 8) == 0
+		       && (str[8] == 0 || str[8] == ',')) {
+	      flag_bits |= S390_INSTR_FLAG_OPTPARM3;
+	      str += 8;
 	    } else if (strncmp (str, "htm", 3) == 0
 		       && (str[3] == 0 || str[3] == ',')) {
 	      flag_bits |= S390_INSTR_FLAG_HTM;


More information about the Binutils-cvs mailing list