Skip to content

Commit f622ea9

Browse files
author
Yao Qi
committed
Remove magic numbers in m68k-dis.c:print_insn_arg
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.
1 parent 404c843 commit f622ea9

2 files changed

Lines changed: 72 additions & 41 deletions

File tree

‎opcodes/ChangeLog‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
2017-01-13 Yao Qi <yao.qi@linaro.org>
2+
3+
* m68k-dis.c (enum print_insn_arg_error): New.
4+
(NEXTBYTE): Replace -3 with
5+
PRINT_INSN_ARG_MEMORY_ERROR.
6+
(NEXTULONG): Likewise.
7+
(NEXTSINGLE): Likewise.
8+
(NEXTDOUBLE): Likewise.
9+
(NEXTDOUBLE): Likewise.
10+
(NEXTPACKED): Likewise.
11+
(FETCH_ARG): Likewise.
12+
(FETCH_DATA): Update comments.
13+
(print_insn_arg): Update comments. Replace magic numbers with
14+
enum.
15+
(match_insn_m68k): Likewise.
16+
117
2017-01-12 Igor Tsimbalist <igor.v.tsimbalist@intel.com>
218

319
* i386-dis.c (enum): Add PREFIX_EVEX_0F3855, EVEX_W_0F3855_P_2.

‎opcodes/m68k-dis.c‎

Lines changed: 56 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,27 @@ static char *const reg_half_names[] =
5757
#define COERCE_SIGNED_CHAR(ch) ((int) (((ch) ^ 0x80) & 0xFF) - 128)
5858
#endif
5959

60+
/* Error code of print_insn_arg's return value. */
61+
62+
enum print_insn_arg_error
63+
{
64+
/* An invalid operand is found. */
65+
PRINT_INSN_ARG_INVALID_OPERAND = -1,
66+
67+
/* An opcode table error. */
68+
PRINT_INSN_ARG_INVALID_OP_TABLE = -2,
69+
70+
/* A memory error. */
71+
PRINT_INSN_ARG_MEMORY_ERROR = -3,
72+
};
73+
6074
/* Get a 1 byte signed integer. */
6175
#define NEXTBYTE(p, val) \
6276
do \
6377
{ \
6478
p += 2; \
6579
if (!FETCH_DATA (info, p)) \
66-
return -3; \
80+
return PRINT_INSN_ARG_MEMORY_ERROR; \
6781
val = COERCE_SIGNED_CHAR (p[-1]); \
6882
} \
6983
while (0)
@@ -100,7 +114,7 @@ static char *const reg_half_names[] =
100114
{ \
101115
p += 4; \
102116
if (!FETCH_DATA (info, p)) \
103-
return -3; \
117+
return PRINT_INSN_ARG_MEMORY_ERROR; \
104118
val = (unsigned int) ((((((p[-4] << 8) + p[-3]) << 8) + p[-2]) << 8) + p[-1]); \
105119
} \
106120
while (0)
@@ -111,7 +125,7 @@ static char *const reg_half_names[] =
111125
{ \
112126
p += 4; \
113127
if (!FETCH_DATA (info, p)) \
114-
return -3; \
128+
return PRINT_INSN_ARG_MEMORY_ERROR; \
115129
floatformat_to_double (& floatformat_ieee_single_big, \
116130
(char *) p - 4, & val); \
117131
} \
@@ -123,7 +137,7 @@ static char *const reg_half_names[] =
123137
{ \
124138
p += 8; \
125139
if (!FETCH_DATA (info, p)) \
126-
return -3; \
140+
return PRINT_INSN_ARG_MEMORY_ERROR; \
127141
floatformat_to_double (& floatformat_ieee_double_big, \
128142
(char *) p - 8, & val); \
129143
} \
@@ -135,7 +149,7 @@ static char *const reg_half_names[] =
135149
{ \
136150
p += 12; \
137151
if (!FETCH_DATA (info, p)) \
138-
return -3; \
152+
return PRINT_INSN_ARG_MEMORY_ERROR; \
139153
floatformat_to_double (& floatformat_m68881_ext, \
140154
(char *) p - 12, & val); \
141155
} \
@@ -150,7 +164,7 @@ static char *const reg_half_names[] =
150164
{ \
151165
p += 12; \
152166
if (!FETCH_DATA (info, p)) \
153-
return -3; \
167+
return PRINT_INSN_ARG_MEMORY_ERROR; \
154168
val = 0.0; \
155169
} \
156170
while (0)
@@ -168,7 +182,8 @@ struct private
168182
};
169183

170184
/* Make sure that bytes from INFO->PRIVATE_DATA->BUFFER (inclusive)
171-
to ADDR (exclusive) are valid. Returns 1 for success, 0 on error. */
185+
to ADDR (exclusive) are valid. Returns 1 for success, 0 on memory
186+
error. */
172187
#define FETCH_DATA(info, addr) \
173188
((addr) <= ((struct private *) (info->private_data))->max_fetched \
174189
? 1 : fetch_data ((info), (addr)))
@@ -617,14 +632,13 @@ print_indexed (int basereg,
617632
{ \
618633
val = fetch_arg (buffer, place, size, info); \
619634
if (val < 0) \
620-
return -3; \
635+
return PRINT_INSN_ARG_MEMORY_ERROR; \
621636
} \
622637
while (0)
623638

624639
/* Returns number of bytes "eaten" by the operand, or
625-
return -1 if an invalid operand was found, or -2 if
626-
an opcode tabe error was found or -3 to simply abort.
627-
ADDR is the pc for this arg to be relative to. */
640+
return enum print_insn_arg_error. ADDR is the pc for this arg to be
641+
relative to. */
628642

629643
static int
630644
print_insn_arg (const char *d,
@@ -864,7 +878,7 @@ print_insn_arg (const char *d,
864878
(*info->fprintf_func) (info->stream, "{#%d}", val);
865879
}
866880
else
867-
return -1;
881+
return PRINT_INSN_ARG_INVALID_OPERAND;
868882
break;
869883

870884
case '#':
@@ -881,11 +895,11 @@ print_insn_arg (const char *d,
881895
else if (place == 'b')
882896
NEXTBYTE (p1, val);
883897
else if (place == 'w' || place == 'W')
884-
NEXTWORD (p1, val, -3);
898+
NEXTWORD (p1, val, PRINT_INSN_ARG_MEMORY_ERROR);
885899
else if (place == 'l')
886-
NEXTLONG (p1, val, -3);
900+
NEXTLONG (p1, val, PRINT_INSN_ARG_MEMORY_ERROR);
887901
else
888-
return -2;
902+
return PRINT_INSN_ARG_INVALID_OP_TABLE;
889903

890904
(*info->fprintf_func) (info->stream, "#%d", val);
891905
break;
@@ -896,26 +910,26 @@ print_insn_arg (const char *d,
896910
else if (place == 'B')
897911
disp = COERCE_SIGNED_CHAR (buffer[1]);
898912
else if (place == 'w' || place == 'W')
899-
NEXTWORD (p, disp, -3);
913+
NEXTWORD (p, disp, PRINT_INSN_ARG_MEMORY_ERROR);
900914
else if (place == 'l' || place == 'L' || place == 'C')
901-
NEXTLONG (p, disp, -3);
915+
NEXTLONG (p, disp, PRINT_INSN_ARG_MEMORY_ERROR);
902916
else if (place == 'g')
903917
{
904918
NEXTBYTE (buffer, disp);
905919
if (disp == 0)
906-
NEXTWORD (p, disp, -3);
920+
NEXTWORD (p, disp, PRINT_INSN_ARG_MEMORY_ERROR);
907921
else if (disp == -1)
908-
NEXTLONG (p, disp, -3);
922+
NEXTLONG (p, disp, PRINT_INSN_ARG_MEMORY_ERROR);
909923
}
910924
else if (place == 'c')
911925
{
912926
if (buffer[1] & 0x40) /* If bit six is one, long offset. */
913-
NEXTLONG (p, disp, -3);
927+
NEXTLONG (p, disp, PRINT_INSN_ARG_MEMORY_ERROR);
914928
else
915-
NEXTWORD (p, disp, -3);
929+
NEXTWORD (p, disp, PRINT_INSN_ARG_MEMORY_ERROR);
916930
}
917931
else
918-
return -2;
932+
return PRINT_INSN_ARG_INVALID_OP_TABLE;
919933

920934
(*info->print_address_func) (addr + disp, info);
921935
break;
@@ -924,7 +938,7 @@ print_insn_arg (const char *d,
924938
{
925939
int val1;
926940

927-
NEXTWORD (p, val, -3);
941+
NEXTWORD (p, val, PRINT_INSN_ARG_MEMORY_ERROR);
928942
FETCH_ARG (3, val1);
929943
(*info->fprintf_func) (info->stream, "%s@(%d)", reg_names[val1 + 8], val);
930944
break;
@@ -952,14 +966,14 @@ print_insn_arg (const char *d,
952966
else if (val == 3)
953967
(*info->fprintf_func) (info->stream, ">>");
954968
else
955-
return -1;
969+
return PRINT_INSN_ARG_INVALID_OPERAND;
956970
break;
957971

958972
case 'I':
959973
/* Get coprocessor ID... */
960974
val = fetch_arg (buffer, 'd', 3, info);
961975
if (val < 0)
962-
return -3;
976+
return PRINT_INSN_ARG_MEMORY_ERROR;
963977
if (val != 1) /* Unusual coprocessor ID? */
964978
(*info->fprintf_func) (info->stream, "(cpid=%d) ", val);
965979
break;
@@ -992,19 +1006,19 @@ print_insn_arg (const char *d,
9921006
{
9931007
val = fetch_arg (buffer, 'x', 6, info);
9941008
if (val < 0)
995-
return -3;
1009+
return PRINT_INSN_ARG_MEMORY_ERROR;
9961010
val = ((val & 7) << 3) + ((val >> 3) & 7);
9971011
}
9981012
else
9991013
{
10001014
val = fetch_arg (buffer, 's', 6, info);
10011015
if (val < 0)
1002-
return -3;
1016+
return PRINT_INSN_ARG_MEMORY_ERROR;
10031017
}
10041018

10051019
/* If the <ea> is invalid for *d, then reject this match. */
10061020
if (!m68k_valid_ea (*d, val))
1007-
return -1;
1021+
return PRINT_INSN_ARG_INVALID_OPERAND;
10081022

10091023
/* Get register number assuming address register. */
10101024
regno = (val & 7) + 8;
@@ -1032,21 +1046,21 @@ print_insn_arg (const char *d,
10321046
break;
10331047

10341048
case 5:
1035-
NEXTWORD (p, val, -3);
1049+
NEXTWORD (p, val, PRINT_INSN_ARG_MEMORY_ERROR);
10361050
(*info->fprintf_func) (info->stream, "%s@(%d)", regname, val);
10371051
break;
10381052

10391053
case 6:
10401054
p = print_indexed (regno, p, addr, info);
10411055
if (p == NULL)
1042-
return -3;
1056+
return PRINT_INSN_ARG_MEMORY_ERROR;
10431057
break;
10441058

10451059
case 7:
10461060
switch (val & 7)
10471061
{
10481062
case 0:
1049-
NEXTWORD (p, val, -3);
1063+
NEXTWORD (p, val, PRINT_INSN_ARG_MEMORY_ERROR);
10501064
(*info->print_address_func) (val, info);
10511065
break;
10521066

@@ -1056,7 +1070,7 @@ print_insn_arg (const char *d,
10561070
break;
10571071

10581072
case 2:
1059-
NEXTWORD (p, val, -3);
1073+
NEXTWORD (p, val, PRINT_INSN_ARG_MEMORY_ERROR);
10601074
(*info->fprintf_func) (info->stream, "%%pc@(");
10611075
(*info->print_address_func) (addr + val, info);
10621076
(*info->fprintf_func) (info->stream, ")");
@@ -1065,7 +1079,7 @@ print_insn_arg (const char *d,
10651079
case 3:
10661080
p = print_indexed (-1, p, addr, info);
10671081
if (p == NULL)
1068-
return -3;
1082+
return PRINT_INSN_ARG_MEMORY_ERROR;
10691083
break;
10701084

10711085
case 4:
@@ -1078,12 +1092,12 @@ print_insn_arg (const char *d,
10781092
break;
10791093

10801094
case 'w':
1081-
NEXTWORD (p, val, -3);
1095+
NEXTWORD (p, val, PRINT_INSN_ARG_MEMORY_ERROR);
10821096
flt_p = 0;
10831097
break;
10841098

10851099
case 'l':
1086-
NEXTLONG (p, val, -3);
1100+
NEXTLONG (p, val, PRINT_INSN_ARG_MEMORY_ERROR);
10871101
flt_p = 0;
10881102
break;
10891103

@@ -1104,7 +1118,7 @@ print_insn_arg (const char *d,
11041118
break;
11051119

11061120
default:
1107-
return -1;
1121+
return PRINT_INSN_ARG_INVALID_OPERAND;
11081122
}
11091123
if (flt_p) /* Print a float? */
11101124
(*info->fprintf_func) (info->stream, "#0e%g", flval);
@@ -1113,7 +1127,7 @@ print_insn_arg (const char *d,
11131127
break;
11141128

11151129
default:
1116-
return -1;
1130+
return PRINT_INSN_ARG_INVALID_OPERAND;
11171131
}
11181132
}
11191133

@@ -1134,7 +1148,7 @@ print_insn_arg (const char *d,
11341148
{
11351149
char doneany;
11361150
p1 = buffer + 2;
1137-
NEXTWORD (p1, val, -3);
1151+
NEXTWORD (p1, val, PRINT_INSN_ARG_MEMORY_ERROR);
11381152
/* Move the pointer ahead if this point is farther ahead
11391153
than the last. */
11401154
p = p1 > p ? p1 : p;
@@ -1215,7 +1229,7 @@ print_insn_arg (const char *d,
12151229
(*info->fprintf_func) (info->stream, "%s", fpcr_names[val]);
12161230
}
12171231
else
1218-
return -2;
1232+
return PRINT_INSN_ARG_INVALID_OP_TABLE;
12191233
break;
12201234

12211235
case 'X':
@@ -1310,7 +1324,7 @@ print_insn_arg (const char *d,
13101324
break;
13111325

13121326
default:
1313-
return -2;
1327+
return PRINT_INSN_ARG_INVALID_OP_TABLE;
13141328
}
13151329

13161330
return p - p0;
@@ -1420,7 +1434,8 @@ match_insn_m68k (bfd_vma memaddr,
14201434

14211435
if (eaten >= 0)
14221436
p += eaten;
1423-
else if (eaten == -1 || eaten == -3)
1437+
else if (eaten == PRINT_INSN_ARG_INVALID_OPERAND
1438+
|| eaten == PRINT_INSN_ARG_MEMORY_ERROR)
14241439
{
14251440
info->fprintf_func = save_printer;
14261441
info->print_address_func = save_print_address;

0 commit comments

Comments
 (0)