[PATCH 11/10] Split tdesc_type into multiple classes

Simon Marchi
2017-12-03T16:04:56Z

Hi Yao,

I revisited this patch, and implemented as best as I could what you
suggested. This patch applies on top of the current series.

This patch makes tdesc_type an abstract base class and creates three
subclasses:

- tdesc_type_builtin, for builtin types
- tdesc_type_vector, for vector types
- tdesc_type_with_fields, for struct, union, flag and enum types

This allows getting rid of the union in tdesc_type and to not allow the
std::vector separately. I tried to go further and create separate
classes for struct, union, flag and enum, but it proved too difficult.
One problem is that from the point of the of the target description
code, the types tdesc_type_* are opaque (only forward-declared).
Therefore, it doesn't know about inheritance relationship between those
classes. This makes it impossible to make functions that accept a
pointer to a base class and pass a pointer to a derived class, for
example. I think this patch here is a good compromise, and if somebody
wants to improve things further, the door is open.

A make_gdb_type virtual pure method is added to tdesc_type, which
replaces the current tdesc_gdb_type function. Calling this method on a
tdesc_type returns the corresponding built gdb type.

This patch was regtested on the buildbot.

gdb/ChangeLog:

* target-descriptions.c (struct tdesc_type): Use default
destructor.
: Remove.
<accept>: Remove.
(struct tdesc_type_builtin): New.
(struct tdesc_type_vector): New.
(struct tdesc_type_with_fields): New.
(tdesc_predefined_types): Change type to tdesc_type_builtin[].
(tdesc_gdb_type): Remove.
(tdesc_register_type): Adjust.
(tdesc_create_vector): Create tdesc_type_vector.
(tdesc_create_struct): Create tdesc_type_with_fields.
(tdesc_set_struct_size): Change parameter type.
(tdesc_create_union): Create tdesc_type_with_fields.
(tdesc_create_flags): Likewise.
(tdesc_create_enum): Likewise.
(tdesc_add_field): Change parameter type.
(tdesc_add_typed_bitfield): Likewise.
(tdesc_add_bitfield): Likewise.
(tdesc_add_flag): Likewise.
(tdesc_add_enum_value): Likewise.
(print_c_tdesc) <visit>: Remove overload with tdesc_type
parameter, add overloads for tdesc_type_builtin,
tdesc_type_with_fields and tdesc_type_vector.
* target-descriptions.h (tdesc_create_enum): Change return type.
(tdesc_add_typed_bitfield): Change parameter type.
(tdesc_add_enum_value): Change parameter type.
* xml-tdesc.c (struct tdesc_parsing_data) <current_type>: Change
type to tdesc_type_with_fields.
(tdesc_start_struct): Adjust.
(tdesc_start_flags): Adjust.
(tdesc_start_enum): Adjust.
(tdesc_start_field): Adjust.
* arch/tdesc.h (struct tdesc_type_builtin): Forward-declare.
(struct tdesc_type_vector): Forward-declare.
(struct tdesc_type_with_fields): Forward-declare.
(tdesc_create_struct): Change return type.
(tdesc_create_union): Likewise.
(tdesc_create_flags): Likewise.
(tdesc_add_field): Change parameter type.
(tdesc_set_struct_size): Likewise.
(tdesc_add_bitfield): Likewise.
(tdesc_add_flag): Likewise.
* features: Re-generate C files.

gdb/gdbserver/ChangeLog:

* tdesc.c (struct tdesc_type): Change return type.
(tdesc_add_flag): Change parameter type.
(tdesc_add_bitfield): Likewise.
(tdesc_add_field): Likewise.
(tdesc_set_struct_size): Likewise.
---
gdb/arch/tdesc.h | 25 +-
gdb/features/aarch64-core.c | 2 +-
gdb/features/aarch64-fpu.c | 2 +-
gdb/features/arc-arcompact.c | 2 +-
gdb/features/arc-v2.c | 2 +-
gdb/features/arm/arm-with-iwmmxt.c | 2 +-
gdb/features/i386/32bit-core.c | 2 +-
gdb/features/i386/32bit-mpx.c | 2 +-
gdb/features/i386/32bit-sse.c | 2 +-
gdb/features/i386/64bit-avx512.c | 2 +-
gdb/features/i386/64bit-core.c | 2 +-
gdb/features/i386/64bit-mpx.c | 2 +-
gdb/features/i386/64bit-sse.c | 2 +-
gdb/features/i386/x32-core.c | 2 +-
gdb/features/rs6000/powerpc-7400.c | 2 +-
gdb/features/rs6000/powerpc-altivec32.c | 2 +-
gdb/features/rs6000/powerpc-altivec32l.c | 2 +-
gdb/features/rs6000/powerpc-altivec64.c | 2 +-
gdb/features/rs6000/powerpc-altivec64l.c | 2 +-
gdb/features/rs6000/powerpc-cell32l.c | 2 +-
gdb/features/rs6000/powerpc-cell64l.c | 2 +-
gdb/features/rs6000/powerpc-isa205-altivec32l.c | 2 +-
gdb/features/rs6000/powerpc-isa205-altivec64l.c | 2 +-
gdb/features/rs6000/powerpc-isa205-vsx32l.c | 2 +-
gdb/features/rs6000/powerpc-isa205-vsx64l.c | 2 +-
gdb/features/rs6000/powerpc-vsx32.c | 2 +-
gdb/features/rs6000/powerpc-vsx32l.c | 2 +-
gdb/features/rs6000/powerpc-vsx64.c | 2 +-
gdb/features/rs6000/powerpc-vsx64l.c | 2 +-
gdb/features/s390-gs-linux64.c | 2 +-
gdb/features/s390-tevx-linux64.c | 2 +-
gdb/features/s390-vx-linux64.c | 2 +-
gdb/features/s390x-gs-linux64.c | 2 +-
gdb/features/s390x-tevx-linux64.c | 2 +-
gdb/features/s390x-vx-linux64.c | 2 +-
gdb/gdbserver/tdesc.c | 14 +-
gdb/target-descriptions.c | 694 ++++++++++++------------
gdb/target-descriptions.h | 10 +-
gdb/xml-tdesc.c | 20 +-
39 files changed, 429 insertions(+), 402 deletions(-)

diff --git a/gdb/arch/tdesc.h b/gdb/arch/tdesc.h
index 78bb0fb152..2240df6741 100644
--- a/gdb/arch/tdesc.h
+++ b/gdb/arch/tdesc.h
@@ -20,6 +20,9 @@

struct tdesc_feature;
struct tdesc_type;
+struct tdesc_type_builtin;
+struct tdesc_type_vector;
+struct tdesc_type_with_fields;
struct tdesc_reg;
struct target_desc;

@@ -51,37 +54,37 @@ struct tdesc_type *tdesc_create_vector (struct tdesc_feature *feature,
int count);

/* Return the created struct tdesc_type named NAME in FEATURE. */
-struct tdesc_type *tdesc_create_struct (struct tdesc_feature *feature,
- const char *name);
+tdesc_type_with_fields *tdesc_create_struct (struct tdesc_feature *feature,
+ const char *name);

/* Return the created union tdesc_type named NAME in FEATURE. */
-struct tdesc_type *tdesc_create_union (struct tdesc_feature *feature,
- const char *name);
+tdesc_type_with_fields *tdesc_create_union (struct tdesc_feature *feature,
+ const char *name);

/* Return the created flags tdesc_type named NAME in FEATURE. */
-struct tdesc_type *tdesc_create_flags (struct tdesc_feature *feature,
- const char *name,
- int size);
+tdesc_type_with_fields *tdesc_create_flags (struct tdesc_feature *feature,
+ const char *name,
+ int size);

/* Add a new field to TYPE. FIELD_NAME is its name, and FIELD_TYPE is
its type. */
-void tdesc_add_field (struct tdesc_type *type, const char *field_name,
+void tdesc_add_field (tdesc_type_with_fields *type, const char *field_name,
struct tdesc_type *field_type);

/* Set the total length of TYPE. Structs which contain bitfields may
omit the reserved bits, so the end of the last field may not
suffice. */
-void tdesc_set_struct_size (struct tdesc_type *type, int size);
+void tdesc_set_struct_size (tdesc_type_with_fields *type, int size);

/* Add a new untyped bitfield to TYPE.
Untyped bitfields become either uint32 or uint64 depending on the size
of the underlying type. */
-void tdesc_add_bitfield (struct tdesc_type *type, const char *field_name,
+void tdesc_add_bitfield (tdesc_type_with_fields *type, const char *field_name,
int start, int end);

/* A flag is just a typed(bool) single-bit bitfield.
This function is kept to minimize changes in generated files. */
-void tdesc_add_flag (struct tdesc_type *type, int start,
+void tdesc_add_flag (tdesc_type_with_fields *type, int start,
const char *flag_name);

/* Create a register in feature FEATURE. */
diff --git a/gdb/features/aarch64-core.c b/gdb/features/aarch64-core.c
index f3fad40133..419539866f 100644
--- a/gdb/features/aarch64-core.c
+++ b/gdb/features/aarch64-core.c
@@ -9,8 +9,8 @@ create_feature_aarch64_core (struct target_desc *result, long regnum)
struct tdesc_feature *feature;

feature = tdesc_create_feature (result, "org.gnu.gdb.aarch64.core", "aarch64-core.xml");
+ tdesc_type_with_fields *type;
struct tdesc_type *field_type;
- struct tdesc_type *type;
type = tdesc_create_flags (feature, "cpsr_flags", 4);
tdesc_add_flag (type, 0, "SP");
tdesc_add_flag (type, 1, "");
diff --git a/gdb/features/aarch64-fpu.c b/gdb/features/aarch64-fpu.c
index 3672f2541e..7e15252a61 100644
--- a/gdb/features/aarch64-fpu.c
+++ b/gdb/features/aarch64-fpu.c
@@ -46,7 +46,7 @@ create_feature_aarch64_fpu (struct target_desc *result, long regnum)
field_type = tdesc_named_type (feature, "int128");
tdesc_create_vector (feature, "v1i", field_type, 1);

- struct tdesc_type *type;
+ tdesc_type_with_fields *type;
type = tdesc_create_union (feature, "vnd");
field_type = tdesc_named_type (feature, "v2d");
tdesc_add_field (type, "f", field_type);
diff --git a/gdb/features/arc-arcompact.c b/gdb/features/arc-arcompact.c
index fd11e31447..cab1cea2a2 100644
--- a/gdb/features/arc-arcompact.c
+++ b/gdb/features/arc-arcompact.c
@@ -51,8 +51,8 @@ initialize_tdesc_arc_arcompact (void)
tdesc_create_reg (feature, "pcl", 33, 1, NULL, 32, "code_ptr");

feature = tdesc_create_feature (result, "org.gnu.gdb.arc.aux-minimal");
+ tdesc_type_with_fields *type;
struct tdesc_type *field_type;
- struct tdesc_type *type;
type = tdesc_create_flags (feature, "status32_type", 4);
tdesc_add_flag (type, 0, "H");
tdesc_add_bitfield (type, "E", 1, 2);
diff --git a/gdb/features/arc-v2.c b/gdb/features/arc-v2.c
index 6eeefdb984..8129ee5945 100644
--- a/gdb/features/arc-v2.c
+++ b/gdb/features/arc-v2.c
@@ -51,8 +51,8 @@ initialize_tdesc_arc_v2 (void)
tdesc_create_reg (feature, "pcl", 33, 1, NULL, 32, "code_ptr");

feature = tdesc_create_feature (result, "org.gnu.gdb.arc.aux-minimal");
+ tdesc_type_with_fields *type;
struct tdesc_type *field_type;
- struct tdesc_type *type;
type = tdesc_create_flags (feature, "status32_type", 4);
tdesc_add_flag (type, 0, "H");
tdesc_add_bitfield (type, "E", 1, 4);
diff --git a/gdb/features/arm/arm-with-iwmmxt.c b/gdb/features/arm/arm-with-iwmmxt.c
index 5f839a000d..c0066caed1 100644
--- a/gdb/features/arm/arm-with-iwmmxt.c
+++ b/gdb/features/arm/arm-with-iwmmxt.c
@@ -44,7 +44,7 @@ initialize_tdesc_arm_with_iwmmxt (void)
field_type = tdesc_named_type (feature, "uint32");
tdesc_create_vector (feature, "iwmmxt_v2u32", field_type, 2);

- struct tdesc_type *type;
+ tdesc_type_with_fields *type;
type = tdesc_create_union (feature, "iwmmxt_vec64i");
field_type = tdesc_named_type (feature, "iwmmxt_v8u8");
tdesc_add_field (type, "u8", field_type);
diff --git a/gdb/features/i386/32bit-core.c b/gdb/features/i386/32bit-core.c
index ec903f3218..17db53df67 100644
--- a/gdb/features/i386/32bit-core.c
+++ b/gdb/features/i386/32bit-core.c
@@ -9,8 +9,8 @@ create_feature_i386_32bit_core (struct target_desc *result, long regnum)
struct tdesc_feature *feature;

feature = tdesc_create_feature (result, "org.gnu.gdb.i386.core", "32bit-core.xml");
+ tdesc_type_with_fields *type;
struct tdesc_type *field_type;
- struct tdesc_type *type;
type = tdesc_create_flags (feature, "i386_eflags", 4);
tdesc_add_flag (type, 0, "CF");
tdesc_add_flag (type, 1, "");
diff --git a/gdb/features/i386/32bit-mpx.c b/gdb/features/i386/32bit-mpx.c
index 25a3fb145a..c6cf5fb51b 100644
--- a/gdb/features/i386/32bit-mpx.c
+++ b/gdb/features/i386/32bit-mpx.c
@@ -9,8 +9,8 @@ create_feature_i386_32bit_mpx (struct target_desc *result, long regnum)
struct tdesc_feature *feature;

feature = tdesc_create_feature (result, "org.gnu.gdb.i386.mpx", "32bit-mpx.xml");
+ tdesc_type_with_fields *type;
struct tdesc_type *field_type;
- struct tdesc_type *type;
type = tdesc_create_struct (feature, "br128");
field_type = tdesc_named_type (feature, "uint64");
tdesc_add_field (type, "lbound", field_type);
diff --git a/gdb/features/i386/32bit-sse.c b/gdb/features/i386/32bit-sse.c
index 01b2058af6..ea7fe83c60 100644
--- a/gdb/features/i386/32bit-sse.c
+++ b/gdb/features/i386/32bit-sse.c
@@ -28,7 +28,7 @@ create_feature_i386_32bit_sse (struct target_desc *result, long regnum)
field_type = tdesc_named_type (feature, "int64");
tdesc_create_vector (feature, "v2i64", field_type, 2);

- struct tdesc_type *type;
+ tdesc_type_with_fields *type;
type = tdesc_create_union (feature, "vec128");
field_type = tdesc_named_type (feature, "v4f");
tdesc_add_field (type, "v4_float", field_type);
diff --git a/gdb/features/i386/64bit-avx512.c b/gdb/features/i386/64bit-avx512.c
index fb50960896..02510de4d5 100644
--- a/gdb/features/i386/64bit-avx512.c
+++ b/gdb/features/i386/64bit-avx512.c
@@ -28,7 +28,7 @@ create_feature_i386_64bit_avx512 (struct target_desc *result, long regnum)
field_type = tdesc_named_type (feature, "int64");
tdesc_create_vector (feature, "v2i64", field_type, 2);

- struct tdesc_type *type;
+ tdesc_type_with_fields *type;
type = tdesc_create_union (feature, "vec128");
field_type = tdesc_named_type (feature, "v4f");
tdesc_add_field (type, "v4_float", field_type);
diff --git a/gdb/features/i386/64bit-core.c b/gdb/features/i386/64bit-core.c
index 14d4a19d67..10f0209c88 100644
--- a/gdb/features/i386/64bit-core.c
+++ b/gdb/features/i386/64bit-core.c
@@ -9,8 +9,8 @@ create_feature_i386_64bit_core (struct target_desc *result, long regnum)
struct tdesc_feature *feature;

feature = tdesc_create_feature (result, "org.gnu.gdb.i386.core", "64bit-core.xml");
+ tdesc_type_with_fields *type;
struct tdesc_type *field_type;
- struct tdesc_type *type;
type = tdesc_create_flags (feature, "i386_eflags", 4);
tdesc_add_flag (type, 0, "CF");
tdesc_add_flag (type, 1, "");
diff --git a/gdb/features/i386/64bit-mpx.c b/gdb/features/i386/64bit-mpx.c
index 2751e03221..ff71f8e6aa 100644
--- a/gdb/features/i386/64bit-mpx.c
+++ b/gdb/features/i386/64bit-mpx.c
@@ -9,8 +9,8 @@ create_feature_i386_64bit_mpx (struct target_desc *result, long regnum)
struct tdesc_feature *feature;

feature = tdesc_create_feature (result, "org.gnu.gdb.i386.mpx", "64bit-mpx.xml");
+ tdesc_type_with_fields *type;
struct tdesc_type *field_type;
- struct tdesc_type *type;
type = tdesc_create_struct (feature, "br128");
field_type = tdesc_named_type (feature, "uint64");
tdesc_add_field (type, "lbound", field_type);
diff --git a/gdb/features/i386/64bit-sse.c b/gdb/features/i386/64bit-sse.c
index bc384988e7..251a825e34 100644
--- a/gdb/features/i386/64bit-sse.c
+++ b/gdb/features/i386/64bit-sse.c
@@ -28,7 +28,7 @@ create_feature_i386_64bit_sse (struct target_desc *result, long regnum)
field_type = tdesc_named_type (feature, "int64");
tdesc_create_vector (feature, "v2i64", field_type, 2);

- struct tdesc_type *type;
+ tdesc_type_with_fields *type;
type = tdesc_create_union (feature, "vec128");
field_type = tdesc_named_type (feature, "v4f");
tdesc_add_field (type, "v4_float", field_type);
diff --git a/gdb/features/i386/x32-core.c b/gdb/features/i386/x32-core.c
index 3939abc694..ce1bd1a248 100644
--- a/gdb/features/i386/x32-core.c
+++ b/gdb/features/i386/x32-core.c
@@ -9,8 +9,8 @@ create_feature_i386_x32_core (struct target_desc *result, long regnum)
struct tdesc_feature *feature;

feature = tdesc_create_feature (result, "org.gnu.gdb.i386.core", "x32-core.xml");
+ tdesc_type_with_fields *type;
struct tdesc_type *field_type;
- struct tdesc_type *type;
type = tdesc_create_flags (feature, "i386_eflags", 4);
tdesc_add_flag (type, 0, "CF");
tdesc_add_flag (type, 1, "");
diff --git a/gdb/features/rs6000/powerpc-7400.c b/gdb/features/rs6000/powerpc-7400.c
index 32b6995fd0..f494973dfc 100644
--- a/gdb/features/rs6000/powerpc-7400.c
+++ b/gdb/features/rs6000/powerpc-7400.c
@@ -151,7 +151,7 @@ initialize_tdesc_powerpc_7400 (void)
field_type = tdesc_named_type (feature, "int8");
tdesc_create_vector (feature, "v16i8", field_type, 16);

- struct tdesc_type *type;
+ tdesc_type_with_fields *type;
type = tdesc_create_union (feature, "vec128");
field_type = tdesc_named_type (feature, "uint128");
tdesc_add_field (type, "uint128", field_type);
diff --git a/gdb/features/rs6000/powerpc-altivec32.c b/gdb/features/rs6000/powerpc-altivec32.c
index e97132ec75..593958d1d8 100644
--- a/gdb/features/rs6000/powerpc-altivec32.c
+++ b/gdb/features/rs6000/powerpc-altivec32.c
@@ -103,7 +103,7 @@ initialize_tdesc_powerpc_altivec32 (void)
field_type = tdesc_named_type (feature, "int8");
tdesc_create_vector (feature, "v16i8", field_type, 16);

- struct tdesc_type *type;
+ tdesc_type_with_fields *type;
type = tdesc_create_union (feature, "vec128");
field_type = tdesc_named_type (feature, "uint128");
tdesc_add_field (type, "uint128", field_type);
diff --git a/gdb/features/rs6000/powerpc-altivec32l.c b/gdb/features/rs6000/powerpc-altivec32l.c
index a9445d96d0..d713615138 100644
--- a/gdb/features/rs6000/powerpc-altivec32l.c
+++ b/gdb/features/rs6000/powerpc-altivec32l.c
@@ -107,7 +107,7 @@ initialize_tdesc_powerpc_altivec32l (void)
field_type = tdesc_named_type (feature, "int8");
tdesc_create_vector (feature, "v16i8", field_type, 16);

- struct tdesc_type *type;
+ tdesc_type_with_fields *type;
type = tdesc_create_union (feature, "vec128");
field_type = tdesc_named_type (feature, "uint128");
tdesc_add_field (type, "uint128", field_type);
diff --git a/gdb/features/rs6000/powerpc-altivec64.c b/gdb/features/rs6000/powerpc-altivec64.c
index 3b626a9441..a7b2229b3a 100644
--- a/gdb/features/rs6000/powerpc-altivec64.c
+++ b/gdb/features/rs6000/powerpc-altivec64.c
@@ -103,7 +103,7 @@ initialize_tdesc_powerpc_altivec64 (void)
field_type = tdesc_named_type (feature, "int8");
tdesc_create_vector (feature, "v16i8", field_type, 16);

- struct tdesc_type *type;
+ tdesc_type_with_fields *type;
type = tdesc_create_union (feature, "vec128");
field_type = tdesc_named_type (feature, "uint128");
tdesc_add_field (type, "uint128", field_type);
diff --git a/gdb/features/rs6000/powerpc-altivec64l.c b/gdb/features/rs6000/powerpc-altivec64l.c
index cca5353772..50415ee8e6 100644
--- a/gdb/features/rs6000/powerpc-altivec64l.c
+++ b/gdb/features/rs6000/powerpc-altivec64l.c
@@ -107,7 +107,7 @@ initialize_tdesc_powerpc_altivec64l (void)
field_type = tdesc_named_type (feature, "int8");
tdesc_create_vector (feature, "v16i8", field_type, 16);

- struct tdesc_type *type;
+ tdesc_type_with_fields *type;
type = tdesc_create_union (feature, "vec128");
field_type = tdesc_named_type (feature, "uint128");
tdesc_add_field (type, "uint128", field_type);
diff --git a/gdb/features/rs6000/powerpc-cell32l.c b/gdb/features/rs6000/powerpc-cell32l.c
index c615b8d8d6..0137e49244 100644
--- a/gdb/features/rs6000/powerpc-cell32l.c
+++ b/gdb/features/rs6000/powerpc-cell32l.c
@@ -109,7 +109,7 @@ initialize_tdesc_powerpc_cell32l (void)
field_type = tdesc_named_type (feature, "int8");
tdesc_create_vector (feature, "v16i8", field_type, 16);

- struct tdesc_type *type;
+ tdesc_type_with_fields *type;
type = tdesc_create_union (feature, "vec128");
field_type = tdesc_named_type (feature, "uint128");
tdesc_add_field (type, "uint128", field_type);
diff --git a/gdb/features/rs6000/powerpc-cell64l.c b/gdb/features/rs6000/powerpc-cell64l.c
index 5040e5e929..f3f7374e17 100644
--- a/gdb/features/rs6000/powerpc-cell64l.c
+++ b/gdb/features/rs6000/powerpc-cell64l.c
@@ -109,7 +109,7 @@ initialize_tdesc_powerpc_cell64l (void)
field_type = tdesc_named_type (feature, "int8");
tdesc_create_vector (feature, "v16i8", field_type, 16);

- struct tdesc_type *type;
+ tdesc_type_with_fields *type;
type = tdesc_create_union (feature, "vec128");
field_type = tdesc_named_type (feature, "uint128");
tdesc_add_field (type, "uint128", field_type);
diff --git a/gdb/features/rs6000/powerpc-isa205-altivec32l.c b/gdb/features/rs6000/powerpc-isa205-altivec32l.c
index 943d02db46..2a8657728f 100644
--- a/gdb/features/rs6000/powerpc-isa205-altivec32l.c
+++ b/gdb/features/rs6000/powerpc-isa205-altivec32l.c
@@ -107,7 +107,7 @@ initialize_tdesc_powerpc_isa205_altivec32l (void)
field_type = tdesc_named_type (feature, "int8");
tdesc_create_vector (feature, "v16i8", field_type, 16);

- struct tdesc_type *type;
+ tdesc_type_with_fields *type;
type = tdesc_create_union (feature, "vec128");
field_type = tdesc_named_type (feature, "uint128");
tdesc_add_field (type, "uint128", field_type);
diff --git a/gdb/features/rs6000/powerpc-isa205-altivec64l.c b/gdb/features/rs6000/powerpc-isa205-altivec64l.c
index d454bac1b8..1aad4470c8 100644
--- a/gdb/features/rs6000/powerpc-isa205-altivec64l.c
+++ b/gdb/features/rs6000/powerpc-isa205-altivec64l.c
@@ -107,7 +107,7 @@ initialize_tdesc_powerpc_isa205_altivec64l (void)
field_type = tdesc_named_type (feature, "int8");
tdesc_create_vector (feature, "v16i8", field_type, 16);

- struct tdesc_type *type;
+ tdesc_type_with_fields *type;
type = tdesc_create_union (feature, "vec128");
field_type = tdesc_named_type (feature, "uint128");
tdesc_add_field (type, "uint128", field_type);
diff --git a/gdb/features/rs6000/powerpc-isa205-vsx32l.c b/gdb/features/rs6000/powerpc-isa205-vsx32l.c
index 09fd5cee29..76b29a9cf9 100644
--- a/gdb/features/rs6000/powerpc-isa205-vsx32l.c
+++ b/gdb/features/rs6000/powerpc-isa205-vsx32l.c
@@ -107,7 +107,7 @@ initialize_tdesc_powerpc_isa205_vsx32l (void)
field_type = tdesc_named_type (feature, "int8");
tdesc_create_vector (feature, "v16i8", field_type, 16);

- struct tdesc_type *type;
+ tdesc_type_with_fields *type;
type = tdesc_create_union (feature, "vec128");
field_type = tdesc_named_type (feature, "uint128");
tdesc_add_field (type, "uint128", field_type);
diff --git a/gdb/features/rs6000/powerpc-isa205-vsx64l.c b/gdb/features/rs6000/powerpc-isa205-vsx64l.c
index d295ab7726..2ce2228501 100644
--- a/gdb/features/rs6000/powerpc-isa205-vsx64l.c
+++ b/gdb/features/rs6000/powerpc-isa205-vsx64l.c
@@ -107,7 +107,7 @@ initialize_tdesc_powerpc_isa205_vsx64l (void)
field_type = tdesc_named_type (feature, "int8");
tdesc_create_vector (feature, "v16i8", field_type, 16);

- struct tdesc_type *type;
+ tdesc_type_with_fields *type;
type = tdesc_create_union (feature, "vec128");
field_type = tdesc_named_type (feature, "uint128");
tdesc_add_field (type, "uint128", field_type);
diff --git a/gdb/features/rs6000/powerpc-vsx32.c b/gdb/features/rs6000/powerpc-vsx32.c
index 8cf7562468..c0dadd0c91 100644
--- a/gdb/features/rs6000/powerpc-vsx32.c
+++ b/gdb/features/rs6000/powerpc-vsx32.c
@@ -103,7 +103,7 @@ initialize_tdesc_powerpc_vsx32 (void)
field_type = tdesc_named_type (feature, "int8");
tdesc_create_vector (feature, "v16i8", field_type, 16);

- struct tdesc_type *type;
+ tdesc_type_with_fields *type;
type = tdesc_create_union (feature, "vec128");
field_type = tdesc_named_type (feature, "uint128");
tdesc_add_field (type, "uint128", field_type);
diff --git a/gdb/features/rs6000/powerpc-vsx32l.c b/gdb/features/rs6000/powerpc-vsx32l.c
index e8c1881553..29fbe62ae4 100644
--- a/gdb/features/rs6000/powerpc-vsx32l.c
+++ b/gdb/features/rs6000/powerpc-vsx32l.c
@@ -107,7 +107,7 @@ initialize_tdesc_powerpc_vsx32l (void)
field_type = tdesc_named_type (feature, "int8");
tdesc_create_vector (feature, "v16i8", field_type, 16);

- struct tdesc_type *type;
+ tdesc_type_with_fields *type;
type = tdesc_create_union (feature, "vec128");
field_type = tdesc_named_type (feature, "uint128");
tdesc_add_field (type, "uint128", field_type);
diff --git a/gdb/features/rs6000/powerpc-vsx64.c b/gdb/features/rs6000/powerpc-vsx64.c
index 30953c539c..0295df6ff9 100644
--- a/gdb/features/rs6000/powerpc-vsx64.c
+++ b/gdb/features/rs6000/powerpc-vsx64.c
@@ -103,7 +103,7 @@ initialize_tdesc_powerpc_vsx64 (void)
field_type = tdesc_named_type (feature, "int8");
tdesc_create_vector (feature, "v16i8", field_type, 16);

- struct tdesc_type *type;
+ tdesc_type_with_fields *type;
type = tdesc_create_union (feature, "vec128");
field_type = tdesc_named_type (feature, "uint128");
tdesc_add_field (type, "uint128", field_type);
diff --git a/gdb/features/rs6000/powerpc-vsx64l.c b/gdb/features/rs6000/powerpc-vsx64l.c
index 5f12650547..e254ef00dc 100644
--- a/gdb/features/rs6000/powerpc-vsx64l.c
+++ b/gdb/features/rs6000/powerpc-vsx64l.c
@@ -107,7 +107,7 @@ initialize_tdesc_powerpc_vsx64l (void)
field_type = tdesc_named_type (feature, "int8");
tdesc_create_vector (feature, "v16i8", field_type, 16);

- struct tdesc_type *type;
+ tdesc_type_with_fields *type;
type = tdesc_create_union (feature, "vec128");
field_type = tdesc_named_type (feature, "uint128");
tdesc_add_field (type, "uint128", field_type);
diff --git a/gdb/features/s390-gs-linux64.c b/gdb/features/s390-gs-linux64.c
index 39e70436a5..545a333375 100644
--- a/gdb/features/s390-gs-linux64.c
+++ b/gdb/features/s390-gs-linux64.c
@@ -134,7 +134,7 @@ initialize_tdesc_s390_gs_linux64 (void)
field_type = tdesc_named_type (feature, "int64");
tdesc_create_vector (feature, "v2i64", field_type, 2);

- struct tdesc_type *type;
+ tdesc_type_with_fields *type;
type = tdesc_create_union (feature, "vec128");
field_type = tdesc_named_type (feature, "v4f");
tdesc_add_field (type, "v4_float", field_type);
diff --git a/gdb/features/s390-tevx-linux64.c b/gdb/features/s390-tevx-linux64.c
index d9b18d3850..1e376b4438 100644
--- a/gdb/features/s390-tevx-linux64.c
+++ b/gdb/features/s390-tevx-linux64.c
@@ -134,7 +134,7 @@ initialize_tdesc_s390_tevx_linux64 (void)
field_type = tdesc_named_type (feature, "int64");
tdesc_create_vector (feature, "v2i64", field_type, 2);

- struct tdesc_type *type;
+ tdesc_type_with_fields *type;
type = tdesc_create_union (feature, "vec128");
field_type = tdesc_named_type (feature, "v4f");
tdesc_add_field (type, "v4_float", field_type);
diff --git a/gdb/features/s390-vx-linux64.c b/gdb/features/s390-vx-linux64.c
index b2138dd90a..46c6bbe14f 100644
--- a/gdb/features/s390-vx-linux64.c
+++ b/gdb/features/s390-vx-linux64.c
@@ -112,7 +112,7 @@ initialize_tdesc_s390_vx_linux64 (void)
field_type = tdesc_named_type (feature, "int64");
tdesc_create_vector (feature, "v2i64", field_type, 2);

- struct tdesc_type *type;
+ tdesc_type_with_fields *type;
type = tdesc_create_union (feature, "vec128");
field_type = tdesc_named_type (feature, "v4f");
tdesc_add_field (type, "v4_float", field_type);
diff --git a/gdb/features/s390x-gs-linux64.c b/gdb/features/s390x-gs-linux64.c
index 652e6cfe02..f9e31bdec4 100644
--- a/gdb/features/s390x-gs-linux64.c
+++ b/gdb/features/s390x-gs-linux64.c
@@ -118,7 +118,7 @@ initialize_tdesc_s390x_gs_linux64 (void)
field_type = tdesc_named_type (feature, "int64");
tdesc_create_vector (feature, "v2i64", field_type, 2);

- struct tdesc_type *type;
+ tdesc_type_with_fields *type;
type = tdesc_create_union (feature, "vec128");
field_type = tdesc_named_type (feature, "v4f");
tdesc_add_field (type, "v4_float", field_type);
diff --git a/gdb/features/s390x-tevx-linux64.c b/gdb/features/s390x-tevx-linux64.c
index 02afd3a5c2..31952d1f91 100644
--- a/gdb/features/s390x-tevx-linux64.c
+++ b/gdb/features/s390x-tevx-linux64.c
@@ -118,7 +118,7 @@ initialize_tdesc_s390x_tevx_linux64 (void)
field_type = tdesc_named_type (feature, "int64");
tdesc_create_vector (feature, "v2i64", field_type, 2);

- struct tdesc_type *type;
+ tdesc_type_with_fields *type;
type = tdesc_create_union (feature, "vec128");
field_type = tdesc_named_type (feature, "v4f");
tdesc_add_field (type, "v4_float", field_type);
diff --git a/gdb/features/s390x-vx-linux64.c b/gdb/features/s390x-vx-linux64.c
index 120db53a33..a4ffd627c2 100644
--- a/gdb/features/s390x-vx-linux64.c
+++ b/gdb/features/s390x-vx-linux64.c
@@ -96,7 +96,7 @@ initialize_tdesc_s390x_vx_linux64 (void)
field_type = tdesc_named_type (feature, "int64");
tdesc_create_vector (feature, "v2i64", field_type, 2);

- struct tdesc_type *type;
+ tdesc_type_with_fields *type;
type = tdesc_create_union (feature, "vec128");
field_type = tdesc_named_type (feature, "v4f");
tdesc_add_field (type, "v4_float", field_type);
diff --git a/gdb/gdbserver/tdesc.c b/gdb/gdbserver/tdesc.c
index e2c4288efb..5ea234145f 100644
--- a/gdb/gdbserver/tdesc.c
+++ b/gdb/gdbserver/tdesc.c
@@ -146,7 +146,7 @@ tdesc_create_feature (struct target_desc *tdesc, const char *name,

/* See arch/tdesc.h. */

-struct tdesc_type *
+tdesc_type_with_fields *
tdesc_create_flags (struct tdesc_feature *feature, const char *name,
int size)
{
@@ -156,7 +156,7 @@ tdesc_create_flags (struct tdesc_feature *feature, const char *name,
/* See arch/tdesc.h. */

void
-tdesc_add_flag (struct tdesc_type *type, int start,
+tdesc_add_flag (tdesc_type_with_fields *type, int start,
const char *flag_name)
{}

@@ -170,7 +170,7 @@ tdesc_named_type (const struct tdesc_feature *feature, const char *id)

/* See arch/tdesc.h. */

-struct tdesc_type *
+tdesc_type_with_fields *
tdesc_create_union (struct tdesc_feature *feature, const char *id)
{
return NULL;
@@ -178,7 +178,7 @@ tdesc_create_union (struct tdesc_feature *feature, const char *id)

/* See arch/tdesc.h. */

-struct tdesc_type *
+tdesc_type_with_fields *
tdesc_create_struct (struct tdesc_feature *feature, const char *id)
{
return NULL;
@@ -222,20 +222,20 @@ tdesc_create_vector (struct tdesc_feature *feature, const char *name,
}

void
-tdesc_add_bitfield (struct tdesc_type *type, const char *field_name,
+tdesc_add_bitfield (tdesc_type_with_fields *type, const char *field_name,
int start, int end)
{}

/* See arch/tdesc.h. */

void
-tdesc_add_field (struct tdesc_type *type, const char *field_name,
+tdesc_add_field (tdesc_type_with_fields *type, const char *field_name,
struct tdesc_type *field_type)
{}

/* See arch/tdesc.h. */

void
-tdesc_set_struct_size (struct tdesc_type *type, int size)
+tdesc_set_struct_size (tdesc_type_with_fields *type, int size)
{
}
diff --git a/gdb/target-descriptions.c b/gdb/target-descriptions.c
index fdb45f374f..5c48296323 100644
--- a/gdb/target-descriptions.c
+++ b/gdb/target-descriptions.c
@@ -49,7 +49,10 @@ public:
virtual void visit_pre (const tdesc_feature *e) = 0;
virtual void visit_post (const tdesc_feature *e) = 0;

- virtual void visit (const tdesc_type *e) = 0;
+ virtual void visit (const tdesc_type_builtin *e) = 0;
+ virtual void visit (const tdesc_type_vector *e) = 0;
+ virtual void visit (const tdesc_type_with_fields *e) = 0;
+
virtual void visit (const tdesc_reg *e) = 0;
};

@@ -200,82 +203,313 @@ struct tdesc_type : tdesc_element
{
tdesc_type (const std::string &name_, enum tdesc_type_kind kind_)
: name (name_), kind (kind_)
+ {}
+
+ virtual ~tdesc_type () = default;
+
+ DISABLE_COPY_AND_ASSIGN (tdesc_type);
+
+ /* The name of this type. */
+ std::string name;
+
+ /* Identify the kind of this type. */
+ enum tdesc_type_kind kind;
+
+ bool operator== (const tdesc_type &other) const
{
- memset (&u, 0, sizeof (u));
+ return name == other.name && kind == other.kind;
+ }

- switch (kind)
- {
- case TDESC_TYPE_STRUCT:
- case TDESC_TYPE_UNION:
- case TDESC_TYPE_FLAGS:
- case TDESC_TYPE_ENUM:
- u.u.fields = new std::vector<tdesc_type_field> ();
- break;
+ bool operator!= (const tdesc_type &other) const
+ {
+ return !(*this == other);
+ }

- default:
- break;
- }
+ /* Construct, if necessary, and return the GDB type implementing this
+ target type for architecture GDBARCH. */
+
+ virtual type *make_gdb_type (struct gdbarch *gdbarch) const = 0;
+};
+
+typedef std::unique_ptr<tdesc_type> tdesc_type_up;
+
+struct tdesc_type_builtin : tdesc_type
+{
+ tdesc_type_builtin (const std::string &name, enum tdesc_type_kind kind)
+ : tdesc_type (name, kind)
+ {}
+
+ void accept (tdesc_element_visitor &v) const override
+ {
+ v.visit (this);
}

- virtual ~tdesc_type ()
+ type *make_gdb_type (struct gdbarch *gdbarch) const override
{
- switch (kind)
+ switch (this->kind)
{
- case TDESC_TYPE_STRUCT:
- case TDESC_TYPE_UNION:
- case TDESC_TYPE_FLAGS:
- case TDESC_TYPE_ENUM:
- delete u.u.fields;
- break;
+ /* Predefined types. */
+ case TDESC_TYPE_BOOL:
+ return builtin_type (gdbarch)->builtin_bool;

- default:
- break;
+ case TDESC_TYPE_INT8:
+ return builtin_type (gdbarch)->builtin_int8;
+
+ case TDESC_TYPE_INT16:
+ return builtin_type (gdbarch)->builtin_int16;
+
+ case TDESC_TYPE_INT32:
+ return builtin_type (gdbarch)->builtin_int32;
+
+ case TDESC_TYPE_INT64:
+ return builtin_type (gdbarch)->builtin_int64;
+
+ case TDESC_TYPE_INT128:
+ return builtin_type (gdbarch)->builtin_int128;
+
+ case TDESC_TYPE_UINT8:
+ return builtin_type (gdbarch)->builtin_uint8;
+
+ case TDESC_TYPE_UINT16:
+ return builtin_type (gdbarch)->builtin_uint16;
+
+ case TDESC_TYPE_UINT32:
+ return builtin_type (gdbarch)->builtin_uint32;
+
+ case TDESC_TYPE_UINT64:
+ return builtin_type (gdbarch)->builtin_uint64;
+
+ case TDESC_TYPE_UINT128:
+ return builtin_type (gdbarch)->builtin_uint128;
+
+ case TDESC_TYPE_CODE_PTR:
+ return builtin_type (gdbarch)->builtin_func_ptr;
+
+ case TDESC_TYPE_DATA_PTR:
+ return builtin_type (gdbarch)->builtin_data_ptr;
+ }
+
+ type *gdb_type = tdesc_find_type (gdbarch, this->name.c_str ());
+ if (gdb_type != NULL)
+ return gdb_type;
+
+ switch (this->kind)
+ {
+ case TDESC_TYPE_IEEE_SINGLE:
+ return arch_float_type (gdbarch, -1, "builtin_type_ieee_single",
+ floatformats_ieee_single);
+
+ case TDESC_TYPE_IEEE_DOUBLE:
+ return arch_float_type (gdbarch, -1, "builtin_type_ieee_double",
+ floatformats_ieee_double);
+
+ case TDESC_TYPE_ARM_FPA_EXT:
+ return arch_float_type (gdbarch, -1, "builtin_type_arm_ext",
+ floatformats_arm_ext);
+
+ case TDESC_TYPE_I387_EXT:
+ return arch_float_type (gdbarch, -1, "builtin_type_i387_ext",
+ floatformats_i387_ext);
}
+
+ internal_error (__FILE__, __LINE__,
+ "Type \"%s\" has an unknown kind %d",
+ this->name.c_str (), this->kind);
+
+ return NULL;
}
+};

- DISABLE_COPY_AND_ASSIGN (tdesc_type);
+/* tdesc_type for vector types. */

- /* The name of this type. */
- std::string name;
+struct tdesc_type_vector : tdesc_type
+{
+ tdesc_type_vector (const std::string &name, tdesc_type *element_type_, int count_)
+ : tdesc_type (name, TDESC_TYPE_VECTOR),
+ element_type (element_type_), count (count_)
+ {}

- /* Identify the kind of this type. */
- enum tdesc_type_kind kind;
+ void accept (tdesc_element_visitor &v) const override
+ {
+ v.visit (this);
+ }

- /* Kind-specific data. */
- union
+ type *make_gdb_type (struct gdbarch *gdbarch) const override
{
- /* Vector type. */
- struct
- {
- struct tdesc_type *type;
- int count;
- } v;
+ type *vector_gdb_type = tdesc_find_type (gdbarch, this->name.c_str ());
+ if (vector_gdb_type != NULL)
+ return vector_gdb_type;

- /* Struct, union, flags, or enum type. */
- struct
- {
- std::vector<tdesc_type_field> *fields;
- int size;
- } u;
- } u;
+ type *element_gdb_type = this->element_type->make_gdb_type (gdbarch);
+ vector_gdb_type = init_vector_type (element_gdb_type, this->count);
+ TYPE_NAME (vector_gdb_type) = xstrdup (this->name.c_str ());
+
+ return vector_gdb_type;
+ }
+
+ struct tdesc_type *element_type;
+ int count;
+};
+
+/* tdesc_type for struct, union, flags, and enum types. */
+
+struct tdesc_type_with_fields : tdesc_type
+{
+ tdesc_type_with_fields (const std::string &name, tdesc_type_kind kind,
+ int size_ = 0)
+ : tdesc_type (name, kind), size (size_)
+ {}

void accept (tdesc_element_visitor &v) const override
{
v.visit (this);
}

- bool operator== (const tdesc_type &other) const
+ type *make_gdb_type_struct (struct gdbarch *gdbarch) const
{
- return name == other.name && kind == other.kind;
+ type *struct_gdb_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
+ TYPE_NAME (struct_gdb_type) = xstrdup (this->name.c_str ());
+ TYPE_TAG_NAME (struct_gdb_type) = TYPE_NAME (struct_gdb_type);
+
+ for (const tdesc_type_field &f : this->fields)
+ {
+ if (f.start != -1 && f.end != -1)
+ {
+ /* Bitfield. */
+ struct field *fld;
+ struct type *field_gdb_type;
+ int bitsize, total_size;
+
+ /* This invariant should be preserved while creating types. */
+ gdb_assert (this->size != 0);
+ if (f.type != NULL)
+ field_gdb_type = f.type->make_gdb_type (gdbarch);
+ else if (this->size > 4)
+ field_gdb_type = builtin_type (gdbarch)->builtin_uint64;
+ else
+ field_gdb_type = builtin_type (gdbarch)->builtin_uint32;
+
+ fld = append_composite_type_field_raw
+ (struct_gdb_type, xstrdup (f.name.c_str ()), field_gdb_type);
+
+ /* For little-endian, BITPOS counts from the LSB of
+ the structure and marks the LSB of the field. For
+ big-endian, BITPOS counts from the MSB of the
+ structure and marks the MSB of the field. Either
+ way, it is the number of bits to the "left" of the
+ field. To calculate this in big-endian, we need
+ the total size of the structure. */
+ bitsize = f.end - f.start + 1;
+ total_size = this->size * TARGET_CHAR_BIT;
+ if (gdbarch_bits_big_endian (gdbarch))
+ SET_FIELD_BITPOS (fld[0], total_size - f.start - bitsize);
+ else
+ SET_FIELD_BITPOS (fld[0], f.start);
+ FIELD_BITSIZE (fld[0]) = bitsize;
+ }
+ else
+ {
+ gdb_assert (f.start == -1 && f.end == -1);
+ type *field_gdb_type = f.type->make_gdb_type (gdbarch);
+ append_composite_type_field (struct_gdb_type,
+ xstrdup (f.name.c_str ()),
+ field_gdb_type);
+ }
+ }
+
+ if (this->size != 0)
+ TYPE_LENGTH (struct_gdb_type) = this->size;
+
+ return struct_gdb_type;
}

- bool operator!= (const tdesc_type &other) const
+ type *make_gdb_type_union (struct gdbarch *gdbarch) const
{
- return !(*this == other);
+ type *union_gdb_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
+ TYPE_NAME (union_gdb_type) = xstrdup (this->name.c_str ());
+
+ for (const tdesc_type_field &f : this->fields)
+ {
+ type* field_gdb_type = f.type->make_gdb_type (gdbarch);
+ append_composite_type_field (union_gdb_type, xstrdup (f.name.c_str ()),
+ field_gdb_type);
+
+ /* If any of the children of a union are vectors, flag the
+ union as a vector also. This allows e.g. a union of two
+ vector types to show up automatically in "info vector". */
+ if (TYPE_VECTOR (field_gdb_type))
+ TYPE_VECTOR (union_gdb_type) = 1;
+ }
+
+ return union_gdb_type;
}
-};

-typedef std::unique_ptr<tdesc_type> tdesc_type_up;
+ type *make_gdb_type_flags (struct gdbarch *gdbarch) const
+ {
+ type *flags_gdb_type = arch_flags_type (gdbarch, this->name.c_str (),
+ this->size * TARGET_CHAR_BIT);
+
+ for (const tdesc_type_field &f : this->fields)
+ {
+ int bitsize = f.end - f.start + 1;
+
+ gdb_assert (f.type != NULL);
+ type *field_gdb_type = f.type->make_gdb_type (gdbarch);
+ append_flags_type_field (flags_gdb_type, f.start, bitsize,
+ field_gdb_type, f.name.c_str ());
+ }
+
+ return flags_gdb_type;
+ }
+
+ type *make_gdb_type_enum (struct gdbarch *gdbarch) const
+ {
+ type *enum_gdb_type = arch_type (gdbarch, TYPE_CODE_ENUM,
+ this->size * TARGET_CHAR_BIT,
+ this->name.c_str ());
+
+ TYPE_UNSIGNED (enum_gdb_type) = 1;
+ for (const tdesc_type_field &f : this->fields)
+ {
+ struct field *fld
+ = append_composite_type_field_raw (enum_gdb_type,
+ xstrdup (f.name.c_str ()),
+ NULL);
+
+ SET_FIELD_BITPOS (fld[0], f.start);
+ }
+
+ return enum_gdb_type;
+ }
+
+ type *make_gdb_type (struct gdbarch *gdbarch) const override
+ {
+ type *gdb_type = tdesc_find_type (gdbarch, this->name.c_str ());
+ if (gdb_type != NULL)
+ return gdb_type;
+
+ switch (this->kind)
+ {
+ case TDESC_TYPE_STRUCT:
+ return make_gdb_type_struct (gdbarch);
+ case TDESC_TYPE_UNION:
+ return make_gdb_type_union (gdbarch);
+ case TDESC_TYPE_FLAGS:
+ return make_gdb_type_flags (gdbarch);
+ case TDESC_TYPE_ENUM:
+ return make_gdb_type_enum (gdbarch);
+ }
+
+ internal_error (__FILE__, __LINE__,
+ "Type \"%s\" has an unknown kind %d",
+ this->name.c_str (), this->kind);
+
+ return NULL;
+ }
+
+ std::vector<tdesc_type_field> fields;
+ int size;
+};

/* A feature from a target description. Each feature is a collection
of other elements, e.g. registers and types. */
@@ -725,7 +959,7 @@ tdesc_feature_name (const struct tdesc_feature *feature)
}

/* Predefined types. */
-static struct tdesc_type tdesc_predefined_types[] =
+static tdesc_type_builtin tdesc_predefined_types[] =
{
{ "bool", TDESC_TYPE_BOOL },
{ "int8", TDESC_TYPE_INT8 },
@@ -799,218 +1033,6 @@ tdesc_find_type (struct gdbarch *gdbarch, const char *id)
return NULL;
}

-/* Construct, if necessary, and return the GDB type implementing target
- type TDESC_TYPE for architecture GDBARCH. */
-
-static struct type *
-tdesc_gdb_type (struct gdbarch *gdbarch, struct tdesc_type *tdesc_type)
-{
- struct type *type;
-
- switch (tdesc_type->kind)
- {
- /* Predefined types. */
- case TDESC_TYPE_BOOL:
- return builtin_type (gdbarch)->builtin_bool;
-
- case TDESC_TYPE_INT8:
- return builtin_type (gdbarch)->builtin_int8;
-
- case TDESC_TYPE_INT16:
- return builtin_type (gdbarch)->builtin_int16;
-
- case TDESC_TYPE_INT32:
- return builtin_type (gdbarch)->builtin_int32;
-
- case TDESC_TYPE_INT64:
- return builtin_type (gdbarch)->builtin_int64;
-
- case TDESC_TYPE_INT128:
- return builtin_type (gdbarch)->builtin_int128;
-
- case TDESC_TYPE_UINT8:
- return builtin_type (gdbarch)->builtin_uint8;
-
- case TDESC_TYPE_UINT16:
- return builtin_type (gdbarch)->builtin_uint16;
-
- case TDESC_TYPE_UINT32:
- return builtin_type (gdbarch)->builtin_uint32;
-
- case TDESC_TYPE_UINT64:
- return builtin_type (gdbarch)->builtin_uint64;
-
- case TDESC_TYPE_UINT128:
- return builtin_type (gdbarch)->builtin_uint128;
-
- case TDESC_TYPE_CODE_PTR:
- return builtin_type (gdbarch)->builtin_func_ptr;
-
- case TDESC_TYPE_DATA_PTR:
- return builtin_type (gdbarch)->builtin_data_ptr;
-
- default:
- break;
- }
-
- type = tdesc_find_type (gdbarch, tdesc_type->name.c_str ());
- if (type)
- return type;
-
- switch (tdesc_type->kind)
- {
- case TDESC_TYPE_IEEE_SINGLE:
- return arch_float_type (gdbarch, -1, "builtin_type_ieee_single",
- floatformats_ieee_single);
-
- case TDESC_TYPE_IEEE_DOUBLE:
- return arch_float_type (gdbarch, -1, "builtin_type_ieee_double",
- floatformats_ieee_double);
-
- case TDESC_TYPE_ARM_FPA_EXT:
- return arch_float_type (gdbarch, -1, "builtin_type_arm_ext",
- floatformats_arm_ext);
-
- case TDESC_TYPE_I387_EXT:
- return arch_float_type (gdbarch, -1, "builtin_type_i387_ext",
- floatformats_i387_ext);
-
- /* Types defined by a target feature. */
- case TDESC_TYPE_VECTOR:
- {
- struct type *type, *field_type;
-
- field_type = tdesc_gdb_type (gdbarch, tdesc_type->u.v.type);
- type = init_vector_type (field_type, tdesc_type->u.v.count);
- TYPE_NAME (type) = xstrdup (tdesc_type->name.c_str ());
-
- return type;
- }
-
- case TDESC_TYPE_STRUCT:
- {
- struct type *type, *field_type;
-
- type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
- TYPE_NAME (type) = xstrdup (tdesc_type->name.c_str ());
- TYPE_TAG_NAME (type) = TYPE_NAME (type);
-
- for (const tdesc_type_field &f : *tdesc_type->u.u.fields)
- {
- if (f.start != -1 && f.end != -1)
- {
- /* Bitfield. */
- struct field *fld;
- struct type *field_type;
- int bitsize, total_size;
-
- /* This invariant should be preserved while creating types. */
- gdb_assert (tdesc_type->u.u.size != 0);
- if (f.type != NULL)
- field_type = tdesc_gdb_type (gdbarch, f.type);
- else if (tdesc_type->u.u.size > 4)
- field_type = builtin_type (gdbarch)->builtin_uint64;
- else
- field_type = builtin_type (gdbarch)->builtin_uint32;
-
- fld = append_composite_type_field_raw
- (type, xstrdup (f.name.c_str ()), field_type);
-
- /* For little-endian, BITPOS counts from the LSB of
- the structure and marks the LSB of the field. For
- big-endian, BITPOS counts from the MSB of the
- structure and marks the MSB of the field. Either
- way, it is the number of bits to the "left" of the
- field. To calculate this in big-endian, we need
- the total size of the structure. */
- bitsize = f.end - f.start + 1;
- total_size = tdesc_type->u.u.size * TARGET_CHAR_BIT;
- if (gdbarch_bits_big_endian (gdbarch))
- SET_FIELD_BITPOS (fld[0], total_size - f.start - bitsize);
- else
- SET_FIELD_BITPOS (fld[0], f.start);
- FIELD_BITSIZE (fld[0]) = bitsize;
- }
- else
- {
- gdb_assert (f.start == -1 && f.end == -1);
- field_type = tdesc_gdb_type (gdbarch, f.type);
- append_composite_type_field (type, xstrdup (f.name.c_str ()),
- field_type);
- }
- }
-
- if (tdesc_type->u.u.size != 0)
- TYPE_LENGTH (type) = tdesc_type->u.u.size;
- return type;
- }
-
- case TDESC_TYPE_UNION:
- {
- struct type *type, *field_type;
-
- type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
- TYPE_NAME (type) = xstrdup (tdesc_type->name.c_str ());
-
- for (const tdesc_type_field &f : *tdesc_type->u.u.fields)
- {
- field_type = tdesc_gdb_type (gdbarch, f.type);
- append_composite_type_field (type, xstrdup (f.name.c_str ()),
- field_type);
-
- /* If any of the children of a union are vectors, flag the
- union as a vector also. This allows e.g. a union of two
- vector types to show up automatically in "info vector". */
- if (TYPE_VECTOR (field_type))
- TYPE_VECTOR (type) = 1;
- }
- return type;
- }
-
- case TDESC_TYPE_FLAGS:
- {
- type = arch_flags_type (gdbarch, tdesc_type->name.c_str (),
- tdesc_type->u.u.size * TARGET_CHAR_BIT);
- for (const tdesc_type_field &f : *tdesc_type->u.u.fields)
- {
- struct type *field_type;
- int bitsize = f.end - f.start + 1;
-
- gdb_assert (f.type != NULL);
- field_type = tdesc_gdb_type (gdbarch, f.type);
- append_flags_type_field (type, f.start, bitsize,
- field_type, f.name.c_str ());
- }
-
- return type;
- }
-
- case TDESC_TYPE_ENUM:
- {
- type = arch_type (gdbarch, TYPE_CODE_ENUM,
- tdesc_type->u.u.size * TARGET_CHAR_BIT,
- tdesc_type->name.c_str ());
- TYPE_UNSIGNED (type) = 1;
- for (const tdesc_type_field &f : *tdesc_type->u.u.fields)
- {
- struct field *fld
- = append_composite_type_field_raw (type,
- xstrdup (f.name.c_str ()),
- NULL);
-
- SET_FIELD_BITPOS (fld[0], f.start);
- }
-
- return type;
- }
- }
-
- internal_error (__FILE__, __LINE__,
- "Type \"%s\" has an unknown kind %d",
- tdesc_type->name.c_str (), tdesc_type->kind);
-}
-\f
-
/* Support for registers from target descriptions. */

/* Construct the per-gdbarch data. */
@@ -1198,7 +1220,7 @@ tdesc_register_type (struct gdbarch *gdbarch, int regno)
{
/* First check for a predefined or target defined type. */
if (reg->tdesc_type)
- arch_reg->type = tdesc_gdb_type (gdbarch, reg->tdesc_type);
+ arch_reg->type = reg->tdesc_type->make_gdb_type (gdbarch);

/* Next try size-sensitive type shortcuts. */
else if (reg->type == "float")
@@ -1458,81 +1480,78 @@ struct tdesc_type *
tdesc_create_vector (struct tdesc_feature *feature, const char *name,
struct tdesc_type *field_type, int count)
{
- struct tdesc_type *type = new tdesc_type (name, TDESC_TYPE_VECTOR);
-
- type->u.v.type = field_type;
- type->u.v.count = count;
-
+ tdesc_type_vector *type = new tdesc_type_vector (name, field_type, count);
feature->types.emplace_back (type);
+
return type;
}

/* See arch/tdesc.h. */

-struct tdesc_type *
+tdesc_type_with_fields *
tdesc_create_struct (struct tdesc_feature *feature, const char *name)
{
- struct tdesc_type *type = new tdesc_type (name, TDESC_TYPE_STRUCT);
-
+ tdesc_type_with_fields *type
+ = new tdesc_type_with_fields (name, TDESC_TYPE_STRUCT);
feature->types.emplace_back (type);
+
return type;
}

/* See arch/tdesc.h. */

void
-tdesc_set_struct_size (struct tdesc_type *type, int size)
+tdesc_set_struct_size (tdesc_type_with_fields *type, int size)
{
gdb_assert (type->kind == TDESC_TYPE_STRUCT);
gdb_assert (size > 0);
- type->u.u.size = size;
+ type->size = size;
}

/* See arch/tdesc.h. */

-struct tdesc_type *
+tdesc_type_with_fields *
tdesc_create_union (struct tdesc_feature *feature, const char *name)
{
- struct tdesc_type *type = new tdesc_type (name, TDESC_TYPE_UNION);
-
+ tdesc_type_with_fields *type
+ = new tdesc_type_with_fields (name, TDESC_TYPE_UNION);
feature->types.emplace_back (type);
+
return type;
}

/* See arch/tdesc.h. */

-struct tdesc_type *
+tdesc_type_with_fields *
tdesc_create_flags (struct tdesc_feature *feature, const char *name,
int size)
{
- struct tdesc_type *type = new tdesc_type (name, TDESC_TYPE_FLAGS);
-
gdb_assert (size > 0);

- type->u.u.size = size;
-
+ tdesc_type_with_fields *type
+ = new tdesc_type_with_fields (name, TDESC_TYPE_FLAGS, size);
feature->types.emplace_back (type);
+
return type;
}

-struct tdesc_type *
+tdesc_type_with_fields *
tdesc_create_enum (struct tdesc_feature *feature, const char *name,
int size)
{
- struct tdesc_type *type = new tdesc_type (name, TDESC_TYPE_ENUM);
-
gdb_assert (size > 0);

- type->u.u.size = size;
-
+ tdesc_type_with_fields *type
+ = new tdesc_type_with_fields (name, TDESC_TYPE_ENUM, size);
feature->types.emplace_back (type);
+
return type;
}

/* See arch/tdesc.h. */

void
-tdesc_add_field (struct tdesc_type *type, const char *field_name,
+tdesc_add_field (tdesc_type_with_fields *type, const char *field_name,
struct tdesc_type *field_type)
{
gdb_assert (type->kind == TDESC_TYPE_UNION
@@ -1540,31 +1559,31 @@ tdesc_add_field (struct tdesc_type *type, const char *field_name,

/* Initialize start and end so we know this is not a bit-field
when we print-c-tdesc. */
- type->u.u.fields->emplace_back (field_name, field_type, -1, -1);
+ type->fields.emplace_back (field_name, field_type, -1, -1);
}

void
-tdesc_add_typed_bitfield (struct tdesc_type *type, const char *field_name,
+tdesc_add_typed_bitfield (tdesc_type_with_fields *type, const char *field_name,
int start, int end, struct tdesc_type *field_type)
{
gdb_assert (type->kind == TDESC_TYPE_STRUCT
|| type->kind == TDESC_TYPE_FLAGS);
gdb_assert (start >= 0 && end >= start);

- type->u.u.fields->emplace_back (field_name, field_type, start, end);
+ type->fields.emplace_back (field_name, field_type, start, end);
}

/* See arch/tdesc.h. */

void
-tdesc_add_bitfield (struct tdesc_type *type, const char *field_name,
+tdesc_add_bitfield (tdesc_type_with_fields *type, const char *field_name,
int start, int end)
{
struct tdesc_type *field_type;

gdb_assert (start >= 0 && end >= start);

- if (type->u.u.size > 4)
+ if (type->size > 4)
field_type = tdesc_predefined_type (TDESC_TYPE_UINT64);
else
field_type = tdesc_predefined_type (TDESC_TYPE_UINT32);
@@ -1575,26 +1594,25 @@ tdesc_add_bitfield (struct tdesc_type *type, const char *field_name,
/* See arch/tdesc.h. */

void
-tdesc_add_flag (struct tdesc_type *type, int start,
+tdesc_add_flag (tdesc_type_with_fields *type, int start,
const char *flag_name)
{
gdb_assert (type->kind == TDESC_TYPE_FLAGS
|| type->kind == TDESC_TYPE_STRUCT);

- type->u.u.fields->emplace_back (flag_name,
- tdesc_predefined_type (TDESC_TYPE_BOOL),
- start, start);
+ type->fields.emplace_back (flag_name,
+ tdesc_predefined_type (TDESC_TYPE_BOOL),
+ start, start);
}

void
-tdesc_add_enum_value (struct tdesc_type *type, int value,
+tdesc_add_enum_value (tdesc_type_with_fields *type, int value,
const char *name)
{
gdb_assert (type->kind == TDESC_TYPE_ENUM);
-
- type->u.u.fields->emplace_back (name,
- tdesc_predefined_type (TDESC_TYPE_INT32),
- value, -1);
+ type->fields.emplace_back (name,
+ tdesc_predefined_type (TDESC_TYPE_INT32),
+ value, -1);
}

/* See arch/tdesc.h. */
@@ -1848,38 +1866,46 @@ public:
printf_unfiltered ("}\n");
}

- void visit (const tdesc_type *type) override
+ void visit (const tdesc_type_builtin *type) override
+ {
+ error (_("C output is not supported type \"%s\"."), type->name.c_str ());
+ }
+
+ void visit (const tdesc_type_vector *type) override
{
- /* Now we do some "filtering" in order to know which variables to
- declare. This is needed because otherwise we would declare unused
- variables `field_type' and `type'. */
if (!m_printed_field_type)
{
printf_unfiltered (" struct tdesc_type *field_type;\n");
m_printed_field_type = true;
}

- if ((type->kind == TDESC_TYPE_UNION
- || type->kind == TDESC_TYPE_STRUCT
- || type->kind == TDESC_TYPE_FLAGS
- || type->kind == TDESC_TYPE_ENUM)
- && !type->u.u.fields->empty ()
- && !m_printed_type)
+ printf_unfiltered
+ (" field_type = tdesc_named_type (feature, \"%s\");\n",
+ type->element_type->name.c_str ());
+ printf_unfiltered
+ (" tdesc_create_vector (feature, \"%s\", field_type, %d);\n",
+ type->name.c_str (), type->count);
+
+ printf_unfiltered ("\n");
+ }
+
+ void visit (const tdesc_type_with_fields *type) override
+ {
+ if (!m_printed_type)
{
- printf_unfiltered (" struct tdesc_type *type;\n");
+ printf_unfiltered (" tdesc_type_with_fields *type;\n");
m_printed_type = true;
}

+ if (!type->fields.empty ()
+ && !m_printed_field_type)
+ {
+ printf_unfiltered (" struct tdesc_type *field_type;\n");
+ m_printed_field_type = true;
+ }
+
switch (type->kind)
{
- case TDESC_TYPE_VECTOR:
- printf_unfiltered
- (" field_type = tdesc_named_type (feature, \"%s\");\n",
- type->u.v.type->name.c_str ());
- printf_unfiltered
- (" tdesc_create_vector (feature, \"%s\", field_type, %d);\n",
- type->name.c_str (), type->u.v.count);
- break;
case TDESC_TYPE_STRUCT:
case TDESC_TYPE_FLAGS:
if (type->kind == TDESC_TYPE_STRUCT)
@@ -1887,18 +1913,17 @@ public:
printf_unfiltered
(" type = tdesc_create_struct (feature, \"%s\");\n",
type->name.c_str ());
- if (type->u.u.size != 0)
+ if (type->size != 0)
printf_unfiltered
- (" tdesc_set_struct_size (type, %d);\n",
- type->u.u.size);
+ (" tdesc_set_struct_size (type, %d);\n", type->size);
}
else
{
printf_unfiltered
(" type = tdesc_create_flags (feature, \"%s\", %d);\n",
- type->name.c_str (), type->u.u.size);
+ type->name.c_str (), type->size);
}
- for (const tdesc_type_field &f : *type->u.u.fields)
+ for (const tdesc_type_field &f : type->fields)
{
const char *type_name;

@@ -1917,9 +1942,8 @@ public:
(" tdesc_add_flag (type, %d, \"%s\");\n",
f.start, f.name.c_str ());
}
- else if ((type->u.u.size == 4
- && f.type->kind == TDESC_TYPE_UINT32)
- || (type->u.u.size == 8
+ else if ((type->size == 4 && f.type->kind == TDESC_TYPE_UINT32)
+ || (type->size == 8
&& f.type->kind == TDESC_TYPE_UINT64))
{
printf_unfiltered
@@ -1929,8 +1953,7 @@ public:
else
{
printf_unfiltered
- (" field_type = tdesc_named_type (feature,"
- " \"%s\");\n",
+ (" field_type = tdesc_named_type (feature, \"%s\");\n",
type_name);
printf_unfiltered
(" tdesc_add_typed_bitfield (type, \"%s\","
@@ -1956,7 +1979,7 @@ public:
printf_unfiltered
(" type = tdesc_create_union (feature, \"%s\");\n",
type->name.c_str ());
- for (const tdesc_type_field &f : *type->u.u.fields)
+ for (const tdesc_type_field &f : type->fields)
{
printf_unfiltered
(" field_type = tdesc_named_type (feature, \"%s\");\n",
@@ -1969,8 +1992,8 @@ public:
case TDESC_TYPE_ENUM:
printf_unfiltered
(" type = tdesc_create_enum (feature, \"%s\", %d);\n",
- type->name.c_str (), type->u.u.size);
- for (const tdesc_type_field &f : *type->u.u.fields)
+ type->name.c_str (), type->size);
+ for (const tdesc_type_field &f : type->fields)
printf_unfiltered
(" tdesc_add_enum_value (type, %d, \"%s\");\n",
f.start, f.name.c_str ());
@@ -1978,6 +2001,7 @@ public:
default:
error (_("C output is not supported type \"%s\"."), type->name.c_str ());
}
+
printf_unfiltered ("\n");
}

@@ -1998,7 +2022,11 @@ protected:

private:
char *m_function;
+
+ /* Did we print "struct tdesc_type *field_type;" yet? */
bool m_printed_field_type = false;
+
+ /* Did we print "struct tdesc_type_with_fields *type;" yet? */
bool m_printed_type = false;
};

diff --git a/gdb/target-descriptions.h b/gdb/target-descriptions.h
index e97a3738e2..8465c9ea69 100644
--- a/gdb/target-descriptions.h
+++ b/gdb/target-descriptions.h
@@ -209,13 +209,13 @@ void set_tdesc_property (struct target_desc *,
const char *key, const char *value);
void tdesc_add_compatible (struct target_desc *,
const struct bfd_arch_info *);
-struct tdesc_type *tdesc_create_enum (struct tdesc_feature *feature,
- const char *name,
- int size);
-void tdesc_add_typed_bitfield (struct tdesc_type *type, const char *field_name,
+tdesc_type_with_fields *tdesc_create_enum (struct tdesc_feature *feature,
+ const char *name,
+ int size);
+void tdesc_add_typed_bitfield (tdesc_type_with_fields *type, const char *field_name,
int start, int end,
struct tdesc_type *field_type);
-void tdesc_add_enum_value (struct tdesc_type *type, int value,
+void tdesc_add_enum_value (tdesc_type_with_fields *type, int value,
const char *name);

#if GDB_SELF_TEST
diff --git a/gdb/xml-tdesc.c b/gdb/xml-tdesc.c
index 5a1f459f98..6baeadfdff 100644
--- a/gdb/xml-tdesc.c
+++ b/gdb/xml-tdesc.c
@@ -83,7 +83,7 @@ struct tdesc_parsing_data
int next_regnum;

/* The struct or union we are currently parsing, or last parsed. */
- struct tdesc_type *current_type;
+ tdesc_type_with_fields *current_type;

/* The byte size of the current struct/flags type, if specified. Zero
if not specified. Flags values must specify a size. */
@@ -244,11 +244,11 @@ tdesc_start_struct (struct gdb_xml_parser *parser,
{
struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
char *id = (char *) xml_find_attribute (attributes, "id")->value;
- struct tdesc_type *type;
struct gdb_xml_value *attr;

- type = tdesc_create_struct (data->current_feature, id);
- data->current_type = type;
+ tdesc_type_with_fields *type_with_fields
+ = tdesc_create_struct (data->current_feature, id);
+ data->current_type = type_with_fields;
data->current_type_size = 0;

attr = xml_find_attribute (attributes, "size");
@@ -262,7 +262,7 @@ tdesc_start_struct (struct gdb_xml_parser *parser,
_("Struct size %s is larger than maximum (%d)"),
pulongest (size), MAX_FIELD_SIZE);
}
- tdesc_set_struct_size (type, size);
+ tdesc_set_struct_size (type_with_fields, size);
data->current_type_size = size;
}
}
@@ -276,7 +276,6 @@ tdesc_start_flags (struct gdb_xml_parser *parser,
char *id = (char *) xml_find_attribute (attributes, "id")->value;
ULONGEST size = * (ULONGEST *)
xml_find_attribute (attributes, "size")->value;
- struct tdesc_type *type;

if (size > MAX_FIELD_SIZE)
{
@@ -284,9 +283,8 @@ tdesc_start_flags (struct gdb_xml_parser *parser,
_("Flags size %s is larger than maximum (%d)"),
pulongest (size), MAX_FIELD_SIZE);
}
- type = tdesc_create_flags (data->current_feature, id, size);

- data->current_type = type;
+ data->current_type = tdesc_create_flags (data->current_feature, id, size);
data->current_type_size = size;
}

@@ -299,7 +297,6 @@ tdesc_start_enum (struct gdb_xml_parser *parser,
char *id = (char *) xml_find_attribute (attributes, "id")->value;
int size = * (ULONGEST *)
xml_find_attribute (attributes, "size")->value;
- struct tdesc_type *type;

if (size > MAX_FIELD_SIZE)
{
@@ -307,9 +304,8 @@ tdesc_start_enum (struct gdb_xml_parser *parser,
_("Enum size %s is larger than maximum (%d)"),
pulongest (size), MAX_FIELD_SIZE);
}
- type = tdesc_create_enum (data->current_feature, id, size);

- data->current_type = type;
+ data->current_type = tdesc_create_enum (data->current_feature, id, size);
data->current_type_size = 0;
}

@@ -375,7 +371,7 @@ tdesc_start_field (struct gdb_xml_parser *parser,

if (start != -1)
{
- struct tdesc_type *t = data->current_type;
+ tdesc_type_with_fields *t = data->current_type;

/* Older versions of gdb can't handle elided end values.
Stick with that for now, to help ensure backward compatibility.
--
2.15.0