From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1726) id 0D3EC383A30E; Mon, 25 Jul 2022 13:14:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0D3EC383A30E Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Andrew Burgess To: bfd-cvs@sourceware.org Subject: [binutils-gdb] libopcodes/ppc: add support for disassembler styling X-Act-Checkin: binutils-gdb X-Git-Author: Andrew Burgess X-Git-Refname: refs/heads/master X-Git-Oldrev: 4f46c0bc36471b725de0253bfec1a42a36e2c5c5 X-Git-Newrev: 36d94bd42b7b5f3d22d96ae8c850075814d315c0 Message-Id: <20220725131408.0D3EC383A30E@sourceware.org> Date: Mon, 25 Jul 2022 13:14:08 +0000 (GMT) X-BeenThere: binutils-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 25 Jul 2022 13:14:08 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D36d94bd42b7b= 5f3d22d96ae8c850075814d315c0 commit 36d94bd42b7b5f3d22d96ae8c850075814d315c0 Author: Andrew Burgess Date: Fri Jul 8 15:03:03 2022 +0100 libopcodes/ppc: add support for disassembler styling =20 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. =20 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. =20 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: =20 crand 4*cr1+lt,4*cr1+gt,4*cr1+eq =20 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: =20 4 - immediate, * - text, cr1 - register + - text lt - sub-mnemonic =20 If the user does not request styled output from objdump, then there should be no change in the disassembler output after this commit. =20 [1] https://sourceware.org/pipermail/binutils/2022-July/121771.html Diff: --- opcodes/disassemble.c | 1 + opcodes/ppc-dis.c | 115 +++++++++++++++++++++++++++++++++++++---------= ---- 2 files changed, 86 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 =3D true; break; #endif #ifdef ARCH_riscv diff --git a/opcodes/ppc-dis.c b/opcodes/ppc-dis.c index 45e8faeef5e..7c7dde87f00 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, stru= ct disassemble_info *info) sym =3D (*info->symbol_at_address_func) (ent, info); } } + (*info->fprintf_styled_func) (info->stream, dis_style_text, " ["); if (sym !=3D 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; =20 - (*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 =3D 8 - strlen (opcode->name); if (blanks <=3D 0) blanks =3D 1; @@ -976,39 +989,49 @@ print_insn_powerpc (bfd_vma memaddr, value =3D operand_value_powerpc (operand, insn, dialect); =20 if (op_separator =3D=3D need_comma) - (*info->fprintf_func) (info->stream, ","); + (*info->fprintf_styled_func) (info->stream, dis_style_text, ","); else if (op_separator =3D=3D 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, " "); =20 /* Print the operand as directed by the flags. */ if ((operand->flags & PPC_OPERAND_GPR) !=3D 0 || ((operand->flags & PPC_OPERAND_GPR_0) !=3D 0 && value !=3D 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) !=3D 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) !=3D 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) !=3D 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) !=3D 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) !=3D 0) (*info->print_address_func) (memaddr + value, info); else if ((operand->flags & PPC_OPERAND_ABSOLUTE) !=3D 0) (*info->print_address_func) ((bfd_vma) value & 0xffffffff, info); else if ((operand->flags & PPC_OPERAND_FSL) !=3D 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) !=3D 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) !=3D 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) !=3D 0 && (operand->flags & PPC_OPERAND_CR_BIT) =3D=3D 0 && (((dialect & PPC_OPCODE_PPC) !=3D 0) || ((dialect & PPC_OPCODE_VLE) !=3D 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) !=3D 0 && (operand->flags & PPC_OPERAND_CR_REG) =3D=3D 0 && (((dialect & PPC_OPCODE_PPC) !=3D 0) @@ -1019,13 +1042,35 @@ print_insn_powerpc (bfd_vma memaddr, int cc; =20 cr =3D value >> 2; - if (cr !=3D 0) - (*info->fprintf_func) (info->stream, "4*cr%d+", cr); cc =3D value & 3; - (*info->fprintf_func) (info->stream, "%s", cbnames[cc]); + if (cr !=3D 0) + { + (*info->fprintf_styled_func) (info->stream, dis_style_text, + "4*"); + (*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) !=3D 0) + style =3D dis_style_address_offset; + else + style =3D dis_style_immediate; + + (*info->fprintf_styled_func) (info->stream, style, + "%" PRId64, value); + } =20 if (operand->shift =3D=3D 52) is_pcrel =3D value !=3D 0; @@ -1033,7 +1078,7 @@ print_insn_powerpc (bfd_vma memaddr, d34 =3D value; =20 if (op_separator =3D=3D need_paren) - (*info->fprintf_func) (info->stream, ")"); + (*info->fprintf_styled_func) (info->stream, dis_style_text, ")"); =20 op_separator =3D need_comma; if ((operand->flags & PPC_OPERAND_PARENS) !=3D 0) @@ -1043,11 +1088,13 @@ print_insn_powerpc (bfd_vma memaddr, if (is_pcrel) { d34 +=3D memaddr; - (*info->fprintf_func) (info->stream, "\t# %" PRIx64, d34); + (*info->fprintf_styled_func) (info->stream, + dis_style_comment_start, + "\t# %" PRIx64, d34); asymbol *sym =3D (*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)); =20 if (info->private_data !=3D NULL && info->section !=3D NULL @@ -1069,11 +1116,19 @@ print_insn_powerpc (bfd_vma memaddr, =20 /* We could not find a match. */ if (insn_length =3D=3D 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 >>=3D 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; }