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 F1EF9385840F for ; Mon, 18 Jul 2022 16:31:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org F1EF9385840F Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-546-JfJ1m-M8O32CRWObidMyJQ-1; Mon, 18 Jul 2022 12:31:42 -0400 X-MC-Unique: JfJ1m-M8O32CRWObidMyJQ-1 Received: by mail-wm1-f69.google.com with SMTP id r10-20020a05600c284a00b003a2ff6c9d6aso7743301wmb.4 for ; Mon, 18 Jul 2022 09:31:42 -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:in-reply-to :references:mime-version:content-transfer-encoding; bh=BVcnxH0sLXjKKZA6cUI39grLCLmhG/t1viA+N/1q9Rk=; b=ZlpnxkxO1wN/10HEHLOD+YH5cA7WeAKbeSZ1Wvm9bR4S8KQy/1q8voL/GjtY8f94ss 5gCRoDjj5zJuFkgU5bewecxchP9eNshMDm8yBCUiLychOB76zz+x44plx0nfXwuTIc/L QSGjGftQ68pNV3Abw0tTfNUbrK2J6CHJZSsGVVn+xW5mS5zc13r4uM1P3xitJ37Ss4c4 QXBnKJPDSXTeGGdU4iCjbYuM33GkR0pzje9rhxOBPtGEE/IzSmR2JwLmhNf9fUrEA5wF 569LsAZ4EwkYEA0o9va3pftX9gw64La1X1dQESftJSNeFL1xYSSi83nzcHUKwwpsu49k 7aqg== X-Gm-Message-State: AJIora81ueiMZyZ56cIr2tYuqLR3fmt+Rukcltyq76FBP7wQtyP0Mjkj fokpsZXvwQRfvphQiDUW0k3ewunCQ0N4YFLB1NKwDw+yJZSM5Zy8fsRTE6xD4gxsnAlw79JDdVD kOIaTZVblY7r1Ss3IqYx85ZI8DajvDKk+JOHv5TX9DG3RTD8dXJz88BpSDQ3RjdcH2rSmVg== X-Received: by 2002:a05:6000:1cf:b0:21d:656c:dd9f with SMTP id t15-20020a05600001cf00b0021d656cdd9fmr23642470wrx.15.1658161901254; Mon, 18 Jul 2022 09:31:41 -0700 (PDT) X-Google-Smtp-Source: AGRyM1s9Bg1YlyGZK0aFxr/wTv7ENfR0WHpEFHQNKb4jDD82m9qOq7XjteOPbgTS5kuzH2j+SJ2Qsw== X-Received: by 2002:a05:6000:1cf:b0:21d:656c:dd9f with SMTP id t15-20020a05600001cf00b0021d656cdd9fmr23642445wrx.15.1658161900805; Mon, 18 Jul 2022 09:31:40 -0700 (PDT) Received: from localhost (15.72.115.87.dyn.plus.net. [87.115.72.15]) by smtp.gmail.com with ESMTPSA id b3-20020a05600c4e0300b003a2cf1ba9e2sm16137871wmq.6.2022.07.18.09.31.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Jul 2022 09:31:40 -0700 (PDT) From: Andrew Burgess To: binutils@sourceware.org Cc: Andrew Burgess Subject: [PATCHv2 2/2] libopcodes/ppc: add support for disassembler styling Date: Mon, 18 Jul 2022 17:31:34 +0100 Message-Id: <48f7e185bbfd3ba9da59fb36e8a49d1b414e3166.1658161688.git.aburgess@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: References: <20220712133609.3348690-1-aburgess@redhat.com> 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.5 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 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: Mon, 18 Jul 2022 16:31:46 -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. I initially tried styling this whole construct as a register[1], but during review it was suggested that instead different parts of the text should have different styles. In this commit I propose styling '4*cr1+lt' like this: 4 - immediate, * - text, cr1 - register + - text lt - sub-mnemonic If the user does not request styled output from objdump, then there should be no change in the disassembler output after this commit. [1] https://sourceware.org/pipermail/binutils/2022-July/121771.html --- opcodes/disassemble.c | 1 + opcodes/ppc-dis.c | 117 +++++++++++++++++++++++++++++++----------- 2 files changed, 88 insertions(+), 30 deletions(-) diff --git a/opcodes/disassemble.c b/opcodes/disassemble.c index e2e5a2608d6..4090e94b623 100644 --- a/opcodes/disassemble.c +++ b/opcodes/disassemble.c @@ -710,6 +710,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..57e792b922b 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_register, + "%" 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,37 @@ 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_immediate, "4"); + (*info->fprintf_styled_func) (info->stream, dis_style_text, + "*"); + (*info->fprintf_styled_func) (info->stream, + dis_style_register, + "cr%d", cr); + (*info->fprintf_styled_func) (info->stream, dis_style_text, + "+"); + } + + (*info->fprintf_styled_func) (info->stream, + dis_style_sub_mnemonic, + "%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 +1080,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 +1090,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 +1118,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