[binutils-gdb] bpf: consolidate register format specifier handling [NFC]

"Jose E. Marchesi via Binutils-cvs" <[email protected]>
Newsgroups gmane.comp.gnu.binutils.cvs
Message-ID <[email protected]>
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=19583046731c8855a5ef6cb68e079b6f0ef7fae4

commit 19583046731c8855a5ef6cb68e079b6f0ef7fae4
Author: Vineet Gupta <[email protected]>
Date:   Tue Mar 31 13:42:10 2026 -0700

    bpf: consolidate register format specifier handling [NFC]
    
    NFC. Combine the separate if blocks for %dr/%dw and %sr/%sw into
    single blocks each, using the tag character to drive
    parse_bpf_register().
    
    Signed-off-by: Vineet Gupta <[email protected]>

Diff:
---
 gas/config/tc-bpf.c | 50 ++++++++------------------------------------------
 opcodes/bpf-dis.c   | 16 ++++------------
 2 files changed, 12 insertions(+), 54 deletions(-)

diff --git a/gas/config/tc-bpf.c b/gas/config/tc-bpf.c
index 57c2b869cba..ecb6c5a7053 100644
--- a/gas/config/tc-bpf.c
+++ b/gas/config/tc-bpf.c
@@ -1524,10 +1524,12 @@ md_assemble (char *str ATTRIBUTE_UNUSED)
                     s += 1;
                   p += 2;
                 }
-              else if (strncmp (p, "%dr", 3) == 0)
+              else if (strncmp (p, "%dr", 3) == 0
+                       || strncmp (p, "%dw", 3) == 0)
                 {
+                  char rw = *(p + 2);
                   uint8_t regno;
-                  char *news = parse_bpf_register (s, 'r', &regno);
+                  char *news = parse_bpf_register (s, rw, &regno);
 
                   if (news == NULL || (insn.has_dst && regno != insn.dst))
                     {
@@ -1543,48 +1545,12 @@ md_assemble (char *str ATTRIBUTE_UNUSED)
                   insn.has_dst = 1;
                   p += 3;
                 }
-              else if (strncmp (p, "%sr", 3) == 0)
+              else if (strncmp (p, "%sr", 3) == 0
+                       || strncmp (p, "%sw", 3) == 0)
                 {
+                  char rw = *(p + 2);
                   uint8_t regno;
-                  char *news = parse_bpf_register (s, 'r', &regno);
-
-                  if (news == NULL || (insn.has_src && regno != insn.src))
-                    {
-                      if (news != NULL)
-                        PARSE_ERROR ("expected register r%d, got r%d",
-                                     insn.dst, regno);
-                      else
-                        PARSE_ERROR ("expected register name, got '%s'", s);
-                      break;
-                    }
-                  s = news;
-                  insn.src = regno;
-                  insn.has_src = 1;
-                  p += 3;
-                }
-              else if (strncmp (p, "%dw", 3) == 0)
-                {
-                  uint8_t regno;
-                  char *news = parse_bpf_register (s, 'w', &regno);
-
-                  if (news == NULL || (insn.has_dst && regno != insn.dst))
-                    {
-                      if (news != NULL)
-                        PARSE_ERROR ("expected register r%d, got r%d",
-                                     insn.dst, regno);
-                      else
-                        PARSE_ERROR ("expected register name, got '%s'", s);
-                      break;
-                    }
-                  s = news;
-                  insn.dst = regno;
-                  insn.has_dst = 1;
-                  p += 3;
-                }
-              else if (strncmp (p, "%sw", 3) == 0)
-                {
-                  uint8_t regno;
-                  char *news = parse_bpf_register (s, 'w', &regno);
+                  char *news = parse_bpf_register (s, rw, &regno);
 
                   if (news == NULL || (insn.has_src && regno != insn.src))
                     {
diff --git a/opcodes/bpf-dis.c b/opcodes/bpf-dis.c
index 7b89a7994ee..de8d417dcd3 100644
--- a/opcodes/bpf-dis.c
+++ b/opcodes/bpf-dis.c
@@ -214,22 +214,14 @@ print_insn_bpf (bfd_vma pc, disassemble_info *info)
                   (*info->fprintf_styled_func) (info->stream, dis_style_text, " ");
                   p += 2;
                 }
-              else if (strncmp (p, "%dr", 3) == 0)
+              else if (strncmp (p, "%dr", 3) == 0
+                       || strncmp (p, "%dw", 3) == 0)
                 {
                   print_register (info, p, bpf_extract_dst (word, endian));
                   p += 3;
                 }
-              else if (strncmp (p, "%sr", 3) == 0)
-                {
-                  print_register (info, p, bpf_extract_src (word, endian));
-                  p += 3;
-                }
-              else if (strncmp (p, "%dw", 3) == 0)
-                {
-                  print_register (info, p, bpf_extract_dst (word, endian));
-                  p += 3;
-                }
-              else if (strncmp (p, "%sw", 3) == 0)
+              else if (strncmp (p, "%sr", 3) == 0
+                       || strncmp (p, "%sw", 3) == 0)
                 {
                   print_register (info, p, bpf_extract_src (word, endian));
                   p += 3;
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.