Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Yao Qi <qiyaoltc@gmail.com>
To: binutils@sourceware.org,	gdb-patches@sourceware.org
Subject: [PATCH 5/8] Remove magic numbers in m68k-dis.c:print_insn_arg
Date: Tue, 10 Jan 2017 12:26:00 -0000	[thread overview]
Message-ID: <1484051178-16013-6-git-send-email-yao.qi@linaro.org> (raw)
In-Reply-To: <1484051178-16013-1-git-send-email-yao.qi@linaro.org>

When I inspect the return values of disassmblers, I happen to see
various -1/-2/-3 magic numbers are used in m68k-dis.c.  This patch
is to replace them with enum.

-1 and -2 is "clearly documented" in print_ins_arg's comments, but
-3 isn't.  In fact, -3 is returned when FETCH_DATA returns false,
which means memory error (because fetch_data return 0 on memory
error).  So I name enum PRINT_INSN_ARG_MEMORY_ERROR for -3.

This patch is a refactor patch, doesn't affect any functionality.

opcodes:

2017-01-10  Yao Qi  <yao.qi@linaro.org>

	* m68k-dis.c (enum print_insn_arg_error): New.
	(NEXTBYTE): Replace -3 with
	PRINT_INSN_ARG_MEMORY_ERROR.
	(NEXTULONG): Likewise.
	(NEXTSINGLE): Likewise.
	(NEXTDOUBLE): Likewise.
	(NEXTDOUBLE): Likewise.
	(NEXTPACKED): Likewise.
	(FETCH_DATA): Update comments.
	(print_insn_arg): Update comments. Replace magic numbers with
	enum.
	(match_insn_m68k): Likewise.
---
 opcodes/m68k-dis.c | 95 +++++++++++++++++++++++++++++++-----------------------
 1 file changed, 55 insertions(+), 40 deletions(-)

diff --git a/opcodes/m68k-dis.c b/opcodes/m68k-dis.c
index de88f23..3159a47 100644
--- a/opcodes/m68k-dis.c
+++ b/opcodes/m68k-dis.c
@@ -57,13 +57,27 @@ static char *const reg_half_names[] =
 #define COERCE_SIGNED_CHAR(ch) ((int) (((ch) ^ 0x80) & 0xFF) - 128)
 #endif
 
+/* Error code of print_insn_arg's return value.  */
+
+enum print_insn_arg_error
+  {
+    /* An invalid operand is found.  */
+    PRINT_INSN_ARG_INVALID_OPERAND = -1,
+
+    /* An opcode table error.  */
+    PRINT_INSN_ARG_INVALID_OP_TABLE = -2,
+
+    /* A memory error.  */
+    PRINT_INSN_ARG_MEMORY_ERROR = -3,
+  };
+
 /* Get a 1 byte signed integer.  */
 #define NEXTBYTE(p, val)			\
   do						\
     {						\
       p += 2;					\
       if (!FETCH_DATA (info, p))		\
-	return -3;				\
+	return PRINT_INSN_ARG_MEMORY_ERROR;	\
       val = COERCE_SIGNED_CHAR (p[-1]);		\
     }						\
   while (0)
@@ -100,7 +114,7 @@ static char *const reg_half_names[] =
     {									\
       p += 4;								\
       if (!FETCH_DATA (info, p))					\
-	return -3;							\
+	return PRINT_INSN_ARG_MEMORY_ERROR;				\
       val = (unsigned int) ((((((p[-4] << 8) + p[-3]) << 8) + p[-2]) << 8) + p[-1]); \
     }									\
   while (0)
@@ -111,7 +125,7 @@ static char *const reg_half_names[] =
     {								\
       p += 4;							\
       if (!FETCH_DATA (info, p))				\
-	return -3;						\
+	return PRINT_INSN_ARG_MEMORY_ERROR;			\
       floatformat_to_double (& floatformat_ieee_single_big,	\
 			     (char *) p - 4, & val);		\
     }								\
@@ -123,7 +137,7 @@ static char *const reg_half_names[] =
     {								\
       p += 8;							\
       if (!FETCH_DATA (info, p))				\
-	return -3;						\
+	return PRINT_INSN_ARG_MEMORY_ERROR;			\
       floatformat_to_double (& floatformat_ieee_double_big,	\
 			     (char *) p - 8, & val);		\
     }								\
@@ -135,7 +149,7 @@ static char *const reg_half_names[] =
     {							\
       p += 12;						\
       if (!FETCH_DATA (info, p))			\
-	return -3;					\
+	return PRINT_INSN_ARG_MEMORY_ERROR;		\
       floatformat_to_double (& floatformat_m68881_ext,	\
 			     (char *) p - 12, & val);	\
     }							\
@@ -150,7 +164,7 @@ static char *const reg_half_names[] =
     {						\
       p += 12;					\
       if (!FETCH_DATA (info, p))		\
-	return -3;				\
+	return PRINT_INSN_ARG_MEMORY_ERROR;	\
       val = 0.0;				\
     }						\
   while (0)
@@ -168,7 +182,8 @@ struct private
 };
 
 /* Make sure that bytes from INFO->PRIVATE_DATA->BUFFER (inclusive)
-   to ADDR (exclusive) are valid.  Returns 1 for success, 0 on error.  */
+   to ADDR (exclusive) are valid.  Returns 1 for success, 0 on memory
+   error.  */
 #define FETCH_DATA(info, addr) \
   ((addr) <= ((struct private *) (info->private_data))->max_fetched \
    ? 1 : fetch_data ((info), (addr)))
@@ -622,9 +637,8 @@ print_indexed (int basereg,
   while (0)
 
 /* Returns number of bytes "eaten" by the operand, or
-   return -1 if an invalid operand was found, or -2 if
-   an opcode tabe error was found or -3 to simply abort.
-   ADDR is the pc for this arg to be relative to.  */
+   return enum print_insn_arg_error.  ADDR is the pc for this arg to be
+   relative to.  */
 
 static int
 print_insn_arg (const char *d,
@@ -864,7 +878,7 @@ print_insn_arg (const char *d,
 	  (*info->fprintf_func) (info->stream, "{#%d}", val);
 	}
       else
-	return -1;
+	return PRINT_INSN_ARG_INVALID_OPERAND;
       break;
 
     case '#':
@@ -881,11 +895,11 @@ print_insn_arg (const char *d,
       else if (place == 'b')
 	NEXTBYTE (p1, val);
       else if (place == 'w' || place == 'W')
-	NEXTWORD (p1, val, -3);
+	NEXTWORD (p1, val, PRINT_INSN_ARG_MEMORY_ERROR);
       else if (place == 'l')
-	NEXTLONG (p1, val, -3);
+	NEXTLONG (p1, val, PRINT_INSN_ARG_MEMORY_ERROR);
       else
-	return -2;
+	return PRINT_INSN_ARG_INVALID_OP_TABLE;
 
       (*info->fprintf_func) (info->stream, "#%d", val);
       break;
@@ -896,26 +910,26 @@ print_insn_arg (const char *d,
       else if (place == 'B')
 	disp = COERCE_SIGNED_CHAR (buffer[1]);
       else if (place == 'w' || place == 'W')
-	NEXTWORD (p, disp, -3);
+	NEXTWORD (p, disp, PRINT_INSN_ARG_MEMORY_ERROR);
       else if (place == 'l' || place == 'L' || place == 'C')
-	NEXTLONG (p, disp, -3);
+	NEXTLONG (p, disp, PRINT_INSN_ARG_MEMORY_ERROR);
       else if (place == 'g')
 	{
 	  NEXTBYTE (buffer, disp);
 	  if (disp == 0)
-	    NEXTWORD (p, disp, -3);
+	    NEXTWORD (p, disp, PRINT_INSN_ARG_MEMORY_ERROR);
 	  else if (disp == -1)
-	    NEXTLONG (p, disp, -3);
+	    NEXTLONG (p, disp, PRINT_INSN_ARG_MEMORY_ERROR);
 	}
       else if (place == 'c')
 	{
 	  if (buffer[1] & 0x40)		/* If bit six is one, long offset.  */
-	    NEXTLONG (p, disp, -3);
+	    NEXTLONG (p, disp, PRINT_INSN_ARG_MEMORY_ERROR);
 	  else
-	    NEXTWORD (p, disp, -3);
+	    NEXTWORD (p, disp, PRINT_INSN_ARG_MEMORY_ERROR);
 	}
       else
-	return -2;
+	return PRINT_INSN_ARG_INVALID_OP_TABLE;
 
       (*info->print_address_func) (addr + disp, info);
       break;
@@ -924,7 +938,7 @@ print_insn_arg (const char *d,
       {
 	int val1;
 
-	NEXTWORD (p, val, -3);
+	NEXTWORD (p, val, PRINT_INSN_ARG_MEMORY_ERROR);
 	FETCH_ARG (3, val1);
 	(*info->fprintf_func) (info->stream, "%s@(%d)", reg_names[val1 + 8], val);
 	break;
@@ -952,14 +966,14 @@ print_insn_arg (const char *d,
       else if (val == 3)
 	(*info->fprintf_func) (info->stream, ">>");
       else
-	return -1;
+	return PRINT_INSN_ARG_INVALID_OPERAND;
       break;
 
     case 'I':
       /* Get coprocessor ID... */
       val = fetch_arg (buffer, 'd', 3, info);
       if (val < 0)
-	return -3;
+	return PRINT_INSN_ARG_MEMORY_ERROR;
       if (val != 1)				/* Unusual coprocessor ID?  */
 	(*info->fprintf_func) (info->stream, "(cpid=%d) ", val);
       break;
@@ -992,19 +1006,19 @@ print_insn_arg (const char *d,
 	{
 	  val = fetch_arg (buffer, 'x', 6, info);
 	  if (val < 0)
-	    return -3;
+	    return PRINT_INSN_ARG_MEMORY_ERROR;
 	  val = ((val & 7) << 3) + ((val >> 3) & 7);
 	}
       else
 	{
 	  val = fetch_arg (buffer, 's', 6, info);
 	  if (val < 0)
-	    return -3;
+	    return PRINT_INSN_ARG_MEMORY_ERROR;
 	}
 
       /* If the <ea> is invalid for *d, then reject this match.  */
       if (!m68k_valid_ea (*d, val))
-	return -1;
+	return PRINT_INSN_ARG_INVALID_OPERAND;
 
       /* Get register number assuming address register.  */
       regno = (val & 7) + 8;
@@ -1032,21 +1046,21 @@ print_insn_arg (const char *d,
 	  break;
 
 	case 5:
-	  NEXTWORD (p, val, -3);
+	  NEXTWORD (p, val, PRINT_INSN_ARG_MEMORY_ERROR);
 	  (*info->fprintf_func) (info->stream, "%s@(%d)", regname, val);
 	  break;
 
 	case 6:
 	  p = print_indexed (regno, p, addr, info);
 	  if (p == NULL)
-	    return -3;
+	    return PRINT_INSN_ARG_MEMORY_ERROR;
 	  break;
 
 	case 7:
 	  switch (val & 7)
 	    {
 	    case 0:
-	      NEXTWORD (p, val, -3);
+	      NEXTWORD (p, val, PRINT_INSN_ARG_MEMORY_ERROR);
 	      (*info->print_address_func) (val, info);
 	      break;
 
@@ -1056,7 +1070,7 @@ print_insn_arg (const char *d,
 	      break;
 
 	    case 2:
-	      NEXTWORD (p, val, -3);
+	      NEXTWORD (p, val, PRINT_INSN_ARG_MEMORY_ERROR);
 	      (*info->fprintf_func) (info->stream, "%%pc@(");
 	      (*info->print_address_func) (addr + val, info);
 	      (*info->fprintf_func) (info->stream, ")");
@@ -1065,7 +1079,7 @@ print_insn_arg (const char *d,
 	    case 3:
 	      p = print_indexed (-1, p, addr, info);
 	      if (p == NULL)
-		return -3;
+		return PRINT_INSN_ARG_MEMORY_ERROR;
 	      break;
 
 	    case 4:
@@ -1078,12 +1092,12 @@ print_insn_arg (const char *d,
 		  break;
 
 		case 'w':
-		  NEXTWORD (p, val, -3);
+		  NEXTWORD (p, val, PRINT_INSN_ARG_MEMORY_ERROR);
 		  flt_p = 0;
 		  break;
 
 		case 'l':
-		  NEXTLONG (p, val, -3);
+		  NEXTLONG (p, val, PRINT_INSN_ARG_MEMORY_ERROR);
 		  flt_p = 0;
 		  break;
 
@@ -1104,7 +1118,7 @@ print_insn_arg (const char *d,
 		  break;
 
 		default:
-		  return -1;
+		  return PRINT_INSN_ARG_INVALID_OPERAND;
 	      }
 	      if (flt_p)	/* Print a float? */
 		(*info->fprintf_func) (info->stream, "#0e%g", flval);
@@ -1113,7 +1127,7 @@ print_insn_arg (const char *d,
 	      break;
 
 	    default:
-	      return -1;
+	      return PRINT_INSN_ARG_INVALID_OPERAND;
 	    }
 	}
 
@@ -1134,7 +1148,7 @@ print_insn_arg (const char *d,
 	  {
 	    char doneany;
 	    p1 = buffer + 2;
-	    NEXTWORD (p1, val, -3);
+	    NEXTWORD (p1, val, PRINT_INSN_ARG_MEMORY_ERROR);
 	    /* Move the pointer ahead if this point is farther ahead
 	       than the last.  */
 	    p = p1 > p ? p1 : p;
@@ -1215,7 +1229,7 @@ print_insn_arg (const char *d,
 	    (*info->fprintf_func) (info->stream, "%s", fpcr_names[val]);
 	  }
 	else
-	  return -2;
+	  return PRINT_INSN_ARG_INVALID_OP_TABLE;
       break;
 
     case 'X':
@@ -1310,7 +1324,7 @@ print_insn_arg (const char *d,
       break;
 
     default:
-      return -2;
+      return PRINT_INSN_ARG_INVALID_OP_TABLE;
     }
 
   return p - p0;
@@ -1420,7 +1434,8 @@ match_insn_m68k (bfd_vma memaddr,
 
       if (eaten >= 0)
 	p += eaten;
-      else if (eaten == -1 || eaten == -3)
+      else if (eaten == PRINT_INSN_ARG_INVALID_OPERAND
+	       || eaten == PRINT_INSN_ARG_MEMORY_ERROR)
 	{
 	  info->fprintf_func = save_printer;
 	  info->print_address_func = save_print_address;
-- 
1.9.1


  parent reply	other threads:[~2017-01-10 12:26 UTC|newest]

Thread overview: 79+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-01-10 12:26 [PATCH 0/8] Handle memory error on disassemble Yao Qi
2017-01-10 12:26 ` [PATCH 6/8] Return -1 on memory error in print_insn_m68k Yao Qi
2017-01-11 22:15   ` Alan Modra
2017-01-12 11:50     ` Yao Qi
2017-01-12 14:38       ` Alan Modra
2017-01-12 14:52         ` Yao Qi
2017-01-13  1:54           ` Alan Modra
2017-01-13 12:29             ` Yao Qi
2017-01-10 12:26 ` Yao Qi [this message]
2017-01-11 22:14   ` [PATCH 5/8] Remove magic numbers in m68k-dis.c:print_insn_arg Alan Modra
2017-01-13 12:23     ` Yao Qi
2017-01-10 12:26 ` [PATCH 7/8] Disassembly unit test: memory error Yao Qi
2017-01-10 12:26 ` [PATCH 4/8] Return -1 on memory error in print_insn_msp430 Yao Qi
2017-01-11 21:54   ` Alan Modra
2017-01-12  9:43     ` Yao Qi
2017-01-10 12:26 ` [PATCH 3/8] Disassembly unit test: disassemble one instruction Yao Qi
2017-01-11 21:15   ` Simon Marchi
2017-01-12 13:06   ` Pedro Alves
2017-01-12 17:03     ` Yao Qi
2017-01-12 17:43       ` Pedro Alves
2017-01-12 21:04         ` Yao Qi
2017-01-12 14:35   ` Pedro Alves
2017-01-12 15:15   ` Pedro Alves
2017-01-12 15:35     ` Yao Qi
2017-01-12 15:44       ` Pedro Alves
2017-01-12 16:06     ` Pedro Alves
2017-01-10 12:27 ` [PATCH 2/8] Call print_insn_mep in mep_gdb_print_insn Yao Qi
2017-01-11 20:50   ` Simon Marchi
2017-01-12 12:21     ` Yao Qi
2017-01-10 12:27 ` [PATCH 8/8] Don't throw exception in dis_asm_memory_error Yao Qi
2017-01-12 16:40   ` Pedro Alves
2017-01-12 21:09     ` Yao Qi
2017-01-10 12:27 ` [PATCH 1/8] Refactor disassembly code Yao Qi
2017-01-11 20:43   ` Simon Marchi
2017-01-12 12:19     ` Yao Qi
2017-01-12 12:36       ` Pedro Alves
2017-01-12 15:29         ` Simon Marchi
2017-01-16 10:03 ` [PATCH 0/6 v2] Handle memory error on disassemble Yao Qi
2017-01-16 10:03   ` [PATCH 1/6] New function null_stream Yao Qi
2017-01-17 13:49     ` Luis Machado
2017-01-18 14:45       ` Yao Qi
2017-01-18 14:53         ` Luis Machado
2017-01-18 14:57           ` Simon Marchi
2017-01-18 15:02             ` Luis Machado
2017-01-18 15:18               ` Simon Marchi
2017-01-18 15:29                 ` Luis Machado
2017-01-18 15:54                   ` Simon Marchi
2017-01-18 16:36                     ` Luis Machado
2017-01-16 10:03   ` [PATCH 5/6] Disassembly unit test: memory error Yao Qi
2017-01-17 14:38     ` Luis Machado
2017-01-24 15:33       ` Yao Qi
2017-01-20  0:08     ` Pedro Alves
2017-01-16 10:03   ` [PATCH 4/6] Disassembly unit test: disassemble one instruction Yao Qi
2017-01-20  0:04     ` Pedro Alves
2017-01-24 15:23       ` Yao Qi
2017-02-02 16:46         ` Pedro Alves
2017-02-02 22:12           ` Yao Qi
2017-02-02 23:39             ` [pushed] Fix "maintenance selftest" printing stray instructions (Re: [PATCH 4/6] Disassembly unit test: disassemble one instruction) Pedro Alves
2017-01-16 10:03   ` [PATCH 6/6] Don't throw exception in dis_asm_memory_error Yao Qi
2017-01-17 14:42     ` Luis Machado
2017-01-18 14:54       ` Yao Qi
2017-01-18 14:58         ` Luis Machado
2017-01-16 10:03   ` [PATCH 3/6] Call print_insn_mep in mep_gdb_print_insn Yao Qi
2017-01-17 14:19     ` Luis Machado
2017-01-24 10:08       ` Yao Qi
2017-01-24 13:41         ` Luis Machado
2017-01-16 10:03   ` [PATCH 2/6] Refactor disassembly code Yao Qi
2017-01-17 14:14     ` Luis Machado
2017-01-18 16:34       ` Yao Qi
2017-01-18 16:53         ` Luis Machado
2017-01-25  8:38   ` [PATCH 0/6 v3] Handle memory error on disassembly Yao Qi
2017-01-25  8:38     ` [PATCH 2/6] Refactor disassembly code Yao Qi
2017-01-25  8:38     ` [PATCH 4/6] Disassembly unit test: disassemble one instruction Yao Qi
2017-01-25  8:38     ` [PATCH 6/6] Don't throw exception in dis_asm_memory_error Yao Qi
2017-01-25  8:38     ` [PATCH 3/6] Call print_insn_mep in mep_gdb_print_insn Yao Qi
2017-01-25  8:38     ` [PATCH 5/6] Disassembly unit test: memory error Yao Qi
2017-01-25  8:38     ` [PATCH 1/6] New function null_stream Yao Qi
2017-01-26 11:34     ` [PATCH 0/6 v3] Handle memory error on disassembly Pedro Alves
2017-01-26 15:00       ` Yao Qi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1484051178-16013-6-git-send-email-yao.qi@linaro.org \
    --to=qiyaoltc@gmail.com \
    --cc=binutils@sourceware.org \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox