public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Simon Marchi <simark@simark.ca>
To: Joel Brobecker <brobecker@adacore.com>,
	GDB patches <gdb-patches@sourceware.org>
Subject: Re: [PATCH 5/9] Add support for printing value of DWARF-based fixed-point type objects
Date: Tue, 10 Nov 2020 16:06:32 -0500	[thread overview]
Message-ID: <2d12525a-3a85-3f69-bfea-22166f7fd358@simark.ca> (raw)
In-Reply-To: <1604817017-25807-6-git-send-email-brobecker@adacore.com>

[Sorry Joel, I failed to reply-all, so I am sending it again including
 gdb-patches]

I did not do an in-depth review of the functionality, because I don't
have that much time right now (and coming from AdaCore I trust that it's
quite well tested), so the comments are mostly coding-style related.

> This commit introduces a new kind of type, meant to describe
> fixed-point types, using a new code added specifically for
> this purpose (TYPE_CODE_FIXED_POINT).
>
> It then adds handling of fixed-point base types in the DWARF reader.
>
> And finally, as a first step, this commit adds support for printing
> the value of fixed-point type objects.
>
> Note that this commit has a known issue: Trying to print the value
> of a fixed-point object with a format letter (e.g. "print /x NAME")
> causes the wrong value to be printed because the scaling factor
> is not applied. Since the fix for this issue is isolated, and
> this is not a regression, the fix will be made in a pach of its own.
> This is meant to simplify review and archeology.
>
> Also, other functionalities related to fixed-point type handling
> (ptype, arithmetics, etc), will be added piecemeal as well, for
> the same reasons (faciliate reviews and archeology). Related to this,
> the testcase gdb.ada/fixed_cmp.exp is adjusted to compile the test
> program with -fgnat-encodings=all, so as to force the use of GNAT
> encodings, rather than rely on the compiler's default to use them.
> The intent is to enhance this testcase to also test the pure DWARF
> approach using -fgnat-encodings=minimal as soon as the corresponding
> suport gets added in. Thus, the modification to the testcase is made
> in a way that it prepares this testcase to be tested in both modes.

Since it's an easy change to do, I'd suggest to change NULL for nullptr
in all the new code.  Also, declare variable on first use when possible.

> diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
> index dbf0a3e..1f5152d 100644
> --- a/gdb/dwarf2/read.c
> +++ b/gdb/dwarf2/read.c
> @@ -18130,6 +18130,157 @@ read_typedef (struct die_info *die, struct dwarf2_cu *cu)
>    return this_type;
>  }
>
> +/* Assuming DIE is a rational DW_TAG_constant, read the DIE's
> +   numerator and denominator into NUMERATOR and DENOMINATOR (resp).
> +
> +   If the numerator and/or numerator attribute is missing,
> +   a complaint is filed, and NUMERATOR and DENOMINATOR are left
> +   untouched.  */
> +
> +static void
> +get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
> +			      LONGEST *numerator, LONGEST *denominator)
> +{
> +  struct attribute *num_attr, *denom_attr;
> +
> +  num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
> +  if (num_attr == NULL)
> +    complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
> +	       dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
> +
> +  denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
> +  if (denom_attr == NULL)
> +    complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
> +	       dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
> +
> +  if (num_attr == NULL || denom_attr == NULL)
> +    return;
> +
> +  *numerator = num_attr->constant_value (1);
> +  *denominator = denom_attr->constant_value (1);
> +}
> +
> +/* Same as get_dwarf2_rational_constant, but extracting an unsigned
> +   rational constant, rather than a signed one.
> +
> +   If the rational constant is has a negative value, a complaint

"is has"

> +   is filed, and NUMERATOR and DENOMINATOR are left untouched.  */
> +
> +static void
> +get_dwarf2_unsigned_rational_constant (struct die_info *die,
> +				       struct dwarf2_cu *cu,
> +				       ULONGEST *numerator,
> +				       ULONGEST *denominator)
> +{
> +  LONGEST num = 1, denom = 1;
> +
> +  get_dwarf2_rational_constant (die, cu, &num, &denom);
> +  if (num < 0 && denom < 0)
> +    {
> +      num = -num;
> +      denom = -denom;
> +    }
> +  else if (num < 0)
> +    {
> +      complaint (_("unexpected negative value for DW_AT_GNU_numerator"
> +		   " in DIE at %s"),
> +		 sect_offset_str (die->sect_off));
> +      return;
> +    }
> +  else if (denom < 0)
> +    {
> +      complaint (_("unexpected negative value for DW_AT_GNU_denominator"
> +		   " in DIE at %s"),
> +		 sect_offset_str (die->sect_off));
> +      return;
> +    }
> +
> +  *numerator = num;
> +  *denominator = denom;
> +}
> +
> +/* Assiuming DIE corresponds to a fixed point type, finish the creation

"Assiuming"

> +   of the corresponding TYPE by setting its TYPE_FIXED_POINT_INFO.
> +   CU is the DIE's CU.  */
> +
> +static void
> +finish_fixed_point_type (struct type *type, struct die_info *die,
> +			 struct dwarf2_cu *cu)

Unless there's a good reason not to (coming up in the following
patches), I would make this function create the "struct type", instead
of having the caller create it and pass it.  In other words, this
signature:

struct type *create_fixed_point_type (die_info *die, dwarf2_cu *cu)

That just makes it more obvious that it's a simple "die" to "type"
transform.

> +{
> +  struct attribute *attr;
> +  /* Numerator and denominator of our fixed-point type's scaling factor.
> +     The default is a scaling factor of 1, which we use as a fallback
> +     when we are not able to decode it (problem with the debugging info,
> +     unsupported forms, bug in GDB, etc...).  Using that as the default
> +     allows us to at least print the unscaled value, which might still
> +     be useful to a user.  */
> +  ULONGEST scale_num = 1;
> +  ULONGEST scale_denom = 1;
> +
> +  gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
> +	      && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
> +
> +  attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
> +  if (!attr)
> +    attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
> +  if (!attr)
> +    attr = dwarf2_attr (die, DW_AT_small, cu);
> +
> +  if (attr == NULL)
> +    {
> +      /* Scaling factor not found.  Assume a scaling factor of 1,
> +	 and hope for the best.  At least the user will be able to see
> +	 the encoded value.  */
> +      complaint (_("no scale found for fixed-point type (DIE at %s)"),
> +		 sect_offset_str (die->sect_off));
> +    }
> +  else if (attr->name == DW_AT_binary_scale)
> +    {
> +      LONGEST scale_exp = attr->constant_value (0);
> +      ULONGEST *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
> +
> +      *num_or_denom = 1 << abs (scale_exp);
> +    }
> +  else if (attr->name == DW_AT_decimal_scale)
> +    {
> +      LONGEST scale_exp = attr->constant_value (0);
> +      ULONGEST *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
> +
> +      *num_or_denom = uinteger_pow (10, abs (scale_exp));
> +    }
> +  else if (attr->name == DW_AT_small)
> +    {
> +      struct die_info *scale_die;
> +      struct dwarf2_cu *scale_cu = cu;
> +
> +      scale_die = follow_die_ref (die, attr, &scale_cu);
> +      if (scale_die->tag == DW_TAG_constant)
> +	get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
> +					       &scale_num, &scale_denom);
> +      else
> +	complaint (_("%s DIE not supported as target of DW_AT_small attribute"
> +		     " (DIE at %s)"),
> +		   dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
> +    }
> +  else
> +    {
> +      complaint (("unsupported scale attribute %s for fixed-point type"
> +		   " (DIE at %s)"),

Missing _()?  Also, the second line has one space too much.

> @@ -1821,6 +1845,9 @@ extern void set_type_vptr_basetype (struct type *, struct type *);
>    (TYPE_CPLUS_SPECIFIC(thistype)->virtual_field_bits == NULL ? 0 \
>      : B_TST(TYPE_CPLUS_SPECIFIC(thistype)->virtual_field_bits, (index)))
>
> +#define TYPE_FIXED_POINT_INFO(thistype) \
> +  (TYPE_MAIN_TYPE(thistype)->type_specific.fixed_point_info)

Do you think you can make this macro a method on struct type in the
style of the ones that were added recently?  It will be one less macro
to convert later :).  That method should contain an assert that the type
code is indeed TYPE_CODE_FIXED_POINT.  I think it could return a
`fixed_point_info &`, since there's no way it can fail and need to
return nullptr (now I see that type::bounds should probably return a
`range_bounds &` instead of a `range_bounds *`).

> +
>  #define FIELD_NAME(thisfld) ((thisfld).name)
>  #define FIELD_LOC_KIND(thisfld) ((thisfld).loc_kind)
>  #define FIELD_BITPOS_LVAL(thisfld) ((thisfld).loc.bitpos)
> @@ -2192,6 +2219,8 @@ extern struct type *init_decfloat_type (struct objfile *, int, const char *);
>  extern struct type *init_complex_type (const char *, struct type *);
>  extern struct type *init_pointer_type (struct objfile *, int, const char *,
>  				       struct type *);
> +extern struct type *init_fixed_point_type (struct objfile *, int, int,
> +					   const char *);
>
>  /* Helper functions to construct architecture-owned types.  */
>  extern struct type *arch_type (struct gdbarch *, enum type_code, int,
> @@ -2529,6 +2558,26 @@ extern int type_not_allocated (const struct type *type);
>
>  extern int type_not_associated (const struct type *type);
>
> +/* Return True if TYPE is a TYPE_CODE_FIXED_POINT or if TYPE is
> +   range whose base type is a TYPE_CODE_FIXED_POINT.  */
> +extern int is_fixed_point_type (struct type *type);

int -> bool

> +
> +/* Assuming that TYPE is a fixed point type, return its base type.
> +
> +   In other words, this returns the type after having peeled all
> +   intermediate type layers (such as TYPE_CODE_RANGE, for instance).
> +   The TYPE_CODE of the type returned is guaranteed to be
> +   a TYPE_CODE_FIXED_POINT.  */
> +extern struct type *fixed_point_type_base_type (struct type *type);

I think it would make sense to make this a `struct type` method.

> +
> +/* Given TYPE, which is a fixed point type, return its scaling factor.  */
> +extern const gdb_mpq &fixed_point_scaling_factor (struct type *type);

I think this would belong in fixed_point_type_info, so you'd access it
using:

  type->fixed_point_info ().scaling_factor ()

> diff --git a/gdb/testsuite/gdb.ada/fixed_cmp.exp b/gdb/testsuite/gdb.ada/fixed_cmp.exp
> index 38e41c4..10e2c9a 100644
> --- a/gdb/testsuite/gdb.ada/fixed_cmp.exp
> +++ b/gdb/testsuite/gdb.ada/fixed_cmp.exp
> @@ -19,25 +19,29 @@ if { [skip_ada_tests] } { return -1 }
>
>  standard_ada_testfile fixed
>
> -if {[gdb_compile_ada "${srcfile}" "${binfile}" executable [list debug ]] != "" } {
> -  return -1
> -}
> +foreach_with_prefix scenario {all} {

I suggest naming this variable gnat-encodings, just because the
resulting test name will be a bit clearer.

Simon

  reply	other threads:[~2020-11-10 21:06 UTC|newest]

Thread overview: 140+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-08  6:30 RFA: Add support for DWARF-based fixed point types Joel Brobecker
2020-11-08  6:30 ` [PATCH 1/9] gdb/configure: Add --with-libgmp-prefix option Joel Brobecker
2020-11-08  6:30 ` [PATCH 2/9] gdb: Make GMP a required dependency for building GDB Joel Brobecker
2020-12-15  6:55   ` Sebastian Huber
2020-12-15  8:57     ` Joel Brobecker
2020-11-08  6:30 ` [PATCH 3/9] gmp-utils: New API to simply use of GMP's integer/rational/float objects Joel Brobecker
2020-11-10 20:15   ` Simon Marchi
2020-11-13  8:12     ` Joel Brobecker
2020-11-13 15:04       ` Tom Tromey
2020-11-13 15:06         ` Simon Marchi
2020-11-16 16:18         ` Tom Tromey
2020-11-16 16:34   ` Luis Machado
2020-11-18  3:52     ` Joel Brobecker
2020-11-08  6:30 ` [PATCH 4/9] Move uinteger_pow gdb/valarith.c to gdb/utils.c and make it public Joel Brobecker
2020-11-08  6:30 ` [PATCH 5/9] Add support for printing value of DWARF-based fixed-point type objects Joel Brobecker
2020-11-10 21:06   ` Simon Marchi [this message]
2020-11-14 10:48     ` Joel Brobecker
2020-11-14 13:20       ` Simon Marchi
2020-11-14 11:30     ` Joel Brobecker
2020-11-14 16:14       ` Simon Marchi
2020-11-15  5:30         ` Joel Brobecker
2020-11-15  6:33     ` Joel Brobecker
2020-11-16  0:13       ` Simon Marchi
2020-11-08  6:30 ` [PATCH 6/9] fix printing of DWARF fixed-point type objects with format modifier Joel Brobecker
2020-11-10 22:50   ` Simon Marchi
2020-11-08  6:30 ` [PATCH 7/9] Add ptype support for DWARF-based fixed-point types Joel Brobecker
2020-11-10 23:00   ` Simon Marchi
2020-11-15  6:57     ` Joel Brobecker
2020-11-15  7:09       ` Joel Brobecker
2020-11-16  0:16         ` Simon Marchi
2020-11-16  4:03           ` Joel Brobecker
2020-11-08  6:30 ` [PATCH 8/9] Add support for fixed-point type arithmetic Joel Brobecker
2020-11-10 23:18   ` Simon Marchi
2020-11-08  6:30 ` [PATCH 9/9] Add support for fixed-point type comparison operators Joel Brobecker
2020-11-10 23:21 ` RFA: Add support for DWARF-based fixed point types Simon Marchi
2020-11-11  4:53   ` Joel Brobecker
2020-11-15  8:35 ` pushed: " Joel Brobecker
2020-11-15  8:35   ` [pushed/v2 1/9] gdb/configure: Add --with-libgmp-prefix option Joel Brobecker
2020-11-15 15:52     ` Bernd Edlinger
2020-11-16  3:45       ` Joel Brobecker
2020-11-16 14:20         ` Bernd Edlinger
2020-11-17  7:41           ` [PATCH] Enable GDB build with in-tree GMP and MPFR Bernd Edlinger
2020-11-18  3:44             ` Joel Brobecker
2020-11-18  8:14               ` Bernd Edlinger
2020-12-01 19:29                 ` Bernd Edlinger
2020-12-01 19:32                   ` Simon Marchi
2020-12-01 19:38                     ` Bernd Edlinger
2020-12-01 20:29                       ` Bernd Edlinger
2020-12-01 20:30                         ` Simon Marchi
2020-12-02  3:21                           ` Joel Brobecker
2020-12-08 20:39                             ` [PING] " Bernd Edlinger
2020-12-14 17:40                         ` [PATCH v2] " Bernd Edlinger
2020-12-14 18:47                           ` Simon Marchi
2020-12-14 21:35                             ` Tom Tromey
2020-12-14 22:17                               ` Simon Marchi
2020-12-15  2:33                                 ` Joel Brobecker
2020-12-15 14:39                                   ` Simon Marchi
2020-12-15 16:24                                     ` Bernd Edlinger
2020-12-16  7:33                                     ` Joel Brobecker
2020-12-16 18:16                                       ` Bernd Edlinger
2020-12-25 12:05                                         ` Bernd Edlinger
2020-12-27 22:01                                           ` Simon Marchi
2020-12-29  8:36                                             ` Bernd Edlinger
2020-12-29 14:50                                               ` Simon Marchi
2021-01-10 14:12                                                 ` Bernd Edlinger
2021-01-10 15:32                                                   ` Simon Marchi
2021-01-11  3:22                                                   ` Joel Brobecker
2021-01-16 18:01                                                     ` Bernd Edlinger
2020-12-15 15:33                                 ` Bernd Edlinger
2020-12-15 15:10                             ` Bernd Edlinger
2020-11-15  8:35   ` [pushed/v2 2/9] gdb: Make GMP a required dependency for building GDB Joel Brobecker
2020-11-15  8:35   ` [pushed/v2 3/9] gmp-utils: New API to simply use of GMP's integer/rational/float objects Joel Brobecker
2020-11-15  8:35   ` [pushed/v2 4/9] Move uinteger_pow gdb/valarith.c to gdb/utils.c and make it public Joel Brobecker
2020-11-15  8:35   ` [pushed/v2 5/9] Add support for printing value of DWARF-based fixed-point type objects Joel Brobecker
2020-11-15  8:35   ` [pushed/v2 6/9] fix printing of DWARF fixed-point type objects with format modifier Joel Brobecker
2020-11-15  8:35   ` [pushed/v2 7/9] Add ptype support for DWARF-based fixed-point types Joel Brobecker
2020-11-15  8:35   ` [pushed/v2 8/9] Add support for fixed-point type arithmetic Joel Brobecker
2020-11-15  8:35   ` [pushed/v2 9/9] Add support for fixed-point type comparison operators Joel Brobecker
2020-11-16 23:48   ` pushed: Add support for DWARF-based fixed point types Pedro Alves
2020-11-17 14:25     ` Simon Marchi
2020-11-18  3:47       ` Joel Brobecker
2020-11-22 13:12         ` [RFA] Add TYPE_CODE_FIXED_POINT handling in print_type_scalar Joel Brobecker
2020-11-22 14:35           ` Simon Marchi
2020-11-24  3:04             ` Joel Brobecker
2020-11-22 14:00         ` pushed: Add support for DWARF-based fixed point types Joel Brobecker
2020-11-22 20:11           ` Simon Marchi
2020-11-23  4:27             ` Joel Brobecker
2020-11-23 16:12               ` Simon Marchi
2020-11-24  2:39                 ` Joel Brobecker
2020-11-29 15:45               ` RFA: wrap mpz_export into gdb_mpz::safe_export Joel Brobecker
2020-11-29 15:45                 ` [RFA 1/2] Fix TARGET_CHAR_BIT/HOST_CHAR_BIT confusion in gmp-utils.c Joel Brobecker
2020-11-30 15:42                   ` Simon Marchi
2020-12-05  8:05                     ` Joel Brobecker
2020-11-29 15:45                 ` [RFA 2/2] gmp-utils: protect gdb_mpz exports against out-of-range values Joel Brobecker
2020-11-30 15:56                   ` Simon Marchi
2020-12-01  3:37                     ` Joel Brobecker
2020-12-01  4:02                       ` Simon Marchi
2020-12-01  7:11                         ` Joel Brobecker
2020-12-05  8:10                   ` [RFAv2 " Joel Brobecker
2020-12-05 23:26                     ` Simon Marchi
2020-12-06  4:58                       ` Joel Brobecker
2020-11-30 12:44                 ` RFA: wrap mpz_export into gdb_mpz::safe_export Christian Biesinger
2020-11-20 14:08   ` pushed: Add support for DWARF-based fixed point types Pedro Alves
2020-11-20 14:14     ` Joel Brobecker
2020-11-22 11:56   ` RFA/doco: Various changes related to GMP and fixed point type support Joel Brobecker
2020-11-22 11:56     ` [RFA/doco 1/4] gdb/NEWS: Document that building GDB now requires GMP Joel Brobecker
2020-11-22 15:31       ` Eli Zaretskii
2020-11-24  3:11         ` Joel Brobecker
2020-11-22 11:56     ` [RFA/doco 2/4] gdb/NEWS: Document that GDB now supports DWARF-based fixed point types Joel Brobecker
2020-11-22 15:33       ` Eli Zaretskii
2020-11-24  3:12         ` Joel Brobecker
2020-11-22 11:56     ` [RFA/doco 3/4] gdb/README: Document the --with-libgmp-prefix configure option Joel Brobecker
2020-11-22 15:32       ` Eli Zaretskii
2020-11-24  3:11         ` Joel Brobecker
2020-11-22 11:56     ` [RFA/doco 4/4] gdb/README: Fix the URL of the MPFR website (now https) Joel Brobecker
2020-11-22 15:33       ` Eli Zaretskii
2020-11-24  3:11         ` Joel Brobecker
2020-11-15  8:49 ` RFA: Various enhancements to the fixed-point support implementation Joel Brobecker
2020-11-15  8:49   ` [RFA 1/6] change gmp_string_asprintf to return an std::string Joel Brobecker
2020-11-16  0:41     ` Simon Marchi
2020-11-16  3:55       ` Joel Brobecker
2020-11-16 20:10         ` Simon Marchi
2020-11-15  8:49   ` [RFA 2/6] gmp-utils: Convert the read/write methods to using gdb::array_view Joel Brobecker
2020-11-16  0:52     ` Simon Marchi
2020-11-16 23:05       ` Pedro Alves
2020-11-17 14:32         ` Simon Marchi
2020-11-15  8:49   ` [RFA 3/6] gdbtypes.h: Get rid of the TYPE_FIXED_POINT_INFO macro Joel Brobecker
2020-11-15  8:49   ` [RFA 4/6] Make fixed_point_type_base_type a method of struct type Joel Brobecker
2020-11-15  8:49   ` [RFA 5/6] Make function fixed_point_scaling_factor " Joel Brobecker
2020-11-15  8:49   ` [RFA 6/6] valarith.c: Replace INIT_VAL_WITH_FIXED_POINT_VAL macro by lambda Joel Brobecker
2020-11-16  1:01   ` RFA: Various enhancements to the fixed-point support implementation Simon Marchi
2020-11-22 11:14   ` RFA v2: " Joel Brobecker
2020-11-22 11:14     ` [RFA v2 1/6] change and rename gmp_string_asprintf to return an std::string Joel Brobecker
2020-11-22 11:14     ` [RFA v2 2/6] gmp-utils: Convert the read/write methods to using gdb::array_view Joel Brobecker
2020-11-22 11:14     ` [RFA v2 3/6] gdbtypes.h: Get rid of the TYPE_FIXED_POINT_INFO macro Joel Brobecker
2020-11-22 11:14     ` [RFA v2 4/6] Make fixed_point_type_base_type a method of struct type Joel Brobecker
2020-11-22 11:14     ` [RFA v2 5/6] Make function fixed_point_scaling_factor " Joel Brobecker
2020-11-22 11:14     ` [RFA v2 6/6] valarith.c: Replace INIT_VAL_WITH_FIXED_POINT_VAL macro by lambda Joel Brobecker
2020-11-23 16:46     ` RFA v2: Various enhancements to the fixed-point support implementation Simon Marchi
2020-11-24  2:56       ` Joel Brobecker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2d12525a-3a85-3f69-bfea-22166f7fd358@simark.ca \
    --to=simark@simark.ca \
    --cc=brobecker@adacore.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).