diff options
author | Andrew Burgess <aburgess@redhat.com> | 2022-04-28 13:31:07 +0100 |
---|---|---|
committer | Andrew Burgess <aburgess@redhat.com> | 2022-07-29 13:58:32 +0100 |
commit | 76a4c1e063fabb45d15597a6dd17813e92875a43 (patch) | |
tree | 6ffa0f49d408e3c15031aa5dc9c99573d492c05d /gas | |
parent | b44cfc5de139d7e2410e91846df0f0164d663d0b (diff) | |
download | gdb-76a4c1e063fabb45d15597a6dd17813e92875a43.zip gdb-76a4c1e063fabb45d15597a6dd17813e92875a43.tar.gz gdb-76a4c1e063fabb45d15597a6dd17813e92875a43.tar.bz2 |
libopcodes/aarch64: add support for disassembler styling
This commit enables disassembler styling for AArch64. After this
commit it is possible to have objdump style AArch64 disassembler
output (using --disassembler-color option). Once the required GDB
patches are merged, GDB will also style the disassembler output.
The changes to support styling are mostly split between two files
opcodes/aarch64-dis.c and opcodes/aarch64-opc.c.
The entry point for the AArch64 disassembler can be found in
aarch64-dis.c, this file handles printing the instruction mnemonics,
and assembler directives (e.g. '.byte', '.word', etc). Some operands,
mostly relating to assembler directives are also printed from this
file. This commit changes all of this to pass through suitable
styling information.
However, for most "normal" instructions, the instruction operands are
printed using a two step process. From aarch64-dis.c, in the
print_operands function, the function aarch64_print_operand is called,
this function is in aarch64-opc.c, and converts an instruction operand
into a string. Then, back in print_operands (aarch64-dis.c), the
operand string is printed.
Unfortunately, the string returned by aarch64_print_operand can be
quite complex, it will include syntax elements, like '[' and ']', in
addition to register names and immediate values. In some cases, a
single operand will expand into what will appear (to the user) as
multiple operands separated with a ','.
This makes the task of styling more complex, all these different
components need to by styled differently, so we need to get the
styling information out of aarch64_print_operand in some way.
The solution that I propose here is similar to the solution that I
used for the i386 disassembler.
Currently, aarch64_print_operand uses snprintf to write the operand
text into a buffer provided by the caller.
What I propose is that we pass an extra argument to the
aarch64_print_operand function, this argument will be a structure, the
structure contains a callback function and some state.
When aarch64_print_operand needs to format part of its output this can
be done by using the callback function within the new structure, this
callback returns a string with special embedded markers that indicate
which mode should be used for each piece of text. Back in
aarch64-dis.c we can spot these special style markers and use this to
split the disassembler output up and apply the correct style to each
piece.
To make aarch64-opc.c clearer a series of new static functions have
been added, e.g. 'style_reg', 'style_imm', etc. Each of these
functions formats a piece of text in a different style, 'register' and
'immediate' in this case.
Here's an example taken from aarch64-opc.c of the new functions in
use:
snprintf (buf, size, "[%s, %s]!",
style_reg (styler, base),
style_imm (styler, "#%d", opnd->addr.offset.imm));
The aarch64_print_operand function is also called from the assembler
to aid in printing diagnostic messages. Right now I have no plans to
add styling to the assembler output, and so, the callback function
used in the assembler ignores the styling information and just returns
an plain string.
I've used the source files in gas/testsuite/gas/aarch64/ for testing,
and have manually gone through and checked that the styling looks
reasonable, however, I'm not an AArch64 expert, so it is possible that
the odd piece is styled incorrectly. Please point out any mistakes
I've made.
With objdump disassembler color turned off, there should be no change
in the output after this commit.
Diffstat (limited to 'gas')
-rw-r--r-- | gas/config/tc-aarch64.c | 45 |
1 files changed, 44 insertions, 1 deletions
diff --git a/gas/config/tc-aarch64.c b/gas/config/tc-aarch64.c index e12b68e..f023e5b 100644 --- a/gas/config/tc-aarch64.c +++ b/gas/config/tc-aarch64.c @@ -5324,6 +5324,41 @@ assign_qualifier_sequence (aarch64_inst *instr, instr->operands[i].qualifier = *qualifiers; } +/* Callback used by aarch64_print_operand to apply STYLE to the + disassembler output created from FMT and ARGS. The STYLER object holds + any required state. Must return a pointer to a string (created from FMT + and ARGS) that will continue to be valid until the complete disassembled + instruction has been printed. + + We don't currently add any styling to the output of the disassembler as + used within assembler error messages, and so STYLE is ignored here. A + new string is allocated on the obstack help within STYLER and returned + to the caller. */ + +static const char *aarch64_apply_style + (struct aarch64_styler *styler, + enum disassembler_style style ATTRIBUTE_UNUSED, + const char *fmt, va_list args) +{ + int res; + char *ptr; + struct obstack *stack = (struct obstack *) styler->state; + va_list ap; + + /* Calculate the required space. */ + va_copy (ap, args); + res = vsnprintf (NULL, 0, fmt, ap); + va_end (ap); + gas_assert (res >= 0); + + /* Allocate space on the obstack and format the result. */ + ptr = (char *) obstack_alloc (stack, res + 1); + res = vsnprintf (ptr, (res + 1), fmt, args); + gas_assert (res >= 0); + + return ptr; +} + /* Print operands for the diagnosis purpose. */ static void @@ -5331,6 +5366,12 @@ print_operands (char *buf, const aarch64_opcode *opcode, const aarch64_opnd_info *opnds) { int i; + struct aarch64_styler styler; + struct obstack content; + obstack_init (&content); + + styler.apply_style = aarch64_apply_style; + styler.state = (void *) &content; for (i = 0; i < AARCH64_MAX_OPND_NUM; ++i) { @@ -5348,7 +5389,7 @@ print_operands (char *buf, const aarch64_opcode *opcode, /* Generate the operand string in STR. */ aarch64_print_operand (str, sizeof (str), 0, opcode, opnds, i, NULL, NULL, - NULL, cmt, sizeof (cmt), cpu_variant); + NULL, cmt, sizeof (cmt), cpu_variant, &styler); /* Delimiter. */ if (str[0] != '\0') @@ -5366,6 +5407,8 @@ print_operands (char *buf, const aarch64_opcode *opcode, strcat (buf, cmt); } } + + obstack_free (&content, NULL); } /* Send to stderr a string as information. */ |