Handle bit offset and bit size in base types

PR symtab/25470 points out that the Zig programming language allows
integers of various bit sizes (including zero), not just sizes that
are a multiple of 8.

This is supported in DWARF by applying both a byte size and a
DW_AT_bit_size.

This patch adds support for this feature to integer and boolean types.
Other base types are not handled -- for floating-point types, this
didn't seem to make sense, and for character types I didn't see much
need.  (These can be added later if desired.)

I've also added support for DW_AT_data_bit_offset at the same time.  I
don't know whether the Zig compiler requires this, but it was
described in the same section in the DWARF standard and was easy to
add.

A new test case is supplied, using the DWARF assembler.

gdb/ChangeLog
2020-09-23  Tom Tromey  <tom@tromey.com>

	PR symtab/25470:
	* value.c (unpack_long, pack_long, pack_unsigned_long): Handle bit
	offset and bit size.
	* printcmd.c (print_scalar_formatted): Handle zero-length
	integer.
	(print_scalar_formatted): Use bit_size_differs_p.
	* gdbtypes.h (enum type_specific_kind) <TYPE_SPECIFIC_INT>: New
	constant.
	(union type_specific): <int_stuff>: New member.
	(struct type) <bit_size_differs_p, bit_size, bit_offset>: New
	methods.
	* gdbtypes.c (init_integer_type, init_boolean_type): Initialize
	TYPE_SPECIFIC_FIELD.
	(recursive_dump_type, copy_type_recursive): Update.
	* dwarf2/read.c (read_base_type): Handle DW_AT_bit_size and
	DW_AT_data_bit_offset.

gdb/testsuite/ChangeLog
2020-09-23  Tom Tromey  <tom@tromey.com>

	* gdb.dwarf2/intbits.exp: New file.
	* gdb.dwarf2/intbits.c: New file.
This commit is contained in:
Tom Tromey 2020-09-23 09:39:24 -06:00
parent bac51ab78d
commit 20a5fcbd5b
9 changed files with 406 additions and 7 deletions

View file

@ -374,6 +374,15 @@ print_scalar_formatted (const gdb_byte *valaddr, struct type *type,
valaddr += TYPE_LENGTH (type) - len;
}
/* Allow LEN == 0, and in this case, don't assume that VALADDR is
valid. */
const gdb_byte zero = 0;
if (len == 0)
{
len = 1;
valaddr = &zero;
}
if (size != 0 && (options->format == 'x' || options->format == 't'))
{
/* Truncate to fit. */
@ -404,8 +413,8 @@ print_scalar_formatted (const gdb_byte *valaddr, struct type *type,
long, and then printing the long. PR cli/16242 suggests changing
this to using C-style hex float format.
Biased range types must also be unbiased here; the unbiasing is
done by unpack_long. */
Biased range types and sub-word scalar types must also be handled
here; the value is correctly computed by unpack_long. */
gdb::byte_vector converted_bytes;
/* Some cases below will unpack the value again. In the biased
range case, we want to avoid this, so we store the unpacked value
@ -418,7 +427,8 @@ print_scalar_formatted (const gdb_byte *valaddr, struct type *type,
|| options->format == 'z'
|| options->format == 'd'
|| options->format == 'u'))
|| (type->code () == TYPE_CODE_RANGE && type->bounds ()->bias != 0))
|| (type->code () == TYPE_CODE_RANGE && type->bounds ()->bias != 0)
|| type->bit_size_differs_p ())
{
val_long.emplace (unpack_long (type, valaddr));
converted_bytes.resize (TYPE_LENGTH (type));