diff options
author | Tom Tromey <tromey@adacore.com> | 2023-03-01 12:58:11 -0700 |
---|---|---|
committer | Tom Tromey <tromey@adacore.com> | 2023-03-27 08:20:29 -0600 |
commit | 1c805ba063dd4531925e85eb603dd8b78a4821d2 (patch) | |
tree | 3dba8f2ab3bbc787a1d75fc981c9e9a530686ad1 /gdb/gmp-utils.h | |
parent | c7c3708ac778625d3a87aad541de5f0666acbcc5 (diff) | |
download | gdb-1c805ba063dd4531925e85eb603dd8b78a4821d2.zip gdb-1c805ba063dd4531925e85eb603dd8b78a4821d2.tar.gz gdb-1c805ba063dd4531925e85eb603dd8b78a4821d2.tar.bz2 |
Add truncation mode to gdb_mpz
This renames gdb_mpz::safe_export to export_bits, and adds a new flag
to export a truncated value. This is needed by value arithmetic.
Diffstat (limited to 'gdb/gmp-utils.h')
-rw-r--r-- | gdb/gmp-utils.h | 29 |
1 files changed, 22 insertions, 7 deletions
diff --git a/gdb/gmp-utils.h b/gdb/gmp-utils.h index 923af01..f294ab6 100644 --- a/gdb/gmp-utils.h +++ b/gdb/gmp-utils.h @@ -137,7 +137,20 @@ struct gdb_mpz UNSIGNED_P indicates whether the number has an unsigned type. */ void write (gdb::array_view<gdb_byte> buf, enum bfd_endian byte_order, - bool unsigned_p) const; + bool unsigned_p) const + { + export_bits (buf, byte_order == BFD_ENDIAN_BIG ? 1 : -1 /* endian */, + unsigned_p, true /* safe */); + } + + /* Like write, but truncates the value to the desired number of + bytes. */ + void truncate (gdb::array_view<gdb_byte> buf, enum bfd_endian byte_order, + bool unsigned_p) const + { + export_bits (buf, byte_order == BFD_ENDIAN_BIG ? 1 : -1 /* endian */, + unsigned_p, false /* safe */); + } /* Return a string containing VAL. */ std::string str () const { return gmp_string_printf ("%Zd", m_val); } @@ -337,10 +350,11 @@ private: . -1 for least significant byte first; or . 0 for native endianness. - An error is raised if BUF is not large enough to contain the value - being exported. */ - void safe_export (gdb::array_view<gdb_byte> buf, - int endian, bool unsigned_p) const; + If SAFE is true, an error is raised if BUF is not large enough to + contain the value being exported. If SAFE is false, the value is + truncated to fit in BUF. */ + void export_bits (gdb::array_view<gdb_byte> buf, int endian, bool unsigned_p, + bool safe) const; friend struct gdb_mpq; friend struct gdb_mpf; @@ -590,9 +604,10 @@ gdb_mpz::as_integer () const { T result; - this->safe_export ({(gdb_byte *) &result, sizeof (result)}, + this->export_bits ({(gdb_byte *) &result, sizeof (result)}, 0 /* endian (0 = native) */, - !std::is_signed<T>::value /* unsigned_p */); + !std::is_signed<T>::value /* unsigned_p */, + true /* safe */); return result; } |