From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from rock.gnat.com (rock.gnat.com [IPv6:2620:20:4000:0:a9e:1ff:fe9b:1d1]) by sourceware.org (Postfix) with ESMTP id 34CBF3857802 for ; Sun, 15 Nov 2020 08:49:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 34CBF3857802 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=brobecke@adacore.com Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id 14EFC5604F; Sun, 15 Nov 2020 03:49:51 -0500 (EST) X-Virus-Scanned: Debian amavisd-new at gnat.com Received: from rock.gnat.com ([127.0.0.1]) by localhost (rock.gnat.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id n0wSdroehY7d; Sun, 15 Nov 2020 03:49:51 -0500 (EST) Received: from tron.gnat.com (tron.gnat.com [205.232.38.10]) by rock.gnat.com (Postfix) with ESMTP id 019545604D; Sun, 15 Nov 2020 03:49:51 -0500 (EST) Received: by tron.gnat.com (Postfix, from userid 4233) id 004C9111; Sun, 15 Nov 2020 03:49:50 -0500 (EST) From: Joel Brobecker To: gdb-patches@sourceware.org Cc: Simon Marchi , Joel Brobecker Subject: [RFA 2/6] gmp-utils: Convert the read/write methods to using gdb::array_view Date: Sun, 15 Nov 2020 03:49:40 -0500 Message-Id: <1605430184-81335-3-git-send-email-brobecker@adacore.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1605430184-81335-1-git-send-email-brobecker@adacore.com> References: <1604817017-25807-1-git-send-email-brobecker@adacore.com> <1605430184-81335-1-git-send-email-brobecker@adacore.com> X-Spam-Status: No, score=-10.0 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 15 Nov 2020 08:49:53 -0000 This commit changes the interfaces of some of the methods declared in gmp-utils to take a gdb::array_view of gdb_byte instead of a (gdb_byte *, size) couple. This makes these methods' API probably more C++-idiomatic. With the way things are structured, this change introduces a minor extra complication at the point of call of these methods, since the data available there is not in the form of an array_view, and thus the array_view needs to be constructed on the spot. * gmp-utils.h (gdb_mpz::read): Change buf and len parameters into one single gdb::array_view parameter. (gdb_mpz::write): Likewise. (gdb_mpq::read_fixed_point, gdb_mpq::write_fixed_point): Likewise. * gmp-utils.c (gdb_mpz::read): Change buf and len parameters into one single gdb::array_view parameter. Adjust implementation accordingly. (gdb_mpz::write): Likewise. (gdb_mpq::read_fixed_point, gdb_mpq::write_fixed_point): Likewise. * unittests/gmp-utils-selftests.c: Adapt following changes above. * valarith.c, valops.c, valprint.c, value.c: Likewise. Change-Id: Ia6e9f077def06b92e089684164066fc81fff5e29 --- gdb/gmp-utils.c | 25 +++++++++++++------------ gdb/gmp-utils.h | 32 ++++++++++++++++++-------------- gdb/unittests/gmp-utils-selftests.c | 12 ++++++++---- gdb/valarith.c | 9 ++++++--- gdb/valops.c | 11 +++++++---- gdb/valprint.c | 3 ++- gdb/value.c | 3 ++- 7 files changed, 56 insertions(+), 39 deletions(-) diff --git a/gdb/gmp-utils.c b/gdb/gmp-utils.c index b70aaa3..6d80f13 100644 --- a/gdb/gmp-utils.c +++ b/gdb/gmp-utils.c @@ -37,12 +37,12 @@ gmp_string_asprintf (const char *fmt, ...) /* See gmp-utils.h. */ void -gdb_mpz::read (const gdb_byte *buf, int len, enum bfd_endian byte_order, +gdb_mpz::read (gdb::array_view buf, enum bfd_endian byte_order, bool unsigned_p) { - mpz_import (val, 1 /* count */, -1 /* order */, len /* size */, + mpz_import (val, 1 /* count */, -1 /* order */, buf.size () /* size */, byte_order == BFD_ENDIAN_BIG ? 1 : -1 /* endian */, - 0 /* nails */, buf /* op */); + 0 /* nails */, buf.data () /* op */); if (!unsigned_p) { @@ -51,7 +51,7 @@ gdb_mpz::read (const gdb_byte *buf, int len, enum bfd_endian byte_order, was in fact negative, we need to adjust VAL accordingly. */ gdb_mpz max; - mpz_ui_pow_ui (max.val, 2, len * TARGET_CHAR_BIT - 1); + mpz_ui_pow_ui (max.val, 2, buf.size () * TARGET_CHAR_BIT - 1); if (mpz_cmp (val, max.val) >= 0) mpz_submul_ui (val, max.val, 2); } @@ -60,7 +60,7 @@ gdb_mpz::read (const gdb_byte *buf, int len, enum bfd_endian byte_order, /* See gmp-utils.h. */ void -gdb_mpz::write (gdb_byte *buf, int len, enum bfd_endian byte_order, +gdb_mpz::write (gdb::array_view buf, enum bfd_endian byte_order, bool unsigned_p) const { gdb_mpz exported_val (val); @@ -72,14 +72,15 @@ gdb_mpz::write (gdb_byte *buf, int len, enum bfd_endian byte_order, would be the same as our negative value. */ gdb_mpz neg_offset; - mpz_ui_pow_ui (neg_offset.val, 2, len * TARGET_CHAR_BIT); + mpz_ui_pow_ui (neg_offset.val, 2, buf.size () * TARGET_CHAR_BIT); mpz_add (exported_val.val, exported_val.val, neg_offset.val); } /* Start by clearing the buffer, as mpz_export only writes as many bytes as it needs (including none, if the value to export is zero. */ - memset (buf, 0, len); - mpz_export (buf, NULL /* count */, -1 /* order */, len /* size */, + memset (buf.data (), 0, buf.size ()); + mpz_export (buf.data (), NULL /* count */, -1 /* order */, + buf.size () /* size */, byte_order == BFD_ENDIAN_BIG ? 1 : -1 /* endian */, 0 /* nails */, exported_val.val); } @@ -120,12 +121,12 @@ gdb_mpq::get_rounded () const /* See gmp-utils.h. */ void -gdb_mpq::read_fixed_point (const gdb_byte *buf, int len, +gdb_mpq::read_fixed_point (gdb::array_view buf, enum bfd_endian byte_order, bool unsigned_p, const gdb_mpq &scaling_factor) { gdb_mpz vz; - vz.read (buf, len, byte_order, unsigned_p); + vz.read (buf, byte_order, unsigned_p); mpq_set_z (val, vz.val); mpq_mul (val, val, scaling_factor.val); @@ -134,7 +135,7 @@ gdb_mpq::read_fixed_point (const gdb_byte *buf, int len, /* See gmp-utils.h. */ void -gdb_mpq::write_fixed_point (gdb_byte *buf, int len, +gdb_mpq::write_fixed_point (gdb::array_view buf, enum bfd_endian byte_order, bool unsigned_p, const gdb_mpq &scaling_factor) const { @@ -143,7 +144,7 @@ gdb_mpq::write_fixed_point (gdb_byte *buf, int len, mpq_div (unscaled.val, unscaled.val, scaling_factor.val); gdb_mpz unscaled_z = unscaled.get_rounded (); - unscaled_z.write (buf, len, byte_order, unsigned_p); + unscaled_z.write (buf, byte_order, unsigned_p); } /* A wrapper around xrealloc that we can then register with GMP diff --git a/gdb/gmp-utils.h b/gdb/gmp-utils.h index 7eed29a..ac6bb0d 100644 --- a/gdb/gmp-utils.h +++ b/gdb/gmp-utils.h @@ -96,17 +96,19 @@ struct gdb_mpz The return type can signed or unsigned, with no size restriction. */ template T as_integer () const; - /* Set VAL by importing the number stored in the byte buffer (BUF), - given its size (LEN) and BYTE_ORDER. + /* Set VAL by importing the number stored in the byte array (BUF), + using the given BYTE_ORDER. The size of the data to read is + the byte array's size. UNSIGNED_P indicates whether the number has an unsigned type. */ - void read (const gdb_byte *buf, int len, enum bfd_endian byte_order, + void read (gdb::array_view buf, enum bfd_endian byte_order, bool unsigned_p); - /* Write VAL into BUF as a LEN-bytes number with the given BYTE_ORDER. + /* Write VAL into BUF as a number whose byte size is the size of BUF, + using the given BYTE_ORDER. UNSIGNED_P indicates whether the number has an unsigned type. */ - void write (gdb_byte *buf, int len, enum bfd_endian byte_order, + void write (gdb::array_view buf, enum bfd_endian byte_order, bool unsigned_p) const; /* Return a string containing VAL. */ @@ -167,24 +169,26 @@ struct gdb_mpq /* Return VAL rounded to the nearest integer. */ gdb_mpz get_rounded () const; - /* Set VAL from the contents of the given buffer (BUF), which - contains the unscaled value of a fixed point type object - with the given size (LEN) and byte order (BYTE_ORDER). + /* Set VAL from the contents of the given byte array (BUF), which + contains the unscaled value of a fixed point type object. + The byte size of the data is the size of BUF. + + BYTE_ORDER provides the byte_order to use when reading the data. UNSIGNED_P indicates whether the number has an unsigned type. SCALING_FACTOR is the scaling factor to apply after having read the unscaled value from our buffer. */ - void read_fixed_point (const gdb_byte *buf, int len, + void read_fixed_point (gdb::array_view buf, enum bfd_endian byte_order, bool unsigned_p, const gdb_mpq &scaling_factor); - /* Write VAL into BUF as a LEN-bytes fixed point value following - the given BYTE_ORDER. + /* Write VAL into BUF as fixed point value following the given BYTE_ORDER. + The size of BUF is used as the length to write the value into. UNSIGNED_P indicates whether the number has an unsigned type. SCALING_FACTOR is the scaling factor to apply before writing the unscaled value to our buffer. */ - void write_fixed_point (gdb_byte *buf, int len, + void write_fixed_point (gdb::array_view buf, enum bfd_endian byte_order, bool unsigned_p, const gdb_mpq &scaling_factor) const; @@ -213,13 +217,13 @@ struct gdb_mpf UNSIGNED_P indicates whether the number has an unsigned type. SCALING_FACTOR is the scaling factor to apply after having read the unscaled value from our buffer. */ - void read_fixed_point (const gdb_byte *buf, int len, + void read_fixed_point (gdb::array_view buf, enum bfd_endian byte_order, bool unsigned_p, const gdb_mpq &scaling_factor) { gdb_mpq tmp_q; - tmp_q.read_fixed_point (buf, len, byte_order, unsigned_p, scaling_factor); + tmp_q.read_fixed_point (buf, byte_order, unsigned_p, scaling_factor); mpf_set_q (val, tmp_q.val); } diff --git a/gdb/unittests/gmp-utils-selftests.c b/gdb/unittests/gmp-utils-selftests.c index e8c3c5c..933d523 100644 --- a/gdb/unittests/gmp-utils-selftests.c +++ b/gdb/unittests/gmp-utils-selftests.c @@ -109,7 +109,8 @@ store_and_read_back (T val, int buf_len, enum bfd_endian byte_order, mpz_set (actual.val, expected.val); mpz_sub_ui (actual.val, actual.val, 500); - actual.read (buf, buf_len, byte_order, !std::is_signed::value); + actual.read (gdb::array_view (buf, buf_len), + byte_order, !std::is_signed::value); } /* Test the gdb_mpz::read method over a reasonable range of values. @@ -234,7 +235,8 @@ write_and_extract (T val, int buf_len, enum bfd_endian byte_order) SELF_CHECK (v.as_integer () == val); gdb_byte *buf = (gdb_byte *) alloca (buf_len); - v.write (buf, buf_len, byte_order, !std::is_signed::value); + v.write (gdb::array_view (buf, buf_len), + byte_order, !std::is_signed::value); return extract_integer (buf, buf_len, byte_order); } @@ -333,7 +335,8 @@ read_fp_test (int unscaled, const gdb_mpq &scaling_factor, gdb_byte buf[len]; store_signed_integer (buf, len, byte_order, unscaled); - actual.read_fixed_point (buf, len, byte_order, 0, scaling_factor); + actual.read_fixed_point (gdb::array_view (buf, len), + byte_order, 0, scaling_factor); mpq_set_si (expected.val, unscaled, 1); mpq_mul (expected.val, expected.val, scaling_factor.val); @@ -402,7 +405,8 @@ write_fp_test (int numerator, unsigned int denominator, gdb_mpq v; mpq_set_ui (v.val, numerator, denominator); mpq_canonicalize (v.val); - v.write_fixed_point (buf, len, byte_order, 0, scaling_factor); + v.write_fixed_point (gdb::array_view (buf, len), + byte_order, 0, scaling_factor); return extract_unsigned_integer (buf, len, byte_order); } diff --git a/gdb/valarith.c b/gdb/valarith.c index f4497cd..8c47249 100644 --- a/gdb/valarith.c +++ b/gdb/valarith.c @@ -908,10 +908,12 @@ fixed_point_binop (struct value *arg1, struct value *arg2, enum exp_opcode op) } gdb_mpq v1, v2, res; - v1.read_fixed_point (value_contents (arg1), TYPE_LENGTH (type1), + v1.read_fixed_point (gdb::array_view (value_contents (arg1), + TYPE_LENGTH (type1)), type_byte_order (type1), type1->is_unsigned (), fixed_point_scaling_factor (type1)); - v2.read_fixed_point (value_contents (arg2), TYPE_LENGTH (type2), + v2.read_fixed_point (gdb::array_view (value_contents (arg2), + TYPE_LENGTH (type2)), type_byte_order (type2), type2->is_unsigned (), fixed_point_scaling_factor (type2)); @@ -919,7 +921,8 @@ fixed_point_binop (struct value *arg1, struct value *arg2, enum exp_opcode op) do { \ val = allocate_value (type1); \ (RESULT).write_fixed_point \ - (value_contents_raw (val), TYPE_LENGTH (type1), \ + (gdb::array_view (value_contents_raw (val), \ + TYPE_LENGTH (type1)), \ type_byte_order (type1), type1->is_unsigned (), \ fixed_point_scaling_factor (type1)); \ } while (0) diff --git a/gdb/valops.c b/gdb/valops.c index 0f84a70..b4c6d88 100644 --- a/gdb/valops.c +++ b/gdb/valops.c @@ -357,7 +357,8 @@ value_cast_to_fixed_point (struct type *to_type, struct value *from_val) { gdb_mpz vz; - vz.read (value_contents (from_val), TYPE_LENGTH (from_type), + vz.read (gdb::array_view (value_contents (from_val), + TYPE_LENGTH (from_type)), type_byte_order (from_type), from_type->is_unsigned ()); mpq_set_z (vq.val, vz.val); @@ -378,8 +379,9 @@ value_cast_to_fixed_point (struct type *to_type, struct value *from_val) /* Finally, create the result value, and pack the unscaled value in it. */ struct value *result = allocate_value (to_type); - unscaled.write (value_contents_raw (result), - TYPE_LENGTH (to_type), type_byte_order (to_type), + unscaled.write (gdb::array_view (value_contents_raw (result), + TYPE_LENGTH (to_type)), + type_byte_order (to_type), to_type->is_unsigned ()); return result; @@ -523,7 +525,8 @@ value_cast (struct type *type, struct value *arg2) gdb_mpq fp_val; fp_val.read_fixed_point - (value_contents (arg2), TYPE_LENGTH (type2), + (gdb::array_view (value_contents (arg2), + TYPE_LENGTH (type2)), type_byte_order (type2), type2->is_unsigned (), fixed_point_scaling_factor (type2)); diff --git a/gdb/valprint.c b/gdb/valprint.c index abef002..21a4bf4 100644 --- a/gdb/valprint.c +++ b/gdb/valprint.c @@ -809,7 +809,8 @@ generic_val_print_fixed_point (struct value *val, struct ui_file *stream, const gdb_byte *valaddr = value_contents_for_printing (val); gdb_mpf f; - f.read_fixed_point (valaddr, TYPE_LENGTH (type), + f.read_fixed_point (gdb::array_view (valaddr, + TYPE_LENGTH (type)), type_byte_order (type), type->is_unsigned (), fixed_point_scaling_factor (type)); diff --git a/gdb/value.c b/gdb/value.c index a0546af..7444e79 100644 --- a/gdb/value.c +++ b/gdb/value.c @@ -2812,7 +2812,8 @@ unpack_long (struct type *type, const gdb_byte *valaddr) case TYPE_CODE_FIXED_POINT: { gdb_mpq vq; - vq.read_fixed_point (valaddr, len, byte_order, nosign, + vq.read_fixed_point (gdb::array_view (valaddr, len), + byte_order, nosign, fixed_point_scaling_factor (type)); gdb_mpz vz; -- 2.1.4