[PATCH 18/24] gdb: migrate i386 and amd64 to the new gdbarch_pseudo_register_write

Simon Marchi
2023-11-08T05:16:59Z

From: Simon Marchi <simon.marchi@efficios.com>

Make i386 and amd64 use the new gdbarch_pseudo_register_write. This
fixes writing to pseudo registers in non-current frames for those
architectures.

Change-Id: I4977e8fe12d2cef116f8834c34cdf6fec618554f
---
gdb/amd64-tdep.c | 39 +++----------
gdb/i386-tdep.c | 146 +++++++++++++++++------------------------------
gdb/i386-tdep.h | 6 +-
gdb/value.c | 3 +-
gdb/value.h | 6 +-
5 files changed, 66 insertions(+), 134 deletions(-)

diff --git a/gdb/amd64-tdep.c b/gdb/amd64-tdep.c
index a0b4986d5b6c..4b9dbbab66e8 100644
--- a/gdb/amd64-tdep.c
+++ b/gdb/amd64-tdep.c
@@ -380,9 +380,8 @@ amd64_pseudo_register_read_value (gdbarch *gdbarch, frame_info_ptr next_frame,
}

static void
-amd64_pseudo_register_write (struct gdbarch *gdbarch,
- struct regcache *regcache,
- int regnum, const gdb_byte *buf)
+amd64_pseudo_register_write (gdbarch *gdbarch, frame_info_ptr next_frame,
+ int regnum, gdb::array_view<const gdb_byte> buf)
{
i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);

@@ -393,41 +392,18 @@ amd64_pseudo_register_write (struct gdbarch *gdbarch,
if (gpnum >= AMD64_NUM_LOWER_BYTE_REGS)
{
gpnum -= AMD64_NUM_LOWER_BYTE_REGS;
- gdb_byte raw_buf[register_size (gdbarch, gpnum)];
-
- /* Read ... AH, BH, CH, DH. */
- regcache->raw_read (gpnum, raw_buf);
- /* ... Modify ... (always little endian). */
- memcpy (raw_buf + 1, buf, 1);
- /* ... Write. */
- regcache->raw_write (gpnum, raw_buf);
+ pseudo_to_raw_part (next_frame, buf, gpnum, 1);
}
else
- {
- gdb_byte raw_buf[register_size (gdbarch, gpnum)];
-
- /* Read ... */
- regcache->raw_read (gpnum, raw_buf);
- /* ... Modify ... (always little endian). */
- memcpy (raw_buf, buf, 1);
- /* ... Write. */
- regcache->raw_write (gpnum, raw_buf);
- }
+ pseudo_to_raw_part (next_frame, buf, gpnum, 0);
}
else if (i386_dword_regnum_p (gdbarch, regnum))
{
int gpnum = regnum - tdep->eax_regnum;
- gdb_byte raw_buf[register_size (gdbarch, gpnum)];
-
- /* Read ... */
- regcache->raw_read (gpnum, raw_buf);
- /* ... Modify ... (always little endian). */
- memcpy (raw_buf, buf, 4);
- /* ... Write. */
- regcache->raw_write (gpnum, raw_buf);
+ pseudo_to_raw_part (next_frame, buf, gpnum, 0);
}
else
- i386_pseudo_register_write (gdbarch, regcache, regnum, buf);
+ i386_pseudo_register_write (gdbarch, next_frame, regnum, buf);
}

/* Implement the 'ax_pseudo_register_collect' gdbarch method. */
@@ -3205,8 +3181,7 @@ amd64_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch,

set_gdbarch_pseudo_register_read_value (gdbarch,
amd64_pseudo_register_read_value);
- set_gdbarch_deprecated_pseudo_register_write (gdbarch,
- amd64_pseudo_register_write);
+ set_gdbarch_pseudo_register_write (gdbarch, amd64_pseudo_register_write);
set_gdbarch_ax_pseudo_register_collect (gdbarch,
amd64_ax_pseudo_register_collect);

diff --git a/gdb/i386-tdep.c b/gdb/i386-tdep.c
index 5f12f17bc5eb..6b4e16155fee 100644
--- a/gdb/i386-tdep.c
+++ b/gdb/i386-tdep.c
@@ -3391,26 +3391,6 @@ i386_pseudo_register_type (struct gdbarch *gdbarch, int regnum)
internal_error (_("invalid regnum"));
}

-/* Map a cooked register onto a raw register or memory. For the i386,
- the MMX registers need to be mapped onto floating point registers. */
-
-static int
-i386_mmx_regnum_to_fp_regnum (readable_regcache *regcache, int regnum)
-{
- gdbarch *arch = regcache->arch ();
- i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (arch);
- int mmxreg, fpreg;
- ULONGEST fstat;
- int tos;
-
- mmxreg = regnum - tdep->mm0_regnum;
- regcache->raw_read (I387_FSTAT_REGNUM (tdep), &fstat);
- tos = (fstat >> 11) & 0x7;
- fpreg = (mmxreg + tos) % 8;
-
- return (I387_ST0_REGNUM (tdep) + fpreg);
-}
-
/* Map a cooked register onto a raw register or memory. For the i386,
the MMX registers need to be mapped onto floating point registers. */

@@ -3537,115 +3517,92 @@ i386_pseudo_register_read_value (gdbarch *gdbarch, frame_info_ptr next_frame,
}

void
-i386_pseudo_register_write (struct gdbarch *gdbarch, struct regcache *regcache,
- int regnum, const gdb_byte *buf)
+i386_pseudo_register_write (gdbarch *gdbarch, frame_info_ptr next_frame,
+ const int pseudo_reg_num,
+ gdb::array_view<const gdb_byte> buf)
{
- gdb_byte raw_buf[I386_MAX_REGISTER_SIZE];
-
- if (i386_mmx_regnum_p (gdbarch, regnum))
+ if (i386_mmx_regnum_p (gdbarch, pseudo_reg_num))
{
- int fpnum = i386_mmx_regnum_to_fp_regnum (regcache, regnum);
+ int fpnum = i386_mmx_regnum_to_fp_regnum (next_frame, pseudo_reg_num);

- /* Read ... */
- regcache->raw_read (fpnum, raw_buf);
- /* ... Modify ... (always little endian). */
- memcpy (raw_buf, buf, register_size (gdbarch, regnum));
- /* ... Write. */
- regcache->raw_write (fpnum, raw_buf);
+ pseudo_to_raw_part (next_frame, buf, fpnum, 0);
}
else
{
i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);

- if (i386_bnd_regnum_p (gdbarch, regnum))
+ if (i386_bnd_regnum_p (gdbarch, pseudo_reg_num))
{
- ULONGEST upper, lower;
int size = builtin_type (gdbarch)->builtin_data_ptr->length ();
bfd_endian byte_order
= gdbarch_byte_order (current_inferior ()->arch ());

/* New values from input value. */
- regnum -= tdep->bnd0_regnum;
- lower = extract_unsigned_integer (buf, size, byte_order);
- upper = extract_unsigned_integer (buf + size, size, byte_order);
+ int reg_index = pseudo_reg_num - tdep->bnd0_regnum;
+ int raw_regnum = I387_BND0R_REGNUM (tdep) + reg_index;

- /* Fetching register buffer. */
- regcache->raw_read (I387_BND0R_REGNUM (tdep) + regnum,
- raw_buf);
+ value *bndr_value = value_of_register (raw_regnum, next_frame);
+ gdb::array_view<gdb_byte> bndr_view
+ = bndr_value->contents_writeable ();

- upper = ~upper;
+ /* Copy lower bytes directly. */
+ copy (buf.slice (0, size), bndr_view.slice (0, size));

- /* Set register bits. */
- memcpy (raw_buf, &lower, 8);
- memcpy (raw_buf + 8, &upper, 8);
+ /* Convert and then copy upper bytes. */
+ ULONGEST upper
+ = extract_unsigned_integer (buf.slice (size, size), byte_order);
+ upper = ~upper;
+ store_unsigned_integer (bndr_view.slice (8, size), byte_order,
+ upper);

- regcache->raw_write (I387_BND0R_REGNUM (tdep) + regnum, raw_buf);
+ put_frame_register (next_frame, raw_regnum, bndr_view);
}
- else if (i386_zmm_regnum_p (gdbarch, regnum))
+ else if (i386_zmm_regnum_p (gdbarch, pseudo_reg_num))
{
- regnum -= tdep->zmm0_regnum;
+ /* Which register is it, relative to zmm0. */
+ int reg_index_0 = pseudo_reg_num - tdep->zmm0_regnum;

- if (regnum < num_lower_zmm_regs)
- {
- /* Write lower 128bits. */
- regcache->raw_write (I387_XMM0_REGNUM (tdep) + regnum, buf);
- /* Write upper 128bits. */
- regcache->raw_write (I387_YMM0_REGNUM (tdep) + regnum, buf + 16);
- }
+ if (reg_index_0 < num_lower_zmm_regs)
+ pseudo_to_concat_raw (next_frame, buf,
+ I387_XMM0_REGNUM (tdep) + reg_index_0,
+ I387_YMM0_REGNUM (tdep) + reg_index_0,
+ tdep->zmm0h_regnum + reg_index_0);
else
{
- /* Write lower 128bits. */
- regcache->raw_write (I387_XMM16_REGNUM (tdep) + regnum
- - num_lower_zmm_regs, buf);
- /* Write upper 128bits. */
- regcache->raw_write (I387_YMM16H_REGNUM (tdep) + regnum
- - num_lower_zmm_regs, buf + 16);
+ /* Which register is it, relative to zmm16. */
+ int reg_index_16 = reg_index_0 - num_lower_zmm_regs;
+
+ pseudo_to_concat_raw (next_frame, buf,
+ I387_XMM16_REGNUM (tdep) + reg_index_16,
+ I387_YMM16H_REGNUM (tdep) + reg_index_16,
+ tdep->zmm0h_regnum + +reg_index_0);
}
- /* Write upper 256bits. */
- regcache->raw_write (tdep->zmm0h_regnum + regnum, buf + 32);
}
- else if (i386_ymm_regnum_p (gdbarch, regnum))
+ else if (i386_ymm_regnum_p (gdbarch, pseudo_reg_num))
{
- regnum -= tdep->ymm0_regnum;
+ int i = pseudo_reg_num - tdep->ymm0_regnum;

- /* ... Write lower 128bits. */
- regcache->raw_write (I387_XMM0_REGNUM (tdep) + regnum, buf);
- /* ... Write upper 128bits. */
- regcache->raw_write (tdep->ymm0h_regnum + regnum, buf + 16);
+ pseudo_to_concat_raw (next_frame, buf, I387_XMM0_REGNUM (tdep) + i,
+ tdep->ymm0h_regnum + i);
}
- else if (i386_ymm_avx512_regnum_p (gdbarch, regnum))
+ else if (i386_ymm_avx512_regnum_p (gdbarch, pseudo_reg_num))
{
- regnum -= tdep->ymm16_regnum;
+ int i = pseudo_reg_num - tdep->ymm16_regnum;

- /* ... Write lower 128bits. */
- regcache->raw_write (I387_XMM16_REGNUM (tdep) + regnum, buf);
- /* ... Write upper 128bits. */
- regcache->raw_write (tdep->ymm16h_regnum + regnum, buf + 16);
+ pseudo_to_concat_raw (next_frame, buf, I387_XMM16_REGNUM (tdep) + i,
+ tdep->ymm16h_regnum + i);
}
- else if (i386_word_regnum_p (gdbarch, regnum))
+ else if (i386_word_regnum_p (gdbarch, pseudo_reg_num))
{
- int gpnum = regnum - tdep->ax_regnum;
+ int gpnum = pseudo_reg_num - tdep->ax_regnum;

- /* Read ... */
- regcache->raw_read (gpnum, raw_buf);
- /* ... Modify ... (always little endian). */
- memcpy (raw_buf, buf, 2);
- /* ... Write. */
- regcache->raw_write (gpnum, raw_buf);
+ pseudo_to_raw_part (next_frame, buf, gpnum, 0);
}
- else if (i386_byte_regnum_p (gdbarch, regnum))
+ else if (i386_byte_regnum_p (gdbarch, pseudo_reg_num))
{
- int gpnum = regnum - tdep->al_regnum;
+ int gpnum = pseudo_reg_num - tdep->al_regnum;

- /* Read ... We read both lower and upper registers. */
- regcache->raw_read (gpnum % 4, raw_buf);
- /* ... Modify ... (always little endian). */
- if (gpnum >= 4)
- memcpy (raw_buf + 1, buf, 1);
- else
- memcpy (raw_buf, buf, 1);
- /* ... Write. */
- regcache->raw_write (gpnum % 4, raw_buf);
+ pseudo_to_raw_part (next_frame, buf, gpnum % 4, gpnum >= 4 ? 1 : 0);
}
else
internal_error (_("invalid regnum"));
@@ -8603,8 +8560,7 @@ i386_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
/* Pseudo registers may be changed by amd64_init_abi. */
set_gdbarch_pseudo_register_read_value (gdbarch,
i386_pseudo_register_read_value);
- set_gdbarch_deprecated_pseudo_register_write (gdbarch,
- i386_pseudo_register_write);
+ set_gdbarch_pseudo_register_write (gdbarch, i386_pseudo_register_write);
set_gdbarch_ax_pseudo_register_collect (gdbarch,
i386_ax_pseudo_register_collect);

diff --git a/gdb/i386-tdep.h b/gdb/i386-tdep.h
index 970dc8904f2a..b132da23a104 100644
--- a/gdb/i386-tdep.h
+++ b/gdb/i386-tdep.h
@@ -380,9 +380,9 @@ extern value *i386_pseudo_register_read_value (gdbarch *gdbarch,
frame_info_ptr next_frame,
int regnum);

-extern void i386_pseudo_register_write (struct gdbarch *gdbarch,
- struct regcache *regcache,
- int regnum, const gdb_byte *buf);
+extern void i386_pseudo_register_write (gdbarch *gdbarch,
+ frame_info_ptr next_frame, int regnum,
+ gdb::array_view<const gdb_byte> buf);

extern int i386_ax_pseudo_register_collect (struct gdbarch *gdbarch,
struct agent_expr *ax,
diff --git a/gdb/value.c b/gdb/value.c
index 51dca972a587..5c18bb17a781 100644
--- a/gdb/value.c
+++ b/gdb/value.c
@@ -4062,7 +4062,8 @@ pseudo_to_raw_part (frame_info_ptr next_frame,
gdb::array_view<const gdb_byte> pseudo_buf,
int raw_reg_num, int raw_offset)
{
- int raw_reg_size = register_size (get_frame_arch (next_frame), raw_reg_num);
+ int raw_reg_size
+ = register_size (frame_unwind_arch (next_frame), raw_reg_num);

/* When overflowing a register, put_frame_register_bytes writes to the
subsequent registers. We don't want that behavior here, so make sure
diff --git a/gdb/value.h b/gdb/value.h
index 6a74d4e2c2ee..3b3cb9c00982 100644
--- a/gdb/value.h
+++ b/gdb/value.h
@@ -1664,7 +1664,7 @@ value *pseudo_from_raw_part (frame_info_ptr next_frame, int pseudo_reg_num,
/* Write PSEUDO_BUF, the contents of a pseudo register, to part of raw register
RAW_REG_NUM starting at RAW_OFFSET. */

-void pseudo_to_raw_part (frame_info_ptr this_frame,
+void pseudo_to_raw_part (frame_info_ptr next_frame,
gdb::array_view<const gdb_byte> pseudo_buf,
int raw_reg_num, int raw_offset);

@@ -1680,7 +1680,7 @@ value *pseudo_from_concat_raw (frame_info_ptr next_frame, int pseudo_reg_num,
/* Write PSEUDO_BUF, the contents of a pseudo register, to the two raw registers
RAW_REG_1_NUM and RAW_REG_2_NUM. */

-void pseudo_to_concat_raw (frame_info_ptr this_frame,
+void pseudo_to_concat_raw (frame_info_ptr next_frame,
gdb::array_view<const gdb_byte> pseudo_buf,
int raw_reg_1_num, int raw_reg_2_num);

@@ -1693,7 +1693,7 @@ value *pseudo_from_concat_raw (frame_info_ptr next_frame, int pseudo_reg_num,
/* Write PSEUDO_BUF, the contents of a pseudo register, to the tthreewo raw
registers RAW_REG_1_NUM, RAW_REG_2_NUM and RAW_REG_3_NUM. */

-void pseudo_to_concat_raw (frame_info_ptr this_frame,
+void pseudo_to_concat_raw (frame_info_ptr next_frame,
gdb::array_view<const gdb_byte> pseudo_buf,
int raw_reg_1_num, int raw_reg_2_num,
int raw_reg_3_num);
--
2.42.1