Add initial type alignment support

This adds some basic type alignment support to gdb.  It changes struct
type to store the alignment, and updates dwarf2read.c to handle
DW_AT_alignment.  It also adds a new gdbarch method and updates
i386-tdep.c.

None of this new functionality is used anywhere yet, so tests will
wait until the next patch.

2018-04-30  Tom Tromey  <tom@tromey.com>

	* i386-tdep.c (i386_type_align): New function.
	(i386_gdbarch_init): Update.
	* gdbarch.sh (type_align): New method.
	* gdbarch.c, gdbarch.h: Rebuild.
	* arch-utils.h (default_type_align): Declare.
	* arch-utils.c (default_type_align): New function.
	* gdbtypes.h (TYPE_ALIGN_BITS): New define.
	(struct type) <align_log2>: New field.
	<instance_flags>: Now a bitfield.
	(TYPE_RAW_ALIGN): New macro.
	(type_align, type_raw_align, set_type_align): Declare.
	* gdbtypes.c (type_align, type_raw_align, set_type_align): New
	functions.
	* dwarf2read.c (quirk_rust_enum): Set type alignment.
	(get_alignment, maybe_set_alignment): New functions.
	(read_structure_type, read_enumeration_type, read_array_type)
	(read_set_type, read_tag_pointer_type, read_tag_reference_type)
	(read_subrange_type, read_base_type): Set type alignment.
This commit is contained in:
Tom Tromey 2018-04-20 11:50:09 -06:00
parent fe944acf8f
commit 2b4424c35b
10 changed files with 359 additions and 5 deletions

View file

@ -3013,6 +3013,128 @@ init_pointer_type (struct objfile *objfile,
return t;
}
/* See gdbtypes.h. */
unsigned
type_raw_align (struct type *type)
{
if (type->align_log2 != 0)
return 1 << (type->align_log2 - 1);
return 0;
}
/* See gdbtypes.h. */
unsigned
type_align (struct type *type)
{
unsigned raw_align = type_raw_align (type);
if (raw_align != 0)
return raw_align;
ULONGEST align = 0;
switch (TYPE_CODE (type))
{
case TYPE_CODE_PTR:
case TYPE_CODE_FUNC:
case TYPE_CODE_FLAGS:
case TYPE_CODE_INT:
case TYPE_CODE_FLT:
case TYPE_CODE_ENUM:
case TYPE_CODE_REF:
case TYPE_CODE_RVALUE_REF:
case TYPE_CODE_CHAR:
case TYPE_CODE_BOOL:
case TYPE_CODE_DECFLOAT:
{
struct gdbarch *arch = get_type_arch (type);
align = gdbarch_type_align (arch, type);
}
break;
case TYPE_CODE_ARRAY:
case TYPE_CODE_COMPLEX:
case TYPE_CODE_TYPEDEF:
align = type_align (TYPE_TARGET_TYPE (type));
break;
case TYPE_CODE_STRUCT:
case TYPE_CODE_UNION:
{
if (TYPE_NFIELDS (type) == 0)
{
/* An empty struct has alignment 1. */
align = 1;
break;
}
for (unsigned i = 0; i < TYPE_NFIELDS (type); ++i)
{
ULONGEST f_align = type_align (TYPE_FIELD_TYPE (type, i));
if (f_align == 0)
{
/* Don't pretend we know something we don't. */
align = 0;
break;
}
if (f_align > align)
align = f_align;
}
}
break;
case TYPE_CODE_SET:
case TYPE_CODE_RANGE:
case TYPE_CODE_STRING:
/* Not sure what to do here, and these can't appear in C or C++
anyway. */
break;
case TYPE_CODE_METHODPTR:
case TYPE_CODE_MEMBERPTR:
align = TYPE_LENGTH (type);
break;
case TYPE_CODE_VOID:
align = 1;
break;
case TYPE_CODE_ERROR:
case TYPE_CODE_METHOD:
default:
break;
}
if ((align & (align - 1)) != 0)
{
/* Not a power of 2, so pass. */
align = 0;
}
return align;
}
/* See gdbtypes.h. */
bool
set_type_align (struct type *type, ULONGEST align)
{
/* Must be a power of 2. Zero is ok. */
gdb_assert ((align & (align - 1)) == 0);
unsigned result = 0;
while (align != 0)
{
++result;
align >>= 1;
}
if (result >= (1 << TYPE_ALIGN_BITS))
return false;
type->align_log2 = result;
return true;
}
/* Queries on types. */