From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id AF9713856968 for ; Tue, 12 Jul 2022 13:36:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org AF9713856968 Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-488-BXDhY9mXOLmafq3q2WBzwQ-1; Tue, 12 Jul 2022 09:36:14 -0400 X-MC-Unique: BXDhY9mXOLmafq3q2WBzwQ-1 Received: by mail-wm1-f70.google.com with SMTP id g22-20020a7bc4d6000000b003a2e0951432so3760048wmk.9 for ; Tue, 12 Jul 2022 06:36:14 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=9B4ueRwBnj7N/9knZWP06GedSqs/g6+NMhqzsqmCsMs=; b=rROJoO+aWIQMG8kHEBcqcqBrkzrED3xOwPhEaFa3MoSGymt98LLTzfu+OTksLxbkp9 LZrjyDqkWhxuWryk2ahStCIUbcIr/48ctfoxcCG/Ntk799o4pfLgPd3+GnX2n9Ap1Qhr AeUISqIZjcJq7133j48cnyN63HcuILD+8zgvFBvl19b1FIvHVhrnqTCSGtk8azYowLrD UefKLioRqEc2gDaIllGjYcFxG/04rYTc+DP/SVtYI1iLQkAqjptLfX4JzoF9WijStoUn kw7uTtlOqNUgM5Og/REll/EtmKttigyNICVsI0Zkz28lWR1EXNo3fDlCMQAQl72u65ie 9tdQ== X-Gm-Message-State: AJIora8AuxLu6cAlRENxd/Aknzo/rMShQQohvyqeIscKbccIZX4zzYcB PJuH1XJpaixXaY7EoeRRYmLBrpp1+i49Xq4E/4SrZ/8H7BFHa8YoID/bWCvEQFvSRVt9VjpOv7n jl5eF5SIA5gUkhX3jIeadr/MUNoM8blzZ8aA5mGF5ki36/wjHGgZuJZk1RSOSW1uGlczJBg== X-Received: by 2002:a5d:64ac:0:b0:21d:7832:ecf9 with SMTP id m12-20020a5d64ac000000b0021d7832ecf9mr22260175wrp.86.1657632972308; Tue, 12 Jul 2022 06:36:12 -0700 (PDT) X-Google-Smtp-Source: AGRyM1uQawZqT/9YfJ4C+xaEZNkLxxIl0rf/yVmvWTawtLTD1Izrzcib2o/2oEa2LVyUhHS4CaVKcw== X-Received: by 2002:a5d:64ac:0:b0:21d:7832:ecf9 with SMTP id m12-20020a5d64ac000000b0021d7832ecf9mr22260142wrp.86.1657632972010; Tue, 12 Jul 2022 06:36:12 -0700 (PDT) Received: from localhost (15.72.115.87.dyn.plus.net. [87.115.72.15]) by smtp.gmail.com with ESMTPSA id c8-20020a05600c0a4800b003a03e63e428sm18520669wmq.36.2022.07.12.06.36.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Jul 2022 06:36:11 -0700 (PDT) From: Andrew Burgess To: binutils@sourceware.org Cc: Andrew Burgess Subject: [PATCH] libopcodes/ppc: add support for disassembler styling Date: Tue, 12 Jul 2022 14:36:09 +0100 Message-Id: <20220712133609.3348690-1-aburgess@redhat.com> X-Mailer: git-send-email 2.25.4 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-10.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: binutils@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 12 Jul 2022 13:36:17 -0000 This commit adds disassembler styling to the libopcodes ppc disassembler. This conversion was pretty straight forward, I just converted the fprintf_func calls to fprintf_styled_func calls and added an appropriate style. For testing the new styling I just assembled then disassembled the source files in gas/testsuite/gas/ppc and manually checked that the styling looked reasonable. I think the only slightly weird case was how things like '4*cr1+eq' are styled. As best I can tell, this construct, used for example in this instruction: crand 4*cr1+lt,4*cr1+gt,4*cr1+eq is used to access a field of a control register (NOTE: I know very little about the PPC ISA, so please correct me if I'm wrong). As such, I have styled the entire construct as a register. In some cases constructs similar to the above can be simplified to just 'eq', as this is still referencing a register field, this is still styled as a register. If the user does not request styled output from objdump, then there should be no change in the disassembler output after this commit. --- opcodes/disassemble.c | 1 + opcodes/ppc-dis.c | 107 ++++++++++++++++++++++++++++++------------ 2 files changed, 78 insertions(+), 30 deletions(-) diff --git a/opcodes/disassemble.c b/opcodes/disassemble.c index bd37f042b31..9c991b8121e 100644 --- a/opcodes/disassemble.c +++ b/opcodes/disassemble.c @@ -705,6 +705,7 @@ disassemble_init_for_target (struct disassemble_info * info) #endif #if defined (ARCH_powerpc) || defined (ARCH_rs6000) disassemble_init_powerpc (info); + info->created_styled_output = true; break; #endif #ifdef ARCH_riscv diff --git a/opcodes/ppc-dis.c b/opcodes/ppc-dis.c index 45e8faeef5e..1447afaecc3 100644 --- a/opcodes/ppc-dis.c +++ b/opcodes/ppc-dis.c @@ -829,12 +829,24 @@ print_got_plt (struct sec_buf *sb, uint64_t vma, struct disassemble_info *info) sym = (*info->symbol_at_address_func) (ent, info); } } + (*info->fprintf_styled_func) (info->stream, dis_style_text, " ["); if (sym != NULL) - (*info->fprintf_func) (info->stream, " [%s@%s]", - bfd_asymbol_name (sym), sb->name + 1); + { + (*info->fprintf_styled_func) (info->stream, dis_style_symbol, + "%s", bfd_asymbol_name (sym)); + (*info->fprintf_styled_func) (info->stream, dis_style_text, "@"); + (*info->fprintf_styled_func) (info->stream, dis_style_symbol, + "%s", sb->name + 1); + } else - (*info->fprintf_func) (info->stream, " [%" PRIx64 "@%s]", - ent, sb->name + 1); + { + (*info->fprintf_styled_func) (info->stream, dis_style_address, + "%" PRIx64, ent); + (*info->fprintf_styled_func) (info->stream, dis_style_text, "@"); + (*info->fprintf_styled_func) (info->stream, dis_style_symbol, + "%s", sb->name + 1); + } + (*info->fprintf_styled_func) (info->stream, dis_style_text, "]"); return true; } } @@ -943,8 +955,9 @@ print_insn_powerpc (bfd_vma memaddr, uint64_t d34; int blanks; - (*info->fprintf_func) (info->stream, "%s", opcode->name); - /* gdb fprintf_func doesn't return count printed. */ + (*info->fprintf_styled_func) (info->stream, dis_style_mnemonic, + "%s", opcode->name); + /* gdb fprintf_styled_func doesn't return count printed. */ blanks = 8 - strlen (opcode->name); if (blanks <= 0) blanks = 1; @@ -976,39 +989,49 @@ print_insn_powerpc (bfd_vma memaddr, value = operand_value_powerpc (operand, insn, dialect); if (op_separator == need_comma) - (*info->fprintf_func) (info->stream, ","); + (*info->fprintf_styled_func) (info->stream, dis_style_text, ","); else if (op_separator == need_paren) - (*info->fprintf_func) (info->stream, "("); + (*info->fprintf_styled_func) (info->stream, dis_style_text, "("); else - (*info->fprintf_func) (info->stream, "%*s", op_separator, " "); + (*info->fprintf_styled_func) (info->stream, dis_style_text, "%*s", + op_separator, " "); /* Print the operand as directed by the flags. */ if ((operand->flags & PPC_OPERAND_GPR) != 0 || ((operand->flags & PPC_OPERAND_GPR_0) != 0 && value != 0)) - (*info->fprintf_func) (info->stream, "r%" PRId64, value); + (*info->fprintf_styled_func) (info->stream, dis_style_register, + "r%" PRId64, value); else if ((operand->flags & PPC_OPERAND_FPR) != 0) - (*info->fprintf_func) (info->stream, "f%" PRId64, value); + (*info->fprintf_styled_func) (info->stream, dis_style_register, + "f%" PRId64, value); else if ((operand->flags & PPC_OPERAND_VR) != 0) - (*info->fprintf_func) (info->stream, "v%" PRId64, value); + (*info->fprintf_styled_func) (info->stream, dis_style_register, + "v%" PRId64, value); else if ((operand->flags & PPC_OPERAND_VSR) != 0) - (*info->fprintf_func) (info->stream, "vs%" PRId64, value); + (*info->fprintf_styled_func) (info->stream, dis_style_register, + "vs%" PRId64, value); else if ((operand->flags & PPC_OPERAND_ACC) != 0) - (*info->fprintf_func) (info->stream, "a%" PRId64, value); + (*info->fprintf_styled_func) (info->stream, dis_style_register, + "a%" PRId64, value); else if ((operand->flags & PPC_OPERAND_RELATIVE) != 0) (*info->print_address_func) (memaddr + value, info); else if ((operand->flags & PPC_OPERAND_ABSOLUTE) != 0) (*info->print_address_func) ((bfd_vma) value & 0xffffffff, info); else if ((operand->flags & PPC_OPERAND_FSL) != 0) - (*info->fprintf_func) (info->stream, "fsl%" PRId64, value); + (*info->fprintf_styled_func) (info->stream, dis_style_register, + "fsl%" PRId64, value); else if ((operand->flags & PPC_OPERAND_FCR) != 0) - (*info->fprintf_func) (info->stream, "fcr%" PRId64, value); + (*info->fprintf_styled_func) (info->stream, dis_style_register, + "fcr%" PRId64, value); else if ((operand->flags & PPC_OPERAND_UDI) != 0) - (*info->fprintf_func) (info->stream, "%" PRId64, value); + (*info->fprintf_styled_func) (info->stream, dis_style_text, + "%" PRId64, value); else if ((operand->flags & PPC_OPERAND_CR_REG) != 0 && (operand->flags & PPC_OPERAND_CR_BIT) == 0 && (((dialect & PPC_OPCODE_PPC) != 0) || ((dialect & PPC_OPCODE_VLE) != 0))) - (*info->fprintf_func) (info->stream, "cr%" PRId64, value); + (*info->fprintf_styled_func) (info->stream, dis_style_register, + "cr%" PRId64, value); else if ((operand->flags & PPC_OPERAND_CR_BIT) != 0 && (operand->flags & PPC_OPERAND_CR_REG) == 0 && (((dialect & PPC_OPCODE_PPC) != 0) @@ -1019,13 +1042,27 @@ print_insn_powerpc (bfd_vma memaddr, int cc; cr = value >> 2; - if (cr != 0) - (*info->fprintf_func) (info->stream, "4*cr%d+", cr); cc = value & 3; - (*info->fprintf_func) (info->stream, "%s", cbnames[cc]); + if (cr != 0) + (*info->fprintf_styled_func) (info->stream, dis_style_register, + "4*cr%d+%s", cr, cbnames[cc]); + else + (*info->fprintf_styled_func) (info->stream, dis_style_register, + "%s", cbnames[cc]); } else - (*info->fprintf_func) (info->stream, "%" PRId64, value); + { + /* An immediate, but what style? */ + enum disassembler_style style; + + if ((operand->flags & PPC_OPERAND_PARENS) != 0) + style = dis_style_address_offset; + else + style = dis_style_immediate; + + (*info->fprintf_styled_func) (info->stream, style, + "%" PRId64, value); + } if (operand->shift == 52) is_pcrel = value != 0; @@ -1033,7 +1070,7 @@ print_insn_powerpc (bfd_vma memaddr, d34 = value; if (op_separator == need_paren) - (*info->fprintf_func) (info->stream, ")"); + (*info->fprintf_styled_func) (info->stream, dis_style_text, ")"); op_separator = need_comma; if ((operand->flags & PPC_OPERAND_PARENS) != 0) @@ -1043,11 +1080,13 @@ print_insn_powerpc (bfd_vma memaddr, if (is_pcrel) { d34 += memaddr; - (*info->fprintf_func) (info->stream, "\t# %" PRIx64, d34); + (*info->fprintf_styled_func) (info->stream, + dis_style_comment_start, + "\t# %" PRIx64, d34); asymbol *sym = (*info->symbol_at_address_func) (d34, info); if (sym) - (*info->fprintf_func) (info->stream, " <%s>", - bfd_asymbol_name (sym)); + (*info->fprintf_styled_func) (info->stream, dis_style_text, + " <%s>", bfd_asymbol_name (sym)); if (info->private_data != NULL && info->section != NULL @@ -1069,11 +1108,19 @@ print_insn_powerpc (bfd_vma memaddr, /* We could not find a match. */ if (insn_length == 4) - (*info->fprintf_func) (info->stream, ".long 0x%x", - (unsigned int) insn); + (*info->fprintf_styled_func) (info->stream, + dis_style_assembler_directive, ".long"); else - (*info->fprintf_func) (info->stream, ".word 0x%x", - (unsigned int) insn >> 16); + { + (*info->fprintf_styled_func) (info->stream, + dis_style_assembler_directive, ".word"); + insn >>= 16; + } + (*info->fprintf_styled_func) (info->stream, dis_style_text, " "); + (*info->fprintf_styled_func) (info->stream, dis_style_immediate, "0x%x", + (unsigned int) insn); + + return insn_length; } -- 2.25.4