Re: RFA: use constructor to build 'struct regset' objects

Jim Blandy
2004-05-20T06:51:00Z

Jim Blandy <jimb@redhat.com> writes:

> kettenis@gnu.org writes:
> > From: Jim Blandy <jimb@redhat.com>
> > Date: 18 May 2004 23:11:06 -0500
> >
> > How's this:
> >
> > 2004-05-17 Jim Blandy <jimb@redhat.com>
> >
> > Use a constructor function to create regset structures.
> > * regset.h (supply_regset_ftype, fill_regset_ftype): New typedefs.
> > (struct regset): Use supply_regset_ftype. Add new
> > 'fill_regset' member.
> > (regset_xmalloc): New declaration.
> > * regset.c: New file.
> > * am64-tdep.c (amd64_regset_from_core_section): Use
> > regset_xmalloc to construct regset structures.
> > * amd64obsd-tdep.c (amd64obsd_regset_from_core_section): Same.
> > * i386-tdep.c (i386_regset_from_core_section): Same.
> > * i386nbsd-tdep.c (i386nbsd_aout_regset_from_core_section): Same.
> > * i386obsd-tdep.c (i386obsd_aout_regset_from_core_section): Same.
> > * sparc64fbsd-tdep.c (sparc64fbsd_init_abi): Same.
> > * sparc64nbsd-tdep.c (sparc64nbsd_init_abi): Same.
> > * sparc64obsd-tdep.c (sparc64obsd_init_abi): Same.
> > * sparcnbsd-tdep.c (sparc32nbsd_init_abi): Same.
> > * Makefile.in (COMMON_OBS): Add regset.o.
> > (regset.o): New rule.
> >
> > Could you use collect_regset instead of fill_regset. I deliberately
> > added regcache_raw_collect instead of regcache_raw_fill. Oh, and your
> > prototype for fill_regset_ftype is wrong. Should be:
> >
> > typedef void (collect_regset_ftype) (const struct regsecache *,
> > const struct regcache *,
> > int, void *, size_t);
> >
> > (watch the `const').
> >
> > Consider a patch with those changes pre-approved.
>
> Committed, thanks.

Oh, and here's the changed patch:

2004-05-17 Jim Blandy <jimb@redhat.com>

Use a constructor function to create regset structures.
* regset.h (supply_regset_ftype, collect_regset_ftype): New typedefs.
(struct regset): Use supply_regset_ftype. Add new
'collect_regset' member.
(regset_xmalloc): New declaration.
* regset.c: New file.
* am64-tdep.c (amd64_regset_from_core_section): Use
regset_xmalloc to construct regset structures.
* amd64obsd-tdep.c (amd64obsd_regset_from_core_section): Same.
* i386-tdep.c (i386_regset_from_core_section): Same.
* i386nbsd-tdep.c (i386nbsd_aout_regset_from_core_section): Same.
* i386obsd-tdep.c (i386obsd_aout_regset_from_core_section): Same.
* sparc64fbsd-tdep.c (sparc64fbsd_init_abi): Same.
* sparc64nbsd-tdep.c (sparc64nbsd_init_abi): Same.
* sparc64obsd-tdep.c (sparc64obsd_init_abi): Same.
* sparcnbsd-tdep.c (sparc32nbsd_init_abi): Same.
* Makefile.in (COMMON_OBS): Add regset.o.
(regset.o): New rule.

Index: gdb/Makefile.in
===================================================================
RCS file: /cvs/src/src/gdb/Makefile.in,v
retrieving revision 1.572
diff -c -p -r1.572 Makefile.in
*** gdb/Makefile.in 17 May 2004 15:16:39 -0000 1.572
--- gdb/Makefile.in 20 May 2004 00:46:57 -0000
*************** COMMON_OBS = $(DEPFILES) $(YYOBJ) \
*** 910,916 ****
frame-base.o \
gnu-v2-abi.o gnu-v3-abi.o hpacc-abi.o cp-abi.o cp-support.o \
cp-namespace.o \
! reggroups.o \
trad-frame.o \
tramp-frame.o

--- 910,916 ----
frame-base.o \
gnu-v2-abi.o gnu-v3-abi.o hpacc-abi.o cp-abi.o cp-support.o \
cp-namespace.o \
! reggroups.o regset.o \
trad-frame.o \
tramp-frame.o

*************** regcache.o: regcache.c $(defs_h) $(infer
*** 2197,2202 ****
--- 2197,2203 ----
$(gdb_string_h) $(gdbcmd_h) $(observer_h)
reggroups.o: reggroups.c $(defs_h) $(reggroups_h) $(gdbtypes_h) \
$(gdb_assert_h) $(regcache_h) $(command_h) $(gdbcmd_h)
+ regset.o: regset.c $(defs_h) $(regset_h) $(gdb_assert_h)
remote.o: remote.c $(defs_h) $(gdb_string_h) $(inferior_h) $(bfd_h) \
$(symfile_h) $(target_h) $(gdbcmd_h) $(objfiles_h) $(gdb_stabs_h) \
$(gdbthread_h) $(remote_h) $(regcache_h) $(value_h) $(gdb_assert_h) \
Index: gdb/amd64-tdep.c
===================================================================
RCS file: /cvs/src/src/gdb/amd64-tdep.c,v
retrieving revision 1.10
diff -c -p -r1.10 amd64-tdep.c
*** gdb/amd64-tdep.c 18 May 2004 21:20:25 -0000 1.10
--- gdb/amd64-tdep.c 20 May 2004 00:46:58 -0000
*************** amd64_regset_from_core_section (struct g
*** 1074,1084 ****
if (strcmp (sect_name, ".reg2") == 0 && sect_size == tdep->sizeof_fpregset)
{
if (tdep->fpregset == NULL)
! {
! tdep->fpregset = XMALLOC (struct regset);
! tdep->fpregset->descr = tdep;
! tdep->fpregset->supply_regset = amd64_supply_fpregset;
! }

return tdep->fpregset;
}
--- 1074,1080 ----
if (strcmp (sect_name, ".reg2") == 0 && sect_size == tdep->sizeof_fpregset)
{
if (tdep->fpregset == NULL)
! tdep->fpregset = regset_xmalloc (tdep, amd64_supply_fpregset, NULL);

return tdep->fpregset;
}
Index: gdb/amd64obsd-tdep.c
===================================================================
RCS file: /cvs/src/src/gdb/amd64obsd-tdep.c,v
retrieving revision 1.10
diff -c -p -r1.10 amd64obsd-tdep.c
*** gdb/amd64obsd-tdep.c 15 May 2004 21:06:50 -0000 1.10
--- gdb/amd64obsd-tdep.c 20 May 2004 00:46:58 -0000
*************** amd64obsd_regset_from_core_section (stru
*** 63,73 ****
&& sect_size >= tdep->sizeof_gregset + I387_SIZEOF_FXSAVE)
{
if (tdep->gregset == NULL)
! {
! tdep->gregset = XMALLOC (struct regset);
! tdep->gregset->descr = tdep;
! tdep->gregset->supply_regset = amd64obsd_supply_regset;
! }
return tdep->gregset;
}

--- 63,69 ----
&& sect_size >= tdep->sizeof_gregset + I387_SIZEOF_FXSAVE)
{
if (tdep->gregset == NULL)
! tdep->gregset = regset_xmalloc (tdep, amd64obsd_supply_regset, NULL);
return tdep->gregset;
}

Index: gdb/i386-tdep.c
===================================================================
RCS file: /cvs/src/src/gdb/i386-tdep.c,v
retrieving revision 1.191
diff -c -p -r1.191 i386-tdep.c
*** gdb/i386-tdep.c 8 May 2004 23:02:10 -0000 1.191
--- gdb/i386-tdep.c 20 May 2004 00:46:59 -0000
*************** i386_regset_from_core_section (struct gd
*** 1662,1672 ****
if (strcmp (sect_name, ".reg") == 0 && sect_size == tdep->sizeof_gregset)
{
if (tdep->gregset == NULL)
! {
! tdep->gregset = XMALLOC (struct regset);
! tdep->gregset->descr = tdep;
! tdep->gregset->supply_regset = i386_supply_gregset;
! }
return tdep->gregset;
}

--- 1662,1668 ----
if (strcmp (sect_name, ".reg") == 0 && sect_size == tdep->sizeof_gregset)
{
if (tdep->gregset == NULL)
! tdep->gregset = regset_xmalloc (tdep, i386_supply_gregset, NULL);
return tdep->gregset;
}

*************** i386_regset_from_core_section (struct gd
*** 1675,1685 ****
&& sect_size == I387_SIZEOF_FXSAVE))
{
if (tdep->fpregset == NULL)
! {
! tdep->fpregset = XMALLOC (struct regset);
! tdep->fpregset->descr = tdep;
! tdep->fpregset->supply_regset = i386_supply_fpregset;
! }
return tdep->fpregset;
}

--- 1671,1677 ----
&& sect_size == I387_SIZEOF_FXSAVE))
{
if (tdep->fpregset == NULL)
! tdep->fpregset = regset_xmalloc (tdep, i386_supply_fpregset, NULL);
return tdep->fpregset;
}

Index: gdb/i386nbsd-tdep.c
===================================================================
RCS file: /cvs/src/src/gdb/i386nbsd-tdep.c,v
retrieving revision 1.24
diff -c -p -r1.24 i386nbsd-tdep.c
*** gdb/i386nbsd-tdep.c 9 Apr 2004 23:26:19 -0000 1.24
--- gdb/i386nbsd-tdep.c 20 May 2004 00:46:59 -0000
*************** i386nbsd_aout_regset_from_core_section (
*** 86,96 ****
&& sect_size >= tdep->sizeof_gregset + I387_SIZEOF_FSAVE)
{
if (tdep->gregset == NULL)
! {
! tdep->gregset = XMALLOC (struct regset);
! tdep->gregset->descr = tdep;
! tdep->gregset->supply_regset = i386nbsd_aout_supply_regset;
! }
return tdep->gregset;
}

--- 86,93 ----
&& sect_size >= tdep->sizeof_gregset + I387_SIZEOF_FSAVE)
{
if (tdep->gregset == NULL)
! tdep->gregset
! = regset_xmalloc (tdep, i386nbsd_aout_supply_regset, NULL);
return tdep->gregset;
}

Index: gdb/i386obsd-tdep.c
===================================================================
RCS file: /cvs/src/src/gdb/i386obsd-tdep.c,v
retrieving revision 1.15
diff -c -p -r1.15 i386obsd-tdep.c
*** gdb/i386obsd-tdep.c 9 Apr 2004 23:26:19 -0000 1.15
--- gdb/i386obsd-tdep.c 20 May 2004 00:46:59 -0000
*************** i386obsd_aout_regset_from_core_section (
*** 141,151 ****
&& sect_size >= tdep->sizeof_gregset + I387_SIZEOF_FSAVE)
{
if (tdep->gregset == NULL)
! {
! tdep->gregset = XMALLOC (struct regset);
! tdep->gregset->descr = tdep;
! tdep->gregset->supply_regset = i386obsd_aout_supply_regset;
! }
return tdep->gregset;
}

--- 141,148 ----
&& sect_size >= tdep->sizeof_gregset + I387_SIZEOF_FSAVE)
{
if (tdep->gregset == NULL)
! tdep->gregset
! = regset_xmalloc (tdep, i386obsd_aout_supply_regset, NULL);
return tdep->gregset;
}

Index: gdb/regset.c
===================================================================
RCS file: gdb/regset.c
diff -N gdb/regset.c
*** gdb/regset.c 1 Jan 1970 00:00:00 -0000
--- gdb/regset.c 20 May 2004 00:46:59 -0000
***************
*** 0 ****
--- 1,39 ----
+ /* Regset support functions, for GDB.
+
+ Copyright 2004 Free Software Foundation, Inc.
+
+ This file is part of GDB.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA. */
+
+ #include "defs.h"
+ #include "regset.h"
+ #include "gdb_assert.h"
+
+
+ struct regset *
+ regset_xmalloc (void *descr,
+ supply_regset_ftype *supply_regset,
+ collect_regset_ftype *collect_regset)
+ {
+ struct regset *r = (struct regset *) xmalloc (sizeof (*r));
+
+ r->descr = descr;
+ r->supply_regset = supply_regset;
+ r->collect_regset = collect_regset;
+
+ return r;
+ }
Index: gdb/regset.h
===================================================================
RCS file: /cvs/src/src/gdb/regset.h,v
retrieving revision 1.1
diff -c -p -r1.1 regset.h
*** gdb/regset.h 26 Sep 2003 14:36:56 -0000 1.1
--- gdb/regset.h 20 May 2004 00:46:59 -0000
*************** struct gdbarch;
*** 26,31 ****
--- 26,36 ----
struct regcache;

/* Data structure describing a register set. */
+ typedef void (supply_regset_ftype) (const struct regset *, struct regcache *,
+ int, const void *, size_t);
+ typedef void (collect_regset_ftype) (const struct regset *,
+ const struct regcache *,
+ int, const void *, size_t);

struct regset
{
*************** struct regset
*** 33,41 ****
providing some sort of description of the register set. */
const void *descr;

! /* Function supplying a register set to a register cache. */
! void (*supply_regset) (const struct regset *, struct regcache *,
! int, const void *, size_t);
};

#endif /* regset.h */
--- 38,60 ----
providing some sort of description of the register set. */
const void *descr;

! /* Function supplying values in a register set to a register cache. */
! supply_regset_ftype *supply_regset;
!
! /* Function collecting values in a register set from a register cache. */
! collect_regset_ftype *collect_regset;
};
+
+
+ /* Allocate a fresh 'struct regset' whose descr is DESCR, whose
+ supply_regset function is SUPPLY_REGSET, and whose collect_regset
+ function is COLLECT_REGSET. If the regset has no collect function,
+ pass NULL for COLLECT_REGSET.
+
+ The object returned is allocated using xmalloc. */
+ extern struct regset *regset_xmalloc (void *descr,
+ supply_regset_ftype *supply_regset,
+ collect_regset_ftype *collect_regset);
+

#endif /* regset.h */
Index: gdb/sparc64fbsd-tdep.c
===================================================================
RCS file: /cvs/src/src/gdb/sparc64fbsd-tdep.c,v
retrieving revision 1.6
diff -c -p -r1.6 sparc64fbsd-tdep.c
*** gdb/sparc64fbsd-tdep.c 10 Apr 2004 09:40:02 -0000 1.6
--- gdb/sparc64fbsd-tdep.c 20 May 2004 00:47:00 -0000
*************** sparc64fbsd_init_abi (struct gdbarch_inf
*** 199,211 ****
{
struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

! tdep->gregset = XMALLOC (struct regset);
! tdep->gregset->descr = &sparc64fbsd_gregset;
! tdep->gregset->supply_regset = sparc64fbsd_supply_gregset;
tdep->sizeof_gregset = 256;

! tdep->fpregset = XMALLOC (struct regset);
! tdep->fpregset->supply_regset = sparc64fbsd_supply_fpregset;
tdep->sizeof_fpregset = 272;

frame_unwind_append_sniffer (gdbarch, sparc64fbsd_sigtramp_frame_sniffer);
--- 199,209 ----
{
struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

! tdep->gregset
! = regset_xmalloc (&sparc64fbsd_gregset, sparc64fbsd_supply_gregset, NULL);
tdep->sizeof_gregset = 256;

! tdep->fpregset = regset_xmalloc (NULL, sparc64fbsd_supply_fpregset, NULL);
tdep->sizeof_fpregset = 272;

frame_unwind_append_sniffer (gdbarch, sparc64fbsd_sigtramp_frame_sniffer);
Index: gdb/sparc64nbsd-tdep.c
===================================================================
RCS file: /cvs/src/src/gdb/sparc64nbsd-tdep.c,v
retrieving revision 1.7
diff -c -p -r1.7 sparc64nbsd-tdep.c
*** gdb/sparc64nbsd-tdep.c 10 Apr 2004 09:40:02 -0000 1.7
--- gdb/sparc64nbsd-tdep.c 20 May 2004 00:47:00 -0000
*************** sparc64nbsd_init_abi (struct gdbarch_inf
*** 226,238 ****
{
struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

! tdep->gregset = XMALLOC (struct regset);
! tdep->gregset->descr = &sparc64nbsd_gregset;
! tdep->gregset->supply_regset = sparc64nbsd_supply_gregset;
tdep->sizeof_gregset = 160;

! tdep->fpregset = XMALLOC (struct regset);
! tdep->fpregset->supply_regset = sparc64nbsd_supply_fpregset;
tdep->sizeof_fpregset = 272;

frame_unwind_append_sniffer (gdbarch, sparc64nbsd_sigtramp_frame_sniffer);
--- 226,236 ----
{
struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

! tdep->gregset
! = regset_xmalloc (&sparc64nbsd_gregset, sparc64nbsd_supply_gregset, NULL);
tdep->sizeof_gregset = 160;

! tdep->fpregset = regset_xmalloc (NULL, sparc64nbsd_supply_fpregset, NULL);
tdep->sizeof_fpregset = 272;

frame_unwind_append_sniffer (gdbarch, sparc64nbsd_sigtramp_frame_sniffer);
Index: gdb/sparc64obsd-tdep.c
===================================================================
RCS file: /cvs/src/src/gdb/sparc64obsd-tdep.c,v
retrieving revision 1.3
diff -c -p -r1.3 sparc64obsd-tdep.c
*** gdb/sparc64obsd-tdep.c 10 Apr 2004 09:40:02 -0000 1.3
--- gdb/sparc64obsd-tdep.c 20 May 2004 00:47:00 -0000
*************** sparc64obsd_init_abi (struct gdbarch_inf
*** 184,192 ****
{
struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

! tdep->gregset = XMALLOC (struct regset);
! tdep->gregset->descr = &sparc64obsd_core_gregset;
! tdep->gregset->supply_regset = sparc64obsd_supply_gregset;
tdep->sizeof_gregset = 832;

frame_unwind_append_sniffer (gdbarch, sparc64obsd_sigtramp_frame_sniffer);
--- 184,192 ----
{
struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

! tdep->gregset = regset_xmalloc (&sparc64obsd_core_gregset,
! sparc64obsd_supply_gregset,
! NULL);
tdep->sizeof_gregset = 832;

frame_unwind_append_sniffer (gdbarch, sparc64obsd_sigtramp_frame_sniffer);
Index: gdb/sparcnbsd-tdep.c
===================================================================
RCS file: /cvs/src/src/gdb/sparcnbsd-tdep.c,v
retrieving revision 1.18
diff -c -p -r1.18 sparcnbsd-tdep.c
*** gdb/sparcnbsd-tdep.c 18 Apr 2004 22:58:06 -0000 1.18
--- gdb/sparcnbsd-tdep.c 20 May 2004 00:47:00 -0000
*************** sparc32nbsd_init_abi (struct gdbarch_inf
*** 274,286 ****
set_gdbarch_long_double_bit (gdbarch, 64);
set_gdbarch_long_double_format (gdbarch, &floatformat_ieee_double_big);

! tdep->gregset = XMALLOC (struct regset);
! tdep->gregset->descr = &sparc32nbsd_gregset;
! tdep->gregset->supply_regset = sparc32nbsd_supply_gregset;
tdep->sizeof_gregset = 20 * 4;

! tdep->fpregset = XMALLOC (struct regset);
! tdep->fpregset->supply_regset = sparc32nbsd_supply_fpregset;
tdep->sizeof_fpregset = 33 * 4;

frame_unwind_append_sniffer (gdbarch, sparc32nbsd_sigtramp_frame_sniffer);
--- 274,284 ----
set_gdbarch_long_double_bit (gdbarch, 64);
set_gdbarch_long_double_format (gdbarch, &floatformat_ieee_double_big);

! tdep->gregset
! = regset_xmalloc (&sparc32nbsd_gregset, sparc32nbsd_supply_gregset, NULL);
tdep->sizeof_gregset = 20 * 4;

! tdep->fpregset = regset_xmalloc (NULL, sparc32nbsd_supply_fpregset, NULL);
tdep->sizeof_fpregset = 33 * 4;

frame_unwind_append_sniffer (gdbarch, sparc32nbsd_sigtramp_frame_sniffer);