[PATCH 8/9] gdb: remove VALUE_REGNUM, add value::regnum

Simon Marchi
2023-12-21T19:19:35Z

Remove VALUE_REGNUM, replace it with a method on struct value. Set
`m_location.reg.regnum` directly from value::allocate_register_lazy,
which is fine because allocate_register_lazy is a static creation
function for struct value.

Change-Id: Id632502357da971617d9dce1e2eab9b56dbcf52d
---
gdb/findvar.c | 2 +-
gdb/frame.c | 7 +++----
gdb/python/py-unwind.c | 2 +-
gdb/stack.c | 3 +--
gdb/valops.c | 15 ++++++---------
gdb/value.c | 20 ++++++++++----------
gdb/value.h | 8 ++++----
7 files changed, 26 insertions(+), 31 deletions(-)

diff --git a/gdb/findvar.c b/gdb/findvar.c
index ef7129dab331..7c360eb37ff9 100644
--- a/gdb/findvar.c
+++ b/gdb/findvar.c
@@ -785,7 +785,7 @@ read_frame_register_value (value *value)
gdbarch *gdbarch = frame_unwind_arch (next_frame);
LONGEST offset = 0;
LONGEST reg_offset = value->offset ();
- int regnum = VALUE_REGNUM (value);
+ int regnum = value->regnum ();
int len = type_length_units (check_typedef (value->type ()));

/* Skip registers wholly inside of REG_OFFSET. */
diff --git a/gdb/frame.c b/gdb/frame.c
index 5f4c8c621a04..41003cc7771e 100644
--- a/gdb/frame.c
+++ b/gdb/frame.c
@@ -1196,7 +1196,7 @@ frame_register_unwind (frame_info_ptr next_frame, int regnum,
*lvalp = value->lval ();
*addrp = value->address ();
if (*lvalp == lval_register)
- *realnump = VALUE_REGNUM (value);
+ *realnump = value->regnum ();
else
*realnump = -1;

@@ -1304,8 +1304,7 @@ frame_unwind_register_value (frame_info_ptr next_frame, int regnum)
else
{
if (value->lval () == lval_register)
- gdb_printf (&debug_file, " register=%d",
- VALUE_REGNUM (value));
+ gdb_printf (&debug_file, " register=%d", value->regnum ());
else if (value->lval () == lval_memory)
gdb_printf (&debug_file, " address=%s",
paddress (gdbarch,
@@ -1417,7 +1416,7 @@ read_frame_register_unsigned (frame_info_ptr frame, int regnum,
{
struct gdbarch *gdbarch = get_frame_arch (frame);
enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
- int size = register_size (gdbarch, VALUE_REGNUM (regval));
+ int size = register_size (gdbarch, regval->regnum ());

*val = extract_unsigned_integer (regval->contents ().data (), size,
byte_order);
diff --git a/gdb/python/py-unwind.c b/gdb/python/py-unwind.c
index 8fed55beadca..f12485c22b77 100644
--- a/gdb/python/py-unwind.c
+++ b/gdb/python/py-unwind.c
@@ -336,7 +336,7 @@ unwind_infopy_add_saved_register (PyObject *self, PyObject *args, PyObject *kw)
struct value *user_reg_value
= value_of_user_reg (regnum, pending_frame->frame_info);
if (user_reg_value->lval () == lval_register)
- regnum = VALUE_REGNUM (user_reg_value);
+ regnum = user_reg_value->regnum ();
if (regnum >= gdbarch_num_cooked_regs (pending_frame->gdbarch))
{
PyErr_SetString (PyExc_ValueError, "Bad register");
diff --git a/gdb/stack.c b/gdb/stack.c
index 20bb85efd19e..fad4b62c6f72 100644
--- a/gdb/stack.c
+++ b/gdb/stack.c
@@ -1724,8 +1724,7 @@ info_frame_command_core (frame_info_ptr fi, bool selected_frame_p)
else if (value->lval () == lval_register)
{
gdb_printf (" Previous frame's sp in %s\n",
- gdbarch_register_name (gdbarch,
- VALUE_REGNUM (value)));
+ gdbarch_register_name (gdbarch, value->regnum ()));
}

release_value (value);
diff --git a/gdb/valops.c b/gdb/valops.c
index 5a5b3f14ad44..16cdf1f45530 100644
--- a/gdb/valops.c
+++ b/gdb/valops.c
@@ -1194,8 +1194,7 @@ value_assign (struct value *toval, struct value *fromval)
case lval_register:
{
frame_info_ptr next_frame = frame_find_by_id (toval->next_frame_id ());
-
- int value_reg = VALUE_REGNUM (toval);
+ int value_reg = toval->regnum ();

if (next_frame == nullptr)
error (_("Value being assigned to is no longer active."));
@@ -1240,15 +1239,13 @@ value_assign (struct value *toval, struct value *fromval)
}
else
{
- if (gdbarch_convert_register_p (gdbarch, VALUE_REGNUM (toval),
- type))
+ if (gdbarch_convert_register_p (gdbarch, toval->regnum (), type))
{
/* If TOVAL is a special machine register requiring
conversion of program values to a special raw
format. */
- gdbarch_value_to_register (gdbarch,
- get_prev_frame_always (next_frame),
- VALUE_REGNUM (toval), type,
+ gdbarch_value_to_register (gdbarch, next_frame,
+ toval->regnum (), type,
fromval->contents ().data ());
}
else
@@ -1415,8 +1412,8 @@ address_of_variable (struct symbol *var, const struct block *b)
frame_info_ptr frame = frame_find_by_id (val->next_frame_id ());
gdb_assert (frame != nullptr);

- regname = gdbarch_register_name (get_frame_arch (frame),
- VALUE_REGNUM (val));
+ regname
+ = gdbarch_register_name (get_frame_arch (frame), val->regnum ());
gdb_assert (regname != nullptr && *regname != '\0');

error (_("Address requested for identifier "
diff --git a/gdb/value.c b/gdb/value.c
index a16ba2fb5d6b..a69bc348167a 100644
--- a/gdb/value.c
+++ b/gdb/value.c
@@ -971,7 +971,7 @@ value::allocate_register_lazy (frame_info_ptr next_frame, int regnum,
value *result = value::allocate_lazy (type);

result->set_lval (lval_register);
- VALUE_REGNUM (result) = regnum;
+ result->m_location.reg.regnum = regnum;
result->m_location.reg.next_frame_id = get_frame_id (next_frame);

return result;
@@ -1429,11 +1429,14 @@ value::next_frame_id ()
return m_location.reg.next_frame_id;
}

-int *
-value::deprecated_regnum_hack ()
+/* See value.h. */
+
+int
+value::regnum ()
{
gdb_assert (m_lval == lval_register);
- return &m_location.reg.regnum;
+
+ return m_location.reg.regnum;
}

\f
@@ -3929,8 +3932,6 @@ value::fetch_lazy_memory ()
void
value::fetch_lazy_register ()
{
-
- int regnum;
struct type *type = check_typedef (this->type ());
struct value *new_val = this;

@@ -3946,7 +3947,7 @@ value::fetch_lazy_register ()
frame_info_ptr next_frame = frame_find_by_id (next_frame_id);
gdb_assert (next_frame != NULL);

- regnum = VALUE_REGNUM (new_val);
+ int regnum = new_val->regnum ();

/* Convertible register routines are used for multi-register
values and for interpretation in different types
@@ -3990,7 +3991,7 @@ value::fetch_lazy_register ()
{
frame_info_ptr frame = frame_find_by_id (this->next_frame_id ());
frame = get_prev_frame_always (frame);
- regnum = VALUE_REGNUM (this);
+ int regnum = this->regnum ();
gdbarch *gdbarch = get_frame_arch (frame);

string_file debug_file;
@@ -4011,8 +4012,7 @@ value::fetch_lazy_register ()
gdb::array_view<const gdb_byte> buf = new_val->contents ();

if (new_val->lval () == lval_register)
- gdb_printf (&debug_file, " register=%d",
- VALUE_REGNUM (new_val));
+ gdb_printf (&debug_file, " register=%d", new_val->regnum ());
else if (new_val->lval () == lval_memory)
gdb_printf (&debug_file, " address=%s",
paddress (gdbarch,
diff --git a/gdb/value.h b/gdb/value.h
index c33d2d8f0cd1..f1202007bb4f 100644
--- a/gdb/value.h
+++ b/gdb/value.h
@@ -378,7 +378,10 @@ struct value
The value must be of lval == lval_register. */
frame_id next_frame_id ();

- int *deprecated_regnum_hack ();
+ /* Return this value's register number.
+
+ The value must be of lval == lval_register. */
+ int regnum ();

/* contents() and contents_raw() both return the address of the gdb
buffer used to hold a copy of the contents of the lval.
@@ -967,9 +970,6 @@ extern void error_value_optimized_out (void);
/* Pointer to internal variable. */
#define VALUE_INTERNALVAR(val) (*((val)->deprecated_internalvar_hack ()))

-/* Register number if the value is from a register. */
-#define VALUE_REGNUM(val) (*((val)->deprecated_regnum_hack ()))
-
/* Return value after lval_funcs->coerce_ref (after check_typedef). Return
NULL if lval_funcs->coerce_ref is not applicable for whatever reason. */

--
2.43.0