MATCH: Remove redundant pattern for `(x | y) & ~x`
[official-gcc.git] / gcc / dwarf2out.cc
blob69018bde238304b057ea812fcba30cffa8a4dd95
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2023 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 wide_int &op)
402 int prec = wi::get_precision (op);
403 return ((prec + HOST_BITS_PER_WIDE_INT - 1)
404 / HOST_BITS_PER_WIDE_INT);
407 static bool
408 should_emit_struct_debug (tree type, enum debug_info_usage usage)
410 if (debug_info_level <= DINFO_LEVEL_TERSE)
411 return false;
413 enum debug_struct_file criterion;
414 tree type_decl;
415 bool generic = lang_hooks.types.generic_p (type);
417 if (generic)
418 criterion = debug_struct_generic[usage];
419 else
420 criterion = debug_struct_ordinary[usage];
422 if (criterion == DINFO_STRUCT_FILE_NONE)
423 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
424 if (criterion == DINFO_STRUCT_FILE_ANY)
425 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
427 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
429 if (type_decl != NULL)
431 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
432 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
434 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
435 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
438 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
441 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
442 switch to the data section instead, and write out a synthetic start label
443 for collect2 the first time around. */
445 static void
446 switch_to_eh_frame_section (bool back ATTRIBUTE_UNUSED)
448 if (eh_frame_section == 0)
450 int flags;
452 if (EH_TABLES_CAN_BE_READ_ONLY)
454 int fde_encoding;
455 int per_encoding;
456 int lsda_encoding;
458 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
459 /*global=*/0);
460 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
461 /*global=*/1);
462 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
463 /*global=*/0);
464 flags = ((! flag_pic
465 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
466 && (fde_encoding & 0x70) != DW_EH_PE_aligned
467 && (per_encoding & 0x70) != DW_EH_PE_absptr
468 && (per_encoding & 0x70) != DW_EH_PE_aligned
469 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
470 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
471 ? 0 : SECTION_WRITE);
473 else
474 flags = SECTION_WRITE;
476 #ifdef EH_FRAME_SECTION_NAME
477 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
478 #else
479 eh_frame_section = ((flags == SECTION_WRITE)
480 ? data_section : readonly_data_section);
481 #endif /* EH_FRAME_SECTION_NAME */
484 switch_to_section (eh_frame_section);
486 #ifdef EH_FRAME_THROUGH_COLLECT2
487 /* We have no special eh_frame section. Emit special labels to guide
488 collect2. */
489 if (!back)
491 tree label = get_file_function_name ("F");
492 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
493 targetm.asm_out.globalize_label (asm_out_file,
494 IDENTIFIER_POINTER (label));
495 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
497 #endif
500 /* Switch [BACK] to the eh or debug frame table section, depending on
501 FOR_EH. */
503 static void
504 switch_to_frame_table_section (int for_eh, bool back)
506 if (for_eh)
507 switch_to_eh_frame_section (back);
508 else
510 if (!debug_frame_section)
511 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
512 SECTION_DEBUG, NULL);
513 switch_to_section (debug_frame_section);
517 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
519 enum dw_cfi_oprnd_type
520 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
522 switch (cfi)
524 case DW_CFA_nop:
525 case DW_CFA_GNU_window_save:
526 case DW_CFA_remember_state:
527 case DW_CFA_restore_state:
528 return dw_cfi_oprnd_unused;
530 case DW_CFA_set_loc:
531 case DW_CFA_advance_loc1:
532 case DW_CFA_advance_loc2:
533 case DW_CFA_advance_loc4:
534 case DW_CFA_MIPS_advance_loc8:
535 return dw_cfi_oprnd_addr;
537 case DW_CFA_offset:
538 case DW_CFA_offset_extended:
539 case DW_CFA_def_cfa:
540 case DW_CFA_offset_extended_sf:
541 case DW_CFA_def_cfa_sf:
542 case DW_CFA_restore:
543 case DW_CFA_restore_extended:
544 case DW_CFA_undefined:
545 case DW_CFA_same_value:
546 case DW_CFA_def_cfa_register:
547 case DW_CFA_register:
548 case DW_CFA_expression:
549 case DW_CFA_val_expression:
550 return dw_cfi_oprnd_reg_num;
552 case DW_CFA_def_cfa_offset:
553 case DW_CFA_GNU_args_size:
554 case DW_CFA_def_cfa_offset_sf:
555 return dw_cfi_oprnd_offset;
557 case DW_CFA_def_cfa_expression:
558 return dw_cfi_oprnd_loc;
560 default:
561 gcc_unreachable ();
565 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
567 enum dw_cfi_oprnd_type
568 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
570 switch (cfi)
572 case DW_CFA_def_cfa:
573 case DW_CFA_def_cfa_sf:
574 case DW_CFA_offset:
575 case DW_CFA_offset_extended_sf:
576 case DW_CFA_offset_extended:
577 return dw_cfi_oprnd_offset;
579 case DW_CFA_register:
580 return dw_cfi_oprnd_reg_num;
582 case DW_CFA_expression:
583 case DW_CFA_val_expression:
584 return dw_cfi_oprnd_loc;
586 case DW_CFA_def_cfa_expression:
587 return dw_cfi_oprnd_cfa_loc;
589 default:
590 return dw_cfi_oprnd_unused;
594 /* Output one FDE. */
596 static void
597 output_fde (dw_fde_ref fde, bool for_eh, bool second,
598 char *section_start_label, int fde_encoding, char *augmentation,
599 bool any_lsda_needed, int lsda_encoding)
601 const char *begin, *end;
602 static unsigned int j;
603 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
605 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
606 /* empty */ 0);
607 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
608 for_eh + j);
609 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
610 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
611 if (!XCOFF_DEBUGGING_INFO || for_eh)
613 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4 && !for_eh)
614 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
615 " indicating 64-bit DWARF extension");
616 dw2_asm_output_delta (for_eh ? 4 : dwarf_offset_size, l2, l1,
617 "FDE Length");
619 ASM_OUTPUT_LABEL (asm_out_file, l1);
621 if (for_eh)
622 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
623 else
624 dw2_asm_output_offset (dwarf_offset_size, section_start_label,
625 debug_frame_section, "FDE CIE offset");
627 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
628 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
630 if (for_eh)
632 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
633 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
634 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
635 "FDE initial location");
636 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
637 end, begin, "FDE address range");
639 else
641 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
642 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
645 if (augmentation[0])
647 if (any_lsda_needed)
649 int size = size_of_encoded_value (lsda_encoding);
651 if (lsda_encoding == DW_EH_PE_aligned)
653 int offset = ( 4 /* Length */
654 + 4 /* CIE offset */
655 + 2 * size_of_encoded_value (fde_encoding)
656 + 1 /* Augmentation size */ );
657 int pad = -offset & (PTR_SIZE - 1);
659 size += pad;
660 gcc_assert (size_of_uleb128 (size) == 1);
663 dw2_asm_output_data_uleb128 (size, "Augmentation size");
665 if (fde->uses_eh_lsda)
667 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
668 fde->funcdef_number);
669 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
670 gen_rtx_SYMBOL_REF (Pmode, l1),
671 false,
672 "Language Specific Data Area");
674 else
676 if (lsda_encoding == DW_EH_PE_aligned)
677 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
678 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
679 "Language Specific Data Area (none)");
682 else
683 dw2_asm_output_data_uleb128 (0, "Augmentation size");
686 /* Loop through the Call Frame Instructions associated with this FDE. */
687 fde->dw_fde_current_label = begin;
689 size_t from, until, i;
691 from = 0;
692 until = vec_safe_length (fde->dw_fde_cfi);
694 if (fde->dw_fde_second_begin == NULL)
696 else if (!second)
697 until = fde->dw_fde_switch_cfi_index;
698 else
699 from = fde->dw_fde_switch_cfi_index;
701 for (i = from; i < until; i++)
702 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
705 /* If we are to emit a ref/link from function bodies to their frame tables,
706 do it now. This is typically performed to make sure that tables
707 associated with functions are dragged with them and not discarded in
708 garbage collecting links. We need to do this on a per function basis to
709 cope with -ffunction-sections. */
711 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
712 /* Switch to the function section, emit the ref to the tables, and
713 switch *back* into the table section. */
714 switch_to_section (function_section (fde->decl));
715 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
716 switch_to_frame_table_section (for_eh, true);
717 #endif
719 /* Pad the FDE out to an address sized boundary. */
720 ASM_OUTPUT_ALIGN (asm_out_file,
721 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
722 ASM_OUTPUT_LABEL (asm_out_file, l2);
724 j += 2;
727 /* Return true if frame description entry FDE is needed for EH. */
729 static bool
730 fde_needed_for_eh_p (dw_fde_ref fde)
732 if (flag_asynchronous_unwind_tables)
733 return true;
735 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
736 return true;
738 if (fde->uses_eh_lsda)
739 return true;
741 /* If exceptions are enabled, we have collected nothrow info. */
742 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
743 return false;
745 return true;
748 /* Output the call frame information used to record information
749 that relates to calculating the frame pointer, and records the
750 location of saved registers. */
752 static void
753 output_call_frame_info (int for_eh)
755 unsigned int i;
756 dw_fde_ref fde;
757 dw_cfi_ref cfi;
758 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
759 char section_start_label[MAX_ARTIFICIAL_LABEL_BYTES];
760 bool any_lsda_needed = false;
761 char augmentation[6];
762 int augmentation_size;
763 int fde_encoding = DW_EH_PE_absptr;
764 int per_encoding = DW_EH_PE_absptr;
765 int lsda_encoding = DW_EH_PE_absptr;
766 int return_reg;
767 rtx personality = NULL;
768 int dw_cie_version;
770 /* Don't emit a CIE if there won't be any FDEs. */
771 if (!fde_vec)
772 return;
774 /* Nothing to do if the assembler's doing it all. */
775 if (dwarf2out_do_cfi_asm ())
776 return;
778 /* If we don't have any functions we'll want to unwind out of, don't emit
779 any EH unwind information. If we make FDEs linkonce, we may have to
780 emit an empty label for an FDE that wouldn't otherwise be emitted. We
781 want to avoid having an FDE kept around when the function it refers to
782 is discarded. Example where this matters: a primary function template
783 in C++ requires EH information, an explicit specialization doesn't. */
784 if (for_eh)
786 bool any_eh_needed = false;
788 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
790 if (fde->uses_eh_lsda)
791 any_eh_needed = any_lsda_needed = true;
792 else if (fde_needed_for_eh_p (fde))
793 any_eh_needed = true;
794 else if (TARGET_USES_WEAK_UNWIND_INFO)
795 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
798 if (!any_eh_needed)
799 return;
802 /* We're going to be generating comments, so turn on app. */
803 if (flag_debug_asm)
804 app_enable ();
806 /* Switch to the proper frame section, first time. */
807 switch_to_frame_table_section (for_eh, false);
809 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
810 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
812 /* Output the CIE. */
813 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
814 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
815 if (!XCOFF_DEBUGGING_INFO || for_eh)
817 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4 && !for_eh)
818 dw2_asm_output_data (4, 0xffffffff,
819 "Initial length escape value indicating 64-bit DWARF extension");
820 dw2_asm_output_delta (for_eh ? 4 : dwarf_offset_size, l2, l1,
821 "Length of Common Information Entry");
823 ASM_OUTPUT_LABEL (asm_out_file, l1);
825 /* Now that the CIE pointer is PC-relative for EH,
826 use 0 to identify the CIE. */
827 dw2_asm_output_data ((for_eh ? 4 : dwarf_offset_size),
828 (for_eh ? 0 : DWARF_CIE_ID),
829 "CIE Identifier Tag");
831 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
832 use CIE version 1, unless that would produce incorrect results
833 due to overflowing the return register column. */
834 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
835 dw_cie_version = 1;
836 if (return_reg >= 256 || dwarf_version > 2)
837 dw_cie_version = 3;
838 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
840 augmentation[0] = 0;
841 augmentation_size = 0;
843 personality = current_unit_personality;
844 if (for_eh)
846 char *p;
848 /* Augmentation:
849 z Indicates that a uleb128 is present to size the
850 augmentation section.
851 L Indicates the encoding (and thus presence) of
852 an LSDA pointer in the FDE augmentation.
853 R Indicates a non-default pointer encoding for
854 FDE code pointers.
855 P Indicates the presence of an encoding + language
856 personality routine in the CIE augmentation. */
858 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
859 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
860 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
862 p = augmentation + 1;
863 if (personality)
865 *p++ = 'P';
866 augmentation_size += 1 + size_of_encoded_value (per_encoding);
867 assemble_external_libcall (personality);
869 if (any_lsda_needed)
871 *p++ = 'L';
872 augmentation_size += 1;
874 if (fde_encoding != DW_EH_PE_absptr)
876 *p++ = 'R';
877 augmentation_size += 1;
879 if (p > augmentation + 1)
881 augmentation[0] = 'z';
882 *p = '\0';
885 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
886 if (personality && per_encoding == DW_EH_PE_aligned)
888 int offset = ( 4 /* Length */
889 + 4 /* CIE Id */
890 + 1 /* CIE version */
891 + strlen (augmentation) + 1 /* Augmentation */
892 + size_of_uleb128 (1) /* Code alignment */
893 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
894 + 1 /* RA column */
895 + 1 /* Augmentation size */
896 + 1 /* Personality encoding */ );
897 int pad = -offset & (PTR_SIZE - 1);
899 augmentation_size += pad;
901 /* Augmentations should be small, so there's scarce need to
902 iterate for a solution. Die if we exceed one uleb128 byte. */
903 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
907 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
908 if (dw_cie_version >= 4)
910 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
911 dw2_asm_output_data (1, 0, "CIE Segment Size");
913 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
914 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
915 "CIE Data Alignment Factor");
917 if (dw_cie_version == 1)
918 dw2_asm_output_data (1, return_reg, "CIE RA Column");
919 else
920 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
922 if (augmentation[0])
924 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
925 if (personality)
927 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
928 eh_data_format_name (per_encoding));
929 dw2_asm_output_encoded_addr_rtx (per_encoding,
930 personality,
931 true, NULL);
934 if (any_lsda_needed)
935 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
936 eh_data_format_name (lsda_encoding));
938 if (fde_encoding != DW_EH_PE_absptr)
939 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
940 eh_data_format_name (fde_encoding));
943 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
944 output_cfi (cfi, NULL, for_eh);
946 /* Pad the CIE out to an address sized boundary. */
947 ASM_OUTPUT_ALIGN (asm_out_file,
948 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
949 ASM_OUTPUT_LABEL (asm_out_file, l2);
951 /* Loop through all of the FDE's. */
952 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
954 unsigned int k;
956 /* Don't emit EH unwind info for leaf functions that don't need it. */
957 if (for_eh && !fde_needed_for_eh_p (fde))
958 continue;
960 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
961 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
962 augmentation, any_lsda_needed, lsda_encoding);
965 if (for_eh && targetm.terminate_dw2_eh_frame_info)
966 dw2_asm_output_data (4, 0, "End of Table");
968 /* Turn off app to make assembly quicker. */
969 if (flag_debug_asm)
970 app_disable ();
973 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
975 static void
976 dwarf2out_do_cfi_startproc (bool second)
978 int enc;
979 rtx ref;
981 fprintf (asm_out_file, "\t.cfi_startproc\n");
983 targetm.asm_out.post_cfi_startproc (asm_out_file, current_function_decl);
985 /* .cfi_personality and .cfi_lsda are only relevant to DWARF2
986 eh unwinders. */
987 if (targetm_common.except_unwind_info (&global_options) != UI_DWARF2)
988 return;
990 rtx personality = get_personality_function (current_function_decl);
992 if (personality)
994 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
995 ref = personality;
997 /* ??? The GAS support isn't entirely consistent. We have to
998 handle indirect support ourselves, but PC-relative is done
999 in the assembler. Further, the assembler can't handle any
1000 of the weirder relocation types. */
1001 if (enc & DW_EH_PE_indirect)
1003 if (targetm.asm_out.make_eh_symbol_indirect != NULL)
1004 ref = targetm.asm_out.make_eh_symbol_indirect (ref, true);
1005 else
1006 ref = dw2_force_const_mem (ref, true);
1009 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
1010 output_addr_const (asm_out_file, ref);
1011 fputc ('\n', asm_out_file);
1014 if (crtl->uses_eh_lsda)
1016 char lab[MAX_ARTIFICIAL_LABEL_BYTES];
1018 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
1019 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
1020 current_function_funcdef_no);
1021 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
1022 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
1024 if (enc & DW_EH_PE_indirect)
1026 if (targetm.asm_out.make_eh_symbol_indirect != NULL)
1027 ref = targetm.asm_out.make_eh_symbol_indirect (ref, true);
1028 else
1029 ref = dw2_force_const_mem (ref, true);
1032 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
1033 output_addr_const (asm_out_file, ref);
1034 fputc ('\n', asm_out_file);
1038 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
1039 this allocation may be done before pass_final. */
1041 dw_fde_ref
1042 dwarf2out_alloc_current_fde (void)
1044 dw_fde_ref fde;
1046 fde = ggc_cleared_alloc<dw_fde_node> ();
1047 fde->decl = current_function_decl;
1048 fde->funcdef_number = current_function_funcdef_no;
1049 fde->fde_index = vec_safe_length (fde_vec);
1050 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1051 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1052 fde->nothrow = crtl->nothrow;
1053 fde->drap_reg = INVALID_REGNUM;
1054 fde->vdrap_reg = INVALID_REGNUM;
1056 /* Record the FDE associated with this function. */
1057 cfun->fde = fde;
1058 vec_safe_push (fde_vec, fde);
1060 return fde;
1063 /* Output a marker (i.e. a label) for the beginning of a function, before
1064 the prologue. */
1066 void
1067 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1068 unsigned int column ATTRIBUTE_UNUSED,
1069 const char *file ATTRIBUTE_UNUSED)
1071 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1072 char * dup_label;
1073 dw_fde_ref fde;
1074 section *fnsec;
1075 bool do_frame;
1077 current_function_func_begin_label = NULL;
1079 do_frame = dwarf2out_do_frame ();
1081 /* ??? current_function_func_begin_label is also used by except.cc for
1082 call-site information. We must emit this label if it might be used. */
1083 if (!do_frame
1084 && (!flag_exceptions
1085 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1086 return;
1088 fnsec = function_section (current_function_decl);
1089 switch_to_section (fnsec);
1090 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1091 current_function_funcdef_no);
1092 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1093 current_function_funcdef_no);
1094 dup_label = xstrdup (label);
1095 current_function_func_begin_label = dup_label;
1097 /* We can elide FDE allocation if we're not emitting frame unwind info. */
1098 if (!do_frame)
1099 return;
1101 /* Unlike the debug version, the EH version of frame unwind info is a per-
1102 function setting so we need to record whether we need it for the unit. */
1103 do_eh_frame |= dwarf2out_do_eh_frame ();
1105 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1106 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1107 would include pass_dwarf2_frame. If we've not created the FDE yet,
1108 do so now. */
1109 fde = cfun->fde;
1110 if (fde == NULL)
1111 fde = dwarf2out_alloc_current_fde ();
1113 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1114 fde->dw_fde_begin = dup_label;
1115 fde->dw_fde_current_label = dup_label;
1116 fde->in_std_section = (fnsec == text_section
1117 || (cold_text_section && fnsec == cold_text_section));
1118 fde->ignored_debug = DECL_IGNORED_P (current_function_decl);
1119 in_text_section_p = fnsec == text_section;
1121 /* We only want to output line number information for the genuine dwarf2
1122 prologue case, not the eh frame case. */
1123 #ifdef DWARF2_DEBUGGING_INFO
1124 if (file)
1125 dwarf2out_source_line (line, column, file, 0, true);
1126 #endif
1128 if (dwarf2out_do_cfi_asm ())
1129 dwarf2out_do_cfi_startproc (false);
1130 else
1132 rtx personality = get_personality_function (current_function_decl);
1133 if (!current_unit_personality)
1134 current_unit_personality = personality;
1136 /* We cannot keep a current personality per function as without CFI
1137 asm, at the point where we emit the CFI data, there is no current
1138 function anymore. */
1139 if (personality && current_unit_personality != personality)
1140 sorry ("multiple EH personalities are supported only with assemblers "
1141 "supporting %<.cfi_personality%> directive");
1145 /* Output a marker (i.e. a label) for the end of the generated code
1146 for a function prologue. This gets called *after* the prologue code has
1147 been generated. */
1149 void
1150 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1151 const char *file ATTRIBUTE_UNUSED)
1153 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1155 /* Output a label to mark the endpoint of the code generated for this
1156 function. */
1157 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1158 current_function_funcdef_no);
1159 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1160 current_function_funcdef_no);
1161 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1164 /* Output a marker (i.e. a label) for the beginning of the generated code
1165 for a function epilogue. This gets called *before* the prologue code has
1166 been generated. */
1168 void
1169 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1170 const char *file ATTRIBUTE_UNUSED)
1172 dw_fde_ref fde = cfun->fde;
1173 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1175 if (fde->dw_fde_vms_begin_epilogue)
1176 return;
1178 /* Output a label to mark the endpoint of the code generated for this
1179 function. */
1180 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1181 current_function_funcdef_no);
1182 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1183 current_function_funcdef_no);
1184 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1187 /* Mark the ranges of non-debug subsections in the std text sections. */
1189 static void
1190 mark_ignored_debug_section (dw_fde_ref fde, bool second)
1192 bool std_section;
1193 const char *begin_label, *end_label;
1194 const char **last_end_label;
1195 vec<const char *, va_gc> **switch_ranges;
1197 if (second)
1199 std_section = fde->second_in_std_section;
1200 begin_label = fde->dw_fde_second_begin;
1201 end_label = fde->dw_fde_second_end;
1203 else
1205 std_section = fde->in_std_section;
1206 begin_label = fde->dw_fde_begin;
1207 end_label = fde->dw_fde_end;
1210 if (!std_section)
1211 return;
1213 if (in_text_section_p)
1215 last_end_label = &last_text_label;
1216 switch_ranges = &switch_text_ranges;
1218 else
1220 last_end_label = &last_cold_label;
1221 switch_ranges = &switch_cold_ranges;
1224 if (fde->ignored_debug)
1226 if (*switch_ranges && !(vec_safe_length (*switch_ranges) & 1))
1227 vec_safe_push (*switch_ranges, *last_end_label);
1229 else
1231 *last_end_label = end_label;
1233 if (!*switch_ranges)
1234 vec_alloc (*switch_ranges, 16);
1235 else if (vec_safe_length (*switch_ranges) & 1)
1236 vec_safe_push (*switch_ranges, begin_label);
1240 /* Output a marker (i.e. a label) for the absolute end of the generated code
1241 for a function definition. This gets called *after* the epilogue code has
1242 been generated. */
1244 void
1245 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1246 const char *file ATTRIBUTE_UNUSED)
1248 dw_fde_ref fde;
1249 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1251 last_var_location_insn = NULL;
1252 cached_next_real_insn = NULL;
1254 if (dwarf2out_do_cfi_asm ())
1255 fprintf (asm_out_file, "\t.cfi_endproc\n");
1257 /* Output a label to mark the endpoint of the code generated for this
1258 function. */
1259 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1260 current_function_funcdef_no);
1261 ASM_OUTPUT_LABEL (asm_out_file, label);
1262 fde = cfun->fde;
1263 gcc_assert (fde != NULL);
1264 if (fde->dw_fde_second_begin == NULL)
1265 fde->dw_fde_end = xstrdup (label);
1267 mark_ignored_debug_section (fde, fde->dw_fde_second_begin != NULL);
1270 void
1271 dwarf2out_frame_finish (void)
1273 /* Output call frame information. */
1274 if (targetm.debug_unwind_info () == UI_DWARF2)
1275 output_call_frame_info (0);
1277 /* Output another copy for the unwinder. */
1278 if (do_eh_frame)
1279 output_call_frame_info (1);
1282 static void var_location_switch_text_section (void);
1283 static void set_cur_line_info_table (section *);
1285 void
1286 dwarf2out_switch_text_section (void)
1288 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1289 section *sect;
1290 dw_fde_ref fde = cfun->fde;
1292 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1294 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_SECOND_SECT_LABEL,
1295 current_function_funcdef_no);
1297 fde->dw_fde_second_begin = ggc_strdup (label);
1298 if (!in_cold_section_p)
1300 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1301 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1303 else
1305 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1306 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1308 have_multiple_function_sections = true;
1310 if (dwarf2out_do_cfi_asm ())
1311 fprintf (asm_out_file, "\t.cfi_endproc\n");
1313 mark_ignored_debug_section (fde, false);
1315 /* Now do the real section switch. */
1316 sect = current_function_section ();
1317 switch_to_section (sect);
1319 fde->second_in_std_section
1320 = (sect == text_section
1321 || (cold_text_section && sect == cold_text_section));
1322 in_text_section_p = sect == text_section;
1324 if (dwarf2out_do_cfi_asm ())
1325 dwarf2out_do_cfi_startproc (true);
1327 var_location_switch_text_section ();
1329 if (cold_text_section != NULL)
1330 set_cur_line_info_table (sect);
1333 /* And now, the subset of the debugging information support code necessary
1334 for emitting location expressions. */
1336 /* Describe an entry into the .debug_addr section. */
1338 enum ate_kind {
1339 ate_kind_rtx,
1340 ate_kind_rtx_dtprel,
1341 ate_kind_label
1344 struct GTY((for_user)) addr_table_entry {
1345 enum ate_kind kind;
1346 unsigned int refcount;
1347 unsigned int index;
1348 union addr_table_entry_struct_union
1350 rtx GTY ((tag ("0"))) rtl;
1351 char * GTY ((tag ("1"))) label;
1353 GTY ((desc ("%1.kind"))) addr;
1356 typedef unsigned int var_loc_view;
1358 /* Location lists are ranges + location descriptions for that range,
1359 so you can track variables that are in different places over
1360 their entire life. */
1361 typedef struct GTY(()) dw_loc_list_struct {
1362 dw_loc_list_ref dw_loc_next;
1363 const char *begin; /* Label and addr_entry for start of range */
1364 addr_table_entry *begin_entry;
1365 const char *end; /* Label for end of range */
1366 addr_table_entry *end_entry;
1367 char *ll_symbol; /* Label for beginning of location list.
1368 Only on head of list. */
1369 char *vl_symbol; /* Label for beginning of view list. Ditto. */
1370 const char *section; /* Section this loclist is relative to */
1371 dw_loc_descr_ref expr;
1372 var_loc_view vbegin, vend;
1373 hashval_t hash;
1374 /* True if all addresses in this and subsequent lists are known to be
1375 resolved. */
1376 bool resolved_addr;
1377 /* True if this list has been replaced by dw_loc_next. */
1378 bool replaced;
1379 /* True if it has been emitted into .debug_loc* / .debug_loclists*
1380 section. */
1381 unsigned char emitted : 1;
1382 /* True if hash field is index rather than hash value. */
1383 unsigned char num_assigned : 1;
1384 /* True if .debug_loclists.dwo offset has been emitted for it already. */
1385 unsigned char offset_emitted : 1;
1386 /* True if note_variable_value_in_expr has been called on it. */
1387 unsigned char noted_variable_value : 1;
1388 /* True if the range should be emitted even if begin and end
1389 are the same. */
1390 bool force;
1391 } dw_loc_list_node;
1393 static dw_loc_descr_ref int_loc_descriptor (poly_int64);
1394 static dw_loc_descr_ref uint_loc_descriptor (unsigned HOST_WIDE_INT);
1396 /* Convert a DWARF stack opcode into its string name. */
1398 static const char *
1399 dwarf_stack_op_name (unsigned int op)
1401 const char *name = get_DW_OP_name (op);
1403 if (name != NULL)
1404 return name;
1406 return "OP_<unknown>";
1409 /* Return TRUE iff we're to output location view lists as a separate
1410 attribute next to the location lists, as an extension compatible
1411 with DWARF 2 and above. */
1413 static inline bool
1414 dwarf2out_locviews_in_attribute ()
1416 return debug_variable_location_views == 1;
1419 /* Return TRUE iff we're to output location view lists as part of the
1420 location lists, as proposed for standardization after DWARF 5. */
1422 static inline bool
1423 dwarf2out_locviews_in_loclist ()
1425 #ifndef DW_LLE_view_pair
1426 return false;
1427 #else
1428 return debug_variable_location_views == -1;
1429 #endif
1432 /* Return a pointer to a newly allocated location description. Location
1433 descriptions are simple expression terms that can be strung
1434 together to form more complicated location (address) descriptions. */
1436 static inline dw_loc_descr_ref
1437 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1438 unsigned HOST_WIDE_INT oprnd2)
1440 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1442 descr->dw_loc_opc = op;
1443 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1444 descr->dw_loc_oprnd1.val_entry = NULL;
1445 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1446 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1447 descr->dw_loc_oprnd2.val_entry = NULL;
1448 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1450 return descr;
1453 /* Add a location description term to a location description expression. */
1455 static inline void
1456 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1458 dw_loc_descr_ref *d;
1460 /* Find the end of the chain. */
1461 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1464 *d = descr;
1467 /* Compare two location operands for exact equality. */
1469 static bool
1470 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1472 if (a->val_class != b->val_class)
1473 return false;
1474 switch (a->val_class)
1476 case dw_val_class_none:
1477 return true;
1478 case dw_val_class_addr:
1479 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1481 case dw_val_class_offset:
1482 case dw_val_class_unsigned_const:
1483 case dw_val_class_const:
1484 case dw_val_class_unsigned_const_implicit:
1485 case dw_val_class_const_implicit:
1486 case dw_val_class_range_list:
1487 /* These are all HOST_WIDE_INT, signed or unsigned. */
1488 return a->v.val_unsigned == b->v.val_unsigned;
1490 case dw_val_class_loc:
1491 return a->v.val_loc == b->v.val_loc;
1492 case dw_val_class_loc_list:
1493 return a->v.val_loc_list == b->v.val_loc_list;
1494 case dw_val_class_view_list:
1495 return a->v.val_view_list == b->v.val_view_list;
1496 case dw_val_class_die_ref:
1497 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1498 case dw_val_class_fde_ref:
1499 return a->v.val_fde_index == b->v.val_fde_index;
1500 case dw_val_class_symview:
1501 return strcmp (a->v.val_symbolic_view, b->v.val_symbolic_view) == 0;
1502 case dw_val_class_lbl_id:
1503 case dw_val_class_lineptr:
1504 case dw_val_class_macptr:
1505 case dw_val_class_loclistsptr:
1506 case dw_val_class_high_pc:
1507 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1508 case dw_val_class_str:
1509 return a->v.val_str == b->v.val_str;
1510 case dw_val_class_flag:
1511 return a->v.val_flag == b->v.val_flag;
1512 case dw_val_class_file:
1513 case dw_val_class_file_implicit:
1514 return a->v.val_file == b->v.val_file;
1515 case dw_val_class_decl_ref:
1516 return a->v.val_decl_ref == b->v.val_decl_ref;
1518 case dw_val_class_const_double:
1519 return (a->v.val_double.high == b->v.val_double.high
1520 && a->v.val_double.low == b->v.val_double.low);
1522 case dw_val_class_wide_int:
1523 return *a->v.val_wide == *b->v.val_wide;
1525 case dw_val_class_vec:
1527 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1528 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1530 return (a_len == b_len
1531 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1534 case dw_val_class_data8:
1535 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1537 case dw_val_class_vms_delta:
1538 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1539 && !strcmp (a->v.val_vms_delta.lbl2, b->v.val_vms_delta.lbl2));
1541 case dw_val_class_discr_value:
1542 return (a->v.val_discr_value.pos == b->v.val_discr_value.pos
1543 && a->v.val_discr_value.v.uval == b->v.val_discr_value.v.uval);
1544 case dw_val_class_discr_list:
1545 /* It makes no sense comparing two discriminant value lists. */
1546 return false;
1548 gcc_unreachable ();
1551 /* Compare two location atoms for exact equality. */
1553 static bool
1554 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1556 if (a->dw_loc_opc != b->dw_loc_opc)
1557 return false;
1559 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1560 address size, but since we always allocate cleared storage it
1561 should be zero for other types of locations. */
1562 if (a->dtprel != b->dtprel)
1563 return false;
1565 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1566 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1569 /* Compare two complete location expressions for exact equality. */
1571 bool
1572 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1574 while (1)
1576 if (a == b)
1577 return true;
1578 if (a == NULL || b == NULL)
1579 return false;
1580 if (!loc_descr_equal_p_1 (a, b))
1581 return false;
1583 a = a->dw_loc_next;
1584 b = b->dw_loc_next;
1589 /* Add a constant POLY_OFFSET to a location expression. */
1591 static void
1592 loc_descr_plus_const (dw_loc_descr_ref *list_head, poly_int64 poly_offset)
1594 dw_loc_descr_ref loc;
1595 HOST_WIDE_INT *p;
1597 gcc_assert (*list_head != NULL);
1599 if (known_eq (poly_offset, 0))
1600 return;
1602 /* Find the end of the chain. */
1603 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1606 HOST_WIDE_INT offset;
1607 if (!poly_offset.is_constant (&offset))
1609 loc->dw_loc_next = int_loc_descriptor (poly_offset);
1610 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_plus, 0, 0));
1611 return;
1614 p = NULL;
1615 if (loc->dw_loc_opc == DW_OP_fbreg
1616 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1617 p = &loc->dw_loc_oprnd1.v.val_int;
1618 else if (loc->dw_loc_opc == DW_OP_bregx)
1619 p = &loc->dw_loc_oprnd2.v.val_int;
1621 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1622 offset. Don't optimize if an signed integer overflow would happen. */
1623 if (p != NULL
1624 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1625 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1626 *p += offset;
1628 else if (offset > 0)
1629 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1631 else
1633 loc->dw_loc_next
1634 = uint_loc_descriptor (-(unsigned HOST_WIDE_INT) offset);
1635 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1639 /* Return a pointer to a newly allocated location description for
1640 REG and OFFSET. */
1642 static inline dw_loc_descr_ref
1643 new_reg_loc_descr (unsigned int reg, poly_int64 offset)
1645 HOST_WIDE_INT const_offset;
1646 if (offset.is_constant (&const_offset))
1648 if (reg <= 31)
1649 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1650 const_offset, 0);
1651 else
1652 return new_loc_descr (DW_OP_bregx, reg, const_offset);
1654 else
1656 dw_loc_descr_ref ret = new_reg_loc_descr (reg, 0);
1657 loc_descr_plus_const (&ret, offset);
1658 return ret;
1662 /* Add a constant OFFSET to a location list. */
1664 static void
1665 loc_list_plus_const (dw_loc_list_ref list_head, poly_int64 offset)
1667 dw_loc_list_ref d;
1668 for (d = list_head; d != NULL; d = d->dw_loc_next)
1669 loc_descr_plus_const (&d->expr, offset);
1672 #define DWARF_REF_SIZE \
1673 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : dwarf_offset_size)
1675 /* The number of bits that can be encoded by largest DW_FORM_dataN.
1676 In DWARF4 and earlier it is DW_FORM_data8 with 64 bits, in DWARF5
1677 DW_FORM_data16 with 128 bits. */
1678 #define DWARF_LARGEST_DATA_FORM_BITS \
1679 (dwarf_version >= 5 ? 128 : 64)
1681 /* Utility inline function for construction of ops that were GNU extension
1682 before DWARF 5. */
1683 static inline enum dwarf_location_atom
1684 dwarf_OP (enum dwarf_location_atom op)
1686 switch (op)
1688 case DW_OP_implicit_pointer:
1689 if (dwarf_version < 5)
1690 return DW_OP_GNU_implicit_pointer;
1691 break;
1693 case DW_OP_entry_value:
1694 if (dwarf_version < 5)
1695 return DW_OP_GNU_entry_value;
1696 break;
1698 case DW_OP_const_type:
1699 if (dwarf_version < 5)
1700 return DW_OP_GNU_const_type;
1701 break;
1703 case DW_OP_regval_type:
1704 if (dwarf_version < 5)
1705 return DW_OP_GNU_regval_type;
1706 break;
1708 case DW_OP_deref_type:
1709 if (dwarf_version < 5)
1710 return DW_OP_GNU_deref_type;
1711 break;
1713 case DW_OP_convert:
1714 if (dwarf_version < 5)
1715 return DW_OP_GNU_convert;
1716 break;
1718 case DW_OP_reinterpret:
1719 if (dwarf_version < 5)
1720 return DW_OP_GNU_reinterpret;
1721 break;
1723 case DW_OP_addrx:
1724 if (dwarf_version < 5)
1725 return DW_OP_GNU_addr_index;
1726 break;
1728 case DW_OP_constx:
1729 if (dwarf_version < 5)
1730 return DW_OP_GNU_const_index;
1731 break;
1733 default:
1734 break;
1736 return op;
1739 /* Similarly for attributes. */
1740 static inline enum dwarf_attribute
1741 dwarf_AT (enum dwarf_attribute at)
1743 switch (at)
1745 case DW_AT_call_return_pc:
1746 if (dwarf_version < 5)
1747 return DW_AT_low_pc;
1748 break;
1750 case DW_AT_call_tail_call:
1751 if (dwarf_version < 5)
1752 return DW_AT_GNU_tail_call;
1753 break;
1755 case DW_AT_call_origin:
1756 if (dwarf_version < 5)
1757 return DW_AT_abstract_origin;
1758 break;
1760 case DW_AT_call_target:
1761 if (dwarf_version < 5)
1762 return DW_AT_GNU_call_site_target;
1763 break;
1765 case DW_AT_call_target_clobbered:
1766 if (dwarf_version < 5)
1767 return DW_AT_GNU_call_site_target_clobbered;
1768 break;
1770 case DW_AT_call_parameter:
1771 if (dwarf_version < 5)
1772 return DW_AT_abstract_origin;
1773 break;
1775 case DW_AT_call_value:
1776 if (dwarf_version < 5)
1777 return DW_AT_GNU_call_site_value;
1778 break;
1780 case DW_AT_call_data_value:
1781 if (dwarf_version < 5)
1782 return DW_AT_GNU_call_site_data_value;
1783 break;
1785 case DW_AT_call_all_calls:
1786 if (dwarf_version < 5)
1787 return DW_AT_GNU_all_call_sites;
1788 break;
1790 case DW_AT_call_all_tail_calls:
1791 if (dwarf_version < 5)
1792 return DW_AT_GNU_all_tail_call_sites;
1793 break;
1795 case DW_AT_dwo_name:
1796 if (dwarf_version < 5)
1797 return DW_AT_GNU_dwo_name;
1798 break;
1800 case DW_AT_addr_base:
1801 if (dwarf_version < 5)
1802 return DW_AT_GNU_addr_base;
1803 break;
1805 default:
1806 break;
1808 return at;
1811 /* And similarly for tags. */
1812 static inline enum dwarf_tag
1813 dwarf_TAG (enum dwarf_tag tag)
1815 switch (tag)
1817 case DW_TAG_call_site:
1818 if (dwarf_version < 5)
1819 return DW_TAG_GNU_call_site;
1820 break;
1822 case DW_TAG_call_site_parameter:
1823 if (dwarf_version < 5)
1824 return DW_TAG_GNU_call_site_parameter;
1825 break;
1827 default:
1828 break;
1830 return tag;
1833 /* And similarly for forms. */
1834 static inline enum dwarf_form
1835 dwarf_FORM (enum dwarf_form form)
1837 switch (form)
1839 case DW_FORM_addrx:
1840 if (dwarf_version < 5)
1841 return DW_FORM_GNU_addr_index;
1842 break;
1844 case DW_FORM_strx:
1845 if (dwarf_version < 5)
1846 return DW_FORM_GNU_str_index;
1847 break;
1849 default:
1850 break;
1852 return form;
1855 static unsigned long int get_base_type_offset (dw_die_ref);
1857 /* Return the size of a location descriptor. */
1859 static unsigned long
1860 size_of_loc_descr (dw_loc_descr_ref loc)
1862 unsigned long size = 1;
1864 switch (loc->dw_loc_opc)
1866 case DW_OP_addr:
1867 size += DWARF2_ADDR_SIZE;
1868 break;
1869 case DW_OP_GNU_addr_index:
1870 case DW_OP_addrx:
1871 case DW_OP_GNU_const_index:
1872 case DW_OP_constx:
1873 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1874 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1875 break;
1876 case DW_OP_const1u:
1877 case DW_OP_const1s:
1878 size += 1;
1879 break;
1880 case DW_OP_const2u:
1881 case DW_OP_const2s:
1882 size += 2;
1883 break;
1884 case DW_OP_const4u:
1885 case DW_OP_const4s:
1886 size += 4;
1887 break;
1888 case DW_OP_const8u:
1889 case DW_OP_const8s:
1890 size += 8;
1891 break;
1892 case DW_OP_constu:
1893 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1894 break;
1895 case DW_OP_consts:
1896 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1897 break;
1898 case DW_OP_pick:
1899 size += 1;
1900 break;
1901 case DW_OP_plus_uconst:
1902 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1903 break;
1904 case DW_OP_skip:
1905 case DW_OP_bra:
1906 size += 2;
1907 break;
1908 case DW_OP_breg0:
1909 case DW_OP_breg1:
1910 case DW_OP_breg2:
1911 case DW_OP_breg3:
1912 case DW_OP_breg4:
1913 case DW_OP_breg5:
1914 case DW_OP_breg6:
1915 case DW_OP_breg7:
1916 case DW_OP_breg8:
1917 case DW_OP_breg9:
1918 case DW_OP_breg10:
1919 case DW_OP_breg11:
1920 case DW_OP_breg12:
1921 case DW_OP_breg13:
1922 case DW_OP_breg14:
1923 case DW_OP_breg15:
1924 case DW_OP_breg16:
1925 case DW_OP_breg17:
1926 case DW_OP_breg18:
1927 case DW_OP_breg19:
1928 case DW_OP_breg20:
1929 case DW_OP_breg21:
1930 case DW_OP_breg22:
1931 case DW_OP_breg23:
1932 case DW_OP_breg24:
1933 case DW_OP_breg25:
1934 case DW_OP_breg26:
1935 case DW_OP_breg27:
1936 case DW_OP_breg28:
1937 case DW_OP_breg29:
1938 case DW_OP_breg30:
1939 case DW_OP_breg31:
1940 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1941 break;
1942 case DW_OP_regx:
1943 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1944 break;
1945 case DW_OP_fbreg:
1946 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1947 break;
1948 case DW_OP_bregx:
1949 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1950 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1951 break;
1952 case DW_OP_piece:
1953 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1954 break;
1955 case DW_OP_bit_piece:
1956 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1957 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1958 break;
1959 case DW_OP_deref_size:
1960 case DW_OP_xderef_size:
1961 size += 1;
1962 break;
1963 case DW_OP_call2:
1964 size += 2;
1965 break;
1966 case DW_OP_call4:
1967 size += 4;
1968 break;
1969 case DW_OP_call_ref:
1970 case DW_OP_GNU_variable_value:
1971 size += DWARF_REF_SIZE;
1972 break;
1973 case DW_OP_implicit_value:
1974 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1975 + loc->dw_loc_oprnd1.v.val_unsigned;
1976 break;
1977 case DW_OP_implicit_pointer:
1978 case DW_OP_GNU_implicit_pointer:
1979 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1980 break;
1981 case DW_OP_entry_value:
1982 case DW_OP_GNU_entry_value:
1984 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1985 size += size_of_uleb128 (op_size) + op_size;
1986 break;
1988 case DW_OP_const_type:
1989 case DW_OP_GNU_const_type:
1991 unsigned long o
1992 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1993 size += size_of_uleb128 (o) + 1;
1994 switch (loc->dw_loc_oprnd2.val_class)
1996 case dw_val_class_vec:
1997 size += loc->dw_loc_oprnd2.v.val_vec.length
1998 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1999 break;
2000 case dw_val_class_const:
2001 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
2002 break;
2003 case dw_val_class_const_double:
2004 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
2005 break;
2006 case dw_val_class_wide_int:
2007 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
2008 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
2009 break;
2010 default:
2011 gcc_unreachable ();
2013 break;
2015 case DW_OP_regval_type:
2016 case DW_OP_GNU_regval_type:
2018 unsigned long o
2019 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
2020 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
2021 + size_of_uleb128 (o);
2023 break;
2024 case DW_OP_deref_type:
2025 case DW_OP_GNU_deref_type:
2027 unsigned long o
2028 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
2029 size += 1 + size_of_uleb128 (o);
2031 break;
2032 case DW_OP_convert:
2033 case DW_OP_reinterpret:
2034 case DW_OP_GNU_convert:
2035 case DW_OP_GNU_reinterpret:
2036 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2037 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2038 else
2040 unsigned long o
2041 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
2042 size += size_of_uleb128 (o);
2044 break;
2045 case DW_OP_GNU_parameter_ref:
2046 size += 4;
2047 break;
2048 default:
2049 break;
2052 return size;
2055 /* Return the size of a series of location descriptors. */
2057 unsigned long
2058 size_of_locs (dw_loc_descr_ref loc)
2060 dw_loc_descr_ref l;
2061 unsigned long size;
2063 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
2064 field, to avoid writing to a PCH file. */
2065 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
2067 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
2068 break;
2069 size += size_of_loc_descr (l);
2071 if (! l)
2072 return size;
2074 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
2076 l->dw_loc_addr = size;
2077 size += size_of_loc_descr (l);
2080 return size;
2083 /* Return the size of the value in a DW_AT_discr_value attribute. */
2085 static int
2086 size_of_discr_value (dw_discr_value *discr_value)
2088 if (discr_value->pos)
2089 return size_of_uleb128 (discr_value->v.uval);
2090 else
2091 return size_of_sleb128 (discr_value->v.sval);
2094 /* Return the size of the value in a DW_AT_discr_list attribute. */
2096 static int
2097 size_of_discr_list (dw_discr_list_ref discr_list)
2099 int size = 0;
2101 for (dw_discr_list_ref list = discr_list;
2102 list != NULL;
2103 list = list->dw_discr_next)
2105 /* One byte for the discriminant value descriptor, and then one or two
2106 LEB128 numbers, depending on whether it's a single case label or a
2107 range label. */
2108 size += 1;
2109 size += size_of_discr_value (&list->dw_discr_lower_bound);
2110 if (list->dw_discr_range != 0)
2111 size += size_of_discr_value (&list->dw_discr_upper_bound);
2113 return size;
2116 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
2117 static void get_ref_die_offset_label (char *, dw_die_ref);
2118 static unsigned long int get_ref_die_offset (dw_die_ref);
2120 /* Output location description stack opcode's operands (if any).
2121 The for_eh_or_skip parameter controls whether register numbers are
2122 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2123 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2124 info). This should be suppressed for the cases that have not been converted
2125 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2127 static void
2128 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
2130 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2131 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2133 switch (loc->dw_loc_opc)
2135 #ifdef DWARF2_DEBUGGING_INFO
2136 case DW_OP_const2u:
2137 case DW_OP_const2s:
2138 dw2_asm_output_data (2, val1->v.val_int, NULL);
2139 break;
2140 case DW_OP_const4u:
2141 if (loc->dtprel)
2143 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2144 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
2145 val1->v.val_addr);
2146 fputc ('\n', asm_out_file);
2147 break;
2149 /* FALLTHRU */
2150 case DW_OP_const4s:
2151 dw2_asm_output_data (4, val1->v.val_int, NULL);
2152 break;
2153 case DW_OP_const8u:
2154 if (loc->dtprel)
2156 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2157 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
2158 val1->v.val_addr);
2159 fputc ('\n', asm_out_file);
2160 break;
2162 /* FALLTHRU */
2163 case DW_OP_const8s:
2164 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2165 dw2_asm_output_data (8, val1->v.val_int, NULL);
2166 break;
2167 case DW_OP_skip:
2168 case DW_OP_bra:
2170 int offset;
2172 gcc_assert (val1->val_class == dw_val_class_loc);
2173 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2175 dw2_asm_output_data (2, offset, NULL);
2177 break;
2178 case DW_OP_implicit_value:
2179 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2180 switch (val2->val_class)
2182 case dw_val_class_const:
2183 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
2184 break;
2185 case dw_val_class_vec:
2187 unsigned int elt_size = val2->v.val_vec.elt_size;
2188 unsigned int len = val2->v.val_vec.length;
2189 unsigned int i;
2190 unsigned char *p;
2192 if (elt_size > sizeof (HOST_WIDE_INT))
2194 elt_size /= 2;
2195 len *= 2;
2197 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2198 i < len;
2199 i++, p += elt_size)
2200 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2201 "fp or vector constant word %u", i);
2203 break;
2204 case dw_val_class_const_double:
2206 unsigned HOST_WIDE_INT first, second;
2208 if (WORDS_BIG_ENDIAN)
2210 first = val2->v.val_double.high;
2211 second = val2->v.val_double.low;
2213 else
2215 first = val2->v.val_double.low;
2216 second = val2->v.val_double.high;
2218 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2219 first, NULL);
2220 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2221 second, NULL);
2223 break;
2224 case dw_val_class_wide_int:
2226 int i;
2227 int len = get_full_len (*val2->v.val_wide);
2228 if (WORDS_BIG_ENDIAN)
2229 for (i = len - 1; i >= 0; --i)
2230 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2231 val2->v.val_wide->elt (i), NULL);
2232 else
2233 for (i = 0; i < len; ++i)
2234 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2235 val2->v.val_wide->elt (i), NULL);
2237 break;
2238 case dw_val_class_addr:
2239 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
2240 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
2241 break;
2242 default:
2243 gcc_unreachable ();
2245 break;
2246 #else
2247 case DW_OP_const2u:
2248 case DW_OP_const2s:
2249 case DW_OP_const4u:
2250 case DW_OP_const4s:
2251 case DW_OP_const8u:
2252 case DW_OP_const8s:
2253 case DW_OP_skip:
2254 case DW_OP_bra:
2255 case DW_OP_implicit_value:
2256 /* We currently don't make any attempt to make sure these are
2257 aligned properly like we do for the main unwind info, so
2258 don't support emitting things larger than a byte if we're
2259 only doing unwinding. */
2260 gcc_unreachable ();
2261 #endif
2262 case DW_OP_const1u:
2263 case DW_OP_const1s:
2264 dw2_asm_output_data (1, val1->v.val_int, NULL);
2265 break;
2266 case DW_OP_constu:
2267 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2268 break;
2269 case DW_OP_consts:
2270 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2271 break;
2272 case DW_OP_pick:
2273 dw2_asm_output_data (1, val1->v.val_int, NULL);
2274 break;
2275 case DW_OP_plus_uconst:
2276 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2277 break;
2278 case DW_OP_breg0:
2279 case DW_OP_breg1:
2280 case DW_OP_breg2:
2281 case DW_OP_breg3:
2282 case DW_OP_breg4:
2283 case DW_OP_breg5:
2284 case DW_OP_breg6:
2285 case DW_OP_breg7:
2286 case DW_OP_breg8:
2287 case DW_OP_breg9:
2288 case DW_OP_breg10:
2289 case DW_OP_breg11:
2290 case DW_OP_breg12:
2291 case DW_OP_breg13:
2292 case DW_OP_breg14:
2293 case DW_OP_breg15:
2294 case DW_OP_breg16:
2295 case DW_OP_breg17:
2296 case DW_OP_breg18:
2297 case DW_OP_breg19:
2298 case DW_OP_breg20:
2299 case DW_OP_breg21:
2300 case DW_OP_breg22:
2301 case DW_OP_breg23:
2302 case DW_OP_breg24:
2303 case DW_OP_breg25:
2304 case DW_OP_breg26:
2305 case DW_OP_breg27:
2306 case DW_OP_breg28:
2307 case DW_OP_breg29:
2308 case DW_OP_breg30:
2309 case DW_OP_breg31:
2310 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2311 break;
2312 case DW_OP_regx:
2314 unsigned r = val1->v.val_unsigned;
2315 if (for_eh_or_skip >= 0)
2316 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2317 gcc_assert (size_of_uleb128 (r)
2318 == size_of_uleb128 (val1->v.val_unsigned));
2319 dw2_asm_output_data_uleb128 (r, NULL);
2321 break;
2322 case DW_OP_fbreg:
2323 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2324 break;
2325 case DW_OP_bregx:
2327 unsigned r = val1->v.val_unsigned;
2328 if (for_eh_or_skip >= 0)
2329 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2330 gcc_assert (size_of_uleb128 (r)
2331 == size_of_uleb128 (val1->v.val_unsigned));
2332 dw2_asm_output_data_uleb128 (r, NULL);
2333 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2335 break;
2336 case DW_OP_piece:
2337 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2338 break;
2339 case DW_OP_bit_piece:
2340 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2341 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2342 break;
2343 case DW_OP_deref_size:
2344 case DW_OP_xderef_size:
2345 dw2_asm_output_data (1, val1->v.val_int, NULL);
2346 break;
2348 case DW_OP_addr:
2349 if (loc->dtprel)
2351 if (targetm.asm_out.output_dwarf_dtprel)
2353 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2354 DWARF2_ADDR_SIZE,
2355 val1->v.val_addr);
2356 fputc ('\n', asm_out_file);
2358 else
2359 gcc_unreachable ();
2361 else
2363 #ifdef DWARF2_DEBUGGING_INFO
2364 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2365 #else
2366 gcc_unreachable ();
2367 #endif
2369 break;
2371 case DW_OP_GNU_addr_index:
2372 case DW_OP_addrx:
2373 case DW_OP_GNU_const_index:
2374 case DW_OP_constx:
2375 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2376 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2377 "(index into .debug_addr)");
2378 break;
2380 case DW_OP_call2:
2381 case DW_OP_call4:
2383 unsigned long die_offset
2384 = get_ref_die_offset (val1->v.val_die_ref.die);
2385 /* Make sure the offset has been computed and that we can encode it as
2386 an operand. */
2387 gcc_assert (die_offset > 0
2388 && die_offset <= (loc->dw_loc_opc == DW_OP_call2
2389 ? 0xffff
2390 : 0xffffffff));
2391 dw2_asm_output_data ((loc->dw_loc_opc == DW_OP_call2) ? 2 : 4,
2392 die_offset, NULL);
2394 break;
2396 case DW_OP_call_ref:
2397 case DW_OP_GNU_variable_value:
2399 char label[MAX_ARTIFICIAL_LABEL_BYTES
2400 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2401 gcc_assert (val1->val_class == dw_val_class_die_ref);
2402 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2403 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2405 break;
2407 case DW_OP_implicit_pointer:
2408 case DW_OP_GNU_implicit_pointer:
2410 char label[MAX_ARTIFICIAL_LABEL_BYTES
2411 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2412 gcc_assert (val1->val_class == dw_val_class_die_ref);
2413 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2414 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2415 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2417 break;
2419 case DW_OP_entry_value:
2420 case DW_OP_GNU_entry_value:
2421 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2422 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2423 break;
2425 case DW_OP_const_type:
2426 case DW_OP_GNU_const_type:
2428 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2429 gcc_assert (o);
2430 dw2_asm_output_data_uleb128 (o, NULL);
2431 switch (val2->val_class)
2433 case dw_val_class_const:
2434 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2435 dw2_asm_output_data (1, l, NULL);
2436 dw2_asm_output_data (l, val2->v.val_int, NULL);
2437 break;
2438 case dw_val_class_vec:
2440 unsigned int elt_size = val2->v.val_vec.elt_size;
2441 unsigned int len = val2->v.val_vec.length;
2442 unsigned int i;
2443 unsigned char *p;
2445 l = len * elt_size;
2446 dw2_asm_output_data (1, l, NULL);
2447 if (elt_size > sizeof (HOST_WIDE_INT))
2449 elt_size /= 2;
2450 len *= 2;
2452 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2453 i < len;
2454 i++, p += elt_size)
2455 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2456 "fp or vector constant word %u", i);
2458 break;
2459 case dw_val_class_const_double:
2461 unsigned HOST_WIDE_INT first, second;
2462 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2464 dw2_asm_output_data (1, 2 * l, NULL);
2465 if (WORDS_BIG_ENDIAN)
2467 first = val2->v.val_double.high;
2468 second = val2->v.val_double.low;
2470 else
2472 first = val2->v.val_double.low;
2473 second = val2->v.val_double.high;
2475 dw2_asm_output_data (l, first, NULL);
2476 dw2_asm_output_data (l, second, NULL);
2478 break;
2479 case dw_val_class_wide_int:
2481 int i;
2482 int len = get_full_len (*val2->v.val_wide);
2483 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2485 dw2_asm_output_data (1, len * l, NULL);
2486 if (WORDS_BIG_ENDIAN)
2487 for (i = len - 1; i >= 0; --i)
2488 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2489 else
2490 for (i = 0; i < len; ++i)
2491 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2493 break;
2494 default:
2495 gcc_unreachable ();
2498 break;
2499 case DW_OP_regval_type:
2500 case DW_OP_GNU_regval_type:
2502 unsigned r = val1->v.val_unsigned;
2503 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2504 gcc_assert (o);
2505 if (for_eh_or_skip >= 0)
2507 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2508 gcc_assert (size_of_uleb128 (r)
2509 == size_of_uleb128 (val1->v.val_unsigned));
2511 dw2_asm_output_data_uleb128 (r, NULL);
2512 dw2_asm_output_data_uleb128 (o, NULL);
2514 break;
2515 case DW_OP_deref_type:
2516 case DW_OP_GNU_deref_type:
2518 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2519 gcc_assert (o);
2520 dw2_asm_output_data (1, val1->v.val_int, NULL);
2521 dw2_asm_output_data_uleb128 (o, NULL);
2523 break;
2524 case DW_OP_convert:
2525 case DW_OP_reinterpret:
2526 case DW_OP_GNU_convert:
2527 case DW_OP_GNU_reinterpret:
2528 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2529 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2530 else
2532 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2533 gcc_assert (o);
2534 dw2_asm_output_data_uleb128 (o, NULL);
2536 break;
2538 case DW_OP_GNU_parameter_ref:
2540 unsigned long o;
2541 gcc_assert (val1->val_class == dw_val_class_die_ref);
2542 o = get_ref_die_offset (val1->v.val_die_ref.die);
2543 dw2_asm_output_data (4, o, NULL);
2545 break;
2547 default:
2548 /* Other codes have no operands. */
2549 break;
2553 /* Output a sequence of location operations.
2554 The for_eh_or_skip parameter controls whether register numbers are
2555 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2556 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2557 info). This should be suppressed for the cases that have not been converted
2558 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2560 void
2561 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2563 for (; loc != NULL; loc = loc->dw_loc_next)
2565 enum dwarf_location_atom opc = loc->dw_loc_opc;
2566 /* Output the opcode. */
2567 if (for_eh_or_skip >= 0
2568 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2570 unsigned r = (opc - DW_OP_breg0);
2571 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2572 gcc_assert (r <= 31);
2573 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2575 else if (for_eh_or_skip >= 0
2576 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2578 unsigned r = (opc - DW_OP_reg0);
2579 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2580 gcc_assert (r <= 31);
2581 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2584 dw2_asm_output_data (1, opc,
2585 "%s", dwarf_stack_op_name (opc));
2587 /* Output the operand(s) (if any). */
2588 output_loc_operands (loc, for_eh_or_skip);
2592 /* Output location description stack opcode's operands (if any).
2593 The output is single bytes on a line, suitable for .cfi_escape. */
2595 static void
2596 output_loc_operands_raw (dw_loc_descr_ref loc)
2598 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2599 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2601 switch (loc->dw_loc_opc)
2603 case DW_OP_addr:
2604 case DW_OP_GNU_addr_index:
2605 case DW_OP_addrx:
2606 case DW_OP_GNU_const_index:
2607 case DW_OP_constx:
2608 case DW_OP_implicit_value:
2609 /* We cannot output addresses in .cfi_escape, only bytes. */
2610 gcc_unreachable ();
2612 case DW_OP_const1u:
2613 case DW_OP_const1s:
2614 case DW_OP_pick:
2615 case DW_OP_deref_size:
2616 case DW_OP_xderef_size:
2617 fputc (',', asm_out_file);
2618 dw2_asm_output_data_raw (1, val1->v.val_int);
2619 break;
2621 case DW_OP_const2u:
2622 case DW_OP_const2s:
2623 fputc (',', asm_out_file);
2624 dw2_asm_output_data_raw (2, val1->v.val_int);
2625 break;
2627 case DW_OP_const4u:
2628 case DW_OP_const4s:
2629 fputc (',', asm_out_file);
2630 dw2_asm_output_data_raw (4, val1->v.val_int);
2631 break;
2633 case DW_OP_const8u:
2634 case DW_OP_const8s:
2635 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2636 fputc (',', asm_out_file);
2637 dw2_asm_output_data_raw (8, val1->v.val_int);
2638 break;
2640 case DW_OP_skip:
2641 case DW_OP_bra:
2643 int offset;
2645 gcc_assert (val1->val_class == dw_val_class_loc);
2646 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2648 fputc (',', asm_out_file);
2649 dw2_asm_output_data_raw (2, offset);
2651 break;
2653 case DW_OP_regx:
2655 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2656 gcc_assert (size_of_uleb128 (r)
2657 == size_of_uleb128 (val1->v.val_unsigned));
2658 fputc (',', asm_out_file);
2659 dw2_asm_output_data_uleb128_raw (r);
2661 break;
2663 case DW_OP_constu:
2664 case DW_OP_plus_uconst:
2665 case DW_OP_piece:
2666 fputc (',', asm_out_file);
2667 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2668 break;
2670 case DW_OP_bit_piece:
2671 fputc (',', asm_out_file);
2672 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2673 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2674 break;
2676 case DW_OP_consts:
2677 case DW_OP_breg0:
2678 case DW_OP_breg1:
2679 case DW_OP_breg2:
2680 case DW_OP_breg3:
2681 case DW_OP_breg4:
2682 case DW_OP_breg5:
2683 case DW_OP_breg6:
2684 case DW_OP_breg7:
2685 case DW_OP_breg8:
2686 case DW_OP_breg9:
2687 case DW_OP_breg10:
2688 case DW_OP_breg11:
2689 case DW_OP_breg12:
2690 case DW_OP_breg13:
2691 case DW_OP_breg14:
2692 case DW_OP_breg15:
2693 case DW_OP_breg16:
2694 case DW_OP_breg17:
2695 case DW_OP_breg18:
2696 case DW_OP_breg19:
2697 case DW_OP_breg20:
2698 case DW_OP_breg21:
2699 case DW_OP_breg22:
2700 case DW_OP_breg23:
2701 case DW_OP_breg24:
2702 case DW_OP_breg25:
2703 case DW_OP_breg26:
2704 case DW_OP_breg27:
2705 case DW_OP_breg28:
2706 case DW_OP_breg29:
2707 case DW_OP_breg30:
2708 case DW_OP_breg31:
2709 case DW_OP_fbreg:
2710 fputc (',', asm_out_file);
2711 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2712 break;
2714 case DW_OP_bregx:
2716 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2717 gcc_assert (size_of_uleb128 (r)
2718 == size_of_uleb128 (val1->v.val_unsigned));
2719 fputc (',', asm_out_file);
2720 dw2_asm_output_data_uleb128_raw (r);
2721 fputc (',', asm_out_file);
2722 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2724 break;
2726 case DW_OP_implicit_pointer:
2727 case DW_OP_entry_value:
2728 case DW_OP_const_type:
2729 case DW_OP_regval_type:
2730 case DW_OP_deref_type:
2731 case DW_OP_convert:
2732 case DW_OP_reinterpret:
2733 case DW_OP_GNU_implicit_pointer:
2734 case DW_OP_GNU_entry_value:
2735 case DW_OP_GNU_const_type:
2736 case DW_OP_GNU_regval_type:
2737 case DW_OP_GNU_deref_type:
2738 case DW_OP_GNU_convert:
2739 case DW_OP_GNU_reinterpret:
2740 case DW_OP_GNU_parameter_ref:
2741 gcc_unreachable ();
2742 break;
2744 default:
2745 /* Other codes have no operands. */
2746 break;
2750 void
2751 output_loc_sequence_raw (dw_loc_descr_ref loc)
2753 while (1)
2755 enum dwarf_location_atom opc = loc->dw_loc_opc;
2756 /* Output the opcode. */
2757 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2759 unsigned r = (opc - DW_OP_breg0);
2760 r = DWARF2_FRAME_REG_OUT (r, 1);
2761 gcc_assert (r <= 31);
2762 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2764 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2766 unsigned r = (opc - DW_OP_reg0);
2767 r = DWARF2_FRAME_REG_OUT (r, 1);
2768 gcc_assert (r <= 31);
2769 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2771 /* Output the opcode. */
2772 fprintf (asm_out_file, "%#x", opc);
2773 output_loc_operands_raw (loc);
2775 if (!loc->dw_loc_next)
2776 break;
2777 loc = loc->dw_loc_next;
2779 fputc (',', asm_out_file);
2783 static void
2784 build_breg_loc (struct dw_loc_descr_node **head, unsigned int regno)
2786 if (regno <= 31)
2787 add_loc_descr (head, new_loc_descr ((enum dwarf_location_atom)
2788 (DW_OP_breg0 + regno), 0, 0));
2789 else
2790 add_loc_descr (head, new_loc_descr (DW_OP_bregx, regno, 0));
2793 /* Build a dwarf location for a cfa_reg spanning multiple
2794 consecutive registers. */
2796 struct dw_loc_descr_node *
2797 build_span_loc (struct cfa_reg reg)
2799 struct dw_loc_descr_node *head = NULL;
2801 gcc_assert (reg.span_width > 0);
2802 gcc_assert (reg.span > 1);
2804 /* Start from the highest number register as it goes in the upper bits. */
2805 unsigned int regno = reg.reg + reg.span - 1;
2806 build_breg_loc (&head, regno);
2808 /* Deal with the remaining registers in the span. */
2809 for (int i = reg.span - 2; i >= 0; i--)
2811 add_loc_descr (&head, int_loc_descriptor (reg.span_width * 8));
2812 add_loc_descr (&head, new_loc_descr (DW_OP_shl, 0, 0));
2813 regno--;
2814 build_breg_loc (&head, regno);
2815 add_loc_descr (&head, new_loc_descr (DW_OP_plus, 0, 0));
2817 return head;
2820 /* This function builds a dwarf location descriptor sequence from a
2821 dw_cfa_location, adding the given OFFSET to the result of the
2822 expression. */
2824 struct dw_loc_descr_node *
2825 build_cfa_loc (dw_cfa_location *cfa, poly_int64 offset)
2827 struct dw_loc_descr_node *head, *tmp;
2829 offset += cfa->offset;
2831 if (cfa->reg.span > 1)
2833 head = build_span_loc (cfa->reg);
2835 if (maybe_ne (offset, 0))
2836 loc_descr_plus_const (&head, offset);
2838 else if (cfa->indirect)
2840 head = new_reg_loc_descr (cfa->reg.reg, cfa->base_offset);
2841 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2842 head->dw_loc_oprnd1.val_entry = NULL;
2843 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2844 add_loc_descr (&head, tmp);
2845 loc_descr_plus_const (&head, offset);
2847 else
2848 head = new_reg_loc_descr (cfa->reg.reg, offset);
2850 return head;
2853 /* This function builds a dwarf location descriptor sequence for
2854 the address at OFFSET from the CFA when stack is aligned to
2855 ALIGNMENT byte. */
2857 struct dw_loc_descr_node *
2858 build_cfa_aligned_loc (dw_cfa_location *cfa,
2859 poly_int64 offset, HOST_WIDE_INT alignment)
2861 struct dw_loc_descr_node *head;
2862 unsigned int dwarf_fp
2863 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2865 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2866 if (cfa->reg.reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2868 head = new_reg_loc_descr (dwarf_fp, 0);
2869 add_loc_descr (&head, int_loc_descriptor (alignment));
2870 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2871 loc_descr_plus_const (&head, offset);
2873 else
2874 head = new_reg_loc_descr (dwarf_fp, offset);
2875 return head;
2878 /* And now, the support for symbolic debugging information. */
2880 /* .debug_str support. */
2882 static void dwarf2out_init (const char *);
2883 static void dwarf2out_finish (const char *);
2884 static void dwarf2out_early_finish (const char *);
2885 static void dwarf2out_assembly_start (void);
2886 static void dwarf2out_define (unsigned int, const char *);
2887 static void dwarf2out_undef (unsigned int, const char *);
2888 static void dwarf2out_start_source_file (unsigned, const char *);
2889 static void dwarf2out_end_source_file (unsigned);
2890 static void dwarf2out_function_decl (tree);
2891 static void dwarf2out_begin_block (unsigned, unsigned);
2892 static void dwarf2out_end_block (unsigned, unsigned);
2893 static bool dwarf2out_ignore_block (const_tree);
2894 static void dwarf2out_set_ignored_loc (unsigned, unsigned, const char *);
2895 static void dwarf2out_early_global_decl (tree);
2896 static void dwarf2out_late_global_decl (tree);
2897 static void dwarf2out_type_decl (tree, int);
2898 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool, bool);
2899 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2900 dw_die_ref);
2901 static void dwarf2out_abstract_function (tree);
2902 static void dwarf2out_var_location (rtx_insn *);
2903 static void dwarf2out_inline_entry (tree);
2904 static void dwarf2out_size_function (tree);
2905 static void dwarf2out_begin_function (tree);
2906 static void dwarf2out_end_function (unsigned int);
2907 static void dwarf2out_register_main_translation_unit (tree unit);
2908 static void dwarf2out_set_name (tree, tree);
2909 static void dwarf2out_register_external_die (tree decl, const char *sym,
2910 unsigned HOST_WIDE_INT off);
2911 static bool dwarf2out_die_ref_for_decl (tree decl, const char **sym,
2912 unsigned HOST_WIDE_INT *off);
2914 /* The debug hooks structure. */
2916 const struct gcc_debug_hooks dwarf2_debug_hooks =
2918 dwarf2out_init,
2919 dwarf2out_finish,
2920 dwarf2out_early_finish,
2921 dwarf2out_assembly_start,
2922 dwarf2out_define,
2923 dwarf2out_undef,
2924 dwarf2out_start_source_file,
2925 dwarf2out_end_source_file,
2926 dwarf2out_begin_block,
2927 dwarf2out_end_block,
2928 dwarf2out_ignore_block,
2929 dwarf2out_source_line,
2930 dwarf2out_set_ignored_loc,
2931 dwarf2out_begin_prologue,
2932 #if VMS_DEBUGGING_INFO
2933 dwarf2out_vms_end_prologue,
2934 dwarf2out_vms_begin_epilogue,
2935 #else
2936 debug_nothing_int_charstar,
2937 debug_nothing_int_charstar,
2938 #endif
2939 dwarf2out_end_epilogue,
2940 dwarf2out_begin_function,
2941 dwarf2out_end_function, /* end_function */
2942 dwarf2out_register_main_translation_unit,
2943 dwarf2out_function_decl, /* function_decl */
2944 dwarf2out_early_global_decl,
2945 dwarf2out_late_global_decl,
2946 dwarf2out_type_decl, /* type_decl */
2947 dwarf2out_imported_module_or_decl,
2948 dwarf2out_die_ref_for_decl,
2949 dwarf2out_register_external_die,
2950 debug_nothing_tree, /* deferred_inline_function */
2951 /* The DWARF 2 backend tries to reduce debugging bloat by not
2952 emitting the abstract description of inline functions until
2953 something tries to reference them. */
2954 dwarf2out_abstract_function, /* outlining_inline_function */
2955 debug_nothing_rtx_code_label, /* label */
2956 debug_nothing_int, /* handle_pch */
2957 dwarf2out_var_location,
2958 dwarf2out_inline_entry, /* inline_entry */
2959 dwarf2out_size_function, /* size_function */
2960 dwarf2out_switch_text_section,
2961 dwarf2out_set_name,
2962 1, /* start_end_main_source_file */
2963 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2966 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2968 dwarf2out_init,
2969 debug_nothing_charstar,
2970 debug_nothing_charstar,
2971 dwarf2out_assembly_start,
2972 debug_nothing_int_charstar,
2973 debug_nothing_int_charstar,
2974 debug_nothing_int_charstar,
2975 debug_nothing_int,
2976 debug_nothing_int_int, /* begin_block */
2977 debug_nothing_int_int, /* end_block */
2978 debug_true_const_tree, /* ignore_block */
2979 dwarf2out_source_line, /* source_line */
2980 debug_nothing_int_int_charstar, /* set_ignored_loc */
2981 debug_nothing_int_int_charstar, /* begin_prologue */
2982 debug_nothing_int_charstar, /* end_prologue */
2983 debug_nothing_int_charstar, /* begin_epilogue */
2984 debug_nothing_int_charstar, /* end_epilogue */
2985 debug_nothing_tree, /* begin_function */
2986 debug_nothing_int, /* end_function */
2987 debug_nothing_tree, /* register_main_translation_unit */
2988 debug_nothing_tree, /* function_decl */
2989 debug_nothing_tree, /* early_global_decl */
2990 debug_nothing_tree, /* late_global_decl */
2991 debug_nothing_tree_int, /* type_decl */
2992 debug_nothing_tree_tree_tree_bool_bool,/* imported_module_or_decl */
2993 debug_false_tree_charstarstar_uhwistar,/* die_ref_for_decl */
2994 debug_nothing_tree_charstar_uhwi, /* register_external_die */
2995 debug_nothing_tree, /* deferred_inline_function */
2996 debug_nothing_tree, /* outlining_inline_function */
2997 debug_nothing_rtx_code_label, /* label */
2998 debug_nothing_int, /* handle_pch */
2999 debug_nothing_rtx_insn, /* var_location */
3000 debug_nothing_tree, /* inline_entry */
3001 debug_nothing_tree, /* size_function */
3002 debug_nothing_void, /* switch_text_section */
3003 debug_nothing_tree_tree, /* set_name */
3004 0, /* start_end_main_source_file */
3005 TYPE_SYMTAB_IS_ADDRESS /* tree_type_symtab_field */
3008 /* NOTE: In the comments in this file, many references are made to
3009 "Debugging Information Entries". This term is abbreviated as `DIE'
3010 throughout the remainder of this file. */
3012 /* An internal representation of the DWARF output is built, and then
3013 walked to generate the DWARF debugging info. The walk of the internal
3014 representation is done after the entire program has been compiled.
3015 The types below are used to describe the internal representation. */
3017 /* Whether to put type DIEs into their own section .debug_types instead
3018 of making them part of the .debug_info section. Only supported for
3019 Dwarf V4 or higher and the user didn't disable them through
3020 -fno-debug-types-section. It is more efficient to put them in a
3021 separate comdat sections since the linker will then be able to
3022 remove duplicates. But not all tools support .debug_types sections
3023 yet. For Dwarf V5 or higher .debug_types doesn't exist any more,
3024 it is DW_UT_type unit type in .debug_info section. For late LTO
3025 debug there should be almost no types emitted so avoid enabling
3026 -fdebug-types-section there. */
3028 #define use_debug_types (dwarf_version >= 4 \
3029 && flag_debug_types_section \
3030 && !in_lto_p)
3032 /* Various DIE's use offsets relative to the beginning of the
3033 .debug_info section to refer to each other. */
3035 typedef long int dw_offset;
3037 struct comdat_type_node;
3039 /* The entries in the line_info table more-or-less mirror the opcodes
3040 that are used in the real dwarf line table. Arrays of these entries
3041 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
3042 supported. */
3044 enum dw_line_info_opcode {
3045 /* Emit DW_LNE_set_address; the operand is the label index. */
3046 LI_set_address,
3048 /* Emit a row to the matrix with the given line. This may be done
3049 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
3050 special opcodes. */
3051 LI_set_line,
3053 /* Emit a DW_LNS_set_file. */
3054 LI_set_file,
3056 /* Emit a DW_LNS_set_column. */
3057 LI_set_column,
3059 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
3060 LI_negate_stmt,
3062 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
3063 LI_set_prologue_end,
3064 LI_set_epilogue_begin,
3066 /* Emit a DW_LNE_set_discriminator. */
3067 LI_set_discriminator,
3069 /* Output a Fixed Advance PC; the target PC is the label index; the
3070 base PC is the previous LI_adv_address or LI_set_address entry.
3071 We only use this when emitting debug views without assembler
3072 support, at explicit user request. Ideally, we should only use
3073 it when the offset might be zero but we can't tell: it's the only
3074 way to maybe change the PC without resetting the view number. */
3075 LI_adv_address
3078 typedef struct GTY(()) dw_line_info_struct {
3079 enum dw_line_info_opcode opcode;
3080 unsigned int val;
3081 } dw_line_info_entry;
3084 struct GTY(()) dw_line_info_table {
3085 /* The label that marks the end of this section. */
3086 const char *end_label;
3088 /* The values for the last row of the matrix, as collected in the table.
3089 These are used to minimize the changes to the next row. */
3090 unsigned int file_num;
3091 unsigned int line_num;
3092 unsigned int column_num;
3093 int discrim_num;
3094 bool is_stmt;
3095 bool in_use;
3097 /* This denotes the NEXT view number.
3099 If it is 0, it is known that the NEXT view will be the first view
3100 at the given PC.
3102 If it is -1, we're forcing the view number to be reset, e.g. at a
3103 function entry.
3105 The meaning of other nonzero values depends on whether we're
3106 computing views internally or leaving it for the assembler to do
3107 so. If we're emitting them internally, view denotes the view
3108 number since the last known advance of PC. If we're leaving it
3109 for the assembler, it denotes the LVU label number that we're
3110 going to ask the assembler to assign. */
3111 var_loc_view view;
3113 /* This counts the number of symbolic views emitted in this table
3114 since the latest view reset. Its max value, over all tables,
3115 sets symview_upper_bound. */
3116 var_loc_view symviews_since_reset;
3118 #define FORCE_RESET_NEXT_VIEW(x) ((x) = (var_loc_view)-1)
3119 #define RESET_NEXT_VIEW(x) ((x) = (var_loc_view)0)
3120 #define FORCE_RESETTING_VIEW_P(x) ((x) == (var_loc_view)-1)
3121 #define RESETTING_VIEW_P(x) ((x) == (var_loc_view)0 || FORCE_RESETTING_VIEW_P (x))
3123 vec<dw_line_info_entry, va_gc> *entries;
3126 /* This is an upper bound for view numbers that the assembler may
3127 assign to symbolic views output in this translation. It is used to
3128 decide how big a field to use to represent view numbers in
3129 symview-classed attributes. */
3131 static var_loc_view symview_upper_bound;
3133 /* If we're keep track of location views and their reset points, and
3134 INSN is a reset point (i.e., it necessarily advances the PC), mark
3135 the next view in TABLE as reset. */
3137 static void
3138 maybe_reset_location_view (rtx_insn *insn, dw_line_info_table *table)
3140 if (!debug_internal_reset_location_views)
3141 return;
3143 /* Maybe turn (part of?) this test into a default target hook. */
3144 int reset = 0;
3146 if (targetm.reset_location_view)
3147 reset = targetm.reset_location_view (insn);
3149 if (reset)
3151 else if (JUMP_TABLE_DATA_P (insn))
3152 reset = 1;
3153 else if (GET_CODE (insn) == USE
3154 || GET_CODE (insn) == CLOBBER
3155 || GET_CODE (insn) == ASM_INPUT
3156 || asm_noperands (insn) >= 0)
3158 else if (get_attr_min_length (insn) > 0)
3159 reset = 1;
3161 if (reset > 0 && !RESETTING_VIEW_P (table->view))
3162 RESET_NEXT_VIEW (table->view);
3165 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
3166 The children of each node form a circular list linked by
3167 die_sib. die_child points to the node *before* the "first" child node. */
3169 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
3170 union die_symbol_or_type_node
3172 const char * GTY ((tag ("0"))) die_symbol;
3173 comdat_type_node *GTY ((tag ("1"))) die_type_node;
3175 GTY ((desc ("%0.comdat_type_p"))) die_id;
3176 vec<dw_attr_node, va_gc> *die_attr;
3177 dw_die_ref die_parent;
3178 dw_die_ref die_child;
3179 dw_die_ref die_sib;
3180 dw_die_ref die_definition; /* ref from a specification to its definition */
3181 dw_offset die_offset;
3182 unsigned long die_abbrev;
3183 int die_mark;
3184 unsigned int decl_id;
3185 enum dwarf_tag die_tag;
3186 /* Die is used and must not be pruned as unused. */
3187 BOOL_BITFIELD die_perennial_p : 1;
3188 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
3189 /* For an external ref to die_symbol if die_offset contains an extra
3190 offset to that symbol. */
3191 BOOL_BITFIELD with_offset : 1;
3192 /* Whether this DIE was removed from the DIE tree, for example via
3193 prune_unused_types. We don't consider those present from the
3194 DIE lookup routines. */
3195 BOOL_BITFIELD removed : 1;
3196 /* Lots of spare bits. */
3198 die_node;
3200 /* Set to TRUE while dwarf2out_early_global_decl is running. */
3201 static bool early_dwarf;
3202 static bool early_dwarf_finished;
3203 class set_early_dwarf {
3204 public:
3205 bool saved;
3206 set_early_dwarf () : saved(early_dwarf)
3208 gcc_assert (! early_dwarf_finished);
3209 early_dwarf = true;
3211 ~set_early_dwarf () { early_dwarf = saved; }
3214 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
3215 #define FOR_EACH_CHILD(die, c, expr) do { \
3216 c = die->die_child; \
3217 if (c) do { \
3218 c = c->die_sib; \
3219 expr; \
3220 } while (c != die->die_child); \
3221 } while (0)
3223 /* The pubname structure */
3225 typedef struct GTY(()) pubname_struct {
3226 dw_die_ref die;
3227 const char *name;
3229 pubname_entry;
3232 struct GTY(()) dw_ranges {
3233 const char *label;
3234 /* If this is positive, it's a block number, otherwise it's a
3235 bitwise-negated index into dw_ranges_by_label. */
3236 int num;
3237 /* If idx is equal to DW_RANGES_IDX_SKELETON, it should be emitted
3238 into .debug_rnglists section rather than .debug_rnglists.dwo
3239 for -gsplit-dwarf and DWARF >= 5. */
3240 #define DW_RANGES_IDX_SKELETON ((1U << 31) - 1)
3241 /* Index for the range list for DW_FORM_rnglistx. */
3242 unsigned int idx : 31;
3243 /* True if this range might be possibly in a different section
3244 from previous entry. */
3245 unsigned int maybe_new_sec : 1;
3246 addr_table_entry *begin_entry;
3247 addr_table_entry *end_entry;
3250 /* A structure to hold a macinfo entry. */
3252 typedef struct GTY(()) macinfo_struct {
3253 unsigned char code;
3254 unsigned HOST_WIDE_INT lineno;
3255 const char *info;
3257 macinfo_entry;
3260 struct GTY(()) dw_ranges_by_label {
3261 const char *begin;
3262 const char *end;
3265 /* The comdat type node structure. */
3266 struct GTY(()) comdat_type_node
3268 dw_die_ref root_die;
3269 dw_die_ref type_die;
3270 dw_die_ref skeleton_die;
3271 char signature[DWARF_TYPE_SIGNATURE_SIZE];
3272 comdat_type_node *next;
3275 /* A list of DIEs for which we can't determine ancestry (parent_die
3276 field) just yet. Later in dwarf2out_finish we will fill in the
3277 missing bits. */
3278 typedef struct GTY(()) limbo_die_struct {
3279 dw_die_ref die;
3280 /* The tree for which this DIE was created. We use this to
3281 determine ancestry later. */
3282 tree created_for;
3283 struct limbo_die_struct *next;
3285 limbo_die_node;
3287 typedef struct skeleton_chain_struct
3289 dw_die_ref old_die;
3290 dw_die_ref new_die;
3291 struct skeleton_chain_struct *parent;
3293 skeleton_chain_node;
3295 /* Define a macro which returns nonzero for a TYPE_DECL which was
3296 implicitly generated for a type.
3298 Note that, unlike the C front-end (which generates a NULL named
3299 TYPE_DECL node for each complete tagged type, each array type,
3300 and each function type node created) the C++ front-end generates
3301 a _named_ TYPE_DECL node for each tagged type node created.
3302 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3303 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
3304 front-end, but for each type, tagged or not. */
3306 #define TYPE_DECL_IS_STUB(decl) \
3307 (DECL_NAME (decl) == NULL_TREE \
3308 || (DECL_ARTIFICIAL (decl) \
3309 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3310 /* This is necessary for stub decls that \
3311 appear in nested inline functions. */ \
3312 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3313 && (decl_ultimate_origin (decl) \
3314 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3316 /* Information concerning the compilation unit's programming
3317 language, and compiler version. */
3319 /* Fixed size portion of the DWARF compilation unit header. */
3320 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3321 (DWARF_INITIAL_LENGTH_SIZE + dwarf_offset_size \
3322 + (dwarf_version >= 5 ? 4 : 3))
3324 /* Fixed size portion of the DWARF comdat type unit header. */
3325 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
3326 (DWARF_COMPILE_UNIT_HEADER_SIZE \
3327 + DWARF_TYPE_SIGNATURE_SIZE + dwarf_offset_size)
3329 /* Fixed size portion of the DWARF skeleton compilation unit header. */
3330 #define DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE \
3331 (DWARF_COMPILE_UNIT_HEADER_SIZE + (dwarf_version >= 5 ? 8 : 0))
3333 /* Fixed size portion of public names info. */
3334 #define DWARF_PUBNAMES_HEADER_SIZE (2 * dwarf_offset_size + 2)
3336 /* Fixed size portion of the address range info. */
3337 #define DWARF_ARANGES_HEADER_SIZE \
3338 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + dwarf_offset_size + 4, \
3339 DWARF2_ADDR_SIZE * 2) \
3340 - DWARF_INITIAL_LENGTH_SIZE)
3342 /* Size of padding portion in the address range info. It must be
3343 aligned to twice the pointer size. */
3344 #define DWARF_ARANGES_PAD_SIZE \
3345 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + dwarf_offset_size + 4, \
3346 DWARF2_ADDR_SIZE * 2) \
3347 - (DWARF_INITIAL_LENGTH_SIZE + dwarf_offset_size + 4))
3349 /* Use assembler line directives if available. */
3350 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3351 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3352 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3353 #else
3354 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3355 #endif
3356 #endif
3358 /* Use assembler views in line directives if available. */
3359 #ifndef DWARF2_ASM_VIEW_DEBUG_INFO
3360 #ifdef HAVE_AS_DWARF2_DEBUG_VIEW
3361 #define DWARF2_ASM_VIEW_DEBUG_INFO 1
3362 #else
3363 #define DWARF2_ASM_VIEW_DEBUG_INFO 0
3364 #endif
3365 #endif
3367 /* Return true if GCC configure detected assembler support for .loc. */
3369 bool
3370 dwarf2out_default_as_loc_support (void)
3372 return DWARF2_ASM_LINE_DEBUG_INFO;
3373 #if (GCC_VERSION >= 3000)
3374 # undef DWARF2_ASM_LINE_DEBUG_INFO
3375 # pragma GCC poison DWARF2_ASM_LINE_DEBUG_INFO
3376 #endif
3379 /* Return true if GCC configure detected assembler support for views
3380 in .loc directives. */
3382 bool
3383 dwarf2out_default_as_locview_support (void)
3385 return DWARF2_ASM_VIEW_DEBUG_INFO;
3386 #if (GCC_VERSION >= 3000)
3387 # undef DWARF2_ASM_VIEW_DEBUG_INFO
3388 # pragma GCC poison DWARF2_ASM_VIEW_DEBUG_INFO
3389 #endif
3392 /* A bit is set in ZERO_VIEW_P if we are using the assembler-supported
3393 view computation, and it refers to a view identifier for which we
3394 will not emit a label because it is known to map to a view number
3395 zero. We won't allocate the bitmap if we're not using assembler
3396 support for location views, but we have to make the variable
3397 visible for GGC and for code that will be optimized out for lack of
3398 support but that's still parsed and compiled. We could abstract it
3399 out with macros, but it's not worth it. */
3400 static GTY(()) bitmap zero_view_p;
3402 /* Evaluate to TRUE iff N is known to identify the first location view
3403 at its PC. When not using assembler location view computation,
3404 that must be view number zero. Otherwise, ZERO_VIEW_P is allocated
3405 and views label numbers recorded in it are the ones known to be
3406 zero. */
3407 #define ZERO_VIEW_P(N) ((N) == (var_loc_view)0 \
3408 || (N) == (var_loc_view)-1 \
3409 || (zero_view_p \
3410 && bitmap_bit_p (zero_view_p, (N))))
3412 /* Return true iff we're to emit .loc directives for the assembler to
3413 generate line number sections.
3415 When we're not emitting views, all we need from the assembler is
3416 support for .loc directives.
3418 If we are emitting views, we can only use the assembler's .loc
3419 support if it also supports views.
3421 When the compiler is emitting the line number programs and
3422 computing view numbers itself, it resets view numbers at known PC
3423 changes and counts from that, and then it emits view numbers as
3424 literal constants in locviewlists. There are cases in which the
3425 compiler is not sure about PC changes, e.g. when extra alignment is
3426 requested for a label. In these cases, the compiler may not reset
3427 the view counter, and the potential PC advance in the line number
3428 program will use an opcode that does not reset the view counter
3429 even if the PC actually changes, so that compiler and debug info
3430 consumer can keep view numbers in sync.
3432 When the compiler defers view computation to the assembler, it
3433 emits symbolic view numbers in locviewlists, with the exception of
3434 views known to be zero (forced resets, or reset after
3435 compiler-visible PC changes): instead of emitting symbols for
3436 these, we emit literal zero and assert the assembler agrees with
3437 the compiler's assessment. We could use symbolic views everywhere,
3438 instead of special-casing zero views, but then we'd be unable to
3439 optimize out locviewlists that contain only zeros. */
3441 static bool
3442 output_asm_line_debug_info (void)
3444 return (dwarf2out_as_loc_support
3445 && (dwarf2out_as_locview_support
3446 || !debug_variable_location_views));
3449 static bool asm_outputs_debug_line_str (void);
3451 /* Minimum line offset in a special line info. opcode.
3452 This value was chosen to give a reasonable range of values. */
3453 #define DWARF_LINE_BASE -10
3455 /* First special line opcode - leave room for the standard opcodes. */
3456 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
3458 /* Range of line offsets in a special line info. opcode. */
3459 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3461 /* Flag that indicates the initial value of the is_stmt_start flag.
3462 In the present implementation, we do not mark any lines as
3463 the beginning of a source statement, because that information
3464 is not made available by the GCC front-end. */
3465 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3467 /* Maximum number of operations per instruction bundle. */
3468 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
3469 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
3470 #endif
3472 /* This location is used by calc_die_sizes() to keep track
3473 the offset of each DIE within the .debug_info section. */
3474 static unsigned long next_die_offset;
3476 /* Record the root of the DIE's built for the current compilation unit. */
3477 static GTY(()) dw_die_ref single_comp_unit_die;
3479 /* A list of type DIEs that have been separated into comdat sections. */
3480 static GTY(()) comdat_type_node *comdat_type_list;
3482 /* A list of CU DIEs that have been separated. */
3483 static GTY(()) limbo_die_node *cu_die_list;
3485 /* A list of DIEs with a NULL parent waiting to be relocated. */
3486 static GTY(()) limbo_die_node *limbo_die_list;
3488 /* A list of DIEs for which we may have to generate
3489 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
3490 static GTY(()) limbo_die_node *deferred_asm_name;
3492 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
3494 typedef const char *compare_type;
3496 static hashval_t hash (dwarf_file_data *);
3497 static bool equal (dwarf_file_data *, const char *);
3500 /* Filenames referenced by this compilation unit. */
3501 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
3503 struct decl_die_hasher : ggc_ptr_hash<die_node>
3505 typedef tree compare_type;
3507 static hashval_t hash (die_node *);
3508 static bool equal (die_node *, tree);
3510 /* A hash table of references to DIE's that describe declarations.
3511 The key is a DECL_UID() which is a unique number identifying each decl. */
3512 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
3514 struct GTY ((for_user)) variable_value_struct {
3515 unsigned int decl_id;
3516 vec<dw_die_ref, va_gc> *dies;
3519 struct variable_value_hasher : ggc_ptr_hash<variable_value_struct>
3521 typedef tree compare_type;
3523 static hashval_t hash (variable_value_struct *);
3524 static bool equal (variable_value_struct *, tree);
3526 /* A hash table of DIEs that contain DW_OP_GNU_variable_value with
3527 dw_val_class_decl_ref class, indexed by FUNCTION_DECLs which is
3528 DECL_CONTEXT of the referenced VAR_DECLs. */
3529 static GTY (()) hash_table<variable_value_hasher> *variable_value_hash;
3531 struct block_die_hasher : ggc_ptr_hash<die_struct>
3533 static hashval_t hash (die_struct *);
3534 static bool equal (die_struct *, die_struct *);
3537 /* A hash table of references to DIE's that describe COMMON blocks.
3538 The key is DECL_UID() ^ die_parent. */
3539 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
3541 typedef struct GTY(()) die_arg_entry_struct {
3542 dw_die_ref die;
3543 tree arg;
3544 } die_arg_entry;
3547 /* Node of the variable location list. */
3548 struct GTY ((chain_next ("%h.next"))) var_loc_node {
3549 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
3550 EXPR_LIST chain. For small bitsizes, bitsize is encoded
3551 in mode of the EXPR_LIST node and first EXPR_LIST operand
3552 is either NOTE_INSN_VAR_LOCATION for a piece with a known
3553 location or NULL for padding. For larger bitsizes,
3554 mode is 0 and first operand is a CONCAT with bitsize
3555 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
3556 NULL as second operand. */
3557 rtx GTY (()) loc;
3558 const char * GTY (()) label;
3559 struct var_loc_node * GTY (()) next;
3560 var_loc_view view;
3563 /* Variable location list. */
3564 struct GTY ((for_user)) var_loc_list_def {
3565 struct var_loc_node * GTY (()) first;
3567 /* Pointer to the last but one or last element of the
3568 chained list. If the list is empty, both first and
3569 last are NULL, if the list contains just one node
3570 or the last node certainly is not redundant, it points
3571 to the last node, otherwise points to the last but one.
3572 Do not mark it for GC because it is marked through the chain. */
3573 struct var_loc_node * GTY ((skip ("%h"))) last;
3575 /* Pointer to the last element before section switch,
3576 if NULL, either sections weren't switched or first
3577 is after section switch. */
3578 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
3580 /* DECL_UID of the variable decl. */
3581 unsigned int decl_id;
3583 typedef struct var_loc_list_def var_loc_list;
3585 /* Call argument location list. */
3586 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
3587 rtx GTY (()) call_arg_loc_note;
3588 const char * GTY (()) label;
3589 tree GTY (()) block;
3590 bool tail_call_p;
3591 rtx GTY (()) symbol_ref;
3592 struct call_arg_loc_node * GTY (()) next;
3596 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
3598 typedef const_tree compare_type;
3600 static hashval_t hash (var_loc_list *);
3601 static bool equal (var_loc_list *, const_tree);
3604 /* Table of decl location linked lists. */
3605 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
3607 /* Head and tail of call_arg_loc chain. */
3608 static GTY (()) struct call_arg_loc_node *call_arg_locations;
3609 static struct call_arg_loc_node *call_arg_loc_last;
3611 /* Number of call sites in the current function. */
3612 static int call_site_count = -1;
3613 /* Number of tail call sites in the current function. */
3614 static int tail_call_site_count = -1;
3616 /* A cached location list. */
3617 struct GTY ((for_user)) cached_dw_loc_list_def {
3618 /* The DECL_UID of the decl that this entry describes. */
3619 unsigned int decl_id;
3621 /* The cached location list. */
3622 dw_loc_list_ref loc_list;
3624 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3626 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
3629 typedef const_tree compare_type;
3631 static hashval_t hash (cached_dw_loc_list *);
3632 static bool equal (cached_dw_loc_list *, const_tree);
3635 /* Table of cached location lists. */
3636 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
3638 /* A vector of references to DIE's that are uniquely identified by their tag,
3639 presence/absence of children DIE's, and list of attribute/value pairs. */
3640 static GTY(()) vec<dw_die_ref, va_gc> *abbrev_die_table;
3642 /* A hash map to remember the stack usage for DWARF procedures. The value
3643 stored is the stack size difference between before the DWARF procedure
3644 invokation and after it returned. In other words, for a DWARF procedure
3645 that consumes N stack slots and that pushes M ones, this stores M - N. */
3646 static hash_map<dw_die_ref, int> *dwarf_proc_stack_usage_map;
3648 /* A global counter for generating labels for line number data. */
3649 static unsigned int line_info_label_num;
3651 /* The current table to which we should emit line number information
3652 for the current function. This will be set up at the beginning of
3653 assembly for the function. */
3654 static GTY(()) dw_line_info_table *cur_line_info_table;
3656 /* The two default tables of line number info. */
3657 static GTY(()) dw_line_info_table *text_section_line_info;
3658 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3660 /* The set of all non-default tables of line number info. */
3661 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
3663 /* A flag to tell pubnames/types export if there is an info section to
3664 refer to. */
3665 static bool info_section_emitted;
3667 /* A pointer to the base of a table that contains a list of publicly
3668 accessible names. */
3669 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3671 /* A pointer to the base of a table that contains a list of publicly
3672 accessible types. */
3673 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3675 /* A pointer to the base of a table that contains a list of macro
3676 defines/undefines (and file start/end markers). */
3677 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3679 /* True if .debug_macinfo or .debug_macros section is going to be
3680 emitted. */
3681 #define have_macinfo \
3682 ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3683 && debug_info_level >= DINFO_LEVEL_VERBOSE \
3684 && !macinfo_table->is_empty ())
3686 /* Vector of dies for which we should generate .debug_ranges info. */
3687 static GTY (()) vec<dw_ranges, va_gc> *ranges_table;
3689 /* Vector of pairs of labels referenced in ranges_table. */
3690 static GTY (()) vec<dw_ranges_by_label, va_gc> *ranges_by_label;
3692 /* Whether we have location lists that need outputting */
3693 static GTY(()) bool have_location_lists;
3695 /* Unique label counter. */
3696 static GTY(()) unsigned int loclabel_num;
3698 /* Unique label counter for point-of-call tables. */
3699 static GTY(()) unsigned int poc_label_num;
3701 /* The last file entry emitted by maybe_emit_file(). */
3702 static GTY(()) struct dwarf_file_data * last_emitted_file;
3704 /* Number of internal labels generated by gen_internal_sym(). */
3705 static GTY(()) int label_num;
3707 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3709 /* Instances of generic types for which we need to generate debug
3710 info that describe their generic parameters and arguments. That
3711 generation needs to happen once all types are properly laid out so
3712 we do it at the end of compilation. */
3713 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3715 /* Offset from the "steady-state frame pointer" to the frame base,
3716 within the current function. */
3717 static poly_int64 frame_pointer_fb_offset;
3718 static bool frame_pointer_fb_offset_valid;
3720 static vec<dw_die_ref> base_types;
3722 /* Flags to represent a set of attribute classes for attributes that represent
3723 a scalar value (bounds, pointers, ...). */
3724 enum dw_scalar_form
3726 dw_scalar_form_constant = 0x01,
3727 dw_scalar_form_exprloc = 0x02,
3728 dw_scalar_form_reference = 0x04
3731 /* Forward declarations for functions defined in this file. */
3733 static bool is_pseudo_reg (const_rtx);
3734 static tree type_main_variant (tree);
3735 static bool is_tagged_type (const_tree);
3736 static const char *dwarf_tag_name (unsigned);
3737 static const char *dwarf_attr_name (unsigned);
3738 static const char *dwarf_form_name (unsigned);
3739 static tree decl_ultimate_origin (const_tree);
3740 static tree decl_class_context (tree);
3741 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3742 static inline unsigned int AT_index (dw_attr_node *);
3743 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3744 static inline unsigned AT_flag (dw_attr_node *);
3745 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3746 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3747 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3748 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3749 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3750 unsigned int, unsigned char *);
3751 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3752 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3753 static inline const char *AT_string (dw_attr_node *);
3754 static enum dwarf_form AT_string_form (dw_attr_node *);
3755 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3756 static void add_AT_specification (dw_die_ref, dw_die_ref);
3757 static inline dw_die_ref AT_ref (dw_attr_node *);
3758 static inline int AT_ref_external (dw_attr_node *);
3759 static inline void set_AT_ref_external (dw_attr_node *, int);
3760 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3761 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3762 dw_loc_list_ref);
3763 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3764 static void add_AT_view_list (dw_die_ref, enum dwarf_attribute);
3765 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3766 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3767 static void remove_addr_table_entry (addr_table_entry *);
3768 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3769 static inline rtx AT_addr (dw_attr_node *);
3770 static void add_AT_symview (dw_die_ref, enum dwarf_attribute, const char *);
3771 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3772 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3773 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3774 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3775 unsigned long, bool);
3776 static inline const char *AT_lbl (dw_attr_node *);
3777 static const char *get_AT_low_pc (dw_die_ref);
3778 static bool is_c (void);
3779 static bool is_cxx (void);
3780 static bool is_cxx (const_tree);
3781 static bool is_fortran (void);
3782 static bool is_ada (void);
3783 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3784 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3785 static void add_child_die (dw_die_ref, dw_die_ref);
3786 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3787 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3788 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3789 static void equate_type_number_to_die (tree, dw_die_ref);
3790 static var_loc_list *lookup_decl_loc (const_tree);
3791 static void equate_decl_number_to_die (tree, dw_die_ref);
3792 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *, var_loc_view);
3793 static void print_spaces (FILE *);
3794 static void print_die (dw_die_ref, FILE *);
3795 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3796 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3797 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3798 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3799 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3800 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3801 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3802 struct md5_ctx *, int *);
3803 struct checksum_attributes;
3804 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3805 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3806 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3807 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3808 static bool same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3809 static bool same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3810 static bool same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3811 static bool same_die_p (dw_die_ref, dw_die_ref, int *);
3812 static bool is_type_die (dw_die_ref);
3813 static inline bool is_template_instantiation (dw_die_ref);
3814 static bool is_declaration_die (dw_die_ref);
3815 static bool should_move_die_to_comdat (dw_die_ref);
3816 static dw_die_ref clone_as_declaration (dw_die_ref);
3817 static dw_die_ref clone_die (dw_die_ref);
3818 static dw_die_ref clone_tree (dw_die_ref);
3819 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3820 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3821 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3822 static dw_die_ref generate_skeleton (dw_die_ref);
3823 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3824 dw_die_ref,
3825 dw_die_ref);
3826 static void break_out_comdat_types (dw_die_ref);
3827 static void copy_decls_for_unworthy_types (dw_die_ref);
3829 static void add_sibling_attributes (dw_die_ref);
3830 static void output_location_lists (dw_die_ref);
3831 static int constant_size (unsigned HOST_WIDE_INT);
3832 static unsigned long size_of_die (dw_die_ref);
3833 static void calc_die_sizes (dw_die_ref);
3834 static void calc_base_type_die_sizes (void);
3835 static void mark_dies (dw_die_ref);
3836 static void unmark_dies (dw_die_ref);
3837 static void unmark_all_dies (dw_die_ref);
3838 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3839 static unsigned long size_of_aranges (void);
3840 static enum dwarf_form value_format (dw_attr_node *);
3841 static void output_value_format (dw_attr_node *);
3842 static void output_abbrev_section (void);
3843 static void output_die_abbrevs (unsigned long, dw_die_ref);
3844 static void output_die (dw_die_ref);
3845 static void output_compilation_unit_header (enum dwarf_unit_type);
3846 static void output_comp_unit (dw_die_ref, int, const unsigned char *);
3847 static void output_comdat_type_unit (comdat_type_node *, bool);
3848 static const char *dwarf2_name (tree, int);
3849 static void add_pubname (tree, dw_die_ref);
3850 static void add_enumerator_pubname (const char *, dw_die_ref);
3851 static void add_pubname_string (const char *, dw_die_ref);
3852 static void add_pubtype (tree, dw_die_ref);
3853 static void output_pubnames (vec<pubname_entry, va_gc> *);
3854 static void output_aranges (void);
3855 static unsigned int add_ranges (const_tree, bool = false);
3856 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3857 bool *, bool);
3858 static void output_ranges (void);
3859 static dw_line_info_table *new_line_info_table (void);
3860 static void output_line_info (bool);
3861 static void output_file_names (void);
3862 static bool is_base_type (tree);
3863 static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3864 static int decl_quals (const_tree);
3865 static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3866 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3867 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3868 static unsigned int debugger_reg_number (const_rtx);
3869 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3870 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3871 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3872 enum var_init_status);
3873 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3874 enum var_init_status);
3875 static dw_loc_descr_ref based_loc_descr (rtx, poly_int64,
3876 enum var_init_status);
3877 static bool is_based_loc (const_rtx);
3878 static bool resolve_one_addr (rtx *);
3879 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3880 enum var_init_status);
3881 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3882 enum var_init_status);
3883 struct loc_descr_context;
3884 static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3885 static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3886 static dw_loc_list_ref loc_list_from_tree (tree, int,
3887 struct loc_descr_context *);
3888 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3889 struct loc_descr_context *);
3890 static tree field_type (const_tree);
3891 static unsigned int simple_type_align_in_bits (const_tree);
3892 static unsigned int simple_decl_align_in_bits (const_tree);
3893 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3894 struct vlr_context;
3895 static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3896 HOST_WIDE_INT *);
3897 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3898 dw_loc_list_ref);
3899 static void add_data_member_location_attribute (dw_die_ref, tree,
3900 struct vlr_context *);
3901 static bool add_const_value_attribute (dw_die_ref, machine_mode, rtx);
3902 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3903 static void insert_wide_int (const wide_int &, unsigned char *, int);
3904 static unsigned insert_float (const_rtx, unsigned char *);
3905 static rtx rtl_for_decl_location (tree);
3906 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3907 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3908 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3909 static void add_desc_attribute (dw_die_ref, tree);
3910 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3911 static void add_comp_dir_attribute (dw_die_ref);
3912 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3913 struct loc_descr_context *);
3914 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3915 struct loc_descr_context *);
3916 static void add_subscript_info (dw_die_ref, tree, bool);
3917 static void add_byte_size_attribute (dw_die_ref, tree);
3918 static void add_alignment_attribute (dw_die_ref, tree);
3919 static void add_bit_offset_attribute (dw_die_ref, tree);
3920 static void add_bit_size_attribute (dw_die_ref, tree);
3921 static void add_prototyped_attribute (dw_die_ref, tree);
3922 static void add_abstract_origin_attribute (dw_die_ref, tree);
3923 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3924 static void add_src_coords_attributes (dw_die_ref, tree);
3925 static void add_name_and_src_coords_attributes (dw_die_ref, tree, bool = false);
3926 static void add_discr_value (dw_die_ref, dw_discr_value *);
3927 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3928 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3929 static dw_die_ref scope_die_for (tree, dw_die_ref);
3930 static inline bool local_scope_p (dw_die_ref);
3931 static inline bool class_scope_p (dw_die_ref);
3932 static inline bool class_or_namespace_scope_p (dw_die_ref);
3933 static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3934 static void add_calling_convention_attribute (dw_die_ref, tree);
3935 static const char *type_tag (const_tree);
3936 static tree member_declared_type (const_tree);
3937 #if 0
3938 static const char *decl_start_label (tree);
3939 #endif
3940 static void gen_array_type_die (tree, dw_die_ref);
3941 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3942 #if 0
3943 static void gen_entry_point_die (tree, dw_die_ref);
3944 #endif
3945 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3946 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3947 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3948 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3949 static void gen_formal_types_die (tree, dw_die_ref);
3950 static void gen_subprogram_die (tree, dw_die_ref);
3951 static void gen_variable_die (tree, tree, dw_die_ref);
3952 static void gen_const_die (tree, dw_die_ref);
3953 static void gen_label_die (tree, dw_die_ref);
3954 static void gen_lexical_block_die (tree, dw_die_ref);
3955 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3956 static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3957 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3958 static dw_die_ref gen_compile_unit_die (const char *);
3959 static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3960 static void gen_member_die (tree, dw_die_ref);
3961 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3962 enum debug_info_usage);
3963 static void gen_subroutine_type_die (tree, dw_die_ref);
3964 static void gen_typedef_die (tree, dw_die_ref);
3965 static void gen_type_die (tree, dw_die_ref);
3966 static void gen_block_die (tree, dw_die_ref);
3967 static void decls_for_scope (tree, dw_die_ref, bool = true);
3968 static bool is_naming_typedef_decl (const_tree);
3969 static inline dw_die_ref get_context_die (tree);
3970 static void gen_namespace_die (tree, dw_die_ref);
3971 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3972 static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3973 static dw_die_ref force_decl_die (tree);
3974 static dw_die_ref force_type_die (tree);
3975 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3976 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3977 static struct dwarf_file_data * lookup_filename (const char *);
3978 static void retry_incomplete_types (void);
3979 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3980 static void gen_generic_params_dies (tree);
3981 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3982 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
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 /* Add an unsigned wide integer attribute value to a DIE. */
4599 static inline void
4600 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
4601 const wide_int& w)
4603 dw_attr_node attr;
4605 attr.dw_attr = attr_kind;
4606 attr.dw_attr_val.val_class = dw_val_class_wide_int;
4607 attr.dw_attr_val.val_entry = NULL;
4608 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
4609 *attr.dw_attr_val.v.val_wide = w;
4610 add_dwarf_attr (die, &attr);
4613 /* Add an unsigned double integer attribute value to a DIE. */
4615 static inline void
4616 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4617 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4619 dw_attr_node attr;
4621 attr.dw_attr = attr_kind;
4622 attr.dw_attr_val.val_class = dw_val_class_const_double;
4623 attr.dw_attr_val.val_entry = NULL;
4624 attr.dw_attr_val.v.val_double.high = high;
4625 attr.dw_attr_val.v.val_double.low = low;
4626 add_dwarf_attr (die, &attr);
4629 /* Add a floating point attribute value to a DIE and return it. */
4631 static inline void
4632 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4633 unsigned int length, unsigned int elt_size, unsigned char *array)
4635 dw_attr_node attr;
4637 attr.dw_attr = attr_kind;
4638 attr.dw_attr_val.val_class = dw_val_class_vec;
4639 attr.dw_attr_val.val_entry = NULL;
4640 attr.dw_attr_val.v.val_vec.length = length;
4641 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4642 attr.dw_attr_val.v.val_vec.array = array;
4643 add_dwarf_attr (die, &attr);
4646 /* Add an 8-byte data attribute value to a DIE. */
4648 static inline void
4649 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4650 unsigned char data8[8])
4652 dw_attr_node attr;
4654 attr.dw_attr = attr_kind;
4655 attr.dw_attr_val.val_class = dw_val_class_data8;
4656 attr.dw_attr_val.val_entry = NULL;
4657 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4658 add_dwarf_attr (die, &attr);
4661 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
4662 dwarf_split_debug_info, address attributes in dies destined for the
4663 final executable have force_direct set to avoid using indexed
4664 references. */
4666 static inline void
4667 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4668 bool force_direct)
4670 dw_attr_node attr;
4671 char * lbl_id;
4673 lbl_id = xstrdup (lbl_low);
4674 attr.dw_attr = DW_AT_low_pc;
4675 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4676 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4677 if (dwarf_split_debug_info && !force_direct)
4678 attr.dw_attr_val.val_entry
4679 = add_addr_table_entry (lbl_id, ate_kind_label);
4680 else
4681 attr.dw_attr_val.val_entry = NULL;
4682 add_dwarf_attr (die, &attr);
4684 attr.dw_attr = DW_AT_high_pc;
4685 if (dwarf_version < 4)
4686 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4687 else
4688 attr.dw_attr_val.val_class = dw_val_class_high_pc;
4689 lbl_id = xstrdup (lbl_high);
4690 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4691 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4692 && dwarf_split_debug_info && !force_direct)
4693 attr.dw_attr_val.val_entry
4694 = add_addr_table_entry (lbl_id, ate_kind_label);
4695 else
4696 attr.dw_attr_val.val_entry = NULL;
4697 add_dwarf_attr (die, &attr);
4700 /* Hash and equality functions for debug_str_hash. */
4702 hashval_t
4703 indirect_string_hasher::hash (indirect_string_node *x)
4705 return htab_hash_string (x->str);
4708 bool
4709 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4711 return strcmp (x1->str, x2) == 0;
4714 /* Add STR to the given string hash table. */
4716 static struct indirect_string_node *
4717 find_AT_string_in_table (const char *str,
4718 hash_table<indirect_string_hasher> *table,
4719 enum insert_option insert = INSERT)
4721 struct indirect_string_node *node;
4723 indirect_string_node **slot
4724 = table->find_slot_with_hash (str, htab_hash_string (str), insert);
4725 if (*slot == NULL)
4727 node = ggc_cleared_alloc<indirect_string_node> ();
4728 node->str = ggc_strdup (str);
4729 *slot = node;
4731 else
4732 node = *slot;
4734 node->refcount++;
4735 return node;
4738 /* Add STR to the indirect string hash table. */
4740 static struct indirect_string_node *
4741 find_AT_string (const char *str, enum insert_option insert = INSERT)
4743 if (! debug_str_hash)
4744 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4746 return find_AT_string_in_table (str, debug_str_hash, insert);
4749 /* Add a string attribute value to a DIE. */
4751 static inline void
4752 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4754 dw_attr_node attr;
4755 struct indirect_string_node *node;
4757 node = find_AT_string (str);
4759 attr.dw_attr = attr_kind;
4760 attr.dw_attr_val.val_class = dw_val_class_str;
4761 attr.dw_attr_val.val_entry = NULL;
4762 attr.dw_attr_val.v.val_str = node;
4763 add_dwarf_attr (die, &attr);
4766 static inline const char *
4767 AT_string (dw_attr_node *a)
4769 gcc_assert (a && AT_class (a) == dw_val_class_str);
4770 return a->dw_attr_val.v.val_str->str;
4773 /* Call this function directly to bypass AT_string_form's logic to put
4774 the string inline in the die. */
4776 static void
4777 set_indirect_string (struct indirect_string_node *node)
4779 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4780 /* Already indirect is a no op. */
4781 if (node->form == DW_FORM_strp
4782 || node->form == DW_FORM_line_strp
4783 || node->form == dwarf_FORM (DW_FORM_strx))
4785 gcc_assert (node->label);
4786 return;
4788 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4789 ++dw2_string_counter;
4790 node->label = xstrdup (label);
4792 if (!dwarf_split_debug_info)
4794 node->form = DW_FORM_strp;
4795 node->index = NOT_INDEXED;
4797 else
4799 node->form = dwarf_FORM (DW_FORM_strx);
4800 node->index = NO_INDEX_ASSIGNED;
4804 /* A helper function for dwarf2out_finish, called to reset indirect
4805 string decisions done for early LTO dwarf output before fat object
4806 dwarf output. */
4809 reset_indirect_string (indirect_string_node **h, void *)
4811 struct indirect_string_node *node = *h;
4812 if (node->form == DW_FORM_strp
4813 || node->form == DW_FORM_line_strp
4814 || node->form == dwarf_FORM (DW_FORM_strx))
4816 free (node->label);
4817 node->label = NULL;
4818 node->form = (dwarf_form) 0;
4819 node->index = 0;
4821 return 1;
4824 /* Add a string representing a file or filepath attribute value to a DIE. */
4826 static inline void
4827 add_filepath_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
4828 const char *str)
4830 if (! asm_outputs_debug_line_str ())
4831 add_AT_string (die, attr_kind, str);
4832 else
4834 dw_attr_node attr;
4835 struct indirect_string_node *node;
4837 if (!debug_line_str_hash)
4838 debug_line_str_hash
4839 = hash_table<indirect_string_hasher>::create_ggc (10);
4841 node = find_AT_string_in_table (str, debug_line_str_hash);
4842 set_indirect_string (node);
4843 node->form = DW_FORM_line_strp;
4845 attr.dw_attr = attr_kind;
4846 attr.dw_attr_val.val_class = dw_val_class_str;
4847 attr.dw_attr_val.val_entry = NULL;
4848 attr.dw_attr_val.v.val_str = node;
4849 add_dwarf_attr (die, &attr);
4853 /* Find out whether a string should be output inline in DIE
4854 or out-of-line in .debug_str section. */
4856 static enum dwarf_form
4857 find_string_form (struct indirect_string_node *node)
4859 unsigned int len;
4861 if (node->form)
4862 return node->form;
4864 len = strlen (node->str) + 1;
4866 /* If the string is shorter or equal to the size of the reference, it is
4867 always better to put it inline. */
4868 if (len <= (unsigned) dwarf_offset_size || node->refcount == 0)
4869 return node->form = DW_FORM_string;
4871 /* If we cannot expect the linker to merge strings in .debug_str
4872 section, only put it into .debug_str if it is worth even in this
4873 single module. */
4874 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4875 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4876 && (len - dwarf_offset_size) * node->refcount <= len))
4877 return node->form = DW_FORM_string;
4879 set_indirect_string (node);
4881 return node->form;
4884 /* Find out whether the string referenced from the attribute should be
4885 output inline in DIE or out-of-line in .debug_str section. */
4887 static enum dwarf_form
4888 AT_string_form (dw_attr_node *a)
4890 gcc_assert (a && AT_class (a) == dw_val_class_str);
4891 return find_string_form (a->dw_attr_val.v.val_str);
4894 /* Add a DIE reference attribute value to a DIE. */
4896 static inline void
4897 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4899 dw_attr_node attr;
4900 gcc_checking_assert (targ_die != NULL);
4902 /* With LTO we can end up trying to reference something we didn't create
4903 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4904 if (targ_die == NULL)
4905 return;
4907 attr.dw_attr = attr_kind;
4908 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4909 attr.dw_attr_val.val_entry = NULL;
4910 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4911 attr.dw_attr_val.v.val_die_ref.external = 0;
4912 add_dwarf_attr (die, &attr);
4915 /* Change DIE reference REF to point to NEW_DIE instead. */
4917 static inline void
4918 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4920 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4921 ref->dw_attr_val.v.val_die_ref.die = new_die;
4922 ref->dw_attr_val.v.val_die_ref.external = 0;
4925 /* Add an AT_specification attribute to a DIE, and also make the back
4926 pointer from the specification to the definition. */
4928 static inline void
4929 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4931 add_AT_die_ref (die, DW_AT_specification, targ_die);
4932 gcc_assert (!targ_die->die_definition);
4933 targ_die->die_definition = die;
4936 static inline dw_die_ref
4937 AT_ref (dw_attr_node *a)
4939 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4940 return a->dw_attr_val.v.val_die_ref.die;
4943 static inline int
4944 AT_ref_external (dw_attr_node *a)
4946 if (a && AT_class (a) == dw_val_class_die_ref)
4947 return a->dw_attr_val.v.val_die_ref.external;
4949 return 0;
4952 static inline void
4953 set_AT_ref_external (dw_attr_node *a, int i)
4955 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4956 a->dw_attr_val.v.val_die_ref.external = i;
4959 /* Add a location description attribute value to a DIE. */
4961 static inline void
4962 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4964 dw_attr_node attr;
4966 attr.dw_attr = attr_kind;
4967 attr.dw_attr_val.val_class = dw_val_class_loc;
4968 attr.dw_attr_val.val_entry = NULL;
4969 attr.dw_attr_val.v.val_loc = loc;
4970 add_dwarf_attr (die, &attr);
4973 dw_loc_descr_ref
4974 AT_loc (dw_attr_node *a)
4976 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4977 return a->dw_attr_val.v.val_loc;
4980 static inline void
4981 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4983 dw_attr_node attr;
4985 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4986 return;
4988 attr.dw_attr = attr_kind;
4989 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4990 attr.dw_attr_val.val_entry = NULL;
4991 attr.dw_attr_val.v.val_loc_list = loc_list;
4992 add_dwarf_attr (die, &attr);
4993 have_location_lists = true;
4996 static inline dw_loc_list_ref
4997 AT_loc_list (dw_attr_node *a)
4999 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
5000 return a->dw_attr_val.v.val_loc_list;
5003 /* Add a view list attribute to DIE. It must have a DW_AT_location
5004 attribute, because the view list complements the location list. */
5006 static inline void
5007 add_AT_view_list (dw_die_ref die, enum dwarf_attribute attr_kind)
5009 dw_attr_node attr;
5011 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
5012 return;
5014 attr.dw_attr = attr_kind;
5015 attr.dw_attr_val.val_class = dw_val_class_view_list;
5016 attr.dw_attr_val.val_entry = NULL;
5017 attr.dw_attr_val.v.val_view_list = die;
5018 add_dwarf_attr (die, &attr);
5019 gcc_checking_assert (get_AT (die, DW_AT_location));
5020 gcc_assert (have_location_lists);
5023 /* Return a pointer to the location list referenced by the attribute.
5024 If the named attribute is a view list, look up the corresponding
5025 DW_AT_location attribute and return its location list. */
5027 static inline dw_loc_list_ref *
5028 AT_loc_list_ptr (dw_attr_node *a)
5030 gcc_assert (a);
5031 switch (AT_class (a))
5033 case dw_val_class_loc_list:
5034 return &a->dw_attr_val.v.val_loc_list;
5035 case dw_val_class_view_list:
5037 dw_attr_node *l;
5038 l = get_AT (a->dw_attr_val.v.val_view_list, DW_AT_location);
5039 if (!l)
5040 return NULL;
5041 gcc_checking_assert (l + 1 == a);
5042 return AT_loc_list_ptr (l);
5044 default:
5045 gcc_unreachable ();
5049 /* Return the location attribute value associated with a view list
5050 attribute value. */
5052 static inline dw_val_node *
5053 view_list_to_loc_list_val_node (dw_val_node *val)
5055 gcc_assert (val->val_class == dw_val_class_view_list);
5056 dw_attr_node *loc = get_AT (val->v.val_view_list, DW_AT_location);
5057 if (!loc)
5058 return NULL;
5059 gcc_checking_assert (&(loc + 1)->dw_attr_val == val);
5060 gcc_assert (AT_class (loc) == dw_val_class_loc_list);
5061 return &loc->dw_attr_val;
5064 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
5066 static hashval_t hash (addr_table_entry *);
5067 static bool equal (addr_table_entry *, addr_table_entry *);
5070 /* Table of entries into the .debug_addr section. */
5072 static GTY (()) hash_table<addr_hasher> *addr_index_table;
5074 /* Hash an address_table_entry. */
5076 hashval_t
5077 addr_hasher::hash (addr_table_entry *a)
5079 inchash::hash hstate;
5080 switch (a->kind)
5082 case ate_kind_rtx:
5083 hstate.add_int (0);
5084 break;
5085 case ate_kind_rtx_dtprel:
5086 hstate.add_int (1);
5087 break;
5088 case ate_kind_label:
5089 return htab_hash_string (a->addr.label);
5090 default:
5091 gcc_unreachable ();
5093 inchash::add_rtx (a->addr.rtl, hstate);
5094 return hstate.end ();
5097 /* Determine equality for two address_table_entries. */
5099 bool
5100 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
5102 if (a1->kind != a2->kind)
5103 return false;
5104 switch (a1->kind)
5106 case ate_kind_rtx:
5107 case ate_kind_rtx_dtprel:
5108 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
5109 case ate_kind_label:
5110 return strcmp (a1->addr.label, a2->addr.label) == 0;
5111 default:
5112 gcc_unreachable ();
5116 /* Initialize an addr_table_entry. */
5118 void
5119 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
5121 e->kind = kind;
5122 switch (kind)
5124 case ate_kind_rtx:
5125 case ate_kind_rtx_dtprel:
5126 e->addr.rtl = (rtx) addr;
5127 break;
5128 case ate_kind_label:
5129 e->addr.label = (char *) addr;
5130 break;
5132 e->refcount = 0;
5133 e->index = NO_INDEX_ASSIGNED;
5136 /* Add attr to the address table entry to the table. Defer setting an
5137 index until output time. */
5139 static addr_table_entry *
5140 add_addr_table_entry (void *addr, enum ate_kind kind)
5142 addr_table_entry *node;
5143 addr_table_entry finder;
5145 gcc_assert (dwarf_split_debug_info);
5146 if (! addr_index_table)
5147 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
5148 init_addr_table_entry (&finder, kind, addr);
5149 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
5151 if (*slot == HTAB_EMPTY_ENTRY)
5153 node = ggc_cleared_alloc<addr_table_entry> ();
5154 init_addr_table_entry (node, kind, addr);
5155 *slot = node;
5157 else
5158 node = *slot;
5160 node->refcount++;
5161 return node;
5164 /* Remove an entry from the addr table by decrementing its refcount.
5165 Strictly, decrementing the refcount would be enough, but the
5166 assertion that the entry is actually in the table has found
5167 bugs. */
5169 static void
5170 remove_addr_table_entry (addr_table_entry *entry)
5172 gcc_assert (dwarf_split_debug_info && addr_index_table);
5173 /* After an index is assigned, the table is frozen. */
5174 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
5175 entry->refcount--;
5178 /* Given a location list, remove all addresses it refers to from the
5179 address_table. */
5181 static void
5182 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
5184 for (; descr; descr = descr->dw_loc_next)
5185 if (descr->dw_loc_oprnd1.val_entry != NULL)
5187 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
5188 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
5192 /* A helper function for dwarf2out_finish called through
5193 htab_traverse. Assign an addr_table_entry its index. All entries
5194 must be collected into the table when this function is called,
5195 because the indexing code relies on htab_traverse to traverse nodes
5196 in the same order for each run. */
5199 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
5201 addr_table_entry *node = *h;
5203 /* Don't index unreferenced nodes. */
5204 if (node->refcount == 0)
5205 return 1;
5207 gcc_assert (node->index == NO_INDEX_ASSIGNED);
5208 node->index = *index;
5209 *index += 1;
5211 return 1;
5214 /* Return the tag of a given DIE. */
5216 enum dwarf_tag
5217 dw_get_die_tag (dw_die_ref die)
5219 return die->die_tag;
5222 /* Return a reference to the children list of a given DIE. */
5224 dw_die_ref
5225 dw_get_die_child (dw_die_ref die)
5227 return die->die_child;
5230 /* Return a reference to the sibling of a given DIE. */
5232 dw_die_ref
5233 dw_get_die_sib (dw_die_ref die)
5235 return die->die_sib;
5238 /* Add an address constant attribute value to a DIE. When using
5239 dwarf_split_debug_info, address attributes in dies destined for the
5240 final executable should be direct references--setting the parameter
5241 force_direct ensures this behavior. */
5243 static inline void
5244 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
5245 bool force_direct)
5247 dw_attr_node attr;
5249 attr.dw_attr = attr_kind;
5250 attr.dw_attr_val.val_class = dw_val_class_addr;
5251 attr.dw_attr_val.v.val_addr = addr;
5252 if (dwarf_split_debug_info && !force_direct)
5253 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
5254 else
5255 attr.dw_attr_val.val_entry = NULL;
5256 add_dwarf_attr (die, &attr);
5259 /* Get the RTX from to an address DIE attribute. */
5261 static inline rtx
5262 AT_addr (dw_attr_node *a)
5264 gcc_assert (a && AT_class (a) == dw_val_class_addr);
5265 return a->dw_attr_val.v.val_addr;
5268 /* Add a file attribute value to a DIE. */
5270 static inline void
5271 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
5272 struct dwarf_file_data *fd)
5274 dw_attr_node attr;
5276 attr.dw_attr = attr_kind;
5277 attr.dw_attr_val.val_class = dw_val_class_file;
5278 attr.dw_attr_val.val_entry = NULL;
5279 attr.dw_attr_val.v.val_file = fd;
5280 add_dwarf_attr (die, &attr);
5283 /* Get the dwarf_file_data from a file DIE attribute. */
5285 static inline struct dwarf_file_data *
5286 AT_file (dw_attr_node *a)
5288 gcc_assert (a && (AT_class (a) == dw_val_class_file
5289 || AT_class (a) == dw_val_class_file_implicit));
5290 return a->dw_attr_val.v.val_file;
5293 #if VMS_DEBUGGING_INFO
5294 /* Add a vms delta attribute value to a DIE. */
5296 static inline void
5297 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
5298 const char *lbl1, const char *lbl2)
5300 dw_attr_node attr;
5302 attr.dw_attr = attr_kind;
5303 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
5304 attr.dw_attr_val.val_entry = NULL;
5305 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
5306 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
5307 add_dwarf_attr (die, &attr);
5309 #endif
5311 /* Add a symbolic view identifier attribute value to a DIE. */
5313 static inline void
5314 add_AT_symview (dw_die_ref die, enum dwarf_attribute attr_kind,
5315 const char *view_label)
5317 dw_attr_node attr;
5319 attr.dw_attr = attr_kind;
5320 attr.dw_attr_val.val_class = dw_val_class_symview;
5321 attr.dw_attr_val.val_entry = NULL;
5322 attr.dw_attr_val.v.val_symbolic_view = xstrdup (view_label);
5323 add_dwarf_attr (die, &attr);
5326 /* Add a label identifier attribute value to a DIE. */
5328 static inline void
5329 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
5330 const char *lbl_id)
5332 dw_attr_node attr;
5334 attr.dw_attr = attr_kind;
5335 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
5336 attr.dw_attr_val.val_entry = NULL;
5337 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5338 if (dwarf_split_debug_info)
5339 attr.dw_attr_val.val_entry
5340 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
5341 ate_kind_label);
5342 add_dwarf_attr (die, &attr);
5345 /* Add a section offset attribute value to a DIE, an offset into the
5346 debug_line section. */
5348 static inline void
5349 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5350 const char *label)
5352 dw_attr_node attr;
5354 attr.dw_attr = attr_kind;
5355 attr.dw_attr_val.val_class = dw_val_class_lineptr;
5356 attr.dw_attr_val.val_entry = NULL;
5357 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5358 add_dwarf_attr (die, &attr);
5361 /* Add a section offset attribute value to a DIE, an offset into the
5362 debug_macinfo section. */
5364 static inline void
5365 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5366 const char *label)
5368 dw_attr_node attr;
5370 attr.dw_attr = attr_kind;
5371 attr.dw_attr_val.val_class = dw_val_class_macptr;
5372 attr.dw_attr_val.val_entry = NULL;
5373 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5374 add_dwarf_attr (die, &attr);
5377 /* Add a range_list attribute value to a DIE. When using
5378 dwarf_split_debug_info, address attributes in dies destined for the
5379 final executable should be direct references--setting the parameter
5380 force_direct ensures this behavior. */
5382 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
5383 #define RELOCATED_OFFSET (NULL)
5385 static void
5386 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5387 long unsigned int offset, bool force_direct)
5389 dw_attr_node attr;
5391 attr.dw_attr = attr_kind;
5392 attr.dw_attr_val.val_class = dw_val_class_range_list;
5393 /* For the range_list attribute, use val_entry to store whether the
5394 offset should follow split-debug-info or normal semantics. This
5395 value is read in output_range_list_offset. */
5396 if (dwarf_split_debug_info && !force_direct)
5397 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
5398 else
5399 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
5400 attr.dw_attr_val.v.val_offset = offset;
5401 add_dwarf_attr (die, &attr);
5404 /* Return the start label of a delta attribute. */
5406 static inline const char *
5407 AT_vms_delta1 (dw_attr_node *a)
5409 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
5410 return a->dw_attr_val.v.val_vms_delta.lbl1;
5413 /* Return the end label of a delta attribute. */
5415 static inline const char *
5416 AT_vms_delta2 (dw_attr_node *a)
5418 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
5419 return a->dw_attr_val.v.val_vms_delta.lbl2;
5422 static inline const char *
5423 AT_lbl (dw_attr_node *a)
5425 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5426 || AT_class (a) == dw_val_class_lineptr
5427 || AT_class (a) == dw_val_class_macptr
5428 || AT_class (a) == dw_val_class_loclistsptr
5429 || AT_class (a) == dw_val_class_high_pc));
5430 return a->dw_attr_val.v.val_lbl_id;
5433 /* Get the attribute of type attr_kind. */
5435 dw_attr_node *
5436 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5438 dw_attr_node *a;
5439 unsigned ix;
5440 dw_die_ref spec = NULL;
5442 if (! die)
5443 return NULL;
5445 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5446 if (a->dw_attr == attr_kind)
5447 return a;
5448 else if (a->dw_attr == DW_AT_specification
5449 || a->dw_attr == DW_AT_abstract_origin)
5450 spec = AT_ref (a);
5452 if (spec)
5453 return get_AT (spec, attr_kind);
5455 return NULL;
5458 /* Returns the parent of the declaration of DIE. */
5460 static dw_die_ref
5461 get_die_parent (dw_die_ref die)
5463 dw_die_ref t;
5465 if (!die)
5466 return NULL;
5468 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
5469 || (t = get_AT_ref (die, DW_AT_specification)))
5470 die = t;
5472 return die->die_parent;
5475 /* Return the "low pc" attribute value, typically associated with a subprogram
5476 DIE. Return null if the "low pc" attribute is either not present, or if it
5477 cannot be represented as an assembler label identifier. */
5479 static inline const char *
5480 get_AT_low_pc (dw_die_ref die)
5482 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
5484 return a ? AT_lbl (a) : NULL;
5487 /* Return the value of the string attribute designated by ATTR_KIND, or
5488 NULL if it is not present. */
5490 const char *
5491 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5493 dw_attr_node *a = get_AT (die, attr_kind);
5495 return a ? AT_string (a) : NULL;
5498 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5499 if it is not present. */
5502 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5504 dw_attr_node *a = get_AT (die, attr_kind);
5506 return a ? AT_flag (a) : 0;
5509 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5510 if it is not present. */
5512 unsigned
5513 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5515 dw_attr_node *a = get_AT (die, attr_kind);
5517 return a ? AT_unsigned (a) : 0;
5520 dw_die_ref
5521 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5523 dw_attr_node *a = get_AT (die, attr_kind);
5525 return a ? AT_ref (a) : NULL;
5528 struct dwarf_file_data *
5529 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5531 dw_attr_node *a = get_AT (die, attr_kind);
5533 return a ? AT_file (a) : NULL;
5536 /* Return TRUE if the language is C. */
5538 static inline bool
5539 is_c (void)
5541 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5543 return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_C99
5544 || lang == DW_LANG_C11 || lang == DW_LANG_ObjC);
5549 /* Return TRUE if the language is C++. */
5551 static inline bool
5552 is_cxx (void)
5554 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5556 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
5557 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
5560 /* Return TRUE if DECL was created by the C++ frontend. */
5562 static bool
5563 is_cxx (const_tree decl)
5565 if (in_lto_p)
5567 const_tree context = get_ultimate_context (decl);
5568 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5569 return startswith (TRANSLATION_UNIT_LANGUAGE (context), "GNU C++");
5571 return is_cxx ();
5574 /* Return TRUE if the language is Fortran. */
5576 static inline bool
5577 is_fortran (void)
5579 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5581 return (lang == DW_LANG_Fortran77
5582 || lang == DW_LANG_Fortran90
5583 || lang == DW_LANG_Fortran95
5584 || lang == DW_LANG_Fortran03
5585 || lang == DW_LANG_Fortran08);
5588 static inline bool
5589 is_fortran (const_tree decl)
5591 if (in_lto_p)
5593 const_tree context = get_ultimate_context (decl);
5594 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5595 return (strncmp (TRANSLATION_UNIT_LANGUAGE (context),
5596 "GNU Fortran", 11) == 0
5597 || strcmp (TRANSLATION_UNIT_LANGUAGE (context),
5598 "GNU F77") == 0);
5600 return is_fortran ();
5603 /* Return TRUE if the language is Rust.
5604 Note, returns FALSE for dwarf_version < 5 && dwarf_strict. */
5606 static inline bool
5607 is_rust (void)
5609 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5611 return lang == DW_LANG_Rust;
5614 /* Return TRUE if the language is Ada. */
5616 static inline bool
5617 is_ada (void)
5619 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5621 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5624 /* Return TRUE if the language is D. */
5626 static inline bool
5627 is_dlang (void)
5629 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5631 return lang == DW_LANG_D;
5634 /* Remove the specified attribute if present. Return TRUE if removal
5635 was successful. */
5637 static bool
5638 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5640 dw_attr_node *a;
5641 unsigned ix;
5643 if (! die)
5644 return false;
5646 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5647 if (a->dw_attr == attr_kind)
5649 if (AT_class (a) == dw_val_class_str)
5650 if (a->dw_attr_val.v.val_str->refcount)
5651 a->dw_attr_val.v.val_str->refcount--;
5653 /* vec::ordered_remove should help reduce the number of abbrevs
5654 that are needed. */
5655 die->die_attr->ordered_remove (ix);
5656 return true;
5658 return false;
5661 /* Remove CHILD from its parent. PREV must have the property that
5662 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
5664 static void
5665 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5667 gcc_assert (child->die_parent == prev->die_parent);
5668 gcc_assert (prev->die_sib == child);
5669 if (prev == child)
5671 gcc_assert (child->die_parent->die_child == child);
5672 prev = NULL;
5674 else
5675 prev->die_sib = child->die_sib;
5676 if (child->die_parent->die_child == child)
5677 child->die_parent->die_child = prev;
5678 child->die_sib = NULL;
5681 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
5682 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
5684 static void
5685 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
5687 dw_die_ref parent = old_child->die_parent;
5689 gcc_assert (parent == prev->die_parent);
5690 gcc_assert (prev->die_sib == old_child);
5692 new_child->die_parent = parent;
5693 if (prev == old_child)
5695 gcc_assert (parent->die_child == old_child);
5696 new_child->die_sib = new_child;
5698 else
5700 prev->die_sib = new_child;
5701 new_child->die_sib = old_child->die_sib;
5703 if (old_child->die_parent->die_child == old_child)
5704 old_child->die_parent->die_child = new_child;
5705 old_child->die_sib = NULL;
5708 /* Move all children from OLD_PARENT to NEW_PARENT. */
5710 static void
5711 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
5713 dw_die_ref c;
5714 new_parent->die_child = old_parent->die_child;
5715 old_parent->die_child = NULL;
5716 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
5719 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
5720 matches TAG. */
5722 static void
5723 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5725 dw_die_ref c;
5727 c = die->die_child;
5728 if (c) do {
5729 dw_die_ref prev = c;
5730 c = c->die_sib;
5731 while (c->die_tag == tag)
5733 remove_child_with_prev (c, prev);
5734 c->die_parent = NULL;
5735 /* Might have removed every child. */
5736 if (die->die_child == NULL)
5737 return;
5738 c = prev->die_sib;
5740 } while (c != die->die_child);
5743 /* Add a CHILD_DIE as the last child of DIE. */
5745 static void
5746 add_child_die (dw_die_ref die, dw_die_ref child_die)
5748 /* FIXME this should probably be an assert. */
5749 if (! die || ! child_die)
5750 return;
5751 gcc_assert (die != child_die);
5753 child_die->die_parent = die;
5754 if (die->die_child)
5756 child_die->die_sib = die->die_child->die_sib;
5757 die->die_child->die_sib = child_die;
5759 else
5760 child_die->die_sib = child_die;
5761 die->die_child = child_die;
5764 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE. */
5766 static void
5767 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
5768 dw_die_ref after_die)
5770 gcc_assert (die
5771 && child_die
5772 && after_die
5773 && die->die_child
5774 && die != child_die);
5776 child_die->die_parent = die;
5777 child_die->die_sib = after_die->die_sib;
5778 after_die->die_sib = child_die;
5779 if (die->die_child == after_die)
5780 die->die_child = child_die;
5783 /* Unassociate CHILD from its parent, and make its parent be
5784 NEW_PARENT. */
5786 static void
5787 reparent_child (dw_die_ref child, dw_die_ref new_parent)
5789 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
5790 if (p->die_sib == child)
5792 remove_child_with_prev (child, p);
5793 break;
5795 add_child_die (new_parent, child);
5798 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5799 is the specification, to the end of PARENT's list of children.
5800 This is done by removing and re-adding it. */
5802 static void
5803 splice_child_die (dw_die_ref parent, dw_die_ref child)
5805 /* We want the declaration DIE from inside the class, not the
5806 specification DIE at toplevel. */
5807 if (child->die_parent != parent)
5809 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5811 if (tmp)
5812 child = tmp;
5815 gcc_assert (child->die_parent == parent
5816 || (child->die_parent
5817 == get_AT_ref (parent, DW_AT_specification)));
5819 reparent_child (child, parent);
5822 /* Create and return a new die with TAG_VALUE as tag. */
5824 dw_die_ref
5825 new_die_raw (enum dwarf_tag tag_value)
5827 dw_die_ref die = ggc_cleared_alloc<die_node> ();
5828 die->die_tag = tag_value;
5829 return die;
5832 /* Create and return a new die with a parent of PARENT_DIE. If
5833 PARENT_DIE is NULL, the new DIE is placed in limbo and an
5834 associated tree T must be supplied to determine parenthood
5835 later. */
5837 static inline dw_die_ref
5838 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5840 dw_die_ref die = new_die_raw (tag_value);
5842 if (parent_die != NULL)
5843 add_child_die (parent_die, die);
5844 else
5846 limbo_die_node *limbo_node;
5848 /* No DIEs created after early dwarf should end up in limbo,
5849 because the limbo list should not persist past LTO
5850 streaming. */
5851 if (tag_value != DW_TAG_compile_unit
5852 /* These are allowed because they're generated while
5853 breaking out COMDAT units late. */
5854 && tag_value != DW_TAG_type_unit
5855 && tag_value != DW_TAG_skeleton_unit
5856 && !early_dwarf
5857 /* Allow nested functions to live in limbo because they will
5858 only temporarily live there, as decls_for_scope will fix
5859 them up. */
5860 && (TREE_CODE (t) != FUNCTION_DECL
5861 || !decl_function_context (t))
5862 /* Same as nested functions above but for types. Types that
5863 are local to a function will be fixed in
5864 decls_for_scope. */
5865 && (!RECORD_OR_UNION_TYPE_P (t)
5866 || !TYPE_CONTEXT (t)
5867 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5868 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5869 especially in the ltrans stage, but once we implement LTO
5870 dwarf streaming, we should remove this exception. */
5871 && !in_lto_p)
5873 fprintf (stderr, "symbol ended up in limbo too late:");
5874 debug_generic_stmt (t);
5875 gcc_unreachable ();
5878 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5879 limbo_node->die = die;
5880 limbo_node->created_for = t;
5881 limbo_node->next = limbo_die_list;
5882 limbo_die_list = limbo_node;
5885 return die;
5888 /* Return the DIE associated with the given type specifier. */
5890 dw_die_ref
5891 lookup_type_die (tree type)
5893 dw_die_ref die = TYPE_SYMTAB_DIE (type);
5894 if (die && die->removed)
5896 TYPE_SYMTAB_DIE (type) = NULL;
5897 TREE_ASM_WRITTEN (type) = 0;
5898 return NULL;
5900 return die;
5903 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5904 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5905 anonymous type instead the one of the naming typedef. */
5907 static inline dw_die_ref
5908 strip_naming_typedef (tree type, dw_die_ref type_die)
5910 if (type
5911 && TREE_CODE (type) == RECORD_TYPE
5912 && type_die
5913 && type_die->die_tag == DW_TAG_typedef
5914 && is_naming_typedef_decl (TYPE_NAME (type)))
5915 type_die = get_AT_ref (type_die, DW_AT_type);
5916 return type_die;
5919 /* Like lookup_type_die, but if type is an anonymous type named by a
5920 typedef[1], return the DIE of the anonymous type instead the one of
5921 the naming typedef. This is because in gen_typedef_die, we did
5922 equate the anonymous struct named by the typedef with the DIE of
5923 the naming typedef. So by default, lookup_type_die on an anonymous
5924 struct yields the DIE of the naming typedef.
5926 [1]: Read the comment of is_naming_typedef_decl to learn about what
5927 a naming typedef is. */
5929 static inline dw_die_ref
5930 lookup_type_die_strip_naming_typedef (tree type)
5932 dw_die_ref die = lookup_type_die (type);
5933 return strip_naming_typedef (type, die);
5936 /* Equate a DIE to a given type specifier. */
5938 static inline void
5939 equate_type_number_to_die (tree type, dw_die_ref type_die)
5941 TYPE_SYMTAB_DIE (type) = type_die;
5944 static dw_die_ref maybe_create_die_with_external_ref (tree);
5945 struct GTY(()) sym_off_pair
5947 const char * GTY((skip)) sym;
5948 unsigned HOST_WIDE_INT off;
5950 static GTY(()) hash_map<tree, sym_off_pair> *external_die_map;
5952 /* Returns a hash value for X (which really is a die_struct). */
5954 inline hashval_t
5955 decl_die_hasher::hash (die_node *x)
5957 return (hashval_t) x->decl_id;
5960 /* Return true if decl_id of die_struct X is the same as UID of decl *Y. */
5962 inline bool
5963 decl_die_hasher::equal (die_node *x, tree y)
5965 return (x->decl_id == DECL_UID (y));
5968 /* Return the DIE associated with a given declaration. */
5970 dw_die_ref
5971 lookup_decl_die (tree decl)
5973 dw_die_ref *die = decl_die_table->find_slot_with_hash (decl, DECL_UID (decl),
5974 NO_INSERT);
5975 if (!die)
5977 if (in_lto_p)
5978 return maybe_create_die_with_external_ref (decl);
5979 return NULL;
5981 if ((*die)->removed)
5983 decl_die_table->clear_slot (die);
5984 return NULL;
5986 return *die;
5990 /* Return the DIE associated with BLOCK. */
5992 static inline dw_die_ref
5993 lookup_block_die (tree block)
5995 dw_die_ref die = BLOCK_DIE (block);
5996 if (!die && in_lto_p)
5997 return maybe_create_die_with_external_ref (block);
5998 return die;
6001 /* Associate DIE with BLOCK. */
6003 static inline void
6004 equate_block_to_die (tree block, dw_die_ref die)
6006 BLOCK_DIE (block) = die;
6008 #undef BLOCK_DIE
6011 /* For DECL which might have early dwarf output query a SYMBOL + OFFSET
6012 style reference. Return true if we found one refering to a DIE for
6013 DECL, otherwise return false. */
6015 static bool
6016 dwarf2out_die_ref_for_decl (tree decl, const char **sym,
6017 unsigned HOST_WIDE_INT *off)
6019 dw_die_ref die;
6021 if (in_lto_p)
6023 /* During WPA stage and incremental linking we use a hash-map
6024 to store the decl <-> label + offset map. */
6025 if (!external_die_map)
6026 return false;
6027 sym_off_pair *desc = external_die_map->get (decl);
6028 if (!desc)
6029 return false;
6030 *sym = desc->sym;
6031 *off = desc->off;
6032 return true;
6035 if (TREE_CODE (decl) == BLOCK)
6036 die = lookup_block_die (decl);
6037 else
6038 die = lookup_decl_die (decl);
6039 if (!die)
6040 return false;
6042 /* Similar to get_ref_die_offset_label, but using the "correct"
6043 label. */
6044 *off = die->die_offset;
6045 while (die->die_parent)
6046 die = die->die_parent;
6047 /* For the containing CU DIE we compute a die_symbol in
6048 compute_comp_unit_symbol. */
6049 gcc_assert (die->die_tag == DW_TAG_compile_unit
6050 && die->die_id.die_symbol != NULL);
6051 *sym = die->die_id.die_symbol;
6052 return true;
6055 /* Add a reference of kind ATTR_KIND to a DIE at SYMBOL + OFFSET to DIE. */
6057 static void
6058 add_AT_external_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind,
6059 const char *symbol, HOST_WIDE_INT offset)
6061 /* Create a fake DIE that contains the reference. Don't use
6062 new_die because we don't want to end up in the limbo list. */
6063 /* ??? We probably want to share these, thus put a ref to the DIE
6064 we create here to the external_die_map entry. */
6065 dw_die_ref ref = new_die_raw (die->die_tag);
6066 ref->die_id.die_symbol = symbol;
6067 ref->die_offset = offset;
6068 ref->with_offset = 1;
6069 add_AT_die_ref (die, attr_kind, ref);
6072 /* Create a DIE for DECL if required and add a reference to a DIE
6073 at SYMBOL + OFFSET which contains attributes dumped early. */
6075 static void
6076 dwarf2out_register_external_die (tree decl, const char *sym,
6077 unsigned HOST_WIDE_INT off)
6079 if (debug_info_level == DINFO_LEVEL_NONE)
6080 return;
6082 if (!external_die_map)
6083 external_die_map = hash_map<tree, sym_off_pair>::create_ggc (1000);
6084 gcc_checking_assert (!external_die_map->get (decl));
6085 sym_off_pair p = { IDENTIFIER_POINTER (get_identifier (sym)), off };
6086 external_die_map->put (decl, p);
6089 /* If we have a registered external DIE for DECL return a new DIE for
6090 the concrete instance with an appropriate abstract origin. */
6092 static dw_die_ref
6093 maybe_create_die_with_external_ref (tree decl)
6095 if (!external_die_map)
6096 return NULL;
6097 sym_off_pair *desc = external_die_map->get (decl);
6098 if (!desc)
6099 return NULL;
6101 const char *sym = desc->sym;
6102 unsigned HOST_WIDE_INT off = desc->off;
6103 external_die_map->remove (decl);
6105 in_lto_p = false;
6106 dw_die_ref die = (TREE_CODE (decl) == BLOCK
6107 ? lookup_block_die (decl) : lookup_decl_die (decl));
6108 gcc_assert (!die);
6109 in_lto_p = true;
6111 tree ctx;
6112 dw_die_ref parent = NULL;
6113 /* Need to lookup a DIE for the decls context - the containing
6114 function or translation unit. */
6115 if (TREE_CODE (decl) == BLOCK)
6117 ctx = BLOCK_SUPERCONTEXT (decl);
6118 /* ??? We do not output DIEs for all scopes thus skip as
6119 many DIEs as needed. */
6120 while (TREE_CODE (ctx) == BLOCK
6121 && !lookup_block_die (ctx))
6122 ctx = BLOCK_SUPERCONTEXT (ctx);
6124 else
6125 ctx = DECL_CONTEXT (decl);
6126 /* Peel types in the context stack. */
6127 while (ctx && TYPE_P (ctx))
6128 ctx = TYPE_CONTEXT (ctx);
6129 /* Likewise namespaces in case we do not want to emit DIEs for them. */
6130 if (debug_info_level <= DINFO_LEVEL_TERSE)
6131 while (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
6132 ctx = DECL_CONTEXT (ctx);
6133 if (ctx)
6135 if (TREE_CODE (ctx) == BLOCK)
6136 parent = lookup_block_die (ctx);
6137 else if (TREE_CODE (ctx) == TRANSLATION_UNIT_DECL
6138 /* Keep the 1:1 association during WPA. */
6139 && !flag_wpa
6140 && flag_incremental_link != INCREMENTAL_LINK_LTO)
6141 /* Otherwise all late annotations go to the main CU which
6142 imports the original CUs. */
6143 parent = comp_unit_die ();
6144 else if (TREE_CODE (ctx) == FUNCTION_DECL
6145 && TREE_CODE (decl) != FUNCTION_DECL
6146 && TREE_CODE (decl) != PARM_DECL
6147 && TREE_CODE (decl) != RESULT_DECL
6148 && TREE_CODE (decl) != BLOCK)
6149 /* Leave function local entities parent determination to when
6150 we process scope vars. */
6152 else
6153 parent = lookup_decl_die (ctx);
6155 else
6156 /* In some cases the FEs fail to set DECL_CONTEXT properly.
6157 Handle this case gracefully by globalizing stuff. */
6158 parent = comp_unit_die ();
6159 /* Create a DIE "stub". */
6160 switch (TREE_CODE (decl))
6162 case TRANSLATION_UNIT_DECL:
6164 die = comp_unit_die ();
6165 /* We re-target all CU decls to the LTRANS CU DIE, so no need
6166 to create a DIE for the original CUs. */
6167 return die;
6169 case NAMESPACE_DECL:
6170 if (is_fortran (decl))
6171 die = new_die (DW_TAG_module, parent, decl);
6172 else
6173 die = new_die (DW_TAG_namespace, parent, decl);
6174 break;
6175 case FUNCTION_DECL:
6176 die = new_die (DW_TAG_subprogram, parent, decl);
6177 break;
6178 case VAR_DECL:
6179 die = new_die (DW_TAG_variable, parent, decl);
6180 break;
6181 case RESULT_DECL:
6182 die = new_die (DW_TAG_variable, parent, decl);
6183 break;
6184 case PARM_DECL:
6185 die = new_die (DW_TAG_formal_parameter, parent, decl);
6186 break;
6187 case CONST_DECL:
6188 die = new_die (DW_TAG_constant, parent, decl);
6189 break;
6190 case LABEL_DECL:
6191 die = new_die (DW_TAG_label, parent, decl);
6192 break;
6193 case BLOCK:
6194 die = new_die (DW_TAG_lexical_block, parent, decl);
6195 break;
6196 default:
6197 gcc_unreachable ();
6199 if (TREE_CODE (decl) == BLOCK)
6200 equate_block_to_die (decl, die);
6201 else
6202 equate_decl_number_to_die (decl, die);
6204 add_desc_attribute (die, decl);
6206 /* Add a reference to the DIE providing early debug at $sym + off. */
6207 add_AT_external_die_ref (die, DW_AT_abstract_origin, sym, off);
6209 return die;
6212 /* Returns a hash value for X (which really is a var_loc_list). */
6214 inline hashval_t
6215 decl_loc_hasher::hash (var_loc_list *x)
6217 return (hashval_t) x->decl_id;
6220 /* Return true if decl_id of var_loc_list X is the same as
6221 UID of decl *Y. */
6223 inline bool
6224 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
6226 return (x->decl_id == DECL_UID (y));
6229 /* Return the var_loc list associated with a given declaration. */
6231 static inline var_loc_list *
6232 lookup_decl_loc (const_tree decl)
6234 if (!decl_loc_table)
6235 return NULL;
6236 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
6239 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
6241 inline hashval_t
6242 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
6244 return (hashval_t) x->decl_id;
6247 /* Return true if decl_id of cached_dw_loc_list X is the same as
6248 UID of decl *Y. */
6250 inline bool
6251 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
6253 return (x->decl_id == DECL_UID (y));
6256 /* Equate a DIE to a particular declaration. */
6258 static void
6259 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
6261 unsigned int decl_id = DECL_UID (decl);
6263 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
6264 decl_die->decl_id = decl_id;
6267 /* Return how many bits covers PIECE EXPR_LIST. */
6269 static HOST_WIDE_INT
6270 decl_piece_bitsize (rtx piece)
6272 int ret = (int) GET_MODE (piece);
6273 if (ret)
6274 return ret;
6275 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
6276 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
6277 return INTVAL (XEXP (XEXP (piece, 0), 0));
6280 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
6282 static rtx *
6283 decl_piece_varloc_ptr (rtx piece)
6285 if ((int) GET_MODE (piece))
6286 return &XEXP (piece, 0);
6287 else
6288 return &XEXP (XEXP (piece, 0), 1);
6291 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
6292 Next is the chain of following piece nodes. */
6294 static rtx_expr_list *
6295 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
6297 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
6298 return alloc_EXPR_LIST (bitsize, loc_note, next);
6299 else
6300 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
6301 GEN_INT (bitsize),
6302 loc_note), next);
6305 /* Return rtx that should be stored into loc field for
6306 LOC_NOTE and BITPOS/BITSIZE. */
6308 static rtx
6309 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
6310 HOST_WIDE_INT bitsize)
6312 if (bitsize != -1)
6314 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
6315 if (bitpos != 0)
6316 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
6318 return loc_note;
6321 /* This function either modifies location piece list *DEST in
6322 place (if SRC and INNER is NULL), or copies location piece list
6323 *SRC to *DEST while modifying it. Location BITPOS is modified
6324 to contain LOC_NOTE, any pieces overlapping it are removed resp.
6325 not copied and if needed some padding around it is added.
6326 When modifying in place, DEST should point to EXPR_LIST where
6327 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
6328 to the start of the whole list and INNER points to the EXPR_LIST
6329 where earlier pieces cover PIECE_BITPOS bits. */
6331 static void
6332 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
6333 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
6334 HOST_WIDE_INT bitsize, rtx loc_note)
6336 HOST_WIDE_INT diff;
6337 bool copy = inner != NULL;
6339 if (copy)
6341 /* First copy all nodes preceding the current bitpos. */
6342 while (src != inner)
6344 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
6345 decl_piece_bitsize (*src), NULL_RTX);
6346 dest = &XEXP (*dest, 1);
6347 src = &XEXP (*src, 1);
6350 /* Add padding if needed. */
6351 if (bitpos != piece_bitpos)
6353 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
6354 copy ? NULL_RTX : *dest);
6355 dest = &XEXP (*dest, 1);
6357 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
6359 gcc_assert (!copy);
6360 /* A piece with correct bitpos and bitsize already exist,
6361 just update the location for it and return. */
6362 *decl_piece_varloc_ptr (*dest) = loc_note;
6363 return;
6365 /* Add the piece that changed. */
6366 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
6367 dest = &XEXP (*dest, 1);
6368 /* Skip over pieces that overlap it. */
6369 diff = bitpos - piece_bitpos + bitsize;
6370 if (!copy)
6371 src = dest;
6372 while (diff > 0 && *src)
6374 rtx piece = *src;
6375 diff -= decl_piece_bitsize (piece);
6376 if (copy)
6377 src = &XEXP (piece, 1);
6378 else
6380 *src = XEXP (piece, 1);
6381 free_EXPR_LIST_node (piece);
6384 /* Add padding if needed. */
6385 if (diff < 0 && *src)
6387 if (!copy)
6388 dest = src;
6389 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
6390 dest = &XEXP (*dest, 1);
6392 if (!copy)
6393 return;
6394 /* Finally copy all nodes following it. */
6395 while (*src)
6397 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
6398 decl_piece_bitsize (*src), NULL_RTX);
6399 dest = &XEXP (*dest, 1);
6400 src = &XEXP (*src, 1);
6404 /* Add a variable location node to the linked list for DECL. */
6406 static struct var_loc_node *
6407 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label, var_loc_view view)
6409 unsigned int decl_id;
6410 var_loc_list *temp;
6411 struct var_loc_node *loc = NULL;
6412 HOST_WIDE_INT bitsize = -1, bitpos = -1;
6414 if (VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
6416 tree realdecl = DECL_DEBUG_EXPR (decl);
6417 if (handled_component_p (realdecl)
6418 || (TREE_CODE (realdecl) == MEM_REF
6419 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
6421 bool reverse;
6422 tree innerdecl = get_ref_base_and_extent_hwi (realdecl, &bitpos,
6423 &bitsize, &reverse);
6424 if (!innerdecl
6425 || !DECL_P (innerdecl)
6426 || DECL_IGNORED_P (innerdecl)
6427 || TREE_STATIC (innerdecl)
6428 || bitsize == 0
6429 || bitpos + bitsize > 256)
6430 return NULL;
6431 decl = innerdecl;
6435 decl_id = DECL_UID (decl);
6436 var_loc_list **slot
6437 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
6438 if (*slot == NULL)
6440 temp = ggc_cleared_alloc<var_loc_list> ();
6441 temp->decl_id = decl_id;
6442 *slot = temp;
6444 else
6445 temp = *slot;
6447 /* For PARM_DECLs try to keep around the original incoming value,
6448 even if that means we'll emit a zero-range .debug_loc entry. */
6449 if (temp->last
6450 && temp->first == temp->last
6451 && TREE_CODE (decl) == PARM_DECL
6452 && NOTE_P (temp->first->loc)
6453 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
6454 && DECL_INCOMING_RTL (decl)
6455 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
6456 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
6457 == GET_CODE (DECL_INCOMING_RTL (decl))
6458 && prev_real_insn (as_a<rtx_insn *> (temp->first->loc)) == NULL_RTX
6459 && (bitsize != -1
6460 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
6461 NOTE_VAR_LOCATION_LOC (loc_note))
6462 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
6463 != NOTE_VAR_LOCATION_STATUS (loc_note))))
6465 loc = ggc_cleared_alloc<var_loc_node> ();
6466 temp->first->next = loc;
6467 temp->last = loc;
6468 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6470 else if (temp->last)
6472 struct var_loc_node *last = temp->last, *unused = NULL;
6473 rtx *piece_loc = NULL, last_loc_note;
6474 HOST_WIDE_INT piece_bitpos = 0;
6475 if (last->next)
6477 last = last->next;
6478 gcc_assert (last->next == NULL);
6480 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
6482 piece_loc = &last->loc;
6485 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
6486 if (piece_bitpos + cur_bitsize > bitpos)
6487 break;
6488 piece_bitpos += cur_bitsize;
6489 piece_loc = &XEXP (*piece_loc, 1);
6491 while (*piece_loc);
6493 /* TEMP->LAST here is either pointer to the last but one or
6494 last element in the chained list, LAST is pointer to the
6495 last element. */
6496 if (label && strcmp (last->label, label) == 0 && last->view == view)
6498 /* For SRA optimized variables if there weren't any real
6499 insns since last note, just modify the last node. */
6500 if (piece_loc != NULL)
6502 adjust_piece_list (piece_loc, NULL, NULL,
6503 bitpos, piece_bitpos, bitsize, loc_note);
6504 return NULL;
6506 /* If the last note doesn't cover any instructions, remove it. */
6507 if (temp->last != last)
6509 temp->last->next = NULL;
6510 unused = last;
6511 last = temp->last;
6512 gcc_assert (strcmp (last->label, label) != 0 || last->view != view);
6514 else
6516 gcc_assert (temp->first == temp->last
6517 || (temp->first->next == temp->last
6518 && TREE_CODE (decl) == PARM_DECL));
6519 memset (temp->last, '\0', sizeof (*temp->last));
6520 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
6521 return temp->last;
6524 if (bitsize == -1 && NOTE_P (last->loc))
6525 last_loc_note = last->loc;
6526 else if (piece_loc != NULL
6527 && *piece_loc != NULL_RTX
6528 && piece_bitpos == bitpos
6529 && decl_piece_bitsize (*piece_loc) == bitsize)
6530 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
6531 else
6532 last_loc_note = NULL_RTX;
6533 /* If the current location is the same as the end of the list,
6534 and either both or neither of the locations is uninitialized,
6535 we have nothing to do. */
6536 if (last_loc_note == NULL_RTX
6537 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
6538 NOTE_VAR_LOCATION_LOC (loc_note)))
6539 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6540 != NOTE_VAR_LOCATION_STATUS (loc_note))
6541 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6542 == VAR_INIT_STATUS_UNINITIALIZED)
6543 || (NOTE_VAR_LOCATION_STATUS (loc_note)
6544 == VAR_INIT_STATUS_UNINITIALIZED))))
6546 /* Add LOC to the end of list and update LAST. If the last
6547 element of the list has been removed above, reuse its
6548 memory for the new node, otherwise allocate a new one. */
6549 if (unused)
6551 loc = unused;
6552 memset (loc, '\0', sizeof (*loc));
6554 else
6555 loc = ggc_cleared_alloc<var_loc_node> ();
6556 if (bitsize == -1 || piece_loc == NULL)
6557 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6558 else
6559 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
6560 bitpos, piece_bitpos, bitsize, loc_note);
6561 last->next = loc;
6562 /* Ensure TEMP->LAST will point either to the new last but one
6563 element of the chain, or to the last element in it. */
6564 if (last != temp->last)
6565 temp->last = last;
6567 else if (unused)
6568 ggc_free (unused);
6570 else
6572 loc = ggc_cleared_alloc<var_loc_node> ();
6573 temp->first = loc;
6574 temp->last = loc;
6575 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6577 return loc;
6580 /* Keep track of the number of spaces used to indent the
6581 output of the debugging routines that print the structure of
6582 the DIE internal representation. */
6583 static int print_indent;
6585 /* Indent the line the number of spaces given by print_indent. */
6587 static inline void
6588 print_spaces (FILE *outfile)
6590 fprintf (outfile, "%*s", print_indent, "");
6593 /* Print a type signature in hex. */
6595 static inline void
6596 print_signature (FILE *outfile, char *sig)
6598 int i;
6600 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
6601 fprintf (outfile, "%02x", sig[i] & 0xff);
6604 static inline void
6605 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
6607 if (discr_value->pos)
6608 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
6609 else
6610 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
6613 static void print_loc_descr (dw_loc_descr_ref, FILE *);
6615 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
6616 RECURSE, output location descriptor operations. */
6618 static void
6619 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
6621 switch (val->val_class)
6623 case dw_val_class_addr:
6624 fprintf (outfile, "address");
6625 break;
6626 case dw_val_class_offset:
6627 fprintf (outfile, "offset");
6628 break;
6629 case dw_val_class_loc:
6630 fprintf (outfile, "location descriptor");
6631 if (val->v.val_loc == NULL)
6632 fprintf (outfile, " -> <null>\n");
6633 else if (recurse)
6635 fprintf (outfile, ":\n");
6636 print_indent += 4;
6637 print_loc_descr (val->v.val_loc, outfile);
6638 print_indent -= 4;
6640 else
6642 if (flag_dump_noaddr || flag_dump_unnumbered)
6643 fprintf (outfile, " #\n");
6644 else
6645 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
6647 break;
6648 case dw_val_class_loc_list:
6649 fprintf (outfile, "location list -> label:%s",
6650 val->v.val_loc_list->ll_symbol);
6651 break;
6652 case dw_val_class_view_list:
6653 val = view_list_to_loc_list_val_node (val);
6654 fprintf (outfile, "location list with views -> labels:%s and %s",
6655 val->v.val_loc_list->ll_symbol,
6656 val->v.val_loc_list->vl_symbol);
6657 break;
6658 case dw_val_class_range_list:
6659 fprintf (outfile, "range list");
6660 break;
6661 case dw_val_class_const:
6662 case dw_val_class_const_implicit:
6663 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
6664 break;
6665 case dw_val_class_unsigned_const:
6666 case dw_val_class_unsigned_const_implicit:
6667 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
6668 break;
6669 case dw_val_class_const_double:
6670 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
6671 HOST_WIDE_INT_PRINT_UNSIGNED")",
6672 val->v.val_double.high,
6673 val->v.val_double.low);
6674 break;
6675 case dw_val_class_wide_int:
6677 int i = val->v.val_wide->get_len ();
6678 fprintf (outfile, "constant (");
6679 gcc_assert (i > 0);
6680 if (val->v.val_wide->elt (i - 1) == 0)
6681 fprintf (outfile, "0x");
6682 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
6683 val->v.val_wide->elt (--i));
6684 while (--i >= 0)
6685 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
6686 val->v.val_wide->elt (i));
6687 fprintf (outfile, ")");
6688 break;
6690 case dw_val_class_vec:
6691 fprintf (outfile, "floating-point or vector constant");
6692 break;
6693 case dw_val_class_flag:
6694 fprintf (outfile, "%u", val->v.val_flag);
6695 break;
6696 case dw_val_class_die_ref:
6697 if (val->v.val_die_ref.die != NULL)
6699 dw_die_ref die = val->v.val_die_ref.die;
6701 if (die->comdat_type_p)
6703 fprintf (outfile, "die -> signature: ");
6704 print_signature (outfile,
6705 die->die_id.die_type_node->signature);
6707 else if (die->die_id.die_symbol)
6709 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
6710 if (die->with_offset)
6711 fprintf (outfile, " + %ld", die->die_offset);
6713 else
6714 fprintf (outfile, "die -> %ld", die->die_offset);
6715 if (flag_dump_noaddr || flag_dump_unnumbered)
6716 fprintf (outfile, " #");
6717 else
6718 fprintf (outfile, " (%p)", (void *) die);
6720 else
6721 fprintf (outfile, "die -> <null>");
6722 break;
6723 case dw_val_class_vms_delta:
6724 fprintf (outfile, "delta: @slotcount(%s-%s)",
6725 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
6726 break;
6727 case dw_val_class_symview:
6728 fprintf (outfile, "view: %s", val->v.val_symbolic_view);
6729 break;
6730 case dw_val_class_lbl_id:
6731 case dw_val_class_lineptr:
6732 case dw_val_class_macptr:
6733 case dw_val_class_loclistsptr:
6734 case dw_val_class_high_pc:
6735 fprintf (outfile, "label: %s", val->v.val_lbl_id);
6736 break;
6737 case dw_val_class_str:
6738 if (val->v.val_str->str != NULL)
6739 fprintf (outfile, "\"%s\"", val->v.val_str->str);
6740 else
6741 fprintf (outfile, "<null>");
6742 break;
6743 case dw_val_class_file:
6744 case dw_val_class_file_implicit:
6745 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
6746 val->v.val_file->emitted_number);
6747 break;
6748 case dw_val_class_data8:
6750 int i;
6752 for (i = 0; i < 8; i++)
6753 fprintf (outfile, "%02x", val->v.val_data8[i]);
6754 break;
6756 case dw_val_class_discr_value:
6757 print_discr_value (outfile, &val->v.val_discr_value);
6758 break;
6759 case dw_val_class_discr_list:
6760 for (dw_discr_list_ref node = val->v.val_discr_list;
6761 node != NULL;
6762 node = node->dw_discr_next)
6764 if (node->dw_discr_range)
6766 fprintf (outfile, " .. ");
6767 print_discr_value (outfile, &node->dw_discr_lower_bound);
6768 print_discr_value (outfile, &node->dw_discr_upper_bound);
6770 else
6771 print_discr_value (outfile, &node->dw_discr_lower_bound);
6773 if (node->dw_discr_next != NULL)
6774 fprintf (outfile, " | ");
6776 default:
6777 break;
6781 /* Likewise, for a DIE attribute. */
6783 static void
6784 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
6786 print_dw_val (&a->dw_attr_val, recurse, outfile);
6790 /* Print the list of operands in the LOC location description to OUTFILE. This
6791 routine is a debugging aid only. */
6793 static void
6794 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
6796 dw_loc_descr_ref l = loc;
6798 if (loc == NULL)
6800 print_spaces (outfile);
6801 fprintf (outfile, "<null>\n");
6802 return;
6805 for (l = loc; l != NULL; l = l->dw_loc_next)
6807 print_spaces (outfile);
6808 if (flag_dump_noaddr || flag_dump_unnumbered)
6809 fprintf (outfile, "#");
6810 else
6811 fprintf (outfile, "(%p)", (void *) l);
6812 fprintf (outfile, " %s",
6813 dwarf_stack_op_name (l->dw_loc_opc));
6814 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
6816 fprintf (outfile, " ");
6817 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
6819 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
6821 fprintf (outfile, ", ");
6822 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
6824 fprintf (outfile, "\n");
6828 /* Print the information associated with a given DIE, and its children.
6829 This routine is a debugging aid only. */
6831 static void
6832 print_die (dw_die_ref die, FILE *outfile)
6834 dw_attr_node *a;
6835 dw_die_ref c;
6836 unsigned ix;
6838 print_spaces (outfile);
6839 fprintf (outfile, "DIE %4ld: %s ",
6840 die->die_offset, dwarf_tag_name (die->die_tag));
6841 if (flag_dump_noaddr || flag_dump_unnumbered)
6842 fprintf (outfile, "#\n");
6843 else
6844 fprintf (outfile, "(%p)\n", (void*) die);
6845 print_spaces (outfile);
6846 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
6847 fprintf (outfile, " offset: %ld", die->die_offset);
6848 fprintf (outfile, " mark: %d\n", die->die_mark);
6850 if (die->comdat_type_p)
6852 print_spaces (outfile);
6853 fprintf (outfile, " signature: ");
6854 print_signature (outfile, die->die_id.die_type_node->signature);
6855 fprintf (outfile, "\n");
6858 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6860 print_spaces (outfile);
6861 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
6863 print_attribute (a, true, outfile);
6864 fprintf (outfile, "\n");
6867 if (die->die_child != NULL)
6869 print_indent += 4;
6870 FOR_EACH_CHILD (die, c, print_die (c, outfile));
6871 print_indent -= 4;
6873 if (print_indent == 0)
6874 fprintf (outfile, "\n");
6877 /* Print the list of operations in the LOC location description. */
6879 DEBUG_FUNCTION void
6880 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
6882 print_loc_descr (loc, stderr);
6885 /* Print the information collected for a given DIE. */
6887 DEBUG_FUNCTION void
6888 debug_dwarf_die (dw_die_ref die)
6890 print_die (die, stderr);
6893 DEBUG_FUNCTION void
6894 debug (die_struct &ref)
6896 print_die (&ref, stderr);
6899 DEBUG_FUNCTION void
6900 debug (die_struct *ptr)
6902 if (ptr)
6903 debug (*ptr);
6904 else
6905 fprintf (stderr, "<nil>\n");
6909 /* Print all DWARF information collected for the compilation unit.
6910 This routine is a debugging aid only. */
6912 DEBUG_FUNCTION void
6913 debug_dwarf (void)
6915 print_indent = 0;
6916 print_die (comp_unit_die (), stderr);
6919 /* Verify the DIE tree structure. */
6921 DEBUG_FUNCTION void
6922 verify_die (dw_die_ref die)
6924 gcc_assert (!die->die_mark);
6925 if (die->die_parent == NULL
6926 && die->die_sib == NULL)
6927 return;
6928 /* Verify the die_sib list is cyclic. */
6929 dw_die_ref x = die;
6932 x->die_mark = 1;
6933 x = x->die_sib;
6935 while (x && !x->die_mark);
6936 gcc_assert (x == die);
6937 x = die;
6940 /* Verify all dies have the same parent. */
6941 gcc_assert (x->die_parent == die->die_parent);
6942 if (x->die_child)
6944 /* Verify the child has the proper parent and recurse. */
6945 gcc_assert (x->die_child->die_parent == x);
6946 verify_die (x->die_child);
6948 x->die_mark = 0;
6949 x = x->die_sib;
6951 while (x && x->die_mark);
6954 /* Sanity checks on DIEs. */
6956 static void
6957 check_die (dw_die_ref die)
6959 unsigned ix;
6960 dw_attr_node *a;
6961 bool inline_found = false;
6962 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
6963 int n_decl_line = 0, n_decl_column = 0, n_decl_file = 0;
6964 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6966 switch (a->dw_attr)
6968 case DW_AT_inline:
6969 if (a->dw_attr_val.v.val_unsigned)
6970 inline_found = true;
6971 break;
6972 case DW_AT_location:
6973 ++n_location;
6974 break;
6975 case DW_AT_low_pc:
6976 ++n_low_pc;
6977 break;
6978 case DW_AT_high_pc:
6979 ++n_high_pc;
6980 break;
6981 case DW_AT_artificial:
6982 ++n_artificial;
6983 break;
6984 case DW_AT_decl_column:
6985 ++n_decl_column;
6986 break;
6987 case DW_AT_decl_line:
6988 ++n_decl_line;
6989 break;
6990 case DW_AT_decl_file:
6991 ++n_decl_file;
6992 break;
6993 default:
6994 break;
6997 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
6998 || n_decl_column > 1 || n_decl_line > 1 || n_decl_file > 1)
7000 fprintf (stderr, "Duplicate attributes in DIE:\n");
7001 debug_dwarf_die (die);
7002 gcc_unreachable ();
7004 if (inline_found)
7006 /* A debugging information entry that is a member of an abstract
7007 instance tree [that has DW_AT_inline] should not contain any
7008 attributes which describe aspects of the subroutine which vary
7009 between distinct inlined expansions or distinct out-of-line
7010 expansions. */
7011 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7012 gcc_assert (a->dw_attr != DW_AT_low_pc
7013 && a->dw_attr != DW_AT_high_pc
7014 && a->dw_attr != DW_AT_location
7015 && a->dw_attr != DW_AT_frame_base
7016 && a->dw_attr != DW_AT_call_all_calls
7017 && a->dw_attr != DW_AT_GNU_all_call_sites);
7021 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
7022 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
7023 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
7025 /* Calculate the checksum of a location expression. */
7027 static inline void
7028 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
7030 int tem;
7031 inchash::hash hstate;
7032 hashval_t hash;
7034 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
7035 CHECKSUM (tem);
7036 hash_loc_operands (loc, hstate);
7037 hash = hstate.end();
7038 CHECKSUM (hash);
7041 /* Calculate the checksum of an attribute. */
7043 static void
7044 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
7046 dw_loc_descr_ref loc;
7047 rtx r;
7049 CHECKSUM (at->dw_attr);
7051 /* We don't care that this was compiled with a different compiler
7052 snapshot; if the output is the same, that's what matters. */
7053 if (at->dw_attr == DW_AT_producer)
7054 return;
7056 switch (AT_class (at))
7058 case dw_val_class_const:
7059 case dw_val_class_const_implicit:
7060 CHECKSUM (at->dw_attr_val.v.val_int);
7061 break;
7062 case dw_val_class_unsigned_const:
7063 case dw_val_class_unsigned_const_implicit:
7064 CHECKSUM (at->dw_attr_val.v.val_unsigned);
7065 break;
7066 case dw_val_class_const_double:
7067 CHECKSUM (at->dw_attr_val.v.val_double);
7068 break;
7069 case dw_val_class_wide_int:
7070 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
7071 get_full_len (*at->dw_attr_val.v.val_wide)
7072 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
7073 break;
7074 case dw_val_class_vec:
7075 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
7076 (at->dw_attr_val.v.val_vec.length
7077 * at->dw_attr_val.v.val_vec.elt_size));
7078 break;
7079 case dw_val_class_flag:
7080 CHECKSUM (at->dw_attr_val.v.val_flag);
7081 break;
7082 case dw_val_class_str:
7083 CHECKSUM_STRING (AT_string (at));
7084 break;
7086 case dw_val_class_addr:
7087 r = AT_addr (at);
7088 gcc_assert (GET_CODE (r) == SYMBOL_REF);
7089 CHECKSUM_STRING (XSTR (r, 0));
7090 break;
7092 case dw_val_class_offset:
7093 CHECKSUM (at->dw_attr_val.v.val_offset);
7094 break;
7096 case dw_val_class_loc:
7097 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
7098 loc_checksum (loc, ctx);
7099 break;
7101 case dw_val_class_die_ref:
7102 die_checksum (AT_ref (at), ctx, mark);
7103 break;
7105 case dw_val_class_fde_ref:
7106 case dw_val_class_vms_delta:
7107 case dw_val_class_symview:
7108 case dw_val_class_lbl_id:
7109 case dw_val_class_lineptr:
7110 case dw_val_class_macptr:
7111 case dw_val_class_loclistsptr:
7112 case dw_val_class_high_pc:
7113 break;
7115 case dw_val_class_file:
7116 case dw_val_class_file_implicit:
7117 CHECKSUM_STRING (AT_file (at)->filename);
7118 break;
7120 case dw_val_class_data8:
7121 CHECKSUM (at->dw_attr_val.v.val_data8);
7122 break;
7124 default:
7125 break;
7129 /* Calculate the checksum of a DIE. */
7131 static void
7132 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7134 dw_die_ref c;
7135 dw_attr_node *a;
7136 unsigned ix;
7138 /* To avoid infinite recursion. */
7139 if (die->die_mark)
7141 CHECKSUM (die->die_mark);
7142 return;
7144 die->die_mark = ++(*mark);
7146 CHECKSUM (die->die_tag);
7148 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7149 attr_checksum (a, ctx, mark);
7151 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
7154 #undef CHECKSUM
7155 #undef CHECKSUM_BLOCK
7156 #undef CHECKSUM_STRING
7158 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
7159 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
7160 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
7161 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
7162 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
7163 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
7164 #define CHECKSUM_ATTR(FOO) \
7165 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
7167 /* Calculate the checksum of a number in signed LEB128 format. */
7169 static void
7170 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
7172 unsigned char byte;
7173 bool more;
7175 while (1)
7177 byte = (value & 0x7f);
7178 value >>= 7;
7179 more = !((value == 0 && (byte & 0x40) == 0)
7180 || (value == -1 && (byte & 0x40) != 0));
7181 if (more)
7182 byte |= 0x80;
7183 CHECKSUM (byte);
7184 if (!more)
7185 break;
7189 /* Calculate the checksum of a number in unsigned LEB128 format. */
7191 static void
7192 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
7194 while (1)
7196 unsigned char byte = (value & 0x7f);
7197 value >>= 7;
7198 if (value != 0)
7199 /* More bytes to follow. */
7200 byte |= 0x80;
7201 CHECKSUM (byte);
7202 if (value == 0)
7203 break;
7207 /* Checksum the context of the DIE. This adds the names of any
7208 surrounding namespaces or structures to the checksum. */
7210 static void
7211 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
7213 const char *name;
7214 dw_die_ref spec;
7215 int tag = die->die_tag;
7217 if (tag != DW_TAG_namespace
7218 && tag != DW_TAG_structure_type
7219 && tag != DW_TAG_class_type)
7220 return;
7222 name = get_AT_string (die, DW_AT_name);
7224 spec = get_AT_ref (die, DW_AT_specification);
7225 if (spec != NULL)
7226 die = spec;
7228 if (die->die_parent != NULL)
7229 checksum_die_context (die->die_parent, ctx);
7231 CHECKSUM_ULEB128 ('C');
7232 CHECKSUM_ULEB128 (tag);
7233 if (name != NULL)
7234 CHECKSUM_STRING (name);
7237 /* Calculate the checksum of a location expression. */
7239 static inline void
7240 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
7242 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
7243 were emitted as a DW_FORM_sdata instead of a location expression. */
7244 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
7246 CHECKSUM_ULEB128 (DW_FORM_sdata);
7247 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
7248 return;
7251 /* Otherwise, just checksum the raw location expression. */
7252 while (loc != NULL)
7254 inchash::hash hstate;
7255 hashval_t hash;
7257 CHECKSUM_ULEB128 (loc->dtprel);
7258 CHECKSUM_ULEB128 (loc->dw_loc_opc);
7259 hash_loc_operands (loc, hstate);
7260 hash = hstate.end ();
7261 CHECKSUM (hash);
7262 loc = loc->dw_loc_next;
7266 /* Calculate the checksum of an attribute. */
7268 static void
7269 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
7270 struct md5_ctx *ctx, int *mark)
7272 dw_loc_descr_ref loc;
7273 rtx r;
7275 if (AT_class (at) == dw_val_class_die_ref)
7277 dw_die_ref target_die = AT_ref (at);
7279 /* For pointer and reference types, we checksum only the (qualified)
7280 name of the target type (if there is a name). For friend entries,
7281 we checksum only the (qualified) name of the target type or function.
7282 This allows the checksum to remain the same whether the target type
7283 is complete or not. */
7284 if ((at->dw_attr == DW_AT_type
7285 && (tag == DW_TAG_pointer_type
7286 || tag == DW_TAG_reference_type
7287 || tag == DW_TAG_rvalue_reference_type
7288 || tag == DW_TAG_ptr_to_member_type))
7289 || (at->dw_attr == DW_AT_friend
7290 && tag == DW_TAG_friend))
7292 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
7294 if (name_attr != NULL)
7296 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
7298 if (decl == NULL)
7299 decl = target_die;
7300 CHECKSUM_ULEB128 ('N');
7301 CHECKSUM_ULEB128 (at->dw_attr);
7302 if (decl->die_parent != NULL)
7303 checksum_die_context (decl->die_parent, ctx);
7304 CHECKSUM_ULEB128 ('E');
7305 CHECKSUM_STRING (AT_string (name_attr));
7306 return;
7310 /* For all other references to another DIE, we check to see if the
7311 target DIE has already been visited. If it has, we emit a
7312 backward reference; if not, we descend recursively. */
7313 if (target_die->die_mark > 0)
7315 CHECKSUM_ULEB128 ('R');
7316 CHECKSUM_ULEB128 (at->dw_attr);
7317 CHECKSUM_ULEB128 (target_die->die_mark);
7319 else
7321 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
7323 if (decl == NULL)
7324 decl = target_die;
7325 target_die->die_mark = ++(*mark);
7326 CHECKSUM_ULEB128 ('T');
7327 CHECKSUM_ULEB128 (at->dw_attr);
7328 if (decl->die_parent != NULL)
7329 checksum_die_context (decl->die_parent, ctx);
7330 die_checksum_ordered (target_die, ctx, mark);
7332 return;
7335 CHECKSUM_ULEB128 ('A');
7336 CHECKSUM_ULEB128 (at->dw_attr);
7338 switch (AT_class (at))
7340 case dw_val_class_const:
7341 case dw_val_class_const_implicit:
7342 CHECKSUM_ULEB128 (DW_FORM_sdata);
7343 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
7344 break;
7346 case dw_val_class_unsigned_const:
7347 case dw_val_class_unsigned_const_implicit:
7348 CHECKSUM_ULEB128 (DW_FORM_sdata);
7349 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
7350 break;
7352 case dw_val_class_const_double:
7353 CHECKSUM_ULEB128 (DW_FORM_block);
7354 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
7355 CHECKSUM (at->dw_attr_val.v.val_double);
7356 break;
7358 case dw_val_class_wide_int:
7359 CHECKSUM_ULEB128 (DW_FORM_block);
7360 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
7361 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
7362 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
7363 get_full_len (*at->dw_attr_val.v.val_wide)
7364 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
7365 break;
7367 case dw_val_class_vec:
7368 CHECKSUM_ULEB128 (DW_FORM_block);
7369 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
7370 * at->dw_attr_val.v.val_vec.elt_size);
7371 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
7372 (at->dw_attr_val.v.val_vec.length
7373 * at->dw_attr_val.v.val_vec.elt_size));
7374 break;
7376 case dw_val_class_flag:
7377 CHECKSUM_ULEB128 (DW_FORM_flag);
7378 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
7379 break;
7381 case dw_val_class_str:
7382 CHECKSUM_ULEB128 (DW_FORM_string);
7383 CHECKSUM_STRING (AT_string (at));
7384 break;
7386 case dw_val_class_addr:
7387 r = AT_addr (at);
7388 gcc_assert (GET_CODE (r) == SYMBOL_REF);
7389 CHECKSUM_ULEB128 (DW_FORM_string);
7390 CHECKSUM_STRING (XSTR (r, 0));
7391 break;
7393 case dw_val_class_offset:
7394 CHECKSUM_ULEB128 (DW_FORM_sdata);
7395 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
7396 break;
7398 case dw_val_class_loc:
7399 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
7400 loc_checksum_ordered (loc, ctx);
7401 break;
7403 case dw_val_class_fde_ref:
7404 case dw_val_class_symview:
7405 case dw_val_class_lbl_id:
7406 case dw_val_class_lineptr:
7407 case dw_val_class_macptr:
7408 case dw_val_class_loclistsptr:
7409 case dw_val_class_high_pc:
7410 break;
7412 case dw_val_class_file:
7413 case dw_val_class_file_implicit:
7414 CHECKSUM_ULEB128 (DW_FORM_string);
7415 CHECKSUM_STRING (AT_file (at)->filename);
7416 break;
7418 case dw_val_class_data8:
7419 CHECKSUM (at->dw_attr_val.v.val_data8);
7420 break;
7422 default:
7423 break;
7427 struct checksum_attributes
7429 dw_attr_node *at_name;
7430 dw_attr_node *at_type;
7431 dw_attr_node *at_friend;
7432 dw_attr_node *at_accessibility;
7433 dw_attr_node *at_address_class;
7434 dw_attr_node *at_alignment;
7435 dw_attr_node *at_allocated;
7436 dw_attr_node *at_artificial;
7437 dw_attr_node *at_associated;
7438 dw_attr_node *at_binary_scale;
7439 dw_attr_node *at_bit_offset;
7440 dw_attr_node *at_bit_size;
7441 dw_attr_node *at_bit_stride;
7442 dw_attr_node *at_byte_size;
7443 dw_attr_node *at_byte_stride;
7444 dw_attr_node *at_const_value;
7445 dw_attr_node *at_containing_type;
7446 dw_attr_node *at_count;
7447 dw_attr_node *at_data_location;
7448 dw_attr_node *at_data_member_location;
7449 dw_attr_node *at_decimal_scale;
7450 dw_attr_node *at_decimal_sign;
7451 dw_attr_node *at_default_value;
7452 dw_attr_node *at_digit_count;
7453 dw_attr_node *at_discr;
7454 dw_attr_node *at_discr_list;
7455 dw_attr_node *at_discr_value;
7456 dw_attr_node *at_encoding;
7457 dw_attr_node *at_endianity;
7458 dw_attr_node *at_explicit;
7459 dw_attr_node *at_is_optional;
7460 dw_attr_node *at_location;
7461 dw_attr_node *at_lower_bound;
7462 dw_attr_node *at_mutable;
7463 dw_attr_node *at_ordering;
7464 dw_attr_node *at_picture_string;
7465 dw_attr_node *at_prototyped;
7466 dw_attr_node *at_small;
7467 dw_attr_node *at_segment;
7468 dw_attr_node *at_string_length;
7469 dw_attr_node *at_string_length_bit_size;
7470 dw_attr_node *at_string_length_byte_size;
7471 dw_attr_node *at_threads_scaled;
7472 dw_attr_node *at_upper_bound;
7473 dw_attr_node *at_use_location;
7474 dw_attr_node *at_use_UTF8;
7475 dw_attr_node *at_variable_parameter;
7476 dw_attr_node *at_virtuality;
7477 dw_attr_node *at_visibility;
7478 dw_attr_node *at_vtable_elem_location;
7481 /* Collect the attributes that we will want to use for the checksum. */
7483 static void
7484 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
7486 dw_attr_node *a;
7487 unsigned ix;
7489 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7491 switch (a->dw_attr)
7493 case DW_AT_name:
7494 attrs->at_name = a;
7495 break;
7496 case DW_AT_type:
7497 attrs->at_type = a;
7498 break;
7499 case DW_AT_friend:
7500 attrs->at_friend = a;
7501 break;
7502 case DW_AT_accessibility:
7503 attrs->at_accessibility = a;
7504 break;
7505 case DW_AT_address_class:
7506 attrs->at_address_class = a;
7507 break;
7508 case DW_AT_alignment:
7509 attrs->at_alignment = a;
7510 break;
7511 case DW_AT_allocated:
7512 attrs->at_allocated = a;
7513 break;
7514 case DW_AT_artificial:
7515 attrs->at_artificial = a;
7516 break;
7517 case DW_AT_associated:
7518 attrs->at_associated = a;
7519 break;
7520 case DW_AT_binary_scale:
7521 attrs->at_binary_scale = a;
7522 break;
7523 case DW_AT_bit_offset:
7524 attrs->at_bit_offset = a;
7525 break;
7526 case DW_AT_bit_size:
7527 attrs->at_bit_size = a;
7528 break;
7529 case DW_AT_bit_stride:
7530 attrs->at_bit_stride = a;
7531 break;
7532 case DW_AT_byte_size:
7533 attrs->at_byte_size = a;
7534 break;
7535 case DW_AT_byte_stride:
7536 attrs->at_byte_stride = a;
7537 break;
7538 case DW_AT_const_value:
7539 attrs->at_const_value = a;
7540 break;
7541 case DW_AT_containing_type:
7542 attrs->at_containing_type = a;
7543 break;
7544 case DW_AT_count:
7545 attrs->at_count = a;
7546 break;
7547 case DW_AT_data_location:
7548 attrs->at_data_location = a;
7549 break;
7550 case DW_AT_data_member_location:
7551 attrs->at_data_member_location = a;
7552 break;
7553 case DW_AT_decimal_scale:
7554 attrs->at_decimal_scale = a;
7555 break;
7556 case DW_AT_decimal_sign:
7557 attrs->at_decimal_sign = a;
7558 break;
7559 case DW_AT_default_value:
7560 attrs->at_default_value = a;
7561 break;
7562 case DW_AT_digit_count:
7563 attrs->at_digit_count = a;
7564 break;
7565 case DW_AT_discr:
7566 attrs->at_discr = a;
7567 break;
7568 case DW_AT_discr_list:
7569 attrs->at_discr_list = a;
7570 break;
7571 case DW_AT_discr_value:
7572 attrs->at_discr_value = a;
7573 break;
7574 case DW_AT_encoding:
7575 attrs->at_encoding = a;
7576 break;
7577 case DW_AT_endianity:
7578 attrs->at_endianity = a;
7579 break;
7580 case DW_AT_explicit:
7581 attrs->at_explicit = a;
7582 break;
7583 case DW_AT_is_optional:
7584 attrs->at_is_optional = a;
7585 break;
7586 case DW_AT_location:
7587 attrs->at_location = a;
7588 break;
7589 case DW_AT_lower_bound:
7590 attrs->at_lower_bound = a;
7591 break;
7592 case DW_AT_mutable:
7593 attrs->at_mutable = a;
7594 break;
7595 case DW_AT_ordering:
7596 attrs->at_ordering = a;
7597 break;
7598 case DW_AT_picture_string:
7599 attrs->at_picture_string = a;
7600 break;
7601 case DW_AT_prototyped:
7602 attrs->at_prototyped = a;
7603 break;
7604 case DW_AT_small:
7605 attrs->at_small = a;
7606 break;
7607 case DW_AT_segment:
7608 attrs->at_segment = a;
7609 break;
7610 case DW_AT_string_length:
7611 attrs->at_string_length = a;
7612 break;
7613 case DW_AT_string_length_bit_size:
7614 attrs->at_string_length_bit_size = a;
7615 break;
7616 case DW_AT_string_length_byte_size:
7617 attrs->at_string_length_byte_size = a;
7618 break;
7619 case DW_AT_threads_scaled:
7620 attrs->at_threads_scaled = a;
7621 break;
7622 case DW_AT_upper_bound:
7623 attrs->at_upper_bound = a;
7624 break;
7625 case DW_AT_use_location:
7626 attrs->at_use_location = a;
7627 break;
7628 case DW_AT_use_UTF8:
7629 attrs->at_use_UTF8 = a;
7630 break;
7631 case DW_AT_variable_parameter:
7632 attrs->at_variable_parameter = a;
7633 break;
7634 case DW_AT_virtuality:
7635 attrs->at_virtuality = a;
7636 break;
7637 case DW_AT_visibility:
7638 attrs->at_visibility = a;
7639 break;
7640 case DW_AT_vtable_elem_location:
7641 attrs->at_vtable_elem_location = a;
7642 break;
7643 default:
7644 break;
7649 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
7651 static void
7652 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7654 dw_die_ref c;
7655 dw_die_ref decl;
7656 struct checksum_attributes attrs;
7658 CHECKSUM_ULEB128 ('D');
7659 CHECKSUM_ULEB128 (die->die_tag);
7661 memset (&attrs, 0, sizeof (attrs));
7663 decl = get_AT_ref (die, DW_AT_specification);
7664 if (decl != NULL)
7665 collect_checksum_attributes (&attrs, decl);
7666 collect_checksum_attributes (&attrs, die);
7668 CHECKSUM_ATTR (attrs.at_name);
7669 CHECKSUM_ATTR (attrs.at_accessibility);
7670 CHECKSUM_ATTR (attrs.at_address_class);
7671 CHECKSUM_ATTR (attrs.at_allocated);
7672 CHECKSUM_ATTR (attrs.at_artificial);
7673 CHECKSUM_ATTR (attrs.at_associated);
7674 CHECKSUM_ATTR (attrs.at_binary_scale);
7675 CHECKSUM_ATTR (attrs.at_bit_offset);
7676 CHECKSUM_ATTR (attrs.at_bit_size);
7677 CHECKSUM_ATTR (attrs.at_bit_stride);
7678 CHECKSUM_ATTR (attrs.at_byte_size);
7679 CHECKSUM_ATTR (attrs.at_byte_stride);
7680 CHECKSUM_ATTR (attrs.at_const_value);
7681 CHECKSUM_ATTR (attrs.at_containing_type);
7682 CHECKSUM_ATTR (attrs.at_count);
7683 CHECKSUM_ATTR (attrs.at_data_location);
7684 CHECKSUM_ATTR (attrs.at_data_member_location);
7685 CHECKSUM_ATTR (attrs.at_decimal_scale);
7686 CHECKSUM_ATTR (attrs.at_decimal_sign);
7687 CHECKSUM_ATTR (attrs.at_default_value);
7688 CHECKSUM_ATTR (attrs.at_digit_count);
7689 CHECKSUM_ATTR (attrs.at_discr);
7690 CHECKSUM_ATTR (attrs.at_discr_list);
7691 CHECKSUM_ATTR (attrs.at_discr_value);
7692 CHECKSUM_ATTR (attrs.at_encoding);
7693 CHECKSUM_ATTR (attrs.at_endianity);
7694 CHECKSUM_ATTR (attrs.at_explicit);
7695 CHECKSUM_ATTR (attrs.at_is_optional);
7696 CHECKSUM_ATTR (attrs.at_location);
7697 CHECKSUM_ATTR (attrs.at_lower_bound);
7698 CHECKSUM_ATTR (attrs.at_mutable);
7699 CHECKSUM_ATTR (attrs.at_ordering);
7700 CHECKSUM_ATTR (attrs.at_picture_string);
7701 CHECKSUM_ATTR (attrs.at_prototyped);
7702 CHECKSUM_ATTR (attrs.at_small);
7703 CHECKSUM_ATTR (attrs.at_segment);
7704 CHECKSUM_ATTR (attrs.at_string_length);
7705 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
7706 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
7707 CHECKSUM_ATTR (attrs.at_threads_scaled);
7708 CHECKSUM_ATTR (attrs.at_upper_bound);
7709 CHECKSUM_ATTR (attrs.at_use_location);
7710 CHECKSUM_ATTR (attrs.at_use_UTF8);
7711 CHECKSUM_ATTR (attrs.at_variable_parameter);
7712 CHECKSUM_ATTR (attrs.at_virtuality);
7713 CHECKSUM_ATTR (attrs.at_visibility);
7714 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
7715 CHECKSUM_ATTR (attrs.at_type);
7716 CHECKSUM_ATTR (attrs.at_friend);
7717 CHECKSUM_ATTR (attrs.at_alignment);
7719 /* Checksum the child DIEs. */
7720 c = die->die_child;
7721 if (c) do {
7722 dw_attr_node *name_attr;
7724 c = c->die_sib;
7725 name_attr = get_AT (c, DW_AT_name);
7726 if (is_template_instantiation (c))
7728 /* Ignore instantiations of member type and function templates. */
7730 else if (name_attr != NULL
7731 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
7733 /* Use a shallow checksum for named nested types and member
7734 functions. */
7735 CHECKSUM_ULEB128 ('S');
7736 CHECKSUM_ULEB128 (c->die_tag);
7737 CHECKSUM_STRING (AT_string (name_attr));
7739 else
7741 /* Use a deep checksum for other children. */
7742 /* Mark this DIE so it gets processed when unmarking. */
7743 if (c->die_mark == 0)
7744 c->die_mark = -1;
7745 die_checksum_ordered (c, ctx, mark);
7747 } while (c != die->die_child);
7749 CHECKSUM_ULEB128 (0);
7752 /* Add a type name and tag to a hash. */
7753 static void
7754 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
7756 CHECKSUM_ULEB128 (tag);
7757 CHECKSUM_STRING (name);
7760 #undef CHECKSUM
7761 #undef CHECKSUM_STRING
7762 #undef CHECKSUM_ATTR
7763 #undef CHECKSUM_LEB128
7764 #undef CHECKSUM_ULEB128
7766 /* Generate the type signature for DIE. This is computed by generating an
7767 MD5 checksum over the DIE's tag, its relevant attributes, and its
7768 children. Attributes that are references to other DIEs are processed
7769 by recursion, using the MARK field to prevent infinite recursion.
7770 If the DIE is nested inside a namespace or another type, we also
7771 need to include that context in the signature. The lower 64 bits
7772 of the resulting MD5 checksum comprise the signature. */
7774 static void
7775 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
7777 int mark;
7778 const char *name;
7779 unsigned char checksum[16];
7780 struct md5_ctx ctx;
7781 dw_die_ref decl;
7782 dw_die_ref parent;
7784 name = get_AT_string (die, DW_AT_name);
7785 decl = get_AT_ref (die, DW_AT_specification);
7786 parent = get_die_parent (die);
7788 /* First, compute a signature for just the type name (and its surrounding
7789 context, if any. This is stored in the type unit DIE for link-time
7790 ODR (one-definition rule) checking. */
7792 if (is_cxx () && name != NULL)
7794 md5_init_ctx (&ctx);
7796 /* Checksum the names of surrounding namespaces and structures. */
7797 if (parent != NULL)
7798 checksum_die_context (parent, &ctx);
7800 /* Checksum the current DIE. */
7801 die_odr_checksum (die->die_tag, name, &ctx);
7802 md5_finish_ctx (&ctx, checksum);
7804 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
7807 /* Next, compute the complete type signature. */
7809 md5_init_ctx (&ctx);
7810 mark = 1;
7811 die->die_mark = mark;
7813 /* Checksum the names of surrounding namespaces and structures. */
7814 if (parent != NULL)
7815 checksum_die_context (parent, &ctx);
7817 /* Checksum the DIE and its children. */
7818 die_checksum_ordered (die, &ctx, &mark);
7819 unmark_all_dies (die);
7820 md5_finish_ctx (&ctx, checksum);
7822 /* Store the signature in the type node and link the type DIE and the
7823 type node together. */
7824 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
7825 DWARF_TYPE_SIGNATURE_SIZE);
7826 die->comdat_type_p = true;
7827 die->die_id.die_type_node = type_node;
7828 type_node->type_die = die;
7830 /* If the DIE is a specification, link its declaration to the type node
7831 as well. */
7832 if (decl != NULL)
7834 decl->comdat_type_p = true;
7835 decl->die_id.die_type_node = type_node;
7839 /* Do the location expressions look same? */
7840 static inline bool
7841 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7843 return loc1->dw_loc_opc == loc2->dw_loc_opc
7844 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7845 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7848 /* Do the values look the same? */
7849 static bool
7850 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7852 dw_loc_descr_ref loc1, loc2;
7853 rtx r1, r2;
7855 if (v1->val_class != v2->val_class)
7856 return false;
7858 switch (v1->val_class)
7860 case dw_val_class_const:
7861 case dw_val_class_const_implicit:
7862 return v1->v.val_int == v2->v.val_int;
7863 case dw_val_class_unsigned_const:
7864 case dw_val_class_unsigned_const_implicit:
7865 return v1->v.val_unsigned == v2->v.val_unsigned;
7866 case dw_val_class_const_double:
7867 return v1->v.val_double.high == v2->v.val_double.high
7868 && v1->v.val_double.low == v2->v.val_double.low;
7869 case dw_val_class_wide_int:
7870 return *v1->v.val_wide == *v2->v.val_wide;
7871 case dw_val_class_vec:
7872 if (v1->v.val_vec.length != v2->v.val_vec.length
7873 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7874 return false;
7875 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7876 v1->v.val_vec.length * v1->v.val_vec.elt_size))
7877 return false;
7878 return true;
7879 case dw_val_class_flag:
7880 return v1->v.val_flag == v2->v.val_flag;
7881 case dw_val_class_str:
7882 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
7884 case dw_val_class_addr:
7885 r1 = v1->v.val_addr;
7886 r2 = v2->v.val_addr;
7887 if (GET_CODE (r1) != GET_CODE (r2))
7888 return false;
7889 return !rtx_equal_p (r1, r2);
7891 case dw_val_class_offset:
7892 return v1->v.val_offset == v2->v.val_offset;
7894 case dw_val_class_loc:
7895 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7896 loc1 && loc2;
7897 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7898 if (!same_loc_p (loc1, loc2, mark))
7899 return false;
7900 return !loc1 && !loc2;
7902 case dw_val_class_die_ref:
7903 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7905 case dw_val_class_symview:
7906 return strcmp (v1->v.val_symbolic_view, v2->v.val_symbolic_view) == 0;
7908 case dw_val_class_fde_ref:
7909 case dw_val_class_vms_delta:
7910 case dw_val_class_lbl_id:
7911 case dw_val_class_lineptr:
7912 case dw_val_class_macptr:
7913 case dw_val_class_loclistsptr:
7914 case dw_val_class_high_pc:
7915 return true;
7917 case dw_val_class_file:
7918 case dw_val_class_file_implicit:
7919 return v1->v.val_file == v2->v.val_file;
7921 case dw_val_class_data8:
7922 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
7924 default:
7925 return true;
7929 /* Do the attributes look the same? */
7931 static bool
7932 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
7934 if (at1->dw_attr != at2->dw_attr)
7935 return false;
7937 /* We don't care that this was compiled with a different compiler
7938 snapshot; if the output is the same, that's what matters. */
7939 if (at1->dw_attr == DW_AT_producer)
7940 return true;
7942 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7945 /* Do the dies look the same? */
7947 static bool
7948 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7950 dw_die_ref c1, c2;
7951 dw_attr_node *a1;
7952 unsigned ix;
7954 /* To avoid infinite recursion. */
7955 if (die1->die_mark)
7956 return die1->die_mark == die2->die_mark;
7957 die1->die_mark = die2->die_mark = ++(*mark);
7959 if (die1->die_tag != die2->die_tag)
7960 return false;
7962 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
7963 return false;
7965 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
7966 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
7967 return false;
7969 c1 = die1->die_child;
7970 c2 = die2->die_child;
7971 if (! c1)
7973 if (c2)
7974 return false;
7976 else
7977 for (;;)
7979 if (!same_die_p (c1, c2, mark))
7980 return false;
7981 c1 = c1->die_sib;
7982 c2 = c2->die_sib;
7983 if (c1 == die1->die_child)
7985 if (c2 == die2->die_child)
7986 break;
7987 else
7988 return false;
7992 return true;
7995 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7996 children, and set die_symbol. */
7998 static void
7999 compute_comp_unit_symbol (dw_die_ref unit_die)
8001 const char *die_name = get_AT_string (unit_die, DW_AT_name);
8002 const char *base = die_name ? lbasename (die_name) : "anonymous";
8003 char *name = XALLOCAVEC (char, strlen (base) + 64);
8004 char *p;
8005 int i, mark;
8006 unsigned char checksum[16];
8007 struct md5_ctx ctx;
8009 /* Compute the checksum of the DIE, then append part of it as hex digits to
8010 the name filename of the unit. */
8012 md5_init_ctx (&ctx);
8013 mark = 0;
8014 die_checksum (unit_die, &ctx, &mark);
8015 unmark_all_dies (unit_die);
8016 md5_finish_ctx (&ctx, checksum);
8018 /* When we this for comp_unit_die () we have a DW_AT_name that might
8019 not start with a letter but with anything valid for filenames and
8020 clean_symbol_name doesn't fix that up. Prepend 'g' if the first
8021 character is not a letter. */
8022 sprintf (name, "%s%s.", ISALPHA (*base) ? "" : "g", base);
8023 clean_symbol_name (name);
8025 p = name + strlen (name);
8026 for (i = 0; i < 4; i++)
8028 sprintf (p, "%.2x", checksum[i]);
8029 p += 2;
8032 unit_die->die_id.die_symbol = xstrdup (name);
8035 /* Returns true if DIE represents a type, in the sense of TYPE_P. */
8037 static bool
8038 is_type_die (dw_die_ref die)
8040 switch (die->die_tag)
8042 case DW_TAG_array_type:
8043 case DW_TAG_class_type:
8044 case DW_TAG_interface_type:
8045 case DW_TAG_enumeration_type:
8046 case DW_TAG_pointer_type:
8047 case DW_TAG_reference_type:
8048 case DW_TAG_rvalue_reference_type:
8049 case DW_TAG_string_type:
8050 case DW_TAG_structure_type:
8051 case DW_TAG_subroutine_type:
8052 case DW_TAG_union_type:
8053 case DW_TAG_ptr_to_member_type:
8054 case DW_TAG_set_type:
8055 case DW_TAG_subrange_type:
8056 case DW_TAG_base_type:
8057 case DW_TAG_const_type:
8058 case DW_TAG_file_type:
8059 case DW_TAG_packed_type:
8060 case DW_TAG_volatile_type:
8061 case DW_TAG_typedef:
8062 return true;
8063 default:
8064 return false;
8068 /* Returns true iff C is a compile-unit DIE. */
8070 static inline bool
8071 is_cu_die (dw_die_ref c)
8073 return c && (c->die_tag == DW_TAG_compile_unit
8074 || c->die_tag == DW_TAG_skeleton_unit);
8077 /* Returns true iff C is a unit DIE of some sort. */
8079 static inline bool
8080 is_unit_die (dw_die_ref c)
8082 return c && (c->die_tag == DW_TAG_compile_unit
8083 || c->die_tag == DW_TAG_partial_unit
8084 || c->die_tag == DW_TAG_type_unit
8085 || c->die_tag == DW_TAG_skeleton_unit);
8088 /* Returns true iff C is a namespace DIE. */
8090 static inline bool
8091 is_namespace_die (dw_die_ref c)
8093 return c && c->die_tag == DW_TAG_namespace;
8096 /* Return true if this DIE is a template parameter. */
8098 static inline bool
8099 is_template_parameter (dw_die_ref die)
8101 switch (die->die_tag)
8103 case DW_TAG_template_type_param:
8104 case DW_TAG_template_value_param:
8105 case DW_TAG_GNU_template_template_param:
8106 case DW_TAG_GNU_template_parameter_pack:
8107 return true;
8108 default:
8109 return false;
8113 /* Return true if this DIE represents a template instantiation. */
8115 static inline bool
8116 is_template_instantiation (dw_die_ref die)
8118 dw_die_ref c;
8120 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
8121 return false;
8122 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
8123 return false;
8126 static char *
8127 gen_internal_sym (const char *prefix)
8129 char buf[MAX_ARTIFICIAL_LABEL_BYTES];
8131 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
8132 return xstrdup (buf);
8135 /* Return true if this DIE is a declaration. */
8137 static bool
8138 is_declaration_die (dw_die_ref die)
8140 dw_attr_node *a;
8141 unsigned ix;
8143 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8144 if (a->dw_attr == DW_AT_declaration)
8145 return true;
8147 return false;
8150 /* Return true if this DIE is nested inside a subprogram. */
8152 static bool
8153 is_nested_in_subprogram (dw_die_ref die)
8155 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
8157 if (decl == NULL)
8158 decl = die;
8159 return local_scope_p (decl);
8162 /* Return true if this DIE contains a defining declaration of a
8163 subprogram. */
8165 static bool
8166 contains_subprogram_definition (dw_die_ref die)
8168 dw_die_ref c;
8170 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
8171 return true;
8172 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
8173 return false;
8176 /* Return true if this is a type DIE that should be moved to a
8177 COMDAT .debug_types section or .debug_info section with DW_UT_*type
8178 unit type. */
8180 static bool
8181 should_move_die_to_comdat (dw_die_ref die)
8183 switch (die->die_tag)
8185 case DW_TAG_class_type:
8186 case DW_TAG_structure_type:
8187 case DW_TAG_enumeration_type:
8188 case DW_TAG_union_type:
8189 /* Don't move declarations, inlined instances, types nested in a
8190 subprogram, or types that contain subprogram definitions. */
8191 if (is_declaration_die (die)
8192 || get_AT (die, DW_AT_abstract_origin)
8193 || is_nested_in_subprogram (die)
8194 || contains_subprogram_definition (die))
8195 return false;
8196 return true;
8197 case DW_TAG_array_type:
8198 case DW_TAG_interface_type:
8199 case DW_TAG_pointer_type:
8200 case DW_TAG_reference_type:
8201 case DW_TAG_rvalue_reference_type:
8202 case DW_TAG_string_type:
8203 case DW_TAG_subroutine_type:
8204 case DW_TAG_ptr_to_member_type:
8205 case DW_TAG_set_type:
8206 case DW_TAG_subrange_type:
8207 case DW_TAG_base_type:
8208 case DW_TAG_const_type:
8209 case DW_TAG_file_type:
8210 case DW_TAG_packed_type:
8211 case DW_TAG_volatile_type:
8212 case DW_TAG_typedef:
8213 default:
8214 return false;
8218 /* Make a clone of DIE. */
8220 static dw_die_ref
8221 clone_die (dw_die_ref die)
8223 dw_die_ref clone = new_die_raw (die->die_tag);
8224 dw_attr_node *a;
8225 unsigned ix;
8227 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8228 add_dwarf_attr (clone, a);
8230 return clone;
8233 /* Make a clone of the tree rooted at DIE. */
8235 static dw_die_ref
8236 clone_tree (dw_die_ref die)
8238 dw_die_ref c;
8239 dw_die_ref clone = clone_die (die);
8241 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
8243 return clone;
8246 /* Make a clone of DIE as a declaration. */
8248 static dw_die_ref
8249 clone_as_declaration (dw_die_ref die)
8251 dw_die_ref clone;
8252 dw_die_ref decl;
8253 dw_attr_node *a;
8254 unsigned ix;
8256 /* If the DIE is already a declaration, just clone it. */
8257 if (is_declaration_die (die))
8258 return clone_die (die);
8260 /* If the DIE is a specification, just clone its declaration DIE. */
8261 decl = get_AT_ref (die, DW_AT_specification);
8262 if (decl != NULL)
8264 clone = clone_die (decl);
8265 if (die->comdat_type_p)
8266 add_AT_die_ref (clone, DW_AT_signature, die);
8267 return clone;
8270 clone = new_die_raw (die->die_tag);
8272 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8274 /* We don't want to copy over all attributes.
8275 For example we don't want DW_AT_byte_size because otherwise we will no
8276 longer have a declaration and GDB will treat it as a definition. */
8278 switch (a->dw_attr)
8280 case DW_AT_abstract_origin:
8281 case DW_AT_artificial:
8282 case DW_AT_containing_type:
8283 case DW_AT_external:
8284 case DW_AT_name:
8285 case DW_AT_type:
8286 case DW_AT_virtuality:
8287 case DW_AT_linkage_name:
8288 case DW_AT_MIPS_linkage_name:
8289 add_dwarf_attr (clone, a);
8290 break;
8291 case DW_AT_byte_size:
8292 case DW_AT_alignment:
8293 default:
8294 break;
8298 if (die->comdat_type_p)
8299 add_AT_die_ref (clone, DW_AT_signature, die);
8301 add_AT_flag (clone, DW_AT_declaration, 1);
8302 return clone;
8306 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
8308 struct decl_table_entry
8310 dw_die_ref orig;
8311 dw_die_ref copy;
8314 /* Helpers to manipulate hash table of copied declarations. */
8316 /* Hashtable helpers. */
8318 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
8320 typedef die_struct *compare_type;
8321 static inline hashval_t hash (const decl_table_entry *);
8322 static inline bool equal (const decl_table_entry *, const die_struct *);
8325 inline hashval_t
8326 decl_table_entry_hasher::hash (const decl_table_entry *entry)
8328 return htab_hash_pointer (entry->orig);
8331 inline bool
8332 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
8333 const die_struct *entry2)
8335 return entry1->orig == entry2;
8338 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
8340 /* Copy DIE and its ancestors, up to, but not including, the compile unit
8341 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
8342 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
8343 to check if the ancestor has already been copied into UNIT. */
8345 static dw_die_ref
8346 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
8347 decl_hash_type *decl_table)
8349 dw_die_ref parent = die->die_parent;
8350 dw_die_ref new_parent = unit;
8351 dw_die_ref copy;
8352 decl_table_entry **slot = NULL;
8353 struct decl_table_entry *entry = NULL;
8355 /* If DIE refers to a stub unfold that so we get the appropriate
8356 DIE registered as orig in decl_table. */
8357 if (dw_die_ref c = get_AT_ref (die, DW_AT_signature))
8358 die = c;
8360 if (decl_table)
8362 /* Check if the entry has already been copied to UNIT. */
8363 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
8364 INSERT);
8365 if (*slot != HTAB_EMPTY_ENTRY)
8367 entry = *slot;
8368 return entry->copy;
8371 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
8372 entry = XCNEW (struct decl_table_entry);
8373 entry->orig = die;
8374 entry->copy = NULL;
8375 *slot = entry;
8378 if (parent != NULL)
8380 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
8381 if (spec != NULL)
8382 parent = spec;
8383 if (!is_unit_die (parent))
8384 new_parent = copy_ancestor_tree (unit, parent, decl_table);
8387 copy = clone_as_declaration (die);
8388 add_child_die (new_parent, copy);
8390 if (decl_table)
8392 /* Record the pointer to the copy. */
8393 entry->copy = copy;
8396 return copy;
8398 /* Copy the declaration context to the new type unit DIE. This includes
8399 any surrounding namespace or type declarations. If the DIE has an
8400 AT_specification attribute, it also includes attributes and children
8401 attached to the specification, and returns a pointer to the original
8402 parent of the declaration DIE. Returns NULL otherwise. */
8404 static dw_die_ref
8405 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
8407 dw_die_ref decl;
8408 dw_die_ref new_decl;
8409 dw_die_ref orig_parent = NULL;
8411 decl = get_AT_ref (die, DW_AT_specification);
8412 if (decl == NULL)
8413 decl = die;
8414 else
8416 unsigned ix;
8417 dw_die_ref c;
8418 dw_attr_node *a;
8420 /* The original DIE will be changed to a declaration, and must
8421 be moved to be a child of the original declaration DIE. */
8422 orig_parent = decl->die_parent;
8424 /* Copy the type node pointer from the new DIE to the original
8425 declaration DIE so we can forward references later. */
8426 decl->comdat_type_p = true;
8427 decl->die_id.die_type_node = die->die_id.die_type_node;
8429 remove_AT (die, DW_AT_specification);
8431 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
8433 if (a->dw_attr != DW_AT_name
8434 && a->dw_attr != DW_AT_declaration
8435 && a->dw_attr != DW_AT_external)
8436 add_dwarf_attr (die, a);
8439 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
8442 if (decl->die_parent != NULL
8443 && !is_unit_die (decl->die_parent))
8445 new_decl = copy_ancestor_tree (unit, decl, NULL);
8446 if (new_decl != NULL)
8448 remove_AT (new_decl, DW_AT_signature);
8449 add_AT_specification (die, new_decl);
8453 return orig_parent;
8456 /* Generate the skeleton ancestor tree for the given NODE, then clone
8457 the DIE and add the clone into the tree. */
8459 static void
8460 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
8462 if (node->new_die != NULL)
8463 return;
8465 node->new_die = clone_as_declaration (node->old_die);
8467 if (node->parent != NULL)
8469 generate_skeleton_ancestor_tree (node->parent);
8470 add_child_die (node->parent->new_die, node->new_die);
8474 /* Generate a skeleton tree of DIEs containing any declarations that are
8475 found in the original tree. We traverse the tree looking for declaration
8476 DIEs, and construct the skeleton from the bottom up whenever we find one. */
8478 static void
8479 generate_skeleton_bottom_up (skeleton_chain_node *parent)
8481 skeleton_chain_node node;
8482 dw_die_ref c;
8483 dw_die_ref first;
8484 dw_die_ref prev = NULL;
8485 dw_die_ref next = NULL;
8487 node.parent = parent;
8489 first = c = parent->old_die->die_child;
8490 if (c)
8491 next = c->die_sib;
8492 if (c) do {
8493 if (prev == NULL || prev->die_sib == c)
8494 prev = c;
8495 c = next;
8496 next = (c == first ? NULL : c->die_sib);
8497 node.old_die = c;
8498 node.new_die = NULL;
8499 if (is_declaration_die (c))
8501 if (is_template_instantiation (c))
8503 /* Instantiated templates do not need to be cloned into the
8504 type unit. Just move the DIE and its children back to
8505 the skeleton tree (in the main CU). */
8506 remove_child_with_prev (c, prev);
8507 add_child_die (parent->new_die, c);
8508 c = prev;
8510 else if (c->comdat_type_p)
8512 /* This is the skeleton of earlier break_out_comdat_types
8513 type. Clone the existing DIE, but keep the children
8514 under the original (which is in the main CU). */
8515 dw_die_ref clone = clone_die (c);
8517 replace_child (c, clone, prev);
8518 generate_skeleton_ancestor_tree (parent);
8519 add_child_die (parent->new_die, c);
8520 c = clone;
8521 continue;
8523 else
8525 /* Clone the existing DIE, move the original to the skeleton
8526 tree (which is in the main CU), and put the clone, with
8527 all the original's children, where the original came from
8528 (which is about to be moved to the type unit). */
8529 dw_die_ref clone = clone_die (c);
8530 move_all_children (c, clone);
8532 /* If the original has a DW_AT_object_pointer attribute,
8533 it would now point to a child DIE just moved to the
8534 cloned tree, so we need to remove that attribute from
8535 the original. */
8536 remove_AT (c, DW_AT_object_pointer);
8538 replace_child (c, clone, prev);
8539 generate_skeleton_ancestor_tree (parent);
8540 add_child_die (parent->new_die, c);
8541 node.old_die = clone;
8542 node.new_die = c;
8543 c = clone;
8546 generate_skeleton_bottom_up (&node);
8547 } while (next != NULL);
8550 /* Wrapper function for generate_skeleton_bottom_up. */
8552 static dw_die_ref
8553 generate_skeleton (dw_die_ref die)
8555 skeleton_chain_node node;
8557 node.old_die = die;
8558 node.new_die = NULL;
8559 node.parent = NULL;
8561 /* If this type definition is nested inside another type,
8562 and is not an instantiation of a template, always leave
8563 at least a declaration in its place. */
8564 if (die->die_parent != NULL
8565 && is_type_die (die->die_parent)
8566 && !is_template_instantiation (die))
8567 node.new_die = clone_as_declaration (die);
8569 generate_skeleton_bottom_up (&node);
8570 return node.new_die;
8573 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
8574 declaration. The original DIE is moved to a new compile unit so that
8575 existing references to it follow it to the new location. If any of the
8576 original DIE's descendants is a declaration, we need to replace the
8577 original DIE with a skeleton tree and move the declarations back into the
8578 skeleton tree. */
8580 static dw_die_ref
8581 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
8582 dw_die_ref prev)
8584 dw_die_ref skeleton, orig_parent;
8586 /* Copy the declaration context to the type unit DIE. If the returned
8587 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
8588 that DIE. */
8589 orig_parent = copy_declaration_context (unit, child);
8591 skeleton = generate_skeleton (child);
8592 if (skeleton == NULL)
8593 remove_child_with_prev (child, prev);
8594 else
8596 skeleton->comdat_type_p = true;
8597 skeleton->die_id.die_type_node = child->die_id.die_type_node;
8599 /* If the original DIE was a specification, we need to put
8600 the skeleton under the parent DIE of the declaration.
8601 This leaves the original declaration in the tree, but
8602 it will be pruned later since there are no longer any
8603 references to it. */
8604 if (orig_parent != NULL)
8606 remove_child_with_prev (child, prev);
8607 add_child_die (orig_parent, skeleton);
8609 else
8610 replace_child (child, skeleton, prev);
8613 return skeleton;
8616 static void
8617 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8618 comdat_type_node *type_node,
8619 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
8621 /* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
8622 procedure, put it under TYPE_NODE and return the copy. Continue looking for
8623 DWARF procedure references in the DW_AT_location attribute. */
8625 static dw_die_ref
8626 copy_dwarf_procedure (dw_die_ref die,
8627 comdat_type_node *type_node,
8628 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8630 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
8632 /* DWARF procedures are not supposed to have children... */
8633 gcc_assert (die->die_child == NULL);
8635 /* ... and they are supposed to have only one attribute: DW_AT_location. */
8636 gcc_assert (vec_safe_length (die->die_attr) == 1
8637 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
8639 /* Do not copy more than once DWARF procedures. */
8640 bool existed;
8641 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
8642 if (existed)
8643 return die_copy;
8645 die_copy = clone_die (die);
8646 add_child_die (type_node->root_die, die_copy);
8647 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
8648 return die_copy;
8651 /* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
8652 procedures in DIE's attributes. */
8654 static void
8655 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8656 comdat_type_node *type_node,
8657 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8659 dw_attr_node *a;
8660 unsigned i;
8662 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
8664 dw_loc_descr_ref loc;
8666 if (a->dw_attr_val.val_class != dw_val_class_loc)
8667 continue;
8669 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
8671 switch (loc->dw_loc_opc)
8673 case DW_OP_call2:
8674 case DW_OP_call4:
8675 case DW_OP_call_ref:
8676 gcc_assert (loc->dw_loc_oprnd1.val_class
8677 == dw_val_class_die_ref);
8678 loc->dw_loc_oprnd1.v.val_die_ref.die
8679 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
8680 type_node,
8681 copied_dwarf_procs);
8683 default:
8684 break;
8690 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
8691 rewrite references to point to the copies.
8693 References are looked for in DIE's attributes and recursively in all its
8694 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
8695 mapping from old DWARF procedures to their copy. It is used not to copy
8696 twice the same DWARF procedure under TYPE_NODE. */
8698 static void
8699 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
8700 comdat_type_node *type_node,
8701 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8703 dw_die_ref c;
8705 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
8706 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
8707 type_node,
8708 copied_dwarf_procs));
8711 /* Traverse the DIE and set up additional .debug_types or .debug_info
8712 DW_UT_*type sections for each type worthy of being placed in a COMDAT
8713 section. */
8715 static void
8716 break_out_comdat_types (dw_die_ref die)
8718 dw_die_ref c;
8719 dw_die_ref first;
8720 dw_die_ref prev = NULL;
8721 dw_die_ref next = NULL;
8722 dw_die_ref unit = NULL;
8724 first = c = die->die_child;
8725 if (c)
8726 next = c->die_sib;
8727 if (c) do {
8728 if (prev == NULL || prev->die_sib == c)
8729 prev = c;
8730 c = next;
8731 next = (c == first ? NULL : c->die_sib);
8732 if (should_move_die_to_comdat (c))
8734 dw_die_ref replacement;
8735 comdat_type_node *type_node;
8737 /* Break out nested types into their own type units. */
8738 break_out_comdat_types (c);
8740 /* Create a new type unit DIE as the root for the new tree. */
8741 unit = new_die (DW_TAG_type_unit, NULL, NULL);
8742 add_AT_unsigned (unit, DW_AT_language,
8743 get_AT_unsigned (comp_unit_die (), DW_AT_language));
8745 /* Add the new unit's type DIE into the comdat type list. */
8746 type_node = ggc_cleared_alloc<comdat_type_node> ();
8747 type_node->root_die = unit;
8748 type_node->next = comdat_type_list;
8749 comdat_type_list = type_node;
8751 /* Generate the type signature. */
8752 generate_type_signature (c, type_node);
8754 /* Copy the declaration context, attributes, and children of the
8755 declaration into the new type unit DIE, then remove this DIE
8756 from the main CU (or replace it with a skeleton if necessary). */
8757 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
8758 type_node->skeleton_die = replacement;
8760 /* Add the DIE to the new compunit. */
8761 add_child_die (unit, c);
8763 /* Types can reference DWARF procedures for type size or data location
8764 expressions. Calls in DWARF expressions cannot target procedures
8765 that are not in the same section. So we must copy DWARF procedures
8766 along with this type and then rewrite references to them. */
8767 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
8768 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
8770 if (replacement != NULL)
8771 c = replacement;
8773 else if (c->die_tag == DW_TAG_namespace
8774 || c->die_tag == DW_TAG_class_type
8775 || c->die_tag == DW_TAG_structure_type
8776 || c->die_tag == DW_TAG_union_type)
8778 /* Look for nested types that can be broken out. */
8779 break_out_comdat_types (c);
8781 } while (next != NULL);
8784 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
8785 Enter all the cloned children into the hash table decl_table. */
8787 static dw_die_ref
8788 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
8790 dw_die_ref c;
8791 dw_die_ref clone;
8792 struct decl_table_entry *entry;
8793 decl_table_entry **slot;
8795 if (die->die_tag == DW_TAG_subprogram)
8796 clone = clone_as_declaration (die);
8797 else
8798 clone = clone_die (die);
8800 slot = decl_table->find_slot_with_hash (die,
8801 htab_hash_pointer (die), INSERT);
8803 /* Assert that DIE isn't in the hash table yet. If it would be there
8804 before, the ancestors would be necessarily there as well, therefore
8805 clone_tree_partial wouldn't be called. */
8806 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8808 entry = XCNEW (struct decl_table_entry);
8809 entry->orig = die;
8810 entry->copy = clone;
8811 *slot = entry;
8813 if (die->die_tag != DW_TAG_subprogram)
8814 FOR_EACH_CHILD (die, c,
8815 add_child_die (clone, clone_tree_partial (c, decl_table)));
8817 return clone;
8820 /* Walk the DIE and its children, looking for references to incomplete
8821 or trivial types that are unmarked (i.e., that are not in the current
8822 type_unit). */
8824 static void
8825 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
8827 dw_die_ref c;
8828 dw_attr_node *a;
8829 unsigned ix;
8831 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8833 if (AT_class (a) == dw_val_class_die_ref)
8835 dw_die_ref targ = AT_ref (a);
8836 decl_table_entry **slot;
8837 struct decl_table_entry *entry;
8839 if (targ->die_mark != 0 || targ->comdat_type_p)
8840 continue;
8842 slot = decl_table->find_slot_with_hash (targ,
8843 htab_hash_pointer (targ),
8844 INSERT);
8846 if (*slot != HTAB_EMPTY_ENTRY)
8848 /* TARG has already been copied, so we just need to
8849 modify the reference to point to the copy. */
8850 entry = *slot;
8851 a->dw_attr_val.v.val_die_ref.die = entry->copy;
8853 else
8855 dw_die_ref parent = unit;
8856 dw_die_ref copy = clone_die (targ);
8858 /* Record in DECL_TABLE that TARG has been copied.
8859 Need to do this now, before the recursive call,
8860 because DECL_TABLE may be expanded and SLOT
8861 would no longer be a valid pointer. */
8862 entry = XCNEW (struct decl_table_entry);
8863 entry->orig = targ;
8864 entry->copy = copy;
8865 *slot = entry;
8867 /* If TARG is not a declaration DIE, we need to copy its
8868 children. */
8869 if (!is_declaration_die (targ))
8871 FOR_EACH_CHILD (
8872 targ, c,
8873 add_child_die (copy,
8874 clone_tree_partial (c, decl_table)));
8877 /* Make sure the cloned tree is marked as part of the
8878 type unit. */
8879 mark_dies (copy);
8881 /* If TARG has surrounding context, copy its ancestor tree
8882 into the new type unit. */
8883 if (targ->die_parent != NULL
8884 && !is_unit_die (targ->die_parent))
8885 parent = copy_ancestor_tree (unit, targ->die_parent,
8886 decl_table);
8888 add_child_die (parent, copy);
8889 a->dw_attr_val.v.val_die_ref.die = copy;
8891 /* Make sure the newly-copied DIE is walked. If it was
8892 installed in a previously-added context, it won't
8893 get visited otherwise. */
8894 if (parent != unit)
8896 /* Find the highest point of the newly-added tree,
8897 mark each node along the way, and walk from there. */
8898 parent->die_mark = 1;
8899 while (parent->die_parent
8900 && parent->die_parent->die_mark == 0)
8902 parent = parent->die_parent;
8903 parent->die_mark = 1;
8905 copy_decls_walk (unit, parent, decl_table);
8911 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8914 /* Collect skeleton dies in DIE created by break_out_comdat_types already
8915 and record them in DECL_TABLE. */
8917 static void
8918 collect_skeleton_dies (dw_die_ref die, decl_hash_type *decl_table)
8920 dw_die_ref c;
8922 if (dw_attr_node *a = get_AT (die, DW_AT_signature))
8924 dw_die_ref targ = AT_ref (a);
8925 gcc_assert (targ->die_mark == 0 && targ->comdat_type_p);
8926 decl_table_entry **slot
8927 = decl_table->find_slot_with_hash (targ,
8928 htab_hash_pointer (targ),
8929 INSERT);
8930 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8931 /* Record in DECL_TABLE that TARG has been already copied
8932 by remove_child_or_replace_with_skeleton. */
8933 decl_table_entry *entry = XCNEW (struct decl_table_entry);
8934 entry->orig = targ;
8935 entry->copy = die;
8936 *slot = entry;
8938 FOR_EACH_CHILD (die, c, collect_skeleton_dies (c, decl_table));
8941 /* Copy declarations for "unworthy" types into the new comdat section.
8942 Incomplete types, modified types, and certain other types aren't broken
8943 out into comdat sections of their own, so they don't have a signature,
8944 and we need to copy the declaration into the same section so that we
8945 don't have an external reference. */
8947 static void
8948 copy_decls_for_unworthy_types (dw_die_ref unit)
8950 mark_dies (unit);
8951 decl_hash_type decl_table (10);
8952 collect_skeleton_dies (unit, &decl_table);
8953 copy_decls_walk (unit, unit, &decl_table);
8954 unmark_dies (unit);
8957 /* Traverse the DIE and add a sibling attribute if it may have the
8958 effect of speeding up access to siblings. To save some space,
8959 avoid generating sibling attributes for DIE's without children. */
8961 static void
8962 add_sibling_attributes (dw_die_ref die)
8964 dw_die_ref c;
8966 if (! die->die_child)
8967 return;
8969 if (die->die_parent && die != die->die_parent->die_child)
8970 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8972 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8975 /* Output all location lists for the DIE and its children. */
8977 static void
8978 output_location_lists (dw_die_ref die)
8980 dw_die_ref c;
8981 dw_attr_node *a;
8982 unsigned ix;
8984 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8985 if (AT_class (a) == dw_val_class_loc_list)
8986 output_loc_list (AT_loc_list (a));
8988 FOR_EACH_CHILD (die, c, output_location_lists (c));
8991 /* During assign_location_list_indexes and output_loclists_offset the
8992 current index, after it the number of assigned indexes (i.e. how
8993 large the .debug_loclists* offset table should be). */
8994 static unsigned int loc_list_idx;
8996 /* Output all location list offsets for the DIE and its children. */
8998 static void
8999 output_loclists_offsets (dw_die_ref die)
9001 dw_die_ref c;
9002 dw_attr_node *a;
9003 unsigned ix;
9005 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9006 if (AT_class (a) == dw_val_class_loc_list)
9008 dw_loc_list_ref l = AT_loc_list (a);
9009 if (l->offset_emitted)
9010 continue;
9011 dw2_asm_output_delta (dwarf_offset_size, l->ll_symbol,
9012 loc_section_label, NULL);
9013 gcc_assert (l->hash == loc_list_idx);
9014 loc_list_idx++;
9015 l->offset_emitted = true;
9018 FOR_EACH_CHILD (die, c, output_loclists_offsets (c));
9021 /* Recursively set indexes of location lists. */
9023 static void
9024 assign_location_list_indexes (dw_die_ref die)
9026 dw_die_ref c;
9027 dw_attr_node *a;
9028 unsigned ix;
9030 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9031 if (AT_class (a) == dw_val_class_loc_list)
9033 dw_loc_list_ref list = AT_loc_list (a);
9034 if (!list->num_assigned)
9036 list->num_assigned = true;
9037 list->hash = loc_list_idx++;
9041 FOR_EACH_CHILD (die, c, assign_location_list_indexes (c));
9044 /* We want to limit the number of external references, because they are
9045 larger than local references: a relocation takes multiple words, and
9046 even a sig8 reference is always eight bytes, whereas a local reference
9047 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
9048 So if we encounter multiple external references to the same type DIE, we
9049 make a local typedef stub for it and redirect all references there.
9051 This is the element of the hash table for keeping track of these
9052 references. */
9054 struct external_ref
9056 dw_die_ref type;
9057 dw_die_ref stub;
9058 unsigned n_refs;
9061 /* Hashtable helpers. */
9063 struct external_ref_hasher : free_ptr_hash <external_ref>
9065 static inline hashval_t hash (const external_ref *);
9066 static inline bool equal (const external_ref *, const external_ref *);
9069 inline hashval_t
9070 external_ref_hasher::hash (const external_ref *r)
9072 dw_die_ref die = r->type;
9073 hashval_t h = 0;
9075 /* We can't use the address of the DIE for hashing, because
9076 that will make the order of the stub DIEs non-deterministic. */
9077 if (! die->comdat_type_p)
9078 /* We have a symbol; use it to compute a hash. */
9079 h = htab_hash_string (die->die_id.die_symbol);
9080 else
9082 /* We have a type signature; use a subset of the bits as the hash.
9083 The 8-byte signature is at least as large as hashval_t. */
9084 comdat_type_node *type_node = die->die_id.die_type_node;
9085 memcpy (&h, type_node->signature, sizeof (h));
9087 return h;
9090 inline bool
9091 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
9093 return r1->type == r2->type;
9096 typedef hash_table<external_ref_hasher> external_ref_hash_type;
9098 /* Return a pointer to the external_ref for references to DIE. */
9100 static struct external_ref *
9101 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
9103 struct external_ref ref, *ref_p;
9104 external_ref **slot;
9106 ref.type = die;
9107 slot = map->find_slot (&ref, INSERT);
9108 if (*slot != HTAB_EMPTY_ENTRY)
9109 return *slot;
9111 ref_p = XCNEW (struct external_ref);
9112 ref_p->type = die;
9113 *slot = ref_p;
9114 return ref_p;
9117 /* Subroutine of optimize_external_refs, below.
9119 If we see a type skeleton, record it as our stub. If we see external
9120 references, remember how many we've seen. */
9122 static void
9123 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
9125 dw_die_ref c;
9126 dw_attr_node *a;
9127 unsigned ix;
9128 struct external_ref *ref_p;
9130 if (is_type_die (die)
9131 && (c = get_AT_ref (die, DW_AT_signature)))
9133 /* This is a local skeleton; use it for local references. */
9134 ref_p = lookup_external_ref (map, c);
9135 ref_p->stub = die;
9138 /* Scan the DIE references, and remember any that refer to DIEs from
9139 other CUs (i.e. those which are not marked). */
9140 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9141 if (AT_class (a) == dw_val_class_die_ref
9142 && (c = AT_ref (a))->die_mark == 0
9143 && is_type_die (c))
9145 ref_p = lookup_external_ref (map, c);
9146 ref_p->n_refs++;
9149 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
9152 /* htab_traverse callback function for optimize_external_refs, below. SLOT
9153 points to an external_ref, DATA is the CU we're processing. If we don't
9154 already have a local stub, and we have multiple refs, build a stub. */
9157 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
9159 struct external_ref *ref_p = *slot;
9161 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
9163 /* We have multiple references to this type, so build a small stub.
9164 Both of these forms are a bit dodgy from the perspective of the
9165 DWARF standard, since technically they should have names. */
9166 dw_die_ref cu = data;
9167 dw_die_ref type = ref_p->type;
9168 dw_die_ref stub = NULL;
9170 if (type->comdat_type_p)
9172 /* If we refer to this type via sig8, use AT_signature. */
9173 stub = new_die (type->die_tag, cu, NULL_TREE);
9174 add_AT_die_ref (stub, DW_AT_signature, type);
9176 else
9178 /* Otherwise, use a typedef with no name. */
9179 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
9180 add_AT_die_ref (stub, DW_AT_type, type);
9183 stub->die_mark++;
9184 ref_p->stub = stub;
9186 return 1;
9189 /* DIE is a unit; look through all the DIE references to see if there are
9190 any external references to types, and if so, create local stubs for
9191 them which will be applied in build_abbrev_table. This is useful because
9192 references to local DIEs are smaller. */
9194 static external_ref_hash_type *
9195 optimize_external_refs (dw_die_ref die)
9197 external_ref_hash_type *map = new external_ref_hash_type (10);
9198 optimize_external_refs_1 (die, map);
9199 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
9200 return map;
9203 /* The following 3 variables are temporaries that are computed only during the
9204 build_abbrev_table call and used and released during the following
9205 optimize_abbrev_table call. */
9207 /* First abbrev_id that can be optimized based on usage. */
9208 static unsigned int abbrev_opt_start;
9210 /* Maximum abbrev_id of a base type plus one (we can't optimize DIEs with
9211 abbrev_id smaller than this, because they must be already sized
9212 during build_abbrev_table). */
9213 static unsigned int abbrev_opt_base_type_end;
9215 /* Vector of usage counts during build_abbrev_table. Indexed by
9216 abbrev_id - abbrev_opt_start. */
9217 static vec<unsigned int> abbrev_usage_count;
9219 /* Vector of all DIEs added with die_abbrev >= abbrev_opt_start. */
9220 static vec<dw_die_ref> sorted_abbrev_dies;
9222 /* The format of each DIE (and its attribute value pairs) is encoded in an
9223 abbreviation table. This routine builds the abbreviation table and assigns
9224 a unique abbreviation id for each abbreviation entry. The children of each
9225 die are visited recursively. */
9227 static void
9228 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
9230 unsigned int abbrev_id = 0;
9231 dw_die_ref c;
9232 dw_attr_node *a;
9233 unsigned ix;
9234 dw_die_ref abbrev;
9236 /* Scan the DIE references, and replace any that refer to
9237 DIEs from other CUs (i.e. those which are not marked) with
9238 the local stubs we built in optimize_external_refs. */
9239 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9240 if (AT_class (a) == dw_val_class_die_ref
9241 && (c = AT_ref (a))->die_mark == 0)
9243 struct external_ref *ref_p;
9244 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
9246 if (is_type_die (c)
9247 && (ref_p = lookup_external_ref (extern_map, c))
9248 && ref_p->stub && ref_p->stub != die)
9250 gcc_assert (a->dw_attr != DW_AT_signature);
9251 change_AT_die_ref (a, ref_p->stub);
9253 else
9254 /* We aren't changing this reference, so mark it external. */
9255 set_AT_ref_external (a, 1);
9258 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
9260 dw_attr_node *die_a, *abbrev_a;
9261 unsigned ix;
9262 bool ok = true;
9264 if (abbrev_id == 0)
9265 continue;
9266 if (abbrev->die_tag != die->die_tag)
9267 continue;
9268 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
9269 continue;
9271 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
9272 continue;
9274 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
9276 abbrev_a = &(*abbrev->die_attr)[ix];
9277 if ((abbrev_a->dw_attr != die_a->dw_attr)
9278 || (value_format (abbrev_a) != value_format (die_a)))
9280 ok = false;
9281 break;
9284 if (ok)
9285 break;
9288 if (abbrev_id >= vec_safe_length (abbrev_die_table))
9290 vec_safe_push (abbrev_die_table, die);
9291 if (abbrev_opt_start)
9292 abbrev_usage_count.safe_push (0);
9294 if (abbrev_opt_start && abbrev_id >= abbrev_opt_start)
9296 abbrev_usage_count[abbrev_id - abbrev_opt_start]++;
9297 sorted_abbrev_dies.safe_push (die);
9300 die->die_abbrev = abbrev_id;
9301 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
9304 /* Callback function for sorted_abbrev_dies vector sorting. We sort
9305 by die_abbrev's usage count, from the most commonly used
9306 abbreviation to the least. */
9308 static int
9309 die_abbrev_cmp (const void *p1, const void *p2)
9311 dw_die_ref die1 = *(const dw_die_ref *) p1;
9312 dw_die_ref die2 = *(const dw_die_ref *) p2;
9314 gcc_checking_assert (die1->die_abbrev >= abbrev_opt_start);
9315 gcc_checking_assert (die2->die_abbrev >= abbrev_opt_start);
9317 if (die1->die_abbrev >= abbrev_opt_base_type_end
9318 && die2->die_abbrev >= abbrev_opt_base_type_end)
9320 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
9321 > abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
9322 return -1;
9323 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
9324 < abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
9325 return 1;
9328 /* Stabilize the sort. */
9329 if (die1->die_abbrev < die2->die_abbrev)
9330 return -1;
9331 if (die1->die_abbrev > die2->die_abbrev)
9332 return 1;
9334 return 0;
9337 /* Convert dw_val_class_const and dw_val_class_unsigned_const class attributes
9338 of DIEs in between sorted_abbrev_dies[first_id] and abbrev_dies[end_id - 1]
9339 into dw_val_class_const_implicit or
9340 dw_val_class_unsigned_const_implicit. */
9342 static void
9343 optimize_implicit_const (unsigned int first_id, unsigned int end,
9344 vec<bool> &implicit_consts)
9346 /* It never makes sense if there is just one DIE using the abbreviation. */
9347 if (end < first_id + 2)
9348 return;
9350 dw_attr_node *a;
9351 unsigned ix, i;
9352 dw_die_ref die = sorted_abbrev_dies[first_id];
9353 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9354 if (implicit_consts[ix])
9356 enum dw_val_class new_class = dw_val_class_none;
9357 switch (AT_class (a))
9359 case dw_val_class_unsigned_const:
9360 if ((HOST_WIDE_INT) AT_unsigned (a) < 0)
9361 continue;
9363 /* The .debug_abbrev section will grow by
9364 size_of_sleb128 (AT_unsigned (a)) and we avoid the constants
9365 in all the DIEs using that abbreviation. */
9366 if (constant_size (AT_unsigned (a)) * (end - first_id)
9367 <= (unsigned) size_of_sleb128 (AT_unsigned (a)))
9368 continue;
9370 new_class = dw_val_class_unsigned_const_implicit;
9371 break;
9373 case dw_val_class_const:
9374 new_class = dw_val_class_const_implicit;
9375 break;
9377 case dw_val_class_file:
9378 new_class = dw_val_class_file_implicit;
9379 break;
9381 default:
9382 continue;
9384 for (i = first_id; i < end; i++)
9385 (*sorted_abbrev_dies[i]->die_attr)[ix].dw_attr_val.val_class
9386 = new_class;
9390 /* Attempt to optimize abbreviation table from abbrev_opt_start
9391 abbreviation above. */
9393 static void
9394 optimize_abbrev_table (void)
9396 if (abbrev_opt_start
9397 && vec_safe_length (abbrev_die_table) > abbrev_opt_start
9398 && (dwarf_version >= 5 || vec_safe_length (abbrev_die_table) > 127))
9400 auto_vec<bool, 32> implicit_consts;
9401 sorted_abbrev_dies.qsort (die_abbrev_cmp);
9403 unsigned int abbrev_id = abbrev_opt_start - 1;
9404 unsigned int first_id = ~0U;
9405 unsigned int last_abbrev_id = 0;
9406 unsigned int i;
9407 dw_die_ref die;
9408 if (abbrev_opt_base_type_end > abbrev_opt_start)
9409 abbrev_id = abbrev_opt_base_type_end - 1;
9410 /* Reassign abbreviation ids from abbrev_opt_start above, so that
9411 most commonly used abbreviations come first. */
9412 FOR_EACH_VEC_ELT (sorted_abbrev_dies, i, die)
9414 dw_attr_node *a;
9415 unsigned ix;
9417 /* If calc_base_type_die_sizes has been called, the CU and
9418 base types after it can't be optimized, because we've already
9419 calculated their DIE offsets. We've sorted them first. */
9420 if (die->die_abbrev < abbrev_opt_base_type_end)
9421 continue;
9422 if (die->die_abbrev != last_abbrev_id)
9424 last_abbrev_id = die->die_abbrev;
9425 if (dwarf_version >= 5 && first_id != ~0U)
9426 optimize_implicit_const (first_id, i, implicit_consts);
9427 abbrev_id++;
9428 (*abbrev_die_table)[abbrev_id] = die;
9429 if (dwarf_version >= 5)
9431 first_id = i;
9432 implicit_consts.truncate (0);
9434 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9435 switch (AT_class (a))
9437 case dw_val_class_const:
9438 case dw_val_class_unsigned_const:
9439 case dw_val_class_file:
9440 implicit_consts.safe_push (true);
9441 break;
9442 default:
9443 implicit_consts.safe_push (false);
9444 break;
9448 else if (dwarf_version >= 5)
9450 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9451 if (!implicit_consts[ix])
9452 continue;
9453 else
9455 dw_attr_node *other_a
9456 = &(*(*abbrev_die_table)[abbrev_id]->die_attr)[ix];
9457 if (!dw_val_equal_p (&a->dw_attr_val,
9458 &other_a->dw_attr_val))
9459 implicit_consts[ix] = false;
9462 die->die_abbrev = abbrev_id;
9464 gcc_assert (abbrev_id == vec_safe_length (abbrev_die_table) - 1);
9465 if (dwarf_version >= 5 && first_id != ~0U)
9466 optimize_implicit_const (first_id, i, implicit_consts);
9469 abbrev_opt_start = 0;
9470 abbrev_opt_base_type_end = 0;
9471 abbrev_usage_count.release ();
9472 sorted_abbrev_dies.release ();
9475 /* Return the power-of-two number of bytes necessary to represent VALUE. */
9477 static int
9478 constant_size (unsigned HOST_WIDE_INT value)
9480 int log;
9482 if (value == 0)
9483 log = 0;
9484 else
9485 log = floor_log2 (value);
9487 log = log / 8;
9488 log = 1 << (floor_log2 (log) + 1);
9490 return log;
9493 /* Return the size of a DIE as it is represented in the
9494 .debug_info section. */
9496 static unsigned long
9497 size_of_die (dw_die_ref die)
9499 unsigned long size = 0;
9500 dw_attr_node *a;
9501 unsigned ix;
9502 enum dwarf_form form;
9504 size += size_of_uleb128 (die->die_abbrev);
9505 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9507 switch (AT_class (a))
9509 case dw_val_class_addr:
9510 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9512 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9513 size += size_of_uleb128 (AT_index (a));
9515 else
9516 size += DWARF2_ADDR_SIZE;
9517 break;
9518 case dw_val_class_offset:
9519 size += dwarf_offset_size;
9520 break;
9521 case dw_val_class_loc:
9523 unsigned long lsize = size_of_locs (AT_loc (a));
9525 /* Block length. */
9526 if (dwarf_version >= 4)
9527 size += size_of_uleb128 (lsize);
9528 else
9529 size += constant_size (lsize);
9530 size += lsize;
9532 break;
9533 case dw_val_class_loc_list:
9534 if (dwarf_split_debug_info && dwarf_version >= 5)
9536 gcc_assert (AT_loc_list (a)->num_assigned);
9537 size += size_of_uleb128 (AT_loc_list (a)->hash);
9539 else
9540 size += dwarf_offset_size;
9541 break;
9542 case dw_val_class_view_list:
9543 size += dwarf_offset_size;
9544 break;
9545 case dw_val_class_range_list:
9546 if (value_format (a) == DW_FORM_rnglistx)
9548 gcc_assert (rnglist_idx);
9549 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9550 size += size_of_uleb128 (r->idx);
9552 else
9553 size += dwarf_offset_size;
9554 break;
9555 case dw_val_class_const:
9556 size += size_of_sleb128 (AT_int (a));
9557 break;
9558 case dw_val_class_unsigned_const:
9560 int csize = constant_size (AT_unsigned (a));
9561 if (dwarf_version == 3
9562 && a->dw_attr == DW_AT_data_member_location
9563 && csize >= 4)
9564 size += size_of_uleb128 (AT_unsigned (a));
9565 else
9566 size += csize;
9568 break;
9569 case dw_val_class_symview:
9570 if (symview_upper_bound <= 0xff)
9571 size += 1;
9572 else if (symview_upper_bound <= 0xffff)
9573 size += 2;
9574 else if (symview_upper_bound <= 0xffffffff)
9575 size += 4;
9576 else
9577 size += 8;
9578 break;
9579 case dw_val_class_const_implicit:
9580 case dw_val_class_unsigned_const_implicit:
9581 case dw_val_class_file_implicit:
9582 /* These occupy no size in the DIE, just an extra sleb128 in
9583 .debug_abbrev. */
9584 break;
9585 case dw_val_class_const_double:
9586 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
9587 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
9588 size++; /* block */
9589 break;
9590 case dw_val_class_wide_int:
9591 size += (get_full_len (*a->dw_attr_val.v.val_wide)
9592 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
9593 if (get_full_len (*a->dw_attr_val.v.val_wide)
9594 * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
9595 size++; /* block */
9596 break;
9597 case dw_val_class_vec:
9598 size += constant_size (a->dw_attr_val.v.val_vec.length
9599 * a->dw_attr_val.v.val_vec.elt_size)
9600 + a->dw_attr_val.v.val_vec.length
9601 * a->dw_attr_val.v.val_vec.elt_size; /* block */
9602 break;
9603 case dw_val_class_flag:
9604 if (dwarf_version >= 4)
9605 /* Currently all add_AT_flag calls pass in 1 as last argument,
9606 so DW_FORM_flag_present can be used. If that ever changes,
9607 we'll need to use DW_FORM_flag and have some optimization
9608 in build_abbrev_table that will change those to
9609 DW_FORM_flag_present if it is set to 1 in all DIEs using
9610 the same abbrev entry. */
9611 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9612 else
9613 size += 1;
9614 break;
9615 case dw_val_class_die_ref:
9616 if (AT_ref_external (a))
9618 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
9619 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
9620 is sized by target address length, whereas in DWARF3
9621 it's always sized as an offset. */
9622 if (AT_ref (a)->comdat_type_p)
9623 size += DWARF_TYPE_SIGNATURE_SIZE;
9624 else if (dwarf_version == 2)
9625 size += DWARF2_ADDR_SIZE;
9626 else
9627 size += dwarf_offset_size;
9629 else
9630 size += dwarf_offset_size;
9631 break;
9632 case dw_val_class_fde_ref:
9633 size += dwarf_offset_size;
9634 break;
9635 case dw_val_class_lbl_id:
9636 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9638 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9639 size += size_of_uleb128 (AT_index (a));
9641 else
9642 size += DWARF2_ADDR_SIZE;
9643 break;
9644 case dw_val_class_lineptr:
9645 case dw_val_class_macptr:
9646 case dw_val_class_loclistsptr:
9647 size += dwarf_offset_size;
9648 break;
9649 case dw_val_class_str:
9650 form = AT_string_form (a);
9651 if (form == DW_FORM_strp || form == DW_FORM_line_strp)
9652 size += dwarf_offset_size;
9653 else if (form == dwarf_FORM (DW_FORM_strx))
9654 size += size_of_uleb128 (AT_index (a));
9655 else
9656 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
9657 break;
9658 case dw_val_class_file:
9659 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
9660 break;
9661 case dw_val_class_data8:
9662 size += 8;
9663 break;
9664 case dw_val_class_vms_delta:
9665 size += dwarf_offset_size;
9666 break;
9667 case dw_val_class_high_pc:
9668 size += DWARF2_ADDR_SIZE;
9669 break;
9670 case dw_val_class_discr_value:
9671 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
9672 break;
9673 case dw_val_class_discr_list:
9675 unsigned block_size = size_of_discr_list (AT_discr_list (a));
9677 /* This is a block, so we have the block length and then its
9678 data. */
9679 size += constant_size (block_size) + block_size;
9681 break;
9682 default:
9683 gcc_unreachable ();
9687 return size;
9690 /* Size the debugging information associated with a given DIE. Visits the
9691 DIE's children recursively. Updates the global variable next_die_offset, on
9692 each time through. Uses the current value of next_die_offset to update the
9693 die_offset field in each DIE. */
9695 static void
9696 calc_die_sizes (dw_die_ref die)
9698 dw_die_ref c;
9700 gcc_assert (die->die_offset == 0
9701 || (unsigned long int) die->die_offset == next_die_offset);
9702 die->die_offset = next_die_offset;
9703 next_die_offset += size_of_die (die);
9705 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
9707 if (die->die_child != NULL)
9708 /* Count the null byte used to terminate sibling lists. */
9709 next_die_offset += 1;
9712 /* Size just the base type children at the start of the CU.
9713 This is needed because build_abbrev needs to size locs
9714 and sizing of type based stack ops needs to know die_offset
9715 values for the base types. */
9717 static void
9718 calc_base_type_die_sizes (void)
9720 unsigned long die_offset = (dwarf_split_debug_info
9721 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
9722 : DWARF_COMPILE_UNIT_HEADER_SIZE);
9723 unsigned int i;
9724 dw_die_ref base_type;
9725 #if ENABLE_ASSERT_CHECKING
9726 dw_die_ref prev = comp_unit_die ()->die_child;
9727 #endif
9729 die_offset += size_of_die (comp_unit_die ());
9730 for (i = 0; base_types.iterate (i, &base_type); i++)
9732 #if ENABLE_ASSERT_CHECKING
9733 gcc_assert (base_type->die_offset == 0
9734 && prev->die_sib == base_type
9735 && base_type->die_child == NULL
9736 && base_type->die_abbrev);
9737 prev = base_type;
9738 #endif
9739 if (abbrev_opt_start
9740 && base_type->die_abbrev >= abbrev_opt_base_type_end)
9741 abbrev_opt_base_type_end = base_type->die_abbrev + 1;
9742 base_type->die_offset = die_offset;
9743 die_offset += size_of_die (base_type);
9747 /* Set the marks for a die and its children. We do this so
9748 that we know whether or not a reference needs to use FORM_ref_addr; only
9749 DIEs in the same CU will be marked. We used to clear out the offset
9750 and use that as the flag, but ran into ordering problems. */
9752 static void
9753 mark_dies (dw_die_ref die)
9755 dw_die_ref c;
9757 gcc_assert (!die->die_mark);
9759 die->die_mark = 1;
9760 FOR_EACH_CHILD (die, c, mark_dies (c));
9763 /* Clear the marks for a die and its children. */
9765 static void
9766 unmark_dies (dw_die_ref die)
9768 dw_die_ref c;
9770 if (! use_debug_types)
9771 gcc_assert (die->die_mark);
9773 die->die_mark = 0;
9774 FOR_EACH_CHILD (die, c, unmark_dies (c));
9777 /* Clear the marks for a die, its children and referred dies. */
9779 static void
9780 unmark_all_dies (dw_die_ref die)
9782 dw_die_ref c;
9783 dw_attr_node *a;
9784 unsigned ix;
9786 if (!die->die_mark)
9787 return;
9788 die->die_mark = 0;
9790 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
9792 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9793 if (AT_class (a) == dw_val_class_die_ref)
9794 unmark_all_dies (AT_ref (a));
9797 /* Calculate if the entry should appear in the final output file. It may be
9798 from a pruned a type. */
9800 static bool
9801 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
9803 /* By limiting gnu pubnames to definitions only, gold can generate a
9804 gdb index without entries for declarations, which don't include
9805 enough information to be useful. */
9806 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
9807 return false;
9809 if (table == pubname_table)
9811 /* Enumerator names are part of the pubname table, but the
9812 parent DW_TAG_enumeration_type die may have been pruned.
9813 Don't output them if that is the case. */
9814 if (p->die->die_tag == DW_TAG_enumerator &&
9815 (p->die->die_parent == NULL
9816 || !p->die->die_parent->die_perennial_p))
9817 return false;
9819 /* Everything else in the pubname table is included. */
9820 return true;
9823 /* The pubtypes table shouldn't include types that have been
9824 pruned. */
9825 return (p->die->die_offset != 0
9826 || !flag_eliminate_unused_debug_types);
9829 /* Return the size of the .debug_pubnames or .debug_pubtypes table
9830 generated for the compilation unit. */
9832 static unsigned long
9833 size_of_pubnames (vec<pubname_entry, va_gc> *names)
9835 unsigned long size;
9836 unsigned i;
9837 pubname_entry *p;
9838 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
9840 size = DWARF_PUBNAMES_HEADER_SIZE;
9841 FOR_EACH_VEC_ELT (*names, i, p)
9842 if (include_pubname_in_output (names, p))
9843 size += strlen (p->name) + dwarf_offset_size + 1 + space_for_flags;
9845 size += dwarf_offset_size;
9846 return size;
9849 /* Return the size of the information in the .debug_aranges section. */
9851 static unsigned long
9852 size_of_aranges (void)
9854 unsigned long size;
9856 size = DWARF_ARANGES_HEADER_SIZE;
9858 /* Count the address/length pair for this compilation unit. */
9859 if (switch_text_ranges)
9860 size += 2 * DWARF2_ADDR_SIZE
9861 * (vec_safe_length (switch_text_ranges) / 2 + 1);
9862 if (switch_cold_ranges)
9863 size += 2 * DWARF2_ADDR_SIZE
9864 * (vec_safe_length (switch_cold_ranges) / 2 + 1);
9865 if (have_multiple_function_sections)
9867 unsigned fde_idx;
9868 dw_fde_ref fde;
9870 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9872 if (fde->ignored_debug)
9873 continue;
9874 if (!fde->in_std_section)
9875 size += 2 * DWARF2_ADDR_SIZE;
9876 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9877 size += 2 * DWARF2_ADDR_SIZE;
9881 /* Count the two zero words used to terminated the address range table. */
9882 size += 2 * DWARF2_ADDR_SIZE;
9883 return size;
9886 /* Select the encoding of an attribute value. */
9888 static enum dwarf_form
9889 value_format (dw_attr_node *a)
9891 switch (AT_class (a))
9893 case dw_val_class_addr:
9894 /* Only very few attributes allow DW_FORM_addr. */
9895 switch (a->dw_attr)
9897 case DW_AT_low_pc:
9898 case DW_AT_high_pc:
9899 case DW_AT_entry_pc:
9900 case DW_AT_trampoline:
9901 return (AT_index (a) == NOT_INDEXED
9902 ? DW_FORM_addr : dwarf_FORM (DW_FORM_addrx));
9903 default:
9904 break;
9906 switch (DWARF2_ADDR_SIZE)
9908 case 1:
9909 return DW_FORM_data1;
9910 case 2:
9911 return DW_FORM_data2;
9912 case 4:
9913 return DW_FORM_data4;
9914 case 8:
9915 return DW_FORM_data8;
9916 default:
9917 gcc_unreachable ();
9919 case dw_val_class_loc_list:
9920 if (dwarf_split_debug_info
9921 && dwarf_version >= 5
9922 && AT_loc_list (a)->num_assigned)
9923 return DW_FORM_loclistx;
9924 /* FALLTHRU */
9925 case dw_val_class_view_list:
9926 case dw_val_class_range_list:
9927 /* For range lists in DWARF 5, use DW_FORM_rnglistx from .debug_info.dwo
9928 but in .debug_info use DW_FORM_sec_offset, which is shorter if we
9929 care about sizes of .debug* sections in shared libraries and
9930 executables and don't take into account relocations that affect just
9931 relocatable objects - for DW_FORM_rnglistx we'd have to emit offset
9932 table in the .debug_rnglists section. */
9933 if (dwarf_split_debug_info
9934 && dwarf_version >= 5
9935 && AT_class (a) == dw_val_class_range_list
9936 && rnglist_idx
9937 && a->dw_attr_val.val_entry != RELOCATED_OFFSET)
9938 return DW_FORM_rnglistx;
9939 if (dwarf_version >= 4)
9940 return DW_FORM_sec_offset;
9941 /* FALLTHRU */
9942 case dw_val_class_vms_delta:
9943 case dw_val_class_offset:
9944 switch (dwarf_offset_size)
9946 case 4:
9947 return DW_FORM_data4;
9948 case 8:
9949 return DW_FORM_data8;
9950 default:
9951 gcc_unreachable ();
9953 case dw_val_class_loc:
9954 if (dwarf_version >= 4)
9955 return DW_FORM_exprloc;
9956 switch (constant_size (size_of_locs (AT_loc (a))))
9958 case 1:
9959 return DW_FORM_block1;
9960 case 2:
9961 return DW_FORM_block2;
9962 case 4:
9963 return DW_FORM_block4;
9964 default:
9965 gcc_unreachable ();
9967 case dw_val_class_const:
9968 return DW_FORM_sdata;
9969 case dw_val_class_unsigned_const:
9970 switch (constant_size (AT_unsigned (a)))
9972 case 1:
9973 return DW_FORM_data1;
9974 case 2:
9975 return DW_FORM_data2;
9976 case 4:
9977 /* In DWARF3 DW_AT_data_member_location with
9978 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
9979 constant, so we need to use DW_FORM_udata if we need
9980 a large constant. */
9981 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9982 return DW_FORM_udata;
9983 return DW_FORM_data4;
9984 case 8:
9985 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9986 return DW_FORM_udata;
9987 return DW_FORM_data8;
9988 default:
9989 gcc_unreachable ();
9991 case dw_val_class_const_implicit:
9992 case dw_val_class_unsigned_const_implicit:
9993 case dw_val_class_file_implicit:
9994 return DW_FORM_implicit_const;
9995 case dw_val_class_const_double:
9996 switch (HOST_BITS_PER_WIDE_INT)
9998 case 8:
9999 return DW_FORM_data2;
10000 case 16:
10001 return DW_FORM_data4;
10002 case 32:
10003 return DW_FORM_data8;
10004 case 64:
10005 if (dwarf_version >= 5)
10006 return DW_FORM_data16;
10007 /* FALLTHRU */
10008 default:
10009 return DW_FORM_block1;
10011 case dw_val_class_wide_int:
10012 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
10014 case 8:
10015 return DW_FORM_data1;
10016 case 16:
10017 return DW_FORM_data2;
10018 case 32:
10019 return DW_FORM_data4;
10020 case 64:
10021 return DW_FORM_data8;
10022 case 128:
10023 if (dwarf_version >= 5)
10024 return DW_FORM_data16;
10025 /* FALLTHRU */
10026 default:
10027 return DW_FORM_block1;
10029 case dw_val_class_symview:
10030 /* ??? We might use uleb128, but then we'd have to compute
10031 .debug_info offsets in the assembler. */
10032 if (symview_upper_bound <= 0xff)
10033 return DW_FORM_data1;
10034 else if (symview_upper_bound <= 0xffff)
10035 return DW_FORM_data2;
10036 else if (symview_upper_bound <= 0xffffffff)
10037 return DW_FORM_data4;
10038 else
10039 return DW_FORM_data8;
10040 case dw_val_class_vec:
10041 switch (constant_size (a->dw_attr_val.v.val_vec.length
10042 * a->dw_attr_val.v.val_vec.elt_size))
10044 case 1:
10045 return DW_FORM_block1;
10046 case 2:
10047 return DW_FORM_block2;
10048 case 4:
10049 return DW_FORM_block4;
10050 default:
10051 gcc_unreachable ();
10053 case dw_val_class_flag:
10054 if (dwarf_version >= 4)
10056 /* Currently all add_AT_flag calls pass in 1 as last argument,
10057 so DW_FORM_flag_present can be used. If that ever changes,
10058 we'll need to use DW_FORM_flag and have some optimization
10059 in build_abbrev_table that will change those to
10060 DW_FORM_flag_present if it is set to 1 in all DIEs using
10061 the same abbrev entry. */
10062 gcc_assert (a->dw_attr_val.v.val_flag == 1);
10063 return DW_FORM_flag_present;
10065 return DW_FORM_flag;
10066 case dw_val_class_die_ref:
10067 if (AT_ref_external (a))
10069 if (AT_ref (a)->comdat_type_p)
10070 return DW_FORM_ref_sig8;
10071 else
10072 return DW_FORM_ref_addr;
10074 else
10075 return DW_FORM_ref;
10076 case dw_val_class_fde_ref:
10077 return DW_FORM_data;
10078 case dw_val_class_lbl_id:
10079 return (AT_index (a) == NOT_INDEXED
10080 ? DW_FORM_addr : dwarf_FORM (DW_FORM_addrx));
10081 case dw_val_class_lineptr:
10082 case dw_val_class_macptr:
10083 case dw_val_class_loclistsptr:
10084 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
10085 case dw_val_class_str:
10086 return AT_string_form (a);
10087 case dw_val_class_file:
10088 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
10090 case 1:
10091 return DW_FORM_data1;
10092 case 2:
10093 return DW_FORM_data2;
10094 case 4:
10095 return DW_FORM_data4;
10096 default:
10097 gcc_unreachable ();
10100 case dw_val_class_data8:
10101 return DW_FORM_data8;
10103 case dw_val_class_high_pc:
10104 switch (DWARF2_ADDR_SIZE)
10106 case 1:
10107 return DW_FORM_data1;
10108 case 2:
10109 return DW_FORM_data2;
10110 case 4:
10111 return DW_FORM_data4;
10112 case 8:
10113 return DW_FORM_data8;
10114 default:
10115 gcc_unreachable ();
10118 case dw_val_class_discr_value:
10119 return (a->dw_attr_val.v.val_discr_value.pos
10120 ? DW_FORM_udata
10121 : DW_FORM_sdata);
10122 case dw_val_class_discr_list:
10123 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
10125 case 1:
10126 return DW_FORM_block1;
10127 case 2:
10128 return DW_FORM_block2;
10129 case 4:
10130 return DW_FORM_block4;
10131 default:
10132 gcc_unreachable ();
10135 default:
10136 gcc_unreachable ();
10140 /* Output the encoding of an attribute value. */
10142 static void
10143 output_value_format (dw_attr_node *a)
10145 enum dwarf_form form = value_format (a);
10147 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
10150 /* Given a die and id, produce the appropriate abbreviations. */
10152 static void
10153 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
10155 unsigned ix;
10156 dw_attr_node *a_attr;
10158 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
10159 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
10160 dwarf_tag_name (abbrev->die_tag));
10162 if (abbrev->die_child != NULL)
10163 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
10164 else
10165 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
10167 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
10169 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
10170 dwarf_attr_name (a_attr->dw_attr));
10171 output_value_format (a_attr);
10172 if (value_format (a_attr) == DW_FORM_implicit_const)
10174 if (AT_class (a_attr) == dw_val_class_file_implicit)
10176 int f = maybe_emit_file (a_attr->dw_attr_val.v.val_file);
10177 const char *filename = a_attr->dw_attr_val.v.val_file->filename;
10178 dw2_asm_output_data_sleb128 (f, "(%s)", filename);
10180 else
10181 dw2_asm_output_data_sleb128 (a_attr->dw_attr_val.v.val_int, NULL);
10185 dw2_asm_output_data (1, 0, NULL);
10186 dw2_asm_output_data (1, 0, NULL);
10190 /* Output the .debug_abbrev section which defines the DIE abbreviation
10191 table. */
10193 static void
10194 output_abbrev_section (void)
10196 unsigned int abbrev_id;
10197 dw_die_ref abbrev;
10199 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
10200 if (abbrev_id != 0)
10201 output_die_abbrevs (abbrev_id, abbrev);
10203 /* Terminate the table. */
10204 dw2_asm_output_data (1, 0, NULL);
10207 /* Return a new location list, given the begin and end range, and the
10208 expression. */
10210 static inline dw_loc_list_ref
10211 new_loc_list (dw_loc_descr_ref expr, const char *begin, var_loc_view vbegin,
10212 const char *end, var_loc_view vend,
10213 const char *section)
10215 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
10217 retlist->begin = begin;
10218 retlist->begin_entry = NULL;
10219 retlist->end = end;
10220 retlist->end_entry = NULL;
10221 retlist->expr = expr;
10222 retlist->section = section;
10223 retlist->vbegin = vbegin;
10224 retlist->vend = vend;
10226 return retlist;
10229 /* Return true iff there's any nonzero view number in the loc list.
10231 ??? When views are not enabled, we'll often extend a single range
10232 to the entire function, so that we emit a single location
10233 expression rather than a location list. With views, even with a
10234 single range, we'll output a list if start or end have a nonzero
10235 view. If we change this, we may want to stop splitting a single
10236 range in dw_loc_list just because of a nonzero view, even if it
10237 straddles across hot/cold partitions. */
10239 static bool
10240 loc_list_has_views (dw_loc_list_ref list)
10242 if (!debug_variable_location_views)
10243 return false;
10245 for (dw_loc_list_ref loc = list;
10246 loc != NULL; loc = loc->dw_loc_next)
10247 if (!ZERO_VIEW_P (loc->vbegin) || !ZERO_VIEW_P (loc->vend))
10248 return true;
10250 return false;
10253 /* Generate a new internal symbol for this location list node, if it
10254 hasn't got one yet. */
10256 static inline void
10257 gen_llsym (dw_loc_list_ref list)
10259 gcc_assert (!list->ll_symbol);
10260 list->ll_symbol = gen_internal_sym ("LLST");
10262 if (!loc_list_has_views (list))
10263 return;
10265 if (dwarf2out_locviews_in_attribute ())
10267 /* Use the same label_num for the view list. */
10268 label_num--;
10269 list->vl_symbol = gen_internal_sym ("LVUS");
10271 else
10272 list->vl_symbol = list->ll_symbol;
10275 /* Generate a symbol for the list, but only if we really want to emit
10276 it as a list. */
10278 static inline void
10279 maybe_gen_llsym (dw_loc_list_ref list)
10281 if (!list || (!list->dw_loc_next && !loc_list_has_views (list)))
10282 return;
10284 gen_llsym (list);
10287 /* Determine whether or not to skip loc_list entry CURR. If SIZEP is
10288 NULL, don't consider size of the location expression. If we're not
10289 to skip it, and SIZEP is non-null, store the size of CURR->expr's
10290 representation in *SIZEP. */
10292 static bool
10293 skip_loc_list_entry (dw_loc_list_ref curr, unsigned long *sizep = NULL)
10295 /* Don't output an entry that starts and ends at the same address. */
10296 if (strcmp (curr->begin, curr->end) == 0
10297 && curr->vbegin == curr->vend && !curr->force)
10298 return true;
10300 if (!sizep)
10301 return false;
10303 unsigned long size = size_of_locs (curr->expr);
10305 /* If the expression is too large, drop it on the floor. We could
10306 perhaps put it into DW_TAG_dwarf_procedure and refer to that
10307 in the expression, but >= 64KB expressions for a single value
10308 in a single range are unlikely very useful. */
10309 if (dwarf_version < 5 && size > 0xffff)
10310 return true;
10312 *sizep = size;
10314 return false;
10317 /* Output a view pair loclist entry for CURR, if it requires one. */
10319 static void
10320 dwarf2out_maybe_output_loclist_view_pair (dw_loc_list_ref curr)
10322 if (!dwarf2out_locviews_in_loclist ())
10323 return;
10325 if (ZERO_VIEW_P (curr->vbegin) && ZERO_VIEW_P (curr->vend))
10326 return;
10328 #ifdef DW_LLE_view_pair
10329 dw2_asm_output_data (1, DW_LLE_view_pair, "DW_LLE_view_pair");
10331 if (dwarf2out_as_locview_support)
10333 if (ZERO_VIEW_P (curr->vbegin))
10334 dw2_asm_output_data_uleb128 (0, "Location view begin");
10335 else
10337 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10338 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vbegin);
10339 dw2_asm_output_symname_uleb128 (label, "Location view begin");
10342 if (ZERO_VIEW_P (curr->vend))
10343 dw2_asm_output_data_uleb128 (0, "Location view end");
10344 else
10346 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10347 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vend);
10348 dw2_asm_output_symname_uleb128 (label, "Location view end");
10351 else
10353 dw2_asm_output_data_uleb128 (curr->vbegin, "Location view begin");
10354 dw2_asm_output_data_uleb128 (curr->vend, "Location view end");
10356 #endif /* DW_LLE_view_pair */
10358 return;
10361 /* Output the location list given to us. */
10363 static void
10364 output_loc_list (dw_loc_list_ref list_head)
10366 int vcount = 0, lcount = 0;
10368 if (list_head->emitted)
10369 return;
10370 list_head->emitted = true;
10372 if (list_head->vl_symbol && dwarf2out_locviews_in_attribute ())
10374 ASM_OUTPUT_LABEL (asm_out_file, list_head->vl_symbol);
10376 for (dw_loc_list_ref curr = list_head; curr != NULL;
10377 curr = curr->dw_loc_next)
10379 unsigned long size;
10381 if (skip_loc_list_entry (curr, &size))
10382 continue;
10384 vcount++;
10386 /* ?? dwarf_split_debug_info? */
10387 if (dwarf2out_as_locview_support)
10389 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10391 if (!ZERO_VIEW_P (curr->vbegin))
10393 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vbegin);
10394 dw2_asm_output_symname_uleb128 (label,
10395 "View list begin (%s)",
10396 list_head->vl_symbol);
10398 else
10399 dw2_asm_output_data_uleb128 (0,
10400 "View list begin (%s)",
10401 list_head->vl_symbol);
10403 if (!ZERO_VIEW_P (curr->vend))
10405 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vend);
10406 dw2_asm_output_symname_uleb128 (label,
10407 "View list end (%s)",
10408 list_head->vl_symbol);
10410 else
10411 dw2_asm_output_data_uleb128 (0,
10412 "View list end (%s)",
10413 list_head->vl_symbol);
10415 else
10417 dw2_asm_output_data_uleb128 (curr->vbegin,
10418 "View list begin (%s)",
10419 list_head->vl_symbol);
10420 dw2_asm_output_data_uleb128 (curr->vend,
10421 "View list end (%s)",
10422 list_head->vl_symbol);
10427 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
10429 const char *last_section = NULL;
10430 const char *base_label = NULL;
10432 /* Walk the location list, and output each range + expression. */
10433 for (dw_loc_list_ref curr = list_head; curr != NULL;
10434 curr = curr->dw_loc_next)
10436 unsigned long size;
10438 /* Skip this entry? If we skip it here, we must skip it in the
10439 view list above as well. */
10440 if (skip_loc_list_entry (curr, &size))
10441 continue;
10443 lcount++;
10445 if (dwarf_version >= 5)
10447 if (dwarf_split_debug_info && HAVE_AS_LEB128)
10449 dwarf2out_maybe_output_loclist_view_pair (curr);
10450 /* For -gsplit-dwarf, emit DW_LLE_startx_length, which has
10451 uleb128 index into .debug_addr and uleb128 length. */
10452 dw2_asm_output_data (1, DW_LLE_startx_length,
10453 "DW_LLE_startx_length (%s)",
10454 list_head->ll_symbol);
10455 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10456 "Location list range start index "
10457 "(%s)", curr->begin);
10458 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
10459 "Location list length (%s)",
10460 list_head->ll_symbol);
10462 else if (dwarf_split_debug_info)
10464 dwarf2out_maybe_output_loclist_view_pair (curr);
10465 /* For -gsplit-dwarf without usable .uleb128 support, emit
10466 DW_LLE_startx_endx, which has two uleb128 indexes into
10467 .debug_addr. */
10468 dw2_asm_output_data (1, DW_LLE_startx_endx,
10469 "DW_LLE_startx_endx (%s)",
10470 list_head->ll_symbol);
10471 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10472 "Location list range start index "
10473 "(%s)", curr->begin);
10474 dw2_asm_output_data_uleb128 (curr->end_entry->index,
10475 "Location list range end index "
10476 "(%s)", curr->end);
10478 else if (!have_multiple_function_sections && HAVE_AS_LEB128)
10480 dwarf2out_maybe_output_loclist_view_pair (curr);
10481 /* If all code is in .text section, the base address is
10482 already provided by the CU attributes. Use
10483 DW_LLE_offset_pair where both addresses are uleb128 encoded
10484 offsets against that base. */
10485 dw2_asm_output_data (1, DW_LLE_offset_pair,
10486 "DW_LLE_offset_pair (%s)",
10487 list_head->ll_symbol);
10488 dw2_asm_output_delta_uleb128 (curr->begin, curr->section,
10489 "Location list begin address (%s)",
10490 list_head->ll_symbol);
10491 dw2_asm_output_delta_uleb128 (curr->end, curr->section,
10492 "Location list end address (%s)",
10493 list_head->ll_symbol);
10495 else if (HAVE_AS_LEB128)
10497 /* Otherwise, find out how many consecutive entries could share
10498 the same base entry. If just one, emit DW_LLE_start_length,
10499 otherwise emit DW_LLE_base_address for the base address
10500 followed by a series of DW_LLE_offset_pair. */
10501 if (last_section == NULL || curr->section != last_section)
10503 dw_loc_list_ref curr2;
10504 for (curr2 = curr->dw_loc_next; curr2 != NULL;
10505 curr2 = curr2->dw_loc_next)
10507 if (strcmp (curr2->begin, curr2->end) == 0
10508 && !curr2->force)
10509 continue;
10510 break;
10512 if (curr2 == NULL || curr->section != curr2->section)
10513 last_section = NULL;
10514 else
10516 last_section = curr->section;
10517 base_label = curr->begin;
10518 dw2_asm_output_data (1, DW_LLE_base_address,
10519 "DW_LLE_base_address (%s)",
10520 list_head->ll_symbol);
10521 dw2_asm_output_addr (DWARF2_ADDR_SIZE, base_label,
10522 "Base address (%s)",
10523 list_head->ll_symbol);
10526 /* Only one entry with the same base address. Use
10527 DW_LLE_start_length with absolute address and uleb128
10528 length. */
10529 if (last_section == NULL)
10531 dwarf2out_maybe_output_loclist_view_pair (curr);
10532 dw2_asm_output_data (1, DW_LLE_start_length,
10533 "DW_LLE_start_length (%s)",
10534 list_head->ll_symbol);
10535 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10536 "Location list begin address (%s)",
10537 list_head->ll_symbol);
10538 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
10539 "Location list length "
10540 "(%s)", list_head->ll_symbol);
10542 /* Otherwise emit DW_LLE_offset_pair, relative to above emitted
10543 DW_LLE_base_address. */
10544 else
10546 dwarf2out_maybe_output_loclist_view_pair (curr);
10547 dw2_asm_output_data (1, DW_LLE_offset_pair,
10548 "DW_LLE_offset_pair (%s)",
10549 list_head->ll_symbol);
10550 dw2_asm_output_delta_uleb128 (curr->begin, base_label,
10551 "Location list begin address "
10552 "(%s)", list_head->ll_symbol);
10553 dw2_asm_output_delta_uleb128 (curr->end, base_label,
10554 "Location list end address "
10555 "(%s)", list_head->ll_symbol);
10558 /* The assembler does not support .uleb128 directive. Emit
10559 DW_LLE_start_end with a pair of absolute addresses. */
10560 else
10562 dwarf2out_maybe_output_loclist_view_pair (curr);
10563 dw2_asm_output_data (1, DW_LLE_start_end,
10564 "DW_LLE_start_end (%s)",
10565 list_head->ll_symbol);
10566 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10567 "Location list begin address (%s)",
10568 list_head->ll_symbol);
10569 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10570 "Location list end address (%s)",
10571 list_head->ll_symbol);
10574 else if (dwarf_split_debug_info)
10576 /* For -gsplit-dwarf -gdwarf-{2,3,4} emit index into .debug_addr
10577 and 4 byte length. */
10578 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
10579 "Location list start/length entry (%s)",
10580 list_head->ll_symbol);
10581 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10582 "Location list range start index (%s)",
10583 curr->begin);
10584 /* The length field is 4 bytes. If we ever need to support
10585 an 8-byte length, we can add a new DW_LLE code or fall back
10586 to DW_LLE_GNU_start_end_entry. */
10587 dw2_asm_output_delta (4, curr->end, curr->begin,
10588 "Location list range length (%s)",
10589 list_head->ll_symbol);
10591 else if (!have_multiple_function_sections)
10593 /* Pair of relative addresses against start of text section. */
10594 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
10595 "Location list begin address (%s)",
10596 list_head->ll_symbol);
10597 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
10598 "Location list end address (%s)",
10599 list_head->ll_symbol);
10601 else
10603 /* Pair of absolute addresses. */
10604 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10605 "Location list begin address (%s)",
10606 list_head->ll_symbol);
10607 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10608 "Location list end address (%s)",
10609 list_head->ll_symbol);
10612 /* Output the block length for this list of location operations. */
10613 if (dwarf_version >= 5)
10614 dw2_asm_output_data_uleb128 (size, "Location expression size");
10615 else
10617 gcc_assert (size <= 0xffff);
10618 dw2_asm_output_data (2, size, "Location expression size");
10621 output_loc_sequence (curr->expr, -1);
10624 /* And finally list termination. */
10625 if (dwarf_version >= 5)
10626 dw2_asm_output_data (1, DW_LLE_end_of_list,
10627 "DW_LLE_end_of_list (%s)", list_head->ll_symbol);
10628 else if (dwarf_split_debug_info)
10629 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
10630 "Location list terminator (%s)",
10631 list_head->ll_symbol);
10632 else
10634 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10635 "Location list terminator begin (%s)",
10636 list_head->ll_symbol);
10637 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10638 "Location list terminator end (%s)",
10639 list_head->ll_symbol);
10642 gcc_assert (!list_head->vl_symbol
10643 || vcount == lcount * (dwarf2out_locviews_in_attribute () ? 1 : 0));
10646 /* Output a range_list offset into the .debug_ranges or .debug_rnglists
10647 section. Emit a relocated reference if val_entry is NULL, otherwise,
10648 emit an indirect reference. */
10650 static void
10651 output_range_list_offset (dw_attr_node *a)
10653 const char *name = dwarf_attr_name (a->dw_attr);
10655 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
10657 if (dwarf_version >= 5)
10659 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
10660 dw2_asm_output_offset (dwarf_offset_size, r->label,
10661 debug_ranges_section, "%s", name);
10663 else
10665 char *p = strchr (ranges_section_label, '\0');
10666 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
10667 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE);
10668 dw2_asm_output_offset (dwarf_offset_size, ranges_section_label,
10669 debug_ranges_section, "%s", name);
10670 *p = '\0';
10673 else if (dwarf_version >= 5)
10675 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
10676 gcc_assert (rnglist_idx);
10677 dw2_asm_output_data_uleb128 (r->idx, "%s", name);
10679 else
10680 dw2_asm_output_data (dwarf_offset_size,
10681 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE,
10682 "%s (offset from %s)", name, ranges_section_label);
10685 /* Output the offset into the debug_loc section. */
10687 static void
10688 output_loc_list_offset (dw_attr_node *a)
10690 char *sym = AT_loc_list (a)->ll_symbol;
10692 gcc_assert (sym);
10693 if (!dwarf_split_debug_info)
10694 dw2_asm_output_offset (dwarf_offset_size, sym, debug_loc_section,
10695 "%s", dwarf_attr_name (a->dw_attr));
10696 else if (dwarf_version >= 5)
10698 gcc_assert (AT_loc_list (a)->num_assigned);
10699 dw2_asm_output_data_uleb128 (AT_loc_list (a)->hash, "%s (%s)",
10700 dwarf_attr_name (a->dw_attr),
10701 sym);
10703 else
10704 dw2_asm_output_delta (dwarf_offset_size, sym, loc_section_label,
10705 "%s", dwarf_attr_name (a->dw_attr));
10708 /* Output the offset into the debug_loc section. */
10710 static void
10711 output_view_list_offset (dw_attr_node *a)
10713 char *sym = (*AT_loc_list_ptr (a))->vl_symbol;
10715 gcc_assert (sym);
10716 if (dwarf_split_debug_info)
10717 dw2_asm_output_delta (dwarf_offset_size, sym, loc_section_label,
10718 "%s", dwarf_attr_name (a->dw_attr));
10719 else
10720 dw2_asm_output_offset (dwarf_offset_size, sym, debug_loc_section,
10721 "%s", dwarf_attr_name (a->dw_attr));
10724 /* Output an attribute's index or value appropriately. */
10726 static void
10727 output_attr_index_or_value (dw_attr_node *a)
10729 const char *name = dwarf_attr_name (a->dw_attr);
10731 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
10733 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
10734 return;
10736 switch (AT_class (a))
10738 case dw_val_class_addr:
10739 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
10740 break;
10741 case dw_val_class_high_pc:
10742 case dw_val_class_lbl_id:
10743 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
10744 break;
10745 default:
10746 gcc_unreachable ();
10750 /* Output a type signature. */
10752 static inline void
10753 output_signature (const char *sig, const char *name)
10755 int i;
10757 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10758 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
10761 /* Output a discriminant value. */
10763 static inline void
10764 output_discr_value (dw_discr_value *discr_value, const char *name)
10766 if (discr_value->pos)
10767 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
10768 else
10769 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
10772 /* Output the DIE and its attributes. Called recursively to generate
10773 the definitions of each child DIE. */
10775 static void
10776 output_die (dw_die_ref die)
10778 dw_attr_node *a;
10779 dw_die_ref c;
10780 unsigned long size;
10781 unsigned ix;
10783 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
10784 (unsigned long)die->die_offset,
10785 dwarf_tag_name (die->die_tag));
10787 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
10789 const char *name = dwarf_attr_name (a->dw_attr);
10791 switch (AT_class (a))
10793 case dw_val_class_addr:
10794 output_attr_index_or_value (a);
10795 break;
10797 case dw_val_class_offset:
10798 dw2_asm_output_data (dwarf_offset_size, a->dw_attr_val.v.val_offset,
10799 "%s", name);
10800 break;
10802 case dw_val_class_range_list:
10803 output_range_list_offset (a);
10804 break;
10806 case dw_val_class_loc:
10807 size = size_of_locs (AT_loc (a));
10809 /* Output the block length for this list of location operations. */
10810 if (dwarf_version >= 4)
10811 dw2_asm_output_data_uleb128 (size, "%s", name);
10812 else
10813 dw2_asm_output_data (constant_size (size), size, "%s", name);
10815 output_loc_sequence (AT_loc (a), -1);
10816 break;
10818 case dw_val_class_const:
10819 /* ??? It would be slightly more efficient to use a scheme like is
10820 used for unsigned constants below, but gdb 4.x does not sign
10821 extend. Gdb 5.x does sign extend. */
10822 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10823 break;
10825 case dw_val_class_unsigned_const:
10827 int csize = constant_size (AT_unsigned (a));
10828 if (dwarf_version == 3
10829 && a->dw_attr == DW_AT_data_member_location
10830 && csize >= 4)
10831 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
10832 else
10833 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
10835 break;
10837 case dw_val_class_symview:
10839 int vsize;
10840 if (symview_upper_bound <= 0xff)
10841 vsize = 1;
10842 else if (symview_upper_bound <= 0xffff)
10843 vsize = 2;
10844 else if (symview_upper_bound <= 0xffffffff)
10845 vsize = 4;
10846 else
10847 vsize = 8;
10848 dw2_asm_output_addr (vsize, a->dw_attr_val.v.val_symbolic_view,
10849 "%s", name);
10851 break;
10853 case dw_val_class_const_implicit:
10854 if (flag_debug_asm)
10855 fprintf (asm_out_file, "\t\t\t%s %s ("
10856 HOST_WIDE_INT_PRINT_DEC ")\n",
10857 ASM_COMMENT_START, name, AT_int (a));
10858 break;
10860 case dw_val_class_unsigned_const_implicit:
10861 if (flag_debug_asm)
10862 fprintf (asm_out_file, "\t\t\t%s %s ("
10863 HOST_WIDE_INT_PRINT_HEX ")\n",
10864 ASM_COMMENT_START, name, AT_unsigned (a));
10865 break;
10867 case dw_val_class_const_double:
10869 unsigned HOST_WIDE_INT first, second;
10871 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
10872 dw2_asm_output_data (1,
10873 HOST_BITS_PER_DOUBLE_INT
10874 / HOST_BITS_PER_CHAR,
10875 NULL);
10877 if (WORDS_BIG_ENDIAN)
10879 first = a->dw_attr_val.v.val_double.high;
10880 second = a->dw_attr_val.v.val_double.low;
10882 else
10884 first = a->dw_attr_val.v.val_double.low;
10885 second = a->dw_attr_val.v.val_double.high;
10888 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10889 first, "%s", name);
10890 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10891 second, NULL);
10893 break;
10895 case dw_val_class_wide_int:
10897 int i;
10898 int len = get_full_len (*a->dw_attr_val.v.val_wide);
10899 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10900 if (len * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
10901 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide)
10902 * l, NULL);
10904 if (WORDS_BIG_ENDIAN)
10905 for (i = len - 1; i >= 0; --i)
10907 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10908 "%s", name);
10909 name = "";
10911 else
10912 for (i = 0; i < len; ++i)
10914 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10915 "%s", name);
10916 name = "";
10919 break;
10921 case dw_val_class_vec:
10923 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10924 unsigned int len = a->dw_attr_val.v.val_vec.length;
10925 unsigned int i;
10926 unsigned char *p;
10928 dw2_asm_output_data (constant_size (len * elt_size),
10929 len * elt_size, "%s", name);
10930 if (elt_size > sizeof (HOST_WIDE_INT))
10932 elt_size /= 2;
10933 len *= 2;
10935 for (i = 0, p = (unsigned char *) a->dw_attr_val.v.val_vec.array;
10936 i < len;
10937 i++, p += elt_size)
10938 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10939 "fp or vector constant word %u", i);
10940 break;
10943 case dw_val_class_flag:
10944 if (dwarf_version >= 4)
10946 /* Currently all add_AT_flag calls pass in 1 as last argument,
10947 so DW_FORM_flag_present can be used. If that ever changes,
10948 we'll need to use DW_FORM_flag and have some optimization
10949 in build_abbrev_table that will change those to
10950 DW_FORM_flag_present if it is set to 1 in all DIEs using
10951 the same abbrev entry. */
10952 gcc_assert (AT_flag (a) == 1);
10953 if (flag_debug_asm)
10954 fprintf (asm_out_file, "\t\t\t%s %s\n",
10955 ASM_COMMENT_START, name);
10956 break;
10958 dw2_asm_output_data (1, AT_flag (a), "%s", name);
10959 break;
10961 case dw_val_class_loc_list:
10962 output_loc_list_offset (a);
10963 break;
10965 case dw_val_class_view_list:
10966 output_view_list_offset (a);
10967 break;
10969 case dw_val_class_die_ref:
10970 if (AT_ref_external (a))
10972 if (AT_ref (a)->comdat_type_p)
10974 comdat_type_node *type_node
10975 = AT_ref (a)->die_id.die_type_node;
10977 gcc_assert (type_node);
10978 output_signature (type_node->signature, name);
10980 else
10982 const char *sym = AT_ref (a)->die_id.die_symbol;
10983 int size;
10985 gcc_assert (sym);
10986 /* In DWARF2, DW_FORM_ref_addr is sized by target address
10987 length, whereas in DWARF3 it's always sized as an
10988 offset. */
10989 if (dwarf_version == 2)
10990 size = DWARF2_ADDR_SIZE;
10991 else
10992 size = dwarf_offset_size;
10993 /* ??? We cannot unconditionally output die_offset if
10994 non-zero - others might create references to those
10995 DIEs via symbols.
10996 And we do not clear its DIE offset after outputting it
10997 (and the label refers to the actual DIEs, not the
10998 DWARF CU unit header which is when using label + offset
10999 would be the correct thing to do).
11000 ??? This is the reason for the with_offset flag. */
11001 if (AT_ref (a)->with_offset)
11002 dw2_asm_output_offset (size, sym, AT_ref (a)->die_offset,
11003 debug_info_section, "%s", name);
11004 else
11005 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
11006 name);
11009 else
11011 gcc_assert (AT_ref (a)->die_offset);
11012 dw2_asm_output_data (dwarf_offset_size, AT_ref (a)->die_offset,
11013 "%s", name);
11015 break;
11017 case dw_val_class_fde_ref:
11019 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
11021 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
11022 a->dw_attr_val.v.val_fde_index * 2);
11023 dw2_asm_output_offset (dwarf_offset_size, l1, debug_frame_section,
11024 "%s", name);
11026 break;
11028 case dw_val_class_vms_delta:
11029 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
11030 dw2_asm_output_vms_delta (dwarf_offset_size,
11031 AT_vms_delta2 (a), AT_vms_delta1 (a),
11032 "%s", name);
11033 #else
11034 dw2_asm_output_delta (dwarf_offset_size,
11035 AT_vms_delta2 (a), AT_vms_delta1 (a),
11036 "%s", name);
11037 #endif
11038 break;
11040 case dw_val_class_lbl_id:
11041 output_attr_index_or_value (a);
11042 break;
11044 case dw_val_class_lineptr:
11045 dw2_asm_output_offset (dwarf_offset_size, AT_lbl (a),
11046 debug_line_section, "%s", name);
11047 break;
11049 case dw_val_class_macptr:
11050 dw2_asm_output_offset (dwarf_offset_size, AT_lbl (a),
11051 debug_macinfo_section, "%s", name);
11052 break;
11054 case dw_val_class_loclistsptr:
11055 dw2_asm_output_offset (dwarf_offset_size, AT_lbl (a),
11056 debug_loc_section, "%s", name);
11057 break;
11059 case dw_val_class_str:
11060 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
11061 dw2_asm_output_offset (dwarf_offset_size,
11062 a->dw_attr_val.v.val_str->label,
11063 debug_str_section,
11064 "%s: \"%s\"", name, AT_string (a));
11065 else if (a->dw_attr_val.v.val_str->form == DW_FORM_line_strp)
11066 dw2_asm_output_offset (dwarf_offset_size,
11067 a->dw_attr_val.v.val_str->label,
11068 debug_line_str_section,
11069 "%s: \"%s\"", name, AT_string (a));
11070 else if (a->dw_attr_val.v.val_str->form == dwarf_FORM (DW_FORM_strx))
11071 dw2_asm_output_data_uleb128 (AT_index (a),
11072 "%s: \"%s\"", name, AT_string (a));
11073 else
11074 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
11075 break;
11077 case dw_val_class_file:
11079 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
11081 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
11082 a->dw_attr_val.v.val_file->filename);
11083 break;
11086 case dw_val_class_file_implicit:
11087 if (flag_debug_asm)
11088 fprintf (asm_out_file, "\t\t\t%s %s (%d, %s)\n",
11089 ASM_COMMENT_START, name,
11090 maybe_emit_file (a->dw_attr_val.v.val_file),
11091 a->dw_attr_val.v.val_file->filename);
11092 break;
11094 case dw_val_class_data8:
11096 int i;
11098 for (i = 0; i < 8; i++)
11099 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
11100 i == 0 ? "%s" : NULL, name);
11101 break;
11104 case dw_val_class_high_pc:
11105 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
11106 get_AT_low_pc (die), "DW_AT_high_pc");
11107 break;
11109 case dw_val_class_discr_value:
11110 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
11111 break;
11113 case dw_val_class_discr_list:
11115 dw_discr_list_ref list = AT_discr_list (a);
11116 const int size = size_of_discr_list (list);
11118 /* This is a block, so output its length first. */
11119 dw2_asm_output_data (constant_size (size), size,
11120 "%s: block size", name);
11122 for (; list != NULL; list = list->dw_discr_next)
11124 /* One byte for the discriminant value descriptor, and then as
11125 many LEB128 numbers as required. */
11126 if (list->dw_discr_range)
11127 dw2_asm_output_data (1, DW_DSC_range,
11128 "%s: DW_DSC_range", name);
11129 else
11130 dw2_asm_output_data (1, DW_DSC_label,
11131 "%s: DW_DSC_label", name);
11133 output_discr_value (&list->dw_discr_lower_bound, name);
11134 if (list->dw_discr_range)
11135 output_discr_value (&list->dw_discr_upper_bound, name);
11137 break;
11140 default:
11141 gcc_unreachable ();
11145 FOR_EACH_CHILD (die, c, output_die (c));
11147 /* Add null byte to terminate sibling list. */
11148 if (die->die_child != NULL)
11149 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
11150 (unsigned long) die->die_offset);
11153 /* Output the dwarf version number. */
11155 static void
11156 output_dwarf_version ()
11158 /* ??? For now, if -gdwarf-6 is specified, we output version 5 with
11159 views in loclist. That will change eventually. */
11160 if (dwarf_version == 6)
11162 static bool once;
11163 if (!once)
11165 warning (0, "%<-gdwarf-6%> is output as version 5 with "
11166 "incompatibilities");
11167 once = true;
11169 dw2_asm_output_data (2, 5, "DWARF version number");
11171 else
11172 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
11175 /* Output the compilation unit that appears at the beginning of the
11176 .debug_info section, and precedes the DIE descriptions. */
11178 static void
11179 output_compilation_unit_header (enum dwarf_unit_type ut)
11181 if (!XCOFF_DEBUGGING_INFO)
11183 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11184 dw2_asm_output_data (4, 0xffffffff,
11185 "Initial length escape value indicating 64-bit DWARF extension");
11186 dw2_asm_output_data (dwarf_offset_size,
11187 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
11188 "Length of Compilation Unit Info");
11191 output_dwarf_version ();
11192 if (dwarf_version >= 5)
11194 const char *name;
11195 switch (ut)
11197 case DW_UT_compile: name = "DW_UT_compile"; break;
11198 case DW_UT_type: name = "DW_UT_type"; break;
11199 case DW_UT_split_compile: name = "DW_UT_split_compile"; break;
11200 case DW_UT_split_type: name = "DW_UT_split_type"; break;
11201 default: gcc_unreachable ();
11203 dw2_asm_output_data (1, ut, "%s", name);
11204 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11206 dw2_asm_output_offset (dwarf_offset_size, abbrev_section_label,
11207 debug_abbrev_section,
11208 "Offset Into Abbrev. Section");
11209 if (dwarf_version < 5)
11210 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11213 /* Output the compilation unit DIE and its children. */
11215 static void
11216 output_comp_unit (dw_die_ref die, int output_if_empty,
11217 const unsigned char *dwo_id)
11219 const char *secname, *oldsym;
11220 char *tmp;
11222 /* Unless we are outputting main CU, we may throw away empty ones. */
11223 if (!output_if_empty && die->die_child == NULL)
11224 return;
11226 /* Even if there are no children of this DIE, we must output the information
11227 about the compilation unit. Otherwise, on an empty translation unit, we
11228 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
11229 will then complain when examining the file. First mark all the DIEs in
11230 this CU so we know which get local refs. */
11231 mark_dies (die);
11233 external_ref_hash_type *extern_map = optimize_external_refs (die);
11235 /* For now, optimize only the main CU, in order to optimize the rest
11236 we'd need to see all of them earlier. Leave the rest for post-linking
11237 tools like DWZ. */
11238 if (die == comp_unit_die ())
11239 abbrev_opt_start = vec_safe_length (abbrev_die_table);
11241 build_abbrev_table (die, extern_map);
11243 optimize_abbrev_table ();
11245 delete extern_map;
11247 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
11248 next_die_offset = (dwo_id
11249 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
11250 : DWARF_COMPILE_UNIT_HEADER_SIZE);
11251 calc_die_sizes (die);
11253 oldsym = die->die_id.die_symbol;
11254 if (oldsym && die->comdat_type_p)
11256 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11258 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
11259 secname = tmp;
11260 die->die_id.die_symbol = NULL;
11261 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11263 else
11265 switch_to_section (debug_info_section);
11266 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
11267 info_section_emitted = true;
11270 /* For LTO cross unit DIE refs we want a symbol on the start of the
11271 debuginfo section, not on the CU DIE. */
11272 if ((flag_generate_lto || flag_generate_offload) && oldsym)
11274 /* ??? No way to get visibility assembled without a decl. */
11275 tree decl = build_decl (UNKNOWN_LOCATION, VAR_DECL,
11276 get_identifier (oldsym), char_type_node);
11277 TREE_PUBLIC (decl) = true;
11278 TREE_STATIC (decl) = true;
11279 DECL_ARTIFICIAL (decl) = true;
11280 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
11281 DECL_VISIBILITY_SPECIFIED (decl) = true;
11282 targetm.asm_out.assemble_visibility (decl, VISIBILITY_HIDDEN);
11283 #ifdef ASM_WEAKEN_LABEL
11284 /* We prefer a .weak because that handles duplicates from duplicate
11285 archive members in a graceful way. */
11286 ASM_WEAKEN_LABEL (asm_out_file, oldsym);
11287 #else
11288 targetm.asm_out.globalize_label (asm_out_file, oldsym);
11289 #endif
11290 ASM_OUTPUT_LABEL (asm_out_file, oldsym);
11293 /* Output debugging information. */
11294 output_compilation_unit_header (dwo_id
11295 ? DW_UT_split_compile : DW_UT_compile);
11296 if (dwarf_version >= 5)
11298 if (dwo_id != NULL)
11299 for (int i = 0; i < 8; i++)
11300 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
11302 output_die (die);
11304 /* Leave the marks on the main CU, so we can check them in
11305 output_pubnames. */
11306 if (oldsym)
11308 unmark_dies (die);
11309 die->die_id.die_symbol = oldsym;
11313 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
11314 and .debug_pubtypes. This is configured per-target, but can be
11315 overridden by the -gpubnames or -gno-pubnames options. */
11317 static inline bool
11318 want_pubnames (void)
11320 if (debug_info_level <= DINFO_LEVEL_TERSE
11321 /* Names and types go to the early debug part only. */
11322 || in_lto_p)
11323 return false;
11324 if (debug_generate_pub_sections != -1)
11325 return debug_generate_pub_sections;
11326 return targetm.want_debug_pub_sections;
11329 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
11331 static void
11332 add_AT_pubnames (dw_die_ref die)
11334 if (want_pubnames ())
11335 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
11338 /* Add a string attribute value to a skeleton DIE. */
11340 static inline void
11341 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
11342 const char *str)
11344 dw_attr_node attr;
11345 struct indirect_string_node *node;
11347 if (! skeleton_debug_str_hash)
11348 skeleton_debug_str_hash
11349 = hash_table<indirect_string_hasher>::create_ggc (10);
11351 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
11352 find_string_form (node);
11353 if (node->form == dwarf_FORM (DW_FORM_strx))
11354 node->form = DW_FORM_strp;
11356 attr.dw_attr = attr_kind;
11357 attr.dw_attr_val.val_class = dw_val_class_str;
11358 attr.dw_attr_val.val_entry = NULL;
11359 attr.dw_attr_val.v.val_str = node;
11360 add_dwarf_attr (die, &attr);
11363 /* Helper function to generate top-level dies for skeleton debug_info and
11364 debug_types. */
11366 static void
11367 add_top_level_skeleton_die_attrs (dw_die_ref die)
11369 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
11370 const char *comp_dir = comp_dir_string ();
11372 add_skeleton_AT_string (die, dwarf_AT (DW_AT_dwo_name), dwo_file_name);
11373 if (comp_dir != NULL)
11374 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
11375 add_AT_pubnames (die);
11376 if (addr_index_table != NULL && addr_index_table->size () > 0)
11377 add_AT_lineptr (die, dwarf_AT (DW_AT_addr_base), debug_addr_section_label);
11380 /* Output skeleton debug sections that point to the dwo file. */
11382 static void
11383 output_skeleton_debug_sections (dw_die_ref comp_unit,
11384 const unsigned char *dwo_id)
11386 /* These attributes will be found in the full debug_info section. */
11387 remove_AT (comp_unit, DW_AT_producer);
11388 remove_AT (comp_unit, DW_AT_language);
11390 switch_to_section (debug_skeleton_info_section);
11391 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
11393 /* Produce the skeleton compilation-unit header. This one differs enough from
11394 a normal CU header that it's better not to call output_compilation_unit
11395 header. */
11396 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11397 dw2_asm_output_data (4, 0xffffffff,
11398 "Initial length escape value indicating 64-bit "
11399 "DWARF extension");
11401 dw2_asm_output_data (dwarf_offset_size,
11402 DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
11403 - DWARF_INITIAL_LENGTH_SIZE
11404 + size_of_die (comp_unit),
11405 "Length of Compilation Unit Info");
11406 output_dwarf_version ();
11407 if (dwarf_version >= 5)
11409 dw2_asm_output_data (1, DW_UT_skeleton, "DW_UT_skeleton");
11410 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11412 dw2_asm_output_offset (dwarf_offset_size, debug_skeleton_abbrev_section_label,
11413 debug_skeleton_abbrev_section,
11414 "Offset Into Abbrev. Section");
11415 if (dwarf_version < 5)
11416 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11417 else
11418 for (int i = 0; i < 8; i++)
11419 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
11421 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
11422 output_die (comp_unit);
11424 /* Build the skeleton debug_abbrev section. */
11425 switch_to_section (debug_skeleton_abbrev_section);
11426 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
11428 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
11430 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
11433 /* Output a comdat type unit DIE and its children. */
11435 static void
11436 output_comdat_type_unit (comdat_type_node *node,
11437 bool early_lto_debug ATTRIBUTE_UNUSED)
11439 const char *secname;
11440 char *tmp;
11441 int i;
11442 #if defined (OBJECT_FORMAT_ELF)
11443 tree comdat_key;
11444 #endif
11446 /* First mark all the DIEs in this CU so we know which get local refs. */
11447 mark_dies (node->root_die);
11449 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
11451 build_abbrev_table (node->root_die, extern_map);
11453 delete extern_map;
11454 extern_map = NULL;
11456 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
11457 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11458 calc_die_sizes (node->root_die);
11460 #if defined (OBJECT_FORMAT_ELF)
11461 if (dwarf_version >= 5)
11463 if (!dwarf_split_debug_info)
11464 secname = early_lto_debug ? DEBUG_LTO_INFO_SECTION : DEBUG_INFO_SECTION;
11465 else
11466 secname = (early_lto_debug
11467 ? DEBUG_LTO_DWO_INFO_SECTION : DEBUG_DWO_INFO_SECTION);
11469 else if (!dwarf_split_debug_info)
11470 secname = early_lto_debug ? ".gnu.debuglto_.debug_types" : ".debug_types";
11471 else
11472 secname = (early_lto_debug
11473 ? ".gnu.debuglto_.debug_types.dwo" : ".debug_types.dwo");
11475 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11476 sprintf (tmp, dwarf_version >= 5 ? "wi." : "wt.");
11477 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11478 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11479 comdat_key = get_identifier (tmp);
11480 targetm.asm_out.named_section (secname,
11481 SECTION_DEBUG | SECTION_LINKONCE,
11482 comdat_key);
11483 #else
11484 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11485 sprintf (tmp, (dwarf_version >= 5
11486 ? ".gnu.linkonce.wi." : ".gnu.linkonce.wt."));
11487 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11488 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11489 secname = tmp;
11490 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11491 #endif
11493 /* Output debugging information. */
11494 output_compilation_unit_header (dwarf_split_debug_info
11495 ? DW_UT_split_type : DW_UT_type);
11496 output_signature (node->signature, "Type Signature");
11497 dw2_asm_output_data (dwarf_offset_size, node->type_die->die_offset,
11498 "Offset to Type DIE");
11499 output_die (node->root_die);
11501 unmark_dies (node->root_die);
11504 /* Return the DWARF2/3 pubname associated with a decl. */
11506 static const char *
11507 dwarf2_name (tree decl, int scope)
11509 if (DECL_NAMELESS (decl))
11510 return NULL;
11511 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11514 /* Add a new entry to .debug_pubnames if appropriate. */
11516 static void
11517 add_pubname_string (const char *str, dw_die_ref die)
11519 pubname_entry e;
11521 e.die = die;
11522 e.name = xstrdup (str);
11523 vec_safe_push (pubname_table, e);
11526 static void
11527 add_pubname (tree decl, dw_die_ref die)
11529 if (!want_pubnames ())
11530 return;
11532 /* Don't add items to the table when we expect that the consumer will have
11533 just read the enclosing die. For example, if the consumer is looking at a
11534 class_member, it will either be inside the class already, or will have just
11535 looked up the class to find the member. Either way, searching the class is
11536 faster than searching the index. */
11537 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
11538 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
11540 const char *name = dwarf2_name (decl, 1);
11542 if (name)
11543 add_pubname_string (name, die);
11547 /* Add an enumerator to the pubnames section. */
11549 static void
11550 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
11552 pubname_entry e;
11554 gcc_assert (scope_name);
11555 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
11556 e.die = die;
11557 vec_safe_push (pubname_table, e);
11560 /* Add a new entry to .debug_pubtypes if appropriate. */
11562 static void
11563 add_pubtype (tree decl, dw_die_ref die)
11565 pubname_entry e;
11567 if (!want_pubnames ())
11568 return;
11570 if ((TREE_PUBLIC (decl)
11571 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
11572 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11574 tree scope = NULL;
11575 const char *scope_name = "";
11576 const char *sep = is_cxx () ? "::" : ".";
11577 const char *name;
11579 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
11580 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
11582 scope_name = lang_hooks.dwarf_name (scope, 1);
11583 if (scope_name != NULL && scope_name[0] != '\0')
11584 scope_name = concat (scope_name, sep, NULL);
11585 else
11586 scope_name = "";
11589 if (TYPE_P (decl))
11590 name = type_tag (decl);
11591 else
11592 name = lang_hooks.dwarf_name (decl, 1);
11594 /* If we don't have a name for the type, there's no point in adding
11595 it to the table. */
11596 if (name != NULL && name[0] != '\0')
11598 e.die = die;
11599 e.name = concat (scope_name, name, NULL);
11600 vec_safe_push (pubtype_table, e);
11603 /* Although it might be more consistent to add the pubinfo for the
11604 enumerators as their dies are created, they should only be added if the
11605 enum type meets the criteria above. So rather than re-check the parent
11606 enum type whenever an enumerator die is created, just output them all
11607 here. This isn't protected by the name conditional because anonymous
11608 enums don't have names. */
11609 if (die->die_tag == DW_TAG_enumeration_type)
11611 dw_die_ref c;
11613 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
11618 /* Output a single entry in the pubnames table. */
11620 static void
11621 output_pubname (dw_offset die_offset, pubname_entry *entry)
11623 dw_die_ref die = entry->die;
11624 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
11626 dw2_asm_output_data (dwarf_offset_size, die_offset, "DIE offset");
11628 if (debug_generate_pub_sections == 2)
11630 /* This logic follows gdb's method for determining the value of the flag
11631 byte. */
11632 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
11633 switch (die->die_tag)
11635 case DW_TAG_typedef:
11636 case DW_TAG_base_type:
11637 case DW_TAG_subrange_type:
11638 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11639 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11640 break;
11641 case DW_TAG_enumerator:
11642 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11643 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11644 if (!is_cxx ())
11645 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11646 break;
11647 case DW_TAG_subprogram:
11648 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11649 GDB_INDEX_SYMBOL_KIND_FUNCTION);
11650 if (!is_ada ())
11651 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11652 break;
11653 case DW_TAG_constant:
11654 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11655 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11656 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11657 break;
11658 case DW_TAG_variable:
11659 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11660 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11661 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11662 break;
11663 case DW_TAG_namespace:
11664 case DW_TAG_imported_declaration:
11665 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11666 break;
11667 case DW_TAG_class_type:
11668 case DW_TAG_interface_type:
11669 case DW_TAG_structure_type:
11670 case DW_TAG_union_type:
11671 case DW_TAG_enumeration_type:
11672 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11673 if (!is_cxx ())
11674 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11675 break;
11676 default:
11677 /* An unusual tag. Leave the flag-byte empty. */
11678 break;
11680 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
11681 "GDB-index flags");
11684 dw2_asm_output_nstring (entry->name, -1, "external name");
11688 /* Output the public names table used to speed up access to externally
11689 visible names; or the public types table used to find type definitions. */
11691 static void
11692 output_pubnames (vec<pubname_entry, va_gc> *names)
11694 unsigned i;
11695 unsigned long pubnames_length = size_of_pubnames (names);
11696 pubname_entry *pub;
11698 if (!XCOFF_DEBUGGING_INFO)
11700 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11701 dw2_asm_output_data (4, 0xffffffff,
11702 "Initial length escape value indicating 64-bit DWARF extension");
11703 dw2_asm_output_data (dwarf_offset_size, pubnames_length,
11704 "Pub Info Length");
11707 /* Version number for pubnames/pubtypes is independent of dwarf version. */
11708 dw2_asm_output_data (2, 2, "DWARF pubnames/pubtypes version");
11710 if (dwarf_split_debug_info)
11711 dw2_asm_output_offset (dwarf_offset_size, debug_skeleton_info_section_label,
11712 debug_skeleton_info_section,
11713 "Offset of Compilation Unit Info");
11714 else
11715 dw2_asm_output_offset (dwarf_offset_size, debug_info_section_label,
11716 debug_info_section,
11717 "Offset of Compilation Unit Info");
11718 dw2_asm_output_data (dwarf_offset_size, next_die_offset,
11719 "Compilation Unit Length");
11721 FOR_EACH_VEC_ELT (*names, i, pub)
11723 if (include_pubname_in_output (names, pub))
11725 dw_offset die_offset = pub->die->die_offset;
11727 /* We shouldn't see pubnames for DIEs outside of the main CU. */
11728 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
11729 gcc_assert (pub->die->die_mark);
11731 /* If we're putting types in their own .debug_types sections,
11732 the .debug_pubtypes table will still point to the compile
11733 unit (not the type unit), so we want to use the offset of
11734 the skeleton DIE (if there is one). */
11735 if (pub->die->comdat_type_p && names == pubtype_table)
11737 comdat_type_node *type_node = pub->die->die_id.die_type_node;
11739 if (type_node != NULL)
11740 die_offset = (type_node->skeleton_die != NULL
11741 ? type_node->skeleton_die->die_offset
11742 : comp_unit_die ()->die_offset);
11745 output_pubname (die_offset, pub);
11749 dw2_asm_output_data (dwarf_offset_size, 0, NULL);
11752 /* Output public names and types tables if necessary. */
11754 static void
11755 output_pubtables (void)
11757 if (!want_pubnames () || !info_section_emitted)
11758 return;
11760 switch_to_section (debug_pubnames_section);
11761 output_pubnames (pubname_table);
11762 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
11763 It shouldn't hurt to emit it always, since pure DWARF2 consumers
11764 simply won't look for the section. */
11765 switch_to_section (debug_pubtypes_section);
11766 output_pubnames (pubtype_table);
11770 /* Output the information that goes into the .debug_aranges table.
11771 Namely, define the beginning and ending address range of the
11772 text section generated for this compilation unit. */
11774 static void
11775 output_aranges (void)
11777 unsigned i;
11778 unsigned long aranges_length = size_of_aranges ();
11780 if (!XCOFF_DEBUGGING_INFO)
11782 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11783 dw2_asm_output_data (4, 0xffffffff,
11784 "Initial length escape value indicating 64-bit DWARF extension");
11785 dw2_asm_output_data (dwarf_offset_size, aranges_length,
11786 "Length of Address Ranges Info");
11789 /* Version number for aranges is still 2, even up to DWARF5. */
11790 dw2_asm_output_data (2, 2, "DWARF aranges version");
11791 if (dwarf_split_debug_info)
11792 dw2_asm_output_offset (dwarf_offset_size, debug_skeleton_info_section_label,
11793 debug_skeleton_info_section,
11794 "Offset of Compilation Unit Info");
11795 else
11796 dw2_asm_output_offset (dwarf_offset_size, debug_info_section_label,
11797 debug_info_section,
11798 "Offset of Compilation Unit Info");
11799 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11800 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11802 /* We need to align to twice the pointer size here. */
11803 if (DWARF_ARANGES_PAD_SIZE)
11805 /* Pad using a 2 byte words so that padding is correct for any
11806 pointer size. */
11807 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11808 2 * DWARF2_ADDR_SIZE);
11809 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11810 dw2_asm_output_data (2, 0, NULL);
11813 /* It is necessary not to output these entries if the sections were
11814 not used; if the sections were not used, the length will be 0 and
11815 the address may end up as 0 if the section is discarded by ld
11816 --gc-sections, leaving an invalid (0, 0) entry that can be
11817 confused with the terminator. */
11818 if (switch_text_ranges)
11820 const char *prev_loc = text_section_label;
11821 const char *loc;
11822 unsigned idx;
11824 FOR_EACH_VEC_ELT (*switch_text_ranges, idx, loc)
11825 if (prev_loc)
11827 dw2_asm_output_addr (DWARF2_ADDR_SIZE, prev_loc, "Address");
11828 dw2_asm_output_delta (DWARF2_ADDR_SIZE, loc, prev_loc, "Length");
11829 prev_loc = NULL;
11831 else
11832 prev_loc = loc;
11834 if (prev_loc)
11836 dw2_asm_output_addr (DWARF2_ADDR_SIZE, prev_loc, "Address");
11837 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11838 prev_loc, "Length");
11842 if (switch_cold_ranges)
11844 const char *prev_loc = cold_text_section_label;
11845 const char *loc;
11846 unsigned idx;
11848 FOR_EACH_VEC_ELT (*switch_cold_ranges, idx, loc)
11849 if (prev_loc)
11851 dw2_asm_output_addr (DWARF2_ADDR_SIZE, prev_loc, "Address");
11852 dw2_asm_output_delta (DWARF2_ADDR_SIZE, loc, prev_loc, "Length");
11853 prev_loc = NULL;
11855 else
11856 prev_loc = loc;
11858 if (prev_loc)
11860 dw2_asm_output_addr (DWARF2_ADDR_SIZE, prev_loc, "Address");
11861 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11862 prev_loc, "Length");
11866 if (have_multiple_function_sections)
11868 unsigned fde_idx;
11869 dw_fde_ref fde;
11871 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
11873 if (fde->ignored_debug)
11874 continue;
11875 if (!fde->in_std_section)
11877 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
11878 "Address");
11879 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
11880 fde->dw_fde_begin, "Length");
11882 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
11884 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
11885 "Address");
11886 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
11887 fde->dw_fde_second_begin, "Length");
11892 /* Output the terminator words. */
11893 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11894 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11897 /* Add a new entry to .debug_ranges. Return its index into
11898 ranges_table vector. */
11900 static unsigned int
11901 add_ranges_num (int num, bool maybe_new_sec)
11903 dw_ranges r = { NULL, num, 0, maybe_new_sec, NULL, NULL };
11904 vec_safe_push (ranges_table, r);
11905 return vec_safe_length (ranges_table) - 1;
11908 /* Add a new entry to .debug_ranges corresponding to a block, or a
11909 range terminator if BLOCK is NULL. MAYBE_NEW_SEC is true if
11910 this entry might be in a different section from previous range. */
11912 static unsigned int
11913 add_ranges (const_tree block, bool maybe_new_sec)
11915 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0, maybe_new_sec);
11918 /* Note that (*rnglist_table)[offset] is either a head of a rnglist
11919 chain, or middle entry of a chain that will be directly referred to. */
11921 static void
11922 note_rnglist_head (unsigned int offset)
11924 if (dwarf_version < 5 || (*ranges_table)[offset].label)
11925 return;
11926 (*ranges_table)[offset].label = gen_internal_sym ("LLRL");
11929 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
11930 When using dwarf_split_debug_info, address attributes in dies destined
11931 for the final executable should be direct references--setting the
11932 parameter force_direct ensures this behavior. */
11934 static void
11935 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11936 bool *added, bool force_direct)
11938 unsigned int in_use = vec_safe_length (ranges_by_label);
11939 unsigned int offset;
11940 dw_ranges_by_label rbl = { begin, end };
11941 vec_safe_push (ranges_by_label, rbl);
11942 offset = add_ranges_num (-(int)in_use - 1, true);
11943 if (!*added)
11945 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
11946 *added = true;
11947 note_rnglist_head (offset);
11948 if (dwarf_split_debug_info && force_direct)
11949 (*ranges_table)[offset].idx = DW_RANGES_IDX_SKELETON;
11953 /* Emit .debug_ranges section. */
11955 static void
11956 output_ranges (void)
11958 unsigned i;
11959 static const char *const start_fmt = "Offset %#x";
11960 const char *fmt = start_fmt;
11961 dw_ranges *r;
11963 switch_to_section (debug_ranges_section);
11964 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11965 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11967 int block_num = r->num;
11969 if (block_num > 0)
11971 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11972 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11974 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11975 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11977 /* If all code is in the text section, then the compilation
11978 unit base address defaults to DW_AT_low_pc, which is the
11979 base of the text section. */
11980 if (!have_multiple_function_sections)
11982 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11983 text_section_label,
11984 fmt, i * 2 * DWARF2_ADDR_SIZE);
11985 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11986 text_section_label, NULL);
11989 /* Otherwise, the compilation unit base address is zero,
11990 which allows us to use absolute addresses, and not worry
11991 about whether the target supports cross-section
11992 arithmetic. */
11993 else
11995 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11996 fmt, i * 2 * DWARF2_ADDR_SIZE);
11997 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
12000 fmt = NULL;
12003 /* Negative block_num stands for an index into ranges_by_label. */
12004 else if (block_num < 0)
12006 int lab_idx = - block_num - 1;
12008 if (!have_multiple_function_sections)
12010 gcc_unreachable ();
12011 #if 0
12012 /* If we ever use add_ranges_by_labels () for a single
12013 function section, all we have to do is to take out
12014 the #if 0 above. */
12015 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
12016 (*ranges_by_label)[lab_idx].begin,
12017 text_section_label,
12018 fmt, i * 2 * DWARF2_ADDR_SIZE);
12019 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
12020 (*ranges_by_label)[lab_idx].end,
12021 text_section_label, NULL);
12022 #endif
12024 else
12026 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
12027 (*ranges_by_label)[lab_idx].begin,
12028 fmt, i * 2 * DWARF2_ADDR_SIZE);
12029 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
12030 (*ranges_by_label)[lab_idx].end,
12031 NULL);
12034 else
12036 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
12037 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
12038 fmt = start_fmt;
12043 /* Non-zero if .debug_line_str should be used for .debug_line section
12044 strings or strings that are likely shareable with those. */
12045 #define DWARF5_USE_DEBUG_LINE_STR \
12046 (!DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET \
12047 && (DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) != 0 \
12048 /* FIXME: there is no .debug_line_str.dwo section, \
12049 for -gsplit-dwarf we should use DW_FORM_strx instead. */ \
12050 && !dwarf_split_debug_info)
12053 /* Returns TRUE if we are outputting DWARF5 and the assembler supports
12054 DWARF5 .debug_line tables using .debug_line_str or we generate
12055 it ourselves, except for split-dwarf which doesn't have a
12056 .debug_line_str. */
12057 static bool
12058 asm_outputs_debug_line_str (void)
12060 if (dwarf_version >= 5
12061 && ! output_asm_line_debug_info ()
12062 && DWARF5_USE_DEBUG_LINE_STR)
12063 return true;
12064 else
12066 #if defined(HAVE_AS_GDWARF_5_DEBUG_FLAG) && defined(HAVE_AS_WORKING_DWARF_N_FLAG)
12067 return !dwarf_split_debug_info && dwarf_version >= 5;
12068 #else
12069 return false;
12070 #endif
12074 /* Return true if it is beneficial to use DW_RLE_base_address{,x}.
12075 I is index of the following range. */
12077 static bool
12078 use_distinct_base_address_for_range (unsigned int i)
12080 if (i >= vec_safe_length (ranges_table))
12081 return false;
12083 dw_ranges *r2 = &(*ranges_table)[i];
12084 /* Use DW_RLE_base_address{,x} if there is a next range in the
12085 range list and is guaranteed to be in the same section. */
12086 return r2->num != 0 && r2->label == NULL && !r2->maybe_new_sec;
12089 /* Assign .debug_rnglists indexes and unique indexes into the debug_addr
12090 section when needed. */
12092 static void
12093 index_rnglists (void)
12095 unsigned i;
12096 dw_ranges *r;
12097 bool base = false;
12099 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
12101 if (r->label && r->idx != DW_RANGES_IDX_SKELETON)
12102 r->idx = rnglist_idx++;
12104 int block_num = r->num;
12105 if ((HAVE_AS_LEB128 || block_num < 0)
12106 && !have_multiple_function_sections)
12107 continue;
12108 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
12109 base = false;
12110 if (block_num > 0)
12112 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
12113 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
12115 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
12116 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
12118 if (HAVE_AS_LEB128)
12120 if (!base && use_distinct_base_address_for_range (i + 1))
12122 r->begin_entry = add_addr_table_entry (xstrdup (blabel),
12123 ate_kind_label);
12124 base = true;
12126 if (base)
12127 /* If we have a base, no need for further
12128 begin_entry/end_entry, as DW_RLE_offset_pair will be
12129 used. */
12130 continue;
12131 r->begin_entry
12132 = add_addr_table_entry (xstrdup (blabel), ate_kind_label);
12133 /* No need for end_entry, DW_RLE_start{,x}_length will use
12134 length as opposed to a pair of addresses. */
12136 else
12138 r->begin_entry
12139 = add_addr_table_entry (xstrdup (blabel), ate_kind_label);
12140 r->end_entry
12141 = add_addr_table_entry (xstrdup (elabel), ate_kind_label);
12145 /* Negative block_num stands for an index into ranges_by_label. */
12146 else if (block_num < 0)
12148 int lab_idx = - block_num - 1;
12149 const char *blabel = (*ranges_by_label)[lab_idx].begin;
12150 const char *elabel = (*ranges_by_label)[lab_idx].end;
12152 r->begin_entry
12153 = add_addr_table_entry (xstrdup (blabel), ate_kind_label);
12154 if (!HAVE_AS_LEB128)
12155 r->end_entry
12156 = add_addr_table_entry (xstrdup (elabel), ate_kind_label);
12161 /* Emit .debug_rnglists or (when DWO is true) .debug_rnglists.dwo section. */
12163 static bool
12164 output_rnglists (unsigned generation, bool dwo)
12166 unsigned i;
12167 dw_ranges *r;
12168 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
12169 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
12170 char basebuf[MAX_ARTIFICIAL_LABEL_BYTES];
12172 if (dwo)
12173 switch_to_section (debug_ranges_dwo_section);
12174 else
12176 switch_to_section (debug_ranges_section);
12177 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
12179 /* There are up to 4 unique ranges labels per generation.
12180 See also init_sections_and_labels. */
12181 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_RANGES_SECTION_LABEL,
12182 2 + 2 * dwo + generation * 6);
12183 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_RANGES_SECTION_LABEL,
12184 3 + 2 * dwo + generation * 6);
12185 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
12186 dw2_asm_output_data (4, 0xffffffff,
12187 "Initial length escape value indicating "
12188 "64-bit DWARF extension");
12189 dw2_asm_output_delta (dwarf_offset_size, l2, l1,
12190 "Length of Range Lists");
12191 ASM_OUTPUT_LABEL (asm_out_file, l1);
12192 output_dwarf_version ();
12193 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
12194 dw2_asm_output_data (1, 0, "Segment Size");
12195 /* Emit the offset table only for -gsplit-dwarf. If we don't care
12196 about relocation sizes and primarily care about the size of .debug*
12197 sections in linked shared libraries and executables, then
12198 the offset table plus corresponding DW_FORM_rnglistx uleb128 indexes
12199 into it are usually larger than just DW_FORM_sec_offset offsets
12200 into the .debug_rnglists section. */
12201 dw2_asm_output_data (4, dwo ? rnglist_idx : 0,
12202 "Offset Entry Count");
12203 if (dwo)
12205 ASM_OUTPUT_LABEL (asm_out_file, ranges_base_label);
12206 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
12207 if (r->label && r->idx != DW_RANGES_IDX_SKELETON)
12208 dw2_asm_output_delta (dwarf_offset_size, r->label,
12209 ranges_base_label, NULL);
12212 const char *lab = "";
12213 const char *base = NULL;
12214 bool skipping = false;
12215 bool ret = false;
12216 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
12218 int block_num = r->num;
12220 if (r->label)
12222 if (dwarf_split_debug_info
12223 && (r->idx == DW_RANGES_IDX_SKELETON) == dwo)
12225 ret = true;
12226 skipping = true;
12227 continue;
12229 ASM_OUTPUT_LABEL (asm_out_file, r->label);
12230 lab = r->label;
12232 if (skipping)
12234 if (block_num == 0)
12235 skipping = false;
12236 continue;
12238 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
12239 base = NULL;
12240 if (block_num > 0)
12242 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
12243 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
12245 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
12246 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
12248 if (HAVE_AS_LEB128)
12250 /* If all code is in the text section, then the compilation
12251 unit base address defaults to DW_AT_low_pc, which is the
12252 base of the text section. */
12253 if (!have_multiple_function_sections)
12255 dw2_asm_output_data (1, DW_RLE_offset_pair,
12256 "DW_RLE_offset_pair (%s)", lab);
12257 dw2_asm_output_delta_uleb128 (blabel, text_section_label,
12258 "Range begin address (%s)", lab);
12259 dw2_asm_output_delta_uleb128 (elabel, text_section_label,
12260 "Range end address (%s)", lab);
12261 continue;
12263 if (base == NULL && use_distinct_base_address_for_range (i + 1))
12265 if (dwarf_split_debug_info)
12267 dw2_asm_output_data (1, DW_RLE_base_addressx,
12268 "DW_RLE_base_addressx (%s)", lab);
12269 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12270 "Base address index (%s)",
12271 blabel);
12273 else
12275 dw2_asm_output_data (1, DW_RLE_base_address,
12276 "DW_RLE_base_address (%s)", lab);
12277 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12278 "Base address (%s)", lab);
12280 strcpy (basebuf, blabel);
12281 base = basebuf;
12283 if (base)
12285 dw2_asm_output_data (1, DW_RLE_offset_pair,
12286 "DW_RLE_offset_pair (%s)", lab);
12287 dw2_asm_output_delta_uleb128 (blabel, base,
12288 "Range begin address (%s)", lab);
12289 dw2_asm_output_delta_uleb128 (elabel, base,
12290 "Range end address (%s)", lab);
12291 continue;
12293 if (dwarf_split_debug_info)
12295 dw2_asm_output_data (1, DW_RLE_startx_length,
12296 "DW_RLE_startx_length (%s)", lab);
12297 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12298 "Range begin address index "
12299 "(%s)", blabel);
12301 else
12303 dw2_asm_output_data (1, DW_RLE_start_length,
12304 "DW_RLE_start_length (%s)", lab);
12305 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12306 "Range begin address (%s)", lab);
12308 dw2_asm_output_delta_uleb128 (elabel, blabel,
12309 "Range length (%s)", lab);
12311 else if (dwarf_split_debug_info)
12313 dw2_asm_output_data (1, DW_RLE_startx_endx,
12314 "DW_RLE_startx_endx (%s)", lab);
12315 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12316 "Range begin address index "
12317 "(%s)", blabel);
12318 dw2_asm_output_data_uleb128 (r->end_entry->index,
12319 "Range end address index "
12320 "(%s)", elabel);
12322 else
12324 dw2_asm_output_data (1, DW_RLE_start_end,
12325 "DW_RLE_start_end (%s)", lab);
12326 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12327 "Range begin address (%s)", lab);
12328 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
12329 "Range end address (%s)", lab);
12333 /* Negative block_num stands for an index into ranges_by_label. */
12334 else if (block_num < 0)
12336 int lab_idx = - block_num - 1;
12337 const char *blabel = (*ranges_by_label)[lab_idx].begin;
12338 const char *elabel = (*ranges_by_label)[lab_idx].end;
12340 if (!have_multiple_function_sections)
12341 gcc_unreachable ();
12342 if (HAVE_AS_LEB128)
12344 if (dwarf_split_debug_info)
12346 dw2_asm_output_data (1, DW_RLE_startx_length,
12347 "DW_RLE_startx_length (%s)", lab);
12348 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12349 "Range begin address index "
12350 "(%s)", blabel);
12352 else
12354 dw2_asm_output_data (1, DW_RLE_start_length,
12355 "DW_RLE_start_length (%s)", lab);
12356 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12357 "Range begin address (%s)", lab);
12359 dw2_asm_output_delta_uleb128 (elabel, blabel,
12360 "Range length (%s)", lab);
12362 else if (dwarf_split_debug_info)
12364 dw2_asm_output_data (1, DW_RLE_startx_endx,
12365 "DW_RLE_startx_endx (%s)", lab);
12366 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12367 "Range begin address index "
12368 "(%s)", blabel);
12369 dw2_asm_output_data_uleb128 (r->end_entry->index,
12370 "Range end address index "
12371 "(%s)", elabel);
12373 else
12375 dw2_asm_output_data (1, DW_RLE_start_end,
12376 "DW_RLE_start_end (%s)", lab);
12377 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12378 "Range begin address (%s)", lab);
12379 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
12380 "Range end address (%s)", lab);
12383 else
12384 dw2_asm_output_data (1, DW_RLE_end_of_list,
12385 "DW_RLE_end_of_list (%s)", lab);
12387 ASM_OUTPUT_LABEL (asm_out_file, l2);
12388 return ret;
12391 /* Data structure containing information about input files. */
12392 struct file_info
12394 const char *path; /* Complete file name. */
12395 const char *fname; /* File name part. */
12396 int length; /* Length of entire string. */
12397 struct dwarf_file_data * file_idx; /* Index in input file table. */
12398 int dir_idx; /* Index in directory table. */
12401 /* Data structure containing information about directories with source
12402 files. */
12403 struct dir_info
12405 const char *path; /* Path including directory name. */
12406 int length; /* Path length. */
12407 int prefix; /* Index of directory entry which is a prefix. */
12408 int count; /* Number of files in this directory. */
12409 int dir_idx; /* Index of directory used as base. */
12412 /* Callback function for file_info comparison. We sort by looking at
12413 the directories in the path. */
12415 static int
12416 file_info_cmp (const void *p1, const void *p2)
12418 const struct file_info *const s1 = (const struct file_info *) p1;
12419 const struct file_info *const s2 = (const struct file_info *) p2;
12420 const unsigned char *cp1;
12421 const unsigned char *cp2;
12423 /* Take care of file names without directories. We need to make sure that
12424 we return consistent values to qsort since some will get confused if
12425 we return the same value when identical operands are passed in opposite
12426 orders. So if neither has a directory, return 0 and otherwise return
12427 1 or -1 depending on which one has the directory. We want the one with
12428 the directory to sort after the one without, so all no directory files
12429 are at the start (normally only the compilation unit file). */
12430 if ((s1->path == s1->fname || s2->path == s2->fname))
12431 return (s2->path == s2->fname) - (s1->path == s1->fname);
12433 cp1 = (const unsigned char *) s1->path;
12434 cp2 = (const unsigned char *) s2->path;
12436 while (1)
12438 ++cp1;
12439 ++cp2;
12440 /* Reached the end of the first path? If so, handle like above,
12441 but now we want longer directory prefixes before shorter ones. */
12442 if ((cp1 == (const unsigned char *) s1->fname)
12443 || (cp2 == (const unsigned char *) s2->fname))
12444 return ((cp1 == (const unsigned char *) s1->fname)
12445 - (cp2 == (const unsigned char *) s2->fname));
12447 /* Character of current path component the same? */
12448 else if (*cp1 != *cp2)
12449 return *cp1 - *cp2;
12453 struct file_name_acquire_data
12455 struct file_info *files;
12456 int used_files;
12457 int max_files;
12460 /* Traversal function for the hash table. */
12463 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
12465 struct dwarf_file_data *d = *slot;
12466 struct file_info *fi;
12467 const char *f;
12469 gcc_assert (fnad->max_files >= d->emitted_number);
12471 if (! d->emitted_number)
12472 return 1;
12474 gcc_assert (fnad->max_files != fnad->used_files);
12476 fi = fnad->files + fnad->used_files++;
12478 f = d->filename;
12480 /* Skip all leading "./". */
12481 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
12482 f += 2;
12484 /* Create a new array entry. */
12485 fi->path = f;
12486 fi->length = strlen (f);
12487 fi->file_idx = d;
12489 /* Search for the file name part. */
12490 f = strrchr (f, DIR_SEPARATOR);
12491 #if defined (DIR_SEPARATOR_2)
12493 const char *g = strrchr (fi->path, DIR_SEPARATOR_2);
12495 if (g != NULL)
12497 if (f == NULL || f < g)
12498 f = g;
12501 #endif
12503 fi->fname = f == NULL ? fi->path : f + 1;
12504 return 1;
12507 /* Helper function for output_file_names. Emit a FORM encoded
12508 string STR, with assembly comment start ENTRY_KIND and
12509 index IDX */
12511 static void
12512 output_line_string (enum dwarf_form form, const char *str,
12513 const char *entry_kind, unsigned int idx)
12515 switch (form)
12517 case DW_FORM_string:
12518 dw2_asm_output_nstring (str, -1, "%s: %#x", entry_kind, idx);
12519 break;
12520 case DW_FORM_line_strp:
12521 if (!debug_line_str_hash)
12522 debug_line_str_hash
12523 = hash_table<indirect_string_hasher>::create_ggc (10);
12525 struct indirect_string_node *node;
12526 node = find_AT_string_in_table (str, debug_line_str_hash);
12527 set_indirect_string (node);
12528 node->form = form;
12529 dw2_asm_output_offset (dwarf_offset_size, node->label,
12530 debug_line_str_section, "%s: %#x: \"%s\"",
12531 entry_kind, 0, node->str);
12532 break;
12533 default:
12534 gcc_unreachable ();
12538 /* Output the directory table and the file name table. We try to minimize
12539 the total amount of memory needed. A heuristic is used to avoid large
12540 slowdowns with many input files. */
12542 static void
12543 output_file_names (void)
12545 struct file_name_acquire_data fnad;
12546 int numfiles;
12547 struct file_info *files;
12548 struct dir_info *dirs;
12549 int *saved;
12550 int *savehere;
12551 int *backmap;
12552 int ndirs;
12553 int idx_offset;
12554 int i;
12556 if (!last_emitted_file)
12558 if (dwarf_version >= 5)
12560 const char *comp_dir = comp_dir_string ();
12561 if (comp_dir == NULL)
12562 comp_dir = "";
12563 dw2_asm_output_data (1, 1, "Directory entry format count");
12564 enum dwarf_form str_form = DW_FORM_string;
12565 if (DWARF5_USE_DEBUG_LINE_STR)
12566 str_form = DW_FORM_line_strp;
12567 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12568 dw2_asm_output_data_uleb128 (str_form, "%s",
12569 get_DW_FORM_name (str_form));
12570 dw2_asm_output_data_uleb128 (1, "Directories count");
12571 if (str_form == DW_FORM_string)
12572 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
12573 else
12574 output_line_string (str_form, comp_dir, "Directory Entry", 0);
12575 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
12576 if (filename0 == NULL)
12577 filename0 = "";
12578 #ifdef VMS_DEBUGGING_INFO
12579 dw2_asm_output_data (1, 4, "File name entry format count");
12580 #else
12581 dw2_asm_output_data (1, 2, "File name entry format count");
12582 #endif
12583 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12584 dw2_asm_output_data_uleb128 (str_form, "%s",
12585 get_DW_FORM_name (str_form));
12586 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
12587 "DW_LNCT_directory_index");
12588 dw2_asm_output_data_uleb128 (DW_FORM_data1, "%s",
12589 get_DW_FORM_name (DW_FORM_data1));
12590 #ifdef VMS_DEBUGGING_INFO
12591 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
12592 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12593 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
12594 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12595 #endif
12596 dw2_asm_output_data_uleb128 (1, "File names count");
12598 output_line_string (str_form, filename0, "File Entry", 0);
12599 dw2_asm_output_data (1, 0, NULL);
12600 #ifdef VMS_DEBUGGING_INFO
12601 dw2_asm_output_data_uleb128 (0, NULL);
12602 dw2_asm_output_data_uleb128 (0, NULL);
12603 #endif
12605 else
12607 dw2_asm_output_data (1, 0, "End directory table");
12608 dw2_asm_output_data (1, 0, "End file name table");
12610 return;
12613 numfiles = last_emitted_file->emitted_number;
12615 /* Allocate the various arrays we need. */
12616 files = XALLOCAVEC (struct file_info, numfiles);
12617 dirs = XALLOCAVEC (struct dir_info, numfiles);
12619 fnad.files = files;
12620 fnad.used_files = 0;
12621 fnad.max_files = numfiles;
12622 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
12623 gcc_assert (fnad.used_files == fnad.max_files);
12625 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
12627 /* Find all the different directories used. */
12628 dirs[0].path = files[0].path;
12629 dirs[0].length = files[0].fname - files[0].path;
12630 dirs[0].prefix = -1;
12631 dirs[0].count = 1;
12632 dirs[0].dir_idx = 0;
12633 files[0].dir_idx = 0;
12634 ndirs = 1;
12636 for (i = 1; i < numfiles; i++)
12637 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
12638 && memcmp (dirs[ndirs - 1].path, files[i].path,
12639 dirs[ndirs - 1].length) == 0)
12641 /* Same directory as last entry. */
12642 files[i].dir_idx = ndirs - 1;
12643 ++dirs[ndirs - 1].count;
12645 else
12647 int j;
12649 /* This is a new directory. */
12650 dirs[ndirs].path = files[i].path;
12651 dirs[ndirs].length = files[i].fname - files[i].path;
12652 dirs[ndirs].count = 1;
12653 dirs[ndirs].dir_idx = ndirs;
12654 files[i].dir_idx = ndirs;
12656 /* Search for a prefix. */
12657 dirs[ndirs].prefix = -1;
12658 for (j = 0; j < ndirs; j++)
12659 if (dirs[j].length < dirs[ndirs].length
12660 && dirs[j].length > 1
12661 && (dirs[ndirs].prefix == -1
12662 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
12663 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
12664 dirs[ndirs].prefix = j;
12666 ++ndirs;
12669 /* Now to the actual work. We have to find a subset of the directories which
12670 allow expressing the file name using references to the directory table
12671 with the least amount of characters. We do not do an exhaustive search
12672 where we would have to check out every combination of every single
12673 possible prefix. Instead we use a heuristic which provides nearly optimal
12674 results in most cases and never is much off. */
12675 saved = XALLOCAVEC (int, ndirs);
12676 savehere = XALLOCAVEC (int, ndirs);
12678 memset (saved, '\0', ndirs * sizeof (saved[0]));
12679 for (i = 0; i < ndirs; i++)
12681 int j;
12682 int total;
12684 /* We can always save some space for the current directory. But this
12685 does not mean it will be enough to justify adding the directory. */
12686 savehere[i] = dirs[i].length;
12687 total = (savehere[i] - saved[i]) * dirs[i].count;
12689 for (j = i + 1; j < ndirs; j++)
12691 savehere[j] = 0;
12692 if (saved[j] < dirs[i].length)
12694 /* Determine whether the dirs[i] path is a prefix of the
12695 dirs[j] path. */
12696 int k;
12698 k = dirs[j].prefix;
12699 while (k != -1 && k != (int) i)
12700 k = dirs[k].prefix;
12702 if (k == (int) i)
12704 /* Yes it is. We can possibly save some memory by
12705 writing the filenames in dirs[j] relative to
12706 dirs[i]. */
12707 savehere[j] = dirs[i].length;
12708 total += (savehere[j] - saved[j]) * dirs[j].count;
12713 /* Check whether we can save enough to justify adding the dirs[i]
12714 directory. */
12715 if (total > dirs[i].length + 1)
12717 /* It's worthwhile adding. */
12718 for (j = i; j < ndirs; j++)
12719 if (savehere[j] > 0)
12721 /* Remember how much we saved for this directory so far. */
12722 saved[j] = savehere[j];
12724 /* Remember the prefix directory. */
12725 dirs[j].dir_idx = i;
12730 /* Emit the directory name table. */
12731 idx_offset = dirs[0].length > 0 ? 1 : 0;
12732 enum dwarf_form str_form = DW_FORM_string;
12733 enum dwarf_form idx_form = DW_FORM_udata;
12734 if (dwarf_version >= 5)
12736 const char *comp_dir = comp_dir_string ();
12737 if (comp_dir == NULL)
12738 comp_dir = "";
12739 dw2_asm_output_data (1, 1, "Directory entry format count");
12740 if (DWARF5_USE_DEBUG_LINE_STR)
12741 str_form = DW_FORM_line_strp;
12742 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12743 dw2_asm_output_data_uleb128 (str_form, "%s",
12744 get_DW_FORM_name (str_form));
12745 dw2_asm_output_data_uleb128 (ndirs + idx_offset, "Directories count");
12746 if (str_form == DW_FORM_string)
12748 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
12749 for (i = 1 - idx_offset; i < ndirs; i++)
12750 dw2_asm_output_nstring (dirs[i].path,
12751 dirs[i].length
12752 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12753 "Directory Entry: %#x", i + idx_offset);
12755 else
12757 output_line_string (str_form, comp_dir, "Directory Entry", 0);
12758 for (i = 1 - idx_offset; i < ndirs; i++)
12760 const char *str
12761 = ggc_alloc_string (dirs[i].path,
12762 dirs[i].length
12763 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR);
12764 output_line_string (str_form, str, "Directory Entry",
12765 (unsigned) i + idx_offset);
12769 else
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 dw2_asm_output_data (1, 0, "End directory table");
12780 /* We have to emit them in the order of emitted_number since that's
12781 used in the debug info generation. To do this efficiently we
12782 generate a back-mapping of the indices first. */
12783 backmap = XALLOCAVEC (int, numfiles);
12784 for (i = 0; i < numfiles; i++)
12785 backmap[files[i].file_idx->emitted_number - 1] = i;
12787 if (dwarf_version >= 5)
12789 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
12790 if (filename0 == NULL)
12791 filename0 = "";
12792 /* DW_LNCT_directory_index can use DW_FORM_udata, DW_FORM_data1 and
12793 DW_FORM_data2. Choose one based on the number of directories
12794 and how much space would they occupy in each encoding.
12795 If we have at most 256 directories, all indexes fit into
12796 a single byte, so DW_FORM_data1 is most compact (if there
12797 are at most 128 directories, DW_FORM_udata would be as
12798 compact as that, but not shorter and slower to decode). */
12799 if (ndirs + idx_offset <= 256)
12800 idx_form = DW_FORM_data1;
12801 /* If there are more than 65536 directories, we have to use
12802 DW_FORM_udata, DW_FORM_data2 can't refer to them.
12803 Otherwise, compute what space would occupy if all the indexes
12804 used DW_FORM_udata - sum - and compare that to how large would
12805 be DW_FORM_data2 encoding, and pick the more efficient one. */
12806 else if (ndirs + idx_offset <= 65536)
12808 unsigned HOST_WIDE_INT sum = 1;
12809 for (i = 0; i < numfiles; i++)
12811 int file_idx = backmap[i];
12812 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12813 sum += size_of_uleb128 (dir_idx);
12815 if (sum >= HOST_WIDE_INT_UC (2) * (numfiles + 1))
12816 idx_form = DW_FORM_data2;
12818 #ifdef VMS_DEBUGGING_INFO
12819 dw2_asm_output_data (1, 4, "File name entry format count");
12820 #else
12821 dw2_asm_output_data (1, 2, "File name entry format count");
12822 #endif
12823 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12824 dw2_asm_output_data_uleb128 (str_form, "%s",
12825 get_DW_FORM_name (str_form));
12826 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
12827 "DW_LNCT_directory_index");
12828 dw2_asm_output_data_uleb128 (idx_form, "%s",
12829 get_DW_FORM_name (idx_form));
12830 #ifdef VMS_DEBUGGING_INFO
12831 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
12832 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12833 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
12834 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12835 #endif
12836 dw2_asm_output_data_uleb128 (numfiles + 1, "File names count");
12838 output_line_string (str_form, filename0, "File Entry", 0);
12840 /* Include directory index. */
12841 if (idx_form != DW_FORM_udata)
12842 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12843 0, NULL);
12844 else
12845 dw2_asm_output_data_uleb128 (0, NULL);
12847 #ifdef VMS_DEBUGGING_INFO
12848 dw2_asm_output_data_uleb128 (0, NULL);
12849 dw2_asm_output_data_uleb128 (0, NULL);
12850 #endif
12853 /* Now write all the file names. */
12854 for (i = 0; i < numfiles; i++)
12856 int file_idx = backmap[i];
12857 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12859 #ifdef VMS_DEBUGGING_INFO
12860 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
12862 /* Setting these fields can lead to debugger miscomparisons,
12863 but VMS Debug requires them to be set correctly. */
12865 int ver;
12866 long long cdt;
12867 long siz;
12868 int maxfilelen = (strlen (files[file_idx].path)
12869 + dirs[dir_idx].length
12870 + MAX_VMS_VERSION_LEN + 1);
12871 char *filebuf = XALLOCAVEC (char, maxfilelen);
12873 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
12874 snprintf (filebuf, maxfilelen, "%s;%d",
12875 files[file_idx].path + dirs[dir_idx].length, ver);
12877 output_line_string (str_form, filebuf, "File Entry", (unsigned) i + 1);
12879 /* Include directory index. */
12880 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
12881 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12882 dir_idx + idx_offset, NULL);
12883 else
12884 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12886 /* Modification time. */
12887 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
12888 &cdt, 0, 0, 0) == 0)
12889 ? cdt : 0, NULL);
12891 /* File length in bytes. */
12892 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
12893 0, &siz, 0, 0) == 0)
12894 ? siz : 0, NULL);
12895 #else
12896 output_line_string (str_form,
12897 files[file_idx].path + dirs[dir_idx].length,
12898 "File Entry", (unsigned) i + 1);
12900 /* Include directory index. */
12901 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
12902 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12903 dir_idx + idx_offset, NULL);
12904 else
12905 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12907 if (dwarf_version >= 5)
12908 continue;
12910 /* Modification time. */
12911 dw2_asm_output_data_uleb128 (0, NULL);
12913 /* File length in bytes. */
12914 dw2_asm_output_data_uleb128 (0, NULL);
12915 #endif /* VMS_DEBUGGING_INFO */
12918 if (dwarf_version < 5)
12919 dw2_asm_output_data (1, 0, "End file name table");
12923 /* Output one line number table into the .debug_line section. */
12925 static void
12926 output_one_line_info_table (dw_line_info_table *table)
12928 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12929 unsigned int current_line = 1;
12930 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
12931 dw_line_info_entry *ent, *prev_addr = NULL;
12932 size_t i;
12933 unsigned int view;
12935 view = 0;
12937 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
12939 switch (ent->opcode)
12941 case LI_set_address:
12942 /* ??? Unfortunately, we have little choice here currently, and
12943 must always use the most general form. GCC does not know the
12944 address delta itself, so we can't use DW_LNS_advance_pc. Many
12945 ports do have length attributes which will give an upper bound
12946 on the address range. We could perhaps use length attributes
12947 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
12948 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12950 view = 0;
12952 /* This can handle any delta. This takes
12953 4+DWARF2_ADDR_SIZE bytes. */
12954 dw2_asm_output_data (1, 0, "set address %s%s", line_label,
12955 debug_variable_location_views
12956 ? ", reset view to 0" : "");
12957 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12958 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12959 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12961 prev_addr = ent;
12962 break;
12964 case LI_adv_address:
12966 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12967 char prev_label[MAX_ARTIFICIAL_LABEL_BYTES];
12968 ASM_GENERATE_INTERNAL_LABEL (prev_label, LINE_CODE_LABEL, prev_addr->val);
12970 view++;
12972 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc, "fixed advance PC, increment view to %i", view);
12973 dw2_asm_output_delta (2, line_label, prev_label,
12974 "from %s to %s", prev_label, line_label);
12976 prev_addr = ent;
12977 break;
12980 case LI_set_line:
12981 if (ent->val == current_line)
12983 /* We still need to start a new row, so output a copy insn. */
12984 dw2_asm_output_data (1, DW_LNS_copy,
12985 "copy line %u", current_line);
12987 else
12989 int line_offset = ent->val - current_line;
12990 int line_delta = line_offset - DWARF_LINE_BASE;
12992 current_line = ent->val;
12993 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12995 /* This can handle deltas from -10 to 234, using the current
12996 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
12997 This takes 1 byte. */
12998 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12999 "line %u", current_line);
13001 else
13003 /* This can handle any delta. This takes at least 4 bytes,
13004 depending on the value being encoded. */
13005 dw2_asm_output_data (1, DW_LNS_advance_line,
13006 "advance to line %u", current_line);
13007 dw2_asm_output_data_sleb128 (line_offset, NULL);
13008 dw2_asm_output_data (1, DW_LNS_copy, NULL);
13011 break;
13013 case LI_set_file:
13014 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
13015 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
13016 break;
13018 case LI_set_column:
13019 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
13020 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
13021 break;
13023 case LI_negate_stmt:
13024 current_is_stmt = !current_is_stmt;
13025 dw2_asm_output_data (1, DW_LNS_negate_stmt,
13026 "is_stmt %d", current_is_stmt);
13027 break;
13029 case LI_set_prologue_end:
13030 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
13031 "set prologue end");
13032 break;
13034 case LI_set_epilogue_begin:
13035 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
13036 "set epilogue begin");
13037 break;
13039 case LI_set_discriminator:
13040 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
13041 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
13042 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
13043 dw2_asm_output_data_uleb128 (ent->val, NULL);
13044 break;
13048 /* Emit debug info for the address of the end of the table. */
13049 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
13050 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
13051 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
13052 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
13054 dw2_asm_output_data (1, 0, "end sequence");
13055 dw2_asm_output_data_uleb128 (1, NULL);
13056 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
13059 static unsigned int output_line_info_generation;
13061 /* Output the source line number correspondence information. This
13062 information goes into the .debug_line section. */
13064 static void
13065 output_line_info (bool prologue_only)
13067 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
13068 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
13069 bool saw_one = false;
13070 int opc;
13072 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL,
13073 output_line_info_generation);
13074 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL,
13075 output_line_info_generation);
13076 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL,
13077 output_line_info_generation);
13078 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL,
13079 output_line_info_generation++);
13081 if (!XCOFF_DEBUGGING_INFO)
13083 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
13084 dw2_asm_output_data (4, 0xffffffff,
13085 "Initial length escape value indicating 64-bit DWARF extension");
13086 dw2_asm_output_delta (dwarf_offset_size, l2, l1,
13087 "Length of Source Line Info");
13090 ASM_OUTPUT_LABEL (asm_out_file, l1);
13092 output_dwarf_version ();
13093 if (dwarf_version >= 5)
13095 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
13096 dw2_asm_output_data (1, 0, "Segment Size");
13098 dw2_asm_output_delta (dwarf_offset_size, p2, p1, "Prolog Length");
13099 ASM_OUTPUT_LABEL (asm_out_file, p1);
13101 /* Define the architecture-dependent minimum instruction length (in bytes).
13102 In this implementation of DWARF, this field is used for information
13103 purposes only. Since GCC generates assembly language, we have no
13104 a priori knowledge of how many instruction bytes are generated for each
13105 source line, and therefore can use only the DW_LNE_set_address and
13106 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
13107 this as '1', which is "correct enough" for all architectures,
13108 and don't let the target override. */
13109 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
13111 if (dwarf_version >= 4)
13112 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
13113 "Maximum Operations Per Instruction");
13114 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
13115 "Default is_stmt_start flag");
13116 dw2_asm_output_data (1, DWARF_LINE_BASE,
13117 "Line Base Value (Special Opcodes)");
13118 dw2_asm_output_data (1, DWARF_LINE_RANGE,
13119 "Line Range Value (Special Opcodes)");
13120 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
13121 "Special Opcode Base");
13123 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
13125 int n_op_args;
13126 switch (opc)
13128 case DW_LNS_advance_pc:
13129 case DW_LNS_advance_line:
13130 case DW_LNS_set_file:
13131 case DW_LNS_set_column:
13132 case DW_LNS_fixed_advance_pc:
13133 case DW_LNS_set_isa:
13134 n_op_args = 1;
13135 break;
13136 default:
13137 n_op_args = 0;
13138 break;
13141 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
13142 opc, n_op_args);
13145 /* Write out the information about the files we use. */
13146 output_file_names ();
13147 ASM_OUTPUT_LABEL (asm_out_file, p2);
13148 if (prologue_only)
13150 /* Output the marker for the end of the line number info. */
13151 ASM_OUTPUT_LABEL (asm_out_file, l2);
13152 return;
13155 if (separate_line_info)
13157 dw_line_info_table *table;
13158 size_t i;
13160 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
13161 if (table->in_use)
13163 output_one_line_info_table (table);
13164 saw_one = true;
13167 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
13169 output_one_line_info_table (cold_text_section_line_info);
13170 saw_one = true;
13173 /* ??? Some Darwin linkers crash on a .debug_line section with no
13174 sequences. Further, merely a DW_LNE_end_sequence entry is not
13175 sufficient -- the address column must also be initialized.
13176 Make sure to output at least one set_address/end_sequence pair,
13177 choosing .text since that section is always present. */
13178 if (text_section_line_info->in_use || !saw_one)
13179 output_one_line_info_table (text_section_line_info);
13181 /* Output the marker for the end of the line number info. */
13182 ASM_OUTPUT_LABEL (asm_out_file, l2);
13185 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
13187 static inline bool
13188 need_endianity_attribute_p (bool reverse)
13190 return reverse && (dwarf_version >= 3 || !dwarf_strict);
13193 /* Given a pointer to a tree node for some base type, return a pointer to
13194 a DIE that describes the given type. REVERSE is true if the type is
13195 to be interpreted in the reverse storage order wrt the target order.
13197 This routine must only be called for GCC type nodes that correspond to
13198 Dwarf base (fundamental) types. */
13200 dw_die_ref
13201 base_type_die (tree type, bool reverse)
13203 dw_die_ref base_type_result;
13204 enum dwarf_type encoding;
13205 bool fpt_used = false;
13206 struct fixed_point_type_info fpt_info;
13207 tree type_bias = NULL_TREE;
13209 /* If this is a subtype that should not be emitted as a subrange type,
13210 use the base type. See subrange_type_for_debug_p. */
13211 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
13212 type = TREE_TYPE (type);
13214 switch (TREE_CODE (type))
13216 case INTEGER_TYPE:
13217 if ((dwarf_version >= 4 || !dwarf_strict)
13218 && TYPE_NAME (type)
13219 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
13220 && DECL_IS_UNDECLARED_BUILTIN (TYPE_NAME (type))
13221 && DECL_NAME (TYPE_NAME (type)))
13223 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
13224 if (strcmp (name, "char16_t") == 0
13225 || strcmp (name, "char8_t") == 0
13226 || strcmp (name, "char32_t") == 0)
13228 encoding = DW_ATE_UTF;
13229 break;
13232 if ((dwarf_version >= 3 || !dwarf_strict)
13233 && lang_hooks.types.get_fixed_point_type_info)
13235 memset (&fpt_info, 0, sizeof (fpt_info));
13236 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
13238 fpt_used = true;
13239 encoding = ((TYPE_UNSIGNED (type))
13240 ? DW_ATE_unsigned_fixed
13241 : DW_ATE_signed_fixed);
13242 break;
13245 if (TYPE_STRING_FLAG (type))
13247 if ((dwarf_version >= 4 || !dwarf_strict)
13248 && is_rust ()
13249 && int_size_in_bytes (type) == 4)
13250 encoding = DW_ATE_UTF;
13251 else if (TYPE_UNSIGNED (type))
13252 encoding = DW_ATE_unsigned_char;
13253 else
13254 encoding = DW_ATE_signed_char;
13256 else if (TYPE_UNSIGNED (type))
13257 encoding = DW_ATE_unsigned;
13258 else
13259 encoding = DW_ATE_signed;
13261 if (!dwarf_strict
13262 && lang_hooks.types.get_type_bias)
13263 type_bias = lang_hooks.types.get_type_bias (type);
13264 break;
13266 case REAL_TYPE:
13267 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
13269 if (dwarf_version >= 3 || !dwarf_strict)
13270 encoding = DW_ATE_decimal_float;
13271 else
13272 encoding = DW_ATE_lo_user;
13274 else
13275 encoding = DW_ATE_float;
13276 break;
13278 case FIXED_POINT_TYPE:
13279 if (!(dwarf_version >= 3 || !dwarf_strict))
13280 encoding = DW_ATE_lo_user;
13281 else if (TYPE_UNSIGNED (type))
13282 encoding = DW_ATE_unsigned_fixed;
13283 else
13284 encoding = DW_ATE_signed_fixed;
13285 break;
13287 /* Dwarf2 doesn't know anything about complex ints, so use
13288 a user defined type for it. */
13289 case COMPLEX_TYPE:
13290 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (type)))
13291 encoding = DW_ATE_complex_float;
13292 else
13293 encoding = DW_ATE_lo_user;
13294 break;
13296 case BOOLEAN_TYPE:
13297 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
13298 encoding = DW_ATE_boolean;
13299 break;
13301 default:
13302 /* No other TREE_CODEs are Dwarf fundamental types. */
13303 gcc_unreachable ();
13306 base_type_result = new_die_raw (DW_TAG_base_type);
13308 add_AT_unsigned (base_type_result, DW_AT_byte_size,
13309 int_size_in_bytes (type));
13310 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
13312 if (need_endianity_attribute_p (reverse))
13313 add_AT_unsigned (base_type_result, DW_AT_endianity,
13314 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
13316 add_alignment_attribute (base_type_result, type);
13318 if (fpt_used)
13320 switch (fpt_info.scale_factor_kind)
13322 case fixed_point_scale_factor_binary:
13323 add_AT_int (base_type_result, DW_AT_binary_scale,
13324 fpt_info.scale_factor.binary);
13325 break;
13327 case fixed_point_scale_factor_decimal:
13328 add_AT_int (base_type_result, DW_AT_decimal_scale,
13329 fpt_info.scale_factor.decimal);
13330 break;
13332 case fixed_point_scale_factor_arbitrary:
13333 /* Arbitrary scale factors cannot be described in standard DWARF. */
13334 if (!dwarf_strict)
13336 /* Describe the scale factor as a rational constant. */
13337 const dw_die_ref scale_factor
13338 = new_die (DW_TAG_constant, comp_unit_die (), type);
13340 add_scalar_info (scale_factor, DW_AT_GNU_numerator,
13341 fpt_info.scale_factor.arbitrary.numerator,
13342 dw_scalar_form_constant, NULL);
13343 add_scalar_info (scale_factor, DW_AT_GNU_denominator,
13344 fpt_info.scale_factor.arbitrary.denominator,
13345 dw_scalar_form_constant, NULL);
13347 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
13349 break;
13351 default:
13352 gcc_unreachable ();
13356 if (type_bias)
13357 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
13358 dw_scalar_form_constant
13359 | dw_scalar_form_exprloc
13360 | dw_scalar_form_reference,
13361 NULL);
13363 return base_type_result;
13366 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
13367 named 'auto' in its type: return true for it, false otherwise. */
13369 static inline bool
13370 is_cxx_auto (tree type)
13372 if (is_cxx ())
13374 tree name = TYPE_IDENTIFIER (type);
13375 if (name == get_identifier ("auto")
13376 || name == get_identifier ("decltype(auto)"))
13377 return true;
13379 return false;
13382 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if the
13383 given input type is a Dwarf "fundamental" type. Otherwise return null. */
13385 static inline bool
13386 is_base_type (tree type)
13388 switch (TREE_CODE (type))
13390 case INTEGER_TYPE:
13391 case REAL_TYPE:
13392 case FIXED_POINT_TYPE:
13393 case COMPLEX_TYPE:
13394 case BOOLEAN_TYPE:
13395 return true;
13397 case VOID_TYPE:
13398 case OPAQUE_TYPE:
13399 case ARRAY_TYPE:
13400 case RECORD_TYPE:
13401 case UNION_TYPE:
13402 case QUAL_UNION_TYPE:
13403 case ENUMERAL_TYPE:
13404 case FUNCTION_TYPE:
13405 case METHOD_TYPE:
13406 case POINTER_TYPE:
13407 case REFERENCE_TYPE:
13408 case NULLPTR_TYPE:
13409 case OFFSET_TYPE:
13410 case LANG_TYPE:
13411 case VECTOR_TYPE:
13412 return false;
13414 default:
13415 if (is_cxx_auto (type))
13416 return false;
13417 gcc_unreachable ();
13421 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
13422 node, return the size in bits for the type if it is a constant, or else
13423 return the alignment for the type if the type's size is not constant, or
13424 else return BITS_PER_WORD if the type actually turns out to be an
13425 ERROR_MARK node. */
13427 static inline unsigned HOST_WIDE_INT
13428 simple_type_size_in_bits (const_tree type)
13430 if (TREE_CODE (type) == ERROR_MARK)
13431 return BITS_PER_WORD;
13432 else if (TYPE_SIZE (type) == NULL_TREE)
13433 return 0;
13434 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
13435 return tree_to_uhwi (TYPE_SIZE (type));
13436 else
13437 return TYPE_ALIGN (type);
13440 /* Similarly, but return an offset_int instead of UHWI. */
13442 static inline offset_int
13443 offset_int_type_size_in_bits (const_tree type)
13445 if (TREE_CODE (type) == ERROR_MARK)
13446 return BITS_PER_WORD;
13447 else if (TYPE_SIZE (type) == NULL_TREE)
13448 return 0;
13449 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
13450 return wi::to_offset (TYPE_SIZE (type));
13451 else
13452 return TYPE_ALIGN (type);
13455 /* Given a pointer to a tree node for a subrange type, return a pointer
13456 to a DIE that describes the given type. */
13458 static dw_die_ref
13459 subrange_type_die (tree type, tree low, tree high, tree bias,
13460 dw_die_ref context_die)
13462 dw_die_ref subrange_die;
13463 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
13465 if (context_die == NULL)
13466 context_die = comp_unit_die ();
13468 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
13470 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
13472 /* The size of the subrange type and its base type do not match,
13473 so we need to generate a size attribute for the subrange type. */
13474 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
13477 add_alignment_attribute (subrange_die, type);
13479 if (low)
13480 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
13481 if (high)
13482 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
13483 if (bias && !dwarf_strict)
13484 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
13485 dw_scalar_form_constant
13486 | dw_scalar_form_exprloc
13487 | dw_scalar_form_reference,
13488 NULL);
13490 return subrange_die;
13493 /* Returns the (const and/or volatile) cv_qualifiers associated with
13494 the decl node. This will normally be augmented with the
13495 cv_qualifiers of the underlying type in add_type_attribute. */
13497 static int
13498 decl_quals (const_tree decl)
13500 return ((TREE_READONLY (decl)
13501 /* The C++ front-end correctly marks reference-typed
13502 variables as readonly, but from a language (and debug
13503 info) standpoint they are not const-qualified. */
13504 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
13505 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
13506 | (TREE_THIS_VOLATILE (decl)
13507 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
13510 /* Determine the TYPE whose qualifiers match the largest strict subset
13511 of the given TYPE_QUALS, and return its qualifiers. Ignore all
13512 qualifiers outside QUAL_MASK. */
13514 static int
13515 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
13517 tree t;
13518 int best_rank = 0, best_qual = 0, max_rank;
13520 type_quals &= qual_mask;
13521 max_rank = popcount_hwi (type_quals) - 1;
13523 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
13524 t = TYPE_NEXT_VARIANT (t))
13526 int q = TYPE_QUALS (t) & qual_mask;
13528 if ((q & type_quals) == q && q != type_quals
13529 && check_base_type (t, type))
13531 int rank = popcount_hwi (q);
13533 if (rank > best_rank)
13535 best_rank = rank;
13536 best_qual = q;
13541 return best_qual;
13544 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
13545 static const dwarf_qual_info_t dwarf_qual_info[] =
13547 { TYPE_QUAL_CONST, DW_TAG_const_type },
13548 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
13549 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
13550 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
13552 static const unsigned int dwarf_qual_info_size = ARRAY_SIZE (dwarf_qual_info);
13554 /* If DIE is a qualified DIE of some base DIE with the same parent,
13555 return the base DIE, otherwise return NULL. Set MASK to the
13556 qualifiers added compared to the returned DIE. */
13558 static dw_die_ref
13559 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
13561 unsigned int i;
13562 for (i = 0; i < dwarf_qual_info_size; i++)
13563 if (die->die_tag == dwarf_qual_info[i].t)
13564 break;
13565 if (i == dwarf_qual_info_size)
13566 return NULL;
13567 if (vec_safe_length (die->die_attr) != 1)
13568 return NULL;
13569 dw_die_ref type = get_AT_ref (die, DW_AT_type);
13570 if (type == NULL || type->die_parent != die->die_parent)
13571 return NULL;
13572 *mask |= dwarf_qual_info[i].q;
13573 if (depth)
13575 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
13576 if (ret)
13577 return ret;
13579 return type;
13582 /* If TYPE is long double or complex long double that
13583 should be emitted as artificial typedef to _Float128 or
13584 complex _Float128, return the type it should be emitted as.
13585 This is done in case the target already supports 16-byte
13586 composite floating point type (ibm_extended_format). */
13588 static tree
13589 long_double_as_float128 (tree type)
13591 if (type != long_double_type_node
13592 && type != complex_long_double_type_node)
13593 return NULL_TREE;
13595 machine_mode mode, fmode;
13596 if (TREE_CODE (type) == COMPLEX_TYPE)
13597 mode = TYPE_MODE (TREE_TYPE (type));
13598 else
13599 mode = TYPE_MODE (type);
13600 if (known_eq (GET_MODE_SIZE (mode), 16) && !MODE_COMPOSITE_P (mode))
13601 FOR_EACH_MODE_IN_CLASS (fmode, MODE_FLOAT)
13602 if (known_eq (GET_MODE_SIZE (fmode), 16)
13603 && MODE_COMPOSITE_P (fmode))
13605 if (type == long_double_type_node)
13607 if (float128_type_node
13608 && (TYPE_MODE (float128_type_node)
13609 == TYPE_MODE (type)))
13610 return float128_type_node;
13611 return NULL_TREE;
13613 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
13614 if (COMPLEX_FLOATN_NX_TYPE_NODE (i) != NULL_TREE
13615 && (TYPE_MODE (COMPLEX_FLOATN_NX_TYPE_NODE (i))
13616 == TYPE_MODE (type)))
13617 return COMPLEX_FLOATN_NX_TYPE_NODE (i);
13620 return NULL_TREE;
13623 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
13624 entry that chains the modifiers specified by CV_QUALS in front of the
13625 given type. REVERSE is true if the type is to be interpreted in the
13626 reverse storage order wrt the target order. */
13628 static dw_die_ref
13629 modified_type_die (tree type, int cv_quals, bool reverse,
13630 dw_die_ref context_die)
13632 enum tree_code code = TREE_CODE (type);
13633 dw_die_ref mod_type_die;
13634 dw_die_ref sub_die = NULL;
13635 tree item_type = NULL;
13636 tree qualified_type;
13637 tree name, low, high;
13638 dw_die_ref mod_scope;
13639 struct array_descr_info info;
13640 /* Only these cv-qualifiers are currently handled. */
13641 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
13642 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC |
13643 ENCODE_QUAL_ADDR_SPACE(~0U));
13644 const bool reverse_base_type
13645 = need_endianity_attribute_p (reverse) && is_base_type (type);
13647 if (code == ERROR_MARK)
13648 return NULL;
13650 if (lang_hooks.types.get_debug_type)
13652 tree debug_type = lang_hooks.types.get_debug_type (type);
13654 if (debug_type != NULL_TREE && debug_type != type)
13655 return modified_type_die (debug_type, cv_quals, reverse, context_die);
13658 cv_quals &= cv_qual_mask;
13660 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
13661 tag modifier (and not an attribute) old consumers won't be able
13662 to handle it. */
13663 if (dwarf_version < 3)
13664 cv_quals &= ~TYPE_QUAL_RESTRICT;
13666 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
13667 if (dwarf_version < 5)
13668 cv_quals &= ~TYPE_QUAL_ATOMIC;
13670 /* See if we already have the appropriately qualified variant of
13671 this type. */
13672 qualified_type = get_qualified_type (type, cv_quals);
13674 if (qualified_type == sizetype)
13676 /* Try not to expose the internal sizetype type's name. */
13677 if (TYPE_NAME (qualified_type)
13678 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
13680 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
13682 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
13683 && (TYPE_PRECISION (t)
13684 == TYPE_PRECISION (qualified_type))
13685 && (TYPE_UNSIGNED (t)
13686 == TYPE_UNSIGNED (qualified_type)));
13687 qualified_type = t;
13689 else if (qualified_type == sizetype
13690 && TREE_CODE (sizetype) == TREE_CODE (size_type_node)
13691 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (size_type_node)
13692 && TYPE_UNSIGNED (sizetype) == TYPE_UNSIGNED (size_type_node))
13693 qualified_type = size_type_node;
13694 if (type == sizetype)
13695 type = qualified_type;
13698 /* If we do, then we can just use its DIE, if it exists. */
13699 if (qualified_type)
13701 mod_type_die = lookup_type_die (qualified_type);
13703 /* DW_AT_endianity doesn't come from a qualifier on the type, so it is
13704 dealt with specially: the DIE with the attribute, if it exists, is
13705 placed immediately after the regular DIE for the same base type. */
13706 if (mod_type_die
13707 && (!reverse_base_type
13708 || ((mod_type_die = mod_type_die->die_sib) != NULL
13709 && get_AT_unsigned (mod_type_die, DW_AT_endianity))))
13710 return mod_type_die;
13713 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
13715 /* Handle C typedef types. */
13716 if (name
13717 && TREE_CODE (name) == TYPE_DECL
13718 && DECL_ORIGINAL_TYPE (name)
13719 && !DECL_ARTIFICIAL (name))
13721 tree dtype = TREE_TYPE (name);
13723 /* Skip the typedef for base types with DW_AT_endianity, no big deal. */
13724 if (qualified_type == dtype && !reverse_base_type)
13726 tree origin = decl_ultimate_origin (name);
13728 /* Typedef variants that have an abstract origin don't get their own
13729 type DIE (see gen_typedef_die), so fall back on the ultimate
13730 abstract origin instead. */
13731 if (origin != NULL && origin != name)
13732 return modified_type_die (TREE_TYPE (origin), cv_quals, reverse,
13733 context_die);
13735 /* For a named type, use the typedef. */
13736 gen_type_die (qualified_type, context_die);
13737 return lookup_type_die (qualified_type);
13739 else
13741 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
13742 dquals &= cv_qual_mask;
13743 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
13744 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
13745 /* cv-unqualified version of named type. Just use
13746 the unnamed type to which it refers. */
13747 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
13748 reverse, context_die);
13749 /* Else cv-qualified version of named type; fall through. */
13753 mod_scope = scope_die_for (type, context_die);
13755 if (cv_quals)
13757 int sub_quals = 0, first_quals = 0;
13758 unsigned i;
13759 dw_die_ref first = NULL, last = NULL;
13761 /* Determine a lesser qualified type that most closely matches
13762 this one. Then generate DW_TAG_* entries for the remaining
13763 qualifiers. */
13764 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
13765 cv_qual_mask);
13766 if (sub_quals && use_debug_types)
13768 bool needed = false;
13769 /* If emitting type units, make sure the order of qualifiers
13770 is canonical. Thus, start from unqualified type if
13771 an earlier qualifier is missing in sub_quals, but some later
13772 one is present there. */
13773 for (i = 0; i < dwarf_qual_info_size; i++)
13774 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
13775 needed = true;
13776 else if (needed && (dwarf_qual_info[i].q & cv_quals))
13778 sub_quals = 0;
13779 break;
13782 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
13783 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
13785 /* As not all intermediate qualified DIEs have corresponding
13786 tree types, ensure that qualified DIEs in the same scope
13787 as their DW_AT_type are emitted after their DW_AT_type,
13788 only with other qualified DIEs for the same type possibly
13789 in between them. Determine the range of such qualified
13790 DIEs now (first being the base type, last being corresponding
13791 last qualified DIE for it). */
13792 unsigned int count = 0;
13793 first = qualified_die_p (mod_type_die, &first_quals,
13794 dwarf_qual_info_size);
13795 if (first == NULL)
13796 first = mod_type_die;
13797 gcc_assert ((first_quals & ~sub_quals) == 0);
13798 for (count = 0, last = first;
13799 count < (1U << dwarf_qual_info_size);
13800 count++, last = last->die_sib)
13802 int quals = 0;
13803 if (last == mod_scope->die_child)
13804 break;
13805 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
13806 != first)
13807 break;
13811 for (i = 0; i < dwarf_qual_info_size; i++)
13812 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
13814 dw_die_ref d;
13815 if (first && first != last)
13817 for (d = first->die_sib; ; d = d->die_sib)
13819 int quals = 0;
13820 qualified_die_p (d, &quals, dwarf_qual_info_size);
13821 if (quals == (first_quals | dwarf_qual_info[i].q))
13822 break;
13823 if (d == last)
13825 d = NULL;
13826 break;
13829 if (d)
13831 mod_type_die = d;
13832 continue;
13835 if (first)
13837 d = new_die_raw (dwarf_qual_info[i].t);
13838 add_child_die_after (mod_scope, d, last);
13839 last = d;
13841 else
13842 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
13843 if (mod_type_die)
13844 add_AT_die_ref (d, DW_AT_type, mod_type_die);
13845 mod_type_die = d;
13846 first_quals |= dwarf_qual_info[i].q;
13849 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
13851 dwarf_tag tag = DW_TAG_pointer_type;
13852 if (code == REFERENCE_TYPE)
13854 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
13855 tag = DW_TAG_rvalue_reference_type;
13856 else
13857 tag = DW_TAG_reference_type;
13859 mod_type_die = new_die (tag, mod_scope, type);
13861 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
13862 simple_type_size_in_bits (type) / BITS_PER_UNIT);
13863 add_alignment_attribute (mod_type_die, type);
13864 item_type = TREE_TYPE (type);
13866 addr_space_t as = TYPE_ADDR_SPACE (item_type);
13867 if (!ADDR_SPACE_GENERIC_P (as))
13869 int action = targetm.addr_space.debug (as);
13870 if (action >= 0)
13872 /* Positive values indicate an address_class. */
13873 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
13875 else
13877 /* Negative values indicate an (inverted) segment base reg. */
13878 dw_loc_descr_ref d
13879 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
13880 add_AT_loc (mod_type_die, DW_AT_segment, d);
13884 else if (code == ARRAY_TYPE
13885 || (lang_hooks.types.get_array_descr_info
13886 && lang_hooks.types.get_array_descr_info (type, &info)))
13888 gen_type_die (type, context_die);
13889 return lookup_type_die (type);
13891 else if (code == INTEGER_TYPE
13892 && TREE_TYPE (type) != NULL_TREE
13893 && subrange_type_for_debug_p (type, &low, &high))
13895 tree bias = NULL_TREE;
13896 if (lang_hooks.types.get_type_bias)
13897 bias = lang_hooks.types.get_type_bias (type);
13898 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
13899 item_type = TREE_TYPE (type);
13901 else if (is_base_type (type))
13903 /* If a target supports long double as different floating point
13904 modes with the same 16-byte size, use normal DW_TAG_base_type
13905 only for the composite (ibm_extended_real_format) type and
13906 for the other for the time being emit instead a "_Float128"
13907 or "complex _Float128" DW_TAG_base_type and a "long double"
13908 or "complex long double" typedef to it. */
13909 if (tree other_type = long_double_as_float128 (type))
13911 dw_die_ref other_die;
13912 if (TYPE_NAME (other_type))
13913 other_die
13914 = modified_type_die (other_type, TYPE_UNQUALIFIED, reverse,
13915 context_die);
13916 else
13918 other_die = base_type_die (type, reverse);
13919 add_child_die (comp_unit_die (), other_die);
13920 add_name_attribute (other_die,
13921 TREE_CODE (type) == COMPLEX_TYPE
13922 ? "complex _Float128" : "_Float128");
13924 mod_type_die = new_die_raw (DW_TAG_typedef);
13925 add_AT_die_ref (mod_type_die, DW_AT_type, other_die);
13927 else
13928 mod_type_die = base_type_die (type, reverse);
13930 /* The DIE with DW_AT_endianity is placed right after the naked DIE. */
13931 if (reverse_base_type)
13933 dw_die_ref after_die
13934 = modified_type_die (type, cv_quals, false, context_die);
13935 add_child_die_after (comp_unit_die (), mod_type_die, after_die);
13937 else
13938 add_child_die (comp_unit_die (), mod_type_die);
13940 add_pubtype (type, mod_type_die);
13942 else
13944 gen_type_die (type, context_die);
13946 /* We have to get the type_main_variant here (and pass that to the
13947 `lookup_type_die' routine) because the ..._TYPE node we have
13948 might simply be a *copy* of some original type node (where the
13949 copy was created to help us keep track of typedef names) and
13950 that copy might have a different TYPE_UID from the original
13951 ..._TYPE node. */
13952 if (code == FUNCTION_TYPE || code == METHOD_TYPE)
13954 /* For function/method types, can't just use type_main_variant here,
13955 because that can have different ref-qualifiers for C++,
13956 but try to canonicalize. */
13957 tree main = TYPE_MAIN_VARIANT (type);
13958 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
13959 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
13960 && check_base_type (t, main)
13961 && check_lang_type (t, type))
13962 return lookup_type_die (t);
13963 return lookup_type_die (type);
13965 /* Vectors have the debugging information in the type,
13966 not the main variant. */
13967 else if (code == VECTOR_TYPE)
13968 return lookup_type_die (type);
13969 else
13970 return lookup_type_die (type_main_variant (type));
13973 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
13974 don't output a DW_TAG_typedef, since there isn't one in the
13975 user's program; just attach a DW_AT_name to the type.
13976 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
13977 if the base type already has the same name. */
13978 if (name
13979 && ((TREE_CODE (name) != TYPE_DECL
13980 && (qualified_type == TYPE_MAIN_VARIANT (type)
13981 || (cv_quals == TYPE_UNQUALIFIED)))
13982 || (TREE_CODE (name) == TYPE_DECL
13983 && TREE_TYPE (name) == qualified_type
13984 && DECL_NAME (name))))
13986 if (TREE_CODE (name) == TYPE_DECL)
13987 /* Could just call add_name_and_src_coords_attributes here,
13988 but since this is a builtin type it doesn't have any
13989 useful source coordinates anyway. */
13990 name = DECL_NAME (name);
13991 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
13993 /* This probably indicates a bug. */
13994 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
13996 name = TYPE_IDENTIFIER (type);
13997 add_name_attribute (mod_type_die,
13998 name ? IDENTIFIER_POINTER (name) : "__unknown__");
14001 if (qualified_type && !reverse_base_type)
14002 equate_type_number_to_die (qualified_type, mod_type_die);
14004 if (item_type)
14005 /* We must do this after the equate_type_number_to_die call, in case
14006 this is a recursive type. This ensures that the modified_type_die
14007 recursion will terminate even if the type is recursive. Recursive
14008 types are possible in Ada. */
14009 sub_die = modified_type_die (item_type,
14010 TYPE_QUALS_NO_ADDR_SPACE (item_type),
14011 reverse,
14012 context_die);
14014 if (sub_die != NULL)
14015 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
14017 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
14018 if (TYPE_ARTIFICIAL (type))
14019 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
14021 return mod_type_die;
14024 /* Generate DIEs for the generic parameters of T.
14025 T must be either a generic type or a generic function.
14026 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
14028 static void
14029 gen_generic_params_dies (tree t)
14031 tree parms, args;
14032 int parms_num, i;
14033 dw_die_ref die = NULL;
14034 int non_default;
14036 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
14037 return;
14039 if (TYPE_P (t))
14040 die = lookup_type_die (t);
14041 else if (DECL_P (t))
14042 die = lookup_decl_die (t);
14044 gcc_assert (die);
14046 parms = lang_hooks.get_innermost_generic_parms (t);
14047 if (!parms)
14048 /* T has no generic parameter. It means T is neither a generic type
14049 or function. End of story. */
14050 return;
14052 parms_num = TREE_VEC_LENGTH (parms);
14053 args = lang_hooks.get_innermost_generic_args (t);
14054 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
14055 non_default = int_cst_value (TREE_CHAIN (args));
14056 else
14057 non_default = TREE_VEC_LENGTH (args);
14058 for (i = 0; i < parms_num; i++)
14060 tree parm, arg, arg_pack_elems;
14061 dw_die_ref parm_die;
14063 parm = TREE_VEC_ELT (parms, i);
14064 arg = TREE_VEC_ELT (args, i);
14065 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
14066 gcc_assert (parm && TREE_VALUE (parm) && arg);
14068 if (parm && TREE_VALUE (parm) && arg)
14070 /* If PARM represents a template parameter pack,
14071 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
14072 by DW_TAG_template_*_parameter DIEs for the argument
14073 pack elements of ARG. Note that ARG would then be
14074 an argument pack. */
14075 if (arg_pack_elems)
14076 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
14077 arg_pack_elems,
14078 die);
14079 else
14080 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
14081 true /* emit name */, die);
14082 if (i >= non_default)
14083 add_AT_flag (parm_die, DW_AT_default_value, 1);
14088 /* Create and return a DIE for PARM which should be
14089 the representation of a generic type parameter.
14090 For instance, in the C++ front end, PARM would be a template parameter.
14091 ARG is the argument to PARM.
14092 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
14093 name of the PARM.
14094 PARENT_DIE is the parent DIE which the new created DIE should be added to,
14095 as a child node. */
14097 static dw_die_ref
14098 generic_parameter_die (tree parm, tree arg,
14099 bool emit_name_p,
14100 dw_die_ref parent_die)
14102 dw_die_ref tmpl_die = NULL;
14103 const char *name = NULL;
14105 /* C++20 accepts class literals as template parameters, and var
14106 decls with initializers represent them. The VAR_DECLs would be
14107 rejected, but we can take the DECL_INITIAL constructor and
14108 attempt to expand it. */
14109 if (arg && VAR_P (arg))
14110 arg = DECL_INITIAL (arg);
14112 if (!parm || !DECL_NAME (parm) || !arg)
14113 return NULL;
14115 /* We support non-type generic parameters and arguments,
14116 type generic parameters and arguments, as well as
14117 generic generic parameters (a.k.a. template template parameters in C++)
14118 and arguments. */
14119 if (TREE_CODE (parm) == PARM_DECL)
14120 /* PARM is a nontype generic parameter */
14121 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
14122 else if (TREE_CODE (parm) == TYPE_DECL)
14123 /* PARM is a type generic parameter. */
14124 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
14125 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
14126 /* PARM is a generic generic parameter.
14127 Its DIE is a GNU extension. It shall have a
14128 DW_AT_name attribute to represent the name of the template template
14129 parameter, and a DW_AT_GNU_template_name attribute to represent the
14130 name of the template template argument. */
14131 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
14132 parent_die, parm);
14133 else
14134 gcc_unreachable ();
14136 if (tmpl_die)
14138 tree tmpl_type;
14140 /* If PARM is a generic parameter pack, it means we are
14141 emitting debug info for a template argument pack element.
14142 In other terms, ARG is a template argument pack element.
14143 In that case, we don't emit any DW_AT_name attribute for
14144 the die. */
14145 if (emit_name_p)
14147 name = IDENTIFIER_POINTER (DECL_NAME (parm));
14148 gcc_assert (name);
14149 add_AT_string (tmpl_die, DW_AT_name, name);
14152 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
14154 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
14155 TMPL_DIE should have a child DW_AT_type attribute that is set
14156 to the type of the argument to PARM, which is ARG.
14157 If PARM is a type generic parameter, TMPL_DIE should have a
14158 child DW_AT_type that is set to ARG. */
14159 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
14160 add_type_attribute (tmpl_die, tmpl_type,
14161 (TREE_THIS_VOLATILE (tmpl_type)
14162 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
14163 false, parent_die);
14165 else
14167 /* So TMPL_DIE is a DIE representing a
14168 a generic generic template parameter, a.k.a template template
14169 parameter in C++ and arg is a template. */
14171 /* The DW_AT_GNU_template_name attribute of the DIE must be set
14172 to the name of the argument. */
14173 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
14174 if (name)
14175 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
14178 if (TREE_CODE (parm) == PARM_DECL)
14179 /* So PARM is a non-type generic parameter.
14180 DWARF3 5.6.8 says we must set a DW_AT_const_value child
14181 attribute of TMPL_DIE which value represents the value
14182 of ARG.
14183 We must be careful here:
14184 The value of ARG might reference some function decls.
14185 We might currently be emitting debug info for a generic
14186 type and types are emitted before function decls, we don't
14187 know if the function decls referenced by ARG will actually be
14188 emitted after cgraph computations.
14189 So must defer the generation of the DW_AT_const_value to
14190 after cgraph is ready. */
14191 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
14194 return tmpl_die;
14197 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
14198 PARM_PACK must be a template parameter pack. The returned DIE
14199 will be child DIE of PARENT_DIE. */
14201 static dw_die_ref
14202 template_parameter_pack_die (tree parm_pack,
14203 tree parm_pack_args,
14204 dw_die_ref parent_die)
14206 dw_die_ref die;
14207 int j;
14209 gcc_assert (parent_die && parm_pack);
14211 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
14212 add_name_and_src_coords_attributes (die, parm_pack);
14213 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
14214 generic_parameter_die (parm_pack,
14215 TREE_VEC_ELT (parm_pack_args, j),
14216 false /* Don't emit DW_AT_name */,
14217 die);
14218 return die;
14221 /* Return the debugger register number described by a given RTL node. */
14223 static unsigned int
14224 debugger_reg_number (const_rtx rtl)
14226 unsigned regno = REGNO (rtl);
14228 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
14230 #ifdef LEAF_REG_REMAP
14231 if (crtl->uses_only_leaf_regs)
14233 int leaf_reg = LEAF_REG_REMAP (regno);
14234 if (leaf_reg != -1)
14235 regno = (unsigned) leaf_reg;
14237 #endif
14239 regno = DEBUGGER_REGNO (regno);
14240 gcc_assert (regno != INVALID_REGNUM);
14241 return regno;
14244 /* Optionally add a DW_OP_piece term to a location description expression.
14245 DW_OP_piece is only added if the location description expression already
14246 doesn't end with DW_OP_piece. */
14248 static void
14249 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
14251 dw_loc_descr_ref loc;
14253 if (*list_head != NULL)
14255 /* Find the end of the chain. */
14256 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
14259 if (loc->dw_loc_opc != DW_OP_piece)
14260 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
14264 /* Return a location descriptor that designates a machine register or
14265 zero if there is none. */
14267 static dw_loc_descr_ref
14268 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
14270 rtx regs;
14272 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
14273 return 0;
14275 /* We only use "frame base" when we're sure we're talking about the
14276 post-prologue local stack frame. We do this by *not* running
14277 register elimination until this point, and recognizing the special
14278 argument pointer and soft frame pointer rtx's.
14279 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
14280 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
14281 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
14283 dw_loc_descr_ref result = NULL;
14285 if (dwarf_version >= 4 || !dwarf_strict)
14287 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
14288 initialized);
14289 if (result)
14290 add_loc_descr (&result,
14291 new_loc_descr (DW_OP_stack_value, 0, 0));
14293 return result;
14296 regs = targetm.dwarf_register_span (rtl);
14298 if (REG_NREGS (rtl) > 1 || regs)
14299 return multiple_reg_loc_descriptor (rtl, regs, initialized);
14300 else
14302 unsigned int debugger_regnum = debugger_reg_number (rtl);
14303 if (debugger_regnum == IGNORED_DWARF_REGNUM)
14304 return 0;
14305 return one_reg_loc_descriptor (debugger_regnum, initialized);
14309 /* Return a location descriptor that designates a machine register for
14310 a given hard register number. */
14312 static dw_loc_descr_ref
14313 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
14315 dw_loc_descr_ref reg_loc_descr;
14317 if (regno <= 31)
14318 reg_loc_descr
14319 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
14320 else
14321 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
14323 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14324 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14326 return reg_loc_descr;
14329 /* Given an RTL of a register, return a location descriptor that
14330 designates a value that spans more than one register. */
14332 static dw_loc_descr_ref
14333 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
14334 enum var_init_status initialized)
14336 int size, i;
14337 dw_loc_descr_ref loc_result = NULL;
14339 /* Simple, contiguous registers. */
14340 if (regs == NULL_RTX)
14342 unsigned reg = REGNO (rtl);
14343 int nregs;
14345 #ifdef LEAF_REG_REMAP
14346 if (crtl->uses_only_leaf_regs)
14348 int leaf_reg = LEAF_REG_REMAP (reg);
14349 if (leaf_reg != -1)
14350 reg = (unsigned) leaf_reg;
14352 #endif
14354 gcc_assert ((unsigned) DEBUGGER_REGNO (reg) == debugger_reg_number (rtl));
14355 nregs = REG_NREGS (rtl);
14357 /* At present we only track constant-sized pieces. */
14358 if (!GET_MODE_SIZE (GET_MODE (rtl)).is_constant (&size))
14359 return NULL;
14360 size /= nregs;
14362 loc_result = NULL;
14363 while (nregs--)
14365 dw_loc_descr_ref t;
14367 t = one_reg_loc_descriptor (DEBUGGER_REGNO (reg),
14368 VAR_INIT_STATUS_INITIALIZED);
14369 add_loc_descr (&loc_result, t);
14370 add_loc_descr_op_piece (&loc_result, size);
14371 ++reg;
14373 return loc_result;
14376 /* Now onto stupid register sets in non contiguous locations. */
14378 gcc_assert (GET_CODE (regs) == PARALLEL);
14380 /* At present we only track constant-sized pieces. */
14381 if (!GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0))).is_constant (&size))
14382 return NULL;
14383 loc_result = NULL;
14385 for (i = 0; i < XVECLEN (regs, 0); ++i)
14387 dw_loc_descr_ref t;
14389 t = one_reg_loc_descriptor (debugger_reg_number (XVECEXP (regs, 0, i)),
14390 VAR_INIT_STATUS_INITIALIZED);
14391 add_loc_descr (&loc_result, t);
14392 add_loc_descr_op_piece (&loc_result, size);
14395 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14396 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14397 return loc_result;
14400 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
14402 /* Return a location descriptor that designates a constant i,
14403 as a compound operation from constant (i >> shift), constant shift
14404 and DW_OP_shl. */
14406 static dw_loc_descr_ref
14407 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
14409 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
14410 add_loc_descr (&ret, int_loc_descriptor (shift));
14411 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14412 return ret;
14415 /* Return a location descriptor that designates constant POLY_I. */
14417 static dw_loc_descr_ref
14418 int_loc_descriptor (poly_int64 poly_i)
14420 enum dwarf_location_atom op;
14422 HOST_WIDE_INT i;
14423 if (!poly_i.is_constant (&i))
14425 /* Create location descriptions for the non-constant part and
14426 add any constant offset at the end. */
14427 dw_loc_descr_ref ret = NULL;
14428 HOST_WIDE_INT constant = poly_i.coeffs[0];
14429 for (unsigned int j = 1; j < NUM_POLY_INT_COEFFS; ++j)
14431 HOST_WIDE_INT coeff = poly_i.coeffs[j];
14432 if (coeff != 0)
14434 dw_loc_descr_ref start = ret;
14435 unsigned int factor;
14436 int bias;
14437 unsigned int regno = targetm.dwarf_poly_indeterminate_value
14438 (j, &factor, &bias);
14440 /* Add COEFF * ((REGNO / FACTOR) - BIAS) to the value:
14441 add COEFF * (REGNO / FACTOR) now and subtract
14442 COEFF * BIAS from the final constant part. */
14443 constant -= coeff * bias;
14444 add_loc_descr (&ret, new_reg_loc_descr (regno, 0));
14445 if (coeff % factor == 0)
14446 coeff /= factor;
14447 else
14449 int amount = exact_log2 (factor);
14450 gcc_assert (amount >= 0);
14451 add_loc_descr (&ret, int_loc_descriptor (amount));
14452 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14454 if (coeff != 1)
14456 add_loc_descr (&ret, int_loc_descriptor (coeff));
14457 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
14459 if (start)
14460 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
14463 loc_descr_plus_const (&ret, constant);
14464 return ret;
14467 /* Pick the smallest representation of a constant, rather than just
14468 defaulting to the LEB encoding. */
14469 if (i >= 0)
14471 int clz = clz_hwi (i);
14472 int ctz = ctz_hwi (i);
14473 if (i <= 31)
14474 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
14475 else if (i <= 0xff)
14476 op = DW_OP_const1u;
14477 else if (i <= 0xffff)
14478 op = DW_OP_const2u;
14479 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
14480 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
14481 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
14482 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
14483 while DW_OP_const4u is 5 bytes. */
14484 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
14485 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14486 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
14487 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
14488 while DW_OP_const4u is 5 bytes. */
14489 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
14491 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
14492 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
14493 <= 4)
14495 /* As i >= 2**31, the double cast above will yield a negative number.
14496 Since wrapping is defined in DWARF expressions we can output big
14497 positive integers as small negative ones, regardless of the size
14498 of host wide ints.
14500 Here, since the evaluator will handle 32-bit values and since i >=
14501 2**31, we know it's going to be interpreted as a negative literal:
14502 store it this way if we can do better than 5 bytes this way. */
14503 return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
14505 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14506 op = DW_OP_const4u;
14508 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
14509 least 6 bytes: see if we can do better before falling back to it. */
14510 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14511 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
14512 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
14513 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
14514 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
14515 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
14516 >= HOST_BITS_PER_WIDE_INT)
14517 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
14518 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
14519 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
14520 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
14521 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
14522 && size_of_uleb128 (i) > 6)
14523 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
14524 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
14525 else
14526 op = DW_OP_constu;
14528 else
14530 if (i >= -0x80)
14531 op = DW_OP_const1s;
14532 else if (i >= -0x8000)
14533 op = DW_OP_const2s;
14534 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
14536 if (size_of_int_loc_descriptor (i) < 5)
14538 dw_loc_descr_ref ret = int_loc_descriptor (-i);
14539 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14540 return ret;
14542 op = DW_OP_const4s;
14544 else
14546 if (size_of_int_loc_descriptor (i)
14547 < (unsigned long) 1 + size_of_sleb128 (i))
14549 dw_loc_descr_ref ret = int_loc_descriptor (-i);
14550 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14551 return ret;
14553 op = DW_OP_consts;
14557 return new_loc_descr (op, i, 0);
14560 /* Likewise, for unsigned constants. */
14562 static dw_loc_descr_ref
14563 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
14565 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
14566 const unsigned HOST_WIDE_INT max_uint
14567 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
14569 /* If possible, use the clever signed constants handling. */
14570 if (i <= max_int)
14571 return int_loc_descriptor ((HOST_WIDE_INT) i);
14573 /* Here, we are left with positive numbers that cannot be represented as
14574 HOST_WIDE_INT, i.e.:
14575 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
14577 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
14578 whereas may be better to output a negative integer: thanks to integer
14579 wrapping, we know that:
14580 x = x - 2 ** DWARF2_ADDR_SIZE
14581 = x - 2 * (max (HOST_WIDE_INT) + 1)
14582 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
14583 small negative integers. Let's try that in cases it will clearly improve
14584 the encoding: there is no gain turning DW_OP_const4u into
14585 DW_OP_const4s. */
14586 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
14587 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
14588 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
14590 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
14592 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
14593 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
14594 const HOST_WIDE_INT second_shift
14595 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
14597 /* So we finally have:
14598 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
14599 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
14600 return int_loc_descriptor (second_shift);
14603 /* Last chance: fallback to a simple constant operation. */
14604 return new_loc_descr
14605 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14606 ? DW_OP_const4u
14607 : DW_OP_const8u,
14608 i, 0);
14611 /* Generate and return a location description that computes the unsigned
14612 comparison of the two stack top entries (a OP b where b is the top-most
14613 entry and a is the second one). The KIND of comparison can be LT_EXPR,
14614 LE_EXPR, GT_EXPR or GE_EXPR. */
14616 static dw_loc_descr_ref
14617 uint_comparison_loc_list (enum tree_code kind)
14619 enum dwarf_location_atom op, flip_op;
14620 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
14622 switch (kind)
14624 case LT_EXPR:
14625 op = DW_OP_lt;
14626 break;
14627 case LE_EXPR:
14628 op = DW_OP_le;
14629 break;
14630 case GT_EXPR:
14631 op = DW_OP_gt;
14632 break;
14633 case GE_EXPR:
14634 op = DW_OP_ge;
14635 break;
14636 default:
14637 gcc_unreachable ();
14640 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14641 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
14643 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
14644 possible to perform unsigned comparisons: we just have to distinguish
14645 three cases:
14647 1. when a and b have the same sign (as signed integers); then we should
14648 return: a OP(signed) b;
14650 2. when a is a negative signed integer while b is a positive one, then a
14651 is a greater unsigned integer than b; likewise when a and b's roles
14652 are flipped.
14654 So first, compare the sign of the two operands. */
14655 ret = new_loc_descr (DW_OP_over, 0, 0);
14656 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14657 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
14658 /* If they have different signs (i.e. they have different sign bits), then
14659 the stack top value has now the sign bit set and thus it's smaller than
14660 zero. */
14661 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
14662 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
14663 add_loc_descr (&ret, bra_node);
14665 /* We are in case 1. At this point, we know both operands have the same
14666 sign, to it's safe to use the built-in signed comparison. */
14667 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14668 add_loc_descr (&ret, jmp_node);
14670 /* We are in case 2. Here, we know both operands do not have the same sign,
14671 so we have to flip the signed comparison. */
14672 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
14673 tmp = new_loc_descr (flip_op, 0, 0);
14674 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14675 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
14676 add_loc_descr (&ret, tmp);
14678 /* This dummy operation is necessary to make the two branches join. */
14679 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14680 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14681 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
14682 add_loc_descr (&ret, tmp);
14684 return ret;
14687 /* Likewise, but takes the location description lists (might be destructive on
14688 them). Return NULL if either is NULL or if concatenation fails. */
14690 static dw_loc_list_ref
14691 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
14692 enum tree_code kind)
14694 if (left == NULL || right == NULL)
14695 return NULL;
14697 add_loc_list (&left, right);
14698 if (left == NULL)
14699 return NULL;
14701 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
14702 return left;
14705 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
14706 without actually allocating it. */
14708 static unsigned long
14709 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
14711 return size_of_int_loc_descriptor (i >> shift)
14712 + size_of_int_loc_descriptor (shift)
14713 + 1;
14716 /* Return size_of_locs (int_loc_descriptor (i)) without
14717 actually allocating it. */
14719 static unsigned long
14720 size_of_int_loc_descriptor (HOST_WIDE_INT i)
14722 unsigned long s;
14724 if (i >= 0)
14726 int clz, ctz;
14727 if (i <= 31)
14728 return 1;
14729 else if (i <= 0xff)
14730 return 2;
14731 else if (i <= 0xffff)
14732 return 3;
14733 clz = clz_hwi (i);
14734 ctz = ctz_hwi (i);
14735 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
14736 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
14737 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14738 - clz - 5);
14739 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14740 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
14741 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14742 - clz - 8);
14743 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
14744 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
14745 <= 4)
14746 return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
14747 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14748 return 5;
14749 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
14750 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14751 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
14752 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14753 - clz - 8);
14754 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
14755 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
14756 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14757 - clz - 16);
14758 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
14759 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
14760 && s > 6)
14761 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14762 - clz - 32);
14763 else
14764 return 1 + s;
14766 else
14768 if (i >= -0x80)
14769 return 2;
14770 else if (i >= -0x8000)
14771 return 3;
14772 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
14774 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
14776 s = size_of_int_loc_descriptor (-i) + 1;
14777 if (s < 5)
14778 return s;
14780 return 5;
14782 else
14784 unsigned long r = 1 + size_of_sleb128 (i);
14785 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
14787 s = size_of_int_loc_descriptor (-i) + 1;
14788 if (s < r)
14789 return s;
14791 return r;
14796 /* Return loc description representing "address" of integer value.
14797 This can appear only as toplevel expression. */
14799 static dw_loc_descr_ref
14800 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
14802 int litsize;
14803 dw_loc_descr_ref loc_result = NULL;
14805 if (!(dwarf_version >= 4 || !dwarf_strict))
14806 return NULL;
14808 litsize = size_of_int_loc_descriptor (i);
14809 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
14810 is more compact. For DW_OP_stack_value we need:
14811 litsize + 1 (DW_OP_stack_value)
14812 and for DW_OP_implicit_value:
14813 1 (DW_OP_implicit_value) + 1 (length) + size. */
14814 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
14816 loc_result = int_loc_descriptor (i);
14817 add_loc_descr (&loc_result,
14818 new_loc_descr (DW_OP_stack_value, 0, 0));
14819 return loc_result;
14822 loc_result = new_loc_descr (DW_OP_implicit_value,
14823 size, 0);
14824 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
14825 loc_result->dw_loc_oprnd2.v.val_int = i;
14826 return loc_result;
14829 /* Return a location descriptor that designates a base+offset location. */
14831 static dw_loc_descr_ref
14832 based_loc_descr (rtx reg, poly_int64 offset,
14833 enum var_init_status initialized)
14835 unsigned int regno;
14836 dw_loc_descr_ref result;
14837 dw_fde_ref fde = cfun->fde;
14839 /* We only use "frame base" when we're sure we're talking about the
14840 post-prologue local stack frame. We do this by *not* running
14841 register elimination until this point, and recognizing the special
14842 argument pointer and soft frame pointer rtx's. */
14843 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
14845 rtx elim = (ira_use_lra_p
14846 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
14847 : eliminate_regs (reg, VOIDmode, NULL_RTX));
14849 if (elim != reg)
14851 /* Allow hard frame pointer here even if frame pointer
14852 isn't used since hard frame pointer is encoded with
14853 DW_OP_fbreg which uses the DW_AT_frame_base attribute,
14854 not hard frame pointer directly. */
14855 elim = strip_offset_and_add (elim, &offset);
14856 gcc_assert (elim == hard_frame_pointer_rtx
14857 || elim == stack_pointer_rtx);
14859 /* If drap register is used to align stack, use frame
14860 pointer + offset to access stack variables. If stack
14861 is aligned without drap, use stack pointer + offset to
14862 access stack variables. */
14863 if (crtl->stack_realign_tried
14864 && reg == frame_pointer_rtx)
14866 int base_reg
14867 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
14868 ? HARD_FRAME_POINTER_REGNUM
14869 : REGNO (elim));
14870 return new_reg_loc_descr (base_reg, offset);
14873 gcc_assert (frame_pointer_fb_offset_valid);
14874 offset += frame_pointer_fb_offset;
14875 HOST_WIDE_INT const_offset;
14876 if (offset.is_constant (&const_offset))
14877 return new_loc_descr (DW_OP_fbreg, const_offset, 0);
14878 else
14880 dw_loc_descr_ref ret = new_loc_descr (DW_OP_fbreg, 0, 0);
14881 loc_descr_plus_const (&ret, offset);
14882 return ret;
14887 regno = REGNO (reg);
14888 #ifdef LEAF_REG_REMAP
14889 if (crtl->uses_only_leaf_regs)
14891 int leaf_reg = LEAF_REG_REMAP (regno);
14892 if (leaf_reg != -1)
14893 regno = (unsigned) leaf_reg;
14895 #endif
14896 regno = DWARF_FRAME_REGNUM (regno);
14898 HOST_WIDE_INT const_offset;
14899 if (!optimize && fde
14900 && (fde->drap_reg == regno || fde->vdrap_reg == regno)
14901 && offset.is_constant (&const_offset))
14903 /* Use cfa+offset to represent the location of arguments passed
14904 on the stack when drap is used to align stack.
14905 Only do this when not optimizing, for optimized code var-tracking
14906 is supposed to track where the arguments live and the register
14907 used as vdrap or drap in some spot might be used for something
14908 else in other part of the routine. */
14909 return new_loc_descr (DW_OP_fbreg, const_offset, 0);
14912 result = new_reg_loc_descr (regno, offset);
14914 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14915 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14917 return result;
14920 /* Return true if this RTL expression describes a base+offset calculation. */
14922 static inline bool
14923 is_based_loc (const_rtx rtl)
14925 return (GET_CODE (rtl) == PLUS
14926 && ((REG_P (XEXP (rtl, 0))
14927 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
14928 && CONST_INT_P (XEXP (rtl, 1)))));
14931 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
14932 failed. */
14934 static dw_loc_descr_ref
14935 tls_mem_loc_descriptor (rtx mem)
14937 tree base;
14938 dw_loc_descr_ref loc_result;
14940 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
14941 return NULL;
14943 base = get_base_address (MEM_EXPR (mem));
14944 if (base == NULL
14945 || !VAR_P (base)
14946 || !DECL_THREAD_LOCAL_P (base))
14947 return NULL;
14949 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
14950 if (loc_result == NULL)
14951 return NULL;
14953 if (maybe_ne (MEM_OFFSET (mem), 0))
14954 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
14956 return loc_result;
14959 /* Output debug info about reason why we failed to expand expression as dwarf
14960 expression. */
14962 static void
14963 expansion_failed (tree expr, rtx rtl, char const *reason)
14965 if (dump_file && (dump_flags & TDF_DETAILS))
14967 fprintf (dump_file, "Failed to expand as dwarf: ");
14968 if (expr)
14969 print_generic_expr (dump_file, expr, dump_flags);
14970 if (rtl)
14972 fprintf (dump_file, "\n");
14973 print_rtl (dump_file, rtl);
14975 fprintf (dump_file, "\nReason: %s\n", reason);
14979 /* Helper function for const_ok_for_output. */
14981 static bool
14982 const_ok_for_output_1 (rtx rtl)
14984 if (targetm.const_not_ok_for_debug_p (rtl))
14986 if (GET_CODE (rtl) != UNSPEC)
14988 expansion_failed (NULL_TREE, rtl,
14989 "Expression rejected for debug by the backend.\n");
14990 return false;
14993 /* If delegitimize_address couldn't do anything with the UNSPEC, and
14994 the target hook doesn't explicitly allow it in debug info, assume
14995 we can't express it in the debug info. */
14996 /* Don't complain about TLS UNSPECs, those are just too hard to
14997 delegitimize. Note this could be a non-decl SYMBOL_REF such as
14998 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
14999 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
15000 if (flag_checking
15001 && (XVECLEN (rtl, 0) == 0
15002 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
15003 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
15004 inform (current_function_decl
15005 ? DECL_SOURCE_LOCATION (current_function_decl)
15006 : UNKNOWN_LOCATION,
15007 #if NUM_UNSPEC_VALUES > 0
15008 "non-delegitimized UNSPEC %s (%d) found in variable location",
15009 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
15010 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
15011 #else
15012 "non-delegitimized UNSPEC %d found in variable location",
15013 #endif
15014 XINT (rtl, 1));
15015 expansion_failed (NULL_TREE, rtl,
15016 "UNSPEC hasn't been delegitimized.\n");
15017 return false;
15020 if (CONST_POLY_INT_P (rtl))
15021 return false;
15023 /* FIXME: Refer to PR60655. It is possible for simplification
15024 of rtl expressions in var tracking to produce such expressions.
15025 We should really identify / validate expressions
15026 enclosed in CONST that can be handled by assemblers on various
15027 targets and only handle legitimate cases here. */
15028 switch (GET_CODE (rtl))
15030 case SYMBOL_REF:
15031 break;
15032 case NOT:
15033 case NEG:
15034 return false;
15035 case PLUS:
15037 /* Make sure SYMBOL_REFs/UNSPECs are at most in one of the
15038 operands. */
15039 subrtx_var_iterator::array_type array;
15040 bool first = false;
15041 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
15042 if (SYMBOL_REF_P (*iter)
15043 || LABEL_P (*iter)
15044 || GET_CODE (*iter) == UNSPEC)
15046 first = true;
15047 break;
15049 if (!first)
15050 return true;
15051 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 1), ALL)
15052 if (SYMBOL_REF_P (*iter)
15053 || LABEL_P (*iter)
15054 || GET_CODE (*iter) == UNSPEC)
15055 return false;
15056 return true;
15058 case MINUS:
15060 /* Disallow negation of SYMBOL_REFs or UNSPECs when they
15061 appear in the second operand of MINUS. */
15062 subrtx_var_iterator::array_type array;
15063 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 1), ALL)
15064 if (SYMBOL_REF_P (*iter)
15065 || LABEL_P (*iter)
15066 || GET_CODE (*iter) == UNSPEC)
15067 return false;
15068 return true;
15070 default:
15071 return true;
15074 if (CONSTANT_POOL_ADDRESS_P (rtl))
15076 bool marked;
15077 get_pool_constant_mark (rtl, &marked);
15078 /* If all references to this pool constant were optimized away,
15079 it was not output and thus we can't represent it. */
15080 if (!marked)
15082 expansion_failed (NULL_TREE, rtl,
15083 "Constant was removed from constant pool.\n");
15084 return false;
15088 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
15089 return false;
15091 /* Avoid references to external symbols in debug info, on several targets
15092 the linker might even refuse to link when linking a shared library,
15093 and in many other cases the relocations for .debug_info/.debug_loc are
15094 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
15095 to be defined within the same shared library or executable are fine. */
15096 if (SYMBOL_REF_EXTERNAL_P (rtl))
15098 tree decl = SYMBOL_REF_DECL (rtl);
15100 if (decl == NULL || !targetm.binds_local_p (decl))
15102 expansion_failed (NULL_TREE, rtl,
15103 "Symbol not defined in current TU.\n");
15104 return false;
15108 return true;
15111 /* Return true if constant RTL can be emitted in DW_OP_addr or
15112 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
15113 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
15115 static bool
15116 const_ok_for_output (rtx rtl)
15118 if (GET_CODE (rtl) == SYMBOL_REF)
15119 return const_ok_for_output_1 (rtl);
15121 if (GET_CODE (rtl) == CONST)
15123 subrtx_var_iterator::array_type array;
15124 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
15125 if (!const_ok_for_output_1 (*iter))
15126 return false;
15127 return true;
15130 return true;
15133 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
15134 if possible, NULL otherwise. */
15136 static dw_die_ref
15137 base_type_for_mode (machine_mode mode, bool unsignedp)
15139 dw_die_ref type_die;
15140 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
15142 if (type == NULL)
15143 return NULL;
15144 switch (TREE_CODE (type))
15146 case INTEGER_TYPE:
15147 case REAL_TYPE:
15148 break;
15149 default:
15150 return NULL;
15152 type_die = lookup_type_die (type);
15153 if (!type_die)
15154 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
15155 comp_unit_die ());
15156 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
15157 return NULL;
15158 return type_die;
15161 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
15162 type matching MODE, or, if MODE is narrower than or as wide as
15163 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
15164 possible. */
15166 static dw_loc_descr_ref
15167 convert_descriptor_to_mode (scalar_int_mode mode, dw_loc_descr_ref op)
15169 machine_mode outer_mode = mode;
15170 dw_die_ref type_die;
15171 dw_loc_descr_ref cvt;
15173 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
15175 add_loc_descr (&op, new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
15176 return op;
15178 type_die = base_type_for_mode (outer_mode, 1);
15179 if (type_die == NULL)
15180 return NULL;
15181 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15182 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15183 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15184 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15185 add_loc_descr (&op, cvt);
15186 return op;
15189 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
15191 static dw_loc_descr_ref
15192 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
15193 dw_loc_descr_ref op1)
15195 dw_loc_descr_ref ret = op0;
15196 add_loc_descr (&ret, op1);
15197 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
15198 if (STORE_FLAG_VALUE != 1)
15200 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
15201 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
15203 return ret;
15206 /* Subroutine of scompare_loc_descriptor for the case in which we're
15207 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
15208 and in which OP_MODE is bigger than DWARF2_ADDR_SIZE. */
15210 static dw_loc_descr_ref
15211 scompare_loc_descriptor_wide (enum dwarf_location_atom op,
15212 scalar_int_mode op_mode,
15213 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
15215 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
15216 dw_loc_descr_ref cvt;
15218 if (type_die == NULL)
15219 return NULL;
15220 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15221 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15222 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15223 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15224 add_loc_descr (&op0, cvt);
15225 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15226 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15227 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15228 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15229 add_loc_descr (&op1, cvt);
15230 return compare_loc_descriptor (op, op0, op1);
15233 /* Subroutine of scompare_loc_descriptor for the case in which we're
15234 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
15235 and in which OP_MODE is smaller than DWARF2_ADDR_SIZE. */
15237 static dw_loc_descr_ref
15238 scompare_loc_descriptor_narrow (enum dwarf_location_atom op, rtx rtl,
15239 scalar_int_mode op_mode,
15240 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
15242 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
15243 /* For eq/ne, if the operands are known to be zero-extended,
15244 there is no need to do the fancy shifting up. */
15245 if (op == DW_OP_eq || op == DW_OP_ne)
15247 dw_loc_descr_ref last0, last1;
15248 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
15250 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
15252 /* deref_size zero extends, and for constants we can check
15253 whether they are zero extended or not. */
15254 if (((last0->dw_loc_opc == DW_OP_deref_size
15255 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
15256 || (CONST_INT_P (XEXP (rtl, 0))
15257 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
15258 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
15259 && ((last1->dw_loc_opc == DW_OP_deref_size
15260 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
15261 || (CONST_INT_P (XEXP (rtl, 1))
15262 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
15263 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
15264 return compare_loc_descriptor (op, op0, op1);
15266 /* EQ/NE comparison against constant in narrower type than
15267 DWARF2_ADDR_SIZE can be performed either as
15268 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
15269 DW_OP_{eq,ne}
15271 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
15272 DW_OP_{eq,ne}. Pick whatever is shorter. */
15273 if (CONST_INT_P (XEXP (rtl, 1))
15274 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
15275 && (size_of_int_loc_descriptor (shift) + 1
15276 + size_of_int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift)
15277 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
15278 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
15279 & GET_MODE_MASK (op_mode))))
15281 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
15282 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
15283 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
15284 & GET_MODE_MASK (op_mode));
15285 return compare_loc_descriptor (op, op0, op1);
15288 add_loc_descr (&op0, int_loc_descriptor (shift));
15289 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
15290 if (CONST_INT_P (XEXP (rtl, 1)))
15291 op1 = int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift);
15292 else
15294 add_loc_descr (&op1, int_loc_descriptor (shift));
15295 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
15297 return compare_loc_descriptor (op, op0, op1);
15300 /* Return location descriptor for signed comparison OP RTL. */
15302 static dw_loc_descr_ref
15303 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
15304 machine_mode mem_mode)
15306 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
15307 dw_loc_descr_ref op0, op1;
15309 if (op_mode == VOIDmode)
15310 op_mode = GET_MODE (XEXP (rtl, 1));
15311 if (op_mode == VOIDmode)
15312 return NULL;
15314 scalar_int_mode int_op_mode;
15315 if (dwarf_strict
15316 && dwarf_version < 5
15317 && (!is_a <scalar_int_mode> (op_mode, &int_op_mode)
15318 || GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE))
15319 return NULL;
15321 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
15322 VAR_INIT_STATUS_INITIALIZED);
15323 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
15324 VAR_INIT_STATUS_INITIALIZED);
15326 if (op0 == NULL || op1 == NULL)
15327 return NULL;
15329 if (is_a <scalar_int_mode> (op_mode, &int_op_mode))
15331 if (GET_MODE_SIZE (int_op_mode) < DWARF2_ADDR_SIZE)
15332 return scompare_loc_descriptor_narrow (op, rtl, int_op_mode, op0, op1);
15334 if (GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE)
15335 return scompare_loc_descriptor_wide (op, int_op_mode, op0, op1);
15337 return compare_loc_descriptor (op, op0, op1);
15340 /* Return location descriptor for unsigned comparison OP RTL. */
15342 static dw_loc_descr_ref
15343 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
15344 machine_mode mem_mode)
15346 dw_loc_descr_ref op0, op1;
15348 machine_mode test_op_mode = GET_MODE (XEXP (rtl, 0));
15349 if (test_op_mode == VOIDmode)
15350 test_op_mode = GET_MODE (XEXP (rtl, 1));
15352 scalar_int_mode op_mode;
15353 if (!is_a <scalar_int_mode> (test_op_mode, &op_mode))
15354 return NULL;
15356 if (dwarf_strict
15357 && dwarf_version < 5
15358 && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
15359 return NULL;
15361 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
15362 VAR_INIT_STATUS_INITIALIZED);
15363 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
15364 VAR_INIT_STATUS_INITIALIZED);
15366 if (op0 == NULL || op1 == NULL)
15367 return NULL;
15369 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
15371 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
15372 dw_loc_descr_ref last0, last1;
15373 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
15375 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
15377 if (CONST_INT_P (XEXP (rtl, 0)))
15378 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
15379 /* deref_size zero extends, so no need to mask it again. */
15380 else if (last0->dw_loc_opc != DW_OP_deref_size
15381 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
15383 add_loc_descr (&op0, int_loc_descriptor (mask));
15384 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
15386 if (CONST_INT_P (XEXP (rtl, 1)))
15387 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
15388 /* deref_size zero extends, so no need to mask it again. */
15389 else if (last1->dw_loc_opc != DW_OP_deref_size
15390 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
15392 add_loc_descr (&op1, int_loc_descriptor (mask));
15393 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
15396 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
15398 HOST_WIDE_INT bias = 1;
15399 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
15400 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
15401 if (CONST_INT_P (XEXP (rtl, 1)))
15402 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
15403 + INTVAL (XEXP (rtl, 1)));
15404 else
15405 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
15406 bias, 0));
15408 return compare_loc_descriptor (op, op0, op1);
15411 /* Return location descriptor for {U,S}{MIN,MAX}. */
15413 static dw_loc_descr_ref
15414 minmax_loc_descriptor (rtx rtl, machine_mode mode,
15415 machine_mode mem_mode)
15417 enum dwarf_location_atom op;
15418 dw_loc_descr_ref op0, op1, ret;
15419 dw_loc_descr_ref bra_node, drop_node;
15421 scalar_int_mode int_mode;
15422 if (dwarf_strict
15423 && dwarf_version < 5
15424 && (!is_a <scalar_int_mode> (mode, &int_mode)
15425 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE))
15426 return NULL;
15428 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15429 VAR_INIT_STATUS_INITIALIZED);
15430 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15431 VAR_INIT_STATUS_INITIALIZED);
15433 if (op0 == NULL || op1 == NULL)
15434 return NULL;
15436 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
15437 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
15438 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
15439 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
15441 /* Checked by the caller. */
15442 int_mode = as_a <scalar_int_mode> (mode);
15443 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
15445 HOST_WIDE_INT mask = GET_MODE_MASK (int_mode);
15446 add_loc_descr (&op0, int_loc_descriptor (mask));
15447 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
15448 add_loc_descr (&op1, int_loc_descriptor (mask));
15449 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
15451 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
15453 HOST_WIDE_INT bias = 1;
15454 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
15455 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
15456 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
15459 else if (is_a <scalar_int_mode> (mode, &int_mode)
15460 && GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
15462 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (int_mode)) * BITS_PER_UNIT;
15463 add_loc_descr (&op0, int_loc_descriptor (shift));
15464 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
15465 add_loc_descr (&op1, int_loc_descriptor (shift));
15466 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
15468 else if (is_a <scalar_int_mode> (mode, &int_mode)
15469 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15471 dw_die_ref type_die = base_type_for_mode (int_mode, 0);
15472 dw_loc_descr_ref cvt;
15473 if (type_die == NULL)
15474 return NULL;
15475 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15476 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15477 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15478 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15479 add_loc_descr (&op0, cvt);
15480 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15481 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15482 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15483 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15484 add_loc_descr (&op1, cvt);
15487 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
15488 op = DW_OP_lt;
15489 else
15490 op = DW_OP_gt;
15491 ret = op0;
15492 add_loc_descr (&ret, op1);
15493 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
15494 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15495 add_loc_descr (&ret, bra_node);
15496 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15497 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
15498 add_loc_descr (&ret, drop_node);
15499 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15500 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
15501 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
15502 && is_a <scalar_int_mode> (mode, &int_mode)
15503 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15504 ret = convert_descriptor_to_mode (int_mode, ret);
15505 return ret;
15508 /* Helper function for mem_loc_descriptor. Perform OP binary op,
15509 but after converting arguments to type_die, afterwards
15510 convert back to unsigned. */
15512 static dw_loc_descr_ref
15513 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
15514 scalar_int_mode mode, machine_mode mem_mode)
15516 dw_loc_descr_ref cvt, op0, op1;
15518 if (type_die == NULL)
15519 return NULL;
15520 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15521 VAR_INIT_STATUS_INITIALIZED);
15522 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15523 VAR_INIT_STATUS_INITIALIZED);
15524 if (op0 == NULL || op1 == NULL)
15525 return NULL;
15526 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15527 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15528 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15529 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15530 add_loc_descr (&op0, cvt);
15531 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15532 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15533 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15534 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15535 add_loc_descr (&op1, cvt);
15536 add_loc_descr (&op0, op1);
15537 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
15538 return convert_descriptor_to_mode (mode, op0);
15541 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
15542 const0 is DW_OP_lit0 or corresponding typed constant,
15543 const1 is DW_OP_lit1 or corresponding typed constant
15544 and constMSB is constant with just the MSB bit set
15545 for the mode):
15546 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
15547 L1: const0 DW_OP_swap
15548 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
15549 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15550 L3: DW_OP_drop
15551 L4: DW_OP_nop
15553 CTZ is similar:
15554 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
15555 L1: const0 DW_OP_swap
15556 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
15557 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15558 L3: DW_OP_drop
15559 L4: DW_OP_nop
15561 FFS is similar:
15562 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
15563 L1: const1 DW_OP_swap
15564 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
15565 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15566 L3: DW_OP_drop
15567 L4: DW_OP_nop */
15569 static dw_loc_descr_ref
15570 clz_loc_descriptor (rtx rtl, scalar_int_mode mode,
15571 machine_mode mem_mode)
15573 dw_loc_descr_ref op0, ret, tmp;
15574 HOST_WIDE_INT valv;
15575 dw_loc_descr_ref l1jump, l1label;
15576 dw_loc_descr_ref l2jump, l2label;
15577 dw_loc_descr_ref l3jump, l3label;
15578 dw_loc_descr_ref l4jump, l4label;
15579 rtx msb;
15581 if (GET_MODE (XEXP (rtl, 0)) != mode)
15582 return NULL;
15584 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15585 VAR_INIT_STATUS_INITIALIZED);
15586 if (op0 == NULL)
15587 return NULL;
15588 ret = op0;
15589 if (GET_CODE (rtl) == CLZ)
15591 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
15592 valv = GET_MODE_BITSIZE (mode);
15594 else if (GET_CODE (rtl) == FFS)
15595 valv = 0;
15596 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
15597 valv = GET_MODE_BITSIZE (mode);
15598 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15599 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
15600 add_loc_descr (&ret, l1jump);
15601 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
15602 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
15603 VAR_INIT_STATUS_INITIALIZED);
15604 if (tmp == NULL)
15605 return NULL;
15606 add_loc_descr (&ret, tmp);
15607 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
15608 add_loc_descr (&ret, l4jump);
15609 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
15610 ? const1_rtx : const0_rtx,
15611 mode, mem_mode,
15612 VAR_INIT_STATUS_INITIALIZED);
15613 if (l1label == NULL)
15614 return NULL;
15615 add_loc_descr (&ret, l1label);
15616 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15617 l2label = new_loc_descr (DW_OP_dup, 0, 0);
15618 add_loc_descr (&ret, l2label);
15619 if (GET_CODE (rtl) != CLZ)
15620 msb = const1_rtx;
15621 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
15622 msb = GEN_INT (HOST_WIDE_INT_1U
15623 << (GET_MODE_BITSIZE (mode) - 1));
15624 else
15625 msb = immed_wide_int_const
15626 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
15627 GET_MODE_PRECISION (mode)), mode);
15628 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
15629 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
15630 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
15631 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
15632 else
15633 tmp = mem_loc_descriptor (msb, mode, mem_mode,
15634 VAR_INIT_STATUS_INITIALIZED);
15635 if (tmp == NULL)
15636 return NULL;
15637 add_loc_descr (&ret, tmp);
15638 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15639 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
15640 add_loc_descr (&ret, l3jump);
15641 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15642 VAR_INIT_STATUS_INITIALIZED);
15643 if (tmp == NULL)
15644 return NULL;
15645 add_loc_descr (&ret, tmp);
15646 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
15647 ? DW_OP_shl : DW_OP_shr, 0, 0));
15648 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15649 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
15650 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15651 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
15652 add_loc_descr (&ret, l2jump);
15653 l3label = new_loc_descr (DW_OP_drop, 0, 0);
15654 add_loc_descr (&ret, l3label);
15655 l4label = new_loc_descr (DW_OP_nop, 0, 0);
15656 add_loc_descr (&ret, l4label);
15657 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15658 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15659 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15660 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15661 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15662 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
15663 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15664 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
15665 return ret;
15668 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
15669 const1 is DW_OP_lit1 or corresponding typed constant):
15670 const0 DW_OP_swap
15671 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
15672 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
15673 L2: DW_OP_drop
15675 PARITY is similar:
15676 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
15677 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
15678 L2: DW_OP_drop */
15680 static dw_loc_descr_ref
15681 popcount_loc_descriptor (rtx rtl, scalar_int_mode mode,
15682 machine_mode mem_mode)
15684 dw_loc_descr_ref op0, ret, tmp;
15685 dw_loc_descr_ref l1jump, l1label;
15686 dw_loc_descr_ref l2jump, l2label;
15688 if (GET_MODE (XEXP (rtl, 0)) != mode)
15689 return NULL;
15691 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15692 VAR_INIT_STATUS_INITIALIZED);
15693 if (op0 == NULL)
15694 return NULL;
15695 ret = op0;
15696 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15697 VAR_INIT_STATUS_INITIALIZED);
15698 if (tmp == NULL)
15699 return NULL;
15700 add_loc_descr (&ret, tmp);
15701 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15702 l1label = new_loc_descr (DW_OP_dup, 0, 0);
15703 add_loc_descr (&ret, l1label);
15704 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
15705 add_loc_descr (&ret, l2jump);
15706 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15707 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
15708 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15709 VAR_INIT_STATUS_INITIALIZED);
15710 if (tmp == NULL)
15711 return NULL;
15712 add_loc_descr (&ret, tmp);
15713 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15714 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
15715 ? DW_OP_plus : DW_OP_xor, 0, 0));
15716 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15717 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15718 VAR_INIT_STATUS_INITIALIZED);
15719 add_loc_descr (&ret, tmp);
15720 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15721 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
15722 add_loc_descr (&ret, l1jump);
15723 l2label = new_loc_descr (DW_OP_drop, 0, 0);
15724 add_loc_descr (&ret, l2label);
15725 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15726 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15727 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15728 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15729 return ret;
15732 /* BSWAP (constS is initial shift count, either 56 or 24):
15733 constS const0
15734 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
15735 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
15736 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
15737 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
15738 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
15740 static dw_loc_descr_ref
15741 bswap_loc_descriptor (rtx rtl, scalar_int_mode mode,
15742 machine_mode mem_mode)
15744 dw_loc_descr_ref op0, ret, tmp;
15745 dw_loc_descr_ref l1jump, l1label;
15746 dw_loc_descr_ref l2jump, l2label;
15748 if (BITS_PER_UNIT != 8
15749 || (GET_MODE_BITSIZE (mode) != 32
15750 && GET_MODE_BITSIZE (mode) != 64))
15751 return NULL;
15753 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15754 VAR_INIT_STATUS_INITIALIZED);
15755 if (op0 == NULL)
15756 return NULL;
15758 ret = op0;
15759 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
15760 mode, mem_mode,
15761 VAR_INIT_STATUS_INITIALIZED);
15762 if (tmp == NULL)
15763 return NULL;
15764 add_loc_descr (&ret, tmp);
15765 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15766 VAR_INIT_STATUS_INITIALIZED);
15767 if (tmp == NULL)
15768 return NULL;
15769 add_loc_descr (&ret, tmp);
15770 l1label = new_loc_descr (DW_OP_pick, 2, 0);
15771 add_loc_descr (&ret, l1label);
15772 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
15773 mode, mem_mode,
15774 VAR_INIT_STATUS_INITIALIZED);
15775 add_loc_descr (&ret, tmp);
15776 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
15777 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
15778 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15779 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
15780 VAR_INIT_STATUS_INITIALIZED);
15781 if (tmp == NULL)
15782 return NULL;
15783 add_loc_descr (&ret, tmp);
15784 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15785 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
15786 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
15787 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
15788 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15789 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15790 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15791 VAR_INIT_STATUS_INITIALIZED);
15792 add_loc_descr (&ret, tmp);
15793 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
15794 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
15795 add_loc_descr (&ret, l2jump);
15796 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
15797 VAR_INIT_STATUS_INITIALIZED);
15798 add_loc_descr (&ret, tmp);
15799 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
15800 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15801 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
15802 add_loc_descr (&ret, l1jump);
15803 l2label = new_loc_descr (DW_OP_drop, 0, 0);
15804 add_loc_descr (&ret, l2label);
15805 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15806 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
15807 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15808 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15809 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15810 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15811 return ret;
15814 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
15815 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
15816 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
15817 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
15819 ROTATERT is similar:
15820 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
15821 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
15822 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
15824 static dw_loc_descr_ref
15825 rotate_loc_descriptor (rtx rtl, scalar_int_mode mode,
15826 machine_mode mem_mode)
15828 rtx rtlop1 = XEXP (rtl, 1);
15829 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
15830 int i;
15832 if (is_narrower_int_mode (GET_MODE (rtlop1), mode))
15833 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
15834 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15835 VAR_INIT_STATUS_INITIALIZED);
15836 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
15837 VAR_INIT_STATUS_INITIALIZED);
15838 if (op0 == NULL || op1 == NULL)
15839 return NULL;
15840 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
15841 for (i = 0; i < 2; i++)
15843 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
15844 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
15845 mode, mem_mode,
15846 VAR_INIT_STATUS_INITIALIZED);
15847 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
15848 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
15849 ? DW_OP_const4u
15850 : HOST_BITS_PER_WIDE_INT == 64
15851 ? DW_OP_const8u : DW_OP_constu,
15852 GET_MODE_MASK (mode), 0);
15853 else
15854 mask[i] = NULL;
15855 if (mask[i] == NULL)
15856 return NULL;
15857 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
15859 ret = op0;
15860 add_loc_descr (&ret, op1);
15861 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
15862 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
15863 if (GET_CODE (rtl) == ROTATERT)
15865 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
15866 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
15867 GET_MODE_BITSIZE (mode), 0));
15869 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
15870 if (mask[0] != NULL)
15871 add_loc_descr (&ret, mask[0]);
15872 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
15873 if (mask[1] != NULL)
15875 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15876 add_loc_descr (&ret, mask[1]);
15877 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15879 if (GET_CODE (rtl) == ROTATE)
15881 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
15882 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
15883 GET_MODE_BITSIZE (mode), 0));
15885 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15886 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
15887 return ret;
15890 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
15891 for DEBUG_PARAMETER_REF RTL. */
15893 static dw_loc_descr_ref
15894 parameter_ref_descriptor (rtx rtl)
15896 dw_loc_descr_ref ret;
15897 dw_die_ref ref;
15899 if (dwarf_strict)
15900 return NULL;
15901 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
15902 /* With LTO during LTRANS we get the late DIE that refers to the early
15903 DIE, thus we add another indirection here. This seems to confuse
15904 gdb enough to make gcc.dg/guality/pr68860-1.c FAIL with LTO. */
15905 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
15906 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
15907 if (ref)
15909 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15910 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15911 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15913 else
15915 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15916 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
15918 return ret;
15921 /* The following routine converts the RTL for a variable or parameter
15922 (resident in memory) into an equivalent Dwarf representation of a
15923 mechanism for getting the address of that same variable onto the top of a
15924 hypothetical "address evaluation" stack.
15926 When creating memory location descriptors, we are effectively transforming
15927 the RTL for a memory-resident object into its Dwarf postfix expression
15928 equivalent. This routine recursively descends an RTL tree, turning
15929 it into Dwarf postfix code as it goes.
15931 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
15933 MEM_MODE is the mode of the memory reference, needed to handle some
15934 autoincrement addressing modes.
15936 Return 0 if we can't represent the location. */
15938 dw_loc_descr_ref
15939 mem_loc_descriptor (rtx rtl, machine_mode mode,
15940 machine_mode mem_mode,
15941 enum var_init_status initialized)
15943 dw_loc_descr_ref mem_loc_result = NULL;
15944 enum dwarf_location_atom op;
15945 dw_loc_descr_ref op0, op1;
15946 rtx inner = NULL_RTX;
15947 poly_int64 offset;
15949 if (mode == VOIDmode)
15950 mode = GET_MODE (rtl);
15952 /* Note that for a dynamically sized array, the location we will generate a
15953 description of here will be the lowest numbered location which is
15954 actually within the array. That's *not* necessarily the same as the
15955 zeroth element of the array. */
15957 rtl = targetm.delegitimize_address (rtl);
15959 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
15960 return NULL;
15962 scalar_int_mode int_mode = BImode, inner_mode, op1_mode;
15963 switch (GET_CODE (rtl))
15965 case POST_INC:
15966 case POST_DEC:
15967 case POST_MODIFY:
15968 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
15970 case SUBREG:
15971 /* The case of a subreg may arise when we have a local (register)
15972 variable or a formal (register) parameter which doesn't quite fill
15973 up an entire register. For now, just assume that it is
15974 legitimate to make the Dwarf info refer to the whole register which
15975 contains the given subreg. */
15976 if (!subreg_lowpart_p (rtl))
15977 break;
15978 inner = SUBREG_REG (rtl);
15979 /* FALLTHRU */
15980 case TRUNCATE:
15981 if (inner == NULL_RTX)
15982 inner = XEXP (rtl, 0);
15983 if (is_a <scalar_int_mode> (mode, &int_mode)
15984 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
15985 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15986 #ifdef POINTERS_EXTEND_UNSIGNED
15987 || (int_mode == Pmode && mem_mode != VOIDmode)
15988 #endif
15990 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE)
15992 mem_loc_result = mem_loc_descriptor (inner,
15993 inner_mode,
15994 mem_mode, initialized);
15995 break;
15997 if (dwarf_strict && dwarf_version < 5)
15998 break;
15999 if (is_a <scalar_int_mode> (mode, &int_mode)
16000 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
16001 ? GET_MODE_SIZE (int_mode) <= GET_MODE_SIZE (inner_mode)
16002 : known_eq (GET_MODE_SIZE (mode), GET_MODE_SIZE (GET_MODE (inner))))
16004 dw_die_ref type_die;
16005 dw_loc_descr_ref cvt;
16007 mem_loc_result = mem_loc_descriptor (inner,
16008 GET_MODE (inner),
16009 mem_mode, initialized);
16010 if (mem_loc_result == NULL)
16011 break;
16012 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16013 if (type_die == NULL)
16015 mem_loc_result = NULL;
16016 break;
16018 if (maybe_ne (GET_MODE_SIZE (mode), GET_MODE_SIZE (GET_MODE (inner))))
16019 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16020 else
16021 cvt = new_loc_descr (dwarf_OP (DW_OP_reinterpret), 0, 0);
16022 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16023 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16024 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16025 add_loc_descr (&mem_loc_result, cvt);
16026 if (is_a <scalar_int_mode> (mode, &int_mode)
16027 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
16029 /* Convert it to untyped afterwards. */
16030 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16031 add_loc_descr (&mem_loc_result, cvt);
16034 break;
16036 case REG:
16037 if (!is_a <scalar_int_mode> (mode, &int_mode)
16038 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
16039 && rtl != arg_pointer_rtx
16040 && rtl != frame_pointer_rtx
16041 #ifdef POINTERS_EXTEND_UNSIGNED
16042 && (int_mode != Pmode || mem_mode == VOIDmode)
16043 #endif
16046 dw_die_ref type_die;
16047 unsigned int debugger_regnum;
16049 if (dwarf_strict && dwarf_version < 5)
16050 break;
16051 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
16052 break;
16053 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16054 if (type_die == NULL)
16055 break;
16057 debugger_regnum = debugger_reg_number (rtl);
16058 if (debugger_regnum == IGNORED_DWARF_REGNUM)
16059 break;
16060 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_regval_type),
16061 debugger_regnum, 0);
16062 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
16063 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
16064 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
16065 break;
16067 /* Whenever a register number forms a part of the description of the
16068 method for calculating the (dynamic) address of a memory resident
16069 object, DWARF rules require the register number be referred to as
16070 a "base register". This distinction is not based in any way upon
16071 what category of register the hardware believes the given register
16072 belongs to. This is strictly DWARF terminology we're dealing with
16073 here. Note that in cases where the location of a memory-resident
16074 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
16075 OP_CONST (0)) the actual DWARF location descriptor that we generate
16076 may just be OP_BASEREG (basereg). This may look deceptively like
16077 the object in question was allocated to a register (rather than in
16078 memory) so DWARF consumers need to be aware of the subtle
16079 distinction between OP_REG and OP_BASEREG. */
16080 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
16081 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
16082 else if (stack_realign_drap
16083 && crtl->drap_reg
16084 && crtl->args.internal_arg_pointer == rtl
16085 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
16087 /* If RTL is internal_arg_pointer, which has been optimized
16088 out, use DRAP instead. */
16089 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
16090 VAR_INIT_STATUS_INITIALIZED);
16092 break;
16094 case SIGN_EXTEND:
16095 case ZERO_EXTEND:
16096 if (!is_a <scalar_int_mode> (mode, &int_mode)
16097 || !is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode))
16098 break;
16099 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
16100 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16101 if (op0 == 0)
16102 break;
16103 else if (GET_CODE (rtl) == ZERO_EXTEND
16104 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16105 && GET_MODE_BITSIZE (inner_mode) < HOST_BITS_PER_WIDE_INT
16106 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
16107 to expand zero extend as two shifts instead of
16108 masking. */
16109 && GET_MODE_SIZE (inner_mode) <= 4)
16111 mem_loc_result = op0;
16112 add_loc_descr (&mem_loc_result,
16113 int_loc_descriptor (GET_MODE_MASK (inner_mode)));
16114 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
16116 else if (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
16118 int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (inner_mode);
16119 shift *= BITS_PER_UNIT;
16120 if (GET_CODE (rtl) == SIGN_EXTEND)
16121 op = DW_OP_shra;
16122 else
16123 op = DW_OP_shr;
16124 mem_loc_result = op0;
16125 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
16126 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
16127 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
16128 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16130 else if (!dwarf_strict || dwarf_version >= 5)
16132 dw_die_ref type_die1, type_die2;
16133 dw_loc_descr_ref cvt;
16135 type_die1 = base_type_for_mode (inner_mode,
16136 GET_CODE (rtl) == ZERO_EXTEND);
16137 if (type_die1 == NULL)
16138 break;
16139 type_die2 = base_type_for_mode (int_mode, 1);
16140 if (type_die2 == NULL)
16141 break;
16142 mem_loc_result = op0;
16143 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16144 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16145 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
16146 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16147 add_loc_descr (&mem_loc_result, cvt);
16148 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16149 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16150 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
16151 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16152 add_loc_descr (&mem_loc_result, cvt);
16154 break;
16156 case MEM:
16158 rtx new_rtl = avoid_constant_pool_reference (rtl);
16159 if (new_rtl != rtl)
16161 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
16162 initialized);
16163 if (mem_loc_result != NULL)
16164 return mem_loc_result;
16167 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
16168 get_address_mode (rtl), mode,
16169 VAR_INIT_STATUS_INITIALIZED);
16170 if (mem_loc_result == NULL)
16171 mem_loc_result = tls_mem_loc_descriptor (rtl);
16172 if (mem_loc_result != NULL)
16174 if (!is_a <scalar_int_mode> (mode, &int_mode)
16175 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16177 dw_die_ref type_die;
16178 dw_loc_descr_ref deref;
16179 HOST_WIDE_INT size;
16181 if (dwarf_strict && dwarf_version < 5)
16182 return NULL;
16183 if (!GET_MODE_SIZE (mode).is_constant (&size))
16184 return NULL;
16185 type_die
16186 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16187 if (type_die == NULL)
16188 return NULL;
16189 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type), size, 0);
16190 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
16191 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
16192 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
16193 add_loc_descr (&mem_loc_result, deref);
16195 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
16196 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
16197 else
16198 add_loc_descr (&mem_loc_result,
16199 new_loc_descr (DW_OP_deref_size,
16200 GET_MODE_SIZE (int_mode), 0));
16202 break;
16204 case LO_SUM:
16205 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
16207 case LABEL_REF:
16208 /* Some ports can transform a symbol ref into a label ref, because
16209 the symbol ref is too far away and has to be dumped into a constant
16210 pool. */
16211 case CONST:
16212 case SYMBOL_REF:
16213 case UNSPEC:
16214 if (!is_a <scalar_int_mode> (mode, &int_mode)
16215 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
16216 #ifdef POINTERS_EXTEND_UNSIGNED
16217 && (int_mode != Pmode || mem_mode == VOIDmode)
16218 #endif
16220 break;
16222 if (GET_CODE (rtl) == UNSPEC)
16224 /* If delegitimize_address couldn't do anything with the UNSPEC, we
16225 can't express it in the debug info. This can happen e.g. with some
16226 TLS UNSPECs. Allow UNSPECs formerly from CONST that the backend
16227 approves. */
16228 bool not_ok = false;
16229 subrtx_var_iterator::array_type array;
16230 FOR_EACH_SUBRTX_VAR (iter, array, rtl, ALL)
16231 if (*iter != rtl && !CONSTANT_P (*iter))
16233 not_ok = true;
16234 break;
16237 if (not_ok)
16238 break;
16240 FOR_EACH_SUBRTX_VAR (iter, array, rtl, ALL)
16241 if (!const_ok_for_output_1 (*iter))
16243 not_ok = true;
16244 break;
16247 if (not_ok)
16248 break;
16250 rtl = gen_rtx_CONST (GET_MODE (rtl), rtl);
16251 goto symref;
16254 if (GET_CODE (rtl) == SYMBOL_REF
16255 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
16257 dw_loc_descr_ref temp;
16259 /* If this is not defined, we have no way to emit the data. */
16260 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
16261 break;
16263 temp = new_addr_loc_descr (rtl, dtprel_true);
16265 /* We check for DWARF 5 here because gdb did not implement
16266 DW_OP_form_tls_address until after 7.12. */
16267 mem_loc_result = new_loc_descr ((dwarf_version >= 5
16268 ? DW_OP_form_tls_address
16269 : DW_OP_GNU_push_tls_address),
16270 0, 0);
16271 add_loc_descr (&mem_loc_result, temp);
16273 break;
16276 if (!const_ok_for_output (rtl))
16278 if (GET_CODE (rtl) == CONST)
16279 switch (GET_CODE (XEXP (rtl, 0)))
16281 case NOT:
16282 op = DW_OP_not;
16283 goto try_const_unop;
16284 case NEG:
16285 op = DW_OP_neg;
16286 goto try_const_unop;
16287 try_const_unop:
16288 rtx arg;
16289 arg = XEXP (XEXP (rtl, 0), 0);
16290 if (!CONSTANT_P (arg))
16291 arg = gen_rtx_CONST (int_mode, arg);
16292 op0 = mem_loc_descriptor (arg, int_mode, mem_mode,
16293 initialized);
16294 if (op0)
16296 mem_loc_result = op0;
16297 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16299 break;
16300 default:
16301 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), int_mode,
16302 mem_mode, initialized);
16303 break;
16305 break;
16308 symref:
16309 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
16310 vec_safe_push (used_rtx_array, rtl);
16311 break;
16313 case CONCAT:
16314 case CONCATN:
16315 case VAR_LOCATION:
16316 case DEBUG_IMPLICIT_PTR:
16317 expansion_failed (NULL_TREE, rtl,
16318 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
16319 return 0;
16321 case ENTRY_VALUE:
16322 if (dwarf_strict && dwarf_version < 5)
16323 return NULL;
16324 if (REG_P (ENTRY_VALUE_EXP (rtl)))
16326 if (!is_a <scalar_int_mode> (mode, &int_mode)
16327 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16328 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
16329 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16330 else
16332 unsigned int debugger_regnum = debugger_reg_number (ENTRY_VALUE_EXP (rtl));
16333 if (debugger_regnum == IGNORED_DWARF_REGNUM)
16334 return NULL;
16335 op0 = one_reg_loc_descriptor (debugger_regnum,
16336 VAR_INIT_STATUS_INITIALIZED);
16339 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
16340 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
16342 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
16343 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16344 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
16345 return NULL;
16347 else
16348 gcc_unreachable ();
16349 if (op0 == NULL)
16350 return NULL;
16351 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_entry_value), 0, 0);
16352 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
16353 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
16354 break;
16356 case DEBUG_PARAMETER_REF:
16357 mem_loc_result = parameter_ref_descriptor (rtl);
16358 break;
16360 case PRE_MODIFY:
16361 /* Extract the PLUS expression nested inside and fall into
16362 PLUS code below. */
16363 rtl = XEXP (rtl, 1);
16364 goto plus;
16366 case PRE_INC:
16367 case PRE_DEC:
16368 /* Turn these into a PLUS expression and fall into the PLUS code
16369 below. */
16370 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
16371 gen_int_mode (GET_CODE (rtl) == PRE_INC
16372 ? GET_MODE_UNIT_SIZE (mem_mode)
16373 : -GET_MODE_UNIT_SIZE (mem_mode),
16374 mode));
16376 /* fall through */
16378 case PLUS:
16379 plus:
16380 if (is_based_loc (rtl)
16381 && is_a <scalar_int_mode> (mode, &int_mode)
16382 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16383 || XEXP (rtl, 0) == arg_pointer_rtx
16384 || XEXP (rtl, 0) == frame_pointer_rtx))
16385 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
16386 INTVAL (XEXP (rtl, 1)),
16387 VAR_INIT_STATUS_INITIALIZED);
16388 else
16390 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16391 VAR_INIT_STATUS_INITIALIZED);
16392 if (mem_loc_result == 0)
16393 break;
16395 if (CONST_INT_P (XEXP (rtl, 1))
16396 && (GET_MODE_SIZE (as_a <scalar_int_mode> (mode))
16397 <= DWARF2_ADDR_SIZE))
16398 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
16399 else
16401 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16402 VAR_INIT_STATUS_INITIALIZED);
16403 if (op1 == 0)
16404 return NULL;
16405 add_loc_descr (&mem_loc_result, op1);
16406 add_loc_descr (&mem_loc_result,
16407 new_loc_descr (DW_OP_plus, 0, 0));
16410 break;
16412 /* If a pseudo-reg is optimized away, it is possible for it to
16413 be replaced with a MEM containing a multiply or shift. */
16414 case MINUS:
16415 op = DW_OP_minus;
16416 goto do_binop;
16418 case MULT:
16419 op = DW_OP_mul;
16420 goto do_binop;
16422 case DIV:
16423 if ((!dwarf_strict || dwarf_version >= 5)
16424 && is_a <scalar_int_mode> (mode, &int_mode)
16425 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16427 mem_loc_result = typed_binop (DW_OP_div, rtl,
16428 base_type_for_mode (mode, 0),
16429 int_mode, mem_mode);
16430 break;
16432 op = DW_OP_div;
16433 goto do_binop;
16435 case UMOD:
16436 op = DW_OP_mod;
16437 goto do_binop;
16439 case ASHIFT:
16440 op = DW_OP_shl;
16441 goto do_shift;
16443 case ASHIFTRT:
16444 op = DW_OP_shra;
16445 goto do_shift;
16447 case LSHIFTRT:
16448 op = DW_OP_shr;
16449 goto do_shift;
16451 do_shift:
16452 if (!is_a <scalar_int_mode> (mode, &int_mode))
16453 break;
16454 op0 = mem_loc_descriptor (XEXP (rtl, 0), int_mode, mem_mode,
16455 VAR_INIT_STATUS_INITIALIZED);
16457 rtx rtlop1 = XEXP (rtl, 1);
16458 if (is_a <scalar_int_mode> (GET_MODE (rtlop1), &op1_mode)
16459 && GET_MODE_BITSIZE (op1_mode) < GET_MODE_BITSIZE (int_mode))
16460 rtlop1 = gen_rtx_ZERO_EXTEND (int_mode, rtlop1);
16461 op1 = mem_loc_descriptor (rtlop1, int_mode, mem_mode,
16462 VAR_INIT_STATUS_INITIALIZED);
16465 if (op0 == 0 || op1 == 0)
16466 break;
16468 mem_loc_result = op0;
16469 add_loc_descr (&mem_loc_result, op1);
16470 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16471 break;
16473 case AND:
16474 op = DW_OP_and;
16475 goto do_binop;
16477 case IOR:
16478 op = DW_OP_or;
16479 goto do_binop;
16481 case XOR:
16482 op = DW_OP_xor;
16483 goto do_binop;
16485 do_binop:
16486 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16487 VAR_INIT_STATUS_INITIALIZED);
16488 if (XEXP (rtl, 0) == XEXP (rtl, 1))
16490 if (op0 == 0)
16491 break;
16492 mem_loc_result = op0;
16493 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_dup, 0, 0));
16494 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16495 break;
16497 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16498 VAR_INIT_STATUS_INITIALIZED);
16500 if (op0 == 0 || op1 == 0)
16501 break;
16503 mem_loc_result = op0;
16504 add_loc_descr (&mem_loc_result, op1);
16505 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16506 break;
16508 case MOD:
16509 if ((!dwarf_strict || dwarf_version >= 5)
16510 && is_a <scalar_int_mode> (mode, &int_mode)
16511 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16513 mem_loc_result = typed_binop (DW_OP_mod, rtl,
16514 base_type_for_mode (mode, 0),
16515 int_mode, mem_mode);
16516 break;
16519 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16520 VAR_INIT_STATUS_INITIALIZED);
16521 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16522 VAR_INIT_STATUS_INITIALIZED);
16524 if (op0 == 0 || op1 == 0)
16525 break;
16527 mem_loc_result = op0;
16528 add_loc_descr (&mem_loc_result, op1);
16529 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
16530 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
16531 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
16532 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
16533 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
16534 break;
16536 case UDIV:
16537 if ((!dwarf_strict || dwarf_version >= 5)
16538 && is_a <scalar_int_mode> (mode, &int_mode))
16540 /* We can use a signed divide if the sign bit is not set. */
16541 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
16543 op = DW_OP_div;
16544 goto do_binop;
16547 mem_loc_result = typed_binop (DW_OP_div, rtl,
16548 base_type_for_mode (int_mode, 1),
16549 int_mode, mem_mode);
16551 break;
16553 case NOT:
16554 op = DW_OP_not;
16555 goto do_unop;
16557 case ABS:
16558 op = DW_OP_abs;
16559 goto do_unop;
16561 case NEG:
16562 op = DW_OP_neg;
16563 goto do_unop;
16565 do_unop:
16566 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16567 VAR_INIT_STATUS_INITIALIZED);
16569 if (op0 == 0)
16570 break;
16572 mem_loc_result = op0;
16573 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16574 break;
16576 case CONST_INT:
16577 if (!is_a <scalar_int_mode> (mode, &int_mode)
16578 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16579 #ifdef POINTERS_EXTEND_UNSIGNED
16580 || (int_mode == Pmode
16581 && mem_mode != VOIDmode
16582 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
16583 #endif
16586 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
16587 break;
16589 if ((!dwarf_strict || dwarf_version >= 5)
16590 && (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT
16591 || GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_DOUBLE_INT))
16593 dw_die_ref type_die = base_type_for_mode (int_mode, 1);
16594 scalar_int_mode amode;
16595 if (type_die == NULL)
16596 return NULL;
16597 if (INTVAL (rtl) >= 0
16598 && (int_mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT, 0)
16599 .exists (&amode))
16600 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
16601 /* const DW_OP_convert <XXX> vs.
16602 DW_OP_const_type <XXX, 1, const>. */
16603 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
16604 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (int_mode))
16606 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
16607 op0 = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16608 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16609 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16610 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
16611 add_loc_descr (&mem_loc_result, op0);
16612 return mem_loc_result;
16614 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0,
16615 INTVAL (rtl));
16616 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16617 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16618 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16619 if (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT)
16620 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
16621 else
16623 mem_loc_result->dw_loc_oprnd2.val_class
16624 = dw_val_class_const_double;
16625 mem_loc_result->dw_loc_oprnd2.v.val_double
16626 = double_int::from_shwi (INTVAL (rtl));
16629 break;
16631 case CONST_DOUBLE:
16632 if (!dwarf_strict || dwarf_version >= 5)
16634 dw_die_ref type_die;
16636 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
16637 CONST_DOUBLE rtx could represent either a large integer
16638 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
16639 the value is always a floating point constant.
16641 When it is an integer, a CONST_DOUBLE is used whenever
16642 the constant requires 2 HWIs to be adequately represented.
16643 We output CONST_DOUBLEs as blocks. */
16644 if (mode == VOIDmode
16645 || (GET_MODE (rtl) == VOIDmode
16646 && maybe_ne (GET_MODE_BITSIZE (mode),
16647 HOST_BITS_PER_DOUBLE_INT)))
16648 break;
16649 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16650 if (type_die == NULL)
16651 return NULL;
16652 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
16653 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16654 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16655 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16656 #if TARGET_SUPPORTS_WIDE_INT == 0
16657 if (!SCALAR_FLOAT_MODE_P (mode))
16659 mem_loc_result->dw_loc_oprnd2.val_class
16660 = dw_val_class_const_double;
16661 mem_loc_result->dw_loc_oprnd2.v.val_double
16662 = rtx_to_double_int (rtl);
16664 else
16665 #endif
16667 scalar_float_mode float_mode = as_a <scalar_float_mode> (mode);
16668 unsigned int length = GET_MODE_SIZE (float_mode);
16669 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
16670 unsigned int elt_size = insert_float (rtl, array);
16672 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
16673 mem_loc_result->dw_loc_oprnd2.v.val_vec.length
16674 = length / elt_size;
16675 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
16676 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
16679 break;
16681 case CONST_WIDE_INT:
16682 if (!dwarf_strict || dwarf_version >= 5)
16684 dw_die_ref type_die;
16686 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16687 if (type_die == NULL)
16688 return NULL;
16689 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
16690 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16691 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16692 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16693 mem_loc_result->dw_loc_oprnd2.val_class
16694 = dw_val_class_wide_int;
16695 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
16696 *mem_loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
16698 break;
16700 case CONST_POLY_INT:
16701 mem_loc_result = int_loc_descriptor (rtx_to_poly_int64 (rtl));
16702 break;
16704 case EQ:
16705 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
16706 break;
16708 case GE:
16709 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
16710 break;
16712 case GT:
16713 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
16714 break;
16716 case LE:
16717 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
16718 break;
16720 case LT:
16721 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
16722 break;
16724 case NE:
16725 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
16726 break;
16728 case GEU:
16729 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
16730 break;
16732 case GTU:
16733 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
16734 break;
16736 case LEU:
16737 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
16738 break;
16740 case LTU:
16741 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
16742 break;
16744 case UMIN:
16745 case UMAX:
16746 if (!SCALAR_INT_MODE_P (mode))
16747 break;
16748 /* FALLTHRU */
16749 case SMIN:
16750 case SMAX:
16751 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
16752 break;
16754 case ZERO_EXTRACT:
16755 case SIGN_EXTRACT:
16756 if (CONST_INT_P (XEXP (rtl, 1))
16757 && CONST_INT_P (XEXP (rtl, 2))
16758 && is_a <scalar_int_mode> (mode, &int_mode)
16759 && is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode)
16760 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16761 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE
16762 && ((unsigned) INTVAL (XEXP (rtl, 1))
16763 + (unsigned) INTVAL (XEXP (rtl, 2))
16764 <= GET_MODE_BITSIZE (int_mode)))
16766 int shift, size;
16767 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
16768 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16769 if (op0 == 0)
16770 break;
16771 if (GET_CODE (rtl) == SIGN_EXTRACT)
16772 op = DW_OP_shra;
16773 else
16774 op = DW_OP_shr;
16775 mem_loc_result = op0;
16776 size = INTVAL (XEXP (rtl, 1));
16777 shift = INTVAL (XEXP (rtl, 2));
16778 if (BITS_BIG_ENDIAN)
16779 shift = GET_MODE_BITSIZE (inner_mode) - shift - size;
16780 if (shift + size != (int) DWARF2_ADDR_SIZE)
16782 add_loc_descr (&mem_loc_result,
16783 int_loc_descriptor (DWARF2_ADDR_SIZE
16784 - shift - size));
16785 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
16787 if (size != (int) DWARF2_ADDR_SIZE)
16789 add_loc_descr (&mem_loc_result,
16790 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
16791 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16794 break;
16796 case IF_THEN_ELSE:
16798 dw_loc_descr_ref op2, bra_node, drop_node;
16799 op0 = mem_loc_descriptor (XEXP (rtl, 0),
16800 GET_MODE (XEXP (rtl, 0)) == VOIDmode
16801 ? word_mode : GET_MODE (XEXP (rtl, 0)),
16802 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16803 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16804 VAR_INIT_STATUS_INITIALIZED);
16805 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
16806 VAR_INIT_STATUS_INITIALIZED);
16807 if (op0 == NULL || op1 == NULL || op2 == NULL)
16808 break;
16810 mem_loc_result = op1;
16811 add_loc_descr (&mem_loc_result, op2);
16812 add_loc_descr (&mem_loc_result, op0);
16813 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
16814 add_loc_descr (&mem_loc_result, bra_node);
16815 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
16816 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
16817 add_loc_descr (&mem_loc_result, drop_node);
16818 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16819 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
16821 break;
16823 case FLOAT_EXTEND:
16824 case FLOAT_TRUNCATE:
16825 case FLOAT:
16826 case UNSIGNED_FLOAT:
16827 case FIX:
16828 case UNSIGNED_FIX:
16829 if (!dwarf_strict || dwarf_version >= 5)
16831 dw_die_ref type_die;
16832 dw_loc_descr_ref cvt;
16834 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
16835 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16836 if (op0 == NULL)
16837 break;
16838 if (is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &int_mode)
16839 && (GET_CODE (rtl) == FLOAT
16840 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE))
16842 type_die = base_type_for_mode (int_mode,
16843 GET_CODE (rtl) == UNSIGNED_FLOAT);
16844 if (type_die == NULL)
16845 break;
16846 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16847 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16848 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16849 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16850 add_loc_descr (&op0, cvt);
16852 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
16853 if (type_die == NULL)
16854 break;
16855 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16856 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16857 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16858 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16859 add_loc_descr (&op0, cvt);
16860 if (is_a <scalar_int_mode> (mode, &int_mode)
16861 && (GET_CODE (rtl) == FIX
16862 || GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE))
16864 op0 = convert_descriptor_to_mode (int_mode, op0);
16865 if (op0 == NULL)
16866 break;
16868 mem_loc_result = op0;
16870 break;
16872 case CLZ:
16873 case CTZ:
16874 case FFS:
16875 if (is_a <scalar_int_mode> (mode, &int_mode))
16876 mem_loc_result = clz_loc_descriptor (rtl, int_mode, mem_mode);
16877 break;
16879 case POPCOUNT:
16880 case PARITY:
16881 if (is_a <scalar_int_mode> (mode, &int_mode))
16882 mem_loc_result = popcount_loc_descriptor (rtl, int_mode, mem_mode);
16883 break;
16885 case BSWAP:
16886 if (is_a <scalar_int_mode> (mode, &int_mode))
16887 mem_loc_result = bswap_loc_descriptor (rtl, int_mode, mem_mode);
16888 break;
16890 case ROTATE:
16891 case ROTATERT:
16892 if (is_a <scalar_int_mode> (mode, &int_mode))
16893 mem_loc_result = rotate_loc_descriptor (rtl, int_mode, mem_mode);
16894 break;
16896 case COMPARE:
16897 /* In theory, we could implement the above. */
16898 /* DWARF cannot represent the unsigned compare operations
16899 natively. */
16900 case SS_MULT:
16901 case US_MULT:
16902 case SS_DIV:
16903 case US_DIV:
16904 case SS_PLUS:
16905 case US_PLUS:
16906 case SS_MINUS:
16907 case US_MINUS:
16908 case SS_NEG:
16909 case US_NEG:
16910 case SS_ABS:
16911 case SS_ASHIFT:
16912 case US_ASHIFT:
16913 case SS_TRUNCATE:
16914 case US_TRUNCATE:
16915 case UNORDERED:
16916 case ORDERED:
16917 case UNEQ:
16918 case UNGE:
16919 case UNGT:
16920 case UNLE:
16921 case UNLT:
16922 case LTGT:
16923 case FRACT_CONVERT:
16924 case UNSIGNED_FRACT_CONVERT:
16925 case SAT_FRACT:
16926 case UNSIGNED_SAT_FRACT:
16927 case SQRT:
16928 case ASM_OPERANDS:
16929 case VEC_MERGE:
16930 case VEC_SELECT:
16931 case VEC_CONCAT:
16932 case VEC_DUPLICATE:
16933 case VEC_SERIES:
16934 case HIGH:
16935 case FMA:
16936 case STRICT_LOW_PART:
16937 case CONST_VECTOR:
16938 case CONST_FIXED:
16939 case CLRSB:
16940 case CLOBBER:
16941 case SMUL_HIGHPART:
16942 case UMUL_HIGHPART:
16943 case BITREVERSE:
16944 case COPYSIGN:
16945 break;
16947 case CONST_STRING:
16948 resolve_one_addr (&rtl);
16949 goto symref;
16951 /* RTL sequences inside PARALLEL record a series of DWARF operations for
16952 the expression. An UNSPEC rtx represents a raw DWARF operation,
16953 new_loc_descr is called for it to build the operation directly.
16954 Otherwise mem_loc_descriptor is called recursively. */
16955 case PARALLEL:
16957 int index = 0;
16958 dw_loc_descr_ref exp_result = NULL;
16960 for (; index < XVECLEN (rtl, 0); index++)
16962 rtx elem = XVECEXP (rtl, 0, index);
16963 if (GET_CODE (elem) == UNSPEC)
16965 /* Each DWARF operation UNSPEC contain two operands, if
16966 one operand is not used for the operation, const0_rtx is
16967 passed. */
16968 gcc_assert (XVECLEN (elem, 0) == 2);
16970 HOST_WIDE_INT dw_op = XINT (elem, 1);
16971 HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
16972 HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
16973 exp_result
16974 = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
16975 oprnd2);
16977 else
16978 exp_result
16979 = mem_loc_descriptor (elem, mode, mem_mode,
16980 VAR_INIT_STATUS_INITIALIZED);
16982 if (!mem_loc_result)
16983 mem_loc_result = exp_result;
16984 else
16985 add_loc_descr (&mem_loc_result, exp_result);
16988 break;
16991 default:
16992 if (flag_checking)
16994 print_rtl (stderr, rtl);
16995 gcc_unreachable ();
16997 break;
17000 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
17001 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
17003 return mem_loc_result;
17006 /* Return a descriptor that describes the concatenation of two locations.
17007 This is typically a complex variable. */
17009 static dw_loc_descr_ref
17010 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
17012 /* At present we only track constant-sized pieces. */
17013 unsigned int size0, size1;
17014 if (!GET_MODE_SIZE (GET_MODE (x0)).is_constant (&size0)
17015 || !GET_MODE_SIZE (GET_MODE (x1)).is_constant (&size1))
17016 return 0;
17018 dw_loc_descr_ref cc_loc_result = NULL;
17019 dw_loc_descr_ref x0_ref
17020 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
17021 dw_loc_descr_ref x1_ref
17022 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
17024 if (x0_ref == 0 || x1_ref == 0)
17025 return 0;
17027 cc_loc_result = x0_ref;
17028 add_loc_descr_op_piece (&cc_loc_result, size0);
17030 add_loc_descr (&cc_loc_result, x1_ref);
17031 add_loc_descr_op_piece (&cc_loc_result, size1);
17033 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
17034 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
17036 return cc_loc_result;
17039 /* Return a descriptor that describes the concatenation of N
17040 locations. */
17042 static dw_loc_descr_ref
17043 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
17045 unsigned int i;
17046 dw_loc_descr_ref cc_loc_result = NULL;
17047 unsigned int n = XVECLEN (concatn, 0);
17048 unsigned int size;
17050 for (i = 0; i < n; ++i)
17052 dw_loc_descr_ref ref;
17053 rtx x = XVECEXP (concatn, 0, i);
17055 /* At present we only track constant-sized pieces. */
17056 if (!GET_MODE_SIZE (GET_MODE (x)).is_constant (&size))
17057 return NULL;
17059 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
17060 if (ref == NULL)
17061 return NULL;
17063 add_loc_descr (&cc_loc_result, ref);
17064 add_loc_descr_op_piece (&cc_loc_result, size);
17067 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
17068 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
17070 return cc_loc_result;
17073 /* Helper function for loc_descriptor. Return DW_OP_implicit_pointer
17074 for DEBUG_IMPLICIT_PTR RTL. */
17076 static dw_loc_descr_ref
17077 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
17079 dw_loc_descr_ref ret;
17080 dw_die_ref ref;
17082 if (dwarf_strict && dwarf_version < 5)
17083 return NULL;
17084 gcc_assert (VAR_P (DEBUG_IMPLICIT_PTR_DECL (rtl))
17085 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
17086 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
17087 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
17088 ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
17089 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
17090 if (ref)
17092 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17093 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
17094 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
17096 else
17098 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
17099 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
17101 return ret;
17104 /* Output a proper Dwarf location descriptor for a variable or parameter
17105 which is either allocated in a register or in a memory location. For a
17106 register, we just generate an OP_REG and the register number. For a
17107 memory location we provide a Dwarf postfix expression describing how to
17108 generate the (dynamic) address of the object onto the address stack.
17110 MODE is mode of the decl if this loc_descriptor is going to be used in
17111 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
17112 allowed, VOIDmode otherwise.
17114 If we don't know how to describe it, return 0. */
17116 static dw_loc_descr_ref
17117 loc_descriptor (rtx rtl, machine_mode mode,
17118 enum var_init_status initialized)
17120 dw_loc_descr_ref loc_result = NULL;
17121 scalar_int_mode int_mode;
17123 switch (GET_CODE (rtl))
17125 case SUBREG:
17126 /* The case of a subreg may arise when we have a local (register)
17127 variable or a formal (register) parameter which doesn't quite fill
17128 up an entire register. For now, just assume that it is
17129 legitimate to make the Dwarf info refer to the whole register which
17130 contains the given subreg. */
17131 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
17132 loc_result = loc_descriptor (SUBREG_REG (rtl),
17133 GET_MODE (SUBREG_REG (rtl)), initialized);
17134 else
17135 goto do_default;
17136 break;
17138 case REG:
17139 loc_result = reg_loc_descriptor (rtl, initialized);
17140 break;
17142 case MEM:
17143 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
17144 GET_MODE (rtl), initialized);
17145 if (loc_result == NULL)
17146 loc_result = tls_mem_loc_descriptor (rtl);
17147 if (loc_result == NULL)
17149 rtx new_rtl = avoid_constant_pool_reference (rtl);
17150 if (new_rtl != rtl)
17151 loc_result = loc_descriptor (new_rtl, mode, initialized);
17153 break;
17155 case CONCAT:
17156 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
17157 initialized);
17158 break;
17160 case CONCATN:
17161 loc_result = concatn_loc_descriptor (rtl, initialized);
17162 break;
17164 case VAR_LOCATION:
17165 /* Single part. */
17166 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
17168 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
17169 if (GET_CODE (loc) == EXPR_LIST)
17170 loc = XEXP (loc, 0);
17171 loc_result = loc_descriptor (loc, mode, initialized);
17172 break;
17175 rtl = XEXP (rtl, 1);
17176 /* FALLTHRU */
17178 case PARALLEL:
17180 rtvec par_elems = XVEC (rtl, 0);
17181 int num_elem = GET_NUM_ELEM (par_elems);
17182 machine_mode mode;
17183 int i, size;
17185 /* Create the first one, so we have something to add to. */
17186 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
17187 VOIDmode, initialized);
17188 if (loc_result == NULL)
17189 return NULL;
17190 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
17191 /* At present we only track constant-sized pieces. */
17192 if (!GET_MODE_SIZE (mode).is_constant (&size))
17193 return NULL;
17194 add_loc_descr_op_piece (&loc_result, size);
17195 for (i = 1; i < num_elem; i++)
17197 dw_loc_descr_ref temp;
17199 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
17200 VOIDmode, initialized);
17201 if (temp == NULL)
17202 return NULL;
17203 add_loc_descr (&loc_result, temp);
17204 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
17205 /* At present we only track constant-sized pieces. */
17206 if (!GET_MODE_SIZE (mode).is_constant (&size))
17207 return NULL;
17208 add_loc_descr_op_piece (&loc_result, size);
17211 break;
17213 case CONST_INT:
17214 if (mode != VOIDmode && mode != BLKmode)
17216 int_mode = as_a <scalar_int_mode> (mode);
17217 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (int_mode),
17218 INTVAL (rtl));
17220 break;
17222 case CONST_DOUBLE:
17223 if (mode == VOIDmode)
17224 mode = GET_MODE (rtl);
17226 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
17228 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
17230 /* Note that a CONST_DOUBLE rtx could represent either an integer
17231 or a floating-point constant. A CONST_DOUBLE is used whenever
17232 the constant requires more than one word in order to be
17233 adequately represented. We output CONST_DOUBLEs as blocks. */
17234 scalar_mode smode = as_a <scalar_mode> (mode);
17235 loc_result = new_loc_descr (DW_OP_implicit_value,
17236 GET_MODE_SIZE (smode), 0);
17237 #if TARGET_SUPPORTS_WIDE_INT == 0
17238 if (!SCALAR_FLOAT_MODE_P (smode))
17240 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
17241 loc_result->dw_loc_oprnd2.v.val_double
17242 = rtx_to_double_int (rtl);
17244 else
17245 #endif
17247 unsigned int length = GET_MODE_SIZE (smode);
17248 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
17249 unsigned int elt_size = insert_float (rtl, array);
17251 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
17252 loc_result->dw_loc_oprnd2.v.val_vec.length = length / elt_size;
17253 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
17254 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
17257 break;
17259 case CONST_WIDE_INT:
17260 if (mode == VOIDmode)
17261 mode = GET_MODE (rtl);
17263 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
17265 int_mode = as_a <scalar_int_mode> (mode);
17266 loc_result = new_loc_descr (DW_OP_implicit_value,
17267 GET_MODE_SIZE (int_mode), 0);
17268 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
17269 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
17270 *loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, int_mode);
17272 break;
17274 case CONST_VECTOR:
17275 if (mode == VOIDmode)
17276 mode = GET_MODE (rtl);
17278 if (mode != VOIDmode
17279 /* The combination of a length and byte elt_size doesn't extend
17280 naturally to boolean vectors, where several elements are packed
17281 into the same byte. */
17282 && GET_MODE_CLASS (mode) != MODE_VECTOR_BOOL
17283 && (dwarf_version >= 4 || !dwarf_strict))
17285 unsigned int length;
17286 if (!CONST_VECTOR_NUNITS (rtl).is_constant (&length))
17287 return NULL;
17289 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
17290 unsigned char *array
17291 = ggc_vec_alloc<unsigned char> (length * elt_size);
17292 unsigned int i;
17293 unsigned char *p;
17294 machine_mode imode = GET_MODE_INNER (mode);
17296 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
17297 switch (GET_MODE_CLASS (mode))
17299 case MODE_VECTOR_INT:
17300 for (i = 0, p = array; i < length; i++, p += elt_size)
17302 rtx elt = CONST_VECTOR_ELT (rtl, i);
17303 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
17305 break;
17307 case MODE_VECTOR_FLOAT:
17308 for (i = 0, p = array; i < length; i++, p += elt_size)
17310 rtx elt = CONST_VECTOR_ELT (rtl, i);
17311 insert_float (elt, p);
17313 break;
17315 default:
17316 gcc_unreachable ();
17319 loc_result = new_loc_descr (DW_OP_implicit_value,
17320 length * elt_size, 0);
17321 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
17322 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
17323 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
17324 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
17326 break;
17328 case CONST:
17329 if (mode == VOIDmode
17330 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
17331 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
17332 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
17334 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
17335 break;
17337 /* FALLTHROUGH */
17338 case SYMBOL_REF:
17339 if (!const_ok_for_output (rtl))
17340 break;
17341 /* FALLTHROUGH */
17342 case LABEL_REF:
17343 if (is_a <scalar_int_mode> (mode, &int_mode)
17344 && GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE
17345 && (dwarf_version >= 4 || !dwarf_strict))
17347 loc_result = new_addr_loc_descr (rtl, dtprel_false);
17348 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
17349 vec_safe_push (used_rtx_array, rtl);
17351 break;
17353 case DEBUG_IMPLICIT_PTR:
17354 loc_result = implicit_ptr_descriptor (rtl, 0);
17355 break;
17357 case PLUS:
17358 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
17359 && CONST_INT_P (XEXP (rtl, 1)))
17361 loc_result
17362 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
17363 break;
17365 /* FALLTHRU */
17366 do_default:
17367 default:
17368 if ((is_a <scalar_int_mode> (mode, &int_mode)
17369 && GET_MODE (rtl) == int_mode
17370 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
17371 && dwarf_version >= 4)
17372 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
17374 /* Value expression. */
17375 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
17376 if (loc_result)
17377 add_loc_descr (&loc_result,
17378 new_loc_descr (DW_OP_stack_value, 0, 0));
17380 break;
17383 return loc_result;
17386 /* We need to figure out what section we should use as the base for the
17387 address ranges where a given location is valid.
17388 1. If this particular DECL has a section associated with it, use that.
17389 2. If this function has a section associated with it, use that.
17390 3. Otherwise, use the text section.
17391 XXX: If you split a variable across multiple sections, we won't notice. */
17393 static const char *
17394 secname_for_decl (const_tree decl)
17396 const char *secname;
17398 if (VAR_OR_FUNCTION_DECL_P (decl)
17399 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
17400 && DECL_SECTION_NAME (decl))
17401 secname = DECL_SECTION_NAME (decl);
17402 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
17404 if (in_cold_section_p)
17406 section *sec = current_function_section ();
17407 if (sec->common.flags & SECTION_NAMED)
17408 return sec->named.name;
17410 secname = DECL_SECTION_NAME (current_function_decl);
17412 else if (cfun && in_cold_section_p)
17413 secname = crtl->subsections.cold_section_label;
17414 else
17415 secname = text_section_label;
17417 return secname;
17420 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
17422 static bool
17423 decl_by_reference_p (tree decl)
17425 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
17426 || VAR_P (decl))
17427 && DECL_BY_REFERENCE (decl));
17430 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
17431 for VARLOC. */
17433 static dw_loc_descr_ref
17434 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
17435 enum var_init_status initialized)
17437 int have_address = 0;
17438 dw_loc_descr_ref descr;
17439 machine_mode mode;
17441 if (want_address != 2)
17443 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
17444 /* Single part. */
17445 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
17447 varloc = PAT_VAR_LOCATION_LOC (varloc);
17448 if (GET_CODE (varloc) == EXPR_LIST)
17449 varloc = XEXP (varloc, 0);
17450 mode = GET_MODE (varloc);
17451 if (MEM_P (varloc))
17453 rtx addr = XEXP (varloc, 0);
17454 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
17455 mode, initialized);
17456 if (descr)
17457 have_address = 1;
17458 else
17460 rtx x = avoid_constant_pool_reference (varloc);
17461 if (x != varloc)
17462 descr = mem_loc_descriptor (x, mode, VOIDmode,
17463 initialized);
17466 else
17467 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
17469 else
17470 return 0;
17472 else
17474 if (GET_CODE (varloc) == VAR_LOCATION)
17475 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
17476 else
17477 mode = DECL_MODE (loc);
17478 descr = loc_descriptor (varloc, mode, initialized);
17479 have_address = 1;
17482 if (!descr)
17483 return 0;
17485 if (want_address == 2 && !have_address
17486 && (dwarf_version >= 4 || !dwarf_strict))
17488 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
17490 expansion_failed (loc, NULL_RTX,
17491 "DWARF address size mismatch");
17492 return 0;
17494 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
17495 have_address = 1;
17497 /* Show if we can't fill the request for an address. */
17498 if (want_address && !have_address)
17500 expansion_failed (loc, NULL_RTX,
17501 "Want address and only have value");
17502 return 0;
17505 /* If we've got an address and don't want one, dereference. */
17506 if (!want_address && have_address)
17508 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
17509 enum dwarf_location_atom op;
17511 if (size > DWARF2_ADDR_SIZE || size == -1)
17513 expansion_failed (loc, NULL_RTX,
17514 "DWARF address size mismatch");
17515 return 0;
17517 else if (size == DWARF2_ADDR_SIZE)
17518 op = DW_OP_deref;
17519 else
17520 op = DW_OP_deref_size;
17522 add_loc_descr (&descr, new_loc_descr (op, size, 0));
17525 return descr;
17528 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
17529 if it is not possible. */
17531 static dw_loc_descr_ref
17532 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
17534 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
17535 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
17536 else if (dwarf_version >= 3 || !dwarf_strict)
17537 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
17538 else
17539 return NULL;
17542 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
17543 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
17545 static dw_loc_descr_ref
17546 dw_sra_loc_expr (tree decl, rtx loc)
17548 rtx p;
17549 unsigned HOST_WIDE_INT padsize = 0;
17550 dw_loc_descr_ref descr, *descr_tail;
17551 unsigned HOST_WIDE_INT decl_size;
17552 rtx varloc;
17553 enum var_init_status initialized;
17555 if (DECL_SIZE (decl) == NULL
17556 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
17557 return NULL;
17559 decl_size = tree_to_uhwi (DECL_SIZE (decl));
17560 descr = NULL;
17561 descr_tail = &descr;
17563 for (p = loc; p; p = XEXP (p, 1))
17565 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
17566 rtx loc_note = *decl_piece_varloc_ptr (p);
17567 dw_loc_descr_ref cur_descr;
17568 dw_loc_descr_ref *tail, last = NULL;
17569 unsigned HOST_WIDE_INT opsize = 0;
17571 if (loc_note == NULL_RTX
17572 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
17574 padsize += bitsize;
17575 continue;
17577 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
17578 varloc = NOTE_VAR_LOCATION (loc_note);
17579 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
17580 if (cur_descr == NULL)
17582 padsize += bitsize;
17583 continue;
17586 /* Check that cur_descr either doesn't use
17587 DW_OP_*piece operations, or their sum is equal
17588 to bitsize. Otherwise we can't embed it. */
17589 for (tail = &cur_descr; *tail != NULL;
17590 tail = &(*tail)->dw_loc_next)
17591 if ((*tail)->dw_loc_opc == DW_OP_piece)
17593 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
17594 * BITS_PER_UNIT;
17595 last = *tail;
17597 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
17599 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
17600 last = *tail;
17603 if (last != NULL && opsize != bitsize)
17605 padsize += bitsize;
17606 /* Discard the current piece of the descriptor and release any
17607 addr_table entries it uses. */
17608 remove_loc_list_addr_table_entries (cur_descr);
17609 continue;
17612 /* If there is a hole, add DW_OP_*piece after empty DWARF
17613 expression, which means that those bits are optimized out. */
17614 if (padsize)
17616 if (padsize > decl_size)
17618 remove_loc_list_addr_table_entries (cur_descr);
17619 goto discard_descr;
17621 decl_size -= padsize;
17622 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
17623 if (*descr_tail == NULL)
17625 remove_loc_list_addr_table_entries (cur_descr);
17626 goto discard_descr;
17628 descr_tail = &(*descr_tail)->dw_loc_next;
17629 padsize = 0;
17631 *descr_tail = cur_descr;
17632 descr_tail = tail;
17633 if (bitsize > decl_size)
17634 goto discard_descr;
17635 decl_size -= bitsize;
17636 if (last == NULL)
17638 HOST_WIDE_INT offset = 0;
17639 if (GET_CODE (varloc) == VAR_LOCATION
17640 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
17642 varloc = PAT_VAR_LOCATION_LOC (varloc);
17643 if (GET_CODE (varloc) == EXPR_LIST)
17644 varloc = XEXP (varloc, 0);
17648 if (GET_CODE (varloc) == CONST
17649 || GET_CODE (varloc) == SIGN_EXTEND
17650 || GET_CODE (varloc) == ZERO_EXTEND)
17651 varloc = XEXP (varloc, 0);
17652 else if (GET_CODE (varloc) == SUBREG)
17653 varloc = SUBREG_REG (varloc);
17654 else
17655 break;
17657 while (1);
17658 /* DW_OP_bit_size offset should be zero for register
17659 or implicit location descriptions and empty location
17660 descriptions, but for memory addresses needs big endian
17661 adjustment. */
17662 if (MEM_P (varloc))
17664 unsigned HOST_WIDE_INT memsize;
17665 if (!poly_uint64 (MEM_SIZE (varloc)).is_constant (&memsize))
17666 goto discard_descr;
17667 memsize *= BITS_PER_UNIT;
17668 if (memsize != bitsize)
17670 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
17671 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
17672 goto discard_descr;
17673 if (memsize < bitsize)
17674 goto discard_descr;
17675 if (BITS_BIG_ENDIAN)
17676 offset = memsize - bitsize;
17680 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
17681 if (*descr_tail == NULL)
17682 goto discard_descr;
17683 descr_tail = &(*descr_tail)->dw_loc_next;
17687 /* If there were any non-empty expressions, add padding till the end of
17688 the decl. */
17689 if (descr != NULL && decl_size != 0)
17691 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
17692 if (*descr_tail == NULL)
17693 goto discard_descr;
17695 return descr;
17697 discard_descr:
17698 /* Discard the descriptor and release any addr_table entries it uses. */
17699 remove_loc_list_addr_table_entries (descr);
17700 return NULL;
17703 /* Return the dwarf representation of the location list LOC_LIST of
17704 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
17705 function. */
17707 static dw_loc_list_ref
17708 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
17710 const char *endname, *secname;
17711 var_loc_view endview;
17712 rtx varloc;
17713 enum var_init_status initialized;
17714 struct var_loc_node *node;
17715 dw_loc_descr_ref descr;
17716 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
17717 dw_loc_list_ref list = NULL;
17718 dw_loc_list_ref *listp = &list;
17720 /* Now that we know what section we are using for a base,
17721 actually construct the list of locations.
17722 The first location information is what is passed to the
17723 function that creates the location list, and the remaining
17724 locations just get added on to that list.
17725 Note that we only know the start address for a location
17726 (IE location changes), so to build the range, we use
17727 the range [current location start, next location start].
17728 This means we have to special case the last node, and generate
17729 a range of [last location start, end of function label]. */
17731 if (cfun && crtl->has_bb_partition)
17733 bool save_in_cold_section_p = in_cold_section_p;
17734 in_cold_section_p = first_function_block_is_cold;
17735 if (loc_list->last_before_switch == NULL)
17736 in_cold_section_p = !in_cold_section_p;
17737 secname = secname_for_decl (decl);
17738 in_cold_section_p = save_in_cold_section_p;
17740 else
17741 secname = secname_for_decl (decl);
17743 for (node = loc_list->first; node; node = node->next)
17745 bool range_across_switch = false;
17746 if (GET_CODE (node->loc) == EXPR_LIST
17747 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
17749 if (GET_CODE (node->loc) == EXPR_LIST)
17751 descr = NULL;
17752 /* This requires DW_OP_{,bit_}piece, which is not usable
17753 inside DWARF expressions. */
17754 if (want_address == 2)
17755 descr = dw_sra_loc_expr (decl, node->loc);
17757 else
17759 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
17760 varloc = NOTE_VAR_LOCATION (node->loc);
17761 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
17763 if (descr)
17765 /* If section switch happens in between node->label
17766 and node->next->label (or end of function) and
17767 we can't emit it as a single entry list,
17768 emit two ranges, first one ending at the end
17769 of first partition and second one starting at the
17770 beginning of second partition. */
17771 if (node == loc_list->last_before_switch
17772 && (node != loc_list->first || loc_list->first->next
17773 /* If we are to emit a view number, we will emit
17774 a loclist rather than a single location
17775 expression for the entire function (see
17776 loc_list_has_views), so we have to split the
17777 range that straddles across partitions. */
17778 || !ZERO_VIEW_P (node->view))
17779 && current_function_decl)
17781 endname = cfun->fde->dw_fde_end;
17782 endview = 0;
17783 range_across_switch = true;
17785 /* The variable has a location between NODE->LABEL and
17786 NODE->NEXT->LABEL. */
17787 else if (node->next)
17788 endname = node->next->label, endview = node->next->view;
17789 /* If the variable has a location at the last label
17790 it keeps its location until the end of function. */
17791 else if (!current_function_decl)
17792 endname = text_end_label, endview = 0;
17793 else
17795 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
17796 current_function_funcdef_no);
17797 endname = ggc_strdup (label_id);
17798 endview = 0;
17801 *listp = new_loc_list (descr, node->label, node->view,
17802 endname, endview, secname);
17803 if (TREE_CODE (decl) == PARM_DECL
17804 && node == loc_list->first
17805 && NOTE_P (node->loc)
17806 && strcmp (node->label, endname) == 0)
17807 (*listp)->force = true;
17808 listp = &(*listp)->dw_loc_next;
17812 if (cfun
17813 && crtl->has_bb_partition
17814 && node == loc_list->last_before_switch)
17816 bool save_in_cold_section_p = in_cold_section_p;
17817 in_cold_section_p = !first_function_block_is_cold;
17818 secname = secname_for_decl (decl);
17819 in_cold_section_p = save_in_cold_section_p;
17822 if (range_across_switch)
17824 if (GET_CODE (node->loc) == EXPR_LIST)
17825 descr = dw_sra_loc_expr (decl, node->loc);
17826 else
17828 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
17829 varloc = NOTE_VAR_LOCATION (node->loc);
17830 descr = dw_loc_list_1 (decl, varloc, want_address,
17831 initialized);
17833 gcc_assert (descr);
17834 /* The variable has a location between NODE->LABEL and
17835 NODE->NEXT->LABEL. */
17836 if (node->next)
17837 endname = node->next->label, endview = node->next->view;
17838 else
17839 endname = cfun->fde->dw_fde_second_end, endview = 0;
17840 *listp = new_loc_list (descr, cfun->fde->dw_fde_second_begin, 0,
17841 endname, endview, secname);
17842 listp = &(*listp)->dw_loc_next;
17846 /* Try to avoid the overhead of a location list emitting a location
17847 expression instead, but only if we didn't have more than one
17848 location entry in the first place. If some entries were not
17849 representable, we don't want to pretend a single entry that was
17850 applies to the entire scope in which the variable is
17851 available. */
17852 if (list && loc_list->first->next)
17853 gen_llsym (list);
17854 else
17855 maybe_gen_llsym (list);
17857 return list;
17860 /* Return true if the loc_list has only single element and thus
17861 can be represented as location description. */
17863 static bool
17864 single_element_loc_list_p (dw_loc_list_ref list)
17866 gcc_assert (!list->dw_loc_next || list->ll_symbol);
17867 return !list->ll_symbol;
17870 /* Duplicate a single element of location list. */
17872 static inline dw_loc_descr_ref
17873 copy_loc_descr (dw_loc_descr_ref ref)
17875 dw_loc_descr_ref copy = ggc_alloc<dw_loc_descr_node> ();
17876 memcpy (copy, ref, sizeof (dw_loc_descr_node));
17877 return copy;
17880 /* To each location in list LIST append loc descr REF. */
17882 static void
17883 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
17885 dw_loc_descr_ref copy;
17886 add_loc_descr (&list->expr, ref);
17887 list = list->dw_loc_next;
17888 while (list)
17890 copy = copy_loc_descr (ref);
17891 add_loc_descr (&list->expr, copy);
17892 while (copy->dw_loc_next)
17893 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
17894 list = list->dw_loc_next;
17898 /* To each location in list LIST prepend loc descr REF. */
17900 static void
17901 prepend_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
17903 dw_loc_descr_ref copy;
17904 dw_loc_descr_ref ref_end = list->expr;
17905 add_loc_descr (&ref, list->expr);
17906 list->expr = ref;
17907 list = list->dw_loc_next;
17908 while (list)
17910 dw_loc_descr_ref end = list->expr;
17911 list->expr = copy = copy_loc_descr (ref);
17912 while (copy->dw_loc_next != ref_end)
17913 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
17914 copy->dw_loc_next = end;
17915 list = list->dw_loc_next;
17919 /* Given two lists RET and LIST
17920 produce location list that is result of adding expression in LIST
17921 to expression in RET on each position in program.
17922 Might be destructive on both RET and LIST.
17924 TODO: We handle only simple cases of RET or LIST having at most one
17925 element. General case would involve sorting the lists in program order
17926 and merging them that will need some additional work.
17927 Adding that will improve quality of debug info especially for SRA-ed
17928 structures. */
17930 static void
17931 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
17933 if (!list)
17934 return;
17935 if (!*ret)
17937 *ret = list;
17938 return;
17940 if (!list->dw_loc_next)
17942 add_loc_descr_to_each (*ret, list->expr);
17943 return;
17945 if (!(*ret)->dw_loc_next)
17947 prepend_loc_descr_to_each (list, (*ret)->expr);
17948 *ret = list;
17949 return;
17951 expansion_failed (NULL_TREE, NULL_RTX,
17952 "Don't know how to merge two non-trivial"
17953 " location lists.\n");
17954 *ret = NULL;
17955 return;
17958 /* LOC is constant expression. Try a luck, look it up in constant
17959 pool and return its loc_descr of its address. */
17961 static dw_loc_descr_ref
17962 cst_pool_loc_descr (tree loc)
17964 /* Get an RTL for this, if something has been emitted. */
17965 rtx rtl = lookup_constant_def (loc);
17967 if (!rtl || !MEM_P (rtl))
17969 gcc_assert (!rtl);
17970 return 0;
17972 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
17974 /* TODO: We might get more coverage if we was actually delaying expansion
17975 of all expressions till end of compilation when constant pools are fully
17976 populated. */
17977 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
17979 expansion_failed (loc, NULL_RTX,
17980 "CST value in contant pool but not marked.");
17981 return 0;
17983 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
17984 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
17987 /* Return dw_loc_list representing address of addr_expr LOC
17988 by looking for inner INDIRECT_REF expression and turning
17989 it into simple arithmetics.
17991 See loc_list_from_tree for the meaning of CONTEXT. */
17993 static dw_loc_list_ref
17994 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
17995 loc_descr_context *context)
17997 tree obj, offset;
17998 poly_int64 bitsize, bitpos, bytepos;
17999 machine_mode mode;
18000 int unsignedp, reversep, volatilep = 0;
18001 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
18003 obj = get_inner_reference (TREE_OPERAND (loc, 0),
18004 &bitsize, &bitpos, &offset, &mode,
18005 &unsignedp, &reversep, &volatilep);
18006 STRIP_NOPS (obj);
18007 if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos))
18009 expansion_failed (loc, NULL_RTX, "bitfield access");
18010 return 0;
18012 if (!INDIRECT_REF_P (obj))
18014 expansion_failed (obj,
18015 NULL_RTX, "no indirect ref in inner refrence");
18016 return 0;
18018 if (!offset && known_eq (bitpos, 0))
18019 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
18020 context);
18021 else if (toplev
18022 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
18023 && (dwarf_version >= 4 || !dwarf_strict))
18025 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
18026 if (!list_ret)
18027 return 0;
18028 if (offset)
18030 /* Variable offset. */
18031 list_ret1 = loc_list_from_tree (offset, 0, context);
18032 if (list_ret1 == 0)
18033 return 0;
18034 add_loc_list (&list_ret, list_ret1);
18035 if (!list_ret)
18036 return 0;
18037 add_loc_descr_to_each (list_ret,
18038 new_loc_descr (DW_OP_plus, 0, 0));
18040 HOST_WIDE_INT value;
18041 if (bytepos.is_constant (&value) && value > 0)
18042 add_loc_descr_to_each (list_ret,
18043 new_loc_descr (DW_OP_plus_uconst, value, 0));
18044 else if (maybe_ne (bytepos, 0))
18045 loc_list_plus_const (list_ret, bytepos);
18046 add_loc_descr_to_each (list_ret,
18047 new_loc_descr (DW_OP_stack_value, 0, 0));
18049 return list_ret;
18052 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
18053 all operations from LOC are nops, move to the last one. Insert in NOPS all
18054 operations that are skipped. */
18056 static void
18057 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
18058 hash_set<dw_loc_descr_ref> &nops)
18060 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
18062 nops.add (loc);
18063 loc = loc->dw_loc_next;
18067 /* Helper for loc_descr_without_nops: free the location description operation
18068 P. */
18070 bool
18071 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
18073 ggc_free (loc);
18074 return true;
18077 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
18078 finishes LOC. */
18080 static void
18081 loc_descr_without_nops (dw_loc_descr_ref &loc)
18083 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
18084 return;
18086 /* Set of all DW_OP_nop operations we remove. */
18087 hash_set<dw_loc_descr_ref> nops;
18089 /* First, strip all prefix NOP operations in order to keep the head of the
18090 operations list. */
18091 loc_descr_to_next_no_nop (loc, nops);
18093 for (dw_loc_descr_ref cur = loc; cur != NULL;)
18095 /* For control flow operations: strip "prefix" nops in destination
18096 labels. */
18097 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
18098 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
18099 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
18100 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
18102 /* Do the same for the operations that follow, then move to the next
18103 iteration. */
18104 if (cur->dw_loc_next != NULL)
18105 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
18106 cur = cur->dw_loc_next;
18109 nops.traverse<void *, free_loc_descr> (NULL);
18113 struct dwarf_procedure_info;
18115 /* Helper structure for location descriptions generation. */
18116 struct loc_descr_context
18118 /* The type that is implicitly referenced by DW_OP_push_object_address, or
18119 NULL_TREE if DW_OP_push_object_address in invalid for this location
18120 description. This is used when processing PLACEHOLDER_EXPR nodes. */
18121 tree context_type;
18122 /* The ..._DECL node that should be translated as a
18123 DW_OP_push_object_address operation. */
18124 tree base_decl;
18125 /* Information about the DWARF procedure we are currently generating. NULL if
18126 we are not generating a DWARF procedure. */
18127 struct dwarf_procedure_info *dpi;
18128 /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
18129 by consumer. Used for DW_TAG_generic_subrange attributes. */
18130 bool placeholder_arg;
18131 /* True if PLACEHOLDER_EXPR has been seen. */
18132 bool placeholder_seen;
18133 /* True if strict preservation of signedness has been requested. */
18134 bool strict_signedness;
18137 /* DWARF procedures generation
18139 DWARF expressions (aka. location descriptions) are used to encode variable
18140 things such as sizes or offsets. Such computations can have redundant parts
18141 that can be factorized in order to reduce the size of the output debug
18142 information. This is the whole point of DWARF procedures.
18144 Thanks to stor-layout.cc, size and offset expressions in GENERIC trees are
18145 already factorized into functions ("size functions") in order to handle very
18146 big and complex types. Such functions are quite simple: they have integral
18147 arguments, they return an integral result and their body contains only a
18148 return statement with arithmetic expressions. This is the only kind of
18149 function we are interested in translating into DWARF procedures, here.
18151 DWARF expressions and DWARF procedure are executed using a stack, so we have
18152 to define some calling convention for them to interact. Let's say that:
18154 - Before calling a DWARF procedure, DWARF expressions must push on the stack
18155 all arguments in reverse order (right-to-left) so that when the DWARF
18156 procedure execution starts, the first argument is the top of the stack.
18158 - Then, when returning, the DWARF procedure must have consumed all arguments
18159 on the stack, must have pushed the result and touched nothing else.
18161 - Each integral argument and the result are integral types can be hold in a
18162 single stack slot.
18164 - We call "frame offset" the number of stack slots that are "under DWARF
18165 procedure control": it includes the arguments slots, the temporaries and
18166 the result slot. Thus, it is equal to the number of arguments when the
18167 procedure execution starts and must be equal to one (the result) when it
18168 returns. */
18170 /* Helper structure used when generating operations for a DWARF procedure. */
18171 struct dwarf_procedure_info
18173 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
18174 currently translated. */
18175 tree fndecl;
18176 /* The number of arguments FNDECL takes. */
18177 unsigned args_count;
18180 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
18181 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
18182 equate it to this DIE. */
18184 static dw_die_ref
18185 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
18186 dw_die_ref parent_die)
18188 dw_die_ref dwarf_proc_die;
18190 if ((dwarf_version < 3 && dwarf_strict)
18191 || location == NULL)
18192 return NULL;
18194 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
18195 if (fndecl)
18196 equate_decl_number_to_die (fndecl, dwarf_proc_die);
18197 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
18198 return dwarf_proc_die;
18201 /* Return whether TYPE is a supported type as a DWARF procedure argument
18202 type or return type (we handle only scalar types and pointer types that
18203 aren't wider than the DWARF expression evaluation stack). */
18205 static bool
18206 is_handled_procedure_type (tree type)
18208 return ((INTEGRAL_TYPE_P (type)
18209 || TREE_CODE (type) == OFFSET_TYPE
18210 || TREE_CODE (type) == POINTER_TYPE)
18211 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
18214 /* Helper for resolve_args_picking: do the same but stop when coming across
18215 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
18216 offset *before* evaluating the corresponding operation. */
18218 static bool
18219 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
18220 struct dwarf_procedure_info *dpi,
18221 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
18223 /* The "frame_offset" identifier is already used to name a macro... */
18224 unsigned frame_offset_ = initial_frame_offset;
18225 dw_loc_descr_ref l;
18227 for (l = loc; l != NULL;)
18229 bool existed;
18230 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
18232 /* If we already met this node, there is nothing to compute anymore. */
18233 if (existed)
18235 /* Make sure that the stack size is consistent wherever the execution
18236 flow comes from. */
18237 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
18238 break;
18240 l_frame_offset = frame_offset_;
18242 /* If needed, relocate the picking offset with respect to the frame
18243 offset. */
18244 if (l->frame_offset_rel)
18246 unsigned HOST_WIDE_INT off;
18247 switch (l->dw_loc_opc)
18249 case DW_OP_pick:
18250 off = l->dw_loc_oprnd1.v.val_unsigned;
18251 break;
18252 case DW_OP_dup:
18253 off = 0;
18254 break;
18255 case DW_OP_over:
18256 off = 1;
18257 break;
18258 default:
18259 gcc_unreachable ();
18261 /* frame_offset_ is the size of the current stack frame, including
18262 incoming arguments. Besides, the arguments are pushed
18263 right-to-left. Thus, in order to access the Nth argument from
18264 this operation node, the picking has to skip temporaries *plus*
18265 one stack slot per argument (0 for the first one, 1 for the second
18266 one, etc.).
18268 The targetted argument number (N) is already set as the operand,
18269 and the number of temporaries can be computed with:
18270 frame_offsets_ - dpi->args_count */
18271 off += frame_offset_ - dpi->args_count;
18273 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
18274 if (off > 255)
18275 return false;
18277 if (off == 0)
18279 l->dw_loc_opc = DW_OP_dup;
18280 l->dw_loc_oprnd1.v.val_unsigned = 0;
18282 else if (off == 1)
18284 l->dw_loc_opc = DW_OP_over;
18285 l->dw_loc_oprnd1.v.val_unsigned = 0;
18287 else
18289 l->dw_loc_opc = DW_OP_pick;
18290 l->dw_loc_oprnd1.v.val_unsigned = off;
18294 /* Update frame_offset according to the effect the current operation has
18295 on the stack. */
18296 switch (l->dw_loc_opc)
18298 case DW_OP_deref:
18299 case DW_OP_swap:
18300 case DW_OP_rot:
18301 case DW_OP_abs:
18302 case DW_OP_neg:
18303 case DW_OP_not:
18304 case DW_OP_plus_uconst:
18305 case DW_OP_skip:
18306 case DW_OP_reg0:
18307 case DW_OP_reg1:
18308 case DW_OP_reg2:
18309 case DW_OP_reg3:
18310 case DW_OP_reg4:
18311 case DW_OP_reg5:
18312 case DW_OP_reg6:
18313 case DW_OP_reg7:
18314 case DW_OP_reg8:
18315 case DW_OP_reg9:
18316 case DW_OP_reg10:
18317 case DW_OP_reg11:
18318 case DW_OP_reg12:
18319 case DW_OP_reg13:
18320 case DW_OP_reg14:
18321 case DW_OP_reg15:
18322 case DW_OP_reg16:
18323 case DW_OP_reg17:
18324 case DW_OP_reg18:
18325 case DW_OP_reg19:
18326 case DW_OP_reg20:
18327 case DW_OP_reg21:
18328 case DW_OP_reg22:
18329 case DW_OP_reg23:
18330 case DW_OP_reg24:
18331 case DW_OP_reg25:
18332 case DW_OP_reg26:
18333 case DW_OP_reg27:
18334 case DW_OP_reg28:
18335 case DW_OP_reg29:
18336 case DW_OP_reg30:
18337 case DW_OP_reg31:
18338 case DW_OP_bregx:
18339 case DW_OP_piece:
18340 case DW_OP_deref_size:
18341 case DW_OP_nop:
18342 case DW_OP_bit_piece:
18343 case DW_OP_implicit_value:
18344 case DW_OP_stack_value:
18345 case DW_OP_deref_type:
18346 case DW_OP_convert:
18347 case DW_OP_reinterpret:
18348 case DW_OP_GNU_deref_type:
18349 case DW_OP_GNU_convert:
18350 case DW_OP_GNU_reinterpret:
18351 break;
18353 case DW_OP_addr:
18354 case DW_OP_const1u:
18355 case DW_OP_const1s:
18356 case DW_OP_const2u:
18357 case DW_OP_const2s:
18358 case DW_OP_const4u:
18359 case DW_OP_const4s:
18360 case DW_OP_const8u:
18361 case DW_OP_const8s:
18362 case DW_OP_constu:
18363 case DW_OP_consts:
18364 case DW_OP_dup:
18365 case DW_OP_over:
18366 case DW_OP_pick:
18367 case DW_OP_lit0:
18368 case DW_OP_lit1:
18369 case DW_OP_lit2:
18370 case DW_OP_lit3:
18371 case DW_OP_lit4:
18372 case DW_OP_lit5:
18373 case DW_OP_lit6:
18374 case DW_OP_lit7:
18375 case DW_OP_lit8:
18376 case DW_OP_lit9:
18377 case DW_OP_lit10:
18378 case DW_OP_lit11:
18379 case DW_OP_lit12:
18380 case DW_OP_lit13:
18381 case DW_OP_lit14:
18382 case DW_OP_lit15:
18383 case DW_OP_lit16:
18384 case DW_OP_lit17:
18385 case DW_OP_lit18:
18386 case DW_OP_lit19:
18387 case DW_OP_lit20:
18388 case DW_OP_lit21:
18389 case DW_OP_lit22:
18390 case DW_OP_lit23:
18391 case DW_OP_lit24:
18392 case DW_OP_lit25:
18393 case DW_OP_lit26:
18394 case DW_OP_lit27:
18395 case DW_OP_lit28:
18396 case DW_OP_lit29:
18397 case DW_OP_lit30:
18398 case DW_OP_lit31:
18399 case DW_OP_breg0:
18400 case DW_OP_breg1:
18401 case DW_OP_breg2:
18402 case DW_OP_breg3:
18403 case DW_OP_breg4:
18404 case DW_OP_breg5:
18405 case DW_OP_breg6:
18406 case DW_OP_breg7:
18407 case DW_OP_breg8:
18408 case DW_OP_breg9:
18409 case DW_OP_breg10:
18410 case DW_OP_breg11:
18411 case DW_OP_breg12:
18412 case DW_OP_breg13:
18413 case DW_OP_breg14:
18414 case DW_OP_breg15:
18415 case DW_OP_breg16:
18416 case DW_OP_breg17:
18417 case DW_OP_breg18:
18418 case DW_OP_breg19:
18419 case DW_OP_breg20:
18420 case DW_OP_breg21:
18421 case DW_OP_breg22:
18422 case DW_OP_breg23:
18423 case DW_OP_breg24:
18424 case DW_OP_breg25:
18425 case DW_OP_breg26:
18426 case DW_OP_breg27:
18427 case DW_OP_breg28:
18428 case DW_OP_breg29:
18429 case DW_OP_breg30:
18430 case DW_OP_breg31:
18431 case DW_OP_fbreg:
18432 case DW_OP_push_object_address:
18433 case DW_OP_call_frame_cfa:
18434 case DW_OP_GNU_variable_value:
18435 case DW_OP_GNU_addr_index:
18436 case DW_OP_GNU_const_index:
18437 ++frame_offset_;
18438 break;
18440 case DW_OP_drop:
18441 case DW_OP_xderef:
18442 case DW_OP_and:
18443 case DW_OP_div:
18444 case DW_OP_minus:
18445 case DW_OP_mod:
18446 case DW_OP_mul:
18447 case DW_OP_or:
18448 case DW_OP_plus:
18449 case DW_OP_shl:
18450 case DW_OP_shr:
18451 case DW_OP_shra:
18452 case DW_OP_xor:
18453 case DW_OP_bra:
18454 case DW_OP_eq:
18455 case DW_OP_ge:
18456 case DW_OP_gt:
18457 case DW_OP_le:
18458 case DW_OP_lt:
18459 case DW_OP_ne:
18460 case DW_OP_regx:
18461 case DW_OP_xderef_size:
18462 --frame_offset_;
18463 break;
18465 case DW_OP_call2:
18466 case DW_OP_call4:
18467 case DW_OP_call_ref:
18469 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
18470 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
18472 if (stack_usage == NULL)
18473 return false;
18474 frame_offset_ += *stack_usage;
18475 break;
18478 case DW_OP_implicit_pointer:
18479 case DW_OP_entry_value:
18480 case DW_OP_const_type:
18481 case DW_OP_regval_type:
18482 case DW_OP_form_tls_address:
18483 case DW_OP_GNU_push_tls_address:
18484 case DW_OP_GNU_uninit:
18485 case DW_OP_GNU_encoded_addr:
18486 case DW_OP_GNU_implicit_pointer:
18487 case DW_OP_GNU_entry_value:
18488 case DW_OP_GNU_const_type:
18489 case DW_OP_GNU_regval_type:
18490 case DW_OP_GNU_parameter_ref:
18491 /* loc_list_from_tree will probably not output these operations for
18492 size functions, so assume they will not appear here. */
18493 /* Fall through... */
18495 default:
18496 gcc_unreachable ();
18499 /* Now, follow the control flow (except subroutine calls). */
18500 switch (l->dw_loc_opc)
18502 case DW_OP_bra:
18503 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
18504 frame_offsets))
18505 return false;
18506 /* Fall through. */
18508 case DW_OP_skip:
18509 l = l->dw_loc_oprnd1.v.val_loc;
18510 break;
18512 case DW_OP_stack_value:
18513 return true;
18515 default:
18516 l = l->dw_loc_next;
18517 break;
18521 return true;
18524 /* Make a DFS over operations reachable through LOC (i.e. follow branch
18525 operations) in order to resolve the operand of DW_OP_pick operations that
18526 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
18527 offset *before* LOC is executed. Return if all relocations were
18528 successful. */
18530 static bool
18531 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
18532 struct dwarf_procedure_info *dpi)
18534 /* Associate to all visited operations the frame offset *before* evaluating
18535 this operation. */
18536 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
18538 return
18539 resolve_args_picking_1 (loc, initial_frame_offset, dpi, frame_offsets);
18542 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
18543 Return NULL if it is not possible. */
18545 static dw_die_ref
18546 function_to_dwarf_procedure (tree fndecl)
18548 struct dwarf_procedure_info dpi;
18549 struct loc_descr_context ctx = {
18550 NULL_TREE, /* context_type */
18551 NULL_TREE, /* base_decl */
18552 &dpi, /* dpi */
18553 false, /* placeholder_arg */
18554 false, /* placeholder_seen */
18555 true /* strict_signedness */
18557 dw_die_ref dwarf_proc_die;
18558 tree tree_body = DECL_SAVED_TREE (fndecl);
18559 dw_loc_descr_ref loc_body, epilogue;
18561 tree cursor;
18562 unsigned i;
18564 /* Do not generate multiple DWARF procedures for the same function
18565 declaration. */
18566 dwarf_proc_die = lookup_decl_die (fndecl);
18567 if (dwarf_proc_die != NULL)
18568 return dwarf_proc_die;
18570 /* DWARF procedures are available starting with the DWARFv3 standard. */
18571 if (dwarf_version < 3 && dwarf_strict)
18572 return NULL;
18574 /* We handle only functions for which we still have a body, that return a
18575 supported type and that takes arguments with supported types. Note that
18576 there is no point translating functions that return nothing. */
18577 if (tree_body == NULL_TREE
18578 || DECL_RESULT (fndecl) == NULL_TREE
18579 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
18580 return NULL;
18582 for (cursor = DECL_ARGUMENTS (fndecl);
18583 cursor != NULL_TREE;
18584 cursor = TREE_CHAIN (cursor))
18585 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
18586 return NULL;
18588 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
18589 if (TREE_CODE (tree_body) != RETURN_EXPR)
18590 return NULL;
18591 tree_body = TREE_OPERAND (tree_body, 0);
18592 if (TREE_CODE (tree_body) != MODIFY_EXPR
18593 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
18594 return NULL;
18595 tree_body = TREE_OPERAND (tree_body, 1);
18597 /* Try to translate the body expression itself. Note that this will probably
18598 cause an infinite recursion if its call graph has a cycle. This is very
18599 unlikely for size functions, however, so don't bother with such things at
18600 the moment. */
18601 dpi.fndecl = fndecl;
18602 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
18603 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
18604 if (!loc_body)
18605 return NULL;
18607 /* After evaluating all operands in "loc_body", we should still have on the
18608 stack all arguments plus the desired function result (top of the stack).
18609 Generate code in order to keep only the result in our stack frame. */
18610 epilogue = NULL;
18611 for (i = 0; i < dpi.args_count; ++i)
18613 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
18614 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
18615 op_couple->dw_loc_next->dw_loc_next = epilogue;
18616 epilogue = op_couple;
18618 add_loc_descr (&loc_body, epilogue);
18619 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
18620 return NULL;
18622 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
18623 because they are considered useful. Now there is an epilogue, they are
18624 not anymore, so give it another try. */
18625 loc_descr_without_nops (loc_body);
18627 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
18628 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
18629 though, given that size functions do not come from source, so they should
18630 not have a dedicated DW_TAG_subprogram DIE. */
18631 dwarf_proc_die
18632 = new_dwarf_proc_die (loc_body, fndecl,
18633 get_context_die (DECL_CONTEXT (fndecl)));
18635 /* The called DWARF procedure consumes one stack slot per argument and
18636 returns one stack slot. */
18637 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
18639 return dwarf_proc_die;
18642 /* Helper function for loc_list_from_tree. Perform OP binary op,
18643 but after converting arguments to type_die, afterwards convert
18644 back to unsigned. */
18646 static dw_loc_list_ref
18647 typed_binop_from_tree (enum dwarf_location_atom op, tree loc,
18648 dw_die_ref type_die, scalar_int_mode mode,
18649 struct loc_descr_context *context)
18651 dw_loc_list_ref op0, op1;
18652 dw_loc_descr_ref cvt, binop;
18654 if (type_die == NULL)
18655 return NULL;
18657 op0 = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
18658 op1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
18659 if (op0 == NULL || op1 == NULL)
18660 return NULL;
18662 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
18663 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18664 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
18665 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
18666 add_loc_descr_to_each (op0, cvt);
18668 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
18669 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18670 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
18671 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
18672 add_loc_descr_to_each (op1, cvt);
18674 add_loc_list (&op0, op1);
18675 if (op0 == NULL)
18676 return NULL;
18678 binop = new_loc_descr (op, 0, 0);
18679 convert_descriptor_to_mode (mode, binop);
18680 add_loc_descr_to_each (op0, binop);
18682 return op0;
18685 /* Generate Dwarf location list representing LOC.
18686 If WANT_ADDRESS is false, expression computing LOC will be computed
18687 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
18688 if WANT_ADDRESS is 2, expression computing address useable in location
18689 will be returned (i.e. DW_OP_reg can be used
18690 to refer to register values).
18692 CONTEXT provides information to customize the location descriptions
18693 generation. Its context_type field specifies what type is implicitly
18694 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
18695 will not be generated.
18697 Its DPI field determines whether we are generating a DWARF expression for a
18698 DWARF procedure, so PARM_DECL references are processed specifically.
18700 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
18701 and dpi fields were null. */
18703 static dw_loc_list_ref
18704 loc_list_from_tree_1 (tree loc, int want_address,
18705 struct loc_descr_context *context)
18707 dw_loc_descr_ref ret = NULL, ret1 = NULL;
18708 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
18709 int have_address = 0;
18710 enum dwarf_location_atom op;
18712 /* ??? Most of the time we do not take proper care for sign/zero
18713 extending the values properly. Hopefully this won't be a real
18714 problem... */
18716 if (context != NULL
18717 && context->base_decl == loc
18718 && want_address == 0)
18720 if (dwarf_version >= 3 || !dwarf_strict)
18721 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
18722 NULL, 0, NULL, 0, NULL);
18723 else
18724 return NULL;
18727 switch (TREE_CODE (loc))
18729 case ERROR_MARK:
18730 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
18731 return 0;
18733 case PLACEHOLDER_EXPR:
18734 /* This case involves extracting fields from an object to determine the
18735 position of other fields. It is supposed to appear only as the first
18736 operand of COMPONENT_REF nodes and to reference precisely the type
18737 that the context allows or its enclosing type. */
18738 if (context != NULL
18739 && (TREE_TYPE (loc) == context->context_type
18740 || TREE_TYPE (loc) == TYPE_CONTEXT (context->context_type))
18741 && want_address >= 1)
18743 if (dwarf_version >= 3 || !dwarf_strict)
18745 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
18746 have_address = 1;
18747 break;
18749 else
18750 return NULL;
18752 /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
18753 the single argument passed by consumer. */
18754 else if (context != NULL
18755 && context->placeholder_arg
18756 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
18757 && want_address == 0)
18759 ret = new_loc_descr (DW_OP_pick, 0, 0);
18760 ret->frame_offset_rel = 1;
18761 context->placeholder_seen = true;
18762 break;
18764 else
18765 expansion_failed (loc, NULL_RTX,
18766 "PLACEHOLDER_EXPR for an unexpected type");
18767 break;
18769 case CALL_EXPR:
18771 tree callee = get_callee_fndecl (loc);
18772 dw_die_ref dwarf_proc;
18774 if (callee
18775 && is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee)))
18776 && (dwarf_proc = function_to_dwarf_procedure (callee)))
18778 /* DWARF procedures are used for size functions, which are built
18779 when size expressions contain conditional constructs, so we
18780 request strict preservation of signedness for comparisons. */
18781 bool old_strict_signedness;
18782 if (context)
18784 old_strict_signedness = context->strict_signedness;
18785 context->strict_signedness = true;
18788 /* Evaluate arguments right-to-left so that the first argument
18789 will be the top-most one on the stack. */
18790 for (int i = call_expr_nargs (loc) - 1; i >= 0; --i)
18792 tree arg = CALL_EXPR_ARG (loc, i);
18793 ret1 = loc_descriptor_from_tree (arg, 0, context);
18794 if (!ret1)
18796 expansion_failed (arg, NULL_RTX, "CALL_EXPR argument");
18797 return NULL;
18799 add_loc_descr (&ret, ret1);
18802 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
18803 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18804 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
18805 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
18806 add_loc_descr (&ret, ret1);
18807 if (context)
18808 context->strict_signedness = old_strict_signedness;
18810 else
18811 expansion_failed (loc, NULL_RTX, "CALL_EXPR target");
18812 break;
18815 case PREINCREMENT_EXPR:
18816 case PREDECREMENT_EXPR:
18817 case POSTINCREMENT_EXPR:
18818 case POSTDECREMENT_EXPR:
18819 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
18820 /* There are no opcodes for these operations. */
18821 return 0;
18823 case ADDR_EXPR:
18824 /* If we already want an address, see if there is INDIRECT_REF inside
18825 e.g. for &this->field. */
18826 if (want_address)
18828 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
18829 (loc, want_address == 2, context);
18830 if (list_ret)
18831 have_address = 1;
18832 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
18833 && (ret = cst_pool_loc_descr (loc)))
18834 have_address = 1;
18836 /* Otherwise, process the argument and look for the address. */
18837 if (!list_ret && !ret)
18838 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
18839 else
18841 if (want_address)
18842 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
18843 return NULL;
18845 break;
18847 case VAR_DECL:
18848 if (DECL_THREAD_LOCAL_P (loc))
18850 rtx rtl;
18851 enum dwarf_location_atom tls_op;
18852 enum dtprel_bool dtprel = dtprel_false;
18854 if (targetm.have_tls)
18856 /* If this is not defined, we have no way to emit the
18857 data. */
18858 if (!targetm.asm_out.output_dwarf_dtprel)
18859 return 0;
18861 /* The way DW_OP_GNU_push_tls_address is specified, we
18862 can only look up addresses of objects in the current
18863 module. We used DW_OP_addr as first op, but that's
18864 wrong, because DW_OP_addr is relocated by the debug
18865 info consumer, while DW_OP_GNU_push_tls_address
18866 operand shouldn't be. */
18867 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
18868 return 0;
18869 dtprel = dtprel_true;
18870 /* We check for DWARF 5 here because gdb did not implement
18871 DW_OP_form_tls_address until after 7.12. */
18872 tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
18873 : DW_OP_GNU_push_tls_address);
18875 else
18877 if (!targetm.emutls.debug_form_tls_address
18878 || !(dwarf_version >= 3 || !dwarf_strict))
18879 return 0;
18880 /* We stuffed the control variable into the DECL_VALUE_EXPR
18881 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
18882 no longer appear in gimple code. We used the control
18883 variable in specific so that we could pick it up here. */
18884 loc = DECL_VALUE_EXPR (loc);
18885 tls_op = DW_OP_form_tls_address;
18888 rtl = rtl_for_decl_location (loc);
18889 if (rtl == NULL_RTX)
18890 return 0;
18892 if (!MEM_P (rtl))
18893 return 0;
18894 rtl = XEXP (rtl, 0);
18895 if (! CONSTANT_P (rtl))
18896 return 0;
18898 ret = new_addr_loc_descr (rtl, dtprel);
18899 ret1 = new_loc_descr (tls_op, 0, 0);
18900 add_loc_descr (&ret, ret1);
18902 have_address = 1;
18903 break;
18905 /* FALLTHRU */
18907 case PARM_DECL:
18908 if (context != NULL && context->dpi != NULL
18909 && DECL_CONTEXT (loc) == context->dpi->fndecl)
18911 /* We are generating code for a DWARF procedure and we want to access
18912 one of its arguments: find the appropriate argument offset and let
18913 the resolve_args_picking pass compute the offset that complies
18914 with the stack frame size. */
18915 unsigned i = 0;
18916 tree cursor;
18918 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
18919 cursor != NULL_TREE && cursor != loc;
18920 cursor = TREE_CHAIN (cursor), ++i)
18922 /* If we are translating a DWARF procedure, all referenced parameters
18923 must belong to the current function. */
18924 gcc_assert (cursor != NULL_TREE);
18926 ret = new_loc_descr (DW_OP_pick, i, 0);
18927 ret->frame_offset_rel = 1;
18928 break;
18930 /* FALLTHRU */
18932 case RESULT_DECL:
18933 if (DECL_HAS_VALUE_EXPR_P (loc))
18935 tree value_expr = DECL_VALUE_EXPR (loc);
18937 /* Non-local frame structures are DECL_IGNORED_P variables so we need
18938 to wait until they get an RTX in order to reference them. */
18939 if (early_dwarf
18940 && TREE_CODE (value_expr) == COMPONENT_REF
18941 && VAR_P (TREE_OPERAND (value_expr, 0))
18942 && DECL_NONLOCAL_FRAME (TREE_OPERAND (value_expr, 0)))
18944 else
18945 return loc_list_from_tree_1 (value_expr, want_address, context);
18948 /* FALLTHRU */
18950 case FUNCTION_DECL:
18952 rtx rtl;
18953 var_loc_list *loc_list = lookup_decl_loc (loc);
18955 if (loc_list && loc_list->first)
18957 list_ret = dw_loc_list (loc_list, loc, want_address);
18958 have_address = want_address != 0;
18959 break;
18961 rtl = rtl_for_decl_location (loc);
18962 if (rtl == NULL_RTX)
18964 if (TREE_CODE (loc) != FUNCTION_DECL
18965 && early_dwarf
18966 && want_address != 1
18967 && ! DECL_IGNORED_P (loc)
18968 && (INTEGRAL_TYPE_P (TREE_TYPE (loc))
18969 || POINTER_TYPE_P (TREE_TYPE (loc)))
18970 && (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (loc)))
18971 <= DWARF2_ADDR_SIZE))
18973 dw_die_ref ref = lookup_decl_die (loc);
18974 if (ref)
18976 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
18977 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18978 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
18979 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
18981 else if (current_function_decl
18982 && DECL_CONTEXT (loc) == current_function_decl)
18984 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
18985 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
18986 ret->dw_loc_oprnd1.v.val_decl_ref = loc;
18988 break;
18990 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
18991 return 0;
18993 else if (CONST_INT_P (rtl))
18995 HOST_WIDE_INT val = INTVAL (rtl);
18996 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
18997 val &= GET_MODE_MASK (DECL_MODE (loc));
18998 ret = int_loc_descriptor (val);
19000 else if (GET_CODE (rtl) == CONST_STRING)
19002 expansion_failed (loc, NULL_RTX, "CONST_STRING");
19003 return 0;
19005 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
19006 ret = new_addr_loc_descr (rtl, dtprel_false);
19007 else
19009 machine_mode mode, mem_mode;
19011 /* Certain constructs can only be represented at top-level. */
19012 if (want_address == 2)
19014 ret = loc_descriptor (rtl, VOIDmode,
19015 VAR_INIT_STATUS_INITIALIZED);
19016 have_address = 1;
19018 else
19020 mode = GET_MODE (rtl);
19021 mem_mode = VOIDmode;
19022 if (MEM_P (rtl))
19024 mem_mode = mode;
19025 mode = get_address_mode (rtl);
19026 rtl = XEXP (rtl, 0);
19027 have_address = 1;
19029 ret = mem_loc_descriptor (rtl, mode, mem_mode,
19030 VAR_INIT_STATUS_INITIALIZED);
19032 if (!ret)
19033 expansion_failed (loc, rtl,
19034 "failed to produce loc descriptor for rtl");
19037 break;
19039 case MEM_REF:
19040 if (!integer_zerop (TREE_OPERAND (loc, 1)))
19042 have_address = 1;
19043 goto do_plus;
19045 /* Fallthru. */
19046 case INDIRECT_REF:
19047 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19048 have_address = 1;
19049 break;
19051 case TARGET_MEM_REF:
19052 case SSA_NAME:
19053 case DEBUG_EXPR_DECL:
19054 return NULL;
19056 case COMPOUND_EXPR:
19057 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
19058 context);
19060 CASE_CONVERT:
19061 case VIEW_CONVERT_EXPR:
19062 case SAVE_EXPR:
19063 case MODIFY_EXPR:
19064 case NON_LVALUE_EXPR:
19065 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
19066 context);
19068 case COMPONENT_REF:
19069 case BIT_FIELD_REF:
19070 case ARRAY_REF:
19071 case ARRAY_RANGE_REF:
19072 case REALPART_EXPR:
19073 case IMAGPART_EXPR:
19075 tree obj, offset;
19076 poly_int64 bitsize, bitpos, bytepos;
19077 machine_mode mode;
19078 int unsignedp, reversep, volatilep = 0;
19080 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
19081 &unsignedp, &reversep, &volatilep);
19083 gcc_assert (obj != loc);
19085 list_ret = loc_list_from_tree_1 (obj,
19086 want_address == 2
19087 && known_eq (bitpos, 0)
19088 && !offset ? 2 : 1,
19089 context);
19090 /* TODO: We can extract value of the small expression via shifting even
19091 for nonzero bitpos. */
19092 if (list_ret == 0)
19093 return 0;
19094 if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
19095 || !multiple_p (bitsize, BITS_PER_UNIT))
19097 expansion_failed (loc, NULL_RTX,
19098 "bitfield access");
19099 return 0;
19102 if (offset != NULL_TREE)
19104 /* Variable offset. */
19105 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
19106 if (list_ret1 == 0)
19107 return 0;
19108 add_loc_list (&list_ret, list_ret1);
19109 if (!list_ret)
19110 return 0;
19111 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
19114 HOST_WIDE_INT value;
19115 if (bytepos.is_constant (&value) && value > 0)
19116 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst,
19117 value, 0));
19118 else if (maybe_ne (bytepos, 0))
19119 loc_list_plus_const (list_ret, bytepos);
19121 have_address = 1;
19122 break;
19125 case INTEGER_CST:
19126 if ((want_address || !tree_fits_shwi_p (loc))
19127 && (ret = cst_pool_loc_descr (loc)))
19128 have_address = 1;
19129 else if (want_address == 2
19130 && tree_fits_shwi_p (loc)
19131 && (ret = address_of_int_loc_descriptor
19132 (int_size_in_bytes (TREE_TYPE (loc)),
19133 tree_to_shwi (loc))))
19134 have_address = 1;
19135 else if (tree_fits_shwi_p (loc))
19136 ret = int_loc_descriptor (tree_to_shwi (loc));
19137 else if (tree_fits_uhwi_p (loc))
19138 ret = uint_loc_descriptor (tree_to_uhwi (loc));
19139 else
19141 expansion_failed (loc, NULL_RTX,
19142 "Integer operand is not host integer");
19143 return 0;
19145 break;
19147 case POLY_INT_CST:
19149 if (want_address)
19151 expansion_failed (loc, NULL_RTX,
19152 "constant address with a runtime component");
19153 return 0;
19155 poly_int64 value;
19156 if (!poly_int_tree_p (loc, &value))
19158 expansion_failed (loc, NULL_RTX, "constant too big");
19159 return 0;
19161 ret = int_loc_descriptor (value);
19163 break;
19165 case CONSTRUCTOR:
19166 case REAL_CST:
19167 case STRING_CST:
19168 case COMPLEX_CST:
19169 if ((ret = cst_pool_loc_descr (loc)))
19170 have_address = 1;
19171 else if (TREE_CODE (loc) == CONSTRUCTOR)
19173 tree type = TREE_TYPE (loc);
19174 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
19175 unsigned HOST_WIDE_INT offset = 0;
19176 unsigned HOST_WIDE_INT cnt;
19177 constructor_elt *ce;
19179 if (TREE_CODE (type) == RECORD_TYPE)
19181 /* This is very limited, but it's enough to output
19182 pointers to member functions, as long as the
19183 referenced function is defined in the current
19184 translation unit. */
19185 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
19187 tree val = ce->value;
19189 tree field = ce->index;
19191 if (val)
19192 STRIP_NOPS (val);
19194 if (!field || DECL_BIT_FIELD (field))
19196 expansion_failed (loc, NULL_RTX,
19197 "bitfield in record type constructor");
19198 size = offset = (unsigned HOST_WIDE_INT)-1;
19199 ret = NULL;
19200 break;
19203 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
19204 unsigned HOST_WIDE_INT pos = int_byte_position (field);
19205 gcc_assert (pos + fieldsize <= size);
19206 if (pos < offset)
19208 expansion_failed (loc, NULL_RTX,
19209 "out-of-order fields in record constructor");
19210 size = offset = (unsigned HOST_WIDE_INT)-1;
19211 ret = NULL;
19212 break;
19214 if (pos > offset)
19216 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
19217 add_loc_descr (&ret, ret1);
19218 offset = pos;
19220 if (val && fieldsize != 0)
19222 ret1 = loc_descriptor_from_tree (val, want_address, context);
19223 if (!ret1)
19225 expansion_failed (loc, NULL_RTX,
19226 "unsupported expression in field");
19227 size = offset = (unsigned HOST_WIDE_INT)-1;
19228 ret = NULL;
19229 break;
19231 add_loc_descr (&ret, ret1);
19233 if (fieldsize)
19235 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
19236 add_loc_descr (&ret, ret1);
19237 offset = pos + fieldsize;
19241 if (offset != size)
19243 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
19244 add_loc_descr (&ret, ret1);
19245 offset = size;
19248 have_address = !!want_address;
19250 else
19251 expansion_failed (loc, NULL_RTX,
19252 "constructor of non-record type");
19254 else
19255 /* We can construct small constants here using int_loc_descriptor. */
19256 expansion_failed (loc, NULL_RTX,
19257 "constructor or constant not in constant pool");
19258 break;
19260 case TRUTH_AND_EXPR:
19261 case TRUTH_ANDIF_EXPR:
19262 case BIT_AND_EXPR:
19263 op = DW_OP_and;
19264 goto do_binop;
19266 case TRUTH_XOR_EXPR:
19267 case BIT_XOR_EXPR:
19268 op = DW_OP_xor;
19269 goto do_binop;
19271 case TRUTH_OR_EXPR:
19272 case TRUTH_ORIF_EXPR:
19273 case BIT_IOR_EXPR:
19274 op = DW_OP_or;
19275 goto do_binop;
19277 case EXACT_DIV_EXPR:
19278 case FLOOR_DIV_EXPR:
19279 case TRUNC_DIV_EXPR:
19280 /* Turn a divide by a power of 2 into a shift when possible. */
19281 if (TYPE_UNSIGNED (TREE_TYPE (loc))
19282 && tree_fits_uhwi_p (TREE_OPERAND (loc, 1)))
19284 const int log2 = exact_log2 (tree_to_uhwi (TREE_OPERAND (loc, 1)));
19285 if (log2 > 0)
19287 list_ret
19288 = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19289 if (list_ret == 0)
19290 return 0;
19292 add_loc_descr_to_each (list_ret, uint_loc_descriptor (log2));
19293 add_loc_descr_to_each (list_ret,
19294 new_loc_descr (DW_OP_shr, 0, 0));
19295 break;
19299 /* fall through */
19301 case CEIL_DIV_EXPR:
19302 case ROUND_DIV_EXPR:
19303 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
19305 enum machine_mode mode = TYPE_MODE (TREE_TYPE (loc));
19306 scalar_int_mode int_mode;
19308 if ((dwarf_strict && dwarf_version < 5)
19309 || !is_a <scalar_int_mode> (mode, &int_mode))
19310 return 0;
19312 /* We can use a signed divide if the sign bit is not set. */
19313 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
19315 op = DW_OP_div;
19316 goto do_binop;
19319 list_ret = typed_binop_from_tree (DW_OP_div, loc,
19320 base_type_for_mode (int_mode, 1),
19321 int_mode, context);
19322 break;
19324 op = DW_OP_div;
19325 goto do_binop;
19327 case MINUS_EXPR:
19328 op = DW_OP_minus;
19329 goto do_binop;
19331 case FLOOR_MOD_EXPR:
19332 case CEIL_MOD_EXPR:
19333 case ROUND_MOD_EXPR:
19334 case TRUNC_MOD_EXPR:
19335 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
19337 op = DW_OP_mod;
19338 goto do_binop;
19340 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19341 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
19342 if (list_ret == 0 || list_ret1 == 0)
19343 return 0;
19345 add_loc_list (&list_ret, list_ret1);
19346 if (list_ret == 0)
19347 return 0;
19348 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
19349 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
19350 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
19351 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
19352 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
19353 break;
19355 case MULT_EXPR:
19356 op = DW_OP_mul;
19357 goto do_binop;
19359 case LSHIFT_EXPR:
19360 op = DW_OP_shl;
19361 goto do_binop;
19363 case RSHIFT_EXPR:
19364 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
19365 goto do_binop;
19367 case POINTER_PLUS_EXPR:
19368 case PLUS_EXPR:
19369 do_plus:
19370 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
19372 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
19373 smarter to encode their opposite. The DW_OP_plus_uconst operation
19374 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
19375 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
19376 bytes, Y being the size of the operation that pushes the opposite
19377 of the addend. So let's choose the smallest representation. */
19378 const tree tree_addend = TREE_OPERAND (loc, 1);
19379 offset_int wi_addend;
19380 HOST_WIDE_INT shwi_addend;
19381 dw_loc_descr_ref loc_naddend;
19383 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19384 if (list_ret == 0)
19385 return 0;
19387 /* Try to get the literal to push. It is the opposite of the addend,
19388 so as we rely on wrapping during DWARF evaluation, first decode
19389 the literal as a "DWARF-sized" signed number. */
19390 wi_addend = wi::to_offset (tree_addend);
19391 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
19392 shwi_addend = wi_addend.to_shwi ();
19393 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
19394 ? int_loc_descriptor (-shwi_addend)
19395 : NULL;
19397 if (loc_naddend != NULL
19398 && ((unsigned) size_of_uleb128 (shwi_addend)
19399 > size_of_loc_descr (loc_naddend)))
19401 add_loc_descr_to_each (list_ret, loc_naddend);
19402 add_loc_descr_to_each (list_ret,
19403 new_loc_descr (DW_OP_minus, 0, 0));
19405 else
19407 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
19409 loc_naddend = loc_cur;
19410 loc_cur = loc_cur->dw_loc_next;
19411 ggc_free (loc_naddend);
19413 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
19415 break;
19418 op = DW_OP_plus;
19419 goto do_binop;
19421 case LE_EXPR:
19422 op = DW_OP_le;
19423 goto do_comp_binop;
19425 case GE_EXPR:
19426 op = DW_OP_ge;
19427 goto do_comp_binop;
19429 case LT_EXPR:
19430 op = DW_OP_lt;
19431 goto do_comp_binop;
19433 case GT_EXPR:
19434 op = DW_OP_gt;
19435 goto do_comp_binop;
19437 do_comp_binop:
19438 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
19440 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
19441 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
19442 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
19443 TREE_CODE (loc));
19444 break;
19446 else
19447 goto do_binop;
19449 case EQ_EXPR:
19450 op = DW_OP_eq;
19451 goto do_binop;
19453 case NE_EXPR:
19454 op = DW_OP_ne;
19455 goto do_binop;
19457 do_binop:
19458 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19459 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
19460 if (list_ret == 0 || list_ret1 == 0)
19461 return 0;
19463 add_loc_list (&list_ret, list_ret1);
19464 if (list_ret == 0)
19465 return 0;
19466 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
19467 break;
19469 case TRUTH_NOT_EXPR:
19470 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19471 if (list_ret == 0)
19472 return 0;
19474 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_lit0, 0, 0));
19475 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_eq, 0, 0));
19476 break;
19478 case BIT_NOT_EXPR:
19479 op = DW_OP_not;
19480 goto do_unop;
19482 case ABS_EXPR:
19483 op = DW_OP_abs;
19484 goto do_unop;
19486 case NEGATE_EXPR:
19487 op = DW_OP_neg;
19488 goto do_unop;
19490 do_unop:
19491 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19492 if (list_ret == 0)
19493 return 0;
19495 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
19496 break;
19498 case MIN_EXPR:
19499 case MAX_EXPR:
19501 const enum tree_code code =
19502 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
19504 loc = build3 (COND_EXPR, TREE_TYPE (loc),
19505 build2 (code, integer_type_node,
19506 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
19507 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
19510 /* fall through */
19512 case COND_EXPR:
19514 dw_loc_descr_ref lhs
19515 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
19516 dw_loc_list_ref rhs
19517 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
19518 dw_loc_descr_ref bra_node, jump_node, tmp;
19520 /* DW_OP_bra is branch-on-nonzero so avoid doing useless work. */
19521 if (TREE_CODE (TREE_OPERAND (loc, 0)) == NE_EXPR
19522 && integer_zerop (TREE_OPERAND (TREE_OPERAND (loc, 0), 1)))
19523 list_ret
19524 = loc_list_from_tree_1 (TREE_OPERAND (TREE_OPERAND (loc, 0), 0),
19525 0, context);
19526 /* Likewise, swap the operands for a logically negated condition. */
19527 else if (TREE_CODE (TREE_OPERAND (loc, 0)) == TRUTH_NOT_EXPR)
19529 lhs = loc_descriptor_from_tree (TREE_OPERAND (loc, 2), 0, context);
19530 rhs = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
19531 list_ret
19532 = loc_list_from_tree_1 (TREE_OPERAND (TREE_OPERAND (loc, 0), 0),
19533 0, context);
19535 else
19536 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19537 if (list_ret == 0 || lhs == 0 || rhs == 0)
19538 return 0;
19540 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
19541 add_loc_descr_to_each (list_ret, bra_node);
19543 add_loc_list (&list_ret, rhs);
19544 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
19545 add_loc_descr_to_each (list_ret, jump_node);
19547 add_loc_descr_to_each (list_ret, lhs);
19548 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
19549 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
19551 /* ??? Need a node to point the skip at. Use a nop. */
19552 tmp = new_loc_descr (DW_OP_nop, 0, 0);
19553 add_loc_descr_to_each (list_ret, tmp);
19554 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
19555 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
19557 break;
19559 case FIX_TRUNC_EXPR:
19560 return 0;
19562 case COMPOUND_LITERAL_EXPR:
19563 return loc_list_from_tree_1 (COMPOUND_LITERAL_EXPR_DECL (loc),
19564 0, context);
19566 default:
19567 /* Leave front-end specific codes as simply unknown. This comes
19568 up, for instance, with the C STMT_EXPR. */
19569 if ((unsigned int) TREE_CODE (loc)
19570 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
19572 expansion_failed (loc, NULL_RTX,
19573 "language specific tree node");
19574 return 0;
19577 /* Otherwise this is a generic code; we should just lists all of
19578 these explicitly. We forgot one. */
19579 if (flag_checking)
19580 gcc_unreachable ();
19582 /* In a release build, we want to degrade gracefully: better to
19583 generate incomplete debugging information than to crash. */
19584 return NULL;
19587 if (!ret && !list_ret)
19588 return 0;
19590 if (want_address == 2 && !have_address
19591 && (dwarf_version >= 4 || !dwarf_strict))
19593 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
19595 expansion_failed (loc, NULL_RTX,
19596 "DWARF address size mismatch");
19597 return 0;
19599 if (ret)
19600 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
19601 else
19602 add_loc_descr_to_each (list_ret,
19603 new_loc_descr (DW_OP_stack_value, 0, 0));
19604 have_address = 1;
19606 /* Show if we can't fill the request for an address. */
19607 if (want_address && !have_address)
19609 expansion_failed (loc, NULL_RTX,
19610 "Want address and only have value");
19611 return 0;
19614 gcc_assert (!ret || !list_ret);
19616 /* If we've got an address and don't want one, dereference. */
19617 if (!want_address && have_address)
19619 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
19620 enum machine_mode mode = TYPE_MODE (TREE_TYPE (loc));
19621 scalar_int_mode int_mode;
19622 dw_die_ref type_die;
19623 dw_loc_descr_ref deref;
19625 /* If the size is greater than DWARF2_ADDR_SIZE, bail out. */
19626 if (size > DWARF2_ADDR_SIZE || size == -1)
19628 expansion_failed (loc, NULL_RTX,
19629 "DWARF address size mismatch");
19630 return 0;
19633 /* If it is equal to DWARF2_ADDR_SIZE, extension does not matter. */
19634 else if (size == DWARF2_ADDR_SIZE)
19635 deref = new_loc_descr (DW_OP_deref, size, 0);
19637 /* If it is lower than DWARF2_ADDR_SIZE, DW_OP_deref_size will zero-
19638 extend the value, which is really OK for unsigned types only. */
19639 else if (!(context && context->strict_signedness)
19640 || TYPE_UNSIGNED (TREE_TYPE (loc))
19641 || (dwarf_strict && dwarf_version < 5)
19642 || !is_a <scalar_int_mode> (mode, &int_mode)
19643 || !(type_die = base_type_for_mode (mode, false)))
19644 deref = new_loc_descr (DW_OP_deref_size, size, 0);
19646 /* Use DW_OP_deref_type for signed integral types if possible, but
19647 convert back to the generic type to avoid type mismatches later. */
19648 else
19650 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type), size, 0);
19651 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
19652 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
19653 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
19654 add_loc_descr (&deref,
19655 new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
19658 if (ret)
19659 add_loc_descr (&ret, deref);
19660 else
19661 add_loc_descr_to_each (list_ret, deref);
19664 if (ret)
19665 list_ret = new_loc_list (ret, NULL, 0, NULL, 0, NULL);
19667 return list_ret;
19670 /* Likewise, but strip useless DW_OP_nop operations in the resulting
19671 expressions. */
19673 static dw_loc_list_ref
19674 loc_list_from_tree (tree loc, int want_address,
19675 struct loc_descr_context *context)
19677 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
19679 for (dw_loc_list_ref loc_cur = result;
19680 loc_cur != NULL; loc_cur = loc_cur->dw_loc_next)
19681 loc_descr_without_nops (loc_cur->expr);
19682 return result;
19685 /* Same as above but return only single location expression. */
19686 static dw_loc_descr_ref
19687 loc_descriptor_from_tree (tree loc, int want_address,
19688 struct loc_descr_context *context)
19690 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
19691 if (!ret)
19692 return NULL;
19693 if (ret->dw_loc_next)
19695 expansion_failed (loc, NULL_RTX,
19696 "Location list where only loc descriptor needed");
19697 return NULL;
19699 return ret->expr;
19702 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
19703 pointer to the declared type for the relevant field variable, or return
19704 `integer_type_node' if the given node turns out to be an
19705 ERROR_MARK node. */
19707 static inline tree
19708 field_type (const_tree decl)
19710 tree type;
19712 if (TREE_CODE (decl) == ERROR_MARK)
19713 return integer_type_node;
19715 type = DECL_BIT_FIELD_TYPE (decl);
19716 if (type == NULL_TREE)
19717 type = TREE_TYPE (decl);
19719 return type;
19722 /* Given a pointer to a tree node, return the alignment in bits for
19723 it, or else return BITS_PER_WORD if the node actually turns out to
19724 be an ERROR_MARK node. */
19726 static inline unsigned
19727 simple_type_align_in_bits (const_tree type)
19729 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
19732 static inline unsigned
19733 simple_decl_align_in_bits (const_tree decl)
19735 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
19738 /* Return the result of rounding T up to ALIGN. */
19740 static inline offset_int
19741 round_up_to_align (const offset_int &t, unsigned int align)
19743 return wi::udiv_trunc (t + align - 1, align) * align;
19746 /* Helper structure for RECORD_TYPE processing. */
19747 struct vlr_context
19749 /* Root RECORD_TYPE. It is needed to generate data member location
19750 descriptions in variable-length records (VLR), but also to cope with
19751 variants, which are composed of nested structures multiplexed with
19752 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
19753 function processing a FIELD_DECL, it is required to be non null. */
19754 tree struct_type;
19756 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
19757 QUAL_UNION_TYPE), this holds an expression that computes the offset for
19758 this variant part as part of the root record (in storage units). For
19759 regular records, it must be NULL_TREE. */
19760 tree variant_part_offset;
19763 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
19764 addressed byte of the "containing object" for the given FIELD_DECL. If
19765 possible, return a native constant through CST_OFFSET (in which case NULL is
19766 returned); otherwise return a DWARF expression that computes the offset.
19768 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
19769 that offset is, either because the argument turns out to be a pointer to an
19770 ERROR_MARK node, or because the offset expression is too complex for us.
19772 CTX is required: see the comment for VLR_CONTEXT. */
19774 static dw_loc_descr_ref
19775 field_byte_offset (const_tree decl, struct vlr_context *ctx,
19776 HOST_WIDE_INT *cst_offset)
19778 tree tree_result;
19779 dw_loc_list_ref loc_result;
19781 *cst_offset = 0;
19783 if (TREE_CODE (decl) == ERROR_MARK)
19784 return NULL;
19785 else
19786 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
19788 /* We cannot handle variable bit offsets at the moment, so abort if it's the
19789 case. */
19790 if (TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST)
19791 return NULL;
19793 /* We used to handle only constant offsets in all cases. Now, we handle
19794 properly dynamic byte offsets only when PCC bitfield type doesn't
19795 matter. */
19796 if (PCC_BITFIELD_TYPE_MATTERS
19797 && DECL_BIT_FIELD_TYPE (decl)
19798 && TREE_CODE (DECL_FIELD_OFFSET (decl)) == INTEGER_CST)
19800 offset_int object_offset_in_bits;
19801 offset_int object_offset_in_bytes;
19802 offset_int bitpos_int;
19803 tree type;
19804 tree field_size_tree;
19805 offset_int deepest_bitpos;
19806 offset_int field_size_in_bits;
19807 unsigned int type_align_in_bits;
19808 unsigned int decl_align_in_bits;
19809 offset_int type_size_in_bits;
19811 bitpos_int = wi::to_offset (bit_position (decl));
19812 type = field_type (decl);
19813 type_size_in_bits = offset_int_type_size_in_bits (type);
19814 type_align_in_bits = simple_type_align_in_bits (type);
19816 field_size_tree = DECL_SIZE (decl);
19818 /* The size could be unspecified if there was an error, or for
19819 a flexible array member. */
19820 if (!field_size_tree)
19821 field_size_tree = bitsize_zero_node;
19823 /* If the size of the field is not constant, use the type size. */
19824 if (TREE_CODE (field_size_tree) == INTEGER_CST)
19825 field_size_in_bits = wi::to_offset (field_size_tree);
19826 else
19827 field_size_in_bits = type_size_in_bits;
19829 decl_align_in_bits = simple_decl_align_in_bits (decl);
19831 /* The GCC front-end doesn't make any attempt to keep track of the
19832 starting bit offset (relative to the start of the containing
19833 structure type) of the hypothetical "containing object" for a
19834 bit-field. Thus, when computing the byte offset value for the
19835 start of the "containing object" of a bit-field, we must deduce
19836 this information on our own. This can be rather tricky to do in
19837 some cases. For example, handling the following structure type
19838 definition when compiling for an i386/i486 target (which only
19839 aligns long long's to 32-bit boundaries) can be very tricky:
19841 struct S { int field1; long long field2:31; };
19843 Fortunately, there is a simple rule-of-thumb which can be used
19844 in such cases. When compiling for an i386/i486, GCC will
19845 allocate 8 bytes for the structure shown above. It decides to
19846 do this based upon one simple rule for bit-field allocation.
19847 GCC allocates each "containing object" for each bit-field at
19848 the first (i.e. lowest addressed) legitimate alignment boundary
19849 (based upon the required minimum alignment for the declared
19850 type of the field) which it can possibly use, subject to the
19851 condition that there is still enough available space remaining
19852 in the containing object (when allocated at the selected point)
19853 to fully accommodate all of the bits of the bit-field itself.
19855 This simple rule makes it obvious why GCC allocates 8 bytes for
19856 each object of the structure type shown above. When looking
19857 for a place to allocate the "containing object" for `field2',
19858 the compiler simply tries to allocate a 64-bit "containing
19859 object" at each successive 32-bit boundary (starting at zero)
19860 until it finds a place to allocate that 64- bit field such that
19861 at least 31 contiguous (and previously unallocated) bits remain
19862 within that selected 64 bit field. (As it turns out, for the
19863 example above, the compiler finds it is OK to allocate the
19864 "containing object" 64-bit field at bit-offset zero within the
19865 structure type.)
19867 Here we attempt to work backwards from the limited set of facts
19868 we're given, and we try to deduce from those facts, where GCC
19869 must have believed that the containing object started (within
19870 the structure type). The value we deduce is then used (by the
19871 callers of this routine) to generate DW_AT_location and
19872 DW_AT_bit_offset attributes for fields (both bit-fields and, in
19873 the case of DW_AT_location, regular fields as well). */
19875 /* Figure out the bit-distance from the start of the structure to
19876 the "deepest" bit of the bit-field. */
19877 deepest_bitpos = bitpos_int + field_size_in_bits;
19879 /* This is the tricky part. Use some fancy footwork to deduce
19880 where the lowest addressed bit of the containing object must
19881 be. */
19882 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
19884 /* Round up to type_align by default. This works best for
19885 bitfields. */
19886 object_offset_in_bits
19887 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
19889 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
19891 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
19893 /* Round up to decl_align instead. */
19894 object_offset_in_bits
19895 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
19898 object_offset_in_bytes
19899 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
19900 if (ctx->variant_part_offset == NULL_TREE)
19902 *cst_offset = object_offset_in_bytes.to_shwi ();
19903 return NULL;
19905 tree_result = wide_int_to_tree (sizetype, object_offset_in_bytes);
19907 else
19908 tree_result = byte_position (decl);
19910 if (ctx->variant_part_offset != NULL_TREE)
19911 tree_result = fold_build2 (PLUS_EXPR, TREE_TYPE (tree_result),
19912 ctx->variant_part_offset, tree_result);
19914 /* If the byte offset is a constant, it's simplier to handle a native
19915 constant rather than a DWARF expression. */
19916 if (TREE_CODE (tree_result) == INTEGER_CST)
19918 *cst_offset = wi::to_offset (tree_result).to_shwi ();
19919 return NULL;
19922 struct loc_descr_context loc_ctx = {
19923 ctx->struct_type, /* context_type */
19924 NULL_TREE, /* base_decl */
19925 NULL, /* dpi */
19926 false, /* placeholder_arg */
19927 false, /* placeholder_seen */
19928 false /* strict_signedness */
19930 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
19932 /* We want a DWARF expression: abort if we only have a location list with
19933 multiple elements. */
19934 if (!loc_result || !single_element_loc_list_p (loc_result))
19935 return NULL;
19936 else
19937 return loc_result->expr;
19940 /* The following routines define various Dwarf attributes and any data
19941 associated with them. */
19943 /* Add a location description attribute value to a DIE.
19945 This emits location attributes suitable for whole variables and
19946 whole parameters. Note that the location attributes for struct fields are
19947 generated by the routine `data_member_location_attribute' below. */
19949 static inline void
19950 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
19951 dw_loc_list_ref descr)
19953 bool check_no_locviews = true;
19954 if (descr == 0)
19955 return;
19956 if (single_element_loc_list_p (descr))
19957 add_AT_loc (die, attr_kind, descr->expr);
19958 else
19960 add_AT_loc_list (die, attr_kind, descr);
19961 gcc_assert (descr->ll_symbol);
19962 if (attr_kind == DW_AT_location && descr->vl_symbol
19963 && dwarf2out_locviews_in_attribute ())
19965 add_AT_view_list (die, DW_AT_GNU_locviews);
19966 check_no_locviews = false;
19970 if (check_no_locviews)
19971 gcc_assert (!get_AT (die, DW_AT_GNU_locviews));
19974 /* Add DW_AT_accessibility attribute to DIE if needed. */
19976 static void
19977 add_accessibility_attribute (dw_die_ref die, tree decl)
19979 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19980 children, otherwise the default is DW_ACCESS_public. In DWARF2
19981 the default has always been DW_ACCESS_public. */
19982 if (TREE_PROTECTED (decl))
19983 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19984 else if (TREE_PRIVATE (decl))
19986 if (dwarf_version == 2
19987 || die->die_parent == NULL
19988 || die->die_parent->die_tag != DW_TAG_class_type)
19989 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19991 else if (dwarf_version > 2
19992 && die->die_parent
19993 && die->die_parent->die_tag == DW_TAG_class_type)
19994 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19997 /* Attach the specialized form of location attribute used for data members of
19998 struct and union types. In the special case of a FIELD_DECL node which
19999 represents a bit-field, the "offset" part of this special location
20000 descriptor must indicate the distance in bytes from the lowest-addressed
20001 byte of the containing struct or union type to the lowest-addressed byte of
20002 the "containing object" for the bit-field. (See the `field_byte_offset'
20003 function above).
20005 For any given bit-field, the "containing object" is a hypothetical object
20006 (of some integral or enum type) within which the given bit-field lives. The
20007 type of this hypothetical "containing object" is always the same as the
20008 declared type of the individual bit-field itself (for GCC anyway... the
20009 DWARF spec doesn't actually mandate this). Note that it is the size (in
20010 bytes) of the hypothetical "containing object" which will be given in the
20011 DW_AT_byte_size attribute for this bit-field. (See the
20012 `byte_size_attribute' function below.) It is also used when calculating the
20013 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
20014 function below.)
20016 CTX is required: see the comment for VLR_CONTEXT. */
20018 static void
20019 add_data_member_location_attribute (dw_die_ref die,
20020 tree decl,
20021 struct vlr_context *ctx)
20023 HOST_WIDE_INT offset;
20024 dw_loc_descr_ref loc_descr = 0;
20026 if (TREE_CODE (decl) == TREE_BINFO)
20028 /* We're working on the TAG_inheritance for a base class. */
20029 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
20031 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
20032 aren't at a fixed offset from all (sub)objects of the same
20033 type. We need to extract the appropriate offset from our
20034 vtable. The following dwarf expression means
20036 BaseAddr = ObAddr + *((*ObAddr) - Offset)
20038 This is specific to the V3 ABI, of course. */
20040 dw_loc_descr_ref tmp;
20042 /* Make a copy of the object address. */
20043 tmp = new_loc_descr (DW_OP_dup, 0, 0);
20044 add_loc_descr (&loc_descr, tmp);
20046 /* Extract the vtable address. */
20047 tmp = new_loc_descr (DW_OP_deref, 0, 0);
20048 add_loc_descr (&loc_descr, tmp);
20050 /* Calculate the address of the offset. */
20051 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
20052 gcc_assert (offset < 0);
20054 tmp = int_loc_descriptor (-offset);
20055 add_loc_descr (&loc_descr, tmp);
20056 tmp = new_loc_descr (DW_OP_minus, 0, 0);
20057 add_loc_descr (&loc_descr, tmp);
20059 /* Extract the offset. */
20060 tmp = new_loc_descr (DW_OP_deref, 0, 0);
20061 add_loc_descr (&loc_descr, tmp);
20063 /* Add it to the object address. */
20064 tmp = new_loc_descr (DW_OP_plus, 0, 0);
20065 add_loc_descr (&loc_descr, tmp);
20067 else
20068 offset = tree_to_shwi (BINFO_OFFSET (decl));
20070 else
20072 loc_descr = field_byte_offset (decl, ctx, &offset);
20074 if (!loc_descr)
20077 /* If loc_descr is available, then we know the offset is dynamic. */
20078 else if (gnat_encodings == DWARF_GNAT_ENCODINGS_ALL)
20080 loc_descr = NULL;
20081 offset = 0;
20084 /* Data member location evaluation starts with the base address on the
20085 stack. Compute the field offset and add it to this base address. */
20086 else
20087 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
20090 if (!loc_descr)
20092 /* While DW_AT_data_bit_offset has been added already in DWARF4,
20093 e.g. GDB only added support to it in November 2016. For DWARF5
20094 we need newer debug info consumers anyway. We might change this
20095 to dwarf_version >= 4 once most consumers catched up. */
20096 if (dwarf_version >= 5
20097 && TREE_CODE (decl) == FIELD_DECL
20098 && DECL_BIT_FIELD_TYPE (decl)
20099 && (ctx->variant_part_offset == NULL_TREE
20100 || TREE_CODE (ctx->variant_part_offset) == INTEGER_CST))
20102 tree off = bit_position (decl);
20103 if (ctx->variant_part_offset)
20104 off = bit_from_pos (ctx->variant_part_offset, off);
20105 if (tree_fits_uhwi_p (off) && get_AT (die, DW_AT_bit_size))
20107 remove_AT (die, DW_AT_byte_size);
20108 remove_AT (die, DW_AT_bit_offset);
20109 add_AT_unsigned (die, DW_AT_data_bit_offset, tree_to_uhwi (off));
20110 return;
20113 if (dwarf_version > 2)
20115 /* Don't need to output a location expression, just the constant. */
20116 if (offset < 0)
20117 add_AT_int (die, DW_AT_data_member_location, offset);
20118 else
20119 add_AT_unsigned (die, DW_AT_data_member_location, offset);
20120 return;
20122 else
20124 enum dwarf_location_atom op;
20126 /* The DWARF2 standard says that we should assume that the structure
20127 address is already on the stack, so we can specify a structure
20128 field address by using DW_OP_plus_uconst. */
20129 op = DW_OP_plus_uconst;
20130 loc_descr = new_loc_descr (op, offset, 0);
20134 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
20137 /* Writes integer values to dw_vec_const array. */
20139 static void
20140 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
20142 while (size != 0)
20144 *dest++ = val & 0xff;
20145 val >>= 8;
20146 --size;
20150 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
20152 static HOST_WIDE_INT
20153 extract_int (const unsigned char *src, unsigned int size)
20155 HOST_WIDE_INT val = 0;
20157 src += size;
20158 while (size != 0)
20160 val <<= 8;
20161 val |= *--src & 0xff;
20162 --size;
20164 return val;
20167 /* Writes wide_int values to dw_vec_const array. */
20169 static void
20170 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
20172 int i;
20174 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
20176 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
20177 return;
20180 /* We'd have to extend this code to support odd sizes. */
20181 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
20183 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
20185 if (WORDS_BIG_ENDIAN)
20186 for (i = n - 1; i >= 0; i--)
20188 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
20189 dest += sizeof (HOST_WIDE_INT);
20191 else
20192 for (i = 0; i < n; i++)
20194 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
20195 dest += sizeof (HOST_WIDE_INT);
20199 /* Writes floating point values to dw_vec_const array. */
20201 static unsigned
20202 insert_float (const_rtx rtl, unsigned char *array)
20204 long val[4];
20205 int i;
20206 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
20208 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), mode);
20210 /* real_to_target puts 32-bit pieces in each long. Pack them. */
20211 if (GET_MODE_SIZE (mode) < 4)
20213 gcc_assert (GET_MODE_SIZE (mode) == 2);
20214 insert_int (val[0], 2, array);
20215 return 2;
20218 for (i = 0; i < GET_MODE_SIZE (mode) / 4; i++)
20220 insert_int (val[i], 4, array);
20221 array += 4;
20223 return 4;
20226 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
20227 does not have a "location" either in memory or in a register. These
20228 things can arise in GNU C when a constant is passed as an actual parameter
20229 to an inlined function. They can also arise in C++ where declared
20230 constants do not necessarily get memory "homes". */
20232 static bool
20233 add_const_value_attribute (dw_die_ref die, machine_mode mode, rtx rtl)
20235 scalar_mode int_mode;
20237 switch (GET_CODE (rtl))
20239 case CONST_INT:
20241 HOST_WIDE_INT val = INTVAL (rtl);
20243 if (val < 0)
20244 add_AT_int (die, DW_AT_const_value, val);
20245 else
20246 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
20248 return true;
20250 case CONST_WIDE_INT:
20251 if (is_int_mode (mode, &int_mode)
20252 && (GET_MODE_PRECISION (int_mode)
20253 & (HOST_BITS_PER_WIDE_INT - 1)) == 0)
20255 wide_int w = rtx_mode_t (rtl, int_mode);
20256 add_AT_wide (die, DW_AT_const_value, w);
20257 return true;
20259 return false;
20261 case CONST_DOUBLE:
20262 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
20263 floating-point constant. A CONST_DOUBLE is used whenever the
20264 constant requires more than one word in order to be adequately
20265 represented. */
20266 if (TARGET_SUPPORTS_WIDE_INT == 0
20267 && !SCALAR_FLOAT_MODE_P (GET_MODE (rtl)))
20268 add_AT_double (die, DW_AT_const_value,
20269 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
20270 else
20272 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
20273 unsigned int length = GET_MODE_SIZE (mode);
20274 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
20275 unsigned int elt_size = insert_float (rtl, array);
20277 add_AT_vec (die, DW_AT_const_value, length / elt_size, elt_size,
20278 array);
20280 return true;
20282 case CONST_VECTOR:
20284 unsigned int length;
20285 if (!CONST_VECTOR_NUNITS (rtl).is_constant (&length))
20286 return false;
20288 machine_mode mode = GET_MODE (rtl);
20289 /* The combination of a length and byte elt_size doesn't extend
20290 naturally to boolean vectors, where several elements are packed
20291 into the same byte. */
20292 if (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL)
20293 return false;
20295 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
20296 unsigned char *array
20297 = ggc_vec_alloc<unsigned char> (length * elt_size);
20298 unsigned int i;
20299 unsigned char *p;
20300 machine_mode imode = GET_MODE_INNER (mode);
20302 switch (GET_MODE_CLASS (mode))
20304 case MODE_VECTOR_INT:
20305 for (i = 0, p = array; i < length; i++, p += elt_size)
20307 rtx elt = CONST_VECTOR_ELT (rtl, i);
20308 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
20310 break;
20312 case MODE_VECTOR_FLOAT:
20313 for (i = 0, p = array; i < length; i++, p += elt_size)
20315 rtx elt = CONST_VECTOR_ELT (rtl, i);
20316 insert_float (elt, p);
20318 break;
20320 default:
20321 gcc_unreachable ();
20324 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
20326 return true;
20328 case CONST_STRING:
20329 if (dwarf_version >= 4 || !dwarf_strict)
20331 dw_loc_descr_ref loc_result;
20332 resolve_one_addr (&rtl);
20333 rtl_addr:
20334 loc_result = new_addr_loc_descr (rtl, dtprel_false);
20335 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
20336 add_AT_loc (die, DW_AT_location, loc_result);
20337 vec_safe_push (used_rtx_array, rtl);
20338 return true;
20340 return false;
20342 case CONST:
20343 if (CONSTANT_P (XEXP (rtl, 0)))
20344 return add_const_value_attribute (die, mode, XEXP (rtl, 0));
20345 /* FALLTHROUGH */
20346 case SYMBOL_REF:
20347 if (!const_ok_for_output (rtl))
20348 return false;
20349 /* FALLTHROUGH */
20350 case LABEL_REF:
20351 if (dwarf_version >= 4 || !dwarf_strict)
20352 goto rtl_addr;
20353 return false;
20355 case PLUS:
20356 /* In cases where an inlined instance of an inline function is passed
20357 the address of an `auto' variable (which is local to the caller) we
20358 can get a situation where the DECL_RTL of the artificial local
20359 variable (for the inlining) which acts as a stand-in for the
20360 corresponding formal parameter (of the inline function) will look
20361 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
20362 exactly a compile-time constant expression, but it isn't the address
20363 of the (artificial) local variable either. Rather, it represents the
20364 *value* which the artificial local variable always has during its
20365 lifetime. We currently have no way to represent such quasi-constant
20366 values in Dwarf, so for now we just punt and generate nothing. */
20367 return false;
20369 case HIGH:
20370 case CONST_FIXED:
20371 case MINUS:
20372 case SIGN_EXTEND:
20373 case ZERO_EXTEND:
20374 case CONST_POLY_INT:
20375 return false;
20377 case MEM:
20378 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
20379 && MEM_READONLY_P (rtl)
20380 && GET_MODE (rtl) == BLKmode)
20382 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
20383 return true;
20385 return false;
20387 default:
20388 /* No other kinds of rtx should be possible here. */
20389 gcc_unreachable ();
20393 /* Determine whether the evaluation of EXPR references any variables
20394 or functions which aren't otherwise used (and therefore may not be
20395 output). */
20396 static tree
20397 reference_to_unused (tree * tp, int * walk_subtrees,
20398 void * data ATTRIBUTE_UNUSED)
20400 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
20401 *walk_subtrees = 0;
20403 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
20404 && ! TREE_ASM_WRITTEN (*tp))
20405 return *tp;
20406 /* ??? The C++ FE emits debug information for using decls, so
20407 putting gcc_unreachable here falls over. See PR31899. For now
20408 be conservative. */
20409 else if (!symtab->global_info_ready && VAR_P (*tp))
20410 return *tp;
20411 else if (VAR_P (*tp))
20413 varpool_node *node = varpool_node::get (*tp);
20414 if (!node || !node->definition)
20415 return *tp;
20417 else if (TREE_CODE (*tp) == FUNCTION_DECL
20418 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
20420 /* The call graph machinery must have finished analyzing,
20421 optimizing and gimplifying the CU by now.
20422 So if *TP has no call graph node associated
20423 to it, it means *TP will not be emitted. */
20424 if (!symtab->global_info_ready || !cgraph_node::get (*tp))
20425 return *tp;
20427 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
20428 return *tp;
20430 return NULL_TREE;
20433 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
20434 for use in a later add_const_value_attribute call. */
20436 static rtx
20437 rtl_for_decl_init (tree init, tree type)
20439 rtx rtl = NULL_RTX;
20441 STRIP_NOPS (init);
20443 /* If a variable is initialized with a string constant without embedded
20444 zeros, build CONST_STRING. */
20445 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
20447 tree enttype = TREE_TYPE (type);
20448 tree domain = TYPE_DOMAIN (type);
20449 scalar_int_mode mode;
20451 if (is_int_mode (TYPE_MODE (enttype), &mode)
20452 && GET_MODE_SIZE (mode) == 1
20453 && domain
20454 && TYPE_MAX_VALUE (domain)
20455 && TREE_CODE (TYPE_MAX_VALUE (domain)) == INTEGER_CST
20456 && integer_zerop (TYPE_MIN_VALUE (domain))
20457 && compare_tree_int (TYPE_MAX_VALUE (domain),
20458 TREE_STRING_LENGTH (init) - 1) == 0
20459 && ((size_t) TREE_STRING_LENGTH (init)
20460 == strlen (TREE_STRING_POINTER (init)) + 1))
20462 rtl = gen_rtx_CONST_STRING (VOIDmode,
20463 ggc_strdup (TREE_STRING_POINTER (init)));
20464 rtl = gen_rtx_MEM (BLKmode, rtl);
20465 MEM_READONLY_P (rtl) = 1;
20468 /* Other aggregates, and complex values, could be represented using
20469 CONCAT: FIXME!
20470 If this changes, please adjust tree_add_const_value_attribute
20471 so that for early_dwarf it will for such initializers mangle referenced
20472 decls. */
20473 else if (AGGREGATE_TYPE_P (type)
20474 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
20475 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
20476 || TREE_CODE (type) == COMPLEX_TYPE)
20478 /* Vectors only work if their mode is supported by the target.
20479 FIXME: generic vectors ought to work too. */
20480 else if (TREE_CODE (type) == VECTOR_TYPE
20481 && !VECTOR_MODE_P (TYPE_MODE (type)))
20483 /* If the initializer is something that we know will expand into an
20484 immediate RTL constant, expand it now. We must be careful not to
20485 reference variables which won't be output. */
20486 else if (initializer_constant_valid_p (init, type)
20487 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
20489 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
20490 possible. */
20491 if (TREE_CODE (type) == VECTOR_TYPE)
20492 switch (TREE_CODE (init))
20494 case VECTOR_CST:
20495 break;
20496 case CONSTRUCTOR:
20497 if (TREE_CONSTANT (init))
20499 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
20500 bool constant_p = true;
20501 tree value;
20502 unsigned HOST_WIDE_INT ix;
20504 /* Even when ctor is constant, it might contain non-*_CST
20505 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
20506 belong into VECTOR_CST nodes. */
20507 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
20508 if (!CONSTANT_CLASS_P (value))
20510 constant_p = false;
20511 break;
20514 if (constant_p)
20516 init = build_vector_from_ctor (type, elts);
20517 break;
20520 /* FALLTHRU */
20522 default:
20523 return NULL;
20526 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
20528 /* If expand_expr returns a MEM, it wasn't immediate. */
20529 gcc_assert (!rtl || !MEM_P (rtl));
20532 return rtl;
20535 /* Generate RTL for the variable DECL to represent its location. */
20537 static rtx
20538 rtl_for_decl_location (tree decl)
20540 rtx rtl;
20542 /* Here we have to decide where we are going to say the parameter "lives"
20543 (as far as the debugger is concerned). We only have a couple of
20544 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
20546 DECL_RTL normally indicates where the parameter lives during most of the
20547 activation of the function. If optimization is enabled however, this
20548 could be either NULL or else a pseudo-reg. Both of those cases indicate
20549 that the parameter doesn't really live anywhere (as far as the code
20550 generation parts of GCC are concerned) during most of the function's
20551 activation. That will happen (for example) if the parameter is never
20552 referenced within the function.
20554 We could just generate a location descriptor here for all non-NULL
20555 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
20556 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
20557 where DECL_RTL is NULL or is a pseudo-reg.
20559 Note however that we can only get away with using DECL_INCOMING_RTL as
20560 a backup substitute for DECL_RTL in certain limited cases. In cases
20561 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
20562 we can be sure that the parameter was passed using the same type as it is
20563 declared to have within the function, and that its DECL_INCOMING_RTL
20564 points us to a place where a value of that type is passed.
20566 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
20567 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
20568 because in these cases DECL_INCOMING_RTL points us to a value of some
20569 type which is *different* from the type of the parameter itself. Thus,
20570 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
20571 such cases, the debugger would end up (for example) trying to fetch a
20572 `float' from a place which actually contains the first part of a
20573 `double'. That would lead to really incorrect and confusing
20574 output at debug-time.
20576 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
20577 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
20578 are a couple of exceptions however. On little-endian machines we can
20579 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
20580 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
20581 an integral type that is smaller than TREE_TYPE (decl). These cases arise
20582 when (on a little-endian machine) a non-prototyped function has a
20583 parameter declared to be of type `short' or `char'. In such cases,
20584 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
20585 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
20586 passed `int' value. If the debugger then uses that address to fetch
20587 a `short' or a `char' (on a little-endian machine) the result will be
20588 the correct data, so we allow for such exceptional cases below.
20590 Note that our goal here is to describe the place where the given formal
20591 parameter lives during most of the function's activation (i.e. between the
20592 end of the prologue and the start of the epilogue). We'll do that as best
20593 as we can. Note however that if the given formal parameter is modified
20594 sometime during the execution of the function, then a stack backtrace (at
20595 debug-time) will show the function as having been called with the *new*
20596 value rather than the value which was originally passed in. This happens
20597 rarely enough that it is not a major problem, but it *is* a problem, and
20598 I'd like to fix it.
20600 A future version of dwarf2out.cc may generate two additional attributes for
20601 any given DW_TAG_formal_parameter DIE which will describe the "passed
20602 type" and the "passed location" for the given formal parameter in addition
20603 to the attributes we now generate to indicate the "declared type" and the
20604 "active location" for each parameter. This additional set of attributes
20605 could be used by debuggers for stack backtraces. Separately, note that
20606 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
20607 This happens (for example) for inlined-instances of inline function formal
20608 parameters which are never referenced. This really shouldn't be
20609 happening. All PARM_DECL nodes should get valid non-NULL
20610 DECL_INCOMING_RTL values. FIXME. */
20612 /* Use DECL_RTL as the "location" unless we find something better. */
20613 rtl = DECL_RTL_IF_SET (decl);
20615 /* When generating abstract instances, ignore everything except
20616 constants, symbols living in memory, and symbols living in
20617 fixed registers. */
20618 if (! reload_completed)
20620 if (rtl
20621 && (CONSTANT_P (rtl)
20622 || (MEM_P (rtl)
20623 && CONSTANT_P (XEXP (rtl, 0)))
20624 || (REG_P (rtl)
20625 && VAR_P (decl)
20626 && TREE_STATIC (decl))))
20628 rtl = targetm.delegitimize_address (rtl);
20629 return rtl;
20631 rtl = NULL_RTX;
20633 else if (TREE_CODE (decl) == PARM_DECL)
20635 if (rtl == NULL_RTX
20636 || is_pseudo_reg (rtl)
20637 || (MEM_P (rtl)
20638 && is_pseudo_reg (XEXP (rtl, 0))
20639 && DECL_INCOMING_RTL (decl)
20640 && MEM_P (DECL_INCOMING_RTL (decl))
20641 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
20643 tree declared_type = TREE_TYPE (decl);
20644 tree passed_type = DECL_ARG_TYPE (decl);
20645 machine_mode dmode = TYPE_MODE (declared_type);
20646 machine_mode pmode = TYPE_MODE (passed_type);
20648 /* This decl represents a formal parameter which was optimized out.
20649 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
20650 all cases where (rtl == NULL_RTX) just below. */
20651 if (dmode == pmode)
20652 rtl = DECL_INCOMING_RTL (decl);
20653 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
20654 && SCALAR_INT_MODE_P (dmode)
20655 && known_le (GET_MODE_SIZE (dmode), GET_MODE_SIZE (pmode))
20656 && DECL_INCOMING_RTL (decl))
20658 rtx inc = DECL_INCOMING_RTL (decl);
20659 if (REG_P (inc))
20660 rtl = inc;
20661 else if (MEM_P (inc))
20663 if (BYTES_BIG_ENDIAN)
20664 rtl = adjust_address_nv (inc, dmode,
20665 GET_MODE_SIZE (pmode)
20666 - GET_MODE_SIZE (dmode));
20667 else
20668 rtl = inc;
20673 /* If the parm was passed in registers, but lives on the stack, then
20674 make a big endian correction if the mode of the type of the
20675 parameter is not the same as the mode of the rtl. */
20676 /* ??? This is the same series of checks that are made in dbxout.cc before
20677 we reach the big endian correction code there. It isn't clear if all
20678 of these checks are necessary here, but keeping them all is the safe
20679 thing to do. */
20680 else if (MEM_P (rtl)
20681 && XEXP (rtl, 0) != const0_rtx
20682 && ! CONSTANT_P (XEXP (rtl, 0))
20683 /* Not passed in memory. */
20684 && !MEM_P (DECL_INCOMING_RTL (decl))
20685 /* Not passed by invisible reference. */
20686 && (!REG_P (XEXP (rtl, 0))
20687 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
20688 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
20689 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
20690 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
20691 #endif
20693 /* Big endian correction check. */
20694 && BYTES_BIG_ENDIAN
20695 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
20696 && known_lt (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))),
20697 UNITS_PER_WORD))
20699 machine_mode addr_mode = get_address_mode (rtl);
20700 poly_int64 offset = (UNITS_PER_WORD
20701 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
20703 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
20704 plus_constant (addr_mode, XEXP (rtl, 0), offset));
20707 else if (VAR_P (decl)
20708 && rtl
20709 && MEM_P (rtl)
20710 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl)))
20712 machine_mode addr_mode = get_address_mode (rtl);
20713 poly_int64 offset = byte_lowpart_offset (TYPE_MODE (TREE_TYPE (decl)),
20714 GET_MODE (rtl));
20716 /* If a variable is declared "register" yet is smaller than
20717 a register, then if we store the variable to memory, it
20718 looks like we're storing a register-sized value, when in
20719 fact we are not. We need to adjust the offset of the
20720 storage location to reflect the actual value's bytes,
20721 else gdb will not be able to display it. */
20722 if (maybe_ne (offset, 0))
20723 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
20724 plus_constant (addr_mode, XEXP (rtl, 0), offset));
20727 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
20728 and will have been substituted directly into all expressions that use it.
20729 C does not have such a concept, but C++ and other languages do. */
20730 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
20731 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
20733 if (rtl)
20734 rtl = targetm.delegitimize_address (rtl);
20736 /* If we don't look past the constant pool, we risk emitting a
20737 reference to a constant pool entry that isn't referenced from
20738 code, and thus is not emitted. */
20739 if (rtl)
20740 rtl = avoid_constant_pool_reference (rtl);
20742 /* Try harder to get a rtl. If this symbol ends up not being emitted
20743 in the current CU, resolve_addr will remove the expression referencing
20744 it. */
20745 if (rtl == NULL_RTX
20746 && !(early_dwarf && (flag_generate_lto || flag_generate_offload))
20747 && VAR_P (decl)
20748 && !DECL_EXTERNAL (decl)
20749 && TREE_STATIC (decl)
20750 && DECL_NAME (decl)
20751 && !DECL_HARD_REGISTER (decl)
20752 && DECL_MODE (decl) != VOIDmode)
20754 rtl = make_decl_rtl_for_debug (decl);
20755 if (!MEM_P (rtl)
20756 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
20757 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
20758 rtl = NULL_RTX;
20761 return rtl;
20764 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
20765 returned. If so, the decl for the COMMON block is returned, and the
20766 value is the offset into the common block for the symbol. */
20768 static tree
20769 fortran_common (tree decl, HOST_WIDE_INT *value)
20771 tree val_expr, cvar;
20772 machine_mode mode;
20773 poly_int64 bitsize, bitpos;
20774 tree offset;
20775 HOST_WIDE_INT cbitpos;
20776 int unsignedp, reversep, volatilep = 0;
20778 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
20779 it does not have a value (the offset into the common area), or if it
20780 is thread local (as opposed to global) then it isn't common, and shouldn't
20781 be handled as such. */
20782 if (!VAR_P (decl)
20783 || !TREE_STATIC (decl)
20784 || !DECL_HAS_VALUE_EXPR_P (decl)
20785 || !is_fortran ())
20786 return NULL_TREE;
20788 val_expr = DECL_VALUE_EXPR (decl);
20789 if (TREE_CODE (val_expr) != COMPONENT_REF)
20790 return NULL_TREE;
20792 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
20793 &unsignedp, &reversep, &volatilep);
20795 if (cvar == NULL_TREE
20796 || !VAR_P (cvar)
20797 || DECL_ARTIFICIAL (cvar)
20798 || !TREE_PUBLIC (cvar)
20799 /* We don't expect to have to cope with variable offsets,
20800 since at present all static data must have a constant size. */
20801 || !bitpos.is_constant (&cbitpos))
20802 return NULL_TREE;
20804 *value = 0;
20805 if (offset != NULL)
20807 if (!tree_fits_shwi_p (offset))
20808 return NULL_TREE;
20809 *value = tree_to_shwi (offset);
20811 if (cbitpos != 0)
20812 *value += cbitpos / BITS_PER_UNIT;
20814 return cvar;
20817 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
20818 data attribute for a variable or a parameter. We generate the
20819 DW_AT_const_value attribute only in those cases where the given variable
20820 or parameter does not have a true "location" either in memory or in a
20821 register. This can happen (for example) when a constant is passed as an
20822 actual argument in a call to an inline function. (It's possible that
20823 these things can crop up in other ways also.) Note that one type of
20824 constant value which can be passed into an inlined function is a constant
20825 pointer. This can happen for example if an actual argument in an inlined
20826 function call evaluates to a compile-time constant address.
20828 CACHE_P is true if it is worth caching the location list for DECL,
20829 so that future calls can reuse it rather than regenerate it from scratch.
20830 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
20831 since we will need to refer to them each time the function is inlined. */
20833 static bool
20834 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
20836 rtx rtl;
20837 dw_loc_list_ref list;
20838 var_loc_list *loc_list;
20839 cached_dw_loc_list *cache;
20841 if (early_dwarf)
20842 return false;
20844 if (TREE_CODE (decl) == ERROR_MARK)
20845 return false;
20847 if (get_AT (die, DW_AT_location)
20848 || get_AT (die, DW_AT_const_value))
20849 return true;
20851 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
20852 || TREE_CODE (decl) == RESULT_DECL);
20854 /* Try to get some constant RTL for this decl, and use that as the value of
20855 the location. */
20857 rtl = rtl_for_decl_location (decl);
20858 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
20859 && add_const_value_attribute (die, DECL_MODE (decl), rtl))
20860 return true;
20862 /* See if we have single element location list that is equivalent to
20863 a constant value. That way we are better to use add_const_value_attribute
20864 rather than expanding constant value equivalent. */
20865 loc_list = lookup_decl_loc (decl);
20866 if (loc_list
20867 && loc_list->first
20868 && loc_list->first->next == NULL
20869 && NOTE_P (loc_list->first->loc)
20870 && NOTE_VAR_LOCATION (loc_list->first->loc)
20871 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
20873 struct var_loc_node *node;
20875 node = loc_list->first;
20876 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
20877 if (GET_CODE (rtl) == EXPR_LIST)
20878 rtl = XEXP (rtl, 0);
20879 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
20880 && add_const_value_attribute (die, DECL_MODE (decl), rtl))
20881 return true;
20883 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
20884 list several times. See if we've already cached the contents. */
20885 list = NULL;
20886 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
20887 cache_p = false;
20888 if (cache_p)
20890 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
20891 if (cache)
20892 list = cache->loc_list;
20894 if (list == NULL)
20896 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
20897 NULL);
20898 /* It is usually worth caching this result if the decl is from
20899 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
20900 if (cache_p && list && list->dw_loc_next)
20902 cached_dw_loc_list **slot
20903 = cached_dw_loc_list_table->find_slot_with_hash (decl,
20904 DECL_UID (decl),
20905 INSERT);
20906 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
20907 cache->decl_id = DECL_UID (decl);
20908 cache->loc_list = list;
20909 *slot = cache;
20912 if (list)
20914 add_AT_location_description (die, DW_AT_location, list);
20915 return true;
20917 /* None of that worked, so it must not really have a location;
20918 try adding a constant value attribute from the DECL_INITIAL. */
20919 return tree_add_const_value_attribute_for_decl (die, decl);
20922 /* Mangle referenced decls. */
20923 static tree
20924 mangle_referenced_decls (tree *tp, int *walk_subtrees, void *)
20926 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
20927 *walk_subtrees = 0;
20929 if (VAR_OR_FUNCTION_DECL_P (*tp))
20930 assign_assembler_name_if_needed (*tp);
20932 return NULL_TREE;
20935 /* Attach a DW_AT_const_value attribute to DIE. The value of the
20936 attribute is the const value T. */
20938 static bool
20939 tree_add_const_value_attribute (dw_die_ref die, tree t)
20941 tree init;
20942 tree type = TREE_TYPE (t);
20944 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
20945 return false;
20947 init = t;
20948 gcc_assert (!DECL_P (init));
20950 if (TREE_CODE (init) == INTEGER_CST)
20952 if (tree_fits_uhwi_p (init))
20954 add_AT_unsigned (die, DW_AT_const_value, tree_to_uhwi (init));
20955 return true;
20957 if (tree_fits_shwi_p (init))
20959 add_AT_int (die, DW_AT_const_value, tree_to_shwi (init));
20960 return true;
20963 if (!early_dwarf)
20965 rtx rtl = rtl_for_decl_init (init, type);
20966 if (rtl)
20967 return add_const_value_attribute (die, TYPE_MODE (type), rtl);
20969 else
20971 /* For early_dwarf force mangling of all referenced symbols. */
20972 tree initializer = init;
20973 STRIP_NOPS (initializer);
20974 /* rtl_for_decl_init punts on other aggregates, and complex values. */
20975 if (AGGREGATE_TYPE_P (type)
20976 || (TREE_CODE (initializer) == VIEW_CONVERT_EXPR
20977 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (initializer, 0))))
20978 || TREE_CODE (type) == COMPLEX_TYPE)
20980 else if (initializer_constant_valid_p (initializer, type))
20981 walk_tree (&initializer, mangle_referenced_decls, NULL, NULL);
20983 /* If the host and target are sane, try harder. */
20984 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
20985 && initializer_constant_valid_p (init, type))
20987 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
20988 if (size > 0 && (int) size == size)
20990 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
20992 if (native_encode_initializer (init, array, size) == size)
20994 add_AT_vec (die, DW_AT_const_value, size, 1, array);
20995 return true;
20997 ggc_free (array);
21000 return false;
21003 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
21004 attribute is the const value of T, where T is an integral constant
21005 variable with static storage duration
21006 (so it can't be a PARM_DECL or a RESULT_DECL). */
21008 static bool
21009 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
21012 if (!decl
21013 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
21014 || (VAR_P (decl) && !TREE_STATIC (decl)))
21015 return false;
21017 if (TREE_READONLY (decl)
21018 && ! TREE_THIS_VOLATILE (decl)
21019 && DECL_INITIAL (decl))
21020 /* OK */;
21021 else
21022 return false;
21024 /* Don't add DW_AT_const_value if abstract origin already has one. */
21025 if (get_AT (var_die, DW_AT_const_value))
21026 return false;
21028 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
21031 /* Convert the CFI instructions for the current function into a
21032 location list. This is used for DW_AT_frame_base when we targeting
21033 a dwarf2 consumer that does not support the dwarf3
21034 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
21035 expressions. */
21037 static dw_loc_list_ref
21038 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
21040 int ix;
21041 dw_fde_ref fde;
21042 dw_loc_list_ref list, *list_tail;
21043 dw_cfi_ref cfi;
21044 dw_cfa_location last_cfa, next_cfa;
21045 const char *start_label, *last_label, *section;
21046 dw_cfa_location remember;
21048 fde = cfun->fde;
21049 gcc_assert (fde != NULL);
21051 section = secname_for_decl (current_function_decl);
21052 list_tail = &list;
21053 list = NULL;
21055 memset (&next_cfa, 0, sizeof (next_cfa));
21056 next_cfa.reg.set_by_dwreg (INVALID_REGNUM);
21057 remember = next_cfa;
21059 start_label = fde->dw_fde_begin;
21061 /* ??? Bald assumption that the CIE opcode list does not contain
21062 advance opcodes. */
21063 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
21064 lookup_cfa_1 (cfi, &next_cfa, &remember);
21066 last_cfa = next_cfa;
21067 last_label = start_label;
21069 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
21071 /* If the first partition contained no CFI adjustments, the
21072 CIE opcodes apply to the whole first partition. */
21073 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
21074 fde->dw_fde_begin, 0, fde->dw_fde_end, 0, section);
21075 list_tail =&(*list_tail)->dw_loc_next;
21076 start_label = last_label = fde->dw_fde_second_begin;
21079 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
21081 switch (cfi->dw_cfi_opc)
21083 case DW_CFA_set_loc:
21084 case DW_CFA_advance_loc1:
21085 case DW_CFA_advance_loc2:
21086 case DW_CFA_advance_loc4:
21087 if (!cfa_equal_p (&last_cfa, &next_cfa))
21089 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
21090 start_label, 0, last_label, 0, section);
21092 list_tail = &(*list_tail)->dw_loc_next;
21093 last_cfa = next_cfa;
21094 start_label = last_label;
21096 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
21097 break;
21099 case DW_CFA_advance_loc:
21100 /* The encoding is complex enough that we should never emit this. */
21101 gcc_unreachable ();
21103 default:
21104 lookup_cfa_1 (cfi, &next_cfa, &remember);
21105 break;
21107 if (ix + 1 == fde->dw_fde_switch_cfi_index)
21109 if (!cfa_equal_p (&last_cfa, &next_cfa))
21111 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
21112 start_label, 0, last_label, 0, section);
21114 list_tail = &(*list_tail)->dw_loc_next;
21115 last_cfa = next_cfa;
21116 start_label = last_label;
21118 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
21119 start_label, 0, fde->dw_fde_end, 0, section);
21120 list_tail = &(*list_tail)->dw_loc_next;
21121 start_label = last_label = fde->dw_fde_second_begin;
21125 if (!cfa_equal_p (&last_cfa, &next_cfa))
21127 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
21128 start_label, 0, last_label, 0, section);
21129 list_tail = &(*list_tail)->dw_loc_next;
21130 start_label = last_label;
21133 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
21134 start_label, 0,
21135 fde->dw_fde_second_begin
21136 ? fde->dw_fde_second_end : fde->dw_fde_end, 0,
21137 section);
21139 maybe_gen_llsym (list);
21141 return list;
21144 /* Compute a displacement from the "steady-state frame pointer" to the
21145 frame base (often the same as the CFA), and store it in
21146 frame_pointer_fb_offset. OFFSET is added to the displacement
21147 before the latter is negated. */
21149 static void
21150 compute_frame_pointer_to_fb_displacement (poly_int64 offset)
21152 rtx reg, elim;
21154 #ifdef FRAME_POINTER_CFA_OFFSET
21155 reg = frame_pointer_rtx;
21156 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
21157 #else
21158 reg = arg_pointer_rtx;
21159 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
21160 #endif
21162 elim = (ira_use_lra_p
21163 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
21164 : eliminate_regs (reg, VOIDmode, NULL_RTX));
21165 elim = strip_offset_and_add (elim, &offset);
21167 frame_pointer_fb_offset = -offset;
21169 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
21170 in which to eliminate. This is because it's stack pointer isn't
21171 directly accessible as a register within the ISA. To work around
21172 this, assume that while we cannot provide a proper value for
21173 frame_pointer_fb_offset, we won't need one either. We can use
21174 hard frame pointer in debug info even if frame pointer isn't used
21175 since hard frame pointer in debug info is encoded with DW_OP_fbreg
21176 which uses the DW_AT_frame_base attribute, not hard frame pointer
21177 directly. */
21178 frame_pointer_fb_offset_valid
21179 = (elim == hard_frame_pointer_rtx || elim == stack_pointer_rtx);
21182 /* Generate a DW_AT_name attribute given some string value to be included as
21183 the value of the attribute. */
21185 void
21186 add_name_attribute (dw_die_ref die, const char *name_string)
21188 if (name_string != NULL && *name_string != 0)
21190 if (demangle_name_func)
21191 name_string = (*demangle_name_func) (name_string);
21193 add_AT_string (die, DW_AT_name, name_string);
21197 /* Generate a DW_AT_name attribute given some string value representing a
21198 file or filepath to be included as value of the attribute. */
21199 static void
21200 add_filename_attribute (dw_die_ref die, const char *name_string)
21202 if (name_string != NULL && *name_string != 0)
21203 add_filepath_AT_string (die, DW_AT_name, name_string);
21206 /* Generate a DW_AT_description attribute given some string value to be included
21207 as the value of the attribute. */
21209 static void
21210 add_desc_attribute (dw_die_ref die, const char *name_string)
21212 if (!flag_describe_dies || (dwarf_version < 3 && dwarf_strict))
21213 return;
21215 if (name_string == NULL || *name_string == 0)
21216 return;
21218 if (demangle_name_func)
21219 name_string = (*demangle_name_func) (name_string);
21221 add_AT_string (die, DW_AT_description, name_string);
21224 /* Generate a DW_AT_description attribute given some decl to be included
21225 as the value of the attribute. */
21227 static void
21228 add_desc_attribute (dw_die_ref die, tree decl)
21230 tree decl_name;
21232 if (!flag_describe_dies || (dwarf_version < 3 && dwarf_strict))
21233 return;
21235 if (decl == NULL_TREE || !DECL_P (decl))
21236 return;
21237 decl_name = DECL_NAME (decl);
21239 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
21241 const char *name = dwarf2_name (decl, 0);
21242 add_desc_attribute (die, name ? name : IDENTIFIER_POINTER (decl_name));
21244 else
21246 char *desc = print_generic_expr_to_str (decl);
21247 add_desc_attribute (die, desc);
21248 free (desc);
21252 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
21253 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
21254 of TYPE accordingly.
21256 ??? This is a temporary measure until after we're able to generate
21257 regular DWARF for the complex Ada type system. */
21259 static void
21260 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
21261 dw_die_ref context_die)
21263 tree dtype;
21264 dw_die_ref dtype_die;
21266 if (!lang_hooks.types.descriptive_type)
21267 return;
21269 dtype = lang_hooks.types.descriptive_type (type);
21270 if (!dtype)
21271 return;
21273 dtype_die = lookup_type_die (dtype);
21274 if (!dtype_die)
21276 gen_type_die (dtype, context_die);
21277 dtype_die = lookup_type_die (dtype);
21278 gcc_assert (dtype_die);
21281 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
21284 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
21286 static const char *
21287 comp_dir_string (void)
21289 const char *wd;
21290 char *wd_plus_sep = NULL;
21291 static const char *cached_wd = NULL;
21293 if (cached_wd != NULL)
21294 return cached_wd;
21296 wd = get_src_pwd ();
21297 if (wd == NULL)
21298 return NULL;
21300 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
21302 size_t wdlen = strlen (wd);
21303 wd_plus_sep = XNEWVEC (char, wdlen + 2);
21304 strcpy (wd_plus_sep, wd);
21305 wd_plus_sep [wdlen] = DIR_SEPARATOR;
21306 wd_plus_sep [wdlen + 1] = 0;
21307 wd = wd_plus_sep;
21310 cached_wd = remap_debug_filename (wd);
21312 /* remap_debug_filename can just pass through wd or return a new gc string.
21313 These two types can't be both stored in a GTY(())-tagged string, but since
21314 the cached value lives forever just copy it if needed. */
21315 if (cached_wd != wd)
21317 cached_wd = xstrdup (cached_wd);
21318 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR && wd_plus_sep != NULL)
21319 free (wd_plus_sep);
21322 return cached_wd;
21325 /* Generate a DW_AT_comp_dir attribute for DIE. */
21327 static void
21328 add_comp_dir_attribute (dw_die_ref die)
21330 const char * wd = comp_dir_string ();
21331 if (wd != NULL)
21332 add_filepath_AT_string (die, DW_AT_comp_dir, wd);
21335 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
21336 pointer computation, ...), output a representation for that bound according
21337 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
21338 loc_list_from_tree for the meaning of CONTEXT. */
21340 static void
21341 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
21342 int forms, struct loc_descr_context *context)
21344 dw_die_ref context_die, decl_die = NULL;
21345 dw_loc_list_ref list;
21346 bool strip_conversions = true;
21347 bool placeholder_seen = false;
21349 while (strip_conversions)
21350 switch (TREE_CODE (value))
21352 case ERROR_MARK:
21353 case SAVE_EXPR:
21354 return;
21356 CASE_CONVERT:
21357 case VIEW_CONVERT_EXPR:
21358 value = TREE_OPERAND (value, 0);
21359 break;
21361 default:
21362 strip_conversions = false;
21363 break;
21366 /* If possible and permitted, output the attribute as a constant. */
21367 if ((forms & dw_scalar_form_constant) != 0
21368 && TREE_CODE (value) == INTEGER_CST)
21370 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
21372 /* If HOST_WIDE_INT is big enough then represent the bound as
21373 a constant value. We need to choose a form based on
21374 whether the type is signed or unsigned. We cannot just
21375 call add_AT_unsigned if the value itself is positive
21376 (add_AT_unsigned might add the unsigned value encoded as
21377 DW_FORM_data[1248]). Some DWARF consumers will lookup the
21378 bounds type and then sign extend any unsigned values found
21379 for signed types. This is needed only for
21380 DW_AT_{lower,upper}_bound, since for most other attributes,
21381 consumers will treat DW_FORM_data[1248] as unsigned values,
21382 regardless of the underlying type. */
21383 if (prec <= HOST_BITS_PER_WIDE_INT
21384 || tree_fits_uhwi_p (value))
21386 if (TYPE_UNSIGNED (TREE_TYPE (value)))
21387 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
21388 else
21389 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
21391 else if (dwarf_version >= 5
21392 && TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (value))) == 128)
21393 /* Otherwise represent the bound as an unsigned value with
21394 the precision of its type. The precision and signedness
21395 of the type will be necessary to re-interpret it
21396 unambiguously. */
21397 add_AT_wide (die, attr, wi::to_wide (value));
21398 else
21400 rtx v = immed_wide_int_const (wi::to_wide (value),
21401 TYPE_MODE (TREE_TYPE (value)));
21402 dw_loc_descr_ref loc
21403 = loc_descriptor (v, TYPE_MODE (TREE_TYPE (value)),
21404 VAR_INIT_STATUS_INITIALIZED);
21405 if (loc)
21406 add_AT_loc (die, attr, loc);
21408 return;
21411 /* Otherwise, if it's possible and permitted too, output a reference to
21412 another DIE. */
21413 if ((forms & dw_scalar_form_reference) != 0)
21415 tree decl = NULL_TREE;
21417 /* Some type attributes reference an outer type. For instance, the upper
21418 bound of an array may reference an embedding record (this happens in
21419 Ada). */
21420 if (TREE_CODE (value) == COMPONENT_REF
21421 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
21422 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
21423 decl = TREE_OPERAND (value, 1);
21425 else if (VAR_P (value)
21426 || TREE_CODE (value) == PARM_DECL
21427 || TREE_CODE (value) == RESULT_DECL)
21428 decl = value;
21430 if (decl != NULL_TREE)
21432 decl_die = lookup_decl_die (decl);
21434 /* ??? Can this happen, or should the variable have been bound
21435 first? Probably it can, since I imagine that we try to create
21436 the types of parameters in the order in which they exist in
21437 the list, and won't have created a forward reference to a
21438 later parameter. */
21439 if (decl_die != NULL)
21441 if (get_AT (decl_die, DW_AT_location)
21442 || get_AT (decl_die, DW_AT_data_member_location)
21443 || get_AT (decl_die, DW_AT_data_bit_offset)
21444 || get_AT (decl_die, DW_AT_const_value))
21446 add_AT_die_ref (die, attr, decl_die);
21447 return;
21453 /* Last chance: try to create a stack operation procedure to evaluate the
21454 value. Do nothing if even that is not possible or permitted. */
21455 if ((forms & dw_scalar_form_exprloc) == 0)
21456 return;
21458 list = loc_list_from_tree (value, 2, context);
21459 if (context && context->placeholder_arg)
21461 placeholder_seen = context->placeholder_seen;
21462 context->placeholder_seen = false;
21464 if (list == NULL || single_element_loc_list_p (list))
21466 /* If this attribute is not a reference nor constant, it is
21467 a DWARF expression rather than location description. For that
21468 loc_list_from_tree (value, 0, &context) is needed. */
21469 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
21470 if (list2 && single_element_loc_list_p (list2))
21472 if (placeholder_seen)
21474 struct dwarf_procedure_info dpi;
21475 dpi.fndecl = NULL_TREE;
21476 dpi.args_count = 1;
21477 if (!resolve_args_picking (list2->expr, 1, &dpi))
21478 return;
21480 add_AT_loc (die, attr, list2->expr);
21481 return;
21485 /* If that failed to give a single element location list, fall back to
21486 outputting this as a reference... still if permitted. */
21487 if (list == NULL
21488 || (forms & dw_scalar_form_reference) == 0
21489 || placeholder_seen)
21490 return;
21492 if (!decl_die)
21494 if (current_function_decl == 0)
21495 context_die = comp_unit_die ();
21496 else
21497 context_die = lookup_decl_die (current_function_decl);
21499 decl_die = new_die (DW_TAG_variable, context_die, value);
21500 add_AT_flag (decl_die, DW_AT_artificial, 1);
21501 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
21502 context_die);
21505 add_AT_location_description (decl_die, DW_AT_location, list);
21506 add_AT_die_ref (die, attr, decl_die);
21509 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
21510 default. */
21512 static int
21513 lower_bound_default (void)
21515 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
21517 case DW_LANG_C:
21518 case DW_LANG_C89:
21519 case DW_LANG_C99:
21520 case DW_LANG_C11:
21521 case DW_LANG_C_plus_plus:
21522 case DW_LANG_C_plus_plus_11:
21523 case DW_LANG_C_plus_plus_14:
21524 case DW_LANG_ObjC:
21525 case DW_LANG_ObjC_plus_plus:
21526 return 0;
21527 case DW_LANG_Fortran77:
21528 case DW_LANG_Fortran90:
21529 case DW_LANG_Fortran95:
21530 case DW_LANG_Fortran03:
21531 case DW_LANG_Fortran08:
21532 return 1;
21533 case DW_LANG_UPC:
21534 case DW_LANG_D:
21535 case DW_LANG_Python:
21536 return dwarf_version >= 4 ? 0 : -1;
21537 case DW_LANG_Ada95:
21538 case DW_LANG_Ada83:
21539 case DW_LANG_Cobol74:
21540 case DW_LANG_Cobol85:
21541 case DW_LANG_Modula2:
21542 case DW_LANG_PLI:
21543 return dwarf_version >= 4 ? 1 : -1;
21544 default:
21545 return -1;
21549 /* Given a tree node describing an array bound (either lower or upper) output
21550 a representation for that bound. */
21552 static void
21553 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
21554 tree bound, struct loc_descr_context *context)
21556 int dflt;
21558 while (1)
21559 switch (TREE_CODE (bound))
21561 /* Strip all conversions. */
21562 CASE_CONVERT:
21563 case VIEW_CONVERT_EXPR:
21564 bound = TREE_OPERAND (bound, 0);
21565 break;
21567 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
21568 are even omitted when they are the default. */
21569 case INTEGER_CST:
21570 /* If the value for this bound is the default one, we can even omit the
21571 attribute. */
21572 if (bound_attr == DW_AT_lower_bound
21573 && tree_fits_shwi_p (bound)
21574 && (dflt = lower_bound_default ()) != -1
21575 && tree_to_shwi (bound) == dflt)
21576 return;
21578 /* FALLTHRU */
21580 default:
21581 /* Let GNAT encodings do the magic for self-referential bounds. */
21582 if (is_ada ()
21583 && gnat_encodings == DWARF_GNAT_ENCODINGS_ALL
21584 && contains_placeholder_p (bound))
21585 return;
21587 add_scalar_info (subrange_die, bound_attr, bound,
21588 dw_scalar_form_constant
21589 | dw_scalar_form_exprloc
21590 | dw_scalar_form_reference,
21591 context);
21592 return;
21596 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
21597 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
21599 This function reuses previously set type and bound information if
21600 available. */
21602 static void
21603 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
21605 dw_die_ref child = type_die->die_child;
21606 struct array_descr_info info;
21607 int dimension_number;
21609 if (lang_hooks.types.get_array_descr_info)
21611 memset (&info, 0, sizeof (info));
21612 if (lang_hooks.types.get_array_descr_info (type, &info))
21613 /* Fortran sometimes emits array types with no dimension. */
21614 gcc_assert (info.ndimensions >= 0
21615 && info.ndimensions
21616 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN);
21618 else
21619 info.ndimensions = 0;
21621 for (dimension_number = 0;
21622 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
21623 type = TREE_TYPE (type), dimension_number++)
21625 tree domain = TYPE_DOMAIN (type);
21627 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
21628 break;
21630 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
21631 and (in GNU C only) variable bounds. Handle all three forms
21632 here. */
21634 /* Find and reuse a previously generated DW_TAG_subrange_type if
21635 available.
21637 For multi-dimensional arrays, as we iterate through the
21638 various dimensions in the enclosing for loop above, we also
21639 iterate through the DIE children and pick at each
21640 DW_TAG_subrange_type previously generated (if available).
21641 Each child DW_TAG_subrange_type DIE describes the range of
21642 the current dimension. At this point we should have as many
21643 DW_TAG_subrange_type's as we have dimensions in the
21644 array. */
21645 dw_die_ref subrange_die = NULL;
21646 if (child)
21647 while (1)
21649 child = child->die_sib;
21650 if (child->die_tag == DW_TAG_subrange_type)
21651 subrange_die = child;
21652 if (child == type_die->die_child)
21654 /* If we wrapped around, stop looking next time. */
21655 child = NULL;
21656 break;
21658 if (child->die_tag == DW_TAG_subrange_type)
21659 break;
21661 if (!subrange_die)
21662 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
21664 if (domain)
21666 /* We have an array type with specified bounds. */
21667 tree lower = TYPE_MIN_VALUE (domain);
21668 tree upper = TYPE_MAX_VALUE (domain);
21669 tree index_type = TREE_TYPE (domain);
21671 if (dimension_number <= info.ndimensions - 1)
21673 lower = info.dimen[dimension_number].lower_bound;
21674 upper = info.dimen[dimension_number].upper_bound;
21675 index_type = info.dimen[dimension_number].bounds_type;
21678 /* Define the index type. */
21679 if (index_type && !get_AT (subrange_die, DW_AT_type))
21680 add_type_attribute (subrange_die, index_type, TYPE_UNQUALIFIED,
21681 false, type_die);
21683 /* ??? If upper is NULL, the array has unspecified length,
21684 but it does have a lower bound. This happens with Fortran
21685 dimension arr(N:*)
21686 Since the debugger is definitely going to need to know N
21687 to produce useful results, go ahead and output the lower
21688 bound solo, and hope the debugger can cope. */
21690 if (lower && !get_AT (subrange_die, DW_AT_lower_bound))
21691 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
21693 if (!get_AT (subrange_die, DW_AT_upper_bound)
21694 && !get_AT (subrange_die, DW_AT_count))
21696 if (upper)
21697 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
21698 else if ((is_c () || is_cxx ()) && COMPLETE_TYPE_P (type))
21699 /* Zero-length array. */
21700 add_bound_info (subrange_die, DW_AT_count,
21701 build_int_cst (TREE_TYPE (lower), 0), NULL);
21705 /* Otherwise we have an array type with an unspecified length. The
21706 DWARF-2 spec does not say how to handle this; let's just leave out the
21707 bounds. */
21711 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
21713 static void
21714 add_byte_size_attribute (dw_die_ref die, tree tree_node)
21716 dw_die_ref decl_die;
21717 HOST_WIDE_INT size;
21719 switch (TREE_CODE (tree_node))
21721 case ERROR_MARK:
21722 size = 0;
21723 break;
21724 case ENUMERAL_TYPE:
21725 case RECORD_TYPE:
21726 case UNION_TYPE:
21727 case QUAL_UNION_TYPE:
21728 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
21729 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
21731 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
21732 return;
21734 size = int_size_in_bytes (tree_node);
21735 break;
21736 case FIELD_DECL:
21737 /* For a data member of a struct or union, the DW_AT_byte_size is
21738 generally given as the number of bytes normally allocated for an
21739 object of the *declared* type of the member itself. This is true
21740 even for bit-fields. */
21741 size = int_size_in_bytes (field_type (tree_node));
21742 break;
21743 default:
21744 gcc_unreachable ();
21747 /* Note that `size' might be -1 when we get to this point. If it is, that
21748 indicates that the byte size of the entity in question is variable. */
21749 if (size >= 0)
21750 add_AT_unsigned (die, DW_AT_byte_size, size);
21752 /* Support for dynamically-sized objects was introduced in DWARF3. */
21753 else if (TYPE_P (tree_node)
21754 && (dwarf_version >= 3 || !dwarf_strict)
21755 && gnat_encodings != DWARF_GNAT_ENCODINGS_ALL)
21757 struct loc_descr_context ctx = {
21758 const_cast<tree> (tree_node), /* context_type */
21759 NULL_TREE, /* base_decl */
21760 NULL, /* dpi */
21761 false, /* placeholder_arg */
21762 false, /* placeholder_seen */
21763 false /* strict_signedness */
21766 tree tree_size = TYPE_SIZE_UNIT (TYPE_MAIN_VARIANT (tree_node));
21767 add_scalar_info (die, DW_AT_byte_size, tree_size,
21768 dw_scalar_form_constant
21769 | dw_scalar_form_exprloc
21770 | dw_scalar_form_reference,
21771 &ctx);
21775 /* Add a DW_AT_alignment attribute to DIE with TREE_NODE's non-default
21776 alignment. */
21778 static void
21779 add_alignment_attribute (dw_die_ref die, tree tree_node)
21781 if (dwarf_version < 5 && dwarf_strict)
21782 return;
21784 unsigned align;
21786 if (DECL_P (tree_node))
21788 if (!DECL_USER_ALIGN (tree_node))
21789 return;
21791 align = DECL_ALIGN_UNIT (tree_node);
21793 else if (TYPE_P (tree_node))
21795 if (!TYPE_USER_ALIGN (tree_node))
21796 return;
21798 align = TYPE_ALIGN_UNIT (tree_node);
21800 else
21801 gcc_unreachable ();
21803 add_AT_unsigned (die, DW_AT_alignment, align);
21806 /* For a FIELD_DECL node which represents a bit-field, output an attribute
21807 which specifies the distance in bits from the highest order bit of the
21808 "containing object" for the bit-field to the highest order bit of the
21809 bit-field itself.
21811 For any given bit-field, the "containing object" is a hypothetical object
21812 (of some integral or enum type) within which the given bit-field lives. The
21813 type of this hypothetical "containing object" is always the same as the
21814 declared type of the individual bit-field itself. The determination of the
21815 exact location of the "containing object" for a bit-field is rather
21816 complicated. It's handled by the `field_byte_offset' function (above).
21818 Note that it is the size (in bytes) of the hypothetical "containing object"
21819 which will be given in the DW_AT_byte_size attribute for this bit-field.
21820 (See `byte_size_attribute' above). */
21822 static inline void
21823 add_bit_offset_attribute (dw_die_ref die, tree decl)
21825 HOST_WIDE_INT object_offset_in_bytes;
21826 tree original_type = DECL_BIT_FIELD_TYPE (decl);
21827 HOST_WIDE_INT bitpos_int;
21828 HOST_WIDE_INT highest_order_object_bit_offset;
21829 HOST_WIDE_INT highest_order_field_bit_offset;
21830 HOST_WIDE_INT bit_offset;
21832 /* The containing object is within the DECL_CONTEXT. */
21833 struct vlr_context ctx = { DECL_CONTEXT (decl), NULL_TREE };
21835 field_byte_offset (decl, &ctx, &object_offset_in_bytes);
21837 /* Must be a field and a bit field. */
21838 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
21840 /* We can't yet handle bit-fields whose offsets are variable, so if we
21841 encounter such things, just return without generating any attribute
21842 whatsoever. Likewise for variable or too large size. */
21843 if (! tree_fits_shwi_p (bit_position (decl))
21844 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
21845 return;
21847 bitpos_int = int_bit_position (decl);
21849 /* Note that the bit offset is always the distance (in bits) from the
21850 highest-order bit of the "containing object" to the highest-order bit of
21851 the bit-field itself. Since the "high-order end" of any object or field
21852 is different on big-endian and little-endian machines, the computation
21853 below must take account of these differences. */
21854 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
21855 highest_order_field_bit_offset = bitpos_int;
21857 if (! BYTES_BIG_ENDIAN)
21859 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
21860 highest_order_object_bit_offset +=
21861 simple_type_size_in_bits (original_type);
21864 bit_offset
21865 = (! BYTES_BIG_ENDIAN
21866 ? highest_order_object_bit_offset - highest_order_field_bit_offset
21867 : highest_order_field_bit_offset - highest_order_object_bit_offset);
21869 if (bit_offset < 0)
21870 add_AT_int (die, DW_AT_bit_offset, bit_offset);
21871 else
21872 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
21875 /* For a FIELD_DECL node which represents a bit field, output an attribute
21876 which specifies the length in bits of the given field. */
21878 static inline void
21879 add_bit_size_attribute (dw_die_ref die, tree decl)
21881 /* Must be a field and a bit field. */
21882 gcc_assert (TREE_CODE (decl) == FIELD_DECL
21883 && DECL_BIT_FIELD_TYPE (decl));
21885 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
21886 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
21889 /* If the compiled language is ANSI C, then add a 'prototyped'
21890 attribute, if arg types are given for the parameters of a function. */
21892 static inline void
21893 add_prototyped_attribute (dw_die_ref die, tree func_type)
21895 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
21897 case DW_LANG_C:
21898 case DW_LANG_C89:
21899 case DW_LANG_C99:
21900 case DW_LANG_C11:
21901 case DW_LANG_ObjC:
21902 if (prototype_p (func_type))
21903 add_AT_flag (die, DW_AT_prototyped, 1);
21904 break;
21905 default:
21906 break;
21910 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
21911 by looking in the type declaration, the object declaration equate table or
21912 the block mapping. */
21914 static inline void
21915 add_abstract_origin_attribute (dw_die_ref die, tree origin)
21917 dw_die_ref origin_die = NULL;
21919 /* For late LTO debug output we want to refer directly to the abstract
21920 DIE in the early debug rather to the possibly existing concrete
21921 instance and avoid creating that just for this purpose. */
21922 sym_off_pair *desc;
21923 if (in_lto_p
21924 && external_die_map
21925 && (desc = external_die_map->get (origin)))
21927 add_AT_external_die_ref (die, DW_AT_abstract_origin,
21928 desc->sym, desc->off);
21929 return;
21932 if (DECL_P (origin))
21933 origin_die = lookup_decl_die (origin);
21934 else if (TYPE_P (origin))
21935 origin_die = lookup_type_die (origin);
21936 else if (TREE_CODE (origin) == BLOCK)
21937 origin_die = lookup_block_die (origin);
21939 /* XXX: Functions that are never lowered don't always have correct block
21940 trees (in the case of java, they simply have no block tree, in some other
21941 languages). For these functions, there is nothing we can really do to
21942 output correct debug info for inlined functions in all cases. Rather
21943 than die, we'll just produce deficient debug info now, in that we will
21944 have variables without a proper abstract origin. In the future, when all
21945 functions are lowered, we should re-add a gcc_assert (origin_die)
21946 here. */
21948 if (origin_die)
21950 dw_attr_node *a;
21951 /* Like above, if we already created a concrete instance DIE
21952 do not use that for the abstract origin but the early DIE
21953 if present. */
21954 if (in_lto_p
21955 && (a = get_AT (origin_die, DW_AT_abstract_origin)))
21956 origin_die = AT_ref (a);
21957 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
21961 /* We do not currently support the pure_virtual attribute. */
21963 static inline void
21964 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
21966 if (DECL_VINDEX (func_decl))
21968 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
21970 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
21971 add_AT_loc (die, DW_AT_vtable_elem_location,
21972 new_loc_descr (DW_OP_constu,
21973 tree_to_shwi (DECL_VINDEX (func_decl)),
21974 0));
21976 /* GNU extension: Record what type this method came from originally. */
21977 if (debug_info_level > DINFO_LEVEL_TERSE
21978 && DECL_CONTEXT (func_decl))
21979 add_AT_die_ref (die, DW_AT_containing_type,
21980 lookup_type_die (DECL_CONTEXT (func_decl)));
21984 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
21985 given decl. This used to be a vendor extension until after DWARF 4
21986 standardized it. */
21988 static void
21989 add_linkage_attr (dw_die_ref die, tree decl)
21991 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
21993 /* Mimic what assemble_name_raw does with a leading '*'. */
21994 if (name[0] == '*')
21995 name = &name[1];
21997 if (dwarf_version >= 4)
21998 add_AT_string (die, DW_AT_linkage_name, name);
21999 else
22000 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
22003 /* Add source coordinate attributes for the given decl. */
22005 static void
22006 add_src_coords_attributes (dw_die_ref die, tree decl)
22008 expanded_location s;
22010 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
22011 return;
22012 s = expand_location (DECL_SOURCE_LOCATION (decl));
22013 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
22014 add_AT_unsigned (die, DW_AT_decl_line, s.line);
22015 if (debug_column_info && s.column)
22016 add_AT_unsigned (die, DW_AT_decl_column, s.column);
22019 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
22021 static void
22022 add_linkage_name_raw (dw_die_ref die, tree decl)
22024 /* Defer until we have an assembler name set. */
22025 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
22027 limbo_die_node *asm_name;
22029 asm_name = ggc_cleared_alloc<limbo_die_node> ();
22030 asm_name->die = die;
22031 asm_name->created_for = decl;
22032 asm_name->next = deferred_asm_name;
22033 deferred_asm_name = asm_name;
22035 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
22036 add_linkage_attr (die, decl);
22039 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
22041 static void
22042 add_linkage_name (dw_die_ref die, tree decl)
22044 if (debug_info_level > DINFO_LEVEL_NONE
22045 && VAR_OR_FUNCTION_DECL_P (decl)
22046 && TREE_PUBLIC (decl)
22047 && !(VAR_P (decl) && DECL_REGISTER (decl))
22048 && die->die_tag != DW_TAG_member)
22049 add_linkage_name_raw (die, decl);
22052 /* Add a DW_AT_name attribute and source coordinate attribute for the
22053 given decl, but only if it actually has a name. */
22055 static void
22056 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
22057 bool no_linkage_name)
22059 tree decl_name;
22061 decl_name = DECL_NAME (decl);
22062 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
22064 const char *name = dwarf2_name (decl, 0);
22065 if (name)
22066 add_name_attribute (die, name);
22067 else
22068 add_desc_attribute (die, decl);
22070 if (! DECL_ARTIFICIAL (decl))
22071 add_src_coords_attributes (die, decl);
22073 if (!no_linkage_name)
22074 add_linkage_name (die, decl);
22076 else
22077 add_desc_attribute (die, decl);
22079 #ifdef VMS_DEBUGGING_INFO
22080 /* Get the function's name, as described by its RTL. This may be different
22081 from the DECL_NAME name used in the source file. */
22082 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
22084 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
22085 XEXP (DECL_RTL (decl), 0), false);
22086 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
22088 #endif /* VMS_DEBUGGING_INFO */
22091 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
22093 static void
22094 add_discr_value (dw_die_ref die, dw_discr_value *value)
22096 dw_attr_node attr;
22098 attr.dw_attr = DW_AT_discr_value;
22099 attr.dw_attr_val.val_class = dw_val_class_discr_value;
22100 attr.dw_attr_val.val_entry = NULL;
22101 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
22102 if (value->pos)
22103 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
22104 else
22105 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
22106 add_dwarf_attr (die, &attr);
22109 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
22111 static void
22112 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
22114 dw_attr_node attr;
22116 attr.dw_attr = DW_AT_discr_list;
22117 attr.dw_attr_val.val_class = dw_val_class_discr_list;
22118 attr.dw_attr_val.val_entry = NULL;
22119 attr.dw_attr_val.v.val_discr_list = discr_list;
22120 add_dwarf_attr (die, &attr);
22123 static inline dw_discr_list_ref
22124 AT_discr_list (dw_attr_node *attr)
22126 return attr->dw_attr_val.v.val_discr_list;
22129 #ifdef VMS_DEBUGGING_INFO
22130 /* Output the debug main pointer die for VMS */
22132 void
22133 dwarf2out_vms_debug_main_pointer (void)
22135 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22136 dw_die_ref die;
22138 /* Allocate the VMS debug main subprogram die. */
22139 die = new_die_raw (DW_TAG_subprogram);
22140 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
22141 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
22142 current_function_funcdef_no);
22143 add_AT_lbl_id (die, DW_AT_entry_pc, label);
22145 /* Make it the first child of comp_unit_die (). */
22146 die->die_parent = comp_unit_die ();
22147 if (comp_unit_die ()->die_child)
22149 die->die_sib = comp_unit_die ()->die_child->die_sib;
22150 comp_unit_die ()->die_child->die_sib = die;
22152 else
22154 die->die_sib = die;
22155 comp_unit_die ()->die_child = die;
22158 #endif /* VMS_DEBUGGING_INFO */
22160 /* walk_tree helper function for uses_local_type, below. */
22162 static tree
22163 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
22165 if (!TYPE_P (*tp))
22166 *walk_subtrees = 0;
22167 else
22169 tree name = TYPE_NAME (*tp);
22170 if (name && DECL_P (name) && decl_function_context (name))
22171 return *tp;
22173 return NULL_TREE;
22176 /* If TYPE involves a function-local type (including a local typedef to a
22177 non-local type), returns that type; otherwise returns NULL_TREE. */
22179 static tree
22180 uses_local_type (tree type)
22182 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
22183 return used;
22186 /* Return the DIE for the scope that immediately contains this type.
22187 Non-named types that do not involve a function-local type get global
22188 scope. Named types nested in namespaces or other types get their
22189 containing scope. All other types (i.e. function-local named types) get
22190 the current active scope. */
22192 static dw_die_ref
22193 scope_die_for (tree t, dw_die_ref context_die)
22195 dw_die_ref scope_die = NULL;
22196 tree containing_scope;
22198 /* Non-types always go in the current scope. */
22199 gcc_assert (TYPE_P (t));
22201 /* Use the scope of the typedef, rather than the scope of the type
22202 it refers to. */
22203 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
22204 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
22205 else
22206 containing_scope = TYPE_CONTEXT (t);
22208 /* Use the containing namespace if there is one. */
22209 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
22211 if (context_die == lookup_decl_die (containing_scope))
22212 /* OK */;
22213 else if (debug_info_level > DINFO_LEVEL_TERSE)
22214 context_die = get_context_die (containing_scope);
22215 else
22216 containing_scope = NULL_TREE;
22219 /* Ignore function type "scopes" from the C frontend. They mean that
22220 a tagged type is local to a parmlist of a function declarator, but
22221 that isn't useful to DWARF. */
22222 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
22223 containing_scope = NULL_TREE;
22225 if (SCOPE_FILE_SCOPE_P (containing_scope))
22227 /* If T uses a local type keep it local as well, to avoid references
22228 to function-local DIEs from outside the function. */
22229 if (current_function_decl && uses_local_type (t))
22230 scope_die = context_die;
22231 else
22232 scope_die = comp_unit_die ();
22234 else if (TYPE_P (containing_scope))
22236 /* For types, we can just look up the appropriate DIE. */
22237 if (debug_info_level > DINFO_LEVEL_TERSE)
22238 scope_die = get_context_die (containing_scope);
22239 else
22241 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
22242 if (scope_die == NULL)
22243 scope_die = comp_unit_die ();
22246 else
22247 scope_die = context_die;
22249 return scope_die;
22252 /* Returns true if CONTEXT_DIE is internal to a function. */
22254 static inline bool
22255 local_scope_p (dw_die_ref context_die)
22257 for (; context_die; context_die = context_die->die_parent)
22258 if (context_die->die_tag == DW_TAG_inlined_subroutine
22259 || context_die->die_tag == DW_TAG_subprogram)
22260 return true;
22262 return false;
22265 /* Returns true if CONTEXT_DIE is a class. */
22267 static inline bool
22268 class_scope_p (dw_die_ref context_die)
22270 return (context_die
22271 && (context_die->die_tag == DW_TAG_structure_type
22272 || context_die->die_tag == DW_TAG_class_type
22273 || context_die->die_tag == DW_TAG_interface_type
22274 || context_die->die_tag == DW_TAG_union_type));
22277 /* Returns true if CONTEXT_DIE is a class or namespace, for deciding
22278 whether or not to treat a DIE in this context as a declaration. */
22280 static inline bool
22281 class_or_namespace_scope_p (dw_die_ref context_die)
22283 return (class_scope_p (context_die)
22284 || (context_die && context_die->die_tag == DW_TAG_namespace));
22287 /* Many forms of DIEs require a "type description" attribute. This
22288 routine locates the proper "type descriptor" die for the type given
22289 by 'type' plus any additional qualifiers given by 'cv_quals', and
22290 adds a DW_AT_type attribute below the given die. */
22292 static void
22293 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
22294 bool reverse, dw_die_ref context_die)
22296 enum tree_code code = TREE_CODE (type);
22297 dw_die_ref type_die = NULL;
22299 if (debug_info_level <= DINFO_LEVEL_TERSE)
22300 return;
22302 /* ??? If this type is an unnamed subrange type of an integral, floating-point
22303 or fixed-point type, use the inner type. This is because we have no
22304 support for unnamed types in base_type_die. This can happen if this is
22305 an Ada subrange type. Correct solution is emit a subrange type die. */
22306 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
22307 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
22308 type = TREE_TYPE (type), code = TREE_CODE (type);
22310 if (code == ERROR_MARK
22311 /* Handle a special case. For functions whose return type is void, we
22312 generate *no* type attribute. (Note that no object may have type
22313 `void', so this only applies to function return types). */
22314 || code == VOID_TYPE)
22315 return;
22317 type_die = modified_type_die (type,
22318 cv_quals | TYPE_QUALS (type),
22319 reverse,
22320 context_die);
22322 if (type_die != NULL)
22323 add_AT_die_ref (object_die, DW_AT_type, type_die);
22326 /* Given an object die, add the calling convention attribute for the
22327 function call type. */
22328 static void
22329 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
22331 enum dwarf_calling_convention value = DW_CC_normal;
22333 value = ((enum dwarf_calling_convention)
22334 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
22336 if (is_fortran ()
22337 && id_equal (DECL_ASSEMBLER_NAME (decl), "MAIN__"))
22339 /* DWARF 2 doesn't provide a way to identify a program's source-level
22340 entry point. DW_AT_calling_convention attributes are only meant
22341 to describe functions' calling conventions. However, lacking a
22342 better way to signal the Fortran main program, we used this for
22343 a long time, following existing custom. Now, DWARF 4 has
22344 DW_AT_main_subprogram, which we add below, but some tools still
22345 rely on the old way, which we thus keep. */
22346 value = DW_CC_program;
22348 if (dwarf_version >= 4 || !dwarf_strict)
22349 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
22352 /* Only add the attribute if the backend requests it, and
22353 is not DW_CC_normal. */
22354 if (value && (value != DW_CC_normal))
22355 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
22358 /* Given a tree pointer to a struct, class, union, or enum type node, return
22359 a pointer to the (string) tag name for the given type, or zero if the type
22360 was declared without a tag. */
22362 static const char *
22363 type_tag (const_tree type)
22365 const char *name = 0;
22367 if (TYPE_NAME (type) != 0)
22369 tree t = 0;
22371 /* Find the IDENTIFIER_NODE for the type name. */
22372 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
22373 && !TYPE_NAMELESS (type))
22374 t = TYPE_NAME (type);
22376 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
22377 a TYPE_DECL node, regardless of whether or not a `typedef' was
22378 involved. */
22379 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
22380 && ! DECL_IGNORED_P (TYPE_NAME (type)))
22382 /* We want to be extra verbose. Don't call dwarf_name if
22383 DECL_NAME isn't set. The default hook for decl_printable_name
22384 doesn't like that, and in this context it's correct to return
22385 0, instead of "<anonymous>" or the like. */
22386 if (DECL_NAME (TYPE_NAME (type))
22387 && !DECL_NAMELESS (TYPE_NAME (type)))
22388 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
22391 /* Now get the name as a string, or invent one. */
22392 if (!name && t != 0)
22393 name = IDENTIFIER_POINTER (t);
22396 return (name == 0 || *name == '\0') ? 0 : name;
22399 /* Return the type associated with a data member, make a special check
22400 for bit field types. */
22402 static inline tree
22403 member_declared_type (const_tree member)
22405 return (DECL_BIT_FIELD_TYPE (member)
22406 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
22409 /* Get the decl's label, as described by its RTL. This may be different
22410 from the DECL_NAME name used in the source file. */
22412 #if 0
22413 static const char *
22414 decl_start_label (tree decl)
22416 rtx x;
22417 const char *fnname;
22419 x = DECL_RTL (decl);
22420 gcc_assert (MEM_P (x));
22422 x = XEXP (x, 0);
22423 gcc_assert (GET_CODE (x) == SYMBOL_REF);
22425 fnname = XSTR (x, 0);
22426 return fnname;
22428 #endif
22430 /* For variable-length arrays that have been previously generated, but
22431 may be incomplete due to missing subscript info, fill the subscript
22432 info. Return TRUE if this is one of those cases. */
22434 static bool
22435 fill_variable_array_bounds (tree type)
22437 if (TREE_ASM_WRITTEN (type)
22438 && TREE_CODE (type) == ARRAY_TYPE
22439 && variably_modified_type_p (type, NULL))
22441 dw_die_ref array_die = lookup_type_die (type);
22442 if (!array_die)
22443 return false;
22444 add_subscript_info (array_die, type, !is_ada ());
22445 return true;
22447 return false;
22450 /* These routines generate the internal representation of the DIE's for
22451 the compilation unit. Debugging information is collected by walking
22452 the declaration trees passed in from dwarf2out_decl(). */
22454 static void
22455 gen_array_type_die (tree type, dw_die_ref context_die)
22457 dw_die_ref array_die;
22459 /* GNU compilers represent multidimensional array types as sequences of one
22460 dimensional array types whose element types are themselves array types.
22461 We sometimes squish that down to a single array_type DIE with multiple
22462 subscripts in the Dwarf debugging info. The draft Dwarf specification
22463 say that we are allowed to do this kind of compression in C, because
22464 there is no difference between an array of arrays and a multidimensional
22465 array. We don't do this for Ada to remain as close as possible to the
22466 actual representation, which is especially important against the language
22467 flexibilty wrt arrays of variable size. */
22469 bool collapse_nested_arrays = !is_ada ();
22471 if (fill_variable_array_bounds (type))
22472 return;
22474 dw_die_ref scope_die = scope_die_for (type, context_die);
22475 tree element_type;
22477 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
22478 DW_TAG_string_type doesn't have DW_AT_type attribute). */
22479 if (TREE_CODE (type) == ARRAY_TYPE
22480 && TYPE_STRING_FLAG (type)
22481 && is_fortran ()
22482 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
22484 HOST_WIDE_INT size;
22486 array_die = new_die (DW_TAG_string_type, scope_die, type);
22487 add_name_attribute (array_die, type_tag (type));
22488 equate_type_number_to_die (type, array_die);
22489 size = int_size_in_bytes (type);
22490 if (size >= 0)
22491 add_AT_unsigned (array_die, DW_AT_byte_size, size);
22492 /* ??? We can't annotate types late, but for LTO we may not
22493 generate a location early either (gfortran.dg/save_6.f90). */
22494 else if (! (early_dwarf && (flag_generate_lto || flag_generate_offload))
22495 && TYPE_DOMAIN (type) != NULL_TREE
22496 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
22498 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
22499 tree rszdecl = szdecl;
22501 size = int_size_in_bytes (TREE_TYPE (szdecl));
22502 if (!DECL_P (szdecl))
22504 if (INDIRECT_REF_P (szdecl)
22505 && DECL_P (TREE_OPERAND (szdecl, 0)))
22507 rszdecl = TREE_OPERAND (szdecl, 0);
22508 if (int_size_in_bytes (TREE_TYPE (rszdecl))
22509 != DWARF2_ADDR_SIZE)
22510 size = 0;
22512 else
22513 size = 0;
22515 if (size > 0)
22517 dw_loc_list_ref loc
22518 = loc_list_from_tree (rszdecl, szdecl == rszdecl ? 2 : 0,
22519 NULL);
22520 if (loc)
22522 add_AT_location_description (array_die, DW_AT_string_length,
22523 loc);
22524 if (size != DWARF2_ADDR_SIZE)
22525 add_AT_unsigned (array_die, dwarf_version >= 5
22526 ? DW_AT_string_length_byte_size
22527 : DW_AT_byte_size, size);
22531 return;
22534 array_die = new_die (DW_TAG_array_type, scope_die, type);
22535 add_name_attribute (array_die, type_tag (type));
22536 equate_type_number_to_die (type, array_die);
22538 if (VECTOR_TYPE_P (type))
22539 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
22541 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
22542 if (is_fortran ()
22543 && TREE_CODE (type) == ARRAY_TYPE
22544 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
22545 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
22546 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
22548 #if 0
22549 /* We default the array ordering. Debuggers will probably do the right
22550 things even if DW_AT_ordering is not present. It's not even an issue
22551 until we start to get into multidimensional arrays anyway. If a debugger
22552 is ever caught doing the Wrong Thing for multi-dimensional arrays,
22553 then we'll have to put the DW_AT_ordering attribute back in. (But if
22554 and when we find out that we need to put these in, we will only do so
22555 for multidimensional arrays. */
22556 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
22557 #endif
22559 if (VECTOR_TYPE_P (type))
22561 /* For VECTOR_TYPEs we use an array DIE with appropriate bounds. */
22562 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
22563 int lb = lower_bound_default ();
22564 if (lb == -1)
22565 lb = 0;
22566 add_bound_info (subrange_die, DW_AT_lower_bound, size_int (lb), NULL);
22567 add_bound_info (subrange_die, DW_AT_upper_bound,
22568 size_int (lb + TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
22570 else
22571 add_subscript_info (array_die, type, collapse_nested_arrays);
22573 /* Add representation of the type of the elements of this array type and
22574 emit the corresponding DIE if we haven't done it already. */
22575 element_type = TREE_TYPE (type);
22576 if (collapse_nested_arrays)
22577 while (TREE_CODE (element_type) == ARRAY_TYPE)
22579 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
22580 break;
22581 element_type = TREE_TYPE (element_type);
22584 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
22585 TREE_CODE (type) == ARRAY_TYPE
22586 && TYPE_REVERSE_STORAGE_ORDER (type),
22587 context_die);
22589 add_gnat_descriptive_type_attribute (array_die, type, context_die);
22590 if (TYPE_ARTIFICIAL (type))
22591 add_AT_flag (array_die, DW_AT_artificial, 1);
22593 if (get_AT (array_die, DW_AT_name))
22594 add_pubtype (type, array_die);
22596 add_alignment_attribute (array_die, type);
22599 /* This routine generates DIE for array with hidden descriptor, details
22600 are filled into *info by a langhook. */
22602 static void
22603 gen_descr_array_type_die (tree type, struct array_descr_info *info,
22604 dw_die_ref context_die)
22606 const dw_die_ref scope_die = scope_die_for (type, context_die);
22607 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
22608 struct loc_descr_context context = {
22609 type, /* context_type */
22610 info->base_decl, /* base_decl */
22611 NULL, /* dpi */
22612 false, /* placeholder_arg */
22613 false, /* placeholder_seen */
22614 false /* strict_signedness */
22616 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
22617 int dim;
22619 add_name_attribute (array_die, type_tag (type));
22620 equate_type_number_to_die (type, array_die);
22622 if (info->ndimensions > 1)
22623 switch (info->ordering)
22625 case array_descr_ordering_row_major:
22626 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
22627 break;
22628 case array_descr_ordering_column_major:
22629 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
22630 break;
22631 default:
22632 break;
22635 if (dwarf_version >= 3 || !dwarf_strict)
22637 if (info->data_location)
22638 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
22639 dw_scalar_form_exprloc, &context);
22640 if (info->associated)
22641 add_scalar_info (array_die, DW_AT_associated, info->associated,
22642 dw_scalar_form_constant
22643 | dw_scalar_form_exprloc
22644 | dw_scalar_form_reference, &context);
22645 if (info->allocated)
22646 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
22647 dw_scalar_form_constant
22648 | dw_scalar_form_exprloc
22649 | dw_scalar_form_reference, &context);
22650 if (info->stride)
22652 const enum dwarf_attribute attr
22653 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
22654 const int forms
22655 = (info->stride_in_bits)
22656 ? dw_scalar_form_constant
22657 : (dw_scalar_form_constant
22658 | dw_scalar_form_exprloc
22659 | dw_scalar_form_reference);
22661 add_scalar_info (array_die, attr, info->stride, forms, &context);
22664 if (dwarf_version >= 5)
22666 if (info->rank)
22668 add_scalar_info (array_die, DW_AT_rank, info->rank,
22669 dw_scalar_form_constant
22670 | dw_scalar_form_exprloc, &context);
22671 subrange_tag = DW_TAG_generic_subrange;
22672 context.placeholder_arg = true;
22676 add_gnat_descriptive_type_attribute (array_die, type, context_die);
22678 for (dim = 0; dim < info->ndimensions; dim++)
22680 dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
22682 if (info->dimen[dim].bounds_type)
22683 add_type_attribute (subrange_die,
22684 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
22685 false, context_die);
22686 if (info->dimen[dim].lower_bound)
22687 add_bound_info (subrange_die, DW_AT_lower_bound,
22688 info->dimen[dim].lower_bound, &context);
22689 if (info->dimen[dim].upper_bound)
22690 add_bound_info (subrange_die, DW_AT_upper_bound,
22691 info->dimen[dim].upper_bound, &context);
22692 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
22693 add_scalar_info (subrange_die, DW_AT_byte_stride,
22694 info->dimen[dim].stride,
22695 dw_scalar_form_constant
22696 | dw_scalar_form_exprloc
22697 | dw_scalar_form_reference,
22698 &context);
22701 gen_type_die (info->element_type, context_die);
22702 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
22703 TREE_CODE (type) == ARRAY_TYPE
22704 && TYPE_REVERSE_STORAGE_ORDER (type),
22705 context_die);
22707 if (get_AT (array_die, DW_AT_name))
22708 add_pubtype (type, array_die);
22710 add_alignment_attribute (array_die, type);
22713 #if 0
22714 static void
22715 gen_entry_point_die (tree decl, dw_die_ref context_die)
22717 tree origin = decl_ultimate_origin (decl);
22718 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
22720 if (origin != NULL)
22721 add_abstract_origin_attribute (decl_die, origin);
22722 else
22724 add_name_and_src_coords_attributes (decl_die, decl);
22725 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
22726 TYPE_UNQUALIFIED, false, context_die);
22729 if (DECL_ABSTRACT_P (decl))
22730 equate_decl_number_to_die (decl, decl_die);
22731 else
22732 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
22734 #endif
22736 /* Walk through the list of incomplete types again, trying once more to
22737 emit full debugging info for them. */
22739 static void
22740 retry_incomplete_types (void)
22742 set_early_dwarf s;
22743 int i;
22745 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
22746 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
22747 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
22748 vec_safe_truncate (incomplete_types, 0);
22751 /* Determine what tag to use for a record type. */
22753 static enum dwarf_tag
22754 record_type_tag (tree type)
22756 if (! lang_hooks.types.classify_record)
22757 return DW_TAG_structure_type;
22759 switch (lang_hooks.types.classify_record (type))
22761 case RECORD_IS_STRUCT:
22762 return DW_TAG_structure_type;
22764 case RECORD_IS_CLASS:
22765 return DW_TAG_class_type;
22767 case RECORD_IS_INTERFACE:
22768 if (dwarf_version >= 3 || !dwarf_strict)
22769 return DW_TAG_interface_type;
22770 return DW_TAG_structure_type;
22772 default:
22773 gcc_unreachable ();
22777 /* Generate a DIE to represent an enumeration type. Note that these DIEs
22778 include all of the information about the enumeration values also. Each
22779 enumerated type name/value is listed as a child of the enumerated type
22780 DIE. */
22782 static dw_die_ref
22783 gen_enumeration_type_die (tree type, dw_die_ref context_die)
22785 dw_die_ref type_die = lookup_type_die (type);
22786 dw_die_ref orig_type_die = type_die;
22788 if (type_die == NULL)
22790 type_die = new_die (DW_TAG_enumeration_type,
22791 scope_die_for (type, context_die), type);
22792 equate_type_number_to_die (type, type_die);
22793 add_name_attribute (type_die, type_tag (type));
22794 if ((dwarf_version >= 4 || !dwarf_strict)
22795 && ENUM_IS_SCOPED (type))
22796 add_AT_flag (type_die, DW_AT_enum_class, 1);
22797 if (ENUM_IS_OPAQUE (type) && TYPE_SIZE (type))
22798 add_AT_flag (type_die, DW_AT_declaration, 1);
22799 if (!dwarf_strict)
22800 add_AT_unsigned (type_die, DW_AT_encoding,
22801 TYPE_UNSIGNED (type)
22802 ? DW_ATE_unsigned
22803 : DW_ATE_signed);
22805 else if (! TYPE_SIZE (type) || ENUM_IS_OPAQUE (type))
22806 return type_die;
22807 else
22808 remove_AT (type_die, DW_AT_declaration);
22810 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
22811 given enum type is incomplete, do not generate the DW_AT_byte_size
22812 attribute or the DW_AT_element_list attribute. */
22813 if (TYPE_SIZE (type))
22815 tree link;
22817 if (!ENUM_IS_OPAQUE (type))
22818 TREE_ASM_WRITTEN (type) = 1;
22819 if (!orig_type_die || !get_AT (type_die, DW_AT_byte_size))
22820 add_byte_size_attribute (type_die, type);
22821 if (!orig_type_die || !get_AT (type_die, DW_AT_alignment))
22822 add_alignment_attribute (type_die, type);
22823 if ((dwarf_version >= 3 || !dwarf_strict)
22824 && (!orig_type_die || !get_AT (type_die, DW_AT_type)))
22826 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
22827 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
22828 context_die);
22830 if (TYPE_STUB_DECL (type) != NULL_TREE)
22832 if (!orig_type_die || !get_AT (type_die, DW_AT_decl_file))
22833 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
22834 if (!orig_type_die || !get_AT (type_die, DW_AT_accessibility))
22835 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
22838 /* If the first reference to this type was as the return type of an
22839 inline function, then it may not have a parent. Fix this now. */
22840 if (type_die->die_parent == NULL)
22841 add_child_die (scope_die_for (type, context_die), type_die);
22843 for (link = TYPE_VALUES (type);
22844 link != NULL; link = TREE_CHAIN (link))
22846 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
22847 tree value = TREE_VALUE (link);
22849 if (DECL_P (value))
22850 equate_decl_number_to_die (value, enum_die);
22852 gcc_assert (!ENUM_IS_OPAQUE (type));
22853 add_name_attribute (enum_die,
22854 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
22856 if (TREE_CODE (value) == CONST_DECL)
22857 value = DECL_INITIAL (value);
22859 if (simple_type_size_in_bits (TREE_TYPE (value))
22860 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
22862 /* For constant forms created by add_AT_unsigned DWARF
22863 consumers (GDB, elfutils, etc.) always zero extend
22864 the value. Only when the actual value is negative
22865 do we need to use add_AT_int to generate a constant
22866 form that can represent negative values. */
22867 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
22868 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
22869 add_AT_unsigned (enum_die, DW_AT_const_value,
22870 (unsigned HOST_WIDE_INT) val);
22871 else
22872 add_AT_int (enum_die, DW_AT_const_value, val);
22874 else
22875 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
22876 that here. TODO: This should be re-worked to use correct
22877 signed/unsigned double tags for all cases. */
22878 add_AT_wide (enum_die, DW_AT_const_value, wi::to_wide (value));
22881 add_gnat_descriptive_type_attribute (type_die, type, context_die);
22882 if (TYPE_ARTIFICIAL (type)
22883 && (!orig_type_die || !get_AT (type_die, DW_AT_artificial)))
22884 add_AT_flag (type_die, DW_AT_artificial, 1);
22886 else
22887 add_AT_flag (type_die, DW_AT_declaration, 1);
22889 add_pubtype (type, type_die);
22891 return type_die;
22894 /* Generate a DIE to represent either a real live formal parameter decl or to
22895 represent just the type of some formal parameter position in some function
22896 type.
22898 Note that this routine is a bit unusual because its argument may be a
22899 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
22900 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
22901 node. If it's the former then this function is being called to output a
22902 DIE to represent a formal parameter object (or some inlining thereof). If
22903 it's the latter, then this function is only being called to output a
22904 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
22905 argument type of some subprogram type.
22906 If EMIT_NAME_P is true, name and source coordinate attributes
22907 are emitted. */
22909 static dw_die_ref
22910 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
22911 dw_die_ref context_die)
22913 tree node_or_origin = node ? node : origin;
22914 tree ultimate_origin;
22915 dw_die_ref parm_die = NULL;
22917 if (DECL_P (node_or_origin))
22919 parm_die = lookup_decl_die (node);
22921 /* If the contexts differ, we may not be talking about the same
22922 thing.
22923 ??? When in LTO the DIE parent is the "abstract" copy and the
22924 context_die is the specification "copy". */
22925 if (parm_die
22926 && parm_die->die_parent != context_die
22927 && (parm_die->die_parent->die_tag != DW_TAG_GNU_formal_parameter_pack
22928 || parm_die->die_parent->die_parent != context_die)
22929 && !in_lto_p)
22931 gcc_assert (!DECL_ABSTRACT_P (node));
22932 /* This can happen when creating a concrete instance, in
22933 which case we need to create a new DIE that will get
22934 annotated with DW_AT_abstract_origin. */
22935 parm_die = NULL;
22938 if (parm_die && parm_die->die_parent == NULL)
22940 /* Check that parm_die already has the right attributes that
22941 we would have added below. If any attributes are
22942 missing, fall through to add them. */
22943 if (! DECL_ABSTRACT_P (node_or_origin)
22944 && !get_AT (parm_die, DW_AT_location)
22945 && !get_AT (parm_die, DW_AT_const_value))
22946 /* We are missing location info, and are about to add it. */
22948 else
22950 add_child_die (context_die, parm_die);
22951 return parm_die;
22956 /* If we have a previously generated DIE, use it, unless this is an
22957 concrete instance (origin != NULL), in which case we need a new
22958 DIE with a corresponding DW_AT_abstract_origin. */
22959 bool reusing_die;
22960 if (parm_die && origin == NULL)
22961 reusing_die = true;
22962 else
22964 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
22965 reusing_die = false;
22968 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
22970 case tcc_declaration:
22971 ultimate_origin = decl_ultimate_origin (node_or_origin);
22972 if (node || ultimate_origin)
22973 origin = ultimate_origin;
22975 if (reusing_die)
22976 goto add_location;
22978 if (origin != NULL)
22979 add_abstract_origin_attribute (parm_die, origin);
22980 else if (emit_name_p)
22981 add_name_and_src_coords_attributes (parm_die, node);
22982 if (origin == NULL
22983 || (! DECL_ABSTRACT_P (node_or_origin)
22984 && variably_modified_type_p (TREE_TYPE (node_or_origin),
22985 decl_function_context
22986 (node_or_origin))))
22988 tree type = TREE_TYPE (node_or_origin);
22989 if (decl_by_reference_p (node_or_origin))
22990 add_type_attribute (parm_die, TREE_TYPE (type),
22991 TYPE_UNQUALIFIED,
22992 false, context_die);
22993 else
22994 add_type_attribute (parm_die, type,
22995 decl_quals (node_or_origin),
22996 false, context_die);
22998 if (origin == NULL && DECL_ARTIFICIAL (node))
22999 add_AT_flag (parm_die, DW_AT_artificial, 1);
23000 add_location:
23001 if (node && node != origin)
23002 equate_decl_number_to_die (node, parm_die);
23003 if (! DECL_ABSTRACT_P (node_or_origin))
23004 add_location_or_const_value_attribute (parm_die, node_or_origin,
23005 node == NULL);
23007 break;
23009 case tcc_type:
23010 /* We were called with some kind of a ..._TYPE node. */
23011 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
23012 context_die);
23013 break;
23015 default:
23016 gcc_unreachable ();
23019 return parm_die;
23022 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
23023 children DW_TAG_formal_parameter DIEs representing the arguments of the
23024 parameter pack.
23026 PARM_PACK must be a function parameter pack.
23027 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
23028 must point to the subsequent arguments of the function PACK_ARG belongs to.
23029 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
23030 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
23031 following the last one for which a DIE was generated. */
23033 static dw_die_ref
23034 gen_formal_parameter_pack_die (tree parm_pack,
23035 tree pack_arg,
23036 dw_die_ref subr_die,
23037 tree *next_arg)
23039 tree arg;
23040 dw_die_ref parm_pack_die;
23042 gcc_assert (parm_pack
23043 && lang_hooks.function_parameter_pack_p (parm_pack)
23044 && subr_die);
23046 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
23047 add_src_coords_attributes (parm_pack_die, parm_pack);
23049 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
23051 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
23052 parm_pack))
23053 break;
23054 gen_formal_parameter_die (arg, NULL,
23055 false /* Don't emit name attribute. */,
23056 parm_pack_die);
23058 if (next_arg)
23059 *next_arg = arg;
23060 return parm_pack_die;
23063 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
23064 at the end of an (ANSI prototyped) formal parameters list. */
23066 static void
23067 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
23069 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
23072 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
23073 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
23074 parameters as specified in some function type specification (except for
23075 those which appear as part of a function *definition*). */
23077 static void
23078 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
23080 tree link;
23081 tree formal_type = NULL;
23082 tree first_parm_type;
23083 tree arg;
23085 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
23087 arg = DECL_ARGUMENTS (function_or_method_type);
23088 function_or_method_type = TREE_TYPE (function_or_method_type);
23090 else
23091 arg = NULL_TREE;
23093 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
23095 /* Make our first pass over the list of formal parameter types and output a
23096 DW_TAG_formal_parameter DIE for each one. */
23097 for (link = first_parm_type; link; )
23099 dw_die_ref parm_die;
23101 formal_type = TREE_VALUE (link);
23102 if (formal_type == void_type_node)
23103 break;
23105 /* Output a (nameless) DIE to represent the formal parameter itself. */
23106 parm_die = gen_formal_parameter_die (formal_type, NULL,
23107 true /* Emit name attribute. */,
23108 context_die);
23109 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
23110 && link == first_parm_type)
23112 add_AT_flag (parm_die, DW_AT_artificial, 1);
23113 if (dwarf_version >= 3 || !dwarf_strict)
23114 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
23116 else if (arg && DECL_ARTIFICIAL (arg))
23117 add_AT_flag (parm_die, DW_AT_artificial, 1);
23119 link = TREE_CHAIN (link);
23120 if (arg)
23121 arg = DECL_CHAIN (arg);
23124 /* If this function type has an ellipsis, add a
23125 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
23126 if (formal_type != void_type_node)
23127 gen_unspecified_parameters_die (function_or_method_type, context_die);
23129 /* Make our second (and final) pass over the list of formal parameter types
23130 and output DIEs to represent those types (as necessary). */
23131 for (link = TYPE_ARG_TYPES (function_or_method_type);
23132 link && TREE_VALUE (link);
23133 link = TREE_CHAIN (link))
23134 gen_type_die (TREE_VALUE (link), context_die);
23137 /* We want to generate the DIE for TYPE so that we can generate the
23138 die for MEMBER, which has been defined; we will need to refer back
23139 to the member declaration nested within TYPE. If we're trying to
23140 generate minimal debug info for TYPE, processing TYPE won't do the
23141 trick; we need to attach the member declaration by hand. */
23143 static void
23144 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
23146 gen_type_die (type, context_die);
23148 /* If we're trying to avoid duplicate debug info, we may not have
23149 emitted the member decl for this function. Emit it now. */
23150 if (TYPE_STUB_DECL (type)
23151 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
23152 && ! lookup_decl_die (member))
23154 dw_die_ref type_die;
23155 gcc_assert (!decl_ultimate_origin (member));
23157 type_die = lookup_type_die_strip_naming_typedef (type);
23158 if (TREE_CODE (member) == FUNCTION_DECL)
23159 gen_subprogram_die (member, type_die);
23160 else if (TREE_CODE (member) == FIELD_DECL)
23162 /* Ignore the nameless fields that are used to skip bits but handle
23163 C++ anonymous unions and structs. */
23164 if (DECL_NAME (member) != NULL_TREE
23165 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
23166 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
23168 struct vlr_context vlr_ctx = {
23169 DECL_CONTEXT (member), /* struct_type */
23170 NULL_TREE /* variant_part_offset */
23172 gen_type_die (member_declared_type (member), type_die);
23173 gen_field_die (member, &vlr_ctx, type_die);
23176 else
23177 gen_variable_die (member, NULL_TREE, type_die);
23181 /* Forward declare these functions, because they are mutually recursive
23182 with their set_block_* pairing functions. */
23183 static void set_decl_origin_self (tree);
23185 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
23186 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
23187 that it points to the node itself, thus indicating that the node is its
23188 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
23189 the given node is NULL, recursively descend the decl/block tree which
23190 it is the root of, and for each other ..._DECL or BLOCK node contained
23191 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
23192 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
23193 values to point to themselves. */
23195 static void
23196 set_block_origin_self (tree stmt)
23198 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
23200 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
23203 tree local_decl;
23205 for (local_decl = BLOCK_VARS (stmt);
23206 local_decl != NULL_TREE;
23207 local_decl = DECL_CHAIN (local_decl))
23208 /* Do not recurse on nested functions since the inlining status
23209 of parent and child can be different as per the DWARF spec. */
23210 if (TREE_CODE (local_decl) != FUNCTION_DECL
23211 && !DECL_EXTERNAL (local_decl))
23212 set_decl_origin_self (local_decl);
23216 tree subblock;
23218 for (subblock = BLOCK_SUBBLOCKS (stmt);
23219 subblock != NULL_TREE;
23220 subblock = BLOCK_CHAIN (subblock))
23221 set_block_origin_self (subblock); /* Recurse. */
23226 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
23227 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
23228 node to so that it points to the node itself, thus indicating that the
23229 node represents its own (abstract) origin. Additionally, if the
23230 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
23231 the decl/block tree of which the given node is the root of, and for
23232 each other ..._DECL or BLOCK node contained therein whose
23233 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
23234 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
23235 point to themselves. */
23237 static void
23238 set_decl_origin_self (tree decl)
23240 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
23242 DECL_ABSTRACT_ORIGIN (decl) = decl;
23243 if (TREE_CODE (decl) == FUNCTION_DECL)
23245 tree arg;
23247 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
23248 DECL_ABSTRACT_ORIGIN (arg) = arg;
23249 if (DECL_INITIAL (decl) != NULL_TREE
23250 && DECL_INITIAL (decl) != error_mark_node)
23251 set_block_origin_self (DECL_INITIAL (decl));
23256 /* Mark the early DIE for DECL as the abstract instance. */
23258 static void
23259 dwarf2out_abstract_function (tree decl)
23261 dw_die_ref old_die;
23263 /* Make sure we have the actual abstract inline, not a clone. */
23264 decl = DECL_ORIGIN (decl);
23266 if (DECL_IGNORED_P (decl))
23267 return;
23269 /* In LTO we're all set. We already created abstract instances
23270 early and we want to avoid creating a concrete instance of that
23271 if we don't output it. */
23272 if (in_lto_p)
23273 return;
23275 old_die = lookup_decl_die (decl);
23276 gcc_assert (old_die != NULL);
23277 if (get_AT (old_die, DW_AT_inline))
23278 /* We've already generated the abstract instance. */
23279 return;
23281 /* Go ahead and put DW_AT_inline on the DIE. */
23282 if (DECL_DECLARED_INLINE_P (decl))
23284 if (cgraph_function_possibly_inlined_p (decl))
23285 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_inlined);
23286 else
23287 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_not_inlined);
23289 else
23291 if (cgraph_function_possibly_inlined_p (decl))
23292 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_inlined);
23293 else
23294 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_not_inlined);
23297 if (DECL_DECLARED_INLINE_P (decl)
23298 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
23299 add_AT_flag (old_die, DW_AT_artificial, 1);
23301 set_decl_origin_self (decl);
23304 /* Helper function of premark_used_types() which gets called through
23305 htab_traverse.
23307 Marks the DIE of a given type in *SLOT as perennial, so it never gets
23308 marked as unused by prune_unused_types. */
23310 bool
23311 premark_used_types_helper (tree const &type, void *)
23313 dw_die_ref die;
23315 die = lookup_type_die (type);
23316 if (die != NULL)
23317 die->die_perennial_p = 1;
23318 return true;
23321 /* Helper function of premark_types_used_by_global_vars which gets called
23322 through htab_traverse.
23324 Marks the DIE of a given type in *SLOT as perennial, so it never gets
23325 marked as unused by prune_unused_types. The DIE of the type is marked
23326 only if the global variable using the type will actually be emitted. */
23329 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
23330 void *)
23332 struct types_used_by_vars_entry *entry;
23333 dw_die_ref die;
23335 entry = (struct types_used_by_vars_entry *) *slot;
23336 gcc_assert (entry->type != NULL
23337 && entry->var_decl != NULL);
23338 die = lookup_type_die (entry->type);
23339 if (die)
23341 /* Ask cgraph if the global variable really is to be emitted.
23342 If yes, then we'll keep the DIE of ENTRY->TYPE. */
23343 varpool_node *node = varpool_node::get (entry->var_decl);
23344 if (node && node->definition)
23346 die->die_perennial_p = 1;
23347 /* Keep the parent DIEs as well. */
23348 while ((die = die->die_parent) && die->die_perennial_p == 0)
23349 die->die_perennial_p = 1;
23352 return 1;
23355 /* Mark all members of used_types_hash as perennial. */
23357 static void
23358 premark_used_types (struct function *fun)
23360 if (fun && fun->used_types_hash)
23361 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
23364 /* Mark all members of types_used_by_vars_entry as perennial. */
23366 static void
23367 premark_types_used_by_global_vars (void)
23369 if (types_used_by_vars_hash)
23370 types_used_by_vars_hash
23371 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
23374 /* Mark all variables used by the symtab as perennial. */
23376 static void
23377 premark_used_variables (void)
23379 /* Mark DIEs in the symtab as used. */
23380 varpool_node *var;
23381 FOR_EACH_VARIABLE (var)
23383 dw_die_ref die = lookup_decl_die (var->decl);
23384 if (die)
23385 die->die_perennial_p = 1;
23389 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
23390 for CA_LOC call arg loc node. */
23392 static dw_die_ref
23393 gen_call_site_die (tree decl, dw_die_ref subr_die,
23394 struct call_arg_loc_node *ca_loc)
23396 dw_die_ref stmt_die = NULL, die;
23397 tree block = ca_loc->block;
23399 while (block
23400 && block != DECL_INITIAL (decl)
23401 && TREE_CODE (block) == BLOCK)
23403 stmt_die = lookup_block_die (block);
23404 if (stmt_die)
23405 break;
23406 block = BLOCK_SUPERCONTEXT (block);
23408 if (stmt_die == NULL)
23409 stmt_die = subr_die;
23410 die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
23411 add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc), ca_loc->label);
23412 if (ca_loc->tail_call_p)
23413 add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
23414 if (ca_loc->symbol_ref)
23416 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
23417 if (tdie)
23418 add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
23419 else
23420 add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
23421 false);
23423 return die;
23426 /* Generate a DIE to represent a declared function (either file-scope or
23427 block-local). */
23429 static void
23430 gen_subprogram_die (tree decl, dw_die_ref context_die)
23432 tree origin = decl_ultimate_origin (decl);
23433 dw_die_ref subr_die;
23434 dw_die_ref old_die = lookup_decl_die (decl);
23435 bool old_die_had_no_children = false;
23437 /* This function gets called multiple times for different stages of
23438 the debug process. For example, for func() in this code:
23440 namespace S
23442 void func() { ... }
23445 ...we get called 4 times. Twice in early debug and twice in
23446 late debug:
23448 Early debug
23449 -----------
23451 1. Once while generating func() within the namespace. This is
23452 the declaration. The declaration bit below is set, as the
23453 context is the namespace.
23455 A new DIE will be generated with DW_AT_declaration set.
23457 2. Once for func() itself. This is the specification. The
23458 declaration bit below is clear as the context is the CU.
23460 We will use the cached DIE from (1) to create a new DIE with
23461 DW_AT_specification pointing to the declaration in (1).
23463 Late debug via rest_of_handle_final()
23464 -------------------------------------
23466 3. Once generating func() within the namespace. This is also the
23467 declaration, as in (1), but this time we will early exit below
23468 as we have a cached DIE and a declaration needs no additional
23469 annotations (no locations), as the source declaration line
23470 info is enough.
23472 4. Once for func() itself. As in (2), this is the specification,
23473 but this time we will re-use the cached DIE, and just annotate
23474 it with the location information that should now be available.
23476 For something without namespaces, but with abstract instances, we
23477 are also called a multiple times:
23479 class Base
23481 public:
23482 Base (); // constructor declaration (1)
23485 Base::Base () { } // constructor specification (2)
23487 Early debug
23488 -----------
23490 1. Once for the Base() constructor by virtue of it being a
23491 member of the Base class. This is done via
23492 rest_of_type_compilation.
23494 This is a declaration, so a new DIE will be created with
23495 DW_AT_declaration.
23497 2. Once for the Base() constructor definition, but this time
23498 while generating the abstract instance of the base
23499 constructor (__base_ctor) which is being generated via early
23500 debug of reachable functions.
23502 Even though we have a cached version of the declaration (1),
23503 we will create a DW_AT_specification of the declaration DIE
23504 in (1).
23506 3. Once for the __base_ctor itself, but this time, we generate
23507 an DW_AT_abstract_origin version of the DW_AT_specification in
23508 (2).
23510 Late debug via rest_of_handle_final
23511 -----------------------------------
23513 4. One final time for the __base_ctor (which will have a cached
23514 DIE with DW_AT_abstract_origin created in (3). This time,
23515 we will just annotate the location information now
23516 available.
23518 int declaration = (current_function_decl != decl
23519 || (!DECL_INITIAL (decl) && !origin)
23520 || class_or_namespace_scope_p (context_die));
23522 /* A declaration that has been previously dumped needs no
23523 additional information. */
23524 if (old_die && declaration)
23525 return;
23527 if (in_lto_p && old_die && old_die->die_child == NULL)
23528 old_die_had_no_children = true;
23530 /* Now that the C++ front end lazily declares artificial member fns, we
23531 might need to retrofit the declaration into its class. */
23532 if (!declaration && !origin && !old_die
23533 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
23534 && !class_or_namespace_scope_p (context_die)
23535 && debug_info_level > DINFO_LEVEL_TERSE)
23536 old_die = force_decl_die (decl);
23538 /* A concrete instance, tag a new DIE with DW_AT_abstract_origin. */
23539 if (origin != NULL)
23541 gcc_assert (!declaration || local_scope_p (context_die));
23543 /* Fixup die_parent for the abstract instance of a nested
23544 inline function. */
23545 if (old_die && old_die->die_parent == NULL)
23546 add_child_die (context_die, old_die);
23548 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
23550 /* If we have a DW_AT_abstract_origin we have a working
23551 cached version. */
23552 subr_die = old_die;
23554 else
23556 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
23557 add_abstract_origin_attribute (subr_die, origin);
23558 /* This is where the actual code for a cloned function is.
23559 Let's emit linkage name attribute for it. This helps
23560 debuggers to e.g, set breakpoints into
23561 constructors/destructors when the user asks "break
23562 K::K". */
23563 add_linkage_name (subr_die, decl);
23566 /* A cached copy, possibly from early dwarf generation. Reuse as
23567 much as possible. */
23568 else if (old_die)
23570 if (!get_AT_flag (old_die, DW_AT_declaration)
23571 /* We can have a normal definition following an inline one in the
23572 case of redefinition of GNU C extern inlines.
23573 It seems reasonable to use AT_specification in this case. */
23574 && !get_AT (old_die, DW_AT_inline))
23576 /* Detect and ignore this case, where we are trying to output
23577 something we have already output. */
23578 if (get_AT (old_die, DW_AT_low_pc)
23579 || get_AT (old_die, DW_AT_ranges))
23580 return;
23582 /* If we have no location information, this must be a
23583 partially generated DIE from early dwarf generation.
23584 Fall through and generate it. */
23587 /* If the definition comes from the same place as the declaration,
23588 maybe use the old DIE. We always want the DIE for this function
23589 that has the *_pc attributes to be under comp_unit_die so the
23590 debugger can find it. We also need to do this for abstract
23591 instances of inlines, since the spec requires the out-of-line copy
23592 to have the same parent. For local class methods, this doesn't
23593 apply; we just use the old DIE. */
23594 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
23595 struct dwarf_file_data * file_index = lookup_filename (s.file);
23596 if (((is_unit_die (old_die->die_parent)
23597 /* This condition fixes the inconsistency/ICE with the
23598 following Fortran test (or some derivative thereof) while
23599 building libgfortran:
23601 module some_m
23602 contains
23603 logical function funky (FLAG)
23604 funky = .true.
23605 end function
23606 end module
23608 || (old_die->die_parent
23609 && old_die->die_parent->die_tag == DW_TAG_module)
23610 || local_scope_p (old_die->die_parent)
23611 || context_die == NULL)
23612 && (DECL_ARTIFICIAL (decl)
23613 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
23614 && (get_AT_unsigned (old_die, DW_AT_decl_line)
23615 == (unsigned) s.line)
23616 && (!debug_column_info
23617 || s.column == 0
23618 || (get_AT_unsigned (old_die, DW_AT_decl_column)
23619 == (unsigned) s.column)))))
23620 /* With LTO if there's an abstract instance for
23621 the old DIE, this is a concrete instance and
23622 thus re-use the DIE. */
23623 || get_AT (old_die, DW_AT_abstract_origin))
23625 subr_die = old_die;
23627 /* Clear out the declaration attribute, but leave the
23628 parameters so they can be augmented with location
23629 information later. Unless this was a declaration, in
23630 which case, wipe out the nameless parameters and recreate
23631 them further down. */
23632 if (remove_AT (subr_die, DW_AT_declaration))
23635 remove_AT (subr_die, DW_AT_object_pointer);
23636 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
23639 /* Make a specification pointing to the previously built
23640 declaration. */
23641 else
23643 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
23644 add_AT_specification (subr_die, old_die);
23645 add_pubname (decl, subr_die);
23646 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
23647 add_AT_file (subr_die, DW_AT_decl_file, file_index);
23648 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
23649 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
23650 if (debug_column_info
23651 && s.column
23652 && (get_AT_unsigned (old_die, DW_AT_decl_column)
23653 != (unsigned) s.column))
23654 add_AT_unsigned (subr_die, DW_AT_decl_column, s.column);
23656 /* If the prototype had an 'auto' or 'decltype(auto)' in
23657 the return type, emit the real type on the definition die. */
23658 if (is_cxx () && debug_info_level > DINFO_LEVEL_TERSE)
23660 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
23661 while (die
23662 && (die->die_tag == DW_TAG_reference_type
23663 || die->die_tag == DW_TAG_rvalue_reference_type
23664 || die->die_tag == DW_TAG_pointer_type
23665 || die->die_tag == DW_TAG_const_type
23666 || die->die_tag == DW_TAG_volatile_type
23667 || die->die_tag == DW_TAG_restrict_type
23668 || die->die_tag == DW_TAG_array_type
23669 || die->die_tag == DW_TAG_ptr_to_member_type
23670 || die->die_tag == DW_TAG_subroutine_type))
23671 die = get_AT_ref (die, DW_AT_type);
23672 if (die == auto_die || die == decltype_auto_die)
23673 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
23674 TYPE_UNQUALIFIED, false, context_die);
23677 /* When we process the method declaration, we haven't seen
23678 the out-of-class defaulted definition yet, so we have to
23679 recheck now. */
23680 if ((dwarf_version >= 5 || ! dwarf_strict)
23681 && !get_AT (subr_die, DW_AT_defaulted))
23683 int defaulted
23684 = lang_hooks.decls.decl_dwarf_attribute (decl,
23685 DW_AT_defaulted);
23686 if (defaulted != -1)
23688 /* Other values must have been handled before. */
23689 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
23690 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
23695 /* Create a fresh DIE for anything else. */
23696 else
23698 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
23700 if (TREE_PUBLIC (decl))
23701 add_AT_flag (subr_die, DW_AT_external, 1);
23703 add_name_and_src_coords_attributes (subr_die, decl);
23704 add_pubname (decl, subr_die);
23705 if (debug_info_level > DINFO_LEVEL_TERSE)
23707 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
23708 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
23709 TYPE_UNQUALIFIED, false, context_die);
23712 add_pure_or_virtual_attribute (subr_die, decl);
23713 if (DECL_ARTIFICIAL (decl))
23714 add_AT_flag (subr_die, DW_AT_artificial, 1);
23716 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
23717 add_AT_flag (subr_die, DW_AT_noreturn, 1);
23719 add_alignment_attribute (subr_die, decl);
23721 add_accessibility_attribute (subr_die, decl);
23724 /* Unless we have an existing non-declaration DIE, equate the new
23725 DIE. */
23726 if (!old_die || is_declaration_die (old_die))
23727 equate_decl_number_to_die (decl, subr_die);
23729 if (declaration)
23731 if (!old_die || !get_AT (old_die, DW_AT_inline))
23733 add_AT_flag (subr_die, DW_AT_declaration, 1);
23735 /* If this is an explicit function declaration then generate
23736 a DW_AT_explicit attribute. */
23737 if ((dwarf_version >= 3 || !dwarf_strict)
23738 && lang_hooks.decls.decl_dwarf_attribute (decl,
23739 DW_AT_explicit) == 1)
23740 add_AT_flag (subr_die, DW_AT_explicit, 1);
23742 /* If this is a C++11 deleted special function member then generate
23743 a DW_AT_deleted attribute. */
23744 if ((dwarf_version >= 5 || !dwarf_strict)
23745 && lang_hooks.decls.decl_dwarf_attribute (decl,
23746 DW_AT_deleted) == 1)
23747 add_AT_flag (subr_die, DW_AT_deleted, 1);
23749 /* If this is a C++11 defaulted special function member then
23750 generate a DW_AT_defaulted attribute. */
23751 if (dwarf_version >= 5 || !dwarf_strict)
23753 int defaulted
23754 = lang_hooks.decls.decl_dwarf_attribute (decl,
23755 DW_AT_defaulted);
23756 if (defaulted != -1)
23757 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
23760 /* If this is a C++11 non-static member function with & ref-qualifier
23761 then generate a DW_AT_reference attribute. */
23762 if ((dwarf_version >= 5 || !dwarf_strict)
23763 && lang_hooks.decls.decl_dwarf_attribute (decl,
23764 DW_AT_reference) == 1)
23765 add_AT_flag (subr_die, DW_AT_reference, 1);
23767 /* If this is a C++11 non-static member function with &&
23768 ref-qualifier then generate a DW_AT_reference attribute. */
23769 if ((dwarf_version >= 5 || !dwarf_strict)
23770 && lang_hooks.decls.decl_dwarf_attribute (decl,
23771 DW_AT_rvalue_reference)
23772 == 1)
23773 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
23776 /* For non DECL_EXTERNALs, if range information is available, fill
23777 the DIE with it. */
23778 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
23780 HOST_WIDE_INT cfa_fb_offset;
23782 struct function *fun = DECL_STRUCT_FUNCTION (decl);
23784 if (!crtl->has_bb_partition)
23786 dw_fde_ref fde = fun->fde;
23787 if (fde->dw_fde_begin)
23789 /* We have already generated the labels. */
23790 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
23791 fde->dw_fde_end, false);
23793 else
23795 /* Create start/end labels and add the range. */
23796 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
23797 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
23798 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
23799 current_function_funcdef_no);
23800 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
23801 current_function_funcdef_no);
23802 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
23803 false);
23806 #if VMS_DEBUGGING_INFO
23807 /* HP OpenVMS Industry Standard 64: DWARF Extensions
23808 Section 2.3 Prologue and Epilogue Attributes:
23809 When a breakpoint is set on entry to a function, it is generally
23810 desirable for execution to be suspended, not on the very first
23811 instruction of the function, but rather at a point after the
23812 function's frame has been set up, after any language defined local
23813 declaration processing has been completed, and before execution of
23814 the first statement of the function begins. Debuggers generally
23815 cannot properly determine where this point is. Similarly for a
23816 breakpoint set on exit from a function. The prologue and epilogue
23817 attributes allow a compiler to communicate the location(s) to use. */
23820 if (fde->dw_fde_vms_end_prologue)
23821 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
23822 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
23824 if (fde->dw_fde_vms_begin_epilogue)
23825 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
23826 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
23828 #endif
23831 else
23833 /* Generate pubnames entries for the split function code ranges. */
23834 dw_fde_ref fde = fun->fde;
23836 if (fde->dw_fde_second_begin)
23838 if (dwarf_version >= 3 || !dwarf_strict)
23840 /* We should use ranges for non-contiguous code section
23841 addresses. Use the actual code range for the initial
23842 section, since the HOT/COLD labels might precede an
23843 alignment offset. */
23844 bool range_list_added = false;
23845 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
23846 fde->dw_fde_end, &range_list_added,
23847 false);
23848 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
23849 fde->dw_fde_second_end,
23850 &range_list_added, false);
23851 if (range_list_added)
23852 add_ranges (NULL);
23854 else
23856 /* There is no real support in DW2 for this .. so we make
23857 a work-around. First, emit the pub name for the segment
23858 containing the function label. Then make and emit a
23859 simplified subprogram DIE for the second segment with the
23860 name pre-fixed by __hot/cold_sect_of_. We use the same
23861 linkage name for the second die so that gdb will find both
23862 sections when given "b foo". */
23863 const char *name = NULL;
23864 tree decl_name = DECL_NAME (decl);
23865 dw_die_ref seg_die;
23867 /* Do the 'primary' section. */
23868 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
23869 fde->dw_fde_end, false);
23871 /* Build a minimal DIE for the secondary section. */
23872 seg_die = new_die (DW_TAG_subprogram,
23873 subr_die->die_parent, decl);
23875 if (TREE_PUBLIC (decl))
23876 add_AT_flag (seg_die, DW_AT_external, 1);
23878 if (decl_name != NULL
23879 && IDENTIFIER_POINTER (decl_name) != NULL)
23881 name = dwarf2_name (decl, 1);
23882 if (! DECL_ARTIFICIAL (decl))
23883 add_src_coords_attributes (seg_die, decl);
23885 add_linkage_name (seg_die, decl);
23887 gcc_assert (name != NULL);
23888 add_pure_or_virtual_attribute (seg_die, decl);
23889 if (DECL_ARTIFICIAL (decl))
23890 add_AT_flag (seg_die, DW_AT_artificial, 1);
23892 name = concat ("__second_sect_of_", name, NULL);
23893 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
23894 fde->dw_fde_second_end, false);
23895 add_name_attribute (seg_die, name);
23896 if (want_pubnames ())
23897 add_pubname_string (name, seg_die);
23900 else
23901 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
23902 false);
23905 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
23907 /* We define the "frame base" as the function's CFA. This is more
23908 convenient for several reasons: (1) It's stable across the prologue
23909 and epilogue, which makes it better than just a frame pointer,
23910 (2) With dwarf3, there exists a one-byte encoding that allows us
23911 to reference the .debug_frame data by proxy, but failing that,
23912 (3) We can at least reuse the code inspection and interpretation
23913 code that determines the CFA position at various points in the
23914 function. */
23915 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
23917 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
23918 add_AT_loc (subr_die, DW_AT_frame_base, op);
23920 else
23922 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
23923 if (list->dw_loc_next)
23924 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
23925 else
23926 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
23929 /* Compute a displacement from the "steady-state frame pointer" to
23930 the CFA. The former is what all stack slots and argument slots
23931 will reference in the rtl; the latter is what we've told the
23932 debugger about. We'll need to adjust all frame_base references
23933 by this displacement. */
23934 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
23936 if (fun->static_chain_decl)
23938 /* DWARF requires here a location expression that computes the
23939 address of the enclosing subprogram's frame base. The machinery
23940 in tree-nested.cc is supposed to store this specific address in the
23941 last field of the FRAME record. */
23942 const tree frame_type
23943 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
23944 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
23946 tree fb_expr
23947 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
23948 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
23949 fb_expr, fb_decl, NULL_TREE);
23951 add_AT_location_description (subr_die, DW_AT_static_link,
23952 loc_list_from_tree (fb_expr, 0, NULL));
23955 resolve_variable_values ();
23958 /* Generate child dies for template parameters. */
23959 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
23960 gen_generic_params_dies (decl);
23962 /* Now output descriptions of the arguments for this function. This gets
23963 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
23964 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
23965 `...' at the end of the formal parameter list. In order to find out if
23966 there was a trailing ellipsis or not, we must instead look at the type
23967 associated with the FUNCTION_DECL. This will be a node of type
23968 FUNCTION_TYPE. If the chain of type nodes hanging off of this
23969 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
23970 an ellipsis at the end. */
23972 /* In the case where we are describing a mere function declaration, all we
23973 need to do here (and all we *can* do here) is to describe the *types* of
23974 its formal parameters. */
23975 if (debug_info_level <= DINFO_LEVEL_TERSE)
23977 else if (declaration)
23978 gen_formal_types_die (decl, subr_die);
23979 else
23981 /* Generate DIEs to represent all known formal parameters. */
23982 tree parm = DECL_ARGUMENTS (decl);
23983 tree generic_decl = early_dwarf
23984 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
23985 tree generic_decl_parm = generic_decl
23986 ? DECL_ARGUMENTS (generic_decl)
23987 : NULL;
23989 /* Now we want to walk the list of parameters of the function and
23990 emit their relevant DIEs.
23992 We consider the case of DECL being an instance of a generic function
23993 as well as it being a normal function.
23995 If DECL is an instance of a generic function we walk the
23996 parameters of the generic function declaration _and_ the parameters of
23997 DECL itself. This is useful because we want to emit specific DIEs for
23998 function parameter packs and those are declared as part of the
23999 generic function declaration. In that particular case,
24000 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
24001 That DIE has children DIEs representing the set of arguments
24002 of the pack. Note that the set of pack arguments can be empty.
24003 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
24004 children DIE.
24006 Otherwise, we just consider the parameters of DECL. */
24007 while (generic_decl_parm || parm)
24009 if (generic_decl_parm
24010 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
24011 gen_formal_parameter_pack_die (generic_decl_parm,
24012 parm, subr_die,
24013 &parm);
24014 else if (parm)
24016 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
24018 if (early_dwarf
24019 && parm == DECL_ARGUMENTS (decl)
24020 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
24021 && parm_die
24022 && (dwarf_version >= 3 || !dwarf_strict))
24023 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
24025 parm = DECL_CHAIN (parm);
24028 if (generic_decl_parm)
24029 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
24032 /* Decide whether we need an unspecified_parameters DIE at the end.
24033 There are 2 more cases to do this for: 1) the ansi ... declaration -
24034 this is detectable when the end of the arg list is not a
24035 void_type_node 2) an unprototyped function declaration (not a
24036 definition). This just means that we have no info about the
24037 parameters at all. */
24038 if (early_dwarf)
24040 if (prototype_p (TREE_TYPE (decl)))
24042 /* This is the prototyped case, check for.... */
24043 if (stdarg_p (TREE_TYPE (decl)))
24044 gen_unspecified_parameters_die (decl, subr_die);
24046 else if (DECL_INITIAL (decl) == NULL_TREE)
24047 gen_unspecified_parameters_die (decl, subr_die);
24049 else if ((subr_die != old_die || old_die_had_no_children)
24050 && prototype_p (TREE_TYPE (decl))
24051 && stdarg_p (TREE_TYPE (decl)))
24052 gen_unspecified_parameters_die (decl, subr_die);
24055 if (subr_die != old_die)
24056 /* Add the calling convention attribute if requested. */
24057 add_calling_convention_attribute (subr_die, decl);
24059 /* Output Dwarf info for all of the stuff within the body of the function
24060 (if it has one - it may be just a declaration).
24062 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
24063 a function. This BLOCK actually represents the outermost binding contour
24064 for the function, i.e. the contour in which the function's formal
24065 parameters and labels get declared. Curiously, it appears that the front
24066 end doesn't actually put the PARM_DECL nodes for the current function onto
24067 the BLOCK_VARS list for this outer scope, but are strung off of the
24068 DECL_ARGUMENTS list for the function instead.
24070 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
24071 the LABEL_DECL nodes for the function however, and we output DWARF info
24072 for those in decls_for_scope. Just within the `outer_scope' there will be
24073 a BLOCK node representing the function's outermost pair of curly braces,
24074 and any blocks used for the base and member initializers of a C++
24075 constructor function. */
24076 tree outer_scope = DECL_INITIAL (decl);
24077 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
24079 int call_site_note_count = 0;
24080 int tail_call_site_note_count = 0;
24082 /* Emit a DW_TAG_variable DIE for a named return value. */
24083 if (DECL_NAME (DECL_RESULT (decl)))
24084 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
24086 /* The first time through decls_for_scope we will generate the
24087 DIEs for the locals. The second time, we fill in the
24088 location info. */
24089 decls_for_scope (outer_scope, subr_die);
24091 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
24093 struct call_arg_loc_node *ca_loc;
24094 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
24096 dw_die_ref die = NULL;
24097 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
24098 rtx arg, next_arg;
24099 tree arg_decl = NULL_TREE;
24101 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
24102 ? XEXP (ca_loc->call_arg_loc_note, 0)
24103 : NULL_RTX);
24104 arg; arg = next_arg)
24106 dw_loc_descr_ref reg, val;
24107 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
24108 dw_die_ref cdie, tdie = NULL;
24110 next_arg = XEXP (arg, 1);
24111 if (REG_P (XEXP (XEXP (arg, 0), 0))
24112 && next_arg
24113 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
24114 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
24115 && REGNO (XEXP (XEXP (arg, 0), 0))
24116 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
24117 next_arg = XEXP (next_arg, 1);
24118 if (mode == VOIDmode)
24120 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
24121 if (mode == VOIDmode)
24122 mode = GET_MODE (XEXP (arg, 0));
24124 if (mode == VOIDmode || mode == BLKmode)
24125 continue;
24126 /* Get dynamic information about call target only if we
24127 have no static information: we cannot generate both
24128 DW_AT_call_origin and DW_AT_call_target
24129 attributes. */
24130 if (ca_loc->symbol_ref == NULL_RTX)
24132 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
24134 tloc = XEXP (XEXP (arg, 0), 1);
24135 continue;
24137 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
24138 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
24140 tlocc = XEXP (XEXP (arg, 0), 1);
24141 continue;
24144 reg = NULL;
24145 if (REG_P (XEXP (XEXP (arg, 0), 0)))
24146 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
24147 VAR_INIT_STATUS_INITIALIZED);
24148 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
24150 rtx mem = XEXP (XEXP (arg, 0), 0);
24151 reg = mem_loc_descriptor (XEXP (mem, 0),
24152 get_address_mode (mem),
24153 GET_MODE (mem),
24154 VAR_INIT_STATUS_INITIALIZED);
24156 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
24157 == DEBUG_PARAMETER_REF)
24159 tree tdecl
24160 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
24161 tdie = lookup_decl_die (tdecl);
24162 if (tdie == NULL)
24163 continue;
24164 arg_decl = tdecl;
24166 else
24167 continue;
24168 if (reg == NULL
24169 && GET_CODE (XEXP (XEXP (arg, 0), 0))
24170 != DEBUG_PARAMETER_REF)
24171 continue;
24172 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
24173 VOIDmode,
24174 VAR_INIT_STATUS_INITIALIZED);
24175 if (val == NULL)
24176 continue;
24177 if (die == NULL)
24178 die = gen_call_site_die (decl, subr_die, ca_loc);
24179 cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
24180 NULL_TREE);
24181 add_desc_attribute (cdie, arg_decl);
24182 if (reg != NULL)
24183 add_AT_loc (cdie, DW_AT_location, reg);
24184 else if (tdie != NULL)
24185 add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
24186 tdie);
24187 add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
24188 if (next_arg != XEXP (arg, 1))
24190 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
24191 if (mode == VOIDmode)
24192 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
24193 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
24194 0), 1),
24195 mode, VOIDmode,
24196 VAR_INIT_STATUS_INITIALIZED);
24197 if (val != NULL)
24198 add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
24199 val);
24202 if (die == NULL
24203 && (ca_loc->symbol_ref || tloc))
24204 die = gen_call_site_die (decl, subr_die, ca_loc);
24205 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
24207 dw_loc_descr_ref tval = NULL;
24209 if (tloc != NULL_RTX)
24210 tval = mem_loc_descriptor (tloc,
24211 GET_MODE (tloc) == VOIDmode
24212 ? Pmode : GET_MODE (tloc),
24213 VOIDmode,
24214 VAR_INIT_STATUS_INITIALIZED);
24215 if (tval)
24216 add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
24217 else if (tlocc != NULL_RTX)
24219 tval = mem_loc_descriptor (tlocc,
24220 GET_MODE (tlocc) == VOIDmode
24221 ? Pmode : GET_MODE (tlocc),
24222 VOIDmode,
24223 VAR_INIT_STATUS_INITIALIZED);
24224 if (tval)
24225 add_AT_loc (die,
24226 dwarf_AT (DW_AT_call_target_clobbered),
24227 tval);
24230 if (die != NULL)
24232 call_site_note_count++;
24233 if (ca_loc->tail_call_p)
24234 tail_call_site_note_count++;
24238 call_arg_locations = NULL;
24239 call_arg_loc_last = NULL;
24240 if (tail_call_site_count >= 0
24241 && tail_call_site_count == tail_call_site_note_count
24242 && (!dwarf_strict || dwarf_version >= 5))
24244 if (call_site_count >= 0
24245 && call_site_count == call_site_note_count)
24246 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
24247 else
24248 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
24250 call_site_count = -1;
24251 tail_call_site_count = -1;
24254 /* Mark used types after we have created DIEs for the functions scopes. */
24255 premark_used_types (DECL_STRUCT_FUNCTION (decl));
24258 /* Returns a hash value for X (which really is a die_struct). */
24260 hashval_t
24261 block_die_hasher::hash (die_struct *d)
24263 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
24266 /* Return true if decl_id and die_parent of die_struct X is the same
24267 as decl_id and die_parent of die_struct Y. */
24269 bool
24270 block_die_hasher::equal (die_struct *x, die_struct *y)
24272 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
24275 /* Hold information about markers for inlined entry points. */
24276 struct GTY ((for_user)) inline_entry_data
24278 /* The block that's the inlined_function_outer_scope for an inlined
24279 function. */
24280 tree block;
24282 /* The label at the inlined entry point. */
24283 const char *label_pfx;
24284 unsigned int label_num;
24286 /* The view number to be used as the inlined entry point. */
24287 var_loc_view view;
24290 struct inline_entry_data_hasher : ggc_ptr_hash <inline_entry_data>
24292 typedef tree compare_type;
24293 static inline hashval_t hash (const inline_entry_data *);
24294 static inline bool equal (const inline_entry_data *, const_tree);
24297 /* Hash table routines for inline_entry_data. */
24299 inline hashval_t
24300 inline_entry_data_hasher::hash (const inline_entry_data *data)
24302 return htab_hash_pointer (data->block);
24305 inline bool
24306 inline_entry_data_hasher::equal (const inline_entry_data *data,
24307 const_tree block)
24309 return data->block == block;
24312 /* Inlined entry points pending DIE creation in this compilation unit. */
24314 static GTY(()) hash_table<inline_entry_data_hasher> *inline_entry_data_table;
24317 /* Return TRUE if DECL, which may have been previously generated as
24318 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
24319 true if decl (or its origin) is either an extern declaration or a
24320 class/namespace scoped declaration.
24322 The declare_in_namespace support causes us to get two DIEs for one
24323 variable, both of which are declarations. We want to avoid
24324 considering one to be a specification, so we must test for
24325 DECLARATION and DW_AT_declaration. */
24326 static inline bool
24327 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
24329 return (old_die && TREE_STATIC (decl) && !declaration
24330 && get_AT_flag (old_die, DW_AT_declaration) == 1);
24333 /* Return true if DECL is a local static. */
24335 static inline bool
24336 local_function_static (tree decl)
24338 gcc_assert (VAR_P (decl));
24339 return TREE_STATIC (decl)
24340 && DECL_CONTEXT (decl)
24341 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
24344 /* Return true iff DECL overrides (presumably completes) the type of
24345 OLD_DIE within CONTEXT_DIE. */
24347 static bool
24348 override_type_for_decl_p (tree decl, dw_die_ref old_die,
24349 dw_die_ref context_die)
24351 tree type = TREE_TYPE (decl);
24352 int cv_quals;
24354 if (decl_by_reference_p (decl))
24356 type = TREE_TYPE (type);
24357 cv_quals = TYPE_UNQUALIFIED;
24359 else
24360 cv_quals = decl_quals (decl);
24362 dw_die_ref type_die = modified_type_die (type,
24363 cv_quals | TYPE_QUALS (type),
24364 false,
24365 context_die);
24367 dw_die_ref old_type_die = get_AT_ref (old_die, DW_AT_type);
24369 return type_die != old_type_die;
24372 /* Generate a DIE to represent a declared data object.
24373 Either DECL or ORIGIN must be non-null. */
24375 static void
24376 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
24378 HOST_WIDE_INT off = 0;
24379 tree com_decl;
24380 tree decl_or_origin = decl ? decl : origin;
24381 tree ultimate_origin;
24382 dw_die_ref var_die;
24383 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
24384 bool declaration = (DECL_EXTERNAL (decl_or_origin)
24385 || class_or_namespace_scope_p (context_die));
24386 bool specialization_p = false;
24387 bool no_linkage_name = false;
24389 /* While C++ inline static data members have definitions inside of the
24390 class, force the first DIE to be a declaration, then let gen_member_die
24391 reparent it to the class context and call gen_variable_die again
24392 to create the outside of the class DIE for the definition. */
24393 if (!declaration
24394 && old_die == NULL
24395 && decl
24396 && DECL_CONTEXT (decl)
24397 && TYPE_P (DECL_CONTEXT (decl))
24398 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
24400 declaration = true;
24401 if (dwarf_version < 5)
24402 no_linkage_name = true;
24405 ultimate_origin = decl_ultimate_origin (decl_or_origin);
24406 if (decl || ultimate_origin)
24407 origin = ultimate_origin;
24408 com_decl = fortran_common (decl_or_origin, &off);
24410 /* Symbol in common gets emitted as a child of the common block, in the form
24411 of a data member. */
24412 if (com_decl)
24414 dw_die_ref com_die;
24415 dw_loc_list_ref loc = NULL;
24416 die_node com_die_arg;
24418 var_die = lookup_decl_die (decl_or_origin);
24419 if (var_die)
24421 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
24423 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
24424 if (loc)
24426 if (off)
24428 /* Optimize the common case. */
24429 if (single_element_loc_list_p (loc)
24430 && loc->expr->dw_loc_opc == DW_OP_addr
24431 && loc->expr->dw_loc_next == NULL
24432 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
24433 == SYMBOL_REF)
24435 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
24436 loc->expr->dw_loc_oprnd1.v.val_addr
24437 = plus_constant (GET_MODE (x), x , off);
24439 else
24440 loc_list_plus_const (loc, off);
24442 add_AT_location_description (var_die, DW_AT_location, loc);
24443 remove_AT (var_die, DW_AT_declaration);
24446 return;
24449 if (common_block_die_table == NULL)
24450 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
24452 com_die_arg.decl_id = DECL_UID (com_decl);
24453 com_die_arg.die_parent = context_die;
24454 com_die = common_block_die_table->find (&com_die_arg);
24455 if (! early_dwarf)
24456 loc = loc_list_from_tree (com_decl, 2, NULL);
24457 if (com_die == NULL)
24459 const char *cnam
24460 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
24461 die_node **slot;
24463 com_die = new_die (DW_TAG_common_block, context_die, decl);
24464 add_name_and_src_coords_attributes (com_die, com_decl);
24465 if (loc)
24467 add_AT_location_description (com_die, DW_AT_location, loc);
24468 /* Avoid sharing the same loc descriptor between
24469 DW_TAG_common_block and DW_TAG_variable. */
24470 loc = loc_list_from_tree (com_decl, 2, NULL);
24472 else if (DECL_EXTERNAL (decl_or_origin))
24473 add_AT_flag (com_die, DW_AT_declaration, 1);
24474 if (want_pubnames ())
24475 add_pubname_string (cnam, com_die); /* ??? needed? */
24476 com_die->decl_id = DECL_UID (com_decl);
24477 slot = common_block_die_table->find_slot (com_die, INSERT);
24478 *slot = com_die;
24480 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
24482 add_AT_location_description (com_die, DW_AT_location, loc);
24483 loc = loc_list_from_tree (com_decl, 2, NULL);
24484 remove_AT (com_die, DW_AT_declaration);
24486 var_die = new_die (DW_TAG_variable, com_die, decl);
24487 add_name_and_src_coords_attributes (var_die, decl_or_origin);
24488 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
24489 decl_quals (decl_or_origin), false,
24490 context_die);
24491 add_alignment_attribute (var_die, decl);
24492 add_AT_flag (var_die, DW_AT_external, 1);
24493 if (loc)
24495 if (off)
24497 /* Optimize the common case. */
24498 if (single_element_loc_list_p (loc)
24499 && loc->expr->dw_loc_opc == DW_OP_addr
24500 && loc->expr->dw_loc_next == NULL
24501 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
24503 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
24504 loc->expr->dw_loc_oprnd1.v.val_addr
24505 = plus_constant (GET_MODE (x), x, off);
24507 else
24508 loc_list_plus_const (loc, off);
24510 add_AT_location_description (var_die, DW_AT_location, loc);
24512 else if (DECL_EXTERNAL (decl_or_origin))
24513 add_AT_flag (var_die, DW_AT_declaration, 1);
24514 if (decl)
24515 equate_decl_number_to_die (decl, var_die);
24516 return;
24519 if (old_die)
24521 if (declaration)
24523 /* A declaration that has been previously dumped, needs no
24524 further annotations, since it doesn't need location on
24525 the second pass. */
24526 return;
24528 else if (decl_will_get_specification_p (old_die, decl, declaration)
24529 && !get_AT (old_die, DW_AT_specification))
24531 /* Fall-thru so we can make a new variable die along with a
24532 DW_AT_specification. */
24534 else if (origin && old_die->die_parent != context_die)
24536 /* If we will be creating an inlined instance, we need a
24537 new DIE that will get annotated with
24538 DW_AT_abstract_origin. */
24539 gcc_assert (!DECL_ABSTRACT_P (decl));
24541 else
24543 /* If a DIE was dumped early, it still needs location info.
24544 Skip to where we fill the location bits. */
24545 var_die = old_die;
24547 /* ??? In LTRANS we cannot annotate early created variably
24548 modified type DIEs without copying them and adjusting all
24549 references to them. Thus we dumped them again. Also add a
24550 reference to them but beware of -g0 compile and -g link
24551 in which case the reference will be already present. */
24552 tree type = TREE_TYPE (decl_or_origin);
24553 if (in_lto_p
24554 && ! get_AT (var_die, DW_AT_type)
24555 && variably_modified_type_p
24556 (type, decl_function_context (decl_or_origin)))
24558 if (decl_by_reference_p (decl_or_origin))
24559 add_type_attribute (var_die, TREE_TYPE (type),
24560 TYPE_UNQUALIFIED, false, context_die);
24561 else
24562 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
24563 false, context_die);
24566 goto gen_variable_die_location;
24570 /* For static data members, the declaration in the class is supposed
24571 to have DW_TAG_member tag in DWARF{3,4} and we emit it for compatibility
24572 also in DWARF2; the specification should still be DW_TAG_variable
24573 referencing the DW_TAG_member DIE. */
24574 if (declaration && class_scope_p (context_die) && dwarf_version < 5)
24575 var_die = new_die (DW_TAG_member, context_die, decl);
24576 else
24577 var_die = new_die (DW_TAG_variable, context_die, decl);
24579 if (origin != NULL)
24580 add_abstract_origin_attribute (var_die, origin);
24582 /* Loop unrolling can create multiple blocks that refer to the same
24583 static variable, so we must test for the DW_AT_declaration flag.
24585 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
24586 copy decls and set the DECL_ABSTRACT_P flag on them instead of
24587 sharing them.
24589 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
24590 else if (decl_will_get_specification_p (old_die, decl, declaration))
24592 /* This is a definition of a C++ class level static. */
24593 add_AT_specification (var_die, old_die);
24594 specialization_p = true;
24595 if (DECL_NAME (decl))
24597 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
24598 struct dwarf_file_data * file_index = lookup_filename (s.file);
24600 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
24601 add_AT_file (var_die, DW_AT_decl_file, file_index);
24603 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
24604 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
24606 if (debug_column_info
24607 && s.column
24608 && (get_AT_unsigned (old_die, DW_AT_decl_column)
24609 != (unsigned) s.column))
24610 add_AT_unsigned (var_die, DW_AT_decl_column, s.column);
24612 if (old_die->die_tag == DW_TAG_member)
24613 add_linkage_name (var_die, decl);
24616 else
24617 add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
24619 if ((origin == NULL && !specialization_p)
24620 || (origin != NULL
24621 && !DECL_ABSTRACT_P (decl_or_origin)
24622 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
24623 decl_function_context
24624 (decl_or_origin)))
24625 || (old_die && specialization_p
24626 && override_type_for_decl_p (decl_or_origin, old_die, context_die)))
24628 tree type = TREE_TYPE (decl_or_origin);
24630 if (decl_by_reference_p (decl_or_origin))
24631 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
24632 context_die);
24633 else
24634 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
24635 context_die);
24638 if (origin == NULL && !specialization_p)
24640 if (TREE_PUBLIC (decl))
24641 add_AT_flag (var_die, DW_AT_external, 1);
24643 if (DECL_ARTIFICIAL (decl))
24644 add_AT_flag (var_die, DW_AT_artificial, 1);
24646 add_alignment_attribute (var_die, decl);
24648 add_accessibility_attribute (var_die, decl);
24651 if (declaration)
24652 add_AT_flag (var_die, DW_AT_declaration, 1);
24654 if (decl && (DECL_ABSTRACT_P (decl)
24655 || !old_die || is_declaration_die (old_die)))
24656 equate_decl_number_to_die (decl, var_die);
24658 gen_variable_die_location:
24659 if (! declaration
24660 && (! DECL_ABSTRACT_P (decl_or_origin)
24661 /* Local static vars are shared between all clones/inlines,
24662 so emit DW_AT_location on the abstract DIE if DECL_RTL is
24663 already set. */
24664 || (VAR_P (decl_or_origin)
24665 && TREE_STATIC (decl_or_origin)
24666 && DECL_RTL_SET_P (decl_or_origin))))
24668 if (early_dwarf)
24670 add_pubname (decl_or_origin, var_die);
24671 /* For global register variables, emit DW_AT_location if possible
24672 already during early_dwarf, as late_global_decl won't be usually
24673 called. */
24674 if (DECL_HARD_REGISTER (decl_or_origin)
24675 && TREE_STATIC (decl_or_origin)
24676 && !decl_by_reference_p (decl_or_origin)
24677 && !get_AT (var_die, DW_AT_location)
24678 && !get_AT (var_die, DW_AT_const_value)
24679 && DECL_RTL_SET_P (decl_or_origin)
24680 && REG_P (DECL_RTL (decl_or_origin)))
24682 dw_loc_descr_ref descr
24683 = reg_loc_descriptor (DECL_RTL (decl_or_origin),
24684 VAR_INIT_STATUS_INITIALIZED);
24685 if (descr)
24686 add_AT_loc (var_die, DW_AT_location, descr);
24689 else
24690 add_location_or_const_value_attribute (var_die, decl_or_origin,
24691 decl == NULL);
24693 else
24694 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
24696 if ((dwarf_version >= 4 || !dwarf_strict)
24697 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
24698 DW_AT_const_expr) == 1
24699 && !get_AT (var_die, DW_AT_const_expr)
24700 && !specialization_p)
24701 add_AT_flag (var_die, DW_AT_const_expr, 1);
24703 if (!dwarf_strict)
24705 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
24706 DW_AT_inline);
24707 if (inl != -1
24708 && !get_AT (var_die, DW_AT_inline)
24709 && !specialization_p)
24710 add_AT_unsigned (var_die, DW_AT_inline, inl);
24714 /* Generate a DIE to represent a named constant. */
24716 static void
24717 gen_const_die (tree decl, dw_die_ref context_die)
24719 dw_die_ref const_die;
24720 tree type = TREE_TYPE (decl);
24722 const_die = lookup_decl_die (decl);
24723 if (const_die)
24724 return;
24726 const_die = new_die (DW_TAG_constant, context_die, decl);
24727 equate_decl_number_to_die (decl, const_die);
24728 add_name_and_src_coords_attributes (const_die, decl);
24729 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
24730 if (TREE_PUBLIC (decl))
24731 add_AT_flag (const_die, DW_AT_external, 1);
24732 if (DECL_ARTIFICIAL (decl))
24733 add_AT_flag (const_die, DW_AT_artificial, 1);
24734 tree_add_const_value_attribute_for_decl (const_die, decl);
24737 /* Generate a DIE to represent a label identifier. */
24739 static void
24740 gen_label_die (tree decl, dw_die_ref context_die)
24742 tree origin = decl_ultimate_origin (decl);
24743 dw_die_ref lbl_die = lookup_decl_die (decl);
24744 rtx insn;
24745 char label[MAX_ARTIFICIAL_LABEL_BYTES];
24747 if (!lbl_die)
24749 lbl_die = new_die (DW_TAG_label, context_die, decl);
24750 equate_decl_number_to_die (decl, lbl_die);
24752 if (origin != NULL)
24753 add_abstract_origin_attribute (lbl_die, origin);
24754 else
24755 add_name_and_src_coords_attributes (lbl_die, decl);
24758 if (DECL_ABSTRACT_P (decl))
24759 equate_decl_number_to_die (decl, lbl_die);
24760 else if (! early_dwarf)
24762 insn = DECL_RTL_IF_SET (decl);
24764 /* Deleted labels are programmer specified labels which have been
24765 eliminated because of various optimizations. We still emit them
24766 here so that it is possible to put breakpoints on them. */
24767 if (insn
24768 && (LABEL_P (insn)
24769 || ((NOTE_P (insn)
24770 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
24772 /* When optimization is enabled (via -O) some parts of the compiler
24773 (e.g. jump.cc and cse.cc) may try to delete CODE_LABEL insns which
24774 represent source-level labels which were explicitly declared by
24775 the user. This really shouldn't be happening though, so catch
24776 it if it ever does happen. */
24777 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
24779 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
24780 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
24782 else if (insn
24783 && NOTE_P (insn)
24784 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
24785 && CODE_LABEL_NUMBER (insn) != -1)
24787 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
24788 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
24793 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
24794 attributes to the DIE for a block STMT, to describe where the inlined
24795 function was called from. This is similar to add_src_coords_attributes. */
24797 static inline void
24798 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
24800 /* We can end up with BUILTINS_LOCATION here. */
24801 if (RESERVED_LOCATION_P (BLOCK_SOURCE_LOCATION (stmt)))
24802 return;
24804 location_t locus = BLOCK_SOURCE_LOCATION (stmt);
24805 expanded_location s = expand_location (locus);
24807 if (dwarf_version >= 3 || !dwarf_strict)
24809 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
24810 add_AT_unsigned (die, DW_AT_call_line, s.line);
24811 if (debug_column_info && s.column)
24812 add_AT_unsigned (die, DW_AT_call_column, s.column);
24813 unsigned discr = get_discriminator_from_loc (locus);
24814 if (discr != 0)
24815 add_AT_unsigned (die, DW_AT_GNU_discriminator, discr);
24820 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
24821 Add low_pc and high_pc attributes to the DIE for a block STMT. */
24823 static inline void
24824 add_high_low_attributes (tree stmt, dw_die_ref die)
24826 char label[MAX_ARTIFICIAL_LABEL_BYTES];
24828 if (inline_entry_data **iedp
24829 = !inline_entry_data_table ? NULL
24830 : inline_entry_data_table->find_slot_with_hash (stmt,
24831 htab_hash_pointer (stmt),
24832 NO_INSERT))
24834 inline_entry_data *ied = *iedp;
24835 gcc_assert (MAY_HAVE_DEBUG_MARKER_INSNS);
24836 gcc_assert (debug_inline_points);
24837 gcc_assert (inlined_function_outer_scope_p (stmt));
24839 ASM_GENERATE_INTERNAL_LABEL (label, ied->label_pfx, ied->label_num);
24840 add_AT_lbl_id (die, DW_AT_entry_pc, label);
24842 if (debug_variable_location_views && !ZERO_VIEW_P (ied->view)
24843 && !dwarf_strict)
24845 if (!output_asm_line_debug_info ())
24846 add_AT_unsigned (die, DW_AT_GNU_entry_view, ied->view);
24847 else
24849 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", ied->view);
24850 /* FIXME: this will resolve to a small number. Could we
24851 possibly emit smaller data? Ideally we'd emit a
24852 uleb128, but that would make the size of DIEs
24853 impossible for the compiler to compute, since it's
24854 the assembler that computes the value of the view
24855 label in this case. Ideally, we'd have a single form
24856 encompassing both the address and the view, and
24857 indirecting them through a table might make things
24858 easier, but even that would be more wasteful,
24859 space-wise, than what we have now. */
24860 add_AT_symview (die, DW_AT_GNU_entry_view, label);
24864 inline_entry_data_table->clear_slot (iedp);
24867 if (BLOCK_FRAGMENT_CHAIN (stmt)
24868 && (dwarf_version >= 3 || !dwarf_strict))
24870 tree chain, superblock = NULL_TREE;
24871 dw_die_ref pdie;
24872 dw_attr_node *attr = NULL;
24874 if (!debug_inline_points && inlined_function_outer_scope_p (stmt))
24876 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
24877 BLOCK_NUMBER (stmt));
24878 add_AT_lbl_id (die, DW_AT_entry_pc, label);
24881 /* Optimize duplicate .debug_ranges lists or even tails of
24882 lists. If this BLOCK has same ranges as its supercontext,
24883 lookup DW_AT_ranges attribute in the supercontext (and
24884 recursively so), verify that the ranges_table contains the
24885 right values and use it instead of adding a new .debug_range. */
24886 for (chain = stmt, pdie = die;
24887 BLOCK_SAME_RANGE (chain);
24888 chain = BLOCK_SUPERCONTEXT (chain))
24890 dw_attr_node *new_attr;
24892 pdie = pdie->die_parent;
24893 if (pdie == NULL)
24894 break;
24895 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
24896 break;
24897 new_attr = get_AT (pdie, DW_AT_ranges);
24898 if (new_attr == NULL
24899 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
24900 break;
24901 attr = new_attr;
24902 superblock = BLOCK_SUPERCONTEXT (chain);
24904 if (attr != NULL
24905 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
24906 == (int)BLOCK_NUMBER (superblock))
24907 && BLOCK_FRAGMENT_CHAIN (superblock))
24909 unsigned long off = attr->dw_attr_val.v.val_offset;
24910 unsigned long supercnt = 0, thiscnt = 0;
24911 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
24912 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
24914 ++supercnt;
24915 gcc_checking_assert ((*ranges_table)[off + supercnt].num
24916 == (int)BLOCK_NUMBER (chain));
24918 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
24919 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
24920 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
24921 ++thiscnt;
24922 gcc_assert (supercnt >= thiscnt);
24923 add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
24924 false);
24925 note_rnglist_head (off + supercnt - thiscnt);
24926 return;
24929 unsigned int offset = add_ranges (stmt, true);
24930 add_AT_range_list (die, DW_AT_ranges, offset, false);
24931 note_rnglist_head (offset);
24933 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
24934 chain = BLOCK_FRAGMENT_CHAIN (stmt);
24937 add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
24938 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
24939 chain = BLOCK_FRAGMENT_CHAIN (chain);
24941 while (chain);
24942 add_ranges (NULL);
24944 else
24946 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
24947 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
24948 BLOCK_NUMBER (stmt));
24949 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
24950 BLOCK_NUMBER (stmt));
24951 add_AT_low_high_pc (die, label, label_high, false);
24955 /* Generate a DIE for a lexical block. */
24957 static void
24958 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
24960 dw_die_ref old_die = lookup_block_die (stmt);
24961 dw_die_ref stmt_die = NULL;
24962 if (!old_die)
24964 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
24965 equate_block_to_die (stmt, stmt_die);
24968 if (BLOCK_ABSTRACT_ORIGIN (stmt))
24970 /* If this is an inlined or conrecte instance, create a new lexical
24971 die for anything below to attach DW_AT_abstract_origin to. */
24972 if (old_die)
24973 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
24975 tree origin = block_ultimate_origin (stmt);
24976 if (origin != NULL_TREE && (origin != stmt || old_die))
24977 add_abstract_origin_attribute (stmt_die, origin);
24979 old_die = NULL;
24982 if (old_die)
24983 stmt_die = old_die;
24985 /* A non abstract block whose blocks have already been reordered
24986 should have the instruction range for this block. If so, set the
24987 high/low attributes. */
24988 if (!early_dwarf && TREE_ASM_WRITTEN (stmt))
24990 gcc_assert (stmt_die);
24991 add_high_low_attributes (stmt, stmt_die);
24994 decls_for_scope (stmt, stmt_die);
24997 /* Generate a DIE for an inlined subprogram. */
24999 static void
25000 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
25002 tree decl = block_ultimate_origin (stmt);
25004 /* Make sure any inlined functions are known to be inlineable. */
25005 gcc_checking_assert (DECL_ABSTRACT_P (decl)
25006 || cgraph_function_possibly_inlined_p (decl));
25008 dw_die_ref subr_die = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
25010 if (call_arg_locations || debug_inline_points)
25011 equate_block_to_die (stmt, subr_die);
25012 add_abstract_origin_attribute (subr_die, decl);
25013 if (TREE_ASM_WRITTEN (stmt))
25014 add_high_low_attributes (stmt, subr_die);
25015 add_call_src_coords_attributes (stmt, subr_die);
25017 /* The inliner creates an extra BLOCK for the parameter setup,
25018 we want to merge that with the actual outermost BLOCK of the
25019 inlined function to avoid duplicate locals in consumers.
25020 Do that by doing the recursion to subblocks on the single subblock
25021 of STMT. */
25022 bool unwrap_one = false;
25023 if (BLOCK_SUBBLOCKS (stmt) && !BLOCK_CHAIN (BLOCK_SUBBLOCKS (stmt)))
25025 tree origin = block_ultimate_origin (BLOCK_SUBBLOCKS (stmt));
25026 if (origin
25027 && TREE_CODE (origin) == BLOCK
25028 && BLOCK_SUPERCONTEXT (origin) == decl)
25029 unwrap_one = true;
25031 decls_for_scope (stmt, subr_die, !unwrap_one);
25032 if (unwrap_one)
25033 decls_for_scope (BLOCK_SUBBLOCKS (stmt), subr_die);
25036 /* Generate a DIE for a field in a record, or structure. CTX is required: see
25037 the comment for VLR_CONTEXT. */
25039 static void
25040 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
25042 dw_die_ref decl_die;
25044 if (TREE_TYPE (decl) == error_mark_node)
25045 return;
25047 decl_die = new_die (DW_TAG_member, context_die, decl);
25048 add_name_and_src_coords_attributes (decl_die, decl);
25049 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
25050 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
25051 context_die);
25053 if (DECL_BIT_FIELD_TYPE (decl))
25055 add_byte_size_attribute (decl_die, decl);
25056 add_bit_size_attribute (decl_die, decl);
25057 add_bit_offset_attribute (decl_die, decl);
25060 add_alignment_attribute (decl_die, decl);
25062 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
25063 add_data_member_location_attribute (decl_die, decl, ctx);
25065 if (DECL_ARTIFICIAL (decl))
25066 add_AT_flag (decl_die, DW_AT_artificial, 1);
25068 add_accessibility_attribute (decl_die, decl);
25070 /* Equate decl number to die, so that we can look up this decl later on. */
25071 equate_decl_number_to_die (decl, decl_die);
25074 /* Generate a DIE for a pointer to a member type. TYPE can be an
25075 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
25076 pointer to member function. */
25078 static void
25079 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
25081 if (lookup_type_die (type))
25082 return;
25084 dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
25085 scope_die_for (type, context_die), type);
25087 equate_type_number_to_die (type, ptr_die);
25088 add_AT_die_ref (ptr_die, DW_AT_containing_type,
25089 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
25090 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
25091 context_die);
25092 add_alignment_attribute (ptr_die, type);
25094 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
25095 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
25097 dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
25098 add_AT_loc (ptr_die, DW_AT_use_location, op);
25102 static char *producer_string;
25104 /* Given a C and/or C++ language/version string return the "highest".
25105 C++ is assumed to be "higher" than C in this case. Used for merging
25106 LTO translation unit languages. */
25107 static const char *
25108 highest_c_language (const char *lang1, const char *lang2)
25110 if (strcmp ("GNU C++26", lang1) == 0 || strcmp ("GNU C++26", lang2) == 0)
25111 return "GNU C++26";
25112 if (strcmp ("GNU C++23", lang1) == 0 || strcmp ("GNU C++23", lang2) == 0)
25113 return "GNU C++23";
25114 if (strcmp ("GNU C++20", lang1) == 0 || strcmp ("GNU C++20", lang2) == 0)
25115 return "GNU C++20";
25116 if (strcmp ("GNU C++17", lang1) == 0 || strcmp ("GNU C++17", lang2) == 0)
25117 return "GNU C++17";
25118 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
25119 return "GNU C++14";
25120 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
25121 return "GNU C++11";
25122 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
25123 return "GNU C++98";
25125 if (strcmp ("GNU C2X", lang1) == 0 || strcmp ("GNU C2X", lang2) == 0)
25126 return "GNU C2X";
25127 if (strcmp ("GNU C17", lang1) == 0 || strcmp ("GNU C17", lang2) == 0)
25128 return "GNU C17";
25129 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
25130 return "GNU C11";
25131 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
25132 return "GNU C99";
25133 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
25134 return "GNU C89";
25136 gcc_unreachable ();
25140 /* Generate the DIE for the compilation unit. */
25142 static dw_die_ref
25143 gen_compile_unit_die (const char *filename)
25145 dw_die_ref die;
25146 const char *language_string = lang_hooks.name;
25147 int language;
25149 die = new_die (DW_TAG_compile_unit, NULL, NULL);
25151 if (filename)
25153 add_filename_attribute (die, filename);
25154 /* Don't add cwd for <built-in>. */
25155 if (filename[0] != '<')
25156 add_comp_dir_attribute (die);
25159 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
25161 /* If our producer is LTO try to figure out a common language to use
25162 from the global list of translation units. */
25163 if (strcmp (language_string, "GNU GIMPLE") == 0)
25165 unsigned i;
25166 tree t;
25167 const char *common_lang = NULL;
25169 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
25171 if (!TRANSLATION_UNIT_LANGUAGE (t))
25172 continue;
25173 if (!common_lang)
25174 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
25175 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
25177 else if (startswith (common_lang, "GNU C")
25178 && startswith (TRANSLATION_UNIT_LANGUAGE (t), "GNU C"))
25179 /* Mixing C and C++ is ok, use C++ in that case. */
25180 common_lang = highest_c_language (common_lang,
25181 TRANSLATION_UNIT_LANGUAGE (t));
25182 else
25184 /* Fall back to C. */
25185 common_lang = NULL;
25186 break;
25190 if (common_lang)
25191 language_string = common_lang;
25194 language = DW_LANG_C;
25195 if (startswith (language_string, "GNU C")
25196 && ISDIGIT (language_string[5]))
25198 language = DW_LANG_C89;
25199 if (dwarf_version >= 3 || !dwarf_strict)
25201 if (strcmp (language_string, "GNU C89") != 0)
25202 language = DW_LANG_C99;
25204 if (dwarf_version >= 5 /* || !dwarf_strict */)
25205 if (strcmp (language_string, "GNU C11") == 0
25206 || strcmp (language_string, "GNU C17") == 0
25207 || strcmp (language_string, "GNU C2X") == 0)
25208 language = DW_LANG_C11;
25211 else if (startswith (language_string, "GNU C++"))
25213 language = DW_LANG_C_plus_plus;
25214 if (dwarf_version >= 5 /* || !dwarf_strict */)
25216 if (strcmp (language_string, "GNU C++11") == 0)
25217 language = DW_LANG_C_plus_plus_11;
25218 else if (strcmp (language_string, "GNU C++14") == 0)
25219 language = DW_LANG_C_plus_plus_14;
25220 else if (strcmp (language_string, "GNU C++17") == 0
25221 || strcmp (language_string, "GNU C++20") == 0
25222 || strcmp (language_string, "GNU C++23") == 0
25223 || strcmp (language_string, "GNU C++26") == 0)
25224 /* For now. */
25225 language = DW_LANG_C_plus_plus_14;
25228 else if (strcmp (language_string, "GNU F77") == 0)
25229 language = DW_LANG_Fortran77;
25230 else if (strcmp (language_string, "GNU Modula-2") == 0)
25231 language = DW_LANG_Modula2;
25232 else if (dwarf_version >= 3 || !dwarf_strict)
25234 if (strcmp (language_string, "GNU Ada") == 0)
25235 language = DW_LANG_Ada95;
25236 else if (startswith (language_string, "GNU Fortran"))
25238 language = DW_LANG_Fortran95;
25239 if (dwarf_version >= 5 /* || !dwarf_strict */)
25241 if (strcmp (language_string, "GNU Fortran2003") == 0)
25242 language = DW_LANG_Fortran03;
25243 else if (strcmp (language_string, "GNU Fortran2008") == 0)
25244 language = DW_LANG_Fortran08;
25247 else if (strcmp (language_string, "GNU Objective-C") == 0)
25248 language = DW_LANG_ObjC;
25249 else if (strcmp (language_string, "GNU Objective-C++") == 0)
25250 language = DW_LANG_ObjC_plus_plus;
25251 else if (strcmp (language_string, "GNU D") == 0)
25252 language = DW_LANG_D;
25253 else if (dwarf_version >= 5 || !dwarf_strict)
25255 if (strcmp (language_string, "GNU Go") == 0)
25256 language = DW_LANG_Go;
25257 else if (strcmp (language_string, "GNU Rust") == 0)
25258 language = DW_LANG_Rust;
25261 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
25262 else if (startswith (language_string, "GNU Fortran"))
25263 language = DW_LANG_Fortran90;
25264 /* Likewise for Ada. */
25265 else if (strcmp (language_string, "GNU Ada") == 0)
25266 language = DW_LANG_Ada83;
25268 add_AT_unsigned (die, DW_AT_language, language);
25270 switch (language)
25272 case DW_LANG_Fortran77:
25273 case DW_LANG_Fortran90:
25274 case DW_LANG_Fortran95:
25275 case DW_LANG_Fortran03:
25276 case DW_LANG_Fortran08:
25277 /* Fortran has case insensitive identifiers and the front-end
25278 lowercases everything. */
25279 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
25280 break;
25281 default:
25282 /* The default DW_ID_case_sensitive doesn't need to be specified. */
25283 break;
25285 return die;
25288 /* Generate the DIE for a base class. */
25290 static void
25291 gen_inheritance_die (tree binfo, tree access, tree type,
25292 dw_die_ref context_die)
25294 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
25295 struct vlr_context ctx = { type, NULL };
25297 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
25298 context_die);
25299 add_data_member_location_attribute (die, binfo, &ctx);
25301 if (BINFO_VIRTUAL_P (binfo))
25302 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
25304 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
25305 children, otherwise the default is DW_ACCESS_public. In DWARF2
25306 the default has always been DW_ACCESS_private. */
25307 if (access == access_public_node)
25309 if (dwarf_version == 2
25310 || context_die->die_tag == DW_TAG_class_type)
25311 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
25313 else if (access == access_protected_node)
25314 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
25315 else if (dwarf_version > 2
25316 && context_die->die_tag != DW_TAG_class_type)
25317 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
25320 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
25321 structure. */
25323 static bool
25324 is_variant_part (tree decl)
25326 return (TREE_CODE (decl) == FIELD_DECL
25327 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
25330 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
25331 return the FIELD_DECL. Return NULL_TREE otherwise. */
25333 static tree
25334 analyze_discr_in_predicate (tree operand, tree struct_type)
25336 while (CONVERT_EXPR_P (operand))
25337 operand = TREE_OPERAND (operand, 0);
25339 /* Match field access to members of struct_type only. */
25340 if (TREE_CODE (operand) == COMPONENT_REF
25341 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
25342 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
25343 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
25344 return TREE_OPERAND (operand, 1);
25345 else
25346 return NULL_TREE;
25349 /* Check that SRC is a constant integer that can be represented as a native
25350 integer constant (either signed or unsigned). If so, store it into DEST and
25351 return true. Return false otherwise. */
25353 static bool
25354 get_discr_value (tree src, dw_discr_value *dest)
25356 tree discr_type = TREE_TYPE (src);
25358 if (lang_hooks.types.get_debug_type)
25360 tree debug_type = lang_hooks.types.get_debug_type (discr_type);
25361 if (debug_type != NULL)
25362 discr_type = debug_type;
25365 if (TREE_CODE (src) != INTEGER_CST || !INTEGRAL_TYPE_P (discr_type))
25366 return false;
25368 /* Signedness can vary between the original type and the debug type. This
25369 can happen for character types in Ada for instance: the character type
25370 used for code generation can be signed, to be compatible with the C one,
25371 but from a debugger point of view, it must be unsigned. */
25372 bool is_orig_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
25373 bool is_debug_unsigned = TYPE_UNSIGNED (discr_type);
25375 if (is_orig_unsigned != is_debug_unsigned)
25376 src = fold_convert (discr_type, src);
25378 if (!(is_debug_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
25379 return false;
25381 dest->pos = is_debug_unsigned;
25382 if (is_debug_unsigned)
25383 dest->v.uval = tree_to_uhwi (src);
25384 else
25385 dest->v.sval = tree_to_shwi (src);
25387 return true;
25390 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
25391 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
25392 store NULL_TREE in DISCR_DECL. Otherwise:
25394 - store the discriminant field in STRUCT_TYPE that controls the variant
25395 part to *DISCR_DECL
25397 - put in *DISCR_LISTS_P an array where for each variant, the item
25398 represents the corresponding matching list of discriminant values.
25400 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
25401 the above array.
25403 Note that when the array is allocated (i.e. when the analysis is
25404 successful), it is up to the caller to free the array. */
25406 static void
25407 analyze_variants_discr (tree variant_part_decl,
25408 tree struct_type,
25409 tree *discr_decl,
25410 dw_discr_list_ref **discr_lists_p,
25411 unsigned *discr_lists_length)
25413 tree variant_part_type = TREE_TYPE (variant_part_decl);
25414 tree variant;
25415 dw_discr_list_ref *discr_lists;
25416 unsigned i;
25418 /* Compute how many variants there are in this variant part. */
25419 *discr_lists_length = 0;
25420 for (variant = TYPE_FIELDS (variant_part_type);
25421 variant != NULL_TREE;
25422 variant = DECL_CHAIN (variant))
25423 ++*discr_lists_length;
25425 *discr_decl = NULL_TREE;
25426 *discr_lists_p
25427 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
25428 sizeof (**discr_lists_p));
25429 discr_lists = *discr_lists_p;
25431 /* And then analyze all variants to extract discriminant information for all
25432 of them. This analysis is conservative: as soon as we detect something we
25433 do not support, abort everything and pretend we found nothing. */
25434 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
25435 variant != NULL_TREE;
25436 variant = DECL_CHAIN (variant), ++i)
25438 tree match_expr = DECL_QUALIFIER (variant);
25440 /* Now, try to analyze the predicate and deduce a discriminant for
25441 it. */
25442 if (match_expr == boolean_true_node)
25443 /* Typically happens for the default variant: it matches all cases that
25444 previous variants rejected. Don't output any matching value for
25445 this one. */
25446 continue;
25448 /* The following loop tries to iterate over each discriminant
25449 possibility: single values or ranges. */
25450 while (match_expr != NULL_TREE)
25452 tree next_round_match_expr;
25453 tree candidate_discr = NULL_TREE;
25454 dw_discr_list_ref new_node = NULL;
25456 /* Possibilities are matched one after the other by nested
25457 TRUTH_ORIF_EXPR expressions. Process the current possibility and
25458 continue with the rest at next iteration. */
25459 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
25461 next_round_match_expr = TREE_OPERAND (match_expr, 0);
25462 match_expr = TREE_OPERAND (match_expr, 1);
25464 else
25465 next_round_match_expr = NULL_TREE;
25467 if (match_expr == boolean_false_node)
25468 /* This sub-expression matches nothing: just wait for the next
25469 one. */
25472 else if (TREE_CODE (match_expr) == EQ_EXPR)
25474 /* We are matching: <discr_field> == <integer_cst>
25475 This sub-expression matches a single value. */
25476 tree integer_cst = TREE_OPERAND (match_expr, 1);
25478 candidate_discr
25479 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
25480 struct_type);
25482 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
25483 if (!get_discr_value (integer_cst,
25484 &new_node->dw_discr_lower_bound))
25485 goto abort;
25486 new_node->dw_discr_range = false;
25489 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
25491 /* We are matching:
25492 <discr_field> > <integer_cst>
25493 && <discr_field> < <integer_cst>.
25494 This sub-expression matches the range of values between the
25495 two matched integer constants. Note that comparisons can be
25496 inclusive or exclusive. */
25497 tree candidate_discr_1, candidate_discr_2;
25498 tree lower_cst, upper_cst;
25499 bool lower_cst_included, upper_cst_included;
25500 tree lower_op = TREE_OPERAND (match_expr, 0);
25501 tree upper_op = TREE_OPERAND (match_expr, 1);
25503 /* When the comparison is exclusive, the integer constant is not
25504 the discriminant range bound we are looking for: we will have
25505 to increment or decrement it. */
25506 if (TREE_CODE (lower_op) == GE_EXPR)
25507 lower_cst_included = true;
25508 else if (TREE_CODE (lower_op) == GT_EXPR)
25509 lower_cst_included = false;
25510 else
25511 goto abort;
25513 if (TREE_CODE (upper_op) == LE_EXPR)
25514 upper_cst_included = true;
25515 else if (TREE_CODE (upper_op) == LT_EXPR)
25516 upper_cst_included = false;
25517 else
25518 goto abort;
25520 /* Extract the discriminant from the first operand and check it
25521 is consistant with the same analysis in the second
25522 operand. */
25523 candidate_discr_1
25524 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
25525 struct_type);
25526 candidate_discr_2
25527 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
25528 struct_type);
25529 if (candidate_discr_1 == candidate_discr_2)
25530 candidate_discr = candidate_discr_1;
25531 else
25532 goto abort;
25534 /* Extract bounds from both. */
25535 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
25536 lower_cst = TREE_OPERAND (lower_op, 1);
25537 upper_cst = TREE_OPERAND (upper_op, 1);
25539 if (!lower_cst_included)
25540 lower_cst
25541 = fold_build2 (PLUS_EXPR, TREE_TYPE (lower_cst), lower_cst,
25542 build_int_cst (TREE_TYPE (lower_cst), 1));
25543 if (!upper_cst_included)
25544 upper_cst
25545 = fold_build2 (MINUS_EXPR, TREE_TYPE (upper_cst), upper_cst,
25546 build_int_cst (TREE_TYPE (upper_cst), 1));
25548 if (!get_discr_value (lower_cst,
25549 &new_node->dw_discr_lower_bound)
25550 || !get_discr_value (upper_cst,
25551 &new_node->dw_discr_upper_bound))
25552 goto abort;
25554 new_node->dw_discr_range = true;
25557 else if ((candidate_discr
25558 = analyze_discr_in_predicate (match_expr, struct_type))
25559 && (TREE_TYPE (candidate_discr) == boolean_type_node
25560 || TREE_TYPE (TREE_TYPE (candidate_discr))
25561 == boolean_type_node))
25563 /* We are matching: <discr_field> for a boolean discriminant.
25564 This sub-expression matches boolean_true_node. */
25565 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
25566 if (!get_discr_value (boolean_true_node,
25567 &new_node->dw_discr_lower_bound))
25568 goto abort;
25569 new_node->dw_discr_range = false;
25572 else
25573 /* Unsupported sub-expression: we cannot determine the set of
25574 matching discriminant values. Abort everything. */
25575 goto abort;
25577 /* If the discriminant info is not consistant with what we saw so
25578 far, consider the analysis failed and abort everything. */
25579 if (candidate_discr == NULL_TREE
25580 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
25581 goto abort;
25582 else
25583 *discr_decl = candidate_discr;
25585 if (new_node != NULL)
25587 new_node->dw_discr_next = discr_lists[i];
25588 discr_lists[i] = new_node;
25590 match_expr = next_round_match_expr;
25594 /* If we reach this point, we could match everything we were interested
25595 in. */
25596 return;
25598 abort:
25599 /* Clean all data structure and return no result. */
25600 free (*discr_lists_p);
25601 *discr_lists_p = NULL;
25602 *discr_decl = NULL_TREE;
25605 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
25606 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
25607 under CONTEXT_DIE.
25609 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
25610 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
25611 this type, which are record types, represent the available variants and each
25612 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
25613 values are inferred from these attributes.
25615 In trees, the offsets for the fields inside these sub-records are relative
25616 to the variant part itself, whereas the corresponding DIEs should have
25617 offset attributes that are relative to the embedding record base address.
25618 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
25619 must be an expression that computes the offset of the variant part to
25620 describe in DWARF. */
25622 static void
25623 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
25624 dw_die_ref context_die)
25626 const tree variant_part_type = TREE_TYPE (variant_part_decl);
25627 tree variant_part_offset = vlr_ctx->variant_part_offset;
25629 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
25630 NULL_TREE if there is no such field. */
25631 tree discr_decl = NULL_TREE;
25632 dw_discr_list_ref *discr_lists;
25633 unsigned discr_lists_length = 0;
25634 unsigned i;
25636 dw_die_ref dwarf_proc_die = NULL;
25637 dw_die_ref variant_part_die
25638 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
25640 equate_decl_number_to_die (variant_part_decl, variant_part_die);
25642 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
25643 &discr_decl, &discr_lists, &discr_lists_length);
25645 if (discr_decl != NULL_TREE)
25647 dw_die_ref discr_die = lookup_decl_die (discr_decl);
25649 if (discr_die)
25650 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
25651 else
25652 /* We have no DIE for the discriminant, so just discard all
25653 discrimimant information in the output. */
25654 discr_decl = NULL_TREE;
25657 /* If the offset for this variant part is more complex than a constant,
25658 create a DWARF procedure for it so that we will not have to generate
25659 DWARF expressions for it for each member. */
25660 if (TREE_CODE (variant_part_offset) != INTEGER_CST
25661 && (dwarf_version >= 3 || !dwarf_strict))
25663 struct loc_descr_context ctx = {
25664 vlr_ctx->struct_type, /* context_type */
25665 NULL_TREE, /* base_decl */
25666 NULL, /* dpi */
25667 false, /* placeholder_arg */
25668 false, /* placeholder_seen */
25669 false /* strict_signedness */
25671 const tree dwarf_proc_fndecl
25672 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
25673 build_function_type (TREE_TYPE (variant_part_offset),
25674 NULL_TREE));
25675 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
25676 const dw_loc_descr_ref dwarf_proc_body
25677 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
25679 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
25680 dwarf_proc_fndecl, context_die);
25681 if (dwarf_proc_die != NULL)
25682 variant_part_offset = dwarf_proc_call;
25685 /* Output DIEs for all variants. */
25686 i = 0;
25687 for (tree variant = TYPE_FIELDS (variant_part_type);
25688 variant != NULL_TREE;
25689 variant = DECL_CHAIN (variant), ++i)
25691 tree variant_type = TREE_TYPE (variant);
25692 dw_die_ref variant_die;
25694 /* All variants (i.e. members of a variant part) are supposed to be
25695 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
25696 under these records. */
25697 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
25699 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
25700 equate_decl_number_to_die (variant, variant_die);
25702 /* Output discriminant values this variant matches, if any. */
25703 if (discr_decl == NULL || discr_lists[i] == NULL)
25704 /* In the case we have discriminant information at all, this is
25705 probably the default variant: as the standard says, don't
25706 output any discriminant value/list attribute. */
25708 else if (discr_lists[i]->dw_discr_next == NULL
25709 && !discr_lists[i]->dw_discr_range)
25710 /* If there is only one accepted value, don't bother outputting a
25711 list. */
25712 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
25713 else
25714 add_discr_list (variant_die, discr_lists[i]);
25716 for (tree member = TYPE_FIELDS (variant_type);
25717 member != NULL_TREE;
25718 member = DECL_CHAIN (member))
25720 struct vlr_context vlr_sub_ctx = {
25721 vlr_ctx->struct_type, /* struct_type */
25722 NULL /* variant_part_offset */
25724 if (is_variant_part (member))
25726 /* All offsets for fields inside variant parts are relative to
25727 the top-level embedding RECORD_TYPE's base address. On the
25728 other hand, offsets in GCC's types are relative to the
25729 nested-most variant part. So we have to sum offsets each time
25730 we recurse. */
25732 vlr_sub_ctx.variant_part_offset
25733 = fold_build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
25734 variant_part_offset, byte_position (member));
25735 gen_variant_part (member, &vlr_sub_ctx, variant_die);
25737 else
25739 vlr_sub_ctx.variant_part_offset = variant_part_offset;
25740 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
25745 free (discr_lists);
25748 /* Generate a DIE for a class member. */
25750 static void
25751 gen_member_die (tree type, dw_die_ref context_die)
25753 tree member;
25754 tree binfo = TYPE_BINFO (type);
25756 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
25758 /* If this is not an incomplete type, output descriptions of each of its
25759 members. Note that as we output the DIEs necessary to represent the
25760 members of this record or union type, we will also be trying to output
25761 DIEs to represent the *types* of those members. However the `type'
25762 function (above) will specifically avoid generating type DIEs for member
25763 types *within* the list of member DIEs for this (containing) type except
25764 for those types (of members) which are explicitly marked as also being
25765 members of this (containing) type themselves. The g++ front- end can
25766 force any given type to be treated as a member of some other (containing)
25767 type by setting the TYPE_CONTEXT of the given (member) type to point to
25768 the TREE node representing the appropriate (containing) type. */
25770 /* First output info about the base classes. */
25771 if (binfo && early_dwarf)
25773 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
25774 int i;
25775 tree base;
25777 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
25778 gen_inheritance_die (base,
25779 (accesses ? (*accesses)[i] : access_public_node),
25780 type,
25781 context_die);
25784 /* Now output info about the members. */
25785 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
25787 /* Ignore clones. */
25788 if (DECL_ABSTRACT_ORIGIN (member))
25789 continue;
25791 struct vlr_context vlr_ctx = { type, NULL_TREE };
25792 bool static_inline_p
25793 = (VAR_P (member)
25794 && TREE_STATIC (member)
25795 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
25796 != -1));
25798 /* If we thought we were generating minimal debug info for TYPE
25799 and then changed our minds, some of the member declarations
25800 may have already been defined. Don't define them again, but
25801 do put them in the right order. */
25803 if (dw_die_ref child = lookup_decl_die (member))
25805 /* Handle inline static data members, which only have in-class
25806 declarations. */
25807 bool splice = true;
25809 dw_die_ref ref = NULL;
25810 if (child->die_tag == DW_TAG_variable
25811 && child->die_parent == comp_unit_die ())
25813 ref = get_AT_ref (child, DW_AT_specification);
25815 /* For C++17 inline static data members followed by redundant
25816 out of class redeclaration, we might get here with
25817 child being the DIE created for the out of class
25818 redeclaration and with its DW_AT_specification being
25819 the DIE created for in-class definition. We want to
25820 reparent the latter, and don't want to create another
25821 DIE with DW_AT_specification in that case, because
25822 we already have one. */
25823 if (ref
25824 && static_inline_p
25825 && ref->die_tag == DW_TAG_variable
25826 && ref->die_parent == comp_unit_die ()
25827 && get_AT (ref, DW_AT_specification) == NULL)
25829 child = ref;
25830 ref = NULL;
25831 static_inline_p = false;
25834 if (!ref)
25836 reparent_child (child, context_die);
25837 if (dwarf_version < 5)
25838 child->die_tag = DW_TAG_member;
25839 splice = false;
25842 else if (child->die_tag == DW_TAG_enumerator)
25843 /* Enumerators remain under their enumeration even if
25844 their names are introduced in the enclosing scope. */
25845 splice = false;
25847 if (splice)
25848 splice_child_die (context_die, child);
25851 /* Do not generate DWARF for variant parts if we are generating the
25852 corresponding GNAT encodings: DIEs generated for the two schemes
25853 would conflict in our mappings. */
25854 else if (is_variant_part (member)
25855 && gnat_encodings != DWARF_GNAT_ENCODINGS_ALL)
25857 vlr_ctx.variant_part_offset = byte_position (member);
25858 gen_variant_part (member, &vlr_ctx, context_die);
25860 else
25862 vlr_ctx.variant_part_offset = NULL_TREE;
25863 gen_decl_die (member, NULL, &vlr_ctx, context_die);
25866 /* For C++ inline static data members emit immediately a DW_TAG_variable
25867 DIE that will refer to that DW_TAG_member/DW_TAG_variable through
25868 DW_AT_specification. */
25869 if (static_inline_p)
25871 int old_extern = DECL_EXTERNAL (member);
25872 DECL_EXTERNAL (member) = 0;
25873 gen_decl_die (member, NULL, NULL, comp_unit_die ());
25874 DECL_EXTERNAL (member) = old_extern;
25879 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
25880 is set, we pretend that the type was never defined, so we only get the
25881 member DIEs needed by later specification DIEs. */
25883 static void
25884 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
25885 enum debug_info_usage usage)
25887 if (TREE_ASM_WRITTEN (type))
25889 /* Fill in the bound of variable-length fields in late dwarf if
25890 still incomplete. */
25891 if (!early_dwarf && variably_modified_type_p (type, NULL))
25892 for (tree member = TYPE_FIELDS (type);
25893 member;
25894 member = DECL_CHAIN (member))
25895 fill_variable_array_bounds (TREE_TYPE (member));
25896 return;
25899 dw_die_ref type_die = lookup_type_die (type);
25900 dw_die_ref scope_die = 0;
25901 bool nested = false;
25902 bool complete = (TYPE_SIZE (type)
25903 && (! TYPE_STUB_DECL (type)
25904 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
25905 bool ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
25906 complete = complete && should_emit_struct_debug (type, usage);
25908 if (type_die && ! complete)
25909 return;
25911 if (TYPE_CONTEXT (type) != NULL_TREE
25912 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
25913 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
25914 nested = true;
25916 scope_die = scope_die_for (type, context_die);
25918 /* Generate child dies for template parameters. */
25919 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
25920 schedule_generic_params_dies_gen (type);
25922 if (! type_die || (nested && is_cu_die (scope_die)))
25923 /* First occurrence of type or toplevel definition of nested class. */
25925 dw_die_ref old_die = type_die;
25927 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
25928 ? record_type_tag (type) : DW_TAG_union_type,
25929 scope_die, type);
25930 equate_type_number_to_die (type, type_die);
25931 if (old_die)
25932 add_AT_specification (type_die, old_die);
25933 else
25934 add_name_attribute (type_die, type_tag (type));
25936 else
25937 remove_AT (type_die, DW_AT_declaration);
25939 /* If this type has been completed, then give it a byte_size attribute and
25940 then give a list of members. */
25941 if (complete && !ns_decl)
25943 /* Prevent infinite recursion in cases where the type of some member of
25944 this type is expressed in terms of this type itself. */
25945 TREE_ASM_WRITTEN (type) = 1;
25946 add_byte_size_attribute (type_die, type);
25947 add_alignment_attribute (type_die, type);
25948 if (TYPE_STUB_DECL (type) != NULL_TREE)
25950 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
25951 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
25954 /* If the first reference to this type was as the return type of an
25955 inline function, then it may not have a parent. Fix this now. */
25956 if (type_die->die_parent == NULL)
25957 add_child_die (scope_die, type_die);
25959 gen_member_die (type, type_die);
25961 add_gnat_descriptive_type_attribute (type_die, type, context_die);
25962 if (TYPE_ARTIFICIAL (type))
25963 add_AT_flag (type_die, DW_AT_artificial, 1);
25965 /* GNU extension: Record what type our vtable lives in. */
25966 if (TYPE_VFIELD (type))
25968 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
25970 gen_type_die (vtype, context_die);
25971 add_AT_die_ref (type_die, DW_AT_containing_type,
25972 lookup_type_die (vtype));
25975 else
25977 add_AT_flag (type_die, DW_AT_declaration, 1);
25979 /* We don't need to do this for function-local types. */
25980 if (TYPE_STUB_DECL (type)
25981 && ! decl_function_context (TYPE_STUB_DECL (type)))
25982 vec_safe_push (incomplete_types, type);
25985 if (get_AT (type_die, DW_AT_name))
25986 add_pubtype (type, type_die);
25989 /* Generate a DIE for a subroutine _type_. */
25991 static void
25992 gen_subroutine_type_die (tree type, dw_die_ref context_die)
25994 tree return_type = TREE_TYPE (type);
25995 dw_die_ref subr_die
25996 = new_die (DW_TAG_subroutine_type,
25997 scope_die_for (type, context_die), type);
25999 equate_type_number_to_die (type, subr_die);
26000 add_prototyped_attribute (subr_die, type);
26001 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
26002 context_die);
26003 add_alignment_attribute (subr_die, type);
26004 gen_formal_types_die (type, subr_die);
26006 if (get_AT (subr_die, DW_AT_name))
26007 add_pubtype (type, subr_die);
26008 if ((dwarf_version >= 5 || !dwarf_strict)
26009 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
26010 add_AT_flag (subr_die, DW_AT_reference, 1);
26011 if ((dwarf_version >= 5 || !dwarf_strict)
26012 && lang_hooks.types.type_dwarf_attribute (type,
26013 DW_AT_rvalue_reference) != -1)
26014 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
26017 /* Generate a DIE for a type definition. */
26019 static void
26020 gen_typedef_die (tree decl, dw_die_ref context_die)
26022 dw_die_ref type_die;
26023 tree type;
26025 if (TREE_ASM_WRITTEN (decl))
26027 if (DECL_ORIGINAL_TYPE (decl))
26028 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
26029 return;
26032 /* As we avoid creating DIEs for local typedefs (see decl_ultimate_origin
26033 checks in process_scope_var and modified_type_die), this should be called
26034 only for original types. */
26035 gcc_assert (decl_ultimate_origin (decl) == NULL
26036 || decl_ultimate_origin (decl) == decl);
26038 TREE_ASM_WRITTEN (decl) = 1;
26039 type_die = new_die (DW_TAG_typedef, context_die, decl);
26041 add_name_and_src_coords_attributes (type_die, decl);
26042 if (DECL_ORIGINAL_TYPE (decl))
26044 type = DECL_ORIGINAL_TYPE (decl);
26045 if (type == error_mark_node)
26046 return;
26048 gcc_assert (type != TREE_TYPE (decl));
26049 equate_type_number_to_die (TREE_TYPE (decl), type_die);
26051 else
26053 type = TREE_TYPE (decl);
26054 if (type == error_mark_node)
26055 return;
26057 if (is_naming_typedef_decl (TYPE_NAME (type)))
26059 /* Here, we are in the case of decl being a typedef naming
26060 an anonymous type, e.g:
26061 typedef struct {...} foo;
26062 In that case TREE_TYPE (decl) is not a typedef variant
26063 type and TYPE_NAME of the anonymous type is set to the
26064 TYPE_DECL of the typedef. This construct is emitted by
26065 the C++ FE.
26067 TYPE is the anonymous struct named by the typedef
26068 DECL. As we need the DW_AT_type attribute of the
26069 DW_TAG_typedef to point to the DIE of TYPE, let's
26070 generate that DIE right away. add_type_attribute
26071 called below will then pick (via lookup_type_die) that
26072 anonymous struct DIE. */
26073 if (!TREE_ASM_WRITTEN (type))
26074 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
26076 /* This is a GNU Extension. We are adding a
26077 DW_AT_linkage_name attribute to the DIE of the
26078 anonymous struct TYPE. The value of that attribute
26079 is the name of the typedef decl naming the anonymous
26080 struct. This greatly eases the work of consumers of
26081 this debug info. */
26082 add_linkage_name_raw (lookup_type_die (type), decl);
26086 add_type_attribute (type_die, type, decl_quals (decl), false,
26087 context_die);
26089 if (is_naming_typedef_decl (decl))
26090 /* We want that all subsequent calls to lookup_type_die with
26091 TYPE in argument yield the DW_TAG_typedef we have just
26092 created. */
26093 equate_type_number_to_die (type, type_die);
26095 add_alignment_attribute (type_die, TREE_TYPE (decl));
26097 add_accessibility_attribute (type_die, decl);
26099 if (DECL_ABSTRACT_P (decl))
26100 equate_decl_number_to_die (decl, type_die);
26102 if (get_AT (type_die, DW_AT_name))
26103 add_pubtype (decl, type_die);
26106 /* Generate a DIE for a struct, class, enum or union type. */
26108 static void
26109 gen_tagged_type_die (tree type,
26110 dw_die_ref context_die,
26111 enum debug_info_usage usage)
26113 if (type == NULL_TREE
26114 || !is_tagged_type (type))
26115 return;
26117 if (TREE_ASM_WRITTEN (type))
26119 /* If this is a nested type whose containing class hasn't been written
26120 out yet, writing it out will cover this one, too. This does not apply
26121 to instantiations of member class templates; they need to be added to
26122 the containing class as they are generated. FIXME: This hurts the
26123 idea of combining type decls from multiple TUs, since we can't predict
26124 what set of template instantiations we'll get. */
26125 else if (TYPE_CONTEXT (type)
26126 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
26127 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
26129 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
26131 if (TREE_ASM_WRITTEN (type))
26132 return;
26134 /* If that failed, attach ourselves to the stub. */
26135 context_die = lookup_type_die (TYPE_CONTEXT (type));
26137 else if (TYPE_CONTEXT (type) != NULL_TREE
26138 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
26140 /* If this type is local to a function that hasn't been written
26141 out yet, use a NULL context for now; it will be fixed up in
26142 decls_for_scope. */
26143 context_die = lookup_decl_die (TYPE_CONTEXT (type));
26144 /* A declaration DIE doesn't count; nested types need to go in the
26145 specification. */
26146 if (context_die && is_declaration_die (context_die))
26147 context_die = NULL;
26149 else
26150 context_die = declare_in_namespace (type, context_die);
26152 if (TREE_CODE (type) == ENUMERAL_TYPE)
26154 /* This might have been written out by the call to
26155 declare_in_namespace. */
26156 if (!TREE_ASM_WRITTEN (type))
26157 gen_enumeration_type_die (type, context_die);
26159 else
26160 gen_struct_or_union_type_die (type, context_die, usage);
26162 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
26163 it up if it is ever completed. gen_*_type_die will set it for us
26164 when appropriate. */
26167 /* Generate a type description DIE. */
26169 static void
26170 gen_type_die_with_usage (tree type, dw_die_ref context_die,
26171 enum debug_info_usage usage)
26173 struct array_descr_info info;
26175 if (type == NULL_TREE || type == error_mark_node)
26176 return;
26178 if (flag_checking && type)
26179 verify_type (type);
26181 if (TYPE_NAME (type) != NULL_TREE
26182 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
26183 && is_redundant_typedef (TYPE_NAME (type))
26184 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
26185 /* The DECL of this type is a typedef we don't want to emit debug
26186 info for but we want debug info for its underlying typedef.
26187 This can happen for e.g, the injected-class-name of a C++
26188 type. */
26189 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
26191 /* If TYPE is a typedef type variant, let's generate debug info
26192 for the parent typedef which TYPE is a type of. */
26193 if (typedef_variant_p (type))
26195 if (TREE_ASM_WRITTEN (type))
26196 return;
26198 tree name = TYPE_NAME (type);
26199 tree origin = decl_ultimate_origin (name);
26200 if (origin != NULL && origin != name)
26202 gen_decl_die (origin, NULL, NULL, context_die);
26203 return;
26206 /* Prevent broken recursion; we can't hand off to the same type. */
26207 gcc_assert (DECL_ORIGINAL_TYPE (name) != type);
26209 /* Give typedefs the right scope. */
26210 context_die = scope_die_for (type, context_die);
26212 TREE_ASM_WRITTEN (type) = 1;
26214 gen_decl_die (name, NULL, NULL, context_die);
26215 return;
26218 /* If type is an anonymous tagged type named by a typedef, let's
26219 generate debug info for the typedef. */
26220 if (is_naming_typedef_decl (TYPE_NAME (type)))
26222 /* Give typedefs the right scope. */
26223 context_die = scope_die_for (type, context_die);
26225 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
26226 return;
26229 if (lang_hooks.types.get_debug_type)
26231 tree debug_type = lang_hooks.types.get_debug_type (type);
26233 if (debug_type != NULL_TREE && debug_type != type)
26235 gen_type_die_with_usage (debug_type, context_die, usage);
26236 return;
26240 /* We are going to output a DIE to represent the unqualified version
26241 of this type (i.e. without any const or volatile qualifiers) so
26242 get the main variant (i.e. the unqualified version) of this type
26243 now. (Vectors and arrays are special because the debugging info is in the
26244 cloned type itself. Similarly function/method types can contain extra
26245 ref-qualification). */
26246 if (FUNC_OR_METHOD_TYPE_P (type))
26248 /* For function/method types, can't use type_main_variant here,
26249 because that can have different ref-qualifiers for C++,
26250 but try to canonicalize. */
26251 tree main = TYPE_MAIN_VARIANT (type);
26252 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
26253 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
26254 && check_base_type (t, main)
26255 && check_lang_type (t, type))
26257 type = t;
26258 break;
26261 else if (TREE_CODE (type) != VECTOR_TYPE
26262 && TREE_CODE (type) != ARRAY_TYPE)
26263 type = type_main_variant (type);
26265 /* If this is an array type with hidden descriptor, handle it first. */
26266 if (!TREE_ASM_WRITTEN (type)
26267 && lang_hooks.types.get_array_descr_info)
26269 memset (&info, 0, sizeof (info));
26270 if (lang_hooks.types.get_array_descr_info (type, &info))
26272 /* Fortran sometimes emits array types with no dimension. */
26273 gcc_assert (info.ndimensions >= 0
26274 && (info.ndimensions
26275 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
26276 gen_descr_array_type_die (type, &info, context_die);
26277 TREE_ASM_WRITTEN (type) = 1;
26278 return;
26282 if (TREE_ASM_WRITTEN (type))
26284 /* Variable-length types may be incomplete even if
26285 TREE_ASM_WRITTEN. For such types, fall through to
26286 gen_array_type_die() and possibly fill in
26287 DW_AT_{upper,lower}_bound attributes. */
26288 if ((TREE_CODE (type) != ARRAY_TYPE
26289 && TREE_CODE (type) != RECORD_TYPE
26290 && TREE_CODE (type) != UNION_TYPE
26291 && TREE_CODE (type) != QUAL_UNION_TYPE)
26292 || !variably_modified_type_p (type, NULL))
26293 return;
26296 switch (TREE_CODE (type))
26298 case ERROR_MARK:
26299 break;
26301 case POINTER_TYPE:
26302 case REFERENCE_TYPE:
26303 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
26304 ensures that the gen_type_die recursion will terminate even if the
26305 type is recursive. Recursive types are possible in Ada. */
26306 /* ??? We could perhaps do this for all types before the switch
26307 statement. */
26308 TREE_ASM_WRITTEN (type) = 1;
26310 /* For these types, all that is required is that we output a DIE (or a
26311 set of DIEs) to represent the "basis" type. */
26312 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26313 DINFO_USAGE_IND_USE);
26314 break;
26316 case OFFSET_TYPE:
26317 /* This code is used for C++ pointer-to-data-member types.
26318 Output a description of the relevant class type. */
26319 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
26320 DINFO_USAGE_IND_USE);
26322 /* Output a description of the type of the object pointed to. */
26323 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26324 DINFO_USAGE_IND_USE);
26326 /* Now output a DIE to represent this pointer-to-data-member type
26327 itself. */
26328 gen_ptr_to_mbr_type_die (type, context_die);
26329 break;
26331 case FUNCTION_TYPE:
26332 /* Force out return type (in case it wasn't forced out already). */
26333 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26334 DINFO_USAGE_DIR_USE);
26335 gen_subroutine_type_die (type, context_die);
26336 break;
26338 case METHOD_TYPE:
26339 /* Force out return type (in case it wasn't forced out already). */
26340 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26341 DINFO_USAGE_DIR_USE);
26342 gen_subroutine_type_die (type, context_die);
26343 break;
26345 case ARRAY_TYPE:
26346 case VECTOR_TYPE:
26347 gen_array_type_die (type, context_die);
26348 break;
26350 case ENUMERAL_TYPE:
26351 case RECORD_TYPE:
26352 case UNION_TYPE:
26353 case QUAL_UNION_TYPE:
26354 gen_tagged_type_die (type, context_die, usage);
26355 return;
26357 case VOID_TYPE:
26358 case OPAQUE_TYPE:
26359 case INTEGER_TYPE:
26360 case REAL_TYPE:
26361 case FIXED_POINT_TYPE:
26362 case COMPLEX_TYPE:
26363 case BOOLEAN_TYPE:
26364 /* No DIEs needed for fundamental types. */
26365 break;
26367 case NULLPTR_TYPE:
26368 case LANG_TYPE:
26369 /* Just use DW_TAG_unspecified_type. */
26371 dw_die_ref type_die = lookup_type_die (type);
26372 if (type_die == NULL)
26374 tree name = TYPE_IDENTIFIER (type);
26375 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
26376 type);
26377 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
26378 equate_type_number_to_die (type, type_die);
26381 break;
26383 default:
26384 if (is_cxx_auto (type))
26386 tree name = TYPE_IDENTIFIER (type);
26387 dw_die_ref *die = (name == get_identifier ("auto")
26388 ? &auto_die : &decltype_auto_die);
26389 if (!*die)
26391 *die = new_die (DW_TAG_unspecified_type,
26392 comp_unit_die (), NULL_TREE);
26393 add_name_attribute (*die, IDENTIFIER_POINTER (name));
26395 equate_type_number_to_die (type, *die);
26396 break;
26398 gcc_unreachable ();
26401 TREE_ASM_WRITTEN (type) = 1;
26404 static void
26405 gen_type_die (tree type, dw_die_ref context_die)
26407 if (type != error_mark_node)
26409 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
26410 if (flag_checking)
26412 dw_die_ref die = lookup_type_die (type);
26413 if (die)
26414 check_die (die);
26419 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
26420 things which are local to the given block. */
26422 static void
26423 gen_block_die (tree stmt, dw_die_ref context_die)
26425 int must_output_die = 0;
26426 bool inlined_func;
26428 /* Ignore blocks that are NULL. */
26429 if (stmt == NULL_TREE)
26430 return;
26432 inlined_func = inlined_function_outer_scope_p (stmt);
26434 /* If the block is one fragment of a non-contiguous block, do not
26435 process the variables, since they will have been done by the
26436 origin block. Do process subblocks. */
26437 if (BLOCK_FRAGMENT_ORIGIN (stmt))
26439 tree sub;
26441 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
26442 gen_block_die (sub, context_die);
26444 return;
26447 /* Determine if we need to output any Dwarf DIEs at all to represent this
26448 block. */
26449 if (inlined_func)
26450 /* The outer scopes for inlinings *must* always be represented. We
26451 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
26452 must_output_die = 1;
26453 else if (lookup_block_die (stmt))
26454 /* If we already have a DIE then it was filled early. Meanwhile
26455 we might have pruned all BLOCK_VARS as optimized out but we
26456 still want to generate high/low PC attributes so output it. */
26457 must_output_die = 1;
26458 else if (TREE_USED (stmt)
26459 || TREE_ASM_WRITTEN (stmt))
26461 /* Determine if this block directly contains any "significant"
26462 local declarations which we will need to output DIEs for. */
26463 if (debug_info_level > DINFO_LEVEL_TERSE)
26465 /* We are not in terse mode so any local declaration that
26466 is not ignored for debug purposes counts as being a
26467 "significant" one. */
26468 if (BLOCK_NUM_NONLOCALIZED_VARS (stmt))
26469 must_output_die = 1;
26470 else
26471 for (tree var = BLOCK_VARS (stmt); var; var = DECL_CHAIN (var))
26472 if (!DECL_IGNORED_P (var))
26474 must_output_die = 1;
26475 break;
26478 else if (!dwarf2out_ignore_block (stmt))
26479 must_output_die = 1;
26482 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
26483 DIE for any block which contains no significant local declarations at
26484 all. Rather, in such cases we just call `decls_for_scope' so that any
26485 needed Dwarf info for any sub-blocks will get properly generated. Note
26486 that in terse mode, our definition of what constitutes a "significant"
26487 local declaration gets restricted to include only inlined function
26488 instances and local (nested) function definitions. */
26489 if (must_output_die)
26491 if (inlined_func)
26492 gen_inlined_subroutine_die (stmt, context_die);
26493 else
26494 gen_lexical_block_die (stmt, context_die);
26496 else
26497 decls_for_scope (stmt, context_die);
26500 /* Process variable DECL (or variable with origin ORIGIN) within
26501 block STMT and add it to CONTEXT_DIE. */
26502 static void
26503 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
26505 dw_die_ref die;
26506 tree decl_or_origin = decl ? decl : origin;
26508 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
26509 die = lookup_decl_die (decl_or_origin);
26510 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
26512 if (TYPE_DECL_IS_STUB (decl_or_origin))
26513 die = lookup_type_die (TREE_TYPE (decl_or_origin));
26514 else
26515 die = lookup_decl_die (decl_or_origin);
26516 /* Avoid re-creating the DIE late if it was optimized as unused early. */
26517 if (! die && ! early_dwarf)
26518 return;
26520 else
26521 die = NULL;
26523 /* Avoid creating DIEs for local typedefs and concrete static variables that
26524 will only be pruned later. */
26525 if ((origin || decl_ultimate_origin (decl))
26526 && (TREE_CODE (decl_or_origin) == TYPE_DECL
26527 || (VAR_P (decl_or_origin) && TREE_STATIC (decl_or_origin))))
26529 origin = decl_ultimate_origin (decl_or_origin);
26530 if (decl && VAR_P (decl) && die != NULL)
26532 die = lookup_decl_die (origin);
26533 if (die != NULL)
26534 equate_decl_number_to_die (decl, die);
26536 return;
26539 if (die != NULL && die->die_parent == NULL)
26540 add_child_die (context_die, die);
26542 if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
26544 if (early_dwarf)
26545 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
26546 stmt, context_die);
26548 else
26550 if (decl && DECL_P (decl))
26552 die = lookup_decl_die (decl);
26554 /* Early created DIEs do not have a parent as the decls refer
26555 to the function as DECL_CONTEXT rather than the BLOCK. */
26556 if (die && die->die_parent == NULL)
26558 gcc_assert (in_lto_p);
26559 add_child_die (context_die, die);
26563 gen_decl_die (decl, origin, NULL, context_die);
26567 /* Generate all of the decls declared within a given scope and (recursively)
26568 all of its sub-blocks. */
26570 static void
26571 decls_for_scope (tree stmt, dw_die_ref context_die, bool recurse)
26573 tree decl;
26574 unsigned int i;
26575 tree subblocks;
26577 /* Ignore NULL blocks. */
26578 if (stmt == NULL_TREE)
26579 return;
26581 /* Output the DIEs to represent all of the data objects and typedefs
26582 declared directly within this block but not within any nested
26583 sub-blocks. Also, nested function and tag DIEs have been
26584 generated with a parent of NULL; fix that up now. We don't
26585 have to do this if we're at -g1. */
26586 if (debug_info_level > DINFO_LEVEL_TERSE)
26588 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
26589 process_scope_var (stmt, decl, NULL_TREE, context_die);
26590 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
26591 origin - avoid doing this twice as we have no good way to see
26592 if we've done it once already. */
26593 if (! early_dwarf)
26594 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
26596 decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
26597 if (decl == current_function_decl)
26598 /* Ignore declarations of the current function, while they
26599 are declarations, gen_subprogram_die would treat them
26600 as definitions again, because they are equal to
26601 current_function_decl and endlessly recurse. */;
26602 else if (TREE_CODE (decl) == FUNCTION_DECL)
26603 process_scope_var (stmt, decl, NULL_TREE, context_die);
26604 else
26605 process_scope_var (stmt, NULL_TREE, decl, context_die);
26609 /* Even if we're at -g1, we need to process the subblocks in order to get
26610 inlined call information. */
26612 /* Output the DIEs to represent all sub-blocks (and the items declared
26613 therein) of this block. */
26614 if (recurse)
26615 for (subblocks = BLOCK_SUBBLOCKS (stmt);
26616 subblocks != NULL;
26617 subblocks = BLOCK_CHAIN (subblocks))
26618 gen_block_die (subblocks, context_die);
26621 /* Is this a typedef we can avoid emitting? */
26623 static bool
26624 is_redundant_typedef (const_tree decl)
26626 if (TYPE_DECL_IS_STUB (decl))
26627 return true;
26629 if (DECL_ARTIFICIAL (decl)
26630 && DECL_CONTEXT (decl)
26631 && is_tagged_type (DECL_CONTEXT (decl))
26632 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
26633 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
26634 /* Also ignore the artificial member typedef for the class name. */
26635 return true;
26637 return false;
26640 /* Return TRUE if TYPE is a typedef that names a type for linkage
26641 purposes. This kind of typedefs is produced by the C++ FE for
26642 constructs like:
26644 typedef struct {...} foo;
26646 In that case, there is no typedef variant type produced for foo.
26647 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
26648 struct type. */
26650 static bool
26651 is_naming_typedef_decl (const_tree decl)
26653 if (decl == NULL_TREE
26654 || TREE_CODE (decl) != TYPE_DECL
26655 || DECL_NAMELESS (decl)
26656 || !is_tagged_type (TREE_TYPE (decl))
26657 || DECL_IS_UNDECLARED_BUILTIN (decl)
26658 || is_redundant_typedef (decl)
26659 /* It looks like Ada produces TYPE_DECLs that are very similar
26660 to C++ naming typedefs but that have different
26661 semantics. Let's be specific to c++ for now. */
26662 || !is_cxx (decl))
26663 return false;
26665 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
26666 && TYPE_NAME (TREE_TYPE (decl)) == decl
26667 && (TYPE_STUB_DECL (TREE_TYPE (decl))
26668 != TYPE_NAME (TREE_TYPE (decl))));
26671 /* Looks up the DIE for a context. */
26673 static inline dw_die_ref
26674 lookup_context_die (tree context)
26676 if (context)
26678 /* Find die that represents this context. */
26679 if (TYPE_P (context))
26681 context = TYPE_MAIN_VARIANT (context);
26682 dw_die_ref ctx = lookup_type_die (context);
26683 if (!ctx)
26684 return NULL;
26685 return strip_naming_typedef (context, ctx);
26687 else
26688 return lookup_decl_die (context);
26690 return comp_unit_die ();
26693 /* Returns the DIE for a context. */
26695 static inline dw_die_ref
26696 get_context_die (tree context)
26698 if (context)
26700 /* Find die that represents this context. */
26701 if (TYPE_P (context))
26703 context = TYPE_MAIN_VARIANT (context);
26704 return strip_naming_typedef (context, force_type_die (context));
26706 else
26707 return force_decl_die (context);
26709 return comp_unit_die ();
26712 /* Returns the DIE for decl. A DIE will always be returned. */
26714 static dw_die_ref
26715 force_decl_die (tree decl)
26717 dw_die_ref decl_die;
26718 unsigned saved_external_flag;
26719 tree save_fn = NULL_TREE;
26720 decl_die = lookup_decl_die (decl);
26721 if (!decl_die)
26723 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
26725 decl_die = lookup_decl_die (decl);
26726 if (decl_die)
26727 return decl_die;
26729 switch (TREE_CODE (decl))
26731 case FUNCTION_DECL:
26732 /* Clear current_function_decl, so that gen_subprogram_die thinks
26733 that this is a declaration. At this point, we just want to force
26734 declaration die. */
26735 save_fn = current_function_decl;
26736 current_function_decl = NULL_TREE;
26737 gen_subprogram_die (decl, context_die);
26738 current_function_decl = save_fn;
26739 break;
26741 case VAR_DECL:
26742 /* Set external flag to force declaration die. Restore it after
26743 gen_decl_die() call. */
26744 saved_external_flag = DECL_EXTERNAL (decl);
26745 DECL_EXTERNAL (decl) = 1;
26746 gen_decl_die (decl, NULL, NULL, context_die);
26747 DECL_EXTERNAL (decl) = saved_external_flag;
26748 break;
26750 case NAMESPACE_DECL:
26751 if (dwarf_version >= 3 || !dwarf_strict)
26752 dwarf2out_decl (decl);
26753 else
26754 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
26755 decl_die = comp_unit_die ();
26756 break;
26758 case CONST_DECL:
26759 /* Enumerators shouldn't need force_decl_die. */
26760 gcc_assert (DECL_CONTEXT (decl) == NULL_TREE
26761 || TREE_CODE (DECL_CONTEXT (decl)) != ENUMERAL_TYPE);
26762 gen_decl_die (decl, NULL, NULL, context_die);
26763 break;
26765 case TRANSLATION_UNIT_DECL:
26766 decl_die = comp_unit_die ();
26767 break;
26769 default:
26770 gcc_unreachable ();
26773 /* We should be able to find the DIE now. */
26774 if (!decl_die)
26775 decl_die = lookup_decl_die (decl);
26776 gcc_assert (decl_die);
26779 return decl_die;
26782 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
26783 always returned. */
26785 static dw_die_ref
26786 force_type_die (tree type)
26788 dw_die_ref type_die;
26790 type_die = lookup_type_die (type);
26791 if (!type_die)
26793 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
26795 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
26796 false, context_die);
26797 gcc_assert (type_die);
26799 return type_die;
26802 /* Force out any required namespaces to be able to output DECL,
26803 and return the new context_die for it, if it's changed. */
26805 static dw_die_ref
26806 setup_namespace_context (tree thing, dw_die_ref context_die)
26808 tree context = (DECL_P (thing)
26809 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
26810 if (context && TREE_CODE (context) == NAMESPACE_DECL)
26811 /* Force out the namespace. */
26812 context_die = force_decl_die (context);
26814 return context_die;
26817 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
26818 type) within its namespace, if appropriate.
26820 For compatibility with older debuggers, namespace DIEs only contain
26821 declarations; all definitions are emitted at CU scope, with
26822 DW_AT_specification pointing to the declaration (like with class
26823 members). */
26825 static dw_die_ref
26826 declare_in_namespace (tree thing, dw_die_ref context_die)
26828 dw_die_ref ns_context;
26830 if (debug_info_level <= DINFO_LEVEL_TERSE)
26831 return context_die;
26833 /* External declarations in the local scope only need to be emitted
26834 once, not once in the namespace and once in the scope.
26836 This avoids declaring the `extern' below in the
26837 namespace DIE as well as in the innermost scope:
26839 namespace S
26841 int i=5;
26842 int foo()
26844 int i=8;
26845 extern int i;
26846 return i;
26850 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
26851 return context_die;
26853 /* If this decl is from an inlined function, then don't try to emit it in its
26854 namespace, as we will get confused. It would have already been emitted
26855 when the abstract instance of the inline function was emitted anyways. */
26856 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
26857 return context_die;
26859 ns_context = setup_namespace_context (thing, context_die);
26861 if (ns_context != context_die)
26863 if (is_fortran () || is_dlang ())
26864 return ns_context;
26865 if (DECL_P (thing))
26866 gen_decl_die (thing, NULL, NULL, ns_context);
26867 else
26868 gen_type_die (thing, ns_context);
26870 return context_die;
26873 /* Generate a DIE for a namespace or namespace alias. */
26875 static void
26876 gen_namespace_die (tree decl, dw_die_ref context_die)
26878 dw_die_ref namespace_die;
26880 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
26881 they are an alias of. */
26882 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
26884 /* Output a real namespace or module. */
26885 context_die = setup_namespace_context (decl, comp_unit_die ());
26886 namespace_die = new_die (is_fortran () || is_dlang ()
26887 ? DW_TAG_module : DW_TAG_namespace,
26888 context_die, decl);
26889 /* For Fortran modules defined in different CU don't add src coords. */
26890 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
26892 const char *name = dwarf2_name (decl, 0);
26893 if (name)
26894 add_name_attribute (namespace_die, name);
26896 else
26897 add_name_and_src_coords_attributes (namespace_die, decl);
26898 if (DECL_EXTERNAL (decl))
26899 add_AT_flag (namespace_die, DW_AT_declaration, 1);
26900 equate_decl_number_to_die (decl, namespace_die);
26902 else
26904 /* Output a namespace alias. */
26906 /* Force out the namespace we are an alias of, if necessary. */
26907 dw_die_ref origin_die
26908 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
26910 if (DECL_FILE_SCOPE_P (decl)
26911 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
26912 context_die = setup_namespace_context (decl, comp_unit_die ());
26913 /* Now create the namespace alias DIE. */
26914 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
26915 add_name_and_src_coords_attributes (namespace_die, decl);
26916 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
26917 equate_decl_number_to_die (decl, namespace_die);
26919 if ((dwarf_version >= 5 || !dwarf_strict)
26920 && lang_hooks.decls.decl_dwarf_attribute (decl,
26921 DW_AT_export_symbols) == 1)
26922 add_AT_flag (namespace_die, DW_AT_export_symbols, 1);
26924 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
26925 if (want_pubnames ())
26926 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
26929 /* Generate Dwarf debug information for a decl described by DECL.
26930 The return value is currently only meaningful for PARM_DECLs,
26931 for all other decls it returns NULL.
26933 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
26934 It can be NULL otherwise. */
26936 static dw_die_ref
26937 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
26938 dw_die_ref context_die)
26940 tree decl_or_origin = decl ? decl : origin;
26941 tree class_origin = NULL, ultimate_origin;
26943 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
26944 return NULL;
26946 switch (TREE_CODE (decl_or_origin))
26948 case ERROR_MARK:
26949 break;
26951 case CONST_DECL:
26952 if (!is_fortran () && !is_ada () && !is_dlang ())
26954 /* The individual enumerators of an enum type get output when we output
26955 the Dwarf representation of the relevant enum type itself. */
26956 break;
26959 /* Emit its type. */
26960 gen_type_die (TREE_TYPE (decl), context_die);
26962 /* And its containing namespace. */
26963 context_die = declare_in_namespace (decl, context_die);
26965 gen_const_die (decl, context_die);
26966 break;
26968 case FUNCTION_DECL:
26969 #if 0
26970 /* FIXME */
26971 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
26972 on local redeclarations of global functions. That seems broken. */
26973 if (current_function_decl != decl)
26974 /* This is only a declaration. */;
26975 #endif
26977 /* We should have abstract copies already and should not generate
26978 stray type DIEs in late LTO dumping. */
26979 if (! early_dwarf)
26982 /* If we're emitting a clone, emit info for the abstract instance. */
26983 else if (origin || DECL_ORIGIN (decl) != decl)
26984 dwarf2out_abstract_function (origin
26985 ? DECL_ORIGIN (origin)
26986 : DECL_ABSTRACT_ORIGIN (decl));
26988 /* If we're emitting a possibly inlined function emit it as
26989 abstract instance. */
26990 else if (cgraph_function_possibly_inlined_p (decl)
26991 && ! DECL_ABSTRACT_P (decl)
26992 && ! class_or_namespace_scope_p (context_die)
26993 /* dwarf2out_abstract_function won't emit a die if this is just
26994 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
26995 that case, because that works only if we have a die. */
26996 && DECL_INITIAL (decl) != NULL_TREE)
26997 dwarf2out_abstract_function (decl);
26999 /* Otherwise we're emitting the primary DIE for this decl. */
27000 else if (debug_info_level > DINFO_LEVEL_TERSE)
27002 /* Before we describe the FUNCTION_DECL itself, make sure that we
27003 have its containing type. */
27004 if (!origin)
27005 origin = decl_class_context (decl);
27006 if (origin != NULL_TREE)
27007 gen_type_die (origin, context_die);
27009 /* And its return type. */
27010 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
27012 /* And its virtual context. */
27013 if (DECL_VINDEX (decl) != NULL_TREE)
27014 gen_type_die (DECL_CONTEXT (decl), context_die);
27016 /* Make sure we have a member DIE for decl. */
27017 if (origin != NULL_TREE)
27018 gen_type_die_for_member (origin, decl, context_die);
27020 /* And its containing namespace. */
27021 context_die = declare_in_namespace (decl, context_die);
27024 /* Now output a DIE to represent the function itself. */
27025 if (decl)
27026 gen_subprogram_die (decl, context_die);
27027 break;
27029 case TYPE_DECL:
27030 /* If we are in terse mode, don't generate any DIEs to represent any
27031 actual typedefs. */
27032 if (debug_info_level <= DINFO_LEVEL_TERSE)
27033 break;
27035 /* In the special case of a TYPE_DECL node representing the declaration
27036 of some type tag, if the given TYPE_DECL is marked as having been
27037 instantiated from some other (original) TYPE_DECL node (e.g. one which
27038 was generated within the original definition of an inline function) we
27039 used to generate a special (abbreviated) DW_TAG_structure_type,
27040 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
27041 should be actually referencing those DIEs, as variable DIEs with that
27042 type would be emitted already in the abstract origin, so it was always
27043 removed during unused type prunning. Don't add anything in this
27044 case. */
27045 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
27046 break;
27048 if (is_redundant_typedef (decl))
27049 gen_type_die (TREE_TYPE (decl), context_die);
27050 else
27051 /* Output a DIE to represent the typedef itself. */
27052 gen_typedef_die (decl, context_die);
27053 break;
27055 case LABEL_DECL:
27056 if (debug_info_level >= DINFO_LEVEL_NORMAL)
27057 gen_label_die (decl, context_die);
27058 break;
27060 case VAR_DECL:
27061 case RESULT_DECL:
27062 /* If we are in terse mode, don't generate any DIEs to represent any
27063 variable declarations or definitions unless it is external. */
27064 if (debug_info_level < DINFO_LEVEL_TERSE
27065 || (debug_info_level == DINFO_LEVEL_TERSE
27066 && !TREE_PUBLIC (decl_or_origin)))
27067 break;
27069 if (debug_info_level > DINFO_LEVEL_TERSE)
27071 /* Avoid generating stray type DIEs during late dwarf dumping.
27072 All types have been dumped early. */
27073 if (early_dwarf
27074 /* ??? But in LTRANS we cannot annotate early created variably
27075 modified type DIEs without copying them and adjusting all
27076 references to them. Dump them again as happens for inlining
27077 which copies both the decl and the types. */
27078 /* ??? And even non-LTO needs to re-visit type DIEs to fill
27079 in VLA bound information for example. */
27080 || (decl && variably_modified_type_p (TREE_TYPE (decl),
27081 current_function_decl)))
27083 /* Output any DIEs that are needed to specify the type of this data
27084 object. */
27085 if (decl_by_reference_p (decl_or_origin))
27086 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
27087 else
27088 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
27091 if (early_dwarf)
27093 /* And its containing type. */
27094 class_origin = decl_class_context (decl_or_origin);
27095 if (class_origin != NULL_TREE)
27096 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
27098 /* And its containing namespace. */
27099 context_die = declare_in_namespace (decl_or_origin, context_die);
27103 /* Now output the DIE to represent the data object itself. This gets
27104 complicated because of the possibility that the VAR_DECL really
27105 represents an inlined instance of a formal parameter for an inline
27106 function. */
27107 ultimate_origin = decl_ultimate_origin (decl_or_origin);
27108 if (ultimate_origin != NULL_TREE
27109 && TREE_CODE (ultimate_origin) == PARM_DECL)
27110 gen_formal_parameter_die (decl, origin,
27111 true /* Emit name attribute. */,
27112 context_die);
27113 else
27114 gen_variable_die (decl, origin, context_die);
27115 break;
27117 case FIELD_DECL:
27118 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
27119 /* Ignore the nameless fields that are used to skip bits but handle C++
27120 anonymous unions and structs. */
27121 if (DECL_NAME (decl) != NULL_TREE
27122 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
27123 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
27125 gen_type_die (member_declared_type (decl), context_die);
27126 gen_field_die (decl, ctx, context_die);
27128 break;
27130 case PARM_DECL:
27131 /* Avoid generating stray type DIEs during late dwarf dumping.
27132 All types have been dumped early. */
27133 if (early_dwarf
27134 /* ??? But in LTRANS we cannot annotate early created variably
27135 modified type DIEs without copying them and adjusting all
27136 references to them. Dump them again as happens for inlining
27137 which copies both the decl and the types. */
27138 /* ??? And even non-LTO needs to re-visit type DIEs to fill
27139 in VLA bound information for example. */
27140 || (decl && variably_modified_type_p (TREE_TYPE (decl),
27141 current_function_decl)))
27143 if (DECL_BY_REFERENCE (decl_or_origin))
27144 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
27145 else
27146 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
27148 return gen_formal_parameter_die (decl, origin,
27149 true /* Emit name attribute. */,
27150 context_die);
27152 case NAMESPACE_DECL:
27153 if (dwarf_version >= 3 || !dwarf_strict)
27154 gen_namespace_die (decl, context_die);
27155 break;
27157 case IMPORTED_DECL:
27158 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
27159 DECL_CONTEXT (decl), context_die);
27160 break;
27162 case NAMELIST_DECL:
27163 gen_namelist_decl (DECL_NAME (decl), context_die,
27164 NAMELIST_DECL_ASSOCIATED_DECL (decl));
27165 break;
27167 default:
27168 /* Probably some frontend-internal decl. Assume we don't care. */
27169 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
27170 break;
27173 return NULL;
27176 /* Output initial debug information for global DECL. Called at the
27177 end of the parsing process.
27179 This is the initial debug generation process. As such, the DIEs
27180 generated may be incomplete. A later debug generation pass
27181 (dwarf2out_late_global_decl) will augment the information generated
27182 in this pass (e.g., with complete location info). */
27184 static void
27185 dwarf2out_early_global_decl (tree decl)
27187 set_early_dwarf s;
27189 /* gen_decl_die() will set DECL_ABSTRACT because
27190 cgraph_function_possibly_inlined_p() returns true. This is in
27191 turn will cause DW_AT_inline attributes to be set.
27193 This happens because at early dwarf generation, there is no
27194 cgraph information, causing cgraph_function_possibly_inlined_p()
27195 to return true. Trick cgraph_function_possibly_inlined_p()
27196 while we generate dwarf early. */
27197 bool save = symtab->global_info_ready;
27198 symtab->global_info_ready = true;
27200 /* We don't handle TYPE_DECLs. If required, they'll be reached via
27201 other DECLs and they can point to template types or other things
27202 that dwarf2out can't handle when done via dwarf2out_decl. */
27203 if (TREE_CODE (decl) != TYPE_DECL
27204 && TREE_CODE (decl) != PARM_DECL)
27206 if (TREE_CODE (decl) == FUNCTION_DECL)
27208 tree save_fndecl = current_function_decl;
27210 /* For nested functions, make sure we have DIEs for the parents first
27211 so that all nested DIEs are generated at the proper scope in the
27212 first shot. */
27213 tree context = decl_function_context (decl);
27214 if (context != NULL)
27216 dw_die_ref context_die = lookup_decl_die (context);
27217 current_function_decl = context;
27219 /* Avoid emitting DIEs multiple times, but still process CONTEXT
27220 enough so that it lands in its own context. This avoids type
27221 pruning issues later on. */
27222 if (context_die == NULL || is_declaration_die (context_die))
27223 dwarf2out_early_global_decl (context);
27226 /* Emit an abstract origin of a function first. This happens
27227 with C++ constructor clones for example and makes
27228 dwarf2out_abstract_function happy which requires the early
27229 DIE of the abstract instance to be present. */
27230 tree origin = DECL_ABSTRACT_ORIGIN (decl);
27231 dw_die_ref origin_die;
27232 if (origin != NULL
27233 /* Do not emit the DIE multiple times but make sure to
27234 process it fully here in case we just saw a declaration. */
27235 && ((origin_die = lookup_decl_die (origin)) == NULL
27236 || is_declaration_die (origin_die)))
27238 current_function_decl = origin;
27239 dwarf2out_decl (origin);
27242 /* Emit the DIE for decl but avoid doing that multiple times. */
27243 dw_die_ref old_die;
27244 if ((old_die = lookup_decl_die (decl)) == NULL
27245 || is_declaration_die (old_die))
27247 current_function_decl = decl;
27248 dwarf2out_decl (decl);
27251 current_function_decl = save_fndecl;
27253 else
27254 dwarf2out_decl (decl);
27256 symtab->global_info_ready = save;
27259 /* Return whether EXPR is an expression with the following pattern:
27260 INDIRECT_REF (NOP_EXPR (INTEGER_CST)). */
27262 static bool
27263 is_trivial_indirect_ref (tree expr)
27265 if (expr == NULL_TREE || TREE_CODE (expr) != INDIRECT_REF)
27266 return false;
27268 tree nop = TREE_OPERAND (expr, 0);
27269 if (nop == NULL_TREE || TREE_CODE (nop) != NOP_EXPR)
27270 return false;
27272 tree int_cst = TREE_OPERAND (nop, 0);
27273 return int_cst != NULL_TREE && TREE_CODE (int_cst) == INTEGER_CST;
27276 /* Output debug information for global decl DECL. Called from
27277 toplev.cc after compilation proper has finished. */
27279 static void
27280 dwarf2out_late_global_decl (tree decl)
27282 /* Fill-in any location information we were unable to determine
27283 on the first pass. */
27284 if (VAR_P (decl))
27286 dw_die_ref die = lookup_decl_die (decl);
27288 /* We may have to generate full debug late for LTO in case debug
27289 was not enabled at compile-time or the target doesn't support
27290 the LTO early debug scheme. */
27291 if (! die && in_lto_p
27292 /* Function scope variables are emitted when emitting the
27293 DIE for the function. */
27294 && ! local_function_static (decl))
27295 dwarf2out_decl (decl);
27296 else if (die)
27298 /* We get called via the symtab code invoking late_global_decl
27299 for symbols that are optimized out.
27301 Do not add locations for those, except if they have a
27302 DECL_VALUE_EXPR, in which case they are relevant for debuggers.
27303 Still don't add a location if the DECL_VALUE_EXPR is not a trivial
27304 INDIRECT_REF expression, as this could generate relocations to
27305 text symbols in LTO object files, which is invalid. */
27306 varpool_node *node = varpool_node::get (decl);
27307 if ((! node || ! node->definition)
27308 && ! (DECL_HAS_VALUE_EXPR_P (decl)
27309 && is_trivial_indirect_ref (DECL_VALUE_EXPR (decl))))
27310 tree_add_const_value_attribute_for_decl (die, decl);
27311 else
27312 add_location_or_const_value_attribute (die, decl, false);
27317 /* Output debug information for type decl DECL. Called from toplev.cc
27318 and from language front ends (to record built-in types). */
27319 static void
27320 dwarf2out_type_decl (tree decl, int local)
27322 if (!local)
27324 set_early_dwarf s;
27325 dwarf2out_decl (decl);
27329 /* Output debug information for imported module or decl DECL.
27330 NAME is non-NULL name in the lexical block if the decl has been renamed.
27331 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
27332 that DECL belongs to.
27333 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
27334 static void
27335 dwarf2out_imported_module_or_decl_1 (tree decl,
27336 tree name,
27337 tree lexical_block,
27338 dw_die_ref lexical_block_die)
27340 expanded_location xloc;
27341 dw_die_ref imported_die = NULL;
27342 dw_die_ref at_import_die;
27344 if (TREE_CODE (decl) == IMPORTED_DECL)
27346 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
27347 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
27348 gcc_assert (decl);
27350 else
27351 xloc = expand_location (input_location);
27353 if (TREE_CODE (decl) == TYPE_DECL)
27355 at_import_die = force_type_die (TREE_TYPE (decl));
27356 /* For namespace N { typedef void T; } using N::T; base_type_die
27357 returns NULL, but DW_TAG_imported_declaration requires
27358 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
27359 if (!at_import_die)
27361 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
27362 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
27363 at_import_die = lookup_type_die (TREE_TYPE (decl));
27364 gcc_assert (at_import_die);
27367 else
27369 at_import_die = lookup_decl_die (decl);
27370 if (!at_import_die)
27372 /* If we're trying to avoid duplicate debug info, we may not have
27373 emitted the member decl for this field. Emit it now. */
27374 if (TREE_CODE (decl) == FIELD_DECL)
27376 tree type = DECL_CONTEXT (decl);
27378 if (TYPE_CONTEXT (type)
27379 && TYPE_P (TYPE_CONTEXT (type))
27380 && !should_emit_struct_debug (TYPE_CONTEXT (type),
27381 DINFO_USAGE_DIR_USE))
27382 return;
27383 gen_type_die_for_member (type, decl,
27384 get_context_die (TYPE_CONTEXT (type)));
27386 if (TREE_CODE (decl) == CONST_DECL)
27388 /* Individual enumerators of an enum type do not get output here
27389 (see gen_decl_die), so we cannot call force_decl_die. */
27390 if (!is_fortran () && !is_ada () && !is_dlang ())
27391 return;
27393 if (TREE_CODE (decl) == NAMELIST_DECL)
27394 at_import_die = gen_namelist_decl (DECL_NAME (decl),
27395 get_context_die (DECL_CONTEXT (decl)),
27396 NULL_TREE);
27397 else
27398 at_import_die = force_decl_die (decl);
27402 if (TREE_CODE (decl) == NAMESPACE_DECL)
27404 if (dwarf_version >= 3 || !dwarf_strict)
27405 imported_die = new_die (DW_TAG_imported_module,
27406 lexical_block_die,
27407 lexical_block);
27408 else
27409 return;
27411 else
27412 imported_die = new_die (DW_TAG_imported_declaration,
27413 lexical_block_die,
27414 lexical_block);
27416 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
27417 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
27418 if (debug_column_info && xloc.column)
27419 add_AT_unsigned (imported_die, DW_AT_decl_column, xloc.column);
27420 if (name)
27421 add_AT_string (imported_die, DW_AT_name,
27422 IDENTIFIER_POINTER (name));
27423 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
27426 /* Output debug information for imported module or decl DECL.
27427 NAME is non-NULL name in context if the decl has been renamed.
27428 CHILD is true if decl is one of the renamed decls as part of
27429 importing whole module.
27430 IMPLICIT is set if this hook is called for an implicit import
27431 such as inline namespace. */
27433 static void
27434 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
27435 bool child, bool implicit)
27437 /* dw_die_ref at_import_die; */
27438 dw_die_ref scope_die;
27440 if (debug_info_level <= DINFO_LEVEL_TERSE)
27441 return;
27443 gcc_assert (decl);
27445 /* For DWARF5, just DW_AT_export_symbols on the DW_TAG_namespace
27446 should be enough, for DWARF4 and older even if we emit as extension
27447 DW_AT_export_symbols add the implicit DW_TAG_imported_module anyway
27448 for the benefit of consumers unaware of DW_AT_export_symbols. */
27449 if (implicit
27450 && dwarf_version >= 5
27451 && lang_hooks.decls.decl_dwarf_attribute (decl,
27452 DW_AT_export_symbols) == 1)
27453 return;
27455 set_early_dwarf s;
27457 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
27458 We need decl DIE for reference and scope die. First, get DIE for the decl
27459 itself. */
27461 /* Get the scope die for decl context. Use comp_unit_die for global module
27462 or decl. If die is not found for non globals, force new die. */
27463 if (context
27464 && TYPE_P (context)
27465 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
27466 return;
27468 scope_die = get_context_die (context);
27470 if (child)
27472 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
27473 there is nothing we can do, here. */
27474 if (dwarf_version < 3 && dwarf_strict)
27475 return;
27477 gcc_assert (scope_die->die_child);
27478 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
27479 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
27480 scope_die = scope_die->die_child;
27483 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
27484 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
27487 /* Output debug information for namelists. */
27489 static dw_die_ref
27490 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
27492 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
27493 tree value;
27494 unsigned i;
27496 if (debug_info_level <= DINFO_LEVEL_TERSE)
27497 return NULL;
27499 gcc_assert (scope_die != NULL);
27500 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
27501 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
27503 /* If there are no item_decls, we have a nondefining namelist, e.g.
27504 with USE association; hence, set DW_AT_declaration. */
27505 if (item_decls == NULL_TREE)
27507 add_AT_flag (nml_die, DW_AT_declaration, 1);
27508 return nml_die;
27511 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
27513 nml_item_ref_die = lookup_decl_die (value);
27514 if (!nml_item_ref_die)
27515 nml_item_ref_die = force_decl_die (value);
27517 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
27518 add_AT_die_ref (nml_item_die, DW_AT_namelist_item, nml_item_ref_die);
27520 return nml_die;
27524 /* Write the debugging output for DECL and return the DIE. */
27526 static void
27527 dwarf2out_decl (tree decl)
27529 dw_die_ref context_die = comp_unit_die ();
27531 switch (TREE_CODE (decl))
27533 case ERROR_MARK:
27534 return;
27536 case FUNCTION_DECL:
27537 /* If we're a nested function, initially use a parent of NULL; if we're
27538 a plain function, this will be fixed up in decls_for_scope. If
27539 we're a method, it will be ignored, since we already have a DIE.
27540 Avoid doing this late though since clones of class methods may
27541 otherwise end up in limbo and create type DIEs late. */
27542 if (early_dwarf
27543 && decl_function_context (decl)
27544 /* But if we're in terse mode, we don't care about scope. */
27545 && debug_info_level > DINFO_LEVEL_TERSE)
27546 context_die = NULL;
27547 break;
27549 case VAR_DECL:
27550 /* For local statics lookup proper context die. */
27551 if (local_function_static (decl))
27552 context_die = lookup_decl_die (DECL_CONTEXT (decl));
27554 /* If we are in terse mode, don't generate any DIEs to represent any
27555 variable declarations or definitions unless it is external. */
27556 if (debug_info_level < DINFO_LEVEL_TERSE
27557 || (debug_info_level == DINFO_LEVEL_TERSE
27558 && !TREE_PUBLIC (decl)))
27559 return;
27560 break;
27562 case CONST_DECL:
27563 if (debug_info_level <= DINFO_LEVEL_TERSE)
27564 return;
27565 if (!is_fortran () && !is_ada () && !is_dlang ())
27566 return;
27567 if (TREE_STATIC (decl) && decl_function_context (decl))
27568 context_die = lookup_decl_die (DECL_CONTEXT (decl));
27569 break;
27571 case NAMESPACE_DECL:
27572 case IMPORTED_DECL:
27573 if (debug_info_level <= DINFO_LEVEL_TERSE)
27574 return;
27575 if (lookup_decl_die (decl) != NULL)
27576 return;
27577 break;
27579 case TYPE_DECL:
27580 /* Don't emit stubs for types unless they are needed by other DIEs. */
27581 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
27582 return;
27584 /* Don't bother trying to generate any DIEs to represent any of the
27585 normal built-in types for the language we are compiling. */
27586 if (DECL_IS_UNDECLARED_BUILTIN (decl))
27587 return;
27589 /* If we are in terse mode, don't generate any DIEs for types. */
27590 if (debug_info_level <= DINFO_LEVEL_TERSE)
27591 return;
27593 /* If we're a function-scope tag, initially use a parent of NULL;
27594 this will be fixed up in decls_for_scope. */
27595 if (decl_function_context (decl))
27596 context_die = NULL;
27598 break;
27600 case NAMELIST_DECL:
27601 break;
27603 default:
27604 return;
27607 gen_decl_die (decl, NULL, NULL, context_die);
27609 if (flag_checking)
27611 dw_die_ref die = lookup_decl_die (decl);
27612 if (die)
27613 check_die (die);
27617 /* Write the debugging output for DECL. */
27619 static void
27620 dwarf2out_function_decl (tree decl)
27622 dwarf2out_decl (decl);
27623 call_arg_locations = NULL;
27624 call_arg_loc_last = NULL;
27625 call_site_count = -1;
27626 tail_call_site_count = -1;
27627 decl_loc_table->empty ();
27628 cached_dw_loc_list_table->empty ();
27631 /* Output a marker (i.e. a label) for the beginning of the generated code for
27632 a lexical block. */
27634 static void
27635 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
27636 unsigned int blocknum)
27638 switch_to_section (current_function_section ());
27639 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
27642 /* Output a marker (i.e. a label) for the end of the generated code for a
27643 lexical block. */
27645 static void
27646 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
27648 switch_to_section (current_function_section ());
27649 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
27652 /* Returns true if it is appropriate not to emit any debugging
27653 information for BLOCK, because it doesn't contain any instructions.
27655 Don't allow this for blocks with nested functions or local classes
27656 as we would end up with orphans, and in the presence of scheduling
27657 we may end up calling them anyway. */
27659 static bool
27660 dwarf2out_ignore_block (const_tree block)
27662 tree decl;
27663 unsigned int i;
27665 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
27666 if (TREE_CODE (decl) == FUNCTION_DECL
27667 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
27668 return false;
27669 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
27671 decl = BLOCK_NONLOCALIZED_VAR (block, i);
27672 if (TREE_CODE (decl) == FUNCTION_DECL
27673 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
27674 return false;
27677 return true;
27680 /* Hash table routines for file_hash. */
27682 bool
27683 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
27685 return filename_cmp (p1->key, p2) == 0;
27688 hashval_t
27689 dwarf_file_hasher::hash (dwarf_file_data *p)
27691 return htab_hash_string (p->key);
27694 /* Lookup FILE_NAME (in the list of filenames that we know about here in
27695 dwarf2out.cc) and return its "index". The index of each (known) filename is
27696 just a unique number which is associated with only that one filename. We
27697 need such numbers for the sake of generating labels (in the .debug_sfnames
27698 section) and references to those files numbers (in the .debug_srcinfo
27699 and .debug_macinfo sections). If the filename given as an argument is not
27700 found in our current list, add it to the list and assign it the next
27701 available unique index number. */
27703 static struct dwarf_file_data *
27704 lookup_filename (const char *file_name)
27706 struct dwarf_file_data * created;
27708 if (!file_name)
27709 return NULL;
27711 if (!file_name[0])
27712 file_name = "<stdin>";
27714 dwarf_file_data **slot
27715 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
27716 INSERT);
27717 if (*slot)
27718 return *slot;
27720 created = ggc_alloc<dwarf_file_data> ();
27721 created->key = file_name;
27722 created->filename = remap_debug_filename (file_name);
27723 created->emitted_number = 0;
27724 *slot = created;
27725 return created;
27728 /* If the assembler will construct the file table, then translate the compiler
27729 internal file table number into the assembler file table number, and emit
27730 a .file directive if we haven't already emitted one yet. The file table
27731 numbers are different because we prune debug info for unused variables and
27732 types, which may include filenames. */
27734 static int
27735 maybe_emit_file (struct dwarf_file_data * fd)
27737 if (! fd->emitted_number)
27739 if (last_emitted_file)
27740 fd->emitted_number = last_emitted_file->emitted_number + 1;
27741 else
27742 fd->emitted_number = 1;
27743 last_emitted_file = fd;
27745 if (output_asm_line_debug_info ())
27747 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
27748 output_quoted_string (asm_out_file, fd->filename);
27749 fputc ('\n', asm_out_file);
27753 return fd->emitted_number;
27756 /* Schedule generation of a DW_AT_const_value attribute to DIE.
27757 That generation should happen after function debug info has been
27758 generated. The value of the attribute is the constant value of ARG. */
27760 static void
27761 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
27763 die_arg_entry entry;
27765 if (!die || !arg)
27766 return;
27768 gcc_assert (early_dwarf);
27770 if (!tmpl_value_parm_die_table)
27771 vec_alloc (tmpl_value_parm_die_table, 32);
27773 entry.die = die;
27774 entry.arg = arg;
27775 vec_safe_push (tmpl_value_parm_die_table, entry);
27778 /* Return TRUE if T is an instance of generic type, FALSE
27779 otherwise. */
27781 static bool
27782 generic_type_p (tree t)
27784 if (t == NULL_TREE || !TYPE_P (t))
27785 return false;
27786 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
27789 /* Schedule the generation of the generic parameter dies for the
27790 instance of generic type T. The proper generation itself is later
27791 done by gen_scheduled_generic_parms_dies. */
27793 static void
27794 schedule_generic_params_dies_gen (tree t)
27796 if (!generic_type_p (t))
27797 return;
27799 gcc_assert (early_dwarf);
27801 if (!generic_type_instances)
27802 vec_alloc (generic_type_instances, 256);
27804 vec_safe_push (generic_type_instances, t);
27807 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
27808 by append_entry_to_tmpl_value_parm_die_table. This function must
27809 be called after function DIEs have been generated. */
27811 static void
27812 gen_remaining_tmpl_value_param_die_attribute (void)
27814 if (tmpl_value_parm_die_table)
27816 unsigned i, j;
27817 die_arg_entry *e;
27819 /* We do this in two phases - first get the cases we can
27820 handle during early-finish, preserving those we cannot
27821 (containing symbolic constants where we don't yet know
27822 whether we are going to output the referenced symbols).
27823 For those we try again at late-finish. */
27824 j = 0;
27825 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
27827 if (!e->die->removed
27828 && !tree_add_const_value_attribute (e->die, e->arg))
27830 dw_loc_descr_ref loc = NULL;
27831 if (! early_dwarf
27832 && (dwarf_version >= 5 || !dwarf_strict))
27833 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
27834 if (loc)
27835 add_AT_loc (e->die, DW_AT_location, loc);
27836 else
27837 (*tmpl_value_parm_die_table)[j++] = *e;
27840 tmpl_value_parm_die_table->truncate (j);
27844 /* Generate generic parameters DIEs for instances of generic types
27845 that have been previously scheduled by
27846 schedule_generic_params_dies_gen. This function must be called
27847 after all the types of the CU have been laid out. */
27849 static void
27850 gen_scheduled_generic_parms_dies (void)
27852 unsigned i;
27853 tree t;
27855 if (!generic_type_instances)
27856 return;
27858 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
27859 if (COMPLETE_TYPE_P (t))
27860 gen_generic_params_dies (t);
27862 generic_type_instances = NULL;
27866 /* Replace DW_AT_name for the decl with name. */
27868 static void
27869 dwarf2out_set_name (tree decl, tree name)
27871 dw_die_ref die;
27872 dw_attr_node *attr;
27873 const char *dname;
27875 die = TYPE_SYMTAB_DIE (decl);
27876 if (!die)
27877 return;
27879 dname = dwarf2_name (name, 0);
27880 if (!dname)
27881 return;
27883 attr = get_AT (die, DW_AT_name);
27884 if (attr)
27886 struct indirect_string_node *node;
27888 node = find_AT_string (dname);
27889 /* replace the string. */
27890 attr->dw_attr_val.v.val_str = node;
27893 else
27894 add_name_attribute (die, dname);
27897 /* True if before or during processing of the first function being emitted. */
27898 static bool in_first_function_p = true;
27899 /* True if loc_note during dwarf2out_var_location call might still be
27900 before first real instruction at address equal to .Ltext0. */
27901 static bool maybe_at_text_label_p = true;
27902 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
27903 static unsigned int first_loclabel_num_not_at_text_label;
27905 /* Look ahead for a real insn. */
27907 static rtx_insn *
27908 dwarf2out_next_real_insn (rtx_insn *loc_note)
27910 rtx_insn *next_real = NEXT_INSN (loc_note);
27912 while (next_real)
27913 if (INSN_P (next_real))
27914 break;
27915 else
27916 next_real = NEXT_INSN (next_real);
27918 return next_real;
27921 /* Called by the final INSN scan whenever we see a var location. We
27922 use it to drop labels in the right places, and throw the location in
27923 our lookup table. */
27925 static void
27926 dwarf2out_var_location (rtx_insn *loc_note)
27928 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
27929 struct var_loc_node *newloc;
27930 rtx_insn *next_real;
27931 rtx_insn *call_insn = NULL;
27932 static const char *last_label;
27933 static const char *last_postcall_label;
27934 static bool last_in_cold_section_p;
27935 static rtx_insn *expected_next_loc_note;
27936 tree decl;
27937 bool var_loc_p;
27938 var_loc_view view = 0;
27940 if (!NOTE_P (loc_note))
27942 if (CALL_P (loc_note))
27944 maybe_reset_location_view (loc_note, cur_line_info_table);
27945 call_site_count++;
27946 if (SIBLING_CALL_P (loc_note))
27947 tail_call_site_count++;
27948 if (find_reg_note (loc_note, REG_CALL_ARG_LOCATION, NULL_RTX))
27950 call_insn = loc_note;
27951 loc_note = NULL;
27952 var_loc_p = false;
27954 next_real = dwarf2out_next_real_insn (call_insn);
27955 cached_next_real_insn = NULL;
27956 goto create_label;
27958 if (optimize == 0 && !flag_var_tracking)
27960 /* When the var-tracking pass is not running, there is no note
27961 for indirect calls whose target is compile-time known. In this
27962 case, process such calls specifically so that we generate call
27963 sites for them anyway. */
27964 rtx x = PATTERN (loc_note);
27965 if (GET_CODE (x) == PARALLEL)
27966 x = XVECEXP (x, 0, 0);
27967 if (GET_CODE (x) == SET)
27968 x = SET_SRC (x);
27969 if (GET_CODE (x) == CALL)
27970 x = XEXP (x, 0);
27971 if (!MEM_P (x)
27972 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
27973 || !SYMBOL_REF_DECL (XEXP (x, 0))
27974 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
27975 != FUNCTION_DECL))
27977 call_insn = loc_note;
27978 loc_note = NULL;
27979 var_loc_p = false;
27981 next_real = dwarf2out_next_real_insn (call_insn);
27982 cached_next_real_insn = NULL;
27983 goto create_label;
27987 else if (!debug_variable_location_views)
27988 gcc_unreachable ();
27989 else
27990 maybe_reset_location_view (loc_note, cur_line_info_table);
27992 return;
27995 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
27996 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
27997 return;
27999 /* Optimize processing a large consecutive sequence of location
28000 notes so we don't spend too much time in next_real_insn. If the
28001 next insn is another location note, remember the next_real_insn
28002 calculation for next time. */
28003 next_real = cached_next_real_insn;
28004 if (next_real)
28006 if (expected_next_loc_note != loc_note)
28007 next_real = NULL;
28010 if (! next_real)
28011 next_real = dwarf2out_next_real_insn (loc_note);
28013 if (next_real)
28015 rtx_insn *next_note = NEXT_INSN (loc_note);
28016 while (next_note != next_real)
28018 if (! next_note->deleted ()
28019 && NOTE_P (next_note)
28020 && NOTE_KIND (next_note) == NOTE_INSN_VAR_LOCATION)
28021 break;
28022 next_note = NEXT_INSN (next_note);
28025 if (next_note == next_real)
28026 cached_next_real_insn = NULL;
28027 else
28029 expected_next_loc_note = next_note;
28030 cached_next_real_insn = next_real;
28033 else
28034 cached_next_real_insn = NULL;
28036 /* If there are no instructions which would be affected by this note,
28037 don't do anything. */
28038 if (var_loc_p
28039 && next_real == NULL_RTX
28040 && !NOTE_DURING_CALL_P (loc_note))
28041 return;
28043 create_label:
28045 if (next_real == NULL_RTX)
28046 next_real = get_last_insn ();
28048 /* If there were any real insns between note we processed last time
28049 and this note (or if it is the first note), clear
28050 last_{,postcall_}label so that they are not reused this time. */
28051 if (last_var_location_insn == NULL_RTX
28052 || last_var_location_insn != next_real
28053 || last_in_cold_section_p != in_cold_section_p)
28055 last_label = NULL;
28056 last_postcall_label = NULL;
28059 if (var_loc_p)
28061 const char *label
28062 = NOTE_DURING_CALL_P (loc_note) ? last_postcall_label : last_label;
28063 view = cur_line_info_table->view;
28064 decl = NOTE_VAR_LOCATION_DECL (loc_note);
28065 newloc = add_var_loc_to_decl (decl, loc_note, label, view);
28066 if (newloc == NULL)
28067 return;
28069 else
28071 decl = NULL_TREE;
28072 newloc = NULL;
28075 /* If there were no real insns between note we processed last time
28076 and this note, use the label we emitted last time. Otherwise
28077 create a new label and emit it. */
28078 if (last_label == NULL)
28080 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
28081 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
28082 loclabel_num++;
28083 last_label = ggc_strdup (loclabel);
28084 /* See if loclabel might be equal to .Ltext0. If yes,
28085 bump first_loclabel_num_not_at_text_label. */
28086 if (!have_multiple_function_sections
28087 && in_first_function_p
28088 && maybe_at_text_label_p)
28090 static rtx_insn *last_start;
28091 rtx_insn *insn;
28092 for (insn = loc_note; insn; insn = previous_insn (insn))
28093 if (insn == last_start)
28094 break;
28095 else if (!NONDEBUG_INSN_P (insn))
28096 continue;
28097 else
28099 rtx body = PATTERN (insn);
28100 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
28101 continue;
28102 /* Inline asm could occupy zero bytes. */
28103 else if (GET_CODE (body) == ASM_INPUT
28104 || asm_noperands (body) >= 0)
28105 continue;
28106 #ifdef HAVE_ATTR_length /* ??? We don't include insn-attr.h. */
28107 else if (HAVE_ATTR_length && get_attr_min_length (insn) == 0)
28108 continue;
28109 #endif
28110 else
28112 /* Assume insn has non-zero length. */
28113 maybe_at_text_label_p = false;
28114 break;
28117 if (maybe_at_text_label_p)
28119 last_start = loc_note;
28120 first_loclabel_num_not_at_text_label = loclabel_num;
28125 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
28126 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
28128 if (!var_loc_p)
28130 struct call_arg_loc_node *ca_loc
28131 = ggc_cleared_alloc<call_arg_loc_node> ();
28132 rtx_insn *prev = call_insn;
28134 ca_loc->call_arg_loc_note
28135 = find_reg_note (call_insn, REG_CALL_ARG_LOCATION, NULL_RTX);
28136 ca_loc->next = NULL;
28137 ca_loc->label = last_label;
28138 gcc_assert (prev
28139 && (CALL_P (prev)
28140 || (NONJUMP_INSN_P (prev)
28141 && GET_CODE (PATTERN (prev)) == SEQUENCE
28142 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
28143 if (!CALL_P (prev))
28144 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
28145 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
28147 /* Look for a SYMBOL_REF in the "prev" instruction. */
28148 rtx x = get_call_rtx_from (prev);
28149 if (x)
28151 /* Try to get the call symbol, if any. */
28152 if (MEM_P (XEXP (x, 0)))
28153 x = XEXP (x, 0);
28154 /* First, look for a memory access to a symbol_ref. */
28155 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
28156 && SYMBOL_REF_DECL (XEXP (x, 0))
28157 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
28158 ca_loc->symbol_ref = XEXP (x, 0);
28159 /* Otherwise, look at a compile-time known user-level function
28160 declaration. */
28161 else if (MEM_P (x)
28162 && MEM_EXPR (x)
28163 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
28164 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
28167 ca_loc->block = insn_scope (prev);
28168 if (call_arg_locations)
28169 call_arg_loc_last->next = ca_loc;
28170 else
28171 call_arg_locations = ca_loc;
28172 call_arg_loc_last = ca_loc;
28174 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
28176 newloc->label = last_label;
28177 newloc->view = view;
28179 else
28181 if (!last_postcall_label)
28183 sprintf (loclabel, "%s-1", last_label);
28184 last_postcall_label = ggc_strdup (loclabel);
28186 newloc->label = last_postcall_label;
28187 /* ??? This view is at last_label, not last_label-1, but we
28188 could only assume view at last_label-1 is zero if we could
28189 assume calls always have length greater than one. This is
28190 probably true in general, though there might be a rare
28191 exception to this rule, e.g. if a call insn is optimized out
28192 by target magic. Then, even the -1 in the label will be
28193 wrong, which might invalidate the range. Anyway, using view,
28194 though technically possibly incorrect, will work as far as
28195 ranges go: since L-1 is in the middle of the call insn,
28196 (L-1).0 and (L-1).V shouldn't make any difference, and having
28197 the loclist entry refer to the .loc entry might be useful, so
28198 leave it like this. */
28199 newloc->view = view;
28202 if (var_loc_p && flag_debug_asm)
28204 const char *name, *sep, *patstr;
28205 if (decl && DECL_NAME (decl))
28206 name = IDENTIFIER_POINTER (DECL_NAME (decl));
28207 else
28208 name = "";
28209 if (NOTE_VAR_LOCATION_LOC (loc_note))
28211 sep = " => ";
28212 patstr = str_pattern_slim (NOTE_VAR_LOCATION_LOC (loc_note));
28214 else
28216 sep = " ";
28217 patstr = "RESET";
28219 fprintf (asm_out_file, "\t%s DEBUG %s%s%s\n", ASM_COMMENT_START,
28220 name, sep, patstr);
28223 last_var_location_insn = next_real;
28224 last_in_cold_section_p = in_cold_section_p;
28227 /* Check whether BLOCK, a lexical block, is nested within OUTER, or is
28228 OUTER itself. If BOTHWAYS, check not only that BLOCK can reach
28229 OUTER through BLOCK_SUPERCONTEXT links, but also that there is a
28230 path from OUTER to BLOCK through BLOCK_SUBBLOCKs and
28231 BLOCK_FRAGMENT_ORIGIN links. */
28232 static bool
28233 block_within_block_p (tree block, tree outer, bool bothways)
28235 if (block == outer)
28236 return true;
28238 /* Quickly check that OUTER is up BLOCK's supercontext chain. */
28239 for (tree context = BLOCK_SUPERCONTEXT (block);
28240 context != outer;
28241 context = BLOCK_SUPERCONTEXT (context))
28242 if (!context || TREE_CODE (context) != BLOCK)
28243 return false;
28245 if (!bothways)
28246 return true;
28248 /* Now check that each block is actually referenced by its
28249 parent. */
28250 for (tree context = BLOCK_SUPERCONTEXT (block); ;
28251 context = BLOCK_SUPERCONTEXT (context))
28253 if (BLOCK_FRAGMENT_ORIGIN (context))
28255 gcc_assert (!BLOCK_SUBBLOCKS (context));
28256 context = BLOCK_FRAGMENT_ORIGIN (context);
28258 for (tree sub = BLOCK_SUBBLOCKS (context);
28259 sub != block;
28260 sub = BLOCK_CHAIN (sub))
28261 if (!sub)
28262 return false;
28263 if (context == outer)
28264 return true;
28265 else
28266 block = context;
28270 /* Called during final while assembling the marker of the entry point
28271 for an inlined function. */
28273 static void
28274 dwarf2out_inline_entry (tree block)
28276 gcc_assert (debug_inline_points);
28278 /* If we can't represent it, don't bother. */
28279 if (!(dwarf_version >= 3 || !dwarf_strict))
28280 return;
28282 gcc_assert (DECL_P (block_ultimate_origin (block)));
28284 /* Sanity check the block tree. This would catch a case in which
28285 BLOCK got removed from the tree reachable from the outermost
28286 lexical block, but got retained in markers. It would still link
28287 back to its parents, but some ancestor would be missing a link
28288 down the path to the sub BLOCK. If the block got removed, its
28289 BLOCK_NUMBER will not be a usable value. */
28290 if (flag_checking)
28291 gcc_assert (block_within_block_p (block,
28292 DECL_INITIAL (current_function_decl),
28293 true));
28295 gcc_assert (inlined_function_outer_scope_p (block));
28296 gcc_assert (!lookup_block_die (block));
28298 if (BLOCK_FRAGMENT_ORIGIN (block))
28299 block = BLOCK_FRAGMENT_ORIGIN (block);
28300 /* Can the entry point ever not be at the beginning of an
28301 unfragmented lexical block? */
28302 else if (!(BLOCK_FRAGMENT_CHAIN (block)
28303 || (cur_line_info_table
28304 && !ZERO_VIEW_P (cur_line_info_table->view))))
28305 return;
28307 if (!inline_entry_data_table)
28308 inline_entry_data_table
28309 = hash_table<inline_entry_data_hasher>::create_ggc (10);
28312 inline_entry_data **iedp
28313 = inline_entry_data_table->find_slot_with_hash (block,
28314 htab_hash_pointer (block),
28315 INSERT);
28316 if (*iedp)
28317 /* ??? Ideally, we'd record all entry points for the same inlined
28318 function (some may have been duplicated by e.g. unrolling), but
28319 we have no way to represent that ATM. */
28320 return;
28322 inline_entry_data *ied = *iedp = ggc_cleared_alloc<inline_entry_data> ();
28323 ied->block = block;
28324 ied->label_pfx = BLOCK_INLINE_ENTRY_LABEL;
28325 ied->label_num = BLOCK_NUMBER (block);
28326 if (cur_line_info_table)
28327 ied->view = cur_line_info_table->view;
28329 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_INLINE_ENTRY_LABEL,
28330 BLOCK_NUMBER (block));
28333 /* Called from finalize_size_functions for size functions so that their body
28334 can be encoded in the debug info to describe the layout of variable-length
28335 structures. */
28337 static void
28338 dwarf2out_size_function (tree decl)
28340 set_early_dwarf s;
28341 function_to_dwarf_procedure (decl);
28344 /* Note in one location list that text section has changed. */
28347 var_location_switch_text_section_1 (var_loc_list **slot, void *)
28349 var_loc_list *list = *slot;
28350 if (list->first)
28351 list->last_before_switch
28352 = list->last->next ? list->last->next : list->last;
28353 return 1;
28356 /* Note in all location lists that text section has changed. */
28358 static void
28359 var_location_switch_text_section (void)
28361 if (decl_loc_table == NULL)
28362 return;
28364 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
28367 /* Create a new line number table. */
28369 static dw_line_info_table *
28370 new_line_info_table (void)
28372 dw_line_info_table *table;
28374 table = ggc_cleared_alloc<dw_line_info_table> ();
28375 table->file_num = 1;
28376 table->line_num = 1;
28377 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
28378 FORCE_RESET_NEXT_VIEW (table->view);
28379 table->symviews_since_reset = 0;
28381 return table;
28384 /* Lookup the "current" table into which we emit line info, so
28385 that we don't have to do it for every source line. */
28387 static void
28388 set_cur_line_info_table (section *sec)
28390 dw_line_info_table *table;
28392 if (sec == text_section)
28393 table = text_section_line_info;
28394 else if (sec == cold_text_section)
28396 table = cold_text_section_line_info;
28397 if (!table)
28399 cold_text_section_line_info = table = new_line_info_table ();
28400 table->end_label = cold_end_label;
28403 else
28405 const char *end_label;
28407 if (crtl->has_bb_partition)
28409 if (in_cold_section_p)
28410 end_label = crtl->subsections.cold_section_end_label;
28411 else
28412 end_label = crtl->subsections.hot_section_end_label;
28414 else
28416 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28417 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
28418 current_function_funcdef_no);
28419 end_label = ggc_strdup (label);
28422 table = new_line_info_table ();
28423 table->end_label = end_label;
28425 vec_safe_push (separate_line_info, table);
28428 if (output_asm_line_debug_info ())
28429 table->is_stmt = (cur_line_info_table
28430 ? cur_line_info_table->is_stmt
28431 : DWARF_LINE_DEFAULT_IS_STMT_START);
28432 cur_line_info_table = table;
28436 /* We need to reset the locations at the beginning of each
28437 function. We can't do this in the end_function hook, because the
28438 declarations that use the locations won't have been output when
28439 that hook is called. Also compute have_multiple_function_sections here. */
28441 static void
28442 dwarf2out_begin_function (tree fun)
28444 section *sec = function_section (fun);
28446 if (sec != text_section)
28447 have_multiple_function_sections = true;
28449 if (crtl->has_bb_partition && !cold_text_section)
28451 gcc_assert (current_function_decl == fun);
28452 cold_text_section = unlikely_text_section ();
28453 switch_to_section (cold_text_section);
28454 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
28455 switch_to_section (sec);
28458 call_site_count = 0;
28459 tail_call_site_count = 0;
28461 set_cur_line_info_table (sec);
28462 FORCE_RESET_NEXT_VIEW (cur_line_info_table->view);
28465 /* Helper function of dwarf2out_end_function, called only after emitting
28466 the very first function into assembly. Check if some .debug_loc range
28467 might end with a .LVL* label that could be equal to .Ltext0.
28468 In that case we must force using absolute addresses in .debug_loc ranges,
28469 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
28470 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
28471 list terminator.
28472 Set have_multiple_function_sections to true in that case and
28473 terminate htab traversal. */
28476 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
28478 var_loc_list *entry = *slot;
28479 struct var_loc_node *node;
28481 node = entry->first;
28482 if (node && node->next && node->next->label)
28484 unsigned int i;
28485 const char *label = node->next->label;
28486 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
28488 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
28490 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
28491 if (strcmp (label, loclabel) == 0)
28493 have_multiple_function_sections = true;
28494 return 0;
28498 return 1;
28501 /* Hook called after emitting a function into assembly.
28502 This does something only for the very first function emitted. */
28504 static void
28505 dwarf2out_end_function (unsigned int)
28507 if (in_first_function_p
28508 && !have_multiple_function_sections
28509 && first_loclabel_num_not_at_text_label
28510 && decl_loc_table)
28511 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
28512 in_first_function_p = false;
28513 maybe_at_text_label_p = false;
28516 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
28517 front-ends register a translation unit even before dwarf2out_init is
28518 called. */
28519 static tree main_translation_unit = NULL_TREE;
28521 /* Hook called by front-ends after they built their main translation unit.
28522 Associate comp_unit_die to UNIT. */
28524 static void
28525 dwarf2out_register_main_translation_unit (tree unit)
28527 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
28528 && main_translation_unit == NULL_TREE);
28529 main_translation_unit = unit;
28530 /* If dwarf2out_init has not been called yet, it will perform the association
28531 itself looking at main_translation_unit. */
28532 if (decl_die_table != NULL)
28533 equate_decl_number_to_die (unit, comp_unit_die ());
28536 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
28538 static void
28539 push_dw_line_info_entry (dw_line_info_table *table,
28540 enum dw_line_info_opcode opcode, unsigned int val)
28542 dw_line_info_entry e;
28543 e.opcode = opcode;
28544 e.val = val;
28545 vec_safe_push (table->entries, e);
28548 /* Output a label to mark the beginning of a source code line entry
28549 and record information relating to this source line, in
28550 'line_info_table' for later output of the .debug_line section. */
28551 /* ??? The discriminator parameter ought to be unsigned. */
28553 static void
28554 dwarf2out_source_line (unsigned int line, unsigned int column,
28555 const char *filename,
28556 int discriminator, bool is_stmt)
28558 unsigned int file_num;
28559 dw_line_info_table *table;
28560 static var_loc_view lvugid;
28562 /* 'line_info_table' information gathering is not needed when the debug
28563 info level is set to the lowest value. Also, the current DWARF-based
28564 debug formats do not use this info. */
28565 if (debug_info_level < DINFO_LEVEL_TERSE || !dwarf_debuginfo_p ())
28566 return;
28568 table = cur_line_info_table;
28570 if (line == 0)
28572 if (debug_variable_location_views
28573 && output_asm_line_debug_info ()
28574 && table && !RESETTING_VIEW_P (table->view))
28576 /* If we're using the assembler to compute view numbers, we
28577 can't issue a .loc directive for line zero, so we can't
28578 get a view number at this point. We might attempt to
28579 compute it from the previous view, or equate it to a
28580 subsequent view (though it might not be there!), but
28581 since we're omitting the line number entry, we might as
28582 well omit the view number as well. That means pretending
28583 it's a view number zero, which might very well turn out
28584 to be correct. ??? Extend the assembler so that the
28585 compiler could emit e.g. ".locview .LVU#", to output a
28586 view without changing line number information. We'd then
28587 have to count it in symviews_since_reset; when it's omitted,
28588 it doesn't count. */
28589 if (!zero_view_p)
28590 zero_view_p = BITMAP_GGC_ALLOC ();
28591 bitmap_set_bit (zero_view_p, table->view);
28592 if (flag_debug_asm)
28594 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28595 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", table->view);
28596 fprintf (asm_out_file, "\t%s line 0, omitted view ",
28597 ASM_COMMENT_START);
28598 assemble_name (asm_out_file, label);
28599 putc ('\n', asm_out_file);
28601 table->view = ++lvugid;
28603 return;
28606 /* The discriminator column was added in dwarf4. Simplify the below
28607 by simply removing it if we're not supposed to output it. */
28608 if (dwarf_version < 4 && dwarf_strict)
28609 discriminator = 0;
28611 if (!debug_column_info)
28612 column = 0;
28614 file_num = maybe_emit_file (lookup_filename (filename));
28616 /* ??? TODO: Elide duplicate line number entries. Traditionally,
28617 the debugger has used the second (possibly duplicate) line number
28618 at the beginning of the function to mark the end of the prologue.
28619 We could eliminate any other duplicates within the function. For
28620 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
28621 that second line number entry. */
28622 /* Recall that this end-of-prologue indication is *not* the same thing
28623 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
28624 to which the hook corresponds, follows the last insn that was
28625 emitted by gen_prologue. What we need is to precede the first insn
28626 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
28627 insn that corresponds to something the user wrote. These may be
28628 very different locations once scheduling is enabled. */
28630 if (0 && file_num == table->file_num
28631 && line == table->line_num
28632 && column == table->column_num
28633 && discriminator == table->discrim_num
28634 && is_stmt == table->is_stmt)
28635 return;
28637 switch_to_section (current_function_section ());
28639 /* If requested, emit something human-readable. */
28640 if (flag_debug_asm)
28642 if (debug_column_info)
28643 fprintf (asm_out_file, "\t%s %s:%d:%d\n", ASM_COMMENT_START,
28644 filename, line, column);
28645 else
28646 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
28647 filename, line);
28650 if (output_asm_line_debug_info ())
28652 /* Emit the .loc directive understood by GNU as. */
28653 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
28654 file_num, line, is_stmt, discriminator */
28655 fputs ("\t.loc ", asm_out_file);
28656 fprint_ul (asm_out_file, file_num);
28657 putc (' ', asm_out_file);
28658 fprint_ul (asm_out_file, line);
28659 putc (' ', asm_out_file);
28660 fprint_ul (asm_out_file, column);
28662 if (is_stmt != table->is_stmt)
28664 #if HAVE_GAS_LOC_STMT
28665 fputs (" is_stmt ", asm_out_file);
28666 putc (is_stmt ? '1' : '0', asm_out_file);
28667 #endif
28669 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
28671 gcc_assert (discriminator > 0);
28672 fputs (" discriminator ", asm_out_file);
28673 fprint_ul (asm_out_file, (unsigned long) discriminator);
28675 if (debug_variable_location_views)
28677 if (!RESETTING_VIEW_P (table->view))
28679 table->symviews_since_reset++;
28680 if (table->symviews_since_reset > symview_upper_bound)
28681 symview_upper_bound = table->symviews_since_reset;
28682 /* When we're using the assembler to compute view
28683 numbers, we output symbolic labels after "view" in
28684 .loc directives, and the assembler will set them for
28685 us, so that we can refer to the view numbers in
28686 location lists. The only exceptions are when we know
28687 a view will be zero: "-0" is a forced reset, used
28688 e.g. in the beginning of functions, whereas "0" tells
28689 the assembler to check that there was a PC change
28690 since the previous view, in a way that implicitly
28691 resets the next view. */
28692 fputs (" view ", asm_out_file);
28693 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28694 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", table->view);
28695 assemble_name (asm_out_file, label);
28696 table->view = ++lvugid;
28698 else
28700 table->symviews_since_reset = 0;
28701 if (FORCE_RESETTING_VIEW_P (table->view))
28702 fputs (" view -0", asm_out_file);
28703 else
28704 fputs (" view 0", asm_out_file);
28705 /* Mark the present view as a zero view. Earlier debug
28706 binds may have already added its id to loclists to be
28707 emitted later, so we can't reuse the id for something
28708 else. However, it's good to know whether a view is
28709 known to be zero, because then we may be able to
28710 optimize out locviews that are all zeros, so take
28711 note of it in zero_view_p. */
28712 if (!zero_view_p)
28713 zero_view_p = BITMAP_GGC_ALLOC ();
28714 bitmap_set_bit (zero_view_p, lvugid);
28715 table->view = ++lvugid;
28718 putc ('\n', asm_out_file);
28720 else
28722 unsigned int label_num = ++line_info_label_num;
28724 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
28726 if (debug_variable_location_views && !RESETTING_VIEW_P (table->view))
28727 push_dw_line_info_entry (table, LI_adv_address, label_num);
28728 else
28729 push_dw_line_info_entry (table, LI_set_address, label_num);
28730 if (debug_variable_location_views)
28732 bool resetting = FORCE_RESETTING_VIEW_P (table->view);
28733 if (resetting)
28734 table->view = 0;
28736 if (flag_debug_asm)
28737 fprintf (asm_out_file, "\t%s view %s%d\n",
28738 ASM_COMMENT_START,
28739 resetting ? "-" : "",
28740 table->view);
28742 table->view++;
28744 if (file_num != table->file_num)
28745 push_dw_line_info_entry (table, LI_set_file, file_num);
28746 if (discriminator != table->discrim_num)
28747 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
28748 if (is_stmt != table->is_stmt)
28749 push_dw_line_info_entry (table, LI_negate_stmt, 0);
28750 push_dw_line_info_entry (table, LI_set_line, line);
28751 if (debug_column_info)
28752 push_dw_line_info_entry (table, LI_set_column, column);
28755 table->file_num = file_num;
28756 table->line_num = line;
28757 table->column_num = column;
28758 table->discrim_num = discriminator;
28759 table->is_stmt = is_stmt;
28760 table->in_use = true;
28763 /* Record a source file location for a DECL_IGNORED_P function. */
28765 static void
28766 dwarf2out_set_ignored_loc (unsigned int line, unsigned int column,
28767 const char *filename)
28769 dw_fde_ref fde = cfun->fde;
28771 fde->ignored_debug = false;
28772 set_cur_line_info_table (function_section (fde->decl));
28774 dwarf2out_source_line (line, column, filename, 0, true);
28777 /* Record the beginning of a new source file. */
28779 static void
28780 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
28782 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28784 macinfo_entry e;
28785 e.code = DW_MACINFO_start_file;
28786 e.lineno = lineno;
28787 e.info = ggc_strdup (filename);
28788 vec_safe_push (macinfo_table, e);
28792 /* Record the end of a source file. */
28794 static void
28795 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
28797 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28799 macinfo_entry e;
28800 e.code = DW_MACINFO_end_file;
28801 e.lineno = lineno;
28802 e.info = NULL;
28803 vec_safe_push (macinfo_table, e);
28807 /* Called from debug_define in toplev.cc. The `buffer' parameter contains
28808 the tail part of the directive line, i.e. the part which is past the
28809 initial whitespace, #, whitespace, directive-name, whitespace part. */
28811 static void
28812 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
28813 const char *buffer ATTRIBUTE_UNUSED)
28815 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28817 macinfo_entry e;
28818 /* Insert a dummy first entry to be able to optimize the whole
28819 predefined macro block using DW_MACRO_import. */
28820 if (macinfo_table->is_empty () && lineno <= 1)
28822 e.code = 0;
28823 e.lineno = 0;
28824 e.info = NULL;
28825 vec_safe_push (macinfo_table, e);
28827 e.code = DW_MACINFO_define;
28828 e.lineno = lineno;
28829 e.info = ggc_strdup (buffer);
28830 vec_safe_push (macinfo_table, e);
28834 /* Called from debug_undef in toplev.cc. The `buffer' parameter contains
28835 the tail part of the directive line, i.e. the part which is past the
28836 initial whitespace, #, whitespace, directive-name, whitespace part. */
28838 static void
28839 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
28840 const char *buffer ATTRIBUTE_UNUSED)
28842 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28844 macinfo_entry e;
28845 /* Insert a dummy first entry to be able to optimize the whole
28846 predefined macro block using DW_MACRO_import. */
28847 if (macinfo_table->is_empty () && lineno <= 1)
28849 e.code = 0;
28850 e.lineno = 0;
28851 e.info = NULL;
28852 vec_safe_push (macinfo_table, e);
28854 e.code = DW_MACINFO_undef;
28855 e.lineno = lineno;
28856 e.info = ggc_strdup (buffer);
28857 vec_safe_push (macinfo_table, e);
28861 /* Helpers to manipulate hash table of CUs. */
28863 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
28865 static inline hashval_t hash (const macinfo_entry *);
28866 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
28869 inline hashval_t
28870 macinfo_entry_hasher::hash (const macinfo_entry *entry)
28872 return htab_hash_string (entry->info);
28875 inline bool
28876 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
28877 const macinfo_entry *entry2)
28879 return !strcmp (entry1->info, entry2->info);
28882 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
28884 /* Output a single .debug_macinfo entry. */
28886 static void
28887 output_macinfo_op (macinfo_entry *ref)
28889 int file_num;
28890 size_t len;
28891 struct indirect_string_node *node;
28892 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28893 struct dwarf_file_data *fd;
28895 switch (ref->code)
28897 case DW_MACINFO_start_file:
28898 fd = lookup_filename (ref->info);
28899 file_num = maybe_emit_file (fd);
28900 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
28901 dw2_asm_output_data_uleb128 (ref->lineno,
28902 "Included from line number %lu",
28903 (unsigned long) ref->lineno);
28904 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
28905 break;
28906 case DW_MACINFO_end_file:
28907 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
28908 break;
28909 case DW_MACINFO_define:
28910 case DW_MACINFO_undef:
28911 len = strlen (ref->info) + 1;
28912 if ((!dwarf_strict || dwarf_version >= 5)
28913 && len > (size_t) dwarf_offset_size
28914 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
28915 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
28917 if (dwarf_split_debug_info && dwarf_version >= 5)
28918 ref->code = ref->code == DW_MACINFO_define
28919 ? DW_MACRO_define_strx : DW_MACRO_undef_strx;
28920 else
28921 ref->code = ref->code == DW_MACINFO_define
28922 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
28923 output_macinfo_op (ref);
28924 return;
28926 dw2_asm_output_data (1, ref->code,
28927 ref->code == DW_MACINFO_define
28928 ? "Define macro" : "Undefine macro");
28929 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
28930 (unsigned long) ref->lineno);
28931 dw2_asm_output_nstring (ref->info, -1, "The macro");
28932 break;
28933 case DW_MACRO_define_strp:
28934 dw2_asm_output_data (1, ref->code, "Define macro strp");
28935 goto do_DW_MACRO_define_strpx;
28936 case DW_MACRO_undef_strp:
28937 dw2_asm_output_data (1, ref->code, "Undefine macro strp");
28938 goto do_DW_MACRO_define_strpx;
28939 case DW_MACRO_define_strx:
28940 dw2_asm_output_data (1, ref->code, "Define macro strx");
28941 goto do_DW_MACRO_define_strpx;
28942 case DW_MACRO_undef_strx:
28943 dw2_asm_output_data (1, ref->code, "Undefine macro strx");
28944 /* FALLTHRU */
28945 do_DW_MACRO_define_strpx:
28946 /* NB: dwarf2out_finish performs:
28947 1. save_macinfo_strings
28948 2. hash table traverse of index_string
28949 3. output_macinfo -> output_macinfo_op
28950 4. output_indirect_strings
28951 -> hash table traverse of output_index_string
28953 When output_macinfo_op is called, all index strings have been
28954 added to hash table by save_macinfo_strings and we can't pass
28955 INSERT to find_slot_with_hash which may expand hash table, even
28956 if no insertion is needed, and change hash table traverse order
28957 between index_string and output_index_string. */
28958 node = find_AT_string (ref->info, NO_INSERT);
28959 gcc_assert (node
28960 && (node->form == DW_FORM_strp
28961 || node->form == dwarf_FORM (DW_FORM_strx)));
28962 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
28963 (unsigned long) ref->lineno);
28964 if (node->form == DW_FORM_strp)
28965 dw2_asm_output_offset (dwarf_offset_size, node->label,
28966 debug_str_section, "The macro: \"%s\"",
28967 ref->info);
28968 else
28969 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
28970 ref->info);
28971 break;
28972 case DW_MACRO_import:
28973 dw2_asm_output_data (1, ref->code, "Import");
28974 ASM_GENERATE_INTERNAL_LABEL (label,
28975 DEBUG_MACRO_SECTION_LABEL,
28976 ref->lineno + macinfo_label_base);
28977 dw2_asm_output_offset (dwarf_offset_size, label, NULL, NULL);
28978 break;
28979 default:
28980 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
28981 ASM_COMMENT_START, (unsigned long) ref->code);
28982 break;
28986 /* Attempt to make a sequence of define/undef macinfo ops shareable with
28987 other compilation unit .debug_macinfo sections. IDX is the first
28988 index of a define/undef, return the number of ops that should be
28989 emitted in a comdat .debug_macinfo section and emit
28990 a DW_MACRO_import entry referencing it.
28991 If the define/undef entry should be emitted normally, return 0. */
28993 static unsigned
28994 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
28995 macinfo_hash_type **macinfo_htab)
28997 macinfo_entry *first, *second, *cur, *inc;
28998 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
28999 unsigned char checksum[16];
29000 struct md5_ctx ctx;
29001 char *grp_name, *tail;
29002 const char *base;
29003 unsigned int i, count, encoded_filename_len, linebuf_len;
29004 macinfo_entry **slot;
29006 first = &(*macinfo_table)[idx];
29007 second = &(*macinfo_table)[idx + 1];
29009 /* Optimize only if there are at least two consecutive define/undef ops,
29010 and either all of them are before first DW_MACINFO_start_file
29011 with lineno {0,1} (i.e. predefined macro block), or all of them are
29012 in some included header file. */
29013 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
29014 return 0;
29015 if (vec_safe_is_empty (files))
29017 if (first->lineno > 1 || second->lineno > 1)
29018 return 0;
29020 else if (first->lineno == 0)
29021 return 0;
29023 /* Find the last define/undef entry that can be grouped together
29024 with first and at the same time compute md5 checksum of their
29025 codes, linenumbers and strings. */
29026 md5_init_ctx (&ctx);
29027 for (i = idx; macinfo_table->iterate (i, &cur); i++)
29028 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
29029 break;
29030 else if (vec_safe_is_empty (files) && cur->lineno > 1)
29031 break;
29032 else
29034 unsigned char code = cur->code;
29035 md5_process_bytes (&code, 1, &ctx);
29036 checksum_uleb128 (cur->lineno, &ctx);
29037 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
29039 md5_finish_ctx (&ctx, checksum);
29040 count = i - idx;
29042 /* From the containing include filename (if any) pick up just
29043 usable characters from its basename. */
29044 if (vec_safe_is_empty (files))
29045 base = "";
29046 else
29047 base = lbasename (files->last ().info);
29048 for (encoded_filename_len = 0, i = 0; base[i]; i++)
29049 if (ISIDNUM (base[i]) || base[i] == '.')
29050 encoded_filename_len++;
29051 /* Count . at the end. */
29052 if (encoded_filename_len)
29053 encoded_filename_len++;
29055 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
29056 linebuf_len = strlen (linebuf);
29058 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
29059 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
29060 + 16 * 2 + 1);
29061 memcpy (grp_name, dwarf_offset_size == 4 ? "wm4." : "wm8.", 4);
29062 tail = grp_name + 4;
29063 if (encoded_filename_len)
29065 for (i = 0; base[i]; i++)
29066 if (ISIDNUM (base[i]) || base[i] == '.')
29067 *tail++ = base[i];
29068 *tail++ = '.';
29070 memcpy (tail, linebuf, linebuf_len);
29071 tail += linebuf_len;
29072 *tail++ = '.';
29073 for (i = 0; i < 16; i++)
29074 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
29076 /* Construct a macinfo_entry for DW_MACRO_import
29077 in the empty vector entry before the first define/undef. */
29078 inc = &(*macinfo_table)[idx - 1];
29079 inc->code = DW_MACRO_import;
29080 inc->lineno = 0;
29081 inc->info = ggc_strdup (grp_name);
29082 if (!*macinfo_htab)
29083 *macinfo_htab = new macinfo_hash_type (10);
29084 /* Avoid emitting duplicates. */
29085 slot = (*macinfo_htab)->find_slot (inc, INSERT);
29086 if (*slot != NULL)
29088 inc->code = 0;
29089 inc->info = NULL;
29090 /* If such an entry has been used before, just emit
29091 a DW_MACRO_import op. */
29092 inc = *slot;
29093 output_macinfo_op (inc);
29094 /* And clear all macinfo_entry in the range to avoid emitting them
29095 in the second pass. */
29096 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
29098 cur->code = 0;
29099 cur->info = NULL;
29102 else
29104 *slot = inc;
29105 inc->lineno = (*macinfo_htab)->elements ();
29106 output_macinfo_op (inc);
29108 return count;
29111 /* Save any strings needed by the macinfo table in the debug str
29112 table. All strings must be collected into the table by the time
29113 index_string is called. */
29115 static void
29116 save_macinfo_strings (void)
29118 unsigned len;
29119 unsigned i;
29120 macinfo_entry *ref;
29122 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
29124 switch (ref->code)
29126 /* Match the logic in output_macinfo_op to decide on
29127 indirect strings. */
29128 case DW_MACINFO_define:
29129 case DW_MACINFO_undef:
29130 len = strlen (ref->info) + 1;
29131 if ((!dwarf_strict || dwarf_version >= 5)
29132 && len > (unsigned) dwarf_offset_size
29133 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
29134 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
29135 set_indirect_string (find_AT_string (ref->info));
29136 break;
29137 case DW_MACINFO_start_file:
29138 /* -gsplit-dwarf -g3 will also output filename as indirect
29139 string. */
29140 if (!dwarf_split_debug_info)
29141 break;
29142 /* Fall through. */
29143 case DW_MACRO_define_strp:
29144 case DW_MACRO_undef_strp:
29145 case DW_MACRO_define_strx:
29146 case DW_MACRO_undef_strx:
29147 set_indirect_string (find_AT_string (ref->info));
29148 break;
29149 default:
29150 break;
29155 /* Output macinfo section(s). */
29157 static void
29158 output_macinfo (const char *debug_line_label, bool early_lto_debug)
29160 unsigned i;
29161 unsigned long length = vec_safe_length (macinfo_table);
29162 macinfo_entry *ref;
29163 vec<macinfo_entry, va_gc> *files = NULL;
29164 macinfo_hash_type *macinfo_htab = NULL;
29165 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
29167 if (! length)
29168 return;
29170 /* output_macinfo* uses these interchangeably. */
29171 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
29172 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
29173 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
29174 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
29176 /* AIX Assembler inserts the length, so adjust the reference to match the
29177 offset expected by debuggers. */
29178 strcpy (dl_section_ref, debug_line_label);
29179 if (XCOFF_DEBUGGING_INFO)
29180 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
29182 /* For .debug_macro emit the section header. */
29183 if (!dwarf_strict || dwarf_version >= 5)
29185 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
29186 "DWARF macro version number");
29187 if (dwarf_offset_size == 8)
29188 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
29189 else
29190 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
29191 dw2_asm_output_offset (dwarf_offset_size, debug_line_label,
29192 debug_line_section, NULL);
29195 /* In the first loop, it emits the primary .debug_macinfo section
29196 and after each emitted op the macinfo_entry is cleared.
29197 If a longer range of define/undef ops can be optimized using
29198 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
29199 the vector before the first define/undef in the range and the
29200 whole range of define/undef ops is not emitted and kept. */
29201 for (i = 0; macinfo_table->iterate (i, &ref); i++)
29203 switch (ref->code)
29205 case DW_MACINFO_start_file:
29206 vec_safe_push (files, *ref);
29207 break;
29208 case DW_MACINFO_end_file:
29209 if (!vec_safe_is_empty (files))
29210 files->pop ();
29211 break;
29212 case DW_MACINFO_define:
29213 case DW_MACINFO_undef:
29214 if ((!dwarf_strict || dwarf_version >= 5)
29215 && HAVE_COMDAT_GROUP
29216 && vec_safe_length (files) != 1
29217 && i > 0
29218 && i + 1 < length
29219 && (*macinfo_table)[i - 1].code == 0)
29221 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
29222 if (count)
29224 i += count - 1;
29225 continue;
29228 break;
29229 case 0:
29230 /* A dummy entry may be inserted at the beginning to be able
29231 to optimize the whole block of predefined macros. */
29232 if (i == 0)
29233 continue;
29234 default:
29235 break;
29237 output_macinfo_op (ref);
29238 ref->info = NULL;
29239 ref->code = 0;
29242 if (!macinfo_htab)
29243 return;
29245 /* Save the number of transparent includes so we can adjust the
29246 label number for the fat LTO object DWARF. */
29247 unsigned macinfo_label_base_adj = macinfo_htab->elements ();
29249 delete macinfo_htab;
29250 macinfo_htab = NULL;
29252 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
29253 terminate the current chain and switch to a new comdat .debug_macinfo
29254 section and emit the define/undef entries within it. */
29255 for (i = 0; macinfo_table->iterate (i, &ref); i++)
29256 switch (ref->code)
29258 case 0:
29259 continue;
29260 case DW_MACRO_import:
29262 char label[MAX_ARTIFICIAL_LABEL_BYTES];
29263 tree comdat_key = get_identifier (ref->info);
29264 /* Terminate the previous .debug_macinfo section. */
29265 dw2_asm_output_data (1, 0, "End compilation unit");
29266 targetm.asm_out.named_section (debug_macinfo_section_name,
29267 SECTION_DEBUG
29268 | SECTION_LINKONCE
29269 | (early_lto_debug
29270 ? SECTION_EXCLUDE : 0),
29271 comdat_key);
29272 ASM_GENERATE_INTERNAL_LABEL (label,
29273 DEBUG_MACRO_SECTION_LABEL,
29274 ref->lineno + macinfo_label_base);
29275 ASM_OUTPUT_LABEL (asm_out_file, label);
29276 ref->code = 0;
29277 ref->info = NULL;
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, 1, "Flags: 64-bit");
29282 else
29283 dw2_asm_output_data (1, 0, "Flags: 32-bit");
29285 break;
29286 case DW_MACINFO_define:
29287 case DW_MACINFO_undef:
29288 output_macinfo_op (ref);
29289 ref->code = 0;
29290 ref->info = NULL;
29291 break;
29292 default:
29293 gcc_unreachable ();
29296 macinfo_label_base += macinfo_label_base_adj;
29299 /* As init_sections_and_labels may get called multiple times, have a
29300 generation count for labels. */
29301 static unsigned init_sections_and_labels_generation;
29303 /* Initialize the various sections and labels for dwarf output and prefix
29304 them with PREFIX if non-NULL. Returns the generation (zero based
29305 number of times function was called). */
29307 static unsigned
29308 init_sections_and_labels (bool early_lto_debug)
29310 if (early_lto_debug)
29312 if (!dwarf_split_debug_info)
29314 debug_info_section = get_section (DEBUG_LTO_INFO_SECTION,
29315 SECTION_DEBUG | SECTION_EXCLUDE,
29316 NULL);
29317 debug_abbrev_section = get_section (DEBUG_LTO_ABBREV_SECTION,
29318 SECTION_DEBUG | SECTION_EXCLUDE,
29319 NULL);
29320 debug_macinfo_section_name
29321 = ((dwarf_strict && dwarf_version < 5)
29322 ? DEBUG_LTO_MACINFO_SECTION : DEBUG_LTO_MACRO_SECTION);
29323 debug_macinfo_section = get_section (debug_macinfo_section_name,
29324 SECTION_DEBUG
29325 | SECTION_EXCLUDE, NULL);
29327 else
29329 /* ??? Which of the following do we need early? */
29330 debug_info_section = get_section (DEBUG_LTO_DWO_INFO_SECTION,
29331 SECTION_DEBUG | SECTION_EXCLUDE,
29332 NULL);
29333 debug_abbrev_section = get_section (DEBUG_LTO_DWO_ABBREV_SECTION,
29334 SECTION_DEBUG | SECTION_EXCLUDE,
29335 NULL);
29336 debug_skeleton_info_section = get_section (DEBUG_LTO_INFO_SECTION,
29337 SECTION_DEBUG
29338 | SECTION_EXCLUDE, NULL);
29339 debug_skeleton_abbrev_section
29340 = get_section (DEBUG_LTO_ABBREV_SECTION,
29341 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29342 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
29343 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
29344 init_sections_and_labels_generation);
29346 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
29347 stay in the main .o, but the skeleton_line goes into the split
29348 off dwo. */
29349 debug_skeleton_line_section
29350 = get_section (DEBUG_LTO_LINE_SECTION,
29351 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29352 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
29353 DEBUG_SKELETON_LINE_SECTION_LABEL,
29354 init_sections_and_labels_generation);
29355 debug_str_offsets_section
29356 = get_section (DEBUG_LTO_DWO_STR_OFFSETS_SECTION,
29357 SECTION_DEBUG | SECTION_EXCLUDE,
29358 NULL);
29359 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
29360 DEBUG_SKELETON_INFO_SECTION_LABEL,
29361 init_sections_and_labels_generation);
29362 debug_str_dwo_section = get_section (DEBUG_LTO_STR_DWO_SECTION,
29363 DEBUG_STR_DWO_SECTION_FLAGS,
29364 NULL);
29365 debug_macinfo_section_name
29366 = ((dwarf_strict && dwarf_version < 5)
29367 ? DEBUG_LTO_DWO_MACINFO_SECTION : DEBUG_LTO_DWO_MACRO_SECTION);
29368 debug_macinfo_section = get_section (debug_macinfo_section_name,
29369 SECTION_DEBUG | SECTION_EXCLUDE,
29370 NULL);
29372 /* For macro info and the file table we have to refer to a
29373 debug_line section. */
29374 debug_line_section = get_section (DEBUG_LTO_LINE_SECTION,
29375 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29376 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
29377 DEBUG_LINE_SECTION_LABEL,
29378 init_sections_and_labels_generation);
29380 debug_str_section = get_section (DEBUG_LTO_STR_SECTION,
29381 DEBUG_STR_SECTION_FLAGS
29382 | SECTION_EXCLUDE, NULL);
29383 if (!dwarf_split_debug_info)
29384 debug_line_str_section
29385 = get_section (DEBUG_LTO_LINE_STR_SECTION,
29386 DEBUG_STR_SECTION_FLAGS | SECTION_EXCLUDE, NULL);
29388 else
29390 if (!dwarf_split_debug_info)
29392 debug_info_section = get_section (DEBUG_INFO_SECTION,
29393 SECTION_DEBUG, NULL);
29394 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
29395 SECTION_DEBUG, NULL);
29396 debug_loc_section = get_section (dwarf_version >= 5
29397 ? DEBUG_LOCLISTS_SECTION
29398 : DEBUG_LOC_SECTION,
29399 SECTION_DEBUG, NULL);
29400 debug_macinfo_section_name
29401 = ((dwarf_strict && dwarf_version < 5)
29402 ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION);
29403 debug_macinfo_section = get_section (debug_macinfo_section_name,
29404 SECTION_DEBUG, NULL);
29406 else
29408 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
29409 SECTION_DEBUG | SECTION_EXCLUDE,
29410 NULL);
29411 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
29412 SECTION_DEBUG | SECTION_EXCLUDE,
29413 NULL);
29414 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
29415 SECTION_DEBUG, NULL);
29416 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
29417 SECTION_DEBUG, NULL);
29418 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
29419 SECTION_DEBUG, NULL);
29420 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
29421 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
29422 init_sections_and_labels_generation);
29424 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
29425 stay in the main .o, but the skeleton_line goes into the
29426 split off dwo. */
29427 debug_skeleton_line_section
29428 = get_section (DEBUG_DWO_LINE_SECTION,
29429 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29430 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
29431 DEBUG_SKELETON_LINE_SECTION_LABEL,
29432 init_sections_and_labels_generation);
29433 debug_str_offsets_section
29434 = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
29435 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29436 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
29437 DEBUG_SKELETON_INFO_SECTION_LABEL,
29438 init_sections_and_labels_generation);
29439 debug_loc_section = get_section (dwarf_version >= 5
29440 ? DEBUG_DWO_LOCLISTS_SECTION
29441 : DEBUG_DWO_LOC_SECTION,
29442 SECTION_DEBUG | SECTION_EXCLUDE,
29443 NULL);
29444 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
29445 DEBUG_STR_DWO_SECTION_FLAGS,
29446 NULL);
29447 debug_macinfo_section_name
29448 = ((dwarf_strict && dwarf_version < 5)
29449 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION);
29450 debug_macinfo_section = get_section (debug_macinfo_section_name,
29451 SECTION_DEBUG | SECTION_EXCLUDE,
29452 NULL);
29453 if (dwarf_version >= 5)
29454 debug_ranges_dwo_section
29455 = get_section (DEBUG_DWO_RNGLISTS_SECTION,
29456 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29458 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
29459 SECTION_DEBUG, NULL);
29460 debug_line_section = get_section (DEBUG_LINE_SECTION,
29461 SECTION_DEBUG, NULL);
29462 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
29463 SECTION_DEBUG, NULL);
29464 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
29465 SECTION_DEBUG, NULL);
29466 debug_str_section = get_section (DEBUG_STR_SECTION,
29467 DEBUG_STR_SECTION_FLAGS, NULL);
29468 if ((!dwarf_split_debug_info && !output_asm_line_debug_info ())
29469 || asm_outputs_debug_line_str ())
29470 debug_line_str_section = get_section (DEBUG_LINE_STR_SECTION,
29471 DEBUG_STR_SECTION_FLAGS, NULL);
29473 debug_ranges_section = get_section (dwarf_version >= 5
29474 ? DEBUG_RNGLISTS_SECTION
29475 : DEBUG_RANGES_SECTION,
29476 SECTION_DEBUG, NULL);
29477 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
29478 SECTION_DEBUG, NULL);
29481 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
29482 DEBUG_ABBREV_SECTION_LABEL,
29483 init_sections_and_labels_generation);
29484 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
29485 DEBUG_INFO_SECTION_LABEL,
29486 init_sections_and_labels_generation);
29487 info_section_emitted = false;
29488 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
29489 DEBUG_LINE_SECTION_LABEL,
29490 init_sections_and_labels_generation);
29491 /* There are up to 6 unique ranges labels per generation.
29492 See also output_rnglists. */
29493 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
29494 DEBUG_RANGES_SECTION_LABEL,
29495 init_sections_and_labels_generation * 6);
29496 if (dwarf_version >= 5 && dwarf_split_debug_info)
29497 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
29498 DEBUG_RANGES_SECTION_LABEL,
29499 1 + init_sections_and_labels_generation * 6);
29500 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
29501 DEBUG_ADDR_SECTION_LABEL,
29502 init_sections_and_labels_generation);
29503 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
29504 (dwarf_strict && dwarf_version < 5)
29505 ? DEBUG_MACINFO_SECTION_LABEL
29506 : DEBUG_MACRO_SECTION_LABEL,
29507 init_sections_and_labels_generation);
29508 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL,
29509 init_sections_and_labels_generation);
29511 ++init_sections_and_labels_generation;
29512 return init_sections_and_labels_generation - 1;
29515 /* Set up for Dwarf output at the start of compilation. */
29517 static void
29518 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
29520 /* Allocate the file_table. */
29521 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
29523 #ifndef DWARF2_LINENO_DEBUGGING_INFO
29524 /* Allocate the decl_die_table. */
29525 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
29527 /* Allocate the decl_loc_table. */
29528 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
29530 /* Allocate the cached_dw_loc_list_table. */
29531 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
29533 /* Allocate the initial hunk of the abbrev_die_table. */
29534 vec_alloc (abbrev_die_table, 256);
29535 /* Zero-th entry is allocated, but unused. */
29536 abbrev_die_table->quick_push (NULL);
29538 /* Allocate the dwarf_proc_stack_usage_map. */
29539 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
29541 /* Allocate the pubtypes and pubnames vectors. */
29542 vec_alloc (pubname_table, 32);
29543 vec_alloc (pubtype_table, 32);
29545 vec_alloc (incomplete_types, 64);
29547 vec_alloc (used_rtx_array, 32);
29549 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
29550 vec_alloc (macinfo_table, 64);
29551 #endif
29553 /* If front-ends already registered a main translation unit but we were not
29554 ready to perform the association, do this now. */
29555 if (main_translation_unit != NULL_TREE)
29556 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
29559 /* Called before compile () starts outputtting functions, variables
29560 and toplevel asms into assembly. */
29562 static void
29563 dwarf2out_assembly_start (void)
29565 if (text_section_line_info)
29566 return;
29568 #ifndef DWARF2_LINENO_DEBUGGING_INFO
29569 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
29570 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
29571 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
29572 COLD_TEXT_SECTION_LABEL, 0);
29573 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
29575 switch_to_section (text_section);
29576 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
29577 #endif
29579 /* Make sure the line number table for .text always exists. */
29580 text_section_line_info = new_line_info_table ();
29581 text_section_line_info->end_label = text_end_label;
29583 #ifdef DWARF2_LINENO_DEBUGGING_INFO
29584 cur_line_info_table = text_section_line_info;
29585 #endif
29587 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
29588 && dwarf2out_do_cfi_asm ()
29589 && !dwarf2out_do_eh_frame ())
29590 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
29592 #if defined(HAVE_AS_GDWARF_5_DEBUG_FLAG) && defined(HAVE_AS_WORKING_DWARF_N_FLAG)
29593 if (output_asm_line_debug_info () && dwarf_version >= 5)
29595 /* When gas outputs DWARF5 .debug_line[_str] then we have to
29596 tell it the comp_dir and main file name for the zero entry
29597 line table. */
29598 const char *comp_dir, *filename0;
29600 comp_dir = comp_dir_string ();
29601 if (comp_dir == NULL)
29602 comp_dir = "";
29604 filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
29605 if (filename0 == NULL)
29606 filename0 = "";
29608 fprintf (asm_out_file, "\t.file 0 ");
29609 output_quoted_string (asm_out_file, remap_debug_filename (comp_dir));
29610 fputc (' ', asm_out_file);
29611 output_quoted_string (asm_out_file, remap_debug_filename (filename0));
29612 fputc ('\n', asm_out_file);
29614 else
29615 #endif
29616 /* Work around for PR101575: output a dummy .file directive. */
29617 if (!last_emitted_file && dwarf_debuginfo_p ()
29618 && debug_info_level >= DINFO_LEVEL_TERSE)
29620 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
29622 if (filename0 == NULL)
29623 filename0 = "<dummy>";
29624 maybe_emit_file (lookup_filename (filename0));
29628 /* A helper function for dwarf2out_finish called through
29629 htab_traverse. Assign a string its index. All strings must be
29630 collected into the table by the time index_string is called,
29631 because the indexing code relies on htab_traverse to traverse nodes
29632 in the same order for each run. */
29635 index_string (indirect_string_node **h, unsigned int *index)
29637 indirect_string_node *node = *h;
29639 find_string_form (node);
29640 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29642 gcc_assert (node->index == NO_INDEX_ASSIGNED);
29643 node->index = *index;
29644 *index += 1;
29646 return 1;
29649 /* A helper function for output_indirect_strings called through
29650 htab_traverse. Output the offset to a string and update the
29651 current offset. */
29654 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
29656 indirect_string_node *node = *h;
29658 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29660 /* Assert that this node has been assigned an index. */
29661 gcc_assert (node->index != NO_INDEX_ASSIGNED
29662 && node->index != NOT_INDEXED);
29663 dw2_asm_output_data (dwarf_offset_size, *offset,
29664 "indexed string 0x%x: %s", node->index, node->str);
29665 *offset += strlen (node->str) + 1;
29667 return 1;
29670 /* A helper function for dwarf2out_finish called through
29671 htab_traverse. Output the indexed string. */
29674 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
29676 struct indirect_string_node *node = *h;
29678 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29680 /* Assert that the strings are output in the same order as their
29681 indexes were assigned. */
29682 gcc_assert (*cur_idx == node->index);
29683 assemble_string (node->str, strlen (node->str) + 1);
29684 *cur_idx += 1;
29686 return 1;
29689 /* A helper function for output_indirect_strings. Counts the number
29690 of index strings offsets. Must match the logic of the functions
29691 output_index_string[_offsets] above. */
29693 count_index_strings (indirect_string_node **h, unsigned int *last_idx)
29695 struct indirect_string_node *node = *h;
29697 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29698 *last_idx += 1;
29699 return 1;
29702 /* A helper function for dwarf2out_finish called through
29703 htab_traverse. Emit one queued .debug_str string. */
29706 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
29708 struct indirect_string_node *node = *h;
29710 node->form = find_string_form (node);
29711 if (node->form == form && node->refcount > 0)
29713 ASM_OUTPUT_LABEL (asm_out_file, node->label);
29714 assemble_string (node->str, strlen (node->str) + 1);
29717 return 1;
29720 /* Output the indexed string table. */
29722 static void
29723 output_indirect_strings (void)
29725 switch_to_section (debug_str_section);
29726 if (!dwarf_split_debug_info)
29727 debug_str_hash->traverse<enum dwarf_form,
29728 output_indirect_string> (DW_FORM_strp);
29729 else
29731 unsigned int offset = 0;
29732 unsigned int cur_idx = 0;
29734 if (skeleton_debug_str_hash)
29735 skeleton_debug_str_hash->traverse<enum dwarf_form,
29736 output_indirect_string> (DW_FORM_strp);
29738 switch_to_section (debug_str_offsets_section);
29739 /* For DWARF5 the .debug_str_offsets[.dwo] section needs a unit
29740 header. Note that we don't need to generate a label to the
29741 actual index table following the header here, because this is
29742 for the split dwarf case only. In an .dwo file there is only
29743 one string offsets table (and one debug info section). But
29744 if we would start using string offset tables for the main (or
29745 skeleton) unit, then we have to add a DW_AT_str_offsets_base
29746 pointing to the actual index after the header. Split dwarf
29747 units will never have a string offsets base attribute. When
29748 a split unit is moved into a .dwp file the string offsets can
29749 be found through the .debug_cu_index section table. */
29750 if (dwarf_version >= 5)
29752 unsigned int last_idx = 0;
29753 unsigned long str_offsets_length;
29755 debug_str_hash->traverse_noresize
29756 <unsigned int *, count_index_strings> (&last_idx);
29757 str_offsets_length = last_idx * dwarf_offset_size + 4;
29758 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
29759 dw2_asm_output_data (4, 0xffffffff,
29760 "Escape value for 64-bit DWARF extension");
29761 dw2_asm_output_data (dwarf_offset_size, str_offsets_length,
29762 "Length of string offsets unit");
29763 dw2_asm_output_data (2, 5, "DWARF string offsets version");
29764 dw2_asm_output_data (2, 0, "Header zero padding");
29766 debug_str_hash->traverse_noresize
29767 <unsigned int *, output_index_string_offset> (&offset);
29768 switch_to_section (debug_str_dwo_section);
29769 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
29770 (&cur_idx);
29774 /* Callback for htab_traverse to assign an index to an entry in the
29775 table, and to write that entry to the .debug_addr section. */
29778 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
29780 addr_table_entry *entry = *slot;
29782 if (entry->refcount == 0)
29784 gcc_assert (entry->index == NO_INDEX_ASSIGNED
29785 || entry->index == NOT_INDEXED);
29786 return 1;
29789 gcc_assert (entry->index == *cur_index);
29790 (*cur_index)++;
29792 switch (entry->kind)
29794 case ate_kind_rtx:
29795 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
29796 "0x%x", entry->index);
29797 break;
29798 case ate_kind_rtx_dtprel:
29799 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
29800 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
29801 DWARF2_ADDR_SIZE,
29802 entry->addr.rtl);
29803 fputc ('\n', asm_out_file);
29804 break;
29805 case ate_kind_label:
29806 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
29807 "0x%x", entry->index);
29808 break;
29809 default:
29810 gcc_unreachable ();
29812 return 1;
29815 /* A helper function for dwarf2out_finish. Counts the number
29816 of indexed addresses. Must match the logic of the functions
29817 output_addr_table_entry above. */
29819 count_index_addrs (addr_table_entry **slot, unsigned int *last_idx)
29821 addr_table_entry *entry = *slot;
29823 if (entry->refcount > 0)
29824 *last_idx += 1;
29825 return 1;
29828 /* Produce the .debug_addr section. */
29830 static void
29831 output_addr_table (void)
29833 unsigned int index = 0;
29834 if (addr_index_table == NULL || addr_index_table->size () == 0)
29835 return;
29837 switch_to_section (debug_addr_section);
29838 /* GNU DebugFission https://gcc.gnu.org/wiki/DebugFission
29839 which GCC uses to implement -gsplit-dwarf as DWARF GNU extension
29840 before DWARF5, didn't have a header for .debug_addr units.
29841 DWARF5 specifies a small header when address tables are used. */
29842 if (dwarf_version >= 5)
29844 unsigned int last_idx = 0;
29845 unsigned long addrs_length;
29847 addr_index_table->traverse_noresize
29848 <unsigned int *, count_index_addrs> (&last_idx);
29849 addrs_length = last_idx * DWARF2_ADDR_SIZE + 4;
29851 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
29852 dw2_asm_output_data (4, 0xffffffff,
29853 "Escape value for 64-bit DWARF extension");
29854 dw2_asm_output_data (dwarf_offset_size, addrs_length,
29855 "Length of Address Unit");
29856 dw2_asm_output_data (2, 5, "DWARF addr version");
29857 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
29858 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
29860 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
29862 addr_index_table
29863 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
29866 #if ENABLE_ASSERT_CHECKING
29867 /* Verify that all marks are clear. */
29869 static void
29870 verify_marks_clear (dw_die_ref die)
29872 dw_die_ref c;
29874 gcc_assert (! die->die_mark);
29875 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
29877 #endif /* ENABLE_ASSERT_CHECKING */
29879 /* Clear the marks for a die and its children.
29880 Be cool if the mark isn't set. */
29882 static void
29883 prune_unmark_dies (dw_die_ref die)
29885 dw_die_ref c;
29887 if (die->die_mark)
29888 die->die_mark = 0;
29889 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
29892 /* Given LOC that is referenced by a DIE we're marking as used, find all
29893 referenced DWARF procedures it references and mark them as used. */
29895 static void
29896 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
29898 for (; loc != NULL; loc = loc->dw_loc_next)
29899 switch (loc->dw_loc_opc)
29901 case DW_OP_implicit_pointer:
29902 case DW_OP_convert:
29903 case DW_OP_reinterpret:
29904 case DW_OP_GNU_implicit_pointer:
29905 case DW_OP_GNU_convert:
29906 case DW_OP_GNU_reinterpret:
29907 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
29908 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
29909 break;
29910 case DW_OP_GNU_variable_value:
29911 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
29913 dw_die_ref ref
29914 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
29915 if (ref == NULL)
29916 break;
29917 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
29918 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
29919 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
29921 /* FALLTHRU */
29922 case DW_OP_call2:
29923 case DW_OP_call4:
29924 case DW_OP_call_ref:
29925 case DW_OP_const_type:
29926 case DW_OP_GNU_const_type:
29927 case DW_OP_GNU_parameter_ref:
29928 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
29929 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
29930 break;
29931 case DW_OP_regval_type:
29932 case DW_OP_deref_type:
29933 case DW_OP_GNU_regval_type:
29934 case DW_OP_GNU_deref_type:
29935 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
29936 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
29937 break;
29938 case DW_OP_entry_value:
29939 case DW_OP_GNU_entry_value:
29940 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
29941 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
29942 break;
29943 default:
29944 break;
29948 /* Given DIE that we're marking as used, find any other dies
29949 it references as attributes and mark them as used. */
29951 static void
29952 prune_unused_types_walk_attribs (dw_die_ref die)
29954 dw_attr_node *a;
29955 unsigned ix;
29957 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29959 switch (AT_class (a))
29961 /* Make sure DWARF procedures referenced by location descriptions will
29962 get emitted. */
29963 case dw_val_class_loc:
29964 prune_unused_types_walk_loc_descr (AT_loc (a));
29965 break;
29966 case dw_val_class_loc_list:
29967 for (dw_loc_list_ref list = AT_loc_list (a);
29968 list != NULL;
29969 list = list->dw_loc_next)
29970 prune_unused_types_walk_loc_descr (list->expr);
29971 break;
29973 case dw_val_class_view_list:
29974 /* This points to a loc_list in another attribute, so it's
29975 already covered. */
29976 break;
29978 case dw_val_class_die_ref:
29979 /* A reference to another DIE.
29980 Make sure that it will get emitted.
29981 If it was broken out into a comdat group, don't follow it. */
29982 if (! AT_ref (a)->comdat_type_p
29983 || a->dw_attr == DW_AT_specification)
29984 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
29985 break;
29987 case dw_val_class_str:
29988 /* Set the string's refcount to 0 so that prune_unused_types_mark
29989 accounts properly for it. */
29990 a->dw_attr_val.v.val_str->refcount = 0;
29991 break;
29993 default:
29994 break;
29999 /* Mark the generic parameters and arguments children DIEs of DIE. */
30001 static void
30002 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
30004 dw_die_ref c;
30006 if (die == NULL || die->die_child == NULL)
30007 return;
30008 c = die->die_child;
30011 if (is_template_parameter (c))
30012 prune_unused_types_mark (c, 1);
30013 c = c->die_sib;
30014 } while (c && c != die->die_child);
30017 /* Mark DIE as being used. If DOKIDS is true, then walk down
30018 to DIE's children. */
30020 static void
30021 prune_unused_types_mark (dw_die_ref die, int dokids)
30023 dw_die_ref c;
30025 if (die->die_mark == 0)
30027 /* We haven't done this node yet. Mark it as used. */
30028 die->die_mark = 1;
30029 /* If this is the DIE of a generic type instantiation,
30030 mark the children DIEs that describe its generic parms and
30031 args. */
30032 prune_unused_types_mark_generic_parms_dies (die);
30034 /* We also have to mark its parents as used.
30035 (But we don't want to mark our parent's kids due to this,
30036 unless it is a class.) */
30037 if (die->die_parent)
30038 prune_unused_types_mark (die->die_parent,
30039 class_scope_p (die->die_parent));
30041 /* Mark any referenced nodes. */
30042 prune_unused_types_walk_attribs (die);
30044 /* If this node is a specification,
30045 also mark the definition, if it exists. */
30046 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
30047 prune_unused_types_mark (die->die_definition, 1);
30050 if (dokids && die->die_mark != 2)
30052 /* We need to walk the children, but haven't done so yet.
30053 Remember that we've walked the kids. */
30054 die->die_mark = 2;
30056 /* If this is an array type, we need to make sure our
30057 kids get marked, even if they're types. If we're
30058 breaking out types into comdat sections, do this
30059 for all type definitions. */
30060 if (die->die_tag == DW_TAG_array_type
30061 || (use_debug_types
30062 && is_type_die (die) && ! is_declaration_die (die)))
30063 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
30064 else
30065 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
30069 /* For local classes, look if any static member functions were emitted
30070 and if so, mark them. */
30072 static void
30073 prune_unused_types_walk_local_classes (dw_die_ref die)
30075 dw_die_ref c;
30077 if (die->die_mark == 2)
30078 return;
30080 switch (die->die_tag)
30082 case DW_TAG_structure_type:
30083 case DW_TAG_union_type:
30084 case DW_TAG_class_type:
30085 case DW_TAG_interface_type:
30086 break;
30088 case DW_TAG_subprogram:
30089 if (!get_AT_flag (die, DW_AT_declaration)
30090 || die->die_definition != NULL)
30091 prune_unused_types_mark (die, 1);
30092 return;
30094 default:
30095 return;
30098 /* Mark children. */
30099 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
30102 /* Walk the tree DIE and mark types that we actually use. */
30104 static void
30105 prune_unused_types_walk (dw_die_ref die)
30107 dw_die_ref c;
30109 /* Don't do anything if this node is already marked and
30110 children have been marked as well. */
30111 if (die->die_mark == 2)
30112 return;
30114 switch (die->die_tag)
30116 case DW_TAG_structure_type:
30117 case DW_TAG_union_type:
30118 case DW_TAG_class_type:
30119 case DW_TAG_interface_type:
30120 if (die->die_perennial_p)
30121 break;
30123 for (c = die->die_parent; c; c = c->die_parent)
30124 if (c->die_tag == DW_TAG_subprogram)
30125 break;
30127 /* Finding used static member functions inside of classes
30128 is needed just for local classes, because for other classes
30129 static member function DIEs with DW_AT_specification
30130 are emitted outside of the DW_TAG_*_type. If we ever change
30131 it, we'd need to call this even for non-local classes. */
30132 if (c)
30133 prune_unused_types_walk_local_classes (die);
30135 /* It's a type node --- don't mark it. */
30136 return;
30138 case DW_TAG_const_type:
30139 case DW_TAG_packed_type:
30140 case DW_TAG_pointer_type:
30141 case DW_TAG_reference_type:
30142 case DW_TAG_rvalue_reference_type:
30143 case DW_TAG_volatile_type:
30144 case DW_TAG_restrict_type:
30145 case DW_TAG_shared_type:
30146 case DW_TAG_atomic_type:
30147 case DW_TAG_immutable_type:
30148 case DW_TAG_typedef:
30149 case DW_TAG_array_type:
30150 case DW_TAG_coarray_type:
30151 case DW_TAG_friend:
30152 case DW_TAG_enumeration_type:
30153 case DW_TAG_subroutine_type:
30154 case DW_TAG_string_type:
30155 case DW_TAG_set_type:
30156 case DW_TAG_subrange_type:
30157 case DW_TAG_ptr_to_member_type:
30158 case DW_TAG_file_type:
30159 case DW_TAG_unspecified_type:
30160 case DW_TAG_dynamic_type:
30161 /* Type nodes are useful only when other DIEs reference them --- don't
30162 mark them. */
30163 /* FALLTHROUGH */
30165 case DW_TAG_dwarf_procedure:
30166 /* Likewise for DWARF procedures. */
30168 if (die->die_perennial_p)
30169 break;
30171 return;
30173 case DW_TAG_variable:
30174 if (flag_debug_only_used_symbols)
30176 if (die->die_perennial_p)
30177 break;
30179 /* For static data members, the declaration in the class is supposed
30180 to have DW_TAG_member tag in DWARF{3,4} but DW_TAG_variable in
30181 DWARF5. DW_TAG_member will be marked, so mark even such
30182 DW_TAG_variables in DWARF5, as long as it has DW_AT_const_value
30183 attribute. */
30184 if (dwarf_version >= 5
30185 && class_scope_p (die->die_parent)
30186 && get_AT (die, DW_AT_const_value))
30187 break;
30189 /* premark_used_variables marks external variables --- don't mark
30190 them here. But function-local externals are always considered
30191 used. */
30192 if (get_AT (die, DW_AT_external))
30194 for (c = die->die_parent; c; c = c->die_parent)
30195 if (c->die_tag == DW_TAG_subprogram)
30196 break;
30197 if (!c)
30198 return;
30201 /* FALLTHROUGH */
30203 default:
30204 /* Mark everything else. */
30205 break;
30208 if (die->die_mark == 0)
30210 die->die_mark = 1;
30212 /* Now, mark any dies referenced from here. */
30213 prune_unused_types_walk_attribs (die);
30216 die->die_mark = 2;
30218 /* Mark children. */
30219 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
30222 /* Increment the string counts on strings referred to from DIE's
30223 attributes. */
30225 static void
30226 prune_unused_types_update_strings (dw_die_ref die)
30228 dw_attr_node *a;
30229 unsigned ix;
30231 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30232 if (AT_class (a) == dw_val_class_str)
30234 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
30235 s->refcount++;
30236 /* Avoid unnecessarily putting strings that are used less than
30237 twice in the hash table. */
30238 if (s->form != DW_FORM_line_strp
30239 && (s->refcount
30240 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2)))
30242 indirect_string_node **slot
30243 = debug_str_hash->find_slot_with_hash (s->str,
30244 htab_hash_string (s->str),
30245 INSERT);
30246 gcc_assert (*slot == NULL);
30247 *slot = s;
30252 /* Mark DIE and its children as removed. */
30254 static void
30255 mark_removed (dw_die_ref die)
30257 dw_die_ref c;
30258 die->removed = true;
30259 FOR_EACH_CHILD (die, c, mark_removed (c));
30262 /* Remove from the tree DIE any dies that aren't marked. */
30264 static void
30265 prune_unused_types_prune (dw_die_ref die)
30267 dw_die_ref c;
30269 gcc_assert (die->die_mark);
30270 prune_unused_types_update_strings (die);
30272 if (! die->die_child)
30273 return;
30275 c = die->die_child;
30276 do {
30277 dw_die_ref prev = c, next;
30278 for (c = c->die_sib; ! c->die_mark; c = next)
30279 if (c == die->die_child)
30281 /* No marked children between 'prev' and the end of the list. */
30282 if (prev == c)
30283 /* No marked children at all. */
30284 die->die_child = NULL;
30285 else
30287 prev->die_sib = c->die_sib;
30288 die->die_child = prev;
30290 c->die_sib = NULL;
30291 mark_removed (c);
30292 return;
30294 else
30296 next = c->die_sib;
30297 c->die_sib = NULL;
30298 mark_removed (c);
30301 if (c != prev->die_sib)
30302 prev->die_sib = c;
30303 prune_unused_types_prune (c);
30304 } while (c != die->die_child);
30307 /* Remove dies representing declarations that we never use. */
30309 static void
30310 prune_unused_types (void)
30312 unsigned int i;
30313 limbo_die_node *node;
30314 comdat_type_node *ctnode;
30315 pubname_entry *pub;
30316 dw_die_ref base_type;
30318 #if ENABLE_ASSERT_CHECKING
30319 /* All the marks should already be clear. */
30320 verify_marks_clear (comp_unit_die ());
30321 for (node = limbo_die_list; node; node = node->next)
30322 verify_marks_clear (node->die);
30323 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30324 verify_marks_clear (ctnode->root_die);
30325 #endif /* ENABLE_ASSERT_CHECKING */
30327 /* Mark types that are used in global variables. */
30328 premark_types_used_by_global_vars ();
30330 /* Mark variables used in the symtab. */
30331 if (flag_debug_only_used_symbols)
30332 premark_used_variables ();
30334 /* Set the mark on nodes that are actually used. */
30335 prune_unused_types_walk (comp_unit_die ());
30336 for (node = limbo_die_list; node; node = node->next)
30337 prune_unused_types_walk (node->die);
30338 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30340 prune_unused_types_walk (ctnode->root_die);
30341 prune_unused_types_mark (ctnode->type_die, 1);
30344 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
30345 are unusual in that they are pubnames that are the children of pubtypes.
30346 They should only be marked via their parent DW_TAG_enumeration_type die,
30347 not as roots in themselves. */
30348 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
30349 if (pub->die->die_tag != DW_TAG_enumerator)
30350 prune_unused_types_mark (pub->die, 1);
30351 for (i = 0; base_types.iterate (i, &base_type); i++)
30352 prune_unused_types_mark (base_type, 1);
30354 /* Also set the mark on nodes that could be referenced by
30355 DW_TAG_call_site DW_AT_call_origin (i.e. direct call callees) or
30356 by DW_TAG_inlined_subroutine origins. */
30357 cgraph_node *cnode;
30358 FOR_EACH_FUNCTION (cnode)
30359 if (cnode->referred_to_p (false))
30361 dw_die_ref die = lookup_decl_die (cnode->decl);
30362 if (die == NULL || die->die_mark)
30363 continue;
30364 for (cgraph_edge *e = cnode->callers; e; e = e->next_caller)
30365 if (e->caller != cnode)
30367 prune_unused_types_mark (die, 1);
30368 break;
30372 if (debug_str_hash)
30373 debug_str_hash->empty ();
30374 if (skeleton_debug_str_hash)
30375 skeleton_debug_str_hash->empty ();
30376 prune_unused_types_prune (comp_unit_die ());
30377 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
30379 node = *pnode;
30380 if (!node->die->die_mark)
30381 *pnode = node->next;
30382 else
30384 prune_unused_types_prune (node->die);
30385 pnode = &node->next;
30388 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30389 prune_unused_types_prune (ctnode->root_die);
30391 /* Leave the marks clear. */
30392 prune_unmark_dies (comp_unit_die ());
30393 for (node = limbo_die_list; node; node = node->next)
30394 prune_unmark_dies (node->die);
30395 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30396 prune_unmark_dies (ctnode->root_die);
30399 /* Helpers to manipulate hash table of comdat type units. */
30401 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
30403 static inline hashval_t hash (const comdat_type_node *);
30404 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
30407 inline hashval_t
30408 comdat_type_hasher::hash (const comdat_type_node *type_node)
30410 hashval_t h;
30411 memcpy (&h, type_node->signature, sizeof (h));
30412 return h;
30415 inline bool
30416 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
30417 const comdat_type_node *type_node_2)
30419 return (! memcmp (type_node_1->signature, type_node_2->signature,
30420 DWARF_TYPE_SIGNATURE_SIZE));
30423 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
30424 to the location it would have been added, should we know its
30425 DECL_ASSEMBLER_NAME when we added other attributes. This will
30426 probably improve compactness of debug info, removing equivalent
30427 abbrevs, and hide any differences caused by deferring the
30428 computation of the assembler name, triggered by e.g. PCH. */
30430 static inline void
30431 move_linkage_attr (dw_die_ref die)
30433 unsigned ix = vec_safe_length (die->die_attr);
30434 dw_attr_node linkage = (*die->die_attr)[ix - 1];
30436 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
30437 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
30439 while (--ix > 0)
30441 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
30443 if (prev->dw_attr == DW_AT_decl_line
30444 || prev->dw_attr == DW_AT_decl_column
30445 || prev->dw_attr == DW_AT_name)
30446 break;
30449 if (ix != vec_safe_length (die->die_attr) - 1)
30451 die->die_attr->pop ();
30452 die->die_attr->quick_insert (ix, linkage);
30456 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
30457 referenced from typed stack ops and count how often they are used. */
30459 static void
30460 mark_base_types (dw_loc_descr_ref loc)
30462 dw_die_ref base_type = NULL;
30464 for (; loc; loc = loc->dw_loc_next)
30466 switch (loc->dw_loc_opc)
30468 case DW_OP_regval_type:
30469 case DW_OP_deref_type:
30470 case DW_OP_GNU_regval_type:
30471 case DW_OP_GNU_deref_type:
30472 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
30473 break;
30474 case DW_OP_convert:
30475 case DW_OP_reinterpret:
30476 case DW_OP_GNU_convert:
30477 case DW_OP_GNU_reinterpret:
30478 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
30479 continue;
30480 /* FALLTHRU */
30481 case DW_OP_const_type:
30482 case DW_OP_GNU_const_type:
30483 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
30484 break;
30485 case DW_OP_entry_value:
30486 case DW_OP_GNU_entry_value:
30487 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
30488 continue;
30489 default:
30490 continue;
30492 gcc_assert (base_type->die_parent == comp_unit_die ());
30493 if (base_type->die_mark)
30494 base_type->die_mark++;
30495 else
30497 base_types.safe_push (base_type);
30498 base_type->die_mark = 1;
30503 /* Stripped-down variant of resolve_addr, mark DW_TAG_base_type nodes
30504 referenced from typed stack ops and count how often they are used. */
30506 static void
30507 mark_base_types (dw_die_ref die)
30509 dw_die_ref c;
30510 dw_attr_node *a;
30511 dw_loc_list_ref *curr;
30512 unsigned ix;
30514 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30515 switch (AT_class (a))
30517 case dw_val_class_loc_list:
30518 curr = AT_loc_list_ptr (a);
30519 while (*curr)
30521 mark_base_types ((*curr)->expr);
30522 curr = &(*curr)->dw_loc_next;
30524 break;
30526 case dw_val_class_loc:
30527 mark_base_types (AT_loc (a));
30528 break;
30530 default:
30531 break;
30534 FOR_EACH_CHILD (die, c, mark_base_types (c));
30537 /* Comparison function for sorting marked base types. */
30539 static int
30540 base_type_cmp (const void *x, const void *y)
30542 dw_die_ref dx = *(const dw_die_ref *) x;
30543 dw_die_ref dy = *(const dw_die_ref *) y;
30544 unsigned int byte_size1, byte_size2;
30545 unsigned int encoding1, encoding2;
30546 unsigned int align1, align2;
30547 if (dx->die_mark > dy->die_mark)
30548 return -1;
30549 if (dx->die_mark < dy->die_mark)
30550 return 1;
30551 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
30552 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
30553 if (byte_size1 < byte_size2)
30554 return 1;
30555 if (byte_size1 > byte_size2)
30556 return -1;
30557 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
30558 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
30559 if (encoding1 < encoding2)
30560 return 1;
30561 if (encoding1 > encoding2)
30562 return -1;
30563 align1 = get_AT_unsigned (dx, DW_AT_alignment);
30564 align2 = get_AT_unsigned (dy, DW_AT_alignment);
30565 if (align1 < align2)
30566 return 1;
30567 if (align1 > align2)
30568 return -1;
30569 return 0;
30572 /* Move base types marked by mark_base_types as early as possible
30573 in the CU, sorted by decreasing usage count both to make the
30574 uleb128 references as small as possible and to make sure they
30575 will have die_offset already computed by calc_die_sizes when
30576 sizes of typed stack loc ops is computed. */
30578 static void
30579 move_marked_base_types (void)
30581 unsigned int i;
30582 dw_die_ref base_type, die, c;
30584 if (base_types.is_empty ())
30585 return;
30587 /* Sort by decreasing usage count, they will be added again in that
30588 order later on. */
30589 base_types.qsort (base_type_cmp);
30590 die = comp_unit_die ();
30591 c = die->die_child;
30594 dw_die_ref prev = c;
30595 c = c->die_sib;
30596 while (c->die_mark)
30598 remove_child_with_prev (c, prev);
30599 /* As base types got marked, there must be at least
30600 one node other than DW_TAG_base_type. */
30601 gcc_assert (die->die_child != NULL);
30602 c = prev->die_sib;
30605 while (c != die->die_child);
30606 gcc_assert (die->die_child);
30607 c = die->die_child;
30608 for (i = 0; base_types.iterate (i, &base_type); i++)
30610 base_type->die_mark = 0;
30611 base_type->die_sib = c->die_sib;
30612 c->die_sib = base_type;
30613 c = base_type;
30617 /* Helper function for resolve_addr, attempt to resolve
30618 one CONST_STRING, return true if successful. Similarly verify that
30619 SYMBOL_REFs refer to variables emitted in the current CU. */
30621 static bool
30622 resolve_one_addr (rtx *addr)
30624 rtx rtl = *addr;
30626 if (GET_CODE (rtl) == CONST_STRING)
30628 size_t len = strlen (XSTR (rtl, 0)) + 1;
30629 tree t = build_string (len, XSTR (rtl, 0));
30630 tree tlen = size_int (len - 1);
30631 TREE_TYPE (t)
30632 = build_array_type (char_type_node, build_index_type (tlen));
30633 rtl = lookup_constant_def (t);
30634 if (!rtl || !MEM_P (rtl))
30635 return false;
30636 rtl = XEXP (rtl, 0);
30637 if (GET_CODE (rtl) == SYMBOL_REF
30638 && SYMBOL_REF_DECL (rtl)
30639 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
30640 return false;
30641 vec_safe_push (used_rtx_array, rtl);
30642 *addr = rtl;
30643 return true;
30646 if (GET_CODE (rtl) == SYMBOL_REF
30647 && SYMBOL_REF_DECL (rtl))
30649 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
30651 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
30652 return false;
30654 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
30655 return false;
30658 if (GET_CODE (rtl) == CONST)
30660 subrtx_ptr_iterator::array_type array;
30661 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
30662 if (!resolve_one_addr (*iter))
30663 return false;
30666 return true;
30669 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
30670 if possible, and create DW_TAG_dwarf_procedure that can be referenced
30671 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
30673 static rtx
30674 string_cst_pool_decl (tree t)
30676 rtx rtl = output_constant_def (t, 1);
30677 unsigned char *array;
30678 dw_loc_descr_ref l;
30679 tree decl;
30680 size_t len;
30681 dw_die_ref ref;
30683 if (!rtl || !MEM_P (rtl))
30684 return NULL_RTX;
30685 rtl = XEXP (rtl, 0);
30686 if (GET_CODE (rtl) != SYMBOL_REF
30687 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
30688 return NULL_RTX;
30690 decl = SYMBOL_REF_DECL (rtl);
30691 if (!lookup_decl_die (decl))
30693 len = TREE_STRING_LENGTH (t);
30694 vec_safe_push (used_rtx_array, rtl);
30695 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
30696 array = ggc_vec_alloc<unsigned char> (len);
30697 memcpy (array, TREE_STRING_POINTER (t), len);
30698 l = new_loc_descr (DW_OP_implicit_value, len, 0);
30699 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
30700 l->dw_loc_oprnd2.v.val_vec.length = len;
30701 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
30702 l->dw_loc_oprnd2.v.val_vec.array = array;
30703 add_AT_loc (ref, DW_AT_location, l);
30704 equate_decl_number_to_die (decl, ref);
30706 return rtl;
30709 /* Helper function of resolve_addr_in_expr. LOC is
30710 a DW_OP_addr followed by DW_OP_stack_value, either at the start
30711 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
30712 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
30713 with DW_OP_implicit_pointer if possible
30714 and return true, if unsuccessful, return false. */
30716 static bool
30717 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
30719 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
30720 HOST_WIDE_INT offset = 0;
30721 dw_die_ref ref = NULL;
30722 tree decl;
30724 if (GET_CODE (rtl) == CONST
30725 && GET_CODE (XEXP (rtl, 0)) == PLUS
30726 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
30728 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
30729 rtl = XEXP (XEXP (rtl, 0), 0);
30731 if (GET_CODE (rtl) == CONST_STRING)
30733 size_t len = strlen (XSTR (rtl, 0)) + 1;
30734 tree t = build_string (len, XSTR (rtl, 0));
30735 tree tlen = size_int (len - 1);
30737 TREE_TYPE (t)
30738 = build_array_type (char_type_node, build_index_type (tlen));
30739 rtl = string_cst_pool_decl (t);
30740 if (!rtl)
30741 return false;
30743 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
30745 decl = SYMBOL_REF_DECL (rtl);
30746 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
30748 ref = lookup_decl_die (decl);
30749 if (ref && (get_AT (ref, DW_AT_location)
30750 || get_AT (ref, DW_AT_const_value)))
30752 loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
30753 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30754 loc->dw_loc_oprnd1.val_entry = NULL;
30755 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30756 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30757 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
30758 loc->dw_loc_oprnd2.v.val_int = offset;
30759 return true;
30763 return false;
30766 /* Helper function for resolve_addr, handle one location
30767 expression, return false if at least one CONST_STRING or SYMBOL_REF in
30768 the location list couldn't be resolved. */
30770 static bool
30771 resolve_addr_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
30773 dw_loc_descr_ref keep = NULL;
30774 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
30775 switch (loc->dw_loc_opc)
30777 case DW_OP_addr:
30778 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
30780 if ((prev == NULL
30781 || prev->dw_loc_opc == DW_OP_piece
30782 || prev->dw_loc_opc == DW_OP_bit_piece)
30783 && loc->dw_loc_next
30784 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
30785 && (!dwarf_strict || dwarf_version >= 5)
30786 && optimize_one_addr_into_implicit_ptr (loc))
30787 break;
30788 return false;
30790 break;
30791 case DW_OP_GNU_addr_index:
30792 case DW_OP_addrx:
30793 case DW_OP_GNU_const_index:
30794 case DW_OP_constx:
30795 if ((loc->dw_loc_opc == DW_OP_GNU_addr_index
30796 || loc->dw_loc_opc == DW_OP_addrx)
30797 || ((loc->dw_loc_opc == DW_OP_GNU_const_index
30798 || loc->dw_loc_opc == DW_OP_constx)
30799 && loc->dtprel))
30801 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
30802 if (!resolve_one_addr (&rtl))
30803 return false;
30804 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
30805 loc->dw_loc_oprnd1.val_entry
30806 = add_addr_table_entry (rtl, ate_kind_rtx);
30808 break;
30809 case DW_OP_const4u:
30810 case DW_OP_const8u:
30811 if (loc->dtprel
30812 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
30813 return false;
30814 break;
30815 case DW_OP_plus_uconst:
30816 if (size_of_loc_descr (loc)
30817 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
30819 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
30821 dw_loc_descr_ref repl
30822 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
30823 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
30824 add_loc_descr (&repl, loc->dw_loc_next);
30825 *loc = *repl;
30827 break;
30828 case DW_OP_implicit_value:
30829 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
30830 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
30831 return false;
30832 break;
30833 case DW_OP_implicit_pointer:
30834 case DW_OP_GNU_implicit_pointer:
30835 case DW_OP_GNU_parameter_ref:
30836 case DW_OP_GNU_variable_value:
30837 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30839 dw_die_ref ref
30840 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
30841 if (ref == NULL)
30842 return false;
30843 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30844 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30845 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30847 if (loc->dw_loc_opc == DW_OP_GNU_variable_value)
30849 if (prev == NULL
30850 && loc->dw_loc_next == NULL
30851 && AT_class (a) == dw_val_class_loc)
30852 switch (a->dw_attr)
30854 /* Following attributes allow both exprloc and reference,
30855 so if the whole expression is DW_OP_GNU_variable_value
30856 alone we could transform it into reference. */
30857 case DW_AT_byte_size:
30858 case DW_AT_bit_size:
30859 case DW_AT_lower_bound:
30860 case DW_AT_upper_bound:
30861 case DW_AT_bit_stride:
30862 case DW_AT_count:
30863 case DW_AT_allocated:
30864 case DW_AT_associated:
30865 case DW_AT_byte_stride:
30866 a->dw_attr_val.val_class = dw_val_class_die_ref;
30867 a->dw_attr_val.val_entry = NULL;
30868 a->dw_attr_val.v.val_die_ref.die
30869 = loc->dw_loc_oprnd1.v.val_die_ref.die;
30870 a->dw_attr_val.v.val_die_ref.external = 0;
30871 return true;
30872 default:
30873 break;
30875 if (dwarf_strict)
30876 return false;
30878 break;
30879 case DW_OP_const_type:
30880 case DW_OP_regval_type:
30881 case DW_OP_deref_type:
30882 case DW_OP_convert:
30883 case DW_OP_reinterpret:
30884 case DW_OP_GNU_const_type:
30885 case DW_OP_GNU_regval_type:
30886 case DW_OP_GNU_deref_type:
30887 case DW_OP_GNU_convert:
30888 case DW_OP_GNU_reinterpret:
30889 while (loc->dw_loc_next
30890 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
30891 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
30893 dw_die_ref base1, base2;
30894 unsigned enc1, enc2, size1, size2;
30895 if (loc->dw_loc_opc == DW_OP_regval_type
30896 || loc->dw_loc_opc == DW_OP_deref_type
30897 || loc->dw_loc_opc == DW_OP_GNU_regval_type
30898 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
30899 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
30900 else if (loc->dw_loc_oprnd1.val_class
30901 == dw_val_class_unsigned_const)
30902 break;
30903 else
30904 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
30905 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
30906 == dw_val_class_unsigned_const)
30907 break;
30908 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
30909 gcc_assert (base1->die_tag == DW_TAG_base_type
30910 && base2->die_tag == DW_TAG_base_type);
30911 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
30912 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
30913 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
30914 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
30915 if (size1 == size2
30916 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
30917 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
30918 && loc != keep)
30919 || enc1 == enc2))
30921 /* Optimize away next DW_OP_convert after
30922 adjusting LOC's base type die reference. */
30923 if (loc->dw_loc_opc == DW_OP_regval_type
30924 || loc->dw_loc_opc == DW_OP_deref_type
30925 || loc->dw_loc_opc == DW_OP_GNU_regval_type
30926 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
30927 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
30928 else
30929 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
30930 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
30931 continue;
30933 /* Don't change integer DW_OP_convert after e.g. floating
30934 point typed stack entry. */
30935 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
30936 keep = loc->dw_loc_next;
30937 break;
30939 break;
30940 default:
30941 break;
30943 return true;
30946 /* Helper function of resolve_addr. DIE had DW_AT_location of
30947 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
30948 and DW_OP_addr couldn't be resolved. resolve_addr has already
30949 removed the DW_AT_location attribute. This function attempts to
30950 add a new DW_AT_location attribute with DW_OP_implicit_pointer
30951 to it or DW_AT_const_value attribute, if possible. */
30953 static void
30954 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
30956 if (!VAR_P (decl)
30957 || lookup_decl_die (decl) != die
30958 || DECL_EXTERNAL (decl)
30959 || !TREE_STATIC (decl)
30960 || DECL_INITIAL (decl) == NULL_TREE
30961 || DECL_P (DECL_INITIAL (decl))
30962 || get_AT (die, DW_AT_const_value))
30963 return;
30965 tree init = DECL_INITIAL (decl);
30966 HOST_WIDE_INT offset = 0;
30967 /* For variables that have been optimized away and thus
30968 don't have a memory location, see if we can emit
30969 DW_AT_const_value instead. */
30970 if (tree_add_const_value_attribute (die, init))
30971 return;
30972 if (dwarf_strict && dwarf_version < 5)
30973 return;
30974 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
30975 and ADDR_EXPR refers to a decl that has DW_AT_location or
30976 DW_AT_const_value (but isn't addressable, otherwise
30977 resolving the original DW_OP_addr wouldn't fail), see if
30978 we can add DW_OP_implicit_pointer. */
30979 STRIP_NOPS (init);
30980 if (TREE_CODE (init) == POINTER_PLUS_EXPR
30981 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
30983 offset = tree_to_shwi (TREE_OPERAND (init, 1));
30984 init = TREE_OPERAND (init, 0);
30985 STRIP_NOPS (init);
30987 if (TREE_CODE (init) != ADDR_EXPR)
30988 return;
30989 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
30990 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
30991 || (VAR_P (TREE_OPERAND (init, 0))
30992 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
30993 && TREE_OPERAND (init, 0) != decl))
30995 dw_die_ref ref;
30996 dw_loc_descr_ref l;
30998 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
31000 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
31001 if (!rtl)
31002 return;
31003 decl = SYMBOL_REF_DECL (rtl);
31005 else
31006 decl = TREE_OPERAND (init, 0);
31007 ref = lookup_decl_die (decl);
31008 if (ref == NULL
31009 || (!get_AT (ref, DW_AT_location)
31010 && !get_AT (ref, DW_AT_const_value)))
31011 return;
31012 l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
31013 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
31014 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
31015 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
31016 add_AT_loc (die, DW_AT_location, l);
31020 /* Return NULL if l is a DWARF expression, or first op that is not
31021 valid DWARF expression. */
31023 static dw_loc_descr_ref
31024 non_dwarf_expression (dw_loc_descr_ref l)
31026 while (l)
31028 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
31029 return l;
31030 switch (l->dw_loc_opc)
31032 case DW_OP_regx:
31033 case DW_OP_implicit_value:
31034 case DW_OP_stack_value:
31035 case DW_OP_implicit_pointer:
31036 case DW_OP_GNU_implicit_pointer:
31037 case DW_OP_GNU_parameter_ref:
31038 case DW_OP_piece:
31039 case DW_OP_bit_piece:
31040 return l;
31041 default:
31042 break;
31044 l = l->dw_loc_next;
31046 return NULL;
31049 /* Return adjusted copy of EXPR:
31050 If it is empty DWARF expression, return it.
31051 If it is valid non-empty DWARF expression,
31052 return copy of EXPR with DW_OP_deref appended to it.
31053 If it is DWARF expression followed by DW_OP_reg{N,x}, return
31054 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended.
31055 If it is DWARF expression followed by DW_OP_stack_value, return
31056 copy of the DWARF expression without anything appended.
31057 Otherwise, return NULL. */
31059 static dw_loc_descr_ref
31060 copy_deref_exprloc (dw_loc_descr_ref expr)
31062 dw_loc_descr_ref tail = NULL;
31064 if (expr == NULL)
31065 return NULL;
31067 dw_loc_descr_ref l = non_dwarf_expression (expr);
31068 if (l && l->dw_loc_next)
31069 return NULL;
31071 if (l)
31073 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
31074 tail = new_loc_descr ((enum dwarf_location_atom)
31075 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
31076 0, 0);
31077 else
31078 switch (l->dw_loc_opc)
31080 case DW_OP_regx:
31081 tail = new_loc_descr (DW_OP_bregx,
31082 l->dw_loc_oprnd1.v.val_unsigned, 0);
31083 break;
31084 case DW_OP_stack_value:
31085 break;
31086 default:
31087 return NULL;
31090 else
31091 tail = new_loc_descr (DW_OP_deref, 0, 0);
31093 dw_loc_descr_ref ret = NULL, *p = &ret;
31094 while (expr != l)
31096 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
31097 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
31098 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
31099 p = &(*p)->dw_loc_next;
31100 expr = expr->dw_loc_next;
31102 *p = tail;
31103 return ret;
31106 /* For DW_AT_string_length attribute with DW_OP_GNU_variable_value
31107 reference to a variable or argument, adjust it if needed and return:
31108 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
31109 attribute if present should be removed
31110 0 keep the attribute perhaps with minor modifications, no need to rescan
31111 1 if the attribute has been successfully adjusted. */
31113 static int
31114 optimize_string_length (dw_attr_node *a)
31116 dw_loc_descr_ref l = AT_loc (a), lv;
31117 dw_die_ref die;
31118 if (l->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
31120 tree decl = l->dw_loc_oprnd1.v.val_decl_ref;
31121 die = lookup_decl_die (decl);
31122 if (die)
31124 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
31125 l->dw_loc_oprnd1.v.val_die_ref.die = die;
31126 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
31128 else
31129 return -1;
31131 else
31132 die = l->dw_loc_oprnd1.v.val_die_ref.die;
31134 /* DWARF5 allows reference class, so we can then reference the DIE.
31135 Only do this for DW_OP_GNU_variable_value DW_OP_stack_value. */
31136 if (l->dw_loc_next != NULL && dwarf_version >= 5)
31138 a->dw_attr_val.val_class = dw_val_class_die_ref;
31139 a->dw_attr_val.val_entry = NULL;
31140 a->dw_attr_val.v.val_die_ref.die = die;
31141 a->dw_attr_val.v.val_die_ref.external = 0;
31142 return 0;
31145 dw_attr_node *av = get_AT (die, DW_AT_location);
31146 dw_loc_list_ref d;
31147 bool non_dwarf_expr = false;
31149 if (av == NULL)
31150 return dwarf_strict ? -1 : 0;
31151 switch (AT_class (av))
31153 case dw_val_class_loc_list:
31154 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
31155 if (d->expr && non_dwarf_expression (d->expr))
31156 non_dwarf_expr = true;
31157 break;
31158 case dw_val_class_view_list:
31159 gcc_unreachable ();
31160 case dw_val_class_loc:
31161 lv = AT_loc (av);
31162 if (lv == NULL)
31163 return dwarf_strict ? -1 : 0;
31164 if (non_dwarf_expression (lv))
31165 non_dwarf_expr = true;
31166 break;
31167 default:
31168 return dwarf_strict ? -1 : 0;
31171 /* If it is safe to transform DW_OP_GNU_variable_value DW_OP_stack_value
31172 into DW_OP_call4 or DW_OP_GNU_variable_value into
31173 DW_OP_call4 DW_OP_deref, do so. */
31174 if (!non_dwarf_expr
31175 && (l->dw_loc_next != NULL || AT_class (av) == dw_val_class_loc))
31177 l->dw_loc_opc = DW_OP_call4;
31178 if (l->dw_loc_next)
31179 l->dw_loc_next = NULL;
31180 else
31181 l->dw_loc_next = new_loc_descr (DW_OP_deref, 0, 0);
31182 return 0;
31185 /* For DW_OP_GNU_variable_value DW_OP_stack_value, we can just
31186 copy over the DW_AT_location attribute from die to a. */
31187 if (l->dw_loc_next != NULL)
31189 a->dw_attr_val = av->dw_attr_val;
31190 return 1;
31193 dw_loc_list_ref list, *p;
31194 switch (AT_class (av))
31196 case dw_val_class_loc_list:
31197 p = &list;
31198 list = NULL;
31199 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
31201 lv = copy_deref_exprloc (d->expr);
31202 if (lv)
31204 *p = new_loc_list (lv, d->begin, d->vbegin, d->end, d->vend, d->section);
31205 p = &(*p)->dw_loc_next;
31207 else if (!dwarf_strict && d->expr)
31208 return 0;
31210 if (list == NULL)
31211 return dwarf_strict ? -1 : 0;
31212 a->dw_attr_val.val_class = dw_val_class_loc_list;
31213 gen_llsym (list);
31214 *AT_loc_list_ptr (a) = list;
31215 return 1;
31216 case dw_val_class_loc:
31217 lv = copy_deref_exprloc (AT_loc (av));
31218 if (lv == NULL)
31219 return dwarf_strict ? -1 : 0;
31220 a->dw_attr_val.v.val_loc = lv;
31221 return 1;
31222 default:
31223 gcc_unreachable ();
31227 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
31228 an address in .rodata section if the string literal is emitted there,
31229 or remove the containing location list or replace DW_AT_const_value
31230 with DW_AT_location and empty location expression, if it isn't found
31231 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
31232 to something that has been emitted in the current CU. */
31234 static void
31235 resolve_addr (dw_die_ref die)
31237 dw_die_ref c;
31238 dw_attr_node *a;
31239 dw_loc_list_ref *curr, *start, loc;
31240 unsigned ix;
31241 bool remove_AT_byte_size = false;
31243 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31244 switch (AT_class (a))
31246 case dw_val_class_loc_list:
31247 start = curr = AT_loc_list_ptr (a);
31248 loc = *curr;
31249 gcc_assert (loc);
31250 /* The same list can be referenced more than once. See if we have
31251 already recorded the result from a previous pass. */
31252 if (loc->replaced)
31253 *curr = loc->dw_loc_next;
31254 else if (!loc->resolved_addr)
31256 /* As things stand, we do not expect or allow one die to
31257 reference a suffix of another die's location list chain.
31258 References must be identical or completely separate.
31259 There is therefore no need to cache the result of this
31260 pass on any list other than the first; doing so
31261 would lead to unnecessary writes. */
31262 while (*curr)
31264 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
31265 if (!resolve_addr_in_expr (a, (*curr)->expr))
31267 dw_loc_list_ref next = (*curr)->dw_loc_next;
31268 dw_loc_descr_ref l = (*curr)->expr;
31270 if (next && (*curr)->ll_symbol)
31272 gcc_assert (!next->ll_symbol);
31273 next->ll_symbol = (*curr)->ll_symbol;
31274 next->vl_symbol = (*curr)->vl_symbol;
31276 if (dwarf_split_debug_info)
31277 remove_loc_list_addr_table_entries (l);
31278 *curr = next;
31280 else
31282 mark_base_types ((*curr)->expr);
31283 curr = &(*curr)->dw_loc_next;
31286 if (loc == *start)
31287 loc->resolved_addr = 1;
31288 else
31290 loc->replaced = 1;
31291 loc->dw_loc_next = *start;
31294 if (!*start)
31296 remove_AT (die, a->dw_attr);
31297 ix--;
31299 break;
31300 case dw_val_class_view_list:
31302 gcc_checking_assert (a->dw_attr == DW_AT_GNU_locviews);
31303 gcc_checking_assert (dwarf2out_locviews_in_attribute ());
31304 dw_val_node *llnode
31305 = view_list_to_loc_list_val_node (&a->dw_attr_val);
31306 /* If we no longer have a loclist, or it no longer needs
31307 views, drop this attribute. */
31308 if (!llnode || !llnode->v.val_loc_list->vl_symbol)
31310 remove_AT (die, a->dw_attr);
31311 ix--;
31313 break;
31315 case dw_val_class_loc:
31317 dw_loc_descr_ref l = AT_loc (a);
31318 /* DW_OP_GNU_variable_value DW_OP_stack_value or
31319 DW_OP_GNU_variable_value in DW_AT_string_length can be converted
31320 into DW_OP_call4 or DW_OP_call4 DW_OP_deref, which is standard
31321 DWARF4 unlike DW_OP_GNU_variable_value. Or for DWARF5
31322 DW_OP_GNU_variable_value DW_OP_stack_value can be replaced
31323 with DW_FORM_ref referencing the same DIE as
31324 DW_OP_GNU_variable_value used to reference. */
31325 if (a->dw_attr == DW_AT_string_length
31326 && l
31327 && l->dw_loc_opc == DW_OP_GNU_variable_value
31328 && (l->dw_loc_next == NULL
31329 || (l->dw_loc_next->dw_loc_next == NULL
31330 && l->dw_loc_next->dw_loc_opc == DW_OP_stack_value)))
31332 switch (optimize_string_length (a))
31334 case -1:
31335 remove_AT (die, a->dw_attr);
31336 ix--;
31337 /* If we drop DW_AT_string_length, we need to drop also
31338 DW_AT_{string_length_,}byte_size. */
31339 remove_AT_byte_size = true;
31340 continue;
31341 default:
31342 break;
31343 case 1:
31344 /* Even if we keep the optimized DW_AT_string_length,
31345 it might have changed AT_class, so process it again. */
31346 ix--;
31347 continue;
31350 /* For -gdwarf-2 don't attempt to optimize
31351 DW_AT_data_member_location containing
31352 DW_OP_plus_uconst - older consumers might
31353 rely on it being that op instead of a more complex,
31354 but shorter, location description. */
31355 if ((dwarf_version > 2
31356 || a->dw_attr != DW_AT_data_member_location
31357 || l == NULL
31358 || l->dw_loc_opc != DW_OP_plus_uconst
31359 || l->dw_loc_next != NULL)
31360 && !resolve_addr_in_expr (a, l))
31362 if (dwarf_split_debug_info)
31363 remove_loc_list_addr_table_entries (l);
31364 if (l != NULL
31365 && l->dw_loc_next == NULL
31366 && l->dw_loc_opc == DW_OP_addr
31367 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
31368 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
31369 && a->dw_attr == DW_AT_location)
31371 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
31372 remove_AT (die, a->dw_attr);
31373 ix--;
31374 optimize_location_into_implicit_ptr (die, decl);
31375 break;
31377 if (a->dw_attr == DW_AT_string_length)
31378 /* If we drop DW_AT_string_length, we need to drop also
31379 DW_AT_{string_length_,}byte_size. */
31380 remove_AT_byte_size = true;
31381 remove_AT (die, a->dw_attr);
31382 ix--;
31384 else
31385 mark_base_types (l);
31387 break;
31388 case dw_val_class_addr:
31389 if (a->dw_attr == DW_AT_const_value
31390 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
31392 if (AT_index (a) != NOT_INDEXED)
31393 remove_addr_table_entry (a->dw_attr_val.val_entry);
31394 remove_AT (die, a->dw_attr);
31395 ix--;
31397 if ((die->die_tag == DW_TAG_call_site
31398 && a->dw_attr == DW_AT_call_origin)
31399 || (die->die_tag == DW_TAG_GNU_call_site
31400 && a->dw_attr == DW_AT_abstract_origin))
31402 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
31403 dw_die_ref tdie = lookup_decl_die (tdecl);
31404 dw_die_ref cdie;
31405 if (tdie == NULL
31406 && DECL_EXTERNAL (tdecl)
31407 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
31408 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
31410 dw_die_ref pdie = cdie;
31411 /* Make sure we don't add these DIEs into type units.
31412 We could emit skeleton DIEs for context (namespaces,
31413 outer structs/classes) and a skeleton DIE for the
31414 innermost context with DW_AT_signature pointing to the
31415 type unit. See PR78835. */
31416 while (pdie && pdie->die_tag != DW_TAG_type_unit)
31417 pdie = pdie->die_parent;
31418 if (pdie == NULL)
31420 /* Creating a full DIE for tdecl is overly expensive and
31421 at this point even wrong when in the LTO phase
31422 as it can end up generating new type DIEs we didn't
31423 output and thus optimize_external_refs will crash. */
31424 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
31425 add_AT_flag (tdie, DW_AT_external, 1);
31426 add_AT_flag (tdie, DW_AT_declaration, 1);
31427 add_linkage_attr (tdie, tdecl);
31428 add_name_and_src_coords_attributes (tdie, tdecl, true);
31429 equate_decl_number_to_die (tdecl, tdie);
31432 if (tdie)
31434 a->dw_attr_val.val_class = dw_val_class_die_ref;
31435 a->dw_attr_val.v.val_die_ref.die = tdie;
31436 a->dw_attr_val.v.val_die_ref.external = 0;
31438 else
31440 if (AT_index (a) != NOT_INDEXED)
31441 remove_addr_table_entry (a->dw_attr_val.val_entry);
31442 remove_AT (die, a->dw_attr);
31443 ix--;
31446 break;
31447 default:
31448 break;
31451 if (remove_AT_byte_size)
31452 remove_AT (die, dwarf_version >= 5
31453 ? DW_AT_string_length_byte_size
31454 : DW_AT_byte_size);
31456 FOR_EACH_CHILD (die, c, resolve_addr (c));
31459 /* Helper routines for optimize_location_lists.
31460 This pass tries to share identical local lists in .debug_loc
31461 section. */
31463 /* Iteratively hash operands of LOC opcode into HSTATE. */
31465 static void
31466 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
31468 dw_val_ref val1 = &loc->dw_loc_oprnd1;
31469 dw_val_ref val2 = &loc->dw_loc_oprnd2;
31471 switch (loc->dw_loc_opc)
31473 case DW_OP_const4u:
31474 case DW_OP_const8u:
31475 if (loc->dtprel)
31476 goto hash_addr;
31477 /* FALLTHRU */
31478 case DW_OP_const1u:
31479 case DW_OP_const1s:
31480 case DW_OP_const2u:
31481 case DW_OP_const2s:
31482 case DW_OP_const4s:
31483 case DW_OP_const8s:
31484 case DW_OP_constu:
31485 case DW_OP_consts:
31486 case DW_OP_pick:
31487 case DW_OP_plus_uconst:
31488 case DW_OP_breg0:
31489 case DW_OP_breg1:
31490 case DW_OP_breg2:
31491 case DW_OP_breg3:
31492 case DW_OP_breg4:
31493 case DW_OP_breg5:
31494 case DW_OP_breg6:
31495 case DW_OP_breg7:
31496 case DW_OP_breg8:
31497 case DW_OP_breg9:
31498 case DW_OP_breg10:
31499 case DW_OP_breg11:
31500 case DW_OP_breg12:
31501 case DW_OP_breg13:
31502 case DW_OP_breg14:
31503 case DW_OP_breg15:
31504 case DW_OP_breg16:
31505 case DW_OP_breg17:
31506 case DW_OP_breg18:
31507 case DW_OP_breg19:
31508 case DW_OP_breg20:
31509 case DW_OP_breg21:
31510 case DW_OP_breg22:
31511 case DW_OP_breg23:
31512 case DW_OP_breg24:
31513 case DW_OP_breg25:
31514 case DW_OP_breg26:
31515 case DW_OP_breg27:
31516 case DW_OP_breg28:
31517 case DW_OP_breg29:
31518 case DW_OP_breg30:
31519 case DW_OP_breg31:
31520 case DW_OP_regx:
31521 case DW_OP_fbreg:
31522 case DW_OP_piece:
31523 case DW_OP_deref_size:
31524 case DW_OP_xderef_size:
31525 hstate.add_object (val1->v.val_int);
31526 break;
31527 case DW_OP_skip:
31528 case DW_OP_bra:
31530 int offset;
31532 gcc_assert (val1->val_class == dw_val_class_loc);
31533 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
31534 hstate.add_object (offset);
31536 break;
31537 case DW_OP_implicit_value:
31538 hstate.add_object (val1->v.val_unsigned);
31539 switch (val2->val_class)
31541 case dw_val_class_const:
31542 hstate.add_object (val2->v.val_int);
31543 break;
31544 case dw_val_class_vec:
31546 unsigned int elt_size = val2->v.val_vec.elt_size;
31547 unsigned int len = val2->v.val_vec.length;
31549 hstate.add_int (elt_size);
31550 hstate.add_int (len);
31551 hstate.add (val2->v.val_vec.array, len * elt_size);
31553 break;
31554 case dw_val_class_const_double:
31555 hstate.add_object (val2->v.val_double.low);
31556 hstate.add_object (val2->v.val_double.high);
31557 break;
31558 case dw_val_class_wide_int:
31559 hstate.add (val2->v.val_wide->get_val (),
31560 get_full_len (*val2->v.val_wide)
31561 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
31562 break;
31563 case dw_val_class_addr:
31564 inchash::add_rtx (val2->v.val_addr, hstate);
31565 break;
31566 default:
31567 gcc_unreachable ();
31569 break;
31570 case DW_OP_bregx:
31571 case DW_OP_bit_piece:
31572 hstate.add_object (val1->v.val_int);
31573 hstate.add_object (val2->v.val_int);
31574 break;
31575 case DW_OP_addr:
31576 hash_addr:
31577 if (loc->dtprel)
31579 unsigned char dtprel = 0xd1;
31580 hstate.add_object (dtprel);
31582 inchash::add_rtx (val1->v.val_addr, hstate);
31583 break;
31584 case DW_OP_GNU_addr_index:
31585 case DW_OP_addrx:
31586 case DW_OP_GNU_const_index:
31587 case DW_OP_constx:
31589 if (loc->dtprel)
31591 unsigned char dtprel = 0xd1;
31592 hstate.add_object (dtprel);
31594 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
31596 break;
31597 case DW_OP_implicit_pointer:
31598 case DW_OP_GNU_implicit_pointer:
31599 hstate.add_int (val2->v.val_int);
31600 break;
31601 case DW_OP_entry_value:
31602 case DW_OP_GNU_entry_value:
31603 hstate.add_object (val1->v.val_loc);
31604 break;
31605 case DW_OP_regval_type:
31606 case DW_OP_deref_type:
31607 case DW_OP_GNU_regval_type:
31608 case DW_OP_GNU_deref_type:
31610 unsigned int byte_size
31611 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
31612 unsigned int encoding
31613 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
31614 hstate.add_object (val1->v.val_int);
31615 hstate.add_object (byte_size);
31616 hstate.add_object (encoding);
31618 break;
31619 case DW_OP_convert:
31620 case DW_OP_reinterpret:
31621 case DW_OP_GNU_convert:
31622 case DW_OP_GNU_reinterpret:
31623 if (val1->val_class == dw_val_class_unsigned_const)
31625 hstate.add_object (val1->v.val_unsigned);
31626 break;
31628 /* FALLTHRU */
31629 case DW_OP_const_type:
31630 case DW_OP_GNU_const_type:
31632 unsigned int byte_size
31633 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
31634 unsigned int encoding
31635 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
31636 hstate.add_object (byte_size);
31637 hstate.add_object (encoding);
31638 if (loc->dw_loc_opc != DW_OP_const_type
31639 && loc->dw_loc_opc != DW_OP_GNU_const_type)
31640 break;
31641 hstate.add_object (val2->val_class);
31642 switch (val2->val_class)
31644 case dw_val_class_const:
31645 hstate.add_object (val2->v.val_int);
31646 break;
31647 case dw_val_class_vec:
31649 unsigned int elt_size = val2->v.val_vec.elt_size;
31650 unsigned int len = val2->v.val_vec.length;
31652 hstate.add_object (elt_size);
31653 hstate.add_object (len);
31654 hstate.add (val2->v.val_vec.array, len * elt_size);
31656 break;
31657 case dw_val_class_const_double:
31658 hstate.add_object (val2->v.val_double.low);
31659 hstate.add_object (val2->v.val_double.high);
31660 break;
31661 case dw_val_class_wide_int:
31662 hstate.add (val2->v.val_wide->get_val (),
31663 get_full_len (*val2->v.val_wide)
31664 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
31665 break;
31666 default:
31667 gcc_unreachable ();
31670 break;
31672 default:
31673 /* Other codes have no operands. */
31674 break;
31678 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
31680 static inline void
31681 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
31683 dw_loc_descr_ref l;
31684 bool sizes_computed = false;
31685 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
31686 size_of_locs (loc);
31688 for (l = loc; l != NULL; l = l->dw_loc_next)
31690 enum dwarf_location_atom opc = l->dw_loc_opc;
31691 hstate.add_object (opc);
31692 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
31694 size_of_locs (loc);
31695 sizes_computed = true;
31697 hash_loc_operands (l, hstate);
31701 /* Compute hash of the whole location list LIST_HEAD. */
31703 static inline void
31704 hash_loc_list (dw_loc_list_ref list_head)
31706 dw_loc_list_ref curr = list_head;
31707 inchash::hash hstate;
31709 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
31711 hstate.add (curr->begin, strlen (curr->begin) + 1);
31712 hstate.add (curr->end, strlen (curr->end) + 1);
31713 hstate.add_object (curr->vbegin);
31714 hstate.add_object (curr->vend);
31715 if (curr->section)
31716 hstate.add (curr->section, strlen (curr->section) + 1);
31717 hash_locs (curr->expr, hstate);
31719 list_head->hash = hstate.end ();
31722 /* Return true if X and Y opcodes have the same operands. */
31724 static inline bool
31725 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
31727 dw_val_ref valx1 = &x->dw_loc_oprnd1;
31728 dw_val_ref valx2 = &x->dw_loc_oprnd2;
31729 dw_val_ref valy1 = &y->dw_loc_oprnd1;
31730 dw_val_ref valy2 = &y->dw_loc_oprnd2;
31732 switch (x->dw_loc_opc)
31734 case DW_OP_const4u:
31735 case DW_OP_const8u:
31736 if (x->dtprel)
31737 goto hash_addr;
31738 /* FALLTHRU */
31739 case DW_OP_const1u:
31740 case DW_OP_const1s:
31741 case DW_OP_const2u:
31742 case DW_OP_const2s:
31743 case DW_OP_const4s:
31744 case DW_OP_const8s:
31745 case DW_OP_constu:
31746 case DW_OP_consts:
31747 case DW_OP_pick:
31748 case DW_OP_plus_uconst:
31749 case DW_OP_breg0:
31750 case DW_OP_breg1:
31751 case DW_OP_breg2:
31752 case DW_OP_breg3:
31753 case DW_OP_breg4:
31754 case DW_OP_breg5:
31755 case DW_OP_breg6:
31756 case DW_OP_breg7:
31757 case DW_OP_breg8:
31758 case DW_OP_breg9:
31759 case DW_OP_breg10:
31760 case DW_OP_breg11:
31761 case DW_OP_breg12:
31762 case DW_OP_breg13:
31763 case DW_OP_breg14:
31764 case DW_OP_breg15:
31765 case DW_OP_breg16:
31766 case DW_OP_breg17:
31767 case DW_OP_breg18:
31768 case DW_OP_breg19:
31769 case DW_OP_breg20:
31770 case DW_OP_breg21:
31771 case DW_OP_breg22:
31772 case DW_OP_breg23:
31773 case DW_OP_breg24:
31774 case DW_OP_breg25:
31775 case DW_OP_breg26:
31776 case DW_OP_breg27:
31777 case DW_OP_breg28:
31778 case DW_OP_breg29:
31779 case DW_OP_breg30:
31780 case DW_OP_breg31:
31781 case DW_OP_regx:
31782 case DW_OP_fbreg:
31783 case DW_OP_piece:
31784 case DW_OP_deref_size:
31785 case DW_OP_xderef_size:
31786 return valx1->v.val_int == valy1->v.val_int;
31787 case DW_OP_skip:
31788 case DW_OP_bra:
31789 /* If splitting debug info, the use of DW_OP_GNU_addr_index
31790 can cause irrelevant differences in dw_loc_addr. */
31791 gcc_assert (valx1->val_class == dw_val_class_loc
31792 && valy1->val_class == dw_val_class_loc
31793 && (dwarf_split_debug_info
31794 || x->dw_loc_addr == y->dw_loc_addr));
31795 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
31796 case DW_OP_implicit_value:
31797 if (valx1->v.val_unsigned != valy1->v.val_unsigned
31798 || valx2->val_class != valy2->val_class)
31799 return false;
31800 switch (valx2->val_class)
31802 case dw_val_class_const:
31803 return valx2->v.val_int == valy2->v.val_int;
31804 case dw_val_class_vec:
31805 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
31806 && valx2->v.val_vec.length == valy2->v.val_vec.length
31807 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
31808 valx2->v.val_vec.elt_size
31809 * valx2->v.val_vec.length) == 0;
31810 case dw_val_class_const_double:
31811 return valx2->v.val_double.low == valy2->v.val_double.low
31812 && valx2->v.val_double.high == valy2->v.val_double.high;
31813 case dw_val_class_wide_int:
31814 return *valx2->v.val_wide == *valy2->v.val_wide;
31815 case dw_val_class_addr:
31816 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
31817 default:
31818 gcc_unreachable ();
31820 case DW_OP_bregx:
31821 case DW_OP_bit_piece:
31822 return valx1->v.val_int == valy1->v.val_int
31823 && valx2->v.val_int == valy2->v.val_int;
31824 case DW_OP_addr:
31825 hash_addr:
31826 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
31827 case DW_OP_GNU_addr_index:
31828 case DW_OP_addrx:
31829 case DW_OP_GNU_const_index:
31830 case DW_OP_constx:
31832 rtx ax1 = valx1->val_entry->addr.rtl;
31833 rtx ay1 = valy1->val_entry->addr.rtl;
31834 return rtx_equal_p (ax1, ay1);
31836 case DW_OP_implicit_pointer:
31837 case DW_OP_GNU_implicit_pointer:
31838 return valx1->val_class == dw_val_class_die_ref
31839 && valx1->val_class == valy1->val_class
31840 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
31841 && valx2->v.val_int == valy2->v.val_int;
31842 case DW_OP_entry_value:
31843 case DW_OP_GNU_entry_value:
31844 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
31845 case DW_OP_const_type:
31846 case DW_OP_GNU_const_type:
31847 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
31848 || valx2->val_class != valy2->val_class)
31849 return false;
31850 switch (valx2->val_class)
31852 case dw_val_class_const:
31853 return valx2->v.val_int == valy2->v.val_int;
31854 case dw_val_class_vec:
31855 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
31856 && valx2->v.val_vec.length == valy2->v.val_vec.length
31857 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
31858 valx2->v.val_vec.elt_size
31859 * valx2->v.val_vec.length) == 0;
31860 case dw_val_class_const_double:
31861 return valx2->v.val_double.low == valy2->v.val_double.low
31862 && valx2->v.val_double.high == valy2->v.val_double.high;
31863 case dw_val_class_wide_int:
31864 return *valx2->v.val_wide == *valy2->v.val_wide;
31865 default:
31866 gcc_unreachable ();
31868 case DW_OP_regval_type:
31869 case DW_OP_deref_type:
31870 case DW_OP_GNU_regval_type:
31871 case DW_OP_GNU_deref_type:
31872 return valx1->v.val_int == valy1->v.val_int
31873 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
31874 case DW_OP_convert:
31875 case DW_OP_reinterpret:
31876 case DW_OP_GNU_convert:
31877 case DW_OP_GNU_reinterpret:
31878 if (valx1->val_class != valy1->val_class)
31879 return false;
31880 if (valx1->val_class == dw_val_class_unsigned_const)
31881 return valx1->v.val_unsigned == valy1->v.val_unsigned;
31882 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
31883 case DW_OP_GNU_parameter_ref:
31884 return valx1->val_class == dw_val_class_die_ref
31885 && valx1->val_class == valy1->val_class
31886 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
31887 default:
31888 /* Other codes have no operands. */
31889 return true;
31893 /* Return true if DWARF location expressions X and Y are the same. */
31895 static inline bool
31896 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
31898 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
31899 if (x->dw_loc_opc != y->dw_loc_opc
31900 || x->dtprel != y->dtprel
31901 || !compare_loc_operands (x, y))
31902 break;
31903 return x == NULL && y == NULL;
31906 /* Hashtable helpers. */
31908 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
31910 static inline hashval_t hash (const dw_loc_list_struct *);
31911 static inline bool equal (const dw_loc_list_struct *,
31912 const dw_loc_list_struct *);
31915 /* Return precomputed hash of location list X. */
31917 inline hashval_t
31918 loc_list_hasher::hash (const dw_loc_list_struct *x)
31920 return x->hash;
31923 /* Return true if location lists A and B are the same. */
31925 inline bool
31926 loc_list_hasher::equal (const dw_loc_list_struct *a,
31927 const dw_loc_list_struct *b)
31929 if (a == b)
31930 return true;
31931 if (a->hash != b->hash)
31932 return false;
31933 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
31934 if (strcmp (a->begin, b->begin) != 0
31935 || strcmp (a->end, b->end) != 0
31936 || (a->section == NULL) != (b->section == NULL)
31937 || (a->section && strcmp (a->section, b->section) != 0)
31938 || a->vbegin != b->vbegin || a->vend != b->vend
31939 || !compare_locs (a->expr, b->expr))
31940 break;
31941 return a == NULL && b == NULL;
31944 typedef hash_table<loc_list_hasher> loc_list_hash_type;
31947 /* Recursively optimize location lists referenced from DIE
31948 children and share them whenever possible. */
31950 static void
31951 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
31953 dw_die_ref c;
31954 dw_attr_node *a;
31955 unsigned ix;
31956 dw_loc_list_struct **slot;
31957 bool drop_locviews = false;
31958 bool has_locviews = false;
31960 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31961 if (AT_class (a) == dw_val_class_loc_list)
31963 dw_loc_list_ref list = AT_loc_list (a);
31964 /* TODO: perform some optimizations here, before hashing
31965 it and storing into the hash table. */
31966 hash_loc_list (list);
31967 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
31968 if (*slot == NULL)
31970 *slot = list;
31971 if (loc_list_has_views (list))
31972 gcc_assert (list->vl_symbol);
31973 else if (list->vl_symbol)
31975 drop_locviews = true;
31976 list->vl_symbol = NULL;
31979 else
31981 if (list->vl_symbol && !(*slot)->vl_symbol)
31982 drop_locviews = true;
31983 a->dw_attr_val.v.val_loc_list = *slot;
31986 else if (AT_class (a) == dw_val_class_view_list)
31988 gcc_checking_assert (a->dw_attr == DW_AT_GNU_locviews);
31989 has_locviews = true;
31993 if (drop_locviews && has_locviews)
31994 remove_AT (die, DW_AT_GNU_locviews);
31996 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
32000 /* Recursively assign each location list a unique index into the debug_addr
32001 section. */
32003 static void
32004 index_location_lists (dw_die_ref die)
32006 dw_die_ref c;
32007 dw_attr_node *a;
32008 unsigned ix;
32010 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
32011 if (AT_class (a) == dw_val_class_loc_list)
32013 dw_loc_list_ref list = AT_loc_list (a);
32014 dw_loc_list_ref curr;
32015 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
32017 /* Don't index an entry that has already been indexed
32018 or won't be output. Make sure skip_loc_list_entry doesn't
32019 call size_of_locs, because that might cause circular dependency,
32020 index_location_lists requiring address table indexes to be
32021 computed, but adding new indexes through add_addr_table_entry
32022 and address table index computation requiring no new additions
32023 to the hash table. In the rare case of DWARF[234] >= 64KB
32024 location expression, we'll just waste unused address table entry
32025 for it. */
32026 if (curr->begin_entry != NULL || skip_loc_list_entry (curr))
32027 continue;
32029 curr->begin_entry
32030 = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
32031 if (dwarf_version >= 5 && !HAVE_AS_LEB128)
32032 curr->end_entry
32033 = add_addr_table_entry (xstrdup (curr->end), ate_kind_label);
32037 FOR_EACH_CHILD (die, c, index_location_lists (c));
32040 /* Optimize location lists referenced from DIE
32041 children and share them whenever possible. */
32043 static void
32044 optimize_location_lists (dw_die_ref die)
32046 loc_list_hash_type htab (500);
32047 optimize_location_lists_1 (die, &htab);
32050 /* Traverse the limbo die list, and add parent/child links. The only
32051 dies without parents that should be here are concrete instances of
32052 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
32053 For concrete instances, we can get the parent die from the abstract
32054 instance. */
32056 static void
32057 flush_limbo_die_list (void)
32059 limbo_die_node *node;
32061 /* get_context_die calls force_decl_die, which can put new DIEs on the
32062 limbo list in LTO mode when nested functions are put in a different
32063 partition than that of their parent function. */
32064 while ((node = limbo_die_list))
32066 dw_die_ref die = node->die;
32067 limbo_die_list = node->next;
32069 if (die->die_parent == NULL)
32071 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
32073 if (origin && origin->die_parent)
32074 add_child_die (origin->die_parent, die);
32075 else if (is_cu_die (die))
32077 else if (seen_error ())
32078 /* It's OK to be confused by errors in the input. */
32079 add_child_die (comp_unit_die (), die);
32080 else
32082 /* In certain situations, the lexical block containing a
32083 nested function can be optimized away, which results
32084 in the nested function die being orphaned. Likewise
32085 with the return type of that nested function. Force
32086 this to be a child of the containing function.
32088 It may happen that even the containing function got fully
32089 inlined and optimized out. In that case we are lost and
32090 assign the empty child. This should not be big issue as
32091 the function is likely unreachable too. */
32092 gcc_assert (node->created_for);
32094 if (DECL_P (node->created_for))
32095 origin = get_context_die (DECL_CONTEXT (node->created_for));
32096 else if (TYPE_P (node->created_for))
32097 origin = scope_die_for (node->created_for, comp_unit_die ());
32098 else
32099 origin = comp_unit_die ();
32101 add_child_die (origin, die);
32107 /* Reset DIEs so we can output them again. */
32109 static void
32110 reset_dies (dw_die_ref die)
32112 dw_die_ref c;
32114 /* Remove stuff we re-generate. */
32115 die->die_mark = 0;
32116 die->die_offset = 0;
32117 die->die_abbrev = 0;
32118 remove_AT (die, DW_AT_sibling);
32120 FOR_EACH_CHILD (die, c, reset_dies (c));
32123 /* reset_indirect_string removed the references coming from DW_AT_name
32124 and DW_AT_comp_dir attributes on compilation unit DIEs. Readd them as
32125 .debug_line_str strings again. */
32127 static void
32128 adjust_name_comp_dir (dw_die_ref die)
32130 for (int i = 0; i < 2; i++)
32132 dwarf_attribute attr_kind = i ? DW_AT_comp_dir : DW_AT_name;
32133 dw_attr_node *a = get_AT (die, attr_kind);
32134 if (a == NULL || a->dw_attr_val.val_class != dw_val_class_str)
32135 continue;
32137 if (!debug_line_str_hash)
32138 debug_line_str_hash
32139 = hash_table<indirect_string_hasher>::create_ggc (10);
32141 struct indirect_string_node *node
32142 = find_AT_string_in_table (a->dw_attr_val.v.val_str->str,
32143 debug_line_str_hash);
32144 set_indirect_string (node);
32145 node->form = DW_FORM_line_strp;
32146 a->dw_attr_val.v.val_str = node;
32150 /* Output stuff that dwarf requires at the end of every file,
32151 and generate the DWARF-2 debugging info. */
32153 static void
32154 dwarf2out_finish (const char *filename)
32156 comdat_type_node *ctnode;
32157 dw_die_ref main_comp_unit_die;
32158 unsigned char checksum[16];
32159 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
32161 /* Generate CTF/BTF debug info. */
32162 if ((ctf_debug_info_level > CTFINFO_LEVEL_NONE
32163 || btf_debuginfo_p ()) && lang_GNU_C ())
32164 ctf_debug_finish (filename);
32166 /* Skip emitting DWARF if not required. */
32167 if (!dwarf_debuginfo_p ())
32168 return;
32170 /* Flush out any latecomers to the limbo party. */
32171 flush_limbo_die_list ();
32173 if (inline_entry_data_table)
32174 gcc_assert (inline_entry_data_table->is_empty ());
32176 if (flag_checking)
32178 verify_die (comp_unit_die ());
32179 for (limbo_die_node *node = cu_die_list; node; node = node->next)
32180 verify_die (node->die);
32183 /* We shouldn't have any symbols with delayed asm names for
32184 DIEs generated after early finish. */
32185 gcc_assert (deferred_asm_name == NULL);
32187 gen_remaining_tmpl_value_param_die_attribute ();
32189 if (flag_generate_lto || flag_generate_offload)
32191 gcc_assert (flag_fat_lto_objects || flag_generate_offload);
32193 /* Prune stuff so that dwarf2out_finish runs successfully
32194 for the fat part of the object. */
32195 reset_dies (comp_unit_die ());
32196 for (limbo_die_node *node = cu_die_list; node; node = node->next)
32197 reset_dies (node->die);
32199 hash_table<comdat_type_hasher> comdat_type_table (100);
32200 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32202 comdat_type_node **slot
32203 = comdat_type_table.find_slot (ctnode, INSERT);
32205 /* Don't reset types twice. */
32206 if (*slot != HTAB_EMPTY_ENTRY)
32207 continue;
32209 /* Remove the pointer to the line table. */
32210 remove_AT (ctnode->root_die, DW_AT_stmt_list);
32212 if (debug_info_level >= DINFO_LEVEL_TERSE)
32213 reset_dies (ctnode->root_die);
32215 *slot = ctnode;
32218 /* Reset die CU symbol so we don't output it twice. */
32219 comp_unit_die ()->die_id.die_symbol = NULL;
32221 /* Remove DW_AT_macro and DW_AT_stmt_list from the early output. */
32222 remove_AT (comp_unit_die (), DW_AT_stmt_list);
32223 if (have_macinfo)
32224 remove_AT (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE);
32226 /* Remove indirect string decisions. */
32227 debug_str_hash->traverse<void *, reset_indirect_string> (NULL);
32228 if (debug_line_str_hash)
32230 debug_line_str_hash->traverse<void *, reset_indirect_string> (NULL);
32231 debug_line_str_hash = NULL;
32232 if (asm_outputs_debug_line_str ())
32234 adjust_name_comp_dir (comp_unit_die ());
32235 for (limbo_die_node *node = cu_die_list; node; node = node->next)
32236 adjust_name_comp_dir (node->die);
32241 #if ENABLE_ASSERT_CHECKING
32243 dw_die_ref die = comp_unit_die (), c;
32244 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
32246 #endif
32247 base_types.truncate (0);
32248 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32249 resolve_addr (ctnode->root_die);
32250 resolve_addr (comp_unit_die ());
32251 move_marked_base_types ();
32253 if (dump_file)
32255 fprintf (dump_file, "DWARF for %s\n", filename);
32256 print_die (comp_unit_die (), dump_file);
32259 /* Initialize sections and labels used for actual assembler output. */
32260 unsigned generation = init_sections_and_labels (false);
32262 /* Traverse the DIE's and add sibling attributes to those DIE's that
32263 have children. */
32264 add_sibling_attributes (comp_unit_die ());
32265 limbo_die_node *node;
32266 for (node = cu_die_list; node; node = node->next)
32267 add_sibling_attributes (node->die);
32268 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32269 add_sibling_attributes (ctnode->root_die);
32271 /* When splitting DWARF info, we put some attributes in the
32272 skeleton compile_unit DIE that remains in the .o, while
32273 most attributes go in the DWO compile_unit_die. */
32274 if (dwarf_split_debug_info)
32276 limbo_die_node *cu;
32277 main_comp_unit_die = gen_compile_unit_die (NULL);
32278 if (dwarf_version >= 5)
32279 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
32280 cu = limbo_die_list;
32281 gcc_assert (cu->die == main_comp_unit_die);
32282 limbo_die_list = limbo_die_list->next;
32283 cu->next = cu_die_list;
32284 cu_die_list = cu;
32286 else
32287 main_comp_unit_die = comp_unit_die ();
32289 /* Output a terminator label for the .text section. */
32290 switch_to_section (text_section);
32291 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
32292 if (cold_text_section)
32294 switch_to_section (cold_text_section);
32295 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
32298 /* We can only use the low/high_pc attributes if all of the code was
32299 in .text. */
32300 if ((!have_multiple_function_sections
32301 && vec_safe_length (switch_text_ranges) < 2)
32302 || (dwarf_version < 3 && dwarf_strict))
32304 const char *end_label = text_end_label;
32305 if (vec_safe_length (switch_text_ranges) == 1)
32306 end_label = (*switch_text_ranges)[0];
32307 /* Don't add if the CU has no associated code. */
32308 if (switch_text_ranges)
32309 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
32310 end_label, true);
32312 else
32314 unsigned fde_idx;
32315 dw_fde_ref fde;
32316 bool range_list_added = false;
32317 if (switch_text_ranges)
32319 const char *prev_loc = text_section_label;
32320 const char *loc;
32321 unsigned idx;
32323 FOR_EACH_VEC_ELT (*switch_text_ranges, idx, loc)
32324 if (prev_loc)
32326 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32327 loc, &range_list_added, true);
32328 prev_loc = NULL;
32330 else
32331 prev_loc = loc;
32333 if (prev_loc)
32334 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32335 text_end_label, &range_list_added, true);
32338 if (switch_cold_ranges)
32340 const char *prev_loc = cold_text_section_label;
32341 const char *loc;
32342 unsigned idx;
32344 FOR_EACH_VEC_ELT (*switch_cold_ranges, idx, loc)
32345 if (prev_loc)
32347 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32348 loc, &range_list_added, true);
32349 prev_loc = NULL;
32351 else
32352 prev_loc = loc;
32354 if (prev_loc)
32355 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32356 cold_end_label, &range_list_added, true);
32359 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
32361 if (fde->ignored_debug)
32362 continue;
32363 if (!fde->in_std_section)
32364 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
32365 fde->dw_fde_end, &range_list_added,
32366 true);
32367 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
32368 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
32369 fde->dw_fde_second_end, &range_list_added,
32370 true);
32373 if (range_list_added)
32375 /* We need to give .debug_loc and .debug_ranges an appropriate
32376 "base address". Use zero so that these addresses become
32377 absolute. Historically, we've emitted the unexpected
32378 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
32379 Emit both to give time for other tools to adapt. */
32380 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
32381 if (! dwarf_strict && dwarf_version < 4)
32382 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
32384 add_ranges (NULL);
32385 have_multiple_function_sections = true;
32389 /* AIX Assembler inserts the length, so adjust the reference to match the
32390 offset expected by debuggers. */
32391 strcpy (dl_section_ref, debug_line_section_label);
32392 if (XCOFF_DEBUGGING_INFO)
32393 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
32395 if (debug_info_level >= DINFO_LEVEL_TERSE)
32396 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
32397 dl_section_ref);
32399 if (have_macinfo)
32400 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
32401 macinfo_section_label);
32403 if (dwarf_split_debug_info)
32405 if (have_location_lists)
32407 /* Since we generate the loclists in the split DWARF .dwo
32408 file itself, we don't need to generate a loclists_base
32409 attribute for the split compile unit DIE. That attribute
32410 (and using relocatable sec_offset FORMs) isn't allowed
32411 for a split compile unit. Only if the .debug_loclists
32412 section was in the main file, would we need to generate a
32413 loclists_base attribute here (for the full or skeleton
32414 unit DIE). */
32416 /* optimize_location_lists calculates the size of the lists,
32417 so index them first, and assign indices to the entries.
32418 Although optimize_location_lists will remove entries from
32419 the table, it only does so for duplicates, and therefore
32420 only reduces ref_counts to 1. */
32421 index_location_lists (comp_unit_die ());
32424 if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
32425 index_rnglists ();
32427 if (addr_index_table != NULL)
32429 unsigned int index = 0;
32430 addr_index_table
32431 ->traverse_noresize<unsigned int *, index_addr_table_entry>
32432 (&index);
32436 loc_list_idx = 0;
32437 if (have_location_lists)
32439 optimize_location_lists (comp_unit_die ());
32440 /* And finally assign indexes to the entries for -gsplit-dwarf. */
32441 if (dwarf_version >= 5 && dwarf_split_debug_info)
32442 assign_location_list_indexes (comp_unit_die ());
32445 save_macinfo_strings ();
32447 if (dwarf_split_debug_info)
32449 unsigned int index = 0;
32451 /* Add attributes common to skeleton compile_units and
32452 type_units. Because these attributes include strings, it
32453 must be done before freezing the string table. Top-level
32454 skeleton die attrs are added when the skeleton type unit is
32455 created, so ensure it is created by this point. */
32456 add_top_level_skeleton_die_attrs (main_comp_unit_die);
32457 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
32460 /* Output all of the compilation units. We put the main one last so that
32461 the offsets are available to output_pubnames. */
32462 for (node = cu_die_list; node; node = node->next)
32463 output_comp_unit (node->die, 0, NULL);
32465 hash_table<comdat_type_hasher> comdat_type_table (100);
32466 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32468 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
32470 /* Don't output duplicate types. */
32471 if (*slot != HTAB_EMPTY_ENTRY)
32472 continue;
32474 /* Add a pointer to the line table for the main compilation unit
32475 so that the debugger can make sense of DW_AT_decl_file
32476 attributes. */
32477 if (debug_info_level >= DINFO_LEVEL_TERSE)
32478 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
32479 (!dwarf_split_debug_info
32480 ? dl_section_ref
32481 : debug_skeleton_line_section_label));
32483 output_comdat_type_unit (ctnode, false);
32484 *slot = ctnode;
32487 if (dwarf_split_debug_info)
32489 int mark;
32490 struct md5_ctx ctx;
32492 /* Compute a checksum of the comp_unit to use as the dwo_id. */
32493 md5_init_ctx (&ctx);
32494 mark = 0;
32495 die_checksum (comp_unit_die (), &ctx, &mark);
32496 unmark_all_dies (comp_unit_die ());
32497 md5_finish_ctx (&ctx, checksum);
32499 if (dwarf_version < 5)
32501 /* Use the first 8 bytes of the checksum as the dwo_id,
32502 and add it to both comp-unit DIEs. */
32503 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
32504 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
32507 /* Add the base offset of the ranges table to the skeleton
32508 comp-unit DIE. */
32509 if (!vec_safe_is_empty (ranges_table))
32511 if (dwarf_version < 5)
32512 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
32513 ranges_section_label);
32516 output_addr_table ();
32519 /* Output the main compilation unit if non-empty or if .debug_macinfo
32520 or .debug_macro will be emitted. */
32521 output_comp_unit (comp_unit_die (), have_macinfo,
32522 dwarf_split_debug_info ? checksum : NULL);
32524 if (dwarf_split_debug_info && info_section_emitted)
32525 output_skeleton_debug_sections (main_comp_unit_die, checksum);
32527 /* Output the abbreviation table. */
32528 if (vec_safe_length (abbrev_die_table) != 1)
32530 switch_to_section (debug_abbrev_section);
32531 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
32532 output_abbrev_section ();
32535 /* Output location list section if necessary. */
32536 if (have_location_lists)
32538 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
32539 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
32540 /* Output the location lists info. */
32541 switch_to_section (debug_loc_section);
32542 if (dwarf_version >= 5)
32544 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 2);
32545 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 3);
32546 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
32547 dw2_asm_output_data (4, 0xffffffff,
32548 "Initial length escape value indicating "
32549 "64-bit DWARF extension");
32550 dw2_asm_output_delta (dwarf_offset_size, l2, l1,
32551 "Length of Location Lists");
32552 ASM_OUTPUT_LABEL (asm_out_file, l1);
32553 output_dwarf_version ();
32554 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
32555 dw2_asm_output_data (1, 0, "Segment Size");
32556 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
32557 "Offset Entry Count");
32559 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
32560 if (dwarf_version >= 5 && dwarf_split_debug_info)
32562 unsigned int save_loc_list_idx = loc_list_idx;
32563 loc_list_idx = 0;
32564 output_loclists_offsets (comp_unit_die ());
32565 gcc_assert (save_loc_list_idx == loc_list_idx);
32567 output_location_lists (comp_unit_die ());
32568 if (dwarf_version >= 5)
32569 ASM_OUTPUT_LABEL (asm_out_file, l2);
32572 output_pubtables ();
32574 /* Output the address range information if a CU (.debug_info section)
32575 was emitted. We output an empty table even if we had no functions
32576 to put in it. This because the consumer has no way to tell the
32577 difference between an empty table that we omitted and failure to
32578 generate a table that would have contained data. */
32579 if (info_section_emitted)
32581 switch_to_section (debug_aranges_section);
32582 output_aranges ();
32585 /* Output ranges section if necessary. */
32586 if (!vec_safe_is_empty (ranges_table))
32588 if (dwarf_version >= 5)
32590 if (dwarf_split_debug_info)
32592 /* We don't know right now whether there are any
32593 ranges for .debug_rnglists and any for .debug_rnglists.dwo.
32594 Depending on into which of those two belongs the first
32595 ranges_table entry, emit that section first and that
32596 output_rnglists call will return true if the other kind of
32597 ranges needs to be emitted as well. */
32598 bool dwo = (*ranges_table)[0].idx != DW_RANGES_IDX_SKELETON;
32599 if (output_rnglists (generation, dwo))
32600 output_rnglists (generation, !dwo);
32602 else
32603 output_rnglists (generation, false);
32605 else
32606 output_ranges ();
32609 /* Have to end the macro section. */
32610 if (have_macinfo)
32612 switch_to_section (debug_macinfo_section);
32613 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
32614 output_macinfo (!dwarf_split_debug_info ? debug_line_section_label
32615 : debug_skeleton_line_section_label, false);
32616 dw2_asm_output_data (1, 0, "End compilation unit");
32619 /* Output the source line correspondence table. We must do this
32620 even if there is no line information. Otherwise, on an empty
32621 translation unit, we will generate a present, but empty,
32622 .debug_info section. IRIX 6.5 `nm' will then complain when
32623 examining the file. This is done late so that any filenames
32624 used by the debug_info section are marked as 'used'. */
32625 switch_to_section (debug_line_section);
32626 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
32627 if (! output_asm_line_debug_info ())
32628 output_line_info (false);
32630 if (dwarf_split_debug_info && info_section_emitted)
32632 switch_to_section (debug_skeleton_line_section);
32633 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
32634 output_line_info (true);
32637 /* If we emitted any indirect strings, output the string table too. */
32638 if (debug_str_hash || skeleton_debug_str_hash)
32639 output_indirect_strings ();
32640 if (debug_line_str_hash)
32642 switch_to_section (debug_line_str_section);
32643 const enum dwarf_form form = DW_FORM_line_strp;
32644 debug_line_str_hash->traverse<enum dwarf_form,
32645 output_indirect_string> (form);
32648 /* ??? Move lvugid out of dwarf2out_source_line and reset it too? */
32649 symview_upper_bound = 0;
32650 if (zero_view_p)
32651 bitmap_clear (zero_view_p);
32654 /* Returns a hash value for X (which really is a variable_value_struct). */
32656 inline hashval_t
32657 variable_value_hasher::hash (variable_value_struct *x)
32659 return (hashval_t) x->decl_id;
32662 /* Return true if decl_id of variable_value_struct X is the same as
32663 UID of decl Y. */
32665 inline bool
32666 variable_value_hasher::equal (variable_value_struct *x, tree y)
32668 return x->decl_id == DECL_UID (y);
32671 /* Helper function for resolve_variable_value, handle
32672 DW_OP_GNU_variable_value in one location expression.
32673 Return true if exprloc has been changed into loclist. */
32675 static bool
32676 resolve_variable_value_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
32678 dw_loc_descr_ref next;
32679 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = next)
32681 next = loc->dw_loc_next;
32682 if (loc->dw_loc_opc != DW_OP_GNU_variable_value
32683 || loc->dw_loc_oprnd1.val_class != dw_val_class_decl_ref)
32684 continue;
32686 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
32687 if (DECL_CONTEXT (decl) != current_function_decl)
32688 continue;
32690 dw_die_ref ref = lookup_decl_die (decl);
32691 if (ref)
32693 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
32694 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
32695 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
32696 continue;
32698 dw_loc_list_ref l = loc_list_from_tree (decl, 0, NULL);
32699 if (l == NULL)
32700 continue;
32701 if (l->dw_loc_next)
32703 if (AT_class (a) != dw_val_class_loc)
32704 continue;
32705 switch (a->dw_attr)
32707 /* Following attributes allow both exprloc and loclist
32708 classes, so we can change them into a loclist. */
32709 case DW_AT_location:
32710 case DW_AT_string_length:
32711 case DW_AT_return_addr:
32712 case DW_AT_data_member_location:
32713 case DW_AT_frame_base:
32714 case DW_AT_segment:
32715 case DW_AT_static_link:
32716 case DW_AT_use_location:
32717 case DW_AT_vtable_elem_location:
32718 if (prev)
32720 prev->dw_loc_next = NULL;
32721 prepend_loc_descr_to_each (l, AT_loc (a));
32723 if (next)
32724 add_loc_descr_to_each (l, next);
32725 a->dw_attr_val.val_class = dw_val_class_loc_list;
32726 a->dw_attr_val.val_entry = NULL;
32727 a->dw_attr_val.v.val_loc_list = l;
32728 have_location_lists = true;
32729 return true;
32730 /* Following attributes allow both exprloc and reference,
32731 so if the whole expression is DW_OP_GNU_variable_value alone
32732 we could transform it into reference. */
32733 case DW_AT_byte_size:
32734 case DW_AT_bit_size:
32735 case DW_AT_lower_bound:
32736 case DW_AT_upper_bound:
32737 case DW_AT_bit_stride:
32738 case DW_AT_count:
32739 case DW_AT_allocated:
32740 case DW_AT_associated:
32741 case DW_AT_byte_stride:
32742 if (prev == NULL && next == NULL)
32743 break;
32744 /* FALLTHRU */
32745 default:
32746 if (dwarf_strict)
32747 continue;
32748 break;
32750 /* Create DW_TAG_variable that we can refer to. */
32751 gen_decl_die (decl, NULL_TREE, NULL,
32752 lookup_decl_die (current_function_decl));
32753 ref = lookup_decl_die (decl);
32754 if (ref)
32756 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
32757 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
32758 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
32760 continue;
32762 if (prev)
32764 prev->dw_loc_next = l->expr;
32765 add_loc_descr (&prev->dw_loc_next, next);
32766 free_loc_descr (loc, NULL);
32767 next = prev->dw_loc_next;
32769 else
32771 memcpy (loc, l->expr, sizeof (dw_loc_descr_node));
32772 add_loc_descr (&loc, next);
32773 next = loc;
32775 loc = prev;
32777 return false;
32780 /* Attempt to resolve DW_OP_GNU_variable_value using loc_list_from_tree. */
32782 static void
32783 resolve_variable_value (dw_die_ref die)
32785 dw_attr_node *a;
32786 dw_loc_list_ref loc;
32787 unsigned ix;
32789 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
32790 switch (AT_class (a))
32792 case dw_val_class_loc:
32793 if (!resolve_variable_value_in_expr (a, AT_loc (a)))
32794 break;
32795 /* FALLTHRU */
32796 case dw_val_class_loc_list:
32797 loc = AT_loc_list (a);
32798 gcc_assert (loc);
32799 for (; loc; loc = loc->dw_loc_next)
32800 resolve_variable_value_in_expr (a, loc->expr);
32801 break;
32802 default:
32803 break;
32807 /* Attempt to optimize DW_OP_GNU_variable_value refering to
32808 temporaries in the current function. */
32810 static void
32811 resolve_variable_values (void)
32813 if (!variable_value_hash || !current_function_decl)
32814 return;
32816 struct variable_value_struct *node
32817 = variable_value_hash->find_with_hash (current_function_decl,
32818 DECL_UID (current_function_decl));
32820 if (node == NULL)
32821 return;
32823 unsigned int i;
32824 dw_die_ref die;
32825 FOR_EACH_VEC_SAFE_ELT (node->dies, i, die)
32826 resolve_variable_value (die);
32829 /* Helper function for note_variable_value, handle one location
32830 expression. */
32832 static void
32833 note_variable_value_in_expr (dw_die_ref die, dw_loc_descr_ref loc)
32835 for (; loc; loc = loc->dw_loc_next)
32836 if (loc->dw_loc_opc == DW_OP_GNU_variable_value
32837 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
32839 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
32840 dw_die_ref ref = lookup_decl_die (decl);
32841 if (! ref && (flag_generate_lto || flag_generate_offload))
32843 /* ??? This is somewhat a hack because we do not create DIEs
32844 for variables not in BLOCK trees early but when generating
32845 early LTO output we need the dw_val_class_decl_ref to be
32846 fully resolved. For fat LTO objects we'd also like to
32847 undo this after LTO dwarf output. */
32848 gcc_assert (DECL_CONTEXT (decl));
32849 dw_die_ref ctx = lookup_decl_die (DECL_CONTEXT (decl));
32850 gcc_assert (ctx != NULL);
32851 gen_decl_die (decl, NULL_TREE, NULL, ctx);
32852 ref = lookup_decl_die (decl);
32853 gcc_assert (ref != NULL);
32855 if (ref)
32857 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
32858 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
32859 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
32860 continue;
32862 if (VAR_P (decl)
32863 && DECL_CONTEXT (decl)
32864 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
32865 && lookup_decl_die (DECL_CONTEXT (decl)))
32867 if (!variable_value_hash)
32868 variable_value_hash
32869 = hash_table<variable_value_hasher>::create_ggc (10);
32871 tree fndecl = DECL_CONTEXT (decl);
32872 struct variable_value_struct *node;
32873 struct variable_value_struct **slot
32874 = variable_value_hash->find_slot_with_hash (fndecl,
32875 DECL_UID (fndecl),
32876 INSERT);
32877 if (*slot == NULL)
32879 node = ggc_cleared_alloc<variable_value_struct> ();
32880 node->decl_id = DECL_UID (fndecl);
32881 *slot = node;
32883 else
32884 node = *slot;
32886 vec_safe_push (node->dies, die);
32891 /* Walk the tree DIE and note DIEs with DW_OP_GNU_variable_value still
32892 with dw_val_class_decl_ref operand. */
32894 static void
32895 note_variable_value (dw_die_ref die)
32897 dw_die_ref c;
32898 dw_attr_node *a;
32899 dw_loc_list_ref loc;
32900 unsigned ix;
32902 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
32903 switch (AT_class (a))
32905 case dw_val_class_loc_list:
32906 loc = AT_loc_list (a);
32907 gcc_assert (loc);
32908 if (!loc->noted_variable_value)
32910 loc->noted_variable_value = 1;
32911 for (; loc; loc = loc->dw_loc_next)
32912 note_variable_value_in_expr (die, loc->expr);
32914 break;
32915 case dw_val_class_loc:
32916 note_variable_value_in_expr (die, AT_loc (a));
32917 break;
32918 default:
32919 break;
32922 /* Mark children. */
32923 FOR_EACH_CHILD (die, c, note_variable_value (c));
32926 /* Process DWARF dies for CTF generation. */
32928 static void
32929 ctf_debug_do_cu (dw_die_ref die)
32931 dw_die_ref c;
32933 if (!ctf_do_die (die))
32934 return;
32936 FOR_EACH_CHILD (die, c, ctf_do_die (c));
32939 /* Perform any cleanups needed after the early debug generation pass
32940 has run. */
32942 static void
32943 dwarf2out_early_finish (const char *filename)
32945 comdat_type_node *ctnode;
32946 set_early_dwarf s;
32947 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
32949 /* PCH might result in DW_AT_producer string being restored from the
32950 header compilation, so always fill it with empty string initially
32951 and overwrite only here. */
32952 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
32954 if (dwarf_record_gcc_switches)
32955 producer_string = gen_producer_string (lang_hooks.name,
32956 save_decoded_options,
32957 save_decoded_options_count);
32958 else
32959 producer_string = concat (lang_hooks.name, " ", version_string, NULL);
32961 producer->dw_attr_val.v.val_str->refcount--;
32962 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
32964 /* Add the name for the main input file now. We delayed this from
32965 dwarf2out_init to avoid complications with PCH. */
32966 add_filename_attribute (comp_unit_die (), remap_debug_filename (filename));
32967 add_comp_dir_attribute (comp_unit_die ());
32969 /* With LTO early dwarf was really finished at compile-time, so make
32970 sure to adjust the phase after annotating the LTRANS CU DIE. */
32971 if (in_lto_p)
32973 early_dwarf_finished = true;
32974 if (dump_file)
32976 fprintf (dump_file, "LTO EARLY DWARF for %s\n", filename);
32977 print_die (comp_unit_die (), dump_file);
32979 return;
32982 /* Walk through the list of incomplete types again, trying once more to
32983 emit full debugging info for them. */
32984 retry_incomplete_types ();
32986 gen_scheduled_generic_parms_dies ();
32987 gen_remaining_tmpl_value_param_die_attribute ();
32989 /* The point here is to flush out the limbo list so that it is empty
32990 and we don't need to stream it for LTO. */
32991 flush_limbo_die_list ();
32993 /* Add DW_AT_linkage_name for all deferred DIEs. */
32994 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
32996 tree decl = node->created_for;
32997 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
32998 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
32999 ended up in deferred_asm_name before we knew it was
33000 constant and never written to disk. */
33001 && DECL_ASSEMBLER_NAME (decl))
33003 add_linkage_attr (node->die, decl);
33004 move_linkage_attr (node->die);
33007 deferred_asm_name = NULL;
33009 if (flag_eliminate_unused_debug_types)
33010 prune_unused_types ();
33012 /* Generate separate COMDAT sections for type DIEs. */
33013 if (use_debug_types)
33015 break_out_comdat_types (comp_unit_die ());
33017 /* Each new type_unit DIE was added to the limbo die list when created.
33018 Since these have all been added to comdat_type_list, clear the
33019 limbo die list. */
33020 limbo_die_list = NULL;
33022 /* For each new comdat type unit, copy declarations for incomplete
33023 types to make the new unit self-contained (i.e., no direct
33024 references to the main compile unit). */
33025 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
33026 copy_decls_for_unworthy_types (ctnode->root_die);
33027 copy_decls_for_unworthy_types (comp_unit_die ());
33029 /* In the process of copying declarations from one unit to another,
33030 we may have left some declarations behind that are no longer
33031 referenced. Prune them. */
33032 prune_unused_types ();
33035 /* Traverse the DIE's and note DIEs with DW_OP_GNU_variable_value still
33036 with dw_val_class_decl_ref operand. */
33037 note_variable_value (comp_unit_die ());
33038 for (limbo_die_node *node = cu_die_list; node; node = node->next)
33039 note_variable_value (node->die);
33040 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
33041 note_variable_value (ctnode->root_die);
33042 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
33043 note_variable_value (node->die);
33045 /* The AT_pubnames attribute needs to go in all skeleton dies, including
33046 both the main_cu and all skeleton TUs. Making this call unconditional
33047 would end up either adding a second copy of the AT_pubnames attribute, or
33048 requiring a special case in add_top_level_skeleton_die_attrs. */
33049 if (!dwarf_split_debug_info)
33050 add_AT_pubnames (comp_unit_die ());
33052 /* The early debug phase is now finished. */
33053 early_dwarf_finished = true;
33054 if (dump_file)
33056 fprintf (dump_file, "EARLY DWARF for %s\n", filename);
33057 print_die (comp_unit_die (), dump_file);
33060 /* Generate CTF/BTF debug info. */
33061 if ((ctf_debug_info_level > CTFINFO_LEVEL_NONE
33062 || btf_debuginfo_p ()) && lang_GNU_C ())
33064 ctf_debug_init ();
33065 ctf_debug_do_cu (comp_unit_die ());
33066 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
33067 ctf_debug_do_cu (node->die);
33068 /* Post process the debug data in the CTF container if necessary. */
33069 ctf_debug_init_postprocess (btf_debuginfo_p ());
33071 ctf_debug_early_finish (filename);
33074 /* Do not generate DWARF assembler now when not producing LTO bytecode. */
33075 if ((!flag_generate_lto && !flag_generate_offload)
33076 /* FIXME: Disable debug info generation for (PE-)COFF targets since the
33077 copy_lto_debug_sections operation of the simple object support in
33078 libiberty is not implemented for them yet. */
33079 || TARGET_PECOFF || TARGET_COFF)
33080 return;
33082 /* Now as we are going to output for LTO initialize sections and labels
33083 to the LTO variants. We don't need a random-seed postfix as other
33084 LTO sections as linking the LTO debug sections into one in a partial
33085 link is fine. */
33086 init_sections_and_labels (true);
33088 /* The output below is modeled after dwarf2out_finish with all
33089 location related output removed and some LTO specific changes.
33090 Some refactoring might make both smaller and easier to match up. */
33092 base_types.truncate (0);
33093 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
33094 mark_base_types (ctnode->root_die);
33095 mark_base_types (comp_unit_die ());
33096 move_marked_base_types ();
33098 /* Traverse the DIE's and add sibling attributes to those DIE's
33099 that have children. */
33100 add_sibling_attributes (comp_unit_die ());
33101 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
33102 add_sibling_attributes (node->die);
33103 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
33104 add_sibling_attributes (ctnode->root_die);
33106 /* AIX Assembler inserts the length, so adjust the reference to match the
33107 offset expected by debuggers. */
33108 strcpy (dl_section_ref, debug_line_section_label);
33109 if (XCOFF_DEBUGGING_INFO)
33110 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
33112 if (debug_info_level >= DINFO_LEVEL_TERSE)
33113 add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list, dl_section_ref);
33115 if (have_macinfo)
33116 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
33117 macinfo_section_label);
33119 save_macinfo_strings ();
33121 if (dwarf_split_debug_info)
33123 unsigned int index = 0;
33124 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
33127 /* Output all of the compilation units. We put the main one last so that
33128 the offsets are available to output_pubnames. */
33129 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
33130 output_comp_unit (node->die, 0, NULL);
33132 hash_table<comdat_type_hasher> comdat_type_table (100);
33133 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
33135 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
33137 /* Don't output duplicate types. */
33138 if (*slot != HTAB_EMPTY_ENTRY)
33139 continue;
33141 /* Add a pointer to the line table for the main compilation unit
33142 so that the debugger can make sense of DW_AT_decl_file
33143 attributes. */
33144 if (debug_info_level >= DINFO_LEVEL_TERSE)
33145 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
33146 (!dwarf_split_debug_info
33147 ? debug_line_section_label
33148 : debug_skeleton_line_section_label));
33150 output_comdat_type_unit (ctnode, true);
33151 *slot = ctnode;
33154 /* Stick a unique symbol to the main debuginfo section. */
33155 compute_comp_unit_symbol (comp_unit_die ());
33157 /* Output the main compilation unit. We always need it if only for
33158 the CU symbol. */
33159 output_comp_unit (comp_unit_die (), true, NULL);
33161 /* Output the abbreviation table. */
33162 if (vec_safe_length (abbrev_die_table) != 1)
33164 switch_to_section (debug_abbrev_section);
33165 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
33166 output_abbrev_section ();
33169 /* Have to end the macro section. */
33170 if (have_macinfo)
33172 /* We have to save macinfo state if we need to output it again
33173 for the FAT part of the object. */
33174 vec<macinfo_entry, va_gc> *saved_macinfo_table = macinfo_table;
33175 if (flag_fat_lto_objects)
33176 macinfo_table = macinfo_table->copy ();
33178 switch_to_section (debug_macinfo_section);
33179 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
33180 output_macinfo (debug_line_section_label, true);
33181 dw2_asm_output_data (1, 0, "End compilation unit");
33183 if (flag_fat_lto_objects)
33185 vec_free (macinfo_table);
33186 macinfo_table = saved_macinfo_table;
33190 /* Emit a skeleton debug_line section. */
33191 switch_to_section (debug_line_section);
33192 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
33193 output_line_info (true);
33195 /* If we emitted any indirect strings, output the string table too. */
33196 if (debug_str_hash || skeleton_debug_str_hash)
33197 output_indirect_strings ();
33198 if (debug_line_str_hash)
33200 switch_to_section (debug_line_str_section);
33201 const enum dwarf_form form = DW_FORM_line_strp;
33202 debug_line_str_hash->traverse<enum dwarf_form,
33203 output_indirect_string> (form);
33206 /* Switch back to the text section. */
33207 switch_to_section (text_section);
33210 /* Reset all state within dwarf2out.cc so that we can rerun the compiler
33211 within the same process. For use by toplev::finalize. */
33213 void
33214 dwarf2out_cc_finalize (void)
33216 last_var_location_insn = NULL;
33217 cached_next_real_insn = NULL;
33218 used_rtx_array = NULL;
33219 incomplete_types = NULL;
33220 debug_info_section = NULL;
33221 debug_skeleton_info_section = NULL;
33222 debug_abbrev_section = NULL;
33223 debug_skeleton_abbrev_section = NULL;
33224 debug_aranges_section = NULL;
33225 debug_addr_section = NULL;
33226 debug_macinfo_section = NULL;
33227 debug_line_section = NULL;
33228 debug_skeleton_line_section = NULL;
33229 debug_loc_section = NULL;
33230 debug_pubnames_section = NULL;
33231 debug_pubtypes_section = NULL;
33232 debug_str_section = NULL;
33233 debug_line_str_section = NULL;
33234 debug_str_dwo_section = NULL;
33235 debug_str_offsets_section = NULL;
33236 debug_ranges_section = NULL;
33237 debug_ranges_dwo_section = NULL;
33238 debug_frame_section = NULL;
33239 fde_vec = NULL;
33240 debug_str_hash = NULL;
33241 debug_line_str_hash = NULL;
33242 skeleton_debug_str_hash = NULL;
33243 dw2_string_counter = 0;
33244 have_multiple_function_sections = false;
33245 in_text_section_p = false;
33246 cold_text_section = NULL;
33247 last_text_label = NULL;
33248 last_cold_label = NULL;
33249 switch_text_ranges = NULL;
33250 switch_cold_ranges = NULL;
33251 current_unit_personality = NULL;
33253 early_dwarf = false;
33254 early_dwarf_finished = false;
33256 next_die_offset = 0;
33257 single_comp_unit_die = NULL;
33258 comdat_type_list = NULL;
33259 limbo_die_list = NULL;
33260 file_table = NULL;
33261 decl_die_table = NULL;
33262 common_block_die_table = NULL;
33263 decl_loc_table = NULL;
33264 call_arg_locations = NULL;
33265 call_arg_loc_last = NULL;
33266 call_site_count = -1;
33267 tail_call_site_count = -1;
33268 cached_dw_loc_list_table = NULL;
33269 abbrev_die_table = NULL;
33270 delete dwarf_proc_stack_usage_map;
33271 dwarf_proc_stack_usage_map = NULL;
33272 line_info_label_num = 0;
33273 cur_line_info_table = NULL;
33274 text_section_line_info = NULL;
33275 cold_text_section_line_info = NULL;
33276 separate_line_info = NULL;
33277 info_section_emitted = false;
33278 pubname_table = NULL;
33279 pubtype_table = NULL;
33280 macinfo_table = NULL;
33281 ranges_table = NULL;
33282 ranges_by_label = NULL;
33283 rnglist_idx = 0;
33284 have_location_lists = false;
33285 loclabel_num = 0;
33286 poc_label_num = 0;
33287 last_emitted_file = NULL;
33288 label_num = 0;
33289 tmpl_value_parm_die_table = NULL;
33290 generic_type_instances = NULL;
33291 frame_pointer_fb_offset = 0;
33292 frame_pointer_fb_offset_valid = false;
33293 base_types.release ();
33294 XDELETEVEC (producer_string);
33295 producer_string = NULL;
33296 output_line_info_generation = 0;
33297 init_sections_and_labels_generation = 0;
33300 #include "gt-dwarf2out.h"