public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 1/6] S/390: Split disassembler routine into smaller functions
  2014-07-25 17:02 [PATCH 0/6] S/390: Some disassembler cleanup Andreas Arnez
  2014-07-25 17:02 ` [PATCH 2/6] S/390: Fix disassembler's treatment of signed/unsigned operands Andreas Arnez
  2014-07-25 17:02 ` [PATCH 6/6] S/390: Various minor simplifications in disassembler Andreas Arnez
@ 2014-07-25 17:02 ` Andreas Arnez
  2014-07-25 17:02 ` [PATCH 4/6] S/390: Simplify opcode search loop in disassembler Andreas Arnez
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Andreas Arnez @ 2014-07-25 17:02 UTC (permalink / raw)
  To: binutils; +Cc: Martin Schwidefsky, Andreas Krebbel

Extract certain logic from print_insn_s390() into separate functions.
This improves readability and reduces some code repetition.

opcodes/
	* s390-dis.c (s390_insn_length, s390_insn_matches_opcode)
	(s390_print_insn_with_opcode, opcode_mask_more_specific): New
	static functions, code was moved from...
	(print_insn_s390): ...here.
	(s390_extract_operand): Adjust comment.  Change type of first
	parameter from 'unsigned char *' to 'const bfd_byte *'.
---
 opcodes/s390-dis.c | 190 +++++++++++++++++++++++++++++++----------------------
 1 file changed, 110 insertions(+), 80 deletions(-)

diff --git a/opcodes/s390-dis.c b/opcodes/s390-dis.c
index 252500c..444e7ae 100644
--- a/opcodes/s390-dis.c
+++ b/opcodes/s390-dis.c
@@ -69,14 +69,38 @@ init_disasm (struct disassemble_info *info)
   init_flag = 1;
 }
 
+/* Derive the length of an instruction from its first byte.  */
+
+static inline int
+s390_insn_length (const bfd_byte *buffer)
+{
+  /* 00xxxxxx -> 2, 01xxxxxx/10xxxxxx -> 4, 11xxxxxx -> 6.  */
+  return ((((buffer[0] >> 6) + 1) >> 1) + 1) << 1;
+}
+
+/* Match the instruction in BUFFER against the given OPCODE, excluding
+   the first byte.  */
+
+static inline int
+s390_insn_matches_opcode (const bfd_byte *buffer,
+			  const struct s390_opcode *opcode)
+{
+  return (buffer[1] & opcode->mask[1]) == opcode->opcode[1]
+    && (buffer[2] & opcode->mask[2]) == opcode->opcode[2]
+    && (buffer[3] & opcode->mask[3]) == opcode->opcode[3]
+    && (buffer[4] & opcode->mask[4]) == opcode->opcode[4]
+    && (buffer[5] & opcode->mask[5]) == opcode->opcode[5];
+}
+
 /* Extracts an operand value from an instruction.  */
 /* We do not perform the shift operation for larl-type address
    operands here since that would lead to an overflow of the 32 bit
    integer value.  Instead the shift operation is done when printing
-   the operand in print_insn_s390.  */
+   the operand.  */
 
 static inline unsigned int
-s390_extract_operand (unsigned char *insn, const struct s390_operand *operand)
+s390_extract_operand (const bfd_byte *insn,
+		      const struct s390_operand *operand)
 {
   unsigned int val;
   int bits;
@@ -110,6 +134,84 @@ s390_extract_operand (unsigned char *insn, const struct s390_operand *operand)
   return val;
 }
 
+/* Print the S390 instruction in BUFFER, assuming that it matches the
+   given OPCODE.  */
+
+static void
+s390_print_insn_with_opcode (bfd_vma memaddr,
+			     struct disassemble_info *info,
+			     const bfd_byte *buffer,
+			     const struct s390_opcode *opcode)
+{
+  const unsigned char *opindex;
+  char separator;
+
+  if (opcode->operands[0] != 0)
+    (*info->fprintf_func) (info->stream, "%s\t", opcode->name);
+  else
+    (*info->fprintf_func) (info->stream, "%s", opcode->name);
+
+  /* Extract the operands.  */
+  separator = 0;
+  for (opindex = opcode->operands; *opindex != 0; opindex++)
+    {
+      const struct s390_operand *operand = s390_operands + *opindex;
+      unsigned int value = s390_extract_operand (buffer, operand);
+
+      if ((operand->flags & S390_OPERAND_INDEX) && value == 0)
+	continue;
+      if ((operand->flags & S390_OPERAND_BASE) &&
+	  value == 0 && separator == '(')
+	{
+	  separator = ',';
+	  continue;
+	}
+
+      if (separator)
+	(*info->fprintf_func) (info->stream, "%c", separator);
+
+      if (operand->flags & S390_OPERAND_GPR)
+	(*info->fprintf_func) (info->stream, "%%r%i", value);
+      else if (operand->flags & S390_OPERAND_FPR)
+	(*info->fprintf_func) (info->stream, "%%f%i", value);
+      else if (operand->flags & S390_OPERAND_AR)
+	(*info->fprintf_func) (info->stream, "%%a%i", value);
+      else if (operand->flags & S390_OPERAND_CR)
+	(*info->fprintf_func) (info->stream, "%%c%i", value);
+      else if (operand->flags & S390_OPERAND_PCREL)
+	(*info->print_address_func) (memaddr + (int)value + (int)value,
+				     info);
+      else if (operand->flags & S390_OPERAND_SIGNED)
+	(*info->fprintf_func) (info->stream, "%i", (int) value);
+      else
+	(*info->fprintf_func) (info->stream, "%u", value);
+
+      if (operand->flags & S390_OPERAND_DISP)
+	{
+	  separator = '(';
+	}
+      else if (operand->flags & S390_OPERAND_BASE)
+	{
+	  (*info->fprintf_func) (info->stream, ")");
+	  separator = ',';
+	}
+      else
+	separator = ',';
+    }
+}
+
+/* Check whether opcode A's mask is more specific than that of B.  */
+
+static int
+opcode_mask_more_specific (const struct s390_opcode *a,
+			   const struct s390_opcode *b)
+{
+  return ( ((int)a->mask[0] + a->mask[1] + a->mask[2]
+	    + a->mask[3] + a->mask[4] + a->mask[5])
+	   > ((int)b->mask[0] + b->mask[1] + b->mask[2]
+	      + b->mask[3] + b->mask[4] + b->mask[5]) );
+}
+
 /* Print a S390 instruction.  */
 
 int
@@ -120,7 +222,6 @@ print_insn_s390 (bfd_vma memaddr, struct disassemble_info *info)
   const struct s390_opcode *opcode_end;
   unsigned int value;
   int status, opsize, bufsize;
-  char separator;
 
   if (init_flag == 0)
     init_disasm (info);
@@ -141,16 +242,13 @@ print_insn_s390 (bfd_vma memaddr, struct disassemble_info *info)
 	  (*info->memory_error_func) (status, memaddr, info);
 	  return -1;
 	}
-      /* Opsize calculation looks strange but it works
-	 00xxxxxx -> 2 bytes, 01xxxxxx/10xxxxxx -> 4 bytes,
-	 11xxxxxx -> 6 bytes.  */
-      opsize = ((((buffer[0] >> 6) + 1) >> 1) + 1) << 1;
+      opsize = s390_insn_length (buffer);
       status = opsize > bufsize;
     }
   else
     {
       bufsize = 6;
-      opsize = ((((buffer[0] >> 6) + 1) >> 1) + 1) << 1;
+      opsize = s390_insn_length (buffer);
     }
 
   if (status == 0)
@@ -163,19 +261,11 @@ print_insn_s390 (bfd_vma memaddr, struct disassemble_info *info)
 	   (opcode < opcode_end) && (buffer[0] == opcode->opcode[0]);
 	   opcode++)
 	{
-	  const struct s390_operand *operand;
-	  const unsigned char *opindex;
-
 	  /* Check architecture.  */
 	  if (!(opcode->modes & current_arch_mask))
 	    continue;
 
-	  /* Check signature of the opcode.  */
-	  if ((buffer[1] & opcode->mask[1]) != opcode->opcode[1]
-	      || (buffer[2] & opcode->mask[2]) != opcode->opcode[2]
-	      || (buffer[3] & opcode->mask[3]) != opcode->opcode[3]
-	      || (buffer[4] & opcode->mask[4]) != opcode->opcode[4]
-	      || (buffer[5] & opcode->mask[5]) != opcode->opcode[5])
+	  if (!s390_insn_matches_opcode (buffer, opcode))
 	    continue;
 
 	  /* Advance to an opcode with a more specific mask.  */
@@ -184,75 +274,15 @@ print_insn_s390 (bfd_vma memaddr, struct disassemble_info *info)
 	      if ((buffer[0] & op->mask[0]) != op->opcode[0])
 		break;
 
-	      if ((buffer[1] & op->mask[1]) != op->opcode[1]
-		  || (buffer[2] & op->mask[2]) != op->opcode[2]
-		  || (buffer[3] & op->mask[3]) != op->opcode[3]
-		  || (buffer[4] & op->mask[4]) != op->opcode[4]
-		  || (buffer[5] & op->mask[5]) != op->opcode[5])
+	      if (!s390_insn_matches_opcode (buffer, op))
 		continue;
 
-	      if (((int)opcode->mask[0] + opcode->mask[1] +
-		   opcode->mask[2] + opcode->mask[3] +
-		   opcode->mask[4] + opcode->mask[5]) <
-		  ((int)op->mask[0] + op->mask[1] +
-		   op->mask[2] + op->mask[3] +
-		   op->mask[4] + op->mask[5]))
+	      if (opcode_mask_more_specific (op, opcode))
 		opcode = op;
 	    }
 
 	  /* The instruction is valid.  */
-	  if (opcode->operands[0] != 0)
-	    (*info->fprintf_func) (info->stream, "%s\t", opcode->name);
-	  else
-	    (*info->fprintf_func) (info->stream, "%s", opcode->name);
-
-	  /* Extract the operands.  */
-	  separator = 0;
-	  for (opindex = opcode->operands; *opindex != 0; opindex++)
-	    {
-	      operand = s390_operands + *opindex;
-	      value = s390_extract_operand (buffer, operand);
-
-	      if ((operand->flags & S390_OPERAND_INDEX) && value == 0)
-		continue;
-	      if ((operand->flags & S390_OPERAND_BASE) &&
-		  value == 0 && separator == '(')
-		{
-		  separator = ',';
-		  continue;
-		}
-
-	      if (separator)
-		(*info->fprintf_func) (info->stream, "%c", separator);
-
-	      if (operand->flags & S390_OPERAND_GPR)
-		(*info->fprintf_func) (info->stream, "%%r%i", value);
-	      else if (operand->flags & S390_OPERAND_FPR)
-		(*info->fprintf_func) (info->stream, "%%f%i", value);
-	      else if (operand->flags & S390_OPERAND_AR)
-		(*info->fprintf_func) (info->stream, "%%a%i", value);
-	      else if (operand->flags & S390_OPERAND_CR)
-		(*info->fprintf_func) (info->stream, "%%c%i", value);
-	      else if (operand->flags & S390_OPERAND_PCREL)
-		(*info->print_address_func) (memaddr + (int)value + (int)value,
-					     info);
-	      else if (operand->flags & S390_OPERAND_SIGNED)
-		(*info->fprintf_func) (info->stream, "%i", (int) value);
-	      else
-		(*info->fprintf_func) (info->stream, "%u", value);
-
-	      if (operand->flags & S390_OPERAND_DISP)
-		{
-		  separator = '(';
-		}
-	      else if (operand->flags & S390_OPERAND_BASE)
-		{
-		  (*info->fprintf_func) (info->stream, ")");
-		  separator = ',';
-		}
-	      else
-		separator = ',';
-	    }
+	  s390_print_insn_with_opcode (memaddr, info, buffer, opcode);
 
 	  /* Found instruction, printed it, return its size.  */
 	  return opsize;
-- 
1.8.4.2

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 4/6] S/390: Simplify opcode search loop in disassembler
  2014-07-25 17:02 [PATCH 0/6] S/390: Some disassembler cleanup Andreas Arnez
                   ` (2 preceding siblings ...)
  2014-07-25 17:02 ` [PATCH 1/6] S/390: Split disassembler routine into smaller functions Andreas Arnez
@ 2014-07-25 17:02 ` Andreas Arnez
  2014-07-25 17:02 ` [PATCH 5/6] S/390: Drop function pointer dereferences " Andreas Arnez
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Andreas Arnez @ 2014-07-25 17:02 UTC (permalink / raw)
  To: binutils; +Cc: Martin Schwidefsky, Andreas Krebbel

opcodes/
	* s390-dis.c (print_insn_s390): Simplify the opcode search loop.
	Check architecture mask against all searched opcodes, not just the
	first matching one.
---
 opcodes/s390-dis.c | 52 +++++++++++++++++++---------------------------------
 1 file changed, 19 insertions(+), 33 deletions(-)

diff --git a/opcodes/s390-dis.c b/opcodes/s390-dis.c
index e6b0ee5..029f9f1 100644
--- a/opcodes/s390-dis.c
+++ b/opcodes/s390-dis.c
@@ -229,8 +229,7 @@ int
 print_insn_s390 (bfd_vma memaddr, struct disassemble_info *info)
 {
   bfd_byte buffer[6];
-  const struct s390_opcode *opcode;
-  const struct s390_opcode *opcode_end;
+  const struct s390_opcode *opcode = NULL;
   unsigned int value;
   int status, opsize, bufsize;
 
@@ -266,41 +265,28 @@ print_insn_s390 (bfd_vma memaddr, struct disassemble_info *info)
     {
       const struct s390_opcode *op;
 
-      /* Find the first match in the opcode table.  */
-      opcode_end = s390_opcodes + s390_num_opcodes;
-      for (opcode = s390_opcodes + opc_index[(int) buffer[0]];
-	   (opcode < opcode_end) && (buffer[0] == opcode->opcode[0]);
-	   opcode++)
+      /* Find the "best match" in the opcode table.  */
+      for (op = s390_opcodes + opc_index[buffer[0]];
+	   op != s390_opcodes + s390_num_opcodes
+	     && op->opcode[0] == buffer[0];
+	   op++)
 	{
-	  /* Check architecture.  */
-	  if (!(opcode->modes & current_arch_mask))
-	    continue;
-
-	  if (!s390_insn_matches_opcode (buffer, opcode))
-	    continue;
-
-	  /* Advance to an opcode with a more specific mask.  */
-	  for (op = opcode + 1; op < opcode_end; op++)
-	    {
-	      if ((buffer[0] & op->mask[0]) != op->opcode[0])
-		break;
-
-	      if (!s390_insn_matches_opcode (buffer, op))
-		continue;
-
-	      if (opcode_mask_more_specific (op, opcode))
-		opcode = op;
-	    }
-
-	  /* The instruction is valid.  */
-	  s390_print_insn_with_opcode (memaddr, info, buffer, opcode);
-
-	  /* Found instruction, printed it, return its size.  */
-	  return opsize;
+	  if ((op->modes & current_arch_mask)
+	      && s390_insn_matches_opcode (buffer, op)
+	      && (opcode == NULL
+		  || opcode_mask_more_specific (op, opcode)))
+	    opcode = op;
 	}
-      /* No matching instruction found, fall through to hex print.  */
     }
 
+  if (opcode != NULL)
+    {
+      /* The instruction is valid.  Print it and return its size.  */
+      s390_print_insn_with_opcode (memaddr, info, buffer, opcode);
+      return opsize;
+    }
+
+  /* Fall back to hex print.  */
   if (bufsize >= 4)
     {
       value = (unsigned int) buffer[0];
-- 
1.8.4.2

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 2/6] S/390: Fix disassembler's treatment of signed/unsigned operands
  2014-07-25 17:02 [PATCH 0/6] S/390: Some disassembler cleanup Andreas Arnez
@ 2014-07-25 17:02 ` Andreas Arnez
  2014-07-25 17:02 ` [PATCH 6/6] S/390: Various minor simplifications in disassembler Andreas Arnez
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Andreas Arnez @ 2014-07-25 17:02 UTC (permalink / raw)
  To: binutils; +Cc: Martin Schwidefsky, Andreas Krebbel

Currently the S/390 disassembler relies on implementation-defined
behavior upon integer overflow and sometimes uses the "%i" printf
format for unsigned values.  This patch intends to fix these.

opcodes/
	* s390-dis.c (union operand_value): New.
	(s390_extract_operand): Change return type to union operand_value.
	Also avoid integer overflow in sign-extension.
	(s390_print_insn_with_opcode): Adjust to changed return value from
	s390_extract_operand().  Change "%i" printf format to "%u" for
	unsigned values.
---
 opcodes/s390-dis.c | 55 ++++++++++++++++++++++++++++++++++--------------------
 1 file changed, 35 insertions(+), 20 deletions(-)

diff --git a/opcodes/s390-dis.c b/opcodes/s390-dis.c
index 444e7ae..47c449a 100644
--- a/opcodes/s390-dis.c
+++ b/opcodes/s390-dis.c
@@ -92,16 +92,23 @@ s390_insn_matches_opcode (const bfd_byte *buffer,
     && (buffer[5] & opcode->mask[5]) == opcode->opcode[5];
 }
 
+union operand_value
+{
+  int i;
+  unsigned int u;
+};
+
 /* Extracts an operand value from an instruction.  */
 /* We do not perform the shift operation for larl-type address
    operands here since that would lead to an overflow of the 32 bit
    integer value.  Instead the shift operation is done when printing
    the operand.  */
 
-static inline unsigned int
+static inline union operand_value
 s390_extract_operand (const bfd_byte *insn,
 		      const struct s390_operand *operand)
 {
+  union operand_value ret;
   unsigned int val;
   int bits;
 
@@ -123,15 +130,24 @@ s390_extract_operand (const bfd_byte *insn,
   if (operand->bits == 20 && operand->shift == 20)
     val = (val & 0xff) << 12 | (val & 0xfff00) >> 8;
 
-  /* Sign extend value if the operand is signed or pc relative.  */
-  if ((operand->flags & (S390_OPERAND_SIGNED | S390_OPERAND_PCREL))
-      && (val & (1U << (operand->bits - 1))))
-    val |= (-1U << (operand->bits - 1)) << 1;
+  /* Sign extend value if the operand is signed or pc relative.  Avoid
+     integer overflows.  */
+  if (operand->flags & (S390_OPERAND_SIGNED | S390_OPERAND_PCREL))
+    {
+      unsigned int m = 1U << (operand->bits - 1);
+
+      if (val >= m)
+	ret.i = (int) (val - m) - 1 - (int) (m - 1U);
+      else
+	ret.i = (int) val;
+    }
+  else if (operand->flags & S390_OPERAND_LENGTH)
+    /* Length x in an instruction has real length x + 1.  */
+    ret.u = val + 1;
+  else
+    ret.u = val;
 
-  /* Length x in an instructions has real length x + 1.  */
-  if (operand->flags & S390_OPERAND_LENGTH)
-    val++;
-  return val;
+  return ret;
 }
 
 /* Print the S390 instruction in BUFFER, assuming that it matches the
@@ -156,12 +172,12 @@ s390_print_insn_with_opcode (bfd_vma memaddr,
   for (opindex = opcode->operands; *opindex != 0; opindex++)
     {
       const struct s390_operand *operand = s390_operands + *opindex;
-      unsigned int value = s390_extract_operand (buffer, operand);
+      union operand_value val = s390_extract_operand (buffer, operand);
 
-      if ((operand->flags & S390_OPERAND_INDEX) && value == 0)
+      if ((operand->flags & S390_OPERAND_INDEX) && val.u == 0)
 	continue;
       if ((operand->flags & S390_OPERAND_BASE) &&
-	  value == 0 && separator == '(')
+	  val.u == 0 && separator == '(')
 	{
 	  separator = ',';
 	  continue;
@@ -171,20 +187,19 @@ s390_print_insn_with_opcode (bfd_vma memaddr,
 	(*info->fprintf_func) (info->stream, "%c", separator);
 
       if (operand->flags & S390_OPERAND_GPR)
-	(*info->fprintf_func) (info->stream, "%%r%i", value);
+	(*info->fprintf_func) (info->stream, "%%r%u", val.u);
       else if (operand->flags & S390_OPERAND_FPR)
-	(*info->fprintf_func) (info->stream, "%%f%i", value);
+	(*info->fprintf_func) (info->stream, "%%f%u", val.u);
       else if (operand->flags & S390_OPERAND_AR)
-	(*info->fprintf_func) (info->stream, "%%a%i", value);
+	(*info->fprintf_func) (info->stream, "%%a%u", val.u);
       else if (operand->flags & S390_OPERAND_CR)
-	(*info->fprintf_func) (info->stream, "%%c%i", value);
+	(*info->fprintf_func) (info->stream, "%%c%u", val.u);
       else if (operand->flags & S390_OPERAND_PCREL)
-	(*info->print_address_func) (memaddr + (int)value + (int)value,
-				     info);
+	(*info->print_address_func) (memaddr + val.i + val.i, info);
       else if (operand->flags & S390_OPERAND_SIGNED)
-	(*info->fprintf_func) (info->stream, "%i", (int) value);
+	(*info->fprintf_func) (info->stream, "%i", val.i);
       else
-	(*info->fprintf_func) (info->stream, "%u", value);
+	(*info->fprintf_func) (info->stream, "%u", val.u);
 
       if (operand->flags & S390_OPERAND_DISP)
 	{
-- 
1.8.4.2

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 0/6] S/390: Some disassembler cleanup
@ 2014-07-25 17:02 Andreas Arnez
  2014-07-25 17:02 ` [PATCH 2/6] S/390: Fix disassembler's treatment of signed/unsigned operands Andreas Arnez
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: Andreas Arnez @ 2014-07-25 17:02 UTC (permalink / raw)
  To: binutils; +Cc: Martin Schwidefsky, Andreas Krebbel

This patch set mainly aims at improving the S/390 disassembler's
readability and also fixes some minor issues.

Andreas Arnez (6):
  S/390: Split disassembler routine into smaller functions
  S/390: Fix disassembler's treatment of signed/unsigned operands
  S/390: Fix off-by-one error in disassembler initialization
  S/390: Simplify opcode search loop in disassembler
  S/390: Drop function pointer dereferences in disassembler
  S/390: Various minor simplifications in disassembler

 opcodes/s390-dis.c | 291 +++++++++++++++++++++++++++++------------------------
 1 file changed, 157 insertions(+), 134 deletions(-)

-- 
1.8.4.2

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 5/6] S/390: Drop function pointer dereferences in disassembler
  2014-07-25 17:02 [PATCH 0/6] S/390: Some disassembler cleanup Andreas Arnez
                   ` (3 preceding siblings ...)
  2014-07-25 17:02 ` [PATCH 4/6] S/390: Simplify opcode search loop in disassembler Andreas Arnez
@ 2014-07-25 17:02 ` Andreas Arnez
  2014-07-25 17:02 ` [PATCH 3/6] S/390: Fix off-by-one error in disassembler initialization Andreas Arnez
  2014-08-19 14:43 ` [PATCH 0/6] S/390: Some disassembler cleanup Nicholas Clifton
  6 siblings, 0 replies; 8+ messages in thread
From: Andreas Arnez @ 2014-07-25 17:02 UTC (permalink / raw)
  To: binutils; +Cc: Martin Schwidefsky, Andreas Krebbel

Convert occurrences of "(*foo->bar) ()" to "foo->bar ()".

opcodes/
	* s390-dis.c (s390_print_insn_with_opcode): Drop function pointer
	dereferences without effect.
	(print_insn_s390): Likewise.
---
 opcodes/s390-dis.c | 34 +++++++++++++++++-----------------
 1 file changed, 17 insertions(+), 17 deletions(-)

diff --git a/opcodes/s390-dis.c b/opcodes/s390-dis.c
index 029f9f1..891970f 100644
--- a/opcodes/s390-dis.c
+++ b/opcodes/s390-dis.c
@@ -159,9 +159,9 @@ s390_print_insn_with_opcode (bfd_vma memaddr,
   char separator;
 
   if (opcode->operands[0] != 0)
-    (*info->fprintf_func) (info->stream, "%s\t", opcode->name);
+    info->fprintf_func (info->stream, "%s\t", opcode->name);
   else
-    (*info->fprintf_func) (info->stream, "%s", opcode->name);
+    info->fprintf_func (info->stream, "%s", opcode->name);
 
   /* Extract the operands.  */
   separator = 0;
@@ -180,22 +180,22 @@ s390_print_insn_with_opcode (bfd_vma memaddr,
 	}
 
       if (separator)
-	(*info->fprintf_func) (info->stream, "%c", separator);
+	info->fprintf_func (info->stream, "%c", separator);
 
       if (operand->flags & S390_OPERAND_GPR)
-	(*info->fprintf_func) (info->stream, "%%r%u", val.u);
+	info->fprintf_func (info->stream, "%%r%u", val.u);
       else if (operand->flags & S390_OPERAND_FPR)
-	(*info->fprintf_func) (info->stream, "%%f%u", val.u);
+	info->fprintf_func (info->stream, "%%f%u", val.u);
       else if (operand->flags & S390_OPERAND_AR)
-	(*info->fprintf_func) (info->stream, "%%a%u", val.u);
+	info->fprintf_func (info->stream, "%%a%u", val.u);
       else if (operand->flags & S390_OPERAND_CR)
-	(*info->fprintf_func) (info->stream, "%%c%u", val.u);
+	info->fprintf_func (info->stream, "%%c%u", val.u);
       else if (operand->flags & S390_OPERAND_PCREL)
-	(*info->print_address_func) (memaddr + val.i + val.i, info);
+	info->print_address_func (memaddr + val.i + val.i, info);
       else if (operand->flags & S390_OPERAND_SIGNED)
-	(*info->fprintf_func) (info->stream, "%i", val.i);
+	info->fprintf_func (info->stream, "%i", val.i);
       else
-	(*info->fprintf_func) (info->stream, "%u", val.u);
+	info->fprintf_func (info->stream, "%u", val.u);
 
       if (operand->flags & S390_OPERAND_DISP)
 	{
@@ -203,7 +203,7 @@ s390_print_insn_with_opcode (bfd_vma memaddr,
 	}
       else if (operand->flags & S390_OPERAND_BASE)
 	{
-	  (*info->fprintf_func) (info->stream, ")");
+	  info->fprintf_func (info->stream, ")");
 	  separator = ',';
 	}
       else
@@ -241,15 +241,15 @@ print_insn_s390 (bfd_vma memaddr, struct disassemble_info *info)
 
   /* Every S390 instruction is max 6 bytes long.  */
   memset (buffer, 0, 6);
-  status = (*info->read_memory_func) (memaddr, buffer, 6, info);
+  status = info->read_memory_func (memaddr, buffer, 6, info);
   if (status != 0)
     {
       for (bufsize = 0; bufsize < 6; bufsize++)
-	if ((*info->read_memory_func) (memaddr, buffer, bufsize + 1, info) != 0)
+	if (info->read_memory_func (memaddr, buffer, bufsize + 1, info) != 0)
 	  break;
       if (bufsize <= 0)
 	{
-	  (*info->memory_error_func) (status, memaddr, info);
+	  info->memory_error_func (status, memaddr, info);
 	  return -1;
 	}
       opsize = s390_insn_length (buffer);
@@ -293,20 +293,20 @@ print_insn_s390 (bfd_vma memaddr, struct disassemble_info *info)
       value = (value << 8) + (unsigned int) buffer[1];
       value = (value << 8) + (unsigned int) buffer[2];
       value = (value << 8) + (unsigned int) buffer[3];
-      (*info->fprintf_func) (info->stream, ".long\t0x%08x", value);
+      info->fprintf_func (info->stream, ".long\t0x%08x", value);
       return 4;
     }
   else if (bufsize >= 2)
     {
       value = (unsigned int) buffer[0];
       value = (value << 8) + (unsigned int) buffer[1];
-      (*info->fprintf_func) (info->stream, ".short\t0x%04x", value);
+      info->fprintf_func (info->stream, ".short\t0x%04x", value);
       return 2;
     }
   else
     {
       value = (unsigned int) buffer[0];
-      (*info->fprintf_func) (info->stream, ".byte\t0x%02x", value);
+      info->fprintf_func (info->stream, ".byte\t0x%02x", value);
       return 1;
     }
 }
-- 
1.8.4.2

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 3/6] S/390: Fix off-by-one error in disassembler initialization
  2014-07-25 17:02 [PATCH 0/6] S/390: Some disassembler cleanup Andreas Arnez
                   ` (4 preceding siblings ...)
  2014-07-25 17:02 ` [PATCH 5/6] S/390: Drop function pointer dereferences " Andreas Arnez
@ 2014-07-25 17:02 ` Andreas Arnez
  2014-08-19 14:43 ` [PATCH 0/6] S/390: Some disassembler cleanup Nicholas Clifton
  6 siblings, 0 replies; 8+ messages in thread
From: Andreas Arnez @ 2014-07-25 17:02 UTC (permalink / raw)
  To: binutils; +Cc: Martin Schwidefsky, Andreas Krebbel

opcodes/
	* s390-dis.c (init_disasm): Simplify initialization of
	opc_index[].  This also fixes an access after the last element of
	s390_opcodes[].
---
 opcodes/s390-dis.c | 16 ++++++----------
 1 file changed, 6 insertions(+), 10 deletions(-)

diff --git a/opcodes/s390-dis.c b/opcodes/s390-dis.c
index 47c449a..e6b0ee5 100644
--- a/opcodes/s390-dis.c
+++ b/opcodes/s390-dis.c
@@ -35,19 +35,15 @@ static int current_arch_mask = 0;
 static void
 init_disasm (struct disassemble_info *info)
 {
-  const struct s390_opcode *opcode;
-  const struct s390_opcode *opcode_end;
+  int i;
   const char *p;
 
   memset (opc_index, 0, sizeof (opc_index));
-  opcode_end = s390_opcodes + s390_num_opcodes;
-  for (opcode = s390_opcodes; opcode < opcode_end; opcode++)
-    {
-      opc_index[(int) opcode->opcode[0]] = opcode - s390_opcodes;
-      while ((opcode < opcode_end) &&
-	     (opcode[1].opcode[0] == opcode->opcode[0]))
-	opcode++;
-    }
+
+  /* Reverse order, such that each opc_index ends up pointing to the
+     first matching entry instead of the last.  */
+  for (i = s390_num_opcodes; i--; )
+    opc_index[s390_opcodes[i].opcode[0]] = i;
 
   for (p = info->disassembler_options; p != NULL; )
     {
-- 
1.8.4.2

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 6/6] S/390: Various minor simplifications in disassembler
  2014-07-25 17:02 [PATCH 0/6] S/390: Some disassembler cleanup Andreas Arnez
  2014-07-25 17:02 ` [PATCH 2/6] S/390: Fix disassembler's treatment of signed/unsigned operands Andreas Arnez
@ 2014-07-25 17:02 ` Andreas Arnez
  2014-07-25 17:02 ` [PATCH 1/6] S/390: Split disassembler routine into smaller functions Andreas Arnez
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Andreas Arnez @ 2014-07-25 17:02 UTC (permalink / raw)
  To: binutils; +Cc: Martin Schwidefsky, Andreas Krebbel

opcodes/
	* s390-dis.c (s390_insn_length): Simplify formula for return
	value.
	(s390_print_insn_with_opcode): Avoid special handling for the
	separator before the first operand.  Use new local variable
	'flags' in place of 'operand->flags'.
---
 opcodes/s390-dis.c | 40 ++++++++++++++++++----------------------
 1 file changed, 18 insertions(+), 22 deletions(-)

diff --git a/opcodes/s390-dis.c b/opcodes/s390-dis.c
index 891970f..5cfc72b 100644
--- a/opcodes/s390-dis.c
+++ b/opcodes/s390-dis.c
@@ -71,7 +71,7 @@ static inline int
 s390_insn_length (const bfd_byte *buffer)
 {
   /* 00xxxxxx -> 2, 01xxxxxx/10xxxxxx -> 4, 11xxxxxx -> 6.  */
-  return ((((buffer[0] >> 6) + 1) >> 1) + 1) << 1;
+  return ((buffer[0] >> 6) + 3) & ~1U;
 }
 
 /* Match the instruction in BUFFER against the given OPCODE, excluding
@@ -158,50 +158,46 @@ s390_print_insn_with_opcode (bfd_vma memaddr,
   const unsigned char *opindex;
   char separator;
 
-  if (opcode->operands[0] != 0)
-    info->fprintf_func (info->stream, "%s\t", opcode->name);
-  else
-    info->fprintf_func (info->stream, "%s", opcode->name);
+  /* Mnemonic.  */
+  info->fprintf_func (info->stream, "%s", opcode->name);
 
-  /* Extract the operands.  */
-  separator = 0;
+  /* Operands.  */
+  separator = '\t';
   for (opindex = opcode->operands; *opindex != 0; opindex++)
     {
       const struct s390_operand *operand = s390_operands + *opindex;
       union operand_value val = s390_extract_operand (buffer, operand);
+      unsigned long flags = operand->flags;
 
-      if ((operand->flags & S390_OPERAND_INDEX) && val.u == 0)
+      if ((flags & S390_OPERAND_INDEX) && val.u == 0)
 	continue;
-      if ((operand->flags & S390_OPERAND_BASE) &&
+      if ((flags & S390_OPERAND_BASE) &&
 	  val.u == 0 && separator == '(')
 	{
 	  separator = ',';
 	  continue;
 	}
 
-      if (separator)
-	info->fprintf_func (info->stream, "%c", separator);
+      info->fprintf_func (info->stream, "%c", separator);
 
-      if (operand->flags & S390_OPERAND_GPR)
+      if (flags & S390_OPERAND_GPR)
 	info->fprintf_func (info->stream, "%%r%u", val.u);
-      else if (operand->flags & S390_OPERAND_FPR)
+      else if (flags & S390_OPERAND_FPR)
 	info->fprintf_func (info->stream, "%%f%u", val.u);
-      else if (operand->flags & S390_OPERAND_AR)
+      else if (flags & S390_OPERAND_AR)
 	info->fprintf_func (info->stream, "%%a%u", val.u);
-      else if (operand->flags & S390_OPERAND_CR)
+      else if (flags & S390_OPERAND_CR)
 	info->fprintf_func (info->stream, "%%c%u", val.u);
-      else if (operand->flags & S390_OPERAND_PCREL)
+      else if (flags & S390_OPERAND_PCREL)
 	info->print_address_func (memaddr + val.i + val.i, info);
-      else if (operand->flags & S390_OPERAND_SIGNED)
+      else if (flags & S390_OPERAND_SIGNED)
 	info->fprintf_func (info->stream, "%i", val.i);
       else
 	info->fprintf_func (info->stream, "%u", val.u);
 
-      if (operand->flags & S390_OPERAND_DISP)
-	{
-	  separator = '(';
-	}
-      else if (operand->flags & S390_OPERAND_BASE)
+      if (flags & S390_OPERAND_DISP)
+	separator = '(';
+      else if (flags & S390_OPERAND_BASE)
 	{
 	  info->fprintf_func (info->stream, ")");
 	  separator = ',';
-- 
1.8.4.2

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 0/6] S/390: Some disassembler cleanup
  2014-07-25 17:02 [PATCH 0/6] S/390: Some disassembler cleanup Andreas Arnez
                   ` (5 preceding siblings ...)
  2014-07-25 17:02 ` [PATCH 3/6] S/390: Fix off-by-one error in disassembler initialization Andreas Arnez
@ 2014-08-19 14:43 ` Nicholas Clifton
  6 siblings, 0 replies; 8+ messages in thread
From: Nicholas Clifton @ 2014-08-19 14:43 UTC (permalink / raw)
  To: Andreas Arnez, binutils; +Cc: Martin Schwidefsky, Andreas Krebbel

Hi Andreas,

> This patch set mainly aims at improving the S/390 disassembler's
> readability and also fixes some minor issues.
>
> Andreas Arnez (6):
>    S/390: Split disassembler routine into smaller functions
>    S/390: Fix disassembler's treatment of signed/unsigned operands
>    S/390: Fix off-by-one error in disassembler initialization
>    S/390: Simplify opcode search loop in disassembler
>    S/390: Drop function pointer dereferences in disassembler
>    S/390: Various minor simplifications in disassembler

Approved and applied.  Thanks for creating these patches.

Cheers
   Nick


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2014-08-19 14:43 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-07-25 17:02 [PATCH 0/6] S/390: Some disassembler cleanup Andreas Arnez
2014-07-25 17:02 ` [PATCH 2/6] S/390: Fix disassembler's treatment of signed/unsigned operands Andreas Arnez
2014-07-25 17:02 ` [PATCH 6/6] S/390: Various minor simplifications in disassembler Andreas Arnez
2014-07-25 17:02 ` [PATCH 1/6] S/390: Split disassembler routine into smaller functions Andreas Arnez
2014-07-25 17:02 ` [PATCH 4/6] S/390: Simplify opcode search loop in disassembler Andreas Arnez
2014-07-25 17:02 ` [PATCH 5/6] S/390: Drop function pointer dereferences " Andreas Arnez
2014-07-25 17:02 ` [PATCH 3/6] S/390: Fix off-by-one error in disassembler initialization Andreas Arnez
2014-08-19 14:43 ` [PATCH 0/6] S/390: Some disassembler cleanup Nicholas Clifton

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).