Remove magic numbers in m68k-dis.c:print_insn_arg
authorYao Qi <yao.qi@linaro.org>
Fri, 13 Jan 2017 12:21:22 +0000 (12:21 +0000)
committerYao Qi <yao.qi@linaro.org>
Fri, 13 Jan 2017 12:21:22 +0000 (12:21 +0000)
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-13  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_ARG): Likewise.
(FETCH_DATA): Update comments.
(print_insn_arg): Update comments. Replace magic numbers with
enum.
(match_insn_m68k): Likewise.

opcodes/ChangeLog
opcodes/m68k-dis.c

index 000948eed9cbbc3f8529d01d1e2db9713f769990..0adbdf1cb15cb04ff7e9cdf8ae46f8fcddf0103b 100644 (file)
@@ -1,3 +1,19 @@
+2017-01-13  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_ARG): Likewise.
+       (FETCH_DATA): Update comments.
+       (print_insn_arg): Update comments. Replace magic numbers with
+       enum.
+       (match_insn_m68k): Likewise.
+
 2017-01-12  Igor Tsimbalist  <igor.v.tsimbalist@intel.com>
 
        * i386-dis.c (enum): Add PREFIX_EVEX_0F3855, EVEX_W_0F3855_P_2.
index de88f23b69613064c3672b243fecc3dcc629e028..a14db9a1f6a523d4bfef4802c665be7740afbfb5 100644 (file)
@@ -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)))
@@ -617,14 +632,13 @@ print_indexed (int basereg,
     {                                                  \
       val = fetch_arg (buffer, place, size, info);     \
       if (val < 0)                                     \
-       return -3;                                      \
+       return PRINT_INSN_ARG_MEMORY_ERROR;             \
     }                                                  \
   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;