Re: [PATCH 4/8] Remove regcache_descr::nr_raw_registers

Yao Qi
2017-11-02T15:20:06Z

Simon Marchi <simon.marchi@ericsson.com> writes:

> I would suggest adding a num_regs method to regcache to wrap the call to
> gdbarch_num_regs, so that if we need to change that call, we have only one
> place to change. Otherwise, LGTM.

Patch below is pushed in.

--
Yao (齐尧)

From f6f09fce6356c68893ad468d4c8872c4107aebcb Mon Sep 17 00:00:00 2001
From: Yao Qi <yao.qi@linaro.org>
Date: Thu, 19 Oct 2017 10:41:14 +0100
Subject: [PATCH 4/8] Remove regcache_descr::nr_raw_registers

struct regcache_descr has fields nr_raw_registers and gdbarch, and
nr_raw_registers can be got via gdbarch_num_regs (gdbarch), so it looks
nr_raw_registers is redundant. This patch removes it and adds a protected
method num_raw_registers.

gdb:

2017-11-02 Yao Qi <yao.qi@linaro.org>

* regcache.c (struct regcache_descr) <nr_raw_registers>: Remove.
(init_regcache_descr): Use gdbarch_num_regs.
(regcache::regcache): Likewise.
(regcache::get_register_status): Likewise.
(regcache::assert_raw_regnum): Likewise.
(regcache::cooked_read): Likewise.
(regcache::cooked_read_value): Likewise.
(regcache::cooked_write): Likewise.
(regcache::dump): Likewise.
(regcache::num_raw_registers): New method.
* regcache.h (class regcache) <num_raw_registers>: New.
---
gdb/regcache.c | 29 ++++++++++++++++-------------
gdb/regcache.h | 2 ++
2 files changed, 18 insertions(+), 13 deletions(-)

diff --git a/gdb/regcache.c b/gdb/regcache.c
index 0aee934..508f37b 100644
--- a/gdb/regcache.c
+++ b/gdb/regcache.c
@@ -51,7 +51,6 @@ struct regcache_descr
redundant information - if the PC is constructed from two
registers then those registers and not the PC lives in the raw
cache. */
- int nr_raw_registers;
long sizeof_raw_registers;

/* The cooked register space. Each cooked register in the range
@@ -100,7 +99,6 @@ init_regcache_descr (struct gdbarch *gdbarch)

/* Construct a strictly RAW register cache. Don't allow pseudo's
into the register cache. */
- descr->nr_raw_registers = gdbarch_num_regs (gdbarch);

/* Lay out the register cache.

@@ -116,7 +114,7 @@ init_regcache_descr (struct gdbarch *gdbarch)
= GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
descr->register_offset
= GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
- for (i = 0; i < descr->nr_raw_registers; i++)
+ for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
{
descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]);
descr->register_offset[i] = offset;
@@ -199,8 +197,7 @@ regcache::regcache (gdbarch *gdbarch, address_space *aspace_,
else
{
m_registers = XCNEWVEC (gdb_byte, m_descr->sizeof_raw_registers);
- m_register_status = XCNEWVEC (signed char,
- m_descr->nr_raw_registers);
+ m_register_status = XCNEWVEC (signed char, gdbarch_num_regs (gdbarch));
}
m_ptid = minus_one_ptid;
}
@@ -378,7 +375,7 @@ regcache::get_register_status (int regnum) const
if (m_readonly_p)
gdb_assert (regnum < m_descr->nr_cooked_registers);
else
- gdb_assert (regnum < m_descr->nr_raw_registers);
+ gdb_assert (regnum < num_raw_registers ());

return (enum register_status) m_register_status[regnum];
}
@@ -401,7 +398,7 @@ regcache::invalidate (int regnum)
void
regcache::assert_regnum (int regnum) const
{
- gdb_assert (regnum >= 0 && regnum < m_descr->nr_raw_registers);
+ gdb_assert (regnum >= 0 && regnum < gdbarch_num_regs (arch ()));
}

/* Global structure containing the current regcache. */
@@ -677,7 +674,7 @@ regcache::cooked_read (int regnum, gdb_byte *buf)
{
gdb_assert (regnum >= 0);
gdb_assert (regnum < m_descr->nr_cooked_registers);
- if (regnum < m_descr->nr_raw_registers)
+ if (regnum < num_raw_registers ())
return raw_read (regnum, buf);
else if (m_readonly_p
&& m_register_status[regnum] != REG_UNKNOWN)
@@ -731,7 +728,7 @@ regcache::cooked_read_value (int regnum)
gdb_assert (regnum >= 0);
gdb_assert (regnum < m_descr->nr_cooked_registers);

- if (regnum < m_descr->nr_raw_registers
+ if (regnum < num_raw_registers ()
|| (m_readonly_p && m_register_status[regnum] != REG_UNKNOWN)
|| !gdbarch_pseudo_register_read_value_p (m_descr->gdbarch))
{
@@ -890,7 +887,7 @@ regcache::cooked_write (int regnum, const gdb_byte *buf)
{
gdb_assert (regnum >= 0);
gdb_assert (regnum < m_descr->nr_cooked_registers);
- if (regnum < m_descr->nr_raw_registers)
+ if (regnum < num_raw_registers ())
raw_write (regnum, buf);
else
gdbarch_pseudo_register_write (m_descr->gdbarch, this,
@@ -1280,6 +1277,12 @@ regcache_write_pc (struct regcache *regcache, CORE_ADDR pc)
reinit_frame_cache ();
}

+int
+regcache::num_raw_registers () const
+{
+ return gdbarch_num_regs (arch ());
+}
+
void
regcache::debug_print_register (const char *func, int regno)
{
@@ -1435,7 +1438,7 @@ regcache::dump (ui_file *file, enum regcache_dump_what what_to_dump)
{
if (regnum < 0)
fprintf_unfiltered (file, "Raw value");
- else if (regnum >= m_descr->nr_raw_registers)
+ else if (regnum >= num_raw_registers ())
fprintf_unfiltered (file, "<cooked>");
else if (get_register_status (regnum) == REG_UNKNOWN)
fprintf_unfiltered (file, "<invalid>");
@@ -1461,7 +1464,7 @@ regcache::dump (ui_file *file, enum regcache_dump_what what_to_dump)
enum register_status status;
struct value *value = NULL;

- if (regnum < m_descr->nr_raw_registers)
+ if (regnum < num_raw_registers ())
{
raw_update (regnum);
status = get_register_status (regnum);
@@ -1529,7 +1532,7 @@ regcache::dump (ui_file *file, enum regcache_dump_what what_to_dump)
{
fprintf_unfiltered (file, "Rmt Nr g/G Offset");
}
- else if (regnum < m_descr->nr_raw_registers)
+ else if (regnum < num_raw_registers ())
{
int pnum, poffset;

diff --git a/gdb/regcache.h b/gdb/regcache.h
index 6fb790d..696b776 100644
--- a/gdb/regcache.h
+++ b/gdb/regcache.h
@@ -343,6 +343,8 @@ public:
protected:
regcache (gdbarch *gdbarch, address_space *aspace_, bool readonly_p_);

+ int num_raw_registers () const;
+
static std::forward_list<regcache *> current_regcache;

private:
--
1.9.1