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

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