[v2,04/11] RISC-V: Split riscv_get_map_state into two steps

Message ID 395973f56b805a000b0e2929c6b2ba6cd8fd140b.1669610611.git.research_trasio@irq.a4lg.com
State Accepted
Headers
Series RISC-V: Requirements for disassembler optimizations batch 1 |

Checks

Context Check Description
snail/binutils-gdb-check success Github commit url

Commit Message

Tsukasa OI Nov. 28, 2022, 4:43 a.m. UTC
  From: Tsukasa OI <research_trasio@irq.a4lg.com>

Because mapping symbol optimization would remove riscv_get_map_state
function, this commit splits symbol name checking step into a separate
function riscv_get_map_state_by_name.

Let alone the optimization, splitting the code improves readability.

opcodes/ChangeLog:

	* riscv-dis.c (riscv_get_map_state): Split symbol name checking
	into a separate function.  (riscv_get_map_state_by_name): New.
---
 opcodes/riscv-dis.c | 41 +++++++++++++++++++++++++++--------------
 1 file changed, 27 insertions(+), 14 deletions(-)
  

Patch

diff --git a/opcodes/riscv-dis.c b/opcodes/riscv-dis.c
index f6fdd5badfe6..38eb91349d9a 100644
--- a/opcodes/riscv-dis.c
+++ b/opcodes/riscv-dis.c
@@ -818,6 +818,24 @@  riscv_disassemble_insn (bfd_vma memaddr,
   return insnlen;
 }
 
+/* Return new mapping state if a given symbol name is of mapping symbols',
+   MAP_NONE otherwise.  If arch is not NULL and name denotes a mapping symbol
+   with ISA string, *arch is updated to the ISA string.  */
+
+static enum riscv_seg_mstate
+riscv_get_map_state_by_name (const char *name, const char** arch)
+{
+  if (startswith (name, "$x"))
+    {
+      if (arch && startswith (name + 2, "rv"))
+	*arch = name + 2;
+      return MAP_INSN;
+    }
+  else if (startswith (name, "$d"))
+    return MAP_DATA;
+  return MAP_NONE;
+}
+
 /* Return true if we find the suitable mapping symbol,
    and also update the STATE.  Otherwise, return false.  */
 
@@ -826,28 +844,23 @@  riscv_get_map_state (int n,
 		     enum riscv_seg_mstate *state,
 		     struct disassemble_info *info)
 {
-  const char *name;
+  const char *name, *arch = NULL;
 
   /* If the symbol is in a different section, ignore it.  */
   if (info->section != NULL
       && info->section != info->symtab[n]->section)
     return false;
 
-  name = bfd_asymbol_name(info->symtab[n]);
-  if (startswith (name, "$x"))
+  name = bfd_asymbol_name (info->symtab[n]);
+  enum riscv_seg_mstate newstate = riscv_get_map_state_by_name (name, &arch);
+  if (newstate == MAP_NONE)
+    return false;
+  *state = newstate;
+  if (arch)
     {
-      if (startswith (name + 2, "rv"))
-	{
-	  riscv_release_subset_list (&riscv_subsets);
-	  riscv_parse_subset (&riscv_rps_dis, name + 2);
-	}
-      *state = MAP_INSN;
+      riscv_release_subset_list (&riscv_subsets);
+      riscv_parse_subset (&riscv_rps_dis, arch);
     }
-  else if (startswith (name, "$d"))
-    *state = MAP_DATA;
-  else
-    return false;
-
   return true;
 }