public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] libopcodes/ppc: add support for disassembler styling
@ 2022-07-12 13:36 Andrew Burgess
  2022-07-14  2:09 ` Alan Modra
  2022-07-18 16:31 ` [PATCHv2 0/2] PPC Styled Disassembler Support Andrew Burgess
  0 siblings, 2 replies; 9+ messages in thread
From: Andrew Burgess @ 2022-07-12 13:36 UTC (permalink / raw)
  To: binutils; +Cc: Andrew Burgess

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


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] libopcodes/ppc: add support for disassembler styling
  2022-07-12 13:36 [PATCH] libopcodes/ppc: add support for disassembler styling Andrew Burgess
@ 2022-07-14  2:09 ` Alan Modra
  2022-07-14  6:08   ` Jan Beulich
  2022-07-18 16:31 ` [PATCHv2 0/2] PPC Styled Disassembler Support Andrew Burgess
  1 sibling, 1 reply; 9+ messages in thread
From: Alan Modra @ 2022-07-14  2:09 UTC (permalink / raw)
  To: Andrew Burgess; +Cc: binutils

On Tue, Jul 12, 2022 at 02:36:09PM +0100, Andrew Burgess via Binutils wrote:
> 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.

Thanks for doing this!  The patch is OK as-is, but see comments
below if you'd like to make some improvements.

> 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.

This field is also present in conditional branches, and looking at
those is perhaps is the best way to understand what ought to be done
here.

eg. this instruction
lab: beq 7,lab
disassembles with "objdump -d" to
	beq cr7,0 <lab>
and with "objdump -d -Mraw" to see the underlying hardware insn
	bc 12,4*cr7+eq,0 <lab>

So the field probably ought to disassemble the "4*" and "+" as text,
the "eq" the same as the opcode mnemonic, and the "cr7" as a
register.  At least, that seems a reasonable way to highlight the
"interesting bits" to me.

>  	  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);

I think these are likely best disassembled as registers.  I really
don't know that much about the Xilinx embedded cpu, but I believe it
has these "user defined instructions" where the field is configurable
as a register or immediate.

-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] libopcodes/ppc: add support for disassembler styling
  2022-07-14  2:09 ` Alan Modra
@ 2022-07-14  6:08   ` Jan Beulich
  2022-07-14 11:18     ` Alan Modra
  0 siblings, 1 reply; 9+ messages in thread
From: Jan Beulich @ 2022-07-14  6:08 UTC (permalink / raw)
  To: Alan Modra, Andrew Burgess; +Cc: binutils

On 14.07.2022 04:09, Alan Modra via Binutils wrote:
> On Tue, Jul 12, 2022 at 02:36:09PM +0100, Andrew Burgess via Binutils wrote:
>> 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.
> 
> This field is also present in conditional branches, and looking at
> those is perhaps is the best way to understand what ought to be done
> here.
> 
> eg. this instruction
> lab: beq 7,lab
> disassembles with "objdump -d" to
> 	beq cr7,0 <lab>
> and with "objdump -d -Mraw" to see the underlying hardware insn
> 	bc 12,4*cr7+eq,0 <lab>
> 
> So the field probably ought to disassemble the "4*" and "+" as text,
> the "eq" the same as the opcode mnemonic,

Or maybe that new type which was introduced for Arm64?

Jan

> and the "cr7" as a
> register.  At least, that seems a reasonable way to highlight the
> "interesting bits" to me.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] libopcodes/ppc: add support for disassembler styling
  2022-07-14  6:08   ` Jan Beulich
@ 2022-07-14 11:18     ` Alan Modra
  0 siblings, 0 replies; 9+ messages in thread
From: Alan Modra @ 2022-07-14 11:18 UTC (permalink / raw)
  To: Jan Beulich; +Cc: Andrew Burgess, binutils

On Thu, Jul 14, 2022 at 08:08:07AM +0200, Jan Beulich wrote:
> On 14.07.2022 04:09, Alan Modra via Binutils wrote:
> > On Tue, Jul 12, 2022 at 02:36:09PM +0100, Andrew Burgess via Binutils wrote:
> >> 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.
> > 
> > This field is also present in conditional branches, and looking at
> > those is perhaps is the best way to understand what ought to be done
> > here.
> > 
> > eg. this instruction
> > lab: beq 7,lab
> > disassembles with "objdump -d" to
> > 	beq cr7,0 <lab>
> > and with "objdump -d -Mraw" to see the underlying hardware insn
> > 	bc 12,4*cr7+eq,0 <lab>
> > 
> > So the field probably ought to disassemble the "4*" and "+" as text,
> > the "eq" the same as the opcode mnemonic,
> 
> Or maybe that new type which was introduced for Arm64?

Yes, dis_style_sub_mnemonic works too.

> > and the "cr7" as a
> > register.  At least, that seems a reasonable way to highlight the
> > "interesting bits" to me.

-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCHv2 0/2] PPC Styled Disassembler Support
  2022-07-12 13:36 [PATCH] libopcodes/ppc: add support for disassembler styling Andrew Burgess
  2022-07-14  2:09 ` Alan Modra
@ 2022-07-18 16:31 ` Andrew Burgess
  2022-07-18 16:31   ` [PATCHv2 1/2] opcodes: add new sub-mnemonic disassembler style Andrew Burgess
  2022-07-18 16:31   ` [PATCHv2 2/2] libopcodes/ppc: add support for disassembler styling Andrew Burgess
  1 sibling, 2 replies; 9+ messages in thread
From: Andrew Burgess @ 2022-07-18 16:31 UTC (permalink / raw)
  To: binutils; +Cc: Andrew Burgess

Since v1:

  - Added patch #1, this is the exact same patch I'm proposing in my
    AArch64 thread; this adds dis_style_sub_mnemonic,

  - In patch #2, updated the formatting for things like '4*cr1+eq',
    the new styling is explained in the commit message,

  - Updated the styling of PPC_OPERAND_UDI operands to be register as
    suggested by Alan.

Thanks,
Andrew

---

Andrew Burgess (2):
  opcodes: add new sub-mnemonic disassembler style
  libopcodes/ppc: add support for disassembler styling

 binutils/objdump.c    |   2 +
 include/dis-asm.h     |   7 +++
 opcodes/disassemble.c |   1 +
 opcodes/ppc-dis.c     | 117 +++++++++++++++++++++++++++++++-----------
 4 files changed, 97 insertions(+), 30 deletions(-)

-- 
2.25.4


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCHv2 1/2] opcodes: add new sub-mnemonic disassembler style
  2022-07-18 16:31 ` [PATCHv2 0/2] PPC Styled Disassembler Support Andrew Burgess
@ 2022-07-18 16:31   ` Andrew Burgess
  2022-07-18 16:31   ` [PATCHv2 2/2] libopcodes/ppc: add support for disassembler styling Andrew Burgess
  1 sibling, 0 replies; 9+ messages in thread
From: Andrew Burgess @ 2022-07-18 16:31 UTC (permalink / raw)
  To: binutils; +Cc: Andrew Burgess

When adding libopcodes disassembler styling support for AArch64, it
feels like the results would be improved by having a new sub-mnemonic
style.  This will be used in cases like:

  add    w16, w7, w1, uxtb #2
                      ^^^^----- Here

And:

  cinc   w0, w1, ne
                 ^^----- Here

This commit just adds the new style, and prepares objdump to handle
the style.  A later commit will add AArch64 styling, and will actually
make use of the style.

As this style is currently unused, there should be no user visible
changes after this commit.
---
 binutils/objdump.c | 2 ++
 include/dis-asm.h  | 7 +++++++
 2 files changed, 9 insertions(+)

diff --git a/binutils/objdump.c b/binutils/objdump.c
index 67824053527..4076587151c 100644
--- a/binutils/objdump.c
+++ b/binutils/objdump.c
@@ -2167,6 +2167,7 @@ objdump_color_for_disassembler_style (enum disassembler_style style)
 	{
 	case dis_style_symbol: color = 32; break;
         case dis_style_assembler_directive:
+	case dis_style_sub_mnemonic:
 	case dis_style_mnemonic: color = 33; break;
 	case dis_style_register: color = 34; break;
 	case dis_style_address:
@@ -2185,6 +2186,7 @@ objdump_color_for_disassembler_style (enum disassembler_style style)
 	{
 	case dis_style_symbol: color = 40; break;
         case dis_style_assembler_directive:
+	case dis_style_sub_mnemonic:
 	case dis_style_mnemonic: color = 142; break;
 	case dis_style_register: color = 27; break;
 	case dis_style_address:
diff --git a/include/dis-asm.h b/include/dis-asm.h
index 4f91df12498..f1a83dc84e5 100644
--- a/include/dis-asm.h
+++ b/include/dis-asm.h
@@ -62,6 +62,13 @@ enum disassembler_style
      instructions.  */
   dis_style_mnemonic,
 
+  /* Some architectures include additional mnemonic like fields within the
+     instruction operands, e.g. on aarch64 'add w16, w7, w1, lsl #2' where
+     the 'lsl' is an additional piece of text that describes how the
+     instruction should behave.  This sub-mnemonic style can be used for
+     these pieces of text.  */
+  dis_style_sub_mnemonic,
+
   /* For things that aren't real machine instructions, but rather
      assembler directives, e.g. .byte, etc.  */
   dis_style_assembler_directive,
-- 
2.25.4


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCHv2 2/2] libopcodes/ppc: add support for disassembler styling
  2022-07-18 16:31 ` [PATCHv2 0/2] PPC Styled Disassembler Support Andrew Burgess
  2022-07-18 16:31   ` [PATCHv2 1/2] opcodes: add new sub-mnemonic disassembler style Andrew Burgess
@ 2022-07-18 16:31   ` Andrew Burgess
  2022-07-21  9:31     ` Alan Modra
  1 sibling, 1 reply; 9+ messages in thread
From: Andrew Burgess @ 2022-07-18 16:31 UTC (permalink / raw)
  To: binutils; +Cc: Andrew Burgess

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


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCHv2 2/2] libopcodes/ppc: add support for disassembler styling
  2022-07-18 16:31   ` [PATCHv2 2/2] libopcodes/ppc: add support for disassembler styling Andrew Burgess
@ 2022-07-21  9:31     ` Alan Modra
  2022-07-25 13:14       ` Andrew Burgess
  0 siblings, 1 reply; 9+ messages in thread
From: Alan Modra @ 2022-07-21  9:31 UTC (permalink / raw)
  To: Andrew Burgess; +Cc: binutils

On Mon, Jul 18, 2022 at 05:31:34PM +0100, Andrew Burgess via Binutils wrote:
> 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

As I said before, I believe "4" should be text.  It is not an
immediate that the user can alter, and is left out entirely when
disassembling if the cr field is cr0.  OK with that change.

-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCHv2 2/2] libopcodes/ppc: add support for disassembler styling
  2022-07-21  9:31     ` Alan Modra
@ 2022-07-25 13:14       ` Andrew Burgess
  0 siblings, 0 replies; 9+ messages in thread
From: Andrew Burgess @ 2022-07-25 13:14 UTC (permalink / raw)
  To: Alan Modra; +Cc: binutils

Alan Modra via Binutils <binutils@sourceware.org> writes:

> On Mon, Jul 18, 2022 at 05:31:34PM +0100, Andrew Burgess via Binutils wrote:
>> 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
>
> As I said before, I believe "4" should be text.  It is not an
> immediate that the user can alter, and is left out entirely when
> disassembling if the cr field is cr0.  OK with that change.

Thanks for the feedback.  I made the change you suggest (the "4" is now
styled as text), and pushed these patches.

Thanks,
Andrew


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2022-07-25 13:14 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-12 13:36 [PATCH] libopcodes/ppc: add support for disassembler styling Andrew Burgess
2022-07-14  2:09 ` Alan Modra
2022-07-14  6:08   ` Jan Beulich
2022-07-14 11:18     ` Alan Modra
2022-07-18 16:31 ` [PATCHv2 0/2] PPC Styled Disassembler Support Andrew Burgess
2022-07-18 16:31   ` [PATCHv2 1/2] opcodes: add new sub-mnemonic disassembler style Andrew Burgess
2022-07-18 16:31   ` [PATCHv2 2/2] libopcodes/ppc: add support for disassembler styling Andrew Burgess
2022-07-21  9:31     ` Alan Modra
2022-07-25 13:14       ` Andrew Burgess

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).