Re: [PATCH 3/11] Add MIPS_MAX_REGISTER_SIZE (2/4)

Yao Qi
2017-05-12T08:53:54Z

Alan Hayward <Alan.Hayward@arm.com> writes:

> }
>
> +/* Supply register REGNUM, whose contents are stored in signed VAL, to
> + REGCACHE. */
> +
> +void
> +regcache::raw_supply_signed (int regnum, LONGEST val)

The unsigned version of this function is also needed, because I see such
pattern also exists,

store_unsigned_integer (buf, 8, byte_order, sp + offset);
regcache_raw_supply (regcache, AMD64_RSP_REGNUM, buf);

this leads me thinking we need to use function template to define
functions for both LONGEST and ULONGEST.

Secondly, this method can be named as raw_supply.

> +{
> + enum bfd_endian byte_order = gdbarch_byte_order (m_descr->gdbarch);
> + gdb_byte *regbuf;
> + size_t size;
> +
> + gdb_assert (regnum >= 0 && regnum < m_descr->nr_raw_registers);
> + gdb_assert (!m_readonly_p);
> +
> + regbuf = register_buffer (regnum);
> + size = m_descr->sizeof_register[regnum];
> +
> + store_signed_integer (regbuf, size, byte_order, val);
> + m_register_status[regnum] = REG_VALID;
> +}
> +
> /* Collect register REGNUM from REGCACHE and store its contents in BUF. */
>
> void
> @@ -1251,6 +1271,23 @@ regcache::raw_collect (int regnum, void *buf) const
> memcpy (buf, regbuf, size);
> }
>
> +/* Collect register REGNUM from REGCACHE and extract its contents into a signed

This line is too long.

> + LONGEST. */
> +
> +LONGEST
> +regcache::raw_collect_signed (int regnum) const

We can define this method like this,

template<typename T>
using LongType = typename std::enable_if<(std::is_same<T, LONGEST>::value
|| std::is_same<T, ULONGEST>::value),
T>::type;

template<typename T>
LongType<T> raw_collect (int regnum) const
{
....
if (std::is_signed<T>::value)
return extract_signed_integer (regbuf, size, byte_order);
else
return extract_unsigned_integer (regbuf, size, byte_order);
}

> +{
> + enum bfd_endian byte_order = gdbarch_byte_order (m_descr->gdbarch);
> + const gdb_byte *regbuf;
> + size_t size;
> +
> + gdb_assert (regnum >= 0 && regnum < m_descr->nr_raw_registers);
> +
> + regbuf = register_buffer (regnum);
> + size = m_descr->sizeof_register[regnum];
> + return extract_signed_integer (regbuf, size, byte_order);
> +}

If you want, we can add a function template for extract_signed_integer
and extract_unsigned_integer.

template<typename T>
LongType<T>
extract_integer (const gdb_byte *addr, int len, enum bfd_endian byte_order)
{
T retval;

...
if (std::is_signed<T>::value)
{
...
}
else
{
...
}
return retval;
}

so, the raw_collect above becomes,

template<typename T>
LongType<T> raw_collect (int regnum) const
{
....
return extract_integer<T> (regbuf, size, byte_order);
}

and raw_read_{signed,unsigned}, cooked_read_{signed,unsigned} can be
merged as function template too.

--
Yao (齐尧)