Remove register_gdbarch_init
[binutils-gdb.git] / gdb / nds32-tdep.c
index 8cc9d5bc84c67720af75c68e67634c0e30db497b..5b555236d46302c59a7232ccdbf4c67d6bf4a6a4 100644 (file)
@@ -1,6 +1,6 @@
 /* Target-dependent code for the NDS32 architecture, for GDB.
 
-   Copyright (C) 2013-2018 Free Software Foundation, Inc.
+   Copyright (C) 2013-2022 Free Software Foundation, Inc.
    Contributed by Andes Technology Corporation.
 
    This file is part of GDB.
@@ -34,7 +34,7 @@
 #include "dis-asm.h"
 #include "user-regs.h"
 #include "elf-bfd.h"
-#include "dwarf2-frame.h"
+#include "dwarf2/frame.h"
 #include "remote.h"
 #include "target-descriptions.h"
 
@@ -289,7 +289,7 @@ typedef BP_MANIPULATION (nds32_break_insn) nds32_breakpoint;
 static int
 nds32_dwarf2_reg_to_regnum (struct gdbarch *gdbarch, int num)
 {
-  struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
+  nds32_gdbarch_tdep *tdep = gdbarch_tdep<nds32_gdbarch_tdep> (gdbarch);
   const int FSR = 38;
   const int FDR = FSR + 32;
 
@@ -314,16 +314,16 @@ nds32_dwarf2_reg_to_regnum (struct gdbarch *gdbarch, int num)
 }
 \f
 /* NDS32 register groups.  */
-static struct reggroup *nds32_cr_reggroup;
-static struct reggroup *nds32_ir_reggroup;
-static struct reggroup *nds32_mr_reggroup;
-static struct reggroup *nds32_dr_reggroup;
-static struct reggroup *nds32_pfr_reggroup;
-static struct reggroup *nds32_hspr_reggroup;
-static struct reggroup *nds32_dmar_reggroup;
-static struct reggroup *nds32_racr_reggroup;
-static struct reggroup *nds32_idr_reggroup;
-static struct reggroup *nds32_secur_reggroup;
+static const reggroup *nds32_cr_reggroup;
+static const reggroup *nds32_ir_reggroup;
+static const reggroup *nds32_mr_reggroup;
+static const reggroup *nds32_dr_reggroup;
+static const reggroup *nds32_pfr_reggroup;
+static const reggroup *nds32_hspr_reggroup;
+static const reggroup *nds32_dmar_reggroup;
+static const reggroup *nds32_racr_reggroup;
+static const reggroup *nds32_idr_reggroup;
+static const reggroup *nds32_secur_reggroup;
 
 static void
 nds32_init_reggroups (void)
@@ -343,14 +343,6 @@ nds32_init_reggroups (void)
 static void
 nds32_add_reggroups (struct gdbarch *gdbarch)
 {
-  /* Add pre-defined register groups.  */
-  reggroup_add (gdbarch, general_reggroup);
-  reggroup_add (gdbarch, float_reggroup);
-  reggroup_add (gdbarch, system_reggroup);
-  reggroup_add (gdbarch, all_reggroup);
-  reggroup_add (gdbarch, save_reggroup);
-  reggroup_add (gdbarch, restore_reggroup);
-
   /* Add NDS32 register groups.  */
   reggroup_add (gdbarch, nds32_cr_reggroup);
   reggroup_add (gdbarch, nds32_ir_reggroup);
@@ -368,7 +360,7 @@ nds32_add_reggroups (struct gdbarch *gdbarch)
 
 static int
 nds32_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
-                          struct reggroup *reggroup)
+                          const struct reggroup *reggroup)
 {
   const char *reg_name;
   const char *group_name;
@@ -398,7 +390,7 @@ nds32_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
   /* The NDS32 reggroup contains registers whose name is prefixed
      by reggroup name.  */
   reg_name = gdbarch_register_name (gdbarch, regnum);
-  group_name = reggroup_name (reggroup);
+  group_name = reggroup->name ();
   return !strncmp (reg_name, group_name, strlen (group_name));
 }
 \f
@@ -440,7 +432,7 @@ nds32_pseudo_register_read (struct gdbarch *gdbarch,
                            readable_regcache *regcache, int regnum,
                            gdb_byte *buf)
 {
-  struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
+  nds32_gdbarch_tdep *tdep = gdbarch_tdep<nds32_gdbarch_tdep> (gdbarch);
   gdb_byte reg_buf[8];
   int offset, fdr_regnum;
   enum register_status status;
@@ -479,7 +471,7 @@ nds32_pseudo_register_write (struct gdbarch *gdbarch,
                             struct regcache *regcache, int regnum,
                             const gdb_byte *buf)
 {
-  struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
+  nds32_gdbarch_tdep *tdep = gdbarch_tdep<nds32_gdbarch_tdep> (gdbarch);
   gdb_byte reg_buf[8];
   int offset, fdr_regnum;
 
@@ -500,9 +492,9 @@ nds32_pseudo_register_write (struct gdbarch *gdbarch,
        offset = (regnum & 1) ? 0 : 4;
 
       fdr_regnum = NDS32_FD0_REGNUM + (regnum >> 1);
-      regcache_raw_read (regcache, fdr_regnum, reg_buf);
+      regcache->raw_read (fdr_regnum, reg_buf);
       memcpy (reg_buf + offset, buf, 4);
-      regcache_raw_write (regcache, fdr_regnum, reg_buf);
+      regcache->raw_write (fdr_regnum, reg_buf);
       return;
     }
 
@@ -616,7 +608,7 @@ static CORE_ADDR
 nds32_analyze_prologue (struct gdbarch *gdbarch, CORE_ADDR pc,
                        CORE_ADDR limit_pc, struct nds32_frame_cache *cache)
 {
-  struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
+  nds32_gdbarch_tdep *tdep = gdbarch_tdep<nds32_gdbarch_tdep> (gdbarch);
   int abi_use_fpr = nds32_abi_use_fpr (tdep->elf_abi);
   /* Current scanning status.  */
   int in_prologue_bb = 0;
@@ -997,6 +989,7 @@ nds32_frame_prev_register (struct frame_info *this_frame, void **this_cache,
 
 static const struct frame_unwind nds32_frame_unwind =
 {
+  "nds32 prologue",
   NORMAL_FRAME,
   default_frame_unwind_stop_reason,
   nds32_frame_this_id,
@@ -1176,7 +1169,7 @@ static int
 nds32_analyze_epilogue (struct gdbarch *gdbarch, CORE_ADDR pc,
                        struct nds32_frame_cache *cache)
 {
-  struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
+  nds32_gdbarch_tdep *tdep = gdbarch_tdep<nds32_gdbarch_tdep> (gdbarch);
   int abi_use_fpr = nds32_abi_use_fpr (tdep->elf_abi);
   CORE_ADDR limit_pc;
   uint32_t insn, insn_len;
@@ -1227,7 +1220,7 @@ nds32_analyze_epilogue (struct gdbarch *gdbarch, CORE_ADDR pc,
 static int
 nds32_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR addr)
 {
-  struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
+  nds32_gdbarch_tdep *tdep = gdbarch_tdep<nds32_gdbarch_tdep> (gdbarch);
   int abi_use_fpr = nds32_abi_use_fpr (tdep->elf_abi);
   int insn_type = INSN_NORMAL;
   int ret_found = 0;
@@ -1380,6 +1373,7 @@ nds32_epilogue_frame_prev_register (struct frame_info *this_frame,
 
 static const struct frame_unwind nds32_epilogue_frame_unwind =
 {
+  "nds32 epilogue",
   NORMAL_FRAME,
   default_frame_unwind_stop_reason,
   nds32_epilogue_frame_this_id,
@@ -1387,32 +1381,7 @@ static const struct frame_unwind nds32_epilogue_frame_unwind =
   NULL,
   nds32_epilogue_frame_sniffer
 };
-\f
-/* Implement the "dummy_id" gdbarch method.  */
-
-static struct frame_id
-nds32_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
-{
-  CORE_ADDR sp = get_frame_register_unsigned (this_frame, NDS32_SP_REGNUM);
-
-  return frame_id_build (sp, get_frame_pc (this_frame));
-}
-
-/* Implement the "unwind_pc" gdbarch method.  */
 
-static CORE_ADDR
-nds32_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
-{
-  return frame_unwind_register_unsigned (next_frame, NDS32_PC_REGNUM);
-}
-
-/* Implement the "unwind_sp" gdbarch method.  */
-
-static CORE_ADDR
-nds32_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
-{
-  return frame_unwind_register_unsigned (next_frame, NDS32_SP_REGNUM);
-}
 \f
 /* Floating type and struct type that has only one floating type member
    can pass value using FPU registers (when FPU ABI is used).  */
@@ -1427,70 +1396,26 @@ nds32_check_calling_use_fpr (struct type *type)
   while (1)
     {
       t = check_typedef (t);
-      typecode = TYPE_CODE (t);
+      typecode = t->code ();
       if (typecode != TYPE_CODE_STRUCT)
        break;
-      else if (TYPE_NFIELDS (t) != 1)
+      else if (t->num_fields () != 1)
        return 0;
       else
-       t = TYPE_FIELD_TYPE (t, 0);
+       t = t->field (0).type ();
     }
 
   return typecode == TYPE_CODE_FLT;
 }
 
-/* Return the alignment (in bytes) of the given type.  */
-
-static int
-nds32_type_align (struct type *type)
-{
-  int n;
-  int align;
-  int falign;
-
-  type = check_typedef (type);
-  switch (TYPE_CODE (type))
-    {
-    default:
-      /* Should never happen.  */
-      internal_error (__FILE__, __LINE__, _("unknown type alignment"));
-      return 4;
-
-    case TYPE_CODE_PTR:
-    case TYPE_CODE_ENUM:
-    case TYPE_CODE_INT:
-    case TYPE_CODE_FLT:
-    case TYPE_CODE_SET:
-    case TYPE_CODE_RANGE:
-    case TYPE_CODE_REF:
-    case TYPE_CODE_CHAR:
-    case TYPE_CODE_BOOL:
-      return TYPE_LENGTH (type);
-
-    case TYPE_CODE_ARRAY:
-    case TYPE_CODE_COMPLEX:
-      return nds32_type_align (TYPE_TARGET_TYPE (type));
-
-    case TYPE_CODE_STRUCT:
-    case TYPE_CODE_UNION:
-      align = 1;
-      for (n = 0; n < TYPE_NFIELDS (type); n++)
-       {
-         falign = nds32_type_align (TYPE_FIELD_TYPE (type, n));
-         if (falign > align)
-           align = falign;
-       }
-      return align;
-    }
-}
-
 /* Implement the "push_dummy_call" gdbarch method.  */
 
 static CORE_ADDR
 nds32_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
                       struct regcache *regcache, CORE_ADDR bp_addr,
                       int nargs, struct value **args, CORE_ADDR sp,
-                      int struct_return, CORE_ADDR struct_addr)
+                      function_call_return_method return_method,
+                      CORE_ADDR struct_addr)
 {
   const int REND = 6;          /* End for register offset.  */
   int goff = 0;                        /* Current gpr offset for argument.  */
@@ -1499,7 +1424,7 @@ nds32_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
   int i;
   ULONGEST regval;
   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
-  struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
+  nds32_gdbarch_tdep *tdep = gdbarch_tdep<nds32_gdbarch_tdep> (gdbarch);
   struct type *func_type = value_type (function);
   int abi_use_fpr = nds32_abi_use_fpr (tdep->elf_abi);
   int abi_split = nds32_abi_split (tdep->elf_abi);
@@ -1511,7 +1436,7 @@ nds32_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
   /* If STRUCT_RETURN is true, then the struct return address (in
      STRUCT_ADDR) will consume the first argument-passing register.
      Both adjust the register count and store that value.  */
-  if (struct_return)
+  if (return_method == return_method_struct)
     {
       regcache_cooked_write_unsigned (regcache, NDS32_R0_REGNUM, struct_addr);
       goff++;
@@ -1521,7 +1446,7 @@ nds32_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
   for (i = 0; i < nargs; i++)
     {
       struct type *type = value_type (args[i]);
-      int align = nds32_type_align (type);
+      int align = type_align (type);
 
       /* If align is zero, it may be an empty struct.
         Just ignore the argument of empty struct.  */
@@ -1547,8 +1472,8 @@ nds32_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
       type = value_type (args[i]);
       calling_use_fpr = nds32_check_calling_use_fpr (type);
       len = TYPE_LENGTH (type);
-      align = nds32_type_align (type);
-      val = value_contents (args[i]);
+      align = type_align (type);
+      val = value_contents (args[i]).data ();
 
       /* The size of a composite type larger than 4 bytes will be rounded
         up to the nearest multiple of 4.  */
@@ -1564,8 +1489,8 @@ nds32_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
         For ABI2FP+, the caller pushes only named arguments in registers
         and pushes all unnamed arguments in stack.  */
 
-      if (abi_use_fpr && TYPE_VARARGS (func_type)
-         && i >= TYPE_NFIELDS (func_type))
+      if (abi_use_fpr && func_type->has_varargs ()
+         && i >= func_type->num_fields ())
        goto use_stack;
 
       /* Try to use FPRs to pass arguments only when
@@ -1587,13 +1512,11 @@ nds32_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
              switch (len)
                {
                case 4:
-                 regcache_cooked_write (regcache,
-                                        tdep->fs0_regnum + foff, val);
+                 regcache->cooked_write (tdep->fs0_regnum + foff, val);
                  foff++;
                  break;
                case 8:
-                 regcache_cooked_write (regcache,
-                                        NDS32_FD0_REGNUM + (foff >> 1), val);
+                 regcache->cooked_write (NDS32_FD0_REGNUM + (foff >> 1), val);
                  foff += 2;
                  break;
                default:
@@ -1729,7 +1652,7 @@ nds32_extract_return_value (struct gdbarch *gdbarch, struct type *type,
                            struct regcache *regcache, gdb_byte *valbuf)
 {
   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
-  struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
+  nds32_gdbarch_tdep *tdep = gdbarch_tdep<nds32_gdbarch_tdep> (gdbarch);
   int abi_use_fpr = nds32_abi_use_fpr (tdep->elf_abi);
   int calling_use_fpr;
   int len;
@@ -1740,9 +1663,9 @@ nds32_extract_return_value (struct gdbarch *gdbarch, struct type *type,
   if (abi_use_fpr && calling_use_fpr)
     {
       if (len == 4)
-       regcache_cooked_read (regcache, tdep->fs0_regnum, valbuf);
+       regcache->cooked_read (tdep->fs0_regnum, valbuf);
       else if (len == 8)
-       regcache_cooked_read (regcache, NDS32_FD0_REGNUM, valbuf);
+       regcache->cooked_read (NDS32_FD0_REGNUM, valbuf);
       else
        internal_error (__FILE__, __LINE__,
                        _("Cannot extract return value of %d bytes "
@@ -1788,7 +1711,7 @@ nds32_extract_return_value (struct gdbarch *gdbarch, struct type *type,
        }
       else if (len == 4)
        {
-         regcache_cooked_read (regcache, NDS32_R0_REGNUM, valbuf);
+         regcache->cooked_read (NDS32_R0_REGNUM, valbuf);
        }
       else if (len < 8)
        {
@@ -1805,8 +1728,8 @@ nds32_extract_return_value (struct gdbarch *gdbarch, struct type *type,
        }
       else
        {
-         regcache_cooked_read (regcache, NDS32_R0_REGNUM, valbuf);
-         regcache_cooked_read (regcache, NDS32_R0_REGNUM + 1, valbuf + 4);
+         regcache->cooked_read (NDS32_R0_REGNUM, valbuf);
+         regcache->cooked_read (NDS32_R0_REGNUM + 1, valbuf + 4);
        }
     }
 }
@@ -1819,7 +1742,7 @@ nds32_store_return_value (struct gdbarch *gdbarch, struct type *type,
                          struct regcache *regcache, const gdb_byte *valbuf)
 {
   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
-  struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
+  nds32_gdbarch_tdep *tdep = gdbarch_tdep<nds32_gdbarch_tdep> (gdbarch);
   int abi_use_fpr = nds32_abi_use_fpr (tdep->elf_abi);
   int calling_use_fpr;
   int len;
@@ -1830,9 +1753,9 @@ nds32_store_return_value (struct gdbarch *gdbarch, struct type *type,
   if (abi_use_fpr && calling_use_fpr)
     {
       if (len == 4)
-       regcache_cooked_write (regcache, tdep->fs0_regnum, valbuf);
+       regcache->cooked_write (tdep->fs0_regnum, valbuf);
       else if (len == 8)
-       regcache_cooked_write (regcache, NDS32_FD0_REGNUM, valbuf);
+       regcache->cooked_write (NDS32_FD0_REGNUM, valbuf);
       else
        internal_error (__FILE__, __LINE__,
                        _("Cannot store return value of %d bytes "
@@ -1849,7 +1772,7 @@ nds32_store_return_value (struct gdbarch *gdbarch, struct type *type,
        }
       else if (len == 4)
        {
-         regcache_cooked_write (regcache, NDS32_R0_REGNUM, valbuf);
+         regcache->cooked_write (NDS32_R0_REGNUM, valbuf);
        }
       else if (len < 8)
        {
@@ -1867,8 +1790,8 @@ nds32_store_return_value (struct gdbarch *gdbarch, struct type *type,
        }
       else
        {
-         regcache_cooked_write (regcache, NDS32_R0_REGNUM, valbuf);
-         regcache_cooked_write (regcache, NDS32_R0_REGNUM + 1, valbuf + 4);
+         regcache->cooked_write (NDS32_R0_REGNUM, valbuf);
+         regcache->cooked_write (NDS32_R0_REGNUM + 1, valbuf + 4);
        }
     }
 }
@@ -2024,9 +1947,8 @@ static struct gdbarch *
 nds32_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
 {
   struct gdbarch *gdbarch;
-  struct gdbarch_tdep *tdep;
   struct gdbarch_list *best_arch;
-  struct tdesc_arch_data *tdesc_data = NULL;
+  tdesc_arch_data_up tdesc_data;
   const struct target_desc *tdesc = info.target_desc;
   int elf_abi = E_NDS_ABI_AABI;
   int fpu_freg = -1;
@@ -2042,7 +1964,8 @@ nds32_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
        best_arch != NULL;
        best_arch = gdbarch_list_lookup_by_info (best_arch->next, &info))
     {
-      struct gdbarch_tdep *idep = gdbarch_tdep (best_arch->gdbarch);
+      nds32_gdbarch_tdep *idep
+       = gdbarch_tdep<nds32_gdbarch_tdep> (best_arch->gdbarch);
 
       if (idep->elf_abi != elf_abi)
        continue;
@@ -2059,14 +1982,12 @@ nds32_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
 
   tdesc_data = tdesc_data_alloc ();
 
-  if (!nds32_validate_tdesc_p (tdesc, tdesc_data, &fpu_freg, &use_pseudo_fsrs))
-    {
-      tdesc_data_cleanup (tdesc_data);
-      return NULL;
-    }
+  if (!nds32_validate_tdesc_p (tdesc, tdesc_data.get (), &fpu_freg,
+                              &use_pseudo_fsrs))
+    return NULL;
 
   /* Allocate space for the new architecture.  */
-  tdep = XCNEW (struct gdbarch_tdep);
+  nds32_gdbarch_tdep *tdep = new nds32_gdbarch_tdep;
   tdep->fpu_freg = fpu_freg;
   tdep->use_pseudo_fsrs = use_pseudo_fsrs;
   tdep->fs0_regnum = -1;
@@ -2093,7 +2014,7 @@ nds32_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
     num_regs = NDS32_NUM_REGS + num_fdr_map[fpu_freg] + num_fsr_map[fpu_freg];
 
   set_gdbarch_num_regs (gdbarch, num_regs);
-  tdesc_use_registers (gdbarch, tdesc, tdesc_data);
+  tdesc_use_registers (gdbarch, tdesc, std::move (tdesc_data));
 
   /* Cache the register number of fs0.  */
   if (fpu_freg != -1)
@@ -2101,7 +2022,7 @@ nds32_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
 
   /* Add NDS32 register aliases.  To avoid search in user register name space,
      user_reg_map_name_to_regnum is not used.  */
-  maxregs = (gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch));
+  maxregs = gdbarch_num_cooked_regs (gdbarch);
   for (i = 0; i < ARRAY_SIZE (nds32_register_aliases); i++)
     {
       int regnum, j;
@@ -2132,7 +2053,7 @@ nds32_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
   nds32_add_reggroups (gdbarch);
 
   /* Hook in ABI-specific overrides, if they have been registered.  */
-  info.tdesc_data = tdesc_data;
+  info.tdesc_data = tdesc_data.get ();
   gdbarch_init_osabi (info, gdbarch);
 
   /* Override tdesc_register callbacks for system registers.  */
@@ -2140,14 +2061,11 @@ nds32_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
 
   set_gdbarch_sp_regnum (gdbarch, NDS32_SP_REGNUM);
   set_gdbarch_pc_regnum (gdbarch, NDS32_PC_REGNUM);
-  set_gdbarch_unwind_sp (gdbarch, nds32_unwind_sp);
-  set_gdbarch_unwind_pc (gdbarch, nds32_unwind_pc);
   set_gdbarch_stack_frame_destroyed_p (gdbarch, nds32_stack_frame_destroyed_p);
   set_gdbarch_dwarf2_reg_to_regnum (gdbarch, nds32_dwarf2_reg_to_regnum);
 
   set_gdbarch_push_dummy_call (gdbarch, nds32_push_dummy_call);
   set_gdbarch_return_value (gdbarch, nds32_return_value);
-  set_gdbarch_dummy_id (gdbarch, nds32_dummy_id);
 
   set_gdbarch_skip_prologue (gdbarch, nds32_skip_prologue);
   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
@@ -2170,11 +2088,12 @@ nds32_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
   return gdbarch;
 }
 
+void _initialize_nds32_tdep ();
 void
-_initialize_nds32_tdep (void)
+_initialize_nds32_tdep ()
 {
   /* Initialize gdbarch.  */
-  register_gdbarch_init (bfd_arch_nds32, nds32_gdbarch_init);
+  gdbarch_register (bfd_arch_nds32, nds32_gdbarch_init);
 
   initialize_tdesc_nds32 ();
   nds32_init_reggroups ();