From: Pierre-Marie de Rodat Date: Thu, 17 Dec 2015 14:09:45 +0000 (+0000) Subject: DWARF: add a language hook to override types in debugging information X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=eb59e42800b805e0bcced98ad2383c66a5839acc;p=gcc.git DWARF: add a language hook to override types in debugging information Many artificial types are introduced by GNAT in order to satisfy constraints in GCC's internal trees or to generate optimal code. These hide original types from sources and miss useful information in the debugging information or add noise to it and make debugging confusing. This change introduces a new language hook to give a chance to front-ends to restore the source types in the debugging information. This change also enhance the array descriptor language hook to handle array-wide bit/byte stride. Some arrays may contain dynamically-sized objects. Debuggers need for these a hint to know the size allocated for each element, hence the need for the array-wide bit/byte stride. The Ada front-end is enhanced to take advantage of both hooks when -fgnat-encodings=minimal, in order to keep compatibility with GDB. gcc/ada/ChangeLog: * gcc-interface/ada-tree.h (struct lang_type): Rename the t field as t1 and add a t2 one. (get_lang_specific): New. (GET_TYPE_LANG_SPECIFIC): Refactor to use get_lang_specific. (SET_TYPE_LANG_SPECIFIC): Likewise. (GET_TYPE_LANG_SPECIFIC2): New macro. (SET_TYPE_LANG_SPECIFIC2): New macro. (TYPE_DEBUG_TYPE): New macro. (SET_TYPE_DEBUG_TYPE): New macro. * gcc-interface/decl.c (gnat_to_gnu_entity): When -fgnat-encodings=minimal, set padding types' debug type to the padded one (i.e. strip ___PAD GNAT encodings) and set constrained record subtypes's debug type to the base type. * gcc-interface/misc.c (gnat_print_type): Print debug types. (gnat_get_debug_type): New. (gnat_get_array_descr_info): When -fgnat-encodings=minimal, set a byte stride for arrays that contain a type whose debug type has variable length. (LANG_HOOKS_GET_DEBUG_TYPE): Redefine macro to implement the debug type language hook. * gcc-interface/utils.c (maybe_pad_type): When -fgnat-encodings=minimal, set padding types' debug type to the padded one. Restore XVZ variables creation when -fgnat-encodings-minimal and use them to hold padding types' byte size. For library-level padding types, share this variable across translation units. Tag XVZ variables as artificial. gcc/ChangeLog: * langhooks.h (struct lang_hooks_for_types): Add a get_debug_type field. * langhooks-def.h (LANG_HOOKS_GET_DEBUG_TYPE): New macro. (LANG_HOOKS_FOR_TYPES_INITIALIZER): Initialize the get_debug_type field. * dwarf2out.h (struct array_descr_info): Add an array-wide stride field. * dwarf2out.c (modified_type_die): Invoke the get_debug_type language hook, process its result instead, if any. (gen_descr_array_type_die): Add array-wide stride processing. From-SVN: r231763 --- diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 04270508d1c..94b772607d0 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,16 @@ +2015-12-17 Pierre-Marie de Rodat + + * langhooks.h (struct lang_hooks_for_types): Add a + get_debug_type field. + * langhooks-def.h (LANG_HOOKS_GET_DEBUG_TYPE): New macro. + (LANG_HOOKS_FOR_TYPES_INITIALIZER): Initialize the + get_debug_type field. + * dwarf2out.h (struct array_descr_info): Add an array-wide + stride field. + * dwarf2out.c (modified_type_die): Invoke the get_debug_type + language hook, process its result instead, if any. + (gen_descr_array_type_die): Add array-wide stride processing. + 2015-12-17 Pierre-Marie de Rodat * debug.h (struct gcc_debug_hooks): Add a new size_function diff --git a/gcc/ada/ChangeLog b/gcc/ada/ChangeLog index 21ed1dc9ad7..8c6a56d7f1a 100644 --- a/gcc/ada/ChangeLog +++ b/gcc/ada/ChangeLog @@ -1,3 +1,32 @@ +2015-12-17 Pierre-Marie de Rodat + + * gcc-interface/ada-tree.h (struct lang_type): Rename the t + field as t1 and add a t2 one. + (get_lang_specific): New. + (GET_TYPE_LANG_SPECIFIC): Refactor to use get_lang_specific. + (SET_TYPE_LANG_SPECIFIC): Likewise. + (GET_TYPE_LANG_SPECIFIC2): New macro. + (SET_TYPE_LANG_SPECIFIC2): New macro. + (TYPE_DEBUG_TYPE): New macro. + (SET_TYPE_DEBUG_TYPE): New macro. + * gcc-interface/decl.c (gnat_to_gnu_entity): When + -fgnat-encodings=minimal, set padding types' debug type to the + padded one (i.e. strip ___PAD GNAT encodings) and set + constrained record subtypes's debug type to the base type. + * gcc-interface/misc.c (gnat_print_type): Print debug types. + (gnat_get_debug_type): New. + (gnat_get_array_descr_info): When -fgnat-encodings=minimal, set + a byte stride for arrays that contain a type whose debug type + has variable length. + (LANG_HOOKS_GET_DEBUG_TYPE): Redefine macro to implement the + debug type language hook. + * gcc-interface/utils.c (maybe_pad_type): When + -fgnat-encodings=minimal, set padding types' debug type to the + padded one. Restore XVZ variables creation when + -fgnat-encodings-minimal and use them to hold padding types' + byte size. For library-level padding types, share this variable + across translation units. Tag XVZ variables as artificial. + 2015-12-17 Pierre-Marie de Rodat * gcc-interface/decl.c (gnat_to_gnu_entity): Disable ___XVS GNAT diff --git a/gcc/ada/gcc-interface/ada-tree.h b/gcc/ada/gcc-interface/ada-tree.h index 5d93ea4bc5c..807da9d97cf 100644 --- a/gcc/ada/gcc-interface/ada-tree.h +++ b/gcc/ada/gcc-interface/ada-tree.h @@ -33,21 +33,21 @@ union GTY((desc ("0"), }; /* Ada uses the lang_decl and lang_type fields to hold a tree. */ -struct GTY(()) lang_type { tree t; }; +struct GTY(()) lang_type { tree t1; tree t2; }; struct GTY(()) lang_decl { tree t; }; -/* Macros to get and set the tree in TYPE_LANG_SPECIFIC. */ +extern struct lang_type *get_lang_specific (tree node); + +/* Macros to get and set the trees in TYPE_LANG_SPECIFIC. */ #define GET_TYPE_LANG_SPECIFIC(NODE) \ - (TYPE_LANG_SPECIFIC (NODE) ? TYPE_LANG_SPECIFIC (NODE)->t : NULL_TREE) + (TYPE_LANG_SPECIFIC (NODE) ? TYPE_LANG_SPECIFIC (NODE)->t1 : NULL_TREE) -#define SET_TYPE_LANG_SPECIFIC(NODE, X) \ -do { \ - tree tmp = (X); \ - if (!TYPE_LANG_SPECIFIC (NODE)) \ - TYPE_LANG_SPECIFIC (NODE) \ - = ggc_alloc (); \ - TYPE_LANG_SPECIFIC (NODE)->t = tmp; \ -} while (0) +#define SET_TYPE_LANG_SPECIFIC(NODE, X) (get_lang_specific (NODE)->t1 = (X)) + +#define GET_TYPE_LANG_SPECIFIC2(NODE) \ + (TYPE_LANG_SPECIFIC (NODE) ? TYPE_LANG_SPECIFIC (NODE)->t2 : NULL_TREE) + +#define SET_TYPE_LANG_SPECIFIC2(NODE, X) (get_lang_specific (NODE)->t2 = (X)) /* Macros to get and set the tree in DECL_LANG_SPECIFIC. */ #define GET_DECL_LANG_SPECIFIC(NODE) \ @@ -351,6 +351,12 @@ do { \ #define SET_TYPE_ADA_SIZE(NODE, X) \ SET_TYPE_LANG_SPECIFIC (RECORD_OR_UNION_CHECK (NODE), X) +/* For types with TYPE_CAN_HAVE_DEBUG_TYPE_P, this is the type to use in + debugging information. */ +#define TYPE_DEBUG_TYPE(NODE) \ + GET_TYPE_LANG_SPECIFIC2(NODE) +#define SET_TYPE_DEBUG_TYPE(NODE, X) \ + SET_TYPE_LANG_SPECIFIC2(NODE, X) /* Flags added to decl nodes. */ diff --git a/gcc/ada/gcc-interface/decl.c b/gcc/ada/gcc-interface/decl.c index 4db11934748..385b720506a 100644 --- a/gcc/ada/gcc-interface/decl.c +++ b/gcc/ada/gcc-interface/decl.c @@ -1846,6 +1846,8 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition) gnu_type = make_node (RECORD_TYPE); TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "PAD"); + if (gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL) + SET_TYPE_DEBUG_TYPE (gnu_type, gnu_field_type); TYPE_PACKED (gnu_type) = 1; TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_field_type); TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_field_type); @@ -3277,6 +3279,8 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition) gnu_type = make_node (RECORD_TYPE); TYPE_NAME (gnu_type) = gnu_entity_name; + if (gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL) + SET_TYPE_DEBUG_TYPE (gnu_type, gnu_base_type); TYPE_PACKED (gnu_type) = TYPE_PACKED (gnu_base_type); TYPE_REVERSE_STORAGE_ORDER (gnu_type) = Reverse_Storage_Order (gnat_entity); diff --git a/gcc/ada/gcc-interface/misc.c b/gcc/ada/gcc-interface/misc.c index ef0fe3f0daf..e9df63c1fc7 100644 --- a/gcc/ada/gcc-interface/misc.c +++ b/gcc/ada/gcc-interface/misc.c @@ -524,6 +524,10 @@ gnat_print_type (FILE *file, tree node, int indent) default: break; } + + if (TYPE_DEBUG_TYPE (node) != NULL_TREE) + print_node_brief (file, "debug type", TYPE_DEBUG_TYPE (node), + indent + 4); } /* Return the name to be printed for DECL. */ @@ -565,6 +569,15 @@ gnat_descriptive_type (const_tree type) return NULL_TREE; } +/* Return the type to used for debugging information instead of TYPE, if any. + NULL_TREE if TYPE is fine. */ + +static tree +gnat_get_debug_type (const_tree type) +{ + return TYPE_DEBUG_TYPE (type); +} + /* Return true if types T1 and T2 are identical for type hashing purposes. Called only after doing all language independent checks. At present, this function is only called when both types are FUNCTION_TYPE. */ @@ -697,6 +710,33 @@ gnat_get_array_descr_info (const_tree type, struct array_descr_info *info) info->element_type = TREE_TYPE (last_dimen); + /* When arrays contain dynamically-sized elements, we usually wrap them in + padding types, or we create constrained types for them. Then, if such + types are stripped in the debugging information output, the debugger needs + a way to know the size that is reserved for each element. This is why we + emit a stride in such situations. */ + if (gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL) + { + tree source_element_type = info->element_type; + + while (1) + { + if (TYPE_DEBUG_TYPE (source_element_type) != NULL_TREE) + source_element_type = TYPE_DEBUG_TYPE (source_element_type); + else if (TYPE_IS_PADDING_P (source_element_type)) + source_element_type + = TREE_TYPE (TYPE_FIELDS (source_element_type)); + else + break; + } + + if (TREE_CODE (TYPE_SIZE_UNIT (source_element_type)) != INTEGER_CST) + { + info->stride = TYPE_SIZE_UNIT (info->element_type); + info->stride_in_bits = false; + } + } + return true; } @@ -947,6 +987,17 @@ gnat_init_ts (void) MARK_TS_TYPED (EXIT_STMT); } +/* Return the lang specific structure attached to NODE. Allocate it (cleared) + if needed. */ + +struct lang_type * +get_lang_specific (tree node) +{ + if (!TYPE_LANG_SPECIFIC (node)) + TYPE_LANG_SPECIFIC (node) = ggc_cleared_alloc (); + return TYPE_LANG_SPECIFIC (node); +} + /* Definitions for our language-specific hooks. */ #undef LANG_HOOKS_NAME @@ -999,6 +1050,8 @@ gnat_init_ts (void) #define LANG_HOOKS_GET_SUBRANGE_BOUNDS gnat_get_subrange_bounds #undef LANG_HOOKS_DESCRIPTIVE_TYPE #define LANG_HOOKS_DESCRIPTIVE_TYPE gnat_descriptive_type +#undef LANG_HOOKS_GET_DEBUG_TYPE +#define LANG_HOOKS_GET_DEBUG_TYPE gnat_get_debug_type #undef LANG_HOOKS_ATTRIBUTE_TABLE #define LANG_HOOKS_ATTRIBUTE_TABLE gnat_internal_attribute_table #undef LANG_HOOKS_BUILTIN_FUNCTION diff --git a/gcc/ada/gcc-interface/utils.c b/gcc/ada/gcc-interface/utils.c index 830a0b8003e..1816f3a9a26 100644 --- a/gcc/ada/gcc-interface/utils.c +++ b/gcc/ada/gcc-interface/utils.c @@ -1278,6 +1278,8 @@ maybe_pad_type (tree type, tree size, unsigned int align, type and name. */ record = make_node (RECORD_TYPE); TYPE_PADDING_P (record) = 1; + if (gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL) + SET_TYPE_DEBUG_TYPE (record, type); if (Present (gnat_entity)) TYPE_NAME (record) = create_concat_name (gnat_entity, "PAD"); @@ -1348,10 +1350,8 @@ maybe_pad_type (tree type, tree size, unsigned int align, /* Unless debugging information isn't being written for the input type, write a record that shows what we are a subtype of and also make a - variable that indicates our size, if still variable. Don't do this if - asked to output as few encodings as possible. */ - if (gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL - && TREE_CODE (orig_size) != INTEGER_CST + variable that indicates our size, if still variable. */ + if (TREE_CODE (orig_size) != INTEGER_CST && TYPE_NAME (record) && TYPE_NAME (type) && !(TREE_CODE (TYPE_NAME (type)) == TYPE_DECL @@ -1367,6 +1367,8 @@ maybe_pad_type (tree type, tree size, unsigned int align, && TREE_CODE (size) != INTEGER_CST && (definition || global_bindings_p ())) { + /* Whether or not gnat_entity comes from source, this XVZ variable is + is a compilation artifact. */ size_unit = create_var_decl (concat_name (name, "XVZ"), NULL_TREE, sizetype, size_unit, true, global_bindings_p (), @@ -1375,19 +1377,25 @@ maybe_pad_type (tree type, tree size, unsigned int align, TYPE_SIZE_UNIT (record) = size_unit; } - tree marker = make_node (RECORD_TYPE); - tree orig_name = TYPE_IDENTIFIER (type); - - TYPE_NAME (marker) = concat_name (name, "XVS"); - finish_record_type (marker, - create_field_decl (orig_name, - build_reference_type (type), - marker, NULL_TREE, NULL_TREE, - 0, 0), - 0, true); - TYPE_SIZE_UNIT (marker) = size_unit; - - add_parallel_type (record, marker); + /* There is no need to show what we are a subtype of when outputting as + few encodings as possible: regular debugging infomation makes this + redundant. */ + if (gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL) + { + tree marker = make_node (RECORD_TYPE); + tree orig_name = TYPE_IDENTIFIER (type); + + TYPE_NAME (marker) = concat_name (name, "XVS"); + finish_record_type (marker, + create_field_decl (orig_name, + build_reference_type (type), + marker, NULL_TREE, NULL_TREE, + 0, 0), + 0, true); + TYPE_SIZE_UNIT (marker) = size_unit; + + add_parallel_type (record, marker); + } } rest_of_record_type_compilation (record); diff --git a/gcc/dwarf2out.c b/gcc/dwarf2out.c index f731e92d1c2..800f283aa31 100644 --- a/gcc/dwarf2out.c +++ b/gcc/dwarf2out.c @@ -11084,6 +11084,14 @@ modified_type_die (tree type, int cv_quals, dw_die_ref context_die) if (code == ERROR_MARK) return NULL; + if (lang_hooks.types.get_debug_type) + { + tree debug_type = lang_hooks.types.get_debug_type (type); + + if (debug_type != NULL_TREE && debug_type != type) + return modified_type_die (debug_type, cv_quals, context_die); + } + cv_quals &= cv_qual_mask; /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type @@ -19073,6 +19081,19 @@ gen_descr_array_type_die (tree type, struct array_descr_info *info, dw_scalar_form_constant | dw_scalar_form_exprloc | dw_scalar_form_reference, &context); + if (info->stride) + { + const enum dwarf_attribute attr + = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride; + const int forms + = (info->stride_in_bits) + ? dw_scalar_form_constant + : (dw_scalar_form_constant + | dw_scalar_form_exprloc + | dw_scalar_form_reference); + + add_scalar_info (array_die, attr, info->stride, forms, &context); + } } add_gnat_descriptive_type_attribute (array_die, type, context_die); diff --git a/gcc/dwarf2out.h b/gcc/dwarf2out.h index 4303e60b55f..7f4a24d977d 100644 --- a/gcc/dwarf2out.h +++ b/gcc/dwarf2out.h @@ -325,6 +325,8 @@ struct array_descr_info tree data_location; tree allocated; tree associated; + tree stride; + bool stride_in_bits; struct array_descr_dimen { /* GCC uses sizetype for array indices, so lower_bound and upper_bound @@ -333,6 +335,9 @@ struct array_descr_info tree bounds_type; tree lower_bound; tree upper_bound; + + /* Only Fortran uses more than one dimension for array types. For other + languages, the stride can be rather specified for the whole array. */ tree stride; } dimen[10]; }; diff --git a/gcc/langhooks-def.h b/gcc/langhooks-def.h index 18ac84d9df3..1eafed6155a 100644 --- a/gcc/langhooks-def.h +++ b/gcc/langhooks-def.h @@ -176,6 +176,7 @@ extern tree lhd_make_node (enum tree_code); #define LANG_HOOKS_DESCRIPTIVE_TYPE NULL #define LANG_HOOKS_RECONSTRUCT_COMPLEX_TYPE reconstruct_complex_type #define LANG_HOOKS_ENUM_UNDERLYING_BASE_TYPE lhd_enum_underlying_base_type +#define LANG_HOOKS_GET_DEBUG_TYPE NULL #define LANG_HOOKS_FOR_TYPES_INITIALIZER { \ LANG_HOOKS_MAKE_TYPE, \ @@ -195,7 +196,8 @@ extern tree lhd_make_node (enum tree_code); LANG_HOOKS_GET_SUBRANGE_BOUNDS, \ LANG_HOOKS_DESCRIPTIVE_TYPE, \ LANG_HOOKS_RECONSTRUCT_COMPLEX_TYPE, \ - LANG_HOOKS_ENUM_UNDERLYING_BASE_TYPE \ + LANG_HOOKS_ENUM_UNDERLYING_BASE_TYPE, \ + LANG_HOOKS_GET_DEBUG_TYPE \ } /* Declaration hooks. */ diff --git a/gcc/langhooks.h b/gcc/langhooks.h index d8d01fa3b87..e80993b8963 100644 --- a/gcc/langhooks.h +++ b/gcc/langhooks.h @@ -143,6 +143,12 @@ struct lang_hooks_for_types type_for_size. Used in dwarf2out.c to add a DW_AT_type base type reference to a DW_TAG_enumeration. */ tree (*enum_underlying_base_type) (const_tree); + + /* Return a type to use in the debug info instead of TYPE, or NULL_TREE to + keep TYPE. This is useful to keep a single "source type" when the + middle-end uses specialized types, for instance constrained discriminated + types in Ada. */ + tree (*get_debug_type) (const_tree); }; /* Language hooks related to decls and the symbol table. */