From patchwork Tue Jan 10 12:26:15 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yao Qi X-Patchwork-Id: 18835 Received: (qmail 89934 invoked by alias); 10 Jan 2017 12:26:42 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 89792 invoked by uid 89); 10 Jan 2017 12:26:40 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.5 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, RCVD_IN_SORBS_SPAM, SPF_PASS autolearn=no version=3.3.2 spammy=eaten, farther, 13247, 13107 X-Spam-User: qpsmtpd, 2 recipients X-HELO: mail-pf0-f194.google.com Received: from mail-pf0-f194.google.com (HELO mail-pf0-f194.google.com) (209.85.192.194) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 10 Jan 2017 12:26:36 +0000 Received: by mail-pf0-f194.google.com with SMTP id b22so12446103pfd.3; Tue, 10 Jan 2017 04:26:36 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=ajDqIkTQIJiKaeSYaoB050fA49wUGawqKVcjm/INGOM=; b=cXSMrlFZuwOVldoLaN5dEWpXGz+OdZaSpF15UYlFlmTc9N1fylhqNg0ZSkXQIsguL4 /0J8Qfj1Ij1pBUODH4EgM1cm4fHEFquw3Pka5Z/tYfV2YnKaW+rxBfO8vVuy1jI/XXD/ R+yqDF0SlUyL5uibklzMwLTz1NkL6yEkpMPnN+e0Z8sjYvoCl20V5e+rHoLPuvVUdHzY gfsi/4ukvzrIFK8X9fKNw0C+MVu0yQ/mkXSLEnSQ/7msDhyJg8QdBGlnuzLAD8/XB7Xw nhLq8xvix248NL9PSXk67GIJw5UzcwTLU/Jqfy27hx7G6fL+LRpb+1u0ag096vXlZpqi zy7g== X-Gm-Message-State: AIkVDXLmPZ7GWmLFgBo4+shTsNQcc1HTZh1QFArZEW1su305Mc3vWuvCjD/H6WuDjWQLxQ== X-Received: by 10.99.112.13 with SMTP id l13mr3707948pgc.7.1484051194973; Tue, 10 Jan 2017 04:26:34 -0800 (PST) Received: from E107787-LIN.cambridge.arm.com (gcc1-power7.osuosl.org. [140.211.15.137]) by smtp.gmail.com with ESMTPSA id r26sm5450661pgd.42.2017.01.10.04.26.33 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 10 Jan 2017 04:26:34 -0800 (PST) From: Yao Qi X-Google-Original-From: Yao Qi 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:15 +0000 Message-Id: <1484051178-16013-6-git-send-email-yao.qi@linaro.org> In-Reply-To: <1484051178-16013-1-git-send-email-yao.qi@linaro.org> References: <1484051178-16013-1-git-send-email-yao.qi@linaro.org> X-IsSubscribed: yes 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 * 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 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;