aboutsummaryrefslogtreecommitdiff
path: root/gdb/opencl-lang.c
diff options
context:
space:
mode:
authorAndrew Burgess <andrew.burgess@embecosm.com>2020-10-30 20:40:59 +0000
committerAndrew Burgess <andrew.burgess@embecosm.com>2020-11-12 23:36:25 +0000
commit7bea47f0012c208d935c50d3b7ce9af7d34482b7 (patch)
tree2af27e112306612fdb6c55597687ef6640f312e0 /gdb/opencl-lang.c
parentbf6e5d01d7b149e116a008bd4348983c6f56e9ba (diff)
downloadgdb-7bea47f0012c208d935c50d3b7ce9af7d34482b7.zip
gdb-7bea47f0012c208d935c50d3b7ce9af7d34482b7.tar.gz
gdb-7bea47f0012c208d935c50d3b7ce9af7d34482b7.tar.bz2
gdb: rewrite how per language primitive types are managed
Consider the following GDB session: $ gdb (gdb) set language c (gdb) ptype void type = void (gdb) set language fortran (gdb) ptype void No symbol table is loaded. Use the "file" command. (gdb) With no symbol file loaded GDB and the language set to C GDB knows about the type void, while when the language is set to Fortran GDB doesn't know about the void, why is that? In f-lang.c, f_language::language_arch_info, we do have this line: lai->primitive_type_vector [f_primitive_type_void] = builtin->builtin_void; where we add the void type to the list of primitive types that GDB should always know about, so what's going wrong? It turns out that the primitive types are stored in a C style array, indexed by an enum, so Fortran uses `enum f_primitive_types'. The array is allocated and populated in each languages language_arch_info member function. The array is allocated with an extra entry at the end which is left as a NULL value, and this indicates the end of the array of types. Unfortunately for Fortran, a type is not assigned for each element in the enum. As a result the final populated array has gaps in it, gaps which are initialised to NULL, and so every time we iterate over the list (for Fortran) we stop early, and never reach the void type. This has been the case since 2007 when this functionality was added to GDB in commit cad351d11d6c3f6487cd. Obviously I could just fix Fortran by ensuring that either the enum is trimmed, or we create types for the missing types. However, I think a better approach would be to move to C++ data structures and removed the fixed enum indexing into the array approach. After this commit the primitive types are pushed into a vector, and GDB just iterates over the vector in the obvious way when it needs to hunt for a type. After this commit all the currently defined primitive types can be found when the language is set to Fortran, for example: $ gdb (gdb) set language fortran (gdb) ptype void type = void (gdb) A new test checks this functionality. I didn't see any other languages with similar issues, but I could have missed something. gdb/ChangeLog: * ada-exp.y (find_primitive_type): Make parameter const. * ada-lang.c (enum ada_primitive_types): Delete. (ada_language::language_arch_info): Update. * c-lang.c (enum c_primitive_types): Delete. (c_language_arch_info): Update. (enum cplus_primitive_types): Delete. (cplus_language::language_arch_info): Update. * d-lang.c (enum d_primitive_types): Delete. (d_language::language_arch_info): Update. * f-lang.c (enum f_primitive_types): Delete. (f_language::language_arch_info): Update. * go-lang.c (enum go_primitive_types): Delete. (go_language::language_arch_info): Update. * language.c (auto_or_unknown_language::language_arch_info): Update. (language_gdbarch_post_init): Use obstack_new, use array indexing. (language_string_char_type): Add header comment, call function in language_arch_info. (language_bool_type): Likewise (language_arch_info::bool_type): Define. (language_lookup_primitive_type_1): Delete. (language_lookup_primitive_type): Rewrite as a templated function to call function in language_arch_info, then instantiate twice. (language_arch_info::type_and_symbol::alloc_type_symbol): Define. (language_arch_info::lookup_primitive_type_and_symbol): Define. (language_arch_info::lookup_primitive_type): Define twice with different signatures. (language_arch_info::lookup_primitive_type_as_symbol): Define. (language_lookup_primitive_type_as_symbol): Rewrite to call a member function in language_arch_info. * language.h (language_arch_info): Complete rewrite. (language_lookup_primitive_type): Make templated. * m2-lang.c (enum m2_primitive_types): Delete. (m2_language::language_arch_info): Update. * opencl-lang.c (OCL_P_TYPE): Delete. (enum opencl_primitive_types): Delete. (opencl_type_data): Delete. (builtin_opencl_type): Delete. (lookup_opencl_vector_type): Update. (opencl_language::language_arch_info): Update, lots of content moved from... (build_opencl_types): ...here. This function is now deleted. (_initialize_opencl_language): Delete. * p-lang.c (enum pascal_primitive_types): Delete. (pascal_language::language_arch_info): Update. * rust-lang.c (enum rust_primitive_types): Delete. (rust_language::language_arch_info): Update. gdb/testsuite/ChangeLog: * gdb.fortran/types.exp: Add more tests.
Diffstat (limited to 'gdb/opencl-lang.c')
-rw-r--r--gdb/opencl-lang.c244
1 files changed, 80 insertions, 164 deletions
diff --git a/gdb/opencl-lang.c b/gdb/opencl-lang.c
index 38b98de..11032b1 100644
--- a/gdb/opencl-lang.c
+++ b/gdb/opencl-lang.c
@@ -28,49 +28,6 @@
#include "c-lang.h"
#include "gdbarch.h"
-/* This macro generates enum values from a given type. */
-
-#define OCL_P_TYPE(TYPE)\
- opencl_primitive_type_##TYPE,\
- opencl_primitive_type_##TYPE##2,\
- opencl_primitive_type_##TYPE##3,\
- opencl_primitive_type_##TYPE##4,\
- opencl_primitive_type_##TYPE##8,\
- opencl_primitive_type_##TYPE##16
-
-enum opencl_primitive_types {
- OCL_P_TYPE (char),
- OCL_P_TYPE (uchar),
- OCL_P_TYPE (short),
- OCL_P_TYPE (ushort),
- OCL_P_TYPE (int),
- OCL_P_TYPE (uint),
- OCL_P_TYPE (long),
- OCL_P_TYPE (ulong),
- OCL_P_TYPE (half),
- OCL_P_TYPE (float),
- OCL_P_TYPE (double),
- opencl_primitive_type_bool,
- opencl_primitive_type_unsigned_char,
- opencl_primitive_type_unsigned_short,
- opencl_primitive_type_unsigned_int,
- opencl_primitive_type_unsigned_long,
- opencl_primitive_type_size_t,
- opencl_primitive_type_ptrdiff_t,
- opencl_primitive_type_intptr_t,
- opencl_primitive_type_uintptr_t,
- opencl_primitive_type_void,
- nr_opencl_primitive_types
-};
-
-static struct gdbarch_data *opencl_type_data;
-
-static struct type **
-builtin_opencl_type (struct gdbarch *gdbarch)
-{
- return (struct type **) gdbarch_data (gdbarch, opencl_type_data);
-}
-
/* Returns the corresponding OpenCL vector type from the given type code,
the length of the element type, the unsigned flag and the amount of
elements (N). */
@@ -80,10 +37,7 @@ lookup_opencl_vector_type (struct gdbarch *gdbarch, enum type_code code,
unsigned int el_length, unsigned int flag_unsigned,
int n)
{
- int i;
unsigned int length;
- struct type *type = NULL;
- struct type **types = builtin_opencl_type (gdbarch);
/* Check if n describes a valid OpenCL vector size (2, 3, 4, 8, 16). */
if (n != 2 && n != 3 && n != 4 && n != 8 && n != 16)
@@ -92,24 +46,20 @@ lookup_opencl_vector_type (struct gdbarch *gdbarch, enum type_code code,
/* Triple vectors have the size of a quad vector. */
length = (n == 3) ? el_length * 4 : el_length * n;
- for (i = 0; i < nr_opencl_primitive_types; i++)
- {
- LONGEST lowb, highb;
-
- if (types[i]->code () == TYPE_CODE_ARRAY && types[i]->is_vector ()
- && get_array_bounds (types[i], &lowb, &highb)
- && TYPE_TARGET_TYPE (types[i])->code () == code
- && TYPE_TARGET_TYPE (types[i])->is_unsigned () == flag_unsigned
- && TYPE_LENGTH (TYPE_TARGET_TYPE (types[i])) == el_length
- && TYPE_LENGTH (types[i]) == length
- && highb - lowb + 1 == n)
- {
- type = types[i];
- break;
- }
- }
-
- return type;
+ std::function<bool (struct type *)> filter = [&] (struct type *type)
+ {
+ LONGEST lowb, highb;
+
+ return (type->code () == TYPE_CODE_ARRAY && type->is_vector ()
+ && get_array_bounds (type, &lowb, &highb)
+ && TYPE_TARGET_TYPE (type)->code () == code
+ && TYPE_TARGET_TYPE (type)->is_unsigned () == flag_unsigned
+ && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) == el_length
+ && TYPE_LENGTH (type) == length
+ && highb - lowb + 1 == n);
+ };
+ const struct language_defn *lang = language_def (language_opencl);
+ return language_lookup_primitive_type (lang, gdbarch, filter);
}
/* Returns nonzero if the array ARR contains duplicates within
@@ -1026,17 +976,77 @@ public:
void language_arch_info (struct gdbarch *gdbarch,
struct language_arch_info *lai) const override
{
- struct type **types = builtin_opencl_type (gdbarch);
+ /* Helper function to allow shorter lines below. */
+ auto add = [&] (struct type * t) -> struct type *
+ {
+ lai->add_primitive_type (t);
+ return t;
+ };
- /* Copy primitive types vector from gdbarch. */
- lai->primitive_type_vector = types;
+/* Helper macro to create strings. */
+#define OCL_STRING(S) #S
+
+/* This macro allocates and assigns the type struct pointers
+ for the vector types. */
+#define BUILD_OCL_VTYPES(TYPE, ELEMENT_TYPE) \
+ do \
+ { \
+ struct type *tmp; \
+ tmp = add (init_vector_type (ELEMENT_TYPE, 2)); \
+ tmp->set_name (OCL_STRING(TYPE ## 2)); \
+ tmp = add (init_vector_type (ELEMENT_TYPE, 3)); \
+ tmp->set_name (OCL_STRING(TYPE ## 3)); \
+ TYPE_LENGTH (tmp) = 4 * TYPE_LENGTH (ELEMENT_TYPE); \
+ tmp = add (init_vector_type (ELEMENT_TYPE, 4)); \
+ tmp->set_name (OCL_STRING(TYPE ## 4)); \
+ tmp = add (init_vector_type (ELEMENT_TYPE, 8)); \
+ tmp->set_name (OCL_STRING(TYPE ## 8)); \
+ tmp = init_vector_type (ELEMENT_TYPE, 16); \
+ tmp->set_name (OCL_STRING(TYPE ## 16)); \
+ } \
+ while (false)
+
+ struct type *el_type, *char_type, *int_type;
+
+ char_type = el_type = add (arch_integer_type (gdbarch, 8, 0, "char"));
+ BUILD_OCL_VTYPES (char, el_type);
+ el_type = add (arch_integer_type (gdbarch, 8, 1, "uchar"));
+ BUILD_OCL_VTYPES (uchar, el_type);
+ el_type = add (arch_integer_type (gdbarch, 16, 0, "short"));
+ BUILD_OCL_VTYPES (short, el_type);
+ el_type = add (arch_integer_type (gdbarch, 16, 1, "ushort"));
+ BUILD_OCL_VTYPES (ushort, el_type);
+ int_type = el_type = add (arch_integer_type (gdbarch, 32, 0, "int"));
+ BUILD_OCL_VTYPES (int, el_type);
+ el_type = add (arch_integer_type (gdbarch, 32, 1, "uint"));
+ BUILD_OCL_VTYPES (uint, el_type);
+ el_type = add (arch_integer_type (gdbarch, 64, 0, "long"));
+ BUILD_OCL_VTYPES (long, el_type);
+ el_type = add (arch_integer_type (gdbarch, 64, 1, "ulong"));
+ BUILD_OCL_VTYPES (ulong, el_type);
+ el_type = add (arch_float_type (gdbarch, 16, "half", floatformats_ieee_half));
+ BUILD_OCL_VTYPES (half, el_type);
+ el_type = add (arch_float_type (gdbarch, 32, "float", floatformats_ieee_single));
+ BUILD_OCL_VTYPES (float, el_type);
+ el_type = add (arch_float_type (gdbarch, 64, "double", floatformats_ieee_double));
+ BUILD_OCL_VTYPES (double, el_type);
+
+ add (arch_boolean_type (gdbarch, 8, 1, "bool"));
+ add (arch_integer_type (gdbarch, 8, 1, "unsigned char"));
+ add (arch_integer_type (gdbarch, 16, 1, "unsigned short"));
+ add (arch_integer_type (gdbarch, 32, 1, "unsigned int"));
+ add (arch_integer_type (gdbarch, 64, 1, "unsigned long"));
+ add (arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch), 1, "size_t"));
+ add (arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch), 0, "ptrdiff_t"));
+ add (arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch), 0, "intptr_t"));
+ add (arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch), 1, "uintptr_t"));
+ add (arch_type (gdbarch, TYPE_CODE_VOID, TARGET_CHAR_BIT, "void"));
/* Type of elements of strings. */
- lai->string_char_type = types [opencl_primitive_type_char];
+ lai->set_string_char_type (char_type);
/* Specifies the return type of logical and relational operations. */
- lai->bool_type_symbol = "int";
- lai->bool_type_default = types [opencl_primitive_type_int];
+ lai->set_bool_type (int_type, "int");
}
/* See language.h. */
@@ -1078,97 +1088,3 @@ public:
/* Single instance of the OpenCL language class. */
static opencl_language opencl_language_defn;
-
-static void *
-build_opencl_types (struct gdbarch *gdbarch)
-{
- struct type **types
- = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_opencl_primitive_types + 1,
- struct type *);
-
-/* Helper macro to create strings. */
-#define OCL_STRING(S) #S
-/* This macro allocates and assigns the type struct pointers
- for the vector types. */
-#define BUILD_OCL_VTYPES(TYPE)\
- types[opencl_primitive_type_##TYPE##2] \
- = init_vector_type (types[opencl_primitive_type_##TYPE], 2); \
- types[opencl_primitive_type_##TYPE##2]->set_name (OCL_STRING(TYPE ## 2)); \
- types[opencl_primitive_type_##TYPE##3] \
- = init_vector_type (types[opencl_primitive_type_##TYPE], 3); \
- types[opencl_primitive_type_##TYPE##3]->set_name (OCL_STRING(TYPE ## 3)); \
- TYPE_LENGTH (types[opencl_primitive_type_##TYPE##3]) \
- = 4 * TYPE_LENGTH (types[opencl_primitive_type_##TYPE]); \
- types[opencl_primitive_type_##TYPE##4] \
- = init_vector_type (types[opencl_primitive_type_##TYPE], 4); \
- types[opencl_primitive_type_##TYPE##4]->set_name (OCL_STRING(TYPE ## 4)); \
- types[opencl_primitive_type_##TYPE##8] \
- = init_vector_type (types[opencl_primitive_type_##TYPE], 8); \
- types[opencl_primitive_type_##TYPE##8]->set_name (OCL_STRING(TYPE ## 8)); \
- types[opencl_primitive_type_##TYPE##16] \
- = init_vector_type (types[opencl_primitive_type_##TYPE], 16); \
- types[opencl_primitive_type_##TYPE##16]->set_name (OCL_STRING(TYPE ## 16))
-
- types[opencl_primitive_type_char]
- = arch_integer_type (gdbarch, 8, 0, "char");
- BUILD_OCL_VTYPES (char);
- types[opencl_primitive_type_uchar]
- = arch_integer_type (gdbarch, 8, 1, "uchar");
- BUILD_OCL_VTYPES (uchar);
- types[opencl_primitive_type_short]
- = arch_integer_type (gdbarch, 16, 0, "short");
- BUILD_OCL_VTYPES (short);
- types[opencl_primitive_type_ushort]
- = arch_integer_type (gdbarch, 16, 1, "ushort");
- BUILD_OCL_VTYPES (ushort);
- types[opencl_primitive_type_int]
- = arch_integer_type (gdbarch, 32, 0, "int");
- BUILD_OCL_VTYPES (int);
- types[opencl_primitive_type_uint]
- = arch_integer_type (gdbarch, 32, 1, "uint");
- BUILD_OCL_VTYPES (uint);
- types[opencl_primitive_type_long]
- = arch_integer_type (gdbarch, 64, 0, "long");
- BUILD_OCL_VTYPES (long);
- types[opencl_primitive_type_ulong]
- = arch_integer_type (gdbarch, 64, 1, "ulong");
- BUILD_OCL_VTYPES (ulong);
- types[opencl_primitive_type_half]
- = arch_float_type (gdbarch, 16, "half", floatformats_ieee_half);
- BUILD_OCL_VTYPES (half);
- types[opencl_primitive_type_float]
- = arch_float_type (gdbarch, 32, "float", floatformats_ieee_single);
- BUILD_OCL_VTYPES (float);
- types[opencl_primitive_type_double]
- = arch_float_type (gdbarch, 64, "double", floatformats_ieee_double);
- BUILD_OCL_VTYPES (double);
- types[opencl_primitive_type_bool]
- = arch_boolean_type (gdbarch, 8, 1, "bool");
- types[opencl_primitive_type_unsigned_char]
- = arch_integer_type (gdbarch, 8, 1, "unsigned char");
- types[opencl_primitive_type_unsigned_short]
- = arch_integer_type (gdbarch, 16, 1, "unsigned short");
- types[opencl_primitive_type_unsigned_int]
- = arch_integer_type (gdbarch, 32, 1, "unsigned int");
- types[opencl_primitive_type_unsigned_long]
- = arch_integer_type (gdbarch, 64, 1, "unsigned long");
- types[opencl_primitive_type_size_t]
- = arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch), 1, "size_t");
- types[opencl_primitive_type_ptrdiff_t]
- = arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch), 0, "ptrdiff_t");
- types[opencl_primitive_type_intptr_t]
- = arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch), 0, "intptr_t");
- types[opencl_primitive_type_uintptr_t]
- = arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch), 1, "uintptr_t");
- types[opencl_primitive_type_void]
- = arch_type (gdbarch, TYPE_CODE_VOID, TARGET_CHAR_BIT, "void");
-
- return types;
-}
-
-void _initialize_opencl_language ();
-void
-_initialize_opencl_language ()
-{
- opencl_type_data = gdbarch_data_register_post_init (build_opencl_types);
-}