[PATCH v2 09/47] s390: Treat signed/unsigned immediate operands in .insn pseudo-mnemonics as either

Jens Remus jremus@linux.ibm.com
Fri Nov 6 15:56:59 GMT 2026


Subsequent commits add all z/Architecture instruction formats including
any variants as .insn pseudo-mnemonics.  Immediate operands in those
can be either signed or unsigned, depending on the instruction.
Therefore the immediate value range check must be performed so that any
possible signed and unsigned value is valid.  That is the values are
within the range [-min_signed, max_unsigned].

Instead of defining a new set of mixed signed and unsigned immediate
operand types as well as new instruction formats use the PSEUDO_MNEMONIC
instruction flag as indication whether to perform this special range
check in s390_insert_operand() for both signed and unsigned operands.
Pass the opcode pointer to s390_insert_operand().  When creating fixups
store the opcode and operand pointers in the target-specific fixup data,
so that md_apply_fix() can pass them to s390_insert_operand().

As a side benefit the operand index is no longer encoded in the fixup
relocation type (as BFD_RELOC_UNUSED + operand index), as the operand
pointer is now contained in the fixup structure.

gas/
	* config/tc-s390.h (struct s390_fix_data): New s390-specific
	fixup data structure.
	(TC_FIX_TYPE, TC_INIT_FIX_DATA): New defines.
	* config/tc-s390.c (s390_insert_operand): Treat signed/unsigned
	immediate operands in .insn pseudo-mnemonics as either.
	(struct s390_fixup): Store operand pointer instead of index.
	(md_gather_operands): Likewise.  Pass opcode to
	s390_insert_operand.  Store opcode and operand pointers in
	target-specific fixup data.
	(md_apply_fix): Pass opcode and operand pointers to
	s390_insert_operand.
	* doc/c-s390.texi (s390 Formats): Document operand Ix as un-/
	signed immediate with range min. signed to max. unsigned.

Signed-off-by: Jens Remus <jremus@linux.ibm.com>
---
 gas/config/tc-s390.c | 72 ++++++++++++++++++++++++++++++--------------
 gas/config/tc-s390.h | 12 ++++++++
 gas/doc/c-s390.texi  |  9 +++---
 3 files changed, 66 insertions(+), 27 deletions(-)

diff --git a/gas/config/tc-s390.c b/gas/config/tc-s390.c
index 707306aac389..5fb4dab1c14e 100644
--- a/gas/config/tc-s390.c
+++ b/gas/config/tc-s390.c
@@ -717,6 +717,7 @@ s390_bad_operand_out_of_range (int operand_number,
 
 static void
 s390_insert_operand (unsigned char *insn,
+		     const struct s390_opcode *opcode,
 		     const struct s390_operand *operand,
 		     offsetT val,
 		     const char *file,
@@ -726,7 +727,28 @@ s390_insert_operand (unsigned char *insn,
   addressT uval;
   int offset;
 
-  if (operand->flags & (S390_OPERAND_SIGNED|S390_OPERAND_PCREL))
+  if ((opcode->flags & S390_INSTR_FLAG_PSEUDO_MNEMONIC) &&
+      ((operand->flags & ~S390_OPERAND_SIGNED) == 0))
+    {
+      /* Signed/unsigned immediate in .insn pseudo-mnemonic.
+	 Treat as either.  */
+      offsetT min, max;
+
+      max = (((addressT) 1 << (operand->bits - 1)) << 1) - 1;
+      min = - ((offsetT) 1 << (operand->bits - 1));
+
+      /* Check for underflow / overflow.  */
+      if (val < min || val > max)
+	{
+	  s390_bad_operand_out_of_range (operand_number, val, min, max,
+					 file, line);
+
+	  return;
+	}
+      /* val is ok, now restrict it to operand->bits bits.  */
+      uval = val & ((((addressT) 1 << (operand->bits-1)) << 1) - 1);
+    }
+  else if (operand->flags & (S390_OPERAND_SIGNED|S390_OPERAND_PCREL))
     {
       offsetT min, max;
 
@@ -1418,7 +1440,7 @@ skip_optargs_p (unsigned int opcode_flags, const unsigned char *opindex_ptr)
 struct s390_fixup
   {
     expressionS exp;
-    int opindex;
+    const struct s390_operand *operand;
     bfd_reloc_code_real_type reloc;
   };
 
@@ -1459,7 +1481,7 @@ md_gather_operands (char *str,
 	{
 	  /* Optional parameters might need to be ORed with a
 	     value so calling s390_insert_operand is needed.  */
-	  s390_insert_operand (insn, operand, 0, NULL, 0, operand_number);
+	  s390_insert_operand (insn, opcode, operand, 0, NULL, 0, operand_number);
 	  break;
 	}
 
@@ -1513,7 +1535,7 @@ md_gather_operands (char *str,
 	      if (fc >= MAX_INSN_FIXUPS)
 		as_fatal (_("operand %d: too many fixups"), operand_number);
 	      fixups[fc].exp = ex;
-	      fixups[fc].opindex = *opindex_ptr;
+	      fixups[fc].operand = operand;
 	      fixups[fc].reloc = BFD_RELOC_UNUSED;
 	      ++fc;
 	    }
@@ -1586,7 +1608,7 @@ md_gather_operands (char *str,
 				 operand_number, expected_regtype);
 		    }
 		}
-	      s390_insert_operand (insn, operand, ex.X_add_number, NULL, 0, operand_number);
+	      s390_insert_operand (insn, opcode, operand, ex.X_add_number, NULL, 0, operand_number);
 	    }
 	}
       else
@@ -1678,7 +1700,7 @@ md_gather_operands (char *str,
 	  if (fc >= MAX_INSN_FIXUPS)
 	    as_fatal (_("operand %d: too many fixups"), operand_number);
 	  fixups[fc].exp = ex;
-	  fixups[fc].opindex = *opindex_ptr;
+	  fixups[fc].operand = operand;
 	  fixups[fc].reloc = reloc;
 	  ++fc;
 	}
@@ -1845,7 +1867,7 @@ md_gather_operands (char *str,
       if (fc >= MAX_INSN_FIXUPS)
 	as_fatal (_("too many fixups"));
       fixups[fc].exp = ex;
-      fixups[fc].opindex = -1;
+      fixups[fc].operand = NULL;
       fixups[fc].reloc = reloc;
       ++fc;
     }
@@ -1868,7 +1890,8 @@ md_gather_operands (char *str,
 
   /* Create any fixups.  At this point we do not use a
      bfd_reloc_code_real_type, but instead just use the
-     BFD_RELOC_UNUSED plus the operand index.  This lets us easily
+     BFD_RELOC_UNUSED and store opcode and operand in the
+     target-specific fixup data.  This lets us easily
      handle fixups for any operand type, although that is admittedly
      not a very exciting feature.  We pick a BFD reloc type in
      md_apply_fix.  */
@@ -1876,7 +1899,7 @@ md_gather_operands (char *str,
     {
       fixS *fixP;
 
-      if (fixups[i].opindex < 0)
+      if (fixups[i].operand == NULL)
 	{
 	  /* Create tls instruction marker relocation.  */
 	  fix_new_exp (frag_now, f - frag_now->fr_literal, opcode->oplen,
@@ -1884,7 +1907,7 @@ md_gather_operands (char *str,
 	  continue;
 	}
 
-      operand = s390_operands + fixups[i].opindex;
+      operand = fixups[i].operand;
 
       if (fixups[i].reloc != BFD_RELOC_UNUSED)
 	{
@@ -1916,10 +1939,15 @@ md_gather_operands (char *str,
 	    fixP->fx_pcrel_adjust = operand->shift / 8;
 	}
       else
-	fixP = fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
-			    &fixups[i].exp,
-			    (operand->flags & S390_OPERAND_PCREL) != 0,
-			    fixups[i].opindex + BFD_RELOC_UNUSED);
+	{
+	  fixP = fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
+			      &fixups[i].exp,
+			      (operand->flags & S390_OPERAND_PCREL) != 0,
+			      BFD_RELOC_UNUSED);
+	  fixP->tc_fix_data.opcode = opcode;
+	  fixP->tc_fix_data.operand = fixups[i].operand;
+	}
+
       /* s390_insert_operand () does the range checking.  */
       if (operand->flags & S390_OPERAND_PCREL)
 	fixP->fx_no_overflow = 1;
@@ -2477,9 +2505,9 @@ tc_s390_force_relocation (struct fix *fixp)
 
 /* Apply a fixup to the object code.  This is called for all the
    fixups we generated by the call to fix_new_exp, above.  In the call
-   above we used a reloc code which was the largest legal reloc code
-   plus the operand index.  Here we undo that to recover the operand
-   index.  At this point all symbol values should be fully resolved,
+   above we used a reloc code which was the largest legal reloc code.
+   Recover the opcode and operand from the target-specific fixup data.
+   At this point all symbol values should be fully resolved,
    and we attempt to completely resolve the reloc.  If we can not do
    that, we determine the correct reloc code and put it back in the
    fixup.  */
@@ -2503,18 +2531,18 @@ md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
   else
     fixP->fx_done = 1;
 
-  if (fixP->fx_r_type >= BFD_RELOC_UNUSED)
+  if (fixP->fx_r_type == BFD_RELOC_UNUSED)
     {
+      const struct s390_opcode *opcode;
       const struct s390_operand *operand;
-      int opindex;
 
-      opindex = fixP->fx_r_type - BFD_RELOC_UNUSED;
-      operand = &s390_operands[opindex];
+      opcode = fixP->tc_fix_data.opcode;
+      operand = fixP->tc_fix_data.operand;
 
       if (fixP->fx_done)
 	{
 	  /* Insert the fully resolved operand value.  */
-	  s390_insert_operand ((unsigned char *) where, operand,
+	  s390_insert_operand ((unsigned char *) where, opcode, operand,
 			       value, fixP->fx_file, fixP->fx_line, 0);
 	  return;
 	}
diff --git a/gas/config/tc-s390.h b/gas/config/tc-s390.h
index 3fd37c075bde..62e910cb2273 100644
--- a/gas/config/tc-s390.h
+++ b/gas/config/tc-s390.h
@@ -24,6 +24,18 @@
 
 struct fix;
 
+struct s390_fix_data {
+  const struct s390_opcode *opcode;
+  const struct s390_operand *operand;
+};
+
+#define TC_FIX_TYPE struct s390_fix_data
+#define TC_INIT_FIX_DATA(FIX)		\
+  {					\
+    (FIX)->tc_fix_data.opcode = NULL;	\
+    (FIX)->tc_fix_data.operand = NULL;	\
+  }
+
 #define TC_FORCE_RELOCATION(FIX) tc_s390_force_relocation(FIX)
 extern int tc_s390_force_relocation (struct fix *);
 
diff --git a/gas/doc/c-s390.texi b/gas/doc/c-s390.texi
index 8cf5718dc38b..53fe7128b362 100644
--- a/gas/doc/c-s390.texi
+++ b/gas/doc/c-s390.texi
@@ -340,8 +340,7 @@ instruction formats:
 @item Xx @tab Index register number for operand x.
 @item Vx @tab Vector register number for operand x.
 @item VXx @tab Vector index register number for operand x.
-@item Ix @tab Signed immediate for operand x.
-@item Ux @tab Unsigned immediate for operand x.
+@item Ix @tab Immediate for operand x in range min. signed to max. unsigned.
 @item RIx @tab Signed PC-relative immediate for operand x.
 @end multitable
 @end display
@@ -471,10 +470,10 @@ with the @samp{.insn} pseudo directive:
 0         8    12   16                                     47
 @end verbatim
 
-@item RILU format: <insn> R1,U2
+@item RILU format: <insn> R1,I2
 @verbatim
 +---------+----+----+---------------------------------------+
-| OpCode  | R1 |OpCd|                   U2                  |
+| OpCode  | R1 |OpCd|                   I2                  |
 +---------+----+----+---------------------------------------+
 0         8    12   16                                     47
 @end verbatim
@@ -599,7 +598,7 @@ with the @samp{.insn} pseudo directive:
 0         8         16   20            31
 @end verbatim
 
-@item SIY format: <insn> D1(B1),U2
+@item SIY format: <insn> D1(B1),I2
 @verbatim
 +---------+---------+----+--------------+---------+---------+
 | OpCode  |   I2    | B1 |     DL1      |   DH1   | OpCode  |
-- 
2.53.0



More information about the Binutils mailing list