Blame SOURCES/gdb-rhbz1182151-ibm-z13-01of22.patch

0b42f8
commit 9ace48f3d7d80ce09c5df60cccb433470410b11b
0b42f8
Author: Andreas Arnez <arnez@linux.vnet.ibm.com>
0b42f8
Date:   Tue Aug 19 15:42:13 2014 +0100
0b42f8
0b42f8
    This patch set mainly aims at improving the S/390 disassembler's
0b42f8
    readability and also fixes some minor issues.
0b42f8
    
0b42f8
      S/390: Split disassembler routine into smaller functions
0b42f8
      S/390: Fix disassembler's treatment of signed/unsigned operands
0b42f8
      S/390: Fix off-by-one error in disassembler initialization
0b42f8
      S/390: Simplify opcode search loop in disassembler
0b42f8
      S/390: Drop function pointer dereferences in disassembler
0b42f8
      S/390: Various minor simplifications in disassembler
0b42f8
0b42f8
### a/opcodes/ChangeLog
0b42f8
### b/opcodes/ChangeLog
0b42f8
## -1,3 +1,30 @@
0b42f8
+2014-08-19  Andreas Arnez  <arnez@linux.vnet.ibm.com>
0b42f8
+
0b42f8
+	* s390-dis.c (s390_insn_length, s390_insn_matches_opcode)
0b42f8
+	(s390_print_insn_with_opcode, opcode_mask_more_specific): New
0b42f8
+	static functions, code was moved from...
0b42f8
+	(print_insn_s390): ...here.
0b42f8
+	(s390_extract_operand): Adjust comment.  Change type of first
0b42f8
+	parameter from 'unsigned char *' to 'const bfd_byte *'.
0b42f8
+	(union operand_value): New.
0b42f8
+	(s390_extract_operand): Change return type to union operand_value.
0b42f8
+	Also avoid integer overflow in sign-extension.
0b42f8
+	(s390_print_insn_with_opcode): Adjust to changed return value from
0b42f8
+	s390_extract_operand().  Change "%i" printf format to "%u" for
0b42f8
+	unsigned values.
0b42f8
+	(init_disasm): Simplify initialization of opc_index[].  This also
0b42f8
+	fixes an access after the last element of s390_opcodes[].
0b42f8
+	(print_insn_s390): Simplify the opcode search loop.
0b42f8
+	Check architecture mask against all searched opcodes, not just the
0b42f8
+	first matching one.
0b42f8
+	(s390_print_insn_with_opcode): Drop function pointer dereferences
0b42f8
+	without effect.
0b42f8
+	(print_insn_s390): Likewise.
0b42f8
+	(s390_insn_length): Simplify formula for return value.
0b42f8
+	(s390_print_insn_with_opcode): Avoid special handling for the
0b42f8
+	separator before the first operand.  Use new local variable
0b42f8
+	'flags' in place of 'operand->flags'.
0b42f8
+
0b42f8
 2014-08-14  Mike Frysinger  <vapier@gentoo.org>
0b42f8
 
0b42f8
 	* bfin-dis.c (struct private): Change int's to bfd_boolean's.
0b42f8
--- a/opcodes/s390-dis.c
0b42f8
+++ b/opcodes/s390-dis.c
0b42f8
@@ -35,19 +35,15 @@ static int current_arch_mask = 0;
0b42f8
 static void
0b42f8
 init_disasm (struct disassemble_info *info)
0b42f8
 {
0b42f8
-  const struct s390_opcode *opcode;
0b42f8
-  const struct s390_opcode *opcode_end;
0b42f8
+  int i;
0b42f8
   const char *p;
0b42f8
 
0b42f8
   memset (opc_index, 0, sizeof (opc_index));
0b42f8
-  opcode_end = s390_opcodes + s390_num_opcodes;
0b42f8
-  for (opcode = s390_opcodes; opcode < opcode_end; opcode++)
0b42f8
-    {
0b42f8
-      opc_index[(int) opcode->opcode[0]] = opcode - s390_opcodes;
0b42f8
-      while ((opcode < opcode_end) &&
0b42f8
-	     (opcode[1].opcode[0] == opcode->opcode[0]))
0b42f8
-	opcode++;
0b42f8
-    }
0b42f8
+
0b42f8
+  /* Reverse order, such that each opc_index ends up pointing to the
0b42f8
+     first matching entry instead of the last.  */
0b42f8
+  for (i = s390_num_opcodes; i--; )
0b42f8
+    opc_index[s390_opcodes[i].opcode[0]] = i;
0b42f8
 
0b42f8
   for (p = info->disassembler_options; p != NULL; )
0b42f8
     {
0b42f8
@@ -69,15 +65,46 @@ init_disasm (struct disassemble_info *info)
0b42f8
   init_flag = 1;
0b42f8
 }
0b42f8
 
0b42f8
+/* Derive the length of an instruction from its first byte.  */
0b42f8
+
0b42f8
+static inline int
0b42f8
+s390_insn_length (const bfd_byte *buffer)
0b42f8
+{
0b42f8
+  /* 00xxxxxx -> 2, 01xxxxxx/10xxxxxx -> 4, 11xxxxxx -> 6.  */
0b42f8
+  return ((buffer[0] >> 6) + 3) & ~1U;
0b42f8
+}
0b42f8
+
0b42f8
+/* Match the instruction in BUFFER against the given OPCODE, excluding
0b42f8
+   the first byte.  */
0b42f8
+
0b42f8
+static inline int
0b42f8
+s390_insn_matches_opcode (const bfd_byte *buffer,
0b42f8
+			  const struct s390_opcode *opcode)
0b42f8
+{
0b42f8
+  return (buffer[1] & opcode->mask[1]) == opcode->opcode[1]
0b42f8
+    && (buffer[2] & opcode->mask[2]) == opcode->opcode[2]
0b42f8
+    && (buffer[3] & opcode->mask[3]) == opcode->opcode[3]
0b42f8
+    && (buffer[4] & opcode->mask[4]) == opcode->opcode[4]
0b42f8
+    && (buffer[5] & opcode->mask[5]) == opcode->opcode[5];
0b42f8
+}
0b42f8
+
0b42f8
+union operand_value
0b42f8
+{
0b42f8
+  int i;
0b42f8
+  unsigned int u;
0b42f8
+};
0b42f8
+
0b42f8
 /* Extracts an operand value from an instruction.  */
0b42f8
 /* We do not perform the shift operation for larl-type address
0b42f8
    operands here since that would lead to an overflow of the 32 bit
0b42f8
    integer value.  Instead the shift operation is done when printing
0b42f8
-   the operand in print_insn_s390.  */
0b42f8
+   the operand.  */
0b42f8
 
0b42f8
-static inline unsigned int
0b42f8
-s390_extract_operand (unsigned char *insn, const struct s390_operand *operand)
0b42f8
+static inline union operand_value
0b42f8
+s390_extract_operand (const bfd_byte *insn,
0b42f8
+		      const struct s390_operand *operand)
0b42f8
 {
0b42f8
+  union operand_value ret;
0b42f8
   unsigned int val;
0b42f8
   int bits;
0b42f8
 
0b42f8
@@ -99,15 +126,97 @@ s390_extract_operand (unsigned char *insn, const struct s390_operand *operand)
0b42f8
   if (operand->bits == 20 && operand->shift == 20)
0b42f8
     val = (val & 0xff) << 12 | (val & 0xfff00) >> 8;
0b42f8
 
0b42f8
-  /* Sign extend value if the operand is signed or pc relative.  */
0b42f8
-  if ((operand->flags & (S390_OPERAND_SIGNED | S390_OPERAND_PCREL))
0b42f8
-      && (val & (1U << (operand->bits - 1))))
0b42f8
-    val |= (-1U << (operand->bits - 1)) << 1;
0b42f8
+  /* Sign extend value if the operand is signed or pc relative.  Avoid
0b42f8
+     integer overflows.  */
0b42f8
+  if (operand->flags & (S390_OPERAND_SIGNED | S390_OPERAND_PCREL))
0b42f8
+    {
0b42f8
+      unsigned int m = 1U << (operand->bits - 1);
0b42f8
+
0b42f8
+      if (val >= m)
0b42f8
+	ret.i = (int) (val - m) - 1 - (int) (m - 1U);
0b42f8
+      else
0b42f8
+	ret.i = (int) val;
0b42f8
+    }
0b42f8
+  else if (operand->flags & S390_OPERAND_LENGTH)
0b42f8
+    /* Length x in an instruction has real length x + 1.  */
0b42f8
+    ret.u = val + 1;
0b42f8
+  else
0b42f8
+    ret.u = val;
0b42f8
+
0b42f8
+  return ret;
0b42f8
+}
0b42f8
+
0b42f8
+/* Print the S390 instruction in BUFFER, assuming that it matches the
0b42f8
+   given OPCODE.  */
0b42f8
+
0b42f8
+static void
0b42f8
+s390_print_insn_with_opcode (bfd_vma memaddr,
0b42f8
+			     struct disassemble_info *info,
0b42f8
+			     const bfd_byte *buffer,
0b42f8
+			     const struct s390_opcode *opcode)
0b42f8
+{
0b42f8
+  const unsigned char *opindex;
0b42f8
+  char separator;
0b42f8
+
0b42f8
+  /* Mnemonic.  */
0b42f8
+  info->fprintf_func (info->stream, "%s", opcode->name);
0b42f8
+
0b42f8
+  /* Operands.  */
0b42f8
+  separator = '\t';
0b42f8
+  for (opindex = opcode->operands; *opindex != 0; opindex++)
0b42f8
+    {
0b42f8
+      const struct s390_operand *operand = s390_operands + *opindex;
0b42f8
+      union operand_value val = s390_extract_operand (buffer, operand);
0b42f8
+      unsigned long flags = operand->flags;
0b42f8
+
0b42f8
+      if ((flags & S390_OPERAND_INDEX) && val.u == 0)
0b42f8
+	continue;
0b42f8
+      if ((flags & S390_OPERAND_BASE) &&
0b42f8
+	  val.u == 0 && separator == '(')
0b42f8
+	{
0b42f8
+	  separator = ',';
0b42f8
+	  continue;
0b42f8
+	}
0b42f8
+
0b42f8
+      info->fprintf_func (info->stream, "%c", separator);
0b42f8
+
0b42f8
+      if (flags & S390_OPERAND_GPR)
0b42f8
+	info->fprintf_func (info->stream, "%%r%u", val.u);
0b42f8
+      else if (flags & S390_OPERAND_FPR)
0b42f8
+	info->fprintf_func (info->stream, "%%f%u", val.u);
0b42f8
+      else if (flags & S390_OPERAND_AR)
0b42f8
+	info->fprintf_func (info->stream, "%%a%u", val.u);
0b42f8
+      else if (flags & S390_OPERAND_CR)
0b42f8
+	info->fprintf_func (info->stream, "%%c%u", val.u);
0b42f8
+      else if (flags & S390_OPERAND_PCREL)
0b42f8
+	info->print_address_func (memaddr + val.i + val.i, info);
0b42f8
+      else if (flags & S390_OPERAND_SIGNED)
0b42f8
+	info->fprintf_func (info->stream, "%i", val.i);
0b42f8
+      else
0b42f8
+	info->fprintf_func (info->stream, "%u", val.u);
0b42f8
+
0b42f8
+      if (flags & S390_OPERAND_DISP)
0b42f8
+	separator = '(';
0b42f8
+      else if (flags & S390_OPERAND_BASE)
0b42f8
+	{
0b42f8
+	  info->fprintf_func (info->stream, ")");
0b42f8
+	  separator = ',';
0b42f8
+	}
0b42f8
+      else
0b42f8
+	separator = ',';
0b42f8
+    }
0b42f8
+}
0b42f8
+
0b42f8
+/* Check whether opcode A's mask is more specific than that of B.  */
0b42f8
 
0b42f8
-  /* Length x in an instructions has real length x + 1.  */
0b42f8
-  if (operand->flags & S390_OPERAND_LENGTH)
0b42f8
-    val++;
0b42f8
-  return val;
0b42f8
+static int
0b42f8
+opcode_mask_more_specific (const struct s390_opcode *a,
0b42f8
+			   const struct s390_opcode *b)
0b42f8
+{
0b42f8
+  return (((int) a->mask[0] + a->mask[1] + a->mask[2]
0b42f8
+	   + a->mask[3] + a->mask[4] + a->mask[5])
0b42f8
+	  > ((int) b->mask[0] + b->mask[1] + b->mask[2]
0b42f8
+	     + b->mask[3] + b->mask[4] + b->mask[5]));
0b42f8
 }
0b42f8
 
0b42f8
 /* Print a S390 instruction.  */
0b42f8
@@ -116,11 +225,9 @@ int
0b42f8
 print_insn_s390 (bfd_vma memaddr, struct disassemble_info *info)
0b42f8
 {
0b42f8
   bfd_byte buffer[6];
0b42f8
-  const struct s390_opcode *opcode;
0b42f8
-  const struct s390_opcode *opcode_end;
0b42f8
+  const struct s390_opcode *opcode = NULL;
0b42f8
   unsigned int value;
0b42f8
   int status, opsize, bufsize;
0b42f8
-  char separator;
0b42f8
 
0b42f8
   if (init_flag == 0)
0b42f8
     init_disasm (info);
0b42f8
@@ -130,156 +237,72 @@ print_insn_s390 (bfd_vma memaddr, struct disassemble_info *info)
0b42f8
 
0b42f8
   /* Every S390 instruction is max 6 bytes long.  */
0b42f8
   memset (buffer, 0, 6);
0b42f8
-  status = (*info->read_memory_func) (memaddr, buffer, 6, info);
0b42f8
+  status = info->read_memory_func (memaddr, buffer, 6, info);
0b42f8
   if (status != 0)
0b42f8
     {
0b42f8
       for (bufsize = 0; bufsize < 6; bufsize++)
0b42f8
-	if ((*info->read_memory_func) (memaddr, buffer, bufsize + 1, info) != 0)
0b42f8
+	if (info->read_memory_func (memaddr, buffer, bufsize + 1, info) != 0)
0b42f8
 	  break;
0b42f8
       if (bufsize <= 0)
0b42f8
 	{
0b42f8
-	  (*info->memory_error_func) (status, memaddr, info);
0b42f8
+	  info->memory_error_func (status, memaddr, info);
0b42f8
 	  return -1;
0b42f8
 	}
0b42f8
-      /* Opsize calculation looks strange but it works
0b42f8
-	 00xxxxxx -> 2 bytes, 01xxxxxx/10xxxxxx -> 4 bytes,
0b42f8
-	 11xxxxxx -> 6 bytes.  */
0b42f8
-      opsize = ((((buffer[0] >> 6) + 1) >> 1) + 1) << 1;
0b42f8
+      opsize = s390_insn_length (buffer);
0b42f8
       status = opsize > bufsize;
0b42f8
     }
0b42f8
   else
0b42f8
     {
0b42f8
       bufsize = 6;
0b42f8
-      opsize = ((((buffer[0] >> 6) + 1) >> 1) + 1) << 1;
0b42f8
+      opsize = s390_insn_length (buffer);
0b42f8
     }
0b42f8
 
0b42f8
   if (status == 0)
0b42f8
     {
0b42f8
       const struct s390_opcode *op;
0b42f8
 
0b42f8
-      /* Find the first match in the opcode table.  */
0b42f8
-      opcode_end = s390_opcodes + s390_num_opcodes;
0b42f8
-      for (opcode = s390_opcodes + opc_index[(int) buffer[0]];
0b42f8
-	   (opcode < opcode_end) && (buffer[0] == opcode->opcode[0]);
0b42f8
-	   opcode++)
0b42f8
+      /* Find the "best match" in the opcode table.  */
0b42f8
+      for (op = s390_opcodes + opc_index[buffer[0]];
0b42f8
+	   op != s390_opcodes + s390_num_opcodes
0b42f8
+	     && op->opcode[0] == buffer[0];
0b42f8
+	   op++)
0b42f8
 	{
0b42f8
-	  const struct s390_operand *operand;
0b42f8
-	  const unsigned char *opindex;
0b42f8
-
0b42f8
-	  /* Check architecture.  */
0b42f8
-	  if (!(opcode->modes & current_arch_mask))
0b42f8
-	    continue;
0b42f8
-
0b42f8
-	  /* Check signature of the opcode.  */
0b42f8
-	  if ((buffer[1] & opcode->mask[1]) != opcode->opcode[1]
0b42f8
-	      || (buffer[2] & opcode->mask[2]) != opcode->opcode[2]
0b42f8
-	      || (buffer[3] & opcode->mask[3]) != opcode->opcode[3]
0b42f8
-	      || (buffer[4] & opcode->mask[4]) != opcode->opcode[4]
0b42f8
-	      || (buffer[5] & opcode->mask[5]) != opcode->opcode[5])
0b42f8
-	    continue;
0b42f8
-
0b42f8
-	  /* Advance to an opcode with a more specific mask.  */
0b42f8
-	  for (op = opcode + 1; op < opcode_end; op++)
0b42f8
-	    {
0b42f8
-	      if ((buffer[0] & op->mask[0]) != op->opcode[0])
0b42f8
-		break;
0b42f8
-
0b42f8
-	      if ((buffer[1] & op->mask[1]) != op->opcode[1]
0b42f8
-		  || (buffer[2] & op->mask[2]) != op->opcode[2]
0b42f8
-		  || (buffer[3] & op->mask[3]) != op->opcode[3]
0b42f8
-		  || (buffer[4] & op->mask[4]) != op->opcode[4]
0b42f8
-		  || (buffer[5] & op->mask[5]) != op->opcode[5])
0b42f8
-		continue;
0b42f8
-
0b42f8
-	      if (((int)opcode->mask[0] + opcode->mask[1] +
0b42f8
-		   opcode->mask[2] + opcode->mask[3] +
0b42f8
-		   opcode->mask[4] + opcode->mask[5]) <
0b42f8
-		  ((int)op->mask[0] + op->mask[1] +
0b42f8
-		   op->mask[2] + op->mask[3] +
0b42f8
-		   op->mask[4] + op->mask[5]))
0b42f8
-		opcode = op;
0b42f8
-	    }
0b42f8
-
0b42f8
-	  /* The instruction is valid.  */
0b42f8
-	  if (opcode->operands[0] != 0)
0b42f8
-	    (*info->fprintf_func) (info->stream, "%s\t", opcode->name);
0b42f8
-	  else
0b42f8
-	    (*info->fprintf_func) (info->stream, "%s", opcode->name);
0b42f8
-
0b42f8
-	  /* Extract the operands.  */
0b42f8
-	  separator = 0;
0b42f8
-	  for (opindex = opcode->operands; *opindex != 0; opindex++)
0b42f8
-	    {
0b42f8
-	      operand = s390_operands + *opindex;
0b42f8
-	      value = s390_extract_operand (buffer, operand);
0b42f8
-
0b42f8
-	      if ((operand->flags & S390_OPERAND_INDEX) && value == 0)
0b42f8
-		continue;
0b42f8
-	      if ((operand->flags & S390_OPERAND_BASE) &&
0b42f8
-		  value == 0 && separator == '(')
0b42f8
-		{
0b42f8
-		  separator = ',';
0b42f8
-		  continue;
0b42f8
-		}
0b42f8
-
0b42f8
-	      if (separator)
0b42f8
-		(*info->fprintf_func) (info->stream, "%c", separator);
0b42f8
-
0b42f8
-	      if (operand->flags & S390_OPERAND_GPR)
0b42f8
-		(*info->fprintf_func) (info->stream, "%%r%i", value);
0b42f8
-	      else if (operand->flags & S390_OPERAND_FPR)
0b42f8
-		(*info->fprintf_func) (info->stream, "%%f%i", value);
0b42f8
-	      else if (operand->flags & S390_OPERAND_AR)
0b42f8
-		(*info->fprintf_func) (info->stream, "%%a%i", value);
0b42f8
-	      else if (operand->flags & S390_OPERAND_CR)
0b42f8
-		(*info->fprintf_func) (info->stream, "%%c%i", value);
0b42f8
-	      else if (operand->flags & S390_OPERAND_PCREL)
0b42f8
-		(*info->print_address_func) (memaddr + (int)value + (int)value,
0b42f8
-					     info);
0b42f8
-	      else if (operand->flags & S390_OPERAND_SIGNED)
0b42f8
-		(*info->fprintf_func) (info->stream, "%i", (int) value);
0b42f8
-	      else
0b42f8
-		(*info->fprintf_func) (info->stream, "%u", value);
0b42f8
-
0b42f8
-	      if (operand->flags & S390_OPERAND_DISP)
0b42f8
-		{
0b42f8
-		  separator = '(';
0b42f8
-		}
0b42f8
-	      else if (operand->flags & S390_OPERAND_BASE)
0b42f8
-		{
0b42f8
-		  (*info->fprintf_func) (info->stream, ")");
0b42f8
-		  separator = ',';
0b42f8
-		}
0b42f8
-	      else
0b42f8
-		separator = ',';
0b42f8
-	    }
0b42f8
-
0b42f8
-	  /* Found instruction, printed it, return its size.  */
0b42f8
-	  return opsize;
0b42f8
+	  if ((op->modes & current_arch_mask)
0b42f8
+	      && s390_insn_matches_opcode (buffer, op)
0b42f8
+	      && (opcode == NULL
0b42f8
+		  || opcode_mask_more_specific (op, opcode)))
0b42f8
+	    opcode = op;
0b42f8
 	}
0b42f8
-      /* No matching instruction found, fall through to hex print.  */
0b42f8
     }
0b42f8
 
0b42f8
+  if (opcode != NULL)
0b42f8
+    {
0b42f8
+      /* The instruction is valid.  Print it and return its size.  */
0b42f8
+      s390_print_insn_with_opcode (memaddr, info, buffer, opcode);
0b42f8
+      return opsize;
0b42f8
+    }
0b42f8
+
0b42f8
+  /* Fall back to hex print.  */
0b42f8
   if (bufsize >= 4)
0b42f8
     {
0b42f8
       value = (unsigned int) buffer[0];
0b42f8
       value = (value << 8) + (unsigned int) buffer[1];
0b42f8
       value = (value << 8) + (unsigned int) buffer[2];
0b42f8
       value = (value << 8) + (unsigned int) buffer[3];
0b42f8
-      (*info->fprintf_func) (info->stream, ".long\t0x%08x", value);
0b42f8
+      info->fprintf_func (info->stream, ".long\t0x%08x", value);
0b42f8
       return 4;
0b42f8
     }
0b42f8
   else if (bufsize >= 2)
0b42f8
     {
0b42f8
       value = (unsigned int) buffer[0];
0b42f8
       value = (value << 8) + (unsigned int) buffer[1];
0b42f8
-      (*info->fprintf_func) (info->stream, ".short\t0x%04x", value);
0b42f8
+      info->fprintf_func (info->stream, ".short\t0x%04x", value);
0b42f8
       return 2;
0b42f8
     }
0b42f8
   else
0b42f8
     {
0b42f8
       value = (unsigned int) buffer[0];
0b42f8
-      (*info->fprintf_func) (info->stream, ".byte\t0x%02x", value);
0b42f8
+      info->fprintf_func (info->stream, ".byte\t0x%02x", value);
0b42f8
       return 1;
0b42f8
     }
0b42f8
 }