[6/9] binutils: Make DWARF register name lookup be via a function pointer

Message ID 594b17c1298aba6b951abfd2770c3436edfdfd23.1574423265.git.andrew.burgess@embecosm.com
State New
Headers show
Series
  • DWARF Register Number Handling, Including RISC-V CSRs
Related show

Commit Message

Andrew Burgess Nov. 22, 2019, 12:10 p.m.
Add a layer of indirection for DWARF register name lookup by
introducing a function pointer.  Right now all targets use the same
table based implementation that they always have, however, this will
change in a later commit.

binutils/ChangeLog:

	* dwarf.c (typedef dwarf_regname_lookup_ftype): New typedef.
	(dwarf_regnames_lookup_func): New static global.
	(init_dwarf_regnames_i386): Set dwarf_regnames_lookup_func.
	(init_dwarf_regnames_iamcu): Likewise.
	(init_dwarf_regnames_x86_64): Likewise.
	(init_dwarf_regnames_aarch64): Likewise.
	(init_dwarf_regnames_s390): Likewise.
	(init_dwarf_regnames_riscv): Likewise.
	(init_dwarf_regnames_by_elf_machine_code): Reset
	dwarf_regnames_lookup_func to NULL.
	(init_dwarf_regnames_by_bfd_arch_and_mach): Likewise.
	(regname_internal_by_table_only): New function.
	(regname): Make use of dwarf_regnames_lookup_func.

Change-Id: Ibbff61c0abea32927f35c9e478793039ab8bb57d
---
 binutils/ChangeLog | 16 ++++++++++++++++
 binutils/dwarf.c   | 43 +++++++++++++++++++++++++++++++++++--------
 2 files changed, 51 insertions(+), 8 deletions(-)

-- 
2.14.5

Comments

Palmer Dabbelt via binutils Nov. 22, 2019, 10:15 p.m. | #1
On Fri, 22 Nov 2019 04:10:30 PST (-0800), andrew.burgess@embecosm.com wrote:
> Add a layer of indirection for DWARF register name lookup by

> introducing a function pointer.  Right now all targets use the same

> table based implementation that they always have, however, this will

> change in a later commit.

>

> binutils/ChangeLog:

>

> 	* dwarf.c (typedef dwarf_regname_lookup_ftype): New typedef.

> 	(dwarf_regnames_lookup_func): New static global.

> 	(init_dwarf_regnames_i386): Set dwarf_regnames_lookup_func.

> 	(init_dwarf_regnames_iamcu): Likewise.

> 	(init_dwarf_regnames_x86_64): Likewise.

> 	(init_dwarf_regnames_aarch64): Likewise.

> 	(init_dwarf_regnames_s390): Likewise.

> 	(init_dwarf_regnames_riscv): Likewise.

> 	(init_dwarf_regnames_by_elf_machine_code): Reset

> 	dwarf_regnames_lookup_func to NULL.

> 	(init_dwarf_regnames_by_bfd_arch_and_mach): Likewise.

> 	(regname_internal_by_table_only): New function.

> 	(regname): Make use of dwarf_regnames_lookup_func.

>

> Change-Id: Ibbff61c0abea32927f35c9e478793039ab8bb57d

> ---

>  binutils/ChangeLog | 16 ++++++++++++++++

>  binutils/dwarf.c   | 43 +++++++++++++++++++++++++++++++++++--------

>  2 files changed, 51 insertions(+), 8 deletions(-)

>

> diff --git a/binutils/dwarf.c b/binutils/dwarf.c

> index ea6e7eefa86..87ac0562eff 100644

> --- a/binutils/dwarf.c

> +++ b/binutils/dwarf.c

> @@ -38,6 +38,7 @@

>  #define MIN(a, b) ((a) < (b) ? (a) : (b))

>

>  static const char *regname (unsigned int regno, int row);

> +static const char *regname_internal_by_table_only (unsigned int regno);

>

>  static int have_frame_base;

>  static int need_base_address;

> @@ -7360,9 +7361,12 @@ typedef struct Frame_Chunk

>  }

>  Frame_Chunk;

>

> +typedef const char *(*dwarf_regname_lookup_ftype) (unsigned int);

> +static dwarf_regname_lookup_ftype dwarf_regnames_lookup_func;

>  static const char *const *dwarf_regnames;

>  static unsigned int dwarf_regnames_count;

>

> +

>  /* A marker for a col_type that means this column was never referenced

>     in the frame info.  */

>  #define DW_CFA_unreferenced (-1)

> @@ -7469,6 +7473,7 @@ init_dwarf_regnames_i386 (void)

>  {

>    dwarf_regnames = dwarf_regnames_i386;

>    dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_i386);

> +  dwarf_regnames_lookup_func = regname_internal_by_table_only;

>  }

>

>  static void

> @@ -7476,6 +7481,7 @@ init_dwarf_regnames_iamcu (void)

>  {

>    dwarf_regnames = dwarf_regnames_iamcu;

>    dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_iamcu);

> +  dwarf_regnames_lookup_func = regname_internal_by_table_only;

>  }

>

>  static const char *const dwarf_regnames_x86_64[] =

> @@ -7515,6 +7521,7 @@ init_dwarf_regnames_x86_64 (void)

>  {

>    dwarf_regnames = dwarf_regnames_x86_64;

>    dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_x86_64);

> +  dwarf_regnames_lookup_func = regname_internal_by_table_only;

>  }

>

>  static const char *const dwarf_regnames_aarch64[] =

> @@ -7542,6 +7549,7 @@ init_dwarf_regnames_aarch64 (void)

>  {

>    dwarf_regnames = dwarf_regnames_aarch64;

>    dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_aarch64);

> +  dwarf_regnames_lookup_func = regname_internal_by_table_only;

>  }

>

>  static const char *const dwarf_regnames_s390[] =

> @@ -7566,6 +7574,7 @@ init_dwarf_regnames_s390 (void)

>  {

>    dwarf_regnames = dwarf_regnames_s390;

>    dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_s390);

> +  dwarf_regnames_lookup_func = regname_internal_by_table_only;

>  }

>

>  static const char *const dwarf_regnames_riscv[] =

> @@ -7587,11 +7596,14 @@ init_dwarf_regnames_riscv (void)

>  {

>    dwarf_regnames = dwarf_regnames_riscv;

>    dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_riscv);

> +  dwarf_regnames_lookup_func = regname_internal_by_table_only;

>  }

>

>  void

>  init_dwarf_regnames_by_elf_machine_code (unsigned int e_machine)

>  {

> +  dwarf_regnames_lookup_func = NULL;

> +

>    switch (e_machine)

>      {

>      case EM_386:

> @@ -7632,6 +7644,8 @@ void

>  init_dwarf_regnames_by_bfd_arch_and_mach (enum bfd_architecture arch,

>                                            unsigned long mach)

>  {

> +  dwarf_regnames_lookup_func = NULL;

> +

>    switch (arch)

>      {

>      case bfd_arch_i386:

> @@ -7674,18 +7688,31 @@ init_dwarf_regnames_by_bfd_arch_and_mach (enum bfd_architecture arch,

>  }

>

>  static const char *

> -regname (unsigned int regno, int row)

> +regname_internal_by_table_only (unsigned int regno)

>  {

> -  static char reg[64];

> -

> -  if (dwarf_regnames

> +  if (dwarf_regnames != NULL

>        && regno < dwarf_regnames_count

>        && dwarf_regnames [regno] != NULL)

> +    return dwarf_regnames [regno];

> +

> +  return NULL;

> +}

> +

> +static const char *

> +regname (unsigned int regno, int name_only_p)

> +{

> +  static char reg[64];

> +

> +  const char *name = NULL;

> +

> +  if (dwarf_regnames_lookup_func != NULL)

> +    name = dwarf_regnames_lookup_func (regno);

> +

> +  if (name != NULL)

>      {

> -      if (row)

> -	return dwarf_regnames [regno];

> -      snprintf (reg, sizeof (reg), "r%d (%s)", regno,

> -		dwarf_regnames [regno]);

> +      if (name_only_p)

> +	return name;

> +      snprintf (reg, sizeof (reg), "r%d (%s)", regno, name);

>      }

>    else

>      snprintf (reg, sizeof (reg), "r%d", regno);


Reviewed-by: Palmer Dabbelt <palmerdabbelt@google.com>

Patch

diff --git a/binutils/dwarf.c b/binutils/dwarf.c
index ea6e7eefa86..87ac0562eff 100644
--- a/binutils/dwarf.c
+++ b/binutils/dwarf.c
@@ -38,6 +38,7 @@ 
 #define MIN(a, b) ((a) < (b) ? (a) : (b))
 
 static const char *regname (unsigned int regno, int row);
+static const char *regname_internal_by_table_only (unsigned int regno);
 
 static int have_frame_base;
 static int need_base_address;
@@ -7360,9 +7361,12 @@  typedef struct Frame_Chunk
 }
 Frame_Chunk;
 
+typedef const char *(*dwarf_regname_lookup_ftype) (unsigned int);
+static dwarf_regname_lookup_ftype dwarf_regnames_lookup_func;
 static const char *const *dwarf_regnames;
 static unsigned int dwarf_regnames_count;
 
+
 /* A marker for a col_type that means this column was never referenced
    in the frame info.  */
 #define DW_CFA_unreferenced (-1)
@@ -7469,6 +7473,7 @@  init_dwarf_regnames_i386 (void)
 {
   dwarf_regnames = dwarf_regnames_i386;
   dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_i386);
+  dwarf_regnames_lookup_func = regname_internal_by_table_only;
 }
 
 static void
@@ -7476,6 +7481,7 @@  init_dwarf_regnames_iamcu (void)
 {
   dwarf_regnames = dwarf_regnames_iamcu;
   dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_iamcu);
+  dwarf_regnames_lookup_func = regname_internal_by_table_only;
 }
 
 static const char *const dwarf_regnames_x86_64[] =
@@ -7515,6 +7521,7 @@  init_dwarf_regnames_x86_64 (void)
 {
   dwarf_regnames = dwarf_regnames_x86_64;
   dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_x86_64);
+  dwarf_regnames_lookup_func = regname_internal_by_table_only;
 }
 
 static const char *const dwarf_regnames_aarch64[] =
@@ -7542,6 +7549,7 @@  init_dwarf_regnames_aarch64 (void)
 {
   dwarf_regnames = dwarf_regnames_aarch64;
   dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_aarch64);
+  dwarf_regnames_lookup_func = regname_internal_by_table_only;
 }
 
 static const char *const dwarf_regnames_s390[] =
@@ -7566,6 +7574,7 @@  init_dwarf_regnames_s390 (void)
 {
   dwarf_regnames = dwarf_regnames_s390;
   dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_s390);
+  dwarf_regnames_lookup_func = regname_internal_by_table_only;
 }
 
 static const char *const dwarf_regnames_riscv[] =
@@ -7587,11 +7596,14 @@  init_dwarf_regnames_riscv (void)
 {
   dwarf_regnames = dwarf_regnames_riscv;
   dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_riscv);
+  dwarf_regnames_lookup_func = regname_internal_by_table_only;
 }
 
 void
 init_dwarf_regnames_by_elf_machine_code (unsigned int e_machine)
 {
+  dwarf_regnames_lookup_func = NULL;
+
   switch (e_machine)
     {
     case EM_386:
@@ -7632,6 +7644,8 @@  void
 init_dwarf_regnames_by_bfd_arch_and_mach (enum bfd_architecture arch,
                                           unsigned long mach)
 {
+  dwarf_regnames_lookup_func = NULL;
+
   switch (arch)
     {
     case bfd_arch_i386:
@@ -7674,18 +7688,31 @@  init_dwarf_regnames_by_bfd_arch_and_mach (enum bfd_architecture arch,
 }
 
 static const char *
-regname (unsigned int regno, int row)
+regname_internal_by_table_only (unsigned int regno)
 {
-  static char reg[64];
-
-  if (dwarf_regnames
+  if (dwarf_regnames != NULL
       && regno < dwarf_regnames_count
       && dwarf_regnames [regno] != NULL)
+    return dwarf_regnames [regno];
+
+  return NULL;
+}
+
+static const char *
+regname (unsigned int regno, int name_only_p)
+{
+  static char reg[64];
+
+  const char *name = NULL;
+
+  if (dwarf_regnames_lookup_func != NULL)
+    name = dwarf_regnames_lookup_func (regno);
+
+  if (name != NULL)
     {
-      if (row)
-	return dwarf_regnames [regno];
-      snprintf (reg, sizeof (reg), "r%d (%s)", regno,
-		dwarf_regnames [regno]);
+      if (name_only_p)
+	return name;
+      snprintf (reg, sizeof (reg), "r%d (%s)", regno, name);
     }
   else
     snprintf (reg, sizeof (reg), "r%d", regno);