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