diagnostic-show-locus.c: remove unused field from class colorizer
[official-gcc.git] / gcc / dwarf2out.c
blob917ab9fa58a3e772f422f3d276b83a7846beaddb
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2017 Free Software Foundation, Inc.
3 Contributed by Gary Funck (gary@intrepid.com).
4 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5 Extensively modified by Jason Merrill (jason@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO: Emit .debug_line header even when there are no functions, since
24 the file numbers are used by .debug_info. Alternately, leave
25 out locations for types and decls.
26 Avoid talking about ctors and op= for PODs.
27 Factor out common prologue sequences into multiple CIEs. */
29 /* The first part of this file deals with the DWARF 2 frame unwind
30 information, which is also used by the GCC efficient exception handling
31 mechanism. The second part, controlled only by an #ifdef
32 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33 information. */
35 /* DWARF2 Abbreviation Glossary:
37 CFA = Canonical Frame Address
38 a fixed address on the stack which identifies a call frame.
39 We define it to be the value of SP just before the call insn.
40 The CFA register and offset, which may change during the course
41 of the function, are used to calculate its value at runtime.
43 CFI = Call Frame Instruction
44 an instruction for the DWARF2 abstract machine
46 CIE = Common Information Entry
47 information describing information common to one or more FDEs
49 DIE = Debugging Information Entry
51 FDE = Frame Description Entry
52 information describing the stack call frame, in particular,
53 how to restore registers
55 DW_CFA_... = DWARF2 CFA call frame instruction
56 DW_TAG_... = DWARF2 DIE tag */
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "target.h"
62 #include "function.h"
63 #include "rtl.h"
64 #include "tree.h"
65 #include "memmodel.h"
66 #include "tm_p.h"
67 #include "stringpool.h"
68 #include "insn-config.h"
69 #include "ira.h"
70 #include "cgraph.h"
71 #include "diagnostic.h"
72 #include "fold-const.h"
73 #include "stor-layout.h"
74 #include "varasm.h"
75 #include "version.h"
76 #include "flags.h"
77 #include "rtlhash.h"
78 #include "reload.h"
79 #include "output.h"
80 #include "expr.h"
81 #include "dwarf2out.h"
82 #include "dwarf2asm.h"
83 #include "toplev.h"
84 #include "md5.h"
85 #include "tree-pretty-print.h"
86 #include "debug.h"
87 #include "common/common-target.h"
88 #include "langhooks.h"
89 #include "lra.h"
90 #include "dumpfile.h"
91 #include "opts.h"
92 #include "tree-dfa.h"
93 #include "gdb/gdb-index.h"
94 #include "rtl-iter.h"
95 #include "stringpool.h"
96 #include "attribs.h"
98 static void dwarf2out_source_line (unsigned int, unsigned int, const char *,
99 int, bool);
100 static rtx_insn *last_var_location_insn;
101 static rtx_insn *cached_next_real_insn;
102 static void dwarf2out_decl (tree);
104 #ifndef XCOFF_DEBUGGING_INFO
105 #define XCOFF_DEBUGGING_INFO 0
106 #endif
108 #ifndef HAVE_XCOFF_DWARF_EXTRAS
109 #define HAVE_XCOFF_DWARF_EXTRAS 0
110 #endif
112 #ifdef VMS_DEBUGGING_INFO
113 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
115 /* Define this macro to be a nonzero value if the directory specifications
116 which are output in the debug info should end with a separator. */
117 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
118 /* Define this macro to evaluate to a nonzero value if GCC should refrain
119 from generating indirect strings in DWARF2 debug information, for instance
120 if your target is stuck with an old version of GDB that is unable to
121 process them properly or uses VMS Debug. */
122 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
123 #else
124 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
125 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
126 #endif
128 /* ??? Poison these here until it can be done generically. They've been
129 totally replaced in this file; make sure it stays that way. */
130 #undef DWARF2_UNWIND_INFO
131 #undef DWARF2_FRAME_INFO
132 #if (GCC_VERSION >= 3000)
133 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
134 #endif
136 /* The size of the target's pointer type. */
137 #ifndef PTR_SIZE
138 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
139 #endif
141 /* Array of RTXes referenced by the debugging information, which therefore
142 must be kept around forever. */
143 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
145 /* A pointer to the base of a list of incomplete types which might be
146 completed at some later time. incomplete_types_list needs to be a
147 vec<tree, va_gc> *because we want to tell the garbage collector about
148 it. */
149 static GTY(()) vec<tree, va_gc> *incomplete_types;
151 /* A pointer to the base of a table of references to declaration
152 scopes. This table is a display which tracks the nesting
153 of declaration scopes at the current scope and containing
154 scopes. This table is used to find the proper place to
155 define type declaration DIE's. */
156 static GTY(()) vec<tree, va_gc> *decl_scope_table;
158 /* Pointers to various DWARF2 sections. */
159 static GTY(()) section *debug_info_section;
160 static GTY(()) section *debug_skeleton_info_section;
161 static GTY(()) section *debug_abbrev_section;
162 static GTY(()) section *debug_skeleton_abbrev_section;
163 static GTY(()) section *debug_aranges_section;
164 static GTY(()) section *debug_addr_section;
165 static GTY(()) section *debug_macinfo_section;
166 static const char *debug_macinfo_section_name;
167 static GTY(()) section *debug_line_section;
168 static GTY(()) section *debug_skeleton_line_section;
169 static GTY(()) section *debug_loc_section;
170 static GTY(()) section *debug_pubnames_section;
171 static GTY(()) section *debug_pubtypes_section;
172 static GTY(()) section *debug_str_section;
173 static GTY(()) section *debug_line_str_section;
174 static GTY(()) section *debug_str_dwo_section;
175 static GTY(()) section *debug_str_offsets_section;
176 static GTY(()) section *debug_ranges_section;
177 static GTY(()) section *debug_frame_section;
179 /* Maximum size (in bytes) of an artificially generated label. */
180 #define MAX_ARTIFICIAL_LABEL_BYTES 30
182 /* According to the (draft) DWARF 3 specification, the initial length
183 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
184 bytes are 0xffffffff, followed by the length stored in the next 8
185 bytes.
187 However, the SGI/MIPS ABI uses an initial length which is equal to
188 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
190 #ifndef DWARF_INITIAL_LENGTH_SIZE
191 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
192 #endif
194 #ifndef DWARF_INITIAL_LENGTH_SIZE_STR
195 #define DWARF_INITIAL_LENGTH_SIZE_STR (DWARF_OFFSET_SIZE == 4 ? "-4" : "-12")
196 #endif
198 /* Round SIZE up to the nearest BOUNDARY. */
199 #define DWARF_ROUND(SIZE,BOUNDARY) \
200 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
202 /* CIE identifier. */
203 #if HOST_BITS_PER_WIDE_INT >= 64
204 #define DWARF_CIE_ID \
205 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
206 #else
207 #define DWARF_CIE_ID DW_CIE_ID
208 #endif
211 /* A vector for a table that contains frame description
212 information for each routine. */
213 #define NOT_INDEXED (-1U)
214 #define NO_INDEX_ASSIGNED (-2U)
216 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
218 struct GTY((for_user)) indirect_string_node {
219 const char *str;
220 unsigned int refcount;
221 enum dwarf_form form;
222 char *label;
223 unsigned int index;
226 struct indirect_string_hasher : ggc_ptr_hash<indirect_string_node>
228 typedef const char *compare_type;
230 static hashval_t hash (indirect_string_node *);
231 static bool equal (indirect_string_node *, const char *);
234 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
236 static GTY (()) hash_table<indirect_string_hasher> *debug_line_str_hash;
238 /* With split_debug_info, both the comp_dir and dwo_name go in the
239 main object file, rather than the dwo, similar to the force_direct
240 parameter elsewhere but with additional complications:
242 1) The string is needed in both the main object file and the dwo.
243 That is, the comp_dir and dwo_name will appear in both places.
245 2) Strings can use four forms: DW_FORM_string, DW_FORM_strp,
246 DW_FORM_line_strp or DW_FORM_GNU_str_index.
248 3) GCC chooses the form to use late, depending on the size and
249 reference count.
251 Rather than forcing the all debug string handling functions and
252 callers to deal with these complications, simply use a separate,
253 special-cased string table for any attribute that should go in the
254 main object file. This limits the complexity to just the places
255 that need it. */
257 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
259 static GTY(()) int dw2_string_counter;
261 /* True if the compilation unit places functions in more than one section. */
262 static GTY(()) bool have_multiple_function_sections = false;
264 /* Whether the default text and cold text sections have been used at all. */
266 static GTY(()) bool text_section_used = false;
267 static GTY(()) bool cold_text_section_used = false;
269 /* The default cold text section. */
270 static GTY(()) section *cold_text_section;
272 /* The DIE for C++14 'auto' in a function return type. */
273 static GTY(()) dw_die_ref auto_die;
275 /* The DIE for C++14 'decltype(auto)' in a function return type. */
276 static GTY(()) dw_die_ref decltype_auto_die;
278 /* Forward declarations for functions defined in this file. */
280 static void output_call_frame_info (int);
281 static void dwarf2out_note_section_used (void);
283 /* Personality decl of current unit. Used only when assembler does not support
284 personality CFI. */
285 static GTY(()) rtx current_unit_personality;
287 /* .debug_rnglists next index. */
288 static unsigned int rnglist_idx;
290 /* Data and reference forms for relocatable data. */
291 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
292 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
294 #ifndef DEBUG_FRAME_SECTION
295 #define DEBUG_FRAME_SECTION ".debug_frame"
296 #endif
298 #ifndef FUNC_BEGIN_LABEL
299 #define FUNC_BEGIN_LABEL "LFB"
300 #endif
302 #ifndef FUNC_END_LABEL
303 #define FUNC_END_LABEL "LFE"
304 #endif
306 #ifndef PROLOGUE_END_LABEL
307 #define PROLOGUE_END_LABEL "LPE"
308 #endif
310 #ifndef EPILOGUE_BEGIN_LABEL
311 #define EPILOGUE_BEGIN_LABEL "LEB"
312 #endif
314 #ifndef FRAME_BEGIN_LABEL
315 #define FRAME_BEGIN_LABEL "Lframe"
316 #endif
317 #define CIE_AFTER_SIZE_LABEL "LSCIE"
318 #define CIE_END_LABEL "LECIE"
319 #define FDE_LABEL "LSFDE"
320 #define FDE_AFTER_SIZE_LABEL "LASFDE"
321 #define FDE_END_LABEL "LEFDE"
322 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
323 #define LINE_NUMBER_END_LABEL "LELT"
324 #define LN_PROLOG_AS_LABEL "LASLTP"
325 #define LN_PROLOG_END_LABEL "LELTP"
326 #define DIE_LABEL_PREFIX "DW"
328 /* Match the base name of a file to the base name of a compilation unit. */
330 static int
331 matches_main_base (const char *path)
333 /* Cache the last query. */
334 static const char *last_path = NULL;
335 static int last_match = 0;
336 if (path != last_path)
338 const char *base;
339 int length = base_of_path (path, &base);
340 last_path = path;
341 last_match = (length == main_input_baselength
342 && memcmp (base, main_input_basename, length) == 0);
344 return last_match;
347 #ifdef DEBUG_DEBUG_STRUCT
349 static int
350 dump_struct_debug (tree type, enum debug_info_usage usage,
351 enum debug_struct_file criterion, int generic,
352 int matches, int result)
354 /* Find the type name. */
355 tree type_decl = TYPE_STUB_DECL (type);
356 tree t = type_decl;
357 const char *name = 0;
358 if (TREE_CODE (t) == TYPE_DECL)
359 t = DECL_NAME (t);
360 if (t)
361 name = IDENTIFIER_POINTER (t);
363 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
364 criterion,
365 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
366 matches ? "bas" : "hdr",
367 generic ? "gen" : "ord",
368 usage == DINFO_USAGE_DFN ? ";" :
369 usage == DINFO_USAGE_DIR_USE ? "." : "*",
370 result,
371 (void*) type_decl, name);
372 return result;
374 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
375 dump_struct_debug (type, usage, criterion, generic, matches, result)
377 #else
379 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
380 (result)
382 #endif
384 /* Get the number of HOST_WIDE_INTs needed to represent the precision
385 of the number. Some constants have a large uniform precision, so
386 we get the precision needed for the actual value of the number. */
388 static unsigned int
389 get_full_len (const wide_int &op)
391 int prec = wi::min_precision (op, UNSIGNED);
392 return ((prec + HOST_BITS_PER_WIDE_INT - 1)
393 / HOST_BITS_PER_WIDE_INT);
396 static bool
397 should_emit_struct_debug (tree type, enum debug_info_usage usage)
399 enum debug_struct_file criterion;
400 tree type_decl;
401 bool generic = lang_hooks.types.generic_p (type);
403 if (generic)
404 criterion = debug_struct_generic[usage];
405 else
406 criterion = debug_struct_ordinary[usage];
408 if (criterion == DINFO_STRUCT_FILE_NONE)
409 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
410 if (criterion == DINFO_STRUCT_FILE_ANY)
411 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
413 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
415 if (type_decl != NULL)
417 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
418 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
420 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
421 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
424 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
427 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
428 switch to the data section instead, and write out a synthetic start label
429 for collect2 the first time around. */
431 static void
432 switch_to_eh_frame_section (bool back ATTRIBUTE_UNUSED)
434 if (eh_frame_section == 0)
436 int flags;
438 if (EH_TABLES_CAN_BE_READ_ONLY)
440 int fde_encoding;
441 int per_encoding;
442 int lsda_encoding;
444 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
445 /*global=*/0);
446 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
447 /*global=*/1);
448 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
449 /*global=*/0);
450 flags = ((! flag_pic
451 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
452 && (fde_encoding & 0x70) != DW_EH_PE_aligned
453 && (per_encoding & 0x70) != DW_EH_PE_absptr
454 && (per_encoding & 0x70) != DW_EH_PE_aligned
455 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
456 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
457 ? 0 : SECTION_WRITE);
459 else
460 flags = SECTION_WRITE;
462 #ifdef EH_FRAME_SECTION_NAME
463 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
464 #else
465 eh_frame_section = ((flags == SECTION_WRITE)
466 ? data_section : readonly_data_section);
467 #endif /* EH_FRAME_SECTION_NAME */
470 switch_to_section (eh_frame_section);
472 #ifdef EH_FRAME_THROUGH_COLLECT2
473 /* We have no special eh_frame section. Emit special labels to guide
474 collect2. */
475 if (!back)
477 tree label = get_file_function_name ("F");
478 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
479 targetm.asm_out.globalize_label (asm_out_file,
480 IDENTIFIER_POINTER (label));
481 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
483 #endif
486 /* Switch [BACK] to the eh or debug frame table section, depending on
487 FOR_EH. */
489 static void
490 switch_to_frame_table_section (int for_eh, bool back)
492 if (for_eh)
493 switch_to_eh_frame_section (back);
494 else
496 if (!debug_frame_section)
497 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
498 SECTION_DEBUG, NULL);
499 switch_to_section (debug_frame_section);
503 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
505 enum dw_cfi_oprnd_type
506 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
508 switch (cfi)
510 case DW_CFA_nop:
511 case DW_CFA_GNU_window_save:
512 case DW_CFA_remember_state:
513 case DW_CFA_restore_state:
514 return dw_cfi_oprnd_unused;
516 case DW_CFA_set_loc:
517 case DW_CFA_advance_loc1:
518 case DW_CFA_advance_loc2:
519 case DW_CFA_advance_loc4:
520 case DW_CFA_MIPS_advance_loc8:
521 return dw_cfi_oprnd_addr;
523 case DW_CFA_offset:
524 case DW_CFA_offset_extended:
525 case DW_CFA_def_cfa:
526 case DW_CFA_offset_extended_sf:
527 case DW_CFA_def_cfa_sf:
528 case DW_CFA_restore:
529 case DW_CFA_restore_extended:
530 case DW_CFA_undefined:
531 case DW_CFA_same_value:
532 case DW_CFA_def_cfa_register:
533 case DW_CFA_register:
534 case DW_CFA_expression:
535 case DW_CFA_val_expression:
536 return dw_cfi_oprnd_reg_num;
538 case DW_CFA_def_cfa_offset:
539 case DW_CFA_GNU_args_size:
540 case DW_CFA_def_cfa_offset_sf:
541 return dw_cfi_oprnd_offset;
543 case DW_CFA_def_cfa_expression:
544 return dw_cfi_oprnd_loc;
546 default:
547 gcc_unreachable ();
551 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
553 enum dw_cfi_oprnd_type
554 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
556 switch (cfi)
558 case DW_CFA_def_cfa:
559 case DW_CFA_def_cfa_sf:
560 case DW_CFA_offset:
561 case DW_CFA_offset_extended_sf:
562 case DW_CFA_offset_extended:
563 return dw_cfi_oprnd_offset;
565 case DW_CFA_register:
566 return dw_cfi_oprnd_reg_num;
568 case DW_CFA_expression:
569 case DW_CFA_val_expression:
570 return dw_cfi_oprnd_loc;
572 default:
573 return dw_cfi_oprnd_unused;
577 /* Output one FDE. */
579 static void
580 output_fde (dw_fde_ref fde, bool for_eh, bool second,
581 char *section_start_label, int fde_encoding, char *augmentation,
582 bool any_lsda_needed, int lsda_encoding)
584 const char *begin, *end;
585 static unsigned int j;
586 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
588 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
589 /* empty */ 0);
590 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
591 for_eh + j);
592 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
593 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
594 if (!XCOFF_DEBUGGING_INFO || for_eh)
596 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
597 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
598 " indicating 64-bit DWARF extension");
599 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
600 "FDE Length");
602 ASM_OUTPUT_LABEL (asm_out_file, l1);
604 if (for_eh)
605 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
606 else
607 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
608 debug_frame_section, "FDE CIE offset");
610 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
611 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
613 if (for_eh)
615 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
616 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
617 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
618 "FDE initial location");
619 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
620 end, begin, "FDE address range");
622 else
624 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
625 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
628 if (augmentation[0])
630 if (any_lsda_needed)
632 int size = size_of_encoded_value (lsda_encoding);
634 if (lsda_encoding == DW_EH_PE_aligned)
636 int offset = ( 4 /* Length */
637 + 4 /* CIE offset */
638 + 2 * size_of_encoded_value (fde_encoding)
639 + 1 /* Augmentation size */ );
640 int pad = -offset & (PTR_SIZE - 1);
642 size += pad;
643 gcc_assert (size_of_uleb128 (size) == 1);
646 dw2_asm_output_data_uleb128 (size, "Augmentation size");
648 if (fde->uses_eh_lsda)
650 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
651 fde->funcdef_number);
652 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
653 gen_rtx_SYMBOL_REF (Pmode, l1),
654 false,
655 "Language Specific Data Area");
657 else
659 if (lsda_encoding == DW_EH_PE_aligned)
660 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
661 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
662 "Language Specific Data Area (none)");
665 else
666 dw2_asm_output_data_uleb128 (0, "Augmentation size");
669 /* Loop through the Call Frame Instructions associated with this FDE. */
670 fde->dw_fde_current_label = begin;
672 size_t from, until, i;
674 from = 0;
675 until = vec_safe_length (fde->dw_fde_cfi);
677 if (fde->dw_fde_second_begin == NULL)
679 else if (!second)
680 until = fde->dw_fde_switch_cfi_index;
681 else
682 from = fde->dw_fde_switch_cfi_index;
684 for (i = from; i < until; i++)
685 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
688 /* If we are to emit a ref/link from function bodies to their frame tables,
689 do it now. This is typically performed to make sure that tables
690 associated with functions are dragged with them and not discarded in
691 garbage collecting links. We need to do this on a per function basis to
692 cope with -ffunction-sections. */
694 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
695 /* Switch to the function section, emit the ref to the tables, and
696 switch *back* into the table section. */
697 switch_to_section (function_section (fde->decl));
698 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
699 switch_to_frame_table_section (for_eh, true);
700 #endif
702 /* Pad the FDE out to an address sized boundary. */
703 ASM_OUTPUT_ALIGN (asm_out_file,
704 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
705 ASM_OUTPUT_LABEL (asm_out_file, l2);
707 j += 2;
710 /* Return true if frame description entry FDE is needed for EH. */
712 static bool
713 fde_needed_for_eh_p (dw_fde_ref fde)
715 if (flag_asynchronous_unwind_tables)
716 return true;
718 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
719 return true;
721 if (fde->uses_eh_lsda)
722 return true;
724 /* If exceptions are enabled, we have collected nothrow info. */
725 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
726 return false;
728 return true;
731 /* Output the call frame information used to record information
732 that relates to calculating the frame pointer, and records the
733 location of saved registers. */
735 static void
736 output_call_frame_info (int for_eh)
738 unsigned int i;
739 dw_fde_ref fde;
740 dw_cfi_ref cfi;
741 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
742 char section_start_label[MAX_ARTIFICIAL_LABEL_BYTES];
743 bool any_lsda_needed = false;
744 char augmentation[6];
745 int augmentation_size;
746 int fde_encoding = DW_EH_PE_absptr;
747 int per_encoding = DW_EH_PE_absptr;
748 int lsda_encoding = DW_EH_PE_absptr;
749 int return_reg;
750 rtx personality = NULL;
751 int dw_cie_version;
753 /* Don't emit a CIE if there won't be any FDEs. */
754 if (!fde_vec)
755 return;
757 /* Nothing to do if the assembler's doing it all. */
758 if (dwarf2out_do_cfi_asm ())
759 return;
761 /* If we don't have any functions we'll want to unwind out of, don't emit
762 any EH unwind information. If we make FDEs linkonce, we may have to
763 emit an empty label for an FDE that wouldn't otherwise be emitted. We
764 want to avoid having an FDE kept around when the function it refers to
765 is discarded. Example where this matters: a primary function template
766 in C++ requires EH information, an explicit specialization doesn't. */
767 if (for_eh)
769 bool any_eh_needed = false;
771 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
773 if (fde->uses_eh_lsda)
774 any_eh_needed = any_lsda_needed = true;
775 else if (fde_needed_for_eh_p (fde))
776 any_eh_needed = true;
777 else if (TARGET_USES_WEAK_UNWIND_INFO)
778 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
781 if (!any_eh_needed)
782 return;
785 /* We're going to be generating comments, so turn on app. */
786 if (flag_debug_asm)
787 app_enable ();
789 /* Switch to the proper frame section, first time. */
790 switch_to_frame_table_section (for_eh, false);
792 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
793 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
795 /* Output the CIE. */
796 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
797 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
798 if (!XCOFF_DEBUGGING_INFO || for_eh)
800 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
801 dw2_asm_output_data (4, 0xffffffff,
802 "Initial length escape value indicating 64-bit DWARF extension");
803 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
804 "Length of Common Information Entry");
806 ASM_OUTPUT_LABEL (asm_out_file, l1);
808 /* Now that the CIE pointer is PC-relative for EH,
809 use 0 to identify the CIE. */
810 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
811 (for_eh ? 0 : DWARF_CIE_ID),
812 "CIE Identifier Tag");
814 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
815 use CIE version 1, unless that would produce incorrect results
816 due to overflowing the return register column. */
817 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
818 dw_cie_version = 1;
819 if (return_reg >= 256 || dwarf_version > 2)
820 dw_cie_version = 3;
821 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
823 augmentation[0] = 0;
824 augmentation_size = 0;
826 personality = current_unit_personality;
827 if (for_eh)
829 char *p;
831 /* Augmentation:
832 z Indicates that a uleb128 is present to size the
833 augmentation section.
834 L Indicates the encoding (and thus presence) of
835 an LSDA pointer in the FDE augmentation.
836 R Indicates a non-default pointer encoding for
837 FDE code pointers.
838 P Indicates the presence of an encoding + language
839 personality routine in the CIE augmentation. */
841 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
842 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
843 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
845 p = augmentation + 1;
846 if (personality)
848 *p++ = 'P';
849 augmentation_size += 1 + size_of_encoded_value (per_encoding);
850 assemble_external_libcall (personality);
852 if (any_lsda_needed)
854 *p++ = 'L';
855 augmentation_size += 1;
857 if (fde_encoding != DW_EH_PE_absptr)
859 *p++ = 'R';
860 augmentation_size += 1;
862 if (p > augmentation + 1)
864 augmentation[0] = 'z';
865 *p = '\0';
868 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
869 if (personality && per_encoding == DW_EH_PE_aligned)
871 int offset = ( 4 /* Length */
872 + 4 /* CIE Id */
873 + 1 /* CIE version */
874 + strlen (augmentation) + 1 /* Augmentation */
875 + size_of_uleb128 (1) /* Code alignment */
876 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
877 + 1 /* RA column */
878 + 1 /* Augmentation size */
879 + 1 /* Personality encoding */ );
880 int pad = -offset & (PTR_SIZE - 1);
882 augmentation_size += pad;
884 /* Augmentations should be small, so there's scarce need to
885 iterate for a solution. Die if we exceed one uleb128 byte. */
886 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
890 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
891 if (dw_cie_version >= 4)
893 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
894 dw2_asm_output_data (1, 0, "CIE Segment Size");
896 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
897 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
898 "CIE Data Alignment Factor");
900 if (dw_cie_version == 1)
901 dw2_asm_output_data (1, return_reg, "CIE RA Column");
902 else
903 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
905 if (augmentation[0])
907 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
908 if (personality)
910 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
911 eh_data_format_name (per_encoding));
912 dw2_asm_output_encoded_addr_rtx (per_encoding,
913 personality,
914 true, NULL);
917 if (any_lsda_needed)
918 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
919 eh_data_format_name (lsda_encoding));
921 if (fde_encoding != DW_EH_PE_absptr)
922 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
923 eh_data_format_name (fde_encoding));
926 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
927 output_cfi (cfi, NULL, for_eh);
929 /* Pad the CIE out to an address sized boundary. */
930 ASM_OUTPUT_ALIGN (asm_out_file,
931 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
932 ASM_OUTPUT_LABEL (asm_out_file, l2);
934 /* Loop through all of the FDE's. */
935 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
937 unsigned int k;
939 /* Don't emit EH unwind info for leaf functions that don't need it. */
940 if (for_eh && !fde_needed_for_eh_p (fde))
941 continue;
943 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
944 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
945 augmentation, any_lsda_needed, lsda_encoding);
948 if (for_eh && targetm.terminate_dw2_eh_frame_info)
949 dw2_asm_output_data (4, 0, "End of Table");
951 /* Turn off app to make assembly quicker. */
952 if (flag_debug_asm)
953 app_disable ();
956 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
958 static void
959 dwarf2out_do_cfi_startproc (bool second)
961 int enc;
962 rtx ref;
963 rtx personality = get_personality_function (current_function_decl);
965 fprintf (asm_out_file, "\t.cfi_startproc\n");
967 if (personality)
969 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
970 ref = personality;
972 /* ??? The GAS support isn't entirely consistent. We have to
973 handle indirect support ourselves, but PC-relative is done
974 in the assembler. Further, the assembler can't handle any
975 of the weirder relocation types. */
976 if (enc & DW_EH_PE_indirect)
977 ref = dw2_force_const_mem (ref, true);
979 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
980 output_addr_const (asm_out_file, ref);
981 fputc ('\n', asm_out_file);
984 if (crtl->uses_eh_lsda)
986 char lab[MAX_ARTIFICIAL_LABEL_BYTES];
988 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
989 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
990 current_function_funcdef_no);
991 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
992 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
994 if (enc & DW_EH_PE_indirect)
995 ref = dw2_force_const_mem (ref, true);
997 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
998 output_addr_const (asm_out_file, ref);
999 fputc ('\n', asm_out_file);
1003 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
1004 this allocation may be done before pass_final. */
1006 dw_fde_ref
1007 dwarf2out_alloc_current_fde (void)
1009 dw_fde_ref fde;
1011 fde = ggc_cleared_alloc<dw_fde_node> ();
1012 fde->decl = current_function_decl;
1013 fde->funcdef_number = current_function_funcdef_no;
1014 fde->fde_index = vec_safe_length (fde_vec);
1015 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1016 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1017 fde->nothrow = crtl->nothrow;
1018 fde->drap_reg = INVALID_REGNUM;
1019 fde->vdrap_reg = INVALID_REGNUM;
1021 /* Record the FDE associated with this function. */
1022 cfun->fde = fde;
1023 vec_safe_push (fde_vec, fde);
1025 return fde;
1028 /* Output a marker (i.e. a label) for the beginning of a function, before
1029 the prologue. */
1031 void
1032 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1033 unsigned int column ATTRIBUTE_UNUSED,
1034 const char *file ATTRIBUTE_UNUSED)
1036 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1037 char * dup_label;
1038 dw_fde_ref fde;
1039 section *fnsec;
1040 bool do_frame;
1042 current_function_func_begin_label = NULL;
1044 do_frame = dwarf2out_do_frame ();
1046 /* ??? current_function_func_begin_label is also used by except.c for
1047 call-site information. We must emit this label if it might be used. */
1048 if (!do_frame
1049 && (!flag_exceptions
1050 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1051 return;
1053 fnsec = function_section (current_function_decl);
1054 switch_to_section (fnsec);
1055 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1056 current_function_funcdef_no);
1057 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1058 current_function_funcdef_no);
1059 dup_label = xstrdup (label);
1060 current_function_func_begin_label = dup_label;
1062 /* We can elide the fde allocation if we're not emitting debug info. */
1063 if (!do_frame)
1064 return;
1066 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1067 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1068 would include pass_dwarf2_frame. If we've not created the FDE yet,
1069 do so now. */
1070 fde = cfun->fde;
1071 if (fde == NULL)
1072 fde = dwarf2out_alloc_current_fde ();
1074 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1075 fde->dw_fde_begin = dup_label;
1076 fde->dw_fde_current_label = dup_label;
1077 fde->in_std_section = (fnsec == text_section
1078 || (cold_text_section && fnsec == cold_text_section));
1080 /* We only want to output line number information for the genuine dwarf2
1081 prologue case, not the eh frame case. */
1082 #ifdef DWARF2_DEBUGGING_INFO
1083 if (file)
1084 dwarf2out_source_line (line, column, file, 0, true);
1085 #endif
1087 if (dwarf2out_do_cfi_asm ())
1088 dwarf2out_do_cfi_startproc (false);
1089 else
1091 rtx personality = get_personality_function (current_function_decl);
1092 if (!current_unit_personality)
1093 current_unit_personality = personality;
1095 /* We cannot keep a current personality per function as without CFI
1096 asm, at the point where we emit the CFI data, there is no current
1097 function anymore. */
1098 if (personality && current_unit_personality != personality)
1099 sorry ("multiple EH personalities are supported only with assemblers "
1100 "supporting .cfi_personality directive");
1104 /* Output a marker (i.e. a label) for the end of the generated code
1105 for a function prologue. This gets called *after* the prologue code has
1106 been generated. */
1108 void
1109 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1110 const char *file ATTRIBUTE_UNUSED)
1112 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1114 /* Output a label to mark the endpoint of the code generated for this
1115 function. */
1116 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1117 current_function_funcdef_no);
1118 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1119 current_function_funcdef_no);
1120 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1123 /* Output a marker (i.e. a label) for the beginning of the generated code
1124 for a function epilogue. This gets called *before* the prologue code has
1125 been generated. */
1127 void
1128 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1129 const char *file ATTRIBUTE_UNUSED)
1131 dw_fde_ref fde = cfun->fde;
1132 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1134 if (fde->dw_fde_vms_begin_epilogue)
1135 return;
1137 /* Output a label to mark the endpoint of the code generated for this
1138 function. */
1139 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1140 current_function_funcdef_no);
1141 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1142 current_function_funcdef_no);
1143 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1146 /* Output a marker (i.e. a label) for the absolute end of the generated code
1147 for a function definition. This gets called *after* the epilogue code has
1148 been generated. */
1150 void
1151 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1152 const char *file ATTRIBUTE_UNUSED)
1154 dw_fde_ref fde;
1155 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1157 last_var_location_insn = NULL;
1158 cached_next_real_insn = NULL;
1160 if (dwarf2out_do_cfi_asm ())
1161 fprintf (asm_out_file, "\t.cfi_endproc\n");
1163 /* Output a label to mark the endpoint of the code generated for this
1164 function. */
1165 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1166 current_function_funcdef_no);
1167 ASM_OUTPUT_LABEL (asm_out_file, label);
1168 fde = cfun->fde;
1169 gcc_assert (fde != NULL);
1170 if (fde->dw_fde_second_begin == NULL)
1171 fde->dw_fde_end = xstrdup (label);
1174 void
1175 dwarf2out_frame_finish (void)
1177 /* Output call frame information. */
1178 if (targetm.debug_unwind_info () == UI_DWARF2)
1179 output_call_frame_info (0);
1181 /* Output another copy for the unwinder. */
1182 if ((flag_unwind_tables || flag_exceptions)
1183 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1184 output_call_frame_info (1);
1187 /* Note that the current function section is being used for code. */
1189 static void
1190 dwarf2out_note_section_used (void)
1192 section *sec = current_function_section ();
1193 if (sec == text_section)
1194 text_section_used = true;
1195 else if (sec == cold_text_section)
1196 cold_text_section_used = true;
1199 static void var_location_switch_text_section (void);
1200 static void set_cur_line_info_table (section *);
1202 void
1203 dwarf2out_switch_text_section (void)
1205 section *sect;
1206 dw_fde_ref fde = cfun->fde;
1208 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1210 if (!in_cold_section_p)
1212 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1213 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1214 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1216 else
1218 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1219 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1220 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1222 have_multiple_function_sections = true;
1224 /* There is no need to mark used sections when not debugging. */
1225 if (cold_text_section != NULL)
1226 dwarf2out_note_section_used ();
1228 if (dwarf2out_do_cfi_asm ())
1229 fprintf (asm_out_file, "\t.cfi_endproc\n");
1231 /* Now do the real section switch. */
1232 sect = current_function_section ();
1233 switch_to_section (sect);
1235 fde->second_in_std_section
1236 = (sect == text_section
1237 || (cold_text_section && sect == cold_text_section));
1239 if (dwarf2out_do_cfi_asm ())
1240 dwarf2out_do_cfi_startproc (true);
1242 var_location_switch_text_section ();
1244 if (cold_text_section != NULL)
1245 set_cur_line_info_table (sect);
1248 /* And now, the subset of the debugging information support code necessary
1249 for emitting location expressions. */
1251 /* Data about a single source file. */
1252 struct GTY((for_user)) dwarf_file_data {
1253 const char * filename;
1254 int emitted_number;
1257 /* Describe an entry into the .debug_addr section. */
1259 enum ate_kind {
1260 ate_kind_rtx,
1261 ate_kind_rtx_dtprel,
1262 ate_kind_label
1265 struct GTY((for_user)) addr_table_entry {
1266 enum ate_kind kind;
1267 unsigned int refcount;
1268 unsigned int index;
1269 union addr_table_entry_struct_union
1271 rtx GTY ((tag ("0"))) rtl;
1272 char * GTY ((tag ("1"))) label;
1274 GTY ((desc ("%1.kind"))) addr;
1277 /* Location lists are ranges + location descriptions for that range,
1278 so you can track variables that are in different places over
1279 their entire life. */
1280 typedef struct GTY(()) dw_loc_list_struct {
1281 dw_loc_list_ref dw_loc_next;
1282 const char *begin; /* Label and addr_entry for start of range */
1283 addr_table_entry *begin_entry;
1284 const char *end; /* Label for end of range */
1285 char *ll_symbol; /* Label for beginning of location list.
1286 Only on head of list */
1287 const char *section; /* Section this loclist is relative to */
1288 dw_loc_descr_ref expr;
1289 hashval_t hash;
1290 /* True if all addresses in this and subsequent lists are known to be
1291 resolved. */
1292 bool resolved_addr;
1293 /* True if this list has been replaced by dw_loc_next. */
1294 bool replaced;
1295 /* True if it has been emitted into .debug_loc* / .debug_loclists*
1296 section. */
1297 unsigned char emitted : 1;
1298 /* True if hash field is index rather than hash value. */
1299 unsigned char num_assigned : 1;
1300 /* True if .debug_loclists.dwo offset has been emitted for it already. */
1301 unsigned char offset_emitted : 1;
1302 /* True if note_variable_value_in_expr has been called on it. */
1303 unsigned char noted_variable_value : 1;
1304 /* True if the range should be emitted even if begin and end
1305 are the same. */
1306 bool force;
1307 } dw_loc_list_node;
1309 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1310 static dw_loc_descr_ref uint_loc_descriptor (unsigned HOST_WIDE_INT);
1312 /* Convert a DWARF stack opcode into its string name. */
1314 static const char *
1315 dwarf_stack_op_name (unsigned int op)
1317 const char *name = get_DW_OP_name (op);
1319 if (name != NULL)
1320 return name;
1322 return "OP_<unknown>";
1325 /* Return a pointer to a newly allocated location description. Location
1326 descriptions are simple expression terms that can be strung
1327 together to form more complicated location (address) descriptions. */
1329 static inline dw_loc_descr_ref
1330 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1331 unsigned HOST_WIDE_INT oprnd2)
1333 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1335 descr->dw_loc_opc = op;
1336 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1337 descr->dw_loc_oprnd1.val_entry = NULL;
1338 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1339 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1340 descr->dw_loc_oprnd2.val_entry = NULL;
1341 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1343 return descr;
1346 /* Return a pointer to a newly allocated location description for
1347 REG and OFFSET. */
1349 static inline dw_loc_descr_ref
1350 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1352 if (reg <= 31)
1353 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1354 offset, 0);
1355 else
1356 return new_loc_descr (DW_OP_bregx, reg, offset);
1359 /* Add a location description term to a location description expression. */
1361 static inline void
1362 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1364 dw_loc_descr_ref *d;
1366 /* Find the end of the chain. */
1367 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1370 *d = descr;
1373 /* Compare two location operands for exact equality. */
1375 static bool
1376 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1378 if (a->val_class != b->val_class)
1379 return false;
1380 switch (a->val_class)
1382 case dw_val_class_none:
1383 return true;
1384 case dw_val_class_addr:
1385 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1387 case dw_val_class_offset:
1388 case dw_val_class_unsigned_const:
1389 case dw_val_class_const:
1390 case dw_val_class_unsigned_const_implicit:
1391 case dw_val_class_const_implicit:
1392 case dw_val_class_range_list:
1393 /* These are all HOST_WIDE_INT, signed or unsigned. */
1394 return a->v.val_unsigned == b->v.val_unsigned;
1396 case dw_val_class_loc:
1397 return a->v.val_loc == b->v.val_loc;
1398 case dw_val_class_loc_list:
1399 return a->v.val_loc_list == b->v.val_loc_list;
1400 case dw_val_class_die_ref:
1401 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1402 case dw_val_class_fde_ref:
1403 return a->v.val_fde_index == b->v.val_fde_index;
1404 case dw_val_class_lbl_id:
1405 case dw_val_class_lineptr:
1406 case dw_val_class_macptr:
1407 case dw_val_class_loclistsptr:
1408 case dw_val_class_high_pc:
1409 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1410 case dw_val_class_str:
1411 return a->v.val_str == b->v.val_str;
1412 case dw_val_class_flag:
1413 return a->v.val_flag == b->v.val_flag;
1414 case dw_val_class_file:
1415 case dw_val_class_file_implicit:
1416 return a->v.val_file == b->v.val_file;
1417 case dw_val_class_decl_ref:
1418 return a->v.val_decl_ref == b->v.val_decl_ref;
1420 case dw_val_class_const_double:
1421 return (a->v.val_double.high == b->v.val_double.high
1422 && a->v.val_double.low == b->v.val_double.low);
1424 case dw_val_class_wide_int:
1425 return *a->v.val_wide == *b->v.val_wide;
1427 case dw_val_class_vec:
1429 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1430 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1432 return (a_len == b_len
1433 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1436 case dw_val_class_data8:
1437 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1439 case dw_val_class_vms_delta:
1440 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1441 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1443 case dw_val_class_discr_value:
1444 return (a->v.val_discr_value.pos == b->v.val_discr_value.pos
1445 && a->v.val_discr_value.v.uval == b->v.val_discr_value.v.uval);
1446 case dw_val_class_discr_list:
1447 /* It makes no sense comparing two discriminant value lists. */
1448 return false;
1450 gcc_unreachable ();
1453 /* Compare two location atoms for exact equality. */
1455 static bool
1456 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1458 if (a->dw_loc_opc != b->dw_loc_opc)
1459 return false;
1461 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1462 address size, but since we always allocate cleared storage it
1463 should be zero for other types of locations. */
1464 if (a->dtprel != b->dtprel)
1465 return false;
1467 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1468 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1471 /* Compare two complete location expressions for exact equality. */
1473 bool
1474 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1476 while (1)
1478 if (a == b)
1479 return true;
1480 if (a == NULL || b == NULL)
1481 return false;
1482 if (!loc_descr_equal_p_1 (a, b))
1483 return false;
1485 a = a->dw_loc_next;
1486 b = b->dw_loc_next;
1491 /* Add a constant OFFSET to a location expression. */
1493 static void
1494 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1496 dw_loc_descr_ref loc;
1497 HOST_WIDE_INT *p;
1499 gcc_assert (*list_head != NULL);
1501 if (!offset)
1502 return;
1504 /* Find the end of the chain. */
1505 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1508 p = NULL;
1509 if (loc->dw_loc_opc == DW_OP_fbreg
1510 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1511 p = &loc->dw_loc_oprnd1.v.val_int;
1512 else if (loc->dw_loc_opc == DW_OP_bregx)
1513 p = &loc->dw_loc_oprnd2.v.val_int;
1515 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1516 offset. Don't optimize if an signed integer overflow would happen. */
1517 if (p != NULL
1518 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1519 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1520 *p += offset;
1522 else if (offset > 0)
1523 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1525 else
1527 loc->dw_loc_next
1528 = uint_loc_descriptor (-(unsigned HOST_WIDE_INT) offset);
1529 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1533 /* Add a constant OFFSET to a location list. */
1535 static void
1536 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1538 dw_loc_list_ref d;
1539 for (d = list_head; d != NULL; d = d->dw_loc_next)
1540 loc_descr_plus_const (&d->expr, offset);
1543 #define DWARF_REF_SIZE \
1544 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1546 /* The number of bits that can be encoded by largest DW_FORM_dataN.
1547 In DWARF4 and earlier it is DW_FORM_data8 with 64 bits, in DWARF5
1548 DW_FORM_data16 with 128 bits. */
1549 #define DWARF_LARGEST_DATA_FORM_BITS \
1550 (dwarf_version >= 5 ? 128 : 64)
1552 /* Utility inline function for construction of ops that were GNU extension
1553 before DWARF 5. */
1554 static inline enum dwarf_location_atom
1555 dwarf_OP (enum dwarf_location_atom op)
1557 switch (op)
1559 case DW_OP_implicit_pointer:
1560 if (dwarf_version < 5)
1561 return DW_OP_GNU_implicit_pointer;
1562 break;
1564 case DW_OP_entry_value:
1565 if (dwarf_version < 5)
1566 return DW_OP_GNU_entry_value;
1567 break;
1569 case DW_OP_const_type:
1570 if (dwarf_version < 5)
1571 return DW_OP_GNU_const_type;
1572 break;
1574 case DW_OP_regval_type:
1575 if (dwarf_version < 5)
1576 return DW_OP_GNU_regval_type;
1577 break;
1579 case DW_OP_deref_type:
1580 if (dwarf_version < 5)
1581 return DW_OP_GNU_deref_type;
1582 break;
1584 case DW_OP_convert:
1585 if (dwarf_version < 5)
1586 return DW_OP_GNU_convert;
1587 break;
1589 case DW_OP_reinterpret:
1590 if (dwarf_version < 5)
1591 return DW_OP_GNU_reinterpret;
1592 break;
1594 default:
1595 break;
1597 return op;
1600 /* Similarly for attributes. */
1601 static inline enum dwarf_attribute
1602 dwarf_AT (enum dwarf_attribute at)
1604 switch (at)
1606 case DW_AT_call_return_pc:
1607 if (dwarf_version < 5)
1608 return DW_AT_low_pc;
1609 break;
1611 case DW_AT_call_tail_call:
1612 if (dwarf_version < 5)
1613 return DW_AT_GNU_tail_call;
1614 break;
1616 case DW_AT_call_origin:
1617 if (dwarf_version < 5)
1618 return DW_AT_abstract_origin;
1619 break;
1621 case DW_AT_call_target:
1622 if (dwarf_version < 5)
1623 return DW_AT_GNU_call_site_target;
1624 break;
1626 case DW_AT_call_target_clobbered:
1627 if (dwarf_version < 5)
1628 return DW_AT_GNU_call_site_target_clobbered;
1629 break;
1631 case DW_AT_call_parameter:
1632 if (dwarf_version < 5)
1633 return DW_AT_abstract_origin;
1634 break;
1636 case DW_AT_call_value:
1637 if (dwarf_version < 5)
1638 return DW_AT_GNU_call_site_value;
1639 break;
1641 case DW_AT_call_data_value:
1642 if (dwarf_version < 5)
1643 return DW_AT_GNU_call_site_data_value;
1644 break;
1646 case DW_AT_call_all_calls:
1647 if (dwarf_version < 5)
1648 return DW_AT_GNU_all_call_sites;
1649 break;
1651 case DW_AT_call_all_tail_calls:
1652 if (dwarf_version < 5)
1653 return DW_AT_GNU_all_tail_call_sites;
1654 break;
1656 case DW_AT_dwo_name:
1657 if (dwarf_version < 5)
1658 return DW_AT_GNU_dwo_name;
1659 break;
1661 default:
1662 break;
1664 return at;
1667 /* And similarly for tags. */
1668 static inline enum dwarf_tag
1669 dwarf_TAG (enum dwarf_tag tag)
1671 switch (tag)
1673 case DW_TAG_call_site:
1674 if (dwarf_version < 5)
1675 return DW_TAG_GNU_call_site;
1676 break;
1678 case DW_TAG_call_site_parameter:
1679 if (dwarf_version < 5)
1680 return DW_TAG_GNU_call_site_parameter;
1681 break;
1683 default:
1684 break;
1686 return tag;
1689 static unsigned long int get_base_type_offset (dw_die_ref);
1691 /* Return the size of a location descriptor. */
1693 static unsigned long
1694 size_of_loc_descr (dw_loc_descr_ref loc)
1696 unsigned long size = 1;
1698 switch (loc->dw_loc_opc)
1700 case DW_OP_addr:
1701 size += DWARF2_ADDR_SIZE;
1702 break;
1703 case DW_OP_GNU_addr_index:
1704 case DW_OP_GNU_const_index:
1705 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1706 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1707 break;
1708 case DW_OP_const1u:
1709 case DW_OP_const1s:
1710 size += 1;
1711 break;
1712 case DW_OP_const2u:
1713 case DW_OP_const2s:
1714 size += 2;
1715 break;
1716 case DW_OP_const4u:
1717 case DW_OP_const4s:
1718 size += 4;
1719 break;
1720 case DW_OP_const8u:
1721 case DW_OP_const8s:
1722 size += 8;
1723 break;
1724 case DW_OP_constu:
1725 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1726 break;
1727 case DW_OP_consts:
1728 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1729 break;
1730 case DW_OP_pick:
1731 size += 1;
1732 break;
1733 case DW_OP_plus_uconst:
1734 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1735 break;
1736 case DW_OP_skip:
1737 case DW_OP_bra:
1738 size += 2;
1739 break;
1740 case DW_OP_breg0:
1741 case DW_OP_breg1:
1742 case DW_OP_breg2:
1743 case DW_OP_breg3:
1744 case DW_OP_breg4:
1745 case DW_OP_breg5:
1746 case DW_OP_breg6:
1747 case DW_OP_breg7:
1748 case DW_OP_breg8:
1749 case DW_OP_breg9:
1750 case DW_OP_breg10:
1751 case DW_OP_breg11:
1752 case DW_OP_breg12:
1753 case DW_OP_breg13:
1754 case DW_OP_breg14:
1755 case DW_OP_breg15:
1756 case DW_OP_breg16:
1757 case DW_OP_breg17:
1758 case DW_OP_breg18:
1759 case DW_OP_breg19:
1760 case DW_OP_breg20:
1761 case DW_OP_breg21:
1762 case DW_OP_breg22:
1763 case DW_OP_breg23:
1764 case DW_OP_breg24:
1765 case DW_OP_breg25:
1766 case DW_OP_breg26:
1767 case DW_OP_breg27:
1768 case DW_OP_breg28:
1769 case DW_OP_breg29:
1770 case DW_OP_breg30:
1771 case DW_OP_breg31:
1772 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1773 break;
1774 case DW_OP_regx:
1775 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1776 break;
1777 case DW_OP_fbreg:
1778 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1779 break;
1780 case DW_OP_bregx:
1781 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1782 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1783 break;
1784 case DW_OP_piece:
1785 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1786 break;
1787 case DW_OP_bit_piece:
1788 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1789 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1790 break;
1791 case DW_OP_deref_size:
1792 case DW_OP_xderef_size:
1793 size += 1;
1794 break;
1795 case DW_OP_call2:
1796 size += 2;
1797 break;
1798 case DW_OP_call4:
1799 size += 4;
1800 break;
1801 case DW_OP_call_ref:
1802 case DW_OP_GNU_variable_value:
1803 size += DWARF_REF_SIZE;
1804 break;
1805 case DW_OP_implicit_value:
1806 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1807 + loc->dw_loc_oprnd1.v.val_unsigned;
1808 break;
1809 case DW_OP_implicit_pointer:
1810 case DW_OP_GNU_implicit_pointer:
1811 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1812 break;
1813 case DW_OP_entry_value:
1814 case DW_OP_GNU_entry_value:
1816 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1817 size += size_of_uleb128 (op_size) + op_size;
1818 break;
1820 case DW_OP_const_type:
1821 case DW_OP_GNU_const_type:
1823 unsigned long o
1824 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1825 size += size_of_uleb128 (o) + 1;
1826 switch (loc->dw_loc_oprnd2.val_class)
1828 case dw_val_class_vec:
1829 size += loc->dw_loc_oprnd2.v.val_vec.length
1830 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1831 break;
1832 case dw_val_class_const:
1833 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1834 break;
1835 case dw_val_class_const_double:
1836 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1837 break;
1838 case dw_val_class_wide_int:
1839 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1840 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1841 break;
1842 default:
1843 gcc_unreachable ();
1845 break;
1847 case DW_OP_regval_type:
1848 case DW_OP_GNU_regval_type:
1850 unsigned long o
1851 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1852 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1853 + size_of_uleb128 (o);
1855 break;
1856 case DW_OP_deref_type:
1857 case DW_OP_GNU_deref_type:
1859 unsigned long o
1860 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1861 size += 1 + size_of_uleb128 (o);
1863 break;
1864 case DW_OP_convert:
1865 case DW_OP_reinterpret:
1866 case DW_OP_GNU_convert:
1867 case DW_OP_GNU_reinterpret:
1868 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1869 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1870 else
1872 unsigned long o
1873 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1874 size += size_of_uleb128 (o);
1876 break;
1877 case DW_OP_GNU_parameter_ref:
1878 size += 4;
1879 break;
1880 default:
1881 break;
1884 return size;
1887 /* Return the size of a series of location descriptors. */
1889 unsigned long
1890 size_of_locs (dw_loc_descr_ref loc)
1892 dw_loc_descr_ref l;
1893 unsigned long size;
1895 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1896 field, to avoid writing to a PCH file. */
1897 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1899 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1900 break;
1901 size += size_of_loc_descr (l);
1903 if (! l)
1904 return size;
1906 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1908 l->dw_loc_addr = size;
1909 size += size_of_loc_descr (l);
1912 return size;
1915 /* Return the size of the value in a DW_AT_discr_value attribute. */
1917 static int
1918 size_of_discr_value (dw_discr_value *discr_value)
1920 if (discr_value->pos)
1921 return size_of_uleb128 (discr_value->v.uval);
1922 else
1923 return size_of_sleb128 (discr_value->v.sval);
1926 /* Return the size of the value in a DW_AT_discr_list attribute. */
1928 static int
1929 size_of_discr_list (dw_discr_list_ref discr_list)
1931 int size = 0;
1933 for (dw_discr_list_ref list = discr_list;
1934 list != NULL;
1935 list = list->dw_discr_next)
1937 /* One byte for the discriminant value descriptor, and then one or two
1938 LEB128 numbers, depending on whether it's a single case label or a
1939 range label. */
1940 size += 1;
1941 size += size_of_discr_value (&list->dw_discr_lower_bound);
1942 if (list->dw_discr_range != 0)
1943 size += size_of_discr_value (&list->dw_discr_upper_bound);
1945 return size;
1948 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1949 static void get_ref_die_offset_label (char *, dw_die_ref);
1950 static unsigned long int get_ref_die_offset (dw_die_ref);
1952 /* Output location description stack opcode's operands (if any).
1953 The for_eh_or_skip parameter controls whether register numbers are
1954 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1955 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1956 info). This should be suppressed for the cases that have not been converted
1957 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1959 static void
1960 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1962 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1963 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1965 switch (loc->dw_loc_opc)
1967 #ifdef DWARF2_DEBUGGING_INFO
1968 case DW_OP_const2u:
1969 case DW_OP_const2s:
1970 dw2_asm_output_data (2, val1->v.val_int, NULL);
1971 break;
1972 case DW_OP_const4u:
1973 if (loc->dtprel)
1975 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1976 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1977 val1->v.val_addr);
1978 fputc ('\n', asm_out_file);
1979 break;
1981 /* FALLTHRU */
1982 case DW_OP_const4s:
1983 dw2_asm_output_data (4, val1->v.val_int, NULL);
1984 break;
1985 case DW_OP_const8u:
1986 if (loc->dtprel)
1988 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1989 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1990 val1->v.val_addr);
1991 fputc ('\n', asm_out_file);
1992 break;
1994 /* FALLTHRU */
1995 case DW_OP_const8s:
1996 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1997 dw2_asm_output_data (8, val1->v.val_int, NULL);
1998 break;
1999 case DW_OP_skip:
2000 case DW_OP_bra:
2002 int offset;
2004 gcc_assert (val1->val_class == dw_val_class_loc);
2005 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2007 dw2_asm_output_data (2, offset, NULL);
2009 break;
2010 case DW_OP_implicit_value:
2011 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2012 switch (val2->val_class)
2014 case dw_val_class_const:
2015 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
2016 break;
2017 case dw_val_class_vec:
2019 unsigned int elt_size = val2->v.val_vec.elt_size;
2020 unsigned int len = val2->v.val_vec.length;
2021 unsigned int i;
2022 unsigned char *p;
2024 if (elt_size > sizeof (HOST_WIDE_INT))
2026 elt_size /= 2;
2027 len *= 2;
2029 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2030 i < len;
2031 i++, p += elt_size)
2032 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2033 "fp or vector constant word %u", i);
2035 break;
2036 case dw_val_class_const_double:
2038 unsigned HOST_WIDE_INT first, second;
2040 if (WORDS_BIG_ENDIAN)
2042 first = val2->v.val_double.high;
2043 second = val2->v.val_double.low;
2045 else
2047 first = val2->v.val_double.low;
2048 second = val2->v.val_double.high;
2050 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2051 first, NULL);
2052 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2053 second, NULL);
2055 break;
2056 case dw_val_class_wide_int:
2058 int i;
2059 int len = get_full_len (*val2->v.val_wide);
2060 if (WORDS_BIG_ENDIAN)
2061 for (i = len - 1; i >= 0; --i)
2062 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2063 val2->v.val_wide->elt (i), NULL);
2064 else
2065 for (i = 0; i < len; ++i)
2066 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2067 val2->v.val_wide->elt (i), NULL);
2069 break;
2070 case dw_val_class_addr:
2071 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
2072 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
2073 break;
2074 default:
2075 gcc_unreachable ();
2077 break;
2078 #else
2079 case DW_OP_const2u:
2080 case DW_OP_const2s:
2081 case DW_OP_const4u:
2082 case DW_OP_const4s:
2083 case DW_OP_const8u:
2084 case DW_OP_const8s:
2085 case DW_OP_skip:
2086 case DW_OP_bra:
2087 case DW_OP_implicit_value:
2088 /* We currently don't make any attempt to make sure these are
2089 aligned properly like we do for the main unwind info, so
2090 don't support emitting things larger than a byte if we're
2091 only doing unwinding. */
2092 gcc_unreachable ();
2093 #endif
2094 case DW_OP_const1u:
2095 case DW_OP_const1s:
2096 dw2_asm_output_data (1, val1->v.val_int, NULL);
2097 break;
2098 case DW_OP_constu:
2099 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2100 break;
2101 case DW_OP_consts:
2102 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2103 break;
2104 case DW_OP_pick:
2105 dw2_asm_output_data (1, val1->v.val_int, NULL);
2106 break;
2107 case DW_OP_plus_uconst:
2108 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2109 break;
2110 case DW_OP_breg0:
2111 case DW_OP_breg1:
2112 case DW_OP_breg2:
2113 case DW_OP_breg3:
2114 case DW_OP_breg4:
2115 case DW_OP_breg5:
2116 case DW_OP_breg6:
2117 case DW_OP_breg7:
2118 case DW_OP_breg8:
2119 case DW_OP_breg9:
2120 case DW_OP_breg10:
2121 case DW_OP_breg11:
2122 case DW_OP_breg12:
2123 case DW_OP_breg13:
2124 case DW_OP_breg14:
2125 case DW_OP_breg15:
2126 case DW_OP_breg16:
2127 case DW_OP_breg17:
2128 case DW_OP_breg18:
2129 case DW_OP_breg19:
2130 case DW_OP_breg20:
2131 case DW_OP_breg21:
2132 case DW_OP_breg22:
2133 case DW_OP_breg23:
2134 case DW_OP_breg24:
2135 case DW_OP_breg25:
2136 case DW_OP_breg26:
2137 case DW_OP_breg27:
2138 case DW_OP_breg28:
2139 case DW_OP_breg29:
2140 case DW_OP_breg30:
2141 case DW_OP_breg31:
2142 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2143 break;
2144 case DW_OP_regx:
2146 unsigned r = val1->v.val_unsigned;
2147 if (for_eh_or_skip >= 0)
2148 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2149 gcc_assert (size_of_uleb128 (r)
2150 == size_of_uleb128 (val1->v.val_unsigned));
2151 dw2_asm_output_data_uleb128 (r, NULL);
2153 break;
2154 case DW_OP_fbreg:
2155 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2156 break;
2157 case DW_OP_bregx:
2159 unsigned r = val1->v.val_unsigned;
2160 if (for_eh_or_skip >= 0)
2161 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2162 gcc_assert (size_of_uleb128 (r)
2163 == size_of_uleb128 (val1->v.val_unsigned));
2164 dw2_asm_output_data_uleb128 (r, NULL);
2165 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2167 break;
2168 case DW_OP_piece:
2169 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2170 break;
2171 case DW_OP_bit_piece:
2172 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2173 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2174 break;
2175 case DW_OP_deref_size:
2176 case DW_OP_xderef_size:
2177 dw2_asm_output_data (1, val1->v.val_int, NULL);
2178 break;
2180 case DW_OP_addr:
2181 if (loc->dtprel)
2183 if (targetm.asm_out.output_dwarf_dtprel)
2185 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2186 DWARF2_ADDR_SIZE,
2187 val1->v.val_addr);
2188 fputc ('\n', asm_out_file);
2190 else
2191 gcc_unreachable ();
2193 else
2195 #ifdef DWARF2_DEBUGGING_INFO
2196 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2197 #else
2198 gcc_unreachable ();
2199 #endif
2201 break;
2203 case DW_OP_GNU_addr_index:
2204 case DW_OP_GNU_const_index:
2205 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2206 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2207 "(index into .debug_addr)");
2208 break;
2210 case DW_OP_call2:
2211 case DW_OP_call4:
2213 unsigned long die_offset
2214 = get_ref_die_offset (val1->v.val_die_ref.die);
2215 /* Make sure the offset has been computed and that we can encode it as
2216 an operand. */
2217 gcc_assert (die_offset > 0
2218 && die_offset <= (loc->dw_loc_opc == DW_OP_call2
2219 ? 0xffff
2220 : 0xffffffff));
2221 dw2_asm_output_data ((loc->dw_loc_opc == DW_OP_call2) ? 2 : 4,
2222 die_offset, NULL);
2224 break;
2226 case DW_OP_call_ref:
2227 case DW_OP_GNU_variable_value:
2229 char label[MAX_ARTIFICIAL_LABEL_BYTES
2230 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2231 gcc_assert (val1->val_class == dw_val_class_die_ref);
2232 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2233 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2235 break;
2237 case DW_OP_implicit_pointer:
2238 case DW_OP_GNU_implicit_pointer:
2240 char label[MAX_ARTIFICIAL_LABEL_BYTES
2241 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2242 gcc_assert (val1->val_class == dw_val_class_die_ref);
2243 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2244 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2245 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2247 break;
2249 case DW_OP_entry_value:
2250 case DW_OP_GNU_entry_value:
2251 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2252 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2253 break;
2255 case DW_OP_const_type:
2256 case DW_OP_GNU_const_type:
2258 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2259 gcc_assert (o);
2260 dw2_asm_output_data_uleb128 (o, NULL);
2261 switch (val2->val_class)
2263 case dw_val_class_const:
2264 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2265 dw2_asm_output_data (1, l, NULL);
2266 dw2_asm_output_data (l, val2->v.val_int, NULL);
2267 break;
2268 case dw_val_class_vec:
2270 unsigned int elt_size = val2->v.val_vec.elt_size;
2271 unsigned int len = val2->v.val_vec.length;
2272 unsigned int i;
2273 unsigned char *p;
2275 l = len * elt_size;
2276 dw2_asm_output_data (1, l, NULL);
2277 if (elt_size > sizeof (HOST_WIDE_INT))
2279 elt_size /= 2;
2280 len *= 2;
2282 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2283 i < len;
2284 i++, p += elt_size)
2285 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2286 "fp or vector constant word %u", i);
2288 break;
2289 case dw_val_class_const_double:
2291 unsigned HOST_WIDE_INT first, second;
2292 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2294 dw2_asm_output_data (1, 2 * l, NULL);
2295 if (WORDS_BIG_ENDIAN)
2297 first = val2->v.val_double.high;
2298 second = val2->v.val_double.low;
2300 else
2302 first = val2->v.val_double.low;
2303 second = val2->v.val_double.high;
2305 dw2_asm_output_data (l, first, NULL);
2306 dw2_asm_output_data (l, second, NULL);
2308 break;
2309 case dw_val_class_wide_int:
2311 int i;
2312 int len = get_full_len (*val2->v.val_wide);
2313 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2315 dw2_asm_output_data (1, len * l, NULL);
2316 if (WORDS_BIG_ENDIAN)
2317 for (i = len - 1; i >= 0; --i)
2318 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2319 else
2320 for (i = 0; i < len; ++i)
2321 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2323 break;
2324 default:
2325 gcc_unreachable ();
2328 break;
2329 case DW_OP_regval_type:
2330 case DW_OP_GNU_regval_type:
2332 unsigned r = val1->v.val_unsigned;
2333 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2334 gcc_assert (o);
2335 if (for_eh_or_skip >= 0)
2337 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2338 gcc_assert (size_of_uleb128 (r)
2339 == size_of_uleb128 (val1->v.val_unsigned));
2341 dw2_asm_output_data_uleb128 (r, NULL);
2342 dw2_asm_output_data_uleb128 (o, NULL);
2344 break;
2345 case DW_OP_deref_type:
2346 case DW_OP_GNU_deref_type:
2348 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2349 gcc_assert (o);
2350 dw2_asm_output_data (1, val1->v.val_int, NULL);
2351 dw2_asm_output_data_uleb128 (o, NULL);
2353 break;
2354 case DW_OP_convert:
2355 case DW_OP_reinterpret:
2356 case DW_OP_GNU_convert:
2357 case DW_OP_GNU_reinterpret:
2358 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2359 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2360 else
2362 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2363 gcc_assert (o);
2364 dw2_asm_output_data_uleb128 (o, NULL);
2366 break;
2368 case DW_OP_GNU_parameter_ref:
2370 unsigned long o;
2371 gcc_assert (val1->val_class == dw_val_class_die_ref);
2372 o = get_ref_die_offset (val1->v.val_die_ref.die);
2373 dw2_asm_output_data (4, o, NULL);
2375 break;
2377 default:
2378 /* Other codes have no operands. */
2379 break;
2383 /* Output a sequence of location operations.
2384 The for_eh_or_skip parameter controls whether register numbers are
2385 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2386 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2387 info). This should be suppressed for the cases that have not been converted
2388 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2390 void
2391 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2393 for (; loc != NULL; loc = loc->dw_loc_next)
2395 enum dwarf_location_atom opc = loc->dw_loc_opc;
2396 /* Output the opcode. */
2397 if (for_eh_or_skip >= 0
2398 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2400 unsigned r = (opc - DW_OP_breg0);
2401 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2402 gcc_assert (r <= 31);
2403 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2405 else if (for_eh_or_skip >= 0
2406 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2408 unsigned r = (opc - DW_OP_reg0);
2409 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2410 gcc_assert (r <= 31);
2411 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2414 dw2_asm_output_data (1, opc,
2415 "%s", dwarf_stack_op_name (opc));
2417 /* Output the operand(s) (if any). */
2418 output_loc_operands (loc, for_eh_or_skip);
2422 /* Output location description stack opcode's operands (if any).
2423 The output is single bytes on a line, suitable for .cfi_escape. */
2425 static void
2426 output_loc_operands_raw (dw_loc_descr_ref loc)
2428 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2429 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2431 switch (loc->dw_loc_opc)
2433 case DW_OP_addr:
2434 case DW_OP_GNU_addr_index:
2435 case DW_OP_GNU_const_index:
2436 case DW_OP_implicit_value:
2437 /* We cannot output addresses in .cfi_escape, only bytes. */
2438 gcc_unreachable ();
2440 case DW_OP_const1u:
2441 case DW_OP_const1s:
2442 case DW_OP_pick:
2443 case DW_OP_deref_size:
2444 case DW_OP_xderef_size:
2445 fputc (',', asm_out_file);
2446 dw2_asm_output_data_raw (1, val1->v.val_int);
2447 break;
2449 case DW_OP_const2u:
2450 case DW_OP_const2s:
2451 fputc (',', asm_out_file);
2452 dw2_asm_output_data_raw (2, val1->v.val_int);
2453 break;
2455 case DW_OP_const4u:
2456 case DW_OP_const4s:
2457 fputc (',', asm_out_file);
2458 dw2_asm_output_data_raw (4, val1->v.val_int);
2459 break;
2461 case DW_OP_const8u:
2462 case DW_OP_const8s:
2463 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2464 fputc (',', asm_out_file);
2465 dw2_asm_output_data_raw (8, val1->v.val_int);
2466 break;
2468 case DW_OP_skip:
2469 case DW_OP_bra:
2471 int offset;
2473 gcc_assert (val1->val_class == dw_val_class_loc);
2474 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2476 fputc (',', asm_out_file);
2477 dw2_asm_output_data_raw (2, offset);
2479 break;
2481 case DW_OP_regx:
2483 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2484 gcc_assert (size_of_uleb128 (r)
2485 == size_of_uleb128 (val1->v.val_unsigned));
2486 fputc (',', asm_out_file);
2487 dw2_asm_output_data_uleb128_raw (r);
2489 break;
2491 case DW_OP_constu:
2492 case DW_OP_plus_uconst:
2493 case DW_OP_piece:
2494 fputc (',', asm_out_file);
2495 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2496 break;
2498 case DW_OP_bit_piece:
2499 fputc (',', asm_out_file);
2500 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2501 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2502 break;
2504 case DW_OP_consts:
2505 case DW_OP_breg0:
2506 case DW_OP_breg1:
2507 case DW_OP_breg2:
2508 case DW_OP_breg3:
2509 case DW_OP_breg4:
2510 case DW_OP_breg5:
2511 case DW_OP_breg6:
2512 case DW_OP_breg7:
2513 case DW_OP_breg8:
2514 case DW_OP_breg9:
2515 case DW_OP_breg10:
2516 case DW_OP_breg11:
2517 case DW_OP_breg12:
2518 case DW_OP_breg13:
2519 case DW_OP_breg14:
2520 case DW_OP_breg15:
2521 case DW_OP_breg16:
2522 case DW_OP_breg17:
2523 case DW_OP_breg18:
2524 case DW_OP_breg19:
2525 case DW_OP_breg20:
2526 case DW_OP_breg21:
2527 case DW_OP_breg22:
2528 case DW_OP_breg23:
2529 case DW_OP_breg24:
2530 case DW_OP_breg25:
2531 case DW_OP_breg26:
2532 case DW_OP_breg27:
2533 case DW_OP_breg28:
2534 case DW_OP_breg29:
2535 case DW_OP_breg30:
2536 case DW_OP_breg31:
2537 case DW_OP_fbreg:
2538 fputc (',', asm_out_file);
2539 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2540 break;
2542 case DW_OP_bregx:
2544 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2545 gcc_assert (size_of_uleb128 (r)
2546 == size_of_uleb128 (val1->v.val_unsigned));
2547 fputc (',', asm_out_file);
2548 dw2_asm_output_data_uleb128_raw (r);
2549 fputc (',', asm_out_file);
2550 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2552 break;
2554 case DW_OP_implicit_pointer:
2555 case DW_OP_entry_value:
2556 case DW_OP_const_type:
2557 case DW_OP_regval_type:
2558 case DW_OP_deref_type:
2559 case DW_OP_convert:
2560 case DW_OP_reinterpret:
2561 case DW_OP_GNU_implicit_pointer:
2562 case DW_OP_GNU_entry_value:
2563 case DW_OP_GNU_const_type:
2564 case DW_OP_GNU_regval_type:
2565 case DW_OP_GNU_deref_type:
2566 case DW_OP_GNU_convert:
2567 case DW_OP_GNU_reinterpret:
2568 case DW_OP_GNU_parameter_ref:
2569 gcc_unreachable ();
2570 break;
2572 default:
2573 /* Other codes have no operands. */
2574 break;
2578 void
2579 output_loc_sequence_raw (dw_loc_descr_ref loc)
2581 while (1)
2583 enum dwarf_location_atom opc = loc->dw_loc_opc;
2584 /* Output the opcode. */
2585 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2587 unsigned r = (opc - DW_OP_breg0);
2588 r = DWARF2_FRAME_REG_OUT (r, 1);
2589 gcc_assert (r <= 31);
2590 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2592 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2594 unsigned r = (opc - DW_OP_reg0);
2595 r = DWARF2_FRAME_REG_OUT (r, 1);
2596 gcc_assert (r <= 31);
2597 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2599 /* Output the opcode. */
2600 fprintf (asm_out_file, "%#x", opc);
2601 output_loc_operands_raw (loc);
2603 if (!loc->dw_loc_next)
2604 break;
2605 loc = loc->dw_loc_next;
2607 fputc (',', asm_out_file);
2611 /* This function builds a dwarf location descriptor sequence from a
2612 dw_cfa_location, adding the given OFFSET to the result of the
2613 expression. */
2615 struct dw_loc_descr_node *
2616 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2618 struct dw_loc_descr_node *head, *tmp;
2620 offset += cfa->offset;
2622 if (cfa->indirect)
2624 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2625 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2626 head->dw_loc_oprnd1.val_entry = NULL;
2627 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2628 add_loc_descr (&head, tmp);
2629 if (offset != 0)
2631 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2632 add_loc_descr (&head, tmp);
2635 else
2636 head = new_reg_loc_descr (cfa->reg, offset);
2638 return head;
2641 /* This function builds a dwarf location descriptor sequence for
2642 the address at OFFSET from the CFA when stack is aligned to
2643 ALIGNMENT byte. */
2645 struct dw_loc_descr_node *
2646 build_cfa_aligned_loc (dw_cfa_location *cfa,
2647 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2649 struct dw_loc_descr_node *head;
2650 unsigned int dwarf_fp
2651 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2653 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2654 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2656 head = new_reg_loc_descr (dwarf_fp, 0);
2657 add_loc_descr (&head, int_loc_descriptor (alignment));
2658 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2659 loc_descr_plus_const (&head, offset);
2661 else
2662 head = new_reg_loc_descr (dwarf_fp, offset);
2663 return head;
2666 /* And now, the support for symbolic debugging information. */
2668 /* .debug_str support. */
2670 static void dwarf2out_init (const char *);
2671 static void dwarf2out_finish (const char *);
2672 static void dwarf2out_early_finish (const char *);
2673 static void dwarf2out_assembly_start (void);
2674 static void dwarf2out_define (unsigned int, const char *);
2675 static void dwarf2out_undef (unsigned int, const char *);
2676 static void dwarf2out_start_source_file (unsigned, const char *);
2677 static void dwarf2out_end_source_file (unsigned);
2678 static void dwarf2out_function_decl (tree);
2679 static void dwarf2out_begin_block (unsigned, unsigned);
2680 static void dwarf2out_end_block (unsigned, unsigned);
2681 static bool dwarf2out_ignore_block (const_tree);
2682 static void dwarf2out_early_global_decl (tree);
2683 static void dwarf2out_late_global_decl (tree);
2684 static void dwarf2out_type_decl (tree, int);
2685 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool, bool);
2686 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2687 dw_die_ref);
2688 static void dwarf2out_abstract_function (tree);
2689 static void dwarf2out_var_location (rtx_insn *);
2690 static void dwarf2out_size_function (tree);
2691 static void dwarf2out_begin_function (tree);
2692 static void dwarf2out_end_function (unsigned int);
2693 static void dwarf2out_register_main_translation_unit (tree unit);
2694 static void dwarf2out_set_name (tree, tree);
2696 /* The debug hooks structure. */
2698 const struct gcc_debug_hooks dwarf2_debug_hooks =
2700 dwarf2out_init,
2701 dwarf2out_finish,
2702 dwarf2out_early_finish,
2703 dwarf2out_assembly_start,
2704 dwarf2out_define,
2705 dwarf2out_undef,
2706 dwarf2out_start_source_file,
2707 dwarf2out_end_source_file,
2708 dwarf2out_begin_block,
2709 dwarf2out_end_block,
2710 dwarf2out_ignore_block,
2711 dwarf2out_source_line,
2712 dwarf2out_begin_prologue,
2713 #if VMS_DEBUGGING_INFO
2714 dwarf2out_vms_end_prologue,
2715 dwarf2out_vms_begin_epilogue,
2716 #else
2717 debug_nothing_int_charstar,
2718 debug_nothing_int_charstar,
2719 #endif
2720 dwarf2out_end_epilogue,
2721 dwarf2out_begin_function,
2722 dwarf2out_end_function, /* end_function */
2723 dwarf2out_register_main_translation_unit,
2724 dwarf2out_function_decl, /* function_decl */
2725 dwarf2out_early_global_decl,
2726 dwarf2out_late_global_decl,
2727 dwarf2out_type_decl, /* type_decl */
2728 dwarf2out_imported_module_or_decl,
2729 debug_nothing_tree, /* deferred_inline_function */
2730 /* The DWARF 2 backend tries to reduce debugging bloat by not
2731 emitting the abstract description of inline functions until
2732 something tries to reference them. */
2733 dwarf2out_abstract_function, /* outlining_inline_function */
2734 debug_nothing_rtx_code_label, /* label */
2735 debug_nothing_int, /* handle_pch */
2736 dwarf2out_var_location,
2737 dwarf2out_size_function, /* size_function */
2738 dwarf2out_switch_text_section,
2739 dwarf2out_set_name,
2740 1, /* start_end_main_source_file */
2741 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2744 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2746 dwarf2out_init,
2747 debug_nothing_charstar,
2748 debug_nothing_charstar,
2749 dwarf2out_assembly_start,
2750 debug_nothing_int_charstar,
2751 debug_nothing_int_charstar,
2752 debug_nothing_int_charstar,
2753 debug_nothing_int,
2754 debug_nothing_int_int, /* begin_block */
2755 debug_nothing_int_int, /* end_block */
2756 debug_true_const_tree, /* ignore_block */
2757 dwarf2out_source_line, /* source_line */
2758 debug_nothing_int_int_charstar, /* begin_prologue */
2759 debug_nothing_int_charstar, /* end_prologue */
2760 debug_nothing_int_charstar, /* begin_epilogue */
2761 debug_nothing_int_charstar, /* end_epilogue */
2762 debug_nothing_tree, /* begin_function */
2763 debug_nothing_int, /* end_function */
2764 debug_nothing_tree, /* register_main_translation_unit */
2765 debug_nothing_tree, /* function_decl */
2766 debug_nothing_tree, /* early_global_decl */
2767 debug_nothing_tree, /* late_global_decl */
2768 debug_nothing_tree_int, /* type_decl */
2769 debug_nothing_tree_tree_tree_bool_bool,/* imported_module_or_decl */
2770 debug_nothing_tree, /* deferred_inline_function */
2771 debug_nothing_tree, /* outlining_inline_function */
2772 debug_nothing_rtx_code_label, /* label */
2773 debug_nothing_int, /* handle_pch */
2774 debug_nothing_rtx_insn, /* var_location */
2775 debug_nothing_tree, /* size_function */
2776 debug_nothing_void, /* switch_text_section */
2777 debug_nothing_tree_tree, /* set_name */
2778 0, /* start_end_main_source_file */
2779 TYPE_SYMTAB_IS_ADDRESS /* tree_type_symtab_field */
2782 /* NOTE: In the comments in this file, many references are made to
2783 "Debugging Information Entries". This term is abbreviated as `DIE'
2784 throughout the remainder of this file. */
2786 /* An internal representation of the DWARF output is built, and then
2787 walked to generate the DWARF debugging info. The walk of the internal
2788 representation is done after the entire program has been compiled.
2789 The types below are used to describe the internal representation. */
2791 /* Whether to put type DIEs into their own section .debug_types instead
2792 of making them part of the .debug_info section. Only supported for
2793 Dwarf V4 or higher and the user didn't disable them through
2794 -fno-debug-types-section. It is more efficient to put them in a
2795 separate comdat sections since the linker will then be able to
2796 remove duplicates. But not all tools support .debug_types sections
2797 yet. For Dwarf V5 or higher .debug_types doesn't exist any more,
2798 it is DW_UT_type unit type in .debug_info section. */
2800 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2802 /* Various DIE's use offsets relative to the beginning of the
2803 .debug_info section to refer to each other. */
2805 typedef long int dw_offset;
2807 struct comdat_type_node;
2809 /* The entries in the line_info table more-or-less mirror the opcodes
2810 that are used in the real dwarf line table. Arrays of these entries
2811 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2812 supported. */
2814 enum dw_line_info_opcode {
2815 /* Emit DW_LNE_set_address; the operand is the label index. */
2816 LI_set_address,
2818 /* Emit a row to the matrix with the given line. This may be done
2819 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2820 special opcodes. */
2821 LI_set_line,
2823 /* Emit a DW_LNS_set_file. */
2824 LI_set_file,
2826 /* Emit a DW_LNS_set_column. */
2827 LI_set_column,
2829 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2830 LI_negate_stmt,
2832 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2833 LI_set_prologue_end,
2834 LI_set_epilogue_begin,
2836 /* Emit a DW_LNE_set_discriminator. */
2837 LI_set_discriminator
2840 typedef struct GTY(()) dw_line_info_struct {
2841 enum dw_line_info_opcode opcode;
2842 unsigned int val;
2843 } dw_line_info_entry;
2846 struct GTY(()) dw_line_info_table {
2847 /* The label that marks the end of this section. */
2848 const char *end_label;
2850 /* The values for the last row of the matrix, as collected in the table.
2851 These are used to minimize the changes to the next row. */
2852 unsigned int file_num;
2853 unsigned int line_num;
2854 unsigned int column_num;
2855 int discrim_num;
2856 bool is_stmt;
2857 bool in_use;
2859 vec<dw_line_info_entry, va_gc> *entries;
2863 /* Each DIE attribute has a field specifying the attribute kind,
2864 a link to the next attribute in the chain, and an attribute value.
2865 Attributes are typically linked below the DIE they modify. */
2867 typedef struct GTY(()) dw_attr_struct {
2868 enum dwarf_attribute dw_attr;
2869 dw_val_node dw_attr_val;
2871 dw_attr_node;
2874 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2875 The children of each node form a circular list linked by
2876 die_sib. die_child points to the node *before* the "first" child node. */
2878 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2879 union die_symbol_or_type_node
2881 const char * GTY ((tag ("0"))) die_symbol;
2882 comdat_type_node *GTY ((tag ("1"))) die_type_node;
2884 GTY ((desc ("%0.comdat_type_p"))) die_id;
2885 vec<dw_attr_node, va_gc> *die_attr;
2886 dw_die_ref die_parent;
2887 dw_die_ref die_child;
2888 dw_die_ref die_sib;
2889 dw_die_ref die_definition; /* ref from a specification to its definition */
2890 dw_offset die_offset;
2891 unsigned long die_abbrev;
2892 int die_mark;
2893 unsigned int decl_id;
2894 enum dwarf_tag die_tag;
2895 /* Die is used and must not be pruned as unused. */
2896 BOOL_BITFIELD die_perennial_p : 1;
2897 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2898 /* Whether this DIE was removed from the DIE tree, for example via
2899 prune_unused_types. We don't consider those present from the
2900 DIE lookup routines. */
2901 BOOL_BITFIELD removed : 1;
2902 /* Lots of spare bits. */
2904 die_node;
2906 /* Set to TRUE while dwarf2out_early_global_decl is running. */
2907 static bool early_dwarf;
2908 static bool early_dwarf_finished;
2909 struct set_early_dwarf {
2910 bool saved;
2911 set_early_dwarf () : saved(early_dwarf)
2913 gcc_assert (! early_dwarf_finished);
2914 early_dwarf = true;
2916 ~set_early_dwarf () { early_dwarf = saved; }
2919 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2920 #define FOR_EACH_CHILD(die, c, expr) do { \
2921 c = die->die_child; \
2922 if (c) do { \
2923 c = c->die_sib; \
2924 expr; \
2925 } while (c != die->die_child); \
2926 } while (0)
2928 /* The pubname structure */
2930 typedef struct GTY(()) pubname_struct {
2931 dw_die_ref die;
2932 const char *name;
2934 pubname_entry;
2937 struct GTY(()) dw_ranges {
2938 const char *label;
2939 /* If this is positive, it's a block number, otherwise it's a
2940 bitwise-negated index into dw_ranges_by_label. */
2941 int num;
2942 /* Index for the range list for DW_FORM_rnglistx. */
2943 unsigned int idx : 31;
2944 /* True if this range might be possibly in a different section
2945 from previous entry. */
2946 unsigned int maybe_new_sec : 1;
2949 /* A structure to hold a macinfo entry. */
2951 typedef struct GTY(()) macinfo_struct {
2952 unsigned char code;
2953 unsigned HOST_WIDE_INT lineno;
2954 const char *info;
2956 macinfo_entry;
2959 struct GTY(()) dw_ranges_by_label {
2960 const char *begin;
2961 const char *end;
2964 /* The comdat type node structure. */
2965 struct GTY(()) comdat_type_node
2967 dw_die_ref root_die;
2968 dw_die_ref type_die;
2969 dw_die_ref skeleton_die;
2970 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2971 comdat_type_node *next;
2974 /* A list of DIEs for which we can't determine ancestry (parent_die
2975 field) just yet. Later in dwarf2out_finish we will fill in the
2976 missing bits. */
2977 typedef struct GTY(()) limbo_die_struct {
2978 dw_die_ref die;
2979 /* The tree for which this DIE was created. We use this to
2980 determine ancestry later. */
2981 tree created_for;
2982 struct limbo_die_struct *next;
2984 limbo_die_node;
2986 typedef struct skeleton_chain_struct
2988 dw_die_ref old_die;
2989 dw_die_ref new_die;
2990 struct skeleton_chain_struct *parent;
2992 skeleton_chain_node;
2994 /* Define a macro which returns nonzero for a TYPE_DECL which was
2995 implicitly generated for a type.
2997 Note that, unlike the C front-end (which generates a NULL named
2998 TYPE_DECL node for each complete tagged type, each array type,
2999 and each function type node created) the C++ front-end generates
3000 a _named_ TYPE_DECL node for each tagged type node created.
3001 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3002 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
3003 front-end, but for each type, tagged or not. */
3005 #define TYPE_DECL_IS_STUB(decl) \
3006 (DECL_NAME (decl) == NULL_TREE \
3007 || (DECL_ARTIFICIAL (decl) \
3008 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3009 /* This is necessary for stub decls that \
3010 appear in nested inline functions. */ \
3011 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3012 && (decl_ultimate_origin (decl) \
3013 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3015 /* Information concerning the compilation unit's programming
3016 language, and compiler version. */
3018 /* Fixed size portion of the DWARF compilation unit header. */
3019 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3020 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE \
3021 + (dwarf_version >= 5 ? 4 : 3))
3023 /* Fixed size portion of the DWARF comdat type unit header. */
3024 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
3025 (DWARF_COMPILE_UNIT_HEADER_SIZE \
3026 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE)
3028 /* Fixed size portion of the DWARF skeleton compilation unit header. */
3029 #define DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE \
3030 (DWARF_COMPILE_UNIT_HEADER_SIZE + (dwarf_version >= 5 ? 8 : 0))
3032 /* Fixed size portion of public names info. */
3033 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3035 /* Fixed size portion of the address range info. */
3036 #define DWARF_ARANGES_HEADER_SIZE \
3037 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3038 DWARF2_ADDR_SIZE * 2) \
3039 - DWARF_INITIAL_LENGTH_SIZE)
3041 /* Size of padding portion in the address range info. It must be
3042 aligned to twice the pointer size. */
3043 #define DWARF_ARANGES_PAD_SIZE \
3044 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3045 DWARF2_ADDR_SIZE * 2) \
3046 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3048 /* Use assembler line directives if available. */
3049 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3050 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3051 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3052 #else
3053 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3054 #endif
3055 #endif
3057 /* Minimum line offset in a special line info. opcode.
3058 This value was chosen to give a reasonable range of values. */
3059 #define DWARF_LINE_BASE -10
3061 /* First special line opcode - leave room for the standard opcodes. */
3062 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
3064 /* Range of line offsets in a special line info. opcode. */
3065 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3067 /* Flag that indicates the initial value of the is_stmt_start flag.
3068 In the present implementation, we do not mark any lines as
3069 the beginning of a source statement, because that information
3070 is not made available by the GCC front-end. */
3071 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3073 /* Maximum number of operations per instruction bundle. */
3074 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
3075 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
3076 #endif
3078 /* This location is used by calc_die_sizes() to keep track
3079 the offset of each DIE within the .debug_info section. */
3080 static unsigned long next_die_offset;
3082 /* Record the root of the DIE's built for the current compilation unit. */
3083 static GTY(()) dw_die_ref single_comp_unit_die;
3085 /* A list of type DIEs that have been separated into comdat sections. */
3086 static GTY(()) comdat_type_node *comdat_type_list;
3088 /* A list of CU DIEs that have been separated. */
3089 static GTY(()) limbo_die_node *cu_die_list;
3091 /* A list of DIEs with a NULL parent waiting to be relocated. */
3092 static GTY(()) limbo_die_node *limbo_die_list;
3094 /* A list of DIEs for which we may have to generate
3095 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
3096 static GTY(()) limbo_die_node *deferred_asm_name;
3098 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
3100 typedef const char *compare_type;
3102 static hashval_t hash (dwarf_file_data *);
3103 static bool equal (dwarf_file_data *, const char *);
3106 /* Filenames referenced by this compilation unit. */
3107 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
3109 struct decl_die_hasher : ggc_ptr_hash<die_node>
3111 typedef tree compare_type;
3113 static hashval_t hash (die_node *);
3114 static bool equal (die_node *, tree);
3116 /* A hash table of references to DIE's that describe declarations.
3117 The key is a DECL_UID() which is a unique number identifying each decl. */
3118 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
3120 struct GTY ((for_user)) variable_value_struct {
3121 unsigned int decl_id;
3122 vec<dw_die_ref, va_gc> *dies;
3125 struct variable_value_hasher : ggc_ptr_hash<variable_value_struct>
3127 typedef tree compare_type;
3129 static hashval_t hash (variable_value_struct *);
3130 static bool equal (variable_value_struct *, tree);
3132 /* A hash table of DIEs that contain DW_OP_GNU_variable_value with
3133 dw_val_class_decl_ref class, indexed by FUNCTION_DECLs which is
3134 DECL_CONTEXT of the referenced VAR_DECLs. */
3135 static GTY (()) hash_table<variable_value_hasher> *variable_value_hash;
3137 struct block_die_hasher : ggc_ptr_hash<die_struct>
3139 static hashval_t hash (die_struct *);
3140 static bool equal (die_struct *, die_struct *);
3143 /* A hash table of references to DIE's that describe COMMON blocks.
3144 The key is DECL_UID() ^ die_parent. */
3145 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
3147 typedef struct GTY(()) die_arg_entry_struct {
3148 dw_die_ref die;
3149 tree arg;
3150 } die_arg_entry;
3153 /* Node of the variable location list. */
3154 struct GTY ((chain_next ("%h.next"))) var_loc_node {
3155 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
3156 EXPR_LIST chain. For small bitsizes, bitsize is encoded
3157 in mode of the EXPR_LIST node and first EXPR_LIST operand
3158 is either NOTE_INSN_VAR_LOCATION for a piece with a known
3159 location or NULL for padding. For larger bitsizes,
3160 mode is 0 and first operand is a CONCAT with bitsize
3161 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
3162 NULL as second operand. */
3163 rtx GTY (()) loc;
3164 const char * GTY (()) label;
3165 struct var_loc_node * GTY (()) next;
3168 /* Variable location list. */
3169 struct GTY ((for_user)) var_loc_list_def {
3170 struct var_loc_node * GTY (()) first;
3172 /* Pointer to the last but one or last element of the
3173 chained list. If the list is empty, both first and
3174 last are NULL, if the list contains just one node
3175 or the last node certainly is not redundant, it points
3176 to the last node, otherwise points to the last but one.
3177 Do not mark it for GC because it is marked through the chain. */
3178 struct var_loc_node * GTY ((skip ("%h"))) last;
3180 /* Pointer to the last element before section switch,
3181 if NULL, either sections weren't switched or first
3182 is after section switch. */
3183 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
3185 /* DECL_UID of the variable decl. */
3186 unsigned int decl_id;
3188 typedef struct var_loc_list_def var_loc_list;
3190 /* Call argument location list. */
3191 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
3192 rtx GTY (()) call_arg_loc_note;
3193 const char * GTY (()) label;
3194 tree GTY (()) block;
3195 bool tail_call_p;
3196 rtx GTY (()) symbol_ref;
3197 struct call_arg_loc_node * GTY (()) next;
3201 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
3203 typedef const_tree compare_type;
3205 static hashval_t hash (var_loc_list *);
3206 static bool equal (var_loc_list *, const_tree);
3209 /* Table of decl location linked lists. */
3210 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
3212 /* Head and tail of call_arg_loc chain. */
3213 static GTY (()) struct call_arg_loc_node *call_arg_locations;
3214 static struct call_arg_loc_node *call_arg_loc_last;
3216 /* Number of call sites in the current function. */
3217 static int call_site_count = -1;
3218 /* Number of tail call sites in the current function. */
3219 static int tail_call_site_count = -1;
3221 /* A cached location list. */
3222 struct GTY ((for_user)) cached_dw_loc_list_def {
3223 /* The DECL_UID of the decl that this entry describes. */
3224 unsigned int decl_id;
3226 /* The cached location list. */
3227 dw_loc_list_ref loc_list;
3229 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3231 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
3234 typedef const_tree compare_type;
3236 static hashval_t hash (cached_dw_loc_list *);
3237 static bool equal (cached_dw_loc_list *, const_tree);
3240 /* Table of cached location lists. */
3241 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
3243 /* A vector of references to DIE's that are uniquely identified by their tag,
3244 presence/absence of children DIE's, and list of attribute/value pairs. */
3245 static GTY(()) vec<dw_die_ref, va_gc> *abbrev_die_table;
3247 /* A hash map to remember the stack usage for DWARF procedures. The value
3248 stored is the stack size difference between before the DWARF procedure
3249 invokation and after it returned. In other words, for a DWARF procedure
3250 that consumes N stack slots and that pushes M ones, this stores M - N. */
3251 static hash_map<dw_die_ref, int> *dwarf_proc_stack_usage_map;
3253 /* A global counter for generating labels for line number data. */
3254 static unsigned int line_info_label_num;
3256 /* The current table to which we should emit line number information
3257 for the current function. This will be set up at the beginning of
3258 assembly for the function. */
3259 static GTY(()) dw_line_info_table *cur_line_info_table;
3261 /* The two default tables of line number info. */
3262 static GTY(()) dw_line_info_table *text_section_line_info;
3263 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3265 /* The set of all non-default tables of line number info. */
3266 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
3268 /* A flag to tell pubnames/types export if there is an info section to
3269 refer to. */
3270 static bool info_section_emitted;
3272 /* A pointer to the base of a table that contains a list of publicly
3273 accessible names. */
3274 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3276 /* A pointer to the base of a table that contains a list of publicly
3277 accessible types. */
3278 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3280 /* A pointer to the base of a table that contains a list of macro
3281 defines/undefines (and file start/end markers). */
3282 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3284 /* True if .debug_macinfo or .debug_macros section is going to be
3285 emitted. */
3286 #define have_macinfo \
3287 ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3288 && debug_info_level >= DINFO_LEVEL_VERBOSE \
3289 && !macinfo_table->is_empty ())
3291 /* Vector of dies for which we should generate .debug_ranges info. */
3292 static GTY (()) vec<dw_ranges, va_gc> *ranges_table;
3294 /* Vector of pairs of labels referenced in ranges_table. */
3295 static GTY (()) vec<dw_ranges_by_label, va_gc> *ranges_by_label;
3297 /* Whether we have location lists that need outputting */
3298 static GTY(()) bool have_location_lists;
3300 /* Unique label counter. */
3301 static GTY(()) unsigned int loclabel_num;
3303 /* Unique label counter for point-of-call tables. */
3304 static GTY(()) unsigned int poc_label_num;
3306 /* The last file entry emitted by maybe_emit_file(). */
3307 static GTY(()) struct dwarf_file_data * last_emitted_file;
3309 /* Number of internal labels generated by gen_internal_sym(). */
3310 static GTY(()) int label_num;
3312 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3314 /* Instances of generic types for which we need to generate debug
3315 info that describe their generic parameters and arguments. That
3316 generation needs to happen once all types are properly laid out so
3317 we do it at the end of compilation. */
3318 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3320 /* Offset from the "steady-state frame pointer" to the frame base,
3321 within the current function. */
3322 static HOST_WIDE_INT frame_pointer_fb_offset;
3323 static bool frame_pointer_fb_offset_valid;
3325 static vec<dw_die_ref> base_types;
3327 /* Flags to represent a set of attribute classes for attributes that represent
3328 a scalar value (bounds, pointers, ...). */
3329 enum dw_scalar_form
3331 dw_scalar_form_constant = 0x01,
3332 dw_scalar_form_exprloc = 0x02,
3333 dw_scalar_form_reference = 0x04
3336 /* Forward declarations for functions defined in this file. */
3338 static int is_pseudo_reg (const_rtx);
3339 static tree type_main_variant (tree);
3340 static int is_tagged_type (const_tree);
3341 static const char *dwarf_tag_name (unsigned);
3342 static const char *dwarf_attr_name (unsigned);
3343 static const char *dwarf_form_name (unsigned);
3344 static tree decl_ultimate_origin (const_tree);
3345 static tree decl_class_context (tree);
3346 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3347 static inline enum dw_val_class AT_class (dw_attr_node *);
3348 static inline unsigned int AT_index (dw_attr_node *);
3349 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3350 static inline unsigned AT_flag (dw_attr_node *);
3351 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3352 static inline HOST_WIDE_INT AT_int (dw_attr_node *);
3353 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3354 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_node *);
3355 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3356 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3357 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3358 unsigned int, unsigned char *);
3359 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3360 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3361 static inline const char *AT_string (dw_attr_node *);
3362 static enum dwarf_form AT_string_form (dw_attr_node *);
3363 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3364 static void add_AT_specification (dw_die_ref, dw_die_ref);
3365 static inline dw_die_ref AT_ref (dw_attr_node *);
3366 static inline int AT_ref_external (dw_attr_node *);
3367 static inline void set_AT_ref_external (dw_attr_node *, int);
3368 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3369 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3370 static inline dw_loc_descr_ref AT_loc (dw_attr_node *);
3371 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3372 dw_loc_list_ref);
3373 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3374 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3375 static void remove_addr_table_entry (addr_table_entry *);
3376 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3377 static inline rtx AT_addr (dw_attr_node *);
3378 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3379 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3380 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3381 static void add_AT_loclistsptr (dw_die_ref, enum dwarf_attribute,
3382 const char *);
3383 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3384 unsigned HOST_WIDE_INT);
3385 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3386 unsigned long, bool);
3387 static inline const char *AT_lbl (dw_attr_node *);
3388 static dw_attr_node *get_AT (dw_die_ref, enum dwarf_attribute);
3389 static const char *get_AT_low_pc (dw_die_ref);
3390 static const char *get_AT_hi_pc (dw_die_ref);
3391 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3392 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3393 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3394 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3395 static bool is_cxx (void);
3396 static bool is_cxx (const_tree);
3397 static bool is_fortran (void);
3398 static bool is_ada (void);
3399 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3400 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3401 static void add_child_die (dw_die_ref, dw_die_ref);
3402 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3403 static dw_die_ref lookup_type_die (tree);
3404 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3405 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3406 static void equate_type_number_to_die (tree, dw_die_ref);
3407 static dw_die_ref lookup_decl_die (tree);
3408 static var_loc_list *lookup_decl_loc (const_tree);
3409 static void equate_decl_number_to_die (tree, dw_die_ref);
3410 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3411 static void print_spaces (FILE *);
3412 static void print_die (dw_die_ref, FILE *);
3413 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3414 static dw_die_ref pop_compile_unit (dw_die_ref);
3415 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3416 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3417 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3418 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3419 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3420 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3421 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3422 struct md5_ctx *, int *);
3423 struct checksum_attributes;
3424 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3425 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3426 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3427 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3428 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3429 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3430 static int same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3431 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3432 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3433 static void compute_section_prefix (dw_die_ref);
3434 static int is_type_die (dw_die_ref);
3435 static int is_comdat_die (dw_die_ref);
3436 static int is_symbol_die (dw_die_ref);
3437 static inline bool is_template_instantiation (dw_die_ref);
3438 static void assign_symbol_names (dw_die_ref);
3439 static void break_out_includes (dw_die_ref);
3440 static int is_declaration_die (dw_die_ref);
3441 static int should_move_die_to_comdat (dw_die_ref);
3442 static dw_die_ref clone_as_declaration (dw_die_ref);
3443 static dw_die_ref clone_die (dw_die_ref);
3444 static dw_die_ref clone_tree (dw_die_ref);
3445 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3446 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3447 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3448 static dw_die_ref generate_skeleton (dw_die_ref);
3449 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3450 dw_die_ref,
3451 dw_die_ref);
3452 static void break_out_comdat_types (dw_die_ref);
3453 static void copy_decls_for_unworthy_types (dw_die_ref);
3455 static void add_sibling_attributes (dw_die_ref);
3456 static void output_location_lists (dw_die_ref);
3457 static int constant_size (unsigned HOST_WIDE_INT);
3458 static unsigned long size_of_die (dw_die_ref);
3459 static void calc_die_sizes (dw_die_ref);
3460 static void calc_base_type_die_sizes (void);
3461 static void mark_dies (dw_die_ref);
3462 static void unmark_dies (dw_die_ref);
3463 static void unmark_all_dies (dw_die_ref);
3464 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3465 static unsigned long size_of_aranges (void);
3466 static enum dwarf_form value_format (dw_attr_node *);
3467 static void output_value_format (dw_attr_node *);
3468 static void output_abbrev_section (void);
3469 static void output_die_abbrevs (unsigned long, dw_die_ref);
3470 static void output_die_symbol (dw_die_ref);
3471 static void output_die (dw_die_ref);
3472 static void output_compilation_unit_header (enum dwarf_unit_type);
3473 static void output_comp_unit (dw_die_ref, int, const unsigned char *);
3474 static void output_comdat_type_unit (comdat_type_node *);
3475 static const char *dwarf2_name (tree, int);
3476 static void add_pubname (tree, dw_die_ref);
3477 static void add_enumerator_pubname (const char *, dw_die_ref);
3478 static void add_pubname_string (const char *, dw_die_ref);
3479 static void add_pubtype (tree, dw_die_ref);
3480 static void output_pubnames (vec<pubname_entry, va_gc> *);
3481 static void output_aranges (void);
3482 static unsigned int add_ranges (const_tree, bool = false);
3483 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3484 bool *, bool);
3485 static void output_ranges (void);
3486 static dw_line_info_table *new_line_info_table (void);
3487 static void output_line_info (bool);
3488 static void output_file_names (void);
3489 static dw_die_ref base_type_die (tree, bool);
3490 static int is_base_type (tree);
3491 static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3492 static int decl_quals (const_tree);
3493 static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3494 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3495 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3496 static int type_is_enum (const_tree);
3497 static unsigned int dbx_reg_number (const_rtx);
3498 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3499 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3500 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3501 enum var_init_status);
3502 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3503 enum var_init_status);
3504 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3505 enum var_init_status);
3506 static int is_based_loc (const_rtx);
3507 static bool resolve_one_addr (rtx *);
3508 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3509 enum var_init_status);
3510 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3511 enum var_init_status);
3512 struct loc_descr_context;
3513 static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3514 static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3515 static dw_loc_list_ref loc_list_from_tree (tree, int,
3516 struct loc_descr_context *);
3517 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3518 struct loc_descr_context *);
3519 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3520 static tree field_type (const_tree);
3521 static unsigned int simple_type_align_in_bits (const_tree);
3522 static unsigned int simple_decl_align_in_bits (const_tree);
3523 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3524 struct vlr_context;
3525 static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3526 HOST_WIDE_INT *);
3527 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3528 dw_loc_list_ref);
3529 static void add_data_member_location_attribute (dw_die_ref, tree,
3530 struct vlr_context *);
3531 static bool add_const_value_attribute (dw_die_ref, rtx);
3532 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3533 static void insert_wide_int (const wide_int &, unsigned char *, int);
3534 static void insert_float (const_rtx, unsigned char *);
3535 static rtx rtl_for_decl_location (tree);
3536 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3537 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3538 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3539 static void add_name_attribute (dw_die_ref, const char *);
3540 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3541 static void add_comp_dir_attribute (dw_die_ref);
3542 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3543 struct loc_descr_context *);
3544 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3545 struct loc_descr_context *);
3546 static void add_subscript_info (dw_die_ref, tree, bool);
3547 static void add_byte_size_attribute (dw_die_ref, tree);
3548 static void add_alignment_attribute (dw_die_ref, tree);
3549 static inline void add_bit_offset_attribute (dw_die_ref, tree,
3550 struct vlr_context *);
3551 static void add_bit_size_attribute (dw_die_ref, tree);
3552 static void add_prototyped_attribute (dw_die_ref, tree);
3553 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3554 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3555 static void add_src_coords_attributes (dw_die_ref, tree);
3556 static void add_name_and_src_coords_attributes (dw_die_ref, tree, bool = false);
3557 static void add_discr_value (dw_die_ref, dw_discr_value *);
3558 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3559 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3560 static void push_decl_scope (tree);
3561 static void pop_decl_scope (void);
3562 static dw_die_ref scope_die_for (tree, dw_die_ref);
3563 static inline int local_scope_p (dw_die_ref);
3564 static inline int class_scope_p (dw_die_ref);
3565 static inline int class_or_namespace_scope_p (dw_die_ref);
3566 static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3567 static void add_calling_convention_attribute (dw_die_ref, tree);
3568 static const char *type_tag (const_tree);
3569 static tree member_declared_type (const_tree);
3570 #if 0
3571 static const char *decl_start_label (tree);
3572 #endif
3573 static void gen_array_type_die (tree, dw_die_ref);
3574 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3575 #if 0
3576 static void gen_entry_point_die (tree, dw_die_ref);
3577 #endif
3578 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3579 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3580 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3581 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3582 static void gen_formal_types_die (tree, dw_die_ref);
3583 static void gen_subprogram_die (tree, dw_die_ref);
3584 static void gen_variable_die (tree, tree, dw_die_ref);
3585 static void gen_const_die (tree, dw_die_ref);
3586 static void gen_label_die (tree, dw_die_ref);
3587 static void gen_lexical_block_die (tree, dw_die_ref);
3588 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3589 static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3590 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3591 static dw_die_ref gen_compile_unit_die (const char *);
3592 static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3593 static void gen_member_die (tree, dw_die_ref);
3594 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3595 enum debug_info_usage);
3596 static void gen_subroutine_type_die (tree, dw_die_ref);
3597 static void gen_typedef_die (tree, dw_die_ref);
3598 static void gen_type_die (tree, dw_die_ref);
3599 static void gen_block_die (tree, dw_die_ref);
3600 static void decls_for_scope (tree, dw_die_ref);
3601 static bool is_naming_typedef_decl (const_tree);
3602 static inline dw_die_ref get_context_die (tree);
3603 static void gen_namespace_die (tree, dw_die_ref);
3604 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3605 static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3606 static dw_die_ref force_decl_die (tree);
3607 static dw_die_ref force_type_die (tree);
3608 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3609 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3610 static struct dwarf_file_data * lookup_filename (const char *);
3611 static void retry_incomplete_types (void);
3612 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3613 static void gen_generic_params_dies (tree);
3614 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3615 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3616 static void splice_child_die (dw_die_ref, dw_die_ref);
3617 static int file_info_cmp (const void *, const void *);
3618 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3619 const char *, const char *);
3620 static void output_loc_list (dw_loc_list_ref);
3621 static char *gen_internal_sym (const char *);
3622 static bool want_pubnames (void);
3624 static void prune_unmark_dies (dw_die_ref);
3625 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3626 static void prune_unused_types_mark (dw_die_ref, int);
3627 static void prune_unused_types_walk (dw_die_ref);
3628 static void prune_unused_types_walk_attribs (dw_die_ref);
3629 static void prune_unused_types_prune (dw_die_ref);
3630 static void prune_unused_types (void);
3631 static int maybe_emit_file (struct dwarf_file_data *fd);
3632 static inline const char *AT_vms_delta1 (dw_attr_node *);
3633 static inline const char *AT_vms_delta2 (dw_attr_node *);
3634 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3635 const char *, const char *);
3636 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3637 static void gen_remaining_tmpl_value_param_die_attribute (void);
3638 static bool generic_type_p (tree);
3639 static void schedule_generic_params_dies_gen (tree t);
3640 static void gen_scheduled_generic_parms_dies (void);
3641 static void resolve_variable_values (void);
3643 static const char *comp_dir_string (void);
3645 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3647 /* enum for tracking thread-local variables whose address is really an offset
3648 relative to the TLS pointer, which will need link-time relocation, but will
3649 not need relocation by the DWARF consumer. */
3651 enum dtprel_bool
3653 dtprel_false = 0,
3654 dtprel_true = 1
3657 /* Return the operator to use for an address of a variable. For dtprel_true, we
3658 use DW_OP_const*. For regular variables, which need both link-time
3659 relocation and consumer-level relocation (e.g., to account for shared objects
3660 loaded at a random address), we use DW_OP_addr*. */
3662 static inline enum dwarf_location_atom
3663 dw_addr_op (enum dtprel_bool dtprel)
3665 if (dtprel == dtprel_true)
3666 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3667 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3668 else
3669 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3672 /* Return a pointer to a newly allocated address location description. If
3673 dwarf_split_debug_info is true, then record the address with the appropriate
3674 relocation. */
3675 static inline dw_loc_descr_ref
3676 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3678 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3680 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3681 ref->dw_loc_oprnd1.v.val_addr = addr;
3682 ref->dtprel = dtprel;
3683 if (dwarf_split_debug_info)
3684 ref->dw_loc_oprnd1.val_entry
3685 = add_addr_table_entry (addr,
3686 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3687 else
3688 ref->dw_loc_oprnd1.val_entry = NULL;
3690 return ref;
3693 /* Section names used to hold DWARF debugging information. */
3695 #ifndef DEBUG_INFO_SECTION
3696 #define DEBUG_INFO_SECTION ".debug_info"
3697 #endif
3698 #ifndef DEBUG_DWO_INFO_SECTION
3699 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3700 #endif
3701 #ifndef DEBUG_ABBREV_SECTION
3702 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3703 #endif
3704 #ifndef DEBUG_DWO_ABBREV_SECTION
3705 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3706 #endif
3707 #ifndef DEBUG_ARANGES_SECTION
3708 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3709 #endif
3710 #ifndef DEBUG_ADDR_SECTION
3711 #define DEBUG_ADDR_SECTION ".debug_addr"
3712 #endif
3713 #ifndef DEBUG_MACINFO_SECTION
3714 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
3715 #endif
3716 #ifndef DEBUG_DWO_MACINFO_SECTION
3717 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3718 #endif
3719 #ifndef DEBUG_DWO_MACRO_SECTION
3720 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3721 #endif
3722 #ifndef DEBUG_MACRO_SECTION
3723 #define DEBUG_MACRO_SECTION ".debug_macro"
3724 #endif
3725 #ifndef DEBUG_LINE_SECTION
3726 #define DEBUG_LINE_SECTION ".debug_line"
3727 #endif
3728 #ifndef DEBUG_DWO_LINE_SECTION
3729 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3730 #endif
3731 #ifndef DEBUG_LOC_SECTION
3732 #define DEBUG_LOC_SECTION ".debug_loc"
3733 #endif
3734 #ifndef DEBUG_DWO_LOC_SECTION
3735 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3736 #endif
3737 #ifndef DEBUG_LOCLISTS_SECTION
3738 #define DEBUG_LOCLISTS_SECTION ".debug_loclists"
3739 #endif
3740 #ifndef DEBUG_DWO_LOCLISTS_SECTION
3741 #define DEBUG_DWO_LOCLISTS_SECTION ".debug_loclists.dwo"
3742 #endif
3743 #ifndef DEBUG_PUBNAMES_SECTION
3744 #define DEBUG_PUBNAMES_SECTION \
3745 ((debug_generate_pub_sections == 2) \
3746 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3747 #endif
3748 #ifndef DEBUG_PUBTYPES_SECTION
3749 #define DEBUG_PUBTYPES_SECTION \
3750 ((debug_generate_pub_sections == 2) \
3751 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3752 #endif
3753 #ifndef DEBUG_STR_OFFSETS_SECTION
3754 #define DEBUG_STR_OFFSETS_SECTION ".debug_str_offsets"
3755 #endif
3756 #ifndef DEBUG_DWO_STR_OFFSETS_SECTION
3757 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3758 #endif
3759 #ifndef DEBUG_STR_DWO_SECTION
3760 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3761 #endif
3762 #ifndef DEBUG_STR_SECTION
3763 #define DEBUG_STR_SECTION ".debug_str"
3764 #endif
3765 #ifndef DEBUG_RANGES_SECTION
3766 #define DEBUG_RANGES_SECTION ".debug_ranges"
3767 #endif
3768 #ifndef DEBUG_RNGLISTS_SECTION
3769 #define DEBUG_RNGLISTS_SECTION ".debug_rnglists"
3770 #endif
3771 #ifndef DEBUG_LINE_STR_SECTION
3772 #define DEBUG_LINE_STR_SECTION ".debug_line_str"
3773 #endif
3775 /* Standard ELF section names for compiled code and data. */
3776 #ifndef TEXT_SECTION_NAME
3777 #define TEXT_SECTION_NAME ".text"
3778 #endif
3780 /* Section flags for .debug_str section. */
3781 #define DEBUG_STR_SECTION_FLAGS \
3782 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3783 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3784 : SECTION_DEBUG)
3786 /* Section flags for .debug_str.dwo section. */
3787 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3789 /* Labels we insert at beginning sections we can reference instead of
3790 the section names themselves. */
3792 #ifndef TEXT_SECTION_LABEL
3793 #define TEXT_SECTION_LABEL "Ltext"
3794 #endif
3795 #ifndef COLD_TEXT_SECTION_LABEL
3796 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3797 #endif
3798 #ifndef DEBUG_LINE_SECTION_LABEL
3799 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3800 #endif
3801 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3802 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3803 #endif
3804 #ifndef DEBUG_INFO_SECTION_LABEL
3805 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3806 #endif
3807 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3808 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3809 #endif
3810 #ifndef DEBUG_ABBREV_SECTION_LABEL
3811 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3812 #endif
3813 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3814 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3815 #endif
3816 #ifndef DEBUG_ADDR_SECTION_LABEL
3817 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3818 #endif
3819 #ifndef DEBUG_LOC_SECTION_LABEL
3820 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3821 #endif
3822 #ifndef DEBUG_RANGES_SECTION_LABEL
3823 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3824 #endif
3825 #ifndef DEBUG_MACINFO_SECTION_LABEL
3826 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3827 #endif
3828 #ifndef DEBUG_MACRO_SECTION_LABEL
3829 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3830 #endif
3831 #define SKELETON_COMP_DIE_ABBREV 1
3832 #define SKELETON_TYPE_DIE_ABBREV 2
3834 /* Definitions of defaults for formats and names of various special
3835 (artificial) labels which may be generated within this file (when the -g
3836 options is used and DWARF2_DEBUGGING_INFO is in effect.
3837 If necessary, these may be overridden from within the tm.h file, but
3838 typically, overriding these defaults is unnecessary. */
3840 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3841 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3842 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3843 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3844 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3845 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3846 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3847 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3848 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3849 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3850 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3851 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3852 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3853 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3854 static char ranges_base_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3856 #ifndef TEXT_END_LABEL
3857 #define TEXT_END_LABEL "Letext"
3858 #endif
3859 #ifndef COLD_END_LABEL
3860 #define COLD_END_LABEL "Letext_cold"
3861 #endif
3862 #ifndef BLOCK_BEGIN_LABEL
3863 #define BLOCK_BEGIN_LABEL "LBB"
3864 #endif
3865 #ifndef BLOCK_END_LABEL
3866 #define BLOCK_END_LABEL "LBE"
3867 #endif
3868 #ifndef LINE_CODE_LABEL
3869 #define LINE_CODE_LABEL "LM"
3870 #endif
3873 /* Return the root of the DIE's built for the current compilation unit. */
3874 static dw_die_ref
3875 comp_unit_die (void)
3877 if (!single_comp_unit_die)
3878 single_comp_unit_die = gen_compile_unit_die (NULL);
3879 return single_comp_unit_die;
3882 /* We allow a language front-end to designate a function that is to be
3883 called to "demangle" any name before it is put into a DIE. */
3885 static const char *(*demangle_name_func) (const char *);
3887 void
3888 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3890 demangle_name_func = func;
3893 /* Test if rtl node points to a pseudo register. */
3895 static inline int
3896 is_pseudo_reg (const_rtx rtl)
3898 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3899 || (GET_CODE (rtl) == SUBREG
3900 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3903 /* Return a reference to a type, with its const and volatile qualifiers
3904 removed. */
3906 static inline tree
3907 type_main_variant (tree type)
3909 type = TYPE_MAIN_VARIANT (type);
3911 /* ??? There really should be only one main variant among any group of
3912 variants of a given type (and all of the MAIN_VARIANT values for all
3913 members of the group should point to that one type) but sometimes the C
3914 front-end messes this up for array types, so we work around that bug
3915 here. */
3916 if (TREE_CODE (type) == ARRAY_TYPE)
3917 while (type != TYPE_MAIN_VARIANT (type))
3918 type = TYPE_MAIN_VARIANT (type);
3920 return type;
3923 /* Return nonzero if the given type node represents a tagged type. */
3925 static inline int
3926 is_tagged_type (const_tree type)
3928 enum tree_code code = TREE_CODE (type);
3930 return (code == RECORD_TYPE || code == UNION_TYPE
3931 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3934 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3936 static void
3937 get_ref_die_offset_label (char *label, dw_die_ref ref)
3939 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3942 /* Return die_offset of a DIE reference to a base type. */
3944 static unsigned long int
3945 get_base_type_offset (dw_die_ref ref)
3947 if (ref->die_offset)
3948 return ref->die_offset;
3949 if (comp_unit_die ()->die_abbrev)
3951 calc_base_type_die_sizes ();
3952 gcc_assert (ref->die_offset);
3954 return ref->die_offset;
3957 /* Return die_offset of a DIE reference other than base type. */
3959 static unsigned long int
3960 get_ref_die_offset (dw_die_ref ref)
3962 gcc_assert (ref->die_offset);
3963 return ref->die_offset;
3966 /* Convert a DIE tag into its string name. */
3968 static const char *
3969 dwarf_tag_name (unsigned int tag)
3971 const char *name = get_DW_TAG_name (tag);
3973 if (name != NULL)
3974 return name;
3976 return "DW_TAG_<unknown>";
3979 /* Convert a DWARF attribute code into its string name. */
3981 static const char *
3982 dwarf_attr_name (unsigned int attr)
3984 const char *name;
3986 switch (attr)
3988 #if VMS_DEBUGGING_INFO
3989 case DW_AT_HP_prologue:
3990 return "DW_AT_HP_prologue";
3991 #else
3992 case DW_AT_MIPS_loop_unroll_factor:
3993 return "DW_AT_MIPS_loop_unroll_factor";
3994 #endif
3996 #if VMS_DEBUGGING_INFO
3997 case DW_AT_HP_epilogue:
3998 return "DW_AT_HP_epilogue";
3999 #else
4000 case DW_AT_MIPS_stride:
4001 return "DW_AT_MIPS_stride";
4002 #endif
4005 name = get_DW_AT_name (attr);
4007 if (name != NULL)
4008 return name;
4010 return "DW_AT_<unknown>";
4013 /* Convert a DWARF value form code into its string name. */
4015 static const char *
4016 dwarf_form_name (unsigned int form)
4018 const char *name = get_DW_FORM_name (form);
4020 if (name != NULL)
4021 return name;
4023 return "DW_FORM_<unknown>";
4026 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4027 instance of an inlined instance of a decl which is local to an inline
4028 function, so we have to trace all of the way back through the origin chain
4029 to find out what sort of node actually served as the original seed for the
4030 given block. */
4032 static tree
4033 decl_ultimate_origin (const_tree decl)
4035 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4036 return NULL_TREE;
4038 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
4039 we're trying to output the abstract instance of this function. */
4040 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4041 return NULL_TREE;
4043 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4044 most distant ancestor, this should never happen. */
4045 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4047 return DECL_ABSTRACT_ORIGIN (decl);
4050 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4051 of a virtual function may refer to a base class, so we check the 'this'
4052 parameter. */
4054 static tree
4055 decl_class_context (tree decl)
4057 tree context = NULL_TREE;
4059 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4060 context = DECL_CONTEXT (decl);
4061 else
4062 context = TYPE_MAIN_VARIANT
4063 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4065 if (context && !TYPE_P (context))
4066 context = NULL_TREE;
4068 return context;
4071 /* Add an attribute/value pair to a DIE. */
4073 static inline void
4074 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
4076 /* Maybe this should be an assert? */
4077 if (die == NULL)
4078 return;
4080 vec_safe_reserve (die->die_attr, 1);
4081 vec_safe_push (die->die_attr, *attr);
4084 static inline enum dw_val_class
4085 AT_class (dw_attr_node *a)
4087 return a->dw_attr_val.val_class;
4090 /* Return the index for any attribute that will be referenced with a
4091 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
4092 are stored in dw_attr_val.v.val_str for reference counting
4093 pruning. */
4095 static inline unsigned int
4096 AT_index (dw_attr_node *a)
4098 if (AT_class (a) == dw_val_class_str)
4099 return a->dw_attr_val.v.val_str->index;
4100 else if (a->dw_attr_val.val_entry != NULL)
4101 return a->dw_attr_val.val_entry->index;
4102 return NOT_INDEXED;
4105 /* Add a flag value attribute to a DIE. */
4107 static inline void
4108 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4110 dw_attr_node attr;
4112 attr.dw_attr = attr_kind;
4113 attr.dw_attr_val.val_class = dw_val_class_flag;
4114 attr.dw_attr_val.val_entry = NULL;
4115 attr.dw_attr_val.v.val_flag = flag;
4116 add_dwarf_attr (die, &attr);
4119 static inline unsigned
4120 AT_flag (dw_attr_node *a)
4122 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4123 return a->dw_attr_val.v.val_flag;
4126 /* Add a signed integer attribute value to a DIE. */
4128 static inline void
4129 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4131 dw_attr_node attr;
4133 attr.dw_attr = attr_kind;
4134 attr.dw_attr_val.val_class = dw_val_class_const;
4135 attr.dw_attr_val.val_entry = NULL;
4136 attr.dw_attr_val.v.val_int = int_val;
4137 add_dwarf_attr (die, &attr);
4140 static inline HOST_WIDE_INT
4141 AT_int (dw_attr_node *a)
4143 gcc_assert (a && (AT_class (a) == dw_val_class_const
4144 || AT_class (a) == dw_val_class_const_implicit));
4145 return a->dw_attr_val.v.val_int;
4148 /* Add an unsigned integer attribute value to a DIE. */
4150 static inline void
4151 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4152 unsigned HOST_WIDE_INT unsigned_val)
4154 dw_attr_node attr;
4156 attr.dw_attr = attr_kind;
4157 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4158 attr.dw_attr_val.val_entry = NULL;
4159 attr.dw_attr_val.v.val_unsigned = unsigned_val;
4160 add_dwarf_attr (die, &attr);
4163 static inline unsigned HOST_WIDE_INT
4164 AT_unsigned (dw_attr_node *a)
4166 gcc_assert (a && (AT_class (a) == dw_val_class_unsigned_const
4167 || AT_class (a) == dw_val_class_unsigned_const_implicit));
4168 return a->dw_attr_val.v.val_unsigned;
4171 /* Add an unsigned wide integer attribute value to a DIE. */
4173 static inline void
4174 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
4175 const wide_int& w)
4177 dw_attr_node attr;
4179 attr.dw_attr = attr_kind;
4180 attr.dw_attr_val.val_class = dw_val_class_wide_int;
4181 attr.dw_attr_val.val_entry = NULL;
4182 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
4183 *attr.dw_attr_val.v.val_wide = w;
4184 add_dwarf_attr (die, &attr);
4187 /* Add an unsigned double integer attribute value to a DIE. */
4189 static inline void
4190 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4191 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4193 dw_attr_node attr;
4195 attr.dw_attr = attr_kind;
4196 attr.dw_attr_val.val_class = dw_val_class_const_double;
4197 attr.dw_attr_val.val_entry = NULL;
4198 attr.dw_attr_val.v.val_double.high = high;
4199 attr.dw_attr_val.v.val_double.low = low;
4200 add_dwarf_attr (die, &attr);
4203 /* Add a floating point attribute value to a DIE and return it. */
4205 static inline void
4206 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4207 unsigned int length, unsigned int elt_size, unsigned char *array)
4209 dw_attr_node attr;
4211 attr.dw_attr = attr_kind;
4212 attr.dw_attr_val.val_class = dw_val_class_vec;
4213 attr.dw_attr_val.val_entry = NULL;
4214 attr.dw_attr_val.v.val_vec.length = length;
4215 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4216 attr.dw_attr_val.v.val_vec.array = array;
4217 add_dwarf_attr (die, &attr);
4220 /* Add an 8-byte data attribute value to a DIE. */
4222 static inline void
4223 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4224 unsigned char data8[8])
4226 dw_attr_node attr;
4228 attr.dw_attr = attr_kind;
4229 attr.dw_attr_val.val_class = dw_val_class_data8;
4230 attr.dw_attr_val.val_entry = NULL;
4231 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4232 add_dwarf_attr (die, &attr);
4235 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
4236 dwarf_split_debug_info, address attributes in dies destined for the
4237 final executable have force_direct set to avoid using indexed
4238 references. */
4240 static inline void
4241 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4242 bool force_direct)
4244 dw_attr_node attr;
4245 char * lbl_id;
4247 lbl_id = xstrdup (lbl_low);
4248 attr.dw_attr = DW_AT_low_pc;
4249 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4250 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4251 if (dwarf_split_debug_info && !force_direct)
4252 attr.dw_attr_val.val_entry
4253 = add_addr_table_entry (lbl_id, ate_kind_label);
4254 else
4255 attr.dw_attr_val.val_entry = NULL;
4256 add_dwarf_attr (die, &attr);
4258 attr.dw_attr = DW_AT_high_pc;
4259 if (dwarf_version < 4)
4260 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4261 else
4262 attr.dw_attr_val.val_class = dw_val_class_high_pc;
4263 lbl_id = xstrdup (lbl_high);
4264 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4265 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4266 && dwarf_split_debug_info && !force_direct)
4267 attr.dw_attr_val.val_entry
4268 = add_addr_table_entry (lbl_id, ate_kind_label);
4269 else
4270 attr.dw_attr_val.val_entry = NULL;
4271 add_dwarf_attr (die, &attr);
4274 /* Hash and equality functions for debug_str_hash. */
4276 hashval_t
4277 indirect_string_hasher::hash (indirect_string_node *x)
4279 return htab_hash_string (x->str);
4282 bool
4283 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4285 return strcmp (x1->str, x2) == 0;
4288 /* Add STR to the given string hash table. */
4290 static struct indirect_string_node *
4291 find_AT_string_in_table (const char *str,
4292 hash_table<indirect_string_hasher> *table)
4294 struct indirect_string_node *node;
4296 indirect_string_node **slot
4297 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4298 if (*slot == NULL)
4300 node = ggc_cleared_alloc<indirect_string_node> ();
4301 node->str = ggc_strdup (str);
4302 *slot = node;
4304 else
4305 node = *slot;
4307 node->refcount++;
4308 return node;
4311 /* Add STR to the indirect string hash table. */
4313 static struct indirect_string_node *
4314 find_AT_string (const char *str)
4316 if (! debug_str_hash)
4317 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4319 return find_AT_string_in_table (str, debug_str_hash);
4322 /* Add a string attribute value to a DIE. */
4324 static inline void
4325 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4327 dw_attr_node attr;
4328 struct indirect_string_node *node;
4330 node = find_AT_string (str);
4332 attr.dw_attr = attr_kind;
4333 attr.dw_attr_val.val_class = dw_val_class_str;
4334 attr.dw_attr_val.val_entry = NULL;
4335 attr.dw_attr_val.v.val_str = node;
4336 add_dwarf_attr (die, &attr);
4339 static inline const char *
4340 AT_string (dw_attr_node *a)
4342 gcc_assert (a && AT_class (a) == dw_val_class_str);
4343 return a->dw_attr_val.v.val_str->str;
4346 /* Call this function directly to bypass AT_string_form's logic to put
4347 the string inline in the die. */
4349 static void
4350 set_indirect_string (struct indirect_string_node *node)
4352 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4353 /* Already indirect is a no op. */
4354 if (node->form == DW_FORM_strp
4355 || node->form == DW_FORM_line_strp
4356 || node->form == DW_FORM_GNU_str_index)
4358 gcc_assert (node->label);
4359 return;
4361 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4362 ++dw2_string_counter;
4363 node->label = xstrdup (label);
4365 if (!dwarf_split_debug_info)
4367 node->form = DW_FORM_strp;
4368 node->index = NOT_INDEXED;
4370 else
4372 node->form = DW_FORM_GNU_str_index;
4373 node->index = NO_INDEX_ASSIGNED;
4377 /* Find out whether a string should be output inline in DIE
4378 or out-of-line in .debug_str section. */
4380 static enum dwarf_form
4381 find_string_form (struct indirect_string_node *node)
4383 unsigned int len;
4385 if (node->form)
4386 return node->form;
4388 len = strlen (node->str) + 1;
4390 /* If the string is shorter or equal to the size of the reference, it is
4391 always better to put it inline. */
4392 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4393 return node->form = DW_FORM_string;
4395 /* If we cannot expect the linker to merge strings in .debug_str
4396 section, only put it into .debug_str if it is worth even in this
4397 single module. */
4398 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4399 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4400 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4401 return node->form = DW_FORM_string;
4403 set_indirect_string (node);
4405 return node->form;
4408 /* Find out whether the string referenced from the attribute should be
4409 output inline in DIE or out-of-line in .debug_str section. */
4411 static enum dwarf_form
4412 AT_string_form (dw_attr_node *a)
4414 gcc_assert (a && AT_class (a) == dw_val_class_str);
4415 return find_string_form (a->dw_attr_val.v.val_str);
4418 /* Add a DIE reference attribute value to a DIE. */
4420 static inline void
4421 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4423 dw_attr_node attr;
4424 gcc_checking_assert (targ_die != NULL);
4426 /* With LTO we can end up trying to reference something we didn't create
4427 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4428 if (targ_die == NULL)
4429 return;
4431 attr.dw_attr = attr_kind;
4432 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4433 attr.dw_attr_val.val_entry = NULL;
4434 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4435 attr.dw_attr_val.v.val_die_ref.external = 0;
4436 add_dwarf_attr (die, &attr);
4439 /* Change DIE reference REF to point to NEW_DIE instead. */
4441 static inline void
4442 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4444 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4445 ref->dw_attr_val.v.val_die_ref.die = new_die;
4446 ref->dw_attr_val.v.val_die_ref.external = 0;
4449 /* Add an AT_specification attribute to a DIE, and also make the back
4450 pointer from the specification to the definition. */
4452 static inline void
4453 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4455 add_AT_die_ref (die, DW_AT_specification, targ_die);
4456 gcc_assert (!targ_die->die_definition);
4457 targ_die->die_definition = die;
4460 static inline dw_die_ref
4461 AT_ref (dw_attr_node *a)
4463 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4464 return a->dw_attr_val.v.val_die_ref.die;
4467 static inline int
4468 AT_ref_external (dw_attr_node *a)
4470 if (a && AT_class (a) == dw_val_class_die_ref)
4471 return a->dw_attr_val.v.val_die_ref.external;
4473 return 0;
4476 static inline void
4477 set_AT_ref_external (dw_attr_node *a, int i)
4479 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4480 a->dw_attr_val.v.val_die_ref.external = i;
4483 /* Add an FDE reference attribute value to a DIE. */
4485 static inline void
4486 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4488 dw_attr_node attr;
4490 attr.dw_attr = attr_kind;
4491 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4492 attr.dw_attr_val.val_entry = NULL;
4493 attr.dw_attr_val.v.val_fde_index = targ_fde;
4494 add_dwarf_attr (die, &attr);
4497 /* Add a location description attribute value to a DIE. */
4499 static inline void
4500 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4502 dw_attr_node attr;
4504 attr.dw_attr = attr_kind;
4505 attr.dw_attr_val.val_class = dw_val_class_loc;
4506 attr.dw_attr_val.val_entry = NULL;
4507 attr.dw_attr_val.v.val_loc = loc;
4508 add_dwarf_attr (die, &attr);
4511 static inline dw_loc_descr_ref
4512 AT_loc (dw_attr_node *a)
4514 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4515 return a->dw_attr_val.v.val_loc;
4518 static inline void
4519 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4521 dw_attr_node attr;
4523 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4524 return;
4526 attr.dw_attr = attr_kind;
4527 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4528 attr.dw_attr_val.val_entry = NULL;
4529 attr.dw_attr_val.v.val_loc_list = loc_list;
4530 add_dwarf_attr (die, &attr);
4531 have_location_lists = true;
4534 static inline dw_loc_list_ref
4535 AT_loc_list (dw_attr_node *a)
4537 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4538 return a->dw_attr_val.v.val_loc_list;
4541 static inline dw_loc_list_ref *
4542 AT_loc_list_ptr (dw_attr_node *a)
4544 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4545 return &a->dw_attr_val.v.val_loc_list;
4548 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
4550 static hashval_t hash (addr_table_entry *);
4551 static bool equal (addr_table_entry *, addr_table_entry *);
4554 /* Table of entries into the .debug_addr section. */
4556 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4558 /* Hash an address_table_entry. */
4560 hashval_t
4561 addr_hasher::hash (addr_table_entry *a)
4563 inchash::hash hstate;
4564 switch (a->kind)
4566 case ate_kind_rtx:
4567 hstate.add_int (0);
4568 break;
4569 case ate_kind_rtx_dtprel:
4570 hstate.add_int (1);
4571 break;
4572 case ate_kind_label:
4573 return htab_hash_string (a->addr.label);
4574 default:
4575 gcc_unreachable ();
4577 inchash::add_rtx (a->addr.rtl, hstate);
4578 return hstate.end ();
4581 /* Determine equality for two address_table_entries. */
4583 bool
4584 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4586 if (a1->kind != a2->kind)
4587 return 0;
4588 switch (a1->kind)
4590 case ate_kind_rtx:
4591 case ate_kind_rtx_dtprel:
4592 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4593 case ate_kind_label:
4594 return strcmp (a1->addr.label, a2->addr.label) == 0;
4595 default:
4596 gcc_unreachable ();
4600 /* Initialize an addr_table_entry. */
4602 void
4603 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4605 e->kind = kind;
4606 switch (kind)
4608 case ate_kind_rtx:
4609 case ate_kind_rtx_dtprel:
4610 e->addr.rtl = (rtx) addr;
4611 break;
4612 case ate_kind_label:
4613 e->addr.label = (char *) addr;
4614 break;
4616 e->refcount = 0;
4617 e->index = NO_INDEX_ASSIGNED;
4620 /* Add attr to the address table entry to the table. Defer setting an
4621 index until output time. */
4623 static addr_table_entry *
4624 add_addr_table_entry (void *addr, enum ate_kind kind)
4626 addr_table_entry *node;
4627 addr_table_entry finder;
4629 gcc_assert (dwarf_split_debug_info);
4630 if (! addr_index_table)
4631 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4632 init_addr_table_entry (&finder, kind, addr);
4633 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4635 if (*slot == HTAB_EMPTY_ENTRY)
4637 node = ggc_cleared_alloc<addr_table_entry> ();
4638 init_addr_table_entry (node, kind, addr);
4639 *slot = node;
4641 else
4642 node = *slot;
4644 node->refcount++;
4645 return node;
4648 /* Remove an entry from the addr table by decrementing its refcount.
4649 Strictly, decrementing the refcount would be enough, but the
4650 assertion that the entry is actually in the table has found
4651 bugs. */
4653 static void
4654 remove_addr_table_entry (addr_table_entry *entry)
4656 gcc_assert (dwarf_split_debug_info && addr_index_table);
4657 /* After an index is assigned, the table is frozen. */
4658 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4659 entry->refcount--;
4662 /* Given a location list, remove all addresses it refers to from the
4663 address_table. */
4665 static void
4666 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4668 for (; descr; descr = descr->dw_loc_next)
4669 if (descr->dw_loc_oprnd1.val_entry != NULL)
4671 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4672 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4676 /* A helper function for dwarf2out_finish called through
4677 htab_traverse. Assign an addr_table_entry its index. All entries
4678 must be collected into the table when this function is called,
4679 because the indexing code relies on htab_traverse to traverse nodes
4680 in the same order for each run. */
4683 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4685 addr_table_entry *node = *h;
4687 /* Don't index unreferenced nodes. */
4688 if (node->refcount == 0)
4689 return 1;
4691 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4692 node->index = *index;
4693 *index += 1;
4695 return 1;
4698 /* Add an address constant attribute value to a DIE. When using
4699 dwarf_split_debug_info, address attributes in dies destined for the
4700 final executable should be direct references--setting the parameter
4701 force_direct ensures this behavior. */
4703 static inline void
4704 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4705 bool force_direct)
4707 dw_attr_node attr;
4709 attr.dw_attr = attr_kind;
4710 attr.dw_attr_val.val_class = dw_val_class_addr;
4711 attr.dw_attr_val.v.val_addr = addr;
4712 if (dwarf_split_debug_info && !force_direct)
4713 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4714 else
4715 attr.dw_attr_val.val_entry = NULL;
4716 add_dwarf_attr (die, &attr);
4719 /* Get the RTX from to an address DIE attribute. */
4721 static inline rtx
4722 AT_addr (dw_attr_node *a)
4724 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4725 return a->dw_attr_val.v.val_addr;
4728 /* Add a file attribute value to a DIE. */
4730 static inline void
4731 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4732 struct dwarf_file_data *fd)
4734 dw_attr_node attr;
4736 attr.dw_attr = attr_kind;
4737 attr.dw_attr_val.val_class = dw_val_class_file;
4738 attr.dw_attr_val.val_entry = NULL;
4739 attr.dw_attr_val.v.val_file = fd;
4740 add_dwarf_attr (die, &attr);
4743 /* Get the dwarf_file_data from a file DIE attribute. */
4745 static inline struct dwarf_file_data *
4746 AT_file (dw_attr_node *a)
4748 gcc_assert (a && (AT_class (a) == dw_val_class_file
4749 || AT_class (a) == dw_val_class_file_implicit));
4750 return a->dw_attr_val.v.val_file;
4753 /* Add a vms delta attribute value to a DIE. */
4755 static inline void
4756 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4757 const char *lbl1, const char *lbl2)
4759 dw_attr_node attr;
4761 attr.dw_attr = attr_kind;
4762 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4763 attr.dw_attr_val.val_entry = NULL;
4764 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4765 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4766 add_dwarf_attr (die, &attr);
4769 /* Add a label identifier attribute value to a DIE. */
4771 static inline void
4772 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4773 const char *lbl_id)
4775 dw_attr_node attr;
4777 attr.dw_attr = attr_kind;
4778 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4779 attr.dw_attr_val.val_entry = NULL;
4780 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4781 if (dwarf_split_debug_info)
4782 attr.dw_attr_val.val_entry
4783 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4784 ate_kind_label);
4785 add_dwarf_attr (die, &attr);
4788 /* Add a section offset attribute value to a DIE, an offset into the
4789 debug_line section. */
4791 static inline void
4792 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4793 const char *label)
4795 dw_attr_node attr;
4797 attr.dw_attr = attr_kind;
4798 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4799 attr.dw_attr_val.val_entry = NULL;
4800 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4801 add_dwarf_attr (die, &attr);
4804 /* Add a section offset attribute value to a DIE, an offset into the
4805 debug_loclists section. */
4807 static inline void
4808 add_AT_loclistsptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4809 const char *label)
4811 dw_attr_node attr;
4813 attr.dw_attr = attr_kind;
4814 attr.dw_attr_val.val_class = dw_val_class_loclistsptr;
4815 attr.dw_attr_val.val_entry = NULL;
4816 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4817 add_dwarf_attr (die, &attr);
4820 /* Add a section offset attribute value to a DIE, an offset into the
4821 debug_macinfo section. */
4823 static inline void
4824 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4825 const char *label)
4827 dw_attr_node attr;
4829 attr.dw_attr = attr_kind;
4830 attr.dw_attr_val.val_class = dw_val_class_macptr;
4831 attr.dw_attr_val.val_entry = NULL;
4832 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4833 add_dwarf_attr (die, &attr);
4836 /* Add an offset attribute value to a DIE. */
4838 static inline void
4839 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4840 unsigned HOST_WIDE_INT offset)
4842 dw_attr_node attr;
4844 attr.dw_attr = attr_kind;
4845 attr.dw_attr_val.val_class = dw_val_class_offset;
4846 attr.dw_attr_val.val_entry = NULL;
4847 attr.dw_attr_val.v.val_offset = offset;
4848 add_dwarf_attr (die, &attr);
4851 /* Add a range_list attribute value to a DIE. When using
4852 dwarf_split_debug_info, address attributes in dies destined for the
4853 final executable should be direct references--setting the parameter
4854 force_direct ensures this behavior. */
4856 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4857 #define RELOCATED_OFFSET (NULL)
4859 static void
4860 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4861 long unsigned int offset, bool force_direct)
4863 dw_attr_node attr;
4865 attr.dw_attr = attr_kind;
4866 attr.dw_attr_val.val_class = dw_val_class_range_list;
4867 /* For the range_list attribute, use val_entry to store whether the
4868 offset should follow split-debug-info or normal semantics. This
4869 value is read in output_range_list_offset. */
4870 if (dwarf_split_debug_info && !force_direct)
4871 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4872 else
4873 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4874 attr.dw_attr_val.v.val_offset = offset;
4875 add_dwarf_attr (die, &attr);
4878 /* Return the start label of a delta attribute. */
4880 static inline const char *
4881 AT_vms_delta1 (dw_attr_node *a)
4883 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4884 return a->dw_attr_val.v.val_vms_delta.lbl1;
4887 /* Return the end label of a delta attribute. */
4889 static inline const char *
4890 AT_vms_delta2 (dw_attr_node *a)
4892 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4893 return a->dw_attr_val.v.val_vms_delta.lbl2;
4896 static inline const char *
4897 AT_lbl (dw_attr_node *a)
4899 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4900 || AT_class (a) == dw_val_class_lineptr
4901 || AT_class (a) == dw_val_class_macptr
4902 || AT_class (a) == dw_val_class_loclistsptr
4903 || AT_class (a) == dw_val_class_high_pc));
4904 return a->dw_attr_val.v.val_lbl_id;
4907 /* Get the attribute of type attr_kind. */
4909 static dw_attr_node *
4910 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4912 dw_attr_node *a;
4913 unsigned ix;
4914 dw_die_ref spec = NULL;
4916 if (! die)
4917 return NULL;
4919 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4920 if (a->dw_attr == attr_kind)
4921 return a;
4922 else if (a->dw_attr == DW_AT_specification
4923 || a->dw_attr == DW_AT_abstract_origin)
4924 spec = AT_ref (a);
4926 if (spec)
4927 return get_AT (spec, attr_kind);
4929 return NULL;
4932 /* Returns the parent of the declaration of DIE. */
4934 static dw_die_ref
4935 get_die_parent (dw_die_ref die)
4937 dw_die_ref t;
4939 if (!die)
4940 return NULL;
4942 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4943 || (t = get_AT_ref (die, DW_AT_specification)))
4944 die = t;
4946 return die->die_parent;
4949 /* Return the "low pc" attribute value, typically associated with a subprogram
4950 DIE. Return null if the "low pc" attribute is either not present, or if it
4951 cannot be represented as an assembler label identifier. */
4953 static inline const char *
4954 get_AT_low_pc (dw_die_ref die)
4956 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
4958 return a ? AT_lbl (a) : NULL;
4961 /* Return the "high pc" attribute value, typically associated with a subprogram
4962 DIE. Return null if the "high pc" attribute is either not present, or if it
4963 cannot be represented as an assembler label identifier. */
4965 static inline const char *
4966 get_AT_hi_pc (dw_die_ref die)
4968 dw_attr_node *a = get_AT (die, DW_AT_high_pc);
4970 return a ? AT_lbl (a) : NULL;
4973 /* Return the value of the string attribute designated by ATTR_KIND, or
4974 NULL if it is not present. */
4976 static inline const char *
4977 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4979 dw_attr_node *a = get_AT (die, attr_kind);
4981 return a ? AT_string (a) : NULL;
4984 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4985 if it is not present. */
4987 static inline int
4988 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4990 dw_attr_node *a = get_AT (die, attr_kind);
4992 return a ? AT_flag (a) : 0;
4995 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4996 if it is not present. */
4998 static inline unsigned
4999 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5001 dw_attr_node *a = get_AT (die, attr_kind);
5003 return a ? AT_unsigned (a) : 0;
5006 static inline dw_die_ref
5007 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5009 dw_attr_node *a = get_AT (die, attr_kind);
5011 return a ? AT_ref (a) : NULL;
5014 static inline struct dwarf_file_data *
5015 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5017 dw_attr_node *a = get_AT (die, attr_kind);
5019 return a ? AT_file (a) : NULL;
5022 /* Return TRUE if the language is C++. */
5024 static inline bool
5025 is_cxx (void)
5027 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5029 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
5030 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
5033 /* Return TRUE if DECL was created by the C++ frontend. */
5035 static bool
5036 is_cxx (const_tree decl)
5038 if (in_lto_p)
5040 const_tree context = decl;
5041 while (context && TREE_CODE (context) != TRANSLATION_UNIT_DECL)
5043 if (TREE_CODE (context) == BLOCK)
5044 context = BLOCK_SUPERCONTEXT (context);
5045 else
5046 context = get_containing_scope (context);
5048 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5049 return strncmp (TRANSLATION_UNIT_LANGUAGE (context), "GNU C++", 7) == 0;
5051 return is_cxx ();
5054 /* Return TRUE if the language is Fortran. */
5056 static inline bool
5057 is_fortran (void)
5059 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5061 return (lang == DW_LANG_Fortran77
5062 || lang == DW_LANG_Fortran90
5063 || lang == DW_LANG_Fortran95
5064 || lang == DW_LANG_Fortran03
5065 || lang == DW_LANG_Fortran08);
5068 /* Return TRUE if the language is Ada. */
5070 static inline bool
5071 is_ada (void)
5073 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5075 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5078 /* Remove the specified attribute if present. Return TRUE if removal
5079 was successful. */
5081 static bool
5082 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5084 dw_attr_node *a;
5085 unsigned ix;
5087 if (! die)
5088 return false;
5090 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5091 if (a->dw_attr == attr_kind)
5093 if (AT_class (a) == dw_val_class_str)
5094 if (a->dw_attr_val.v.val_str->refcount)
5095 a->dw_attr_val.v.val_str->refcount--;
5097 /* vec::ordered_remove should help reduce the number of abbrevs
5098 that are needed. */
5099 die->die_attr->ordered_remove (ix);
5100 return true;
5102 return false;
5105 /* Remove CHILD from its parent. PREV must have the property that
5106 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
5108 static void
5109 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5111 gcc_assert (child->die_parent == prev->die_parent);
5112 gcc_assert (prev->die_sib == child);
5113 if (prev == child)
5115 gcc_assert (child->die_parent->die_child == child);
5116 prev = NULL;
5118 else
5119 prev->die_sib = child->die_sib;
5120 if (child->die_parent->die_child == child)
5121 child->die_parent->die_child = prev;
5122 child->die_sib = NULL;
5125 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
5126 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
5128 static void
5129 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
5131 dw_die_ref parent = old_child->die_parent;
5133 gcc_assert (parent == prev->die_parent);
5134 gcc_assert (prev->die_sib == old_child);
5136 new_child->die_parent = parent;
5137 if (prev == old_child)
5139 gcc_assert (parent->die_child == old_child);
5140 new_child->die_sib = new_child;
5142 else
5144 prev->die_sib = new_child;
5145 new_child->die_sib = old_child->die_sib;
5147 if (old_child->die_parent->die_child == old_child)
5148 old_child->die_parent->die_child = new_child;
5149 old_child->die_sib = NULL;
5152 /* Move all children from OLD_PARENT to NEW_PARENT. */
5154 static void
5155 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
5157 dw_die_ref c;
5158 new_parent->die_child = old_parent->die_child;
5159 old_parent->die_child = NULL;
5160 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
5163 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
5164 matches TAG. */
5166 static void
5167 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5169 dw_die_ref c;
5171 c = die->die_child;
5172 if (c) do {
5173 dw_die_ref prev = c;
5174 c = c->die_sib;
5175 while (c->die_tag == tag)
5177 remove_child_with_prev (c, prev);
5178 c->die_parent = NULL;
5179 /* Might have removed every child. */
5180 if (die->die_child == NULL)
5181 return;
5182 c = prev->die_sib;
5184 } while (c != die->die_child);
5187 /* Add a CHILD_DIE as the last child of DIE. */
5189 static void
5190 add_child_die (dw_die_ref die, dw_die_ref child_die)
5192 /* FIXME this should probably be an assert. */
5193 if (! die || ! child_die)
5194 return;
5195 gcc_assert (die != child_die);
5197 child_die->die_parent = die;
5198 if (die->die_child)
5200 child_die->die_sib = die->die_child->die_sib;
5201 die->die_child->die_sib = child_die;
5203 else
5204 child_die->die_sib = child_die;
5205 die->die_child = child_die;
5208 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE. */
5210 static void
5211 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
5212 dw_die_ref after_die)
5214 gcc_assert (die
5215 && child_die
5216 && after_die
5217 && die->die_child
5218 && die != child_die);
5220 child_die->die_parent = die;
5221 child_die->die_sib = after_die->die_sib;
5222 after_die->die_sib = child_die;
5223 if (die->die_child == after_die)
5224 die->die_child = child_die;
5227 /* Unassociate CHILD from its parent, and make its parent be
5228 NEW_PARENT. */
5230 static void
5231 reparent_child (dw_die_ref child, dw_die_ref new_parent)
5233 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
5234 if (p->die_sib == child)
5236 remove_child_with_prev (child, p);
5237 break;
5239 add_child_die (new_parent, child);
5242 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5243 is the specification, to the end of PARENT's list of children.
5244 This is done by removing and re-adding it. */
5246 static void
5247 splice_child_die (dw_die_ref parent, dw_die_ref child)
5249 /* We want the declaration DIE from inside the class, not the
5250 specification DIE at toplevel. */
5251 if (child->die_parent != parent)
5253 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5255 if (tmp)
5256 child = tmp;
5259 gcc_assert (child->die_parent == parent
5260 || (child->die_parent
5261 == get_AT_ref (parent, DW_AT_specification)));
5263 reparent_child (child, parent);
5266 /* Create and return a new die with a parent of PARENT_DIE. If
5267 PARENT_DIE is NULL, the new DIE is placed in limbo and an
5268 associated tree T must be supplied to determine parenthood
5269 later. */
5271 static inline dw_die_ref
5272 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5274 dw_die_ref die = ggc_cleared_alloc<die_node> ();
5276 die->die_tag = tag_value;
5278 if (parent_die != NULL)
5279 add_child_die (parent_die, die);
5280 else
5282 limbo_die_node *limbo_node;
5284 /* No DIEs created after early dwarf should end up in limbo,
5285 because the limbo list should not persist past LTO
5286 streaming. */
5287 if (tag_value != DW_TAG_compile_unit
5288 /* These are allowed because they're generated while
5289 breaking out COMDAT units late. */
5290 && tag_value != DW_TAG_type_unit
5291 && tag_value != DW_TAG_skeleton_unit
5292 && !early_dwarf
5293 /* Allow nested functions to live in limbo because they will
5294 only temporarily live there, as decls_for_scope will fix
5295 them up. */
5296 && (TREE_CODE (t) != FUNCTION_DECL
5297 || !decl_function_context (t))
5298 /* Same as nested functions above but for types. Types that
5299 are local to a function will be fixed in
5300 decls_for_scope. */
5301 && (!RECORD_OR_UNION_TYPE_P (t)
5302 || !TYPE_CONTEXT (t)
5303 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5304 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5305 especially in the ltrans stage, but once we implement LTO
5306 dwarf streaming, we should remove this exception. */
5307 && !in_lto_p)
5309 fprintf (stderr, "symbol ended up in limbo too late:");
5310 debug_generic_stmt (t);
5311 gcc_unreachable ();
5314 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5315 limbo_node->die = die;
5316 limbo_node->created_for = t;
5317 limbo_node->next = limbo_die_list;
5318 limbo_die_list = limbo_node;
5321 return die;
5324 /* Return the DIE associated with the given type specifier. */
5326 static inline dw_die_ref
5327 lookup_type_die (tree type)
5329 dw_die_ref die = TYPE_SYMTAB_DIE (type);
5330 if (die && die->removed)
5332 TYPE_SYMTAB_DIE (type) = NULL;
5333 return NULL;
5335 return die;
5338 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5339 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5340 anonymous type instead the one of the naming typedef. */
5342 static inline dw_die_ref
5343 strip_naming_typedef (tree type, dw_die_ref type_die)
5345 if (type
5346 && TREE_CODE (type) == RECORD_TYPE
5347 && type_die
5348 && type_die->die_tag == DW_TAG_typedef
5349 && is_naming_typedef_decl (TYPE_NAME (type)))
5350 type_die = get_AT_ref (type_die, DW_AT_type);
5351 return type_die;
5354 /* Like lookup_type_die, but if type is an anonymous type named by a
5355 typedef[1], return the DIE of the anonymous type instead the one of
5356 the naming typedef. This is because in gen_typedef_die, we did
5357 equate the anonymous struct named by the typedef with the DIE of
5358 the naming typedef. So by default, lookup_type_die on an anonymous
5359 struct yields the DIE of the naming typedef.
5361 [1]: Read the comment of is_naming_typedef_decl to learn about what
5362 a naming typedef is. */
5364 static inline dw_die_ref
5365 lookup_type_die_strip_naming_typedef (tree type)
5367 dw_die_ref die = lookup_type_die (type);
5368 return strip_naming_typedef (type, die);
5371 /* Equate a DIE to a given type specifier. */
5373 static inline void
5374 equate_type_number_to_die (tree type, dw_die_ref type_die)
5376 TYPE_SYMTAB_DIE (type) = type_die;
5379 /* Returns a hash value for X (which really is a die_struct). */
5381 inline hashval_t
5382 decl_die_hasher::hash (die_node *x)
5384 return (hashval_t) x->decl_id;
5387 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5389 inline bool
5390 decl_die_hasher::equal (die_node *x, tree y)
5392 return (x->decl_id == DECL_UID (y));
5395 /* Return the DIE associated with a given declaration. */
5397 static inline dw_die_ref
5398 lookup_decl_die (tree decl)
5400 dw_die_ref *die = decl_die_table->find_slot_with_hash (decl, DECL_UID (decl),
5401 NO_INSERT);
5402 if (!die)
5403 return NULL;
5404 if ((*die)->removed)
5406 decl_die_table->clear_slot (die);
5407 return NULL;
5409 return *die;
5412 /* Returns a hash value for X (which really is a var_loc_list). */
5414 inline hashval_t
5415 decl_loc_hasher::hash (var_loc_list *x)
5417 return (hashval_t) x->decl_id;
5420 /* Return nonzero if decl_id of var_loc_list X is the same as
5421 UID of decl *Y. */
5423 inline bool
5424 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5426 return (x->decl_id == DECL_UID (y));
5429 /* Return the var_loc list associated with a given declaration. */
5431 static inline var_loc_list *
5432 lookup_decl_loc (const_tree decl)
5434 if (!decl_loc_table)
5435 return NULL;
5436 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5439 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5441 inline hashval_t
5442 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5444 return (hashval_t) x->decl_id;
5447 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5448 UID of decl *Y. */
5450 inline bool
5451 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5453 return (x->decl_id == DECL_UID (y));
5456 /* Equate a DIE to a particular declaration. */
5458 static void
5459 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5461 unsigned int decl_id = DECL_UID (decl);
5463 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5464 decl_die->decl_id = decl_id;
5467 /* Return how many bits covers PIECE EXPR_LIST. */
5469 static HOST_WIDE_INT
5470 decl_piece_bitsize (rtx piece)
5472 int ret = (int) GET_MODE (piece);
5473 if (ret)
5474 return ret;
5475 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5476 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5477 return INTVAL (XEXP (XEXP (piece, 0), 0));
5480 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5482 static rtx *
5483 decl_piece_varloc_ptr (rtx piece)
5485 if ((int) GET_MODE (piece))
5486 return &XEXP (piece, 0);
5487 else
5488 return &XEXP (XEXP (piece, 0), 1);
5491 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5492 Next is the chain of following piece nodes. */
5494 static rtx_expr_list *
5495 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5497 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5498 return alloc_EXPR_LIST (bitsize, loc_note, next);
5499 else
5500 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5501 GEN_INT (bitsize),
5502 loc_note), next);
5505 /* Return rtx that should be stored into loc field for
5506 LOC_NOTE and BITPOS/BITSIZE. */
5508 static rtx
5509 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5510 HOST_WIDE_INT bitsize)
5512 if (bitsize != -1)
5514 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5515 if (bitpos != 0)
5516 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5518 return loc_note;
5521 /* This function either modifies location piece list *DEST in
5522 place (if SRC and INNER is NULL), or copies location piece list
5523 *SRC to *DEST while modifying it. Location BITPOS is modified
5524 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5525 not copied and if needed some padding around it is added.
5526 When modifying in place, DEST should point to EXPR_LIST where
5527 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5528 to the start of the whole list and INNER points to the EXPR_LIST
5529 where earlier pieces cover PIECE_BITPOS bits. */
5531 static void
5532 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5533 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5534 HOST_WIDE_INT bitsize, rtx loc_note)
5536 HOST_WIDE_INT diff;
5537 bool copy = inner != NULL;
5539 if (copy)
5541 /* First copy all nodes preceding the current bitpos. */
5542 while (src != inner)
5544 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5545 decl_piece_bitsize (*src), NULL_RTX);
5546 dest = &XEXP (*dest, 1);
5547 src = &XEXP (*src, 1);
5550 /* Add padding if needed. */
5551 if (bitpos != piece_bitpos)
5553 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5554 copy ? NULL_RTX : *dest);
5555 dest = &XEXP (*dest, 1);
5557 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5559 gcc_assert (!copy);
5560 /* A piece with correct bitpos and bitsize already exist,
5561 just update the location for it and return. */
5562 *decl_piece_varloc_ptr (*dest) = loc_note;
5563 return;
5565 /* Add the piece that changed. */
5566 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5567 dest = &XEXP (*dest, 1);
5568 /* Skip over pieces that overlap it. */
5569 diff = bitpos - piece_bitpos + bitsize;
5570 if (!copy)
5571 src = dest;
5572 while (diff > 0 && *src)
5574 rtx piece = *src;
5575 diff -= decl_piece_bitsize (piece);
5576 if (copy)
5577 src = &XEXP (piece, 1);
5578 else
5580 *src = XEXP (piece, 1);
5581 free_EXPR_LIST_node (piece);
5584 /* Add padding if needed. */
5585 if (diff < 0 && *src)
5587 if (!copy)
5588 dest = src;
5589 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5590 dest = &XEXP (*dest, 1);
5592 if (!copy)
5593 return;
5594 /* Finally copy all nodes following it. */
5595 while (*src)
5597 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5598 decl_piece_bitsize (*src), NULL_RTX);
5599 dest = &XEXP (*dest, 1);
5600 src = &XEXP (*src, 1);
5604 /* Add a variable location node to the linked list for DECL. */
5606 static struct var_loc_node *
5607 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5609 unsigned int decl_id;
5610 var_loc_list *temp;
5611 struct var_loc_node *loc = NULL;
5612 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5614 if (VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
5616 tree realdecl = DECL_DEBUG_EXPR (decl);
5617 if (handled_component_p (realdecl)
5618 || (TREE_CODE (realdecl) == MEM_REF
5619 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5621 HOST_WIDE_INT maxsize;
5622 bool reverse;
5623 tree innerdecl
5624 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize,
5625 &reverse);
5626 if (!DECL_P (innerdecl)
5627 || DECL_IGNORED_P (innerdecl)
5628 || TREE_STATIC (innerdecl)
5629 || bitsize <= 0
5630 || bitpos + bitsize > 256
5631 || bitsize != maxsize)
5632 return NULL;
5633 decl = innerdecl;
5637 decl_id = DECL_UID (decl);
5638 var_loc_list **slot
5639 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5640 if (*slot == NULL)
5642 temp = ggc_cleared_alloc<var_loc_list> ();
5643 temp->decl_id = decl_id;
5644 *slot = temp;
5646 else
5647 temp = *slot;
5649 /* For PARM_DECLs try to keep around the original incoming value,
5650 even if that means we'll emit a zero-range .debug_loc entry. */
5651 if (temp->last
5652 && temp->first == temp->last
5653 && TREE_CODE (decl) == PARM_DECL
5654 && NOTE_P (temp->first->loc)
5655 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5656 && DECL_INCOMING_RTL (decl)
5657 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5658 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5659 == GET_CODE (DECL_INCOMING_RTL (decl))
5660 && prev_real_insn (as_a<rtx_insn *> (temp->first->loc)) == NULL_RTX
5661 && (bitsize != -1
5662 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5663 NOTE_VAR_LOCATION_LOC (loc_note))
5664 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5665 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5667 loc = ggc_cleared_alloc<var_loc_node> ();
5668 temp->first->next = loc;
5669 temp->last = loc;
5670 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5672 else if (temp->last)
5674 struct var_loc_node *last = temp->last, *unused = NULL;
5675 rtx *piece_loc = NULL, last_loc_note;
5676 HOST_WIDE_INT piece_bitpos = 0;
5677 if (last->next)
5679 last = last->next;
5680 gcc_assert (last->next == NULL);
5682 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5684 piece_loc = &last->loc;
5687 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5688 if (piece_bitpos + cur_bitsize > bitpos)
5689 break;
5690 piece_bitpos += cur_bitsize;
5691 piece_loc = &XEXP (*piece_loc, 1);
5693 while (*piece_loc);
5695 /* TEMP->LAST here is either pointer to the last but one or
5696 last element in the chained list, LAST is pointer to the
5697 last element. */
5698 if (label && strcmp (last->label, label) == 0)
5700 /* For SRA optimized variables if there weren't any real
5701 insns since last note, just modify the last node. */
5702 if (piece_loc != NULL)
5704 adjust_piece_list (piece_loc, NULL, NULL,
5705 bitpos, piece_bitpos, bitsize, loc_note);
5706 return NULL;
5708 /* If the last note doesn't cover any instructions, remove it. */
5709 if (temp->last != last)
5711 temp->last->next = NULL;
5712 unused = last;
5713 last = temp->last;
5714 gcc_assert (strcmp (last->label, label) != 0);
5716 else
5718 gcc_assert (temp->first == temp->last
5719 || (temp->first->next == temp->last
5720 && TREE_CODE (decl) == PARM_DECL));
5721 memset (temp->last, '\0', sizeof (*temp->last));
5722 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5723 return temp->last;
5726 if (bitsize == -1 && NOTE_P (last->loc))
5727 last_loc_note = last->loc;
5728 else if (piece_loc != NULL
5729 && *piece_loc != NULL_RTX
5730 && piece_bitpos == bitpos
5731 && decl_piece_bitsize (*piece_loc) == bitsize)
5732 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5733 else
5734 last_loc_note = NULL_RTX;
5735 /* If the current location is the same as the end of the list,
5736 and either both or neither of the locations is uninitialized,
5737 we have nothing to do. */
5738 if (last_loc_note == NULL_RTX
5739 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5740 NOTE_VAR_LOCATION_LOC (loc_note)))
5741 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5742 != NOTE_VAR_LOCATION_STATUS (loc_note))
5743 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5744 == VAR_INIT_STATUS_UNINITIALIZED)
5745 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5746 == VAR_INIT_STATUS_UNINITIALIZED))))
5748 /* Add LOC to the end of list and update LAST. If the last
5749 element of the list has been removed above, reuse its
5750 memory for the new node, otherwise allocate a new one. */
5751 if (unused)
5753 loc = unused;
5754 memset (loc, '\0', sizeof (*loc));
5756 else
5757 loc = ggc_cleared_alloc<var_loc_node> ();
5758 if (bitsize == -1 || piece_loc == NULL)
5759 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5760 else
5761 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5762 bitpos, piece_bitpos, bitsize, loc_note);
5763 last->next = loc;
5764 /* Ensure TEMP->LAST will point either to the new last but one
5765 element of the chain, or to the last element in it. */
5766 if (last != temp->last)
5767 temp->last = last;
5769 else if (unused)
5770 ggc_free (unused);
5772 else
5774 loc = ggc_cleared_alloc<var_loc_node> ();
5775 temp->first = loc;
5776 temp->last = loc;
5777 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5779 return loc;
5782 /* Keep track of the number of spaces used to indent the
5783 output of the debugging routines that print the structure of
5784 the DIE internal representation. */
5785 static int print_indent;
5787 /* Indent the line the number of spaces given by print_indent. */
5789 static inline void
5790 print_spaces (FILE *outfile)
5792 fprintf (outfile, "%*s", print_indent, "");
5795 /* Print a type signature in hex. */
5797 static inline void
5798 print_signature (FILE *outfile, char *sig)
5800 int i;
5802 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5803 fprintf (outfile, "%02x", sig[i] & 0xff);
5806 static inline void
5807 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
5809 if (discr_value->pos)
5810 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
5811 else
5812 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
5815 static void print_loc_descr (dw_loc_descr_ref, FILE *);
5817 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
5818 RECURSE, output location descriptor operations. */
5820 static void
5821 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
5823 switch (val->val_class)
5825 case dw_val_class_addr:
5826 fprintf (outfile, "address");
5827 break;
5828 case dw_val_class_offset:
5829 fprintf (outfile, "offset");
5830 break;
5831 case dw_val_class_loc:
5832 fprintf (outfile, "location descriptor");
5833 if (val->v.val_loc == NULL)
5834 fprintf (outfile, " -> <null>\n");
5835 else if (recurse)
5837 fprintf (outfile, ":\n");
5838 print_indent += 4;
5839 print_loc_descr (val->v.val_loc, outfile);
5840 print_indent -= 4;
5842 else
5843 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
5844 break;
5845 case dw_val_class_loc_list:
5846 fprintf (outfile, "location list -> label:%s",
5847 val->v.val_loc_list->ll_symbol);
5848 break;
5849 case dw_val_class_range_list:
5850 fprintf (outfile, "range list");
5851 break;
5852 case dw_val_class_const:
5853 case dw_val_class_const_implicit:
5854 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
5855 break;
5856 case dw_val_class_unsigned_const:
5857 case dw_val_class_unsigned_const_implicit:
5858 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
5859 break;
5860 case dw_val_class_const_double:
5861 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
5862 HOST_WIDE_INT_PRINT_UNSIGNED")",
5863 val->v.val_double.high,
5864 val->v.val_double.low);
5865 break;
5866 case dw_val_class_wide_int:
5868 int i = val->v.val_wide->get_len ();
5869 fprintf (outfile, "constant (");
5870 gcc_assert (i > 0);
5871 if (val->v.val_wide->elt (i - 1) == 0)
5872 fprintf (outfile, "0x");
5873 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5874 val->v.val_wide->elt (--i));
5875 while (--i >= 0)
5876 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5877 val->v.val_wide->elt (i));
5878 fprintf (outfile, ")");
5879 break;
5881 case dw_val_class_vec:
5882 fprintf (outfile, "floating-point or vector constant");
5883 break;
5884 case dw_val_class_flag:
5885 fprintf (outfile, "%u", val->v.val_flag);
5886 break;
5887 case dw_val_class_die_ref:
5888 if (val->v.val_die_ref.die != NULL)
5890 dw_die_ref die = val->v.val_die_ref.die;
5892 if (die->comdat_type_p)
5894 fprintf (outfile, "die -> signature: ");
5895 print_signature (outfile,
5896 die->die_id.die_type_node->signature);
5898 else if (die->die_id.die_symbol)
5899 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
5900 else
5901 fprintf (outfile, "die -> %ld", die->die_offset);
5902 fprintf (outfile, " (%p)", (void *) die);
5904 else
5905 fprintf (outfile, "die -> <null>");
5906 break;
5907 case dw_val_class_vms_delta:
5908 fprintf (outfile, "delta: @slotcount(%s-%s)",
5909 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
5910 break;
5911 case dw_val_class_lbl_id:
5912 case dw_val_class_lineptr:
5913 case dw_val_class_macptr:
5914 case dw_val_class_loclistsptr:
5915 case dw_val_class_high_pc:
5916 fprintf (outfile, "label: %s", val->v.val_lbl_id);
5917 break;
5918 case dw_val_class_str:
5919 if (val->v.val_str->str != NULL)
5920 fprintf (outfile, "\"%s\"", val->v.val_str->str);
5921 else
5922 fprintf (outfile, "<null>");
5923 break;
5924 case dw_val_class_file:
5925 case dw_val_class_file_implicit:
5926 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
5927 val->v.val_file->emitted_number);
5928 break;
5929 case dw_val_class_data8:
5931 int i;
5933 for (i = 0; i < 8; i++)
5934 fprintf (outfile, "%02x", val->v.val_data8[i]);
5935 break;
5937 case dw_val_class_discr_value:
5938 print_discr_value (outfile, &val->v.val_discr_value);
5939 break;
5940 case dw_val_class_discr_list:
5941 for (dw_discr_list_ref node = val->v.val_discr_list;
5942 node != NULL;
5943 node = node->dw_discr_next)
5945 if (node->dw_discr_range)
5947 fprintf (outfile, " .. ");
5948 print_discr_value (outfile, &node->dw_discr_lower_bound);
5949 print_discr_value (outfile, &node->dw_discr_upper_bound);
5951 else
5952 print_discr_value (outfile, &node->dw_discr_lower_bound);
5954 if (node->dw_discr_next != NULL)
5955 fprintf (outfile, " | ");
5957 default:
5958 break;
5962 /* Likewise, for a DIE attribute. */
5964 static void
5965 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
5967 print_dw_val (&a->dw_attr_val, recurse, outfile);
5971 /* Print the list of operands in the LOC location description to OUTFILE. This
5972 routine is a debugging aid only. */
5974 static void
5975 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
5977 dw_loc_descr_ref l = loc;
5979 if (loc == NULL)
5981 print_spaces (outfile);
5982 fprintf (outfile, "<null>\n");
5983 return;
5986 for (l = loc; l != NULL; l = l->dw_loc_next)
5988 print_spaces (outfile);
5989 fprintf (outfile, "(%p) %s",
5990 (void *) l,
5991 dwarf_stack_op_name (l->dw_loc_opc));
5992 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
5994 fprintf (outfile, " ");
5995 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
5997 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
5999 fprintf (outfile, ", ");
6000 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
6002 fprintf (outfile, "\n");
6006 /* Print the information associated with a given DIE, and its children.
6007 This routine is a debugging aid only. */
6009 static void
6010 print_die (dw_die_ref die, FILE *outfile)
6012 dw_attr_node *a;
6013 dw_die_ref c;
6014 unsigned ix;
6016 print_spaces (outfile);
6017 fprintf (outfile, "DIE %4ld: %s (%p)\n",
6018 die->die_offset, dwarf_tag_name (die->die_tag),
6019 (void*) die);
6020 print_spaces (outfile);
6021 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
6022 fprintf (outfile, " offset: %ld", die->die_offset);
6023 fprintf (outfile, " mark: %d\n", die->die_mark);
6025 if (die->comdat_type_p)
6027 print_spaces (outfile);
6028 fprintf (outfile, " signature: ");
6029 print_signature (outfile, die->die_id.die_type_node->signature);
6030 fprintf (outfile, "\n");
6033 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6035 print_spaces (outfile);
6036 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
6038 print_attribute (a, true, outfile);
6039 fprintf (outfile, "\n");
6042 if (die->die_child != NULL)
6044 print_indent += 4;
6045 FOR_EACH_CHILD (die, c, print_die (c, outfile));
6046 print_indent -= 4;
6048 if (print_indent == 0)
6049 fprintf (outfile, "\n");
6052 /* Print the list of operations in the LOC location description. */
6054 DEBUG_FUNCTION void
6055 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
6057 print_loc_descr (loc, stderr);
6060 /* Print the information collected for a given DIE. */
6062 DEBUG_FUNCTION void
6063 debug_dwarf_die (dw_die_ref die)
6065 print_die (die, stderr);
6068 DEBUG_FUNCTION void
6069 debug (die_struct &ref)
6071 print_die (&ref, stderr);
6074 DEBUG_FUNCTION void
6075 debug (die_struct *ptr)
6077 if (ptr)
6078 debug (*ptr);
6079 else
6080 fprintf (stderr, "<nil>\n");
6084 /* Print all DWARF information collected for the compilation unit.
6085 This routine is a debugging aid only. */
6087 DEBUG_FUNCTION void
6088 debug_dwarf (void)
6090 print_indent = 0;
6091 print_die (comp_unit_die (), stderr);
6094 /* Verify the DIE tree structure. */
6096 DEBUG_FUNCTION void
6097 verify_die (dw_die_ref die)
6099 gcc_assert (!die->die_mark);
6100 if (die->die_parent == NULL
6101 && die->die_sib == NULL)
6102 return;
6103 /* Verify the die_sib list is cyclic. */
6104 dw_die_ref x = die;
6107 x->die_mark = 1;
6108 x = x->die_sib;
6110 while (x && !x->die_mark);
6111 gcc_assert (x == die);
6112 x = die;
6115 /* Verify all dies have the same parent. */
6116 gcc_assert (x->die_parent == die->die_parent);
6117 if (x->die_child)
6119 /* Verify the child has the proper parent and recurse. */
6120 gcc_assert (x->die_child->die_parent == x);
6121 verify_die (x->die_child);
6123 x->die_mark = 0;
6124 x = x->die_sib;
6126 while (x && x->die_mark);
6129 /* Sanity checks on DIEs. */
6131 static void
6132 check_die (dw_die_ref die)
6134 unsigned ix;
6135 dw_attr_node *a;
6136 bool inline_found = false;
6137 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
6138 int n_decl_line = 0, n_decl_column = 0, n_decl_file = 0;
6139 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6141 switch (a->dw_attr)
6143 case DW_AT_inline:
6144 if (a->dw_attr_val.v.val_unsigned)
6145 inline_found = true;
6146 break;
6147 case DW_AT_location:
6148 ++n_location;
6149 break;
6150 case DW_AT_low_pc:
6151 ++n_low_pc;
6152 break;
6153 case DW_AT_high_pc:
6154 ++n_high_pc;
6155 break;
6156 case DW_AT_artificial:
6157 ++n_artificial;
6158 break;
6159 case DW_AT_decl_column:
6160 ++n_decl_column;
6161 break;
6162 case DW_AT_decl_line:
6163 ++n_decl_line;
6164 break;
6165 case DW_AT_decl_file:
6166 ++n_decl_file;
6167 break;
6168 default:
6169 break;
6172 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
6173 || n_decl_column > 1 || n_decl_line > 1 || n_decl_file > 1)
6175 fprintf (stderr, "Duplicate attributes in DIE:\n");
6176 debug_dwarf_die (die);
6177 gcc_unreachable ();
6179 if (inline_found)
6181 /* A debugging information entry that is a member of an abstract
6182 instance tree [that has DW_AT_inline] should not contain any
6183 attributes which describe aspects of the subroutine which vary
6184 between distinct inlined expansions or distinct out-of-line
6185 expansions. */
6186 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6187 gcc_assert (a->dw_attr != DW_AT_low_pc
6188 && a->dw_attr != DW_AT_high_pc
6189 && a->dw_attr != DW_AT_location
6190 && a->dw_attr != DW_AT_frame_base
6191 && a->dw_attr != DW_AT_call_all_calls
6192 && a->dw_attr != DW_AT_GNU_all_call_sites);
6196 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
6197 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
6198 DIE that marks the start of the DIEs for this include file. */
6200 static dw_die_ref
6201 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
6203 const char *filename = get_AT_string (bincl_die, DW_AT_name);
6204 dw_die_ref new_unit = gen_compile_unit_die (filename);
6206 new_unit->die_sib = old_unit;
6207 return new_unit;
6210 /* Close an include-file CU and reopen the enclosing one. */
6212 static dw_die_ref
6213 pop_compile_unit (dw_die_ref old_unit)
6215 dw_die_ref new_unit = old_unit->die_sib;
6217 old_unit->die_sib = NULL;
6218 return new_unit;
6221 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6222 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6223 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6225 /* Calculate the checksum of a location expression. */
6227 static inline void
6228 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6230 int tem;
6231 inchash::hash hstate;
6232 hashval_t hash;
6234 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
6235 CHECKSUM (tem);
6236 hash_loc_operands (loc, hstate);
6237 hash = hstate.end();
6238 CHECKSUM (hash);
6241 /* Calculate the checksum of an attribute. */
6243 static void
6244 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
6246 dw_loc_descr_ref loc;
6247 rtx r;
6249 CHECKSUM (at->dw_attr);
6251 /* We don't care that this was compiled with a different compiler
6252 snapshot; if the output is the same, that's what matters. */
6253 if (at->dw_attr == DW_AT_producer)
6254 return;
6256 switch (AT_class (at))
6258 case dw_val_class_const:
6259 case dw_val_class_const_implicit:
6260 CHECKSUM (at->dw_attr_val.v.val_int);
6261 break;
6262 case dw_val_class_unsigned_const:
6263 case dw_val_class_unsigned_const_implicit:
6264 CHECKSUM (at->dw_attr_val.v.val_unsigned);
6265 break;
6266 case dw_val_class_const_double:
6267 CHECKSUM (at->dw_attr_val.v.val_double);
6268 break;
6269 case dw_val_class_wide_int:
6270 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6271 get_full_len (*at->dw_attr_val.v.val_wide)
6272 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6273 break;
6274 case dw_val_class_vec:
6275 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6276 (at->dw_attr_val.v.val_vec.length
6277 * at->dw_attr_val.v.val_vec.elt_size));
6278 break;
6279 case dw_val_class_flag:
6280 CHECKSUM (at->dw_attr_val.v.val_flag);
6281 break;
6282 case dw_val_class_str:
6283 CHECKSUM_STRING (AT_string (at));
6284 break;
6286 case dw_val_class_addr:
6287 r = AT_addr (at);
6288 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6289 CHECKSUM_STRING (XSTR (r, 0));
6290 break;
6292 case dw_val_class_offset:
6293 CHECKSUM (at->dw_attr_val.v.val_offset);
6294 break;
6296 case dw_val_class_loc:
6297 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6298 loc_checksum (loc, ctx);
6299 break;
6301 case dw_val_class_die_ref:
6302 die_checksum (AT_ref (at), ctx, mark);
6303 break;
6305 case dw_val_class_fde_ref:
6306 case dw_val_class_vms_delta:
6307 case dw_val_class_lbl_id:
6308 case dw_val_class_lineptr:
6309 case dw_val_class_macptr:
6310 case dw_val_class_loclistsptr:
6311 case dw_val_class_high_pc:
6312 break;
6314 case dw_val_class_file:
6315 case dw_val_class_file_implicit:
6316 CHECKSUM_STRING (AT_file (at)->filename);
6317 break;
6319 case dw_val_class_data8:
6320 CHECKSUM (at->dw_attr_val.v.val_data8);
6321 break;
6323 default:
6324 break;
6328 /* Calculate the checksum of a DIE. */
6330 static void
6331 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6333 dw_die_ref c;
6334 dw_attr_node *a;
6335 unsigned ix;
6337 /* To avoid infinite recursion. */
6338 if (die->die_mark)
6340 CHECKSUM (die->die_mark);
6341 return;
6343 die->die_mark = ++(*mark);
6345 CHECKSUM (die->die_tag);
6347 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6348 attr_checksum (a, ctx, mark);
6350 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6353 #undef CHECKSUM
6354 #undef CHECKSUM_BLOCK
6355 #undef CHECKSUM_STRING
6357 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
6358 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6359 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6360 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
6361 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
6362 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
6363 #define CHECKSUM_ATTR(FOO) \
6364 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
6366 /* Calculate the checksum of a number in signed LEB128 format. */
6368 static void
6369 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
6371 unsigned char byte;
6372 bool more;
6374 while (1)
6376 byte = (value & 0x7f);
6377 value >>= 7;
6378 more = !((value == 0 && (byte & 0x40) == 0)
6379 || (value == -1 && (byte & 0x40) != 0));
6380 if (more)
6381 byte |= 0x80;
6382 CHECKSUM (byte);
6383 if (!more)
6384 break;
6388 /* Calculate the checksum of a number in unsigned LEB128 format. */
6390 static void
6391 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
6393 while (1)
6395 unsigned char byte = (value & 0x7f);
6396 value >>= 7;
6397 if (value != 0)
6398 /* More bytes to follow. */
6399 byte |= 0x80;
6400 CHECKSUM (byte);
6401 if (value == 0)
6402 break;
6406 /* Checksum the context of the DIE. This adds the names of any
6407 surrounding namespaces or structures to the checksum. */
6409 static void
6410 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
6412 const char *name;
6413 dw_die_ref spec;
6414 int tag = die->die_tag;
6416 if (tag != DW_TAG_namespace
6417 && tag != DW_TAG_structure_type
6418 && tag != DW_TAG_class_type)
6419 return;
6421 name = get_AT_string (die, DW_AT_name);
6423 spec = get_AT_ref (die, DW_AT_specification);
6424 if (spec != NULL)
6425 die = spec;
6427 if (die->die_parent != NULL)
6428 checksum_die_context (die->die_parent, ctx);
6430 CHECKSUM_ULEB128 ('C');
6431 CHECKSUM_ULEB128 (tag);
6432 if (name != NULL)
6433 CHECKSUM_STRING (name);
6436 /* Calculate the checksum of a location expression. */
6438 static inline void
6439 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6441 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
6442 were emitted as a DW_FORM_sdata instead of a location expression. */
6443 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
6445 CHECKSUM_ULEB128 (DW_FORM_sdata);
6446 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6447 return;
6450 /* Otherwise, just checksum the raw location expression. */
6451 while (loc != NULL)
6453 inchash::hash hstate;
6454 hashval_t hash;
6456 CHECKSUM_ULEB128 (loc->dtprel);
6457 CHECKSUM_ULEB128 (loc->dw_loc_opc);
6458 hash_loc_operands (loc, hstate);
6459 hash = hstate.end ();
6460 CHECKSUM (hash);
6461 loc = loc->dw_loc_next;
6465 /* Calculate the checksum of an attribute. */
6467 static void
6468 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
6469 struct md5_ctx *ctx, int *mark)
6471 dw_loc_descr_ref loc;
6472 rtx r;
6474 if (AT_class (at) == dw_val_class_die_ref)
6476 dw_die_ref target_die = AT_ref (at);
6478 /* For pointer and reference types, we checksum only the (qualified)
6479 name of the target type (if there is a name). For friend entries,
6480 we checksum only the (qualified) name of the target type or function.
6481 This allows the checksum to remain the same whether the target type
6482 is complete or not. */
6483 if ((at->dw_attr == DW_AT_type
6484 && (tag == DW_TAG_pointer_type
6485 || tag == DW_TAG_reference_type
6486 || tag == DW_TAG_rvalue_reference_type
6487 || tag == DW_TAG_ptr_to_member_type))
6488 || (at->dw_attr == DW_AT_friend
6489 && tag == DW_TAG_friend))
6491 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
6493 if (name_attr != NULL)
6495 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6497 if (decl == NULL)
6498 decl = target_die;
6499 CHECKSUM_ULEB128 ('N');
6500 CHECKSUM_ULEB128 (at->dw_attr);
6501 if (decl->die_parent != NULL)
6502 checksum_die_context (decl->die_parent, ctx);
6503 CHECKSUM_ULEB128 ('E');
6504 CHECKSUM_STRING (AT_string (name_attr));
6505 return;
6509 /* For all other references to another DIE, we check to see if the
6510 target DIE has already been visited. If it has, we emit a
6511 backward reference; if not, we descend recursively. */
6512 if (target_die->die_mark > 0)
6514 CHECKSUM_ULEB128 ('R');
6515 CHECKSUM_ULEB128 (at->dw_attr);
6516 CHECKSUM_ULEB128 (target_die->die_mark);
6518 else
6520 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6522 if (decl == NULL)
6523 decl = target_die;
6524 target_die->die_mark = ++(*mark);
6525 CHECKSUM_ULEB128 ('T');
6526 CHECKSUM_ULEB128 (at->dw_attr);
6527 if (decl->die_parent != NULL)
6528 checksum_die_context (decl->die_parent, ctx);
6529 die_checksum_ordered (target_die, ctx, mark);
6531 return;
6534 CHECKSUM_ULEB128 ('A');
6535 CHECKSUM_ULEB128 (at->dw_attr);
6537 switch (AT_class (at))
6539 case dw_val_class_const:
6540 case dw_val_class_const_implicit:
6541 CHECKSUM_ULEB128 (DW_FORM_sdata);
6542 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6543 break;
6545 case dw_val_class_unsigned_const:
6546 case dw_val_class_unsigned_const_implicit:
6547 CHECKSUM_ULEB128 (DW_FORM_sdata);
6548 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6549 break;
6551 case dw_val_class_const_double:
6552 CHECKSUM_ULEB128 (DW_FORM_block);
6553 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6554 CHECKSUM (at->dw_attr_val.v.val_double);
6555 break;
6557 case dw_val_class_wide_int:
6558 CHECKSUM_ULEB128 (DW_FORM_block);
6559 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6560 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6561 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6562 get_full_len (*at->dw_attr_val.v.val_wide)
6563 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6564 break;
6566 case dw_val_class_vec:
6567 CHECKSUM_ULEB128 (DW_FORM_block);
6568 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6569 * at->dw_attr_val.v.val_vec.elt_size);
6570 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6571 (at->dw_attr_val.v.val_vec.length
6572 * at->dw_attr_val.v.val_vec.elt_size));
6573 break;
6575 case dw_val_class_flag:
6576 CHECKSUM_ULEB128 (DW_FORM_flag);
6577 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6578 break;
6580 case dw_val_class_str:
6581 CHECKSUM_ULEB128 (DW_FORM_string);
6582 CHECKSUM_STRING (AT_string (at));
6583 break;
6585 case dw_val_class_addr:
6586 r = AT_addr (at);
6587 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6588 CHECKSUM_ULEB128 (DW_FORM_string);
6589 CHECKSUM_STRING (XSTR (r, 0));
6590 break;
6592 case dw_val_class_offset:
6593 CHECKSUM_ULEB128 (DW_FORM_sdata);
6594 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6595 break;
6597 case dw_val_class_loc:
6598 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6599 loc_checksum_ordered (loc, ctx);
6600 break;
6602 case dw_val_class_fde_ref:
6603 case dw_val_class_lbl_id:
6604 case dw_val_class_lineptr:
6605 case dw_val_class_macptr:
6606 case dw_val_class_loclistsptr:
6607 case dw_val_class_high_pc:
6608 break;
6610 case dw_val_class_file:
6611 case dw_val_class_file_implicit:
6612 CHECKSUM_ULEB128 (DW_FORM_string);
6613 CHECKSUM_STRING (AT_file (at)->filename);
6614 break;
6616 case dw_val_class_data8:
6617 CHECKSUM (at->dw_attr_val.v.val_data8);
6618 break;
6620 default:
6621 break;
6625 struct checksum_attributes
6627 dw_attr_node *at_name;
6628 dw_attr_node *at_type;
6629 dw_attr_node *at_friend;
6630 dw_attr_node *at_accessibility;
6631 dw_attr_node *at_address_class;
6632 dw_attr_node *at_alignment;
6633 dw_attr_node *at_allocated;
6634 dw_attr_node *at_artificial;
6635 dw_attr_node *at_associated;
6636 dw_attr_node *at_binary_scale;
6637 dw_attr_node *at_bit_offset;
6638 dw_attr_node *at_bit_size;
6639 dw_attr_node *at_bit_stride;
6640 dw_attr_node *at_byte_size;
6641 dw_attr_node *at_byte_stride;
6642 dw_attr_node *at_const_value;
6643 dw_attr_node *at_containing_type;
6644 dw_attr_node *at_count;
6645 dw_attr_node *at_data_location;
6646 dw_attr_node *at_data_member_location;
6647 dw_attr_node *at_decimal_scale;
6648 dw_attr_node *at_decimal_sign;
6649 dw_attr_node *at_default_value;
6650 dw_attr_node *at_digit_count;
6651 dw_attr_node *at_discr;
6652 dw_attr_node *at_discr_list;
6653 dw_attr_node *at_discr_value;
6654 dw_attr_node *at_encoding;
6655 dw_attr_node *at_endianity;
6656 dw_attr_node *at_explicit;
6657 dw_attr_node *at_is_optional;
6658 dw_attr_node *at_location;
6659 dw_attr_node *at_lower_bound;
6660 dw_attr_node *at_mutable;
6661 dw_attr_node *at_ordering;
6662 dw_attr_node *at_picture_string;
6663 dw_attr_node *at_prototyped;
6664 dw_attr_node *at_small;
6665 dw_attr_node *at_segment;
6666 dw_attr_node *at_string_length;
6667 dw_attr_node *at_string_length_bit_size;
6668 dw_attr_node *at_string_length_byte_size;
6669 dw_attr_node *at_threads_scaled;
6670 dw_attr_node *at_upper_bound;
6671 dw_attr_node *at_use_location;
6672 dw_attr_node *at_use_UTF8;
6673 dw_attr_node *at_variable_parameter;
6674 dw_attr_node *at_virtuality;
6675 dw_attr_node *at_visibility;
6676 dw_attr_node *at_vtable_elem_location;
6679 /* Collect the attributes that we will want to use for the checksum. */
6681 static void
6682 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6684 dw_attr_node *a;
6685 unsigned ix;
6687 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6689 switch (a->dw_attr)
6691 case DW_AT_name:
6692 attrs->at_name = a;
6693 break;
6694 case DW_AT_type:
6695 attrs->at_type = a;
6696 break;
6697 case DW_AT_friend:
6698 attrs->at_friend = a;
6699 break;
6700 case DW_AT_accessibility:
6701 attrs->at_accessibility = a;
6702 break;
6703 case DW_AT_address_class:
6704 attrs->at_address_class = a;
6705 break;
6706 case DW_AT_alignment:
6707 attrs->at_alignment = a;
6708 break;
6709 case DW_AT_allocated:
6710 attrs->at_allocated = a;
6711 break;
6712 case DW_AT_artificial:
6713 attrs->at_artificial = a;
6714 break;
6715 case DW_AT_associated:
6716 attrs->at_associated = a;
6717 break;
6718 case DW_AT_binary_scale:
6719 attrs->at_binary_scale = a;
6720 break;
6721 case DW_AT_bit_offset:
6722 attrs->at_bit_offset = a;
6723 break;
6724 case DW_AT_bit_size:
6725 attrs->at_bit_size = a;
6726 break;
6727 case DW_AT_bit_stride:
6728 attrs->at_bit_stride = a;
6729 break;
6730 case DW_AT_byte_size:
6731 attrs->at_byte_size = a;
6732 break;
6733 case DW_AT_byte_stride:
6734 attrs->at_byte_stride = a;
6735 break;
6736 case DW_AT_const_value:
6737 attrs->at_const_value = a;
6738 break;
6739 case DW_AT_containing_type:
6740 attrs->at_containing_type = a;
6741 break;
6742 case DW_AT_count:
6743 attrs->at_count = a;
6744 break;
6745 case DW_AT_data_location:
6746 attrs->at_data_location = a;
6747 break;
6748 case DW_AT_data_member_location:
6749 attrs->at_data_member_location = a;
6750 break;
6751 case DW_AT_decimal_scale:
6752 attrs->at_decimal_scale = a;
6753 break;
6754 case DW_AT_decimal_sign:
6755 attrs->at_decimal_sign = a;
6756 break;
6757 case DW_AT_default_value:
6758 attrs->at_default_value = a;
6759 break;
6760 case DW_AT_digit_count:
6761 attrs->at_digit_count = a;
6762 break;
6763 case DW_AT_discr:
6764 attrs->at_discr = a;
6765 break;
6766 case DW_AT_discr_list:
6767 attrs->at_discr_list = a;
6768 break;
6769 case DW_AT_discr_value:
6770 attrs->at_discr_value = a;
6771 break;
6772 case DW_AT_encoding:
6773 attrs->at_encoding = a;
6774 break;
6775 case DW_AT_endianity:
6776 attrs->at_endianity = a;
6777 break;
6778 case DW_AT_explicit:
6779 attrs->at_explicit = a;
6780 break;
6781 case DW_AT_is_optional:
6782 attrs->at_is_optional = a;
6783 break;
6784 case DW_AT_location:
6785 attrs->at_location = a;
6786 break;
6787 case DW_AT_lower_bound:
6788 attrs->at_lower_bound = a;
6789 break;
6790 case DW_AT_mutable:
6791 attrs->at_mutable = a;
6792 break;
6793 case DW_AT_ordering:
6794 attrs->at_ordering = a;
6795 break;
6796 case DW_AT_picture_string:
6797 attrs->at_picture_string = a;
6798 break;
6799 case DW_AT_prototyped:
6800 attrs->at_prototyped = a;
6801 break;
6802 case DW_AT_small:
6803 attrs->at_small = a;
6804 break;
6805 case DW_AT_segment:
6806 attrs->at_segment = a;
6807 break;
6808 case DW_AT_string_length:
6809 attrs->at_string_length = a;
6810 break;
6811 case DW_AT_string_length_bit_size:
6812 attrs->at_string_length_bit_size = a;
6813 break;
6814 case DW_AT_string_length_byte_size:
6815 attrs->at_string_length_byte_size = a;
6816 break;
6817 case DW_AT_threads_scaled:
6818 attrs->at_threads_scaled = a;
6819 break;
6820 case DW_AT_upper_bound:
6821 attrs->at_upper_bound = a;
6822 break;
6823 case DW_AT_use_location:
6824 attrs->at_use_location = a;
6825 break;
6826 case DW_AT_use_UTF8:
6827 attrs->at_use_UTF8 = a;
6828 break;
6829 case DW_AT_variable_parameter:
6830 attrs->at_variable_parameter = a;
6831 break;
6832 case DW_AT_virtuality:
6833 attrs->at_virtuality = a;
6834 break;
6835 case DW_AT_visibility:
6836 attrs->at_visibility = a;
6837 break;
6838 case DW_AT_vtable_elem_location:
6839 attrs->at_vtable_elem_location = a;
6840 break;
6841 default:
6842 break;
6847 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6849 static void
6850 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6852 dw_die_ref c;
6853 dw_die_ref decl;
6854 struct checksum_attributes attrs;
6856 CHECKSUM_ULEB128 ('D');
6857 CHECKSUM_ULEB128 (die->die_tag);
6859 memset (&attrs, 0, sizeof (attrs));
6861 decl = get_AT_ref (die, DW_AT_specification);
6862 if (decl != NULL)
6863 collect_checksum_attributes (&attrs, decl);
6864 collect_checksum_attributes (&attrs, die);
6866 CHECKSUM_ATTR (attrs.at_name);
6867 CHECKSUM_ATTR (attrs.at_accessibility);
6868 CHECKSUM_ATTR (attrs.at_address_class);
6869 CHECKSUM_ATTR (attrs.at_allocated);
6870 CHECKSUM_ATTR (attrs.at_artificial);
6871 CHECKSUM_ATTR (attrs.at_associated);
6872 CHECKSUM_ATTR (attrs.at_binary_scale);
6873 CHECKSUM_ATTR (attrs.at_bit_offset);
6874 CHECKSUM_ATTR (attrs.at_bit_size);
6875 CHECKSUM_ATTR (attrs.at_bit_stride);
6876 CHECKSUM_ATTR (attrs.at_byte_size);
6877 CHECKSUM_ATTR (attrs.at_byte_stride);
6878 CHECKSUM_ATTR (attrs.at_const_value);
6879 CHECKSUM_ATTR (attrs.at_containing_type);
6880 CHECKSUM_ATTR (attrs.at_count);
6881 CHECKSUM_ATTR (attrs.at_data_location);
6882 CHECKSUM_ATTR (attrs.at_data_member_location);
6883 CHECKSUM_ATTR (attrs.at_decimal_scale);
6884 CHECKSUM_ATTR (attrs.at_decimal_sign);
6885 CHECKSUM_ATTR (attrs.at_default_value);
6886 CHECKSUM_ATTR (attrs.at_digit_count);
6887 CHECKSUM_ATTR (attrs.at_discr);
6888 CHECKSUM_ATTR (attrs.at_discr_list);
6889 CHECKSUM_ATTR (attrs.at_discr_value);
6890 CHECKSUM_ATTR (attrs.at_encoding);
6891 CHECKSUM_ATTR (attrs.at_endianity);
6892 CHECKSUM_ATTR (attrs.at_explicit);
6893 CHECKSUM_ATTR (attrs.at_is_optional);
6894 CHECKSUM_ATTR (attrs.at_location);
6895 CHECKSUM_ATTR (attrs.at_lower_bound);
6896 CHECKSUM_ATTR (attrs.at_mutable);
6897 CHECKSUM_ATTR (attrs.at_ordering);
6898 CHECKSUM_ATTR (attrs.at_picture_string);
6899 CHECKSUM_ATTR (attrs.at_prototyped);
6900 CHECKSUM_ATTR (attrs.at_small);
6901 CHECKSUM_ATTR (attrs.at_segment);
6902 CHECKSUM_ATTR (attrs.at_string_length);
6903 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
6904 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
6905 CHECKSUM_ATTR (attrs.at_threads_scaled);
6906 CHECKSUM_ATTR (attrs.at_upper_bound);
6907 CHECKSUM_ATTR (attrs.at_use_location);
6908 CHECKSUM_ATTR (attrs.at_use_UTF8);
6909 CHECKSUM_ATTR (attrs.at_variable_parameter);
6910 CHECKSUM_ATTR (attrs.at_virtuality);
6911 CHECKSUM_ATTR (attrs.at_visibility);
6912 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6913 CHECKSUM_ATTR (attrs.at_type);
6914 CHECKSUM_ATTR (attrs.at_friend);
6915 CHECKSUM_ATTR (attrs.at_alignment);
6917 /* Checksum the child DIEs. */
6918 c = die->die_child;
6919 if (c) do {
6920 dw_attr_node *name_attr;
6922 c = c->die_sib;
6923 name_attr = get_AT (c, DW_AT_name);
6924 if (is_template_instantiation (c))
6926 /* Ignore instantiations of member type and function templates. */
6928 else if (name_attr != NULL
6929 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6931 /* Use a shallow checksum for named nested types and member
6932 functions. */
6933 CHECKSUM_ULEB128 ('S');
6934 CHECKSUM_ULEB128 (c->die_tag);
6935 CHECKSUM_STRING (AT_string (name_attr));
6937 else
6939 /* Use a deep checksum for other children. */
6940 /* Mark this DIE so it gets processed when unmarking. */
6941 if (c->die_mark == 0)
6942 c->die_mark = -1;
6943 die_checksum_ordered (c, ctx, mark);
6945 } while (c != die->die_child);
6947 CHECKSUM_ULEB128 (0);
6950 /* Add a type name and tag to a hash. */
6951 static void
6952 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6954 CHECKSUM_ULEB128 (tag);
6955 CHECKSUM_STRING (name);
6958 #undef CHECKSUM
6959 #undef CHECKSUM_STRING
6960 #undef CHECKSUM_ATTR
6961 #undef CHECKSUM_LEB128
6962 #undef CHECKSUM_ULEB128
6964 /* Generate the type signature for DIE. This is computed by generating an
6965 MD5 checksum over the DIE's tag, its relevant attributes, and its
6966 children. Attributes that are references to other DIEs are processed
6967 by recursion, using the MARK field to prevent infinite recursion.
6968 If the DIE is nested inside a namespace or another type, we also
6969 need to include that context in the signature. The lower 64 bits
6970 of the resulting MD5 checksum comprise the signature. */
6972 static void
6973 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6975 int mark;
6976 const char *name;
6977 unsigned char checksum[16];
6978 struct md5_ctx ctx;
6979 dw_die_ref decl;
6980 dw_die_ref parent;
6982 name = get_AT_string (die, DW_AT_name);
6983 decl = get_AT_ref (die, DW_AT_specification);
6984 parent = get_die_parent (die);
6986 /* First, compute a signature for just the type name (and its surrounding
6987 context, if any. This is stored in the type unit DIE for link-time
6988 ODR (one-definition rule) checking. */
6990 if (is_cxx () && name != NULL)
6992 md5_init_ctx (&ctx);
6994 /* Checksum the names of surrounding namespaces and structures. */
6995 if (parent != NULL)
6996 checksum_die_context (parent, &ctx);
6998 /* Checksum the current DIE. */
6999 die_odr_checksum (die->die_tag, name, &ctx);
7000 md5_finish_ctx (&ctx, checksum);
7002 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
7005 /* Next, compute the complete type signature. */
7007 md5_init_ctx (&ctx);
7008 mark = 1;
7009 die->die_mark = mark;
7011 /* Checksum the names of surrounding namespaces and structures. */
7012 if (parent != NULL)
7013 checksum_die_context (parent, &ctx);
7015 /* Checksum the DIE and its children. */
7016 die_checksum_ordered (die, &ctx, &mark);
7017 unmark_all_dies (die);
7018 md5_finish_ctx (&ctx, checksum);
7020 /* Store the signature in the type node and link the type DIE and the
7021 type node together. */
7022 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
7023 DWARF_TYPE_SIGNATURE_SIZE);
7024 die->comdat_type_p = true;
7025 die->die_id.die_type_node = type_node;
7026 type_node->type_die = die;
7028 /* If the DIE is a specification, link its declaration to the type node
7029 as well. */
7030 if (decl != NULL)
7032 decl->comdat_type_p = true;
7033 decl->die_id.die_type_node = type_node;
7037 /* Do the location expressions look same? */
7038 static inline int
7039 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7041 return loc1->dw_loc_opc == loc2->dw_loc_opc
7042 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7043 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7046 /* Do the values look the same? */
7047 static int
7048 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7050 dw_loc_descr_ref loc1, loc2;
7051 rtx r1, r2;
7053 if (v1->val_class != v2->val_class)
7054 return 0;
7056 switch (v1->val_class)
7058 case dw_val_class_const:
7059 case dw_val_class_const_implicit:
7060 return v1->v.val_int == v2->v.val_int;
7061 case dw_val_class_unsigned_const:
7062 case dw_val_class_unsigned_const_implicit:
7063 return v1->v.val_unsigned == v2->v.val_unsigned;
7064 case dw_val_class_const_double:
7065 return v1->v.val_double.high == v2->v.val_double.high
7066 && v1->v.val_double.low == v2->v.val_double.low;
7067 case dw_val_class_wide_int:
7068 return *v1->v.val_wide == *v2->v.val_wide;
7069 case dw_val_class_vec:
7070 if (v1->v.val_vec.length != v2->v.val_vec.length
7071 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7072 return 0;
7073 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7074 v1->v.val_vec.length * v1->v.val_vec.elt_size))
7075 return 0;
7076 return 1;
7077 case dw_val_class_flag:
7078 return v1->v.val_flag == v2->v.val_flag;
7079 case dw_val_class_str:
7080 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
7082 case dw_val_class_addr:
7083 r1 = v1->v.val_addr;
7084 r2 = v2->v.val_addr;
7085 if (GET_CODE (r1) != GET_CODE (r2))
7086 return 0;
7087 return !rtx_equal_p (r1, r2);
7089 case dw_val_class_offset:
7090 return v1->v.val_offset == v2->v.val_offset;
7092 case dw_val_class_loc:
7093 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7094 loc1 && loc2;
7095 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7096 if (!same_loc_p (loc1, loc2, mark))
7097 return 0;
7098 return !loc1 && !loc2;
7100 case dw_val_class_die_ref:
7101 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7103 case dw_val_class_fde_ref:
7104 case dw_val_class_vms_delta:
7105 case dw_val_class_lbl_id:
7106 case dw_val_class_lineptr:
7107 case dw_val_class_macptr:
7108 case dw_val_class_loclistsptr:
7109 case dw_val_class_high_pc:
7110 return 1;
7112 case dw_val_class_file:
7113 case dw_val_class_file_implicit:
7114 return v1->v.val_file == v2->v.val_file;
7116 case dw_val_class_data8:
7117 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
7119 default:
7120 return 1;
7124 /* Do the attributes look the same? */
7126 static int
7127 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
7129 if (at1->dw_attr != at2->dw_attr)
7130 return 0;
7132 /* We don't care that this was compiled with a different compiler
7133 snapshot; if the output is the same, that's what matters. */
7134 if (at1->dw_attr == DW_AT_producer)
7135 return 1;
7137 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7140 /* Do the dies look the same? */
7142 static int
7143 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7145 dw_die_ref c1, c2;
7146 dw_attr_node *a1;
7147 unsigned ix;
7149 /* To avoid infinite recursion. */
7150 if (die1->die_mark)
7151 return die1->die_mark == die2->die_mark;
7152 die1->die_mark = die2->die_mark = ++(*mark);
7154 if (die1->die_tag != die2->die_tag)
7155 return 0;
7157 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
7158 return 0;
7160 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
7161 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
7162 return 0;
7164 c1 = die1->die_child;
7165 c2 = die2->die_child;
7166 if (! c1)
7168 if (c2)
7169 return 0;
7171 else
7172 for (;;)
7174 if (!same_die_p (c1, c2, mark))
7175 return 0;
7176 c1 = c1->die_sib;
7177 c2 = c2->die_sib;
7178 if (c1 == die1->die_child)
7180 if (c2 == die2->die_child)
7181 break;
7182 else
7183 return 0;
7187 return 1;
7190 /* Do the dies look the same? Wrapper around same_die_p. */
7192 static int
7193 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
7195 int mark = 0;
7196 int ret = same_die_p (die1, die2, &mark);
7198 unmark_all_dies (die1);
7199 unmark_all_dies (die2);
7201 return ret;
7204 /* The prefix to attach to symbols on DIEs in the current comdat debug
7205 info section. */
7206 static const char *comdat_symbol_id;
7208 /* The index of the current symbol within the current comdat CU. */
7209 static unsigned int comdat_symbol_number;
7211 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7212 children, and set comdat_symbol_id accordingly. */
7214 static void
7215 compute_section_prefix (dw_die_ref unit_die)
7217 const char *die_name = get_AT_string (unit_die, DW_AT_name);
7218 const char *base = die_name ? lbasename (die_name) : "anonymous";
7219 char *name = XALLOCAVEC (char, strlen (base) + 64);
7220 char *p;
7221 int i, mark;
7222 unsigned char checksum[16];
7223 struct md5_ctx ctx;
7225 /* Compute the checksum of the DIE, then append part of it as hex digits to
7226 the name filename of the unit. */
7228 md5_init_ctx (&ctx);
7229 mark = 0;
7230 die_checksum (unit_die, &ctx, &mark);
7231 unmark_all_dies (unit_die);
7232 md5_finish_ctx (&ctx, checksum);
7234 sprintf (name, "%s.", base);
7235 clean_symbol_name (name);
7237 p = name + strlen (name);
7238 for (i = 0; i < 4; i++)
7240 sprintf (p, "%.2x", checksum[i]);
7241 p += 2;
7244 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
7245 comdat_symbol_number = 0;
7248 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
7250 static int
7251 is_type_die (dw_die_ref die)
7253 switch (die->die_tag)
7255 case DW_TAG_array_type:
7256 case DW_TAG_class_type:
7257 case DW_TAG_interface_type:
7258 case DW_TAG_enumeration_type:
7259 case DW_TAG_pointer_type:
7260 case DW_TAG_reference_type:
7261 case DW_TAG_rvalue_reference_type:
7262 case DW_TAG_string_type:
7263 case DW_TAG_structure_type:
7264 case DW_TAG_subroutine_type:
7265 case DW_TAG_union_type:
7266 case DW_TAG_ptr_to_member_type:
7267 case DW_TAG_set_type:
7268 case DW_TAG_subrange_type:
7269 case DW_TAG_base_type:
7270 case DW_TAG_const_type:
7271 case DW_TAG_file_type:
7272 case DW_TAG_packed_type:
7273 case DW_TAG_volatile_type:
7274 case DW_TAG_typedef:
7275 return 1;
7276 default:
7277 return 0;
7281 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7282 Basically, we want to choose the bits that are likely to be shared between
7283 compilations (types) and leave out the bits that are specific to individual
7284 compilations (functions). */
7286 static int
7287 is_comdat_die (dw_die_ref c)
7289 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7290 we do for stabs. The advantage is a greater likelihood of sharing between
7291 objects that don't include headers in the same order (and therefore would
7292 put the base types in a different comdat). jason 8/28/00 */
7294 if (c->die_tag == DW_TAG_base_type)
7295 return 0;
7297 if (c->die_tag == DW_TAG_pointer_type
7298 || c->die_tag == DW_TAG_reference_type
7299 || c->die_tag == DW_TAG_rvalue_reference_type
7300 || c->die_tag == DW_TAG_const_type
7301 || c->die_tag == DW_TAG_volatile_type)
7303 dw_die_ref t = get_AT_ref (c, DW_AT_type);
7305 return t ? is_comdat_die (t) : 0;
7308 return is_type_die (c);
7311 /* Returns 1 iff C is the sort of DIE that might be referred to from another
7312 compilation unit. */
7314 static int
7315 is_symbol_die (dw_die_ref c)
7317 return (is_type_die (c)
7318 || is_declaration_die (c)
7319 || c->die_tag == DW_TAG_namespace
7320 || c->die_tag == DW_TAG_module);
7323 /* Returns true iff C is a compile-unit DIE. */
7325 static inline bool
7326 is_cu_die (dw_die_ref c)
7328 return c && (c->die_tag == DW_TAG_compile_unit
7329 || c->die_tag == DW_TAG_skeleton_unit);
7332 /* Returns true iff C is a unit DIE of some sort. */
7334 static inline bool
7335 is_unit_die (dw_die_ref c)
7337 return c && (c->die_tag == DW_TAG_compile_unit
7338 || c->die_tag == DW_TAG_partial_unit
7339 || c->die_tag == DW_TAG_type_unit
7340 || c->die_tag == DW_TAG_skeleton_unit);
7343 /* Returns true iff C is a namespace DIE. */
7345 static inline bool
7346 is_namespace_die (dw_die_ref c)
7348 return c && c->die_tag == DW_TAG_namespace;
7351 /* Returns true iff C is a class or structure DIE. */
7353 static inline bool
7354 is_class_die (dw_die_ref c)
7356 return c && (c->die_tag == DW_TAG_class_type
7357 || c->die_tag == DW_TAG_structure_type);
7360 /* Return non-zero if this DIE is a template parameter. */
7362 static inline bool
7363 is_template_parameter (dw_die_ref die)
7365 switch (die->die_tag)
7367 case DW_TAG_template_type_param:
7368 case DW_TAG_template_value_param:
7369 case DW_TAG_GNU_template_template_param:
7370 case DW_TAG_GNU_template_parameter_pack:
7371 return true;
7372 default:
7373 return false;
7377 /* Return non-zero if this DIE represents a template instantiation. */
7379 static inline bool
7380 is_template_instantiation (dw_die_ref die)
7382 dw_die_ref c;
7384 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
7385 return false;
7386 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
7387 return false;
7390 static char *
7391 gen_internal_sym (const char *prefix)
7393 char buf[MAX_ARTIFICIAL_LABEL_BYTES];
7395 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7396 return xstrdup (buf);
7399 /* Assign symbols to all worthy DIEs under DIE. */
7401 static void
7402 assign_symbol_names (dw_die_ref die)
7404 dw_die_ref c;
7406 if (is_symbol_die (die) && !die->comdat_type_p)
7408 if (comdat_symbol_id)
7410 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
7412 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
7413 comdat_symbol_id, comdat_symbol_number++);
7414 die->die_id.die_symbol = xstrdup (p);
7416 else
7417 die->die_id.die_symbol = gen_internal_sym ("LDIE");
7420 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
7423 struct cu_hash_table_entry
7425 dw_die_ref cu;
7426 unsigned min_comdat_num, max_comdat_num;
7427 struct cu_hash_table_entry *next;
7430 /* Helpers to manipulate hash table of CUs. */
7432 struct cu_hash_table_entry_hasher : pointer_hash <cu_hash_table_entry>
7434 typedef die_struct *compare_type;
7435 static inline hashval_t hash (const cu_hash_table_entry *);
7436 static inline bool equal (const cu_hash_table_entry *, const die_struct *);
7437 static inline void remove (cu_hash_table_entry *);
7440 inline hashval_t
7441 cu_hash_table_entry_hasher::hash (const cu_hash_table_entry *entry)
7443 return htab_hash_string (entry->cu->die_id.die_symbol);
7446 inline bool
7447 cu_hash_table_entry_hasher::equal (const cu_hash_table_entry *entry1,
7448 const die_struct *entry2)
7450 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
7453 inline void
7454 cu_hash_table_entry_hasher::remove (cu_hash_table_entry *entry)
7456 struct cu_hash_table_entry *next;
7458 while (entry)
7460 next = entry->next;
7461 free (entry);
7462 entry = next;
7466 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
7468 /* Check whether we have already seen this CU and set up SYM_NUM
7469 accordingly. */
7470 static int
7471 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
7473 struct cu_hash_table_entry dummy;
7474 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7476 dummy.max_comdat_num = 0;
7478 slot = htable->find_slot_with_hash (cu,
7479 htab_hash_string (cu->die_id.die_symbol),
7480 INSERT);
7481 entry = *slot;
7483 for (; entry; last = entry, entry = entry->next)
7485 if (same_die_p_wrap (cu, entry->cu))
7486 break;
7489 if (entry)
7491 *sym_num = entry->min_comdat_num;
7492 return 1;
7495 entry = XCNEW (struct cu_hash_table_entry);
7496 entry->cu = cu;
7497 entry->min_comdat_num = *sym_num = last->max_comdat_num;
7498 entry->next = *slot;
7499 *slot = entry;
7501 return 0;
7504 /* Record SYM_NUM to record of CU in HTABLE. */
7505 static void
7506 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
7507 unsigned int sym_num)
7509 struct cu_hash_table_entry **slot, *entry;
7511 slot = htable->find_slot_with_hash (cu,
7512 htab_hash_string (cu->die_id.die_symbol),
7513 NO_INSERT);
7514 entry = *slot;
7516 entry->max_comdat_num = sym_num;
7519 /* Traverse the DIE (which is always comp_unit_die), and set up
7520 additional compilation units for each of the include files we see
7521 bracketed by BINCL/EINCL. */
7523 static void
7524 break_out_includes (dw_die_ref die)
7526 dw_die_ref c;
7527 dw_die_ref unit = NULL;
7528 limbo_die_node *node, **pnode;
7530 c = die->die_child;
7531 if (c) do {
7532 dw_die_ref prev = c;
7533 c = c->die_sib;
7534 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7535 || (unit && is_comdat_die (c)))
7537 dw_die_ref next = c->die_sib;
7539 /* This DIE is for a secondary CU; remove it from the main one. */
7540 remove_child_with_prev (c, prev);
7542 if (c->die_tag == DW_TAG_GNU_BINCL)
7543 unit = push_new_compile_unit (unit, c);
7544 else if (c->die_tag == DW_TAG_GNU_EINCL)
7545 unit = pop_compile_unit (unit);
7546 else
7547 add_child_die (unit, c);
7548 c = next;
7549 if (c == die->die_child)
7550 break;
7552 } while (c != die->die_child);
7554 #if 0
7555 /* We can only use this in debugging, since the frontend doesn't check
7556 to make sure that we leave every include file we enter. */
7557 gcc_assert (!unit);
7558 #endif
7560 assign_symbol_names (die);
7561 cu_hash_type cu_hash_table (10);
7562 for (node = limbo_die_list, pnode = &limbo_die_list;
7563 node;
7564 node = node->next)
7566 int is_dupl;
7568 compute_section_prefix (node->die);
7569 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
7570 &comdat_symbol_number);
7571 assign_symbol_names (node->die);
7572 if (is_dupl)
7573 *pnode = node->next;
7574 else
7576 pnode = &node->next;
7577 record_comdat_symbol_number (node->die, &cu_hash_table,
7578 comdat_symbol_number);
7583 /* Return non-zero if this DIE is a declaration. */
7585 static int
7586 is_declaration_die (dw_die_ref die)
7588 dw_attr_node *a;
7589 unsigned ix;
7591 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7592 if (a->dw_attr == DW_AT_declaration)
7593 return 1;
7595 return 0;
7598 /* Return non-zero if this DIE is nested inside a subprogram. */
7600 static int
7601 is_nested_in_subprogram (dw_die_ref die)
7603 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7605 if (decl == NULL)
7606 decl = die;
7607 return local_scope_p (decl);
7610 /* Return non-zero if this DIE contains a defining declaration of a
7611 subprogram. */
7613 static int
7614 contains_subprogram_definition (dw_die_ref die)
7616 dw_die_ref c;
7618 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7619 return 1;
7620 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7621 return 0;
7624 /* Return non-zero if this is a type DIE that should be moved to a
7625 COMDAT .debug_types section or .debug_info section with DW_UT_*type
7626 unit type. */
7628 static int
7629 should_move_die_to_comdat (dw_die_ref die)
7631 switch (die->die_tag)
7633 case DW_TAG_class_type:
7634 case DW_TAG_structure_type:
7635 case DW_TAG_enumeration_type:
7636 case DW_TAG_union_type:
7637 /* Don't move declarations, inlined instances, types nested in a
7638 subprogram, or types that contain subprogram definitions. */
7639 if (is_declaration_die (die)
7640 || get_AT (die, DW_AT_abstract_origin)
7641 || is_nested_in_subprogram (die)
7642 || contains_subprogram_definition (die))
7643 return 0;
7644 return 1;
7645 case DW_TAG_array_type:
7646 case DW_TAG_interface_type:
7647 case DW_TAG_pointer_type:
7648 case DW_TAG_reference_type:
7649 case DW_TAG_rvalue_reference_type:
7650 case DW_TAG_string_type:
7651 case DW_TAG_subroutine_type:
7652 case DW_TAG_ptr_to_member_type:
7653 case DW_TAG_set_type:
7654 case DW_TAG_subrange_type:
7655 case DW_TAG_base_type:
7656 case DW_TAG_const_type:
7657 case DW_TAG_file_type:
7658 case DW_TAG_packed_type:
7659 case DW_TAG_volatile_type:
7660 case DW_TAG_typedef:
7661 default:
7662 return 0;
7666 /* Make a clone of DIE. */
7668 static dw_die_ref
7669 clone_die (dw_die_ref die)
7671 dw_die_ref clone;
7672 dw_attr_node *a;
7673 unsigned ix;
7675 clone = ggc_cleared_alloc<die_node> ();
7676 clone->die_tag = die->die_tag;
7678 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7679 add_dwarf_attr (clone, a);
7681 return clone;
7684 /* Make a clone of the tree rooted at DIE. */
7686 static dw_die_ref
7687 clone_tree (dw_die_ref die)
7689 dw_die_ref c;
7690 dw_die_ref clone = clone_die (die);
7692 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7694 return clone;
7697 /* Make a clone of DIE as a declaration. */
7699 static dw_die_ref
7700 clone_as_declaration (dw_die_ref die)
7702 dw_die_ref clone;
7703 dw_die_ref decl;
7704 dw_attr_node *a;
7705 unsigned ix;
7707 /* If the DIE is already a declaration, just clone it. */
7708 if (is_declaration_die (die))
7709 return clone_die (die);
7711 /* If the DIE is a specification, just clone its declaration DIE. */
7712 decl = get_AT_ref (die, DW_AT_specification);
7713 if (decl != NULL)
7715 clone = clone_die (decl);
7716 if (die->comdat_type_p)
7717 add_AT_die_ref (clone, DW_AT_signature, die);
7718 return clone;
7721 clone = ggc_cleared_alloc<die_node> ();
7722 clone->die_tag = die->die_tag;
7724 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7726 /* We don't want to copy over all attributes.
7727 For example we don't want DW_AT_byte_size because otherwise we will no
7728 longer have a declaration and GDB will treat it as a definition. */
7730 switch (a->dw_attr)
7732 case DW_AT_abstract_origin:
7733 case DW_AT_artificial:
7734 case DW_AT_containing_type:
7735 case DW_AT_external:
7736 case DW_AT_name:
7737 case DW_AT_type:
7738 case DW_AT_virtuality:
7739 case DW_AT_linkage_name:
7740 case DW_AT_MIPS_linkage_name:
7741 add_dwarf_attr (clone, a);
7742 break;
7743 case DW_AT_byte_size:
7744 case DW_AT_alignment:
7745 default:
7746 break;
7750 if (die->comdat_type_p)
7751 add_AT_die_ref (clone, DW_AT_signature, die);
7753 add_AT_flag (clone, DW_AT_declaration, 1);
7754 return clone;
7758 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7760 struct decl_table_entry
7762 dw_die_ref orig;
7763 dw_die_ref copy;
7766 /* Helpers to manipulate hash table of copied declarations. */
7768 /* Hashtable helpers. */
7770 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
7772 typedef die_struct *compare_type;
7773 static inline hashval_t hash (const decl_table_entry *);
7774 static inline bool equal (const decl_table_entry *, const die_struct *);
7777 inline hashval_t
7778 decl_table_entry_hasher::hash (const decl_table_entry *entry)
7780 return htab_hash_pointer (entry->orig);
7783 inline bool
7784 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7785 const die_struct *entry2)
7787 return entry1->orig == entry2;
7790 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7792 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7793 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7794 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7795 to check if the ancestor has already been copied into UNIT. */
7797 static dw_die_ref
7798 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7799 decl_hash_type *decl_table)
7801 dw_die_ref parent = die->die_parent;
7802 dw_die_ref new_parent = unit;
7803 dw_die_ref copy;
7804 decl_table_entry **slot = NULL;
7805 struct decl_table_entry *entry = NULL;
7807 if (decl_table)
7809 /* Check if the entry has already been copied to UNIT. */
7810 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7811 INSERT);
7812 if (*slot != HTAB_EMPTY_ENTRY)
7814 entry = *slot;
7815 return entry->copy;
7818 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7819 entry = XCNEW (struct decl_table_entry);
7820 entry->orig = die;
7821 entry->copy = NULL;
7822 *slot = entry;
7825 if (parent != NULL)
7827 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7828 if (spec != NULL)
7829 parent = spec;
7830 if (!is_unit_die (parent))
7831 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7834 copy = clone_as_declaration (die);
7835 add_child_die (new_parent, copy);
7837 if (decl_table)
7839 /* Record the pointer to the copy. */
7840 entry->copy = copy;
7843 return copy;
7845 /* Copy the declaration context to the new type unit DIE. This includes
7846 any surrounding namespace or type declarations. If the DIE has an
7847 AT_specification attribute, it also includes attributes and children
7848 attached to the specification, and returns a pointer to the original
7849 parent of the declaration DIE. Returns NULL otherwise. */
7851 static dw_die_ref
7852 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7854 dw_die_ref decl;
7855 dw_die_ref new_decl;
7856 dw_die_ref orig_parent = NULL;
7858 decl = get_AT_ref (die, DW_AT_specification);
7859 if (decl == NULL)
7860 decl = die;
7861 else
7863 unsigned ix;
7864 dw_die_ref c;
7865 dw_attr_node *a;
7867 /* The original DIE will be changed to a declaration, and must
7868 be moved to be a child of the original declaration DIE. */
7869 orig_parent = decl->die_parent;
7871 /* Copy the type node pointer from the new DIE to the original
7872 declaration DIE so we can forward references later. */
7873 decl->comdat_type_p = true;
7874 decl->die_id.die_type_node = die->die_id.die_type_node;
7876 remove_AT (die, DW_AT_specification);
7878 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7880 if (a->dw_attr != DW_AT_name
7881 && a->dw_attr != DW_AT_declaration
7882 && a->dw_attr != DW_AT_external)
7883 add_dwarf_attr (die, a);
7886 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7889 if (decl->die_parent != NULL
7890 && !is_unit_die (decl->die_parent))
7892 new_decl = copy_ancestor_tree (unit, decl, NULL);
7893 if (new_decl != NULL)
7895 remove_AT (new_decl, DW_AT_signature);
7896 add_AT_specification (die, new_decl);
7900 return orig_parent;
7903 /* Generate the skeleton ancestor tree for the given NODE, then clone
7904 the DIE and add the clone into the tree. */
7906 static void
7907 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7909 if (node->new_die != NULL)
7910 return;
7912 node->new_die = clone_as_declaration (node->old_die);
7914 if (node->parent != NULL)
7916 generate_skeleton_ancestor_tree (node->parent);
7917 add_child_die (node->parent->new_die, node->new_die);
7921 /* Generate a skeleton tree of DIEs containing any declarations that are
7922 found in the original tree. We traverse the tree looking for declaration
7923 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7925 static void
7926 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7928 skeleton_chain_node node;
7929 dw_die_ref c;
7930 dw_die_ref first;
7931 dw_die_ref prev = NULL;
7932 dw_die_ref next = NULL;
7934 node.parent = parent;
7936 first = c = parent->old_die->die_child;
7937 if (c)
7938 next = c->die_sib;
7939 if (c) do {
7940 if (prev == NULL || prev->die_sib == c)
7941 prev = c;
7942 c = next;
7943 next = (c == first ? NULL : c->die_sib);
7944 node.old_die = c;
7945 node.new_die = NULL;
7946 if (is_declaration_die (c))
7948 if (is_template_instantiation (c))
7950 /* Instantiated templates do not need to be cloned into the
7951 type unit. Just move the DIE and its children back to
7952 the skeleton tree (in the main CU). */
7953 remove_child_with_prev (c, prev);
7954 add_child_die (parent->new_die, c);
7955 c = prev;
7957 else if (c->comdat_type_p)
7959 /* This is the skeleton of earlier break_out_comdat_types
7960 type. Clone the existing DIE, but keep the children
7961 under the original (which is in the main CU). */
7962 dw_die_ref clone = clone_die (c);
7964 replace_child (c, clone, prev);
7965 generate_skeleton_ancestor_tree (parent);
7966 add_child_die (parent->new_die, c);
7967 c = clone;
7968 continue;
7970 else
7972 /* Clone the existing DIE, move the original to the skeleton
7973 tree (which is in the main CU), and put the clone, with
7974 all the original's children, where the original came from
7975 (which is about to be moved to the type unit). */
7976 dw_die_ref clone = clone_die (c);
7977 move_all_children (c, clone);
7979 /* If the original has a DW_AT_object_pointer attribute,
7980 it would now point to a child DIE just moved to the
7981 cloned tree, so we need to remove that attribute from
7982 the original. */
7983 remove_AT (c, DW_AT_object_pointer);
7985 replace_child (c, clone, prev);
7986 generate_skeleton_ancestor_tree (parent);
7987 add_child_die (parent->new_die, c);
7988 node.old_die = clone;
7989 node.new_die = c;
7990 c = clone;
7993 generate_skeleton_bottom_up (&node);
7994 } while (next != NULL);
7997 /* Wrapper function for generate_skeleton_bottom_up. */
7999 static dw_die_ref
8000 generate_skeleton (dw_die_ref die)
8002 skeleton_chain_node node;
8004 node.old_die = die;
8005 node.new_die = NULL;
8006 node.parent = NULL;
8008 /* If this type definition is nested inside another type,
8009 and is not an instantiation of a template, always leave
8010 at least a declaration in its place. */
8011 if (die->die_parent != NULL
8012 && is_type_die (die->die_parent)
8013 && !is_template_instantiation (die))
8014 node.new_die = clone_as_declaration (die);
8016 generate_skeleton_bottom_up (&node);
8017 return node.new_die;
8020 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
8021 declaration. The original DIE is moved to a new compile unit so that
8022 existing references to it follow it to the new location. If any of the
8023 original DIE's descendants is a declaration, we need to replace the
8024 original DIE with a skeleton tree and move the declarations back into the
8025 skeleton tree. */
8027 static dw_die_ref
8028 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
8029 dw_die_ref prev)
8031 dw_die_ref skeleton, orig_parent;
8033 /* Copy the declaration context to the type unit DIE. If the returned
8034 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
8035 that DIE. */
8036 orig_parent = copy_declaration_context (unit, child);
8038 skeleton = generate_skeleton (child);
8039 if (skeleton == NULL)
8040 remove_child_with_prev (child, prev);
8041 else
8043 skeleton->comdat_type_p = true;
8044 skeleton->die_id.die_type_node = child->die_id.die_type_node;
8046 /* If the original DIE was a specification, we need to put
8047 the skeleton under the parent DIE of the declaration.
8048 This leaves the original declaration in the tree, but
8049 it will be pruned later since there are no longer any
8050 references to it. */
8051 if (orig_parent != NULL)
8053 remove_child_with_prev (child, prev);
8054 add_child_die (orig_parent, skeleton);
8056 else
8057 replace_child (child, skeleton, prev);
8060 return skeleton;
8063 static void
8064 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8065 comdat_type_node *type_node,
8066 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
8068 /* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
8069 procedure, put it under TYPE_NODE and return the copy. Continue looking for
8070 DWARF procedure references in the DW_AT_location attribute. */
8072 static dw_die_ref
8073 copy_dwarf_procedure (dw_die_ref die,
8074 comdat_type_node *type_node,
8075 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8077 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
8079 /* DWARF procedures are not supposed to have children... */
8080 gcc_assert (die->die_child == NULL);
8082 /* ... and they are supposed to have only one attribute: DW_AT_location. */
8083 gcc_assert (vec_safe_length (die->die_attr) == 1
8084 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
8086 /* Do not copy more than once DWARF procedures. */
8087 bool existed;
8088 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
8089 if (existed)
8090 return die_copy;
8092 die_copy = clone_die (die);
8093 add_child_die (type_node->root_die, die_copy);
8094 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
8095 return die_copy;
8098 /* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
8099 procedures in DIE's attributes. */
8101 static void
8102 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8103 comdat_type_node *type_node,
8104 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8106 dw_attr_node *a;
8107 unsigned i;
8109 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
8111 dw_loc_descr_ref loc;
8113 if (a->dw_attr_val.val_class != dw_val_class_loc)
8114 continue;
8116 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
8118 switch (loc->dw_loc_opc)
8120 case DW_OP_call2:
8121 case DW_OP_call4:
8122 case DW_OP_call_ref:
8123 gcc_assert (loc->dw_loc_oprnd1.val_class
8124 == dw_val_class_die_ref);
8125 loc->dw_loc_oprnd1.v.val_die_ref.die
8126 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
8127 type_node,
8128 copied_dwarf_procs);
8130 default:
8131 break;
8137 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
8138 rewrite references to point to the copies.
8140 References are looked for in DIE's attributes and recursively in all its
8141 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
8142 mapping from old DWARF procedures to their copy. It is used not to copy
8143 twice the same DWARF procedure under TYPE_NODE. */
8145 static void
8146 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
8147 comdat_type_node *type_node,
8148 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8150 dw_die_ref c;
8152 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
8153 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
8154 type_node,
8155 copied_dwarf_procs));
8158 /* Traverse the DIE and set up additional .debug_types or .debug_info
8159 DW_UT_*type sections for each type worthy of being placed in a COMDAT
8160 section. */
8162 static void
8163 break_out_comdat_types (dw_die_ref die)
8165 dw_die_ref c;
8166 dw_die_ref first;
8167 dw_die_ref prev = NULL;
8168 dw_die_ref next = NULL;
8169 dw_die_ref unit = NULL;
8171 first = c = die->die_child;
8172 if (c)
8173 next = c->die_sib;
8174 if (c) do {
8175 if (prev == NULL || prev->die_sib == c)
8176 prev = c;
8177 c = next;
8178 next = (c == first ? NULL : c->die_sib);
8179 if (should_move_die_to_comdat (c))
8181 dw_die_ref replacement;
8182 comdat_type_node *type_node;
8184 /* Break out nested types into their own type units. */
8185 break_out_comdat_types (c);
8187 /* Create a new type unit DIE as the root for the new tree, and
8188 add it to the list of comdat types. */
8189 unit = new_die (DW_TAG_type_unit, NULL, NULL);
8190 add_AT_unsigned (unit, DW_AT_language,
8191 get_AT_unsigned (comp_unit_die (), DW_AT_language));
8192 type_node = ggc_cleared_alloc<comdat_type_node> ();
8193 type_node->root_die = unit;
8194 type_node->next = comdat_type_list;
8195 comdat_type_list = type_node;
8197 /* Generate the type signature. */
8198 generate_type_signature (c, type_node);
8200 /* Copy the declaration context, attributes, and children of the
8201 declaration into the new type unit DIE, then remove this DIE
8202 from the main CU (or replace it with a skeleton if necessary). */
8203 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
8204 type_node->skeleton_die = replacement;
8206 /* Add the DIE to the new compunit. */
8207 add_child_die (unit, c);
8209 /* Types can reference DWARF procedures for type size or data location
8210 expressions. Calls in DWARF expressions cannot target procedures
8211 that are not in the same section. So we must copy DWARF procedures
8212 along with this type and then rewrite references to them. */
8213 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
8214 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
8216 if (replacement != NULL)
8217 c = replacement;
8219 else if (c->die_tag == DW_TAG_namespace
8220 || c->die_tag == DW_TAG_class_type
8221 || c->die_tag == DW_TAG_structure_type
8222 || c->die_tag == DW_TAG_union_type)
8224 /* Look for nested types that can be broken out. */
8225 break_out_comdat_types (c);
8227 } while (next != NULL);
8230 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
8231 Enter all the cloned children into the hash table decl_table. */
8233 static dw_die_ref
8234 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
8236 dw_die_ref c;
8237 dw_die_ref clone;
8238 struct decl_table_entry *entry;
8239 decl_table_entry **slot;
8241 if (die->die_tag == DW_TAG_subprogram)
8242 clone = clone_as_declaration (die);
8243 else
8244 clone = clone_die (die);
8246 slot = decl_table->find_slot_with_hash (die,
8247 htab_hash_pointer (die), INSERT);
8249 /* Assert that DIE isn't in the hash table yet. If it would be there
8250 before, the ancestors would be necessarily there as well, therefore
8251 clone_tree_partial wouldn't be called. */
8252 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8254 entry = XCNEW (struct decl_table_entry);
8255 entry->orig = die;
8256 entry->copy = clone;
8257 *slot = entry;
8259 if (die->die_tag != DW_TAG_subprogram)
8260 FOR_EACH_CHILD (die, c,
8261 add_child_die (clone, clone_tree_partial (c, decl_table)));
8263 return clone;
8266 /* Walk the DIE and its children, looking for references to incomplete
8267 or trivial types that are unmarked (i.e., that are not in the current
8268 type_unit). */
8270 static void
8271 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
8273 dw_die_ref c;
8274 dw_attr_node *a;
8275 unsigned ix;
8277 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8279 if (AT_class (a) == dw_val_class_die_ref)
8281 dw_die_ref targ = AT_ref (a);
8282 decl_table_entry **slot;
8283 struct decl_table_entry *entry;
8285 if (targ->die_mark != 0 || targ->comdat_type_p)
8286 continue;
8288 slot = decl_table->find_slot_with_hash (targ,
8289 htab_hash_pointer (targ),
8290 INSERT);
8292 if (*slot != HTAB_EMPTY_ENTRY)
8294 /* TARG has already been copied, so we just need to
8295 modify the reference to point to the copy. */
8296 entry = *slot;
8297 a->dw_attr_val.v.val_die_ref.die = entry->copy;
8299 else
8301 dw_die_ref parent = unit;
8302 dw_die_ref copy = clone_die (targ);
8304 /* Record in DECL_TABLE that TARG has been copied.
8305 Need to do this now, before the recursive call,
8306 because DECL_TABLE may be expanded and SLOT
8307 would no longer be a valid pointer. */
8308 entry = XCNEW (struct decl_table_entry);
8309 entry->orig = targ;
8310 entry->copy = copy;
8311 *slot = entry;
8313 /* If TARG is not a declaration DIE, we need to copy its
8314 children. */
8315 if (!is_declaration_die (targ))
8317 FOR_EACH_CHILD (
8318 targ, c,
8319 add_child_die (copy,
8320 clone_tree_partial (c, decl_table)));
8323 /* Make sure the cloned tree is marked as part of the
8324 type unit. */
8325 mark_dies (copy);
8327 /* If TARG has surrounding context, copy its ancestor tree
8328 into the new type unit. */
8329 if (targ->die_parent != NULL
8330 && !is_unit_die (targ->die_parent))
8331 parent = copy_ancestor_tree (unit, targ->die_parent,
8332 decl_table);
8334 add_child_die (parent, copy);
8335 a->dw_attr_val.v.val_die_ref.die = copy;
8337 /* Make sure the newly-copied DIE is walked. If it was
8338 installed in a previously-added context, it won't
8339 get visited otherwise. */
8340 if (parent != unit)
8342 /* Find the highest point of the newly-added tree,
8343 mark each node along the way, and walk from there. */
8344 parent->die_mark = 1;
8345 while (parent->die_parent
8346 && parent->die_parent->die_mark == 0)
8348 parent = parent->die_parent;
8349 parent->die_mark = 1;
8351 copy_decls_walk (unit, parent, decl_table);
8357 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8360 /* Copy declarations for "unworthy" types into the new comdat section.
8361 Incomplete types, modified types, and certain other types aren't broken
8362 out into comdat sections of their own, so they don't have a signature,
8363 and we need to copy the declaration into the same section so that we
8364 don't have an external reference. */
8366 static void
8367 copy_decls_for_unworthy_types (dw_die_ref unit)
8369 mark_dies (unit);
8370 decl_hash_type decl_table (10);
8371 copy_decls_walk (unit, unit, &decl_table);
8372 unmark_dies (unit);
8375 /* Traverse the DIE and add a sibling attribute if it may have the
8376 effect of speeding up access to siblings. To save some space,
8377 avoid generating sibling attributes for DIE's without children. */
8379 static void
8380 add_sibling_attributes (dw_die_ref die)
8382 dw_die_ref c;
8384 if (! die->die_child)
8385 return;
8387 if (die->die_parent && die != die->die_parent->die_child)
8388 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8390 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8393 /* Output all location lists for the DIE and its children. */
8395 static void
8396 output_location_lists (dw_die_ref die)
8398 dw_die_ref c;
8399 dw_attr_node *a;
8400 unsigned ix;
8402 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8403 if (AT_class (a) == dw_val_class_loc_list)
8404 output_loc_list (AT_loc_list (a));
8406 FOR_EACH_CHILD (die, c, output_location_lists (c));
8409 /* During assign_location_list_indexes and output_loclists_offset the
8410 current index, after it the number of assigned indexes (i.e. how
8411 large the .debug_loclists* offset table should be). */
8412 static unsigned int loc_list_idx;
8414 /* Output all location list offsets for the DIE and its children. */
8416 static void
8417 output_loclists_offsets (dw_die_ref die)
8419 dw_die_ref c;
8420 dw_attr_node *a;
8421 unsigned ix;
8423 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8424 if (AT_class (a) == dw_val_class_loc_list)
8426 dw_loc_list_ref l = AT_loc_list (a);
8427 if (l->offset_emitted)
8428 continue;
8429 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l->ll_symbol,
8430 loc_section_label, NULL);
8431 gcc_assert (l->hash == loc_list_idx);
8432 loc_list_idx++;
8433 l->offset_emitted = true;
8436 FOR_EACH_CHILD (die, c, output_loclists_offsets (c));
8439 /* Recursively set indexes of location lists. */
8441 static void
8442 assign_location_list_indexes (dw_die_ref die)
8444 dw_die_ref c;
8445 dw_attr_node *a;
8446 unsigned ix;
8448 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8449 if (AT_class (a) == dw_val_class_loc_list)
8451 dw_loc_list_ref list = AT_loc_list (a);
8452 if (!list->num_assigned)
8454 list->num_assigned = true;
8455 list->hash = loc_list_idx++;
8459 FOR_EACH_CHILD (die, c, assign_location_list_indexes (c));
8462 /* We want to limit the number of external references, because they are
8463 larger than local references: a relocation takes multiple words, and
8464 even a sig8 reference is always eight bytes, whereas a local reference
8465 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
8466 So if we encounter multiple external references to the same type DIE, we
8467 make a local typedef stub for it and redirect all references there.
8469 This is the element of the hash table for keeping track of these
8470 references. */
8472 struct external_ref
8474 dw_die_ref type;
8475 dw_die_ref stub;
8476 unsigned n_refs;
8479 /* Hashtable helpers. */
8481 struct external_ref_hasher : free_ptr_hash <external_ref>
8483 static inline hashval_t hash (const external_ref *);
8484 static inline bool equal (const external_ref *, const external_ref *);
8487 inline hashval_t
8488 external_ref_hasher::hash (const external_ref *r)
8490 dw_die_ref die = r->type;
8491 hashval_t h = 0;
8493 /* We can't use the address of the DIE for hashing, because
8494 that will make the order of the stub DIEs non-deterministic. */
8495 if (! die->comdat_type_p)
8496 /* We have a symbol; use it to compute a hash. */
8497 h = htab_hash_string (die->die_id.die_symbol);
8498 else
8500 /* We have a type signature; use a subset of the bits as the hash.
8501 The 8-byte signature is at least as large as hashval_t. */
8502 comdat_type_node *type_node = die->die_id.die_type_node;
8503 memcpy (&h, type_node->signature, sizeof (h));
8505 return h;
8508 inline bool
8509 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
8511 return r1->type == r2->type;
8514 typedef hash_table<external_ref_hasher> external_ref_hash_type;
8516 /* Return a pointer to the external_ref for references to DIE. */
8518 static struct external_ref *
8519 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
8521 struct external_ref ref, *ref_p;
8522 external_ref **slot;
8524 ref.type = die;
8525 slot = map->find_slot (&ref, INSERT);
8526 if (*slot != HTAB_EMPTY_ENTRY)
8527 return *slot;
8529 ref_p = XCNEW (struct external_ref);
8530 ref_p->type = die;
8531 *slot = ref_p;
8532 return ref_p;
8535 /* Subroutine of optimize_external_refs, below.
8537 If we see a type skeleton, record it as our stub. If we see external
8538 references, remember how many we've seen. */
8540 static void
8541 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
8543 dw_die_ref c;
8544 dw_attr_node *a;
8545 unsigned ix;
8546 struct external_ref *ref_p;
8548 if (is_type_die (die)
8549 && (c = get_AT_ref (die, DW_AT_signature)))
8551 /* This is a local skeleton; use it for local references. */
8552 ref_p = lookup_external_ref (map, c);
8553 ref_p->stub = die;
8556 /* Scan the DIE references, and remember any that refer to DIEs from
8557 other CUs (i.e. those which are not marked). */
8558 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8559 if (AT_class (a) == dw_val_class_die_ref
8560 && (c = AT_ref (a))->die_mark == 0
8561 && is_type_die (c))
8563 ref_p = lookup_external_ref (map, c);
8564 ref_p->n_refs++;
8567 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
8570 /* htab_traverse callback function for optimize_external_refs, below. SLOT
8571 points to an external_ref, DATA is the CU we're processing. If we don't
8572 already have a local stub, and we have multiple refs, build a stub. */
8575 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
8577 struct external_ref *ref_p = *slot;
8579 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
8581 /* We have multiple references to this type, so build a small stub.
8582 Both of these forms are a bit dodgy from the perspective of the
8583 DWARF standard, since technically they should have names. */
8584 dw_die_ref cu = data;
8585 dw_die_ref type = ref_p->type;
8586 dw_die_ref stub = NULL;
8588 if (type->comdat_type_p)
8590 /* If we refer to this type via sig8, use AT_signature. */
8591 stub = new_die (type->die_tag, cu, NULL_TREE);
8592 add_AT_die_ref (stub, DW_AT_signature, type);
8594 else
8596 /* Otherwise, use a typedef with no name. */
8597 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
8598 add_AT_die_ref (stub, DW_AT_type, type);
8601 stub->die_mark++;
8602 ref_p->stub = stub;
8604 return 1;
8607 /* DIE is a unit; look through all the DIE references to see if there are
8608 any external references to types, and if so, create local stubs for
8609 them which will be applied in build_abbrev_table. This is useful because
8610 references to local DIEs are smaller. */
8612 static external_ref_hash_type *
8613 optimize_external_refs (dw_die_ref die)
8615 external_ref_hash_type *map = new external_ref_hash_type (10);
8616 optimize_external_refs_1 (die, map);
8617 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
8618 return map;
8621 /* The following 3 variables are temporaries that are computed only during the
8622 build_abbrev_table call and used and released during the following
8623 optimize_abbrev_table call. */
8625 /* First abbrev_id that can be optimized based on usage. */
8626 static unsigned int abbrev_opt_start;
8628 /* Maximum abbrev_id of a base type plus one (we can't optimize DIEs with
8629 abbrev_id smaller than this, because they must be already sized
8630 during build_abbrev_table). */
8631 static unsigned int abbrev_opt_base_type_end;
8633 /* Vector of usage counts during build_abbrev_table. Indexed by
8634 abbrev_id - abbrev_opt_start. */
8635 static vec<unsigned int> abbrev_usage_count;
8637 /* Vector of all DIEs added with die_abbrev >= abbrev_opt_start. */
8638 static vec<dw_die_ref> sorted_abbrev_dies;
8640 /* The format of each DIE (and its attribute value pairs) is encoded in an
8641 abbreviation table. This routine builds the abbreviation table and assigns
8642 a unique abbreviation id for each abbreviation entry. The children of each
8643 die are visited recursively. */
8645 static void
8646 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
8648 unsigned int abbrev_id = 0;
8649 dw_die_ref c;
8650 dw_attr_node *a;
8651 unsigned ix;
8652 dw_die_ref abbrev;
8654 /* Scan the DIE references, and replace any that refer to
8655 DIEs from other CUs (i.e. those which are not marked) with
8656 the local stubs we built in optimize_external_refs. */
8657 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8658 if (AT_class (a) == dw_val_class_die_ref
8659 && (c = AT_ref (a))->die_mark == 0)
8661 struct external_ref *ref_p;
8662 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
8664 ref_p = lookup_external_ref (extern_map, c);
8665 if (ref_p->stub && ref_p->stub != die)
8666 change_AT_die_ref (a, ref_p->stub);
8667 else
8668 /* We aren't changing this reference, so mark it external. */
8669 set_AT_ref_external (a, 1);
8672 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
8674 dw_attr_node *die_a, *abbrev_a;
8675 unsigned ix;
8676 bool ok = true;
8678 if (abbrev_id == 0)
8679 continue;
8680 if (abbrev->die_tag != die->die_tag)
8681 continue;
8682 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8683 continue;
8685 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8686 continue;
8688 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8690 abbrev_a = &(*abbrev->die_attr)[ix];
8691 if ((abbrev_a->dw_attr != die_a->dw_attr)
8692 || (value_format (abbrev_a) != value_format (die_a)))
8694 ok = false;
8695 break;
8698 if (ok)
8699 break;
8702 if (abbrev_id >= vec_safe_length (abbrev_die_table))
8704 vec_safe_push (abbrev_die_table, die);
8705 if (abbrev_opt_start)
8706 abbrev_usage_count.safe_push (0);
8708 if (abbrev_opt_start && abbrev_id >= abbrev_opt_start)
8710 abbrev_usage_count[abbrev_id - abbrev_opt_start]++;
8711 sorted_abbrev_dies.safe_push (die);
8714 die->die_abbrev = abbrev_id;
8715 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
8718 /* Callback function for sorted_abbrev_dies vector sorting. We sort
8719 by die_abbrev's usage count, from the most commonly used
8720 abbreviation to the least. */
8722 static int
8723 die_abbrev_cmp (const void *p1, const void *p2)
8725 dw_die_ref die1 = *(const dw_die_ref *) p1;
8726 dw_die_ref die2 = *(const dw_die_ref *) p2;
8728 gcc_checking_assert (die1->die_abbrev >= abbrev_opt_start);
8729 gcc_checking_assert (die2->die_abbrev >= abbrev_opt_start);
8731 if (die1->die_abbrev >= abbrev_opt_base_type_end
8732 && die2->die_abbrev >= abbrev_opt_base_type_end)
8734 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
8735 > abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
8736 return -1;
8737 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
8738 < abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
8739 return 1;
8742 /* Stabilize the sort. */
8743 if (die1->die_abbrev < die2->die_abbrev)
8744 return -1;
8745 if (die1->die_abbrev > die2->die_abbrev)
8746 return 1;
8748 return 0;
8751 /* Convert dw_val_class_const and dw_val_class_unsigned_const class attributes
8752 of DIEs in between sorted_abbrev_dies[first_id] and abbrev_dies[end_id - 1]
8753 into dw_val_class_const_implicit or
8754 dw_val_class_unsigned_const_implicit. */
8756 static void
8757 optimize_implicit_const (unsigned int first_id, unsigned int end,
8758 vec<bool> &implicit_consts)
8760 /* It never makes sense if there is just one DIE using the abbreviation. */
8761 if (end < first_id + 2)
8762 return;
8764 dw_attr_node *a;
8765 unsigned ix, i;
8766 dw_die_ref die = sorted_abbrev_dies[first_id];
8767 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8768 if (implicit_consts[ix])
8770 enum dw_val_class new_class = dw_val_class_none;
8771 switch (AT_class (a))
8773 case dw_val_class_unsigned_const:
8774 if ((HOST_WIDE_INT) AT_unsigned (a) < 0)
8775 continue;
8777 /* The .debug_abbrev section will grow by
8778 size_of_sleb128 (AT_unsigned (a)) and we avoid the constants
8779 in all the DIEs using that abbreviation. */
8780 if (constant_size (AT_unsigned (a)) * (end - first_id)
8781 <= (unsigned) size_of_sleb128 (AT_unsigned (a)))
8782 continue;
8784 new_class = dw_val_class_unsigned_const_implicit;
8785 break;
8787 case dw_val_class_const:
8788 new_class = dw_val_class_const_implicit;
8789 break;
8791 case dw_val_class_file:
8792 new_class = dw_val_class_file_implicit;
8793 break;
8795 default:
8796 continue;
8798 for (i = first_id; i < end; i++)
8799 (*sorted_abbrev_dies[i]->die_attr)[ix].dw_attr_val.val_class
8800 = new_class;
8804 /* Attempt to optimize abbreviation table from abbrev_opt_start
8805 abbreviation above. */
8807 static void
8808 optimize_abbrev_table (void)
8810 if (abbrev_opt_start
8811 && vec_safe_length (abbrev_die_table) > abbrev_opt_start
8812 && (dwarf_version >= 5 || vec_safe_length (abbrev_die_table) > 127))
8814 auto_vec<bool, 32> implicit_consts;
8815 sorted_abbrev_dies.qsort (die_abbrev_cmp);
8817 unsigned int abbrev_id = abbrev_opt_start - 1;
8818 unsigned int first_id = ~0U;
8819 unsigned int last_abbrev_id = 0;
8820 unsigned int i;
8821 dw_die_ref die;
8822 if (abbrev_opt_base_type_end > abbrev_opt_start)
8823 abbrev_id = abbrev_opt_base_type_end - 1;
8824 /* Reassign abbreviation ids from abbrev_opt_start above, so that
8825 most commonly used abbreviations come first. */
8826 FOR_EACH_VEC_ELT (sorted_abbrev_dies, i, die)
8828 dw_attr_node *a;
8829 unsigned ix;
8831 /* If calc_base_type_die_sizes has been called, the CU and
8832 base types after it can't be optimized, because we've already
8833 calculated their DIE offsets. We've sorted them first. */
8834 if (die->die_abbrev < abbrev_opt_base_type_end)
8835 continue;
8836 if (die->die_abbrev != last_abbrev_id)
8838 last_abbrev_id = die->die_abbrev;
8839 if (dwarf_version >= 5 && first_id != ~0U)
8840 optimize_implicit_const (first_id, i, implicit_consts);
8841 abbrev_id++;
8842 (*abbrev_die_table)[abbrev_id] = die;
8843 if (dwarf_version >= 5)
8845 first_id = i;
8846 implicit_consts.truncate (0);
8848 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8849 switch (AT_class (a))
8851 case dw_val_class_const:
8852 case dw_val_class_unsigned_const:
8853 case dw_val_class_file:
8854 implicit_consts.safe_push (true);
8855 break;
8856 default:
8857 implicit_consts.safe_push (false);
8858 break;
8862 else if (dwarf_version >= 5)
8864 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8865 if (!implicit_consts[ix])
8866 continue;
8867 else
8869 dw_attr_node *other_a
8870 = &(*(*abbrev_die_table)[abbrev_id]->die_attr)[ix];
8871 if (!dw_val_equal_p (&a->dw_attr_val,
8872 &other_a->dw_attr_val))
8873 implicit_consts[ix] = false;
8876 die->die_abbrev = abbrev_id;
8878 gcc_assert (abbrev_id == vec_safe_length (abbrev_die_table) - 1);
8879 if (dwarf_version >= 5 && first_id != ~0U)
8880 optimize_implicit_const (first_id, i, implicit_consts);
8883 abbrev_opt_start = 0;
8884 abbrev_opt_base_type_end = 0;
8885 abbrev_usage_count.release ();
8886 sorted_abbrev_dies.release ();
8889 /* Return the power-of-two number of bytes necessary to represent VALUE. */
8891 static int
8892 constant_size (unsigned HOST_WIDE_INT value)
8894 int log;
8896 if (value == 0)
8897 log = 0;
8898 else
8899 log = floor_log2 (value);
8901 log = log / 8;
8902 log = 1 << (floor_log2 (log) + 1);
8904 return log;
8907 /* Return the size of a DIE as it is represented in the
8908 .debug_info section. */
8910 static unsigned long
8911 size_of_die (dw_die_ref die)
8913 unsigned long size = 0;
8914 dw_attr_node *a;
8915 unsigned ix;
8916 enum dwarf_form form;
8918 size += size_of_uleb128 (die->die_abbrev);
8919 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8921 switch (AT_class (a))
8923 case dw_val_class_addr:
8924 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8926 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8927 size += size_of_uleb128 (AT_index (a));
8929 else
8930 size += DWARF2_ADDR_SIZE;
8931 break;
8932 case dw_val_class_offset:
8933 size += DWARF_OFFSET_SIZE;
8934 break;
8935 case dw_val_class_loc:
8937 unsigned long lsize = size_of_locs (AT_loc (a));
8939 /* Block length. */
8940 if (dwarf_version >= 4)
8941 size += size_of_uleb128 (lsize);
8942 else
8943 size += constant_size (lsize);
8944 size += lsize;
8946 break;
8947 case dw_val_class_loc_list:
8948 if (dwarf_split_debug_info && dwarf_version >= 5)
8950 gcc_assert (AT_loc_list (a)->num_assigned);
8951 size += size_of_uleb128 (AT_loc_list (a)->hash);
8953 else
8954 size += DWARF_OFFSET_SIZE;
8955 break;
8956 case dw_val_class_range_list:
8957 if (value_format (a) == DW_FORM_rnglistx)
8959 gcc_assert (rnglist_idx);
8960 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
8961 size += size_of_uleb128 (r->idx);
8963 else
8964 size += DWARF_OFFSET_SIZE;
8965 break;
8966 case dw_val_class_const:
8967 size += size_of_sleb128 (AT_int (a));
8968 break;
8969 case dw_val_class_unsigned_const:
8971 int csize = constant_size (AT_unsigned (a));
8972 if (dwarf_version == 3
8973 && a->dw_attr == DW_AT_data_member_location
8974 && csize >= 4)
8975 size += size_of_uleb128 (AT_unsigned (a));
8976 else
8977 size += csize;
8979 break;
8980 case dw_val_class_const_implicit:
8981 case dw_val_class_unsigned_const_implicit:
8982 case dw_val_class_file_implicit:
8983 /* These occupy no size in the DIE, just an extra sleb128 in
8984 .debug_abbrev. */
8985 break;
8986 case dw_val_class_const_double:
8987 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
8988 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
8989 size++; /* block */
8990 break;
8991 case dw_val_class_wide_int:
8992 size += (get_full_len (*a->dw_attr_val.v.val_wide)
8993 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
8994 if (get_full_len (*a->dw_attr_val.v.val_wide)
8995 * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
8996 size++; /* block */
8997 break;
8998 case dw_val_class_vec:
8999 size += constant_size (a->dw_attr_val.v.val_vec.length
9000 * a->dw_attr_val.v.val_vec.elt_size)
9001 + a->dw_attr_val.v.val_vec.length
9002 * a->dw_attr_val.v.val_vec.elt_size; /* block */
9003 break;
9004 case dw_val_class_flag:
9005 if (dwarf_version >= 4)
9006 /* Currently all add_AT_flag calls pass in 1 as last argument,
9007 so DW_FORM_flag_present can be used. If that ever changes,
9008 we'll need to use DW_FORM_flag and have some optimization
9009 in build_abbrev_table that will change those to
9010 DW_FORM_flag_present if it is set to 1 in all DIEs using
9011 the same abbrev entry. */
9012 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9013 else
9014 size += 1;
9015 break;
9016 case dw_val_class_die_ref:
9017 if (AT_ref_external (a))
9019 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
9020 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
9021 is sized by target address length, whereas in DWARF3
9022 it's always sized as an offset. */
9023 if (use_debug_types)
9024 size += DWARF_TYPE_SIGNATURE_SIZE;
9025 else if (dwarf_version == 2)
9026 size += DWARF2_ADDR_SIZE;
9027 else
9028 size += DWARF_OFFSET_SIZE;
9030 else
9031 size += DWARF_OFFSET_SIZE;
9032 break;
9033 case dw_val_class_fde_ref:
9034 size += DWARF_OFFSET_SIZE;
9035 break;
9036 case dw_val_class_lbl_id:
9037 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9039 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9040 size += size_of_uleb128 (AT_index (a));
9042 else
9043 size += DWARF2_ADDR_SIZE;
9044 break;
9045 case dw_val_class_lineptr:
9046 case dw_val_class_macptr:
9047 case dw_val_class_loclistsptr:
9048 size += DWARF_OFFSET_SIZE;
9049 break;
9050 case dw_val_class_str:
9051 form = AT_string_form (a);
9052 if (form == DW_FORM_strp || form == DW_FORM_line_strp)
9053 size += DWARF_OFFSET_SIZE;
9054 else if (form == DW_FORM_GNU_str_index)
9055 size += size_of_uleb128 (AT_index (a));
9056 else
9057 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
9058 break;
9059 case dw_val_class_file:
9060 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
9061 break;
9062 case dw_val_class_data8:
9063 size += 8;
9064 break;
9065 case dw_val_class_vms_delta:
9066 size += DWARF_OFFSET_SIZE;
9067 break;
9068 case dw_val_class_high_pc:
9069 size += DWARF2_ADDR_SIZE;
9070 break;
9071 case dw_val_class_discr_value:
9072 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
9073 break;
9074 case dw_val_class_discr_list:
9076 unsigned block_size = size_of_discr_list (AT_discr_list (a));
9078 /* This is a block, so we have the block length and then its
9079 data. */
9080 size += constant_size (block_size) + block_size;
9082 break;
9083 default:
9084 gcc_unreachable ();
9088 return size;
9091 /* Size the debugging information associated with a given DIE. Visits the
9092 DIE's children recursively. Updates the global variable next_die_offset, on
9093 each time through. Uses the current value of next_die_offset to update the
9094 die_offset field in each DIE. */
9096 static void
9097 calc_die_sizes (dw_die_ref die)
9099 dw_die_ref c;
9101 gcc_assert (die->die_offset == 0
9102 || (unsigned long int) die->die_offset == next_die_offset);
9103 die->die_offset = next_die_offset;
9104 next_die_offset += size_of_die (die);
9106 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
9108 if (die->die_child != NULL)
9109 /* Count the null byte used to terminate sibling lists. */
9110 next_die_offset += 1;
9113 /* Size just the base type children at the start of the CU.
9114 This is needed because build_abbrev needs to size locs
9115 and sizing of type based stack ops needs to know die_offset
9116 values for the base types. */
9118 static void
9119 calc_base_type_die_sizes (void)
9121 unsigned long die_offset = (dwarf_split_debug_info
9122 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
9123 : DWARF_COMPILE_UNIT_HEADER_SIZE);
9124 unsigned int i;
9125 dw_die_ref base_type;
9126 #if ENABLE_ASSERT_CHECKING
9127 dw_die_ref prev = comp_unit_die ()->die_child;
9128 #endif
9130 die_offset += size_of_die (comp_unit_die ());
9131 for (i = 0; base_types.iterate (i, &base_type); i++)
9133 #if ENABLE_ASSERT_CHECKING
9134 gcc_assert (base_type->die_offset == 0
9135 && prev->die_sib == base_type
9136 && base_type->die_child == NULL
9137 && base_type->die_abbrev);
9138 prev = base_type;
9139 #endif
9140 if (abbrev_opt_start
9141 && base_type->die_abbrev >= abbrev_opt_base_type_end)
9142 abbrev_opt_base_type_end = base_type->die_abbrev + 1;
9143 base_type->die_offset = die_offset;
9144 die_offset += size_of_die (base_type);
9148 /* Set the marks for a die and its children. We do this so
9149 that we know whether or not a reference needs to use FORM_ref_addr; only
9150 DIEs in the same CU will be marked. We used to clear out the offset
9151 and use that as the flag, but ran into ordering problems. */
9153 static void
9154 mark_dies (dw_die_ref die)
9156 dw_die_ref c;
9158 gcc_assert (!die->die_mark);
9160 die->die_mark = 1;
9161 FOR_EACH_CHILD (die, c, mark_dies (c));
9164 /* Clear the marks for a die and its children. */
9166 static void
9167 unmark_dies (dw_die_ref die)
9169 dw_die_ref c;
9171 if (! use_debug_types)
9172 gcc_assert (die->die_mark);
9174 die->die_mark = 0;
9175 FOR_EACH_CHILD (die, c, unmark_dies (c));
9178 /* Clear the marks for a die, its children and referred dies. */
9180 static void
9181 unmark_all_dies (dw_die_ref die)
9183 dw_die_ref c;
9184 dw_attr_node *a;
9185 unsigned ix;
9187 if (!die->die_mark)
9188 return;
9189 die->die_mark = 0;
9191 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
9193 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9194 if (AT_class (a) == dw_val_class_die_ref)
9195 unmark_all_dies (AT_ref (a));
9198 /* Calculate if the entry should appear in the final output file. It may be
9199 from a pruned a type. */
9201 static bool
9202 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
9204 /* By limiting gnu pubnames to definitions only, gold can generate a
9205 gdb index without entries for declarations, which don't include
9206 enough information to be useful. */
9207 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
9208 return false;
9210 if (table == pubname_table)
9212 /* Enumerator names are part of the pubname table, but the
9213 parent DW_TAG_enumeration_type die may have been pruned.
9214 Don't output them if that is the case. */
9215 if (p->die->die_tag == DW_TAG_enumerator &&
9216 (p->die->die_parent == NULL
9217 || !p->die->die_parent->die_perennial_p))
9218 return false;
9220 /* Everything else in the pubname table is included. */
9221 return true;
9224 /* The pubtypes table shouldn't include types that have been
9225 pruned. */
9226 return (p->die->die_offset != 0
9227 || !flag_eliminate_unused_debug_types);
9230 /* Return the size of the .debug_pubnames or .debug_pubtypes table
9231 generated for the compilation unit. */
9233 static unsigned long
9234 size_of_pubnames (vec<pubname_entry, va_gc> *names)
9236 unsigned long size;
9237 unsigned i;
9238 pubname_entry *p;
9239 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
9241 size = DWARF_PUBNAMES_HEADER_SIZE;
9242 FOR_EACH_VEC_ELT (*names, i, p)
9243 if (include_pubname_in_output (names, p))
9244 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
9246 size += DWARF_OFFSET_SIZE;
9247 return size;
9250 /* Return the size of the information in the .debug_aranges section. */
9252 static unsigned long
9253 size_of_aranges (void)
9255 unsigned long size;
9257 size = DWARF_ARANGES_HEADER_SIZE;
9259 /* Count the address/length pair for this compilation unit. */
9260 if (text_section_used)
9261 size += 2 * DWARF2_ADDR_SIZE;
9262 if (cold_text_section_used)
9263 size += 2 * DWARF2_ADDR_SIZE;
9264 if (have_multiple_function_sections)
9266 unsigned fde_idx;
9267 dw_fde_ref fde;
9269 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9271 if (DECL_IGNORED_P (fde->decl))
9272 continue;
9273 if (!fde->in_std_section)
9274 size += 2 * DWARF2_ADDR_SIZE;
9275 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9276 size += 2 * DWARF2_ADDR_SIZE;
9280 /* Count the two zero words used to terminated the address range table. */
9281 size += 2 * DWARF2_ADDR_SIZE;
9282 return size;
9285 /* Select the encoding of an attribute value. */
9287 static enum dwarf_form
9288 value_format (dw_attr_node *a)
9290 switch (AT_class (a))
9292 case dw_val_class_addr:
9293 /* Only very few attributes allow DW_FORM_addr. */
9294 switch (a->dw_attr)
9296 case DW_AT_low_pc:
9297 case DW_AT_high_pc:
9298 case DW_AT_entry_pc:
9299 case DW_AT_trampoline:
9300 return (AT_index (a) == NOT_INDEXED
9301 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9302 default:
9303 break;
9305 switch (DWARF2_ADDR_SIZE)
9307 case 1:
9308 return DW_FORM_data1;
9309 case 2:
9310 return DW_FORM_data2;
9311 case 4:
9312 return DW_FORM_data4;
9313 case 8:
9314 return DW_FORM_data8;
9315 default:
9316 gcc_unreachable ();
9318 case dw_val_class_loc_list:
9319 if (dwarf_split_debug_info
9320 && dwarf_version >= 5
9321 && AT_loc_list (a)->num_assigned)
9322 return DW_FORM_loclistx;
9323 /* FALLTHRU */
9324 case dw_val_class_range_list:
9325 /* For range lists in DWARF 5, use DW_FORM_rnglistx from .debug_info.dwo
9326 but in .debug_info use DW_FORM_sec_offset, which is shorter if we
9327 care about sizes of .debug* sections in shared libraries and
9328 executables and don't take into account relocations that affect just
9329 relocatable objects - for DW_FORM_rnglistx we'd have to emit offset
9330 table in the .debug_rnglists section. */
9331 if (dwarf_split_debug_info
9332 && dwarf_version >= 5
9333 && AT_class (a) == dw_val_class_range_list
9334 && rnglist_idx
9335 && a->dw_attr_val.val_entry != RELOCATED_OFFSET)
9336 return DW_FORM_rnglistx;
9337 if (dwarf_version >= 4)
9338 return DW_FORM_sec_offset;
9339 /* FALLTHRU */
9340 case dw_val_class_vms_delta:
9341 case dw_val_class_offset:
9342 switch (DWARF_OFFSET_SIZE)
9344 case 4:
9345 return DW_FORM_data4;
9346 case 8:
9347 return DW_FORM_data8;
9348 default:
9349 gcc_unreachable ();
9351 case dw_val_class_loc:
9352 if (dwarf_version >= 4)
9353 return DW_FORM_exprloc;
9354 switch (constant_size (size_of_locs (AT_loc (a))))
9356 case 1:
9357 return DW_FORM_block1;
9358 case 2:
9359 return DW_FORM_block2;
9360 case 4:
9361 return DW_FORM_block4;
9362 default:
9363 gcc_unreachable ();
9365 case dw_val_class_const:
9366 return DW_FORM_sdata;
9367 case dw_val_class_unsigned_const:
9368 switch (constant_size (AT_unsigned (a)))
9370 case 1:
9371 return DW_FORM_data1;
9372 case 2:
9373 return DW_FORM_data2;
9374 case 4:
9375 /* In DWARF3 DW_AT_data_member_location with
9376 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
9377 constant, so we need to use DW_FORM_udata if we need
9378 a large constant. */
9379 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9380 return DW_FORM_udata;
9381 return DW_FORM_data4;
9382 case 8:
9383 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9384 return DW_FORM_udata;
9385 return DW_FORM_data8;
9386 default:
9387 gcc_unreachable ();
9389 case dw_val_class_const_implicit:
9390 case dw_val_class_unsigned_const_implicit:
9391 case dw_val_class_file_implicit:
9392 return DW_FORM_implicit_const;
9393 case dw_val_class_const_double:
9394 switch (HOST_BITS_PER_WIDE_INT)
9396 case 8:
9397 return DW_FORM_data2;
9398 case 16:
9399 return DW_FORM_data4;
9400 case 32:
9401 return DW_FORM_data8;
9402 case 64:
9403 if (dwarf_version >= 5)
9404 return DW_FORM_data16;
9405 /* FALLTHRU */
9406 default:
9407 return DW_FORM_block1;
9409 case dw_val_class_wide_int:
9410 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
9412 case 8:
9413 return DW_FORM_data1;
9414 case 16:
9415 return DW_FORM_data2;
9416 case 32:
9417 return DW_FORM_data4;
9418 case 64:
9419 return DW_FORM_data8;
9420 case 128:
9421 if (dwarf_version >= 5)
9422 return DW_FORM_data16;
9423 /* FALLTHRU */
9424 default:
9425 return DW_FORM_block1;
9427 case dw_val_class_vec:
9428 switch (constant_size (a->dw_attr_val.v.val_vec.length
9429 * a->dw_attr_val.v.val_vec.elt_size))
9431 case 1:
9432 return DW_FORM_block1;
9433 case 2:
9434 return DW_FORM_block2;
9435 case 4:
9436 return DW_FORM_block4;
9437 default:
9438 gcc_unreachable ();
9440 case dw_val_class_flag:
9441 if (dwarf_version >= 4)
9443 /* Currently all add_AT_flag calls pass in 1 as last argument,
9444 so DW_FORM_flag_present can be used. If that ever changes,
9445 we'll need to use DW_FORM_flag and have some optimization
9446 in build_abbrev_table that will change those to
9447 DW_FORM_flag_present if it is set to 1 in all DIEs using
9448 the same abbrev entry. */
9449 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9450 return DW_FORM_flag_present;
9452 return DW_FORM_flag;
9453 case dw_val_class_die_ref:
9454 if (AT_ref_external (a))
9455 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
9456 else
9457 return DW_FORM_ref;
9458 case dw_val_class_fde_ref:
9459 return DW_FORM_data;
9460 case dw_val_class_lbl_id:
9461 return (AT_index (a) == NOT_INDEXED
9462 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9463 case dw_val_class_lineptr:
9464 case dw_val_class_macptr:
9465 case dw_val_class_loclistsptr:
9466 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
9467 case dw_val_class_str:
9468 return AT_string_form (a);
9469 case dw_val_class_file:
9470 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
9472 case 1:
9473 return DW_FORM_data1;
9474 case 2:
9475 return DW_FORM_data2;
9476 case 4:
9477 return DW_FORM_data4;
9478 default:
9479 gcc_unreachable ();
9482 case dw_val_class_data8:
9483 return DW_FORM_data8;
9485 case dw_val_class_high_pc:
9486 switch (DWARF2_ADDR_SIZE)
9488 case 1:
9489 return DW_FORM_data1;
9490 case 2:
9491 return DW_FORM_data2;
9492 case 4:
9493 return DW_FORM_data4;
9494 case 8:
9495 return DW_FORM_data8;
9496 default:
9497 gcc_unreachable ();
9500 case dw_val_class_discr_value:
9501 return (a->dw_attr_val.v.val_discr_value.pos
9502 ? DW_FORM_udata
9503 : DW_FORM_sdata);
9504 case dw_val_class_discr_list:
9505 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
9507 case 1:
9508 return DW_FORM_block1;
9509 case 2:
9510 return DW_FORM_block2;
9511 case 4:
9512 return DW_FORM_block4;
9513 default:
9514 gcc_unreachable ();
9517 default:
9518 gcc_unreachable ();
9522 /* Output the encoding of an attribute value. */
9524 static void
9525 output_value_format (dw_attr_node *a)
9527 enum dwarf_form form = value_format (a);
9529 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
9532 /* Given a die and id, produce the appropriate abbreviations. */
9534 static void
9535 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
9537 unsigned ix;
9538 dw_attr_node *a_attr;
9540 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
9541 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
9542 dwarf_tag_name (abbrev->die_tag));
9544 if (abbrev->die_child != NULL)
9545 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
9546 else
9547 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
9549 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
9551 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
9552 dwarf_attr_name (a_attr->dw_attr));
9553 output_value_format (a_attr);
9554 if (value_format (a_attr) == DW_FORM_implicit_const)
9556 if (AT_class (a_attr) == dw_val_class_file_implicit)
9558 int f = maybe_emit_file (a_attr->dw_attr_val.v.val_file);
9559 const char *filename = a_attr->dw_attr_val.v.val_file->filename;
9560 dw2_asm_output_data_sleb128 (f, "(%s)", filename);
9562 else
9563 dw2_asm_output_data_sleb128 (a_attr->dw_attr_val.v.val_int, NULL);
9567 dw2_asm_output_data (1, 0, NULL);
9568 dw2_asm_output_data (1, 0, NULL);
9572 /* Output the .debug_abbrev section which defines the DIE abbreviation
9573 table. */
9575 static void
9576 output_abbrev_section (void)
9578 unsigned int abbrev_id;
9579 dw_die_ref abbrev;
9581 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
9582 if (abbrev_id != 0)
9583 output_die_abbrevs (abbrev_id, abbrev);
9585 /* Terminate the table. */
9586 dw2_asm_output_data (1, 0, NULL);
9589 /* Output a symbol we can use to refer to this DIE from another CU. */
9591 static inline void
9592 output_die_symbol (dw_die_ref die)
9594 const char *sym = die->die_id.die_symbol;
9596 gcc_assert (!die->comdat_type_p);
9598 if (sym == 0)
9599 return;
9601 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
9602 /* We make these global, not weak; if the target doesn't support
9603 .linkonce, it doesn't support combining the sections, so debugging
9604 will break. */
9605 targetm.asm_out.globalize_label (asm_out_file, sym);
9607 ASM_OUTPUT_LABEL (asm_out_file, sym);
9610 /* Return a new location list, given the begin and end range, and the
9611 expression. */
9613 static inline dw_loc_list_ref
9614 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
9615 const char *section)
9617 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
9619 retlist->begin = begin;
9620 retlist->begin_entry = NULL;
9621 retlist->end = end;
9622 retlist->expr = expr;
9623 retlist->section = section;
9625 return retlist;
9628 /* Generate a new internal symbol for this location list node, if it
9629 hasn't got one yet. */
9631 static inline void
9632 gen_llsym (dw_loc_list_ref list)
9634 gcc_assert (!list->ll_symbol);
9635 list->ll_symbol = gen_internal_sym ("LLST");
9638 /* Output the location list given to us. */
9640 static void
9641 output_loc_list (dw_loc_list_ref list_head)
9643 if (list_head->emitted)
9644 return;
9645 list_head->emitted = true;
9647 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
9649 dw_loc_list_ref curr = list_head;
9650 const char *last_section = NULL;
9651 const char *base_label = NULL;
9653 /* Walk the location list, and output each range + expression. */
9654 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
9656 unsigned long size;
9657 /* Don't output an entry that starts and ends at the same address. */
9658 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
9659 continue;
9660 size = size_of_locs (curr->expr);
9661 /* If the expression is too large, drop it on the floor. We could
9662 perhaps put it into DW_TAG_dwarf_procedure and refer to that
9663 in the expression, but >= 64KB expressions for a single value
9664 in a single range are unlikely very useful. */
9665 if (dwarf_version < 5 && size > 0xffff)
9666 continue;
9667 if (dwarf_version >= 5)
9669 if (dwarf_split_debug_info)
9671 /* For -gsplit-dwarf, emit DW_LLE_starx_length, which has
9672 uleb128 index into .debug_addr and uleb128 length. */
9673 dw2_asm_output_data (1, DW_LLE_startx_length,
9674 "DW_LLE_startx_length (%s)",
9675 list_head->ll_symbol);
9676 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9677 "Location list range start index "
9678 "(%s)", curr->begin);
9679 /* FIXME: This will ICE ifndef HAVE_AS_LEB128.
9680 For that case we probably need to emit DW_LLE_startx_endx,
9681 but we'd need 2 .debug_addr entries rather than just one. */
9682 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9683 "Location list length (%s)",
9684 list_head->ll_symbol);
9686 else if (!have_multiple_function_sections && HAVE_AS_LEB128)
9688 /* If all code is in .text section, the base address is
9689 already provided by the CU attributes. Use
9690 DW_LLE_offset_pair where both addresses are uleb128 encoded
9691 offsets against that base. */
9692 dw2_asm_output_data (1, DW_LLE_offset_pair,
9693 "DW_LLE_offset_pair (%s)",
9694 list_head->ll_symbol);
9695 dw2_asm_output_delta_uleb128 (curr->begin, curr->section,
9696 "Location list begin address (%s)",
9697 list_head->ll_symbol);
9698 dw2_asm_output_delta_uleb128 (curr->end, curr->section,
9699 "Location list end address (%s)",
9700 list_head->ll_symbol);
9702 else if (HAVE_AS_LEB128)
9704 /* Otherwise, find out how many consecutive entries could share
9705 the same base entry. If just one, emit DW_LLE_start_length,
9706 otherwise emit DW_LLE_base_address for the base address
9707 followed by a series of DW_LLE_offset_pair. */
9708 if (last_section == NULL || curr->section != last_section)
9710 dw_loc_list_ref curr2;
9711 for (curr2 = curr->dw_loc_next; curr2 != NULL;
9712 curr2 = curr2->dw_loc_next)
9714 if (strcmp (curr2->begin, curr2->end) == 0
9715 && !curr2->force)
9716 continue;
9717 break;
9719 if (curr2 == NULL || curr->section != curr2->section)
9720 last_section = NULL;
9721 else
9723 last_section = curr->section;
9724 base_label = curr->begin;
9725 dw2_asm_output_data (1, DW_LLE_base_address,
9726 "DW_LLE_base_address (%s)",
9727 list_head->ll_symbol);
9728 dw2_asm_output_addr (DWARF2_ADDR_SIZE, base_label,
9729 "Base address (%s)",
9730 list_head->ll_symbol);
9733 /* Only one entry with the same base address. Use
9734 DW_LLE_start_length with absolute address and uleb128
9735 length. */
9736 if (last_section == NULL)
9738 dw2_asm_output_data (1, DW_LLE_start_length,
9739 "DW_LLE_start_length (%s)",
9740 list_head->ll_symbol);
9741 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9742 "Location list begin address (%s)",
9743 list_head->ll_symbol);
9744 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9745 "Location list length "
9746 "(%s)", list_head->ll_symbol);
9748 /* Otherwise emit DW_LLE_offset_pair, relative to above emitted
9749 DW_LLE_base_address. */
9750 else
9752 dw2_asm_output_data (1, DW_LLE_offset_pair,
9753 "DW_LLE_offset_pair (%s)",
9754 list_head->ll_symbol);
9755 dw2_asm_output_delta_uleb128 (curr->begin, base_label,
9756 "Location list begin address "
9757 "(%s)", list_head->ll_symbol);
9758 dw2_asm_output_delta_uleb128 (curr->end, base_label,
9759 "Location list end address "
9760 "(%s)", list_head->ll_symbol);
9763 /* The assembler does not support .uleb128 directive. Emit
9764 DW_LLE_start_end with a pair of absolute addresses. */
9765 else
9767 dw2_asm_output_data (1, DW_LLE_start_end,
9768 "DW_LLE_start_end (%s)",
9769 list_head->ll_symbol);
9770 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9771 "Location list begin address (%s)",
9772 list_head->ll_symbol);
9773 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9774 "Location list end address (%s)",
9775 list_head->ll_symbol);
9778 else if (dwarf_split_debug_info)
9780 /* For -gsplit-dwarf -gdwarf-{2,3,4} emit index into .debug_addr
9781 and 4 byte length. */
9782 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
9783 "Location list start/length entry (%s)",
9784 list_head->ll_symbol);
9785 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9786 "Location list range start index (%s)",
9787 curr->begin);
9788 /* The length field is 4 bytes. If we ever need to support
9789 an 8-byte length, we can add a new DW_LLE code or fall back
9790 to DW_LLE_GNU_start_end_entry. */
9791 dw2_asm_output_delta (4, curr->end, curr->begin,
9792 "Location list range length (%s)",
9793 list_head->ll_symbol);
9795 else if (!have_multiple_function_sections)
9797 /* Pair of relative addresses against start of text section. */
9798 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
9799 "Location list begin address (%s)",
9800 list_head->ll_symbol);
9801 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
9802 "Location list end address (%s)",
9803 list_head->ll_symbol);
9805 else
9807 /* Pair of absolute addresses. */
9808 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9809 "Location list begin address (%s)",
9810 list_head->ll_symbol);
9811 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9812 "Location list end address (%s)",
9813 list_head->ll_symbol);
9816 /* Output the block length for this list of location operations. */
9817 if (dwarf_version >= 5)
9818 dw2_asm_output_data_uleb128 (size, "Location expression size");
9819 else
9821 gcc_assert (size <= 0xffff);
9822 dw2_asm_output_data (2, size, "Location expression size");
9825 output_loc_sequence (curr->expr, -1);
9828 /* And finally list termination. */
9829 if (dwarf_version >= 5)
9830 dw2_asm_output_data (1, DW_LLE_end_of_list,
9831 "DW_LLE_end_of_list (%s)", list_head->ll_symbol);
9832 else if (dwarf_split_debug_info)
9833 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
9834 "Location list terminator (%s)",
9835 list_head->ll_symbol);
9836 else
9838 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9839 "Location list terminator begin (%s)",
9840 list_head->ll_symbol);
9841 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9842 "Location list terminator end (%s)",
9843 list_head->ll_symbol);
9847 /* Output a range_list offset into the .debug_ranges or .debug_rnglists
9848 section. Emit a relocated reference if val_entry is NULL, otherwise,
9849 emit an indirect reference. */
9851 static void
9852 output_range_list_offset (dw_attr_node *a)
9854 const char *name = dwarf_attr_name (a->dw_attr);
9856 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
9858 if (dwarf_version >= 5)
9860 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9861 dw2_asm_output_offset (DWARF_OFFSET_SIZE, r->label,
9862 debug_ranges_section, "%s", name);
9864 else
9866 char *p = strchr (ranges_section_label, '\0');
9867 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
9868 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE);
9869 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
9870 debug_ranges_section, "%s", name);
9871 *p = '\0';
9874 else if (dwarf_version >= 5)
9876 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9877 gcc_assert (rnglist_idx);
9878 dw2_asm_output_data_uleb128 (r->idx, "%s", name);
9880 else
9881 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9882 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE,
9883 "%s (offset from %s)", name, ranges_section_label);
9886 /* Output the offset into the debug_loc section. */
9888 static void
9889 output_loc_list_offset (dw_attr_node *a)
9891 char *sym = AT_loc_list (a)->ll_symbol;
9893 gcc_assert (sym);
9894 if (!dwarf_split_debug_info)
9895 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
9896 "%s", dwarf_attr_name (a->dw_attr));
9897 else if (dwarf_version >= 5)
9899 gcc_assert (AT_loc_list (a)->num_assigned);
9900 dw2_asm_output_data_uleb128 (AT_loc_list (a)->hash, "%s (%s)",
9901 dwarf_attr_name (a->dw_attr),
9902 sym);
9904 else
9905 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
9906 "%s", dwarf_attr_name (a->dw_attr));
9909 /* Output an attribute's index or value appropriately. */
9911 static void
9912 output_attr_index_or_value (dw_attr_node *a)
9914 const char *name = dwarf_attr_name (a->dw_attr);
9916 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9918 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
9919 return;
9921 switch (AT_class (a))
9923 case dw_val_class_addr:
9924 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
9925 break;
9926 case dw_val_class_high_pc:
9927 case dw_val_class_lbl_id:
9928 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
9929 break;
9930 default:
9931 gcc_unreachable ();
9935 /* Output a type signature. */
9937 static inline void
9938 output_signature (const char *sig, const char *name)
9940 int i;
9942 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9943 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
9946 /* Output a discriminant value. */
9948 static inline void
9949 output_discr_value (dw_discr_value *discr_value, const char *name)
9951 if (discr_value->pos)
9952 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
9953 else
9954 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
9957 /* Output the DIE and its attributes. Called recursively to generate
9958 the definitions of each child DIE. */
9960 static void
9961 output_die (dw_die_ref die)
9963 dw_attr_node *a;
9964 dw_die_ref c;
9965 unsigned long size;
9966 unsigned ix;
9968 /* If someone in another CU might refer to us, set up a symbol for
9969 them to point to. */
9970 if (! die->comdat_type_p && die->die_id.die_symbol)
9971 output_die_symbol (die);
9973 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
9974 (unsigned long)die->die_offset,
9975 dwarf_tag_name (die->die_tag));
9977 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9979 const char *name = dwarf_attr_name (a->dw_attr);
9981 switch (AT_class (a))
9983 case dw_val_class_addr:
9984 output_attr_index_or_value (a);
9985 break;
9987 case dw_val_class_offset:
9988 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
9989 "%s", name);
9990 break;
9992 case dw_val_class_range_list:
9993 output_range_list_offset (a);
9994 break;
9996 case dw_val_class_loc:
9997 size = size_of_locs (AT_loc (a));
9999 /* Output the block length for this list of location operations. */
10000 if (dwarf_version >= 4)
10001 dw2_asm_output_data_uleb128 (size, "%s", name);
10002 else
10003 dw2_asm_output_data (constant_size (size), size, "%s", name);
10005 output_loc_sequence (AT_loc (a), -1);
10006 break;
10008 case dw_val_class_const:
10009 /* ??? It would be slightly more efficient to use a scheme like is
10010 used for unsigned constants below, but gdb 4.x does not sign
10011 extend. Gdb 5.x does sign extend. */
10012 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10013 break;
10015 case dw_val_class_unsigned_const:
10017 int csize = constant_size (AT_unsigned (a));
10018 if (dwarf_version == 3
10019 && a->dw_attr == DW_AT_data_member_location
10020 && csize >= 4)
10021 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
10022 else
10023 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
10025 break;
10027 case dw_val_class_const_implicit:
10028 if (flag_debug_asm)
10029 fprintf (asm_out_file, "\t\t\t%s %s ("
10030 HOST_WIDE_INT_PRINT_DEC ")\n",
10031 ASM_COMMENT_START, name, AT_int (a));
10032 break;
10034 case dw_val_class_unsigned_const_implicit:
10035 if (flag_debug_asm)
10036 fprintf (asm_out_file, "\t\t\t%s %s ("
10037 HOST_WIDE_INT_PRINT_HEX ")\n",
10038 ASM_COMMENT_START, name, AT_unsigned (a));
10039 break;
10041 case dw_val_class_const_double:
10043 unsigned HOST_WIDE_INT first, second;
10045 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
10046 dw2_asm_output_data (1,
10047 HOST_BITS_PER_DOUBLE_INT
10048 / HOST_BITS_PER_CHAR,
10049 NULL);
10051 if (WORDS_BIG_ENDIAN)
10053 first = a->dw_attr_val.v.val_double.high;
10054 second = a->dw_attr_val.v.val_double.low;
10056 else
10058 first = a->dw_attr_val.v.val_double.low;
10059 second = a->dw_attr_val.v.val_double.high;
10062 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10063 first, "%s", name);
10064 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10065 second, NULL);
10067 break;
10069 case dw_val_class_wide_int:
10071 int i;
10072 int len = get_full_len (*a->dw_attr_val.v.val_wide);
10073 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10074 if (len * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
10075 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide)
10076 * l, NULL);
10078 if (WORDS_BIG_ENDIAN)
10079 for (i = len - 1; i >= 0; --i)
10081 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10082 "%s", name);
10083 name = "";
10085 else
10086 for (i = 0; i < len; ++i)
10088 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10089 "%s", name);
10090 name = "";
10093 break;
10095 case dw_val_class_vec:
10097 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10098 unsigned int len = a->dw_attr_val.v.val_vec.length;
10099 unsigned int i;
10100 unsigned char *p;
10102 dw2_asm_output_data (constant_size (len * elt_size),
10103 len * elt_size, "%s", name);
10104 if (elt_size > sizeof (HOST_WIDE_INT))
10106 elt_size /= 2;
10107 len *= 2;
10109 for (i = 0, p = (unsigned char *) a->dw_attr_val.v.val_vec.array;
10110 i < len;
10111 i++, p += elt_size)
10112 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10113 "fp or vector constant word %u", i);
10114 break;
10117 case dw_val_class_flag:
10118 if (dwarf_version >= 4)
10120 /* Currently all add_AT_flag calls pass in 1 as last argument,
10121 so DW_FORM_flag_present can be used. If that ever changes,
10122 we'll need to use DW_FORM_flag and have some optimization
10123 in build_abbrev_table that will change those to
10124 DW_FORM_flag_present if it is set to 1 in all DIEs using
10125 the same abbrev entry. */
10126 gcc_assert (AT_flag (a) == 1);
10127 if (flag_debug_asm)
10128 fprintf (asm_out_file, "\t\t\t%s %s\n",
10129 ASM_COMMENT_START, name);
10130 break;
10132 dw2_asm_output_data (1, AT_flag (a), "%s", name);
10133 break;
10135 case dw_val_class_loc_list:
10136 output_loc_list_offset (a);
10137 break;
10139 case dw_val_class_die_ref:
10140 if (AT_ref_external (a))
10142 if (AT_ref (a)->comdat_type_p)
10144 comdat_type_node *type_node
10145 = AT_ref (a)->die_id.die_type_node;
10147 gcc_assert (type_node);
10148 output_signature (type_node->signature, name);
10150 else
10152 const char *sym = AT_ref (a)->die_id.die_symbol;
10153 int size;
10155 gcc_assert (sym);
10156 /* In DWARF2, DW_FORM_ref_addr is sized by target address
10157 length, whereas in DWARF3 it's always sized as an
10158 offset. */
10159 if (dwarf_version == 2)
10160 size = DWARF2_ADDR_SIZE;
10161 else
10162 size = DWARF_OFFSET_SIZE;
10163 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
10164 name);
10167 else
10169 gcc_assert (AT_ref (a)->die_offset);
10170 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
10171 "%s", name);
10173 break;
10175 case dw_val_class_fde_ref:
10177 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
10179 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
10180 a->dw_attr_val.v.val_fde_index * 2);
10181 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
10182 "%s", name);
10184 break;
10186 case dw_val_class_vms_delta:
10187 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
10188 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
10189 AT_vms_delta2 (a), AT_vms_delta1 (a),
10190 "%s", name);
10191 #else
10192 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
10193 AT_vms_delta2 (a), AT_vms_delta1 (a),
10194 "%s", name);
10195 #endif
10196 break;
10198 case dw_val_class_lbl_id:
10199 output_attr_index_or_value (a);
10200 break;
10202 case dw_val_class_lineptr:
10203 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10204 debug_line_section, "%s", name);
10205 break;
10207 case dw_val_class_macptr:
10208 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10209 debug_macinfo_section, "%s", name);
10210 break;
10212 case dw_val_class_loclistsptr:
10213 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10214 debug_loc_section, "%s", name);
10215 break;
10217 case dw_val_class_str:
10218 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
10219 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10220 a->dw_attr_val.v.val_str->label,
10221 debug_str_section,
10222 "%s: \"%s\"", name, AT_string (a));
10223 else if (a->dw_attr_val.v.val_str->form == DW_FORM_line_strp)
10224 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10225 a->dw_attr_val.v.val_str->label,
10226 debug_line_str_section,
10227 "%s: \"%s\"", name, AT_string (a));
10228 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
10229 dw2_asm_output_data_uleb128 (AT_index (a),
10230 "%s: \"%s\"", name, AT_string (a));
10231 else
10232 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
10233 break;
10235 case dw_val_class_file:
10237 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
10239 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
10240 a->dw_attr_val.v.val_file->filename);
10241 break;
10244 case dw_val_class_file_implicit:
10245 if (flag_debug_asm)
10246 fprintf (asm_out_file, "\t\t\t%s %s (%d, %s)\n",
10247 ASM_COMMENT_START, name,
10248 maybe_emit_file (a->dw_attr_val.v.val_file),
10249 a->dw_attr_val.v.val_file->filename);
10250 break;
10252 case dw_val_class_data8:
10254 int i;
10256 for (i = 0; i < 8; i++)
10257 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
10258 i == 0 ? "%s" : NULL, name);
10259 break;
10262 case dw_val_class_high_pc:
10263 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
10264 get_AT_low_pc (die), "DW_AT_high_pc");
10265 break;
10267 case dw_val_class_discr_value:
10268 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
10269 break;
10271 case dw_val_class_discr_list:
10273 dw_discr_list_ref list = AT_discr_list (a);
10274 const int size = size_of_discr_list (list);
10276 /* This is a block, so output its length first. */
10277 dw2_asm_output_data (constant_size (size), size,
10278 "%s: block size", name);
10280 for (; list != NULL; list = list->dw_discr_next)
10282 /* One byte for the discriminant value descriptor, and then as
10283 many LEB128 numbers as required. */
10284 if (list->dw_discr_range)
10285 dw2_asm_output_data (1, DW_DSC_range,
10286 "%s: DW_DSC_range", name);
10287 else
10288 dw2_asm_output_data (1, DW_DSC_label,
10289 "%s: DW_DSC_label", name);
10291 output_discr_value (&list->dw_discr_lower_bound, name);
10292 if (list->dw_discr_range)
10293 output_discr_value (&list->dw_discr_upper_bound, name);
10295 break;
10298 default:
10299 gcc_unreachable ();
10303 FOR_EACH_CHILD (die, c, output_die (c));
10305 /* Add null byte to terminate sibling list. */
10306 if (die->die_child != NULL)
10307 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
10308 (unsigned long) die->die_offset);
10311 /* Output the compilation unit that appears at the beginning of the
10312 .debug_info section, and precedes the DIE descriptions. */
10314 static void
10315 output_compilation_unit_header (enum dwarf_unit_type ut)
10317 if (!XCOFF_DEBUGGING_INFO)
10319 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10320 dw2_asm_output_data (4, 0xffffffff,
10321 "Initial length escape value indicating 64-bit DWARF extension");
10322 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10323 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
10324 "Length of Compilation Unit Info");
10327 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10328 if (dwarf_version >= 5)
10330 const char *name;
10331 switch (ut)
10333 case DW_UT_compile: name = "DW_UT_compile"; break;
10334 case DW_UT_type: name = "DW_UT_type"; break;
10335 case DW_UT_split_compile: name = "DW_UT_split_compile"; break;
10336 case DW_UT_split_type: name = "DW_UT_split_type"; break;
10337 default: gcc_unreachable ();
10339 dw2_asm_output_data (1, ut, "%s", name);
10340 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10342 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
10343 debug_abbrev_section,
10344 "Offset Into Abbrev. Section");
10345 if (dwarf_version < 5)
10346 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10349 /* Output the compilation unit DIE and its children. */
10351 static void
10352 output_comp_unit (dw_die_ref die, int output_if_empty,
10353 const unsigned char *dwo_id)
10355 const char *secname, *oldsym;
10356 char *tmp;
10358 /* Unless we are outputting main CU, we may throw away empty ones. */
10359 if (!output_if_empty && die->die_child == NULL)
10360 return;
10362 /* Even if there are no children of this DIE, we must output the information
10363 about the compilation unit. Otherwise, on an empty translation unit, we
10364 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
10365 will then complain when examining the file. First mark all the DIEs in
10366 this CU so we know which get local refs. */
10367 mark_dies (die);
10369 external_ref_hash_type *extern_map = optimize_external_refs (die);
10371 /* For now, optimize only the main CU, in order to optimize the rest
10372 we'd need to see all of them earlier. Leave the rest for post-linking
10373 tools like DWZ. */
10374 if (die == comp_unit_die ())
10375 abbrev_opt_start = vec_safe_length (abbrev_die_table);
10377 build_abbrev_table (die, extern_map);
10379 optimize_abbrev_table ();
10381 delete extern_map;
10383 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10384 next_die_offset = (dwo_id
10385 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
10386 : DWARF_COMPILE_UNIT_HEADER_SIZE);
10387 calc_die_sizes (die);
10389 oldsym = die->die_id.die_symbol;
10390 if (oldsym)
10392 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
10394 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
10395 secname = tmp;
10396 die->die_id.die_symbol = NULL;
10397 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10399 else
10401 switch_to_section (debug_info_section);
10402 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
10403 info_section_emitted = true;
10406 /* Output debugging information. */
10407 output_compilation_unit_header (dwo_id
10408 ? DW_UT_split_compile : DW_UT_compile);
10409 if (dwarf_version >= 5)
10411 if (dwo_id != NULL)
10412 for (int i = 0; i < 8; i++)
10413 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10415 output_die (die);
10417 /* Leave the marks on the main CU, so we can check them in
10418 output_pubnames. */
10419 if (oldsym)
10421 unmark_dies (die);
10422 die->die_id.die_symbol = oldsym;
10426 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
10427 and .debug_pubtypes. This is configured per-target, but can be
10428 overridden by the -gpubnames or -gno-pubnames options. */
10430 static inline bool
10431 want_pubnames (void)
10433 if (debug_info_level <= DINFO_LEVEL_TERSE)
10434 return false;
10435 if (debug_generate_pub_sections != -1)
10436 return debug_generate_pub_sections;
10437 return targetm.want_debug_pub_sections;
10440 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
10442 static void
10443 add_AT_pubnames (dw_die_ref die)
10445 if (want_pubnames ())
10446 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
10449 /* Add a string attribute value to a skeleton DIE. */
10451 static inline void
10452 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
10453 const char *str)
10455 dw_attr_node attr;
10456 struct indirect_string_node *node;
10458 if (! skeleton_debug_str_hash)
10459 skeleton_debug_str_hash
10460 = hash_table<indirect_string_hasher>::create_ggc (10);
10462 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
10463 find_string_form (node);
10464 if (node->form == DW_FORM_GNU_str_index)
10465 node->form = DW_FORM_strp;
10467 attr.dw_attr = attr_kind;
10468 attr.dw_attr_val.val_class = dw_val_class_str;
10469 attr.dw_attr_val.val_entry = NULL;
10470 attr.dw_attr_val.v.val_str = node;
10471 add_dwarf_attr (die, &attr);
10474 /* Helper function to generate top-level dies for skeleton debug_info and
10475 debug_types. */
10477 static void
10478 add_top_level_skeleton_die_attrs (dw_die_ref die)
10480 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
10481 const char *comp_dir = comp_dir_string ();
10483 add_skeleton_AT_string (die, dwarf_AT (DW_AT_dwo_name), dwo_file_name);
10484 if (comp_dir != NULL)
10485 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
10486 add_AT_pubnames (die);
10487 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
10490 /* Output skeleton debug sections that point to the dwo file. */
10492 static void
10493 output_skeleton_debug_sections (dw_die_ref comp_unit,
10494 const unsigned char *dwo_id)
10496 /* These attributes will be found in the full debug_info section. */
10497 remove_AT (comp_unit, DW_AT_producer);
10498 remove_AT (comp_unit, DW_AT_language);
10500 switch_to_section (debug_skeleton_info_section);
10501 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
10503 /* Produce the skeleton compilation-unit header. This one differs enough from
10504 a normal CU header that it's better not to call output_compilation_unit
10505 header. */
10506 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10507 dw2_asm_output_data (4, 0xffffffff,
10508 "Initial length escape value indicating 64-bit "
10509 "DWARF extension");
10511 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10512 DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
10513 - DWARF_INITIAL_LENGTH_SIZE
10514 + size_of_die (comp_unit),
10515 "Length of Compilation Unit Info");
10516 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10517 if (dwarf_version >= 5)
10519 dw2_asm_output_data (1, DW_UT_skeleton, "DW_UT_skeleton");
10520 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10522 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
10523 debug_skeleton_abbrev_section,
10524 "Offset Into Abbrev. Section");
10525 if (dwarf_version < 5)
10526 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10527 else
10528 for (int i = 0; i < 8; i++)
10529 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10531 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
10532 output_die (comp_unit);
10534 /* Build the skeleton debug_abbrev section. */
10535 switch_to_section (debug_skeleton_abbrev_section);
10536 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
10538 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
10540 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
10543 /* Output a comdat type unit DIE and its children. */
10545 static void
10546 output_comdat_type_unit (comdat_type_node *node)
10548 const char *secname;
10549 char *tmp;
10550 int i;
10551 #if defined (OBJECT_FORMAT_ELF)
10552 tree comdat_key;
10553 #endif
10555 /* First mark all the DIEs in this CU so we know which get local refs. */
10556 mark_dies (node->root_die);
10558 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
10560 build_abbrev_table (node->root_die, extern_map);
10562 delete extern_map;
10563 extern_map = NULL;
10565 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10566 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
10567 calc_die_sizes (node->root_die);
10569 #if defined (OBJECT_FORMAT_ELF)
10570 if (dwarf_version >= 5)
10572 if (!dwarf_split_debug_info)
10573 secname = ".debug_info";
10574 else
10575 secname = ".debug_info.dwo";
10577 else if (!dwarf_split_debug_info)
10578 secname = ".debug_types";
10579 else
10580 secname = ".debug_types.dwo";
10582 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10583 sprintf (tmp, dwarf_version >= 5 ? "wi." : "wt.");
10584 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10585 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
10586 comdat_key = get_identifier (tmp);
10587 targetm.asm_out.named_section (secname,
10588 SECTION_DEBUG | SECTION_LINKONCE,
10589 comdat_key);
10590 #else
10591 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10592 sprintf (tmp, (dwarf_version >= 5
10593 ? ".gnu.linkonce.wi." : ".gnu.linkonce.wt."));
10594 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10595 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
10596 secname = tmp;
10597 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10598 #endif
10600 /* Output debugging information. */
10601 output_compilation_unit_header (dwarf_split_debug_info
10602 ? DW_UT_split_type : DW_UT_type);
10603 output_signature (node->signature, "Type Signature");
10604 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
10605 "Offset to Type DIE");
10606 output_die (node->root_die);
10608 unmark_dies (node->root_die);
10611 /* Return the DWARF2/3 pubname associated with a decl. */
10613 static const char *
10614 dwarf2_name (tree decl, int scope)
10616 if (DECL_NAMELESS (decl))
10617 return NULL;
10618 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
10621 /* Add a new entry to .debug_pubnames if appropriate. */
10623 static void
10624 add_pubname_string (const char *str, dw_die_ref die)
10626 pubname_entry e;
10628 e.die = die;
10629 e.name = xstrdup (str);
10630 vec_safe_push (pubname_table, e);
10633 static void
10634 add_pubname (tree decl, dw_die_ref die)
10636 if (!want_pubnames ())
10637 return;
10639 /* Don't add items to the table when we expect that the consumer will have
10640 just read the enclosing die. For example, if the consumer is looking at a
10641 class_member, it will either be inside the class already, or will have just
10642 looked up the class to find the member. Either way, searching the class is
10643 faster than searching the index. */
10644 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
10645 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10647 const char *name = dwarf2_name (decl, 1);
10649 if (name)
10650 add_pubname_string (name, die);
10654 /* Add an enumerator to the pubnames section. */
10656 static void
10657 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
10659 pubname_entry e;
10661 gcc_assert (scope_name);
10662 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
10663 e.die = die;
10664 vec_safe_push (pubname_table, e);
10667 /* Add a new entry to .debug_pubtypes if appropriate. */
10669 static void
10670 add_pubtype (tree decl, dw_die_ref die)
10672 pubname_entry e;
10674 if (!want_pubnames ())
10675 return;
10677 if ((TREE_PUBLIC (decl)
10678 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10679 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
10681 tree scope = NULL;
10682 const char *scope_name = "";
10683 const char *sep = is_cxx () ? "::" : ".";
10684 const char *name;
10686 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
10687 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
10689 scope_name = lang_hooks.dwarf_name (scope, 1);
10690 if (scope_name != NULL && scope_name[0] != '\0')
10691 scope_name = concat (scope_name, sep, NULL);
10692 else
10693 scope_name = "";
10696 if (TYPE_P (decl))
10697 name = type_tag (decl);
10698 else
10699 name = lang_hooks.dwarf_name (decl, 1);
10701 /* If we don't have a name for the type, there's no point in adding
10702 it to the table. */
10703 if (name != NULL && name[0] != '\0')
10705 e.die = die;
10706 e.name = concat (scope_name, name, NULL);
10707 vec_safe_push (pubtype_table, e);
10710 /* Although it might be more consistent to add the pubinfo for the
10711 enumerators as their dies are created, they should only be added if the
10712 enum type meets the criteria above. So rather than re-check the parent
10713 enum type whenever an enumerator die is created, just output them all
10714 here. This isn't protected by the name conditional because anonymous
10715 enums don't have names. */
10716 if (die->die_tag == DW_TAG_enumeration_type)
10718 dw_die_ref c;
10720 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
10725 /* Output a single entry in the pubnames table. */
10727 static void
10728 output_pubname (dw_offset die_offset, pubname_entry *entry)
10730 dw_die_ref die = entry->die;
10731 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
10733 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
10735 if (debug_generate_pub_sections == 2)
10737 /* This logic follows gdb's method for determining the value of the flag
10738 byte. */
10739 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
10740 switch (die->die_tag)
10742 case DW_TAG_typedef:
10743 case DW_TAG_base_type:
10744 case DW_TAG_subrange_type:
10745 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10746 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10747 break;
10748 case DW_TAG_enumerator:
10749 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10750 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10751 if (!is_cxx ())
10752 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10753 break;
10754 case DW_TAG_subprogram:
10755 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10756 GDB_INDEX_SYMBOL_KIND_FUNCTION);
10757 if (!is_ada ())
10758 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10759 break;
10760 case DW_TAG_constant:
10761 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10762 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10763 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10764 break;
10765 case DW_TAG_variable:
10766 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10767 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10768 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10769 break;
10770 case DW_TAG_namespace:
10771 case DW_TAG_imported_declaration:
10772 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10773 break;
10774 case DW_TAG_class_type:
10775 case DW_TAG_interface_type:
10776 case DW_TAG_structure_type:
10777 case DW_TAG_union_type:
10778 case DW_TAG_enumeration_type:
10779 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10780 if (!is_cxx ())
10781 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10782 break;
10783 default:
10784 /* An unusual tag. Leave the flag-byte empty. */
10785 break;
10787 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
10788 "GDB-index flags");
10791 dw2_asm_output_nstring (entry->name, -1, "external name");
10795 /* Output the public names table used to speed up access to externally
10796 visible names; or the public types table used to find type definitions. */
10798 static void
10799 output_pubnames (vec<pubname_entry, va_gc> *names)
10801 unsigned i;
10802 unsigned long pubnames_length = size_of_pubnames (names);
10803 pubname_entry *pub;
10805 if (!XCOFF_DEBUGGING_INFO)
10807 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10808 dw2_asm_output_data (4, 0xffffffff,
10809 "Initial length escape value indicating 64-bit DWARF extension");
10810 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
10811 "Pub Info Length");
10814 /* Version number for pubnames/pubtypes is independent of dwarf version. */
10815 dw2_asm_output_data (2, 2, "DWARF Version");
10817 if (dwarf_split_debug_info)
10818 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10819 debug_skeleton_info_section,
10820 "Offset of Compilation Unit Info");
10821 else
10822 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10823 debug_info_section,
10824 "Offset of Compilation Unit Info");
10825 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
10826 "Compilation Unit Length");
10828 FOR_EACH_VEC_ELT (*names, i, pub)
10830 if (include_pubname_in_output (names, pub))
10832 dw_offset die_offset = pub->die->die_offset;
10834 /* We shouldn't see pubnames for DIEs outside of the main CU. */
10835 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
10836 gcc_assert (pub->die->die_mark);
10838 /* If we're putting types in their own .debug_types sections,
10839 the .debug_pubtypes table will still point to the compile
10840 unit (not the type unit), so we want to use the offset of
10841 the skeleton DIE (if there is one). */
10842 if (pub->die->comdat_type_p && names == pubtype_table)
10844 comdat_type_node *type_node = pub->die->die_id.die_type_node;
10846 if (type_node != NULL)
10847 die_offset = (type_node->skeleton_die != NULL
10848 ? type_node->skeleton_die->die_offset
10849 : comp_unit_die ()->die_offset);
10852 output_pubname (die_offset, pub);
10856 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
10859 /* Output public names and types tables if necessary. */
10861 static void
10862 output_pubtables (void)
10864 if (!want_pubnames () || !info_section_emitted)
10865 return;
10867 switch_to_section (debug_pubnames_section);
10868 output_pubnames (pubname_table);
10869 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
10870 It shouldn't hurt to emit it always, since pure DWARF2 consumers
10871 simply won't look for the section. */
10872 switch_to_section (debug_pubtypes_section);
10873 output_pubnames (pubtype_table);
10877 /* Output the information that goes into the .debug_aranges table.
10878 Namely, define the beginning and ending address range of the
10879 text section generated for this compilation unit. */
10881 static void
10882 output_aranges (void)
10884 unsigned i;
10885 unsigned long aranges_length = size_of_aranges ();
10887 if (!XCOFF_DEBUGGING_INFO)
10889 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10890 dw2_asm_output_data (4, 0xffffffff,
10891 "Initial length escape value indicating 64-bit DWARF extension");
10892 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
10893 "Length of Address Ranges Info");
10896 /* Version number for aranges is still 2, even up to DWARF5. */
10897 dw2_asm_output_data (2, 2, "DWARF Version");
10898 if (dwarf_split_debug_info)
10899 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10900 debug_skeleton_info_section,
10901 "Offset of Compilation Unit Info");
10902 else
10903 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10904 debug_info_section,
10905 "Offset of Compilation Unit Info");
10906 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
10907 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
10909 /* We need to align to twice the pointer size here. */
10910 if (DWARF_ARANGES_PAD_SIZE)
10912 /* Pad using a 2 byte words so that padding is correct for any
10913 pointer size. */
10914 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
10915 2 * DWARF2_ADDR_SIZE);
10916 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
10917 dw2_asm_output_data (2, 0, NULL);
10920 /* It is necessary not to output these entries if the sections were
10921 not used; if the sections were not used, the length will be 0 and
10922 the address may end up as 0 if the section is discarded by ld
10923 --gc-sections, leaving an invalid (0, 0) entry that can be
10924 confused with the terminator. */
10925 if (text_section_used)
10927 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
10928 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
10929 text_section_label, "Length");
10931 if (cold_text_section_used)
10933 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
10934 "Address");
10935 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
10936 cold_text_section_label, "Length");
10939 if (have_multiple_function_sections)
10941 unsigned fde_idx;
10942 dw_fde_ref fde;
10944 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
10946 if (DECL_IGNORED_P (fde->decl))
10947 continue;
10948 if (!fde->in_std_section)
10950 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
10951 "Address");
10952 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
10953 fde->dw_fde_begin, "Length");
10955 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
10957 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
10958 "Address");
10959 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
10960 fde->dw_fde_second_begin, "Length");
10965 /* Output the terminator words. */
10966 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10967 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10970 /* Add a new entry to .debug_ranges. Return its index into
10971 ranges_table vector. */
10973 static unsigned int
10974 add_ranges_num (int num, bool maybe_new_sec)
10976 dw_ranges r = { NULL, num, 0, maybe_new_sec };
10977 vec_safe_push (ranges_table, r);
10978 return vec_safe_length (ranges_table) - 1;
10981 /* Add a new entry to .debug_ranges corresponding to a block, or a
10982 range terminator if BLOCK is NULL. MAYBE_NEW_SEC is true if
10983 this entry might be in a different section from previous range. */
10985 static unsigned int
10986 add_ranges (const_tree block, bool maybe_new_sec)
10988 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0, maybe_new_sec);
10991 /* Note that (*rnglist_table)[offset] is either a head of a rnglist
10992 chain, or middle entry of a chain that will be directly referred to. */
10994 static void
10995 note_rnglist_head (unsigned int offset)
10997 if (dwarf_version < 5 || (*ranges_table)[offset].label)
10998 return;
10999 (*ranges_table)[offset].label = gen_internal_sym ("LLRL");
11002 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
11003 When using dwarf_split_debug_info, address attributes in dies destined
11004 for the final executable should be direct references--setting the
11005 parameter force_direct ensures this behavior. */
11007 static void
11008 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11009 bool *added, bool force_direct)
11011 unsigned int in_use = vec_safe_length (ranges_by_label);
11012 unsigned int offset;
11013 dw_ranges_by_label rbl = { begin, end };
11014 vec_safe_push (ranges_by_label, rbl);
11015 offset = add_ranges_num (-(int)in_use - 1, true);
11016 if (!*added)
11018 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
11019 *added = true;
11020 note_rnglist_head (offset);
11024 /* Emit .debug_ranges section. */
11026 static void
11027 output_ranges (void)
11029 unsigned i;
11030 static const char *const start_fmt = "Offset %#x";
11031 const char *fmt = start_fmt;
11032 dw_ranges *r;
11034 switch_to_section (debug_ranges_section);
11035 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11036 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11038 int block_num = r->num;
11040 if (block_num > 0)
11042 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11043 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11045 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11046 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11048 /* If all code is in the text section, then the compilation
11049 unit base address defaults to DW_AT_low_pc, which is the
11050 base of the text section. */
11051 if (!have_multiple_function_sections)
11053 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11054 text_section_label,
11055 fmt, i * 2 * DWARF2_ADDR_SIZE);
11056 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11057 text_section_label, NULL);
11060 /* Otherwise, the compilation unit base address is zero,
11061 which allows us to use absolute addresses, and not worry
11062 about whether the target supports cross-section
11063 arithmetic. */
11064 else
11066 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11067 fmt, i * 2 * DWARF2_ADDR_SIZE);
11068 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11071 fmt = NULL;
11074 /* Negative block_num stands for an index into ranges_by_label. */
11075 else if (block_num < 0)
11077 int lab_idx = - block_num - 1;
11079 if (!have_multiple_function_sections)
11081 gcc_unreachable ();
11082 #if 0
11083 /* If we ever use add_ranges_by_labels () for a single
11084 function section, all we have to do is to take out
11085 the #if 0 above. */
11086 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11087 (*ranges_by_label)[lab_idx].begin,
11088 text_section_label,
11089 fmt, i * 2 * DWARF2_ADDR_SIZE);
11090 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11091 (*ranges_by_label)[lab_idx].end,
11092 text_section_label, NULL);
11093 #endif
11095 else
11097 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11098 (*ranges_by_label)[lab_idx].begin,
11099 fmt, i * 2 * DWARF2_ADDR_SIZE);
11100 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11101 (*ranges_by_label)[lab_idx].end,
11102 NULL);
11105 else
11107 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11108 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11109 fmt = start_fmt;
11114 /* Non-zero if .debug_line_str should be used for .debug_line section
11115 strings or strings that are likely shareable with those. */
11116 #define DWARF5_USE_DEBUG_LINE_STR \
11117 (!DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET \
11118 && (DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) != 0 \
11119 /* FIXME: there is no .debug_line_str.dwo section, \
11120 for -gsplit-dwarf we should use DW_FORM_strx instead. */ \
11121 && !dwarf_split_debug_info)
11123 /* Assign .debug_rnglists indexes. */
11125 static void
11126 index_rnglists (void)
11128 unsigned i;
11129 dw_ranges *r;
11131 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11132 if (r->label)
11133 r->idx = rnglist_idx++;
11136 /* Emit .debug_rnglists section. */
11138 static void
11139 output_rnglists (void)
11141 unsigned i;
11142 dw_ranges *r;
11143 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
11144 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
11145 char basebuf[MAX_ARTIFICIAL_LABEL_BYTES];
11147 switch_to_section (debug_ranges_section);
11148 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11149 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_RANGES_SECTION_LABEL, 2);
11150 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_RANGES_SECTION_LABEL, 3);
11151 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11152 dw2_asm_output_data (4, 0xffffffff,
11153 "Initial length escape value indicating "
11154 "64-bit DWARF extension");
11155 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11156 "Length of Range Lists");
11157 ASM_OUTPUT_LABEL (asm_out_file, l1);
11158 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11159 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11160 dw2_asm_output_data (1, 0, "Segment Size");
11161 /* Emit the offset table only for -gsplit-dwarf. If we don't care
11162 about relocation sizes and primarily care about the size of .debug*
11163 sections in linked shared libraries and executables, then
11164 the offset table plus corresponding DW_FORM_rnglistx uleb128 indexes
11165 into it are usually larger than just DW_FORM_sec_offset offsets
11166 into the .debug_rnglists section. */
11167 dw2_asm_output_data (4, dwarf_split_debug_info ? rnglist_idx : 0,
11168 "Offset Entry Count");
11169 if (dwarf_split_debug_info)
11171 ASM_OUTPUT_LABEL (asm_out_file, ranges_base_label);
11172 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11173 if (r->label)
11174 dw2_asm_output_delta (DWARF_OFFSET_SIZE, r->label,
11175 ranges_base_label, NULL);
11178 const char *lab = "";
11179 unsigned int len = vec_safe_length (ranges_table);
11180 const char *base = NULL;
11181 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11183 int block_num = r->num;
11185 if (r->label)
11187 ASM_OUTPUT_LABEL (asm_out_file, r->label);
11188 lab = r->label;
11190 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
11191 base = NULL;
11192 if (block_num > 0)
11194 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11195 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11197 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11198 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11200 if (HAVE_AS_LEB128)
11202 /* If all code is in the text section, then the compilation
11203 unit base address defaults to DW_AT_low_pc, which is the
11204 base of the text section. */
11205 if (!have_multiple_function_sections)
11207 dw2_asm_output_data (1, DW_RLE_offset_pair,
11208 "DW_RLE_offset_pair (%s)", lab);
11209 dw2_asm_output_delta_uleb128 (blabel, text_section_label,
11210 "Range begin address (%s)", lab);
11211 dw2_asm_output_delta_uleb128 (elabel, text_section_label,
11212 "Range end address (%s)", lab);
11213 continue;
11215 if (base == NULL)
11217 dw_ranges *r2 = NULL;
11218 if (i < len - 1)
11219 r2 = &(*ranges_table)[i + 1];
11220 if (r2
11221 && r2->num != 0
11222 && r2->label == NULL
11223 && !r2->maybe_new_sec)
11225 dw2_asm_output_data (1, DW_RLE_base_address,
11226 "DW_RLE_base_address (%s)", lab);
11227 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11228 "Base address (%s)", lab);
11229 strcpy (basebuf, blabel);
11230 base = basebuf;
11233 if (base)
11235 dw2_asm_output_data (1, DW_RLE_offset_pair,
11236 "DW_RLE_offset_pair (%s)", lab);
11237 dw2_asm_output_delta_uleb128 (blabel, base,
11238 "Range begin address (%s)", lab);
11239 dw2_asm_output_delta_uleb128 (elabel, base,
11240 "Range end address (%s)", lab);
11241 continue;
11243 dw2_asm_output_data (1, DW_RLE_start_length,
11244 "DW_RLE_start_length (%s)", lab);
11245 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11246 "Range begin address (%s)", lab);
11247 dw2_asm_output_delta_uleb128 (elabel, blabel,
11248 "Range length (%s)", lab);
11250 else
11252 dw2_asm_output_data (1, DW_RLE_start_end,
11253 "DW_RLE_start_end (%s)", lab);
11254 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11255 "Range begin address (%s)", lab);
11256 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11257 "Range end address (%s)", lab);
11261 /* Negative block_num stands for an index into ranges_by_label. */
11262 else if (block_num < 0)
11264 int lab_idx = - block_num - 1;
11265 const char *blabel = (*ranges_by_label)[lab_idx].begin;
11266 const char *elabel = (*ranges_by_label)[lab_idx].end;
11268 if (!have_multiple_function_sections)
11269 gcc_unreachable ();
11270 if (HAVE_AS_LEB128)
11272 dw2_asm_output_data (1, DW_RLE_start_length,
11273 "DW_RLE_start_length (%s)", lab);
11274 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11275 "Range begin address (%s)", lab);
11276 dw2_asm_output_delta_uleb128 (elabel, blabel,
11277 "Range length (%s)", lab);
11279 else
11281 dw2_asm_output_data (1, DW_RLE_start_end,
11282 "DW_RLE_start_end (%s)", lab);
11283 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11284 "Range begin address (%s)", lab);
11285 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11286 "Range end address (%s)", lab);
11289 else
11290 dw2_asm_output_data (1, DW_RLE_end_of_list,
11291 "DW_RLE_end_of_list (%s)", lab);
11293 ASM_OUTPUT_LABEL (asm_out_file, l2);
11296 /* Data structure containing information about input files. */
11297 struct file_info
11299 const char *path; /* Complete file name. */
11300 const char *fname; /* File name part. */
11301 int length; /* Length of entire string. */
11302 struct dwarf_file_data * file_idx; /* Index in input file table. */
11303 int dir_idx; /* Index in directory table. */
11306 /* Data structure containing information about directories with source
11307 files. */
11308 struct dir_info
11310 const char *path; /* Path including directory name. */
11311 int length; /* Path length. */
11312 int prefix; /* Index of directory entry which is a prefix. */
11313 int count; /* Number of files in this directory. */
11314 int dir_idx; /* Index of directory used as base. */
11317 /* Callback function for file_info comparison. We sort by looking at
11318 the directories in the path. */
11320 static int
11321 file_info_cmp (const void *p1, const void *p2)
11323 const struct file_info *const s1 = (const struct file_info *) p1;
11324 const struct file_info *const s2 = (const struct file_info *) p2;
11325 const unsigned char *cp1;
11326 const unsigned char *cp2;
11328 /* Take care of file names without directories. We need to make sure that
11329 we return consistent values to qsort since some will get confused if
11330 we return the same value when identical operands are passed in opposite
11331 orders. So if neither has a directory, return 0 and otherwise return
11332 1 or -1 depending on which one has the directory. */
11333 if ((s1->path == s1->fname || s2->path == s2->fname))
11334 return (s2->path == s2->fname) - (s1->path == s1->fname);
11336 cp1 = (const unsigned char *) s1->path;
11337 cp2 = (const unsigned char *) s2->path;
11339 while (1)
11341 ++cp1;
11342 ++cp2;
11343 /* Reached the end of the first path? If so, handle like above. */
11344 if ((cp1 == (const unsigned char *) s1->fname)
11345 || (cp2 == (const unsigned char *) s2->fname))
11346 return ((cp2 == (const unsigned char *) s2->fname)
11347 - (cp1 == (const unsigned char *) s1->fname));
11349 /* Character of current path component the same? */
11350 else if (*cp1 != *cp2)
11351 return *cp1 - *cp2;
11355 struct file_name_acquire_data
11357 struct file_info *files;
11358 int used_files;
11359 int max_files;
11362 /* Traversal function for the hash table. */
11365 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
11367 struct dwarf_file_data *d = *slot;
11368 struct file_info *fi;
11369 const char *f;
11371 gcc_assert (fnad->max_files >= d->emitted_number);
11373 if (! d->emitted_number)
11374 return 1;
11376 gcc_assert (fnad->max_files != fnad->used_files);
11378 fi = fnad->files + fnad->used_files++;
11380 /* Skip all leading "./". */
11381 f = d->filename;
11382 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
11383 f += 2;
11385 /* Create a new array entry. */
11386 fi->path = f;
11387 fi->length = strlen (f);
11388 fi->file_idx = d;
11390 /* Search for the file name part. */
11391 f = strrchr (f, DIR_SEPARATOR);
11392 #if defined (DIR_SEPARATOR_2)
11394 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
11396 if (g != NULL)
11398 if (f == NULL || f < g)
11399 f = g;
11402 #endif
11404 fi->fname = f == NULL ? fi->path : f + 1;
11405 return 1;
11408 /* Helper function for output_file_names. Emit a FORM encoded
11409 string STR, with assembly comment start ENTRY_KIND and
11410 index IDX */
11412 static void
11413 output_line_string (enum dwarf_form form, const char *str,
11414 const char *entry_kind, unsigned int idx)
11416 switch (form)
11418 case DW_FORM_string:
11419 dw2_asm_output_nstring (str, -1, "%s: %#x", entry_kind, idx);
11420 break;
11421 case DW_FORM_line_strp:
11422 if (!debug_line_str_hash)
11423 debug_line_str_hash
11424 = hash_table<indirect_string_hasher>::create_ggc (10);
11426 struct indirect_string_node *node;
11427 node = find_AT_string_in_table (str, debug_line_str_hash);
11428 set_indirect_string (node);
11429 node->form = form;
11430 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
11431 debug_line_str_section, "%s: %#x: \"%s\"",
11432 entry_kind, 0, node->str);
11433 break;
11434 default:
11435 gcc_unreachable ();
11439 /* Output the directory table and the file name table. We try to minimize
11440 the total amount of memory needed. A heuristic is used to avoid large
11441 slowdowns with many input files. */
11443 static void
11444 output_file_names (void)
11446 struct file_name_acquire_data fnad;
11447 int numfiles;
11448 struct file_info *files;
11449 struct dir_info *dirs;
11450 int *saved;
11451 int *savehere;
11452 int *backmap;
11453 int ndirs;
11454 int idx_offset;
11455 int i;
11457 if (!last_emitted_file)
11459 if (dwarf_version >= 5)
11461 dw2_asm_output_data (1, 0, "Directory entry format count");
11462 dw2_asm_output_data_uleb128 (0, "Directories count");
11463 dw2_asm_output_data (1, 0, "File name entry format count");
11464 dw2_asm_output_data_uleb128 (0, "File names count");
11466 else
11468 dw2_asm_output_data (1, 0, "End directory table");
11469 dw2_asm_output_data (1, 0, "End file name table");
11471 return;
11474 numfiles = last_emitted_file->emitted_number;
11476 /* Allocate the various arrays we need. */
11477 files = XALLOCAVEC (struct file_info, numfiles);
11478 dirs = XALLOCAVEC (struct dir_info, numfiles);
11480 fnad.files = files;
11481 fnad.used_files = 0;
11482 fnad.max_files = numfiles;
11483 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
11484 gcc_assert (fnad.used_files == fnad.max_files);
11486 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
11488 /* Find all the different directories used. */
11489 dirs[0].path = files[0].path;
11490 dirs[0].length = files[0].fname - files[0].path;
11491 dirs[0].prefix = -1;
11492 dirs[0].count = 1;
11493 dirs[0].dir_idx = 0;
11494 files[0].dir_idx = 0;
11495 ndirs = 1;
11497 for (i = 1; i < numfiles; i++)
11498 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
11499 && memcmp (dirs[ndirs - 1].path, files[i].path,
11500 dirs[ndirs - 1].length) == 0)
11502 /* Same directory as last entry. */
11503 files[i].dir_idx = ndirs - 1;
11504 ++dirs[ndirs - 1].count;
11506 else
11508 int j;
11510 /* This is a new directory. */
11511 dirs[ndirs].path = files[i].path;
11512 dirs[ndirs].length = files[i].fname - files[i].path;
11513 dirs[ndirs].count = 1;
11514 dirs[ndirs].dir_idx = ndirs;
11515 files[i].dir_idx = ndirs;
11517 /* Search for a prefix. */
11518 dirs[ndirs].prefix = -1;
11519 for (j = 0; j < ndirs; j++)
11520 if (dirs[j].length < dirs[ndirs].length
11521 && dirs[j].length > 1
11522 && (dirs[ndirs].prefix == -1
11523 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
11524 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
11525 dirs[ndirs].prefix = j;
11527 ++ndirs;
11530 /* Now to the actual work. We have to find a subset of the directories which
11531 allow expressing the file name using references to the directory table
11532 with the least amount of characters. We do not do an exhaustive search
11533 where we would have to check out every combination of every single
11534 possible prefix. Instead we use a heuristic which provides nearly optimal
11535 results in most cases and never is much off. */
11536 saved = XALLOCAVEC (int, ndirs);
11537 savehere = XALLOCAVEC (int, ndirs);
11539 memset (saved, '\0', ndirs * sizeof (saved[0]));
11540 for (i = 0; i < ndirs; i++)
11542 int j;
11543 int total;
11545 /* We can always save some space for the current directory. But this
11546 does not mean it will be enough to justify adding the directory. */
11547 savehere[i] = dirs[i].length;
11548 total = (savehere[i] - saved[i]) * dirs[i].count;
11550 for (j = i + 1; j < ndirs; j++)
11552 savehere[j] = 0;
11553 if (saved[j] < dirs[i].length)
11555 /* Determine whether the dirs[i] path is a prefix of the
11556 dirs[j] path. */
11557 int k;
11559 k = dirs[j].prefix;
11560 while (k != -1 && k != (int) i)
11561 k = dirs[k].prefix;
11563 if (k == (int) i)
11565 /* Yes it is. We can possibly save some memory by
11566 writing the filenames in dirs[j] relative to
11567 dirs[i]. */
11568 savehere[j] = dirs[i].length;
11569 total += (savehere[j] - saved[j]) * dirs[j].count;
11574 /* Check whether we can save enough to justify adding the dirs[i]
11575 directory. */
11576 if (total > dirs[i].length + 1)
11578 /* It's worthwhile adding. */
11579 for (j = i; j < ndirs; j++)
11580 if (savehere[j] > 0)
11582 /* Remember how much we saved for this directory so far. */
11583 saved[j] = savehere[j];
11585 /* Remember the prefix directory. */
11586 dirs[j].dir_idx = i;
11591 /* Emit the directory name table. */
11592 idx_offset = dirs[0].length > 0 ? 1 : 0;
11593 enum dwarf_form str_form = DW_FORM_string;
11594 enum dwarf_form idx_form = DW_FORM_udata;
11595 if (dwarf_version >= 5)
11597 const char *comp_dir = comp_dir_string ();
11598 if (comp_dir == NULL)
11599 comp_dir = "";
11600 dw2_asm_output_data (1, 1, "Directory entry format count");
11601 if (DWARF5_USE_DEBUG_LINE_STR)
11602 str_form = DW_FORM_line_strp;
11603 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11604 dw2_asm_output_data_uleb128 (str_form, "%s",
11605 get_DW_FORM_name (str_form));
11606 dw2_asm_output_data_uleb128 (ndirs + idx_offset, "Directories count");
11607 if (str_form == DW_FORM_string)
11609 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
11610 for (i = 1 - idx_offset; i < ndirs; i++)
11611 dw2_asm_output_nstring (dirs[i].path,
11612 dirs[i].length
11613 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11614 "Directory Entry: %#x", i + idx_offset);
11616 else
11618 output_line_string (str_form, comp_dir, "Directory Entry", 0);
11619 for (i = 1 - idx_offset; i < ndirs; i++)
11621 const char *str
11622 = ggc_alloc_string (dirs[i].path,
11623 dirs[i].length
11624 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR);
11625 output_line_string (str_form, str, "Directory Entry",
11626 (unsigned) i + idx_offset);
11630 else
11632 for (i = 1 - idx_offset; i < ndirs; i++)
11633 dw2_asm_output_nstring (dirs[i].path,
11634 dirs[i].length
11635 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11636 "Directory Entry: %#x", i + idx_offset);
11638 dw2_asm_output_data (1, 0, "End directory table");
11641 /* We have to emit them in the order of emitted_number since that's
11642 used in the debug info generation. To do this efficiently we
11643 generate a back-mapping of the indices first. */
11644 backmap = XALLOCAVEC (int, numfiles);
11645 for (i = 0; i < numfiles; i++)
11646 backmap[files[i].file_idx->emitted_number - 1] = i;
11648 if (dwarf_version >= 5)
11650 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
11651 if (filename0 == NULL)
11652 filename0 = "";
11653 /* DW_LNCT_directory_index can use DW_FORM_udata, DW_FORM_data1 and
11654 DW_FORM_data2. Choose one based on the number of directories
11655 and how much space would they occupy in each encoding.
11656 If we have at most 256 directories, all indexes fit into
11657 a single byte, so DW_FORM_data1 is most compact (if there
11658 are at most 128 directories, DW_FORM_udata would be as
11659 compact as that, but not shorter and slower to decode). */
11660 if (ndirs + idx_offset <= 256)
11661 idx_form = DW_FORM_data1;
11662 /* If there are more than 65536 directories, we have to use
11663 DW_FORM_udata, DW_FORM_data2 can't refer to them.
11664 Otherwise, compute what space would occupy if all the indexes
11665 used DW_FORM_udata - sum - and compare that to how large would
11666 be DW_FORM_data2 encoding, and pick the more efficient one. */
11667 else if (ndirs + idx_offset <= 65536)
11669 unsigned HOST_WIDE_INT sum = 1;
11670 for (i = 0; i < numfiles; i++)
11672 int file_idx = backmap[i];
11673 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11674 sum += size_of_uleb128 (dir_idx);
11676 if (sum >= HOST_WIDE_INT_UC (2) * (numfiles + 1))
11677 idx_form = DW_FORM_data2;
11679 #ifdef VMS_DEBUGGING_INFO
11680 dw2_asm_output_data (1, 4, "File name entry format count");
11681 #else
11682 dw2_asm_output_data (1, 2, "File name entry format count");
11683 #endif
11684 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11685 dw2_asm_output_data_uleb128 (str_form, "%s",
11686 get_DW_FORM_name (str_form));
11687 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
11688 "DW_LNCT_directory_index");
11689 dw2_asm_output_data_uleb128 (idx_form, "%s",
11690 get_DW_FORM_name (idx_form));
11691 #ifdef VMS_DEBUGGING_INFO
11692 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
11693 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11694 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
11695 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11696 #endif
11697 dw2_asm_output_data_uleb128 (numfiles + 1, "File names count");
11699 output_line_string (str_form, filename0, "File Entry", 0);
11701 /* Include directory index. */
11702 if (idx_form != DW_FORM_udata)
11703 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11704 0, NULL);
11705 else
11706 dw2_asm_output_data_uleb128 (0, NULL);
11708 #ifdef VMS_DEBUGGING_INFO
11709 dw2_asm_output_data_uleb128 (0, NULL);
11710 dw2_asm_output_data_uleb128 (0, NULL);
11711 #endif
11714 /* Now write all the file names. */
11715 for (i = 0; i < numfiles; i++)
11717 int file_idx = backmap[i];
11718 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11720 #ifdef VMS_DEBUGGING_INFO
11721 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
11723 /* Setting these fields can lead to debugger miscomparisons,
11724 but VMS Debug requires them to be set correctly. */
11726 int ver;
11727 long long cdt;
11728 long siz;
11729 int maxfilelen = (strlen (files[file_idx].path)
11730 + dirs[dir_idx].length
11731 + MAX_VMS_VERSION_LEN + 1);
11732 char *filebuf = XALLOCAVEC (char, maxfilelen);
11734 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
11735 snprintf (filebuf, maxfilelen, "%s;%d",
11736 files[file_idx].path + dirs[dir_idx].length, ver);
11738 output_line_string (str_form, filebuf, "File Entry", (unsigned) i + 1);
11740 /* Include directory index. */
11741 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11742 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11743 dir_idx + idx_offset, NULL);
11744 else
11745 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11747 /* Modification time. */
11748 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11749 &cdt, 0, 0, 0) == 0)
11750 ? cdt : 0, NULL);
11752 /* File length in bytes. */
11753 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11754 0, &siz, 0, 0) == 0)
11755 ? siz : 0, NULL);
11756 #else
11757 output_line_string (str_form,
11758 files[file_idx].path + dirs[dir_idx].length,
11759 "File Entry", (unsigned) i + 1);
11761 /* Include directory index. */
11762 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11763 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11764 dir_idx + idx_offset, NULL);
11765 else
11766 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11768 if (dwarf_version >= 5)
11769 continue;
11771 /* Modification time. */
11772 dw2_asm_output_data_uleb128 (0, NULL);
11774 /* File length in bytes. */
11775 dw2_asm_output_data_uleb128 (0, NULL);
11776 #endif /* VMS_DEBUGGING_INFO */
11779 if (dwarf_version < 5)
11780 dw2_asm_output_data (1, 0, "End file name table");
11784 /* Output one line number table into the .debug_line section. */
11786 static void
11787 output_one_line_info_table (dw_line_info_table *table)
11789 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
11790 unsigned int current_line = 1;
11791 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
11792 dw_line_info_entry *ent;
11793 size_t i;
11795 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
11797 switch (ent->opcode)
11799 case LI_set_address:
11800 /* ??? Unfortunately, we have little choice here currently, and
11801 must always use the most general form. GCC does not know the
11802 address delta itself, so we can't use DW_LNS_advance_pc. Many
11803 ports do have length attributes which will give an upper bound
11804 on the address range. We could perhaps use length attributes
11805 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
11806 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
11808 /* This can handle any delta. This takes
11809 4+DWARF2_ADDR_SIZE bytes. */
11810 dw2_asm_output_data (1, 0, "set address %s", line_label);
11811 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11812 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11813 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
11814 break;
11816 case LI_set_line:
11817 if (ent->val == current_line)
11819 /* We still need to start a new row, so output a copy insn. */
11820 dw2_asm_output_data (1, DW_LNS_copy,
11821 "copy line %u", current_line);
11823 else
11825 int line_offset = ent->val - current_line;
11826 int line_delta = line_offset - DWARF_LINE_BASE;
11828 current_line = ent->val;
11829 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
11831 /* This can handle deltas from -10 to 234, using the current
11832 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
11833 This takes 1 byte. */
11834 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
11835 "line %u", current_line);
11837 else
11839 /* This can handle any delta. This takes at least 4 bytes,
11840 depending on the value being encoded. */
11841 dw2_asm_output_data (1, DW_LNS_advance_line,
11842 "advance to line %u", current_line);
11843 dw2_asm_output_data_sleb128 (line_offset, NULL);
11844 dw2_asm_output_data (1, DW_LNS_copy, NULL);
11847 break;
11849 case LI_set_file:
11850 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
11851 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11852 break;
11854 case LI_set_column:
11855 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
11856 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11857 break;
11859 case LI_negate_stmt:
11860 current_is_stmt = !current_is_stmt;
11861 dw2_asm_output_data (1, DW_LNS_negate_stmt,
11862 "is_stmt %d", current_is_stmt);
11863 break;
11865 case LI_set_prologue_end:
11866 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
11867 "set prologue end");
11868 break;
11870 case LI_set_epilogue_begin:
11871 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
11872 "set epilogue begin");
11873 break;
11875 case LI_set_discriminator:
11876 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
11877 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
11878 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
11879 dw2_asm_output_data_uleb128 (ent->val, NULL);
11880 break;
11884 /* Emit debug info for the address of the end of the table. */
11885 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
11886 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11887 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11888 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
11890 dw2_asm_output_data (1, 0, "end sequence");
11891 dw2_asm_output_data_uleb128 (1, NULL);
11892 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
11895 /* Output the source line number correspondence information. This
11896 information goes into the .debug_line section. */
11898 static void
11899 output_line_info (bool prologue_only)
11901 static unsigned int generation;
11902 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
11903 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
11904 bool saw_one = false;
11905 int opc;
11907 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, generation);
11908 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, generation);
11909 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, generation);
11910 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, generation++);
11912 if (!XCOFF_DEBUGGING_INFO)
11914 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11915 dw2_asm_output_data (4, 0xffffffff,
11916 "Initial length escape value indicating 64-bit DWARF extension");
11917 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11918 "Length of Source Line Info");
11921 ASM_OUTPUT_LABEL (asm_out_file, l1);
11923 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11924 if (dwarf_version >= 5)
11926 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11927 dw2_asm_output_data (1, 0, "Segment Size");
11929 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
11930 ASM_OUTPUT_LABEL (asm_out_file, p1);
11932 /* Define the architecture-dependent minimum instruction length (in bytes).
11933 In this implementation of DWARF, this field is used for information
11934 purposes only. Since GCC generates assembly language, we have no
11935 a priori knowledge of how many instruction bytes are generated for each
11936 source line, and therefore can use only the DW_LNE_set_address and
11937 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
11938 this as '1', which is "correct enough" for all architectures,
11939 and don't let the target override. */
11940 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
11942 if (dwarf_version >= 4)
11943 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
11944 "Maximum Operations Per Instruction");
11945 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
11946 "Default is_stmt_start flag");
11947 dw2_asm_output_data (1, DWARF_LINE_BASE,
11948 "Line Base Value (Special Opcodes)");
11949 dw2_asm_output_data (1, DWARF_LINE_RANGE,
11950 "Line Range Value (Special Opcodes)");
11951 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
11952 "Special Opcode Base");
11954 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
11956 int n_op_args;
11957 switch (opc)
11959 case DW_LNS_advance_pc:
11960 case DW_LNS_advance_line:
11961 case DW_LNS_set_file:
11962 case DW_LNS_set_column:
11963 case DW_LNS_fixed_advance_pc:
11964 case DW_LNS_set_isa:
11965 n_op_args = 1;
11966 break;
11967 default:
11968 n_op_args = 0;
11969 break;
11972 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
11973 opc, n_op_args);
11976 /* Write out the information about the files we use. */
11977 output_file_names ();
11978 ASM_OUTPUT_LABEL (asm_out_file, p2);
11979 if (prologue_only)
11981 /* Output the marker for the end of the line number info. */
11982 ASM_OUTPUT_LABEL (asm_out_file, l2);
11983 return;
11986 if (separate_line_info)
11988 dw_line_info_table *table;
11989 size_t i;
11991 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
11992 if (table->in_use)
11994 output_one_line_info_table (table);
11995 saw_one = true;
11998 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
12000 output_one_line_info_table (cold_text_section_line_info);
12001 saw_one = true;
12004 /* ??? Some Darwin linkers crash on a .debug_line section with no
12005 sequences. Further, merely a DW_LNE_end_sequence entry is not
12006 sufficient -- the address column must also be initialized.
12007 Make sure to output at least one set_address/end_sequence pair,
12008 choosing .text since that section is always present. */
12009 if (text_section_line_info->in_use || !saw_one)
12010 output_one_line_info_table (text_section_line_info);
12012 /* Output the marker for the end of the line number info. */
12013 ASM_OUTPUT_LABEL (asm_out_file, l2);
12016 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
12018 static inline bool
12019 need_endianity_attribute_p (bool reverse)
12021 return reverse && (dwarf_version >= 3 || !dwarf_strict);
12024 /* Given a pointer to a tree node for some base type, return a pointer to
12025 a DIE that describes the given type. REVERSE is true if the type is
12026 to be interpreted in the reverse storage order wrt the target order.
12028 This routine must only be called for GCC type nodes that correspond to
12029 Dwarf base (fundamental) types. */
12031 static dw_die_ref
12032 base_type_die (tree type, bool reverse)
12034 dw_die_ref base_type_result;
12035 enum dwarf_type encoding;
12036 bool fpt_used = false;
12037 struct fixed_point_type_info fpt_info;
12038 tree type_bias = NULL_TREE;
12040 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
12041 return 0;
12043 /* If this is a subtype that should not be emitted as a subrange type,
12044 use the base type. See subrange_type_for_debug_p. */
12045 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12046 type = TREE_TYPE (type);
12048 switch (TREE_CODE (type))
12050 case INTEGER_TYPE:
12051 if ((dwarf_version >= 4 || !dwarf_strict)
12052 && TYPE_NAME (type)
12053 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12054 && DECL_IS_BUILTIN (TYPE_NAME (type))
12055 && DECL_NAME (TYPE_NAME (type)))
12057 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12058 if (strcmp (name, "char16_t") == 0
12059 || strcmp (name, "char32_t") == 0)
12061 encoding = DW_ATE_UTF;
12062 break;
12065 if ((dwarf_version >= 3 || !dwarf_strict)
12066 && lang_hooks.types.get_fixed_point_type_info)
12068 memset (&fpt_info, 0, sizeof (fpt_info));
12069 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
12071 fpt_used = true;
12072 encoding = ((TYPE_UNSIGNED (type))
12073 ? DW_ATE_unsigned_fixed
12074 : DW_ATE_signed_fixed);
12075 break;
12078 if (TYPE_STRING_FLAG (type))
12080 if (TYPE_UNSIGNED (type))
12081 encoding = DW_ATE_unsigned_char;
12082 else
12083 encoding = DW_ATE_signed_char;
12085 else if (TYPE_UNSIGNED (type))
12086 encoding = DW_ATE_unsigned;
12087 else
12088 encoding = DW_ATE_signed;
12090 if (!dwarf_strict
12091 && lang_hooks.types.get_type_bias)
12092 type_bias = lang_hooks.types.get_type_bias (type);
12093 break;
12095 case REAL_TYPE:
12096 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12098 if (dwarf_version >= 3 || !dwarf_strict)
12099 encoding = DW_ATE_decimal_float;
12100 else
12101 encoding = DW_ATE_lo_user;
12103 else
12104 encoding = DW_ATE_float;
12105 break;
12107 case FIXED_POINT_TYPE:
12108 if (!(dwarf_version >= 3 || !dwarf_strict))
12109 encoding = DW_ATE_lo_user;
12110 else if (TYPE_UNSIGNED (type))
12111 encoding = DW_ATE_unsigned_fixed;
12112 else
12113 encoding = DW_ATE_signed_fixed;
12114 break;
12116 /* Dwarf2 doesn't know anything about complex ints, so use
12117 a user defined type for it. */
12118 case COMPLEX_TYPE:
12119 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12120 encoding = DW_ATE_complex_float;
12121 else
12122 encoding = DW_ATE_lo_user;
12123 break;
12125 case BOOLEAN_TYPE:
12126 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
12127 encoding = DW_ATE_boolean;
12128 break;
12130 default:
12131 /* No other TREE_CODEs are Dwarf fundamental types. */
12132 gcc_unreachable ();
12135 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
12137 add_AT_unsigned (base_type_result, DW_AT_byte_size,
12138 int_size_in_bytes (type));
12139 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12141 if (need_endianity_attribute_p (reverse))
12142 add_AT_unsigned (base_type_result, DW_AT_endianity,
12143 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
12145 add_alignment_attribute (base_type_result, type);
12147 if (fpt_used)
12149 switch (fpt_info.scale_factor_kind)
12151 case fixed_point_scale_factor_binary:
12152 add_AT_int (base_type_result, DW_AT_binary_scale,
12153 fpt_info.scale_factor.binary);
12154 break;
12156 case fixed_point_scale_factor_decimal:
12157 add_AT_int (base_type_result, DW_AT_decimal_scale,
12158 fpt_info.scale_factor.decimal);
12159 break;
12161 case fixed_point_scale_factor_arbitrary:
12162 /* Arbitrary scale factors cannot be described in standard DWARF,
12163 yet. */
12164 if (!dwarf_strict)
12166 /* Describe the scale factor as a rational constant. */
12167 const dw_die_ref scale_factor
12168 = new_die (DW_TAG_constant, comp_unit_die (), type);
12170 add_AT_unsigned (scale_factor, DW_AT_GNU_numerator,
12171 fpt_info.scale_factor.arbitrary.numerator);
12172 add_AT_int (scale_factor, DW_AT_GNU_denominator,
12173 fpt_info.scale_factor.arbitrary.denominator);
12175 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
12177 break;
12179 default:
12180 gcc_unreachable ();
12184 if (type_bias)
12185 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
12186 dw_scalar_form_constant
12187 | dw_scalar_form_exprloc
12188 | dw_scalar_form_reference,
12189 NULL);
12191 add_pubtype (type, base_type_result);
12193 return base_type_result;
12196 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
12197 named 'auto' in its type: return true for it, false otherwise. */
12199 static inline bool
12200 is_cxx_auto (tree type)
12202 if (is_cxx ())
12204 tree name = TYPE_IDENTIFIER (type);
12205 if (name == get_identifier ("auto")
12206 || name == get_identifier ("decltype(auto)"))
12207 return true;
12209 return false;
12212 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12213 given input type is a Dwarf "fundamental" type. Otherwise return null. */
12215 static inline int
12216 is_base_type (tree type)
12218 switch (TREE_CODE (type))
12220 case ERROR_MARK:
12221 case VOID_TYPE:
12222 case INTEGER_TYPE:
12223 case REAL_TYPE:
12224 case FIXED_POINT_TYPE:
12225 case COMPLEX_TYPE:
12226 case BOOLEAN_TYPE:
12227 case POINTER_BOUNDS_TYPE:
12228 return 1;
12230 case ARRAY_TYPE:
12231 case RECORD_TYPE:
12232 case UNION_TYPE:
12233 case QUAL_UNION_TYPE:
12234 case ENUMERAL_TYPE:
12235 case FUNCTION_TYPE:
12236 case METHOD_TYPE:
12237 case POINTER_TYPE:
12238 case REFERENCE_TYPE:
12239 case NULLPTR_TYPE:
12240 case OFFSET_TYPE:
12241 case LANG_TYPE:
12242 case VECTOR_TYPE:
12243 return 0;
12245 default:
12246 if (is_cxx_auto (type))
12247 return 0;
12248 gcc_unreachable ();
12251 return 0;
12254 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12255 node, return the size in bits for the type if it is a constant, or else
12256 return the alignment for the type if the type's size is not constant, or
12257 else return BITS_PER_WORD if the type actually turns out to be an
12258 ERROR_MARK node. */
12260 static inline unsigned HOST_WIDE_INT
12261 simple_type_size_in_bits (const_tree type)
12263 if (TREE_CODE (type) == ERROR_MARK)
12264 return BITS_PER_WORD;
12265 else if (TYPE_SIZE (type) == NULL_TREE)
12266 return 0;
12267 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
12268 return tree_to_uhwi (TYPE_SIZE (type));
12269 else
12270 return TYPE_ALIGN (type);
12273 /* Similarly, but return an offset_int instead of UHWI. */
12275 static inline offset_int
12276 offset_int_type_size_in_bits (const_tree type)
12278 if (TREE_CODE (type) == ERROR_MARK)
12279 return BITS_PER_WORD;
12280 else if (TYPE_SIZE (type) == NULL_TREE)
12281 return 0;
12282 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12283 return wi::to_offset (TYPE_SIZE (type));
12284 else
12285 return TYPE_ALIGN (type);
12288 /* Given a pointer to a tree node for a subrange type, return a pointer
12289 to a DIE that describes the given type. */
12291 static dw_die_ref
12292 subrange_type_die (tree type, tree low, tree high, tree bias,
12293 dw_die_ref context_die)
12295 dw_die_ref subrange_die;
12296 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12298 if (context_die == NULL)
12299 context_die = comp_unit_die ();
12301 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12303 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12305 /* The size of the subrange type and its base type do not match,
12306 so we need to generate a size attribute for the subrange type. */
12307 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12310 add_alignment_attribute (subrange_die, type);
12312 if (low)
12313 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
12314 if (high)
12315 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
12316 if (bias && !dwarf_strict)
12317 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
12318 dw_scalar_form_constant
12319 | dw_scalar_form_exprloc
12320 | dw_scalar_form_reference,
12321 NULL);
12323 return subrange_die;
12326 /* Returns the (const and/or volatile) cv_qualifiers associated with
12327 the decl node. This will normally be augmented with the
12328 cv_qualifiers of the underlying type in add_type_attribute. */
12330 static int
12331 decl_quals (const_tree decl)
12333 return ((TREE_READONLY (decl)
12334 /* The C++ front-end correctly marks reference-typed
12335 variables as readonly, but from a language (and debug
12336 info) standpoint they are not const-qualified. */
12337 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
12338 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
12339 | (TREE_THIS_VOLATILE (decl)
12340 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
12343 /* Determine the TYPE whose qualifiers match the largest strict subset
12344 of the given TYPE_QUALS, and return its qualifiers. Ignore all
12345 qualifiers outside QUAL_MASK. */
12347 static int
12348 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
12350 tree t;
12351 int best_rank = 0, best_qual = 0, max_rank;
12353 type_quals &= qual_mask;
12354 max_rank = popcount_hwi (type_quals) - 1;
12356 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
12357 t = TYPE_NEXT_VARIANT (t))
12359 int q = TYPE_QUALS (t) & qual_mask;
12361 if ((q & type_quals) == q && q != type_quals
12362 && check_base_type (t, type))
12364 int rank = popcount_hwi (q);
12366 if (rank > best_rank)
12368 best_rank = rank;
12369 best_qual = q;
12374 return best_qual;
12377 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
12378 static const dwarf_qual_info_t dwarf_qual_info[] =
12380 { TYPE_QUAL_CONST, DW_TAG_const_type },
12381 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
12382 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
12383 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
12385 static const unsigned int dwarf_qual_info_size
12386 = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
12388 /* If DIE is a qualified DIE of some base DIE with the same parent,
12389 return the base DIE, otherwise return NULL. Set MASK to the
12390 qualifiers added compared to the returned DIE. */
12392 static dw_die_ref
12393 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
12395 unsigned int i;
12396 for (i = 0; i < dwarf_qual_info_size; i++)
12397 if (die->die_tag == dwarf_qual_info[i].t)
12398 break;
12399 if (i == dwarf_qual_info_size)
12400 return NULL;
12401 if (vec_safe_length (die->die_attr) != 1)
12402 return NULL;
12403 dw_die_ref type = get_AT_ref (die, DW_AT_type);
12404 if (type == NULL || type->die_parent != die->die_parent)
12405 return NULL;
12406 *mask |= dwarf_qual_info[i].q;
12407 if (depth)
12409 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
12410 if (ret)
12411 return ret;
12413 return type;
12416 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12417 entry that chains the modifiers specified by CV_QUALS in front of the
12418 given type. REVERSE is true if the type is to be interpreted in the
12419 reverse storage order wrt the target order. */
12421 static dw_die_ref
12422 modified_type_die (tree type, int cv_quals, bool reverse,
12423 dw_die_ref context_die)
12425 enum tree_code code = TREE_CODE (type);
12426 dw_die_ref mod_type_die;
12427 dw_die_ref sub_die = NULL;
12428 tree item_type = NULL;
12429 tree qualified_type;
12430 tree name, low, high;
12431 dw_die_ref mod_scope;
12432 /* Only these cv-qualifiers are currently handled. */
12433 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
12434 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
12436 if (code == ERROR_MARK)
12437 return NULL;
12439 if (lang_hooks.types.get_debug_type)
12441 tree debug_type = lang_hooks.types.get_debug_type (type);
12443 if (debug_type != NULL_TREE && debug_type != type)
12444 return modified_type_die (debug_type, cv_quals, reverse, context_die);
12447 cv_quals &= cv_qual_mask;
12449 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
12450 tag modifier (and not an attribute) old consumers won't be able
12451 to handle it. */
12452 if (dwarf_version < 3)
12453 cv_quals &= ~TYPE_QUAL_RESTRICT;
12455 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
12456 if (dwarf_version < 5)
12457 cv_quals &= ~TYPE_QUAL_ATOMIC;
12459 /* See if we already have the appropriately qualified variant of
12460 this type. */
12461 qualified_type = get_qualified_type (type, cv_quals);
12463 if (qualified_type == sizetype)
12465 /* Try not to expose the internal sizetype type's name. */
12466 if (TYPE_NAME (qualified_type)
12467 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12469 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
12471 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
12472 && (TYPE_PRECISION (t)
12473 == TYPE_PRECISION (qualified_type))
12474 && (TYPE_UNSIGNED (t)
12475 == TYPE_UNSIGNED (qualified_type)));
12476 qualified_type = t;
12478 else if (qualified_type == sizetype
12479 && TREE_CODE (sizetype) == TREE_CODE (size_type_node)
12480 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (size_type_node)
12481 && TYPE_UNSIGNED (sizetype) == TYPE_UNSIGNED (size_type_node))
12482 qualified_type = size_type_node;
12486 /* If we do, then we can just use its DIE, if it exists. */
12487 if (qualified_type)
12489 mod_type_die = lookup_type_die (qualified_type);
12491 /* DW_AT_endianity doesn't come from a qualifier on the type. */
12492 if (mod_type_die
12493 && (!need_endianity_attribute_p (reverse)
12494 || !is_base_type (type)
12495 || get_AT_unsigned (mod_type_die, DW_AT_endianity)))
12496 return mod_type_die;
12499 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12501 /* Handle C typedef types. */
12502 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
12503 && !DECL_ARTIFICIAL (name))
12505 tree dtype = TREE_TYPE (name);
12507 if (qualified_type == dtype)
12509 tree origin = decl_ultimate_origin (name);
12511 /* Typedef variants that have an abstract origin don't get their own
12512 type DIE (see gen_typedef_die), so fall back on the ultimate
12513 abstract origin instead. */
12514 if (origin != NULL)
12515 return modified_type_die (TREE_TYPE (origin), cv_quals, reverse,
12516 context_die);
12518 /* For a named type, use the typedef. */
12519 gen_type_die (qualified_type, context_die);
12520 return lookup_type_die (qualified_type);
12522 else
12524 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
12525 dquals &= cv_qual_mask;
12526 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
12527 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
12528 /* cv-unqualified version of named type. Just use
12529 the unnamed type to which it refers. */
12530 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
12531 reverse, context_die);
12532 /* Else cv-qualified version of named type; fall through. */
12536 mod_scope = scope_die_for (type, context_die);
12538 if (cv_quals)
12540 int sub_quals = 0, first_quals = 0;
12541 unsigned i;
12542 dw_die_ref first = NULL, last = NULL;
12544 /* Determine a lesser qualified type that most closely matches
12545 this one. Then generate DW_TAG_* entries for the remaining
12546 qualifiers. */
12547 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
12548 cv_qual_mask);
12549 if (sub_quals && use_debug_types)
12551 bool needed = false;
12552 /* If emitting type units, make sure the order of qualifiers
12553 is canonical. Thus, start from unqualified type if
12554 an earlier qualifier is missing in sub_quals, but some later
12555 one is present there. */
12556 for (i = 0; i < dwarf_qual_info_size; i++)
12557 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12558 needed = true;
12559 else if (needed && (dwarf_qual_info[i].q & cv_quals))
12561 sub_quals = 0;
12562 break;
12565 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
12566 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
12568 /* As not all intermediate qualified DIEs have corresponding
12569 tree types, ensure that qualified DIEs in the same scope
12570 as their DW_AT_type are emitted after their DW_AT_type,
12571 only with other qualified DIEs for the same type possibly
12572 in between them. Determine the range of such qualified
12573 DIEs now (first being the base type, last being corresponding
12574 last qualified DIE for it). */
12575 unsigned int count = 0;
12576 first = qualified_die_p (mod_type_die, &first_quals,
12577 dwarf_qual_info_size);
12578 if (first == NULL)
12579 first = mod_type_die;
12580 gcc_assert ((first_quals & ~sub_quals) == 0);
12581 for (count = 0, last = first;
12582 count < (1U << dwarf_qual_info_size);
12583 count++, last = last->die_sib)
12585 int quals = 0;
12586 if (last == mod_scope->die_child)
12587 break;
12588 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
12589 != first)
12590 break;
12594 for (i = 0; i < dwarf_qual_info_size; i++)
12595 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12597 dw_die_ref d;
12598 if (first && first != last)
12600 for (d = first->die_sib; ; d = d->die_sib)
12602 int quals = 0;
12603 qualified_die_p (d, &quals, dwarf_qual_info_size);
12604 if (quals == (first_quals | dwarf_qual_info[i].q))
12605 break;
12606 if (d == last)
12608 d = NULL;
12609 break;
12612 if (d)
12614 mod_type_die = d;
12615 continue;
12618 if (first)
12620 d = ggc_cleared_alloc<die_node> ();
12621 d->die_tag = dwarf_qual_info[i].t;
12622 add_child_die_after (mod_scope, d, last);
12623 last = d;
12625 else
12626 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
12627 if (mod_type_die)
12628 add_AT_die_ref (d, DW_AT_type, mod_type_die);
12629 mod_type_die = d;
12630 first_quals |= dwarf_qual_info[i].q;
12633 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
12635 dwarf_tag tag = DW_TAG_pointer_type;
12636 if (code == REFERENCE_TYPE)
12638 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
12639 tag = DW_TAG_rvalue_reference_type;
12640 else
12641 tag = DW_TAG_reference_type;
12643 mod_type_die = new_die (tag, mod_scope, type);
12645 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12646 simple_type_size_in_bits (type) / BITS_PER_UNIT);
12647 add_alignment_attribute (mod_type_die, type);
12648 item_type = TREE_TYPE (type);
12650 addr_space_t as = TYPE_ADDR_SPACE (item_type);
12651 if (!ADDR_SPACE_GENERIC_P (as))
12653 int action = targetm.addr_space.debug (as);
12654 if (action >= 0)
12656 /* Positive values indicate an address_class. */
12657 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
12659 else
12661 /* Negative values indicate an (inverted) segment base reg. */
12662 dw_loc_descr_ref d
12663 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
12664 add_AT_loc (mod_type_die, DW_AT_segment, d);
12668 else if (code == INTEGER_TYPE
12669 && TREE_TYPE (type) != NULL_TREE
12670 && subrange_type_for_debug_p (type, &low, &high))
12672 tree bias = NULL_TREE;
12673 if (lang_hooks.types.get_type_bias)
12674 bias = lang_hooks.types.get_type_bias (type);
12675 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
12676 item_type = TREE_TYPE (type);
12678 else if (is_base_type (type))
12679 mod_type_die = base_type_die (type, reverse);
12680 else
12682 gen_type_die (type, context_die);
12684 /* We have to get the type_main_variant here (and pass that to the
12685 `lookup_type_die' routine) because the ..._TYPE node we have
12686 might simply be a *copy* of some original type node (where the
12687 copy was created to help us keep track of typedef names) and
12688 that copy might have a different TYPE_UID from the original
12689 ..._TYPE node. */
12690 if (TREE_CODE (type) == FUNCTION_TYPE
12691 || TREE_CODE (type) == METHOD_TYPE)
12693 /* For function/method types, can't just use type_main_variant here,
12694 because that can have different ref-qualifiers for C++,
12695 but try to canonicalize. */
12696 tree main = TYPE_MAIN_VARIANT (type);
12697 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
12698 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
12699 && check_base_type (t, main)
12700 && check_lang_type (t, type))
12701 return lookup_type_die (t);
12702 return lookup_type_die (type);
12704 else if (TREE_CODE (type) != VECTOR_TYPE
12705 && TREE_CODE (type) != ARRAY_TYPE)
12706 return lookup_type_die (type_main_variant (type));
12707 else
12708 /* Vectors have the debugging information in the type,
12709 not the main variant. */
12710 return lookup_type_die (type);
12713 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
12714 don't output a DW_TAG_typedef, since there isn't one in the
12715 user's program; just attach a DW_AT_name to the type.
12716 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12717 if the base type already has the same name. */
12718 if (name
12719 && ((TREE_CODE (name) != TYPE_DECL
12720 && (qualified_type == TYPE_MAIN_VARIANT (type)
12721 || (cv_quals == TYPE_UNQUALIFIED)))
12722 || (TREE_CODE (name) == TYPE_DECL
12723 && TREE_TYPE (name) == qualified_type
12724 && DECL_NAME (name))))
12726 if (TREE_CODE (name) == TYPE_DECL)
12727 /* Could just call add_name_and_src_coords_attributes here,
12728 but since this is a builtin type it doesn't have any
12729 useful source coordinates anyway. */
12730 name = DECL_NAME (name);
12731 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12733 /* This probably indicates a bug. */
12734 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
12736 name = TYPE_IDENTIFIER (type);
12737 add_name_attribute (mod_type_die,
12738 name ? IDENTIFIER_POINTER (name) : "__unknown__");
12741 if (qualified_type)
12742 equate_type_number_to_die (qualified_type, mod_type_die);
12744 if (item_type)
12745 /* We must do this after the equate_type_number_to_die call, in case
12746 this is a recursive type. This ensures that the modified_type_die
12747 recursion will terminate even if the type is recursive. Recursive
12748 types are possible in Ada. */
12749 sub_die = modified_type_die (item_type,
12750 TYPE_QUALS_NO_ADDR_SPACE (item_type),
12751 reverse,
12752 context_die);
12754 if (sub_die != NULL)
12755 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
12757 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
12758 if (TYPE_ARTIFICIAL (type))
12759 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
12761 return mod_type_die;
12764 /* Generate DIEs for the generic parameters of T.
12765 T must be either a generic type or a generic function.
12766 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
12768 static void
12769 gen_generic_params_dies (tree t)
12771 tree parms, args;
12772 int parms_num, i;
12773 dw_die_ref die = NULL;
12774 int non_default;
12776 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
12777 return;
12779 if (TYPE_P (t))
12780 die = lookup_type_die (t);
12781 else if (DECL_P (t))
12782 die = lookup_decl_die (t);
12784 gcc_assert (die);
12786 parms = lang_hooks.get_innermost_generic_parms (t);
12787 if (!parms)
12788 /* T has no generic parameter. It means T is neither a generic type
12789 or function. End of story. */
12790 return;
12792 parms_num = TREE_VEC_LENGTH (parms);
12793 args = lang_hooks.get_innermost_generic_args (t);
12794 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
12795 non_default = int_cst_value (TREE_CHAIN (args));
12796 else
12797 non_default = TREE_VEC_LENGTH (args);
12798 for (i = 0; i < parms_num; i++)
12800 tree parm, arg, arg_pack_elems;
12801 dw_die_ref parm_die;
12803 parm = TREE_VEC_ELT (parms, i);
12804 arg = TREE_VEC_ELT (args, i);
12805 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
12806 gcc_assert (parm && TREE_VALUE (parm) && arg);
12808 if (parm && TREE_VALUE (parm) && arg)
12810 /* If PARM represents a template parameter pack,
12811 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
12812 by DW_TAG_template_*_parameter DIEs for the argument
12813 pack elements of ARG. Note that ARG would then be
12814 an argument pack. */
12815 if (arg_pack_elems)
12816 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
12817 arg_pack_elems,
12818 die);
12819 else
12820 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
12821 true /* emit name */, die);
12822 if (i >= non_default)
12823 add_AT_flag (parm_die, DW_AT_default_value, 1);
12828 /* Create and return a DIE for PARM which should be
12829 the representation of a generic type parameter.
12830 For instance, in the C++ front end, PARM would be a template parameter.
12831 ARG is the argument to PARM.
12832 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
12833 name of the PARM.
12834 PARENT_DIE is the parent DIE which the new created DIE should be added to,
12835 as a child node. */
12837 static dw_die_ref
12838 generic_parameter_die (tree parm, tree arg,
12839 bool emit_name_p,
12840 dw_die_ref parent_die)
12842 dw_die_ref tmpl_die = NULL;
12843 const char *name = NULL;
12845 if (!parm || !DECL_NAME (parm) || !arg)
12846 return NULL;
12848 /* We support non-type generic parameters and arguments,
12849 type generic parameters and arguments, as well as
12850 generic generic parameters (a.k.a. template template parameters in C++)
12851 and arguments. */
12852 if (TREE_CODE (parm) == PARM_DECL)
12853 /* PARM is a nontype generic parameter */
12854 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
12855 else if (TREE_CODE (parm) == TYPE_DECL)
12856 /* PARM is a type generic parameter. */
12857 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
12858 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12859 /* PARM is a generic generic parameter.
12860 Its DIE is a GNU extension. It shall have a
12861 DW_AT_name attribute to represent the name of the template template
12862 parameter, and a DW_AT_GNU_template_name attribute to represent the
12863 name of the template template argument. */
12864 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
12865 parent_die, parm);
12866 else
12867 gcc_unreachable ();
12869 if (tmpl_die)
12871 tree tmpl_type;
12873 /* If PARM is a generic parameter pack, it means we are
12874 emitting debug info for a template argument pack element.
12875 In other terms, ARG is a template argument pack element.
12876 In that case, we don't emit any DW_AT_name attribute for
12877 the die. */
12878 if (emit_name_p)
12880 name = IDENTIFIER_POINTER (DECL_NAME (parm));
12881 gcc_assert (name);
12882 add_AT_string (tmpl_die, DW_AT_name, name);
12885 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12887 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
12888 TMPL_DIE should have a child DW_AT_type attribute that is set
12889 to the type of the argument to PARM, which is ARG.
12890 If PARM is a type generic parameter, TMPL_DIE should have a
12891 child DW_AT_type that is set to ARG. */
12892 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
12893 add_type_attribute (tmpl_die, tmpl_type,
12894 (TREE_THIS_VOLATILE (tmpl_type)
12895 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
12896 false, parent_die);
12898 else
12900 /* So TMPL_DIE is a DIE representing a
12901 a generic generic template parameter, a.k.a template template
12902 parameter in C++ and arg is a template. */
12904 /* The DW_AT_GNU_template_name attribute of the DIE must be set
12905 to the name of the argument. */
12906 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
12907 if (name)
12908 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
12911 if (TREE_CODE (parm) == PARM_DECL)
12912 /* So PARM is a non-type generic parameter.
12913 DWARF3 5.6.8 says we must set a DW_AT_const_value child
12914 attribute of TMPL_DIE which value represents the value
12915 of ARG.
12916 We must be careful here:
12917 The value of ARG might reference some function decls.
12918 We might currently be emitting debug info for a generic
12919 type and types are emitted before function decls, we don't
12920 know if the function decls referenced by ARG will actually be
12921 emitted after cgraph computations.
12922 So must defer the generation of the DW_AT_const_value to
12923 after cgraph is ready. */
12924 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
12927 return tmpl_die;
12930 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
12931 PARM_PACK must be a template parameter pack. The returned DIE
12932 will be child DIE of PARENT_DIE. */
12934 static dw_die_ref
12935 template_parameter_pack_die (tree parm_pack,
12936 tree parm_pack_args,
12937 dw_die_ref parent_die)
12939 dw_die_ref die;
12940 int j;
12942 gcc_assert (parent_die && parm_pack);
12944 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
12945 add_name_and_src_coords_attributes (die, parm_pack);
12946 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
12947 generic_parameter_die (parm_pack,
12948 TREE_VEC_ELT (parm_pack_args, j),
12949 false /* Don't emit DW_AT_name */,
12950 die);
12951 return die;
12954 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
12955 an enumerated type. */
12957 static inline int
12958 type_is_enum (const_tree type)
12960 return TREE_CODE (type) == ENUMERAL_TYPE;
12963 /* Return the DBX register number described by a given RTL node. */
12965 static unsigned int
12966 dbx_reg_number (const_rtx rtl)
12968 unsigned regno = REGNO (rtl);
12970 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
12972 #ifdef LEAF_REG_REMAP
12973 if (crtl->uses_only_leaf_regs)
12975 int leaf_reg = LEAF_REG_REMAP (regno);
12976 if (leaf_reg != -1)
12977 regno = (unsigned) leaf_reg;
12979 #endif
12981 regno = DBX_REGISTER_NUMBER (regno);
12982 gcc_assert (regno != INVALID_REGNUM);
12983 return regno;
12986 /* Optionally add a DW_OP_piece term to a location description expression.
12987 DW_OP_piece is only added if the location description expression already
12988 doesn't end with DW_OP_piece. */
12990 static void
12991 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
12993 dw_loc_descr_ref loc;
12995 if (*list_head != NULL)
12997 /* Find the end of the chain. */
12998 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13001 if (loc->dw_loc_opc != DW_OP_piece)
13002 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13006 /* Return a location descriptor that designates a machine register or
13007 zero if there is none. */
13009 static dw_loc_descr_ref
13010 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13012 rtx regs;
13014 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13015 return 0;
13017 /* We only use "frame base" when we're sure we're talking about the
13018 post-prologue local stack frame. We do this by *not* running
13019 register elimination until this point, and recognizing the special
13020 argument pointer and soft frame pointer rtx's.
13021 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
13022 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13023 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13025 dw_loc_descr_ref result = NULL;
13027 if (dwarf_version >= 4 || !dwarf_strict)
13029 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
13030 initialized);
13031 if (result)
13032 add_loc_descr (&result,
13033 new_loc_descr (DW_OP_stack_value, 0, 0));
13035 return result;
13038 regs = targetm.dwarf_register_span (rtl);
13040 if (REG_NREGS (rtl) > 1 || regs)
13041 return multiple_reg_loc_descriptor (rtl, regs, initialized);
13042 else
13044 unsigned int dbx_regnum = dbx_reg_number (rtl);
13045 if (dbx_regnum == IGNORED_DWARF_REGNUM)
13046 return 0;
13047 return one_reg_loc_descriptor (dbx_regnum, initialized);
13051 /* Return a location descriptor that designates a machine register for
13052 a given hard register number. */
13054 static dw_loc_descr_ref
13055 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13057 dw_loc_descr_ref reg_loc_descr;
13059 if (regno <= 31)
13060 reg_loc_descr
13061 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13062 else
13063 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13065 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13066 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13068 return reg_loc_descr;
13071 /* Given an RTL of a register, return a location descriptor that
13072 designates a value that spans more than one register. */
13074 static dw_loc_descr_ref
13075 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13076 enum var_init_status initialized)
13078 int size, i;
13079 dw_loc_descr_ref loc_result = NULL;
13081 /* Simple, contiguous registers. */
13082 if (regs == NULL_RTX)
13084 unsigned reg = REGNO (rtl);
13085 int nregs;
13087 #ifdef LEAF_REG_REMAP
13088 if (crtl->uses_only_leaf_regs)
13090 int leaf_reg = LEAF_REG_REMAP (reg);
13091 if (leaf_reg != -1)
13092 reg = (unsigned) leaf_reg;
13094 #endif
13096 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13097 nregs = REG_NREGS (rtl);
13099 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13101 loc_result = NULL;
13102 while (nregs--)
13104 dw_loc_descr_ref t;
13106 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13107 VAR_INIT_STATUS_INITIALIZED);
13108 add_loc_descr (&loc_result, t);
13109 add_loc_descr_op_piece (&loc_result, size);
13110 ++reg;
13112 return loc_result;
13115 /* Now onto stupid register sets in non contiguous locations. */
13117 gcc_assert (GET_CODE (regs) == PARALLEL);
13119 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13120 loc_result = NULL;
13122 for (i = 0; i < XVECLEN (regs, 0); ++i)
13124 dw_loc_descr_ref t;
13126 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
13127 VAR_INIT_STATUS_INITIALIZED);
13128 add_loc_descr (&loc_result, t);
13129 add_loc_descr_op_piece (&loc_result, size);
13132 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13133 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13134 return loc_result;
13137 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
13139 /* Return a location descriptor that designates a constant i,
13140 as a compound operation from constant (i >> shift), constant shift
13141 and DW_OP_shl. */
13143 static dw_loc_descr_ref
13144 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13146 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
13147 add_loc_descr (&ret, int_loc_descriptor (shift));
13148 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13149 return ret;
13152 /* Return a location descriptor that designates a constant. */
13154 static dw_loc_descr_ref
13155 int_loc_descriptor (HOST_WIDE_INT i)
13157 enum dwarf_location_atom op;
13159 /* Pick the smallest representation of a constant, rather than just
13160 defaulting to the LEB encoding. */
13161 if (i >= 0)
13163 int clz = clz_hwi (i);
13164 int ctz = ctz_hwi (i);
13165 if (i <= 31)
13166 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13167 else if (i <= 0xff)
13168 op = DW_OP_const1u;
13169 else if (i <= 0xffff)
13170 op = DW_OP_const2u;
13171 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13172 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13173 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
13174 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
13175 while DW_OP_const4u is 5 bytes. */
13176 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
13177 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13178 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13179 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
13180 while DW_OP_const4u is 5 bytes. */
13181 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13183 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13184 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13185 <= 4)
13187 /* As i >= 2**31, the double cast above will yield a negative number.
13188 Since wrapping is defined in DWARF expressions we can output big
13189 positive integers as small negative ones, regardless of the size
13190 of host wide ints.
13192 Here, since the evaluator will handle 32-bit values and since i >=
13193 2**31, we know it's going to be interpreted as a negative literal:
13194 store it this way if we can do better than 5 bytes this way. */
13195 return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13197 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13198 op = DW_OP_const4u;
13200 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
13201 least 6 bytes: see if we can do better before falling back to it. */
13202 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13203 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13204 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
13205 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13206 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13207 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
13208 >= HOST_BITS_PER_WIDE_INT)
13209 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
13210 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
13211 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
13212 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13213 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13214 && size_of_uleb128 (i) > 6)
13215 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
13216 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
13217 else
13218 op = DW_OP_constu;
13220 else
13222 if (i >= -0x80)
13223 op = DW_OP_const1s;
13224 else if (i >= -0x8000)
13225 op = DW_OP_const2s;
13226 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13228 if (size_of_int_loc_descriptor (i) < 5)
13230 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13231 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13232 return ret;
13234 op = DW_OP_const4s;
13236 else
13238 if (size_of_int_loc_descriptor (i)
13239 < (unsigned long) 1 + size_of_sleb128 (i))
13241 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13242 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13243 return ret;
13245 op = DW_OP_consts;
13249 return new_loc_descr (op, i, 0);
13252 /* Likewise, for unsigned constants. */
13254 static dw_loc_descr_ref
13255 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
13257 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
13258 const unsigned HOST_WIDE_INT max_uint
13259 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
13261 /* If possible, use the clever signed constants handling. */
13262 if (i <= max_int)
13263 return int_loc_descriptor ((HOST_WIDE_INT) i);
13265 /* Here, we are left with positive numbers that cannot be represented as
13266 HOST_WIDE_INT, i.e.:
13267 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
13269 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
13270 whereas may be better to output a negative integer: thanks to integer
13271 wrapping, we know that:
13272 x = x - 2 ** DWARF2_ADDR_SIZE
13273 = x - 2 * (max (HOST_WIDE_INT) + 1)
13274 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
13275 small negative integers. Let's try that in cases it will clearly improve
13276 the encoding: there is no gain turning DW_OP_const4u into
13277 DW_OP_const4s. */
13278 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
13279 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
13280 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
13282 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
13284 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
13285 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
13286 const HOST_WIDE_INT second_shift
13287 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
13289 /* So we finally have:
13290 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
13291 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
13292 return int_loc_descriptor (second_shift);
13295 /* Last chance: fallback to a simple constant operation. */
13296 return new_loc_descr
13297 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13298 ? DW_OP_const4u
13299 : DW_OP_const8u,
13300 i, 0);
13303 /* Generate and return a location description that computes the unsigned
13304 comparison of the two stack top entries (a OP b where b is the top-most
13305 entry and a is the second one). The KIND of comparison can be LT_EXPR,
13306 LE_EXPR, GT_EXPR or GE_EXPR. */
13308 static dw_loc_descr_ref
13309 uint_comparison_loc_list (enum tree_code kind)
13311 enum dwarf_location_atom op, flip_op;
13312 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
13314 switch (kind)
13316 case LT_EXPR:
13317 op = DW_OP_lt;
13318 break;
13319 case LE_EXPR:
13320 op = DW_OP_le;
13321 break;
13322 case GT_EXPR:
13323 op = DW_OP_gt;
13324 break;
13325 case GE_EXPR:
13326 op = DW_OP_ge;
13327 break;
13328 default:
13329 gcc_unreachable ();
13332 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13333 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
13335 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
13336 possible to perform unsigned comparisons: we just have to distinguish
13337 three cases:
13339 1. when a and b have the same sign (as signed integers); then we should
13340 return: a OP(signed) b;
13342 2. when a is a negative signed integer while b is a positive one, then a
13343 is a greater unsigned integer than b; likewise when a and b's roles
13344 are flipped.
13346 So first, compare the sign of the two operands. */
13347 ret = new_loc_descr (DW_OP_over, 0, 0);
13348 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13349 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
13350 /* If they have different signs (i.e. they have different sign bits), then
13351 the stack top value has now the sign bit set and thus it's smaller than
13352 zero. */
13353 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
13354 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
13355 add_loc_descr (&ret, bra_node);
13357 /* We are in case 1. At this point, we know both operands have the same
13358 sign, to it's safe to use the built-in signed comparison. */
13359 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13360 add_loc_descr (&ret, jmp_node);
13362 /* We are in case 2. Here, we know both operands do not have the same sign,
13363 so we have to flip the signed comparison. */
13364 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
13365 tmp = new_loc_descr (flip_op, 0, 0);
13366 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13367 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
13368 add_loc_descr (&ret, tmp);
13370 /* This dummy operation is necessary to make the two branches join. */
13371 tmp = new_loc_descr (DW_OP_nop, 0, 0);
13372 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13373 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
13374 add_loc_descr (&ret, tmp);
13376 return ret;
13379 /* Likewise, but takes the location description lists (might be destructive on
13380 them). Return NULL if either is NULL or if concatenation fails. */
13382 static dw_loc_list_ref
13383 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
13384 enum tree_code kind)
13386 if (left == NULL || right == NULL)
13387 return NULL;
13389 add_loc_list (&left, right);
13390 if (left == NULL)
13391 return NULL;
13393 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
13394 return left;
13397 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
13398 without actually allocating it. */
13400 static unsigned long
13401 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13403 return size_of_int_loc_descriptor (i >> shift)
13404 + size_of_int_loc_descriptor (shift)
13405 + 1;
13408 /* Return size_of_locs (int_loc_descriptor (i)) without
13409 actually allocating it. */
13411 static unsigned long
13412 size_of_int_loc_descriptor (HOST_WIDE_INT i)
13414 unsigned long s;
13416 if (i >= 0)
13418 int clz, ctz;
13419 if (i <= 31)
13420 return 1;
13421 else if (i <= 0xff)
13422 return 2;
13423 else if (i <= 0xffff)
13424 return 3;
13425 clz = clz_hwi (i);
13426 ctz = ctz_hwi (i);
13427 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13428 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13429 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13430 - clz - 5);
13431 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13432 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13433 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13434 - clz - 8);
13435 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13436 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13437 <= 4)
13438 return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13439 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13440 return 5;
13441 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13442 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13443 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13444 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13445 - clz - 8);
13446 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13447 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
13448 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13449 - clz - 16);
13450 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13451 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13452 && s > 6)
13453 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13454 - clz - 32);
13455 else
13456 return 1 + s;
13458 else
13460 if (i >= -0x80)
13461 return 2;
13462 else if (i >= -0x8000)
13463 return 3;
13464 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13466 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13468 s = size_of_int_loc_descriptor (-i) + 1;
13469 if (s < 5)
13470 return s;
13472 return 5;
13474 else
13476 unsigned long r = 1 + size_of_sleb128 (i);
13477 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13479 s = size_of_int_loc_descriptor (-i) + 1;
13480 if (s < r)
13481 return s;
13483 return r;
13488 /* Return loc description representing "address" of integer value.
13489 This can appear only as toplevel expression. */
13491 static dw_loc_descr_ref
13492 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13494 int litsize;
13495 dw_loc_descr_ref loc_result = NULL;
13497 if (!(dwarf_version >= 4 || !dwarf_strict))
13498 return NULL;
13500 litsize = size_of_int_loc_descriptor (i);
13501 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13502 is more compact. For DW_OP_stack_value we need:
13503 litsize + 1 (DW_OP_stack_value)
13504 and for DW_OP_implicit_value:
13505 1 (DW_OP_implicit_value) + 1 (length) + size. */
13506 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13508 loc_result = int_loc_descriptor (i);
13509 add_loc_descr (&loc_result,
13510 new_loc_descr (DW_OP_stack_value, 0, 0));
13511 return loc_result;
13514 loc_result = new_loc_descr (DW_OP_implicit_value,
13515 size, 0);
13516 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13517 loc_result->dw_loc_oprnd2.v.val_int = i;
13518 return loc_result;
13521 /* Return a location descriptor that designates a base+offset location. */
13523 static dw_loc_descr_ref
13524 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13525 enum var_init_status initialized)
13527 unsigned int regno;
13528 dw_loc_descr_ref result;
13529 dw_fde_ref fde = cfun->fde;
13531 /* We only use "frame base" when we're sure we're talking about the
13532 post-prologue local stack frame. We do this by *not* running
13533 register elimination until this point, and recognizing the special
13534 argument pointer and soft frame pointer rtx's. */
13535 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13537 rtx elim = (ira_use_lra_p
13538 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
13539 : eliminate_regs (reg, VOIDmode, NULL_RTX));
13541 if (elim != reg)
13543 if (GET_CODE (elim) == PLUS)
13545 offset += INTVAL (XEXP (elim, 1));
13546 elim = XEXP (elim, 0);
13548 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13549 && (elim == hard_frame_pointer_rtx
13550 || elim == stack_pointer_rtx))
13551 || elim == (frame_pointer_needed
13552 ? hard_frame_pointer_rtx
13553 : stack_pointer_rtx));
13555 /* If drap register is used to align stack, use frame
13556 pointer + offset to access stack variables. If stack
13557 is aligned without drap, use stack pointer + offset to
13558 access stack variables. */
13559 if (crtl->stack_realign_tried
13560 && reg == frame_pointer_rtx)
13562 int base_reg
13563 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13564 ? HARD_FRAME_POINTER_REGNUM
13565 : REGNO (elim));
13566 return new_reg_loc_descr (base_reg, offset);
13569 gcc_assert (frame_pointer_fb_offset_valid);
13570 offset += frame_pointer_fb_offset;
13571 return new_loc_descr (DW_OP_fbreg, offset, 0);
13575 regno = REGNO (reg);
13576 #ifdef LEAF_REG_REMAP
13577 if (crtl->uses_only_leaf_regs)
13579 int leaf_reg = LEAF_REG_REMAP (regno);
13580 if (leaf_reg != -1)
13581 regno = (unsigned) leaf_reg;
13583 #endif
13584 regno = DWARF_FRAME_REGNUM (regno);
13586 if (!optimize && fde
13587 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
13589 /* Use cfa+offset to represent the location of arguments passed
13590 on the stack when drap is used to align stack.
13591 Only do this when not optimizing, for optimized code var-tracking
13592 is supposed to track where the arguments live and the register
13593 used as vdrap or drap in some spot might be used for something
13594 else in other part of the routine. */
13595 return new_loc_descr (DW_OP_fbreg, offset, 0);
13598 if (regno <= 31)
13599 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13600 offset, 0);
13601 else
13602 result = new_loc_descr (DW_OP_bregx, regno, offset);
13604 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13605 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13607 return result;
13610 /* Return true if this RTL expression describes a base+offset calculation. */
13612 static inline int
13613 is_based_loc (const_rtx rtl)
13615 return (GET_CODE (rtl) == PLUS
13616 && ((REG_P (XEXP (rtl, 0))
13617 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13618 && CONST_INT_P (XEXP (rtl, 1)))));
13621 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13622 failed. */
13624 static dw_loc_descr_ref
13625 tls_mem_loc_descriptor (rtx mem)
13627 tree base;
13628 dw_loc_descr_ref loc_result;
13630 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
13631 return NULL;
13633 base = get_base_address (MEM_EXPR (mem));
13634 if (base == NULL
13635 || !VAR_P (base)
13636 || !DECL_THREAD_LOCAL_P (base))
13637 return NULL;
13639 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
13640 if (loc_result == NULL)
13641 return NULL;
13643 if (MEM_OFFSET (mem))
13644 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
13646 return loc_result;
13649 /* Output debug info about reason why we failed to expand expression as dwarf
13650 expression. */
13652 static void
13653 expansion_failed (tree expr, rtx rtl, char const *reason)
13655 if (dump_file && (dump_flags & TDF_DETAILS))
13657 fprintf (dump_file, "Failed to expand as dwarf: ");
13658 if (expr)
13659 print_generic_expr (dump_file, expr, dump_flags);
13660 if (rtl)
13662 fprintf (dump_file, "\n");
13663 print_rtl (dump_file, rtl);
13665 fprintf (dump_file, "\nReason: %s\n", reason);
13669 /* Helper function for const_ok_for_output. */
13671 static bool
13672 const_ok_for_output_1 (rtx rtl)
13674 if (GET_CODE (rtl) == UNSPEC)
13676 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13677 we can't express it in the debug info. */
13678 /* Don't complain about TLS UNSPECs, those are just too hard to
13679 delegitimize. Note this could be a non-decl SYMBOL_REF such as
13680 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
13681 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
13682 if (flag_checking
13683 && (XVECLEN (rtl, 0) == 0
13684 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
13685 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
13686 inform (current_function_decl
13687 ? DECL_SOURCE_LOCATION (current_function_decl)
13688 : UNKNOWN_LOCATION,
13689 #if NUM_UNSPEC_VALUES > 0
13690 "non-delegitimized UNSPEC %s (%d) found in variable location",
13691 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
13692 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
13693 XINT (rtl, 1));
13694 #else
13695 "non-delegitimized UNSPEC %d found in variable location",
13696 XINT (rtl, 1));
13697 #endif
13698 expansion_failed (NULL_TREE, rtl,
13699 "UNSPEC hasn't been delegitimized.\n");
13700 return false;
13703 if (targetm.const_not_ok_for_debug_p (rtl))
13705 expansion_failed (NULL_TREE, rtl,
13706 "Expression rejected for debug by the backend.\n");
13707 return false;
13710 /* FIXME: Refer to PR60655. It is possible for simplification
13711 of rtl expressions in var tracking to produce such expressions.
13712 We should really identify / validate expressions
13713 enclosed in CONST that can be handled by assemblers on various
13714 targets and only handle legitimate cases here. */
13715 if (GET_CODE (rtl) != SYMBOL_REF)
13717 if (GET_CODE (rtl) == NOT)
13718 return false;
13719 return true;
13722 if (CONSTANT_POOL_ADDRESS_P (rtl))
13724 bool marked;
13725 get_pool_constant_mark (rtl, &marked);
13726 /* If all references to this pool constant were optimized away,
13727 it was not output and thus we can't represent it. */
13728 if (!marked)
13730 expansion_failed (NULL_TREE, rtl,
13731 "Constant was removed from constant pool.\n");
13732 return false;
13736 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13737 return false;
13739 /* Avoid references to external symbols in debug info, on several targets
13740 the linker might even refuse to link when linking a shared library,
13741 and in many other cases the relocations for .debug_info/.debug_loc are
13742 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
13743 to be defined within the same shared library or executable are fine. */
13744 if (SYMBOL_REF_EXTERNAL_P (rtl))
13746 tree decl = SYMBOL_REF_DECL (rtl);
13748 if (decl == NULL || !targetm.binds_local_p (decl))
13750 expansion_failed (NULL_TREE, rtl,
13751 "Symbol not defined in current TU.\n");
13752 return false;
13756 return true;
13759 /* Return true if constant RTL can be emitted in DW_OP_addr or
13760 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
13761 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
13763 static bool
13764 const_ok_for_output (rtx rtl)
13766 if (GET_CODE (rtl) == SYMBOL_REF)
13767 return const_ok_for_output_1 (rtl);
13769 if (GET_CODE (rtl) == CONST)
13771 subrtx_var_iterator::array_type array;
13772 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
13773 if (!const_ok_for_output_1 (*iter))
13774 return false;
13775 return true;
13778 return true;
13781 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
13782 if possible, NULL otherwise. */
13784 static dw_die_ref
13785 base_type_for_mode (machine_mode mode, bool unsignedp)
13787 dw_die_ref type_die;
13788 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
13790 if (type == NULL)
13791 return NULL;
13792 switch (TREE_CODE (type))
13794 case INTEGER_TYPE:
13795 case REAL_TYPE:
13796 break;
13797 default:
13798 return NULL;
13800 type_die = lookup_type_die (type);
13801 if (!type_die)
13802 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
13803 comp_unit_die ());
13804 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
13805 return NULL;
13806 return type_die;
13809 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
13810 type matching MODE, or, if MODE is narrower than or as wide as
13811 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
13812 possible. */
13814 static dw_loc_descr_ref
13815 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
13817 machine_mode outer_mode = mode;
13818 dw_die_ref type_die;
13819 dw_loc_descr_ref cvt;
13821 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13823 add_loc_descr (&op, new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
13824 return op;
13826 type_die = base_type_for_mode (outer_mode, 1);
13827 if (type_die == NULL)
13828 return NULL;
13829 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13830 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13831 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13832 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13833 add_loc_descr (&op, cvt);
13834 return op;
13837 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
13839 static dw_loc_descr_ref
13840 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
13841 dw_loc_descr_ref op1)
13843 dw_loc_descr_ref ret = op0;
13844 add_loc_descr (&ret, op1);
13845 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13846 if (STORE_FLAG_VALUE != 1)
13848 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
13849 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
13851 return ret;
13854 /* Return location descriptor for signed comparison OP RTL. */
13856 static dw_loc_descr_ref
13857 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
13858 machine_mode mem_mode)
13860 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13861 dw_loc_descr_ref op0, op1;
13862 int shift;
13864 if (op_mode == VOIDmode)
13865 op_mode = GET_MODE (XEXP (rtl, 1));
13866 if (op_mode == VOIDmode)
13867 return NULL;
13869 if (dwarf_strict
13870 && dwarf_version < 5
13871 && (!SCALAR_INT_MODE_P (op_mode)
13872 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
13873 return NULL;
13875 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
13876 VAR_INIT_STATUS_INITIALIZED);
13877 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
13878 VAR_INIT_STATUS_INITIALIZED);
13880 if (op0 == NULL || op1 == NULL)
13881 return NULL;
13883 if (!SCALAR_INT_MODE_P (op_mode)
13884 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
13885 return compare_loc_descriptor (op, op0, op1);
13887 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
13889 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
13890 dw_loc_descr_ref cvt;
13892 if (type_die == NULL)
13893 return NULL;
13894 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13895 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13896 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13897 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13898 add_loc_descr (&op0, cvt);
13899 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13900 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13901 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13902 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13903 add_loc_descr (&op1, cvt);
13904 return compare_loc_descriptor (op, op0, op1);
13907 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
13908 /* For eq/ne, if the operands are known to be zero-extended,
13909 there is no need to do the fancy shifting up. */
13910 if (op == DW_OP_eq || op == DW_OP_ne)
13912 dw_loc_descr_ref last0, last1;
13913 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
13915 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
13917 /* deref_size zero extends, and for constants we can check
13918 whether they are zero extended or not. */
13919 if (((last0->dw_loc_opc == DW_OP_deref_size
13920 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13921 || (CONST_INT_P (XEXP (rtl, 0))
13922 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
13923 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
13924 && ((last1->dw_loc_opc == DW_OP_deref_size
13925 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13926 || (CONST_INT_P (XEXP (rtl, 1))
13927 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
13928 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
13929 return compare_loc_descriptor (op, op0, op1);
13931 /* EQ/NE comparison against constant in narrower type than
13932 DWARF2_ADDR_SIZE can be performed either as
13933 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
13934 DW_OP_{eq,ne}
13936 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
13937 DW_OP_{eq,ne}. Pick whatever is shorter. */
13938 if (CONST_INT_P (XEXP (rtl, 1))
13939 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
13940 && (size_of_int_loc_descriptor (shift) + 1
13941 + size_of_int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift)
13942 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
13943 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
13944 & GET_MODE_MASK (op_mode))))
13946 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
13947 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
13948 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
13949 & GET_MODE_MASK (op_mode));
13950 return compare_loc_descriptor (op, op0, op1);
13953 add_loc_descr (&op0, int_loc_descriptor (shift));
13954 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
13955 if (CONST_INT_P (XEXP (rtl, 1)))
13956 op1 = int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift);
13957 else
13959 add_loc_descr (&op1, int_loc_descriptor (shift));
13960 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
13962 return compare_loc_descriptor (op, op0, op1);
13965 /* Return location descriptor for unsigned comparison OP RTL. */
13967 static dw_loc_descr_ref
13968 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
13969 machine_mode mem_mode)
13971 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13972 dw_loc_descr_ref op0, op1;
13974 if (op_mode == VOIDmode)
13975 op_mode = GET_MODE (XEXP (rtl, 1));
13976 if (op_mode == VOIDmode)
13977 return NULL;
13978 if (!SCALAR_INT_MODE_P (op_mode))
13979 return NULL;
13981 if (dwarf_strict
13982 && dwarf_version < 5
13983 && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
13984 return NULL;
13986 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
13987 VAR_INIT_STATUS_INITIALIZED);
13988 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
13989 VAR_INIT_STATUS_INITIALIZED);
13991 if (op0 == NULL || op1 == NULL)
13992 return NULL;
13994 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
13996 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
13997 dw_loc_descr_ref last0, last1;
13998 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
14000 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
14002 if (CONST_INT_P (XEXP (rtl, 0)))
14003 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
14004 /* deref_size zero extends, so no need to mask it again. */
14005 else if (last0->dw_loc_opc != DW_OP_deref_size
14006 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14008 add_loc_descr (&op0, int_loc_descriptor (mask));
14009 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14011 if (CONST_INT_P (XEXP (rtl, 1)))
14012 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14013 /* deref_size zero extends, so no need to mask it again. */
14014 else if (last1->dw_loc_opc != DW_OP_deref_size
14015 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14017 add_loc_descr (&op1, int_loc_descriptor (mask));
14018 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14021 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
14023 HOST_WIDE_INT bias = 1;
14024 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14025 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14026 if (CONST_INT_P (XEXP (rtl, 1)))
14027 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14028 + INTVAL (XEXP (rtl, 1)));
14029 else
14030 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14031 bias, 0));
14033 return compare_loc_descriptor (op, op0, op1);
14036 /* Return location descriptor for {U,S}{MIN,MAX}. */
14038 static dw_loc_descr_ref
14039 minmax_loc_descriptor (rtx rtl, machine_mode mode,
14040 machine_mode mem_mode)
14042 enum dwarf_location_atom op;
14043 dw_loc_descr_ref op0, op1, ret;
14044 dw_loc_descr_ref bra_node, drop_node;
14046 if (dwarf_strict
14047 && dwarf_version < 5
14048 && (!SCALAR_INT_MODE_P (mode)
14049 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
14050 return NULL;
14052 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14053 VAR_INIT_STATUS_INITIALIZED);
14054 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14055 VAR_INIT_STATUS_INITIALIZED);
14057 if (op0 == NULL || op1 == NULL)
14058 return NULL;
14060 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14061 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14062 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14063 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14065 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14067 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
14068 add_loc_descr (&op0, int_loc_descriptor (mask));
14069 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14070 add_loc_descr (&op1, int_loc_descriptor (mask));
14071 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14073 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
14075 HOST_WIDE_INT bias = 1;
14076 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14077 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14078 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14081 else if (!SCALAR_INT_MODE_P (mode)
14082 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14084 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
14085 add_loc_descr (&op0, int_loc_descriptor (shift));
14086 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14087 add_loc_descr (&op1, int_loc_descriptor (shift));
14088 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14090 else if (SCALAR_INT_MODE_P (mode)
14091 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14093 dw_die_ref type_die = base_type_for_mode (mode, 0);
14094 dw_loc_descr_ref cvt;
14095 if (type_die == NULL)
14096 return NULL;
14097 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14098 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14099 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14100 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14101 add_loc_descr (&op0, cvt);
14102 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14103 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14104 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14105 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14106 add_loc_descr (&op1, cvt);
14109 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14110 op = DW_OP_lt;
14111 else
14112 op = DW_OP_gt;
14113 ret = op0;
14114 add_loc_descr (&ret, op1);
14115 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14116 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14117 add_loc_descr (&ret, bra_node);
14118 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14119 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14120 add_loc_descr (&ret, drop_node);
14121 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14122 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14123 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
14124 && SCALAR_INT_MODE_P (mode)
14125 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14126 ret = convert_descriptor_to_mode (mode, ret);
14127 return ret;
14130 /* Helper function for mem_loc_descriptor. Perform OP binary op,
14131 but after converting arguments to type_die, afterwards
14132 convert back to unsigned. */
14134 static dw_loc_descr_ref
14135 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
14136 machine_mode mode, machine_mode mem_mode)
14138 dw_loc_descr_ref cvt, op0, op1;
14140 if (type_die == NULL)
14141 return NULL;
14142 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14143 VAR_INIT_STATUS_INITIALIZED);
14144 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14145 VAR_INIT_STATUS_INITIALIZED);
14146 if (op0 == NULL || op1 == NULL)
14147 return NULL;
14148 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14149 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14150 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14151 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14152 add_loc_descr (&op0, cvt);
14153 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14154 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14155 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14156 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14157 add_loc_descr (&op1, cvt);
14158 add_loc_descr (&op0, op1);
14159 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
14160 return convert_descriptor_to_mode (mode, op0);
14163 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
14164 const0 is DW_OP_lit0 or corresponding typed constant,
14165 const1 is DW_OP_lit1 or corresponding typed constant
14166 and constMSB is constant with just the MSB bit set
14167 for the mode):
14168 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14169 L1: const0 DW_OP_swap
14170 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
14171 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14172 L3: DW_OP_drop
14173 L4: DW_OP_nop
14175 CTZ is similar:
14176 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14177 L1: const0 DW_OP_swap
14178 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14179 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14180 L3: DW_OP_drop
14181 L4: DW_OP_nop
14183 FFS is similar:
14184 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
14185 L1: const1 DW_OP_swap
14186 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14187 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14188 L3: DW_OP_drop
14189 L4: DW_OP_nop */
14191 static dw_loc_descr_ref
14192 clz_loc_descriptor (rtx rtl, machine_mode mode,
14193 machine_mode mem_mode)
14195 dw_loc_descr_ref op0, ret, tmp;
14196 HOST_WIDE_INT valv;
14197 dw_loc_descr_ref l1jump, l1label;
14198 dw_loc_descr_ref l2jump, l2label;
14199 dw_loc_descr_ref l3jump, l3label;
14200 dw_loc_descr_ref l4jump, l4label;
14201 rtx msb;
14203 if (!SCALAR_INT_MODE_P (mode)
14204 || GET_MODE (XEXP (rtl, 0)) != mode)
14205 return NULL;
14207 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14208 VAR_INIT_STATUS_INITIALIZED);
14209 if (op0 == NULL)
14210 return NULL;
14211 ret = op0;
14212 if (GET_CODE (rtl) == CLZ)
14214 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14215 valv = GET_MODE_BITSIZE (mode);
14217 else if (GET_CODE (rtl) == FFS)
14218 valv = 0;
14219 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14220 valv = GET_MODE_BITSIZE (mode);
14221 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14222 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
14223 add_loc_descr (&ret, l1jump);
14224 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14225 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
14226 VAR_INIT_STATUS_INITIALIZED);
14227 if (tmp == NULL)
14228 return NULL;
14229 add_loc_descr (&ret, tmp);
14230 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
14231 add_loc_descr (&ret, l4jump);
14232 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
14233 ? const1_rtx : const0_rtx,
14234 mode, mem_mode,
14235 VAR_INIT_STATUS_INITIALIZED);
14236 if (l1label == NULL)
14237 return NULL;
14238 add_loc_descr (&ret, l1label);
14239 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14240 l2label = new_loc_descr (DW_OP_dup, 0, 0);
14241 add_loc_descr (&ret, l2label);
14242 if (GET_CODE (rtl) != CLZ)
14243 msb = const1_rtx;
14244 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
14245 msb = GEN_INT (HOST_WIDE_INT_1U
14246 << (GET_MODE_BITSIZE (mode) - 1));
14247 else
14248 msb = immed_wide_int_const
14249 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
14250 GET_MODE_PRECISION (mode)), mode);
14251 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
14252 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14253 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
14254 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
14255 else
14256 tmp = mem_loc_descriptor (msb, mode, mem_mode,
14257 VAR_INIT_STATUS_INITIALIZED);
14258 if (tmp == NULL)
14259 return NULL;
14260 add_loc_descr (&ret, tmp);
14261 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14262 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
14263 add_loc_descr (&ret, l3jump);
14264 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14265 VAR_INIT_STATUS_INITIALIZED);
14266 if (tmp == NULL)
14267 return NULL;
14268 add_loc_descr (&ret, tmp);
14269 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
14270 ? DW_OP_shl : DW_OP_shr, 0, 0));
14271 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14272 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
14273 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14274 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
14275 add_loc_descr (&ret, l2jump);
14276 l3label = new_loc_descr (DW_OP_drop, 0, 0);
14277 add_loc_descr (&ret, l3label);
14278 l4label = new_loc_descr (DW_OP_nop, 0, 0);
14279 add_loc_descr (&ret, l4label);
14280 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14281 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14282 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14283 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14284 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14285 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
14286 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14287 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
14288 return ret;
14291 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
14292 const1 is DW_OP_lit1 or corresponding typed constant):
14293 const0 DW_OP_swap
14294 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14295 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14296 L2: DW_OP_drop
14298 PARITY is similar:
14299 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14300 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14301 L2: DW_OP_drop */
14303 static dw_loc_descr_ref
14304 popcount_loc_descriptor (rtx rtl, machine_mode mode,
14305 machine_mode mem_mode)
14307 dw_loc_descr_ref op0, ret, tmp;
14308 dw_loc_descr_ref l1jump, l1label;
14309 dw_loc_descr_ref l2jump, l2label;
14311 if (!SCALAR_INT_MODE_P (mode)
14312 || GET_MODE (XEXP (rtl, 0)) != mode)
14313 return NULL;
14315 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14316 VAR_INIT_STATUS_INITIALIZED);
14317 if (op0 == NULL)
14318 return NULL;
14319 ret = op0;
14320 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14321 VAR_INIT_STATUS_INITIALIZED);
14322 if (tmp == NULL)
14323 return NULL;
14324 add_loc_descr (&ret, tmp);
14325 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14326 l1label = new_loc_descr (DW_OP_dup, 0, 0);
14327 add_loc_descr (&ret, l1label);
14328 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14329 add_loc_descr (&ret, l2jump);
14330 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14331 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14332 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14333 VAR_INIT_STATUS_INITIALIZED);
14334 if (tmp == NULL)
14335 return NULL;
14336 add_loc_descr (&ret, tmp);
14337 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14338 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
14339 ? DW_OP_plus : DW_OP_xor, 0, 0));
14340 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14341 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14342 VAR_INIT_STATUS_INITIALIZED);
14343 add_loc_descr (&ret, tmp);
14344 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14345 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14346 add_loc_descr (&ret, l1jump);
14347 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14348 add_loc_descr (&ret, l2label);
14349 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14350 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14351 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14352 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14353 return ret;
14356 /* BSWAP (constS is initial shift count, either 56 or 24):
14357 constS const0
14358 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
14359 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
14360 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
14361 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
14362 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
14364 static dw_loc_descr_ref
14365 bswap_loc_descriptor (rtx rtl, machine_mode mode,
14366 machine_mode mem_mode)
14368 dw_loc_descr_ref op0, ret, tmp;
14369 dw_loc_descr_ref l1jump, l1label;
14370 dw_loc_descr_ref l2jump, l2label;
14372 if (!SCALAR_INT_MODE_P (mode)
14373 || BITS_PER_UNIT != 8
14374 || (GET_MODE_BITSIZE (mode) != 32
14375 && GET_MODE_BITSIZE (mode) != 64))
14376 return NULL;
14378 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14379 VAR_INIT_STATUS_INITIALIZED);
14380 if (op0 == NULL)
14381 return NULL;
14383 ret = op0;
14384 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14385 mode, mem_mode,
14386 VAR_INIT_STATUS_INITIALIZED);
14387 if (tmp == NULL)
14388 return NULL;
14389 add_loc_descr (&ret, tmp);
14390 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14391 VAR_INIT_STATUS_INITIALIZED);
14392 if (tmp == NULL)
14393 return NULL;
14394 add_loc_descr (&ret, tmp);
14395 l1label = new_loc_descr (DW_OP_pick, 2, 0);
14396 add_loc_descr (&ret, l1label);
14397 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14398 mode, mem_mode,
14399 VAR_INIT_STATUS_INITIALIZED);
14400 add_loc_descr (&ret, tmp);
14401 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
14402 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14403 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14404 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
14405 VAR_INIT_STATUS_INITIALIZED);
14406 if (tmp == NULL)
14407 return NULL;
14408 add_loc_descr (&ret, tmp);
14409 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14410 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
14411 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14412 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14413 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14414 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14415 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14416 VAR_INIT_STATUS_INITIALIZED);
14417 add_loc_descr (&ret, tmp);
14418 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
14419 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14420 add_loc_descr (&ret, l2jump);
14421 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
14422 VAR_INIT_STATUS_INITIALIZED);
14423 add_loc_descr (&ret, tmp);
14424 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14425 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14426 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14427 add_loc_descr (&ret, l1jump);
14428 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14429 add_loc_descr (&ret, l2label);
14430 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14431 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14432 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14433 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14434 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14435 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14436 return ret;
14439 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
14440 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14441 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
14442 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
14444 ROTATERT is similar:
14445 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
14446 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14447 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
14449 static dw_loc_descr_ref
14450 rotate_loc_descriptor (rtx rtl, machine_mode mode,
14451 machine_mode mem_mode)
14453 rtx rtlop1 = XEXP (rtl, 1);
14454 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
14455 int i;
14457 if (!SCALAR_INT_MODE_P (mode))
14458 return NULL;
14460 if (GET_MODE (rtlop1) != VOIDmode
14461 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
14462 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
14463 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14464 VAR_INIT_STATUS_INITIALIZED);
14465 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
14466 VAR_INIT_STATUS_INITIALIZED);
14467 if (op0 == NULL || op1 == NULL)
14468 return NULL;
14469 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14470 for (i = 0; i < 2; i++)
14472 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
14473 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
14474 mode, mem_mode,
14475 VAR_INIT_STATUS_INITIALIZED);
14476 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
14477 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14478 ? DW_OP_const4u
14479 : HOST_BITS_PER_WIDE_INT == 64
14480 ? DW_OP_const8u : DW_OP_constu,
14481 GET_MODE_MASK (mode), 0);
14482 else
14483 mask[i] = NULL;
14484 if (mask[i] == NULL)
14485 return NULL;
14486 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
14488 ret = op0;
14489 add_loc_descr (&ret, op1);
14490 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14491 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14492 if (GET_CODE (rtl) == ROTATERT)
14494 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14495 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14496 GET_MODE_BITSIZE (mode), 0));
14498 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14499 if (mask[0] != NULL)
14500 add_loc_descr (&ret, mask[0]);
14501 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14502 if (mask[1] != NULL)
14504 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14505 add_loc_descr (&ret, mask[1]);
14506 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14508 if (GET_CODE (rtl) == ROTATE)
14510 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14511 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14512 GET_MODE_BITSIZE (mode), 0));
14514 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14515 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14516 return ret;
14519 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
14520 for DEBUG_PARAMETER_REF RTL. */
14522 static dw_loc_descr_ref
14523 parameter_ref_descriptor (rtx rtl)
14525 dw_loc_descr_ref ret;
14526 dw_die_ref ref;
14528 if (dwarf_strict)
14529 return NULL;
14530 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
14531 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
14532 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
14533 if (ref)
14535 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14536 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14537 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14539 else
14541 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14542 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
14544 return ret;
14547 /* The following routine converts the RTL for a variable or parameter
14548 (resident in memory) into an equivalent Dwarf representation of a
14549 mechanism for getting the address of that same variable onto the top of a
14550 hypothetical "address evaluation" stack.
14552 When creating memory location descriptors, we are effectively transforming
14553 the RTL for a memory-resident object into its Dwarf postfix expression
14554 equivalent. This routine recursively descends an RTL tree, turning
14555 it into Dwarf postfix code as it goes.
14557 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
14559 MEM_MODE is the mode of the memory reference, needed to handle some
14560 autoincrement addressing modes.
14562 Return 0 if we can't represent the location. */
14564 dw_loc_descr_ref
14565 mem_loc_descriptor (rtx rtl, machine_mode mode,
14566 machine_mode mem_mode,
14567 enum var_init_status initialized)
14569 dw_loc_descr_ref mem_loc_result = NULL;
14570 enum dwarf_location_atom op;
14571 dw_loc_descr_ref op0, op1;
14572 rtx inner = NULL_RTX;
14574 if (mode == VOIDmode)
14575 mode = GET_MODE (rtl);
14577 /* Note that for a dynamically sized array, the location we will generate a
14578 description of here will be the lowest numbered location which is
14579 actually within the array. That's *not* necessarily the same as the
14580 zeroth element of the array. */
14582 rtl = targetm.delegitimize_address (rtl);
14584 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
14585 return NULL;
14587 switch (GET_CODE (rtl))
14589 case POST_INC:
14590 case POST_DEC:
14591 case POST_MODIFY:
14592 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
14594 case SUBREG:
14595 /* The case of a subreg may arise when we have a local (register)
14596 variable or a formal (register) parameter which doesn't quite fill
14597 up an entire register. For now, just assume that it is
14598 legitimate to make the Dwarf info refer to the whole register which
14599 contains the given subreg. */
14600 if (!subreg_lowpart_p (rtl))
14601 break;
14602 inner = SUBREG_REG (rtl);
14603 /* FALLTHRU */
14604 case TRUNCATE:
14605 if (inner == NULL_RTX)
14606 inner = XEXP (rtl, 0);
14607 if (SCALAR_INT_MODE_P (mode)
14608 && SCALAR_INT_MODE_P (GET_MODE (inner))
14609 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14610 #ifdef POINTERS_EXTEND_UNSIGNED
14611 || (mode == Pmode && mem_mode != VOIDmode)
14612 #endif
14614 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
14616 mem_loc_result = mem_loc_descriptor (inner,
14617 GET_MODE (inner),
14618 mem_mode, initialized);
14619 break;
14621 if (dwarf_strict && dwarf_version < 5)
14622 break;
14623 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
14624 break;
14625 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
14626 && (!SCALAR_INT_MODE_P (mode)
14627 || !SCALAR_INT_MODE_P (GET_MODE (inner))))
14628 break;
14629 else
14631 dw_die_ref type_die;
14632 dw_loc_descr_ref cvt;
14634 mem_loc_result = mem_loc_descriptor (inner,
14635 GET_MODE (inner),
14636 mem_mode, initialized);
14637 if (mem_loc_result == NULL)
14638 break;
14639 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14640 if (type_die == NULL)
14642 mem_loc_result = NULL;
14643 break;
14645 if (GET_MODE_SIZE (mode)
14646 != GET_MODE_SIZE (GET_MODE (inner)))
14647 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14648 else
14649 cvt = new_loc_descr (dwarf_OP (DW_OP_reinterpret), 0, 0);
14650 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14651 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14652 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14653 add_loc_descr (&mem_loc_result, cvt);
14654 if (SCALAR_INT_MODE_P (mode)
14655 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14657 /* Convert it to untyped afterwards. */
14658 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14659 add_loc_descr (&mem_loc_result, cvt);
14662 break;
14664 case REG:
14665 if (! SCALAR_INT_MODE_P (mode)
14666 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14667 && rtl != arg_pointer_rtx
14668 && rtl != frame_pointer_rtx
14669 #ifdef POINTERS_EXTEND_UNSIGNED
14670 && (mode != Pmode || mem_mode == VOIDmode)
14671 #endif
14674 dw_die_ref type_die;
14675 unsigned int dbx_regnum;
14677 if (dwarf_strict && dwarf_version < 5)
14678 break;
14679 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
14680 break;
14681 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14682 if (type_die == NULL)
14683 break;
14685 dbx_regnum = dbx_reg_number (rtl);
14686 if (dbx_regnum == IGNORED_DWARF_REGNUM)
14687 break;
14688 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_regval_type),
14689 dbx_regnum, 0);
14690 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14691 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14692 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
14693 break;
14695 /* Whenever a register number forms a part of the description of the
14696 method for calculating the (dynamic) address of a memory resident
14697 object, DWARF rules require the register number be referred to as
14698 a "base register". This distinction is not based in any way upon
14699 what category of register the hardware believes the given register
14700 belongs to. This is strictly DWARF terminology we're dealing with
14701 here. Note that in cases where the location of a memory-resident
14702 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
14703 OP_CONST (0)) the actual DWARF location descriptor that we generate
14704 may just be OP_BASEREG (basereg). This may look deceptively like
14705 the object in question was allocated to a register (rather than in
14706 memory) so DWARF consumers need to be aware of the subtle
14707 distinction between OP_REG and OP_BASEREG. */
14708 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
14709 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
14710 else if (stack_realign_drap
14711 && crtl->drap_reg
14712 && crtl->args.internal_arg_pointer == rtl
14713 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
14715 /* If RTL is internal_arg_pointer, which has been optimized
14716 out, use DRAP instead. */
14717 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
14718 VAR_INIT_STATUS_INITIALIZED);
14720 break;
14722 case SIGN_EXTEND:
14723 case ZERO_EXTEND:
14724 if (!SCALAR_INT_MODE_P (mode))
14725 break;
14726 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
14727 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14728 if (op0 == 0)
14729 break;
14730 else if (GET_CODE (rtl) == ZERO_EXTEND
14731 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14732 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14733 < HOST_BITS_PER_WIDE_INT
14734 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
14735 to expand zero extend as two shifts instead of
14736 masking. */
14737 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
14739 machine_mode imode = GET_MODE (XEXP (rtl, 0));
14740 mem_loc_result = op0;
14741 add_loc_descr (&mem_loc_result,
14742 int_loc_descriptor (GET_MODE_MASK (imode)));
14743 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
14745 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14747 int shift = DWARF2_ADDR_SIZE
14748 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
14749 shift *= BITS_PER_UNIT;
14750 if (GET_CODE (rtl) == SIGN_EXTEND)
14751 op = DW_OP_shra;
14752 else
14753 op = DW_OP_shr;
14754 mem_loc_result = op0;
14755 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14756 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14757 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14758 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14760 else if (!dwarf_strict || dwarf_version >= 5)
14762 dw_die_ref type_die1, type_die2;
14763 dw_loc_descr_ref cvt;
14765 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
14766 GET_CODE (rtl) == ZERO_EXTEND);
14767 if (type_die1 == NULL)
14768 break;
14769 type_die2 = base_type_for_mode (mode, 1);
14770 if (type_die2 == NULL)
14771 break;
14772 mem_loc_result = op0;
14773 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14774 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14775 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
14776 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14777 add_loc_descr (&mem_loc_result, cvt);
14778 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14779 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14780 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
14781 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14782 add_loc_descr (&mem_loc_result, cvt);
14784 break;
14786 case MEM:
14788 rtx new_rtl = avoid_constant_pool_reference (rtl);
14789 if (new_rtl != rtl)
14791 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
14792 initialized);
14793 if (mem_loc_result != NULL)
14794 return mem_loc_result;
14797 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
14798 get_address_mode (rtl), mode,
14799 VAR_INIT_STATUS_INITIALIZED);
14800 if (mem_loc_result == NULL)
14801 mem_loc_result = tls_mem_loc_descriptor (rtl);
14802 if (mem_loc_result != NULL)
14804 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14805 || !SCALAR_INT_MODE_P(mode))
14807 dw_die_ref type_die;
14808 dw_loc_descr_ref deref;
14810 if (dwarf_strict && dwarf_version < 5)
14811 return NULL;
14812 type_die
14813 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14814 if (type_die == NULL)
14815 return NULL;
14816 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type),
14817 GET_MODE_SIZE (mode), 0);
14818 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14819 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14820 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
14821 add_loc_descr (&mem_loc_result, deref);
14823 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
14824 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
14825 else
14826 add_loc_descr (&mem_loc_result,
14827 new_loc_descr (DW_OP_deref_size,
14828 GET_MODE_SIZE (mode), 0));
14830 break;
14832 case LO_SUM:
14833 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
14835 case LABEL_REF:
14836 /* Some ports can transform a symbol ref into a label ref, because
14837 the symbol ref is too far away and has to be dumped into a constant
14838 pool. */
14839 case CONST:
14840 case SYMBOL_REF:
14841 if (!SCALAR_INT_MODE_P (mode)
14842 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14843 #ifdef POINTERS_EXTEND_UNSIGNED
14844 && (mode != Pmode || mem_mode == VOIDmode)
14845 #endif
14847 break;
14848 if (GET_CODE (rtl) == SYMBOL_REF
14849 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
14851 dw_loc_descr_ref temp;
14853 /* If this is not defined, we have no way to emit the data. */
14854 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
14855 break;
14857 temp = new_addr_loc_descr (rtl, dtprel_true);
14859 /* We check for DWARF 5 here because gdb did not implement
14860 DW_OP_form_tls_address until after 7.12. */
14861 mem_loc_result = new_loc_descr ((dwarf_version >= 5
14862 ? DW_OP_form_tls_address
14863 : DW_OP_GNU_push_tls_address),
14864 0, 0);
14865 add_loc_descr (&mem_loc_result, temp);
14867 break;
14870 if (!const_ok_for_output (rtl))
14872 if (GET_CODE (rtl) == CONST)
14873 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14874 initialized);
14875 break;
14878 symref:
14879 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
14880 vec_safe_push (used_rtx_array, rtl);
14881 break;
14883 case CONCAT:
14884 case CONCATN:
14885 case VAR_LOCATION:
14886 case DEBUG_IMPLICIT_PTR:
14887 expansion_failed (NULL_TREE, rtl,
14888 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
14889 return 0;
14891 case ENTRY_VALUE:
14892 if (dwarf_strict && dwarf_version < 5)
14893 return NULL;
14894 if (REG_P (ENTRY_VALUE_EXP (rtl)))
14896 if (!SCALAR_INT_MODE_P (mode)
14897 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14898 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
14899 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14900 else
14902 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
14903 if (dbx_regnum == IGNORED_DWARF_REGNUM)
14904 return NULL;
14905 op0 = one_reg_loc_descriptor (dbx_regnum,
14906 VAR_INIT_STATUS_INITIALIZED);
14909 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
14910 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
14912 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
14913 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14914 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
14915 return NULL;
14917 else
14918 gcc_unreachable ();
14919 if (op0 == NULL)
14920 return NULL;
14921 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_entry_value), 0, 0);
14922 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
14923 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
14924 break;
14926 case DEBUG_PARAMETER_REF:
14927 mem_loc_result = parameter_ref_descriptor (rtl);
14928 break;
14930 case PRE_MODIFY:
14931 /* Extract the PLUS expression nested inside and fall into
14932 PLUS code below. */
14933 rtl = XEXP (rtl, 1);
14934 goto plus;
14936 case PRE_INC:
14937 case PRE_DEC:
14938 /* Turn these into a PLUS expression and fall into the PLUS code
14939 below. */
14940 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
14941 gen_int_mode (GET_CODE (rtl) == PRE_INC
14942 ? GET_MODE_UNIT_SIZE (mem_mode)
14943 : -GET_MODE_UNIT_SIZE (mem_mode),
14944 mode));
14946 /* fall through */
14948 case PLUS:
14949 plus:
14950 if (is_based_loc (rtl)
14951 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14952 || XEXP (rtl, 0) == arg_pointer_rtx
14953 || XEXP (rtl, 0) == frame_pointer_rtx)
14954 && SCALAR_INT_MODE_P (mode))
14955 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
14956 INTVAL (XEXP (rtl, 1)),
14957 VAR_INIT_STATUS_INITIALIZED);
14958 else
14960 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14961 VAR_INIT_STATUS_INITIALIZED);
14962 if (mem_loc_result == 0)
14963 break;
14965 if (CONST_INT_P (XEXP (rtl, 1))
14966 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14967 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
14968 else
14970 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14971 VAR_INIT_STATUS_INITIALIZED);
14972 if (op1 == 0)
14973 return NULL;
14974 add_loc_descr (&mem_loc_result, op1);
14975 add_loc_descr (&mem_loc_result,
14976 new_loc_descr (DW_OP_plus, 0, 0));
14979 break;
14981 /* If a pseudo-reg is optimized away, it is possible for it to
14982 be replaced with a MEM containing a multiply or shift. */
14983 case MINUS:
14984 op = DW_OP_minus;
14985 goto do_binop;
14987 case MULT:
14988 op = DW_OP_mul;
14989 goto do_binop;
14991 case DIV:
14992 if ((!dwarf_strict || dwarf_version >= 5)
14993 && SCALAR_INT_MODE_P (mode)
14994 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14996 mem_loc_result = typed_binop (DW_OP_div, rtl,
14997 base_type_for_mode (mode, 0),
14998 mode, mem_mode);
14999 break;
15001 op = DW_OP_div;
15002 goto do_binop;
15004 case UMOD:
15005 op = DW_OP_mod;
15006 goto do_binop;
15008 case ASHIFT:
15009 op = DW_OP_shl;
15010 goto do_shift;
15012 case ASHIFTRT:
15013 op = DW_OP_shra;
15014 goto do_shift;
15016 case LSHIFTRT:
15017 op = DW_OP_shr;
15018 goto do_shift;
15020 do_shift:
15021 if (!SCALAR_INT_MODE_P (mode))
15022 break;
15023 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15024 VAR_INIT_STATUS_INITIALIZED);
15026 rtx rtlop1 = XEXP (rtl, 1);
15027 if (GET_MODE (rtlop1) != VOIDmode
15028 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
15029 < GET_MODE_BITSIZE (mode))
15030 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
15031 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
15032 VAR_INIT_STATUS_INITIALIZED);
15035 if (op0 == 0 || op1 == 0)
15036 break;
15038 mem_loc_result = op0;
15039 add_loc_descr (&mem_loc_result, op1);
15040 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15041 break;
15043 case AND:
15044 op = DW_OP_and;
15045 goto do_binop;
15047 case IOR:
15048 op = DW_OP_or;
15049 goto do_binop;
15051 case XOR:
15052 op = DW_OP_xor;
15053 goto do_binop;
15055 do_binop:
15056 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15057 VAR_INIT_STATUS_INITIALIZED);
15058 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15059 VAR_INIT_STATUS_INITIALIZED);
15061 if (op0 == 0 || op1 == 0)
15062 break;
15064 mem_loc_result = op0;
15065 add_loc_descr (&mem_loc_result, op1);
15066 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15067 break;
15069 case MOD:
15070 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
15071 && (!dwarf_strict || dwarf_version >= 5))
15073 mem_loc_result = typed_binop (DW_OP_mod, rtl,
15074 base_type_for_mode (mode, 0),
15075 mode, mem_mode);
15076 break;
15079 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15080 VAR_INIT_STATUS_INITIALIZED);
15081 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15082 VAR_INIT_STATUS_INITIALIZED);
15084 if (op0 == 0 || op1 == 0)
15085 break;
15087 mem_loc_result = op0;
15088 add_loc_descr (&mem_loc_result, op1);
15089 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15090 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15091 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
15092 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
15093 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
15094 break;
15096 case UDIV:
15097 if ((!dwarf_strict || dwarf_version >= 5)
15098 && SCALAR_INT_MODE_P (mode))
15100 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
15102 op = DW_OP_div;
15103 goto do_binop;
15105 mem_loc_result = typed_binop (DW_OP_div, rtl,
15106 base_type_for_mode (mode, 1),
15107 mode, mem_mode);
15109 break;
15111 case NOT:
15112 op = DW_OP_not;
15113 goto do_unop;
15115 case ABS:
15116 op = DW_OP_abs;
15117 goto do_unop;
15119 case NEG:
15120 op = DW_OP_neg;
15121 goto do_unop;
15123 do_unop:
15124 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15125 VAR_INIT_STATUS_INITIALIZED);
15127 if (op0 == 0)
15128 break;
15130 mem_loc_result = op0;
15131 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15132 break;
15134 case CONST_INT:
15135 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
15136 #ifdef POINTERS_EXTEND_UNSIGNED
15137 || (mode == Pmode
15138 && mem_mode != VOIDmode
15139 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
15140 #endif
15143 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15144 break;
15146 if ((!dwarf_strict || dwarf_version >= 5)
15147 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
15148 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
15150 dw_die_ref type_die = base_type_for_mode (mode, 1);
15151 machine_mode amode;
15152 if (type_die == NULL)
15153 return NULL;
15154 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
15155 MODE_INT, 0);
15156 if (INTVAL (rtl) >= 0
15157 && amode != BLKmode
15158 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
15159 /* const DW_OP_convert <XXX> vs.
15160 DW_OP_const_type <XXX, 1, const>. */
15161 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
15162 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
15164 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15165 op0 = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15166 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15167 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15168 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
15169 add_loc_descr (&mem_loc_result, op0);
15170 return mem_loc_result;
15172 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0,
15173 INTVAL (rtl));
15174 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15175 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15176 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15177 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
15178 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
15179 else
15181 mem_loc_result->dw_loc_oprnd2.val_class
15182 = dw_val_class_const_double;
15183 mem_loc_result->dw_loc_oprnd2.v.val_double
15184 = double_int::from_shwi (INTVAL (rtl));
15187 break;
15189 case CONST_DOUBLE:
15190 if (!dwarf_strict || dwarf_version >= 5)
15192 dw_die_ref type_die;
15194 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
15195 CONST_DOUBLE rtx could represent either a large integer
15196 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
15197 the value is always a floating point constant.
15199 When it is an integer, a CONST_DOUBLE is used whenever
15200 the constant requires 2 HWIs to be adequately represented.
15201 We output CONST_DOUBLEs as blocks. */
15202 if (mode == VOIDmode
15203 || (GET_MODE (rtl) == VOIDmode
15204 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
15205 break;
15206 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15207 if (type_die == NULL)
15208 return NULL;
15209 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15210 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15211 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15212 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15213 #if TARGET_SUPPORTS_WIDE_INT == 0
15214 if (!SCALAR_FLOAT_MODE_P (mode))
15216 mem_loc_result->dw_loc_oprnd2.val_class
15217 = dw_val_class_const_double;
15218 mem_loc_result->dw_loc_oprnd2.v.val_double
15219 = rtx_to_double_int (rtl);
15221 else
15222 #endif
15224 unsigned int length = GET_MODE_SIZE (mode);
15225 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15227 insert_float (rtl, array);
15228 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15229 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15230 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15231 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15234 break;
15236 case CONST_WIDE_INT:
15237 if (!dwarf_strict || dwarf_version >= 5)
15239 dw_die_ref type_die;
15241 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15242 if (type_die == NULL)
15243 return NULL;
15244 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15245 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15246 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15247 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15248 mem_loc_result->dw_loc_oprnd2.val_class
15249 = dw_val_class_wide_int;
15250 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15251 *mem_loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
15253 break;
15255 case EQ:
15256 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
15257 break;
15259 case GE:
15260 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15261 break;
15263 case GT:
15264 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15265 break;
15267 case LE:
15268 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15269 break;
15271 case LT:
15272 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15273 break;
15275 case NE:
15276 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
15277 break;
15279 case GEU:
15280 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15281 break;
15283 case GTU:
15284 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15285 break;
15287 case LEU:
15288 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15289 break;
15291 case LTU:
15292 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15293 break;
15295 case UMIN:
15296 case UMAX:
15297 if (!SCALAR_INT_MODE_P (mode))
15298 break;
15299 /* FALLTHRU */
15300 case SMIN:
15301 case SMAX:
15302 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
15303 break;
15305 case ZERO_EXTRACT:
15306 case SIGN_EXTRACT:
15307 if (CONST_INT_P (XEXP (rtl, 1))
15308 && CONST_INT_P (XEXP (rtl, 2))
15309 && ((unsigned) INTVAL (XEXP (rtl, 1))
15310 + (unsigned) INTVAL (XEXP (rtl, 2))
15311 <= GET_MODE_BITSIZE (mode))
15312 && SCALAR_INT_MODE_P (mode)
15313 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
15314 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
15316 int shift, size;
15317 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15318 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15319 if (op0 == 0)
15320 break;
15321 if (GET_CODE (rtl) == SIGN_EXTRACT)
15322 op = DW_OP_shra;
15323 else
15324 op = DW_OP_shr;
15325 mem_loc_result = op0;
15326 size = INTVAL (XEXP (rtl, 1));
15327 shift = INTVAL (XEXP (rtl, 2));
15328 if (BITS_BIG_ENDIAN)
15329 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
15330 - shift - size;
15331 if (shift + size != (int) DWARF2_ADDR_SIZE)
15333 add_loc_descr (&mem_loc_result,
15334 int_loc_descriptor (DWARF2_ADDR_SIZE
15335 - shift - size));
15336 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
15338 if (size != (int) DWARF2_ADDR_SIZE)
15340 add_loc_descr (&mem_loc_result,
15341 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
15342 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15345 break;
15347 case IF_THEN_ELSE:
15349 dw_loc_descr_ref op2, bra_node, drop_node;
15350 op0 = mem_loc_descriptor (XEXP (rtl, 0),
15351 GET_MODE (XEXP (rtl, 0)) == VOIDmode
15352 ? word_mode : GET_MODE (XEXP (rtl, 0)),
15353 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15354 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15355 VAR_INIT_STATUS_INITIALIZED);
15356 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
15357 VAR_INIT_STATUS_INITIALIZED);
15358 if (op0 == NULL || op1 == NULL || op2 == NULL)
15359 break;
15361 mem_loc_result = op1;
15362 add_loc_descr (&mem_loc_result, op2);
15363 add_loc_descr (&mem_loc_result, op0);
15364 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15365 add_loc_descr (&mem_loc_result, bra_node);
15366 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
15367 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
15368 add_loc_descr (&mem_loc_result, drop_node);
15369 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15370 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
15372 break;
15374 case FLOAT_EXTEND:
15375 case FLOAT_TRUNCATE:
15376 case FLOAT:
15377 case UNSIGNED_FLOAT:
15378 case FIX:
15379 case UNSIGNED_FIX:
15380 if (!dwarf_strict || dwarf_version >= 5)
15382 dw_die_ref type_die;
15383 dw_loc_descr_ref cvt;
15385 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15386 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15387 if (op0 == NULL)
15388 break;
15389 if (SCALAR_INT_MODE_P (GET_MODE (XEXP (rtl, 0)))
15390 && (GET_CODE (rtl) == FLOAT
15391 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
15392 <= DWARF2_ADDR_SIZE))
15394 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
15395 GET_CODE (rtl) == UNSIGNED_FLOAT);
15396 if (type_die == NULL)
15397 break;
15398 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15399 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15400 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15401 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15402 add_loc_descr (&op0, cvt);
15404 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
15405 if (type_die == NULL)
15406 break;
15407 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15408 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15409 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15410 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15411 add_loc_descr (&op0, cvt);
15412 if (SCALAR_INT_MODE_P (mode)
15413 && (GET_CODE (rtl) == FIX
15414 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
15416 op0 = convert_descriptor_to_mode (mode, op0);
15417 if (op0 == NULL)
15418 break;
15420 mem_loc_result = op0;
15422 break;
15424 case CLZ:
15425 case CTZ:
15426 case FFS:
15427 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
15428 break;
15430 case POPCOUNT:
15431 case PARITY:
15432 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
15433 break;
15435 case BSWAP:
15436 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
15437 break;
15439 case ROTATE:
15440 case ROTATERT:
15441 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
15442 break;
15444 case COMPARE:
15445 /* In theory, we could implement the above. */
15446 /* DWARF cannot represent the unsigned compare operations
15447 natively. */
15448 case SS_MULT:
15449 case US_MULT:
15450 case SS_DIV:
15451 case US_DIV:
15452 case SS_PLUS:
15453 case US_PLUS:
15454 case SS_MINUS:
15455 case US_MINUS:
15456 case SS_NEG:
15457 case US_NEG:
15458 case SS_ABS:
15459 case SS_ASHIFT:
15460 case US_ASHIFT:
15461 case SS_TRUNCATE:
15462 case US_TRUNCATE:
15463 case UNORDERED:
15464 case ORDERED:
15465 case UNEQ:
15466 case UNGE:
15467 case UNGT:
15468 case UNLE:
15469 case UNLT:
15470 case LTGT:
15471 case FRACT_CONVERT:
15472 case UNSIGNED_FRACT_CONVERT:
15473 case SAT_FRACT:
15474 case UNSIGNED_SAT_FRACT:
15475 case SQRT:
15476 case ASM_OPERANDS:
15477 case VEC_MERGE:
15478 case VEC_SELECT:
15479 case VEC_CONCAT:
15480 case VEC_DUPLICATE:
15481 case UNSPEC:
15482 case HIGH:
15483 case FMA:
15484 case STRICT_LOW_PART:
15485 case CONST_VECTOR:
15486 case CONST_FIXED:
15487 case CLRSB:
15488 case CLOBBER:
15489 /* If delegitimize_address couldn't do anything with the UNSPEC, we
15490 can't express it in the debug info. This can happen e.g. with some
15491 TLS UNSPECs. */
15492 break;
15494 case CONST_STRING:
15495 resolve_one_addr (&rtl);
15496 goto symref;
15498 /* RTL sequences inside PARALLEL record a series of DWARF operations for
15499 the expression. An UNSPEC rtx represents a raw DWARF operation,
15500 new_loc_descr is called for it to build the operation directly.
15501 Otherwise mem_loc_descriptor is called recursively. */
15502 case PARALLEL:
15504 int index = 0;
15505 dw_loc_descr_ref exp_result = NULL;
15507 for (; index < XVECLEN (rtl, 0); index++)
15509 rtx elem = XVECEXP (rtl, 0, index);
15510 if (GET_CODE (elem) == UNSPEC)
15512 /* Each DWARF operation UNSPEC contain two operands, if
15513 one operand is not used for the operation, const0_rtx is
15514 passed. */
15515 gcc_assert (XVECLEN (elem, 0) == 2);
15517 HOST_WIDE_INT dw_op = XINT (elem, 1);
15518 HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
15519 HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
15520 exp_result
15521 = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
15522 oprnd2);
15524 else
15525 exp_result
15526 = mem_loc_descriptor (elem, mode, mem_mode,
15527 VAR_INIT_STATUS_INITIALIZED);
15529 if (!mem_loc_result)
15530 mem_loc_result = exp_result;
15531 else
15532 add_loc_descr (&mem_loc_result, exp_result);
15535 break;
15538 default:
15539 if (flag_checking)
15541 print_rtl (stderr, rtl);
15542 gcc_unreachable ();
15544 break;
15547 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15548 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15550 return mem_loc_result;
15553 /* Return a descriptor that describes the concatenation of two locations.
15554 This is typically a complex variable. */
15556 static dw_loc_descr_ref
15557 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
15559 dw_loc_descr_ref cc_loc_result = NULL;
15560 dw_loc_descr_ref x0_ref
15561 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15562 dw_loc_descr_ref x1_ref
15563 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15565 if (x0_ref == 0 || x1_ref == 0)
15566 return 0;
15568 cc_loc_result = x0_ref;
15569 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
15571 add_loc_descr (&cc_loc_result, x1_ref);
15572 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
15574 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
15575 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15577 return cc_loc_result;
15580 /* Return a descriptor that describes the concatenation of N
15581 locations. */
15583 static dw_loc_descr_ref
15584 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
15586 unsigned int i;
15587 dw_loc_descr_ref cc_loc_result = NULL;
15588 unsigned int n = XVECLEN (concatn, 0);
15590 for (i = 0; i < n; ++i)
15592 dw_loc_descr_ref ref;
15593 rtx x = XVECEXP (concatn, 0, i);
15595 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15596 if (ref == NULL)
15597 return NULL;
15599 add_loc_descr (&cc_loc_result, ref);
15600 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
15603 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15604 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15606 return cc_loc_result;
15609 /* Helper function for loc_descriptor. Return DW_OP_implicit_pointer
15610 for DEBUG_IMPLICIT_PTR RTL. */
15612 static dw_loc_descr_ref
15613 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
15615 dw_loc_descr_ref ret;
15616 dw_die_ref ref;
15618 if (dwarf_strict && dwarf_version < 5)
15619 return NULL;
15620 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
15621 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
15622 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
15623 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
15624 ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
15625 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
15626 if (ref)
15628 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15629 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15630 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15632 else
15634 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15635 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
15637 return ret;
15640 /* Output a proper Dwarf location descriptor for a variable or parameter
15641 which is either allocated in a register or in a memory location. For a
15642 register, we just generate an OP_REG and the register number. For a
15643 memory location we provide a Dwarf postfix expression describing how to
15644 generate the (dynamic) address of the object onto the address stack.
15646 MODE is mode of the decl if this loc_descriptor is going to be used in
15647 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
15648 allowed, VOIDmode otherwise.
15650 If we don't know how to describe it, return 0. */
15652 static dw_loc_descr_ref
15653 loc_descriptor (rtx rtl, machine_mode mode,
15654 enum var_init_status initialized)
15656 dw_loc_descr_ref loc_result = NULL;
15658 switch (GET_CODE (rtl))
15660 case SUBREG:
15661 /* The case of a subreg may arise when we have a local (register)
15662 variable or a formal (register) parameter which doesn't quite fill
15663 up an entire register. For now, just assume that it is
15664 legitimate to make the Dwarf info refer to the whole register which
15665 contains the given subreg. */
15666 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
15667 loc_result = loc_descriptor (SUBREG_REG (rtl),
15668 GET_MODE (SUBREG_REG (rtl)), initialized);
15669 else
15670 goto do_default;
15671 break;
15673 case REG:
15674 loc_result = reg_loc_descriptor (rtl, initialized);
15675 break;
15677 case MEM:
15678 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
15679 GET_MODE (rtl), initialized);
15680 if (loc_result == NULL)
15681 loc_result = tls_mem_loc_descriptor (rtl);
15682 if (loc_result == NULL)
15684 rtx new_rtl = avoid_constant_pool_reference (rtl);
15685 if (new_rtl != rtl)
15686 loc_result = loc_descriptor (new_rtl, mode, initialized);
15688 break;
15690 case CONCAT:
15691 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
15692 initialized);
15693 break;
15695 case CONCATN:
15696 loc_result = concatn_loc_descriptor (rtl, initialized);
15697 break;
15699 case VAR_LOCATION:
15700 /* Single part. */
15701 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
15703 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
15704 if (GET_CODE (loc) == EXPR_LIST)
15705 loc = XEXP (loc, 0);
15706 loc_result = loc_descriptor (loc, mode, initialized);
15707 break;
15710 rtl = XEXP (rtl, 1);
15711 /* FALLTHRU */
15713 case PARALLEL:
15715 rtvec par_elems = XVEC (rtl, 0);
15716 int num_elem = GET_NUM_ELEM (par_elems);
15717 machine_mode mode;
15718 int i;
15720 /* Create the first one, so we have something to add to. */
15721 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
15722 VOIDmode, initialized);
15723 if (loc_result == NULL)
15724 return NULL;
15725 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
15726 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15727 for (i = 1; i < num_elem; i++)
15729 dw_loc_descr_ref temp;
15731 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
15732 VOIDmode, initialized);
15733 if (temp == NULL)
15734 return NULL;
15735 add_loc_descr (&loc_result, temp);
15736 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
15737 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15740 break;
15742 case CONST_INT:
15743 if (mode != VOIDmode && mode != BLKmode)
15744 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
15745 INTVAL (rtl));
15746 break;
15748 case CONST_DOUBLE:
15749 if (mode == VOIDmode)
15750 mode = GET_MODE (rtl);
15752 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15754 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15756 /* Note that a CONST_DOUBLE rtx could represent either an integer
15757 or a floating-point constant. A CONST_DOUBLE is used whenever
15758 the constant requires more than one word in order to be
15759 adequately represented. We output CONST_DOUBLEs as blocks. */
15760 loc_result = new_loc_descr (DW_OP_implicit_value,
15761 GET_MODE_SIZE (mode), 0);
15762 #if TARGET_SUPPORTS_WIDE_INT == 0
15763 if (!SCALAR_FLOAT_MODE_P (mode))
15765 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
15766 loc_result->dw_loc_oprnd2.v.val_double
15767 = rtx_to_double_int (rtl);
15769 else
15770 #endif
15772 unsigned int length = GET_MODE_SIZE (mode);
15773 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15775 insert_float (rtl, array);
15776 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15777 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15778 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15779 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15782 break;
15784 case CONST_WIDE_INT:
15785 if (mode == VOIDmode)
15786 mode = GET_MODE (rtl);
15788 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15790 loc_result = new_loc_descr (DW_OP_implicit_value,
15791 GET_MODE_SIZE (mode), 0);
15792 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
15793 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15794 *loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
15796 break;
15798 case CONST_VECTOR:
15799 if (mode == VOIDmode)
15800 mode = GET_MODE (rtl);
15802 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15804 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
15805 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15806 unsigned char *array
15807 = ggc_vec_alloc<unsigned char> (length * elt_size);
15808 unsigned int i;
15809 unsigned char *p;
15810 machine_mode imode = GET_MODE_INNER (mode);
15812 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15813 switch (GET_MODE_CLASS (mode))
15815 case MODE_VECTOR_INT:
15816 for (i = 0, p = array; i < length; i++, p += elt_size)
15818 rtx elt = CONST_VECTOR_ELT (rtl, i);
15819 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
15821 break;
15823 case MODE_VECTOR_FLOAT:
15824 for (i = 0, p = array; i < length; i++, p += elt_size)
15826 rtx elt = CONST_VECTOR_ELT (rtl, i);
15827 insert_float (elt, p);
15829 break;
15831 default:
15832 gcc_unreachable ();
15835 loc_result = new_loc_descr (DW_OP_implicit_value,
15836 length * elt_size, 0);
15837 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15838 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
15839 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
15840 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15842 break;
15844 case CONST:
15845 if (mode == VOIDmode
15846 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
15847 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
15848 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
15850 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
15851 break;
15853 /* FALLTHROUGH */
15854 case SYMBOL_REF:
15855 if (!const_ok_for_output (rtl))
15856 break;
15857 /* FALLTHROUGH */
15858 case LABEL_REF:
15859 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
15860 && (dwarf_version >= 4 || !dwarf_strict))
15862 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15863 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15864 vec_safe_push (used_rtx_array, rtl);
15866 break;
15868 case DEBUG_IMPLICIT_PTR:
15869 loc_result = implicit_ptr_descriptor (rtl, 0);
15870 break;
15872 case PLUS:
15873 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
15874 && CONST_INT_P (XEXP (rtl, 1)))
15876 loc_result
15877 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
15878 break;
15880 /* FALLTHRU */
15881 do_default:
15882 default:
15883 if ((SCALAR_INT_MODE_P (mode)
15884 && GET_MODE (rtl) == mode
15885 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
15886 && dwarf_version >= 4)
15887 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
15889 /* Value expression. */
15890 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
15891 if (loc_result)
15892 add_loc_descr (&loc_result,
15893 new_loc_descr (DW_OP_stack_value, 0, 0));
15895 break;
15898 return loc_result;
15901 /* We need to figure out what section we should use as the base for the
15902 address ranges where a given location is valid.
15903 1. If this particular DECL has a section associated with it, use that.
15904 2. If this function has a section associated with it, use that.
15905 3. Otherwise, use the text section.
15906 XXX: If you split a variable across multiple sections, we won't notice. */
15908 static const char *
15909 secname_for_decl (const_tree decl)
15911 const char *secname;
15913 if (VAR_OR_FUNCTION_DECL_P (decl)
15914 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
15915 && DECL_SECTION_NAME (decl))
15916 secname = DECL_SECTION_NAME (decl);
15917 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
15918 secname = DECL_SECTION_NAME (current_function_decl);
15919 else if (cfun && in_cold_section_p)
15920 secname = crtl->subsections.cold_section_label;
15921 else
15922 secname = text_section_label;
15924 return secname;
15927 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
15929 static bool
15930 decl_by_reference_p (tree decl)
15932 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
15933 || VAR_P (decl))
15934 && DECL_BY_REFERENCE (decl));
15937 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
15938 for VARLOC. */
15940 static dw_loc_descr_ref
15941 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
15942 enum var_init_status initialized)
15944 int have_address = 0;
15945 dw_loc_descr_ref descr;
15946 machine_mode mode;
15948 if (want_address != 2)
15950 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
15951 /* Single part. */
15952 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
15954 varloc = PAT_VAR_LOCATION_LOC (varloc);
15955 if (GET_CODE (varloc) == EXPR_LIST)
15956 varloc = XEXP (varloc, 0);
15957 mode = GET_MODE (varloc);
15958 if (MEM_P (varloc))
15960 rtx addr = XEXP (varloc, 0);
15961 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
15962 mode, initialized);
15963 if (descr)
15964 have_address = 1;
15965 else
15967 rtx x = avoid_constant_pool_reference (varloc);
15968 if (x != varloc)
15969 descr = mem_loc_descriptor (x, mode, VOIDmode,
15970 initialized);
15973 else
15974 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
15976 else
15977 return 0;
15979 else
15981 if (GET_CODE (varloc) == VAR_LOCATION)
15982 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
15983 else
15984 mode = DECL_MODE (loc);
15985 descr = loc_descriptor (varloc, mode, initialized);
15986 have_address = 1;
15989 if (!descr)
15990 return 0;
15992 if (want_address == 2 && !have_address
15993 && (dwarf_version >= 4 || !dwarf_strict))
15995 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15997 expansion_failed (loc, NULL_RTX,
15998 "DWARF address size mismatch");
15999 return 0;
16001 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
16002 have_address = 1;
16004 /* Show if we can't fill the request for an address. */
16005 if (want_address && !have_address)
16007 expansion_failed (loc, NULL_RTX,
16008 "Want address and only have value");
16009 return 0;
16012 /* If we've got an address and don't want one, dereference. */
16013 if (!want_address && have_address)
16015 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
16016 enum dwarf_location_atom op;
16018 if (size > DWARF2_ADDR_SIZE || size == -1)
16020 expansion_failed (loc, NULL_RTX,
16021 "DWARF address size mismatch");
16022 return 0;
16024 else if (size == DWARF2_ADDR_SIZE)
16025 op = DW_OP_deref;
16026 else
16027 op = DW_OP_deref_size;
16029 add_loc_descr (&descr, new_loc_descr (op, size, 0));
16032 return descr;
16035 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
16036 if it is not possible. */
16038 static dw_loc_descr_ref
16039 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
16041 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
16042 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
16043 else if (dwarf_version >= 3 || !dwarf_strict)
16044 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
16045 else
16046 return NULL;
16049 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
16050 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
16052 static dw_loc_descr_ref
16053 dw_sra_loc_expr (tree decl, rtx loc)
16055 rtx p;
16056 unsigned HOST_WIDE_INT padsize = 0;
16057 dw_loc_descr_ref descr, *descr_tail;
16058 unsigned HOST_WIDE_INT decl_size;
16059 rtx varloc;
16060 enum var_init_status initialized;
16062 if (DECL_SIZE (decl) == NULL
16063 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
16064 return NULL;
16066 decl_size = tree_to_uhwi (DECL_SIZE (decl));
16067 descr = NULL;
16068 descr_tail = &descr;
16070 for (p = loc; p; p = XEXP (p, 1))
16072 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
16073 rtx loc_note = *decl_piece_varloc_ptr (p);
16074 dw_loc_descr_ref cur_descr;
16075 dw_loc_descr_ref *tail, last = NULL;
16076 unsigned HOST_WIDE_INT opsize = 0;
16078 if (loc_note == NULL_RTX
16079 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
16081 padsize += bitsize;
16082 continue;
16084 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
16085 varloc = NOTE_VAR_LOCATION (loc_note);
16086 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
16087 if (cur_descr == NULL)
16089 padsize += bitsize;
16090 continue;
16093 /* Check that cur_descr either doesn't use
16094 DW_OP_*piece operations, or their sum is equal
16095 to bitsize. Otherwise we can't embed it. */
16096 for (tail = &cur_descr; *tail != NULL;
16097 tail = &(*tail)->dw_loc_next)
16098 if ((*tail)->dw_loc_opc == DW_OP_piece)
16100 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
16101 * BITS_PER_UNIT;
16102 last = *tail;
16104 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
16106 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
16107 last = *tail;
16110 if (last != NULL && opsize != bitsize)
16112 padsize += bitsize;
16113 /* Discard the current piece of the descriptor and release any
16114 addr_table entries it uses. */
16115 remove_loc_list_addr_table_entries (cur_descr);
16116 continue;
16119 /* If there is a hole, add DW_OP_*piece after empty DWARF
16120 expression, which means that those bits are optimized out. */
16121 if (padsize)
16123 if (padsize > decl_size)
16125 remove_loc_list_addr_table_entries (cur_descr);
16126 goto discard_descr;
16128 decl_size -= padsize;
16129 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
16130 if (*descr_tail == NULL)
16132 remove_loc_list_addr_table_entries (cur_descr);
16133 goto discard_descr;
16135 descr_tail = &(*descr_tail)->dw_loc_next;
16136 padsize = 0;
16138 *descr_tail = cur_descr;
16139 descr_tail = tail;
16140 if (bitsize > decl_size)
16141 goto discard_descr;
16142 decl_size -= bitsize;
16143 if (last == NULL)
16145 HOST_WIDE_INT offset = 0;
16146 if (GET_CODE (varloc) == VAR_LOCATION
16147 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16149 varloc = PAT_VAR_LOCATION_LOC (varloc);
16150 if (GET_CODE (varloc) == EXPR_LIST)
16151 varloc = XEXP (varloc, 0);
16155 if (GET_CODE (varloc) == CONST
16156 || GET_CODE (varloc) == SIGN_EXTEND
16157 || GET_CODE (varloc) == ZERO_EXTEND)
16158 varloc = XEXP (varloc, 0);
16159 else if (GET_CODE (varloc) == SUBREG)
16160 varloc = SUBREG_REG (varloc);
16161 else
16162 break;
16164 while (1);
16165 /* DW_OP_bit_size offset should be zero for register
16166 or implicit location descriptions and empty location
16167 descriptions, but for memory addresses needs big endian
16168 adjustment. */
16169 if (MEM_P (varloc))
16171 unsigned HOST_WIDE_INT memsize
16172 = MEM_SIZE (varloc) * BITS_PER_UNIT;
16173 if (memsize != bitsize)
16175 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
16176 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
16177 goto discard_descr;
16178 if (memsize < bitsize)
16179 goto discard_descr;
16180 if (BITS_BIG_ENDIAN)
16181 offset = memsize - bitsize;
16185 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
16186 if (*descr_tail == NULL)
16187 goto discard_descr;
16188 descr_tail = &(*descr_tail)->dw_loc_next;
16192 /* If there were any non-empty expressions, add padding till the end of
16193 the decl. */
16194 if (descr != NULL && decl_size != 0)
16196 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
16197 if (*descr_tail == NULL)
16198 goto discard_descr;
16200 return descr;
16202 discard_descr:
16203 /* Discard the descriptor and release any addr_table entries it uses. */
16204 remove_loc_list_addr_table_entries (descr);
16205 return NULL;
16208 /* Return the dwarf representation of the location list LOC_LIST of
16209 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
16210 function. */
16212 static dw_loc_list_ref
16213 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
16215 const char *endname, *secname;
16216 rtx varloc;
16217 enum var_init_status initialized;
16218 struct var_loc_node *node;
16219 dw_loc_descr_ref descr;
16220 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
16221 dw_loc_list_ref list = NULL;
16222 dw_loc_list_ref *listp = &list;
16224 /* Now that we know what section we are using for a base,
16225 actually construct the list of locations.
16226 The first location information is what is passed to the
16227 function that creates the location list, and the remaining
16228 locations just get added on to that list.
16229 Note that we only know the start address for a location
16230 (IE location changes), so to build the range, we use
16231 the range [current location start, next location start].
16232 This means we have to special case the last node, and generate
16233 a range of [last location start, end of function label]. */
16235 secname = secname_for_decl (decl);
16237 for (node = loc_list->first; node; node = node->next)
16238 if (GET_CODE (node->loc) == EXPR_LIST
16239 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
16241 if (GET_CODE (node->loc) == EXPR_LIST)
16243 /* This requires DW_OP_{,bit_}piece, which is not usable
16244 inside DWARF expressions. */
16245 if (want_address != 2)
16246 continue;
16247 descr = dw_sra_loc_expr (decl, node->loc);
16248 if (descr == NULL)
16249 continue;
16251 else
16253 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16254 varloc = NOTE_VAR_LOCATION (node->loc);
16255 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
16257 if (descr)
16259 bool range_across_switch = false;
16260 /* If section switch happens in between node->label
16261 and node->next->label (or end of function) and
16262 we can't emit it as a single entry list,
16263 emit two ranges, first one ending at the end
16264 of first partition and second one starting at the
16265 beginning of second partition. */
16266 if (node == loc_list->last_before_switch
16267 && (node != loc_list->first || loc_list->first->next)
16268 && current_function_decl)
16270 endname = cfun->fde->dw_fde_end;
16271 range_across_switch = true;
16273 /* The variable has a location between NODE->LABEL and
16274 NODE->NEXT->LABEL. */
16275 else if (node->next)
16276 endname = node->next->label;
16277 /* If the variable has a location at the last label
16278 it keeps its location until the end of function. */
16279 else if (!current_function_decl)
16280 endname = text_end_label;
16281 else
16283 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
16284 current_function_funcdef_no);
16285 endname = ggc_strdup (label_id);
16288 *listp = new_loc_list (descr, node->label, endname, secname);
16289 if (TREE_CODE (decl) == PARM_DECL
16290 && node == loc_list->first
16291 && NOTE_P (node->loc)
16292 && strcmp (node->label, endname) == 0)
16293 (*listp)->force = true;
16294 listp = &(*listp)->dw_loc_next;
16296 if (range_across_switch)
16298 if (GET_CODE (node->loc) == EXPR_LIST)
16299 descr = dw_sra_loc_expr (decl, node->loc);
16300 else
16302 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16303 varloc = NOTE_VAR_LOCATION (node->loc);
16304 descr = dw_loc_list_1 (decl, varloc, want_address,
16305 initialized);
16307 gcc_assert (descr);
16308 /* The variable has a location between NODE->LABEL and
16309 NODE->NEXT->LABEL. */
16310 if (node->next)
16311 endname = node->next->label;
16312 else
16313 endname = cfun->fde->dw_fde_second_end;
16314 *listp = new_loc_list (descr,
16315 cfun->fde->dw_fde_second_begin,
16316 endname, secname);
16317 listp = &(*listp)->dw_loc_next;
16322 /* Try to avoid the overhead of a location list emitting a location
16323 expression instead, but only if we didn't have more than one
16324 location entry in the first place. If some entries were not
16325 representable, we don't want to pretend a single entry that was
16326 applies to the entire scope in which the variable is
16327 available. */
16328 if (list && loc_list->first->next)
16329 gen_llsym (list);
16331 return list;
16334 /* Return if the loc_list has only single element and thus can be represented
16335 as location description. */
16337 static bool
16338 single_element_loc_list_p (dw_loc_list_ref list)
16340 gcc_assert (!list->dw_loc_next || list->ll_symbol);
16341 return !list->ll_symbol;
16344 /* Duplicate a single element of location list. */
16346 static inline dw_loc_descr_ref
16347 copy_loc_descr (dw_loc_descr_ref ref)
16349 dw_loc_descr_ref copy = ggc_alloc<dw_loc_descr_node> ();
16350 memcpy (copy, ref, sizeof (dw_loc_descr_node));
16351 return copy;
16354 /* To each location in list LIST append loc descr REF. */
16356 static void
16357 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16359 dw_loc_descr_ref copy;
16360 add_loc_descr (&list->expr, ref);
16361 list = list->dw_loc_next;
16362 while (list)
16364 copy = copy_loc_descr (ref);
16365 add_loc_descr (&list->expr, copy);
16366 while (copy->dw_loc_next)
16367 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
16368 list = list->dw_loc_next;
16372 /* To each location in list LIST prepend loc descr REF. */
16374 static void
16375 prepend_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16377 dw_loc_descr_ref copy;
16378 dw_loc_descr_ref ref_end = list->expr;
16379 add_loc_descr (&ref, list->expr);
16380 list->expr = ref;
16381 list = list->dw_loc_next;
16382 while (list)
16384 dw_loc_descr_ref end = list->expr;
16385 list->expr = copy = copy_loc_descr (ref);
16386 while (copy->dw_loc_next != ref_end)
16387 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
16388 copy->dw_loc_next = end;
16389 list = list->dw_loc_next;
16393 /* Given two lists RET and LIST
16394 produce location list that is result of adding expression in LIST
16395 to expression in RET on each position in program.
16396 Might be destructive on both RET and LIST.
16398 TODO: We handle only simple cases of RET or LIST having at most one
16399 element. General case would involve sorting the lists in program order
16400 and merging them that will need some additional work.
16401 Adding that will improve quality of debug info especially for SRA-ed
16402 structures. */
16404 static void
16405 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
16407 if (!list)
16408 return;
16409 if (!*ret)
16411 *ret = list;
16412 return;
16414 if (!list->dw_loc_next)
16416 add_loc_descr_to_each (*ret, list->expr);
16417 return;
16419 if (!(*ret)->dw_loc_next)
16421 prepend_loc_descr_to_each (list, (*ret)->expr);
16422 *ret = list;
16423 return;
16425 expansion_failed (NULL_TREE, NULL_RTX,
16426 "Don't know how to merge two non-trivial"
16427 " location lists.\n");
16428 *ret = NULL;
16429 return;
16432 /* LOC is constant expression. Try a luck, look it up in constant
16433 pool and return its loc_descr of its address. */
16435 static dw_loc_descr_ref
16436 cst_pool_loc_descr (tree loc)
16438 /* Get an RTL for this, if something has been emitted. */
16439 rtx rtl = lookup_constant_def (loc);
16441 if (!rtl || !MEM_P (rtl))
16443 gcc_assert (!rtl);
16444 return 0;
16446 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
16448 /* TODO: We might get more coverage if we was actually delaying expansion
16449 of all expressions till end of compilation when constant pools are fully
16450 populated. */
16451 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
16453 expansion_failed (loc, NULL_RTX,
16454 "CST value in contant pool but not marked.");
16455 return 0;
16457 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
16458 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
16461 /* Return dw_loc_list representing address of addr_expr LOC
16462 by looking for inner INDIRECT_REF expression and turning
16463 it into simple arithmetics.
16465 See loc_list_from_tree for the meaning of CONTEXT. */
16467 static dw_loc_list_ref
16468 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
16469 loc_descr_context *context)
16471 tree obj, offset;
16472 HOST_WIDE_INT bitsize, bitpos, bytepos;
16473 machine_mode mode;
16474 int unsignedp, reversep, volatilep = 0;
16475 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
16477 obj = get_inner_reference (TREE_OPERAND (loc, 0),
16478 &bitsize, &bitpos, &offset, &mode,
16479 &unsignedp, &reversep, &volatilep);
16480 STRIP_NOPS (obj);
16481 if (bitpos % BITS_PER_UNIT)
16483 expansion_failed (loc, NULL_RTX, "bitfield access");
16484 return 0;
16486 if (!INDIRECT_REF_P (obj))
16488 expansion_failed (obj,
16489 NULL_RTX, "no indirect ref in inner refrence");
16490 return 0;
16492 if (!offset && !bitpos)
16493 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
16494 context);
16495 else if (toplev
16496 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
16497 && (dwarf_version >= 4 || !dwarf_strict))
16499 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
16500 if (!list_ret)
16501 return 0;
16502 if (offset)
16504 /* Variable offset. */
16505 list_ret1 = loc_list_from_tree (offset, 0, context);
16506 if (list_ret1 == 0)
16507 return 0;
16508 add_loc_list (&list_ret, list_ret1);
16509 if (!list_ret)
16510 return 0;
16511 add_loc_descr_to_each (list_ret,
16512 new_loc_descr (DW_OP_plus, 0, 0));
16514 bytepos = bitpos / BITS_PER_UNIT;
16515 if (bytepos > 0)
16516 add_loc_descr_to_each (list_ret,
16517 new_loc_descr (DW_OP_plus_uconst,
16518 bytepos, 0));
16519 else if (bytepos < 0)
16520 loc_list_plus_const (list_ret, bytepos);
16521 add_loc_descr_to_each (list_ret,
16522 new_loc_descr (DW_OP_stack_value, 0, 0));
16524 return list_ret;
16527 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
16528 all operations from LOC are nops, move to the last one. Insert in NOPS all
16529 operations that are skipped. */
16531 static void
16532 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
16533 hash_set<dw_loc_descr_ref> &nops)
16535 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
16537 nops.add (loc);
16538 loc = loc->dw_loc_next;
16542 /* Helper for loc_descr_without_nops: free the location description operation
16543 P. */
16545 bool
16546 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
16548 ggc_free (loc);
16549 return true;
16552 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
16553 finishes LOC. */
16555 static void
16556 loc_descr_without_nops (dw_loc_descr_ref &loc)
16558 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
16559 return;
16561 /* Set of all DW_OP_nop operations we remove. */
16562 hash_set<dw_loc_descr_ref> nops;
16564 /* First, strip all prefix NOP operations in order to keep the head of the
16565 operations list. */
16566 loc_descr_to_next_no_nop (loc, nops);
16568 for (dw_loc_descr_ref cur = loc; cur != NULL;)
16570 /* For control flow operations: strip "prefix" nops in destination
16571 labels. */
16572 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
16573 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
16574 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
16575 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
16577 /* Do the same for the operations that follow, then move to the next
16578 iteration. */
16579 if (cur->dw_loc_next != NULL)
16580 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
16581 cur = cur->dw_loc_next;
16584 nops.traverse<void *, free_loc_descr> (NULL);
16588 struct dwarf_procedure_info;
16590 /* Helper structure for location descriptions generation. */
16591 struct loc_descr_context
16593 /* The type that is implicitly referenced by DW_OP_push_object_address, or
16594 NULL_TREE if DW_OP_push_object_address in invalid for this location
16595 description. This is used when processing PLACEHOLDER_EXPR nodes. */
16596 tree context_type;
16597 /* The ..._DECL node that should be translated as a
16598 DW_OP_push_object_address operation. */
16599 tree base_decl;
16600 /* Information about the DWARF procedure we are currently generating. NULL if
16601 we are not generating a DWARF procedure. */
16602 struct dwarf_procedure_info *dpi;
16603 /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
16604 by consumer. Used for DW_TAG_generic_subrange attributes. */
16605 bool placeholder_arg;
16606 /* True if PLACEHOLDER_EXPR has been seen. */
16607 bool placeholder_seen;
16610 /* DWARF procedures generation
16612 DWARF expressions (aka. location descriptions) are used to encode variable
16613 things such as sizes or offsets. Such computations can have redundant parts
16614 that can be factorized in order to reduce the size of the output debug
16615 information. This is the whole point of DWARF procedures.
16617 Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
16618 already factorized into functions ("size functions") in order to handle very
16619 big and complex types. Such functions are quite simple: they have integral
16620 arguments, they return an integral result and their body contains only a
16621 return statement with arithmetic expressions. This is the only kind of
16622 function we are interested in translating into DWARF procedures, here.
16624 DWARF expressions and DWARF procedure are executed using a stack, so we have
16625 to define some calling convention for them to interact. Let's say that:
16627 - Before calling a DWARF procedure, DWARF expressions must push on the stack
16628 all arguments in reverse order (right-to-left) so that when the DWARF
16629 procedure execution starts, the first argument is the top of the stack.
16631 - Then, when returning, the DWARF procedure must have consumed all arguments
16632 on the stack, must have pushed the result and touched nothing else.
16634 - Each integral argument and the result are integral types can be hold in a
16635 single stack slot.
16637 - We call "frame offset" the number of stack slots that are "under DWARF
16638 procedure control": it includes the arguments slots, the temporaries and
16639 the result slot. Thus, it is equal to the number of arguments when the
16640 procedure execution starts and must be equal to one (the result) when it
16641 returns. */
16643 /* Helper structure used when generating operations for a DWARF procedure. */
16644 struct dwarf_procedure_info
16646 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
16647 currently translated. */
16648 tree fndecl;
16649 /* The number of arguments FNDECL takes. */
16650 unsigned args_count;
16653 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
16654 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
16655 equate it to this DIE. */
16657 static dw_die_ref
16658 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
16659 dw_die_ref parent_die)
16661 dw_die_ref dwarf_proc_die;
16663 if ((dwarf_version < 3 && dwarf_strict)
16664 || location == NULL)
16665 return NULL;
16667 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
16668 if (fndecl)
16669 equate_decl_number_to_die (fndecl, dwarf_proc_die);
16670 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
16671 return dwarf_proc_die;
16674 /* Return whether TYPE is a supported type as a DWARF procedure argument
16675 type or return type (we handle only scalar types and pointer types that
16676 aren't wider than the DWARF expression evaluation stack. */
16678 static bool
16679 is_handled_procedure_type (tree type)
16681 return ((INTEGRAL_TYPE_P (type)
16682 || TREE_CODE (type) == OFFSET_TYPE
16683 || TREE_CODE (type) == POINTER_TYPE)
16684 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
16687 /* Helper for resolve_args_picking: do the same but stop when coming across
16688 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
16689 offset *before* evaluating the corresponding operation. */
16691 static bool
16692 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
16693 struct dwarf_procedure_info *dpi,
16694 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
16696 /* The "frame_offset" identifier is already used to name a macro... */
16697 unsigned frame_offset_ = initial_frame_offset;
16698 dw_loc_descr_ref l;
16700 for (l = loc; l != NULL;)
16702 bool existed;
16703 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
16705 /* If we already met this node, there is nothing to compute anymore. */
16706 if (existed)
16708 /* Make sure that the stack size is consistent wherever the execution
16709 flow comes from. */
16710 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
16711 break;
16713 l_frame_offset = frame_offset_;
16715 /* If needed, relocate the picking offset with respect to the frame
16716 offset. */
16717 if (l->frame_offset_rel)
16719 unsigned HOST_WIDE_INT off;
16720 switch (l->dw_loc_opc)
16722 case DW_OP_pick:
16723 off = l->dw_loc_oprnd1.v.val_unsigned;
16724 break;
16725 case DW_OP_dup:
16726 off = 0;
16727 break;
16728 case DW_OP_over:
16729 off = 1;
16730 break;
16731 default:
16732 gcc_unreachable ();
16734 /* frame_offset_ is the size of the current stack frame, including
16735 incoming arguments. Besides, the arguments are pushed
16736 right-to-left. Thus, in order to access the Nth argument from
16737 this operation node, the picking has to skip temporaries *plus*
16738 one stack slot per argument (0 for the first one, 1 for the second
16739 one, etc.).
16741 The targetted argument number (N) is already set as the operand,
16742 and the number of temporaries can be computed with:
16743 frame_offsets_ - dpi->args_count */
16744 off += frame_offset_ - dpi->args_count;
16746 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
16747 if (off > 255)
16748 return false;
16750 if (off == 0)
16752 l->dw_loc_opc = DW_OP_dup;
16753 l->dw_loc_oprnd1.v.val_unsigned = 0;
16755 else if (off == 1)
16757 l->dw_loc_opc = DW_OP_over;
16758 l->dw_loc_oprnd1.v.val_unsigned = 0;
16760 else
16762 l->dw_loc_opc = DW_OP_pick;
16763 l->dw_loc_oprnd1.v.val_unsigned = off;
16767 /* Update frame_offset according to the effect the current operation has
16768 on the stack. */
16769 switch (l->dw_loc_opc)
16771 case DW_OP_deref:
16772 case DW_OP_swap:
16773 case DW_OP_rot:
16774 case DW_OP_abs:
16775 case DW_OP_neg:
16776 case DW_OP_not:
16777 case DW_OP_plus_uconst:
16778 case DW_OP_skip:
16779 case DW_OP_reg0:
16780 case DW_OP_reg1:
16781 case DW_OP_reg2:
16782 case DW_OP_reg3:
16783 case DW_OP_reg4:
16784 case DW_OP_reg5:
16785 case DW_OP_reg6:
16786 case DW_OP_reg7:
16787 case DW_OP_reg8:
16788 case DW_OP_reg9:
16789 case DW_OP_reg10:
16790 case DW_OP_reg11:
16791 case DW_OP_reg12:
16792 case DW_OP_reg13:
16793 case DW_OP_reg14:
16794 case DW_OP_reg15:
16795 case DW_OP_reg16:
16796 case DW_OP_reg17:
16797 case DW_OP_reg18:
16798 case DW_OP_reg19:
16799 case DW_OP_reg20:
16800 case DW_OP_reg21:
16801 case DW_OP_reg22:
16802 case DW_OP_reg23:
16803 case DW_OP_reg24:
16804 case DW_OP_reg25:
16805 case DW_OP_reg26:
16806 case DW_OP_reg27:
16807 case DW_OP_reg28:
16808 case DW_OP_reg29:
16809 case DW_OP_reg30:
16810 case DW_OP_reg31:
16811 case DW_OP_bregx:
16812 case DW_OP_piece:
16813 case DW_OP_deref_size:
16814 case DW_OP_nop:
16815 case DW_OP_bit_piece:
16816 case DW_OP_implicit_value:
16817 case DW_OP_stack_value:
16818 break;
16820 case DW_OP_addr:
16821 case DW_OP_const1u:
16822 case DW_OP_const1s:
16823 case DW_OP_const2u:
16824 case DW_OP_const2s:
16825 case DW_OP_const4u:
16826 case DW_OP_const4s:
16827 case DW_OP_const8u:
16828 case DW_OP_const8s:
16829 case DW_OP_constu:
16830 case DW_OP_consts:
16831 case DW_OP_dup:
16832 case DW_OP_over:
16833 case DW_OP_pick:
16834 case DW_OP_lit0:
16835 case DW_OP_lit1:
16836 case DW_OP_lit2:
16837 case DW_OP_lit3:
16838 case DW_OP_lit4:
16839 case DW_OP_lit5:
16840 case DW_OP_lit6:
16841 case DW_OP_lit7:
16842 case DW_OP_lit8:
16843 case DW_OP_lit9:
16844 case DW_OP_lit10:
16845 case DW_OP_lit11:
16846 case DW_OP_lit12:
16847 case DW_OP_lit13:
16848 case DW_OP_lit14:
16849 case DW_OP_lit15:
16850 case DW_OP_lit16:
16851 case DW_OP_lit17:
16852 case DW_OP_lit18:
16853 case DW_OP_lit19:
16854 case DW_OP_lit20:
16855 case DW_OP_lit21:
16856 case DW_OP_lit22:
16857 case DW_OP_lit23:
16858 case DW_OP_lit24:
16859 case DW_OP_lit25:
16860 case DW_OP_lit26:
16861 case DW_OP_lit27:
16862 case DW_OP_lit28:
16863 case DW_OP_lit29:
16864 case DW_OP_lit30:
16865 case DW_OP_lit31:
16866 case DW_OP_breg0:
16867 case DW_OP_breg1:
16868 case DW_OP_breg2:
16869 case DW_OP_breg3:
16870 case DW_OP_breg4:
16871 case DW_OP_breg5:
16872 case DW_OP_breg6:
16873 case DW_OP_breg7:
16874 case DW_OP_breg8:
16875 case DW_OP_breg9:
16876 case DW_OP_breg10:
16877 case DW_OP_breg11:
16878 case DW_OP_breg12:
16879 case DW_OP_breg13:
16880 case DW_OP_breg14:
16881 case DW_OP_breg15:
16882 case DW_OP_breg16:
16883 case DW_OP_breg17:
16884 case DW_OP_breg18:
16885 case DW_OP_breg19:
16886 case DW_OP_breg20:
16887 case DW_OP_breg21:
16888 case DW_OP_breg22:
16889 case DW_OP_breg23:
16890 case DW_OP_breg24:
16891 case DW_OP_breg25:
16892 case DW_OP_breg26:
16893 case DW_OP_breg27:
16894 case DW_OP_breg28:
16895 case DW_OP_breg29:
16896 case DW_OP_breg30:
16897 case DW_OP_breg31:
16898 case DW_OP_fbreg:
16899 case DW_OP_push_object_address:
16900 case DW_OP_call_frame_cfa:
16901 case DW_OP_GNU_variable_value:
16902 ++frame_offset_;
16903 break;
16905 case DW_OP_drop:
16906 case DW_OP_xderef:
16907 case DW_OP_and:
16908 case DW_OP_div:
16909 case DW_OP_minus:
16910 case DW_OP_mod:
16911 case DW_OP_mul:
16912 case DW_OP_or:
16913 case DW_OP_plus:
16914 case DW_OP_shl:
16915 case DW_OP_shr:
16916 case DW_OP_shra:
16917 case DW_OP_xor:
16918 case DW_OP_bra:
16919 case DW_OP_eq:
16920 case DW_OP_ge:
16921 case DW_OP_gt:
16922 case DW_OP_le:
16923 case DW_OP_lt:
16924 case DW_OP_ne:
16925 case DW_OP_regx:
16926 case DW_OP_xderef_size:
16927 --frame_offset_;
16928 break;
16930 case DW_OP_call2:
16931 case DW_OP_call4:
16932 case DW_OP_call_ref:
16934 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
16935 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
16937 if (stack_usage == NULL)
16938 return false;
16939 frame_offset_ += *stack_usage;
16940 break;
16943 case DW_OP_implicit_pointer:
16944 case DW_OP_entry_value:
16945 case DW_OP_const_type:
16946 case DW_OP_regval_type:
16947 case DW_OP_deref_type:
16948 case DW_OP_convert:
16949 case DW_OP_reinterpret:
16950 case DW_OP_form_tls_address:
16951 case DW_OP_GNU_push_tls_address:
16952 case DW_OP_GNU_uninit:
16953 case DW_OP_GNU_encoded_addr:
16954 case DW_OP_GNU_implicit_pointer:
16955 case DW_OP_GNU_entry_value:
16956 case DW_OP_GNU_const_type:
16957 case DW_OP_GNU_regval_type:
16958 case DW_OP_GNU_deref_type:
16959 case DW_OP_GNU_convert:
16960 case DW_OP_GNU_reinterpret:
16961 case DW_OP_GNU_parameter_ref:
16962 /* loc_list_from_tree will probably not output these operations for
16963 size functions, so assume they will not appear here. */
16964 /* Fall through... */
16966 default:
16967 gcc_unreachable ();
16970 /* Now, follow the control flow (except subroutine calls). */
16971 switch (l->dw_loc_opc)
16973 case DW_OP_bra:
16974 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
16975 frame_offsets))
16976 return false;
16977 /* Fall through. */
16979 case DW_OP_skip:
16980 l = l->dw_loc_oprnd1.v.val_loc;
16981 break;
16983 case DW_OP_stack_value:
16984 return true;
16986 default:
16987 l = l->dw_loc_next;
16988 break;
16992 return true;
16995 /* Make a DFS over operations reachable through LOC (i.e. follow branch
16996 operations) in order to resolve the operand of DW_OP_pick operations that
16997 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
16998 offset *before* LOC is executed. Return if all relocations were
16999 successful. */
17001 static bool
17002 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
17003 struct dwarf_procedure_info *dpi)
17005 /* Associate to all visited operations the frame offset *before* evaluating
17006 this operation. */
17007 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
17009 return resolve_args_picking_1 (loc, initial_frame_offset, dpi,
17010 frame_offsets);
17013 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
17014 Return NULL if it is not possible. */
17016 static dw_die_ref
17017 function_to_dwarf_procedure (tree fndecl)
17019 struct loc_descr_context ctx;
17020 struct dwarf_procedure_info dpi;
17021 dw_die_ref dwarf_proc_die;
17022 tree tree_body = DECL_SAVED_TREE (fndecl);
17023 dw_loc_descr_ref loc_body, epilogue;
17025 tree cursor;
17026 unsigned i;
17028 /* Do not generate multiple DWARF procedures for the same function
17029 declaration. */
17030 dwarf_proc_die = lookup_decl_die (fndecl);
17031 if (dwarf_proc_die != NULL)
17032 return dwarf_proc_die;
17034 /* DWARF procedures are available starting with the DWARFv3 standard. */
17035 if (dwarf_version < 3 && dwarf_strict)
17036 return NULL;
17038 /* We handle only functions for which we still have a body, that return a
17039 supported type and that takes arguments with supported types. Note that
17040 there is no point translating functions that return nothing. */
17041 if (tree_body == NULL_TREE
17042 || DECL_RESULT (fndecl) == NULL_TREE
17043 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
17044 return NULL;
17046 for (cursor = DECL_ARGUMENTS (fndecl);
17047 cursor != NULL_TREE;
17048 cursor = TREE_CHAIN (cursor))
17049 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
17050 return NULL;
17052 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
17053 if (TREE_CODE (tree_body) != RETURN_EXPR)
17054 return NULL;
17055 tree_body = TREE_OPERAND (tree_body, 0);
17056 if (TREE_CODE (tree_body) != MODIFY_EXPR
17057 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
17058 return NULL;
17059 tree_body = TREE_OPERAND (tree_body, 1);
17061 /* Try to translate the body expression itself. Note that this will probably
17062 cause an infinite recursion if its call graph has a cycle. This is very
17063 unlikely for size functions, however, so don't bother with such things at
17064 the moment. */
17065 ctx.context_type = NULL_TREE;
17066 ctx.base_decl = NULL_TREE;
17067 ctx.dpi = &dpi;
17068 ctx.placeholder_arg = false;
17069 ctx.placeholder_seen = false;
17070 dpi.fndecl = fndecl;
17071 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
17072 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
17073 if (!loc_body)
17074 return NULL;
17076 /* After evaluating all operands in "loc_body", we should still have on the
17077 stack all arguments plus the desired function result (top of the stack).
17078 Generate code in order to keep only the result in our stack frame. */
17079 epilogue = NULL;
17080 for (i = 0; i < dpi.args_count; ++i)
17082 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
17083 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
17084 op_couple->dw_loc_next->dw_loc_next = epilogue;
17085 epilogue = op_couple;
17087 add_loc_descr (&loc_body, epilogue);
17088 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
17089 return NULL;
17091 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
17092 because they are considered useful. Now there is an epilogue, they are
17093 not anymore, so give it another try. */
17094 loc_descr_without_nops (loc_body);
17096 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
17097 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
17098 though, given that size functions do not come from source, so they should
17099 not have a dedicated DW_TAG_subprogram DIE. */
17100 dwarf_proc_die
17101 = new_dwarf_proc_die (loc_body, fndecl,
17102 get_context_die (DECL_CONTEXT (fndecl)));
17104 /* The called DWARF procedure consumes one stack slot per argument and
17105 returns one stack slot. */
17106 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
17108 return dwarf_proc_die;
17112 /* Generate Dwarf location list representing LOC.
17113 If WANT_ADDRESS is false, expression computing LOC will be computed
17114 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
17115 if WANT_ADDRESS is 2, expression computing address useable in location
17116 will be returned (i.e. DW_OP_reg can be used
17117 to refer to register values).
17119 CONTEXT provides information to customize the location descriptions
17120 generation. Its context_type field specifies what type is implicitly
17121 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
17122 will not be generated.
17124 Its DPI field determines whether we are generating a DWARF expression for a
17125 DWARF procedure, so PARM_DECL references are processed specifically.
17127 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
17128 and dpi fields were null. */
17130 static dw_loc_list_ref
17131 loc_list_from_tree_1 (tree loc, int want_address,
17132 struct loc_descr_context *context)
17134 dw_loc_descr_ref ret = NULL, ret1 = NULL;
17135 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
17136 int have_address = 0;
17137 enum dwarf_location_atom op;
17139 /* ??? Most of the time we do not take proper care for sign/zero
17140 extending the values properly. Hopefully this won't be a real
17141 problem... */
17143 if (context != NULL
17144 && context->base_decl == loc
17145 && want_address == 0)
17147 if (dwarf_version >= 3 || !dwarf_strict)
17148 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
17149 NULL, NULL, NULL);
17150 else
17151 return NULL;
17154 switch (TREE_CODE (loc))
17156 case ERROR_MARK:
17157 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
17158 return 0;
17160 case PLACEHOLDER_EXPR:
17161 /* This case involves extracting fields from an object to determine the
17162 position of other fields. It is supposed to appear only as the first
17163 operand of COMPONENT_REF nodes and to reference precisely the type
17164 that the context allows. */
17165 if (context != NULL
17166 && TREE_TYPE (loc) == context->context_type
17167 && want_address >= 1)
17169 if (dwarf_version >= 3 || !dwarf_strict)
17171 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
17172 have_address = 1;
17173 break;
17175 else
17176 return NULL;
17178 /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
17179 the single argument passed by consumer. */
17180 else if (context != NULL
17181 && context->placeholder_arg
17182 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
17183 && want_address == 0)
17185 ret = new_loc_descr (DW_OP_pick, 0, 0);
17186 ret->frame_offset_rel = 1;
17187 context->placeholder_seen = true;
17188 break;
17190 else
17191 expansion_failed (loc, NULL_RTX,
17192 "PLACEHOLDER_EXPR for an unexpected type");
17193 break;
17195 case CALL_EXPR:
17197 const int nargs = call_expr_nargs (loc);
17198 tree callee = get_callee_fndecl (loc);
17199 int i;
17200 dw_die_ref dwarf_proc;
17202 if (callee == NULL_TREE)
17203 goto call_expansion_failed;
17205 /* We handle only functions that return an integer. */
17206 if (!is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee))))
17207 goto call_expansion_failed;
17209 dwarf_proc = function_to_dwarf_procedure (callee);
17210 if (dwarf_proc == NULL)
17211 goto call_expansion_failed;
17213 /* Evaluate arguments right-to-left so that the first argument will
17214 be the top-most one on the stack. */
17215 for (i = nargs - 1; i >= 0; --i)
17217 dw_loc_descr_ref loc_descr
17218 = loc_descriptor_from_tree (CALL_EXPR_ARG (loc, i), 0,
17219 context);
17221 if (loc_descr == NULL)
17222 goto call_expansion_failed;
17224 add_loc_descr (&ret, loc_descr);
17227 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
17228 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17229 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
17230 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
17231 add_loc_descr (&ret, ret1);
17232 break;
17234 call_expansion_failed:
17235 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
17236 /* There are no opcodes for these operations. */
17237 return 0;
17240 case PREINCREMENT_EXPR:
17241 case PREDECREMENT_EXPR:
17242 case POSTINCREMENT_EXPR:
17243 case POSTDECREMENT_EXPR:
17244 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
17245 /* There are no opcodes for these operations. */
17246 return 0;
17248 case ADDR_EXPR:
17249 /* If we already want an address, see if there is INDIRECT_REF inside
17250 e.g. for &this->field. */
17251 if (want_address)
17253 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
17254 (loc, want_address == 2, context);
17255 if (list_ret)
17256 have_address = 1;
17257 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
17258 && (ret = cst_pool_loc_descr (loc)))
17259 have_address = 1;
17261 /* Otherwise, process the argument and look for the address. */
17262 if (!list_ret && !ret)
17263 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
17264 else
17266 if (want_address)
17267 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
17268 return NULL;
17270 break;
17272 case VAR_DECL:
17273 if (DECL_THREAD_LOCAL_P (loc))
17275 rtx rtl;
17276 enum dwarf_location_atom tls_op;
17277 enum dtprel_bool dtprel = dtprel_false;
17279 if (targetm.have_tls)
17281 /* If this is not defined, we have no way to emit the
17282 data. */
17283 if (!targetm.asm_out.output_dwarf_dtprel)
17284 return 0;
17286 /* The way DW_OP_GNU_push_tls_address is specified, we
17287 can only look up addresses of objects in the current
17288 module. We used DW_OP_addr as first op, but that's
17289 wrong, because DW_OP_addr is relocated by the debug
17290 info consumer, while DW_OP_GNU_push_tls_address
17291 operand shouldn't be. */
17292 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
17293 return 0;
17294 dtprel = dtprel_true;
17295 /* We check for DWARF 5 here because gdb did not implement
17296 DW_OP_form_tls_address until after 7.12. */
17297 tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
17298 : DW_OP_GNU_push_tls_address);
17300 else
17302 if (!targetm.emutls.debug_form_tls_address
17303 || !(dwarf_version >= 3 || !dwarf_strict))
17304 return 0;
17305 /* We stuffed the control variable into the DECL_VALUE_EXPR
17306 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
17307 no longer appear in gimple code. We used the control
17308 variable in specific so that we could pick it up here. */
17309 loc = DECL_VALUE_EXPR (loc);
17310 tls_op = DW_OP_form_tls_address;
17313 rtl = rtl_for_decl_location (loc);
17314 if (rtl == NULL_RTX)
17315 return 0;
17317 if (!MEM_P (rtl))
17318 return 0;
17319 rtl = XEXP (rtl, 0);
17320 if (! CONSTANT_P (rtl))
17321 return 0;
17323 ret = new_addr_loc_descr (rtl, dtprel);
17324 ret1 = new_loc_descr (tls_op, 0, 0);
17325 add_loc_descr (&ret, ret1);
17327 have_address = 1;
17328 break;
17330 /* FALLTHRU */
17332 case PARM_DECL:
17333 if (context != NULL && context->dpi != NULL
17334 && DECL_CONTEXT (loc) == context->dpi->fndecl)
17336 /* We are generating code for a DWARF procedure and we want to access
17337 one of its arguments: find the appropriate argument offset and let
17338 the resolve_args_picking pass compute the offset that complies
17339 with the stack frame size. */
17340 unsigned i = 0;
17341 tree cursor;
17343 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
17344 cursor != NULL_TREE && cursor != loc;
17345 cursor = TREE_CHAIN (cursor), ++i)
17347 /* If we are translating a DWARF procedure, all referenced parameters
17348 must belong to the current function. */
17349 gcc_assert (cursor != NULL_TREE);
17351 ret = new_loc_descr (DW_OP_pick, i, 0);
17352 ret->frame_offset_rel = 1;
17353 break;
17355 /* FALLTHRU */
17357 case RESULT_DECL:
17358 if (DECL_HAS_VALUE_EXPR_P (loc))
17359 return loc_list_from_tree_1 (DECL_VALUE_EXPR (loc),
17360 want_address, context);
17361 /* FALLTHRU */
17363 case FUNCTION_DECL:
17365 rtx rtl;
17366 var_loc_list *loc_list = lookup_decl_loc (loc);
17368 if (loc_list && loc_list->first)
17370 list_ret = dw_loc_list (loc_list, loc, want_address);
17371 have_address = want_address != 0;
17372 break;
17374 rtl = rtl_for_decl_location (loc);
17375 if (rtl == NULL_RTX)
17377 if (TREE_CODE (loc) != FUNCTION_DECL
17378 && early_dwarf
17379 && current_function_decl
17380 && want_address != 1
17381 && ! DECL_IGNORED_P (loc)
17382 && (INTEGRAL_TYPE_P (TREE_TYPE (loc))
17383 || POINTER_TYPE_P (TREE_TYPE (loc)))
17384 && DECL_CONTEXT (loc) == current_function_decl
17385 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (loc)))
17386 <= DWARF2_ADDR_SIZE))
17388 dw_die_ref ref = lookup_decl_die (loc);
17389 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
17390 if (ref)
17392 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17393 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
17394 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
17396 else
17398 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
17399 ret->dw_loc_oprnd1.v.val_decl_ref = loc;
17401 break;
17403 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
17404 return 0;
17406 else if (CONST_INT_P (rtl))
17408 HOST_WIDE_INT val = INTVAL (rtl);
17409 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17410 val &= GET_MODE_MASK (DECL_MODE (loc));
17411 ret = int_loc_descriptor (val);
17413 else if (GET_CODE (rtl) == CONST_STRING)
17415 expansion_failed (loc, NULL_RTX, "CONST_STRING");
17416 return 0;
17418 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
17419 ret = new_addr_loc_descr (rtl, dtprel_false);
17420 else
17422 machine_mode mode, mem_mode;
17424 /* Certain constructs can only be represented at top-level. */
17425 if (want_address == 2)
17427 ret = loc_descriptor (rtl, VOIDmode,
17428 VAR_INIT_STATUS_INITIALIZED);
17429 have_address = 1;
17431 else
17433 mode = GET_MODE (rtl);
17434 mem_mode = VOIDmode;
17435 if (MEM_P (rtl))
17437 mem_mode = mode;
17438 mode = get_address_mode (rtl);
17439 rtl = XEXP (rtl, 0);
17440 have_address = 1;
17442 ret = mem_loc_descriptor (rtl, mode, mem_mode,
17443 VAR_INIT_STATUS_INITIALIZED);
17445 if (!ret)
17446 expansion_failed (loc, rtl,
17447 "failed to produce loc descriptor for rtl");
17450 break;
17452 case MEM_REF:
17453 if (!integer_zerop (TREE_OPERAND (loc, 1)))
17455 have_address = 1;
17456 goto do_plus;
17458 /* Fallthru. */
17459 case INDIRECT_REF:
17460 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17461 have_address = 1;
17462 break;
17464 case TARGET_MEM_REF:
17465 case SSA_NAME:
17466 case DEBUG_EXPR_DECL:
17467 return NULL;
17469 case COMPOUND_EXPR:
17470 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
17471 context);
17473 CASE_CONVERT:
17474 case VIEW_CONVERT_EXPR:
17475 case SAVE_EXPR:
17476 case MODIFY_EXPR:
17477 case NON_LVALUE_EXPR:
17478 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
17479 context);
17481 case COMPONENT_REF:
17482 case BIT_FIELD_REF:
17483 case ARRAY_REF:
17484 case ARRAY_RANGE_REF:
17485 case REALPART_EXPR:
17486 case IMAGPART_EXPR:
17488 tree obj, offset;
17489 HOST_WIDE_INT bitsize, bitpos, bytepos;
17490 machine_mode mode;
17491 int unsignedp, reversep, volatilep = 0;
17493 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
17494 &unsignedp, &reversep, &volatilep);
17496 gcc_assert (obj != loc);
17498 list_ret = loc_list_from_tree_1 (obj,
17499 want_address == 2
17500 && !bitpos && !offset ? 2 : 1,
17501 context);
17502 /* TODO: We can extract value of the small expression via shifting even
17503 for nonzero bitpos. */
17504 if (list_ret == 0)
17505 return 0;
17506 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
17508 expansion_failed (loc, NULL_RTX,
17509 "bitfield access");
17510 return 0;
17513 if (offset != NULL_TREE)
17515 /* Variable offset. */
17516 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
17517 if (list_ret1 == 0)
17518 return 0;
17519 add_loc_list (&list_ret, list_ret1);
17520 if (!list_ret)
17521 return 0;
17522 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
17525 bytepos = bitpos / BITS_PER_UNIT;
17526 if (bytepos > 0)
17527 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
17528 else if (bytepos < 0)
17529 loc_list_plus_const (list_ret, bytepos);
17531 have_address = 1;
17532 break;
17535 case INTEGER_CST:
17536 if ((want_address || !tree_fits_shwi_p (loc))
17537 && (ret = cst_pool_loc_descr (loc)))
17538 have_address = 1;
17539 else if (want_address == 2
17540 && tree_fits_shwi_p (loc)
17541 && (ret = address_of_int_loc_descriptor
17542 (int_size_in_bytes (TREE_TYPE (loc)),
17543 tree_to_shwi (loc))))
17544 have_address = 1;
17545 else if (tree_fits_shwi_p (loc))
17546 ret = int_loc_descriptor (tree_to_shwi (loc));
17547 else if (tree_fits_uhwi_p (loc))
17548 ret = uint_loc_descriptor (tree_to_uhwi (loc));
17549 else
17551 expansion_failed (loc, NULL_RTX,
17552 "Integer operand is not host integer");
17553 return 0;
17555 break;
17557 case CONSTRUCTOR:
17558 case REAL_CST:
17559 case STRING_CST:
17560 case COMPLEX_CST:
17561 if ((ret = cst_pool_loc_descr (loc)))
17562 have_address = 1;
17563 else if (TREE_CODE (loc) == CONSTRUCTOR)
17565 tree type = TREE_TYPE (loc);
17566 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
17567 unsigned HOST_WIDE_INT offset = 0;
17568 unsigned HOST_WIDE_INT cnt;
17569 constructor_elt *ce;
17571 if (TREE_CODE (type) == RECORD_TYPE)
17573 /* This is very limited, but it's enough to output
17574 pointers to member functions, as long as the
17575 referenced function is defined in the current
17576 translation unit. */
17577 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
17579 tree val = ce->value;
17581 tree field = ce->index;
17583 if (val)
17584 STRIP_NOPS (val);
17586 if (!field || DECL_BIT_FIELD (field))
17588 expansion_failed (loc, NULL_RTX,
17589 "bitfield in record type constructor");
17590 size = offset = (unsigned HOST_WIDE_INT)-1;
17591 ret = NULL;
17592 break;
17595 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
17596 unsigned HOST_WIDE_INT pos = int_byte_position (field);
17597 gcc_assert (pos + fieldsize <= size);
17598 if (pos < offset)
17600 expansion_failed (loc, NULL_RTX,
17601 "out-of-order fields in record constructor");
17602 size = offset = (unsigned HOST_WIDE_INT)-1;
17603 ret = NULL;
17604 break;
17606 if (pos > offset)
17608 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
17609 add_loc_descr (&ret, ret1);
17610 offset = pos;
17612 if (val && fieldsize != 0)
17614 ret1 = loc_descriptor_from_tree (val, want_address, context);
17615 if (!ret1)
17617 expansion_failed (loc, NULL_RTX,
17618 "unsupported expression in field");
17619 size = offset = (unsigned HOST_WIDE_INT)-1;
17620 ret = NULL;
17621 break;
17623 add_loc_descr (&ret, ret1);
17625 if (fieldsize)
17627 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
17628 add_loc_descr (&ret, ret1);
17629 offset = pos + fieldsize;
17633 if (offset != size)
17635 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
17636 add_loc_descr (&ret, ret1);
17637 offset = size;
17640 have_address = !!want_address;
17642 else
17643 expansion_failed (loc, NULL_RTX,
17644 "constructor of non-record type");
17646 else
17647 /* We can construct small constants here using int_loc_descriptor. */
17648 expansion_failed (loc, NULL_RTX,
17649 "constructor or constant not in constant pool");
17650 break;
17652 case TRUTH_AND_EXPR:
17653 case TRUTH_ANDIF_EXPR:
17654 case BIT_AND_EXPR:
17655 op = DW_OP_and;
17656 goto do_binop;
17658 case TRUTH_XOR_EXPR:
17659 case BIT_XOR_EXPR:
17660 op = DW_OP_xor;
17661 goto do_binop;
17663 case TRUTH_OR_EXPR:
17664 case TRUTH_ORIF_EXPR:
17665 case BIT_IOR_EXPR:
17666 op = DW_OP_or;
17667 goto do_binop;
17669 case FLOOR_DIV_EXPR:
17670 case CEIL_DIV_EXPR:
17671 case ROUND_DIV_EXPR:
17672 case TRUNC_DIV_EXPR:
17673 case EXACT_DIV_EXPR:
17674 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17675 return 0;
17676 op = DW_OP_div;
17677 goto do_binop;
17679 case MINUS_EXPR:
17680 op = DW_OP_minus;
17681 goto do_binop;
17683 case FLOOR_MOD_EXPR:
17684 case CEIL_MOD_EXPR:
17685 case ROUND_MOD_EXPR:
17686 case TRUNC_MOD_EXPR:
17687 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17689 op = DW_OP_mod;
17690 goto do_binop;
17692 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17693 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17694 if (list_ret == 0 || list_ret1 == 0)
17695 return 0;
17697 add_loc_list (&list_ret, list_ret1);
17698 if (list_ret == 0)
17699 return 0;
17700 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17701 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17702 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
17703 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
17704 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
17705 break;
17707 case MULT_EXPR:
17708 op = DW_OP_mul;
17709 goto do_binop;
17711 case LSHIFT_EXPR:
17712 op = DW_OP_shl;
17713 goto do_binop;
17715 case RSHIFT_EXPR:
17716 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
17717 goto do_binop;
17719 case POINTER_PLUS_EXPR:
17720 case PLUS_EXPR:
17721 do_plus:
17722 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
17724 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
17725 smarter to encode their opposite. The DW_OP_plus_uconst operation
17726 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
17727 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
17728 bytes, Y being the size of the operation that pushes the opposite
17729 of the addend. So let's choose the smallest representation. */
17730 const tree tree_addend = TREE_OPERAND (loc, 1);
17731 offset_int wi_addend;
17732 HOST_WIDE_INT shwi_addend;
17733 dw_loc_descr_ref loc_naddend;
17735 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17736 if (list_ret == 0)
17737 return 0;
17739 /* Try to get the literal to push. It is the opposite of the addend,
17740 so as we rely on wrapping during DWARF evaluation, first decode
17741 the literal as a "DWARF-sized" signed number. */
17742 wi_addend = wi::to_offset (tree_addend);
17743 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
17744 shwi_addend = wi_addend.to_shwi ();
17745 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
17746 ? int_loc_descriptor (-shwi_addend)
17747 : NULL;
17749 if (loc_naddend != NULL
17750 && ((unsigned) size_of_uleb128 (shwi_addend)
17751 > size_of_loc_descr (loc_naddend)))
17753 add_loc_descr_to_each (list_ret, loc_naddend);
17754 add_loc_descr_to_each (list_ret,
17755 new_loc_descr (DW_OP_minus, 0, 0));
17757 else
17759 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
17761 loc_naddend = loc_cur;
17762 loc_cur = loc_cur->dw_loc_next;
17763 ggc_free (loc_naddend);
17765 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
17767 break;
17770 op = DW_OP_plus;
17771 goto do_binop;
17773 case LE_EXPR:
17774 op = DW_OP_le;
17775 goto do_comp_binop;
17777 case GE_EXPR:
17778 op = DW_OP_ge;
17779 goto do_comp_binop;
17781 case LT_EXPR:
17782 op = DW_OP_lt;
17783 goto do_comp_binop;
17785 case GT_EXPR:
17786 op = DW_OP_gt;
17787 goto do_comp_binop;
17789 do_comp_binop:
17790 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
17792 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
17793 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
17794 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
17795 TREE_CODE (loc));
17796 break;
17798 else
17799 goto do_binop;
17801 case EQ_EXPR:
17802 op = DW_OP_eq;
17803 goto do_binop;
17805 case NE_EXPR:
17806 op = DW_OP_ne;
17807 goto do_binop;
17809 do_binop:
17810 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17811 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17812 if (list_ret == 0 || list_ret1 == 0)
17813 return 0;
17815 add_loc_list (&list_ret, list_ret1);
17816 if (list_ret == 0)
17817 return 0;
17818 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
17819 break;
17821 case TRUTH_NOT_EXPR:
17822 case BIT_NOT_EXPR:
17823 op = DW_OP_not;
17824 goto do_unop;
17826 case ABS_EXPR:
17827 op = DW_OP_abs;
17828 goto do_unop;
17830 case NEGATE_EXPR:
17831 op = DW_OP_neg;
17832 goto do_unop;
17834 do_unop:
17835 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17836 if (list_ret == 0)
17837 return 0;
17839 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
17840 break;
17842 case MIN_EXPR:
17843 case MAX_EXPR:
17845 const enum tree_code code =
17846 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
17848 loc = build3 (COND_EXPR, TREE_TYPE (loc),
17849 build2 (code, integer_type_node,
17850 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
17851 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
17854 /* fall through */
17856 case COND_EXPR:
17858 dw_loc_descr_ref lhs
17859 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
17860 dw_loc_list_ref rhs
17861 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
17862 dw_loc_descr_ref bra_node, jump_node, tmp;
17864 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17865 if (list_ret == 0 || lhs == 0 || rhs == 0)
17866 return 0;
17868 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
17869 add_loc_descr_to_each (list_ret, bra_node);
17871 add_loc_list (&list_ret, rhs);
17872 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
17873 add_loc_descr_to_each (list_ret, jump_node);
17875 add_loc_descr_to_each (list_ret, lhs);
17876 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
17877 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
17879 /* ??? Need a node to point the skip at. Use a nop. */
17880 tmp = new_loc_descr (DW_OP_nop, 0, 0);
17881 add_loc_descr_to_each (list_ret, tmp);
17882 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
17883 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
17885 break;
17887 case FIX_TRUNC_EXPR:
17888 return 0;
17890 default:
17891 /* Leave front-end specific codes as simply unknown. This comes
17892 up, for instance, with the C STMT_EXPR. */
17893 if ((unsigned int) TREE_CODE (loc)
17894 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
17896 expansion_failed (loc, NULL_RTX,
17897 "language specific tree node");
17898 return 0;
17901 /* Otherwise this is a generic code; we should just lists all of
17902 these explicitly. We forgot one. */
17903 if (flag_checking)
17904 gcc_unreachable ();
17906 /* In a release build, we want to degrade gracefully: better to
17907 generate incomplete debugging information than to crash. */
17908 return NULL;
17911 if (!ret && !list_ret)
17912 return 0;
17914 if (want_address == 2 && !have_address
17915 && (dwarf_version >= 4 || !dwarf_strict))
17917 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
17919 expansion_failed (loc, NULL_RTX,
17920 "DWARF address size mismatch");
17921 return 0;
17923 if (ret)
17924 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
17925 else
17926 add_loc_descr_to_each (list_ret,
17927 new_loc_descr (DW_OP_stack_value, 0, 0));
17928 have_address = 1;
17930 /* Show if we can't fill the request for an address. */
17931 if (want_address && !have_address)
17933 expansion_failed (loc, NULL_RTX,
17934 "Want address and only have value");
17935 return 0;
17938 gcc_assert (!ret || !list_ret);
17940 /* If we've got an address and don't want one, dereference. */
17941 if (!want_address && have_address)
17943 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
17945 if (size > DWARF2_ADDR_SIZE || size == -1)
17947 expansion_failed (loc, NULL_RTX,
17948 "DWARF address size mismatch");
17949 return 0;
17951 else if (size == DWARF2_ADDR_SIZE)
17952 op = DW_OP_deref;
17953 else
17954 op = DW_OP_deref_size;
17956 if (ret)
17957 add_loc_descr (&ret, new_loc_descr (op, size, 0));
17958 else
17959 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
17961 if (ret)
17962 list_ret = new_loc_list (ret, NULL, NULL, NULL);
17964 return list_ret;
17967 /* Likewise, but strip useless DW_OP_nop operations in the resulting
17968 expressions. */
17970 static dw_loc_list_ref
17971 loc_list_from_tree (tree loc, int want_address,
17972 struct loc_descr_context *context)
17974 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
17976 for (dw_loc_list_ref loc_cur = result;
17977 loc_cur != NULL; loc_cur = loc_cur->dw_loc_next)
17978 loc_descr_without_nops (loc_cur->expr);
17979 return result;
17982 /* Same as above but return only single location expression. */
17983 static dw_loc_descr_ref
17984 loc_descriptor_from_tree (tree loc, int want_address,
17985 struct loc_descr_context *context)
17987 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
17988 if (!ret)
17989 return NULL;
17990 if (ret->dw_loc_next)
17992 expansion_failed (loc, NULL_RTX,
17993 "Location list where only loc descriptor needed");
17994 return NULL;
17996 return ret->expr;
17999 /* Given a value, round it up to the lowest multiple of `boundary'
18000 which is not less than the value itself. */
18002 static inline HOST_WIDE_INT
18003 ceiling (HOST_WIDE_INT value, unsigned int boundary)
18005 return (((value + boundary - 1) / boundary) * boundary);
18008 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
18009 pointer to the declared type for the relevant field variable, or return
18010 `integer_type_node' if the given node turns out to be an
18011 ERROR_MARK node. */
18013 static inline tree
18014 field_type (const_tree decl)
18016 tree type;
18018 if (TREE_CODE (decl) == ERROR_MARK)
18019 return integer_type_node;
18021 type = DECL_BIT_FIELD_TYPE (decl);
18022 if (type == NULL_TREE)
18023 type = TREE_TYPE (decl);
18025 return type;
18028 /* Given a pointer to a tree node, return the alignment in bits for
18029 it, or else return BITS_PER_WORD if the node actually turns out to
18030 be an ERROR_MARK node. */
18032 static inline unsigned
18033 simple_type_align_in_bits (const_tree type)
18035 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
18038 static inline unsigned
18039 simple_decl_align_in_bits (const_tree decl)
18041 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
18044 /* Return the result of rounding T up to ALIGN. */
18046 static inline offset_int
18047 round_up_to_align (const offset_int &t, unsigned int align)
18049 return wi::udiv_trunc (t + align - 1, align) * align;
18052 /* Compute the size of TYPE in bytes. If possible, return NULL and store the
18053 size as an integer constant in CST_SIZE. Otherwise, if possible, return a
18054 DWARF expression that computes the size. Return NULL and set CST_SIZE to -1
18055 if we fail to return the size in one of these two forms. */
18057 static dw_loc_descr_ref
18058 type_byte_size (const_tree type, HOST_WIDE_INT *cst_size)
18060 tree tree_size;
18061 struct loc_descr_context ctx;
18063 /* Return a constant integer in priority, if possible. */
18064 *cst_size = int_size_in_bytes (type);
18065 if (*cst_size != -1)
18066 return NULL;
18068 ctx.context_type = const_cast<tree> (type);
18069 ctx.base_decl = NULL_TREE;
18070 ctx.dpi = NULL;
18071 ctx.placeholder_arg = false;
18072 ctx.placeholder_seen = false;
18074 type = TYPE_MAIN_VARIANT (type);
18075 tree_size = TYPE_SIZE_UNIT (type);
18076 return ((tree_size != NULL_TREE)
18077 ? loc_descriptor_from_tree (tree_size, 0, &ctx)
18078 : NULL);
18081 /* Helper structure for RECORD_TYPE processing. */
18082 struct vlr_context
18084 /* Root RECORD_TYPE. It is needed to generate data member location
18085 descriptions in variable-length records (VLR), but also to cope with
18086 variants, which are composed of nested structures multiplexed with
18087 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
18088 function processing a FIELD_DECL, it is required to be non null. */
18089 tree struct_type;
18090 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
18091 QUAL_UNION_TYPE), this holds an expression that computes the offset for
18092 this variant part as part of the root record (in storage units). For
18093 regular records, it must be NULL_TREE. */
18094 tree variant_part_offset;
18097 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
18098 addressed byte of the "containing object" for the given FIELD_DECL. If
18099 possible, return a native constant through CST_OFFSET (in which case NULL is
18100 returned); otherwise return a DWARF expression that computes the offset.
18102 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
18103 that offset is, either because the argument turns out to be a pointer to an
18104 ERROR_MARK node, or because the offset expression is too complex for us.
18106 CTX is required: see the comment for VLR_CONTEXT. */
18108 static dw_loc_descr_ref
18109 field_byte_offset (const_tree decl, struct vlr_context *ctx,
18110 HOST_WIDE_INT *cst_offset)
18112 tree tree_result;
18113 dw_loc_list_ref loc_result;
18115 *cst_offset = 0;
18117 if (TREE_CODE (decl) == ERROR_MARK)
18118 return NULL;
18119 else
18120 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
18122 /* We cannot handle variable bit offsets at the moment, so abort if it's the
18123 case. */
18124 if (TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST)
18125 return NULL;
18127 #ifdef PCC_BITFIELD_TYPE_MATTERS
18128 /* We used to handle only constant offsets in all cases. Now, we handle
18129 properly dynamic byte offsets only when PCC bitfield type doesn't
18130 matter. */
18131 if (PCC_BITFIELD_TYPE_MATTERS
18132 && TREE_CODE (DECL_FIELD_OFFSET (decl)) == INTEGER_CST)
18134 offset_int object_offset_in_bits;
18135 offset_int object_offset_in_bytes;
18136 offset_int bitpos_int;
18137 tree type;
18138 tree field_size_tree;
18139 offset_int deepest_bitpos;
18140 offset_int field_size_in_bits;
18141 unsigned int type_align_in_bits;
18142 unsigned int decl_align_in_bits;
18143 offset_int type_size_in_bits;
18145 bitpos_int = wi::to_offset (bit_position (decl));
18146 type = field_type (decl);
18147 type_size_in_bits = offset_int_type_size_in_bits (type);
18148 type_align_in_bits = simple_type_align_in_bits (type);
18150 field_size_tree = DECL_SIZE (decl);
18152 /* The size could be unspecified if there was an error, or for
18153 a flexible array member. */
18154 if (!field_size_tree)
18155 field_size_tree = bitsize_zero_node;
18157 /* If the size of the field is not constant, use the type size. */
18158 if (TREE_CODE (field_size_tree) == INTEGER_CST)
18159 field_size_in_bits = wi::to_offset (field_size_tree);
18160 else
18161 field_size_in_bits = type_size_in_bits;
18163 decl_align_in_bits = simple_decl_align_in_bits (decl);
18165 /* The GCC front-end doesn't make any attempt to keep track of the
18166 starting bit offset (relative to the start of the containing
18167 structure type) of the hypothetical "containing object" for a
18168 bit-field. Thus, when computing the byte offset value for the
18169 start of the "containing object" of a bit-field, we must deduce
18170 this information on our own. This can be rather tricky to do in
18171 some cases. For example, handling the following structure type
18172 definition when compiling for an i386/i486 target (which only
18173 aligns long long's to 32-bit boundaries) can be very tricky:
18175 struct S { int field1; long long field2:31; };
18177 Fortunately, there is a simple rule-of-thumb which can be used
18178 in such cases. When compiling for an i386/i486, GCC will
18179 allocate 8 bytes for the structure shown above. It decides to
18180 do this based upon one simple rule for bit-field allocation.
18181 GCC allocates each "containing object" for each bit-field at
18182 the first (i.e. lowest addressed) legitimate alignment boundary
18183 (based upon the required minimum alignment for the declared
18184 type of the field) which it can possibly use, subject to the
18185 condition that there is still enough available space remaining
18186 in the containing object (when allocated at the selected point)
18187 to fully accommodate all of the bits of the bit-field itself.
18189 This simple rule makes it obvious why GCC allocates 8 bytes for
18190 each object of the structure type shown above. When looking
18191 for a place to allocate the "containing object" for `field2',
18192 the compiler simply tries to allocate a 64-bit "containing
18193 object" at each successive 32-bit boundary (starting at zero)
18194 until it finds a place to allocate that 64- bit field such that
18195 at least 31 contiguous (and previously unallocated) bits remain
18196 within that selected 64 bit field. (As it turns out, for the
18197 example above, the compiler finds it is OK to allocate the
18198 "containing object" 64-bit field at bit-offset zero within the
18199 structure type.)
18201 Here we attempt to work backwards from the limited set of facts
18202 we're given, and we try to deduce from those facts, where GCC
18203 must have believed that the containing object started (within
18204 the structure type). The value we deduce is then used (by the
18205 callers of this routine) to generate DW_AT_location and
18206 DW_AT_bit_offset attributes for fields (both bit-fields and, in
18207 the case of DW_AT_location, regular fields as well). */
18209 /* Figure out the bit-distance from the start of the structure to
18210 the "deepest" bit of the bit-field. */
18211 deepest_bitpos = bitpos_int + field_size_in_bits;
18213 /* This is the tricky part. Use some fancy footwork to deduce
18214 where the lowest addressed bit of the containing object must
18215 be. */
18216 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18218 /* Round up to type_align by default. This works best for
18219 bitfields. */
18220 object_offset_in_bits
18221 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
18223 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
18225 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18227 /* Round up to decl_align instead. */
18228 object_offset_in_bits
18229 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
18232 object_offset_in_bytes
18233 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
18234 if (ctx->variant_part_offset == NULL_TREE)
18236 *cst_offset = object_offset_in_bytes.to_shwi ();
18237 return NULL;
18239 tree_result = wide_int_to_tree (sizetype, object_offset_in_bytes);
18241 else
18242 #endif /* PCC_BITFIELD_TYPE_MATTERS */
18243 tree_result = byte_position (decl);
18245 if (ctx->variant_part_offset != NULL_TREE)
18246 tree_result = fold_build2 (PLUS_EXPR, TREE_TYPE (tree_result),
18247 ctx->variant_part_offset, tree_result);
18249 /* If the byte offset is a constant, it's simplier to handle a native
18250 constant rather than a DWARF expression. */
18251 if (TREE_CODE (tree_result) == INTEGER_CST)
18253 *cst_offset = wi::to_offset (tree_result).to_shwi ();
18254 return NULL;
18256 struct loc_descr_context loc_ctx = {
18257 ctx->struct_type, /* context_type */
18258 NULL_TREE, /* base_decl */
18259 NULL, /* dpi */
18260 false, /* placeholder_arg */
18261 false /* placeholder_seen */
18263 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
18265 /* We want a DWARF expression: abort if we only have a location list with
18266 multiple elements. */
18267 if (!loc_result || !single_element_loc_list_p (loc_result))
18268 return NULL;
18269 else
18270 return loc_result->expr;
18273 /* The following routines define various Dwarf attributes and any data
18274 associated with them. */
18276 /* Add a location description attribute value to a DIE.
18278 This emits location attributes suitable for whole variables and
18279 whole parameters. Note that the location attributes for struct fields are
18280 generated by the routine `data_member_location_attribute' below. */
18282 static inline void
18283 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
18284 dw_loc_list_ref descr)
18286 if (descr == 0)
18287 return;
18288 if (single_element_loc_list_p (descr))
18289 add_AT_loc (die, attr_kind, descr->expr);
18290 else
18291 add_AT_loc_list (die, attr_kind, descr);
18294 /* Add DW_AT_accessibility attribute to DIE if needed. */
18296 static void
18297 add_accessibility_attribute (dw_die_ref die, tree decl)
18299 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18300 children, otherwise the default is DW_ACCESS_public. In DWARF2
18301 the default has always been DW_ACCESS_public. */
18302 if (TREE_PROTECTED (decl))
18303 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18304 else if (TREE_PRIVATE (decl))
18306 if (dwarf_version == 2
18307 || die->die_parent == NULL
18308 || die->die_parent->die_tag != DW_TAG_class_type)
18309 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18311 else if (dwarf_version > 2
18312 && die->die_parent
18313 && die->die_parent->die_tag == DW_TAG_class_type)
18314 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18317 /* Attach the specialized form of location attribute used for data members of
18318 struct and union types. In the special case of a FIELD_DECL node which
18319 represents a bit-field, the "offset" part of this special location
18320 descriptor must indicate the distance in bytes from the lowest-addressed
18321 byte of the containing struct or union type to the lowest-addressed byte of
18322 the "containing object" for the bit-field. (See the `field_byte_offset'
18323 function above).
18325 For any given bit-field, the "containing object" is a hypothetical object
18326 (of some integral or enum type) within which the given bit-field lives. The
18327 type of this hypothetical "containing object" is always the same as the
18328 declared type of the individual bit-field itself (for GCC anyway... the
18329 DWARF spec doesn't actually mandate this). Note that it is the size (in
18330 bytes) of the hypothetical "containing object" which will be given in the
18331 DW_AT_byte_size attribute for this bit-field. (See the
18332 `byte_size_attribute' function below.) It is also used when calculating the
18333 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
18334 function below.)
18336 CTX is required: see the comment for VLR_CONTEXT. */
18338 static void
18339 add_data_member_location_attribute (dw_die_ref die,
18340 tree decl,
18341 struct vlr_context *ctx)
18343 HOST_WIDE_INT offset;
18344 dw_loc_descr_ref loc_descr = 0;
18346 if (TREE_CODE (decl) == TREE_BINFO)
18348 /* We're working on the TAG_inheritance for a base class. */
18349 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
18351 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
18352 aren't at a fixed offset from all (sub)objects of the same
18353 type. We need to extract the appropriate offset from our
18354 vtable. The following dwarf expression means
18356 BaseAddr = ObAddr + *((*ObAddr) - Offset)
18358 This is specific to the V3 ABI, of course. */
18360 dw_loc_descr_ref tmp;
18362 /* Make a copy of the object address. */
18363 tmp = new_loc_descr (DW_OP_dup, 0, 0);
18364 add_loc_descr (&loc_descr, tmp);
18366 /* Extract the vtable address. */
18367 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18368 add_loc_descr (&loc_descr, tmp);
18370 /* Calculate the address of the offset. */
18371 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
18372 gcc_assert (offset < 0);
18374 tmp = int_loc_descriptor (-offset);
18375 add_loc_descr (&loc_descr, tmp);
18376 tmp = new_loc_descr (DW_OP_minus, 0, 0);
18377 add_loc_descr (&loc_descr, tmp);
18379 /* Extract the offset. */
18380 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18381 add_loc_descr (&loc_descr, tmp);
18383 /* Add it to the object address. */
18384 tmp = new_loc_descr (DW_OP_plus, 0, 0);
18385 add_loc_descr (&loc_descr, tmp);
18387 else
18388 offset = tree_to_shwi (BINFO_OFFSET (decl));
18390 else
18392 loc_descr = field_byte_offset (decl, ctx, &offset);
18394 /* If loc_descr is available then we know the field offset is dynamic.
18395 However, GDB does not handle dynamic field offsets very well at the
18396 moment. */
18397 if (loc_descr != NULL && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
18399 loc_descr = NULL;
18400 offset = 0;
18403 /* Data member location evalutation starts with the base address on the
18404 stack. Compute the field offset and add it to this base address. */
18405 else if (loc_descr != NULL)
18406 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
18409 if (! loc_descr)
18411 /* While DW_AT_data_bit_offset has been added already in DWARF4,
18412 e.g. GDB only added support to it in November 2016. For DWARF5
18413 we need newer debug info consumers anyway. We might change this
18414 to dwarf_version >= 4 once most consumers catched up. */
18415 if (dwarf_version >= 5
18416 && TREE_CODE (decl) == FIELD_DECL
18417 && DECL_BIT_FIELD_TYPE (decl))
18419 tree off = bit_position (decl);
18420 if (tree_fits_uhwi_p (off) && get_AT (die, DW_AT_bit_size))
18422 remove_AT (die, DW_AT_byte_size);
18423 remove_AT (die, DW_AT_bit_offset);
18424 add_AT_unsigned (die, DW_AT_data_bit_offset, tree_to_uhwi (off));
18425 return;
18428 if (dwarf_version > 2)
18430 /* Don't need to output a location expression, just the constant. */
18431 if (offset < 0)
18432 add_AT_int (die, DW_AT_data_member_location, offset);
18433 else
18434 add_AT_unsigned (die, DW_AT_data_member_location, offset);
18435 return;
18437 else
18439 enum dwarf_location_atom op;
18441 /* The DWARF2 standard says that we should assume that the structure
18442 address is already on the stack, so we can specify a structure
18443 field address by using DW_OP_plus_uconst. */
18444 op = DW_OP_plus_uconst;
18445 loc_descr = new_loc_descr (op, offset, 0);
18449 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
18452 /* Writes integer values to dw_vec_const array. */
18454 static void
18455 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
18457 while (size != 0)
18459 *dest++ = val & 0xff;
18460 val >>= 8;
18461 --size;
18465 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
18467 static HOST_WIDE_INT
18468 extract_int (const unsigned char *src, unsigned int size)
18470 HOST_WIDE_INT val = 0;
18472 src += size;
18473 while (size != 0)
18475 val <<= 8;
18476 val |= *--src & 0xff;
18477 --size;
18479 return val;
18482 /* Writes wide_int values to dw_vec_const array. */
18484 static void
18485 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
18487 int i;
18489 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
18491 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
18492 return;
18495 /* We'd have to extend this code to support odd sizes. */
18496 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
18498 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
18500 if (WORDS_BIG_ENDIAN)
18501 for (i = n - 1; i >= 0; i--)
18503 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18504 dest += sizeof (HOST_WIDE_INT);
18506 else
18507 for (i = 0; i < n; i++)
18509 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18510 dest += sizeof (HOST_WIDE_INT);
18514 /* Writes floating point values to dw_vec_const array. */
18516 static void
18517 insert_float (const_rtx rtl, unsigned char *array)
18519 long val[4];
18520 int i;
18522 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), GET_MODE (rtl));
18524 /* real_to_target puts 32-bit pieces in each long. Pack them. */
18525 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
18527 insert_int (val[i], 4, array);
18528 array += 4;
18532 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
18533 does not have a "location" either in memory or in a register. These
18534 things can arise in GNU C when a constant is passed as an actual parameter
18535 to an inlined function. They can also arise in C++ where declared
18536 constants do not necessarily get memory "homes". */
18538 static bool
18539 add_const_value_attribute (dw_die_ref die, rtx rtl)
18541 switch (GET_CODE (rtl))
18543 case CONST_INT:
18545 HOST_WIDE_INT val = INTVAL (rtl);
18547 if (val < 0)
18548 add_AT_int (die, DW_AT_const_value, val);
18549 else
18550 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
18552 return true;
18554 case CONST_WIDE_INT:
18556 wide_int w1 = rtx_mode_t (rtl, MAX_MODE_INT);
18557 unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
18558 (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
18559 wide_int w = wi::zext (w1, prec);
18560 add_AT_wide (die, DW_AT_const_value, w);
18562 return true;
18564 case CONST_DOUBLE:
18565 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
18566 floating-point constant. A CONST_DOUBLE is used whenever the
18567 constant requires more than one word in order to be adequately
18568 represented. */
18570 machine_mode mode = GET_MODE (rtl);
18572 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
18573 add_AT_double (die, DW_AT_const_value,
18574 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
18575 else
18577 unsigned int length = GET_MODE_SIZE (mode);
18578 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
18580 insert_float (rtl, array);
18581 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
18584 return true;
18586 case CONST_VECTOR:
18588 machine_mode mode = GET_MODE (rtl);
18589 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
18590 unsigned int length = CONST_VECTOR_NUNITS (rtl);
18591 unsigned char *array
18592 = ggc_vec_alloc<unsigned char> (length * elt_size);
18593 unsigned int i;
18594 unsigned char *p;
18595 machine_mode imode = GET_MODE_INNER (mode);
18597 switch (GET_MODE_CLASS (mode))
18599 case MODE_VECTOR_INT:
18600 for (i = 0, p = array; i < length; i++, p += elt_size)
18602 rtx elt = CONST_VECTOR_ELT (rtl, i);
18603 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
18605 break;
18607 case MODE_VECTOR_FLOAT:
18608 for (i = 0, p = array; i < length; i++, p += elt_size)
18610 rtx elt = CONST_VECTOR_ELT (rtl, i);
18611 insert_float (elt, p);
18613 break;
18615 default:
18616 gcc_unreachable ();
18619 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
18621 return true;
18623 case CONST_STRING:
18624 if (dwarf_version >= 4 || !dwarf_strict)
18626 dw_loc_descr_ref loc_result;
18627 resolve_one_addr (&rtl);
18628 rtl_addr:
18629 loc_result = new_addr_loc_descr (rtl, dtprel_false);
18630 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
18631 add_AT_loc (die, DW_AT_location, loc_result);
18632 vec_safe_push (used_rtx_array, rtl);
18633 return true;
18635 return false;
18637 case CONST:
18638 if (CONSTANT_P (XEXP (rtl, 0)))
18639 return add_const_value_attribute (die, XEXP (rtl, 0));
18640 /* FALLTHROUGH */
18641 case SYMBOL_REF:
18642 if (!const_ok_for_output (rtl))
18643 return false;
18644 /* FALLTHROUGH */
18645 case LABEL_REF:
18646 if (dwarf_version >= 4 || !dwarf_strict)
18647 goto rtl_addr;
18648 return false;
18650 case PLUS:
18651 /* In cases where an inlined instance of an inline function is passed
18652 the address of an `auto' variable (which is local to the caller) we
18653 can get a situation where the DECL_RTL of the artificial local
18654 variable (for the inlining) which acts as a stand-in for the
18655 corresponding formal parameter (of the inline function) will look
18656 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
18657 exactly a compile-time constant expression, but it isn't the address
18658 of the (artificial) local variable either. Rather, it represents the
18659 *value* which the artificial local variable always has during its
18660 lifetime. We currently have no way to represent such quasi-constant
18661 values in Dwarf, so for now we just punt and generate nothing. */
18662 return false;
18664 case HIGH:
18665 case CONST_FIXED:
18666 return false;
18668 case MEM:
18669 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
18670 && MEM_READONLY_P (rtl)
18671 && GET_MODE (rtl) == BLKmode)
18673 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
18674 return true;
18676 return false;
18678 default:
18679 /* No other kinds of rtx should be possible here. */
18680 gcc_unreachable ();
18682 return false;
18685 /* Determine whether the evaluation of EXPR references any variables
18686 or functions which aren't otherwise used (and therefore may not be
18687 output). */
18688 static tree
18689 reference_to_unused (tree * tp, int * walk_subtrees,
18690 void * data ATTRIBUTE_UNUSED)
18692 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
18693 *walk_subtrees = 0;
18695 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
18696 && ! TREE_ASM_WRITTEN (*tp))
18697 return *tp;
18698 /* ??? The C++ FE emits debug information for using decls, so
18699 putting gcc_unreachable here falls over. See PR31899. For now
18700 be conservative. */
18701 else if (!symtab->global_info_ready && VAR_OR_FUNCTION_DECL_P (*tp))
18702 return *tp;
18703 else if (VAR_P (*tp))
18705 varpool_node *node = varpool_node::get (*tp);
18706 if (!node || !node->definition)
18707 return *tp;
18709 else if (TREE_CODE (*tp) == FUNCTION_DECL
18710 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
18712 /* The call graph machinery must have finished analyzing,
18713 optimizing and gimplifying the CU by now.
18714 So if *TP has no call graph node associated
18715 to it, it means *TP will not be emitted. */
18716 if (!cgraph_node::get (*tp))
18717 return *tp;
18719 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
18720 return *tp;
18722 return NULL_TREE;
18725 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
18726 for use in a later add_const_value_attribute call. */
18728 static rtx
18729 rtl_for_decl_init (tree init, tree type)
18731 rtx rtl = NULL_RTX;
18733 STRIP_NOPS (init);
18735 /* If a variable is initialized with a string constant without embedded
18736 zeros, build CONST_STRING. */
18737 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
18739 tree enttype = TREE_TYPE (type);
18740 tree domain = TYPE_DOMAIN (type);
18741 machine_mode mode = TYPE_MODE (enttype);
18743 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
18744 && domain
18745 && integer_zerop (TYPE_MIN_VALUE (domain))
18746 && compare_tree_int (TYPE_MAX_VALUE (domain),
18747 TREE_STRING_LENGTH (init) - 1) == 0
18748 && ((size_t) TREE_STRING_LENGTH (init)
18749 == strlen (TREE_STRING_POINTER (init)) + 1))
18751 rtl = gen_rtx_CONST_STRING (VOIDmode,
18752 ggc_strdup (TREE_STRING_POINTER (init)));
18753 rtl = gen_rtx_MEM (BLKmode, rtl);
18754 MEM_READONLY_P (rtl) = 1;
18757 /* Other aggregates, and complex values, could be represented using
18758 CONCAT: FIXME! */
18759 else if (AGGREGATE_TYPE_P (type)
18760 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
18761 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
18762 || TREE_CODE (type) == COMPLEX_TYPE)
18764 /* Vectors only work if their mode is supported by the target.
18765 FIXME: generic vectors ought to work too. */
18766 else if (TREE_CODE (type) == VECTOR_TYPE
18767 && !VECTOR_MODE_P (TYPE_MODE (type)))
18769 /* If the initializer is something that we know will expand into an
18770 immediate RTL constant, expand it now. We must be careful not to
18771 reference variables which won't be output. */
18772 else if (initializer_constant_valid_p (init, type)
18773 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
18775 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
18776 possible. */
18777 if (TREE_CODE (type) == VECTOR_TYPE)
18778 switch (TREE_CODE (init))
18780 case VECTOR_CST:
18781 break;
18782 case CONSTRUCTOR:
18783 if (TREE_CONSTANT (init))
18785 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
18786 bool constant_p = true;
18787 tree value;
18788 unsigned HOST_WIDE_INT ix;
18790 /* Even when ctor is constant, it might contain non-*_CST
18791 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
18792 belong into VECTOR_CST nodes. */
18793 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
18794 if (!CONSTANT_CLASS_P (value))
18796 constant_p = false;
18797 break;
18800 if (constant_p)
18802 init = build_vector_from_ctor (type, elts);
18803 break;
18806 /* FALLTHRU */
18808 default:
18809 return NULL;
18812 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
18814 /* If expand_expr returns a MEM, it wasn't immediate. */
18815 gcc_assert (!rtl || !MEM_P (rtl));
18818 return rtl;
18821 /* Generate RTL for the variable DECL to represent its location. */
18823 static rtx
18824 rtl_for_decl_location (tree decl)
18826 rtx rtl;
18828 /* Here we have to decide where we are going to say the parameter "lives"
18829 (as far as the debugger is concerned). We only have a couple of
18830 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
18832 DECL_RTL normally indicates where the parameter lives during most of the
18833 activation of the function. If optimization is enabled however, this
18834 could be either NULL or else a pseudo-reg. Both of those cases indicate
18835 that the parameter doesn't really live anywhere (as far as the code
18836 generation parts of GCC are concerned) during most of the function's
18837 activation. That will happen (for example) if the parameter is never
18838 referenced within the function.
18840 We could just generate a location descriptor here for all non-NULL
18841 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
18842 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
18843 where DECL_RTL is NULL or is a pseudo-reg.
18845 Note however that we can only get away with using DECL_INCOMING_RTL as
18846 a backup substitute for DECL_RTL in certain limited cases. In cases
18847 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
18848 we can be sure that the parameter was passed using the same type as it is
18849 declared to have within the function, and that its DECL_INCOMING_RTL
18850 points us to a place where a value of that type is passed.
18852 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
18853 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
18854 because in these cases DECL_INCOMING_RTL points us to a value of some
18855 type which is *different* from the type of the parameter itself. Thus,
18856 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
18857 such cases, the debugger would end up (for example) trying to fetch a
18858 `float' from a place which actually contains the first part of a
18859 `double'. That would lead to really incorrect and confusing
18860 output at debug-time.
18862 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
18863 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
18864 are a couple of exceptions however. On little-endian machines we can
18865 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
18866 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
18867 an integral type that is smaller than TREE_TYPE (decl). These cases arise
18868 when (on a little-endian machine) a non-prototyped function has a
18869 parameter declared to be of type `short' or `char'. In such cases,
18870 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
18871 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
18872 passed `int' value. If the debugger then uses that address to fetch
18873 a `short' or a `char' (on a little-endian machine) the result will be
18874 the correct data, so we allow for such exceptional cases below.
18876 Note that our goal here is to describe the place where the given formal
18877 parameter lives during most of the function's activation (i.e. between the
18878 end of the prologue and the start of the epilogue). We'll do that as best
18879 as we can. Note however that if the given formal parameter is modified
18880 sometime during the execution of the function, then a stack backtrace (at
18881 debug-time) will show the function as having been called with the *new*
18882 value rather than the value which was originally passed in. This happens
18883 rarely enough that it is not a major problem, but it *is* a problem, and
18884 I'd like to fix it.
18886 A future version of dwarf2out.c may generate two additional attributes for
18887 any given DW_TAG_formal_parameter DIE which will describe the "passed
18888 type" and the "passed location" for the given formal parameter in addition
18889 to the attributes we now generate to indicate the "declared type" and the
18890 "active location" for each parameter. This additional set of attributes
18891 could be used by debuggers for stack backtraces. Separately, note that
18892 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
18893 This happens (for example) for inlined-instances of inline function formal
18894 parameters which are never referenced. This really shouldn't be
18895 happening. All PARM_DECL nodes should get valid non-NULL
18896 DECL_INCOMING_RTL values. FIXME. */
18898 /* Use DECL_RTL as the "location" unless we find something better. */
18899 rtl = DECL_RTL_IF_SET (decl);
18901 /* When generating abstract instances, ignore everything except
18902 constants, symbols living in memory, and symbols living in
18903 fixed registers. */
18904 if (! reload_completed)
18906 if (rtl
18907 && (CONSTANT_P (rtl)
18908 || (MEM_P (rtl)
18909 && CONSTANT_P (XEXP (rtl, 0)))
18910 || (REG_P (rtl)
18911 && VAR_P (decl)
18912 && TREE_STATIC (decl))))
18914 rtl = targetm.delegitimize_address (rtl);
18915 return rtl;
18917 rtl = NULL_RTX;
18919 else if (TREE_CODE (decl) == PARM_DECL)
18921 if (rtl == NULL_RTX
18922 || is_pseudo_reg (rtl)
18923 || (MEM_P (rtl)
18924 && is_pseudo_reg (XEXP (rtl, 0))
18925 && DECL_INCOMING_RTL (decl)
18926 && MEM_P (DECL_INCOMING_RTL (decl))
18927 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
18929 tree declared_type = TREE_TYPE (decl);
18930 tree passed_type = DECL_ARG_TYPE (decl);
18931 machine_mode dmode = TYPE_MODE (declared_type);
18932 machine_mode pmode = TYPE_MODE (passed_type);
18934 /* This decl represents a formal parameter which was optimized out.
18935 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
18936 all cases where (rtl == NULL_RTX) just below. */
18937 if (dmode == pmode)
18938 rtl = DECL_INCOMING_RTL (decl);
18939 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
18940 && SCALAR_INT_MODE_P (dmode)
18941 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
18942 && DECL_INCOMING_RTL (decl))
18944 rtx inc = DECL_INCOMING_RTL (decl);
18945 if (REG_P (inc))
18946 rtl = inc;
18947 else if (MEM_P (inc))
18949 if (BYTES_BIG_ENDIAN)
18950 rtl = adjust_address_nv (inc, dmode,
18951 GET_MODE_SIZE (pmode)
18952 - GET_MODE_SIZE (dmode));
18953 else
18954 rtl = inc;
18959 /* If the parm was passed in registers, but lives on the stack, then
18960 make a big endian correction if the mode of the type of the
18961 parameter is not the same as the mode of the rtl. */
18962 /* ??? This is the same series of checks that are made in dbxout.c before
18963 we reach the big endian correction code there. It isn't clear if all
18964 of these checks are necessary here, but keeping them all is the safe
18965 thing to do. */
18966 else if (MEM_P (rtl)
18967 && XEXP (rtl, 0) != const0_rtx
18968 && ! CONSTANT_P (XEXP (rtl, 0))
18969 /* Not passed in memory. */
18970 && !MEM_P (DECL_INCOMING_RTL (decl))
18971 /* Not passed by invisible reference. */
18972 && (!REG_P (XEXP (rtl, 0))
18973 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
18974 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
18975 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
18976 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
18977 #endif
18979 /* Big endian correction check. */
18980 && BYTES_BIG_ENDIAN
18981 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
18982 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
18983 < UNITS_PER_WORD))
18985 machine_mode addr_mode = get_address_mode (rtl);
18986 int offset = (UNITS_PER_WORD
18987 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
18989 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
18990 plus_constant (addr_mode, XEXP (rtl, 0), offset));
18993 else if (VAR_P (decl)
18994 && rtl
18995 && MEM_P (rtl)
18996 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
18997 && BYTES_BIG_ENDIAN)
18999 machine_mode addr_mode = get_address_mode (rtl);
19000 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
19001 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
19003 /* If a variable is declared "register" yet is smaller than
19004 a register, then if we store the variable to memory, it
19005 looks like we're storing a register-sized value, when in
19006 fact we are not. We need to adjust the offset of the
19007 storage location to reflect the actual value's bytes,
19008 else gdb will not be able to display it. */
19009 if (rsize > dsize)
19010 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
19011 plus_constant (addr_mode, XEXP (rtl, 0),
19012 rsize - dsize));
19015 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
19016 and will have been substituted directly into all expressions that use it.
19017 C does not have such a concept, but C++ and other languages do. */
19018 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
19019 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
19021 if (rtl)
19022 rtl = targetm.delegitimize_address (rtl);
19024 /* If we don't look past the constant pool, we risk emitting a
19025 reference to a constant pool entry that isn't referenced from
19026 code, and thus is not emitted. */
19027 if (rtl)
19028 rtl = avoid_constant_pool_reference (rtl);
19030 /* Try harder to get a rtl. If this symbol ends up not being emitted
19031 in the current CU, resolve_addr will remove the expression referencing
19032 it. */
19033 if (rtl == NULL_RTX
19034 && VAR_P (decl)
19035 && !DECL_EXTERNAL (decl)
19036 && TREE_STATIC (decl)
19037 && DECL_NAME (decl)
19038 && !DECL_HARD_REGISTER (decl)
19039 && DECL_MODE (decl) != VOIDmode)
19041 rtl = make_decl_rtl_for_debug (decl);
19042 if (!MEM_P (rtl)
19043 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
19044 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
19045 rtl = NULL_RTX;
19048 return rtl;
19051 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
19052 returned. If so, the decl for the COMMON block is returned, and the
19053 value is the offset into the common block for the symbol. */
19055 static tree
19056 fortran_common (tree decl, HOST_WIDE_INT *value)
19058 tree val_expr, cvar;
19059 machine_mode mode;
19060 HOST_WIDE_INT bitsize, bitpos;
19061 tree offset;
19062 int unsignedp, reversep, volatilep = 0;
19064 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
19065 it does not have a value (the offset into the common area), or if it
19066 is thread local (as opposed to global) then it isn't common, and shouldn't
19067 be handled as such. */
19068 if (!VAR_P (decl)
19069 || !TREE_STATIC (decl)
19070 || !DECL_HAS_VALUE_EXPR_P (decl)
19071 || !is_fortran ())
19072 return NULL_TREE;
19074 val_expr = DECL_VALUE_EXPR (decl);
19075 if (TREE_CODE (val_expr) != COMPONENT_REF)
19076 return NULL_TREE;
19078 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
19079 &unsignedp, &reversep, &volatilep);
19081 if (cvar == NULL_TREE
19082 || !VAR_P (cvar)
19083 || DECL_ARTIFICIAL (cvar)
19084 || !TREE_PUBLIC (cvar))
19085 return NULL_TREE;
19087 *value = 0;
19088 if (offset != NULL)
19090 if (!tree_fits_shwi_p (offset))
19091 return NULL_TREE;
19092 *value = tree_to_shwi (offset);
19094 if (bitpos != 0)
19095 *value += bitpos / BITS_PER_UNIT;
19097 return cvar;
19100 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
19101 data attribute for a variable or a parameter. We generate the
19102 DW_AT_const_value attribute only in those cases where the given variable
19103 or parameter does not have a true "location" either in memory or in a
19104 register. This can happen (for example) when a constant is passed as an
19105 actual argument in a call to an inline function. (It's possible that
19106 these things can crop up in other ways also.) Note that one type of
19107 constant value which can be passed into an inlined function is a constant
19108 pointer. This can happen for example if an actual argument in an inlined
19109 function call evaluates to a compile-time constant address.
19111 CACHE_P is true if it is worth caching the location list for DECL,
19112 so that future calls can reuse it rather than regenerate it from scratch.
19113 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
19114 since we will need to refer to them each time the function is inlined. */
19116 static bool
19117 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
19119 rtx rtl;
19120 dw_loc_list_ref list;
19121 var_loc_list *loc_list;
19122 cached_dw_loc_list *cache;
19124 if (early_dwarf)
19125 return false;
19127 if (TREE_CODE (decl) == ERROR_MARK)
19128 return false;
19130 if (get_AT (die, DW_AT_location)
19131 || get_AT (die, DW_AT_const_value))
19132 return true;
19134 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
19135 || TREE_CODE (decl) == RESULT_DECL);
19137 /* Try to get some constant RTL for this decl, and use that as the value of
19138 the location. */
19140 rtl = rtl_for_decl_location (decl);
19141 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19142 && add_const_value_attribute (die, rtl))
19143 return true;
19145 /* See if we have single element location list that is equivalent to
19146 a constant value. That way we are better to use add_const_value_attribute
19147 rather than expanding constant value equivalent. */
19148 loc_list = lookup_decl_loc (decl);
19149 if (loc_list
19150 && loc_list->first
19151 && loc_list->first->next == NULL
19152 && NOTE_P (loc_list->first->loc)
19153 && NOTE_VAR_LOCATION (loc_list->first->loc)
19154 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
19156 struct var_loc_node *node;
19158 node = loc_list->first;
19159 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
19160 if (GET_CODE (rtl) == EXPR_LIST)
19161 rtl = XEXP (rtl, 0);
19162 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19163 && add_const_value_attribute (die, rtl))
19164 return true;
19166 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
19167 list several times. See if we've already cached the contents. */
19168 list = NULL;
19169 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
19170 cache_p = false;
19171 if (cache_p)
19173 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
19174 if (cache)
19175 list = cache->loc_list;
19177 if (list == NULL)
19179 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
19180 NULL);
19181 /* It is usually worth caching this result if the decl is from
19182 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
19183 if (cache_p && list && list->dw_loc_next)
19185 cached_dw_loc_list **slot
19186 = cached_dw_loc_list_table->find_slot_with_hash (decl,
19187 DECL_UID (decl),
19188 INSERT);
19189 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
19190 cache->decl_id = DECL_UID (decl);
19191 cache->loc_list = list;
19192 *slot = cache;
19195 if (list)
19197 add_AT_location_description (die, DW_AT_location, list);
19198 return true;
19200 /* None of that worked, so it must not really have a location;
19201 try adding a constant value attribute from the DECL_INITIAL. */
19202 return tree_add_const_value_attribute_for_decl (die, decl);
19205 /* Helper function for tree_add_const_value_attribute. Natively encode
19206 initializer INIT into an array. Return true if successful. */
19208 static bool
19209 native_encode_initializer (tree init, unsigned char *array, int size)
19211 tree type;
19213 if (init == NULL_TREE)
19214 return false;
19216 STRIP_NOPS (init);
19217 switch (TREE_CODE (init))
19219 case STRING_CST:
19220 type = TREE_TYPE (init);
19221 if (TREE_CODE (type) == ARRAY_TYPE)
19223 tree enttype = TREE_TYPE (type);
19224 machine_mode mode = TYPE_MODE (enttype);
19226 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
19227 return false;
19228 if (int_size_in_bytes (type) != size)
19229 return false;
19230 if (size > TREE_STRING_LENGTH (init))
19232 memcpy (array, TREE_STRING_POINTER (init),
19233 TREE_STRING_LENGTH (init));
19234 memset (array + TREE_STRING_LENGTH (init),
19235 '\0', size - TREE_STRING_LENGTH (init));
19237 else
19238 memcpy (array, TREE_STRING_POINTER (init), size);
19239 return true;
19241 return false;
19242 case CONSTRUCTOR:
19243 type = TREE_TYPE (init);
19244 if (int_size_in_bytes (type) != size)
19245 return false;
19246 if (TREE_CODE (type) == ARRAY_TYPE)
19248 HOST_WIDE_INT min_index;
19249 unsigned HOST_WIDE_INT cnt;
19250 int curpos = 0, fieldsize;
19251 constructor_elt *ce;
19253 if (TYPE_DOMAIN (type) == NULL_TREE
19254 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
19255 return false;
19257 fieldsize = int_size_in_bytes (TREE_TYPE (type));
19258 if (fieldsize <= 0)
19259 return false;
19261 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
19262 memset (array, '\0', size);
19263 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19265 tree val = ce->value;
19266 tree index = ce->index;
19267 int pos = curpos;
19268 if (index && TREE_CODE (index) == RANGE_EXPR)
19269 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
19270 * fieldsize;
19271 else if (index)
19272 pos = (tree_to_shwi (index) - min_index) * fieldsize;
19274 if (val)
19276 STRIP_NOPS (val);
19277 if (!native_encode_initializer (val, array + pos, fieldsize))
19278 return false;
19280 curpos = pos + fieldsize;
19281 if (index && TREE_CODE (index) == RANGE_EXPR)
19283 int count = tree_to_shwi (TREE_OPERAND (index, 1))
19284 - tree_to_shwi (TREE_OPERAND (index, 0));
19285 while (count-- > 0)
19287 if (val)
19288 memcpy (array + curpos, array + pos, fieldsize);
19289 curpos += fieldsize;
19292 gcc_assert (curpos <= size);
19294 return true;
19296 else if (TREE_CODE (type) == RECORD_TYPE
19297 || TREE_CODE (type) == UNION_TYPE)
19299 tree field = NULL_TREE;
19300 unsigned HOST_WIDE_INT cnt;
19301 constructor_elt *ce;
19303 if (int_size_in_bytes (type) != size)
19304 return false;
19306 if (TREE_CODE (type) == RECORD_TYPE)
19307 field = TYPE_FIELDS (type);
19309 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19311 tree val = ce->value;
19312 int pos, fieldsize;
19314 if (ce->index != 0)
19315 field = ce->index;
19317 if (val)
19318 STRIP_NOPS (val);
19320 if (field == NULL_TREE || DECL_BIT_FIELD (field))
19321 return false;
19323 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
19324 && TYPE_DOMAIN (TREE_TYPE (field))
19325 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
19326 return false;
19327 else if (DECL_SIZE_UNIT (field) == NULL_TREE
19328 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
19329 return false;
19330 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
19331 pos = int_byte_position (field);
19332 gcc_assert (pos + fieldsize <= size);
19333 if (val && fieldsize != 0
19334 && !native_encode_initializer (val, array + pos, fieldsize))
19335 return false;
19337 return true;
19339 return false;
19340 case VIEW_CONVERT_EXPR:
19341 case NON_LVALUE_EXPR:
19342 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
19343 default:
19344 return native_encode_expr (init, array, size) == size;
19348 /* Attach a DW_AT_const_value attribute to DIE. The value of the
19349 attribute is the const value T. */
19351 static bool
19352 tree_add_const_value_attribute (dw_die_ref die, tree t)
19354 tree init;
19355 tree type = TREE_TYPE (t);
19356 rtx rtl;
19358 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
19359 return false;
19361 init = t;
19362 gcc_assert (!DECL_P (init));
19364 if (! early_dwarf)
19366 rtl = rtl_for_decl_init (init, type);
19367 if (rtl)
19368 return add_const_value_attribute (die, rtl);
19370 /* If the host and target are sane, try harder. */
19371 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
19372 && initializer_constant_valid_p (init, type))
19374 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
19375 if (size > 0 && (int) size == size)
19377 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
19379 if (native_encode_initializer (init, array, size))
19381 add_AT_vec (die, DW_AT_const_value, size, 1, array);
19382 return true;
19384 ggc_free (array);
19387 return false;
19390 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
19391 attribute is the const value of T, where T is an integral constant
19392 variable with static storage duration
19393 (so it can't be a PARM_DECL or a RESULT_DECL). */
19395 static bool
19396 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
19399 if (!decl
19400 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
19401 || (VAR_P (decl) && !TREE_STATIC (decl)))
19402 return false;
19404 if (TREE_READONLY (decl)
19405 && ! TREE_THIS_VOLATILE (decl)
19406 && DECL_INITIAL (decl))
19407 /* OK */;
19408 else
19409 return false;
19411 /* Don't add DW_AT_const_value if abstract origin already has one. */
19412 if (get_AT (var_die, DW_AT_const_value))
19413 return false;
19415 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
19418 /* Convert the CFI instructions for the current function into a
19419 location list. This is used for DW_AT_frame_base when we targeting
19420 a dwarf2 consumer that does not support the dwarf3
19421 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
19422 expressions. */
19424 static dw_loc_list_ref
19425 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
19427 int ix;
19428 dw_fde_ref fde;
19429 dw_loc_list_ref list, *list_tail;
19430 dw_cfi_ref cfi;
19431 dw_cfa_location last_cfa, next_cfa;
19432 const char *start_label, *last_label, *section;
19433 dw_cfa_location remember;
19435 fde = cfun->fde;
19436 gcc_assert (fde != NULL);
19438 section = secname_for_decl (current_function_decl);
19439 list_tail = &list;
19440 list = NULL;
19442 memset (&next_cfa, 0, sizeof (next_cfa));
19443 next_cfa.reg = INVALID_REGNUM;
19444 remember = next_cfa;
19446 start_label = fde->dw_fde_begin;
19448 /* ??? Bald assumption that the CIE opcode list does not contain
19449 advance opcodes. */
19450 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
19451 lookup_cfa_1 (cfi, &next_cfa, &remember);
19453 last_cfa = next_cfa;
19454 last_label = start_label;
19456 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
19458 /* If the first partition contained no CFI adjustments, the
19459 CIE opcodes apply to the whole first partition. */
19460 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19461 fde->dw_fde_begin, fde->dw_fde_end, section);
19462 list_tail =&(*list_tail)->dw_loc_next;
19463 start_label = last_label = fde->dw_fde_second_begin;
19466 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
19468 switch (cfi->dw_cfi_opc)
19470 case DW_CFA_set_loc:
19471 case DW_CFA_advance_loc1:
19472 case DW_CFA_advance_loc2:
19473 case DW_CFA_advance_loc4:
19474 if (!cfa_equal_p (&last_cfa, &next_cfa))
19476 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19477 start_label, last_label, section);
19479 list_tail = &(*list_tail)->dw_loc_next;
19480 last_cfa = next_cfa;
19481 start_label = last_label;
19483 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
19484 break;
19486 case DW_CFA_advance_loc:
19487 /* The encoding is complex enough that we should never emit this. */
19488 gcc_unreachable ();
19490 default:
19491 lookup_cfa_1 (cfi, &next_cfa, &remember);
19492 break;
19494 if (ix + 1 == fde->dw_fde_switch_cfi_index)
19496 if (!cfa_equal_p (&last_cfa, &next_cfa))
19498 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19499 start_label, last_label, section);
19501 list_tail = &(*list_tail)->dw_loc_next;
19502 last_cfa = next_cfa;
19503 start_label = last_label;
19505 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19506 start_label, fde->dw_fde_end, section);
19507 list_tail = &(*list_tail)->dw_loc_next;
19508 start_label = last_label = fde->dw_fde_second_begin;
19512 if (!cfa_equal_p (&last_cfa, &next_cfa))
19514 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19515 start_label, last_label, section);
19516 list_tail = &(*list_tail)->dw_loc_next;
19517 start_label = last_label;
19520 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
19521 start_label,
19522 fde->dw_fde_second_begin
19523 ? fde->dw_fde_second_end : fde->dw_fde_end,
19524 section);
19526 if (list && list->dw_loc_next)
19527 gen_llsym (list);
19529 return list;
19532 /* Compute a displacement from the "steady-state frame pointer" to the
19533 frame base (often the same as the CFA), and store it in
19534 frame_pointer_fb_offset. OFFSET is added to the displacement
19535 before the latter is negated. */
19537 static void
19538 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
19540 rtx reg, elim;
19542 #ifdef FRAME_POINTER_CFA_OFFSET
19543 reg = frame_pointer_rtx;
19544 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
19545 #else
19546 reg = arg_pointer_rtx;
19547 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
19548 #endif
19550 elim = (ira_use_lra_p
19551 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
19552 : eliminate_regs (reg, VOIDmode, NULL_RTX));
19553 if (GET_CODE (elim) == PLUS)
19555 offset += INTVAL (XEXP (elim, 1));
19556 elim = XEXP (elim, 0);
19559 frame_pointer_fb_offset = -offset;
19561 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
19562 in which to eliminate. This is because it's stack pointer isn't
19563 directly accessible as a register within the ISA. To work around
19564 this, assume that while we cannot provide a proper value for
19565 frame_pointer_fb_offset, we won't need one either. */
19566 frame_pointer_fb_offset_valid
19567 = ((SUPPORTS_STACK_ALIGNMENT
19568 && (elim == hard_frame_pointer_rtx
19569 || elim == stack_pointer_rtx))
19570 || elim == (frame_pointer_needed
19571 ? hard_frame_pointer_rtx
19572 : stack_pointer_rtx));
19575 /* Generate a DW_AT_name attribute given some string value to be included as
19576 the value of the attribute. */
19578 static void
19579 add_name_attribute (dw_die_ref die, const char *name_string)
19581 if (name_string != NULL && *name_string != 0)
19583 if (demangle_name_func)
19584 name_string = (*demangle_name_func) (name_string);
19586 add_AT_string (die, DW_AT_name, name_string);
19590 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
19591 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
19592 of TYPE accordingly.
19594 ??? This is a temporary measure until after we're able to generate
19595 regular DWARF for the complex Ada type system. */
19597 static void
19598 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
19599 dw_die_ref context_die)
19601 tree dtype;
19602 dw_die_ref dtype_die;
19604 if (!lang_hooks.types.descriptive_type)
19605 return;
19607 dtype = lang_hooks.types.descriptive_type (type);
19608 if (!dtype)
19609 return;
19611 dtype_die = lookup_type_die (dtype);
19612 if (!dtype_die)
19614 gen_type_die (dtype, context_die);
19615 dtype_die = lookup_type_die (dtype);
19616 gcc_assert (dtype_die);
19619 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
19622 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
19624 static const char *
19625 comp_dir_string (void)
19627 const char *wd;
19628 char *wd1;
19629 static const char *cached_wd = NULL;
19631 if (cached_wd != NULL)
19632 return cached_wd;
19634 wd = get_src_pwd ();
19635 if (wd == NULL)
19636 return NULL;
19638 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
19640 int wdlen;
19642 wdlen = strlen (wd);
19643 wd1 = ggc_vec_alloc<char> (wdlen + 2);
19644 strcpy (wd1, wd);
19645 wd1 [wdlen] = DIR_SEPARATOR;
19646 wd1 [wdlen + 1] = 0;
19647 wd = wd1;
19650 cached_wd = remap_debug_filename (wd);
19651 return cached_wd;
19654 /* Generate a DW_AT_comp_dir attribute for DIE. */
19656 static void
19657 add_comp_dir_attribute (dw_die_ref die)
19659 const char * wd = comp_dir_string ();
19660 if (wd != NULL)
19661 add_AT_string (die, DW_AT_comp_dir, wd);
19664 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
19665 pointer computation, ...), output a representation for that bound according
19666 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
19667 loc_list_from_tree for the meaning of CONTEXT. */
19669 static void
19670 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
19671 int forms, struct loc_descr_context *context)
19673 dw_die_ref context_die, decl_die;
19674 dw_loc_list_ref list;
19675 bool strip_conversions = true;
19676 bool placeholder_seen = false;
19678 while (strip_conversions)
19679 switch (TREE_CODE (value))
19681 case ERROR_MARK:
19682 case SAVE_EXPR:
19683 return;
19685 CASE_CONVERT:
19686 case VIEW_CONVERT_EXPR:
19687 value = TREE_OPERAND (value, 0);
19688 break;
19690 default:
19691 strip_conversions = false;
19692 break;
19695 /* If possible and permitted, output the attribute as a constant. */
19696 if ((forms & dw_scalar_form_constant) != 0
19697 && TREE_CODE (value) == INTEGER_CST)
19699 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
19701 /* If HOST_WIDE_INT is big enough then represent the bound as
19702 a constant value. We need to choose a form based on
19703 whether the type is signed or unsigned. We cannot just
19704 call add_AT_unsigned if the value itself is positive
19705 (add_AT_unsigned might add the unsigned value encoded as
19706 DW_FORM_data[1248]). Some DWARF consumers will lookup the
19707 bounds type and then sign extend any unsigned values found
19708 for signed types. This is needed only for
19709 DW_AT_{lower,upper}_bound, since for most other attributes,
19710 consumers will treat DW_FORM_data[1248] as unsigned values,
19711 regardless of the underlying type. */
19712 if (prec <= HOST_BITS_PER_WIDE_INT
19713 || tree_fits_uhwi_p (value))
19715 if (TYPE_UNSIGNED (TREE_TYPE (value)))
19716 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
19717 else
19718 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
19720 else
19721 /* Otherwise represent the bound as an unsigned value with
19722 the precision of its type. The precision and signedness
19723 of the type will be necessary to re-interpret it
19724 unambiguously. */
19725 add_AT_wide (die, attr, value);
19726 return;
19729 /* Otherwise, if it's possible and permitted too, output a reference to
19730 another DIE. */
19731 if ((forms & dw_scalar_form_reference) != 0)
19733 tree decl = NULL_TREE;
19735 /* Some type attributes reference an outer type. For instance, the upper
19736 bound of an array may reference an embedding record (this happens in
19737 Ada). */
19738 if (TREE_CODE (value) == COMPONENT_REF
19739 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
19740 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
19741 decl = TREE_OPERAND (value, 1);
19743 else if (VAR_P (value)
19744 || TREE_CODE (value) == PARM_DECL
19745 || TREE_CODE (value) == RESULT_DECL)
19746 decl = value;
19748 if (decl != NULL_TREE)
19750 dw_die_ref decl_die = lookup_decl_die (decl);
19752 /* ??? Can this happen, or should the variable have been bound
19753 first? Probably it can, since I imagine that we try to create
19754 the types of parameters in the order in which they exist in
19755 the list, and won't have created a forward reference to a
19756 later parameter. */
19757 if (decl_die != NULL)
19759 add_AT_die_ref (die, attr, decl_die);
19760 return;
19765 /* Last chance: try to create a stack operation procedure to evaluate the
19766 value. Do nothing if even that is not possible or permitted. */
19767 if ((forms & dw_scalar_form_exprloc) == 0)
19768 return;
19770 list = loc_list_from_tree (value, 2, context);
19771 if (context && context->placeholder_arg)
19773 placeholder_seen = context->placeholder_seen;
19774 context->placeholder_seen = false;
19776 if (list == NULL || single_element_loc_list_p (list))
19778 /* If this attribute is not a reference nor constant, it is
19779 a DWARF expression rather than location description. For that
19780 loc_list_from_tree (value, 0, &context) is needed. */
19781 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
19782 if (list2 && single_element_loc_list_p (list2))
19784 if (placeholder_seen)
19786 struct dwarf_procedure_info dpi;
19787 dpi.fndecl = NULL_TREE;
19788 dpi.args_count = 1;
19789 if (!resolve_args_picking (list2->expr, 1, &dpi))
19790 return;
19792 add_AT_loc (die, attr, list2->expr);
19793 return;
19797 /* If that failed to give a single element location list, fall back to
19798 outputting this as a reference... still if permitted. */
19799 if (list == NULL
19800 || (forms & dw_scalar_form_reference) == 0
19801 || placeholder_seen)
19802 return;
19804 if (current_function_decl == 0)
19805 context_die = comp_unit_die ();
19806 else
19807 context_die = lookup_decl_die (current_function_decl);
19809 decl_die = new_die (DW_TAG_variable, context_die, value);
19810 add_AT_flag (decl_die, DW_AT_artificial, 1);
19811 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
19812 context_die);
19813 add_AT_location_description (decl_die, DW_AT_location, list);
19814 add_AT_die_ref (die, attr, decl_die);
19817 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
19818 default. */
19820 static int
19821 lower_bound_default (void)
19823 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
19825 case DW_LANG_C:
19826 case DW_LANG_C89:
19827 case DW_LANG_C99:
19828 case DW_LANG_C11:
19829 case DW_LANG_C_plus_plus:
19830 case DW_LANG_C_plus_plus_11:
19831 case DW_LANG_C_plus_plus_14:
19832 case DW_LANG_ObjC:
19833 case DW_LANG_ObjC_plus_plus:
19834 return 0;
19835 case DW_LANG_Fortran77:
19836 case DW_LANG_Fortran90:
19837 case DW_LANG_Fortran95:
19838 case DW_LANG_Fortran03:
19839 case DW_LANG_Fortran08:
19840 return 1;
19841 case DW_LANG_UPC:
19842 case DW_LANG_D:
19843 case DW_LANG_Python:
19844 return dwarf_version >= 4 ? 0 : -1;
19845 case DW_LANG_Ada95:
19846 case DW_LANG_Ada83:
19847 case DW_LANG_Cobol74:
19848 case DW_LANG_Cobol85:
19849 case DW_LANG_Modula2:
19850 case DW_LANG_PLI:
19851 return dwarf_version >= 4 ? 1 : -1;
19852 default:
19853 return -1;
19857 /* Given a tree node describing an array bound (either lower or upper) output
19858 a representation for that bound. */
19860 static void
19861 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
19862 tree bound, struct loc_descr_context *context)
19864 int dflt;
19866 while (1)
19867 switch (TREE_CODE (bound))
19869 /* Strip all conversions. */
19870 CASE_CONVERT:
19871 case VIEW_CONVERT_EXPR:
19872 bound = TREE_OPERAND (bound, 0);
19873 break;
19875 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
19876 are even omitted when they are the default. */
19877 case INTEGER_CST:
19878 /* If the value for this bound is the default one, we can even omit the
19879 attribute. */
19880 if (bound_attr == DW_AT_lower_bound
19881 && tree_fits_shwi_p (bound)
19882 && (dflt = lower_bound_default ()) != -1
19883 && tree_to_shwi (bound) == dflt)
19884 return;
19886 /* FALLTHRU */
19888 default:
19889 /* Because of the complex interaction there can be with other GNAT
19890 encodings, GDB isn't ready yet to handle proper DWARF description
19891 for self-referencial subrange bounds: let GNAT encodings do the
19892 magic in such a case. */
19893 if (is_ada ()
19894 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL
19895 && contains_placeholder_p (bound))
19896 return;
19898 add_scalar_info (subrange_die, bound_attr, bound,
19899 dw_scalar_form_constant
19900 | dw_scalar_form_exprloc
19901 | dw_scalar_form_reference,
19902 context);
19903 return;
19907 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
19908 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
19909 Note that the block of subscript information for an array type also
19910 includes information about the element type of the given array type.
19912 This function reuses previously set type and bound information if
19913 available. */
19915 static void
19916 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
19918 unsigned dimension_number;
19919 tree lower, upper;
19920 dw_die_ref child = type_die->die_child;
19922 for (dimension_number = 0;
19923 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
19924 type = TREE_TYPE (type), dimension_number++)
19926 tree domain = TYPE_DOMAIN (type);
19928 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
19929 break;
19931 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
19932 and (in GNU C only) variable bounds. Handle all three forms
19933 here. */
19935 /* Find and reuse a previously generated DW_TAG_subrange_type if
19936 available.
19938 For multi-dimensional arrays, as we iterate through the
19939 various dimensions in the enclosing for loop above, we also
19940 iterate through the DIE children and pick at each
19941 DW_TAG_subrange_type previously generated (if available).
19942 Each child DW_TAG_subrange_type DIE describes the range of
19943 the current dimension. At this point we should have as many
19944 DW_TAG_subrange_type's as we have dimensions in the
19945 array. */
19946 dw_die_ref subrange_die = NULL;
19947 if (child)
19948 while (1)
19950 child = child->die_sib;
19951 if (child->die_tag == DW_TAG_subrange_type)
19952 subrange_die = child;
19953 if (child == type_die->die_child)
19955 /* If we wrapped around, stop looking next time. */
19956 child = NULL;
19957 break;
19959 if (child->die_tag == DW_TAG_subrange_type)
19960 break;
19962 if (!subrange_die)
19963 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
19965 if (domain)
19967 /* We have an array type with specified bounds. */
19968 lower = TYPE_MIN_VALUE (domain);
19969 upper = TYPE_MAX_VALUE (domain);
19971 /* Define the index type. */
19972 if (TREE_TYPE (domain)
19973 && !get_AT (subrange_die, DW_AT_type))
19975 /* ??? This is probably an Ada unnamed subrange type. Ignore the
19976 TREE_TYPE field. We can't emit debug info for this
19977 because it is an unnamed integral type. */
19978 if (TREE_CODE (domain) == INTEGER_TYPE
19979 && TYPE_NAME (domain) == NULL_TREE
19980 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
19981 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
19983 else
19984 add_type_attribute (subrange_die, TREE_TYPE (domain),
19985 TYPE_UNQUALIFIED, false, type_die);
19988 /* ??? If upper is NULL, the array has unspecified length,
19989 but it does have a lower bound. This happens with Fortran
19990 dimension arr(N:*)
19991 Since the debugger is definitely going to need to know N
19992 to produce useful results, go ahead and output the lower
19993 bound solo, and hope the debugger can cope. */
19995 if (!get_AT (subrange_die, DW_AT_lower_bound))
19996 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
19997 if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
19998 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
20001 /* Otherwise we have an array type with an unspecified length. The
20002 DWARF-2 spec does not say how to handle this; let's just leave out the
20003 bounds. */
20007 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
20009 static void
20010 add_byte_size_attribute (dw_die_ref die, tree tree_node)
20012 dw_die_ref decl_die;
20013 HOST_WIDE_INT size;
20014 dw_loc_descr_ref size_expr = NULL;
20016 switch (TREE_CODE (tree_node))
20018 case ERROR_MARK:
20019 size = 0;
20020 break;
20021 case ENUMERAL_TYPE:
20022 case RECORD_TYPE:
20023 case UNION_TYPE:
20024 case QUAL_UNION_TYPE:
20025 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
20026 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
20028 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
20029 return;
20031 size_expr = type_byte_size (tree_node, &size);
20032 break;
20033 case FIELD_DECL:
20034 /* For a data member of a struct or union, the DW_AT_byte_size is
20035 generally given as the number of bytes normally allocated for an
20036 object of the *declared* type of the member itself. This is true
20037 even for bit-fields. */
20038 size = int_size_in_bytes (field_type (tree_node));
20039 break;
20040 default:
20041 gcc_unreachable ();
20044 /* Support for dynamically-sized objects was introduced by DWARFv3.
20045 At the moment, GDB does not handle variable byte sizes very well,
20046 though. */
20047 if ((dwarf_version >= 3 || !dwarf_strict)
20048 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL
20049 && size_expr != NULL)
20050 add_AT_loc (die, DW_AT_byte_size, size_expr);
20052 /* Note that `size' might be -1 when we get to this point. If it is, that
20053 indicates that the byte size of the entity in question is variable and
20054 that we could not generate a DWARF expression that computes it. */
20055 if (size >= 0)
20056 add_AT_unsigned (die, DW_AT_byte_size, size);
20059 /* Add a DW_AT_alignment attribute to DIE with TREE_NODE's non-default
20060 alignment. */
20062 static void
20063 add_alignment_attribute (dw_die_ref die, tree tree_node)
20065 if (dwarf_version < 5 && dwarf_strict)
20066 return;
20068 unsigned align;
20070 if (DECL_P (tree_node))
20072 if (!DECL_USER_ALIGN (tree_node))
20073 return;
20075 align = DECL_ALIGN_UNIT (tree_node);
20077 else if (TYPE_P (tree_node))
20079 if (!TYPE_USER_ALIGN (tree_node))
20080 return;
20082 align = TYPE_ALIGN_UNIT (tree_node);
20084 else
20085 gcc_unreachable ();
20087 add_AT_unsigned (die, DW_AT_alignment, align);
20090 /* For a FIELD_DECL node which represents a bit-field, output an attribute
20091 which specifies the distance in bits from the highest order bit of the
20092 "containing object" for the bit-field to the highest order bit of the
20093 bit-field itself.
20095 For any given bit-field, the "containing object" is a hypothetical object
20096 (of some integral or enum type) within which the given bit-field lives. The
20097 type of this hypothetical "containing object" is always the same as the
20098 declared type of the individual bit-field itself. The determination of the
20099 exact location of the "containing object" for a bit-field is rather
20100 complicated. It's handled by the `field_byte_offset' function (above).
20102 CTX is required: see the comment for VLR_CONTEXT.
20104 Note that it is the size (in bytes) of the hypothetical "containing object"
20105 which will be given in the DW_AT_byte_size attribute for this bit-field.
20106 (See `byte_size_attribute' above). */
20108 static inline void
20109 add_bit_offset_attribute (dw_die_ref die, tree decl, struct vlr_context *ctx)
20111 HOST_WIDE_INT object_offset_in_bytes;
20112 tree original_type = DECL_BIT_FIELD_TYPE (decl);
20113 HOST_WIDE_INT bitpos_int;
20114 HOST_WIDE_INT highest_order_object_bit_offset;
20115 HOST_WIDE_INT highest_order_field_bit_offset;
20116 HOST_WIDE_INT bit_offset;
20118 field_byte_offset (decl, ctx, &object_offset_in_bytes);
20120 /* Must be a field and a bit field. */
20121 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
20123 /* We can't yet handle bit-fields whose offsets are variable, so if we
20124 encounter such things, just return without generating any attribute
20125 whatsoever. Likewise for variable or too large size. */
20126 if (! tree_fits_shwi_p (bit_position (decl))
20127 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
20128 return;
20130 bitpos_int = int_bit_position (decl);
20132 /* Note that the bit offset is always the distance (in bits) from the
20133 highest-order bit of the "containing object" to the highest-order bit of
20134 the bit-field itself. Since the "high-order end" of any object or field
20135 is different on big-endian and little-endian machines, the computation
20136 below must take account of these differences. */
20137 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
20138 highest_order_field_bit_offset = bitpos_int;
20140 if (! BYTES_BIG_ENDIAN)
20142 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
20143 highest_order_object_bit_offset +=
20144 simple_type_size_in_bits (original_type);
20147 bit_offset
20148 = (! BYTES_BIG_ENDIAN
20149 ? highest_order_object_bit_offset - highest_order_field_bit_offset
20150 : highest_order_field_bit_offset - highest_order_object_bit_offset);
20152 if (bit_offset < 0)
20153 add_AT_int (die, DW_AT_bit_offset, bit_offset);
20154 else
20155 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
20158 /* For a FIELD_DECL node which represents a bit field, output an attribute
20159 which specifies the length in bits of the given field. */
20161 static inline void
20162 add_bit_size_attribute (dw_die_ref die, tree decl)
20164 /* Must be a field and a bit field. */
20165 gcc_assert (TREE_CODE (decl) == FIELD_DECL
20166 && DECL_BIT_FIELD_TYPE (decl));
20168 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
20169 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
20172 /* If the compiled language is ANSI C, then add a 'prototyped'
20173 attribute, if arg types are given for the parameters of a function. */
20175 static inline void
20176 add_prototyped_attribute (dw_die_ref die, tree func_type)
20178 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
20180 case DW_LANG_C:
20181 case DW_LANG_C89:
20182 case DW_LANG_C99:
20183 case DW_LANG_C11:
20184 case DW_LANG_ObjC:
20185 if (prototype_p (func_type))
20186 add_AT_flag (die, DW_AT_prototyped, 1);
20187 break;
20188 default:
20189 break;
20193 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
20194 by looking in the type declaration, the object declaration equate table or
20195 the block mapping. */
20197 static inline dw_die_ref
20198 add_abstract_origin_attribute (dw_die_ref die, tree origin)
20200 dw_die_ref origin_die = NULL;
20202 if (TREE_CODE (origin) != FUNCTION_DECL
20203 && TREE_CODE (origin) != BLOCK)
20205 /* We may have gotten separated from the block for the inlined
20206 function, if we're in an exception handler or some such; make
20207 sure that the abstract function has been written out.
20209 Doing this for nested functions is wrong, however; functions are
20210 distinct units, and our context might not even be inline. */
20211 tree fn = origin;
20213 if (TYPE_P (fn))
20214 fn = TYPE_STUB_DECL (fn);
20216 fn = decl_function_context (fn);
20217 if (fn)
20218 dwarf2out_abstract_function (fn);
20221 if (DECL_P (origin))
20222 origin_die = lookup_decl_die (origin);
20223 else if (TYPE_P (origin))
20224 origin_die = lookup_type_die (origin);
20225 else if (TREE_CODE (origin) == BLOCK)
20226 origin_die = BLOCK_DIE (origin);
20228 /* XXX: Functions that are never lowered don't always have correct block
20229 trees (in the case of java, they simply have no block tree, in some other
20230 languages). For these functions, there is nothing we can really do to
20231 output correct debug info for inlined functions in all cases. Rather
20232 than die, we'll just produce deficient debug info now, in that we will
20233 have variables without a proper abstract origin. In the future, when all
20234 functions are lowered, we should re-add a gcc_assert (origin_die)
20235 here. */
20237 if (origin_die)
20238 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
20239 return origin_die;
20242 /* We do not currently support the pure_virtual attribute. */
20244 static inline void
20245 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
20247 if (DECL_VINDEX (func_decl))
20249 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20251 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
20252 add_AT_loc (die, DW_AT_vtable_elem_location,
20253 new_loc_descr (DW_OP_constu,
20254 tree_to_shwi (DECL_VINDEX (func_decl)),
20255 0));
20257 /* GNU extension: Record what type this method came from originally. */
20258 if (debug_info_level > DINFO_LEVEL_TERSE
20259 && DECL_CONTEXT (func_decl))
20260 add_AT_die_ref (die, DW_AT_containing_type,
20261 lookup_type_die (DECL_CONTEXT (func_decl)));
20265 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
20266 given decl. This used to be a vendor extension until after DWARF 4
20267 standardized it. */
20269 static void
20270 add_linkage_attr (dw_die_ref die, tree decl)
20272 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
20274 /* Mimic what assemble_name_raw does with a leading '*'. */
20275 if (name[0] == '*')
20276 name = &name[1];
20278 if (dwarf_version >= 4)
20279 add_AT_string (die, DW_AT_linkage_name, name);
20280 else
20281 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
20284 /* Add source coordinate attributes for the given decl. */
20286 static void
20287 add_src_coords_attributes (dw_die_ref die, tree decl)
20289 expanded_location s;
20291 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
20292 return;
20293 s = expand_location (DECL_SOURCE_LOCATION (decl));
20294 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
20295 add_AT_unsigned (die, DW_AT_decl_line, s.line);
20296 if (debug_column_info && s.column)
20297 add_AT_unsigned (die, DW_AT_decl_column, s.column);
20300 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
20302 static void
20303 add_linkage_name_raw (dw_die_ref die, tree decl)
20305 /* Defer until we have an assembler name set. */
20306 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
20308 limbo_die_node *asm_name;
20310 asm_name = ggc_cleared_alloc<limbo_die_node> ();
20311 asm_name->die = die;
20312 asm_name->created_for = decl;
20313 asm_name->next = deferred_asm_name;
20314 deferred_asm_name = asm_name;
20316 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
20317 add_linkage_attr (die, decl);
20320 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
20322 static void
20323 add_linkage_name (dw_die_ref die, tree decl)
20325 if (debug_info_level > DINFO_LEVEL_NONE
20326 && VAR_OR_FUNCTION_DECL_P (decl)
20327 && TREE_PUBLIC (decl)
20328 && !(VAR_P (decl) && DECL_REGISTER (decl))
20329 && die->die_tag != DW_TAG_member)
20330 add_linkage_name_raw (die, decl);
20333 /* Add a DW_AT_name attribute and source coordinate attribute for the
20334 given decl, but only if it actually has a name. */
20336 static void
20337 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
20338 bool no_linkage_name)
20340 tree decl_name;
20342 decl_name = DECL_NAME (decl);
20343 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
20345 const char *name = dwarf2_name (decl, 0);
20346 if (name)
20347 add_name_attribute (die, name);
20348 if (! DECL_ARTIFICIAL (decl))
20349 add_src_coords_attributes (die, decl);
20351 if (!no_linkage_name)
20352 add_linkage_name (die, decl);
20355 #ifdef VMS_DEBUGGING_INFO
20356 /* Get the function's name, as described by its RTL. This may be different
20357 from the DECL_NAME name used in the source file. */
20358 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
20360 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
20361 XEXP (DECL_RTL (decl), 0), false);
20362 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
20364 #endif /* VMS_DEBUGGING_INFO */
20367 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
20369 static void
20370 add_discr_value (dw_die_ref die, dw_discr_value *value)
20372 dw_attr_node attr;
20374 attr.dw_attr = DW_AT_discr_value;
20375 attr.dw_attr_val.val_class = dw_val_class_discr_value;
20376 attr.dw_attr_val.val_entry = NULL;
20377 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
20378 if (value->pos)
20379 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
20380 else
20381 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
20382 add_dwarf_attr (die, &attr);
20385 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
20387 static void
20388 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
20390 dw_attr_node attr;
20392 attr.dw_attr = DW_AT_discr_list;
20393 attr.dw_attr_val.val_class = dw_val_class_discr_list;
20394 attr.dw_attr_val.val_entry = NULL;
20395 attr.dw_attr_val.v.val_discr_list = discr_list;
20396 add_dwarf_attr (die, &attr);
20399 static inline dw_discr_list_ref
20400 AT_discr_list (dw_attr_node *attr)
20402 return attr->dw_attr_val.v.val_discr_list;
20405 #ifdef VMS_DEBUGGING_INFO
20406 /* Output the debug main pointer die for VMS */
20408 void
20409 dwarf2out_vms_debug_main_pointer (void)
20411 char label[MAX_ARTIFICIAL_LABEL_BYTES];
20412 dw_die_ref die;
20414 /* Allocate the VMS debug main subprogram die. */
20415 die = ggc_cleared_alloc<die_node> ();
20416 die->die_tag = DW_TAG_subprogram;
20417 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
20418 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
20419 current_function_funcdef_no);
20420 add_AT_lbl_id (die, DW_AT_entry_pc, label);
20422 /* Make it the first child of comp_unit_die (). */
20423 die->die_parent = comp_unit_die ();
20424 if (comp_unit_die ()->die_child)
20426 die->die_sib = comp_unit_die ()->die_child->die_sib;
20427 comp_unit_die ()->die_child->die_sib = die;
20429 else
20431 die->die_sib = die;
20432 comp_unit_die ()->die_child = die;
20435 #endif /* VMS_DEBUGGING_INFO */
20437 /* Push a new declaration scope. */
20439 static void
20440 push_decl_scope (tree scope)
20442 vec_safe_push (decl_scope_table, scope);
20445 /* Pop a declaration scope. */
20447 static inline void
20448 pop_decl_scope (void)
20450 decl_scope_table->pop ();
20453 /* walk_tree helper function for uses_local_type, below. */
20455 static tree
20456 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
20458 if (!TYPE_P (*tp))
20459 *walk_subtrees = 0;
20460 else
20462 tree name = TYPE_NAME (*tp);
20463 if (name && DECL_P (name) && decl_function_context (name))
20464 return *tp;
20466 return NULL_TREE;
20469 /* If TYPE involves a function-local type (including a local typedef to a
20470 non-local type), returns that type; otherwise returns NULL_TREE. */
20472 static tree
20473 uses_local_type (tree type)
20475 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
20476 return used;
20479 /* Return the DIE for the scope that immediately contains this type.
20480 Non-named types that do not involve a function-local type get global
20481 scope. Named types nested in namespaces or other types get their
20482 containing scope. All other types (i.e. function-local named types) get
20483 the current active scope. */
20485 static dw_die_ref
20486 scope_die_for (tree t, dw_die_ref context_die)
20488 dw_die_ref scope_die = NULL;
20489 tree containing_scope;
20491 /* Non-types always go in the current scope. */
20492 gcc_assert (TYPE_P (t));
20494 /* Use the scope of the typedef, rather than the scope of the type
20495 it refers to. */
20496 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
20497 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
20498 else
20499 containing_scope = TYPE_CONTEXT (t);
20501 /* Use the containing namespace if there is one. */
20502 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
20504 if (context_die == lookup_decl_die (containing_scope))
20505 /* OK */;
20506 else if (debug_info_level > DINFO_LEVEL_TERSE)
20507 context_die = get_context_die (containing_scope);
20508 else
20509 containing_scope = NULL_TREE;
20512 /* Ignore function type "scopes" from the C frontend. They mean that
20513 a tagged type is local to a parmlist of a function declarator, but
20514 that isn't useful to DWARF. */
20515 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
20516 containing_scope = NULL_TREE;
20518 if (SCOPE_FILE_SCOPE_P (containing_scope))
20520 /* If T uses a local type keep it local as well, to avoid references
20521 to function-local DIEs from outside the function. */
20522 if (current_function_decl && uses_local_type (t))
20523 scope_die = context_die;
20524 else
20525 scope_die = comp_unit_die ();
20527 else if (TYPE_P (containing_scope))
20529 /* For types, we can just look up the appropriate DIE. */
20530 if (debug_info_level > DINFO_LEVEL_TERSE)
20531 scope_die = get_context_die (containing_scope);
20532 else
20534 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
20535 if (scope_die == NULL)
20536 scope_die = comp_unit_die ();
20539 else
20540 scope_die = context_die;
20542 return scope_die;
20545 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
20547 static inline int
20548 local_scope_p (dw_die_ref context_die)
20550 for (; context_die; context_die = context_die->die_parent)
20551 if (context_die->die_tag == DW_TAG_inlined_subroutine
20552 || context_die->die_tag == DW_TAG_subprogram)
20553 return 1;
20555 return 0;
20558 /* Returns nonzero if CONTEXT_DIE is a class. */
20560 static inline int
20561 class_scope_p (dw_die_ref context_die)
20563 return (context_die
20564 && (context_die->die_tag == DW_TAG_structure_type
20565 || context_die->die_tag == DW_TAG_class_type
20566 || context_die->die_tag == DW_TAG_interface_type
20567 || context_die->die_tag == DW_TAG_union_type));
20570 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
20571 whether or not to treat a DIE in this context as a declaration. */
20573 static inline int
20574 class_or_namespace_scope_p (dw_die_ref context_die)
20576 return (class_scope_p (context_die)
20577 || (context_die && context_die->die_tag == DW_TAG_namespace));
20580 /* Many forms of DIEs require a "type description" attribute. This
20581 routine locates the proper "type descriptor" die for the type given
20582 by 'type' plus any additional qualifiers given by 'cv_quals', and
20583 adds a DW_AT_type attribute below the given die. */
20585 static void
20586 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
20587 bool reverse, dw_die_ref context_die)
20589 enum tree_code code = TREE_CODE (type);
20590 dw_die_ref type_die = NULL;
20592 /* ??? If this type is an unnamed subrange type of an integral, floating-point
20593 or fixed-point type, use the inner type. This is because we have no
20594 support for unnamed types in base_type_die. This can happen if this is
20595 an Ada subrange type. Correct solution is emit a subrange type die. */
20596 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
20597 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
20598 type = TREE_TYPE (type), code = TREE_CODE (type);
20600 if (code == ERROR_MARK
20601 /* Handle a special case. For functions whose return type is void, we
20602 generate *no* type attribute. (Note that no object may have type
20603 `void', so this only applies to function return types). */
20604 || code == VOID_TYPE)
20605 return;
20607 type_die = modified_type_die (type,
20608 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
20609 reverse,
20610 context_die);
20612 if (type_die != NULL)
20613 add_AT_die_ref (object_die, DW_AT_type, type_die);
20616 /* Given an object die, add the calling convention attribute for the
20617 function call type. */
20618 static void
20619 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
20621 enum dwarf_calling_convention value = DW_CC_normal;
20623 value = ((enum dwarf_calling_convention)
20624 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
20626 if (is_fortran ()
20627 && id_equal (DECL_ASSEMBLER_NAME (decl), "MAIN__"))
20629 /* DWARF 2 doesn't provide a way to identify a program's source-level
20630 entry point. DW_AT_calling_convention attributes are only meant
20631 to describe functions' calling conventions. However, lacking a
20632 better way to signal the Fortran main program, we used this for
20633 a long time, following existing custom. Now, DWARF 4 has
20634 DW_AT_main_subprogram, which we add below, but some tools still
20635 rely on the old way, which we thus keep. */
20636 value = DW_CC_program;
20638 if (dwarf_version >= 4 || !dwarf_strict)
20639 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
20642 /* Only add the attribute if the backend requests it, and
20643 is not DW_CC_normal. */
20644 if (value && (value != DW_CC_normal))
20645 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
20648 /* Given a tree pointer to a struct, class, union, or enum type node, return
20649 a pointer to the (string) tag name for the given type, or zero if the type
20650 was declared without a tag. */
20652 static const char *
20653 type_tag (const_tree type)
20655 const char *name = 0;
20657 if (TYPE_NAME (type) != 0)
20659 tree t = 0;
20661 /* Find the IDENTIFIER_NODE for the type name. */
20662 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
20663 && !TYPE_NAMELESS (type))
20664 t = TYPE_NAME (type);
20666 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
20667 a TYPE_DECL node, regardless of whether or not a `typedef' was
20668 involved. */
20669 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20670 && ! DECL_IGNORED_P (TYPE_NAME (type)))
20672 /* We want to be extra verbose. Don't call dwarf_name if
20673 DECL_NAME isn't set. The default hook for decl_printable_name
20674 doesn't like that, and in this context it's correct to return
20675 0, instead of "<anonymous>" or the like. */
20676 if (DECL_NAME (TYPE_NAME (type))
20677 && !DECL_NAMELESS (TYPE_NAME (type)))
20678 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
20681 /* Now get the name as a string, or invent one. */
20682 if (!name && t != 0)
20683 name = IDENTIFIER_POINTER (t);
20686 return (name == 0 || *name == '\0') ? 0 : name;
20689 /* Return the type associated with a data member, make a special check
20690 for bit field types. */
20692 static inline tree
20693 member_declared_type (const_tree member)
20695 return (DECL_BIT_FIELD_TYPE (member)
20696 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
20699 /* Get the decl's label, as described by its RTL. This may be different
20700 from the DECL_NAME name used in the source file. */
20702 #if 0
20703 static const char *
20704 decl_start_label (tree decl)
20706 rtx x;
20707 const char *fnname;
20709 x = DECL_RTL (decl);
20710 gcc_assert (MEM_P (x));
20712 x = XEXP (x, 0);
20713 gcc_assert (GET_CODE (x) == SYMBOL_REF);
20715 fnname = XSTR (x, 0);
20716 return fnname;
20718 #endif
20720 /* For variable-length arrays that have been previously generated, but
20721 may be incomplete due to missing subscript info, fill the subscript
20722 info. Return TRUE if this is one of those cases. */
20723 static bool
20724 fill_variable_array_bounds (tree type)
20726 if (TREE_ASM_WRITTEN (type)
20727 && TREE_CODE (type) == ARRAY_TYPE
20728 && variably_modified_type_p (type, NULL))
20730 dw_die_ref array_die = lookup_type_die (type);
20731 if (!array_die)
20732 return false;
20733 add_subscript_info (array_die, type, !is_ada ());
20734 return true;
20736 return false;
20739 /* These routines generate the internal representation of the DIE's for
20740 the compilation unit. Debugging information is collected by walking
20741 the declaration trees passed in from dwarf2out_decl(). */
20743 static void
20744 gen_array_type_die (tree type, dw_die_ref context_die)
20746 dw_die_ref array_die;
20748 /* GNU compilers represent multidimensional array types as sequences of one
20749 dimensional array types whose element types are themselves array types.
20750 We sometimes squish that down to a single array_type DIE with multiple
20751 subscripts in the Dwarf debugging info. The draft Dwarf specification
20752 say that we are allowed to do this kind of compression in C, because
20753 there is no difference between an array of arrays and a multidimensional
20754 array. We don't do this for Ada to remain as close as possible to the
20755 actual representation, which is especially important against the language
20756 flexibilty wrt arrays of variable size. */
20758 bool collapse_nested_arrays = !is_ada ();
20760 if (fill_variable_array_bounds (type))
20761 return;
20763 dw_die_ref scope_die = scope_die_for (type, context_die);
20764 tree element_type;
20766 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
20767 DW_TAG_string_type doesn't have DW_AT_type attribute). */
20768 if (TYPE_STRING_FLAG (type)
20769 && TREE_CODE (type) == ARRAY_TYPE
20770 && is_fortran ()
20771 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
20773 HOST_WIDE_INT size;
20775 array_die = new_die (DW_TAG_string_type, scope_die, type);
20776 add_name_attribute (array_die, type_tag (type));
20777 equate_type_number_to_die (type, array_die);
20778 size = int_size_in_bytes (type);
20779 if (size >= 0)
20780 add_AT_unsigned (array_die, DW_AT_byte_size, size);
20781 else if (TYPE_DOMAIN (type) != NULL_TREE
20782 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
20784 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
20785 tree rszdecl = szdecl;
20787 size = int_size_in_bytes (TREE_TYPE (szdecl));
20788 if (!DECL_P (szdecl))
20790 if (TREE_CODE (szdecl) == INDIRECT_REF
20791 && DECL_P (TREE_OPERAND (szdecl, 0)))
20793 rszdecl = TREE_OPERAND (szdecl, 0);
20794 if (int_size_in_bytes (TREE_TYPE (rszdecl))
20795 != DWARF2_ADDR_SIZE)
20796 size = 0;
20798 else
20799 size = 0;
20801 if (size > 0)
20803 dw_loc_list_ref loc
20804 = loc_list_from_tree (rszdecl, szdecl == rszdecl ? 2 : 0,
20805 NULL);
20806 if (loc)
20808 add_AT_location_description (array_die, DW_AT_string_length,
20809 loc);
20810 if (size != DWARF2_ADDR_SIZE)
20811 add_AT_unsigned (array_die, dwarf_version >= 5
20812 ? DW_AT_string_length_byte_size
20813 : DW_AT_byte_size, size);
20817 return;
20820 array_die = new_die (DW_TAG_array_type, scope_die, type);
20821 add_name_attribute (array_die, type_tag (type));
20822 equate_type_number_to_die (type, array_die);
20824 if (TREE_CODE (type) == VECTOR_TYPE)
20825 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
20827 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
20828 if (is_fortran ()
20829 && TREE_CODE (type) == ARRAY_TYPE
20830 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
20831 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
20832 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
20834 #if 0
20835 /* We default the array ordering. SDB will probably do
20836 the right things even if DW_AT_ordering is not present. It's not even
20837 an issue until we start to get into multidimensional arrays anyway. If
20838 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
20839 then we'll have to put the DW_AT_ordering attribute back in. (But if
20840 and when we find out that we need to put these in, we will only do so
20841 for multidimensional arrays. */
20842 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
20843 #endif
20845 if (TREE_CODE (type) == VECTOR_TYPE)
20847 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
20848 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
20849 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
20850 add_bound_info (subrange_die, DW_AT_upper_bound,
20851 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
20853 else
20854 add_subscript_info (array_die, type, collapse_nested_arrays);
20856 /* Add representation of the type of the elements of this array type and
20857 emit the corresponding DIE if we haven't done it already. */
20858 element_type = TREE_TYPE (type);
20859 if (collapse_nested_arrays)
20860 while (TREE_CODE (element_type) == ARRAY_TYPE)
20862 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
20863 break;
20864 element_type = TREE_TYPE (element_type);
20867 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
20868 TREE_CODE (type) == ARRAY_TYPE
20869 && TYPE_REVERSE_STORAGE_ORDER (type),
20870 context_die);
20872 add_gnat_descriptive_type_attribute (array_die, type, context_die);
20873 if (TYPE_ARTIFICIAL (type))
20874 add_AT_flag (array_die, DW_AT_artificial, 1);
20876 if (get_AT (array_die, DW_AT_name))
20877 add_pubtype (type, array_die);
20879 add_alignment_attribute (array_die, type);
20882 /* This routine generates DIE for array with hidden descriptor, details
20883 are filled into *info by a langhook. */
20885 static void
20886 gen_descr_array_type_die (tree type, struct array_descr_info *info,
20887 dw_die_ref context_die)
20889 const dw_die_ref scope_die = scope_die_for (type, context_die);
20890 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
20891 struct loc_descr_context context = { type, info->base_decl, NULL,
20892 false, false };
20893 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
20894 int dim;
20896 add_name_attribute (array_die, type_tag (type));
20897 equate_type_number_to_die (type, array_die);
20899 if (info->ndimensions > 1)
20900 switch (info->ordering)
20902 case array_descr_ordering_row_major:
20903 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
20904 break;
20905 case array_descr_ordering_column_major:
20906 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
20907 break;
20908 default:
20909 break;
20912 if (dwarf_version >= 3 || !dwarf_strict)
20914 if (info->data_location)
20915 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
20916 dw_scalar_form_exprloc, &context);
20917 if (info->associated)
20918 add_scalar_info (array_die, DW_AT_associated, info->associated,
20919 dw_scalar_form_constant
20920 | dw_scalar_form_exprloc
20921 | dw_scalar_form_reference, &context);
20922 if (info->allocated)
20923 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
20924 dw_scalar_form_constant
20925 | dw_scalar_form_exprloc
20926 | dw_scalar_form_reference, &context);
20927 if (info->stride)
20929 const enum dwarf_attribute attr
20930 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
20931 const int forms
20932 = (info->stride_in_bits)
20933 ? dw_scalar_form_constant
20934 : (dw_scalar_form_constant
20935 | dw_scalar_form_exprloc
20936 | dw_scalar_form_reference);
20938 add_scalar_info (array_die, attr, info->stride, forms, &context);
20941 if (dwarf_version >= 5)
20943 if (info->rank)
20945 add_scalar_info (array_die, DW_AT_rank, info->rank,
20946 dw_scalar_form_constant
20947 | dw_scalar_form_exprloc, &context);
20948 subrange_tag = DW_TAG_generic_subrange;
20949 context.placeholder_arg = true;
20953 add_gnat_descriptive_type_attribute (array_die, type, context_die);
20955 for (dim = 0; dim < info->ndimensions; dim++)
20957 dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
20959 if (info->dimen[dim].bounds_type)
20960 add_type_attribute (subrange_die,
20961 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
20962 false, context_die);
20963 if (info->dimen[dim].lower_bound)
20964 add_bound_info (subrange_die, DW_AT_lower_bound,
20965 info->dimen[dim].lower_bound, &context);
20966 if (info->dimen[dim].upper_bound)
20967 add_bound_info (subrange_die, DW_AT_upper_bound,
20968 info->dimen[dim].upper_bound, &context);
20969 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
20970 add_scalar_info (subrange_die, DW_AT_byte_stride,
20971 info->dimen[dim].stride,
20972 dw_scalar_form_constant
20973 | dw_scalar_form_exprloc
20974 | dw_scalar_form_reference,
20975 &context);
20978 gen_type_die (info->element_type, context_die);
20979 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
20980 TREE_CODE (type) == ARRAY_TYPE
20981 && TYPE_REVERSE_STORAGE_ORDER (type),
20982 context_die);
20984 if (get_AT (array_die, DW_AT_name))
20985 add_pubtype (type, array_die);
20987 add_alignment_attribute (array_die, type);
20990 #if 0
20991 static void
20992 gen_entry_point_die (tree decl, dw_die_ref context_die)
20994 tree origin = decl_ultimate_origin (decl);
20995 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
20997 if (origin != NULL)
20998 add_abstract_origin_attribute (decl_die, origin);
20999 else
21001 add_name_and_src_coords_attributes (decl_die, decl);
21002 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
21003 TYPE_UNQUALIFIED, false, context_die);
21006 if (DECL_ABSTRACT_P (decl))
21007 equate_decl_number_to_die (decl, decl_die);
21008 else
21009 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
21011 #endif
21013 /* Walk through the list of incomplete types again, trying once more to
21014 emit full debugging info for them. */
21016 static void
21017 retry_incomplete_types (void)
21019 set_early_dwarf s;
21020 int i;
21022 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
21023 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
21024 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
21025 vec_safe_truncate (incomplete_types, 0);
21028 /* Determine what tag to use for a record type. */
21030 static enum dwarf_tag
21031 record_type_tag (tree type)
21033 if (! lang_hooks.types.classify_record)
21034 return DW_TAG_structure_type;
21036 switch (lang_hooks.types.classify_record (type))
21038 case RECORD_IS_STRUCT:
21039 return DW_TAG_structure_type;
21041 case RECORD_IS_CLASS:
21042 return DW_TAG_class_type;
21044 case RECORD_IS_INTERFACE:
21045 if (dwarf_version >= 3 || !dwarf_strict)
21046 return DW_TAG_interface_type;
21047 return DW_TAG_structure_type;
21049 default:
21050 gcc_unreachable ();
21054 /* Generate a DIE to represent an enumeration type. Note that these DIEs
21055 include all of the information about the enumeration values also. Each
21056 enumerated type name/value is listed as a child of the enumerated type
21057 DIE. */
21059 static dw_die_ref
21060 gen_enumeration_type_die (tree type, dw_die_ref context_die)
21062 dw_die_ref type_die = lookup_type_die (type);
21064 if (type_die == NULL)
21066 type_die = new_die (DW_TAG_enumeration_type,
21067 scope_die_for (type, context_die), type);
21068 equate_type_number_to_die (type, type_die);
21069 add_name_attribute (type_die, type_tag (type));
21070 if (dwarf_version >= 4 || !dwarf_strict)
21072 if (ENUM_IS_SCOPED (type))
21073 add_AT_flag (type_die, DW_AT_enum_class, 1);
21074 if (ENUM_IS_OPAQUE (type))
21075 add_AT_flag (type_die, DW_AT_declaration, 1);
21077 if (!dwarf_strict)
21078 add_AT_unsigned (type_die, DW_AT_encoding,
21079 TYPE_UNSIGNED (type)
21080 ? DW_ATE_unsigned
21081 : DW_ATE_signed);
21083 else if (! TYPE_SIZE (type))
21084 return type_die;
21085 else
21086 remove_AT (type_die, DW_AT_declaration);
21088 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
21089 given enum type is incomplete, do not generate the DW_AT_byte_size
21090 attribute or the DW_AT_element_list attribute. */
21091 if (TYPE_SIZE (type))
21093 tree link;
21095 TREE_ASM_WRITTEN (type) = 1;
21096 add_byte_size_attribute (type_die, type);
21097 add_alignment_attribute (type_die, type);
21098 if (dwarf_version >= 3 || !dwarf_strict)
21100 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
21101 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
21102 context_die);
21104 if (TYPE_STUB_DECL (type) != NULL_TREE)
21106 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
21107 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
21110 /* If the first reference to this type was as the return type of an
21111 inline function, then it may not have a parent. Fix this now. */
21112 if (type_die->die_parent == NULL)
21113 add_child_die (scope_die_for (type, context_die), type_die);
21115 for (link = TYPE_VALUES (type);
21116 link != NULL; link = TREE_CHAIN (link))
21118 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
21119 tree value = TREE_VALUE (link);
21121 add_name_attribute (enum_die,
21122 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
21124 if (TREE_CODE (value) == CONST_DECL)
21125 value = DECL_INITIAL (value);
21127 if (simple_type_size_in_bits (TREE_TYPE (value))
21128 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
21130 /* For constant forms created by add_AT_unsigned DWARF
21131 consumers (GDB, elfutils, etc.) always zero extend
21132 the value. Only when the actual value is negative
21133 do we need to use add_AT_int to generate a constant
21134 form that can represent negative values. */
21135 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
21136 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
21137 add_AT_unsigned (enum_die, DW_AT_const_value,
21138 (unsigned HOST_WIDE_INT) val);
21139 else
21140 add_AT_int (enum_die, DW_AT_const_value, val);
21142 else
21143 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
21144 that here. TODO: This should be re-worked to use correct
21145 signed/unsigned double tags for all cases. */
21146 add_AT_wide (enum_die, DW_AT_const_value, value);
21149 add_gnat_descriptive_type_attribute (type_die, type, context_die);
21150 if (TYPE_ARTIFICIAL (type))
21151 add_AT_flag (type_die, DW_AT_artificial, 1);
21153 else
21154 add_AT_flag (type_die, DW_AT_declaration, 1);
21156 add_alignment_attribute (type_die, type);
21158 add_pubtype (type, type_die);
21160 return type_die;
21163 /* Generate a DIE to represent either a real live formal parameter decl or to
21164 represent just the type of some formal parameter position in some function
21165 type.
21167 Note that this routine is a bit unusual because its argument may be a
21168 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
21169 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
21170 node. If it's the former then this function is being called to output a
21171 DIE to represent a formal parameter object (or some inlining thereof). If
21172 it's the latter, then this function is only being called to output a
21173 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
21174 argument type of some subprogram type.
21175 If EMIT_NAME_P is true, name and source coordinate attributes
21176 are emitted. */
21178 static dw_die_ref
21179 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
21180 dw_die_ref context_die)
21182 tree node_or_origin = node ? node : origin;
21183 tree ultimate_origin;
21184 dw_die_ref parm_die = NULL;
21186 if (TREE_CODE_CLASS (TREE_CODE (node_or_origin)) == tcc_declaration)
21188 parm_die = lookup_decl_die (node);
21190 /* If the contexts differ, we may not be talking about the same
21191 thing. */
21192 if (parm_die && parm_die->die_parent != context_die)
21194 if (!DECL_ABSTRACT_P (node))
21196 /* This can happen when creating an inlined instance, in
21197 which case we need to create a new DIE that will get
21198 annotated with DW_AT_abstract_origin. */
21199 parm_die = NULL;
21201 else
21203 /* FIXME: Reuse DIE even with a differing context.
21205 This can happen when calling
21206 dwarf2out_abstract_function to build debug info for
21207 the abstract instance of a function for which we have
21208 already generated a DIE in
21209 dwarf2out_early_global_decl.
21211 Once we remove dwarf2out_abstract_function, we should
21212 have a call to gcc_unreachable here. */
21216 if (parm_die && parm_die->die_parent == NULL)
21218 /* Check that parm_die already has the right attributes that
21219 we would have added below. If any attributes are
21220 missing, fall through to add them. */
21221 if (! DECL_ABSTRACT_P (node_or_origin)
21222 && !get_AT (parm_die, DW_AT_location)
21223 && !get_AT (parm_die, DW_AT_const_value))
21224 /* We are missing location info, and are about to add it. */
21226 else
21228 add_child_die (context_die, parm_die);
21229 return parm_die;
21234 /* If we have a previously generated DIE, use it, unless this is an
21235 concrete instance (origin != NULL), in which case we need a new
21236 DIE with a corresponding DW_AT_abstract_origin. */
21237 bool reusing_die;
21238 if (parm_die && origin == NULL)
21239 reusing_die = true;
21240 else
21242 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
21243 reusing_die = false;
21246 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
21248 case tcc_declaration:
21249 ultimate_origin = decl_ultimate_origin (node_or_origin);
21250 if (node || ultimate_origin)
21251 origin = ultimate_origin;
21253 if (reusing_die)
21254 goto add_location;
21256 if (origin != NULL)
21257 add_abstract_origin_attribute (parm_die, origin);
21258 else if (emit_name_p)
21259 add_name_and_src_coords_attributes (parm_die, node);
21260 if (origin == NULL
21261 || (! DECL_ABSTRACT_P (node_or_origin)
21262 && variably_modified_type_p (TREE_TYPE (node_or_origin),
21263 decl_function_context
21264 (node_or_origin))))
21266 tree type = TREE_TYPE (node_or_origin);
21267 if (decl_by_reference_p (node_or_origin))
21268 add_type_attribute (parm_die, TREE_TYPE (type),
21269 TYPE_UNQUALIFIED,
21270 false, context_die);
21271 else
21272 add_type_attribute (parm_die, type,
21273 decl_quals (node_or_origin),
21274 false, context_die);
21276 if (origin == NULL && DECL_ARTIFICIAL (node))
21277 add_AT_flag (parm_die, DW_AT_artificial, 1);
21278 add_location:
21279 if (node && node != origin)
21280 equate_decl_number_to_die (node, parm_die);
21281 if (! DECL_ABSTRACT_P (node_or_origin))
21282 add_location_or_const_value_attribute (parm_die, node_or_origin,
21283 node == NULL);
21285 break;
21287 case tcc_type:
21288 /* We were called with some kind of a ..._TYPE node. */
21289 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
21290 context_die);
21291 break;
21293 default:
21294 gcc_unreachable ();
21297 return parm_die;
21300 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
21301 children DW_TAG_formal_parameter DIEs representing the arguments of the
21302 parameter pack.
21304 PARM_PACK must be a function parameter pack.
21305 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
21306 must point to the subsequent arguments of the function PACK_ARG belongs to.
21307 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
21308 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
21309 following the last one for which a DIE was generated. */
21311 static dw_die_ref
21312 gen_formal_parameter_pack_die (tree parm_pack,
21313 tree pack_arg,
21314 dw_die_ref subr_die,
21315 tree *next_arg)
21317 tree arg;
21318 dw_die_ref parm_pack_die;
21320 gcc_assert (parm_pack
21321 && lang_hooks.function_parameter_pack_p (parm_pack)
21322 && subr_die);
21324 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
21325 add_src_coords_attributes (parm_pack_die, parm_pack);
21327 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
21329 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
21330 parm_pack))
21331 break;
21332 gen_formal_parameter_die (arg, NULL,
21333 false /* Don't emit name attribute. */,
21334 parm_pack_die);
21336 if (next_arg)
21337 *next_arg = arg;
21338 return parm_pack_die;
21341 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
21342 at the end of an (ANSI prototyped) formal parameters list. */
21344 static void
21345 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
21347 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
21350 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
21351 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
21352 parameters as specified in some function type specification (except for
21353 those which appear as part of a function *definition*). */
21355 static void
21356 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
21358 tree link;
21359 tree formal_type = NULL;
21360 tree first_parm_type;
21361 tree arg;
21363 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
21365 arg = DECL_ARGUMENTS (function_or_method_type);
21366 function_or_method_type = TREE_TYPE (function_or_method_type);
21368 else
21369 arg = NULL_TREE;
21371 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
21373 /* Make our first pass over the list of formal parameter types and output a
21374 DW_TAG_formal_parameter DIE for each one. */
21375 for (link = first_parm_type; link; )
21377 dw_die_ref parm_die;
21379 formal_type = TREE_VALUE (link);
21380 if (formal_type == void_type_node)
21381 break;
21383 /* Output a (nameless) DIE to represent the formal parameter itself. */
21384 if (!POINTER_BOUNDS_TYPE_P (formal_type))
21386 parm_die = gen_formal_parameter_die (formal_type, NULL,
21387 true /* Emit name attribute. */,
21388 context_die);
21389 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
21390 && link == first_parm_type)
21392 add_AT_flag (parm_die, DW_AT_artificial, 1);
21393 if (dwarf_version >= 3 || !dwarf_strict)
21394 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
21396 else if (arg && DECL_ARTIFICIAL (arg))
21397 add_AT_flag (parm_die, DW_AT_artificial, 1);
21400 link = TREE_CHAIN (link);
21401 if (arg)
21402 arg = DECL_CHAIN (arg);
21405 /* If this function type has an ellipsis, add a
21406 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
21407 if (formal_type != void_type_node)
21408 gen_unspecified_parameters_die (function_or_method_type, context_die);
21410 /* Make our second (and final) pass over the list of formal parameter types
21411 and output DIEs to represent those types (as necessary). */
21412 for (link = TYPE_ARG_TYPES (function_or_method_type);
21413 link && TREE_VALUE (link);
21414 link = TREE_CHAIN (link))
21415 gen_type_die (TREE_VALUE (link), context_die);
21418 /* We want to generate the DIE for TYPE so that we can generate the
21419 die for MEMBER, which has been defined; we will need to refer back
21420 to the member declaration nested within TYPE. If we're trying to
21421 generate minimal debug info for TYPE, processing TYPE won't do the
21422 trick; we need to attach the member declaration by hand. */
21424 static void
21425 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
21427 gen_type_die (type, context_die);
21429 /* If we're trying to avoid duplicate debug info, we may not have
21430 emitted the member decl for this function. Emit it now. */
21431 if (TYPE_STUB_DECL (type)
21432 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
21433 && ! lookup_decl_die (member))
21435 dw_die_ref type_die;
21436 gcc_assert (!decl_ultimate_origin (member));
21438 push_decl_scope (type);
21439 type_die = lookup_type_die_strip_naming_typedef (type);
21440 if (TREE_CODE (member) == FUNCTION_DECL)
21441 gen_subprogram_die (member, type_die);
21442 else if (TREE_CODE (member) == FIELD_DECL)
21444 /* Ignore the nameless fields that are used to skip bits but handle
21445 C++ anonymous unions and structs. */
21446 if (DECL_NAME (member) != NULL_TREE
21447 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
21448 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
21450 struct vlr_context vlr_ctx = {
21451 DECL_CONTEXT (member), /* struct_type */
21452 NULL_TREE /* variant_part_offset */
21454 gen_type_die (member_declared_type (member), type_die);
21455 gen_field_die (member, &vlr_ctx, type_die);
21458 else
21459 gen_variable_die (member, NULL_TREE, type_die);
21461 pop_decl_scope ();
21465 /* Forward declare these functions, because they are mutually recursive
21466 with their set_block_* pairing functions. */
21467 static void set_decl_origin_self (tree);
21468 static void set_decl_abstract_flags (tree, vec<tree> &);
21470 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
21471 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
21472 that it points to the node itself, thus indicating that the node is its
21473 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
21474 the given node is NULL, recursively descend the decl/block tree which
21475 it is the root of, and for each other ..._DECL or BLOCK node contained
21476 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
21477 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
21478 values to point to themselves. */
21480 static void
21481 set_block_origin_self (tree stmt)
21483 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
21485 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
21488 tree local_decl;
21490 for (local_decl = BLOCK_VARS (stmt);
21491 local_decl != NULL_TREE;
21492 local_decl = DECL_CHAIN (local_decl))
21493 /* Do not recurse on nested functions since the inlining status
21494 of parent and child can be different as per the DWARF spec. */
21495 if (TREE_CODE (local_decl) != FUNCTION_DECL
21496 && !DECL_EXTERNAL (local_decl))
21497 set_decl_origin_self (local_decl);
21501 tree subblock;
21503 for (subblock = BLOCK_SUBBLOCKS (stmt);
21504 subblock != NULL_TREE;
21505 subblock = BLOCK_CHAIN (subblock))
21506 set_block_origin_self (subblock); /* Recurse. */
21511 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
21512 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
21513 node to so that it points to the node itself, thus indicating that the
21514 node represents its own (abstract) origin. Additionally, if the
21515 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
21516 the decl/block tree of which the given node is the root of, and for
21517 each other ..._DECL or BLOCK node contained therein whose
21518 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
21519 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
21520 point to themselves. */
21522 static void
21523 set_decl_origin_self (tree decl)
21525 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
21527 DECL_ABSTRACT_ORIGIN (decl) = decl;
21528 if (TREE_CODE (decl) == FUNCTION_DECL)
21530 tree arg;
21532 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
21533 DECL_ABSTRACT_ORIGIN (arg) = arg;
21534 if (DECL_INITIAL (decl) != NULL_TREE
21535 && DECL_INITIAL (decl) != error_mark_node)
21536 set_block_origin_self (DECL_INITIAL (decl));
21541 /* Given a pointer to some BLOCK node, set the BLOCK_ABSTRACT flag to 1
21542 and if it wasn't 1 before, push it to abstract_vec vector.
21543 For all local decls and all local sub-blocks (recursively) do it
21544 too. */
21546 static void
21547 set_block_abstract_flags (tree stmt, vec<tree> &abstract_vec)
21549 tree local_decl;
21550 tree subblock;
21551 unsigned int i;
21553 if (!BLOCK_ABSTRACT (stmt))
21555 abstract_vec.safe_push (stmt);
21556 BLOCK_ABSTRACT (stmt) = 1;
21559 for (local_decl = BLOCK_VARS (stmt);
21560 local_decl != NULL_TREE;
21561 local_decl = DECL_CHAIN (local_decl))
21562 if (! DECL_EXTERNAL (local_decl))
21563 set_decl_abstract_flags (local_decl, abstract_vec);
21565 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
21567 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
21568 if ((VAR_P (local_decl) && !TREE_STATIC (local_decl))
21569 || TREE_CODE (local_decl) == PARM_DECL)
21570 set_decl_abstract_flags (local_decl, abstract_vec);
21573 for (subblock = BLOCK_SUBBLOCKS (stmt);
21574 subblock != NULL_TREE;
21575 subblock = BLOCK_CHAIN (subblock))
21576 set_block_abstract_flags (subblock, abstract_vec);
21579 /* Given a pointer to some ..._DECL node, set DECL_ABSTRACT_P flag on it
21580 to 1 and if it wasn't 1 before, push to abstract_vec vector.
21581 In the case where the decl is a FUNCTION_DECL also set the abstract
21582 flags for all of the parameters, local vars, local
21583 blocks and sub-blocks (recursively). */
21585 static void
21586 set_decl_abstract_flags (tree decl, vec<tree> &abstract_vec)
21588 if (!DECL_ABSTRACT_P (decl))
21590 abstract_vec.safe_push (decl);
21591 DECL_ABSTRACT_P (decl) = 1;
21594 if (TREE_CODE (decl) == FUNCTION_DECL)
21596 tree arg;
21598 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
21599 if (!DECL_ABSTRACT_P (arg))
21601 abstract_vec.safe_push (arg);
21602 DECL_ABSTRACT_P (arg) = 1;
21604 if (DECL_INITIAL (decl) != NULL_TREE
21605 && DECL_INITIAL (decl) != error_mark_node)
21606 set_block_abstract_flags (DECL_INITIAL (decl), abstract_vec);
21610 /* Generate the DWARF2 info for the "abstract" instance of a function which we
21611 may later generate inlined and/or out-of-line instances of.
21613 FIXME: In the early-dwarf world, this function, and most of the
21614 DECL_ABSTRACT code should be obsoleted. The early DIE _is_
21615 the abstract instance. All we would need to do is annotate
21616 the early DIE with the appropriate DW_AT_inline in late
21617 dwarf (perhaps in gen_inlined_subroutine_die).
21619 However, we can't do this yet, because LTO streaming of DIEs
21620 has not been implemented yet. */
21622 static void
21623 dwarf2out_abstract_function (tree decl)
21625 dw_die_ref old_die;
21626 tree save_fn;
21627 tree context;
21628 hash_table<decl_loc_hasher> *old_decl_loc_table;
21629 hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
21630 int old_call_site_count, old_tail_call_site_count;
21631 struct call_arg_loc_node *old_call_arg_locations;
21633 /* Make sure we have the actual abstract inline, not a clone. */
21634 decl = DECL_ORIGIN (decl);
21636 old_die = lookup_decl_die (decl);
21637 if (old_die && get_AT (old_die, DW_AT_inline))
21638 /* We've already generated the abstract instance. */
21639 return;
21641 /* We can be called while recursively when seeing block defining inlined subroutine
21642 DIE. Be sure to not clobber the outer location table nor use it or we would
21643 get locations in abstract instantces. */
21644 old_decl_loc_table = decl_loc_table;
21645 decl_loc_table = NULL;
21646 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
21647 cached_dw_loc_list_table = NULL;
21648 old_call_arg_locations = call_arg_locations;
21649 call_arg_locations = NULL;
21650 old_call_site_count = call_site_count;
21651 call_site_count = -1;
21652 old_tail_call_site_count = tail_call_site_count;
21653 tail_call_site_count = -1;
21655 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
21656 we don't get confused by DECL_ABSTRACT_P. */
21657 if (debug_info_level > DINFO_LEVEL_TERSE)
21659 context = decl_class_context (decl);
21660 if (context)
21661 gen_type_die_for_member
21662 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
21665 /* Pretend we've just finished compiling this function. */
21666 save_fn = current_function_decl;
21667 current_function_decl = decl;
21669 auto_vec<tree, 64> abstract_vec;
21670 set_decl_abstract_flags (decl, abstract_vec);
21671 dwarf2out_decl (decl);
21672 unsigned int i;
21673 tree t;
21674 FOR_EACH_VEC_ELT (abstract_vec, i, t)
21675 if (TREE_CODE (t) == BLOCK)
21676 BLOCK_ABSTRACT (t) = 0;
21677 else
21678 DECL_ABSTRACT_P (t) = 0;
21680 current_function_decl = save_fn;
21681 decl_loc_table = old_decl_loc_table;
21682 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
21683 call_arg_locations = old_call_arg_locations;
21684 call_site_count = old_call_site_count;
21685 tail_call_site_count = old_tail_call_site_count;
21688 /* Helper function of premark_used_types() which gets called through
21689 htab_traverse.
21691 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21692 marked as unused by prune_unused_types. */
21694 bool
21695 premark_used_types_helper (tree const &type, void *)
21697 dw_die_ref die;
21699 die = lookup_type_die (type);
21700 if (die != NULL)
21701 die->die_perennial_p = 1;
21702 return true;
21705 /* Helper function of premark_types_used_by_global_vars which gets called
21706 through htab_traverse.
21708 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21709 marked as unused by prune_unused_types. The DIE of the type is marked
21710 only if the global variable using the type will actually be emitted. */
21713 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
21714 void *)
21716 struct types_used_by_vars_entry *entry;
21717 dw_die_ref die;
21719 entry = (struct types_used_by_vars_entry *) *slot;
21720 gcc_assert (entry->type != NULL
21721 && entry->var_decl != NULL);
21722 die = lookup_type_die (entry->type);
21723 if (die)
21725 /* Ask cgraph if the global variable really is to be emitted.
21726 If yes, then we'll keep the DIE of ENTRY->TYPE. */
21727 varpool_node *node = varpool_node::get (entry->var_decl);
21728 if (node && node->definition)
21730 die->die_perennial_p = 1;
21731 /* Keep the parent DIEs as well. */
21732 while ((die = die->die_parent) && die->die_perennial_p == 0)
21733 die->die_perennial_p = 1;
21736 return 1;
21739 /* Mark all members of used_types_hash as perennial. */
21741 static void
21742 premark_used_types (struct function *fun)
21744 if (fun && fun->used_types_hash)
21745 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
21748 /* Mark all members of types_used_by_vars_entry as perennial. */
21750 static void
21751 premark_types_used_by_global_vars (void)
21753 if (types_used_by_vars_hash)
21754 types_used_by_vars_hash
21755 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
21758 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
21759 for CA_LOC call arg loc node. */
21761 static dw_die_ref
21762 gen_call_site_die (tree decl, dw_die_ref subr_die,
21763 struct call_arg_loc_node *ca_loc)
21765 dw_die_ref stmt_die = NULL, die;
21766 tree block = ca_loc->block;
21768 while (block
21769 && block != DECL_INITIAL (decl)
21770 && TREE_CODE (block) == BLOCK)
21772 stmt_die = BLOCK_DIE (block);
21773 if (stmt_die)
21774 break;
21775 block = BLOCK_SUPERCONTEXT (block);
21777 if (stmt_die == NULL)
21778 stmt_die = subr_die;
21779 die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
21780 add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc), ca_loc->label);
21781 if (ca_loc->tail_call_p)
21782 add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
21783 if (ca_loc->symbol_ref)
21785 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
21786 if (tdie)
21787 add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
21788 else
21789 add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
21790 false);
21792 return die;
21795 /* Generate a DIE to represent a declared function (either file-scope or
21796 block-local). */
21798 static void
21799 gen_subprogram_die (tree decl, dw_die_ref context_die)
21801 tree origin = decl_ultimate_origin (decl);
21802 dw_die_ref subr_die;
21803 dw_die_ref old_die = lookup_decl_die (decl);
21805 /* This function gets called multiple times for different stages of
21806 the debug process. For example, for func() in this code:
21808 namespace S
21810 void func() { ... }
21813 ...we get called 4 times. Twice in early debug and twice in
21814 late debug:
21816 Early debug
21817 -----------
21819 1. Once while generating func() within the namespace. This is
21820 the declaration. The declaration bit below is set, as the
21821 context is the namespace.
21823 A new DIE will be generated with DW_AT_declaration set.
21825 2. Once for func() itself. This is the specification. The
21826 declaration bit below is clear as the context is the CU.
21828 We will use the cached DIE from (1) to create a new DIE with
21829 DW_AT_specification pointing to the declaration in (1).
21831 Late debug via rest_of_handle_final()
21832 -------------------------------------
21834 3. Once generating func() within the namespace. This is also the
21835 declaration, as in (1), but this time we will early exit below
21836 as we have a cached DIE and a declaration needs no additional
21837 annotations (no locations), as the source declaration line
21838 info is enough.
21840 4. Once for func() itself. As in (2), this is the specification,
21841 but this time we will re-use the cached DIE, and just annotate
21842 it with the location information that should now be available.
21844 For something without namespaces, but with abstract instances, we
21845 are also called a multiple times:
21847 class Base
21849 public:
21850 Base (); // constructor declaration (1)
21853 Base::Base () { } // constructor specification (2)
21855 Early debug
21856 -----------
21858 1. Once for the Base() constructor by virtue of it being a
21859 member of the Base class. This is done via
21860 rest_of_type_compilation.
21862 This is a declaration, so a new DIE will be created with
21863 DW_AT_declaration.
21865 2. Once for the Base() constructor definition, but this time
21866 while generating the abstract instance of the base
21867 constructor (__base_ctor) which is being generated via early
21868 debug of reachable functions.
21870 Even though we have a cached version of the declaration (1),
21871 we will create a DW_AT_specification of the declaration DIE
21872 in (1).
21874 3. Once for the __base_ctor itself, but this time, we generate
21875 an DW_AT_abstract_origin version of the DW_AT_specification in
21876 (2).
21878 Late debug via rest_of_handle_final
21879 -----------------------------------
21881 4. One final time for the __base_ctor (which will have a cached
21882 DIE with DW_AT_abstract_origin created in (3). This time,
21883 we will just annotate the location information now
21884 available.
21886 int declaration = (current_function_decl != decl
21887 || class_or_namespace_scope_p (context_die));
21889 /* Now that the C++ front end lazily declares artificial member fns, we
21890 might need to retrofit the declaration into its class. */
21891 if (!declaration && !origin && !old_die
21892 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
21893 && !class_or_namespace_scope_p (context_die)
21894 && debug_info_level > DINFO_LEVEL_TERSE)
21895 old_die = force_decl_die (decl);
21897 /* An inlined instance, tag a new DIE with DW_AT_abstract_origin. */
21898 if (origin != NULL)
21900 gcc_assert (!declaration || local_scope_p (context_die));
21902 /* Fixup die_parent for the abstract instance of a nested
21903 inline function. */
21904 if (old_die && old_die->die_parent == NULL)
21905 add_child_die (context_die, old_die);
21907 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
21909 /* If we have a DW_AT_abstract_origin we have a working
21910 cached version. */
21911 subr_die = old_die;
21913 else
21915 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
21916 add_abstract_origin_attribute (subr_die, origin);
21917 /* This is where the actual code for a cloned function is.
21918 Let's emit linkage name attribute for it. This helps
21919 debuggers to e.g, set breakpoints into
21920 constructors/destructors when the user asks "break
21921 K::K". */
21922 add_linkage_name (subr_die, decl);
21925 /* A cached copy, possibly from early dwarf generation. Reuse as
21926 much as possible. */
21927 else if (old_die)
21929 /* A declaration that has been previously dumped needs no
21930 additional information. */
21931 if (declaration)
21932 return;
21934 if (!get_AT_flag (old_die, DW_AT_declaration)
21935 /* We can have a normal definition following an inline one in the
21936 case of redefinition of GNU C extern inlines.
21937 It seems reasonable to use AT_specification in this case. */
21938 && !get_AT (old_die, DW_AT_inline))
21940 /* Detect and ignore this case, where we are trying to output
21941 something we have already output. */
21942 if (get_AT (old_die, DW_AT_low_pc)
21943 || get_AT (old_die, DW_AT_ranges))
21944 return;
21946 /* If we have no location information, this must be a
21947 partially generated DIE from early dwarf generation.
21948 Fall through and generate it. */
21951 /* If the definition comes from the same place as the declaration,
21952 maybe use the old DIE. We always want the DIE for this function
21953 that has the *_pc attributes to be under comp_unit_die so the
21954 debugger can find it. We also need to do this for abstract
21955 instances of inlines, since the spec requires the out-of-line copy
21956 to have the same parent. For local class methods, this doesn't
21957 apply; we just use the old DIE. */
21958 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
21959 struct dwarf_file_data * file_index = lookup_filename (s.file);
21960 if ((is_cu_die (old_die->die_parent)
21961 /* This condition fixes the inconsistency/ICE with the
21962 following Fortran test (or some derivative thereof) while
21963 building libgfortran:
21965 module some_m
21966 contains
21967 logical function funky (FLAG)
21968 funky = .true.
21969 end function
21970 end module
21972 || (old_die->die_parent
21973 && old_die->die_parent->die_tag == DW_TAG_module)
21974 || context_die == NULL)
21975 && (DECL_ARTIFICIAL (decl)
21976 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
21977 && (get_AT_unsigned (old_die, DW_AT_decl_line)
21978 == (unsigned) s.line)
21979 && (!debug_column_info
21980 || s.column == 0
21981 || (get_AT_unsigned (old_die, DW_AT_decl_column)
21982 == (unsigned) s.column)))))
21984 subr_die = old_die;
21986 /* Clear out the declaration attribute, but leave the
21987 parameters so they can be augmented with location
21988 information later. Unless this was a declaration, in
21989 which case, wipe out the nameless parameters and recreate
21990 them further down. */
21991 if (remove_AT (subr_die, DW_AT_declaration))
21994 remove_AT (subr_die, DW_AT_object_pointer);
21995 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
21998 /* Make a specification pointing to the previously built
21999 declaration. */
22000 else
22002 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22003 add_AT_specification (subr_die, old_die);
22004 add_pubname (decl, subr_die);
22005 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
22006 add_AT_file (subr_die, DW_AT_decl_file, file_index);
22007 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
22008 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
22009 if (debug_column_info
22010 && s.column
22011 && (get_AT_unsigned (old_die, DW_AT_decl_column)
22012 != (unsigned) s.column))
22013 add_AT_unsigned (subr_die, DW_AT_decl_column, s.column);
22015 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
22016 emit the real type on the definition die. */
22017 if (is_cxx () && debug_info_level > DINFO_LEVEL_TERSE)
22019 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
22020 if (die == auto_die || die == decltype_auto_die)
22021 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22022 TYPE_UNQUALIFIED, false, context_die);
22025 /* When we process the method declaration, we haven't seen
22026 the out-of-class defaulted definition yet, so we have to
22027 recheck now. */
22028 if ((dwarf_version >= 5 || ! dwarf_strict)
22029 && !get_AT (subr_die, DW_AT_defaulted))
22031 int defaulted
22032 = lang_hooks.decls.decl_dwarf_attribute (decl,
22033 DW_AT_defaulted);
22034 if (defaulted != -1)
22036 /* Other values must have been handled before. */
22037 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
22038 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22043 /* Create a fresh DIE for anything else. */
22044 else
22046 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22048 if (TREE_PUBLIC (decl))
22049 add_AT_flag (subr_die, DW_AT_external, 1);
22051 add_name_and_src_coords_attributes (subr_die, decl);
22052 add_pubname (decl, subr_die);
22053 if (debug_info_level > DINFO_LEVEL_TERSE)
22055 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
22056 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22057 TYPE_UNQUALIFIED, false, context_die);
22060 add_pure_or_virtual_attribute (subr_die, decl);
22061 if (DECL_ARTIFICIAL (decl))
22062 add_AT_flag (subr_die, DW_AT_artificial, 1);
22064 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
22065 add_AT_flag (subr_die, DW_AT_noreturn, 1);
22067 add_alignment_attribute (subr_die, decl);
22069 add_accessibility_attribute (subr_die, decl);
22072 /* Unless we have an existing non-declaration DIE, equate the new
22073 DIE. */
22074 if (!old_die || is_declaration_die (old_die))
22075 equate_decl_number_to_die (decl, subr_die);
22077 if (declaration)
22079 if (!old_die || !get_AT (old_die, DW_AT_inline))
22081 add_AT_flag (subr_die, DW_AT_declaration, 1);
22083 /* If this is an explicit function declaration then generate
22084 a DW_AT_explicit attribute. */
22085 if ((dwarf_version >= 3 || !dwarf_strict)
22086 && lang_hooks.decls.decl_dwarf_attribute (decl,
22087 DW_AT_explicit) == 1)
22088 add_AT_flag (subr_die, DW_AT_explicit, 1);
22090 /* If this is a C++11 deleted special function member then generate
22091 a DW_AT_deleted attribute. */
22092 if ((dwarf_version >= 5 || !dwarf_strict)
22093 && lang_hooks.decls.decl_dwarf_attribute (decl,
22094 DW_AT_deleted) == 1)
22095 add_AT_flag (subr_die, DW_AT_deleted, 1);
22097 /* If this is a C++11 defaulted special function member then
22098 generate a DW_AT_defaulted attribute. */
22099 if (dwarf_version >= 5 || !dwarf_strict)
22101 int defaulted
22102 = lang_hooks.decls.decl_dwarf_attribute (decl,
22103 DW_AT_defaulted);
22104 if (defaulted != -1)
22105 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22108 /* If this is a C++11 non-static member function with & ref-qualifier
22109 then generate a DW_AT_reference attribute. */
22110 if ((dwarf_version >= 5 || !dwarf_strict)
22111 && lang_hooks.decls.decl_dwarf_attribute (decl,
22112 DW_AT_reference) == 1)
22113 add_AT_flag (subr_die, DW_AT_reference, 1);
22115 /* If this is a C++11 non-static member function with &&
22116 ref-qualifier then generate a DW_AT_reference attribute. */
22117 if ((dwarf_version >= 5 || !dwarf_strict)
22118 && lang_hooks.decls.decl_dwarf_attribute (decl,
22119 DW_AT_rvalue_reference)
22120 == 1)
22121 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
22124 /* Tag abstract instances with DW_AT_inline. */
22125 else if (DECL_ABSTRACT_P (decl))
22127 if (DECL_DECLARED_INLINE_P (decl))
22129 if (cgraph_function_possibly_inlined_p (decl))
22130 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
22131 else
22132 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
22134 else
22136 if (cgraph_function_possibly_inlined_p (decl))
22137 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
22138 else
22139 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
22142 if (DECL_DECLARED_INLINE_P (decl)
22143 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
22144 add_AT_flag (subr_die, DW_AT_artificial, 1);
22146 /* For non DECL_EXTERNALs, if range information is available, fill
22147 the DIE with it. */
22148 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
22150 HOST_WIDE_INT cfa_fb_offset;
22152 struct function *fun = DECL_STRUCT_FUNCTION (decl);
22154 if (!crtl->has_bb_partition)
22156 dw_fde_ref fde = fun->fde;
22157 if (fde->dw_fde_begin)
22159 /* We have already generated the labels. */
22160 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22161 fde->dw_fde_end, false);
22163 else
22165 /* Create start/end labels and add the range. */
22166 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
22167 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
22168 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
22169 current_function_funcdef_no);
22170 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
22171 current_function_funcdef_no);
22172 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
22173 false);
22176 #if VMS_DEBUGGING_INFO
22177 /* HP OpenVMS Industry Standard 64: DWARF Extensions
22178 Section 2.3 Prologue and Epilogue Attributes:
22179 When a breakpoint is set on entry to a function, it is generally
22180 desirable for execution to be suspended, not on the very first
22181 instruction of the function, but rather at a point after the
22182 function's frame has been set up, after any language defined local
22183 declaration processing has been completed, and before execution of
22184 the first statement of the function begins. Debuggers generally
22185 cannot properly determine where this point is. Similarly for a
22186 breakpoint set on exit from a function. The prologue and epilogue
22187 attributes allow a compiler to communicate the location(s) to use. */
22190 if (fde->dw_fde_vms_end_prologue)
22191 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
22192 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
22194 if (fde->dw_fde_vms_begin_epilogue)
22195 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
22196 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
22198 #endif
22201 else
22203 /* Generate pubnames entries for the split function code ranges. */
22204 dw_fde_ref fde = fun->fde;
22206 if (fde->dw_fde_second_begin)
22208 if (dwarf_version >= 3 || !dwarf_strict)
22210 /* We should use ranges for non-contiguous code section
22211 addresses. Use the actual code range for the initial
22212 section, since the HOT/COLD labels might precede an
22213 alignment offset. */
22214 bool range_list_added = false;
22215 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
22216 fde->dw_fde_end, &range_list_added,
22217 false);
22218 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
22219 fde->dw_fde_second_end,
22220 &range_list_added, false);
22221 if (range_list_added)
22222 add_ranges (NULL);
22224 else
22226 /* There is no real support in DW2 for this .. so we make
22227 a work-around. First, emit the pub name for the segment
22228 containing the function label. Then make and emit a
22229 simplified subprogram DIE for the second segment with the
22230 name pre-fixed by __hot/cold_sect_of_. We use the same
22231 linkage name for the second die so that gdb will find both
22232 sections when given "b foo". */
22233 const char *name = NULL;
22234 tree decl_name = DECL_NAME (decl);
22235 dw_die_ref seg_die;
22237 /* Do the 'primary' section. */
22238 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22239 fde->dw_fde_end, false);
22241 /* Build a minimal DIE for the secondary section. */
22242 seg_die = new_die (DW_TAG_subprogram,
22243 subr_die->die_parent, decl);
22245 if (TREE_PUBLIC (decl))
22246 add_AT_flag (seg_die, DW_AT_external, 1);
22248 if (decl_name != NULL
22249 && IDENTIFIER_POINTER (decl_name) != NULL)
22251 name = dwarf2_name (decl, 1);
22252 if (! DECL_ARTIFICIAL (decl))
22253 add_src_coords_attributes (seg_die, decl);
22255 add_linkage_name (seg_die, decl);
22257 gcc_assert (name != NULL);
22258 add_pure_or_virtual_attribute (seg_die, decl);
22259 if (DECL_ARTIFICIAL (decl))
22260 add_AT_flag (seg_die, DW_AT_artificial, 1);
22262 name = concat ("__second_sect_of_", name, NULL);
22263 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
22264 fde->dw_fde_second_end, false);
22265 add_name_attribute (seg_die, name);
22266 if (want_pubnames ())
22267 add_pubname_string (name, seg_die);
22270 else
22271 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
22272 false);
22275 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
22277 /* We define the "frame base" as the function's CFA. This is more
22278 convenient for several reasons: (1) It's stable across the prologue
22279 and epilogue, which makes it better than just a frame pointer,
22280 (2) With dwarf3, there exists a one-byte encoding that allows us
22281 to reference the .debug_frame data by proxy, but failing that,
22282 (3) We can at least reuse the code inspection and interpretation
22283 code that determines the CFA position at various points in the
22284 function. */
22285 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
22287 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
22288 add_AT_loc (subr_die, DW_AT_frame_base, op);
22290 else
22292 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
22293 if (list->dw_loc_next)
22294 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
22295 else
22296 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
22299 /* Compute a displacement from the "steady-state frame pointer" to
22300 the CFA. The former is what all stack slots and argument slots
22301 will reference in the rtl; the latter is what we've told the
22302 debugger about. We'll need to adjust all frame_base references
22303 by this displacement. */
22304 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
22306 if (fun->static_chain_decl)
22308 /* DWARF requires here a location expression that computes the
22309 address of the enclosing subprogram's frame base. The machinery
22310 in tree-nested.c is supposed to store this specific address in the
22311 last field of the FRAME record. */
22312 const tree frame_type
22313 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
22314 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
22316 tree fb_expr
22317 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
22318 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
22319 fb_expr, fb_decl, NULL_TREE);
22321 add_AT_location_description (subr_die, DW_AT_static_link,
22322 loc_list_from_tree (fb_expr, 0, NULL));
22325 resolve_variable_values ();
22328 /* Generate child dies for template paramaters. */
22329 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
22330 gen_generic_params_dies (decl);
22332 /* Now output descriptions of the arguments for this function. This gets
22333 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
22334 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
22335 `...' at the end of the formal parameter list. In order to find out if
22336 there was a trailing ellipsis or not, we must instead look at the type
22337 associated with the FUNCTION_DECL. This will be a node of type
22338 FUNCTION_TYPE. If the chain of type nodes hanging off of this
22339 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
22340 an ellipsis at the end. */
22342 /* In the case where we are describing a mere function declaration, all we
22343 need to do here (and all we *can* do here) is to describe the *types* of
22344 its formal parameters. */
22345 if (debug_info_level <= DINFO_LEVEL_TERSE)
22347 else if (declaration)
22348 gen_formal_types_die (decl, subr_die);
22349 else
22351 /* Generate DIEs to represent all known formal parameters. */
22352 tree parm = DECL_ARGUMENTS (decl);
22353 tree generic_decl = early_dwarf
22354 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
22355 tree generic_decl_parm = generic_decl
22356 ? DECL_ARGUMENTS (generic_decl)
22357 : NULL;
22359 /* Now we want to walk the list of parameters of the function and
22360 emit their relevant DIEs.
22362 We consider the case of DECL being an instance of a generic function
22363 as well as it being a normal function.
22365 If DECL is an instance of a generic function we walk the
22366 parameters of the generic function declaration _and_ the parameters of
22367 DECL itself. This is useful because we want to emit specific DIEs for
22368 function parameter packs and those are declared as part of the
22369 generic function declaration. In that particular case,
22370 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
22371 That DIE has children DIEs representing the set of arguments
22372 of the pack. Note that the set of pack arguments can be empty.
22373 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
22374 children DIE.
22376 Otherwise, we just consider the parameters of DECL. */
22377 while (generic_decl_parm || parm)
22379 if (generic_decl_parm
22380 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
22381 gen_formal_parameter_pack_die (generic_decl_parm,
22382 parm, subr_die,
22383 &parm);
22384 else if (parm && !POINTER_BOUNDS_P (parm))
22386 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
22388 if (parm == DECL_ARGUMENTS (decl)
22389 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
22390 && parm_die
22391 && (dwarf_version >= 3 || !dwarf_strict))
22392 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
22394 parm = DECL_CHAIN (parm);
22396 else if (parm)
22397 parm = DECL_CHAIN (parm);
22399 if (generic_decl_parm)
22400 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
22403 /* Decide whether we need an unspecified_parameters DIE at the end.
22404 There are 2 more cases to do this for: 1) the ansi ... declaration -
22405 this is detectable when the end of the arg list is not a
22406 void_type_node 2) an unprototyped function declaration (not a
22407 definition). This just means that we have no info about the
22408 parameters at all. */
22409 if (early_dwarf)
22411 if (prototype_p (TREE_TYPE (decl)))
22413 /* This is the prototyped case, check for.... */
22414 if (stdarg_p (TREE_TYPE (decl)))
22415 gen_unspecified_parameters_die (decl, subr_die);
22417 else if (DECL_INITIAL (decl) == NULL_TREE)
22418 gen_unspecified_parameters_die (decl, subr_die);
22422 if (subr_die != old_die)
22423 /* Add the calling convention attribute if requested. */
22424 add_calling_convention_attribute (subr_die, decl);
22426 /* Output Dwarf info for all of the stuff within the body of the function
22427 (if it has one - it may be just a declaration).
22429 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
22430 a function. This BLOCK actually represents the outermost binding contour
22431 for the function, i.e. the contour in which the function's formal
22432 parameters and labels get declared. Curiously, it appears that the front
22433 end doesn't actually put the PARM_DECL nodes for the current function onto
22434 the BLOCK_VARS list for this outer scope, but are strung off of the
22435 DECL_ARGUMENTS list for the function instead.
22437 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
22438 the LABEL_DECL nodes for the function however, and we output DWARF info
22439 for those in decls_for_scope. Just within the `outer_scope' there will be
22440 a BLOCK node representing the function's outermost pair of curly braces,
22441 and any blocks used for the base and member initializers of a C++
22442 constructor function. */
22443 tree outer_scope = DECL_INITIAL (decl);
22444 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
22446 int call_site_note_count = 0;
22447 int tail_call_site_note_count = 0;
22449 /* Emit a DW_TAG_variable DIE for a named return value. */
22450 if (DECL_NAME (DECL_RESULT (decl)))
22451 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
22453 /* The first time through decls_for_scope we will generate the
22454 DIEs for the locals. The second time, we fill in the
22455 location info. */
22456 decls_for_scope (outer_scope, subr_die);
22458 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
22460 struct call_arg_loc_node *ca_loc;
22461 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
22463 dw_die_ref die = NULL;
22464 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
22465 rtx arg, next_arg;
22467 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
22468 ? NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note)
22469 : NULL_RTX);
22470 arg; arg = next_arg)
22472 dw_loc_descr_ref reg, val;
22473 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
22474 dw_die_ref cdie, tdie = NULL;
22476 next_arg = XEXP (arg, 1);
22477 if (REG_P (XEXP (XEXP (arg, 0), 0))
22478 && next_arg
22479 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
22480 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
22481 && REGNO (XEXP (XEXP (arg, 0), 0))
22482 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
22483 next_arg = XEXP (next_arg, 1);
22484 if (mode == VOIDmode)
22486 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
22487 if (mode == VOIDmode)
22488 mode = GET_MODE (XEXP (arg, 0));
22490 if (mode == VOIDmode || mode == BLKmode)
22491 continue;
22492 /* Get dynamic information about call target only if we
22493 have no static information: we cannot generate both
22494 DW_AT_call_origin and DW_AT_call_target
22495 attributes. */
22496 if (ca_loc->symbol_ref == NULL_RTX)
22498 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
22500 tloc = XEXP (XEXP (arg, 0), 1);
22501 continue;
22503 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
22504 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
22506 tlocc = XEXP (XEXP (arg, 0), 1);
22507 continue;
22510 reg = NULL;
22511 if (REG_P (XEXP (XEXP (arg, 0), 0)))
22512 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
22513 VAR_INIT_STATUS_INITIALIZED);
22514 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
22516 rtx mem = XEXP (XEXP (arg, 0), 0);
22517 reg = mem_loc_descriptor (XEXP (mem, 0),
22518 get_address_mode (mem),
22519 GET_MODE (mem),
22520 VAR_INIT_STATUS_INITIALIZED);
22522 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
22523 == DEBUG_PARAMETER_REF)
22525 tree tdecl
22526 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
22527 tdie = lookup_decl_die (tdecl);
22528 if (tdie == NULL)
22529 continue;
22531 else
22532 continue;
22533 if (reg == NULL
22534 && GET_CODE (XEXP (XEXP (arg, 0), 0))
22535 != DEBUG_PARAMETER_REF)
22536 continue;
22537 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
22538 VOIDmode,
22539 VAR_INIT_STATUS_INITIALIZED);
22540 if (val == NULL)
22541 continue;
22542 if (die == NULL)
22543 die = gen_call_site_die (decl, subr_die, ca_loc);
22544 cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
22545 NULL_TREE);
22546 if (reg != NULL)
22547 add_AT_loc (cdie, DW_AT_location, reg);
22548 else if (tdie != NULL)
22549 add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
22550 tdie);
22551 add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
22552 if (next_arg != XEXP (arg, 1))
22554 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
22555 if (mode == VOIDmode)
22556 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
22557 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
22558 0), 1),
22559 mode, VOIDmode,
22560 VAR_INIT_STATUS_INITIALIZED);
22561 if (val != NULL)
22562 add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
22563 val);
22566 if (die == NULL
22567 && (ca_loc->symbol_ref || tloc))
22568 die = gen_call_site_die (decl, subr_die, ca_loc);
22569 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
22571 dw_loc_descr_ref tval = NULL;
22573 if (tloc != NULL_RTX)
22574 tval = mem_loc_descriptor (tloc,
22575 GET_MODE (tloc) == VOIDmode
22576 ? Pmode : GET_MODE (tloc),
22577 VOIDmode,
22578 VAR_INIT_STATUS_INITIALIZED);
22579 if (tval)
22580 add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
22581 else if (tlocc != NULL_RTX)
22583 tval = mem_loc_descriptor (tlocc,
22584 GET_MODE (tlocc) == VOIDmode
22585 ? Pmode : GET_MODE (tlocc),
22586 VOIDmode,
22587 VAR_INIT_STATUS_INITIALIZED);
22588 if (tval)
22589 add_AT_loc (die,
22590 dwarf_AT (DW_AT_call_target_clobbered),
22591 tval);
22594 if (die != NULL)
22596 call_site_note_count++;
22597 if (ca_loc->tail_call_p)
22598 tail_call_site_note_count++;
22602 call_arg_locations = NULL;
22603 call_arg_loc_last = NULL;
22604 if (tail_call_site_count >= 0
22605 && tail_call_site_count == tail_call_site_note_count
22606 && (!dwarf_strict || dwarf_version >= 5))
22608 if (call_site_count >= 0
22609 && call_site_count == call_site_note_count)
22610 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
22611 else
22612 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
22614 call_site_count = -1;
22615 tail_call_site_count = -1;
22618 /* Mark used types after we have created DIEs for the functions scopes. */
22619 premark_used_types (DECL_STRUCT_FUNCTION (decl));
22622 /* Returns a hash value for X (which really is a die_struct). */
22624 hashval_t
22625 block_die_hasher::hash (die_struct *d)
22627 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
22630 /* Return nonzero if decl_id and die_parent of die_struct X is the same
22631 as decl_id and die_parent of die_struct Y. */
22633 bool
22634 block_die_hasher::equal (die_struct *x, die_struct *y)
22636 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
22639 /* Return TRUE if DECL, which may have been previously generated as
22640 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
22641 true if decl (or its origin) is either an extern declaration or a
22642 class/namespace scoped declaration.
22644 The declare_in_namespace support causes us to get two DIEs for one
22645 variable, both of which are declarations. We want to avoid
22646 considering one to be a specification, so we must test for
22647 DECLARATION and DW_AT_declaration. */
22648 static inline bool
22649 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
22651 return (old_die && TREE_STATIC (decl) && !declaration
22652 && get_AT_flag (old_die, DW_AT_declaration) == 1);
22655 /* Return true if DECL is a local static. */
22657 static inline bool
22658 local_function_static (tree decl)
22660 gcc_assert (VAR_P (decl));
22661 return TREE_STATIC (decl)
22662 && DECL_CONTEXT (decl)
22663 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
22666 /* Generate a DIE to represent a declared data object.
22667 Either DECL or ORIGIN must be non-null. */
22669 static void
22670 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
22672 HOST_WIDE_INT off = 0;
22673 tree com_decl;
22674 tree decl_or_origin = decl ? decl : origin;
22675 tree ultimate_origin;
22676 dw_die_ref var_die;
22677 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
22678 bool declaration = (DECL_EXTERNAL (decl_or_origin)
22679 || class_or_namespace_scope_p (context_die));
22680 bool specialization_p = false;
22681 bool no_linkage_name = false;
22683 /* While C++ inline static data members have definitions inside of the
22684 class, force the first DIE to be a declaration, then let gen_member_die
22685 reparent it to the class context and call gen_variable_die again
22686 to create the outside of the class DIE for the definition. */
22687 if (!declaration
22688 && old_die == NULL
22689 && decl
22690 && DECL_CONTEXT (decl)
22691 && TYPE_P (DECL_CONTEXT (decl))
22692 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
22694 declaration = true;
22695 if (dwarf_version < 5)
22696 no_linkage_name = true;
22699 ultimate_origin = decl_ultimate_origin (decl_or_origin);
22700 if (decl || ultimate_origin)
22701 origin = ultimate_origin;
22702 com_decl = fortran_common (decl_or_origin, &off);
22704 /* Symbol in common gets emitted as a child of the common block, in the form
22705 of a data member. */
22706 if (com_decl)
22708 dw_die_ref com_die;
22709 dw_loc_list_ref loc = NULL;
22710 die_node com_die_arg;
22712 var_die = lookup_decl_die (decl_or_origin);
22713 if (var_die)
22715 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
22717 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
22718 if (loc)
22720 if (off)
22722 /* Optimize the common case. */
22723 if (single_element_loc_list_p (loc)
22724 && loc->expr->dw_loc_opc == DW_OP_addr
22725 && loc->expr->dw_loc_next == NULL
22726 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
22727 == SYMBOL_REF)
22729 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22730 loc->expr->dw_loc_oprnd1.v.val_addr
22731 = plus_constant (GET_MODE (x), x , off);
22733 else
22734 loc_list_plus_const (loc, off);
22736 add_AT_location_description (var_die, DW_AT_location, loc);
22737 remove_AT (var_die, DW_AT_declaration);
22740 return;
22743 if (common_block_die_table == NULL)
22744 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
22746 com_die_arg.decl_id = DECL_UID (com_decl);
22747 com_die_arg.die_parent = context_die;
22748 com_die = common_block_die_table->find (&com_die_arg);
22749 if (! early_dwarf)
22750 loc = loc_list_from_tree (com_decl, 2, NULL);
22751 if (com_die == NULL)
22753 const char *cnam
22754 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
22755 die_node **slot;
22757 com_die = new_die (DW_TAG_common_block, context_die, decl);
22758 add_name_and_src_coords_attributes (com_die, com_decl);
22759 if (loc)
22761 add_AT_location_description (com_die, DW_AT_location, loc);
22762 /* Avoid sharing the same loc descriptor between
22763 DW_TAG_common_block and DW_TAG_variable. */
22764 loc = loc_list_from_tree (com_decl, 2, NULL);
22766 else if (DECL_EXTERNAL (decl_or_origin))
22767 add_AT_flag (com_die, DW_AT_declaration, 1);
22768 if (want_pubnames ())
22769 add_pubname_string (cnam, com_die); /* ??? needed? */
22770 com_die->decl_id = DECL_UID (com_decl);
22771 slot = common_block_die_table->find_slot (com_die, INSERT);
22772 *slot = com_die;
22774 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
22776 add_AT_location_description (com_die, DW_AT_location, loc);
22777 loc = loc_list_from_tree (com_decl, 2, NULL);
22778 remove_AT (com_die, DW_AT_declaration);
22780 var_die = new_die (DW_TAG_variable, com_die, decl);
22781 add_name_and_src_coords_attributes (var_die, decl_or_origin);
22782 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
22783 decl_quals (decl_or_origin), false,
22784 context_die);
22785 add_alignment_attribute (var_die, decl);
22786 add_AT_flag (var_die, DW_AT_external, 1);
22787 if (loc)
22789 if (off)
22791 /* Optimize the common case. */
22792 if (single_element_loc_list_p (loc)
22793 && loc->expr->dw_loc_opc == DW_OP_addr
22794 && loc->expr->dw_loc_next == NULL
22795 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
22797 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22798 loc->expr->dw_loc_oprnd1.v.val_addr
22799 = plus_constant (GET_MODE (x), x, off);
22801 else
22802 loc_list_plus_const (loc, off);
22804 add_AT_location_description (var_die, DW_AT_location, loc);
22806 else if (DECL_EXTERNAL (decl_or_origin))
22807 add_AT_flag (var_die, DW_AT_declaration, 1);
22808 if (decl)
22809 equate_decl_number_to_die (decl, var_die);
22810 return;
22813 if (old_die)
22815 if (declaration)
22817 /* A declaration that has been previously dumped, needs no
22818 further annotations, since it doesn't need location on
22819 the second pass. */
22820 return;
22822 else if (decl_will_get_specification_p (old_die, decl, declaration)
22823 && !get_AT (old_die, DW_AT_specification))
22825 /* Fall-thru so we can make a new variable die along with a
22826 DW_AT_specification. */
22828 else if (origin && old_die->die_parent != context_die)
22830 /* If we will be creating an inlined instance, we need a
22831 new DIE that will get annotated with
22832 DW_AT_abstract_origin. Clear things so we can get a
22833 new DIE. */
22834 gcc_assert (!DECL_ABSTRACT_P (decl));
22835 old_die = NULL;
22837 else
22839 /* If a DIE was dumped early, it still needs location info.
22840 Skip to where we fill the location bits. */
22841 var_die = old_die;
22842 goto gen_variable_die_location;
22846 /* For static data members, the declaration in the class is supposed
22847 to have DW_TAG_member tag in DWARF{3,4} and we emit it for compatibility
22848 also in DWARF2; the specification should still be DW_TAG_variable
22849 referencing the DW_TAG_member DIE. */
22850 if (declaration && class_scope_p (context_die) && dwarf_version < 5)
22851 var_die = new_die (DW_TAG_member, context_die, decl);
22852 else
22853 var_die = new_die (DW_TAG_variable, context_die, decl);
22855 if (origin != NULL)
22856 add_abstract_origin_attribute (var_die, origin);
22858 /* Loop unrolling can create multiple blocks that refer to the same
22859 static variable, so we must test for the DW_AT_declaration flag.
22861 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
22862 copy decls and set the DECL_ABSTRACT_P flag on them instead of
22863 sharing them.
22865 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
22866 else if (decl_will_get_specification_p (old_die, decl, declaration))
22868 /* This is a definition of a C++ class level static. */
22869 add_AT_specification (var_die, old_die);
22870 specialization_p = true;
22871 if (DECL_NAME (decl))
22873 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
22874 struct dwarf_file_data * file_index = lookup_filename (s.file);
22876 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
22877 add_AT_file (var_die, DW_AT_decl_file, file_index);
22879 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
22880 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
22882 if (debug_column_info
22883 && s.column
22884 && (get_AT_unsigned (old_die, DW_AT_decl_column)
22885 != (unsigned) s.column))
22886 add_AT_unsigned (var_die, DW_AT_decl_column, s.column);
22888 if (old_die->die_tag == DW_TAG_member)
22889 add_linkage_name (var_die, decl);
22892 else
22893 add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
22895 if ((origin == NULL && !specialization_p)
22896 || (origin != NULL
22897 && !DECL_ABSTRACT_P (decl_or_origin)
22898 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
22899 decl_function_context
22900 (decl_or_origin))))
22902 tree type = TREE_TYPE (decl_or_origin);
22904 if (decl_by_reference_p (decl_or_origin))
22905 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
22906 context_die);
22907 else
22908 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
22909 context_die);
22912 if (origin == NULL && !specialization_p)
22914 if (TREE_PUBLIC (decl))
22915 add_AT_flag (var_die, DW_AT_external, 1);
22917 if (DECL_ARTIFICIAL (decl))
22918 add_AT_flag (var_die, DW_AT_artificial, 1);
22920 add_alignment_attribute (var_die, decl);
22922 add_accessibility_attribute (var_die, decl);
22925 if (declaration)
22926 add_AT_flag (var_die, DW_AT_declaration, 1);
22928 if (decl && (DECL_ABSTRACT_P (decl)
22929 || !old_die || is_declaration_die (old_die)))
22930 equate_decl_number_to_die (decl, var_die);
22932 gen_variable_die_location:
22933 if (! declaration
22934 && (! DECL_ABSTRACT_P (decl_or_origin)
22935 /* Local static vars are shared between all clones/inlines,
22936 so emit DW_AT_location on the abstract DIE if DECL_RTL is
22937 already set. */
22938 || (VAR_P (decl_or_origin)
22939 && TREE_STATIC (decl_or_origin)
22940 && DECL_RTL_SET_P (decl_or_origin))))
22942 if (early_dwarf)
22943 add_pubname (decl_or_origin, var_die);
22944 else
22945 add_location_or_const_value_attribute (var_die, decl_or_origin,
22946 decl == NULL);
22948 else
22949 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
22951 if ((dwarf_version >= 4 || !dwarf_strict)
22952 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
22953 DW_AT_const_expr) == 1
22954 && !get_AT (var_die, DW_AT_const_expr)
22955 && !specialization_p)
22956 add_AT_flag (var_die, DW_AT_const_expr, 1);
22958 if (!dwarf_strict)
22960 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
22961 DW_AT_inline);
22962 if (inl != -1
22963 && !get_AT (var_die, DW_AT_inline)
22964 && !specialization_p)
22965 add_AT_unsigned (var_die, DW_AT_inline, inl);
22969 /* Generate a DIE to represent a named constant. */
22971 static void
22972 gen_const_die (tree decl, dw_die_ref context_die)
22974 dw_die_ref const_die;
22975 tree type = TREE_TYPE (decl);
22977 const_die = lookup_decl_die (decl);
22978 if (const_die)
22979 return;
22981 const_die = new_die (DW_TAG_constant, context_die, decl);
22982 equate_decl_number_to_die (decl, const_die);
22983 add_name_and_src_coords_attributes (const_die, decl);
22984 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
22985 if (TREE_PUBLIC (decl))
22986 add_AT_flag (const_die, DW_AT_external, 1);
22987 if (DECL_ARTIFICIAL (decl))
22988 add_AT_flag (const_die, DW_AT_artificial, 1);
22989 tree_add_const_value_attribute_for_decl (const_die, decl);
22992 /* Generate a DIE to represent a label identifier. */
22994 static void
22995 gen_label_die (tree decl, dw_die_ref context_die)
22997 tree origin = decl_ultimate_origin (decl);
22998 dw_die_ref lbl_die = lookup_decl_die (decl);
22999 rtx insn;
23000 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23002 if (!lbl_die)
23004 lbl_die = new_die (DW_TAG_label, context_die, decl);
23005 equate_decl_number_to_die (decl, lbl_die);
23007 if (origin != NULL)
23008 add_abstract_origin_attribute (lbl_die, origin);
23009 else
23010 add_name_and_src_coords_attributes (lbl_die, decl);
23013 if (DECL_ABSTRACT_P (decl))
23014 equate_decl_number_to_die (decl, lbl_die);
23015 else if (! early_dwarf)
23017 insn = DECL_RTL_IF_SET (decl);
23019 /* Deleted labels are programmer specified labels which have been
23020 eliminated because of various optimizations. We still emit them
23021 here so that it is possible to put breakpoints on them. */
23022 if (insn
23023 && (LABEL_P (insn)
23024 || ((NOTE_P (insn)
23025 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
23027 /* When optimization is enabled (via -O) some parts of the compiler
23028 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
23029 represent source-level labels which were explicitly declared by
23030 the user. This really shouldn't be happening though, so catch
23031 it if it ever does happen. */
23032 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
23034 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
23035 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23037 else if (insn
23038 && NOTE_P (insn)
23039 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
23040 && CODE_LABEL_NUMBER (insn) != -1)
23042 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
23043 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23048 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
23049 attributes to the DIE for a block STMT, to describe where the inlined
23050 function was called from. This is similar to add_src_coords_attributes. */
23052 static inline void
23053 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
23055 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
23057 if (dwarf_version >= 3 || !dwarf_strict)
23059 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
23060 add_AT_unsigned (die, DW_AT_call_line, s.line);
23061 if (debug_column_info && s.column)
23062 add_AT_unsigned (die, DW_AT_call_column, s.column);
23067 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
23068 Add low_pc and high_pc attributes to the DIE for a block STMT. */
23070 static inline void
23071 add_high_low_attributes (tree stmt, dw_die_ref die)
23073 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23075 if (BLOCK_FRAGMENT_CHAIN (stmt)
23076 && (dwarf_version >= 3 || !dwarf_strict))
23078 tree chain, superblock = NULL_TREE;
23079 dw_die_ref pdie;
23080 dw_attr_node *attr = NULL;
23082 if (inlined_function_outer_scope_p (stmt))
23084 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23085 BLOCK_NUMBER (stmt));
23086 add_AT_lbl_id (die, DW_AT_entry_pc, label);
23089 /* Optimize duplicate .debug_ranges lists or even tails of
23090 lists. If this BLOCK has same ranges as its supercontext,
23091 lookup DW_AT_ranges attribute in the supercontext (and
23092 recursively so), verify that the ranges_table contains the
23093 right values and use it instead of adding a new .debug_range. */
23094 for (chain = stmt, pdie = die;
23095 BLOCK_SAME_RANGE (chain);
23096 chain = BLOCK_SUPERCONTEXT (chain))
23098 dw_attr_node *new_attr;
23100 pdie = pdie->die_parent;
23101 if (pdie == NULL)
23102 break;
23103 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
23104 break;
23105 new_attr = get_AT (pdie, DW_AT_ranges);
23106 if (new_attr == NULL
23107 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
23108 break;
23109 attr = new_attr;
23110 superblock = BLOCK_SUPERCONTEXT (chain);
23112 if (attr != NULL
23113 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
23114 == BLOCK_NUMBER (superblock))
23115 && BLOCK_FRAGMENT_CHAIN (superblock))
23117 unsigned long off = attr->dw_attr_val.v.val_offset;
23118 unsigned long supercnt = 0, thiscnt = 0;
23119 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
23120 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23122 ++supercnt;
23123 gcc_checking_assert ((*ranges_table)[off + supercnt].num
23124 == BLOCK_NUMBER (chain));
23126 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
23127 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
23128 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23129 ++thiscnt;
23130 gcc_assert (supercnt >= thiscnt);
23131 add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
23132 false);
23133 note_rnglist_head (off + supercnt - thiscnt);
23134 return;
23137 unsigned int offset = add_ranges (stmt, true);
23138 add_AT_range_list (die, DW_AT_ranges, offset, false);
23139 note_rnglist_head (offset);
23141 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
23142 chain = BLOCK_FRAGMENT_CHAIN (stmt);
23145 add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
23146 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
23147 chain = BLOCK_FRAGMENT_CHAIN (chain);
23149 while (chain);
23150 add_ranges (NULL);
23152 else
23154 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
23155 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23156 BLOCK_NUMBER (stmt));
23157 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
23158 BLOCK_NUMBER (stmt));
23159 add_AT_low_high_pc (die, label, label_high, false);
23163 /* Generate a DIE for a lexical block. */
23165 static void
23166 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
23168 dw_die_ref old_die = BLOCK_DIE (stmt);
23169 dw_die_ref stmt_die = NULL;
23170 if (!old_die)
23172 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23173 BLOCK_DIE (stmt) = stmt_die;
23176 if (BLOCK_ABSTRACT (stmt))
23178 if (old_die)
23180 /* This must have been generated early and it won't even
23181 need location information since it's a DW_AT_inline
23182 function. */
23183 if (flag_checking)
23184 for (dw_die_ref c = context_die; c; c = c->die_parent)
23185 if (c->die_tag == DW_TAG_inlined_subroutine
23186 || c->die_tag == DW_TAG_subprogram)
23188 gcc_assert (get_AT (c, DW_AT_inline));
23189 break;
23191 return;
23194 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
23196 /* If this is an inlined instance, create a new lexical die for
23197 anything below to attach DW_AT_abstract_origin to. */
23198 if (old_die)
23200 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23201 BLOCK_DIE (stmt) = stmt_die;
23202 old_die = NULL;
23205 tree origin = block_ultimate_origin (stmt);
23206 if (origin != NULL_TREE && origin != stmt)
23207 add_abstract_origin_attribute (stmt_die, origin);
23210 if (old_die)
23211 stmt_die = old_die;
23213 /* A non abstract block whose blocks have already been reordered
23214 should have the instruction range for this block. If so, set the
23215 high/low attributes. */
23216 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
23218 gcc_assert (stmt_die);
23219 add_high_low_attributes (stmt, stmt_die);
23222 decls_for_scope (stmt, stmt_die);
23225 /* Generate a DIE for an inlined subprogram. */
23227 static void
23228 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
23230 tree decl;
23232 /* The instance of function that is effectively being inlined shall not
23233 be abstract. */
23234 gcc_assert (! BLOCK_ABSTRACT (stmt));
23236 decl = block_ultimate_origin (stmt);
23238 /* Make sure any inlined functions are known to be inlineable. */
23239 gcc_checking_assert (DECL_ABSTRACT_P (decl)
23240 || cgraph_function_possibly_inlined_p (decl));
23242 /* Emit info for the abstract instance first, if we haven't yet. We
23243 must emit this even if the block is abstract, otherwise when we
23244 emit the block below (or elsewhere), we may end up trying to emit
23245 a die whose origin die hasn't been emitted, and crashing. */
23246 dwarf2out_abstract_function (decl);
23248 if (! BLOCK_ABSTRACT (stmt))
23250 dw_die_ref subr_die
23251 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
23253 if (call_arg_locations)
23254 BLOCK_DIE (stmt) = subr_die;
23255 add_abstract_origin_attribute (subr_die, decl);
23256 if (TREE_ASM_WRITTEN (stmt))
23257 add_high_low_attributes (stmt, subr_die);
23258 add_call_src_coords_attributes (stmt, subr_die);
23260 decls_for_scope (stmt, subr_die);
23264 /* Generate a DIE for a field in a record, or structure. CTX is required: see
23265 the comment for VLR_CONTEXT. */
23267 static void
23268 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
23270 dw_die_ref decl_die;
23272 if (TREE_TYPE (decl) == error_mark_node)
23273 return;
23275 decl_die = new_die (DW_TAG_member, context_die, decl);
23276 add_name_and_src_coords_attributes (decl_die, decl);
23277 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
23278 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
23279 context_die);
23281 if (DECL_BIT_FIELD_TYPE (decl))
23283 add_byte_size_attribute (decl_die, decl);
23284 add_bit_size_attribute (decl_die, decl);
23285 add_bit_offset_attribute (decl_die, decl, ctx);
23288 add_alignment_attribute (decl_die, decl);
23290 /* If we have a variant part offset, then we are supposed to process a member
23291 of a QUAL_UNION_TYPE, which is how we represent variant parts in
23292 trees. */
23293 gcc_assert (ctx->variant_part_offset == NULL_TREE
23294 || TREE_CODE (DECL_FIELD_CONTEXT (decl)) != QUAL_UNION_TYPE);
23295 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
23296 add_data_member_location_attribute (decl_die, decl, ctx);
23298 if (DECL_ARTIFICIAL (decl))
23299 add_AT_flag (decl_die, DW_AT_artificial, 1);
23301 add_accessibility_attribute (decl_die, decl);
23303 /* Equate decl number to die, so that we can look up this decl later on. */
23304 equate_decl_number_to_die (decl, decl_die);
23307 /* Generate a DIE for a pointer to a member type. TYPE can be an
23308 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
23309 pointer to member function. */
23311 static void
23312 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
23314 if (lookup_type_die (type))
23315 return;
23317 dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
23318 scope_die_for (type, context_die), type);
23320 equate_type_number_to_die (type, ptr_die);
23321 add_AT_die_ref (ptr_die, DW_AT_containing_type,
23322 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
23323 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23324 context_die);
23325 add_alignment_attribute (ptr_die, type);
23327 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
23328 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
23330 dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
23331 add_AT_loc (ptr_die, DW_AT_use_location, op);
23335 static char *producer_string;
23337 /* Return a heap allocated producer string including command line options
23338 if -grecord-gcc-switches. */
23340 static char *
23341 gen_producer_string (void)
23343 size_t j;
23344 auto_vec<const char *> switches;
23345 const char *language_string = lang_hooks.name;
23346 char *producer, *tail;
23347 const char *p;
23348 size_t len = dwarf_record_gcc_switches ? 0 : 3;
23349 size_t plen = strlen (language_string) + 1 + strlen (version_string);
23351 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
23352 switch (save_decoded_options[j].opt_index)
23354 case OPT_o:
23355 case OPT_d:
23356 case OPT_dumpbase:
23357 case OPT_dumpdir:
23358 case OPT_auxbase:
23359 case OPT_auxbase_strip:
23360 case OPT_quiet:
23361 case OPT_version:
23362 case OPT_v:
23363 case OPT_w:
23364 case OPT_L:
23365 case OPT_D:
23366 case OPT_I:
23367 case OPT_U:
23368 case OPT_SPECIAL_unknown:
23369 case OPT_SPECIAL_ignore:
23370 case OPT_SPECIAL_program_name:
23371 case OPT_SPECIAL_input_file:
23372 case OPT_grecord_gcc_switches:
23373 case OPT_gno_record_gcc_switches:
23374 case OPT__output_pch_:
23375 case OPT_fdiagnostics_show_location_:
23376 case OPT_fdiagnostics_show_option:
23377 case OPT_fdiagnostics_show_caret:
23378 case OPT_fdiagnostics_color_:
23379 case OPT_fverbose_asm:
23380 case OPT____:
23381 case OPT__sysroot_:
23382 case OPT_nostdinc:
23383 case OPT_nostdinc__:
23384 case OPT_fpreprocessed:
23385 case OPT_fltrans_output_list_:
23386 case OPT_fresolution_:
23387 case OPT_fdebug_prefix_map_:
23388 /* Ignore these. */
23389 continue;
23390 default:
23391 if (cl_options[save_decoded_options[j].opt_index].flags
23392 & CL_NO_DWARF_RECORD)
23393 continue;
23394 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
23395 == '-');
23396 switch (save_decoded_options[j].canonical_option[0][1])
23398 case 'M':
23399 case 'i':
23400 case 'W':
23401 continue;
23402 case 'f':
23403 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
23404 "dump", 4) == 0)
23405 continue;
23406 break;
23407 default:
23408 break;
23410 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
23411 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
23412 break;
23415 producer = XNEWVEC (char, plen + 1 + len + 1);
23416 tail = producer;
23417 sprintf (tail, "%s %s", language_string, version_string);
23418 tail += plen;
23420 FOR_EACH_VEC_ELT (switches, j, p)
23422 len = strlen (p);
23423 *tail = ' ';
23424 memcpy (tail + 1, p, len);
23425 tail += len + 1;
23428 *tail = '\0';
23429 return producer;
23432 /* Given a C and/or C++ language/version string return the "highest".
23433 C++ is assumed to be "higher" than C in this case. Used for merging
23434 LTO translation unit languages. */
23435 static const char *
23436 highest_c_language (const char *lang1, const char *lang2)
23438 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
23439 return "GNU C++14";
23440 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
23441 return "GNU C++11";
23442 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
23443 return "GNU C++98";
23445 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
23446 return "GNU C11";
23447 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
23448 return "GNU C99";
23449 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
23450 return "GNU C89";
23452 gcc_unreachable ();
23456 /* Generate the DIE for the compilation unit. */
23458 static dw_die_ref
23459 gen_compile_unit_die (const char *filename)
23461 dw_die_ref die;
23462 const char *language_string = lang_hooks.name;
23463 int language;
23465 die = new_die (DW_TAG_compile_unit, NULL, NULL);
23467 if (filename)
23469 add_name_attribute (die, filename);
23470 /* Don't add cwd for <built-in>. */
23471 if (filename[0] != '<')
23472 add_comp_dir_attribute (die);
23475 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
23477 /* If our producer is LTO try to figure out a common language to use
23478 from the global list of translation units. */
23479 if (strcmp (language_string, "GNU GIMPLE") == 0)
23481 unsigned i;
23482 tree t;
23483 const char *common_lang = NULL;
23485 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
23487 if (!TRANSLATION_UNIT_LANGUAGE (t))
23488 continue;
23489 if (!common_lang)
23490 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
23491 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
23493 else if (strncmp (common_lang, "GNU C", 5) == 0
23494 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
23495 /* Mixing C and C++ is ok, use C++ in that case. */
23496 common_lang = highest_c_language (common_lang,
23497 TRANSLATION_UNIT_LANGUAGE (t));
23498 else
23500 /* Fall back to C. */
23501 common_lang = NULL;
23502 break;
23506 if (common_lang)
23507 language_string = common_lang;
23510 language = DW_LANG_C;
23511 if (strncmp (language_string, "GNU C", 5) == 0
23512 && ISDIGIT (language_string[5]))
23514 language = DW_LANG_C89;
23515 if (dwarf_version >= 3 || !dwarf_strict)
23517 if (strcmp (language_string, "GNU C89") != 0)
23518 language = DW_LANG_C99;
23520 if (dwarf_version >= 5 /* || !dwarf_strict */)
23521 if (strcmp (language_string, "GNU C11") == 0)
23522 language = DW_LANG_C11;
23525 else if (strncmp (language_string, "GNU C++", 7) == 0)
23527 language = DW_LANG_C_plus_plus;
23528 if (dwarf_version >= 5 /* || !dwarf_strict */)
23530 if (strcmp (language_string, "GNU C++11") == 0)
23531 language = DW_LANG_C_plus_plus_11;
23532 else if (strcmp (language_string, "GNU C++14") == 0)
23533 language = DW_LANG_C_plus_plus_14;
23536 else if (strcmp (language_string, "GNU F77") == 0)
23537 language = DW_LANG_Fortran77;
23538 else if (dwarf_version >= 3 || !dwarf_strict)
23540 if (strcmp (language_string, "GNU Ada") == 0)
23541 language = DW_LANG_Ada95;
23542 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23544 language = DW_LANG_Fortran95;
23545 if (dwarf_version >= 5 /* || !dwarf_strict */)
23547 if (strcmp (language_string, "GNU Fortran2003") == 0)
23548 language = DW_LANG_Fortran03;
23549 else if (strcmp (language_string, "GNU Fortran2008") == 0)
23550 language = DW_LANG_Fortran08;
23553 else if (strcmp (language_string, "GNU Objective-C") == 0)
23554 language = DW_LANG_ObjC;
23555 else if (strcmp (language_string, "GNU Objective-C++") == 0)
23556 language = DW_LANG_ObjC_plus_plus;
23557 else if (dwarf_version >= 5 || !dwarf_strict)
23559 if (strcmp (language_string, "GNU Go") == 0)
23560 language = DW_LANG_Go;
23563 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
23564 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23565 language = DW_LANG_Fortran90;
23567 add_AT_unsigned (die, DW_AT_language, language);
23569 switch (language)
23571 case DW_LANG_Fortran77:
23572 case DW_LANG_Fortran90:
23573 case DW_LANG_Fortran95:
23574 case DW_LANG_Fortran03:
23575 case DW_LANG_Fortran08:
23576 /* Fortran has case insensitive identifiers and the front-end
23577 lowercases everything. */
23578 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
23579 break;
23580 default:
23581 /* The default DW_ID_case_sensitive doesn't need to be specified. */
23582 break;
23584 return die;
23587 /* Generate the DIE for a base class. */
23589 static void
23590 gen_inheritance_die (tree binfo, tree access, tree type,
23591 dw_die_ref context_die)
23593 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
23594 struct vlr_context ctx = { type, NULL };
23596 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
23597 context_die);
23598 add_data_member_location_attribute (die, binfo, &ctx);
23600 if (BINFO_VIRTUAL_P (binfo))
23601 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
23603 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
23604 children, otherwise the default is DW_ACCESS_public. In DWARF2
23605 the default has always been DW_ACCESS_private. */
23606 if (access == access_public_node)
23608 if (dwarf_version == 2
23609 || context_die->die_tag == DW_TAG_class_type)
23610 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
23612 else if (access == access_protected_node)
23613 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
23614 else if (dwarf_version > 2
23615 && context_die->die_tag != DW_TAG_class_type)
23616 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
23619 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
23620 structure. */
23621 static bool
23622 is_variant_part (tree decl)
23624 return (TREE_CODE (decl) == FIELD_DECL
23625 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
23628 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
23629 return the FIELD_DECL. Return NULL_TREE otherwise. */
23631 static tree
23632 analyze_discr_in_predicate (tree operand, tree struct_type)
23634 bool continue_stripping = true;
23635 while (continue_stripping)
23636 switch (TREE_CODE (operand))
23638 CASE_CONVERT:
23639 operand = TREE_OPERAND (operand, 0);
23640 break;
23641 default:
23642 continue_stripping = false;
23643 break;
23646 /* Match field access to members of struct_type only. */
23647 if (TREE_CODE (operand) == COMPONENT_REF
23648 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
23649 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
23650 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
23651 return TREE_OPERAND (operand, 1);
23652 else
23653 return NULL_TREE;
23656 /* Check that SRC is a constant integer that can be represented as a native
23657 integer constant (either signed or unsigned). If so, store it into DEST and
23658 return true. Return false otherwise. */
23660 static bool
23661 get_discr_value (tree src, dw_discr_value *dest)
23663 tree discr_type = TREE_TYPE (src);
23665 if (lang_hooks.types.get_debug_type)
23667 tree debug_type = lang_hooks.types.get_debug_type (discr_type);
23668 if (debug_type != NULL)
23669 discr_type = debug_type;
23672 if (TREE_CODE (src) != INTEGER_CST || !INTEGRAL_TYPE_P (discr_type))
23673 return false;
23675 /* Signedness can vary between the original type and the debug type. This
23676 can happen for character types in Ada for instance: the character type
23677 used for code generation can be signed, to be compatible with the C one,
23678 but from a debugger point of view, it must be unsigned. */
23679 bool is_orig_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
23680 bool is_debug_unsigned = TYPE_UNSIGNED (discr_type);
23682 if (is_orig_unsigned != is_debug_unsigned)
23683 src = fold_convert (discr_type, src);
23685 if (!(is_debug_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
23686 return false;
23688 dest->pos = is_debug_unsigned;
23689 if (is_debug_unsigned)
23690 dest->v.uval = tree_to_uhwi (src);
23691 else
23692 dest->v.sval = tree_to_shwi (src);
23694 return true;
23697 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
23698 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
23699 store NULL_TREE in DISCR_DECL. Otherwise:
23701 - store the discriminant field in STRUCT_TYPE that controls the variant
23702 part to *DISCR_DECL
23704 - put in *DISCR_LISTS_P an array where for each variant, the item
23705 represents the corresponding matching list of discriminant values.
23707 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
23708 the above array.
23710 Note that when the array is allocated (i.e. when the analysis is
23711 successful), it is up to the caller to free the array. */
23713 static void
23714 analyze_variants_discr (tree variant_part_decl,
23715 tree struct_type,
23716 tree *discr_decl,
23717 dw_discr_list_ref **discr_lists_p,
23718 unsigned *discr_lists_length)
23720 tree variant_part_type = TREE_TYPE (variant_part_decl);
23721 tree variant;
23722 dw_discr_list_ref *discr_lists;
23723 unsigned i;
23725 /* Compute how many variants there are in this variant part. */
23726 *discr_lists_length = 0;
23727 for (variant = TYPE_FIELDS (variant_part_type);
23728 variant != NULL_TREE;
23729 variant = DECL_CHAIN (variant))
23730 ++*discr_lists_length;
23732 *discr_decl = NULL_TREE;
23733 *discr_lists_p
23734 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
23735 sizeof (**discr_lists_p));
23736 discr_lists = *discr_lists_p;
23738 /* And then analyze all variants to extract discriminant information for all
23739 of them. This analysis is conservative: as soon as we detect something we
23740 do not support, abort everything and pretend we found nothing. */
23741 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
23742 variant != NULL_TREE;
23743 variant = DECL_CHAIN (variant), ++i)
23745 tree match_expr = DECL_QUALIFIER (variant);
23747 /* Now, try to analyze the predicate and deduce a discriminant for
23748 it. */
23749 if (match_expr == boolean_true_node)
23750 /* Typically happens for the default variant: it matches all cases that
23751 previous variants rejected. Don't output any matching value for
23752 this one. */
23753 continue;
23755 /* The following loop tries to iterate over each discriminant
23756 possibility: single values or ranges. */
23757 while (match_expr != NULL_TREE)
23759 tree next_round_match_expr;
23760 tree candidate_discr = NULL_TREE;
23761 dw_discr_list_ref new_node = NULL;
23763 /* Possibilities are matched one after the other by nested
23764 TRUTH_ORIF_EXPR expressions. Process the current possibility and
23765 continue with the rest at next iteration. */
23766 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
23768 next_round_match_expr = TREE_OPERAND (match_expr, 0);
23769 match_expr = TREE_OPERAND (match_expr, 1);
23771 else
23772 next_round_match_expr = NULL_TREE;
23774 if (match_expr == boolean_false_node)
23775 /* This sub-expression matches nothing: just wait for the next
23776 one. */
23779 else if (TREE_CODE (match_expr) == EQ_EXPR)
23781 /* We are matching: <discr_field> == <integer_cst>
23782 This sub-expression matches a single value. */
23783 tree integer_cst = TREE_OPERAND (match_expr, 1);
23785 candidate_discr
23786 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
23787 struct_type);
23789 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23790 if (!get_discr_value (integer_cst,
23791 &new_node->dw_discr_lower_bound))
23792 goto abort;
23793 new_node->dw_discr_range = false;
23796 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
23798 /* We are matching:
23799 <discr_field> > <integer_cst>
23800 && <discr_field> < <integer_cst>.
23801 This sub-expression matches the range of values between the
23802 two matched integer constants. Note that comparisons can be
23803 inclusive or exclusive. */
23804 tree candidate_discr_1, candidate_discr_2;
23805 tree lower_cst, upper_cst;
23806 bool lower_cst_included, upper_cst_included;
23807 tree lower_op = TREE_OPERAND (match_expr, 0);
23808 tree upper_op = TREE_OPERAND (match_expr, 1);
23810 /* When the comparison is exclusive, the integer constant is not
23811 the discriminant range bound we are looking for: we will have
23812 to increment or decrement it. */
23813 if (TREE_CODE (lower_op) == GE_EXPR)
23814 lower_cst_included = true;
23815 else if (TREE_CODE (lower_op) == GT_EXPR)
23816 lower_cst_included = false;
23817 else
23818 goto abort;
23820 if (TREE_CODE (upper_op) == LE_EXPR)
23821 upper_cst_included = true;
23822 else if (TREE_CODE (upper_op) == LT_EXPR)
23823 upper_cst_included = false;
23824 else
23825 goto abort;
23827 /* Extract the discriminant from the first operand and check it
23828 is consistant with the same analysis in the second
23829 operand. */
23830 candidate_discr_1
23831 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
23832 struct_type);
23833 candidate_discr_2
23834 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
23835 struct_type);
23836 if (candidate_discr_1 == candidate_discr_2)
23837 candidate_discr = candidate_discr_1;
23838 else
23839 goto abort;
23841 /* Extract bounds from both. */
23842 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23843 lower_cst = TREE_OPERAND (lower_op, 1);
23844 upper_cst = TREE_OPERAND (upper_op, 1);
23846 if (!lower_cst_included)
23847 lower_cst
23848 = fold_build2 (PLUS_EXPR, TREE_TYPE (lower_cst), lower_cst,
23849 build_int_cst (TREE_TYPE (lower_cst), 1));
23850 if (!upper_cst_included)
23851 upper_cst
23852 = fold_build2 (MINUS_EXPR, TREE_TYPE (upper_cst), upper_cst,
23853 build_int_cst (TREE_TYPE (upper_cst), 1));
23855 if (!get_discr_value (lower_cst,
23856 &new_node->dw_discr_lower_bound)
23857 || !get_discr_value (upper_cst,
23858 &new_node->dw_discr_upper_bound))
23859 goto abort;
23861 new_node->dw_discr_range = true;
23864 else
23865 /* Unsupported sub-expression: we cannot determine the set of
23866 matching discriminant values. Abort everything. */
23867 goto abort;
23869 /* If the discriminant info is not consistant with what we saw so
23870 far, consider the analysis failed and abort everything. */
23871 if (candidate_discr == NULL_TREE
23872 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
23873 goto abort;
23874 else
23875 *discr_decl = candidate_discr;
23877 if (new_node != NULL)
23879 new_node->dw_discr_next = discr_lists[i];
23880 discr_lists[i] = new_node;
23882 match_expr = next_round_match_expr;
23886 /* If we reach this point, we could match everything we were interested
23887 in. */
23888 return;
23890 abort:
23891 /* Clean all data structure and return no result. */
23892 free (*discr_lists_p);
23893 *discr_lists_p = NULL;
23894 *discr_decl = NULL_TREE;
23897 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
23898 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
23899 under CONTEXT_DIE.
23901 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
23902 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
23903 this type, which are record types, represent the available variants and each
23904 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
23905 values are inferred from these attributes.
23907 In trees, the offsets for the fields inside these sub-records are relative
23908 to the variant part itself, whereas the corresponding DIEs should have
23909 offset attributes that are relative to the embedding record base address.
23910 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
23911 must be an expression that computes the offset of the variant part to
23912 describe in DWARF. */
23914 static void
23915 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
23916 dw_die_ref context_die)
23918 const tree variant_part_type = TREE_TYPE (variant_part_decl);
23919 tree variant_part_offset = vlr_ctx->variant_part_offset;
23920 struct loc_descr_context ctx = {
23921 vlr_ctx->struct_type, /* context_type */
23922 NULL_TREE, /* base_decl */
23923 NULL, /* dpi */
23924 false, /* placeholder_arg */
23925 false /* placeholder_seen */
23928 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
23929 NULL_TREE if there is no such field. */
23930 tree discr_decl = NULL_TREE;
23931 dw_discr_list_ref *discr_lists;
23932 unsigned discr_lists_length = 0;
23933 unsigned i;
23935 dw_die_ref dwarf_proc_die = NULL;
23936 dw_die_ref variant_part_die
23937 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
23939 equate_decl_number_to_die (variant_part_decl, variant_part_die);
23941 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
23942 &discr_decl, &discr_lists, &discr_lists_length);
23944 if (discr_decl != NULL_TREE)
23946 dw_die_ref discr_die = lookup_decl_die (discr_decl);
23948 if (discr_die)
23949 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
23950 else
23951 /* We have no DIE for the discriminant, so just discard all
23952 discrimimant information in the output. */
23953 discr_decl = NULL_TREE;
23956 /* If the offset for this variant part is more complex than a constant,
23957 create a DWARF procedure for it so that we will not have to generate DWARF
23958 expressions for it for each member. */
23959 if (TREE_CODE (variant_part_offset) != INTEGER_CST
23960 && (dwarf_version >= 3 || !dwarf_strict))
23962 const tree dwarf_proc_fndecl
23963 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
23964 build_function_type (TREE_TYPE (variant_part_offset),
23965 NULL_TREE));
23966 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
23967 const dw_loc_descr_ref dwarf_proc_body
23968 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
23970 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
23971 dwarf_proc_fndecl, context_die);
23972 if (dwarf_proc_die != NULL)
23973 variant_part_offset = dwarf_proc_call;
23976 /* Output DIEs for all variants. */
23977 i = 0;
23978 for (tree variant = TYPE_FIELDS (variant_part_type);
23979 variant != NULL_TREE;
23980 variant = DECL_CHAIN (variant), ++i)
23982 tree variant_type = TREE_TYPE (variant);
23983 dw_die_ref variant_die;
23985 /* All variants (i.e. members of a variant part) are supposed to be
23986 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
23987 under these records. */
23988 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
23990 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
23991 equate_decl_number_to_die (variant, variant_die);
23993 /* Output discriminant values this variant matches, if any. */
23994 if (discr_decl == NULL || discr_lists[i] == NULL)
23995 /* In the case we have discriminant information at all, this is
23996 probably the default variant: as the standard says, don't
23997 output any discriminant value/list attribute. */
23999 else if (discr_lists[i]->dw_discr_next == NULL
24000 && !discr_lists[i]->dw_discr_range)
24001 /* If there is only one accepted value, don't bother outputting a
24002 list. */
24003 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
24004 else
24005 add_discr_list (variant_die, discr_lists[i]);
24007 for (tree member = TYPE_FIELDS (variant_type);
24008 member != NULL_TREE;
24009 member = DECL_CHAIN (member))
24011 struct vlr_context vlr_sub_ctx = {
24012 vlr_ctx->struct_type, /* struct_type */
24013 NULL /* variant_part_offset */
24015 if (is_variant_part (member))
24017 /* All offsets for fields inside variant parts are relative to
24018 the top-level embedding RECORD_TYPE's base address. On the
24019 other hand, offsets in GCC's types are relative to the
24020 nested-most variant part. So we have to sum offsets each time
24021 we recurse. */
24023 vlr_sub_ctx.variant_part_offset
24024 = fold_build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
24025 variant_part_offset, byte_position (member));
24026 gen_variant_part (member, &vlr_sub_ctx, variant_die);
24028 else
24030 vlr_sub_ctx.variant_part_offset = variant_part_offset;
24031 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
24036 free (discr_lists);
24039 /* Generate a DIE for a class member. */
24041 static void
24042 gen_member_die (tree type, dw_die_ref context_die)
24044 tree member;
24045 tree binfo = TYPE_BINFO (type);
24047 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
24049 /* If this is not an incomplete type, output descriptions of each of its
24050 members. Note that as we output the DIEs necessary to represent the
24051 members of this record or union type, we will also be trying to output
24052 DIEs to represent the *types* of those members. However the `type'
24053 function (above) will specifically avoid generating type DIEs for member
24054 types *within* the list of member DIEs for this (containing) type except
24055 for those types (of members) which are explicitly marked as also being
24056 members of this (containing) type themselves. The g++ front- end can
24057 force any given type to be treated as a member of some other (containing)
24058 type by setting the TYPE_CONTEXT of the given (member) type to point to
24059 the TREE node representing the appropriate (containing) type. */
24061 /* First output info about the base classes. */
24062 if (binfo)
24064 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
24065 int i;
24066 tree base;
24068 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
24069 gen_inheritance_die (base,
24070 (accesses ? (*accesses)[i] : access_public_node),
24071 type,
24072 context_die);
24075 /* Now output info about the data members and type members. */
24076 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
24078 struct vlr_context vlr_ctx = { type, NULL_TREE };
24079 bool static_inline_p
24080 = (TREE_STATIC (member)
24081 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
24082 != -1));
24084 /* Ignore clones. */
24085 if (DECL_ABSTRACT_ORIGIN (member))
24086 continue;
24088 /* If we thought we were generating minimal debug info for TYPE
24089 and then changed our minds, some of the member declarations
24090 may have already been defined. Don't define them again, but
24091 do put them in the right order. */
24093 if (dw_die_ref child = lookup_decl_die (member))
24095 /* Handle inline static data members, which only have in-class
24096 declarations. */
24097 dw_die_ref ref = NULL;
24098 if (child->die_tag == DW_TAG_variable
24099 && child->die_parent == comp_unit_die ())
24101 ref = get_AT_ref (child, DW_AT_specification);
24102 /* For C++17 inline static data members followed by redundant
24103 out of class redeclaration, we might get here with
24104 child being the DIE created for the out of class
24105 redeclaration and with its DW_AT_specification being
24106 the DIE created for in-class definition. We want to
24107 reparent the latter, and don't want to create another
24108 DIE with DW_AT_specification in that case, because
24109 we already have one. */
24110 if (ref
24111 && static_inline_p
24112 && ref->die_tag == DW_TAG_variable
24113 && ref->die_parent == comp_unit_die ()
24114 && get_AT (ref, DW_AT_specification) == NULL)
24116 child = ref;
24117 ref = NULL;
24118 static_inline_p = false;
24122 if (child->die_tag == DW_TAG_variable
24123 && child->die_parent == comp_unit_die ()
24124 && ref == NULL)
24126 reparent_child (child, context_die);
24127 if (dwarf_version < 5)
24128 child->die_tag = DW_TAG_member;
24130 else
24131 splice_child_die (context_die, child);
24134 /* Do not generate standard DWARF for variant parts if we are generating
24135 the corresponding GNAT encodings: DIEs generated for both would
24136 conflict in our mappings. */
24137 else if (is_variant_part (member)
24138 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
24140 vlr_ctx.variant_part_offset = byte_position (member);
24141 gen_variant_part (member, &vlr_ctx, context_die);
24143 else
24145 vlr_ctx.variant_part_offset = NULL_TREE;
24146 gen_decl_die (member, NULL, &vlr_ctx, context_die);
24149 /* For C++ inline static data members emit immediately a DW_TAG_variable
24150 DIE that will refer to that DW_TAG_member/DW_TAG_variable through
24151 DW_AT_specification. */
24152 if (static_inline_p)
24154 int old_extern = DECL_EXTERNAL (member);
24155 DECL_EXTERNAL (member) = 0;
24156 gen_decl_die (member, NULL, NULL, comp_unit_die ());
24157 DECL_EXTERNAL (member) = old_extern;
24162 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
24163 is set, we pretend that the type was never defined, so we only get the
24164 member DIEs needed by later specification DIEs. */
24166 static void
24167 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
24168 enum debug_info_usage usage)
24170 if (TREE_ASM_WRITTEN (type))
24172 /* Fill in the bound of variable-length fields in late dwarf if
24173 still incomplete. */
24174 if (!early_dwarf && variably_modified_type_p (type, NULL))
24175 for (tree member = TYPE_FIELDS (type);
24176 member;
24177 member = DECL_CHAIN (member))
24178 fill_variable_array_bounds (TREE_TYPE (member));
24179 return;
24182 dw_die_ref type_die = lookup_type_die (type);
24183 dw_die_ref scope_die = 0;
24184 int nested = 0;
24185 int complete = (TYPE_SIZE (type)
24186 && (! TYPE_STUB_DECL (type)
24187 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
24188 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
24189 complete = complete && should_emit_struct_debug (type, usage);
24191 if (type_die && ! complete)
24192 return;
24194 if (TYPE_CONTEXT (type) != NULL_TREE
24195 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24196 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
24197 nested = 1;
24199 scope_die = scope_die_for (type, context_die);
24201 /* Generate child dies for template paramaters. */
24202 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
24203 schedule_generic_params_dies_gen (type);
24205 if (! type_die || (nested && is_cu_die (scope_die)))
24206 /* First occurrence of type or toplevel definition of nested class. */
24208 dw_die_ref old_die = type_die;
24210 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
24211 ? record_type_tag (type) : DW_TAG_union_type,
24212 scope_die, type);
24213 equate_type_number_to_die (type, type_die);
24214 if (old_die)
24215 add_AT_specification (type_die, old_die);
24216 else
24217 add_name_attribute (type_die, type_tag (type));
24219 else
24220 remove_AT (type_die, DW_AT_declaration);
24222 /* If this type has been completed, then give it a byte_size attribute and
24223 then give a list of members. */
24224 if (complete && !ns_decl)
24226 /* Prevent infinite recursion in cases where the type of some member of
24227 this type is expressed in terms of this type itself. */
24228 TREE_ASM_WRITTEN (type) = 1;
24229 add_byte_size_attribute (type_die, type);
24230 add_alignment_attribute (type_die, type);
24231 if (TYPE_STUB_DECL (type) != NULL_TREE)
24233 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
24234 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
24237 /* If the first reference to this type was as the return type of an
24238 inline function, then it may not have a parent. Fix this now. */
24239 if (type_die->die_parent == NULL)
24240 add_child_die (scope_die, type_die);
24242 push_decl_scope (type);
24243 gen_member_die (type, type_die);
24244 pop_decl_scope ();
24246 add_gnat_descriptive_type_attribute (type_die, type, context_die);
24247 if (TYPE_ARTIFICIAL (type))
24248 add_AT_flag (type_die, DW_AT_artificial, 1);
24250 /* GNU extension: Record what type our vtable lives in. */
24251 if (TYPE_VFIELD (type))
24253 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
24255 gen_type_die (vtype, context_die);
24256 add_AT_die_ref (type_die, DW_AT_containing_type,
24257 lookup_type_die (vtype));
24260 else
24262 add_AT_flag (type_die, DW_AT_declaration, 1);
24264 /* We don't need to do this for function-local types. */
24265 if (TYPE_STUB_DECL (type)
24266 && ! decl_function_context (TYPE_STUB_DECL (type)))
24267 vec_safe_push (incomplete_types, type);
24270 if (get_AT (type_die, DW_AT_name))
24271 add_pubtype (type, type_die);
24274 /* Generate a DIE for a subroutine _type_. */
24276 static void
24277 gen_subroutine_type_die (tree type, dw_die_ref context_die)
24279 tree return_type = TREE_TYPE (type);
24280 dw_die_ref subr_die
24281 = new_die (DW_TAG_subroutine_type,
24282 scope_die_for (type, context_die), type);
24284 equate_type_number_to_die (type, subr_die);
24285 add_prototyped_attribute (subr_die, type);
24286 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
24287 context_die);
24288 add_alignment_attribute (subr_die, type);
24289 gen_formal_types_die (type, subr_die);
24291 if (get_AT (subr_die, DW_AT_name))
24292 add_pubtype (type, subr_die);
24293 if ((dwarf_version >= 5 || !dwarf_strict)
24294 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
24295 add_AT_flag (subr_die, DW_AT_reference, 1);
24296 if ((dwarf_version >= 5 || !dwarf_strict)
24297 && lang_hooks.types.type_dwarf_attribute (type,
24298 DW_AT_rvalue_reference) != -1)
24299 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
24302 /* Generate a DIE for a type definition. */
24304 static void
24305 gen_typedef_die (tree decl, dw_die_ref context_die)
24307 dw_die_ref type_die;
24308 tree type;
24310 if (TREE_ASM_WRITTEN (decl))
24312 if (DECL_ORIGINAL_TYPE (decl))
24313 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
24314 return;
24317 /* As we avoid creating DIEs for local typedefs (see decl_ultimate_origin
24318 checks in process_scope_var and modified_type_die), this should be called
24319 only for original types. */
24320 gcc_assert (decl_ultimate_origin (decl) == NULL);
24322 TREE_ASM_WRITTEN (decl) = 1;
24323 type_die = new_die (DW_TAG_typedef, context_die, decl);
24325 add_name_and_src_coords_attributes (type_die, decl);
24326 if (DECL_ORIGINAL_TYPE (decl))
24328 type = DECL_ORIGINAL_TYPE (decl);
24329 if (type == error_mark_node)
24330 return;
24332 gcc_assert (type != TREE_TYPE (decl));
24333 equate_type_number_to_die (TREE_TYPE (decl), type_die);
24335 else
24337 type = TREE_TYPE (decl);
24338 if (type == error_mark_node)
24339 return;
24341 if (is_naming_typedef_decl (TYPE_NAME (type)))
24343 /* Here, we are in the case of decl being a typedef naming
24344 an anonymous type, e.g:
24345 typedef struct {...} foo;
24346 In that case TREE_TYPE (decl) is not a typedef variant
24347 type and TYPE_NAME of the anonymous type is set to the
24348 TYPE_DECL of the typedef. This construct is emitted by
24349 the C++ FE.
24351 TYPE is the anonymous struct named by the typedef
24352 DECL. As we need the DW_AT_type attribute of the
24353 DW_TAG_typedef to point to the DIE of TYPE, let's
24354 generate that DIE right away. add_type_attribute
24355 called below will then pick (via lookup_type_die) that
24356 anonymous struct DIE. */
24357 if (!TREE_ASM_WRITTEN (type))
24358 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
24360 /* This is a GNU Extension. We are adding a
24361 DW_AT_linkage_name attribute to the DIE of the
24362 anonymous struct TYPE. The value of that attribute
24363 is the name of the typedef decl naming the anonymous
24364 struct. This greatly eases the work of consumers of
24365 this debug info. */
24366 add_linkage_name_raw (lookup_type_die (type), decl);
24370 add_type_attribute (type_die, type, decl_quals (decl), false,
24371 context_die);
24373 if (is_naming_typedef_decl (decl))
24374 /* We want that all subsequent calls to lookup_type_die with
24375 TYPE in argument yield the DW_TAG_typedef we have just
24376 created. */
24377 equate_type_number_to_die (type, type_die);
24379 add_alignment_attribute (type_die, TREE_TYPE (decl));
24381 add_accessibility_attribute (type_die, decl);
24383 if (DECL_ABSTRACT_P (decl))
24384 equate_decl_number_to_die (decl, type_die);
24386 if (get_AT (type_die, DW_AT_name))
24387 add_pubtype (decl, type_die);
24390 /* Generate a DIE for a struct, class, enum or union type. */
24392 static void
24393 gen_tagged_type_die (tree type,
24394 dw_die_ref context_die,
24395 enum debug_info_usage usage)
24397 int need_pop;
24399 if (type == NULL_TREE
24400 || !is_tagged_type (type))
24401 return;
24403 if (TREE_ASM_WRITTEN (type))
24404 need_pop = 0;
24405 /* If this is a nested type whose containing class hasn't been written
24406 out yet, writing it out will cover this one, too. This does not apply
24407 to instantiations of member class templates; they need to be added to
24408 the containing class as they are generated. FIXME: This hurts the
24409 idea of combining type decls from multiple TUs, since we can't predict
24410 what set of template instantiations we'll get. */
24411 else if (TYPE_CONTEXT (type)
24412 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24413 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
24415 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
24417 if (TREE_ASM_WRITTEN (type))
24418 return;
24420 /* If that failed, attach ourselves to the stub. */
24421 push_decl_scope (TYPE_CONTEXT (type));
24422 context_die = lookup_type_die (TYPE_CONTEXT (type));
24423 need_pop = 1;
24425 else if (TYPE_CONTEXT (type) != NULL_TREE
24426 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
24428 /* If this type is local to a function that hasn't been written
24429 out yet, use a NULL context for now; it will be fixed up in
24430 decls_for_scope. */
24431 context_die = lookup_decl_die (TYPE_CONTEXT (type));
24432 /* A declaration DIE doesn't count; nested types need to go in the
24433 specification. */
24434 if (context_die && is_declaration_die (context_die))
24435 context_die = NULL;
24436 need_pop = 0;
24438 else
24440 context_die = declare_in_namespace (type, context_die);
24441 need_pop = 0;
24444 if (TREE_CODE (type) == ENUMERAL_TYPE)
24446 /* This might have been written out by the call to
24447 declare_in_namespace. */
24448 if (!TREE_ASM_WRITTEN (type))
24449 gen_enumeration_type_die (type, context_die);
24451 else
24452 gen_struct_or_union_type_die (type, context_die, usage);
24454 if (need_pop)
24455 pop_decl_scope ();
24457 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
24458 it up if it is ever completed. gen_*_type_die will set it for us
24459 when appropriate. */
24462 /* Generate a type description DIE. */
24464 static void
24465 gen_type_die_with_usage (tree type, dw_die_ref context_die,
24466 enum debug_info_usage usage)
24468 struct array_descr_info info;
24470 if (type == NULL_TREE || type == error_mark_node)
24471 return;
24473 if (flag_checking && type)
24474 verify_type (type);
24476 if (TYPE_NAME (type) != NULL_TREE
24477 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
24478 && is_redundant_typedef (TYPE_NAME (type))
24479 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
24480 /* The DECL of this type is a typedef we don't want to emit debug
24481 info for but we want debug info for its underlying typedef.
24482 This can happen for e.g, the injected-class-name of a C++
24483 type. */
24484 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
24486 /* If TYPE is a typedef type variant, let's generate debug info
24487 for the parent typedef which TYPE is a type of. */
24488 if (typedef_variant_p (type))
24490 if (TREE_ASM_WRITTEN (type))
24491 return;
24493 tree name = TYPE_NAME (type);
24494 tree origin = decl_ultimate_origin (name);
24495 if (origin != NULL)
24497 gen_decl_die (origin, NULL, NULL, context_die);
24498 return;
24501 /* Prevent broken recursion; we can't hand off to the same type. */
24502 gcc_assert (DECL_ORIGINAL_TYPE (name) != type);
24504 /* Give typedefs the right scope. */
24505 context_die = scope_die_for (type, context_die);
24507 TREE_ASM_WRITTEN (type) = 1;
24509 gen_decl_die (name, NULL, NULL, context_die);
24510 return;
24513 /* If type is an anonymous tagged type named by a typedef, let's
24514 generate debug info for the typedef. */
24515 if (is_naming_typedef_decl (TYPE_NAME (type)))
24517 /* Use the DIE of the containing namespace as the parent DIE of
24518 the type description DIE we want to generate. */
24519 if (DECL_CONTEXT (TYPE_NAME (type))
24520 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
24521 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
24523 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
24524 return;
24527 if (lang_hooks.types.get_debug_type)
24529 tree debug_type = lang_hooks.types.get_debug_type (type);
24531 if (debug_type != NULL_TREE && debug_type != type)
24533 gen_type_die_with_usage (debug_type, context_die, usage);
24534 return;
24538 /* We are going to output a DIE to represent the unqualified version
24539 of this type (i.e. without any const or volatile qualifiers) so
24540 get the main variant (i.e. the unqualified version) of this type
24541 now. (Vectors and arrays are special because the debugging info is in the
24542 cloned type itself. Similarly function/method types can contain extra
24543 ref-qualification). */
24544 if (TREE_CODE (type) == FUNCTION_TYPE
24545 || TREE_CODE (type) == METHOD_TYPE)
24547 /* For function/method types, can't use type_main_variant here,
24548 because that can have different ref-qualifiers for C++,
24549 but try to canonicalize. */
24550 tree main = TYPE_MAIN_VARIANT (type);
24551 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
24552 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
24553 && check_base_type (t, main)
24554 && check_lang_type (t, type))
24556 type = t;
24557 break;
24560 else if (TREE_CODE (type) != VECTOR_TYPE
24561 && TREE_CODE (type) != ARRAY_TYPE)
24562 type = type_main_variant (type);
24564 /* If this is an array type with hidden descriptor, handle it first. */
24565 if (!TREE_ASM_WRITTEN (type)
24566 && lang_hooks.types.get_array_descr_info)
24568 memset (&info, 0, sizeof (info));
24569 if (lang_hooks.types.get_array_descr_info (type, &info))
24571 /* Fortran sometimes emits array types with no dimension. */
24572 gcc_assert (info.ndimensions >= 0
24573 && (info.ndimensions
24574 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
24575 gen_descr_array_type_die (type, &info, context_die);
24576 TREE_ASM_WRITTEN (type) = 1;
24577 return;
24581 if (TREE_ASM_WRITTEN (type))
24583 /* Variable-length types may be incomplete even if
24584 TREE_ASM_WRITTEN. For such types, fall through to
24585 gen_array_type_die() and possibly fill in
24586 DW_AT_{upper,lower}_bound attributes. */
24587 if ((TREE_CODE (type) != ARRAY_TYPE
24588 && TREE_CODE (type) != RECORD_TYPE
24589 && TREE_CODE (type) != UNION_TYPE
24590 && TREE_CODE (type) != QUAL_UNION_TYPE)
24591 || !variably_modified_type_p (type, NULL))
24592 return;
24595 switch (TREE_CODE (type))
24597 case ERROR_MARK:
24598 break;
24600 case POINTER_TYPE:
24601 case REFERENCE_TYPE:
24602 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
24603 ensures that the gen_type_die recursion will terminate even if the
24604 type is recursive. Recursive types are possible in Ada. */
24605 /* ??? We could perhaps do this for all types before the switch
24606 statement. */
24607 TREE_ASM_WRITTEN (type) = 1;
24609 /* For these types, all that is required is that we output a DIE (or a
24610 set of DIEs) to represent the "basis" type. */
24611 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24612 DINFO_USAGE_IND_USE);
24613 break;
24615 case OFFSET_TYPE:
24616 /* This code is used for C++ pointer-to-data-member types.
24617 Output a description of the relevant class type. */
24618 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
24619 DINFO_USAGE_IND_USE);
24621 /* Output a description of the type of the object pointed to. */
24622 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24623 DINFO_USAGE_IND_USE);
24625 /* Now output a DIE to represent this pointer-to-data-member type
24626 itself. */
24627 gen_ptr_to_mbr_type_die (type, context_die);
24628 break;
24630 case FUNCTION_TYPE:
24631 /* Force out return type (in case it wasn't forced out already). */
24632 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24633 DINFO_USAGE_DIR_USE);
24634 gen_subroutine_type_die (type, context_die);
24635 break;
24637 case METHOD_TYPE:
24638 /* Force out return type (in case it wasn't forced out already). */
24639 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24640 DINFO_USAGE_DIR_USE);
24641 gen_subroutine_type_die (type, context_die);
24642 break;
24644 case ARRAY_TYPE:
24645 case VECTOR_TYPE:
24646 gen_array_type_die (type, context_die);
24647 break;
24649 case ENUMERAL_TYPE:
24650 case RECORD_TYPE:
24651 case UNION_TYPE:
24652 case QUAL_UNION_TYPE:
24653 gen_tagged_type_die (type, context_die, usage);
24654 return;
24656 case VOID_TYPE:
24657 case INTEGER_TYPE:
24658 case REAL_TYPE:
24659 case FIXED_POINT_TYPE:
24660 case COMPLEX_TYPE:
24661 case BOOLEAN_TYPE:
24662 case POINTER_BOUNDS_TYPE:
24663 /* No DIEs needed for fundamental types. */
24664 break;
24666 case NULLPTR_TYPE:
24667 case LANG_TYPE:
24668 /* Just use DW_TAG_unspecified_type. */
24670 dw_die_ref type_die = lookup_type_die (type);
24671 if (type_die == NULL)
24673 tree name = TYPE_IDENTIFIER (type);
24674 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
24675 type);
24676 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
24677 equate_type_number_to_die (type, type_die);
24680 break;
24682 default:
24683 if (is_cxx_auto (type))
24685 tree name = TYPE_IDENTIFIER (type);
24686 dw_die_ref *die = (name == get_identifier ("auto")
24687 ? &auto_die : &decltype_auto_die);
24688 if (!*die)
24690 *die = new_die (DW_TAG_unspecified_type,
24691 comp_unit_die (), NULL_TREE);
24692 add_name_attribute (*die, IDENTIFIER_POINTER (name));
24694 equate_type_number_to_die (type, *die);
24695 break;
24697 gcc_unreachable ();
24700 TREE_ASM_WRITTEN (type) = 1;
24703 static void
24704 gen_type_die (tree type, dw_die_ref context_die)
24706 if (type != error_mark_node)
24708 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
24709 if (flag_checking)
24711 dw_die_ref die = lookup_type_die (type);
24712 if (die)
24713 check_die (die);
24718 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
24719 things which are local to the given block. */
24721 static void
24722 gen_block_die (tree stmt, dw_die_ref context_die)
24724 int must_output_die = 0;
24725 bool inlined_func;
24727 /* Ignore blocks that are NULL. */
24728 if (stmt == NULL_TREE)
24729 return;
24731 inlined_func = inlined_function_outer_scope_p (stmt);
24733 /* If the block is one fragment of a non-contiguous block, do not
24734 process the variables, since they will have been done by the
24735 origin block. Do process subblocks. */
24736 if (BLOCK_FRAGMENT_ORIGIN (stmt))
24738 tree sub;
24740 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
24741 gen_block_die (sub, context_die);
24743 return;
24746 /* Determine if we need to output any Dwarf DIEs at all to represent this
24747 block. */
24748 if (inlined_func)
24749 /* The outer scopes for inlinings *must* always be represented. We
24750 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
24751 must_output_die = 1;
24752 else
24754 /* Determine if this block directly contains any "significant"
24755 local declarations which we will need to output DIEs for. */
24756 if (debug_info_level > DINFO_LEVEL_TERSE)
24757 /* We are not in terse mode so *any* local declaration counts
24758 as being a "significant" one. */
24759 must_output_die = ((BLOCK_VARS (stmt) != NULL
24760 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
24761 && (TREE_USED (stmt)
24762 || TREE_ASM_WRITTEN (stmt)
24763 || BLOCK_ABSTRACT (stmt)));
24764 else if ((TREE_USED (stmt)
24765 || TREE_ASM_WRITTEN (stmt)
24766 || BLOCK_ABSTRACT (stmt))
24767 && !dwarf2out_ignore_block (stmt))
24768 must_output_die = 1;
24771 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
24772 DIE for any block which contains no significant local declarations at
24773 all. Rather, in such cases we just call `decls_for_scope' so that any
24774 needed Dwarf info for any sub-blocks will get properly generated. Note
24775 that in terse mode, our definition of what constitutes a "significant"
24776 local declaration gets restricted to include only inlined function
24777 instances and local (nested) function definitions. */
24778 if (must_output_die)
24780 if (inlined_func)
24782 /* If STMT block is abstract, that means we have been called
24783 indirectly from dwarf2out_abstract_function.
24784 That function rightfully marks the descendent blocks (of
24785 the abstract function it is dealing with) as being abstract,
24786 precisely to prevent us from emitting any
24787 DW_TAG_inlined_subroutine DIE as a descendent
24788 of an abstract function instance. So in that case, we should
24789 not call gen_inlined_subroutine_die.
24791 Later though, when cgraph asks dwarf2out to emit info
24792 for the concrete instance of the function decl into which
24793 the concrete instance of STMT got inlined, the later will lead
24794 to the generation of a DW_TAG_inlined_subroutine DIE. */
24795 if (! BLOCK_ABSTRACT (stmt))
24796 gen_inlined_subroutine_die (stmt, context_die);
24798 else
24799 gen_lexical_block_die (stmt, context_die);
24801 else
24802 decls_for_scope (stmt, context_die);
24805 /* Process variable DECL (or variable with origin ORIGIN) within
24806 block STMT and add it to CONTEXT_DIE. */
24807 static void
24808 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
24810 dw_die_ref die;
24811 tree decl_or_origin = decl ? decl : origin;
24813 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
24814 die = lookup_decl_die (decl_or_origin);
24815 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
24817 if (TYPE_DECL_IS_STUB (decl_or_origin))
24818 die = lookup_type_die (TREE_TYPE (decl_or_origin));
24819 else
24820 die = lookup_decl_die (decl_or_origin);
24821 /* Avoid re-creating the DIE late if it was optimized as unused early. */
24822 if (! die && ! early_dwarf)
24823 return;
24825 else
24826 die = NULL;
24828 /* Avoid creating DIEs for local typedefs and concrete static variables that
24829 will only be pruned later. */
24830 if ((origin || decl_ultimate_origin (decl))
24831 && (TREE_CODE (decl_or_origin) == TYPE_DECL
24832 || (VAR_P (decl_or_origin) && TREE_STATIC (decl_or_origin))))
24834 origin = decl_ultimate_origin (decl_or_origin);
24835 if (decl && VAR_P (decl) && die != NULL)
24837 die = lookup_decl_die (origin);
24838 if (die != NULL)
24839 equate_decl_number_to_die (decl, die);
24841 return;
24844 if (die != NULL && die->die_parent == NULL)
24845 add_child_die (context_die, die);
24846 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
24848 if (early_dwarf)
24849 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
24850 stmt, context_die);
24852 else
24853 gen_decl_die (decl, origin, NULL, context_die);
24856 /* Generate all of the decls declared within a given scope and (recursively)
24857 all of its sub-blocks. */
24859 static void
24860 decls_for_scope (tree stmt, dw_die_ref context_die)
24862 tree decl;
24863 unsigned int i;
24864 tree subblocks;
24866 /* Ignore NULL blocks. */
24867 if (stmt == NULL_TREE)
24868 return;
24870 /* Output the DIEs to represent all of the data objects and typedefs
24871 declared directly within this block but not within any nested
24872 sub-blocks. Also, nested function and tag DIEs have been
24873 generated with a parent of NULL; fix that up now. We don't
24874 have to do this if we're at -g1. */
24875 if (debug_info_level > DINFO_LEVEL_TERSE)
24877 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
24878 process_scope_var (stmt, decl, NULL_TREE, context_die);
24879 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
24880 origin - avoid doing this twice as we have no good way to see
24881 if we've done it once already. */
24882 if (! early_dwarf)
24883 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
24885 decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
24886 if (decl == current_function_decl)
24887 /* Ignore declarations of the current function, while they
24888 are declarations, gen_subprogram_die would treat them
24889 as definitions again, because they are equal to
24890 current_function_decl and endlessly recurse. */;
24891 else if (TREE_CODE (decl) == FUNCTION_DECL)
24892 process_scope_var (stmt, decl, NULL_TREE, context_die);
24893 else
24894 process_scope_var (stmt, NULL_TREE, decl, context_die);
24898 /* Even if we're at -g1, we need to process the subblocks in order to get
24899 inlined call information. */
24901 /* Output the DIEs to represent all sub-blocks (and the items declared
24902 therein) of this block. */
24903 for (subblocks = BLOCK_SUBBLOCKS (stmt);
24904 subblocks != NULL;
24905 subblocks = BLOCK_CHAIN (subblocks))
24906 gen_block_die (subblocks, context_die);
24909 /* Is this a typedef we can avoid emitting? */
24911 bool
24912 is_redundant_typedef (const_tree decl)
24914 if (TYPE_DECL_IS_STUB (decl))
24915 return true;
24917 if (DECL_ARTIFICIAL (decl)
24918 && DECL_CONTEXT (decl)
24919 && is_tagged_type (DECL_CONTEXT (decl))
24920 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
24921 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
24922 /* Also ignore the artificial member typedef for the class name. */
24923 return true;
24925 return false;
24928 /* Return TRUE if TYPE is a typedef that names a type for linkage
24929 purposes. This kind of typedefs is produced by the C++ FE for
24930 constructs like:
24932 typedef struct {...} foo;
24934 In that case, there is no typedef variant type produced for foo.
24935 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
24936 struct type. */
24938 static bool
24939 is_naming_typedef_decl (const_tree decl)
24941 if (decl == NULL_TREE
24942 || TREE_CODE (decl) != TYPE_DECL
24943 || DECL_NAMELESS (decl)
24944 || !is_tagged_type (TREE_TYPE (decl))
24945 || DECL_IS_BUILTIN (decl)
24946 || is_redundant_typedef (decl)
24947 /* It looks like Ada produces TYPE_DECLs that are very similar
24948 to C++ naming typedefs but that have different
24949 semantics. Let's be specific to c++ for now. */
24950 || !is_cxx (decl))
24951 return FALSE;
24953 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
24954 && TYPE_NAME (TREE_TYPE (decl)) == decl
24955 && (TYPE_STUB_DECL (TREE_TYPE (decl))
24956 != TYPE_NAME (TREE_TYPE (decl))));
24959 /* Looks up the DIE for a context. */
24961 static inline dw_die_ref
24962 lookup_context_die (tree context)
24964 if (context)
24966 /* Find die that represents this context. */
24967 if (TYPE_P (context))
24969 context = TYPE_MAIN_VARIANT (context);
24970 dw_die_ref ctx = lookup_type_die (context);
24971 if (!ctx)
24972 return NULL;
24973 return strip_naming_typedef (context, ctx);
24975 else
24976 return lookup_decl_die (context);
24978 return comp_unit_die ();
24981 /* Returns the DIE for a context. */
24983 static inline dw_die_ref
24984 get_context_die (tree context)
24986 if (context)
24988 /* Find die that represents this context. */
24989 if (TYPE_P (context))
24991 context = TYPE_MAIN_VARIANT (context);
24992 return strip_naming_typedef (context, force_type_die (context));
24994 else
24995 return force_decl_die (context);
24997 return comp_unit_die ();
25000 /* Returns the DIE for decl. A DIE will always be returned. */
25002 static dw_die_ref
25003 force_decl_die (tree decl)
25005 dw_die_ref decl_die;
25006 unsigned saved_external_flag;
25007 tree save_fn = NULL_TREE;
25008 decl_die = lookup_decl_die (decl);
25009 if (!decl_die)
25011 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
25013 decl_die = lookup_decl_die (decl);
25014 if (decl_die)
25015 return decl_die;
25017 switch (TREE_CODE (decl))
25019 case FUNCTION_DECL:
25020 /* Clear current_function_decl, so that gen_subprogram_die thinks
25021 that this is a declaration. At this point, we just want to force
25022 declaration die. */
25023 save_fn = current_function_decl;
25024 current_function_decl = NULL_TREE;
25025 gen_subprogram_die (decl, context_die);
25026 current_function_decl = save_fn;
25027 break;
25029 case VAR_DECL:
25030 /* Set external flag to force declaration die. Restore it after
25031 gen_decl_die() call. */
25032 saved_external_flag = DECL_EXTERNAL (decl);
25033 DECL_EXTERNAL (decl) = 1;
25034 gen_decl_die (decl, NULL, NULL, context_die);
25035 DECL_EXTERNAL (decl) = saved_external_flag;
25036 break;
25038 case NAMESPACE_DECL:
25039 if (dwarf_version >= 3 || !dwarf_strict)
25040 dwarf2out_decl (decl);
25041 else
25042 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
25043 decl_die = comp_unit_die ();
25044 break;
25046 case TRANSLATION_UNIT_DECL:
25047 decl_die = comp_unit_die ();
25048 break;
25050 default:
25051 gcc_unreachable ();
25054 /* We should be able to find the DIE now. */
25055 if (!decl_die)
25056 decl_die = lookup_decl_die (decl);
25057 gcc_assert (decl_die);
25060 return decl_die;
25063 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
25064 always returned. */
25066 static dw_die_ref
25067 force_type_die (tree type)
25069 dw_die_ref type_die;
25071 type_die = lookup_type_die (type);
25072 if (!type_die)
25074 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
25076 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
25077 false, context_die);
25078 gcc_assert (type_die);
25080 return type_die;
25083 /* Force out any required namespaces to be able to output DECL,
25084 and return the new context_die for it, if it's changed. */
25086 static dw_die_ref
25087 setup_namespace_context (tree thing, dw_die_ref context_die)
25089 tree context = (DECL_P (thing)
25090 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
25091 if (context && TREE_CODE (context) == NAMESPACE_DECL)
25092 /* Force out the namespace. */
25093 context_die = force_decl_die (context);
25095 return context_die;
25098 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
25099 type) within its namespace, if appropriate.
25101 For compatibility with older debuggers, namespace DIEs only contain
25102 declarations; all definitions are emitted at CU scope, with
25103 DW_AT_specification pointing to the declaration (like with class
25104 members). */
25106 static dw_die_ref
25107 declare_in_namespace (tree thing, dw_die_ref context_die)
25109 dw_die_ref ns_context;
25111 if (debug_info_level <= DINFO_LEVEL_TERSE)
25112 return context_die;
25114 /* External declarations in the local scope only need to be emitted
25115 once, not once in the namespace and once in the scope.
25117 This avoids declaring the `extern' below in the
25118 namespace DIE as well as in the innermost scope:
25120 namespace S
25122 int i=5;
25123 int foo()
25125 int i=8;
25126 extern int i;
25127 return i;
25131 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
25132 return context_die;
25134 /* If this decl is from an inlined function, then don't try to emit it in its
25135 namespace, as we will get confused. It would have already been emitted
25136 when the abstract instance of the inline function was emitted anyways. */
25137 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
25138 return context_die;
25140 ns_context = setup_namespace_context (thing, context_die);
25142 if (ns_context != context_die)
25144 if (is_fortran ())
25145 return ns_context;
25146 if (DECL_P (thing))
25147 gen_decl_die (thing, NULL, NULL, ns_context);
25148 else
25149 gen_type_die (thing, ns_context);
25151 return context_die;
25154 /* Generate a DIE for a namespace or namespace alias. */
25156 static void
25157 gen_namespace_die (tree decl, dw_die_ref context_die)
25159 dw_die_ref namespace_die;
25161 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
25162 they are an alias of. */
25163 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
25165 /* Output a real namespace or module. */
25166 context_die = setup_namespace_context (decl, comp_unit_die ());
25167 namespace_die = new_die (is_fortran ()
25168 ? DW_TAG_module : DW_TAG_namespace,
25169 context_die, decl);
25170 /* For Fortran modules defined in different CU don't add src coords. */
25171 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
25173 const char *name = dwarf2_name (decl, 0);
25174 if (name)
25175 add_name_attribute (namespace_die, name);
25177 else
25178 add_name_and_src_coords_attributes (namespace_die, decl);
25179 if (DECL_EXTERNAL (decl))
25180 add_AT_flag (namespace_die, DW_AT_declaration, 1);
25181 equate_decl_number_to_die (decl, namespace_die);
25183 else
25185 /* Output a namespace alias. */
25187 /* Force out the namespace we are an alias of, if necessary. */
25188 dw_die_ref origin_die
25189 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
25191 if (DECL_FILE_SCOPE_P (decl)
25192 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
25193 context_die = setup_namespace_context (decl, comp_unit_die ());
25194 /* Now create the namespace alias DIE. */
25195 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
25196 add_name_and_src_coords_attributes (namespace_die, decl);
25197 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
25198 equate_decl_number_to_die (decl, namespace_die);
25200 if ((dwarf_version >= 5 || !dwarf_strict)
25201 && lang_hooks.decls.decl_dwarf_attribute (decl,
25202 DW_AT_export_symbols) == 1)
25203 add_AT_flag (namespace_die, DW_AT_export_symbols, 1);
25205 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
25206 if (want_pubnames ())
25207 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
25210 /* Generate Dwarf debug information for a decl described by DECL.
25211 The return value is currently only meaningful for PARM_DECLs,
25212 for all other decls it returns NULL.
25214 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
25215 It can be NULL otherwise. */
25217 static dw_die_ref
25218 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
25219 dw_die_ref context_die)
25221 tree decl_or_origin = decl ? decl : origin;
25222 tree class_origin = NULL, ultimate_origin;
25224 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
25225 return NULL;
25227 /* Ignore pointer bounds decls. */
25228 if (DECL_P (decl_or_origin)
25229 && TREE_TYPE (decl_or_origin)
25230 && POINTER_BOUNDS_P (decl_or_origin))
25231 return NULL;
25233 switch (TREE_CODE (decl_or_origin))
25235 case ERROR_MARK:
25236 break;
25238 case CONST_DECL:
25239 if (!is_fortran () && !is_ada ())
25241 /* The individual enumerators of an enum type get output when we output
25242 the Dwarf representation of the relevant enum type itself. */
25243 break;
25246 /* Emit its type. */
25247 gen_type_die (TREE_TYPE (decl), context_die);
25249 /* And its containing namespace. */
25250 context_die = declare_in_namespace (decl, context_die);
25252 gen_const_die (decl, context_die);
25253 break;
25255 case FUNCTION_DECL:
25256 #if 0
25257 /* FIXME */
25258 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
25259 on local redeclarations of global functions. That seems broken. */
25260 if (current_function_decl != decl)
25261 /* This is only a declaration. */;
25262 #endif
25264 /* If we're emitting a clone, emit info for the abstract instance. */
25265 if (origin || DECL_ORIGIN (decl) != decl)
25266 dwarf2out_abstract_function (origin
25267 ? DECL_ORIGIN (origin)
25268 : DECL_ABSTRACT_ORIGIN (decl));
25270 /* If we're emitting an out-of-line copy of an inline function,
25271 emit info for the abstract instance and set up to refer to it. */
25272 else if (cgraph_function_possibly_inlined_p (decl)
25273 && ! DECL_ABSTRACT_P (decl)
25274 && ! class_or_namespace_scope_p (context_die)
25275 /* dwarf2out_abstract_function won't emit a die if this is just
25276 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
25277 that case, because that works only if we have a die. */
25278 && DECL_INITIAL (decl) != NULL_TREE)
25280 dwarf2out_abstract_function (decl);
25281 set_decl_origin_self (decl);
25284 /* Otherwise we're emitting the primary DIE for this decl. */
25285 else if (debug_info_level > DINFO_LEVEL_TERSE)
25287 /* Before we describe the FUNCTION_DECL itself, make sure that we
25288 have its containing type. */
25289 if (!origin)
25290 origin = decl_class_context (decl);
25291 if (origin != NULL_TREE)
25292 gen_type_die (origin, context_die);
25294 /* And its return type. */
25295 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
25297 /* And its virtual context. */
25298 if (DECL_VINDEX (decl) != NULL_TREE)
25299 gen_type_die (DECL_CONTEXT (decl), context_die);
25301 /* Make sure we have a member DIE for decl. */
25302 if (origin != NULL_TREE)
25303 gen_type_die_for_member (origin, decl, context_die);
25305 /* And its containing namespace. */
25306 context_die = declare_in_namespace (decl, context_die);
25309 /* Now output a DIE to represent the function itself. */
25310 if (decl)
25311 gen_subprogram_die (decl, context_die);
25312 break;
25314 case TYPE_DECL:
25315 /* If we are in terse mode, don't generate any DIEs to represent any
25316 actual typedefs. */
25317 if (debug_info_level <= DINFO_LEVEL_TERSE)
25318 break;
25320 /* In the special case of a TYPE_DECL node representing the declaration
25321 of some type tag, if the given TYPE_DECL is marked as having been
25322 instantiated from some other (original) TYPE_DECL node (e.g. one which
25323 was generated within the original definition of an inline function) we
25324 used to generate a special (abbreviated) DW_TAG_structure_type,
25325 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
25326 should be actually referencing those DIEs, as variable DIEs with that
25327 type would be emitted already in the abstract origin, so it was always
25328 removed during unused type prunning. Don't add anything in this
25329 case. */
25330 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
25331 break;
25333 if (is_redundant_typedef (decl))
25334 gen_type_die (TREE_TYPE (decl), context_die);
25335 else
25336 /* Output a DIE to represent the typedef itself. */
25337 gen_typedef_die (decl, context_die);
25338 break;
25340 case LABEL_DECL:
25341 if (debug_info_level >= DINFO_LEVEL_NORMAL)
25342 gen_label_die (decl, context_die);
25343 break;
25345 case VAR_DECL:
25346 case RESULT_DECL:
25347 /* If we are in terse mode, don't generate any DIEs to represent any
25348 variable declarations or definitions. */
25349 if (debug_info_level <= DINFO_LEVEL_TERSE)
25350 break;
25352 /* Output any DIEs that are needed to specify the type of this data
25353 object. */
25354 if (decl_by_reference_p (decl_or_origin))
25355 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25356 else
25357 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25359 /* And its containing type. */
25360 class_origin = decl_class_context (decl_or_origin);
25361 if (class_origin != NULL_TREE)
25362 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
25364 /* And its containing namespace. */
25365 context_die = declare_in_namespace (decl_or_origin, context_die);
25367 /* Now output the DIE to represent the data object itself. This gets
25368 complicated because of the possibility that the VAR_DECL really
25369 represents an inlined instance of a formal parameter for an inline
25370 function. */
25371 ultimate_origin = decl_ultimate_origin (decl_or_origin);
25372 if (ultimate_origin != NULL_TREE
25373 && TREE_CODE (ultimate_origin) == PARM_DECL)
25374 gen_formal_parameter_die (decl, origin,
25375 true /* Emit name attribute. */,
25376 context_die);
25377 else
25378 gen_variable_die (decl, origin, context_die);
25379 break;
25381 case FIELD_DECL:
25382 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
25383 /* Ignore the nameless fields that are used to skip bits but handle C++
25384 anonymous unions and structs. */
25385 if (DECL_NAME (decl) != NULL_TREE
25386 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
25387 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
25389 gen_type_die (member_declared_type (decl), context_die);
25390 gen_field_die (decl, ctx, context_die);
25392 break;
25394 case PARM_DECL:
25395 if (DECL_BY_REFERENCE (decl_or_origin))
25396 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25397 else
25398 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25399 return gen_formal_parameter_die (decl, origin,
25400 true /* Emit name attribute. */,
25401 context_die);
25403 case NAMESPACE_DECL:
25404 if (dwarf_version >= 3 || !dwarf_strict)
25405 gen_namespace_die (decl, context_die);
25406 break;
25408 case IMPORTED_DECL:
25409 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
25410 DECL_CONTEXT (decl), context_die);
25411 break;
25413 case NAMELIST_DECL:
25414 gen_namelist_decl (DECL_NAME (decl), context_die,
25415 NAMELIST_DECL_ASSOCIATED_DECL (decl));
25416 break;
25418 default:
25419 /* Probably some frontend-internal decl. Assume we don't care. */
25420 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
25421 break;
25424 return NULL;
25427 /* Output initial debug information for global DECL. Called at the
25428 end of the parsing process.
25430 This is the initial debug generation process. As such, the DIEs
25431 generated may be incomplete. A later debug generation pass
25432 (dwarf2out_late_global_decl) will augment the information generated
25433 in this pass (e.g., with complete location info). */
25435 static void
25436 dwarf2out_early_global_decl (tree decl)
25438 set_early_dwarf s;
25440 /* gen_decl_die() will set DECL_ABSTRACT because
25441 cgraph_function_possibly_inlined_p() returns true. This is in
25442 turn will cause DW_AT_inline attributes to be set.
25444 This happens because at early dwarf generation, there is no
25445 cgraph information, causing cgraph_function_possibly_inlined_p()
25446 to return true. Trick cgraph_function_possibly_inlined_p()
25447 while we generate dwarf early. */
25448 bool save = symtab->global_info_ready;
25449 symtab->global_info_ready = true;
25451 /* We don't handle TYPE_DECLs. If required, they'll be reached via
25452 other DECLs and they can point to template types or other things
25453 that dwarf2out can't handle when done via dwarf2out_decl. */
25454 if (TREE_CODE (decl) != TYPE_DECL
25455 && TREE_CODE (decl) != PARM_DECL)
25457 tree save_fndecl = current_function_decl;
25458 if (TREE_CODE (decl) == FUNCTION_DECL)
25460 /* For nested functions, make sure we have DIEs for the parents first
25461 so that all nested DIEs are generated at the proper scope in the
25462 first shot. */
25463 tree context = decl_function_context (decl);
25464 if (context != NULL && lookup_decl_die (context) == NULL)
25466 current_function_decl = context;
25467 dwarf2out_decl (context);
25470 current_function_decl = decl;
25472 dwarf2out_decl (decl);
25473 if (TREE_CODE (decl) == FUNCTION_DECL)
25474 current_function_decl = save_fndecl;
25476 symtab->global_info_ready = save;
25479 /* Output debug information for global decl DECL. Called from
25480 toplev.c after compilation proper has finished. */
25482 static void
25483 dwarf2out_late_global_decl (tree decl)
25485 /* Fill-in any location information we were unable to determine
25486 on the first pass. */
25487 if (VAR_P (decl) && !POINTER_BOUNDS_P (decl))
25489 dw_die_ref die = lookup_decl_die (decl);
25491 /* We have to generate early debug late for LTO. */
25492 if (! die && in_lto_p)
25494 dwarf2out_decl (decl);
25495 die = lookup_decl_die (decl);
25498 if (die)
25500 /* We get called via the symtab code invoking late_global_decl
25501 for symbols that are optimized out. Do not add locations
25502 for those, except if they have a DECL_VALUE_EXPR, in which case
25503 they are relevant for debuggers. */
25504 varpool_node *node = varpool_node::get (decl);
25505 if ((! node || ! node->definition) && ! DECL_HAS_VALUE_EXPR_P (decl))
25506 tree_add_const_value_attribute_for_decl (die, decl);
25507 else
25508 add_location_or_const_value_attribute (die, decl, false);
25513 /* Output debug information for type decl DECL. Called from toplev.c
25514 and from language front ends (to record built-in types). */
25515 static void
25516 dwarf2out_type_decl (tree decl, int local)
25518 if (!local)
25520 set_early_dwarf s;
25521 dwarf2out_decl (decl);
25525 /* Output debug information for imported module or decl DECL.
25526 NAME is non-NULL name in the lexical block if the decl has been renamed.
25527 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
25528 that DECL belongs to.
25529 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
25530 static void
25531 dwarf2out_imported_module_or_decl_1 (tree decl,
25532 tree name,
25533 tree lexical_block,
25534 dw_die_ref lexical_block_die)
25536 expanded_location xloc;
25537 dw_die_ref imported_die = NULL;
25538 dw_die_ref at_import_die;
25540 if (TREE_CODE (decl) == IMPORTED_DECL)
25542 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
25543 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
25544 gcc_assert (decl);
25546 else
25547 xloc = expand_location (input_location);
25549 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
25551 at_import_die = force_type_die (TREE_TYPE (decl));
25552 /* For namespace N { typedef void T; } using N::T; base_type_die
25553 returns NULL, but DW_TAG_imported_declaration requires
25554 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
25555 if (!at_import_die)
25557 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
25558 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
25559 at_import_die = lookup_type_die (TREE_TYPE (decl));
25560 gcc_assert (at_import_die);
25563 else
25565 at_import_die = lookup_decl_die (decl);
25566 if (!at_import_die)
25568 /* If we're trying to avoid duplicate debug info, we may not have
25569 emitted the member decl for this field. Emit it now. */
25570 if (TREE_CODE (decl) == FIELD_DECL)
25572 tree type = DECL_CONTEXT (decl);
25574 if (TYPE_CONTEXT (type)
25575 && TYPE_P (TYPE_CONTEXT (type))
25576 && !should_emit_struct_debug (TYPE_CONTEXT (type),
25577 DINFO_USAGE_DIR_USE))
25578 return;
25579 gen_type_die_for_member (type, decl,
25580 get_context_die (TYPE_CONTEXT (type)));
25582 if (TREE_CODE (decl) == NAMELIST_DECL)
25583 at_import_die = gen_namelist_decl (DECL_NAME (decl),
25584 get_context_die (DECL_CONTEXT (decl)),
25585 NULL_TREE);
25586 else
25587 at_import_die = force_decl_die (decl);
25591 if (TREE_CODE (decl) == NAMESPACE_DECL)
25593 if (dwarf_version >= 3 || !dwarf_strict)
25594 imported_die = new_die (DW_TAG_imported_module,
25595 lexical_block_die,
25596 lexical_block);
25597 else
25598 return;
25600 else
25601 imported_die = new_die (DW_TAG_imported_declaration,
25602 lexical_block_die,
25603 lexical_block);
25605 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
25606 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
25607 if (debug_column_info && xloc.column)
25608 add_AT_unsigned (imported_die, DW_AT_decl_column, xloc.column);
25609 if (name)
25610 add_AT_string (imported_die, DW_AT_name,
25611 IDENTIFIER_POINTER (name));
25612 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
25615 /* Output debug information for imported module or decl DECL.
25616 NAME is non-NULL name in context if the decl has been renamed.
25617 CHILD is true if decl is one of the renamed decls as part of
25618 importing whole module.
25619 IMPLICIT is set if this hook is called for an implicit import
25620 such as inline namespace. */
25622 static void
25623 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
25624 bool child, bool implicit)
25626 /* dw_die_ref at_import_die; */
25627 dw_die_ref scope_die;
25629 if (debug_info_level <= DINFO_LEVEL_TERSE)
25630 return;
25632 gcc_assert (decl);
25634 /* For DWARF5, just DW_AT_export_symbols on the DW_TAG_namespace
25635 should be enough, for DWARF4 and older even if we emit as extension
25636 DW_AT_export_symbols add the implicit DW_TAG_imported_module anyway
25637 for the benefit of consumers unaware of DW_AT_export_symbols. */
25638 if (implicit
25639 && dwarf_version >= 5
25640 && lang_hooks.decls.decl_dwarf_attribute (decl,
25641 DW_AT_export_symbols) == 1)
25642 return;
25644 set_early_dwarf s;
25646 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
25647 We need decl DIE for reference and scope die. First, get DIE for the decl
25648 itself. */
25650 /* Get the scope die for decl context. Use comp_unit_die for global module
25651 or decl. If die is not found for non globals, force new die. */
25652 if (context
25653 && TYPE_P (context)
25654 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
25655 return;
25657 scope_die = get_context_die (context);
25659 if (child)
25661 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
25662 there is nothing we can do, here. */
25663 if (dwarf_version < 3 && dwarf_strict)
25664 return;
25666 gcc_assert (scope_die->die_child);
25667 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
25668 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
25669 scope_die = scope_die->die_child;
25672 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
25673 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
25676 /* Output debug information for namelists. */
25678 static dw_die_ref
25679 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
25681 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
25682 tree value;
25683 unsigned i;
25685 if (debug_info_level <= DINFO_LEVEL_TERSE)
25686 return NULL;
25688 gcc_assert (scope_die != NULL);
25689 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
25690 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
25692 /* If there are no item_decls, we have a nondefining namelist, e.g.
25693 with USE association; hence, set DW_AT_declaration. */
25694 if (item_decls == NULL_TREE)
25696 add_AT_flag (nml_die, DW_AT_declaration, 1);
25697 return nml_die;
25700 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
25702 nml_item_ref_die = lookup_decl_die (value);
25703 if (!nml_item_ref_die)
25704 nml_item_ref_die = force_decl_die (value);
25706 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
25707 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
25709 return nml_die;
25713 /* Write the debugging output for DECL and return the DIE. */
25715 static void
25716 dwarf2out_decl (tree decl)
25718 dw_die_ref context_die = comp_unit_die ();
25720 switch (TREE_CODE (decl))
25722 case ERROR_MARK:
25723 return;
25725 case FUNCTION_DECL:
25726 /* If we're a nested function, initially use a parent of NULL; if we're
25727 a plain function, this will be fixed up in decls_for_scope. If
25728 we're a method, it will be ignored, since we already have a DIE. */
25729 if (decl_function_context (decl)
25730 /* But if we're in terse mode, we don't care about scope. */
25731 && debug_info_level > DINFO_LEVEL_TERSE)
25732 context_die = NULL;
25733 break;
25735 case VAR_DECL:
25736 /* For local statics lookup proper context die. */
25737 if (local_function_static (decl))
25738 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25740 /* If we are in terse mode, don't generate any DIEs to represent any
25741 variable declarations or definitions. */
25742 if (debug_info_level <= DINFO_LEVEL_TERSE)
25743 return;
25744 break;
25746 case CONST_DECL:
25747 if (debug_info_level <= DINFO_LEVEL_TERSE)
25748 return;
25749 if (!is_fortran () && !is_ada ())
25750 return;
25751 if (TREE_STATIC (decl) && decl_function_context (decl))
25752 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25753 break;
25755 case NAMESPACE_DECL:
25756 case IMPORTED_DECL:
25757 if (debug_info_level <= DINFO_LEVEL_TERSE)
25758 return;
25759 if (lookup_decl_die (decl) != NULL)
25760 return;
25761 break;
25763 case TYPE_DECL:
25764 /* Don't emit stubs for types unless they are needed by other DIEs. */
25765 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
25766 return;
25768 /* Don't bother trying to generate any DIEs to represent any of the
25769 normal built-in types for the language we are compiling. */
25770 if (DECL_IS_BUILTIN (decl))
25771 return;
25773 /* If we are in terse mode, don't generate any DIEs for types. */
25774 if (debug_info_level <= DINFO_LEVEL_TERSE)
25775 return;
25777 /* If we're a function-scope tag, initially use a parent of NULL;
25778 this will be fixed up in decls_for_scope. */
25779 if (decl_function_context (decl))
25780 context_die = NULL;
25782 break;
25784 case NAMELIST_DECL:
25785 break;
25787 default:
25788 return;
25791 gen_decl_die (decl, NULL, NULL, context_die);
25793 if (flag_checking)
25795 dw_die_ref die = lookup_decl_die (decl);
25796 if (die)
25797 check_die (die);
25801 /* Write the debugging output for DECL. */
25803 static void
25804 dwarf2out_function_decl (tree decl)
25806 dwarf2out_decl (decl);
25807 call_arg_locations = NULL;
25808 call_arg_loc_last = NULL;
25809 call_site_count = -1;
25810 tail_call_site_count = -1;
25811 decl_loc_table->empty ();
25812 cached_dw_loc_list_table->empty ();
25815 /* Output a marker (i.e. a label) for the beginning of the generated code for
25816 a lexical block. */
25818 static void
25819 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
25820 unsigned int blocknum)
25822 switch_to_section (current_function_section ());
25823 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
25826 /* Output a marker (i.e. a label) for the end of the generated code for a
25827 lexical block. */
25829 static void
25830 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
25832 switch_to_section (current_function_section ());
25833 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
25836 /* Returns nonzero if it is appropriate not to emit any debugging
25837 information for BLOCK, because it doesn't contain any instructions.
25839 Don't allow this for blocks with nested functions or local classes
25840 as we would end up with orphans, and in the presence of scheduling
25841 we may end up calling them anyway. */
25843 static bool
25844 dwarf2out_ignore_block (const_tree block)
25846 tree decl;
25847 unsigned int i;
25849 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
25850 if (TREE_CODE (decl) == FUNCTION_DECL
25851 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
25852 return 0;
25853 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
25855 decl = BLOCK_NONLOCALIZED_VAR (block, i);
25856 if (TREE_CODE (decl) == FUNCTION_DECL
25857 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
25858 return 0;
25861 return 1;
25864 /* Hash table routines for file_hash. */
25866 bool
25867 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
25869 return filename_cmp (p1->filename, p2) == 0;
25872 hashval_t
25873 dwarf_file_hasher::hash (dwarf_file_data *p)
25875 return htab_hash_string (p->filename);
25878 /* Lookup FILE_NAME (in the list of filenames that we know about here in
25879 dwarf2out.c) and return its "index". The index of each (known) filename is
25880 just a unique number which is associated with only that one filename. We
25881 need such numbers for the sake of generating labels (in the .debug_sfnames
25882 section) and references to those files numbers (in the .debug_srcinfo
25883 and .debug_macinfo sections). If the filename given as an argument is not
25884 found in our current list, add it to the list and assign it the next
25885 available unique index number. */
25887 static struct dwarf_file_data *
25888 lookup_filename (const char *file_name)
25890 struct dwarf_file_data * created;
25892 if (!file_name)
25893 return NULL;
25895 dwarf_file_data **slot
25896 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
25897 INSERT);
25898 if (*slot)
25899 return *slot;
25901 created = ggc_alloc<dwarf_file_data> ();
25902 created->filename = file_name;
25903 created->emitted_number = 0;
25904 *slot = created;
25905 return created;
25908 /* If the assembler will construct the file table, then translate the compiler
25909 internal file table number into the assembler file table number, and emit
25910 a .file directive if we haven't already emitted one yet. The file table
25911 numbers are different because we prune debug info for unused variables and
25912 types, which may include filenames. */
25914 static int
25915 maybe_emit_file (struct dwarf_file_data * fd)
25917 if (! fd->emitted_number)
25919 if (last_emitted_file)
25920 fd->emitted_number = last_emitted_file->emitted_number + 1;
25921 else
25922 fd->emitted_number = 1;
25923 last_emitted_file = fd;
25925 if (DWARF2_ASM_LINE_DEBUG_INFO)
25927 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
25928 output_quoted_string (asm_out_file,
25929 remap_debug_filename (fd->filename));
25930 fputc ('\n', asm_out_file);
25934 return fd->emitted_number;
25937 /* Schedule generation of a DW_AT_const_value attribute to DIE.
25938 That generation should happen after function debug info has been
25939 generated. The value of the attribute is the constant value of ARG. */
25941 static void
25942 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
25944 die_arg_entry entry;
25946 if (!die || !arg)
25947 return;
25949 gcc_assert (early_dwarf);
25951 if (!tmpl_value_parm_die_table)
25952 vec_alloc (tmpl_value_parm_die_table, 32);
25954 entry.die = die;
25955 entry.arg = arg;
25956 vec_safe_push (tmpl_value_parm_die_table, entry);
25959 /* Return TRUE if T is an instance of generic type, FALSE
25960 otherwise. */
25962 static bool
25963 generic_type_p (tree t)
25965 if (t == NULL_TREE || !TYPE_P (t))
25966 return false;
25967 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
25970 /* Schedule the generation of the generic parameter dies for the
25971 instance of generic type T. The proper generation itself is later
25972 done by gen_scheduled_generic_parms_dies. */
25974 static void
25975 schedule_generic_params_dies_gen (tree t)
25977 if (!generic_type_p (t))
25978 return;
25980 gcc_assert (early_dwarf);
25982 if (!generic_type_instances)
25983 vec_alloc (generic_type_instances, 256);
25985 vec_safe_push (generic_type_instances, t);
25988 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
25989 by append_entry_to_tmpl_value_parm_die_table. This function must
25990 be called after function DIEs have been generated. */
25992 static void
25993 gen_remaining_tmpl_value_param_die_attribute (void)
25995 if (tmpl_value_parm_die_table)
25997 unsigned i, j;
25998 die_arg_entry *e;
26000 /* We do this in two phases - first get the cases we can
26001 handle during early-finish, preserving those we cannot
26002 (containing symbolic constants where we don't yet know
26003 whether we are going to output the referenced symbols).
26004 For those we try again at late-finish. */
26005 j = 0;
26006 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
26008 if (!tree_add_const_value_attribute (e->die, e->arg))
26010 dw_loc_descr_ref loc = NULL;
26011 if (! early_dwarf
26012 && (dwarf_version >= 5 || !dwarf_strict))
26013 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
26014 if (loc)
26015 add_AT_loc (e->die, DW_AT_location, loc);
26016 else
26017 (*tmpl_value_parm_die_table)[j++] = *e;
26020 tmpl_value_parm_die_table->truncate (j);
26024 /* Generate generic parameters DIEs for instances of generic types
26025 that have been previously scheduled by
26026 schedule_generic_params_dies_gen. This function must be called
26027 after all the types of the CU have been laid out. */
26029 static void
26030 gen_scheduled_generic_parms_dies (void)
26032 unsigned i;
26033 tree t;
26035 if (!generic_type_instances)
26036 return;
26038 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
26039 if (COMPLETE_TYPE_P (t))
26040 gen_generic_params_dies (t);
26042 generic_type_instances = NULL;
26046 /* Replace DW_AT_name for the decl with name. */
26048 static void
26049 dwarf2out_set_name (tree decl, tree name)
26051 dw_die_ref die;
26052 dw_attr_node *attr;
26053 const char *dname;
26055 die = TYPE_SYMTAB_DIE (decl);
26056 if (!die)
26057 return;
26059 dname = dwarf2_name (name, 0);
26060 if (!dname)
26061 return;
26063 attr = get_AT (die, DW_AT_name);
26064 if (attr)
26066 struct indirect_string_node *node;
26068 node = find_AT_string (dname);
26069 /* replace the string. */
26070 attr->dw_attr_val.v.val_str = node;
26073 else
26074 add_name_attribute (die, dname);
26077 /* True if before or during processing of the first function being emitted. */
26078 static bool in_first_function_p = true;
26079 /* True if loc_note during dwarf2out_var_location call might still be
26080 before first real instruction at address equal to .Ltext0. */
26081 static bool maybe_at_text_label_p = true;
26082 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
26083 static unsigned int first_loclabel_num_not_at_text_label;
26085 /* Called by the final INSN scan whenever we see a var location. We
26086 use it to drop labels in the right places, and throw the location in
26087 our lookup table. */
26089 static void
26090 dwarf2out_var_location (rtx_insn *loc_note)
26092 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
26093 struct var_loc_node *newloc;
26094 rtx_insn *next_real, *next_note;
26095 rtx_insn *call_insn = NULL;
26096 static const char *last_label;
26097 static const char *last_postcall_label;
26098 static bool last_in_cold_section_p;
26099 static rtx_insn *expected_next_loc_note;
26100 tree decl;
26101 bool var_loc_p;
26103 if (!NOTE_P (loc_note))
26105 if (CALL_P (loc_note))
26107 call_site_count++;
26108 if (SIBLING_CALL_P (loc_note))
26109 tail_call_site_count++;
26110 if (optimize == 0 && !flag_var_tracking)
26112 /* When the var-tracking pass is not running, there is no note
26113 for indirect calls whose target is compile-time known. In this
26114 case, process such calls specifically so that we generate call
26115 sites for them anyway. */
26116 rtx x = PATTERN (loc_note);
26117 if (GET_CODE (x) == PARALLEL)
26118 x = XVECEXP (x, 0, 0);
26119 if (GET_CODE (x) == SET)
26120 x = SET_SRC (x);
26121 if (GET_CODE (x) == CALL)
26122 x = XEXP (x, 0);
26123 if (!MEM_P (x)
26124 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
26125 || !SYMBOL_REF_DECL (XEXP (x, 0))
26126 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
26127 != FUNCTION_DECL))
26129 call_insn = loc_note;
26130 loc_note = NULL;
26131 var_loc_p = false;
26133 next_real = next_real_insn (call_insn);
26134 next_note = NULL;
26135 cached_next_real_insn = NULL;
26136 goto create_label;
26140 return;
26143 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
26144 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
26145 return;
26147 /* Optimize processing a large consecutive sequence of location
26148 notes so we don't spend too much time in next_real_insn. If the
26149 next insn is another location note, remember the next_real_insn
26150 calculation for next time. */
26151 next_real = cached_next_real_insn;
26152 if (next_real)
26154 if (expected_next_loc_note != loc_note)
26155 next_real = NULL;
26158 next_note = NEXT_INSN (loc_note);
26159 if (! next_note
26160 || next_note->deleted ()
26161 || ! NOTE_P (next_note)
26162 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
26163 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
26164 next_note = NULL;
26166 if (! next_real)
26167 next_real = next_real_insn (loc_note);
26169 if (next_note)
26171 expected_next_loc_note = next_note;
26172 cached_next_real_insn = next_real;
26174 else
26175 cached_next_real_insn = NULL;
26177 /* If there are no instructions which would be affected by this note,
26178 don't do anything. */
26179 if (var_loc_p
26180 && next_real == NULL_RTX
26181 && !NOTE_DURING_CALL_P (loc_note))
26182 return;
26184 create_label:
26186 if (next_real == NULL_RTX)
26187 next_real = get_last_insn ();
26189 /* If there were any real insns between note we processed last time
26190 and this note (or if it is the first note), clear
26191 last_{,postcall_}label so that they are not reused this time. */
26192 if (last_var_location_insn == NULL_RTX
26193 || last_var_location_insn != next_real
26194 || last_in_cold_section_p != in_cold_section_p)
26196 last_label = NULL;
26197 last_postcall_label = NULL;
26200 if (var_loc_p)
26202 decl = NOTE_VAR_LOCATION_DECL (loc_note);
26203 newloc = add_var_loc_to_decl (decl, loc_note,
26204 NOTE_DURING_CALL_P (loc_note)
26205 ? last_postcall_label : last_label);
26206 if (newloc == NULL)
26207 return;
26209 else
26211 decl = NULL_TREE;
26212 newloc = NULL;
26215 /* If there were no real insns between note we processed last time
26216 and this note, use the label we emitted last time. Otherwise
26217 create a new label and emit it. */
26218 if (last_label == NULL)
26220 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
26221 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
26222 loclabel_num++;
26223 last_label = ggc_strdup (loclabel);
26224 /* See if loclabel might be equal to .Ltext0. If yes,
26225 bump first_loclabel_num_not_at_text_label. */
26226 if (!have_multiple_function_sections
26227 && in_first_function_p
26228 && maybe_at_text_label_p)
26230 static rtx_insn *last_start;
26231 rtx_insn *insn;
26232 for (insn = loc_note; insn; insn = previous_insn (insn))
26233 if (insn == last_start)
26234 break;
26235 else if (!NONDEBUG_INSN_P (insn))
26236 continue;
26237 else
26239 rtx body = PATTERN (insn);
26240 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
26241 continue;
26242 /* Inline asm could occupy zero bytes. */
26243 else if (GET_CODE (body) == ASM_INPUT
26244 || asm_noperands (body) >= 0)
26245 continue;
26246 #ifdef HAVE_attr_length
26247 else if (get_attr_min_length (insn) == 0)
26248 continue;
26249 #endif
26250 else
26252 /* Assume insn has non-zero length. */
26253 maybe_at_text_label_p = false;
26254 break;
26257 if (maybe_at_text_label_p)
26259 last_start = loc_note;
26260 first_loclabel_num_not_at_text_label = loclabel_num;
26265 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
26266 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
26268 if (!var_loc_p)
26270 struct call_arg_loc_node *ca_loc
26271 = ggc_cleared_alloc<call_arg_loc_node> ();
26272 rtx_insn *prev
26273 = loc_note != NULL_RTX ? prev_real_insn (loc_note) : call_insn;
26275 ca_loc->call_arg_loc_note = loc_note;
26276 ca_loc->next = NULL;
26277 ca_loc->label = last_label;
26278 gcc_assert (prev
26279 && (CALL_P (prev)
26280 || (NONJUMP_INSN_P (prev)
26281 && GET_CODE (PATTERN (prev)) == SEQUENCE
26282 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
26283 if (!CALL_P (prev))
26284 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
26285 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
26287 /* Look for a SYMBOL_REF in the "prev" instruction. */
26288 rtx x = get_call_rtx_from (PATTERN (prev));
26289 if (x)
26291 /* Try to get the call symbol, if any. */
26292 if (MEM_P (XEXP (x, 0)))
26293 x = XEXP (x, 0);
26294 /* First, look for a memory access to a symbol_ref. */
26295 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
26296 && SYMBOL_REF_DECL (XEXP (x, 0))
26297 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
26298 ca_loc->symbol_ref = XEXP (x, 0);
26299 /* Otherwise, look at a compile-time known user-level function
26300 declaration. */
26301 else if (MEM_P (x)
26302 && MEM_EXPR (x)
26303 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
26304 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
26307 ca_loc->block = insn_scope (prev);
26308 if (call_arg_locations)
26309 call_arg_loc_last->next = ca_loc;
26310 else
26311 call_arg_locations = ca_loc;
26312 call_arg_loc_last = ca_loc;
26314 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
26315 newloc->label = last_label;
26316 else
26318 if (!last_postcall_label)
26320 sprintf (loclabel, "%s-1", last_label);
26321 last_postcall_label = ggc_strdup (loclabel);
26323 newloc->label = last_postcall_label;
26326 last_var_location_insn = next_real;
26327 last_in_cold_section_p = in_cold_section_p;
26330 /* Called from finalize_size_functions for size functions so that their body
26331 can be encoded in the debug info to describe the layout of variable-length
26332 structures. */
26334 static void
26335 dwarf2out_size_function (tree decl)
26337 function_to_dwarf_procedure (decl);
26340 /* Note in one location list that text section has changed. */
26343 var_location_switch_text_section_1 (var_loc_list **slot, void *)
26345 var_loc_list *list = *slot;
26346 if (list->first)
26347 list->last_before_switch
26348 = list->last->next ? list->last->next : list->last;
26349 return 1;
26352 /* Note in all location lists that text section has changed. */
26354 static void
26355 var_location_switch_text_section (void)
26357 if (decl_loc_table == NULL)
26358 return;
26360 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
26363 /* Create a new line number table. */
26365 static dw_line_info_table *
26366 new_line_info_table (void)
26368 dw_line_info_table *table;
26370 table = ggc_cleared_alloc<dw_line_info_table> ();
26371 table->file_num = 1;
26372 table->line_num = 1;
26373 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
26375 return table;
26378 /* Lookup the "current" table into which we emit line info, so
26379 that we don't have to do it for every source line. */
26381 static void
26382 set_cur_line_info_table (section *sec)
26384 dw_line_info_table *table;
26386 if (sec == text_section)
26387 table = text_section_line_info;
26388 else if (sec == cold_text_section)
26390 table = cold_text_section_line_info;
26391 if (!table)
26393 cold_text_section_line_info = table = new_line_info_table ();
26394 table->end_label = cold_end_label;
26397 else
26399 const char *end_label;
26401 if (crtl->has_bb_partition)
26403 if (in_cold_section_p)
26404 end_label = crtl->subsections.cold_section_end_label;
26405 else
26406 end_label = crtl->subsections.hot_section_end_label;
26408 else
26410 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26411 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
26412 current_function_funcdef_no);
26413 end_label = ggc_strdup (label);
26416 table = new_line_info_table ();
26417 table->end_label = end_label;
26419 vec_safe_push (separate_line_info, table);
26422 if (DWARF2_ASM_LINE_DEBUG_INFO)
26423 table->is_stmt = (cur_line_info_table
26424 ? cur_line_info_table->is_stmt
26425 : DWARF_LINE_DEFAULT_IS_STMT_START);
26426 cur_line_info_table = table;
26430 /* We need to reset the locations at the beginning of each
26431 function. We can't do this in the end_function hook, because the
26432 declarations that use the locations won't have been output when
26433 that hook is called. Also compute have_multiple_function_sections here. */
26435 static void
26436 dwarf2out_begin_function (tree fun)
26438 section *sec = function_section (fun);
26440 if (sec != text_section)
26441 have_multiple_function_sections = true;
26443 if (crtl->has_bb_partition && !cold_text_section)
26445 gcc_assert (current_function_decl == fun);
26446 cold_text_section = unlikely_text_section ();
26447 switch_to_section (cold_text_section);
26448 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
26449 switch_to_section (sec);
26452 dwarf2out_note_section_used ();
26453 call_site_count = 0;
26454 tail_call_site_count = 0;
26456 set_cur_line_info_table (sec);
26459 /* Helper function of dwarf2out_end_function, called only after emitting
26460 the very first function into assembly. Check if some .debug_loc range
26461 might end with a .LVL* label that could be equal to .Ltext0.
26462 In that case we must force using absolute addresses in .debug_loc ranges,
26463 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
26464 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
26465 list terminator.
26466 Set have_multiple_function_sections to true in that case and
26467 terminate htab traversal. */
26470 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
26472 var_loc_list *entry = *slot;
26473 struct var_loc_node *node;
26475 node = entry->first;
26476 if (node && node->next && node->next->label)
26478 unsigned int i;
26479 const char *label = node->next->label;
26480 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
26482 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
26484 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
26485 if (strcmp (label, loclabel) == 0)
26487 have_multiple_function_sections = true;
26488 return 0;
26492 return 1;
26495 /* Hook called after emitting a function into assembly.
26496 This does something only for the very first function emitted. */
26498 static void
26499 dwarf2out_end_function (unsigned int)
26501 if (in_first_function_p
26502 && !have_multiple_function_sections
26503 && first_loclabel_num_not_at_text_label
26504 && decl_loc_table)
26505 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
26506 in_first_function_p = false;
26507 maybe_at_text_label_p = false;
26510 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
26511 front-ends register a translation unit even before dwarf2out_init is
26512 called. */
26513 static tree main_translation_unit = NULL_TREE;
26515 /* Hook called by front-ends after they built their main translation unit.
26516 Associate comp_unit_die to UNIT. */
26518 static void
26519 dwarf2out_register_main_translation_unit (tree unit)
26521 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
26522 && main_translation_unit == NULL_TREE);
26523 main_translation_unit = unit;
26524 /* If dwarf2out_init has not been called yet, it will perform the association
26525 itself looking at main_translation_unit. */
26526 if (decl_die_table != NULL)
26527 equate_decl_number_to_die (unit, comp_unit_die ());
26530 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
26532 static void
26533 push_dw_line_info_entry (dw_line_info_table *table,
26534 enum dw_line_info_opcode opcode, unsigned int val)
26536 dw_line_info_entry e;
26537 e.opcode = opcode;
26538 e.val = val;
26539 vec_safe_push (table->entries, e);
26542 /* Output a label to mark the beginning of a source code line entry
26543 and record information relating to this source line, in
26544 'line_info_table' for later output of the .debug_line section. */
26545 /* ??? The discriminator parameter ought to be unsigned. */
26547 static void
26548 dwarf2out_source_line (unsigned int line, unsigned int column,
26549 const char *filename,
26550 int discriminator, bool is_stmt)
26552 unsigned int file_num;
26553 dw_line_info_table *table;
26555 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
26556 return;
26558 /* The discriminator column was added in dwarf4. Simplify the below
26559 by simply removing it if we're not supposed to output it. */
26560 if (dwarf_version < 4 && dwarf_strict)
26561 discriminator = 0;
26563 if (!debug_column_info)
26564 column = 0;
26566 table = cur_line_info_table;
26567 file_num = maybe_emit_file (lookup_filename (filename));
26569 /* ??? TODO: Elide duplicate line number entries. Traditionally,
26570 the debugger has used the second (possibly duplicate) line number
26571 at the beginning of the function to mark the end of the prologue.
26572 We could eliminate any other duplicates within the function. For
26573 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
26574 that second line number entry. */
26575 /* Recall that this end-of-prologue indication is *not* the same thing
26576 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
26577 to which the hook corresponds, follows the last insn that was
26578 emitted by gen_prologue. What we need is to precede the first insn
26579 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
26580 insn that corresponds to something the user wrote. These may be
26581 very different locations once scheduling is enabled. */
26583 if (0 && file_num == table->file_num
26584 && line == table->line_num
26585 && column == table->column_num
26586 && discriminator == table->discrim_num
26587 && is_stmt == table->is_stmt)
26588 return;
26590 switch_to_section (current_function_section ());
26592 /* If requested, emit something human-readable. */
26593 if (flag_debug_asm)
26595 if (debug_column_info)
26596 fprintf (asm_out_file, "\t%s %s:%d:%d\n", ASM_COMMENT_START,
26597 filename, line, column);
26598 else
26599 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
26600 filename, line);
26603 if (DWARF2_ASM_LINE_DEBUG_INFO)
26605 /* Emit the .loc directive understood by GNU as. */
26606 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
26607 file_num, line, is_stmt, discriminator */
26608 fputs ("\t.loc ", asm_out_file);
26609 fprint_ul (asm_out_file, file_num);
26610 putc (' ', asm_out_file);
26611 fprint_ul (asm_out_file, line);
26612 putc (' ', asm_out_file);
26613 if (debug_column_info)
26614 fprint_ul (asm_out_file, column);
26615 else
26616 putc ('0', asm_out_file);
26618 if (is_stmt != table->is_stmt)
26620 fputs (" is_stmt ", asm_out_file);
26621 putc (is_stmt ? '1' : '0', asm_out_file);
26623 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
26625 gcc_assert (discriminator > 0);
26626 fputs (" discriminator ", asm_out_file);
26627 fprint_ul (asm_out_file, (unsigned long) discriminator);
26629 putc ('\n', asm_out_file);
26631 else
26633 unsigned int label_num = ++line_info_label_num;
26635 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
26637 push_dw_line_info_entry (table, LI_set_address, label_num);
26638 if (file_num != table->file_num)
26639 push_dw_line_info_entry (table, LI_set_file, file_num);
26640 if (discriminator != table->discrim_num)
26641 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
26642 if (is_stmt != table->is_stmt)
26643 push_dw_line_info_entry (table, LI_negate_stmt, 0);
26644 push_dw_line_info_entry (table, LI_set_line, line);
26645 if (debug_column_info)
26646 push_dw_line_info_entry (table, LI_set_column, column);
26649 table->file_num = file_num;
26650 table->line_num = line;
26651 table->column_num = column;
26652 table->discrim_num = discriminator;
26653 table->is_stmt = is_stmt;
26654 table->in_use = true;
26657 /* Record the beginning of a new source file. */
26659 static void
26660 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
26662 if (flag_eliminate_dwarf2_dups)
26664 /* Record the beginning of the file for break_out_includes. */
26665 dw_die_ref bincl_die;
26667 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
26668 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
26671 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26673 macinfo_entry e;
26674 e.code = DW_MACINFO_start_file;
26675 e.lineno = lineno;
26676 e.info = ggc_strdup (filename);
26677 vec_safe_push (macinfo_table, e);
26681 /* Record the end of a source file. */
26683 static void
26684 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
26686 if (flag_eliminate_dwarf2_dups)
26687 /* Record the end of the file for break_out_includes. */
26688 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
26690 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26692 macinfo_entry e;
26693 e.code = DW_MACINFO_end_file;
26694 e.lineno = lineno;
26695 e.info = NULL;
26696 vec_safe_push (macinfo_table, e);
26700 /* Called from debug_define in toplev.c. The `buffer' parameter contains
26701 the tail part of the directive line, i.e. the part which is past the
26702 initial whitespace, #, whitespace, directive-name, whitespace part. */
26704 static void
26705 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
26706 const char *buffer ATTRIBUTE_UNUSED)
26708 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26710 macinfo_entry e;
26711 /* Insert a dummy first entry to be able to optimize the whole
26712 predefined macro block using DW_MACRO_import. */
26713 if (macinfo_table->is_empty () && lineno <= 1)
26715 e.code = 0;
26716 e.lineno = 0;
26717 e.info = NULL;
26718 vec_safe_push (macinfo_table, e);
26720 e.code = DW_MACINFO_define;
26721 e.lineno = lineno;
26722 e.info = ggc_strdup (buffer);
26723 vec_safe_push (macinfo_table, e);
26727 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
26728 the tail part of the directive line, i.e. the part which is past the
26729 initial whitespace, #, whitespace, directive-name, whitespace part. */
26731 static void
26732 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
26733 const char *buffer ATTRIBUTE_UNUSED)
26735 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26737 macinfo_entry e;
26738 /* Insert a dummy first entry to be able to optimize the whole
26739 predefined macro block using DW_MACRO_import. */
26740 if (macinfo_table->is_empty () && lineno <= 1)
26742 e.code = 0;
26743 e.lineno = 0;
26744 e.info = NULL;
26745 vec_safe_push (macinfo_table, e);
26747 e.code = DW_MACINFO_undef;
26748 e.lineno = lineno;
26749 e.info = ggc_strdup (buffer);
26750 vec_safe_push (macinfo_table, e);
26754 /* Helpers to manipulate hash table of CUs. */
26756 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
26758 static inline hashval_t hash (const macinfo_entry *);
26759 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
26762 inline hashval_t
26763 macinfo_entry_hasher::hash (const macinfo_entry *entry)
26765 return htab_hash_string (entry->info);
26768 inline bool
26769 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
26770 const macinfo_entry *entry2)
26772 return !strcmp (entry1->info, entry2->info);
26775 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
26777 /* Output a single .debug_macinfo entry. */
26779 static void
26780 output_macinfo_op (macinfo_entry *ref)
26782 int file_num;
26783 size_t len;
26784 struct indirect_string_node *node;
26785 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26786 struct dwarf_file_data *fd;
26788 switch (ref->code)
26790 case DW_MACINFO_start_file:
26791 fd = lookup_filename (ref->info);
26792 file_num = maybe_emit_file (fd);
26793 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
26794 dw2_asm_output_data_uleb128 (ref->lineno,
26795 "Included from line number %lu",
26796 (unsigned long) ref->lineno);
26797 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
26798 break;
26799 case DW_MACINFO_end_file:
26800 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
26801 break;
26802 case DW_MACINFO_define:
26803 case DW_MACINFO_undef:
26804 len = strlen (ref->info) + 1;
26805 if (!dwarf_strict
26806 && len > DWARF_OFFSET_SIZE
26807 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
26808 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
26810 ref->code = ref->code == DW_MACINFO_define
26811 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
26812 output_macinfo_op (ref);
26813 return;
26815 dw2_asm_output_data (1, ref->code,
26816 ref->code == DW_MACINFO_define
26817 ? "Define macro" : "Undefine macro");
26818 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
26819 (unsigned long) ref->lineno);
26820 dw2_asm_output_nstring (ref->info, -1, "The macro");
26821 break;
26822 case DW_MACRO_define_strp:
26823 case DW_MACRO_undef_strp:
26824 node = find_AT_string (ref->info);
26825 gcc_assert (node
26826 && (node->form == DW_FORM_strp
26827 || node->form == DW_FORM_GNU_str_index));
26828 dw2_asm_output_data (1, ref->code,
26829 ref->code == DW_MACRO_define_strp
26830 ? "Define macro strp"
26831 : "Undefine macro strp");
26832 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
26833 (unsigned long) ref->lineno);
26834 if (node->form == DW_FORM_strp)
26835 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
26836 debug_str_section, "The macro: \"%s\"",
26837 ref->info);
26838 else
26839 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
26840 ref->info);
26841 break;
26842 case DW_MACRO_import:
26843 dw2_asm_output_data (1, ref->code, "Import");
26844 ASM_GENERATE_INTERNAL_LABEL (label,
26845 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
26846 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
26847 break;
26848 default:
26849 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
26850 ASM_COMMENT_START, (unsigned long) ref->code);
26851 break;
26855 /* Attempt to make a sequence of define/undef macinfo ops shareable with
26856 other compilation unit .debug_macinfo sections. IDX is the first
26857 index of a define/undef, return the number of ops that should be
26858 emitted in a comdat .debug_macinfo section and emit
26859 a DW_MACRO_import entry referencing it.
26860 If the define/undef entry should be emitted normally, return 0. */
26862 static unsigned
26863 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
26864 macinfo_hash_type **macinfo_htab)
26866 macinfo_entry *first, *second, *cur, *inc;
26867 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
26868 unsigned char checksum[16];
26869 struct md5_ctx ctx;
26870 char *grp_name, *tail;
26871 const char *base;
26872 unsigned int i, count, encoded_filename_len, linebuf_len;
26873 macinfo_entry **slot;
26875 first = &(*macinfo_table)[idx];
26876 second = &(*macinfo_table)[idx + 1];
26878 /* Optimize only if there are at least two consecutive define/undef ops,
26879 and either all of them are before first DW_MACINFO_start_file
26880 with lineno {0,1} (i.e. predefined macro block), or all of them are
26881 in some included header file. */
26882 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
26883 return 0;
26884 if (vec_safe_is_empty (files))
26886 if (first->lineno > 1 || second->lineno > 1)
26887 return 0;
26889 else if (first->lineno == 0)
26890 return 0;
26892 /* Find the last define/undef entry that can be grouped together
26893 with first and at the same time compute md5 checksum of their
26894 codes, linenumbers and strings. */
26895 md5_init_ctx (&ctx);
26896 for (i = idx; macinfo_table->iterate (i, &cur); i++)
26897 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
26898 break;
26899 else if (vec_safe_is_empty (files) && cur->lineno > 1)
26900 break;
26901 else
26903 unsigned char code = cur->code;
26904 md5_process_bytes (&code, 1, &ctx);
26905 checksum_uleb128 (cur->lineno, &ctx);
26906 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
26908 md5_finish_ctx (&ctx, checksum);
26909 count = i - idx;
26911 /* From the containing include filename (if any) pick up just
26912 usable characters from its basename. */
26913 if (vec_safe_is_empty (files))
26914 base = "";
26915 else
26916 base = lbasename (files->last ().info);
26917 for (encoded_filename_len = 0, i = 0; base[i]; i++)
26918 if (ISIDNUM (base[i]) || base[i] == '.')
26919 encoded_filename_len++;
26920 /* Count . at the end. */
26921 if (encoded_filename_len)
26922 encoded_filename_len++;
26924 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
26925 linebuf_len = strlen (linebuf);
26927 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
26928 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
26929 + 16 * 2 + 1);
26930 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
26931 tail = grp_name + 4;
26932 if (encoded_filename_len)
26934 for (i = 0; base[i]; i++)
26935 if (ISIDNUM (base[i]) || base[i] == '.')
26936 *tail++ = base[i];
26937 *tail++ = '.';
26939 memcpy (tail, linebuf, linebuf_len);
26940 tail += linebuf_len;
26941 *tail++ = '.';
26942 for (i = 0; i < 16; i++)
26943 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
26945 /* Construct a macinfo_entry for DW_MACRO_import
26946 in the empty vector entry before the first define/undef. */
26947 inc = &(*macinfo_table)[idx - 1];
26948 inc->code = DW_MACRO_import;
26949 inc->lineno = 0;
26950 inc->info = ggc_strdup (grp_name);
26951 if (!*macinfo_htab)
26952 *macinfo_htab = new macinfo_hash_type (10);
26953 /* Avoid emitting duplicates. */
26954 slot = (*macinfo_htab)->find_slot (inc, INSERT);
26955 if (*slot != NULL)
26957 inc->code = 0;
26958 inc->info = NULL;
26959 /* If such an entry has been used before, just emit
26960 a DW_MACRO_import op. */
26961 inc = *slot;
26962 output_macinfo_op (inc);
26963 /* And clear all macinfo_entry in the range to avoid emitting them
26964 in the second pass. */
26965 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
26967 cur->code = 0;
26968 cur->info = NULL;
26971 else
26973 *slot = inc;
26974 inc->lineno = (*macinfo_htab)->elements ();
26975 output_macinfo_op (inc);
26977 return count;
26980 /* Save any strings needed by the macinfo table in the debug str
26981 table. All strings must be collected into the table by the time
26982 index_string is called. */
26984 static void
26985 save_macinfo_strings (void)
26987 unsigned len;
26988 unsigned i;
26989 macinfo_entry *ref;
26991 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
26993 switch (ref->code)
26995 /* Match the logic in output_macinfo_op to decide on
26996 indirect strings. */
26997 case DW_MACINFO_define:
26998 case DW_MACINFO_undef:
26999 len = strlen (ref->info) + 1;
27000 if (!dwarf_strict
27001 && len > DWARF_OFFSET_SIZE
27002 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
27003 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
27004 set_indirect_string (find_AT_string (ref->info));
27005 break;
27006 case DW_MACRO_define_strp:
27007 case DW_MACRO_undef_strp:
27008 set_indirect_string (find_AT_string (ref->info));
27009 break;
27010 default:
27011 break;
27016 /* Output macinfo section(s). */
27018 static void
27019 output_macinfo (void)
27021 unsigned i;
27022 unsigned long length = vec_safe_length (macinfo_table);
27023 macinfo_entry *ref;
27024 vec<macinfo_entry, va_gc> *files = NULL;
27025 macinfo_hash_type *macinfo_htab = NULL;
27026 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
27028 if (! length)
27029 return;
27031 /* output_macinfo* uses these interchangeably. */
27032 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
27033 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
27034 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
27035 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
27037 /* AIX Assembler inserts the length, so adjust the reference to match the
27038 offset expected by debuggers. */
27039 strcpy (dl_section_ref, debug_line_section_label);
27040 if (XCOFF_DEBUGGING_INFO)
27041 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
27043 /* For .debug_macro emit the section header. */
27044 if (!dwarf_strict || dwarf_version >= 5)
27046 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
27047 "DWARF macro version number");
27048 if (DWARF_OFFSET_SIZE == 8)
27049 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
27050 else
27051 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
27052 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
27053 (!dwarf_split_debug_info ? dl_section_ref
27054 : debug_skeleton_line_section_label),
27055 debug_line_section, NULL);
27058 /* In the first loop, it emits the primary .debug_macinfo section
27059 and after each emitted op the macinfo_entry is cleared.
27060 If a longer range of define/undef ops can be optimized using
27061 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
27062 the vector before the first define/undef in the range and the
27063 whole range of define/undef ops is not emitted and kept. */
27064 for (i = 0; macinfo_table->iterate (i, &ref); i++)
27066 switch (ref->code)
27068 case DW_MACINFO_start_file:
27069 vec_safe_push (files, *ref);
27070 break;
27071 case DW_MACINFO_end_file:
27072 if (!vec_safe_is_empty (files))
27073 files->pop ();
27074 break;
27075 case DW_MACINFO_define:
27076 case DW_MACINFO_undef:
27077 if ((!dwarf_strict || dwarf_version >= 5)
27078 && HAVE_COMDAT_GROUP
27079 && vec_safe_length (files) != 1
27080 && i > 0
27081 && i + 1 < length
27082 && (*macinfo_table)[i - 1].code == 0)
27084 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
27085 if (count)
27087 i += count - 1;
27088 continue;
27091 break;
27092 case 0:
27093 /* A dummy entry may be inserted at the beginning to be able
27094 to optimize the whole block of predefined macros. */
27095 if (i == 0)
27096 continue;
27097 default:
27098 break;
27100 output_macinfo_op (ref);
27101 ref->info = NULL;
27102 ref->code = 0;
27105 if (!macinfo_htab)
27106 return;
27108 delete macinfo_htab;
27109 macinfo_htab = NULL;
27111 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
27112 terminate the current chain and switch to a new comdat .debug_macinfo
27113 section and emit the define/undef entries within it. */
27114 for (i = 0; macinfo_table->iterate (i, &ref); i++)
27115 switch (ref->code)
27117 case 0:
27118 continue;
27119 case DW_MACRO_import:
27121 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27122 tree comdat_key = get_identifier (ref->info);
27123 /* Terminate the previous .debug_macinfo section. */
27124 dw2_asm_output_data (1, 0, "End compilation unit");
27125 targetm.asm_out.named_section (debug_macinfo_section_name,
27126 SECTION_DEBUG
27127 | SECTION_LINKONCE,
27128 comdat_key);
27129 ASM_GENERATE_INTERNAL_LABEL (label,
27130 DEBUG_MACRO_SECTION_LABEL,
27131 ref->lineno);
27132 ASM_OUTPUT_LABEL (asm_out_file, label);
27133 ref->code = 0;
27134 ref->info = NULL;
27135 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
27136 "DWARF macro version number");
27137 if (DWARF_OFFSET_SIZE == 8)
27138 dw2_asm_output_data (1, 1, "Flags: 64-bit");
27139 else
27140 dw2_asm_output_data (1, 0, "Flags: 32-bit");
27142 break;
27143 case DW_MACINFO_define:
27144 case DW_MACINFO_undef:
27145 output_macinfo_op (ref);
27146 ref->code = 0;
27147 ref->info = NULL;
27148 break;
27149 default:
27150 gcc_unreachable ();
27154 /* Initialize the various sections and labels for dwarf output. */
27156 static void
27157 init_sections_and_labels (void)
27159 if (!dwarf_split_debug_info)
27161 debug_info_section = get_section (DEBUG_INFO_SECTION,
27162 SECTION_DEBUG, NULL);
27163 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
27164 SECTION_DEBUG, NULL);
27165 debug_loc_section = get_section (dwarf_version >= 5
27166 ? DEBUG_LOCLISTS_SECTION
27167 : DEBUG_LOC_SECTION,
27168 SECTION_DEBUG, NULL);
27169 debug_macinfo_section_name
27170 = (dwarf_strict && dwarf_version < 5)
27171 ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION;
27172 debug_macinfo_section = get_section (debug_macinfo_section_name,
27173 SECTION_DEBUG, NULL);
27175 else
27177 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
27178 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27179 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
27180 SECTION_DEBUG | SECTION_EXCLUDE,
27181 NULL);
27182 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
27183 SECTION_DEBUG, NULL);
27184 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
27185 SECTION_DEBUG, NULL);
27186 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
27187 SECTION_DEBUG, NULL);
27188 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
27189 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
27191 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
27192 the main .o, but the skeleton_line goes into the split off dwo. */
27193 debug_skeleton_line_section
27194 = get_section (DEBUG_DWO_LINE_SECTION,
27195 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27196 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
27197 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
27198 debug_str_offsets_section = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
27199 SECTION_DEBUG | SECTION_EXCLUDE,
27200 NULL);
27201 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
27202 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
27203 debug_loc_section = get_section (dwarf_version >= 5
27204 ? DEBUG_DWO_LOCLISTS_SECTION
27205 : DEBUG_DWO_LOC_SECTION,
27206 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27207 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
27208 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
27209 debug_macinfo_section_name
27210 = (dwarf_strict && dwarf_version < 5)
27211 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION;
27212 debug_macinfo_section = get_section (debug_macinfo_section_name,
27213 SECTION_DEBUG | SECTION_EXCLUDE,
27214 NULL);
27216 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
27217 SECTION_DEBUG, NULL);
27218 debug_line_section = get_section (DEBUG_LINE_SECTION,
27219 SECTION_DEBUG, NULL);
27220 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
27221 SECTION_DEBUG, NULL);
27222 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
27223 SECTION_DEBUG, NULL);
27224 debug_str_section = get_section (DEBUG_STR_SECTION,
27225 DEBUG_STR_SECTION_FLAGS, NULL);
27226 if (!dwarf_split_debug_info && !DWARF2_ASM_LINE_DEBUG_INFO)
27227 debug_line_str_section = get_section (DEBUG_LINE_STR_SECTION,
27228 DEBUG_STR_SECTION_FLAGS, NULL);
27230 debug_ranges_section = get_section (dwarf_version >= 5
27231 ? DEBUG_RNGLISTS_SECTION
27232 : DEBUG_RANGES_SECTION,
27233 SECTION_DEBUG, NULL);
27234 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
27235 SECTION_DEBUG, NULL);
27237 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
27238 DEBUG_ABBREV_SECTION_LABEL, 0);
27239 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
27240 DEBUG_INFO_SECTION_LABEL, 0);
27241 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
27242 DEBUG_LINE_SECTION_LABEL, 0);
27243 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
27244 DEBUG_RANGES_SECTION_LABEL, 0);
27245 if (dwarf_version >= 5 && dwarf_split_debug_info)
27246 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
27247 DEBUG_RANGES_SECTION_LABEL, 1);
27248 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
27249 DEBUG_ADDR_SECTION_LABEL, 0);
27250 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
27251 (dwarf_strict && dwarf_version < 5)
27252 ? DEBUG_MACINFO_SECTION_LABEL
27253 : DEBUG_MACRO_SECTION_LABEL, 0);
27254 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
27257 /* Set up for Dwarf output at the start of compilation. */
27259 static void
27260 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
27262 /* This option is currently broken, see (PR53118 and PR46102). */
27263 if (flag_eliminate_dwarf2_dups
27264 && strstr (lang_hooks.name, "C++"))
27266 warning (0, "-feliminate-dwarf2-dups is broken for C++, ignoring");
27267 flag_eliminate_dwarf2_dups = 0;
27270 /* Allocate the file_table. */
27271 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
27273 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27274 /* Allocate the decl_die_table. */
27275 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
27277 /* Allocate the decl_loc_table. */
27278 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
27280 /* Allocate the cached_dw_loc_list_table. */
27281 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
27283 /* Allocate the initial hunk of the decl_scope_table. */
27284 vec_alloc (decl_scope_table, 256);
27286 /* Allocate the initial hunk of the abbrev_die_table. */
27287 vec_alloc (abbrev_die_table, 256);
27288 /* Zero-th entry is allocated, but unused. */
27289 abbrev_die_table->quick_push (NULL);
27291 /* Allocate the dwarf_proc_stack_usage_map. */
27292 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
27294 /* Allocate the pubtypes and pubnames vectors. */
27295 vec_alloc (pubname_table, 32);
27296 vec_alloc (pubtype_table, 32);
27298 vec_alloc (incomplete_types, 64);
27300 vec_alloc (used_rtx_array, 32);
27302 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27303 vec_alloc (macinfo_table, 64);
27304 #endif
27306 /* If front-ends already registered a main translation unit but we were not
27307 ready to perform the association, do this now. */
27308 if (main_translation_unit != NULL_TREE)
27309 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
27312 /* Called before compile () starts outputtting functions, variables
27313 and toplevel asms into assembly. */
27315 static void
27316 dwarf2out_assembly_start (void)
27318 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27319 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
27320 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
27321 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
27322 COLD_TEXT_SECTION_LABEL, 0);
27323 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
27325 switch_to_section (text_section);
27326 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
27327 #endif
27329 /* Make sure the line number table for .text always exists. */
27330 text_section_line_info = new_line_info_table ();
27331 text_section_line_info->end_label = text_end_label;
27333 #ifdef DWARF2_LINENO_DEBUGGING_INFO
27334 cur_line_info_table = text_section_line_info;
27335 #endif
27337 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
27338 && dwarf2out_do_cfi_asm ()
27339 && (!(flag_unwind_tables || flag_exceptions)
27340 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
27341 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
27344 /* A helper function for dwarf2out_finish called through
27345 htab_traverse. Assign a string its index. All strings must be
27346 collected into the table by the time index_string is called,
27347 because the indexing code relies on htab_traverse to traverse nodes
27348 in the same order for each run. */
27351 index_string (indirect_string_node **h, unsigned int *index)
27353 indirect_string_node *node = *h;
27355 find_string_form (node);
27356 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27358 gcc_assert (node->index == NO_INDEX_ASSIGNED);
27359 node->index = *index;
27360 *index += 1;
27362 return 1;
27365 /* A helper function for output_indirect_strings called through
27366 htab_traverse. Output the offset to a string and update the
27367 current offset. */
27370 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
27372 indirect_string_node *node = *h;
27374 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27376 /* Assert that this node has been assigned an index. */
27377 gcc_assert (node->index != NO_INDEX_ASSIGNED
27378 && node->index != NOT_INDEXED);
27379 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
27380 "indexed string 0x%x: %s", node->index, node->str);
27381 *offset += strlen (node->str) + 1;
27383 return 1;
27386 /* A helper function for dwarf2out_finish called through
27387 htab_traverse. Output the indexed string. */
27390 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
27392 struct indirect_string_node *node = *h;
27394 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27396 /* Assert that the strings are output in the same order as their
27397 indexes were assigned. */
27398 gcc_assert (*cur_idx == node->index);
27399 assemble_string (node->str, strlen (node->str) + 1);
27400 *cur_idx += 1;
27402 return 1;
27405 /* A helper function for dwarf2out_finish called through
27406 htab_traverse. Emit one queued .debug_str string. */
27409 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
27411 struct indirect_string_node *node = *h;
27413 node->form = find_string_form (node);
27414 if (node->form == form && node->refcount > 0)
27416 ASM_OUTPUT_LABEL (asm_out_file, node->label);
27417 assemble_string (node->str, strlen (node->str) + 1);
27420 return 1;
27423 /* Output the indexed string table. */
27425 static void
27426 output_indirect_strings (void)
27428 switch_to_section (debug_str_section);
27429 if (!dwarf_split_debug_info)
27430 debug_str_hash->traverse<enum dwarf_form,
27431 output_indirect_string> (DW_FORM_strp);
27432 else
27434 unsigned int offset = 0;
27435 unsigned int cur_idx = 0;
27437 skeleton_debug_str_hash->traverse<enum dwarf_form,
27438 output_indirect_string> (DW_FORM_strp);
27440 switch_to_section (debug_str_offsets_section);
27441 debug_str_hash->traverse_noresize
27442 <unsigned int *, output_index_string_offset> (&offset);
27443 switch_to_section (debug_str_dwo_section);
27444 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
27445 (&cur_idx);
27449 /* Callback for htab_traverse to assign an index to an entry in the
27450 table, and to write that entry to the .debug_addr section. */
27453 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
27455 addr_table_entry *entry = *slot;
27457 if (entry->refcount == 0)
27459 gcc_assert (entry->index == NO_INDEX_ASSIGNED
27460 || entry->index == NOT_INDEXED);
27461 return 1;
27464 gcc_assert (entry->index == *cur_index);
27465 (*cur_index)++;
27467 switch (entry->kind)
27469 case ate_kind_rtx:
27470 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
27471 "0x%x", entry->index);
27472 break;
27473 case ate_kind_rtx_dtprel:
27474 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
27475 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
27476 DWARF2_ADDR_SIZE,
27477 entry->addr.rtl);
27478 fputc ('\n', asm_out_file);
27479 break;
27480 case ate_kind_label:
27481 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
27482 "0x%x", entry->index);
27483 break;
27484 default:
27485 gcc_unreachable ();
27487 return 1;
27490 /* Produce the .debug_addr section. */
27492 static void
27493 output_addr_table (void)
27495 unsigned int index = 0;
27496 if (addr_index_table == NULL || addr_index_table->size () == 0)
27497 return;
27499 switch_to_section (debug_addr_section);
27500 addr_index_table
27501 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
27504 #if ENABLE_ASSERT_CHECKING
27505 /* Verify that all marks are clear. */
27507 static void
27508 verify_marks_clear (dw_die_ref die)
27510 dw_die_ref c;
27512 gcc_assert (! die->die_mark);
27513 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
27515 #endif /* ENABLE_ASSERT_CHECKING */
27517 /* Clear the marks for a die and its children.
27518 Be cool if the mark isn't set. */
27520 static void
27521 prune_unmark_dies (dw_die_ref die)
27523 dw_die_ref c;
27525 if (die->die_mark)
27526 die->die_mark = 0;
27527 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
27530 /* Given LOC that is referenced by a DIE we're marking as used, find all
27531 referenced DWARF procedures it references and mark them as used. */
27533 static void
27534 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
27536 for (; loc != NULL; loc = loc->dw_loc_next)
27537 switch (loc->dw_loc_opc)
27539 case DW_OP_implicit_pointer:
27540 case DW_OP_convert:
27541 case DW_OP_reinterpret:
27542 case DW_OP_GNU_implicit_pointer:
27543 case DW_OP_GNU_convert:
27544 case DW_OP_GNU_reinterpret:
27545 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
27546 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27547 break;
27548 case DW_OP_GNU_variable_value:
27549 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
27551 dw_die_ref ref
27552 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
27553 if (ref == NULL)
27554 break;
27555 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
27556 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
27557 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
27559 /* FALLTHRU */
27560 case DW_OP_call2:
27561 case DW_OP_call4:
27562 case DW_OP_call_ref:
27563 case DW_OP_const_type:
27564 case DW_OP_GNU_const_type:
27565 case DW_OP_GNU_parameter_ref:
27566 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
27567 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27568 break;
27569 case DW_OP_regval_type:
27570 case DW_OP_deref_type:
27571 case DW_OP_GNU_regval_type:
27572 case DW_OP_GNU_deref_type:
27573 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
27574 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
27575 break;
27576 case DW_OP_entry_value:
27577 case DW_OP_GNU_entry_value:
27578 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
27579 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
27580 break;
27581 default:
27582 break;
27586 /* Given DIE that we're marking as used, find any other dies
27587 it references as attributes and mark them as used. */
27589 static void
27590 prune_unused_types_walk_attribs (dw_die_ref die)
27592 dw_attr_node *a;
27593 unsigned ix;
27595 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27597 switch (AT_class (a))
27599 /* Make sure DWARF procedures referenced by location descriptions will
27600 get emitted. */
27601 case dw_val_class_loc:
27602 prune_unused_types_walk_loc_descr (AT_loc (a));
27603 break;
27604 case dw_val_class_loc_list:
27605 for (dw_loc_list_ref list = AT_loc_list (a);
27606 list != NULL;
27607 list = list->dw_loc_next)
27608 prune_unused_types_walk_loc_descr (list->expr);
27609 break;
27611 case dw_val_class_die_ref:
27612 /* A reference to another DIE.
27613 Make sure that it will get emitted.
27614 If it was broken out into a comdat group, don't follow it. */
27615 if (! AT_ref (a)->comdat_type_p
27616 || a->dw_attr == DW_AT_specification)
27617 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
27618 break;
27620 case dw_val_class_str:
27621 /* Set the string's refcount to 0 so that prune_unused_types_mark
27622 accounts properly for it. */
27623 a->dw_attr_val.v.val_str->refcount = 0;
27624 break;
27626 default:
27627 break;
27632 /* Mark the generic parameters and arguments children DIEs of DIE. */
27634 static void
27635 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
27637 dw_die_ref c;
27639 if (die == NULL || die->die_child == NULL)
27640 return;
27641 c = die->die_child;
27644 if (is_template_parameter (c))
27645 prune_unused_types_mark (c, 1);
27646 c = c->die_sib;
27647 } while (c && c != die->die_child);
27650 /* Mark DIE as being used. If DOKIDS is true, then walk down
27651 to DIE's children. */
27653 static void
27654 prune_unused_types_mark (dw_die_ref die, int dokids)
27656 dw_die_ref c;
27658 if (die->die_mark == 0)
27660 /* We haven't done this node yet. Mark it as used. */
27661 die->die_mark = 1;
27662 /* If this is the DIE of a generic type instantiation,
27663 mark the children DIEs that describe its generic parms and
27664 args. */
27665 prune_unused_types_mark_generic_parms_dies (die);
27667 /* We also have to mark its parents as used.
27668 (But we don't want to mark our parent's kids due to this,
27669 unless it is a class.) */
27670 if (die->die_parent)
27671 prune_unused_types_mark (die->die_parent,
27672 class_scope_p (die->die_parent));
27674 /* Mark any referenced nodes. */
27675 prune_unused_types_walk_attribs (die);
27677 /* If this node is a specification,
27678 also mark the definition, if it exists. */
27679 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
27680 prune_unused_types_mark (die->die_definition, 1);
27683 if (dokids && die->die_mark != 2)
27685 /* We need to walk the children, but haven't done so yet.
27686 Remember that we've walked the kids. */
27687 die->die_mark = 2;
27689 /* If this is an array type, we need to make sure our
27690 kids get marked, even if they're types. If we're
27691 breaking out types into comdat sections, do this
27692 for all type definitions. */
27693 if (die->die_tag == DW_TAG_array_type
27694 || (use_debug_types
27695 && is_type_die (die) && ! is_declaration_die (die)))
27696 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
27697 else
27698 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
27702 /* For local classes, look if any static member functions were emitted
27703 and if so, mark them. */
27705 static void
27706 prune_unused_types_walk_local_classes (dw_die_ref die)
27708 dw_die_ref c;
27710 if (die->die_mark == 2)
27711 return;
27713 switch (die->die_tag)
27715 case DW_TAG_structure_type:
27716 case DW_TAG_union_type:
27717 case DW_TAG_class_type:
27718 break;
27720 case DW_TAG_subprogram:
27721 if (!get_AT_flag (die, DW_AT_declaration)
27722 || die->die_definition != NULL)
27723 prune_unused_types_mark (die, 1);
27724 return;
27726 default:
27727 return;
27730 /* Mark children. */
27731 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
27734 /* Walk the tree DIE and mark types that we actually use. */
27736 static void
27737 prune_unused_types_walk (dw_die_ref die)
27739 dw_die_ref c;
27741 /* Don't do anything if this node is already marked and
27742 children have been marked as well. */
27743 if (die->die_mark == 2)
27744 return;
27746 switch (die->die_tag)
27748 case DW_TAG_structure_type:
27749 case DW_TAG_union_type:
27750 case DW_TAG_class_type:
27751 if (die->die_perennial_p)
27752 break;
27754 for (c = die->die_parent; c; c = c->die_parent)
27755 if (c->die_tag == DW_TAG_subprogram)
27756 break;
27758 /* Finding used static member functions inside of classes
27759 is needed just for local classes, because for other classes
27760 static member function DIEs with DW_AT_specification
27761 are emitted outside of the DW_TAG_*_type. If we ever change
27762 it, we'd need to call this even for non-local classes. */
27763 if (c)
27764 prune_unused_types_walk_local_classes (die);
27766 /* It's a type node --- don't mark it. */
27767 return;
27769 case DW_TAG_const_type:
27770 case DW_TAG_packed_type:
27771 case DW_TAG_pointer_type:
27772 case DW_TAG_reference_type:
27773 case DW_TAG_rvalue_reference_type:
27774 case DW_TAG_volatile_type:
27775 case DW_TAG_typedef:
27776 case DW_TAG_array_type:
27777 case DW_TAG_interface_type:
27778 case DW_TAG_friend:
27779 case DW_TAG_enumeration_type:
27780 case DW_TAG_subroutine_type:
27781 case DW_TAG_string_type:
27782 case DW_TAG_set_type:
27783 case DW_TAG_subrange_type:
27784 case DW_TAG_ptr_to_member_type:
27785 case DW_TAG_file_type:
27786 /* Type nodes are useful only when other DIEs reference them --- don't
27787 mark them. */
27788 /* FALLTHROUGH */
27790 case DW_TAG_dwarf_procedure:
27791 /* Likewise for DWARF procedures. */
27793 if (die->die_perennial_p)
27794 break;
27796 return;
27798 default:
27799 /* Mark everything else. */
27800 break;
27803 if (die->die_mark == 0)
27805 die->die_mark = 1;
27807 /* Now, mark any dies referenced from here. */
27808 prune_unused_types_walk_attribs (die);
27811 die->die_mark = 2;
27813 /* Mark children. */
27814 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
27817 /* Increment the string counts on strings referred to from DIE's
27818 attributes. */
27820 static void
27821 prune_unused_types_update_strings (dw_die_ref die)
27823 dw_attr_node *a;
27824 unsigned ix;
27826 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27827 if (AT_class (a) == dw_val_class_str)
27829 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
27830 s->refcount++;
27831 /* Avoid unnecessarily putting strings that are used less than
27832 twice in the hash table. */
27833 if (s->refcount
27834 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
27836 indirect_string_node **slot
27837 = debug_str_hash->find_slot_with_hash (s->str,
27838 htab_hash_string (s->str),
27839 INSERT);
27840 gcc_assert (*slot == NULL);
27841 *slot = s;
27846 /* Mark DIE and its children as removed. */
27848 static void
27849 mark_removed (dw_die_ref die)
27851 dw_die_ref c;
27852 die->removed = true;
27853 FOR_EACH_CHILD (die, c, mark_removed (c));
27856 /* Remove from the tree DIE any dies that aren't marked. */
27858 static void
27859 prune_unused_types_prune (dw_die_ref die)
27861 dw_die_ref c;
27863 gcc_assert (die->die_mark);
27864 prune_unused_types_update_strings (die);
27866 if (! die->die_child)
27867 return;
27869 c = die->die_child;
27870 do {
27871 dw_die_ref prev = c, next;
27872 for (c = c->die_sib; ! c->die_mark; c = next)
27873 if (c == die->die_child)
27875 /* No marked children between 'prev' and the end of the list. */
27876 if (prev == c)
27877 /* No marked children at all. */
27878 die->die_child = NULL;
27879 else
27881 prev->die_sib = c->die_sib;
27882 die->die_child = prev;
27884 c->die_sib = NULL;
27885 mark_removed (c);
27886 return;
27888 else
27890 next = c->die_sib;
27891 c->die_sib = NULL;
27892 mark_removed (c);
27895 if (c != prev->die_sib)
27896 prev->die_sib = c;
27897 prune_unused_types_prune (c);
27898 } while (c != die->die_child);
27901 /* Remove dies representing declarations that we never use. */
27903 static void
27904 prune_unused_types (void)
27906 unsigned int i;
27907 limbo_die_node *node;
27908 comdat_type_node *ctnode;
27909 pubname_entry *pub;
27910 dw_die_ref base_type;
27912 #if ENABLE_ASSERT_CHECKING
27913 /* All the marks should already be clear. */
27914 verify_marks_clear (comp_unit_die ());
27915 for (node = limbo_die_list; node; node = node->next)
27916 verify_marks_clear (node->die);
27917 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27918 verify_marks_clear (ctnode->root_die);
27919 #endif /* ENABLE_ASSERT_CHECKING */
27921 /* Mark types that are used in global variables. */
27922 premark_types_used_by_global_vars ();
27924 /* Set the mark on nodes that are actually used. */
27925 prune_unused_types_walk (comp_unit_die ());
27926 for (node = limbo_die_list; node; node = node->next)
27927 prune_unused_types_walk (node->die);
27928 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27930 prune_unused_types_walk (ctnode->root_die);
27931 prune_unused_types_mark (ctnode->type_die, 1);
27934 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
27935 are unusual in that they are pubnames that are the children of pubtypes.
27936 They should only be marked via their parent DW_TAG_enumeration_type die,
27937 not as roots in themselves. */
27938 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
27939 if (pub->die->die_tag != DW_TAG_enumerator)
27940 prune_unused_types_mark (pub->die, 1);
27941 for (i = 0; base_types.iterate (i, &base_type); i++)
27942 prune_unused_types_mark (base_type, 1);
27944 /* For -fvar-tracking-assignments, also set the mark on nodes that could be
27945 referenced by DW_TAG_call_site DW_AT_call_origin (i.e. direct call
27946 callees). */
27947 cgraph_node *cnode;
27948 FOR_EACH_FUNCTION (cnode)
27949 if (cnode->referred_to_p (false))
27951 dw_die_ref die = lookup_decl_die (cnode->decl);
27952 if (die == NULL || die->die_mark)
27953 continue;
27954 for (cgraph_edge *e = cnode->callers; e; e = e->next_caller)
27955 if (e->caller != cnode
27956 && opt_for_fn (e->caller->decl, flag_var_tracking_assignments))
27958 prune_unused_types_mark (die, 1);
27959 break;
27963 if (debug_str_hash)
27964 debug_str_hash->empty ();
27965 if (skeleton_debug_str_hash)
27966 skeleton_debug_str_hash->empty ();
27967 prune_unused_types_prune (comp_unit_die ());
27968 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
27970 node = *pnode;
27971 if (!node->die->die_mark)
27972 *pnode = node->next;
27973 else
27975 prune_unused_types_prune (node->die);
27976 pnode = &node->next;
27979 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27980 prune_unused_types_prune (ctnode->root_die);
27982 /* Leave the marks clear. */
27983 prune_unmark_dies (comp_unit_die ());
27984 for (node = limbo_die_list; node; node = node->next)
27985 prune_unmark_dies (node->die);
27986 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27987 prune_unmark_dies (ctnode->root_die);
27990 /* Helpers to manipulate hash table of comdat type units. */
27992 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
27994 static inline hashval_t hash (const comdat_type_node *);
27995 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
27998 inline hashval_t
27999 comdat_type_hasher::hash (const comdat_type_node *type_node)
28001 hashval_t h;
28002 memcpy (&h, type_node->signature, sizeof (h));
28003 return h;
28006 inline bool
28007 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
28008 const comdat_type_node *type_node_2)
28010 return (! memcmp (type_node_1->signature, type_node_2->signature,
28011 DWARF_TYPE_SIGNATURE_SIZE));
28014 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
28015 to the location it would have been added, should we know its
28016 DECL_ASSEMBLER_NAME when we added other attributes. This will
28017 probably improve compactness of debug info, removing equivalent
28018 abbrevs, and hide any differences caused by deferring the
28019 computation of the assembler name, triggered by e.g. PCH. */
28021 static inline void
28022 move_linkage_attr (dw_die_ref die)
28024 unsigned ix = vec_safe_length (die->die_attr);
28025 dw_attr_node linkage = (*die->die_attr)[ix - 1];
28027 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
28028 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
28030 while (--ix > 0)
28032 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
28034 if (prev->dw_attr == DW_AT_decl_line
28035 || prev->dw_attr == DW_AT_decl_column
28036 || prev->dw_attr == DW_AT_name)
28037 break;
28040 if (ix != vec_safe_length (die->die_attr) - 1)
28042 die->die_attr->pop ();
28043 die->die_attr->quick_insert (ix, linkage);
28047 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
28048 referenced from typed stack ops and count how often they are used. */
28050 static void
28051 mark_base_types (dw_loc_descr_ref loc)
28053 dw_die_ref base_type = NULL;
28055 for (; loc; loc = loc->dw_loc_next)
28057 switch (loc->dw_loc_opc)
28059 case DW_OP_regval_type:
28060 case DW_OP_deref_type:
28061 case DW_OP_GNU_regval_type:
28062 case DW_OP_GNU_deref_type:
28063 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
28064 break;
28065 case DW_OP_convert:
28066 case DW_OP_reinterpret:
28067 case DW_OP_GNU_convert:
28068 case DW_OP_GNU_reinterpret:
28069 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
28070 continue;
28071 /* FALLTHRU */
28072 case DW_OP_const_type:
28073 case DW_OP_GNU_const_type:
28074 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
28075 break;
28076 case DW_OP_entry_value:
28077 case DW_OP_GNU_entry_value:
28078 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
28079 continue;
28080 default:
28081 continue;
28083 gcc_assert (base_type->die_parent == comp_unit_die ());
28084 if (base_type->die_mark)
28085 base_type->die_mark++;
28086 else
28088 base_types.safe_push (base_type);
28089 base_type->die_mark = 1;
28094 /* Comparison function for sorting marked base types. */
28096 static int
28097 base_type_cmp (const void *x, const void *y)
28099 dw_die_ref dx = *(const dw_die_ref *) x;
28100 dw_die_ref dy = *(const dw_die_ref *) y;
28101 unsigned int byte_size1, byte_size2;
28102 unsigned int encoding1, encoding2;
28103 unsigned int align1, align2;
28104 if (dx->die_mark > dy->die_mark)
28105 return -1;
28106 if (dx->die_mark < dy->die_mark)
28107 return 1;
28108 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
28109 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
28110 if (byte_size1 < byte_size2)
28111 return 1;
28112 if (byte_size1 > byte_size2)
28113 return -1;
28114 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
28115 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
28116 if (encoding1 < encoding2)
28117 return 1;
28118 if (encoding1 > encoding2)
28119 return -1;
28120 align1 = get_AT_unsigned (dx, DW_AT_alignment);
28121 align2 = get_AT_unsigned (dy, DW_AT_alignment);
28122 if (align1 < align2)
28123 return 1;
28124 if (align1 > align2)
28125 return -1;
28126 return 0;
28129 /* Move base types marked by mark_base_types as early as possible
28130 in the CU, sorted by decreasing usage count both to make the
28131 uleb128 references as small as possible and to make sure they
28132 will have die_offset already computed by calc_die_sizes when
28133 sizes of typed stack loc ops is computed. */
28135 static void
28136 move_marked_base_types (void)
28138 unsigned int i;
28139 dw_die_ref base_type, die, c;
28141 if (base_types.is_empty ())
28142 return;
28144 /* Sort by decreasing usage count, they will be added again in that
28145 order later on. */
28146 base_types.qsort (base_type_cmp);
28147 die = comp_unit_die ();
28148 c = die->die_child;
28151 dw_die_ref prev = c;
28152 c = c->die_sib;
28153 while (c->die_mark)
28155 remove_child_with_prev (c, prev);
28156 /* As base types got marked, there must be at least
28157 one node other than DW_TAG_base_type. */
28158 gcc_assert (die->die_child != NULL);
28159 c = prev->die_sib;
28162 while (c != die->die_child);
28163 gcc_assert (die->die_child);
28164 c = die->die_child;
28165 for (i = 0; base_types.iterate (i, &base_type); i++)
28167 base_type->die_mark = 0;
28168 base_type->die_sib = c->die_sib;
28169 c->die_sib = base_type;
28170 c = base_type;
28174 /* Helper function for resolve_addr, attempt to resolve
28175 one CONST_STRING, return true if successful. Similarly verify that
28176 SYMBOL_REFs refer to variables emitted in the current CU. */
28178 static bool
28179 resolve_one_addr (rtx *addr)
28181 rtx rtl = *addr;
28183 if (GET_CODE (rtl) == CONST_STRING)
28185 size_t len = strlen (XSTR (rtl, 0)) + 1;
28186 tree t = build_string (len, XSTR (rtl, 0));
28187 tree tlen = size_int (len - 1);
28188 TREE_TYPE (t)
28189 = build_array_type (char_type_node, build_index_type (tlen));
28190 rtl = lookup_constant_def (t);
28191 if (!rtl || !MEM_P (rtl))
28192 return false;
28193 rtl = XEXP (rtl, 0);
28194 if (GET_CODE (rtl) == SYMBOL_REF
28195 && SYMBOL_REF_DECL (rtl)
28196 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
28197 return false;
28198 vec_safe_push (used_rtx_array, rtl);
28199 *addr = rtl;
28200 return true;
28203 if (GET_CODE (rtl) == SYMBOL_REF
28204 && SYMBOL_REF_DECL (rtl))
28206 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
28208 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
28209 return false;
28211 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
28212 return false;
28215 if (GET_CODE (rtl) == CONST)
28217 subrtx_ptr_iterator::array_type array;
28218 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
28219 if (!resolve_one_addr (*iter))
28220 return false;
28223 return true;
28226 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
28227 if possible, and create DW_TAG_dwarf_procedure that can be referenced
28228 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
28230 static rtx
28231 string_cst_pool_decl (tree t)
28233 rtx rtl = output_constant_def (t, 1);
28234 unsigned char *array;
28235 dw_loc_descr_ref l;
28236 tree decl;
28237 size_t len;
28238 dw_die_ref ref;
28240 if (!rtl || !MEM_P (rtl))
28241 return NULL_RTX;
28242 rtl = XEXP (rtl, 0);
28243 if (GET_CODE (rtl) != SYMBOL_REF
28244 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
28245 return NULL_RTX;
28247 decl = SYMBOL_REF_DECL (rtl);
28248 if (!lookup_decl_die (decl))
28250 len = TREE_STRING_LENGTH (t);
28251 vec_safe_push (used_rtx_array, rtl);
28252 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
28253 array = ggc_vec_alloc<unsigned char> (len);
28254 memcpy (array, TREE_STRING_POINTER (t), len);
28255 l = new_loc_descr (DW_OP_implicit_value, len, 0);
28256 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
28257 l->dw_loc_oprnd2.v.val_vec.length = len;
28258 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
28259 l->dw_loc_oprnd2.v.val_vec.array = array;
28260 add_AT_loc (ref, DW_AT_location, l);
28261 equate_decl_number_to_die (decl, ref);
28263 return rtl;
28266 /* Helper function of resolve_addr_in_expr. LOC is
28267 a DW_OP_addr followed by DW_OP_stack_value, either at the start
28268 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
28269 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
28270 with DW_OP_implicit_pointer if possible
28271 and return true, if unsuccessful, return false. */
28273 static bool
28274 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
28276 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
28277 HOST_WIDE_INT offset = 0;
28278 dw_die_ref ref = NULL;
28279 tree decl;
28281 if (GET_CODE (rtl) == CONST
28282 && GET_CODE (XEXP (rtl, 0)) == PLUS
28283 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
28285 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
28286 rtl = XEXP (XEXP (rtl, 0), 0);
28288 if (GET_CODE (rtl) == CONST_STRING)
28290 size_t len = strlen (XSTR (rtl, 0)) + 1;
28291 tree t = build_string (len, XSTR (rtl, 0));
28292 tree tlen = size_int (len - 1);
28294 TREE_TYPE (t)
28295 = build_array_type (char_type_node, build_index_type (tlen));
28296 rtl = string_cst_pool_decl (t);
28297 if (!rtl)
28298 return false;
28300 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
28302 decl = SYMBOL_REF_DECL (rtl);
28303 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
28305 ref = lookup_decl_die (decl);
28306 if (ref && (get_AT (ref, DW_AT_location)
28307 || get_AT (ref, DW_AT_const_value)))
28309 loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
28310 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28311 loc->dw_loc_oprnd1.val_entry = NULL;
28312 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28313 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28314 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28315 loc->dw_loc_oprnd2.v.val_int = offset;
28316 return true;
28320 return false;
28323 /* Helper function for resolve_addr, handle one location
28324 expression, return false if at least one CONST_STRING or SYMBOL_REF in
28325 the location list couldn't be resolved. */
28327 static bool
28328 resolve_addr_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
28330 dw_loc_descr_ref keep = NULL;
28331 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
28332 switch (loc->dw_loc_opc)
28334 case DW_OP_addr:
28335 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28337 if ((prev == NULL
28338 || prev->dw_loc_opc == DW_OP_piece
28339 || prev->dw_loc_opc == DW_OP_bit_piece)
28340 && loc->dw_loc_next
28341 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
28342 && (!dwarf_strict || dwarf_version >= 5)
28343 && optimize_one_addr_into_implicit_ptr (loc))
28344 break;
28345 return false;
28347 break;
28348 case DW_OP_GNU_addr_index:
28349 case DW_OP_GNU_const_index:
28350 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
28351 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
28353 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
28354 if (!resolve_one_addr (&rtl))
28355 return false;
28356 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
28357 loc->dw_loc_oprnd1.val_entry
28358 = add_addr_table_entry (rtl, ate_kind_rtx);
28360 break;
28361 case DW_OP_const4u:
28362 case DW_OP_const8u:
28363 if (loc->dtprel
28364 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28365 return false;
28366 break;
28367 case DW_OP_plus_uconst:
28368 if (size_of_loc_descr (loc)
28369 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
28371 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
28373 dw_loc_descr_ref repl
28374 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
28375 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
28376 add_loc_descr (&repl, loc->dw_loc_next);
28377 *loc = *repl;
28379 break;
28380 case DW_OP_implicit_value:
28381 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
28382 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
28383 return false;
28384 break;
28385 case DW_OP_implicit_pointer:
28386 case DW_OP_GNU_implicit_pointer:
28387 case DW_OP_GNU_parameter_ref:
28388 case DW_OP_GNU_variable_value:
28389 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28391 dw_die_ref ref
28392 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
28393 if (ref == NULL)
28394 return false;
28395 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28396 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28397 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28399 if (loc->dw_loc_opc == DW_OP_GNU_variable_value)
28401 if (prev == NULL
28402 && loc->dw_loc_next == NULL
28403 && AT_class (a) == dw_val_class_loc)
28404 switch (a->dw_attr)
28406 /* Following attributes allow both exprloc and reference,
28407 so if the whole expression is DW_OP_GNU_variable_value
28408 alone we could transform it into reference. */
28409 case DW_AT_byte_size:
28410 case DW_AT_bit_size:
28411 case DW_AT_lower_bound:
28412 case DW_AT_upper_bound:
28413 case DW_AT_bit_stride:
28414 case DW_AT_count:
28415 case DW_AT_allocated:
28416 case DW_AT_associated:
28417 case DW_AT_byte_stride:
28418 a->dw_attr_val.val_class = dw_val_class_die_ref;
28419 a->dw_attr_val.val_entry = NULL;
28420 a->dw_attr_val.v.val_die_ref.die
28421 = loc->dw_loc_oprnd1.v.val_die_ref.die;
28422 a->dw_attr_val.v.val_die_ref.external = 0;
28423 return true;
28424 default:
28425 break;
28427 if (dwarf_strict)
28428 return false;
28430 break;
28431 case DW_OP_const_type:
28432 case DW_OP_regval_type:
28433 case DW_OP_deref_type:
28434 case DW_OP_convert:
28435 case DW_OP_reinterpret:
28436 case DW_OP_GNU_const_type:
28437 case DW_OP_GNU_regval_type:
28438 case DW_OP_GNU_deref_type:
28439 case DW_OP_GNU_convert:
28440 case DW_OP_GNU_reinterpret:
28441 while (loc->dw_loc_next
28442 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
28443 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
28445 dw_die_ref base1, base2;
28446 unsigned enc1, enc2, size1, size2;
28447 if (loc->dw_loc_opc == DW_OP_regval_type
28448 || loc->dw_loc_opc == DW_OP_deref_type
28449 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28450 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28451 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
28452 else if (loc->dw_loc_oprnd1.val_class
28453 == dw_val_class_unsigned_const)
28454 break;
28455 else
28456 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
28457 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
28458 == dw_val_class_unsigned_const)
28459 break;
28460 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
28461 gcc_assert (base1->die_tag == DW_TAG_base_type
28462 && base2->die_tag == DW_TAG_base_type);
28463 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
28464 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
28465 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
28466 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
28467 if (size1 == size2
28468 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
28469 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
28470 && loc != keep)
28471 || enc1 == enc2))
28473 /* Optimize away next DW_OP_convert after
28474 adjusting LOC's base type die reference. */
28475 if (loc->dw_loc_opc == DW_OP_regval_type
28476 || loc->dw_loc_opc == DW_OP_deref_type
28477 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28478 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28479 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
28480 else
28481 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
28482 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28483 continue;
28485 /* Don't change integer DW_OP_convert after e.g. floating
28486 point typed stack entry. */
28487 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
28488 keep = loc->dw_loc_next;
28489 break;
28491 break;
28492 default:
28493 break;
28495 return true;
28498 /* Helper function of resolve_addr. DIE had DW_AT_location of
28499 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
28500 and DW_OP_addr couldn't be resolved. resolve_addr has already
28501 removed the DW_AT_location attribute. This function attempts to
28502 add a new DW_AT_location attribute with DW_OP_implicit_pointer
28503 to it or DW_AT_const_value attribute, if possible. */
28505 static void
28506 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
28508 if (!VAR_P (decl)
28509 || lookup_decl_die (decl) != die
28510 || DECL_EXTERNAL (decl)
28511 || !TREE_STATIC (decl)
28512 || DECL_INITIAL (decl) == NULL_TREE
28513 || DECL_P (DECL_INITIAL (decl))
28514 || get_AT (die, DW_AT_const_value))
28515 return;
28517 tree init = DECL_INITIAL (decl);
28518 HOST_WIDE_INT offset = 0;
28519 /* For variables that have been optimized away and thus
28520 don't have a memory location, see if we can emit
28521 DW_AT_const_value instead. */
28522 if (tree_add_const_value_attribute (die, init))
28523 return;
28524 if (dwarf_strict && dwarf_version < 5)
28525 return;
28526 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
28527 and ADDR_EXPR refers to a decl that has DW_AT_location or
28528 DW_AT_const_value (but isn't addressable, otherwise
28529 resolving the original DW_OP_addr wouldn't fail), see if
28530 we can add DW_OP_implicit_pointer. */
28531 STRIP_NOPS (init);
28532 if (TREE_CODE (init) == POINTER_PLUS_EXPR
28533 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
28535 offset = tree_to_shwi (TREE_OPERAND (init, 1));
28536 init = TREE_OPERAND (init, 0);
28537 STRIP_NOPS (init);
28539 if (TREE_CODE (init) != ADDR_EXPR)
28540 return;
28541 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
28542 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
28543 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
28544 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
28545 && TREE_OPERAND (init, 0) != decl))
28547 dw_die_ref ref;
28548 dw_loc_descr_ref l;
28550 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
28552 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
28553 if (!rtl)
28554 return;
28555 decl = SYMBOL_REF_DECL (rtl);
28557 else
28558 decl = TREE_OPERAND (init, 0);
28559 ref = lookup_decl_die (decl);
28560 if (ref == NULL
28561 || (!get_AT (ref, DW_AT_location)
28562 && !get_AT (ref, DW_AT_const_value)))
28563 return;
28564 l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
28565 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28566 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
28567 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
28568 add_AT_loc (die, DW_AT_location, l);
28572 /* Return NULL if l is a DWARF expression, or first op that is not
28573 valid DWARF expression. */
28575 static dw_loc_descr_ref
28576 non_dwarf_expression (dw_loc_descr_ref l)
28578 while (l)
28580 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28581 return l;
28582 switch (l->dw_loc_opc)
28584 case DW_OP_regx:
28585 case DW_OP_implicit_value:
28586 case DW_OP_stack_value:
28587 case DW_OP_implicit_pointer:
28588 case DW_OP_GNU_implicit_pointer:
28589 case DW_OP_GNU_parameter_ref:
28590 case DW_OP_piece:
28591 case DW_OP_bit_piece:
28592 return l;
28593 default:
28594 break;
28596 l = l->dw_loc_next;
28598 return NULL;
28601 /* Return adjusted copy of EXPR:
28602 If it is empty DWARF expression, return it.
28603 If it is valid non-empty DWARF expression,
28604 return copy of EXPR with DW_OP_deref appended to it.
28605 If it is DWARF expression followed by DW_OP_reg{N,x}, return
28606 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended.
28607 If it is DWARF expression followed by DW_OP_stack_value, return
28608 copy of the DWARF expression without anything appended.
28609 Otherwise, return NULL. */
28611 static dw_loc_descr_ref
28612 copy_deref_exprloc (dw_loc_descr_ref expr)
28614 dw_loc_descr_ref tail = NULL;
28616 if (expr == NULL)
28617 return NULL;
28619 dw_loc_descr_ref l = non_dwarf_expression (expr);
28620 if (l && l->dw_loc_next)
28621 return NULL;
28623 if (l)
28625 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28626 tail = new_loc_descr ((enum dwarf_location_atom)
28627 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
28628 0, 0);
28629 else
28630 switch (l->dw_loc_opc)
28632 case DW_OP_regx:
28633 tail = new_loc_descr (DW_OP_bregx,
28634 l->dw_loc_oprnd1.v.val_unsigned, 0);
28635 break;
28636 case DW_OP_stack_value:
28637 break;
28638 default:
28639 return NULL;
28642 else
28643 tail = new_loc_descr (DW_OP_deref, 0, 0);
28645 dw_loc_descr_ref ret = NULL, *p = &ret;
28646 while (expr != l)
28648 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
28649 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
28650 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
28651 p = &(*p)->dw_loc_next;
28652 expr = expr->dw_loc_next;
28654 *p = tail;
28655 return ret;
28658 /* For DW_AT_string_length attribute with DW_OP_GNU_variable_value
28659 reference to a variable or argument, adjust it if needed and return:
28660 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
28661 attribute if present should be removed
28662 0 keep the attribute perhaps with minor modifications, no need to rescan
28663 1 if the attribute has been successfully adjusted. */
28665 static int
28666 optimize_string_length (dw_attr_node *a)
28668 dw_loc_descr_ref l = AT_loc (a), lv;
28669 dw_die_ref die;
28670 if (l->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28672 tree decl = l->dw_loc_oprnd1.v.val_decl_ref;
28673 die = lookup_decl_die (decl);
28674 if (die)
28676 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28677 l->dw_loc_oprnd1.v.val_die_ref.die = die;
28678 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
28680 else
28681 return -1;
28683 else
28684 die = l->dw_loc_oprnd1.v.val_die_ref.die;
28686 /* DWARF5 allows reference class, so we can then reference the DIE.
28687 Only do this for DW_OP_GNU_variable_value DW_OP_stack_value. */
28688 if (l->dw_loc_next != NULL && dwarf_version >= 5)
28690 a->dw_attr_val.val_class = dw_val_class_die_ref;
28691 a->dw_attr_val.val_entry = NULL;
28692 a->dw_attr_val.v.val_die_ref.die = die;
28693 a->dw_attr_val.v.val_die_ref.external = 0;
28694 return 0;
28697 dw_attr_node *av = get_AT (die, DW_AT_location);
28698 dw_loc_list_ref d;
28699 bool non_dwarf_expr = false;
28701 if (av == NULL)
28702 return dwarf_strict ? -1 : 0;
28703 switch (AT_class (av))
28705 case dw_val_class_loc_list:
28706 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
28707 if (d->expr && non_dwarf_expression (d->expr))
28708 non_dwarf_expr = true;
28709 break;
28710 case dw_val_class_loc:
28711 lv = AT_loc (av);
28712 if (lv == NULL)
28713 return dwarf_strict ? -1 : 0;
28714 if (non_dwarf_expression (lv))
28715 non_dwarf_expr = true;
28716 break;
28717 default:
28718 return dwarf_strict ? -1 : 0;
28721 /* If it is safe to transform DW_OP_GNU_variable_value DW_OP_stack_value
28722 into DW_OP_call4 or DW_OP_GNU_variable_value into
28723 DW_OP_call4 DW_OP_deref, do so. */
28724 if (!non_dwarf_expr
28725 && (l->dw_loc_next != NULL || AT_class (av) == dw_val_class_loc))
28727 l->dw_loc_opc = DW_OP_call4;
28728 if (l->dw_loc_next)
28729 l->dw_loc_next = NULL;
28730 else
28731 l->dw_loc_next = new_loc_descr (DW_OP_deref, 0, 0);
28732 return 0;
28735 /* For DW_OP_GNU_variable_value DW_OP_stack_value, we can just
28736 copy over the DW_AT_location attribute from die to a. */
28737 if (l->dw_loc_next != NULL)
28739 a->dw_attr_val = av->dw_attr_val;
28740 return 1;
28743 dw_loc_list_ref list, *p;
28744 switch (AT_class (av))
28746 case dw_val_class_loc_list:
28747 p = &list;
28748 list = NULL;
28749 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
28751 lv = copy_deref_exprloc (d->expr);
28752 if (lv)
28754 *p = new_loc_list (lv, d->begin, d->end, d->section);
28755 p = &(*p)->dw_loc_next;
28757 else if (!dwarf_strict && d->expr)
28758 return 0;
28760 if (list == NULL)
28761 return dwarf_strict ? -1 : 0;
28762 a->dw_attr_val.val_class = dw_val_class_loc_list;
28763 gen_llsym (list);
28764 *AT_loc_list_ptr (a) = list;
28765 return 1;
28766 case dw_val_class_loc:
28767 lv = copy_deref_exprloc (AT_loc (av));
28768 if (lv == NULL)
28769 return dwarf_strict ? -1 : 0;
28770 a->dw_attr_val.v.val_loc = lv;
28771 return 1;
28772 default:
28773 gcc_unreachable ();
28777 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
28778 an address in .rodata section if the string literal is emitted there,
28779 or remove the containing location list or replace DW_AT_const_value
28780 with DW_AT_location and empty location expression, if it isn't found
28781 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
28782 to something that has been emitted in the current CU. */
28784 static void
28785 resolve_addr (dw_die_ref die)
28787 dw_die_ref c;
28788 dw_attr_node *a;
28789 dw_loc_list_ref *curr, *start, loc;
28790 unsigned ix;
28791 bool remove_AT_byte_size = false;
28793 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
28794 switch (AT_class (a))
28796 case dw_val_class_loc_list:
28797 start = curr = AT_loc_list_ptr (a);
28798 loc = *curr;
28799 gcc_assert (loc);
28800 /* The same list can be referenced more than once. See if we have
28801 already recorded the result from a previous pass. */
28802 if (loc->replaced)
28803 *curr = loc->dw_loc_next;
28804 else if (!loc->resolved_addr)
28806 /* As things stand, we do not expect or allow one die to
28807 reference a suffix of another die's location list chain.
28808 References must be identical or completely separate.
28809 There is therefore no need to cache the result of this
28810 pass on any list other than the first; doing so
28811 would lead to unnecessary writes. */
28812 while (*curr)
28814 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
28815 if (!resolve_addr_in_expr (a, (*curr)->expr))
28817 dw_loc_list_ref next = (*curr)->dw_loc_next;
28818 dw_loc_descr_ref l = (*curr)->expr;
28820 if (next && (*curr)->ll_symbol)
28822 gcc_assert (!next->ll_symbol);
28823 next->ll_symbol = (*curr)->ll_symbol;
28825 if (dwarf_split_debug_info)
28826 remove_loc_list_addr_table_entries (l);
28827 *curr = next;
28829 else
28831 mark_base_types ((*curr)->expr);
28832 curr = &(*curr)->dw_loc_next;
28835 if (loc == *start)
28836 loc->resolved_addr = 1;
28837 else
28839 loc->replaced = 1;
28840 loc->dw_loc_next = *start;
28843 if (!*start)
28845 remove_AT (die, a->dw_attr);
28846 ix--;
28848 break;
28849 case dw_val_class_loc:
28851 dw_loc_descr_ref l = AT_loc (a);
28852 /* DW_OP_GNU_variable_value DW_OP_stack_value or
28853 DW_OP_GNU_variable_value in DW_AT_string_length can be converted
28854 into DW_OP_call4 or DW_OP_call4 DW_OP_deref, which is standard
28855 DWARF4 unlike DW_OP_GNU_variable_value. Or for DWARF5
28856 DW_OP_GNU_variable_value DW_OP_stack_value can be replaced
28857 with DW_FORM_ref referencing the same DIE as
28858 DW_OP_GNU_variable_value used to reference. */
28859 if (a->dw_attr == DW_AT_string_length
28860 && l
28861 && l->dw_loc_opc == DW_OP_GNU_variable_value
28862 && (l->dw_loc_next == NULL
28863 || (l->dw_loc_next->dw_loc_next == NULL
28864 && l->dw_loc_next->dw_loc_opc == DW_OP_stack_value)))
28866 switch (optimize_string_length (a))
28868 case -1:
28869 remove_AT (die, a->dw_attr);
28870 ix--;
28871 /* If we drop DW_AT_string_length, we need to drop also
28872 DW_AT_{string_length_,}byte_size. */
28873 remove_AT_byte_size = true;
28874 continue;
28875 default:
28876 break;
28877 case 1:
28878 /* Even if we keep the optimized DW_AT_string_length,
28879 it might have changed AT_class, so process it again. */
28880 ix--;
28881 continue;
28884 /* For -gdwarf-2 don't attempt to optimize
28885 DW_AT_data_member_location containing
28886 DW_OP_plus_uconst - older consumers might
28887 rely on it being that op instead of a more complex,
28888 but shorter, location description. */
28889 if ((dwarf_version > 2
28890 || a->dw_attr != DW_AT_data_member_location
28891 || l == NULL
28892 || l->dw_loc_opc != DW_OP_plus_uconst
28893 || l->dw_loc_next != NULL)
28894 && !resolve_addr_in_expr (a, l))
28896 if (dwarf_split_debug_info)
28897 remove_loc_list_addr_table_entries (l);
28898 if (l != NULL
28899 && l->dw_loc_next == NULL
28900 && l->dw_loc_opc == DW_OP_addr
28901 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
28902 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
28903 && a->dw_attr == DW_AT_location)
28905 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
28906 remove_AT (die, a->dw_attr);
28907 ix--;
28908 optimize_location_into_implicit_ptr (die, decl);
28909 break;
28911 if (a->dw_attr == DW_AT_string_length)
28912 /* If we drop DW_AT_string_length, we need to drop also
28913 DW_AT_{string_length_,}byte_size. */
28914 remove_AT_byte_size = true;
28915 remove_AT (die, a->dw_attr);
28916 ix--;
28918 else
28919 mark_base_types (l);
28921 break;
28922 case dw_val_class_addr:
28923 if (a->dw_attr == DW_AT_const_value
28924 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
28926 if (AT_index (a) != NOT_INDEXED)
28927 remove_addr_table_entry (a->dw_attr_val.val_entry);
28928 remove_AT (die, a->dw_attr);
28929 ix--;
28931 if ((die->die_tag == DW_TAG_call_site
28932 && a->dw_attr == DW_AT_call_origin)
28933 || (die->die_tag == DW_TAG_GNU_call_site
28934 && a->dw_attr == DW_AT_abstract_origin))
28936 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
28937 dw_die_ref tdie = lookup_decl_die (tdecl);
28938 dw_die_ref cdie;
28939 if (tdie == NULL
28940 && DECL_EXTERNAL (tdecl)
28941 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
28942 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
28944 dw_die_ref pdie = cdie;
28945 /* Make sure we don't add these DIEs into type units.
28946 We could emit skeleton DIEs for context (namespaces,
28947 outer structs/classes) and a skeleton DIE for the
28948 innermost context with DW_AT_signature pointing to the
28949 type unit. See PR78835. */
28950 while (pdie && pdie->die_tag != DW_TAG_type_unit)
28951 pdie = pdie->die_parent;
28952 if (pdie == NULL)
28954 /* Creating a full DIE for tdecl is overly expensive and
28955 at this point even wrong when in the LTO phase
28956 as it can end up generating new type DIEs we didn't
28957 output and thus optimize_external_refs will crash. */
28958 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
28959 add_AT_flag (tdie, DW_AT_external, 1);
28960 add_AT_flag (tdie, DW_AT_declaration, 1);
28961 add_linkage_attr (tdie, tdecl);
28962 add_name_and_src_coords_attributes (tdie, tdecl);
28963 equate_decl_number_to_die (tdecl, tdie);
28966 if (tdie)
28968 a->dw_attr_val.val_class = dw_val_class_die_ref;
28969 a->dw_attr_val.v.val_die_ref.die = tdie;
28970 a->dw_attr_val.v.val_die_ref.external = 0;
28972 else
28974 if (AT_index (a) != NOT_INDEXED)
28975 remove_addr_table_entry (a->dw_attr_val.val_entry);
28976 remove_AT (die, a->dw_attr);
28977 ix--;
28980 break;
28981 default:
28982 break;
28985 if (remove_AT_byte_size)
28986 remove_AT (die, dwarf_version >= 5
28987 ? DW_AT_string_length_byte_size
28988 : DW_AT_byte_size);
28990 FOR_EACH_CHILD (die, c, resolve_addr (c));
28993 /* Helper routines for optimize_location_lists.
28994 This pass tries to share identical local lists in .debug_loc
28995 section. */
28997 /* Iteratively hash operands of LOC opcode into HSTATE. */
28999 static void
29000 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
29002 dw_val_ref val1 = &loc->dw_loc_oprnd1;
29003 dw_val_ref val2 = &loc->dw_loc_oprnd2;
29005 switch (loc->dw_loc_opc)
29007 case DW_OP_const4u:
29008 case DW_OP_const8u:
29009 if (loc->dtprel)
29010 goto hash_addr;
29011 /* FALLTHRU */
29012 case DW_OP_const1u:
29013 case DW_OP_const1s:
29014 case DW_OP_const2u:
29015 case DW_OP_const2s:
29016 case DW_OP_const4s:
29017 case DW_OP_const8s:
29018 case DW_OP_constu:
29019 case DW_OP_consts:
29020 case DW_OP_pick:
29021 case DW_OP_plus_uconst:
29022 case DW_OP_breg0:
29023 case DW_OP_breg1:
29024 case DW_OP_breg2:
29025 case DW_OP_breg3:
29026 case DW_OP_breg4:
29027 case DW_OP_breg5:
29028 case DW_OP_breg6:
29029 case DW_OP_breg7:
29030 case DW_OP_breg8:
29031 case DW_OP_breg9:
29032 case DW_OP_breg10:
29033 case DW_OP_breg11:
29034 case DW_OP_breg12:
29035 case DW_OP_breg13:
29036 case DW_OP_breg14:
29037 case DW_OP_breg15:
29038 case DW_OP_breg16:
29039 case DW_OP_breg17:
29040 case DW_OP_breg18:
29041 case DW_OP_breg19:
29042 case DW_OP_breg20:
29043 case DW_OP_breg21:
29044 case DW_OP_breg22:
29045 case DW_OP_breg23:
29046 case DW_OP_breg24:
29047 case DW_OP_breg25:
29048 case DW_OP_breg26:
29049 case DW_OP_breg27:
29050 case DW_OP_breg28:
29051 case DW_OP_breg29:
29052 case DW_OP_breg30:
29053 case DW_OP_breg31:
29054 case DW_OP_regx:
29055 case DW_OP_fbreg:
29056 case DW_OP_piece:
29057 case DW_OP_deref_size:
29058 case DW_OP_xderef_size:
29059 hstate.add_object (val1->v.val_int);
29060 break;
29061 case DW_OP_skip:
29062 case DW_OP_bra:
29064 int offset;
29066 gcc_assert (val1->val_class == dw_val_class_loc);
29067 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
29068 hstate.add_object (offset);
29070 break;
29071 case DW_OP_implicit_value:
29072 hstate.add_object (val1->v.val_unsigned);
29073 switch (val2->val_class)
29075 case dw_val_class_const:
29076 hstate.add_object (val2->v.val_int);
29077 break;
29078 case dw_val_class_vec:
29080 unsigned int elt_size = val2->v.val_vec.elt_size;
29081 unsigned int len = val2->v.val_vec.length;
29083 hstate.add_int (elt_size);
29084 hstate.add_int (len);
29085 hstate.add (val2->v.val_vec.array, len * elt_size);
29087 break;
29088 case dw_val_class_const_double:
29089 hstate.add_object (val2->v.val_double.low);
29090 hstate.add_object (val2->v.val_double.high);
29091 break;
29092 case dw_val_class_wide_int:
29093 hstate.add (val2->v.val_wide->get_val (),
29094 get_full_len (*val2->v.val_wide)
29095 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
29096 break;
29097 case dw_val_class_addr:
29098 inchash::add_rtx (val2->v.val_addr, hstate);
29099 break;
29100 default:
29101 gcc_unreachable ();
29103 break;
29104 case DW_OP_bregx:
29105 case DW_OP_bit_piece:
29106 hstate.add_object (val1->v.val_int);
29107 hstate.add_object (val2->v.val_int);
29108 break;
29109 case DW_OP_addr:
29110 hash_addr:
29111 if (loc->dtprel)
29113 unsigned char dtprel = 0xd1;
29114 hstate.add_object (dtprel);
29116 inchash::add_rtx (val1->v.val_addr, hstate);
29117 break;
29118 case DW_OP_GNU_addr_index:
29119 case DW_OP_GNU_const_index:
29121 if (loc->dtprel)
29123 unsigned char dtprel = 0xd1;
29124 hstate.add_object (dtprel);
29126 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
29128 break;
29129 case DW_OP_implicit_pointer:
29130 case DW_OP_GNU_implicit_pointer:
29131 hstate.add_int (val2->v.val_int);
29132 break;
29133 case DW_OP_entry_value:
29134 case DW_OP_GNU_entry_value:
29135 hstate.add_object (val1->v.val_loc);
29136 break;
29137 case DW_OP_regval_type:
29138 case DW_OP_deref_type:
29139 case DW_OP_GNU_regval_type:
29140 case DW_OP_GNU_deref_type:
29142 unsigned int byte_size
29143 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
29144 unsigned int encoding
29145 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
29146 hstate.add_object (val1->v.val_int);
29147 hstate.add_object (byte_size);
29148 hstate.add_object (encoding);
29150 break;
29151 case DW_OP_convert:
29152 case DW_OP_reinterpret:
29153 case DW_OP_GNU_convert:
29154 case DW_OP_GNU_reinterpret:
29155 if (val1->val_class == dw_val_class_unsigned_const)
29157 hstate.add_object (val1->v.val_unsigned);
29158 break;
29160 /* FALLTHRU */
29161 case DW_OP_const_type:
29162 case DW_OP_GNU_const_type:
29164 unsigned int byte_size
29165 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
29166 unsigned int encoding
29167 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
29168 hstate.add_object (byte_size);
29169 hstate.add_object (encoding);
29170 if (loc->dw_loc_opc != DW_OP_const_type
29171 && loc->dw_loc_opc != DW_OP_GNU_const_type)
29172 break;
29173 hstate.add_object (val2->val_class);
29174 switch (val2->val_class)
29176 case dw_val_class_const:
29177 hstate.add_object (val2->v.val_int);
29178 break;
29179 case dw_val_class_vec:
29181 unsigned int elt_size = val2->v.val_vec.elt_size;
29182 unsigned int len = val2->v.val_vec.length;
29184 hstate.add_object (elt_size);
29185 hstate.add_object (len);
29186 hstate.add (val2->v.val_vec.array, len * elt_size);
29188 break;
29189 case dw_val_class_const_double:
29190 hstate.add_object (val2->v.val_double.low);
29191 hstate.add_object (val2->v.val_double.high);
29192 break;
29193 case dw_val_class_wide_int:
29194 hstate.add (val2->v.val_wide->get_val (),
29195 get_full_len (*val2->v.val_wide)
29196 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
29197 break;
29198 default:
29199 gcc_unreachable ();
29202 break;
29204 default:
29205 /* Other codes have no operands. */
29206 break;
29210 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
29212 static inline void
29213 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
29215 dw_loc_descr_ref l;
29216 bool sizes_computed = false;
29217 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
29218 size_of_locs (loc);
29220 for (l = loc; l != NULL; l = l->dw_loc_next)
29222 enum dwarf_location_atom opc = l->dw_loc_opc;
29223 hstate.add_object (opc);
29224 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
29226 size_of_locs (loc);
29227 sizes_computed = true;
29229 hash_loc_operands (l, hstate);
29233 /* Compute hash of the whole location list LIST_HEAD. */
29235 static inline void
29236 hash_loc_list (dw_loc_list_ref list_head)
29238 dw_loc_list_ref curr = list_head;
29239 inchash::hash hstate;
29241 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
29243 hstate.add (curr->begin, strlen (curr->begin) + 1);
29244 hstate.add (curr->end, strlen (curr->end) + 1);
29245 if (curr->section)
29246 hstate.add (curr->section, strlen (curr->section) + 1);
29247 hash_locs (curr->expr, hstate);
29249 list_head->hash = hstate.end ();
29252 /* Return true if X and Y opcodes have the same operands. */
29254 static inline bool
29255 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
29257 dw_val_ref valx1 = &x->dw_loc_oprnd1;
29258 dw_val_ref valx2 = &x->dw_loc_oprnd2;
29259 dw_val_ref valy1 = &y->dw_loc_oprnd1;
29260 dw_val_ref valy2 = &y->dw_loc_oprnd2;
29262 switch (x->dw_loc_opc)
29264 case DW_OP_const4u:
29265 case DW_OP_const8u:
29266 if (x->dtprel)
29267 goto hash_addr;
29268 /* FALLTHRU */
29269 case DW_OP_const1u:
29270 case DW_OP_const1s:
29271 case DW_OP_const2u:
29272 case DW_OP_const2s:
29273 case DW_OP_const4s:
29274 case DW_OP_const8s:
29275 case DW_OP_constu:
29276 case DW_OP_consts:
29277 case DW_OP_pick:
29278 case DW_OP_plus_uconst:
29279 case DW_OP_breg0:
29280 case DW_OP_breg1:
29281 case DW_OP_breg2:
29282 case DW_OP_breg3:
29283 case DW_OP_breg4:
29284 case DW_OP_breg5:
29285 case DW_OP_breg6:
29286 case DW_OP_breg7:
29287 case DW_OP_breg8:
29288 case DW_OP_breg9:
29289 case DW_OP_breg10:
29290 case DW_OP_breg11:
29291 case DW_OP_breg12:
29292 case DW_OP_breg13:
29293 case DW_OP_breg14:
29294 case DW_OP_breg15:
29295 case DW_OP_breg16:
29296 case DW_OP_breg17:
29297 case DW_OP_breg18:
29298 case DW_OP_breg19:
29299 case DW_OP_breg20:
29300 case DW_OP_breg21:
29301 case DW_OP_breg22:
29302 case DW_OP_breg23:
29303 case DW_OP_breg24:
29304 case DW_OP_breg25:
29305 case DW_OP_breg26:
29306 case DW_OP_breg27:
29307 case DW_OP_breg28:
29308 case DW_OP_breg29:
29309 case DW_OP_breg30:
29310 case DW_OP_breg31:
29311 case DW_OP_regx:
29312 case DW_OP_fbreg:
29313 case DW_OP_piece:
29314 case DW_OP_deref_size:
29315 case DW_OP_xderef_size:
29316 return valx1->v.val_int == valy1->v.val_int;
29317 case DW_OP_skip:
29318 case DW_OP_bra:
29319 /* If splitting debug info, the use of DW_OP_GNU_addr_index
29320 can cause irrelevant differences in dw_loc_addr. */
29321 gcc_assert (valx1->val_class == dw_val_class_loc
29322 && valy1->val_class == dw_val_class_loc
29323 && (dwarf_split_debug_info
29324 || x->dw_loc_addr == y->dw_loc_addr));
29325 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
29326 case DW_OP_implicit_value:
29327 if (valx1->v.val_unsigned != valy1->v.val_unsigned
29328 || valx2->val_class != valy2->val_class)
29329 return false;
29330 switch (valx2->val_class)
29332 case dw_val_class_const:
29333 return valx2->v.val_int == valy2->v.val_int;
29334 case dw_val_class_vec:
29335 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29336 && valx2->v.val_vec.length == valy2->v.val_vec.length
29337 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29338 valx2->v.val_vec.elt_size
29339 * valx2->v.val_vec.length) == 0;
29340 case dw_val_class_const_double:
29341 return valx2->v.val_double.low == valy2->v.val_double.low
29342 && valx2->v.val_double.high == valy2->v.val_double.high;
29343 case dw_val_class_wide_int:
29344 return *valx2->v.val_wide == *valy2->v.val_wide;
29345 case dw_val_class_addr:
29346 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
29347 default:
29348 gcc_unreachable ();
29350 case DW_OP_bregx:
29351 case DW_OP_bit_piece:
29352 return valx1->v.val_int == valy1->v.val_int
29353 && valx2->v.val_int == valy2->v.val_int;
29354 case DW_OP_addr:
29355 hash_addr:
29356 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
29357 case DW_OP_GNU_addr_index:
29358 case DW_OP_GNU_const_index:
29360 rtx ax1 = valx1->val_entry->addr.rtl;
29361 rtx ay1 = valy1->val_entry->addr.rtl;
29362 return rtx_equal_p (ax1, ay1);
29364 case DW_OP_implicit_pointer:
29365 case DW_OP_GNU_implicit_pointer:
29366 return valx1->val_class == dw_val_class_die_ref
29367 && valx1->val_class == valy1->val_class
29368 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
29369 && valx2->v.val_int == valy2->v.val_int;
29370 case DW_OP_entry_value:
29371 case DW_OP_GNU_entry_value:
29372 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
29373 case DW_OP_const_type:
29374 case DW_OP_GNU_const_type:
29375 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
29376 || valx2->val_class != valy2->val_class)
29377 return false;
29378 switch (valx2->val_class)
29380 case dw_val_class_const:
29381 return valx2->v.val_int == valy2->v.val_int;
29382 case dw_val_class_vec:
29383 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29384 && valx2->v.val_vec.length == valy2->v.val_vec.length
29385 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29386 valx2->v.val_vec.elt_size
29387 * valx2->v.val_vec.length) == 0;
29388 case dw_val_class_const_double:
29389 return valx2->v.val_double.low == valy2->v.val_double.low
29390 && valx2->v.val_double.high == valy2->v.val_double.high;
29391 case dw_val_class_wide_int:
29392 return *valx2->v.val_wide == *valy2->v.val_wide;
29393 default:
29394 gcc_unreachable ();
29396 case DW_OP_regval_type:
29397 case DW_OP_deref_type:
29398 case DW_OP_GNU_regval_type:
29399 case DW_OP_GNU_deref_type:
29400 return valx1->v.val_int == valy1->v.val_int
29401 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
29402 case DW_OP_convert:
29403 case DW_OP_reinterpret:
29404 case DW_OP_GNU_convert:
29405 case DW_OP_GNU_reinterpret:
29406 if (valx1->val_class != valy1->val_class)
29407 return false;
29408 if (valx1->val_class == dw_val_class_unsigned_const)
29409 return valx1->v.val_unsigned == valy1->v.val_unsigned;
29410 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29411 case DW_OP_GNU_parameter_ref:
29412 return valx1->val_class == dw_val_class_die_ref
29413 && valx1->val_class == valy1->val_class
29414 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29415 default:
29416 /* Other codes have no operands. */
29417 return true;
29421 /* Return true if DWARF location expressions X and Y are the same. */
29423 static inline bool
29424 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
29426 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
29427 if (x->dw_loc_opc != y->dw_loc_opc
29428 || x->dtprel != y->dtprel
29429 || !compare_loc_operands (x, y))
29430 break;
29431 return x == NULL && y == NULL;
29434 /* Hashtable helpers. */
29436 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
29438 static inline hashval_t hash (const dw_loc_list_struct *);
29439 static inline bool equal (const dw_loc_list_struct *,
29440 const dw_loc_list_struct *);
29443 /* Return precomputed hash of location list X. */
29445 inline hashval_t
29446 loc_list_hasher::hash (const dw_loc_list_struct *x)
29448 return x->hash;
29451 /* Return true if location lists A and B are the same. */
29453 inline bool
29454 loc_list_hasher::equal (const dw_loc_list_struct *a,
29455 const dw_loc_list_struct *b)
29457 if (a == b)
29458 return 1;
29459 if (a->hash != b->hash)
29460 return 0;
29461 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
29462 if (strcmp (a->begin, b->begin) != 0
29463 || strcmp (a->end, b->end) != 0
29464 || (a->section == NULL) != (b->section == NULL)
29465 || (a->section && strcmp (a->section, b->section) != 0)
29466 || !compare_locs (a->expr, b->expr))
29467 break;
29468 return a == NULL && b == NULL;
29471 typedef hash_table<loc_list_hasher> loc_list_hash_type;
29474 /* Recursively optimize location lists referenced from DIE
29475 children and share them whenever possible. */
29477 static void
29478 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
29480 dw_die_ref c;
29481 dw_attr_node *a;
29482 unsigned ix;
29483 dw_loc_list_struct **slot;
29485 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29486 if (AT_class (a) == dw_val_class_loc_list)
29488 dw_loc_list_ref list = AT_loc_list (a);
29489 /* TODO: perform some optimizations here, before hashing
29490 it and storing into the hash table. */
29491 hash_loc_list (list);
29492 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
29493 if (*slot == NULL)
29494 *slot = list;
29495 else
29496 a->dw_attr_val.v.val_loc_list = *slot;
29499 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
29503 /* Recursively assign each location list a unique index into the debug_addr
29504 section. */
29506 static void
29507 index_location_lists (dw_die_ref die)
29509 dw_die_ref c;
29510 dw_attr_node *a;
29511 unsigned ix;
29513 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29514 if (AT_class (a) == dw_val_class_loc_list)
29516 dw_loc_list_ref list = AT_loc_list (a);
29517 dw_loc_list_ref curr;
29518 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
29520 /* Don't index an entry that has already been indexed
29521 or won't be output. */
29522 if (curr->begin_entry != NULL
29523 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
29524 continue;
29526 curr->begin_entry
29527 = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
29531 FOR_EACH_CHILD (die, c, index_location_lists (c));
29534 /* Optimize location lists referenced from DIE
29535 children and share them whenever possible. */
29537 static void
29538 optimize_location_lists (dw_die_ref die)
29540 loc_list_hash_type htab (500);
29541 optimize_location_lists_1 (die, &htab);
29544 /* Traverse the limbo die list, and add parent/child links. The only
29545 dies without parents that should be here are concrete instances of
29546 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
29547 For concrete instances, we can get the parent die from the abstract
29548 instance. */
29550 static void
29551 flush_limbo_die_list (void)
29553 limbo_die_node *node;
29555 /* get_context_die calls force_decl_die, which can put new DIEs on the
29556 limbo list in LTO mode when nested functions are put in a different
29557 partition than that of their parent function. */
29558 while ((node = limbo_die_list))
29560 dw_die_ref die = node->die;
29561 limbo_die_list = node->next;
29563 if (die->die_parent == NULL)
29565 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
29567 if (origin && origin->die_parent)
29568 add_child_die (origin->die_parent, die);
29569 else if (is_cu_die (die))
29571 else if (seen_error ())
29572 /* It's OK to be confused by errors in the input. */
29573 add_child_die (comp_unit_die (), die);
29574 else
29576 /* In certain situations, the lexical block containing a
29577 nested function can be optimized away, which results
29578 in the nested function die being orphaned. Likewise
29579 with the return type of that nested function. Force
29580 this to be a child of the containing function.
29582 It may happen that even the containing function got fully
29583 inlined and optimized out. In that case we are lost and
29584 assign the empty child. This should not be big issue as
29585 the function is likely unreachable too. */
29586 gcc_assert (node->created_for);
29588 if (DECL_P (node->created_for))
29589 origin = get_context_die (DECL_CONTEXT (node->created_for));
29590 else if (TYPE_P (node->created_for))
29591 origin = scope_die_for (node->created_for, comp_unit_die ());
29592 else
29593 origin = comp_unit_die ();
29595 add_child_die (origin, die);
29601 /* Output stuff that dwarf requires at the end of every file,
29602 and generate the DWARF-2 debugging info. */
29604 static void
29605 dwarf2out_finish (const char *)
29607 comdat_type_node *ctnode;
29608 dw_die_ref main_comp_unit_die;
29609 unsigned char checksum[16];
29610 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
29612 /* Flush out any latecomers to the limbo party. */
29613 flush_limbo_die_list ();
29615 if (flag_checking)
29617 verify_die (comp_unit_die ());
29618 for (limbo_die_node *node = cu_die_list; node; node = node->next)
29619 verify_die (node->die);
29622 /* We shouldn't have any symbols with delayed asm names for
29623 DIEs generated after early finish. */
29624 gcc_assert (deferred_asm_name == NULL);
29626 gen_remaining_tmpl_value_param_die_attribute ();
29628 #if ENABLE_ASSERT_CHECKING
29630 dw_die_ref die = comp_unit_die (), c;
29631 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
29633 #endif
29634 resolve_addr (comp_unit_die ());
29635 move_marked_base_types ();
29637 /* Initialize sections and labels used for actual assembler output. */
29638 init_sections_and_labels ();
29640 /* Traverse the DIE's and add sibling attributes to those DIE's that
29641 have children. */
29642 add_sibling_attributes (comp_unit_die ());
29643 limbo_die_node *node;
29644 for (node = cu_die_list; node; node = node->next)
29645 add_sibling_attributes (node->die);
29646 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29647 add_sibling_attributes (ctnode->root_die);
29649 /* When splitting DWARF info, we put some attributes in the
29650 skeleton compile_unit DIE that remains in the .o, while
29651 most attributes go in the DWO compile_unit_die. */
29652 if (dwarf_split_debug_info)
29654 limbo_die_node *cu;
29655 main_comp_unit_die = gen_compile_unit_die (NULL);
29656 if (dwarf_version >= 5)
29657 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
29658 cu = limbo_die_list;
29659 gcc_assert (cu->die == main_comp_unit_die);
29660 limbo_die_list = limbo_die_list->next;
29661 cu->next = cu_die_list;
29662 cu_die_list = cu;
29664 else
29665 main_comp_unit_die = comp_unit_die ();
29667 /* Output a terminator label for the .text section. */
29668 switch_to_section (text_section);
29669 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
29670 if (cold_text_section)
29672 switch_to_section (cold_text_section);
29673 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
29676 /* We can only use the low/high_pc attributes if all of the code was
29677 in .text. */
29678 if (!have_multiple_function_sections
29679 || (dwarf_version < 3 && dwarf_strict))
29681 /* Don't add if the CU has no associated code. */
29682 if (text_section_used)
29683 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
29684 text_end_label, true);
29686 else
29688 unsigned fde_idx;
29689 dw_fde_ref fde;
29690 bool range_list_added = false;
29692 if (text_section_used)
29693 add_ranges_by_labels (main_comp_unit_die, text_section_label,
29694 text_end_label, &range_list_added, true);
29695 if (cold_text_section_used)
29696 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
29697 cold_end_label, &range_list_added, true);
29699 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
29701 if (DECL_IGNORED_P (fde->decl))
29702 continue;
29703 if (!fde->in_std_section)
29704 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
29705 fde->dw_fde_end, &range_list_added,
29706 true);
29707 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
29708 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
29709 fde->dw_fde_second_end, &range_list_added,
29710 true);
29713 if (range_list_added)
29715 /* We need to give .debug_loc and .debug_ranges an appropriate
29716 "base address". Use zero so that these addresses become
29717 absolute. Historically, we've emitted the unexpected
29718 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
29719 Emit both to give time for other tools to adapt. */
29720 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
29721 if (! dwarf_strict && dwarf_version < 4)
29722 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
29724 add_ranges (NULL);
29728 /* AIX Assembler inserts the length, so adjust the reference to match the
29729 offset expected by debuggers. */
29730 strcpy (dl_section_ref, debug_line_section_label);
29731 if (XCOFF_DEBUGGING_INFO)
29732 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
29734 if (debug_info_level >= DINFO_LEVEL_TERSE)
29735 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
29736 dl_section_ref);
29738 if (have_macinfo)
29739 add_AT_macptr (comp_unit_die (),
29740 dwarf_version >= 5 ? DW_AT_macros
29741 : dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
29742 macinfo_section_label);
29744 if (dwarf_split_debug_info)
29746 if (have_location_lists)
29748 if (dwarf_version >= 5)
29749 add_AT_loclistsptr (comp_unit_die (), DW_AT_loclists_base,
29750 loc_section_label);
29751 /* optimize_location_lists calculates the size of the lists,
29752 so index them first, and assign indices to the entries.
29753 Although optimize_location_lists will remove entries from
29754 the table, it only does so for duplicates, and therefore
29755 only reduces ref_counts to 1. */
29756 index_location_lists (comp_unit_die ());
29759 if (addr_index_table != NULL)
29761 unsigned int index = 0;
29762 addr_index_table
29763 ->traverse_noresize<unsigned int *, index_addr_table_entry>
29764 (&index);
29768 loc_list_idx = 0;
29769 if (have_location_lists)
29771 optimize_location_lists (comp_unit_die ());
29772 /* And finally assign indexes to the entries for -gsplit-dwarf. */
29773 if (dwarf_version >= 5 && dwarf_split_debug_info)
29774 assign_location_list_indexes (comp_unit_die ());
29777 save_macinfo_strings ();
29779 if (dwarf_split_debug_info)
29781 unsigned int index = 0;
29783 /* Add attributes common to skeleton compile_units and
29784 type_units. Because these attributes include strings, it
29785 must be done before freezing the string table. Top-level
29786 skeleton die attrs are added when the skeleton type unit is
29787 created, so ensure it is created by this point. */
29788 add_top_level_skeleton_die_attrs (main_comp_unit_die);
29789 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
29792 /* Output all of the compilation units. We put the main one last so that
29793 the offsets are available to output_pubnames. */
29794 for (node = cu_die_list; node; node = node->next)
29795 output_comp_unit (node->die, 0, NULL);
29797 hash_table<comdat_type_hasher> comdat_type_table (100);
29798 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29800 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
29802 /* Don't output duplicate types. */
29803 if (*slot != HTAB_EMPTY_ENTRY)
29804 continue;
29806 /* Add a pointer to the line table for the main compilation unit
29807 so that the debugger can make sense of DW_AT_decl_file
29808 attributes. */
29809 if (debug_info_level >= DINFO_LEVEL_TERSE)
29810 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
29811 (!dwarf_split_debug_info
29812 ? dl_section_ref
29813 : debug_skeleton_line_section_label));
29815 output_comdat_type_unit (ctnode);
29816 *slot = ctnode;
29819 /* The AT_pubnames attribute needs to go in all skeleton dies, including
29820 both the main_cu and all skeleton TUs. Making this call unconditional
29821 would end up either adding a second copy of the AT_pubnames attribute, or
29822 requiring a special case in add_top_level_skeleton_die_attrs. */
29823 if (!dwarf_split_debug_info)
29824 add_AT_pubnames (comp_unit_die ());
29826 if (dwarf_split_debug_info)
29828 int mark;
29829 struct md5_ctx ctx;
29831 if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
29832 index_rnglists ();
29834 /* Compute a checksum of the comp_unit to use as the dwo_id. */
29835 md5_init_ctx (&ctx);
29836 mark = 0;
29837 die_checksum (comp_unit_die (), &ctx, &mark);
29838 unmark_all_dies (comp_unit_die ());
29839 md5_finish_ctx (&ctx, checksum);
29841 if (dwarf_version < 5)
29843 /* Use the first 8 bytes of the checksum as the dwo_id,
29844 and add it to both comp-unit DIEs. */
29845 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
29846 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
29849 /* Add the base offset of the ranges table to the skeleton
29850 comp-unit DIE. */
29851 if (!vec_safe_is_empty (ranges_table))
29853 if (dwarf_version >= 5)
29854 add_AT_lineptr (main_comp_unit_die, DW_AT_rnglists_base,
29855 ranges_base_label);
29856 else
29857 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
29858 ranges_section_label);
29861 switch_to_section (debug_addr_section);
29862 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
29863 output_addr_table ();
29866 /* Output the main compilation unit if non-empty or if .debug_macinfo
29867 or .debug_macro will be emitted. */
29868 output_comp_unit (comp_unit_die (), have_macinfo,
29869 dwarf_split_debug_info ? checksum : NULL);
29871 if (dwarf_split_debug_info && info_section_emitted)
29872 output_skeleton_debug_sections (main_comp_unit_die, checksum);
29874 /* Output the abbreviation table. */
29875 if (vec_safe_length (abbrev_die_table) != 1)
29877 switch_to_section (debug_abbrev_section);
29878 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
29879 output_abbrev_section ();
29882 /* Output location list section if necessary. */
29883 if (have_location_lists)
29885 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
29886 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
29887 /* Output the location lists info. */
29888 switch_to_section (debug_loc_section);
29889 if (dwarf_version >= 5)
29891 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 1);
29892 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 2);
29893 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
29894 dw2_asm_output_data (4, 0xffffffff,
29895 "Initial length escape value indicating "
29896 "64-bit DWARF extension");
29897 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
29898 "Length of Location Lists");
29899 ASM_OUTPUT_LABEL (asm_out_file, l1);
29900 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
29901 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
29902 dw2_asm_output_data (1, 0, "Segment Size");
29903 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
29904 "Offset Entry Count");
29906 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
29907 if (dwarf_version >= 5 && dwarf_split_debug_info)
29909 unsigned int save_loc_list_idx = loc_list_idx;
29910 loc_list_idx = 0;
29911 output_loclists_offsets (comp_unit_die ());
29912 gcc_assert (save_loc_list_idx == loc_list_idx);
29914 output_location_lists (comp_unit_die ());
29915 if (dwarf_version >= 5)
29916 ASM_OUTPUT_LABEL (asm_out_file, l2);
29919 output_pubtables ();
29921 /* Output the address range information if a CU (.debug_info section)
29922 was emitted. We output an empty table even if we had no functions
29923 to put in it. This because the consumer has no way to tell the
29924 difference between an empty table that we omitted and failure to
29925 generate a table that would have contained data. */
29926 if (info_section_emitted)
29928 switch_to_section (debug_aranges_section);
29929 output_aranges ();
29932 /* Output ranges section if necessary. */
29933 if (!vec_safe_is_empty (ranges_table))
29935 if (dwarf_version >= 5)
29936 output_rnglists ();
29937 else
29938 output_ranges ();
29941 /* Have to end the macro section. */
29942 if (have_macinfo)
29944 switch_to_section (debug_macinfo_section);
29945 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
29946 output_macinfo ();
29947 dw2_asm_output_data (1, 0, "End compilation unit");
29950 /* Output the source line correspondence table. We must do this
29951 even if there is no line information. Otherwise, on an empty
29952 translation unit, we will generate a present, but empty,
29953 .debug_info section. IRIX 6.5 `nm' will then complain when
29954 examining the file. This is done late so that any filenames
29955 used by the debug_info section are marked as 'used'. */
29956 switch_to_section (debug_line_section);
29957 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
29958 if (! DWARF2_ASM_LINE_DEBUG_INFO)
29959 output_line_info (false);
29961 if (dwarf_split_debug_info && info_section_emitted)
29963 switch_to_section (debug_skeleton_line_section);
29964 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
29965 output_line_info (true);
29968 /* If we emitted any indirect strings, output the string table too. */
29969 if (debug_str_hash || skeleton_debug_str_hash)
29970 output_indirect_strings ();
29971 if (debug_line_str_hash)
29973 switch_to_section (debug_line_str_section);
29974 const enum dwarf_form form = DW_FORM_line_strp;
29975 debug_line_str_hash->traverse<enum dwarf_form,
29976 output_indirect_string> (form);
29980 /* Returns a hash value for X (which really is a variable_value_struct). */
29982 inline hashval_t
29983 variable_value_hasher::hash (variable_value_struct *x)
29985 return (hashval_t) x->decl_id;
29988 /* Return nonzero if decl_id of variable_value_struct X is the same as
29989 UID of decl Y. */
29991 inline bool
29992 variable_value_hasher::equal (variable_value_struct *x, tree y)
29994 return x->decl_id == DECL_UID (y);
29997 /* Helper function for resolve_variable_value, handle
29998 DW_OP_GNU_variable_value in one location expression.
29999 Return true if exprloc has been changed into loclist. */
30001 static bool
30002 resolve_variable_value_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
30004 dw_loc_descr_ref next;
30005 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = next)
30007 next = loc->dw_loc_next;
30008 if (loc->dw_loc_opc != DW_OP_GNU_variable_value
30009 || loc->dw_loc_oprnd1.val_class != dw_val_class_decl_ref)
30010 continue;
30012 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
30013 if (DECL_CONTEXT (decl) != current_function_decl)
30014 continue;
30016 dw_die_ref ref = lookup_decl_die (decl);
30017 if (ref)
30019 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30020 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30021 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30022 continue;
30024 dw_loc_list_ref l = loc_list_from_tree (decl, 0, NULL);
30025 if (l == NULL)
30026 continue;
30027 if (l->dw_loc_next)
30029 if (AT_class (a) != dw_val_class_loc)
30030 continue;
30031 switch (a->dw_attr)
30033 /* Following attributes allow both exprloc and loclist
30034 classes, so we can change them into a loclist. */
30035 case DW_AT_location:
30036 case DW_AT_string_length:
30037 case DW_AT_return_addr:
30038 case DW_AT_data_member_location:
30039 case DW_AT_frame_base:
30040 case DW_AT_segment:
30041 case DW_AT_static_link:
30042 case DW_AT_use_location:
30043 case DW_AT_vtable_elem_location:
30044 if (prev)
30046 prev->dw_loc_next = NULL;
30047 prepend_loc_descr_to_each (l, AT_loc (a));
30049 if (next)
30050 add_loc_descr_to_each (l, next);
30051 a->dw_attr_val.val_class = dw_val_class_loc_list;
30052 a->dw_attr_val.val_entry = NULL;
30053 a->dw_attr_val.v.val_loc_list = l;
30054 have_location_lists = true;
30055 return true;
30056 /* Following attributes allow both exprloc and reference,
30057 so if the whole expression is DW_OP_GNU_variable_value alone
30058 we could transform it into reference. */
30059 case DW_AT_byte_size:
30060 case DW_AT_bit_size:
30061 case DW_AT_lower_bound:
30062 case DW_AT_upper_bound:
30063 case DW_AT_bit_stride:
30064 case DW_AT_count:
30065 case DW_AT_allocated:
30066 case DW_AT_associated:
30067 case DW_AT_byte_stride:
30068 if (prev == NULL && next == NULL)
30069 break;
30070 /* FALLTHRU */
30071 default:
30072 if (dwarf_strict)
30073 continue;
30074 break;
30076 /* Create DW_TAG_variable that we can refer to. */
30077 gen_decl_die (decl, NULL_TREE, NULL,
30078 lookup_decl_die (current_function_decl));
30079 ref = lookup_decl_die (decl);
30080 if (ref)
30082 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30083 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30084 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30086 continue;
30088 if (prev)
30090 prev->dw_loc_next = l->expr;
30091 add_loc_descr (&prev->dw_loc_next, next);
30092 free_loc_descr (loc, NULL);
30093 next = prev->dw_loc_next;
30095 else
30097 memcpy (loc, l->expr, sizeof (dw_loc_descr_node));
30098 add_loc_descr (&loc, next);
30099 next = loc;
30101 loc = prev;
30103 return false;
30106 /* Attempt to resolve DW_OP_GNU_variable_value using loc_list_from_tree. */
30108 static void
30109 resolve_variable_value (dw_die_ref die)
30111 dw_attr_node *a;
30112 dw_loc_list_ref loc;
30113 unsigned ix;
30115 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30116 switch (AT_class (a))
30118 case dw_val_class_loc:
30119 if (!resolve_variable_value_in_expr (a, AT_loc (a)))
30120 break;
30121 /* FALLTHRU */
30122 case dw_val_class_loc_list:
30123 loc = AT_loc_list (a);
30124 gcc_assert (loc);
30125 for (; loc; loc = loc->dw_loc_next)
30126 resolve_variable_value_in_expr (a, loc->expr);
30127 break;
30128 default:
30129 break;
30133 /* Attempt to optimize DW_OP_GNU_variable_value refering to
30134 temporaries in the current function. */
30136 static void
30137 resolve_variable_values (void)
30139 if (!variable_value_hash || !current_function_decl)
30140 return;
30142 struct variable_value_struct *node
30143 = variable_value_hash->find_with_hash (current_function_decl,
30144 DECL_UID (current_function_decl));
30146 if (node == NULL)
30147 return;
30149 unsigned int i;
30150 dw_die_ref die;
30151 FOR_EACH_VEC_SAFE_ELT (node->dies, i, die)
30152 resolve_variable_value (die);
30155 /* Helper function for note_variable_value, handle one location
30156 expression. */
30158 static void
30159 note_variable_value_in_expr (dw_die_ref die, dw_loc_descr_ref loc)
30161 for (; loc; loc = loc->dw_loc_next)
30162 if (loc->dw_loc_opc == DW_OP_GNU_variable_value
30163 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30165 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
30166 dw_die_ref ref = lookup_decl_die (decl);
30167 if (ref)
30169 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30170 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30171 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30172 continue;
30174 if (VAR_P (decl)
30175 && DECL_CONTEXT (decl)
30176 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
30177 && lookup_decl_die (DECL_CONTEXT (decl)))
30179 if (!variable_value_hash)
30180 variable_value_hash
30181 = hash_table<variable_value_hasher>::create_ggc (10);
30183 tree fndecl = DECL_CONTEXT (decl);
30184 struct variable_value_struct *node;
30185 struct variable_value_struct **slot
30186 = variable_value_hash->find_slot_with_hash (fndecl,
30187 DECL_UID (fndecl),
30188 INSERT);
30189 if (*slot == NULL)
30191 node = ggc_cleared_alloc<variable_value_struct> ();
30192 node->decl_id = DECL_UID (fndecl);
30193 *slot = node;
30195 else
30196 node = *slot;
30198 vec_safe_push (node->dies, die);
30203 /* Walk the tree DIE and note DIEs with DW_OP_GNU_variable_value still
30204 with dw_val_class_decl_ref operand. */
30206 static void
30207 note_variable_value (dw_die_ref die)
30209 dw_die_ref c;
30210 dw_attr_node *a;
30211 dw_loc_list_ref loc;
30212 unsigned ix;
30214 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30215 switch (AT_class (a))
30217 case dw_val_class_loc_list:
30218 loc = AT_loc_list (a);
30219 gcc_assert (loc);
30220 if (!loc->noted_variable_value)
30222 loc->noted_variable_value = 1;
30223 for (; loc; loc = loc->dw_loc_next)
30224 note_variable_value_in_expr (die, loc->expr);
30226 break;
30227 case dw_val_class_loc:
30228 note_variable_value_in_expr (die, AT_loc (a));
30229 break;
30230 default:
30231 break;
30234 /* Mark children. */
30235 FOR_EACH_CHILD (die, c, note_variable_value (c));
30238 /* Perform any cleanups needed after the early debug generation pass
30239 has run. */
30241 static void
30242 dwarf2out_early_finish (const char *filename)
30244 set_early_dwarf s;
30246 /* PCH might result in DW_AT_producer string being restored from the
30247 header compilation, so always fill it with empty string initially
30248 and overwrite only here. */
30249 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
30250 producer_string = gen_producer_string ();
30251 producer->dw_attr_val.v.val_str->refcount--;
30252 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
30254 /* Add the name for the main input file now. We delayed this from
30255 dwarf2out_init to avoid complications with PCH. */
30256 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
30257 add_comp_dir_attribute (comp_unit_die ());
30259 /* When emitting DWARF5 .debug_line_str, move DW_AT_name and
30260 DW_AT_comp_dir into .debug_line_str section. */
30261 if (!DWARF2_ASM_LINE_DEBUG_INFO
30262 && dwarf_version >= 5
30263 && DWARF5_USE_DEBUG_LINE_STR)
30265 for (int i = 0; i < 2; i++)
30267 dw_attr_node *a = get_AT (comp_unit_die (),
30268 i ? DW_AT_comp_dir : DW_AT_name);
30269 if (a == NULL
30270 || AT_class (a) != dw_val_class_str
30271 || strlen (AT_string (a)) + 1 <= DWARF_OFFSET_SIZE)
30272 continue;
30274 if (! debug_line_str_hash)
30275 debug_line_str_hash
30276 = hash_table<indirect_string_hasher>::create_ggc (10);
30278 struct indirect_string_node *node
30279 = find_AT_string_in_table (AT_string (a), debug_line_str_hash);
30280 set_indirect_string (node);
30281 node->form = DW_FORM_line_strp;
30282 a->dw_attr_val.v.val_str->refcount--;
30283 a->dw_attr_val.v.val_str = node;
30287 /* With LTO early dwarf was really finished at compile-time, so make
30288 sure to adjust the phase after annotating the LTRANS CU DIE. */
30289 if (in_lto_p)
30291 early_dwarf_finished = true;
30292 return;
30295 /* Walk through the list of incomplete types again, trying once more to
30296 emit full debugging info for them. */
30297 retry_incomplete_types ();
30299 /* The point here is to flush out the limbo list so that it is empty
30300 and we don't need to stream it for LTO. */
30301 flush_limbo_die_list ();
30303 gen_scheduled_generic_parms_dies ();
30304 gen_remaining_tmpl_value_param_die_attribute ();
30306 /* Add DW_AT_linkage_name for all deferred DIEs. */
30307 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
30309 tree decl = node->created_for;
30310 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
30311 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
30312 ended up in deferred_asm_name before we knew it was
30313 constant and never written to disk. */
30314 && DECL_ASSEMBLER_NAME (decl))
30316 add_linkage_attr (node->die, decl);
30317 move_linkage_attr (node->die);
30320 deferred_asm_name = NULL;
30322 if (flag_eliminate_unused_debug_types)
30323 prune_unused_types ();
30325 /* Generate separate COMDAT sections for type DIEs. */
30326 if (use_debug_types)
30328 break_out_comdat_types (comp_unit_die ());
30330 /* Each new type_unit DIE was added to the limbo die list when created.
30331 Since these have all been added to comdat_type_list, clear the
30332 limbo die list. */
30333 limbo_die_list = NULL;
30335 /* For each new comdat type unit, copy declarations for incomplete
30336 types to make the new unit self-contained (i.e., no direct
30337 references to the main compile unit). */
30338 for (comdat_type_node *ctnode = comdat_type_list;
30339 ctnode != NULL; ctnode = ctnode->next)
30340 copy_decls_for_unworthy_types (ctnode->root_die);
30341 copy_decls_for_unworthy_types (comp_unit_die ());
30343 /* In the process of copying declarations from one unit to another,
30344 we may have left some declarations behind that are no longer
30345 referenced. Prune them. */
30346 prune_unused_types ();
30349 /* Generate separate CUs for each of the include files we've seen.
30350 They will go into limbo_die_list and from there to cu_die_list. */
30351 if (flag_eliminate_dwarf2_dups)
30353 gcc_assert (limbo_die_list == NULL);
30354 break_out_includes (comp_unit_die ());
30355 limbo_die_node *cu;
30356 while ((cu = limbo_die_list))
30358 limbo_die_list = cu->next;
30359 cu->next = cu_die_list;
30360 cu_die_list = cu;
30364 /* Traverse the DIE's and note DIEs with DW_OP_GNU_variable_value still
30365 with dw_val_class_decl_ref operand. */
30366 note_variable_value (comp_unit_die ());
30367 for (limbo_die_node *node = cu_die_list; node; node = node->next)
30368 note_variable_value (node->die);
30369 for (comdat_type_node *ctnode = comdat_type_list; ctnode != NULL;
30370 ctnode = ctnode->next)
30371 note_variable_value (ctnode->root_die);
30372 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
30373 note_variable_value (node->die);
30375 /* The early debug phase is now finished. */
30376 early_dwarf_finished = true;
30379 /* Reset all state within dwarf2out.c so that we can rerun the compiler
30380 within the same process. For use by toplev::finalize. */
30382 void
30383 dwarf2out_c_finalize (void)
30385 last_var_location_insn = NULL;
30386 cached_next_real_insn = NULL;
30387 used_rtx_array = NULL;
30388 incomplete_types = NULL;
30389 decl_scope_table = NULL;
30390 debug_info_section = NULL;
30391 debug_skeleton_info_section = NULL;
30392 debug_abbrev_section = NULL;
30393 debug_skeleton_abbrev_section = NULL;
30394 debug_aranges_section = NULL;
30395 debug_addr_section = NULL;
30396 debug_macinfo_section = NULL;
30397 debug_line_section = NULL;
30398 debug_skeleton_line_section = NULL;
30399 debug_loc_section = NULL;
30400 debug_pubnames_section = NULL;
30401 debug_pubtypes_section = NULL;
30402 debug_str_section = NULL;
30403 debug_line_str_section = NULL;
30404 debug_str_dwo_section = NULL;
30405 debug_str_offsets_section = NULL;
30406 debug_ranges_section = NULL;
30407 debug_frame_section = NULL;
30408 fde_vec = NULL;
30409 debug_str_hash = NULL;
30410 debug_line_str_hash = NULL;
30411 skeleton_debug_str_hash = NULL;
30412 dw2_string_counter = 0;
30413 have_multiple_function_sections = false;
30414 text_section_used = false;
30415 cold_text_section_used = false;
30416 cold_text_section = NULL;
30417 current_unit_personality = NULL;
30419 early_dwarf = false;
30420 early_dwarf_finished = false;
30422 next_die_offset = 0;
30423 single_comp_unit_die = NULL;
30424 comdat_type_list = NULL;
30425 limbo_die_list = NULL;
30426 file_table = NULL;
30427 decl_die_table = NULL;
30428 common_block_die_table = NULL;
30429 decl_loc_table = NULL;
30430 call_arg_locations = NULL;
30431 call_arg_loc_last = NULL;
30432 call_site_count = -1;
30433 tail_call_site_count = -1;
30434 cached_dw_loc_list_table = NULL;
30435 abbrev_die_table = NULL;
30436 delete dwarf_proc_stack_usage_map;
30437 dwarf_proc_stack_usage_map = NULL;
30438 line_info_label_num = 0;
30439 cur_line_info_table = NULL;
30440 text_section_line_info = NULL;
30441 cold_text_section_line_info = NULL;
30442 separate_line_info = NULL;
30443 info_section_emitted = false;
30444 pubname_table = NULL;
30445 pubtype_table = NULL;
30446 macinfo_table = NULL;
30447 ranges_table = NULL;
30448 ranges_by_label = NULL;
30449 rnglist_idx = 0;
30450 have_location_lists = false;
30451 loclabel_num = 0;
30452 poc_label_num = 0;
30453 last_emitted_file = NULL;
30454 label_num = 0;
30455 tmpl_value_parm_die_table = NULL;
30456 generic_type_instances = NULL;
30457 frame_pointer_fb_offset = 0;
30458 frame_pointer_fb_offset_valid = false;
30459 base_types.release ();
30460 XDELETEVEC (producer_string);
30461 producer_string = NULL;
30464 #include "gt-dwarf2out.h"