libgcc: Fix BIL_TYPE_SIZE == 32 support in _BitInt <-> dfp support
[official-gcc.git] / gcc / dwarf2out.cc
blob5d64100b95c56ac9b76718fa51a649fe0f722144
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2024 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 "dwarf2ctf.h"
83 #include "dwarf2asm.h"
84 #include "toplev.h"
85 #include "md5.h"
86 #include "tree-pretty-print.h"
87 #include "print-rtl.h"
88 #include "debug.h"
89 #include "common/common-target.h"
90 #include "langhooks.h"
91 #include "lra.h"
92 #include "dumpfile.h"
93 #include "opts.h"
94 #include "tree-dfa.h"
95 #include "gdb/gdb-index.h"
96 #include "rtl-iter.h"
97 #include "stringpool.h"
98 #include "attribs.h"
99 #include "file-prefix-map.h" /* remap_debug_filename() */
101 static void dwarf2out_source_line (unsigned int, unsigned int, const char *,
102 int, bool);
103 static rtx_insn *last_var_location_insn;
104 static rtx_insn *cached_next_real_insn;
105 static void dwarf2out_decl (tree);
106 static bool is_redundant_typedef (const_tree);
108 #ifndef XCOFF_DEBUGGING_INFO
109 #define XCOFF_DEBUGGING_INFO 0
110 #endif
112 #ifndef HAVE_XCOFF_DWARF_EXTRAS
113 #define HAVE_XCOFF_DWARF_EXTRAS 0
114 #endif
116 #ifdef VMS_DEBUGGING_INFO
117 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
119 /* Define this macro to be a nonzero value if the directory specifications
120 which are output in the debug info should end with a separator. */
121 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
122 /* Define this macro to evaluate to a nonzero value if GCC should refrain
123 from generating indirect strings in DWARF2 debug information, for instance
124 if your target is stuck with an old version of GDB that is unable to
125 process them properly or uses VMS Debug. */
126 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
127 #else
128 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
129 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
130 #endif
132 /* ??? Poison these here until it can be done generically. They've been
133 totally replaced in this file; make sure it stays that way. */
134 #undef DWARF2_UNWIND_INFO
135 #undef DWARF2_FRAME_INFO
136 #if (GCC_VERSION >= 3000)
137 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
138 #endif
140 /* The size of the target's pointer type. */
141 #ifndef PTR_SIZE
142 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
143 #endif
145 /* Array of RTXes referenced by the debugging information, which therefore
146 must be kept around forever. */
147 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
149 /* A pointer to the base of a list of incomplete types which might be
150 completed at some later time. incomplete_types_list needs to be a
151 vec<tree, va_gc> *because we want to tell the garbage collector about
152 it. */
153 static GTY(()) vec<tree, va_gc> *incomplete_types;
155 /* Pointers to various DWARF2 sections. */
156 static GTY(()) section *debug_info_section;
157 static GTY(()) section *debug_skeleton_info_section;
158 static GTY(()) section *debug_abbrev_section;
159 static GTY(()) section *debug_skeleton_abbrev_section;
160 static GTY(()) section *debug_aranges_section;
161 static GTY(()) section *debug_addr_section;
162 static GTY(()) section *debug_macinfo_section;
163 static const char *debug_macinfo_section_name;
164 static unsigned macinfo_label_base = 1;
165 static GTY(()) section *debug_line_section;
166 static GTY(()) section *debug_skeleton_line_section;
167 static GTY(()) section *debug_loc_section;
168 static GTY(()) section *debug_pubnames_section;
169 static GTY(()) section *debug_pubtypes_section;
170 static GTY(()) section *debug_str_section;
171 static GTY(()) section *debug_line_str_section;
172 static GTY(()) section *debug_str_dwo_section;
173 static GTY(()) section *debug_str_offsets_section;
174 static GTY(()) section *debug_ranges_section;
175 static GTY(()) section *debug_ranges_dwo_section;
176 static GTY(()) section *debug_frame_section;
178 /* Maximum size (in bytes) of an artificially generated label. */
179 #define MAX_ARTIFICIAL_LABEL_BYTES 40
181 /* According to the (draft) DWARF 3 specification, the initial length
182 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
183 bytes are 0xffffffff, followed by the length stored in the next 8
184 bytes.
186 However, the SGI/MIPS ABI uses an initial length which is equal to
187 dwarf_offset_size. It is defined (elsewhere) accordingly. */
189 #ifndef DWARF_INITIAL_LENGTH_SIZE
190 #define DWARF_INITIAL_LENGTH_SIZE (dwarf_offset_size == 4 ? 4 : 12)
191 #endif
193 #ifndef DWARF_INITIAL_LENGTH_SIZE_STR
194 #define DWARF_INITIAL_LENGTH_SIZE_STR (dwarf_offset_size == 4 ? "-4" : "-12")
195 #endif
197 /* Round SIZE up to the nearest BOUNDARY. */
198 #define DWARF_ROUND(SIZE,BOUNDARY) \
199 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
201 /* CIE identifier. */
202 #if HOST_BITS_PER_WIDE_INT >= 64
203 #define DWARF_CIE_ID \
204 (unsigned HOST_WIDE_INT) (dwarf_offset_size == 4 ? DW_CIE_ID : DW64_CIE_ID)
205 #else
206 #define DWARF_CIE_ID DW_CIE_ID
207 #endif
210 /* A vector for a table that contains frame description
211 information for each routine. */
212 #define NOT_INDEXED (-1U)
213 #define NO_INDEX_ASSIGNED (-2U)
215 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
217 struct GTY((for_user)) indirect_string_node {
218 const char *str;
219 unsigned int refcount;
220 enum dwarf_form form;
221 char *label;
222 unsigned int index;
225 struct indirect_string_hasher : ggc_ptr_hash<indirect_string_node>
227 typedef const char *compare_type;
229 static hashval_t hash (indirect_string_node *);
230 static bool equal (indirect_string_node *, const char *);
233 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
235 static GTY (()) hash_table<indirect_string_hasher> *debug_line_str_hash;
237 /* With split_debug_info, both the comp_dir and dwo_name go in the
238 main object file, rather than the dwo, similar to the force_direct
239 parameter elsewhere but with additional complications:
241 1) The string is needed in both the main object file and the dwo.
242 That is, the comp_dir and dwo_name will appear in both places.
244 2) Strings can use four forms: DW_FORM_string, DW_FORM_strp,
245 DW_FORM_line_strp or DW_FORM_strx/GNU_str_index.
247 3) GCC chooses the form to use late, depending on the size and
248 reference count.
250 Rather than forcing the all debug string handling functions and
251 callers to deal with these complications, simply use a separate,
252 special-cased string table for any attribute that should go in the
253 main object file. This limits the complexity to just the places
254 that need it. */
256 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
258 static GTY(()) int dw2_string_counter;
260 /* True if the compilation unit places functions in more than one section. */
261 static GTY(()) bool have_multiple_function_sections = false;
263 /* The default cold text section. */
264 static GTY(()) section *cold_text_section;
266 /* True if currently in text section. */
267 static GTY(()) bool in_text_section_p = false;
269 /* Last debug-on location in corresponding section. */
270 static GTY(()) const char *last_text_label;
271 static GTY(()) const char *last_cold_label;
273 /* Mark debug-on/off locations per section.
274 NULL means the section is not used at all. */
275 static GTY(()) vec<const char *, va_gc> *switch_text_ranges;
276 static GTY(()) vec<const char *, va_gc> *switch_cold_ranges;
278 /* The DIE for C++14 'auto' in a function return type. */
279 static GTY(()) dw_die_ref auto_die;
281 /* The DIE for C++14 'decltype(auto)' in a function return type. */
282 static GTY(()) dw_die_ref decltype_auto_die;
284 /* Forward declarations for functions defined in this file. */
286 static void output_call_frame_info (int);
288 /* Personality decl of current unit. Used only when assembler does not support
289 personality CFI. */
290 static GTY(()) rtx current_unit_personality;
292 /* Whether an eh_frame section is required. */
293 static GTY(()) bool do_eh_frame = false;
295 /* .debug_rnglists next index. */
296 static unsigned int rnglist_idx;
298 /* Data and reference forms for relocatable data. */
299 #define DW_FORM_data (dwarf_offset_size == 8 ? DW_FORM_data8 : DW_FORM_data4)
300 #define DW_FORM_ref (dwarf_offset_size == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
302 #ifndef DEBUG_FRAME_SECTION
303 #define DEBUG_FRAME_SECTION ".debug_frame"
304 #endif
306 #ifndef FUNC_BEGIN_LABEL
307 #define FUNC_BEGIN_LABEL "LFB"
308 #endif
310 #ifndef FUNC_SECOND_SECT_LABEL
311 #define FUNC_SECOND_SECT_LABEL "LFSB"
312 #endif
314 #ifndef FUNC_END_LABEL
315 #define FUNC_END_LABEL "LFE"
316 #endif
318 #ifndef PROLOGUE_END_LABEL
319 #define PROLOGUE_END_LABEL "LPE"
320 #endif
322 #ifndef EPILOGUE_BEGIN_LABEL
323 #define EPILOGUE_BEGIN_LABEL "LEB"
324 #endif
326 #ifndef FRAME_BEGIN_LABEL
327 #define FRAME_BEGIN_LABEL "Lframe"
328 #endif
329 #define CIE_AFTER_SIZE_LABEL "LSCIE"
330 #define CIE_END_LABEL "LECIE"
331 #define FDE_LABEL "LSFDE"
332 #define FDE_AFTER_SIZE_LABEL "LASFDE"
333 #define FDE_END_LABEL "LEFDE"
334 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
335 #define LINE_NUMBER_END_LABEL "LELT"
336 #define LN_PROLOG_AS_LABEL "LASLTP"
337 #define LN_PROLOG_END_LABEL "LELTP"
338 #define DIE_LABEL_PREFIX "DW"
340 /* Match the base name of a file to the base name of a compilation unit. */
342 static bool
343 matches_main_base (const char *path)
345 /* Cache the last query. */
346 static const char *last_path = NULL;
347 static bool last_match = false;
348 if (path != last_path)
350 const char *base;
351 int length = base_of_path (path, &base);
352 last_path = path;
353 last_match = (length == main_input_baselength
354 && memcmp (base, main_input_basename, length) == 0);
356 return last_match;
359 #ifdef DEBUG_DEBUG_STRUCT
361 static bool
362 dump_struct_debug (tree type, enum debug_info_usage usage,
363 enum debug_struct_file criterion, int generic,
364 bool matches, bool result)
366 /* Find the type name. */
367 tree type_decl = TYPE_STUB_DECL (type);
368 tree t = type_decl;
369 const char *name = 0;
370 if (TREE_CODE (t) == TYPE_DECL)
371 t = DECL_NAME (t);
372 if (t)
373 name = IDENTIFIER_POINTER (t);
375 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
376 criterion,
377 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
378 matches ? "bas" : "hdr",
379 generic ? "gen" : "ord",
380 usage == DINFO_USAGE_DFN ? ";" :
381 usage == DINFO_USAGE_DIR_USE ? "." : "*",
382 result,
383 (void*) type_decl, name);
384 return result;
386 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
387 dump_struct_debug (type, usage, criterion, generic, matches, result)
389 #else
391 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
392 (result)
394 #endif
396 /* Get the number of HOST_WIDE_INTs needed to represent the precision
397 of the number. */
399 static unsigned int
400 get_full_len (const dw_wide_int &op)
402 return CEIL (op.get_precision (), HOST_BITS_PER_WIDE_INT);
405 static bool
406 should_emit_struct_debug (tree type, enum debug_info_usage usage)
408 if (debug_info_level <= DINFO_LEVEL_TERSE)
409 return false;
411 enum debug_struct_file criterion;
412 tree type_decl;
413 bool generic = lang_hooks.types.generic_p (type);
415 if (generic)
416 criterion = debug_struct_generic[usage];
417 else
418 criterion = debug_struct_ordinary[usage];
420 if (criterion == DINFO_STRUCT_FILE_NONE)
421 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
422 if (criterion == DINFO_STRUCT_FILE_ANY)
423 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
425 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
427 if (type_decl != NULL)
429 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
430 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
432 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
433 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
436 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
439 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
440 switch to the data section instead, and write out a synthetic start label
441 for collect2 the first time around. */
443 static void
444 switch_to_eh_frame_section (bool back ATTRIBUTE_UNUSED)
446 if (eh_frame_section == 0)
448 int flags;
450 if (EH_TABLES_CAN_BE_READ_ONLY)
452 int fde_encoding;
453 int per_encoding;
454 int lsda_encoding;
456 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
457 /*global=*/0);
458 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
459 /*global=*/1);
460 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
461 /*global=*/0);
462 flags = ((! flag_pic
463 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
464 && (fde_encoding & 0x70) != DW_EH_PE_aligned
465 && (per_encoding & 0x70) != DW_EH_PE_absptr
466 && (per_encoding & 0x70) != DW_EH_PE_aligned
467 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
468 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
469 ? 0 : SECTION_WRITE);
471 else
472 flags = SECTION_WRITE;
474 #ifdef EH_FRAME_SECTION_NAME
475 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
476 #else
477 eh_frame_section = ((flags == SECTION_WRITE)
478 ? data_section : readonly_data_section);
479 #endif /* EH_FRAME_SECTION_NAME */
482 switch_to_section (eh_frame_section);
484 #ifdef EH_FRAME_THROUGH_COLLECT2
485 /* We have no special eh_frame section. Emit special labels to guide
486 collect2. */
487 if (!back)
489 tree label = get_file_function_name ("F");
490 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
491 targetm.asm_out.globalize_label (asm_out_file,
492 IDENTIFIER_POINTER (label));
493 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
495 #endif
498 /* Switch [BACK] to the eh or debug frame table section, depending on
499 FOR_EH. */
501 static void
502 switch_to_frame_table_section (int for_eh, bool back)
504 if (for_eh)
505 switch_to_eh_frame_section (back);
506 else
508 if (!debug_frame_section)
509 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
510 SECTION_DEBUG, NULL);
511 switch_to_section (debug_frame_section);
515 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
517 enum dw_cfi_oprnd_type
518 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
520 switch (cfi)
522 case DW_CFA_nop:
523 case DW_CFA_GNU_window_save:
524 case DW_CFA_remember_state:
525 case DW_CFA_restore_state:
526 return dw_cfi_oprnd_unused;
528 case DW_CFA_set_loc:
529 case DW_CFA_advance_loc1:
530 case DW_CFA_advance_loc2:
531 case DW_CFA_advance_loc4:
532 case DW_CFA_MIPS_advance_loc8:
533 return dw_cfi_oprnd_addr;
535 case DW_CFA_offset:
536 case DW_CFA_offset_extended:
537 case DW_CFA_def_cfa:
538 case DW_CFA_offset_extended_sf:
539 case DW_CFA_def_cfa_sf:
540 case DW_CFA_restore:
541 case DW_CFA_restore_extended:
542 case DW_CFA_undefined:
543 case DW_CFA_same_value:
544 case DW_CFA_def_cfa_register:
545 case DW_CFA_register:
546 case DW_CFA_expression:
547 case DW_CFA_val_expression:
548 return dw_cfi_oprnd_reg_num;
550 case DW_CFA_def_cfa_offset:
551 case DW_CFA_GNU_args_size:
552 case DW_CFA_def_cfa_offset_sf:
553 return dw_cfi_oprnd_offset;
555 case DW_CFA_def_cfa_expression:
556 return dw_cfi_oprnd_loc;
558 default:
559 gcc_unreachable ();
563 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
565 enum dw_cfi_oprnd_type
566 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
568 switch (cfi)
570 case DW_CFA_def_cfa:
571 case DW_CFA_def_cfa_sf:
572 case DW_CFA_offset:
573 case DW_CFA_offset_extended_sf:
574 case DW_CFA_offset_extended:
575 return dw_cfi_oprnd_offset;
577 case DW_CFA_register:
578 return dw_cfi_oprnd_reg_num;
580 case DW_CFA_expression:
581 case DW_CFA_val_expression:
582 return dw_cfi_oprnd_loc;
584 case DW_CFA_def_cfa_expression:
585 return dw_cfi_oprnd_cfa_loc;
587 default:
588 return dw_cfi_oprnd_unused;
592 /* Output one FDE. */
594 static void
595 output_fde (dw_fde_ref fde, bool for_eh, bool second,
596 char *section_start_label, int fde_encoding, char *augmentation,
597 bool any_lsda_needed, int lsda_encoding)
599 const char *begin, *end;
600 static unsigned int j;
601 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
603 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
604 /* empty */ 0);
605 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
606 for_eh + j);
607 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
608 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
609 if (!XCOFF_DEBUGGING_INFO || for_eh)
611 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4 && !for_eh)
612 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
613 " indicating 64-bit DWARF extension");
614 dw2_asm_output_delta (for_eh ? 4 : dwarf_offset_size, l2, l1,
615 "FDE Length");
617 ASM_OUTPUT_LABEL (asm_out_file, l1);
619 if (for_eh)
620 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
621 else
622 dw2_asm_output_offset (dwarf_offset_size, section_start_label,
623 debug_frame_section, "FDE CIE offset");
625 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
626 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
628 if (for_eh)
630 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
631 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
632 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
633 "FDE initial location");
634 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
635 end, begin, "FDE address range");
637 else
639 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
640 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
643 if (augmentation[0])
645 if (any_lsda_needed)
647 int size = size_of_encoded_value (lsda_encoding);
649 if (lsda_encoding == DW_EH_PE_aligned)
651 int offset = ( 4 /* Length */
652 + 4 /* CIE offset */
653 + 2 * size_of_encoded_value (fde_encoding)
654 + 1 /* Augmentation size */ );
655 int pad = -offset & (PTR_SIZE - 1);
657 size += pad;
658 gcc_assert (size_of_uleb128 (size) == 1);
661 dw2_asm_output_data_uleb128 (size, "Augmentation size");
663 if (fde->uses_eh_lsda)
665 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
666 fde->funcdef_number);
667 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
668 gen_rtx_SYMBOL_REF (Pmode, l1),
669 false,
670 "Language Specific Data Area");
672 else
674 if (lsda_encoding == DW_EH_PE_aligned)
675 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
676 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
677 "Language Specific Data Area (none)");
680 else
681 dw2_asm_output_data_uleb128 (0, "Augmentation size");
684 /* Loop through the Call Frame Instructions associated with this FDE. */
685 fde->dw_fde_current_label = begin;
687 size_t from, until, i;
689 from = 0;
690 until = vec_safe_length (fde->dw_fde_cfi);
692 if (fde->dw_fde_second_begin == NULL)
694 else if (!second)
695 until = fde->dw_fde_switch_cfi_index;
696 else
697 from = fde->dw_fde_switch_cfi_index;
699 for (i = from; i < until; i++)
700 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
703 /* If we are to emit a ref/link from function bodies to their frame tables,
704 do it now. This is typically performed to make sure that tables
705 associated with functions are dragged with them and not discarded in
706 garbage collecting links. We need to do this on a per function basis to
707 cope with -ffunction-sections. */
709 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
710 /* Switch to the function section, emit the ref to the tables, and
711 switch *back* into the table section. */
712 switch_to_section (function_section (fde->decl));
713 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
714 switch_to_frame_table_section (for_eh, true);
715 #endif
717 /* Pad the FDE out to an address sized boundary. */
718 ASM_OUTPUT_ALIGN (asm_out_file,
719 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
720 ASM_OUTPUT_LABEL (asm_out_file, l2);
722 j += 2;
725 /* Return true if frame description entry FDE is needed for EH. */
727 static bool
728 fde_needed_for_eh_p (dw_fde_ref fde)
730 if (flag_asynchronous_unwind_tables)
731 return true;
733 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
734 return true;
736 if (fde->uses_eh_lsda)
737 return true;
739 /* If exceptions are enabled, we have collected nothrow info. */
740 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
741 return false;
743 return true;
746 /* Output the call frame information used to record information
747 that relates to calculating the frame pointer, and records the
748 location of saved registers. */
750 static void
751 output_call_frame_info (int for_eh)
753 unsigned int i;
754 dw_fde_ref fde;
755 dw_cfi_ref cfi;
756 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
757 char section_start_label[MAX_ARTIFICIAL_LABEL_BYTES];
758 bool any_lsda_needed = false;
759 char augmentation[6];
760 int augmentation_size;
761 int fde_encoding = DW_EH_PE_absptr;
762 int per_encoding = DW_EH_PE_absptr;
763 int lsda_encoding = DW_EH_PE_absptr;
764 int return_reg;
765 rtx personality = NULL;
766 int dw_cie_version;
768 /* Don't emit a CIE if there won't be any FDEs. */
769 if (!fde_vec)
770 return;
772 /* Nothing to do if the assembler's doing it all. */
773 if (dwarf2out_do_cfi_asm ())
774 return;
776 /* If we don't have any functions we'll want to unwind out of, don't emit
777 any EH unwind information. If we make FDEs linkonce, we may have to
778 emit an empty label for an FDE that wouldn't otherwise be emitted. We
779 want to avoid having an FDE kept around when the function it refers to
780 is discarded. Example where this matters: a primary function template
781 in C++ requires EH information, an explicit specialization doesn't. */
782 if (for_eh)
784 bool any_eh_needed = false;
786 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
788 if (fde->uses_eh_lsda)
789 any_eh_needed = any_lsda_needed = true;
790 else if (fde_needed_for_eh_p (fde))
791 any_eh_needed = true;
792 else if (TARGET_USES_WEAK_UNWIND_INFO)
793 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
796 if (!any_eh_needed)
797 return;
800 /* We're going to be generating comments, so turn on app. */
801 if (flag_debug_asm)
802 app_enable ();
804 /* Switch to the proper frame section, first time. */
805 switch_to_frame_table_section (for_eh, false);
807 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
808 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
810 /* Output the CIE. */
811 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
812 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
813 if (!XCOFF_DEBUGGING_INFO || for_eh)
815 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4 && !for_eh)
816 dw2_asm_output_data (4, 0xffffffff,
817 "Initial length escape value indicating 64-bit DWARF extension");
818 dw2_asm_output_delta (for_eh ? 4 : dwarf_offset_size, l2, l1,
819 "Length of Common Information Entry");
821 ASM_OUTPUT_LABEL (asm_out_file, l1);
823 /* Now that the CIE pointer is PC-relative for EH,
824 use 0 to identify the CIE. */
825 dw2_asm_output_data ((for_eh ? 4 : dwarf_offset_size),
826 (for_eh ? 0 : DWARF_CIE_ID),
827 "CIE Identifier Tag");
829 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
830 use CIE version 1, unless that would produce incorrect results
831 due to overflowing the return register column. */
832 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
833 dw_cie_version = 1;
834 if (return_reg >= 256 || dwarf_version > 2)
835 dw_cie_version = 3;
836 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
838 augmentation[0] = 0;
839 augmentation_size = 0;
841 personality = current_unit_personality;
842 if (for_eh)
844 char *p;
846 /* Augmentation:
847 z Indicates that a uleb128 is present to size the
848 augmentation section.
849 L Indicates the encoding (and thus presence) of
850 an LSDA pointer in the FDE augmentation.
851 R Indicates a non-default pointer encoding for
852 FDE code pointers.
853 P Indicates the presence of an encoding + language
854 personality routine in the CIE augmentation. */
856 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
857 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
858 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
860 p = augmentation + 1;
861 if (personality)
863 *p++ = 'P';
864 augmentation_size += 1 + size_of_encoded_value (per_encoding);
865 assemble_external_libcall (personality);
867 if (any_lsda_needed)
869 *p++ = 'L';
870 augmentation_size += 1;
872 if (fde_encoding != DW_EH_PE_absptr)
874 *p++ = 'R';
875 augmentation_size += 1;
877 if (p > augmentation + 1)
879 augmentation[0] = 'z';
880 *p = '\0';
883 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
884 if (personality && per_encoding == DW_EH_PE_aligned)
886 int offset = ( 4 /* Length */
887 + 4 /* CIE Id */
888 + 1 /* CIE version */
889 + strlen (augmentation) + 1 /* Augmentation */
890 + size_of_uleb128 (1) /* Code alignment */
891 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
892 + 1 /* RA column */
893 + 1 /* Augmentation size */
894 + 1 /* Personality encoding */ );
895 int pad = -offset & (PTR_SIZE - 1);
897 augmentation_size += pad;
899 /* Augmentations should be small, so there's scarce need to
900 iterate for a solution. Die if we exceed one uleb128 byte. */
901 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
905 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
906 if (dw_cie_version >= 4)
908 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
909 dw2_asm_output_data (1, 0, "CIE Segment Size");
911 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
912 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
913 "CIE Data Alignment Factor");
915 if (dw_cie_version == 1)
916 dw2_asm_output_data (1, return_reg, "CIE RA Column");
917 else
918 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
920 if (augmentation[0])
922 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
923 if (personality)
925 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
926 eh_data_format_name (per_encoding));
927 dw2_asm_output_encoded_addr_rtx (per_encoding,
928 personality,
929 true, NULL);
932 if (any_lsda_needed)
933 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
934 eh_data_format_name (lsda_encoding));
936 if (fde_encoding != DW_EH_PE_absptr)
937 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
938 eh_data_format_name (fde_encoding));
941 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
942 output_cfi (cfi, NULL, for_eh);
944 /* Pad the CIE out to an address sized boundary. */
945 ASM_OUTPUT_ALIGN (asm_out_file,
946 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
947 ASM_OUTPUT_LABEL (asm_out_file, l2);
949 /* Loop through all of the FDE's. */
950 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
952 unsigned int k;
954 /* Don't emit EH unwind info for leaf functions that don't need it. */
955 if (for_eh && !fde_needed_for_eh_p (fde))
956 continue;
958 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
959 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
960 augmentation, any_lsda_needed, lsda_encoding);
963 if (for_eh && targetm.terminate_dw2_eh_frame_info)
964 dw2_asm_output_data (4, 0, "End of Table");
966 /* Turn off app to make assembly quicker. */
967 if (flag_debug_asm)
968 app_disable ();
971 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
973 static void
974 dwarf2out_do_cfi_startproc (bool second)
976 int enc;
977 rtx ref;
979 fprintf (asm_out_file, "\t.cfi_startproc\n");
981 targetm.asm_out.post_cfi_startproc (asm_out_file, current_function_decl);
983 /* .cfi_personality and .cfi_lsda are only relevant to DWARF2
984 eh unwinders. */
985 if (targetm_common.except_unwind_info (&global_options) != UI_DWARF2)
986 return;
988 rtx personality = get_personality_function (current_function_decl);
990 if (personality)
992 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
993 ref = personality;
995 /* ??? The GAS support isn't entirely consistent. We have to
996 handle indirect support ourselves, but PC-relative is done
997 in the assembler. Further, the assembler can't handle any
998 of the weirder relocation types. */
999 if (enc & DW_EH_PE_indirect)
1001 if (targetm.asm_out.make_eh_symbol_indirect != NULL)
1002 ref = targetm.asm_out.make_eh_symbol_indirect (ref, true);
1003 else
1004 ref = dw2_force_const_mem (ref, true);
1007 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
1008 output_addr_const (asm_out_file, ref);
1009 fputc ('\n', asm_out_file);
1012 if (crtl->uses_eh_lsda)
1014 char lab[MAX_ARTIFICIAL_LABEL_BYTES];
1016 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
1017 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
1018 current_function_funcdef_no);
1019 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
1020 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
1022 if (enc & DW_EH_PE_indirect)
1024 if (targetm.asm_out.make_eh_symbol_indirect != NULL)
1025 ref = targetm.asm_out.make_eh_symbol_indirect (ref, true);
1026 else
1027 ref = dw2_force_const_mem (ref, true);
1030 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
1031 output_addr_const (asm_out_file, ref);
1032 fputc ('\n', asm_out_file);
1036 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
1037 this allocation may be done before pass_final. */
1039 dw_fde_ref
1040 dwarf2out_alloc_current_fde (void)
1042 dw_fde_ref fde;
1044 fde = ggc_cleared_alloc<dw_fde_node> ();
1045 fde->decl = current_function_decl;
1046 fde->funcdef_number = current_function_funcdef_no;
1047 fde->fde_index = vec_safe_length (fde_vec);
1048 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1049 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1050 fde->nothrow = crtl->nothrow;
1051 fde->drap_reg = INVALID_REGNUM;
1052 fde->vdrap_reg = INVALID_REGNUM;
1054 /* Record the FDE associated with this function. */
1055 cfun->fde = fde;
1056 vec_safe_push (fde_vec, fde);
1058 return fde;
1061 /* Output a marker (i.e. a label) for the beginning of a function, before
1062 the prologue. */
1064 void
1065 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1066 unsigned int column ATTRIBUTE_UNUSED,
1067 const char *file ATTRIBUTE_UNUSED)
1069 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1070 char * dup_label;
1071 dw_fde_ref fde;
1072 section *fnsec;
1073 bool do_frame;
1075 current_function_func_begin_label = NULL;
1077 do_frame = dwarf2out_do_frame ();
1079 /* ??? current_function_func_begin_label is also used by except.cc for
1080 call-site information. We must emit this label if it might be used. */
1081 if (!do_frame
1082 && (!flag_exceptions
1083 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1084 return;
1086 fnsec = function_section (current_function_decl);
1087 switch_to_section (fnsec);
1088 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1089 current_function_funcdef_no);
1090 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1091 current_function_funcdef_no);
1092 dup_label = xstrdup (label);
1093 current_function_func_begin_label = dup_label;
1095 /* We can elide FDE allocation if we're not emitting frame unwind info. */
1096 if (!do_frame)
1097 return;
1099 /* Unlike the debug version, the EH version of frame unwind info is a per-
1100 function setting so we need to record whether we need it for the unit. */
1101 do_eh_frame |= dwarf2out_do_eh_frame ();
1103 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1104 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1105 would include pass_dwarf2_frame. If we've not created the FDE yet,
1106 do so now. */
1107 fde = cfun->fde;
1108 if (fde == NULL)
1109 fde = dwarf2out_alloc_current_fde ();
1111 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1112 fde->dw_fde_begin = dup_label;
1113 fde->dw_fde_current_label = dup_label;
1114 fde->in_std_section = (fnsec == text_section
1115 || (cold_text_section && fnsec == cold_text_section));
1116 fde->ignored_debug = DECL_IGNORED_P (current_function_decl);
1117 in_text_section_p = fnsec == text_section;
1119 /* We only want to output line number information for the genuine dwarf2
1120 prologue case, not the eh frame case. */
1121 #ifdef DWARF2_DEBUGGING_INFO
1122 if (file)
1123 dwarf2out_source_line (line, column, file, 0, true);
1124 #endif
1126 if (dwarf2out_do_cfi_asm ())
1127 dwarf2out_do_cfi_startproc (false);
1128 else
1130 rtx personality = get_personality_function (current_function_decl);
1131 if (!current_unit_personality)
1132 current_unit_personality = personality;
1134 /* We cannot keep a current personality per function as without CFI
1135 asm, at the point where we emit the CFI data, there is no current
1136 function anymore. */
1137 if (personality && current_unit_personality != personality)
1138 sorry ("multiple EH personalities are supported only with assemblers "
1139 "supporting %<.cfi_personality%> directive");
1143 /* Output a marker (i.e. a label) for the end of the generated code
1144 for a function prologue. This gets called *after* the prologue code has
1145 been generated. */
1147 void
1148 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1149 const char *file ATTRIBUTE_UNUSED)
1151 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1153 /* Output a label to mark the endpoint of the code generated for this
1154 function. */
1155 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1156 current_function_funcdef_no);
1157 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1158 current_function_funcdef_no);
1159 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1162 /* Output a marker (i.e. a label) for the beginning of the generated code
1163 for a function epilogue. This gets called *before* the prologue code has
1164 been generated. */
1166 void
1167 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1168 const char *file ATTRIBUTE_UNUSED)
1170 dw_fde_ref fde = cfun->fde;
1171 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1173 if (fde->dw_fde_vms_begin_epilogue)
1174 return;
1176 /* Output a label to mark the endpoint of the code generated for this
1177 function. */
1178 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1179 current_function_funcdef_no);
1180 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1181 current_function_funcdef_no);
1182 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1185 /* Mark the ranges of non-debug subsections in the std text sections. */
1187 static void
1188 mark_ignored_debug_section (dw_fde_ref fde, bool second)
1190 bool std_section;
1191 const char *begin_label, *end_label;
1192 const char **last_end_label;
1193 vec<const char *, va_gc> **switch_ranges;
1195 if (second)
1197 std_section = fde->second_in_std_section;
1198 begin_label = fde->dw_fde_second_begin;
1199 end_label = fde->dw_fde_second_end;
1201 else
1203 std_section = fde->in_std_section;
1204 begin_label = fde->dw_fde_begin;
1205 end_label = fde->dw_fde_end;
1208 if (!std_section)
1209 return;
1211 if (in_text_section_p)
1213 last_end_label = &last_text_label;
1214 switch_ranges = &switch_text_ranges;
1216 else
1218 last_end_label = &last_cold_label;
1219 switch_ranges = &switch_cold_ranges;
1222 if (fde->ignored_debug)
1224 if (*switch_ranges && !(vec_safe_length (*switch_ranges) & 1))
1225 vec_safe_push (*switch_ranges, *last_end_label);
1227 else
1229 *last_end_label = end_label;
1231 if (!*switch_ranges)
1232 vec_alloc (*switch_ranges, 16);
1233 else if (vec_safe_length (*switch_ranges) & 1)
1234 vec_safe_push (*switch_ranges, begin_label);
1238 /* Output a marker (i.e. a label) for the absolute end of the generated code
1239 for a function definition. This gets called *after* the epilogue code has
1240 been generated. */
1242 void
1243 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1244 const char *file ATTRIBUTE_UNUSED)
1246 dw_fde_ref fde;
1247 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1249 last_var_location_insn = NULL;
1250 cached_next_real_insn = NULL;
1252 if (dwarf2out_do_cfi_asm ())
1253 fprintf (asm_out_file, "\t.cfi_endproc\n");
1255 /* Output a label to mark the endpoint of the code generated for this
1256 function. */
1257 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1258 current_function_funcdef_no);
1259 ASM_OUTPUT_LABEL (asm_out_file, label);
1260 fde = cfun->fde;
1261 gcc_assert (fde != NULL);
1262 if (fde->dw_fde_second_begin == NULL)
1263 fde->dw_fde_end = xstrdup (label);
1265 mark_ignored_debug_section (fde, fde->dw_fde_second_begin != NULL);
1268 void
1269 dwarf2out_frame_finish (void)
1271 /* Output call frame information. */
1272 if (targetm.debug_unwind_info () == UI_DWARF2)
1273 output_call_frame_info (0);
1275 /* Output another copy for the unwinder. */
1276 if (do_eh_frame)
1277 output_call_frame_info (1);
1280 static void var_location_switch_text_section (void);
1281 static void set_cur_line_info_table (section *);
1283 void
1284 dwarf2out_switch_text_section (void)
1286 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1287 section *sect;
1288 dw_fde_ref fde = cfun->fde;
1290 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1292 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_SECOND_SECT_LABEL,
1293 current_function_funcdef_no);
1295 fde->dw_fde_second_begin = ggc_strdup (label);
1296 if (!in_cold_section_p)
1298 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1299 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1301 else
1303 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1304 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1306 have_multiple_function_sections = true;
1308 if (dwarf2out_do_cfi_asm ())
1309 fprintf (asm_out_file, "\t.cfi_endproc\n");
1311 mark_ignored_debug_section (fde, false);
1313 /* Now do the real section switch. */
1314 sect = current_function_section ();
1315 switch_to_section (sect);
1317 fde->second_in_std_section
1318 = (sect == text_section
1319 || (cold_text_section && sect == cold_text_section));
1320 in_text_section_p = sect == text_section;
1322 if (dwarf2out_do_cfi_asm ())
1323 dwarf2out_do_cfi_startproc (true);
1325 var_location_switch_text_section ();
1327 if (cold_text_section != NULL)
1328 set_cur_line_info_table (sect);
1331 /* And now, the subset of the debugging information support code necessary
1332 for emitting location expressions. */
1334 /* Describe an entry into the .debug_addr section. */
1336 enum ate_kind {
1337 ate_kind_rtx,
1338 ate_kind_rtx_dtprel,
1339 ate_kind_label
1342 struct GTY((for_user)) addr_table_entry {
1343 enum ate_kind kind;
1344 unsigned int refcount;
1345 unsigned int index;
1346 union addr_table_entry_struct_union
1348 rtx GTY ((tag ("0"))) rtl;
1349 char * GTY ((tag ("1"))) label;
1351 GTY ((desc ("%1.kind"))) addr;
1354 typedef unsigned int var_loc_view;
1356 /* Location lists are ranges + location descriptions for that range,
1357 so you can track variables that are in different places over
1358 their entire life. */
1359 typedef struct GTY(()) dw_loc_list_struct {
1360 dw_loc_list_ref dw_loc_next;
1361 const char *begin; /* Label and addr_entry for start of range */
1362 addr_table_entry *begin_entry;
1363 const char *end; /* Label for end of range */
1364 addr_table_entry *end_entry;
1365 char *ll_symbol; /* Label for beginning of location list.
1366 Only on head of list. */
1367 char *vl_symbol; /* Label for beginning of view list. Ditto. */
1368 const char *section; /* Section this loclist is relative to */
1369 dw_loc_descr_ref expr;
1370 var_loc_view vbegin, vend;
1371 hashval_t hash;
1372 /* True if all addresses in this and subsequent lists are known to be
1373 resolved. */
1374 bool resolved_addr;
1375 /* True if this list has been replaced by dw_loc_next. */
1376 bool replaced;
1377 /* True if it has been emitted into .debug_loc* / .debug_loclists*
1378 section. */
1379 unsigned char emitted : 1;
1380 /* True if hash field is index rather than hash value. */
1381 unsigned char num_assigned : 1;
1382 /* True if .debug_loclists.dwo offset has been emitted for it already. */
1383 unsigned char offset_emitted : 1;
1384 /* True if note_variable_value_in_expr has been called on it. */
1385 unsigned char noted_variable_value : 1;
1386 /* True if the range should be emitted even if begin and end
1387 are the same. */
1388 bool force;
1389 } dw_loc_list_node;
1391 static dw_loc_descr_ref int_loc_descriptor (poly_int64);
1392 static dw_loc_descr_ref uint_loc_descriptor (unsigned HOST_WIDE_INT);
1394 /* Convert a DWARF stack opcode into its string name. */
1396 static const char *
1397 dwarf_stack_op_name (unsigned int op)
1399 const char *name = get_DW_OP_name (op);
1401 if (name != NULL)
1402 return name;
1404 return "OP_<unknown>";
1407 /* Return TRUE iff we're to output location view lists as a separate
1408 attribute next to the location lists, as an extension compatible
1409 with DWARF 2 and above. */
1411 static inline bool
1412 dwarf2out_locviews_in_attribute ()
1414 return debug_variable_location_views == 1;
1417 /* Return TRUE iff we're to output location view lists as part of the
1418 location lists, as proposed for standardization after DWARF 5. */
1420 static inline bool
1421 dwarf2out_locviews_in_loclist ()
1423 #ifndef DW_LLE_view_pair
1424 return false;
1425 #else
1426 return debug_variable_location_views == -1;
1427 #endif
1430 /* Return a pointer to a newly allocated location description. Location
1431 descriptions are simple expression terms that can be strung
1432 together to form more complicated location (address) descriptions. */
1434 static inline dw_loc_descr_ref
1435 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1436 unsigned HOST_WIDE_INT oprnd2)
1438 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1440 descr->dw_loc_opc = op;
1441 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1442 descr->dw_loc_oprnd1.val_entry = NULL;
1443 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1444 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1445 descr->dw_loc_oprnd2.val_entry = NULL;
1446 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1448 return descr;
1451 /* Add a location description term to a location description expression. */
1453 static inline void
1454 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1456 dw_loc_descr_ref *d;
1458 /* Find the end of the chain. */
1459 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1462 *d = descr;
1465 /* Compare two location operands for exact equality. */
1467 static bool
1468 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1470 if (a->val_class != b->val_class)
1471 return false;
1472 switch (a->val_class)
1474 case dw_val_class_none:
1475 return true;
1476 case dw_val_class_addr:
1477 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1479 case dw_val_class_offset:
1480 case dw_val_class_unsigned_const:
1481 case dw_val_class_const:
1482 case dw_val_class_unsigned_const_implicit:
1483 case dw_val_class_const_implicit:
1484 case dw_val_class_range_list:
1485 /* These are all HOST_WIDE_INT, signed or unsigned. */
1486 return a->v.val_unsigned == b->v.val_unsigned;
1488 case dw_val_class_loc:
1489 return a->v.val_loc == b->v.val_loc;
1490 case dw_val_class_loc_list:
1491 return a->v.val_loc_list == b->v.val_loc_list;
1492 case dw_val_class_view_list:
1493 return a->v.val_view_list == b->v.val_view_list;
1494 case dw_val_class_die_ref:
1495 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1496 case dw_val_class_fde_ref:
1497 return a->v.val_fde_index == b->v.val_fde_index;
1498 case dw_val_class_symview:
1499 return strcmp (a->v.val_symbolic_view, b->v.val_symbolic_view) == 0;
1500 case dw_val_class_lbl_id:
1501 case dw_val_class_lineptr:
1502 case dw_val_class_macptr:
1503 case dw_val_class_loclistsptr:
1504 case dw_val_class_high_pc:
1505 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1506 case dw_val_class_str:
1507 return a->v.val_str == b->v.val_str;
1508 case dw_val_class_flag:
1509 return a->v.val_flag == b->v.val_flag;
1510 case dw_val_class_file:
1511 case dw_val_class_file_implicit:
1512 return a->v.val_file == b->v.val_file;
1513 case dw_val_class_decl_ref:
1514 return a->v.val_decl_ref == b->v.val_decl_ref;
1516 case dw_val_class_const_double:
1517 return (a->v.val_double.high == b->v.val_double.high
1518 && a->v.val_double.low == b->v.val_double.low);
1520 case dw_val_class_wide_int:
1521 return *a->v.val_wide == *b->v.val_wide;
1523 case dw_val_class_vec:
1525 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1526 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1528 return (a_len == b_len
1529 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1532 case dw_val_class_data8:
1533 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1535 case dw_val_class_vms_delta:
1536 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1537 && !strcmp (a->v.val_vms_delta.lbl2, b->v.val_vms_delta.lbl2));
1539 case dw_val_class_discr_value:
1540 return (a->v.val_discr_value.pos == b->v.val_discr_value.pos
1541 && a->v.val_discr_value.v.uval == b->v.val_discr_value.v.uval);
1542 case dw_val_class_discr_list:
1543 /* It makes no sense comparing two discriminant value lists. */
1544 return false;
1546 gcc_unreachable ();
1549 /* Compare two location atoms for exact equality. */
1551 static bool
1552 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1554 if (a->dw_loc_opc != b->dw_loc_opc)
1555 return false;
1557 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1558 address size, but since we always allocate cleared storage it
1559 should be zero for other types of locations. */
1560 if (a->dtprel != b->dtprel)
1561 return false;
1563 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1564 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1567 /* Compare two complete location expressions for exact equality. */
1569 bool
1570 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1572 while (1)
1574 if (a == b)
1575 return true;
1576 if (a == NULL || b == NULL)
1577 return false;
1578 if (!loc_descr_equal_p_1 (a, b))
1579 return false;
1581 a = a->dw_loc_next;
1582 b = b->dw_loc_next;
1587 /* Add a constant POLY_OFFSET to a location expression. */
1589 static void
1590 loc_descr_plus_const (dw_loc_descr_ref *list_head, poly_int64 poly_offset)
1592 dw_loc_descr_ref loc;
1593 HOST_WIDE_INT *p;
1595 gcc_assert (*list_head != NULL);
1597 if (known_eq (poly_offset, 0))
1598 return;
1600 /* Find the end of the chain. */
1601 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1604 HOST_WIDE_INT offset;
1605 if (!poly_offset.is_constant (&offset))
1607 loc->dw_loc_next = int_loc_descriptor (poly_offset);
1608 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_plus, 0, 0));
1609 return;
1612 p = NULL;
1613 if (loc->dw_loc_opc == DW_OP_fbreg
1614 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1615 p = &loc->dw_loc_oprnd1.v.val_int;
1616 else if (loc->dw_loc_opc == DW_OP_bregx)
1617 p = &loc->dw_loc_oprnd2.v.val_int;
1619 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1620 offset. Don't optimize if an signed integer overflow would happen. */
1621 if (p != NULL
1622 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1623 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1624 *p += offset;
1626 else if (offset > 0)
1627 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1629 else
1631 loc->dw_loc_next
1632 = uint_loc_descriptor (-(unsigned HOST_WIDE_INT) offset);
1633 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1637 /* Return a pointer to a newly allocated location description for
1638 REG and OFFSET. */
1640 static inline dw_loc_descr_ref
1641 new_reg_loc_descr (unsigned int reg, poly_int64 offset)
1643 HOST_WIDE_INT const_offset;
1644 if (offset.is_constant (&const_offset))
1646 if (reg <= 31)
1647 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1648 const_offset, 0);
1649 else
1650 return new_loc_descr (DW_OP_bregx, reg, const_offset);
1652 else
1654 dw_loc_descr_ref ret = new_reg_loc_descr (reg, 0);
1655 loc_descr_plus_const (&ret, offset);
1656 return ret;
1660 /* Add a constant OFFSET to a location list. */
1662 static void
1663 loc_list_plus_const (dw_loc_list_ref list_head, poly_int64 offset)
1665 dw_loc_list_ref d;
1666 for (d = list_head; d != NULL; d = d->dw_loc_next)
1667 loc_descr_plus_const (&d->expr, offset);
1670 #define DWARF_REF_SIZE \
1671 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : dwarf_offset_size)
1673 /* The number of bits that can be encoded by largest DW_FORM_dataN.
1674 In DWARF4 and earlier it is DW_FORM_data8 with 64 bits, in DWARF5
1675 DW_FORM_data16 with 128 bits. */
1676 #define DWARF_LARGEST_DATA_FORM_BITS \
1677 (dwarf_version >= 5 ? 128 : 64)
1679 /* Utility inline function for construction of ops that were GNU extension
1680 before DWARF 5. */
1681 static inline enum dwarf_location_atom
1682 dwarf_OP (enum dwarf_location_atom op)
1684 switch (op)
1686 case DW_OP_implicit_pointer:
1687 if (dwarf_version < 5)
1688 return DW_OP_GNU_implicit_pointer;
1689 break;
1691 case DW_OP_entry_value:
1692 if (dwarf_version < 5)
1693 return DW_OP_GNU_entry_value;
1694 break;
1696 case DW_OP_const_type:
1697 if (dwarf_version < 5)
1698 return DW_OP_GNU_const_type;
1699 break;
1701 case DW_OP_regval_type:
1702 if (dwarf_version < 5)
1703 return DW_OP_GNU_regval_type;
1704 break;
1706 case DW_OP_deref_type:
1707 if (dwarf_version < 5)
1708 return DW_OP_GNU_deref_type;
1709 break;
1711 case DW_OP_convert:
1712 if (dwarf_version < 5)
1713 return DW_OP_GNU_convert;
1714 break;
1716 case DW_OP_reinterpret:
1717 if (dwarf_version < 5)
1718 return DW_OP_GNU_reinterpret;
1719 break;
1721 case DW_OP_addrx:
1722 if (dwarf_version < 5)
1723 return DW_OP_GNU_addr_index;
1724 break;
1726 case DW_OP_constx:
1727 if (dwarf_version < 5)
1728 return DW_OP_GNU_const_index;
1729 break;
1731 default:
1732 break;
1734 return op;
1737 /* Similarly for attributes. */
1738 static inline enum dwarf_attribute
1739 dwarf_AT (enum dwarf_attribute at)
1741 switch (at)
1743 case DW_AT_call_return_pc:
1744 if (dwarf_version < 5)
1745 return DW_AT_low_pc;
1746 break;
1748 case DW_AT_call_tail_call:
1749 if (dwarf_version < 5)
1750 return DW_AT_GNU_tail_call;
1751 break;
1753 case DW_AT_call_origin:
1754 if (dwarf_version < 5)
1755 return DW_AT_abstract_origin;
1756 break;
1758 case DW_AT_call_target:
1759 if (dwarf_version < 5)
1760 return DW_AT_GNU_call_site_target;
1761 break;
1763 case DW_AT_call_target_clobbered:
1764 if (dwarf_version < 5)
1765 return DW_AT_GNU_call_site_target_clobbered;
1766 break;
1768 case DW_AT_call_parameter:
1769 if (dwarf_version < 5)
1770 return DW_AT_abstract_origin;
1771 break;
1773 case DW_AT_call_value:
1774 if (dwarf_version < 5)
1775 return DW_AT_GNU_call_site_value;
1776 break;
1778 case DW_AT_call_data_value:
1779 if (dwarf_version < 5)
1780 return DW_AT_GNU_call_site_data_value;
1781 break;
1783 case DW_AT_call_all_calls:
1784 if (dwarf_version < 5)
1785 return DW_AT_GNU_all_call_sites;
1786 break;
1788 case DW_AT_call_all_tail_calls:
1789 if (dwarf_version < 5)
1790 return DW_AT_GNU_all_tail_call_sites;
1791 break;
1793 case DW_AT_dwo_name:
1794 if (dwarf_version < 5)
1795 return DW_AT_GNU_dwo_name;
1796 break;
1798 case DW_AT_addr_base:
1799 if (dwarf_version < 5)
1800 return DW_AT_GNU_addr_base;
1801 break;
1803 default:
1804 break;
1806 return at;
1809 /* And similarly for tags. */
1810 static inline enum dwarf_tag
1811 dwarf_TAG (enum dwarf_tag tag)
1813 switch (tag)
1815 case DW_TAG_call_site:
1816 if (dwarf_version < 5)
1817 return DW_TAG_GNU_call_site;
1818 break;
1820 case DW_TAG_call_site_parameter:
1821 if (dwarf_version < 5)
1822 return DW_TAG_GNU_call_site_parameter;
1823 break;
1825 default:
1826 break;
1828 return tag;
1831 /* And similarly for forms. */
1832 static inline enum dwarf_form
1833 dwarf_FORM (enum dwarf_form form)
1835 switch (form)
1837 case DW_FORM_addrx:
1838 if (dwarf_version < 5)
1839 return DW_FORM_GNU_addr_index;
1840 break;
1842 case DW_FORM_strx:
1843 if (dwarf_version < 5)
1844 return DW_FORM_GNU_str_index;
1845 break;
1847 default:
1848 break;
1850 return form;
1853 static unsigned long int get_base_type_offset (dw_die_ref);
1855 /* Return the size of a location descriptor. */
1857 static unsigned long
1858 size_of_loc_descr (dw_loc_descr_ref loc)
1860 unsigned long size = 1;
1862 switch (loc->dw_loc_opc)
1864 case DW_OP_addr:
1865 size += DWARF2_ADDR_SIZE;
1866 break;
1867 case DW_OP_GNU_addr_index:
1868 case DW_OP_addrx:
1869 case DW_OP_GNU_const_index:
1870 case DW_OP_constx:
1871 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1872 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1873 break;
1874 case DW_OP_const1u:
1875 case DW_OP_const1s:
1876 size += 1;
1877 break;
1878 case DW_OP_const2u:
1879 case DW_OP_const2s:
1880 size += 2;
1881 break;
1882 case DW_OP_const4u:
1883 case DW_OP_const4s:
1884 size += 4;
1885 break;
1886 case DW_OP_const8u:
1887 case DW_OP_const8s:
1888 size += 8;
1889 break;
1890 case DW_OP_constu:
1891 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1892 break;
1893 case DW_OP_consts:
1894 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1895 break;
1896 case DW_OP_pick:
1897 size += 1;
1898 break;
1899 case DW_OP_plus_uconst:
1900 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1901 break;
1902 case DW_OP_skip:
1903 case DW_OP_bra:
1904 size += 2;
1905 break;
1906 case DW_OP_breg0:
1907 case DW_OP_breg1:
1908 case DW_OP_breg2:
1909 case DW_OP_breg3:
1910 case DW_OP_breg4:
1911 case DW_OP_breg5:
1912 case DW_OP_breg6:
1913 case DW_OP_breg7:
1914 case DW_OP_breg8:
1915 case DW_OP_breg9:
1916 case DW_OP_breg10:
1917 case DW_OP_breg11:
1918 case DW_OP_breg12:
1919 case DW_OP_breg13:
1920 case DW_OP_breg14:
1921 case DW_OP_breg15:
1922 case DW_OP_breg16:
1923 case DW_OP_breg17:
1924 case DW_OP_breg18:
1925 case DW_OP_breg19:
1926 case DW_OP_breg20:
1927 case DW_OP_breg21:
1928 case DW_OP_breg22:
1929 case DW_OP_breg23:
1930 case DW_OP_breg24:
1931 case DW_OP_breg25:
1932 case DW_OP_breg26:
1933 case DW_OP_breg27:
1934 case DW_OP_breg28:
1935 case DW_OP_breg29:
1936 case DW_OP_breg30:
1937 case DW_OP_breg31:
1938 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1939 break;
1940 case DW_OP_regx:
1941 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1942 break;
1943 case DW_OP_fbreg:
1944 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1945 break;
1946 case DW_OP_bregx:
1947 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1948 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1949 break;
1950 case DW_OP_piece:
1951 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1952 break;
1953 case DW_OP_bit_piece:
1954 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1955 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1956 break;
1957 case DW_OP_deref_size:
1958 case DW_OP_xderef_size:
1959 size += 1;
1960 break;
1961 case DW_OP_call2:
1962 size += 2;
1963 break;
1964 case DW_OP_call4:
1965 size += 4;
1966 break;
1967 case DW_OP_call_ref:
1968 case DW_OP_GNU_variable_value:
1969 size += DWARF_REF_SIZE;
1970 break;
1971 case DW_OP_implicit_value:
1972 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1973 + loc->dw_loc_oprnd1.v.val_unsigned;
1974 break;
1975 case DW_OP_implicit_pointer:
1976 case DW_OP_GNU_implicit_pointer:
1977 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1978 break;
1979 case DW_OP_entry_value:
1980 case DW_OP_GNU_entry_value:
1982 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1983 size += size_of_uleb128 (op_size) + op_size;
1984 break;
1986 case DW_OP_const_type:
1987 case DW_OP_GNU_const_type:
1989 unsigned long o
1990 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1991 size += size_of_uleb128 (o) + 1;
1992 switch (loc->dw_loc_oprnd2.val_class)
1994 case dw_val_class_vec:
1995 size += loc->dw_loc_oprnd2.v.val_vec.length
1996 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1997 break;
1998 case dw_val_class_const:
1999 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
2000 break;
2001 case dw_val_class_const_double:
2002 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
2003 break;
2004 case dw_val_class_wide_int:
2005 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
2006 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
2007 break;
2008 default:
2009 gcc_unreachable ();
2011 break;
2013 case DW_OP_regval_type:
2014 case DW_OP_GNU_regval_type:
2016 unsigned long o
2017 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
2018 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
2019 + size_of_uleb128 (o);
2021 break;
2022 case DW_OP_deref_type:
2023 case DW_OP_GNU_deref_type:
2025 unsigned long o
2026 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
2027 size += 1 + size_of_uleb128 (o);
2029 break;
2030 case DW_OP_convert:
2031 case DW_OP_reinterpret:
2032 case DW_OP_GNU_convert:
2033 case DW_OP_GNU_reinterpret:
2034 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2035 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2036 else
2038 unsigned long o
2039 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
2040 size += size_of_uleb128 (o);
2042 break;
2043 case DW_OP_GNU_parameter_ref:
2044 size += 4;
2045 break;
2046 default:
2047 break;
2050 return size;
2053 /* Return the size of a series of location descriptors. */
2055 unsigned long
2056 size_of_locs (dw_loc_descr_ref loc)
2058 dw_loc_descr_ref l;
2059 unsigned long size;
2061 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
2062 field, to avoid writing to a PCH file. */
2063 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
2065 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
2066 break;
2067 size += size_of_loc_descr (l);
2069 if (! l)
2070 return size;
2072 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
2074 l->dw_loc_addr = size;
2075 size += size_of_loc_descr (l);
2078 return size;
2081 /* Return the size of the value in a DW_AT_discr_value attribute. */
2083 static int
2084 size_of_discr_value (dw_discr_value *discr_value)
2086 if (discr_value->pos)
2087 return size_of_uleb128 (discr_value->v.uval);
2088 else
2089 return size_of_sleb128 (discr_value->v.sval);
2092 /* Return the size of the value in a DW_AT_discr_list attribute. */
2094 static int
2095 size_of_discr_list (dw_discr_list_ref discr_list)
2097 int size = 0;
2099 for (dw_discr_list_ref list = discr_list;
2100 list != NULL;
2101 list = list->dw_discr_next)
2103 /* One byte for the discriminant value descriptor, and then one or two
2104 LEB128 numbers, depending on whether it's a single case label or a
2105 range label. */
2106 size += 1;
2107 size += size_of_discr_value (&list->dw_discr_lower_bound);
2108 if (list->dw_discr_range != 0)
2109 size += size_of_discr_value (&list->dw_discr_upper_bound);
2111 return size;
2114 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
2115 static void get_ref_die_offset_label (char *, dw_die_ref);
2116 static unsigned long int get_ref_die_offset (dw_die_ref);
2118 /* Output location description stack opcode's operands (if any).
2119 The for_eh_or_skip parameter controls whether register numbers are
2120 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2121 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2122 info). This should be suppressed for the cases that have not been converted
2123 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2125 static void
2126 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
2128 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2129 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2131 switch (loc->dw_loc_opc)
2133 #ifdef DWARF2_DEBUGGING_INFO
2134 case DW_OP_const2u:
2135 case DW_OP_const2s:
2136 dw2_asm_output_data (2, val1->v.val_int, NULL);
2137 break;
2138 case DW_OP_const4u:
2139 if (loc->dtprel)
2141 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2142 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
2143 val1->v.val_addr);
2144 fputc ('\n', asm_out_file);
2145 break;
2147 /* FALLTHRU */
2148 case DW_OP_const4s:
2149 dw2_asm_output_data (4, val1->v.val_int, NULL);
2150 break;
2151 case DW_OP_const8u:
2152 if (loc->dtprel)
2154 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2155 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
2156 val1->v.val_addr);
2157 fputc ('\n', asm_out_file);
2158 break;
2160 /* FALLTHRU */
2161 case DW_OP_const8s:
2162 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2163 dw2_asm_output_data (8, val1->v.val_int, NULL);
2164 break;
2165 case DW_OP_skip:
2166 case DW_OP_bra:
2168 int offset;
2170 gcc_assert (val1->val_class == dw_val_class_loc);
2171 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2173 dw2_asm_output_data (2, offset, NULL);
2175 break;
2176 case DW_OP_implicit_value:
2177 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2178 switch (val2->val_class)
2180 case dw_val_class_const:
2181 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
2182 break;
2183 case dw_val_class_vec:
2185 unsigned int elt_size = val2->v.val_vec.elt_size;
2186 unsigned int len = val2->v.val_vec.length;
2187 unsigned int i;
2188 unsigned char *p;
2190 if (elt_size > sizeof (HOST_WIDE_INT))
2192 elt_size /= 2;
2193 len *= 2;
2195 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2196 i < len;
2197 i++, p += elt_size)
2198 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2199 "fp or vector constant word %u", i);
2201 break;
2202 case dw_val_class_const_double:
2204 unsigned HOST_WIDE_INT first, second;
2206 if (WORDS_BIG_ENDIAN)
2208 first = val2->v.val_double.high;
2209 second = val2->v.val_double.low;
2211 else
2213 first = val2->v.val_double.low;
2214 second = val2->v.val_double.high;
2216 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2217 first, NULL);
2218 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2219 second, NULL);
2221 break;
2222 case dw_val_class_wide_int:
2224 int i;
2225 int len = get_full_len (*val2->v.val_wide);
2226 if (WORDS_BIG_ENDIAN)
2227 for (i = len - 1; i >= 0; --i)
2228 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2229 val2->v.val_wide->elt (i), NULL);
2230 else
2231 for (i = 0; i < len; ++i)
2232 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2233 val2->v.val_wide->elt (i), NULL);
2235 break;
2236 case dw_val_class_addr:
2237 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
2238 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
2239 break;
2240 default:
2241 gcc_unreachable ();
2243 break;
2244 #else
2245 case DW_OP_const2u:
2246 case DW_OP_const2s:
2247 case DW_OP_const4u:
2248 case DW_OP_const4s:
2249 case DW_OP_const8u:
2250 case DW_OP_const8s:
2251 case DW_OP_skip:
2252 case DW_OP_bra:
2253 case DW_OP_implicit_value:
2254 /* We currently don't make any attempt to make sure these are
2255 aligned properly like we do for the main unwind info, so
2256 don't support emitting things larger than a byte if we're
2257 only doing unwinding. */
2258 gcc_unreachable ();
2259 #endif
2260 case DW_OP_const1u:
2261 case DW_OP_const1s:
2262 dw2_asm_output_data (1, val1->v.val_int, NULL);
2263 break;
2264 case DW_OP_constu:
2265 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2266 break;
2267 case DW_OP_consts:
2268 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2269 break;
2270 case DW_OP_pick:
2271 dw2_asm_output_data (1, val1->v.val_int, NULL);
2272 break;
2273 case DW_OP_plus_uconst:
2274 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2275 break;
2276 case DW_OP_breg0:
2277 case DW_OP_breg1:
2278 case DW_OP_breg2:
2279 case DW_OP_breg3:
2280 case DW_OP_breg4:
2281 case DW_OP_breg5:
2282 case DW_OP_breg6:
2283 case DW_OP_breg7:
2284 case DW_OP_breg8:
2285 case DW_OP_breg9:
2286 case DW_OP_breg10:
2287 case DW_OP_breg11:
2288 case DW_OP_breg12:
2289 case DW_OP_breg13:
2290 case DW_OP_breg14:
2291 case DW_OP_breg15:
2292 case DW_OP_breg16:
2293 case DW_OP_breg17:
2294 case DW_OP_breg18:
2295 case DW_OP_breg19:
2296 case DW_OP_breg20:
2297 case DW_OP_breg21:
2298 case DW_OP_breg22:
2299 case DW_OP_breg23:
2300 case DW_OP_breg24:
2301 case DW_OP_breg25:
2302 case DW_OP_breg26:
2303 case DW_OP_breg27:
2304 case DW_OP_breg28:
2305 case DW_OP_breg29:
2306 case DW_OP_breg30:
2307 case DW_OP_breg31:
2308 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2309 break;
2310 case DW_OP_regx:
2312 unsigned r = val1->v.val_unsigned;
2313 if (for_eh_or_skip >= 0)
2314 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2315 gcc_assert (size_of_uleb128 (r)
2316 == size_of_uleb128 (val1->v.val_unsigned));
2317 dw2_asm_output_data_uleb128 (r, NULL);
2319 break;
2320 case DW_OP_fbreg:
2321 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2322 break;
2323 case DW_OP_bregx:
2325 unsigned r = val1->v.val_unsigned;
2326 if (for_eh_or_skip >= 0)
2327 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2328 gcc_assert (size_of_uleb128 (r)
2329 == size_of_uleb128 (val1->v.val_unsigned));
2330 dw2_asm_output_data_uleb128 (r, NULL);
2331 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2333 break;
2334 case DW_OP_piece:
2335 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2336 break;
2337 case DW_OP_bit_piece:
2338 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2339 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2340 break;
2341 case DW_OP_deref_size:
2342 case DW_OP_xderef_size:
2343 dw2_asm_output_data (1, val1->v.val_int, NULL);
2344 break;
2346 case DW_OP_addr:
2347 if (loc->dtprel)
2349 if (targetm.asm_out.output_dwarf_dtprel)
2351 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2352 DWARF2_ADDR_SIZE,
2353 val1->v.val_addr);
2354 fputc ('\n', asm_out_file);
2356 else
2357 gcc_unreachable ();
2359 else
2361 #ifdef DWARF2_DEBUGGING_INFO
2362 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2363 #else
2364 gcc_unreachable ();
2365 #endif
2367 break;
2369 case DW_OP_GNU_addr_index:
2370 case DW_OP_addrx:
2371 case DW_OP_GNU_const_index:
2372 case DW_OP_constx:
2373 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2374 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2375 "(index into .debug_addr)");
2376 break;
2378 case DW_OP_call2:
2379 case DW_OP_call4:
2381 unsigned long die_offset
2382 = get_ref_die_offset (val1->v.val_die_ref.die);
2383 /* Make sure the offset has been computed and that we can encode it as
2384 an operand. */
2385 gcc_assert (die_offset > 0
2386 && die_offset <= (loc->dw_loc_opc == DW_OP_call2
2387 ? 0xffff
2388 : 0xffffffff));
2389 dw2_asm_output_data ((loc->dw_loc_opc == DW_OP_call2) ? 2 : 4,
2390 die_offset, NULL);
2392 break;
2394 case DW_OP_call_ref:
2395 case DW_OP_GNU_variable_value:
2397 char label[MAX_ARTIFICIAL_LABEL_BYTES
2398 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2399 gcc_assert (val1->val_class == dw_val_class_die_ref);
2400 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2401 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2403 break;
2405 case DW_OP_implicit_pointer:
2406 case DW_OP_GNU_implicit_pointer:
2408 char label[MAX_ARTIFICIAL_LABEL_BYTES
2409 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2410 gcc_assert (val1->val_class == dw_val_class_die_ref);
2411 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2412 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2413 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2415 break;
2417 case DW_OP_entry_value:
2418 case DW_OP_GNU_entry_value:
2419 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2420 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2421 break;
2423 case DW_OP_const_type:
2424 case DW_OP_GNU_const_type:
2426 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2427 gcc_assert (o);
2428 dw2_asm_output_data_uleb128 (o, NULL);
2429 switch (val2->val_class)
2431 case dw_val_class_const:
2432 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2433 dw2_asm_output_data (1, l, NULL);
2434 dw2_asm_output_data (l, val2->v.val_int, NULL);
2435 break;
2436 case dw_val_class_vec:
2438 unsigned int elt_size = val2->v.val_vec.elt_size;
2439 unsigned int len = val2->v.val_vec.length;
2440 unsigned int i;
2441 unsigned char *p;
2443 l = len * elt_size;
2444 dw2_asm_output_data (1, l, NULL);
2445 if (elt_size > sizeof (HOST_WIDE_INT))
2447 elt_size /= 2;
2448 len *= 2;
2450 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2451 i < len;
2452 i++, p += elt_size)
2453 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2454 "fp or vector constant word %u", i);
2456 break;
2457 case dw_val_class_const_double:
2459 unsigned HOST_WIDE_INT first, second;
2460 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2462 dw2_asm_output_data (1, 2 * l, NULL);
2463 if (WORDS_BIG_ENDIAN)
2465 first = val2->v.val_double.high;
2466 second = val2->v.val_double.low;
2468 else
2470 first = val2->v.val_double.low;
2471 second = val2->v.val_double.high;
2473 dw2_asm_output_data (l, first, NULL);
2474 dw2_asm_output_data (l, second, NULL);
2476 break;
2477 case dw_val_class_wide_int:
2479 int i;
2480 int len = get_full_len (*val2->v.val_wide);
2481 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2483 dw2_asm_output_data (1, len * l, NULL);
2484 if (WORDS_BIG_ENDIAN)
2485 for (i = len - 1; i >= 0; --i)
2486 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2487 else
2488 for (i = 0; i < len; ++i)
2489 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2491 break;
2492 default:
2493 gcc_unreachable ();
2496 break;
2497 case DW_OP_regval_type:
2498 case DW_OP_GNU_regval_type:
2500 unsigned r = val1->v.val_unsigned;
2501 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2502 gcc_assert (o);
2503 if (for_eh_or_skip >= 0)
2505 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2506 gcc_assert (size_of_uleb128 (r)
2507 == size_of_uleb128 (val1->v.val_unsigned));
2509 dw2_asm_output_data_uleb128 (r, NULL);
2510 dw2_asm_output_data_uleb128 (o, NULL);
2512 break;
2513 case DW_OP_deref_type:
2514 case DW_OP_GNU_deref_type:
2516 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2517 gcc_assert (o);
2518 dw2_asm_output_data (1, val1->v.val_int, NULL);
2519 dw2_asm_output_data_uleb128 (o, NULL);
2521 break;
2522 case DW_OP_convert:
2523 case DW_OP_reinterpret:
2524 case DW_OP_GNU_convert:
2525 case DW_OP_GNU_reinterpret:
2526 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2527 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2528 else
2530 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2531 gcc_assert (o);
2532 dw2_asm_output_data_uleb128 (o, NULL);
2534 break;
2536 case DW_OP_GNU_parameter_ref:
2538 unsigned long o;
2539 gcc_assert (val1->val_class == dw_val_class_die_ref);
2540 o = get_ref_die_offset (val1->v.val_die_ref.die);
2541 dw2_asm_output_data (4, o, NULL);
2543 break;
2545 default:
2546 /* Other codes have no operands. */
2547 break;
2551 /* Output a sequence of location operations.
2552 The for_eh_or_skip parameter controls whether register numbers are
2553 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2554 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2555 info). This should be suppressed for the cases that have not been converted
2556 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2558 void
2559 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2561 for (; loc != NULL; loc = loc->dw_loc_next)
2563 enum dwarf_location_atom opc = loc->dw_loc_opc;
2564 /* Output the opcode. */
2565 if (for_eh_or_skip >= 0
2566 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2568 unsigned r = (opc - DW_OP_breg0);
2569 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2570 gcc_assert (r <= 31);
2571 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2573 else if (for_eh_or_skip >= 0
2574 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2576 unsigned r = (opc - DW_OP_reg0);
2577 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2578 gcc_assert (r <= 31);
2579 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2582 dw2_asm_output_data (1, opc,
2583 "%s", dwarf_stack_op_name (opc));
2585 /* Output the operand(s) (if any). */
2586 output_loc_operands (loc, for_eh_or_skip);
2590 /* Output location description stack opcode's operands (if any).
2591 The output is single bytes on a line, suitable for .cfi_escape. */
2593 static void
2594 output_loc_operands_raw (dw_loc_descr_ref loc)
2596 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2597 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2599 switch (loc->dw_loc_opc)
2601 case DW_OP_addr:
2602 case DW_OP_GNU_addr_index:
2603 case DW_OP_addrx:
2604 case DW_OP_GNU_const_index:
2605 case DW_OP_constx:
2606 case DW_OP_implicit_value:
2607 /* We cannot output addresses in .cfi_escape, only bytes. */
2608 gcc_unreachable ();
2610 case DW_OP_const1u:
2611 case DW_OP_const1s:
2612 case DW_OP_pick:
2613 case DW_OP_deref_size:
2614 case DW_OP_xderef_size:
2615 fputc (',', asm_out_file);
2616 dw2_asm_output_data_raw (1, val1->v.val_int);
2617 break;
2619 case DW_OP_const2u:
2620 case DW_OP_const2s:
2621 fputc (',', asm_out_file);
2622 dw2_asm_output_data_raw (2, val1->v.val_int);
2623 break;
2625 case DW_OP_const4u:
2626 case DW_OP_const4s:
2627 fputc (',', asm_out_file);
2628 dw2_asm_output_data_raw (4, val1->v.val_int);
2629 break;
2631 case DW_OP_const8u:
2632 case DW_OP_const8s:
2633 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2634 fputc (',', asm_out_file);
2635 dw2_asm_output_data_raw (8, val1->v.val_int);
2636 break;
2638 case DW_OP_skip:
2639 case DW_OP_bra:
2641 int offset;
2643 gcc_assert (val1->val_class == dw_val_class_loc);
2644 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2646 fputc (',', asm_out_file);
2647 dw2_asm_output_data_raw (2, offset);
2649 break;
2651 case DW_OP_regx:
2653 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2654 gcc_assert (size_of_uleb128 (r)
2655 == size_of_uleb128 (val1->v.val_unsigned));
2656 fputc (',', asm_out_file);
2657 dw2_asm_output_data_uleb128_raw (r);
2659 break;
2661 case DW_OP_constu:
2662 case DW_OP_plus_uconst:
2663 case DW_OP_piece:
2664 fputc (',', asm_out_file);
2665 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2666 break;
2668 case DW_OP_bit_piece:
2669 fputc (',', asm_out_file);
2670 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2671 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2672 break;
2674 case DW_OP_consts:
2675 case DW_OP_breg0:
2676 case DW_OP_breg1:
2677 case DW_OP_breg2:
2678 case DW_OP_breg3:
2679 case DW_OP_breg4:
2680 case DW_OP_breg5:
2681 case DW_OP_breg6:
2682 case DW_OP_breg7:
2683 case DW_OP_breg8:
2684 case DW_OP_breg9:
2685 case DW_OP_breg10:
2686 case DW_OP_breg11:
2687 case DW_OP_breg12:
2688 case DW_OP_breg13:
2689 case DW_OP_breg14:
2690 case DW_OP_breg15:
2691 case DW_OP_breg16:
2692 case DW_OP_breg17:
2693 case DW_OP_breg18:
2694 case DW_OP_breg19:
2695 case DW_OP_breg20:
2696 case DW_OP_breg21:
2697 case DW_OP_breg22:
2698 case DW_OP_breg23:
2699 case DW_OP_breg24:
2700 case DW_OP_breg25:
2701 case DW_OP_breg26:
2702 case DW_OP_breg27:
2703 case DW_OP_breg28:
2704 case DW_OP_breg29:
2705 case DW_OP_breg30:
2706 case DW_OP_breg31:
2707 case DW_OP_fbreg:
2708 fputc (',', asm_out_file);
2709 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2710 break;
2712 case DW_OP_bregx:
2714 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2715 gcc_assert (size_of_uleb128 (r)
2716 == size_of_uleb128 (val1->v.val_unsigned));
2717 fputc (',', asm_out_file);
2718 dw2_asm_output_data_uleb128_raw (r);
2719 fputc (',', asm_out_file);
2720 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2722 break;
2724 case DW_OP_implicit_pointer:
2725 case DW_OP_entry_value:
2726 case DW_OP_const_type:
2727 case DW_OP_regval_type:
2728 case DW_OP_deref_type:
2729 case DW_OP_convert:
2730 case DW_OP_reinterpret:
2731 case DW_OP_GNU_implicit_pointer:
2732 case DW_OP_GNU_entry_value:
2733 case DW_OP_GNU_const_type:
2734 case DW_OP_GNU_regval_type:
2735 case DW_OP_GNU_deref_type:
2736 case DW_OP_GNU_convert:
2737 case DW_OP_GNU_reinterpret:
2738 case DW_OP_GNU_parameter_ref:
2739 gcc_unreachable ();
2740 break;
2742 default:
2743 /* Other codes have no operands. */
2744 break;
2748 void
2749 output_loc_sequence_raw (dw_loc_descr_ref loc)
2751 while (1)
2753 enum dwarf_location_atom opc = loc->dw_loc_opc;
2754 /* Output the opcode. */
2755 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2757 unsigned r = (opc - DW_OP_breg0);
2758 r = DWARF2_FRAME_REG_OUT (r, 1);
2759 gcc_assert (r <= 31);
2760 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2762 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2764 unsigned r = (opc - DW_OP_reg0);
2765 r = DWARF2_FRAME_REG_OUT (r, 1);
2766 gcc_assert (r <= 31);
2767 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2769 /* Output the opcode. */
2770 fprintf (asm_out_file, "%#x", opc);
2771 output_loc_operands_raw (loc);
2773 if (!loc->dw_loc_next)
2774 break;
2775 loc = loc->dw_loc_next;
2777 fputc (',', asm_out_file);
2781 static void
2782 build_breg_loc (struct dw_loc_descr_node **head, unsigned int regno)
2784 if (regno <= 31)
2785 add_loc_descr (head, new_loc_descr ((enum dwarf_location_atom)
2786 (DW_OP_breg0 + regno), 0, 0));
2787 else
2788 add_loc_descr (head, new_loc_descr (DW_OP_bregx, regno, 0));
2791 /* Build a dwarf location for a cfa_reg spanning multiple
2792 consecutive registers. */
2794 struct dw_loc_descr_node *
2795 build_span_loc (struct cfa_reg reg)
2797 struct dw_loc_descr_node *head = NULL;
2799 gcc_assert (reg.span_width > 0);
2800 gcc_assert (reg.span > 1);
2802 /* Start from the highest number register as it goes in the upper bits. */
2803 unsigned int regno = reg.reg + reg.span - 1;
2804 build_breg_loc (&head, regno);
2806 /* Deal with the remaining registers in the span. */
2807 for (int i = reg.span - 2; i >= 0; i--)
2809 add_loc_descr (&head, int_loc_descriptor (reg.span_width * 8));
2810 add_loc_descr (&head, new_loc_descr (DW_OP_shl, 0, 0));
2811 regno--;
2812 build_breg_loc (&head, regno);
2813 add_loc_descr (&head, new_loc_descr (DW_OP_plus, 0, 0));
2815 return head;
2818 /* This function builds a dwarf location descriptor sequence from a
2819 dw_cfa_location, adding the given OFFSET to the result of the
2820 expression. */
2822 struct dw_loc_descr_node *
2823 build_cfa_loc (dw_cfa_location *cfa, poly_int64 offset)
2825 struct dw_loc_descr_node *head, *tmp;
2827 offset += cfa->offset;
2829 if (cfa->reg.span > 1)
2831 head = build_span_loc (cfa->reg);
2833 if (maybe_ne (offset, 0))
2834 loc_descr_plus_const (&head, offset);
2836 else if (cfa->indirect)
2838 head = new_reg_loc_descr (cfa->reg.reg, cfa->base_offset);
2839 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2840 head->dw_loc_oprnd1.val_entry = NULL;
2841 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2842 add_loc_descr (&head, tmp);
2843 loc_descr_plus_const (&head, offset);
2845 else
2846 head = new_reg_loc_descr (cfa->reg.reg, offset);
2848 return head;
2851 /* This function builds a dwarf location descriptor sequence for
2852 the address at OFFSET from the CFA when stack is aligned to
2853 ALIGNMENT byte. */
2855 struct dw_loc_descr_node *
2856 build_cfa_aligned_loc (dw_cfa_location *cfa,
2857 poly_int64 offset, HOST_WIDE_INT alignment)
2859 struct dw_loc_descr_node *head;
2860 unsigned int dwarf_fp
2861 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2863 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2864 if (cfa->reg.reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2866 head = new_reg_loc_descr (dwarf_fp, 0);
2867 add_loc_descr (&head, int_loc_descriptor (alignment));
2868 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2869 loc_descr_plus_const (&head, offset);
2871 else
2872 head = new_reg_loc_descr (dwarf_fp, offset);
2873 return head;
2876 /* And now, the support for symbolic debugging information. */
2878 /* .debug_str support. */
2880 static void dwarf2out_init (const char *);
2881 static void dwarf2out_finish (const char *);
2882 static void dwarf2out_early_finish (const char *);
2883 static void dwarf2out_assembly_start (void);
2884 static void dwarf2out_define (unsigned int, const char *);
2885 static void dwarf2out_undef (unsigned int, const char *);
2886 static void dwarf2out_start_source_file (unsigned, const char *);
2887 static void dwarf2out_end_source_file (unsigned);
2888 static void dwarf2out_function_decl (tree);
2889 static void dwarf2out_begin_block (unsigned, unsigned);
2890 static void dwarf2out_end_block (unsigned, unsigned);
2891 static bool dwarf2out_ignore_block (const_tree);
2892 static void dwarf2out_set_ignored_loc (unsigned, unsigned, const char *);
2893 static void dwarf2out_early_global_decl (tree);
2894 static void dwarf2out_late_global_decl (tree);
2895 static void dwarf2out_type_decl (tree, int);
2896 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool, bool);
2897 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2898 dw_die_ref);
2899 static void dwarf2out_abstract_function (tree);
2900 static void dwarf2out_var_location (rtx_insn *);
2901 static void dwarf2out_inline_entry (tree);
2902 static void dwarf2out_size_function (tree);
2903 static void dwarf2out_begin_function (tree);
2904 static void dwarf2out_end_function (unsigned int);
2905 static void dwarf2out_register_main_translation_unit (tree unit);
2906 static void dwarf2out_set_name (tree, tree);
2907 static void dwarf2out_register_external_die (tree decl, const char *sym,
2908 unsigned HOST_WIDE_INT off);
2909 static bool dwarf2out_die_ref_for_decl (tree decl, const char **sym,
2910 unsigned HOST_WIDE_INT *off);
2912 /* The debug hooks structure. */
2914 const struct gcc_debug_hooks dwarf2_debug_hooks =
2916 dwarf2out_init,
2917 dwarf2out_finish,
2918 dwarf2out_early_finish,
2919 dwarf2out_assembly_start,
2920 dwarf2out_define,
2921 dwarf2out_undef,
2922 dwarf2out_start_source_file,
2923 dwarf2out_end_source_file,
2924 dwarf2out_begin_block,
2925 dwarf2out_end_block,
2926 dwarf2out_ignore_block,
2927 dwarf2out_source_line,
2928 dwarf2out_set_ignored_loc,
2929 dwarf2out_begin_prologue,
2930 #if VMS_DEBUGGING_INFO
2931 dwarf2out_vms_end_prologue,
2932 dwarf2out_vms_begin_epilogue,
2933 #else
2934 debug_nothing_int_charstar,
2935 debug_nothing_int_charstar,
2936 #endif
2937 dwarf2out_end_epilogue,
2938 dwarf2out_begin_function,
2939 dwarf2out_end_function, /* end_function */
2940 dwarf2out_register_main_translation_unit,
2941 dwarf2out_function_decl, /* function_decl */
2942 dwarf2out_early_global_decl,
2943 dwarf2out_late_global_decl,
2944 dwarf2out_type_decl, /* type_decl */
2945 dwarf2out_imported_module_or_decl,
2946 dwarf2out_die_ref_for_decl,
2947 dwarf2out_register_external_die,
2948 debug_nothing_tree, /* deferred_inline_function */
2949 /* The DWARF 2 backend tries to reduce debugging bloat by not
2950 emitting the abstract description of inline functions until
2951 something tries to reference them. */
2952 dwarf2out_abstract_function, /* outlining_inline_function */
2953 debug_nothing_rtx_code_label, /* label */
2954 debug_nothing_int, /* handle_pch */
2955 dwarf2out_var_location,
2956 dwarf2out_inline_entry, /* inline_entry */
2957 dwarf2out_size_function, /* size_function */
2958 dwarf2out_switch_text_section,
2959 dwarf2out_set_name,
2960 1, /* start_end_main_source_file */
2961 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2964 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2966 dwarf2out_init,
2967 debug_nothing_charstar,
2968 debug_nothing_charstar,
2969 dwarf2out_assembly_start,
2970 debug_nothing_int_charstar,
2971 debug_nothing_int_charstar,
2972 debug_nothing_int_charstar,
2973 debug_nothing_int,
2974 debug_nothing_int_int, /* begin_block */
2975 debug_nothing_int_int, /* end_block */
2976 debug_true_const_tree, /* ignore_block */
2977 dwarf2out_source_line, /* source_line */
2978 debug_nothing_int_int_charstar, /* set_ignored_loc */
2979 debug_nothing_int_int_charstar, /* begin_prologue */
2980 debug_nothing_int_charstar, /* end_prologue */
2981 debug_nothing_int_charstar, /* begin_epilogue */
2982 debug_nothing_int_charstar, /* end_epilogue */
2983 debug_nothing_tree, /* begin_function */
2984 debug_nothing_int, /* end_function */
2985 debug_nothing_tree, /* register_main_translation_unit */
2986 debug_nothing_tree, /* function_decl */
2987 debug_nothing_tree, /* early_global_decl */
2988 debug_nothing_tree, /* late_global_decl */
2989 debug_nothing_tree_int, /* type_decl */
2990 debug_nothing_tree_tree_tree_bool_bool,/* imported_module_or_decl */
2991 debug_false_tree_charstarstar_uhwistar,/* die_ref_for_decl */
2992 debug_nothing_tree_charstar_uhwi, /* register_external_die */
2993 debug_nothing_tree, /* deferred_inline_function */
2994 debug_nothing_tree, /* outlining_inline_function */
2995 debug_nothing_rtx_code_label, /* label */
2996 debug_nothing_int, /* handle_pch */
2997 debug_nothing_rtx_insn, /* var_location */
2998 debug_nothing_tree, /* inline_entry */
2999 debug_nothing_tree, /* size_function */
3000 debug_nothing_void, /* switch_text_section */
3001 debug_nothing_tree_tree, /* set_name */
3002 0, /* start_end_main_source_file */
3003 TYPE_SYMTAB_IS_ADDRESS /* tree_type_symtab_field */
3006 /* NOTE: In the comments in this file, many references are made to
3007 "Debugging Information Entries". This term is abbreviated as `DIE'
3008 throughout the remainder of this file. */
3010 /* An internal representation of the DWARF output is built, and then
3011 walked to generate the DWARF debugging info. The walk of the internal
3012 representation is done after the entire program has been compiled.
3013 The types below are used to describe the internal representation. */
3015 /* Whether to put type DIEs into their own section .debug_types instead
3016 of making them part of the .debug_info section. Only supported for
3017 Dwarf V4 or higher and the user didn't disable them through
3018 -fno-debug-types-section. It is more efficient to put them in a
3019 separate comdat sections since the linker will then be able to
3020 remove duplicates. But not all tools support .debug_types sections
3021 yet. For Dwarf V5 or higher .debug_types doesn't exist any more,
3022 it is DW_UT_type unit type in .debug_info section. For late LTO
3023 debug there should be almost no types emitted so avoid enabling
3024 -fdebug-types-section there. */
3026 #define use_debug_types (dwarf_version >= 4 \
3027 && flag_debug_types_section \
3028 && !in_lto_p)
3030 /* Various DIE's use offsets relative to the beginning of the
3031 .debug_info section to refer to each other. */
3033 typedef long int dw_offset;
3035 struct comdat_type_node;
3037 /* The entries in the line_info table more-or-less mirror the opcodes
3038 that are used in the real dwarf line table. Arrays of these entries
3039 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
3040 supported. */
3042 enum dw_line_info_opcode {
3043 /* Emit DW_LNE_set_address; the operand is the label index. */
3044 LI_set_address,
3046 /* Emit a row to the matrix with the given line. This may be done
3047 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
3048 special opcodes. */
3049 LI_set_line,
3051 /* Emit a DW_LNS_set_file. */
3052 LI_set_file,
3054 /* Emit a DW_LNS_set_column. */
3055 LI_set_column,
3057 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
3058 LI_negate_stmt,
3060 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
3061 LI_set_prologue_end,
3062 LI_set_epilogue_begin,
3064 /* Emit a DW_LNE_set_discriminator. */
3065 LI_set_discriminator,
3067 /* Output a Fixed Advance PC; the target PC is the label index; the
3068 base PC is the previous LI_adv_address or LI_set_address entry.
3069 We only use this when emitting debug views without assembler
3070 support, at explicit user request. Ideally, we should only use
3071 it when the offset might be zero but we can't tell: it's the only
3072 way to maybe change the PC without resetting the view number. */
3073 LI_adv_address
3076 typedef struct GTY(()) dw_line_info_struct {
3077 enum dw_line_info_opcode opcode;
3078 unsigned int val;
3079 } dw_line_info_entry;
3082 struct GTY(()) dw_line_info_table {
3083 /* The label that marks the end of this section. */
3084 const char *end_label;
3086 /* The values for the last row of the matrix, as collected in the table.
3087 These are used to minimize the changes to the next row. */
3088 unsigned int file_num;
3089 unsigned int line_num;
3090 unsigned int column_num;
3091 int discrim_num;
3092 bool is_stmt;
3093 bool in_use;
3095 /* This denotes the NEXT view number.
3097 If it is 0, it is known that the NEXT view will be the first view
3098 at the given PC.
3100 If it is -1, we're forcing the view number to be reset, e.g. at a
3101 function entry.
3103 The meaning of other nonzero values depends on whether we're
3104 computing views internally or leaving it for the assembler to do
3105 so. If we're emitting them internally, view denotes the view
3106 number since the last known advance of PC. If we're leaving it
3107 for the assembler, it denotes the LVU label number that we're
3108 going to ask the assembler to assign. */
3109 var_loc_view view;
3111 /* This counts the number of symbolic views emitted in this table
3112 since the latest view reset. Its max value, over all tables,
3113 sets symview_upper_bound. */
3114 var_loc_view symviews_since_reset;
3116 #define FORCE_RESET_NEXT_VIEW(x) ((x) = (var_loc_view)-1)
3117 #define RESET_NEXT_VIEW(x) ((x) = (var_loc_view)0)
3118 #define FORCE_RESETTING_VIEW_P(x) ((x) == (var_loc_view)-1)
3119 #define RESETTING_VIEW_P(x) ((x) == (var_loc_view)0 || FORCE_RESETTING_VIEW_P (x))
3121 vec<dw_line_info_entry, va_gc> *entries;
3124 /* This is an upper bound for view numbers that the assembler may
3125 assign to symbolic views output in this translation. It is used to
3126 decide how big a field to use to represent view numbers in
3127 symview-classed attributes. */
3129 static var_loc_view symview_upper_bound;
3131 /* If we're keep track of location views and their reset points, and
3132 INSN is a reset point (i.e., it necessarily advances the PC), mark
3133 the next view in TABLE as reset. */
3135 static void
3136 maybe_reset_location_view (rtx_insn *insn, dw_line_info_table *table)
3138 if (!debug_internal_reset_location_views)
3139 return;
3141 /* Maybe turn (part of?) this test into a default target hook. */
3142 int reset = 0;
3144 if (targetm.reset_location_view)
3145 reset = targetm.reset_location_view (insn);
3147 if (reset)
3149 else if (JUMP_TABLE_DATA_P (insn))
3150 reset = 1;
3151 else if (GET_CODE (insn) == USE
3152 || GET_CODE (insn) == CLOBBER
3153 || GET_CODE (insn) == ASM_INPUT
3154 || asm_noperands (insn) >= 0)
3156 else if (get_attr_min_length (insn) > 0)
3157 reset = 1;
3159 if (reset > 0 && !RESETTING_VIEW_P (table->view))
3160 RESET_NEXT_VIEW (table->view);
3163 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
3164 The children of each node form a circular list linked by
3165 die_sib. die_child points to the node *before* the "first" child node. */
3167 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
3168 union die_symbol_or_type_node
3170 const char * GTY ((tag ("0"))) die_symbol;
3171 comdat_type_node *GTY ((tag ("1"))) die_type_node;
3173 GTY ((desc ("%0.comdat_type_p"))) die_id;
3174 vec<dw_attr_node, va_gc> *die_attr;
3175 dw_die_ref die_parent;
3176 dw_die_ref die_child;
3177 dw_die_ref die_sib;
3178 dw_die_ref die_definition; /* ref from a specification to its definition */
3179 dw_offset die_offset;
3180 unsigned long die_abbrev;
3181 int die_mark;
3182 unsigned int decl_id;
3183 enum dwarf_tag die_tag;
3184 /* Die is used and must not be pruned as unused. */
3185 BOOL_BITFIELD die_perennial_p : 1;
3186 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
3187 /* For an external ref to die_symbol if die_offset contains an extra
3188 offset to that symbol. */
3189 BOOL_BITFIELD with_offset : 1;
3190 /* Whether this DIE was removed from the DIE tree, for example via
3191 prune_unused_types. We don't consider those present from the
3192 DIE lookup routines. */
3193 BOOL_BITFIELD removed : 1;
3194 /* Lots of spare bits. */
3196 die_node;
3198 /* Set to TRUE while dwarf2out_early_global_decl is running. */
3199 static bool early_dwarf;
3200 static bool early_dwarf_finished;
3201 class set_early_dwarf {
3202 public:
3203 bool saved;
3204 set_early_dwarf () : saved(early_dwarf)
3206 gcc_assert (! early_dwarf_finished);
3207 early_dwarf = true;
3209 ~set_early_dwarf () { early_dwarf = saved; }
3212 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
3213 #define FOR_EACH_CHILD(die, c, expr) do { \
3214 c = die->die_child; \
3215 if (c) do { \
3216 c = c->die_sib; \
3217 expr; \
3218 } while (c != die->die_child); \
3219 } while (0)
3221 /* The pubname structure */
3223 typedef struct GTY(()) pubname_struct {
3224 dw_die_ref die;
3225 const char *name;
3227 pubname_entry;
3230 struct GTY(()) dw_ranges {
3231 const char *label;
3232 /* If this is positive, it's a block number, otherwise it's a
3233 bitwise-negated index into dw_ranges_by_label. */
3234 int num;
3235 /* If idx is equal to DW_RANGES_IDX_SKELETON, it should be emitted
3236 into .debug_rnglists section rather than .debug_rnglists.dwo
3237 for -gsplit-dwarf and DWARF >= 5. */
3238 #define DW_RANGES_IDX_SKELETON ((1U << 31) - 1)
3239 /* Index for the range list for DW_FORM_rnglistx. */
3240 unsigned int idx : 31;
3241 /* True if this range might be possibly in a different section
3242 from previous entry. */
3243 unsigned int maybe_new_sec : 1;
3244 addr_table_entry *begin_entry;
3245 addr_table_entry *end_entry;
3248 /* A structure to hold a macinfo entry. */
3250 typedef struct GTY(()) macinfo_struct {
3251 unsigned char code;
3252 unsigned HOST_WIDE_INT lineno;
3253 const char *info;
3255 macinfo_entry;
3258 struct GTY(()) dw_ranges_by_label {
3259 const char *begin;
3260 const char *end;
3263 /* The comdat type node structure. */
3264 struct GTY(()) comdat_type_node
3266 dw_die_ref root_die;
3267 dw_die_ref type_die;
3268 dw_die_ref skeleton_die;
3269 char signature[DWARF_TYPE_SIGNATURE_SIZE];
3270 comdat_type_node *next;
3273 /* A list of DIEs for which we can't determine ancestry (parent_die
3274 field) just yet. Later in dwarf2out_finish we will fill in the
3275 missing bits. */
3276 typedef struct GTY(()) limbo_die_struct {
3277 dw_die_ref die;
3278 /* The tree for which this DIE was created. We use this to
3279 determine ancestry later. */
3280 tree created_for;
3281 struct limbo_die_struct *next;
3283 limbo_die_node;
3285 typedef struct skeleton_chain_struct
3287 dw_die_ref old_die;
3288 dw_die_ref new_die;
3289 struct skeleton_chain_struct *parent;
3291 skeleton_chain_node;
3293 /* Define a macro which returns nonzero for a TYPE_DECL which was
3294 implicitly generated for a type.
3296 Note that, unlike the C front-end (which generates a NULL named
3297 TYPE_DECL node for each complete tagged type, each array type,
3298 and each function type node created) the C++ front-end generates
3299 a _named_ TYPE_DECL node for each tagged type node created.
3300 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3301 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
3302 front-end, but for each type, tagged or not. */
3304 #define TYPE_DECL_IS_STUB(decl) \
3305 (DECL_NAME (decl) == NULL_TREE \
3306 || (DECL_ARTIFICIAL (decl) \
3307 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3308 /* This is necessary for stub decls that \
3309 appear in nested inline functions. */ \
3310 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3311 && (decl_ultimate_origin (decl) \
3312 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3314 /* Information concerning the compilation unit's programming
3315 language, and compiler version. */
3317 /* Fixed size portion of the DWARF compilation unit header. */
3318 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3319 (DWARF_INITIAL_LENGTH_SIZE + dwarf_offset_size \
3320 + (dwarf_version >= 5 ? 4 : 3))
3322 /* Fixed size portion of the DWARF comdat type unit header. */
3323 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
3324 (DWARF_COMPILE_UNIT_HEADER_SIZE \
3325 + DWARF_TYPE_SIGNATURE_SIZE + dwarf_offset_size)
3327 /* Fixed size portion of the DWARF skeleton compilation unit header. */
3328 #define DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE \
3329 (DWARF_COMPILE_UNIT_HEADER_SIZE + (dwarf_version >= 5 ? 8 : 0))
3331 /* Fixed size portion of public names info. */
3332 #define DWARF_PUBNAMES_HEADER_SIZE (2 * dwarf_offset_size + 2)
3334 /* Fixed size portion of the address range info. */
3335 #define DWARF_ARANGES_HEADER_SIZE \
3336 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + dwarf_offset_size + 4, \
3337 DWARF2_ADDR_SIZE * 2) \
3338 - DWARF_INITIAL_LENGTH_SIZE)
3340 /* Size of padding portion in the address range info. It must be
3341 aligned to twice the pointer size. */
3342 #define DWARF_ARANGES_PAD_SIZE \
3343 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + dwarf_offset_size + 4, \
3344 DWARF2_ADDR_SIZE * 2) \
3345 - (DWARF_INITIAL_LENGTH_SIZE + dwarf_offset_size + 4))
3347 /* Use assembler line directives if available. */
3348 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3349 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3350 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3351 #else
3352 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3353 #endif
3354 #endif
3356 /* Use assembler views in line directives if available. */
3357 #ifndef DWARF2_ASM_VIEW_DEBUG_INFO
3358 #ifdef HAVE_AS_DWARF2_DEBUG_VIEW
3359 #define DWARF2_ASM_VIEW_DEBUG_INFO 1
3360 #else
3361 #define DWARF2_ASM_VIEW_DEBUG_INFO 0
3362 #endif
3363 #endif
3365 /* Return true if GCC configure detected assembler support for .loc. */
3367 bool
3368 dwarf2out_default_as_loc_support (void)
3370 return DWARF2_ASM_LINE_DEBUG_INFO;
3371 #if (GCC_VERSION >= 3000)
3372 # undef DWARF2_ASM_LINE_DEBUG_INFO
3373 # pragma GCC poison DWARF2_ASM_LINE_DEBUG_INFO
3374 #endif
3377 /* Return true if GCC configure detected assembler support for views
3378 in .loc directives. */
3380 bool
3381 dwarf2out_default_as_locview_support (void)
3383 return DWARF2_ASM_VIEW_DEBUG_INFO;
3384 #if (GCC_VERSION >= 3000)
3385 # undef DWARF2_ASM_VIEW_DEBUG_INFO
3386 # pragma GCC poison DWARF2_ASM_VIEW_DEBUG_INFO
3387 #endif
3390 /* A bit is set in ZERO_VIEW_P if we are using the assembler-supported
3391 view computation, and it refers to a view identifier for which we
3392 will not emit a label because it is known to map to a view number
3393 zero. We won't allocate the bitmap if we're not using assembler
3394 support for location views, but we have to make the variable
3395 visible for GGC and for code that will be optimized out for lack of
3396 support but that's still parsed and compiled. We could abstract it
3397 out with macros, but it's not worth it. */
3398 static GTY(()) bitmap zero_view_p;
3400 /* Evaluate to TRUE iff N is known to identify the first location view
3401 at its PC. When not using assembler location view computation,
3402 that must be view number zero. Otherwise, ZERO_VIEW_P is allocated
3403 and views label numbers recorded in it are the ones known to be
3404 zero. */
3405 #define ZERO_VIEW_P(N) ((N) == (var_loc_view)0 \
3406 || (N) == (var_loc_view)-1 \
3407 || (zero_view_p \
3408 && bitmap_bit_p (zero_view_p, (N))))
3410 /* Return true iff we're to emit .loc directives for the assembler to
3411 generate line number sections.
3413 When we're not emitting views, all we need from the assembler is
3414 support for .loc directives.
3416 If we are emitting views, we can only use the assembler's .loc
3417 support if it also supports views.
3419 When the compiler is emitting the line number programs and
3420 computing view numbers itself, it resets view numbers at known PC
3421 changes and counts from that, and then it emits view numbers as
3422 literal constants in locviewlists. There are cases in which the
3423 compiler is not sure about PC changes, e.g. when extra alignment is
3424 requested for a label. In these cases, the compiler may not reset
3425 the view counter, and the potential PC advance in the line number
3426 program will use an opcode that does not reset the view counter
3427 even if the PC actually changes, so that compiler and debug info
3428 consumer can keep view numbers in sync.
3430 When the compiler defers view computation to the assembler, it
3431 emits symbolic view numbers in locviewlists, with the exception of
3432 views known to be zero (forced resets, or reset after
3433 compiler-visible PC changes): instead of emitting symbols for
3434 these, we emit literal zero and assert the assembler agrees with
3435 the compiler's assessment. We could use symbolic views everywhere,
3436 instead of special-casing zero views, but then we'd be unable to
3437 optimize out locviewlists that contain only zeros. */
3439 static bool
3440 output_asm_line_debug_info (void)
3442 return (dwarf2out_as_loc_support
3443 && (dwarf2out_as_locview_support
3444 || !debug_variable_location_views));
3447 static bool asm_outputs_debug_line_str (void);
3449 /* Minimum line offset in a special line info. opcode.
3450 This value was chosen to give a reasonable range of values. */
3451 #define DWARF_LINE_BASE -10
3453 /* First special line opcode - leave room for the standard opcodes. */
3454 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
3456 /* Range of line offsets in a special line info. opcode. */
3457 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3459 /* Flag that indicates the initial value of the is_stmt_start flag.
3460 In the present implementation, we do not mark any lines as
3461 the beginning of a source statement, because that information
3462 is not made available by the GCC front-end. */
3463 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3465 /* Maximum number of operations per instruction bundle. */
3466 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
3467 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
3468 #endif
3470 /* This location is used by calc_die_sizes() to keep track
3471 the offset of each DIE within the .debug_info section. */
3472 static unsigned long next_die_offset;
3474 /* Record the root of the DIE's built for the current compilation unit. */
3475 static GTY(()) dw_die_ref single_comp_unit_die;
3477 /* A list of type DIEs that have been separated into comdat sections. */
3478 static GTY(()) comdat_type_node *comdat_type_list;
3480 /* A list of CU DIEs that have been separated. */
3481 static GTY(()) limbo_die_node *cu_die_list;
3483 /* A list of DIEs with a NULL parent waiting to be relocated. */
3484 static GTY(()) limbo_die_node *limbo_die_list;
3486 /* A list of DIEs for which we may have to generate
3487 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
3488 static GTY(()) limbo_die_node *deferred_asm_name;
3490 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
3492 typedef const char *compare_type;
3494 static hashval_t hash (dwarf_file_data *);
3495 static bool equal (dwarf_file_data *, const char *);
3498 /* Filenames referenced by this compilation unit. */
3499 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
3501 struct decl_die_hasher : ggc_ptr_hash<die_node>
3503 typedef tree compare_type;
3505 static hashval_t hash (die_node *);
3506 static bool equal (die_node *, tree);
3508 /* A hash table of references to DIE's that describe declarations.
3509 The key is a DECL_UID() which is a unique number identifying each decl. */
3510 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
3512 struct GTY ((for_user)) variable_value_struct {
3513 unsigned int decl_id;
3514 vec<dw_die_ref, va_gc> *dies;
3517 struct variable_value_hasher : ggc_ptr_hash<variable_value_struct>
3519 typedef tree compare_type;
3521 static hashval_t hash (variable_value_struct *);
3522 static bool equal (variable_value_struct *, tree);
3524 /* A hash table of DIEs that contain DW_OP_GNU_variable_value with
3525 dw_val_class_decl_ref class, indexed by FUNCTION_DECLs which is
3526 DECL_CONTEXT of the referenced VAR_DECLs. */
3527 static GTY (()) hash_table<variable_value_hasher> *variable_value_hash;
3529 struct block_die_hasher : ggc_ptr_hash<die_struct>
3531 static hashval_t hash (die_struct *);
3532 static bool equal (die_struct *, die_struct *);
3535 /* A hash table of references to DIE's that describe COMMON blocks.
3536 The key is DECL_UID() ^ die_parent. */
3537 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
3539 typedef struct GTY(()) die_arg_entry_struct {
3540 dw_die_ref die;
3541 tree arg;
3542 } die_arg_entry;
3545 /* Node of the variable location list. */
3546 struct GTY ((chain_next ("%h.next"))) var_loc_node {
3547 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
3548 EXPR_LIST chain. For small bitsizes, bitsize is encoded
3549 in mode of the EXPR_LIST node and first EXPR_LIST operand
3550 is either NOTE_INSN_VAR_LOCATION for a piece with a known
3551 location or NULL for padding. For larger bitsizes,
3552 mode is 0 and first operand is a CONCAT with bitsize
3553 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
3554 NULL as second operand. */
3555 rtx GTY (()) loc;
3556 const char * GTY (()) label;
3557 struct var_loc_node * GTY (()) next;
3558 var_loc_view view;
3561 /* Variable location list. */
3562 struct GTY ((for_user)) var_loc_list_def {
3563 struct var_loc_node * GTY (()) first;
3565 /* Pointer to the last but one or last element of the
3566 chained list. If the list is empty, both first and
3567 last are NULL, if the list contains just one node
3568 or the last node certainly is not redundant, it points
3569 to the last node, otherwise points to the last but one.
3570 Do not mark it for GC because it is marked through the chain. */
3571 struct var_loc_node * GTY ((skip ("%h"))) last;
3573 /* Pointer to the last element before section switch,
3574 if NULL, either sections weren't switched or first
3575 is after section switch. */
3576 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
3578 /* DECL_UID of the variable decl. */
3579 unsigned int decl_id;
3581 typedef struct var_loc_list_def var_loc_list;
3583 /* Call argument location list. */
3584 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
3585 rtx GTY (()) call_arg_loc_note;
3586 const char * GTY (()) label;
3587 tree GTY (()) block;
3588 bool tail_call_p;
3589 rtx GTY (()) symbol_ref;
3590 struct call_arg_loc_node * GTY (()) next;
3594 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
3596 typedef const_tree compare_type;
3598 static hashval_t hash (var_loc_list *);
3599 static bool equal (var_loc_list *, const_tree);
3602 /* Table of decl location linked lists. */
3603 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
3605 /* Head and tail of call_arg_loc chain. */
3606 static GTY (()) struct call_arg_loc_node *call_arg_locations;
3607 static struct call_arg_loc_node *call_arg_loc_last;
3609 /* Number of call sites in the current function. */
3610 static int call_site_count = -1;
3611 /* Number of tail call sites in the current function. */
3612 static int tail_call_site_count = -1;
3614 /* A cached location list. */
3615 struct GTY ((for_user)) cached_dw_loc_list_def {
3616 /* The DECL_UID of the decl that this entry describes. */
3617 unsigned int decl_id;
3619 /* The cached location list. */
3620 dw_loc_list_ref loc_list;
3622 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3624 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
3627 typedef const_tree compare_type;
3629 static hashval_t hash (cached_dw_loc_list *);
3630 static bool equal (cached_dw_loc_list *, const_tree);
3633 /* Table of cached location lists. */
3634 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
3636 /* A vector of references to DIE's that are uniquely identified by their tag,
3637 presence/absence of children DIE's, and list of attribute/value pairs. */
3638 static GTY(()) vec<dw_die_ref, va_gc> *abbrev_die_table;
3640 /* A hash map to remember the stack usage for DWARF procedures. The value
3641 stored is the stack size difference between before the DWARF procedure
3642 invokation and after it returned. In other words, for a DWARF procedure
3643 that consumes N stack slots and that pushes M ones, this stores M - N. */
3644 static hash_map<dw_die_ref, int> *dwarf_proc_stack_usage_map;
3646 /* A global counter for generating labels for line number data. */
3647 static unsigned int line_info_label_num;
3649 /* The current table to which we should emit line number information
3650 for the current function. This will be set up at the beginning of
3651 assembly for the function. */
3652 static GTY(()) dw_line_info_table *cur_line_info_table;
3654 /* The two default tables of line number info. */
3655 static GTY(()) dw_line_info_table *text_section_line_info;
3656 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3658 /* The set of all non-default tables of line number info. */
3659 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
3661 /* A flag to tell pubnames/types export if there is an info section to
3662 refer to. */
3663 static bool info_section_emitted;
3665 /* A pointer to the base of a table that contains a list of publicly
3666 accessible names. */
3667 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3669 /* A pointer to the base of a table that contains a list of publicly
3670 accessible types. */
3671 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3673 /* A pointer to the base of a table that contains a list of macro
3674 defines/undefines (and file start/end markers). */
3675 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3677 /* True if .debug_macinfo or .debug_macros section is going to be
3678 emitted. */
3679 #define have_macinfo \
3680 ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3681 && debug_info_level >= DINFO_LEVEL_VERBOSE \
3682 && !macinfo_table->is_empty ())
3684 /* Vector of dies for which we should generate .debug_ranges info. */
3685 static GTY (()) vec<dw_ranges, va_gc> *ranges_table;
3687 /* Vector of pairs of labels referenced in ranges_table. */
3688 static GTY (()) vec<dw_ranges_by_label, va_gc> *ranges_by_label;
3690 /* Whether we have location lists that need outputting */
3691 static GTY(()) bool have_location_lists;
3693 /* Unique label counter. */
3694 static GTY(()) unsigned int loclabel_num;
3696 /* Unique label counter for point-of-call tables. */
3697 static GTY(()) unsigned int poc_label_num;
3699 /* The last file entry emitted by maybe_emit_file(). */
3700 static GTY(()) struct dwarf_file_data * last_emitted_file;
3702 /* Number of internal labels generated by gen_internal_sym(). */
3703 static GTY(()) int label_num;
3705 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3707 /* Instances of generic types for which we need to generate debug
3708 info that describe their generic parameters and arguments. That
3709 generation needs to happen once all types are properly laid out so
3710 we do it at the end of compilation. */
3711 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3713 /* Offset from the "steady-state frame pointer" to the frame base,
3714 within the current function. */
3715 static poly_int64 frame_pointer_fb_offset;
3716 static bool frame_pointer_fb_offset_valid;
3718 static vec<dw_die_ref> base_types;
3720 /* Flags to represent a set of attribute classes for attributes that represent
3721 a scalar value (bounds, pointers, ...). */
3722 enum dw_scalar_form
3724 dw_scalar_form_constant = 0x01,
3725 dw_scalar_form_exprloc = 0x02,
3726 dw_scalar_form_reference = 0x04
3729 /* Forward declarations for functions defined in this file. */
3731 static bool is_pseudo_reg (const_rtx);
3732 static tree type_main_variant (tree);
3733 static bool is_tagged_type (const_tree);
3734 static const char *dwarf_tag_name (unsigned);
3735 static const char *dwarf_attr_name (unsigned);
3736 static const char *dwarf_form_name (unsigned);
3737 static tree decl_ultimate_origin (const_tree);
3738 static tree decl_class_context (tree);
3739 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3740 static inline unsigned int AT_index (dw_attr_node *);
3741 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3742 static inline unsigned AT_flag (dw_attr_node *);
3743 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3744 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3745 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3746 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3747 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3748 unsigned int, unsigned char *);
3749 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3750 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3751 static inline const char *AT_string (dw_attr_node *);
3752 static enum dwarf_form AT_string_form (dw_attr_node *);
3753 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3754 static void add_AT_specification (dw_die_ref, dw_die_ref);
3755 static inline dw_die_ref AT_ref (dw_attr_node *);
3756 static inline int AT_ref_external (dw_attr_node *);
3757 static inline void set_AT_ref_external (dw_attr_node *, int);
3758 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3759 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3760 dw_loc_list_ref);
3761 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3762 static void add_AT_view_list (dw_die_ref, enum dwarf_attribute);
3763 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3764 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3765 static void remove_addr_table_entry (addr_table_entry *);
3766 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3767 static inline rtx AT_addr (dw_attr_node *);
3768 static void add_AT_symview (dw_die_ref, enum dwarf_attribute, const char *);
3769 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3770 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3771 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3772 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3773 unsigned long, bool);
3774 static inline const char *AT_lbl (dw_attr_node *);
3775 static const char *get_AT_low_pc (dw_die_ref);
3776 static bool is_c (void);
3777 static bool is_cxx (void);
3778 static bool is_cxx (const_tree);
3779 static bool is_fortran (void);
3780 static bool is_ada (void);
3781 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3782 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3783 static void add_child_die (dw_die_ref, dw_die_ref);
3784 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3785 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3786 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3787 static void equate_type_number_to_die (tree, dw_die_ref);
3788 static var_loc_list *lookup_decl_loc (const_tree);
3789 static void equate_decl_number_to_die (tree, dw_die_ref);
3790 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *, var_loc_view);
3791 static void print_spaces (FILE *);
3792 static void print_die (dw_die_ref, FILE *);
3793 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3794 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3795 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3796 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3797 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3798 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3799 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3800 struct md5_ctx *, int *);
3801 struct checksum_attributes;
3802 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3803 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3804 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3805 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3806 static bool same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3807 static bool same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3808 static bool same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3809 static bool same_die_p (dw_die_ref, dw_die_ref, int *);
3810 static bool is_type_die (dw_die_ref);
3811 static inline bool is_template_instantiation (dw_die_ref);
3812 static bool is_declaration_die (dw_die_ref);
3813 static bool should_move_die_to_comdat (dw_die_ref);
3814 static dw_die_ref clone_as_declaration (dw_die_ref);
3815 static dw_die_ref clone_die (dw_die_ref);
3816 static dw_die_ref clone_tree (dw_die_ref);
3817 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3818 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3819 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3820 static dw_die_ref generate_skeleton (dw_die_ref);
3821 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3822 dw_die_ref,
3823 dw_die_ref);
3824 static void break_out_comdat_types (dw_die_ref);
3825 static void copy_decls_for_unworthy_types (dw_die_ref);
3827 static void add_sibling_attributes (dw_die_ref);
3828 static void output_location_lists (dw_die_ref);
3829 static int constant_size (unsigned HOST_WIDE_INT);
3830 static unsigned long size_of_die (dw_die_ref);
3831 static void calc_die_sizes (dw_die_ref);
3832 static void calc_base_type_die_sizes (void);
3833 static void mark_dies (dw_die_ref);
3834 static void unmark_dies (dw_die_ref);
3835 static void unmark_all_dies (dw_die_ref);
3836 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3837 static unsigned long size_of_aranges (void);
3838 static enum dwarf_form value_format (dw_attr_node *);
3839 static void output_value_format (dw_attr_node *);
3840 static void output_abbrev_section (void);
3841 static void output_die_abbrevs (unsigned long, dw_die_ref);
3842 static void output_die (dw_die_ref);
3843 static void output_compilation_unit_header (enum dwarf_unit_type);
3844 static void output_comp_unit (dw_die_ref, int, const unsigned char *);
3845 static void output_comdat_type_unit (comdat_type_node *, bool);
3846 static const char *dwarf2_name (tree, int);
3847 static void add_pubname (tree, dw_die_ref);
3848 static void add_enumerator_pubname (const char *, dw_die_ref);
3849 static void add_pubname_string (const char *, dw_die_ref);
3850 static void add_pubtype (tree, dw_die_ref);
3851 static void output_pubnames (vec<pubname_entry, va_gc> *);
3852 static void output_aranges (void);
3853 static unsigned int add_ranges (const_tree, bool = false);
3854 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3855 bool *, bool);
3856 static void output_ranges (void);
3857 static dw_line_info_table *new_line_info_table (void);
3858 static void output_line_info (bool);
3859 static void output_file_names (void);
3860 static bool is_base_type (tree);
3861 static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3862 static int decl_quals (const_tree);
3863 static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3864 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3865 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3866 static unsigned int debugger_reg_number (const_rtx);
3867 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3868 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3869 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3870 enum var_init_status);
3871 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3872 enum var_init_status);
3873 static dw_loc_descr_ref based_loc_descr (rtx, poly_int64,
3874 enum var_init_status);
3875 static bool is_based_loc (const_rtx);
3876 static bool resolve_one_addr (rtx *);
3877 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3878 enum var_init_status);
3879 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3880 enum var_init_status);
3881 struct loc_descr_context;
3882 static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3883 static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3884 static dw_loc_list_ref loc_list_from_tree (tree, int,
3885 struct loc_descr_context *);
3886 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3887 struct loc_descr_context *);
3888 static tree field_type (const_tree);
3889 static unsigned int simple_type_align_in_bits (const_tree);
3890 static unsigned int simple_decl_align_in_bits (const_tree);
3891 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3892 struct vlr_context;
3893 static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3894 HOST_WIDE_INT *);
3895 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3896 dw_loc_list_ref);
3897 static void add_data_member_location_attribute (dw_die_ref, tree,
3898 struct vlr_context *);
3899 static bool add_const_value_attribute (dw_die_ref, machine_mode, rtx);
3900 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3901 static void insert_wide_int (const wide_int_ref &, unsigned char *, int);
3902 static unsigned insert_float (const_rtx, unsigned char *);
3903 static rtx rtl_for_decl_location (tree);
3904 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3905 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3906 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3907 static void add_desc_attribute (dw_die_ref, tree);
3908 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3909 static void add_comp_dir_attribute (dw_die_ref);
3910 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3911 struct loc_descr_context *);
3912 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3913 struct loc_descr_context *);
3914 static void add_subscript_info (dw_die_ref, tree, bool);
3915 static void add_byte_size_attribute (dw_die_ref, tree);
3916 static void add_alignment_attribute (dw_die_ref, tree);
3917 static void add_bit_offset_attribute (dw_die_ref, tree);
3918 static void add_bit_size_attribute (dw_die_ref, tree);
3919 static void add_prototyped_attribute (dw_die_ref, tree);
3920 static void add_abstract_origin_attribute (dw_die_ref, tree);
3921 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3922 static void add_src_coords_attributes (dw_die_ref, tree);
3923 static void add_name_and_src_coords_attributes (dw_die_ref, tree, bool = false);
3924 static void add_discr_value (dw_die_ref, dw_discr_value *);
3925 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3926 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3927 static dw_die_ref scope_die_for (tree, dw_die_ref);
3928 static inline bool local_scope_p (dw_die_ref);
3929 static inline bool class_scope_p (dw_die_ref);
3930 static inline bool class_or_namespace_scope_p (dw_die_ref);
3931 static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3932 static void add_calling_convention_attribute (dw_die_ref, tree);
3933 static const char *type_tag (const_tree);
3934 static tree member_declared_type (const_tree);
3935 #if 0
3936 static const char *decl_start_label (tree);
3937 #endif
3938 static void gen_array_type_die (tree, dw_die_ref);
3939 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3940 #if 0
3941 static void gen_entry_point_die (tree, dw_die_ref);
3942 #endif
3943 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref, bool);
3944 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3945 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3946 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3947 static void gen_formal_types_die (tree, dw_die_ref);
3948 static void gen_subprogram_die (tree, dw_die_ref);
3949 static void gen_variable_die (tree, tree, dw_die_ref);
3950 static void gen_const_die (tree, dw_die_ref);
3951 static void gen_label_die (tree, dw_die_ref);
3952 static void gen_lexical_block_die (tree, dw_die_ref);
3953 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3954 static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3955 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3956 static dw_die_ref gen_compile_unit_die (const char *);
3957 static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3958 static void gen_member_die (tree, dw_die_ref);
3959 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3960 enum debug_info_usage);
3961 static void gen_subroutine_type_die (tree, dw_die_ref);
3962 static void gen_typedef_die (tree, dw_die_ref);
3963 static void gen_type_die (tree, dw_die_ref, bool = false);
3964 static void gen_block_die (tree, dw_die_ref);
3965 static void decls_for_scope (tree, dw_die_ref, bool = true);
3966 static bool is_naming_typedef_decl (const_tree);
3967 static inline dw_die_ref get_context_die (tree);
3968 static void gen_namespace_die (tree, dw_die_ref);
3969 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3970 static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3971 static dw_die_ref force_decl_die (tree);
3972 static dw_die_ref force_type_die (tree);
3973 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3974 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3975 static struct dwarf_file_data * lookup_filename (const char *);
3976 static void retry_incomplete_types (void);
3977 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3978 static void gen_generic_params_dies (tree);
3979 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage,
3980 bool = false);
3981 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage,
3982 bool = false);
3983 static void splice_child_die (dw_die_ref, dw_die_ref);
3984 static int file_info_cmp (const void *, const void *);
3985 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *, var_loc_view,
3986 const char *, var_loc_view, const char *);
3987 static void output_loc_list (dw_loc_list_ref);
3988 static char *gen_internal_sym (const char *);
3989 static bool want_pubnames (void);
3991 static void prune_unmark_dies (dw_die_ref);
3992 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3993 static void prune_unused_types_mark (dw_die_ref, int);
3994 static void prune_unused_types_walk (dw_die_ref);
3995 static void prune_unused_types_walk_attribs (dw_die_ref);
3996 static void prune_unused_types_prune (dw_die_ref);
3997 static void prune_unused_types (void);
3998 static int maybe_emit_file (struct dwarf_file_data *fd);
3999 static inline const char *AT_vms_delta1 (dw_attr_node *);
4000 static inline const char *AT_vms_delta2 (dw_attr_node *);
4001 #if VMS_DEBUGGING_INFO
4002 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
4003 const char *, const char *);
4004 #endif
4005 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
4006 static void gen_remaining_tmpl_value_param_die_attribute (void);
4007 static bool generic_type_p (tree);
4008 static void schedule_generic_params_dies_gen (tree t);
4009 static void gen_scheduled_generic_parms_dies (void);
4010 static void resolve_variable_values (void);
4012 static const char *comp_dir_string (void);
4014 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
4016 /* enum for tracking thread-local variables whose address is really an offset
4017 relative to the TLS pointer, which will need link-time relocation, but will
4018 not need relocation by the DWARF consumer. */
4020 enum dtprel_bool
4022 dtprel_false = 0,
4023 dtprel_true = 1
4026 /* Return the operator to use for an address of a variable. For dtprel_true, we
4027 use DW_OP_const*. For regular variables, which need both link-time
4028 relocation and consumer-level relocation (e.g., to account for shared objects
4029 loaded at a random address), we use DW_OP_addr*. */
4031 static inline enum dwarf_location_atom
4032 dw_addr_op (enum dtprel_bool dtprel)
4034 if (dtprel == dtprel_true)
4035 return (dwarf_split_debug_info ? dwarf_OP (DW_OP_constx)
4036 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
4037 else
4038 return dwarf_split_debug_info ? dwarf_OP (DW_OP_addrx) : DW_OP_addr;
4041 /* Return a pointer to a newly allocated address location description. If
4042 dwarf_split_debug_info is true, then record the address with the appropriate
4043 relocation. */
4044 static inline dw_loc_descr_ref
4045 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
4047 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
4049 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
4050 ref->dw_loc_oprnd1.v.val_addr = addr;
4051 ref->dtprel = dtprel;
4052 if (dwarf_split_debug_info)
4053 ref->dw_loc_oprnd1.val_entry
4054 = add_addr_table_entry (addr,
4055 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
4056 else
4057 ref->dw_loc_oprnd1.val_entry = NULL;
4059 return ref;
4062 /* Section names used to hold DWARF debugging information. */
4064 #ifndef DEBUG_INFO_SECTION
4065 #define DEBUG_INFO_SECTION ".debug_info"
4066 #endif
4067 #ifndef DEBUG_DWO_INFO_SECTION
4068 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
4069 #endif
4070 #ifndef DEBUG_LTO_INFO_SECTION
4071 #define DEBUG_LTO_INFO_SECTION ".gnu.debuglto_.debug_info"
4072 #endif
4073 #ifndef DEBUG_LTO_DWO_INFO_SECTION
4074 #define DEBUG_LTO_DWO_INFO_SECTION ".gnu.debuglto_.debug_info.dwo"
4075 #endif
4076 #ifndef DEBUG_ABBREV_SECTION
4077 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
4078 #endif
4079 #ifndef DEBUG_LTO_ABBREV_SECTION
4080 #define DEBUG_LTO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev"
4081 #endif
4082 #ifndef DEBUG_DWO_ABBREV_SECTION
4083 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
4084 #endif
4085 #ifndef DEBUG_LTO_DWO_ABBREV_SECTION
4086 #define DEBUG_LTO_DWO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev.dwo"
4087 #endif
4088 #ifndef DEBUG_ARANGES_SECTION
4089 #define DEBUG_ARANGES_SECTION ".debug_aranges"
4090 #endif
4091 #ifndef DEBUG_ADDR_SECTION
4092 #define DEBUG_ADDR_SECTION ".debug_addr"
4093 #endif
4094 #ifndef DEBUG_MACINFO_SECTION
4095 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
4096 #endif
4097 #ifndef DEBUG_LTO_MACINFO_SECTION
4098 #define DEBUG_LTO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo"
4099 #endif
4100 #ifndef DEBUG_DWO_MACINFO_SECTION
4101 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
4102 #endif
4103 #ifndef DEBUG_LTO_DWO_MACINFO_SECTION
4104 #define DEBUG_LTO_DWO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo.dwo"
4105 #endif
4106 #ifndef DEBUG_MACRO_SECTION
4107 #define DEBUG_MACRO_SECTION ".debug_macro"
4108 #endif
4109 #ifndef DEBUG_LTO_MACRO_SECTION
4110 #define DEBUG_LTO_MACRO_SECTION ".gnu.debuglto_.debug_macro"
4111 #endif
4112 #ifndef DEBUG_DWO_MACRO_SECTION
4113 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
4114 #endif
4115 #ifndef DEBUG_LTO_DWO_MACRO_SECTION
4116 #define DEBUG_LTO_DWO_MACRO_SECTION ".gnu.debuglto_.debug_macro.dwo"
4117 #endif
4118 #ifndef DEBUG_LINE_SECTION
4119 #define DEBUG_LINE_SECTION ".debug_line"
4120 #endif
4121 #ifndef DEBUG_LTO_LINE_SECTION
4122 #define DEBUG_LTO_LINE_SECTION ".gnu.debuglto_.debug_line"
4123 #endif
4124 #ifndef DEBUG_DWO_LINE_SECTION
4125 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
4126 #endif
4127 #ifndef DEBUG_LTO_DWO_LINE_SECTION
4128 #define DEBUG_LTO_DWO_LINE_SECTION ".gnu.debuglto_.debug_line.dwo"
4129 #endif
4130 #ifndef DEBUG_LOC_SECTION
4131 #define DEBUG_LOC_SECTION ".debug_loc"
4132 #endif
4133 #ifndef DEBUG_DWO_LOC_SECTION
4134 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
4135 #endif
4136 #ifndef DEBUG_LOCLISTS_SECTION
4137 #define DEBUG_LOCLISTS_SECTION ".debug_loclists"
4138 #endif
4139 #ifndef DEBUG_DWO_LOCLISTS_SECTION
4140 #define DEBUG_DWO_LOCLISTS_SECTION ".debug_loclists.dwo"
4141 #endif
4142 #ifndef DEBUG_PUBNAMES_SECTION
4143 #define DEBUG_PUBNAMES_SECTION \
4144 ((debug_generate_pub_sections == 2) \
4145 ? ".debug_gnu_pubnames" : ".debug_pubnames")
4146 #endif
4147 #ifndef DEBUG_PUBTYPES_SECTION
4148 #define DEBUG_PUBTYPES_SECTION \
4149 ((debug_generate_pub_sections == 2) \
4150 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
4151 #endif
4152 #ifndef DEBUG_STR_OFFSETS_SECTION
4153 #define DEBUG_STR_OFFSETS_SECTION ".debug_str_offsets"
4154 #endif
4155 #ifndef DEBUG_DWO_STR_OFFSETS_SECTION
4156 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
4157 #endif
4158 #ifndef DEBUG_LTO_DWO_STR_OFFSETS_SECTION
4159 #define DEBUG_LTO_DWO_STR_OFFSETS_SECTION ".gnu.debuglto_.debug_str_offsets.dwo"
4160 #endif
4161 #ifndef DEBUG_STR_SECTION
4162 #define DEBUG_STR_SECTION ".debug_str"
4163 #endif
4164 #ifndef DEBUG_LTO_STR_SECTION
4165 #define DEBUG_LTO_STR_SECTION ".gnu.debuglto_.debug_str"
4166 #endif
4167 #ifndef DEBUG_STR_DWO_SECTION
4168 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
4169 #endif
4170 #ifndef DEBUG_LTO_STR_DWO_SECTION
4171 #define DEBUG_LTO_STR_DWO_SECTION ".gnu.debuglto_.debug_str.dwo"
4172 #endif
4173 #ifndef DEBUG_RANGES_SECTION
4174 #define DEBUG_RANGES_SECTION ".debug_ranges"
4175 #endif
4176 #ifndef DEBUG_RNGLISTS_SECTION
4177 #define DEBUG_RNGLISTS_SECTION ".debug_rnglists"
4178 #endif
4179 #ifndef DEBUG_DWO_RNGLISTS_SECTION
4180 #define DEBUG_DWO_RNGLISTS_SECTION ".debug_rnglists.dwo"
4181 #endif
4182 #ifndef DEBUG_LINE_STR_SECTION
4183 #define DEBUG_LINE_STR_SECTION ".debug_line_str"
4184 #endif
4185 #ifndef DEBUG_LTO_LINE_STR_SECTION
4186 #define DEBUG_LTO_LINE_STR_SECTION ".gnu.debuglto_.debug_line_str"
4187 #endif
4189 /* Section flags for .debug_str section. */
4190 #define DEBUG_STR_SECTION_FLAGS \
4191 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
4192 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
4193 : SECTION_DEBUG)
4195 /* Section flags for .debug_str.dwo section. */
4196 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
4198 /* Attribute used to refer to the macro section. */
4199 #define DEBUG_MACRO_ATTRIBUTE (dwarf_version >= 5 ? DW_AT_macros \
4200 : dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros)
4202 /* Labels we insert at beginning sections we can reference instead of
4203 the section names themselves. */
4205 #ifndef TEXT_SECTION_LABEL
4206 #define TEXT_SECTION_LABEL "Ltext"
4207 #endif
4208 #ifndef COLD_TEXT_SECTION_LABEL
4209 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
4210 #endif
4211 #ifndef DEBUG_LINE_SECTION_LABEL
4212 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
4213 #endif
4214 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
4215 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
4216 #endif
4217 #ifndef DEBUG_INFO_SECTION_LABEL
4218 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
4219 #endif
4220 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
4221 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
4222 #endif
4223 #ifndef DEBUG_ABBREV_SECTION_LABEL
4224 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
4225 #endif
4226 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
4227 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
4228 #endif
4229 #ifndef DEBUG_ADDR_SECTION_LABEL
4230 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
4231 #endif
4232 #ifndef DEBUG_LOC_SECTION_LABEL
4233 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
4234 #endif
4235 #ifndef DEBUG_RANGES_SECTION_LABEL
4236 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
4237 #endif
4238 #ifndef DEBUG_MACINFO_SECTION_LABEL
4239 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
4240 #endif
4241 #ifndef DEBUG_MACRO_SECTION_LABEL
4242 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
4243 #endif
4244 #define SKELETON_COMP_DIE_ABBREV 1
4245 #define SKELETON_TYPE_DIE_ABBREV 2
4247 /* Definitions of defaults for formats and names of various special
4248 (artificial) labels which may be generated within this file (when the -g
4249 options is used and DWARF2_DEBUGGING_INFO is in effect.
4250 If necessary, these may be overridden from within the tm.h file, but
4251 typically, overriding these defaults is unnecessary. */
4253 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4254 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4255 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4256 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4257 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4258 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4259 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4260 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4261 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4262 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4263 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4264 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4265 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4266 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4267 static char ranges_base_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4269 #ifndef TEXT_END_LABEL
4270 #define TEXT_END_LABEL "Letext"
4271 #endif
4272 #ifndef COLD_END_LABEL
4273 #define COLD_END_LABEL "Letext_cold"
4274 #endif
4275 #ifndef BLOCK_BEGIN_LABEL
4276 #define BLOCK_BEGIN_LABEL "LBB"
4277 #endif
4278 #ifndef BLOCK_INLINE_ENTRY_LABEL
4279 #define BLOCK_INLINE_ENTRY_LABEL "LBI"
4280 #endif
4281 #ifndef BLOCK_END_LABEL
4282 #define BLOCK_END_LABEL "LBE"
4283 #endif
4284 #ifndef LINE_CODE_LABEL
4285 #define LINE_CODE_LABEL "LM"
4286 #endif
4289 /* Return the root of the DIE's built for the current compilation unit. */
4290 static dw_die_ref
4291 comp_unit_die (void)
4293 if (!single_comp_unit_die)
4294 single_comp_unit_die = gen_compile_unit_die (NULL);
4295 return single_comp_unit_die;
4298 /* We allow a language front-end to designate a function that is to be
4299 called to "demangle" any name before it is put into a DIE. */
4301 static const char *(*demangle_name_func) (const char *);
4303 void
4304 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4306 demangle_name_func = func;
4309 /* Test if rtl node points to a pseudo register. */
4311 static inline bool
4312 is_pseudo_reg (const_rtx rtl)
4314 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4315 || (GET_CODE (rtl) == SUBREG
4316 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4319 /* Return a reference to a type, with its const and volatile qualifiers
4320 removed. */
4322 static inline tree
4323 type_main_variant (tree type)
4325 type = TYPE_MAIN_VARIANT (type);
4327 /* ??? There really should be only one main variant among any group of
4328 variants of a given type (and all of the MAIN_VARIANT values for all
4329 members of the group should point to that one type) but sometimes the C
4330 front-end messes this up for array types, so we work around that bug
4331 here. */
4332 if (TREE_CODE (type) == ARRAY_TYPE)
4333 while (type != TYPE_MAIN_VARIANT (type))
4334 type = TYPE_MAIN_VARIANT (type);
4336 return type;
4339 /* Return true if the given type node represents a tagged type. */
4341 static inline bool
4342 is_tagged_type (const_tree type)
4344 enum tree_code code = TREE_CODE (type);
4346 return (code == RECORD_TYPE || code == UNION_TYPE
4347 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4350 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
4352 static void
4353 get_ref_die_offset_label (char *label, dw_die_ref ref)
4355 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
4358 /* Return die_offset of a DIE reference to a base type. */
4360 static unsigned long int
4361 get_base_type_offset (dw_die_ref ref)
4363 if (ref->die_offset)
4364 return ref->die_offset;
4365 if (comp_unit_die ()->die_abbrev)
4367 calc_base_type_die_sizes ();
4368 gcc_assert (ref->die_offset);
4370 return ref->die_offset;
4373 /* Return die_offset of a DIE reference other than base type. */
4375 static unsigned long int
4376 get_ref_die_offset (dw_die_ref ref)
4378 gcc_assert (ref->die_offset);
4379 return ref->die_offset;
4382 /* Convert a DIE tag into its string name. */
4384 static const char *
4385 dwarf_tag_name (unsigned int tag)
4387 const char *name = get_DW_TAG_name (tag);
4389 if (name != NULL)
4390 return name;
4392 return "DW_TAG_<unknown>";
4395 /* Convert a DWARF attribute code into its string name. */
4397 static const char *
4398 dwarf_attr_name (unsigned int attr)
4400 const char *name;
4402 switch (attr)
4404 #if VMS_DEBUGGING_INFO
4405 case DW_AT_HP_prologue:
4406 return "DW_AT_HP_prologue";
4407 #else
4408 case DW_AT_MIPS_loop_unroll_factor:
4409 return "DW_AT_MIPS_loop_unroll_factor";
4410 #endif
4412 #if VMS_DEBUGGING_INFO
4413 case DW_AT_HP_epilogue:
4414 return "DW_AT_HP_epilogue";
4415 #else
4416 case DW_AT_MIPS_stride:
4417 return "DW_AT_MIPS_stride";
4418 #endif
4421 name = get_DW_AT_name (attr);
4423 if (name != NULL)
4424 return name;
4426 return "DW_AT_<unknown>";
4429 /* Convert a DWARF value form code into its string name. */
4431 static const char *
4432 dwarf_form_name (unsigned int form)
4434 const char *name = get_DW_FORM_name (form);
4436 if (name != NULL)
4437 return name;
4439 return "DW_FORM_<unknown>";
4442 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4443 instance of an inlined instance of a decl which is local to an inline
4444 function, so we have to trace all of the way back through the origin chain
4445 to find out what sort of node actually served as the original seed for the
4446 given block. */
4448 static tree
4449 decl_ultimate_origin (const_tree decl)
4451 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4452 return NULL_TREE;
4454 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
4455 we're trying to output the abstract instance of this function. */
4456 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4457 return NULL_TREE;
4459 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4460 most distant ancestor, this should never happen. */
4461 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4463 return DECL_ABSTRACT_ORIGIN (decl);
4466 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4467 of a virtual function may refer to a base class, so we check the 'this'
4468 parameter. */
4470 static tree
4471 decl_class_context (tree decl)
4473 tree context = NULL_TREE;
4475 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4476 context = DECL_CONTEXT (decl);
4477 else
4478 context = TYPE_MAIN_VARIANT
4479 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4481 if (context && !TYPE_P (context))
4482 context = NULL_TREE;
4484 return context;
4487 /* Add an attribute/value pair to a DIE. */
4489 static inline void
4490 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
4492 /* Maybe this should be an assert? */
4493 if (die == NULL)
4494 return;
4496 if (flag_checking)
4498 /* Check we do not add duplicate attrs. Can't use get_AT here
4499 because that recurses to the specification/abstract origin DIE. */
4500 dw_attr_node *a;
4501 unsigned ix;
4502 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4503 gcc_assert (a->dw_attr != attr->dw_attr);
4506 vec_safe_reserve (die->die_attr, 1);
4507 vec_safe_push (die->die_attr, *attr);
4510 enum dw_val_class
4511 AT_class (dw_attr_node *a)
4513 return a->dw_attr_val.val_class;
4516 /* Return the index for any attribute that will be referenced with a
4517 DW_FORM_addrx/GNU_addr_index or DW_FORM_strx/GNU_str_index. String
4518 indices are stored in dw_attr_val.v.val_str for reference counting
4519 pruning. */
4521 static inline unsigned int
4522 AT_index (dw_attr_node *a)
4524 if (AT_class (a) == dw_val_class_str)
4525 return a->dw_attr_val.v.val_str->index;
4526 else if (a->dw_attr_val.val_entry != NULL)
4527 return a->dw_attr_val.val_entry->index;
4528 return NOT_INDEXED;
4531 /* Add a flag value attribute to a DIE. */
4533 static inline void
4534 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4536 dw_attr_node attr;
4538 attr.dw_attr = attr_kind;
4539 attr.dw_attr_val.val_class = dw_val_class_flag;
4540 attr.dw_attr_val.val_entry = NULL;
4541 attr.dw_attr_val.v.val_flag = flag;
4542 add_dwarf_attr (die, &attr);
4545 static inline unsigned
4546 AT_flag (dw_attr_node *a)
4548 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4549 return a->dw_attr_val.v.val_flag;
4552 /* Add a signed integer attribute value to a DIE. */
4554 static inline void
4555 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4557 dw_attr_node attr;
4559 attr.dw_attr = attr_kind;
4560 attr.dw_attr_val.val_class = dw_val_class_const;
4561 attr.dw_attr_val.val_entry = NULL;
4562 attr.dw_attr_val.v.val_int = int_val;
4563 add_dwarf_attr (die, &attr);
4566 HOST_WIDE_INT
4567 AT_int (dw_attr_node *a)
4569 gcc_assert (a && (AT_class (a) == dw_val_class_const
4570 || AT_class (a) == dw_val_class_const_implicit));
4571 return a->dw_attr_val.v.val_int;
4574 /* Add an unsigned integer attribute value to a DIE. */
4576 static inline void
4577 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4578 unsigned HOST_WIDE_INT unsigned_val)
4580 dw_attr_node attr;
4582 attr.dw_attr = attr_kind;
4583 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4584 attr.dw_attr_val.val_entry = NULL;
4585 attr.dw_attr_val.v.val_unsigned = unsigned_val;
4586 add_dwarf_attr (die, &attr);
4589 unsigned HOST_WIDE_INT
4590 AT_unsigned (dw_attr_node *a)
4592 gcc_assert (a && (AT_class (a) == dw_val_class_unsigned_const
4593 || AT_class (a) == dw_val_class_unsigned_const_implicit));
4594 return a->dw_attr_val.v.val_unsigned;
4597 dw_wide_int *
4598 alloc_dw_wide_int (const wide_int_ref &w)
4600 dw_wide_int *p
4601 = (dw_wide_int *) ggc_internal_alloc (sizeof (dw_wide_int)
4602 + ((w.get_len () - 1)
4603 * sizeof (HOST_WIDE_INT)));
4604 p->precision = w.get_precision ();
4605 p->len = w.get_len ();
4606 memcpy (p->val, w.get_val (), p->len * sizeof (HOST_WIDE_INT));
4607 return p;
4610 /* Add an unsigned wide integer attribute value to a DIE. */
4612 static inline void
4613 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
4614 const wide_int_ref &w)
4616 dw_attr_node attr;
4618 attr.dw_attr = attr_kind;
4619 attr.dw_attr_val.val_class = dw_val_class_wide_int;
4620 attr.dw_attr_val.val_entry = NULL;
4621 attr.dw_attr_val.v.val_wide = alloc_dw_wide_int (w);
4622 add_dwarf_attr (die, &attr);
4625 /* Add an unsigned double integer attribute value to a DIE. */
4627 static inline void
4628 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4629 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4631 dw_attr_node attr;
4633 attr.dw_attr = attr_kind;
4634 attr.dw_attr_val.val_class = dw_val_class_const_double;
4635 attr.dw_attr_val.val_entry = NULL;
4636 attr.dw_attr_val.v.val_double.high = high;
4637 attr.dw_attr_val.v.val_double.low = low;
4638 add_dwarf_attr (die, &attr);
4641 /* Add a floating point attribute value to a DIE and return it. */
4643 static inline void
4644 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4645 unsigned int length, unsigned int elt_size, unsigned char *array)
4647 dw_attr_node attr;
4649 attr.dw_attr = attr_kind;
4650 attr.dw_attr_val.val_class = dw_val_class_vec;
4651 attr.dw_attr_val.val_entry = NULL;
4652 attr.dw_attr_val.v.val_vec.length = length;
4653 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4654 attr.dw_attr_val.v.val_vec.array = array;
4655 add_dwarf_attr (die, &attr);
4658 /* Add an 8-byte data attribute value to a DIE. */
4660 static inline void
4661 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4662 unsigned char data8[8])
4664 dw_attr_node attr;
4666 attr.dw_attr = attr_kind;
4667 attr.dw_attr_val.val_class = dw_val_class_data8;
4668 attr.dw_attr_val.val_entry = NULL;
4669 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4670 add_dwarf_attr (die, &attr);
4673 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
4674 dwarf_split_debug_info, address attributes in dies destined for the
4675 final executable have force_direct set to avoid using indexed
4676 references. */
4678 static inline void
4679 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4680 bool force_direct)
4682 dw_attr_node attr;
4683 char * lbl_id;
4685 lbl_id = xstrdup (lbl_low);
4686 attr.dw_attr = DW_AT_low_pc;
4687 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4688 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4689 if (dwarf_split_debug_info && !force_direct)
4690 attr.dw_attr_val.val_entry
4691 = add_addr_table_entry (lbl_id, ate_kind_label);
4692 else
4693 attr.dw_attr_val.val_entry = NULL;
4694 add_dwarf_attr (die, &attr);
4696 attr.dw_attr = DW_AT_high_pc;
4697 if (dwarf_version < 4)
4698 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4699 else
4700 attr.dw_attr_val.val_class = dw_val_class_high_pc;
4701 lbl_id = xstrdup (lbl_high);
4702 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4703 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4704 && dwarf_split_debug_info && !force_direct)
4705 attr.dw_attr_val.val_entry
4706 = add_addr_table_entry (lbl_id, ate_kind_label);
4707 else
4708 attr.dw_attr_val.val_entry = NULL;
4709 add_dwarf_attr (die, &attr);
4712 /* Hash and equality functions for debug_str_hash. */
4714 hashval_t
4715 indirect_string_hasher::hash (indirect_string_node *x)
4717 return htab_hash_string (x->str);
4720 bool
4721 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4723 return strcmp (x1->str, x2) == 0;
4726 /* Add STR to the given string hash table. */
4728 static struct indirect_string_node *
4729 find_AT_string_in_table (const char *str,
4730 hash_table<indirect_string_hasher> *table,
4731 enum insert_option insert = INSERT)
4733 struct indirect_string_node *node;
4735 indirect_string_node **slot
4736 = table->find_slot_with_hash (str, htab_hash_string (str), insert);
4737 if (*slot == NULL)
4739 node = ggc_cleared_alloc<indirect_string_node> ();
4740 node->str = ggc_strdup (str);
4741 *slot = node;
4743 else
4744 node = *slot;
4746 node->refcount++;
4747 return node;
4750 /* Add STR to the indirect string hash table. */
4752 static struct indirect_string_node *
4753 find_AT_string (const char *str, enum insert_option insert = INSERT)
4755 if (! debug_str_hash)
4756 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4758 return find_AT_string_in_table (str, debug_str_hash, insert);
4761 /* Add a string attribute value to a DIE. */
4763 static inline void
4764 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4766 dw_attr_node attr;
4767 struct indirect_string_node *node;
4769 node = find_AT_string (str);
4771 attr.dw_attr = attr_kind;
4772 attr.dw_attr_val.val_class = dw_val_class_str;
4773 attr.dw_attr_val.val_entry = NULL;
4774 attr.dw_attr_val.v.val_str = node;
4775 add_dwarf_attr (die, &attr);
4778 static inline const char *
4779 AT_string (dw_attr_node *a)
4781 gcc_assert (a && AT_class (a) == dw_val_class_str);
4782 return a->dw_attr_val.v.val_str->str;
4785 /* Call this function directly to bypass AT_string_form's logic to put
4786 the string inline in the die. */
4788 static void
4789 set_indirect_string (struct indirect_string_node *node)
4791 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4792 /* Already indirect is a no op. */
4793 if (node->form == DW_FORM_strp
4794 || node->form == DW_FORM_line_strp
4795 || node->form == dwarf_FORM (DW_FORM_strx))
4797 gcc_assert (node->label);
4798 return;
4800 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4801 ++dw2_string_counter;
4802 node->label = xstrdup (label);
4804 if (!dwarf_split_debug_info)
4806 node->form = DW_FORM_strp;
4807 node->index = NOT_INDEXED;
4809 else
4811 node->form = dwarf_FORM (DW_FORM_strx);
4812 node->index = NO_INDEX_ASSIGNED;
4816 /* A helper function for dwarf2out_finish, called to reset indirect
4817 string decisions done for early LTO dwarf output before fat object
4818 dwarf output. */
4821 reset_indirect_string (indirect_string_node **h, void *)
4823 struct indirect_string_node *node = *h;
4824 if (node->form == DW_FORM_strp
4825 || node->form == DW_FORM_line_strp
4826 || node->form == dwarf_FORM (DW_FORM_strx))
4828 free (node->label);
4829 node->label = NULL;
4830 node->form = (dwarf_form) 0;
4831 node->index = 0;
4833 return 1;
4836 /* Add a string representing a file or filepath attribute value to a DIE. */
4838 static inline void
4839 add_filepath_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
4840 const char *str)
4842 if (! asm_outputs_debug_line_str ())
4843 add_AT_string (die, attr_kind, str);
4844 else
4846 dw_attr_node attr;
4847 struct indirect_string_node *node;
4849 if (!debug_line_str_hash)
4850 debug_line_str_hash
4851 = hash_table<indirect_string_hasher>::create_ggc (10);
4853 node = find_AT_string_in_table (str, debug_line_str_hash);
4854 set_indirect_string (node);
4855 node->form = DW_FORM_line_strp;
4857 attr.dw_attr = attr_kind;
4858 attr.dw_attr_val.val_class = dw_val_class_str;
4859 attr.dw_attr_val.val_entry = NULL;
4860 attr.dw_attr_val.v.val_str = node;
4861 add_dwarf_attr (die, &attr);
4865 /* Find out whether a string should be output inline in DIE
4866 or out-of-line in .debug_str section. */
4868 static enum dwarf_form
4869 find_string_form (struct indirect_string_node *node)
4871 unsigned int len;
4873 if (node->form)
4874 return node->form;
4876 len = strlen (node->str) + 1;
4878 /* If the string is shorter or equal to the size of the reference, it is
4879 always better to put it inline. */
4880 if (len <= (unsigned) dwarf_offset_size || node->refcount == 0)
4881 return node->form = DW_FORM_string;
4883 /* If we cannot expect the linker to merge strings in .debug_str
4884 section, only put it into .debug_str if it is worth even in this
4885 single module. */
4886 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4887 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4888 && (len - dwarf_offset_size) * node->refcount <= len))
4889 return node->form = DW_FORM_string;
4891 set_indirect_string (node);
4893 return node->form;
4896 /* Find out whether the string referenced from the attribute should be
4897 output inline in DIE or out-of-line in .debug_str section. */
4899 static enum dwarf_form
4900 AT_string_form (dw_attr_node *a)
4902 gcc_assert (a && AT_class (a) == dw_val_class_str);
4903 return find_string_form (a->dw_attr_val.v.val_str);
4906 /* Add a DIE reference attribute value to a DIE. */
4908 static inline void
4909 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4911 dw_attr_node attr;
4912 gcc_checking_assert (targ_die != NULL);
4913 gcc_assert (targ_die != die
4914 || (attr_kind != DW_AT_abstract_origin
4915 && attr_kind != DW_AT_specification));
4917 /* With LTO we can end up trying to reference something we didn't create
4918 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4919 if (targ_die == NULL)
4920 return;
4922 attr.dw_attr = attr_kind;
4923 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4924 attr.dw_attr_val.val_entry = NULL;
4925 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4926 attr.dw_attr_val.v.val_die_ref.external = 0;
4927 add_dwarf_attr (die, &attr);
4930 /* Change DIE reference REF to point to NEW_DIE instead. */
4932 static inline void
4933 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4935 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4936 ref->dw_attr_val.v.val_die_ref.die = new_die;
4937 ref->dw_attr_val.v.val_die_ref.external = 0;
4940 /* Add an AT_specification attribute to a DIE, and also make the back
4941 pointer from the specification to the definition. */
4943 static inline void
4944 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4946 add_AT_die_ref (die, DW_AT_specification, targ_die);
4947 gcc_assert (!targ_die->die_definition);
4948 targ_die->die_definition = die;
4951 static inline dw_die_ref
4952 AT_ref (dw_attr_node *a)
4954 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4955 return a->dw_attr_val.v.val_die_ref.die;
4958 static inline int
4959 AT_ref_external (dw_attr_node *a)
4961 if (a && AT_class (a) == dw_val_class_die_ref)
4962 return a->dw_attr_val.v.val_die_ref.external;
4964 return 0;
4967 static inline void
4968 set_AT_ref_external (dw_attr_node *a, int i)
4970 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4971 a->dw_attr_val.v.val_die_ref.external = i;
4974 /* Add a location description attribute value to a DIE. */
4976 static inline void
4977 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4979 dw_attr_node attr;
4981 attr.dw_attr = attr_kind;
4982 attr.dw_attr_val.val_class = dw_val_class_loc;
4983 attr.dw_attr_val.val_entry = NULL;
4984 attr.dw_attr_val.v.val_loc = loc;
4985 add_dwarf_attr (die, &attr);
4988 dw_loc_descr_ref
4989 AT_loc (dw_attr_node *a)
4991 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4992 return a->dw_attr_val.v.val_loc;
4995 static inline void
4996 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4998 dw_attr_node attr;
5000 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
5001 return;
5003 attr.dw_attr = attr_kind;
5004 attr.dw_attr_val.val_class = dw_val_class_loc_list;
5005 attr.dw_attr_val.val_entry = NULL;
5006 attr.dw_attr_val.v.val_loc_list = loc_list;
5007 add_dwarf_attr (die, &attr);
5008 have_location_lists = true;
5011 static inline dw_loc_list_ref
5012 AT_loc_list (dw_attr_node *a)
5014 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
5015 return a->dw_attr_val.v.val_loc_list;
5018 /* Add a view list attribute to DIE. It must have a DW_AT_location
5019 attribute, because the view list complements the location list. */
5021 static inline void
5022 add_AT_view_list (dw_die_ref die, enum dwarf_attribute attr_kind)
5024 dw_attr_node attr;
5026 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
5027 return;
5029 attr.dw_attr = attr_kind;
5030 attr.dw_attr_val.val_class = dw_val_class_view_list;
5031 attr.dw_attr_val.val_entry = NULL;
5032 attr.dw_attr_val.v.val_view_list = die;
5033 add_dwarf_attr (die, &attr);
5034 gcc_checking_assert (get_AT (die, DW_AT_location));
5035 gcc_assert (have_location_lists);
5038 /* Return a pointer to the location list referenced by the attribute.
5039 If the named attribute is a view list, look up the corresponding
5040 DW_AT_location attribute and return its location list. */
5042 static inline dw_loc_list_ref *
5043 AT_loc_list_ptr (dw_attr_node *a)
5045 gcc_assert (a);
5046 switch (AT_class (a))
5048 case dw_val_class_loc_list:
5049 return &a->dw_attr_val.v.val_loc_list;
5050 case dw_val_class_view_list:
5052 dw_attr_node *l;
5053 l = get_AT (a->dw_attr_val.v.val_view_list, DW_AT_location);
5054 if (!l)
5055 return NULL;
5056 gcc_checking_assert (l + 1 == a);
5057 return AT_loc_list_ptr (l);
5059 default:
5060 gcc_unreachable ();
5064 /* Return the location attribute value associated with a view list
5065 attribute value. */
5067 static inline dw_val_node *
5068 view_list_to_loc_list_val_node (dw_val_node *val)
5070 gcc_assert (val->val_class == dw_val_class_view_list);
5071 dw_attr_node *loc = get_AT (val->v.val_view_list, DW_AT_location);
5072 if (!loc)
5073 return NULL;
5074 gcc_checking_assert (&(loc + 1)->dw_attr_val == val);
5075 gcc_assert (AT_class (loc) == dw_val_class_loc_list);
5076 return &loc->dw_attr_val;
5079 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
5081 static hashval_t hash (addr_table_entry *);
5082 static bool equal (addr_table_entry *, addr_table_entry *);
5085 /* Table of entries into the .debug_addr section. */
5087 static GTY (()) hash_table<addr_hasher> *addr_index_table;
5089 /* Hash an address_table_entry. */
5091 hashval_t
5092 addr_hasher::hash (addr_table_entry *a)
5094 inchash::hash hstate;
5095 switch (a->kind)
5097 case ate_kind_rtx:
5098 hstate.add_int (0);
5099 break;
5100 case ate_kind_rtx_dtprel:
5101 hstate.add_int (1);
5102 break;
5103 case ate_kind_label:
5104 return htab_hash_string (a->addr.label);
5105 default:
5106 gcc_unreachable ();
5108 inchash::add_rtx (a->addr.rtl, hstate);
5109 return hstate.end ();
5112 /* Determine equality for two address_table_entries. */
5114 bool
5115 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
5117 if (a1->kind != a2->kind)
5118 return false;
5119 switch (a1->kind)
5121 case ate_kind_rtx:
5122 case ate_kind_rtx_dtprel:
5123 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
5124 case ate_kind_label:
5125 return strcmp (a1->addr.label, a2->addr.label) == 0;
5126 default:
5127 gcc_unreachable ();
5131 /* Initialize an addr_table_entry. */
5133 void
5134 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
5136 e->kind = kind;
5137 switch (kind)
5139 case ate_kind_rtx:
5140 case ate_kind_rtx_dtprel:
5141 e->addr.rtl = (rtx) addr;
5142 break;
5143 case ate_kind_label:
5144 e->addr.label = (char *) addr;
5145 break;
5147 e->refcount = 0;
5148 e->index = NO_INDEX_ASSIGNED;
5151 /* Add attr to the address table entry to the table. Defer setting an
5152 index until output time. */
5154 static addr_table_entry *
5155 add_addr_table_entry (void *addr, enum ate_kind kind)
5157 addr_table_entry *node;
5158 addr_table_entry finder;
5160 gcc_assert (dwarf_split_debug_info);
5161 if (! addr_index_table)
5162 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
5163 init_addr_table_entry (&finder, kind, addr);
5164 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
5166 if (*slot == HTAB_EMPTY_ENTRY)
5168 node = ggc_cleared_alloc<addr_table_entry> ();
5169 init_addr_table_entry (node, kind, addr);
5170 *slot = node;
5172 else
5173 node = *slot;
5175 node->refcount++;
5176 return node;
5179 /* Remove an entry from the addr table by decrementing its refcount.
5180 Strictly, decrementing the refcount would be enough, but the
5181 assertion that the entry is actually in the table has found
5182 bugs. */
5184 static void
5185 remove_addr_table_entry (addr_table_entry *entry)
5187 gcc_assert (dwarf_split_debug_info && addr_index_table);
5188 /* After an index is assigned, the table is frozen. */
5189 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
5190 entry->refcount--;
5193 /* Given a location list, remove all addresses it refers to from the
5194 address_table. */
5196 static void
5197 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
5199 for (; descr; descr = descr->dw_loc_next)
5200 if (descr->dw_loc_oprnd1.val_entry != NULL)
5202 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
5203 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
5207 /* A helper function for dwarf2out_finish called through
5208 htab_traverse. Assign an addr_table_entry its index. All entries
5209 must be collected into the table when this function is called,
5210 because the indexing code relies on htab_traverse to traverse nodes
5211 in the same order for each run. */
5214 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
5216 addr_table_entry *node = *h;
5218 /* Don't index unreferenced nodes. */
5219 if (node->refcount == 0)
5220 return 1;
5222 gcc_assert (node->index == NO_INDEX_ASSIGNED);
5223 node->index = *index;
5224 *index += 1;
5226 return 1;
5229 /* Return the tag of a given DIE. */
5231 enum dwarf_tag
5232 dw_get_die_tag (dw_die_ref die)
5234 return die->die_tag;
5237 /* Return a reference to the children list of a given DIE. */
5239 dw_die_ref
5240 dw_get_die_child (dw_die_ref die)
5242 return die->die_child;
5245 /* Return a reference to the sibling of a given DIE. */
5247 dw_die_ref
5248 dw_get_die_sib (dw_die_ref die)
5250 return die->die_sib;
5253 /* Add an address constant attribute value to a DIE. When using
5254 dwarf_split_debug_info, address attributes in dies destined for the
5255 final executable should be direct references--setting the parameter
5256 force_direct ensures this behavior. */
5258 static inline void
5259 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
5260 bool force_direct)
5262 dw_attr_node attr;
5264 attr.dw_attr = attr_kind;
5265 attr.dw_attr_val.val_class = dw_val_class_addr;
5266 attr.dw_attr_val.v.val_addr = addr;
5267 if (dwarf_split_debug_info && !force_direct)
5268 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
5269 else
5270 attr.dw_attr_val.val_entry = NULL;
5271 add_dwarf_attr (die, &attr);
5274 /* Get the RTX from to an address DIE attribute. */
5276 static inline rtx
5277 AT_addr (dw_attr_node *a)
5279 gcc_assert (a && AT_class (a) == dw_val_class_addr);
5280 return a->dw_attr_val.v.val_addr;
5283 /* Add a file attribute value to a DIE. */
5285 static inline void
5286 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
5287 struct dwarf_file_data *fd)
5289 dw_attr_node attr;
5291 attr.dw_attr = attr_kind;
5292 attr.dw_attr_val.val_class = dw_val_class_file;
5293 attr.dw_attr_val.val_entry = NULL;
5294 attr.dw_attr_val.v.val_file = fd;
5295 add_dwarf_attr (die, &attr);
5298 /* Get the dwarf_file_data from a file DIE attribute. */
5300 static inline struct dwarf_file_data *
5301 AT_file (dw_attr_node *a)
5303 gcc_assert (a && (AT_class (a) == dw_val_class_file
5304 || AT_class (a) == dw_val_class_file_implicit));
5305 return a->dw_attr_val.v.val_file;
5308 #if VMS_DEBUGGING_INFO
5309 /* Add a vms delta attribute value to a DIE. */
5311 static inline void
5312 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
5313 const char *lbl1, const char *lbl2)
5315 dw_attr_node attr;
5317 attr.dw_attr = attr_kind;
5318 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
5319 attr.dw_attr_val.val_entry = NULL;
5320 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
5321 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
5322 add_dwarf_attr (die, &attr);
5324 #endif
5326 /* Add a symbolic view identifier attribute value to a DIE. */
5328 static inline void
5329 add_AT_symview (dw_die_ref die, enum dwarf_attribute attr_kind,
5330 const char *view_label)
5332 dw_attr_node attr;
5334 attr.dw_attr = attr_kind;
5335 attr.dw_attr_val.val_class = dw_val_class_symview;
5336 attr.dw_attr_val.val_entry = NULL;
5337 attr.dw_attr_val.v.val_symbolic_view = xstrdup (view_label);
5338 add_dwarf_attr (die, &attr);
5341 /* Add a label identifier attribute value to a DIE. */
5343 static inline void
5344 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
5345 const char *lbl_id)
5347 dw_attr_node attr;
5349 attr.dw_attr = attr_kind;
5350 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
5351 attr.dw_attr_val.val_entry = NULL;
5352 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5353 if (dwarf_split_debug_info)
5354 attr.dw_attr_val.val_entry
5355 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
5356 ate_kind_label);
5357 add_dwarf_attr (die, &attr);
5360 /* Add a section offset attribute value to a DIE, an offset into the
5361 debug_line section. */
5363 static inline void
5364 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5365 const char *label)
5367 dw_attr_node attr;
5369 attr.dw_attr = attr_kind;
5370 attr.dw_attr_val.val_class = dw_val_class_lineptr;
5371 attr.dw_attr_val.val_entry = NULL;
5372 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5373 add_dwarf_attr (die, &attr);
5376 /* Add a section offset attribute value to a DIE, an offset into the
5377 debug_macinfo section. */
5379 static inline void
5380 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5381 const char *label)
5383 dw_attr_node attr;
5385 attr.dw_attr = attr_kind;
5386 attr.dw_attr_val.val_class = dw_val_class_macptr;
5387 attr.dw_attr_val.val_entry = NULL;
5388 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5389 add_dwarf_attr (die, &attr);
5392 /* Add a range_list attribute value to a DIE. When using
5393 dwarf_split_debug_info, address attributes in dies destined for the
5394 final executable should be direct references--setting the parameter
5395 force_direct ensures this behavior. */
5397 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
5398 #define RELOCATED_OFFSET (NULL)
5400 static void
5401 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5402 long unsigned int offset, bool force_direct)
5404 dw_attr_node attr;
5406 attr.dw_attr = attr_kind;
5407 attr.dw_attr_val.val_class = dw_val_class_range_list;
5408 /* For the range_list attribute, use val_entry to store whether the
5409 offset should follow split-debug-info or normal semantics. This
5410 value is read in output_range_list_offset. */
5411 if (dwarf_split_debug_info && !force_direct)
5412 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
5413 else
5414 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
5415 attr.dw_attr_val.v.val_offset = offset;
5416 add_dwarf_attr (die, &attr);
5419 /* Return the start label of a delta attribute. */
5421 static inline const char *
5422 AT_vms_delta1 (dw_attr_node *a)
5424 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
5425 return a->dw_attr_val.v.val_vms_delta.lbl1;
5428 /* Return the end label of a delta attribute. */
5430 static inline const char *
5431 AT_vms_delta2 (dw_attr_node *a)
5433 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
5434 return a->dw_attr_val.v.val_vms_delta.lbl2;
5437 static inline const char *
5438 AT_lbl (dw_attr_node *a)
5440 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5441 || AT_class (a) == dw_val_class_lineptr
5442 || AT_class (a) == dw_val_class_macptr
5443 || AT_class (a) == dw_val_class_loclistsptr
5444 || AT_class (a) == dw_val_class_high_pc));
5445 return a->dw_attr_val.v.val_lbl_id;
5448 /* Get the attribute of type attr_kind. */
5450 dw_attr_node *
5451 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5453 dw_attr_node *a;
5454 unsigned ix;
5455 dw_die_ref spec = NULL;
5457 if (! die)
5458 return NULL;
5460 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5461 if (a->dw_attr == attr_kind)
5462 return a;
5463 else if (a->dw_attr == DW_AT_specification
5464 || a->dw_attr == DW_AT_abstract_origin)
5465 spec = AT_ref (a);
5467 if (spec)
5468 return get_AT (spec, attr_kind);
5470 return NULL;
5473 /* Returns the parent of the declaration of DIE. */
5475 static dw_die_ref
5476 get_die_parent (dw_die_ref die)
5478 dw_die_ref t;
5480 if (!die)
5481 return NULL;
5483 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
5484 || (t = get_AT_ref (die, DW_AT_specification)))
5485 die = t;
5487 return die->die_parent;
5490 /* Return the "low pc" attribute value, typically associated with a subprogram
5491 DIE. Return null if the "low pc" attribute is either not present, or if it
5492 cannot be represented as an assembler label identifier. */
5494 static inline const char *
5495 get_AT_low_pc (dw_die_ref die)
5497 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
5499 return a ? AT_lbl (a) : NULL;
5502 /* Return the value of the string attribute designated by ATTR_KIND, or
5503 NULL if it is not present. */
5505 const char *
5506 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5508 dw_attr_node *a = get_AT (die, attr_kind);
5510 return a ? AT_string (a) : NULL;
5513 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5514 if it is not present. */
5517 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5519 dw_attr_node *a = get_AT (die, attr_kind);
5521 return a ? AT_flag (a) : 0;
5524 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5525 if it is not present. */
5527 unsigned
5528 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5530 dw_attr_node *a = get_AT (die, attr_kind);
5532 return a ? AT_unsigned (a) : 0;
5535 dw_die_ref
5536 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5538 dw_attr_node *a = get_AT (die, attr_kind);
5540 return a ? AT_ref (a) : NULL;
5543 struct dwarf_file_data *
5544 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5546 dw_attr_node *a = get_AT (die, attr_kind);
5548 return a ? AT_file (a) : NULL;
5551 /* Return TRUE if the language is C. */
5553 static inline bool
5554 is_c (void)
5556 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5558 return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_C99
5559 || lang == DW_LANG_C11 || lang == DW_LANG_ObjC);
5564 /* Return TRUE if the language is C++. */
5566 static inline bool
5567 is_cxx (void)
5569 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5571 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
5572 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
5575 /* Return TRUE if DECL was created by the C++ frontend. */
5577 static bool
5578 is_cxx (const_tree decl)
5580 if (in_lto_p)
5582 const_tree context = get_ultimate_context (decl);
5583 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5584 return startswith (TRANSLATION_UNIT_LANGUAGE (context), "GNU C++");
5586 return is_cxx ();
5589 /* Return TRUE if the language is Fortran. */
5591 static inline bool
5592 is_fortran (void)
5594 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5596 return (lang == DW_LANG_Fortran77
5597 || lang == DW_LANG_Fortran90
5598 || lang == DW_LANG_Fortran95
5599 || lang == DW_LANG_Fortran03
5600 || lang == DW_LANG_Fortran08);
5603 static inline bool
5604 is_fortran (const_tree decl)
5606 if (in_lto_p)
5608 const_tree context = get_ultimate_context (decl);
5609 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5610 return (strncmp (TRANSLATION_UNIT_LANGUAGE (context),
5611 "GNU Fortran", 11) == 0
5612 || strcmp (TRANSLATION_UNIT_LANGUAGE (context),
5613 "GNU F77") == 0);
5615 return is_fortran ();
5618 /* Return TRUE if the language is Rust.
5619 Note, returns FALSE for dwarf_version < 5 && dwarf_strict. */
5621 static inline bool
5622 is_rust (void)
5624 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5626 return lang == DW_LANG_Rust;
5629 /* Return TRUE if the language is Ada. */
5631 static inline bool
5632 is_ada (void)
5634 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5636 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5639 /* Return TRUE if the language is D. */
5641 static inline bool
5642 is_dlang (void)
5644 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5646 return lang == DW_LANG_D;
5649 /* Remove the specified attribute if present. Return TRUE if removal
5650 was successful. */
5652 static bool
5653 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5655 dw_attr_node *a;
5656 unsigned ix;
5658 if (! die)
5659 return false;
5661 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5662 if (a->dw_attr == attr_kind)
5664 if (AT_class (a) == dw_val_class_str)
5665 if (a->dw_attr_val.v.val_str->refcount)
5666 a->dw_attr_val.v.val_str->refcount--;
5668 /* vec::ordered_remove should help reduce the number of abbrevs
5669 that are needed. */
5670 die->die_attr->ordered_remove (ix);
5671 return true;
5673 return false;
5676 /* Remove CHILD from its parent. PREV must have the property that
5677 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
5679 static void
5680 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5682 gcc_assert (child->die_parent == prev->die_parent);
5683 gcc_assert (prev->die_sib == child);
5684 if (prev == child)
5686 gcc_assert (child->die_parent->die_child == child);
5687 prev = NULL;
5689 else
5690 prev->die_sib = child->die_sib;
5691 if (child->die_parent->die_child == child)
5692 child->die_parent->die_child = prev;
5693 child->die_sib = NULL;
5696 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
5697 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
5699 static void
5700 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
5702 dw_die_ref parent = old_child->die_parent;
5704 gcc_assert (parent == prev->die_parent);
5705 gcc_assert (prev->die_sib == old_child);
5707 new_child->die_parent = parent;
5708 if (prev == old_child)
5710 gcc_assert (parent->die_child == old_child);
5711 new_child->die_sib = new_child;
5713 else
5715 prev->die_sib = new_child;
5716 new_child->die_sib = old_child->die_sib;
5718 if (old_child->die_parent->die_child == old_child)
5719 old_child->die_parent->die_child = new_child;
5720 old_child->die_sib = NULL;
5723 /* Move all children from OLD_PARENT to NEW_PARENT. */
5725 static void
5726 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
5728 dw_die_ref c;
5729 new_parent->die_child = old_parent->die_child;
5730 old_parent->die_child = NULL;
5731 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
5734 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
5735 matches TAG. */
5737 static void
5738 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5740 dw_die_ref c;
5742 c = die->die_child;
5743 if (c) do {
5744 dw_die_ref prev = c;
5745 c = c->die_sib;
5746 while (c->die_tag == tag)
5748 remove_child_with_prev (c, prev);
5749 c->die_parent = NULL;
5750 /* Might have removed every child. */
5751 if (die->die_child == NULL)
5752 return;
5753 c = prev->die_sib;
5755 } while (c != die->die_child);
5758 /* Add a CHILD_DIE as the last child of DIE. */
5760 static void
5761 add_child_die (dw_die_ref die, dw_die_ref child_die)
5763 /* FIXME this should probably be an assert. */
5764 if (! die || ! child_die)
5765 return;
5766 gcc_assert (die != child_die);
5768 child_die->die_parent = die;
5769 if (die->die_child)
5771 child_die->die_sib = die->die_child->die_sib;
5772 die->die_child->die_sib = child_die;
5774 else
5775 child_die->die_sib = child_die;
5776 die->die_child = child_die;
5779 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE. */
5781 static void
5782 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
5783 dw_die_ref after_die)
5785 gcc_assert (die
5786 && child_die
5787 && after_die
5788 && die->die_child
5789 && die != child_die);
5791 child_die->die_parent = die;
5792 child_die->die_sib = after_die->die_sib;
5793 after_die->die_sib = child_die;
5794 if (die->die_child == after_die)
5795 die->die_child = child_die;
5798 /* Unassociate CHILD from its parent, and make its parent be
5799 NEW_PARENT. */
5801 static void
5802 reparent_child (dw_die_ref child, dw_die_ref new_parent)
5804 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
5805 if (p->die_sib == child)
5807 remove_child_with_prev (child, p);
5808 break;
5810 add_child_die (new_parent, child);
5813 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5814 is the specification, to the end of PARENT's list of children.
5815 This is done by removing and re-adding it. */
5817 static void
5818 splice_child_die (dw_die_ref parent, dw_die_ref child)
5820 /* We want the declaration DIE from inside the class, not the
5821 specification DIE at toplevel. */
5822 if (child->die_parent != parent)
5824 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5826 if (tmp)
5827 child = tmp;
5830 gcc_assert (child->die_parent == parent
5831 || (child->die_parent
5832 == get_AT_ref (parent, DW_AT_specification)));
5834 reparent_child (child, parent);
5837 /* Create and return a new die with TAG_VALUE as tag. */
5839 dw_die_ref
5840 new_die_raw (enum dwarf_tag tag_value)
5842 dw_die_ref die = ggc_cleared_alloc<die_node> ();
5843 die->die_tag = tag_value;
5844 return die;
5847 /* Create and return a new die with a parent of PARENT_DIE. If
5848 PARENT_DIE is NULL, the new DIE is placed in limbo and an
5849 associated tree T must be supplied to determine parenthood
5850 later. */
5852 static inline dw_die_ref
5853 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5855 dw_die_ref die = new_die_raw (tag_value);
5857 if (parent_die != NULL)
5858 add_child_die (parent_die, die);
5859 else
5861 limbo_die_node *limbo_node;
5863 /* No DIEs created after early dwarf should end up in limbo,
5864 because the limbo list should not persist past LTO
5865 streaming. */
5866 if (tag_value != DW_TAG_compile_unit
5867 /* These are allowed because they're generated while
5868 breaking out COMDAT units late. */
5869 && tag_value != DW_TAG_type_unit
5870 && tag_value != DW_TAG_skeleton_unit
5871 && !early_dwarf
5872 /* Allow nested functions to live in limbo because they will
5873 only temporarily live there, as decls_for_scope will fix
5874 them up. */
5875 && (TREE_CODE (t) != FUNCTION_DECL
5876 || !decl_function_context (t))
5877 /* Same as nested functions above but for types. Types that
5878 are local to a function will be fixed in
5879 decls_for_scope. */
5880 && (!RECORD_OR_UNION_TYPE_P (t)
5881 || !TYPE_CONTEXT (t)
5882 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5883 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5884 especially in the ltrans stage, but once we implement LTO
5885 dwarf streaming, we should remove this exception. */
5886 && !in_lto_p)
5888 fprintf (stderr, "symbol ended up in limbo too late:");
5889 debug_generic_stmt (t);
5890 gcc_unreachable ();
5893 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5894 limbo_node->die = die;
5895 limbo_node->created_for = t;
5896 limbo_node->next = limbo_die_list;
5897 limbo_die_list = limbo_node;
5900 return die;
5903 /* Return the DIE associated with the given type specifier. */
5905 dw_die_ref
5906 lookup_type_die (tree type)
5908 dw_die_ref die = TYPE_SYMTAB_DIE (type);
5909 if (die && die->removed)
5911 TYPE_SYMTAB_DIE (type) = NULL;
5912 TREE_ASM_WRITTEN (type) = 0;
5913 return NULL;
5915 return die;
5918 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5919 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5920 anonymous type instead the one of the naming typedef. */
5922 static inline dw_die_ref
5923 strip_naming_typedef (tree type, dw_die_ref type_die)
5925 if (type
5926 && TREE_CODE (type) == RECORD_TYPE
5927 && type_die
5928 && type_die->die_tag == DW_TAG_typedef
5929 && is_naming_typedef_decl (TYPE_NAME (type)))
5930 type_die = get_AT_ref (type_die, DW_AT_type);
5931 return type_die;
5934 /* Like lookup_type_die, but if type is an anonymous type named by a
5935 typedef[1], return the DIE of the anonymous type instead the one of
5936 the naming typedef. This is because in gen_typedef_die, we did
5937 equate the anonymous struct named by the typedef with the DIE of
5938 the naming typedef. So by default, lookup_type_die on an anonymous
5939 struct yields the DIE of the naming typedef.
5941 [1]: Read the comment of is_naming_typedef_decl to learn about what
5942 a naming typedef is. */
5944 static inline dw_die_ref
5945 lookup_type_die_strip_naming_typedef (tree type)
5947 dw_die_ref die = lookup_type_die (type);
5948 return strip_naming_typedef (type, die);
5951 /* Equate a DIE to a given type specifier. */
5953 static inline void
5954 equate_type_number_to_die (tree type, dw_die_ref type_die)
5956 TYPE_SYMTAB_DIE (type) = type_die;
5959 static dw_die_ref maybe_create_die_with_external_ref (tree);
5960 struct GTY(()) sym_off_pair
5962 const char * GTY((skip)) sym;
5963 unsigned HOST_WIDE_INT off;
5965 static GTY(()) hash_map<tree, sym_off_pair> *external_die_map;
5967 /* Returns a hash value for X (which really is a die_struct). */
5969 inline hashval_t
5970 decl_die_hasher::hash (die_node *x)
5972 return (hashval_t) x->decl_id;
5975 /* Return true if decl_id of die_struct X is the same as UID of decl *Y. */
5977 inline bool
5978 decl_die_hasher::equal (die_node *x, tree y)
5980 return (x->decl_id == DECL_UID (y));
5983 /* Return the DIE associated with a given declaration. */
5985 dw_die_ref
5986 lookup_decl_die (tree decl)
5988 dw_die_ref *die = decl_die_table->find_slot_with_hash (decl, DECL_UID (decl),
5989 NO_INSERT);
5990 if (!die)
5992 if (in_lto_p)
5993 return maybe_create_die_with_external_ref (decl);
5994 return NULL;
5996 if ((*die)->removed)
5998 decl_die_table->clear_slot (die);
5999 return NULL;
6001 return *die;
6005 /* Return the DIE associated with BLOCK. */
6007 static inline dw_die_ref
6008 lookup_block_die (tree block)
6010 dw_die_ref die = BLOCK_DIE (block);
6011 if (!die && in_lto_p)
6012 return maybe_create_die_with_external_ref (block);
6013 return die;
6016 /* Associate DIE with BLOCK. */
6018 static inline void
6019 equate_block_to_die (tree block, dw_die_ref die)
6021 BLOCK_DIE (block) = die;
6023 #undef BLOCK_DIE
6026 /* For DECL which might have early dwarf output query a SYMBOL + OFFSET
6027 style reference. Return true if we found one refering to a DIE for
6028 DECL, otherwise return false. */
6030 static bool
6031 dwarf2out_die_ref_for_decl (tree decl, const char **sym,
6032 unsigned HOST_WIDE_INT *off)
6034 dw_die_ref die;
6036 if (in_lto_p)
6038 /* During WPA stage and incremental linking we use a hash-map
6039 to store the decl <-> label + offset map. */
6040 if (!external_die_map)
6041 return false;
6042 sym_off_pair *desc = external_die_map->get (decl);
6043 if (!desc)
6044 return false;
6045 *sym = desc->sym;
6046 *off = desc->off;
6047 return true;
6050 if (TREE_CODE (decl) == BLOCK)
6051 die = lookup_block_die (decl);
6052 else
6053 die = lookup_decl_die (decl);
6054 if (!die)
6055 return false;
6057 /* Similar to get_ref_die_offset_label, but using the "correct"
6058 label. */
6059 *off = die->die_offset;
6060 while (die->die_parent)
6061 die = die->die_parent;
6062 /* For the containing CU DIE we compute a die_symbol in
6063 compute_comp_unit_symbol. */
6064 if (die->die_tag == DW_TAG_compile_unit)
6066 gcc_assert (die->die_id.die_symbol != NULL);
6067 *sym = die->die_id.die_symbol;
6068 return true;
6070 /* While we can gracefully handle running into say a type unit
6071 we don't really want and consider this a bug. */
6072 if (flag_checking)
6073 gcc_unreachable ();
6074 return false;
6077 /* Add a reference of kind ATTR_KIND to a DIE at SYMBOL + OFFSET to DIE. */
6079 static void
6080 add_AT_external_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind,
6081 const char *symbol, HOST_WIDE_INT offset)
6083 /* Create a fake DIE that contains the reference. Don't use
6084 new_die because we don't want to end up in the limbo list. */
6085 /* ??? We probably want to share these, thus put a ref to the DIE
6086 we create here to the external_die_map entry. */
6087 dw_die_ref ref = new_die_raw (die->die_tag);
6088 ref->die_id.die_symbol = symbol;
6089 ref->die_offset = offset;
6090 ref->with_offset = 1;
6091 add_AT_die_ref (die, attr_kind, ref);
6094 /* Create a DIE for DECL if required and add a reference to a DIE
6095 at SYMBOL + OFFSET which contains attributes dumped early. */
6097 static void
6098 dwarf2out_register_external_die (tree decl, const char *sym,
6099 unsigned HOST_WIDE_INT off)
6101 if (debug_info_level == DINFO_LEVEL_NONE)
6102 return;
6104 if (!external_die_map)
6105 external_die_map = hash_map<tree, sym_off_pair>::create_ggc (1000);
6106 gcc_checking_assert (!external_die_map->get (decl));
6107 sym_off_pair p = { IDENTIFIER_POINTER (get_identifier (sym)), off };
6108 external_die_map->put (decl, p);
6111 /* If we have a registered external DIE for DECL return a new DIE for
6112 the concrete instance with an appropriate abstract origin. */
6114 static dw_die_ref
6115 maybe_create_die_with_external_ref (tree decl)
6117 if (!external_die_map)
6118 return NULL;
6119 sym_off_pair *desc = external_die_map->get (decl);
6120 if (!desc)
6121 return NULL;
6123 const char *sym = desc->sym;
6124 unsigned HOST_WIDE_INT off = desc->off;
6125 external_die_map->remove (decl);
6127 in_lto_p = false;
6128 dw_die_ref die = (TREE_CODE (decl) == BLOCK
6129 ? lookup_block_die (decl) : lookup_decl_die (decl));
6130 gcc_assert (!die);
6131 in_lto_p = true;
6133 tree ctx;
6134 dw_die_ref parent = NULL;
6135 /* Need to lookup a DIE for the decls context - the containing
6136 function or translation unit. */
6137 if (TREE_CODE (decl) == BLOCK)
6139 ctx = BLOCK_SUPERCONTEXT (decl);
6140 /* ??? We do not output DIEs for all scopes thus skip as
6141 many DIEs as needed. */
6142 while (TREE_CODE (ctx) == BLOCK
6143 && !lookup_block_die (ctx))
6144 ctx = BLOCK_SUPERCONTEXT (ctx);
6146 else
6147 ctx = DECL_CONTEXT (decl);
6148 /* Peel types in the context stack. */
6149 while (ctx && TYPE_P (ctx))
6150 ctx = TYPE_CONTEXT (ctx);
6151 /* Likewise namespaces in case we do not want to emit DIEs for them. */
6152 if (debug_info_level <= DINFO_LEVEL_TERSE)
6153 while (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
6154 ctx = DECL_CONTEXT (ctx);
6155 if (ctx)
6157 if (TREE_CODE (ctx) == BLOCK)
6158 parent = lookup_block_die (ctx);
6159 else if (TREE_CODE (ctx) == TRANSLATION_UNIT_DECL
6160 /* Keep the 1:1 association during WPA. */
6161 && !flag_wpa
6162 && flag_incremental_link != INCREMENTAL_LINK_LTO)
6163 /* Otherwise all late annotations go to the main CU which
6164 imports the original CUs. */
6165 parent = comp_unit_die ();
6166 else if (TREE_CODE (ctx) == FUNCTION_DECL
6167 && TREE_CODE (decl) != FUNCTION_DECL
6168 && TREE_CODE (decl) != PARM_DECL
6169 && TREE_CODE (decl) != RESULT_DECL
6170 && TREE_CODE (decl) != BLOCK)
6171 /* Leave function local entities parent determination to when
6172 we process scope vars. */
6174 else
6175 parent = lookup_decl_die (ctx);
6177 else
6178 /* In some cases the FEs fail to set DECL_CONTEXT properly.
6179 Handle this case gracefully by globalizing stuff. */
6180 parent = comp_unit_die ();
6181 /* Create a DIE "stub". */
6182 switch (TREE_CODE (decl))
6184 case TRANSLATION_UNIT_DECL:
6186 die = comp_unit_die ();
6187 /* We re-target all CU decls to the LTRANS CU DIE, so no need
6188 to create a DIE for the original CUs. */
6189 return die;
6191 case NAMESPACE_DECL:
6192 if (is_fortran (decl))
6193 die = new_die (DW_TAG_module, parent, decl);
6194 else
6195 die = new_die (DW_TAG_namespace, parent, decl);
6196 break;
6197 case FUNCTION_DECL:
6198 die = new_die (DW_TAG_subprogram, parent, decl);
6199 break;
6200 case VAR_DECL:
6201 die = new_die (DW_TAG_variable, parent, decl);
6202 break;
6203 case RESULT_DECL:
6204 die = new_die (DW_TAG_variable, parent, decl);
6205 break;
6206 case PARM_DECL:
6207 die = new_die (DW_TAG_formal_parameter, parent, decl);
6208 break;
6209 case CONST_DECL:
6210 die = new_die (DW_TAG_constant, parent, decl);
6211 break;
6212 case LABEL_DECL:
6213 die = new_die (DW_TAG_label, parent, decl);
6214 break;
6215 case BLOCK:
6216 die = new_die (DW_TAG_lexical_block, parent, decl);
6217 break;
6218 default:
6219 gcc_unreachable ();
6221 if (TREE_CODE (decl) == BLOCK)
6222 equate_block_to_die (decl, die);
6223 else
6224 equate_decl_number_to_die (decl, die);
6226 add_desc_attribute (die, decl);
6228 /* Add a reference to the DIE providing early debug at $sym + off. */
6229 add_AT_external_die_ref (die, DW_AT_abstract_origin, sym, off);
6231 return die;
6234 /* Returns a hash value for X (which really is a var_loc_list). */
6236 inline hashval_t
6237 decl_loc_hasher::hash (var_loc_list *x)
6239 return (hashval_t) x->decl_id;
6242 /* Return true if decl_id of var_loc_list X is the same as
6243 UID of decl *Y. */
6245 inline bool
6246 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
6248 return (x->decl_id == DECL_UID (y));
6251 /* Return the var_loc list associated with a given declaration. */
6253 static inline var_loc_list *
6254 lookup_decl_loc (const_tree decl)
6256 if (!decl_loc_table)
6257 return NULL;
6258 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
6261 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
6263 inline hashval_t
6264 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
6266 return (hashval_t) x->decl_id;
6269 /* Return true if decl_id of cached_dw_loc_list X is the same as
6270 UID of decl *Y. */
6272 inline bool
6273 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
6275 return (x->decl_id == DECL_UID (y));
6278 /* Equate a DIE to a particular declaration. */
6280 static void
6281 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
6283 unsigned int decl_id = DECL_UID (decl);
6285 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
6286 decl_die->decl_id = decl_id;
6289 /* Return how many bits covers PIECE EXPR_LIST. */
6291 static HOST_WIDE_INT
6292 decl_piece_bitsize (rtx piece)
6294 int ret = (int) GET_MODE (piece);
6295 if (ret)
6296 return ret;
6297 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
6298 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
6299 return INTVAL (XEXP (XEXP (piece, 0), 0));
6302 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
6304 static rtx *
6305 decl_piece_varloc_ptr (rtx piece)
6307 if ((int) GET_MODE (piece))
6308 return &XEXP (piece, 0);
6309 else
6310 return &XEXP (XEXP (piece, 0), 1);
6313 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
6314 Next is the chain of following piece nodes. */
6316 static rtx_expr_list *
6317 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
6319 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
6320 return alloc_EXPR_LIST (bitsize, loc_note, next);
6321 else
6322 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
6323 GEN_INT (bitsize),
6324 loc_note), next);
6327 /* Return rtx that should be stored into loc field for
6328 LOC_NOTE and BITPOS/BITSIZE. */
6330 static rtx
6331 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
6332 HOST_WIDE_INT bitsize)
6334 if (bitsize != -1)
6336 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
6337 if (bitpos != 0)
6338 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
6340 return loc_note;
6343 /* This function either modifies location piece list *DEST in
6344 place (if SRC and INNER is NULL), or copies location piece list
6345 *SRC to *DEST while modifying it. Location BITPOS is modified
6346 to contain LOC_NOTE, any pieces overlapping it are removed resp.
6347 not copied and if needed some padding around it is added.
6348 When modifying in place, DEST should point to EXPR_LIST where
6349 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
6350 to the start of the whole list and INNER points to the EXPR_LIST
6351 where earlier pieces cover PIECE_BITPOS bits. */
6353 static void
6354 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
6355 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
6356 HOST_WIDE_INT bitsize, rtx loc_note)
6358 HOST_WIDE_INT diff;
6359 bool copy = inner != NULL;
6361 if (copy)
6363 /* First copy all nodes preceding the current bitpos. */
6364 while (src != inner)
6366 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
6367 decl_piece_bitsize (*src), NULL_RTX);
6368 dest = &XEXP (*dest, 1);
6369 src = &XEXP (*src, 1);
6372 /* Add padding if needed. */
6373 if (bitpos != piece_bitpos)
6375 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
6376 copy ? NULL_RTX : *dest);
6377 dest = &XEXP (*dest, 1);
6379 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
6381 gcc_assert (!copy);
6382 /* A piece with correct bitpos and bitsize already exist,
6383 just update the location for it and return. */
6384 *decl_piece_varloc_ptr (*dest) = loc_note;
6385 return;
6387 /* Add the piece that changed. */
6388 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
6389 dest = &XEXP (*dest, 1);
6390 /* Skip over pieces that overlap it. */
6391 diff = bitpos - piece_bitpos + bitsize;
6392 if (!copy)
6393 src = dest;
6394 while (diff > 0 && *src)
6396 rtx piece = *src;
6397 diff -= decl_piece_bitsize (piece);
6398 if (copy)
6399 src = &XEXP (piece, 1);
6400 else
6402 *src = XEXP (piece, 1);
6403 free_EXPR_LIST_node (piece);
6406 /* Add padding if needed. */
6407 if (diff < 0 && *src)
6409 if (!copy)
6410 dest = src;
6411 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
6412 dest = &XEXP (*dest, 1);
6414 if (!copy)
6415 return;
6416 /* Finally copy all nodes following it. */
6417 while (*src)
6419 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
6420 decl_piece_bitsize (*src), NULL_RTX);
6421 dest = &XEXP (*dest, 1);
6422 src = &XEXP (*src, 1);
6426 /* Add a variable location node to the linked list for DECL. */
6428 static struct var_loc_node *
6429 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label, var_loc_view view)
6431 unsigned int decl_id;
6432 var_loc_list *temp;
6433 struct var_loc_node *loc = NULL;
6434 HOST_WIDE_INT bitsize = -1, bitpos = -1;
6436 if (VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
6438 tree realdecl = DECL_DEBUG_EXPR (decl);
6439 if (handled_component_p (realdecl)
6440 || (TREE_CODE (realdecl) == MEM_REF
6441 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
6443 bool reverse;
6444 tree innerdecl = get_ref_base_and_extent_hwi (realdecl, &bitpos,
6445 &bitsize, &reverse);
6446 if (!innerdecl
6447 || !DECL_P (innerdecl)
6448 || DECL_IGNORED_P (innerdecl)
6449 || TREE_STATIC (innerdecl)
6450 || bitsize == 0
6451 || bitpos + bitsize > 256)
6452 return NULL;
6453 decl = innerdecl;
6457 decl_id = DECL_UID (decl);
6458 var_loc_list **slot
6459 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
6460 if (*slot == NULL)
6462 temp = ggc_cleared_alloc<var_loc_list> ();
6463 temp->decl_id = decl_id;
6464 *slot = temp;
6466 else
6467 temp = *slot;
6469 /* For PARM_DECLs try to keep around the original incoming value,
6470 even if that means we'll emit a zero-range .debug_loc entry. */
6471 if (temp->last
6472 && temp->first == temp->last
6473 && TREE_CODE (decl) == PARM_DECL
6474 && NOTE_P (temp->first->loc)
6475 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
6476 && DECL_INCOMING_RTL (decl)
6477 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
6478 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
6479 == GET_CODE (DECL_INCOMING_RTL (decl))
6480 && prev_real_insn (as_a<rtx_insn *> (temp->first->loc)) == NULL_RTX
6481 && (bitsize != -1
6482 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
6483 NOTE_VAR_LOCATION_LOC (loc_note))
6484 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
6485 != NOTE_VAR_LOCATION_STATUS (loc_note))))
6487 loc = ggc_cleared_alloc<var_loc_node> ();
6488 temp->first->next = loc;
6489 temp->last = loc;
6490 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6492 else if (temp->last)
6494 struct var_loc_node *last = temp->last, *unused = NULL;
6495 rtx *piece_loc = NULL, last_loc_note;
6496 HOST_WIDE_INT piece_bitpos = 0;
6497 if (last->next)
6499 last = last->next;
6500 gcc_assert (last->next == NULL);
6502 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
6504 piece_loc = &last->loc;
6507 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
6508 if (piece_bitpos + cur_bitsize > bitpos)
6509 break;
6510 piece_bitpos += cur_bitsize;
6511 piece_loc = &XEXP (*piece_loc, 1);
6513 while (*piece_loc);
6515 /* TEMP->LAST here is either pointer to the last but one or
6516 last element in the chained list, LAST is pointer to the
6517 last element. */
6518 if (label && strcmp (last->label, label) == 0 && last->view == view)
6520 /* For SRA optimized variables if there weren't any real
6521 insns since last note, just modify the last node. */
6522 if (piece_loc != NULL)
6524 adjust_piece_list (piece_loc, NULL, NULL,
6525 bitpos, piece_bitpos, bitsize, loc_note);
6526 return NULL;
6528 /* If the last note doesn't cover any instructions, remove it. */
6529 if (temp->last != last)
6531 temp->last->next = NULL;
6532 unused = last;
6533 last = temp->last;
6534 gcc_assert (strcmp (last->label, label) != 0 || last->view != view);
6536 else
6538 gcc_assert (temp->first == temp->last
6539 || (temp->first->next == temp->last
6540 && TREE_CODE (decl) == PARM_DECL));
6541 memset (temp->last, '\0', sizeof (*temp->last));
6542 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
6543 return temp->last;
6546 if (bitsize == -1 && NOTE_P (last->loc))
6547 last_loc_note = last->loc;
6548 else if (piece_loc != NULL
6549 && *piece_loc != NULL_RTX
6550 && piece_bitpos == bitpos
6551 && decl_piece_bitsize (*piece_loc) == bitsize)
6552 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
6553 else
6554 last_loc_note = NULL_RTX;
6555 /* If the current location is the same as the end of the list,
6556 and either both or neither of the locations is uninitialized,
6557 we have nothing to do. */
6558 if (last_loc_note == NULL_RTX
6559 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
6560 NOTE_VAR_LOCATION_LOC (loc_note)))
6561 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6562 != NOTE_VAR_LOCATION_STATUS (loc_note))
6563 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6564 == VAR_INIT_STATUS_UNINITIALIZED)
6565 || (NOTE_VAR_LOCATION_STATUS (loc_note)
6566 == VAR_INIT_STATUS_UNINITIALIZED))))
6568 /* Add LOC to the end of list and update LAST. If the last
6569 element of the list has been removed above, reuse its
6570 memory for the new node, otherwise allocate a new one. */
6571 if (unused)
6573 loc = unused;
6574 memset (loc, '\0', sizeof (*loc));
6576 else
6577 loc = ggc_cleared_alloc<var_loc_node> ();
6578 if (bitsize == -1 || piece_loc == NULL)
6579 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6580 else
6581 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
6582 bitpos, piece_bitpos, bitsize, loc_note);
6583 last->next = loc;
6584 /* Ensure TEMP->LAST will point either to the new last but one
6585 element of the chain, or to the last element in it. */
6586 if (last != temp->last)
6587 temp->last = last;
6589 else if (unused)
6590 ggc_free (unused);
6592 else
6594 loc = ggc_cleared_alloc<var_loc_node> ();
6595 temp->first = loc;
6596 temp->last = loc;
6597 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6599 return loc;
6602 /* Keep track of the number of spaces used to indent the
6603 output of the debugging routines that print the structure of
6604 the DIE internal representation. */
6605 static int print_indent;
6607 /* Indent the line the number of spaces given by print_indent. */
6609 static inline void
6610 print_spaces (FILE *outfile)
6612 fprintf (outfile, "%*s", print_indent, "");
6615 /* Print a type signature in hex. */
6617 static inline void
6618 print_signature (FILE *outfile, char *sig)
6620 int i;
6622 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
6623 fprintf (outfile, "%02x", sig[i] & 0xff);
6626 static inline void
6627 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
6629 if (discr_value->pos)
6630 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
6631 else
6632 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
6635 static void print_loc_descr (dw_loc_descr_ref, FILE *);
6637 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
6638 RECURSE, output location descriptor operations. */
6640 static void
6641 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
6643 switch (val->val_class)
6645 case dw_val_class_addr:
6646 fprintf (outfile, "address");
6647 break;
6648 case dw_val_class_offset:
6649 fprintf (outfile, "offset");
6650 break;
6651 case dw_val_class_loc:
6652 fprintf (outfile, "location descriptor");
6653 if (val->v.val_loc == NULL)
6654 fprintf (outfile, " -> <null>\n");
6655 else if (recurse)
6657 fprintf (outfile, ":\n");
6658 print_indent += 4;
6659 print_loc_descr (val->v.val_loc, outfile);
6660 print_indent -= 4;
6662 else
6664 if (flag_dump_noaddr || flag_dump_unnumbered)
6665 fprintf (outfile, " #\n");
6666 else
6667 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
6669 break;
6670 case dw_val_class_loc_list:
6671 fprintf (outfile, "location list -> label:%s",
6672 val->v.val_loc_list->ll_symbol);
6673 break;
6674 case dw_val_class_view_list:
6675 val = view_list_to_loc_list_val_node (val);
6676 fprintf (outfile, "location list with views -> labels:%s and %s",
6677 val->v.val_loc_list->ll_symbol,
6678 val->v.val_loc_list->vl_symbol);
6679 break;
6680 case dw_val_class_range_list:
6681 fprintf (outfile, "range list");
6682 break;
6683 case dw_val_class_const:
6684 case dw_val_class_const_implicit:
6685 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
6686 break;
6687 case dw_val_class_unsigned_const:
6688 case dw_val_class_unsigned_const_implicit:
6689 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
6690 break;
6691 case dw_val_class_const_double:
6692 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
6693 HOST_WIDE_INT_PRINT_UNSIGNED")",
6694 val->v.val_double.high,
6695 val->v.val_double.low);
6696 break;
6697 case dw_val_class_wide_int:
6699 int i = val->v.val_wide->get_len ();
6700 fprintf (outfile, "constant (");
6701 gcc_assert (i > 0);
6702 if (val->v.val_wide->elt (i - 1) == 0)
6703 fprintf (outfile, "0x");
6704 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
6705 val->v.val_wide->elt (--i));
6706 while (--i >= 0)
6707 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
6708 val->v.val_wide->elt (i));
6709 fprintf (outfile, ")");
6710 break;
6712 case dw_val_class_vec:
6713 fprintf (outfile, "floating-point or vector constant");
6714 break;
6715 case dw_val_class_flag:
6716 fprintf (outfile, "%u", val->v.val_flag);
6717 break;
6718 case dw_val_class_die_ref:
6719 if (val->v.val_die_ref.die != NULL)
6721 dw_die_ref die = val->v.val_die_ref.die;
6723 if (die->comdat_type_p)
6725 fprintf (outfile, "die -> signature: ");
6726 print_signature (outfile,
6727 die->die_id.die_type_node->signature);
6729 else if (die->die_id.die_symbol)
6731 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
6732 if (die->with_offset)
6733 fprintf (outfile, " + %ld", die->die_offset);
6735 else
6736 fprintf (outfile, "die -> %ld", die->die_offset);
6737 if (flag_dump_noaddr || flag_dump_unnumbered)
6738 fprintf (outfile, " #");
6739 else
6740 fprintf (outfile, " (%p)", (void *) die);
6742 else
6743 fprintf (outfile, "die -> <null>");
6744 break;
6745 case dw_val_class_vms_delta:
6746 fprintf (outfile, "delta: @slotcount(%s-%s)",
6747 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
6748 break;
6749 case dw_val_class_symview:
6750 fprintf (outfile, "view: %s", val->v.val_symbolic_view);
6751 break;
6752 case dw_val_class_lbl_id:
6753 case dw_val_class_lineptr:
6754 case dw_val_class_macptr:
6755 case dw_val_class_loclistsptr:
6756 case dw_val_class_high_pc:
6757 fprintf (outfile, "label: %s", val->v.val_lbl_id);
6758 break;
6759 case dw_val_class_str:
6760 if (val->v.val_str->str != NULL)
6761 fprintf (outfile, "\"%s\"", val->v.val_str->str);
6762 else
6763 fprintf (outfile, "<null>");
6764 break;
6765 case dw_val_class_file:
6766 case dw_val_class_file_implicit:
6767 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
6768 val->v.val_file->emitted_number);
6769 break;
6770 case dw_val_class_data8:
6772 int i;
6774 for (i = 0; i < 8; i++)
6775 fprintf (outfile, "%02x", val->v.val_data8[i]);
6776 break;
6778 case dw_val_class_discr_value:
6779 print_discr_value (outfile, &val->v.val_discr_value);
6780 break;
6781 case dw_val_class_discr_list:
6782 for (dw_discr_list_ref node = val->v.val_discr_list;
6783 node != NULL;
6784 node = node->dw_discr_next)
6786 if (node->dw_discr_range)
6788 fprintf (outfile, " .. ");
6789 print_discr_value (outfile, &node->dw_discr_lower_bound);
6790 print_discr_value (outfile, &node->dw_discr_upper_bound);
6792 else
6793 print_discr_value (outfile, &node->dw_discr_lower_bound);
6795 if (node->dw_discr_next != NULL)
6796 fprintf (outfile, " | ");
6798 default:
6799 break;
6803 /* Likewise, for a DIE attribute. */
6805 static void
6806 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
6808 print_dw_val (&a->dw_attr_val, recurse, outfile);
6812 /* Print the list of operands in the LOC location description to OUTFILE. This
6813 routine is a debugging aid only. */
6815 static void
6816 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
6818 dw_loc_descr_ref l = loc;
6820 if (loc == NULL)
6822 print_spaces (outfile);
6823 fprintf (outfile, "<null>\n");
6824 return;
6827 for (l = loc; l != NULL; l = l->dw_loc_next)
6829 print_spaces (outfile);
6830 if (flag_dump_noaddr || flag_dump_unnumbered)
6831 fprintf (outfile, "#");
6832 else
6833 fprintf (outfile, "(%p)", (void *) l);
6834 fprintf (outfile, " %s",
6835 dwarf_stack_op_name (l->dw_loc_opc));
6836 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
6838 fprintf (outfile, " ");
6839 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
6841 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
6843 fprintf (outfile, ", ");
6844 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
6846 fprintf (outfile, "\n");
6850 /* Print the information associated with a given DIE, and its children.
6851 This routine is a debugging aid only. */
6853 static void
6854 print_die (dw_die_ref die, FILE *outfile)
6856 dw_attr_node *a;
6857 dw_die_ref c;
6858 unsigned ix;
6860 print_spaces (outfile);
6861 fprintf (outfile, "DIE %4ld: %s ",
6862 die->die_offset, dwarf_tag_name (die->die_tag));
6863 if (flag_dump_noaddr || flag_dump_unnumbered)
6864 fprintf (outfile, "#\n");
6865 else
6866 fprintf (outfile, "(%p)\n", (void*) die);
6867 print_spaces (outfile);
6868 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
6869 fprintf (outfile, " offset: %ld", die->die_offset);
6870 fprintf (outfile, " mark: %d\n", die->die_mark);
6872 if (die->comdat_type_p)
6874 print_spaces (outfile);
6875 fprintf (outfile, " signature: ");
6876 print_signature (outfile, die->die_id.die_type_node->signature);
6877 fprintf (outfile, "\n");
6880 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6882 print_spaces (outfile);
6883 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
6885 print_attribute (a, true, outfile);
6886 fprintf (outfile, "\n");
6889 if (die->die_child != NULL)
6891 print_indent += 4;
6892 FOR_EACH_CHILD (die, c, print_die (c, outfile));
6893 print_indent -= 4;
6895 if (print_indent == 0)
6896 fprintf (outfile, "\n");
6899 /* Print the list of operations in the LOC location description. */
6901 DEBUG_FUNCTION void
6902 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
6904 print_loc_descr (loc, stderr);
6907 /* Print the information collected for a given DIE. */
6909 DEBUG_FUNCTION void
6910 debug_dwarf_die (dw_die_ref die)
6912 print_die (die, stderr);
6915 DEBUG_FUNCTION void
6916 debug (die_struct &ref)
6918 print_die (&ref, stderr);
6921 DEBUG_FUNCTION void
6922 debug (die_struct *ptr)
6924 if (ptr)
6925 debug (*ptr);
6926 else
6927 fprintf (stderr, "<nil>\n");
6931 /* Print all DWARF information collected for the compilation unit.
6932 This routine is a debugging aid only. */
6934 DEBUG_FUNCTION void
6935 debug_dwarf (void)
6937 print_indent = 0;
6938 print_die (comp_unit_die (), stderr);
6941 /* Verify the DIE tree structure. */
6943 DEBUG_FUNCTION void
6944 verify_die (dw_die_ref die)
6946 gcc_assert (!die->die_mark);
6947 if (die->die_parent == NULL
6948 && die->die_sib == NULL)
6949 return;
6950 /* Verify the die_sib list is cyclic. */
6951 dw_die_ref x = die;
6954 x->die_mark = 1;
6955 x = x->die_sib;
6957 while (x && !x->die_mark);
6958 gcc_assert (x == die);
6959 x = die;
6962 /* Verify all dies have the same parent. */
6963 gcc_assert (x->die_parent == die->die_parent);
6964 if (x->die_child)
6966 /* Verify the child has the proper parent and recurse. */
6967 gcc_assert (x->die_child->die_parent == x);
6968 verify_die (x->die_child);
6970 x->die_mark = 0;
6971 x = x->die_sib;
6973 while (x && x->die_mark);
6976 /* Sanity checks on DIEs. */
6978 static void
6979 check_die (dw_die_ref die)
6981 unsigned ix;
6982 dw_attr_node *a;
6983 bool inline_found = false;
6984 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
6985 int n_decl_line = 0, n_decl_column = 0, n_decl_file = 0;
6986 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6988 switch (a->dw_attr)
6990 case DW_AT_inline:
6991 if (a->dw_attr_val.v.val_unsigned)
6992 inline_found = true;
6993 break;
6994 case DW_AT_location:
6995 ++n_location;
6996 break;
6997 case DW_AT_low_pc:
6998 ++n_low_pc;
6999 break;
7000 case DW_AT_high_pc:
7001 ++n_high_pc;
7002 break;
7003 case DW_AT_artificial:
7004 ++n_artificial;
7005 break;
7006 case DW_AT_decl_column:
7007 ++n_decl_column;
7008 break;
7009 case DW_AT_decl_line:
7010 ++n_decl_line;
7011 break;
7012 case DW_AT_decl_file:
7013 ++n_decl_file;
7014 break;
7015 default:
7016 break;
7019 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
7020 || n_decl_column > 1 || n_decl_line > 1 || n_decl_file > 1)
7022 fprintf (stderr, "Duplicate attributes in DIE:\n");
7023 debug_dwarf_die (die);
7024 gcc_unreachable ();
7026 if (inline_found)
7028 /* A debugging information entry that is a member of an abstract
7029 instance tree [that has DW_AT_inline] should not contain any
7030 attributes which describe aspects of the subroutine which vary
7031 between distinct inlined expansions or distinct out-of-line
7032 expansions. */
7033 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7034 gcc_assert (a->dw_attr != DW_AT_low_pc
7035 && a->dw_attr != DW_AT_high_pc
7036 && a->dw_attr != DW_AT_location
7037 && a->dw_attr != DW_AT_frame_base
7038 && a->dw_attr != DW_AT_call_all_calls
7039 && a->dw_attr != DW_AT_GNU_all_call_sites);
7043 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
7044 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
7045 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
7047 /* Calculate the checksum of a location expression. */
7049 static inline void
7050 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
7052 int tem;
7053 inchash::hash hstate;
7054 hashval_t hash;
7056 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
7057 CHECKSUM (tem);
7058 hash_loc_operands (loc, hstate);
7059 hash = hstate.end();
7060 CHECKSUM (hash);
7063 /* Calculate the checksum of an attribute. */
7065 static void
7066 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
7068 dw_loc_descr_ref loc;
7069 rtx r;
7071 CHECKSUM (at->dw_attr);
7073 /* We don't care that this was compiled with a different compiler
7074 snapshot; if the output is the same, that's what matters. */
7075 if (at->dw_attr == DW_AT_producer)
7076 return;
7078 switch (AT_class (at))
7080 case dw_val_class_const:
7081 case dw_val_class_const_implicit:
7082 CHECKSUM (at->dw_attr_val.v.val_int);
7083 break;
7084 case dw_val_class_unsigned_const:
7085 case dw_val_class_unsigned_const_implicit:
7086 CHECKSUM (at->dw_attr_val.v.val_unsigned);
7087 break;
7088 case dw_val_class_const_double:
7089 CHECKSUM (at->dw_attr_val.v.val_double);
7090 break;
7091 case dw_val_class_wide_int:
7092 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
7093 get_full_len (*at->dw_attr_val.v.val_wide)
7094 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
7095 break;
7096 case dw_val_class_vec:
7097 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
7098 (at->dw_attr_val.v.val_vec.length
7099 * at->dw_attr_val.v.val_vec.elt_size));
7100 break;
7101 case dw_val_class_flag:
7102 CHECKSUM (at->dw_attr_val.v.val_flag);
7103 break;
7104 case dw_val_class_str:
7105 CHECKSUM_STRING (AT_string (at));
7106 break;
7108 case dw_val_class_addr:
7109 r = AT_addr (at);
7110 gcc_assert (GET_CODE (r) == SYMBOL_REF);
7111 CHECKSUM_STRING (XSTR (r, 0));
7112 break;
7114 case dw_val_class_offset:
7115 CHECKSUM (at->dw_attr_val.v.val_offset);
7116 break;
7118 case dw_val_class_loc:
7119 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
7120 loc_checksum (loc, ctx);
7121 break;
7123 case dw_val_class_die_ref:
7124 die_checksum (AT_ref (at), ctx, mark);
7125 break;
7127 case dw_val_class_fde_ref:
7128 case dw_val_class_vms_delta:
7129 case dw_val_class_symview:
7130 case dw_val_class_lbl_id:
7131 case dw_val_class_lineptr:
7132 case dw_val_class_macptr:
7133 case dw_val_class_loclistsptr:
7134 case dw_val_class_high_pc:
7135 break;
7137 case dw_val_class_file:
7138 case dw_val_class_file_implicit:
7139 CHECKSUM_STRING (AT_file (at)->filename);
7140 break;
7142 case dw_val_class_data8:
7143 CHECKSUM (at->dw_attr_val.v.val_data8);
7144 break;
7146 default:
7147 break;
7151 /* Calculate the checksum of a DIE. */
7153 static void
7154 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7156 dw_die_ref c;
7157 dw_attr_node *a;
7158 unsigned ix;
7160 /* To avoid infinite recursion. */
7161 if (die->die_mark)
7163 CHECKSUM (die->die_mark);
7164 return;
7166 die->die_mark = ++(*mark);
7168 CHECKSUM (die->die_tag);
7170 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7171 attr_checksum (a, ctx, mark);
7173 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
7176 #undef CHECKSUM
7177 #undef CHECKSUM_BLOCK
7178 #undef CHECKSUM_STRING
7180 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
7181 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
7182 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
7183 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
7184 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
7185 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
7186 #define CHECKSUM_ATTR(FOO) \
7187 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
7189 /* Calculate the checksum of a number in signed LEB128 format. */
7191 static void
7192 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
7194 unsigned char byte;
7195 bool more;
7197 while (1)
7199 byte = (value & 0x7f);
7200 value >>= 7;
7201 more = !((value == 0 && (byte & 0x40) == 0)
7202 || (value == -1 && (byte & 0x40) != 0));
7203 if (more)
7204 byte |= 0x80;
7205 CHECKSUM (byte);
7206 if (!more)
7207 break;
7211 /* Calculate the checksum of a number in unsigned LEB128 format. */
7213 static void
7214 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
7216 while (1)
7218 unsigned char byte = (value & 0x7f);
7219 value >>= 7;
7220 if (value != 0)
7221 /* More bytes to follow. */
7222 byte |= 0x80;
7223 CHECKSUM (byte);
7224 if (value == 0)
7225 break;
7229 /* Checksum the context of the DIE. This adds the names of any
7230 surrounding namespaces or structures to the checksum. */
7232 static void
7233 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
7235 const char *name;
7236 dw_die_ref spec;
7237 int tag = die->die_tag;
7239 if (tag != DW_TAG_namespace
7240 && tag != DW_TAG_structure_type
7241 && tag != DW_TAG_class_type)
7242 return;
7244 name = get_AT_string (die, DW_AT_name);
7246 spec = get_AT_ref (die, DW_AT_specification);
7247 if (spec != NULL)
7248 die = spec;
7250 if (die->die_parent != NULL)
7251 checksum_die_context (die->die_parent, ctx);
7253 CHECKSUM_ULEB128 ('C');
7254 CHECKSUM_ULEB128 (tag);
7255 if (name != NULL)
7256 CHECKSUM_STRING (name);
7259 /* Calculate the checksum of a location expression. */
7261 static inline void
7262 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
7264 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
7265 were emitted as a DW_FORM_sdata instead of a location expression. */
7266 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
7268 CHECKSUM_ULEB128 (DW_FORM_sdata);
7269 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
7270 return;
7273 /* Otherwise, just checksum the raw location expression. */
7274 while (loc != NULL)
7276 inchash::hash hstate;
7277 hashval_t hash;
7279 CHECKSUM_ULEB128 (loc->dtprel);
7280 CHECKSUM_ULEB128 (loc->dw_loc_opc);
7281 hash_loc_operands (loc, hstate);
7282 hash = hstate.end ();
7283 CHECKSUM (hash);
7284 loc = loc->dw_loc_next;
7288 /* Calculate the checksum of an attribute. */
7290 static void
7291 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
7292 struct md5_ctx *ctx, int *mark)
7294 dw_loc_descr_ref loc;
7295 rtx r;
7297 if (AT_class (at) == dw_val_class_die_ref)
7299 dw_die_ref target_die = AT_ref (at);
7301 /* For pointer and reference types, we checksum only the (qualified)
7302 name of the target type (if there is a name). For friend entries,
7303 we checksum only the (qualified) name of the target type or function.
7304 This allows the checksum to remain the same whether the target type
7305 is complete or not. */
7306 if ((at->dw_attr == DW_AT_type
7307 && (tag == DW_TAG_pointer_type
7308 || tag == DW_TAG_reference_type
7309 || tag == DW_TAG_rvalue_reference_type
7310 || tag == DW_TAG_ptr_to_member_type))
7311 || (at->dw_attr == DW_AT_friend
7312 && tag == DW_TAG_friend))
7314 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
7316 if (name_attr != NULL)
7318 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
7320 if (decl == NULL)
7321 decl = target_die;
7322 CHECKSUM_ULEB128 ('N');
7323 CHECKSUM_ULEB128 (at->dw_attr);
7324 if (decl->die_parent != NULL)
7325 checksum_die_context (decl->die_parent, ctx);
7326 CHECKSUM_ULEB128 ('E');
7327 CHECKSUM_STRING (AT_string (name_attr));
7328 return;
7332 /* For all other references to another DIE, we check to see if the
7333 target DIE has already been visited. If it has, we emit a
7334 backward reference; if not, we descend recursively. */
7335 if (target_die->die_mark > 0)
7337 CHECKSUM_ULEB128 ('R');
7338 CHECKSUM_ULEB128 (at->dw_attr);
7339 CHECKSUM_ULEB128 (target_die->die_mark);
7341 else
7343 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
7345 if (decl == NULL)
7346 decl = target_die;
7347 target_die->die_mark = ++(*mark);
7348 CHECKSUM_ULEB128 ('T');
7349 CHECKSUM_ULEB128 (at->dw_attr);
7350 if (decl->die_parent != NULL)
7351 checksum_die_context (decl->die_parent, ctx);
7352 die_checksum_ordered (target_die, ctx, mark);
7354 return;
7357 CHECKSUM_ULEB128 ('A');
7358 CHECKSUM_ULEB128 (at->dw_attr);
7360 switch (AT_class (at))
7362 case dw_val_class_const:
7363 case dw_val_class_const_implicit:
7364 CHECKSUM_ULEB128 (DW_FORM_sdata);
7365 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
7366 break;
7368 case dw_val_class_unsigned_const:
7369 case dw_val_class_unsigned_const_implicit:
7370 CHECKSUM_ULEB128 (DW_FORM_sdata);
7371 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
7372 break;
7374 case dw_val_class_const_double:
7375 CHECKSUM_ULEB128 (DW_FORM_block);
7376 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
7377 CHECKSUM (at->dw_attr_val.v.val_double);
7378 break;
7380 case dw_val_class_wide_int:
7381 CHECKSUM_ULEB128 (DW_FORM_block);
7382 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
7383 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
7384 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
7385 get_full_len (*at->dw_attr_val.v.val_wide)
7386 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
7387 break;
7389 case dw_val_class_vec:
7390 CHECKSUM_ULEB128 (DW_FORM_block);
7391 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
7392 * at->dw_attr_val.v.val_vec.elt_size);
7393 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
7394 (at->dw_attr_val.v.val_vec.length
7395 * at->dw_attr_val.v.val_vec.elt_size));
7396 break;
7398 case dw_val_class_flag:
7399 CHECKSUM_ULEB128 (DW_FORM_flag);
7400 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
7401 break;
7403 case dw_val_class_str:
7404 CHECKSUM_ULEB128 (DW_FORM_string);
7405 CHECKSUM_STRING (AT_string (at));
7406 break;
7408 case dw_val_class_addr:
7409 r = AT_addr (at);
7410 gcc_assert (GET_CODE (r) == SYMBOL_REF);
7411 CHECKSUM_ULEB128 (DW_FORM_string);
7412 CHECKSUM_STRING (XSTR (r, 0));
7413 break;
7415 case dw_val_class_offset:
7416 CHECKSUM_ULEB128 (DW_FORM_sdata);
7417 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
7418 break;
7420 case dw_val_class_loc:
7421 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
7422 loc_checksum_ordered (loc, ctx);
7423 break;
7425 case dw_val_class_fde_ref:
7426 case dw_val_class_symview:
7427 case dw_val_class_lbl_id:
7428 case dw_val_class_lineptr:
7429 case dw_val_class_macptr:
7430 case dw_val_class_loclistsptr:
7431 case dw_val_class_high_pc:
7432 break;
7434 case dw_val_class_file:
7435 case dw_val_class_file_implicit:
7436 CHECKSUM_ULEB128 (DW_FORM_string);
7437 CHECKSUM_STRING (AT_file (at)->filename);
7438 break;
7440 case dw_val_class_data8:
7441 CHECKSUM (at->dw_attr_val.v.val_data8);
7442 break;
7444 default:
7445 break;
7449 struct checksum_attributes
7451 dw_attr_node *at_name;
7452 dw_attr_node *at_type;
7453 dw_attr_node *at_friend;
7454 dw_attr_node *at_accessibility;
7455 dw_attr_node *at_address_class;
7456 dw_attr_node *at_alignment;
7457 dw_attr_node *at_allocated;
7458 dw_attr_node *at_artificial;
7459 dw_attr_node *at_associated;
7460 dw_attr_node *at_binary_scale;
7461 dw_attr_node *at_bit_offset;
7462 dw_attr_node *at_bit_size;
7463 dw_attr_node *at_bit_stride;
7464 dw_attr_node *at_byte_size;
7465 dw_attr_node *at_byte_stride;
7466 dw_attr_node *at_const_value;
7467 dw_attr_node *at_containing_type;
7468 dw_attr_node *at_count;
7469 dw_attr_node *at_data_location;
7470 dw_attr_node *at_data_member_location;
7471 dw_attr_node *at_decimal_scale;
7472 dw_attr_node *at_decimal_sign;
7473 dw_attr_node *at_default_value;
7474 dw_attr_node *at_digit_count;
7475 dw_attr_node *at_discr;
7476 dw_attr_node *at_discr_list;
7477 dw_attr_node *at_discr_value;
7478 dw_attr_node *at_encoding;
7479 dw_attr_node *at_endianity;
7480 dw_attr_node *at_explicit;
7481 dw_attr_node *at_is_optional;
7482 dw_attr_node *at_location;
7483 dw_attr_node *at_lower_bound;
7484 dw_attr_node *at_mutable;
7485 dw_attr_node *at_ordering;
7486 dw_attr_node *at_picture_string;
7487 dw_attr_node *at_prototyped;
7488 dw_attr_node *at_small;
7489 dw_attr_node *at_segment;
7490 dw_attr_node *at_string_length;
7491 dw_attr_node *at_string_length_bit_size;
7492 dw_attr_node *at_string_length_byte_size;
7493 dw_attr_node *at_threads_scaled;
7494 dw_attr_node *at_upper_bound;
7495 dw_attr_node *at_use_location;
7496 dw_attr_node *at_use_UTF8;
7497 dw_attr_node *at_variable_parameter;
7498 dw_attr_node *at_virtuality;
7499 dw_attr_node *at_visibility;
7500 dw_attr_node *at_vtable_elem_location;
7503 /* Collect the attributes that we will want to use for the checksum. */
7505 static void
7506 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
7508 dw_attr_node *a;
7509 unsigned ix;
7511 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7513 switch (a->dw_attr)
7515 case DW_AT_name:
7516 attrs->at_name = a;
7517 break;
7518 case DW_AT_type:
7519 attrs->at_type = a;
7520 break;
7521 case DW_AT_friend:
7522 attrs->at_friend = a;
7523 break;
7524 case DW_AT_accessibility:
7525 attrs->at_accessibility = a;
7526 break;
7527 case DW_AT_address_class:
7528 attrs->at_address_class = a;
7529 break;
7530 case DW_AT_alignment:
7531 attrs->at_alignment = a;
7532 break;
7533 case DW_AT_allocated:
7534 attrs->at_allocated = a;
7535 break;
7536 case DW_AT_artificial:
7537 attrs->at_artificial = a;
7538 break;
7539 case DW_AT_associated:
7540 attrs->at_associated = a;
7541 break;
7542 case DW_AT_binary_scale:
7543 attrs->at_binary_scale = a;
7544 break;
7545 case DW_AT_bit_offset:
7546 attrs->at_bit_offset = a;
7547 break;
7548 case DW_AT_bit_size:
7549 attrs->at_bit_size = a;
7550 break;
7551 case DW_AT_bit_stride:
7552 attrs->at_bit_stride = a;
7553 break;
7554 case DW_AT_byte_size:
7555 attrs->at_byte_size = a;
7556 break;
7557 case DW_AT_byte_stride:
7558 attrs->at_byte_stride = a;
7559 break;
7560 case DW_AT_const_value:
7561 attrs->at_const_value = a;
7562 break;
7563 case DW_AT_containing_type:
7564 attrs->at_containing_type = a;
7565 break;
7566 case DW_AT_count:
7567 attrs->at_count = a;
7568 break;
7569 case DW_AT_data_location:
7570 attrs->at_data_location = a;
7571 break;
7572 case DW_AT_data_member_location:
7573 attrs->at_data_member_location = a;
7574 break;
7575 case DW_AT_decimal_scale:
7576 attrs->at_decimal_scale = a;
7577 break;
7578 case DW_AT_decimal_sign:
7579 attrs->at_decimal_sign = a;
7580 break;
7581 case DW_AT_default_value:
7582 attrs->at_default_value = a;
7583 break;
7584 case DW_AT_digit_count:
7585 attrs->at_digit_count = a;
7586 break;
7587 case DW_AT_discr:
7588 attrs->at_discr = a;
7589 break;
7590 case DW_AT_discr_list:
7591 attrs->at_discr_list = a;
7592 break;
7593 case DW_AT_discr_value:
7594 attrs->at_discr_value = a;
7595 break;
7596 case DW_AT_encoding:
7597 attrs->at_encoding = a;
7598 break;
7599 case DW_AT_endianity:
7600 attrs->at_endianity = a;
7601 break;
7602 case DW_AT_explicit:
7603 attrs->at_explicit = a;
7604 break;
7605 case DW_AT_is_optional:
7606 attrs->at_is_optional = a;
7607 break;
7608 case DW_AT_location:
7609 attrs->at_location = a;
7610 break;
7611 case DW_AT_lower_bound:
7612 attrs->at_lower_bound = a;
7613 break;
7614 case DW_AT_mutable:
7615 attrs->at_mutable = a;
7616 break;
7617 case DW_AT_ordering:
7618 attrs->at_ordering = a;
7619 break;
7620 case DW_AT_picture_string:
7621 attrs->at_picture_string = a;
7622 break;
7623 case DW_AT_prototyped:
7624 attrs->at_prototyped = a;
7625 break;
7626 case DW_AT_small:
7627 attrs->at_small = a;
7628 break;
7629 case DW_AT_segment:
7630 attrs->at_segment = a;
7631 break;
7632 case DW_AT_string_length:
7633 attrs->at_string_length = a;
7634 break;
7635 case DW_AT_string_length_bit_size:
7636 attrs->at_string_length_bit_size = a;
7637 break;
7638 case DW_AT_string_length_byte_size:
7639 attrs->at_string_length_byte_size = a;
7640 break;
7641 case DW_AT_threads_scaled:
7642 attrs->at_threads_scaled = a;
7643 break;
7644 case DW_AT_upper_bound:
7645 attrs->at_upper_bound = a;
7646 break;
7647 case DW_AT_use_location:
7648 attrs->at_use_location = a;
7649 break;
7650 case DW_AT_use_UTF8:
7651 attrs->at_use_UTF8 = a;
7652 break;
7653 case DW_AT_variable_parameter:
7654 attrs->at_variable_parameter = a;
7655 break;
7656 case DW_AT_virtuality:
7657 attrs->at_virtuality = a;
7658 break;
7659 case DW_AT_visibility:
7660 attrs->at_visibility = a;
7661 break;
7662 case DW_AT_vtable_elem_location:
7663 attrs->at_vtable_elem_location = a;
7664 break;
7665 default:
7666 break;
7671 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
7673 static void
7674 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7676 dw_die_ref c;
7677 dw_die_ref decl;
7678 struct checksum_attributes attrs;
7680 CHECKSUM_ULEB128 ('D');
7681 CHECKSUM_ULEB128 (die->die_tag);
7683 memset (&attrs, 0, sizeof (attrs));
7685 decl = get_AT_ref (die, DW_AT_specification);
7686 if (decl != NULL)
7687 collect_checksum_attributes (&attrs, decl);
7688 collect_checksum_attributes (&attrs, die);
7690 CHECKSUM_ATTR (attrs.at_name);
7691 CHECKSUM_ATTR (attrs.at_accessibility);
7692 CHECKSUM_ATTR (attrs.at_address_class);
7693 CHECKSUM_ATTR (attrs.at_allocated);
7694 CHECKSUM_ATTR (attrs.at_artificial);
7695 CHECKSUM_ATTR (attrs.at_associated);
7696 CHECKSUM_ATTR (attrs.at_binary_scale);
7697 CHECKSUM_ATTR (attrs.at_bit_offset);
7698 CHECKSUM_ATTR (attrs.at_bit_size);
7699 CHECKSUM_ATTR (attrs.at_bit_stride);
7700 CHECKSUM_ATTR (attrs.at_byte_size);
7701 CHECKSUM_ATTR (attrs.at_byte_stride);
7702 CHECKSUM_ATTR (attrs.at_const_value);
7703 CHECKSUM_ATTR (attrs.at_containing_type);
7704 CHECKSUM_ATTR (attrs.at_count);
7705 CHECKSUM_ATTR (attrs.at_data_location);
7706 CHECKSUM_ATTR (attrs.at_data_member_location);
7707 CHECKSUM_ATTR (attrs.at_decimal_scale);
7708 CHECKSUM_ATTR (attrs.at_decimal_sign);
7709 CHECKSUM_ATTR (attrs.at_default_value);
7710 CHECKSUM_ATTR (attrs.at_digit_count);
7711 CHECKSUM_ATTR (attrs.at_discr);
7712 CHECKSUM_ATTR (attrs.at_discr_list);
7713 CHECKSUM_ATTR (attrs.at_discr_value);
7714 CHECKSUM_ATTR (attrs.at_encoding);
7715 CHECKSUM_ATTR (attrs.at_endianity);
7716 CHECKSUM_ATTR (attrs.at_explicit);
7717 CHECKSUM_ATTR (attrs.at_is_optional);
7718 CHECKSUM_ATTR (attrs.at_location);
7719 CHECKSUM_ATTR (attrs.at_lower_bound);
7720 CHECKSUM_ATTR (attrs.at_mutable);
7721 CHECKSUM_ATTR (attrs.at_ordering);
7722 CHECKSUM_ATTR (attrs.at_picture_string);
7723 CHECKSUM_ATTR (attrs.at_prototyped);
7724 CHECKSUM_ATTR (attrs.at_small);
7725 CHECKSUM_ATTR (attrs.at_segment);
7726 CHECKSUM_ATTR (attrs.at_string_length);
7727 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
7728 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
7729 CHECKSUM_ATTR (attrs.at_threads_scaled);
7730 CHECKSUM_ATTR (attrs.at_upper_bound);
7731 CHECKSUM_ATTR (attrs.at_use_location);
7732 CHECKSUM_ATTR (attrs.at_use_UTF8);
7733 CHECKSUM_ATTR (attrs.at_variable_parameter);
7734 CHECKSUM_ATTR (attrs.at_virtuality);
7735 CHECKSUM_ATTR (attrs.at_visibility);
7736 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
7737 CHECKSUM_ATTR (attrs.at_type);
7738 CHECKSUM_ATTR (attrs.at_friend);
7739 CHECKSUM_ATTR (attrs.at_alignment);
7741 /* Checksum the child DIEs. */
7742 c = die->die_child;
7743 if (c) do {
7744 dw_attr_node *name_attr;
7746 c = c->die_sib;
7747 name_attr = get_AT (c, DW_AT_name);
7748 if (is_template_instantiation (c))
7750 /* Ignore instantiations of member type and function templates. */
7752 else if (name_attr != NULL
7753 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
7755 /* Use a shallow checksum for named nested types and member
7756 functions. */
7757 CHECKSUM_ULEB128 ('S');
7758 CHECKSUM_ULEB128 (c->die_tag);
7759 CHECKSUM_STRING (AT_string (name_attr));
7761 else
7763 /* Use a deep checksum for other children. */
7764 /* Mark this DIE so it gets processed when unmarking. */
7765 if (c->die_mark == 0)
7766 c->die_mark = -1;
7767 die_checksum_ordered (c, ctx, mark);
7769 } while (c != die->die_child);
7771 CHECKSUM_ULEB128 (0);
7774 /* Add a type name and tag to a hash. */
7775 static void
7776 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
7778 CHECKSUM_ULEB128 (tag);
7779 CHECKSUM_STRING (name);
7782 #undef CHECKSUM
7783 #undef CHECKSUM_STRING
7784 #undef CHECKSUM_ATTR
7785 #undef CHECKSUM_LEB128
7786 #undef CHECKSUM_ULEB128
7788 /* Generate the type signature for DIE. This is computed by generating an
7789 MD5 checksum over the DIE's tag, its relevant attributes, and its
7790 children. Attributes that are references to other DIEs are processed
7791 by recursion, using the MARK field to prevent infinite recursion.
7792 If the DIE is nested inside a namespace or another type, we also
7793 need to include that context in the signature. The lower 64 bits
7794 of the resulting MD5 checksum comprise the signature. */
7796 static void
7797 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
7799 int mark;
7800 const char *name;
7801 unsigned char checksum[16];
7802 struct md5_ctx ctx;
7803 dw_die_ref decl;
7804 dw_die_ref parent;
7806 name = get_AT_string (die, DW_AT_name);
7807 decl = get_AT_ref (die, DW_AT_specification);
7808 parent = get_die_parent (die);
7810 /* First, compute a signature for just the type name (and its surrounding
7811 context, if any. This is stored in the type unit DIE for link-time
7812 ODR (one-definition rule) checking. */
7814 if (is_cxx () && name != NULL)
7816 md5_init_ctx (&ctx);
7818 /* Checksum the names of surrounding namespaces and structures. */
7819 if (parent != NULL)
7820 checksum_die_context (parent, &ctx);
7822 /* Checksum the current DIE. */
7823 die_odr_checksum (die->die_tag, name, &ctx);
7824 md5_finish_ctx (&ctx, checksum);
7826 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
7829 /* Next, compute the complete type signature. */
7831 md5_init_ctx (&ctx);
7832 mark = 1;
7833 die->die_mark = mark;
7835 /* Checksum the names of surrounding namespaces and structures. */
7836 if (parent != NULL)
7837 checksum_die_context (parent, &ctx);
7839 /* Checksum the DIE and its children. */
7840 die_checksum_ordered (die, &ctx, &mark);
7841 unmark_all_dies (die);
7842 md5_finish_ctx (&ctx, checksum);
7844 /* Store the signature in the type node and link the type DIE and the
7845 type node together. */
7846 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
7847 DWARF_TYPE_SIGNATURE_SIZE);
7848 die->comdat_type_p = true;
7849 die->die_id.die_type_node = type_node;
7850 type_node->type_die = die;
7852 /* If the DIE is a specification, link its declaration to the type node
7853 as well. */
7854 if (decl != NULL)
7856 decl->comdat_type_p = true;
7857 decl->die_id.die_type_node = type_node;
7861 /* Do the location expressions look same? */
7862 static inline bool
7863 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7865 return loc1->dw_loc_opc == loc2->dw_loc_opc
7866 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7867 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7870 /* Do the values look the same? */
7871 static bool
7872 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7874 dw_loc_descr_ref loc1, loc2;
7875 rtx r1, r2;
7877 if (v1->val_class != v2->val_class)
7878 return false;
7880 switch (v1->val_class)
7882 case dw_val_class_const:
7883 case dw_val_class_const_implicit:
7884 return v1->v.val_int == v2->v.val_int;
7885 case dw_val_class_unsigned_const:
7886 case dw_val_class_unsigned_const_implicit:
7887 return v1->v.val_unsigned == v2->v.val_unsigned;
7888 case dw_val_class_const_double:
7889 return v1->v.val_double.high == v2->v.val_double.high
7890 && v1->v.val_double.low == v2->v.val_double.low;
7891 case dw_val_class_wide_int:
7892 return *v1->v.val_wide == *v2->v.val_wide;
7893 case dw_val_class_vec:
7894 if (v1->v.val_vec.length != v2->v.val_vec.length
7895 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7896 return false;
7897 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7898 v1->v.val_vec.length * v1->v.val_vec.elt_size))
7899 return false;
7900 return true;
7901 case dw_val_class_flag:
7902 return v1->v.val_flag == v2->v.val_flag;
7903 case dw_val_class_str:
7904 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
7906 case dw_val_class_addr:
7907 r1 = v1->v.val_addr;
7908 r2 = v2->v.val_addr;
7909 if (GET_CODE (r1) != GET_CODE (r2))
7910 return false;
7911 return !rtx_equal_p (r1, r2);
7913 case dw_val_class_offset:
7914 return v1->v.val_offset == v2->v.val_offset;
7916 case dw_val_class_loc:
7917 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7918 loc1 && loc2;
7919 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7920 if (!same_loc_p (loc1, loc2, mark))
7921 return false;
7922 return !loc1 && !loc2;
7924 case dw_val_class_die_ref:
7925 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7927 case dw_val_class_symview:
7928 return strcmp (v1->v.val_symbolic_view, v2->v.val_symbolic_view) == 0;
7930 case dw_val_class_fde_ref:
7931 case dw_val_class_vms_delta:
7932 case dw_val_class_lbl_id:
7933 case dw_val_class_lineptr:
7934 case dw_val_class_macptr:
7935 case dw_val_class_loclistsptr:
7936 case dw_val_class_high_pc:
7937 return true;
7939 case dw_val_class_file:
7940 case dw_val_class_file_implicit:
7941 return v1->v.val_file == v2->v.val_file;
7943 case dw_val_class_data8:
7944 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
7946 default:
7947 return true;
7951 /* Do the attributes look the same? */
7953 static bool
7954 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
7956 if (at1->dw_attr != at2->dw_attr)
7957 return false;
7959 /* We don't care that this was compiled with a different compiler
7960 snapshot; if the output is the same, that's what matters. */
7961 if (at1->dw_attr == DW_AT_producer)
7962 return true;
7964 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7967 /* Do the dies look the same? */
7969 static bool
7970 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7972 dw_die_ref c1, c2;
7973 dw_attr_node *a1;
7974 unsigned ix;
7976 /* To avoid infinite recursion. */
7977 if (die1->die_mark)
7978 return die1->die_mark == die2->die_mark;
7979 die1->die_mark = die2->die_mark = ++(*mark);
7981 if (die1->die_tag != die2->die_tag)
7982 return false;
7984 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
7985 return false;
7987 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
7988 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
7989 return false;
7991 c1 = die1->die_child;
7992 c2 = die2->die_child;
7993 if (! c1)
7995 if (c2)
7996 return false;
7998 else
7999 for (;;)
8001 if (!same_die_p (c1, c2, mark))
8002 return false;
8003 c1 = c1->die_sib;
8004 c2 = c2->die_sib;
8005 if (c1 == die1->die_child)
8007 if (c2 == die2->die_child)
8008 break;
8009 else
8010 return false;
8014 return true;
8017 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
8018 children, and set die_symbol. */
8020 static void
8021 compute_comp_unit_symbol (dw_die_ref unit_die)
8023 const char *die_name = get_AT_string (unit_die, DW_AT_name);
8024 const char *base = die_name ? lbasename (die_name) : "anonymous";
8025 char *name = XALLOCAVEC (char, strlen (base) + 64);
8026 char *p;
8027 int i, mark;
8028 unsigned char checksum[16];
8029 struct md5_ctx ctx;
8031 /* Compute the checksum of the DIE, then append part of it as hex digits to
8032 the name filename of the unit. */
8034 md5_init_ctx (&ctx);
8035 mark = 0;
8036 die_checksum (unit_die, &ctx, &mark);
8037 unmark_all_dies (unit_die);
8038 md5_finish_ctx (&ctx, checksum);
8040 /* When we this for comp_unit_die () we have a DW_AT_name that might
8041 not start with a letter but with anything valid for filenames and
8042 clean_symbol_name doesn't fix that up. Prepend 'g' if the first
8043 character is not a letter. */
8044 sprintf (name, "%s%s.", ISALPHA (*base) ? "" : "g", base);
8045 clean_symbol_name (name);
8047 p = name + strlen (name);
8048 for (i = 0; i < 4; i++)
8050 sprintf (p, "%.2x", checksum[i]);
8051 p += 2;
8054 unit_die->die_id.die_symbol = xstrdup (name);
8057 /* Returns true if DIE represents a type, in the sense of TYPE_P. */
8059 static bool
8060 is_type_die (dw_die_ref die)
8062 switch (die->die_tag)
8064 case DW_TAG_array_type:
8065 case DW_TAG_class_type:
8066 case DW_TAG_interface_type:
8067 case DW_TAG_enumeration_type:
8068 case DW_TAG_pointer_type:
8069 case DW_TAG_reference_type:
8070 case DW_TAG_rvalue_reference_type:
8071 case DW_TAG_string_type:
8072 case DW_TAG_structure_type:
8073 case DW_TAG_subroutine_type:
8074 case DW_TAG_union_type:
8075 case DW_TAG_ptr_to_member_type:
8076 case DW_TAG_set_type:
8077 case DW_TAG_subrange_type:
8078 case DW_TAG_base_type:
8079 case DW_TAG_const_type:
8080 case DW_TAG_file_type:
8081 case DW_TAG_packed_type:
8082 case DW_TAG_volatile_type:
8083 case DW_TAG_typedef:
8084 return true;
8085 default:
8086 return false;
8090 /* Returns true iff C is a compile-unit DIE. */
8092 static inline bool
8093 is_cu_die (dw_die_ref c)
8095 return c && (c->die_tag == DW_TAG_compile_unit
8096 || c->die_tag == DW_TAG_skeleton_unit);
8099 /* Returns true iff C is a unit DIE of some sort. */
8101 static inline bool
8102 is_unit_die (dw_die_ref c)
8104 return c && (c->die_tag == DW_TAG_compile_unit
8105 || c->die_tag == DW_TAG_partial_unit
8106 || c->die_tag == DW_TAG_type_unit
8107 || c->die_tag == DW_TAG_skeleton_unit);
8110 /* Returns true iff C is a namespace DIE. */
8112 static inline bool
8113 is_namespace_die (dw_die_ref c)
8115 return c && c->die_tag == DW_TAG_namespace;
8118 /* Return true if this DIE is a template parameter. */
8120 static inline bool
8121 is_template_parameter (dw_die_ref die)
8123 switch (die->die_tag)
8125 case DW_TAG_template_type_param:
8126 case DW_TAG_template_value_param:
8127 case DW_TAG_GNU_template_template_param:
8128 case DW_TAG_GNU_template_parameter_pack:
8129 return true;
8130 default:
8131 return false;
8135 /* Return true if this DIE represents a template instantiation. */
8137 static inline bool
8138 is_template_instantiation (dw_die_ref die)
8140 dw_die_ref c;
8142 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
8143 return false;
8144 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
8145 return false;
8148 static char *
8149 gen_internal_sym (const char *prefix)
8151 char buf[MAX_ARTIFICIAL_LABEL_BYTES];
8153 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
8154 return xstrdup (buf);
8157 /* Return true if this DIE is a declaration. */
8159 static bool
8160 is_declaration_die (dw_die_ref die)
8162 dw_attr_node *a;
8163 unsigned ix;
8165 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8166 if (a->dw_attr == DW_AT_declaration)
8167 return true;
8169 return false;
8172 /* Return true if this DIE is nested inside a subprogram. */
8174 static bool
8175 is_nested_in_subprogram (dw_die_ref die)
8177 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
8179 if (decl == NULL)
8180 decl = die;
8181 return local_scope_p (decl);
8184 /* Return true if this DIE contains a defining declaration of a
8185 subprogram. */
8187 static bool
8188 contains_subprogram_definition (dw_die_ref die)
8190 dw_die_ref c;
8192 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
8193 return true;
8194 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
8195 return false;
8198 /* Return true if this is a type DIE that should be moved to a
8199 COMDAT .debug_types section or .debug_info section with DW_UT_*type
8200 unit type. */
8202 static bool
8203 should_move_die_to_comdat (dw_die_ref die)
8205 switch (die->die_tag)
8207 case DW_TAG_class_type:
8208 case DW_TAG_structure_type:
8209 case DW_TAG_enumeration_type:
8210 case DW_TAG_union_type:
8211 /* Don't move declarations, inlined instances, types nested in a
8212 subprogram, or types that contain subprogram definitions. */
8213 if (is_declaration_die (die)
8214 || get_AT (die, DW_AT_abstract_origin)
8215 || is_nested_in_subprogram (die)
8216 || contains_subprogram_definition (die))
8217 return false;
8218 return true;
8219 case DW_TAG_array_type:
8220 case DW_TAG_interface_type:
8221 case DW_TAG_pointer_type:
8222 case DW_TAG_reference_type:
8223 case DW_TAG_rvalue_reference_type:
8224 case DW_TAG_string_type:
8225 case DW_TAG_subroutine_type:
8226 case DW_TAG_ptr_to_member_type:
8227 case DW_TAG_set_type:
8228 case DW_TAG_subrange_type:
8229 case DW_TAG_base_type:
8230 case DW_TAG_const_type:
8231 case DW_TAG_file_type:
8232 case DW_TAG_packed_type:
8233 case DW_TAG_volatile_type:
8234 case DW_TAG_typedef:
8235 default:
8236 return false;
8240 /* Make a clone of DIE. */
8242 static dw_die_ref
8243 clone_die (dw_die_ref die)
8245 dw_die_ref clone = new_die_raw (die->die_tag);
8246 dw_attr_node *a;
8247 unsigned ix;
8249 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8250 add_dwarf_attr (clone, a);
8252 return clone;
8255 /* Make a clone of the tree rooted at DIE. */
8257 static dw_die_ref
8258 clone_tree (dw_die_ref die)
8260 dw_die_ref c;
8261 dw_die_ref clone = clone_die (die);
8263 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
8265 return clone;
8268 /* Make a clone of DIE as a declaration. */
8270 static dw_die_ref
8271 clone_as_declaration (dw_die_ref die)
8273 dw_die_ref clone;
8274 dw_die_ref decl;
8275 dw_attr_node *a;
8276 unsigned ix;
8278 /* If the DIE is already a declaration, just clone it. */
8279 if (is_declaration_die (die))
8280 return clone_die (die);
8282 /* If the DIE is a specification, just clone its declaration DIE. */
8283 decl = get_AT_ref (die, DW_AT_specification);
8284 if (decl != NULL)
8286 clone = clone_die (decl);
8287 if (die->comdat_type_p)
8288 add_AT_die_ref (clone, DW_AT_signature, die);
8289 return clone;
8292 clone = new_die_raw (die->die_tag);
8294 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8296 /* We don't want to copy over all attributes.
8297 For example we don't want DW_AT_byte_size because otherwise we will no
8298 longer have a declaration and GDB will treat it as a definition. */
8300 switch (a->dw_attr)
8302 case DW_AT_abstract_origin:
8303 case DW_AT_artificial:
8304 case DW_AT_containing_type:
8305 case DW_AT_external:
8306 case DW_AT_name:
8307 case DW_AT_type:
8308 case DW_AT_virtuality:
8309 case DW_AT_linkage_name:
8310 case DW_AT_MIPS_linkage_name:
8311 add_dwarf_attr (clone, a);
8312 break;
8313 case DW_AT_byte_size:
8314 case DW_AT_alignment:
8315 default:
8316 break;
8320 if (die->comdat_type_p)
8321 add_AT_die_ref (clone, DW_AT_signature, die);
8323 add_AT_flag (clone, DW_AT_declaration, 1);
8324 return clone;
8328 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
8330 struct decl_table_entry
8332 dw_die_ref orig;
8333 dw_die_ref copy;
8336 /* Helpers to manipulate hash table of copied declarations. */
8338 /* Hashtable helpers. */
8340 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
8342 typedef die_struct *compare_type;
8343 static inline hashval_t hash (const decl_table_entry *);
8344 static inline bool equal (const decl_table_entry *, const die_struct *);
8347 inline hashval_t
8348 decl_table_entry_hasher::hash (const decl_table_entry *entry)
8350 return htab_hash_pointer (entry->orig);
8353 inline bool
8354 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
8355 const die_struct *entry2)
8357 return entry1->orig == entry2;
8360 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
8362 /* Copy DIE and its ancestors, up to, but not including, the compile unit
8363 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
8364 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
8365 to check if the ancestor has already been copied into UNIT. */
8367 static dw_die_ref
8368 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
8369 decl_hash_type *decl_table)
8371 dw_die_ref parent = die->die_parent;
8372 dw_die_ref new_parent = unit;
8373 dw_die_ref copy;
8374 decl_table_entry **slot = NULL;
8375 struct decl_table_entry *entry = NULL;
8377 /* If DIE refers to a stub unfold that so we get the appropriate
8378 DIE registered as orig in decl_table. */
8379 if (dw_die_ref c = get_AT_ref (die, DW_AT_signature))
8380 die = c;
8382 if (decl_table)
8384 /* Check if the entry has already been copied to UNIT. */
8385 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
8386 INSERT);
8387 if (*slot != HTAB_EMPTY_ENTRY)
8389 entry = *slot;
8390 return entry->copy;
8393 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
8394 entry = XCNEW (struct decl_table_entry);
8395 entry->orig = die;
8396 entry->copy = NULL;
8397 *slot = entry;
8400 if (parent != NULL)
8402 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
8403 if (spec != NULL)
8404 parent = spec;
8405 if (!is_unit_die (parent))
8406 new_parent = copy_ancestor_tree (unit, parent, decl_table);
8409 copy = clone_as_declaration (die);
8410 add_child_die (new_parent, copy);
8412 if (decl_table)
8414 /* Record the pointer to the copy. */
8415 entry->copy = copy;
8418 return copy;
8420 /* Copy the declaration context to the new type unit DIE. This includes
8421 any surrounding namespace or type declarations. If the DIE has an
8422 AT_specification attribute, it also includes attributes and children
8423 attached to the specification, and returns a pointer to the original
8424 parent of the declaration DIE. Returns NULL otherwise. */
8426 static dw_die_ref
8427 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
8429 dw_die_ref decl;
8430 dw_die_ref new_decl;
8431 dw_die_ref orig_parent = NULL;
8433 decl = get_AT_ref (die, DW_AT_specification);
8434 if (decl == NULL)
8435 decl = die;
8436 else
8438 unsigned ix;
8439 dw_die_ref c;
8440 dw_attr_node *a;
8442 /* The original DIE will be changed to a declaration, and must
8443 be moved to be a child of the original declaration DIE. */
8444 orig_parent = decl->die_parent;
8446 /* Copy the type node pointer from the new DIE to the original
8447 declaration DIE so we can forward references later. */
8448 decl->comdat_type_p = true;
8449 decl->die_id.die_type_node = die->die_id.die_type_node;
8451 remove_AT (die, DW_AT_specification);
8453 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
8455 if (a->dw_attr != DW_AT_name
8456 && a->dw_attr != DW_AT_declaration
8457 && a->dw_attr != DW_AT_external)
8458 add_dwarf_attr (die, a);
8461 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
8464 if (decl->die_parent != NULL
8465 && !is_unit_die (decl->die_parent))
8467 new_decl = copy_ancestor_tree (unit, decl, NULL);
8468 if (new_decl != NULL)
8470 remove_AT (new_decl, DW_AT_signature);
8471 add_AT_specification (die, new_decl);
8475 return orig_parent;
8478 /* Generate the skeleton ancestor tree for the given NODE, then clone
8479 the DIE and add the clone into the tree. */
8481 static void
8482 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
8484 if (node->new_die != NULL)
8485 return;
8487 node->new_die = clone_as_declaration (node->old_die);
8489 if (node->parent != NULL)
8491 generate_skeleton_ancestor_tree (node->parent);
8492 add_child_die (node->parent->new_die, node->new_die);
8496 /* Generate a skeleton tree of DIEs containing any declarations that are
8497 found in the original tree. We traverse the tree looking for declaration
8498 DIEs, and construct the skeleton from the bottom up whenever we find one. */
8500 static void
8501 generate_skeleton_bottom_up (skeleton_chain_node *parent)
8503 skeleton_chain_node node;
8504 dw_die_ref c;
8505 dw_die_ref first;
8506 dw_die_ref prev = NULL;
8507 dw_die_ref next = NULL;
8509 node.parent = parent;
8511 first = c = parent->old_die->die_child;
8512 if (c)
8513 next = c->die_sib;
8514 if (c) do {
8515 if (prev == NULL || prev->die_sib == c)
8516 prev = c;
8517 c = next;
8518 next = (c == first ? NULL : c->die_sib);
8519 node.old_die = c;
8520 node.new_die = NULL;
8521 if (is_declaration_die (c))
8523 if (is_template_instantiation (c))
8525 /* Instantiated templates do not need to be cloned into the
8526 type unit. Just move the DIE and its children back to
8527 the skeleton tree (in the main CU). */
8528 remove_child_with_prev (c, prev);
8529 add_child_die (parent->new_die, c);
8530 c = prev;
8532 else if (c->comdat_type_p)
8534 /* This is the skeleton of earlier break_out_comdat_types
8535 type. Clone the existing DIE, but keep the children
8536 under the original (which is in the main CU). */
8537 dw_die_ref clone = clone_die (c);
8539 replace_child (c, clone, prev);
8540 generate_skeleton_ancestor_tree (parent);
8541 add_child_die (parent->new_die, c);
8542 c = clone;
8543 continue;
8545 else
8547 /* Clone the existing DIE, move the original to the skeleton
8548 tree (which is in the main CU), and put the clone, with
8549 all the original's children, where the original came from
8550 (which is about to be moved to the type unit). */
8551 dw_die_ref clone = clone_die (c);
8552 move_all_children (c, clone);
8554 /* If the original has a DW_AT_object_pointer attribute,
8555 it would now point to a child DIE just moved to the
8556 cloned tree, so we need to remove that attribute from
8557 the original. */
8558 remove_AT (c, DW_AT_object_pointer);
8560 replace_child (c, clone, prev);
8561 generate_skeleton_ancestor_tree (parent);
8562 add_child_die (parent->new_die, c);
8563 node.old_die = clone;
8564 node.new_die = c;
8565 c = clone;
8568 generate_skeleton_bottom_up (&node);
8569 } while (next != NULL);
8572 /* Wrapper function for generate_skeleton_bottom_up. */
8574 static dw_die_ref
8575 generate_skeleton (dw_die_ref die)
8577 skeleton_chain_node node;
8579 node.old_die = die;
8580 node.new_die = NULL;
8581 node.parent = NULL;
8583 /* If this type definition is nested inside another type,
8584 and is not an instantiation of a template, always leave
8585 at least a declaration in its place. */
8586 if (die->die_parent != NULL
8587 && is_type_die (die->die_parent)
8588 && !is_template_instantiation (die))
8589 node.new_die = clone_as_declaration (die);
8591 generate_skeleton_bottom_up (&node);
8592 return node.new_die;
8595 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
8596 declaration. The original DIE is moved to a new compile unit so that
8597 existing references to it follow it to the new location. If any of the
8598 original DIE's descendants is a declaration, we need to replace the
8599 original DIE with a skeleton tree and move the declarations back into the
8600 skeleton tree. */
8602 static dw_die_ref
8603 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
8604 dw_die_ref prev)
8606 dw_die_ref skeleton, orig_parent;
8608 /* Copy the declaration context to the type unit DIE. If the returned
8609 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
8610 that DIE. */
8611 orig_parent = copy_declaration_context (unit, child);
8613 skeleton = generate_skeleton (child);
8614 if (skeleton == NULL)
8615 remove_child_with_prev (child, prev);
8616 else
8618 skeleton->comdat_type_p = true;
8619 skeleton->die_id.die_type_node = child->die_id.die_type_node;
8621 /* If the original DIE was a specification, we need to put
8622 the skeleton under the parent DIE of the declaration.
8623 This leaves the original declaration in the tree, but
8624 it will be pruned later since there are no longer any
8625 references to it. */
8626 if (orig_parent != NULL)
8628 remove_child_with_prev (child, prev);
8629 add_child_die (orig_parent, skeleton);
8631 else
8632 replace_child (child, skeleton, prev);
8635 return skeleton;
8638 static void
8639 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8640 comdat_type_node *type_node,
8641 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
8643 /* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
8644 procedure, put it under TYPE_NODE and return the copy. Continue looking for
8645 DWARF procedure references in the DW_AT_location attribute. */
8647 static dw_die_ref
8648 copy_dwarf_procedure (dw_die_ref die,
8649 comdat_type_node *type_node,
8650 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8652 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
8654 /* DWARF procedures are not supposed to have children... */
8655 gcc_assert (die->die_child == NULL);
8657 /* ... and they are supposed to have only one attribute: DW_AT_location. */
8658 gcc_assert (vec_safe_length (die->die_attr) == 1
8659 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
8661 /* Do not copy more than once DWARF procedures. */
8662 bool existed;
8663 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
8664 if (existed)
8665 return die_copy;
8667 die_copy = clone_die (die);
8668 add_child_die (type_node->root_die, die_copy);
8669 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
8670 return die_copy;
8673 /* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
8674 procedures in DIE's attributes. */
8676 static void
8677 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8678 comdat_type_node *type_node,
8679 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8681 dw_attr_node *a;
8682 unsigned i;
8684 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
8686 dw_loc_descr_ref loc;
8688 if (a->dw_attr_val.val_class != dw_val_class_loc)
8689 continue;
8691 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
8693 switch (loc->dw_loc_opc)
8695 case DW_OP_call2:
8696 case DW_OP_call4:
8697 case DW_OP_call_ref:
8698 gcc_assert (loc->dw_loc_oprnd1.val_class
8699 == dw_val_class_die_ref);
8700 loc->dw_loc_oprnd1.v.val_die_ref.die
8701 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
8702 type_node,
8703 copied_dwarf_procs);
8705 default:
8706 break;
8712 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
8713 rewrite references to point to the copies.
8715 References are looked for in DIE's attributes and recursively in all its
8716 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
8717 mapping from old DWARF procedures to their copy. It is used not to copy
8718 twice the same DWARF procedure under TYPE_NODE. */
8720 static void
8721 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
8722 comdat_type_node *type_node,
8723 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8725 dw_die_ref c;
8727 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
8728 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
8729 type_node,
8730 copied_dwarf_procs));
8733 /* Traverse the DIE and set up additional .debug_types or .debug_info
8734 DW_UT_*type sections for each type worthy of being placed in a COMDAT
8735 section. */
8737 static void
8738 break_out_comdat_types (dw_die_ref die)
8740 dw_die_ref c;
8741 dw_die_ref first;
8742 dw_die_ref prev = NULL;
8743 dw_die_ref next = NULL;
8744 dw_die_ref unit = NULL;
8746 first = c = die->die_child;
8747 if (c)
8748 next = c->die_sib;
8749 if (c) do {
8750 if (prev == NULL || prev->die_sib == c)
8751 prev = c;
8752 c = next;
8753 next = (c == first ? NULL : c->die_sib);
8754 if (should_move_die_to_comdat (c))
8756 dw_die_ref replacement;
8757 comdat_type_node *type_node;
8759 /* Break out nested types into their own type units. */
8760 break_out_comdat_types (c);
8762 /* Create a new type unit DIE as the root for the new tree. */
8763 unit = new_die (DW_TAG_type_unit, NULL, NULL);
8764 add_AT_unsigned (unit, DW_AT_language,
8765 get_AT_unsigned (comp_unit_die (), DW_AT_language));
8767 /* Add the new unit's type DIE into the comdat type list. */
8768 type_node = ggc_cleared_alloc<comdat_type_node> ();
8769 type_node->root_die = unit;
8770 type_node->next = comdat_type_list;
8771 comdat_type_list = type_node;
8773 /* Generate the type signature. */
8774 generate_type_signature (c, type_node);
8776 /* Copy the declaration context, attributes, and children of the
8777 declaration into the new type unit DIE, then remove this DIE
8778 from the main CU (or replace it with a skeleton if necessary). */
8779 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
8780 type_node->skeleton_die = replacement;
8782 /* Add the DIE to the new compunit. */
8783 add_child_die (unit, c);
8785 /* Types can reference DWARF procedures for type size or data location
8786 expressions. Calls in DWARF expressions cannot target procedures
8787 that are not in the same section. So we must copy DWARF procedures
8788 along with this type and then rewrite references to them. */
8789 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
8790 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
8792 if (replacement != NULL)
8793 c = replacement;
8795 else if (c->die_tag == DW_TAG_namespace
8796 || c->die_tag == DW_TAG_class_type
8797 || c->die_tag == DW_TAG_structure_type
8798 || c->die_tag == DW_TAG_union_type)
8800 /* Look for nested types that can be broken out. */
8801 break_out_comdat_types (c);
8803 } while (next != NULL);
8806 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
8807 Enter all the cloned children into the hash table decl_table. */
8809 static dw_die_ref
8810 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
8812 dw_die_ref c;
8813 dw_die_ref clone;
8814 struct decl_table_entry *entry;
8815 decl_table_entry **slot;
8817 if (die->die_tag == DW_TAG_subprogram)
8818 clone = clone_as_declaration (die);
8819 else
8820 clone = clone_die (die);
8822 slot = decl_table->find_slot_with_hash (die,
8823 htab_hash_pointer (die), INSERT);
8825 /* Assert that DIE isn't in the hash table yet. If it would be there
8826 before, the ancestors would be necessarily there as well, therefore
8827 clone_tree_partial wouldn't be called. */
8828 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8830 entry = XCNEW (struct decl_table_entry);
8831 entry->orig = die;
8832 entry->copy = clone;
8833 *slot = entry;
8835 if (die->die_tag != DW_TAG_subprogram)
8836 FOR_EACH_CHILD (die, c,
8837 add_child_die (clone, clone_tree_partial (c, decl_table)));
8839 return clone;
8842 /* Walk the DIE and its children, looking for references to incomplete
8843 or trivial types that are unmarked (i.e., that are not in the current
8844 type_unit). */
8846 static void
8847 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
8849 dw_die_ref c;
8850 dw_attr_node *a;
8851 unsigned ix;
8853 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8855 if (AT_class (a) == dw_val_class_die_ref)
8857 dw_die_ref targ = AT_ref (a);
8858 decl_table_entry **slot;
8859 struct decl_table_entry *entry;
8861 if (targ->die_mark != 0 || targ->comdat_type_p)
8862 continue;
8864 slot = decl_table->find_slot_with_hash (targ,
8865 htab_hash_pointer (targ),
8866 INSERT);
8868 if (*slot != HTAB_EMPTY_ENTRY)
8870 /* TARG has already been copied, so we just need to
8871 modify the reference to point to the copy. */
8872 entry = *slot;
8873 a->dw_attr_val.v.val_die_ref.die = entry->copy;
8875 else
8877 dw_die_ref parent = unit;
8878 dw_die_ref copy = clone_die (targ);
8880 /* Record in DECL_TABLE that TARG has been copied.
8881 Need to do this now, before the recursive call,
8882 because DECL_TABLE may be expanded and SLOT
8883 would no longer be a valid pointer. */
8884 entry = XCNEW (struct decl_table_entry);
8885 entry->orig = targ;
8886 entry->copy = copy;
8887 *slot = entry;
8889 /* If TARG is not a declaration DIE, we need to copy its
8890 children. */
8891 if (!is_declaration_die (targ))
8893 FOR_EACH_CHILD (
8894 targ, c,
8895 add_child_die (copy,
8896 clone_tree_partial (c, decl_table)));
8899 /* Make sure the cloned tree is marked as part of the
8900 type unit. */
8901 mark_dies (copy);
8903 /* If TARG has surrounding context, copy its ancestor tree
8904 into the new type unit. */
8905 if (targ->die_parent != NULL
8906 && !is_unit_die (targ->die_parent))
8907 parent = copy_ancestor_tree (unit, targ->die_parent,
8908 decl_table);
8910 add_child_die (parent, copy);
8911 a->dw_attr_val.v.val_die_ref.die = copy;
8913 /* Make sure the newly-copied DIE is walked. If it was
8914 installed in a previously-added context, it won't
8915 get visited otherwise. */
8916 if (parent != unit)
8918 /* Find the highest point of the newly-added tree,
8919 mark each node along the way, and walk from there. */
8920 parent->die_mark = 1;
8921 while (parent->die_parent
8922 && parent->die_parent->die_mark == 0)
8924 parent = parent->die_parent;
8925 parent->die_mark = 1;
8927 copy_decls_walk (unit, parent, decl_table);
8933 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8936 /* Collect skeleton dies in DIE created by break_out_comdat_types already
8937 and record them in DECL_TABLE. */
8939 static void
8940 collect_skeleton_dies (dw_die_ref die, decl_hash_type *decl_table)
8942 dw_die_ref c;
8944 if (dw_attr_node *a = get_AT (die, DW_AT_signature))
8946 dw_die_ref targ = AT_ref (a);
8947 gcc_assert (targ->die_mark == 0 && targ->comdat_type_p);
8948 decl_table_entry **slot
8949 = decl_table->find_slot_with_hash (targ,
8950 htab_hash_pointer (targ),
8951 INSERT);
8952 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8953 /* Record in DECL_TABLE that TARG has been already copied
8954 by remove_child_or_replace_with_skeleton. */
8955 decl_table_entry *entry = XCNEW (struct decl_table_entry);
8956 entry->orig = targ;
8957 entry->copy = die;
8958 *slot = entry;
8960 FOR_EACH_CHILD (die, c, collect_skeleton_dies (c, decl_table));
8963 /* Copy declarations for "unworthy" types into the new comdat section.
8964 Incomplete types, modified types, and certain other types aren't broken
8965 out into comdat sections of their own, so they don't have a signature,
8966 and we need to copy the declaration into the same section so that we
8967 don't have an external reference. */
8969 static void
8970 copy_decls_for_unworthy_types (dw_die_ref unit)
8972 mark_dies (unit);
8973 decl_hash_type decl_table (10);
8974 collect_skeleton_dies (unit, &decl_table);
8975 copy_decls_walk (unit, unit, &decl_table);
8976 unmark_dies (unit);
8979 /* Traverse the DIE and add a sibling attribute if it may have the
8980 effect of speeding up access to siblings. To save some space,
8981 avoid generating sibling attributes for DIE's without children. */
8983 static void
8984 add_sibling_attributes (dw_die_ref die)
8986 dw_die_ref c;
8988 if (! die->die_child)
8989 return;
8991 if (die->die_parent && die != die->die_parent->die_child)
8992 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8994 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8997 /* Output all location lists for the DIE and its children. */
8999 static void
9000 output_location_lists (dw_die_ref die)
9002 dw_die_ref c;
9003 dw_attr_node *a;
9004 unsigned ix;
9006 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9007 if (AT_class (a) == dw_val_class_loc_list)
9008 output_loc_list (AT_loc_list (a));
9010 FOR_EACH_CHILD (die, c, output_location_lists (c));
9013 /* During assign_location_list_indexes and output_loclists_offset the
9014 current index, after it the number of assigned indexes (i.e. how
9015 large the .debug_loclists* offset table should be). */
9016 static unsigned int loc_list_idx;
9018 /* Output all location list offsets for the DIE and its children. */
9020 static void
9021 output_loclists_offsets (dw_die_ref die)
9023 dw_die_ref c;
9024 dw_attr_node *a;
9025 unsigned ix;
9027 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9028 if (AT_class (a) == dw_val_class_loc_list)
9030 dw_loc_list_ref l = AT_loc_list (a);
9031 if (l->offset_emitted)
9032 continue;
9033 dw2_asm_output_delta (dwarf_offset_size, l->ll_symbol,
9034 loc_section_label, NULL);
9035 gcc_assert (l->hash == loc_list_idx);
9036 loc_list_idx++;
9037 l->offset_emitted = true;
9040 FOR_EACH_CHILD (die, c, output_loclists_offsets (c));
9043 /* Recursively set indexes of location lists. */
9045 static void
9046 assign_location_list_indexes (dw_die_ref die)
9048 dw_die_ref c;
9049 dw_attr_node *a;
9050 unsigned ix;
9052 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9053 if (AT_class (a) == dw_val_class_loc_list)
9055 dw_loc_list_ref list = AT_loc_list (a);
9056 if (!list->num_assigned)
9058 list->num_assigned = true;
9059 list->hash = loc_list_idx++;
9063 FOR_EACH_CHILD (die, c, assign_location_list_indexes (c));
9066 /* We want to limit the number of external references, because they are
9067 larger than local references: a relocation takes multiple words, and
9068 even a sig8 reference is always eight bytes, whereas a local reference
9069 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
9070 So if we encounter multiple external references to the same type DIE, we
9071 make a local typedef stub for it and redirect all references there.
9073 This is the element of the hash table for keeping track of these
9074 references. */
9076 struct external_ref
9078 dw_die_ref type;
9079 dw_die_ref stub;
9080 unsigned n_refs;
9083 /* Hashtable helpers. */
9085 struct external_ref_hasher : free_ptr_hash <external_ref>
9087 static inline hashval_t hash (const external_ref *);
9088 static inline bool equal (const external_ref *, const external_ref *);
9091 inline hashval_t
9092 external_ref_hasher::hash (const external_ref *r)
9094 dw_die_ref die = r->type;
9095 hashval_t h = 0;
9097 /* We can't use the address of the DIE for hashing, because
9098 that will make the order of the stub DIEs non-deterministic. */
9099 if (! die->comdat_type_p)
9100 /* We have a symbol; use it to compute a hash. */
9101 h = htab_hash_string (die->die_id.die_symbol);
9102 else
9104 /* We have a type signature; use a subset of the bits as the hash.
9105 The 8-byte signature is at least as large as hashval_t. */
9106 comdat_type_node *type_node = die->die_id.die_type_node;
9107 memcpy (&h, type_node->signature, sizeof (h));
9109 return h;
9112 inline bool
9113 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
9115 return r1->type == r2->type;
9118 typedef hash_table<external_ref_hasher> external_ref_hash_type;
9120 /* Return a pointer to the external_ref for references to DIE. */
9122 static struct external_ref *
9123 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
9125 struct external_ref ref, *ref_p;
9126 external_ref **slot;
9128 ref.type = die;
9129 slot = map->find_slot (&ref, INSERT);
9130 if (*slot != HTAB_EMPTY_ENTRY)
9131 return *slot;
9133 ref_p = XCNEW (struct external_ref);
9134 ref_p->type = die;
9135 *slot = ref_p;
9136 return ref_p;
9139 /* Subroutine of optimize_external_refs, below.
9141 If we see a type skeleton, record it as our stub. If we see external
9142 references, remember how many we've seen. */
9144 static void
9145 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
9147 dw_die_ref c;
9148 dw_attr_node *a;
9149 unsigned ix;
9150 struct external_ref *ref_p;
9152 if (is_type_die (die)
9153 && (c = get_AT_ref (die, DW_AT_signature)))
9155 /* This is a local skeleton; use it for local references. */
9156 ref_p = lookup_external_ref (map, c);
9157 ref_p->stub = die;
9160 /* Scan the DIE references, and remember any that refer to DIEs from
9161 other CUs (i.e. those which are not marked). */
9162 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9163 if (AT_class (a) == dw_val_class_die_ref
9164 && (c = AT_ref (a))->die_mark == 0
9165 && is_type_die (c))
9167 ref_p = lookup_external_ref (map, c);
9168 ref_p->n_refs++;
9171 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
9174 /* htab_traverse callback function for optimize_external_refs, below. SLOT
9175 points to an external_ref, DATA is the CU we're processing. If we don't
9176 already have a local stub, and we have multiple refs, build a stub. */
9179 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
9181 struct external_ref *ref_p = *slot;
9183 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
9185 /* We have multiple references to this type, so build a small stub.
9186 Both of these forms are a bit dodgy from the perspective of the
9187 DWARF standard, since technically they should have names. */
9188 dw_die_ref cu = data;
9189 dw_die_ref type = ref_p->type;
9190 dw_die_ref stub = NULL;
9192 if (type->comdat_type_p)
9194 /* If we refer to this type via sig8, use AT_signature. */
9195 stub = new_die (type->die_tag, cu, NULL_TREE);
9196 add_AT_die_ref (stub, DW_AT_signature, type);
9198 else
9200 /* Otherwise, use a typedef with no name. */
9201 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
9202 add_AT_die_ref (stub, DW_AT_type, type);
9205 stub->die_mark++;
9206 ref_p->stub = stub;
9208 return 1;
9211 /* DIE is a unit; look through all the DIE references to see if there are
9212 any external references to types, and if so, create local stubs for
9213 them which will be applied in build_abbrev_table. This is useful because
9214 references to local DIEs are smaller. */
9216 static external_ref_hash_type *
9217 optimize_external_refs (dw_die_ref die)
9219 external_ref_hash_type *map = new external_ref_hash_type (10);
9220 optimize_external_refs_1 (die, map);
9221 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
9222 return map;
9225 /* The following 3 variables are temporaries that are computed only during the
9226 build_abbrev_table call and used and released during the following
9227 optimize_abbrev_table call. */
9229 /* First abbrev_id that can be optimized based on usage. */
9230 static unsigned int abbrev_opt_start;
9232 /* Maximum abbrev_id of a base type plus one (we can't optimize DIEs with
9233 abbrev_id smaller than this, because they must be already sized
9234 during build_abbrev_table). */
9235 static unsigned int abbrev_opt_base_type_end;
9237 /* Vector of usage counts during build_abbrev_table. Indexed by
9238 abbrev_id - abbrev_opt_start. */
9239 static vec<unsigned int> abbrev_usage_count;
9241 /* Vector of all DIEs added with die_abbrev >= abbrev_opt_start. */
9242 static vec<dw_die_ref> sorted_abbrev_dies;
9244 /* The format of each DIE (and its attribute value pairs) is encoded in an
9245 abbreviation table. This routine builds the abbreviation table and assigns
9246 a unique abbreviation id for each abbreviation entry. The children of each
9247 die are visited recursively. */
9249 static void
9250 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
9252 unsigned int abbrev_id = 0;
9253 dw_die_ref c;
9254 dw_attr_node *a;
9255 unsigned ix;
9256 dw_die_ref abbrev;
9258 /* Scan the DIE references, and replace any that refer to
9259 DIEs from other CUs (i.e. those which are not marked) with
9260 the local stubs we built in optimize_external_refs. */
9261 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9262 if (AT_class (a) == dw_val_class_die_ref
9263 && (c = AT_ref (a))->die_mark == 0)
9265 struct external_ref *ref_p;
9266 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
9268 if (is_type_die (c)
9269 && (ref_p = lookup_external_ref (extern_map, c))
9270 && ref_p->stub && ref_p->stub != die)
9272 gcc_assert (a->dw_attr != DW_AT_signature);
9273 change_AT_die_ref (a, ref_p->stub);
9275 else
9276 /* We aren't changing this reference, so mark it external. */
9277 set_AT_ref_external (a, 1);
9280 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
9282 dw_attr_node *die_a, *abbrev_a;
9283 unsigned ix;
9284 bool ok = true;
9286 if (abbrev_id == 0)
9287 continue;
9288 if (abbrev->die_tag != die->die_tag)
9289 continue;
9290 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
9291 continue;
9293 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
9294 continue;
9296 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
9298 abbrev_a = &(*abbrev->die_attr)[ix];
9299 if ((abbrev_a->dw_attr != die_a->dw_attr)
9300 || (value_format (abbrev_a) != value_format (die_a)))
9302 ok = false;
9303 break;
9306 if (ok)
9307 break;
9310 if (abbrev_id >= vec_safe_length (abbrev_die_table))
9312 vec_safe_push (abbrev_die_table, die);
9313 if (abbrev_opt_start)
9314 abbrev_usage_count.safe_push (0);
9316 if (abbrev_opt_start && abbrev_id >= abbrev_opt_start)
9318 abbrev_usage_count[abbrev_id - abbrev_opt_start]++;
9319 sorted_abbrev_dies.safe_push (die);
9322 die->die_abbrev = abbrev_id;
9323 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
9326 /* Callback function for sorted_abbrev_dies vector sorting. We sort
9327 by die_abbrev's usage count, from the most commonly used
9328 abbreviation to the least. */
9330 static int
9331 die_abbrev_cmp (const void *p1, const void *p2)
9333 dw_die_ref die1 = *(const dw_die_ref *) p1;
9334 dw_die_ref die2 = *(const dw_die_ref *) p2;
9336 gcc_checking_assert (die1->die_abbrev >= abbrev_opt_start);
9337 gcc_checking_assert (die2->die_abbrev >= abbrev_opt_start);
9339 if (die1->die_abbrev >= abbrev_opt_base_type_end
9340 && die2->die_abbrev >= abbrev_opt_base_type_end)
9342 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
9343 > abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
9344 return -1;
9345 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
9346 < abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
9347 return 1;
9350 /* Stabilize the sort. */
9351 if (die1->die_abbrev < die2->die_abbrev)
9352 return -1;
9353 if (die1->die_abbrev > die2->die_abbrev)
9354 return 1;
9356 return 0;
9359 /* Convert dw_val_class_const and dw_val_class_unsigned_const class attributes
9360 of DIEs in between sorted_abbrev_dies[first_id] and abbrev_dies[end_id - 1]
9361 into dw_val_class_const_implicit or
9362 dw_val_class_unsigned_const_implicit. */
9364 static void
9365 optimize_implicit_const (unsigned int first_id, unsigned int end,
9366 vec<bool> &implicit_consts)
9368 /* It never makes sense if there is just one DIE using the abbreviation. */
9369 if (end < first_id + 2)
9370 return;
9372 dw_attr_node *a;
9373 unsigned ix, i;
9374 dw_die_ref die = sorted_abbrev_dies[first_id];
9375 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9376 if (implicit_consts[ix])
9378 enum dw_val_class new_class = dw_val_class_none;
9379 switch (AT_class (a))
9381 case dw_val_class_unsigned_const:
9382 if ((HOST_WIDE_INT) AT_unsigned (a) < 0)
9383 continue;
9385 /* The .debug_abbrev section will grow by
9386 size_of_sleb128 (AT_unsigned (a)) and we avoid the constants
9387 in all the DIEs using that abbreviation. */
9388 if (constant_size (AT_unsigned (a)) * (end - first_id)
9389 <= (unsigned) size_of_sleb128 (AT_unsigned (a)))
9390 continue;
9392 new_class = dw_val_class_unsigned_const_implicit;
9393 break;
9395 case dw_val_class_const:
9396 new_class = dw_val_class_const_implicit;
9397 break;
9399 case dw_val_class_file:
9400 new_class = dw_val_class_file_implicit;
9401 break;
9403 default:
9404 continue;
9406 for (i = first_id; i < end; i++)
9407 (*sorted_abbrev_dies[i]->die_attr)[ix].dw_attr_val.val_class
9408 = new_class;
9412 /* Attempt to optimize abbreviation table from abbrev_opt_start
9413 abbreviation above. */
9415 static void
9416 optimize_abbrev_table (void)
9418 if (abbrev_opt_start
9419 && vec_safe_length (abbrev_die_table) > abbrev_opt_start
9420 && (dwarf_version >= 5 || vec_safe_length (abbrev_die_table) > 127))
9422 auto_vec<bool, 32> implicit_consts;
9423 sorted_abbrev_dies.qsort (die_abbrev_cmp);
9425 unsigned int abbrev_id = abbrev_opt_start - 1;
9426 unsigned int first_id = ~0U;
9427 unsigned int last_abbrev_id = 0;
9428 unsigned int i;
9429 dw_die_ref die;
9430 if (abbrev_opt_base_type_end > abbrev_opt_start)
9431 abbrev_id = abbrev_opt_base_type_end - 1;
9432 /* Reassign abbreviation ids from abbrev_opt_start above, so that
9433 most commonly used abbreviations come first. */
9434 FOR_EACH_VEC_ELT (sorted_abbrev_dies, i, die)
9436 dw_attr_node *a;
9437 unsigned ix;
9439 /* If calc_base_type_die_sizes has been called, the CU and
9440 base types after it can't be optimized, because we've already
9441 calculated their DIE offsets. We've sorted them first. */
9442 if (die->die_abbrev < abbrev_opt_base_type_end)
9443 continue;
9444 if (die->die_abbrev != last_abbrev_id)
9446 last_abbrev_id = die->die_abbrev;
9447 if (dwarf_version >= 5 && first_id != ~0U)
9448 optimize_implicit_const (first_id, i, implicit_consts);
9449 abbrev_id++;
9450 (*abbrev_die_table)[abbrev_id] = die;
9451 if (dwarf_version >= 5)
9453 first_id = i;
9454 implicit_consts.truncate (0);
9456 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9457 switch (AT_class (a))
9459 case dw_val_class_const:
9460 case dw_val_class_unsigned_const:
9461 case dw_val_class_file:
9462 implicit_consts.safe_push (true);
9463 break;
9464 default:
9465 implicit_consts.safe_push (false);
9466 break;
9470 else if (dwarf_version >= 5)
9472 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9473 if (!implicit_consts[ix])
9474 continue;
9475 else
9477 dw_attr_node *other_a
9478 = &(*(*abbrev_die_table)[abbrev_id]->die_attr)[ix];
9479 if (!dw_val_equal_p (&a->dw_attr_val,
9480 &other_a->dw_attr_val))
9481 implicit_consts[ix] = false;
9484 die->die_abbrev = abbrev_id;
9486 gcc_assert (abbrev_id == vec_safe_length (abbrev_die_table) - 1);
9487 if (dwarf_version >= 5 && first_id != ~0U)
9488 optimize_implicit_const (first_id, i, implicit_consts);
9491 abbrev_opt_start = 0;
9492 abbrev_opt_base_type_end = 0;
9493 abbrev_usage_count.release ();
9494 sorted_abbrev_dies.release ();
9497 /* Return the power-of-two number of bytes necessary to represent VALUE. */
9499 static int
9500 constant_size (unsigned HOST_WIDE_INT value)
9502 int log;
9504 if (value == 0)
9505 log = 0;
9506 else
9507 log = floor_log2 (value);
9509 log = log / 8;
9510 log = 1 << (floor_log2 (log) + 1);
9512 return log;
9515 /* Return the size of a DIE as it is represented in the
9516 .debug_info section. */
9518 static unsigned long
9519 size_of_die (dw_die_ref die)
9521 unsigned long size = 0;
9522 dw_attr_node *a;
9523 unsigned ix;
9524 enum dwarf_form form;
9526 size += size_of_uleb128 (die->die_abbrev);
9527 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9529 switch (AT_class (a))
9531 case dw_val_class_addr:
9532 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9534 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9535 size += size_of_uleb128 (AT_index (a));
9537 else
9538 size += DWARF2_ADDR_SIZE;
9539 break;
9540 case dw_val_class_offset:
9541 size += dwarf_offset_size;
9542 break;
9543 case dw_val_class_loc:
9545 unsigned long lsize = size_of_locs (AT_loc (a));
9547 /* Block length. */
9548 if (dwarf_version >= 4)
9549 size += size_of_uleb128 (lsize);
9550 else
9551 size += constant_size (lsize);
9552 size += lsize;
9554 break;
9555 case dw_val_class_loc_list:
9556 if (dwarf_split_debug_info && dwarf_version >= 5)
9558 gcc_assert (AT_loc_list (a)->num_assigned);
9559 size += size_of_uleb128 (AT_loc_list (a)->hash);
9561 else
9562 size += dwarf_offset_size;
9563 break;
9564 case dw_val_class_view_list:
9565 size += dwarf_offset_size;
9566 break;
9567 case dw_val_class_range_list:
9568 if (value_format (a) == DW_FORM_rnglistx)
9570 gcc_assert (rnglist_idx);
9571 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9572 size += size_of_uleb128 (r->idx);
9574 else
9575 size += dwarf_offset_size;
9576 break;
9577 case dw_val_class_const:
9578 size += size_of_sleb128 (AT_int (a));
9579 break;
9580 case dw_val_class_unsigned_const:
9582 int csize = constant_size (AT_unsigned (a));
9583 if (dwarf_version == 3
9584 && a->dw_attr == DW_AT_data_member_location
9585 && csize >= 4)
9586 size += size_of_uleb128 (AT_unsigned (a));
9587 else
9588 size += csize;
9590 break;
9591 case dw_val_class_symview:
9592 if (symview_upper_bound <= 0xff)
9593 size += 1;
9594 else if (symview_upper_bound <= 0xffff)
9595 size += 2;
9596 else if (symview_upper_bound <= 0xffffffff)
9597 size += 4;
9598 else
9599 size += 8;
9600 break;
9601 case dw_val_class_const_implicit:
9602 case dw_val_class_unsigned_const_implicit:
9603 case dw_val_class_file_implicit:
9604 /* These occupy no size in the DIE, just an extra sleb128 in
9605 .debug_abbrev. */
9606 break;
9607 case dw_val_class_const_double:
9608 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
9609 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
9610 size++; /* block */
9611 break;
9612 case dw_val_class_wide_int:
9613 size += (get_full_len (*a->dw_attr_val.v.val_wide)
9614 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
9615 if (get_full_len (*a->dw_attr_val.v.val_wide)
9616 * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
9617 size++; /* block */
9618 break;
9619 case dw_val_class_vec:
9620 size += constant_size (a->dw_attr_val.v.val_vec.length
9621 * a->dw_attr_val.v.val_vec.elt_size)
9622 + a->dw_attr_val.v.val_vec.length
9623 * a->dw_attr_val.v.val_vec.elt_size; /* block */
9624 break;
9625 case dw_val_class_flag:
9626 if (dwarf_version >= 4)
9627 /* Currently all add_AT_flag calls pass in 1 as last argument,
9628 so DW_FORM_flag_present can be used. If that ever changes,
9629 we'll need to use DW_FORM_flag and have some optimization
9630 in build_abbrev_table that will change those to
9631 DW_FORM_flag_present if it is set to 1 in all DIEs using
9632 the same abbrev entry. */
9633 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9634 else
9635 size += 1;
9636 break;
9637 case dw_val_class_die_ref:
9638 if (AT_ref_external (a))
9640 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
9641 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
9642 is sized by target address length, whereas in DWARF3
9643 it's always sized as an offset. */
9644 if (AT_ref (a)->comdat_type_p)
9645 size += DWARF_TYPE_SIGNATURE_SIZE;
9646 else if (dwarf_version == 2)
9647 size += DWARF2_ADDR_SIZE;
9648 else
9649 size += dwarf_offset_size;
9651 else
9652 size += dwarf_offset_size;
9653 break;
9654 case dw_val_class_fde_ref:
9655 size += dwarf_offset_size;
9656 break;
9657 case dw_val_class_lbl_id:
9658 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9660 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9661 size += size_of_uleb128 (AT_index (a));
9663 else
9664 size += DWARF2_ADDR_SIZE;
9665 break;
9666 case dw_val_class_lineptr:
9667 case dw_val_class_macptr:
9668 case dw_val_class_loclistsptr:
9669 size += dwarf_offset_size;
9670 break;
9671 case dw_val_class_str:
9672 form = AT_string_form (a);
9673 if (form == DW_FORM_strp || form == DW_FORM_line_strp)
9674 size += dwarf_offset_size;
9675 else if (form == dwarf_FORM (DW_FORM_strx))
9676 size += size_of_uleb128 (AT_index (a));
9677 else
9678 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
9679 break;
9680 case dw_val_class_file:
9681 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
9682 break;
9683 case dw_val_class_data8:
9684 size += 8;
9685 break;
9686 case dw_val_class_vms_delta:
9687 size += dwarf_offset_size;
9688 break;
9689 case dw_val_class_high_pc:
9690 size += DWARF2_ADDR_SIZE;
9691 break;
9692 case dw_val_class_discr_value:
9693 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
9694 break;
9695 case dw_val_class_discr_list:
9697 unsigned block_size = size_of_discr_list (AT_discr_list (a));
9699 /* This is a block, so we have the block length and then its
9700 data. */
9701 size += constant_size (block_size) + block_size;
9703 break;
9704 default:
9705 gcc_unreachable ();
9709 return size;
9712 /* Size the debugging information associated with a given DIE. Visits the
9713 DIE's children recursively. Updates the global variable next_die_offset, on
9714 each time through. Uses the current value of next_die_offset to update the
9715 die_offset field in each DIE. */
9717 static void
9718 calc_die_sizes (dw_die_ref die)
9720 dw_die_ref c;
9722 gcc_assert (die->die_offset == 0
9723 || (unsigned long int) die->die_offset == next_die_offset);
9724 die->die_offset = next_die_offset;
9725 next_die_offset += size_of_die (die);
9727 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
9729 if (die->die_child != NULL)
9730 /* Count the null byte used to terminate sibling lists. */
9731 next_die_offset += 1;
9734 /* Size just the base type children at the start of the CU.
9735 This is needed because build_abbrev needs to size locs
9736 and sizing of type based stack ops needs to know die_offset
9737 values for the base types. */
9739 static void
9740 calc_base_type_die_sizes (void)
9742 unsigned long die_offset = (dwarf_split_debug_info
9743 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
9744 : DWARF_COMPILE_UNIT_HEADER_SIZE);
9745 unsigned int i;
9746 dw_die_ref base_type;
9747 #if ENABLE_ASSERT_CHECKING
9748 dw_die_ref prev = comp_unit_die ()->die_child;
9749 #endif
9751 die_offset += size_of_die (comp_unit_die ());
9752 for (i = 0; base_types.iterate (i, &base_type); i++)
9754 #if ENABLE_ASSERT_CHECKING
9755 gcc_assert (base_type->die_offset == 0
9756 && prev->die_sib == base_type
9757 && base_type->die_child == NULL
9758 && base_type->die_abbrev);
9759 prev = base_type;
9760 #endif
9761 if (abbrev_opt_start
9762 && base_type->die_abbrev >= abbrev_opt_base_type_end)
9763 abbrev_opt_base_type_end = base_type->die_abbrev + 1;
9764 base_type->die_offset = die_offset;
9765 die_offset += size_of_die (base_type);
9769 /* Set the marks for a die and its children. We do this so
9770 that we know whether or not a reference needs to use FORM_ref_addr; only
9771 DIEs in the same CU will be marked. We used to clear out the offset
9772 and use that as the flag, but ran into ordering problems. */
9774 static void
9775 mark_dies (dw_die_ref die)
9777 dw_die_ref c;
9779 gcc_assert (!die->die_mark);
9781 die->die_mark = 1;
9782 FOR_EACH_CHILD (die, c, mark_dies (c));
9785 /* Clear the marks for a die and its children. */
9787 static void
9788 unmark_dies (dw_die_ref die)
9790 dw_die_ref c;
9792 if (! use_debug_types)
9793 gcc_assert (die->die_mark);
9795 die->die_mark = 0;
9796 FOR_EACH_CHILD (die, c, unmark_dies (c));
9799 /* Clear the marks for a die, its children and referred dies. */
9801 static void
9802 unmark_all_dies (dw_die_ref die)
9804 dw_die_ref c;
9805 dw_attr_node *a;
9806 unsigned ix;
9808 if (!die->die_mark)
9809 return;
9810 die->die_mark = 0;
9812 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
9814 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9815 if (AT_class (a) == dw_val_class_die_ref)
9816 unmark_all_dies (AT_ref (a));
9819 /* Calculate if the entry should appear in the final output file. It may be
9820 from a pruned a type. */
9822 static bool
9823 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
9825 /* By limiting gnu pubnames to definitions only, gold can generate a
9826 gdb index without entries for declarations, which don't include
9827 enough information to be useful. */
9828 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
9829 return false;
9831 if (table == pubname_table)
9833 /* Enumerator names are part of the pubname table, but the
9834 parent DW_TAG_enumeration_type die may have been pruned.
9835 Don't output them if that is the case. */
9836 if (p->die->die_tag == DW_TAG_enumerator &&
9837 (p->die->die_parent == NULL
9838 || !p->die->die_parent->die_perennial_p))
9839 return false;
9841 /* Everything else in the pubname table is included. */
9842 return true;
9845 /* The pubtypes table shouldn't include types that have been
9846 pruned. */
9847 return (p->die->die_offset != 0
9848 || !flag_eliminate_unused_debug_types);
9851 /* Return the size of the .debug_pubnames or .debug_pubtypes table
9852 generated for the compilation unit. */
9854 static unsigned long
9855 size_of_pubnames (vec<pubname_entry, va_gc> *names)
9857 unsigned long size;
9858 unsigned i;
9859 pubname_entry *p;
9860 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
9862 size = DWARF_PUBNAMES_HEADER_SIZE;
9863 FOR_EACH_VEC_ELT (*names, i, p)
9864 if (include_pubname_in_output (names, p))
9865 size += strlen (p->name) + dwarf_offset_size + 1 + space_for_flags;
9867 size += dwarf_offset_size;
9868 return size;
9871 /* Return the size of the information in the .debug_aranges section. */
9873 static unsigned long
9874 size_of_aranges (void)
9876 unsigned long size;
9878 size = DWARF_ARANGES_HEADER_SIZE;
9880 /* Count the address/length pair for this compilation unit. */
9881 if (switch_text_ranges)
9882 size += 2 * DWARF2_ADDR_SIZE
9883 * (vec_safe_length (switch_text_ranges) / 2 + 1);
9884 if (switch_cold_ranges)
9885 size += 2 * DWARF2_ADDR_SIZE
9886 * (vec_safe_length (switch_cold_ranges) / 2 + 1);
9887 if (have_multiple_function_sections)
9889 unsigned fde_idx;
9890 dw_fde_ref fde;
9892 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9894 if (fde->ignored_debug)
9895 continue;
9896 if (!fde->in_std_section)
9897 size += 2 * DWARF2_ADDR_SIZE;
9898 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9899 size += 2 * DWARF2_ADDR_SIZE;
9903 /* Count the two zero words used to terminated the address range table. */
9904 size += 2 * DWARF2_ADDR_SIZE;
9905 return size;
9908 /* Select the encoding of an attribute value. */
9910 static enum dwarf_form
9911 value_format (dw_attr_node *a)
9913 switch (AT_class (a))
9915 case dw_val_class_addr:
9916 /* Only very few attributes allow DW_FORM_addr. */
9917 switch (a->dw_attr)
9919 case DW_AT_low_pc:
9920 case DW_AT_high_pc:
9921 case DW_AT_entry_pc:
9922 case DW_AT_trampoline:
9923 return (AT_index (a) == NOT_INDEXED
9924 ? DW_FORM_addr : dwarf_FORM (DW_FORM_addrx));
9925 default:
9926 break;
9928 switch (DWARF2_ADDR_SIZE)
9930 case 1:
9931 return DW_FORM_data1;
9932 case 2:
9933 return DW_FORM_data2;
9934 case 4:
9935 return DW_FORM_data4;
9936 case 8:
9937 return DW_FORM_data8;
9938 default:
9939 gcc_unreachable ();
9941 case dw_val_class_loc_list:
9942 if (dwarf_split_debug_info
9943 && dwarf_version >= 5
9944 && AT_loc_list (a)->num_assigned)
9945 return DW_FORM_loclistx;
9946 /* FALLTHRU */
9947 case dw_val_class_view_list:
9948 case dw_val_class_range_list:
9949 /* For range lists in DWARF 5, use DW_FORM_rnglistx from .debug_info.dwo
9950 but in .debug_info use DW_FORM_sec_offset, which is shorter if we
9951 care about sizes of .debug* sections in shared libraries and
9952 executables and don't take into account relocations that affect just
9953 relocatable objects - for DW_FORM_rnglistx we'd have to emit offset
9954 table in the .debug_rnglists section. */
9955 if (dwarf_split_debug_info
9956 && dwarf_version >= 5
9957 && AT_class (a) == dw_val_class_range_list
9958 && rnglist_idx
9959 && a->dw_attr_val.val_entry != RELOCATED_OFFSET)
9960 return DW_FORM_rnglistx;
9961 if (dwarf_version >= 4)
9962 return DW_FORM_sec_offset;
9963 /* FALLTHRU */
9964 case dw_val_class_vms_delta:
9965 case dw_val_class_offset:
9966 switch (dwarf_offset_size)
9968 case 4:
9969 return DW_FORM_data4;
9970 case 8:
9971 return DW_FORM_data8;
9972 default:
9973 gcc_unreachable ();
9975 case dw_val_class_loc:
9976 if (dwarf_version >= 4)
9977 return DW_FORM_exprloc;
9978 switch (constant_size (size_of_locs (AT_loc (a))))
9980 case 1:
9981 return DW_FORM_block1;
9982 case 2:
9983 return DW_FORM_block2;
9984 case 4:
9985 return DW_FORM_block4;
9986 default:
9987 gcc_unreachable ();
9989 case dw_val_class_const:
9990 return DW_FORM_sdata;
9991 case dw_val_class_unsigned_const:
9992 switch (constant_size (AT_unsigned (a)))
9994 case 1:
9995 return DW_FORM_data1;
9996 case 2:
9997 return DW_FORM_data2;
9998 case 4:
9999 /* In DWARF3 DW_AT_data_member_location with
10000 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
10001 constant, so we need to use DW_FORM_udata if we need
10002 a large constant. */
10003 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
10004 return DW_FORM_udata;
10005 return DW_FORM_data4;
10006 case 8:
10007 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
10008 return DW_FORM_udata;
10009 return DW_FORM_data8;
10010 default:
10011 gcc_unreachable ();
10013 case dw_val_class_const_implicit:
10014 case dw_val_class_unsigned_const_implicit:
10015 case dw_val_class_file_implicit:
10016 return DW_FORM_implicit_const;
10017 case dw_val_class_const_double:
10018 switch (HOST_BITS_PER_WIDE_INT)
10020 case 8:
10021 return DW_FORM_data2;
10022 case 16:
10023 return DW_FORM_data4;
10024 case 32:
10025 return DW_FORM_data8;
10026 case 64:
10027 if (dwarf_version >= 5)
10028 return DW_FORM_data16;
10029 /* FALLTHRU */
10030 default:
10031 return DW_FORM_block1;
10033 case dw_val_class_wide_int:
10034 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
10036 case 8:
10037 return DW_FORM_data1;
10038 case 16:
10039 return DW_FORM_data2;
10040 case 32:
10041 return DW_FORM_data4;
10042 case 64:
10043 return DW_FORM_data8;
10044 case 128:
10045 if (dwarf_version >= 5)
10046 return DW_FORM_data16;
10047 /* FALLTHRU */
10048 default:
10049 return DW_FORM_block1;
10051 case dw_val_class_symview:
10052 /* ??? We might use uleb128, but then we'd have to compute
10053 .debug_info offsets in the assembler. */
10054 if (symview_upper_bound <= 0xff)
10055 return DW_FORM_data1;
10056 else if (symview_upper_bound <= 0xffff)
10057 return DW_FORM_data2;
10058 else if (symview_upper_bound <= 0xffffffff)
10059 return DW_FORM_data4;
10060 else
10061 return DW_FORM_data8;
10062 case dw_val_class_vec:
10063 switch (constant_size (a->dw_attr_val.v.val_vec.length
10064 * a->dw_attr_val.v.val_vec.elt_size))
10066 case 1:
10067 return DW_FORM_block1;
10068 case 2:
10069 return DW_FORM_block2;
10070 case 4:
10071 return DW_FORM_block4;
10072 default:
10073 gcc_unreachable ();
10075 case dw_val_class_flag:
10076 if (dwarf_version >= 4)
10078 /* Currently all add_AT_flag calls pass in 1 as last argument,
10079 so DW_FORM_flag_present can be used. If that ever changes,
10080 we'll need to use DW_FORM_flag and have some optimization
10081 in build_abbrev_table that will change those to
10082 DW_FORM_flag_present if it is set to 1 in all DIEs using
10083 the same abbrev entry. */
10084 gcc_assert (a->dw_attr_val.v.val_flag == 1);
10085 return DW_FORM_flag_present;
10087 return DW_FORM_flag;
10088 case dw_val_class_die_ref:
10089 if (AT_ref_external (a))
10091 if (AT_ref (a)->comdat_type_p)
10092 return DW_FORM_ref_sig8;
10093 else
10094 return DW_FORM_ref_addr;
10096 else
10097 return DW_FORM_ref;
10098 case dw_val_class_fde_ref:
10099 return DW_FORM_data;
10100 case dw_val_class_lbl_id:
10101 return (AT_index (a) == NOT_INDEXED
10102 ? DW_FORM_addr : dwarf_FORM (DW_FORM_addrx));
10103 case dw_val_class_lineptr:
10104 case dw_val_class_macptr:
10105 case dw_val_class_loclistsptr:
10106 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
10107 case dw_val_class_str:
10108 return AT_string_form (a);
10109 case dw_val_class_file:
10110 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
10112 case 1:
10113 return DW_FORM_data1;
10114 case 2:
10115 return DW_FORM_data2;
10116 case 4:
10117 return DW_FORM_data4;
10118 default:
10119 gcc_unreachable ();
10122 case dw_val_class_data8:
10123 return DW_FORM_data8;
10125 case dw_val_class_high_pc:
10126 switch (DWARF2_ADDR_SIZE)
10128 case 1:
10129 return DW_FORM_data1;
10130 case 2:
10131 return DW_FORM_data2;
10132 case 4:
10133 return DW_FORM_data4;
10134 case 8:
10135 return DW_FORM_data8;
10136 default:
10137 gcc_unreachable ();
10140 case dw_val_class_discr_value:
10141 return (a->dw_attr_val.v.val_discr_value.pos
10142 ? DW_FORM_udata
10143 : DW_FORM_sdata);
10144 case dw_val_class_discr_list:
10145 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
10147 case 1:
10148 return DW_FORM_block1;
10149 case 2:
10150 return DW_FORM_block2;
10151 case 4:
10152 return DW_FORM_block4;
10153 default:
10154 gcc_unreachable ();
10157 default:
10158 gcc_unreachable ();
10162 /* Output the encoding of an attribute value. */
10164 static void
10165 output_value_format (dw_attr_node *a)
10167 enum dwarf_form form = value_format (a);
10169 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
10172 /* Given a die and id, produce the appropriate abbreviations. */
10174 static void
10175 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
10177 unsigned ix;
10178 dw_attr_node *a_attr;
10180 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
10181 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
10182 dwarf_tag_name (abbrev->die_tag));
10184 if (abbrev->die_child != NULL)
10185 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
10186 else
10187 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
10189 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
10191 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
10192 dwarf_attr_name (a_attr->dw_attr));
10193 output_value_format (a_attr);
10194 if (value_format (a_attr) == DW_FORM_implicit_const)
10196 if (AT_class (a_attr) == dw_val_class_file_implicit)
10198 int f = maybe_emit_file (a_attr->dw_attr_val.v.val_file);
10199 const char *filename = a_attr->dw_attr_val.v.val_file->filename;
10200 dw2_asm_output_data_sleb128 (f, "(%s)", filename);
10202 else
10203 dw2_asm_output_data_sleb128 (a_attr->dw_attr_val.v.val_int, NULL);
10207 dw2_asm_output_data (1, 0, NULL);
10208 dw2_asm_output_data (1, 0, NULL);
10212 /* Output the .debug_abbrev section which defines the DIE abbreviation
10213 table. */
10215 static void
10216 output_abbrev_section (void)
10218 unsigned int abbrev_id;
10219 dw_die_ref abbrev;
10221 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
10222 if (abbrev_id != 0)
10223 output_die_abbrevs (abbrev_id, abbrev);
10225 /* Terminate the table. */
10226 dw2_asm_output_data (1, 0, NULL);
10229 /* Return a new location list, given the begin and end range, and the
10230 expression. */
10232 static inline dw_loc_list_ref
10233 new_loc_list (dw_loc_descr_ref expr, const char *begin, var_loc_view vbegin,
10234 const char *end, var_loc_view vend,
10235 const char *section)
10237 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
10239 retlist->begin = begin;
10240 retlist->begin_entry = NULL;
10241 retlist->end = end;
10242 retlist->end_entry = NULL;
10243 retlist->expr = expr;
10244 retlist->section = section;
10245 retlist->vbegin = vbegin;
10246 retlist->vend = vend;
10248 return retlist;
10251 /* Return true iff there's any nonzero view number in the loc list.
10253 ??? When views are not enabled, we'll often extend a single range
10254 to the entire function, so that we emit a single location
10255 expression rather than a location list. With views, even with a
10256 single range, we'll output a list if start or end have a nonzero
10257 view. If we change this, we may want to stop splitting a single
10258 range in dw_loc_list just because of a nonzero view, even if it
10259 straddles across hot/cold partitions. */
10261 static bool
10262 loc_list_has_views (dw_loc_list_ref list)
10264 if (!debug_variable_location_views)
10265 return false;
10267 for (dw_loc_list_ref loc = list;
10268 loc != NULL; loc = loc->dw_loc_next)
10269 if (!ZERO_VIEW_P (loc->vbegin) || !ZERO_VIEW_P (loc->vend))
10270 return true;
10272 return false;
10275 /* Generate a new internal symbol for this location list node, if it
10276 hasn't got one yet. */
10278 static inline void
10279 gen_llsym (dw_loc_list_ref list)
10281 gcc_assert (!list->ll_symbol);
10282 list->ll_symbol = gen_internal_sym ("LLST");
10284 if (!loc_list_has_views (list))
10285 return;
10287 if (dwarf2out_locviews_in_attribute ())
10289 /* Use the same label_num for the view list. */
10290 label_num--;
10291 list->vl_symbol = gen_internal_sym ("LVUS");
10293 else
10294 list->vl_symbol = list->ll_symbol;
10297 /* Generate a symbol for the list, but only if we really want to emit
10298 it as a list. */
10300 static inline void
10301 maybe_gen_llsym (dw_loc_list_ref list)
10303 if (!list || (!list->dw_loc_next && !loc_list_has_views (list)))
10304 return;
10306 gen_llsym (list);
10309 /* Determine whether or not to skip loc_list entry CURR. If SIZEP is
10310 NULL, don't consider size of the location expression. If we're not
10311 to skip it, and SIZEP is non-null, store the size of CURR->expr's
10312 representation in *SIZEP. */
10314 static bool
10315 skip_loc_list_entry (dw_loc_list_ref curr, unsigned long *sizep = NULL)
10317 /* Don't output an entry that starts and ends at the same address. */
10318 if (strcmp (curr->begin, curr->end) == 0
10319 && curr->vbegin == curr->vend && !curr->force)
10320 return true;
10322 if (!sizep)
10323 return false;
10325 unsigned long size = size_of_locs (curr->expr);
10327 /* If the expression is too large, drop it on the floor. We could
10328 perhaps put it into DW_TAG_dwarf_procedure and refer to that
10329 in the expression, but >= 64KB expressions for a single value
10330 in a single range are unlikely very useful. */
10331 if (dwarf_version < 5 && size > 0xffff)
10332 return true;
10334 *sizep = size;
10336 return false;
10339 /* Output a view pair loclist entry for CURR, if it requires one. */
10341 static void
10342 dwarf2out_maybe_output_loclist_view_pair (dw_loc_list_ref curr)
10344 if (!dwarf2out_locviews_in_loclist ())
10345 return;
10347 if (ZERO_VIEW_P (curr->vbegin) && ZERO_VIEW_P (curr->vend))
10348 return;
10350 #ifdef DW_LLE_view_pair
10351 dw2_asm_output_data (1, DW_LLE_view_pair, "DW_LLE_view_pair");
10353 if (dwarf2out_as_locview_support)
10355 if (ZERO_VIEW_P (curr->vbegin))
10356 dw2_asm_output_data_uleb128 (0, "Location view begin");
10357 else
10359 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10360 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vbegin);
10361 dw2_asm_output_symname_uleb128 (label, "Location view begin");
10364 if (ZERO_VIEW_P (curr->vend))
10365 dw2_asm_output_data_uleb128 (0, "Location view end");
10366 else
10368 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10369 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vend);
10370 dw2_asm_output_symname_uleb128 (label, "Location view end");
10373 else
10375 dw2_asm_output_data_uleb128 (curr->vbegin, "Location view begin");
10376 dw2_asm_output_data_uleb128 (curr->vend, "Location view end");
10378 #endif /* DW_LLE_view_pair */
10380 return;
10383 /* Output the location list given to us. */
10385 static void
10386 output_loc_list (dw_loc_list_ref list_head)
10388 int vcount = 0, lcount = 0;
10390 if (list_head->emitted)
10391 return;
10392 list_head->emitted = true;
10394 if (list_head->vl_symbol && dwarf2out_locviews_in_attribute ())
10396 ASM_OUTPUT_LABEL (asm_out_file, list_head->vl_symbol);
10398 for (dw_loc_list_ref curr = list_head; curr != NULL;
10399 curr = curr->dw_loc_next)
10401 unsigned long size;
10403 if (skip_loc_list_entry (curr, &size))
10404 continue;
10406 vcount++;
10408 /* ?? dwarf_split_debug_info? */
10409 if (dwarf2out_as_locview_support)
10411 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10413 if (!ZERO_VIEW_P (curr->vbegin))
10415 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vbegin);
10416 dw2_asm_output_symname_uleb128 (label,
10417 "View list begin (%s)",
10418 list_head->vl_symbol);
10420 else
10421 dw2_asm_output_data_uleb128 (0,
10422 "View list begin (%s)",
10423 list_head->vl_symbol);
10425 if (!ZERO_VIEW_P (curr->vend))
10427 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vend);
10428 dw2_asm_output_symname_uleb128 (label,
10429 "View list end (%s)",
10430 list_head->vl_symbol);
10432 else
10433 dw2_asm_output_data_uleb128 (0,
10434 "View list end (%s)",
10435 list_head->vl_symbol);
10437 else
10439 dw2_asm_output_data_uleb128 (curr->vbegin,
10440 "View list begin (%s)",
10441 list_head->vl_symbol);
10442 dw2_asm_output_data_uleb128 (curr->vend,
10443 "View list end (%s)",
10444 list_head->vl_symbol);
10449 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
10451 const char *last_section = NULL;
10452 const char *base_label = NULL;
10454 /* Walk the location list, and output each range + expression. */
10455 for (dw_loc_list_ref curr = list_head; curr != NULL;
10456 curr = curr->dw_loc_next)
10458 unsigned long size;
10460 /* Skip this entry? If we skip it here, we must skip it in the
10461 view list above as well. */
10462 if (skip_loc_list_entry (curr, &size))
10463 continue;
10465 lcount++;
10467 if (dwarf_version >= 5)
10469 if (dwarf_split_debug_info && HAVE_AS_LEB128)
10471 dwarf2out_maybe_output_loclist_view_pair (curr);
10472 /* For -gsplit-dwarf, emit DW_LLE_startx_length, which has
10473 uleb128 index into .debug_addr and uleb128 length. */
10474 dw2_asm_output_data (1, DW_LLE_startx_length,
10475 "DW_LLE_startx_length (%s)",
10476 list_head->ll_symbol);
10477 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10478 "Location list range start index "
10479 "(%s)", curr->begin);
10480 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
10481 "Location list length (%s)",
10482 list_head->ll_symbol);
10484 else if (dwarf_split_debug_info)
10486 dwarf2out_maybe_output_loclist_view_pair (curr);
10487 /* For -gsplit-dwarf without usable .uleb128 support, emit
10488 DW_LLE_startx_endx, which has two uleb128 indexes into
10489 .debug_addr. */
10490 dw2_asm_output_data (1, DW_LLE_startx_endx,
10491 "DW_LLE_startx_endx (%s)",
10492 list_head->ll_symbol);
10493 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10494 "Location list range start index "
10495 "(%s)", curr->begin);
10496 dw2_asm_output_data_uleb128 (curr->end_entry->index,
10497 "Location list range end index "
10498 "(%s)", curr->end);
10500 else if (!have_multiple_function_sections && HAVE_AS_LEB128)
10502 dwarf2out_maybe_output_loclist_view_pair (curr);
10503 /* If all code is in .text section, the base address is
10504 already provided by the CU attributes. Use
10505 DW_LLE_offset_pair where both addresses are uleb128 encoded
10506 offsets against that base. */
10507 dw2_asm_output_data (1, DW_LLE_offset_pair,
10508 "DW_LLE_offset_pair (%s)",
10509 list_head->ll_symbol);
10510 dw2_asm_output_delta_uleb128 (curr->begin, curr->section,
10511 "Location list begin address (%s)",
10512 list_head->ll_symbol);
10513 dw2_asm_output_delta_uleb128 (curr->end, curr->section,
10514 "Location list end address (%s)",
10515 list_head->ll_symbol);
10517 else if (HAVE_AS_LEB128)
10519 /* Otherwise, find out how many consecutive entries could share
10520 the same base entry. If just one, emit DW_LLE_start_length,
10521 otherwise emit DW_LLE_base_address for the base address
10522 followed by a series of DW_LLE_offset_pair. */
10523 if (last_section == NULL || curr->section != last_section)
10525 dw_loc_list_ref curr2;
10526 for (curr2 = curr->dw_loc_next; curr2 != NULL;
10527 curr2 = curr2->dw_loc_next)
10529 if (strcmp (curr2->begin, curr2->end) == 0
10530 && !curr2->force)
10531 continue;
10532 break;
10534 if (curr2 == NULL || curr->section != curr2->section)
10535 last_section = NULL;
10536 else
10538 last_section = curr->section;
10539 base_label = curr->begin;
10540 dw2_asm_output_data (1, DW_LLE_base_address,
10541 "DW_LLE_base_address (%s)",
10542 list_head->ll_symbol);
10543 dw2_asm_output_addr (DWARF2_ADDR_SIZE, base_label,
10544 "Base address (%s)",
10545 list_head->ll_symbol);
10548 /* Only one entry with the same base address. Use
10549 DW_LLE_start_length with absolute address and uleb128
10550 length. */
10551 if (last_section == NULL)
10553 dwarf2out_maybe_output_loclist_view_pair (curr);
10554 dw2_asm_output_data (1, DW_LLE_start_length,
10555 "DW_LLE_start_length (%s)",
10556 list_head->ll_symbol);
10557 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10558 "Location list begin address (%s)",
10559 list_head->ll_symbol);
10560 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
10561 "Location list length "
10562 "(%s)", list_head->ll_symbol);
10564 /* Otherwise emit DW_LLE_offset_pair, relative to above emitted
10565 DW_LLE_base_address. */
10566 else
10568 dwarf2out_maybe_output_loclist_view_pair (curr);
10569 dw2_asm_output_data (1, DW_LLE_offset_pair,
10570 "DW_LLE_offset_pair (%s)",
10571 list_head->ll_symbol);
10572 dw2_asm_output_delta_uleb128 (curr->begin, base_label,
10573 "Location list begin address "
10574 "(%s)", list_head->ll_symbol);
10575 dw2_asm_output_delta_uleb128 (curr->end, base_label,
10576 "Location list end address "
10577 "(%s)", list_head->ll_symbol);
10580 /* The assembler does not support .uleb128 directive. Emit
10581 DW_LLE_start_end with a pair of absolute addresses. */
10582 else
10584 dwarf2out_maybe_output_loclist_view_pair (curr);
10585 dw2_asm_output_data (1, DW_LLE_start_end,
10586 "DW_LLE_start_end (%s)",
10587 list_head->ll_symbol);
10588 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10589 "Location list begin address (%s)",
10590 list_head->ll_symbol);
10591 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10592 "Location list end address (%s)",
10593 list_head->ll_symbol);
10596 else if (dwarf_split_debug_info)
10598 /* For -gsplit-dwarf -gdwarf-{2,3,4} emit index into .debug_addr
10599 and 4 byte length. */
10600 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
10601 "Location list start/length entry (%s)",
10602 list_head->ll_symbol);
10603 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10604 "Location list range start index (%s)",
10605 curr->begin);
10606 /* The length field is 4 bytes. If we ever need to support
10607 an 8-byte length, we can add a new DW_LLE code or fall back
10608 to DW_LLE_GNU_start_end_entry. */
10609 dw2_asm_output_delta (4, curr->end, curr->begin,
10610 "Location list range length (%s)",
10611 list_head->ll_symbol);
10613 else if (!have_multiple_function_sections)
10615 /* Pair of relative addresses against start of text section. */
10616 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
10617 "Location list begin address (%s)",
10618 list_head->ll_symbol);
10619 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
10620 "Location list end address (%s)",
10621 list_head->ll_symbol);
10623 else
10625 /* Pair of absolute addresses. */
10626 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10627 "Location list begin address (%s)",
10628 list_head->ll_symbol);
10629 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10630 "Location list end address (%s)",
10631 list_head->ll_symbol);
10634 /* Output the block length for this list of location operations. */
10635 if (dwarf_version >= 5)
10636 dw2_asm_output_data_uleb128 (size, "Location expression size");
10637 else
10639 gcc_assert (size <= 0xffff);
10640 dw2_asm_output_data (2, size, "Location expression size");
10643 output_loc_sequence (curr->expr, -1);
10646 /* And finally list termination. */
10647 if (dwarf_version >= 5)
10648 dw2_asm_output_data (1, DW_LLE_end_of_list,
10649 "DW_LLE_end_of_list (%s)", list_head->ll_symbol);
10650 else if (dwarf_split_debug_info)
10651 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
10652 "Location list terminator (%s)",
10653 list_head->ll_symbol);
10654 else
10656 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10657 "Location list terminator begin (%s)",
10658 list_head->ll_symbol);
10659 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10660 "Location list terminator end (%s)",
10661 list_head->ll_symbol);
10664 gcc_assert (!list_head->vl_symbol
10665 || vcount == lcount * (dwarf2out_locviews_in_attribute () ? 1 : 0));
10668 /* Output a range_list offset into the .debug_ranges or .debug_rnglists
10669 section. Emit a relocated reference if val_entry is NULL, otherwise,
10670 emit an indirect reference. */
10672 static void
10673 output_range_list_offset (dw_attr_node *a)
10675 const char *name = dwarf_attr_name (a->dw_attr);
10677 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
10679 if (dwarf_version >= 5)
10681 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
10682 dw2_asm_output_offset (dwarf_offset_size, r->label,
10683 debug_ranges_section, "%s", name);
10685 else
10687 char *p = strchr (ranges_section_label, '\0');
10688 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
10689 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE);
10690 dw2_asm_output_offset (dwarf_offset_size, ranges_section_label,
10691 debug_ranges_section, "%s", name);
10692 *p = '\0';
10695 else if (dwarf_version >= 5)
10697 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
10698 gcc_assert (rnglist_idx);
10699 dw2_asm_output_data_uleb128 (r->idx, "%s", name);
10701 else
10702 dw2_asm_output_data (dwarf_offset_size,
10703 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE,
10704 "%s (offset from %s)", name, ranges_section_label);
10707 /* Output the offset into the debug_loc section. */
10709 static void
10710 output_loc_list_offset (dw_attr_node *a)
10712 char *sym = AT_loc_list (a)->ll_symbol;
10714 gcc_assert (sym);
10715 if (!dwarf_split_debug_info)
10716 dw2_asm_output_offset (dwarf_offset_size, sym, debug_loc_section,
10717 "%s", dwarf_attr_name (a->dw_attr));
10718 else if (dwarf_version >= 5)
10720 gcc_assert (AT_loc_list (a)->num_assigned);
10721 dw2_asm_output_data_uleb128 (AT_loc_list (a)->hash, "%s (%s)",
10722 dwarf_attr_name (a->dw_attr),
10723 sym);
10725 else
10726 dw2_asm_output_delta (dwarf_offset_size, sym, loc_section_label,
10727 "%s", dwarf_attr_name (a->dw_attr));
10730 /* Output the offset into the debug_loc section. */
10732 static void
10733 output_view_list_offset (dw_attr_node *a)
10735 char *sym = (*AT_loc_list_ptr (a))->vl_symbol;
10737 gcc_assert (sym);
10738 if (dwarf_split_debug_info)
10739 dw2_asm_output_delta (dwarf_offset_size, sym, loc_section_label,
10740 "%s", dwarf_attr_name (a->dw_attr));
10741 else
10742 dw2_asm_output_offset (dwarf_offset_size, sym, debug_loc_section,
10743 "%s", dwarf_attr_name (a->dw_attr));
10746 /* Output an attribute's index or value appropriately. */
10748 static void
10749 output_attr_index_or_value (dw_attr_node *a)
10751 const char *name = dwarf_attr_name (a->dw_attr);
10753 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
10755 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
10756 return;
10758 switch (AT_class (a))
10760 case dw_val_class_addr:
10761 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
10762 break;
10763 case dw_val_class_high_pc:
10764 case dw_val_class_lbl_id:
10765 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
10766 break;
10767 default:
10768 gcc_unreachable ();
10772 /* Output a type signature. */
10774 static inline void
10775 output_signature (const char *sig, const char *name)
10777 int i;
10779 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10780 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
10783 /* Output a discriminant value. */
10785 static inline void
10786 output_discr_value (dw_discr_value *discr_value, const char *name)
10788 if (discr_value->pos)
10789 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
10790 else
10791 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
10794 /* Output the DIE and its attributes. Called recursively to generate
10795 the definitions of each child DIE. */
10797 static void
10798 output_die (dw_die_ref die)
10800 dw_attr_node *a;
10801 dw_die_ref c;
10802 unsigned long size;
10803 unsigned ix;
10805 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
10806 (unsigned long)die->die_offset,
10807 dwarf_tag_name (die->die_tag));
10809 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
10811 const char *name = dwarf_attr_name (a->dw_attr);
10813 switch (AT_class (a))
10815 case dw_val_class_addr:
10816 output_attr_index_or_value (a);
10817 break;
10819 case dw_val_class_offset:
10820 dw2_asm_output_data (dwarf_offset_size, a->dw_attr_val.v.val_offset,
10821 "%s", name);
10822 break;
10824 case dw_val_class_range_list:
10825 output_range_list_offset (a);
10826 break;
10828 case dw_val_class_loc:
10829 size = size_of_locs (AT_loc (a));
10831 /* Output the block length for this list of location operations. */
10832 if (dwarf_version >= 4)
10833 dw2_asm_output_data_uleb128 (size, "%s", name);
10834 else
10835 dw2_asm_output_data (constant_size (size), size, "%s", name);
10837 output_loc_sequence (AT_loc (a), -1);
10838 break;
10840 case dw_val_class_const:
10841 /* ??? It would be slightly more efficient to use a scheme like is
10842 used for unsigned constants below, but gdb 4.x does not sign
10843 extend. Gdb 5.x does sign extend. */
10844 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10845 break;
10847 case dw_val_class_unsigned_const:
10849 int csize = constant_size (AT_unsigned (a));
10850 if (dwarf_version == 3
10851 && a->dw_attr == DW_AT_data_member_location
10852 && csize >= 4)
10853 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
10854 else
10855 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
10857 break;
10859 case dw_val_class_symview:
10861 int vsize;
10862 if (symview_upper_bound <= 0xff)
10863 vsize = 1;
10864 else if (symview_upper_bound <= 0xffff)
10865 vsize = 2;
10866 else if (symview_upper_bound <= 0xffffffff)
10867 vsize = 4;
10868 else
10869 vsize = 8;
10870 dw2_asm_output_addr (vsize, a->dw_attr_val.v.val_symbolic_view,
10871 "%s", name);
10873 break;
10875 case dw_val_class_const_implicit:
10876 if (flag_debug_asm)
10877 fprintf (asm_out_file, "\t\t\t%s %s ("
10878 HOST_WIDE_INT_PRINT_DEC ")\n",
10879 ASM_COMMENT_START, name, AT_int (a));
10880 break;
10882 case dw_val_class_unsigned_const_implicit:
10883 if (flag_debug_asm)
10884 fprintf (asm_out_file, "\t\t\t%s %s ("
10885 HOST_WIDE_INT_PRINT_HEX ")\n",
10886 ASM_COMMENT_START, name, AT_unsigned (a));
10887 break;
10889 case dw_val_class_const_double:
10891 unsigned HOST_WIDE_INT first, second;
10893 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
10894 dw2_asm_output_data (1,
10895 HOST_BITS_PER_DOUBLE_INT
10896 / HOST_BITS_PER_CHAR,
10897 NULL);
10899 if (WORDS_BIG_ENDIAN)
10901 first = a->dw_attr_val.v.val_double.high;
10902 second = a->dw_attr_val.v.val_double.low;
10904 else
10906 first = a->dw_attr_val.v.val_double.low;
10907 second = a->dw_attr_val.v.val_double.high;
10910 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10911 first, "%s", name);
10912 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10913 second, NULL);
10915 break;
10917 case dw_val_class_wide_int:
10919 int i;
10920 int len = get_full_len (*a->dw_attr_val.v.val_wide);
10921 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10922 if (len * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
10923 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide)
10924 * l, NULL);
10926 if (WORDS_BIG_ENDIAN)
10927 for (i = len - 1; i >= 0; --i)
10929 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10930 "%s", name);
10931 name = "";
10933 else
10934 for (i = 0; i < len; ++i)
10936 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10937 "%s", name);
10938 name = "";
10941 break;
10943 case dw_val_class_vec:
10945 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10946 unsigned int len = a->dw_attr_val.v.val_vec.length;
10947 unsigned int i;
10948 unsigned char *p;
10950 dw2_asm_output_data (constant_size (len * elt_size),
10951 len * elt_size, "%s", name);
10952 if (elt_size > sizeof (HOST_WIDE_INT))
10954 elt_size /= 2;
10955 len *= 2;
10957 for (i = 0, p = (unsigned char *) a->dw_attr_val.v.val_vec.array;
10958 i < len;
10959 i++, p += elt_size)
10960 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10961 "fp or vector constant word %u", i);
10962 break;
10965 case dw_val_class_flag:
10966 if (dwarf_version >= 4)
10968 /* Currently all add_AT_flag calls pass in 1 as last argument,
10969 so DW_FORM_flag_present can be used. If that ever changes,
10970 we'll need to use DW_FORM_flag and have some optimization
10971 in build_abbrev_table that will change those to
10972 DW_FORM_flag_present if it is set to 1 in all DIEs using
10973 the same abbrev entry. */
10974 gcc_assert (AT_flag (a) == 1);
10975 if (flag_debug_asm)
10976 fprintf (asm_out_file, "\t\t\t%s %s\n",
10977 ASM_COMMENT_START, name);
10978 break;
10980 dw2_asm_output_data (1, AT_flag (a), "%s", name);
10981 break;
10983 case dw_val_class_loc_list:
10984 output_loc_list_offset (a);
10985 break;
10987 case dw_val_class_view_list:
10988 output_view_list_offset (a);
10989 break;
10991 case dw_val_class_die_ref:
10992 if (AT_ref_external (a))
10994 if (AT_ref (a)->comdat_type_p)
10996 comdat_type_node *type_node
10997 = AT_ref (a)->die_id.die_type_node;
10999 gcc_assert (type_node);
11000 output_signature (type_node->signature, name);
11002 else
11004 const char *sym = AT_ref (a)->die_id.die_symbol;
11005 int size;
11007 gcc_assert (sym);
11008 /* In DWARF2, DW_FORM_ref_addr is sized by target address
11009 length, whereas in DWARF3 it's always sized as an
11010 offset. */
11011 if (dwarf_version == 2)
11012 size = DWARF2_ADDR_SIZE;
11013 else
11014 size = dwarf_offset_size;
11015 /* ??? We cannot unconditionally output die_offset if
11016 non-zero - others might create references to those
11017 DIEs via symbols.
11018 And we do not clear its DIE offset after outputting it
11019 (and the label refers to the actual DIEs, not the
11020 DWARF CU unit header which is when using label + offset
11021 would be the correct thing to do).
11022 ??? This is the reason for the with_offset flag. */
11023 if (AT_ref (a)->with_offset)
11024 dw2_asm_output_offset (size, sym, AT_ref (a)->die_offset,
11025 debug_info_section, "%s", name);
11026 else
11027 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
11028 name);
11031 else
11033 gcc_assert (AT_ref (a)->die_offset);
11034 dw2_asm_output_data (dwarf_offset_size, AT_ref (a)->die_offset,
11035 "%s", name);
11037 break;
11039 case dw_val_class_fde_ref:
11041 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
11043 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
11044 a->dw_attr_val.v.val_fde_index * 2);
11045 dw2_asm_output_offset (dwarf_offset_size, l1, debug_frame_section,
11046 "%s", name);
11048 break;
11050 case dw_val_class_vms_delta:
11051 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
11052 dw2_asm_output_vms_delta (dwarf_offset_size,
11053 AT_vms_delta2 (a), AT_vms_delta1 (a),
11054 "%s", name);
11055 #else
11056 dw2_asm_output_delta (dwarf_offset_size,
11057 AT_vms_delta2 (a), AT_vms_delta1 (a),
11058 "%s", name);
11059 #endif
11060 break;
11062 case dw_val_class_lbl_id:
11063 output_attr_index_or_value (a);
11064 break;
11066 case dw_val_class_lineptr:
11067 dw2_asm_output_offset (dwarf_offset_size, AT_lbl (a),
11068 debug_line_section, "%s", name);
11069 break;
11071 case dw_val_class_macptr:
11072 dw2_asm_output_offset (dwarf_offset_size, AT_lbl (a),
11073 debug_macinfo_section, "%s", name);
11074 break;
11076 case dw_val_class_loclistsptr:
11077 dw2_asm_output_offset (dwarf_offset_size, AT_lbl (a),
11078 debug_loc_section, "%s", name);
11079 break;
11081 case dw_val_class_str:
11082 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
11083 dw2_asm_output_offset (dwarf_offset_size,
11084 a->dw_attr_val.v.val_str->label,
11085 debug_str_section,
11086 "%s: \"%s\"", name, AT_string (a));
11087 else if (a->dw_attr_val.v.val_str->form == DW_FORM_line_strp)
11088 dw2_asm_output_offset (dwarf_offset_size,
11089 a->dw_attr_val.v.val_str->label,
11090 debug_line_str_section,
11091 "%s: \"%s\"", name, AT_string (a));
11092 else if (a->dw_attr_val.v.val_str->form == dwarf_FORM (DW_FORM_strx))
11093 dw2_asm_output_data_uleb128 (AT_index (a),
11094 "%s: \"%s\"", name, AT_string (a));
11095 else
11096 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
11097 break;
11099 case dw_val_class_file:
11101 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
11103 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
11104 a->dw_attr_val.v.val_file->filename);
11105 break;
11108 case dw_val_class_file_implicit:
11109 if (flag_debug_asm)
11110 fprintf (asm_out_file, "\t\t\t%s %s (%d, %s)\n",
11111 ASM_COMMENT_START, name,
11112 maybe_emit_file (a->dw_attr_val.v.val_file),
11113 a->dw_attr_val.v.val_file->filename);
11114 break;
11116 case dw_val_class_data8:
11118 int i;
11120 for (i = 0; i < 8; i++)
11121 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
11122 i == 0 ? "%s" : NULL, name);
11123 break;
11126 case dw_val_class_high_pc:
11127 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
11128 get_AT_low_pc (die), "DW_AT_high_pc");
11129 break;
11131 case dw_val_class_discr_value:
11132 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
11133 break;
11135 case dw_val_class_discr_list:
11137 dw_discr_list_ref list = AT_discr_list (a);
11138 const int size = size_of_discr_list (list);
11140 /* This is a block, so output its length first. */
11141 dw2_asm_output_data (constant_size (size), size,
11142 "%s: block size", name);
11144 for (; list != NULL; list = list->dw_discr_next)
11146 /* One byte for the discriminant value descriptor, and then as
11147 many LEB128 numbers as required. */
11148 if (list->dw_discr_range)
11149 dw2_asm_output_data (1, DW_DSC_range,
11150 "%s: DW_DSC_range", name);
11151 else
11152 dw2_asm_output_data (1, DW_DSC_label,
11153 "%s: DW_DSC_label", name);
11155 output_discr_value (&list->dw_discr_lower_bound, name);
11156 if (list->dw_discr_range)
11157 output_discr_value (&list->dw_discr_upper_bound, name);
11159 break;
11162 default:
11163 gcc_unreachable ();
11167 FOR_EACH_CHILD (die, c, output_die (c));
11169 /* Add null byte to terminate sibling list. */
11170 if (die->die_child != NULL)
11171 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
11172 (unsigned long) die->die_offset);
11175 /* Output the dwarf version number. */
11177 static void
11178 output_dwarf_version ()
11180 /* ??? For now, if -gdwarf-6 is specified, we output version 5 with
11181 views in loclist. That will change eventually. */
11182 if (dwarf_version == 6)
11184 static bool once;
11185 if (!once)
11187 warning (0, "%<-gdwarf-6%> is output as version 5 with "
11188 "incompatibilities");
11189 once = true;
11191 dw2_asm_output_data (2, 5, "DWARF version number");
11193 else
11194 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
11197 /* Output the compilation unit that appears at the beginning of the
11198 .debug_info section, and precedes the DIE descriptions. */
11200 static void
11201 output_compilation_unit_header (enum dwarf_unit_type ut)
11203 if (!XCOFF_DEBUGGING_INFO)
11205 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11206 dw2_asm_output_data (4, 0xffffffff,
11207 "Initial length escape value indicating 64-bit DWARF extension");
11208 dw2_asm_output_data (dwarf_offset_size,
11209 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
11210 "Length of Compilation Unit Info");
11213 output_dwarf_version ();
11214 if (dwarf_version >= 5)
11216 const char *name;
11217 switch (ut)
11219 case DW_UT_compile: name = "DW_UT_compile"; break;
11220 case DW_UT_type: name = "DW_UT_type"; break;
11221 case DW_UT_split_compile: name = "DW_UT_split_compile"; break;
11222 case DW_UT_split_type: name = "DW_UT_split_type"; break;
11223 default: gcc_unreachable ();
11225 dw2_asm_output_data (1, ut, "%s", name);
11226 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11228 dw2_asm_output_offset (dwarf_offset_size, abbrev_section_label,
11229 debug_abbrev_section,
11230 "Offset Into Abbrev. Section");
11231 if (dwarf_version < 5)
11232 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11235 /* Output the compilation unit DIE and its children. */
11237 static void
11238 output_comp_unit (dw_die_ref die, int output_if_empty,
11239 const unsigned char *dwo_id)
11241 const char *secname, *oldsym;
11242 char *tmp;
11244 /* Unless we are outputting main CU, we may throw away empty ones. */
11245 if (!output_if_empty && die->die_child == NULL)
11246 return;
11248 /* Even if there are no children of this DIE, we must output the information
11249 about the compilation unit. Otherwise, on an empty translation unit, we
11250 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
11251 will then complain when examining the file. First mark all the DIEs in
11252 this CU so we know which get local refs. */
11253 mark_dies (die);
11255 external_ref_hash_type *extern_map = optimize_external_refs (die);
11257 /* For now, optimize only the main CU, in order to optimize the rest
11258 we'd need to see all of them earlier. Leave the rest for post-linking
11259 tools like DWZ. */
11260 if (die == comp_unit_die ())
11261 abbrev_opt_start = vec_safe_length (abbrev_die_table);
11263 build_abbrev_table (die, extern_map);
11265 optimize_abbrev_table ();
11267 delete extern_map;
11269 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
11270 next_die_offset = (dwo_id
11271 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
11272 : DWARF_COMPILE_UNIT_HEADER_SIZE);
11273 calc_die_sizes (die);
11275 oldsym = die->die_id.die_symbol;
11276 if (oldsym && die->comdat_type_p)
11278 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11280 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
11281 secname = tmp;
11282 die->die_id.die_symbol = NULL;
11283 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11285 else
11287 switch_to_section (debug_info_section);
11288 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
11289 info_section_emitted = true;
11292 /* For LTO cross unit DIE refs we want a symbol on the start of the
11293 debuginfo section, not on the CU DIE. */
11294 if ((flag_generate_lto || flag_generate_offload) && oldsym)
11296 /* ??? No way to get visibility assembled without a decl. */
11297 tree decl = build_decl (UNKNOWN_LOCATION, VAR_DECL,
11298 get_identifier (oldsym), char_type_node);
11299 TREE_PUBLIC (decl) = true;
11300 TREE_STATIC (decl) = true;
11301 DECL_ARTIFICIAL (decl) = true;
11302 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
11303 DECL_VISIBILITY_SPECIFIED (decl) = true;
11304 targetm.asm_out.assemble_visibility (decl, VISIBILITY_HIDDEN);
11305 #ifdef ASM_WEAKEN_LABEL
11306 /* We prefer a .weak because that handles duplicates from duplicate
11307 archive members in a graceful way. */
11308 ASM_WEAKEN_LABEL (asm_out_file, oldsym);
11309 #else
11310 targetm.asm_out.globalize_label (asm_out_file, oldsym);
11311 #endif
11312 ASM_OUTPUT_LABEL (asm_out_file, oldsym);
11315 /* Output debugging information. */
11316 output_compilation_unit_header (dwo_id
11317 ? DW_UT_split_compile : DW_UT_compile);
11318 if (dwarf_version >= 5)
11320 if (dwo_id != NULL)
11321 for (int i = 0; i < 8; i++)
11322 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
11324 output_die (die);
11326 /* Leave the marks on the main CU, so we can check them in
11327 output_pubnames. */
11328 if (oldsym)
11330 unmark_dies (die);
11331 die->die_id.die_symbol = oldsym;
11335 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
11336 and .debug_pubtypes. This is configured per-target, but can be
11337 overridden by the -gpubnames or -gno-pubnames options. */
11339 static inline bool
11340 want_pubnames (void)
11342 if (debug_info_level <= DINFO_LEVEL_TERSE
11343 /* Names and types go to the early debug part only. */
11344 || in_lto_p)
11345 return false;
11346 if (debug_generate_pub_sections != -1)
11347 return debug_generate_pub_sections;
11348 return targetm.want_debug_pub_sections;
11351 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
11353 static void
11354 add_AT_pubnames (dw_die_ref die)
11356 if (want_pubnames ())
11357 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
11360 /* Add a string attribute value to a skeleton DIE. */
11362 static inline void
11363 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
11364 const char *str)
11366 dw_attr_node attr;
11367 struct indirect_string_node *node;
11369 if (! skeleton_debug_str_hash)
11370 skeleton_debug_str_hash
11371 = hash_table<indirect_string_hasher>::create_ggc (10);
11373 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
11374 find_string_form (node);
11375 if (node->form == dwarf_FORM (DW_FORM_strx))
11376 node->form = DW_FORM_strp;
11378 attr.dw_attr = attr_kind;
11379 attr.dw_attr_val.val_class = dw_val_class_str;
11380 attr.dw_attr_val.val_entry = NULL;
11381 attr.dw_attr_val.v.val_str = node;
11382 add_dwarf_attr (die, &attr);
11385 /* Helper function to generate top-level dies for skeleton debug_info and
11386 debug_types. */
11388 static void
11389 add_top_level_skeleton_die_attrs (dw_die_ref die)
11391 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
11392 const char *comp_dir = comp_dir_string ();
11394 add_skeleton_AT_string (die, dwarf_AT (DW_AT_dwo_name), dwo_file_name);
11395 if (comp_dir != NULL)
11396 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
11397 add_AT_pubnames (die);
11398 if (addr_index_table != NULL && addr_index_table->size () > 0)
11399 add_AT_lineptr (die, dwarf_AT (DW_AT_addr_base), debug_addr_section_label);
11402 /* Output skeleton debug sections that point to the dwo file. */
11404 static void
11405 output_skeleton_debug_sections (dw_die_ref comp_unit,
11406 const unsigned char *dwo_id)
11408 /* These attributes will be found in the full debug_info section. */
11409 remove_AT (comp_unit, DW_AT_producer);
11410 remove_AT (comp_unit, DW_AT_language);
11412 switch_to_section (debug_skeleton_info_section);
11413 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
11415 /* Produce the skeleton compilation-unit header. This one differs enough from
11416 a normal CU header that it's better not to call output_compilation_unit
11417 header. */
11418 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11419 dw2_asm_output_data (4, 0xffffffff,
11420 "Initial length escape value indicating 64-bit "
11421 "DWARF extension");
11423 dw2_asm_output_data (dwarf_offset_size,
11424 DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
11425 - DWARF_INITIAL_LENGTH_SIZE
11426 + size_of_die (comp_unit),
11427 "Length of Compilation Unit Info");
11428 output_dwarf_version ();
11429 if (dwarf_version >= 5)
11431 dw2_asm_output_data (1, DW_UT_skeleton, "DW_UT_skeleton");
11432 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11434 dw2_asm_output_offset (dwarf_offset_size, debug_skeleton_abbrev_section_label,
11435 debug_skeleton_abbrev_section,
11436 "Offset Into Abbrev. Section");
11437 if (dwarf_version < 5)
11438 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11439 else
11440 for (int i = 0; i < 8; i++)
11441 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
11443 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
11444 output_die (comp_unit);
11446 /* Build the skeleton debug_abbrev section. */
11447 switch_to_section (debug_skeleton_abbrev_section);
11448 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
11450 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
11452 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
11455 /* Output a comdat type unit DIE and its children. */
11457 static void
11458 output_comdat_type_unit (comdat_type_node *node,
11459 bool early_lto_debug ATTRIBUTE_UNUSED)
11461 const char *secname;
11462 char *tmp;
11463 int i;
11464 #if defined (OBJECT_FORMAT_ELF)
11465 tree comdat_key;
11466 #endif
11468 /* First mark all the DIEs in this CU so we know which get local refs. */
11469 mark_dies (node->root_die);
11471 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
11473 build_abbrev_table (node->root_die, extern_map);
11475 delete extern_map;
11476 extern_map = NULL;
11478 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
11479 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11480 calc_die_sizes (node->root_die);
11482 #if defined (OBJECT_FORMAT_ELF)
11483 if (dwarf_version >= 5)
11485 if (!dwarf_split_debug_info)
11486 secname = early_lto_debug ? DEBUG_LTO_INFO_SECTION : DEBUG_INFO_SECTION;
11487 else
11488 secname = (early_lto_debug
11489 ? DEBUG_LTO_DWO_INFO_SECTION : DEBUG_DWO_INFO_SECTION);
11491 else if (!dwarf_split_debug_info)
11492 secname = early_lto_debug ? ".gnu.debuglto_.debug_types" : ".debug_types";
11493 else
11494 secname = (early_lto_debug
11495 ? ".gnu.debuglto_.debug_types.dwo" : ".debug_types.dwo");
11497 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11498 sprintf (tmp, dwarf_version >= 5 ? "wi." : "wt.");
11499 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11500 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11501 comdat_key = get_identifier (tmp);
11502 targetm.asm_out.named_section (secname,
11503 SECTION_DEBUG | SECTION_LINKONCE,
11504 comdat_key);
11505 #else
11506 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11507 sprintf (tmp, (dwarf_version >= 5
11508 ? ".gnu.linkonce.wi." : ".gnu.linkonce.wt."));
11509 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11510 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11511 secname = tmp;
11512 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11513 #endif
11515 /* Output debugging information. */
11516 output_compilation_unit_header (dwarf_split_debug_info
11517 ? DW_UT_split_type : DW_UT_type);
11518 output_signature (node->signature, "Type Signature");
11519 dw2_asm_output_data (dwarf_offset_size, node->type_die->die_offset,
11520 "Offset to Type DIE");
11521 output_die (node->root_die);
11523 unmark_dies (node->root_die);
11526 /* Return the DWARF2/3 pubname associated with a decl. */
11528 static const char *
11529 dwarf2_name (tree decl, int scope)
11531 if (DECL_NAMELESS (decl))
11532 return NULL;
11533 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11536 /* Add a new entry to .debug_pubnames if appropriate. */
11538 static void
11539 add_pubname_string (const char *str, dw_die_ref die)
11541 pubname_entry e;
11543 e.die = die;
11544 e.name = xstrdup (str);
11545 vec_safe_push (pubname_table, e);
11548 static void
11549 add_pubname (tree decl, dw_die_ref die)
11551 if (!want_pubnames ())
11552 return;
11554 /* Don't add items to the table when we expect that the consumer will have
11555 just read the enclosing die. For example, if the consumer is looking at a
11556 class_member, it will either be inside the class already, or will have just
11557 looked up the class to find the member. Either way, searching the class is
11558 faster than searching the index. */
11559 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
11560 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
11562 const char *name = dwarf2_name (decl, 1);
11564 if (name)
11565 add_pubname_string (name, die);
11569 /* Add an enumerator to the pubnames section. */
11571 static void
11572 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
11574 pubname_entry e;
11576 gcc_assert (scope_name);
11577 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
11578 e.die = die;
11579 vec_safe_push (pubname_table, e);
11582 /* Add a new entry to .debug_pubtypes if appropriate. */
11584 static void
11585 add_pubtype (tree decl, dw_die_ref die)
11587 pubname_entry e;
11589 if (!want_pubnames ())
11590 return;
11592 if ((TREE_PUBLIC (decl)
11593 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
11594 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11596 tree scope = NULL;
11597 const char *scope_name = "";
11598 const char *sep = is_cxx () ? "::" : ".";
11599 const char *name;
11601 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
11602 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
11604 scope_name = lang_hooks.dwarf_name (scope, 1);
11605 if (scope_name != NULL && scope_name[0] != '\0')
11606 scope_name = concat (scope_name, sep, NULL);
11607 else
11608 scope_name = "";
11611 if (TYPE_P (decl))
11612 name = type_tag (decl);
11613 else
11614 name = lang_hooks.dwarf_name (decl, 1);
11616 /* If we don't have a name for the type, there's no point in adding
11617 it to the table. */
11618 if (name != NULL && name[0] != '\0')
11620 e.die = die;
11621 e.name = concat (scope_name, name, NULL);
11622 vec_safe_push (pubtype_table, e);
11625 /* Although it might be more consistent to add the pubinfo for the
11626 enumerators as their dies are created, they should only be added if the
11627 enum type meets the criteria above. So rather than re-check the parent
11628 enum type whenever an enumerator die is created, just output them all
11629 here. This isn't protected by the name conditional because anonymous
11630 enums don't have names. */
11631 if (die->die_tag == DW_TAG_enumeration_type)
11633 dw_die_ref c;
11635 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
11640 /* Output a single entry in the pubnames table. */
11642 static void
11643 output_pubname (dw_offset die_offset, pubname_entry *entry)
11645 dw_die_ref die = entry->die;
11646 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
11648 dw2_asm_output_data (dwarf_offset_size, die_offset, "DIE offset");
11650 if (debug_generate_pub_sections == 2)
11652 /* This logic follows gdb's method for determining the value of the flag
11653 byte. */
11654 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
11655 switch (die->die_tag)
11657 case DW_TAG_typedef:
11658 case DW_TAG_base_type:
11659 case DW_TAG_subrange_type:
11660 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11661 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11662 break;
11663 case DW_TAG_enumerator:
11664 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11665 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11666 if (!is_cxx ())
11667 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11668 break;
11669 case DW_TAG_subprogram:
11670 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11671 GDB_INDEX_SYMBOL_KIND_FUNCTION);
11672 if (!is_ada ())
11673 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11674 break;
11675 case DW_TAG_constant:
11676 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11677 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11678 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11679 break;
11680 case DW_TAG_variable:
11681 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11682 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11683 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11684 break;
11685 case DW_TAG_namespace:
11686 case DW_TAG_imported_declaration:
11687 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11688 break;
11689 case DW_TAG_class_type:
11690 case DW_TAG_interface_type:
11691 case DW_TAG_structure_type:
11692 case DW_TAG_union_type:
11693 case DW_TAG_enumeration_type:
11694 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11695 if (!is_cxx ())
11696 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11697 break;
11698 default:
11699 /* An unusual tag. Leave the flag-byte empty. */
11700 break;
11702 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
11703 "GDB-index flags");
11706 dw2_asm_output_nstring (entry->name, -1, "external name");
11710 /* Output the public names table used to speed up access to externally
11711 visible names; or the public types table used to find type definitions. */
11713 static void
11714 output_pubnames (vec<pubname_entry, va_gc> *names)
11716 unsigned i;
11717 unsigned long pubnames_length = size_of_pubnames (names);
11718 pubname_entry *pub;
11720 if (!XCOFF_DEBUGGING_INFO)
11722 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11723 dw2_asm_output_data (4, 0xffffffff,
11724 "Initial length escape value indicating 64-bit DWARF extension");
11725 dw2_asm_output_data (dwarf_offset_size, pubnames_length,
11726 "Pub Info Length");
11729 /* Version number for pubnames/pubtypes is independent of dwarf version. */
11730 dw2_asm_output_data (2, 2, "DWARF pubnames/pubtypes version");
11732 if (dwarf_split_debug_info)
11733 dw2_asm_output_offset (dwarf_offset_size, debug_skeleton_info_section_label,
11734 debug_skeleton_info_section,
11735 "Offset of Compilation Unit Info");
11736 else
11737 dw2_asm_output_offset (dwarf_offset_size, debug_info_section_label,
11738 debug_info_section,
11739 "Offset of Compilation Unit Info");
11740 dw2_asm_output_data (dwarf_offset_size, next_die_offset,
11741 "Compilation Unit Length");
11743 FOR_EACH_VEC_ELT (*names, i, pub)
11745 if (include_pubname_in_output (names, pub))
11747 dw_offset die_offset = pub->die->die_offset;
11749 /* We shouldn't see pubnames for DIEs outside of the main CU. */
11750 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
11751 gcc_assert (pub->die->die_mark);
11753 /* If we're putting types in their own .debug_types sections,
11754 the .debug_pubtypes table will still point to the compile
11755 unit (not the type unit), so we want to use the offset of
11756 the skeleton DIE (if there is one). */
11757 if (pub->die->comdat_type_p && names == pubtype_table)
11759 comdat_type_node *type_node = pub->die->die_id.die_type_node;
11761 if (type_node != NULL)
11762 die_offset = (type_node->skeleton_die != NULL
11763 ? type_node->skeleton_die->die_offset
11764 : comp_unit_die ()->die_offset);
11767 output_pubname (die_offset, pub);
11771 dw2_asm_output_data (dwarf_offset_size, 0, NULL);
11774 /* Output public names and types tables if necessary. */
11776 static void
11777 output_pubtables (void)
11779 if (!want_pubnames () || !info_section_emitted)
11780 return;
11782 switch_to_section (debug_pubnames_section);
11783 output_pubnames (pubname_table);
11784 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
11785 It shouldn't hurt to emit it always, since pure DWARF2 consumers
11786 simply won't look for the section. */
11787 switch_to_section (debug_pubtypes_section);
11788 output_pubnames (pubtype_table);
11792 /* Output the information that goes into the .debug_aranges table.
11793 Namely, define the beginning and ending address range of the
11794 text section generated for this compilation unit. */
11796 static void
11797 output_aranges (void)
11799 unsigned i;
11800 unsigned long aranges_length = size_of_aranges ();
11802 if (!XCOFF_DEBUGGING_INFO)
11804 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11805 dw2_asm_output_data (4, 0xffffffff,
11806 "Initial length escape value indicating 64-bit DWARF extension");
11807 dw2_asm_output_data (dwarf_offset_size, aranges_length,
11808 "Length of Address Ranges Info");
11811 /* Version number for aranges is still 2, even up to DWARF5. */
11812 dw2_asm_output_data (2, 2, "DWARF aranges version");
11813 if (dwarf_split_debug_info)
11814 dw2_asm_output_offset (dwarf_offset_size, debug_skeleton_info_section_label,
11815 debug_skeleton_info_section,
11816 "Offset of Compilation Unit Info");
11817 else
11818 dw2_asm_output_offset (dwarf_offset_size, debug_info_section_label,
11819 debug_info_section,
11820 "Offset of Compilation Unit Info");
11821 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11822 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11824 /* We need to align to twice the pointer size here. */
11825 if (DWARF_ARANGES_PAD_SIZE)
11827 /* Pad using a 2 byte words so that padding is correct for any
11828 pointer size. */
11829 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11830 2 * DWARF2_ADDR_SIZE);
11831 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11832 dw2_asm_output_data (2, 0, NULL);
11835 /* It is necessary not to output these entries if the sections were
11836 not used; if the sections were not used, the length will be 0 and
11837 the address may end up as 0 if the section is discarded by ld
11838 --gc-sections, leaving an invalid (0, 0) entry that can be
11839 confused with the terminator. */
11840 if (switch_text_ranges)
11842 const char *prev_loc = text_section_label;
11843 const char *loc;
11844 unsigned idx;
11846 FOR_EACH_VEC_ELT (*switch_text_ranges, idx, loc)
11847 if (prev_loc)
11849 dw2_asm_output_addr (DWARF2_ADDR_SIZE, prev_loc, "Address");
11850 dw2_asm_output_delta (DWARF2_ADDR_SIZE, loc, prev_loc, "Length");
11851 prev_loc = NULL;
11853 else
11854 prev_loc = loc;
11856 if (prev_loc)
11858 dw2_asm_output_addr (DWARF2_ADDR_SIZE, prev_loc, "Address");
11859 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11860 prev_loc, "Length");
11864 if (switch_cold_ranges)
11866 const char *prev_loc = cold_text_section_label;
11867 const char *loc;
11868 unsigned idx;
11870 FOR_EACH_VEC_ELT (*switch_cold_ranges, idx, loc)
11871 if (prev_loc)
11873 dw2_asm_output_addr (DWARF2_ADDR_SIZE, prev_loc, "Address");
11874 dw2_asm_output_delta (DWARF2_ADDR_SIZE, loc, prev_loc, "Length");
11875 prev_loc = NULL;
11877 else
11878 prev_loc = loc;
11880 if (prev_loc)
11882 dw2_asm_output_addr (DWARF2_ADDR_SIZE, prev_loc, "Address");
11883 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11884 prev_loc, "Length");
11888 if (have_multiple_function_sections)
11890 unsigned fde_idx;
11891 dw_fde_ref fde;
11893 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
11895 if (fde->ignored_debug)
11896 continue;
11897 if (!fde->in_std_section)
11899 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
11900 "Address");
11901 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
11902 fde->dw_fde_begin, "Length");
11904 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
11906 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
11907 "Address");
11908 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
11909 fde->dw_fde_second_begin, "Length");
11914 /* Output the terminator words. */
11915 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11916 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11919 /* Add a new entry to .debug_ranges. Return its index into
11920 ranges_table vector. */
11922 static unsigned int
11923 add_ranges_num (int num, bool maybe_new_sec)
11925 dw_ranges r = { NULL, num, 0, maybe_new_sec, NULL, NULL };
11926 vec_safe_push (ranges_table, r);
11927 return vec_safe_length (ranges_table) - 1;
11930 /* Add a new entry to .debug_ranges corresponding to a block, or a
11931 range terminator if BLOCK is NULL. MAYBE_NEW_SEC is true if
11932 this entry might be in a different section from previous range. */
11934 static unsigned int
11935 add_ranges (const_tree block, bool maybe_new_sec)
11937 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0, maybe_new_sec);
11940 /* Note that (*rnglist_table)[offset] is either a head of a rnglist
11941 chain, or middle entry of a chain that will be directly referred to. */
11943 static void
11944 note_rnglist_head (unsigned int offset)
11946 if (dwarf_version < 5 || (*ranges_table)[offset].label)
11947 return;
11948 (*ranges_table)[offset].label = gen_internal_sym ("LLRL");
11951 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
11952 When using dwarf_split_debug_info, address attributes in dies destined
11953 for the final executable should be direct references--setting the
11954 parameter force_direct ensures this behavior. */
11956 static void
11957 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11958 bool *added, bool force_direct)
11960 unsigned int in_use = vec_safe_length (ranges_by_label);
11961 unsigned int offset;
11962 dw_ranges_by_label rbl = { begin, end };
11963 vec_safe_push (ranges_by_label, rbl);
11964 offset = add_ranges_num (-(int)in_use - 1, true);
11965 if (!*added)
11967 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
11968 *added = true;
11969 note_rnglist_head (offset);
11970 if (dwarf_split_debug_info && force_direct)
11971 (*ranges_table)[offset].idx = DW_RANGES_IDX_SKELETON;
11975 /* Emit .debug_ranges section. */
11977 static void
11978 output_ranges (void)
11980 unsigned i;
11981 static const char *const start_fmt = "Offset %#x";
11982 const char *fmt = start_fmt;
11983 dw_ranges *r;
11985 switch_to_section (debug_ranges_section);
11986 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11987 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11989 int block_num = r->num;
11991 if (block_num > 0)
11993 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11994 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11996 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11997 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11999 /* If all code is in the text section, then the compilation
12000 unit base address defaults to DW_AT_low_pc, which is the
12001 base of the text section. */
12002 if (!have_multiple_function_sections)
12004 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
12005 text_section_label,
12006 fmt, i * 2 * DWARF2_ADDR_SIZE);
12007 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
12008 text_section_label, NULL);
12011 /* Otherwise, the compilation unit base address is zero,
12012 which allows us to use absolute addresses, and not worry
12013 about whether the target supports cross-section
12014 arithmetic. */
12015 else
12017 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12018 fmt, i * 2 * DWARF2_ADDR_SIZE);
12019 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
12022 fmt = NULL;
12025 /* Negative block_num stands for an index into ranges_by_label. */
12026 else if (block_num < 0)
12028 int lab_idx = - block_num - 1;
12030 if (!have_multiple_function_sections)
12032 gcc_unreachable ();
12033 #if 0
12034 /* If we ever use add_ranges_by_labels () for a single
12035 function section, all we have to do is to take out
12036 the #if 0 above. */
12037 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
12038 (*ranges_by_label)[lab_idx].begin,
12039 text_section_label,
12040 fmt, i * 2 * DWARF2_ADDR_SIZE);
12041 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
12042 (*ranges_by_label)[lab_idx].end,
12043 text_section_label, NULL);
12044 #endif
12046 else
12048 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
12049 (*ranges_by_label)[lab_idx].begin,
12050 fmt, i * 2 * DWARF2_ADDR_SIZE);
12051 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
12052 (*ranges_by_label)[lab_idx].end,
12053 NULL);
12056 else
12058 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
12059 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
12060 fmt = start_fmt;
12065 /* Non-zero if .debug_line_str should be used for .debug_line section
12066 strings or strings that are likely shareable with those. */
12067 #define DWARF5_USE_DEBUG_LINE_STR \
12068 (!DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET \
12069 && (DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) != 0 \
12070 /* FIXME: there is no .debug_line_str.dwo section, \
12071 for -gsplit-dwarf we should use DW_FORM_strx instead. */ \
12072 && !dwarf_split_debug_info)
12075 /* Returns TRUE if we are outputting DWARF5 and the assembler supports
12076 DWARF5 .debug_line tables using .debug_line_str or we generate
12077 it ourselves, except for split-dwarf which doesn't have a
12078 .debug_line_str. */
12079 static bool
12080 asm_outputs_debug_line_str (void)
12082 if (dwarf_version >= 5
12083 && ! output_asm_line_debug_info ()
12084 && DWARF5_USE_DEBUG_LINE_STR)
12085 return true;
12086 else
12088 #if defined(HAVE_AS_GDWARF_5_DEBUG_FLAG) && defined(HAVE_AS_WORKING_DWARF_N_FLAG)
12089 return !dwarf_split_debug_info && dwarf_version >= 5;
12090 #else
12091 return false;
12092 #endif
12096 /* Return true if it is beneficial to use DW_RLE_base_address{,x}.
12097 I is index of the following range. */
12099 static bool
12100 use_distinct_base_address_for_range (unsigned int i)
12102 if (i >= vec_safe_length (ranges_table))
12103 return false;
12105 dw_ranges *r2 = &(*ranges_table)[i];
12106 /* Use DW_RLE_base_address{,x} if there is a next range in the
12107 range list and is guaranteed to be in the same section. */
12108 return r2->num != 0 && r2->label == NULL && !r2->maybe_new_sec;
12111 /* Assign .debug_rnglists indexes and unique indexes into the debug_addr
12112 section when needed. */
12114 static void
12115 index_rnglists (void)
12117 unsigned i;
12118 dw_ranges *r;
12119 bool base = false;
12121 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
12123 if (r->label && r->idx != DW_RANGES_IDX_SKELETON)
12124 r->idx = rnglist_idx++;
12126 int block_num = r->num;
12127 if ((HAVE_AS_LEB128 || block_num < 0)
12128 && !have_multiple_function_sections)
12129 continue;
12130 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
12131 base = false;
12132 if (block_num > 0)
12134 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
12135 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
12137 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
12138 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
12140 if (HAVE_AS_LEB128)
12142 if (!base && use_distinct_base_address_for_range (i + 1))
12144 r->begin_entry = add_addr_table_entry (xstrdup (blabel),
12145 ate_kind_label);
12146 base = true;
12148 if (base)
12149 /* If we have a base, no need for further
12150 begin_entry/end_entry, as DW_RLE_offset_pair will be
12151 used. */
12152 continue;
12153 r->begin_entry
12154 = add_addr_table_entry (xstrdup (blabel), ate_kind_label);
12155 /* No need for end_entry, DW_RLE_start{,x}_length will use
12156 length as opposed to a pair of addresses. */
12158 else
12160 r->begin_entry
12161 = add_addr_table_entry (xstrdup (blabel), ate_kind_label);
12162 r->end_entry
12163 = add_addr_table_entry (xstrdup (elabel), ate_kind_label);
12167 /* Negative block_num stands for an index into ranges_by_label. */
12168 else if (block_num < 0)
12170 int lab_idx = - block_num - 1;
12171 const char *blabel = (*ranges_by_label)[lab_idx].begin;
12172 const char *elabel = (*ranges_by_label)[lab_idx].end;
12174 r->begin_entry
12175 = add_addr_table_entry (xstrdup (blabel), ate_kind_label);
12176 if (!HAVE_AS_LEB128)
12177 r->end_entry
12178 = add_addr_table_entry (xstrdup (elabel), ate_kind_label);
12183 /* Emit .debug_rnglists or (when DWO is true) .debug_rnglists.dwo section. */
12185 static bool
12186 output_rnglists (unsigned generation, bool dwo)
12188 unsigned i;
12189 dw_ranges *r;
12190 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
12191 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
12192 char basebuf[MAX_ARTIFICIAL_LABEL_BYTES];
12194 if (dwo)
12195 switch_to_section (debug_ranges_dwo_section);
12196 else
12198 switch_to_section (debug_ranges_section);
12199 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
12201 /* There are up to 4 unique ranges labels per generation.
12202 See also init_sections_and_labels. */
12203 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_RANGES_SECTION_LABEL,
12204 2 + 2 * dwo + generation * 6);
12205 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_RANGES_SECTION_LABEL,
12206 3 + 2 * dwo + generation * 6);
12207 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
12208 dw2_asm_output_data (4, 0xffffffff,
12209 "Initial length escape value indicating "
12210 "64-bit DWARF extension");
12211 dw2_asm_output_delta (dwarf_offset_size, l2, l1,
12212 "Length of Range Lists");
12213 ASM_OUTPUT_LABEL (asm_out_file, l1);
12214 output_dwarf_version ();
12215 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
12216 dw2_asm_output_data (1, 0, "Segment Size");
12217 /* Emit the offset table only for -gsplit-dwarf. If we don't care
12218 about relocation sizes and primarily care about the size of .debug*
12219 sections in linked shared libraries and executables, then
12220 the offset table plus corresponding DW_FORM_rnglistx uleb128 indexes
12221 into it are usually larger than just DW_FORM_sec_offset offsets
12222 into the .debug_rnglists section. */
12223 dw2_asm_output_data (4, dwo ? rnglist_idx : 0,
12224 "Offset Entry Count");
12225 if (dwo)
12227 ASM_OUTPUT_LABEL (asm_out_file, ranges_base_label);
12228 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
12229 if (r->label && r->idx != DW_RANGES_IDX_SKELETON)
12230 dw2_asm_output_delta (dwarf_offset_size, r->label,
12231 ranges_base_label, NULL);
12234 const char *lab = "";
12235 const char *base = NULL;
12236 bool skipping = false;
12237 bool ret = false;
12238 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
12240 int block_num = r->num;
12242 if (r->label)
12244 if (dwarf_split_debug_info
12245 && (r->idx == DW_RANGES_IDX_SKELETON) == dwo)
12247 ret = true;
12248 skipping = true;
12249 continue;
12251 ASM_OUTPUT_LABEL (asm_out_file, r->label);
12252 lab = r->label;
12254 if (skipping)
12256 if (block_num == 0)
12257 skipping = false;
12258 continue;
12260 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
12261 base = NULL;
12262 if (block_num > 0)
12264 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
12265 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
12267 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
12268 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
12270 if (HAVE_AS_LEB128)
12272 /* If all code is in the text section, then the compilation
12273 unit base address defaults to DW_AT_low_pc, which is the
12274 base of the text section. */
12275 if (!have_multiple_function_sections)
12277 dw2_asm_output_data (1, DW_RLE_offset_pair,
12278 "DW_RLE_offset_pair (%s)", lab);
12279 dw2_asm_output_delta_uleb128 (blabel, text_section_label,
12280 "Range begin address (%s)", lab);
12281 dw2_asm_output_delta_uleb128 (elabel, text_section_label,
12282 "Range end address (%s)", lab);
12283 continue;
12285 if (base == NULL && use_distinct_base_address_for_range (i + 1))
12287 if (dwarf_split_debug_info)
12289 dw2_asm_output_data (1, DW_RLE_base_addressx,
12290 "DW_RLE_base_addressx (%s)", lab);
12291 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12292 "Base address index (%s)",
12293 blabel);
12295 else
12297 dw2_asm_output_data (1, DW_RLE_base_address,
12298 "DW_RLE_base_address (%s)", lab);
12299 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12300 "Base address (%s)", lab);
12302 strcpy (basebuf, blabel);
12303 base = basebuf;
12305 if (base)
12307 dw2_asm_output_data (1, DW_RLE_offset_pair,
12308 "DW_RLE_offset_pair (%s)", lab);
12309 dw2_asm_output_delta_uleb128 (blabel, base,
12310 "Range begin address (%s)", lab);
12311 dw2_asm_output_delta_uleb128 (elabel, base,
12312 "Range end address (%s)", lab);
12313 continue;
12315 if (dwarf_split_debug_info)
12317 dw2_asm_output_data (1, DW_RLE_startx_length,
12318 "DW_RLE_startx_length (%s)", lab);
12319 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12320 "Range begin address index "
12321 "(%s)", blabel);
12323 else
12325 dw2_asm_output_data (1, DW_RLE_start_length,
12326 "DW_RLE_start_length (%s)", lab);
12327 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12328 "Range begin address (%s)", lab);
12330 dw2_asm_output_delta_uleb128 (elabel, blabel,
12331 "Range length (%s)", lab);
12333 else if (dwarf_split_debug_info)
12335 dw2_asm_output_data (1, DW_RLE_startx_endx,
12336 "DW_RLE_startx_endx (%s)", lab);
12337 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12338 "Range begin address index "
12339 "(%s)", blabel);
12340 dw2_asm_output_data_uleb128 (r->end_entry->index,
12341 "Range end address index "
12342 "(%s)", elabel);
12344 else
12346 dw2_asm_output_data (1, DW_RLE_start_end,
12347 "DW_RLE_start_end (%s)", lab);
12348 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12349 "Range begin address (%s)", lab);
12350 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
12351 "Range end address (%s)", lab);
12355 /* Negative block_num stands for an index into ranges_by_label. */
12356 else if (block_num < 0)
12358 int lab_idx = - block_num - 1;
12359 const char *blabel = (*ranges_by_label)[lab_idx].begin;
12360 const char *elabel = (*ranges_by_label)[lab_idx].end;
12362 if (!have_multiple_function_sections)
12363 gcc_unreachable ();
12364 if (HAVE_AS_LEB128)
12366 if (dwarf_split_debug_info)
12368 dw2_asm_output_data (1, DW_RLE_startx_length,
12369 "DW_RLE_startx_length (%s)", lab);
12370 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12371 "Range begin address index "
12372 "(%s)", blabel);
12374 else
12376 dw2_asm_output_data (1, DW_RLE_start_length,
12377 "DW_RLE_start_length (%s)", lab);
12378 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12379 "Range begin address (%s)", lab);
12381 dw2_asm_output_delta_uleb128 (elabel, blabel,
12382 "Range length (%s)", lab);
12384 else if (dwarf_split_debug_info)
12386 dw2_asm_output_data (1, DW_RLE_startx_endx,
12387 "DW_RLE_startx_endx (%s)", lab);
12388 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12389 "Range begin address index "
12390 "(%s)", blabel);
12391 dw2_asm_output_data_uleb128 (r->end_entry->index,
12392 "Range end address index "
12393 "(%s)", elabel);
12395 else
12397 dw2_asm_output_data (1, DW_RLE_start_end,
12398 "DW_RLE_start_end (%s)", lab);
12399 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12400 "Range begin address (%s)", lab);
12401 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
12402 "Range end address (%s)", lab);
12405 else
12406 dw2_asm_output_data (1, DW_RLE_end_of_list,
12407 "DW_RLE_end_of_list (%s)", lab);
12409 ASM_OUTPUT_LABEL (asm_out_file, l2);
12410 return ret;
12413 /* Data structure containing information about input files. */
12414 struct file_info
12416 const char *path; /* Complete file name. */
12417 const char *fname; /* File name part. */
12418 int length; /* Length of entire string. */
12419 struct dwarf_file_data * file_idx; /* Index in input file table. */
12420 int dir_idx; /* Index in directory table. */
12423 /* Data structure containing information about directories with source
12424 files. */
12425 struct dir_info
12427 const char *path; /* Path including directory name. */
12428 int length; /* Path length. */
12429 int prefix; /* Index of directory entry which is a prefix. */
12430 int count; /* Number of files in this directory. */
12431 int dir_idx; /* Index of directory used as base. */
12434 /* Callback function for file_info comparison. We sort by looking at
12435 the directories in the path. */
12437 static int
12438 file_info_cmp (const void *p1, const void *p2)
12440 const struct file_info *const s1 = (const struct file_info *) p1;
12441 const struct file_info *const s2 = (const struct file_info *) p2;
12442 const unsigned char *cp1;
12443 const unsigned char *cp2;
12445 /* Take care of file names without directories. We need to make sure that
12446 we return consistent values to qsort since some will get confused if
12447 we return the same value when identical operands are passed in opposite
12448 orders. So if neither has a directory, return 0 and otherwise return
12449 1 or -1 depending on which one has the directory. We want the one with
12450 the directory to sort after the one without, so all no directory files
12451 are at the start (normally only the compilation unit file). */
12452 if ((s1->path == s1->fname || s2->path == s2->fname))
12453 return (s2->path == s2->fname) - (s1->path == s1->fname);
12455 cp1 = (const unsigned char *) s1->path;
12456 cp2 = (const unsigned char *) s2->path;
12458 while (1)
12460 ++cp1;
12461 ++cp2;
12462 /* Reached the end of the first path? If so, handle like above,
12463 but now we want longer directory prefixes before shorter ones. */
12464 if ((cp1 == (const unsigned char *) s1->fname)
12465 || (cp2 == (const unsigned char *) s2->fname))
12466 return ((cp1 == (const unsigned char *) s1->fname)
12467 - (cp2 == (const unsigned char *) s2->fname));
12469 /* Character of current path component the same? */
12470 else if (*cp1 != *cp2)
12471 return *cp1 - *cp2;
12475 struct file_name_acquire_data
12477 struct file_info *files;
12478 int used_files;
12479 int max_files;
12482 /* Traversal function for the hash table. */
12485 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
12487 struct dwarf_file_data *d = *slot;
12488 struct file_info *fi;
12489 const char *f;
12491 gcc_assert (fnad->max_files >= d->emitted_number);
12493 if (! d->emitted_number)
12494 return 1;
12496 gcc_assert (fnad->max_files != fnad->used_files);
12498 fi = fnad->files + fnad->used_files++;
12500 f = d->filename;
12502 /* Skip all leading "./". */
12503 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
12504 f += 2;
12506 /* Create a new array entry. */
12507 fi->path = f;
12508 fi->length = strlen (f);
12509 fi->file_idx = d;
12511 /* Search for the file name part. */
12512 f = strrchr (f, DIR_SEPARATOR);
12513 #if defined (DIR_SEPARATOR_2)
12515 const char *g = strrchr (fi->path, DIR_SEPARATOR_2);
12517 if (g != NULL)
12519 if (f == NULL || f < g)
12520 f = g;
12523 #endif
12525 fi->fname = f == NULL ? fi->path : f + 1;
12526 return 1;
12529 /* Helper function for output_file_names. Emit a FORM encoded
12530 string STR, with assembly comment start ENTRY_KIND and
12531 index IDX */
12533 static void
12534 output_line_string (enum dwarf_form form, const char *str,
12535 const char *entry_kind, unsigned int idx)
12537 switch (form)
12539 case DW_FORM_string:
12540 dw2_asm_output_nstring (str, -1, "%s: %#x", entry_kind, idx);
12541 break;
12542 case DW_FORM_line_strp:
12543 if (!debug_line_str_hash)
12544 debug_line_str_hash
12545 = hash_table<indirect_string_hasher>::create_ggc (10);
12547 struct indirect_string_node *node;
12548 node = find_AT_string_in_table (str, debug_line_str_hash);
12549 set_indirect_string (node);
12550 node->form = form;
12551 dw2_asm_output_offset (dwarf_offset_size, node->label,
12552 debug_line_str_section, "%s: %#x: \"%s\"",
12553 entry_kind, 0, node->str);
12554 break;
12555 default:
12556 gcc_unreachable ();
12560 /* Output the directory table and the file name table. We try to minimize
12561 the total amount of memory needed. A heuristic is used to avoid large
12562 slowdowns with many input files. */
12564 static void
12565 output_file_names (void)
12567 struct file_name_acquire_data fnad;
12568 int numfiles;
12569 struct file_info *files;
12570 struct dir_info *dirs;
12571 int *saved;
12572 int *savehere;
12573 int *backmap;
12574 int ndirs;
12575 int idx_offset;
12576 int i;
12578 if (!last_emitted_file)
12580 if (dwarf_version >= 5)
12582 const char *comp_dir = comp_dir_string ();
12583 if (comp_dir == NULL)
12584 comp_dir = "";
12585 dw2_asm_output_data (1, 1, "Directory entry format count");
12586 enum dwarf_form str_form = DW_FORM_string;
12587 if (DWARF5_USE_DEBUG_LINE_STR)
12588 str_form = DW_FORM_line_strp;
12589 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12590 dw2_asm_output_data_uleb128 (str_form, "%s",
12591 get_DW_FORM_name (str_form));
12592 dw2_asm_output_data_uleb128 (1, "Directories count");
12593 if (str_form == DW_FORM_string)
12594 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
12595 else
12596 output_line_string (str_form, comp_dir, "Directory Entry", 0);
12597 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
12598 if (filename0 == NULL)
12599 filename0 = "";
12600 #ifdef VMS_DEBUGGING_INFO
12601 dw2_asm_output_data (1, 4, "File name entry format count");
12602 #else
12603 dw2_asm_output_data (1, 2, "File name entry format count");
12604 #endif
12605 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12606 dw2_asm_output_data_uleb128 (str_form, "%s",
12607 get_DW_FORM_name (str_form));
12608 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
12609 "DW_LNCT_directory_index");
12610 dw2_asm_output_data_uleb128 (DW_FORM_data1, "%s",
12611 get_DW_FORM_name (DW_FORM_data1));
12612 #ifdef VMS_DEBUGGING_INFO
12613 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
12614 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12615 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
12616 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12617 #endif
12618 dw2_asm_output_data_uleb128 (1, "File names count");
12620 output_line_string (str_form, filename0, "File Entry", 0);
12621 dw2_asm_output_data (1, 0, NULL);
12622 #ifdef VMS_DEBUGGING_INFO
12623 dw2_asm_output_data_uleb128 (0, NULL);
12624 dw2_asm_output_data_uleb128 (0, NULL);
12625 #endif
12627 else
12629 dw2_asm_output_data (1, 0, "End directory table");
12630 dw2_asm_output_data (1, 0, "End file name table");
12632 return;
12635 numfiles = last_emitted_file->emitted_number;
12637 /* Allocate the various arrays we need. */
12638 files = XALLOCAVEC (struct file_info, numfiles);
12639 dirs = XALLOCAVEC (struct dir_info, numfiles);
12641 fnad.files = files;
12642 fnad.used_files = 0;
12643 fnad.max_files = numfiles;
12644 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
12645 gcc_assert (fnad.used_files == fnad.max_files);
12647 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
12649 /* Find all the different directories used. */
12650 dirs[0].path = files[0].path;
12651 dirs[0].length = files[0].fname - files[0].path;
12652 dirs[0].prefix = -1;
12653 dirs[0].count = 1;
12654 dirs[0].dir_idx = 0;
12655 files[0].dir_idx = 0;
12656 ndirs = 1;
12658 for (i = 1; i < numfiles; i++)
12659 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
12660 && memcmp (dirs[ndirs - 1].path, files[i].path,
12661 dirs[ndirs - 1].length) == 0)
12663 /* Same directory as last entry. */
12664 files[i].dir_idx = ndirs - 1;
12665 ++dirs[ndirs - 1].count;
12667 else
12669 int j;
12671 /* This is a new directory. */
12672 dirs[ndirs].path = files[i].path;
12673 dirs[ndirs].length = files[i].fname - files[i].path;
12674 dirs[ndirs].count = 1;
12675 dirs[ndirs].dir_idx = ndirs;
12676 files[i].dir_idx = ndirs;
12678 /* Search for a prefix. */
12679 dirs[ndirs].prefix = -1;
12680 for (j = 0; j < ndirs; j++)
12681 if (dirs[j].length < dirs[ndirs].length
12682 && dirs[j].length > 1
12683 && (dirs[ndirs].prefix == -1
12684 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
12685 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
12686 dirs[ndirs].prefix = j;
12688 ++ndirs;
12691 /* Now to the actual work. We have to find a subset of the directories which
12692 allow expressing the file name using references to the directory table
12693 with the least amount of characters. We do not do an exhaustive search
12694 where we would have to check out every combination of every single
12695 possible prefix. Instead we use a heuristic which provides nearly optimal
12696 results in most cases and never is much off. */
12697 saved = XALLOCAVEC (int, ndirs);
12698 savehere = XALLOCAVEC (int, ndirs);
12700 memset (saved, '\0', ndirs * sizeof (saved[0]));
12701 for (i = 0; i < ndirs; i++)
12703 int j;
12704 int total;
12706 /* We can always save some space for the current directory. But this
12707 does not mean it will be enough to justify adding the directory. */
12708 savehere[i] = dirs[i].length;
12709 total = (savehere[i] - saved[i]) * dirs[i].count;
12711 for (j = i + 1; j < ndirs; j++)
12713 savehere[j] = 0;
12714 if (saved[j] < dirs[i].length)
12716 /* Determine whether the dirs[i] path is a prefix of the
12717 dirs[j] path. */
12718 int k;
12720 k = dirs[j].prefix;
12721 while (k != -1 && k != (int) i)
12722 k = dirs[k].prefix;
12724 if (k == (int) i)
12726 /* Yes it is. We can possibly save some memory by
12727 writing the filenames in dirs[j] relative to
12728 dirs[i]. */
12729 savehere[j] = dirs[i].length;
12730 total += (savehere[j] - saved[j]) * dirs[j].count;
12735 /* Check whether we can save enough to justify adding the dirs[i]
12736 directory. */
12737 if (total > dirs[i].length + 1)
12739 /* It's worthwhile adding. */
12740 for (j = i; j < ndirs; j++)
12741 if (savehere[j] > 0)
12743 /* Remember how much we saved for this directory so far. */
12744 saved[j] = savehere[j];
12746 /* Remember the prefix directory. */
12747 dirs[j].dir_idx = i;
12752 /* Emit the directory name table. */
12753 idx_offset = dirs[0].length > 0 ? 1 : 0;
12754 enum dwarf_form str_form = DW_FORM_string;
12755 enum dwarf_form idx_form = DW_FORM_udata;
12756 if (dwarf_version >= 5)
12758 const char *comp_dir = comp_dir_string ();
12759 if (comp_dir == NULL)
12760 comp_dir = "";
12761 dw2_asm_output_data (1, 1, "Directory entry format count");
12762 if (DWARF5_USE_DEBUG_LINE_STR)
12763 str_form = DW_FORM_line_strp;
12764 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12765 dw2_asm_output_data_uleb128 (str_form, "%s",
12766 get_DW_FORM_name (str_form));
12767 dw2_asm_output_data_uleb128 (ndirs + idx_offset, "Directories count");
12768 if (str_form == DW_FORM_string)
12770 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
12771 for (i = 1 - idx_offset; i < ndirs; i++)
12772 dw2_asm_output_nstring (dirs[i].path,
12773 dirs[i].length
12774 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12775 "Directory Entry: %#x", i + idx_offset);
12777 else
12779 output_line_string (str_form, comp_dir, "Directory Entry", 0);
12780 for (i = 1 - idx_offset; i < ndirs; i++)
12782 const char *str
12783 = ggc_alloc_string (dirs[i].path,
12784 dirs[i].length
12785 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR);
12786 output_line_string (str_form, str, "Directory Entry",
12787 (unsigned) i + idx_offset);
12791 else
12793 for (i = 1 - idx_offset; i < ndirs; i++)
12794 dw2_asm_output_nstring (dirs[i].path,
12795 dirs[i].length
12796 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12797 "Directory Entry: %#x", i + idx_offset);
12799 dw2_asm_output_data (1, 0, "End directory table");
12802 /* We have to emit them in the order of emitted_number since that's
12803 used in the debug info generation. To do this efficiently we
12804 generate a back-mapping of the indices first. */
12805 backmap = XALLOCAVEC (int, numfiles);
12806 for (i = 0; i < numfiles; i++)
12807 backmap[files[i].file_idx->emitted_number - 1] = i;
12809 if (dwarf_version >= 5)
12811 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
12812 if (filename0 == NULL)
12813 filename0 = "";
12814 /* DW_LNCT_directory_index can use DW_FORM_udata, DW_FORM_data1 and
12815 DW_FORM_data2. Choose one based on the number of directories
12816 and how much space would they occupy in each encoding.
12817 If we have at most 256 directories, all indexes fit into
12818 a single byte, so DW_FORM_data1 is most compact (if there
12819 are at most 128 directories, DW_FORM_udata would be as
12820 compact as that, but not shorter and slower to decode). */
12821 if (ndirs + idx_offset <= 256)
12822 idx_form = DW_FORM_data1;
12823 /* If there are more than 65536 directories, we have to use
12824 DW_FORM_udata, DW_FORM_data2 can't refer to them.
12825 Otherwise, compute what space would occupy if all the indexes
12826 used DW_FORM_udata - sum - and compare that to how large would
12827 be DW_FORM_data2 encoding, and pick the more efficient one. */
12828 else if (ndirs + idx_offset <= 65536)
12830 unsigned HOST_WIDE_INT sum = 1;
12831 for (i = 0; i < numfiles; i++)
12833 int file_idx = backmap[i];
12834 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12835 sum += size_of_uleb128 (dir_idx);
12837 if (sum >= HOST_WIDE_INT_UC (2) * (numfiles + 1))
12838 idx_form = DW_FORM_data2;
12840 #ifdef VMS_DEBUGGING_INFO
12841 dw2_asm_output_data (1, 4, "File name entry format count");
12842 #else
12843 dw2_asm_output_data (1, 2, "File name entry format count");
12844 #endif
12845 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12846 dw2_asm_output_data_uleb128 (str_form, "%s",
12847 get_DW_FORM_name (str_form));
12848 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
12849 "DW_LNCT_directory_index");
12850 dw2_asm_output_data_uleb128 (idx_form, "%s",
12851 get_DW_FORM_name (idx_form));
12852 #ifdef VMS_DEBUGGING_INFO
12853 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
12854 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12855 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
12856 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12857 #endif
12858 dw2_asm_output_data_uleb128 (numfiles + 1, "File names count");
12860 output_line_string (str_form, filename0, "File Entry", 0);
12862 /* Include directory index. */
12863 if (idx_form != DW_FORM_udata)
12864 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12865 0, NULL);
12866 else
12867 dw2_asm_output_data_uleb128 (0, NULL);
12869 #ifdef VMS_DEBUGGING_INFO
12870 dw2_asm_output_data_uleb128 (0, NULL);
12871 dw2_asm_output_data_uleb128 (0, NULL);
12872 #endif
12875 /* Now write all the file names. */
12876 for (i = 0; i < numfiles; i++)
12878 int file_idx = backmap[i];
12879 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12881 #ifdef VMS_DEBUGGING_INFO
12882 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
12884 /* Setting these fields can lead to debugger miscomparisons,
12885 but VMS Debug requires them to be set correctly. */
12887 int ver;
12888 long long cdt;
12889 long siz;
12890 int maxfilelen = (strlen (files[file_idx].path)
12891 + dirs[dir_idx].length
12892 + MAX_VMS_VERSION_LEN + 1);
12893 char *filebuf = XALLOCAVEC (char, maxfilelen);
12895 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
12896 snprintf (filebuf, maxfilelen, "%s;%d",
12897 files[file_idx].path + dirs[dir_idx].length, ver);
12899 output_line_string (str_form, filebuf, "File Entry", (unsigned) i + 1);
12901 /* Include directory index. */
12902 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
12903 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12904 dir_idx + idx_offset, NULL);
12905 else
12906 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12908 /* Modification time. */
12909 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
12910 &cdt, 0, 0, 0) == 0)
12911 ? cdt : 0, NULL);
12913 /* File length in bytes. */
12914 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
12915 0, &siz, 0, 0) == 0)
12916 ? siz : 0, NULL);
12917 #else
12918 output_line_string (str_form,
12919 files[file_idx].path + dirs[dir_idx].length,
12920 "File Entry", (unsigned) i + 1);
12922 /* Include directory index. */
12923 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
12924 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12925 dir_idx + idx_offset, NULL);
12926 else
12927 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12929 if (dwarf_version >= 5)
12930 continue;
12932 /* Modification time. */
12933 dw2_asm_output_data_uleb128 (0, NULL);
12935 /* File length in bytes. */
12936 dw2_asm_output_data_uleb128 (0, NULL);
12937 #endif /* VMS_DEBUGGING_INFO */
12940 if (dwarf_version < 5)
12941 dw2_asm_output_data (1, 0, "End file name table");
12945 /* Output one line number table into the .debug_line section. */
12947 static void
12948 output_one_line_info_table (dw_line_info_table *table)
12950 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12951 unsigned int current_line = 1;
12952 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
12953 dw_line_info_entry *ent, *prev_addr = NULL;
12954 size_t i;
12955 unsigned int view;
12957 view = 0;
12959 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
12961 switch (ent->opcode)
12963 case LI_set_address:
12964 /* ??? Unfortunately, we have little choice here currently, and
12965 must always use the most general form. GCC does not know the
12966 address delta itself, so we can't use DW_LNS_advance_pc. Many
12967 ports do have length attributes which will give an upper bound
12968 on the address range. We could perhaps use length attributes
12969 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
12970 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12972 view = 0;
12974 /* This can handle any delta. This takes
12975 4+DWARF2_ADDR_SIZE bytes. */
12976 dw2_asm_output_data (1, 0, "set address %s%s", line_label,
12977 debug_variable_location_views
12978 ? ", reset view to 0" : "");
12979 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12980 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12981 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12983 prev_addr = ent;
12984 break;
12986 case LI_adv_address:
12988 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12989 char prev_label[MAX_ARTIFICIAL_LABEL_BYTES];
12990 ASM_GENERATE_INTERNAL_LABEL (prev_label, LINE_CODE_LABEL, prev_addr->val);
12992 view++;
12994 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc, "fixed advance PC, increment view to %i", view);
12995 dw2_asm_output_delta (2, line_label, prev_label,
12996 "from %s to %s", prev_label, line_label);
12998 prev_addr = ent;
12999 break;
13002 case LI_set_line:
13003 if (ent->val == current_line)
13005 /* We still need to start a new row, so output a copy insn. */
13006 dw2_asm_output_data (1, DW_LNS_copy,
13007 "copy line %u", current_line);
13009 else
13011 int line_offset = ent->val - current_line;
13012 int line_delta = line_offset - DWARF_LINE_BASE;
13014 current_line = ent->val;
13015 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
13017 /* This can handle deltas from -10 to 234, using the current
13018 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
13019 This takes 1 byte. */
13020 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
13021 "line %u", current_line);
13023 else
13025 /* This can handle any delta. This takes at least 4 bytes,
13026 depending on the value being encoded. */
13027 dw2_asm_output_data (1, DW_LNS_advance_line,
13028 "advance to line %u", current_line);
13029 dw2_asm_output_data_sleb128 (line_offset, NULL);
13030 dw2_asm_output_data (1, DW_LNS_copy, NULL);
13033 break;
13035 case LI_set_file:
13036 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
13037 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
13038 break;
13040 case LI_set_column:
13041 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
13042 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
13043 break;
13045 case LI_negate_stmt:
13046 current_is_stmt = !current_is_stmt;
13047 dw2_asm_output_data (1, DW_LNS_negate_stmt,
13048 "is_stmt %d", current_is_stmt);
13049 break;
13051 case LI_set_prologue_end:
13052 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
13053 "set prologue end");
13054 break;
13056 case LI_set_epilogue_begin:
13057 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
13058 "set epilogue begin");
13059 break;
13061 case LI_set_discriminator:
13062 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
13063 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
13064 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
13065 dw2_asm_output_data_uleb128 (ent->val, NULL);
13066 break;
13070 /* Emit debug info for the address of the end of the table. */
13071 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
13072 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
13073 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
13074 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
13076 dw2_asm_output_data (1, 0, "end sequence");
13077 dw2_asm_output_data_uleb128 (1, NULL);
13078 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
13081 static unsigned int output_line_info_generation;
13083 /* Output the source line number correspondence information. This
13084 information goes into the .debug_line section. */
13086 static void
13087 output_line_info (bool prologue_only)
13089 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
13090 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
13091 bool saw_one = false;
13092 int opc;
13094 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL,
13095 output_line_info_generation);
13096 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL,
13097 output_line_info_generation);
13098 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL,
13099 output_line_info_generation);
13100 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL,
13101 output_line_info_generation++);
13103 if (!XCOFF_DEBUGGING_INFO)
13105 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
13106 dw2_asm_output_data (4, 0xffffffff,
13107 "Initial length escape value indicating 64-bit DWARF extension");
13108 dw2_asm_output_delta (dwarf_offset_size, l2, l1,
13109 "Length of Source Line Info");
13112 ASM_OUTPUT_LABEL (asm_out_file, l1);
13114 output_dwarf_version ();
13115 if (dwarf_version >= 5)
13117 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
13118 dw2_asm_output_data (1, 0, "Segment Size");
13120 dw2_asm_output_delta (dwarf_offset_size, p2, p1, "Prolog Length");
13121 ASM_OUTPUT_LABEL (asm_out_file, p1);
13123 /* Define the architecture-dependent minimum instruction length (in bytes).
13124 In this implementation of DWARF, this field is used for information
13125 purposes only. Since GCC generates assembly language, we have no
13126 a priori knowledge of how many instruction bytes are generated for each
13127 source line, and therefore can use only the DW_LNE_set_address and
13128 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
13129 this as '1', which is "correct enough" for all architectures,
13130 and don't let the target override. */
13131 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
13133 if (dwarf_version >= 4)
13134 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
13135 "Maximum Operations Per Instruction");
13136 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
13137 "Default is_stmt_start flag");
13138 dw2_asm_output_data (1, DWARF_LINE_BASE,
13139 "Line Base Value (Special Opcodes)");
13140 dw2_asm_output_data (1, DWARF_LINE_RANGE,
13141 "Line Range Value (Special Opcodes)");
13142 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
13143 "Special Opcode Base");
13145 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
13147 int n_op_args;
13148 switch (opc)
13150 case DW_LNS_advance_pc:
13151 case DW_LNS_advance_line:
13152 case DW_LNS_set_file:
13153 case DW_LNS_set_column:
13154 case DW_LNS_fixed_advance_pc:
13155 case DW_LNS_set_isa:
13156 n_op_args = 1;
13157 break;
13158 default:
13159 n_op_args = 0;
13160 break;
13163 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
13164 opc, n_op_args);
13167 /* Write out the information about the files we use. */
13168 output_file_names ();
13169 ASM_OUTPUT_LABEL (asm_out_file, p2);
13170 if (prologue_only)
13172 /* Output the marker for the end of the line number info. */
13173 ASM_OUTPUT_LABEL (asm_out_file, l2);
13174 return;
13177 if (separate_line_info)
13179 dw_line_info_table *table;
13180 size_t i;
13182 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
13183 if (table->in_use)
13185 output_one_line_info_table (table);
13186 saw_one = true;
13189 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
13191 output_one_line_info_table (cold_text_section_line_info);
13192 saw_one = true;
13195 /* ??? Some Darwin linkers crash on a .debug_line section with no
13196 sequences. Further, merely a DW_LNE_end_sequence entry is not
13197 sufficient -- the address column must also be initialized.
13198 Make sure to output at least one set_address/end_sequence pair,
13199 choosing .text since that section is always present. */
13200 if (text_section_line_info->in_use || !saw_one)
13201 output_one_line_info_table (text_section_line_info);
13203 /* Output the marker for the end of the line number info. */
13204 ASM_OUTPUT_LABEL (asm_out_file, l2);
13207 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
13209 static inline bool
13210 need_endianity_attribute_p (bool reverse)
13212 return reverse && (dwarf_version >= 3 || !dwarf_strict);
13215 /* Given a pointer to a tree node for some base type, return a pointer to
13216 a DIE that describes the given type. REVERSE is true if the type is
13217 to be interpreted in the reverse storage order wrt the target order.
13219 This routine must only be called for GCC type nodes that correspond to
13220 Dwarf base (fundamental) types. */
13222 dw_die_ref
13223 base_type_die (tree type, bool reverse)
13225 dw_die_ref base_type_result;
13226 enum dwarf_type encoding;
13227 bool fpt_used = false;
13228 struct fixed_point_type_info fpt_info;
13229 tree type_bias = NULL_TREE;
13231 /* If this is a subtype that should not be emitted as a subrange type,
13232 use the base type. See subrange_type_for_debug_p. */
13233 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
13234 type = TREE_TYPE (type);
13236 switch (TREE_CODE (type))
13238 case INTEGER_TYPE:
13239 if ((dwarf_version >= 4 || !dwarf_strict)
13240 && TYPE_NAME (type)
13241 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
13242 && DECL_IS_UNDECLARED_BUILTIN (TYPE_NAME (type))
13243 && DECL_NAME (TYPE_NAME (type)))
13245 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
13246 if (strcmp (name, "char16_t") == 0
13247 || strcmp (name, "char8_t") == 0
13248 || strcmp (name, "char32_t") == 0)
13250 encoding = DW_ATE_UTF;
13251 break;
13254 if ((dwarf_version >= 3 || !dwarf_strict)
13255 && lang_hooks.types.get_fixed_point_type_info)
13257 memset (&fpt_info, 0, sizeof (fpt_info));
13258 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
13260 fpt_used = true;
13261 encoding = ((TYPE_UNSIGNED (type))
13262 ? DW_ATE_unsigned_fixed
13263 : DW_ATE_signed_fixed);
13264 break;
13267 if (TYPE_STRING_FLAG (type))
13269 if ((dwarf_version >= 4 || !dwarf_strict)
13270 && is_rust ()
13271 && int_size_in_bytes (type) == 4)
13272 encoding = DW_ATE_UTF;
13273 else if (TYPE_UNSIGNED (type))
13274 encoding = DW_ATE_unsigned_char;
13275 else
13276 encoding = DW_ATE_signed_char;
13278 else if (TYPE_UNSIGNED (type))
13279 encoding = DW_ATE_unsigned;
13280 else
13281 encoding = DW_ATE_signed;
13283 if (!dwarf_strict
13284 && lang_hooks.types.get_type_bias)
13285 type_bias = lang_hooks.types.get_type_bias (type);
13286 break;
13288 case REAL_TYPE:
13289 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
13291 if (dwarf_version >= 3 || !dwarf_strict)
13292 encoding = DW_ATE_decimal_float;
13293 else
13294 encoding = DW_ATE_lo_user;
13296 else
13297 encoding = DW_ATE_float;
13298 break;
13300 case FIXED_POINT_TYPE:
13301 if (!(dwarf_version >= 3 || !dwarf_strict))
13302 encoding = DW_ATE_lo_user;
13303 else if (TYPE_UNSIGNED (type))
13304 encoding = DW_ATE_unsigned_fixed;
13305 else
13306 encoding = DW_ATE_signed_fixed;
13307 break;
13309 /* Dwarf2 doesn't know anything about complex ints, so use
13310 a user defined type for it. */
13311 case COMPLEX_TYPE:
13312 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (type)))
13313 encoding = DW_ATE_complex_float;
13314 else
13315 encoding = DW_ATE_lo_user;
13316 break;
13318 case BOOLEAN_TYPE:
13319 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
13320 encoding = DW_ATE_boolean;
13321 break;
13323 case BITINT_TYPE:
13324 /* C23 _BitInt(N). */
13325 if (TYPE_UNSIGNED (type))
13326 encoding = DW_ATE_unsigned;
13327 else
13328 encoding = DW_ATE_signed;
13329 break;
13331 default:
13332 /* No other TREE_CODEs are Dwarf fundamental types. */
13333 gcc_unreachable ();
13336 base_type_result = new_die_raw (DW_TAG_base_type);
13338 add_AT_unsigned (base_type_result, DW_AT_byte_size,
13339 int_size_in_bytes (type));
13340 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
13341 if (TREE_CODE (type) == BITINT_TYPE)
13342 add_AT_unsigned (base_type_result, DW_AT_bit_size, TYPE_PRECISION (type));
13344 if (need_endianity_attribute_p (reverse))
13345 add_AT_unsigned (base_type_result, DW_AT_endianity,
13346 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
13348 add_alignment_attribute (base_type_result, type);
13350 if (fpt_used)
13352 switch (fpt_info.scale_factor_kind)
13354 case fixed_point_scale_factor_binary:
13355 add_AT_int (base_type_result, DW_AT_binary_scale,
13356 fpt_info.scale_factor.binary);
13357 break;
13359 case fixed_point_scale_factor_decimal:
13360 add_AT_int (base_type_result, DW_AT_decimal_scale,
13361 fpt_info.scale_factor.decimal);
13362 break;
13364 case fixed_point_scale_factor_arbitrary:
13365 /* Arbitrary scale factors cannot be described in standard DWARF. */
13366 if (!dwarf_strict)
13368 /* Describe the scale factor as a rational constant. */
13369 const dw_die_ref scale_factor
13370 = new_die (DW_TAG_constant, comp_unit_die (), type);
13372 add_scalar_info (scale_factor, DW_AT_GNU_numerator,
13373 fpt_info.scale_factor.arbitrary.numerator,
13374 dw_scalar_form_constant, NULL);
13375 add_scalar_info (scale_factor, DW_AT_GNU_denominator,
13376 fpt_info.scale_factor.arbitrary.denominator,
13377 dw_scalar_form_constant, NULL);
13379 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
13381 break;
13383 default:
13384 gcc_unreachable ();
13388 if (type_bias)
13389 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
13390 dw_scalar_form_constant
13391 | dw_scalar_form_exprloc
13392 | dw_scalar_form_reference,
13393 NULL);
13395 return base_type_result;
13398 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
13399 named 'auto' in its type: return true for it, false otherwise. */
13401 static inline bool
13402 is_cxx_auto (tree type)
13404 if (is_cxx ())
13406 tree name = TYPE_IDENTIFIER (type);
13407 if (name == get_identifier ("auto")
13408 || name == get_identifier ("decltype(auto)"))
13409 return true;
13411 return false;
13414 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if the
13415 given input type is a Dwarf "fundamental" type. Otherwise return null. */
13417 static inline bool
13418 is_base_type (tree type)
13420 switch (TREE_CODE (type))
13422 case INTEGER_TYPE:
13423 case REAL_TYPE:
13424 case FIXED_POINT_TYPE:
13425 case COMPLEX_TYPE:
13426 case BOOLEAN_TYPE:
13427 case BITINT_TYPE:
13428 return true;
13430 case VOID_TYPE:
13431 case OPAQUE_TYPE:
13432 case ARRAY_TYPE:
13433 case RECORD_TYPE:
13434 case UNION_TYPE:
13435 case QUAL_UNION_TYPE:
13436 case ENUMERAL_TYPE:
13437 case FUNCTION_TYPE:
13438 case METHOD_TYPE:
13439 case POINTER_TYPE:
13440 case REFERENCE_TYPE:
13441 case NULLPTR_TYPE:
13442 case OFFSET_TYPE:
13443 case LANG_TYPE:
13444 case VECTOR_TYPE:
13445 return false;
13447 default:
13448 if (is_cxx_auto (type))
13449 return false;
13450 gcc_unreachable ();
13454 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
13455 node, return the size in bits for the type if it is a constant, or else
13456 return the alignment for the type if the type's size is not constant, or
13457 else return BITS_PER_WORD if the type actually turns out to be an
13458 ERROR_MARK node. */
13460 static inline unsigned HOST_WIDE_INT
13461 simple_type_size_in_bits (const_tree type)
13463 if (TREE_CODE (type) == ERROR_MARK)
13464 return BITS_PER_WORD;
13465 else if (TYPE_SIZE (type) == NULL_TREE)
13466 return 0;
13467 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
13468 return tree_to_uhwi (TYPE_SIZE (type));
13469 else
13470 return TYPE_ALIGN (type);
13473 /* Similarly, but return an offset_int instead of UHWI. */
13475 static inline offset_int
13476 offset_int_type_size_in_bits (const_tree type)
13478 if (TREE_CODE (type) == ERROR_MARK)
13479 return BITS_PER_WORD;
13480 else if (TYPE_SIZE (type) == NULL_TREE)
13481 return 0;
13482 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
13483 return wi::to_offset (TYPE_SIZE (type));
13484 else
13485 return TYPE_ALIGN (type);
13488 /* Given a pointer to a tree node for a subrange type, return a pointer
13489 to a DIE that describes the given type. */
13491 static dw_die_ref
13492 subrange_type_die (tree type, tree low, tree high, tree bias,
13493 dw_die_ref context_die)
13495 dw_die_ref subrange_die;
13496 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
13498 if (context_die == NULL)
13499 context_die = comp_unit_die ();
13501 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
13503 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
13505 /* The size of the subrange type and its base type do not match,
13506 so we need to generate a size attribute for the subrange type. */
13507 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
13510 add_alignment_attribute (subrange_die, type);
13512 if (low)
13513 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
13514 if (high)
13515 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
13516 if (bias && !dwarf_strict)
13517 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
13518 dw_scalar_form_constant
13519 | dw_scalar_form_exprloc
13520 | dw_scalar_form_reference,
13521 NULL);
13523 return subrange_die;
13526 /* Returns the (const and/or volatile) cv_qualifiers associated with
13527 the decl node. This will normally be augmented with the
13528 cv_qualifiers of the underlying type in add_type_attribute. */
13530 static int
13531 decl_quals (const_tree decl)
13533 return ((TREE_READONLY (decl)
13534 /* The C++ front-end correctly marks reference-typed
13535 variables as readonly, but from a language (and debug
13536 info) standpoint they are not const-qualified. */
13537 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
13538 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
13539 | (TREE_THIS_VOLATILE (decl)
13540 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
13543 /* Determine the TYPE whose qualifiers match the largest strict subset
13544 of the given TYPE_QUALS, and return its qualifiers. Ignore all
13545 qualifiers outside QUAL_MASK. */
13547 static int
13548 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
13550 tree t;
13551 int best_rank = 0, best_qual = 0, max_rank;
13553 type_quals &= qual_mask;
13554 max_rank = popcount_hwi (type_quals) - 1;
13556 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
13557 t = TYPE_NEXT_VARIANT (t))
13559 int q = TYPE_QUALS (t) & qual_mask;
13561 if ((q & type_quals) == q && q != type_quals
13562 && check_base_type (t, type))
13564 int rank = popcount_hwi (q);
13566 if (rank > best_rank)
13568 best_rank = rank;
13569 best_qual = q;
13574 return best_qual;
13577 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
13578 static const dwarf_qual_info_t dwarf_qual_info[] =
13580 { TYPE_QUAL_CONST, DW_TAG_const_type },
13581 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
13582 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
13583 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
13585 static const unsigned int dwarf_qual_info_size = ARRAY_SIZE (dwarf_qual_info);
13587 /* If DIE is a qualified DIE of some base DIE with the same parent,
13588 return the base DIE, otherwise return NULL. Set MASK to the
13589 qualifiers added compared to the returned DIE. */
13591 static dw_die_ref
13592 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
13594 unsigned int i;
13595 for (i = 0; i < dwarf_qual_info_size; i++)
13596 if (die->die_tag == dwarf_qual_info[i].t)
13597 break;
13598 if (i == dwarf_qual_info_size)
13599 return NULL;
13600 if (vec_safe_length (die->die_attr) != 1)
13601 return NULL;
13602 dw_die_ref type = get_AT_ref (die, DW_AT_type);
13603 if (type == NULL || type->die_parent != die->die_parent)
13604 return NULL;
13605 *mask |= dwarf_qual_info[i].q;
13606 if (depth)
13608 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
13609 if (ret)
13610 return ret;
13612 return type;
13615 /* If TYPE is long double or complex long double that
13616 should be emitted as artificial typedef to _Float128 or
13617 complex _Float128, return the type it should be emitted as.
13618 This is done in case the target already supports 16-byte
13619 composite floating point type (ibm_extended_format). */
13621 static tree
13622 long_double_as_float128 (tree type)
13624 if (type != long_double_type_node
13625 && type != complex_long_double_type_node)
13626 return NULL_TREE;
13628 machine_mode mode, fmode;
13629 if (TREE_CODE (type) == COMPLEX_TYPE)
13630 mode = TYPE_MODE (TREE_TYPE (type));
13631 else
13632 mode = TYPE_MODE (type);
13633 if (known_eq (GET_MODE_SIZE (mode), 16) && !MODE_COMPOSITE_P (mode))
13634 FOR_EACH_MODE_IN_CLASS (fmode, MODE_FLOAT)
13635 if (known_eq (GET_MODE_SIZE (fmode), 16)
13636 && MODE_COMPOSITE_P (fmode))
13638 if (type == long_double_type_node)
13640 if (float128_type_node
13641 && (TYPE_MODE (float128_type_node)
13642 == TYPE_MODE (type)))
13643 return float128_type_node;
13644 return NULL_TREE;
13646 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
13647 if (COMPLEX_FLOATN_NX_TYPE_NODE (i) != NULL_TREE
13648 && (TYPE_MODE (COMPLEX_FLOATN_NX_TYPE_NODE (i))
13649 == TYPE_MODE (type)))
13650 return COMPLEX_FLOATN_NX_TYPE_NODE (i);
13653 return NULL_TREE;
13656 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
13657 entry that chains the modifiers specified by CV_QUALS in front of the
13658 given type. REVERSE is true if the type is to be interpreted in the
13659 reverse storage order wrt the target order. */
13661 static dw_die_ref
13662 modified_type_die (tree type, int cv_quals, bool reverse,
13663 dw_die_ref context_die)
13665 enum tree_code code = TREE_CODE (type);
13666 dw_die_ref mod_type_die;
13667 dw_die_ref sub_die = NULL;
13668 tree item_type = NULL;
13669 tree qualified_type;
13670 tree name, low, high;
13671 dw_die_ref mod_scope;
13672 struct array_descr_info info;
13673 /* Only these cv-qualifiers are currently handled. */
13674 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
13675 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC |
13676 ENCODE_QUAL_ADDR_SPACE(~0U));
13677 /* DW_AT_endianity is specified only for base types in the standard. */
13678 const bool reverse_type
13679 = need_endianity_attribute_p (reverse)
13680 && (is_base_type (type)
13681 || (TREE_CODE (type) == ENUMERAL_TYPE && !dwarf_strict));
13683 if (code == ERROR_MARK)
13684 return NULL;
13686 if (lang_hooks.types.get_debug_type)
13688 tree debug_type = lang_hooks.types.get_debug_type (type);
13690 if (debug_type != NULL_TREE && debug_type != type)
13691 return modified_type_die (debug_type, cv_quals, reverse, context_die);
13694 cv_quals &= cv_qual_mask;
13696 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
13697 tag modifier (and not an attribute) old consumers won't be able
13698 to handle it. */
13699 if (dwarf_version < 3)
13700 cv_quals &= ~TYPE_QUAL_RESTRICT;
13702 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
13703 if (dwarf_version < 5)
13704 cv_quals &= ~TYPE_QUAL_ATOMIC;
13706 /* See if we already have the appropriately qualified variant of
13707 this type. */
13708 qualified_type = get_qualified_type (type, cv_quals);
13710 if (qualified_type == sizetype)
13712 /* Try not to expose the internal sizetype type's name. */
13713 if (TYPE_NAME (qualified_type)
13714 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
13716 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
13718 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
13719 && (TYPE_PRECISION (t)
13720 == TYPE_PRECISION (qualified_type))
13721 && (TYPE_UNSIGNED (t)
13722 == TYPE_UNSIGNED (qualified_type)));
13723 qualified_type = t;
13725 else if (qualified_type == sizetype
13726 && TREE_CODE (sizetype) == TREE_CODE (size_type_node)
13727 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (size_type_node)
13728 && TYPE_UNSIGNED (sizetype) == TYPE_UNSIGNED (size_type_node))
13729 qualified_type = size_type_node;
13730 if (type == sizetype)
13731 type = qualified_type;
13734 /* If we do, then we can just use its DIE, if it exists. */
13735 if (qualified_type)
13737 mod_type_die = lookup_type_die (qualified_type);
13739 /* DW_AT_endianity doesn't come from a qualifier on the type, so it is
13740 dealt with specially: the DIE with the attribute, if it exists, is
13741 placed immediately after the regular DIE for the same type. */
13742 if (mod_type_die
13743 && (!reverse_type
13744 || ((mod_type_die = mod_type_die->die_sib) != NULL
13745 && get_AT_unsigned (mod_type_die, DW_AT_endianity))))
13746 return mod_type_die;
13749 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
13751 /* Handle C typedef types. */
13752 if (name
13753 && TREE_CODE (name) == TYPE_DECL
13754 && DECL_ORIGINAL_TYPE (name)
13755 && !DECL_ARTIFICIAL (name))
13757 tree dtype = TREE_TYPE (name);
13759 /* Skip the typedef for base types with DW_AT_endianity, no big deal. */
13760 if (qualified_type == dtype && !reverse_type)
13762 tree origin = decl_ultimate_origin (name);
13764 /* Typedef variants that have an abstract origin don't get their own
13765 type DIE (see gen_typedef_die), so fall back on the ultimate
13766 abstract origin instead. */
13767 if (origin != NULL && origin != name)
13768 return modified_type_die (TREE_TYPE (origin), cv_quals, reverse,
13769 context_die);
13771 /* For a named type, use the typedef. */
13772 gen_type_die (qualified_type, context_die);
13773 return lookup_type_die (qualified_type);
13775 else
13777 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
13778 dquals &= cv_qual_mask;
13779 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
13780 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
13781 /* cv-unqualified version of named type. Just use
13782 the unnamed type to which it refers. */
13783 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
13784 reverse, context_die);
13785 /* Else cv-qualified version of named type; fall through. */
13789 mod_scope = scope_die_for (type, context_die);
13791 if (cv_quals)
13793 int sub_quals = 0, first_quals = 0;
13794 unsigned i;
13795 dw_die_ref first = NULL, last = NULL;
13797 /* Determine a lesser qualified type that most closely matches
13798 this one. Then generate DW_TAG_* entries for the remaining
13799 qualifiers. */
13800 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
13801 cv_qual_mask);
13802 if (sub_quals && use_debug_types)
13804 bool needed = false;
13805 /* If emitting type units, make sure the order of qualifiers
13806 is canonical. Thus, start from unqualified type if
13807 an earlier qualifier is missing in sub_quals, but some later
13808 one is present there. */
13809 for (i = 0; i < dwarf_qual_info_size; i++)
13810 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
13811 needed = true;
13812 else if (needed && (dwarf_qual_info[i].q & cv_quals))
13814 sub_quals = 0;
13815 break;
13818 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
13819 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
13821 /* As not all intermediate qualified DIEs have corresponding
13822 tree types, ensure that qualified DIEs in the same scope
13823 as their DW_AT_type are emitted after their DW_AT_type,
13824 only with other qualified DIEs for the same type possibly
13825 in between them. Determine the range of such qualified
13826 DIEs now (first being the base type, last being corresponding
13827 last qualified DIE for it). */
13828 unsigned int count = 0;
13829 first = qualified_die_p (mod_type_die, &first_quals,
13830 dwarf_qual_info_size);
13831 if (first == NULL)
13832 first = mod_type_die;
13833 gcc_assert ((first_quals & ~sub_quals) == 0);
13834 for (count = 0, last = first;
13835 count < (1U << dwarf_qual_info_size);
13836 count++, last = last->die_sib)
13838 int quals = 0;
13839 if (last == mod_scope->die_child)
13840 break;
13841 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
13842 != first)
13843 break;
13847 for (i = 0; i < dwarf_qual_info_size; i++)
13848 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
13850 dw_die_ref d;
13851 if (first && first != last)
13853 for (d = first->die_sib; ; d = d->die_sib)
13855 int quals = 0;
13856 qualified_die_p (d, &quals, dwarf_qual_info_size);
13857 if (quals == (first_quals | dwarf_qual_info[i].q))
13858 break;
13859 if (d == last)
13861 d = NULL;
13862 break;
13865 if (d)
13867 mod_type_die = d;
13868 continue;
13871 if (first)
13873 d = new_die_raw (dwarf_qual_info[i].t);
13874 add_child_die_after (mod_scope, d, last);
13875 last = d;
13877 else
13878 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
13879 if (mod_type_die)
13880 add_AT_die_ref (d, DW_AT_type, mod_type_die);
13881 mod_type_die = d;
13882 first_quals |= dwarf_qual_info[i].q;
13885 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
13887 dwarf_tag tag = DW_TAG_pointer_type;
13888 if (code == REFERENCE_TYPE)
13890 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
13891 tag = DW_TAG_rvalue_reference_type;
13892 else
13893 tag = DW_TAG_reference_type;
13895 mod_type_die = new_die (tag, mod_scope, type);
13897 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
13898 simple_type_size_in_bits (type) / BITS_PER_UNIT);
13899 add_alignment_attribute (mod_type_die, type);
13900 item_type = TREE_TYPE (type);
13902 addr_space_t as = TYPE_ADDR_SPACE (item_type);
13903 if (!ADDR_SPACE_GENERIC_P (as))
13905 int action = targetm.addr_space.debug (as);
13906 if (action >= 0)
13908 /* Positive values indicate an address_class. */
13909 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
13911 else
13913 /* Negative values indicate an (inverted) segment base reg. */
13914 dw_loc_descr_ref d
13915 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
13916 add_AT_loc (mod_type_die, DW_AT_segment, d);
13920 else if (code == ARRAY_TYPE
13921 || (lang_hooks.types.get_array_descr_info
13922 && lang_hooks.types.get_array_descr_info (type, &info)))
13924 gen_type_die (type, context_die);
13925 return lookup_type_die (type);
13927 else if (code == INTEGER_TYPE
13928 && TREE_TYPE (type) != NULL_TREE
13929 && subrange_type_for_debug_p (type, &low, &high))
13931 tree bias = NULL_TREE;
13932 if (lang_hooks.types.get_type_bias)
13933 bias = lang_hooks.types.get_type_bias (type);
13934 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
13935 item_type = TREE_TYPE (type);
13937 else if (is_base_type (type))
13939 /* If a target supports long double as different floating point
13940 modes with the same 16-byte size, use normal DW_TAG_base_type
13941 only for the composite (ibm_extended_real_format) type and
13942 for the other for the time being emit instead a "_Float128"
13943 or "complex _Float128" DW_TAG_base_type and a "long double"
13944 or "complex long double" typedef to it. */
13945 if (tree other_type = long_double_as_float128 (type))
13947 dw_die_ref other_die;
13948 if (TYPE_NAME (other_type))
13949 other_die
13950 = modified_type_die (other_type, TYPE_UNQUALIFIED, reverse,
13951 context_die);
13952 else
13954 other_die = base_type_die (type, reverse);
13955 add_child_die (comp_unit_die (), other_die);
13956 add_name_attribute (other_die,
13957 TREE_CODE (type) == COMPLEX_TYPE
13958 ? "complex _Float128" : "_Float128");
13960 mod_type_die = new_die_raw (DW_TAG_typedef);
13961 add_AT_die_ref (mod_type_die, DW_AT_type, other_die);
13963 else
13964 mod_type_die = base_type_die (type, reverse);
13966 /* The DIE with DW_AT_endianity is placed right after the naked DIE. */
13967 if (reverse_type)
13969 dw_die_ref after_die
13970 = modified_type_die (type, cv_quals, false, context_die);
13971 add_child_die_after (comp_unit_die (), mod_type_die, after_die);
13973 else
13974 add_child_die (comp_unit_die (), mod_type_die);
13976 add_pubtype (type, mod_type_die);
13978 else
13980 /* The DIE with DW_AT_endianity is placed right after the naked DIE. */
13981 if (reverse_type)
13983 dw_die_ref after_die
13984 = modified_type_die (type, cv_quals, false, context_die);
13985 gen_type_die (type, context_die, true);
13986 gcc_assert (after_die->die_sib
13987 && get_AT_unsigned (after_die->die_sib, DW_AT_endianity));
13988 return after_die->die_sib;
13991 gen_type_die (type, context_die);
13993 /* We have to get the type_main_variant here (and pass that to the
13994 `lookup_type_die' routine) because the ..._TYPE node we have
13995 might simply be a *copy* of some original type node (where the
13996 copy was created to help us keep track of typedef names) and
13997 that copy might have a different TYPE_UID from the original
13998 ..._TYPE node. */
13999 if (code == FUNCTION_TYPE || code == METHOD_TYPE)
14001 /* For function/method types, can't just use type_main_variant here,
14002 because that can have different ref-qualifiers for C++,
14003 but try to canonicalize. */
14004 tree main = TYPE_MAIN_VARIANT (type);
14005 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
14006 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
14007 && check_base_type (t, main)
14008 && check_lang_type (t, type))
14009 return lookup_type_die (t);
14010 return lookup_type_die (type);
14012 /* Vectors have the debugging information in the type,
14013 not the main variant. */
14014 else if (code == VECTOR_TYPE)
14015 return lookup_type_die (type);
14016 else
14017 return lookup_type_die (type_main_variant (type));
14020 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
14021 don't output a DW_TAG_typedef, since there isn't one in the
14022 user's program; just attach a DW_AT_name to the type.
14023 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
14024 if the base type already has the same name. */
14025 if (name
14026 && ((TREE_CODE (name) != TYPE_DECL
14027 && (qualified_type == TYPE_MAIN_VARIANT (type)
14028 || (cv_quals == TYPE_UNQUALIFIED)))
14029 || (TREE_CODE (name) == TYPE_DECL
14030 && TREE_TYPE (name) == qualified_type
14031 && DECL_NAME (name))))
14033 if (TREE_CODE (name) == TYPE_DECL)
14034 /* Could just call add_name_and_src_coords_attributes here,
14035 but since this is a builtin type it doesn't have any
14036 useful source coordinates anyway. */
14037 name = DECL_NAME (name);
14038 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
14040 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
14042 if (TREE_CODE (type) == BITINT_TYPE)
14044 char name_buf[sizeof ("unsigned _BitInt(2147483647)")];
14045 snprintf (name_buf, sizeof (name_buf),
14046 "%s_BitInt(%d)", TYPE_UNSIGNED (type) ? "unsigned " : "",
14047 TYPE_PRECISION (type));
14048 add_name_attribute (mod_type_die, name_buf);
14050 else
14052 /* This probably indicates a bug. */
14053 name = TYPE_IDENTIFIER (type);
14054 add_name_attribute (mod_type_die,
14055 name
14056 ? IDENTIFIER_POINTER (name) : "__unknown__");
14060 if (qualified_type && !reverse_type)
14061 equate_type_number_to_die (qualified_type, mod_type_die);
14063 if (item_type)
14064 /* We must do this after the equate_type_number_to_die call, in case
14065 this is a recursive type. This ensures that the modified_type_die
14066 recursion will terminate even if the type is recursive. Recursive
14067 types are possible in Ada. */
14068 sub_die = modified_type_die (item_type,
14069 TYPE_QUALS_NO_ADDR_SPACE (item_type),
14070 reverse,
14071 context_die);
14073 if (sub_die != NULL)
14074 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
14076 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
14077 if (TYPE_ARTIFICIAL (type))
14078 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
14080 return mod_type_die;
14083 /* Generate DIEs for the generic parameters of T.
14084 T must be either a generic type or a generic function.
14085 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
14087 static void
14088 gen_generic_params_dies (tree t)
14090 tree parms, args;
14091 int parms_num, i;
14092 dw_die_ref die = NULL;
14093 int non_default;
14095 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
14096 return;
14098 if (TYPE_P (t))
14099 die = lookup_type_die (t);
14100 else if (DECL_P (t))
14101 die = lookup_decl_die (t);
14103 gcc_assert (die);
14105 parms = lang_hooks.get_innermost_generic_parms (t);
14106 if (!parms)
14107 /* T has no generic parameter. It means T is neither a generic type
14108 or function. End of story. */
14109 return;
14111 parms_num = TREE_VEC_LENGTH (parms);
14112 args = lang_hooks.get_innermost_generic_args (t);
14113 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
14114 non_default = int_cst_value (TREE_CHAIN (args));
14115 else
14116 non_default = TREE_VEC_LENGTH (args);
14117 for (i = 0; i < parms_num; i++)
14119 tree parm, arg, arg_pack_elems;
14120 dw_die_ref parm_die;
14122 parm = TREE_VEC_ELT (parms, i);
14123 arg = TREE_VEC_ELT (args, i);
14124 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
14125 gcc_assert (parm && TREE_VALUE (parm) && arg);
14127 if (parm && TREE_VALUE (parm) && arg)
14129 /* If PARM represents a template parameter pack,
14130 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
14131 by DW_TAG_template_*_parameter DIEs for the argument
14132 pack elements of ARG. Note that ARG would then be
14133 an argument pack. */
14134 if (arg_pack_elems)
14135 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
14136 arg_pack_elems,
14137 die);
14138 else
14139 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
14140 true /* emit name */, die);
14141 if (i >= non_default)
14142 add_AT_flag (parm_die, DW_AT_default_value, 1);
14147 /* Create and return a DIE for PARM which should be
14148 the representation of a generic type parameter.
14149 For instance, in the C++ front end, PARM would be a template parameter.
14150 ARG is the argument to PARM.
14151 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
14152 name of the PARM.
14153 PARENT_DIE is the parent DIE which the new created DIE should be added to,
14154 as a child node. */
14156 static dw_die_ref
14157 generic_parameter_die (tree parm, tree arg,
14158 bool emit_name_p,
14159 dw_die_ref parent_die)
14161 dw_die_ref tmpl_die = NULL;
14162 const char *name = NULL;
14164 /* C++20 accepts class literals as template parameters, and var
14165 decls with initializers represent them. The VAR_DECLs would be
14166 rejected, but we can take the DECL_INITIAL constructor and
14167 attempt to expand it. */
14168 if (arg && VAR_P (arg))
14169 arg = DECL_INITIAL (arg);
14171 if (!parm || !DECL_NAME (parm) || !arg)
14172 return NULL;
14174 /* We support non-type generic parameters and arguments,
14175 type generic parameters and arguments, as well as
14176 generic generic parameters (a.k.a. template template parameters in C++)
14177 and arguments. */
14178 if (TREE_CODE (parm) == PARM_DECL)
14179 /* PARM is a nontype generic parameter */
14180 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
14181 else if (TREE_CODE (parm) == TYPE_DECL)
14182 /* PARM is a type generic parameter. */
14183 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
14184 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
14185 /* PARM is a generic generic parameter.
14186 Its DIE is a GNU extension. It shall have a
14187 DW_AT_name attribute to represent the name of the template template
14188 parameter, and a DW_AT_GNU_template_name attribute to represent the
14189 name of the template template argument. */
14190 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
14191 parent_die, parm);
14192 else
14193 gcc_unreachable ();
14195 if (tmpl_die)
14197 tree tmpl_type;
14199 /* If PARM is a generic parameter pack, it means we are
14200 emitting debug info for a template argument pack element.
14201 In other terms, ARG is a template argument pack element.
14202 In that case, we don't emit any DW_AT_name attribute for
14203 the die. */
14204 if (emit_name_p)
14206 name = IDENTIFIER_POINTER (DECL_NAME (parm));
14207 gcc_assert (name);
14208 add_AT_string (tmpl_die, DW_AT_name, name);
14211 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
14213 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
14214 TMPL_DIE should have a child DW_AT_type attribute that is set
14215 to the type of the argument to PARM, which is ARG.
14216 If PARM is a type generic parameter, TMPL_DIE should have a
14217 child DW_AT_type that is set to ARG. */
14218 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
14219 add_type_attribute (tmpl_die, tmpl_type,
14220 (TREE_THIS_VOLATILE (tmpl_type)
14221 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
14222 false, parent_die);
14224 else
14226 /* So TMPL_DIE is a DIE representing a
14227 a generic generic template parameter, a.k.a template template
14228 parameter in C++ and arg is a template. */
14230 /* The DW_AT_GNU_template_name attribute of the DIE must be set
14231 to the name of the argument. */
14232 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
14233 if (name)
14234 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
14237 if (TREE_CODE (parm) == PARM_DECL)
14238 /* So PARM is a non-type generic parameter.
14239 DWARF3 5.6.8 says we must set a DW_AT_const_value child
14240 attribute of TMPL_DIE which value represents the value
14241 of ARG.
14242 We must be careful here:
14243 The value of ARG might reference some function decls.
14244 We might currently be emitting debug info for a generic
14245 type and types are emitted before function decls, we don't
14246 know if the function decls referenced by ARG will actually be
14247 emitted after cgraph computations.
14248 So must defer the generation of the DW_AT_const_value to
14249 after cgraph is ready. */
14250 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
14253 return tmpl_die;
14256 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
14257 PARM_PACK must be a template parameter pack. The returned DIE
14258 will be child DIE of PARENT_DIE. */
14260 static dw_die_ref
14261 template_parameter_pack_die (tree parm_pack,
14262 tree parm_pack_args,
14263 dw_die_ref parent_die)
14265 dw_die_ref die;
14266 int j;
14268 gcc_assert (parent_die && parm_pack);
14270 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
14271 add_name_and_src_coords_attributes (die, parm_pack);
14272 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
14273 generic_parameter_die (parm_pack,
14274 TREE_VEC_ELT (parm_pack_args, j),
14275 false /* Don't emit DW_AT_name */,
14276 die);
14277 return die;
14280 /* Return the debugger register number described by a given RTL node. */
14282 static unsigned int
14283 debugger_reg_number (const_rtx rtl)
14285 unsigned regno = REGNO (rtl);
14287 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
14289 #ifdef LEAF_REG_REMAP
14290 if (crtl->uses_only_leaf_regs)
14292 int leaf_reg = LEAF_REG_REMAP (regno);
14293 if (leaf_reg != -1)
14294 regno = (unsigned) leaf_reg;
14296 #endif
14298 regno = DEBUGGER_REGNO (regno);
14299 gcc_assert (regno != INVALID_REGNUM);
14300 return regno;
14303 /* Optionally add a DW_OP_piece term to a location description expression.
14304 DW_OP_piece is only added if the location description expression already
14305 doesn't end with DW_OP_piece. */
14307 static void
14308 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
14310 dw_loc_descr_ref loc;
14312 if (*list_head != NULL)
14314 /* Find the end of the chain. */
14315 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
14318 if (loc->dw_loc_opc != DW_OP_piece)
14319 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
14323 /* Return a location descriptor that designates a machine register or
14324 zero if there is none. */
14326 static dw_loc_descr_ref
14327 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
14329 rtx regs;
14331 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
14332 return 0;
14334 /* We only use "frame base" when we're sure we're talking about the
14335 post-prologue local stack frame. We do this by *not* running
14336 register elimination until this point, and recognizing the special
14337 argument pointer and soft frame pointer rtx's.
14338 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
14339 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
14340 && (ira_use_lra_p
14341 ? lra_eliminate_regs (rtl, VOIDmode, NULL_RTX)
14342 : eliminate_regs (rtl, VOIDmode, NULL_RTX)) != rtl)
14344 dw_loc_descr_ref result = NULL;
14346 if (dwarf_version >= 4 || !dwarf_strict)
14348 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
14349 initialized);
14350 if (result)
14351 add_loc_descr (&result,
14352 new_loc_descr (DW_OP_stack_value, 0, 0));
14354 return result;
14357 regs = targetm.dwarf_register_span (rtl);
14359 if (REG_NREGS (rtl) > 1 || regs)
14360 return multiple_reg_loc_descriptor (rtl, regs, initialized);
14361 else
14363 unsigned int debugger_regnum = debugger_reg_number (rtl);
14364 if (debugger_regnum == IGNORED_DWARF_REGNUM)
14365 return 0;
14366 return one_reg_loc_descriptor (debugger_regnum, initialized);
14370 /* Return a location descriptor that designates a machine register for
14371 a given hard register number. */
14373 static dw_loc_descr_ref
14374 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
14376 dw_loc_descr_ref reg_loc_descr;
14378 if (regno <= 31)
14379 reg_loc_descr
14380 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
14381 else
14382 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
14384 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14385 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14387 return reg_loc_descr;
14390 /* Given an RTL of a register, return a location descriptor that
14391 designates a value that spans more than one register. */
14393 static dw_loc_descr_ref
14394 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
14395 enum var_init_status initialized)
14397 int size, i;
14398 dw_loc_descr_ref loc_result = NULL;
14400 /* Simple, contiguous registers. */
14401 if (regs == NULL_RTX)
14403 unsigned reg = REGNO (rtl);
14404 int nregs;
14406 #ifdef LEAF_REG_REMAP
14407 if (crtl->uses_only_leaf_regs)
14409 int leaf_reg = LEAF_REG_REMAP (reg);
14410 if (leaf_reg != -1)
14411 reg = (unsigned) leaf_reg;
14413 #endif
14415 gcc_assert ((unsigned) DEBUGGER_REGNO (reg) == debugger_reg_number (rtl));
14416 nregs = REG_NREGS (rtl);
14418 /* At present we only track constant-sized pieces. */
14419 if (!GET_MODE_SIZE (GET_MODE (rtl)).is_constant (&size))
14420 return NULL;
14421 size /= nregs;
14423 loc_result = NULL;
14424 while (nregs--)
14426 dw_loc_descr_ref t;
14428 t = one_reg_loc_descriptor (DEBUGGER_REGNO (reg),
14429 VAR_INIT_STATUS_INITIALIZED);
14430 add_loc_descr (&loc_result, t);
14431 add_loc_descr_op_piece (&loc_result, size);
14432 ++reg;
14434 return loc_result;
14437 /* Now onto stupid register sets in non contiguous locations. */
14439 gcc_assert (GET_CODE (regs) == PARALLEL);
14441 /* At present we only track constant-sized pieces. */
14442 if (!GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0))).is_constant (&size))
14443 return NULL;
14444 loc_result = NULL;
14446 for (i = 0; i < XVECLEN (regs, 0); ++i)
14448 dw_loc_descr_ref t;
14450 t = one_reg_loc_descriptor (debugger_reg_number (XVECEXP (regs, 0, i)),
14451 VAR_INIT_STATUS_INITIALIZED);
14452 add_loc_descr (&loc_result, t);
14453 add_loc_descr_op_piece (&loc_result, size);
14456 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14457 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14458 return loc_result;
14461 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
14463 /* Return a location descriptor that designates a constant i,
14464 as a compound operation from constant (i >> shift), constant shift
14465 and DW_OP_shl. */
14467 static dw_loc_descr_ref
14468 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
14470 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
14471 add_loc_descr (&ret, int_loc_descriptor (shift));
14472 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14473 return ret;
14476 /* Return a location descriptor that designates constant POLY_I. */
14478 static dw_loc_descr_ref
14479 int_loc_descriptor (poly_int64 poly_i)
14481 enum dwarf_location_atom op;
14483 HOST_WIDE_INT i;
14484 if (!poly_i.is_constant (&i))
14486 /* Create location descriptions for the non-constant part and
14487 add any constant offset at the end. */
14488 dw_loc_descr_ref ret = NULL;
14489 HOST_WIDE_INT constant = poly_i.coeffs[0];
14490 for (unsigned int j = 1; j < NUM_POLY_INT_COEFFS; ++j)
14492 HOST_WIDE_INT coeff = poly_i.coeffs[j];
14493 if (coeff != 0)
14495 dw_loc_descr_ref start = ret;
14496 unsigned int factor;
14497 int bias;
14498 unsigned int regno = targetm.dwarf_poly_indeterminate_value
14499 (j, &factor, &bias);
14501 /* Add COEFF * ((REGNO / FACTOR) - BIAS) to the value:
14502 add COEFF * (REGNO / FACTOR) now and subtract
14503 COEFF * BIAS from the final constant part. */
14504 constant -= coeff * bias;
14505 add_loc_descr (&ret, new_reg_loc_descr (regno, 0));
14506 if (coeff % factor == 0)
14507 coeff /= factor;
14508 else
14510 int amount = exact_log2 (factor);
14511 gcc_assert (amount >= 0);
14512 add_loc_descr (&ret, int_loc_descriptor (amount));
14513 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14515 if (coeff != 1)
14517 add_loc_descr (&ret, int_loc_descriptor (coeff));
14518 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
14520 if (start)
14521 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
14524 loc_descr_plus_const (&ret, constant);
14525 return ret;
14528 /* Pick the smallest representation of a constant, rather than just
14529 defaulting to the LEB encoding. */
14530 if (i >= 0)
14532 int clz = clz_hwi (i);
14533 int ctz = ctz_hwi (i);
14534 if (i <= 31)
14535 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
14536 else if (i <= 0xff)
14537 op = DW_OP_const1u;
14538 else if (i <= 0xffff)
14539 op = DW_OP_const2u;
14540 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
14541 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
14542 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
14543 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
14544 while DW_OP_const4u is 5 bytes. */
14545 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
14546 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14547 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
14548 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
14549 while DW_OP_const4u is 5 bytes. */
14550 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
14552 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
14553 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
14554 <= 4)
14556 /* As i >= 2**31, the double cast above will yield a negative number.
14557 Since wrapping is defined in DWARF expressions we can output big
14558 positive integers as small negative ones, regardless of the size
14559 of host wide ints.
14561 Here, since the evaluator will handle 32-bit values and since i >=
14562 2**31, we know it's going to be interpreted as a negative literal:
14563 store it this way if we can do better than 5 bytes this way. */
14564 return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
14566 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14567 op = DW_OP_const4u;
14569 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
14570 least 6 bytes: see if we can do better before falling back to it. */
14571 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14572 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
14573 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
14574 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
14575 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
14576 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
14577 >= HOST_BITS_PER_WIDE_INT)
14578 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
14579 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
14580 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
14581 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
14582 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
14583 && size_of_uleb128 (i) > 6)
14584 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
14585 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
14586 else
14587 op = DW_OP_constu;
14589 else
14591 if (i >= -0x80)
14592 op = DW_OP_const1s;
14593 else if (i >= -0x8000)
14594 op = DW_OP_const2s;
14595 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
14597 if (size_of_int_loc_descriptor (i) < 5)
14599 dw_loc_descr_ref ret = int_loc_descriptor (-i);
14600 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14601 return ret;
14603 op = DW_OP_const4s;
14605 else
14607 if (size_of_int_loc_descriptor (i)
14608 < (unsigned long) 1 + size_of_sleb128 (i))
14610 dw_loc_descr_ref ret = int_loc_descriptor (-i);
14611 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14612 return ret;
14614 op = DW_OP_consts;
14618 return new_loc_descr (op, i, 0);
14621 /* Likewise, for unsigned constants. */
14623 static dw_loc_descr_ref
14624 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
14626 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
14627 const unsigned HOST_WIDE_INT max_uint
14628 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
14630 /* If possible, use the clever signed constants handling. */
14631 if (i <= max_int)
14632 return int_loc_descriptor ((HOST_WIDE_INT) i);
14634 /* Here, we are left with positive numbers that cannot be represented as
14635 HOST_WIDE_INT, i.e.:
14636 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
14638 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
14639 whereas may be better to output a negative integer: thanks to integer
14640 wrapping, we know that:
14641 x = x - 2 ** DWARF2_ADDR_SIZE
14642 = x - 2 * (max (HOST_WIDE_INT) + 1)
14643 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
14644 small negative integers. Let's try that in cases it will clearly improve
14645 the encoding: there is no gain turning DW_OP_const4u into
14646 DW_OP_const4s. */
14647 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
14648 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
14649 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
14651 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
14653 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
14654 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
14655 const HOST_WIDE_INT second_shift
14656 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
14658 /* So we finally have:
14659 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
14660 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
14661 return int_loc_descriptor (second_shift);
14664 /* Last chance: fallback to a simple constant operation. */
14665 return new_loc_descr
14666 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14667 ? DW_OP_const4u
14668 : DW_OP_const8u,
14669 i, 0);
14672 /* Generate and return a location description that computes the unsigned
14673 comparison of the two stack top entries (a OP b where b is the top-most
14674 entry and a is the second one). The KIND of comparison can be LT_EXPR,
14675 LE_EXPR, GT_EXPR or GE_EXPR. */
14677 static dw_loc_descr_ref
14678 uint_comparison_loc_list (enum tree_code kind)
14680 enum dwarf_location_atom op, flip_op;
14681 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
14683 switch (kind)
14685 case LT_EXPR:
14686 op = DW_OP_lt;
14687 break;
14688 case LE_EXPR:
14689 op = DW_OP_le;
14690 break;
14691 case GT_EXPR:
14692 op = DW_OP_gt;
14693 break;
14694 case GE_EXPR:
14695 op = DW_OP_ge;
14696 break;
14697 default:
14698 gcc_unreachable ();
14701 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14702 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
14704 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
14705 possible to perform unsigned comparisons: we just have to distinguish
14706 three cases:
14708 1. when a and b have the same sign (as signed integers); then we should
14709 return: a OP(signed) b;
14711 2. when a is a negative signed integer while b is a positive one, then a
14712 is a greater unsigned integer than b; likewise when a and b's roles
14713 are flipped.
14715 So first, compare the sign of the two operands. */
14716 ret = new_loc_descr (DW_OP_over, 0, 0);
14717 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14718 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
14719 /* If they have different signs (i.e. they have different sign bits), then
14720 the stack top value has now the sign bit set and thus it's smaller than
14721 zero. */
14722 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
14723 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
14724 add_loc_descr (&ret, bra_node);
14726 /* We are in case 1. At this point, we know both operands have the same
14727 sign, to it's safe to use the built-in signed comparison. */
14728 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14729 add_loc_descr (&ret, jmp_node);
14731 /* We are in case 2. Here, we know both operands do not have the same sign,
14732 so we have to flip the signed comparison. */
14733 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
14734 tmp = new_loc_descr (flip_op, 0, 0);
14735 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14736 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
14737 add_loc_descr (&ret, tmp);
14739 /* This dummy operation is necessary to make the two branches join. */
14740 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14741 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14742 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
14743 add_loc_descr (&ret, tmp);
14745 return ret;
14748 /* Likewise, but takes the location description lists (might be destructive on
14749 them). Return NULL if either is NULL or if concatenation fails. */
14751 static dw_loc_list_ref
14752 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
14753 enum tree_code kind)
14755 if (left == NULL || right == NULL)
14756 return NULL;
14758 add_loc_list (&left, right);
14759 if (left == NULL)
14760 return NULL;
14762 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
14763 return left;
14766 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
14767 without actually allocating it. */
14769 static unsigned long
14770 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
14772 return size_of_int_loc_descriptor (i >> shift)
14773 + size_of_int_loc_descriptor (shift)
14774 + 1;
14777 /* Return size_of_locs (int_loc_descriptor (i)) without
14778 actually allocating it. */
14780 static unsigned long
14781 size_of_int_loc_descriptor (HOST_WIDE_INT i)
14783 unsigned long s;
14785 if (i >= 0)
14787 int clz, ctz;
14788 if (i <= 31)
14789 return 1;
14790 else if (i <= 0xff)
14791 return 2;
14792 else if (i <= 0xffff)
14793 return 3;
14794 clz = clz_hwi (i);
14795 ctz = ctz_hwi (i);
14796 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
14797 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
14798 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14799 - clz - 5);
14800 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14801 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
14802 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14803 - clz - 8);
14804 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
14805 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
14806 <= 4)
14807 return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
14808 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14809 return 5;
14810 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
14811 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14812 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
14813 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14814 - clz - 8);
14815 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
14816 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
14817 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14818 - clz - 16);
14819 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
14820 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
14821 && s > 6)
14822 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14823 - clz - 32);
14824 else
14825 return 1 + s;
14827 else
14829 if (i >= -0x80)
14830 return 2;
14831 else if (i >= -0x8000)
14832 return 3;
14833 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
14835 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
14837 s = size_of_int_loc_descriptor (-i) + 1;
14838 if (s < 5)
14839 return s;
14841 return 5;
14843 else
14845 unsigned long r = 1 + size_of_sleb128 (i);
14846 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
14848 s = size_of_int_loc_descriptor (-i) + 1;
14849 if (s < r)
14850 return s;
14852 return r;
14857 /* Return loc description representing "address" of integer value.
14858 This can appear only as toplevel expression. */
14860 static dw_loc_descr_ref
14861 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
14863 int litsize;
14864 dw_loc_descr_ref loc_result = NULL;
14866 if (!(dwarf_version >= 4 || !dwarf_strict))
14867 return NULL;
14869 litsize = size_of_int_loc_descriptor (i);
14870 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
14871 is more compact. For DW_OP_stack_value we need:
14872 litsize + 1 (DW_OP_stack_value)
14873 and for DW_OP_implicit_value:
14874 1 (DW_OP_implicit_value) + 1 (length) + size. */
14875 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
14877 loc_result = int_loc_descriptor (i);
14878 add_loc_descr (&loc_result,
14879 new_loc_descr (DW_OP_stack_value, 0, 0));
14880 return loc_result;
14883 loc_result = new_loc_descr (DW_OP_implicit_value,
14884 size, 0);
14885 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
14886 loc_result->dw_loc_oprnd2.v.val_int = i;
14887 return loc_result;
14890 /* Return a location descriptor that designates a base+offset location. */
14892 static dw_loc_descr_ref
14893 based_loc_descr (rtx reg, poly_int64 offset,
14894 enum var_init_status initialized)
14896 unsigned int regno;
14897 dw_loc_descr_ref result;
14898 dw_fde_ref fde = cfun->fde;
14900 /* We only use "frame base" when we're sure we're talking about the
14901 post-prologue local stack frame. We do this by *not* running
14902 register elimination until this point, and recognizing the special
14903 argument pointer and soft frame pointer rtx's. */
14904 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
14906 rtx elim = (ira_use_lra_p
14907 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
14908 : eliminate_regs (reg, VOIDmode, NULL_RTX));
14910 if (elim != reg)
14912 /* Allow hard frame pointer here even if frame pointer
14913 isn't used since hard frame pointer is encoded with
14914 DW_OP_fbreg which uses the DW_AT_frame_base attribute,
14915 not hard frame pointer directly. */
14916 elim = strip_offset_and_add (elim, &offset);
14917 gcc_assert (elim == hard_frame_pointer_rtx
14918 || elim == stack_pointer_rtx);
14920 /* If drap register is used to align stack, use frame
14921 pointer + offset to access stack variables. If stack
14922 is aligned without drap, use stack pointer + offset to
14923 access stack variables. */
14924 if (crtl->stack_realign_tried
14925 && reg == frame_pointer_rtx)
14927 int base_reg
14928 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
14929 ? HARD_FRAME_POINTER_REGNUM
14930 : REGNO (elim));
14931 return new_reg_loc_descr (base_reg, offset);
14934 gcc_assert (frame_pointer_fb_offset_valid);
14935 offset += frame_pointer_fb_offset;
14936 HOST_WIDE_INT const_offset;
14937 if (offset.is_constant (&const_offset))
14938 return new_loc_descr (DW_OP_fbreg, const_offset, 0);
14939 else
14941 dw_loc_descr_ref ret = new_loc_descr (DW_OP_fbreg, 0, 0);
14942 loc_descr_plus_const (&ret, offset);
14943 return ret;
14948 regno = REGNO (reg);
14949 #ifdef LEAF_REG_REMAP
14950 if (crtl->uses_only_leaf_regs)
14952 int leaf_reg = LEAF_REG_REMAP (regno);
14953 if (leaf_reg != -1)
14954 regno = (unsigned) leaf_reg;
14956 #endif
14957 regno = DWARF_FRAME_REGNUM (regno);
14959 HOST_WIDE_INT const_offset;
14960 if (!optimize && fde
14961 && (fde->drap_reg == regno || fde->vdrap_reg == regno)
14962 && offset.is_constant (&const_offset))
14964 /* Use cfa+offset to represent the location of arguments passed
14965 on the stack when drap is used to align stack.
14966 Only do this when not optimizing, for optimized code var-tracking
14967 is supposed to track where the arguments live and the register
14968 used as vdrap or drap in some spot might be used for something
14969 else in other part of the routine. */
14970 return new_loc_descr (DW_OP_fbreg, const_offset, 0);
14973 result = new_reg_loc_descr (regno, offset);
14975 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14976 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14978 return result;
14981 /* Return true if this RTL expression describes a base+offset calculation. */
14983 static inline bool
14984 is_based_loc (const_rtx rtl)
14986 return (GET_CODE (rtl) == PLUS
14987 && ((REG_P (XEXP (rtl, 0))
14988 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
14989 && CONST_INT_P (XEXP (rtl, 1)))));
14992 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
14993 failed. */
14995 static dw_loc_descr_ref
14996 tls_mem_loc_descriptor (rtx mem)
14998 tree base;
14999 dw_loc_descr_ref loc_result;
15001 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
15002 return NULL;
15004 base = get_base_address (MEM_EXPR (mem));
15005 if (base == NULL
15006 || !VAR_P (base)
15007 || !DECL_THREAD_LOCAL_P (base))
15008 return NULL;
15010 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
15011 if (loc_result == NULL)
15012 return NULL;
15014 if (maybe_ne (MEM_OFFSET (mem), 0))
15015 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
15017 return loc_result;
15020 /* Output debug info about reason why we failed to expand expression as dwarf
15021 expression. */
15023 static void
15024 expansion_failed (tree expr, rtx rtl, char const *reason)
15026 if (dump_file && (dump_flags & TDF_DETAILS))
15028 fprintf (dump_file, "Failed to expand as dwarf: ");
15029 if (expr)
15030 print_generic_expr (dump_file, expr, dump_flags);
15031 if (rtl)
15033 fprintf (dump_file, "\n");
15034 print_rtl (dump_file, rtl);
15036 fprintf (dump_file, "\nReason: %s\n", reason);
15040 /* Helper function for const_ok_for_output. */
15042 static bool
15043 const_ok_for_output_1 (rtx rtl)
15045 if (targetm.const_not_ok_for_debug_p (rtl))
15047 if (GET_CODE (rtl) != UNSPEC)
15049 expansion_failed (NULL_TREE, rtl,
15050 "Expression rejected for debug by the backend.\n");
15051 return false;
15054 /* If delegitimize_address couldn't do anything with the UNSPEC, and
15055 the target hook doesn't explicitly allow it in debug info, assume
15056 we can't express it in the debug info. */
15057 /* Don't complain about TLS UNSPECs, those are just too hard to
15058 delegitimize. Note this could be a non-decl SYMBOL_REF such as
15059 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
15060 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
15061 if (flag_checking
15062 && (XVECLEN (rtl, 0) == 0
15063 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
15064 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
15065 inform (current_function_decl
15066 ? DECL_SOURCE_LOCATION (current_function_decl)
15067 : UNKNOWN_LOCATION,
15068 #if NUM_UNSPEC_VALUES > 0
15069 "non-delegitimized UNSPEC %s (%d) found in variable location",
15070 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
15071 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
15072 #else
15073 "non-delegitimized UNSPEC %d found in variable location",
15074 #endif
15075 XINT (rtl, 1));
15076 expansion_failed (NULL_TREE, rtl,
15077 "UNSPEC hasn't been delegitimized.\n");
15078 return false;
15081 if (CONST_POLY_INT_P (rtl))
15082 return false;
15084 /* FIXME: Refer to PR60655. It is possible for simplification
15085 of rtl expressions in var tracking to produce such expressions.
15086 We should really identify / validate expressions
15087 enclosed in CONST that can be handled by assemblers on various
15088 targets and only handle legitimate cases here. */
15089 switch (GET_CODE (rtl))
15091 case SYMBOL_REF:
15092 break;
15093 case NOT:
15094 case NEG:
15095 return false;
15096 case PLUS:
15098 /* Make sure SYMBOL_REFs/UNSPECs are at most in one of the
15099 operands. */
15100 subrtx_var_iterator::array_type array;
15101 bool first = false;
15102 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
15103 if (SYMBOL_REF_P (*iter)
15104 || LABEL_P (*iter)
15105 || GET_CODE (*iter) == UNSPEC)
15107 first = true;
15108 break;
15110 if (!first)
15111 return true;
15112 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 1), ALL)
15113 if (SYMBOL_REF_P (*iter)
15114 || LABEL_P (*iter)
15115 || GET_CODE (*iter) == UNSPEC)
15116 return false;
15117 return true;
15119 case MINUS:
15121 /* Disallow negation of SYMBOL_REFs or UNSPECs when they
15122 appear in the second operand of MINUS. */
15123 subrtx_var_iterator::array_type array;
15124 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 1), ALL)
15125 if (SYMBOL_REF_P (*iter)
15126 || LABEL_P (*iter)
15127 || GET_CODE (*iter) == UNSPEC)
15128 return false;
15129 return true;
15131 default:
15132 return true;
15135 if (CONSTANT_POOL_ADDRESS_P (rtl))
15137 bool marked;
15138 get_pool_constant_mark (rtl, &marked);
15139 /* If all references to this pool constant were optimized away,
15140 it was not output and thus we can't represent it. */
15141 if (!marked)
15143 expansion_failed (NULL_TREE, rtl,
15144 "Constant was removed from constant pool.\n");
15145 return false;
15149 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
15150 return false;
15152 /* Avoid references to external symbols in debug info, on several targets
15153 the linker might even refuse to link when linking a shared library,
15154 and in many other cases the relocations for .debug_info/.debug_loc are
15155 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
15156 to be defined within the same shared library or executable are fine. */
15157 if (SYMBOL_REF_EXTERNAL_P (rtl))
15159 tree decl = SYMBOL_REF_DECL (rtl);
15161 if (decl == NULL || !targetm.binds_local_p (decl))
15163 expansion_failed (NULL_TREE, rtl,
15164 "Symbol not defined in current TU.\n");
15165 return false;
15169 return true;
15172 /* Return true if constant RTL can be emitted in DW_OP_addr or
15173 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
15174 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
15176 static bool
15177 const_ok_for_output (rtx rtl)
15179 if (GET_CODE (rtl) == SYMBOL_REF)
15180 return const_ok_for_output_1 (rtl);
15182 if (GET_CODE (rtl) == CONST)
15184 subrtx_var_iterator::array_type array;
15185 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
15186 if (!const_ok_for_output_1 (*iter))
15187 return false;
15188 return true;
15191 return true;
15194 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
15195 if possible, NULL otherwise. */
15197 static dw_die_ref
15198 base_type_for_mode (machine_mode mode, bool unsignedp)
15200 dw_die_ref type_die;
15201 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
15203 if (type == NULL)
15204 return NULL;
15205 switch (TREE_CODE (type))
15207 case INTEGER_TYPE:
15208 case REAL_TYPE:
15209 break;
15210 default:
15211 return NULL;
15213 type_die = lookup_type_die (type);
15214 if (!type_die)
15215 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
15216 comp_unit_die ());
15217 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
15218 return NULL;
15219 return type_die;
15222 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
15223 type matching MODE, or, if MODE is narrower than or as wide as
15224 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
15225 possible. */
15227 static dw_loc_descr_ref
15228 convert_descriptor_to_mode (scalar_int_mode mode, dw_loc_descr_ref op)
15230 machine_mode outer_mode = mode;
15231 dw_die_ref type_die;
15232 dw_loc_descr_ref cvt;
15234 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
15236 add_loc_descr (&op, new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
15237 return op;
15239 type_die = base_type_for_mode (outer_mode, 1);
15240 if (type_die == NULL)
15241 return NULL;
15242 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15243 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15244 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15245 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15246 add_loc_descr (&op, cvt);
15247 return op;
15250 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
15252 static dw_loc_descr_ref
15253 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
15254 dw_loc_descr_ref op1)
15256 dw_loc_descr_ref ret = op0;
15257 add_loc_descr (&ret, op1);
15258 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
15259 if (STORE_FLAG_VALUE != 1)
15261 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
15262 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
15264 return ret;
15267 /* Subroutine of scompare_loc_descriptor for the case in which we're
15268 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
15269 and in which OP_MODE is bigger than DWARF2_ADDR_SIZE. */
15271 static dw_loc_descr_ref
15272 scompare_loc_descriptor_wide (enum dwarf_location_atom op,
15273 scalar_int_mode op_mode,
15274 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
15276 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
15277 dw_loc_descr_ref cvt;
15279 if (type_die == NULL)
15280 return NULL;
15281 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15282 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15283 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15284 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15285 add_loc_descr (&op0, cvt);
15286 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15287 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15288 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15289 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15290 add_loc_descr (&op1, cvt);
15291 return compare_loc_descriptor (op, op0, op1);
15294 /* Subroutine of scompare_loc_descriptor for the case in which we're
15295 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
15296 and in which OP_MODE is smaller than DWARF2_ADDR_SIZE. */
15298 static dw_loc_descr_ref
15299 scompare_loc_descriptor_narrow (enum dwarf_location_atom op, rtx rtl,
15300 scalar_int_mode op_mode,
15301 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
15303 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
15304 /* For eq/ne, if the operands are known to be zero-extended,
15305 there is no need to do the fancy shifting up. */
15306 if (op == DW_OP_eq || op == DW_OP_ne)
15308 dw_loc_descr_ref last0, last1;
15309 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
15311 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
15313 /* deref_size zero extends, and for constants we can check
15314 whether they are zero extended or not. */
15315 if (((last0->dw_loc_opc == DW_OP_deref_size
15316 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
15317 || (CONST_INT_P (XEXP (rtl, 0))
15318 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
15319 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
15320 && ((last1->dw_loc_opc == DW_OP_deref_size
15321 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
15322 || (CONST_INT_P (XEXP (rtl, 1))
15323 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
15324 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
15325 return compare_loc_descriptor (op, op0, op1);
15327 /* EQ/NE comparison against constant in narrower type than
15328 DWARF2_ADDR_SIZE can be performed either as
15329 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
15330 DW_OP_{eq,ne}
15332 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
15333 DW_OP_{eq,ne}. Pick whatever is shorter. */
15334 if (CONST_INT_P (XEXP (rtl, 1))
15335 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
15336 && (size_of_int_loc_descriptor (shift) + 1
15337 + size_of_int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift)
15338 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
15339 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
15340 & GET_MODE_MASK (op_mode))))
15342 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
15343 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
15344 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
15345 & GET_MODE_MASK (op_mode));
15346 return compare_loc_descriptor (op, op0, op1);
15349 add_loc_descr (&op0, int_loc_descriptor (shift));
15350 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
15351 if (CONST_INT_P (XEXP (rtl, 1)))
15352 op1 = int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift);
15353 else
15355 add_loc_descr (&op1, int_loc_descriptor (shift));
15356 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
15358 return compare_loc_descriptor (op, op0, op1);
15361 /* Return location descriptor for signed comparison OP RTL. */
15363 static dw_loc_descr_ref
15364 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
15365 machine_mode mem_mode)
15367 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
15368 dw_loc_descr_ref op0, op1;
15370 if (op_mode == VOIDmode)
15371 op_mode = GET_MODE (XEXP (rtl, 1));
15372 if (op_mode == VOIDmode)
15373 return NULL;
15375 scalar_int_mode int_op_mode;
15376 if (dwarf_strict
15377 && dwarf_version < 5
15378 && (!is_a <scalar_int_mode> (op_mode, &int_op_mode)
15379 || GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE))
15380 return NULL;
15382 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
15383 VAR_INIT_STATUS_INITIALIZED);
15384 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
15385 VAR_INIT_STATUS_INITIALIZED);
15387 if (op0 == NULL || op1 == NULL)
15388 return NULL;
15390 if (is_a <scalar_int_mode> (op_mode, &int_op_mode))
15392 if (GET_MODE_SIZE (int_op_mode) < DWARF2_ADDR_SIZE)
15393 return scompare_loc_descriptor_narrow (op, rtl, int_op_mode, op0, op1);
15395 if (GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE)
15396 return scompare_loc_descriptor_wide (op, int_op_mode, op0, op1);
15398 return compare_loc_descriptor (op, op0, op1);
15401 /* Return location descriptor for unsigned comparison OP RTL. */
15403 static dw_loc_descr_ref
15404 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
15405 machine_mode mem_mode)
15407 dw_loc_descr_ref op0, op1;
15409 machine_mode test_op_mode = GET_MODE (XEXP (rtl, 0));
15410 if (test_op_mode == VOIDmode)
15411 test_op_mode = GET_MODE (XEXP (rtl, 1));
15413 scalar_int_mode op_mode;
15414 if (!is_a <scalar_int_mode> (test_op_mode, &op_mode))
15415 return NULL;
15417 if (dwarf_strict
15418 && dwarf_version < 5
15419 && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
15420 return NULL;
15422 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
15423 VAR_INIT_STATUS_INITIALIZED);
15424 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
15425 VAR_INIT_STATUS_INITIALIZED);
15427 if (op0 == NULL || op1 == NULL)
15428 return NULL;
15430 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
15432 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
15433 dw_loc_descr_ref last0, last1;
15434 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
15436 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
15438 if (CONST_INT_P (XEXP (rtl, 0)))
15439 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
15440 /* deref_size zero extends, so no need to mask it again. */
15441 else if (last0->dw_loc_opc != DW_OP_deref_size
15442 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
15444 add_loc_descr (&op0, int_loc_descriptor (mask));
15445 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
15447 if (CONST_INT_P (XEXP (rtl, 1)))
15448 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
15449 /* deref_size zero extends, so no need to mask it again. */
15450 else if (last1->dw_loc_opc != DW_OP_deref_size
15451 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
15453 add_loc_descr (&op1, int_loc_descriptor (mask));
15454 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
15457 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
15459 HOST_WIDE_INT bias = 1;
15460 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
15461 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
15462 if (CONST_INT_P (XEXP (rtl, 1)))
15463 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
15464 + INTVAL (XEXP (rtl, 1)));
15465 else
15466 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
15467 bias, 0));
15469 return compare_loc_descriptor (op, op0, op1);
15472 /* Return location descriptor for {U,S}{MIN,MAX}. */
15474 static dw_loc_descr_ref
15475 minmax_loc_descriptor (rtx rtl, machine_mode mode,
15476 machine_mode mem_mode)
15478 enum dwarf_location_atom op;
15479 dw_loc_descr_ref op0, op1, ret;
15480 dw_loc_descr_ref bra_node, drop_node;
15482 scalar_int_mode int_mode;
15483 if (dwarf_strict
15484 && dwarf_version < 5
15485 && (!is_a <scalar_int_mode> (mode, &int_mode)
15486 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE))
15487 return NULL;
15489 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15490 VAR_INIT_STATUS_INITIALIZED);
15491 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15492 VAR_INIT_STATUS_INITIALIZED);
15494 if (op0 == NULL || op1 == NULL)
15495 return NULL;
15497 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
15498 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
15499 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
15500 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
15502 /* Checked by the caller. */
15503 int_mode = as_a <scalar_int_mode> (mode);
15504 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
15506 HOST_WIDE_INT mask = GET_MODE_MASK (int_mode);
15507 add_loc_descr (&op0, int_loc_descriptor (mask));
15508 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
15509 add_loc_descr (&op1, int_loc_descriptor (mask));
15510 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
15512 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
15514 HOST_WIDE_INT bias = 1;
15515 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
15516 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
15517 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
15520 else if (is_a <scalar_int_mode> (mode, &int_mode)
15521 && GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
15523 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (int_mode)) * BITS_PER_UNIT;
15524 add_loc_descr (&op0, int_loc_descriptor (shift));
15525 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
15526 add_loc_descr (&op1, int_loc_descriptor (shift));
15527 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
15529 else if (is_a <scalar_int_mode> (mode, &int_mode)
15530 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15532 dw_die_ref type_die = base_type_for_mode (int_mode, 0);
15533 dw_loc_descr_ref cvt;
15534 if (type_die == NULL)
15535 return NULL;
15536 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15537 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15538 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15539 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15540 add_loc_descr (&op0, cvt);
15541 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15542 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15543 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15544 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15545 add_loc_descr (&op1, cvt);
15548 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
15549 op = DW_OP_lt;
15550 else
15551 op = DW_OP_gt;
15552 ret = op0;
15553 add_loc_descr (&ret, op1);
15554 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
15555 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15556 add_loc_descr (&ret, bra_node);
15557 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15558 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
15559 add_loc_descr (&ret, drop_node);
15560 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15561 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
15562 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
15563 && is_a <scalar_int_mode> (mode, &int_mode)
15564 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15565 ret = convert_descriptor_to_mode (int_mode, ret);
15566 return ret;
15569 /* Helper function for mem_loc_descriptor. Perform OP binary op,
15570 but after converting arguments to type_die, afterwards
15571 convert back to unsigned. */
15573 static dw_loc_descr_ref
15574 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
15575 scalar_int_mode mode, machine_mode mem_mode)
15577 dw_loc_descr_ref cvt, op0, op1;
15579 if (type_die == NULL)
15580 return NULL;
15581 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15582 VAR_INIT_STATUS_INITIALIZED);
15583 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15584 VAR_INIT_STATUS_INITIALIZED);
15585 if (op0 == NULL || op1 == NULL)
15586 return NULL;
15587 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15588 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15589 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15590 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15591 add_loc_descr (&op0, cvt);
15592 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15593 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15594 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15595 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15596 add_loc_descr (&op1, cvt);
15597 add_loc_descr (&op0, op1);
15598 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
15599 return convert_descriptor_to_mode (mode, op0);
15602 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
15603 const0 is DW_OP_lit0 or corresponding typed constant,
15604 const1 is DW_OP_lit1 or corresponding typed constant
15605 and constMSB is constant with just the MSB bit set
15606 for the mode):
15607 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
15608 L1: const0 DW_OP_swap
15609 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
15610 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15611 L3: DW_OP_drop
15612 L4: DW_OP_nop
15614 CTZ is similar:
15615 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
15616 L1: const0 DW_OP_swap
15617 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
15618 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15619 L3: DW_OP_drop
15620 L4: DW_OP_nop
15622 FFS is similar:
15623 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
15624 L1: const1 DW_OP_swap
15625 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
15626 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15627 L3: DW_OP_drop
15628 L4: DW_OP_nop */
15630 static dw_loc_descr_ref
15631 clz_loc_descriptor (rtx rtl, scalar_int_mode mode,
15632 machine_mode mem_mode)
15634 dw_loc_descr_ref op0, ret, tmp;
15635 HOST_WIDE_INT valv;
15636 dw_loc_descr_ref l1jump, l1label;
15637 dw_loc_descr_ref l2jump, l2label;
15638 dw_loc_descr_ref l3jump, l3label;
15639 dw_loc_descr_ref l4jump, l4label;
15640 rtx msb;
15642 if (GET_MODE (XEXP (rtl, 0)) != mode)
15643 return NULL;
15645 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15646 VAR_INIT_STATUS_INITIALIZED);
15647 if (op0 == NULL)
15648 return NULL;
15649 ret = op0;
15650 if (GET_CODE (rtl) == CLZ)
15652 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
15653 valv = GET_MODE_BITSIZE (mode);
15655 else if (GET_CODE (rtl) == FFS)
15656 valv = 0;
15657 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
15658 valv = GET_MODE_BITSIZE (mode);
15659 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15660 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
15661 add_loc_descr (&ret, l1jump);
15662 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
15663 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
15664 VAR_INIT_STATUS_INITIALIZED);
15665 if (tmp == NULL)
15666 return NULL;
15667 add_loc_descr (&ret, tmp);
15668 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
15669 add_loc_descr (&ret, l4jump);
15670 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
15671 ? const1_rtx : const0_rtx,
15672 mode, mem_mode,
15673 VAR_INIT_STATUS_INITIALIZED);
15674 if (l1label == NULL)
15675 return NULL;
15676 add_loc_descr (&ret, l1label);
15677 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15678 l2label = new_loc_descr (DW_OP_dup, 0, 0);
15679 add_loc_descr (&ret, l2label);
15680 if (GET_CODE (rtl) != CLZ)
15681 msb = const1_rtx;
15682 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
15683 msb = GEN_INT (HOST_WIDE_INT_1U
15684 << (GET_MODE_BITSIZE (mode) - 1));
15685 else
15686 msb = immed_wide_int_const
15687 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
15688 GET_MODE_PRECISION (mode)), mode);
15689 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
15690 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
15691 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
15692 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
15693 else
15694 tmp = mem_loc_descriptor (msb, mode, mem_mode,
15695 VAR_INIT_STATUS_INITIALIZED);
15696 if (tmp == NULL)
15697 return NULL;
15698 add_loc_descr (&ret, tmp);
15699 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15700 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
15701 add_loc_descr (&ret, l3jump);
15702 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15703 VAR_INIT_STATUS_INITIALIZED);
15704 if (tmp == NULL)
15705 return NULL;
15706 add_loc_descr (&ret, tmp);
15707 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
15708 ? DW_OP_shl : DW_OP_shr, 0, 0));
15709 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15710 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
15711 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15712 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
15713 add_loc_descr (&ret, l2jump);
15714 l3label = new_loc_descr (DW_OP_drop, 0, 0);
15715 add_loc_descr (&ret, l3label);
15716 l4label = new_loc_descr (DW_OP_nop, 0, 0);
15717 add_loc_descr (&ret, l4label);
15718 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15719 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15720 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15721 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15722 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15723 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
15724 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15725 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
15726 return ret;
15729 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
15730 const1 is DW_OP_lit1 or corresponding typed constant):
15731 const0 DW_OP_swap
15732 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
15733 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
15734 L2: DW_OP_drop
15736 PARITY is similar:
15737 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
15738 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
15739 L2: DW_OP_drop */
15741 static dw_loc_descr_ref
15742 popcount_loc_descriptor (rtx rtl, scalar_int_mode mode,
15743 machine_mode mem_mode)
15745 dw_loc_descr_ref op0, ret, tmp;
15746 dw_loc_descr_ref l1jump, l1label;
15747 dw_loc_descr_ref l2jump, l2label;
15749 if (GET_MODE (XEXP (rtl, 0)) != mode)
15750 return NULL;
15752 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15753 VAR_INIT_STATUS_INITIALIZED);
15754 if (op0 == NULL)
15755 return NULL;
15756 ret = op0;
15757 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15758 VAR_INIT_STATUS_INITIALIZED);
15759 if (tmp == NULL)
15760 return NULL;
15761 add_loc_descr (&ret, tmp);
15762 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15763 l1label = new_loc_descr (DW_OP_dup, 0, 0);
15764 add_loc_descr (&ret, l1label);
15765 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
15766 add_loc_descr (&ret, l2jump);
15767 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15768 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
15769 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15770 VAR_INIT_STATUS_INITIALIZED);
15771 if (tmp == NULL)
15772 return NULL;
15773 add_loc_descr (&ret, tmp);
15774 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15775 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
15776 ? DW_OP_plus : DW_OP_xor, 0, 0));
15777 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15778 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15779 VAR_INIT_STATUS_INITIALIZED);
15780 add_loc_descr (&ret, tmp);
15781 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15782 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
15783 add_loc_descr (&ret, l1jump);
15784 l2label = new_loc_descr (DW_OP_drop, 0, 0);
15785 add_loc_descr (&ret, l2label);
15786 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15787 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15788 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15789 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15790 return ret;
15793 /* BSWAP (constS is initial shift count, either 56 or 24):
15794 constS const0
15795 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
15796 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
15797 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
15798 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
15799 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
15801 static dw_loc_descr_ref
15802 bswap_loc_descriptor (rtx rtl, scalar_int_mode mode,
15803 machine_mode mem_mode)
15805 dw_loc_descr_ref op0, ret, tmp;
15806 dw_loc_descr_ref l1jump, l1label;
15807 dw_loc_descr_ref l2jump, l2label;
15809 if (BITS_PER_UNIT != 8
15810 || (GET_MODE_BITSIZE (mode) != 32
15811 && GET_MODE_BITSIZE (mode) != 64))
15812 return NULL;
15814 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15815 VAR_INIT_STATUS_INITIALIZED);
15816 if (op0 == NULL)
15817 return NULL;
15819 ret = op0;
15820 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
15821 mode, mem_mode,
15822 VAR_INIT_STATUS_INITIALIZED);
15823 if (tmp == NULL)
15824 return NULL;
15825 add_loc_descr (&ret, tmp);
15826 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15827 VAR_INIT_STATUS_INITIALIZED);
15828 if (tmp == NULL)
15829 return NULL;
15830 add_loc_descr (&ret, tmp);
15831 l1label = new_loc_descr (DW_OP_pick, 2, 0);
15832 add_loc_descr (&ret, l1label);
15833 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
15834 mode, mem_mode,
15835 VAR_INIT_STATUS_INITIALIZED);
15836 add_loc_descr (&ret, tmp);
15837 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
15838 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
15839 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15840 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
15841 VAR_INIT_STATUS_INITIALIZED);
15842 if (tmp == NULL)
15843 return NULL;
15844 add_loc_descr (&ret, tmp);
15845 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15846 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
15847 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
15848 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
15849 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15850 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15851 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15852 VAR_INIT_STATUS_INITIALIZED);
15853 add_loc_descr (&ret, tmp);
15854 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
15855 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
15856 add_loc_descr (&ret, l2jump);
15857 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
15858 VAR_INIT_STATUS_INITIALIZED);
15859 add_loc_descr (&ret, tmp);
15860 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
15861 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15862 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
15863 add_loc_descr (&ret, l1jump);
15864 l2label = new_loc_descr (DW_OP_drop, 0, 0);
15865 add_loc_descr (&ret, l2label);
15866 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15867 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
15868 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15869 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15870 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15871 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15872 return ret;
15875 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
15876 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
15877 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
15878 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
15880 ROTATERT is similar:
15881 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
15882 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
15883 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
15885 static dw_loc_descr_ref
15886 rotate_loc_descriptor (rtx rtl, scalar_int_mode mode,
15887 machine_mode mem_mode)
15889 rtx rtlop1 = XEXP (rtl, 1);
15890 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
15891 int i;
15893 if (is_narrower_int_mode (GET_MODE (rtlop1), mode))
15894 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
15895 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15896 VAR_INIT_STATUS_INITIALIZED);
15897 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
15898 VAR_INIT_STATUS_INITIALIZED);
15899 if (op0 == NULL || op1 == NULL)
15900 return NULL;
15901 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
15902 for (i = 0; i < 2; i++)
15904 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
15905 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
15906 mode, mem_mode,
15907 VAR_INIT_STATUS_INITIALIZED);
15908 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
15909 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
15910 ? DW_OP_const4u
15911 : HOST_BITS_PER_WIDE_INT == 64
15912 ? DW_OP_const8u : DW_OP_constu,
15913 GET_MODE_MASK (mode), 0);
15914 else
15915 mask[i] = NULL;
15916 if (mask[i] == NULL)
15917 return NULL;
15918 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
15920 ret = op0;
15921 add_loc_descr (&ret, op1);
15922 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
15923 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
15924 if (GET_CODE (rtl) == ROTATERT)
15926 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
15927 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
15928 GET_MODE_BITSIZE (mode), 0));
15930 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
15931 if (mask[0] != NULL)
15932 add_loc_descr (&ret, mask[0]);
15933 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
15934 if (mask[1] != NULL)
15936 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15937 add_loc_descr (&ret, mask[1]);
15938 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15940 if (GET_CODE (rtl) == ROTATE)
15942 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
15943 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
15944 GET_MODE_BITSIZE (mode), 0));
15946 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15947 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
15948 return ret;
15951 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
15952 for DEBUG_PARAMETER_REF RTL. */
15954 static dw_loc_descr_ref
15955 parameter_ref_descriptor (rtx rtl)
15957 dw_loc_descr_ref ret;
15958 dw_die_ref ref;
15960 if (dwarf_strict)
15961 return NULL;
15962 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
15963 /* With LTO during LTRANS we get the late DIE that refers to the early
15964 DIE, thus we add another indirection here. This seems to confuse
15965 gdb enough to make gcc.dg/guality/pr68860-1.c FAIL with LTO. */
15966 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
15967 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
15968 if (ref)
15970 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15971 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15972 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15974 else
15976 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15977 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
15979 return ret;
15982 /* The following routine converts the RTL for a variable or parameter
15983 (resident in memory) into an equivalent Dwarf representation of a
15984 mechanism for getting the address of that same variable onto the top of a
15985 hypothetical "address evaluation" stack.
15987 When creating memory location descriptors, we are effectively transforming
15988 the RTL for a memory-resident object into its Dwarf postfix expression
15989 equivalent. This routine recursively descends an RTL tree, turning
15990 it into Dwarf postfix code as it goes.
15992 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
15994 MEM_MODE is the mode of the memory reference, needed to handle some
15995 autoincrement addressing modes.
15997 Return 0 if we can't represent the location. */
15999 dw_loc_descr_ref
16000 mem_loc_descriptor (rtx rtl, machine_mode mode,
16001 machine_mode mem_mode,
16002 enum var_init_status initialized)
16004 dw_loc_descr_ref mem_loc_result = NULL;
16005 enum dwarf_location_atom op;
16006 dw_loc_descr_ref op0, op1;
16007 rtx inner = NULL_RTX;
16009 if (mode == VOIDmode)
16010 mode = GET_MODE (rtl);
16012 /* Note that for a dynamically sized array, the location we will generate a
16013 description of here will be the lowest numbered location which is
16014 actually within the array. That's *not* necessarily the same as the
16015 zeroth element of the array. */
16017 rtl = targetm.delegitimize_address (rtl);
16019 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
16020 return NULL;
16022 scalar_int_mode int_mode = BImode, inner_mode, op1_mode;
16023 switch (GET_CODE (rtl))
16025 case POST_INC:
16026 case POST_DEC:
16027 case POST_MODIFY:
16028 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
16030 case SUBREG:
16031 /* The case of a subreg may arise when we have a local (register)
16032 variable or a formal (register) parameter which doesn't quite fill
16033 up an entire register. For now, just assume that it is
16034 legitimate to make the Dwarf info refer to the whole register which
16035 contains the given subreg. */
16036 if (!subreg_lowpart_p (rtl))
16037 break;
16038 inner = SUBREG_REG (rtl);
16039 /* FALLTHRU */
16040 case TRUNCATE:
16041 if (inner == NULL_RTX)
16042 inner = XEXP (rtl, 0);
16043 if (is_a <scalar_int_mode> (mode, &int_mode)
16044 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
16045 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16046 #ifdef POINTERS_EXTEND_UNSIGNED
16047 || (int_mode == Pmode && mem_mode != VOIDmode)
16048 #endif
16050 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE)
16052 mem_loc_result = mem_loc_descriptor (inner,
16053 inner_mode,
16054 mem_mode, initialized);
16055 break;
16057 if (dwarf_strict && dwarf_version < 5)
16058 break;
16059 if (is_a <scalar_int_mode> (mode, &int_mode)
16060 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
16061 ? GET_MODE_SIZE (int_mode) <= GET_MODE_SIZE (inner_mode)
16062 : known_eq (GET_MODE_SIZE (mode), GET_MODE_SIZE (GET_MODE (inner))))
16064 dw_die_ref type_die;
16065 dw_loc_descr_ref cvt;
16067 mem_loc_result = mem_loc_descriptor (inner,
16068 GET_MODE (inner),
16069 mem_mode, initialized);
16070 if (mem_loc_result == NULL)
16071 break;
16072 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16073 if (type_die == NULL)
16075 mem_loc_result = NULL;
16076 break;
16078 if (maybe_ne (GET_MODE_SIZE (mode), GET_MODE_SIZE (GET_MODE (inner))))
16079 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16080 else
16081 cvt = new_loc_descr (dwarf_OP (DW_OP_reinterpret), 0, 0);
16082 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16083 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16084 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16085 add_loc_descr (&mem_loc_result, cvt);
16086 if (is_a <scalar_int_mode> (mode, &int_mode)
16087 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
16089 /* Convert it to untyped afterwards. */
16090 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16091 add_loc_descr (&mem_loc_result, cvt);
16094 break;
16096 case REG:
16097 if (!is_a <scalar_int_mode> (mode, &int_mode)
16098 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
16099 && rtl != arg_pointer_rtx
16100 && rtl != frame_pointer_rtx
16101 #ifdef POINTERS_EXTEND_UNSIGNED
16102 && (int_mode != Pmode || mem_mode == VOIDmode)
16103 #endif
16106 dw_die_ref type_die;
16107 unsigned int debugger_regnum;
16109 if (dwarf_strict && dwarf_version < 5)
16110 break;
16111 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
16112 break;
16113 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16114 if (type_die == NULL)
16115 break;
16117 debugger_regnum = debugger_reg_number (rtl);
16118 if (debugger_regnum == IGNORED_DWARF_REGNUM)
16119 break;
16120 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_regval_type),
16121 debugger_regnum, 0);
16122 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
16123 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
16124 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
16125 break;
16127 /* Whenever a register number forms a part of the description of the
16128 method for calculating the (dynamic) address of a memory resident
16129 object, DWARF rules require the register number be referred to as
16130 a "base register". This distinction is not based in any way upon
16131 what category of register the hardware believes the given register
16132 belongs to. This is strictly DWARF terminology we're dealing with
16133 here. Note that in cases where the location of a memory-resident
16134 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
16135 OP_CONST (0)) the actual DWARF location descriptor that we generate
16136 may just be OP_BASEREG (basereg). This may look deceptively like
16137 the object in question was allocated to a register (rather than in
16138 memory) so DWARF consumers need to be aware of the subtle
16139 distinction between OP_REG and OP_BASEREG. */
16140 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
16141 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
16142 else if (stack_realign_drap
16143 && crtl->drap_reg
16144 && crtl->args.internal_arg_pointer == rtl
16145 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
16147 /* If RTL is internal_arg_pointer, which has been optimized
16148 out, use DRAP instead. */
16149 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
16150 VAR_INIT_STATUS_INITIALIZED);
16152 break;
16154 case SIGN_EXTEND:
16155 case ZERO_EXTEND:
16156 if (!is_a <scalar_int_mode> (mode, &int_mode)
16157 || !is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode))
16158 break;
16159 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
16160 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16161 if (op0 == 0)
16162 break;
16163 else if (GET_CODE (rtl) == ZERO_EXTEND
16164 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16165 && GET_MODE_BITSIZE (inner_mode) < HOST_BITS_PER_WIDE_INT
16166 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
16167 to expand zero extend as two shifts instead of
16168 masking. */
16169 && GET_MODE_SIZE (inner_mode) <= 4)
16171 mem_loc_result = op0;
16172 add_loc_descr (&mem_loc_result,
16173 int_loc_descriptor (GET_MODE_MASK (inner_mode)));
16174 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
16176 else if (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
16178 int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (inner_mode);
16179 shift *= BITS_PER_UNIT;
16180 if (GET_CODE (rtl) == SIGN_EXTEND)
16181 op = DW_OP_shra;
16182 else
16183 op = DW_OP_shr;
16184 mem_loc_result = op0;
16185 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
16186 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
16187 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
16188 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16190 else if (!dwarf_strict || dwarf_version >= 5)
16192 dw_die_ref type_die1, type_die2;
16193 dw_loc_descr_ref cvt;
16195 type_die1 = base_type_for_mode (inner_mode,
16196 GET_CODE (rtl) == ZERO_EXTEND);
16197 if (type_die1 == NULL)
16198 break;
16199 type_die2 = base_type_for_mode (int_mode, 1);
16200 if (type_die2 == NULL)
16201 break;
16202 mem_loc_result = op0;
16203 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16204 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16205 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
16206 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16207 add_loc_descr (&mem_loc_result, cvt);
16208 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16209 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16210 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
16211 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16212 add_loc_descr (&mem_loc_result, cvt);
16214 break;
16216 case MEM:
16218 rtx new_rtl = avoid_constant_pool_reference (rtl);
16219 if (new_rtl != rtl)
16221 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
16222 initialized);
16223 if (mem_loc_result != NULL)
16224 return mem_loc_result;
16227 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
16228 get_address_mode (rtl), mode,
16229 VAR_INIT_STATUS_INITIALIZED);
16230 if (mem_loc_result == NULL)
16231 mem_loc_result = tls_mem_loc_descriptor (rtl);
16232 if (mem_loc_result != NULL)
16234 if (!is_a <scalar_int_mode> (mode, &int_mode)
16235 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16237 dw_die_ref type_die;
16238 dw_loc_descr_ref deref;
16239 HOST_WIDE_INT size;
16241 if (dwarf_strict && dwarf_version < 5)
16242 return NULL;
16243 if (!GET_MODE_SIZE (mode).is_constant (&size))
16244 return NULL;
16245 type_die
16246 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16247 if (type_die == NULL)
16248 return NULL;
16249 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type), size, 0);
16250 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
16251 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
16252 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
16253 add_loc_descr (&mem_loc_result, deref);
16255 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
16256 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
16257 else
16258 add_loc_descr (&mem_loc_result,
16259 new_loc_descr (DW_OP_deref_size,
16260 GET_MODE_SIZE (int_mode), 0));
16262 break;
16264 case LO_SUM:
16265 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
16267 case LABEL_REF:
16268 /* Some ports can transform a symbol ref into a label ref, because
16269 the symbol ref is too far away and has to be dumped into a constant
16270 pool. */
16271 case CONST:
16272 case SYMBOL_REF:
16273 case UNSPEC:
16274 if (!is_a <scalar_int_mode> (mode, &int_mode)
16275 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
16276 #ifdef POINTERS_EXTEND_UNSIGNED
16277 && (int_mode != Pmode || mem_mode == VOIDmode)
16278 #endif
16280 break;
16282 if (GET_CODE (rtl) == UNSPEC)
16284 /* If delegitimize_address couldn't do anything with the UNSPEC, we
16285 can't express it in the debug info. This can happen e.g. with some
16286 TLS UNSPECs. Allow UNSPECs formerly from CONST that the backend
16287 approves. */
16288 bool not_ok = false;
16289 subrtx_var_iterator::array_type array;
16290 FOR_EACH_SUBRTX_VAR (iter, array, rtl, ALL)
16291 if (*iter != rtl && !CONSTANT_P (*iter))
16293 not_ok = true;
16294 break;
16297 if (not_ok)
16298 break;
16300 FOR_EACH_SUBRTX_VAR (iter, array, rtl, ALL)
16301 if (!const_ok_for_output_1 (*iter))
16303 not_ok = true;
16304 break;
16307 if (not_ok)
16308 break;
16310 rtl = gen_rtx_CONST (GET_MODE (rtl), rtl);
16311 goto symref;
16314 if (GET_CODE (rtl) == SYMBOL_REF
16315 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
16317 dw_loc_descr_ref temp;
16319 /* If this is not defined, we have no way to emit the data. */
16320 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
16321 break;
16323 temp = new_addr_loc_descr (rtl, dtprel_true);
16325 /* We check for DWARF 5 here because gdb did not implement
16326 DW_OP_form_tls_address until after 7.12. */
16327 mem_loc_result = new_loc_descr ((dwarf_version >= 5
16328 ? DW_OP_form_tls_address
16329 : DW_OP_GNU_push_tls_address),
16330 0, 0);
16331 add_loc_descr (&mem_loc_result, temp);
16333 break;
16336 if (!const_ok_for_output (rtl))
16338 if (GET_CODE (rtl) == CONST)
16339 switch (GET_CODE (XEXP (rtl, 0)))
16341 case NOT:
16342 op = DW_OP_not;
16343 goto try_const_unop;
16344 case NEG:
16345 op = DW_OP_neg;
16346 goto try_const_unop;
16347 try_const_unop:
16348 rtx arg;
16349 arg = XEXP (XEXP (rtl, 0), 0);
16350 if (!CONSTANT_P (arg))
16351 arg = gen_rtx_CONST (int_mode, arg);
16352 op0 = mem_loc_descriptor (arg, int_mode, mem_mode,
16353 initialized);
16354 if (op0)
16356 mem_loc_result = op0;
16357 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16359 break;
16360 default:
16361 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), int_mode,
16362 mem_mode, initialized);
16363 break;
16365 break;
16368 symref:
16369 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
16370 vec_safe_push (used_rtx_array, rtl);
16371 break;
16373 case CONCAT:
16374 case CONCATN:
16375 case VAR_LOCATION:
16376 case DEBUG_IMPLICIT_PTR:
16377 expansion_failed (NULL_TREE, rtl,
16378 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
16379 return 0;
16381 case ENTRY_VALUE:
16382 if (dwarf_strict && dwarf_version < 5)
16383 return NULL;
16384 if (REG_P (ENTRY_VALUE_EXP (rtl)))
16386 if (!is_a <scalar_int_mode> (mode, &int_mode)
16387 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16388 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
16389 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16390 else
16392 unsigned int debugger_regnum = debugger_reg_number (ENTRY_VALUE_EXP (rtl));
16393 if (debugger_regnum == IGNORED_DWARF_REGNUM)
16394 return NULL;
16395 op0 = one_reg_loc_descriptor (debugger_regnum,
16396 VAR_INIT_STATUS_INITIALIZED);
16399 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
16400 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
16402 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
16403 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16404 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
16405 return NULL;
16407 else
16408 gcc_unreachable ();
16409 if (op0 == NULL)
16410 return NULL;
16411 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_entry_value), 0, 0);
16412 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
16413 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
16414 break;
16416 case DEBUG_PARAMETER_REF:
16417 mem_loc_result = parameter_ref_descriptor (rtl);
16418 break;
16420 case PRE_MODIFY:
16421 /* Extract the PLUS expression nested inside and fall into
16422 PLUS code below. */
16423 rtl = XEXP (rtl, 1);
16424 goto plus;
16426 case PRE_INC:
16427 case PRE_DEC:
16428 /* Turn these into a PLUS expression and fall into the PLUS code
16429 below. */
16430 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
16431 gen_int_mode (GET_CODE (rtl) == PRE_INC
16432 ? GET_MODE_UNIT_SIZE (mem_mode)
16433 : -GET_MODE_UNIT_SIZE (mem_mode),
16434 mode));
16436 /* fall through */
16438 case PLUS:
16439 plus:
16440 if (is_based_loc (rtl)
16441 && is_a <scalar_int_mode> (mode, &int_mode)
16442 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16443 || XEXP (rtl, 0) == arg_pointer_rtx
16444 || XEXP (rtl, 0) == frame_pointer_rtx))
16445 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
16446 INTVAL (XEXP (rtl, 1)),
16447 VAR_INIT_STATUS_INITIALIZED);
16448 else
16450 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16451 VAR_INIT_STATUS_INITIALIZED);
16452 if (mem_loc_result == 0)
16453 break;
16455 if (CONST_INT_P (XEXP (rtl, 1))
16456 && (GET_MODE_SIZE (as_a <scalar_int_mode> (mode))
16457 <= DWARF2_ADDR_SIZE))
16458 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
16459 else
16461 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16462 VAR_INIT_STATUS_INITIALIZED);
16463 if (op1 == 0)
16464 return NULL;
16465 add_loc_descr (&mem_loc_result, op1);
16466 add_loc_descr (&mem_loc_result,
16467 new_loc_descr (DW_OP_plus, 0, 0));
16470 break;
16472 /* If a pseudo-reg is optimized away, it is possible for it to
16473 be replaced with a MEM containing a multiply or shift. */
16474 case MINUS:
16475 op = DW_OP_minus;
16476 goto do_binop;
16478 case MULT:
16479 op = DW_OP_mul;
16480 goto do_binop;
16482 case DIV:
16483 if ((!dwarf_strict || dwarf_version >= 5)
16484 && is_a <scalar_int_mode> (mode, &int_mode)
16485 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16487 mem_loc_result = typed_binop (DW_OP_div, rtl,
16488 base_type_for_mode (mode, 0),
16489 int_mode, mem_mode);
16490 break;
16492 op = DW_OP_div;
16493 goto do_binop;
16495 case UMOD:
16496 op = DW_OP_mod;
16497 goto do_binop;
16499 case ASHIFT:
16500 op = DW_OP_shl;
16501 goto do_shift;
16503 case ASHIFTRT:
16504 op = DW_OP_shra;
16505 goto do_shift;
16507 case LSHIFTRT:
16508 op = DW_OP_shr;
16509 goto do_shift;
16511 do_shift:
16512 if (!is_a <scalar_int_mode> (mode, &int_mode))
16513 break;
16514 op0 = mem_loc_descriptor (XEXP (rtl, 0), int_mode, mem_mode,
16515 VAR_INIT_STATUS_INITIALIZED);
16517 rtx rtlop1 = XEXP (rtl, 1);
16518 if (is_a <scalar_int_mode> (GET_MODE (rtlop1), &op1_mode)
16519 && GET_MODE_BITSIZE (op1_mode) < GET_MODE_BITSIZE (int_mode))
16520 rtlop1 = gen_rtx_ZERO_EXTEND (int_mode, rtlop1);
16521 op1 = mem_loc_descriptor (rtlop1, int_mode, mem_mode,
16522 VAR_INIT_STATUS_INITIALIZED);
16525 if (op0 == 0 || op1 == 0)
16526 break;
16528 mem_loc_result = op0;
16529 add_loc_descr (&mem_loc_result, op1);
16530 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16531 break;
16533 case AND:
16534 op = DW_OP_and;
16535 goto do_binop;
16537 case IOR:
16538 op = DW_OP_or;
16539 goto do_binop;
16541 case XOR:
16542 op = DW_OP_xor;
16543 goto do_binop;
16545 do_binop:
16546 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16547 VAR_INIT_STATUS_INITIALIZED);
16548 if (XEXP (rtl, 0) == XEXP (rtl, 1))
16550 if (op0 == 0)
16551 break;
16552 mem_loc_result = op0;
16553 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_dup, 0, 0));
16554 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16555 break;
16557 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16558 VAR_INIT_STATUS_INITIALIZED);
16560 if (op0 == 0 || op1 == 0)
16561 break;
16563 mem_loc_result = op0;
16564 add_loc_descr (&mem_loc_result, op1);
16565 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16566 break;
16568 case MOD:
16569 if ((!dwarf_strict || dwarf_version >= 5)
16570 && is_a <scalar_int_mode> (mode, &int_mode)
16571 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16573 mem_loc_result = typed_binop (DW_OP_mod, rtl,
16574 base_type_for_mode (mode, 0),
16575 int_mode, mem_mode);
16576 break;
16579 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16580 VAR_INIT_STATUS_INITIALIZED);
16581 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16582 VAR_INIT_STATUS_INITIALIZED);
16584 if (op0 == 0 || op1 == 0)
16585 break;
16587 mem_loc_result = op0;
16588 add_loc_descr (&mem_loc_result, op1);
16589 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
16590 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
16591 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
16592 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
16593 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
16594 break;
16596 case UDIV:
16597 if ((!dwarf_strict || dwarf_version >= 5)
16598 && is_a <scalar_int_mode> (mode, &int_mode))
16600 /* We can use a signed divide if the sign bit is not set. */
16601 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
16603 op = DW_OP_div;
16604 goto do_binop;
16607 mem_loc_result = typed_binop (DW_OP_div, rtl,
16608 base_type_for_mode (int_mode, 1),
16609 int_mode, mem_mode);
16611 break;
16613 case NOT:
16614 op = DW_OP_not;
16615 goto do_unop;
16617 case ABS:
16618 op = DW_OP_abs;
16619 goto do_unop;
16621 case NEG:
16622 op = DW_OP_neg;
16623 goto do_unop;
16625 do_unop:
16626 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16627 VAR_INIT_STATUS_INITIALIZED);
16629 if (op0 == 0)
16630 break;
16632 mem_loc_result = op0;
16633 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16634 break;
16636 case CONST_INT:
16637 if (!is_a <scalar_int_mode> (mode, &int_mode)
16638 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16639 #ifdef POINTERS_EXTEND_UNSIGNED
16640 || (int_mode == Pmode
16641 && mem_mode != VOIDmode
16642 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
16643 #endif
16646 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
16647 break;
16649 if ((!dwarf_strict || dwarf_version >= 5)
16650 && (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT
16651 || GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_DOUBLE_INT))
16653 dw_die_ref type_die = base_type_for_mode (int_mode, 1);
16654 scalar_int_mode amode;
16655 if (type_die == NULL)
16656 return NULL;
16657 if (INTVAL (rtl) >= 0
16658 && (int_mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT, 0)
16659 .exists (&amode))
16660 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
16661 /* const DW_OP_convert <XXX> vs.
16662 DW_OP_const_type <XXX, 1, const>. */
16663 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
16664 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (int_mode))
16666 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
16667 op0 = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16668 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16669 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16670 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
16671 add_loc_descr (&mem_loc_result, op0);
16672 return mem_loc_result;
16674 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0,
16675 INTVAL (rtl));
16676 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16677 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16678 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16679 if (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT)
16680 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
16681 else
16683 mem_loc_result->dw_loc_oprnd2.val_class
16684 = dw_val_class_const_double;
16685 mem_loc_result->dw_loc_oprnd2.v.val_double
16686 = double_int::from_shwi (INTVAL (rtl));
16689 break;
16691 case CONST_DOUBLE:
16692 if (!dwarf_strict || dwarf_version >= 5)
16694 dw_die_ref type_die;
16696 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
16697 CONST_DOUBLE rtx could represent either a large integer
16698 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
16699 the value is always a floating point constant.
16701 When it is an integer, a CONST_DOUBLE is used whenever
16702 the constant requires 2 HWIs to be adequately represented.
16703 We output CONST_DOUBLEs as blocks. */
16704 if (mode == VOIDmode
16705 || (GET_MODE (rtl) == VOIDmode
16706 && maybe_ne (GET_MODE_BITSIZE (mode),
16707 HOST_BITS_PER_DOUBLE_INT)))
16708 break;
16709 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16710 if (type_die == NULL)
16711 return NULL;
16712 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
16713 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16714 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16715 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16716 #if TARGET_SUPPORTS_WIDE_INT == 0
16717 if (!SCALAR_FLOAT_MODE_P (mode))
16719 mem_loc_result->dw_loc_oprnd2.val_class
16720 = dw_val_class_const_double;
16721 mem_loc_result->dw_loc_oprnd2.v.val_double
16722 = rtx_to_double_int (rtl);
16724 else
16725 #endif
16727 scalar_float_mode float_mode = as_a <scalar_float_mode> (mode);
16728 unsigned int length = GET_MODE_SIZE (float_mode);
16729 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
16730 unsigned int elt_size = insert_float (rtl, array);
16732 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
16733 mem_loc_result->dw_loc_oprnd2.v.val_vec.length
16734 = length / elt_size;
16735 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
16736 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
16739 break;
16741 case CONST_WIDE_INT:
16742 if (!dwarf_strict || dwarf_version >= 5)
16744 dw_die_ref type_die;
16746 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16747 if (type_die == NULL)
16748 return NULL;
16749 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
16750 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16751 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16752 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16753 mem_loc_result->dw_loc_oprnd2.val_class
16754 = dw_val_class_wide_int;
16755 mem_loc_result->dw_loc_oprnd2.v.val_wide
16756 = alloc_dw_wide_int (rtx_mode_t (rtl, mode));
16758 break;
16760 case CONST_POLY_INT:
16761 mem_loc_result = int_loc_descriptor (rtx_to_poly_int64 (rtl));
16762 break;
16764 case EQ:
16765 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
16766 break;
16768 case GE:
16769 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
16770 break;
16772 case GT:
16773 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
16774 break;
16776 case LE:
16777 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
16778 break;
16780 case LT:
16781 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
16782 break;
16784 case NE:
16785 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
16786 break;
16788 case GEU:
16789 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
16790 break;
16792 case GTU:
16793 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
16794 break;
16796 case LEU:
16797 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
16798 break;
16800 case LTU:
16801 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
16802 break;
16804 case UMIN:
16805 case UMAX:
16806 if (!SCALAR_INT_MODE_P (mode))
16807 break;
16808 /* FALLTHRU */
16809 case SMIN:
16810 case SMAX:
16811 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
16812 break;
16814 case ZERO_EXTRACT:
16815 case SIGN_EXTRACT:
16816 if (CONST_INT_P (XEXP (rtl, 1))
16817 && CONST_INT_P (XEXP (rtl, 2))
16818 && is_a <scalar_int_mode> (mode, &int_mode)
16819 && is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode)
16820 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16821 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE
16822 && ((unsigned) INTVAL (XEXP (rtl, 1))
16823 + (unsigned) INTVAL (XEXP (rtl, 2))
16824 <= GET_MODE_BITSIZE (int_mode)))
16826 int shift, size;
16827 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
16828 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16829 if (op0 == 0)
16830 break;
16831 if (GET_CODE (rtl) == SIGN_EXTRACT)
16832 op = DW_OP_shra;
16833 else
16834 op = DW_OP_shr;
16835 mem_loc_result = op0;
16836 size = INTVAL (XEXP (rtl, 1));
16837 shift = INTVAL (XEXP (rtl, 2));
16838 if (BITS_BIG_ENDIAN)
16839 shift = GET_MODE_BITSIZE (inner_mode) - shift - size;
16840 if (shift + size != (int) DWARF2_ADDR_SIZE)
16842 add_loc_descr (&mem_loc_result,
16843 int_loc_descriptor (DWARF2_ADDR_SIZE
16844 - shift - size));
16845 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
16847 if (size != (int) DWARF2_ADDR_SIZE)
16849 add_loc_descr (&mem_loc_result,
16850 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
16851 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16854 break;
16856 case IF_THEN_ELSE:
16858 dw_loc_descr_ref op2, bra_node, drop_node;
16859 op0 = mem_loc_descriptor (XEXP (rtl, 0),
16860 GET_MODE (XEXP (rtl, 0)) == VOIDmode
16861 ? word_mode : GET_MODE (XEXP (rtl, 0)),
16862 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16863 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16864 VAR_INIT_STATUS_INITIALIZED);
16865 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
16866 VAR_INIT_STATUS_INITIALIZED);
16867 if (op0 == NULL || op1 == NULL || op2 == NULL)
16868 break;
16870 mem_loc_result = op1;
16871 add_loc_descr (&mem_loc_result, op2);
16872 add_loc_descr (&mem_loc_result, op0);
16873 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
16874 add_loc_descr (&mem_loc_result, bra_node);
16875 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
16876 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
16877 add_loc_descr (&mem_loc_result, drop_node);
16878 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16879 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
16881 break;
16883 case FLOAT_EXTEND:
16884 case FLOAT_TRUNCATE:
16885 case FLOAT:
16886 case UNSIGNED_FLOAT:
16887 case FIX:
16888 case UNSIGNED_FIX:
16889 if (!dwarf_strict || dwarf_version >= 5)
16891 dw_die_ref type_die;
16892 dw_loc_descr_ref cvt;
16894 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
16895 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16896 if (op0 == NULL)
16897 break;
16898 if (is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &int_mode)
16899 && (GET_CODE (rtl) == FLOAT
16900 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE))
16902 type_die = base_type_for_mode (int_mode,
16903 GET_CODE (rtl) == UNSIGNED_FLOAT);
16904 if (type_die == NULL)
16905 break;
16906 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16907 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16908 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16909 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16910 add_loc_descr (&op0, cvt);
16912 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
16913 if (type_die == NULL)
16914 break;
16915 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16916 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16917 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16918 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16919 add_loc_descr (&op0, cvt);
16920 if (is_a <scalar_int_mode> (mode, &int_mode)
16921 && (GET_CODE (rtl) == FIX
16922 || GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE))
16924 op0 = convert_descriptor_to_mode (int_mode, op0);
16925 if (op0 == NULL)
16926 break;
16928 mem_loc_result = op0;
16930 break;
16932 case CLZ:
16933 case CTZ:
16934 case FFS:
16935 if (is_a <scalar_int_mode> (mode, &int_mode))
16936 mem_loc_result = clz_loc_descriptor (rtl, int_mode, mem_mode);
16937 break;
16939 case POPCOUNT:
16940 case PARITY:
16941 if (is_a <scalar_int_mode> (mode, &int_mode))
16942 mem_loc_result = popcount_loc_descriptor (rtl, int_mode, mem_mode);
16943 break;
16945 case BSWAP:
16946 if (is_a <scalar_int_mode> (mode, &int_mode))
16947 mem_loc_result = bswap_loc_descriptor (rtl, int_mode, mem_mode);
16948 break;
16950 case ROTATE:
16951 case ROTATERT:
16952 if (is_a <scalar_int_mode> (mode, &int_mode))
16953 mem_loc_result = rotate_loc_descriptor (rtl, int_mode, mem_mode);
16954 break;
16956 case COMPARE:
16957 /* In theory, we could implement the above. */
16958 /* DWARF cannot represent the unsigned compare operations
16959 natively. */
16960 case SS_MULT:
16961 case US_MULT:
16962 case SS_DIV:
16963 case US_DIV:
16964 case SS_PLUS:
16965 case US_PLUS:
16966 case SS_MINUS:
16967 case US_MINUS:
16968 case SS_NEG:
16969 case US_NEG:
16970 case SS_ABS:
16971 case SS_ASHIFT:
16972 case US_ASHIFT:
16973 case SS_TRUNCATE:
16974 case US_TRUNCATE:
16975 case UNORDERED:
16976 case ORDERED:
16977 case UNEQ:
16978 case UNGE:
16979 case UNGT:
16980 case UNLE:
16981 case UNLT:
16982 case LTGT:
16983 case FRACT_CONVERT:
16984 case UNSIGNED_FRACT_CONVERT:
16985 case SAT_FRACT:
16986 case UNSIGNED_SAT_FRACT:
16987 case SQRT:
16988 case ASM_OPERANDS:
16989 case VEC_MERGE:
16990 case VEC_SELECT:
16991 case VEC_CONCAT:
16992 case VEC_DUPLICATE:
16993 case VEC_SERIES:
16994 case HIGH:
16995 case FMA:
16996 case STRICT_LOW_PART:
16997 case CONST_VECTOR:
16998 case CONST_FIXED:
16999 case CLRSB:
17000 case CLOBBER:
17001 case SMUL_HIGHPART:
17002 case UMUL_HIGHPART:
17003 case BITREVERSE:
17004 case COPYSIGN:
17005 break;
17007 case CONST_STRING:
17008 resolve_one_addr (&rtl);
17009 goto symref;
17011 /* RTL sequences inside PARALLEL record a series of DWARF operations for
17012 the expression. An UNSPEC rtx represents a raw DWARF operation,
17013 new_loc_descr is called for it to build the operation directly.
17014 Otherwise mem_loc_descriptor is called recursively. */
17015 case PARALLEL:
17017 int index = 0;
17018 dw_loc_descr_ref exp_result = NULL;
17020 for (; index < XVECLEN (rtl, 0); index++)
17022 rtx elem = XVECEXP (rtl, 0, index);
17023 if (GET_CODE (elem) == UNSPEC)
17025 /* Each DWARF operation UNSPEC contain two operands, if
17026 one operand is not used for the operation, const0_rtx is
17027 passed. */
17028 gcc_assert (XVECLEN (elem, 0) == 2);
17030 HOST_WIDE_INT dw_op = XINT (elem, 1);
17031 HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
17032 HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
17033 exp_result
17034 = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
17035 oprnd2);
17037 else
17038 exp_result
17039 = mem_loc_descriptor (elem, mode, mem_mode,
17040 VAR_INIT_STATUS_INITIALIZED);
17042 if (!mem_loc_result)
17043 mem_loc_result = exp_result;
17044 else
17045 add_loc_descr (&mem_loc_result, exp_result);
17048 break;
17051 default:
17052 if (flag_checking)
17054 print_rtl (stderr, rtl);
17055 gcc_unreachable ();
17057 break;
17060 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
17061 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
17063 return mem_loc_result;
17066 /* Return a descriptor that describes the concatenation of two locations.
17067 This is typically a complex variable. */
17069 static dw_loc_descr_ref
17070 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
17072 /* At present we only track constant-sized pieces. */
17073 unsigned int size0, size1;
17074 if (!GET_MODE_SIZE (GET_MODE (x0)).is_constant (&size0)
17075 || !GET_MODE_SIZE (GET_MODE (x1)).is_constant (&size1))
17076 return 0;
17078 dw_loc_descr_ref cc_loc_result = NULL;
17079 dw_loc_descr_ref x0_ref
17080 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
17081 dw_loc_descr_ref x1_ref
17082 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
17084 if (x0_ref == 0 || x1_ref == 0)
17085 return 0;
17087 cc_loc_result = x0_ref;
17088 add_loc_descr_op_piece (&cc_loc_result, size0);
17090 add_loc_descr (&cc_loc_result, x1_ref);
17091 add_loc_descr_op_piece (&cc_loc_result, size1);
17093 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
17094 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
17096 return cc_loc_result;
17099 /* Return a descriptor that describes the concatenation of N
17100 locations. */
17102 static dw_loc_descr_ref
17103 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
17105 unsigned int i;
17106 dw_loc_descr_ref cc_loc_result = NULL;
17107 unsigned int n = XVECLEN (concatn, 0);
17108 unsigned int size;
17110 for (i = 0; i < n; ++i)
17112 dw_loc_descr_ref ref;
17113 rtx x = XVECEXP (concatn, 0, i);
17115 /* At present we only track constant-sized pieces. */
17116 if (!GET_MODE_SIZE (GET_MODE (x)).is_constant (&size))
17117 return NULL;
17119 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
17120 if (ref == NULL)
17121 return NULL;
17123 add_loc_descr (&cc_loc_result, ref);
17124 add_loc_descr_op_piece (&cc_loc_result, size);
17127 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
17128 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
17130 return cc_loc_result;
17133 /* Helper function for loc_descriptor. Return DW_OP_implicit_pointer
17134 for DEBUG_IMPLICIT_PTR RTL. */
17136 static dw_loc_descr_ref
17137 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
17139 dw_loc_descr_ref ret;
17140 dw_die_ref ref;
17142 if (dwarf_strict && dwarf_version < 5)
17143 return NULL;
17144 gcc_assert (VAR_P (DEBUG_IMPLICIT_PTR_DECL (rtl))
17145 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
17146 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
17147 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
17148 ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
17149 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
17150 if (ref)
17152 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17153 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
17154 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
17156 else
17158 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
17159 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
17161 return ret;
17164 /* Output a proper Dwarf location descriptor for a variable or parameter
17165 which is either allocated in a register or in a memory location. For a
17166 register, we just generate an OP_REG and the register number. For a
17167 memory location we provide a Dwarf postfix expression describing how to
17168 generate the (dynamic) address of the object onto the address stack.
17170 MODE is mode of the decl if this loc_descriptor is going to be used in
17171 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
17172 allowed, VOIDmode otherwise.
17174 If we don't know how to describe it, return 0. */
17176 static dw_loc_descr_ref
17177 loc_descriptor (rtx rtl, machine_mode mode,
17178 enum var_init_status initialized)
17180 dw_loc_descr_ref loc_result = NULL;
17181 scalar_int_mode int_mode;
17183 switch (GET_CODE (rtl))
17185 case SUBREG:
17186 /* The case of a subreg may arise when we have a local (register)
17187 variable or a formal (register) parameter which doesn't quite fill
17188 up an entire register. For now, just assume that it is
17189 legitimate to make the Dwarf info refer to the whole register which
17190 contains the given subreg. */
17191 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
17192 loc_result = loc_descriptor (SUBREG_REG (rtl),
17193 GET_MODE (SUBREG_REG (rtl)), initialized);
17194 else
17195 goto do_default;
17196 break;
17198 case REG:
17199 loc_result = reg_loc_descriptor (rtl, initialized);
17200 break;
17202 case MEM:
17203 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
17204 GET_MODE (rtl), initialized);
17205 if (loc_result == NULL)
17206 loc_result = tls_mem_loc_descriptor (rtl);
17207 if (loc_result == NULL)
17209 rtx new_rtl = avoid_constant_pool_reference (rtl);
17210 if (new_rtl != rtl)
17211 loc_result = loc_descriptor (new_rtl, mode, initialized);
17213 break;
17215 case CONCAT:
17216 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
17217 initialized);
17218 break;
17220 case CONCATN:
17221 loc_result = concatn_loc_descriptor (rtl, initialized);
17222 break;
17224 case VAR_LOCATION:
17225 /* Single part. */
17226 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
17228 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
17229 if (GET_CODE (loc) == EXPR_LIST)
17230 loc = XEXP (loc, 0);
17231 loc_result = loc_descriptor (loc, mode, initialized);
17232 break;
17235 rtl = XEXP (rtl, 1);
17236 /* FALLTHRU */
17238 case PARALLEL:
17240 rtvec par_elems = XVEC (rtl, 0);
17241 int num_elem = GET_NUM_ELEM (par_elems);
17242 machine_mode mode;
17243 int i, size;
17245 /* Create the first one, so we have something to add to. */
17246 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
17247 VOIDmode, initialized);
17248 if (loc_result == NULL)
17249 return NULL;
17250 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
17251 /* At present we only track constant-sized pieces. */
17252 if (!GET_MODE_SIZE (mode).is_constant (&size))
17253 return NULL;
17254 add_loc_descr_op_piece (&loc_result, size);
17255 for (i = 1; i < num_elem; i++)
17257 dw_loc_descr_ref temp;
17259 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
17260 VOIDmode, initialized);
17261 if (temp == NULL)
17262 return NULL;
17263 add_loc_descr (&loc_result, temp);
17264 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
17265 /* At present we only track constant-sized pieces. */
17266 if (!GET_MODE_SIZE (mode).is_constant (&size))
17267 return NULL;
17268 add_loc_descr_op_piece (&loc_result, size);
17271 break;
17273 case CONST_INT:
17274 if (mode != VOIDmode && mode != BLKmode)
17276 int_mode = as_a <scalar_int_mode> (mode);
17277 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (int_mode),
17278 INTVAL (rtl));
17280 break;
17282 case CONST_DOUBLE:
17283 if (mode == VOIDmode)
17284 mode = GET_MODE (rtl);
17286 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
17288 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
17290 /* Note that a CONST_DOUBLE rtx could represent either an integer
17291 or a floating-point constant. A CONST_DOUBLE is used whenever
17292 the constant requires more than one word in order to be
17293 adequately represented. We output CONST_DOUBLEs as blocks. */
17294 scalar_mode smode = as_a <scalar_mode> (mode);
17295 loc_result = new_loc_descr (DW_OP_implicit_value,
17296 GET_MODE_SIZE (smode), 0);
17297 #if TARGET_SUPPORTS_WIDE_INT == 0
17298 if (!SCALAR_FLOAT_MODE_P (smode))
17300 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
17301 loc_result->dw_loc_oprnd2.v.val_double
17302 = rtx_to_double_int (rtl);
17304 else
17305 #endif
17307 unsigned int length = GET_MODE_SIZE (smode);
17308 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
17309 unsigned int elt_size = insert_float (rtl, array);
17311 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
17312 loc_result->dw_loc_oprnd2.v.val_vec.length = length / elt_size;
17313 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
17314 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
17317 break;
17319 case CONST_WIDE_INT:
17320 if (mode == VOIDmode)
17321 mode = GET_MODE (rtl);
17323 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
17325 int_mode = as_a <scalar_int_mode> (mode);
17326 loc_result = new_loc_descr (DW_OP_implicit_value,
17327 GET_MODE_SIZE (int_mode), 0);
17328 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
17329 loc_result->dw_loc_oprnd2.v.val_wide
17330 = alloc_dw_wide_int (rtx_mode_t (rtl, int_mode));
17332 break;
17334 case CONST_VECTOR:
17335 if (mode == VOIDmode)
17336 mode = GET_MODE (rtl);
17338 if (mode != VOIDmode
17339 /* The combination of a length and byte elt_size doesn't extend
17340 naturally to boolean vectors, where several elements are packed
17341 into the same byte. */
17342 && GET_MODE_CLASS (mode) != MODE_VECTOR_BOOL
17343 && (dwarf_version >= 4 || !dwarf_strict))
17345 unsigned int length;
17346 if (!CONST_VECTOR_NUNITS (rtl).is_constant (&length))
17347 return NULL;
17349 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
17350 unsigned char *array
17351 = ggc_vec_alloc<unsigned char> (length * elt_size);
17352 unsigned int i;
17353 unsigned char *p;
17354 machine_mode imode = GET_MODE_INNER (mode);
17356 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
17357 switch (GET_MODE_CLASS (mode))
17359 case MODE_VECTOR_INT:
17360 for (i = 0, p = array; i < length; i++, p += elt_size)
17362 rtx elt = CONST_VECTOR_ELT (rtl, i);
17363 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
17365 break;
17367 case MODE_VECTOR_FLOAT:
17368 for (i = 0, p = array; i < length; i++, p += elt_size)
17370 rtx elt = CONST_VECTOR_ELT (rtl, i);
17371 insert_float (elt, p);
17373 break;
17375 default:
17376 gcc_unreachable ();
17379 loc_result = new_loc_descr (DW_OP_implicit_value,
17380 length * elt_size, 0);
17381 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
17382 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
17383 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
17384 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
17386 break;
17388 case CONST:
17389 if (mode == VOIDmode
17390 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
17391 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
17392 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
17394 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
17395 break;
17397 /* FALLTHROUGH */
17398 case SYMBOL_REF:
17399 if (!const_ok_for_output (rtl))
17400 break;
17401 /* FALLTHROUGH */
17402 case LABEL_REF:
17403 if (is_a <scalar_int_mode> (mode, &int_mode)
17404 && GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE
17405 && (dwarf_version >= 4 || !dwarf_strict))
17407 loc_result = new_addr_loc_descr (rtl, dtprel_false);
17408 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
17409 vec_safe_push (used_rtx_array, rtl);
17411 break;
17413 case DEBUG_IMPLICIT_PTR:
17414 loc_result = implicit_ptr_descriptor (rtl, 0);
17415 break;
17417 case PLUS:
17418 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
17419 && CONST_INT_P (XEXP (rtl, 1)))
17421 loc_result
17422 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
17423 break;
17425 /* FALLTHRU */
17426 do_default:
17427 default:
17428 if ((is_a <scalar_int_mode> (mode, &int_mode)
17429 && GET_MODE (rtl) == int_mode
17430 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
17431 && dwarf_version >= 4)
17432 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
17434 /* Value expression. */
17435 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
17436 if (loc_result)
17437 add_loc_descr (&loc_result,
17438 new_loc_descr (DW_OP_stack_value, 0, 0));
17440 break;
17443 return loc_result;
17446 /* We need to figure out what section we should use as the base for the
17447 address ranges where a given location is valid.
17448 1. If this particular DECL has a section associated with it, use that.
17449 2. If this function has a section associated with it, use that.
17450 3. Otherwise, use the text section.
17451 XXX: If you split a variable across multiple sections, we won't notice. */
17453 static const char *
17454 secname_for_decl (const_tree decl)
17456 const char *secname;
17458 if (VAR_OR_FUNCTION_DECL_P (decl)
17459 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
17460 && DECL_SECTION_NAME (decl))
17461 secname = DECL_SECTION_NAME (decl);
17462 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
17464 if (in_cold_section_p)
17466 section *sec = current_function_section ();
17467 if (sec->common.flags & SECTION_NAMED)
17468 return sec->named.name;
17470 secname = DECL_SECTION_NAME (current_function_decl);
17472 else if (cfun && in_cold_section_p)
17473 secname = crtl->subsections.cold_section_label;
17474 else
17475 secname = text_section_label;
17477 return secname;
17480 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
17482 static bool
17483 decl_by_reference_p (tree decl)
17485 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
17486 || VAR_P (decl))
17487 && DECL_BY_REFERENCE (decl));
17490 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
17491 for VARLOC. */
17493 static dw_loc_descr_ref
17494 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
17495 enum var_init_status initialized)
17497 int have_address = 0;
17498 dw_loc_descr_ref descr;
17499 machine_mode mode;
17501 if (want_address != 2)
17503 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
17504 /* Single part. */
17505 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
17507 varloc = PAT_VAR_LOCATION_LOC (varloc);
17508 if (GET_CODE (varloc) == EXPR_LIST)
17509 varloc = XEXP (varloc, 0);
17510 mode = GET_MODE (varloc);
17511 if (MEM_P (varloc))
17513 rtx addr = XEXP (varloc, 0);
17514 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
17515 mode, initialized);
17516 if (descr)
17517 have_address = 1;
17518 else
17520 rtx x = avoid_constant_pool_reference (varloc);
17521 if (x != varloc)
17522 descr = mem_loc_descriptor (x, mode, VOIDmode,
17523 initialized);
17526 else
17527 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
17529 else
17530 return 0;
17532 else
17534 if (GET_CODE (varloc) == VAR_LOCATION)
17535 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
17536 else
17537 mode = DECL_MODE (loc);
17538 descr = loc_descriptor (varloc, mode, initialized);
17539 have_address = 1;
17542 if (!descr)
17543 return 0;
17545 if (want_address == 2 && !have_address
17546 && (dwarf_version >= 4 || !dwarf_strict))
17548 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
17550 expansion_failed (loc, NULL_RTX,
17551 "DWARF address size mismatch");
17552 return 0;
17554 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
17555 have_address = 1;
17557 /* Show if we can't fill the request for an address. */
17558 if (want_address && !have_address)
17560 expansion_failed (loc, NULL_RTX,
17561 "Want address and only have value");
17562 return 0;
17565 /* If we've got an address and don't want one, dereference. */
17566 if (!want_address && have_address)
17568 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
17569 enum dwarf_location_atom op;
17571 if (size > DWARF2_ADDR_SIZE || size == -1)
17573 expansion_failed (loc, NULL_RTX,
17574 "DWARF address size mismatch");
17575 return 0;
17577 else if (size == DWARF2_ADDR_SIZE)
17578 op = DW_OP_deref;
17579 else
17580 op = DW_OP_deref_size;
17582 add_loc_descr (&descr, new_loc_descr (op, size, 0));
17585 return descr;
17588 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
17589 if it is not possible. */
17591 static dw_loc_descr_ref
17592 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
17594 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
17595 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
17596 else if (dwarf_version >= 3 || !dwarf_strict)
17597 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
17598 else
17599 return NULL;
17602 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
17603 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
17605 static dw_loc_descr_ref
17606 dw_sra_loc_expr (tree decl, rtx loc)
17608 rtx p;
17609 unsigned HOST_WIDE_INT padsize = 0;
17610 dw_loc_descr_ref descr, *descr_tail;
17611 unsigned HOST_WIDE_INT decl_size;
17612 rtx varloc;
17613 enum var_init_status initialized;
17615 if (DECL_SIZE (decl) == NULL
17616 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
17617 return NULL;
17619 decl_size = tree_to_uhwi (DECL_SIZE (decl));
17620 descr = NULL;
17621 descr_tail = &descr;
17623 for (p = loc; p; p = XEXP (p, 1))
17625 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
17626 rtx loc_note = *decl_piece_varloc_ptr (p);
17627 dw_loc_descr_ref cur_descr;
17628 dw_loc_descr_ref *tail, last = NULL;
17629 unsigned HOST_WIDE_INT opsize = 0;
17631 if (loc_note == NULL_RTX
17632 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
17634 padsize += bitsize;
17635 continue;
17637 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
17638 varloc = NOTE_VAR_LOCATION (loc_note);
17639 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
17640 if (cur_descr == NULL)
17642 padsize += bitsize;
17643 continue;
17646 /* Check that cur_descr either doesn't use
17647 DW_OP_*piece operations, or their sum is equal
17648 to bitsize. Otherwise we can't embed it. */
17649 for (tail = &cur_descr; *tail != NULL;
17650 tail = &(*tail)->dw_loc_next)
17651 if ((*tail)->dw_loc_opc == DW_OP_piece)
17653 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
17654 * BITS_PER_UNIT;
17655 last = *tail;
17657 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
17659 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
17660 last = *tail;
17663 if (last != NULL && opsize != bitsize)
17665 padsize += bitsize;
17666 /* Discard the current piece of the descriptor and release any
17667 addr_table entries it uses. */
17668 remove_loc_list_addr_table_entries (cur_descr);
17669 continue;
17672 /* If there is a hole, add DW_OP_*piece after empty DWARF
17673 expression, which means that those bits are optimized out. */
17674 if (padsize)
17676 if (padsize > decl_size)
17678 remove_loc_list_addr_table_entries (cur_descr);
17679 goto discard_descr;
17681 decl_size -= padsize;
17682 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
17683 if (*descr_tail == NULL)
17685 remove_loc_list_addr_table_entries (cur_descr);
17686 goto discard_descr;
17688 descr_tail = &(*descr_tail)->dw_loc_next;
17689 padsize = 0;
17691 *descr_tail = cur_descr;
17692 descr_tail = tail;
17693 if (bitsize > decl_size)
17694 goto discard_descr;
17695 decl_size -= bitsize;
17696 if (last == NULL)
17698 HOST_WIDE_INT offset = 0;
17699 if (GET_CODE (varloc) == VAR_LOCATION
17700 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
17702 varloc = PAT_VAR_LOCATION_LOC (varloc);
17703 if (GET_CODE (varloc) == EXPR_LIST)
17704 varloc = XEXP (varloc, 0);
17708 if (GET_CODE (varloc) == CONST
17709 || GET_CODE (varloc) == SIGN_EXTEND
17710 || GET_CODE (varloc) == ZERO_EXTEND)
17711 varloc = XEXP (varloc, 0);
17712 else if (GET_CODE (varloc) == SUBREG)
17713 varloc = SUBREG_REG (varloc);
17714 else
17715 break;
17717 while (1);
17718 /* DW_OP_bit_size offset should be zero for register
17719 or implicit location descriptions and empty location
17720 descriptions, but for memory addresses needs big endian
17721 adjustment. */
17722 if (MEM_P (varloc))
17724 unsigned HOST_WIDE_INT memsize;
17725 if (!poly_uint64 (MEM_SIZE (varloc)).is_constant (&memsize))
17726 goto discard_descr;
17727 memsize *= BITS_PER_UNIT;
17728 if (memsize != bitsize)
17730 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
17731 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
17732 goto discard_descr;
17733 if (memsize < bitsize)
17734 goto discard_descr;
17735 if (BITS_BIG_ENDIAN)
17736 offset = memsize - bitsize;
17740 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
17741 if (*descr_tail == NULL)
17742 goto discard_descr;
17743 descr_tail = &(*descr_tail)->dw_loc_next;
17747 /* If there were any non-empty expressions, add padding till the end of
17748 the decl. */
17749 if (descr != NULL && decl_size != 0)
17751 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
17752 if (*descr_tail == NULL)
17753 goto discard_descr;
17755 return descr;
17757 discard_descr:
17758 /* Discard the descriptor and release any addr_table entries it uses. */
17759 remove_loc_list_addr_table_entries (descr);
17760 return NULL;
17763 /* Return the dwarf representation of the location list LOC_LIST of
17764 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
17765 function. */
17767 static dw_loc_list_ref
17768 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
17770 const char *endname, *secname;
17771 var_loc_view endview;
17772 rtx varloc;
17773 enum var_init_status initialized;
17774 struct var_loc_node *node;
17775 dw_loc_descr_ref descr;
17776 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
17777 dw_loc_list_ref list = NULL;
17778 dw_loc_list_ref *listp = &list;
17780 /* Now that we know what section we are using for a base,
17781 actually construct the list of locations.
17782 The first location information is what is passed to the
17783 function that creates the location list, and the remaining
17784 locations just get added on to that list.
17785 Note that we only know the start address for a location
17786 (IE location changes), so to build the range, we use
17787 the range [current location start, next location start].
17788 This means we have to special case the last node, and generate
17789 a range of [last location start, end of function label]. */
17791 if (cfun && crtl->has_bb_partition)
17793 bool save_in_cold_section_p = in_cold_section_p;
17794 in_cold_section_p = first_function_block_is_cold;
17795 if (loc_list->last_before_switch == NULL)
17796 in_cold_section_p = !in_cold_section_p;
17797 secname = secname_for_decl (decl);
17798 in_cold_section_p = save_in_cold_section_p;
17800 else
17801 secname = secname_for_decl (decl);
17803 for (node = loc_list->first; node; node = node->next)
17805 bool range_across_switch = false;
17806 if (GET_CODE (node->loc) == EXPR_LIST
17807 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
17809 if (GET_CODE (node->loc) == EXPR_LIST)
17811 descr = NULL;
17812 /* This requires DW_OP_{,bit_}piece, which is not usable
17813 inside DWARF expressions. */
17814 if (want_address == 2)
17815 descr = dw_sra_loc_expr (decl, node->loc);
17817 else
17819 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
17820 varloc = NOTE_VAR_LOCATION (node->loc);
17821 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
17823 if (descr)
17825 /* If section switch happens in between node->label
17826 and node->next->label (or end of function) and
17827 we can't emit it as a single entry list,
17828 emit two ranges, first one ending at the end
17829 of first partition and second one starting at the
17830 beginning of second partition. */
17831 if (node == loc_list->last_before_switch
17832 && (node != loc_list->first || loc_list->first->next
17833 /* If we are to emit a view number, we will emit
17834 a loclist rather than a single location
17835 expression for the entire function (see
17836 loc_list_has_views), so we have to split the
17837 range that straddles across partitions. */
17838 || !ZERO_VIEW_P (node->view))
17839 && current_function_decl)
17841 endname = cfun->fde->dw_fde_end;
17842 endview = 0;
17843 range_across_switch = true;
17845 /* The variable has a location between NODE->LABEL and
17846 NODE->NEXT->LABEL. */
17847 else if (node->next)
17848 endname = node->next->label, endview = node->next->view;
17849 /* If the variable has a location at the last label
17850 it keeps its location until the end of function. */
17851 else if (!current_function_decl)
17852 endname = text_end_label, endview = 0;
17853 else
17855 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
17856 current_function_funcdef_no);
17857 endname = ggc_strdup (label_id);
17858 endview = 0;
17861 *listp = new_loc_list (descr, node->label, node->view,
17862 endname, endview, secname);
17863 if (TREE_CODE (decl) == PARM_DECL
17864 && node == loc_list->first
17865 && NOTE_P (node->loc)
17866 && strcmp (node->label, endname) == 0)
17867 (*listp)->force = true;
17868 listp = &(*listp)->dw_loc_next;
17872 if (cfun
17873 && crtl->has_bb_partition
17874 && node == loc_list->last_before_switch)
17876 bool save_in_cold_section_p = in_cold_section_p;
17877 in_cold_section_p = !first_function_block_is_cold;
17878 secname = secname_for_decl (decl);
17879 in_cold_section_p = save_in_cold_section_p;
17882 if (range_across_switch)
17884 if (GET_CODE (node->loc) == EXPR_LIST)
17885 descr = dw_sra_loc_expr (decl, node->loc);
17886 else
17888 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
17889 varloc = NOTE_VAR_LOCATION (node->loc);
17890 descr = dw_loc_list_1 (decl, varloc, want_address,
17891 initialized);
17893 gcc_assert (descr);
17894 /* The variable has a location between NODE->LABEL and
17895 NODE->NEXT->LABEL. */
17896 if (node->next)
17897 endname = node->next->label, endview = node->next->view;
17898 else
17899 endname = cfun->fde->dw_fde_second_end, endview = 0;
17900 *listp = new_loc_list (descr, cfun->fde->dw_fde_second_begin, 0,
17901 endname, endview, secname);
17902 listp = &(*listp)->dw_loc_next;
17906 /* Try to avoid the overhead of a location list emitting a location
17907 expression instead, but only if we didn't have more than one
17908 location entry in the first place. If some entries were not
17909 representable, we don't want to pretend a single entry that was
17910 applies to the entire scope in which the variable is
17911 available. */
17912 if (list && loc_list->first->next)
17913 gen_llsym (list);
17914 else
17915 maybe_gen_llsym (list);
17917 return list;
17920 /* Return true if the loc_list has only single element and thus
17921 can be represented as location description. */
17923 static bool
17924 single_element_loc_list_p (dw_loc_list_ref list)
17926 gcc_assert (!list->dw_loc_next || list->ll_symbol);
17927 return !list->ll_symbol;
17930 /* Duplicate a single element of location list. */
17932 static inline dw_loc_descr_ref
17933 copy_loc_descr (dw_loc_descr_ref ref)
17935 dw_loc_descr_ref copy = ggc_alloc<dw_loc_descr_node> ();
17936 memcpy (copy, ref, sizeof (dw_loc_descr_node));
17937 return copy;
17940 /* To each location in list LIST append loc descr REF. */
17942 static void
17943 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
17945 dw_loc_descr_ref copy;
17946 add_loc_descr (&list->expr, ref);
17947 list = list->dw_loc_next;
17948 while (list)
17950 copy = copy_loc_descr (ref);
17951 add_loc_descr (&list->expr, copy);
17952 while (copy->dw_loc_next)
17953 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
17954 list = list->dw_loc_next;
17958 /* To each location in list LIST prepend loc descr REF. */
17960 static void
17961 prepend_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
17963 dw_loc_descr_ref copy;
17964 dw_loc_descr_ref ref_end = list->expr;
17965 add_loc_descr (&ref, list->expr);
17966 list->expr = ref;
17967 list = list->dw_loc_next;
17968 while (list)
17970 dw_loc_descr_ref end = list->expr;
17971 list->expr = copy = copy_loc_descr (ref);
17972 while (copy->dw_loc_next != ref_end)
17973 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
17974 copy->dw_loc_next = end;
17975 list = list->dw_loc_next;
17979 /* Given two lists RET and LIST
17980 produce location list that is result of adding expression in LIST
17981 to expression in RET on each position in program.
17982 Might be destructive on both RET and LIST.
17984 TODO: We handle only simple cases of RET or LIST having at most one
17985 element. General case would involve sorting the lists in program order
17986 and merging them that will need some additional work.
17987 Adding that will improve quality of debug info especially for SRA-ed
17988 structures. */
17990 static void
17991 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
17993 if (!list)
17994 return;
17995 if (!*ret)
17997 *ret = list;
17998 return;
18000 if (!list->dw_loc_next)
18002 add_loc_descr_to_each (*ret, list->expr);
18003 return;
18005 if (!(*ret)->dw_loc_next)
18007 prepend_loc_descr_to_each (list, (*ret)->expr);
18008 *ret = list;
18009 return;
18011 expansion_failed (NULL_TREE, NULL_RTX,
18012 "Don't know how to merge two non-trivial"
18013 " location lists.\n");
18014 *ret = NULL;
18015 return;
18018 /* LOC is constant expression. Try a luck, look it up in constant
18019 pool and return its loc_descr of its address. */
18021 static dw_loc_descr_ref
18022 cst_pool_loc_descr (tree loc)
18024 /* Get an RTL for this, if something has been emitted. */
18025 rtx rtl = lookup_constant_def (loc);
18027 if (!rtl || !MEM_P (rtl))
18029 gcc_assert (!rtl);
18030 return 0;
18032 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
18034 /* TODO: We might get more coverage if we was actually delaying expansion
18035 of all expressions till end of compilation when constant pools are fully
18036 populated. */
18037 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
18039 expansion_failed (loc, NULL_RTX,
18040 "CST value in contant pool but not marked.");
18041 return 0;
18043 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
18044 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
18047 /* Return dw_loc_list representing address of addr_expr LOC
18048 by looking for inner INDIRECT_REF expression and turning
18049 it into simple arithmetics.
18051 See loc_list_from_tree for the meaning of CONTEXT. */
18053 static dw_loc_list_ref
18054 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
18055 loc_descr_context *context)
18057 tree obj, offset;
18058 poly_int64 bitsize, bitpos, bytepos;
18059 machine_mode mode;
18060 int unsignedp, reversep, volatilep = 0;
18061 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
18063 obj = get_inner_reference (TREE_OPERAND (loc, 0),
18064 &bitsize, &bitpos, &offset, &mode,
18065 &unsignedp, &reversep, &volatilep);
18066 STRIP_NOPS (obj);
18067 if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos))
18069 expansion_failed (loc, NULL_RTX, "bitfield access");
18070 return 0;
18072 if (!INDIRECT_REF_P (obj))
18074 expansion_failed (obj,
18075 NULL_RTX, "no indirect ref in inner refrence");
18076 return 0;
18078 if (!offset && known_eq (bitpos, 0))
18079 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
18080 context);
18081 else if (toplev
18082 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
18083 && (dwarf_version >= 4 || !dwarf_strict))
18085 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
18086 if (!list_ret)
18087 return 0;
18088 if (offset)
18090 /* Variable offset. */
18091 list_ret1 = loc_list_from_tree (offset, 0, context);
18092 if (list_ret1 == 0)
18093 return 0;
18094 add_loc_list (&list_ret, list_ret1);
18095 if (!list_ret)
18096 return 0;
18097 add_loc_descr_to_each (list_ret,
18098 new_loc_descr (DW_OP_plus, 0, 0));
18100 HOST_WIDE_INT value;
18101 if (bytepos.is_constant (&value) && value > 0)
18102 add_loc_descr_to_each (list_ret,
18103 new_loc_descr (DW_OP_plus_uconst, value, 0));
18104 else if (maybe_ne (bytepos, 0))
18105 loc_list_plus_const (list_ret, bytepos);
18106 add_loc_descr_to_each (list_ret,
18107 new_loc_descr (DW_OP_stack_value, 0, 0));
18109 return list_ret;
18112 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
18113 all operations from LOC are nops, move to the last one. Insert in NOPS all
18114 operations that are skipped. */
18116 static void
18117 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
18118 hash_set<dw_loc_descr_ref> &nops)
18120 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
18122 nops.add (loc);
18123 loc = loc->dw_loc_next;
18127 /* Helper for loc_descr_without_nops: free the location description operation
18128 P. */
18130 bool
18131 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
18133 ggc_free (loc);
18134 return true;
18137 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
18138 finishes LOC. */
18140 static void
18141 loc_descr_without_nops (dw_loc_descr_ref &loc)
18143 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
18144 return;
18146 /* Set of all DW_OP_nop operations we remove. */
18147 hash_set<dw_loc_descr_ref> nops;
18149 /* First, strip all prefix NOP operations in order to keep the head of the
18150 operations list. */
18151 loc_descr_to_next_no_nop (loc, nops);
18153 for (dw_loc_descr_ref cur = loc; cur != NULL;)
18155 /* For control flow operations: strip "prefix" nops in destination
18156 labels. */
18157 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
18158 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
18159 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
18160 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
18162 /* Do the same for the operations that follow, then move to the next
18163 iteration. */
18164 if (cur->dw_loc_next != NULL)
18165 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
18166 cur = cur->dw_loc_next;
18169 nops.traverse<void *, free_loc_descr> (NULL);
18173 struct dwarf_procedure_info;
18175 /* Helper structure for location descriptions generation. */
18176 struct loc_descr_context
18178 /* The type that is implicitly referenced by DW_OP_push_object_address, or
18179 NULL_TREE if DW_OP_push_object_address in invalid for this location
18180 description. This is used when processing PLACEHOLDER_EXPR nodes. */
18181 tree context_type;
18182 /* The ..._DECL node that should be translated as a
18183 DW_OP_push_object_address operation. */
18184 tree base_decl;
18185 /* Information about the DWARF procedure we are currently generating. NULL if
18186 we are not generating a DWARF procedure. */
18187 struct dwarf_procedure_info *dpi;
18188 /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
18189 by consumer. Used for DW_TAG_generic_subrange attributes. */
18190 bool placeholder_arg;
18191 /* True if PLACEHOLDER_EXPR has been seen. */
18192 bool placeholder_seen;
18193 /* True if strict preservation of signedness has been requested. */
18194 bool strict_signedness;
18197 /* DWARF procedures generation
18199 DWARF expressions (aka. location descriptions) are used to encode variable
18200 things such as sizes or offsets. Such computations can have redundant parts
18201 that can be factorized in order to reduce the size of the output debug
18202 information. This is the whole point of DWARF procedures.
18204 Thanks to stor-layout.cc, size and offset expressions in GENERIC trees are
18205 already factorized into functions ("size functions") in order to handle very
18206 big and complex types. Such functions are quite simple: they have integral
18207 arguments, they return an integral result and their body contains only a
18208 return statement with arithmetic expressions. This is the only kind of
18209 function we are interested in translating into DWARF procedures, here.
18211 DWARF expressions and DWARF procedure are executed using a stack, so we have
18212 to define some calling convention for them to interact. Let's say that:
18214 - Before calling a DWARF procedure, DWARF expressions must push on the stack
18215 all arguments in reverse order (right-to-left) so that when the DWARF
18216 procedure execution starts, the first argument is the top of the stack.
18218 - Then, when returning, the DWARF procedure must have consumed all arguments
18219 on the stack, must have pushed the result and touched nothing else.
18221 - Each integral argument and the result are integral types can be hold in a
18222 single stack slot.
18224 - We call "frame offset" the number of stack slots that are "under DWARF
18225 procedure control": it includes the arguments slots, the temporaries and
18226 the result slot. Thus, it is equal to the number of arguments when the
18227 procedure execution starts and must be equal to one (the result) when it
18228 returns. */
18230 /* Helper structure used when generating operations for a DWARF procedure. */
18231 struct dwarf_procedure_info
18233 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
18234 currently translated. */
18235 tree fndecl;
18236 /* The number of arguments FNDECL takes. */
18237 unsigned args_count;
18240 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
18241 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
18242 equate it to this DIE. */
18244 static dw_die_ref
18245 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
18246 dw_die_ref parent_die)
18248 dw_die_ref dwarf_proc_die;
18250 if ((dwarf_version < 3 && dwarf_strict)
18251 || location == NULL)
18252 return NULL;
18254 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
18255 if (fndecl)
18256 equate_decl_number_to_die (fndecl, dwarf_proc_die);
18257 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
18258 return dwarf_proc_die;
18261 /* Return whether TYPE is a supported type as a DWARF procedure argument
18262 type or return type (we handle only scalar types and pointer types that
18263 aren't wider than the DWARF expression evaluation stack). */
18265 static bool
18266 is_handled_procedure_type (tree type)
18268 return ((INTEGRAL_TYPE_P (type)
18269 || TREE_CODE (type) == OFFSET_TYPE
18270 || TREE_CODE (type) == POINTER_TYPE)
18271 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
18274 /* Helper for resolve_args_picking: do the same but stop when coming across
18275 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
18276 offset *before* evaluating the corresponding operation. */
18278 static bool
18279 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
18280 struct dwarf_procedure_info *dpi,
18281 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
18283 /* The "frame_offset" identifier is already used to name a macro... */
18284 unsigned frame_offset_ = initial_frame_offset;
18285 dw_loc_descr_ref l;
18287 for (l = loc; l != NULL;)
18289 bool existed;
18290 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
18292 /* If we already met this node, there is nothing to compute anymore. */
18293 if (existed)
18295 /* Make sure that the stack size is consistent wherever the execution
18296 flow comes from. */
18297 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
18298 break;
18300 l_frame_offset = frame_offset_;
18302 /* If needed, relocate the picking offset with respect to the frame
18303 offset. */
18304 if (l->frame_offset_rel)
18306 unsigned HOST_WIDE_INT off;
18307 switch (l->dw_loc_opc)
18309 case DW_OP_pick:
18310 off = l->dw_loc_oprnd1.v.val_unsigned;
18311 break;
18312 case DW_OP_dup:
18313 off = 0;
18314 break;
18315 case DW_OP_over:
18316 off = 1;
18317 break;
18318 default:
18319 gcc_unreachable ();
18321 /* frame_offset_ is the size of the current stack frame, including
18322 incoming arguments. Besides, the arguments are pushed
18323 right-to-left. Thus, in order to access the Nth argument from
18324 this operation node, the picking has to skip temporaries *plus*
18325 one stack slot per argument (0 for the first one, 1 for the second
18326 one, etc.).
18328 The targetted argument number (N) is already set as the operand,
18329 and the number of temporaries can be computed with:
18330 frame_offsets_ - dpi->args_count */
18331 off += frame_offset_ - dpi->args_count;
18333 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
18334 if (off > 255)
18335 return false;
18337 if (off == 0)
18339 l->dw_loc_opc = DW_OP_dup;
18340 l->dw_loc_oprnd1.v.val_unsigned = 0;
18342 else if (off == 1)
18344 l->dw_loc_opc = DW_OP_over;
18345 l->dw_loc_oprnd1.v.val_unsigned = 0;
18347 else
18349 l->dw_loc_opc = DW_OP_pick;
18350 l->dw_loc_oprnd1.v.val_unsigned = off;
18354 /* Update frame_offset according to the effect the current operation has
18355 on the stack. */
18356 switch (l->dw_loc_opc)
18358 case DW_OP_deref:
18359 case DW_OP_swap:
18360 case DW_OP_rot:
18361 case DW_OP_abs:
18362 case DW_OP_neg:
18363 case DW_OP_not:
18364 case DW_OP_plus_uconst:
18365 case DW_OP_skip:
18366 case DW_OP_reg0:
18367 case DW_OP_reg1:
18368 case DW_OP_reg2:
18369 case DW_OP_reg3:
18370 case DW_OP_reg4:
18371 case DW_OP_reg5:
18372 case DW_OP_reg6:
18373 case DW_OP_reg7:
18374 case DW_OP_reg8:
18375 case DW_OP_reg9:
18376 case DW_OP_reg10:
18377 case DW_OP_reg11:
18378 case DW_OP_reg12:
18379 case DW_OP_reg13:
18380 case DW_OP_reg14:
18381 case DW_OP_reg15:
18382 case DW_OP_reg16:
18383 case DW_OP_reg17:
18384 case DW_OP_reg18:
18385 case DW_OP_reg19:
18386 case DW_OP_reg20:
18387 case DW_OP_reg21:
18388 case DW_OP_reg22:
18389 case DW_OP_reg23:
18390 case DW_OP_reg24:
18391 case DW_OP_reg25:
18392 case DW_OP_reg26:
18393 case DW_OP_reg27:
18394 case DW_OP_reg28:
18395 case DW_OP_reg29:
18396 case DW_OP_reg30:
18397 case DW_OP_reg31:
18398 case DW_OP_bregx:
18399 case DW_OP_piece:
18400 case DW_OP_deref_size:
18401 case DW_OP_nop:
18402 case DW_OP_bit_piece:
18403 case DW_OP_implicit_value:
18404 case DW_OP_stack_value:
18405 case DW_OP_deref_type:
18406 case DW_OP_convert:
18407 case DW_OP_reinterpret:
18408 case DW_OP_GNU_deref_type:
18409 case DW_OP_GNU_convert:
18410 case DW_OP_GNU_reinterpret:
18411 break;
18413 case DW_OP_addr:
18414 case DW_OP_const1u:
18415 case DW_OP_const1s:
18416 case DW_OP_const2u:
18417 case DW_OP_const2s:
18418 case DW_OP_const4u:
18419 case DW_OP_const4s:
18420 case DW_OP_const8u:
18421 case DW_OP_const8s:
18422 case DW_OP_constu:
18423 case DW_OP_consts:
18424 case DW_OP_dup:
18425 case DW_OP_over:
18426 case DW_OP_pick:
18427 case DW_OP_lit0:
18428 case DW_OP_lit1:
18429 case DW_OP_lit2:
18430 case DW_OP_lit3:
18431 case DW_OP_lit4:
18432 case DW_OP_lit5:
18433 case DW_OP_lit6:
18434 case DW_OP_lit7:
18435 case DW_OP_lit8:
18436 case DW_OP_lit9:
18437 case DW_OP_lit10:
18438 case DW_OP_lit11:
18439 case DW_OP_lit12:
18440 case DW_OP_lit13:
18441 case DW_OP_lit14:
18442 case DW_OP_lit15:
18443 case DW_OP_lit16:
18444 case DW_OP_lit17:
18445 case DW_OP_lit18:
18446 case DW_OP_lit19:
18447 case DW_OP_lit20:
18448 case DW_OP_lit21:
18449 case DW_OP_lit22:
18450 case DW_OP_lit23:
18451 case DW_OP_lit24:
18452 case DW_OP_lit25:
18453 case DW_OP_lit26:
18454 case DW_OP_lit27:
18455 case DW_OP_lit28:
18456 case DW_OP_lit29:
18457 case DW_OP_lit30:
18458 case DW_OP_lit31:
18459 case DW_OP_breg0:
18460 case DW_OP_breg1:
18461 case DW_OP_breg2:
18462 case DW_OP_breg3:
18463 case DW_OP_breg4:
18464 case DW_OP_breg5:
18465 case DW_OP_breg6:
18466 case DW_OP_breg7:
18467 case DW_OP_breg8:
18468 case DW_OP_breg9:
18469 case DW_OP_breg10:
18470 case DW_OP_breg11:
18471 case DW_OP_breg12:
18472 case DW_OP_breg13:
18473 case DW_OP_breg14:
18474 case DW_OP_breg15:
18475 case DW_OP_breg16:
18476 case DW_OP_breg17:
18477 case DW_OP_breg18:
18478 case DW_OP_breg19:
18479 case DW_OP_breg20:
18480 case DW_OP_breg21:
18481 case DW_OP_breg22:
18482 case DW_OP_breg23:
18483 case DW_OP_breg24:
18484 case DW_OP_breg25:
18485 case DW_OP_breg26:
18486 case DW_OP_breg27:
18487 case DW_OP_breg28:
18488 case DW_OP_breg29:
18489 case DW_OP_breg30:
18490 case DW_OP_breg31:
18491 case DW_OP_fbreg:
18492 case DW_OP_push_object_address:
18493 case DW_OP_call_frame_cfa:
18494 case DW_OP_GNU_variable_value:
18495 case DW_OP_GNU_addr_index:
18496 case DW_OP_GNU_const_index:
18497 ++frame_offset_;
18498 break;
18500 case DW_OP_drop:
18501 case DW_OP_xderef:
18502 case DW_OP_and:
18503 case DW_OP_div:
18504 case DW_OP_minus:
18505 case DW_OP_mod:
18506 case DW_OP_mul:
18507 case DW_OP_or:
18508 case DW_OP_plus:
18509 case DW_OP_shl:
18510 case DW_OP_shr:
18511 case DW_OP_shra:
18512 case DW_OP_xor:
18513 case DW_OP_bra:
18514 case DW_OP_eq:
18515 case DW_OP_ge:
18516 case DW_OP_gt:
18517 case DW_OP_le:
18518 case DW_OP_lt:
18519 case DW_OP_ne:
18520 case DW_OP_regx:
18521 case DW_OP_xderef_size:
18522 --frame_offset_;
18523 break;
18525 case DW_OP_call2:
18526 case DW_OP_call4:
18527 case DW_OP_call_ref:
18529 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
18530 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
18532 if (stack_usage == NULL)
18533 return false;
18534 frame_offset_ += *stack_usage;
18535 break;
18538 case DW_OP_implicit_pointer:
18539 case DW_OP_entry_value:
18540 case DW_OP_const_type:
18541 case DW_OP_regval_type:
18542 case DW_OP_form_tls_address:
18543 case DW_OP_GNU_push_tls_address:
18544 case DW_OP_GNU_uninit:
18545 case DW_OP_GNU_encoded_addr:
18546 case DW_OP_GNU_implicit_pointer:
18547 case DW_OP_GNU_entry_value:
18548 case DW_OP_GNU_const_type:
18549 case DW_OP_GNU_regval_type:
18550 case DW_OP_GNU_parameter_ref:
18551 /* loc_list_from_tree will probably not output these operations for
18552 size functions, so assume they will not appear here. */
18553 /* Fall through... */
18555 default:
18556 gcc_unreachable ();
18559 /* Now, follow the control flow (except subroutine calls). */
18560 switch (l->dw_loc_opc)
18562 case DW_OP_bra:
18563 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
18564 frame_offsets))
18565 return false;
18566 /* Fall through. */
18568 case DW_OP_skip:
18569 l = l->dw_loc_oprnd1.v.val_loc;
18570 break;
18572 case DW_OP_stack_value:
18573 return true;
18575 default:
18576 l = l->dw_loc_next;
18577 break;
18581 return true;
18584 /* Make a DFS over operations reachable through LOC (i.e. follow branch
18585 operations) in order to resolve the operand of DW_OP_pick operations that
18586 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
18587 offset *before* LOC is executed. Return if all relocations were
18588 successful. */
18590 static bool
18591 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
18592 struct dwarf_procedure_info *dpi)
18594 /* Associate to all visited operations the frame offset *before* evaluating
18595 this operation. */
18596 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
18598 return
18599 resolve_args_picking_1 (loc, initial_frame_offset, dpi, frame_offsets);
18602 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
18603 Return NULL if it is not possible. */
18605 static dw_die_ref
18606 function_to_dwarf_procedure (tree fndecl)
18608 struct dwarf_procedure_info dpi;
18609 struct loc_descr_context ctx = {
18610 NULL_TREE, /* context_type */
18611 NULL_TREE, /* base_decl */
18612 &dpi, /* dpi */
18613 false, /* placeholder_arg */
18614 false, /* placeholder_seen */
18615 true /* strict_signedness */
18617 dw_die_ref dwarf_proc_die;
18618 tree tree_body = DECL_SAVED_TREE (fndecl);
18619 dw_loc_descr_ref loc_body, epilogue;
18621 tree cursor;
18622 unsigned i;
18624 /* Do not generate multiple DWARF procedures for the same function
18625 declaration. */
18626 dwarf_proc_die = lookup_decl_die (fndecl);
18627 if (dwarf_proc_die != NULL)
18628 return dwarf_proc_die;
18630 /* DWARF procedures are available starting with the DWARFv3 standard. */
18631 if (dwarf_version < 3 && dwarf_strict)
18632 return NULL;
18634 /* We handle only functions for which we still have a body, that return a
18635 supported type and that takes arguments with supported types. Note that
18636 there is no point translating functions that return nothing. */
18637 if (tree_body == NULL_TREE
18638 || DECL_RESULT (fndecl) == NULL_TREE
18639 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
18640 return NULL;
18642 for (cursor = DECL_ARGUMENTS (fndecl);
18643 cursor != NULL_TREE;
18644 cursor = TREE_CHAIN (cursor))
18645 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
18646 return NULL;
18648 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
18649 if (TREE_CODE (tree_body) != RETURN_EXPR)
18650 return NULL;
18651 tree_body = TREE_OPERAND (tree_body, 0);
18652 if (TREE_CODE (tree_body) != MODIFY_EXPR
18653 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
18654 return NULL;
18655 tree_body = TREE_OPERAND (tree_body, 1);
18657 /* Try to translate the body expression itself. Note that this will probably
18658 cause an infinite recursion if its call graph has a cycle. This is very
18659 unlikely for size functions, however, so don't bother with such things at
18660 the moment. */
18661 dpi.fndecl = fndecl;
18662 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
18663 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
18664 if (!loc_body)
18665 return NULL;
18667 /* After evaluating all operands in "loc_body", we should still have on the
18668 stack all arguments plus the desired function result (top of the stack).
18669 Generate code in order to keep only the result in our stack frame. */
18670 epilogue = NULL;
18671 for (i = 0; i < dpi.args_count; ++i)
18673 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
18674 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
18675 op_couple->dw_loc_next->dw_loc_next = epilogue;
18676 epilogue = op_couple;
18678 add_loc_descr (&loc_body, epilogue);
18679 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
18680 return NULL;
18682 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
18683 because they are considered useful. Now there is an epilogue, they are
18684 not anymore, so give it another try. */
18685 loc_descr_without_nops (loc_body);
18687 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
18688 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
18689 though, given that size functions do not come from source, so they should
18690 not have a dedicated DW_TAG_subprogram DIE. */
18691 dwarf_proc_die
18692 = new_dwarf_proc_die (loc_body, fndecl,
18693 get_context_die (DECL_CONTEXT (fndecl)));
18695 /* The called DWARF procedure consumes one stack slot per argument and
18696 returns one stack slot. */
18697 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
18699 return dwarf_proc_die;
18702 /* Helper function for loc_list_from_tree. Perform OP binary op,
18703 but after converting arguments to type_die, afterwards convert
18704 back to unsigned. */
18706 static dw_loc_list_ref
18707 typed_binop_from_tree (enum dwarf_location_atom op, tree loc,
18708 dw_die_ref type_die, scalar_int_mode mode,
18709 struct loc_descr_context *context)
18711 dw_loc_list_ref op0, op1;
18712 dw_loc_descr_ref cvt, binop;
18714 if (type_die == NULL)
18715 return NULL;
18717 op0 = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
18718 op1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
18719 if (op0 == NULL || op1 == NULL)
18720 return NULL;
18722 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
18723 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18724 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
18725 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
18726 add_loc_descr_to_each (op0, cvt);
18728 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
18729 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18730 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
18731 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
18732 add_loc_descr_to_each (op1, cvt);
18734 add_loc_list (&op0, op1);
18735 if (op0 == NULL)
18736 return NULL;
18738 binop = new_loc_descr (op, 0, 0);
18739 convert_descriptor_to_mode (mode, binop);
18740 add_loc_descr_to_each (op0, binop);
18742 return op0;
18745 /* Generate Dwarf location list representing LOC.
18746 If WANT_ADDRESS is false, expression computing LOC will be computed
18747 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
18748 if WANT_ADDRESS is 2, expression computing address useable in location
18749 will be returned (i.e. DW_OP_reg can be used
18750 to refer to register values).
18752 CONTEXT provides information to customize the location descriptions
18753 generation. Its context_type field specifies what type is implicitly
18754 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
18755 will not be generated.
18757 Its DPI field determines whether we are generating a DWARF expression for a
18758 DWARF procedure, so PARM_DECL references are processed specifically.
18760 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
18761 and dpi fields were null. */
18763 static dw_loc_list_ref
18764 loc_list_from_tree_1 (tree loc, int want_address,
18765 struct loc_descr_context *context)
18767 dw_loc_descr_ref ret = NULL, ret1 = NULL;
18768 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
18769 int have_address = 0;
18770 enum dwarf_location_atom op;
18772 /* ??? Most of the time we do not take proper care for sign/zero
18773 extending the values properly. Hopefully this won't be a real
18774 problem... */
18776 if (context != NULL
18777 && context->base_decl == loc
18778 && want_address == 0)
18780 if (dwarf_version >= 3 || !dwarf_strict)
18781 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
18782 NULL, 0, NULL, 0, NULL);
18783 else
18784 return NULL;
18787 switch (TREE_CODE (loc))
18789 case ERROR_MARK:
18790 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
18791 return 0;
18793 case PLACEHOLDER_EXPR:
18794 /* This case involves extracting fields from an object to determine the
18795 position of other fields. It is supposed to appear only as the first
18796 operand of COMPONENT_REF nodes and to reference precisely the type
18797 that the context allows or its enclosing type. */
18798 if (context != NULL
18799 && (TREE_TYPE (loc) == context->context_type
18800 || TREE_TYPE (loc) == TYPE_CONTEXT (context->context_type))
18801 && want_address >= 1)
18803 if (dwarf_version >= 3 || !dwarf_strict)
18805 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
18806 have_address = 1;
18807 break;
18809 else
18810 return NULL;
18812 /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
18813 the single argument passed by consumer. */
18814 else if (context != NULL
18815 && context->placeholder_arg
18816 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
18817 && want_address == 0)
18819 ret = new_loc_descr (DW_OP_pick, 0, 0);
18820 ret->frame_offset_rel = 1;
18821 context->placeholder_seen = true;
18822 break;
18824 else
18825 expansion_failed (loc, NULL_RTX,
18826 "PLACEHOLDER_EXPR for an unexpected type");
18827 break;
18829 case CALL_EXPR:
18831 tree callee = get_callee_fndecl (loc);
18832 dw_die_ref dwarf_proc;
18834 if (callee
18835 && is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee)))
18836 && (dwarf_proc = function_to_dwarf_procedure (callee)))
18838 /* DWARF procedures are used for size functions, which are built
18839 when size expressions contain conditional constructs, so we
18840 request strict preservation of signedness for comparisons. */
18841 bool old_strict_signedness;
18842 if (context)
18844 old_strict_signedness = context->strict_signedness;
18845 context->strict_signedness = true;
18848 /* Evaluate arguments right-to-left so that the first argument
18849 will be the top-most one on the stack. */
18850 for (int i = call_expr_nargs (loc) - 1; i >= 0; --i)
18852 tree arg = CALL_EXPR_ARG (loc, i);
18853 ret1 = loc_descriptor_from_tree (arg, 0, context);
18854 if (!ret1)
18856 expansion_failed (arg, NULL_RTX, "CALL_EXPR argument");
18857 return NULL;
18859 add_loc_descr (&ret, ret1);
18862 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
18863 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18864 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
18865 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
18866 add_loc_descr (&ret, ret1);
18867 if (context)
18868 context->strict_signedness = old_strict_signedness;
18870 else
18871 expansion_failed (loc, NULL_RTX, "CALL_EXPR target");
18872 break;
18875 case PREINCREMENT_EXPR:
18876 case PREDECREMENT_EXPR:
18877 case POSTINCREMENT_EXPR:
18878 case POSTDECREMENT_EXPR:
18879 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
18880 /* There are no opcodes for these operations. */
18881 return 0;
18883 case ADDR_EXPR:
18884 /* If we already want an address, see if there is INDIRECT_REF inside
18885 e.g. for &this->field. */
18886 if (want_address)
18888 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
18889 (loc, want_address == 2, context);
18890 if (list_ret)
18891 have_address = 1;
18892 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
18893 && (ret = cst_pool_loc_descr (loc)))
18894 have_address = 1;
18896 /* Otherwise, process the argument and look for the address. */
18897 if (!list_ret && !ret)
18898 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
18899 else
18901 if (want_address)
18902 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
18903 return NULL;
18905 break;
18907 case VAR_DECL:
18908 if (DECL_THREAD_LOCAL_P (loc))
18910 rtx rtl;
18911 enum dwarf_location_atom tls_op;
18912 enum dtprel_bool dtprel = dtprel_false;
18914 if (targetm.have_tls)
18916 /* If this is not defined, we have no way to emit the
18917 data. */
18918 if (!targetm.asm_out.output_dwarf_dtprel)
18919 return 0;
18921 /* The way DW_OP_GNU_push_tls_address is specified, we
18922 can only look up addresses of objects in the current
18923 module. We used DW_OP_addr as first op, but that's
18924 wrong, because DW_OP_addr is relocated by the debug
18925 info consumer, while DW_OP_GNU_push_tls_address
18926 operand shouldn't be. */
18927 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
18928 return 0;
18929 dtprel = dtprel_true;
18930 /* We check for DWARF 5 here because gdb did not implement
18931 DW_OP_form_tls_address until after 7.12. */
18932 tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
18933 : DW_OP_GNU_push_tls_address);
18935 else
18937 if (!targetm.emutls.debug_form_tls_address
18938 || !(dwarf_version >= 3 || !dwarf_strict))
18939 return 0;
18940 /* We stuffed the control variable into the DECL_VALUE_EXPR
18941 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
18942 no longer appear in gimple code. We used the control
18943 variable in specific so that we could pick it up here. */
18944 loc = DECL_VALUE_EXPR (loc);
18945 tls_op = DW_OP_form_tls_address;
18948 rtl = rtl_for_decl_location (loc);
18949 if (rtl == NULL_RTX)
18950 return 0;
18952 if (!MEM_P (rtl))
18953 return 0;
18954 rtl = XEXP (rtl, 0);
18955 if (! CONSTANT_P (rtl))
18956 return 0;
18958 ret = new_addr_loc_descr (rtl, dtprel);
18959 ret1 = new_loc_descr (tls_op, 0, 0);
18960 add_loc_descr (&ret, ret1);
18962 have_address = 1;
18963 break;
18965 /* FALLTHRU */
18967 case PARM_DECL:
18968 if (context != NULL && context->dpi != NULL
18969 && DECL_CONTEXT (loc) == context->dpi->fndecl)
18971 /* We are generating code for a DWARF procedure and we want to access
18972 one of its arguments: find the appropriate argument offset and let
18973 the resolve_args_picking pass compute the offset that complies
18974 with the stack frame size. */
18975 unsigned i = 0;
18976 tree cursor;
18978 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
18979 cursor != NULL_TREE && cursor != loc;
18980 cursor = TREE_CHAIN (cursor), ++i)
18982 /* If we are translating a DWARF procedure, all referenced parameters
18983 must belong to the current function. */
18984 gcc_assert (cursor != NULL_TREE);
18986 ret = new_loc_descr (DW_OP_pick, i, 0);
18987 ret->frame_offset_rel = 1;
18988 break;
18990 /* FALLTHRU */
18992 case RESULT_DECL:
18993 if (DECL_HAS_VALUE_EXPR_P (loc))
18995 tree value_expr = DECL_VALUE_EXPR (loc);
18997 /* Non-local frame structures are DECL_IGNORED_P variables so we need
18998 to wait until they get an RTX in order to reference them. */
18999 if (early_dwarf
19000 && TREE_CODE (value_expr) == COMPONENT_REF
19001 && VAR_P (TREE_OPERAND (value_expr, 0))
19002 && DECL_NONLOCAL_FRAME (TREE_OPERAND (value_expr, 0)))
19004 else
19005 return loc_list_from_tree_1 (value_expr, want_address, context);
19008 /* FALLTHRU */
19010 case FUNCTION_DECL:
19012 rtx rtl;
19013 var_loc_list *loc_list = lookup_decl_loc (loc);
19015 if (loc_list && loc_list->first)
19017 list_ret = dw_loc_list (loc_list, loc, want_address);
19018 have_address = want_address != 0;
19019 break;
19021 rtl = rtl_for_decl_location (loc);
19022 if (rtl == NULL_RTX)
19024 if (TREE_CODE (loc) != FUNCTION_DECL
19025 && early_dwarf
19026 && want_address != 1
19027 && ! DECL_IGNORED_P (loc)
19028 && (INTEGRAL_TYPE_P (TREE_TYPE (loc))
19029 || POINTER_TYPE_P (TREE_TYPE (loc)))
19030 && TYPE_MODE (TREE_TYPE (loc)) != BLKmode
19031 && (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (loc)))
19032 <= DWARF2_ADDR_SIZE))
19034 dw_die_ref ref = lookup_decl_die (loc);
19035 if (ref)
19037 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
19038 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
19039 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
19040 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
19042 else if (current_function_decl
19043 && DECL_CONTEXT (loc) == current_function_decl)
19045 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
19046 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
19047 ret->dw_loc_oprnd1.v.val_decl_ref = loc;
19049 break;
19051 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
19052 return 0;
19054 else if (CONST_INT_P (rtl))
19056 HOST_WIDE_INT val = INTVAL (rtl);
19057 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
19058 val &= GET_MODE_MASK (DECL_MODE (loc));
19059 ret = int_loc_descriptor (val);
19061 else if (GET_CODE (rtl) == CONST_STRING)
19063 expansion_failed (loc, NULL_RTX, "CONST_STRING");
19064 return 0;
19066 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
19067 ret = new_addr_loc_descr (rtl, dtprel_false);
19068 else
19070 machine_mode mode, mem_mode;
19072 /* Certain constructs can only be represented at top-level. */
19073 if (want_address == 2)
19075 ret = loc_descriptor (rtl, VOIDmode,
19076 VAR_INIT_STATUS_INITIALIZED);
19077 have_address = 1;
19079 else
19081 mode = GET_MODE (rtl);
19082 mem_mode = VOIDmode;
19083 if (MEM_P (rtl))
19085 mem_mode = mode;
19086 mode = get_address_mode (rtl);
19087 rtl = XEXP (rtl, 0);
19088 have_address = 1;
19090 ret = mem_loc_descriptor (rtl, mode, mem_mode,
19091 VAR_INIT_STATUS_INITIALIZED);
19093 if (!ret)
19094 expansion_failed (loc, rtl,
19095 "failed to produce loc descriptor for rtl");
19098 break;
19100 case MEM_REF:
19101 if (!integer_zerop (TREE_OPERAND (loc, 1)))
19103 have_address = 1;
19104 goto do_plus;
19106 /* Fallthru. */
19107 case INDIRECT_REF:
19108 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19109 have_address = 1;
19110 break;
19112 case TARGET_MEM_REF:
19113 case SSA_NAME:
19114 case DEBUG_EXPR_DECL:
19115 return NULL;
19117 case COMPOUND_EXPR:
19118 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
19119 context);
19121 CASE_CONVERT:
19122 case VIEW_CONVERT_EXPR:
19123 case SAVE_EXPR:
19124 case MODIFY_EXPR:
19125 case NON_LVALUE_EXPR:
19126 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
19127 context);
19129 case COMPONENT_REF:
19130 case BIT_FIELD_REF:
19131 case ARRAY_REF:
19132 case ARRAY_RANGE_REF:
19133 case REALPART_EXPR:
19134 case IMAGPART_EXPR:
19136 tree obj, offset;
19137 poly_int64 bitsize, bitpos, bytepos;
19138 machine_mode mode;
19139 int unsignedp, reversep, volatilep = 0;
19141 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
19142 &unsignedp, &reversep, &volatilep);
19144 gcc_assert (obj != loc);
19146 list_ret = loc_list_from_tree_1 (obj,
19147 want_address == 2
19148 && known_eq (bitpos, 0)
19149 && !offset ? 2 : 1,
19150 context);
19151 /* TODO: We can extract value of the small expression via shifting even
19152 for nonzero bitpos. */
19153 if (list_ret == 0)
19154 return 0;
19155 if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
19156 || !multiple_p (bitsize, BITS_PER_UNIT))
19158 expansion_failed (loc, NULL_RTX,
19159 "bitfield access");
19160 return 0;
19163 if (offset != NULL_TREE)
19165 /* Variable offset. */
19166 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
19167 if (list_ret1 == 0)
19168 return 0;
19169 add_loc_list (&list_ret, list_ret1);
19170 if (!list_ret)
19171 return 0;
19172 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
19175 HOST_WIDE_INT value;
19176 if (bytepos.is_constant (&value) && value > 0)
19177 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst,
19178 value, 0));
19179 else if (maybe_ne (bytepos, 0))
19180 loc_list_plus_const (list_ret, bytepos);
19182 have_address = 1;
19183 break;
19186 case INTEGER_CST:
19187 if ((want_address || !tree_fits_shwi_p (loc))
19188 && (ret = cst_pool_loc_descr (loc)))
19189 have_address = 1;
19190 else if (want_address == 2
19191 && tree_fits_shwi_p (loc)
19192 && (ret = address_of_int_loc_descriptor
19193 (int_size_in_bytes (TREE_TYPE (loc)),
19194 tree_to_shwi (loc))))
19195 have_address = 1;
19196 else if (tree_fits_shwi_p (loc))
19197 ret = int_loc_descriptor (tree_to_shwi (loc));
19198 else if (tree_fits_uhwi_p (loc))
19199 ret = uint_loc_descriptor (tree_to_uhwi (loc));
19200 else
19202 expansion_failed (loc, NULL_RTX,
19203 "Integer operand is not host integer");
19204 return 0;
19206 break;
19208 case POLY_INT_CST:
19210 if (want_address)
19212 expansion_failed (loc, NULL_RTX,
19213 "constant address with a runtime component");
19214 return 0;
19216 poly_int64 value;
19217 if (!poly_int_tree_p (loc, &value))
19219 expansion_failed (loc, NULL_RTX, "constant too big");
19220 return 0;
19222 ret = int_loc_descriptor (value);
19224 break;
19226 case CONSTRUCTOR:
19227 case REAL_CST:
19228 case STRING_CST:
19229 case COMPLEX_CST:
19230 if ((ret = cst_pool_loc_descr (loc)))
19231 have_address = 1;
19232 else if (TREE_CODE (loc) == CONSTRUCTOR)
19234 tree type = TREE_TYPE (loc);
19235 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
19236 unsigned HOST_WIDE_INT offset = 0;
19237 unsigned HOST_WIDE_INT cnt;
19238 constructor_elt *ce;
19240 if (TREE_CODE (type) == RECORD_TYPE)
19242 /* This is very limited, but it's enough to output
19243 pointers to member functions, as long as the
19244 referenced function is defined in the current
19245 translation unit. */
19246 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
19248 tree val = ce->value;
19250 tree field = ce->index;
19252 if (val)
19253 STRIP_NOPS (val);
19255 if (!field || DECL_BIT_FIELD (field))
19257 expansion_failed (loc, NULL_RTX,
19258 "bitfield in record type constructor");
19259 size = offset = (unsigned HOST_WIDE_INT)-1;
19260 ret = NULL;
19261 break;
19264 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
19265 unsigned HOST_WIDE_INT pos = int_byte_position (field);
19266 gcc_assert (pos + fieldsize <= size);
19267 if (pos < offset)
19269 expansion_failed (loc, NULL_RTX,
19270 "out-of-order fields in record constructor");
19271 size = offset = (unsigned HOST_WIDE_INT)-1;
19272 ret = NULL;
19273 break;
19275 if (pos > offset)
19277 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
19278 add_loc_descr (&ret, ret1);
19279 offset = pos;
19281 if (val && fieldsize != 0)
19283 ret1 = loc_descriptor_from_tree (val, want_address, context);
19284 if (!ret1)
19286 expansion_failed (loc, NULL_RTX,
19287 "unsupported expression in field");
19288 size = offset = (unsigned HOST_WIDE_INT)-1;
19289 ret = NULL;
19290 break;
19292 add_loc_descr (&ret, ret1);
19294 if (fieldsize)
19296 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
19297 add_loc_descr (&ret, ret1);
19298 offset = pos + fieldsize;
19302 if (offset != size)
19304 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
19305 add_loc_descr (&ret, ret1);
19306 offset = size;
19309 have_address = !!want_address;
19311 else
19312 expansion_failed (loc, NULL_RTX,
19313 "constructor of non-record type");
19315 else
19316 /* We can construct small constants here using int_loc_descriptor. */
19317 expansion_failed (loc, NULL_RTX,
19318 "constructor or constant not in constant pool");
19319 break;
19321 case TRUTH_AND_EXPR:
19322 case TRUTH_ANDIF_EXPR:
19323 case BIT_AND_EXPR:
19324 op = DW_OP_and;
19325 goto do_binop;
19327 case TRUTH_XOR_EXPR:
19328 case BIT_XOR_EXPR:
19329 op = DW_OP_xor;
19330 goto do_binop;
19332 case TRUTH_OR_EXPR:
19333 case TRUTH_ORIF_EXPR:
19334 case BIT_IOR_EXPR:
19335 op = DW_OP_or;
19336 goto do_binop;
19338 case EXACT_DIV_EXPR:
19339 case FLOOR_DIV_EXPR:
19340 case TRUNC_DIV_EXPR:
19341 /* Turn a divide by a power of 2 into a shift when possible. */
19342 if (TYPE_UNSIGNED (TREE_TYPE (loc))
19343 && tree_fits_uhwi_p (TREE_OPERAND (loc, 1)))
19345 const int log2 = exact_log2 (tree_to_uhwi (TREE_OPERAND (loc, 1)));
19346 if (log2 > 0)
19348 list_ret
19349 = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19350 if (list_ret == 0)
19351 return 0;
19353 add_loc_descr_to_each (list_ret, uint_loc_descriptor (log2));
19354 add_loc_descr_to_each (list_ret,
19355 new_loc_descr (DW_OP_shr, 0, 0));
19356 break;
19360 /* fall through */
19362 case CEIL_DIV_EXPR:
19363 case ROUND_DIV_EXPR:
19364 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
19366 enum machine_mode mode = TYPE_MODE (TREE_TYPE (loc));
19367 scalar_int_mode int_mode;
19369 if ((dwarf_strict && dwarf_version < 5)
19370 || !is_a <scalar_int_mode> (mode, &int_mode))
19371 return 0;
19373 /* We can use a signed divide if the sign bit is not set. */
19374 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
19376 op = DW_OP_div;
19377 goto do_binop;
19380 list_ret = typed_binop_from_tree (DW_OP_div, loc,
19381 base_type_for_mode (int_mode, 1),
19382 int_mode, context);
19383 break;
19385 op = DW_OP_div;
19386 goto do_binop;
19388 case MINUS_EXPR:
19389 op = DW_OP_minus;
19390 goto do_binop;
19392 case FLOOR_MOD_EXPR:
19393 case CEIL_MOD_EXPR:
19394 case ROUND_MOD_EXPR:
19395 case TRUNC_MOD_EXPR:
19396 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
19398 op = DW_OP_mod;
19399 goto do_binop;
19401 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19402 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
19403 if (list_ret == 0 || list_ret1 == 0)
19404 return 0;
19406 add_loc_list (&list_ret, list_ret1);
19407 if (list_ret == 0)
19408 return 0;
19409 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
19410 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
19411 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
19412 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
19413 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
19414 break;
19416 case MULT_EXPR:
19417 op = DW_OP_mul;
19418 goto do_binop;
19420 case LSHIFT_EXPR:
19421 op = DW_OP_shl;
19422 goto do_binop;
19424 case RSHIFT_EXPR:
19425 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
19426 goto do_binop;
19428 case POINTER_PLUS_EXPR:
19429 case PLUS_EXPR:
19430 do_plus:
19431 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
19433 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
19434 smarter to encode their opposite. The DW_OP_plus_uconst operation
19435 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
19436 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
19437 bytes, Y being the size of the operation that pushes the opposite
19438 of the addend. So let's choose the smallest representation. */
19439 const tree tree_addend = TREE_OPERAND (loc, 1);
19440 offset_int wi_addend;
19441 HOST_WIDE_INT shwi_addend;
19442 dw_loc_descr_ref loc_naddend;
19444 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19445 if (list_ret == 0)
19446 return 0;
19448 /* Try to get the literal to push. It is the opposite of the addend,
19449 so as we rely on wrapping during DWARF evaluation, first decode
19450 the literal as a "DWARF-sized" signed number. */
19451 wi_addend = wi::to_offset (tree_addend);
19452 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
19453 shwi_addend = wi_addend.to_shwi ();
19454 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
19455 ? int_loc_descriptor (-shwi_addend)
19456 : NULL;
19458 if (loc_naddend != NULL
19459 && ((unsigned) size_of_uleb128 (shwi_addend)
19460 > size_of_loc_descr (loc_naddend)))
19462 add_loc_descr_to_each (list_ret, loc_naddend);
19463 add_loc_descr_to_each (list_ret,
19464 new_loc_descr (DW_OP_minus, 0, 0));
19466 else
19468 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
19470 loc_naddend = loc_cur;
19471 loc_cur = loc_cur->dw_loc_next;
19472 ggc_free (loc_naddend);
19474 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
19476 break;
19479 op = DW_OP_plus;
19480 goto do_binop;
19482 case LE_EXPR:
19483 op = DW_OP_le;
19484 goto do_comp_binop;
19486 case GE_EXPR:
19487 op = DW_OP_ge;
19488 goto do_comp_binop;
19490 case LT_EXPR:
19491 op = DW_OP_lt;
19492 goto do_comp_binop;
19494 case GT_EXPR:
19495 op = DW_OP_gt;
19496 goto do_comp_binop;
19498 do_comp_binop:
19499 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
19501 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
19502 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
19503 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
19504 TREE_CODE (loc));
19505 break;
19507 else
19508 goto do_binop;
19510 case EQ_EXPR:
19511 op = DW_OP_eq;
19512 goto do_binop;
19514 case NE_EXPR:
19515 op = DW_OP_ne;
19516 goto do_binop;
19518 do_binop:
19519 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19520 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
19521 if (list_ret == 0 || list_ret1 == 0)
19522 return 0;
19524 add_loc_list (&list_ret, list_ret1);
19525 if (list_ret == 0)
19526 return 0;
19527 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
19528 break;
19530 case TRUTH_NOT_EXPR:
19531 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19532 if (list_ret == 0)
19533 return 0;
19535 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_lit0, 0, 0));
19536 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_eq, 0, 0));
19537 break;
19539 case BIT_NOT_EXPR:
19540 op = DW_OP_not;
19541 goto do_unop;
19543 case ABS_EXPR:
19544 op = DW_OP_abs;
19545 goto do_unop;
19547 case NEGATE_EXPR:
19548 op = DW_OP_neg;
19549 goto do_unop;
19551 do_unop:
19552 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19553 if (list_ret == 0)
19554 return 0;
19556 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
19557 break;
19559 case MIN_EXPR:
19560 case MAX_EXPR:
19562 const enum tree_code code =
19563 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
19565 loc = build3 (COND_EXPR, TREE_TYPE (loc),
19566 build2 (code, integer_type_node,
19567 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
19568 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
19571 /* fall through */
19573 case COND_EXPR:
19575 dw_loc_descr_ref lhs
19576 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
19577 dw_loc_list_ref rhs
19578 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
19579 dw_loc_descr_ref bra_node, jump_node, tmp;
19581 /* DW_OP_bra is branch-on-nonzero so avoid doing useless work. */
19582 if (TREE_CODE (TREE_OPERAND (loc, 0)) == NE_EXPR
19583 && integer_zerop (TREE_OPERAND (TREE_OPERAND (loc, 0), 1)))
19584 list_ret
19585 = loc_list_from_tree_1 (TREE_OPERAND (TREE_OPERAND (loc, 0), 0),
19586 0, context);
19587 /* Likewise, swap the operands for a logically negated condition. */
19588 else if (TREE_CODE (TREE_OPERAND (loc, 0)) == TRUTH_NOT_EXPR)
19590 lhs = loc_descriptor_from_tree (TREE_OPERAND (loc, 2), 0, context);
19591 rhs = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
19592 list_ret
19593 = loc_list_from_tree_1 (TREE_OPERAND (TREE_OPERAND (loc, 0), 0),
19594 0, context);
19596 else
19597 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19598 if (list_ret == 0 || lhs == 0 || rhs == 0)
19599 return 0;
19601 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
19602 add_loc_descr_to_each (list_ret, bra_node);
19604 add_loc_list (&list_ret, rhs);
19605 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
19606 add_loc_descr_to_each (list_ret, jump_node);
19608 add_loc_descr_to_each (list_ret, lhs);
19609 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
19610 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
19612 /* ??? Need a node to point the skip at. Use a nop. */
19613 tmp = new_loc_descr (DW_OP_nop, 0, 0);
19614 add_loc_descr_to_each (list_ret, tmp);
19615 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
19616 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
19618 break;
19620 case FIX_TRUNC_EXPR:
19621 return 0;
19623 case COMPOUND_LITERAL_EXPR:
19624 return loc_list_from_tree_1 (COMPOUND_LITERAL_EXPR_DECL (loc),
19625 0, context);
19627 default:
19628 /* Leave front-end specific codes as simply unknown. This comes
19629 up, for instance, with the C STMT_EXPR. */
19630 if ((unsigned int) TREE_CODE (loc)
19631 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
19633 expansion_failed (loc, NULL_RTX,
19634 "language specific tree node");
19635 return 0;
19638 /* Otherwise this is a generic code; we should just lists all of
19639 these explicitly. We forgot one. */
19640 if (flag_checking)
19641 gcc_unreachable ();
19643 /* In a release build, we want to degrade gracefully: better to
19644 generate incomplete debugging information than to crash. */
19645 return NULL;
19648 if (!ret && !list_ret)
19649 return 0;
19651 if (want_address == 2 && !have_address
19652 && (dwarf_version >= 4 || !dwarf_strict))
19654 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
19656 expansion_failed (loc, NULL_RTX,
19657 "DWARF address size mismatch");
19658 return 0;
19660 if (ret)
19661 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
19662 else
19663 add_loc_descr_to_each (list_ret,
19664 new_loc_descr (DW_OP_stack_value, 0, 0));
19665 have_address = 1;
19667 /* Show if we can't fill the request for an address. */
19668 if (want_address && !have_address)
19670 expansion_failed (loc, NULL_RTX,
19671 "Want address and only have value");
19672 return 0;
19675 gcc_assert (!ret || !list_ret);
19677 /* If we've got an address and don't want one, dereference. */
19678 if (!want_address && have_address)
19680 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
19681 enum machine_mode mode = TYPE_MODE (TREE_TYPE (loc));
19682 scalar_int_mode int_mode;
19683 dw_die_ref type_die;
19684 dw_loc_descr_ref deref;
19686 /* If the size is greater than DWARF2_ADDR_SIZE, bail out. */
19687 if (size > DWARF2_ADDR_SIZE || size == -1)
19689 expansion_failed (loc, NULL_RTX,
19690 "DWARF address size mismatch");
19691 return 0;
19694 /* If it is equal to DWARF2_ADDR_SIZE, extension does not matter. */
19695 else if (size == DWARF2_ADDR_SIZE)
19696 deref = new_loc_descr (DW_OP_deref, size, 0);
19698 /* If it is lower than DWARF2_ADDR_SIZE, DW_OP_deref_size will zero-
19699 extend the value, which is really OK for unsigned types only. */
19700 else if (!(context && context->strict_signedness)
19701 || TYPE_UNSIGNED (TREE_TYPE (loc))
19702 || (dwarf_strict && dwarf_version < 5)
19703 || !is_a <scalar_int_mode> (mode, &int_mode)
19704 || !(type_die = base_type_for_mode (mode, false)))
19705 deref = new_loc_descr (DW_OP_deref_size, size, 0);
19707 /* Use DW_OP_deref_type for signed integral types if possible, but
19708 convert back to the generic type to avoid type mismatches later. */
19709 else
19711 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type), size, 0);
19712 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
19713 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
19714 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
19715 add_loc_descr (&deref,
19716 new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
19719 if (ret)
19720 add_loc_descr (&ret, deref);
19721 else
19722 add_loc_descr_to_each (list_ret, deref);
19725 if (ret)
19726 list_ret = new_loc_list (ret, NULL, 0, NULL, 0, NULL);
19728 return list_ret;
19731 /* Likewise, but strip useless DW_OP_nop operations in the resulting
19732 expressions. */
19734 static dw_loc_list_ref
19735 loc_list_from_tree (tree loc, int want_address,
19736 struct loc_descr_context *context)
19738 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
19740 for (dw_loc_list_ref loc_cur = result;
19741 loc_cur != NULL; loc_cur = loc_cur->dw_loc_next)
19742 loc_descr_without_nops (loc_cur->expr);
19743 return result;
19746 /* Same as above but return only single location expression. */
19747 static dw_loc_descr_ref
19748 loc_descriptor_from_tree (tree loc, int want_address,
19749 struct loc_descr_context *context)
19751 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
19752 if (!ret)
19753 return NULL;
19754 if (ret->dw_loc_next)
19756 expansion_failed (loc, NULL_RTX,
19757 "Location list where only loc descriptor needed");
19758 return NULL;
19760 return ret->expr;
19763 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
19764 pointer to the declared type for the relevant field variable, or return
19765 `integer_type_node' if the given node turns out to be an
19766 ERROR_MARK node. */
19768 static inline tree
19769 field_type (const_tree decl)
19771 tree type;
19773 if (TREE_CODE (decl) == ERROR_MARK)
19774 return integer_type_node;
19776 type = DECL_BIT_FIELD_TYPE (decl);
19777 if (type == NULL_TREE)
19778 type = TREE_TYPE (decl);
19780 return type;
19783 /* Given a pointer to a tree node, return the alignment in bits for
19784 it, or else return BITS_PER_WORD if the node actually turns out to
19785 be an ERROR_MARK node. */
19787 static inline unsigned
19788 simple_type_align_in_bits (const_tree type)
19790 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
19793 static inline unsigned
19794 simple_decl_align_in_bits (const_tree decl)
19796 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
19799 /* Return the result of rounding T up to ALIGN. */
19801 static inline offset_int
19802 round_up_to_align (const offset_int &t, unsigned int align)
19804 return wi::udiv_trunc (t + align - 1, align) * align;
19807 /* Helper structure for RECORD_TYPE processing. */
19808 struct vlr_context
19810 /* Root RECORD_TYPE. It is needed to generate data member location
19811 descriptions in variable-length records (VLR), but also to cope with
19812 variants, which are composed of nested structures multiplexed with
19813 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
19814 function processing a FIELD_DECL, it is required to be non null. */
19815 tree struct_type;
19817 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
19818 QUAL_UNION_TYPE), this holds an expression that computes the offset for
19819 this variant part as part of the root record (in storage units). For
19820 regular records, it must be NULL_TREE. */
19821 tree variant_part_offset;
19824 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
19825 addressed byte of the "containing object" for the given FIELD_DECL. If
19826 possible, return a native constant through CST_OFFSET (in which case NULL is
19827 returned); otherwise return a DWARF expression that computes the offset.
19829 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
19830 that offset is, either because the argument turns out to be a pointer to an
19831 ERROR_MARK node, or because the offset expression is too complex for us.
19833 CTX is required: see the comment for VLR_CONTEXT. */
19835 static dw_loc_descr_ref
19836 field_byte_offset (const_tree decl, struct vlr_context *ctx,
19837 HOST_WIDE_INT *cst_offset)
19839 tree tree_result;
19840 dw_loc_list_ref loc_result;
19842 *cst_offset = 0;
19844 if (TREE_CODE (decl) == ERROR_MARK)
19845 return NULL;
19846 else
19847 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
19849 /* We cannot handle variable bit offsets at the moment, so abort if it's the
19850 case. */
19851 if (TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST)
19852 return NULL;
19854 /* We used to handle only constant offsets in all cases. Now, we handle
19855 properly dynamic byte offsets only when PCC bitfield type doesn't
19856 matter. */
19857 if (PCC_BITFIELD_TYPE_MATTERS
19858 && DECL_BIT_FIELD_TYPE (decl)
19859 && TREE_CODE (DECL_FIELD_OFFSET (decl)) == INTEGER_CST)
19861 offset_int object_offset_in_bits;
19862 offset_int object_offset_in_bytes;
19863 offset_int bitpos_int;
19864 tree type;
19865 tree field_size_tree;
19866 offset_int deepest_bitpos;
19867 offset_int field_size_in_bits;
19868 unsigned int type_align_in_bits;
19869 unsigned int decl_align_in_bits;
19870 offset_int type_size_in_bits;
19872 bitpos_int = wi::to_offset (bit_position (decl));
19873 type = field_type (decl);
19874 type_size_in_bits = offset_int_type_size_in_bits (type);
19875 type_align_in_bits = simple_type_align_in_bits (type);
19877 field_size_tree = DECL_SIZE (decl);
19879 /* The size could be unspecified if there was an error, or for
19880 a flexible array member. */
19881 if (!field_size_tree)
19882 field_size_tree = bitsize_zero_node;
19884 /* If the size of the field is not constant, use the type size. */
19885 if (TREE_CODE (field_size_tree) == INTEGER_CST)
19886 field_size_in_bits = wi::to_offset (field_size_tree);
19887 else
19888 field_size_in_bits = type_size_in_bits;
19890 decl_align_in_bits = simple_decl_align_in_bits (decl);
19892 /* The GCC front-end doesn't make any attempt to keep track of the
19893 starting bit offset (relative to the start of the containing
19894 structure type) of the hypothetical "containing object" for a
19895 bit-field. Thus, when computing the byte offset value for the
19896 start of the "containing object" of a bit-field, we must deduce
19897 this information on our own. This can be rather tricky to do in
19898 some cases. For example, handling the following structure type
19899 definition when compiling for an i386/i486 target (which only
19900 aligns long long's to 32-bit boundaries) can be very tricky:
19902 struct S { int field1; long long field2:31; };
19904 Fortunately, there is a simple rule-of-thumb which can be used
19905 in such cases. When compiling for an i386/i486, GCC will
19906 allocate 8 bytes for the structure shown above. It decides to
19907 do this based upon one simple rule for bit-field allocation.
19908 GCC allocates each "containing object" for each bit-field at
19909 the first (i.e. lowest addressed) legitimate alignment boundary
19910 (based upon the required minimum alignment for the declared
19911 type of the field) which it can possibly use, subject to the
19912 condition that there is still enough available space remaining
19913 in the containing object (when allocated at the selected point)
19914 to fully accommodate all of the bits of the bit-field itself.
19916 This simple rule makes it obvious why GCC allocates 8 bytes for
19917 each object of the structure type shown above. When looking
19918 for a place to allocate the "containing object" for `field2',
19919 the compiler simply tries to allocate a 64-bit "containing
19920 object" at each successive 32-bit boundary (starting at zero)
19921 until it finds a place to allocate that 64- bit field such that
19922 at least 31 contiguous (and previously unallocated) bits remain
19923 within that selected 64 bit field. (As it turns out, for the
19924 example above, the compiler finds it is OK to allocate the
19925 "containing object" 64-bit field at bit-offset zero within the
19926 structure type.)
19928 Here we attempt to work backwards from the limited set of facts
19929 we're given, and we try to deduce from those facts, where GCC
19930 must have believed that the containing object started (within
19931 the structure type). The value we deduce is then used (by the
19932 callers of this routine) to generate DW_AT_location and
19933 DW_AT_bit_offset attributes for fields (both bit-fields and, in
19934 the case of DW_AT_location, regular fields as well). */
19936 /* Figure out the bit-distance from the start of the structure to
19937 the "deepest" bit of the bit-field. */
19938 deepest_bitpos = bitpos_int + field_size_in_bits;
19940 /* This is the tricky part. Use some fancy footwork to deduce
19941 where the lowest addressed bit of the containing object must
19942 be. */
19943 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
19945 /* Round up to type_align by default. This works best for
19946 bitfields. */
19947 object_offset_in_bits
19948 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
19950 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
19952 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
19954 /* Round up to decl_align instead. */
19955 object_offset_in_bits
19956 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
19959 object_offset_in_bytes
19960 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
19961 if (ctx->variant_part_offset == NULL_TREE)
19963 *cst_offset = object_offset_in_bytes.to_shwi ();
19964 return NULL;
19966 tree_result = wide_int_to_tree (sizetype, object_offset_in_bytes);
19968 else
19969 tree_result = byte_position (decl);
19971 if (ctx->variant_part_offset != NULL_TREE)
19972 tree_result = fold_build2 (PLUS_EXPR, TREE_TYPE (tree_result),
19973 ctx->variant_part_offset, tree_result);
19975 /* If the byte offset is a constant, it's simplier to handle a native
19976 constant rather than a DWARF expression. */
19977 if (TREE_CODE (tree_result) == INTEGER_CST)
19979 *cst_offset = wi::to_offset (tree_result).to_shwi ();
19980 return NULL;
19983 struct loc_descr_context loc_ctx = {
19984 ctx->struct_type, /* context_type */
19985 NULL_TREE, /* base_decl */
19986 NULL, /* dpi */
19987 false, /* placeholder_arg */
19988 false, /* placeholder_seen */
19989 false /* strict_signedness */
19991 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
19993 /* We want a DWARF expression: abort if we only have a location list with
19994 multiple elements. */
19995 if (!loc_result || !single_element_loc_list_p (loc_result))
19996 return NULL;
19997 else
19998 return loc_result->expr;
20001 /* The following routines define various Dwarf attributes and any data
20002 associated with them. */
20004 /* Add a location description attribute value to a DIE.
20006 This emits location attributes suitable for whole variables and
20007 whole parameters. Note that the location attributes for struct fields are
20008 generated by the routine `data_member_location_attribute' below. */
20010 static inline void
20011 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
20012 dw_loc_list_ref descr)
20014 bool check_no_locviews = true;
20015 if (descr == 0)
20016 return;
20017 if (single_element_loc_list_p (descr))
20018 add_AT_loc (die, attr_kind, descr->expr);
20019 else
20021 add_AT_loc_list (die, attr_kind, descr);
20022 gcc_assert (descr->ll_symbol);
20023 if (attr_kind == DW_AT_location && descr->vl_symbol
20024 && dwarf2out_locviews_in_attribute ())
20026 add_AT_view_list (die, DW_AT_GNU_locviews);
20027 check_no_locviews = false;
20031 if (check_no_locviews)
20032 gcc_assert (!get_AT (die, DW_AT_GNU_locviews));
20035 /* Add DW_AT_accessibility attribute to DIE if needed. */
20037 static void
20038 add_accessibility_attribute (dw_die_ref die, tree decl)
20040 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
20041 children, otherwise the default is DW_ACCESS_public. In DWARF2
20042 the default has always been DW_ACCESS_public. */
20043 if (TREE_PROTECTED (decl))
20044 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
20045 else if (TREE_PRIVATE (decl))
20047 if (dwarf_version == 2
20048 || die->die_parent == NULL
20049 || die->die_parent->die_tag != DW_TAG_class_type)
20050 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
20052 else if (dwarf_version > 2
20053 && die->die_parent
20054 && die->die_parent->die_tag == DW_TAG_class_type)
20055 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
20058 /* Attach the specialized form of location attribute used for data members of
20059 struct and union types. In the special case of a FIELD_DECL node which
20060 represents a bit-field, the "offset" part of this special location
20061 descriptor must indicate the distance in bytes from the lowest-addressed
20062 byte of the containing struct or union type to the lowest-addressed byte of
20063 the "containing object" for the bit-field. (See the `field_byte_offset'
20064 function above).
20066 For any given bit-field, the "containing object" is a hypothetical object
20067 (of some integral or enum type) within which the given bit-field lives. The
20068 type of this hypothetical "containing object" is always the same as the
20069 declared type of the individual bit-field itself (for GCC anyway... the
20070 DWARF spec doesn't actually mandate this). Note that it is the size (in
20071 bytes) of the hypothetical "containing object" which will be given in the
20072 DW_AT_byte_size attribute for this bit-field. (See the
20073 `byte_size_attribute' function below.) It is also used when calculating the
20074 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
20075 function below.)
20077 CTX is required: see the comment for VLR_CONTEXT. */
20079 static void
20080 add_data_member_location_attribute (dw_die_ref die,
20081 tree decl,
20082 struct vlr_context *ctx)
20084 HOST_WIDE_INT offset;
20085 dw_loc_descr_ref loc_descr = 0;
20087 if (TREE_CODE (decl) == TREE_BINFO)
20089 /* We're working on the TAG_inheritance for a base class. */
20090 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
20092 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
20093 aren't at a fixed offset from all (sub)objects of the same
20094 type. We need to extract the appropriate offset from our
20095 vtable. The following dwarf expression means
20097 BaseAddr = ObAddr + *((*ObAddr) - Offset)
20099 This is specific to the V3 ABI, of course. */
20101 dw_loc_descr_ref tmp;
20103 /* Make a copy of the object address. */
20104 tmp = new_loc_descr (DW_OP_dup, 0, 0);
20105 add_loc_descr (&loc_descr, tmp);
20107 /* Extract the vtable address. */
20108 tmp = new_loc_descr (DW_OP_deref, 0, 0);
20109 add_loc_descr (&loc_descr, tmp);
20111 /* Calculate the address of the offset. */
20112 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
20113 gcc_assert (offset < 0);
20115 tmp = int_loc_descriptor (-offset);
20116 add_loc_descr (&loc_descr, tmp);
20117 tmp = new_loc_descr (DW_OP_minus, 0, 0);
20118 add_loc_descr (&loc_descr, tmp);
20120 /* Extract the offset. */
20121 tmp = new_loc_descr (DW_OP_deref, 0, 0);
20122 add_loc_descr (&loc_descr, tmp);
20124 /* Add it to the object address. */
20125 tmp = new_loc_descr (DW_OP_plus, 0, 0);
20126 add_loc_descr (&loc_descr, tmp);
20128 else
20129 offset = tree_to_shwi (BINFO_OFFSET (decl));
20131 else
20133 loc_descr = field_byte_offset (decl, ctx, &offset);
20135 if (!loc_descr)
20138 /* If loc_descr is available, then we know the offset is dynamic. */
20139 else if (gnat_encodings == DWARF_GNAT_ENCODINGS_ALL)
20141 loc_descr = NULL;
20142 offset = 0;
20145 /* Data member location evaluation starts with the base address on the
20146 stack. Compute the field offset and add it to this base address. */
20147 else
20148 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
20151 if (!loc_descr)
20153 /* While DW_AT_data_bit_offset has been added already in DWARF4,
20154 e.g. GDB only added support to it in November 2016. For DWARF5
20155 we need newer debug info consumers anyway. We might change this
20156 to dwarf_version >= 4 once most consumers catched up. */
20157 if (dwarf_version >= 5
20158 && TREE_CODE (decl) == FIELD_DECL
20159 && DECL_BIT_FIELD_TYPE (decl)
20160 && (ctx->variant_part_offset == NULL_TREE
20161 || TREE_CODE (ctx->variant_part_offset) == INTEGER_CST))
20163 tree off = bit_position (decl);
20164 if (ctx->variant_part_offset)
20165 off = bit_from_pos (ctx->variant_part_offset, off);
20166 if (tree_fits_uhwi_p (off) && get_AT (die, DW_AT_bit_size))
20168 remove_AT (die, DW_AT_byte_size);
20169 remove_AT (die, DW_AT_bit_offset);
20170 add_AT_unsigned (die, DW_AT_data_bit_offset, tree_to_uhwi (off));
20171 return;
20174 if (dwarf_version > 2)
20176 /* Don't need to output a location expression, just the constant. */
20177 if (offset < 0)
20178 add_AT_int (die, DW_AT_data_member_location, offset);
20179 else
20180 add_AT_unsigned (die, DW_AT_data_member_location, offset);
20181 return;
20183 else
20185 enum dwarf_location_atom op;
20187 /* The DWARF2 standard says that we should assume that the structure
20188 address is already on the stack, so we can specify a structure
20189 field address by using DW_OP_plus_uconst. */
20190 op = DW_OP_plus_uconst;
20191 loc_descr = new_loc_descr (op, offset, 0);
20195 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
20198 /* Writes integer values to dw_vec_const array. */
20200 static void
20201 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
20203 while (size != 0)
20205 *dest++ = val & 0xff;
20206 val >>= 8;
20207 --size;
20211 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
20213 static HOST_WIDE_INT
20214 extract_int (const unsigned char *src, unsigned int size)
20216 HOST_WIDE_INT val = 0;
20218 src += size;
20219 while (size != 0)
20221 val <<= 8;
20222 val |= *--src & 0xff;
20223 --size;
20225 return val;
20228 /* Writes wide_int values to dw_vec_const array. */
20230 static void
20231 insert_wide_int (const wide_int_ref &val, unsigned char *dest, int elt_size)
20233 int i;
20235 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
20237 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
20238 return;
20241 /* We'd have to extend this code to support odd sizes. */
20242 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
20244 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
20246 if (WORDS_BIG_ENDIAN)
20247 for (i = n - 1; i >= 0; i--)
20249 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
20250 dest += sizeof (HOST_WIDE_INT);
20252 else
20253 for (i = 0; i < n; i++)
20255 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
20256 dest += sizeof (HOST_WIDE_INT);
20260 /* Writes floating point values to dw_vec_const array. */
20262 static unsigned
20263 insert_float (const_rtx rtl, unsigned char *array)
20265 long val[4];
20266 int i;
20267 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
20269 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), mode);
20271 /* real_to_target puts 32-bit pieces in each long. Pack them. */
20272 if (GET_MODE_SIZE (mode) < 4)
20274 gcc_assert (GET_MODE_SIZE (mode) == 2);
20275 insert_int (val[0], 2, array);
20276 return 2;
20279 for (i = 0; i < GET_MODE_SIZE (mode) / 4; i++)
20281 insert_int (val[i], 4, array);
20282 array += 4;
20284 return 4;
20287 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
20288 does not have a "location" either in memory or in a register. These
20289 things can arise in GNU C when a constant is passed as an actual parameter
20290 to an inlined function. They can also arise in C++ where declared
20291 constants do not necessarily get memory "homes". */
20293 static bool
20294 add_const_value_attribute (dw_die_ref die, machine_mode mode, rtx rtl)
20296 scalar_mode int_mode;
20298 switch (GET_CODE (rtl))
20300 case CONST_INT:
20302 HOST_WIDE_INT val = INTVAL (rtl);
20304 if (val < 0)
20305 add_AT_int (die, DW_AT_const_value, val);
20306 else
20307 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
20309 return true;
20311 case CONST_WIDE_INT:
20312 if (is_int_mode (mode, &int_mode)
20313 && (GET_MODE_PRECISION (int_mode)
20314 & (HOST_BITS_PER_WIDE_INT - 1)) == 0)
20316 add_AT_wide (die, DW_AT_const_value, rtx_mode_t (rtl, int_mode));
20317 return true;
20319 return false;
20321 case CONST_DOUBLE:
20322 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
20323 floating-point constant. A CONST_DOUBLE is used whenever the
20324 constant requires more than one word in order to be adequately
20325 represented. */
20326 if (TARGET_SUPPORTS_WIDE_INT == 0
20327 && !SCALAR_FLOAT_MODE_P (GET_MODE (rtl)))
20328 add_AT_double (die, DW_AT_const_value,
20329 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
20330 else
20332 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
20333 unsigned int length = GET_MODE_SIZE (mode);
20334 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
20335 unsigned int elt_size = insert_float (rtl, array);
20337 add_AT_vec (die, DW_AT_const_value, length / elt_size, elt_size,
20338 array);
20340 return true;
20342 case CONST_VECTOR:
20344 unsigned int length;
20345 if (!CONST_VECTOR_NUNITS (rtl).is_constant (&length))
20346 return false;
20348 machine_mode mode = GET_MODE (rtl);
20349 /* The combination of a length and byte elt_size doesn't extend
20350 naturally to boolean vectors, where several elements are packed
20351 into the same byte. */
20352 if (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL)
20353 return false;
20355 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
20356 unsigned char *array
20357 = ggc_vec_alloc<unsigned char> (length * elt_size);
20358 unsigned int i;
20359 unsigned char *p;
20360 machine_mode imode = GET_MODE_INNER (mode);
20362 switch (GET_MODE_CLASS (mode))
20364 case MODE_VECTOR_INT:
20365 for (i = 0, p = array; i < length; i++, p += elt_size)
20367 rtx elt = CONST_VECTOR_ELT (rtl, i);
20368 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
20370 break;
20372 case MODE_VECTOR_FLOAT:
20373 for (i = 0, p = array; i < length; i++, p += elt_size)
20375 rtx elt = CONST_VECTOR_ELT (rtl, i);
20376 insert_float (elt, p);
20378 break;
20380 default:
20381 gcc_unreachable ();
20384 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
20386 return true;
20388 case CONST_STRING:
20389 if (dwarf_version >= 4 || !dwarf_strict)
20391 dw_loc_descr_ref loc_result;
20392 resolve_one_addr (&rtl);
20393 rtl_addr:
20394 loc_result = new_addr_loc_descr (rtl, dtprel_false);
20395 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
20396 add_AT_loc (die, DW_AT_location, loc_result);
20397 vec_safe_push (used_rtx_array, rtl);
20398 return true;
20400 return false;
20402 case CONST:
20403 if (CONSTANT_P (XEXP (rtl, 0)))
20404 return add_const_value_attribute (die, mode, XEXP (rtl, 0));
20405 /* FALLTHROUGH */
20406 case SYMBOL_REF:
20407 if (!const_ok_for_output (rtl))
20408 return false;
20409 /* FALLTHROUGH */
20410 case LABEL_REF:
20411 if (dwarf_version >= 4 || !dwarf_strict)
20412 goto rtl_addr;
20413 return false;
20415 case PLUS:
20416 /* In cases where an inlined instance of an inline function is passed
20417 the address of an `auto' variable (which is local to the caller) we
20418 can get a situation where the DECL_RTL of the artificial local
20419 variable (for the inlining) which acts as a stand-in for the
20420 corresponding formal parameter (of the inline function) will look
20421 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
20422 exactly a compile-time constant expression, but it isn't the address
20423 of the (artificial) local variable either. Rather, it represents the
20424 *value* which the artificial local variable always has during its
20425 lifetime. We currently have no way to represent such quasi-constant
20426 values in Dwarf, so for now we just punt and generate nothing. */
20427 return false;
20429 case HIGH:
20430 case CONST_FIXED:
20431 case MINUS:
20432 case SIGN_EXTEND:
20433 case ZERO_EXTEND:
20434 case CONST_POLY_INT:
20435 return false;
20437 case MEM:
20438 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
20439 && MEM_READONLY_P (rtl)
20440 && GET_MODE (rtl) == BLKmode)
20442 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
20443 return true;
20445 return false;
20447 default:
20448 /* No other kinds of rtx should be possible here. */
20449 gcc_unreachable ();
20453 /* Determine whether the evaluation of EXPR references any variables
20454 or functions which aren't otherwise used (and therefore may not be
20455 output). */
20456 static tree
20457 reference_to_unused (tree * tp, int * walk_subtrees,
20458 void * data ATTRIBUTE_UNUSED)
20460 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
20461 *walk_subtrees = 0;
20463 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
20464 && ! TREE_ASM_WRITTEN (*tp))
20465 return *tp;
20466 /* ??? The C++ FE emits debug information for using decls, so
20467 putting gcc_unreachable here falls over. See PR31899. For now
20468 be conservative. */
20469 else if (!symtab->global_info_ready && VAR_P (*tp))
20470 return *tp;
20471 else if (VAR_P (*tp))
20473 varpool_node *node = varpool_node::get (*tp);
20474 if (!node || !node->definition)
20475 return *tp;
20477 else if (TREE_CODE (*tp) == FUNCTION_DECL
20478 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
20480 /* The call graph machinery must have finished analyzing,
20481 optimizing and gimplifying the CU by now.
20482 So if *TP has no call graph node associated
20483 to it, it means *TP will not be emitted. */
20484 if (!symtab->global_info_ready || !cgraph_node::get (*tp))
20485 return *tp;
20487 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
20488 return *tp;
20490 return NULL_TREE;
20493 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
20494 for use in a later add_const_value_attribute call. */
20496 static rtx
20497 rtl_for_decl_init (tree init, tree type)
20499 rtx rtl = NULL_RTX;
20501 STRIP_NOPS (init);
20503 /* If a variable is initialized with a string constant without embedded
20504 zeros, build CONST_STRING. */
20505 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
20507 tree enttype = TREE_TYPE (type);
20508 tree domain = TYPE_DOMAIN (type);
20509 scalar_int_mode mode;
20511 if (is_int_mode (TYPE_MODE (enttype), &mode)
20512 && GET_MODE_SIZE (mode) == 1
20513 && domain
20514 && TYPE_MAX_VALUE (domain)
20515 && TREE_CODE (TYPE_MAX_VALUE (domain)) == INTEGER_CST
20516 && integer_zerop (TYPE_MIN_VALUE (domain))
20517 && compare_tree_int (TYPE_MAX_VALUE (domain),
20518 TREE_STRING_LENGTH (init) - 1) == 0
20519 && ((size_t) TREE_STRING_LENGTH (init)
20520 == strlen (TREE_STRING_POINTER (init)) + 1))
20522 rtl = gen_rtx_CONST_STRING (VOIDmode,
20523 ggc_strdup (TREE_STRING_POINTER (init)));
20524 rtl = gen_rtx_MEM (BLKmode, rtl);
20525 MEM_READONLY_P (rtl) = 1;
20528 /* Other aggregates, and complex values, could be represented using
20529 CONCAT: FIXME!
20530 If this changes, please adjust tree_add_const_value_attribute
20531 so that for early_dwarf it will for such initializers mangle referenced
20532 decls. */
20533 else if (AGGREGATE_TYPE_P (type)
20534 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
20535 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
20536 || TREE_CODE (type) == COMPLEX_TYPE)
20538 /* Vectors only work if their mode is supported by the target.
20539 FIXME: generic vectors ought to work too. */
20540 else if (TREE_CODE (type) == VECTOR_TYPE
20541 && !VECTOR_MODE_P (TYPE_MODE (type)))
20543 /* If the initializer is something that we know will expand into an
20544 immediate RTL constant, expand it now. We must be careful not to
20545 reference variables which won't be output. */
20546 else if (initializer_constant_valid_p (init, type)
20547 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
20549 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
20550 possible. */
20551 if (TREE_CODE (type) == VECTOR_TYPE)
20552 switch (TREE_CODE (init))
20554 case VECTOR_CST:
20555 break;
20556 case CONSTRUCTOR:
20557 if (TREE_CONSTANT (init))
20559 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
20560 bool constant_p = true;
20561 tree value;
20562 unsigned HOST_WIDE_INT ix;
20564 /* Even when ctor is constant, it might contain non-*_CST
20565 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
20566 belong into VECTOR_CST nodes. */
20567 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
20568 if (!CONSTANT_CLASS_P (value))
20570 constant_p = false;
20571 break;
20574 if (constant_p)
20576 init = build_vector_from_ctor (type, elts);
20577 break;
20580 /* FALLTHRU */
20582 default:
20583 return NULL;
20586 /* Large _BitInt BLKmode INTEGER_CSTs would yield a MEM. */
20587 if (TREE_CODE (init) == INTEGER_CST
20588 && TREE_CODE (TREE_TYPE (init)) == BITINT_TYPE
20589 && TYPE_MODE (TREE_TYPE (init)) == BLKmode)
20591 if (tree_fits_shwi_p (init))
20592 return GEN_INT (tree_to_shwi (init));
20593 else
20594 return NULL;
20597 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
20599 /* If expand_expr returns a MEM, it wasn't immediate. */
20600 gcc_assert (!rtl || !MEM_P (rtl));
20603 return rtl;
20606 /* Generate RTL for the variable DECL to represent its location. */
20608 static rtx
20609 rtl_for_decl_location (tree decl)
20611 rtx rtl;
20613 /* Here we have to decide where we are going to say the parameter "lives"
20614 (as far as the debugger is concerned). We only have a couple of
20615 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
20617 DECL_RTL normally indicates where the parameter lives during most of the
20618 activation of the function. If optimization is enabled however, this
20619 could be either NULL or else a pseudo-reg. Both of those cases indicate
20620 that the parameter doesn't really live anywhere (as far as the code
20621 generation parts of GCC are concerned) during most of the function's
20622 activation. That will happen (for example) if the parameter is never
20623 referenced within the function.
20625 We could just generate a location descriptor here for all non-NULL
20626 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
20627 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
20628 where DECL_RTL is NULL or is a pseudo-reg.
20630 Note however that we can only get away with using DECL_INCOMING_RTL as
20631 a backup substitute for DECL_RTL in certain limited cases. In cases
20632 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
20633 we can be sure that the parameter was passed using the same type as it is
20634 declared to have within the function, and that its DECL_INCOMING_RTL
20635 points us to a place where a value of that type is passed.
20637 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
20638 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
20639 because in these cases DECL_INCOMING_RTL points us to a value of some
20640 type which is *different* from the type of the parameter itself. Thus,
20641 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
20642 such cases, the debugger would end up (for example) trying to fetch a
20643 `float' from a place which actually contains the first part of a
20644 `double'. That would lead to really incorrect and confusing
20645 output at debug-time.
20647 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
20648 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
20649 are a couple of exceptions however. On little-endian machines we can
20650 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
20651 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
20652 an integral type that is smaller than TREE_TYPE (decl). These cases arise
20653 when (on a little-endian machine) a non-prototyped function has a
20654 parameter declared to be of type `short' or `char'. In such cases,
20655 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
20656 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
20657 passed `int' value. If the debugger then uses that address to fetch
20658 a `short' or a `char' (on a little-endian machine) the result will be
20659 the correct data, so we allow for such exceptional cases below.
20661 Note that our goal here is to describe the place where the given formal
20662 parameter lives during most of the function's activation (i.e. between the
20663 end of the prologue and the start of the epilogue). We'll do that as best
20664 as we can. Note however that if the given formal parameter is modified
20665 sometime during the execution of the function, then a stack backtrace (at
20666 debug-time) will show the function as having been called with the *new*
20667 value rather than the value which was originally passed in. This happens
20668 rarely enough that it is not a major problem, but it *is* a problem, and
20669 I'd like to fix it.
20671 A future version of dwarf2out.cc may generate two additional attributes for
20672 any given DW_TAG_formal_parameter DIE which will describe the "passed
20673 type" and the "passed location" for the given formal parameter in addition
20674 to the attributes we now generate to indicate the "declared type" and the
20675 "active location" for each parameter. This additional set of attributes
20676 could be used by debuggers for stack backtraces. Separately, note that
20677 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
20678 This happens (for example) for inlined-instances of inline function formal
20679 parameters which are never referenced. This really shouldn't be
20680 happening. All PARM_DECL nodes should get valid non-NULL
20681 DECL_INCOMING_RTL values. FIXME. */
20683 /* Use DECL_RTL as the "location" unless we find something better. */
20684 rtl = DECL_RTL_IF_SET (decl);
20686 /* When generating abstract instances, ignore everything except
20687 constants, symbols living in memory, and symbols living in
20688 fixed registers. */
20689 if (! reload_completed)
20691 if (rtl
20692 && (CONSTANT_P (rtl)
20693 || (MEM_P (rtl)
20694 && CONSTANT_P (XEXP (rtl, 0)))
20695 || (REG_P (rtl)
20696 && VAR_P (decl)
20697 && TREE_STATIC (decl))))
20699 rtl = targetm.delegitimize_address (rtl);
20700 return rtl;
20702 rtl = NULL_RTX;
20704 else if (TREE_CODE (decl) == PARM_DECL)
20706 if (rtl == NULL_RTX
20707 || is_pseudo_reg (rtl)
20708 || (MEM_P (rtl)
20709 && is_pseudo_reg (XEXP (rtl, 0))
20710 && DECL_INCOMING_RTL (decl)
20711 && MEM_P (DECL_INCOMING_RTL (decl))
20712 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
20714 tree declared_type = TREE_TYPE (decl);
20715 tree passed_type = DECL_ARG_TYPE (decl);
20716 machine_mode dmode = TYPE_MODE (declared_type);
20717 machine_mode pmode = TYPE_MODE (passed_type);
20719 /* This decl represents a formal parameter which was optimized out.
20720 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
20721 all cases where (rtl == NULL_RTX) just below. */
20722 if (dmode == pmode)
20723 rtl = DECL_INCOMING_RTL (decl);
20724 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
20725 && SCALAR_INT_MODE_P (dmode)
20726 && known_le (GET_MODE_SIZE (dmode), GET_MODE_SIZE (pmode))
20727 && DECL_INCOMING_RTL (decl))
20729 rtx inc = DECL_INCOMING_RTL (decl);
20730 if (REG_P (inc))
20731 rtl = inc;
20732 else if (MEM_P (inc))
20734 if (BYTES_BIG_ENDIAN)
20735 rtl = adjust_address_nv (inc, dmode,
20736 GET_MODE_SIZE (pmode)
20737 - GET_MODE_SIZE (dmode));
20738 else
20739 rtl = inc;
20744 /* If the parm was passed in registers, but lives on the stack, then
20745 make a big endian correction if the mode of the type of the
20746 parameter is not the same as the mode of the rtl. */
20747 /* ??? This is the same series of checks that are made in dbxout.cc before
20748 we reach the big endian correction code there. It isn't clear if all
20749 of these checks are necessary here, but keeping them all is the safe
20750 thing to do. */
20751 else if (MEM_P (rtl)
20752 && XEXP (rtl, 0) != const0_rtx
20753 && ! CONSTANT_P (XEXP (rtl, 0))
20754 /* Not passed in memory. */
20755 && !MEM_P (DECL_INCOMING_RTL (decl))
20756 /* Not passed by invisible reference. */
20757 && (!REG_P (XEXP (rtl, 0))
20758 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
20759 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
20760 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
20761 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
20762 #endif
20764 /* Big endian correction check. */
20765 && BYTES_BIG_ENDIAN
20766 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
20767 && known_lt (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))),
20768 UNITS_PER_WORD))
20770 machine_mode addr_mode = get_address_mode (rtl);
20771 poly_int64 offset = (UNITS_PER_WORD
20772 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
20774 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
20775 plus_constant (addr_mode, XEXP (rtl, 0), offset));
20778 else if (VAR_P (decl)
20779 && rtl
20780 && MEM_P (rtl)
20781 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl)))
20783 machine_mode addr_mode = get_address_mode (rtl);
20784 poly_int64 offset = byte_lowpart_offset (TYPE_MODE (TREE_TYPE (decl)),
20785 GET_MODE (rtl));
20787 /* If a variable is declared "register" yet is smaller than
20788 a register, then if we store the variable to memory, it
20789 looks like we're storing a register-sized value, when in
20790 fact we are not. We need to adjust the offset of the
20791 storage location to reflect the actual value's bytes,
20792 else gdb will not be able to display it. */
20793 if (maybe_ne (offset, 0))
20794 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
20795 plus_constant (addr_mode, XEXP (rtl, 0), offset));
20798 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
20799 and will have been substituted directly into all expressions that use it.
20800 C does not have such a concept, but C++ and other languages do. */
20801 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
20802 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
20804 if (rtl)
20805 rtl = targetm.delegitimize_address (rtl);
20807 /* If we don't look past the constant pool, we risk emitting a
20808 reference to a constant pool entry that isn't referenced from
20809 code, and thus is not emitted. */
20810 if (rtl)
20811 rtl = avoid_constant_pool_reference (rtl);
20813 /* Try harder to get a rtl. If this symbol ends up not being emitted
20814 in the current CU, resolve_addr will remove the expression referencing
20815 it. */
20816 if (rtl == NULL_RTX
20817 && !(early_dwarf && (flag_generate_lto || flag_generate_offload))
20818 && VAR_P (decl)
20819 && !DECL_EXTERNAL (decl)
20820 && TREE_STATIC (decl)
20821 && DECL_NAME (decl)
20822 && !DECL_HARD_REGISTER (decl)
20823 && DECL_MODE (decl) != VOIDmode)
20825 rtl = make_decl_rtl_for_debug (decl);
20826 if (!MEM_P (rtl)
20827 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
20828 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
20829 rtl = NULL_RTX;
20832 return rtl;
20835 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
20836 returned. If so, the decl for the COMMON block is returned, and the
20837 value is the offset into the common block for the symbol. */
20839 static tree
20840 fortran_common (tree decl, HOST_WIDE_INT *value)
20842 tree val_expr, cvar;
20843 machine_mode mode;
20844 poly_int64 bitsize, bitpos;
20845 tree offset;
20846 HOST_WIDE_INT cbitpos;
20847 int unsignedp, reversep, volatilep = 0;
20849 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
20850 it does not have a value (the offset into the common area), or if it
20851 is thread local (as opposed to global) then it isn't common, and shouldn't
20852 be handled as such. */
20853 if (!VAR_P (decl)
20854 || !TREE_STATIC (decl)
20855 || !DECL_HAS_VALUE_EXPR_P (decl)
20856 || !is_fortran ())
20857 return NULL_TREE;
20859 val_expr = DECL_VALUE_EXPR (decl);
20860 if (TREE_CODE (val_expr) != COMPONENT_REF)
20861 return NULL_TREE;
20863 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
20864 &unsignedp, &reversep, &volatilep);
20866 if (cvar == NULL_TREE
20867 || !VAR_P (cvar)
20868 || DECL_ARTIFICIAL (cvar)
20869 || !TREE_PUBLIC (cvar)
20870 /* We don't expect to have to cope with variable offsets,
20871 since at present all static data must have a constant size. */
20872 || !bitpos.is_constant (&cbitpos))
20873 return NULL_TREE;
20875 *value = 0;
20876 if (offset != NULL)
20878 if (!tree_fits_shwi_p (offset))
20879 return NULL_TREE;
20880 *value = tree_to_shwi (offset);
20882 if (cbitpos != 0)
20883 *value += cbitpos / BITS_PER_UNIT;
20885 return cvar;
20888 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
20889 data attribute for a variable or a parameter. We generate the
20890 DW_AT_const_value attribute only in those cases where the given variable
20891 or parameter does not have a true "location" either in memory or in a
20892 register. This can happen (for example) when a constant is passed as an
20893 actual argument in a call to an inline function. (It's possible that
20894 these things can crop up in other ways also.) Note that one type of
20895 constant value which can be passed into an inlined function is a constant
20896 pointer. This can happen for example if an actual argument in an inlined
20897 function call evaluates to a compile-time constant address.
20899 CACHE_P is true if it is worth caching the location list for DECL,
20900 so that future calls can reuse it rather than regenerate it from scratch.
20901 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
20902 since we will need to refer to them each time the function is inlined. */
20904 static bool
20905 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
20907 rtx rtl;
20908 dw_loc_list_ref list;
20909 var_loc_list *loc_list;
20910 cached_dw_loc_list *cache;
20912 if (early_dwarf)
20913 return false;
20915 if (TREE_CODE (decl) == ERROR_MARK)
20916 return false;
20918 if (get_AT (die, DW_AT_location)
20919 || get_AT (die, DW_AT_const_value))
20920 return true;
20922 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
20923 || TREE_CODE (decl) == RESULT_DECL);
20925 /* Try to get some constant RTL for this decl, and use that as the value of
20926 the location. */
20928 rtl = rtl_for_decl_location (decl);
20929 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
20930 && add_const_value_attribute (die, DECL_MODE (decl), rtl))
20931 return true;
20933 /* See if we have single element location list that is equivalent to
20934 a constant value. That way we are better to use add_const_value_attribute
20935 rather than expanding constant value equivalent. */
20936 loc_list = lookup_decl_loc (decl);
20937 if (loc_list
20938 && loc_list->first
20939 && loc_list->first->next == NULL
20940 && NOTE_P (loc_list->first->loc)
20941 && NOTE_VAR_LOCATION (loc_list->first->loc)
20942 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
20944 struct var_loc_node *node;
20946 node = loc_list->first;
20947 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
20948 if (GET_CODE (rtl) == EXPR_LIST)
20949 rtl = XEXP (rtl, 0);
20950 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
20951 && add_const_value_attribute (die, DECL_MODE (decl), rtl))
20952 return true;
20954 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
20955 list several times. See if we've already cached the contents. */
20956 list = NULL;
20957 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
20958 cache_p = false;
20959 if (cache_p)
20961 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
20962 if (cache)
20963 list = cache->loc_list;
20965 if (list == NULL)
20967 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
20968 NULL);
20969 /* It is usually worth caching this result if the decl is from
20970 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
20971 if (cache_p && list && list->dw_loc_next)
20973 cached_dw_loc_list **slot
20974 = cached_dw_loc_list_table->find_slot_with_hash (decl,
20975 DECL_UID (decl),
20976 INSERT);
20977 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
20978 cache->decl_id = DECL_UID (decl);
20979 cache->loc_list = list;
20980 *slot = cache;
20983 if (list)
20985 add_AT_location_description (die, DW_AT_location, list);
20986 return true;
20988 /* None of that worked, so it must not really have a location;
20989 try adding a constant value attribute from the DECL_INITIAL. */
20990 return tree_add_const_value_attribute_for_decl (die, decl);
20993 /* Mangle referenced decls. */
20994 static tree
20995 mangle_referenced_decls (tree *tp, int *walk_subtrees, void *)
20997 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
20998 *walk_subtrees = 0;
21000 if (VAR_OR_FUNCTION_DECL_P (*tp))
21001 assign_assembler_name_if_needed (*tp);
21003 return NULL_TREE;
21006 /* Attach a DW_AT_const_value attribute to DIE. The value of the
21007 attribute is the const value T. */
21009 static bool
21010 tree_add_const_value_attribute (dw_die_ref die, tree t)
21012 tree init;
21013 tree type = TREE_TYPE (t);
21015 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
21016 return false;
21018 init = t;
21019 gcc_assert (!DECL_P (init));
21021 if (TREE_CODE (init) == INTEGER_CST)
21023 if (tree_fits_uhwi_p (init))
21025 add_AT_unsigned (die, DW_AT_const_value, tree_to_uhwi (init));
21026 return true;
21028 if (tree_fits_shwi_p (init))
21030 add_AT_int (die, DW_AT_const_value, tree_to_shwi (init));
21031 return true;
21034 if (!early_dwarf)
21036 rtx rtl = rtl_for_decl_init (init, type);
21037 if (rtl)
21038 return add_const_value_attribute (die, TYPE_MODE (type), rtl);
21040 else
21042 /* For early_dwarf force mangling of all referenced symbols. */
21043 tree initializer = init;
21044 STRIP_NOPS (initializer);
21045 /* rtl_for_decl_init punts on other aggregates, and complex values. */
21046 if (AGGREGATE_TYPE_P (type)
21047 || (TREE_CODE (initializer) == VIEW_CONVERT_EXPR
21048 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (initializer, 0))))
21049 || TREE_CODE (type) == COMPLEX_TYPE)
21051 else if (initializer_constant_valid_p (initializer, type))
21052 walk_tree (&initializer, mangle_referenced_decls, NULL, NULL);
21054 /* If the host and target are sane, try harder. */
21055 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
21056 && initializer_constant_valid_p (init, type))
21058 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
21059 if (size > 0 && (int) size == size)
21061 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
21063 if (native_encode_initializer (init, array, size) == size)
21065 add_AT_vec (die, DW_AT_const_value, size, 1, array);
21066 return true;
21068 ggc_free (array);
21071 return false;
21074 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
21075 attribute is the const value of T, where T is an integral constant
21076 variable with static storage duration
21077 (so it can't be a PARM_DECL or a RESULT_DECL). */
21079 static bool
21080 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
21083 if (!decl
21084 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
21085 || (VAR_P (decl) && !TREE_STATIC (decl)))
21086 return false;
21088 if (TREE_READONLY (decl)
21089 && ! TREE_THIS_VOLATILE (decl)
21090 && DECL_INITIAL (decl))
21091 /* OK */;
21092 else
21093 return false;
21095 /* Don't add DW_AT_const_value if abstract origin already has one. */
21096 if (get_AT (var_die, DW_AT_const_value))
21097 return false;
21099 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
21102 /* Convert the CFI instructions for the current function into a
21103 location list. This is used for DW_AT_frame_base when we targeting
21104 a dwarf2 consumer that does not support the dwarf3
21105 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
21106 expressions. */
21108 static dw_loc_list_ref
21109 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
21111 int ix;
21112 dw_fde_ref fde;
21113 dw_loc_list_ref list, *list_tail;
21114 dw_cfi_ref cfi;
21115 dw_cfa_location last_cfa, next_cfa;
21116 const char *start_label, *last_label, *section;
21117 dw_cfa_location remember;
21119 fde = cfun->fde;
21120 gcc_assert (fde != NULL);
21122 section = secname_for_decl (current_function_decl);
21123 list_tail = &list;
21124 list = NULL;
21126 memset (&next_cfa, 0, sizeof (next_cfa));
21127 next_cfa.reg.set_by_dwreg (INVALID_REGNUM);
21128 remember = next_cfa;
21130 start_label = fde->dw_fde_begin;
21132 /* ??? Bald assumption that the CIE opcode list does not contain
21133 advance opcodes. */
21134 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
21135 lookup_cfa_1 (cfi, &next_cfa, &remember);
21137 last_cfa = next_cfa;
21138 last_label = start_label;
21140 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
21142 /* If the first partition contained no CFI adjustments, the
21143 CIE opcodes apply to the whole first partition. */
21144 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
21145 fde->dw_fde_begin, 0, fde->dw_fde_end, 0, section);
21146 list_tail =&(*list_tail)->dw_loc_next;
21147 start_label = last_label = fde->dw_fde_second_begin;
21150 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
21152 switch (cfi->dw_cfi_opc)
21154 case DW_CFA_set_loc:
21155 case DW_CFA_advance_loc1:
21156 case DW_CFA_advance_loc2:
21157 case DW_CFA_advance_loc4:
21158 if (!cfa_equal_p (&last_cfa, &next_cfa))
21160 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
21161 start_label, 0, last_label, 0, section);
21163 list_tail = &(*list_tail)->dw_loc_next;
21164 last_cfa = next_cfa;
21165 start_label = last_label;
21167 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
21168 break;
21170 case DW_CFA_advance_loc:
21171 /* The encoding is complex enough that we should never emit this. */
21172 gcc_unreachable ();
21174 default:
21175 lookup_cfa_1 (cfi, &next_cfa, &remember);
21176 break;
21178 if (ix + 1 == fde->dw_fde_switch_cfi_index)
21180 if (!cfa_equal_p (&last_cfa, &next_cfa))
21182 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
21183 start_label, 0, last_label, 0, section);
21185 list_tail = &(*list_tail)->dw_loc_next;
21186 last_cfa = next_cfa;
21187 start_label = last_label;
21189 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
21190 start_label, 0, fde->dw_fde_end, 0, section);
21191 list_tail = &(*list_tail)->dw_loc_next;
21192 start_label = last_label = fde->dw_fde_second_begin;
21196 if (!cfa_equal_p (&last_cfa, &next_cfa))
21198 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
21199 start_label, 0, last_label, 0, section);
21200 list_tail = &(*list_tail)->dw_loc_next;
21201 start_label = last_label;
21204 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
21205 start_label, 0,
21206 fde->dw_fde_second_begin
21207 ? fde->dw_fde_second_end : fde->dw_fde_end, 0,
21208 section);
21210 maybe_gen_llsym (list);
21212 return list;
21215 /* Compute a displacement from the "steady-state frame pointer" to the
21216 frame base (often the same as the CFA), and store it in
21217 frame_pointer_fb_offset. OFFSET is added to the displacement
21218 before the latter is negated. */
21220 static void
21221 compute_frame_pointer_to_fb_displacement (poly_int64 offset)
21223 rtx reg, elim;
21225 #ifdef FRAME_POINTER_CFA_OFFSET
21226 reg = frame_pointer_rtx;
21227 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
21228 #else
21229 reg = arg_pointer_rtx;
21230 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
21231 #endif
21233 elim = (ira_use_lra_p
21234 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
21235 : eliminate_regs (reg, VOIDmode, NULL_RTX));
21236 elim = strip_offset_and_add (elim, &offset);
21238 frame_pointer_fb_offset = -offset;
21240 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
21241 in which to eliminate. This is because it's stack pointer isn't
21242 directly accessible as a register within the ISA. To work around
21243 this, assume that while we cannot provide a proper value for
21244 frame_pointer_fb_offset, we won't need one either. We can use
21245 hard frame pointer in debug info even if frame pointer isn't used
21246 since hard frame pointer in debug info is encoded with DW_OP_fbreg
21247 which uses the DW_AT_frame_base attribute, not hard frame pointer
21248 directly. */
21249 frame_pointer_fb_offset_valid
21250 = (elim == hard_frame_pointer_rtx || elim == stack_pointer_rtx);
21253 /* Generate a DW_AT_name attribute given some string value to be included as
21254 the value of the attribute. */
21256 void
21257 add_name_attribute (dw_die_ref die, const char *name_string)
21259 if (name_string != NULL && *name_string != 0)
21261 if (demangle_name_func)
21262 name_string = (*demangle_name_func) (name_string);
21264 add_AT_string (die, DW_AT_name, name_string);
21268 /* Generate a DW_AT_name attribute given some string value representing a
21269 file or filepath to be included as value of the attribute. */
21270 static void
21271 add_filename_attribute (dw_die_ref die, const char *name_string)
21273 if (name_string != NULL && *name_string != 0)
21274 add_filepath_AT_string (die, DW_AT_name, name_string);
21277 /* Generate a DW_AT_description attribute given some string value to be included
21278 as the value of the attribute. */
21280 static void
21281 add_desc_attribute (dw_die_ref die, const char *name_string)
21283 if (!flag_describe_dies || (dwarf_version < 3 && dwarf_strict))
21284 return;
21286 if (name_string == NULL || *name_string == 0)
21287 return;
21289 if (demangle_name_func)
21290 name_string = (*demangle_name_func) (name_string);
21292 add_AT_string (die, DW_AT_description, name_string);
21295 /* Generate a DW_AT_description attribute given some decl to be included
21296 as the value of the attribute. */
21298 static void
21299 add_desc_attribute (dw_die_ref die, tree decl)
21301 tree decl_name;
21303 if (!flag_describe_dies || (dwarf_version < 3 && dwarf_strict))
21304 return;
21306 if (decl == NULL_TREE || !DECL_P (decl))
21307 return;
21308 decl_name = DECL_NAME (decl);
21310 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
21312 const char *name = dwarf2_name (decl, 0);
21313 add_desc_attribute (die, name ? name : IDENTIFIER_POINTER (decl_name));
21315 else
21317 char *desc = print_generic_expr_to_str (decl);
21318 add_desc_attribute (die, desc);
21319 free (desc);
21323 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
21324 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
21325 of TYPE accordingly.
21327 ??? This is a temporary measure until after we're able to generate
21328 regular DWARF for the complex Ada type system. */
21330 static void
21331 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
21332 dw_die_ref context_die)
21334 tree dtype;
21335 dw_die_ref dtype_die;
21337 if (!lang_hooks.types.descriptive_type)
21338 return;
21340 dtype = lang_hooks.types.descriptive_type (type);
21341 if (!dtype)
21342 return;
21344 dtype_die = lookup_type_die (dtype);
21345 if (!dtype_die)
21347 gen_type_die (dtype, context_die);
21348 dtype_die = lookup_type_die (dtype);
21349 gcc_assert (dtype_die);
21352 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
21355 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
21357 static const char *
21358 comp_dir_string (void)
21360 const char *wd;
21361 char *wd_plus_sep = NULL;
21362 static const char *cached_wd = NULL;
21364 if (cached_wd != NULL)
21365 return cached_wd;
21367 wd = get_src_pwd ();
21368 if (wd == NULL)
21369 return NULL;
21371 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
21373 size_t wdlen = strlen (wd);
21374 wd_plus_sep = XNEWVEC (char, wdlen + 2);
21375 strcpy (wd_plus_sep, wd);
21376 wd_plus_sep [wdlen] = DIR_SEPARATOR;
21377 wd_plus_sep [wdlen + 1] = 0;
21378 wd = wd_plus_sep;
21381 cached_wd = remap_debug_filename (wd);
21383 /* remap_debug_filename can just pass through wd or return a new gc string.
21384 These two types can't be both stored in a GTY(())-tagged string, but since
21385 the cached value lives forever just copy it if needed. */
21386 if (cached_wd != wd)
21388 cached_wd = xstrdup (cached_wd);
21389 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR && wd_plus_sep != NULL)
21390 free (wd_plus_sep);
21393 return cached_wd;
21396 /* Generate a DW_AT_comp_dir attribute for DIE. */
21398 static void
21399 add_comp_dir_attribute (dw_die_ref die)
21401 const char * wd = comp_dir_string ();
21402 if (wd != NULL)
21403 add_filepath_AT_string (die, DW_AT_comp_dir, wd);
21406 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
21407 pointer computation, ...), output a representation for that bound according
21408 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
21409 loc_list_from_tree for the meaning of CONTEXT. */
21411 static void
21412 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
21413 int forms, struct loc_descr_context *context)
21415 dw_die_ref context_die, decl_die = NULL;
21416 dw_loc_list_ref list;
21417 bool strip_conversions = true;
21418 bool placeholder_seen = false;
21420 while (strip_conversions)
21421 switch (TREE_CODE (value))
21423 case ERROR_MARK:
21424 case SAVE_EXPR:
21425 return;
21427 CASE_CONVERT:
21428 case VIEW_CONVERT_EXPR:
21429 value = TREE_OPERAND (value, 0);
21430 break;
21432 default:
21433 strip_conversions = false;
21434 break;
21437 /* If possible and permitted, output the attribute as a constant. */
21438 if ((forms & dw_scalar_form_constant) != 0
21439 && TREE_CODE (value) == INTEGER_CST)
21441 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
21443 /* If HOST_WIDE_INT is big enough then represent the bound as
21444 a constant value. We need to choose a form based on
21445 whether the type is signed or unsigned. We cannot just
21446 call add_AT_unsigned if the value itself is positive
21447 (add_AT_unsigned might add the unsigned value encoded as
21448 DW_FORM_data[1248]). Some DWARF consumers will lookup the
21449 bounds type and then sign extend any unsigned values found
21450 for signed types. This is needed only for
21451 DW_AT_{lower,upper}_bound, since for most other attributes,
21452 consumers will treat DW_FORM_data[1248] as unsigned values,
21453 regardless of the underlying type. */
21454 if (prec <= HOST_BITS_PER_WIDE_INT
21455 || tree_fits_uhwi_p (value))
21457 if (TYPE_UNSIGNED (TREE_TYPE (value)))
21458 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
21459 else
21460 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
21462 else if (dwarf_version >= 5
21463 && TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (value))) == 128)
21464 /* Otherwise represent the bound as an unsigned value with
21465 the precision of its type. The precision and signedness
21466 of the type will be necessary to re-interpret it
21467 unambiguously. */
21468 add_AT_wide (die, attr, wi::to_wide (value));
21469 else
21471 rtx v = immed_wide_int_const (wi::to_wide (value),
21472 TYPE_MODE (TREE_TYPE (value)));
21473 dw_loc_descr_ref loc
21474 = loc_descriptor (v, TYPE_MODE (TREE_TYPE (value)),
21475 VAR_INIT_STATUS_INITIALIZED);
21476 if (loc)
21477 add_AT_loc (die, attr, loc);
21479 return;
21482 /* Otherwise, if it's possible and permitted too, output a reference to
21483 another DIE. */
21484 if ((forms & dw_scalar_form_reference) != 0)
21486 tree decl = NULL_TREE;
21488 /* Some type attributes reference an outer type. For instance, the upper
21489 bound of an array may reference an embedding record (this happens in
21490 Ada). */
21491 if (TREE_CODE (value) == COMPONENT_REF
21492 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
21493 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
21494 decl = TREE_OPERAND (value, 1);
21496 else if (VAR_P (value)
21497 || TREE_CODE (value) == PARM_DECL
21498 || TREE_CODE (value) == RESULT_DECL)
21499 decl = value;
21501 if (decl != NULL_TREE)
21503 decl_die = lookup_decl_die (decl);
21505 /* ??? Can this happen, or should the variable have been bound
21506 first? Probably it can, since I imagine that we try to create
21507 the types of parameters in the order in which they exist in
21508 the list, and won't have created a forward reference to a
21509 later parameter. */
21510 if (decl_die != NULL)
21512 if (get_AT (decl_die, DW_AT_location)
21513 || get_AT (decl_die, DW_AT_data_member_location)
21514 || get_AT (decl_die, DW_AT_data_bit_offset)
21515 || get_AT (decl_die, DW_AT_const_value))
21517 add_AT_die_ref (die, attr, decl_die);
21518 return;
21524 /* Last chance: try to create a stack operation procedure to evaluate the
21525 value. Do nothing if even that is not possible or permitted. */
21526 if ((forms & dw_scalar_form_exprloc) == 0)
21527 return;
21529 list = loc_list_from_tree (value, 2, context);
21530 if (context && context->placeholder_arg)
21532 placeholder_seen = context->placeholder_seen;
21533 context->placeholder_seen = false;
21535 if (list == NULL || single_element_loc_list_p (list))
21537 /* If this attribute is not a reference nor constant, it is
21538 a DWARF expression rather than location description. For that
21539 loc_list_from_tree (value, 0, &context) is needed. */
21540 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
21541 if (list2 && single_element_loc_list_p (list2))
21543 if (placeholder_seen)
21545 struct dwarf_procedure_info dpi;
21546 dpi.fndecl = NULL_TREE;
21547 dpi.args_count = 1;
21548 if (!resolve_args_picking (list2->expr, 1, &dpi))
21549 return;
21551 add_AT_loc (die, attr, list2->expr);
21552 return;
21556 /* If that failed to give a single element location list, fall back to
21557 outputting this as a reference... still if permitted. */
21558 if (list == NULL
21559 || (forms & dw_scalar_form_reference) == 0
21560 || placeholder_seen)
21561 return;
21563 if (!decl_die)
21565 if (current_function_decl == 0)
21566 context_die = comp_unit_die ();
21567 else
21568 context_die = lookup_decl_die (current_function_decl);
21570 decl_die = new_die (DW_TAG_variable, context_die, value);
21571 add_AT_flag (decl_die, DW_AT_artificial, 1);
21572 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
21573 context_die);
21576 add_AT_location_description (decl_die, DW_AT_location, list);
21577 add_AT_die_ref (die, attr, decl_die);
21580 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
21581 default. */
21583 static int
21584 lower_bound_default (void)
21586 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
21588 case DW_LANG_C:
21589 case DW_LANG_C89:
21590 case DW_LANG_C99:
21591 case DW_LANG_C11:
21592 case DW_LANG_C_plus_plus:
21593 case DW_LANG_C_plus_plus_11:
21594 case DW_LANG_C_plus_plus_14:
21595 case DW_LANG_ObjC:
21596 case DW_LANG_ObjC_plus_plus:
21597 return 0;
21598 case DW_LANG_Fortran77:
21599 case DW_LANG_Fortran90:
21600 case DW_LANG_Fortran95:
21601 case DW_LANG_Fortran03:
21602 case DW_LANG_Fortran08:
21603 return 1;
21604 case DW_LANG_UPC:
21605 case DW_LANG_D:
21606 case DW_LANG_Python:
21607 return dwarf_version >= 4 ? 0 : -1;
21608 case DW_LANG_Ada95:
21609 case DW_LANG_Ada83:
21610 case DW_LANG_Cobol74:
21611 case DW_LANG_Cobol85:
21612 case DW_LANG_Modula2:
21613 case DW_LANG_PLI:
21614 return dwarf_version >= 4 ? 1 : -1;
21615 default:
21616 return -1;
21620 /* Given a tree node describing an array bound (either lower or upper) output
21621 a representation for that bound. */
21623 static void
21624 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
21625 tree bound, struct loc_descr_context *context)
21627 int dflt;
21629 while (1)
21630 switch (TREE_CODE (bound))
21632 /* Strip all conversions. */
21633 CASE_CONVERT:
21634 case VIEW_CONVERT_EXPR:
21635 bound = TREE_OPERAND (bound, 0);
21636 break;
21638 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
21639 are even omitted when they are the default. */
21640 case INTEGER_CST:
21641 /* If the value for this bound is the default one, we can even omit the
21642 attribute. */
21643 if (bound_attr == DW_AT_lower_bound
21644 && tree_fits_shwi_p (bound)
21645 && (dflt = lower_bound_default ()) != -1
21646 && tree_to_shwi (bound) == dflt)
21647 return;
21649 /* FALLTHRU */
21651 default:
21652 /* Let GNAT encodings do the magic for self-referential bounds. */
21653 if (is_ada ()
21654 && gnat_encodings == DWARF_GNAT_ENCODINGS_ALL
21655 && contains_placeholder_p (bound))
21656 return;
21658 add_scalar_info (subrange_die, bound_attr, bound,
21659 dw_scalar_form_constant
21660 | dw_scalar_form_exprloc
21661 | dw_scalar_form_reference,
21662 context);
21663 return;
21667 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
21668 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
21670 This function reuses previously set type and bound information if
21671 available. */
21673 static void
21674 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
21676 dw_die_ref child = type_die->die_child;
21677 struct array_descr_info info;
21678 int dimension_number;
21680 if (lang_hooks.types.get_array_descr_info)
21682 memset (&info, 0, sizeof (info));
21683 if (lang_hooks.types.get_array_descr_info (type, &info))
21684 /* Fortran sometimes emits array types with no dimension. */
21685 gcc_assert (info.ndimensions >= 0
21686 && info.ndimensions
21687 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN);
21689 else
21690 info.ndimensions = 0;
21692 for (dimension_number = 0;
21693 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
21694 type = TREE_TYPE (type), dimension_number++)
21696 tree domain = TYPE_DOMAIN (type);
21698 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
21699 break;
21701 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
21702 and (in GNU C only) variable bounds. Handle all three forms
21703 here. */
21705 /* Find and reuse a previously generated DW_TAG_subrange_type if
21706 available.
21708 For multi-dimensional arrays, as we iterate through the
21709 various dimensions in the enclosing for loop above, we also
21710 iterate through the DIE children and pick at each
21711 DW_TAG_subrange_type previously generated (if available).
21712 Each child DW_TAG_subrange_type DIE describes the range of
21713 the current dimension. At this point we should have as many
21714 DW_TAG_subrange_type's as we have dimensions in the
21715 array. */
21716 dw_die_ref subrange_die = NULL;
21717 if (child)
21718 while (1)
21720 child = child->die_sib;
21721 if (child->die_tag == DW_TAG_subrange_type)
21722 subrange_die = child;
21723 if (child == type_die->die_child)
21725 /* If we wrapped around, stop looking next time. */
21726 child = NULL;
21727 break;
21729 if (child->die_tag == DW_TAG_subrange_type)
21730 break;
21732 if (!subrange_die)
21733 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
21735 if (domain)
21737 /* We have an array type with specified bounds. */
21738 tree lower = TYPE_MIN_VALUE (domain);
21739 tree upper = TYPE_MAX_VALUE (domain);
21740 tree index_type = TREE_TYPE (domain);
21742 if (dimension_number <= info.ndimensions - 1)
21744 lower = info.dimen[dimension_number].lower_bound;
21745 upper = info.dimen[dimension_number].upper_bound;
21746 index_type = info.dimen[dimension_number].bounds_type;
21749 /* Define the index type. */
21750 if (index_type && !get_AT (subrange_die, DW_AT_type))
21751 add_type_attribute (subrange_die, index_type, TYPE_UNQUALIFIED,
21752 false, type_die);
21754 /* ??? If upper is NULL, the array has unspecified length,
21755 but it does have a lower bound. This happens with Fortran
21756 dimension arr(N:*)
21757 Since the debugger is definitely going to need to know N
21758 to produce useful results, go ahead and output the lower
21759 bound solo, and hope the debugger can cope. */
21761 if (lower && !get_AT (subrange_die, DW_AT_lower_bound))
21762 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
21764 if (!get_AT (subrange_die, DW_AT_upper_bound)
21765 && !get_AT (subrange_die, DW_AT_count))
21767 if (upper)
21768 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
21769 else if ((is_c () || is_cxx ()) && COMPLETE_TYPE_P (type))
21770 /* Zero-length array. */
21771 add_bound_info (subrange_die, DW_AT_count,
21772 build_int_cst (TREE_TYPE (lower), 0), NULL);
21776 /* Otherwise we have an array type with an unspecified length. The
21777 DWARF-2 spec does not say how to handle this; let's just leave out the
21778 bounds. */
21782 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
21784 static void
21785 add_byte_size_attribute (dw_die_ref die, tree tree_node)
21787 dw_die_ref decl_die;
21788 HOST_WIDE_INT size;
21790 switch (TREE_CODE (tree_node))
21792 case ERROR_MARK:
21793 size = 0;
21794 break;
21795 case ENUMERAL_TYPE:
21796 case RECORD_TYPE:
21797 case UNION_TYPE:
21798 case QUAL_UNION_TYPE:
21799 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
21800 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
21802 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
21803 return;
21805 size = int_size_in_bytes (tree_node);
21806 break;
21807 case FIELD_DECL:
21808 /* For a data member of a struct or union, the DW_AT_byte_size is
21809 generally given as the number of bytes normally allocated for an
21810 object of the *declared* type of the member itself. This is true
21811 even for bit-fields. */
21812 size = int_size_in_bytes (field_type (tree_node));
21813 break;
21814 default:
21815 gcc_unreachable ();
21818 /* Note that `size' might be -1 when we get to this point. If it is, that
21819 indicates that the byte size of the entity in question is variable. */
21820 if (size >= 0)
21821 add_AT_unsigned (die, DW_AT_byte_size, size);
21823 /* Support for dynamically-sized objects was introduced in DWARF3. */
21824 else if (TYPE_P (tree_node)
21825 && (dwarf_version >= 3 || !dwarf_strict)
21826 && gnat_encodings != DWARF_GNAT_ENCODINGS_ALL)
21828 struct loc_descr_context ctx = {
21829 const_cast<tree> (tree_node), /* context_type */
21830 NULL_TREE, /* base_decl */
21831 NULL, /* dpi */
21832 false, /* placeholder_arg */
21833 false, /* placeholder_seen */
21834 false /* strict_signedness */
21837 tree tree_size = TYPE_SIZE_UNIT (TYPE_MAIN_VARIANT (tree_node));
21838 add_scalar_info (die, DW_AT_byte_size, tree_size,
21839 dw_scalar_form_constant
21840 | dw_scalar_form_exprloc
21841 | dw_scalar_form_reference,
21842 &ctx);
21846 /* Add a DW_AT_alignment attribute to DIE with TREE_NODE's non-default
21847 alignment. */
21849 static void
21850 add_alignment_attribute (dw_die_ref die, tree tree_node)
21852 if (dwarf_version < 5 && dwarf_strict)
21853 return;
21855 unsigned align;
21857 if (DECL_P (tree_node))
21859 if (!DECL_USER_ALIGN (tree_node))
21860 return;
21862 align = DECL_ALIGN_UNIT (tree_node);
21864 else if (TYPE_P (tree_node))
21866 if (!TYPE_USER_ALIGN (tree_node))
21867 return;
21869 align = TYPE_ALIGN_UNIT (tree_node);
21871 else
21872 gcc_unreachable ();
21874 add_AT_unsigned (die, DW_AT_alignment, align);
21877 /* For a FIELD_DECL node which represents a bit-field, output an attribute
21878 which specifies the distance in bits from the highest order bit of the
21879 "containing object" for the bit-field to the highest order bit of the
21880 bit-field itself.
21882 For any given bit-field, the "containing object" is a hypothetical object
21883 (of some integral or enum type) within which the given bit-field lives. The
21884 type of this hypothetical "containing object" is always the same as the
21885 declared type of the individual bit-field itself. The determination of the
21886 exact location of the "containing object" for a bit-field is rather
21887 complicated. It's handled by the `field_byte_offset' function (above).
21889 Note that it is the size (in bytes) of the hypothetical "containing object"
21890 which will be given in the DW_AT_byte_size attribute for this bit-field.
21891 (See `byte_size_attribute' above). */
21893 static inline void
21894 add_bit_offset_attribute (dw_die_ref die, tree decl)
21896 HOST_WIDE_INT object_offset_in_bytes;
21897 tree original_type = DECL_BIT_FIELD_TYPE (decl);
21898 HOST_WIDE_INT bitpos_int;
21899 HOST_WIDE_INT highest_order_object_bit_offset;
21900 HOST_WIDE_INT highest_order_field_bit_offset;
21901 HOST_WIDE_INT bit_offset;
21903 /* The containing object is within the DECL_CONTEXT. */
21904 struct vlr_context ctx = { DECL_CONTEXT (decl), NULL_TREE };
21906 field_byte_offset (decl, &ctx, &object_offset_in_bytes);
21908 /* Must be a field and a bit field. */
21909 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
21911 /* We can't yet handle bit-fields whose offsets are variable, so if we
21912 encounter such things, just return without generating any attribute
21913 whatsoever. Likewise for variable or too large size. */
21914 if (! tree_fits_shwi_p (bit_position (decl))
21915 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
21916 return;
21918 bitpos_int = int_bit_position (decl);
21920 /* Note that the bit offset is always the distance (in bits) from the
21921 highest-order bit of the "containing object" to the highest-order bit of
21922 the bit-field itself. Since the "high-order end" of any object or field
21923 is different on big-endian and little-endian machines, the computation
21924 below must take account of these differences. */
21925 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
21926 highest_order_field_bit_offset = bitpos_int;
21928 if (! BYTES_BIG_ENDIAN)
21930 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
21931 highest_order_object_bit_offset +=
21932 simple_type_size_in_bits (original_type);
21935 bit_offset
21936 = (! BYTES_BIG_ENDIAN
21937 ? highest_order_object_bit_offset - highest_order_field_bit_offset
21938 : highest_order_field_bit_offset - highest_order_object_bit_offset);
21940 if (bit_offset < 0)
21941 add_AT_int (die, DW_AT_bit_offset, bit_offset);
21942 else
21943 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
21946 /* For a FIELD_DECL node which represents a bit field, output an attribute
21947 which specifies the length in bits of the given field. */
21949 static inline void
21950 add_bit_size_attribute (dw_die_ref die, tree decl)
21952 /* Must be a field and a bit field. */
21953 gcc_assert (TREE_CODE (decl) == FIELD_DECL
21954 && DECL_BIT_FIELD_TYPE (decl));
21956 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
21957 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
21960 /* If the compiled language is ANSI C, then add a 'prototyped'
21961 attribute, if arg types are given for the parameters of a function. */
21963 static inline void
21964 add_prototyped_attribute (dw_die_ref die, tree func_type)
21966 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
21968 case DW_LANG_C:
21969 case DW_LANG_C89:
21970 case DW_LANG_C99:
21971 case DW_LANG_C11:
21972 case DW_LANG_ObjC:
21973 if (prototype_p (func_type))
21974 add_AT_flag (die, DW_AT_prototyped, 1);
21975 break;
21976 default:
21977 break;
21981 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
21982 by looking in the type declaration, the object declaration equate table or
21983 the block mapping. */
21985 static inline void
21986 add_abstract_origin_attribute (dw_die_ref die, tree origin)
21988 dw_die_ref origin_die = NULL;
21990 /* For late LTO debug output we want to refer directly to the abstract
21991 DIE in the early debug rather to the possibly existing concrete
21992 instance and avoid creating that just for this purpose. */
21993 sym_off_pair *desc;
21994 if (in_lto_p
21995 && external_die_map
21996 && (desc = external_die_map->get (origin)))
21998 add_AT_external_die_ref (die, DW_AT_abstract_origin,
21999 desc->sym, desc->off);
22000 return;
22003 if (DECL_P (origin))
22004 origin_die = lookup_decl_die (origin);
22005 else if (TYPE_P (origin))
22006 origin_die = lookup_type_die (origin);
22007 else if (TREE_CODE (origin) == BLOCK)
22008 origin_die = lookup_block_die (origin);
22010 /* XXX: Functions that are never lowered don't always have correct block
22011 trees (in the case of java, they simply have no block tree, in some other
22012 languages). For these functions, there is nothing we can really do to
22013 output correct debug info for inlined functions in all cases. Rather
22014 than die, we'll just produce deficient debug info now, in that we will
22015 have variables without a proper abstract origin. In the future, when all
22016 functions are lowered, we should re-add a gcc_assert (origin_die)
22017 here. */
22019 if (origin_die)
22021 dw_attr_node *a;
22022 /* Like above, if we already created a concrete instance DIE
22023 do not use that for the abstract origin but the early DIE
22024 if present. */
22025 if (in_lto_p
22026 && (a = get_AT (origin_die, DW_AT_abstract_origin)))
22027 origin_die = AT_ref (a);
22028 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
22032 /* We do not currently support the pure_virtual attribute. */
22034 static inline void
22035 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
22037 if (DECL_VINDEX (func_decl))
22039 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
22041 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
22042 add_AT_loc (die, DW_AT_vtable_elem_location,
22043 new_loc_descr (DW_OP_constu,
22044 tree_to_shwi (DECL_VINDEX (func_decl)),
22045 0));
22047 /* GNU extension: Record what type this method came from originally. */
22048 if (debug_info_level > DINFO_LEVEL_TERSE
22049 && DECL_CONTEXT (func_decl))
22050 add_AT_die_ref (die, DW_AT_containing_type,
22051 lookup_type_die (DECL_CONTEXT (func_decl)));
22055 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
22056 given decl. This used to be a vendor extension until after DWARF 4
22057 standardized it. */
22059 static void
22060 add_linkage_attr (dw_die_ref die, tree decl)
22062 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
22064 /* Mimic what assemble_name_raw does with a leading '*'. */
22065 if (name[0] == '*')
22066 name = &name[1];
22068 if (dwarf_version >= 4)
22069 add_AT_string (die, DW_AT_linkage_name, name);
22070 else
22071 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
22074 /* Add source coordinate attributes for the given decl. */
22076 static void
22077 add_src_coords_attributes (dw_die_ref die, tree decl)
22079 expanded_location s;
22081 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
22082 return;
22083 s = expand_location (DECL_SOURCE_LOCATION (decl));
22084 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
22085 add_AT_unsigned (die, DW_AT_decl_line, s.line);
22086 if (debug_column_info && s.column)
22087 add_AT_unsigned (die, DW_AT_decl_column, s.column);
22090 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
22092 static void
22093 add_linkage_name_raw (dw_die_ref die, tree decl)
22095 /* Defer until we have an assembler name set. */
22096 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
22098 limbo_die_node *asm_name;
22100 asm_name = ggc_cleared_alloc<limbo_die_node> ();
22101 asm_name->die = die;
22102 asm_name->created_for = decl;
22103 asm_name->next = deferred_asm_name;
22104 deferred_asm_name = asm_name;
22106 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
22107 add_linkage_attr (die, decl);
22110 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
22112 static void
22113 add_linkage_name (dw_die_ref die, tree decl)
22115 if (debug_info_level > DINFO_LEVEL_NONE
22116 && VAR_OR_FUNCTION_DECL_P (decl)
22117 && TREE_PUBLIC (decl)
22118 && !(VAR_P (decl) && DECL_REGISTER (decl))
22119 && die->die_tag != DW_TAG_member)
22120 add_linkage_name_raw (die, decl);
22123 /* Add a DW_AT_name attribute and source coordinate attribute for the
22124 given decl, but only if it actually has a name. */
22126 static void
22127 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
22128 bool no_linkage_name)
22130 tree decl_name;
22132 decl_name = DECL_NAME (decl);
22133 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
22135 const char *name = dwarf2_name (decl, 0);
22136 if (name)
22137 add_name_attribute (die, name);
22138 else
22139 add_desc_attribute (die, decl);
22141 if (! DECL_ARTIFICIAL (decl))
22142 add_src_coords_attributes (die, decl);
22144 if (!no_linkage_name)
22145 add_linkage_name (die, decl);
22147 else
22148 add_desc_attribute (die, decl);
22150 #ifdef VMS_DEBUGGING_INFO
22151 /* Get the function's name, as described by its RTL. This may be different
22152 from the DECL_NAME name used in the source file. */
22153 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
22155 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
22156 XEXP (DECL_RTL (decl), 0), false);
22157 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
22159 #endif /* VMS_DEBUGGING_INFO */
22162 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
22164 static void
22165 add_discr_value (dw_die_ref die, dw_discr_value *value)
22167 dw_attr_node attr;
22169 attr.dw_attr = DW_AT_discr_value;
22170 attr.dw_attr_val.val_class = dw_val_class_discr_value;
22171 attr.dw_attr_val.val_entry = NULL;
22172 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
22173 if (value->pos)
22174 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
22175 else
22176 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
22177 add_dwarf_attr (die, &attr);
22180 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
22182 static void
22183 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
22185 dw_attr_node attr;
22187 attr.dw_attr = DW_AT_discr_list;
22188 attr.dw_attr_val.val_class = dw_val_class_discr_list;
22189 attr.dw_attr_val.val_entry = NULL;
22190 attr.dw_attr_val.v.val_discr_list = discr_list;
22191 add_dwarf_attr (die, &attr);
22194 static inline dw_discr_list_ref
22195 AT_discr_list (dw_attr_node *attr)
22197 return attr->dw_attr_val.v.val_discr_list;
22200 #ifdef VMS_DEBUGGING_INFO
22201 /* Output the debug main pointer die for VMS */
22203 void
22204 dwarf2out_vms_debug_main_pointer (void)
22206 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22207 dw_die_ref die;
22209 /* Allocate the VMS debug main subprogram die. */
22210 die = new_die_raw (DW_TAG_subprogram);
22211 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
22212 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
22213 current_function_funcdef_no);
22214 add_AT_lbl_id (die, DW_AT_entry_pc, label);
22216 /* Make it the first child of comp_unit_die (). */
22217 die->die_parent = comp_unit_die ();
22218 if (comp_unit_die ()->die_child)
22220 die->die_sib = comp_unit_die ()->die_child->die_sib;
22221 comp_unit_die ()->die_child->die_sib = die;
22223 else
22225 die->die_sib = die;
22226 comp_unit_die ()->die_child = die;
22229 #endif /* VMS_DEBUGGING_INFO */
22231 /* walk_tree helper function for uses_local_type, below. */
22233 static tree
22234 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
22236 if (!TYPE_P (*tp))
22237 *walk_subtrees = 0;
22238 else
22240 tree name = TYPE_NAME (*tp);
22241 if (name && DECL_P (name) && decl_function_context (name))
22242 return *tp;
22244 return NULL_TREE;
22247 /* If TYPE involves a function-local type (including a local typedef to a
22248 non-local type), returns that type; otherwise returns NULL_TREE. */
22250 static tree
22251 uses_local_type (tree type)
22253 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
22254 return used;
22257 /* Return the DIE for the scope that immediately contains this type.
22258 Non-named types that do not involve a function-local type get global
22259 scope. Named types nested in namespaces or other types get their
22260 containing scope. All other types (i.e. function-local named types) get
22261 the current active scope. */
22263 static dw_die_ref
22264 scope_die_for (tree t, dw_die_ref context_die)
22266 dw_die_ref scope_die = NULL;
22267 tree containing_scope;
22269 /* Non-types always go in the current scope. */
22270 gcc_assert (TYPE_P (t));
22272 /* Use the scope of the typedef, rather than the scope of the type
22273 it refers to. */
22274 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
22275 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
22276 else
22277 containing_scope = TYPE_CONTEXT (t);
22279 /* Use the containing namespace if there is one. */
22280 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
22282 if (context_die == lookup_decl_die (containing_scope))
22283 /* OK */;
22284 else if (debug_info_level > DINFO_LEVEL_TERSE)
22285 context_die = get_context_die (containing_scope);
22286 else
22287 containing_scope = NULL_TREE;
22290 /* Ignore function type "scopes" from the C frontend. They mean that
22291 a tagged type is local to a parmlist of a function declarator, but
22292 that isn't useful to DWARF. */
22293 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
22294 containing_scope = NULL_TREE;
22296 if (SCOPE_FILE_SCOPE_P (containing_scope))
22298 /* If T uses a local type keep it local as well, to avoid references
22299 to function-local DIEs from outside the function. */
22300 if (current_function_decl && uses_local_type (t))
22301 scope_die = context_die;
22302 else
22303 scope_die = comp_unit_die ();
22305 else if (TYPE_P (containing_scope))
22307 /* For types, we can just look up the appropriate DIE. */
22308 if (debug_info_level > DINFO_LEVEL_TERSE)
22309 scope_die = get_context_die (containing_scope);
22310 else
22312 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
22313 if (scope_die == NULL)
22314 scope_die = comp_unit_die ();
22317 else
22318 scope_die = context_die;
22320 return scope_die;
22323 /* Returns true if CONTEXT_DIE is internal to a function. */
22325 static inline bool
22326 local_scope_p (dw_die_ref context_die)
22328 for (; context_die; context_die = context_die->die_parent)
22329 if (context_die->die_tag == DW_TAG_inlined_subroutine
22330 || context_die->die_tag == DW_TAG_subprogram)
22331 return true;
22333 return false;
22336 /* Returns true if CONTEXT_DIE is a class. */
22338 static inline bool
22339 class_scope_p (dw_die_ref context_die)
22341 return (context_die
22342 && (context_die->die_tag == DW_TAG_structure_type
22343 || context_die->die_tag == DW_TAG_class_type
22344 || context_die->die_tag == DW_TAG_interface_type
22345 || context_die->die_tag == DW_TAG_union_type));
22348 /* Returns true if CONTEXT_DIE is a class or namespace, for deciding
22349 whether or not to treat a DIE in this context as a declaration. */
22351 static inline bool
22352 class_or_namespace_scope_p (dw_die_ref context_die)
22354 return (class_scope_p (context_die)
22355 || (context_die && context_die->die_tag == DW_TAG_namespace));
22358 /* Many forms of DIEs require a "type description" attribute. This
22359 routine locates the proper "type descriptor" die for the type given
22360 by 'type' plus any additional qualifiers given by 'cv_quals', and
22361 adds a DW_AT_type attribute below the given die. */
22363 static void
22364 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
22365 bool reverse, dw_die_ref context_die)
22367 enum tree_code code = TREE_CODE (type);
22368 dw_die_ref type_die = NULL;
22370 if (debug_info_level <= DINFO_LEVEL_TERSE)
22371 return;
22373 /* ??? If this type is an unnamed subrange type of an integral, floating-point
22374 or fixed-point type, use the inner type. This is because we have no
22375 support for unnamed types in base_type_die. This can happen if this is
22376 an Ada subrange type. Correct solution is emit a subrange type die. */
22377 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
22378 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
22379 type = TREE_TYPE (type), code = TREE_CODE (type);
22381 if (code == ERROR_MARK
22382 /* Handle a special case. For functions whose return type is void, we
22383 generate *no* type attribute. (Note that no object may have type
22384 `void', so this only applies to function return types). */
22385 || code == VOID_TYPE)
22386 return;
22388 type_die = modified_type_die (type,
22389 cv_quals | TYPE_QUALS (type),
22390 reverse,
22391 context_die);
22393 if (type_die != NULL)
22394 add_AT_die_ref (object_die, DW_AT_type, type_die);
22397 /* Given an object die, add the calling convention attribute for the
22398 function call type. */
22399 static void
22400 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
22402 enum dwarf_calling_convention value = DW_CC_normal;
22404 value = ((enum dwarf_calling_convention)
22405 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
22407 if (is_fortran ()
22408 && id_equal (DECL_ASSEMBLER_NAME (decl), "MAIN__"))
22410 /* DWARF 2 doesn't provide a way to identify a program's source-level
22411 entry point. DW_AT_calling_convention attributes are only meant
22412 to describe functions' calling conventions. However, lacking a
22413 better way to signal the Fortran main program, we used this for
22414 a long time, following existing custom. Now, DWARF 4 has
22415 DW_AT_main_subprogram, which we add below, but some tools still
22416 rely on the old way, which we thus keep. */
22417 value = DW_CC_program;
22419 if (dwarf_version >= 4 || !dwarf_strict)
22420 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
22423 /* Only add the attribute if the backend requests it, and
22424 is not DW_CC_normal. */
22425 if (value && (value != DW_CC_normal))
22426 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
22429 /* Given a tree pointer to a struct, class, union, or enum type node, return
22430 a pointer to the (string) tag name for the given type, or zero if the type
22431 was declared without a tag. */
22433 static const char *
22434 type_tag (const_tree type)
22436 const char *name = 0;
22438 if (TYPE_NAME (type) != 0)
22440 tree t = 0;
22442 /* Find the IDENTIFIER_NODE for the type name. */
22443 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
22444 && !TYPE_NAMELESS (type))
22445 t = TYPE_NAME (type);
22447 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
22448 a TYPE_DECL node, regardless of whether or not a `typedef' was
22449 involved. */
22450 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
22451 && ! DECL_IGNORED_P (TYPE_NAME (type)))
22453 /* We want to be extra verbose. Don't call dwarf_name if
22454 DECL_NAME isn't set. The default hook for decl_printable_name
22455 doesn't like that, and in this context it's correct to return
22456 0, instead of "<anonymous>" or the like. */
22457 if (DECL_NAME (TYPE_NAME (type))
22458 && !DECL_NAMELESS (TYPE_NAME (type)))
22459 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
22462 /* Now get the name as a string, or invent one. */
22463 if (!name && t != 0)
22464 name = IDENTIFIER_POINTER (t);
22467 return (name == 0 || *name == '\0') ? 0 : name;
22470 /* Return the type associated with a data member, make a special check
22471 for bit field types. */
22473 static inline tree
22474 member_declared_type (const_tree member)
22476 return (DECL_BIT_FIELD_TYPE (member)
22477 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
22480 /* Get the decl's label, as described by its RTL. This may be different
22481 from the DECL_NAME name used in the source file. */
22483 #if 0
22484 static const char *
22485 decl_start_label (tree decl)
22487 rtx x;
22488 const char *fnname;
22490 x = DECL_RTL (decl);
22491 gcc_assert (MEM_P (x));
22493 x = XEXP (x, 0);
22494 gcc_assert (GET_CODE (x) == SYMBOL_REF);
22496 fnname = XSTR (x, 0);
22497 return fnname;
22499 #endif
22501 /* For variable-length arrays that have been previously generated, but
22502 may be incomplete due to missing subscript info, fill the subscript
22503 info. Return TRUE if this is one of those cases. */
22505 static bool
22506 fill_variable_array_bounds (tree type)
22508 if (TREE_ASM_WRITTEN (type)
22509 && TREE_CODE (type) == ARRAY_TYPE
22510 && variably_modified_type_p (type, NULL))
22512 dw_die_ref array_die = lookup_type_die (type);
22513 if (!array_die)
22514 return false;
22515 add_subscript_info (array_die, type, !is_ada ());
22516 return true;
22518 return false;
22521 /* These routines generate the internal representation of the DIE's for
22522 the compilation unit. Debugging information is collected by walking
22523 the declaration trees passed in from dwarf2out_decl(). */
22525 static void
22526 gen_array_type_die (tree type, dw_die_ref context_die)
22528 dw_die_ref array_die;
22530 /* GNU compilers represent multidimensional array types as sequences of one
22531 dimensional array types whose element types are themselves array types.
22532 We sometimes squish that down to a single array_type DIE with multiple
22533 subscripts in the Dwarf debugging info. The draft Dwarf specification
22534 say that we are allowed to do this kind of compression in C, because
22535 there is no difference between an array of arrays and a multidimensional
22536 array. We don't do this for Ada to remain as close as possible to the
22537 actual representation, which is especially important against the language
22538 flexibilty wrt arrays of variable size. */
22540 bool collapse_nested_arrays = !is_ada ();
22542 if (fill_variable_array_bounds (type))
22543 return;
22545 dw_die_ref scope_die = scope_die_for (type, context_die);
22546 tree element_type;
22548 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
22549 DW_TAG_string_type doesn't have DW_AT_type attribute). */
22550 if (TREE_CODE (type) == ARRAY_TYPE
22551 && TYPE_STRING_FLAG (type)
22552 && is_fortran ()
22553 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
22555 HOST_WIDE_INT size;
22557 array_die = new_die (DW_TAG_string_type, scope_die, type);
22558 add_name_attribute (array_die, type_tag (type));
22559 equate_type_number_to_die (type, array_die);
22560 size = int_size_in_bytes (type);
22561 if (size >= 0)
22562 add_AT_unsigned (array_die, DW_AT_byte_size, size);
22563 /* ??? We can't annotate types late, but for LTO we may not
22564 generate a location early either (gfortran.dg/save_6.f90). */
22565 else if (! (early_dwarf && (flag_generate_lto || flag_generate_offload))
22566 && TYPE_DOMAIN (type) != NULL_TREE
22567 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
22569 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
22570 tree rszdecl = szdecl;
22572 size = int_size_in_bytes (TREE_TYPE (szdecl));
22573 if (!DECL_P (szdecl))
22575 if (INDIRECT_REF_P (szdecl)
22576 && DECL_P (TREE_OPERAND (szdecl, 0)))
22578 rszdecl = TREE_OPERAND (szdecl, 0);
22579 if (int_size_in_bytes (TREE_TYPE (rszdecl))
22580 != DWARF2_ADDR_SIZE)
22581 size = 0;
22583 else
22584 size = 0;
22586 if (size > 0)
22588 dw_loc_list_ref loc
22589 = loc_list_from_tree (rszdecl, szdecl == rszdecl ? 2 : 0,
22590 NULL);
22591 if (loc)
22593 add_AT_location_description (array_die, DW_AT_string_length,
22594 loc);
22595 if (size != DWARF2_ADDR_SIZE)
22596 add_AT_unsigned (array_die, dwarf_version >= 5
22597 ? DW_AT_string_length_byte_size
22598 : DW_AT_byte_size, size);
22602 return;
22605 array_die = new_die (DW_TAG_array_type, scope_die, type);
22606 add_name_attribute (array_die, type_tag (type));
22607 equate_type_number_to_die (type, array_die);
22609 if (VECTOR_TYPE_P (type))
22610 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
22612 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
22613 if (is_fortran ()
22614 && TREE_CODE (type) == ARRAY_TYPE
22615 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
22616 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
22617 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
22619 #if 0
22620 /* We default the array ordering. Debuggers will probably do the right
22621 things even if DW_AT_ordering is not present. It's not even an issue
22622 until we start to get into multidimensional arrays anyway. If a debugger
22623 is ever caught doing the Wrong Thing for multi-dimensional arrays,
22624 then we'll have to put the DW_AT_ordering attribute back in. (But if
22625 and when we find out that we need to put these in, we will only do so
22626 for multidimensional arrays. */
22627 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
22628 #endif
22630 if (VECTOR_TYPE_P (type))
22632 /* For VECTOR_TYPEs we use an array DIE with appropriate bounds. */
22633 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
22634 int lb = lower_bound_default ();
22635 if (lb == -1)
22636 lb = 0;
22637 add_bound_info (subrange_die, DW_AT_lower_bound, size_int (lb), NULL);
22638 add_bound_info (subrange_die, DW_AT_upper_bound,
22639 size_int (lb + TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
22641 else
22642 add_subscript_info (array_die, type, collapse_nested_arrays);
22644 /* Add representation of the type of the elements of this array type and
22645 emit the corresponding DIE if we haven't done it already. */
22646 element_type = TREE_TYPE (type);
22647 if (collapse_nested_arrays)
22648 while (TREE_CODE (element_type) == ARRAY_TYPE)
22650 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
22651 break;
22652 element_type = TREE_TYPE (element_type);
22655 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
22656 TREE_CODE (type) == ARRAY_TYPE
22657 && TYPE_REVERSE_STORAGE_ORDER (type),
22658 context_die);
22660 add_gnat_descriptive_type_attribute (array_die, type, context_die);
22661 if (TYPE_ARTIFICIAL (type))
22662 add_AT_flag (array_die, DW_AT_artificial, 1);
22664 if (get_AT (array_die, DW_AT_name))
22665 add_pubtype (type, array_die);
22667 add_alignment_attribute (array_die, type);
22670 /* This routine generates DIE for array with hidden descriptor, details
22671 are filled into *info by a langhook. */
22673 static void
22674 gen_descr_array_type_die (tree type, struct array_descr_info *info,
22675 dw_die_ref context_die)
22677 const dw_die_ref scope_die = scope_die_for (type, context_die);
22678 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
22679 struct loc_descr_context context = {
22680 type, /* context_type */
22681 info->base_decl, /* base_decl */
22682 NULL, /* dpi */
22683 false, /* placeholder_arg */
22684 false, /* placeholder_seen */
22685 false /* strict_signedness */
22687 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
22688 int dim;
22690 add_name_attribute (array_die, type_tag (type));
22691 equate_type_number_to_die (type, array_die);
22693 if (info->ndimensions > 1)
22694 switch (info->ordering)
22696 case array_descr_ordering_row_major:
22697 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
22698 break;
22699 case array_descr_ordering_column_major:
22700 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
22701 break;
22702 default:
22703 break;
22706 if (dwarf_version >= 3 || !dwarf_strict)
22708 if (info->data_location)
22709 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
22710 dw_scalar_form_exprloc, &context);
22711 if (info->associated)
22712 add_scalar_info (array_die, DW_AT_associated, info->associated,
22713 dw_scalar_form_constant
22714 | dw_scalar_form_exprloc
22715 | dw_scalar_form_reference, &context);
22716 if (info->allocated)
22717 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
22718 dw_scalar_form_constant
22719 | dw_scalar_form_exprloc
22720 | dw_scalar_form_reference, &context);
22721 if (info->stride)
22723 const enum dwarf_attribute attr
22724 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
22725 const int forms
22726 = (info->stride_in_bits)
22727 ? dw_scalar_form_constant
22728 : (dw_scalar_form_constant
22729 | dw_scalar_form_exprloc
22730 | dw_scalar_form_reference);
22732 add_scalar_info (array_die, attr, info->stride, forms, &context);
22735 if (dwarf_version >= 5)
22737 if (info->rank)
22739 add_scalar_info (array_die, DW_AT_rank, info->rank,
22740 dw_scalar_form_constant
22741 | dw_scalar_form_exprloc, &context);
22742 subrange_tag = DW_TAG_generic_subrange;
22743 context.placeholder_arg = true;
22747 add_gnat_descriptive_type_attribute (array_die, type, context_die);
22749 for (dim = 0; dim < info->ndimensions; dim++)
22751 dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
22753 if (info->dimen[dim].bounds_type)
22754 add_type_attribute (subrange_die,
22755 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
22756 false, context_die);
22757 if (info->dimen[dim].lower_bound)
22758 add_bound_info (subrange_die, DW_AT_lower_bound,
22759 info->dimen[dim].lower_bound, &context);
22760 if (info->dimen[dim].upper_bound)
22761 add_bound_info (subrange_die, DW_AT_upper_bound,
22762 info->dimen[dim].upper_bound, &context);
22763 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
22764 add_scalar_info (subrange_die, DW_AT_byte_stride,
22765 info->dimen[dim].stride,
22766 dw_scalar_form_constant
22767 | dw_scalar_form_exprloc
22768 | dw_scalar_form_reference,
22769 &context);
22772 gen_type_die (info->element_type, context_die);
22773 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
22774 TREE_CODE (type) == ARRAY_TYPE
22775 && TYPE_REVERSE_STORAGE_ORDER (type),
22776 context_die);
22778 if (get_AT (array_die, DW_AT_name))
22779 add_pubtype (type, array_die);
22781 add_alignment_attribute (array_die, type);
22784 #if 0
22785 static void
22786 gen_entry_point_die (tree decl, dw_die_ref context_die)
22788 tree origin = decl_ultimate_origin (decl);
22789 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
22791 if (origin != NULL)
22792 add_abstract_origin_attribute (decl_die, origin);
22793 else
22795 add_name_and_src_coords_attributes (decl_die, decl);
22796 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
22797 TYPE_UNQUALIFIED, false, context_die);
22800 if (DECL_ABSTRACT_P (decl))
22801 equate_decl_number_to_die (decl, decl_die);
22802 else
22803 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
22805 #endif
22807 /* Walk through the list of incomplete types again, trying once more to
22808 emit full debugging info for them. */
22810 static void
22811 retry_incomplete_types (void)
22813 set_early_dwarf s;
22814 int i;
22816 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
22817 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
22818 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
22819 vec_safe_truncate (incomplete_types, 0);
22822 /* Determine what tag to use for a record type. */
22824 static enum dwarf_tag
22825 record_type_tag (tree type)
22827 if (! lang_hooks.types.classify_record)
22828 return DW_TAG_structure_type;
22830 switch (lang_hooks.types.classify_record (type))
22832 case RECORD_IS_STRUCT:
22833 return DW_TAG_structure_type;
22835 case RECORD_IS_CLASS:
22836 return DW_TAG_class_type;
22838 case RECORD_IS_INTERFACE:
22839 if (dwarf_version >= 3 || !dwarf_strict)
22840 return DW_TAG_interface_type;
22841 return DW_TAG_structure_type;
22843 default:
22844 gcc_unreachable ();
22848 /* Generate a DIE to represent an enumeration type. Note that these DIEs
22849 include all of the information about the enumeration values also. Each
22850 enumerated type name/value is listed as a child of the enumerated type
22851 DIE. REVERSE is true if the type is to be interpreted in the reverse
22852 storage order wrt the target order. */
22854 static dw_die_ref
22855 gen_enumeration_type_die (tree type, dw_die_ref context_die, bool reverse)
22857 dw_die_ref type_die = lookup_type_die (type);
22858 dw_die_ref orig_type_die = type_die;
22860 if (type_die == NULL || reverse)
22862 /* The DIE with DW_AT_endianity is placed right after the naked DIE. */
22863 if (reverse)
22865 gcc_assert (type_die);
22866 dw_die_ref after_die = type_die;
22867 type_die = new_die_raw (DW_TAG_enumeration_type);
22868 add_child_die_after (context_die, type_die, after_die);
22870 else
22872 type_die = new_die (DW_TAG_enumeration_type,
22873 scope_die_for (type, context_die), type);
22874 equate_type_number_to_die (type, type_die);
22876 add_name_attribute (type_die, type_tag (type));
22877 if ((dwarf_version >= 4 || !dwarf_strict)
22878 && ENUM_IS_SCOPED (type))
22879 add_AT_flag (type_die, DW_AT_enum_class, 1);
22880 if (ENUM_IS_OPAQUE (type) && TYPE_SIZE (type))
22881 add_AT_flag (type_die, DW_AT_declaration, 1);
22882 if (!dwarf_strict)
22883 add_AT_unsigned (type_die, DW_AT_encoding,
22884 TYPE_UNSIGNED (type)
22885 ? DW_ATE_unsigned
22886 : DW_ATE_signed);
22887 if (reverse)
22888 add_AT_unsigned (type_die, DW_AT_endianity,
22889 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
22891 else if (! TYPE_SIZE (type) || ENUM_IS_OPAQUE (type))
22892 return type_die;
22893 else
22894 remove_AT (type_die, DW_AT_declaration);
22896 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
22897 given enum type is incomplete, do not generate the DW_AT_byte_size
22898 attribute or the DW_AT_element_list attribute. */
22899 if (TYPE_SIZE (type))
22901 tree link;
22903 if (!ENUM_IS_OPAQUE (type))
22904 TREE_ASM_WRITTEN (type) = 1;
22905 if (!orig_type_die || !get_AT (type_die, DW_AT_byte_size))
22906 add_byte_size_attribute (type_die, type);
22907 if (!orig_type_die || !get_AT (type_die, DW_AT_alignment))
22908 add_alignment_attribute (type_die, type);
22909 if ((dwarf_version >= 3 || !dwarf_strict)
22910 && (!orig_type_die || !get_AT (type_die, DW_AT_type)))
22912 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
22913 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
22914 context_die);
22916 if (TYPE_STUB_DECL (type) != NULL_TREE)
22918 if (!orig_type_die || !get_AT (type_die, DW_AT_decl_file))
22919 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
22920 if (!orig_type_die || !get_AT (type_die, DW_AT_accessibility))
22921 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
22924 /* If the first reference to this type was as the return type of an
22925 inline function, then it may not have a parent. Fix this now. */
22926 if (type_die->die_parent == NULL)
22927 add_child_die (scope_die_for (type, context_die), type_die);
22929 for (link = TYPE_VALUES (type);
22930 link != NULL; link = TREE_CHAIN (link))
22932 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
22933 tree value = TREE_VALUE (link);
22935 if (DECL_P (value))
22936 equate_decl_number_to_die (value, enum_die);
22938 gcc_assert (!ENUM_IS_OPAQUE (type));
22939 add_name_attribute (enum_die,
22940 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
22942 if (TREE_CODE (value) == CONST_DECL)
22943 value = DECL_INITIAL (value);
22945 if (simple_type_size_in_bits (TREE_TYPE (value))
22946 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
22948 /* For constant forms created by add_AT_unsigned DWARF
22949 consumers (GDB, elfutils, etc.) always zero extend
22950 the value. Only when the actual value is negative
22951 do we need to use add_AT_int to generate a constant
22952 form that can represent negative values. */
22953 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
22954 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
22955 add_AT_unsigned (enum_die, DW_AT_const_value,
22956 (unsigned HOST_WIDE_INT) val);
22957 else
22958 add_AT_int (enum_die, DW_AT_const_value, val);
22960 else
22961 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
22962 that here. TODO: This should be re-worked to use correct
22963 signed/unsigned double tags for all cases. */
22964 add_AT_wide (enum_die, DW_AT_const_value, wi::to_wide (value));
22967 add_gnat_descriptive_type_attribute (type_die, type, context_die);
22968 if (TYPE_ARTIFICIAL (type)
22969 && (!orig_type_die || !get_AT (type_die, DW_AT_artificial)))
22970 add_AT_flag (type_die, DW_AT_artificial, 1);
22972 else
22973 add_AT_flag (type_die, DW_AT_declaration, 1);
22975 add_pubtype (type, type_die);
22977 return type_die;
22980 /* Generate a DIE to represent either a real live formal parameter decl or to
22981 represent just the type of some formal parameter position in some function
22982 type.
22984 Note that this routine is a bit unusual because its argument may be a
22985 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
22986 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
22987 node. If it's the former then this function is being called to output a
22988 DIE to represent a formal parameter object (or some inlining thereof). If
22989 it's the latter, then this function is only being called to output a
22990 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
22991 argument type of some subprogram type.
22992 If EMIT_NAME_P is true, name and source coordinate attributes
22993 are emitted. */
22995 static dw_die_ref
22996 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
22997 dw_die_ref context_die)
22999 tree node_or_origin = node ? node : origin;
23000 tree ultimate_origin;
23001 dw_die_ref parm_die = NULL;
23003 if (DECL_P (node_or_origin))
23005 parm_die = lookup_decl_die (node);
23007 /* If the contexts differ, we may not be talking about the same
23008 thing.
23009 ??? When in LTO the DIE parent is the "abstract" copy and the
23010 context_die is the specification "copy". */
23011 if (parm_die
23012 && parm_die->die_parent != context_die
23013 && (parm_die->die_parent->die_tag != DW_TAG_GNU_formal_parameter_pack
23014 || parm_die->die_parent->die_parent != context_die)
23015 && !in_lto_p)
23017 gcc_assert (!DECL_ABSTRACT_P (node));
23018 /* This can happen when creating a concrete instance, in
23019 which case we need to create a new DIE that will get
23020 annotated with DW_AT_abstract_origin. */
23021 parm_die = NULL;
23024 if (parm_die && parm_die->die_parent == NULL)
23026 /* Check that parm_die already has the right attributes that
23027 we would have added below. If any attributes are
23028 missing, fall through to add them. */
23029 if (! DECL_ABSTRACT_P (node_or_origin)
23030 && !get_AT (parm_die, DW_AT_location)
23031 && !get_AT (parm_die, DW_AT_const_value))
23032 /* We are missing location info, and are about to add it. */
23034 else
23036 add_child_die (context_die, parm_die);
23037 return parm_die;
23042 /* If we have a previously generated DIE, use it, unless this is an
23043 concrete instance (origin != NULL), in which case we need a new
23044 DIE with a corresponding DW_AT_abstract_origin. */
23045 bool reusing_die;
23046 if (parm_die && origin == NULL)
23047 reusing_die = true;
23048 else
23050 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
23051 reusing_die = false;
23054 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
23056 case tcc_declaration:
23057 ultimate_origin = decl_ultimate_origin (node_or_origin);
23058 if (node || ultimate_origin)
23059 origin = ultimate_origin;
23061 if (reusing_die)
23062 goto add_location;
23064 if (origin != NULL)
23065 add_abstract_origin_attribute (parm_die, origin);
23066 else if (emit_name_p)
23067 add_name_and_src_coords_attributes (parm_die, node);
23068 if (origin == NULL
23069 || (! DECL_ABSTRACT_P (node_or_origin)
23070 && variably_modified_type_p (TREE_TYPE (node_or_origin),
23071 decl_function_context
23072 (node_or_origin))))
23074 tree type = TREE_TYPE (node_or_origin);
23075 if (decl_by_reference_p (node_or_origin))
23076 add_type_attribute (parm_die, TREE_TYPE (type),
23077 TYPE_UNQUALIFIED,
23078 false, context_die);
23079 else
23080 add_type_attribute (parm_die, type,
23081 decl_quals (node_or_origin),
23082 false, context_die);
23084 if (origin == NULL && DECL_ARTIFICIAL (node))
23085 add_AT_flag (parm_die, DW_AT_artificial, 1);
23086 add_location:
23087 if (node && node != origin)
23088 equate_decl_number_to_die (node, parm_die);
23089 if (! DECL_ABSTRACT_P (node_or_origin))
23090 add_location_or_const_value_attribute (parm_die, node_or_origin,
23091 node == NULL);
23093 break;
23095 case tcc_type:
23096 /* We were called with some kind of a ..._TYPE node. */
23097 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
23098 context_die);
23099 break;
23101 default:
23102 gcc_unreachable ();
23105 return parm_die;
23108 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
23109 children DW_TAG_formal_parameter DIEs representing the arguments of the
23110 parameter pack.
23112 PARM_PACK must be a function parameter pack.
23113 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
23114 must point to the subsequent arguments of the function PACK_ARG belongs to.
23115 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
23116 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
23117 following the last one for which a DIE was generated. */
23119 static dw_die_ref
23120 gen_formal_parameter_pack_die (tree parm_pack,
23121 tree pack_arg,
23122 dw_die_ref subr_die,
23123 tree *next_arg)
23125 tree arg;
23126 dw_die_ref parm_pack_die;
23128 gcc_assert (parm_pack
23129 && lang_hooks.function_parameter_pack_p (parm_pack)
23130 && subr_die);
23132 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
23133 add_src_coords_attributes (parm_pack_die, parm_pack);
23135 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
23137 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
23138 parm_pack))
23139 break;
23140 gen_formal_parameter_die (arg, NULL,
23141 false /* Don't emit name attribute. */,
23142 parm_pack_die);
23144 if (next_arg)
23145 *next_arg = arg;
23146 return parm_pack_die;
23149 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
23150 at the end of an (ANSI prototyped) formal parameters list. */
23152 static void
23153 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
23155 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
23158 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
23159 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
23160 parameters as specified in some function type specification (except for
23161 those which appear as part of a function *definition*). */
23163 static void
23164 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
23166 tree link;
23167 tree formal_type = NULL;
23168 tree first_parm_type;
23169 tree arg;
23171 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
23173 arg = DECL_ARGUMENTS (function_or_method_type);
23174 function_or_method_type = TREE_TYPE (function_or_method_type);
23176 else
23177 arg = NULL_TREE;
23179 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
23181 /* Make our first pass over the list of formal parameter types and output a
23182 DW_TAG_formal_parameter DIE for each one. */
23183 for (link = first_parm_type; link; )
23185 dw_die_ref parm_die;
23187 formal_type = TREE_VALUE (link);
23188 if (formal_type == void_type_node)
23189 break;
23191 /* Output a (nameless) DIE to represent the formal parameter itself. */
23192 parm_die = gen_formal_parameter_die (formal_type, NULL,
23193 true /* Emit name attribute. */,
23194 context_die);
23195 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
23196 && link == first_parm_type)
23198 add_AT_flag (parm_die, DW_AT_artificial, 1);
23199 if (dwarf_version >= 3 || !dwarf_strict)
23200 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
23202 else if (arg && DECL_ARTIFICIAL (arg))
23203 add_AT_flag (parm_die, DW_AT_artificial, 1);
23205 link = TREE_CHAIN (link);
23206 if (arg)
23207 arg = DECL_CHAIN (arg);
23210 /* If this function type has an ellipsis, add a
23211 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
23212 if (formal_type != void_type_node)
23213 gen_unspecified_parameters_die (function_or_method_type, context_die);
23215 /* Make our second (and final) pass over the list of formal parameter types
23216 and output DIEs to represent those types (as necessary). */
23217 for (link = TYPE_ARG_TYPES (function_or_method_type);
23218 link && TREE_VALUE (link);
23219 link = TREE_CHAIN (link))
23220 gen_type_die (TREE_VALUE (link), context_die);
23223 /* We want to generate the DIE for TYPE so that we can generate the
23224 die for MEMBER, which has been defined; we will need to refer back
23225 to the member declaration nested within TYPE. If we're trying to
23226 generate minimal debug info for TYPE, processing TYPE won't do the
23227 trick; we need to attach the member declaration by hand. */
23229 static void
23230 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
23232 gen_type_die (type, context_die);
23234 /* If we're trying to avoid duplicate debug info, we may not have
23235 emitted the member decl for this function. Emit it now. */
23236 if (TYPE_STUB_DECL (type)
23237 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
23238 && ! lookup_decl_die (member))
23240 dw_die_ref type_die;
23241 gcc_assert (!decl_ultimate_origin (member));
23243 type_die = lookup_type_die_strip_naming_typedef (type);
23244 if (TREE_CODE (member) == FUNCTION_DECL)
23245 gen_subprogram_die (member, type_die);
23246 else if (TREE_CODE (member) == FIELD_DECL)
23248 /* Ignore the nameless fields that are used to skip bits but handle
23249 C++ anonymous unions and structs. */
23250 if (DECL_NAME (member) != NULL_TREE
23251 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
23252 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
23254 struct vlr_context vlr_ctx = {
23255 DECL_CONTEXT (member), /* struct_type */
23256 NULL_TREE /* variant_part_offset */
23258 gen_type_die (member_declared_type (member), type_die);
23259 gen_field_die (member, &vlr_ctx, type_die);
23262 else
23263 gen_variable_die (member, NULL_TREE, type_die);
23267 /* Forward declare these functions, because they are mutually recursive
23268 with their set_block_* pairing functions. */
23269 static void set_decl_origin_self (tree);
23271 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
23272 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
23273 that it points to the node itself, thus indicating that the node is its
23274 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
23275 the given node is NULL, recursively descend the decl/block tree which
23276 it is the root of, and for each other ..._DECL or BLOCK node contained
23277 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
23278 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
23279 values to point to themselves. */
23281 static void
23282 set_block_origin_self (tree stmt)
23284 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
23286 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
23289 tree local_decl;
23291 for (local_decl = BLOCK_VARS (stmt);
23292 local_decl != NULL_TREE;
23293 local_decl = DECL_CHAIN (local_decl))
23294 /* Do not recurse on nested functions since the inlining status
23295 of parent and child can be different as per the DWARF spec. */
23296 if (TREE_CODE (local_decl) != FUNCTION_DECL
23297 && !DECL_EXTERNAL (local_decl))
23298 set_decl_origin_self (local_decl);
23302 tree subblock;
23304 for (subblock = BLOCK_SUBBLOCKS (stmt);
23305 subblock != NULL_TREE;
23306 subblock = BLOCK_CHAIN (subblock))
23307 set_block_origin_self (subblock); /* Recurse. */
23312 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
23313 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
23314 node to so that it points to the node itself, thus indicating that the
23315 node represents its own (abstract) origin. Additionally, if the
23316 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
23317 the decl/block tree of which the given node is the root of, and for
23318 each other ..._DECL or BLOCK node contained therein whose
23319 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
23320 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
23321 point to themselves. */
23323 static void
23324 set_decl_origin_self (tree decl)
23326 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
23328 DECL_ABSTRACT_ORIGIN (decl) = decl;
23329 if (TREE_CODE (decl) == FUNCTION_DECL)
23331 tree arg;
23333 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
23334 DECL_ABSTRACT_ORIGIN (arg) = arg;
23335 if (DECL_INITIAL (decl) != NULL_TREE
23336 && DECL_INITIAL (decl) != error_mark_node)
23337 set_block_origin_self (DECL_INITIAL (decl));
23342 /* Mark the early DIE for DECL as the abstract instance. */
23344 static void
23345 dwarf2out_abstract_function (tree decl)
23347 dw_die_ref old_die;
23349 /* Make sure we have the actual abstract inline, not a clone. */
23350 decl = DECL_ORIGIN (decl);
23352 if (DECL_IGNORED_P (decl))
23353 return;
23355 /* In LTO we're all set. We already created abstract instances
23356 early and we want to avoid creating a concrete instance of that
23357 if we don't output it. */
23358 if (in_lto_p)
23359 return;
23361 old_die = lookup_decl_die (decl);
23362 gcc_assert (old_die != NULL);
23363 if (get_AT (old_die, DW_AT_inline))
23364 /* We've already generated the abstract instance. */
23365 return;
23367 /* Go ahead and put DW_AT_inline on the DIE. */
23368 if (DECL_DECLARED_INLINE_P (decl))
23370 if (cgraph_function_possibly_inlined_p (decl))
23371 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_inlined);
23372 else
23373 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_not_inlined);
23375 else
23377 if (cgraph_function_possibly_inlined_p (decl))
23378 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_inlined);
23379 else
23380 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_not_inlined);
23383 if (DECL_DECLARED_INLINE_P (decl)
23384 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
23385 add_AT_flag (old_die, DW_AT_artificial, 1);
23387 set_decl_origin_self (decl);
23390 /* Helper function of premark_used_types() which gets called through
23391 htab_traverse.
23393 Marks the DIE of a given type in *SLOT as perennial, so it never gets
23394 marked as unused by prune_unused_types. */
23396 bool
23397 premark_used_types_helper (tree const &type, void *)
23399 dw_die_ref die;
23401 die = lookup_type_die (type);
23402 if (die != NULL)
23403 die->die_perennial_p = 1;
23404 return true;
23407 /* Helper function of premark_types_used_by_global_vars which gets called
23408 through htab_traverse.
23410 Marks the DIE of a given type in *SLOT as perennial, so it never gets
23411 marked as unused by prune_unused_types. The DIE of the type is marked
23412 only if the global variable using the type will actually be emitted. */
23415 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
23416 void *)
23418 struct types_used_by_vars_entry *entry;
23419 dw_die_ref die;
23421 entry = (struct types_used_by_vars_entry *) *slot;
23422 gcc_assert (entry->type != NULL
23423 && entry->var_decl != NULL);
23424 die = lookup_type_die (entry->type);
23425 if (die)
23427 /* Ask cgraph if the global variable really is to be emitted.
23428 If yes, then we'll keep the DIE of ENTRY->TYPE. */
23429 varpool_node *node = varpool_node::get (entry->var_decl);
23430 if (node && node->definition)
23432 die->die_perennial_p = 1;
23433 /* Keep the parent DIEs as well. */
23434 while ((die = die->die_parent) && die->die_perennial_p == 0)
23435 die->die_perennial_p = 1;
23438 return 1;
23441 /* Mark all members of used_types_hash as perennial. */
23443 static void
23444 premark_used_types (struct function *fun)
23446 if (fun && fun->used_types_hash)
23447 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
23450 /* Mark all members of types_used_by_vars_entry as perennial. */
23452 static void
23453 premark_types_used_by_global_vars (void)
23455 if (types_used_by_vars_hash)
23456 types_used_by_vars_hash
23457 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
23460 /* Mark all variables used by the symtab as perennial. */
23462 static void
23463 premark_used_variables (void)
23465 /* Mark DIEs in the symtab as used. */
23466 varpool_node *var;
23467 FOR_EACH_VARIABLE (var)
23469 dw_die_ref die = lookup_decl_die (var->decl);
23470 if (die)
23471 die->die_perennial_p = 1;
23475 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
23476 for CA_LOC call arg loc node. */
23478 static dw_die_ref
23479 gen_call_site_die (tree decl, dw_die_ref subr_die,
23480 struct call_arg_loc_node *ca_loc)
23482 dw_die_ref stmt_die = NULL, die;
23483 tree block = ca_loc->block;
23485 while (block
23486 && block != DECL_INITIAL (decl)
23487 && TREE_CODE (block) == BLOCK)
23489 stmt_die = lookup_block_die (block);
23490 if (stmt_die)
23491 break;
23492 block = BLOCK_SUPERCONTEXT (block);
23494 if (stmt_die == NULL)
23495 stmt_die = subr_die;
23496 die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
23497 add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc), ca_loc->label);
23498 if (ca_loc->tail_call_p)
23499 add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
23500 if (ca_loc->symbol_ref)
23502 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
23503 if (tdie)
23504 add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
23505 else
23506 add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
23507 false);
23509 return die;
23512 /* Generate a DIE to represent a declared function (either file-scope or
23513 block-local). */
23515 static void
23516 gen_subprogram_die (tree decl, dw_die_ref context_die)
23518 tree origin = decl_ultimate_origin (decl);
23519 dw_die_ref subr_die;
23520 dw_die_ref old_die = lookup_decl_die (decl);
23521 bool old_die_had_no_children = false;
23523 /* This function gets called multiple times for different stages of
23524 the debug process. For example, for func() in this code:
23526 namespace S
23528 void func() { ... }
23531 ...we get called 4 times. Twice in early debug and twice in
23532 late debug:
23534 Early debug
23535 -----------
23537 1. Once while generating func() within the namespace. This is
23538 the declaration. The declaration bit below is set, as the
23539 context is the namespace.
23541 A new DIE will be generated with DW_AT_declaration set.
23543 2. Once for func() itself. This is the specification. The
23544 declaration bit below is clear as the context is the CU.
23546 We will use the cached DIE from (1) to create a new DIE with
23547 DW_AT_specification pointing to the declaration in (1).
23549 Late debug via rest_of_handle_final()
23550 -------------------------------------
23552 3. Once generating func() within the namespace. This is also the
23553 declaration, as in (1), but this time we will early exit below
23554 as we have a cached DIE and a declaration needs no additional
23555 annotations (no locations), as the source declaration line
23556 info is enough.
23558 4. Once for func() itself. As in (2), this is the specification,
23559 but this time we will re-use the cached DIE, and just annotate
23560 it with the location information that should now be available.
23562 For something without namespaces, but with abstract instances, we
23563 are also called a multiple times:
23565 class Base
23567 public:
23568 Base (); // constructor declaration (1)
23571 Base::Base () { } // constructor specification (2)
23573 Early debug
23574 -----------
23576 1. Once for the Base() constructor by virtue of it being a
23577 member of the Base class. This is done via
23578 rest_of_type_compilation.
23580 This is a declaration, so a new DIE will be created with
23581 DW_AT_declaration.
23583 2. Once for the Base() constructor definition, but this time
23584 while generating the abstract instance of the base
23585 constructor (__base_ctor) which is being generated via early
23586 debug of reachable functions.
23588 Even though we have a cached version of the declaration (1),
23589 we will create a DW_AT_specification of the declaration DIE
23590 in (1).
23592 3. Once for the __base_ctor itself, but this time, we generate
23593 an DW_AT_abstract_origin version of the DW_AT_specification in
23594 (2).
23596 Late debug via rest_of_handle_final
23597 -----------------------------------
23599 4. One final time for the __base_ctor (which will have a cached
23600 DIE with DW_AT_abstract_origin created in (3). This time,
23601 we will just annotate the location information now
23602 available.
23604 int declaration = (current_function_decl != decl
23605 || (!DECL_INITIAL (decl) && !origin)
23606 || class_or_namespace_scope_p (context_die));
23608 /* A declaration that has been previously dumped needs no
23609 additional information. */
23610 if (old_die && declaration)
23611 return;
23613 if (in_lto_p && old_die && old_die->die_child == NULL)
23614 old_die_had_no_children = true;
23616 /* Now that the C++ front end lazily declares artificial member fns, we
23617 might need to retrofit the declaration into its class. */
23618 if (!declaration && !origin && !old_die
23619 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
23620 && !class_or_namespace_scope_p (context_die)
23621 && debug_info_level > DINFO_LEVEL_TERSE)
23622 old_die = force_decl_die (decl);
23624 /* A concrete instance, tag a new DIE with DW_AT_abstract_origin. */
23625 if (origin != NULL)
23627 gcc_assert (!declaration || local_scope_p (context_die));
23629 /* Fixup die_parent for the abstract instance of a nested
23630 inline function. */
23631 if (old_die && old_die->die_parent == NULL)
23632 add_child_die (context_die, old_die);
23634 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
23636 /* If we have a DW_AT_abstract_origin we have a working
23637 cached version. */
23638 subr_die = old_die;
23640 else
23642 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
23643 add_abstract_origin_attribute (subr_die, origin);
23644 /* This is where the actual code for a cloned function is.
23645 Let's emit linkage name attribute for it. This helps
23646 debuggers to e.g, set breakpoints into
23647 constructors/destructors when the user asks "break
23648 K::K". */
23649 add_linkage_name (subr_die, decl);
23652 /* A cached copy, possibly from early dwarf generation. Reuse as
23653 much as possible. */
23654 else if (old_die)
23656 if (!get_AT_flag (old_die, DW_AT_declaration)
23657 /* We can have a normal definition following an inline one in the
23658 case of redefinition of GNU C extern inlines.
23659 It seems reasonable to use AT_specification in this case. */
23660 && !get_AT (old_die, DW_AT_inline))
23662 /* Detect and ignore this case, where we are trying to output
23663 something we have already output. */
23664 if (get_AT (old_die, DW_AT_low_pc)
23665 || get_AT (old_die, DW_AT_ranges))
23666 return;
23668 /* If we have no location information, this must be a
23669 partially generated DIE from early dwarf generation.
23670 Fall through and generate it. */
23673 /* If the definition comes from the same place as the declaration,
23674 maybe use the old DIE. We always want the DIE for this function
23675 that has the *_pc attributes to be under comp_unit_die so the
23676 debugger can find it. We also need to do this for abstract
23677 instances of inlines, since the spec requires the out-of-line copy
23678 to have the same parent. For local class methods, this doesn't
23679 apply; we just use the old DIE. */
23680 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
23681 struct dwarf_file_data * file_index = lookup_filename (s.file);
23682 if (((is_unit_die (old_die->die_parent)
23683 /* This condition fixes the inconsistency/ICE with the
23684 following Fortran test (or some derivative thereof) while
23685 building libgfortran:
23687 module some_m
23688 contains
23689 logical function funky (FLAG)
23690 funky = .true.
23691 end function
23692 end module
23694 || (old_die->die_parent
23695 && old_die->die_parent->die_tag == DW_TAG_module)
23696 || local_scope_p (old_die->die_parent)
23697 || context_die == NULL)
23698 && (DECL_ARTIFICIAL (decl)
23699 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
23700 && (get_AT_unsigned (old_die, DW_AT_decl_line)
23701 == (unsigned) s.line)
23702 && (!debug_column_info
23703 || s.column == 0
23704 || (get_AT_unsigned (old_die, DW_AT_decl_column)
23705 == (unsigned) s.column)))))
23706 /* With LTO if there's an abstract instance for
23707 the old DIE, this is a concrete instance and
23708 thus re-use the DIE. */
23709 || get_AT (old_die, DW_AT_abstract_origin))
23711 subr_die = old_die;
23713 /* Clear out the declaration attribute, but leave the
23714 parameters so they can be augmented with location
23715 information later. Unless this was a declaration, in
23716 which case, wipe out the nameless parameters and recreate
23717 them further down. */
23718 if (remove_AT (subr_die, DW_AT_declaration))
23721 remove_AT (subr_die, DW_AT_object_pointer);
23722 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
23725 /* Make a specification pointing to the previously built
23726 declaration. */
23727 else
23729 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
23730 add_AT_specification (subr_die, old_die);
23731 add_pubname (decl, subr_die);
23732 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
23733 add_AT_file (subr_die, DW_AT_decl_file, file_index);
23734 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
23735 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
23736 if (debug_column_info
23737 && s.column
23738 && (get_AT_unsigned (old_die, DW_AT_decl_column)
23739 != (unsigned) s.column))
23740 add_AT_unsigned (subr_die, DW_AT_decl_column, s.column);
23742 /* If the prototype had an 'auto' or 'decltype(auto)' in
23743 the return type, emit the real type on the definition die. */
23744 if (is_cxx () && debug_info_level > DINFO_LEVEL_TERSE)
23746 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
23747 while (die
23748 && (die->die_tag == DW_TAG_reference_type
23749 || die->die_tag == DW_TAG_rvalue_reference_type
23750 || die->die_tag == DW_TAG_pointer_type
23751 || die->die_tag == DW_TAG_const_type
23752 || die->die_tag == DW_TAG_volatile_type
23753 || die->die_tag == DW_TAG_restrict_type
23754 || die->die_tag == DW_TAG_array_type
23755 || die->die_tag == DW_TAG_ptr_to_member_type
23756 || die->die_tag == DW_TAG_subroutine_type))
23757 die = get_AT_ref (die, DW_AT_type);
23758 if (die == auto_die || die == decltype_auto_die)
23759 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
23760 TYPE_UNQUALIFIED, false, context_die);
23763 /* When we process the method declaration, we haven't seen
23764 the out-of-class defaulted definition yet, so we have to
23765 recheck now. */
23766 if ((dwarf_version >= 5 || ! dwarf_strict)
23767 && !get_AT (subr_die, DW_AT_defaulted))
23769 int defaulted
23770 = lang_hooks.decls.decl_dwarf_attribute (decl,
23771 DW_AT_defaulted);
23772 if (defaulted != -1)
23774 /* Other values must have been handled before. */
23775 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
23776 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
23781 /* Create a fresh DIE for anything else. */
23782 else
23784 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
23786 if (TREE_PUBLIC (decl))
23787 add_AT_flag (subr_die, DW_AT_external, 1);
23789 add_name_and_src_coords_attributes (subr_die, decl);
23790 add_pubname (decl, subr_die);
23791 if (debug_info_level > DINFO_LEVEL_TERSE)
23793 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
23794 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
23795 TYPE_UNQUALIFIED, false, context_die);
23798 add_pure_or_virtual_attribute (subr_die, decl);
23799 if (DECL_ARTIFICIAL (decl))
23800 add_AT_flag (subr_die, DW_AT_artificial, 1);
23802 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
23803 add_AT_flag (subr_die, DW_AT_noreturn, 1);
23805 add_alignment_attribute (subr_die, decl);
23807 add_accessibility_attribute (subr_die, decl);
23810 /* Unless we have an existing non-declaration DIE, equate the new
23811 DIE. */
23812 if (!old_die || is_declaration_die (old_die))
23813 equate_decl_number_to_die (decl, subr_die);
23815 if (declaration)
23817 if (!old_die || !get_AT (old_die, DW_AT_inline))
23819 add_AT_flag (subr_die, DW_AT_declaration, 1);
23821 /* If this is an explicit function declaration then generate
23822 a DW_AT_explicit attribute. */
23823 if ((dwarf_version >= 3 || !dwarf_strict)
23824 && lang_hooks.decls.decl_dwarf_attribute (decl,
23825 DW_AT_explicit) == 1)
23826 add_AT_flag (subr_die, DW_AT_explicit, 1);
23828 /* If this is a C++11 deleted special function member then generate
23829 a DW_AT_deleted attribute. */
23830 if ((dwarf_version >= 5 || !dwarf_strict)
23831 && lang_hooks.decls.decl_dwarf_attribute (decl,
23832 DW_AT_deleted) == 1)
23833 add_AT_flag (subr_die, DW_AT_deleted, 1);
23835 /* If this is a C++11 defaulted special function member then
23836 generate a DW_AT_defaulted attribute. */
23837 if (dwarf_version >= 5 || !dwarf_strict)
23839 int defaulted
23840 = lang_hooks.decls.decl_dwarf_attribute (decl,
23841 DW_AT_defaulted);
23842 if (defaulted != -1)
23843 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
23846 /* If this is a C++11 non-static member function with & ref-qualifier
23847 then generate a DW_AT_reference attribute. */
23848 if ((dwarf_version >= 5 || !dwarf_strict)
23849 && lang_hooks.decls.decl_dwarf_attribute (decl,
23850 DW_AT_reference) == 1)
23851 add_AT_flag (subr_die, DW_AT_reference, 1);
23853 /* If this is a C++11 non-static member function with &&
23854 ref-qualifier then generate a DW_AT_reference attribute. */
23855 if ((dwarf_version >= 5 || !dwarf_strict)
23856 && lang_hooks.decls.decl_dwarf_attribute (decl,
23857 DW_AT_rvalue_reference)
23858 == 1)
23859 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
23862 /* For non DECL_EXTERNALs, if range information is available, fill
23863 the DIE with it. */
23864 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
23866 HOST_WIDE_INT cfa_fb_offset;
23868 struct function *fun = DECL_STRUCT_FUNCTION (decl);
23870 if (!crtl->has_bb_partition)
23872 dw_fde_ref fde = fun->fde;
23873 if (fde->dw_fde_begin)
23875 /* We have already generated the labels. */
23876 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
23877 fde->dw_fde_end, false);
23879 else
23881 /* Create start/end labels and add the range. */
23882 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
23883 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
23884 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
23885 current_function_funcdef_no);
23886 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
23887 current_function_funcdef_no);
23888 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
23889 false);
23892 #if VMS_DEBUGGING_INFO
23893 /* HP OpenVMS Industry Standard 64: DWARF Extensions
23894 Section 2.3 Prologue and Epilogue Attributes:
23895 When a breakpoint is set on entry to a function, it is generally
23896 desirable for execution to be suspended, not on the very first
23897 instruction of the function, but rather at a point after the
23898 function's frame has been set up, after any language defined local
23899 declaration processing has been completed, and before execution of
23900 the first statement of the function begins. Debuggers generally
23901 cannot properly determine where this point is. Similarly for a
23902 breakpoint set on exit from a function. The prologue and epilogue
23903 attributes allow a compiler to communicate the location(s) to use. */
23906 if (fde->dw_fde_vms_end_prologue)
23907 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
23908 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
23910 if (fde->dw_fde_vms_begin_epilogue)
23911 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
23912 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
23914 #endif
23917 else
23919 /* Generate pubnames entries for the split function code ranges. */
23920 dw_fde_ref fde = fun->fde;
23922 if (fde->dw_fde_second_begin)
23924 if (dwarf_version >= 3 || !dwarf_strict)
23926 /* We should use ranges for non-contiguous code section
23927 addresses. Use the actual code range for the initial
23928 section, since the HOT/COLD labels might precede an
23929 alignment offset. */
23930 bool range_list_added = false;
23931 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
23932 fde->dw_fde_end, &range_list_added,
23933 false);
23934 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
23935 fde->dw_fde_second_end,
23936 &range_list_added, false);
23937 if (range_list_added)
23938 add_ranges (NULL);
23940 else
23942 /* There is no real support in DW2 for this .. so we make
23943 a work-around. First, emit the pub name for the segment
23944 containing the function label. Then make and emit a
23945 simplified subprogram DIE for the second segment with the
23946 name pre-fixed by __hot/cold_sect_of_. We use the same
23947 linkage name for the second die so that gdb will find both
23948 sections when given "b foo". */
23949 const char *name = NULL;
23950 tree decl_name = DECL_NAME (decl);
23951 dw_die_ref seg_die;
23953 /* Do the 'primary' section. */
23954 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
23955 fde->dw_fde_end, false);
23957 /* Build a minimal DIE for the secondary section. */
23958 seg_die = new_die (DW_TAG_subprogram,
23959 subr_die->die_parent, decl);
23961 if (TREE_PUBLIC (decl))
23962 add_AT_flag (seg_die, DW_AT_external, 1);
23964 if (decl_name != NULL
23965 && IDENTIFIER_POINTER (decl_name) != NULL)
23967 name = dwarf2_name (decl, 1);
23968 if (! DECL_ARTIFICIAL (decl))
23969 add_src_coords_attributes (seg_die, decl);
23971 add_linkage_name (seg_die, decl);
23973 gcc_assert (name != NULL);
23974 add_pure_or_virtual_attribute (seg_die, decl);
23975 if (DECL_ARTIFICIAL (decl))
23976 add_AT_flag (seg_die, DW_AT_artificial, 1);
23978 name = concat ("__second_sect_of_", name, NULL);
23979 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
23980 fde->dw_fde_second_end, false);
23981 add_name_attribute (seg_die, name);
23982 if (want_pubnames ())
23983 add_pubname_string (name, seg_die);
23986 else
23987 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
23988 false);
23991 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
23993 /* We define the "frame base" as the function's CFA. This is more
23994 convenient for several reasons: (1) It's stable across the prologue
23995 and epilogue, which makes it better than just a frame pointer,
23996 (2) With dwarf3, there exists a one-byte encoding that allows us
23997 to reference the .debug_frame data by proxy, but failing that,
23998 (3) We can at least reuse the code inspection and interpretation
23999 code that determines the CFA position at various points in the
24000 function. */
24001 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
24003 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
24004 add_AT_loc (subr_die, DW_AT_frame_base, op);
24006 else
24008 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
24009 if (list->dw_loc_next)
24010 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
24011 else
24012 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
24015 /* Compute a displacement from the "steady-state frame pointer" to
24016 the CFA. The former is what all stack slots and argument slots
24017 will reference in the rtl; the latter is what we've told the
24018 debugger about. We'll need to adjust all frame_base references
24019 by this displacement. */
24020 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
24022 if (fun->static_chain_decl)
24024 /* DWARF requires here a location expression that computes the
24025 address of the enclosing subprogram's frame base. The machinery
24026 in tree-nested.cc is supposed to store this specific address in the
24027 last field of the FRAME record. */
24028 const tree frame_type
24029 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
24030 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
24032 tree fb_expr
24033 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
24034 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
24035 fb_expr, fb_decl, NULL_TREE);
24037 add_AT_location_description (subr_die, DW_AT_static_link,
24038 loc_list_from_tree (fb_expr, 0, NULL));
24041 resolve_variable_values ();
24044 /* Generate child dies for template parameters. */
24045 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
24046 gen_generic_params_dies (decl);
24048 /* Now output descriptions of the arguments for this function. This gets
24049 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
24050 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
24051 `...' at the end of the formal parameter list. In order to find out if
24052 there was a trailing ellipsis or not, we must instead look at the type
24053 associated with the FUNCTION_DECL. This will be a node of type
24054 FUNCTION_TYPE. If the chain of type nodes hanging off of this
24055 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
24056 an ellipsis at the end. */
24058 /* In the case where we are describing a mere function declaration, all we
24059 need to do here (and all we *can* do here) is to describe the *types* of
24060 its formal parameters. */
24061 if (debug_info_level <= DINFO_LEVEL_TERSE)
24063 else if (declaration)
24064 gen_formal_types_die (decl, subr_die);
24065 else
24067 /* Generate DIEs to represent all known formal parameters. */
24068 tree parm = DECL_ARGUMENTS (decl);
24069 tree generic_decl = early_dwarf
24070 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
24071 tree generic_decl_parm = generic_decl
24072 ? DECL_ARGUMENTS (generic_decl)
24073 : NULL;
24075 /* Now we want to walk the list of parameters of the function and
24076 emit their relevant DIEs.
24078 We consider the case of DECL being an instance of a generic function
24079 as well as it being a normal function.
24081 If DECL is an instance of a generic function we walk the
24082 parameters of the generic function declaration _and_ the parameters of
24083 DECL itself. This is useful because we want to emit specific DIEs for
24084 function parameter packs and those are declared as part of the
24085 generic function declaration. In that particular case,
24086 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
24087 That DIE has children DIEs representing the set of arguments
24088 of the pack. Note that the set of pack arguments can be empty.
24089 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
24090 children DIE.
24092 Otherwise, we just consider the parameters of DECL. */
24093 while (generic_decl_parm || parm)
24095 if (generic_decl_parm
24096 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
24097 gen_formal_parameter_pack_die (generic_decl_parm,
24098 parm, subr_die,
24099 &parm);
24100 else if (parm)
24102 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
24104 if (early_dwarf
24105 && parm == DECL_ARGUMENTS (decl)
24106 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
24107 && parm_die
24108 && (dwarf_version >= 3 || !dwarf_strict))
24109 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
24111 parm = DECL_CHAIN (parm);
24114 if (generic_decl_parm)
24115 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
24118 /* Decide whether we need an unspecified_parameters DIE at the end.
24119 There are 2 more cases to do this for: 1) the ansi ... declaration -
24120 this is detectable when the end of the arg list is not a
24121 void_type_node 2) an unprototyped function declaration (not a
24122 definition). This just means that we have no info about the
24123 parameters at all. */
24124 if (early_dwarf)
24126 if (prototype_p (TREE_TYPE (decl)))
24128 /* This is the prototyped case, check for.... */
24129 if (stdarg_p (TREE_TYPE (decl)))
24130 gen_unspecified_parameters_die (decl, subr_die);
24132 else if (DECL_INITIAL (decl) == NULL_TREE)
24133 gen_unspecified_parameters_die (decl, subr_die);
24135 else if ((subr_die != old_die || old_die_had_no_children)
24136 && prototype_p (TREE_TYPE (decl))
24137 && stdarg_p (TREE_TYPE (decl)))
24138 gen_unspecified_parameters_die (decl, subr_die);
24141 if (subr_die != old_die)
24142 /* Add the calling convention attribute if requested. */
24143 add_calling_convention_attribute (subr_die, decl);
24145 /* Output Dwarf info for all of the stuff within the body of the function
24146 (if it has one - it may be just a declaration).
24148 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
24149 a function. This BLOCK actually represents the outermost binding contour
24150 for the function, i.e. the contour in which the function's formal
24151 parameters and labels get declared. Curiously, it appears that the front
24152 end doesn't actually put the PARM_DECL nodes for the current function onto
24153 the BLOCK_VARS list for this outer scope, but are strung off of the
24154 DECL_ARGUMENTS list for the function instead.
24156 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
24157 the LABEL_DECL nodes for the function however, and we output DWARF info
24158 for those in decls_for_scope. Just within the `outer_scope' there will be
24159 a BLOCK node representing the function's outermost pair of curly braces,
24160 and any blocks used for the base and member initializers of a C++
24161 constructor function. */
24162 tree outer_scope = DECL_INITIAL (decl);
24163 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
24165 int call_site_note_count = 0;
24166 int tail_call_site_note_count = 0;
24168 /* Emit a DW_TAG_variable DIE for a named return value. */
24169 if (DECL_NAME (DECL_RESULT (decl)))
24170 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
24172 /* The first time through decls_for_scope we will generate the
24173 DIEs for the locals. The second time, we fill in the
24174 location info. */
24175 decls_for_scope (outer_scope, subr_die);
24177 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
24179 struct call_arg_loc_node *ca_loc;
24180 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
24182 dw_die_ref die = NULL;
24183 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
24184 rtx arg, next_arg;
24185 tree arg_decl = NULL_TREE;
24187 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
24188 ? XEXP (ca_loc->call_arg_loc_note, 0)
24189 : NULL_RTX);
24190 arg; arg = next_arg)
24192 dw_loc_descr_ref reg, val;
24193 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
24194 dw_die_ref cdie, tdie = NULL;
24196 next_arg = XEXP (arg, 1);
24197 if (REG_P (XEXP (XEXP (arg, 0), 0))
24198 && next_arg
24199 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
24200 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
24201 && REGNO (XEXP (XEXP (arg, 0), 0))
24202 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
24203 next_arg = XEXP (next_arg, 1);
24204 if (mode == VOIDmode)
24206 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
24207 if (mode == VOIDmode)
24208 mode = GET_MODE (XEXP (arg, 0));
24210 if (mode == VOIDmode || mode == BLKmode)
24211 continue;
24212 /* Get dynamic information about call target only if we
24213 have no static information: we cannot generate both
24214 DW_AT_call_origin and DW_AT_call_target
24215 attributes. */
24216 if (ca_loc->symbol_ref == NULL_RTX)
24218 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
24220 tloc = XEXP (XEXP (arg, 0), 1);
24221 continue;
24223 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
24224 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
24226 tlocc = XEXP (XEXP (arg, 0), 1);
24227 continue;
24230 reg = NULL;
24231 if (REG_P (XEXP (XEXP (arg, 0), 0)))
24232 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
24233 VAR_INIT_STATUS_INITIALIZED);
24234 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
24236 rtx mem = XEXP (XEXP (arg, 0), 0);
24237 reg = mem_loc_descriptor (XEXP (mem, 0),
24238 get_address_mode (mem),
24239 GET_MODE (mem),
24240 VAR_INIT_STATUS_INITIALIZED);
24242 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
24243 == DEBUG_PARAMETER_REF)
24245 tree tdecl
24246 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
24247 tdie = lookup_decl_die (tdecl);
24248 if (tdie == NULL)
24249 continue;
24250 arg_decl = tdecl;
24252 else
24253 continue;
24254 if (reg == NULL
24255 && GET_CODE (XEXP (XEXP (arg, 0), 0))
24256 != DEBUG_PARAMETER_REF)
24257 continue;
24258 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
24259 VOIDmode,
24260 VAR_INIT_STATUS_INITIALIZED);
24261 if (val == NULL)
24262 continue;
24263 if (die == NULL)
24264 die = gen_call_site_die (decl, subr_die, ca_loc);
24265 cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
24266 NULL_TREE);
24267 add_desc_attribute (cdie, arg_decl);
24268 if (reg != NULL)
24269 add_AT_loc (cdie, DW_AT_location, reg);
24270 else if (tdie != NULL)
24271 add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
24272 tdie);
24273 add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
24274 if (next_arg != XEXP (arg, 1))
24276 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
24277 if (mode == VOIDmode)
24278 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
24279 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
24280 0), 1),
24281 mode, VOIDmode,
24282 VAR_INIT_STATUS_INITIALIZED);
24283 if (val != NULL)
24284 add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
24285 val);
24288 if (die == NULL
24289 && (ca_loc->symbol_ref || tloc))
24290 die = gen_call_site_die (decl, subr_die, ca_loc);
24291 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
24293 dw_loc_descr_ref tval = NULL;
24295 if (tloc != NULL_RTX)
24296 tval = mem_loc_descriptor (tloc,
24297 GET_MODE (tloc) == VOIDmode
24298 ? Pmode : GET_MODE (tloc),
24299 VOIDmode,
24300 VAR_INIT_STATUS_INITIALIZED);
24301 if (tval)
24302 add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
24303 else if (tlocc != NULL_RTX)
24305 tval = mem_loc_descriptor (tlocc,
24306 GET_MODE (tlocc) == VOIDmode
24307 ? Pmode : GET_MODE (tlocc),
24308 VOIDmode,
24309 VAR_INIT_STATUS_INITIALIZED);
24310 if (tval)
24311 add_AT_loc (die,
24312 dwarf_AT (DW_AT_call_target_clobbered),
24313 tval);
24316 if (die != NULL)
24318 call_site_note_count++;
24319 if (ca_loc->tail_call_p)
24320 tail_call_site_note_count++;
24324 call_arg_locations = NULL;
24325 call_arg_loc_last = NULL;
24326 if (tail_call_site_count >= 0
24327 && tail_call_site_count == tail_call_site_note_count
24328 && (!dwarf_strict || dwarf_version >= 5))
24330 if (call_site_count >= 0
24331 && call_site_count == call_site_note_count)
24332 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
24333 else
24334 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
24336 call_site_count = -1;
24337 tail_call_site_count = -1;
24340 /* Mark used types after we have created DIEs for the functions scopes. */
24341 premark_used_types (DECL_STRUCT_FUNCTION (decl));
24344 /* Returns a hash value for X (which really is a die_struct). */
24346 hashval_t
24347 block_die_hasher::hash (die_struct *d)
24349 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
24352 /* Return true if decl_id and die_parent of die_struct X is the same
24353 as decl_id and die_parent of die_struct Y. */
24355 bool
24356 block_die_hasher::equal (die_struct *x, die_struct *y)
24358 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
24361 /* Hold information about markers for inlined entry points. */
24362 struct GTY ((for_user)) inline_entry_data
24364 /* The block that's the inlined_function_outer_scope for an inlined
24365 function. */
24366 tree block;
24368 /* The label at the inlined entry point. */
24369 const char *label_pfx;
24370 unsigned int label_num;
24372 /* The view number to be used as the inlined entry point. */
24373 var_loc_view view;
24376 struct inline_entry_data_hasher : ggc_ptr_hash <inline_entry_data>
24378 typedef tree compare_type;
24379 static inline hashval_t hash (const inline_entry_data *);
24380 static inline bool equal (const inline_entry_data *, const_tree);
24383 /* Hash table routines for inline_entry_data. */
24385 inline hashval_t
24386 inline_entry_data_hasher::hash (const inline_entry_data *data)
24388 return htab_hash_pointer (data->block);
24391 inline bool
24392 inline_entry_data_hasher::equal (const inline_entry_data *data,
24393 const_tree block)
24395 return data->block == block;
24398 /* Inlined entry points pending DIE creation in this compilation unit. */
24400 static GTY(()) hash_table<inline_entry_data_hasher> *inline_entry_data_table;
24403 /* Return TRUE if DECL, which may have been previously generated as
24404 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
24405 true if decl (or its origin) is either an extern declaration or a
24406 class/namespace scoped declaration.
24408 The declare_in_namespace support causes us to get two DIEs for one
24409 variable, both of which are declarations. We want to avoid
24410 considering one to be a specification, so we must test for
24411 DECLARATION and DW_AT_declaration. */
24412 static inline bool
24413 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
24415 return (old_die && TREE_STATIC (decl) && !declaration
24416 && get_AT_flag (old_die, DW_AT_declaration) == 1);
24419 /* Return true if DECL is a local static. */
24421 static inline bool
24422 local_function_static (tree decl)
24424 gcc_assert (VAR_P (decl));
24425 return TREE_STATIC (decl)
24426 && DECL_CONTEXT (decl)
24427 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
24430 /* Return true iff DECL overrides (presumably completes) the type of
24431 OLD_DIE within CONTEXT_DIE. */
24433 static bool
24434 override_type_for_decl_p (tree decl, dw_die_ref old_die,
24435 dw_die_ref context_die)
24437 tree type = TREE_TYPE (decl);
24438 int cv_quals;
24440 if (decl_by_reference_p (decl))
24442 type = TREE_TYPE (type);
24443 cv_quals = TYPE_UNQUALIFIED;
24445 else
24446 cv_quals = decl_quals (decl);
24448 dw_die_ref type_die = modified_type_die (type,
24449 cv_quals | TYPE_QUALS (type),
24450 false,
24451 context_die);
24453 dw_die_ref old_type_die = get_AT_ref (old_die, DW_AT_type);
24455 return type_die != old_type_die;
24458 /* Generate a DIE to represent a declared data object.
24459 Either DECL or ORIGIN must be non-null. */
24461 static void
24462 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
24464 HOST_WIDE_INT off = 0;
24465 tree com_decl;
24466 tree decl_or_origin = decl ? decl : origin;
24467 tree ultimate_origin;
24468 dw_die_ref var_die;
24469 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
24470 bool declaration = (DECL_EXTERNAL (decl_or_origin)
24471 || class_or_namespace_scope_p (context_die));
24472 bool specialization_p = false;
24473 bool no_linkage_name = false;
24475 /* While C++ inline static data members have definitions inside of the
24476 class, force the first DIE to be a declaration, then let gen_member_die
24477 reparent it to the class context and call gen_variable_die again
24478 to create the outside of the class DIE for the definition. */
24479 if (!declaration
24480 && old_die == NULL
24481 && decl
24482 && DECL_CONTEXT (decl)
24483 && TYPE_P (DECL_CONTEXT (decl))
24484 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
24486 declaration = true;
24487 if (dwarf_version < 5)
24488 no_linkage_name = true;
24491 ultimate_origin = decl_ultimate_origin (decl_or_origin);
24492 if (decl || ultimate_origin)
24493 origin = ultimate_origin;
24494 com_decl = fortran_common (decl_or_origin, &off);
24496 /* Symbol in common gets emitted as a child of the common block, in the form
24497 of a data member. */
24498 if (com_decl)
24500 dw_die_ref com_die;
24501 dw_loc_list_ref loc = NULL;
24502 die_node com_die_arg;
24504 var_die = lookup_decl_die (decl_or_origin);
24505 if (var_die)
24507 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
24509 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
24510 if (loc)
24512 if (off)
24514 /* Optimize the common case. */
24515 if (single_element_loc_list_p (loc)
24516 && loc->expr->dw_loc_opc == DW_OP_addr
24517 && loc->expr->dw_loc_next == NULL
24518 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
24519 == SYMBOL_REF)
24521 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
24522 loc->expr->dw_loc_oprnd1.v.val_addr
24523 = plus_constant (GET_MODE (x), x , off);
24525 else
24526 loc_list_plus_const (loc, off);
24528 add_AT_location_description (var_die, DW_AT_location, loc);
24529 remove_AT (var_die, DW_AT_declaration);
24532 return;
24535 if (common_block_die_table == NULL)
24536 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
24538 com_die_arg.decl_id = DECL_UID (com_decl);
24539 com_die_arg.die_parent = context_die;
24540 com_die = common_block_die_table->find (&com_die_arg);
24541 if (! early_dwarf)
24542 loc = loc_list_from_tree (com_decl, 2, NULL);
24543 if (com_die == NULL)
24545 const char *cnam
24546 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
24547 die_node **slot;
24549 com_die = new_die (DW_TAG_common_block, context_die, decl);
24550 add_name_and_src_coords_attributes (com_die, com_decl);
24551 if (loc)
24553 add_AT_location_description (com_die, DW_AT_location, loc);
24554 /* Avoid sharing the same loc descriptor between
24555 DW_TAG_common_block and DW_TAG_variable. */
24556 loc = loc_list_from_tree (com_decl, 2, NULL);
24558 else if (DECL_EXTERNAL (decl_or_origin))
24559 add_AT_flag (com_die, DW_AT_declaration, 1);
24560 if (want_pubnames ())
24561 add_pubname_string (cnam, com_die); /* ??? needed? */
24562 com_die->decl_id = DECL_UID (com_decl);
24563 slot = common_block_die_table->find_slot (com_die, INSERT);
24564 *slot = com_die;
24566 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
24568 add_AT_location_description (com_die, DW_AT_location, loc);
24569 loc = loc_list_from_tree (com_decl, 2, NULL);
24570 remove_AT (com_die, DW_AT_declaration);
24572 var_die = new_die (DW_TAG_variable, com_die, decl);
24573 add_name_and_src_coords_attributes (var_die, decl_or_origin);
24574 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
24575 decl_quals (decl_or_origin), false,
24576 context_die);
24577 add_alignment_attribute (var_die, decl);
24578 add_AT_flag (var_die, DW_AT_external, 1);
24579 if (loc)
24581 if (off)
24583 /* Optimize the common case. */
24584 if (single_element_loc_list_p (loc)
24585 && loc->expr->dw_loc_opc == DW_OP_addr
24586 && loc->expr->dw_loc_next == NULL
24587 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
24589 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
24590 loc->expr->dw_loc_oprnd1.v.val_addr
24591 = plus_constant (GET_MODE (x), x, off);
24593 else
24594 loc_list_plus_const (loc, off);
24596 add_AT_location_description (var_die, DW_AT_location, loc);
24598 else if (DECL_EXTERNAL (decl_or_origin))
24599 add_AT_flag (var_die, DW_AT_declaration, 1);
24600 if (decl)
24601 equate_decl_number_to_die (decl, var_die);
24602 return;
24605 if (old_die)
24607 if (declaration)
24609 /* A declaration that has been previously dumped, needs no
24610 further annotations, since it doesn't need location on
24611 the second pass. */
24612 return;
24614 else if (decl_will_get_specification_p (old_die, decl, declaration)
24615 && !get_AT (old_die, DW_AT_specification))
24617 /* Fall-thru so we can make a new variable die along with a
24618 DW_AT_specification. */
24620 else if (origin && old_die->die_parent != context_die)
24622 /* If we will be creating an inlined instance, we need a
24623 new DIE that will get annotated with
24624 DW_AT_abstract_origin. */
24625 gcc_assert (!DECL_ABSTRACT_P (decl));
24627 else
24629 /* If a DIE was dumped early, it still needs location info.
24630 Skip to where we fill the location bits. */
24631 var_die = old_die;
24633 /* ??? In LTRANS we cannot annotate early created variably
24634 modified type DIEs without copying them and adjusting all
24635 references to them. Thus we dumped them again. Also add a
24636 reference to them but beware of -g0 compile and -g link
24637 in which case the reference will be already present. */
24638 tree type = TREE_TYPE (decl_or_origin);
24639 if (in_lto_p
24640 && ! get_AT (var_die, DW_AT_type)
24641 && variably_modified_type_p
24642 (type, decl_function_context (decl_or_origin)))
24644 if (decl_by_reference_p (decl_or_origin))
24645 add_type_attribute (var_die, TREE_TYPE (type),
24646 TYPE_UNQUALIFIED, false, context_die);
24647 else
24648 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
24649 false, context_die);
24652 goto gen_variable_die_location;
24656 /* For static data members, the declaration in the class is supposed
24657 to have DW_TAG_member tag in DWARF{3,4} and we emit it for compatibility
24658 also in DWARF2; the specification should still be DW_TAG_variable
24659 referencing the DW_TAG_member DIE. */
24660 if (declaration && class_scope_p (context_die) && dwarf_version < 5)
24661 var_die = new_die (DW_TAG_member, context_die, decl);
24662 else
24663 var_die = new_die (DW_TAG_variable, context_die, decl);
24665 if (origin != NULL)
24666 add_abstract_origin_attribute (var_die, origin);
24668 /* Loop unrolling can create multiple blocks that refer to the same
24669 static variable, so we must test for the DW_AT_declaration flag.
24671 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
24672 copy decls and set the DECL_ABSTRACT_P flag on them instead of
24673 sharing them.
24675 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
24676 else if (decl_will_get_specification_p (old_die, decl, declaration))
24678 /* This is a definition of a C++ class level static. */
24679 add_AT_specification (var_die, old_die);
24680 specialization_p = true;
24681 if (DECL_NAME (decl))
24683 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
24684 struct dwarf_file_data * file_index = lookup_filename (s.file);
24686 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
24687 add_AT_file (var_die, DW_AT_decl_file, file_index);
24689 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
24690 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
24692 if (debug_column_info
24693 && s.column
24694 && (get_AT_unsigned (old_die, DW_AT_decl_column)
24695 != (unsigned) s.column))
24696 add_AT_unsigned (var_die, DW_AT_decl_column, s.column);
24698 if (old_die->die_tag == DW_TAG_member)
24699 add_linkage_name (var_die, decl);
24702 else
24703 add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
24705 if ((origin == NULL && !specialization_p)
24706 || (origin != NULL
24707 && !DECL_ABSTRACT_P (decl_or_origin)
24708 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
24709 decl_function_context
24710 (decl_or_origin)))
24711 || (old_die && specialization_p
24712 && override_type_for_decl_p (decl_or_origin, old_die, context_die)))
24714 tree type = TREE_TYPE (decl_or_origin);
24716 if (decl_by_reference_p (decl_or_origin))
24717 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
24718 context_die);
24719 else
24720 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
24721 context_die);
24724 if (origin == NULL && !specialization_p)
24726 if (TREE_PUBLIC (decl))
24727 add_AT_flag (var_die, DW_AT_external, 1);
24729 if (DECL_ARTIFICIAL (decl))
24730 add_AT_flag (var_die, DW_AT_artificial, 1);
24732 add_alignment_attribute (var_die, decl);
24734 add_accessibility_attribute (var_die, decl);
24737 if (declaration)
24738 add_AT_flag (var_die, DW_AT_declaration, 1);
24740 if (decl && (DECL_ABSTRACT_P (decl)
24741 || !old_die || is_declaration_die (old_die)))
24742 equate_decl_number_to_die (decl, var_die);
24744 gen_variable_die_location:
24745 if (! declaration
24746 && (! DECL_ABSTRACT_P (decl_or_origin)
24747 /* Local static vars are shared between all clones/inlines,
24748 so emit DW_AT_location on the abstract DIE if DECL_RTL is
24749 already set. */
24750 || (VAR_P (decl_or_origin)
24751 && TREE_STATIC (decl_or_origin)
24752 && DECL_RTL_SET_P (decl_or_origin))))
24754 if (early_dwarf)
24756 add_pubname (decl_or_origin, var_die);
24757 /* For global register variables, emit DW_AT_location if possible
24758 already during early_dwarf, as late_global_decl won't be usually
24759 called. */
24760 if (DECL_HARD_REGISTER (decl_or_origin)
24761 && TREE_STATIC (decl_or_origin)
24762 && !decl_by_reference_p (decl_or_origin)
24763 && !get_AT (var_die, DW_AT_location)
24764 && !get_AT (var_die, DW_AT_const_value)
24765 && DECL_RTL_SET_P (decl_or_origin)
24766 && REG_P (DECL_RTL (decl_or_origin)))
24768 dw_loc_descr_ref descr
24769 = reg_loc_descriptor (DECL_RTL (decl_or_origin),
24770 VAR_INIT_STATUS_INITIALIZED);
24771 if (descr)
24772 add_AT_loc (var_die, DW_AT_location, descr);
24775 else
24776 add_location_or_const_value_attribute (var_die, decl_or_origin,
24777 decl == NULL);
24779 else
24780 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
24782 if ((dwarf_version >= 4 || !dwarf_strict)
24783 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
24784 DW_AT_const_expr) == 1
24785 && !get_AT (var_die, DW_AT_const_expr)
24786 && !specialization_p)
24787 add_AT_flag (var_die, DW_AT_const_expr, 1);
24789 if (!dwarf_strict)
24791 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
24792 DW_AT_inline);
24793 if (inl != -1
24794 && !get_AT (var_die, DW_AT_inline)
24795 && !specialization_p)
24796 add_AT_unsigned (var_die, DW_AT_inline, inl);
24800 /* Generate a DIE to represent a named constant. */
24802 static void
24803 gen_const_die (tree decl, dw_die_ref context_die)
24805 dw_die_ref const_die;
24806 tree type = TREE_TYPE (decl);
24808 const_die = lookup_decl_die (decl);
24809 if (const_die)
24810 return;
24812 const_die = new_die (DW_TAG_constant, context_die, decl);
24813 equate_decl_number_to_die (decl, const_die);
24814 add_name_and_src_coords_attributes (const_die, decl);
24815 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
24816 if (TREE_PUBLIC (decl))
24817 add_AT_flag (const_die, DW_AT_external, 1);
24818 if (DECL_ARTIFICIAL (decl))
24819 add_AT_flag (const_die, DW_AT_artificial, 1);
24820 tree_add_const_value_attribute_for_decl (const_die, decl);
24823 /* Generate a DIE to represent a label identifier. */
24825 static void
24826 gen_label_die (tree decl, dw_die_ref context_die)
24828 tree origin = decl_ultimate_origin (decl);
24829 dw_die_ref lbl_die = lookup_decl_die (decl);
24830 rtx insn;
24831 char label[MAX_ARTIFICIAL_LABEL_BYTES];
24833 if (!lbl_die)
24835 lbl_die = new_die (DW_TAG_label, context_die, decl);
24836 equate_decl_number_to_die (decl, lbl_die);
24838 if (origin != NULL)
24839 add_abstract_origin_attribute (lbl_die, origin);
24840 else
24841 add_name_and_src_coords_attributes (lbl_die, decl);
24844 if (DECL_ABSTRACT_P (decl))
24845 equate_decl_number_to_die (decl, lbl_die);
24846 else if (! early_dwarf)
24848 insn = DECL_RTL_IF_SET (decl);
24850 /* Deleted labels are programmer specified labels which have been
24851 eliminated because of various optimizations. We still emit them
24852 here so that it is possible to put breakpoints on them. */
24853 if (insn
24854 && (LABEL_P (insn)
24855 || ((NOTE_P (insn)
24856 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
24858 /* When optimization is enabled (via -O) some parts of the compiler
24859 (e.g. jump.cc and cse.cc) may try to delete CODE_LABEL insns which
24860 represent source-level labels which were explicitly declared by
24861 the user. This really shouldn't be happening though, so catch
24862 it if it ever does happen. */
24863 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
24865 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
24866 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
24868 else if (insn
24869 && NOTE_P (insn)
24870 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
24871 && CODE_LABEL_NUMBER (insn) != -1)
24873 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
24874 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
24879 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
24880 attributes to the DIE for a block STMT, to describe where the inlined
24881 function was called from. This is similar to add_src_coords_attributes. */
24883 static inline void
24884 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
24886 /* We can end up with BUILTINS_LOCATION here. */
24887 if (RESERVED_LOCATION_P (BLOCK_SOURCE_LOCATION (stmt)))
24888 return;
24890 location_t locus = BLOCK_SOURCE_LOCATION (stmt);
24891 expanded_location s = expand_location (locus);
24893 if (dwarf_version >= 3 || !dwarf_strict)
24895 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
24896 add_AT_unsigned (die, DW_AT_call_line, s.line);
24897 if (debug_column_info && s.column)
24898 add_AT_unsigned (die, DW_AT_call_column, s.column);
24899 unsigned discr = get_discriminator_from_loc (locus);
24900 if (discr != 0)
24901 add_AT_unsigned (die, DW_AT_GNU_discriminator, discr);
24906 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
24907 Add low_pc and high_pc attributes to the DIE for a block STMT. */
24909 static inline void
24910 add_high_low_attributes (tree stmt, dw_die_ref die)
24912 char label[MAX_ARTIFICIAL_LABEL_BYTES];
24914 if (inline_entry_data **iedp
24915 = !inline_entry_data_table ? NULL
24916 : inline_entry_data_table->find_slot_with_hash (stmt,
24917 htab_hash_pointer (stmt),
24918 NO_INSERT))
24920 inline_entry_data *ied = *iedp;
24921 gcc_assert (MAY_HAVE_DEBUG_MARKER_INSNS);
24922 gcc_assert (debug_inline_points);
24923 gcc_assert (inlined_function_outer_scope_p (stmt));
24925 ASM_GENERATE_INTERNAL_LABEL (label, ied->label_pfx, ied->label_num);
24926 add_AT_lbl_id (die, DW_AT_entry_pc, label);
24928 if (debug_variable_location_views && !ZERO_VIEW_P (ied->view)
24929 && !dwarf_strict)
24931 if (!output_asm_line_debug_info ())
24932 add_AT_unsigned (die, DW_AT_GNU_entry_view, ied->view);
24933 else
24935 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", ied->view);
24936 /* FIXME: this will resolve to a small number. Could we
24937 possibly emit smaller data? Ideally we'd emit a
24938 uleb128, but that would make the size of DIEs
24939 impossible for the compiler to compute, since it's
24940 the assembler that computes the value of the view
24941 label in this case. Ideally, we'd have a single form
24942 encompassing both the address and the view, and
24943 indirecting them through a table might make things
24944 easier, but even that would be more wasteful,
24945 space-wise, than what we have now. */
24946 add_AT_symview (die, DW_AT_GNU_entry_view, label);
24950 inline_entry_data_table->clear_slot (iedp);
24953 if (BLOCK_FRAGMENT_CHAIN (stmt)
24954 && (dwarf_version >= 3 || !dwarf_strict))
24956 tree chain, superblock = NULL_TREE;
24957 dw_die_ref pdie;
24958 dw_attr_node *attr = NULL;
24960 if (!debug_inline_points && inlined_function_outer_scope_p (stmt))
24962 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
24963 BLOCK_NUMBER (stmt));
24964 add_AT_lbl_id (die, DW_AT_entry_pc, label);
24967 /* Optimize duplicate .debug_ranges lists or even tails of
24968 lists. If this BLOCK has same ranges as its supercontext,
24969 lookup DW_AT_ranges attribute in the supercontext (and
24970 recursively so), verify that the ranges_table contains the
24971 right values and use it instead of adding a new .debug_range. */
24972 for (chain = stmt, pdie = die;
24973 BLOCK_SAME_RANGE (chain);
24974 chain = BLOCK_SUPERCONTEXT (chain))
24976 dw_attr_node *new_attr;
24978 pdie = pdie->die_parent;
24979 if (pdie == NULL)
24980 break;
24981 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
24982 break;
24983 new_attr = get_AT (pdie, DW_AT_ranges);
24984 if (new_attr == NULL
24985 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
24986 break;
24987 attr = new_attr;
24988 superblock = BLOCK_SUPERCONTEXT (chain);
24990 if (attr != NULL
24991 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
24992 == (int)BLOCK_NUMBER (superblock))
24993 && BLOCK_FRAGMENT_CHAIN (superblock))
24995 unsigned long off = attr->dw_attr_val.v.val_offset;
24996 unsigned long supercnt = 0, thiscnt = 0;
24997 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
24998 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
25000 ++supercnt;
25001 gcc_checking_assert ((*ranges_table)[off + supercnt].num
25002 == (int)BLOCK_NUMBER (chain));
25004 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
25005 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
25006 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
25007 ++thiscnt;
25008 gcc_assert (supercnt >= thiscnt);
25009 add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
25010 false);
25011 note_rnglist_head (off + supercnt - thiscnt);
25012 return;
25015 unsigned int offset = add_ranges (stmt, true);
25016 add_AT_range_list (die, DW_AT_ranges, offset, false);
25017 note_rnglist_head (offset);
25019 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
25020 chain = BLOCK_FRAGMENT_CHAIN (stmt);
25023 add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
25024 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
25025 chain = BLOCK_FRAGMENT_CHAIN (chain);
25027 while (chain);
25028 add_ranges (NULL);
25030 else
25032 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
25033 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
25034 BLOCK_NUMBER (stmt));
25035 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
25036 BLOCK_NUMBER (stmt));
25037 add_AT_low_high_pc (die, label, label_high, false);
25041 /* Generate a DIE for a lexical block. */
25043 static void
25044 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
25046 dw_die_ref old_die = lookup_block_die (stmt);
25047 dw_die_ref stmt_die = NULL;
25048 if (!old_die)
25050 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
25051 equate_block_to_die (stmt, stmt_die);
25054 if (BLOCK_ABSTRACT_ORIGIN (stmt))
25056 /* If this is an inlined or conrecte instance, create a new lexical
25057 die for anything below to attach DW_AT_abstract_origin to. */
25058 if (old_die)
25059 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
25061 tree origin = block_ultimate_origin (stmt);
25062 if (origin != NULL_TREE && (origin != stmt || old_die))
25063 add_abstract_origin_attribute (stmt_die, origin);
25065 old_die = NULL;
25068 if (old_die)
25069 stmt_die = old_die;
25071 /* A non abstract block whose blocks have already been reordered
25072 should have the instruction range for this block. If so, set the
25073 high/low attributes. */
25074 if (!early_dwarf && TREE_ASM_WRITTEN (stmt))
25076 gcc_assert (stmt_die);
25077 add_high_low_attributes (stmt, stmt_die);
25080 decls_for_scope (stmt, stmt_die);
25083 /* Generate a DIE for an inlined subprogram. */
25085 static void
25086 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
25088 tree decl = block_ultimate_origin (stmt);
25090 /* Make sure any inlined functions are known to be inlineable. */
25091 gcc_checking_assert (DECL_ABSTRACT_P (decl)
25092 || cgraph_function_possibly_inlined_p (decl));
25094 dw_die_ref subr_die = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
25096 if (call_arg_locations || debug_inline_points)
25097 equate_block_to_die (stmt, subr_die);
25098 add_abstract_origin_attribute (subr_die, decl);
25099 if (TREE_ASM_WRITTEN (stmt))
25100 add_high_low_attributes (stmt, subr_die);
25101 add_call_src_coords_attributes (stmt, subr_die);
25103 /* The inliner creates an extra BLOCK for the parameter setup,
25104 we want to merge that with the actual outermost BLOCK of the
25105 inlined function to avoid duplicate locals in consumers.
25106 Do that by doing the recursion to subblocks on the single subblock
25107 of STMT. */
25108 bool unwrap_one = false;
25109 if (BLOCK_SUBBLOCKS (stmt) && !BLOCK_CHAIN (BLOCK_SUBBLOCKS (stmt)))
25111 tree origin = block_ultimate_origin (BLOCK_SUBBLOCKS (stmt));
25112 if (origin
25113 && TREE_CODE (origin) == BLOCK
25114 && BLOCK_SUPERCONTEXT (origin) == decl)
25115 unwrap_one = true;
25117 decls_for_scope (stmt, subr_die, !unwrap_one);
25118 if (unwrap_one)
25119 decls_for_scope (BLOCK_SUBBLOCKS (stmt), subr_die);
25122 /* Generate a DIE for a field in a record, or structure. CTX is required: see
25123 the comment for VLR_CONTEXT. */
25125 static void
25126 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
25128 dw_die_ref decl_die;
25130 if (TREE_TYPE (decl) == error_mark_node)
25131 return;
25133 decl_die = new_die (DW_TAG_member, context_die, decl);
25134 add_name_and_src_coords_attributes (decl_die, decl);
25135 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
25136 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
25137 context_die);
25139 if (DECL_BIT_FIELD_TYPE (decl))
25141 add_byte_size_attribute (decl_die, decl);
25142 add_bit_size_attribute (decl_die, decl);
25143 add_bit_offset_attribute (decl_die, decl);
25146 add_alignment_attribute (decl_die, decl);
25148 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
25149 add_data_member_location_attribute (decl_die, decl, ctx);
25151 if (DECL_ARTIFICIAL (decl))
25152 add_AT_flag (decl_die, DW_AT_artificial, 1);
25154 add_accessibility_attribute (decl_die, decl);
25156 /* Equate decl number to die, so that we can look up this decl later on. */
25157 equate_decl_number_to_die (decl, decl_die);
25160 /* Generate a DIE for a pointer to a member type. TYPE can be an
25161 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
25162 pointer to member function. */
25164 static void
25165 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
25167 if (lookup_type_die (type))
25168 return;
25170 dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
25171 scope_die_for (type, context_die), type);
25173 equate_type_number_to_die (type, ptr_die);
25174 add_AT_die_ref (ptr_die, DW_AT_containing_type,
25175 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
25176 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
25177 context_die);
25178 add_alignment_attribute (ptr_die, type);
25180 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
25181 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
25183 dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
25184 add_AT_loc (ptr_die, DW_AT_use_location, op);
25188 static char *producer_string;
25190 /* Given a C and/or C++ language/version string return the "highest".
25191 C++ is assumed to be "higher" than C in this case. Used for merging
25192 LTO translation unit languages. */
25193 static const char *
25194 highest_c_language (const char *lang1, const char *lang2)
25196 if (strcmp ("GNU C++26", lang1) == 0 || strcmp ("GNU C++26", lang2) == 0)
25197 return "GNU C++26";
25198 if (strcmp ("GNU C++23", lang1) == 0 || strcmp ("GNU C++23", lang2) == 0)
25199 return "GNU C++23";
25200 if (strcmp ("GNU C++20", lang1) == 0 || strcmp ("GNU C++20", lang2) == 0)
25201 return "GNU C++20";
25202 if (strcmp ("GNU C++17", lang1) == 0 || strcmp ("GNU C++17", lang2) == 0)
25203 return "GNU C++17";
25204 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
25205 return "GNU C++14";
25206 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
25207 return "GNU C++11";
25208 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
25209 return "GNU C++98";
25211 if (strcmp ("GNU C23", lang1) == 0 || strcmp ("GNU C23", lang2) == 0)
25212 return "GNU C23";
25213 if (strcmp ("GNU C17", lang1) == 0 || strcmp ("GNU C17", lang2) == 0)
25214 return "GNU C17";
25215 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
25216 return "GNU C11";
25217 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
25218 return "GNU C99";
25219 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
25220 return "GNU C89";
25222 gcc_unreachable ();
25226 /* Generate the DIE for the compilation unit. */
25228 static dw_die_ref
25229 gen_compile_unit_die (const char *filename)
25231 dw_die_ref die;
25232 const char *language_string = lang_hooks.name;
25233 int language;
25235 die = new_die (DW_TAG_compile_unit, NULL, NULL);
25237 if (filename)
25239 add_filename_attribute (die, filename);
25240 /* Don't add cwd for <built-in>. */
25241 if (filename[0] != '<')
25242 add_comp_dir_attribute (die);
25245 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
25247 /* If our producer is LTO try to figure out a common language to use
25248 from the global list of translation units. */
25249 if (strcmp (language_string, "GNU GIMPLE") == 0)
25251 unsigned i;
25252 tree t;
25253 const char *common_lang = NULL;
25255 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
25257 if (!TRANSLATION_UNIT_LANGUAGE (t))
25258 continue;
25259 if (!common_lang)
25260 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
25261 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
25263 else if (startswith (common_lang, "GNU C")
25264 && startswith (TRANSLATION_UNIT_LANGUAGE (t), "GNU C"))
25265 /* Mixing C and C++ is ok, use C++ in that case. */
25266 common_lang = highest_c_language (common_lang,
25267 TRANSLATION_UNIT_LANGUAGE (t));
25268 else
25270 /* Fall back to C. */
25271 common_lang = NULL;
25272 break;
25276 if (common_lang)
25277 language_string = common_lang;
25280 language = DW_LANG_C;
25281 if (startswith (language_string, "GNU C")
25282 && ISDIGIT (language_string[5]))
25284 language = DW_LANG_C89;
25285 if (dwarf_version >= 3 || !dwarf_strict)
25287 if (strcmp (language_string, "GNU C89") != 0)
25288 language = DW_LANG_C99;
25290 if (dwarf_version >= 5 /* || !dwarf_strict */)
25291 if (strcmp (language_string, "GNU C11") == 0
25292 || strcmp (language_string, "GNU C17") == 0
25293 || strcmp (language_string, "GNU C23") == 0)
25294 language = DW_LANG_C11;
25297 else if (startswith (language_string, "GNU C++"))
25299 language = DW_LANG_C_plus_plus;
25300 if (dwarf_version >= 5 /* || !dwarf_strict */)
25302 if (strcmp (language_string, "GNU C++11") == 0)
25303 language = DW_LANG_C_plus_plus_11;
25304 else if (strcmp (language_string, "GNU C++14") == 0)
25305 language = DW_LANG_C_plus_plus_14;
25306 else if (strcmp (language_string, "GNU C++17") == 0
25307 || strcmp (language_string, "GNU C++20") == 0
25308 || strcmp (language_string, "GNU C++23") == 0
25309 || strcmp (language_string, "GNU C++26") == 0)
25310 /* For now. */
25311 language = DW_LANG_C_plus_plus_14;
25314 else if (strcmp (language_string, "GNU F77") == 0)
25315 language = DW_LANG_Fortran77;
25316 else if (strcmp (language_string, "GNU Modula-2") == 0)
25317 language = DW_LANG_Modula2;
25318 else if (dwarf_version >= 3 || !dwarf_strict)
25320 if (strcmp (language_string, "GNU Ada") == 0)
25321 language = DW_LANG_Ada95;
25322 else if (startswith (language_string, "GNU Fortran"))
25324 language = DW_LANG_Fortran95;
25325 if (dwarf_version >= 5 /* || !dwarf_strict */)
25327 if (strcmp (language_string, "GNU Fortran2003") == 0)
25328 language = DW_LANG_Fortran03;
25329 else if (strcmp (language_string, "GNU Fortran2008") == 0)
25330 language = DW_LANG_Fortran08;
25333 else if (strcmp (language_string, "GNU Objective-C") == 0)
25334 language = DW_LANG_ObjC;
25335 else if (strcmp (language_string, "GNU Objective-C++") == 0)
25336 language = DW_LANG_ObjC_plus_plus;
25337 else if (strcmp (language_string, "GNU D") == 0)
25338 language = DW_LANG_D;
25339 else if (dwarf_version >= 5 || !dwarf_strict)
25341 if (strcmp (language_string, "GNU Go") == 0)
25342 language = DW_LANG_Go;
25343 else if (strcmp (language_string, "GNU Rust") == 0)
25344 language = DW_LANG_Rust;
25347 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
25348 else if (startswith (language_string, "GNU Fortran"))
25349 language = DW_LANG_Fortran90;
25350 /* Likewise for Ada. */
25351 else if (strcmp (language_string, "GNU Ada") == 0)
25352 language = DW_LANG_Ada83;
25354 add_AT_unsigned (die, DW_AT_language, language);
25356 switch (language)
25358 case DW_LANG_Fortran77:
25359 case DW_LANG_Fortran90:
25360 case DW_LANG_Fortran95:
25361 case DW_LANG_Fortran03:
25362 case DW_LANG_Fortran08:
25363 /* Fortran has case insensitive identifiers and the front-end
25364 lowercases everything. */
25365 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
25366 break;
25367 default:
25368 /* The default DW_ID_case_sensitive doesn't need to be specified. */
25369 break;
25371 return die;
25374 /* Generate the DIE for a base class. */
25376 static void
25377 gen_inheritance_die (tree binfo, tree access, tree type,
25378 dw_die_ref context_die)
25380 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
25381 struct vlr_context ctx = { type, NULL };
25383 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
25384 context_die);
25385 add_data_member_location_attribute (die, binfo, &ctx);
25387 if (BINFO_VIRTUAL_P (binfo))
25388 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
25390 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
25391 children, otherwise the default is DW_ACCESS_public. In DWARF2
25392 the default has always been DW_ACCESS_private. */
25393 if (access == access_public_node)
25395 if (dwarf_version == 2
25396 || context_die->die_tag == DW_TAG_class_type)
25397 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
25399 else if (access == access_protected_node)
25400 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
25401 else if (dwarf_version > 2
25402 && context_die->die_tag != DW_TAG_class_type)
25403 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
25406 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
25407 structure. */
25409 static bool
25410 is_variant_part (tree decl)
25412 return (TREE_CODE (decl) == FIELD_DECL
25413 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
25416 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
25417 return the FIELD_DECL. Return NULL_TREE otherwise. */
25419 static tree
25420 analyze_discr_in_predicate (tree operand, tree struct_type)
25422 while (CONVERT_EXPR_P (operand))
25423 operand = TREE_OPERAND (operand, 0);
25425 /* Match field access to members of struct_type only. */
25426 if (TREE_CODE (operand) == COMPONENT_REF
25427 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
25428 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
25429 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
25430 return TREE_OPERAND (operand, 1);
25431 else
25432 return NULL_TREE;
25435 /* Check that SRC is a constant integer that can be represented as a native
25436 integer constant (either signed or unsigned). If so, store it into DEST and
25437 return true. Return false otherwise. */
25439 static bool
25440 get_discr_value (tree src, dw_discr_value *dest)
25442 tree discr_type = TREE_TYPE (src);
25444 if (lang_hooks.types.get_debug_type)
25446 tree debug_type = lang_hooks.types.get_debug_type (discr_type);
25447 if (debug_type != NULL)
25448 discr_type = debug_type;
25451 if (TREE_CODE (src) != INTEGER_CST || !INTEGRAL_TYPE_P (discr_type))
25452 return false;
25454 /* Signedness can vary between the original type and the debug type. This
25455 can happen for character types in Ada for instance: the character type
25456 used for code generation can be signed, to be compatible with the C one,
25457 but from a debugger point of view, it must be unsigned. */
25458 bool is_orig_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
25459 bool is_debug_unsigned = TYPE_UNSIGNED (discr_type);
25461 if (is_orig_unsigned != is_debug_unsigned)
25462 src = fold_convert (discr_type, src);
25464 if (!(is_debug_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
25465 return false;
25467 dest->pos = is_debug_unsigned;
25468 if (is_debug_unsigned)
25469 dest->v.uval = tree_to_uhwi (src);
25470 else
25471 dest->v.sval = tree_to_shwi (src);
25473 return true;
25476 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
25477 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
25478 store NULL_TREE in DISCR_DECL. Otherwise:
25480 - store the discriminant field in STRUCT_TYPE that controls the variant
25481 part to *DISCR_DECL
25483 - put in *DISCR_LISTS_P an array where for each variant, the item
25484 represents the corresponding matching list of discriminant values.
25486 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
25487 the above array.
25489 Note that when the array is allocated (i.e. when the analysis is
25490 successful), it is up to the caller to free the array. */
25492 static void
25493 analyze_variants_discr (tree variant_part_decl,
25494 tree struct_type,
25495 tree *discr_decl,
25496 dw_discr_list_ref **discr_lists_p,
25497 unsigned *discr_lists_length)
25499 tree variant_part_type = TREE_TYPE (variant_part_decl);
25500 tree variant;
25501 dw_discr_list_ref *discr_lists;
25502 unsigned i;
25504 /* Compute how many variants there are in this variant part. */
25505 *discr_lists_length = 0;
25506 for (variant = TYPE_FIELDS (variant_part_type);
25507 variant != NULL_TREE;
25508 variant = DECL_CHAIN (variant))
25509 ++*discr_lists_length;
25511 *discr_decl = NULL_TREE;
25512 *discr_lists_p
25513 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
25514 sizeof (**discr_lists_p));
25515 discr_lists = *discr_lists_p;
25517 /* And then analyze all variants to extract discriminant information for all
25518 of them. This analysis is conservative: as soon as we detect something we
25519 do not support, abort everything and pretend we found nothing. */
25520 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
25521 variant != NULL_TREE;
25522 variant = DECL_CHAIN (variant), ++i)
25524 tree match_expr = DECL_QUALIFIER (variant);
25526 /* Now, try to analyze the predicate and deduce a discriminant for
25527 it. */
25528 if (match_expr == boolean_true_node)
25529 /* Typically happens for the default variant: it matches all cases that
25530 previous variants rejected. Don't output any matching value for
25531 this one. */
25532 continue;
25534 /* The following loop tries to iterate over each discriminant
25535 possibility: single values or ranges. */
25536 while (match_expr != NULL_TREE)
25538 tree next_round_match_expr;
25539 tree candidate_discr = NULL_TREE;
25540 dw_discr_list_ref new_node = NULL;
25542 /* Possibilities are matched one after the other by nested
25543 TRUTH_ORIF_EXPR expressions. Process the current possibility and
25544 continue with the rest at next iteration. */
25545 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
25547 next_round_match_expr = TREE_OPERAND (match_expr, 0);
25548 match_expr = TREE_OPERAND (match_expr, 1);
25550 else
25551 next_round_match_expr = NULL_TREE;
25553 if (match_expr == boolean_false_node)
25554 /* This sub-expression matches nothing: just wait for the next
25555 one. */
25558 else if (TREE_CODE (match_expr) == EQ_EXPR)
25560 /* We are matching: <discr_field> == <integer_cst>
25561 This sub-expression matches a single value. */
25562 tree integer_cst = TREE_OPERAND (match_expr, 1);
25564 candidate_discr
25565 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
25566 struct_type);
25568 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
25569 if (!get_discr_value (integer_cst,
25570 &new_node->dw_discr_lower_bound))
25571 goto abort;
25572 new_node->dw_discr_range = false;
25575 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
25577 /* We are matching:
25578 <discr_field> > <integer_cst>
25579 && <discr_field> < <integer_cst>.
25580 This sub-expression matches the range of values between the
25581 two matched integer constants. Note that comparisons can be
25582 inclusive or exclusive. */
25583 tree candidate_discr_1, candidate_discr_2;
25584 tree lower_cst, upper_cst;
25585 bool lower_cst_included, upper_cst_included;
25586 tree lower_op = TREE_OPERAND (match_expr, 0);
25587 tree upper_op = TREE_OPERAND (match_expr, 1);
25589 /* When the comparison is exclusive, the integer constant is not
25590 the discriminant range bound we are looking for: we will have
25591 to increment or decrement it. */
25592 if (TREE_CODE (lower_op) == GE_EXPR)
25593 lower_cst_included = true;
25594 else if (TREE_CODE (lower_op) == GT_EXPR)
25595 lower_cst_included = false;
25596 else
25597 goto abort;
25599 if (TREE_CODE (upper_op) == LE_EXPR)
25600 upper_cst_included = true;
25601 else if (TREE_CODE (upper_op) == LT_EXPR)
25602 upper_cst_included = false;
25603 else
25604 goto abort;
25606 /* Extract the discriminant from the first operand and check it
25607 is consistant with the same analysis in the second
25608 operand. */
25609 candidate_discr_1
25610 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
25611 struct_type);
25612 candidate_discr_2
25613 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
25614 struct_type);
25615 if (candidate_discr_1 == candidate_discr_2)
25616 candidate_discr = candidate_discr_1;
25617 else
25618 goto abort;
25620 /* Extract bounds from both. */
25621 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
25622 lower_cst = TREE_OPERAND (lower_op, 1);
25623 upper_cst = TREE_OPERAND (upper_op, 1);
25625 if (!lower_cst_included)
25626 lower_cst
25627 = fold_build2 (PLUS_EXPR, TREE_TYPE (lower_cst), lower_cst,
25628 build_int_cst (TREE_TYPE (lower_cst), 1));
25629 if (!upper_cst_included)
25630 upper_cst
25631 = fold_build2 (MINUS_EXPR, TREE_TYPE (upper_cst), upper_cst,
25632 build_int_cst (TREE_TYPE (upper_cst), 1));
25634 if (!get_discr_value (lower_cst,
25635 &new_node->dw_discr_lower_bound)
25636 || !get_discr_value (upper_cst,
25637 &new_node->dw_discr_upper_bound))
25638 goto abort;
25640 new_node->dw_discr_range = true;
25643 else if ((candidate_discr
25644 = analyze_discr_in_predicate (match_expr, struct_type))
25645 && (TREE_TYPE (candidate_discr) == boolean_type_node
25646 || TREE_TYPE (TREE_TYPE (candidate_discr))
25647 == boolean_type_node))
25649 /* We are matching: <discr_field> for a boolean discriminant.
25650 This sub-expression matches boolean_true_node. */
25651 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
25652 if (!get_discr_value (boolean_true_node,
25653 &new_node->dw_discr_lower_bound))
25654 goto abort;
25655 new_node->dw_discr_range = false;
25658 else
25659 /* Unsupported sub-expression: we cannot determine the set of
25660 matching discriminant values. Abort everything. */
25661 goto abort;
25663 /* If the discriminant info is not consistant with what we saw so
25664 far, consider the analysis failed and abort everything. */
25665 if (candidate_discr == NULL_TREE
25666 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
25667 goto abort;
25668 else
25669 *discr_decl = candidate_discr;
25671 if (new_node != NULL)
25673 new_node->dw_discr_next = discr_lists[i];
25674 discr_lists[i] = new_node;
25676 match_expr = next_round_match_expr;
25680 /* If we reach this point, we could match everything we were interested
25681 in. */
25682 return;
25684 abort:
25685 /* Clean all data structure and return no result. */
25686 free (*discr_lists_p);
25687 *discr_lists_p = NULL;
25688 *discr_decl = NULL_TREE;
25691 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
25692 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
25693 under CONTEXT_DIE.
25695 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
25696 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
25697 this type, which are record types, represent the available variants and each
25698 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
25699 values are inferred from these attributes.
25701 In trees, the offsets for the fields inside these sub-records are relative
25702 to the variant part itself, whereas the corresponding DIEs should have
25703 offset attributes that are relative to the embedding record base address.
25704 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
25705 must be an expression that computes the offset of the variant part to
25706 describe in DWARF. */
25708 static void
25709 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
25710 dw_die_ref context_die)
25712 const tree variant_part_type = TREE_TYPE (variant_part_decl);
25713 tree variant_part_offset = vlr_ctx->variant_part_offset;
25715 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
25716 NULL_TREE if there is no such field. */
25717 tree discr_decl = NULL_TREE;
25718 dw_discr_list_ref *discr_lists;
25719 unsigned discr_lists_length = 0;
25720 unsigned i;
25722 dw_die_ref dwarf_proc_die = NULL;
25723 dw_die_ref variant_part_die
25724 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
25726 equate_decl_number_to_die (variant_part_decl, variant_part_die);
25728 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
25729 &discr_decl, &discr_lists, &discr_lists_length);
25731 if (discr_decl != NULL_TREE)
25733 dw_die_ref discr_die = lookup_decl_die (discr_decl);
25735 if (discr_die)
25736 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
25737 else
25738 /* We have no DIE for the discriminant, so just discard all
25739 discrimimant information in the output. */
25740 discr_decl = NULL_TREE;
25743 /* If the offset for this variant part is more complex than a constant,
25744 create a DWARF procedure for it so that we will not have to generate
25745 DWARF expressions for it for each member. */
25746 if (TREE_CODE (variant_part_offset) != INTEGER_CST
25747 && (dwarf_version >= 3 || !dwarf_strict))
25749 struct loc_descr_context ctx = {
25750 vlr_ctx->struct_type, /* context_type */
25751 NULL_TREE, /* base_decl */
25752 NULL, /* dpi */
25753 false, /* placeholder_arg */
25754 false, /* placeholder_seen */
25755 false /* strict_signedness */
25757 const tree dwarf_proc_fndecl
25758 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
25759 build_function_type (TREE_TYPE (variant_part_offset),
25760 NULL_TREE));
25761 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
25762 const dw_loc_descr_ref dwarf_proc_body
25763 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
25765 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
25766 dwarf_proc_fndecl, context_die);
25767 if (dwarf_proc_die != NULL)
25768 variant_part_offset = dwarf_proc_call;
25771 /* Output DIEs for all variants. */
25772 i = 0;
25773 for (tree variant = TYPE_FIELDS (variant_part_type);
25774 variant != NULL_TREE;
25775 variant = DECL_CHAIN (variant), ++i)
25777 tree variant_type = TREE_TYPE (variant);
25778 dw_die_ref variant_die;
25780 /* All variants (i.e. members of a variant part) are supposed to be
25781 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
25782 under these records. */
25783 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
25785 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
25786 equate_decl_number_to_die (variant, variant_die);
25788 /* Output discriminant values this variant matches, if any. */
25789 if (discr_decl == NULL || discr_lists[i] == NULL)
25790 /* In the case we have discriminant information at all, this is
25791 probably the default variant: as the standard says, don't
25792 output any discriminant value/list attribute. */
25794 else if (discr_lists[i]->dw_discr_next == NULL
25795 && !discr_lists[i]->dw_discr_range)
25796 /* If there is only one accepted value, don't bother outputting a
25797 list. */
25798 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
25799 else
25800 add_discr_list (variant_die, discr_lists[i]);
25802 for (tree member = TYPE_FIELDS (variant_type);
25803 member != NULL_TREE;
25804 member = DECL_CHAIN (member))
25806 struct vlr_context vlr_sub_ctx = {
25807 vlr_ctx->struct_type, /* struct_type */
25808 NULL /* variant_part_offset */
25810 if (is_variant_part (member))
25812 /* All offsets for fields inside variant parts are relative to
25813 the top-level embedding RECORD_TYPE's base address. On the
25814 other hand, offsets in GCC's types are relative to the
25815 nested-most variant part. So we have to sum offsets each time
25816 we recurse. */
25818 vlr_sub_ctx.variant_part_offset
25819 = fold_build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
25820 variant_part_offset, byte_position (member));
25821 gen_variant_part (member, &vlr_sub_ctx, variant_die);
25823 else
25825 vlr_sub_ctx.variant_part_offset = variant_part_offset;
25826 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
25831 free (discr_lists);
25834 /* Generate a DIE for a class member. */
25836 static void
25837 gen_member_die (tree type, dw_die_ref context_die)
25839 tree member;
25840 tree binfo = TYPE_BINFO (type);
25842 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
25844 /* If this is not an incomplete type, output descriptions of each of its
25845 members. Note that as we output the DIEs necessary to represent the
25846 members of this record or union type, we will also be trying to output
25847 DIEs to represent the *types* of those members. However the `type'
25848 function (above) will specifically avoid generating type DIEs for member
25849 types *within* the list of member DIEs for this (containing) type except
25850 for those types (of members) which are explicitly marked as also being
25851 members of this (containing) type themselves. The g++ front- end can
25852 force any given type to be treated as a member of some other (containing)
25853 type by setting the TYPE_CONTEXT of the given (member) type to point to
25854 the TREE node representing the appropriate (containing) type. */
25856 /* First output info about the base classes. */
25857 if (binfo && early_dwarf)
25859 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
25860 int i;
25861 tree base;
25863 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
25864 gen_inheritance_die (base,
25865 (accesses ? (*accesses)[i] : access_public_node),
25866 type,
25867 context_die);
25870 /* Now output info about the members. */
25871 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
25873 /* Ignore clones. */
25874 if (DECL_ABSTRACT_ORIGIN (member))
25875 continue;
25877 struct vlr_context vlr_ctx = { type, NULL_TREE };
25878 bool static_inline_p
25879 = (VAR_P (member)
25880 && TREE_STATIC (member)
25881 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
25882 != -1));
25884 /* If we thought we were generating minimal debug info for TYPE
25885 and then changed our minds, some of the member declarations
25886 may have already been defined. Don't define them again, but
25887 do put them in the right order. */
25889 if (dw_die_ref child = lookup_decl_die (member))
25891 /* Handle inline static data members, which only have in-class
25892 declarations. */
25893 bool splice = true;
25895 dw_die_ref ref = NULL;
25896 if (child->die_tag == DW_TAG_variable
25897 && child->die_parent == comp_unit_die ())
25899 ref = get_AT_ref (child, DW_AT_specification);
25901 /* For C++17 inline static data members followed by redundant
25902 out of class redeclaration, we might get here with
25903 child being the DIE created for the out of class
25904 redeclaration and with its DW_AT_specification being
25905 the DIE created for in-class definition. We want to
25906 reparent the latter, and don't want to create another
25907 DIE with DW_AT_specification in that case, because
25908 we already have one. */
25909 if (ref
25910 && static_inline_p
25911 && ref->die_tag == DW_TAG_variable
25912 && ref->die_parent == comp_unit_die ()
25913 && get_AT (ref, DW_AT_specification) == NULL)
25915 child = ref;
25916 ref = NULL;
25917 static_inline_p = false;
25920 if (!ref)
25922 reparent_child (child, context_die);
25923 if (dwarf_version < 5)
25924 child->die_tag = DW_TAG_member;
25925 splice = false;
25928 else if (child->die_tag == DW_TAG_enumerator)
25929 /* Enumerators remain under their enumeration even if
25930 their names are introduced in the enclosing scope. */
25931 splice = false;
25933 if (splice)
25934 splice_child_die (context_die, child);
25937 /* Do not generate DWARF for variant parts if we are generating the
25938 corresponding GNAT encodings: DIEs generated for the two schemes
25939 would conflict in our mappings. */
25940 else if (is_variant_part (member)
25941 && gnat_encodings != DWARF_GNAT_ENCODINGS_ALL)
25943 vlr_ctx.variant_part_offset = byte_position (member);
25944 gen_variant_part (member, &vlr_ctx, context_die);
25946 else
25948 vlr_ctx.variant_part_offset = NULL_TREE;
25949 gen_decl_die (member, NULL, &vlr_ctx, context_die);
25952 /* For C++ inline static data members emit immediately a DW_TAG_variable
25953 DIE that will refer to that DW_TAG_member/DW_TAG_variable through
25954 DW_AT_specification. */
25955 if (static_inline_p)
25957 int old_extern = DECL_EXTERNAL (member);
25958 DECL_EXTERNAL (member) = 0;
25959 gen_decl_die (member, NULL, NULL, comp_unit_die ());
25960 DECL_EXTERNAL (member) = old_extern;
25965 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
25966 is set, we pretend that the type was never defined, so we only get the
25967 member DIEs needed by later specification DIEs. */
25969 static void
25970 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
25971 enum debug_info_usage usage)
25973 if (TREE_ASM_WRITTEN (type))
25975 /* Fill in the bound of variable-length fields in late dwarf if
25976 still incomplete. */
25977 if (!early_dwarf && variably_modified_type_p (type, NULL))
25978 for (tree member = TYPE_FIELDS (type);
25979 member;
25980 member = DECL_CHAIN (member))
25981 fill_variable_array_bounds (TREE_TYPE (member));
25982 return;
25985 dw_die_ref type_die = lookup_type_die (type);
25986 dw_die_ref scope_die = 0;
25987 bool nested = false;
25988 bool complete = (TYPE_SIZE (type)
25989 && (! TYPE_STUB_DECL (type)
25990 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
25991 bool ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
25992 complete = complete && should_emit_struct_debug (type, usage);
25994 if (type_die && ! complete)
25995 return;
25997 if (TYPE_CONTEXT (type) != NULL_TREE
25998 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
25999 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
26000 nested = true;
26002 scope_die = scope_die_for (type, context_die);
26004 /* Generate child dies for template parameters. */
26005 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
26006 schedule_generic_params_dies_gen (type);
26008 if (! type_die || (nested && is_cu_die (scope_die)))
26009 /* First occurrence of type or toplevel definition of nested class. */
26011 dw_die_ref old_die = type_die;
26013 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
26014 ? record_type_tag (type) : DW_TAG_union_type,
26015 scope_die, type);
26016 equate_type_number_to_die (type, type_die);
26017 if (old_die)
26018 add_AT_specification (type_die, old_die);
26019 else
26020 add_name_attribute (type_die, type_tag (type));
26022 else
26023 remove_AT (type_die, DW_AT_declaration);
26025 /* If this type has been completed, then give it a byte_size attribute and
26026 then give a list of members. */
26027 if (complete && !ns_decl)
26029 /* Prevent infinite recursion in cases where the type of some member of
26030 this type is expressed in terms of this type itself. */
26031 TREE_ASM_WRITTEN (type) = 1;
26032 add_byte_size_attribute (type_die, type);
26033 add_alignment_attribute (type_die, type);
26034 if (TYPE_STUB_DECL (type) != NULL_TREE)
26036 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
26037 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
26040 /* If the first reference to this type was as the return type of an
26041 inline function, then it may not have a parent. Fix this now. */
26042 if (type_die->die_parent == NULL)
26043 add_child_die (scope_die, type_die);
26045 gen_member_die (type, type_die);
26047 add_gnat_descriptive_type_attribute (type_die, type, context_die);
26048 if (TYPE_ARTIFICIAL (type))
26049 add_AT_flag (type_die, DW_AT_artificial, 1);
26051 /* GNU extension: Record what type our vtable lives in. */
26052 if (TYPE_VFIELD (type))
26054 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
26056 gen_type_die (vtype, context_die);
26057 add_AT_die_ref (type_die, DW_AT_containing_type,
26058 lookup_type_die (vtype));
26061 else
26063 add_AT_flag (type_die, DW_AT_declaration, 1);
26065 /* We don't need to do this for function-local types. */
26066 if (TYPE_STUB_DECL (type)
26067 && ! decl_function_context (TYPE_STUB_DECL (type)))
26068 vec_safe_push (incomplete_types, type);
26071 if (get_AT (type_die, DW_AT_name))
26072 add_pubtype (type, type_die);
26075 /* Generate a DIE for a subroutine _type_. */
26077 static void
26078 gen_subroutine_type_die (tree type, dw_die_ref context_die)
26080 tree return_type = TREE_TYPE (type);
26081 dw_die_ref subr_die
26082 = new_die (DW_TAG_subroutine_type,
26083 scope_die_for (type, context_die), type);
26085 equate_type_number_to_die (type, subr_die);
26086 add_prototyped_attribute (subr_die, type);
26087 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
26088 context_die);
26089 add_alignment_attribute (subr_die, type);
26090 gen_formal_types_die (type, subr_die);
26092 if (get_AT (subr_die, DW_AT_name))
26093 add_pubtype (type, subr_die);
26094 if ((dwarf_version >= 5 || !dwarf_strict)
26095 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
26096 add_AT_flag (subr_die, DW_AT_reference, 1);
26097 if ((dwarf_version >= 5 || !dwarf_strict)
26098 && lang_hooks.types.type_dwarf_attribute (type,
26099 DW_AT_rvalue_reference) != -1)
26100 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
26103 /* Generate a DIE for a type definition. */
26105 static void
26106 gen_typedef_die (tree decl, dw_die_ref context_die)
26108 dw_die_ref type_die;
26109 tree type;
26111 if (TREE_ASM_WRITTEN (decl))
26113 if (DECL_ORIGINAL_TYPE (decl))
26114 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
26115 return;
26118 /* As we avoid creating DIEs for local typedefs (see decl_ultimate_origin
26119 checks in process_scope_var and modified_type_die), this should be called
26120 only for original types. */
26121 gcc_assert (decl_ultimate_origin (decl) == NULL
26122 || decl_ultimate_origin (decl) == decl);
26124 TREE_ASM_WRITTEN (decl) = 1;
26125 type_die = new_die (DW_TAG_typedef, context_die, decl);
26127 add_name_and_src_coords_attributes (type_die, decl);
26128 if (DECL_ORIGINAL_TYPE (decl))
26130 type = DECL_ORIGINAL_TYPE (decl);
26131 if (type == error_mark_node)
26132 return;
26134 gcc_assert (type != TREE_TYPE (decl));
26135 equate_type_number_to_die (TREE_TYPE (decl), type_die);
26137 else
26139 type = TREE_TYPE (decl);
26140 if (type == error_mark_node)
26141 return;
26143 if (is_naming_typedef_decl (TYPE_NAME (type)))
26145 /* Here, we are in the case of decl being a typedef naming
26146 an anonymous type, e.g:
26147 typedef struct {...} foo;
26148 In that case TREE_TYPE (decl) is not a typedef variant
26149 type and TYPE_NAME of the anonymous type is set to the
26150 TYPE_DECL of the typedef. This construct is emitted by
26151 the C++ FE.
26153 TYPE is the anonymous struct named by the typedef
26154 DECL. As we need the DW_AT_type attribute of the
26155 DW_TAG_typedef to point to the DIE of TYPE, let's
26156 generate that DIE right away. add_type_attribute
26157 called below will then pick (via lookup_type_die) that
26158 anonymous struct DIE. */
26159 if (!TREE_ASM_WRITTEN (type))
26160 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
26162 /* This is a GNU Extension. We are adding a
26163 DW_AT_linkage_name attribute to the DIE of the
26164 anonymous struct TYPE. The value of that attribute
26165 is the name of the typedef decl naming the anonymous
26166 struct. This greatly eases the work of consumers of
26167 this debug info. */
26168 add_linkage_name_raw (lookup_type_die (type), decl);
26172 add_type_attribute (type_die, type, decl_quals (decl), false,
26173 context_die);
26175 if (is_naming_typedef_decl (decl))
26176 /* We want that all subsequent calls to lookup_type_die with
26177 TYPE in argument yield the DW_TAG_typedef we have just
26178 created. */
26179 equate_type_number_to_die (type, type_die);
26181 add_alignment_attribute (type_die, TREE_TYPE (decl));
26183 add_accessibility_attribute (type_die, decl);
26185 if (DECL_ABSTRACT_P (decl))
26186 equate_decl_number_to_die (decl, type_die);
26188 if (get_AT (type_die, DW_AT_name))
26189 add_pubtype (decl, type_die);
26192 /* Generate a DIE for a struct, class, enum or union type. */
26194 static void
26195 gen_tagged_type_die (tree type,
26196 dw_die_ref context_die,
26197 enum debug_info_usage usage,
26198 bool reverse)
26200 if (type == NULL_TREE
26201 || !is_tagged_type (type))
26202 return;
26204 if (TREE_ASM_WRITTEN (type))
26206 /* If this is a nested type whose containing class hasn't been written
26207 out yet, writing it out will cover this one, too. This does not apply
26208 to instantiations of member class templates; they need to be added to
26209 the containing class as they are generated. FIXME: This hurts the
26210 idea of combining type decls from multiple TUs, since we can't predict
26211 what set of template instantiations we'll get. */
26212 else if (TYPE_CONTEXT (type)
26213 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
26214 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
26216 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
26218 if (TREE_ASM_WRITTEN (type))
26219 return;
26221 /* If that failed, attach ourselves to the stub. */
26222 context_die = lookup_type_die (TYPE_CONTEXT (type));
26224 else if (TYPE_CONTEXT (type) != NULL_TREE
26225 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
26227 /* If this type is local to a function that hasn't been written
26228 out yet, use a NULL context for now; it will be fixed up in
26229 decls_for_scope. */
26230 context_die = lookup_decl_die (TYPE_CONTEXT (type));
26231 /* A declaration DIE doesn't count; nested types need to go in the
26232 specification. */
26233 if (context_die && is_declaration_die (context_die))
26234 context_die = NULL;
26236 else
26237 context_die = declare_in_namespace (type, context_die);
26239 if (TREE_CODE (type) == ENUMERAL_TYPE)
26241 /* This might have been written out by the call to
26242 declare_in_namespace. */
26243 if (!TREE_ASM_WRITTEN (type) || reverse)
26244 gen_enumeration_type_die (type, context_die, reverse);
26246 else
26247 gen_struct_or_union_type_die (type, context_die, usage);
26249 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
26250 it up if it is ever completed. gen_*_type_die will set it for us
26251 when appropriate. */
26254 /* Generate a type description DIE. */
26256 static void
26257 gen_type_die_with_usage (tree type, dw_die_ref context_die,
26258 enum debug_info_usage usage, bool reverse)
26260 struct array_descr_info info;
26262 if (type == NULL_TREE || type == error_mark_node)
26263 return;
26265 if (flag_checking && type)
26266 verify_type (type);
26268 if (TYPE_NAME (type) != NULL_TREE
26269 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
26270 && is_redundant_typedef (TYPE_NAME (type))
26271 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
26272 /* The DECL of this type is a typedef we don't want to emit debug
26273 info for but we want debug info for its underlying typedef.
26274 This can happen for e.g, the injected-class-name of a C++
26275 type. */
26276 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
26278 /* If TYPE is a typedef type variant, let's generate debug info
26279 for the parent typedef which TYPE is a type of. */
26280 if (typedef_variant_p (type))
26282 if (TREE_ASM_WRITTEN (type))
26283 return;
26285 tree name = TYPE_NAME (type);
26286 tree origin = decl_ultimate_origin (name);
26287 if (origin != NULL && origin != name)
26289 gen_decl_die (origin, NULL, NULL, context_die);
26290 return;
26293 /* Prevent broken recursion; we can't hand off to the same type. */
26294 gcc_assert (DECL_ORIGINAL_TYPE (name) != type);
26296 /* Give typedefs the right scope. */
26297 context_die = scope_die_for (type, context_die);
26299 TREE_ASM_WRITTEN (type) = 1;
26301 gen_decl_die (name, NULL, NULL, context_die);
26302 return;
26305 /* If type is an anonymous tagged type named by a typedef, let's
26306 generate debug info for the typedef. */
26307 if (is_naming_typedef_decl (TYPE_NAME (type)))
26309 /* Give typedefs the right scope. */
26310 context_die = scope_die_for (type, context_die);
26312 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
26313 return;
26316 if (lang_hooks.types.get_debug_type)
26318 tree debug_type = lang_hooks.types.get_debug_type (type);
26320 if (debug_type != NULL_TREE && debug_type != type)
26322 gen_type_die_with_usage (debug_type, context_die, usage, reverse);
26323 return;
26327 /* We are going to output a DIE to represent the unqualified version
26328 of this type (i.e. without any const or volatile qualifiers) so
26329 get the main variant (i.e. the unqualified version) of this type
26330 now. (Vectors and arrays are special because the debugging info is in the
26331 cloned type itself. Similarly function/method types can contain extra
26332 ref-qualification). */
26333 if (FUNC_OR_METHOD_TYPE_P (type))
26335 /* For function/method types, can't use type_main_variant here,
26336 because that can have different ref-qualifiers for C++,
26337 but try to canonicalize. */
26338 tree main = TYPE_MAIN_VARIANT (type);
26339 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
26340 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
26341 && check_base_type (t, main)
26342 && check_lang_type (t, type))
26344 type = t;
26345 break;
26348 else if (TREE_CODE (type) != VECTOR_TYPE
26349 && TREE_CODE (type) != ARRAY_TYPE)
26350 type = type_main_variant (type);
26352 /* If this is an array type with hidden descriptor, handle it first. */
26353 if (!TREE_ASM_WRITTEN (type)
26354 && lang_hooks.types.get_array_descr_info)
26356 memset (&info, 0, sizeof (info));
26357 if (lang_hooks.types.get_array_descr_info (type, &info))
26359 /* Fortran sometimes emits array types with no dimension. */
26360 gcc_assert (info.ndimensions >= 0
26361 && (info.ndimensions
26362 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
26363 gen_descr_array_type_die (type, &info, context_die);
26364 TREE_ASM_WRITTEN (type) = 1;
26365 return;
26369 if (TREE_ASM_WRITTEN (type) && !reverse)
26371 /* Variable-length types may be incomplete even if
26372 TREE_ASM_WRITTEN. For such types, fall through to
26373 gen_array_type_die() and possibly fill in
26374 DW_AT_{upper,lower}_bound attributes. */
26375 if ((TREE_CODE (type) != ARRAY_TYPE
26376 && TREE_CODE (type) != RECORD_TYPE
26377 && TREE_CODE (type) != UNION_TYPE
26378 && TREE_CODE (type) != QUAL_UNION_TYPE)
26379 || !variably_modified_type_p (type, NULL))
26380 return;
26383 switch (TREE_CODE (type))
26385 case ERROR_MARK:
26386 break;
26388 case POINTER_TYPE:
26389 case REFERENCE_TYPE:
26390 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
26391 ensures that the gen_type_die recursion will terminate even if the
26392 type is recursive. Recursive types are possible in Ada. */
26393 /* ??? We could perhaps do this for all types before the switch
26394 statement. */
26395 TREE_ASM_WRITTEN (type) = 1;
26397 /* For these types, all that is required is that we output a DIE (or a
26398 set of DIEs) to represent the "basis" type. */
26399 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26400 DINFO_USAGE_IND_USE);
26401 break;
26403 case OFFSET_TYPE:
26404 /* This code is used for C++ pointer-to-data-member types.
26405 Output a description of the relevant class type. */
26406 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
26407 DINFO_USAGE_IND_USE);
26409 /* Output a description of the type of the object pointed to. */
26410 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26411 DINFO_USAGE_IND_USE);
26413 /* Now output a DIE to represent this pointer-to-data-member type
26414 itself. */
26415 gen_ptr_to_mbr_type_die (type, context_die);
26416 break;
26418 case FUNCTION_TYPE:
26419 /* Force out return type (in case it wasn't forced out already). */
26420 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26421 DINFO_USAGE_DIR_USE);
26422 gen_subroutine_type_die (type, context_die);
26423 break;
26425 case METHOD_TYPE:
26426 /* Force out return type (in case it wasn't forced out already). */
26427 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26428 DINFO_USAGE_DIR_USE);
26429 gen_subroutine_type_die (type, context_die);
26430 break;
26432 case ARRAY_TYPE:
26433 case VECTOR_TYPE:
26434 gen_array_type_die (type, context_die);
26435 break;
26437 case ENUMERAL_TYPE:
26438 case RECORD_TYPE:
26439 case UNION_TYPE:
26440 case QUAL_UNION_TYPE:
26441 gen_tagged_type_die (type, context_die, usage, reverse);
26442 return;
26444 case VOID_TYPE:
26445 case OPAQUE_TYPE:
26446 case INTEGER_TYPE:
26447 case REAL_TYPE:
26448 case FIXED_POINT_TYPE:
26449 case COMPLEX_TYPE:
26450 case BOOLEAN_TYPE:
26451 case BITINT_TYPE:
26452 /* No DIEs needed for fundamental types. */
26453 break;
26455 case NULLPTR_TYPE:
26456 case LANG_TYPE:
26457 /* Just use DW_TAG_unspecified_type. */
26459 dw_die_ref type_die = lookup_type_die (type);
26460 if (type_die == NULL)
26462 tree name = TYPE_IDENTIFIER (type);
26463 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
26464 type);
26465 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
26466 equate_type_number_to_die (type, type_die);
26469 break;
26471 default:
26472 if (is_cxx_auto (type))
26474 tree name = TYPE_IDENTIFIER (type);
26475 dw_die_ref *die = (name == get_identifier ("auto")
26476 ? &auto_die : &decltype_auto_die);
26477 if (!*die)
26479 *die = new_die (DW_TAG_unspecified_type,
26480 comp_unit_die (), NULL_TREE);
26481 add_name_attribute (*die, IDENTIFIER_POINTER (name));
26483 equate_type_number_to_die (type, *die);
26484 break;
26486 gcc_unreachable ();
26489 TREE_ASM_WRITTEN (type) = 1;
26492 static void
26493 gen_type_die (tree type, dw_die_ref context_die, bool reverse)
26495 if (type != error_mark_node)
26497 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE, reverse);
26498 if (flag_checking)
26500 dw_die_ref die = lookup_type_die (type);
26501 if (die)
26502 check_die (die);
26507 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
26508 things which are local to the given block. */
26510 static void
26511 gen_block_die (tree stmt, dw_die_ref context_die)
26513 int must_output_die = 0;
26514 bool inlined_func;
26516 /* Ignore blocks that are NULL. */
26517 if (stmt == NULL_TREE)
26518 return;
26520 inlined_func = inlined_function_outer_scope_p (stmt);
26522 /* If the block is one fragment of a non-contiguous block, do not
26523 process the variables, since they will have been done by the
26524 origin block. Do process subblocks. */
26525 if (BLOCK_FRAGMENT_ORIGIN (stmt))
26527 tree sub;
26529 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
26530 gen_block_die (sub, context_die);
26532 return;
26535 /* Determine if we need to output any Dwarf DIEs at all to represent this
26536 block. */
26537 if (inlined_func)
26538 /* The outer scopes for inlinings *must* always be represented. We
26539 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
26540 must_output_die = 1;
26541 else if (lookup_block_die (stmt))
26542 /* If we already have a DIE then it was filled early. Meanwhile
26543 we might have pruned all BLOCK_VARS as optimized out but we
26544 still want to generate high/low PC attributes so output it. */
26545 must_output_die = 1;
26546 else if (TREE_USED (stmt)
26547 || TREE_ASM_WRITTEN (stmt))
26549 /* Determine if this block directly contains any "significant"
26550 local declarations which we will need to output DIEs for. */
26551 if (debug_info_level > DINFO_LEVEL_TERSE)
26553 /* We are not in terse mode so any local declaration that
26554 is not ignored for debug purposes counts as being a
26555 "significant" one. */
26556 if (BLOCK_NUM_NONLOCALIZED_VARS (stmt))
26557 must_output_die = 1;
26558 else
26559 for (tree var = BLOCK_VARS (stmt); var; var = DECL_CHAIN (var))
26560 if (!DECL_IGNORED_P (var))
26562 must_output_die = 1;
26563 break;
26566 else if (!dwarf2out_ignore_block (stmt))
26567 must_output_die = 1;
26570 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
26571 DIE for any block which contains no significant local declarations at
26572 all. Rather, in such cases we just call `decls_for_scope' so that any
26573 needed Dwarf info for any sub-blocks will get properly generated. Note
26574 that in terse mode, our definition of what constitutes a "significant"
26575 local declaration gets restricted to include only inlined function
26576 instances and local (nested) function definitions. */
26577 if (must_output_die)
26579 if (inlined_func)
26580 gen_inlined_subroutine_die (stmt, context_die);
26581 else
26582 gen_lexical_block_die (stmt, context_die);
26584 else
26585 decls_for_scope (stmt, context_die);
26588 /* Process variable DECL (or variable with origin ORIGIN) within
26589 block STMT and add it to CONTEXT_DIE. */
26590 static void
26591 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
26593 dw_die_ref die;
26594 tree decl_or_origin = decl ? decl : origin;
26596 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
26597 die = lookup_decl_die (decl_or_origin);
26598 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
26600 if (TYPE_DECL_IS_STUB (decl_or_origin))
26601 die = lookup_type_die (TREE_TYPE (decl_or_origin));
26602 else
26603 die = lookup_decl_die (decl_or_origin);
26604 /* Avoid re-creating the DIE late if it was optimized as unused early. */
26605 if (! die && ! early_dwarf)
26606 return;
26608 else
26609 die = NULL;
26611 /* Avoid creating DIEs for local typedefs and concrete static variables that
26612 will only be pruned later. */
26613 if ((origin || decl_ultimate_origin (decl))
26614 && (TREE_CODE (decl_or_origin) == TYPE_DECL
26615 || (VAR_P (decl_or_origin) && TREE_STATIC (decl_or_origin))))
26617 origin = decl_ultimate_origin (decl_or_origin);
26618 if (decl && VAR_P (decl) && die != NULL)
26620 die = lookup_decl_die (origin);
26621 if (die != NULL)
26622 equate_decl_number_to_die (decl, die);
26624 return;
26627 if (die != NULL && die->die_parent == NULL)
26628 add_child_die (context_die, die);
26630 if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
26632 if (early_dwarf)
26633 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
26634 stmt, context_die);
26636 else
26638 if (decl && DECL_P (decl))
26640 die = lookup_decl_die (decl);
26642 /* Early created DIEs do not have a parent as the decls refer
26643 to the function as DECL_CONTEXT rather than the BLOCK. */
26644 if (die && die->die_parent == NULL)
26646 gcc_assert (in_lto_p);
26647 add_child_die (context_die, die);
26651 gen_decl_die (decl, origin, NULL, context_die);
26655 /* Generate all of the decls declared within a given scope and (recursively)
26656 all of its sub-blocks. */
26658 static void
26659 decls_for_scope (tree stmt, dw_die_ref context_die, bool recurse)
26661 tree decl;
26662 unsigned int i;
26663 tree subblocks;
26665 /* Ignore NULL blocks. */
26666 if (stmt == NULL_TREE)
26667 return;
26669 /* Output the DIEs to represent all of the data objects and typedefs
26670 declared directly within this block but not within any nested
26671 sub-blocks. Also, nested function and tag DIEs have been
26672 generated with a parent of NULL; fix that up now. We don't
26673 have to do this if we're at -g1. */
26674 if (debug_info_level > DINFO_LEVEL_TERSE)
26676 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
26677 process_scope_var (stmt, decl, NULL_TREE, context_die);
26678 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
26679 origin - avoid doing this twice as we have no good way to see
26680 if we've done it once already. */
26681 if (! early_dwarf)
26682 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
26684 decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
26685 if (decl == current_function_decl)
26686 /* Ignore declarations of the current function, while they
26687 are declarations, gen_subprogram_die would treat them
26688 as definitions again, because they are equal to
26689 current_function_decl and endlessly recurse. */;
26690 else if (TREE_CODE (decl) == FUNCTION_DECL)
26691 process_scope_var (stmt, decl, NULL_TREE, context_die);
26692 else
26693 process_scope_var (stmt, NULL_TREE, decl, context_die);
26697 /* Even if we're at -g1, we need to process the subblocks in order to get
26698 inlined call information. */
26700 /* Output the DIEs to represent all sub-blocks (and the items declared
26701 therein) of this block. */
26702 if (recurse)
26703 for (subblocks = BLOCK_SUBBLOCKS (stmt);
26704 subblocks != NULL;
26705 subblocks = BLOCK_CHAIN (subblocks))
26706 gen_block_die (subblocks, context_die);
26709 /* Is this a typedef we can avoid emitting? */
26711 static bool
26712 is_redundant_typedef (const_tree decl)
26714 if (TYPE_DECL_IS_STUB (decl))
26715 return true;
26717 if (DECL_ARTIFICIAL (decl)
26718 && DECL_CONTEXT (decl)
26719 && is_tagged_type (DECL_CONTEXT (decl))
26720 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
26721 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
26722 /* Also ignore the artificial member typedef for the class name. */
26723 return true;
26725 return false;
26728 /* Return TRUE if TYPE is a typedef that names a type for linkage
26729 purposes. This kind of typedefs is produced by the C++ FE for
26730 constructs like:
26732 typedef struct {...} foo;
26734 In that case, there is no typedef variant type produced for foo.
26735 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
26736 struct type. */
26738 static bool
26739 is_naming_typedef_decl (const_tree decl)
26741 if (decl == NULL_TREE
26742 || TREE_CODE (decl) != TYPE_DECL
26743 || DECL_NAMELESS (decl)
26744 || !is_tagged_type (TREE_TYPE (decl))
26745 || DECL_IS_UNDECLARED_BUILTIN (decl)
26746 || is_redundant_typedef (decl)
26747 /* It looks like Ada produces TYPE_DECLs that are very similar
26748 to C++ naming typedefs but that have different
26749 semantics. Let's be specific to c++ for now. */
26750 || !is_cxx (decl))
26751 return false;
26753 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
26754 && TYPE_NAME (TREE_TYPE (decl)) == decl
26755 && (TYPE_STUB_DECL (TREE_TYPE (decl))
26756 != TYPE_NAME (TREE_TYPE (decl))));
26759 /* Looks up the DIE for a context. */
26761 static inline dw_die_ref
26762 lookup_context_die (tree context)
26764 if (context)
26766 /* Find die that represents this context. */
26767 if (TYPE_P (context))
26769 context = TYPE_MAIN_VARIANT (context);
26770 dw_die_ref ctx = lookup_type_die (context);
26771 if (!ctx)
26772 return NULL;
26773 return strip_naming_typedef (context, ctx);
26775 else
26776 return lookup_decl_die (context);
26778 return comp_unit_die ();
26781 /* Returns the DIE for a context. */
26783 static inline dw_die_ref
26784 get_context_die (tree context)
26786 if (context)
26788 /* Find die that represents this context. */
26789 if (TYPE_P (context))
26791 context = TYPE_MAIN_VARIANT (context);
26792 return strip_naming_typedef (context, force_type_die (context));
26794 else
26795 return force_decl_die (context);
26797 return comp_unit_die ();
26800 /* Returns the DIE for decl. A DIE will always be returned. */
26802 static dw_die_ref
26803 force_decl_die (tree decl)
26805 dw_die_ref decl_die;
26806 unsigned saved_external_flag;
26807 tree save_fn = NULL_TREE;
26808 decl_die = lookup_decl_die (decl);
26809 if (!decl_die)
26811 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
26813 decl_die = lookup_decl_die (decl);
26814 if (decl_die)
26815 return decl_die;
26817 switch (TREE_CODE (decl))
26819 case FUNCTION_DECL:
26820 /* Clear current_function_decl, so that gen_subprogram_die thinks
26821 that this is a declaration. At this point, we just want to force
26822 declaration die. */
26823 save_fn = current_function_decl;
26824 current_function_decl = NULL_TREE;
26825 gen_subprogram_die (decl, context_die);
26826 current_function_decl = save_fn;
26827 break;
26829 case VAR_DECL:
26830 /* Set external flag to force declaration die. Restore it after
26831 gen_decl_die() call. */
26832 saved_external_flag = DECL_EXTERNAL (decl);
26833 DECL_EXTERNAL (decl) = 1;
26834 gen_decl_die (decl, NULL, NULL, context_die);
26835 DECL_EXTERNAL (decl) = saved_external_flag;
26836 break;
26838 case NAMESPACE_DECL:
26839 if (dwarf_version >= 3 || !dwarf_strict)
26840 dwarf2out_decl (decl);
26841 else
26842 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
26843 decl_die = comp_unit_die ();
26844 break;
26846 case CONST_DECL:
26847 /* Enumerators shouldn't need force_decl_die. */
26848 gcc_assert (DECL_CONTEXT (decl) == NULL_TREE
26849 || TREE_CODE (DECL_CONTEXT (decl)) != ENUMERAL_TYPE);
26850 gen_decl_die (decl, NULL, NULL, context_die);
26851 break;
26853 case TRANSLATION_UNIT_DECL:
26854 decl_die = comp_unit_die ();
26855 break;
26857 default:
26858 gcc_unreachable ();
26861 /* We should be able to find the DIE now. */
26862 if (!decl_die)
26863 decl_die = lookup_decl_die (decl);
26864 gcc_assert (decl_die);
26867 return decl_die;
26870 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
26871 always returned. */
26873 static dw_die_ref
26874 force_type_die (tree type)
26876 dw_die_ref type_die;
26878 type_die = lookup_type_die (type);
26879 if (!type_die)
26881 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
26883 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
26884 false, context_die);
26885 gcc_assert (type_die);
26887 return type_die;
26890 /* Force out any required namespaces to be able to output DECL,
26891 and return the new context_die for it, if it's changed. */
26893 static dw_die_ref
26894 setup_namespace_context (tree thing, dw_die_ref context_die)
26896 tree context = (DECL_P (thing)
26897 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
26898 if (context && TREE_CODE (context) == NAMESPACE_DECL)
26899 /* Force out the namespace. */
26900 context_die = force_decl_die (context);
26902 return context_die;
26905 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
26906 type) within its namespace, if appropriate.
26908 For compatibility with older debuggers, namespace DIEs only contain
26909 declarations; all definitions are emitted at CU scope, with
26910 DW_AT_specification pointing to the declaration (like with class
26911 members). */
26913 static dw_die_ref
26914 declare_in_namespace (tree thing, dw_die_ref context_die)
26916 dw_die_ref ns_context;
26918 if (debug_info_level <= DINFO_LEVEL_TERSE)
26919 return context_die;
26921 /* External declarations in the local scope only need to be emitted
26922 once, not once in the namespace and once in the scope.
26924 This avoids declaring the `extern' below in the
26925 namespace DIE as well as in the innermost scope:
26927 namespace S
26929 int i=5;
26930 int foo()
26932 int i=8;
26933 extern int i;
26934 return i;
26938 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
26939 return context_die;
26941 /* If this decl is from an inlined function, then don't try to emit it in its
26942 namespace, as we will get confused. It would have already been emitted
26943 when the abstract instance of the inline function was emitted anyways. */
26944 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
26945 return context_die;
26947 ns_context = setup_namespace_context (thing, context_die);
26949 if (ns_context != context_die)
26951 if (is_fortran () || is_dlang ())
26952 return ns_context;
26953 if (DECL_P (thing))
26954 gen_decl_die (thing, NULL, NULL, ns_context);
26955 else
26956 gen_type_die (thing, ns_context);
26958 return context_die;
26961 /* Generate a DIE for a namespace or namespace alias. */
26963 static void
26964 gen_namespace_die (tree decl, dw_die_ref context_die)
26966 dw_die_ref namespace_die;
26968 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
26969 they are an alias of. */
26970 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
26972 /* Output a real namespace or module. */
26973 context_die = setup_namespace_context (decl, comp_unit_die ());
26974 namespace_die = new_die (is_fortran () || is_dlang ()
26975 ? DW_TAG_module : DW_TAG_namespace,
26976 context_die, decl);
26977 /* For Fortran modules defined in different CU don't add src coords. */
26978 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
26980 const char *name = dwarf2_name (decl, 0);
26981 if (name)
26982 add_name_attribute (namespace_die, name);
26984 else
26985 add_name_and_src_coords_attributes (namespace_die, decl);
26986 if (DECL_EXTERNAL (decl))
26987 add_AT_flag (namespace_die, DW_AT_declaration, 1);
26988 equate_decl_number_to_die (decl, namespace_die);
26990 else
26992 /* Output a namespace alias. */
26994 /* Force out the namespace we are an alias of, if necessary. */
26995 dw_die_ref origin_die
26996 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
26998 if (DECL_FILE_SCOPE_P (decl)
26999 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
27000 context_die = setup_namespace_context (decl, comp_unit_die ());
27001 /* Now create the namespace alias DIE. */
27002 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
27003 add_name_and_src_coords_attributes (namespace_die, decl);
27004 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
27005 equate_decl_number_to_die (decl, namespace_die);
27007 if ((dwarf_version >= 5 || !dwarf_strict)
27008 && lang_hooks.decls.decl_dwarf_attribute (decl,
27009 DW_AT_export_symbols) == 1)
27010 add_AT_flag (namespace_die, DW_AT_export_symbols, 1);
27012 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
27013 if (want_pubnames ())
27014 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
27017 /* Generate Dwarf debug information for a decl described by DECL.
27018 The return value is currently only meaningful for PARM_DECLs,
27019 for all other decls it returns NULL.
27021 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
27022 It can be NULL otherwise. */
27024 static dw_die_ref
27025 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
27026 dw_die_ref context_die)
27028 tree decl_or_origin = decl ? decl : origin;
27029 tree class_origin = NULL, ultimate_origin;
27031 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
27032 return NULL;
27034 switch (TREE_CODE (decl_or_origin))
27036 case ERROR_MARK:
27037 break;
27039 case CONST_DECL:
27040 if (!is_fortran () && !is_ada () && !is_dlang ())
27042 /* The individual enumerators of an enum type get output when we output
27043 the Dwarf representation of the relevant enum type itself. */
27044 break;
27047 /* Emit its type. */
27048 gen_type_die (TREE_TYPE (decl), context_die);
27050 /* And its containing namespace. */
27051 context_die = declare_in_namespace (decl, context_die);
27053 gen_const_die (decl, context_die);
27054 break;
27056 case FUNCTION_DECL:
27057 #if 0
27058 /* FIXME */
27059 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
27060 on local redeclarations of global functions. That seems broken. */
27061 if (current_function_decl != decl)
27062 /* This is only a declaration. */;
27063 #endif
27065 /* We should have abstract copies already and should not generate
27066 stray type DIEs in late LTO dumping. */
27067 if (! early_dwarf)
27070 /* If we're emitting a clone, emit info for the abstract instance. */
27071 else if (origin || DECL_ORIGIN (decl) != decl)
27072 dwarf2out_abstract_function (origin
27073 ? DECL_ORIGIN (origin)
27074 : DECL_ABSTRACT_ORIGIN (decl));
27076 /* If we're emitting a possibly inlined function emit it as
27077 abstract instance. */
27078 else if (cgraph_function_possibly_inlined_p (decl)
27079 && ! DECL_ABSTRACT_P (decl)
27080 && ! class_or_namespace_scope_p (context_die)
27081 /* dwarf2out_abstract_function won't emit a die if this is just
27082 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
27083 that case, because that works only if we have a die. */
27084 && DECL_INITIAL (decl) != NULL_TREE)
27085 dwarf2out_abstract_function (decl);
27087 /* Otherwise we're emitting the primary DIE for this decl. */
27088 else if (debug_info_level > DINFO_LEVEL_TERSE)
27090 /* Before we describe the FUNCTION_DECL itself, make sure that we
27091 have its containing type. */
27092 if (!origin)
27093 origin = decl_class_context (decl);
27094 if (origin != NULL_TREE)
27095 gen_type_die (origin, context_die);
27097 /* And its return type. */
27098 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
27100 /* And its virtual context. */
27101 if (DECL_VINDEX (decl) != NULL_TREE)
27102 gen_type_die (DECL_CONTEXT (decl), context_die);
27104 /* Make sure we have a member DIE for decl. */
27105 if (origin != NULL_TREE)
27106 gen_type_die_for_member (origin, decl, context_die);
27108 /* And its containing namespace. */
27109 context_die = declare_in_namespace (decl, context_die);
27112 /* Now output a DIE to represent the function itself. */
27113 if (decl)
27114 gen_subprogram_die (decl, context_die);
27115 break;
27117 case TYPE_DECL:
27118 /* If we are in terse mode, don't generate any DIEs to represent any
27119 actual typedefs. */
27120 if (debug_info_level <= DINFO_LEVEL_TERSE)
27121 break;
27123 /* In the special case of a TYPE_DECL node representing the declaration
27124 of some type tag, if the given TYPE_DECL is marked as having been
27125 instantiated from some other (original) TYPE_DECL node (e.g. one which
27126 was generated within the original definition of an inline function) we
27127 used to generate a special (abbreviated) DW_TAG_structure_type,
27128 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
27129 should be actually referencing those DIEs, as variable DIEs with that
27130 type would be emitted already in the abstract origin, so it was always
27131 removed during unused type prunning. Don't add anything in this
27132 case. */
27133 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
27134 break;
27136 if (is_redundant_typedef (decl))
27137 gen_type_die (TREE_TYPE (decl), context_die);
27138 else
27139 /* Output a DIE to represent the typedef itself. */
27140 gen_typedef_die (decl, context_die);
27141 break;
27143 case LABEL_DECL:
27144 if (debug_info_level >= DINFO_LEVEL_NORMAL)
27145 gen_label_die (decl, context_die);
27146 break;
27148 case VAR_DECL:
27149 case RESULT_DECL:
27150 /* If we are in terse mode, don't generate any DIEs to represent any
27151 variable declarations or definitions unless it is external. */
27152 if (debug_info_level < DINFO_LEVEL_TERSE
27153 || (debug_info_level == DINFO_LEVEL_TERSE
27154 && !TREE_PUBLIC (decl_or_origin)))
27155 break;
27157 if (debug_info_level > DINFO_LEVEL_TERSE)
27159 /* Avoid generating stray type DIEs during late dwarf dumping.
27160 All types have been dumped early. */
27161 if (early_dwarf
27162 /* ??? But in LTRANS we cannot annotate early created variably
27163 modified type DIEs without copying them and adjusting all
27164 references to them. Dump them again as happens for inlining
27165 which copies both the decl and the types. */
27166 /* ??? And even non-LTO needs to re-visit type DIEs to fill
27167 in VLA bound information for example. */
27168 || (decl && variably_modified_type_p (TREE_TYPE (decl),
27169 current_function_decl)))
27171 /* Output any DIEs that are needed to specify the type of this data
27172 object. */
27173 if (decl_by_reference_p (decl_or_origin))
27174 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
27175 else
27176 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
27179 if (early_dwarf)
27181 /* And its containing type. */
27182 class_origin = decl_class_context (decl_or_origin);
27183 if (class_origin != NULL_TREE)
27184 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
27186 /* And its containing namespace. */
27187 context_die = declare_in_namespace (decl_or_origin, context_die);
27191 /* Now output the DIE to represent the data object itself. This gets
27192 complicated because of the possibility that the VAR_DECL really
27193 represents an inlined instance of a formal parameter for an inline
27194 function. */
27195 ultimate_origin = decl_ultimate_origin (decl_or_origin);
27196 if (ultimate_origin != NULL_TREE
27197 && TREE_CODE (ultimate_origin) == PARM_DECL)
27198 gen_formal_parameter_die (decl, origin,
27199 true /* Emit name attribute. */,
27200 context_die);
27201 else
27202 gen_variable_die (decl, origin, context_die);
27203 break;
27205 case FIELD_DECL:
27206 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
27207 /* Ignore the nameless fields that are used to skip bits but handle C++
27208 anonymous unions and structs. */
27209 if (DECL_NAME (decl) != NULL_TREE
27210 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
27211 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
27213 gen_type_die (member_declared_type (decl), context_die);
27214 gen_field_die (decl, ctx, context_die);
27216 break;
27218 case PARM_DECL:
27219 /* Avoid generating stray type DIEs during late dwarf dumping.
27220 All types have been dumped early. */
27221 if (early_dwarf
27222 /* ??? But in LTRANS we cannot annotate early created variably
27223 modified type DIEs without copying them and adjusting all
27224 references to them. Dump them again as happens for inlining
27225 which copies both the decl and the types. */
27226 /* ??? And even non-LTO needs to re-visit type DIEs to fill
27227 in VLA bound information for example. */
27228 || (decl && variably_modified_type_p (TREE_TYPE (decl),
27229 current_function_decl)))
27231 if (DECL_BY_REFERENCE (decl_or_origin))
27232 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
27233 else
27234 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
27236 return gen_formal_parameter_die (decl, origin,
27237 true /* Emit name attribute. */,
27238 context_die);
27240 case NAMESPACE_DECL:
27241 if (dwarf_version >= 3 || !dwarf_strict)
27242 gen_namespace_die (decl, context_die);
27243 break;
27245 case IMPORTED_DECL:
27246 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
27247 DECL_CONTEXT (decl), context_die);
27248 break;
27250 case NAMELIST_DECL:
27251 gen_namelist_decl (DECL_NAME (decl), context_die,
27252 NAMELIST_DECL_ASSOCIATED_DECL (decl));
27253 break;
27255 default:
27256 /* Probably some frontend-internal decl. Assume we don't care. */
27257 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
27258 break;
27261 return NULL;
27264 /* Output initial debug information for global DECL. Called at the
27265 end of the parsing process.
27267 This is the initial debug generation process. As such, the DIEs
27268 generated may be incomplete. A later debug generation pass
27269 (dwarf2out_late_global_decl) will augment the information generated
27270 in this pass (e.g., with complete location info). */
27272 static void
27273 dwarf2out_early_global_decl (tree decl)
27275 set_early_dwarf s;
27277 /* gen_decl_die() will set DECL_ABSTRACT because
27278 cgraph_function_possibly_inlined_p() returns true. This is in
27279 turn will cause DW_AT_inline attributes to be set.
27281 This happens because at early dwarf generation, there is no
27282 cgraph information, causing cgraph_function_possibly_inlined_p()
27283 to return true. Trick cgraph_function_possibly_inlined_p()
27284 while we generate dwarf early. */
27285 bool save = symtab->global_info_ready;
27286 symtab->global_info_ready = true;
27288 /* We don't handle TYPE_DECLs. If required, they'll be reached via
27289 other DECLs and they can point to template types or other things
27290 that dwarf2out can't handle when done via dwarf2out_decl. */
27291 if (TREE_CODE (decl) != TYPE_DECL
27292 && TREE_CODE (decl) != PARM_DECL)
27294 if (TREE_CODE (decl) == FUNCTION_DECL)
27296 tree save_fndecl = current_function_decl;
27298 /* For nested functions, make sure we have DIEs for the parents first
27299 so that all nested DIEs are generated at the proper scope in the
27300 first shot. */
27301 tree context = decl_function_context (decl);
27302 if (context != NULL)
27304 dw_die_ref context_die = lookup_decl_die (context);
27305 current_function_decl = context;
27307 /* Avoid emitting DIEs multiple times, but still process CONTEXT
27308 enough so that it lands in its own context. This avoids type
27309 pruning issues later on. */
27310 if (context_die == NULL || is_declaration_die (context_die))
27311 dwarf2out_early_global_decl (context);
27314 /* Emit an abstract origin of a function first. This happens
27315 with C++ constructor clones for example and makes
27316 dwarf2out_abstract_function happy which requires the early
27317 DIE of the abstract instance to be present. */
27318 tree origin = DECL_ABSTRACT_ORIGIN (decl);
27319 dw_die_ref origin_die;
27320 if (origin != NULL
27321 /* Do not emit the DIE multiple times but make sure to
27322 process it fully here in case we just saw a declaration. */
27323 && ((origin_die = lookup_decl_die (origin)) == NULL
27324 || is_declaration_die (origin_die)))
27326 current_function_decl = origin;
27327 dwarf2out_decl (origin);
27330 /* Emit the DIE for decl but avoid doing that multiple times. */
27331 dw_die_ref old_die;
27332 if ((old_die = lookup_decl_die (decl)) == NULL
27333 || is_declaration_die (old_die))
27335 current_function_decl = decl;
27336 dwarf2out_decl (decl);
27339 current_function_decl = save_fndecl;
27341 else
27342 dwarf2out_decl (decl);
27344 symtab->global_info_ready = save;
27347 /* Return whether EXPR is an expression with the following pattern:
27348 INDIRECT_REF (NOP_EXPR (INTEGER_CST)). */
27350 static bool
27351 is_trivial_indirect_ref (tree expr)
27353 if (expr == NULL_TREE || TREE_CODE (expr) != INDIRECT_REF)
27354 return false;
27356 tree nop = TREE_OPERAND (expr, 0);
27357 if (nop == NULL_TREE || TREE_CODE (nop) != NOP_EXPR)
27358 return false;
27360 tree int_cst = TREE_OPERAND (nop, 0);
27361 return int_cst != NULL_TREE && TREE_CODE (int_cst) == INTEGER_CST;
27364 /* Output debug information for global decl DECL. Called from
27365 toplev.cc after compilation proper has finished. */
27367 static void
27368 dwarf2out_late_global_decl (tree decl)
27370 /* Fill-in any location information we were unable to determine
27371 on the first pass. */
27372 if (VAR_P (decl))
27374 dw_die_ref die = lookup_decl_die (decl);
27376 /* We may have to generate full debug late for LTO in case debug
27377 was not enabled at compile-time or the target doesn't support
27378 the LTO early debug scheme. */
27379 if (! die && in_lto_p
27380 /* Function scope variables are emitted when emitting the
27381 DIE for the function. */
27382 && ! local_function_static (decl))
27383 dwarf2out_decl (decl);
27384 else if (die)
27386 /* We get called via the symtab code invoking late_global_decl
27387 for symbols that are optimized out.
27389 Do not add locations for those, except if they have a
27390 DECL_VALUE_EXPR, in which case they are relevant for debuggers.
27391 Still don't add a location if the DECL_VALUE_EXPR is not a trivial
27392 INDIRECT_REF expression, as this could generate relocations to
27393 text symbols in LTO object files, which is invalid. */
27394 varpool_node *node = varpool_node::get (decl);
27395 if ((! node || ! node->definition)
27396 && ! (DECL_HAS_VALUE_EXPR_P (decl)
27397 && is_trivial_indirect_ref (DECL_VALUE_EXPR (decl))))
27398 tree_add_const_value_attribute_for_decl (die, decl);
27399 else
27400 add_location_or_const_value_attribute (die, decl, false);
27405 /* Output debug information for type decl DECL. Called from toplev.cc
27406 and from language front ends (to record built-in types). */
27407 static void
27408 dwarf2out_type_decl (tree decl, int local)
27410 if (!local)
27412 set_early_dwarf s;
27413 dwarf2out_decl (decl);
27417 /* Output debug information for imported module or decl DECL.
27418 NAME is non-NULL name in the lexical block if the decl has been renamed.
27419 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
27420 that DECL belongs to.
27421 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
27422 static void
27423 dwarf2out_imported_module_or_decl_1 (tree decl,
27424 tree name,
27425 tree lexical_block,
27426 dw_die_ref lexical_block_die)
27428 expanded_location xloc;
27429 dw_die_ref imported_die = NULL;
27430 dw_die_ref at_import_die;
27432 if (TREE_CODE (decl) == IMPORTED_DECL)
27434 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
27435 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
27436 gcc_assert (decl);
27438 else
27439 xloc = expand_location (input_location);
27441 if (TREE_CODE (decl) == TYPE_DECL)
27443 at_import_die = force_type_die (TREE_TYPE (decl));
27444 /* For namespace N { typedef void T; } using N::T; base_type_die
27445 returns NULL, but DW_TAG_imported_declaration requires
27446 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
27447 if (!at_import_die)
27449 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
27450 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
27451 at_import_die = lookup_type_die (TREE_TYPE (decl));
27452 gcc_assert (at_import_die);
27455 else
27457 at_import_die = lookup_decl_die (decl);
27458 if (!at_import_die)
27460 /* If we're trying to avoid duplicate debug info, we may not have
27461 emitted the member decl for this field. Emit it now. */
27462 if (TREE_CODE (decl) == FIELD_DECL)
27464 tree type = DECL_CONTEXT (decl);
27466 if (TYPE_CONTEXT (type)
27467 && TYPE_P (TYPE_CONTEXT (type))
27468 && !should_emit_struct_debug (TYPE_CONTEXT (type),
27469 DINFO_USAGE_DIR_USE))
27470 return;
27471 gen_type_die_for_member (type, decl,
27472 get_context_die (TYPE_CONTEXT (type)));
27474 if (TREE_CODE (decl) == CONST_DECL)
27476 /* Individual enumerators of an enum type do not get output here
27477 (see gen_decl_die), so we cannot call force_decl_die. */
27478 if (!is_fortran () && !is_ada () && !is_dlang ())
27479 return;
27481 if (TREE_CODE (decl) == NAMELIST_DECL)
27482 at_import_die = gen_namelist_decl (DECL_NAME (decl),
27483 get_context_die (DECL_CONTEXT (decl)),
27484 NULL_TREE);
27485 else
27486 at_import_die = force_decl_die (decl);
27490 if (TREE_CODE (decl) == NAMESPACE_DECL)
27492 if (dwarf_version >= 3 || !dwarf_strict)
27493 imported_die = new_die (DW_TAG_imported_module,
27494 lexical_block_die,
27495 lexical_block);
27496 else
27497 return;
27499 else
27500 imported_die = new_die (DW_TAG_imported_declaration,
27501 lexical_block_die,
27502 lexical_block);
27504 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
27505 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
27506 if (debug_column_info && xloc.column)
27507 add_AT_unsigned (imported_die, DW_AT_decl_column, xloc.column);
27508 if (name)
27509 add_AT_string (imported_die, DW_AT_name,
27510 IDENTIFIER_POINTER (name));
27511 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
27514 /* Output debug information for imported module or decl DECL.
27515 NAME is non-NULL name in context if the decl has been renamed.
27516 CHILD is true if decl is one of the renamed decls as part of
27517 importing whole module.
27518 IMPLICIT is set if this hook is called for an implicit import
27519 such as inline namespace. */
27521 static void
27522 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
27523 bool child, bool implicit)
27525 /* dw_die_ref at_import_die; */
27526 dw_die_ref scope_die;
27528 if (debug_info_level <= DINFO_LEVEL_TERSE)
27529 return;
27531 gcc_assert (decl);
27533 /* For DWARF5, just DW_AT_export_symbols on the DW_TAG_namespace
27534 should be enough, for DWARF4 and older even if we emit as extension
27535 DW_AT_export_symbols add the implicit DW_TAG_imported_module anyway
27536 for the benefit of consumers unaware of DW_AT_export_symbols. */
27537 if (implicit
27538 && dwarf_version >= 5
27539 && lang_hooks.decls.decl_dwarf_attribute (decl,
27540 DW_AT_export_symbols) == 1)
27541 return;
27543 set_early_dwarf s;
27545 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
27546 We need decl DIE for reference and scope die. First, get DIE for the decl
27547 itself. */
27549 /* Get the scope die for decl context. Use comp_unit_die for global module
27550 or decl. If die is not found for non globals, force new die. */
27551 if (context
27552 && TYPE_P (context)
27553 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
27554 return;
27556 scope_die = get_context_die (context);
27558 if (child)
27560 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
27561 there is nothing we can do, here. */
27562 if (dwarf_version < 3 && dwarf_strict)
27563 return;
27565 gcc_assert (scope_die->die_child);
27566 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
27567 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
27568 scope_die = scope_die->die_child;
27571 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
27572 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
27575 /* Output debug information for namelists. */
27577 static dw_die_ref
27578 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
27580 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
27581 tree value;
27582 unsigned i;
27584 if (debug_info_level <= DINFO_LEVEL_TERSE)
27585 return NULL;
27587 gcc_assert (scope_die != NULL);
27588 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
27589 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
27591 /* If there are no item_decls, we have a nondefining namelist, e.g.
27592 with USE association; hence, set DW_AT_declaration. */
27593 if (item_decls == NULL_TREE)
27595 add_AT_flag (nml_die, DW_AT_declaration, 1);
27596 return nml_die;
27599 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
27601 nml_item_ref_die = lookup_decl_die (value);
27602 if (!nml_item_ref_die)
27603 nml_item_ref_die = force_decl_die (value);
27605 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
27606 add_AT_die_ref (nml_item_die, DW_AT_namelist_item, nml_item_ref_die);
27608 return nml_die;
27612 /* Write the debugging output for DECL and return the DIE. */
27614 static void
27615 dwarf2out_decl (tree decl)
27617 dw_die_ref context_die = comp_unit_die ();
27619 switch (TREE_CODE (decl))
27621 case ERROR_MARK:
27622 return;
27624 case FUNCTION_DECL:
27625 /* If we're a nested function, initially use a parent of NULL; if we're
27626 a plain function, this will be fixed up in decls_for_scope. If
27627 we're a method, it will be ignored, since we already have a DIE.
27628 Avoid doing this late though since clones of class methods may
27629 otherwise end up in limbo and create type DIEs late. */
27630 if (early_dwarf
27631 && decl_function_context (decl)
27632 /* But if we're in terse mode, we don't care about scope. */
27633 && debug_info_level > DINFO_LEVEL_TERSE)
27634 context_die = NULL;
27635 break;
27637 case VAR_DECL:
27638 /* For local statics lookup proper context die. */
27639 if (local_function_static (decl))
27640 context_die = lookup_decl_die (DECL_CONTEXT (decl));
27642 /* If we are in terse mode, don't generate any DIEs to represent any
27643 variable declarations or definitions unless it is external. */
27644 if (debug_info_level < DINFO_LEVEL_TERSE
27645 || (debug_info_level == DINFO_LEVEL_TERSE
27646 && !TREE_PUBLIC (decl)))
27647 return;
27648 break;
27650 case CONST_DECL:
27651 if (debug_info_level <= DINFO_LEVEL_TERSE)
27652 return;
27653 if (!is_fortran () && !is_ada () && !is_dlang ())
27654 return;
27655 if (TREE_STATIC (decl) && decl_function_context (decl))
27656 context_die = lookup_decl_die (DECL_CONTEXT (decl));
27657 break;
27659 case NAMESPACE_DECL:
27660 case IMPORTED_DECL:
27661 if (debug_info_level <= DINFO_LEVEL_TERSE)
27662 return;
27663 if (lookup_decl_die (decl) != NULL)
27664 return;
27665 break;
27667 case TYPE_DECL:
27668 /* Don't emit stubs for types unless they are needed by other DIEs. */
27669 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
27670 return;
27672 /* Don't bother trying to generate any DIEs to represent any of the
27673 normal built-in types for the language we are compiling. */
27674 if (DECL_IS_UNDECLARED_BUILTIN (decl))
27675 return;
27677 /* If we are in terse mode, don't generate any DIEs for types. */
27678 if (debug_info_level <= DINFO_LEVEL_TERSE)
27679 return;
27681 /* If we're a function-scope tag, initially use a parent of NULL;
27682 this will be fixed up in decls_for_scope. */
27683 if (decl_function_context (decl))
27684 context_die = NULL;
27686 break;
27688 case NAMELIST_DECL:
27689 break;
27691 default:
27692 return;
27695 gen_decl_die (decl, NULL, NULL, context_die);
27697 if (flag_checking)
27699 dw_die_ref die = lookup_decl_die (decl);
27700 if (die)
27701 check_die (die);
27705 /* Write the debugging output for DECL. */
27707 static void
27708 dwarf2out_function_decl (tree decl)
27710 dwarf2out_decl (decl);
27711 call_arg_locations = NULL;
27712 call_arg_loc_last = NULL;
27713 call_site_count = -1;
27714 tail_call_site_count = -1;
27715 decl_loc_table->empty ();
27716 cached_dw_loc_list_table->empty ();
27719 /* Output a marker (i.e. a label) for the beginning of the generated code for
27720 a lexical block. */
27722 static void
27723 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
27724 unsigned int blocknum)
27726 switch_to_section (current_function_section ());
27727 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
27730 /* Output a marker (i.e. a label) for the end of the generated code for a
27731 lexical block. */
27733 static void
27734 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
27736 switch_to_section (current_function_section ());
27737 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
27740 /* Returns true if it is appropriate not to emit any debugging
27741 information for BLOCK, because it doesn't contain any instructions.
27743 Don't allow this for blocks with nested functions or local classes
27744 as we would end up with orphans, and in the presence of scheduling
27745 we may end up calling them anyway. */
27747 static bool
27748 dwarf2out_ignore_block (const_tree block)
27750 tree decl;
27751 unsigned int i;
27753 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
27754 if (TREE_CODE (decl) == FUNCTION_DECL
27755 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
27756 return false;
27757 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
27759 decl = BLOCK_NONLOCALIZED_VAR (block, i);
27760 if (TREE_CODE (decl) == FUNCTION_DECL
27761 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
27762 return false;
27765 return true;
27768 /* Hash table routines for file_hash. */
27770 bool
27771 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
27773 return filename_cmp (p1->key, p2) == 0;
27776 hashval_t
27777 dwarf_file_hasher::hash (dwarf_file_data *p)
27779 return htab_hash_string (p->key);
27782 /* Lookup FILE_NAME (in the list of filenames that we know about here in
27783 dwarf2out.cc) and return its "index". The index of each (known) filename is
27784 just a unique number which is associated with only that one filename. We
27785 need such numbers for the sake of generating labels (in the .debug_sfnames
27786 section) and references to those files numbers (in the .debug_srcinfo
27787 and .debug_macinfo sections). If the filename given as an argument is not
27788 found in our current list, add it to the list and assign it the next
27789 available unique index number. */
27791 static struct dwarf_file_data *
27792 lookup_filename (const char *file_name)
27794 struct dwarf_file_data * created;
27796 if (!file_name)
27797 return NULL;
27799 if (!file_name[0])
27800 file_name = "<stdin>";
27802 dwarf_file_data **slot
27803 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
27804 INSERT);
27805 if (*slot)
27806 return *slot;
27808 created = ggc_alloc<dwarf_file_data> ();
27809 created->key = file_name;
27810 created->filename = remap_debug_filename (file_name);
27811 created->emitted_number = 0;
27812 *slot = created;
27813 return created;
27816 /* If the assembler will construct the file table, then translate the compiler
27817 internal file table number into the assembler file table number, and emit
27818 a .file directive if we haven't already emitted one yet. The file table
27819 numbers are different because we prune debug info for unused variables and
27820 types, which may include filenames. */
27822 static int
27823 maybe_emit_file (struct dwarf_file_data * fd)
27825 if (! fd->emitted_number)
27827 if (last_emitted_file)
27828 fd->emitted_number = last_emitted_file->emitted_number + 1;
27829 else
27830 fd->emitted_number = 1;
27831 last_emitted_file = fd;
27833 if (output_asm_line_debug_info ())
27835 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
27836 output_quoted_string (asm_out_file, fd->filename);
27837 fputc ('\n', asm_out_file);
27841 return fd->emitted_number;
27844 /* Schedule generation of a DW_AT_const_value attribute to DIE.
27845 That generation should happen after function debug info has been
27846 generated. The value of the attribute is the constant value of ARG. */
27848 static void
27849 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
27851 die_arg_entry entry;
27853 if (!die || !arg)
27854 return;
27856 gcc_assert (early_dwarf);
27858 if (!tmpl_value_parm_die_table)
27859 vec_alloc (tmpl_value_parm_die_table, 32);
27861 entry.die = die;
27862 entry.arg = arg;
27863 vec_safe_push (tmpl_value_parm_die_table, entry);
27866 /* Return TRUE if T is an instance of generic type, FALSE
27867 otherwise. */
27869 static bool
27870 generic_type_p (tree t)
27872 if (t == NULL_TREE || !TYPE_P (t))
27873 return false;
27874 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
27877 /* Schedule the generation of the generic parameter dies for the
27878 instance of generic type T. The proper generation itself is later
27879 done by gen_scheduled_generic_parms_dies. */
27881 static void
27882 schedule_generic_params_dies_gen (tree t)
27884 if (!generic_type_p (t))
27885 return;
27887 gcc_assert (early_dwarf);
27889 if (!generic_type_instances)
27890 vec_alloc (generic_type_instances, 256);
27892 vec_safe_push (generic_type_instances, t);
27895 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
27896 by append_entry_to_tmpl_value_parm_die_table. This function must
27897 be called after function DIEs have been generated. */
27899 static void
27900 gen_remaining_tmpl_value_param_die_attribute (void)
27902 if (tmpl_value_parm_die_table)
27904 unsigned i, j;
27905 die_arg_entry *e;
27907 /* We do this in two phases - first get the cases we can
27908 handle during early-finish, preserving those we cannot
27909 (containing symbolic constants where we don't yet know
27910 whether we are going to output the referenced symbols).
27911 For those we try again at late-finish. */
27912 j = 0;
27913 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
27915 if (!e->die->removed
27916 && !tree_add_const_value_attribute (e->die, e->arg))
27918 dw_loc_descr_ref loc = NULL;
27919 if (! early_dwarf
27920 && (dwarf_version >= 5 || !dwarf_strict))
27921 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
27922 if (loc)
27923 add_AT_loc (e->die, DW_AT_location, loc);
27924 else
27925 (*tmpl_value_parm_die_table)[j++] = *e;
27928 tmpl_value_parm_die_table->truncate (j);
27932 /* Generate generic parameters DIEs for instances of generic types
27933 that have been previously scheduled by
27934 schedule_generic_params_dies_gen. This function must be called
27935 after all the types of the CU have been laid out. */
27937 static void
27938 gen_scheduled_generic_parms_dies (void)
27940 unsigned i;
27941 tree t;
27943 if (!generic_type_instances)
27944 return;
27946 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
27947 if (COMPLETE_TYPE_P (t))
27948 gen_generic_params_dies (t);
27950 generic_type_instances = NULL;
27954 /* Replace DW_AT_name for the decl with name. */
27956 static void
27957 dwarf2out_set_name (tree decl, tree name)
27959 dw_die_ref die;
27960 dw_attr_node *attr;
27961 const char *dname;
27963 die = TYPE_SYMTAB_DIE (decl);
27964 if (!die)
27965 return;
27967 dname = dwarf2_name (name, 0);
27968 if (!dname)
27969 return;
27971 attr = get_AT (die, DW_AT_name);
27972 if (attr)
27974 struct indirect_string_node *node;
27976 node = find_AT_string (dname);
27977 /* replace the string. */
27978 attr->dw_attr_val.v.val_str = node;
27981 else
27982 add_name_attribute (die, dname);
27985 /* True if before or during processing of the first function being emitted. */
27986 static bool in_first_function_p = true;
27987 /* True if loc_note during dwarf2out_var_location call might still be
27988 before first real instruction at address equal to .Ltext0. */
27989 static bool maybe_at_text_label_p = true;
27990 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
27991 static unsigned int first_loclabel_num_not_at_text_label;
27993 /* Look ahead for a real insn. */
27995 static rtx_insn *
27996 dwarf2out_next_real_insn (rtx_insn *loc_note)
27998 rtx_insn *next_real = NEXT_INSN (loc_note);
28000 while (next_real)
28001 if (INSN_P (next_real))
28002 break;
28003 else
28004 next_real = NEXT_INSN (next_real);
28006 return next_real;
28009 /* Called by the final INSN scan whenever we see a var location. We
28010 use it to drop labels in the right places, and throw the location in
28011 our lookup table. */
28013 static void
28014 dwarf2out_var_location (rtx_insn *loc_note)
28016 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
28017 struct var_loc_node *newloc;
28018 rtx_insn *next_real;
28019 rtx_insn *call_insn = NULL;
28020 static const char *last_label;
28021 static const char *last_postcall_label;
28022 static bool last_in_cold_section_p;
28023 static rtx_insn *expected_next_loc_note;
28024 tree decl;
28025 bool var_loc_p;
28026 var_loc_view view = 0;
28028 if (!NOTE_P (loc_note))
28030 if (CALL_P (loc_note))
28032 maybe_reset_location_view (loc_note, cur_line_info_table);
28033 call_site_count++;
28034 if (SIBLING_CALL_P (loc_note))
28035 tail_call_site_count++;
28036 if (find_reg_note (loc_note, REG_CALL_ARG_LOCATION, NULL_RTX))
28038 call_insn = loc_note;
28039 loc_note = NULL;
28040 var_loc_p = false;
28042 next_real = dwarf2out_next_real_insn (call_insn);
28043 cached_next_real_insn = NULL;
28044 goto create_label;
28046 if (optimize == 0 && !flag_var_tracking)
28048 /* When the var-tracking pass is not running, there is no note
28049 for indirect calls whose target is compile-time known. In this
28050 case, process such calls specifically so that we generate call
28051 sites for them anyway. */
28052 rtx x = PATTERN (loc_note);
28053 if (GET_CODE (x) == PARALLEL)
28054 x = XVECEXP (x, 0, 0);
28055 if (GET_CODE (x) == SET)
28056 x = SET_SRC (x);
28057 if (GET_CODE (x) == CALL)
28058 x = XEXP (x, 0);
28059 if (!MEM_P (x)
28060 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
28061 || !SYMBOL_REF_DECL (XEXP (x, 0))
28062 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
28063 != FUNCTION_DECL))
28065 call_insn = loc_note;
28066 loc_note = NULL;
28067 var_loc_p = false;
28069 next_real = dwarf2out_next_real_insn (call_insn);
28070 cached_next_real_insn = NULL;
28071 goto create_label;
28075 else if (!debug_variable_location_views)
28076 gcc_unreachable ();
28077 else
28078 maybe_reset_location_view (loc_note, cur_line_info_table);
28080 return;
28083 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
28084 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
28085 return;
28087 /* Optimize processing a large consecutive sequence of location
28088 notes so we don't spend too much time in next_real_insn. If the
28089 next insn is another location note, remember the next_real_insn
28090 calculation for next time. */
28091 next_real = cached_next_real_insn;
28092 if (next_real)
28094 if (expected_next_loc_note != loc_note)
28095 next_real = NULL;
28098 if (! next_real)
28099 next_real = dwarf2out_next_real_insn (loc_note);
28101 if (next_real)
28103 rtx_insn *next_note = NEXT_INSN (loc_note);
28104 while (next_note != next_real)
28106 if (! next_note->deleted ()
28107 && NOTE_P (next_note)
28108 && NOTE_KIND (next_note) == NOTE_INSN_VAR_LOCATION)
28109 break;
28110 next_note = NEXT_INSN (next_note);
28113 if (next_note == next_real)
28114 cached_next_real_insn = NULL;
28115 else
28117 expected_next_loc_note = next_note;
28118 cached_next_real_insn = next_real;
28121 else
28122 cached_next_real_insn = NULL;
28124 /* If there are no instructions which would be affected by this note,
28125 don't do anything. */
28126 if (var_loc_p
28127 && next_real == NULL_RTX
28128 && !NOTE_DURING_CALL_P (loc_note))
28129 return;
28131 create_label:
28133 if (next_real == NULL_RTX)
28134 next_real = get_last_insn ();
28136 /* If there were any real insns between note we processed last time
28137 and this note (or if it is the first note), clear
28138 last_{,postcall_}label so that they are not reused this time. */
28139 if (last_var_location_insn == NULL_RTX
28140 || last_var_location_insn != next_real
28141 || last_in_cold_section_p != in_cold_section_p)
28143 last_label = NULL;
28144 last_postcall_label = NULL;
28147 if (var_loc_p)
28149 const char *label
28150 = NOTE_DURING_CALL_P (loc_note) ? last_postcall_label : last_label;
28151 view = cur_line_info_table->view;
28152 decl = NOTE_VAR_LOCATION_DECL (loc_note);
28153 newloc = add_var_loc_to_decl (decl, loc_note, label, view);
28154 if (newloc == NULL)
28155 return;
28157 else
28159 decl = NULL_TREE;
28160 newloc = NULL;
28163 /* If there were no real insns between note we processed last time
28164 and this note, use the label we emitted last time. Otherwise
28165 create a new label and emit it. */
28166 if (last_label == NULL)
28168 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
28169 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
28170 loclabel_num++;
28171 last_label = ggc_strdup (loclabel);
28172 /* See if loclabel might be equal to .Ltext0. If yes,
28173 bump first_loclabel_num_not_at_text_label. */
28174 if (!have_multiple_function_sections
28175 && in_first_function_p
28176 && maybe_at_text_label_p)
28178 static rtx_insn *last_start;
28179 rtx_insn *insn;
28180 for (insn = loc_note; insn; insn = previous_insn (insn))
28181 if (insn == last_start)
28182 break;
28183 else if (!NONDEBUG_INSN_P (insn))
28184 continue;
28185 else
28187 rtx body = PATTERN (insn);
28188 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
28189 continue;
28190 /* Inline asm could occupy zero bytes. */
28191 else if (GET_CODE (body) == ASM_INPUT
28192 || asm_noperands (body) >= 0)
28193 continue;
28194 #ifdef HAVE_ATTR_length /* ??? We don't include insn-attr.h. */
28195 else if (HAVE_ATTR_length && get_attr_min_length (insn) == 0)
28196 continue;
28197 #endif
28198 else
28200 /* Assume insn has non-zero length. */
28201 maybe_at_text_label_p = false;
28202 break;
28205 if (maybe_at_text_label_p)
28207 last_start = loc_note;
28208 first_loclabel_num_not_at_text_label = loclabel_num;
28213 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
28214 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
28216 if (!var_loc_p)
28218 struct call_arg_loc_node *ca_loc
28219 = ggc_cleared_alloc<call_arg_loc_node> ();
28220 rtx_insn *prev = call_insn;
28222 ca_loc->call_arg_loc_note
28223 = find_reg_note (call_insn, REG_CALL_ARG_LOCATION, NULL_RTX);
28224 ca_loc->next = NULL;
28225 ca_loc->label = last_label;
28226 gcc_assert (prev
28227 && (CALL_P (prev)
28228 || (NONJUMP_INSN_P (prev)
28229 && GET_CODE (PATTERN (prev)) == SEQUENCE
28230 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
28231 if (!CALL_P (prev))
28232 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
28233 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
28235 /* Look for a SYMBOL_REF in the "prev" instruction. */
28236 rtx x = get_call_rtx_from (prev);
28237 if (x)
28239 /* Try to get the call symbol, if any. */
28240 if (MEM_P (XEXP (x, 0)))
28241 x = XEXP (x, 0);
28242 /* First, look for a memory access to a symbol_ref. */
28243 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
28244 && SYMBOL_REF_DECL (XEXP (x, 0))
28245 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
28246 ca_loc->symbol_ref = XEXP (x, 0);
28247 /* Otherwise, look at a compile-time known user-level function
28248 declaration. */
28249 else if (MEM_P (x)
28250 && MEM_EXPR (x)
28251 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
28252 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
28255 ca_loc->block = insn_scope (prev);
28256 if (call_arg_locations)
28257 call_arg_loc_last->next = ca_loc;
28258 else
28259 call_arg_locations = ca_loc;
28260 call_arg_loc_last = ca_loc;
28262 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
28264 newloc->label = last_label;
28265 newloc->view = view;
28267 else
28269 if (!last_postcall_label)
28271 sprintf (loclabel, "%s-1", last_label);
28272 last_postcall_label = ggc_strdup (loclabel);
28274 newloc->label = last_postcall_label;
28275 /* ??? This view is at last_label, not last_label-1, but we
28276 could only assume view at last_label-1 is zero if we could
28277 assume calls always have length greater than one. This is
28278 probably true in general, though there might be a rare
28279 exception to this rule, e.g. if a call insn is optimized out
28280 by target magic. Then, even the -1 in the label will be
28281 wrong, which might invalidate the range. Anyway, using view,
28282 though technically possibly incorrect, will work as far as
28283 ranges go: since L-1 is in the middle of the call insn,
28284 (L-1).0 and (L-1).V shouldn't make any difference, and having
28285 the loclist entry refer to the .loc entry might be useful, so
28286 leave it like this. */
28287 newloc->view = view;
28290 if (var_loc_p && flag_debug_asm)
28292 const char *name, *sep, *patstr;
28293 if (decl && DECL_NAME (decl))
28294 name = IDENTIFIER_POINTER (DECL_NAME (decl));
28295 else
28296 name = "";
28297 if (NOTE_VAR_LOCATION_LOC (loc_note))
28299 sep = " => ";
28300 patstr = str_pattern_slim (NOTE_VAR_LOCATION_LOC (loc_note));
28302 else
28304 sep = " ";
28305 patstr = "RESET";
28307 fprintf (asm_out_file, "\t%s DEBUG %s%s%s\n", ASM_COMMENT_START,
28308 name, sep, patstr);
28311 last_var_location_insn = next_real;
28312 last_in_cold_section_p = in_cold_section_p;
28315 /* Check whether BLOCK, a lexical block, is nested within OUTER, or is
28316 OUTER itself. If BOTHWAYS, check not only that BLOCK can reach
28317 OUTER through BLOCK_SUPERCONTEXT links, but also that there is a
28318 path from OUTER to BLOCK through BLOCK_SUBBLOCKs and
28319 BLOCK_FRAGMENT_ORIGIN links. */
28320 static bool
28321 block_within_block_p (tree block, tree outer, bool bothways)
28323 if (block == outer)
28324 return true;
28326 /* Quickly check that OUTER is up BLOCK's supercontext chain. */
28327 for (tree context = BLOCK_SUPERCONTEXT (block);
28328 context != outer;
28329 context = BLOCK_SUPERCONTEXT (context))
28330 if (!context || TREE_CODE (context) != BLOCK)
28331 return false;
28333 if (!bothways)
28334 return true;
28336 /* Now check that each block is actually referenced by its
28337 parent. */
28338 for (tree context = BLOCK_SUPERCONTEXT (block); ;
28339 context = BLOCK_SUPERCONTEXT (context))
28341 if (BLOCK_FRAGMENT_ORIGIN (context))
28343 gcc_assert (!BLOCK_SUBBLOCKS (context));
28344 context = BLOCK_FRAGMENT_ORIGIN (context);
28346 for (tree sub = BLOCK_SUBBLOCKS (context);
28347 sub != block;
28348 sub = BLOCK_CHAIN (sub))
28349 if (!sub)
28350 return false;
28351 if (context == outer)
28352 return true;
28353 else
28354 block = context;
28358 /* Called during final while assembling the marker of the entry point
28359 for an inlined function. */
28361 static void
28362 dwarf2out_inline_entry (tree block)
28364 gcc_assert (debug_inline_points);
28366 /* If we can't represent it, don't bother. */
28367 if (!(dwarf_version >= 3 || !dwarf_strict))
28368 return;
28370 gcc_assert (DECL_P (block_ultimate_origin (block)));
28372 /* Sanity check the block tree. This would catch a case in which
28373 BLOCK got removed from the tree reachable from the outermost
28374 lexical block, but got retained in markers. It would still link
28375 back to its parents, but some ancestor would be missing a link
28376 down the path to the sub BLOCK. If the block got removed, its
28377 BLOCK_NUMBER will not be a usable value. */
28378 if (flag_checking)
28379 gcc_assert (block_within_block_p (block,
28380 DECL_INITIAL (current_function_decl),
28381 true));
28383 gcc_assert (inlined_function_outer_scope_p (block));
28384 gcc_assert (!lookup_block_die (block));
28386 if (BLOCK_FRAGMENT_ORIGIN (block))
28387 block = BLOCK_FRAGMENT_ORIGIN (block);
28388 /* Can the entry point ever not be at the beginning of an
28389 unfragmented lexical block? */
28390 else if (!(BLOCK_FRAGMENT_CHAIN (block)
28391 || (cur_line_info_table
28392 && !ZERO_VIEW_P (cur_line_info_table->view))))
28393 return;
28395 if (!inline_entry_data_table)
28396 inline_entry_data_table
28397 = hash_table<inline_entry_data_hasher>::create_ggc (10);
28400 inline_entry_data **iedp
28401 = inline_entry_data_table->find_slot_with_hash (block,
28402 htab_hash_pointer (block),
28403 INSERT);
28404 if (*iedp)
28405 /* ??? Ideally, we'd record all entry points for the same inlined
28406 function (some may have been duplicated by e.g. unrolling), but
28407 we have no way to represent that ATM. */
28408 return;
28410 inline_entry_data *ied = *iedp = ggc_cleared_alloc<inline_entry_data> ();
28411 ied->block = block;
28412 ied->label_pfx = BLOCK_INLINE_ENTRY_LABEL;
28413 ied->label_num = BLOCK_NUMBER (block);
28414 if (cur_line_info_table)
28415 ied->view = cur_line_info_table->view;
28417 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_INLINE_ENTRY_LABEL,
28418 BLOCK_NUMBER (block));
28421 /* Called from finalize_size_functions for size functions so that their body
28422 can be encoded in the debug info to describe the layout of variable-length
28423 structures. */
28425 static void
28426 dwarf2out_size_function (tree decl)
28428 set_early_dwarf s;
28429 function_to_dwarf_procedure (decl);
28432 /* Note in one location list that text section has changed. */
28435 var_location_switch_text_section_1 (var_loc_list **slot, void *)
28437 var_loc_list *list = *slot;
28438 if (list->first)
28439 list->last_before_switch
28440 = list->last->next ? list->last->next : list->last;
28441 return 1;
28444 /* Note in all location lists that text section has changed. */
28446 static void
28447 var_location_switch_text_section (void)
28449 if (decl_loc_table == NULL)
28450 return;
28452 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
28455 /* Create a new line number table. */
28457 static dw_line_info_table *
28458 new_line_info_table (void)
28460 dw_line_info_table *table;
28462 table = ggc_cleared_alloc<dw_line_info_table> ();
28463 table->file_num = 1;
28464 table->line_num = 1;
28465 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
28466 FORCE_RESET_NEXT_VIEW (table->view);
28467 table->symviews_since_reset = 0;
28469 return table;
28472 /* Lookup the "current" table into which we emit line info, so
28473 that we don't have to do it for every source line. */
28475 static void
28476 set_cur_line_info_table (section *sec)
28478 dw_line_info_table *table;
28480 if (sec == text_section)
28481 table = text_section_line_info;
28482 else if (sec == cold_text_section)
28484 table = cold_text_section_line_info;
28485 if (!table)
28487 cold_text_section_line_info = table = new_line_info_table ();
28488 table->end_label = cold_end_label;
28491 else
28493 const char *end_label;
28495 if (crtl->has_bb_partition)
28497 if (in_cold_section_p)
28498 end_label = crtl->subsections.cold_section_end_label;
28499 else
28500 end_label = crtl->subsections.hot_section_end_label;
28502 else
28504 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28505 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
28506 current_function_funcdef_no);
28507 end_label = ggc_strdup (label);
28510 table = new_line_info_table ();
28511 table->end_label = end_label;
28513 vec_safe_push (separate_line_info, table);
28516 if (output_asm_line_debug_info ())
28517 table->is_stmt = (cur_line_info_table
28518 ? cur_line_info_table->is_stmt
28519 : DWARF_LINE_DEFAULT_IS_STMT_START);
28520 cur_line_info_table = table;
28524 /* We need to reset the locations at the beginning of each
28525 function. We can't do this in the end_function hook, because the
28526 declarations that use the locations won't have been output when
28527 that hook is called. Also compute have_multiple_function_sections here. */
28529 static void
28530 dwarf2out_begin_function (tree fun)
28532 section *sec = function_section (fun);
28534 if (sec != text_section)
28535 have_multiple_function_sections = true;
28537 if (crtl->has_bb_partition && !cold_text_section)
28539 gcc_assert (current_function_decl == fun);
28540 cold_text_section = unlikely_text_section ();
28541 switch_to_section (cold_text_section);
28542 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
28543 switch_to_section (sec);
28546 call_site_count = 0;
28547 tail_call_site_count = 0;
28549 set_cur_line_info_table (sec);
28550 FORCE_RESET_NEXT_VIEW (cur_line_info_table->view);
28553 /* Helper function of dwarf2out_end_function, called only after emitting
28554 the very first function into assembly. Check if some .debug_loc range
28555 might end with a .LVL* label that could be equal to .Ltext0.
28556 In that case we must force using absolute addresses in .debug_loc ranges,
28557 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
28558 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
28559 list terminator.
28560 Set have_multiple_function_sections to true in that case and
28561 terminate htab traversal. */
28564 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
28566 var_loc_list *entry = *slot;
28567 struct var_loc_node *node;
28569 node = entry->first;
28570 if (node && node->next && node->next->label)
28572 unsigned int i;
28573 const char *label = node->next->label;
28574 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
28576 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
28578 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
28579 if (strcmp (label, loclabel) == 0)
28581 have_multiple_function_sections = true;
28582 return 0;
28586 return 1;
28589 /* Hook called after emitting a function into assembly.
28590 This does something only for the very first function emitted. */
28592 static void
28593 dwarf2out_end_function (unsigned int)
28595 if (in_first_function_p
28596 && !have_multiple_function_sections
28597 && first_loclabel_num_not_at_text_label
28598 && decl_loc_table)
28599 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
28600 in_first_function_p = false;
28601 maybe_at_text_label_p = false;
28604 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
28605 front-ends register a translation unit even before dwarf2out_init is
28606 called. */
28607 static tree main_translation_unit = NULL_TREE;
28609 /* Hook called by front-ends after they built their main translation unit.
28610 Associate comp_unit_die to UNIT. */
28612 static void
28613 dwarf2out_register_main_translation_unit (tree unit)
28615 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
28616 && main_translation_unit == NULL_TREE);
28617 main_translation_unit = unit;
28618 /* If dwarf2out_init has not been called yet, it will perform the association
28619 itself looking at main_translation_unit. */
28620 if (decl_die_table != NULL)
28621 equate_decl_number_to_die (unit, comp_unit_die ());
28624 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
28626 static void
28627 push_dw_line_info_entry (dw_line_info_table *table,
28628 enum dw_line_info_opcode opcode, unsigned int val)
28630 dw_line_info_entry e;
28631 e.opcode = opcode;
28632 e.val = val;
28633 vec_safe_push (table->entries, e);
28636 /* Output a label to mark the beginning of a source code line entry
28637 and record information relating to this source line, in
28638 'line_info_table' for later output of the .debug_line section. */
28639 /* ??? The discriminator parameter ought to be unsigned. */
28641 static void
28642 dwarf2out_source_line (unsigned int line, unsigned int column,
28643 const char *filename,
28644 int discriminator, bool is_stmt)
28646 unsigned int file_num;
28647 dw_line_info_table *table;
28648 static var_loc_view lvugid;
28650 /* 'line_info_table' information gathering is not needed when the debug
28651 info level is set to the lowest value. Also, the current DWARF-based
28652 debug formats do not use this info. */
28653 if (debug_info_level < DINFO_LEVEL_TERSE || !dwarf_debuginfo_p ())
28654 return;
28656 table = cur_line_info_table;
28658 if (line == 0)
28660 if (debug_variable_location_views
28661 && output_asm_line_debug_info ()
28662 && table && !RESETTING_VIEW_P (table->view))
28664 /* If we're using the assembler to compute view numbers, we
28665 can't issue a .loc directive for line zero, so we can't
28666 get a view number at this point. We might attempt to
28667 compute it from the previous view, or equate it to a
28668 subsequent view (though it might not be there!), but
28669 since we're omitting the line number entry, we might as
28670 well omit the view number as well. That means pretending
28671 it's a view number zero, which might very well turn out
28672 to be correct. ??? Extend the assembler so that the
28673 compiler could emit e.g. ".locview .LVU#", to output a
28674 view without changing line number information. We'd then
28675 have to count it in symviews_since_reset; when it's omitted,
28676 it doesn't count. */
28677 if (!zero_view_p)
28678 zero_view_p = BITMAP_GGC_ALLOC ();
28679 bitmap_set_bit (zero_view_p, table->view);
28680 if (flag_debug_asm)
28682 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28683 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", table->view);
28684 fprintf (asm_out_file, "\t%s line 0, omitted view ",
28685 ASM_COMMENT_START);
28686 assemble_name (asm_out_file, label);
28687 putc ('\n', asm_out_file);
28689 table->view = ++lvugid;
28691 return;
28694 /* The discriminator column was added in dwarf4. Simplify the below
28695 by simply removing it if we're not supposed to output it. */
28696 if (dwarf_version < 4 && dwarf_strict)
28697 discriminator = 0;
28699 if (!debug_column_info)
28700 column = 0;
28702 file_num = maybe_emit_file (lookup_filename (filename));
28704 /* ??? TODO: Elide duplicate line number entries. Traditionally,
28705 the debugger has used the second (possibly duplicate) line number
28706 at the beginning of the function to mark the end of the prologue.
28707 We could eliminate any other duplicates within the function. For
28708 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
28709 that second line number entry. */
28710 /* Recall that this end-of-prologue indication is *not* the same thing
28711 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
28712 to which the hook corresponds, follows the last insn that was
28713 emitted by gen_prologue. What we need is to precede the first insn
28714 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
28715 insn that corresponds to something the user wrote. These may be
28716 very different locations once scheduling is enabled. */
28718 if (0 && file_num == table->file_num
28719 && line == table->line_num
28720 && column == table->column_num
28721 && discriminator == table->discrim_num
28722 && is_stmt == table->is_stmt)
28723 return;
28725 switch_to_section (current_function_section ());
28727 /* If requested, emit something human-readable. */
28728 if (flag_debug_asm)
28730 if (debug_column_info)
28731 fprintf (asm_out_file, "\t%s %s:%d:%d\n", ASM_COMMENT_START,
28732 filename, line, column);
28733 else
28734 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
28735 filename, line);
28738 if (output_asm_line_debug_info ())
28740 /* Emit the .loc directive understood by GNU as. */
28741 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
28742 file_num, line, is_stmt, discriminator */
28743 fputs ("\t.loc ", asm_out_file);
28744 fprint_ul (asm_out_file, file_num);
28745 putc (' ', asm_out_file);
28746 fprint_ul (asm_out_file, line);
28747 putc (' ', asm_out_file);
28748 fprint_ul (asm_out_file, column);
28750 if (is_stmt != table->is_stmt)
28752 #if HAVE_GAS_LOC_STMT
28753 fputs (" is_stmt ", asm_out_file);
28754 putc (is_stmt ? '1' : '0', asm_out_file);
28755 #endif
28757 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
28759 gcc_assert (discriminator > 0);
28760 fputs (" discriminator ", asm_out_file);
28761 fprint_ul (asm_out_file, (unsigned long) discriminator);
28763 if (debug_variable_location_views)
28765 if (!RESETTING_VIEW_P (table->view))
28767 table->symviews_since_reset++;
28768 if (table->symviews_since_reset > symview_upper_bound)
28769 symview_upper_bound = table->symviews_since_reset;
28770 /* When we're using the assembler to compute view
28771 numbers, we output symbolic labels after "view" in
28772 .loc directives, and the assembler will set them for
28773 us, so that we can refer to the view numbers in
28774 location lists. The only exceptions are when we know
28775 a view will be zero: "-0" is a forced reset, used
28776 e.g. in the beginning of functions, whereas "0" tells
28777 the assembler to check that there was a PC change
28778 since the previous view, in a way that implicitly
28779 resets the next view. */
28780 fputs (" view ", asm_out_file);
28781 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28782 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", table->view);
28783 assemble_name (asm_out_file, label);
28784 table->view = ++lvugid;
28786 else
28788 table->symviews_since_reset = 0;
28789 if (FORCE_RESETTING_VIEW_P (table->view))
28790 fputs (" view -0", asm_out_file);
28791 else
28792 fputs (" view 0", asm_out_file);
28793 /* Mark the present view as a zero view. Earlier debug
28794 binds may have already added its id to loclists to be
28795 emitted later, so we can't reuse the id for something
28796 else. However, it's good to know whether a view is
28797 known to be zero, because then we may be able to
28798 optimize out locviews that are all zeros, so take
28799 note of it in zero_view_p. */
28800 if (!zero_view_p)
28801 zero_view_p = BITMAP_GGC_ALLOC ();
28802 bitmap_set_bit (zero_view_p, lvugid);
28803 table->view = ++lvugid;
28806 putc ('\n', asm_out_file);
28808 else
28810 unsigned int label_num = ++line_info_label_num;
28812 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
28814 if (debug_variable_location_views && !RESETTING_VIEW_P (table->view))
28815 push_dw_line_info_entry (table, LI_adv_address, label_num);
28816 else
28817 push_dw_line_info_entry (table, LI_set_address, label_num);
28818 if (debug_variable_location_views)
28820 bool resetting = FORCE_RESETTING_VIEW_P (table->view);
28821 if (resetting)
28822 table->view = 0;
28824 if (flag_debug_asm)
28825 fprintf (asm_out_file, "\t%s view %s%d\n",
28826 ASM_COMMENT_START,
28827 resetting ? "-" : "",
28828 table->view);
28830 table->view++;
28832 if (file_num != table->file_num)
28833 push_dw_line_info_entry (table, LI_set_file, file_num);
28834 if (discriminator != table->discrim_num)
28835 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
28836 if (is_stmt != table->is_stmt)
28837 push_dw_line_info_entry (table, LI_negate_stmt, 0);
28838 push_dw_line_info_entry (table, LI_set_line, line);
28839 if (debug_column_info)
28840 push_dw_line_info_entry (table, LI_set_column, column);
28843 table->file_num = file_num;
28844 table->line_num = line;
28845 table->column_num = column;
28846 table->discrim_num = discriminator;
28847 table->is_stmt = is_stmt;
28848 table->in_use = true;
28851 /* Record a source file location for a DECL_IGNORED_P function. */
28853 static void
28854 dwarf2out_set_ignored_loc (unsigned int line, unsigned int column,
28855 const char *filename)
28857 dw_fde_ref fde = cfun->fde;
28859 fde->ignored_debug = false;
28860 set_cur_line_info_table (function_section (fde->decl));
28862 dwarf2out_source_line (line, column, filename, 0, true);
28865 /* Record the beginning of a new source file. */
28867 static void
28868 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
28870 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28872 macinfo_entry e;
28873 e.code = DW_MACINFO_start_file;
28874 e.lineno = lineno;
28875 e.info = ggc_strdup (filename);
28876 vec_safe_push (macinfo_table, e);
28880 /* Record the end of a source file. */
28882 static void
28883 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
28885 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28887 macinfo_entry e;
28888 e.code = DW_MACINFO_end_file;
28889 e.lineno = lineno;
28890 e.info = NULL;
28891 vec_safe_push (macinfo_table, e);
28895 /* Called from debug_define in toplev.cc. The `buffer' parameter contains
28896 the tail part of the directive line, i.e. the part which is past the
28897 initial whitespace, #, whitespace, directive-name, whitespace part. */
28899 static void
28900 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
28901 const char *buffer ATTRIBUTE_UNUSED)
28903 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28905 macinfo_entry e;
28906 /* Insert a dummy first entry to be able to optimize the whole
28907 predefined macro block using DW_MACRO_import. */
28908 if (macinfo_table->is_empty () && lineno <= 1)
28910 e.code = 0;
28911 e.lineno = 0;
28912 e.info = NULL;
28913 vec_safe_push (macinfo_table, e);
28915 e.code = DW_MACINFO_define;
28916 e.lineno = lineno;
28917 e.info = ggc_strdup (buffer);
28918 vec_safe_push (macinfo_table, e);
28922 /* Called from debug_undef in toplev.cc. The `buffer' parameter contains
28923 the tail part of the directive line, i.e. the part which is past the
28924 initial whitespace, #, whitespace, directive-name, whitespace part. */
28926 static void
28927 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
28928 const char *buffer ATTRIBUTE_UNUSED)
28930 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28932 macinfo_entry e;
28933 /* Insert a dummy first entry to be able to optimize the whole
28934 predefined macro block using DW_MACRO_import. */
28935 if (macinfo_table->is_empty () && lineno <= 1)
28937 e.code = 0;
28938 e.lineno = 0;
28939 e.info = NULL;
28940 vec_safe_push (macinfo_table, e);
28942 e.code = DW_MACINFO_undef;
28943 e.lineno = lineno;
28944 e.info = ggc_strdup (buffer);
28945 vec_safe_push (macinfo_table, e);
28949 /* Helpers to manipulate hash table of CUs. */
28951 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
28953 static inline hashval_t hash (const macinfo_entry *);
28954 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
28957 inline hashval_t
28958 macinfo_entry_hasher::hash (const macinfo_entry *entry)
28960 return htab_hash_string (entry->info);
28963 inline bool
28964 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
28965 const macinfo_entry *entry2)
28967 return !strcmp (entry1->info, entry2->info);
28970 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
28972 /* Output a single .debug_macinfo entry. */
28974 static void
28975 output_macinfo_op (macinfo_entry *ref)
28977 int file_num;
28978 size_t len;
28979 struct indirect_string_node *node;
28980 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28981 struct dwarf_file_data *fd;
28983 switch (ref->code)
28985 case DW_MACINFO_start_file:
28986 fd = lookup_filename (ref->info);
28987 file_num = maybe_emit_file (fd);
28988 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
28989 dw2_asm_output_data_uleb128 (ref->lineno,
28990 "Included from line number "
28991 HOST_WIDE_INT_PRINT_UNSIGNED,
28992 ref->lineno);
28993 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
28994 break;
28995 case DW_MACINFO_end_file:
28996 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
28997 break;
28998 case DW_MACINFO_define:
28999 case DW_MACINFO_undef:
29000 len = strlen (ref->info) + 1;
29001 if ((!dwarf_strict || dwarf_version >= 5)
29002 && len > (size_t) dwarf_offset_size
29003 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
29004 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
29006 if (dwarf_split_debug_info && dwarf_version >= 5)
29007 ref->code = ref->code == DW_MACINFO_define
29008 ? DW_MACRO_define_strx : DW_MACRO_undef_strx;
29009 else
29010 ref->code = ref->code == DW_MACINFO_define
29011 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
29012 output_macinfo_op (ref);
29013 return;
29015 dw2_asm_output_data (1, ref->code,
29016 ref->code == DW_MACINFO_define
29017 ? "Define macro" : "Undefine macro");
29018 dw2_asm_output_data_uleb128 (ref->lineno,
29019 "At line number "
29020 HOST_WIDE_INT_PRINT_UNSIGNED,
29021 ref->lineno);
29022 dw2_asm_output_nstring (ref->info, -1, "The macro");
29023 break;
29024 case DW_MACRO_define_strp:
29025 dw2_asm_output_data (1, ref->code, "Define macro strp");
29026 goto do_DW_MACRO_define_strpx;
29027 case DW_MACRO_undef_strp:
29028 dw2_asm_output_data (1, ref->code, "Undefine macro strp");
29029 goto do_DW_MACRO_define_strpx;
29030 case DW_MACRO_define_strx:
29031 dw2_asm_output_data (1, ref->code, "Define macro strx");
29032 goto do_DW_MACRO_define_strpx;
29033 case DW_MACRO_undef_strx:
29034 dw2_asm_output_data (1, ref->code, "Undefine macro strx");
29035 /* FALLTHRU */
29036 do_DW_MACRO_define_strpx:
29037 /* NB: dwarf2out_finish performs:
29038 1. save_macinfo_strings
29039 2. hash table traverse of index_string
29040 3. output_macinfo -> output_macinfo_op
29041 4. output_indirect_strings
29042 -> hash table traverse of output_index_string
29044 When output_macinfo_op is called, all index strings have been
29045 added to hash table by save_macinfo_strings and we can't pass
29046 INSERT to find_slot_with_hash which may expand hash table, even
29047 if no insertion is needed, and change hash table traverse order
29048 between index_string and output_index_string. */
29049 node = find_AT_string (ref->info, NO_INSERT);
29050 gcc_assert (node
29051 && (node->form == DW_FORM_strp
29052 || node->form == dwarf_FORM (DW_FORM_strx)));
29053 dw2_asm_output_data_uleb128 (ref->lineno,
29054 "At line number "
29055 HOST_WIDE_INT_PRINT_UNSIGNED,
29056 ref->lineno);
29057 if (node->form == DW_FORM_strp)
29058 dw2_asm_output_offset (dwarf_offset_size, node->label,
29059 debug_str_section, "The macro: \"%s\"",
29060 ref->info);
29061 else
29062 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
29063 ref->info);
29064 break;
29065 case DW_MACRO_import:
29066 dw2_asm_output_data (1, ref->code, "Import");
29067 ASM_GENERATE_INTERNAL_LABEL (label,
29068 DEBUG_MACRO_SECTION_LABEL,
29069 ref->lineno + macinfo_label_base);
29070 dw2_asm_output_offset (dwarf_offset_size, label, NULL, NULL);
29071 break;
29072 default:
29073 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
29074 ASM_COMMENT_START, (unsigned long) ref->code);
29075 break;
29079 /* Attempt to make a sequence of define/undef macinfo ops shareable with
29080 other compilation unit .debug_macinfo sections. IDX is the first
29081 index of a define/undef, return the number of ops that should be
29082 emitted in a comdat .debug_macinfo section and emit
29083 a DW_MACRO_import entry referencing it.
29084 If the define/undef entry should be emitted normally, return 0. */
29086 static unsigned
29087 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
29088 macinfo_hash_type **macinfo_htab)
29090 macinfo_entry *first, *second, *cur, *inc;
29091 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
29092 unsigned char checksum[16];
29093 struct md5_ctx ctx;
29094 char *grp_name, *tail;
29095 const char *base;
29096 unsigned int i, count, encoded_filename_len, linebuf_len;
29097 macinfo_entry **slot;
29099 first = &(*macinfo_table)[idx];
29100 second = &(*macinfo_table)[idx + 1];
29102 /* Optimize only if there are at least two consecutive define/undef ops,
29103 and either all of them are before first DW_MACINFO_start_file
29104 with lineno {0,1} (i.e. predefined macro block), or all of them are
29105 in some included header file. */
29106 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
29107 return 0;
29108 if (vec_safe_is_empty (files))
29110 if (first->lineno > 1 || second->lineno > 1)
29111 return 0;
29113 else if (first->lineno == 0)
29114 return 0;
29116 /* Find the last define/undef entry that can be grouped together
29117 with first and at the same time compute md5 checksum of their
29118 codes, linenumbers and strings. */
29119 md5_init_ctx (&ctx);
29120 for (i = idx; macinfo_table->iterate (i, &cur); i++)
29121 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
29122 break;
29123 else if (vec_safe_is_empty (files) && cur->lineno > 1)
29124 break;
29125 else
29127 unsigned char code = cur->code;
29128 md5_process_bytes (&code, 1, &ctx);
29129 checksum_uleb128 (cur->lineno, &ctx);
29130 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
29132 md5_finish_ctx (&ctx, checksum);
29133 count = i - idx;
29135 /* From the containing include filename (if any) pick up just
29136 usable characters from its basename. */
29137 if (vec_safe_is_empty (files))
29138 base = "";
29139 else
29140 base = lbasename (files->last ().info);
29141 for (encoded_filename_len = 0, i = 0; base[i]; i++)
29142 if (ISIDNUM (base[i]) || base[i] == '.')
29143 encoded_filename_len++;
29144 /* Count . at the end. */
29145 if (encoded_filename_len)
29146 encoded_filename_len++;
29148 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
29149 linebuf_len = strlen (linebuf);
29151 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
29152 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
29153 + 16 * 2 + 1);
29154 memcpy (grp_name, dwarf_offset_size == 4 ? "wm4." : "wm8.", 4);
29155 tail = grp_name + 4;
29156 if (encoded_filename_len)
29158 for (i = 0; base[i]; i++)
29159 if (ISIDNUM (base[i]) || base[i] == '.')
29160 *tail++ = base[i];
29161 *tail++ = '.';
29163 memcpy (tail, linebuf, linebuf_len);
29164 tail += linebuf_len;
29165 *tail++ = '.';
29166 for (i = 0; i < 16; i++)
29167 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
29169 /* Construct a macinfo_entry for DW_MACRO_import
29170 in the empty vector entry before the first define/undef. */
29171 inc = &(*macinfo_table)[idx - 1];
29172 inc->code = DW_MACRO_import;
29173 inc->lineno = 0;
29174 inc->info = ggc_strdup (grp_name);
29175 if (!*macinfo_htab)
29176 *macinfo_htab = new macinfo_hash_type (10);
29177 /* Avoid emitting duplicates. */
29178 slot = (*macinfo_htab)->find_slot (inc, INSERT);
29179 if (*slot != NULL)
29181 inc->code = 0;
29182 inc->info = NULL;
29183 /* If such an entry has been used before, just emit
29184 a DW_MACRO_import op. */
29185 inc = *slot;
29186 output_macinfo_op (inc);
29187 /* And clear all macinfo_entry in the range to avoid emitting them
29188 in the second pass. */
29189 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
29191 cur->code = 0;
29192 cur->info = NULL;
29195 else
29197 *slot = inc;
29198 inc->lineno = (*macinfo_htab)->elements ();
29199 output_macinfo_op (inc);
29201 return count;
29204 /* Save any strings needed by the macinfo table in the debug str
29205 table. All strings must be collected into the table by the time
29206 index_string is called. */
29208 static void
29209 save_macinfo_strings (void)
29211 unsigned len;
29212 unsigned i;
29213 macinfo_entry *ref;
29215 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
29217 switch (ref->code)
29219 /* Match the logic in output_macinfo_op to decide on
29220 indirect strings. */
29221 case DW_MACINFO_define:
29222 case DW_MACINFO_undef:
29223 len = strlen (ref->info) + 1;
29224 if ((!dwarf_strict || dwarf_version >= 5)
29225 && len > (unsigned) dwarf_offset_size
29226 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
29227 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
29228 set_indirect_string (find_AT_string (ref->info));
29229 break;
29230 case DW_MACINFO_start_file:
29231 /* -gsplit-dwarf -g3 will also output filename as indirect
29232 string. */
29233 if (!dwarf_split_debug_info)
29234 break;
29235 /* Fall through. */
29236 case DW_MACRO_define_strp:
29237 case DW_MACRO_undef_strp:
29238 case DW_MACRO_define_strx:
29239 case DW_MACRO_undef_strx:
29240 set_indirect_string (find_AT_string (ref->info));
29241 break;
29242 default:
29243 break;
29248 /* Output macinfo section(s). */
29250 static void
29251 output_macinfo (const char *debug_line_label, bool early_lto_debug)
29253 unsigned i;
29254 unsigned long length = vec_safe_length (macinfo_table);
29255 macinfo_entry *ref;
29256 vec<macinfo_entry, va_gc> *files = NULL;
29257 macinfo_hash_type *macinfo_htab = NULL;
29258 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
29260 if (! length)
29261 return;
29263 /* output_macinfo* uses these interchangeably. */
29264 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
29265 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
29266 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
29267 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
29269 /* AIX Assembler inserts the length, so adjust the reference to match the
29270 offset expected by debuggers. */
29271 strcpy (dl_section_ref, debug_line_label);
29272 if (XCOFF_DEBUGGING_INFO)
29273 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
29275 /* For .debug_macro emit the section header. */
29276 if (!dwarf_strict || dwarf_version >= 5)
29278 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
29279 "DWARF macro version number");
29280 if (dwarf_offset_size == 8)
29281 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
29282 else
29283 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
29284 dw2_asm_output_offset (dwarf_offset_size, debug_line_label,
29285 debug_line_section, NULL);
29288 /* In the first loop, it emits the primary .debug_macinfo section
29289 and after each emitted op the macinfo_entry is cleared.
29290 If a longer range of define/undef ops can be optimized using
29291 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
29292 the vector before the first define/undef in the range and the
29293 whole range of define/undef ops is not emitted and kept. */
29294 for (i = 0; macinfo_table->iterate (i, &ref); i++)
29296 switch (ref->code)
29298 case DW_MACINFO_start_file:
29299 vec_safe_push (files, *ref);
29300 break;
29301 case DW_MACINFO_end_file:
29302 if (!vec_safe_is_empty (files))
29303 files->pop ();
29304 break;
29305 case DW_MACINFO_define:
29306 case DW_MACINFO_undef:
29307 if ((!dwarf_strict || dwarf_version >= 5)
29308 && !dwarf_split_debug_info
29309 && HAVE_COMDAT_GROUP
29310 && vec_safe_length (files) != 1
29311 && i > 0
29312 && i + 1 < length
29313 && (*macinfo_table)[i - 1].code == 0)
29315 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
29316 if (count)
29318 i += count - 1;
29319 continue;
29322 break;
29323 case 0:
29324 /* A dummy entry may be inserted at the beginning to be able
29325 to optimize the whole block of predefined macros. */
29326 if (i == 0)
29327 continue;
29328 default:
29329 break;
29331 output_macinfo_op (ref);
29332 ref->info = NULL;
29333 ref->code = 0;
29336 if (!macinfo_htab)
29337 return;
29339 /* Save the number of transparent includes so we can adjust the
29340 label number for the fat LTO object DWARF. */
29341 unsigned macinfo_label_base_adj = macinfo_htab->elements ();
29343 delete macinfo_htab;
29344 macinfo_htab = NULL;
29346 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
29347 terminate the current chain and switch to a new comdat .debug_macinfo
29348 section and emit the define/undef entries within it. */
29349 for (i = 0; macinfo_table->iterate (i, &ref); i++)
29350 switch (ref->code)
29352 case 0:
29353 continue;
29354 case DW_MACRO_import:
29356 char label[MAX_ARTIFICIAL_LABEL_BYTES];
29357 tree comdat_key = get_identifier (ref->info);
29358 /* Terminate the previous .debug_macinfo section. */
29359 dw2_asm_output_data (1, 0, "End compilation unit");
29360 targetm.asm_out.named_section (debug_macinfo_section_name,
29361 SECTION_DEBUG
29362 | SECTION_LINKONCE
29363 | (early_lto_debug
29364 ? SECTION_EXCLUDE : 0),
29365 comdat_key);
29366 ASM_GENERATE_INTERNAL_LABEL (label,
29367 DEBUG_MACRO_SECTION_LABEL,
29368 ref->lineno + macinfo_label_base);
29369 ASM_OUTPUT_LABEL (asm_out_file, label);
29370 ref->code = 0;
29371 ref->info = NULL;
29372 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
29373 "DWARF macro version number");
29374 if (dwarf_offset_size == 8)
29375 dw2_asm_output_data (1, 1, "Flags: 64-bit");
29376 else
29377 dw2_asm_output_data (1, 0, "Flags: 32-bit");
29379 break;
29380 case DW_MACINFO_define:
29381 case DW_MACINFO_undef:
29382 output_macinfo_op (ref);
29383 ref->code = 0;
29384 ref->info = NULL;
29385 break;
29386 default:
29387 gcc_unreachable ();
29390 macinfo_label_base += macinfo_label_base_adj;
29393 /* As init_sections_and_labels may get called multiple times, have a
29394 generation count for labels. */
29395 static unsigned init_sections_and_labels_generation;
29397 /* Initialize the various sections and labels for dwarf output and prefix
29398 them with PREFIX if non-NULL. Returns the generation (zero based
29399 number of times function was called). */
29401 static unsigned
29402 init_sections_and_labels (bool early_lto_debug)
29404 if (early_lto_debug)
29406 if (!dwarf_split_debug_info)
29408 debug_info_section = get_section (DEBUG_LTO_INFO_SECTION,
29409 SECTION_DEBUG | SECTION_EXCLUDE,
29410 NULL);
29411 debug_abbrev_section = get_section (DEBUG_LTO_ABBREV_SECTION,
29412 SECTION_DEBUG | SECTION_EXCLUDE,
29413 NULL);
29414 debug_macinfo_section_name
29415 = ((dwarf_strict && dwarf_version < 5)
29416 ? DEBUG_LTO_MACINFO_SECTION : DEBUG_LTO_MACRO_SECTION);
29417 debug_macinfo_section = get_section (debug_macinfo_section_name,
29418 SECTION_DEBUG
29419 | SECTION_EXCLUDE, NULL);
29421 else
29423 /* ??? Which of the following do we need early? */
29424 debug_info_section = get_section (DEBUG_LTO_DWO_INFO_SECTION,
29425 SECTION_DEBUG | SECTION_EXCLUDE,
29426 NULL);
29427 debug_abbrev_section = get_section (DEBUG_LTO_DWO_ABBREV_SECTION,
29428 SECTION_DEBUG | SECTION_EXCLUDE,
29429 NULL);
29430 debug_skeleton_info_section = get_section (DEBUG_LTO_INFO_SECTION,
29431 SECTION_DEBUG
29432 | SECTION_EXCLUDE, NULL);
29433 debug_skeleton_abbrev_section
29434 = get_section (DEBUG_LTO_ABBREV_SECTION,
29435 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29436 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
29437 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
29438 init_sections_and_labels_generation);
29440 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
29441 stay in the main .o, but the skeleton_line goes into the split
29442 off dwo. */
29443 debug_skeleton_line_section
29444 = get_section (DEBUG_LTO_LINE_SECTION,
29445 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29446 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
29447 DEBUG_SKELETON_LINE_SECTION_LABEL,
29448 init_sections_and_labels_generation);
29449 debug_str_offsets_section
29450 = get_section (DEBUG_LTO_DWO_STR_OFFSETS_SECTION,
29451 SECTION_DEBUG | SECTION_EXCLUDE,
29452 NULL);
29453 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
29454 DEBUG_SKELETON_INFO_SECTION_LABEL,
29455 init_sections_and_labels_generation);
29456 debug_str_dwo_section = get_section (DEBUG_LTO_STR_DWO_SECTION,
29457 DEBUG_STR_DWO_SECTION_FLAGS,
29458 NULL);
29459 debug_macinfo_section_name
29460 = ((dwarf_strict && dwarf_version < 5)
29461 ? DEBUG_LTO_DWO_MACINFO_SECTION : DEBUG_LTO_DWO_MACRO_SECTION);
29462 debug_macinfo_section = get_section (debug_macinfo_section_name,
29463 SECTION_DEBUG | SECTION_EXCLUDE,
29464 NULL);
29466 /* For macro info and the file table we have to refer to a
29467 debug_line section. */
29468 debug_line_section = get_section (DEBUG_LTO_LINE_SECTION,
29469 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29470 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
29471 DEBUG_LINE_SECTION_LABEL,
29472 init_sections_and_labels_generation);
29474 debug_str_section = get_section (DEBUG_LTO_STR_SECTION,
29475 DEBUG_STR_SECTION_FLAGS
29476 | SECTION_EXCLUDE, NULL);
29477 if (!dwarf_split_debug_info)
29478 debug_line_str_section
29479 = get_section (DEBUG_LTO_LINE_STR_SECTION,
29480 DEBUG_STR_SECTION_FLAGS | SECTION_EXCLUDE, NULL);
29482 else
29484 if (!dwarf_split_debug_info)
29486 debug_info_section = get_section (DEBUG_INFO_SECTION,
29487 SECTION_DEBUG, NULL);
29488 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
29489 SECTION_DEBUG, NULL);
29490 debug_loc_section = get_section (dwarf_version >= 5
29491 ? DEBUG_LOCLISTS_SECTION
29492 : DEBUG_LOC_SECTION,
29493 SECTION_DEBUG, NULL);
29494 debug_macinfo_section_name
29495 = ((dwarf_strict && dwarf_version < 5)
29496 ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION);
29497 debug_macinfo_section = get_section (debug_macinfo_section_name,
29498 SECTION_DEBUG, NULL);
29500 else
29502 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
29503 SECTION_DEBUG | SECTION_EXCLUDE,
29504 NULL);
29505 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
29506 SECTION_DEBUG | SECTION_EXCLUDE,
29507 NULL);
29508 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
29509 SECTION_DEBUG, NULL);
29510 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
29511 SECTION_DEBUG, NULL);
29512 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
29513 SECTION_DEBUG, NULL);
29514 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
29515 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
29516 init_sections_and_labels_generation);
29518 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
29519 stay in the main .o, but the skeleton_line goes into the
29520 split off dwo. */
29521 debug_skeleton_line_section
29522 = get_section (DEBUG_DWO_LINE_SECTION,
29523 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29524 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
29525 DEBUG_SKELETON_LINE_SECTION_LABEL,
29526 init_sections_and_labels_generation);
29527 debug_str_offsets_section
29528 = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
29529 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29530 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
29531 DEBUG_SKELETON_INFO_SECTION_LABEL,
29532 init_sections_and_labels_generation);
29533 debug_loc_section = get_section (dwarf_version >= 5
29534 ? DEBUG_DWO_LOCLISTS_SECTION
29535 : DEBUG_DWO_LOC_SECTION,
29536 SECTION_DEBUG | SECTION_EXCLUDE,
29537 NULL);
29538 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
29539 DEBUG_STR_DWO_SECTION_FLAGS,
29540 NULL);
29541 debug_macinfo_section_name
29542 = ((dwarf_strict && dwarf_version < 5)
29543 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION);
29544 debug_macinfo_section = get_section (debug_macinfo_section_name,
29545 SECTION_DEBUG | SECTION_EXCLUDE,
29546 NULL);
29547 if (dwarf_version >= 5)
29548 debug_ranges_dwo_section
29549 = get_section (DEBUG_DWO_RNGLISTS_SECTION,
29550 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29552 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
29553 SECTION_DEBUG, NULL);
29554 debug_line_section = get_section (DEBUG_LINE_SECTION,
29555 SECTION_DEBUG, NULL);
29556 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
29557 SECTION_DEBUG, NULL);
29558 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
29559 SECTION_DEBUG, NULL);
29560 debug_str_section = get_section (DEBUG_STR_SECTION,
29561 DEBUG_STR_SECTION_FLAGS, NULL);
29562 if ((!dwarf_split_debug_info && !output_asm_line_debug_info ())
29563 || asm_outputs_debug_line_str ())
29564 debug_line_str_section = get_section (DEBUG_LINE_STR_SECTION,
29565 DEBUG_STR_SECTION_FLAGS, NULL);
29567 debug_ranges_section = get_section (dwarf_version >= 5
29568 ? DEBUG_RNGLISTS_SECTION
29569 : DEBUG_RANGES_SECTION,
29570 SECTION_DEBUG, NULL);
29571 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
29572 SECTION_DEBUG, NULL);
29575 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
29576 DEBUG_ABBREV_SECTION_LABEL,
29577 init_sections_and_labels_generation);
29578 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
29579 DEBUG_INFO_SECTION_LABEL,
29580 init_sections_and_labels_generation);
29581 info_section_emitted = false;
29582 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
29583 DEBUG_LINE_SECTION_LABEL,
29584 init_sections_and_labels_generation);
29585 /* There are up to 6 unique ranges labels per generation.
29586 See also output_rnglists. */
29587 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
29588 DEBUG_RANGES_SECTION_LABEL,
29589 init_sections_and_labels_generation * 6);
29590 if (dwarf_version >= 5 && dwarf_split_debug_info)
29591 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
29592 DEBUG_RANGES_SECTION_LABEL,
29593 1 + init_sections_and_labels_generation * 6);
29594 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
29595 DEBUG_ADDR_SECTION_LABEL,
29596 init_sections_and_labels_generation);
29597 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
29598 (dwarf_strict && dwarf_version < 5)
29599 ? DEBUG_MACINFO_SECTION_LABEL
29600 : DEBUG_MACRO_SECTION_LABEL,
29601 init_sections_and_labels_generation);
29602 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL,
29603 init_sections_and_labels_generation);
29605 ++init_sections_and_labels_generation;
29606 return init_sections_and_labels_generation - 1;
29609 /* Set up for Dwarf output at the start of compilation. */
29611 static void
29612 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
29614 /* Allocate the file_table. */
29615 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
29617 #ifndef DWARF2_LINENO_DEBUGGING_INFO
29618 /* Allocate the decl_die_table. */
29619 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
29621 /* Allocate the decl_loc_table. */
29622 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
29624 /* Allocate the cached_dw_loc_list_table. */
29625 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
29627 /* Allocate the initial hunk of the abbrev_die_table. */
29628 vec_alloc (abbrev_die_table, 256);
29629 /* Zero-th entry is allocated, but unused. */
29630 abbrev_die_table->quick_push (NULL);
29632 /* Allocate the dwarf_proc_stack_usage_map. */
29633 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
29635 /* Allocate the pubtypes and pubnames vectors. */
29636 vec_alloc (pubname_table, 32);
29637 vec_alloc (pubtype_table, 32);
29639 vec_alloc (incomplete_types, 64);
29641 vec_alloc (used_rtx_array, 32);
29643 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
29644 vec_alloc (macinfo_table, 64);
29645 #endif
29647 /* If front-ends already registered a main translation unit but we were not
29648 ready to perform the association, do this now. */
29649 if (main_translation_unit != NULL_TREE)
29650 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
29653 /* Called before compile () starts outputtting functions, variables
29654 and toplevel asms into assembly. */
29656 static void
29657 dwarf2out_assembly_start (void)
29659 if (text_section_line_info)
29660 return;
29662 #ifndef DWARF2_LINENO_DEBUGGING_INFO
29663 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
29664 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
29665 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
29666 COLD_TEXT_SECTION_LABEL, 0);
29667 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
29669 switch_to_section (text_section);
29670 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
29671 #endif
29673 /* Make sure the line number table for .text always exists. */
29674 text_section_line_info = new_line_info_table ();
29675 text_section_line_info->end_label = text_end_label;
29677 #ifdef DWARF2_LINENO_DEBUGGING_INFO
29678 cur_line_info_table = text_section_line_info;
29679 #endif
29681 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
29682 && dwarf2out_do_cfi_asm ()
29683 && !dwarf2out_do_eh_frame ())
29684 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
29686 #if defined(HAVE_AS_GDWARF_5_DEBUG_FLAG) && defined(HAVE_AS_WORKING_DWARF_N_FLAG)
29687 if (output_asm_line_debug_info () && dwarf_version >= 5)
29689 /* When gas outputs DWARF5 .debug_line[_str] then we have to
29690 tell it the comp_dir and main file name for the zero entry
29691 line table. */
29692 const char *comp_dir, *filename0;
29694 comp_dir = comp_dir_string ();
29695 if (comp_dir == NULL)
29696 comp_dir = "";
29698 filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
29699 if (filename0 == NULL)
29700 filename0 = "";
29702 fprintf (asm_out_file, "\t.file 0 ");
29703 output_quoted_string (asm_out_file, remap_debug_filename (comp_dir));
29704 fputc (' ', asm_out_file);
29705 output_quoted_string (asm_out_file, remap_debug_filename (filename0));
29706 fputc ('\n', asm_out_file);
29708 else
29709 #endif
29710 /* Work around for PR101575: output a dummy .file directive. */
29711 if (!last_emitted_file && dwarf_debuginfo_p ()
29712 && debug_info_level >= DINFO_LEVEL_TERSE)
29714 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
29716 if (filename0 == NULL)
29717 filename0 = "<dummy>";
29718 maybe_emit_file (lookup_filename (filename0));
29722 /* A helper function for dwarf2out_finish called through
29723 htab_traverse. Assign a string its index. All strings must be
29724 collected into the table by the time index_string is called,
29725 because the indexing code relies on htab_traverse to traverse nodes
29726 in the same order for each run. */
29729 index_string (indirect_string_node **h, unsigned int *index)
29731 indirect_string_node *node = *h;
29733 find_string_form (node);
29734 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29736 gcc_assert (node->index == NO_INDEX_ASSIGNED);
29737 node->index = *index;
29738 *index += 1;
29740 return 1;
29743 /* A helper function for output_indirect_strings called through
29744 htab_traverse. Output the offset to a string and update the
29745 current offset. */
29748 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
29750 indirect_string_node *node = *h;
29752 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29754 /* Assert that this node has been assigned an index. */
29755 gcc_assert (node->index != NO_INDEX_ASSIGNED
29756 && node->index != NOT_INDEXED);
29757 dw2_asm_output_data (dwarf_offset_size, *offset,
29758 "indexed string 0x%x: %s", node->index, node->str);
29759 *offset += strlen (node->str) + 1;
29761 return 1;
29764 /* A helper function for dwarf2out_finish called through
29765 htab_traverse. Output the indexed string. */
29768 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
29770 struct indirect_string_node *node = *h;
29772 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29774 /* Assert that the strings are output in the same order as their
29775 indexes were assigned. */
29776 gcc_assert (*cur_idx == node->index);
29777 assemble_string (node->str, strlen (node->str) + 1);
29778 *cur_idx += 1;
29780 return 1;
29783 /* A helper function for output_indirect_strings. Counts the number
29784 of index strings offsets. Must match the logic of the functions
29785 output_index_string[_offsets] above. */
29787 count_index_strings (indirect_string_node **h, unsigned int *last_idx)
29789 struct indirect_string_node *node = *h;
29791 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29792 *last_idx += 1;
29793 return 1;
29796 /* A helper function for dwarf2out_finish called through
29797 htab_traverse. Emit one queued .debug_str string. */
29800 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
29802 struct indirect_string_node *node = *h;
29804 node->form = find_string_form (node);
29805 if (node->form == form && node->refcount > 0)
29807 ASM_OUTPUT_LABEL (asm_out_file, node->label);
29808 assemble_string (node->str, strlen (node->str) + 1);
29811 return 1;
29814 /* Output the indexed string table. */
29816 static void
29817 output_indirect_strings (void)
29819 switch_to_section (debug_str_section);
29820 if (!dwarf_split_debug_info)
29821 debug_str_hash->traverse<enum dwarf_form,
29822 output_indirect_string> (DW_FORM_strp);
29823 else
29825 unsigned int offset = 0;
29826 unsigned int cur_idx = 0;
29828 if (skeleton_debug_str_hash)
29829 skeleton_debug_str_hash->traverse<enum dwarf_form,
29830 output_indirect_string> (DW_FORM_strp);
29832 switch_to_section (debug_str_offsets_section);
29833 /* For DWARF5 the .debug_str_offsets[.dwo] section needs a unit
29834 header. Note that we don't need to generate a label to the
29835 actual index table following the header here, because this is
29836 for the split dwarf case only. In an .dwo file there is only
29837 one string offsets table (and one debug info section). But
29838 if we would start using string offset tables for the main (or
29839 skeleton) unit, then we have to add a DW_AT_str_offsets_base
29840 pointing to the actual index after the header. Split dwarf
29841 units will never have a string offsets base attribute. When
29842 a split unit is moved into a .dwp file the string offsets can
29843 be found through the .debug_cu_index section table. */
29844 if (dwarf_version >= 5)
29846 unsigned int last_idx = 0;
29847 unsigned long str_offsets_length;
29849 debug_str_hash->traverse_noresize
29850 <unsigned int *, count_index_strings> (&last_idx);
29851 str_offsets_length = last_idx * dwarf_offset_size + 4;
29852 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
29853 dw2_asm_output_data (4, 0xffffffff,
29854 "Escape value for 64-bit DWARF extension");
29855 dw2_asm_output_data (dwarf_offset_size, str_offsets_length,
29856 "Length of string offsets unit");
29857 dw2_asm_output_data (2, 5, "DWARF string offsets version");
29858 dw2_asm_output_data (2, 0, "Header zero padding");
29860 debug_str_hash->traverse_noresize
29861 <unsigned int *, output_index_string_offset> (&offset);
29862 switch_to_section (debug_str_dwo_section);
29863 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
29864 (&cur_idx);
29868 /* Callback for htab_traverse to assign an index to an entry in the
29869 table, and to write that entry to the .debug_addr section. */
29872 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
29874 addr_table_entry *entry = *slot;
29876 if (entry->refcount == 0)
29878 gcc_assert (entry->index == NO_INDEX_ASSIGNED
29879 || entry->index == NOT_INDEXED);
29880 return 1;
29883 gcc_assert (entry->index == *cur_index);
29884 (*cur_index)++;
29886 switch (entry->kind)
29888 case ate_kind_rtx:
29889 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
29890 "0x%x", entry->index);
29891 break;
29892 case ate_kind_rtx_dtprel:
29893 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
29894 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
29895 DWARF2_ADDR_SIZE,
29896 entry->addr.rtl);
29897 fputc ('\n', asm_out_file);
29898 break;
29899 case ate_kind_label:
29900 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
29901 "0x%x", entry->index);
29902 break;
29903 default:
29904 gcc_unreachable ();
29906 return 1;
29909 /* A helper function for dwarf2out_finish. Counts the number
29910 of indexed addresses. Must match the logic of the functions
29911 output_addr_table_entry above. */
29913 count_index_addrs (addr_table_entry **slot, unsigned int *last_idx)
29915 addr_table_entry *entry = *slot;
29917 if (entry->refcount > 0)
29918 *last_idx += 1;
29919 return 1;
29922 /* Produce the .debug_addr section. */
29924 static void
29925 output_addr_table (void)
29927 unsigned int index = 0;
29928 if (addr_index_table == NULL || addr_index_table->size () == 0)
29929 return;
29931 switch_to_section (debug_addr_section);
29932 /* GNU DebugFission https://gcc.gnu.org/wiki/DebugFission
29933 which GCC uses to implement -gsplit-dwarf as DWARF GNU extension
29934 before DWARF5, didn't have a header for .debug_addr units.
29935 DWARF5 specifies a small header when address tables are used. */
29936 if (dwarf_version >= 5)
29938 unsigned int last_idx = 0;
29939 unsigned long addrs_length;
29941 addr_index_table->traverse_noresize
29942 <unsigned int *, count_index_addrs> (&last_idx);
29943 addrs_length = last_idx * DWARF2_ADDR_SIZE + 4;
29945 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
29946 dw2_asm_output_data (4, 0xffffffff,
29947 "Escape value for 64-bit DWARF extension");
29948 dw2_asm_output_data (dwarf_offset_size, addrs_length,
29949 "Length of Address Unit");
29950 dw2_asm_output_data (2, 5, "DWARF addr version");
29951 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
29952 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
29954 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
29956 addr_index_table
29957 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
29960 #if ENABLE_ASSERT_CHECKING
29961 /* Verify that all marks are clear. */
29963 static void
29964 verify_marks_clear (dw_die_ref die)
29966 dw_die_ref c;
29968 gcc_assert (! die->die_mark);
29969 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
29971 #endif /* ENABLE_ASSERT_CHECKING */
29973 /* Clear the marks for a die and its children.
29974 Be cool if the mark isn't set. */
29976 static void
29977 prune_unmark_dies (dw_die_ref die)
29979 dw_die_ref c;
29981 if (die->die_mark)
29982 die->die_mark = 0;
29983 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
29986 /* Given LOC that is referenced by a DIE we're marking as used, find all
29987 referenced DWARF procedures it references and mark them as used. */
29989 static void
29990 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
29992 for (; loc != NULL; loc = loc->dw_loc_next)
29993 switch (loc->dw_loc_opc)
29995 case DW_OP_implicit_pointer:
29996 case DW_OP_convert:
29997 case DW_OP_reinterpret:
29998 case DW_OP_GNU_implicit_pointer:
29999 case DW_OP_GNU_convert:
30000 case DW_OP_GNU_reinterpret:
30001 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
30002 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
30003 break;
30004 case DW_OP_GNU_variable_value:
30005 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30007 dw_die_ref ref
30008 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
30009 if (ref == NULL)
30010 break;
30011 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30012 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30013 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30015 /* FALLTHRU */
30016 case DW_OP_call2:
30017 case DW_OP_call4:
30018 case DW_OP_call_ref:
30019 case DW_OP_const_type:
30020 case DW_OP_GNU_const_type:
30021 case DW_OP_GNU_parameter_ref:
30022 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
30023 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
30024 break;
30025 case DW_OP_regval_type:
30026 case DW_OP_deref_type:
30027 case DW_OP_GNU_regval_type:
30028 case DW_OP_GNU_deref_type:
30029 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
30030 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
30031 break;
30032 case DW_OP_entry_value:
30033 case DW_OP_GNU_entry_value:
30034 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
30035 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
30036 break;
30037 default:
30038 break;
30042 /* Given DIE that we're marking as used, find any other dies
30043 it references as attributes and mark them as used. */
30045 static void
30046 prune_unused_types_walk_attribs (dw_die_ref die)
30048 dw_attr_node *a;
30049 unsigned ix;
30051 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30053 switch (AT_class (a))
30055 /* Make sure DWARF procedures referenced by location descriptions will
30056 get emitted. */
30057 case dw_val_class_loc:
30058 prune_unused_types_walk_loc_descr (AT_loc (a));
30059 break;
30060 case dw_val_class_loc_list:
30061 for (dw_loc_list_ref list = AT_loc_list (a);
30062 list != NULL;
30063 list = list->dw_loc_next)
30064 prune_unused_types_walk_loc_descr (list->expr);
30065 break;
30067 case dw_val_class_view_list:
30068 /* This points to a loc_list in another attribute, so it's
30069 already covered. */
30070 break;
30072 case dw_val_class_die_ref:
30073 /* A reference to another DIE.
30074 Make sure that it will get emitted.
30075 If it was broken out into a comdat group, don't follow it. */
30076 if (! AT_ref (a)->comdat_type_p
30077 || a->dw_attr == DW_AT_specification)
30078 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
30079 break;
30081 case dw_val_class_str:
30082 /* Set the string's refcount to 0 so that prune_unused_types_mark
30083 accounts properly for it. */
30084 a->dw_attr_val.v.val_str->refcount = 0;
30085 break;
30087 default:
30088 break;
30093 /* Mark the generic parameters and arguments children DIEs of DIE. */
30095 static void
30096 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
30098 dw_die_ref c;
30100 if (die == NULL || die->die_child == NULL)
30101 return;
30102 c = die->die_child;
30105 if (is_template_parameter (c))
30106 prune_unused_types_mark (c, 1);
30107 c = c->die_sib;
30108 } while (c && c != die->die_child);
30111 /* Mark DIE as being used. If DOKIDS is true, then walk down
30112 to DIE's children. */
30114 static void
30115 prune_unused_types_mark (dw_die_ref die, int dokids)
30117 dw_die_ref c;
30119 if (die->die_mark == 0)
30121 /* We haven't done this node yet. Mark it as used. */
30122 die->die_mark = 1;
30123 /* If this is the DIE of a generic type instantiation,
30124 mark the children DIEs that describe its generic parms and
30125 args. */
30126 prune_unused_types_mark_generic_parms_dies (die);
30128 /* We also have to mark its parents as used.
30129 (But we don't want to mark our parent's kids due to this,
30130 unless it is a class.) */
30131 if (die->die_parent)
30132 prune_unused_types_mark (die->die_parent,
30133 class_scope_p (die->die_parent));
30135 /* Mark any referenced nodes. */
30136 prune_unused_types_walk_attribs (die);
30138 /* If this node is a specification,
30139 also mark the definition, if it exists. */
30140 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
30141 prune_unused_types_mark (die->die_definition, 1);
30144 if (dokids && die->die_mark != 2)
30146 /* We need to walk the children, but haven't done so yet.
30147 Remember that we've walked the kids. */
30148 die->die_mark = 2;
30150 /* If this is an array type, we need to make sure our
30151 kids get marked, even if they're types. If we're
30152 breaking out types into comdat sections, do this
30153 for all type definitions. */
30154 if (die->die_tag == DW_TAG_array_type
30155 || (use_debug_types
30156 && is_type_die (die) && ! is_declaration_die (die)))
30157 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
30158 else
30159 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
30163 /* For local classes, look if any static member functions were emitted
30164 and if so, mark them. */
30166 static void
30167 prune_unused_types_walk_local_classes (dw_die_ref die)
30169 dw_die_ref c;
30171 if (die->die_mark == 2)
30172 return;
30174 switch (die->die_tag)
30176 case DW_TAG_structure_type:
30177 case DW_TAG_union_type:
30178 case DW_TAG_class_type:
30179 case DW_TAG_interface_type:
30180 break;
30182 case DW_TAG_subprogram:
30183 if (!get_AT_flag (die, DW_AT_declaration)
30184 || die->die_definition != NULL)
30185 prune_unused_types_mark (die, 1);
30186 return;
30188 default:
30189 return;
30192 /* Mark children. */
30193 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
30196 /* Walk the tree DIE and mark types that we actually use. */
30198 static void
30199 prune_unused_types_walk (dw_die_ref die)
30201 dw_die_ref c;
30203 /* Don't do anything if this node is already marked and
30204 children have been marked as well. */
30205 if (die->die_mark == 2)
30206 return;
30208 switch (die->die_tag)
30210 case DW_TAG_structure_type:
30211 case DW_TAG_union_type:
30212 case DW_TAG_class_type:
30213 case DW_TAG_interface_type:
30214 if (die->die_perennial_p)
30215 break;
30217 for (c = die->die_parent; c; c = c->die_parent)
30218 if (c->die_tag == DW_TAG_subprogram)
30219 break;
30221 /* Finding used static member functions inside of classes
30222 is needed just for local classes, because for other classes
30223 static member function DIEs with DW_AT_specification
30224 are emitted outside of the DW_TAG_*_type. If we ever change
30225 it, we'd need to call this even for non-local classes. */
30226 if (c)
30227 prune_unused_types_walk_local_classes (die);
30229 /* It's a type node --- don't mark it. */
30230 return;
30232 case DW_TAG_const_type:
30233 case DW_TAG_packed_type:
30234 case DW_TAG_pointer_type:
30235 case DW_TAG_reference_type:
30236 case DW_TAG_rvalue_reference_type:
30237 case DW_TAG_volatile_type:
30238 case DW_TAG_restrict_type:
30239 case DW_TAG_shared_type:
30240 case DW_TAG_atomic_type:
30241 case DW_TAG_immutable_type:
30242 case DW_TAG_typedef:
30243 case DW_TAG_array_type:
30244 case DW_TAG_coarray_type:
30245 case DW_TAG_friend:
30246 case DW_TAG_enumeration_type:
30247 case DW_TAG_subroutine_type:
30248 case DW_TAG_string_type:
30249 case DW_TAG_set_type:
30250 case DW_TAG_subrange_type:
30251 case DW_TAG_ptr_to_member_type:
30252 case DW_TAG_file_type:
30253 case DW_TAG_unspecified_type:
30254 case DW_TAG_dynamic_type:
30255 /* Type nodes are useful only when other DIEs reference them --- don't
30256 mark them. */
30257 /* FALLTHROUGH */
30259 case DW_TAG_dwarf_procedure:
30260 /* Likewise for DWARF procedures. */
30262 if (die->die_perennial_p)
30263 break;
30265 return;
30267 case DW_TAG_variable:
30268 if (flag_debug_only_used_symbols)
30270 if (die->die_perennial_p)
30271 break;
30273 /* For static data members, the declaration in the class is supposed
30274 to have DW_TAG_member tag in DWARF{3,4} but DW_TAG_variable in
30275 DWARF5. DW_TAG_member will be marked, so mark even such
30276 DW_TAG_variables in DWARF5, as long as it has DW_AT_const_value
30277 attribute. */
30278 if (dwarf_version >= 5
30279 && class_scope_p (die->die_parent)
30280 && get_AT (die, DW_AT_const_value))
30281 break;
30283 /* premark_used_variables marks external variables --- don't mark
30284 them here. But function-local externals are always considered
30285 used. */
30286 if (get_AT (die, DW_AT_external))
30288 for (c = die->die_parent; c; c = c->die_parent)
30289 if (c->die_tag == DW_TAG_subprogram)
30290 break;
30291 if (!c)
30292 return;
30295 /* FALLTHROUGH */
30297 default:
30298 /* Mark everything else. */
30299 break;
30302 if (die->die_mark == 0)
30304 die->die_mark = 1;
30306 /* Now, mark any dies referenced from here. */
30307 prune_unused_types_walk_attribs (die);
30310 die->die_mark = 2;
30312 /* Mark children. */
30313 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
30316 /* Increment the string counts on strings referred to from DIE's
30317 attributes. */
30319 static void
30320 prune_unused_types_update_strings (dw_die_ref die)
30322 dw_attr_node *a;
30323 unsigned ix;
30325 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30326 if (AT_class (a) == dw_val_class_str)
30328 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
30329 s->refcount++;
30330 /* Avoid unnecessarily putting strings that are used less than
30331 twice in the hash table. */
30332 if (s->form != DW_FORM_line_strp
30333 && (s->refcount
30334 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2)))
30336 indirect_string_node **slot
30337 = debug_str_hash->find_slot_with_hash (s->str,
30338 htab_hash_string (s->str),
30339 INSERT);
30340 gcc_assert (*slot == NULL);
30341 *slot = s;
30346 /* Mark DIE and its children as removed. */
30348 static void
30349 mark_removed (dw_die_ref die)
30351 dw_die_ref c;
30352 die->removed = true;
30353 FOR_EACH_CHILD (die, c, mark_removed (c));
30356 /* Remove from the tree DIE any dies that aren't marked. */
30358 static void
30359 prune_unused_types_prune (dw_die_ref die)
30361 dw_die_ref c;
30363 gcc_assert (die->die_mark);
30364 prune_unused_types_update_strings (die);
30366 if (! die->die_child)
30367 return;
30369 c = die->die_child;
30370 do {
30371 dw_die_ref prev = c, next;
30372 for (c = c->die_sib; ! c->die_mark; c = next)
30373 if (c == die->die_child)
30375 /* No marked children between 'prev' and the end of the list. */
30376 if (prev == c)
30377 /* No marked children at all. */
30378 die->die_child = NULL;
30379 else
30381 prev->die_sib = c->die_sib;
30382 die->die_child = prev;
30384 c->die_sib = NULL;
30385 mark_removed (c);
30386 return;
30388 else
30390 next = c->die_sib;
30391 c->die_sib = NULL;
30392 mark_removed (c);
30395 if (c != prev->die_sib)
30396 prev->die_sib = c;
30397 prune_unused_types_prune (c);
30398 } while (c != die->die_child);
30401 /* Remove dies representing declarations that we never use. */
30403 static void
30404 prune_unused_types (void)
30406 unsigned int i;
30407 limbo_die_node *node;
30408 comdat_type_node *ctnode;
30409 pubname_entry *pub;
30410 dw_die_ref base_type;
30412 #if ENABLE_ASSERT_CHECKING
30413 /* All the marks should already be clear. */
30414 verify_marks_clear (comp_unit_die ());
30415 for (node = limbo_die_list; node; node = node->next)
30416 verify_marks_clear (node->die);
30417 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30418 verify_marks_clear (ctnode->root_die);
30419 #endif /* ENABLE_ASSERT_CHECKING */
30421 /* Mark types that are used in global variables. */
30422 premark_types_used_by_global_vars ();
30424 /* Mark variables used in the symtab. */
30425 if (flag_debug_only_used_symbols)
30426 premark_used_variables ();
30428 /* Set the mark on nodes that are actually used. */
30429 prune_unused_types_walk (comp_unit_die ());
30430 for (node = limbo_die_list; node; node = node->next)
30431 prune_unused_types_walk (node->die);
30432 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30434 prune_unused_types_walk (ctnode->root_die);
30435 prune_unused_types_mark (ctnode->type_die, 1);
30438 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
30439 are unusual in that they are pubnames that are the children of pubtypes.
30440 They should only be marked via their parent DW_TAG_enumeration_type die,
30441 not as roots in themselves. */
30442 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
30443 if (pub->die->die_tag != DW_TAG_enumerator)
30444 prune_unused_types_mark (pub->die, 1);
30445 for (i = 0; base_types.iterate (i, &base_type); i++)
30446 prune_unused_types_mark (base_type, 1);
30448 /* Also set the mark on nodes that could be referenced by
30449 DW_TAG_call_site DW_AT_call_origin (i.e. direct call callees) or
30450 by DW_TAG_inlined_subroutine origins. */
30451 cgraph_node *cnode;
30452 FOR_EACH_FUNCTION (cnode)
30453 if (cnode->referred_to_p (false))
30455 dw_die_ref die = lookup_decl_die (cnode->decl);
30456 if (die == NULL || die->die_mark)
30457 continue;
30458 for (cgraph_edge *e = cnode->callers; e; e = e->next_caller)
30459 if (e->caller != cnode)
30461 prune_unused_types_mark (die, 1);
30462 break;
30466 if (debug_str_hash)
30467 debug_str_hash->empty ();
30468 if (skeleton_debug_str_hash)
30469 skeleton_debug_str_hash->empty ();
30470 prune_unused_types_prune (comp_unit_die ());
30471 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
30473 node = *pnode;
30474 if (!node->die->die_mark)
30475 *pnode = node->next;
30476 else
30478 prune_unused_types_prune (node->die);
30479 pnode = &node->next;
30482 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30483 prune_unused_types_prune (ctnode->root_die);
30485 /* Leave the marks clear. */
30486 prune_unmark_dies (comp_unit_die ());
30487 for (node = limbo_die_list; node; node = node->next)
30488 prune_unmark_dies (node->die);
30489 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30490 prune_unmark_dies (ctnode->root_die);
30493 /* Helpers to manipulate hash table of comdat type units. */
30495 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
30497 static inline hashval_t hash (const comdat_type_node *);
30498 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
30501 inline hashval_t
30502 comdat_type_hasher::hash (const comdat_type_node *type_node)
30504 hashval_t h;
30505 memcpy (&h, type_node->signature, sizeof (h));
30506 return h;
30509 inline bool
30510 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
30511 const comdat_type_node *type_node_2)
30513 return (! memcmp (type_node_1->signature, type_node_2->signature,
30514 DWARF_TYPE_SIGNATURE_SIZE));
30517 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
30518 to the location it would have been added, should we know its
30519 DECL_ASSEMBLER_NAME when we added other attributes. This will
30520 probably improve compactness of debug info, removing equivalent
30521 abbrevs, and hide any differences caused by deferring the
30522 computation of the assembler name, triggered by e.g. PCH. */
30524 static inline void
30525 move_linkage_attr (dw_die_ref die)
30527 unsigned ix = vec_safe_length (die->die_attr);
30528 dw_attr_node linkage = (*die->die_attr)[ix - 1];
30530 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
30531 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
30533 while (--ix > 0)
30535 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
30537 if (prev->dw_attr == DW_AT_decl_line
30538 || prev->dw_attr == DW_AT_decl_column
30539 || prev->dw_attr == DW_AT_name)
30540 break;
30543 if (ix != vec_safe_length (die->die_attr) - 1)
30545 die->die_attr->pop ();
30546 die->die_attr->quick_insert (ix, linkage);
30550 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
30551 referenced from typed stack ops and count how often they are used. */
30553 static void
30554 mark_base_types (dw_loc_descr_ref loc)
30556 dw_die_ref base_type = NULL;
30558 for (; loc; loc = loc->dw_loc_next)
30560 switch (loc->dw_loc_opc)
30562 case DW_OP_regval_type:
30563 case DW_OP_deref_type:
30564 case DW_OP_GNU_regval_type:
30565 case DW_OP_GNU_deref_type:
30566 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
30567 break;
30568 case DW_OP_convert:
30569 case DW_OP_reinterpret:
30570 case DW_OP_GNU_convert:
30571 case DW_OP_GNU_reinterpret:
30572 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
30573 continue;
30574 /* FALLTHRU */
30575 case DW_OP_const_type:
30576 case DW_OP_GNU_const_type:
30577 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
30578 break;
30579 case DW_OP_entry_value:
30580 case DW_OP_GNU_entry_value:
30581 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
30582 continue;
30583 default:
30584 continue;
30586 gcc_assert (base_type->die_parent == comp_unit_die ());
30587 if (base_type->die_mark)
30588 base_type->die_mark++;
30589 else
30591 base_types.safe_push (base_type);
30592 base_type->die_mark = 1;
30597 /* Stripped-down variant of resolve_addr, mark DW_TAG_base_type nodes
30598 referenced from typed stack ops and count how often they are used. */
30600 static void
30601 mark_base_types (dw_die_ref die)
30603 dw_die_ref c;
30604 dw_attr_node *a;
30605 dw_loc_list_ref *curr;
30606 unsigned ix;
30608 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30609 switch (AT_class (a))
30611 case dw_val_class_loc_list:
30612 curr = AT_loc_list_ptr (a);
30613 while (*curr)
30615 mark_base_types ((*curr)->expr);
30616 curr = &(*curr)->dw_loc_next;
30618 break;
30620 case dw_val_class_loc:
30621 mark_base_types (AT_loc (a));
30622 break;
30624 default:
30625 break;
30628 FOR_EACH_CHILD (die, c, mark_base_types (c));
30631 /* Comparison function for sorting marked base types. */
30633 static int
30634 base_type_cmp (const void *x, const void *y)
30636 dw_die_ref dx = *(const dw_die_ref *) x;
30637 dw_die_ref dy = *(const dw_die_ref *) y;
30638 unsigned int byte_size1, byte_size2;
30639 unsigned int encoding1, encoding2;
30640 unsigned int align1, align2;
30641 if (dx->die_mark > dy->die_mark)
30642 return -1;
30643 if (dx->die_mark < dy->die_mark)
30644 return 1;
30645 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
30646 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
30647 if (byte_size1 < byte_size2)
30648 return 1;
30649 if (byte_size1 > byte_size2)
30650 return -1;
30651 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
30652 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
30653 if (encoding1 < encoding2)
30654 return 1;
30655 if (encoding1 > encoding2)
30656 return -1;
30657 align1 = get_AT_unsigned (dx, DW_AT_alignment);
30658 align2 = get_AT_unsigned (dy, DW_AT_alignment);
30659 if (align1 < align2)
30660 return 1;
30661 if (align1 > align2)
30662 return -1;
30663 return 0;
30666 /* Move base types marked by mark_base_types as early as possible
30667 in the CU, sorted by decreasing usage count both to make the
30668 uleb128 references as small as possible and to make sure they
30669 will have die_offset already computed by calc_die_sizes when
30670 sizes of typed stack loc ops is computed. */
30672 static void
30673 move_marked_base_types (void)
30675 unsigned int i;
30676 dw_die_ref base_type, die, c;
30678 if (base_types.is_empty ())
30679 return;
30681 /* Sort by decreasing usage count, they will be added again in that
30682 order later on. */
30683 base_types.qsort (base_type_cmp);
30684 die = comp_unit_die ();
30685 c = die->die_child;
30688 dw_die_ref prev = c;
30689 c = c->die_sib;
30690 while (c->die_mark)
30692 remove_child_with_prev (c, prev);
30693 /* As base types got marked, there must be at least
30694 one node other than DW_TAG_base_type. */
30695 gcc_assert (die->die_child != NULL);
30696 c = prev->die_sib;
30699 while (c != die->die_child);
30700 gcc_assert (die->die_child);
30701 c = die->die_child;
30702 for (i = 0; base_types.iterate (i, &base_type); i++)
30704 base_type->die_mark = 0;
30705 base_type->die_sib = c->die_sib;
30706 c->die_sib = base_type;
30707 c = base_type;
30711 /* Helper function for resolve_addr, attempt to resolve
30712 one CONST_STRING, return true if successful. Similarly verify that
30713 SYMBOL_REFs refer to variables emitted in the current CU. */
30715 static bool
30716 resolve_one_addr (rtx *addr)
30718 rtx rtl = *addr;
30720 if (GET_CODE (rtl) == CONST_STRING)
30722 size_t len = strlen (XSTR (rtl, 0)) + 1;
30723 tree t = build_string (len, XSTR (rtl, 0));
30724 tree tlen = size_int (len - 1);
30725 TREE_TYPE (t)
30726 = build_array_type (char_type_node, build_index_type (tlen));
30727 rtl = lookup_constant_def (t);
30728 if (!rtl || !MEM_P (rtl))
30729 return false;
30730 rtl = XEXP (rtl, 0);
30731 if (GET_CODE (rtl) == SYMBOL_REF
30732 && SYMBOL_REF_DECL (rtl)
30733 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
30734 return false;
30735 vec_safe_push (used_rtx_array, rtl);
30736 *addr = rtl;
30737 return true;
30740 if (GET_CODE (rtl) == SYMBOL_REF
30741 && SYMBOL_REF_DECL (rtl))
30743 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
30745 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
30746 return false;
30748 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
30749 return false;
30752 if (GET_CODE (rtl) == CONST)
30754 subrtx_ptr_iterator::array_type array;
30755 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
30756 if (!resolve_one_addr (*iter))
30757 return false;
30760 return true;
30763 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
30764 if possible, and create DW_TAG_dwarf_procedure that can be referenced
30765 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
30767 static rtx
30768 string_cst_pool_decl (tree t)
30770 rtx rtl = output_constant_def (t, 1);
30771 unsigned char *array;
30772 dw_loc_descr_ref l;
30773 tree decl;
30774 size_t len;
30775 dw_die_ref ref;
30777 if (!rtl || !MEM_P (rtl))
30778 return NULL_RTX;
30779 rtl = XEXP (rtl, 0);
30780 if (GET_CODE (rtl) != SYMBOL_REF
30781 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
30782 return NULL_RTX;
30784 decl = SYMBOL_REF_DECL (rtl);
30785 if (!lookup_decl_die (decl))
30787 len = TREE_STRING_LENGTH (t);
30788 vec_safe_push (used_rtx_array, rtl);
30789 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
30790 array = ggc_vec_alloc<unsigned char> (len);
30791 memcpy (array, TREE_STRING_POINTER (t), len);
30792 l = new_loc_descr (DW_OP_implicit_value, len, 0);
30793 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
30794 l->dw_loc_oprnd2.v.val_vec.length = len;
30795 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
30796 l->dw_loc_oprnd2.v.val_vec.array = array;
30797 add_AT_loc (ref, DW_AT_location, l);
30798 equate_decl_number_to_die (decl, ref);
30800 return rtl;
30803 /* Helper function of resolve_addr_in_expr. LOC is
30804 a DW_OP_addr followed by DW_OP_stack_value, either at the start
30805 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
30806 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
30807 with DW_OP_implicit_pointer if possible
30808 and return true, if unsuccessful, return false. */
30810 static bool
30811 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
30813 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
30814 HOST_WIDE_INT offset = 0;
30815 dw_die_ref ref = NULL;
30816 tree decl;
30818 if (GET_CODE (rtl) == CONST
30819 && GET_CODE (XEXP (rtl, 0)) == PLUS
30820 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
30822 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
30823 rtl = XEXP (XEXP (rtl, 0), 0);
30825 if (GET_CODE (rtl) == CONST_STRING)
30827 size_t len = strlen (XSTR (rtl, 0)) + 1;
30828 tree t = build_string (len, XSTR (rtl, 0));
30829 tree tlen = size_int (len - 1);
30831 TREE_TYPE (t)
30832 = build_array_type (char_type_node, build_index_type (tlen));
30833 rtl = string_cst_pool_decl (t);
30834 if (!rtl)
30835 return false;
30837 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
30839 decl = SYMBOL_REF_DECL (rtl);
30840 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
30842 ref = lookup_decl_die (decl);
30843 if (ref && (get_AT (ref, DW_AT_location)
30844 || get_AT (ref, DW_AT_const_value)))
30846 loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
30847 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30848 loc->dw_loc_oprnd1.val_entry = NULL;
30849 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30850 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30851 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
30852 loc->dw_loc_oprnd2.v.val_int = offset;
30853 return true;
30857 return false;
30860 /* Helper function for resolve_addr, handle one location
30861 expression, return false if at least one CONST_STRING or SYMBOL_REF in
30862 the location list couldn't be resolved. */
30864 static bool
30865 resolve_addr_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
30867 dw_loc_descr_ref keep = NULL;
30868 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
30869 switch (loc->dw_loc_opc)
30871 case DW_OP_addr:
30872 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
30874 if ((prev == NULL
30875 || prev->dw_loc_opc == DW_OP_piece
30876 || prev->dw_loc_opc == DW_OP_bit_piece)
30877 && loc->dw_loc_next
30878 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
30879 && (!dwarf_strict || dwarf_version >= 5)
30880 && optimize_one_addr_into_implicit_ptr (loc))
30881 break;
30882 return false;
30884 break;
30885 case DW_OP_GNU_addr_index:
30886 case DW_OP_addrx:
30887 case DW_OP_GNU_const_index:
30888 case DW_OP_constx:
30889 if ((loc->dw_loc_opc == DW_OP_GNU_addr_index
30890 || loc->dw_loc_opc == DW_OP_addrx)
30891 || ((loc->dw_loc_opc == DW_OP_GNU_const_index
30892 || loc->dw_loc_opc == DW_OP_constx)
30893 && loc->dtprel))
30895 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
30896 if (!resolve_one_addr (&rtl))
30897 return false;
30898 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
30899 loc->dw_loc_oprnd1.val_entry
30900 = add_addr_table_entry (rtl, ate_kind_rtx);
30902 break;
30903 case DW_OP_const4u:
30904 case DW_OP_const8u:
30905 if (loc->dtprel
30906 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
30907 return false;
30908 break;
30909 case DW_OP_plus_uconst:
30910 if (size_of_loc_descr (loc)
30911 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
30913 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
30915 dw_loc_descr_ref repl
30916 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
30917 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
30918 add_loc_descr (&repl, loc->dw_loc_next);
30919 *loc = *repl;
30921 break;
30922 case DW_OP_implicit_value:
30923 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
30924 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
30925 return false;
30926 break;
30927 case DW_OP_implicit_pointer:
30928 case DW_OP_GNU_implicit_pointer:
30929 case DW_OP_GNU_parameter_ref:
30930 case DW_OP_GNU_variable_value:
30931 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30933 dw_die_ref ref
30934 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
30935 if (ref == NULL)
30936 return false;
30937 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30938 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30939 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30941 if (loc->dw_loc_opc == DW_OP_GNU_variable_value)
30943 if (prev == NULL
30944 && loc->dw_loc_next == NULL
30945 && AT_class (a) == dw_val_class_loc)
30946 switch (a->dw_attr)
30948 /* Following attributes allow both exprloc and reference,
30949 so if the whole expression is DW_OP_GNU_variable_value
30950 alone we could transform it into reference. */
30951 case DW_AT_byte_size:
30952 case DW_AT_bit_size:
30953 case DW_AT_lower_bound:
30954 case DW_AT_upper_bound:
30955 case DW_AT_bit_stride:
30956 case DW_AT_count:
30957 case DW_AT_allocated:
30958 case DW_AT_associated:
30959 case DW_AT_byte_stride:
30960 a->dw_attr_val.val_class = dw_val_class_die_ref;
30961 a->dw_attr_val.val_entry = NULL;
30962 a->dw_attr_val.v.val_die_ref.die
30963 = loc->dw_loc_oprnd1.v.val_die_ref.die;
30964 a->dw_attr_val.v.val_die_ref.external = 0;
30965 return true;
30966 default:
30967 break;
30969 if (dwarf_strict)
30970 return false;
30972 break;
30973 case DW_OP_const_type:
30974 case DW_OP_regval_type:
30975 case DW_OP_deref_type:
30976 case DW_OP_convert:
30977 case DW_OP_reinterpret:
30978 case DW_OP_GNU_const_type:
30979 case DW_OP_GNU_regval_type:
30980 case DW_OP_GNU_deref_type:
30981 case DW_OP_GNU_convert:
30982 case DW_OP_GNU_reinterpret:
30983 while (loc->dw_loc_next
30984 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
30985 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
30987 dw_die_ref base1, base2;
30988 unsigned enc1, enc2, size1, size2;
30989 if (loc->dw_loc_opc == DW_OP_regval_type
30990 || loc->dw_loc_opc == DW_OP_deref_type
30991 || loc->dw_loc_opc == DW_OP_GNU_regval_type
30992 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
30993 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
30994 else if (loc->dw_loc_oprnd1.val_class
30995 == dw_val_class_unsigned_const)
30996 break;
30997 else
30998 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
30999 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
31000 == dw_val_class_unsigned_const)
31001 break;
31002 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
31003 gcc_assert (base1->die_tag == DW_TAG_base_type
31004 && base2->die_tag == DW_TAG_base_type);
31005 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
31006 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
31007 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
31008 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
31009 if (size1 == size2
31010 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
31011 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
31012 && loc != keep)
31013 || enc1 == enc2))
31015 /* Optimize away next DW_OP_convert after
31016 adjusting LOC's base type die reference. */
31017 if (loc->dw_loc_opc == DW_OP_regval_type
31018 || loc->dw_loc_opc == DW_OP_deref_type
31019 || loc->dw_loc_opc == DW_OP_GNU_regval_type
31020 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
31021 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
31022 else
31023 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
31024 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
31025 continue;
31027 /* Don't change integer DW_OP_convert after e.g. floating
31028 point typed stack entry. */
31029 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
31030 keep = loc->dw_loc_next;
31031 break;
31033 break;
31034 default:
31035 break;
31037 return true;
31040 /* Helper function of resolve_addr. DIE had DW_AT_location of
31041 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
31042 and DW_OP_addr couldn't be resolved. resolve_addr has already
31043 removed the DW_AT_location attribute. This function attempts to
31044 add a new DW_AT_location attribute with DW_OP_implicit_pointer
31045 to it or DW_AT_const_value attribute, if possible. */
31047 static void
31048 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
31050 if (!VAR_P (decl)
31051 || lookup_decl_die (decl) != die
31052 || DECL_EXTERNAL (decl)
31053 || !TREE_STATIC (decl)
31054 || DECL_INITIAL (decl) == NULL_TREE
31055 || DECL_P (DECL_INITIAL (decl))
31056 || get_AT (die, DW_AT_const_value))
31057 return;
31059 tree init = DECL_INITIAL (decl);
31060 HOST_WIDE_INT offset = 0;
31061 /* For variables that have been optimized away and thus
31062 don't have a memory location, see if we can emit
31063 DW_AT_const_value instead. */
31064 if (tree_add_const_value_attribute (die, init))
31065 return;
31066 if (dwarf_strict && dwarf_version < 5)
31067 return;
31068 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
31069 and ADDR_EXPR refers to a decl that has DW_AT_location or
31070 DW_AT_const_value (but isn't addressable, otherwise
31071 resolving the original DW_OP_addr wouldn't fail), see if
31072 we can add DW_OP_implicit_pointer. */
31073 STRIP_NOPS (init);
31074 if (TREE_CODE (init) == POINTER_PLUS_EXPR
31075 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
31077 offset = tree_to_shwi (TREE_OPERAND (init, 1));
31078 init = TREE_OPERAND (init, 0);
31079 STRIP_NOPS (init);
31081 if (TREE_CODE (init) != ADDR_EXPR)
31082 return;
31083 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
31084 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
31085 || (VAR_P (TREE_OPERAND (init, 0))
31086 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
31087 && TREE_OPERAND (init, 0) != decl))
31089 dw_die_ref ref;
31090 dw_loc_descr_ref l;
31092 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
31094 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
31095 if (!rtl)
31096 return;
31097 decl = SYMBOL_REF_DECL (rtl);
31099 else
31100 decl = TREE_OPERAND (init, 0);
31101 ref = lookup_decl_die (decl);
31102 if (ref == NULL
31103 || (!get_AT (ref, DW_AT_location)
31104 && !get_AT (ref, DW_AT_const_value)))
31105 return;
31106 l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
31107 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
31108 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
31109 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
31110 add_AT_loc (die, DW_AT_location, l);
31114 /* Return NULL if l is a DWARF expression, or first op that is not
31115 valid DWARF expression. */
31117 static dw_loc_descr_ref
31118 non_dwarf_expression (dw_loc_descr_ref l)
31120 while (l)
31122 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
31123 return l;
31124 switch (l->dw_loc_opc)
31126 case DW_OP_regx:
31127 case DW_OP_implicit_value:
31128 case DW_OP_stack_value:
31129 case DW_OP_implicit_pointer:
31130 case DW_OP_GNU_implicit_pointer:
31131 case DW_OP_GNU_parameter_ref:
31132 case DW_OP_piece:
31133 case DW_OP_bit_piece:
31134 return l;
31135 default:
31136 break;
31138 l = l->dw_loc_next;
31140 return NULL;
31143 /* Return adjusted copy of EXPR:
31144 If it is empty DWARF expression, return it.
31145 If it is valid non-empty DWARF expression,
31146 return copy of EXPR with DW_OP_deref appended to it.
31147 If it is DWARF expression followed by DW_OP_reg{N,x}, return
31148 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended.
31149 If it is DWARF expression followed by DW_OP_stack_value, return
31150 copy of the DWARF expression without anything appended.
31151 Otherwise, return NULL. */
31153 static dw_loc_descr_ref
31154 copy_deref_exprloc (dw_loc_descr_ref expr)
31156 dw_loc_descr_ref tail = NULL;
31158 if (expr == NULL)
31159 return NULL;
31161 dw_loc_descr_ref l = non_dwarf_expression (expr);
31162 if (l && l->dw_loc_next)
31163 return NULL;
31165 if (l)
31167 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
31168 tail = new_loc_descr ((enum dwarf_location_atom)
31169 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
31170 0, 0);
31171 else
31172 switch (l->dw_loc_opc)
31174 case DW_OP_regx:
31175 tail = new_loc_descr (DW_OP_bregx,
31176 l->dw_loc_oprnd1.v.val_unsigned, 0);
31177 break;
31178 case DW_OP_stack_value:
31179 break;
31180 default:
31181 return NULL;
31184 else
31185 tail = new_loc_descr (DW_OP_deref, 0, 0);
31187 dw_loc_descr_ref ret = NULL, *p = &ret;
31188 while (expr != l)
31190 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
31191 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
31192 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
31193 p = &(*p)->dw_loc_next;
31194 expr = expr->dw_loc_next;
31196 *p = tail;
31197 return ret;
31200 /* For DW_AT_string_length attribute with DW_OP_GNU_variable_value
31201 reference to a variable or argument, adjust it if needed and return:
31202 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
31203 attribute if present should be removed
31204 0 keep the attribute perhaps with minor modifications, no need to rescan
31205 1 if the attribute has been successfully adjusted. */
31207 static int
31208 optimize_string_length (dw_attr_node *a)
31210 dw_loc_descr_ref l = AT_loc (a), lv;
31211 dw_die_ref die;
31212 if (l->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
31214 tree decl = l->dw_loc_oprnd1.v.val_decl_ref;
31215 die = lookup_decl_die (decl);
31216 if (die)
31218 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
31219 l->dw_loc_oprnd1.v.val_die_ref.die = die;
31220 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
31222 else
31223 return -1;
31225 else
31226 die = l->dw_loc_oprnd1.v.val_die_ref.die;
31228 /* DWARF5 allows reference class, so we can then reference the DIE.
31229 Only do this for DW_OP_GNU_variable_value DW_OP_stack_value. */
31230 if (l->dw_loc_next != NULL && dwarf_version >= 5)
31232 a->dw_attr_val.val_class = dw_val_class_die_ref;
31233 a->dw_attr_val.val_entry = NULL;
31234 a->dw_attr_val.v.val_die_ref.die = die;
31235 a->dw_attr_val.v.val_die_ref.external = 0;
31236 return 0;
31239 dw_attr_node *av = get_AT (die, DW_AT_location);
31240 dw_loc_list_ref d;
31241 bool non_dwarf_expr = false;
31243 if (av == NULL)
31244 return dwarf_strict ? -1 : 0;
31245 switch (AT_class (av))
31247 case dw_val_class_loc_list:
31248 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
31249 if (d->expr && non_dwarf_expression (d->expr))
31250 non_dwarf_expr = true;
31251 break;
31252 case dw_val_class_view_list:
31253 gcc_unreachable ();
31254 case dw_val_class_loc:
31255 lv = AT_loc (av);
31256 if (lv == NULL)
31257 return dwarf_strict ? -1 : 0;
31258 if (non_dwarf_expression (lv))
31259 non_dwarf_expr = true;
31260 break;
31261 default:
31262 return dwarf_strict ? -1 : 0;
31265 /* If it is safe to transform DW_OP_GNU_variable_value DW_OP_stack_value
31266 into DW_OP_call4 or DW_OP_GNU_variable_value into
31267 DW_OP_call4 DW_OP_deref, do so. */
31268 if (!non_dwarf_expr
31269 && (l->dw_loc_next != NULL || AT_class (av) == dw_val_class_loc))
31271 l->dw_loc_opc = DW_OP_call4;
31272 if (l->dw_loc_next)
31273 l->dw_loc_next = NULL;
31274 else
31275 l->dw_loc_next = new_loc_descr (DW_OP_deref, 0, 0);
31276 return 0;
31279 /* For DW_OP_GNU_variable_value DW_OP_stack_value, we can just
31280 copy over the DW_AT_location attribute from die to a. */
31281 if (l->dw_loc_next != NULL)
31283 a->dw_attr_val = av->dw_attr_val;
31284 return 1;
31287 dw_loc_list_ref list, *p;
31288 switch (AT_class (av))
31290 case dw_val_class_loc_list:
31291 p = &list;
31292 list = NULL;
31293 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
31295 lv = copy_deref_exprloc (d->expr);
31296 if (lv)
31298 *p = new_loc_list (lv, d->begin, d->vbegin, d->end, d->vend, d->section);
31299 p = &(*p)->dw_loc_next;
31301 else if (!dwarf_strict && d->expr)
31302 return 0;
31304 if (list == NULL)
31305 return dwarf_strict ? -1 : 0;
31306 a->dw_attr_val.val_class = dw_val_class_loc_list;
31307 gen_llsym (list);
31308 *AT_loc_list_ptr (a) = list;
31309 return 1;
31310 case dw_val_class_loc:
31311 lv = copy_deref_exprloc (AT_loc (av));
31312 if (lv == NULL)
31313 return dwarf_strict ? -1 : 0;
31314 a->dw_attr_val.v.val_loc = lv;
31315 return 1;
31316 default:
31317 gcc_unreachable ();
31321 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
31322 an address in .rodata section if the string literal is emitted there,
31323 or remove the containing location list or replace DW_AT_const_value
31324 with DW_AT_location and empty location expression, if it isn't found
31325 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
31326 to something that has been emitted in the current CU. */
31328 static void
31329 resolve_addr (dw_die_ref die)
31331 dw_die_ref c;
31332 dw_attr_node *a;
31333 dw_loc_list_ref *curr, *start, loc;
31334 unsigned ix;
31335 bool remove_AT_byte_size = false;
31337 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31338 switch (AT_class (a))
31340 case dw_val_class_loc_list:
31341 start = curr = AT_loc_list_ptr (a);
31342 loc = *curr;
31343 gcc_assert (loc);
31344 /* The same list can be referenced more than once. See if we have
31345 already recorded the result from a previous pass. */
31346 if (loc->replaced)
31347 *curr = loc->dw_loc_next;
31348 else if (!loc->resolved_addr)
31350 /* As things stand, we do not expect or allow one die to
31351 reference a suffix of another die's location list chain.
31352 References must be identical or completely separate.
31353 There is therefore no need to cache the result of this
31354 pass on any list other than the first; doing so
31355 would lead to unnecessary writes. */
31356 while (*curr)
31358 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
31359 if (!resolve_addr_in_expr (a, (*curr)->expr))
31361 dw_loc_list_ref next = (*curr)->dw_loc_next;
31362 dw_loc_descr_ref l = (*curr)->expr;
31364 if (next && (*curr)->ll_symbol)
31366 gcc_assert (!next->ll_symbol);
31367 next->ll_symbol = (*curr)->ll_symbol;
31368 next->vl_symbol = (*curr)->vl_symbol;
31370 if (dwarf_split_debug_info)
31371 remove_loc_list_addr_table_entries (l);
31372 *curr = next;
31374 else
31376 mark_base_types ((*curr)->expr);
31377 curr = &(*curr)->dw_loc_next;
31380 if (loc == *start)
31381 loc->resolved_addr = 1;
31382 else
31384 loc->replaced = 1;
31385 loc->dw_loc_next = *start;
31388 if (!*start)
31390 remove_AT (die, a->dw_attr);
31391 ix--;
31393 break;
31394 case dw_val_class_view_list:
31396 gcc_checking_assert (a->dw_attr == DW_AT_GNU_locviews);
31397 gcc_checking_assert (dwarf2out_locviews_in_attribute ());
31398 dw_val_node *llnode
31399 = view_list_to_loc_list_val_node (&a->dw_attr_val);
31400 /* If we no longer have a loclist, or it no longer needs
31401 views, drop this attribute. */
31402 if (!llnode || !llnode->v.val_loc_list->vl_symbol)
31404 remove_AT (die, a->dw_attr);
31405 ix--;
31407 break;
31409 case dw_val_class_loc:
31411 dw_loc_descr_ref l = AT_loc (a);
31412 /* DW_OP_GNU_variable_value DW_OP_stack_value or
31413 DW_OP_GNU_variable_value in DW_AT_string_length can be converted
31414 into DW_OP_call4 or DW_OP_call4 DW_OP_deref, which is standard
31415 DWARF4 unlike DW_OP_GNU_variable_value. Or for DWARF5
31416 DW_OP_GNU_variable_value DW_OP_stack_value can be replaced
31417 with DW_FORM_ref referencing the same DIE as
31418 DW_OP_GNU_variable_value used to reference. */
31419 if (a->dw_attr == DW_AT_string_length
31420 && l
31421 && l->dw_loc_opc == DW_OP_GNU_variable_value
31422 && (l->dw_loc_next == NULL
31423 || (l->dw_loc_next->dw_loc_next == NULL
31424 && l->dw_loc_next->dw_loc_opc == DW_OP_stack_value)))
31426 switch (optimize_string_length (a))
31428 case -1:
31429 remove_AT (die, a->dw_attr);
31430 ix--;
31431 /* If we drop DW_AT_string_length, we need to drop also
31432 DW_AT_{string_length_,}byte_size. */
31433 remove_AT_byte_size = true;
31434 continue;
31435 default:
31436 break;
31437 case 1:
31438 /* Even if we keep the optimized DW_AT_string_length,
31439 it might have changed AT_class, so process it again. */
31440 ix--;
31441 continue;
31444 /* For -gdwarf-2 don't attempt to optimize
31445 DW_AT_data_member_location containing
31446 DW_OP_plus_uconst - older consumers might
31447 rely on it being that op instead of a more complex,
31448 but shorter, location description. */
31449 if ((dwarf_version > 2
31450 || a->dw_attr != DW_AT_data_member_location
31451 || l == NULL
31452 || l->dw_loc_opc != DW_OP_plus_uconst
31453 || l->dw_loc_next != NULL)
31454 && !resolve_addr_in_expr (a, l))
31456 if (dwarf_split_debug_info)
31457 remove_loc_list_addr_table_entries (l);
31458 if (l != NULL
31459 && l->dw_loc_next == NULL
31460 && l->dw_loc_opc == DW_OP_addr
31461 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
31462 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
31463 && a->dw_attr == DW_AT_location)
31465 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
31466 remove_AT (die, a->dw_attr);
31467 ix--;
31468 optimize_location_into_implicit_ptr (die, decl);
31469 break;
31471 if (a->dw_attr == DW_AT_string_length)
31472 /* If we drop DW_AT_string_length, we need to drop also
31473 DW_AT_{string_length_,}byte_size. */
31474 remove_AT_byte_size = true;
31475 remove_AT (die, a->dw_attr);
31476 ix--;
31478 else
31479 mark_base_types (l);
31481 break;
31482 case dw_val_class_addr:
31483 if (a->dw_attr == DW_AT_const_value
31484 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
31486 if (AT_index (a) != NOT_INDEXED)
31487 remove_addr_table_entry (a->dw_attr_val.val_entry);
31488 remove_AT (die, a->dw_attr);
31489 ix--;
31491 if ((die->die_tag == DW_TAG_call_site
31492 && a->dw_attr == DW_AT_call_origin)
31493 || (die->die_tag == DW_TAG_GNU_call_site
31494 && a->dw_attr == DW_AT_abstract_origin))
31496 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
31497 dw_die_ref tdie = lookup_decl_die (tdecl);
31498 dw_die_ref cdie;
31499 if (tdie == NULL
31500 && DECL_EXTERNAL (tdecl)
31501 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
31502 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
31504 dw_die_ref pdie = cdie;
31505 /* Make sure we don't add these DIEs into type units.
31506 We could emit skeleton DIEs for context (namespaces,
31507 outer structs/classes) and a skeleton DIE for the
31508 innermost context with DW_AT_signature pointing to the
31509 type unit. See PR78835. */
31510 while (pdie && pdie->die_tag != DW_TAG_type_unit)
31511 pdie = pdie->die_parent;
31512 if (pdie == NULL)
31514 /* Creating a full DIE for tdecl is overly expensive and
31515 at this point even wrong when in the LTO phase
31516 as it can end up generating new type DIEs we didn't
31517 output and thus optimize_external_refs will crash. */
31518 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
31519 add_AT_flag (tdie, DW_AT_external, 1);
31520 add_AT_flag (tdie, DW_AT_declaration, 1);
31521 add_linkage_attr (tdie, tdecl);
31522 add_name_and_src_coords_attributes (tdie, tdecl, true);
31523 equate_decl_number_to_die (tdecl, tdie);
31526 if (tdie)
31528 a->dw_attr_val.val_class = dw_val_class_die_ref;
31529 a->dw_attr_val.v.val_die_ref.die = tdie;
31530 a->dw_attr_val.v.val_die_ref.external = 0;
31532 else
31534 if (AT_index (a) != NOT_INDEXED)
31535 remove_addr_table_entry (a->dw_attr_val.val_entry);
31536 remove_AT (die, a->dw_attr);
31537 ix--;
31540 break;
31541 default:
31542 break;
31545 if (remove_AT_byte_size)
31546 remove_AT (die, dwarf_version >= 5
31547 ? DW_AT_string_length_byte_size
31548 : DW_AT_byte_size);
31550 FOR_EACH_CHILD (die, c, resolve_addr (c));
31553 /* Helper routines for optimize_location_lists.
31554 This pass tries to share identical local lists in .debug_loc
31555 section. */
31557 /* Iteratively hash operands of LOC opcode into HSTATE. */
31559 static void
31560 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
31562 dw_val_ref val1 = &loc->dw_loc_oprnd1;
31563 dw_val_ref val2 = &loc->dw_loc_oprnd2;
31565 switch (loc->dw_loc_opc)
31567 case DW_OP_const4u:
31568 case DW_OP_const8u:
31569 if (loc->dtprel)
31570 goto hash_addr;
31571 /* FALLTHRU */
31572 case DW_OP_const1u:
31573 case DW_OP_const1s:
31574 case DW_OP_const2u:
31575 case DW_OP_const2s:
31576 case DW_OP_const4s:
31577 case DW_OP_const8s:
31578 case DW_OP_constu:
31579 case DW_OP_consts:
31580 case DW_OP_pick:
31581 case DW_OP_plus_uconst:
31582 case DW_OP_breg0:
31583 case DW_OP_breg1:
31584 case DW_OP_breg2:
31585 case DW_OP_breg3:
31586 case DW_OP_breg4:
31587 case DW_OP_breg5:
31588 case DW_OP_breg6:
31589 case DW_OP_breg7:
31590 case DW_OP_breg8:
31591 case DW_OP_breg9:
31592 case DW_OP_breg10:
31593 case DW_OP_breg11:
31594 case DW_OP_breg12:
31595 case DW_OP_breg13:
31596 case DW_OP_breg14:
31597 case DW_OP_breg15:
31598 case DW_OP_breg16:
31599 case DW_OP_breg17:
31600 case DW_OP_breg18:
31601 case DW_OP_breg19:
31602 case DW_OP_breg20:
31603 case DW_OP_breg21:
31604 case DW_OP_breg22:
31605 case DW_OP_breg23:
31606 case DW_OP_breg24:
31607 case DW_OP_breg25:
31608 case DW_OP_breg26:
31609 case DW_OP_breg27:
31610 case DW_OP_breg28:
31611 case DW_OP_breg29:
31612 case DW_OP_breg30:
31613 case DW_OP_breg31:
31614 case DW_OP_regx:
31615 case DW_OP_fbreg:
31616 case DW_OP_piece:
31617 case DW_OP_deref_size:
31618 case DW_OP_xderef_size:
31619 hstate.add_object (val1->v.val_int);
31620 break;
31621 case DW_OP_skip:
31622 case DW_OP_bra:
31624 int offset;
31626 gcc_assert (val1->val_class == dw_val_class_loc);
31627 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
31628 hstate.add_object (offset);
31630 break;
31631 case DW_OP_implicit_value:
31632 hstate.add_object (val1->v.val_unsigned);
31633 switch (val2->val_class)
31635 case dw_val_class_const:
31636 hstate.add_object (val2->v.val_int);
31637 break;
31638 case dw_val_class_vec:
31640 unsigned int elt_size = val2->v.val_vec.elt_size;
31641 unsigned int len = val2->v.val_vec.length;
31643 hstate.add_int (elt_size);
31644 hstate.add_int (len);
31645 hstate.add (val2->v.val_vec.array, len * elt_size);
31647 break;
31648 case dw_val_class_const_double:
31649 hstate.add_object (val2->v.val_double.low);
31650 hstate.add_object (val2->v.val_double.high);
31651 break;
31652 case dw_val_class_wide_int:
31653 hstate.add (val2->v.val_wide->get_val (),
31654 get_full_len (*val2->v.val_wide)
31655 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
31656 break;
31657 case dw_val_class_addr:
31658 inchash::add_rtx (val2->v.val_addr, hstate);
31659 break;
31660 default:
31661 gcc_unreachable ();
31663 break;
31664 case DW_OP_bregx:
31665 case DW_OP_bit_piece:
31666 hstate.add_object (val1->v.val_int);
31667 hstate.add_object (val2->v.val_int);
31668 break;
31669 case DW_OP_addr:
31670 hash_addr:
31671 if (loc->dtprel)
31673 unsigned char dtprel = 0xd1;
31674 hstate.add_object (dtprel);
31676 inchash::add_rtx (val1->v.val_addr, hstate);
31677 break;
31678 case DW_OP_GNU_addr_index:
31679 case DW_OP_addrx:
31680 case DW_OP_GNU_const_index:
31681 case DW_OP_constx:
31683 if (loc->dtprel)
31685 unsigned char dtprel = 0xd1;
31686 hstate.add_object (dtprel);
31688 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
31690 break;
31691 case DW_OP_implicit_pointer:
31692 case DW_OP_GNU_implicit_pointer:
31693 hstate.add_int (val2->v.val_int);
31694 break;
31695 case DW_OP_entry_value:
31696 case DW_OP_GNU_entry_value:
31697 hstate.add_object (val1->v.val_loc);
31698 break;
31699 case DW_OP_regval_type:
31700 case DW_OP_deref_type:
31701 case DW_OP_GNU_regval_type:
31702 case DW_OP_GNU_deref_type:
31704 unsigned int byte_size
31705 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
31706 unsigned int encoding
31707 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
31708 hstate.add_object (val1->v.val_int);
31709 hstate.add_object (byte_size);
31710 hstate.add_object (encoding);
31712 break;
31713 case DW_OP_convert:
31714 case DW_OP_reinterpret:
31715 case DW_OP_GNU_convert:
31716 case DW_OP_GNU_reinterpret:
31717 if (val1->val_class == dw_val_class_unsigned_const)
31719 hstate.add_object (val1->v.val_unsigned);
31720 break;
31722 /* FALLTHRU */
31723 case DW_OP_const_type:
31724 case DW_OP_GNU_const_type:
31726 unsigned int byte_size
31727 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
31728 unsigned int encoding
31729 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
31730 hstate.add_object (byte_size);
31731 hstate.add_object (encoding);
31732 if (loc->dw_loc_opc != DW_OP_const_type
31733 && loc->dw_loc_opc != DW_OP_GNU_const_type)
31734 break;
31735 hstate.add_object (val2->val_class);
31736 switch (val2->val_class)
31738 case dw_val_class_const:
31739 hstate.add_object (val2->v.val_int);
31740 break;
31741 case dw_val_class_vec:
31743 unsigned int elt_size = val2->v.val_vec.elt_size;
31744 unsigned int len = val2->v.val_vec.length;
31746 hstate.add_object (elt_size);
31747 hstate.add_object (len);
31748 hstate.add (val2->v.val_vec.array, len * elt_size);
31750 break;
31751 case dw_val_class_const_double:
31752 hstate.add_object (val2->v.val_double.low);
31753 hstate.add_object (val2->v.val_double.high);
31754 break;
31755 case dw_val_class_wide_int:
31756 hstate.add (val2->v.val_wide->get_val (),
31757 get_full_len (*val2->v.val_wide)
31758 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
31759 break;
31760 default:
31761 gcc_unreachable ();
31764 break;
31766 default:
31767 /* Other codes have no operands. */
31768 break;
31772 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
31774 static inline void
31775 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
31777 dw_loc_descr_ref l;
31778 bool sizes_computed = false;
31779 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
31780 size_of_locs (loc);
31782 for (l = loc; l != NULL; l = l->dw_loc_next)
31784 enum dwarf_location_atom opc = l->dw_loc_opc;
31785 hstate.add_object (opc);
31786 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
31788 size_of_locs (loc);
31789 sizes_computed = true;
31791 hash_loc_operands (l, hstate);
31795 /* Compute hash of the whole location list LIST_HEAD. */
31797 static inline void
31798 hash_loc_list (dw_loc_list_ref list_head)
31800 dw_loc_list_ref curr = list_head;
31801 inchash::hash hstate;
31803 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
31805 hstate.add (curr->begin, strlen (curr->begin) + 1);
31806 hstate.add (curr->end, strlen (curr->end) + 1);
31807 hstate.add_object (curr->vbegin);
31808 hstate.add_object (curr->vend);
31809 if (curr->section)
31810 hstate.add (curr->section, strlen (curr->section) + 1);
31811 hash_locs (curr->expr, hstate);
31813 list_head->hash = hstate.end ();
31816 /* Return true if X and Y opcodes have the same operands. */
31818 static inline bool
31819 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
31821 dw_val_ref valx1 = &x->dw_loc_oprnd1;
31822 dw_val_ref valx2 = &x->dw_loc_oprnd2;
31823 dw_val_ref valy1 = &y->dw_loc_oprnd1;
31824 dw_val_ref valy2 = &y->dw_loc_oprnd2;
31826 switch (x->dw_loc_opc)
31828 case DW_OP_const4u:
31829 case DW_OP_const8u:
31830 if (x->dtprel)
31831 goto hash_addr;
31832 /* FALLTHRU */
31833 case DW_OP_const1u:
31834 case DW_OP_const1s:
31835 case DW_OP_const2u:
31836 case DW_OP_const2s:
31837 case DW_OP_const4s:
31838 case DW_OP_const8s:
31839 case DW_OP_constu:
31840 case DW_OP_consts:
31841 case DW_OP_pick:
31842 case DW_OP_plus_uconst:
31843 case DW_OP_breg0:
31844 case DW_OP_breg1:
31845 case DW_OP_breg2:
31846 case DW_OP_breg3:
31847 case DW_OP_breg4:
31848 case DW_OP_breg5:
31849 case DW_OP_breg6:
31850 case DW_OP_breg7:
31851 case DW_OP_breg8:
31852 case DW_OP_breg9:
31853 case DW_OP_breg10:
31854 case DW_OP_breg11:
31855 case DW_OP_breg12:
31856 case DW_OP_breg13:
31857 case DW_OP_breg14:
31858 case DW_OP_breg15:
31859 case DW_OP_breg16:
31860 case DW_OP_breg17:
31861 case DW_OP_breg18:
31862 case DW_OP_breg19:
31863 case DW_OP_breg20:
31864 case DW_OP_breg21:
31865 case DW_OP_breg22:
31866 case DW_OP_breg23:
31867 case DW_OP_breg24:
31868 case DW_OP_breg25:
31869 case DW_OP_breg26:
31870 case DW_OP_breg27:
31871 case DW_OP_breg28:
31872 case DW_OP_breg29:
31873 case DW_OP_breg30:
31874 case DW_OP_breg31:
31875 case DW_OP_regx:
31876 case DW_OP_fbreg:
31877 case DW_OP_piece:
31878 case DW_OP_deref_size:
31879 case DW_OP_xderef_size:
31880 return valx1->v.val_int == valy1->v.val_int;
31881 case DW_OP_skip:
31882 case DW_OP_bra:
31883 /* If splitting debug info, the use of DW_OP_GNU_addr_index
31884 can cause irrelevant differences in dw_loc_addr. */
31885 gcc_assert (valx1->val_class == dw_val_class_loc
31886 && valy1->val_class == dw_val_class_loc
31887 && (dwarf_split_debug_info
31888 || x->dw_loc_addr == y->dw_loc_addr));
31889 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
31890 case DW_OP_implicit_value:
31891 if (valx1->v.val_unsigned != valy1->v.val_unsigned
31892 || valx2->val_class != valy2->val_class)
31893 return false;
31894 switch (valx2->val_class)
31896 case dw_val_class_const:
31897 return valx2->v.val_int == valy2->v.val_int;
31898 case dw_val_class_vec:
31899 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
31900 && valx2->v.val_vec.length == valy2->v.val_vec.length
31901 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
31902 valx2->v.val_vec.elt_size
31903 * valx2->v.val_vec.length) == 0;
31904 case dw_val_class_const_double:
31905 return valx2->v.val_double.low == valy2->v.val_double.low
31906 && valx2->v.val_double.high == valy2->v.val_double.high;
31907 case dw_val_class_wide_int:
31908 return *valx2->v.val_wide == *valy2->v.val_wide;
31909 case dw_val_class_addr:
31910 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
31911 default:
31912 gcc_unreachable ();
31914 case DW_OP_bregx:
31915 case DW_OP_bit_piece:
31916 return valx1->v.val_int == valy1->v.val_int
31917 && valx2->v.val_int == valy2->v.val_int;
31918 case DW_OP_addr:
31919 hash_addr:
31920 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
31921 case DW_OP_GNU_addr_index:
31922 case DW_OP_addrx:
31923 case DW_OP_GNU_const_index:
31924 case DW_OP_constx:
31926 rtx ax1 = valx1->val_entry->addr.rtl;
31927 rtx ay1 = valy1->val_entry->addr.rtl;
31928 return rtx_equal_p (ax1, ay1);
31930 case DW_OP_implicit_pointer:
31931 case DW_OP_GNU_implicit_pointer:
31932 return valx1->val_class == dw_val_class_die_ref
31933 && valx1->val_class == valy1->val_class
31934 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
31935 && valx2->v.val_int == valy2->v.val_int;
31936 case DW_OP_entry_value:
31937 case DW_OP_GNU_entry_value:
31938 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
31939 case DW_OP_const_type:
31940 case DW_OP_GNU_const_type:
31941 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
31942 || valx2->val_class != valy2->val_class)
31943 return false;
31944 switch (valx2->val_class)
31946 case dw_val_class_const:
31947 return valx2->v.val_int == valy2->v.val_int;
31948 case dw_val_class_vec:
31949 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
31950 && valx2->v.val_vec.length == valy2->v.val_vec.length
31951 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
31952 valx2->v.val_vec.elt_size
31953 * valx2->v.val_vec.length) == 0;
31954 case dw_val_class_const_double:
31955 return valx2->v.val_double.low == valy2->v.val_double.low
31956 && valx2->v.val_double.high == valy2->v.val_double.high;
31957 case dw_val_class_wide_int:
31958 return *valx2->v.val_wide == *valy2->v.val_wide;
31959 default:
31960 gcc_unreachable ();
31962 case DW_OP_regval_type:
31963 case DW_OP_deref_type:
31964 case DW_OP_GNU_regval_type:
31965 case DW_OP_GNU_deref_type:
31966 return valx1->v.val_int == valy1->v.val_int
31967 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
31968 case DW_OP_convert:
31969 case DW_OP_reinterpret:
31970 case DW_OP_GNU_convert:
31971 case DW_OP_GNU_reinterpret:
31972 if (valx1->val_class != valy1->val_class)
31973 return false;
31974 if (valx1->val_class == dw_val_class_unsigned_const)
31975 return valx1->v.val_unsigned == valy1->v.val_unsigned;
31976 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
31977 case DW_OP_GNU_parameter_ref:
31978 return valx1->val_class == dw_val_class_die_ref
31979 && valx1->val_class == valy1->val_class
31980 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
31981 default:
31982 /* Other codes have no operands. */
31983 return true;
31987 /* Return true if DWARF location expressions X and Y are the same. */
31989 static inline bool
31990 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
31992 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
31993 if (x->dw_loc_opc != y->dw_loc_opc
31994 || x->dtprel != y->dtprel
31995 || !compare_loc_operands (x, y))
31996 break;
31997 return x == NULL && y == NULL;
32000 /* Hashtable helpers. */
32002 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
32004 static inline hashval_t hash (const dw_loc_list_struct *);
32005 static inline bool equal (const dw_loc_list_struct *,
32006 const dw_loc_list_struct *);
32009 /* Return precomputed hash of location list X. */
32011 inline hashval_t
32012 loc_list_hasher::hash (const dw_loc_list_struct *x)
32014 return x->hash;
32017 /* Return true if location lists A and B are the same. */
32019 inline bool
32020 loc_list_hasher::equal (const dw_loc_list_struct *a,
32021 const dw_loc_list_struct *b)
32023 if (a == b)
32024 return true;
32025 if (a->hash != b->hash)
32026 return false;
32027 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
32028 if (strcmp (a->begin, b->begin) != 0
32029 || strcmp (a->end, b->end) != 0
32030 || (a->section == NULL) != (b->section == NULL)
32031 || (a->section && strcmp (a->section, b->section) != 0)
32032 || a->vbegin != b->vbegin || a->vend != b->vend
32033 || !compare_locs (a->expr, b->expr))
32034 break;
32035 return a == NULL && b == NULL;
32038 typedef hash_table<loc_list_hasher> loc_list_hash_type;
32041 /* Recursively optimize location lists referenced from DIE
32042 children and share them whenever possible. */
32044 static void
32045 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
32047 dw_die_ref c;
32048 dw_attr_node *a;
32049 unsigned ix;
32050 dw_loc_list_struct **slot;
32051 bool drop_locviews = false;
32052 bool has_locviews = false;
32054 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
32055 if (AT_class (a) == dw_val_class_loc_list)
32057 dw_loc_list_ref list = AT_loc_list (a);
32058 /* TODO: perform some optimizations here, before hashing
32059 it and storing into the hash table. */
32060 hash_loc_list (list);
32061 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
32062 if (*slot == NULL)
32064 *slot = list;
32065 if (loc_list_has_views (list))
32066 gcc_assert (list->vl_symbol);
32067 else if (list->vl_symbol)
32069 drop_locviews = true;
32070 list->vl_symbol = NULL;
32073 else
32075 if (list->vl_symbol && !(*slot)->vl_symbol)
32076 drop_locviews = true;
32077 a->dw_attr_val.v.val_loc_list = *slot;
32080 else if (AT_class (a) == dw_val_class_view_list)
32082 gcc_checking_assert (a->dw_attr == DW_AT_GNU_locviews);
32083 has_locviews = true;
32087 if (drop_locviews && has_locviews)
32088 remove_AT (die, DW_AT_GNU_locviews);
32090 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
32094 /* Recursively assign each location list a unique index into the debug_addr
32095 section. */
32097 static void
32098 index_location_lists (dw_die_ref die)
32100 dw_die_ref c;
32101 dw_attr_node *a;
32102 unsigned ix;
32104 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
32105 if (AT_class (a) == dw_val_class_loc_list)
32107 dw_loc_list_ref list = AT_loc_list (a);
32108 dw_loc_list_ref curr;
32109 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
32111 /* Don't index an entry that has already been indexed
32112 or won't be output. Make sure skip_loc_list_entry doesn't
32113 call size_of_locs, because that might cause circular dependency,
32114 index_location_lists requiring address table indexes to be
32115 computed, but adding new indexes through add_addr_table_entry
32116 and address table index computation requiring no new additions
32117 to the hash table. In the rare case of DWARF[234] >= 64KB
32118 location expression, we'll just waste unused address table entry
32119 for it. */
32120 if (curr->begin_entry != NULL || skip_loc_list_entry (curr))
32121 continue;
32123 curr->begin_entry
32124 = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
32125 if (dwarf_version >= 5 && !HAVE_AS_LEB128)
32126 curr->end_entry
32127 = add_addr_table_entry (xstrdup (curr->end), ate_kind_label);
32131 FOR_EACH_CHILD (die, c, index_location_lists (c));
32134 /* Optimize location lists referenced from DIE
32135 children and share them whenever possible. */
32137 static void
32138 optimize_location_lists (dw_die_ref die)
32140 loc_list_hash_type htab (500);
32141 optimize_location_lists_1 (die, &htab);
32144 /* Traverse the limbo die list, and add parent/child links. The only
32145 dies without parents that should be here are concrete instances of
32146 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
32147 For concrete instances, we can get the parent die from the abstract
32148 instance. */
32150 static void
32151 flush_limbo_die_list (void)
32153 limbo_die_node *node;
32155 /* get_context_die calls force_decl_die, which can put new DIEs on the
32156 limbo list in LTO mode when nested functions are put in a different
32157 partition than that of their parent function. */
32158 while ((node = limbo_die_list))
32160 dw_die_ref die = node->die;
32161 limbo_die_list = node->next;
32163 if (die->die_parent == NULL)
32165 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
32167 if (origin && origin->die_parent)
32168 add_child_die (origin->die_parent, die);
32169 else if (is_cu_die (die))
32171 else if (seen_error ())
32172 /* It's OK to be confused by errors in the input. */
32173 add_child_die (comp_unit_die (), die);
32174 else
32176 /* In certain situations, the lexical block containing a
32177 nested function can be optimized away, which results
32178 in the nested function die being orphaned. Likewise
32179 with the return type of that nested function. Force
32180 this to be a child of the containing function.
32182 It may happen that even the containing function got fully
32183 inlined and optimized out. In that case we are lost and
32184 assign the empty child. This should not be big issue as
32185 the function is likely unreachable too. */
32186 gcc_assert (node->created_for);
32188 if (DECL_P (node->created_for))
32189 origin = get_context_die (DECL_CONTEXT (node->created_for));
32190 else if (TYPE_P (node->created_for))
32191 origin = scope_die_for (node->created_for, comp_unit_die ());
32192 else
32193 origin = comp_unit_die ();
32195 add_child_die (origin, die);
32201 /* Reset DIEs so we can output them again. */
32203 static void
32204 reset_dies (dw_die_ref die)
32206 dw_die_ref c;
32208 /* Remove stuff we re-generate. */
32209 die->die_mark = 0;
32210 die->die_offset = 0;
32211 die->die_abbrev = 0;
32212 remove_AT (die, DW_AT_sibling);
32214 FOR_EACH_CHILD (die, c, reset_dies (c));
32217 /* reset_indirect_string removed the references coming from DW_AT_name
32218 and DW_AT_comp_dir attributes on compilation unit DIEs. Readd them as
32219 .debug_line_str strings again. */
32221 static void
32222 adjust_name_comp_dir (dw_die_ref die)
32224 for (int i = 0; i < 2; i++)
32226 dwarf_attribute attr_kind = i ? DW_AT_comp_dir : DW_AT_name;
32227 dw_attr_node *a = get_AT (die, attr_kind);
32228 if (a == NULL || a->dw_attr_val.val_class != dw_val_class_str)
32229 continue;
32231 if (!debug_line_str_hash)
32232 debug_line_str_hash
32233 = hash_table<indirect_string_hasher>::create_ggc (10);
32235 struct indirect_string_node *node
32236 = find_AT_string_in_table (a->dw_attr_val.v.val_str->str,
32237 debug_line_str_hash);
32238 set_indirect_string (node);
32239 node->form = DW_FORM_line_strp;
32240 a->dw_attr_val.v.val_str = node;
32244 /* Output stuff that dwarf requires at the end of every file,
32245 and generate the DWARF-2 debugging info. */
32247 static void
32248 dwarf2out_finish (const char *filename)
32250 comdat_type_node *ctnode;
32251 dw_die_ref main_comp_unit_die;
32252 unsigned char checksum[16];
32253 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
32255 /* Generate CTF/BTF debug info. */
32256 if ((ctf_debug_info_level > CTFINFO_LEVEL_NONE
32257 || btf_debuginfo_p ()) && lang_GNU_C ())
32258 ctf_debug_finish (filename);
32260 /* Skip emitting DWARF if not required. */
32261 if (!dwarf_debuginfo_p ())
32262 return;
32264 /* Flush out any latecomers to the limbo party. */
32265 flush_limbo_die_list ();
32267 if (inline_entry_data_table)
32268 gcc_assert (inline_entry_data_table->is_empty ());
32270 if (flag_checking)
32272 verify_die (comp_unit_die ());
32273 for (limbo_die_node *node = cu_die_list; node; node = node->next)
32274 verify_die (node->die);
32277 /* We shouldn't have any symbols with delayed asm names for
32278 DIEs generated after early finish. */
32279 gcc_assert (deferred_asm_name == NULL);
32281 gen_remaining_tmpl_value_param_die_attribute ();
32283 if (flag_generate_lto || flag_generate_offload)
32285 gcc_assert (flag_fat_lto_objects || flag_generate_offload);
32287 /* Prune stuff so that dwarf2out_finish runs successfully
32288 for the fat part of the object. */
32289 reset_dies (comp_unit_die ());
32290 for (limbo_die_node *node = cu_die_list; node; node = node->next)
32291 reset_dies (node->die);
32292 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32294 /* Remove the pointer to the line table. */
32295 remove_AT (ctnode->root_die, DW_AT_stmt_list);
32296 if (debug_info_level >= DINFO_LEVEL_TERSE)
32297 reset_dies (ctnode->root_die);
32300 /* Reset die CU symbol so we don't output it twice. */
32301 comp_unit_die ()->die_id.die_symbol = NULL;
32303 /* Remove DW_AT_macro and DW_AT_stmt_list from the early output. */
32304 remove_AT (comp_unit_die (), DW_AT_stmt_list);
32305 if (have_macinfo)
32306 remove_AT (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE);
32308 /* Remove indirect string decisions. */
32309 debug_str_hash->traverse<void *, reset_indirect_string> (NULL);
32310 if (debug_line_str_hash)
32312 debug_line_str_hash->traverse<void *, reset_indirect_string> (NULL);
32313 debug_line_str_hash = NULL;
32314 if (asm_outputs_debug_line_str ())
32316 adjust_name_comp_dir (comp_unit_die ());
32317 for (limbo_die_node *node = cu_die_list; node; node = node->next)
32318 adjust_name_comp_dir (node->die);
32323 #if ENABLE_ASSERT_CHECKING
32325 dw_die_ref die = comp_unit_die (), c;
32326 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
32328 #endif
32329 base_types.truncate (0);
32330 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32331 resolve_addr (ctnode->root_die);
32332 resolve_addr (comp_unit_die ());
32333 move_marked_base_types ();
32335 if (dump_file)
32337 fprintf (dump_file, "DWARF for %s\n", filename);
32338 print_die (comp_unit_die (), dump_file);
32341 /* Initialize sections and labels used for actual assembler output. */
32342 unsigned generation = init_sections_and_labels (false);
32344 /* Traverse the DIE's and add sibling attributes to those DIE's that
32345 have children. */
32346 add_sibling_attributes (comp_unit_die ());
32347 limbo_die_node *node;
32348 for (node = cu_die_list; node; node = node->next)
32349 add_sibling_attributes (node->die);
32350 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32351 add_sibling_attributes (ctnode->root_die);
32353 /* When splitting DWARF info, we put some attributes in the
32354 skeleton compile_unit DIE that remains in the .o, while
32355 most attributes go in the DWO compile_unit_die. */
32356 if (dwarf_split_debug_info)
32358 limbo_die_node *cu;
32359 main_comp_unit_die = gen_compile_unit_die (NULL);
32360 if (dwarf_version >= 5)
32361 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
32362 cu = limbo_die_list;
32363 gcc_assert (cu->die == main_comp_unit_die);
32364 limbo_die_list = limbo_die_list->next;
32365 cu->next = cu_die_list;
32366 cu_die_list = cu;
32368 else
32369 main_comp_unit_die = comp_unit_die ();
32371 /* Output a terminator label for the .text section. */
32372 switch_to_section (text_section);
32373 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
32374 if (cold_text_section)
32376 switch_to_section (cold_text_section);
32377 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
32380 /* We can only use the low/high_pc attributes if all of the code was
32381 in .text. */
32382 if ((!have_multiple_function_sections
32383 && vec_safe_length (switch_text_ranges) < 2)
32384 || (dwarf_version < 3 && dwarf_strict))
32386 const char *end_label = text_end_label;
32387 if (vec_safe_length (switch_text_ranges) == 1)
32388 end_label = (*switch_text_ranges)[0];
32389 /* Don't add if the CU has no associated code. */
32390 if (switch_text_ranges)
32391 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
32392 end_label, true);
32394 else
32396 unsigned fde_idx;
32397 dw_fde_ref fde;
32398 bool range_list_added = false;
32399 if (switch_text_ranges)
32401 const char *prev_loc = text_section_label;
32402 const char *loc;
32403 unsigned idx;
32405 FOR_EACH_VEC_ELT (*switch_text_ranges, idx, loc)
32406 if (prev_loc)
32408 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32409 loc, &range_list_added, true);
32410 prev_loc = NULL;
32412 else
32413 prev_loc = loc;
32415 if (prev_loc)
32416 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32417 text_end_label, &range_list_added, true);
32420 if (switch_cold_ranges)
32422 const char *prev_loc = cold_text_section_label;
32423 const char *loc;
32424 unsigned idx;
32426 FOR_EACH_VEC_ELT (*switch_cold_ranges, idx, loc)
32427 if (prev_loc)
32429 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32430 loc, &range_list_added, true);
32431 prev_loc = NULL;
32433 else
32434 prev_loc = loc;
32436 if (prev_loc)
32437 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32438 cold_end_label, &range_list_added, true);
32441 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
32443 if (fde->ignored_debug)
32444 continue;
32445 if (!fde->in_std_section)
32446 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
32447 fde->dw_fde_end, &range_list_added,
32448 true);
32449 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
32450 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
32451 fde->dw_fde_second_end, &range_list_added,
32452 true);
32455 if (range_list_added)
32457 /* We need to give .debug_loc and .debug_ranges an appropriate
32458 "base address". Use zero so that these addresses become
32459 absolute. Historically, we've emitted the unexpected
32460 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
32461 Emit both to give time for other tools to adapt. */
32462 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
32463 if (! dwarf_strict && dwarf_version < 4)
32464 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
32466 add_ranges (NULL);
32467 have_multiple_function_sections = true;
32471 /* AIX Assembler inserts the length, so adjust the reference to match the
32472 offset expected by debuggers. */
32473 strcpy (dl_section_ref, debug_line_section_label);
32474 if (XCOFF_DEBUGGING_INFO)
32475 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
32477 if (debug_info_level >= DINFO_LEVEL_TERSE)
32478 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
32479 dl_section_ref);
32481 if (have_macinfo)
32482 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
32483 macinfo_section_label);
32485 if (dwarf_split_debug_info)
32487 if (have_location_lists)
32489 /* Since we generate the loclists in the split DWARF .dwo
32490 file itself, we don't need to generate a loclists_base
32491 attribute for the split compile unit DIE. That attribute
32492 (and using relocatable sec_offset FORMs) isn't allowed
32493 for a split compile unit. Only if the .debug_loclists
32494 section was in the main file, would we need to generate a
32495 loclists_base attribute here (for the full or skeleton
32496 unit DIE). */
32498 /* optimize_location_lists calculates the size of the lists,
32499 so index them first, and assign indices to the entries.
32500 Although optimize_location_lists will remove entries from
32501 the table, it only does so for duplicates, and therefore
32502 only reduces ref_counts to 1. */
32503 index_location_lists (comp_unit_die ());
32506 if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
32507 index_rnglists ();
32509 if (addr_index_table != NULL)
32511 unsigned int index = 0;
32512 addr_index_table
32513 ->traverse_noresize<unsigned int *, index_addr_table_entry>
32514 (&index);
32518 loc_list_idx = 0;
32519 if (have_location_lists)
32521 optimize_location_lists (comp_unit_die ());
32522 /* And finally assign indexes to the entries for -gsplit-dwarf. */
32523 if (dwarf_version >= 5 && dwarf_split_debug_info)
32524 assign_location_list_indexes (comp_unit_die ());
32527 save_macinfo_strings ();
32529 if (dwarf_split_debug_info)
32531 unsigned int index = 0;
32533 /* Add attributes common to skeleton compile_units and
32534 type_units. Because these attributes include strings, it
32535 must be done before freezing the string table. Top-level
32536 skeleton die attrs are added when the skeleton type unit is
32537 created, so ensure it is created by this point. */
32538 add_top_level_skeleton_die_attrs (main_comp_unit_die);
32539 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
32542 /* Output all of the compilation units. We put the main one last so that
32543 the offsets are available to output_pubnames. */
32544 for (node = cu_die_list; node; node = node->next)
32545 output_comp_unit (node->die, 0, NULL);
32547 hash_table<comdat_type_hasher> comdat_type_table (100);
32548 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32550 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
32552 /* Don't output duplicate types. */
32553 if (*slot != HTAB_EMPTY_ENTRY)
32554 continue;
32556 /* Add a pointer to the line table for the main compilation unit
32557 so that the debugger can make sense of DW_AT_decl_file
32558 attributes. */
32559 if (debug_info_level >= DINFO_LEVEL_TERSE)
32560 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
32561 (!dwarf_split_debug_info
32562 ? dl_section_ref
32563 : debug_skeleton_line_section_label));
32565 output_comdat_type_unit (ctnode, false);
32566 *slot = ctnode;
32569 if (dwarf_split_debug_info)
32571 int mark;
32572 struct md5_ctx ctx;
32574 /* Compute a checksum of the comp_unit to use as the dwo_id. */
32575 md5_init_ctx (&ctx);
32576 mark = 0;
32577 die_checksum (comp_unit_die (), &ctx, &mark);
32578 unmark_all_dies (comp_unit_die ());
32579 md5_finish_ctx (&ctx, checksum);
32581 if (dwarf_version < 5)
32583 /* Use the first 8 bytes of the checksum as the dwo_id,
32584 and add it to both comp-unit DIEs. */
32585 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
32586 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
32589 /* Add the base offset of the ranges table to the skeleton
32590 comp-unit DIE. */
32591 if (!vec_safe_is_empty (ranges_table))
32593 if (dwarf_version < 5)
32594 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
32595 ranges_section_label);
32598 output_addr_table ();
32601 /* Output the main compilation unit if non-empty or if .debug_macinfo
32602 or .debug_macro will be emitted. */
32603 output_comp_unit (comp_unit_die (), have_macinfo,
32604 dwarf_split_debug_info ? checksum : NULL);
32606 if (dwarf_split_debug_info && info_section_emitted)
32607 output_skeleton_debug_sections (main_comp_unit_die, checksum);
32609 /* Output the abbreviation table. */
32610 if (vec_safe_length (abbrev_die_table) != 1)
32612 switch_to_section (debug_abbrev_section);
32613 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
32614 output_abbrev_section ();
32617 /* Output location list section if necessary. */
32618 if (have_location_lists)
32620 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
32621 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
32622 /* Output the location lists info. */
32623 switch_to_section (debug_loc_section);
32624 if (dwarf_version >= 5)
32626 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 2);
32627 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 3);
32628 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
32629 dw2_asm_output_data (4, 0xffffffff,
32630 "Initial length escape value indicating "
32631 "64-bit DWARF extension");
32632 dw2_asm_output_delta (dwarf_offset_size, l2, l1,
32633 "Length of Location Lists");
32634 ASM_OUTPUT_LABEL (asm_out_file, l1);
32635 output_dwarf_version ();
32636 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
32637 dw2_asm_output_data (1, 0, "Segment Size");
32638 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
32639 "Offset Entry Count");
32641 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
32642 if (dwarf_version >= 5 && dwarf_split_debug_info)
32644 unsigned int save_loc_list_idx = loc_list_idx;
32645 loc_list_idx = 0;
32646 output_loclists_offsets (comp_unit_die ());
32647 gcc_assert (save_loc_list_idx == loc_list_idx);
32649 output_location_lists (comp_unit_die ());
32650 if (dwarf_version >= 5)
32651 ASM_OUTPUT_LABEL (asm_out_file, l2);
32654 output_pubtables ();
32656 /* Output the address range information if a CU (.debug_info section)
32657 was emitted. We output an empty table even if we had no functions
32658 to put in it. This because the consumer has no way to tell the
32659 difference between an empty table that we omitted and failure to
32660 generate a table that would have contained data. */
32661 if (info_section_emitted)
32663 switch_to_section (debug_aranges_section);
32664 output_aranges ();
32667 /* Output ranges section if necessary. */
32668 if (!vec_safe_is_empty (ranges_table))
32670 if (dwarf_version >= 5)
32672 if (dwarf_split_debug_info)
32674 /* We don't know right now whether there are any
32675 ranges for .debug_rnglists and any for .debug_rnglists.dwo.
32676 Depending on into which of those two belongs the first
32677 ranges_table entry, emit that section first and that
32678 output_rnglists call will return true if the other kind of
32679 ranges needs to be emitted as well. */
32680 bool dwo = (*ranges_table)[0].idx != DW_RANGES_IDX_SKELETON;
32681 if (output_rnglists (generation, dwo))
32682 output_rnglists (generation, !dwo);
32684 else
32685 output_rnglists (generation, false);
32687 else
32688 output_ranges ();
32691 /* Have to end the macro section. */
32692 if (have_macinfo)
32694 switch_to_section (debug_macinfo_section);
32695 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
32696 output_macinfo (!dwarf_split_debug_info ? debug_line_section_label
32697 : debug_skeleton_line_section_label, false);
32698 dw2_asm_output_data (1, 0, "End compilation unit");
32701 /* Output the source line correspondence table. We must do this
32702 even if there is no line information. Otherwise, on an empty
32703 translation unit, we will generate a present, but empty,
32704 .debug_info section. IRIX 6.5 `nm' will then complain when
32705 examining the file. This is done late so that any filenames
32706 used by the debug_info section are marked as 'used'. */
32707 switch_to_section (debug_line_section);
32708 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
32709 if (! output_asm_line_debug_info ())
32710 output_line_info (false);
32712 if (dwarf_split_debug_info && info_section_emitted)
32714 switch_to_section (debug_skeleton_line_section);
32715 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
32716 output_line_info (true);
32719 /* If we emitted any indirect strings, output the string table too. */
32720 if (debug_str_hash || skeleton_debug_str_hash)
32721 output_indirect_strings ();
32722 if (debug_line_str_hash)
32724 switch_to_section (debug_line_str_section);
32725 const enum dwarf_form form = DW_FORM_line_strp;
32726 debug_line_str_hash->traverse<enum dwarf_form,
32727 output_indirect_string> (form);
32730 /* ??? Move lvugid out of dwarf2out_source_line and reset it too? */
32731 symview_upper_bound = 0;
32732 if (zero_view_p)
32733 bitmap_clear (zero_view_p);
32736 /* Returns a hash value for X (which really is a variable_value_struct). */
32738 inline hashval_t
32739 variable_value_hasher::hash (variable_value_struct *x)
32741 return (hashval_t) x->decl_id;
32744 /* Return true if decl_id of variable_value_struct X is the same as
32745 UID of decl Y. */
32747 inline bool
32748 variable_value_hasher::equal (variable_value_struct *x, tree y)
32750 return x->decl_id == DECL_UID (y);
32753 /* Helper function for resolve_variable_value, handle
32754 DW_OP_GNU_variable_value in one location expression.
32755 Return true if exprloc has been changed into loclist. */
32757 static bool
32758 resolve_variable_value_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
32760 dw_loc_descr_ref next;
32761 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = next)
32763 next = loc->dw_loc_next;
32764 if (loc->dw_loc_opc != DW_OP_GNU_variable_value
32765 || loc->dw_loc_oprnd1.val_class != dw_val_class_decl_ref)
32766 continue;
32768 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
32769 if (DECL_CONTEXT (decl) != current_function_decl)
32770 continue;
32772 dw_die_ref ref = lookup_decl_die (decl);
32773 if (ref)
32775 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
32776 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
32777 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
32778 continue;
32780 dw_loc_list_ref l = loc_list_from_tree (decl, 0, NULL);
32781 if (l == NULL)
32782 continue;
32783 if (l->dw_loc_next)
32785 if (AT_class (a) != dw_val_class_loc)
32786 continue;
32787 switch (a->dw_attr)
32789 /* Following attributes allow both exprloc and loclist
32790 classes, so we can change them into a loclist. */
32791 case DW_AT_location:
32792 case DW_AT_string_length:
32793 case DW_AT_return_addr:
32794 case DW_AT_data_member_location:
32795 case DW_AT_frame_base:
32796 case DW_AT_segment:
32797 case DW_AT_static_link:
32798 case DW_AT_use_location:
32799 case DW_AT_vtable_elem_location:
32800 if (prev)
32802 prev->dw_loc_next = NULL;
32803 prepend_loc_descr_to_each (l, AT_loc (a));
32805 if (next)
32806 add_loc_descr_to_each (l, next);
32807 a->dw_attr_val.val_class = dw_val_class_loc_list;
32808 a->dw_attr_val.val_entry = NULL;
32809 a->dw_attr_val.v.val_loc_list = l;
32810 have_location_lists = true;
32811 return true;
32812 /* Following attributes allow both exprloc and reference,
32813 so if the whole expression is DW_OP_GNU_variable_value alone
32814 we could transform it into reference. */
32815 case DW_AT_byte_size:
32816 case DW_AT_bit_size:
32817 case DW_AT_lower_bound:
32818 case DW_AT_upper_bound:
32819 case DW_AT_bit_stride:
32820 case DW_AT_count:
32821 case DW_AT_allocated:
32822 case DW_AT_associated:
32823 case DW_AT_byte_stride:
32824 if (prev == NULL && next == NULL)
32825 break;
32826 /* FALLTHRU */
32827 default:
32828 if (dwarf_strict)
32829 continue;
32830 break;
32832 /* Create DW_TAG_variable that we can refer to. */
32833 gen_decl_die (decl, NULL_TREE, NULL,
32834 lookup_decl_die (current_function_decl));
32835 ref = lookup_decl_die (decl);
32836 if (ref)
32838 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
32839 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
32840 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
32842 continue;
32844 if (prev)
32846 prev->dw_loc_next = l->expr;
32847 add_loc_descr (&prev->dw_loc_next, next);
32848 free_loc_descr (loc, NULL);
32849 next = prev->dw_loc_next;
32851 else
32853 memcpy (loc, l->expr, sizeof (dw_loc_descr_node));
32854 add_loc_descr (&loc, next);
32855 next = loc;
32857 loc = prev;
32859 return false;
32862 /* Attempt to resolve DW_OP_GNU_variable_value using loc_list_from_tree. */
32864 static void
32865 resolve_variable_value (dw_die_ref die)
32867 dw_attr_node *a;
32868 dw_loc_list_ref loc;
32869 unsigned ix;
32871 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
32872 switch (AT_class (a))
32874 case dw_val_class_loc:
32875 if (!resolve_variable_value_in_expr (a, AT_loc (a)))
32876 break;
32877 /* FALLTHRU */
32878 case dw_val_class_loc_list:
32879 loc = AT_loc_list (a);
32880 gcc_assert (loc);
32881 for (; loc; loc = loc->dw_loc_next)
32882 resolve_variable_value_in_expr (a, loc->expr);
32883 break;
32884 default:
32885 break;
32889 /* Attempt to optimize DW_OP_GNU_variable_value refering to
32890 temporaries in the current function. */
32892 static void
32893 resolve_variable_values (void)
32895 if (!variable_value_hash || !current_function_decl)
32896 return;
32898 struct variable_value_struct *node
32899 = variable_value_hash->find_with_hash (current_function_decl,
32900 DECL_UID (current_function_decl));
32902 if (node == NULL)
32903 return;
32905 unsigned int i;
32906 dw_die_ref die;
32907 FOR_EACH_VEC_SAFE_ELT (node->dies, i, die)
32908 resolve_variable_value (die);
32911 /* Helper function for note_variable_value, handle one location
32912 expression. */
32914 static void
32915 note_variable_value_in_expr (dw_die_ref die, dw_loc_descr_ref loc)
32917 for (; loc; loc = loc->dw_loc_next)
32918 if (loc->dw_loc_opc == DW_OP_GNU_variable_value
32919 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
32921 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
32922 dw_die_ref ref = lookup_decl_die (decl);
32923 if (! ref && (flag_generate_lto || flag_generate_offload))
32925 /* ??? This is somewhat a hack because we do not create DIEs
32926 for variables not in BLOCK trees early but when generating
32927 early LTO output we need the dw_val_class_decl_ref to be
32928 fully resolved. For fat LTO objects we'd also like to
32929 undo this after LTO dwarf output. */
32930 gcc_assert (DECL_CONTEXT (decl));
32931 dw_die_ref ctx = lookup_decl_die (DECL_CONTEXT (decl));
32932 gcc_assert (ctx != NULL);
32933 gen_decl_die (decl, NULL_TREE, NULL, ctx);
32934 ref = lookup_decl_die (decl);
32935 gcc_assert (ref != NULL);
32937 if (ref)
32939 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
32940 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
32941 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
32942 continue;
32944 if (VAR_P (decl)
32945 && DECL_CONTEXT (decl)
32946 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
32947 && lookup_decl_die (DECL_CONTEXT (decl)))
32949 if (!variable_value_hash)
32950 variable_value_hash
32951 = hash_table<variable_value_hasher>::create_ggc (10);
32953 tree fndecl = DECL_CONTEXT (decl);
32954 struct variable_value_struct *node;
32955 struct variable_value_struct **slot
32956 = variable_value_hash->find_slot_with_hash (fndecl,
32957 DECL_UID (fndecl),
32958 INSERT);
32959 if (*slot == NULL)
32961 node = ggc_cleared_alloc<variable_value_struct> ();
32962 node->decl_id = DECL_UID (fndecl);
32963 *slot = node;
32965 else
32966 node = *slot;
32968 vec_safe_push (node->dies, die);
32973 /* Walk the tree DIE and note DIEs with DW_OP_GNU_variable_value still
32974 with dw_val_class_decl_ref operand. */
32976 static void
32977 note_variable_value (dw_die_ref die)
32979 dw_die_ref c;
32980 dw_attr_node *a;
32981 dw_loc_list_ref loc;
32982 unsigned ix;
32984 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
32985 switch (AT_class (a))
32987 case dw_val_class_loc_list:
32988 loc = AT_loc_list (a);
32989 gcc_assert (loc);
32990 if (!loc->noted_variable_value)
32992 loc->noted_variable_value = 1;
32993 for (; loc; loc = loc->dw_loc_next)
32994 note_variable_value_in_expr (die, loc->expr);
32996 break;
32997 case dw_val_class_loc:
32998 note_variable_value_in_expr (die, AT_loc (a));
32999 break;
33000 default:
33001 break;
33004 /* Mark children. */
33005 FOR_EACH_CHILD (die, c, note_variable_value (c));
33008 /* Process DWARF dies for CTF generation. */
33010 static void
33011 ctf_debug_do_cu (dw_die_ref die)
33013 dw_die_ref c;
33015 if (!ctf_do_die (die))
33016 return;
33018 FOR_EACH_CHILD (die, c, ctf_do_die (c));
33021 /* Perform any cleanups needed after the early debug generation pass
33022 has run. */
33024 static void
33025 dwarf2out_early_finish (const char *filename)
33027 comdat_type_node *ctnode;
33028 set_early_dwarf s;
33029 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
33031 /* PCH might result in DW_AT_producer string being restored from the
33032 header compilation, so always fill it with empty string initially
33033 and overwrite only here. */
33034 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
33036 if (dwarf_record_gcc_switches)
33037 producer_string = gen_producer_string (lang_hooks.name,
33038 save_decoded_options,
33039 save_decoded_options_count);
33040 else
33041 producer_string = concat (lang_hooks.name, " ", version_string, NULL);
33043 producer->dw_attr_val.v.val_str->refcount--;
33044 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
33046 /* Add the name for the main input file now. We delayed this from
33047 dwarf2out_init to avoid complications with PCH. */
33048 add_filename_attribute (comp_unit_die (), remap_debug_filename (filename));
33049 add_comp_dir_attribute (comp_unit_die ());
33051 /* With LTO early dwarf was really finished at compile-time, so make
33052 sure to adjust the phase after annotating the LTRANS CU DIE. */
33053 if (in_lto_p)
33055 early_dwarf_finished = true;
33056 if (dump_file)
33058 fprintf (dump_file, "LTO EARLY DWARF for %s\n", filename);
33059 print_die (comp_unit_die (), dump_file);
33061 return;
33064 /* Walk through the list of incomplete types again, trying once more to
33065 emit full debugging info for them. */
33066 retry_incomplete_types ();
33068 gen_scheduled_generic_parms_dies ();
33069 gen_remaining_tmpl_value_param_die_attribute ();
33071 /* The point here is to flush out the limbo list so that it is empty
33072 and we don't need to stream it for LTO. */
33073 flush_limbo_die_list ();
33075 /* Add DW_AT_linkage_name for all deferred DIEs. */
33076 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
33078 tree decl = node->created_for;
33079 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
33080 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
33081 ended up in deferred_asm_name before we knew it was
33082 constant and never written to disk. */
33083 && DECL_ASSEMBLER_NAME (decl))
33085 add_linkage_attr (node->die, decl);
33086 move_linkage_attr (node->die);
33089 deferred_asm_name = NULL;
33091 if (flag_eliminate_unused_debug_types)
33092 prune_unused_types ();
33094 /* Generate separate COMDAT sections for type DIEs. */
33095 if (use_debug_types)
33097 break_out_comdat_types (comp_unit_die ());
33099 /* Each new type_unit DIE was added to the limbo die list when created.
33100 Since these have all been added to comdat_type_list, clear the
33101 limbo die list. */
33102 limbo_die_list = NULL;
33104 /* For each new comdat type unit, copy declarations for incomplete
33105 types to make the new unit self-contained (i.e., no direct
33106 references to the main compile unit). */
33107 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
33108 copy_decls_for_unworthy_types (ctnode->root_die);
33109 copy_decls_for_unworthy_types (comp_unit_die ());
33111 /* In the process of copying declarations from one unit to another,
33112 we may have left some declarations behind that are no longer
33113 referenced. Prune them. */
33114 prune_unused_types ();
33117 /* Traverse the DIE's and note DIEs with DW_OP_GNU_variable_value still
33118 with dw_val_class_decl_ref operand. */
33119 note_variable_value (comp_unit_die ());
33120 for (limbo_die_node *node = cu_die_list; node; node = node->next)
33121 note_variable_value (node->die);
33122 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
33123 note_variable_value (ctnode->root_die);
33124 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
33125 note_variable_value (node->die);
33127 /* The AT_pubnames attribute needs to go in all skeleton dies, including
33128 both the main_cu and all skeleton TUs. Making this call unconditional
33129 would end up either adding a second copy of the AT_pubnames attribute, or
33130 requiring a special case in add_top_level_skeleton_die_attrs. */
33131 if (!dwarf_split_debug_info)
33132 add_AT_pubnames (comp_unit_die ());
33134 /* The early debug phase is now finished. */
33135 early_dwarf_finished = true;
33136 if (dump_file)
33138 fprintf (dump_file, "EARLY DWARF for %s\n", filename);
33139 print_die (comp_unit_die (), dump_file);
33142 /* Generate CTF/BTF debug info. */
33143 if ((ctf_debug_info_level > CTFINFO_LEVEL_NONE
33144 || btf_debuginfo_p ()) && lang_GNU_C ())
33146 ctf_debug_init ();
33147 ctf_debug_do_cu (comp_unit_die ());
33148 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
33149 ctf_debug_do_cu (node->die);
33150 /* Post process the debug data in the CTF container if necessary. */
33151 ctf_debug_init_postprocess (btf_debuginfo_p ());
33153 ctf_debug_early_finish (filename);
33156 /* Do not generate DWARF assembler now when not producing LTO bytecode. */
33157 if ((!flag_generate_lto && !flag_generate_offload)
33158 /* FIXME: Disable debug info generation for (PE-)COFF targets since the
33159 copy_lto_debug_sections operation of the simple object support in
33160 libiberty is not implemented for them yet. */
33161 || TARGET_PECOFF || TARGET_COFF)
33162 return;
33164 /* Now as we are going to output for LTO initialize sections and labels
33165 to the LTO variants. We don't need a random-seed postfix as other
33166 LTO sections as linking the LTO debug sections into one in a partial
33167 link is fine. */
33168 init_sections_and_labels (true);
33170 /* The output below is modeled after dwarf2out_finish with all
33171 location related output removed and some LTO specific changes.
33172 Some refactoring might make both smaller and easier to match up. */
33174 base_types.truncate (0);
33175 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
33176 mark_base_types (ctnode->root_die);
33177 mark_base_types (comp_unit_die ());
33178 move_marked_base_types ();
33180 /* Traverse the DIE's and add sibling attributes to those DIE's
33181 that have children. */
33182 add_sibling_attributes (comp_unit_die ());
33183 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
33184 add_sibling_attributes (node->die);
33185 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
33186 add_sibling_attributes (ctnode->root_die);
33188 /* AIX Assembler inserts the length, so adjust the reference to match the
33189 offset expected by debuggers. */
33190 strcpy (dl_section_ref, debug_line_section_label);
33191 if (XCOFF_DEBUGGING_INFO)
33192 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
33194 if (debug_info_level >= DINFO_LEVEL_TERSE)
33195 add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list, dl_section_ref);
33197 if (have_macinfo)
33198 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
33199 macinfo_section_label);
33201 save_macinfo_strings ();
33203 if (dwarf_split_debug_info)
33205 unsigned int index = 0;
33206 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
33209 /* Output all of the compilation units. We put the main one last so that
33210 the offsets are available to output_pubnames. */
33211 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
33212 output_comp_unit (node->die, 0, NULL);
33214 hash_table<comdat_type_hasher> comdat_type_table (100);
33215 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
33217 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
33219 /* Don't output duplicate types. */
33220 if (*slot != HTAB_EMPTY_ENTRY)
33221 continue;
33223 /* Add a pointer to the line table for the main compilation unit
33224 so that the debugger can make sense of DW_AT_decl_file
33225 attributes. */
33226 if (debug_info_level >= DINFO_LEVEL_TERSE)
33227 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
33228 (!dwarf_split_debug_info
33229 ? debug_line_section_label
33230 : debug_skeleton_line_section_label));
33232 output_comdat_type_unit (ctnode, true);
33233 *slot = ctnode;
33236 /* Stick a unique symbol to the main debuginfo section. */
33237 compute_comp_unit_symbol (comp_unit_die ());
33239 /* Output the main compilation unit. We always need it if only for
33240 the CU symbol. */
33241 output_comp_unit (comp_unit_die (), true, NULL);
33243 /* Output the abbreviation table. */
33244 if (vec_safe_length (abbrev_die_table) != 1)
33246 switch_to_section (debug_abbrev_section);
33247 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
33248 output_abbrev_section ();
33251 /* Have to end the macro section. */
33252 if (have_macinfo)
33254 /* We have to save macinfo state if we need to output it again
33255 for the FAT part of the object. */
33256 vec<macinfo_entry, va_gc> *saved_macinfo_table = macinfo_table;
33257 if (flag_fat_lto_objects)
33258 macinfo_table = macinfo_table->copy ();
33260 switch_to_section (debug_macinfo_section);
33261 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
33262 output_macinfo (debug_line_section_label, true);
33263 dw2_asm_output_data (1, 0, "End compilation unit");
33265 if (flag_fat_lto_objects)
33267 vec_free (macinfo_table);
33268 macinfo_table = saved_macinfo_table;
33272 /* Emit a skeleton debug_line section. */
33273 switch_to_section (debug_line_section);
33274 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
33275 output_line_info (true);
33277 /* If we emitted any indirect strings, output the string table too. */
33278 if (debug_str_hash || skeleton_debug_str_hash)
33279 output_indirect_strings ();
33280 if (debug_line_str_hash)
33282 switch_to_section (debug_line_str_section);
33283 const enum dwarf_form form = DW_FORM_line_strp;
33284 debug_line_str_hash->traverse<enum dwarf_form,
33285 output_indirect_string> (form);
33288 /* Switch back to the text section. */
33289 switch_to_section (text_section);
33292 /* Reset all state within dwarf2out.cc so that we can rerun the compiler
33293 within the same process. For use by toplev::finalize. */
33295 void
33296 dwarf2out_cc_finalize (void)
33298 last_var_location_insn = NULL;
33299 cached_next_real_insn = NULL;
33300 used_rtx_array = NULL;
33301 incomplete_types = NULL;
33302 debug_info_section = NULL;
33303 debug_skeleton_info_section = NULL;
33304 debug_abbrev_section = NULL;
33305 debug_skeleton_abbrev_section = NULL;
33306 debug_aranges_section = NULL;
33307 debug_addr_section = NULL;
33308 debug_macinfo_section = NULL;
33309 debug_line_section = NULL;
33310 debug_skeleton_line_section = NULL;
33311 debug_loc_section = NULL;
33312 debug_pubnames_section = NULL;
33313 debug_pubtypes_section = NULL;
33314 debug_str_section = NULL;
33315 debug_line_str_section = NULL;
33316 debug_str_dwo_section = NULL;
33317 debug_str_offsets_section = NULL;
33318 debug_ranges_section = NULL;
33319 debug_ranges_dwo_section = NULL;
33320 debug_frame_section = NULL;
33321 fde_vec = NULL;
33322 debug_str_hash = NULL;
33323 debug_line_str_hash = NULL;
33324 skeleton_debug_str_hash = NULL;
33325 dw2_string_counter = 0;
33326 have_multiple_function_sections = false;
33327 in_text_section_p = false;
33328 cold_text_section = NULL;
33329 last_text_label = NULL;
33330 last_cold_label = NULL;
33331 switch_text_ranges = NULL;
33332 switch_cold_ranges = NULL;
33333 current_unit_personality = NULL;
33335 early_dwarf = false;
33336 early_dwarf_finished = false;
33338 next_die_offset = 0;
33339 single_comp_unit_die = NULL;
33340 comdat_type_list = NULL;
33341 limbo_die_list = NULL;
33342 file_table = NULL;
33343 decl_die_table = NULL;
33344 common_block_die_table = NULL;
33345 decl_loc_table = NULL;
33346 call_arg_locations = NULL;
33347 call_arg_loc_last = NULL;
33348 call_site_count = -1;
33349 tail_call_site_count = -1;
33350 cached_dw_loc_list_table = NULL;
33351 abbrev_die_table = NULL;
33352 delete dwarf_proc_stack_usage_map;
33353 dwarf_proc_stack_usage_map = NULL;
33354 line_info_label_num = 0;
33355 cur_line_info_table = NULL;
33356 text_section_line_info = NULL;
33357 cold_text_section_line_info = NULL;
33358 separate_line_info = NULL;
33359 info_section_emitted = false;
33360 pubname_table = NULL;
33361 pubtype_table = NULL;
33362 macinfo_table = NULL;
33363 ranges_table = NULL;
33364 ranges_by_label = NULL;
33365 rnglist_idx = 0;
33366 have_location_lists = false;
33367 loclabel_num = 0;
33368 poc_label_num = 0;
33369 last_emitted_file = NULL;
33370 label_num = 0;
33371 tmpl_value_parm_die_table = NULL;
33372 generic_type_instances = NULL;
33373 frame_pointer_fb_offset = 0;
33374 frame_pointer_fb_offset_valid = false;
33375 base_types.release ();
33376 XDELETEVEC (producer_string);
33377 producer_string = NULL;
33378 output_line_info_generation = 0;
33379 init_sections_and_labels_generation = 0;
33382 #include "gt-dwarf2out.h"